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

perf x86/topdown: Refine helper arch_is_topdown_metrics()

Leverage the existed function perf_pmu__name_from_config() to check if
an event is topdown metrics event. perf_pmu__name_from_config() goes
through the defined formats and figures out the config of pre-defined
topdown events.

This avoids to figure out the config of topdown pre-defined events with
hard-coded format strings "event=" and "umask=" and provides more
flexibility.

Suggested-by: Ian Rogers <irogers@google.com>
Signed-off-by: Dapeng Mi <dapeng1.mi@linux.intel.com>
Reviewed-by: Ian Rogers <irogers@google.com>
Link: https://lore.kernel.org/r/20241011110207.1032235-2-dapeng1.mi@linux.intel.com
Signed-off-by: Namhyung Kim <namhyung@kernel.org>

authored by

Dapeng Mi and committed by
Namhyung Kim
fbc79831 b68b5b36

+9 -30
+9 -30
tools/perf/arch/x86/util/topdown.c
··· 41 41 return false; 42 42 } 43 43 44 - static int compare_topdown_event(void *vstate, struct pmu_event_info *info) 45 - { 46 - int *config = vstate; 47 - int event = 0; 48 - int umask = 0; 49 - char *str; 50 - 51 - if (!strcasestr(info->name, "topdown")) 52 - return 0; 53 - 54 - str = strcasestr(info->str, "event="); 55 - if (str) 56 - sscanf(str, "event=%x", &event); 57 - 58 - str = strcasestr(info->str, "umask="); 59 - if (str) 60 - sscanf(str, "umask=%x", &umask); 61 - 62 - if (event == 0 && *config == (event | umask << 8)) 63 - return 1; 64 - 65 - return 0; 66 - } 67 - 68 44 bool arch_is_topdown_metrics(const struct evsel *evsel) 69 45 { 70 - struct perf_pmu *pmu = evsel__find_pmu(evsel); 71 46 int config = evsel->core.attr.config; 47 + const char *name_from_config; 48 + struct perf_pmu *pmu; 72 49 50 + /* All topdown events have an event code of 0. */ 51 + if ((config & 0xFF) != 0) 52 + return false; 53 + 54 + pmu = evsel__find_pmu(evsel); 73 55 if (!pmu || !pmu->is_core) 74 56 return false; 75 57 76 - if (perf_pmu__for_each_event(pmu, false, &config, 77 - compare_topdown_event)) 78 - return true; 79 - 80 - return false; 58 + name_from_config = perf_pmu__name_from_config(pmu, config); 59 + return name_from_config && strcasestr(name_from_config, "topdown"); 81 60 } 82 61 83 62 /*