Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf tools: Add/use PMU reverse lookup from config to name

Add perf_pmu__name_from_config that does a reverse lookup from a
config number to an alias name. The lookup is expensive as the config
is computed for every alias by filling in a perf_event_attr, but this
is only done when verbose output is enabled. The lookup also only
considers config, and not config1, config2 or config3.

An example of the output:

$ perf stat -vv -e data_read true
...
perf_event_attr:
type 24 (uncore_imc_free_running_0)
size 136
config 0x20ff (data_read)
sample_type IDENTIFIER
read_format TOTAL_TIME_ENABLED|TOTAL_TIME_RUNNING
disabled 1
inherit 1
exclude_guest 1
...

Committer notes:

Fix the python binding build by adding dummies for not strictly
needed perf_pmu__name_from_config() and perf_pmus__find_by_type().

Signed-off-by: Ian Rogers <irogers@google.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Tested-by: Kan Liang <kan.liang@linux.intel.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@arm.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ravi Bangoria <ravi.bangoria@amd.com>
Cc: Yang Jihong <yangjihong1@huawei.com>
Link: https://lore.kernel.org/r/20240308001915.4060155-7-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Ian Rogers and committed by
Arnaldo Carvalho de Melo
67ee8e71 70938820

+37 -2
+8 -2
tools/perf/util/perf_event_attr_fprintf.c
··· 222 222 } 223 223 #endif 224 224 225 - static void __p_config_id(char *buf, size_t size, u32 type, u64 value) 225 + static void __p_config_id(struct perf_pmu *pmu, char *buf, size_t size, u32 type, u64 value) 226 226 { 227 + const char *name = perf_pmu__name_from_config(pmu, value); 228 + 229 + if (name) { 230 + print_id_hex(name); 231 + return; 232 + } 227 233 switch (type) { 228 234 case PERF_TYPE_HARDWARE: 229 235 return __p_config_hw_id(buf, size, value); ··· 258 252 #define p_branch_sample_type(val) __p_branch_sample_type(buf, BUF_SIZE, val) 259 253 #define p_read_format(val) __p_read_format(buf, BUF_SIZE, val) 260 254 #define p_type_id(val) __p_type_id(pmu, buf, BUF_SIZE, val) 261 - #define p_config_id(val) __p_config_id(buf, BUF_SIZE, attr->type, val) 255 + #define p_config_id(val) __p_config_id(pmu, buf, BUF_SIZE, attr->type, val) 262 256 263 257 #define PRINT_ATTRn(_n, _f, _p, _a) \ 264 258 do { \
+18
tools/perf/util/pmu.c
··· 2146 2146 zfree(&pmu->id); 2147 2147 free(pmu); 2148 2148 } 2149 + 2150 + const char *perf_pmu__name_from_config(struct perf_pmu *pmu, u64 config) 2151 + { 2152 + struct perf_pmu_alias *event; 2153 + 2154 + if (!pmu) 2155 + return NULL; 2156 + 2157 + pmu_add_cpu_aliases(pmu); 2158 + list_for_each_entry(event, &pmu->aliases, list) { 2159 + struct perf_event_attr attr = {.config = 0,}; 2160 + int ret = perf_pmu__config(pmu, &attr, &event->terms, NULL); 2161 + 2162 + if (ret == 0 && config == attr.config) 2163 + return event->name; 2164 + } 2165 + return NULL; 2166 + }
+1
tools/perf/util/pmu.h
··· 276 276 struct perf_pmu *perf_pmu__create_placeholder_core_pmu(struct list_head *core_pmus); 277 277 void perf_pmu__delete(struct perf_pmu *pmu); 278 278 struct perf_pmu *perf_pmus__find_core_pmu(void); 279 + const char *perf_pmu__name_from_config(struct perf_pmu *pmu, u64 config); 279 280 280 281 #endif /* __PMU_H */
+10
tools/perf/util/python.c
··· 103 103 return EOF; 104 104 } 105 105 106 + const char *perf_pmu__name_from_config(struct perf_pmu *pmu __maybe_unused, u64 config __maybe_unused) 107 + { 108 + return NULL; 109 + } 110 + 111 + struct perf_pmu *perf_pmus__find_by_type(unsigned int type __maybe_unused) 112 + { 113 + return NULL; 114 + } 115 + 106 116 int perf_pmus__num_core_pmus(void) 107 117 { 108 118 return 1;