From nobody Fri Dec 19 07:50:26 2025 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DB85C1FAC37 for ; Sat, 22 Feb 2025 06:10:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740204629; cv=none; b=o7jh4JI145nOqpuaC1i9q6w4+KylCP3g5cOCumEF1JyIx0rDnjkd4gpsc8pQjnk1s5slZZRbQaoONBbhUw98wh1p/Alrk9Bfbp16e4Econ7T0wnlyoGeU2QmmTjv5kbhYwaTBSthzqPKPUrI87NXzVKSKHquOMIStgPANRwZgsk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740204629; c=relaxed/simple; bh=JlhCie1KVGPxky6hLFrhYmegSBkXKTMJ+o4py22ZdJY=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Cc:Content-Type; b=bZw+jM9adAoverY91+czlhU2F7tYWGGtx+nMnhnJneLSloVUzl+FOalRlKXetdtLJ81deC62JzVWcavy6+I0Oilmlf8Pi0dh0ogNih9QkA4sEEH8wA90n2ps31osXd/9tdCrs1wZQkJZDsViQKRdzoJcDgYh48NmKdRUGvlceGc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=eNAvZlvf; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="eNAvZlvf" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6f79c1cf49dso35427497b3.2 for ; Fri, 21 Feb 2025 22:10:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740204626; x=1740809426; darn=vger.kernel.org; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=U/7ZX9JhDgLMSQLTzuNEs8rLWBMeKnvwebReBgtVAUg=; b=eNAvZlvf97MojJ1yYbhbYlwMf1BPjiW0yy+gi32S/S/X+Pq9JfRZl4hNZHjTi1LTKt QkUgHYL7kgI2R+QBZr8vyHhsrkDM+o2qW3VKhWrgXUEKO4fcqfPDenXxWE7r5WmIpC+G 5zubFSHjWJsvuCA+JYJuckKV2jXea6bY0srKnkWYqWVMEeetvJHQfalP4QVbHfcXUYV9 tf9xPJaAJvcmUILSDQYEgGYhylyQQ6H8THwS+bM+7uRIAyWxFDSPPeA+v2ggtg0NNMD4 Wyd6zPOrU2THp+EVAeAnzyBFxcAqhFj0g0YYR7SY8bRvGCxTkihOV+Kdkj5z8IguLalw csfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740204626; x=1740809426; h=cc: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=U/7ZX9JhDgLMSQLTzuNEs8rLWBMeKnvwebReBgtVAUg=; b=FdPkSW5iv6R+negkzQnQswOSLcLx8gygfxAOPPiXA+WOyxRdMpGBw1ikvGkXypMqQ3 kmIo8l/d0cC1HPgftCbFZV7N4i2wgcEs5W8rCLZALZ7HVXm3Ea4uvaKoCTSWXoQK2pdV MHydhnNKJdDCMqfFE+ULdIONGo6bwgU6Lwav6faaD0LMXHIf2L6Be2MHuqdpsvJMck0y Xrq5eeOxmQ2rz6nPtNl3T8AZRNgxB355xnDZAZgcwTh0T5hXVHjKfksUs4Yl4gNjcjy9 NUX6zszZ/et1kzlGikwPGiEzS4/ZXjocZQOBVBoYyQAyX5Zs+C2Sh8yDG8JsQ5DD/E0y nCPg== X-Forwarded-Encrypted: i=1; AJvYcCU+eQXkhvDbeO2TD/dDs64u7KpbQWI6tqWCww1CCLb/SpZQN0UjhKcNOS9W5Mallq8hb5SoM38tx+fVeSc=@vger.kernel.org X-Gm-Message-State: AOJu0Ywz/luLqnTliA+jHwQ5iS6iZLZpy20YgFqB3GslHbMrt2tKjXfK q9VS9pbLEdEWBNy4CduRYaagsB+M2bTSo/gbzjgcSvMcukAZrr5QyZVheMaJtm2ERxMXQZDcM/7 TOVJZug== X-Google-Smtp-Source: AGHT+IEYFAPRiX8fFmj1juQHrQSqiPZVIrB8A6o/q8ZsWipx0bDxLmVyKrA3nYPlyo25uJmD0bFLmIOqFEoH X-Received: from irogers.svl.corp.google.com ([2620:15c:2c5:11:5003:3a14:d4e7:121]) (user=irogers job=sendgmr) by 2002:a25:83d0:0:b0:e48:2ea1:d9ea with SMTP id 3f1490d57ef6-e5e8b073be9mr28959276.7.1740204625695; Fri, 21 Feb 2025 22:10:25 -0800 (PST) Date: Fri, 21 Feb 2025 22:10:06 -0800 In-Reply-To: <20250222061015.303622-1-irogers@google.com> Message-Id: <20250222061015.303622-2-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250222061015.303622-1-irogers@google.com> X-Mailer: git-send-email 2.48.1.658.g4767266eb4-goog Subject: [PATCH v3 1/8] tools lib api: Add io_dir an allocation free readdir alternative From: Ian Rogers To: Namhyung Kim , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Adrian Hunter , Kan Liang , Sam James , Jesper Juhl , James Clark , Zhongqiu Han , Yicong Yang , Thomas Richter , Michael Petlan , Anne Macedo , Dominique Martinet , Jean-Philippe Romain , Junhao He , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, "=?UTF-8?q?Krzysztof=20=C5=81opatowski?=" Cc: Ian Rogers 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. As the system call number maybe missing, add #defines for those. Note, an earlier version of this patch had a feature test for getdents64 but there were problems on certains distros where getdents64 would be #define renamed to getdents breaking the code. The syscall use was made uncondtional to work around this. There is context in: https://lore.kernel.org/lkml/20231207050433.1426834-1-irogers@google.com/ Signed-off-by: Ian Rogers --- tools/lib/api/Makefile | 2 +- tools/lib/api/io_dir.h | 104 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 105 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 7f6396087b46..8665c799e0fa 100644 --- a/tools/lib/api/Makefile +++ b/tools/lib/api/Makefile @@ -95,7 +95,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..aab73393d2a2 --- /dev/null +++ b/tools/lib/api/io_dir.h @@ -0,0 +1,104 @@ +/* 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 +#include + +#if !defined(SYS_getdents64) +#if defined(__x86_64__) || defined(__arm__) + #define SYS_getdents64 217 +#elif defined(__i386__) || defined(__s390x__) || defined(__sh__) + #define SYS_getdents64 220 +#elif defined(__alpha__) + #define SYS_getdents64 377 +#elif defined(__mips__) + #define SYS_getdents64 308 +#elif defined(__powerpc64__) || defined(__powerpc__) + #define SYS_getdents64 202 +#elif defined(__sparc64__) || defined(__sparc__) + #define SYS_getdents64 154 +#elif defined(__xtensa__) + #define SYS_getdents64 60 +#else + #define SYS_getdents64 61 +#endif +#endif /* !defined(SYS_getdents64) */ + +static inline ssize_t perf_getdents64(int fd, void *dirp, size_t count) +{ +#ifdef MEMORY_SANITIZER + memset(dirp, 0, count); +#endif + return syscall(SYS_getdents64, fd, dirp, count); +} + +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 perf_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 /* __API_IO_DIR__ */ --=20 2.48.1.658.g4767266eb4-goog From nobody Fri Dec 19 07:50:26 2025 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1343820011D for ; Sat, 22 Feb 2025 06:10:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740204630; cv=none; b=mvEu1zpwV0TWu3rJqccnvbTD+TEcxO5H8XSpE3l3d1AfDU0avFIxwaz6Tj0Cw4rkceIaz3+Arkv9hw89EgFR8CnHGyZiEmGJVgqw6MJa4MLWT3aTgRZq7hB9PBj2pJ5xX6tKcRWcMJVoi9fFlSOYfAne3wUYSUbaNtDw7e0WDjI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740204630; c=relaxed/simple; bh=uZKZq4ptD24vFXFwJHlzTnn9G+8E1Z8PHZx5KZGUu3M=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Cc:Content-Type; b=M3YE3Qx5sV3iInXmhz7Xwk51lH8Un9EcAn1PQckLMNXgJT8vgMJzHMAoODLEsQmqKbkwZXr462vn1LHNJSSz9fwapUGxYWOg0EJ6TO+3+teRVzZlgbApu5WbNp6HuILJvD4E++qhQWKweAkYhxaA/aT8ckGe5VRlC+qUHaiKIl4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=hRHIWNqf; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="hRHIWNqf" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6fb91a99b18so40382437b3.3 for ; Fri, 21 Feb 2025 22:10:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740204628; x=1740809428; darn=vger.kernel.org; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=7VtHjp1N8qbHeA5gb9C+9YjzRoSPteEkA5kdevNpasM=; b=hRHIWNqfTOxWcJRTcNxqKhMJlnXqAfyukKK4U/C1Uuwb2RKbdAkXFv/NpeT6EgnKNL RzsKtevk8NEbGNEbmtR4rp4mZYE9GikVE0M7Xq9mL/hb7iiYK43uD1Rd8meWO3XXxlcZ 0pLBpHw7upg7QlujB4+kOc0frOjoyAklKIz83suUx4ZPbHP/c20TMbNNvWaz6TmgeMp0 NbDxlyRxaBKy25y0vX3/0zAQFGB0xUAwqT33MbpJ1QEWn0rIb4Ej3l0kAvaimpuMbl01 9oCCxah38NeQD2CwmwDP918Lgy1nJsQyTdWSUQGfBc6ITjHLAnTvXseJO05YpSn9TIPK 63eg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740204628; x=1740809428; h=cc: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=7VtHjp1N8qbHeA5gb9C+9YjzRoSPteEkA5kdevNpasM=; b=NdmkQaygibQAlLZdw41Hnd14UwB05bye4Mbs744vTN71pl5qZgh7T8+oaYNg7t0hCC yb+vS3D1to3HeKd5XwWEKUnhmHC1v85SXX4/ErSRjTOOuR/PiiYMaKXNp6kmVnZRWtcv Atq++6ahu15rKbXj85q9q6ZQGqq/d7Z0BgjBW332Tpm77r6iyVDOhGXBqcT1IHwtSr9S IP+7OXER8LLoNoeXI3zoiKz4cOezGA2QE7A5olrwOZ3Q+epOSdjVO4Lbo1xakoesTU2Q 3d2Jtfw53O5RT8pVst81BCAHqHeFEdT0VyO9bIgxgLmMryOT1J65zMig94d7C7CzOYwl NGRQ== X-Forwarded-Encrypted: i=1; AJvYcCWRNyrLwmAEHnuAgGQ1paM7duUBVbTolz6AkBPc7dfYggrVKV2BG3waNEEJqtddlwg/mBDAZWTU7hWvL6Y=@vger.kernel.org X-Gm-Message-State: AOJu0YxlTxxE+rBpug14ipuVcXmBZKbhPPHeJxArwR1jIWEFH3bqjUGA 8Luqpmv6txJ8XIwyxaxCCnXRFbCAX9nFLcDKLS7MK8Zr0XCJfAloeCv6hQoEWSvQBzzVKRhnoVu y/AngkQ== X-Google-Smtp-Source: AGHT+IE65L8aluDbSw023cnsnaEhizB//+QYeu9aiZcX9uqw2fvJ5SWSNv1xb4rtN0ccuBBufv/OlbuXxuxn X-Received: from irogers.svl.corp.google.com ([2620:15c:2c5:11:5003:3a14:d4e7:121]) (user=irogers job=sendgmr) by 2002:a25:694b:0:b0:e58:991a:e855 with SMTP id 3f1490d57ef6-e5e24502529mr39737276.0.1740204627922; Fri, 21 Feb 2025 22:10:27 -0800 (PST) Date: Fri, 21 Feb 2025 22:10:07 -0800 In-Reply-To: <20250222061015.303622-1-irogers@google.com> Message-Id: <20250222061015.303622-3-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250222061015.303622-1-irogers@google.com> X-Mailer: git-send-email 2.48.1.658.g4767266eb4-goog Subject: [PATCH v3 2/8] perf maps: Switch modules tree walk to io_dir__readdir From: Ian Rogers To: Namhyung Kim , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Adrian Hunter , Kan Liang , Sam James , Jesper Juhl , James Clark , Zhongqiu Han , Yicong Yang , Thomas Richter , Michael Petlan , Anne Macedo , Dominique Martinet , Jean-Philippe Romain , Junhao He , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, "=?UTF-8?q?Krzysztof=20=C5=81opatowski?=" Cc: Ian Rogers 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. Acked-by: Namhyung Kim Signed-off-by: Ian Rogers --- tools/perf/util/machine.c | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 316f0879e5e0..e394c630e3a2 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -37,6 +37,7 @@ #include // page_size #include "cgroup.h" #include "arm64-frame-pointer-unwind-support.h" +#include =20 #include #include @@ -1339,31 +1340,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) { - const 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]; - unsigned char d_type =3D dent->d_type; =20 path__join(path, sizeof(path), dir_name, dent->d_name); - - if (d_type =3D=3D DT_UNKNOWN) { - struct stat st; - - if (stat(path, &st)) - continue; - if (S_ISDIR(st.st_mode)) - d_type =3D DT_DIR; - } - - if (d_type =3D=3D DT_DIR) { + if (io_dir__is_dir(&iod, dent)) { if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) continue; @@ -1396,7 +1387,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.48.1.658.g4767266eb4-goog From nobody Fri Dec 19 07:50:26 2025 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 72EB6201267 for ; Sat, 22 Feb 2025 06:10:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740204633; cv=none; b=tu2G+krcGk4CsCctJvitaj/q74B0v9glVgQo2C0vqhtKsiAzrWKcFHsGAQ4FYJIGuF0GHizUJVHh6sDtbZiAt5Ccx8XjPoy4C0zl3bV1v1INkFEGTMa58/dhHyD3ids5nhnlj0q5MeUn7REzAAlD00QKbqtJTfeTHk7jPu+l/2A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740204633; c=relaxed/simple; bh=El3XeRtQ8OVZvnoXo6U/JPgYkzRfppXqk9slQa6YGzI=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Cc:Content-Type; b=jztGKVwT0Epc2zXrxZeOdnvjv3qtb8FVEZP4aMBhi8xsG9diX33n+6xmIwM96crqMKqnKfZrSlyunVOZBLpaGEfVROz8pWu4DAbLGLVSt1uaNZMlFnh39nCzudv+YFPhXmMV+7uN8r42b+db1sX56YouDtJlGGH3y0TbUzxFUEo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=zSrLymD2; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="zSrLymD2" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e549c458692so4760030276.2 for ; Fri, 21 Feb 2025 22:10:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740204630; x=1740809430; darn=vger.kernel.org; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=oQgs9D90lYlG1wh1e4gxKo/IB7ELhv6Ph1wQ4nXY+AA=; b=zSrLymD2E3DBGS70h+UrHydxyTqu2WJD7K8uHjyIkqMFfia7XhNYaA/oWYr9HZM6/D Jsc8Cm1/0809mZntgwCMxHRzTYD416DpAPVe8AJCoa36KLnZZYv0k1/xO3HdmX0rSb0H +tK48qFrhdWuBYPUYT1XWh3MGJaPEs+g3OIqpj8qL5dbNz0SjQwXmak0txUsmn2wyRhy ngpa5mA95mmOSi11ANqTy4PXQ5ihbttXTmJxFPTGrA078G2HFh8I98S9x40j33Bn2U9q ZrkP+dZ7OO23mZxkM6wtvKIpGKJnQ+DVPz5xVPY83b/X/b49E598S0McXGGCBfoir/Wa ET5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740204630; x=1740809430; h=cc: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=oQgs9D90lYlG1wh1e4gxKo/IB7ELhv6Ph1wQ4nXY+AA=; b=uhtNGHlG5jOQj2jRWnqO8+Q3cZM7xWIsdpQs/LRMpAtJGLucG8SueRt4yQ+/F1f2L/ fEsLMmGSzysKFJjdJ0m7uhmqS+iXpMix2CpFO5Bn6SUsFvMknFcMPaKkvf8mW8UOVzEV dkEqj77fAIWNj/ulWsCXIwOWux3+tUTWKluZjC9sdFaqRrh7lLiUrhvseLu9BjnHjejq Eid98FItAKEGroXmm71IMqM2zpMkRtPdSp73lh3t+Afn5G6VatZgUgPSJNr/rX4wCpoP N8Ca0A+fm9jGMiZu/TYbnNvGb6ByDthdEc/mIiuKRF1hpThEa6aO+xNTxnz7VfH8mLqO AXHg== X-Forwarded-Encrypted: i=1; AJvYcCUtuMyAOi3PtZrq/qYPHS7k9+6lysk+15J2SHZ9yKDkyvNep6CrG58Z/yL09VqqcH9sSnLvHAp0yIomrqY=@vger.kernel.org X-Gm-Message-State: AOJu0YzI8LaM3Nzi/SmgZaM1vHwzDgMnGUJGyCXXVJrakcUu2jaZjODJ ck5YX51K1KKeYLhfVVEKQHJlyo53p1mUga6JDsmKGgo42vgmmk6MPySV+Lzj0OIJ5754BMwWlsm YdTlhoQ== X-Google-Smtp-Source: AGHT+IGXtFIaZMEMC4tZMr9039JzzAtxMiM9SCVzH+3K7kZMO5jt7NrWjUZ4fOIJ1SFOMD2oBerQw8yCZt/s X-Received: from irogers.svl.corp.google.com ([2620:15c:2c5:11:5003:3a14:d4e7:121]) (user=irogers job=sendgmr) by 2002:a25:7c43:0:b0:e57:d22f:3449 with SMTP id 3f1490d57ef6-e5e2484ff55mr479026276.1.1740204630388; Fri, 21 Feb 2025 22:10:30 -0800 (PST) Date: Fri, 21 Feb 2025 22:10:08 -0800 In-Reply-To: <20250222061015.303622-1-irogers@google.com> Message-Id: <20250222061015.303622-4-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250222061015.303622-1-irogers@google.com> X-Mailer: git-send-email 2.48.1.658.g4767266eb4-goog Subject: [PATCH v3 3/8] perf pmu: Switch to io_dir__readdir From: Ian Rogers To: Namhyung Kim , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Adrian Hunter , Kan Liang , Sam James , Jesper Juhl , James Clark , Zhongqiu Han , Yicong Yang , Thomas Richter , Michael Petlan , Anne Macedo , Dominique Martinet , Jean-Philippe Romain , Junhao He , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, "=?UTF-8?q?Krzysztof=20=C5=81opatowski?=" Cc: Ian Rogers 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. Acked-by: Namhyung Kim Signed-off-by: Ian Rogers --- tools/perf/util/pmu.c | 46 +++++++++++++++++------------------------- tools/perf/util/pmus.c | 30 ++++++++++----------------- 2 files changed, 30 insertions(+), 46 deletions(-) diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index ec3878c890a9..e51c17b1ea35 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -195,19 +196,17 @@ static void perf_pmu_format__load(const struct perf_p= mu *pmu, struct perf_pmu_fo */ static int perf_pmu__format_parse(struct perf_pmu *pmu, int dirfd, bool ea= ger_load) { - 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); @@ -234,7 +233,7 @@ static int perf_pmu__format_parse(struct perf_pmu *pmu,= int dirfd, bool eager_lo } } =20 - closedir(format_dir); + close(format_dir.dirfd); return ret; } =20 @@ -635,14 +634,12 @@ static inline bool pmu_alias_info_file(const char *na= me) */ static int __pmu_aliases_parse(struct perf_pmu *pmu, int events_dir_fd) { - struct dirent *evt_ent; - DIR *event_dir; + struct io_dirent64 *evt_ent; + struct io_dir event_dir; =20 - event_dir =3D fdopendir(events_dir_fd); - if (!event_dir) - return -EINVAL; + io_dir__init(&event_dir, events_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; int fd; FILE *file; @@ -674,7 +671,6 @@ static int __pmu_aliases_parse(struct perf_pmu *pmu, in= t events_dir_fd) fclose(file); } =20 - closedir(event_dir); pmu->sysfs_aliases_loaded =3D true; return 0; } @@ -2221,10 +2217,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) @@ -2235,24 +2230,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); @@ -2274,7 +2266,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 8a0a919415d4..afd59d678fd0 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 @@ -235,20 +235,16 @@ static void pmu_read_sysfs(unsigned int to_read_types) =20 if (to_read_types & (PERF_TOOL_PMU_TYPE_PE_CORE_MASK | PERF_TOOL_PMU_TYPE= _PE_OTHER_MASK)) { int fd =3D perf_pmu__event_source_devices_fd(); - DIR *dir; - struct dirent *dent; + struct io_dir dir; + struct io_dirent64 *dent; bool core_only =3D (to_read_types & PERF_TOOL_PMU_TYPE_PE_OTHER_MASK) = =3D=3D 0; =20 if (fd < 0) goto skip_pe_pmus; =20 - dir =3D fdopendir(fd); - if (!dir) { - close(fd); - goto skip_pe_pmus; - } + 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)) @@ -257,7 +253,7 @@ static void pmu_read_sysfs(unsigned int to_read_types) perf_pmu__find2(fd, dent->d_name); } =20 - closedir(dir); + close(fd); } skip_pe_pmus: if ((to_read_types & PERF_TOOL_PMU_TYPE_PE_CORE_MASK) && list_empty(&core= _pmus)) { @@ -721,8 +717,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)) @@ -732,13 +728,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)) { @@ -747,7 +739,7 @@ char *perf_pmus__default_pmu_name(void) } } =20 - closedir(dir); + close(fd); return result ?: strdup("cpu"); } =20 --=20 2.48.1.658.g4767266eb4-goog From nobody Fri Dec 19 07:50:26 2025 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C1B43202F61 for ; Sat, 22 Feb 2025 06:10:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740204635; cv=none; b=oIgXKkQF3kkG8fZsWKYhCbfN3HafaNlNnOhV1DPxHONs05wnsW8o4GW3tmdrGLIP1zViLBq8IIni7lxuwKzJqKKt4/YD0lMrAdiC/Cc8cDUgo9u32fTi2iL133mgtyEa87nkZUtRAj+i5Uahtoh5baC3ZJZ5y3f5nBCRdPHaPHk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740204635; c=relaxed/simple; bh=KmpojbQOHU3/yvskQ5S+KuVwQMXy4Kd7tQUOuLUv6hU=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Cc:Content-Type; b=GoFM4iSXetXDyZhbEFmm9Du8Xbcip58jed1Bn7dikcyd+Dv27NpNh7dcgCqplCH7bG/yXtEblU/bw6V+yuY31ue64Hq7OEx19FXzzuQ17LpV4ZuMiI6Kks5eQMwGleWc9uXcMM11D8N9++qZbxwgQY5BQ+7fcnJMGVRUXIYWEDk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=JRQjCJA9; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="JRQjCJA9" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e582bfcada6so4559718276.1 for ; Fri, 21 Feb 2025 22:10:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740204633; x=1740809433; darn=vger.kernel.org; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=HeV0TF04x4Q55zwmuqZvIQwfsPrnE53+pXX+ltT5R/I=; b=JRQjCJA9hosOSquRUJK3PYYABUrT1vMLoNQhww3OHXqdxVUHlAlPaDeXLugBgzFRZR w7MmIaLM3uaowXp3LKvUxTscpatXG9knJjs/oLQ8E/Pha2lIADoBXOlGMXwxFbjpYhwU cTQPT8uKSsji/LiDoYnkE+6EB+uyQ3E1J79MsDsuy+GTWyF6Hsz7iay4TTiWcMTQsaNw KJ3OXrn+ubN/kEyrCi0Taoa/hyETm8x5qiwR61wO9VNc1aYyOzHoRATnkdu4gzGfU9bK 1KxeqC0fy9gwkDZwxKZSZzjwr3gwPReIxOd/T6ebdp7M46h1YqSeX96E6jUnrSqK8aMD efkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740204633; x=1740809433; h=cc: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=HeV0TF04x4Q55zwmuqZvIQwfsPrnE53+pXX+ltT5R/I=; b=vjrg1eu3HFndpDIm1Sbwo/N7vvVRUWvRb7ODS0WsxUQOjV07IT0pFSIrc3jQLg7/7s Ff6G8XAkTHx7cMP3Q+RaLG4EOPtOPEWALlXz7giK52zCO6dwOhh3tLoeNmEcOuQf7G7d M0Vj0YGbOaxah6WZyBOG0j96B4uThyurUYsiQdGuOCwaLKA9ipmCOXXB2iB4E49K2Uyv rxtkcgwdWK5skRJVRbWsYaFJZcNxEA+BwdcquNDvajgEA+gcUQSN0Ea9+PVrpySgwtP2 SkZwL45AOBhpG9m2ROMHhS9znYvolVP9Kl6uYcMI/m7Y5VTeYvlFvY0L4WvPI59TuXt8 3mcA== X-Forwarded-Encrypted: i=1; AJvYcCUO8ouKzFOUFf5XJG2pHuOOhvZ1qM8VdOsa/ps3+Xq6GElw8c520pWAofQtc9erHLTCu6rqVq0bkOkDwU0=@vger.kernel.org X-Gm-Message-State: AOJu0YyzLXAbisU0dQDLehIObttGrSvhWYSu6dXHWtYZvRMvZo6MTrJS gQg9+/qTDb16JuTKTgDCA6lLuQYwhN3L7OUKlk/S0AP27Lc8rbY/lP/r1/l29gF9pP7+GBsJnNG s3wtslw== X-Google-Smtp-Source: AGHT+IE8mpRXA3wnwCNX1nBHvyWRtHCtakW5d7vzFI8JEASHw4DDWqNpJ04dj1qMI6ZICsw2IX93zX5fRaW9 X-Received: from irogers.svl.corp.google.com ([2620:15c:2c5:11:5003:3a14:d4e7:121]) (user=irogers job=sendgmr) by 2002:a25:b123:0:b0:e5d:bc9c:1c6c with SMTP id 3f1490d57ef6-e5e24675417mr517176276.5.1740204632752; Fri, 21 Feb 2025 22:10:32 -0800 (PST) Date: Fri, 21 Feb 2025 22:10:09 -0800 In-Reply-To: <20250222061015.303622-1-irogers@google.com> Message-Id: <20250222061015.303622-5-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250222061015.303622-1-irogers@google.com> X-Mailer: git-send-email 2.48.1.658.g4767266eb4-goog Subject: [PATCH v3 4/8] perf header: Switch mem topology to io_dir__readdir From: Ian Rogers To: Namhyung Kim , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Adrian Hunter , Kan Liang , Sam James , Jesper Juhl , James Clark , Zhongqiu Han , Yicong Yang , Thomas Richter , Michael Petlan , Anne Macedo , Dominique Martinet , Jean-Philippe Romain , Junhao He , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, "=?UTF-8?q?Krzysztof=20=C5=81opatowski?=" Cc: Ian Rogers 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 d06aa86352d3..1900965f8752 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" @@ -1311,11 +1312,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) @@ -1324,9 +1325,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 @@ -1338,20 +1339,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 @@ -1374,8 +1375,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; @@ -1383,14 +1384,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 @@ -1419,7 +1420,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.48.1.658.g4767266eb4-goog From nobody Fri Dec 19 07:50:26 2025 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D5916203709 for ; Sat, 22 Feb 2025 06:10:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740204637; cv=none; b=iSPaxETD4UxCyXVSAYMK/PShFAqBlU1y9XAhZEXjbAjRosb1EMiHFffpvnqyySAAumPwWoPZ3BP2drJaT3vD3lEHYZ1LG+spy627D/DPSfbre6MwVZSX5pBUuz5He+lq0f+dhr2b5p+pgPv7lsj84hEj8lI4bjMZZ3KDjRS3rWY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740204637; c=relaxed/simple; bh=LRozhCJXL5k6IKqHIrkVd30rMKT/7gwCcsKW8bf38xo=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Cc:Content-Type; b=MVPFUpWDcMsFegnQmDNd4XdeckfPsSuzzHsU0pJQpdFk3tQhtp+lkU+GlFq2nMYGJq+gXanfRSKXzZEJ6xZVYTNlNm8h5zwTXyIViU9BRV6XmSB5j1kMuSlH8lURxjcdfD154D9p7dDNdRlv9fAJzYJElXdqL32DvBT9LOyIzGE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=f0UgK3m8; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="f0UgK3m8" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6f27dd44f86so39759317b3.0 for ; Fri, 21 Feb 2025 22:10:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740204635; x=1740809435; darn=vger.kernel.org; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=TNqTKXDdLcTTDXYG7o7DuC+JW2tM4rFG1DmHs5toIAA=; b=f0UgK3m8z1M6AuzVluTZbL5POerRmG5AXxyU0W+tQvVpiVimxNFenFtkkFomxgs+RK ZvrlDi0bgAavJDkyab5YibwJjgJ3oPs7jsc0s02HTjNB3n6ZgBnOo8F7lTQNP3k8k1ha TN61x5hSWV2FYbLlPkgp2zEa0tlBTs+/noQ0ShwzoPpC2NKnI2x4BVUWnO1k2AD7Rc9a P95cz2RbkWYInk4CyHeQSq6me2KuaOV94QUezruAQciMpsDsrg6DufKrs+zaBJ3tfzKe qQSVCOE8zEYF2ro5PDD9lqSyy9mMTo1GfpxOo+L5RL2AXIzk3hLOd27JN8lbn7Qt/kY/ KP8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740204635; x=1740809435; h=cc: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=TNqTKXDdLcTTDXYG7o7DuC+JW2tM4rFG1DmHs5toIAA=; b=bvg5e/2zwu+TRiDSMaeu8rFSxEQ96kZ3sJxgbADkWJ/nYWl4748vlqEV6vbZBLwu97 +t/C+w+58GbeOgcEdBWsLhWUsNOCjVWI0WwAwD+l56oPz9c7tokTOT2s8BtKcZCc3LCT QY1vSPfoIr0whxNaxwQpGdAtXQZ4VmT+/Rj6xK0bFoRCmVnqqm7Imdp6aT/NuykoWC/a omdWiWvFVw3Y1NePd66/Diukv9hn5gVCAp/5thvMSYR8qX5VjGqbi0QvTk/9XVWIQS7u NPMDBPXgqdmTRtlbuRyEDOh6NzdsnIlwgr5veIy9UBMWUknjbkLt6A5SwF0zsr54eodY Qw8g== X-Forwarded-Encrypted: i=1; AJvYcCVQPKHE2TWVZP8r0WuJ9JREU7ks6AP+a7DadyGfHtwqcVCXhe9vJOi0OFqehw3uaZIIOZ6f+5YMSd+NNCY=@vger.kernel.org X-Gm-Message-State: AOJu0YwxBgpeK7i9xdOQZctxvyEWQ9qynIurjlp3IdGrKxVtMnRljeGV wdyNTJJ1uRbDY/Wqw7mNwylmmNTUAjgY7gFAEp3Z0NT5ntN6n6X++m6KQR8rfPa3Ymmykju53aW iDvTsLw== X-Google-Smtp-Source: AGHT+IEHISW9UexRhcuM6WLs8YyW+s2ZKyLI5YvTgb5iTjI0yLM9WEcZKp6clAbMyUZaJSQjrqAKYihmNOWq X-Received: from irogers.svl.corp.google.com ([2620:15c:2c5:11:5003:3a14:d4e7:121]) (user=irogers job=sendgmr) by 2002:a05:690c:6e0f:b0:6fb:b96f:b3e7 with SMTP id 00721157ae682-6fbcc39fd50mr7540767b3.6.1740204634743; Fri, 21 Feb 2025 22:10:34 -0800 (PST) Date: Fri, 21 Feb 2025 22:10:10 -0800 In-Reply-To: <20250222061015.303622-1-irogers@google.com> Message-Id: <20250222061015.303622-6-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250222061015.303622-1-irogers@google.com> X-Mailer: git-send-email 2.48.1.658.g4767266eb4-goog Subject: [PATCH v3 5/8] perf events: Remove scandir in thread synthesis From: Ian Rogers To: Namhyung Kim , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Adrian Hunter , Kan Liang , Sam James , Jesper Juhl , James Clark , Zhongqiu Han , Yicong Yang , Thomas Richter , Michael Petlan , Anne Macedo , Dominique Martinet , Jean-Philippe Romain , Junhao He , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, "=?UTF-8?q?Krzysztof=20=C5=81opatowski?=" Cc: Ian Rogers 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. Acked-by: Namhyung Kim 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 2dfc4260d36d..2fc4d0537840 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 @@ -767,10 +768,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) { @@ -802,16 +803,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 @@ -845,9 +849,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.48.1.658.g4767266eb4-goog From nobody Fri Dec 19 07:50:26 2025 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BBC78204590 for ; Sat, 22 Feb 2025 06:10:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740204639; cv=none; b=eNcEi0mpS7olWOzHNR/tB1ruKK05+52PtRk4OrR/Ha3VZmH8rlz2aSp8jw8nslM+5Vt942HAQr2j/cvMfDkfNs9DGNdzzqIlBhTIKqmTkNurhHH7lvYjJS6KIclDVaruEgGAVv8JfYow97nzkFMFU+86CFlfwjdAaXSFaeRG0Wg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740204639; c=relaxed/simple; bh=9ucfSZ+DSIqnt9/D0VSNTgbnBQ56WKkLObX9RoT/pvQ=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Cc:Content-Type; b=KNfnBm+TAYtSIDyDWUZe8MqKBasOfDTFpNBqAlSQvOtuF3t+nSa8SnMNFsueFWjv5jKSRq8WhQTJu6NsZUzT26q4SCRu0FO3VTJlEBweM9SLe8ZFJjDqBWDLOEoq5OE8SV5N++RQR631FylUKI7BsRsOOQFuPKytsmJ4UGBK1Bg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=j2yk/MFg; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="j2yk/MFg" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e5b2c00f76bso5211437276.0 for ; Fri, 21 Feb 2025 22:10:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740204637; x=1740809437; darn=vger.kernel.org; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=fJkz0HMkEeLA7dO/rwYMGkbidcoBGERjpw0WaVKXEv0=; b=j2yk/MFgGhZZvVOMdrH5lD0d0tF/zSXnhXF8S9r+PWmAe7ro3qfPhmjnL67t+D3nq2 jGeN40Olg3B9aOVnQn4fzP3MtD0gjIBZzgK3z3bMH7EwmZy7SKzrBSksottgzRM7tZ7Z F6djVB8m3/eoSNUaxrNoIAGOOo4cjjF37pXdW2V06DSlQz/QUixmcCTd2HRI/CTXEi1S n7841tMRi4PWRvLRE54S5zh4IaMeg9SsJljvftbSb7dfia3FFZ2wKRp5PW/oiMEoC5hO gt3PAiU7ziilRuQwnXQngXyCzEygK9+EXRRkP47wRl7MIKuSEN8WVvERxwi+7UFn+dNp 3Qhw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740204637; x=1740809437; h=cc: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=fJkz0HMkEeLA7dO/rwYMGkbidcoBGERjpw0WaVKXEv0=; b=tx5qUvL3ffhBn+10Ryu9cbCRP88XffMV8D/oAFLj1Rg2SeNhfClL1JWQ3FiHKzZVBF gsvsmVP62Teq/047SQ2Zg1/TUtRKjH65/YokdsjVzUFbXptkgyCaScUEzqSYKqdxaGF/ D+0rJHXoJEK+gTKAc8UkapyINwB2i9KvzMIuRt8MlEIquk/OC7Dc/6UTGB3qWr0j9hub BPDIreEOqQZ8fl8WFW/eXDO0bPIrIQcJ2Vinf3IaoY4nCLnn54MgHU8rgqVKfnpwxKzL B3t7yZM19iSVZGzslMNdK5AB9eCk1HtPSMISnJ5ut98789jukltQ6xgFqsN1rvXsQA8h Y1IA== X-Forwarded-Encrypted: i=1; AJvYcCV+WmCiqitVrEMktk6L1s/V6336X+FrYcIGUeUXMsOPmwmYSXW7d8+/6/oiZB2Wz+5hAT0o83Lx2ihdAag=@vger.kernel.org X-Gm-Message-State: AOJu0Yx04bz/n5q5adzvNnBWe7nrjmhIPUnWnhDgnM6pGtp9PPdOsNrA MB+MMRJd+C4/H83ab3718IIAS57qdcKFt+rQOPclq8LW99aUE0ByR35rnn7+WTNzSVSooPrQ3AF fyViocg== X-Google-Smtp-Source: AGHT+IEBspnJAKBzWkWTjIu1qILbutTNKhI5LZBLyodUxmyHgXnLBOW2hkoSK/Tlq102Fc0XLDK0NA1tOHlJ X-Received: from irogers.svl.corp.google.com ([2620:15c:2c5:11:5003:3a14:d4e7:121]) (user=irogers job=sendgmr) by 2002:a25:dc4b:0:b0:e5b:3bfe:4bf4 with SMTP id 3f1490d57ef6-e5e24844e2dmr471779276.1.1740204636687; Fri, 21 Feb 2025 22:10:36 -0800 (PST) Date: Fri, 21 Feb 2025 22:10:11 -0800 In-Reply-To: <20250222061015.303622-1-irogers@google.com> Message-Id: <20250222061015.303622-7-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250222061015.303622-1-irogers@google.com> X-Mailer: git-send-email 2.48.1.658.g4767266eb4-goog Subject: [PATCH v3 6/8] perf parse-events: Switch tracepoints to io_dir__readdir From: Ian Rogers To: Namhyung Kim , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Adrian Hunter , Kan Liang , Sam James , Jesper Juhl , James Clark , Zhongqiu Han , Yicong Yang , Thomas Richter , Michael Petlan , Anne Macedo , Dominique Martinet , Jean-Philippe Romain , Junhao He , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, "=?UTF-8?q?Krzysztof=20=C5=81opatowski?=" Cc: Ian Rogers 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/parse-events.c | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c index 6c36b98875bc..35e48fe56dfa 100644 --- a/tools/perf/util/parse-events.c +++ b/tools/perf/util/parse-events.c @@ -17,6 +17,7 @@ #include "strbuf.h" #include "debug.h" #include +#include #include #include #include @@ -554,8 +555,8 @@ static int add_tracepoint_multi_event(struct parse_even= ts_state *parse_state, struct parse_events_terms *head_config, YYLTYPE *loc) { char *evt_path; - struct dirent *evt_ent; - DIR *evt_dir; + struct io_dirent64 *evt_ent; + struct io_dir evt_dir; int ret =3D 0, found =3D 0; =20 evt_path =3D get_events_file(sys_name); @@ -563,14 +564,14 @@ static int add_tracepoint_multi_event(struct parse_ev= ents_state *parse_state, tracepoint_error(err, errno, sys_name, evt_name, loc->first_column); return -1; } - evt_dir =3D opendir(evt_path); - if (!evt_dir) { + io_dir__init(&evt_dir, open(evt_path, O_CLOEXEC | O_DIRECTORY | O_RDONLY)= ); + if (evt_dir.dirfd < 0) { put_events_file(evt_path); tracepoint_error(err, errno, sys_name, evt_name, loc->first_column); return -1; } =20 - while (!ret && (evt_ent =3D readdir(evt_dir))) { + while (!ret && (evt_ent =3D io_dir__readdir(&evt_dir))) { if (!strcmp(evt_ent->d_name, ".") || !strcmp(evt_ent->d_name, "..") || !strcmp(evt_ent->d_name, "enable") @@ -592,7 +593,7 @@ static int add_tracepoint_multi_event(struct parse_even= ts_state *parse_state, } =20 put_events_file(evt_path); - closedir(evt_dir); + close(evt_dir.dirfd); return ret; } =20 @@ -615,17 +616,23 @@ static int add_tracepoint_multi_sys(struct parse_even= ts_state *parse_state, struct parse_events_error *err, struct parse_events_terms *head_config, YYLTYPE *loc) { - struct dirent *events_ent; - DIR *events_dir; + struct io_dirent64 *events_ent; + struct io_dir events_dir; int ret =3D 0; + char *events_dir_path =3D get_tracing_file("events"); =20 - events_dir =3D tracing_events__opendir(); - if (!events_dir) { + if (!events_dir_path) { + tracepoint_error(err, errno, sys_name, evt_name, loc->first_column); + return -1; + } + io_dir__init(&events_dir, open(events_dir_path, O_CLOEXEC | O_DIRECTORY |= O_RDONLY)); + put_events_file(events_dir_path); + if (events_dir.dirfd < 0) { tracepoint_error(err, errno, sys_name, evt_name, loc->first_column); return -1; } =20 - while (!ret && (events_ent =3D readdir(events_dir))) { + while (!ret && (events_ent =3D io_dir__readdir(&events_dir))) { if (!strcmp(events_ent->d_name, ".") || !strcmp(events_ent->d_name, "..") || !strcmp(events_ent->d_name, "enable") @@ -639,8 +646,7 @@ static int add_tracepoint_multi_sys(struct parse_events= _state *parse_state, ret =3D add_tracepoint_event(parse_state, list, events_ent->d_name, evt_name, err, head_config, loc); } - - closedir(events_dir); + close(events_dir.dirfd); return ret; } =20 --=20 2.48.1.658.g4767266eb4-goog From nobody Fri Dec 19 07:50:26 2025 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 264B1204697 for ; Sat, 22 Feb 2025 06:10:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740204641; cv=none; b=hgfyTAThNRQiJl+EF3ECpl+yuKrvIjqp/NAg/ksjZ9V9gHY2Ux7K02nb1M/C2LYfTWecFYCQFIH3EgKfqn2em+/8I81nlgNQSBzF7EaEvUOYi7ZsSO04nVj9h5xNkRQhA/ddIy3PE0t2Da5NIAHtQ37MWE0Yv1wt0p8A53s0fE8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740204641; c=relaxed/simple; bh=k06H4Vcpgsd6/Mkydj/FGzxUQvxN79VwTK7kPz5nwgE=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Cc:Content-Type; b=FoU+Z1vG8mqQqu63G+qNLkgw2lc2iwubaVxZhBk6rAKlt+kFGRI3veNDt8jz0B4fNh2EvvtCsVLj1RqY0uoTUvyltWyvV1/WVUO6CIUOMe6ncczlCbkdeBFXig6iWLTbrpeSpJd7NWL5BIeEpwrD80yl3areETLz9URZKDOcv5k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=eVG46lBR; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="eVG46lBR" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6f788a1de55so38802507b3.3 for ; Fri, 21 Feb 2025 22:10:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740204639; x=1740809439; darn=vger.kernel.org; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=dc03GGKjNrJ36IJY1bOTZFOcM6Q7AOhYDNgE9sBgJC0=; b=eVG46lBRolyf0Ta+LKpWb8+WQLvHAM3M24GV2AYRfrGhkmOShGbCvz8lj+LjTgG/uk S4sBahAT8rx8oHCdHutkgmHTWisXAzkPMR3vndHlVOMLzIx16miROMu7IeVUkkEt1wJQ sgAnnKJGm6zhVMkr9qdno3EWql6QygCiAOu2akg4NyLi0+g15qMvUC0Q12UKJJyBDL6t JwjNOSswLrjBNFO8naWKdv6fk+0p3OY4YK7WQDt7q3grBORxq2xFvB3I8oT/cnwu6KZj bDiW9rQMiDi5qfVbgi5fW/K2/MH4Isd/Z1uExqK8f0s/6oz04H/vP6+CsNxokaxNVmIi J20A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740204639; x=1740809439; h=cc: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=dc03GGKjNrJ36IJY1bOTZFOcM6Q7AOhYDNgE9sBgJC0=; b=atHbnNmY0vywNFDxc7ua91TZN1nCjjnZ88tKRDZp8SdmBQGCYT43pG4Av3BgDEmOcZ OLJwg905c3Ip3MYu4JraQ60oMelxTeEcFDIEdqrwqolbKqi2emUeEz/+7be1OPSKt0Zm EueLjoH7D+3yslK2u6LXq8iXCN6nXtM9x5fwjettcMXj7H7ZjoXWXZIslDoSR1qRak8R r7Ww9ZhMfo+VoaOvFPZdc5gimN7PBmhSnr6/tPBnovK2yju0niwtivoNSEirOJ2XBmqD 3H+6nLOkCs0n1oivDRwzRIEKuXCuYEYGmHSiKXZFIjaYV9aW2mQg4vkdu54ksu9/W3FU hlpA== X-Forwarded-Encrypted: i=1; AJvYcCUjrSvgH1jPFDukG42E1gf0WbCE/utgfYexeazTXyObZQPYN01iS8L0BEx4UnlsX36isdQVZDMAFPACnl4=@vger.kernel.org X-Gm-Message-State: AOJu0YzI2kpy6heuliuk9it+Pz0i4PaAYy8PZsAsmdzXpPn6lk87xg1Z D/lVxYQXhqcxannYDt5X/o+NAjTnIIdO4ICshpKk/XN98sMWfOUG4jcnNMZ8R76OXkVMQs+Vn54 fUHMEww== X-Google-Smtp-Source: AGHT+IF78oS90ACeZ4zO0NJL6IhAOII04X3736RziRNp0CvmfHhXtOIC+7iDnuqfxH8n999RV62wjVQYprmo X-Received: from irogers.svl.corp.google.com ([2620:15c:2c5:11:5003:3a14:d4e7:121]) (user=irogers job=sendgmr) by 2002:a05:690c:4712:b0:6fb:8e5b:bd2e with SMTP id 00721157ae682-6fbcc37552dmr5271527b3.4.1740204639211; Fri, 21 Feb 2025 22:10:39 -0800 (PST) Date: Fri, 21 Feb 2025 22:10:12 -0800 In-Reply-To: <20250222061015.303622-1-irogers@google.com> Message-Id: <20250222061015.303622-8-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250222061015.303622-1-irogers@google.com> X-Mailer: git-send-email 2.48.1.658.g4767266eb4-goog Subject: [PATCH v3 7/8] perf hwmon_pmu: Switch event discovery to io_dir__readdir From: Ian Rogers To: Namhyung Kim , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Adrian Hunter , Kan Liang , Sam James , Jesper Juhl , James Clark , Zhongqiu Han , Yicong Yang , Thomas Richter , Michael Petlan , Anne Macedo , Dominique Martinet , Jean-Philippe Romain , Junhao He , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, "=?UTF-8?q?Krzysztof=20=C5=81opatowski?=" Cc: Ian Rogers 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/hwmon_pmu.c | 42 +++++++++++++++---------------------- 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/tools/perf/util/hwmon_pmu.c b/tools/perf/util/hwmon_pmu.c index acd889b2462f..3cce77fc8004 100644 --- a/tools/perf/util/hwmon_pmu.c +++ b/tools/perf/util/hwmon_pmu.c @@ -11,13 +11,13 @@ #include #include #include -#include #include #include #include #include #include #include +#include #include #include #include @@ -235,31 +235,22 @@ static void fix_name(char *p) =20 static int hwmon_pmu__read_events(struct hwmon_pmu *pmu) { - DIR *dir; - struct dirent *ent; - int dup_fd, err =3D 0; + int err =3D 0; struct hashmap_entry *cur, *tmp; size_t bkt; + struct io_dirent64 *ent; + struct io_dir dir; =20 if (pmu->pmu.sysfs_aliases_loaded) return 0; =20 - /* - * Use a dup-ed fd as closedir will close it. Use openat so that the - * directory contents are refreshed. - */ - dup_fd =3D openat(pmu->hwmon_dir_fd, ".", O_DIRECTORY); + /* Use openat so that the directory contents are refreshed. */ + io_dir__init(&dir, openat(pmu->hwmon_dir_fd, ".", O_CLOEXEC | O_DIRECTORY= | O_RDONLY)); =20 - if (dup_fd =3D=3D -1) - return -ENOMEM; + if (dir.dirfd < 0) + return -ENOENT; =20 - dir =3D fdopendir(dup_fd); - if (!dir) { - close(dup_fd); - return -ENOMEM; - } - - while ((ent =3D readdir(dir)) !=3D NULL) { + while ((ent =3D io_dir__readdir(&dir)) !=3D NULL) { enum hwmon_type type; int number; enum hwmon_item item; @@ -347,7 +338,7 @@ static int hwmon_pmu__read_events(struct hwmon_pmu *pmu) pmu->pmu.sysfs_aliases_loaded =3D true; =20 err_out: - closedir(dir); + close(dir.dirfd); return err; } =20 @@ -702,8 +693,8 @@ int hwmon_pmu__check_alias(struct parse_events_terms *t= erms, struct perf_pmu_inf int perf_pmus__read_hwmon_pmus(struct list_head *pmus) { char *line =3D NULL; - DIR *class_hwmon_dir; - struct dirent *class_hwmon_ent; + struct io_dirent64 *class_hwmon_ent; + struct io_dir class_hwmon_dir; char buf[PATH_MAX]; const char *sysfs =3D sysfs__mountpoint(); =20 @@ -711,11 +702,12 @@ int perf_pmus__read_hwmon_pmus(struct list_head *pmus) return 0; =20 scnprintf(buf, sizeof(buf), "%s/class/hwmon/", sysfs); - class_hwmon_dir =3D opendir(buf); - if (!class_hwmon_dir) + io_dir__init(&class_hwmon_dir, open(buf, O_CLOEXEC | O_DIRECTORY | O_RDON= LY)); + + if (class_hwmon_dir.dirfd < 0) return 0; =20 - while ((class_hwmon_ent =3D readdir(class_hwmon_dir)) !=3D NULL) { + while ((class_hwmon_ent =3D io_dir__readdir(&class_hwmon_dir)) !=3D NULL)= { size_t line_len; int hwmon_dir, name_fd; struct io io; @@ -745,7 +737,7 @@ int perf_pmus__read_hwmon_pmus(struct list_head *pmus) close(name_fd); } free(line); - closedir(class_hwmon_dir); + close(class_hwmon_dir.dirfd); return 0; } =20 --=20 2.48.1.658.g4767266eb4-goog From nobody Fri Dec 19 07:50:26 2025 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A0E6C204F77 for ; Sat, 22 Feb 2025 06:10:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740204646; cv=none; b=LNIw/0gHBCFGSilf90lu9vSxmW3w9qoRJ74bRhG7SPV3MwuCJ+R8nmPjIoKoI6u/Vo1x6RPWYC8K47HPcZp+QQUigLz7CmN51PcjxdRhszEvTlgxHmIZna71AxdyllSI4A3VwhOaPiVyKkn2JkM3JNW9KPrJKuemYb95kce8JzE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740204646; c=relaxed/simple; bh=0rGe2Tx7vGTWJQZpyarukIB5VD5h2OOWpGiW/oGj8Co=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Cc:Content-Type; b=XGrFR6KHzkV/f3XZnWNk7VQ7WVmtPFvUKeX4eTpr/ez8uUBRH9WcXdA+W6MODyQ4yE4jx7lssTY5i8cAuXUqiju9VmREw5+D4o2CdoJo3uGQCNetgX9I/vqq+jLIS7WozbYJk8zxWLizPGGemD5auefpIq9f6v6LRppdfnKa8Zc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=It2lLtrs; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="It2lLtrs" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e5de0ad7db6so4938719276.1 for ; Fri, 21 Feb 2025 22:10:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740204641; x=1740809441; darn=vger.kernel.org; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=GLpMV9YZOp3kQVJ6OIZMf/6FQONJ7O+cIn3Mp/VbejI=; b=It2lLtrsvQX8hGMGF0G3/hDBLRVFkJQ1jiLR+fQXgtEYirjxkyPf2mRKVHeVxUo1bs iivYJpMMXCs1AHxr9fZsz9DbFwIdL5w1tUA7qAvJ2w6kicuoFWTSPHAWR08V1Yq/tQDj RegsWV8a7SSmscoIFe5/uJQz0HhUF2ChV+gscLwpyaIU2lOPFU/acENmoPdXUig+v4cZ pt5k0515j/lUweA8ljquJAbPmwi9CDfYTj8stbHgLkHyfe3BJ0ySQ9xvv3st5cZ/6VVq 4YgcUQelhSptJiK9uhOjX8qTVXIYexkfyD3n7sxwS8MqADDeIDfIalc14cR/k1aIqBJH P4rw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740204641; x=1740809441; h=cc: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=GLpMV9YZOp3kQVJ6OIZMf/6FQONJ7O+cIn3Mp/VbejI=; b=ljv3o8jxQnq0u89i33XkxrmE68sfHfmP3YQ8I3NCuUItDIw0qzt1xczLgeRG4IWwim xWJPrLJmi8DsUsMvbzmT0ZrLsh5BxBKsXGWw4xAiSAV+DD4xWC0dtSJxmFhuLtANp+p3 O+j2hukYQ8/8Inqv6BeC2In0zOyHvp7csd40sz0OMVpxaO7aYhklaaygr5hcPKDnoriR MjhINgAuXhu7tr+wxVi2S3ZA55AusC56J9LKrOfrxurusfmgEK30AfQOCTNHKYsVryEm 3HgKBUcHWHkZsaiCTdviTF5p/AKbZVhxsddv1pM7P6xNgfXEl8A3Eu7wmwY38FltcWFG DSKQ== X-Forwarded-Encrypted: i=1; AJvYcCUgwHwZVjzv5kEaSiNNxTC8f1w+k7QSDBArp7hVabZ7WiZyXnR4C4qYQwQ/FQKMWkOTFDI8r15iR4wuzns=@vger.kernel.org X-Gm-Message-State: AOJu0YxCyiCqoo6VVzNGqzulHWFSLk5gUomGEEGR3AFrAfx7NR7aMrCC 3nBdAYQVBKXva57/9W9ZL9sdeTT+WiHUTEg95kDQoMtaTkmZ+7xu6+LYJNly+tr9OkftOLCwUxE VvTnRKg== X-Google-Smtp-Source: AGHT+IGkVRJQVO24ZIN0lXFVx1WEQn5Knpt3I24rS3BLhFGjT3PFDvokyY7XvCUGe/Psu14UYhkCKp9UY5Jr X-Received: from irogers.svl.corp.google.com ([2620:15c:2c5:11:5003:3a14:d4e7:121]) (user=irogers job=sendgmr) by 2002:a25:d6d6:0:b0:e57:f46a:98ce with SMTP id 3f1490d57ef6-e5e19290b77mr848613276.6.1740204641328; Fri, 21 Feb 2025 22:10:41 -0800 (PST) Date: Fri, 21 Feb 2025 22:10:13 -0800 In-Reply-To: <20250222061015.303622-1-irogers@google.com> Message-Id: <20250222061015.303622-9-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250222061015.303622-1-irogers@google.com> X-Mailer: git-send-email 2.48.1.658.g4767266eb4-goog Subject: [PATCH v3 8/8] perf machine: Reuse module path buffer From: Ian Rogers To: Namhyung Kim , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Adrian Hunter , Kan Liang , Sam James , Jesper Juhl , James Clark , Zhongqiu Han , Yicong Yang , Thomas Richter , Michael Petlan , Anne Macedo , Dominique Martinet , Jean-Philippe Romain , Junhao He , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, "=?UTF-8?q?Krzysztof=20=C5=81opatowski?=" Cc: Ian Rogers Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Rather than copying the path and appending the directory entry in a fresh path buffer, append to the path at the end of where it is for the recursion level. This saves a PATH_MAX buffer per recursion level and some unnecessary copying. Signed-off-by: Ian Rogers --- tools/perf/util/machine.c | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index e394c630e3a2..3f1faf94198d 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -1338,22 +1338,23 @@ static int maps__set_module_path(struct maps *maps,= const char *path, struct kmo return 0; } =20 -static int maps__set_modules_path_dir(struct maps *maps, const char *dir_n= ame, int depth) +static int maps__set_modules_path_dir(struct maps *maps, char *path, size_= t path_size, int depth) { struct io_dirent64 *dent; struct io_dir iod; + size_t root_len =3D strlen(path); int ret =3D 0; =20 - io_dir__init(&iod, open(dir_name, O_CLOEXEC | O_DIRECTORY | O_RDONLY)); + io_dir__init(&iod, open(path, O_CLOEXEC | O_DIRECTORY | O_RDONLY)); if (iod.dirfd < 0) { - pr_debug("%s: cannot open %s dir\n", __func__, dir_name); + pr_debug("%s: cannot open %s dir\n", __func__, path); return -1; } - + /* Bounds check, should never happen. */ + if (root_len >=3D path_size) + return -1; + path[root_len++] =3D '/'; while ((dent =3D io_dir__readdir(&iod)) !=3D NULL) { - char path[PATH_MAX]; - - path__join(path, sizeof(path), dir_name, dent->d_name); if (io_dir__is_dir(&iod, dent)) { if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) @@ -1366,7 +1367,12 @@ static int maps__set_modules_path_dir(struct maps *m= aps, const char *dir_name, i continue; } =20 - ret =3D maps__set_modules_path_dir(maps, path, depth + 1); + /* Bounds check, should never happen. */ + if (root_len + strlen(dent->d_name) >=3D path_size) + continue; + + strcpy(path + root_len, dent->d_name); + ret =3D maps__set_modules_path_dir(maps, path, path_size, depth + 1); if (ret < 0) goto out; } else { @@ -1376,9 +1382,14 @@ static int maps__set_modules_path_dir(struct maps *m= aps, const char *dir_name, i if (ret) goto out; =20 - if (m.kmod) - ret =3D maps__set_module_path(maps, path, &m); + if (m.kmod) { + /* Bounds check, should never happen. */ + if (root_len + strlen(dent->d_name) < path_size) { + strcpy(path + root_len, dent->d_name); + ret =3D maps__set_module_path(maps, path, &m); =20 + } + } zfree(&m.name); =20 if (ret) @@ -1404,7 +1415,8 @@ static int machine__set_modules_path(struct machine *= machine) machine->root_dir, version); free(version); =20 - return maps__set_modules_path_dir(machine__kernel_maps(machine), modules_= path, 0); + return maps__set_modules_path_dir(machine__kernel_maps(machine), + modules_path, sizeof(modules_path), 0); } int __weak arch__fix_module_text_start(u64 *start __maybe_unused, u64 *size __maybe_unused, --=20 2.48.1.658.g4767266eb4-goog