[PATCH v3 2/7] perf metric: Event "Compat" value supports matching multiple identifiers

From: Jing Zhang
Date: Tue May 30 2023 - 05:20:29 EST


The jevent "Compat" is used for uncore PMU alias or metric definitions.

The same PMU driver has different PMU identifiers due to different hardware
versions and types, but they may have some common PMU event/metric. Since a
Compat value can only match one identifier, when adding the same event
alias and metric to PMUs with different identifiers, each identifier needs
to be defined once, which is not streamlined enough.

So let "Compat" value supports matching multiple identifiers. For example,
the Compat value "arm_cmn600;arm_cmn700;arm_ci700" can match the PMU
identifier "arm_cmn600X" or "arm_cmn700X" or "arm_ci700X", where "X" is a
wildcard. Tokens in Unit field are delimited by ';'.

Signed-off-by: Jing Zhang <renyu.zj@xxxxxxxxxxxxxxxxx>
---
tools/perf/util/metricgroup.c | 24 +++++++++++++++++++++++-
1 file changed, 23 insertions(+), 1 deletion(-)

diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c
index f3559be..c12ccd9 100644
--- a/tools/perf/util/metricgroup.c
+++ b/tools/perf/util/metricgroup.c
@@ -456,6 +456,28 @@ struct metricgroup_iter_data {
void *data;
};

+static bool match_pmu_identifier(const char *id, const char *compat)
+{
+ char *tmp = NULL, *tok, *str;
+ bool res;
+
+ str = strdup(compat);
+ if (!str)
+ return false;
+
+ tok = strtok_r(str, ";", &tmp);
+ for (; tok; tok = strtok_r(NULL, ";", &tmp)) {
+ if (!strncmp(id, tok, strlen(tok))) {
+ res = true;
+ goto out;
+ }
+ }
+ res = false;
+out:
+ free(str);
+ return res;
+}
+
static int metricgroup__sys_event_iter(const struct pmu_metric *pm,
const struct pmu_metrics_table *table,
void *data)
@@ -468,7 +490,7 @@ static int metricgroup__sys_event_iter(const struct pmu_metric *pm,

while ((pmu = perf_pmu__scan(pmu))) {

- if (!pmu->id || strcmp(pmu->id, pm->compat))
+ if (!pmu->id || !match_pmu_identifier(pmu->id, pm->compat))
continue;

return d->fn(pm, table, d->data);
--
1.8.3.1