From patchwork Thu Nov 2 17:56:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 161013 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:8f47:0:b0:403:3b70:6f57 with SMTP id j7csp533566vqu; Thu, 2 Nov 2023 10:59:35 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGFdTkR9fkbFSCc2XeREBBMMz1AweNV4d6Omm6KlHMFwebV/H+5TC2olDZ06yhP5GxZGztj X-Received: by 2002:a05:6a21:3398:b0:181:a8ed:4898 with SMTP id yy24-20020a056a21339800b00181a8ed4898mr1808955pzb.38.1698947974957; Thu, 02 Nov 2023 10:59:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698947974; cv=none; d=google.com; s=arc-20160816; b=cHwwqbqj6W21ufBaNbILK3aYTd8aKzUETzfXwnxXZdkda6xeYdLKv0LYHzyDstemEg zkJflZQMp48hDxLE0vLWD/7YmG76D27QkvWOaMhlzBRN9Nsq07ChLCbZxMBPPhDRr99G 6yTwkXPLM9D3Pj0hf1y/Pxvd5WmChKidJkm3UGlLXAsVeLwKv3HroRkfTKlvjw+JG6xO X1A3M/RiJNWsTutp3xCSaGSXkWxNrzfaKD94wxNFrxsgT/x0gKjszYf+x1n0ov0v9gox PkWGEqm+s/3XiPNpGLGmXyucfKya3foLZg26mpj6DdzFSwnUkUdcXfnBGYD/AK5vzVNh S6Ow== 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.37 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 snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id m5-20020a632605000000b00577448019cbsi48880pgm.841.2023.11.02.10.59.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Nov 2023 10:59:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; 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.37 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 snail.vger.email (Postfix) with ESMTP id 27D8C82B1C4C; Thu, 2 Nov 2023 10:59:34 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377175AbjKBR7X (ORCPT + 35 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 X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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 (snail.vger.email [0.0.0.0]); Thu, 02 Nov 2023 10:59:34 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781476071550671024 X-GMAIL-MSGID: 1781476071550671024 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;