From nobody Thu Jan 1 08:57:43 2026 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 4C12BC25B47 for ; Tue, 24 Oct 2023 22:25:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344508AbjJXWZ4 (ORCPT ); Tue, 24 Oct 2023 18:25:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45646 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234983AbjJXWZ3 (ORCPT ); Tue, 24 Oct 2023 18:25:29 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DADE610D4 for ; Tue, 24 Oct 2023 15:24:54 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-da03c5ae220so243979276.1 for ; Tue, 24 Oct 2023 15:24:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186293; x=1698791093; 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=te3vsO4FFSO2xyEIikKiywGT5AKUPIsHpoNC65majkY=; b=EU+aw5wBe8FZvBL8crMRweg+EtN2PXjPDp24t4tZS6NGh7DtAdvSifsXhDpa7oPG2k KkOOlBOnHKnfaheuCAgsfj9gHTQ4vEBNywX9PstW6/fHnbM+k/a0K/Rmj/TtX16vgSHW PfNKFSB8NLH+JrFWJ0liIgra8hQLQtpmh5gug/6yNu2u3YHiT/A/UVZ4NJFSHHxEqkAV v4++5qYr61lq72+p72jh6f2VMbCdX3hxx4EKP23L3aFlSsmIFsUXqO+MitPidgKrY7YL x2pwSUk7v0woASAcnX9mwYdqqd7PcBY4Ju40gJRGohUmUgsyKaDNOAUDoEVgdNu/xR8S HI9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186293; x=1698791093; 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=te3vsO4FFSO2xyEIikKiywGT5AKUPIsHpoNC65majkY=; b=g2nDj8JgQyxkajFyrXcQD8ySDLlHLOSmZkEP0aCla0SAS7+bTEtwN0JGXxn5aDLQU9 M27Mi6JtXp5iok9akFue4zYlAyGMB2wLG6gdGQxdSFLmwehDhFoIbt8nNd9PdF7fkf75 3n2S08PUPZbOU0bk+oLJvOiItxjiG5sc+OH7iCcZ37oJDldG1AXHajqCxPoxIM0GZEfQ 1BxUf8vJpgR3JR/7zRjNT0tt2Ishx87gpyZ3VDbauMbXJ3TJIDBgCommq4uLdGPwc3rd pofhs2Ujy3/Z8DFN3rvYqVs1oo78jLprKe3HweyPlTpzNsCx0F8w2/fQLpukj0tT4e+/ QXng== X-Gm-Message-State: AOJu0YyAzMX0CunaliaZg0+39WrLpSQ6wtQJKbySeSjIHbSkDEljmtJN K76NtuiC90eQqP/bOYzbmQoEGMVpOm+r X-Google-Smtp-Source: AGHT+IG0qlkZIkteZoBsDEjUJuPv/qSoyKpAZj2pIJc3FbHR64e6ge1+0/2JSt3i23IoBIy48WvXUELizceV X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a25:34d1:0:b0:d9a:6360:485b with SMTP id b200-20020a2534d1000000b00d9a6360485bmr385698yba.2.1698186293540; Tue, 24 Oct 2023 15:24:53 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:21 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-19-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 18/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 , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org 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 --- tools/lib/api/Makefile | 2 +- tools/lib/api/io_dir.h | 72 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 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..8a70c0718df5 --- /dev/null +++ b/tools/lib/api/io_dir.h @@ -0,0 +1,72 @@ +/* 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, const struct i= o_dirent64 *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; + + return S_ISDIR(st.st_mode); + } + return dent->d_type =3D=3D DT_DIR; +} + +#endif --=20 2.42.0.758.gaed0368e0e-goog