1336b92daSRavi Bangoria // SPDX-License-Identifier: GPL-2.0
2336b92daSRavi Bangoria #include <linux/list.h>
38d9f5146SIan Rogers #include <linux/list_sort.h>
48d9f5146SIan Rogers #include <linux/string.h>
51eaf496eSIan Rogers #include <linux/zalloc.h>
61eaf496eSIan Rogers #include <subcmd/pager.h>
71eaf496eSIan Rogers #include <sys/types.h>
88d9f5146SIan Rogers #include <ctype.h>
91eaf496eSIan Rogers #include <dirent.h>
1082fe2e45SArnaldo Carvalho de Melo #include <pthread.h>
11003be8c4SIan Rogers #include <string.h>
121eaf496eSIan Rogers #include <unistd.h>
13f989dc00SJames Clark #include "cpumap.h"
141eaf496eSIan Rogers #include "debug.h"
151eaf496eSIan Rogers #include "evsel.h"
16003be8c4SIan Rogers #include "pmus.h"
17003be8c4SIan Rogers #include "pmu.h"
181eaf496eSIan Rogers #include "print-events.h"
19336b92daSRavi Bangoria
20cddfc5fbSRavi Bangoria /*
21cddfc5fbSRavi Bangoria * core_pmus: A PMU belongs to core_pmus if it's name is "cpu" or it's sysfs
22cddfc5fbSRavi Bangoria * directory contains "cpus" file. All PMUs belonging to core_pmus
23cddfc5fbSRavi Bangoria * must have pmu->is_core=1. If there are more than one PMU in
24cddfc5fbSRavi Bangoria * this list, perf interprets it as a heterogeneous platform.
25cddfc5fbSRavi Bangoria * (FWIW, certain ARM platforms having heterogeneous cores uses
26cddfc5fbSRavi Bangoria * homogeneous PMU, and thus they are treated as homogeneous
27cddfc5fbSRavi Bangoria * platform by perf because core_pmus will have only one entry)
28cddfc5fbSRavi Bangoria * other_pmus: All other PMUs which are not part of core_pmus list. It doesn't
29cddfc5fbSRavi Bangoria * matter whether PMU is present per SMT-thread or outside of the
30cddfc5fbSRavi Bangoria * core in the hw. For e.g., an instance of AMD ibs_fetch// and
31cddfc5fbSRavi Bangoria * ibs_op// PMUs is present in each hw SMT thread, however they
32cddfc5fbSRavi Bangoria * are captured under other_pmus. PMUs belonging to other_pmus
33cddfc5fbSRavi Bangoria * must have pmu->is_core=0 but pmu->is_uncore could be 0 or 1.
34cddfc5fbSRavi Bangoria */
3515c57a80SIan Rogers static LIST_HEAD(core_pmus);
3615c57a80SIan Rogers static LIST_HEAD(other_pmus);
378e7d8a2eSIan Rogers static bool read_sysfs_core_pmus;
388e7d8a2eSIan Rogers static bool read_sysfs_all_pmus;
391eaf496eSIan Rogers
pmu_name_len_no_suffix(const char * str,unsigned long * num)40cd4e1efbSIan Rogers int pmu_name_len_no_suffix(const char *str, unsigned long *num)
418d9f5146SIan Rogers {
428d9f5146SIan Rogers int orig_len, len;
438d9f5146SIan Rogers
448d9f5146SIan Rogers orig_len = len = strlen(str);
458d9f5146SIan Rogers
468d9f5146SIan Rogers /* Non-uncore PMUs have their full length, for example, i915. */
478d9f5146SIan Rogers if (!strstarts(str, "uncore_"))
488d9f5146SIan Rogers return len;
498d9f5146SIan Rogers
508d9f5146SIan Rogers /*
518d9f5146SIan Rogers * Count trailing digits and '_', if '_{num}' suffix isn't present use
528d9f5146SIan Rogers * the full length.
538d9f5146SIan Rogers */
548d9f5146SIan Rogers while (len > 0 && isdigit(str[len - 1]))
558d9f5146SIan Rogers len--;
568d9f5146SIan Rogers
578d9f5146SIan Rogers if (len > 0 && len != orig_len && str[len - 1] == '_') {
588d9f5146SIan Rogers if (num)
598d9f5146SIan Rogers *num = strtoul(&str[len], NULL, 10);
608d9f5146SIan Rogers return len - 1;
618d9f5146SIan Rogers }
628d9f5146SIan Rogers return orig_len;
638d9f5146SIan Rogers }
648d9f5146SIan Rogers
perf_pmus__destroy(void)651eaf496eSIan Rogers void perf_pmus__destroy(void)
661eaf496eSIan Rogers {
671eaf496eSIan Rogers struct perf_pmu *pmu, *tmp;
681eaf496eSIan Rogers
6915c57a80SIan Rogers list_for_each_entry_safe(pmu, tmp, &core_pmus, list) {
7015c57a80SIan Rogers list_del(&pmu->list);
7115c57a80SIan Rogers
7215c57a80SIan Rogers perf_pmu__delete(pmu);
7315c57a80SIan Rogers }
7415c57a80SIan Rogers list_for_each_entry_safe(pmu, tmp, &other_pmus, list) {
751eaf496eSIan Rogers list_del(&pmu->list);
761eaf496eSIan Rogers
771eaf496eSIan Rogers perf_pmu__delete(pmu);
781eaf496eSIan Rogers }
798e7d8a2eSIan Rogers read_sysfs_core_pmus = false;
808e7d8a2eSIan Rogers read_sysfs_all_pmus = false;
811eaf496eSIan Rogers }
821eaf496eSIan Rogers
pmu_find(const char * name)831eaf496eSIan Rogers static struct perf_pmu *pmu_find(const char *name)
841eaf496eSIan Rogers {
851eaf496eSIan Rogers struct perf_pmu *pmu;
861eaf496eSIan Rogers
8715c57a80SIan Rogers list_for_each_entry(pmu, &core_pmus, list) {
8815c57a80SIan Rogers if (!strcmp(pmu->name, name) ||
8915c57a80SIan Rogers (pmu->alias_name && !strcmp(pmu->alias_name, name)))
9015c57a80SIan Rogers return pmu;
9115c57a80SIan Rogers }
9215c57a80SIan Rogers list_for_each_entry(pmu, &other_pmus, list) {
931eaf496eSIan Rogers if (!strcmp(pmu->name, name) ||
941eaf496eSIan Rogers (pmu->alias_name && !strcmp(pmu->alias_name, name)))
951eaf496eSIan Rogers return pmu;
961eaf496eSIan Rogers }
971eaf496eSIan Rogers
981eaf496eSIan Rogers return NULL;
991eaf496eSIan Rogers }
1001eaf496eSIan Rogers
perf_pmus__find(const char * name)1011eaf496eSIan Rogers struct perf_pmu *perf_pmus__find(const char *name)
1021eaf496eSIan Rogers {
1031eaf496eSIan Rogers struct perf_pmu *pmu;
1041eaf496eSIan Rogers int dirfd;
1058e7d8a2eSIan Rogers bool core_pmu;
1061eaf496eSIan Rogers
1071eaf496eSIan Rogers /*
1081eaf496eSIan Rogers * Once PMU is loaded it stays in the list,
1091eaf496eSIan Rogers * so we keep us from multiple reading/parsing
1101eaf496eSIan Rogers * the pmu format definitions.
1111eaf496eSIan Rogers */
1121eaf496eSIan Rogers pmu = pmu_find(name);
1131eaf496eSIan Rogers if (pmu)
1141eaf496eSIan Rogers return pmu;
1151eaf496eSIan Rogers
1168e7d8a2eSIan Rogers if (read_sysfs_all_pmus)
1178e7d8a2eSIan Rogers return NULL;
1188e7d8a2eSIan Rogers
1198e7d8a2eSIan Rogers core_pmu = is_pmu_core(name);
1208e7d8a2eSIan Rogers if (core_pmu && read_sysfs_core_pmus)
1218e7d8a2eSIan Rogers return NULL;
1228e7d8a2eSIan Rogers
1231eaf496eSIan Rogers dirfd = perf_pmu__event_source_devices_fd();
1248e7d8a2eSIan Rogers pmu = perf_pmu__lookup(core_pmu ? &core_pmus : &other_pmus, dirfd, name);
1251eaf496eSIan Rogers close(dirfd);
1261eaf496eSIan Rogers
1271eaf496eSIan Rogers return pmu;
1281eaf496eSIan Rogers }
1291eaf496eSIan Rogers
perf_pmu__find2(int dirfd,const char * name)1301eaf496eSIan Rogers static struct perf_pmu *perf_pmu__find2(int dirfd, const char *name)
1311eaf496eSIan Rogers {
1321eaf496eSIan Rogers struct perf_pmu *pmu;
1338e7d8a2eSIan Rogers bool core_pmu;
1341eaf496eSIan Rogers
1351eaf496eSIan Rogers /*
1361eaf496eSIan Rogers * Once PMU is loaded it stays in the list,
1371eaf496eSIan Rogers * so we keep us from multiple reading/parsing
1381eaf496eSIan Rogers * the pmu format definitions.
1391eaf496eSIan Rogers */
1401eaf496eSIan Rogers pmu = pmu_find(name);
1411eaf496eSIan Rogers if (pmu)
1421eaf496eSIan Rogers return pmu;
1431eaf496eSIan Rogers
1448e7d8a2eSIan Rogers if (read_sysfs_all_pmus)
1458e7d8a2eSIan Rogers return NULL;
1468e7d8a2eSIan Rogers
1478e7d8a2eSIan Rogers core_pmu = is_pmu_core(name);
1488e7d8a2eSIan Rogers if (core_pmu && read_sysfs_core_pmus)
1498e7d8a2eSIan Rogers return NULL;
1508e7d8a2eSIan Rogers
1518e7d8a2eSIan Rogers return perf_pmu__lookup(core_pmu ? &core_pmus : &other_pmus, dirfd, name);
1521eaf496eSIan Rogers }
1531eaf496eSIan Rogers
pmus_cmp(void * priv __maybe_unused,const struct list_head * lhs,const struct list_head * rhs)1548d9f5146SIan Rogers static int pmus_cmp(void *priv __maybe_unused,
1558d9f5146SIan Rogers const struct list_head *lhs, const struct list_head *rhs)
1568d9f5146SIan Rogers {
1578d9f5146SIan Rogers unsigned long lhs_num = 0, rhs_num = 0;
1588d9f5146SIan Rogers struct perf_pmu *lhs_pmu = container_of(lhs, struct perf_pmu, list);
1598d9f5146SIan Rogers struct perf_pmu *rhs_pmu = container_of(rhs, struct perf_pmu, list);
1608d9f5146SIan Rogers const char *lhs_pmu_name = lhs_pmu->name ?: "";
1618d9f5146SIan Rogers const char *rhs_pmu_name = rhs_pmu->name ?: "";
1628d9f5146SIan Rogers int lhs_pmu_name_len = pmu_name_len_no_suffix(lhs_pmu_name, &lhs_num);
1638d9f5146SIan Rogers int rhs_pmu_name_len = pmu_name_len_no_suffix(rhs_pmu_name, &rhs_num);
1648d9f5146SIan Rogers int ret = strncmp(lhs_pmu_name, rhs_pmu_name,
1658d9f5146SIan Rogers lhs_pmu_name_len < rhs_pmu_name_len ? lhs_pmu_name_len : rhs_pmu_name_len);
1668d9f5146SIan Rogers
1678d9f5146SIan Rogers if (lhs_pmu_name_len != rhs_pmu_name_len || ret != 0 || lhs_pmu_name_len == 0)
1688d9f5146SIan Rogers return ret;
1698d9f5146SIan Rogers
1708d9f5146SIan Rogers return lhs_num < rhs_num ? -1 : (lhs_num > rhs_num ? 1 : 0);
1718d9f5146SIan Rogers }
1728d9f5146SIan Rogers
1731eaf496eSIan Rogers /* Add all pmus in sysfs to pmu list: */
pmu_read_sysfs(bool core_only)1749d6a1df9SIan Rogers static void pmu_read_sysfs(bool core_only)
1751eaf496eSIan Rogers {
1761eaf496eSIan Rogers int fd;
1771eaf496eSIan Rogers DIR *dir;
1781eaf496eSIan Rogers struct dirent *dent;
1791eaf496eSIan Rogers
1808e7d8a2eSIan Rogers if (read_sysfs_all_pmus || (core_only && read_sysfs_core_pmus))
1818e7d8a2eSIan Rogers return;
1828e7d8a2eSIan Rogers
1831eaf496eSIan Rogers fd = perf_pmu__event_source_devices_fd();
1841eaf496eSIan Rogers if (fd < 0)
1851eaf496eSIan Rogers return;
1861eaf496eSIan Rogers
1871eaf496eSIan Rogers dir = fdopendir(fd);
188d685819bSIan Rogers if (!dir) {
189d685819bSIan Rogers close(fd);
1901eaf496eSIan Rogers return;
191d685819bSIan Rogers }
1921eaf496eSIan Rogers
1931eaf496eSIan Rogers while ((dent = readdir(dir))) {
1941eaf496eSIan Rogers if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
1951eaf496eSIan Rogers continue;
1969d6a1df9SIan Rogers if (core_only && !is_pmu_core(dent->d_name))
1979d6a1df9SIan Rogers continue;
19815c57a80SIan Rogers /* add to static LIST_HEAD(core_pmus) or LIST_HEAD(other_pmus): */
1991eaf496eSIan Rogers perf_pmu__find2(fd, dent->d_name);
2001eaf496eSIan Rogers }
2011eaf496eSIan Rogers
2021eaf496eSIan Rogers closedir(dir);
20334bc65d6SIan Rogers if (list_empty(&core_pmus)) {
20434bc65d6SIan Rogers if (!perf_pmu__create_placeholder_core_pmu(&core_pmus))
20534bc65d6SIan Rogers pr_err("Failure to set up any core PMUs\n");
206628eaa4eSIan Rogers }
2078d9f5146SIan Rogers list_sort(NULL, &core_pmus, pmus_cmp);
2088d9f5146SIan Rogers list_sort(NULL, &other_pmus, pmus_cmp);
20934bc65d6SIan Rogers if (!list_empty(&core_pmus)) {
2108e7d8a2eSIan Rogers read_sysfs_core_pmus = true;
21134bc65d6SIan Rogers if (!core_only)
2128e7d8a2eSIan Rogers read_sysfs_all_pmus = true;
2138e7d8a2eSIan Rogers }
2141eaf496eSIan Rogers }
2151eaf496eSIan Rogers
__perf_pmus__find_by_type(unsigned int type)2161dd5f78dSIan Rogers static struct perf_pmu *__perf_pmus__find_by_type(unsigned int type)
2171eaf496eSIan Rogers {
2181eaf496eSIan Rogers struct perf_pmu *pmu;
2191eaf496eSIan Rogers
22015c57a80SIan Rogers list_for_each_entry(pmu, &core_pmus, list) {
2211eaf496eSIan Rogers if (pmu->type == type)
2221eaf496eSIan Rogers return pmu;
22315c57a80SIan Rogers }
2241dd5f78dSIan Rogers
22515c57a80SIan Rogers list_for_each_entry(pmu, &other_pmus, list) {
22615c57a80SIan Rogers if (pmu->type == type)
22715c57a80SIan Rogers return pmu;
22815c57a80SIan Rogers }
2291eaf496eSIan Rogers return NULL;
2301eaf496eSIan Rogers }
2311eaf496eSIan Rogers
perf_pmus__find_by_type(unsigned int type)2321dd5f78dSIan Rogers struct perf_pmu *perf_pmus__find_by_type(unsigned int type)
2331dd5f78dSIan Rogers {
2341dd5f78dSIan Rogers struct perf_pmu *pmu = __perf_pmus__find_by_type(type);
2351dd5f78dSIan Rogers
2361dd5f78dSIan Rogers if (pmu || read_sysfs_all_pmus)
2371dd5f78dSIan Rogers return pmu;
2381dd5f78dSIan Rogers
2391dd5f78dSIan Rogers pmu_read_sysfs(/*core_only=*/false);
2401dd5f78dSIan Rogers pmu = __perf_pmus__find_by_type(type);
2411dd5f78dSIan Rogers return pmu;
2421dd5f78dSIan Rogers }
2431dd5f78dSIan Rogers
24415c57a80SIan Rogers /*
24515c57a80SIan Rogers * pmu iterator: If pmu is NULL, we start at the begin, otherwise return the
24615c57a80SIan Rogers * next pmu. Returns NULL on end.
24715c57a80SIan Rogers */
perf_pmus__scan(struct perf_pmu * pmu)2481eaf496eSIan Rogers struct perf_pmu *perf_pmus__scan(struct perf_pmu *pmu)
2491eaf496eSIan Rogers {
25015c57a80SIan Rogers bool use_core_pmus = !pmu || pmu->is_core;
25115c57a80SIan Rogers
2521eaf496eSIan Rogers if (!pmu) {
2539d6a1df9SIan Rogers pmu_read_sysfs(/*core_only=*/false);
25415c57a80SIan Rogers pmu = list_prepare_entry(pmu, &core_pmus, list);
2551eaf496eSIan Rogers }
25615c57a80SIan Rogers if (use_core_pmus) {
25715c57a80SIan Rogers list_for_each_entry_continue(pmu, &core_pmus, list)
25815c57a80SIan Rogers return pmu;
25915c57a80SIan Rogers
26015c57a80SIan Rogers pmu = NULL;
26115c57a80SIan Rogers pmu = list_prepare_entry(pmu, &other_pmus, list);
26215c57a80SIan Rogers }
26315c57a80SIan Rogers list_for_each_entry_continue(pmu, &other_pmus, list)
2641eaf496eSIan Rogers return pmu;
2651eaf496eSIan Rogers return NULL;
2661eaf496eSIan Rogers }
267003be8c4SIan Rogers
perf_pmus__scan_core(struct perf_pmu * pmu)2689d6a1df9SIan Rogers struct perf_pmu *perf_pmus__scan_core(struct perf_pmu *pmu)
2699d6a1df9SIan Rogers {
2709d6a1df9SIan Rogers if (!pmu) {
2719d6a1df9SIan Rogers pmu_read_sysfs(/*core_only=*/true);
272f989dc00SJames Clark return list_first_entry_or_null(&core_pmus, typeof(*pmu), list);
2739d6a1df9SIan Rogers }
2749d6a1df9SIan Rogers list_for_each_entry_continue(pmu, &core_pmus, list)
2759d6a1df9SIan Rogers return pmu;
2769d6a1df9SIan Rogers
2779d6a1df9SIan Rogers return NULL;
2789d6a1df9SIan Rogers }
2799d6a1df9SIan Rogers
perf_pmus__scan_skip_duplicates(struct perf_pmu * pmu)280cd4e1efbSIan Rogers static struct perf_pmu *perf_pmus__scan_skip_duplicates(struct perf_pmu *pmu)
281cd4e1efbSIan Rogers {
282cd4e1efbSIan Rogers bool use_core_pmus = !pmu || pmu->is_core;
283cd4e1efbSIan Rogers int last_pmu_name_len = 0;
284cd4e1efbSIan Rogers const char *last_pmu_name = (pmu && pmu->name) ? pmu->name : "";
285cd4e1efbSIan Rogers
286cd4e1efbSIan Rogers if (!pmu) {
287cd4e1efbSIan Rogers pmu_read_sysfs(/*core_only=*/false);
288cd4e1efbSIan Rogers pmu = list_prepare_entry(pmu, &core_pmus, list);
289cd4e1efbSIan Rogers } else
290cd4e1efbSIan Rogers last_pmu_name_len = pmu_name_len_no_suffix(pmu->name ?: "", NULL);
291cd4e1efbSIan Rogers
292cd4e1efbSIan Rogers if (use_core_pmus) {
293cd4e1efbSIan Rogers list_for_each_entry_continue(pmu, &core_pmus, list) {
294cd4e1efbSIan Rogers int pmu_name_len = pmu_name_len_no_suffix(pmu->name ?: "", /*num=*/NULL);
295cd4e1efbSIan Rogers
296cd4e1efbSIan Rogers if (last_pmu_name_len == pmu_name_len &&
297cd4e1efbSIan Rogers !strncmp(last_pmu_name, pmu->name ?: "", pmu_name_len))
298cd4e1efbSIan Rogers continue;
299cd4e1efbSIan Rogers
300cd4e1efbSIan Rogers return pmu;
301cd4e1efbSIan Rogers }
302cd4e1efbSIan Rogers pmu = NULL;
303cd4e1efbSIan Rogers pmu = list_prepare_entry(pmu, &other_pmus, list);
304cd4e1efbSIan Rogers }
305cd4e1efbSIan Rogers list_for_each_entry_continue(pmu, &other_pmus, list) {
306cd4e1efbSIan Rogers int pmu_name_len = pmu_name_len_no_suffix(pmu->name ?: "", /*num=*/NULL);
307cd4e1efbSIan Rogers
308cd4e1efbSIan Rogers if (last_pmu_name_len == pmu_name_len &&
309cd4e1efbSIan Rogers !strncmp(last_pmu_name, pmu->name ?: "", pmu_name_len))
310cd4e1efbSIan Rogers continue;
311cd4e1efbSIan Rogers
312cd4e1efbSIan Rogers return pmu;
313cd4e1efbSIan Rogers }
314cd4e1efbSIan Rogers return NULL;
315cd4e1efbSIan Rogers }
316cd4e1efbSIan Rogers
perf_pmus__pmu_for_pmu_filter(const char * str)317003be8c4SIan Rogers const struct perf_pmu *perf_pmus__pmu_for_pmu_filter(const char *str)
318003be8c4SIan Rogers {
319003be8c4SIan Rogers struct perf_pmu *pmu = NULL;
320003be8c4SIan Rogers
3211eaf496eSIan Rogers while ((pmu = perf_pmus__scan(pmu)) != NULL) {
322003be8c4SIan Rogers if (!strcmp(pmu->name, str))
323003be8c4SIan Rogers return pmu;
324003be8c4SIan Rogers /* Ignore "uncore_" prefix. */
325003be8c4SIan Rogers if (!strncmp(pmu->name, "uncore_", 7)) {
326003be8c4SIan Rogers if (!strcmp(pmu->name + 7, str))
327003be8c4SIan Rogers return pmu;
328003be8c4SIan Rogers }
329003be8c4SIan Rogers /* Ignore "cpu_" prefix on Intel hybrid PMUs. */
330003be8c4SIan Rogers if (!strncmp(pmu->name, "cpu_", 4)) {
331003be8c4SIan Rogers if (!strcmp(pmu->name + 4, str))
332003be8c4SIan Rogers return pmu;
333003be8c4SIan Rogers }
334003be8c4SIan Rogers }
335003be8c4SIan Rogers return NULL;
336003be8c4SIan Rogers }
3371eaf496eSIan Rogers
perf_pmus__num_mem_pmus(void)338f0dc2082SRavi Bangoria int __weak perf_pmus__num_mem_pmus(void)
3391eaf496eSIan Rogers {
3409d6a1df9SIan Rogers /* All core PMUs are for mem events. */
341002c4845SIan Rogers return perf_pmus__num_core_pmus();
3421eaf496eSIan Rogers }
3431eaf496eSIan Rogers
3441eaf496eSIan Rogers /** Struct for ordering events as output in perf list. */
3451eaf496eSIan Rogers struct sevent {
3461eaf496eSIan Rogers /** PMU for event. */
3471eaf496eSIan Rogers const struct perf_pmu *pmu;
348c3245d20SIan Rogers const char *name;
349c3245d20SIan Rogers const char* alias;
350c3245d20SIan Rogers const char *scale_unit;
351c3245d20SIan Rogers const char *desc;
352c3245d20SIan Rogers const char *long_desc;
353c3245d20SIan Rogers const char *encoding_desc;
354c3245d20SIan Rogers const char *topic;
355c3245d20SIan Rogers const char *pmu_name;
356c3245d20SIan Rogers bool deprecated;
3571eaf496eSIan Rogers };
3581eaf496eSIan Rogers
cmp_sevent(const void * a,const void * b)3591eaf496eSIan Rogers static int cmp_sevent(const void *a, const void *b)
3601eaf496eSIan Rogers {
3611eaf496eSIan Rogers const struct sevent *as = a;
3621eaf496eSIan Rogers const struct sevent *bs = b;
363c3245d20SIan Rogers bool a_iscpu, b_iscpu;
3641eaf496eSIan Rogers int ret;
3651eaf496eSIan Rogers
3661eaf496eSIan Rogers /* Put extra events last. */
367c3245d20SIan Rogers if (!!as->desc != !!bs->desc)
368c3245d20SIan Rogers return !!as->desc - !!bs->desc;
3691eaf496eSIan Rogers
3701eaf496eSIan Rogers /* Order by topics. */
371c3245d20SIan Rogers ret = strcmp(as->topic ?: "", bs->topic ?: "");
3721eaf496eSIan Rogers if (ret)
3731eaf496eSIan Rogers return ret;
3741eaf496eSIan Rogers
3751eaf496eSIan Rogers /* Order CPU core events to be first */
376c3245d20SIan Rogers a_iscpu = as->pmu ? as->pmu->is_core : true;
377c3245d20SIan Rogers b_iscpu = bs->pmu ? bs->pmu->is_core : true;
378c3245d20SIan Rogers if (a_iscpu != b_iscpu)
379c3245d20SIan Rogers return a_iscpu ? -1 : 1;
3801eaf496eSIan Rogers
3811eaf496eSIan Rogers /* Order by PMU name. */
3821eaf496eSIan Rogers if (as->pmu != bs->pmu) {
383c3245d20SIan Rogers ret = strcmp(as->pmu_name ?: "", bs->pmu_name ?: "");
3841eaf496eSIan Rogers if (ret)
3851eaf496eSIan Rogers return ret;
3861eaf496eSIan Rogers }
3871eaf496eSIan Rogers
3881eaf496eSIan Rogers /* Order by event name. */
389c3245d20SIan Rogers return strcmp(as->name, bs->name);
3901eaf496eSIan Rogers }
3911eaf496eSIan Rogers
pmu_alias_is_duplicate(struct sevent * a,struct sevent * b)392c3245d20SIan Rogers static bool pmu_alias_is_duplicate(struct sevent *a, struct sevent *b)
3931eaf496eSIan Rogers {
3941eaf496eSIan Rogers /* Different names -> never duplicates */
395c3245d20SIan Rogers if (strcmp(a->name ?: "//", b->name ?: "//"))
3961eaf496eSIan Rogers return false;
3971eaf496eSIan Rogers
3981eaf496eSIan Rogers /* Don't remove duplicates for different PMUs */
399c3245d20SIan Rogers return strcmp(a->pmu_name, b->pmu_name) == 0;
4001eaf496eSIan Rogers }
4011eaf496eSIan Rogers
402c3245d20SIan Rogers struct events_callback_state {
403c3245d20SIan Rogers struct sevent *aliases;
404c3245d20SIan Rogers size_t aliases_len;
405c3245d20SIan Rogers size_t index;
406c3245d20SIan Rogers };
407c3245d20SIan Rogers
perf_pmus__print_pmu_events__callback(void * vstate,struct pmu_event_info * info)408c3245d20SIan Rogers static int perf_pmus__print_pmu_events__callback(void *vstate,
409c3245d20SIan Rogers struct pmu_event_info *info)
4101eaf496eSIan Rogers {
411c3245d20SIan Rogers struct events_callback_state *state = vstate;
412c3245d20SIan Rogers struct sevent *s;
413c3245d20SIan Rogers
414c3245d20SIan Rogers if (state->index >= state->aliases_len) {
415c3245d20SIan Rogers pr_err("Unexpected event %s/%s/\n", info->pmu->name, info->name);
416c3245d20SIan Rogers return 1;
417c3245d20SIan Rogers }
418c3245d20SIan Rogers s = &state->aliases[state->index];
419c3245d20SIan Rogers s->pmu = info->pmu;
420c3245d20SIan Rogers #define COPY_STR(str) s->str = info->str ? strdup(info->str) : NULL
421c3245d20SIan Rogers COPY_STR(name);
422c3245d20SIan Rogers COPY_STR(alias);
423c3245d20SIan Rogers COPY_STR(scale_unit);
424c3245d20SIan Rogers COPY_STR(desc);
425c3245d20SIan Rogers COPY_STR(long_desc);
426c3245d20SIan Rogers COPY_STR(encoding_desc);
427c3245d20SIan Rogers COPY_STR(topic);
428c3245d20SIan Rogers COPY_STR(pmu_name);
429c3245d20SIan Rogers #undef COPY_STR
430c3245d20SIan Rogers s->deprecated = info->deprecated;
431c3245d20SIan Rogers state->index++;
4321eaf496eSIan Rogers return 0;
4331eaf496eSIan Rogers }
4341eaf496eSIan Rogers
perf_pmus__print_pmu_events(const struct print_callbacks * print_cb,void * print_state)4351eaf496eSIan Rogers void perf_pmus__print_pmu_events(const struct print_callbacks *print_cb, void *print_state)
4361eaf496eSIan Rogers {
4371eaf496eSIan Rogers struct perf_pmu *pmu;
4381eaf496eSIan Rogers int printed = 0;
439c3245d20SIan Rogers int len;
4401eaf496eSIan Rogers struct sevent *aliases;
441c3245d20SIan Rogers struct events_callback_state state;
442cd4e1efbSIan Rogers bool skip_duplicate_pmus = print_cb->skip_duplicate_pmus(print_state);
443cd4e1efbSIan Rogers struct perf_pmu *(*scan_fn)(struct perf_pmu *);
444cd4e1efbSIan Rogers
445cd4e1efbSIan Rogers if (skip_duplicate_pmus)
446cd4e1efbSIan Rogers scan_fn = perf_pmus__scan_skip_duplicates;
447cd4e1efbSIan Rogers else
448cd4e1efbSIan Rogers scan_fn = perf_pmus__scan;
4491eaf496eSIan Rogers
4501eaf496eSIan Rogers pmu = NULL;
4511eaf496eSIan Rogers len = 0;
452cd4e1efbSIan Rogers while ((pmu = scan_fn(pmu)) != NULL)
453c3245d20SIan Rogers len += perf_pmu__num_events(pmu);
454c3245d20SIan Rogers
4551eaf496eSIan Rogers aliases = zalloc(sizeof(struct sevent) * len);
4561eaf496eSIan Rogers if (!aliases) {
4571eaf496eSIan Rogers pr_err("FATAL: not enough memory to print PMU events\n");
4581eaf496eSIan Rogers return;
4591eaf496eSIan Rogers }
4601eaf496eSIan Rogers pmu = NULL;
461c3245d20SIan Rogers state = (struct events_callback_state) {
462c3245d20SIan Rogers .aliases = aliases,
463c3245d20SIan Rogers .aliases_len = len,
464c3245d20SIan Rogers .index = 0,
465c3245d20SIan Rogers };
466cd4e1efbSIan Rogers while ((pmu = scan_fn(pmu)) != NULL) {
467cd4e1efbSIan Rogers perf_pmu__for_each_event(pmu, skip_duplicate_pmus, &state,
468cd4e1efbSIan Rogers perf_pmus__print_pmu_events__callback);
4691eaf496eSIan Rogers }
4701eaf496eSIan Rogers qsort(aliases, len, sizeof(struct sevent), cmp_sevent);
471c3245d20SIan Rogers for (int j = 0; j < len; j++) {
4721eaf496eSIan Rogers /* Skip duplicates */
4736b3b9c23SJunhao He if (j < len - 1 && pmu_alias_is_duplicate(&aliases[j], &aliases[j + 1]))
4746b3b9c23SJunhao He goto free;
4751eaf496eSIan Rogers
4761eaf496eSIan Rogers print_cb->print_event(print_state,
477c3245d20SIan Rogers aliases[j].topic,
478*f38ab496SJean-Philippe Romain aliases[j].pmu_name,
479c3245d20SIan Rogers aliases[j].name,
480c3245d20SIan Rogers aliases[j].alias,
481c3245d20SIan Rogers aliases[j].scale_unit,
482c3245d20SIan Rogers aliases[j].deprecated,
4831eaf496eSIan Rogers "Kernel PMU event",
484c3245d20SIan Rogers aliases[j].desc,
485c3245d20SIan Rogers aliases[j].long_desc,
486c3245d20SIan Rogers aliases[j].encoding_desc);
4876b3b9c23SJunhao He free:
488c3245d20SIan Rogers zfree(&aliases[j].name);
489c3245d20SIan Rogers zfree(&aliases[j].alias);
490c3245d20SIan Rogers zfree(&aliases[j].scale_unit);
491c3245d20SIan Rogers zfree(&aliases[j].desc);
492c3245d20SIan Rogers zfree(&aliases[j].long_desc);
493c3245d20SIan Rogers zfree(&aliases[j].encoding_desc);
494c3245d20SIan Rogers zfree(&aliases[j].topic);
495c3245d20SIan Rogers zfree(&aliases[j].pmu_name);
4961eaf496eSIan Rogers }
4971eaf496eSIan Rogers if (printed && pager_in_use())
4981eaf496eSIan Rogers printf("\n");
4991eaf496eSIan Rogers
5001eaf496eSIan Rogers zfree(&aliases);
5011eaf496eSIan Rogers }
5021eaf496eSIan Rogers
perf_pmus__have_event(const char * pname,const char * name)5031eaf496eSIan Rogers bool perf_pmus__have_event(const char *pname, const char *name)
5041eaf496eSIan Rogers {
5051eaf496eSIan Rogers struct perf_pmu *pmu = perf_pmus__find(pname);
5061eaf496eSIan Rogers
5071eaf496eSIan Rogers return pmu && perf_pmu__have_event(pmu, name);
5081eaf496eSIan Rogers }
5091eaf496eSIan Rogers
perf_pmus__num_core_pmus(void)510002c4845SIan Rogers int perf_pmus__num_core_pmus(void)
511002c4845SIan Rogers {
512002c4845SIan Rogers static int count;
513002c4845SIan Rogers
514002c4845SIan Rogers if (!count) {
515002c4845SIan Rogers struct perf_pmu *pmu = NULL;
516002c4845SIan Rogers
517002c4845SIan Rogers while ((pmu = perf_pmus__scan_core(pmu)) != NULL)
518002c4845SIan Rogers count++;
519002c4845SIan Rogers }
520002c4845SIan Rogers return count;
521002c4845SIan Rogers }
522002c4845SIan Rogers
__perf_pmus__supports_extended_type(void)52382fe2e45SArnaldo Carvalho de Melo static bool __perf_pmus__supports_extended_type(void)
52482fe2e45SArnaldo Carvalho de Melo {
52582fe2e45SArnaldo Carvalho de Melo struct perf_pmu *pmu = NULL;
52682fe2e45SArnaldo Carvalho de Melo
52782fe2e45SArnaldo Carvalho de Melo if (perf_pmus__num_core_pmus() <= 1)
52882fe2e45SArnaldo Carvalho de Melo return false;
52982fe2e45SArnaldo Carvalho de Melo
53082fe2e45SArnaldo Carvalho de Melo while ((pmu = perf_pmus__scan_core(pmu)) != NULL) {
53182fe2e45SArnaldo Carvalho de Melo if (!is_event_supported(PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES | ((__u64)pmu->type << PERF_PMU_TYPE_SHIFT)))
53282fe2e45SArnaldo Carvalho de Melo return false;
53382fe2e45SArnaldo Carvalho de Melo }
53482fe2e45SArnaldo Carvalho de Melo
53582fe2e45SArnaldo Carvalho de Melo return true;
53682fe2e45SArnaldo Carvalho de Melo }
53782fe2e45SArnaldo Carvalho de Melo
53882fe2e45SArnaldo Carvalho de Melo static bool perf_pmus__do_support_extended_type;
53982fe2e45SArnaldo Carvalho de Melo
perf_pmus__init_supports_extended_type(void)54082fe2e45SArnaldo Carvalho de Melo static void perf_pmus__init_supports_extended_type(void)
54182fe2e45SArnaldo Carvalho de Melo {
54282fe2e45SArnaldo Carvalho de Melo perf_pmus__do_support_extended_type = __perf_pmus__supports_extended_type();
54382fe2e45SArnaldo Carvalho de Melo }
54482fe2e45SArnaldo Carvalho de Melo
perf_pmus__supports_extended_type(void)545251aa040SIan Rogers bool perf_pmus__supports_extended_type(void)
546251aa040SIan Rogers {
54782fe2e45SArnaldo Carvalho de Melo static pthread_once_t extended_type_once = PTHREAD_ONCE_INIT;
54882fe2e45SArnaldo Carvalho de Melo
54982fe2e45SArnaldo Carvalho de Melo pthread_once(&extended_type_once, perf_pmus__init_supports_extended_type);
55082fe2e45SArnaldo Carvalho de Melo
55182fe2e45SArnaldo Carvalho de Melo return perf_pmus__do_support_extended_type;
552251aa040SIan Rogers }
553251aa040SIan Rogers
perf_pmus__default_pmu_name(void)554d685819bSIan Rogers char *perf_pmus__default_pmu_name(void)
555d685819bSIan Rogers {
556d685819bSIan Rogers int fd;
557d685819bSIan Rogers DIR *dir;
558d685819bSIan Rogers struct dirent *dent;
559d685819bSIan Rogers char *result = NULL;
560d685819bSIan Rogers
561d685819bSIan Rogers if (!list_empty(&core_pmus))
562d685819bSIan Rogers return strdup(list_first_entry(&core_pmus, struct perf_pmu, list)->name);
563d685819bSIan Rogers
564d685819bSIan Rogers fd = perf_pmu__event_source_devices_fd();
565d685819bSIan Rogers if (fd < 0)
566d685819bSIan Rogers return strdup("cpu");
567d685819bSIan Rogers
568d685819bSIan Rogers dir = fdopendir(fd);
569d685819bSIan Rogers if (!dir) {
570d685819bSIan Rogers close(fd);
571d685819bSIan Rogers return strdup("cpu");
572d685819bSIan Rogers }
573d685819bSIan Rogers
574d685819bSIan Rogers while ((dent = readdir(dir))) {
575d685819bSIan Rogers if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
576d685819bSIan Rogers continue;
577d685819bSIan Rogers if (is_pmu_core(dent->d_name)) {
578d685819bSIan Rogers result = strdup(dent->d_name);
579d685819bSIan Rogers break;
580d685819bSIan Rogers }
581d685819bSIan Rogers }
582d685819bSIan Rogers
583d685819bSIan Rogers closedir(dir);
584d685819bSIan Rogers return result ?: strdup("cpu");
585d685819bSIan Rogers }
586d685819bSIan Rogers
evsel__find_pmu(const struct evsel * evsel)5871eaf496eSIan Rogers struct perf_pmu *evsel__find_pmu(const struct evsel *evsel)
5881eaf496eSIan Rogers {
5891eaf496eSIan Rogers struct perf_pmu *pmu = evsel->pmu;
5901eaf496eSIan Rogers
5911eaf496eSIan Rogers if (!pmu) {
5921eaf496eSIan Rogers pmu = perf_pmus__find_by_type(evsel->core.attr.type);
5931eaf496eSIan Rogers ((struct evsel *)evsel)->pmu = pmu;
5941eaf496eSIan Rogers }
5951eaf496eSIan Rogers return pmu;
5961eaf496eSIan Rogers }
597f989dc00SJames Clark
perf_pmus__find_core_pmu(void)598f989dc00SJames Clark struct perf_pmu *perf_pmus__find_core_pmu(void)
599f989dc00SJames Clark {
600f989dc00SJames Clark struct perf_pmu *pmu = NULL;
601f989dc00SJames Clark
602f989dc00SJames Clark while ((pmu = perf_pmus__scan_core(pmu))) {
603f989dc00SJames Clark /*
604f989dc00SJames Clark * The cpumap should cover all CPUs. Otherwise, some CPUs may
605f989dc00SJames Clark * not support some events or have different event IDs.
606f989dc00SJames Clark */
607f989dc00SJames Clark if (RC_CHK_ACCESS(pmu->cpus)->nr != cpu__max_cpu().cpu)
608f989dc00SJames Clark return NULL;
609f989dc00SJames Clark
610f989dc00SJames Clark return pmu;
611f989dc00SJames Clark }
612f989dc00SJames Clark return NULL;
613f989dc00SJames Clark }
614