From nobody Sun Feb 8 17:36:48 2026 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 9BAF31D90C5 for ; Fri, 7 Feb 2025 23:24:59 +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=1738970701; cv=none; b=qFWWM256cvtsoC6FuIQ1R8HvAsM7juS5fmRbQiKNsLqlq10p5e84ZrQFoJQH3pvism2Qhen3O3+rncJwrZRcpQskUJN8JUb7diCHv1PyN5zkA9Wd55ZXI/JyqAR/5pDUnDldteSu2VQsC1iTzAOC0t8kHMNOIcJ5t44W0KsZdow= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738970701; c=relaxed/simple; bh=ElWenOyfvaucerfCny5mEDKaWfo6X8s3yJAt0bwuZO0=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=oPlJ1GeI0Tczo/SmkWMs+PJPhxkyvJmcCOJh5BJbHJwc15SxKoKK8gwvsVU/W3bbGotLObPwIFk+LxSy3fUpH/1toC746NhhHVajrH05WhU4zdrO+y2qeTLI60LDTgPc8syyKzi29Br/AbYYftkneVtMmiozhvscSw96HCCjhu8= 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=BLiaoEYq; 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="BLiaoEYq" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6f2c7746509so39377747b3.1 for ; Fri, 07 Feb 2025 15:24:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738970698; x=1739575498; 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=5W/64OKdjEHEkY7BmxbGDOrXjI8zGvJwzbQ9pnE/HiI=; b=BLiaoEYqbS6vJ8bzVhDUsHQq5Y1g/9My7CyszTub7pcLEF50MqnAZYtUXGoXElK+gK fMdrM3bhkakCw631W9UHgmbgXR3CAk9MTnDb3bbx/+updGA/G8rKftAfMHCIFo220TZx TJWAX31bdjtg3eqhoVJxGE18LDuJc6lcBre5edGNh5Tjn7weJYFxCq7pKx8saWKkmemz E9djRK/YQ/yA4wcAqixW2BZJWsDhCKhFB9Q0Ao0A//dG0mi+RY93AGeS6eI/SJJ6mf8z d5Mr6ab3CqbFL8S+jLkxYII/7P+6svbNSbU1wsDZsoaTh98nvD1vkQ4BoNwQUWAWdMM7 LAmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738970698; x=1739575498; 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=5W/64OKdjEHEkY7BmxbGDOrXjI8zGvJwzbQ9pnE/HiI=; b=qXKcUJgo9964WORi+KvIAVlfe0Au+WvSxFU2snSEOtNBzT7/kY0GzkYG2qMezQpxDK MSx9H/aZNnRpQBr/SG8eBZrKk6y0t9r1T9Bp261DPNR8m39VwYE2MzKqtaMW6SXw1oMO BHHj+vEV5ctiJ7n58CdhteVz/FYkMnsz00ZlQSMt8h+SZ6qGB8EmZfzpn+1rtffKwpIJ zb6ltX9+DSbkiz576+RA1mgqxYeIjs111P2K+vhuGZ/EPRYwbc/QrRfbdqDaRvqjUFk3 b/qBCk3UbMahY3W7HI+A0pPimAtg6aExkgHna8T6JYHwlZ+0JddilA8/2QkclRqgBMlj hogA== X-Forwarded-Encrypted: i=1; AJvYcCWPSnLFdW2oo23oZ5+qPHuTtjWVdYJZ/RFVsEQ1QAVX36UiDTfAjuTIl06eyboowvElKelCCS5J/1KPBaM=@vger.kernel.org X-Gm-Message-State: AOJu0YyJHNz/Q5Rt7JrwIxJgLrQ2BpLruUKMzgVHpFgoDG672rpCaI1q IdCzPO3UG/fSYJqOyfEOw0ur3E+374HEbO8mnb/2yNaFAB5wVLtJ3TFLJRID5aiEPxPkf9YRNMe 8Eu/hZw== X-Google-Smtp-Source: AGHT+IE6ysQvWeGLDwktkI386nBL83Kpeyvrn6R1QrqV1e8oodaSQ2vxtnD60YGacTEEgesF2jQk8jzy/LsC X-Received: from irogers.svl.corp.google.com ([2620:15c:2c5:11:79e4:4a45:1e80:ace5]) (user=irogers job=sendgmr) by 2002:a25:c1c2:0:b0:e57:3500:2294 with SMTP id 3f1490d57ef6-e5b4618c4b0mr33944276.3.1738970698544; Fri, 07 Feb 2025 15:24:58 -0800 (PST) Date: Fri, 7 Feb 2025 15:24:42 -0800 In-Reply-To: <20250207232452.994822-1-irogers@google.com> Message-Id: <20250207232452.994822-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: <20250207232452.994822-1-irogers@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Subject: [PATCH v2 1/7] tools lib api: Add io_dir an allocation free readdir alternative From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , Sam James , Jesper Juhl , James Clark , Zhongqiu Han , Yicong Yang , Thomas Richter , Michael Petlan , Veronika Molnarova , 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?=" 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. 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 | 93 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 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..c84738923c96 --- /dev/null +++ b/tools/lib/api/io_dir.h @@ -0,0 +1,93 @@ +/* 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__) +#define SYS_getdents64 217 +#elif defined(__aarch64__) +#define SYS_getdents64 61 +#endif +#endif + +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); +} +#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 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 --=20 2.48.1.502.g6dc24dfdaf-goog From nobody Sun Feb 8 17:36:48 2026 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 EDBAE1DD9A8 for ; Fri, 7 Feb 2025 23:25:01 +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=1738970703; cv=none; b=EDQGSZJVw6yBq0UkOyM+Ad+zsqcrGDJK/AmtAaO7Hh9SI7LAx1TbV1LDWn7CotSlh84qjLuWv2aMkXDjbApVjdX1DDGWyxwtMqLfu/5DqDpeKBOQdo1i8t+ks5LPJuEFNgs5EJ3ugyMg2gZs5HtyJNpsuD8TNVLFG/6zj2UHWsE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738970703; c=relaxed/simple; bh=zYNGAkgMdd9XUiROyIWPJ+Mlzc02RJhIL7WMmuG/ANk=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=hZqwBY2WGv5x1c9FIbMTMao9LQb3ekFcrp8ftKgU7MCXy8HDE2NGoPDrrlNbHxPVWqjKZDg47Z1Gg04SjC77X7zTH77Ok5hD9WaN1MKxt/V7pnyGnDGn46rwSJqmlzXQv/eVnZEaUmVYUDUzRcOVHVgDk/W9Lump3Zh0hHDhVbw= 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=YCH63TyX; 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="YCH63TyX" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6f2c7008c05so39238677b3.0 for ; Fri, 07 Feb 2025 15:25:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738970701; x=1739575501; 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=SK5AV+lqqx4bn5Q1XZOeQFYE3IU1Rh1nSkTxsM9kwPs=; b=YCH63TyXDwQb4STHaWAL+hUqUdTcrNhfKwB/o5vzjOUIhohBRztfM98IeRkAgI8f3P xG6ZiXJywzOdS88kNfJ1dLlw72k9jZs9KSZZdyptKiaybvwMqoPYDO2z7VQCI7pGD4So Ae9hLOyE+07dsXSbysyhcIWma8368O7apF+phCD/6edwa5qjRFCd53Vg2R9gG4cCVo6i hQmqXh2faIIi1U/pClngYCA9h0gAb/q5zXC8J2mcpnkNO2Vz5oq8JGmcKFDkJWUNhwjc 7teFCT9ujYgvTflwda37iIIUa7f3LWFK/80VlCQ3TpTbe6iCONyIqktPnTC+Ze2yvzXX zgGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738970701; x=1739575501; 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=SK5AV+lqqx4bn5Q1XZOeQFYE3IU1Rh1nSkTxsM9kwPs=; b=Bf9ikVU8uc1NRQmGzQMRkSKiwVyLuCytKn89CPhcevXxf9bPOY4PB1wCNriAE5NEuc O2hduTQ9Bcg3EVC1Cw+VHOgT2+spdp6pLn4R1DniB3q/34u85f9wlx9dyxsHEpi8XpBj ZjkcF0Y4L+FayzUXqDSn0hPvXMwY3+31P8GVbeMGRGXAh5Q8J/K1zv9Xc7yBJtHRj3V3 1qCOdGpJIDFEFmVTxC4Us8GgQoyOGedQnshyoeLGerOiYGpDcNjtzMW/MkGaD2YWZFxZ AXfndz+4YMSQypkCysCz9+ng7S+jYopAV0sCcEz8+Ur9QxqQCaDl5XXUhheRalLug/oT EcwQ== X-Forwarded-Encrypted: i=1; AJvYcCXWVrbHGQChvQp1empoEUCzSPZwB2dl+8R8vB3oNvWLo0UeoJZjW2cBaHNVi4SEWFhFAs7Gy7AhC08M9zA=@vger.kernel.org X-Gm-Message-State: AOJu0YxMa97ecqQDGVI5nB4VqbdjWWFgtNeJ5tHrhiq65/J5K7G5L9+5 QIL/1okBNPxC197uhwUp4zR6gVU2aa2X/GjkJRtNCuVZoLPzx9yrIcySCX8BtnK9DZxcR67OS6v e/MzRSA== X-Google-Smtp-Source: AGHT+IGNMSvc4/dmPsVB5ZKRDbOu0NNtmtkE0ZYHFYRHCb/11YooZqZk50AbSjXlheuxley0tKZG/Im1zyjt X-Received: from irogers.svl.corp.google.com ([2620:15c:2c5:11:79e4:4a45:1e80:ace5]) (user=irogers job=sendgmr) by 2002:a05:690c:9315:b0:6f9:a402:ff3e with SMTP id 00721157ae682-6f9b2960936mr337647b3.5.1738970700926; Fri, 07 Feb 2025 15:25:00 -0800 (PST) Date: Fri, 7 Feb 2025 15:24:43 -0800 In-Reply-To: <20250207232452.994822-1-irogers@google.com> Message-Id: <20250207232452.994822-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: <20250207232452.994822-1-irogers@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Subject: [PATCH v2 2/7] perf maps: Switch modules tree walk to io_dir__readdir From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , Sam James , Jesper Juhl , James Clark , Zhongqiu Han , Yicong Yang , Thomas Richter , Michael Petlan , Veronika Molnarova , 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?=" 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 | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 2d51badfbf2e..bdd7969533c6 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 @@ -1354,25 +1355,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; @@ -1405,7 +1402,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.502.g6dc24dfdaf-goog From nobody Sun Feb 8 17:36:48 2026 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 6809D1DE2C0 for ; Fri, 7 Feb 2025 23:25:04 +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=1738970706; cv=none; b=lo5Q41QmdsK237pXi7rtKS+e02hfAxvC4MiPpIsIB/5/kkyEX1zFNVNOjAJ97zt/Lj1DEFJoqX3jhuJA8nzRvrRF32+41EY8Dxuu1PzPYM0hWBaxKd2qiX1Kvt0Yv87DVyKSsu+G6JGwAsh2EwJ4S38aeiObHuG+JiRr3yKVMAo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738970706; c=relaxed/simple; bh=svnWEsdd0RH2EO1mazYA60VXYRHZD9wTFEGGdpTDYK8=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=DtR2pMjxt9Gmt3aF+oVmhhU99nC9DkxHQVmPimYdevzbbSsx6FvAf9ZFkGtfPdFuCb3sC4IkD0sq6PEJuv3WPxar4JsgBT/jU+EZrFLiMYlQG+cz5M3dNzOpo86P9iXdrIWqINQACzcyAwQxiBObBtQx7/TewqgDWdj66ZVUKAQ= 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=c7uimYUq; 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="c7uimYUq" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6f2793679ebso30029367b3.0 for ; Fri, 07 Feb 2025 15:25:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738970703; x=1739575503; 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=zThM502oWmZskuFWvBnh7q8yzaYbumd3GHoPECBIme8=; b=c7uimYUqUz1SCg9xdE8CTixSK81zAcv+tZAPoX8kpdT3BpKTfRetHwsMev9GvNveaN qvpo5+PN97Qr24SYRawoeiX3Ebrrq6jj7qKN1GUuoqf/T9ygz72PhMwHyGn8MzjRLZYk 74ZAUOj6Kpl021d1yNkoK8pkru3a6m28iTwKxDYJvJnh6DbTYQiUZH99D9sdjO3oOP+l HEM9avxYmvRKHhskrmtcSzFWzNnSXcbds6APZcA6wJ0k0ADMUoeg40JxrpRKxXACdnfP lBYwig11/A33xNBynwLQo8ekcnk4P3xbkWXL2NuRx2BWITzbR0Mt6CqvmFBoRX83pRhE ozDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738970703; x=1739575503; 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=zThM502oWmZskuFWvBnh7q8yzaYbumd3GHoPECBIme8=; b=Ydvju0mtqzF++DOjIdCgOc/8AFkuli/86LgkDC9fiu1T21NK6a5/BeCzuxSFPkxL32 wFIx+gDBUEJl7gPT2MhFYjqvRMER3EggVYNoKr70n17tD3nJ01u1Luw5OBA0UI5phFU2 OX/0ssY3pITeYb7/X5+jMQrDYYkyQ+fgxUlNv0iJZPs7aQeYE0skg0J3hXvbVhtYmAHd 5X6hJvGymrPWeTfH/docNnpDxsYp8KhrPsY0xheqrOChVO+BmCJx5p/TPWtDZEmQlurh uQkE4IJ0L1TQx18oCz7qnEJM1ClYiaIAoDvvDLpPi6iY8XuJjMwph7XYYBkiKkg1Mxis IzBQ== X-Forwarded-Encrypted: i=1; AJvYcCXw+TuavqepAN+0huRYR04BCF3hicLkp9hcqb3Hn2abaNNkZlDpcDB/gPPT7iYTsyzPRIqCYRVvEs154r4=@vger.kernel.org X-Gm-Message-State: AOJu0YyHLvcgVqiGtc0tCT+HKvsheVZwfUfZqB+K6SegnkNttYATvcw8 evojc+WeISM0fuAZKcgkeH1zlybJjJX7+zy+BEujkYKIgpFlyzrOLrEK0IiW7owcjcKv4jYZTNf h4BQS6A== X-Google-Smtp-Source: AGHT+IFAhE/XkHhwi5uq3vCpmrpZE+Y0JWgUbIMBDiq2rNgMaOB6aOCcctu3WZpW4W+A+Y+gQnTMvb2kVcM0 X-Received: from irogers.svl.corp.google.com ([2620:15c:2c5:11:79e4:4a45:1e80:ace5]) (user=irogers job=sendgmr) by 2002:a05:6902:726:b0:e5b:4651:ad0c with SMTP id 3f1490d57ef6-e5b4651adbamr47032276.3.1738970703232; Fri, 07 Feb 2025 15:25:03 -0800 (PST) Date: Fri, 7 Feb 2025 15:24:44 -0800 In-Reply-To: <20250207232452.994822-1-irogers@google.com> Message-Id: <20250207232452.994822-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: <20250207232452.994822-1-irogers@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Subject: [PATCH v2 3/7] perf pmu: Switch to io_dir__readdir From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , Sam James , Jesper Juhl , James Clark , Zhongqiu Han , Yicong Yang , Thomas Richter , Michael Petlan , Veronika Molnarova , 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?=" 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 c2a15b0259cf..91ed909433cc 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.502.g6dc24dfdaf-goog From nobody Sun Feb 8 17:36:48 2026 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 B72D01DE4C8 for ; Fri, 7 Feb 2025 23:25:06 +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=1738970708; cv=none; b=Q4+RxfF1mnl/r/D6QC86h+I9eA40FH3gDRodFZk/v2RwSHnT4RKNd3dgKdMQFGneVqFhCHHPAGiwnow4vAl9/HNJEkVozeYBLTnt8O2qihhoqODjmj0hILjS7kELmR4G9paqzb8rvwlJPwCVWB+3lgLEhf29P0S6SBcCrH8OT5Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738970708; c=relaxed/simple; bh=5aRNT+AFzX7JKDjBTlrIYAKWaFe7b1kC3Ps8PgQ3sAg=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=tBHhwYk8QN8j0/mA0Pe83gMi9dRyUCFhhCMdIw+5UmkwZ2Ndy9Iimww7tW30UztG5qoEC4e0plIfevnO/g9qzx3svN0sHtsTMwLTkkrEeVOX+4ozKewaKe4amWseHFFHi/jwSKxoz9LdrsYDif1ui50jJobabrz+b+y6iQwTRTo= 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=4gteXJ6G; 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="4gteXJ6G" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6f97ec73df7so15167607b3.1 for ; Fri, 07 Feb 2025 15:25:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738970705; x=1739575505; 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=KS+tqbMW67c9gn9HPH93hQ26ck+oWW3I2CAzT0FW6oo=; b=4gteXJ6GPpm+0dqem38U4iZGdC593KVy1HjwiMQaGMA6hxD0c9wXjRVva8fD9lHFcj SjG3lBpfh0W7WPYOyRLcNb2cQFUqKyrGxr2WNOmFyLOlk0v5QLHB2OzOhl25ZbmWR4Qc LTXLuwAqLZIYI+R7GjrSLiyMa4MJBE58NoBogLQPcsPss7lRr4nVb9n8QpIBIJMbghS+ zNpAoyj4VuEbCIbmxk/EZbJPEH+9lHTFWyinYz41ThdcbWTeXQNW97yIm6MrfMMEe0+h 1TivzFFgHE03Om8Xf47INV1/rxMl9URePHztoJ04rtlm7SySKQZ6XULMBwsnCIsvwh+u QThw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738970705; x=1739575505; 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=KS+tqbMW67c9gn9HPH93hQ26ck+oWW3I2CAzT0FW6oo=; b=Q4h6hIGU4cERVk/y266bfOJHvr9bSIxFxO3VsIq4l56kecTtcRPBSJASNsUBW6Vwrj DyKwfmyNsxTHqd/OJEOJE9Wjq4PgrohCq2fT4HxE/YnTfwvt2GFAy4NusqeyqJPvubX+ gkscPXJaDRVPuTCmGqC77M0r/zC4DI8NFRPXq3sgj2bahHyuu9yukH6rQ/uVqUnNE5cj O03623DarKP40RtSr2vy3bBTuSEuS9sx/Om0ob9R96yw2rNSd1TSjVN+B+m1+p2cS98z bWY9A8K0At1xMQGUABd/eGIPhcXh0uVPtjtQFxiW0uLMmiypw1dbYmR+FbsJF9SWrR42 pqSg== X-Forwarded-Encrypted: i=1; AJvYcCXtRWmXZvTjUwedXkX9hR8cAu71HML++GFk+joBJAKPVnDEBTK3Qv1rLdYbxoImPatEzWTG2Y+ndjCTCIk=@vger.kernel.org X-Gm-Message-State: AOJu0Yw7+k/4IiJZI8D5jkWAJ6bzBFtqBHswN4BssC7ZoDE3NDl3GGH6 jXxRbTrGvTDlhR3CeH1AJ0EhmayogtyQ/N0YZfPtif5UIq2mZ5Z9kqUmFngVO+LmzyJ7R9a78AV wnVsg9Q== X-Google-Smtp-Source: AGHT+IENQY6UASBJx0jxxx9ueRpM6qeWuNuVIi1ghJIH/SvVzdy0j/DypkgqZpMb2qmbWhaduC8Lvc2+e2cx X-Received: from irogers.svl.corp.google.com ([2620:15c:2c5:11:79e4:4a45:1e80:ace5]) (user=irogers job=sendgmr) by 2002:a05:690c:25c2:b0:6f3:e216:9dd with SMTP id 00721157ae682-6f9b2914de9mr477627b3.4.1738970705693; Fri, 07 Feb 2025 15:25:05 -0800 (PST) Date: Fri, 7 Feb 2025 15:24:45 -0800 In-Reply-To: <20250207232452.994822-1-irogers@google.com> Message-Id: <20250207232452.994822-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: <20250207232452.994822-1-irogers@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Subject: [PATCH v2 4/7] perf header: Switch mem topology to io_dir__readdir From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , Sam James , Jesper Juhl , James Clark , Zhongqiu Han , Yicong Yang , Thomas Richter , Michael Petlan , Veronika Molnarova , 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?=" 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.502.g6dc24dfdaf-goog From nobody Sun Feb 8 17:36:48 2026 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 EF2FB1DE8BE for ; Fri, 7 Feb 2025 23:25:08 +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=1738970710; cv=none; b=fN+3Vh1iAYvjixHo1vj+JdcY9a534+wpwrUKsRAkB4VciDdU7omLVxxGcANs5wbDLnT/9SJymbkjb9pmmSFDQrQfSLio353rqRxoYPtPtn9lJPoQodnbjhI+j2cptBRE9l6RQBtjsqBmIsJCV4sL1aOnGq6XJZ4iQE9Vgij+k34= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738970710; c=relaxed/simple; bh=ObEhRCyFV6ghYkayj4qkPZWcTOssCEFkt4QMyf1+G88=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=XKSKuXCzIlHPlxD3nERc2rFl+EPjj12f698MVHi3cSTmko+HzyZvtW2zrP0AdLyRYrq8BcTlIfahCXedA+t0UULFY7BgIg6CGnlDXhEhcAUR7sIs2ksOe861x+j1UCq3zK5F9+bN6B6o3fdJwAenmFHEeeAAZGj+Mt0Tu/a1h3U= 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=YF4+1A2o; 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="YF4+1A2o" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e54da6701d2so3886927276.0 for ; Fri, 07 Feb 2025 15:25:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738970708; x=1739575508; 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=vdb9tKccQmSzInyuTYu1/jMSVTG4YqjN0UDkWk/dVpA=; b=YF4+1A2o606w2PX0Tc14McR9bG0X29DR/20v16yuuxvC+6Xeu2rYZRdKQ5NSUsuzet wj4o0n5W6eRgRTt+8/pUEqI1QEDwh9g0ZGu4hIolXOFFBhWde6+YcRW7qYnar5jZdBfg cvzBso458wj5l8JQQgybFH7WYtsQFjv+ewfx19iEobSiSqAXy5DjOIhEaKAdKImtSAqh el/6YXiJUZedh0/NSbyxPyFCDEOc9GGC5UpGKQEhC9wsXuqq76WT3rAobfTm8GaGZbS+ BMC9aY2BpyEvPzbyinzINscmUpjv/pilQ/MSePiVOQi/lB9IjkZ3U2o7n7xOQdj4EziS rggw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738970708; x=1739575508; 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=vdb9tKccQmSzInyuTYu1/jMSVTG4YqjN0UDkWk/dVpA=; b=trSsc+L3ITt4QmbWc1/zXAWbhUzAzZ7ZY4KikpSJDzhuOhop65fI3c5VkvEyWePBrL qOthTz64Sq1iFB8Dhl6j9M/0sSCSWdS4U4RyWpbuNNPYh176vrou8Z2FLAX2nnF/ciLW m5+ux+agJJUOxREL4+tYe7jWT5U2t/iCLTXzIgZOYtBSymCWefak7ZJeBhys0ztElMBB 7+kbiaiB8TbbSzLNiIuwcW3hmG0naR4Sl1paozz0WeMwT1NqQil6dvV+y0/F8PbNwrY8 LQLfxPSOIzI4rdncFgHQoOckFD9zR1zTh+rqG4Pf2Yz7/b1kXtbDC9IK62UtlZyzG9K8 SW7A== X-Forwarded-Encrypted: i=1; AJvYcCUKPFaUmRb3nQMPNFq+CTk5gQrCGcjAGDntqpzpf5YBdpHDU38NJTVVaWYFxCYKnmy23WZTrC/EA9GOmuI=@vger.kernel.org X-Gm-Message-State: AOJu0Yxfk+3uwoNcCqUBqMVy3xp7olMus71QKYM2QJobLw9Lr6UjP9Vc oYtBflKGFsVY75T8kXLMhlz97agXJTJTAB76whZhEuqtA+HJI75Gwa+v2yTQuaP5AVM+MteeNAD xz9OgUA== X-Google-Smtp-Source: AGHT+IERCib+1uyyjqUgaird04HbEnd296RFEB5jEzvAoiTwxBqmnTh0Azo5wFkQiLmJsdm2RtuNyjy39V4M X-Received: from irogers.svl.corp.google.com ([2620:15c:2c5:11:79e4:4a45:1e80:ace5]) (user=irogers job=sendgmr) by 2002:a5b:d04:0:b0:e5b:423e:4757 with SMTP id 3f1490d57ef6-e5b4625ad03mr43743276.4.1738970707967; Fri, 07 Feb 2025 15:25:07 -0800 (PST) Date: Fri, 7 Feb 2025 15:24:46 -0800 In-Reply-To: <20250207232452.994822-1-irogers@google.com> Message-Id: <20250207232452.994822-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: <20250207232452.994822-1-irogers@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Subject: [PATCH v2 5/7] perf events: Remove scandir in thread synthesis From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , Sam James , Jesper Juhl , James Clark , Zhongqiu Han , Yicong Yang , Thomas Richter , Michael Petlan , Veronika Molnarova , 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?=" 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 6923b0d5efed..2c1ba39dfdde 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.502.g6dc24dfdaf-goog From nobody Sun Feb 8 17:36:48 2026 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 42E0D1DF269 for ; Fri, 7 Feb 2025 23:25:11 +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=1738970712; cv=none; b=Lakw5duxponVUuNbs/EhnAtRl9bgBLS+Q0FrVQJHwWbS9b9pgtlCGNNApDf1iDrL7ChoNzPESS85Mf5plUDGXdo/AV12IeopsDeltXLYXK5O5CJtm/JCpZfFod6P2B02bH08UxGV/uUvm2CYRmj9hj58sZbuJCt/UCsxaVlKETI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738970712; c=relaxed/simple; bh=lPSjxe8EQdyLJNhxVnaEAEUzrbZ+uiKlVP9aEAxUbic=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=lMW1BRm60tFyOHdZb6/ZutVJKVvbnAj4QY2tlEVUCyzaXNQqfZ/GuPjy0qbDspT/O83vmFQi/WdItLmXCaYAokq702YHmhH+FVJUBXskAUsL9QN4Lg7aNj8GBiDgISAugv59Fq0CPbE0QhzjEmPbNfxhPphHPQhaGDPQs56mkZM= 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=sZRsMpdQ; 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="sZRsMpdQ" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e575f472438so3391303276.1 for ; Fri, 07 Feb 2025 15:25:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738970710; x=1739575510; 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=vrDPJWFQMloTVXFZxhclfRLm+mWnVUJwYXKzkzhD8f0=; b=sZRsMpdQ5XpoRvsFwyzzOzCNCKg+NPXkXU8zk7/ZPbkoxc5w8tHnurBaaUEC29ATBJ cmoADdSdryB/b7KWmV6LVqA1U1hsHlVgdDcFlnuPmCjPPijxq4jf+umxMODMn3nNcQxN Z82vzb4SNGWYKmJZHPTj8tDg6aW8cxTKwYJaw/L6MWyzKQgtzx6918eDwV+ax/45BbyG Q/LWssc9uALtInIaVbiXQUjfRgnXb+i6bNewvUYXTheXIG2fN+hDfDznDUBQ4SFmeShS i2r3KTVliikoIUYzg+8GkjhlvVJZ/3wzk8glSAevWmp6lGzZOlBxqoQcCguWpGB7djRL IEhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738970710; x=1739575510; 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=vrDPJWFQMloTVXFZxhclfRLm+mWnVUJwYXKzkzhD8f0=; b=TQ9XZHClVXLX7wekf+1XfP2s2GTsgBzh+mVjg+CvEAlHs0TOrBykcrCVQxm+kyUzNu QnNhX+08XtVqIuMqnIdHa7oO9Y4Qzfff+7NvQQ4xRTy0vY9p+eLo1rD57e7rskkCtp+P xplY1wv166VVUK1Z1S5OrFbnhspUnN9nFqdrvsyj5DRiKlHE1YWbvKBRkdwhGcEQcS+Y ZkAqYSWXP4Zm5b1agcwYYxV2lsF976iA7uKtFFZ0ph8nIsNU8q+X/pG8Md705eIKTGnS MbIHKDI5EtR+ezAUi0RDINfeFJ9ty/vOQ8lvI+oACZx/QhXUo1gctiOEiIUvTHw0/+WA ql0w== X-Forwarded-Encrypted: i=1; AJvYcCUo5rkowy20hys/RNPc6hjwV7yOj/aimSJwl89ipxofEmXO54TYkGGKtmCo7QqACawWNGM0YJ2Fjei8lT4=@vger.kernel.org X-Gm-Message-State: AOJu0YwXCZEnkjlDPfD1JwMl7EKAI7FwSPl8g37rCfb+1Dj/+DOkeWFr 8aazlTdsGlaRiVQmMW7sArNoO+SiUzOT+vXPPGPXP00mR1fqgrMvaHftVCL1PSTMz3bW4P5labu bdohpbw== X-Google-Smtp-Source: AGHT+IHN5ZtGiB8X7PuL9QQhXelXTLNxO7zsQQpubBASNLxja4NwnfQS/hy//fqiIqfp+6TVHlXERFY8gPTI X-Received: from irogers.svl.corp.google.com ([2620:15c:2c5:11:79e4:4a45:1e80:ace5]) (user=irogers job=sendgmr) by 2002:a25:5f0e:0:b0:e5b:12df:4e08 with SMTP id 3f1490d57ef6-e5b46063273mr36231276.0.1738970710243; Fri, 07 Feb 2025 15:25:10 -0800 (PST) Date: Fri, 7 Feb 2025 15:24:47 -0800 In-Reply-To: <20250207232452.994822-1-irogers@google.com> Message-Id: <20250207232452.994822-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: <20250207232452.994822-1-irogers@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Subject: [PATCH v2 6/7] perf parse-events: Switch tracepoints to io_dir__readdir From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , Sam James , Jesper Juhl , James Clark , Zhongqiu Han , Yicong Yang , Thomas Richter , Michael Petlan , Veronika Molnarova , 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?=" 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/lib/api/io_dir.h | 2 -- tools/perf/util/parse-events.c | 32 +++++++++++++++++++------------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/tools/lib/api/io_dir.h b/tools/lib/api/io_dir.h index c84738923c96..9f07460afacc 100644 --- a/tools/lib/api/io_dir.h +++ b/tools/lib/api/io_dir.h @@ -89,5 +89,3 @@ static inline bool io_dir__is_dir(const struct io_dir *io= d, struct io_dirent64 * } return dent->d_type =3D=3D DT_DIR; } - -#endif 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.502.g6dc24dfdaf-goog From nobody Sun Feb 8 17:36:48 2026 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 837C01DC04A for ; Fri, 7 Feb 2025 23:25:13 +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=1738970715; cv=none; b=Hu2mgyCyk3nBq8MwhMa88IvZ0lh//j7HKTkjKGbh8H4s1jwArYHiIcF59UoB5px0FGvkdqXpl/59P7f2HpBkS7RxZL6usDGcfkityhMXTVNWmXRWgnkAnxgI2Wo9sEVFXSsMVgnORwHZMxNUjnczwdUq1hkat365kkM7oiFmd1U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738970715; c=relaxed/simple; bh=t65Qte1hMK2K/SqGXWL6WDwX51wRPkkWC4a83KGD0zg=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=sFxkP021UK4EV6+cjZQNawGJRaRn1jBfhieITXI8abk2Ke2gzWira+hRkk8yresGcFMMFYztTkPeEm2JW50t07c5Qpc+ZsgOdnF40j7/RklXz8taSOa92QX7x4kyiyQIccECO31Joz4yeHgtsMa20lICEVTbMnEHtQ/PpEsW2L8= 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=FQb5SPO/; 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="FQb5SPO/" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6f2c7746509so39380457b3.1 for ; Fri, 07 Feb 2025 15:25:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738970712; x=1739575512; 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=dEq/K8vKiseEoA2QFSLjXeZ/dhyahLOHi8u2z0uKAPQ=; b=FQb5SPO/VOvDXGF0luobyyUEuHnJ8VICJg8mrqkN+5ywN4Kg4RrGt1L2EwR4rJ3+Gf FlCjARZ0WmhJHdeHNXJoxWYpaYaaYkQzbTkIYpox+cXJrghUOxI6oa488gfD4ze9eESj 6FvfQC9MzfTFDuATBIIcZM8Ba7A101qUMHgpUNa6rnTnrNpoR7b5PV65jg3cUsyaFr7w Wo7CyNbSKLqMMbmVo4Q9t/GZsUoqAJvyKIv53Ls2tbn4pC3Ihzz97yMoATk2/eQKd0o1 A6QnCVK2F4uOffQfTzhg1vJ+jdZs5GCqBGEQALB6kCRHHchA0hALD+7qTd+KgM4rFp24 nAFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738970712; x=1739575512; 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=dEq/K8vKiseEoA2QFSLjXeZ/dhyahLOHi8u2z0uKAPQ=; b=lhdrR6F8BIX+Eu28H5/2dlq6IipfnA3FDmaPe7k/4RIIkRW5vMSGLmYQaidwMWct0w iHN8skVfF3bs+AhadlIrmQOh4+MfDfgit2hqHTDS8zMjcHRsyBNk9mfQwidWMdt0WaWj rxyzRSa9SqEfYn/iCPxPly2J+wJMCpyZnkEu7fYu91IY0Xgo6naXO7dWQP5UVDlhUUPc 42uoiRNTe1SlkUsYFGNRWHJ3O6ADSlpQu/swJrWgX5CKDZRAm/S0QceTEFG5vXNaRA8d bFInuRzweOECE6l7mi7EoY/RzJkgXEOICrh4bnTy+dhCMWYe1lzIwal2ZsaCwgU8/KO+ +rsQ== X-Forwarded-Encrypted: i=1; AJvYcCWpTkN+QjOOGzaGlxcioRcV0fPVFWzAtq5eUbNerU9Bqb+UeaqHVFPkEZstB0xYU3in6AxYRJABcYbE7tE=@vger.kernel.org X-Gm-Message-State: AOJu0YwtuDGWXIFmHCztXKBF5YCFEP1bKT7T8KjILKvEEkuozPcbqQys JkKwNRTdzIoQQzWjQjkKgaiqb89EeTMygznlPrD9D52pdphhN5V8njJfgYKqVLQmB4heXmTjI8d cBJYB6A== X-Google-Smtp-Source: AGHT+IFnOnokinwj7W6d66xeycuW5pfirx4gJhlEr4QKRDn9npVQrsFBWLER1GrO5dxcUrZ3qNKDmQLXh2ul X-Received: from irogers.svl.corp.google.com ([2620:15c:2c5:11:79e4:4a45:1e80:ace5]) (user=irogers job=sendgmr) by 2002:a05:690c:9315:b0:6f9:7447:79a1 with SMTP id 00721157ae682-6f9b29584bdmr340987b3.6.1738970712533; Fri, 07 Feb 2025 15:25:12 -0800 (PST) Date: Fri, 7 Feb 2025 15:24:48 -0800 In-Reply-To: <20250207232452.994822-1-irogers@google.com> Message-Id: <20250207232452.994822-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: <20250207232452.994822-1-irogers@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Subject: [PATCH v2 7/7] perf hwmon_pmu: Switch event discovery to io_dir__readdir From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , Sam James , Jesper Juhl , James Clark , Zhongqiu Han , Yicong Yang , Thomas Richter , Michael Petlan , Veronika Molnarova , 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?=" 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.502.g6dc24dfdaf-goog