From nobody Wed Dec 17 12:44:21 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B5B7BC4167B for ; Mon, 27 Nov 2023 22:09:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233604AbjK0WJk (ORCPT ); Mon, 27 Nov 2023 17:09:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46832 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233605AbjK0WJ2 (ORCPT ); Mon, 27 Nov 2023 17:09:28 -0500 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3991BD63 for ; Mon, 27 Nov 2023 14:09:35 -0800 (PST) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5cd573c2cccso29210047b3.1 for ; Mon, 27 Nov 2023 14:09:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701122974; x=1701727774; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=MEmgO09+w7D/FXE2RqMfg/xWGErZ4jMQFLdNI3fBx44=; b=YheQzlfsb9155A0nVfE/EJ9mhvmONOIFZ+JLlSedA7E2YsQBZM6fiprcBmWAmQUEs4 KXqJhg0eNCW7DORy9Y+W2kJV8E9AsKTXfP1nOHUubRdIyX4WDUimtYL/RtuIIMLfUcdA 5yPAYK/QfVeQ5PsF5oF8P9qmIZrGV/j3pSEL99UT/i5UPjaus+q+AYQ2wDcu7F/f3eEA cWhK5rsMhlFoIi9clg1MuKQHFCe0TGvZm4QbkVHBHuZeJ6Dy8K2J3I/BQV4AWB3xEx9M EG8OizUGXZZNqARwJBn+YrZz0+tG3Wz95PO2h2mMDGyV+dTHEUKhUrpN9j2LPDigdouo ePdg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701122974; x=1701727774; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=MEmgO09+w7D/FXE2RqMfg/xWGErZ4jMQFLdNI3fBx44=; b=liO6IQV3/YWHY1Cf6OU4WNbNLCcpF5CD5nSqHyuDKkmknFOvI7zE547IS3F1hFi1jR O2/1TiZTQ7Fs9NuBsrhVGz3CWA90F6UmF07wssb78ghNY86kAv6hwH1ArpoW1L0MIK2y PoN7G1KkTm7E/G27Yr6rwE/vbFSRtkJBqkNeaifpkeyrg4nkenfOftTC2/4ZZY1PPq49 9ew4NbrmssWQIHwkaE1ff/xzCMPuboc/KXDfNWvsrp434KV7wDlkooZaYriSm/bElYRK sJKr6ll18oC453/7wnZXIKKlv1LGqukEJ87PI8A3RdzJFI7RDe0hg/jB3LmI1AeFXaVe tsDg== X-Gm-Message-State: AOJu0YzdHOseJx3jtRErqbCYkzk25occldH8fuaDHcRspNzr0Q9dkerA 3hjqAjEzqV+RAPAqoUoJLZrvy18Oi0aY X-Google-Smtp-Source: AGHT+IHus6n9B6wO57xmLGcAOXuveh7hgGsxA/NX82QWYM0uv7NsouBu6yln/ZjYTA96ykxWGX/23AO7mqbX X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:829:6e77:9093:f39b]) (user=irogers job=sendgmr) by 2002:a05:690c:2b0a:b0:5d0:8fb3:559c with SMTP id em10-20020a05690c2b0a00b005d08fb3559cmr120925ywb.0.1701122974473; Mon, 27 Nov 2023 14:09:34 -0800 (PST) Date: Mon, 27 Nov 2023 14:08:18 -0800 In-Reply-To: <20231127220902.1315692-1-irogers@google.com> Message-Id: <20231127220902.1315692-7-irogers@google.com> Mime-Version: 1.0 References: <20231127220902.1315692-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.rc1.413.gea7ed67945-goog Subject: [PATCH v5 06/50] tools lib api: Add io_dir an allocation free readdir alternative From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Kajol Jain , Athira Rajeev , Huacai Chen , Masami Hiramatsu , Vincent Whitchurch , "Steinar H. Gunderson" , Liam Howlett , Miguel Ojeda , Colin Ian King , Dmitrii Dolgov <9erthalion6@gmail.com>, Yang Jihong , Ming Wang , James Clark , K Prateek Nayak , Sean Christopherson , Leo Yan , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Li Dong , Sandipan Das , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Guilherme Amadio Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" glibc's opendir allocates a minimum of 32kb, when called recursively for a directory tree the memory consumption can add up - nearly 300kb during perf start-up when processing modules. Add a stack allocated variant of readdir sized a little more than 1kb. Signed-off-by: Ian Rogers Acked-by: Namhyung Kim --- tools/lib/api/Makefile | 2 +- tools/lib/api/io_dir.h | 75 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 tools/lib/api/io_dir.h diff --git a/tools/lib/api/Makefile b/tools/lib/api/Makefile index 044860ac1ed1..186aa407de8c 100644 --- a/tools/lib/api/Makefile +++ b/tools/lib/api/Makefile @@ -99,7 +99,7 @@ install_lib: $(LIBFILE) $(call do_install_mkdir,$(libdir_SQ)); \ cp -fpR $(LIBFILE) $(DESTDIR)$(libdir_SQ) =20 -HDRS :=3D cpu.h debug.h io.h +HDRS :=3D cpu.h debug.h io.h io_dir.h FD_HDRS :=3D fd/array.h FS_HDRS :=3D fs/fs.h fs/tracing_path.h INSTALL_HDRS_PFX :=3D $(DESTDIR)$(prefix)/include/api diff --git a/tools/lib/api/io_dir.h b/tools/lib/api/io_dir.h new file mode 100644 index 000000000000..f3479006edb6 --- /dev/null +++ b/tools/lib/api/io_dir.h @@ -0,0 +1,75 @@ +/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ +/* + * Lightweight directory reading library. + */ +#ifndef __API_IO_DIR__ +#define __API_IO_DIR__ + +#include +#include +#include +#include +#include + +struct io_dirent64 { + ino64_t d_ino; /* 64-bit inode number */ + off64_t d_off; /* 64-bit offset to next structure */ + unsigned short d_reclen; /* Size of this dirent */ + unsigned char d_type; /* File type */ + char d_name[NAME_MAX + 1]; /* Filename (null-terminated) */ +}; + +struct io_dir { + int dirfd; + ssize_t available_bytes; + struct io_dirent64 *next; + struct io_dirent64 buff[4]; +}; + +static inline void io_dir__init(struct io_dir *iod, int dirfd) +{ + iod->dirfd =3D dirfd; + iod->available_bytes =3D 0; +} + +static inline void io_dir__rewinddir(struct io_dir *iod) +{ + lseek(iod->dirfd, 0, SEEK_SET); + iod->available_bytes =3D 0; +} + +static inline struct io_dirent64 *io_dir__readdir(struct io_dir *iod) +{ + struct io_dirent64 *entry; + + if (iod->available_bytes <=3D 0) { + ssize_t rc =3D getdents64(iod->dirfd, iod->buff, sizeof(iod->buff)); + + if (rc <=3D 0) + return NULL; + iod->available_bytes =3D rc; + iod->next =3D iod->buff; + } + entry =3D iod->next; + iod->next =3D (struct io_dirent64 *)((char *)entry + entry->d_reclen); + iod->available_bytes -=3D entry->d_reclen; + return entry; +} + +static inline bool io_dir__is_dir(const struct io_dir *iod, struct io_dire= nt64 *dent) +{ + if (dent->d_type =3D=3D DT_UNKNOWN) { + struct stat st; + + if (fstatat(iod->dirfd, dent->d_name, &st, /*flags=3D*/0)) + return false; + + if (S_ISDIR(st.st_mode)) { + dent->d_type =3D DT_DIR; + return true; + } + } + return dent->d_type =3D=3D DT_DIR; +} + +#endif --=20 2.43.0.rc1.413.gea7ed67945-goog