From nobody Thu Jan 1 08:57:42 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C362CC25B47 for ; Tue, 24 Oct 2023 22:36:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344713AbjJXWgQ (ORCPT ); Tue, 24 Oct 2023 18:36:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39888 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344706AbjJXWgC (ORCPT ); Tue, 24 Oct 2023 18:36:02 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 549F5213C for ; Tue, 24 Oct 2023 15:25:06 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5a7b10c488cso62544877b3.2 for ; Tue, 24 Oct 2023 15:25:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186304; x=1698791104; 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=Tu7+ha5Pu/Ntln1eJ+Wk/N1rQ4pn7e+WtxhSwRWOU6E=; b=nWMr1oTw8roaNp+9Sylu+T0C3hxhXC7feCnrnJO42Ym/bW5suRUwgU5aH5oox+Blw2 opyC/uehKO8wdm6bpayS0ZHdEvCaEXUJnCP5YqnNWgKv1jws5+ls0UtZiMPGAh+EjGVS tR/hRFabU+/Zl0yQV1rDsZQr6ICwIDMzqQU3EftDDj1Jj1B9VdHBbQ6iDab33maBTpV0 WUJandZ00NdLY1NpXilU4zaWq3pq6Ug9Ztae7X2scryhQSfxwZHi0bMUAzp/t/VZ74fb z23Jr/ruVmZ3pV2zAFxHAidX/mj5QiUZmGPSChC87GmTfV/X3GYGkMC5g6yE7FBQL2lE agUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186304; x=1698791104; 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=Tu7+ha5Pu/Ntln1eJ+Wk/N1rQ4pn7e+WtxhSwRWOU6E=; b=dpY/VqDAfNzrxVE2v/ewPmAfXL6zL9ArCodL/YxQIO1IWDkp4vB88fCh6qlzXZ0H8i /UjAEctLaXj92Vp+/RvVIhPu4n91rtK/K7YQpXjuAnQHy6INUsX3mUkNWSSHrJHg8B9m fh9z0YoRSWuDIJUr9ZLZOpZS3ZfL22FO49cvzbRWo6mUR4KCThnEQyfeYzKkYJIUiDhg rI+9uUo7TOI0PO+YnDQpOiGY9JH7M63vLS8UAEzAdzEIPKfEJSBjtZ/6qWXUBUEYUSYJ q8VIts/SMSg6YhcV61/CVNDMnJPS0qjwL+++X8CaVnGJbbTJ0w92lUJSsaeuD8miYi/p ds3g== X-Gm-Message-State: AOJu0YxBPRYleDIbAArlLx3U06Ujp+Jd8my5usClKf209emkpdq/myGA +mnYJXVjKPW6Bt/0iqF+6G/lJjcKKXGY X-Google-Smtp-Source: AGHT+IHbUOpZ1mNL6MtiFPe23d21qsdlVbRyYddVESeEKyempcUhER3AlL7H32WDdpbQeSBz86Kn8XcfdQy4 X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a0d:d403:0:b0:5a7:acc1:5142 with SMTP id w3-20020a0dd403000000b005a7acc15142mr298455ywd.8.1698186304467; Tue, 24 Oct 2023 15:25:04 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:26 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-24-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 23/50] perf header: Switch mem topology to io_dir__readdir From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 e86b9439ffee..55f63d2ee232 100644 --- a/tools/perf/util/header.c +++ b/tools/perf/util/header.c @@ -44,6 +44,7 @@ #include "build-id.h" #include "data.h" #include +#include #include "asm/bug.h" #include "tool.h" #include "time-utils.h" @@ -1341,11 +1342,11 @@ static int memory_node__read(struct memory_node *n,= unsigned long idx) { unsigned int phys, size =3D 0; char path[PATH_MAX]; - struct dirent *ent; - DIR *dir; + struct io_dirent64 *ent; + struct io_dir dir; =20 #define for_each_memory(mem, dir) \ - while ((ent =3D readdir(dir))) \ + while ((ent =3D io_dir__readdir(&dir)) !=3D NULL) \ if (strcmp(ent->d_name, ".") && \ strcmp(ent->d_name, "..") && \ sscanf(ent->d_name, "memory%u", &mem) =3D=3D 1) @@ -1354,9 +1355,9 @@ static int memory_node__read(struct memory_node *n, u= nsigned long idx) "%s/devices/system/node/node%lu", sysfs__mountpoint(), idx); =20 - dir =3D opendir(path); - if (!dir) { - pr_warning("failed: can't open memory sysfs data\n"); + io_dir__init(&dir, open(path, O_CLOEXEC | O_DIRECTORY | O_RDONLY)); + if (dir.dirfd < 0) { + pr_warning("failed: can't open memory sysfs data '%s'\n", path); return -1; } =20 @@ -1368,20 +1369,20 @@ static int memory_node__read(struct memory_node *n,= unsigned long idx) =20 n->set =3D bitmap_zalloc(size); if (!n->set) { - closedir(dir); + close(dir.dirfd); return -ENOMEM; } =20 n->node =3D idx; n->size =3D size; =20 - rewinddir(dir); + io_dir__rewinddir(&dir); =20 for_each_memory(phys, dir) { __set_bit(phys, n->set); } =20 - closedir(dir); + close(dir.dirfd); return 0; } =20 @@ -1404,8 +1405,8 @@ static int memory_node__sort(const void *a, const voi= d *b) static int build_mem_topology(struct memory_node **nodesp, u64 *cntp) { char path[PATH_MAX]; - struct dirent *ent; - DIR *dir; + struct io_dirent64 *ent; + struct io_dir dir; int ret =3D 0; size_t cnt =3D 0, size =3D 0; struct memory_node *nodes =3D NULL; @@ -1413,14 +1414,14 @@ static int build_mem_topology(struct memory_node **= nodesp, u64 *cntp) scnprintf(path, PATH_MAX, "%s/devices/system/node/", sysfs__mountpoint()); =20 - dir =3D opendir(path); - if (!dir) { + io_dir__init(&dir, open(path, O_CLOEXEC | O_DIRECTORY | O_RDONLY)); + if (dir.dirfd < 0) { pr_debug2("%s: couldn't read %s, does this arch have topology informatio= n?\n", __func__, path); return -1; } =20 - while (!ret && (ent =3D readdir(dir))) { + while (!ret && (ent =3D io_dir__readdir(&dir))) { unsigned int idx; int r; =20 @@ -1447,7 +1448,7 @@ static int build_mem_topology(struct memory_node **no= desp, u64 *cntp) ret =3D memory_node__read(&nodes[cnt++], idx); } out: - closedir(dir); + close(dir.dirfd); if (!ret) { *cntp =3D cnt; *nodesp =3D nodes; --=20 2.42.0.758.gaed0368e0e-goog