From nobody Wed Dec 17 12:44:11 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id F0656C4167B for ; Mon, 27 Nov 2023 22:10:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233971AbjK0WKP (ORCPT ); Mon, 27 Nov 2023 17:10:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44064 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233798AbjK0WJ5 (ORCPT ); Mon, 27 Nov 2023 17:09:57 -0500 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C201FD5D for ; Mon, 27 Nov 2023 14:09:47 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-da04776a869so4675068276.0 for ; Mon, 27 Nov 2023 14:09:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701122987; x=1701727787; 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=TbYo/e+Oo6OZ75p0Cqn890sWL5w2DTMT0czkndzQENc=; b=zRWBliloJcQUR5orxGhpuTiIircSSBMLWjvV4DjUCwWAR64s8BuQfP65IXLRzLHePi bWbhiVEp8z+RW3eezHFzXFf3zIS1Owyl1YoDpDB+OMI9f6dBGxhGNsmlbc6rPnqeUS+r WPEfxAMhI4c/W2/0/Y0eWi8GPtMonM0q4/6DM3iJqn3Lf8bH0ao6kYTUS5O39aUaiPsX 5q/GuOgtucFwt0RBgQ86xfOxHA/JxwGwTdjy1UvONg/pVS49SeZrR5QBZGeJo17IEzuP NCTJHuT+qicFdE2605RD02bk2b28ubCIy2DoXVcHxXsXulSxjnbA5w2y3zlbZCM7NA+z EuLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701122987; x=1701727787; 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=TbYo/e+Oo6OZ75p0Cqn890sWL5w2DTMT0czkndzQENc=; b=iZRr+qG2F2rbMNktfiLQoG3pdzAqfIqklEj/3cp+p/6/jRQzfsW5KdehE2qhR6zmlm 5Es2C6LGddh2icleCdqd4OmvJPVntG6sXI2QIK40Xi9yGqoxUJTuLxG2tOVQNIcuBARb G345ZcF/1BjSrLAx/557W1EFlrJ4UiGLsnBoCoAdPWPjEzhjsv4fILiCXmFnXSesbL+G qRsxO0hd6dZiEyefVf4RM8y8E+dNA7Ir6uncQDlIDcYmk6QjWprh9sbsdAvQHVNKIB8i zmxFcVArOGiviJoxnMujqCKfTBJu6HhZgVwREhB46ZOJps2fGby0YdcgJlsdOWcYvhPC Pv/A== X-Gm-Message-State: AOJu0YwUJAqLRL7UWK0ZLqcjByHnbPKTgSbhwJ2l8A1V40W+rSXg88Ru jEuB7kiSrpeWt4Wxoj9Wfd6ZI6Qpsn5O X-Google-Smtp-Source: AGHT+IHpeF45uVXnTF4U8Yf67ksrkcOvng6QBlNI4QqT8eEkVTnNz6Z0FJysgjt8llF4vNlWI3eRINwU4ohy X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:829:6e77:9093:f39b]) (user=irogers job=sendgmr) by 2002:a25:aacd:0:b0:db4:65ab:abdd with SMTP id t71-20020a25aacd000000b00db465ababddmr368950ybi.13.1701122986924; Mon, 27 Nov 2023 14:09:46 -0800 (PST) Date: Mon, 27 Nov 2023 14:08:22 -0800 In-Reply-To: <20231127220902.1315692-1-irogers@google.com> Message-Id: <20231127220902.1315692-11-irogers@google.com> Mime-Version: 1.0 References: <20231127220902.1315692-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.rc1.413.gea7ed67945-goog Subject: [PATCH v5 10/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 , Kajol Jain , Athira Rajeev , Huacai Chen , Masami Hiramatsu , Vincent Whitchurch , "Steinar H. Gunderson" , Liam Howlett , Miguel Ojeda , Colin Ian King , Dmitrii Dolgov <9erthalion6@gmail.com>, Yang Jihong , Ming Wang , James Clark , K Prateek Nayak , Sean Christopherson , Leo Yan , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Li Dong , Sandipan Das , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Guilherme Amadio 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 Acked-by: Namhyung Kim --- 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 1c687b5789c0..54f7b7ec9cdd 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.43.0.rc1.413.gea7ed67945-goog