Received: by 2002:a05:7412:b795:b0:e2:908c:2ebd with SMTP id iv21csp554800rdb; Thu, 2 Nov 2023 11:01:04 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGVuJ2WGcYq8+1mVf5Dh2z84uEvkdGXRhkjJNdF5VS6a1r2GWAPfyEG4hMxK2ifCA1CbZHA X-Received: by 2002:a17:903:24f:b0:1cc:78a5:50a2 with SMTP id j15-20020a170903024f00b001cc78a550a2mr7045106plh.39.1698948063873; Thu, 02 Nov 2023 11:01:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698948063; cv=none; d=google.com; s=arc-20160816; b=Vi5iZkkql4mX9BNLZmJM3gE2MAvmYK/EI6XaVaT8HiPyrwZGfTUD+1nHKm/CX5FL1u N+CZgHPD2pTx7FerRaiEdwIeM0OWnmxdglcNqrmO9AQF8qiu0EuscA/TF3BintEpj45L nh8ZfrN+iqRUMnq1f3SwNIrKyVKT61lYLSTjSYh7sgyYf9h4wYaI/shLs+dAUIlc/xV+ bc/qvxsrDI1e0Sh3YkacX7IhSv2by0/aQnUiJIF5iQ9yHM6byplHhIX3y4IAxPS8DqZM jsC7ralC/mfe83+ogrri9mulHISlj/UkMh5Wqi+oFfQ/w0AOyYgt4KXKSvWC2pr+Ydq6 8GNA== 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=yysZzz5KsjdB52OWy1zT/Vl17Oi4/hQFvJPxuk/sMvQ=; fh=z1KcSqUpYQ9oC4uLSeIkhAYTnJ2bvP0QbNW2xqV5NqA=; b=isnCG5oVMdtcxmMI+lHExizGhmb8aB5YGOYuTuHzOEPwEADgtHsBmKXhu68t3R4OP+ peNx3OEoLx50mkZgSeN65M4YCyv9xnvbY0cPGPjUnxKYF81Kq0ePZxr/bXPCZUKl/WIe 5FgiG0z0U2KDJnNboIg3F3myxEycmeVhAwEv9UQ5HXL/mHuKkm2NQqMc7YELsKQHuC1Z VH/J/pxNSsznvhkECBUOKzQ/HYdiXOUXBOHntnLaSZTdn6SaHT/FcB3aiVKxxeRrm9PS CDA0D28wII9/hxWkZBY1qk79+8hNmJRIYX30hhF8SvXVDk8sYDueqA9HhG2whMOv8qPP fuTw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=zlq3ymrm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 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. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id kl14-20020a170903074e00b001c73e886f68si52407plb.316.2023.11.02.11.01.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Nov 2023 11:01:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=zlq3ymrm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 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 24CAE837EA5D; Thu, 2 Nov 2023 10:59:55 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377140AbjKBR7Q (ORCPT + 99 others); Thu, 2 Nov 2023 13:59:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59392 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345780AbjKBR6i (ORCPT ); Thu, 2 Nov 2023 13:58:38 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 23A0810CE for ; Thu, 2 Nov 2023 10:58:22 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d99ec34829aso1446634276.1 for ; Thu, 02 Nov 2023 10:58:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698947901; x=1699552701; 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=yysZzz5KsjdB52OWy1zT/Vl17Oi4/hQFvJPxuk/sMvQ=; b=zlq3ymrmN8Lc7ob++onxMma3rt9g17xEv5a7Y9y5s5QRHqLLTjs4xMlKXIYKYVm3EU ccBhFHXBYVEC5i3g/FYCh2UoWnYDOMgk4FRaiR9xDev0KY7/4Y/4onls/Rmrciy5AnrS Tjx1ZCKrvAf3z7TttV/KLqNO01nTRaIhIc4NZpdxRLHHhZ9nRXc1eAvyBwYy3TiJyJHc RcXFBkDV3AZ8xgv7/SlicOvdf5keaY0/MqKUqu/GiJX9vfyGaaDys/ypSJxw6OhGBQQY 5iU0ckBSLj3eKW06m0bNv9efVOcdURf0hm6bvTxE3ryUBVBCllYVv/9QvLdKil512QJq /H+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698947901; x=1699552701; 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=yysZzz5KsjdB52OWy1zT/Vl17Oi4/hQFvJPxuk/sMvQ=; b=oJSqQEy4G3hHdO+4iQgaa/HK3Cm42SW//6YEuDTPvFLrEF/rw7zYCp6OP1uURKfKD7 4S7dAB6wBlsw8njwdczUF5FajhzDuoHl4WoAjwaqqxLRvK6ChptyMM/c2KxoIIRKVQYw wcVIBKTL+FLPBa9d58NGwmsn5ribgX8Li3Fa1zurTE+rk8DIwglqnixWuYO0l/shc7ve 3V4qa5KtKgKAkne2rJF+ZFkP8COlD/4+yZF4j8t2p3pyTGOodbIBBTBKLclN2T508rqr Ol+k27gqEH0DwLADu/oVWNcnWhvBUMzUBOm4wfTF2QJgv/VHimyMCcgXMv/wWD9izerP KYEQ== X-Gm-Message-State: AOJu0YyTSlqRnhyP3cXXdL8crcT9rDBzClm+PrqKs2HbJL3ezRdLK1Qa 6rK7UoBDbthBruTRRTFr0qvDzffkX4x/ X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:bb34:df9c:836c:afca]) (user=irogers job=sendgmr) by 2002:a05:6902:1083:b0:d9a:c3b8:4274 with SMTP id v3-20020a056902108300b00d9ac3b84274mr460446ybu.7.1698947901164; Thu, 02 Nov 2023 10:58:21 -0700 (PDT) Date: Thu, 2 Nov 2023 10:56:53 -0700 In-Reply-To: <20231102175735.2272696-1-irogers@google.com> Message-Id: <20231102175735.2272696-12-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 11/53] perf pmu: Switch 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 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]); Thu, 02 Nov 2023 10:59:55 -0700 (PDT) Avoid DIR allocations when scanning sysfs by using io_dir for the readdir implementation, that allocates about 1kb on the stack. Signed-off-by: Ian Rogers --- tools/perf/util/pmu.c | 48 +++++++++++++++++------------------------- tools/perf/util/pmus.c | 30 ++++++++++---------------- 2 files changed, 30 insertions(+), 48 deletions(-) diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index d3c9aa4326be..35548bbf7d17 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -184,19 +185,17 @@ static void perf_pmu_format__load(const struct perf_pmu *pmu, struct perf_pmu_fo */ int perf_pmu__format_parse(struct perf_pmu *pmu, int dirfd, bool eager_load) { - struct dirent *evt_ent; - DIR *format_dir; + struct io_dirent64 *evt_ent; + struct io_dir format_dir; int ret = 0; - format_dir = fdopendir(dirfd); - if (!format_dir) - return -EINVAL; + io_dir__init(&format_dir, dirfd); - while ((evt_ent = readdir(format_dir)) != NULL) { + while ((evt_ent = io_dir__readdir(&format_dir)) != NULL) { struct perf_pmu_format *format; char *name = evt_ent->d_name; - if (!strcmp(name, ".") || !strcmp(name, "..")) + if (io_dir__is_dir(&format_dir, evt_ent)) continue; format = perf_pmu__new_format(&pmu->format, name); @@ -223,7 +222,7 @@ int perf_pmu__format_parse(struct perf_pmu *pmu, int dirfd, bool eager_load) } } - closedir(format_dir); + close(format_dir.dirfd); return ret; } @@ -599,8 +598,8 @@ static inline bool pmu_alias_info_file(const char *name) static int pmu_aliases_parse(struct perf_pmu *pmu) { char path[PATH_MAX]; - struct dirent *evt_ent; - DIR *event_dir; + struct io_dirent64 *evt_ent; + struct io_dir event_dir; size_t len; int fd, dir_fd; @@ -615,13 +614,9 @@ static int pmu_aliases_parse(struct perf_pmu *pmu) return 0; } - event_dir = fdopendir(dir_fd); - if (!event_dir){ - close (dir_fd); - return -EINVAL; - } + io_dir__init(&event_dir, dir_fd); - while ((evt_ent = readdir(event_dir))) { + while ((evt_ent = io_dir__readdir(&event_dir))) { char *name = evt_ent->d_name; FILE *file; @@ -651,7 +646,6 @@ static int pmu_aliases_parse(struct perf_pmu *pmu) fclose(file); } - closedir(event_dir); close (dir_fd); pmu->sysfs_aliases_loaded = true; return 0; @@ -1879,10 +1873,9 @@ static void perf_pmu__del_caps(struct perf_pmu *pmu) */ int perf_pmu__caps_parse(struct perf_pmu *pmu) { - struct stat st; char caps_path[PATH_MAX]; - DIR *caps_dir; - struct dirent *evt_ent; + struct io_dir caps_dir; + struct io_dirent64 *evt_ent; int caps_fd; if (pmu->caps_initialized) @@ -1893,24 +1886,21 @@ int perf_pmu__caps_parse(struct perf_pmu *pmu) if (!perf_pmu__pathname_scnprintf(caps_path, sizeof(caps_path), pmu->name, "caps")) return -1; - if (stat(caps_path, &st) < 0) { + caps_fd = open(caps_path, O_CLOEXEC | O_DIRECTORY | O_RDONLY); + if (caps_fd == -1) { pmu->caps_initialized = true; return 0; /* no error if caps does not exist */ } - caps_dir = opendir(caps_path); - if (!caps_dir) - return -EINVAL; - - caps_fd = dirfd(caps_dir); + io_dir__init(&caps_dir, caps_fd); - while ((evt_ent = readdir(caps_dir)) != NULL) { + while ((evt_ent = io_dir__readdir(&caps_dir)) != NULL) { char *name = evt_ent->d_name; char value[128]; FILE *file; int fd; - if (!strcmp(name, ".") || !strcmp(name, "..")) + if (io_dir__is_dir(&caps_dir, evt_ent)) continue; fd = openat(caps_fd, name, O_RDONLY); @@ -1932,7 +1922,7 @@ int perf_pmu__caps_parse(struct perf_pmu *pmu) fclose(file); } - closedir(caps_dir); + close(caps_fd); pmu->caps_initialized = true; return pmu->nr_caps; diff --git a/tools/perf/util/pmus.c b/tools/perf/util/pmus.c index ce4931461741..65b23b98666b 100644 --- a/tools/perf/util/pmus.c +++ b/tools/perf/util/pmus.c @@ -3,10 +3,10 @@ #include #include #include +#include #include #include #include -#include #include #include #include @@ -184,8 +184,8 @@ static int pmus_cmp(void *priv __maybe_unused, static void pmu_read_sysfs(bool core_only) { int fd; - DIR *dir; - struct dirent *dent; + struct io_dir dir; + struct io_dirent64 *dent; if (read_sysfs_all_pmus || (core_only && read_sysfs_core_pmus)) return; @@ -194,13 +194,9 @@ static void pmu_read_sysfs(bool core_only) if (fd < 0) return; - dir = fdopendir(fd); - if (!dir) { - close(fd); - return; - } + io_dir__init(&dir, fd); - while ((dent = readdir(dir))) { + while ((dent = io_dir__readdir(&dir)) != NULL) { if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) continue; if (core_only && !is_pmu_core(dent->d_name)) @@ -209,7 +205,7 @@ static void pmu_read_sysfs(bool core_only) perf_pmu__find2(fd, dent->d_name); } - closedir(dir); + close(fd); if (list_empty(&core_pmus)) { if (!perf_pmu__create_placeholder_core_pmu(&core_pmus)) pr_err("Failure to set up any core PMUs\n"); @@ -563,8 +559,8 @@ bool perf_pmus__supports_extended_type(void) char *perf_pmus__default_pmu_name(void) { int fd; - DIR *dir; - struct dirent *dent; + struct io_dir dir; + struct io_dirent64 *dent; char *result = NULL; if (!list_empty(&core_pmus)) @@ -574,13 +570,9 @@ char *perf_pmus__default_pmu_name(void) if (fd < 0) return strdup("cpu"); - dir = fdopendir(fd); - if (!dir) { - close(fd); - return strdup("cpu"); - } + io_dir__init(&dir, fd); - while ((dent = readdir(dir))) { + while ((dent = io_dir__readdir(&dir)) != NULL) { if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) continue; if (is_pmu_core(dent->d_name)) { @@ -589,7 +581,7 @@ char *perf_pmus__default_pmu_name(void) } } - closedir(dir); + close(fd); return result ?: strdup("cpu"); } -- 2.42.0.869.gea05f2083d-goog