From nobody Sun Feb 8 14:13:06 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