Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp3427757rdh; Mon, 27 Nov 2023 14:10:59 -0800 (PST) X-Google-Smtp-Source: AGHT+IHgRFTG1GOWtGdg640tgA2CVl96wwXocBMQXUg+rO2hJDW7TICI9jJiZnA/IJT0h53Byqr7 X-Received: by 2002:a17:902:d487:b0:1cf:dbf8:f233 with SMTP id c7-20020a170902d48700b001cfdbf8f233mr4058060plg.3.1701123059227; Mon, 27 Nov 2023 14:10:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701123059; cv=none; d=google.com; s=arc-20160816; b=SQpUQ3cv2gvkbQG4o/yN6X1dlailL+iz8vPbz+0ZMeePpNEKYP19EeJoaG1yEHXyir 64qlxRbwJop+9HvA6X9uaJB6V8CifilusKxLWg51iG4/yki1TpG2SGiMFIi4ki/y8K1f F7+nEoydOD7m1DCTtHnDtb57jNordFELpJ6xvMyG+jjW5kgCExleRsZCw5137N6q5FZ2 n9/Yr6d/N/CezjO3x46XyLHDCIIVIx3CJNWFhq54eMVXpoduauvrseEix8HNgZR73aB9 0gd4TUwB/D08CAocNZf2NDJpA4bCBqjCNvFGplAP5ys8WjgXrxhC0D+RLoYiyGc9CKOn 8F6Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=TbYo/e+Oo6OZ75p0Cqn890sWL5w2DTMT0czkndzQENc=; fh=Io7x3OacpefNRRNKoYAzVE0aWTDwD7FRzyCdD1e3AN4=; b=A074GuTODXFtSmyT1iFHXJTdDIvDx4fbiNF8+K8cQaeTyhb6wCND3gH/iY3BbT3oOS ooluywGuQhRvIcuS9F8B+i3u3U1BEI19YQwj0+yQ2tqAj9Ij22xRHS/pRVzG6hmbUqMj M7cSFzFR4n6wAE1xqYwvMbWkT4vAJ5LuFmA8h/NjF5g8W0Gh7D9AGvHn6Rvft8uX0aY1 RfbhNxEC32xqoJ5d9OdV2pPpzoTTivbXj46u3Vl1r7kxYhtUWfriBwtr+uCNo9JI3/UL RUXi9+1tGng91NQcclrrbK5q/tMk+uK4/uMcPo2jgTpTHuITvcZNys5qHYO2zpas2koP NI6A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=zRWBlilo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id x4-20020a170902820400b001ce67644c86si10199878pln.567.2023.11.27.14.10.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 14:10:59 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=zRWBlilo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 473748096BAF; Mon, 27 Nov 2023 14:10:56 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233635AbjK0WKO (ORCPT + 99 others); Mon, 27 Nov 2023 17:10:14 -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-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 Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Mon, 27 Nov 2023 14:10:56 -0800 (PST) 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 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 = 0; char path[PATH_MAX]; - struct dirent *ent; - DIR *dir; + struct io_dirent64 *ent; + struct io_dir dir; #define for_each_memory(mem, dir) \ - while ((ent = readdir(dir))) \ + while ((ent = io_dir__readdir(&dir)) != NULL) \ if (strcmp(ent->d_name, ".") && \ strcmp(ent->d_name, "..") && \ sscanf(ent->d_name, "memory%u", &mem) == 1) @@ -1354,9 +1355,9 @@ static int memory_node__read(struct memory_node *n, unsigned long idx) "%s/devices/system/node/node%lu", sysfs__mountpoint(), idx); - dir = 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; } @@ -1368,20 +1369,20 @@ static int memory_node__read(struct memory_node *n, unsigned long idx) n->set = bitmap_zalloc(size); if (!n->set) { - closedir(dir); + close(dir.dirfd); return -ENOMEM; } n->node = idx; n->size = size; - rewinddir(dir); + io_dir__rewinddir(&dir); for_each_memory(phys, dir) { __set_bit(phys, n->set); } - closedir(dir); + close(dir.dirfd); return 0; } @@ -1404,8 +1405,8 @@ static int memory_node__sort(const void *a, const void *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 = 0; size_t cnt = 0, size = 0; struct memory_node *nodes = 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()); - dir = 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 information?\n", __func__, path); return -1; } - while (!ret && (ent = readdir(dir))) { + while (!ret && (ent = io_dir__readdir(&dir))) { unsigned int idx; int r; @@ -1447,7 +1448,7 @@ static int build_mem_topology(struct memory_node **nodesp, u64 *cntp) ret = memory_node__read(&nodes[cnt++], idx); } out: - closedir(dir); + close(dir.dirfd); if (!ret) { *cntp = cnt; *nodesp = nodes; -- 2.43.0.rc1.413.gea7ed67945-goog