[PATCH v1 14/41] perf metricgroup: Pass struct perf_pmus to metricgroup__for_each_metric()
From: Ian Rogers
Date: Tue Nov 03 2026 - 18:53:55 EST
metricgroup__sys_event_iter() matched system metrics against uncore
PMUs using the live host's PMUs, so when processing a perf.data file
from another machine system metrics were matched against the wrong PMU
topology. Add a struct perf_pmus * argument to
metricgroup__for_each_metric() and use it to match the metric's compat
identifier. Callers pass the PMUs of the environment they are working
on.
Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/builtin-script.c | 4 +++-
tools/perf/util/metricgroup.c | 12 +++++++-----
tools/perf/util/metricgroup.h | 14 ++++++++++++--
tools/perf/util/print-events.c | 3 ++-
4 files changed, 24 insertions(+), 9 deletions(-)
diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
index b6691ebb1b4e..8bef76eaf67d 100644
--- a/tools/perf/builtin-script.c
+++ b/tools/perf/builtin-script.c
@@ -2528,6 +2528,7 @@ static void perf_sample__fprint_metric(struct thread *thread,
if (!init_metrics) {
/* One time initialization of stat_config and metric data. */
+ struct perf_env *env = evlist__env(evsel->evlist) ?: host_env();
struct script_find_metrics_args args = {
.evlist = evsel->evlist,
.system_wide = perf_thread_map__pid(evsel->core.threads, /*idx=*/0) == -1,
@@ -2546,7 +2547,8 @@ static void perf_sample__fprint_metric(struct thread *thread,
/*needs_sort=*/false);
}
- metricgroup__for_each_metric(pmu_metrics_table__find(), script_find_metrics, &args);
+ metricgroup__for_each_metric(perf_env__pmus(env), pmu_metrics_table__find(),
+ script_find_metrics, &args);
init_metrics = true;
}
diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c
index 100f54338e8e..876c549069e5 100644
--- a/tools/perf/util/metricgroup.c
+++ b/tools/perf/util/metricgroup.c
@@ -406,6 +406,7 @@ static bool match_pm_metric_or_groups(struct perf_pmus *pmus, const struct pmu_m
}
struct metricgroup_iter_data {
+ struct perf_pmus *pmus;
pmu_metric_iter_fn fn;
void *data;
};
@@ -421,14 +422,15 @@ static int metricgroup__sys_event_iter(const struct pmu_metric *pm,
return 0;
/* Only process with the iterator if there is a a PMU that matches the ID. */
- pmu = perf_pmus__scan_for_uncore_id(perf_env__pmus(host_env()), pmu, pm->compat);
+ pmu = perf_pmus__scan_for_uncore_id(d->pmus, pmu, pm->compat);
return pmu ? d->fn(pm, table, d->data) : 0;
}
-int metricgroup__for_each_metric(const struct pmu_metrics_table *table, pmu_metric_iter_fn fn,
- void *data)
+int metricgroup__for_each_metric(struct perf_pmus *pmus, const struct pmu_metrics_table *table,
+ pmu_metric_iter_fn fn, void *data)
{
struct metricgroup_iter_data sys_data = {
+ .pmus = pmus,
.fn = fn,
.data = data,
};
@@ -1117,7 +1119,7 @@ static int metricgroup__add_metric(struct perf_pmus *pmus, const char *pmu,
* Iterate over all metrics seeing if metric matches either the
* name or group. When it does add the metric to the list.
*/
- ret = metricgroup__for_each_metric(table, metricgroup__add_metric_callback, &data);
+ ret = metricgroup__for_each_metric(pmus, table, metricgroup__add_metric_callback, &data);
if (!ret && !data.has_match)
ret = -ENOENT;
@@ -1653,7 +1655,7 @@ bool metricgroup__has_metric_or_groups(struct perf_pmus *pmus, const char *pmu,
.metric_or_groups = metric_or_groups,
};
- return metricgroup__for_each_metric(table,
+ return metricgroup__for_each_metric(pmus, table,
metricgroup__has_metric_or_groups_callback,
&data)
? true : false;
diff --git a/tools/perf/util/metricgroup.h b/tools/perf/util/metricgroup.h
index bc5d3d21f405..2cc28b3eb983 100644
--- a/tools/perf/util/metricgroup.h
+++ b/tools/perf/util/metricgroup.h
@@ -85,8 +85,18 @@ int metricgroup__parse_groups_test(struct evlist *evlist,
const char *str,
bool cputype_filter);
-int metricgroup__for_each_metric(const struct pmu_metrics_table *table, pmu_metric_iter_fn fn,
- void *data);
+/**
+ * metricgroup__for_each_metric() - Iterate over metrics in @table and system
+ * metrics matching @pmus.
+ * @pmus: The PMUs used to match system metric compat identifiers.
+ * @table: Optional CPU metrics table to iterate over.
+ * @fn: Callback invoked for each metric.
+ * @data: User data passed to @fn.
+ *
+ * Return: 0 on success or the non-zero return value from @fn.
+ */
+int metricgroup__for_each_metric(struct perf_pmus *pmus, const struct pmu_metrics_table *table,
+ pmu_metric_iter_fn fn, void *data);
bool metricgroup__has_metric_or_groups(struct perf_pmus *pmus, const char *pmu,
const char *metric_or_groups);
unsigned int metricgroups__topdown_max_level(void);
diff --git a/tools/perf/util/print-events.c b/tools/perf/util/print-events.c
index fe48efe9aefb..6145367d2184 100644
--- a/tools/perf/util/print-events.c
+++ b/tools/perf/util/print-events.c
@@ -297,7 +297,8 @@ void metricgroup__print(const struct print_callbacks *print_cb, void *print_stat
groups.node_cmp = mep_cmp;
groups.node_delete = mep_delete;
- metricgroup__for_each_metric(table, metricgroup__add_to_mep_groups_callback, &groups);
+ metricgroup__for_each_metric(perf_env__pmus(host_env()), table,
+ metricgroup__add_to_mep_groups_callback, &groups);
for (node = rb_first_cached(&groups.entries); node; node = next) {
struct mep *me = container_of(node, struct mep, nd);
--
2.56.0.385.gac671c819a-goog