From nobody Fri Dec 19 07:50:32 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 B1EC6C4167B for ; Thu, 7 Dec 2023 05:04:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230262AbjLGFEp (ORCPT ); Thu, 7 Dec 2023 00:04:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44778 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229449AbjLGFEm (ORCPT ); Thu, 7 Dec 2023 00:04:42 -0500 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 553DDD44 for ; Wed, 6 Dec 2023 21:04:48 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-db53919e062so751910276.0 for ; Wed, 06 Dec 2023 21:04:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701925487; x=1702530287; 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=gJ09ILS0FKw69nllEGHZTddJyQyifMD6obVQZgpi4Qw=; b=ljFipjXPu2ocEJjGS5+bo4ZvUR5mU9WkFW6EY0cXddnbdqCN7uxP4VpZYVQaSh+bN2 CZUTGz6UU5H0lp1cXq91jVlmEXpsne41KYWUCiZDhNp2Mr8XyAxdrUqY8R1lMeOAe6ae O+XT+QLgm5NShAQcqAdRq8fhTOrhiDufhvsM7lIfHKdZQ09xG6APHhuyotWzh2SkGZDs tonIxluC68vbr8xowwfdA3vDmRmPp9/K/ZOZcFGI6QZnFoqirJdEQQYHb7HEZZxvnT5w GIP2XIRqHKCSLDzZDX/8XYAt9pIF+8bLljLI/UmMB0YDm0NZaBbB2tWrtLXzQH9+G475 vhEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701925487; x=1702530287; 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=gJ09ILS0FKw69nllEGHZTddJyQyifMD6obVQZgpi4Qw=; b=KaTFQYLnmngPKuT38q+pCyw4HdE3f3VjyPlIoM0Ya+WZPekSZdQ3m6uZjBrQGLzfP8 PlIqmPEXgoEgIjG+ER4+o1EItpseZXBnxyaNRZr57xoBuFxng6CtENGqZfd9V5R4QGWV vWYWIjuB8SPy0lKlAKX8YM1UoO7Fa+tpr66CFjr3YQYoYZRv9t383NsNvyCrN7Tp5qLJ 0iPb3EBFm3nKRBt/boq0ouEzrm/9BpjypZRX8s0eGc9LMP7Q+sD/K3f6j0dRTSiVBEe3 MOG1yEOtdZtH5isG35HQ2uAS6TJ7KxK7jNTuJJbwUG/1TudQwg0QNVR8hHHTgTJDxCdB RRuw== X-Gm-Message-State: AOJu0YwmHGQd5ihdyoBN/JFhHk77dZhvdjU1om1xnttmH1o3oY4qssmV IIC7z7TB5aqvBTZe/MdioB1XRfn5hRFW X-Google-Smtp-Source: AGHT+IE25ICHr+L0rrthsfiMJT9vor7826Id8YRj/BihCnqEuwJ+wuOTJjyX1ATd/qisp8dd7xqJkTPlrH7E X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:35bf:293e:7696:34e9]) (user=irogers job=sendgmr) by 2002:a25:d144:0:b0:db7:dce9:76cd with SMTP id i65-20020a25d144000000b00db7dce976cdmr24086ybg.9.1701925487528; Wed, 06 Dec 2023 21:04:47 -0800 (PST) Date: Wed, 6 Dec 2023 21:04:28 -0800 In-Reply-To: <20231207050433.1426834-1-irogers@google.com> Message-Id: <20231207050433.1426834-2-irogers@google.com> Mime-Version: 1.0 References: <20231207050433.1426834-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.rc2.451.g8631bc7472-goog Subject: [PATCH v1 1/6] tools build: Add a feature test for getdents64 From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , "Masami Hiramatsu (Google)" , Thomas Richter , Quentin Monnet , Fangrui Song , Kan Liang , Yanteng Si , K Prateek Nayak , Artem Savkov , James Clark , Ravi Bangoria , 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" getdents64 may be missing from certain libcs, add a feature test to determine when such a libc is being used. Signed-off-by: Ian Rogers --- tools/build/Makefile.feature | 1 + tools/build/feature/Makefile | 4 ++++ tools/build/feature/test-all.c | 5 +++++ tools/build/feature/test-getdents64.c | 12 ++++++++++++ 4 files changed, 22 insertions(+) create mode 100644 tools/build/feature/test-getdents64.c diff --git a/tools/build/Makefile.feature b/tools/build/Makefile.feature index 64df118376df..f051d4d8c71c 100644 --- a/tools/build/Makefile.feature +++ b/tools/build/Makefile.feature @@ -58,6 +58,7 @@ FEATURE_TESTS_BASIC :=3D \ pthread-attr-setaffinity-np \ pthread-barrier \ reallocarray \ + getdents64 \ stackprotector-all \ timerfd \ libdw-dwarf-unwind \ diff --git a/tools/build/feature/Makefile b/tools/build/feature/Makefile index 37722e509eb9..5efe89c3b0a6 100644 --- a/tools/build/feature/Makefile +++ b/tools/build/feature/Makefile @@ -21,6 +21,7 @@ FILES=3D \ test-disassembler-four-args.bin \ test-disassembler-init-styled.bin \ test-reallocarray.bin \ + test-getdents64.bin \ test-libbfd-liberty.bin \ test-libbfd-liberty-z.bin \ test-cplus-demangle.bin \ @@ -262,6 +263,9 @@ $(OUTPUT)test-disassembler-init-styled.bin: $(OUTPUT)test-reallocarray.bin: $(BUILD) =20 +$(OUTPUT)test-getdents64.bin: + $(BUILD) + $(OUTPUT)test-libbfd-liberty.bin: $(CC) $(CFLAGS) -Wall -Werror -o $@ test-libbfd.c -DPACKAGE=3D'"perf"' $(= LDFLAGS) -lbfd -ldl -liberty =20 diff --git a/tools/build/feature/test-all.c b/tools/build/feature/test-all.c index 6f4bf386a3b5..c65096f75032 100644 --- a/tools/build/feature/test-all.c +++ b/tools/build/feature/test-all.c @@ -166,6 +166,10 @@ # include "test-reallocarray.c" #undef main =20 +#define main main_test_getdents64 +# include "test-getdents64.c" +#undef main + #define main main_test_disassembler_four_args # include "test-disassembler-four-args.c" #undef main @@ -216,6 +220,7 @@ int main(int argc, char *argv[]) main_test_setns(); main_test_libaio(); main_test_reallocarray(); + main_test_getdents64(); main_test_disassembler_four_args(); main_test_libzstd(); =20 diff --git a/tools/build/feature/test-getdents64.c b/tools/build/feature/te= st-getdents64.c new file mode 100644 index 000000000000..f7c9df1e2f05 --- /dev/null +++ b/tools/build/feature/test-getdents64.c @@ -0,0 +1,12 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#define _GNU_SOURCE +#include + +int main(void) +{ + char buf[128]; + return (int)getdents64(0, buf, sizeof(buf)); +} + +#undef _GNU_SOURCE --=20 2.43.0.rc2.451.g8631bc7472-goog From nobody Fri Dec 19 07:50:32 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 39D84C4167B for ; Thu, 7 Dec 2023 05:05:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230304AbjLGFEw (ORCPT ); Thu, 7 Dec 2023 00:04:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44798 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230264AbjLGFEr (ORCPT ); Thu, 7 Dec 2023 00:04:47 -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 96DEDD44 for ; Wed, 6 Dec 2023 21:04:53 -0800 (PST) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5d3a1e5f8d6so3973157b3.3 for ; Wed, 06 Dec 2023 21:04:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701925493; x=1702530293; 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=HLfF4gdeUWNr8FF8ACObQvFWnydI6Js6QIkQSQodKEI=; b=16JKr/OAlmIySJ2uZLXZC4p70yZiecAN8EhnkomFKLRuI8/eJ+HXfBmc9iI1Cq67rv 1T+ncKFZBzub0uuI67C/Rrf8+HL+kC1+1EqjdnqUxmJ3pi9L9/Rk2RPxBbsbizJ52Fkg 11JhkT1Z12L1LsPQQielsZdIbFLpz+B5rV8wxCJ7BEomwakbQlHHrx+ZFVgY/akc2lNG dafpmPc5KgqTa9V9dx9FVoqVmKL+WvcmeyWJhwIfhmjUc07tO+YB+PbDQrLwND2A+HNR oYA0PxhpM3VYtNN19I+CY83SNFgJ6ep0KgUE3vrs2pFRS3r2YQyxym5EvW8sJNcj1I6J WxCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701925493; x=1702530293; 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=HLfF4gdeUWNr8FF8ACObQvFWnydI6Js6QIkQSQodKEI=; b=iEhRYRed2mT5NVF2wk98XY4NYHbb1aEdx088aizpD8EvgIMRDJgkAJlhF2xfLB/Uvf hXYUaqT2xGEJBLKzb41prVzORAFu9fNMTNnWEl7rTOLKiJjQHaUpzXKbeQYcW36U4cVE 0KFiRQidsVRJqiGgG2nDpjr4joX+gNr3im7Jet09Rk3o//xNAKbDHz8zYeMTSuk3V2h8 OqQ/LJPwTteFXKpUIYeWrjVeo/d0nTrJTkcvckycbFMRbexySW+JGndSwF1EqmegLWbw DNIVFHDnFjR6NAAEomClZ3FzZ5SLAjgDBb/UXNJFozjREwSgb6Jj/zvBuJp4gIAPCy8n HMDQ== X-Gm-Message-State: AOJu0YwNce6hrN376jGGUQUkvO/EjI7Jq+IWqbDn+BbtSKX+D1yyYcvj Yh/dlHSM7oB0x6diEVLjH5igrTGRPypa X-Google-Smtp-Source: AGHT+IFJ8VLzkhmv5lIHBwHao+8hbxbTzxh0PLIX/Y/8B0uG5Cn/P5rXMoDTUFtOLFwHal2ivb+PmHh5k+sj X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:35bf:293e:7696:34e9]) (user=irogers job=sendgmr) by 2002:a81:ad14:0:b0:5d5:6a4c:b0b3 with SMTP id l20-20020a81ad14000000b005d56a4cb0b3mr26965ywh.8.1701925492804; Wed, 06 Dec 2023 21:04:52 -0800 (PST) Date: Wed, 6 Dec 2023 21:04:29 -0800 In-Reply-To: <20231207050433.1426834-1-irogers@google.com> Message-Id: <20231207050433.1426834-3-irogers@google.com> Mime-Version: 1.0 References: <20231207050433.1426834-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.rc2.451.g8631bc7472-goog Subject: [PATCH v1 2/6] 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 , "Masami Hiramatsu (Google)" , Thomas Richter , Quentin Monnet , Fangrui Song , Kan Liang , Yanteng Si , K Prateek Nayak , Artem Savkov , James Clark , Ravi Bangoria , 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. As getdents64 may be missing from libc, add support using syscall. Signed-off-by: Ian Rogers --- tools/lib/api/Makefile | 2 +- tools/lib/api/io_dir.h | 84 ++++++++++++++++++++++++++++++++++++++ tools/perf/Makefile.config | 4 ++ 3 files changed, 89 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..9b702497e05c --- /dev/null +++ b/tools/lib/api/io_dir.h @@ -0,0 +1,84 @@ +/* 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 + +#ifndef HAVE_GETDENTS64 +#include + +static inline ssize_t getdents64(int fd, void *dirp, size_t count) +{ + return syscall(SYS_getdents64, fd, dirp, count); +} +#endif + +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 diff --git a/tools/perf/Makefile.config b/tools/perf/Makefile.config index aa55850fbc21..1cef1ab4ddb7 100644 --- a/tools/perf/Makefile.config +++ b/tools/perf/Makefile.config @@ -357,6 +357,10 @@ ifeq ($(feature-stackprotector-all), 1) CORE_CFLAGS +=3D -fstack-protector-all endif =20 +ifeq ($(feature-getdents64), 1) + CFLAGS +=3D -DHAVE_GETDENTS64 +endif + ifeq ($(DEBUG),0) ifeq ($(feature-fortify-source), 1) CORE_CFLAGS +=3D -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3D2 --=20 2.43.0.rc2.451.g8631bc7472-goog From nobody Fri Dec 19 07:50:32 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 27673C4167B for ; Thu, 7 Dec 2023 05:05:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230340AbjLGFE5 (ORCPT ); Thu, 7 Dec 2023 00:04:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44838 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230274AbjLGFEt (ORCPT ); Thu, 7 Dec 2023 00:04:49 -0500 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D02BBD62 for ; Wed, 6 Dec 2023 21:04:55 -0800 (PST) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5d42c43d8daso2129047b3.0 for ; Wed, 06 Dec 2023 21:04:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701925495; x=1702530295; 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=vr0pLcjARWkwRyuRy64mQ5hPn4FBvP7Yrmix4YMSHq8=; b=tzLVYRepPMTmJKb7UMmnkBF+o2AIHgW8nOnfnUOdz9sIJNjLQiO/H1dHUgM46pgmu6 wFWPg82dOuZ4mZgufU5a9GASZpUPegL3jki9oN3ckLC1zjgCVsV8fIZnJDuXuIvo4zkk Isj1a1FQFsTvJ7HBci0K+MkzVOkWRAhIuWdaUw4qzQ3UgEBKxr829aBYnGtwJfThEFhW z+D1pxDM7jNrowFzhiFOK+DwteSh35x4maPZ0IFL/kxG9zDGHSHRLxO0YGPs1eeIVBBv 7vbKSjgeJFxFzGpxvhaJXs82GbiegClW9EIIS2qQHUZTiC0K9E2x623A8L1RJh2wjyg5 5UBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701925495; x=1702530295; 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=vr0pLcjARWkwRyuRy64mQ5hPn4FBvP7Yrmix4YMSHq8=; b=BvhYiCahqHiYXFp5tHIE6yiQy6xcLTFJpkdJsxbfCzwBfKe0uYE6XaEOQ+x4dp35Ae sS2bcsdL/Pl8JJBimgorCLQZczsRo20Vb2XjyOaXh7d4NPYy7dgV+Xx1CSPR9KbrblPi y+7mb2PseLAe1xyeIzHrx38iPLLfW24OyEKkhP3nDawIii/IhOJ7l/o+tG+bpEh59F6P UQXBEURKornr2/C+VmBrg5yB/Ou5mkqlVgcq7lPwLEGkXfIX3Y0lXPLMe5gvE4b5A/RS B+mjAuy1jlezBzcbI96HO3Wkzn9r2X6uYFd0Vg78LxHSYovaXma41nRCb6D1++lgBekn UqPg== X-Gm-Message-State: AOJu0YyKKZZCvgiL3NtKx1EwyMev5pB2RrHpdXINovLKEmT8sW326/Vx 5vOODq9PRcBFNPcue1l5NQqAU/xbcOv1 X-Google-Smtp-Source: AGHT+IGpJnEggCqs3sKsdlfcaOIA3oGBRfH282sCatINwA7vw7EM96PDp2OvW3EEYzOGNt/yZgDRvDFdKDp3 X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:35bf:293e:7696:34e9]) (user=irogers job=sendgmr) by 2002:a05:690c:3382:b0:5d3:985c:800c with SMTP id fl2-20020a05690c338200b005d3985c800cmr144441ywb.3.1701925495076; Wed, 06 Dec 2023 21:04:55 -0800 (PST) Date: Wed, 6 Dec 2023 21:04:30 -0800 In-Reply-To: <20231207050433.1426834-1-irogers@google.com> Message-Id: <20231207050433.1426834-4-irogers@google.com> Mime-Version: 1.0 References: <20231207050433.1426834-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.rc2.451.g8631bc7472-goog Subject: [PATCH v1 3/6] perf maps: Switch modules tree walk to io_dir__readdir From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , "Masami Hiramatsu (Google)" , Thomas Richter , Quentin Monnet , Fangrui Song , Kan Liang , Yanteng Si , K Prateek Nayak , Artem Savkov , James Clark , Ravi Bangoria , 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" Compared to glibc's opendir/readdir this lowers the max RSS of perf record by 1.8MB on a Debian machine. Signed-off-by: Ian Rogers --- tools/perf/util/machine.c | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index c5de5363b5e7..b6831a1f909d 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -36,6 +36,7 @@ #include // page_size #include "cgroup.h" #include "arm64-frame-pointer-unwind-support.h" +#include =20 #include #include @@ -1551,25 +1552,21 @@ static int maps__set_module_path(struct maps *maps,= const char *path, struct kmo =20 static int maps__set_modules_path_dir(struct maps *maps, const char *dir_n= ame, int depth) { - struct dirent *dent; - DIR *dir =3D opendir(dir_name); + struct io_dirent64 *dent; + struct io_dir iod; int ret =3D 0; =20 - if (!dir) { + io_dir__init(&iod, open(dir_name, O_CLOEXEC | O_DIRECTORY | O_RDONLY)); + if (iod.dirfd < 0) { pr_debug("%s: cannot open %s dir\n", __func__, dir_name); return -1; } =20 - while ((dent =3D readdir(dir)) !=3D NULL) { + while ((dent =3D io_dir__readdir(&iod)) !=3D NULL) { char path[PATH_MAX]; - struct stat st; =20 - /*sshfs might return bad dent->d_type, so we have to stat*/ path__join(path, sizeof(path), dir_name, dent->d_name); - if (stat(path, &st)) - continue; - - if (S_ISDIR(st.st_mode)) { + if (io_dir__is_dir(&iod, dent)) { if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) continue; @@ -1602,7 +1599,7 @@ static int maps__set_modules_path_dir(struct maps *ma= ps, const char *dir_name, i } =20 out: - closedir(dir); + close(iod.dirfd); return ret; } =20 --=20 2.43.0.rc2.451.g8631bc7472-goog From nobody Fri Dec 19 07:50:32 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 0BFD2C4167B for ; Thu, 7 Dec 2023 05:05:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230424AbjLGFFE (ORCPT ); Thu, 7 Dec 2023 00:05:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40910 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229537AbjLGFEw (ORCPT ); Thu, 7 Dec 2023 00:04:52 -0500 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2042AD51 for ; Wed, 6 Dec 2023 21:04:58 -0800 (PST) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5d8f69da4c8so3960947b3.2 for ; Wed, 06 Dec 2023 21:04:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701925497; x=1702530297; 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=8yddnIh+eN5/+1uKGHF2Xb5nfq0hpynkUoMoJjaaQGg=; b=q34xsdtWe+M02IKvzjOSxgMLzNDMir7y4GddACF4uwS1xLYobrcr5JqQlwvTEbOqFb Bdbni7PR4oS1Ulkd0+x6lLWMNPPUJwZa36paZYHagqkSxsviR5WamB3dpsjoxF2ApD7P A2ue9Dn5U5e2W3RHSm++aMFN3HfhDmg1LpKnfUOj4nOTRmjm533bxa7fXx1yepq0SwGu ivEUDJa9+ij9Hbr8cgTMv1nVubCdxgx64i0quslEc8kyX7itmHLUxMm+Vy2tZVKN0wpk Vq90xrUt21f6EhxVtptXt+VBeu3IDyYjflmWKnCvNRoeH7608C6ZhgFhYMWaLwqNs2yT vjrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701925497; x=1702530297; 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=8yddnIh+eN5/+1uKGHF2Xb5nfq0hpynkUoMoJjaaQGg=; b=Sd018liynRWb7FJ6Ifxysr1ebJ99HxUW2Uh7BLCqX2UuTj9FLrTgxzLEHv9aq5itiw j1cX9h7GyQey41IXU2gdoLVVAc+i1nwPD+o0dLukgJ49YKtFJjUzaS6sgyWkbUcxgfE6 ypx6EbWRFCY3Gezc8b8R7juf7Tdr2R8y+lZ25SSBGtD4S5UMaMHIIgyTevbSq1Ds9VnE rThfd+tRkapTat32TEQqgfviD5JtXcamlSN9U653wYzeTW/XOkfOSsG8u1c4Wo9lvzXo wo6HBW69UIHhb5cLyyYRXDn8M071PPCgQBsDNDaYCAtTsX7ovVNY2TGNKtjnbPtv0bQS CxWA== X-Gm-Message-State: AOJu0YwJ5akEETBEnUxLVPMYo8Xoe77mU345dI1p9mErsZezT8e4xrSu J9KByF9YwRX45YuIA1fE+VUjtBmc4FrB X-Google-Smtp-Source: AGHT+IE882a5TV3lzEwPVuhsbdGQD4YkJ4nM2OZr83LhR4in6R8D9tV/naisyo0LtFoYkBhOxvaSkjr1xQ4u X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:35bf:293e:7696:34e9]) (user=irogers job=sendgmr) by 2002:a25:74d1:0:b0:db4:5e66:4a05 with SMTP id p200-20020a2574d1000000b00db45e664a05mr30614ybc.2.1701925497312; Wed, 06 Dec 2023 21:04:57 -0800 (PST) Date: Wed, 6 Dec 2023 21:04:31 -0800 In-Reply-To: <20231207050433.1426834-1-irogers@google.com> Message-Id: <20231207050433.1426834-5-irogers@google.com> Mime-Version: 1.0 References: <20231207050433.1426834-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.rc2.451.g8631bc7472-goog Subject: [PATCH v1 4/6] perf pmu: Switch to io_dir__readdir From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , "Masami Hiramatsu (Google)" , Thomas Richter , Quentin Monnet , Fangrui Song , Kan Liang , Yanteng Si , K Prateek Nayak , Artem Savkov , James Clark , Ravi Bangoria , 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" Avoid DIR allocations when scanning sysfs by using io_dir for the readdir implementation, that allocates about 1kb on the stack. Signed-off-by: Ian Rogers --- tools/perf/util/pmu.c | 48 +++++++++++++++++------------------------- tools/perf/util/pmus.c | 30 ++++++++++---------------- 2 files changed, 30 insertions(+), 48 deletions(-) diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index 3c9609944a2f..cddb82123a1e 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -184,19 +185,17 @@ static void perf_pmu_format__load(const struct perf_p= mu *pmu, struct perf_pmu_fo */ int perf_pmu__format_parse(struct perf_pmu *pmu, int dirfd, bool eager_loa= d) { - struct dirent *evt_ent; - DIR *format_dir; + struct io_dirent64 *evt_ent; + struct io_dir format_dir; int ret =3D 0; =20 - format_dir =3D fdopendir(dirfd); - if (!format_dir) - return -EINVAL; + io_dir__init(&format_dir, dirfd); =20 - while ((evt_ent =3D readdir(format_dir)) !=3D NULL) { + while ((evt_ent =3D io_dir__readdir(&format_dir)) !=3D NULL) { struct perf_pmu_format *format; char *name =3D evt_ent->d_name; =20 - if (!strcmp(name, ".") || !strcmp(name, "..")) + if (io_dir__is_dir(&format_dir, evt_ent)) continue; =20 format =3D perf_pmu__new_format(&pmu->format, name); @@ -223,7 +222,7 @@ int perf_pmu__format_parse(struct perf_pmu *pmu, int di= rfd, bool eager_load) } } =20 - closedir(format_dir); + close(format_dir.dirfd); return ret; } =20 @@ -599,8 +598,8 @@ static inline bool pmu_alias_info_file(const char *name) static int pmu_aliases_parse(struct perf_pmu *pmu) { char path[PATH_MAX]; - struct dirent *evt_ent; - DIR *event_dir; + struct io_dirent64 *evt_ent; + struct io_dir event_dir; size_t len; int fd, dir_fd; =20 @@ -615,13 +614,9 @@ static int pmu_aliases_parse(struct perf_pmu *pmu) return 0; } =20 - event_dir =3D fdopendir(dir_fd); - if (!event_dir){ - close (dir_fd); - return -EINVAL; - } + io_dir__init(&event_dir, dir_fd); =20 - while ((evt_ent =3D readdir(event_dir))) { + while ((evt_ent =3D io_dir__readdir(&event_dir))) { char *name =3D evt_ent->d_name; FILE *file; =20 @@ -651,7 +646,6 @@ static int pmu_aliases_parse(struct perf_pmu *pmu) fclose(file); } =20 - closedir(event_dir); close (dir_fd); pmu->sysfs_aliases_loaded =3D true; return 0; @@ -1883,10 +1877,9 @@ static void perf_pmu__del_caps(struct perf_pmu *pmu) */ int perf_pmu__caps_parse(struct perf_pmu *pmu) { - struct stat st; char caps_path[PATH_MAX]; - DIR *caps_dir; - struct dirent *evt_ent; + struct io_dir caps_dir; + struct io_dirent64 *evt_ent; int caps_fd; =20 if (pmu->caps_initialized) @@ -1897,24 +1890,21 @@ int perf_pmu__caps_parse(struct perf_pmu *pmu) if (!perf_pmu__pathname_scnprintf(caps_path, sizeof(caps_path), pmu->name= , "caps")) return -1; =20 - if (stat(caps_path, &st) < 0) { + caps_fd =3D open(caps_path, O_CLOEXEC | O_DIRECTORY | O_RDONLY); + if (caps_fd =3D=3D -1) { pmu->caps_initialized =3D true; return 0; /* no error if caps does not exist */ } =20 - caps_dir =3D opendir(caps_path); - if (!caps_dir) - return -EINVAL; - - caps_fd =3D dirfd(caps_dir); + io_dir__init(&caps_dir, caps_fd); =20 - while ((evt_ent =3D readdir(caps_dir)) !=3D NULL) { + while ((evt_ent =3D io_dir__readdir(&caps_dir)) !=3D NULL) { char *name =3D evt_ent->d_name; char value[128]; FILE *file; int fd; =20 - if (!strcmp(name, ".") || !strcmp(name, "..")) + if (io_dir__is_dir(&caps_dir, evt_ent)) continue; =20 fd =3D openat(caps_fd, name, O_RDONLY); @@ -1936,7 +1926,7 @@ int perf_pmu__caps_parse(struct perf_pmu *pmu) fclose(file); } =20 - closedir(caps_dir); + close(caps_fd); =20 pmu->caps_initialized =3D true; return pmu->nr_caps; diff --git a/tools/perf/util/pmus.c b/tools/perf/util/pmus.c index ce4931461741..65b23b98666b 100644 --- a/tools/perf/util/pmus.c +++ b/tools/perf/util/pmus.c @@ -3,10 +3,10 @@ #include #include #include +#include #include #include #include -#include #include #include #include @@ -184,8 +184,8 @@ static int pmus_cmp(void *priv __maybe_unused, static void pmu_read_sysfs(bool core_only) { int fd; - DIR *dir; - struct dirent *dent; + struct io_dir dir; + struct io_dirent64 *dent; =20 if (read_sysfs_all_pmus || (core_only && read_sysfs_core_pmus)) return; @@ -194,13 +194,9 @@ static void pmu_read_sysfs(bool core_only) if (fd < 0) return; =20 - dir =3D fdopendir(fd); - if (!dir) { - close(fd); - return; - } + io_dir__init(&dir, fd); =20 - while ((dent =3D readdir(dir))) { + while ((dent =3D io_dir__readdir(&dir)) !=3D NULL) { if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) continue; if (core_only && !is_pmu_core(dent->d_name)) @@ -209,7 +205,7 @@ static void pmu_read_sysfs(bool core_only) perf_pmu__find2(fd, dent->d_name); } =20 - closedir(dir); + close(fd); if (list_empty(&core_pmus)) { if (!perf_pmu__create_placeholder_core_pmu(&core_pmus)) pr_err("Failure to set up any core PMUs\n"); @@ -563,8 +559,8 @@ bool perf_pmus__supports_extended_type(void) char *perf_pmus__default_pmu_name(void) { int fd; - DIR *dir; - struct dirent *dent; + struct io_dir dir; + struct io_dirent64 *dent; char *result =3D NULL; =20 if (!list_empty(&core_pmus)) @@ -574,13 +570,9 @@ char *perf_pmus__default_pmu_name(void) if (fd < 0) return strdup("cpu"); =20 - dir =3D fdopendir(fd); - if (!dir) { - close(fd); - return strdup("cpu"); - } + io_dir__init(&dir, fd); =20 - while ((dent =3D readdir(dir))) { + while ((dent =3D io_dir__readdir(&dir)) !=3D NULL) { if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) continue; if (is_pmu_core(dent->d_name)) { @@ -589,7 +581,7 @@ char *perf_pmus__default_pmu_name(void) } } =20 - closedir(dir); + close(fd); return result ?: strdup("cpu"); } =20 --=20 2.43.0.rc2.451.g8631bc7472-goog From nobody Fri Dec 19 07:50:32 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 DD7C8C4167B for ; Thu, 7 Dec 2023 05:05:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230385AbjLGFFI (ORCPT ); Thu, 7 Dec 2023 00:05:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40918 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230315AbjLGFEy (ORCPT ); Thu, 7 Dec 2023 00:04:54 -0500 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 85691D44 for ; Wed, 6 Dec 2023 21:05:00 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-db4004a8aa9so703817276.1 for ; Wed, 06 Dec 2023 21:05:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701925499; x=1702530299; 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=fQL7jSRusjmNnXVovrf6s5ZXv2q3XjGZXt0cQXTXyH0=; b=adeQ1HrBBNuzpsW+RthE9e4k5jpdyG60N+dtMasE1XmaiThK0/Jj/xIjweED8y2MPv 6KdfWgEaIOLLXt+AohCvXB2q33bSM/W9KVD1IfiLjGqlULGzCwPri8TcIsxMO1XQW5rM uLzbNxCUCI0tGfZt5pMSBGn9B3Zonji+OcPzRst5anItTipC96Go9rkcT41GThTwgS7U hCdoWZMFbF/bHDkj/WlMOz/E9vPSV79adX3eMYRdgjybII5jt3V07KPHsB0AgK0TrjpH 0/9ynUYGUOnkuv8xt70AOtEDC5puUUQmN6JQUV0KSBsv6PZPDAGcFFhhngoadEDSTTz4 zFBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701925499; x=1702530299; 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=fQL7jSRusjmNnXVovrf6s5ZXv2q3XjGZXt0cQXTXyH0=; b=MJ++R9u3kLA+KJnHZPSpabqPScblZMN2D416PvjdJEE98a0xZJoGb2s2VQ7aDQ+saX RSk1hl1A+WYAB1FlVQZJohFf5yf1h/gBf2UO9e3JVTWI/gmdmq4jU6P6eT4w7WYdraqk 2meMDchlXfYBG6b1fQZAUspTUCpTJV8WGVLc6XKYbKzRwpFQs1+I80j+D0SWm3dwSXdZ 2Xq2s2AeNF4PONkb4zjRgrKVaqTs8IKVXFP1Tv/qiLBJpHvIw/LCC2l89VJe7RZJeceS ysxTx+6B+ONMovQqOAxfmqt/NwTpGqMnmAjwk+zdJzoX4Sq540P/7skse7bLVPx1usdD vdAw== X-Gm-Message-State: AOJu0Yxd5X9nIlyujBDLmpVATtkgenFK/fj+ivTP8Lv0kRkuOo6HyqDJ kMYLbetRmlUPKFZZWSlgulsnonyp9Ji4 X-Google-Smtp-Source: AGHT+IEIES8vtcKWQ6hHeWuSFzb//31lxElZ3NI6bEf1mOom/dSrMlQcefANtfLZm0opUAdKEtbAMTMjqjGE X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:35bf:293e:7696:34e9]) (user=irogers job=sendgmr) by 2002:a25:df0b:0:b0:d9a:efcc:42af with SMTP id w11-20020a25df0b000000b00d9aefcc42afmr31557ybg.2.1701925499548; Wed, 06 Dec 2023 21:04:59 -0800 (PST) Date: Wed, 6 Dec 2023 21:04:32 -0800 In-Reply-To: <20231207050433.1426834-1-irogers@google.com> Message-Id: <20231207050433.1426834-6-irogers@google.com> Mime-Version: 1.0 References: <20231207050433.1426834-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.rc2.451.g8631bc7472-goog Subject: [PATCH v1 5/6] perf header: Switch mem topology to io_dir__readdir From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , "Masami Hiramatsu (Google)" , Thomas Richter , Quentin Monnet , Fangrui Song , Kan Liang , Yanteng Si , K Prateek Nayak , Artem Savkov , James Clark , Ravi Bangoria , 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" Switch memory_node__read and build_mem_topology from opendir/readdir to io_dir__readdir, with smaller stack allocations. Reduces peak memory consumption of perf record by 10kb. Signed-off-by: Ian Rogers --- tools/perf/util/header.c | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c index 08cc2febabde..d4a3e28376fd 100644 --- a/tools/perf/util/header.c +++ b/tools/perf/util/header.c @@ -44,6 +44,7 @@ #include "build-id.h" #include "data.h" #include +#include #include "asm/bug.h" #include "tool.h" #include "time-utils.h" @@ -1341,11 +1342,11 @@ static int memory_node__read(struct memory_node *n,= unsigned long idx) { unsigned int phys, size =3D 0; char path[PATH_MAX]; - struct dirent *ent; - DIR *dir; + struct io_dirent64 *ent; + struct io_dir dir; =20 #define for_each_memory(mem, dir) \ - while ((ent =3D readdir(dir))) \ + while ((ent =3D io_dir__readdir(&dir)) !=3D NULL) \ if (strcmp(ent->d_name, ".") && \ strcmp(ent->d_name, "..") && \ sscanf(ent->d_name, "memory%u", &mem) =3D=3D 1) @@ -1354,9 +1355,9 @@ static int memory_node__read(struct memory_node *n, u= nsigned long idx) "%s/devices/system/node/node%lu", sysfs__mountpoint(), idx); =20 - dir =3D opendir(path); - if (!dir) { - pr_warning("failed: can't open memory sysfs data\n"); + io_dir__init(&dir, open(path, O_CLOEXEC | O_DIRECTORY | O_RDONLY)); + if (dir.dirfd < 0) { + pr_warning("failed: can't open memory sysfs data '%s'\n", path); return -1; } =20 @@ -1368,20 +1369,20 @@ static int memory_node__read(struct memory_node *n,= unsigned long idx) =20 n->set =3D bitmap_zalloc(size); if (!n->set) { - closedir(dir); + close(dir.dirfd); return -ENOMEM; } =20 n->node =3D idx; n->size =3D size; =20 - rewinddir(dir); + io_dir__rewinddir(&dir); =20 for_each_memory(phys, dir) { __set_bit(phys, n->set); } =20 - closedir(dir); + close(dir.dirfd); return 0; } =20 @@ -1404,8 +1405,8 @@ static int memory_node__sort(const void *a, const voi= d *b) static int build_mem_topology(struct memory_node **nodesp, u64 *cntp) { char path[PATH_MAX]; - struct dirent *ent; - DIR *dir; + struct io_dirent64 *ent; + struct io_dir dir; int ret =3D 0; size_t cnt =3D 0, size =3D 0; struct memory_node *nodes =3D NULL; @@ -1413,14 +1414,14 @@ static int build_mem_topology(struct memory_node **= nodesp, u64 *cntp) scnprintf(path, PATH_MAX, "%s/devices/system/node/", sysfs__mountpoint()); =20 - dir =3D opendir(path); - if (!dir) { + io_dir__init(&dir, open(path, O_CLOEXEC | O_DIRECTORY | O_RDONLY)); + if (dir.dirfd < 0) { pr_debug2("%s: couldn't read %s, does this arch have topology informatio= n?\n", __func__, path); return -1; } =20 - while (!ret && (ent =3D readdir(dir))) { + while (!ret && (ent =3D io_dir__readdir(&dir))) { unsigned int idx; int r; =20 @@ -1449,7 +1450,7 @@ static int build_mem_topology(struct memory_node **no= desp, u64 *cntp) cnt +=3D 1; } out: - closedir(dir); + close(dir.dirfd); if (!ret) { *cntp =3D cnt; *nodesp =3D nodes; --=20 2.43.0.rc2.451.g8631bc7472-goog From nobody Fri Dec 19 07:50:32 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 3765AC4167B for ; Thu, 7 Dec 2023 05:05:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230266AbjLGFFL (ORCPT ); Thu, 7 Dec 2023 00:05:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40946 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230338AbjLGFE5 (ORCPT ); Thu, 7 Dec 2023 00:04:57 -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 CE270D51 for ; Wed, 6 Dec 2023 21:05:02 -0800 (PST) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5d3eafe3d17so1929387b3.1 for ; Wed, 06 Dec 2023 21:05:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701925502; x=1702530302; 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=bNmJSk2Mf8wiupmA7otv2vwMpu3HCNA090aeI5fVLaE=; b=j66J+ZPkvukEjBSMTiJqbYvNks4a/6dOQ4AghyY/zuocABJaYCry4143ccTRt7IO12 cYV7ElMH/wyxsT8O+mscRYokLeg4Kelm1c9kkCufTmMQS2xSB+Y0uaQERrli8gjNFT2R ftAMltvudLWurWANfRAedFRT8HD2S6bAk+OebcjhQihsyFq+tM08ZT8za14+WtLh70CA OaWS6ZeQ3qRNuWnKY9MbEyDIbXlFf8PGQ+vbtHtJ/Zc4F0GZ7u+oAoCRPO5RVw0M6MAH +WefPPlJiERBxJHnEyZIo4M+9OOJK2wN5gnItQK6bCV74Sb4M/miQNoil+CRlfirz9cf Tpkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701925502; x=1702530302; 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=bNmJSk2Mf8wiupmA7otv2vwMpu3HCNA090aeI5fVLaE=; b=v+xQ86awFBA0e6U5Ijn04wINuoW1yV4L3vVtDeubZFq414AaJktNqq+AqsQNDTAAPi 2B3UR6ZKUjfzQq8M9DNhp/L1prIKfi/8APymhsyQCfIuRIVeElFDT0zA9Lg+Vf8Gu7ga XYX0JXNT/O9EZiAD7hlsIsuGPycXz/ExG0U2Y7kmS4wlsITKWUBALCHjHpgAE+6gNPW9 I7qawYzC8oFm2xdH/dVUA4zEvCynF3qYS3ICRSYqx6Tx9nJ8ul+AfUFplAxNZqw5dj8/ tWVkOD22rfOBYvXWXe5IaE/3z11WWER7/qWS03FANTNR5QG1kft3EADBDdIofDM5j9EK XSYg== X-Gm-Message-State: AOJu0Yx7BMlK8MCQISo65UvxFgORKYSRF5cn3drj00IRbCDSQpt3z5in nDbm/yKI9MmjRC5l9vbmVM0Dhk9OeIIA X-Google-Smtp-Source: AGHT+IF/nzirSeJnMIvE0mGEycoV9XdkGUbzHi13uh8ZFStCgczXxDlh1x3wJVZVQti1bAtMbyQyGetvXwAv X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:35bf:293e:7696:34e9]) (user=irogers job=sendgmr) by 2002:a25:3144:0:b0:db5:4a00:a153 with SMTP id x65-20020a253144000000b00db54a00a153mr46968ybx.4.1701925501849; Wed, 06 Dec 2023 21:05:01 -0800 (PST) Date: Wed, 6 Dec 2023 21:04:33 -0800 In-Reply-To: <20231207050433.1426834-1-irogers@google.com> Message-Id: <20231207050433.1426834-7-irogers@google.com> Mime-Version: 1.0 References: <20231207050433.1426834-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.rc2.451.g8631bc7472-goog Subject: [PATCH v1 6/6] perf events: Remove scandir in thread synthesis From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , "Masami Hiramatsu (Google)" , Thomas Richter , Quentin Monnet , Fangrui Song , Kan Liang , Yanteng Si , K Prateek Nayak , Artem Savkov , James Clark , Ravi Bangoria , 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" This avoids scanddir reading the directory into memory that's allocated and instead allocates on the stack. Signed-off-by: Ian Rogers --- tools/perf/util/synthetic-events.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/tools/perf/util/synthetic-events.c b/tools/perf/util/synthetic= -events.c index a0579c7d7b9e..7cc38f2a0e9e 100644 --- a/tools/perf/util/synthetic-events.c +++ b/tools/perf/util/synthetic-events.c @@ -38,6 +38,7 @@ #include /* To get things like MAP_HUGETLB even on old= er libc headers */ #include #include +#include #include #include #include @@ -751,10 +752,10 @@ static int __event__synthesize_thread(union perf_even= t *comm_event, bool needs_mmap, bool mmap_data) { char filename[PATH_MAX]; - struct dirent **dirent; + struct io_dir iod; + struct io_dirent64 *dent; pid_t tgid, ppid; int rc =3D 0; - int i, n; =20 /* special case: only send one comm event using passed in pid */ if (!full) { @@ -786,16 +787,19 @@ static int __event__synthesize_thread(union perf_even= t *comm_event, snprintf(filename, sizeof(filename), "%s/proc/%d/task", machine->root_dir, pid); =20 - n =3D scandir(filename, &dirent, filter_task, NULL); - if (n < 0) - return n; + io_dir__init(&iod, open(filename, O_CLOEXEC | O_DIRECTORY | O_RDONLY)); + if (iod.dirfd < 0) + return -1; =20 - for (i =3D 0; i < n; i++) { + while ((dent =3D io_dir__readdir(&iod)) !=3D NULL) { char *end; pid_t _pid; bool kernel_thread =3D false; =20 - _pid =3D strtol(dirent[i]->d_name, &end, 10); + if (!isdigit(dent->d_name[0])) + continue; + + _pid =3D strtol(dent->d_name, &end, 10); if (*end) continue; =20 @@ -829,9 +833,7 @@ static int __event__synthesize_thread(union perf_event = *comm_event, } } =20 - for (i =3D 0; i < n; i++) - zfree(&dirent[i]); - free(dirent); + close(iod.dirfd); =20 return rc; } --=20 2.43.0.rc2.451.g8631bc7472-goog