[PATCH v1 18/41] perf pmu: Add perf_pmu__for_each_name_from_config()

From: Ian Rogers

Date: Tue Nov 03 2026 - 18:51:07 EST


perf_pmu__name_from_config() returns the first event alias of a PMU
whose parsed config matches, ignoring the perf_event_attr type. So a
legacy hardware event alias can be returned for a raw config of the
same value, only one of possibly several names is found, and the names
of legacy events whose aliases were replaced by sysfs or json events of
the same name can't be found.

Add perf_pmu__for_each_name_from_config() that calls a callback for
each event name of a PMU whose encoding has a given type and config,
ignoring the extended PMU type of legacy encodings and, for core PMUs,
also searching the legacy hardware and cache event tables. Make
perf_pmu__name_from_config() return the first such name for the PMU's
type.

Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/util/pmu.c | 105 +++++++++++++++++++++++++++++++++++++++---
tools/perf/util/pmu.h | 3 ++
2 files changed, 101 insertions(+), 7 deletions(-)

diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c
index ee52fd18c927..fecfe791bf8a 100644
--- a/tools/perf/util/pmu.c
+++ b/tools/perf/util/pmu.c
@@ -2840,25 +2840,116 @@ void perf_pmu__delete(struct perf_pmu *pmu)
free(pmu);
}

-const char *perf_pmu__name_from_config(struct perf_pmu *pmu, u64 config)
+static bool pmu_config_equal(__u32 type, u64 a, u64 b)
+{
+ /* Ignore the extended PMU type in the upper bits of legacy encodings. */
+ if (type == PERF_TYPE_HARDWARE || type == PERF_TYPE_HW_CACHE)
+ return (a & PERF_HW_EVENT_MASK) == (b & PERF_HW_EVENT_MASK);
+ return a == b;
+}
+
+struct legacy_name_args {
+ __u32 type;
+ u64 config;
+ int (*cb)(const char *name, void *data);
+ void *data;
+};
+
+static int pmu_legacy_name_cb(const struct pmu_event *pe,
+ const struct pmu_events_table *table __maybe_unused,
+ void *data)
+{
+ static const char hw_prefix[] = "legacy-hardware-config=";
+ static const char cache_prefix[] = "legacy-cache-config=";
+ struct legacy_name_args *args = data;
+ const char *prefix = args->type == PERF_TYPE_HARDWARE ? hw_prefix : cache_prefix;
+
+ if (!pe->name || !pe->event || !strstarts(pe->event, prefix))
+ return 0;
+ if (!pmu_config_equal(args->type, strtoull(pe->event + strlen(prefix), NULL, 0),
+ args->config))
+ return 0;
+ return args->cb(pe->name, args->data);
+}
+
+/**
+ * perf_pmu__for_each_name_from_config - Call @cb for event names of an encoding.
+ * @pmu: The PMU whose event names are searched.
+ * @type: The perf_event_attr type of the encoding, such as the PMU's type or
+ * PERF_TYPE_HARDWARE for a legacy hardware event.
+ * @config: The perf_event_attr config of the encoding.
+ * @cb: Called with each event name of @pmu with the encoding, a non-zero return
+ * stops the iteration.
+ * @data: Passed to @cb.
+ *
+ * As event aliases from sysfs or json replace legacy event aliases of the same
+ * name, the names of legacy hardware and cache events of core PMUs are found
+ * from the legacy event tables. @cb may be called more than once for a name.
+ *
+ * Return: 0 or the first non-zero value returned by @cb.
+ */
+int perf_pmu__for_each_name_from_config(struct perf_pmu *pmu, __u32 type, u64 config,
+ int (*cb)(const char *name, void *data),
+ void *data)
{
struct hashmap_entry *entry;
size_t bkt;
+ int ret;

if (!pmu)
- return NULL;
+ return 0;

pmu_aliases_parse(pmu);
pmu_add_cpu_aliases(pmu);
hashmap__for_each_entry(pmu->aliases, entry, bkt) {
struct perf_pmu_alias *event = entry->pvalue;
- struct perf_event_attr attr = {.config = 0,};
- int ret = perf_pmu__parse_terms_to_attr(pmu, event->terms, &attr);
+ struct perf_event_attr attr = { .type = pmu->type, };

- if (ret == 0 && config == attr.config)
- return event->name;
+ if (perf_pmu__parse_terms_to_attr(pmu, event->terms, &attr) ||
+ attr.type != type || !pmu_config_equal(type, attr.config, config))
+ continue;
+ ret = cb(event->name, data);
+ if (ret)
+ return ret;
}
- return NULL;
+ if (pmu->is_core && (type == PERF_TYPE_HARDWARE || type == PERF_TYPE_HW_CACHE)) {
+ struct legacy_name_args args = {
+ .type = type,
+ .config = config,
+ .cb = cb,
+ .data = data,
+ };
+
+ return pmu_events_table__for_each_event(perf_pmu__default_core_events_table(),
+ /*pmu=*/NULL, pmu_legacy_name_cb, &args);
+ }
+ return 0;
+}
+
+static int first_name_cb(const char *name, void *data)
+{
+ const char **first = data;
+
+ *first = name;
+ return 1;
+}
+
+/**
+ * perf_pmu__name_from_config - The first event name of @pmu with @config.
+ * @pmu: The PMU whose event names are searched.
+ * @config: The perf_event_attr config of an event of @pmu's type.
+ *
+ * Return: An event name or NULL if none was found.
+ */
+const char *perf_pmu__name_from_config(struct perf_pmu *pmu, u64 config)
+{
+ const char *name = NULL;
+
+ if (!pmu)
+ return NULL;
+
+ perf_pmu__for_each_name_from_config(pmu, pmu->type, config, first_name_cb, &name);
+ return name;
}

bool perf_pmu__reads_only_on_cpu_idx0(const struct perf_event_attr *attr)
diff --git a/tools/perf/util/pmu.h b/tools/perf/util/pmu.h
index 06f922ce1f68..b556ec51fdeb 100644
--- a/tools/perf/util/pmu.h
+++ b/tools/perf/util/pmu.h
@@ -374,6 +374,9 @@ struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char
struct perf_pmu *perf_pmu__create_placeholder_core_pmu(struct list_head *core_pmus);
void perf_pmu__delete(struct perf_pmu *pmu);

+int perf_pmu__for_each_name_from_config(struct perf_pmu *pmu, __u32 type, u64 config,
+ int (*cb)(const char *name, void *data),
+ void *data);
const char *perf_pmu__name_from_config(struct perf_pmu *pmu, u64 config);
bool perf_pmu__is_fake(const struct perf_pmu *pmu);

--
2.56.0.385.gac671c819a-goog