Received: by 2002:a05:7412:b795:b0:e2:908c:2ebd with SMTP id iv21csp556032rdb; Thu, 2 Nov 2023 11:02:21 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE1LhYVGD7BnDffy4SFXVaHbJEQaCsvN3iOP22M0GcxrgJVCTp//eLeao+35nujeFH3Z11t X-Received: by 2002:a05:6a00:2e07:b0:6be:43f8:4e0b with SMTP id fc7-20020a056a002e0700b006be43f84e0bmr19325145pfb.24.1698948141133; Thu, 02 Nov 2023 11:02:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698948141; cv=none; d=google.com; s=arc-20160816; b=pltvEA9jzNg3umlYlrGSWxlQSsJjq7TOtcS906KrU8KEN9RqcHRreJNFxBM5nHn9VV 4atpT/duJccZRzffcGxUPqwg//IXVRNzq+5DsUwxw4Ly8M7D9Vw8Lpb2OzTO3MkSYNQ1 MJE5HAPNcMs2GpTiwI2LTFLnbxQt9TqRzE+qC5fijWXCZ24aQEN85k5Sa2ZAzY6OtcBs WE068iBd0ZJM5vPzICEDcRe5EtYB3pzDzDFq2mUUoJCEFzBgboRIta2ueO7FIGnTbW6y AfBLUthqR4IlRFQtyNiDSSJksE5AbNIBPfckI3BR4l5AAhW2oYlguqlGV2961sPP7rT9 yOYw== 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=EV9D1jPa+kElHXElyrI3gRx863B2kEe3iRpQbNSicgw=; fh=z1KcSqUpYQ9oC4uLSeIkhAYTnJ2bvP0QbNW2xqV5NqA=; b=bMhAypAQnGkwsOpppD1ouK3gS4gHmBhxlf9MUDAUc9jlpU6wzxCfzsXE9XldWg07/n pMaM2xfgQtGUaUlq4oHSyO9pMmWTPAJAvNQEXQgKfSAc43pq0r9L9LpFleRwHLWlVc+m aQYlUX5c5wOa82gU8MHgK9Vy9IhiIYqKV6q+JOnFPEg4ywerzaiWSqxWk/W5llXfeP3b NMuieqmBy+70GKqJAaC545fb1H1eHpRJAJinbqsCA1V526NfzAKyUUQoqkRs9yCHtuEB HJURfm7Q7eCHkrhJMj3QS3gyGn2Z6YuzctauJHdHE1+Nu/rQqo3biDgkB6Cfaft0/Asx +Psw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=GL4apXn0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 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 groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id v22-20020aa78516000000b006bfd890289dsi61856pfn.366.2023.11.02.11.02.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Nov 2023 11:02:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=GL4apXn0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 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 groat.vger.email (Postfix) with ESMTP id 368E9803560C; Thu, 2 Nov 2023 11:01:55 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346415AbjKBR7X (ORCPT + 99 others); Thu, 2 Nov 2023 13:59:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60972 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234389AbjKBR6l (ORCPT ); Thu, 2 Nov 2023 13:58:41 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D38CC10EC for ; Thu, 2 Nov 2023 10:58:26 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5a7af53bde4so17607827b3.0 for ; Thu, 02 Nov 2023 10:58:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698947906; x=1699552706; 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=EV9D1jPa+kElHXElyrI3gRx863B2kEe3iRpQbNSicgw=; b=GL4apXn023PImmRdYsH9t5iRcNNCTYzxVZ2yb/mRuJ7NBeu8H0qrwZr7KaWeohmZip MXsjRdDgsyaLLsEdladjz0bNE6IJCzmZHKcJCsl8l8BU8VKxMGAlaC1tFVgi1pJhFDdd YagFo9LHZGQfOFThW6bnXmsE4jEkmfS6IlH+KHenV/t5BC+BE04zo2fDxgCMTtm9nbZW Kd00BeMIOIuRDwkOsI7RK4DN0Au2sPkkz3fiLWsc1Xe/6s8YOtLWp0hIWSqp9R/dBiRK XC1Q9mR2CX3Bqp0poGluVKfOBCC0NWK5NS+kxi0WBnHD3Eudt4Rvsxs2MeWIFdlgiBPA W3DA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698947906; x=1699552706; 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=EV9D1jPa+kElHXElyrI3gRx863B2kEe3iRpQbNSicgw=; b=RisOJo2IffSGDQvxJrZ+NSBFZGpuLDcpZ5xkCBac/itYneXw8jEe2hTxf09LFJ3SEW CwKXKr1wPZlR6g8g8lo9VAwVHO0cmBsb+S7vkVPg5qA7aBQUhsLv6CMrdFam1k6+Xu51 e07BIsckxsSpL5/yMJM8fRD44M+7gsra+5N2IcCWmRYQ0y3KggtIXQs3xNXbPOLjRC1B oT8pFIHyRy+iJqdMP0Uyqpet9n6DB6gL7syREVzikr7CGIWKzbeiGeKL2gtAMHtt7FBf C1hf9bgdurhyeV0ffjRzT/0Ddb5a67aXXP/UYUpZV0bv3rSCuXHuAoSVtlwO0TFJdKF7 WHmg== X-Gm-Message-State: AOJu0YxVjXLcsN/Hw6hz9jTNHggd/bACveRfHWA5RuWxnhDbZyZz7V13 ofd2qLwVbUpxGeSQu5LuOHChcT5wPv8s X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:bb34:df9c:836c:afca]) (user=irogers job=sendgmr) by 2002:a81:83c6:0:b0:59b:eea4:a5a6 with SMTP id t189-20020a8183c6000000b0059beea4a5a6mr8562ywf.0.1698947906006; Thu, 02 Nov 2023 10:58:26 -0700 (PDT) Date: Thu, 2 Nov 2023 10:56:55 -0700 In-Reply-To: <20231102175735.2272696-1-irogers@google.com> Message-Id: <20231102175735.2272696-14-irogers@google.com> Mime-Version: 1.0 References: <20231102175735.2272696-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.869.gea05f2083d-goog Subject: [PATCH v4 13/53] 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 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 groat.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 (groat.vger.email [0.0.0.0]); Thu, 02 Nov 2023 11:01:55 -0700 (PDT) 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 = 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.42.0.869.gea05f2083d-goog