perf tools: Add/use PMU reverse lookup from config to name
authorIan Rogers <irogers@google.com>
Fri, 8 Mar 2024 00:19:15 +0000 (16:19 -0800)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Thu, 21 Mar 2024 16:53:45 +0000 (13:53 -0300)
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>
tools/perf/util/perf_event_attr_fprintf.c
tools/perf/util/pmu.c
tools/perf/util/pmu.h
tools/perf/util/python.c

index 29e66835da3a76a130f0650b378f8a3bd3bba1a4..59fbbba7969740586a40c1d5462b890c53e3b627 100644 (file)
@@ -222,8 +222,14 @@ static void __p_config_tracepoint_id(char *buf, size_t size, u64 value)
 }
 #endif
 
-static void __p_config_id(char *buf, size_t size, u32 type, u64 value)
+static void __p_config_id(struct perf_pmu *pmu, char *buf, size_t size, u32 type, u64 value)
 {
+       const char *name = perf_pmu__name_from_config(pmu, value);
+
+       if (name) {
+               print_id_hex(name);
+               return;
+       }
        switch (type) {
        case PERF_TYPE_HARDWARE:
                return __p_config_hw_id(buf, size, value);
@@ -252,7 +258,7 @@ static void __p_config_id(char *buf, size_t size, u32 type, u64 value)
 #define p_branch_sample_type(val) __p_branch_sample_type(buf, BUF_SIZE, val)
 #define p_read_format(val)     __p_read_format(buf, BUF_SIZE, val)
 #define p_type_id(val)         __p_type_id(pmu, buf, BUF_SIZE, val)
-#define p_config_id(val)       __p_config_id(buf, BUF_SIZE, attr->type, val)
+#define p_config_id(val)       __p_config_id(pmu, buf, BUF_SIZE, attr->type, val)
 
 #define PRINT_ATTRn(_n, _f, _p, _a)                    \
 do {                                                   \
index 81952c6cd3c6f5e8d33e20eb3a2674e9396755f8..ab30f22eaf10a19af442cccb1a052c06e419e2f9 100644 (file)
@@ -2146,3 +2146,21 @@ void perf_pmu__delete(struct perf_pmu *pmu)
        zfree(&pmu->id);
        free(pmu);
 }
+
+const char *perf_pmu__name_from_config(struct perf_pmu *pmu, u64 config)
+{
+       struct perf_pmu_alias *event;
+
+       if (!pmu)
+               return NULL;
+
+       pmu_add_cpu_aliases(pmu);
+       list_for_each_entry(event, &pmu->aliases, list) {
+               struct perf_event_attr attr = {.config = 0,};
+               int ret = perf_pmu__config(pmu, &attr, &event->terms, NULL);
+
+               if (ret == 0 && config == attr.config)
+                       return event->name;
+       }
+       return NULL;
+}
index 9f5284b29ecf46e731cfc14054d43366803540fb..152700f784556207807906e2f9dfcb1cce4db0e0 100644 (file)
@@ -276,5 +276,6 @@ 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);
 struct perf_pmu *perf_pmus__find_core_pmu(void);
+const char *perf_pmu__name_from_config(struct perf_pmu *pmu, u64 config);
 
 #endif /* __PMU_H */
index 075c0f79b1b92d4c41f218adccd0c5c602556028..0aeb97c11c03067fd9f4149c949f83fd69a48f48 100644 (file)
@@ -103,6 +103,16 @@ int perf_pmu__scan_file(const struct perf_pmu *pmu, const char *name, const char
        return EOF;
 }
 
+const char *perf_pmu__name_from_config(struct perf_pmu *pmu __maybe_unused, u64 config __maybe_unused)
+{
+       return NULL;
+}
+
+struct perf_pmu *perf_pmus__find_by_type(unsigned int type __maybe_unused)
+{
+       return NULL;
+}
+
 int perf_pmus__num_core_pmus(void)
 {
        return 1;