[PATCH RFC 7/9] perf metrics: Test metric match in metricgroup__sys_event_iter()

From: John Garry
Date: Wed Jun 28 2023 - 06:35:47 EST


When we add a metric in metricgroup__add_metric(), we first iter through
all metrics in each table from pmu_sys_event_tables tables and call
metricgroup_sys_metric_supported() to test whether we support that metric.
The second step is to call metricgroup__add_metric_sys_event_iter() ->
match_pm_metric() to check if this is actually the metric we're looking
for. It would be better before calling metricgroup_sys_metric_supported()
at all to first test whether we're interested in that metric.

Add metricgroup_iter_data.metric_name, which will be set when we're
looking for a specific metric in the iter, and check for that in
metricgroup__sys_event_iter(). In a way this duplicates the
metricgroup__add_metric_sys_event_iter() -> match_pm_metric() check, but
that is needed for other cases.

Signed-off-by: John Garry <john.g.garry@xxxxxxxxxx>
---
tools/perf/util/metricgroup.c | 6 ++++++
1 file changed, 6 insertions(+)

diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c
index 6be410363099..111ad4e3eb6b 100644
--- a/tools/perf/util/metricgroup.c
+++ b/tools/perf/util/metricgroup.c
@@ -483,6 +483,7 @@ static int metricgroup__add_to_mep_groups(const struct pmu_metric *pm,

struct metricgroup_iter_data {
pmu_metric_iter_fn fn;
+ const char *metric_name;
void *data;
};

@@ -495,6 +496,10 @@ static int metricgroup__sys_event_iter(const struct pmu_metric *pm,
{
struct metricgroup_iter_data *d = data;

+ /* We may be only interested in a specific metric */
+ if (d->metric_name && strcasecmp(d->metric_name, pm->metric_name))
+ return 0;
+
if (metricgroup_sys_metric_supported(pm, table))
return d->fn(pm, table, d->data);

@@ -1291,6 +1296,7 @@ static int metricgroup__add_metric(const char *pmu, const char *metric_name, con
.has_match = &has_match,
.ret = &ret,
},
+ .metric_name = metric_name,
};

pmu_for_each_sys_metric(metricgroup__sys_event_iter, &data);
--
2.35.3