perf parse-events: Avoid scanning PMUs that can't contain events
authorIan Rogers <irogers@google.com>
Tue, 24 Jun 2025 23:18:35 +0000 (16:18 -0700)
committerNamhyung Kim <namhyung@kernel.org>
Wed, 25 Jun 2025 18:12:35 +0000 (11:12 -0700)
Add perf_pmus__scan_for_event that only reads sysfs for pmus that
could contain a given event.

Signed-off-by: Ian Rogers <irogers@google.com>
Link: https://lore.kernel.org/r/20250624231837.179536-2-irogers@google.com
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
tools/perf/util/parse-events.c
tools/perf/util/pmus.c
tools/perf/util/pmus.h

index d1965a7b97ed6b97148101fb75ff68f136e9fe0d..4cd64ffa4fcd6fe8c38d4fd68117eb9ad76bced5 100644 (file)
@@ -490,7 +490,7 @@ int parse_events_add_cache(struct list_head *list, int *idx, const char *name,
        int ret = 0;
        struct evsel *first_wildcard_match = NULL;
 
-       while ((pmu = perf_pmus__scan(pmu)) != NULL) {
+       while ((pmu = perf_pmus__scan_for_event(pmu, name)) != NULL) {
                LIST_HEAD(config_terms);
                struct perf_event_attr attr;
 
@@ -1681,7 +1681,8 @@ int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
 
        INIT_LIST_HEAD(list);
 
-       while ((pmu = perf_pmus__scan(pmu)) != NULL) {
+       while ((pmu = perf_pmus__scan_for_event(pmu, event_name)) != NULL) {
+
                if (parse_events__filter_pmu(parse_state, pmu))
                        continue;
 
@@ -1760,19 +1761,21 @@ int parse_events_multi_pmu_add_or_add_pmu(struct parse_events_state *parse_state
 
        pmu = NULL;
        /* Failed to add, try wildcard expansion of event_or_pmu as a PMU name. */
-       while ((pmu = perf_pmus__scan(pmu)) != NULL) {
-               if (!parse_events__filter_pmu(parse_state, pmu) &&
-                   perf_pmu__wildcard_match(pmu, event_or_pmu)) {
-                       if (!parse_events_add_pmu(parse_state, *listp, pmu,
-                                                 const_parsed_terms,
-                                                 first_wildcard_match,
-                                                 /*alternate_hw_config=*/PERF_COUNT_HW_MAX)) {
-                               ok++;
-                               parse_state->wild_card_pmus = true;
-                       }
-                       if (first_wildcard_match == NULL)
-                               first_wildcard_match =
-                                       container_of((*listp)->prev, struct evsel, core.node);
+       while ((pmu = perf_pmus__scan_matching_wildcard(pmu, event_or_pmu)) != NULL) {
+
+               if (parse_events__filter_pmu(parse_state, pmu))
+                       continue;
+
+               if (!parse_events_add_pmu(parse_state, *listp, pmu,
+                                         const_parsed_terms,
+                                         first_wildcard_match,
+                                         /*alternate_hw_config=*/PERF_COUNT_HW_MAX)) {
+                       ok++;
+                       parse_state->wild_card_pmus = true;
+               }
+               if (first_wildcard_match == NULL) {
+                       first_wildcard_match =
+                               container_of((*listp)->prev, struct evsel, core.node);
                }
        }
        if (ok)
index 3bbd26fec78a10e2b08149d62545fceefaf7858f..e0094f56b8e7b019b5da4620f7a367642f7acea3 100644 (file)
@@ -19,6 +19,7 @@
 #include "tool_pmu.h"
 #include "print-events.h"
 #include "strbuf.h"
+#include "string2.h"
 
 /*
  * core_pmus:  A PMU belongs to core_pmus if it's name is "cpu" or it's sysfs
@@ -350,6 +351,82 @@ struct perf_pmu *perf_pmus__scan_core(struct perf_pmu *pmu)
        return NULL;
 }
 
+struct perf_pmu *perf_pmus__scan_for_event(struct perf_pmu *pmu, const char *event)
+{
+       bool use_core_pmus = !pmu || pmu->is_core;
+
+       if (!pmu) {
+               /* Hwmon filename values that aren't used. */
+               enum hwmon_type type;
+               int number;
+               /*
+                * Core PMUs, other sysfs PMUs and tool PMU can take all event
+                * types or aren't wother optimizing for.
+                */
+               unsigned int to_read_pmus =  PERF_TOOL_PMU_TYPE_PE_CORE_MASK |
+                       PERF_TOOL_PMU_TYPE_PE_OTHER_MASK |
+                       PERF_TOOL_PMU_TYPE_TOOL_MASK;
+
+               /* Could the event be a hwmon event? */
+               if (parse_hwmon_filename(event, &type, &number, /*item=*/NULL, /*alarm=*/NULL))
+                       to_read_pmus |= PERF_TOOL_PMU_TYPE_HWMON_MASK;
+
+               pmu_read_sysfs(to_read_pmus);
+               pmu = list_prepare_entry(pmu, &core_pmus, list);
+       }
+       if (use_core_pmus) {
+               list_for_each_entry_continue(pmu, &core_pmus, list)
+                       return pmu;
+
+               pmu = NULL;
+               pmu = list_prepare_entry(pmu, &other_pmus, list);
+       }
+       list_for_each_entry_continue(pmu, &other_pmus, list)
+               return pmu;
+       return NULL;
+}
+
+struct perf_pmu *perf_pmus__scan_matching_wildcard(struct perf_pmu *pmu, const char *wildcard)
+{
+       bool use_core_pmus = !pmu || pmu->is_core;
+
+       if (!pmu) {
+               /*
+                * Core PMUs, other sysfs PMUs and tool PMU can have any name or
+                * aren't wother optimizing for.
+                */
+               unsigned int to_read_pmus =  PERF_TOOL_PMU_TYPE_PE_CORE_MASK |
+                       PERF_TOOL_PMU_TYPE_PE_OTHER_MASK |
+                       PERF_TOOL_PMU_TYPE_TOOL_MASK;
+
+               /*
+                * Hwmon PMUs have an alias from a sysfs name like hwmon0,
+                * hwmon1, etc. or have a name of hwmon_<name>. They therefore
+                * can only have a wildcard match if the wildcard begins with
+                * "hwmon".
+                */
+               if (strisglob(wildcard) ||
+                   (strlen(wildcard) >= 5 && strncmp("hwmon", wildcard, 5) == 0))
+                       to_read_pmus |= PERF_TOOL_PMU_TYPE_HWMON_MASK;
+
+               pmu_read_sysfs(to_read_pmus);
+               pmu = list_prepare_entry(pmu, &core_pmus, list);
+       }
+       if (use_core_pmus) {
+               list_for_each_entry_continue(pmu, &core_pmus, list) {
+                       if (perf_pmu__wildcard_match(pmu, wildcard))
+                               return pmu;
+               }
+               pmu = NULL;
+               pmu = list_prepare_entry(pmu, &other_pmus, list);
+       }
+       list_for_each_entry_continue(pmu, &other_pmus, list) {
+               if (perf_pmu__wildcard_match(pmu, wildcard))
+                       return pmu;
+       }
+       return NULL;
+}
+
 static struct perf_pmu *perf_pmus__scan_skip_duplicates(struct perf_pmu *pmu)
 {
        bool use_core_pmus = !pmu || pmu->is_core;
index 8def20e615adcb23a1d47ea101029d3634e33d28..2794d8c3a4663f4131542cbadcd9e9b9557c5edf 100644 (file)
@@ -19,6 +19,8 @@ struct perf_pmu *perf_pmus__find_by_type(unsigned int type);
 
 struct perf_pmu *perf_pmus__scan(struct perf_pmu *pmu);
 struct perf_pmu *perf_pmus__scan_core(struct perf_pmu *pmu);
+struct perf_pmu *perf_pmus__scan_for_event(struct perf_pmu *pmu, const char *event);
+struct perf_pmu *perf_pmus__scan_matching_wildcard(struct perf_pmu *pmu, const char *wildcard);
 
 const struct perf_pmu *perf_pmus__pmu_for_pmu_filter(const char *str);