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

perf pmus: Add notion of default PMU for JSON events

JSON events created in pmu-events.c by jevents.py may not specify a
PMU they are associated with, in which case it is implied that it is
the first core PMU. Care is needed to select this for regular 'cpu',
s390 'cpum_cf' and ARMs many names as at the point the name is first
needed the core PMUs list hasn't been initialized. Add a helper in
perf_pmus to create this value, in the worst case by scanning sysfs.

v2. Add missing close if fdopendir fails.

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

authored by

Ian Rogers and committed by
Namhyung Kim
d685819b 33941dbd

+53 -20
+16 -19
tools/perf/util/pmu.c
··· 741 741 } 742 742 743 743 struct pmu_add_cpu_aliases_map_data { 744 + /* List being added to. */ 744 745 struct list_head *head; 745 - const char *name; 746 - const char *cpu_name; 746 + /* If a pmu_event lacks a given PMU the default used. */ 747 + char *default_pmu_name; 748 + /* The PMU that we're searching for events for. */ 747 749 struct perf_pmu *pmu; 748 750 }; 749 751 ··· 754 752 void *vdata) 755 753 { 756 754 struct pmu_add_cpu_aliases_map_data *data = vdata; 757 - const char *pname = pe->pmu ? pe->pmu : data->cpu_name; 755 + const char *pname = pe->pmu ?: data->default_pmu_name; 758 756 759 - if (data->pmu->is_uncore && pmu_uncore_alias_match(pname, data->name)) 760 - goto new_alias; 761 - 762 - if (strcmp(pname, data->name)) 763 - return 0; 764 - 765 - new_alias: 766 - /* need type casts to override 'const' */ 767 - __perf_pmu__new_alias(data->head, -1, (char *)pe->name, (char *)pe->desc, 768 - (char *)pe->event, pe); 757 + if (!strcmp(pname, data->pmu->name) || 758 + (data->pmu->is_uncore && pmu_uncore_alias_match(pname, data->pmu->name))) { 759 + /* need type casts to override 'const' */ 760 + __perf_pmu__new_alias(data->head, -1, (char *)pe->name, (char *)pe->desc, 761 + (char *)pe->event, pe); 762 + } 769 763 return 0; 770 764 } 771 765 772 766 /* 773 - * From the pmu_events_map, find the table of PMU events that corresponds 774 - * to the current running CPU. Then, add all PMU events from that table 775 - * as aliases. 767 + * From the pmu_events_table, find the events that correspond to the given 768 + * PMU and add them to the list 'head'. 776 769 */ 777 770 void pmu_add_cpu_aliases_table(struct list_head *head, struct perf_pmu *pmu, 778 - const struct pmu_events_table *table) 771 + const struct pmu_events_table *table) 779 772 { 780 773 struct pmu_add_cpu_aliases_map_data data = { 781 774 .head = head, 782 - .name = pmu->name, 783 - .cpu_name = is_sysfs_pmu_core(pmu->name) ? pmu->name : "cpu", 775 + .default_pmu_name = perf_pmus__default_pmu_name(), 784 776 .pmu = pmu, 785 777 }; 786 778 787 779 pmu_events_table_for_each_event(table, pmu_add_cpu_aliases_map_callback, &data); 780 + free(data.default_pmu_name); 788 781 } 789 782 790 783 static void pmu_add_cpu_aliases(struct list_head *head, struct perf_pmu *pmu)
+36 -1
tools/perf/util/pmus.c
··· 137 137 return; 138 138 139 139 dir = fdopendir(fd); 140 - if (!dir) 140 + if (!dir) { 141 + close(fd); 141 142 return; 143 + } 142 144 143 145 while ((dent = readdir(dir))) { 144 146 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) ··· 524 522 pthread_once(&extended_type_once, perf_pmus__init_supports_extended_type); 525 523 526 524 return perf_pmus__do_support_extended_type; 525 + } 526 + 527 + char *perf_pmus__default_pmu_name(void) 528 + { 529 + int fd; 530 + DIR *dir; 531 + struct dirent *dent; 532 + char *result = NULL; 533 + 534 + if (!list_empty(&core_pmus)) 535 + return strdup(list_first_entry(&core_pmus, struct perf_pmu, list)->name); 536 + 537 + fd = perf_pmu__event_source_devices_fd(); 538 + if (fd < 0) 539 + return strdup("cpu"); 540 + 541 + dir = fdopendir(fd); 542 + if (!dir) { 543 + close(fd); 544 + return strdup("cpu"); 545 + } 546 + 547 + while ((dent = readdir(dir))) { 548 + if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) 549 + continue; 550 + if (is_pmu_core(dent->d_name)) { 551 + result = strdup(dent->d_name); 552 + break; 553 + } 554 + } 555 + 556 + closedir(dir); 557 + return result ?: strdup("cpu"); 527 558 } 528 559 529 560 struct perf_pmu *evsel__find_pmu(const struct evsel *evsel)
+1
tools/perf/util/pmus.h
··· 20 20 bool perf_pmus__have_event(const char *pname, const char *name); 21 21 int perf_pmus__num_core_pmus(void); 22 22 bool perf_pmus__supports_extended_type(void); 23 + char *perf_pmus__default_pmu_name(void); 23 24 24 25 #endif /* __PMUS_H */