From nobody Thu Dec 18 08:36:24 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 42620C83003 for ; Thu, 24 Aug 2023 04:15:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240155AbjHXEPN (ORCPT ); Thu, 24 Aug 2023 00:15:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51690 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239977AbjHXEOZ (ORCPT ); Thu, 24 Aug 2023 00:14:25 -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 E901110F8 for ; Wed, 23 Aug 2023 21:14:22 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-58c8b2d6784so84343177b3.3 for ; Wed, 23 Aug 2023 21:14:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1692850462; x=1693455262; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=d5aK29bVjoAy4Lg42V+uHrKm1O4Ro++evLmNFf4Bj+4=; b=Ej47e+lD/6RUF8E+q/OUQboUGSy2GppKYeKdXAutMPAcnUr7Oca2I73HS0yMkqvY5h y2Yxb0wZ7ScRzX0SnUqDplzs24HAFOEwAmn2mCsGXqmUfM3hwlahCn0u1QhA7mK6c8gU B5gFziscdZojWeWII4OGzloZcOiq59cxPW5rPgR/JheuX0dkckaHIQTBK9HBQs07Fyzc J7SCcFy9whThNyXSRk1H0UPmzj5gFSTLYmaiaUftdNUFZvRrkDujO9yQIzD7e+WTmqCg C4XSZs4UaRILWM/KRW7hjR5qsGIs8h+lLjBk9kZIQIMysbgVI8ahGgEiFAa082FUxTv8 /jHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692850462; x=1693455262; 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=d5aK29bVjoAy4Lg42V+uHrKm1O4Ro++evLmNFf4Bj+4=; b=JTERxrr20fBJUMDo79ESbvk9jTXcj/dMz/rDYA8r8r9Et2XuBnx3JeKtx2tvOnPDfv Gm3cac3AgWN40TxXR0B26rSiTHSQl4C9jetlK1/9xj5uofFVdQMsbFTmt1G7hdw2ZTCk tKvspJLZb83aMeOlR8AAbNHsd4NsrCKzO/fRSoN/WkZ6jOzLlxBjwo+F0h4RE5I9xtZg JyQZuwvAvNEoRJB+UNu/dwpUf2+USrVTdb5avMtA+FNgv8Pd/+YmhZVXf/7MpX0pld8h uyd8ug1x/L5oS5/kKIU78ouy3P/+qZ1UG4+Jqw1d56ILQzWGi8nfN0rd9l9K31gSTYh2 ez6A== X-Gm-Message-State: AOJu0Yws2JeJQ7e77lXw2YtvH7xcGG1zWXVB31AfFyGUVJH/oku5cYJF 05D1EFpOSTfYFlcLkheWCcp+1yqEyWcF X-Google-Smtp-Source: AGHT+IFnH0XPJnTREa9Kk0vTe/JHpq8PlttbOJfotvZXiFY17QPpGIDOlBQNjC+LqyK4y+pRGiO5kl4zlOYR X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:fbb9:d9e7:7405:2651]) (user=irogers job=sendgmr) by 2002:a25:2442:0:b0:d11:6816:2d31 with SMTP id k63-20020a252442000000b00d1168162d31mr184546ybk.7.1692850462167; Wed, 23 Aug 2023 21:14:22 -0700 (PDT) Date: Wed, 23 Aug 2023 21:13:27 -0700 In-Reply-To: <20230824041330.266337-1-irogers@google.com> Message-Id: <20230824041330.266337-16-irogers@google.com> Mime-Version: 1.0 References: <20230824041330.266337-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.rc1.204.g551eb34607-goog Subject: [PATCH v2 15/18] perf pmu: Be lazy about loading event info files from sysfs From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , James Clark , Kan Liang , John Garry , Kajol Jain , Jing Zhang , Ravi Bangoria , Rob Herring , Gaosheng Cui , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Event info is only needed when an event is parsed or when merging data from an json and sysfs event. Be lazy in its loading to reduce file accesses. Signed-off-by: Ian Rogers --- tools/perf/util/pmu.c | 128 +++++++++++++++++++++++++++--------------- 1 file changed, 83 insertions(+), 45 deletions(-) diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index 9e3b72d84168..493d3e59fd50 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -58,6 +58,11 @@ struct perf_pmu_alias { struct list_head terms; /** @list: List element of struct perf_pmu aliases. */ struct list_head list; + /** + * @pmu_name: The name copied from the json struct pmu_event. This can + * differ from the PMU name as it won't have suffixes. + */ + char *pmu_name; /** @unit: Units for the event, such as bytes or cache lines. */ char unit[UNIT_MAX_LEN+1]; /** @scale: Value to scale read counter values by. */ @@ -79,11 +84,10 @@ struct perf_pmu_alias { * default. */ bool deprecated; - /** - * @pmu_name: The name copied from the json struct pmu_event. This can - * differ from the PMU name as it won't have suffixes. - */ - char *pmu_name; + /** @from_sysfs: Was the alias from sysfs or a json event? */ + bool from_sysfs; + /** @info_loaded: Have the scale, unit and other values been read from di= sk? */ + bool info_loaded; }; =20 /** @@ -280,17 +284,21 @@ int perf_pmu__convert_scale(const char *scale, char *= *end, double *sval) return ret; } =20 -static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, int dirfd, = const char *name) +static int perf_pmu__parse_scale(struct perf_pmu *pmu, struct perf_pmu_ali= as *alias) { struct stat st; ssize_t sret; + size_t len; char scale[128]; int fd, ret =3D -1; char path[PATH_MAX]; =20 - scnprintf(path, PATH_MAX, "%s.scale", name); + len =3D perf_pmu__event_source_devices_scnprintf(path, sizeof(path)); + if (!len) + return 0; + scnprintf(path + len, sizeof(path) - len, "%s/%s.scale", pmu->name, alias= ->name); =20 - fd =3D openat(dirfd, path, O_RDONLY); + fd =3D open(path, O_RDONLY); if (fd =3D=3D -1) return -1; =20 @@ -312,15 +320,20 @@ static int perf_pmu__parse_scale(struct perf_pmu_alia= s *alias, int dirfd, const return ret; } =20 -static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, int dirfd, c= onst char *name) +static int perf_pmu__parse_unit(struct perf_pmu *pmu, struct perf_pmu_alia= s *alias) { char path[PATH_MAX]; + size_t len; ssize_t sret; int fd; =20 - scnprintf(path, PATH_MAX, "%s.unit", name); =20 - fd =3D openat(dirfd, path, O_RDONLY); + len =3D perf_pmu__event_source_devices_scnprintf(path, sizeof(path)); + if (!len) + return 0; + scnprintf(path + len, sizeof(path) - len, "%s/%s.unit", pmu->name, alias-= >name); + + fd =3D open(path, O_RDONLY); if (fd =3D=3D -1) return -1; =20 @@ -343,14 +356,18 @@ static int perf_pmu__parse_unit(struct perf_pmu_alias= *alias, int dirfd, const c } =20 static int -perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, int dirfd, const cha= r *name) +perf_pmu__parse_per_pkg(struct perf_pmu *pmu, struct perf_pmu_alias *alias) { char path[PATH_MAX]; + size_t len; int fd; =20 - scnprintf(path, PATH_MAX, "%s.per-pkg", name); + len =3D perf_pmu__event_source_devices_scnprintf(path, sizeof(path)); + if (!len) + return 0; + scnprintf(path + len, sizeof(path) - len, "%s/%s.per-pkg", pmu->name, ali= as->name); =20 - fd =3D openat(dirfd, path, O_RDONLY); + fd =3D open(path, O_RDONLY); if (fd =3D=3D -1) return -1; =20 @@ -360,15 +377,18 @@ perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias,= int dirfd, const char *nam return 0; } =20 -static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias, - int dirfd, const char *name) +static int perf_pmu__parse_snapshot(struct perf_pmu *pmu, struct perf_pmu_= alias *alias) { char path[PATH_MAX]; + size_t len; int fd; =20 - scnprintf(path, PATH_MAX, "%s.snapshot", name); + len =3D perf_pmu__event_source_devices_scnprintf(path, sizeof(path)); + if (!len) + return 0; + scnprintf(path + len, sizeof(path) - len, "%s/%s.snapshot", pmu->name, al= ias->name); =20 - fd =3D openat(dirfd, path, O_RDONLY); + fd =3D open(path, O_RDONLY); if (fd =3D=3D -1) return -1; =20 @@ -429,32 +449,52 @@ static bool assign_str(const char *name, const char *= field, char **old_str, return true; } =20 +static void read_alias_info(struct perf_pmu *pmu, struct perf_pmu_alias *a= lias) +{ + if (!alias->from_sysfs || alias->info_loaded) + return; + + /* + * load unit name and scale if available + */ + perf_pmu__parse_unit(pmu, alias); + perf_pmu__parse_scale(pmu, alias); + perf_pmu__parse_per_pkg(pmu, alias); + perf_pmu__parse_snapshot(pmu, alias); +} + +struct update_alias_data { + struct perf_pmu *pmu; + struct perf_pmu_alias *alias; +}; + static int update_alias(const struct pmu_event *pe, const struct pmu_events_table *table __maybe_unused, void *vdata) { - struct perf_pmu_alias *alias =3D vdata; + struct update_alias_data *data =3D vdata; int ret =3D 0; =20 - assign_str(pe->name, "desc", &alias->desc, pe->desc); - assign_str(pe->name, "long_desc", &alias->long_desc, pe->long_desc); - assign_str(pe->name, "topic", &alias->topic, pe->topic); - alias->per_pkg =3D pe->perpkg; - if (assign_str(pe->name, "value", &alias->str, pe->event)) { - parse_events_terms__purge(&alias->terms); - ret =3D parse_events_terms(&alias->terms, pe->event, /*input=3D*/NULL); + read_alias_info(data->pmu, data->alias); + assign_str(pe->name, "desc", &data->alias->desc, pe->desc); + assign_str(pe->name, "long_desc", &data->alias->long_desc, pe->long_desc); + assign_str(pe->name, "topic", &data->alias->topic, pe->topic); + data->alias->per_pkg =3D pe->perpkg; + if (assign_str(pe->name, "value", &data->alias->str, pe->event)) { + parse_events_terms__purge(&data->alias->terms); + ret =3D parse_events_terms(&data->alias->terms, pe->event, /*input=3D*/N= ULL); } if (!ret && pe->unit) { char *unit; =20 - ret =3D perf_pmu__convert_scale(pe->unit, &unit, &alias->scale); + ret =3D perf_pmu__convert_scale(pe->unit, &unit, &data->alias->scale); if (!ret) - snprintf(alias->unit, sizeof(alias->unit), "%s", unit); + snprintf(data->alias->unit, sizeof(data->alias->unit), "%s", unit); } return ret; } =20 -static int perf_pmu__new_alias(struct perf_pmu *pmu, int dirfd, const char= *name, +static int perf_pmu__new_alias(struct perf_pmu *pmu, const char *name, const char *desc, const char *val, FILE *val_fd, const struct pmu_event *pe) { @@ -498,16 +538,6 @@ static int perf_pmu__new_alias(struct perf_pmu *pmu, i= nt dirfd, const char *name } =20 alias->name =3D strdup(name); - if (dirfd >=3D 0) { - /* - * load unit name and scale if available - */ - perf_pmu__parse_unit(alias, dirfd, name); - perf_pmu__parse_scale(alias, dirfd, name); - perf_pmu__parse_per_pkg(alias, dirfd, name); - perf_pmu__parse_snapshot(alias, dirfd, name); - } - alias->desc =3D desc ? strdup(desc) : NULL; alias->long_desc =3D long_desc ? strdup(long_desc) : desc ? strdup(desc) : NULL; @@ -522,9 +552,15 @@ static int perf_pmu__new_alias(struct perf_pmu *pmu, i= nt dirfd, const char *name } if (!pe) { /* Update an event from sysfs with json data. */ + struct update_alias_data data =3D { + .pmu =3D pmu, + .alias =3D alias, + }; + + alias->from_sysfs =3D true; if (pmu->events_table) { if (pmu_events_table__find_event(pmu->events_table, pmu, name, - update_alias, alias) =3D=3D 0) + update_alias, &data) =3D=3D 0) pmu->loaded_json_aliases++; } } @@ -612,7 +648,7 @@ static int pmu_aliases_parse(struct perf_pmu *pmu, int = dirfd) continue; } =20 - if (perf_pmu__new_alias(pmu, dirfd, name, /*desc=3D*/ NULL, + if (perf_pmu__new_alias(pmu, name, /*desc=3D*/ NULL, /*val=3D*/ NULL, file, /*pe=3D*/ NULL) < 0) pr_debug("Cannot set up %s\n", name); fclose(file); @@ -865,7 +901,7 @@ static int pmu_add_cpu_aliases_map_callback(const struc= t pmu_event *pe, { struct perf_pmu *pmu =3D vdata; =20 - perf_pmu__new_alias(pmu, -1, pe->name, pe->desc, pe->event, /*val_fd=3D*/= NULL, pe); + perf_pmu__new_alias(pmu, pe->name, pe->desc, pe->event, /*val_fd=3D*/ NUL= L, pe); return 0; } =20 @@ -901,7 +937,7 @@ static int pmu_add_sys_aliases_iter_fn(const struct pmu= _event *pe, =20 if (!strcmp(pmu->id, pe->compat) && pmu_uncore_alias_match(pe->pmu, pmu->name)) { - perf_pmu__new_alias(pmu, -1, + perf_pmu__new_alias(pmu, pe->name, pe->desc, pe->event, @@ -1417,11 +1453,13 @@ static struct perf_pmu_alias *pmu_find_alias(struct= perf_pmu *pmu, } =20 =20 -static int check_info_data(struct perf_pmu_alias *alias, +static int check_info_data(struct perf_pmu *pmu, + struct perf_pmu_alias *alias, struct perf_pmu_info *info, struct parse_events_error *err, int column) { + read_alias_info(pmu, alias); /* * Only one term in event definition can * define unit, scale and snapshot, fail @@ -1491,7 +1529,7 @@ int perf_pmu__check_alias(struct perf_pmu *pmu, struc= t list_head *head_terms, return ret; } =20 - ret =3D check_info_data(alias, info, err, term->err_term); + ret =3D check_info_data(pmu, alias, info, err, term->err_term); if (ret) return ret; =20 --=20 2.42.0.rc1.204.g551eb34607-goog