xref: /openbmc/linux/tools/perf/util/pmus.c (revision ecc23d0a422a3118fcf6e4f0a46e17a6c2047b02)
1  // SPDX-License-Identifier: GPL-2.0
2  #include <linux/list.h>
3  #include <linux/list_sort.h>
4  #include <linux/string.h>
5  #include <linux/zalloc.h>
6  #include <subcmd/pager.h>
7  #include <sys/types.h>
8  #include <ctype.h>
9  #include <dirent.h>
10  #include <pthread.h>
11  #include <string.h>
12  #include <unistd.h>
13  #include "cpumap.h"
14  #include "debug.h"
15  #include "evsel.h"
16  #include "pmus.h"
17  #include "pmu.h"
18  #include "print-events.h"
19  
20  /*
21   * core_pmus:  A PMU belongs to core_pmus if it's name is "cpu" or it's sysfs
22   *             directory contains "cpus" file. All PMUs belonging to core_pmus
23   *             must have pmu->is_core=1. If there are more than one PMU in
24   *             this list, perf interprets it as a heterogeneous platform.
25   *             (FWIW, certain ARM platforms having heterogeneous cores uses
26   *             homogeneous PMU, and thus they are treated as homogeneous
27   *             platform by perf because core_pmus will have only one entry)
28   * other_pmus: All other PMUs which are not part of core_pmus list. It doesn't
29   *             matter whether PMU is present per SMT-thread or outside of the
30   *             core in the hw. For e.g., an instance of AMD ibs_fetch// and
31   *             ibs_op// PMUs is present in each hw SMT thread, however they
32   *             are captured under other_pmus. PMUs belonging to other_pmus
33   *             must have pmu->is_core=0 but pmu->is_uncore could be 0 or 1.
34   */
35  static LIST_HEAD(core_pmus);
36  static LIST_HEAD(other_pmus);
37  static bool read_sysfs_core_pmus;
38  static bool read_sysfs_all_pmus;
39  
pmu_name_len_no_suffix(const char * str,unsigned long * num)40  int pmu_name_len_no_suffix(const char *str, unsigned long *num)
41  {
42  	int orig_len, len;
43  
44  	orig_len = len = strlen(str);
45  
46  	/* Non-uncore PMUs have their full length, for example, i915. */
47  	if (!strstarts(str, "uncore_"))
48  		return len;
49  
50  	/*
51  	 * Count trailing digits and '_', if '_{num}' suffix isn't present use
52  	 * the full length.
53  	 */
54  	while (len > 0 && isdigit(str[len - 1]))
55  		len--;
56  
57  	if (len > 0 && len != orig_len && str[len - 1] == '_') {
58  		if (num)
59  			*num = strtoul(&str[len], NULL, 10);
60  		return len - 1;
61  	}
62  	return orig_len;
63  }
64  
perf_pmus__destroy(void)65  void perf_pmus__destroy(void)
66  {
67  	struct perf_pmu *pmu, *tmp;
68  
69  	list_for_each_entry_safe(pmu, tmp, &core_pmus, list) {
70  		list_del(&pmu->list);
71  
72  		perf_pmu__delete(pmu);
73  	}
74  	list_for_each_entry_safe(pmu, tmp, &other_pmus, list) {
75  		list_del(&pmu->list);
76  
77  		perf_pmu__delete(pmu);
78  	}
79  	read_sysfs_core_pmus = false;
80  	read_sysfs_all_pmus = false;
81  }
82  
pmu_find(const char * name)83  static struct perf_pmu *pmu_find(const char *name)
84  {
85  	struct perf_pmu *pmu;
86  
87  	list_for_each_entry(pmu, &core_pmus, list) {
88  		if (!strcmp(pmu->name, name) ||
89  		    (pmu->alias_name && !strcmp(pmu->alias_name, name)))
90  			return pmu;
91  	}
92  	list_for_each_entry(pmu, &other_pmus, list) {
93  		if (!strcmp(pmu->name, name) ||
94  		    (pmu->alias_name && !strcmp(pmu->alias_name, name)))
95  			return pmu;
96  	}
97  
98  	return NULL;
99  }
100  
perf_pmus__find(const char * name)101  struct perf_pmu *perf_pmus__find(const char *name)
102  {
103  	struct perf_pmu *pmu;
104  	int dirfd;
105  	bool core_pmu;
106  
107  	/*
108  	 * Once PMU is loaded it stays in the list,
109  	 * so we keep us from multiple reading/parsing
110  	 * the pmu format definitions.
111  	 */
112  	pmu = pmu_find(name);
113  	if (pmu)
114  		return pmu;
115  
116  	if (read_sysfs_all_pmus)
117  		return NULL;
118  
119  	core_pmu = is_pmu_core(name);
120  	if (core_pmu && read_sysfs_core_pmus)
121  		return NULL;
122  
123  	dirfd = perf_pmu__event_source_devices_fd();
124  	pmu = perf_pmu__lookup(core_pmu ? &core_pmus : &other_pmus, dirfd, name);
125  	close(dirfd);
126  
127  	return pmu;
128  }
129  
perf_pmu__find2(int dirfd,const char * name)130  static struct perf_pmu *perf_pmu__find2(int dirfd, const char *name)
131  {
132  	struct perf_pmu *pmu;
133  	bool core_pmu;
134  
135  	/*
136  	 * Once PMU is loaded it stays in the list,
137  	 * so we keep us from multiple reading/parsing
138  	 * the pmu format definitions.
139  	 */
140  	pmu = pmu_find(name);
141  	if (pmu)
142  		return pmu;
143  
144  	if (read_sysfs_all_pmus)
145  		return NULL;
146  
147  	core_pmu = is_pmu_core(name);
148  	if (core_pmu && read_sysfs_core_pmus)
149  		return NULL;
150  
151  	return perf_pmu__lookup(core_pmu ? &core_pmus : &other_pmus, dirfd, name);
152  }
153  
pmus_cmp(void * priv __maybe_unused,const struct list_head * lhs,const struct list_head * rhs)154  static int pmus_cmp(void *priv __maybe_unused,
155  		    const struct list_head *lhs, const struct list_head *rhs)
156  {
157  	unsigned long lhs_num = 0, rhs_num = 0;
158  	struct perf_pmu *lhs_pmu = container_of(lhs, struct perf_pmu, list);
159  	struct perf_pmu *rhs_pmu = container_of(rhs, struct perf_pmu, list);
160  	const char *lhs_pmu_name = lhs_pmu->name ?: "";
161  	const char *rhs_pmu_name = rhs_pmu->name ?: "";
162  	int lhs_pmu_name_len = pmu_name_len_no_suffix(lhs_pmu_name, &lhs_num);
163  	int rhs_pmu_name_len = pmu_name_len_no_suffix(rhs_pmu_name, &rhs_num);
164  	int ret = strncmp(lhs_pmu_name, rhs_pmu_name,
165  			lhs_pmu_name_len < rhs_pmu_name_len ? lhs_pmu_name_len : rhs_pmu_name_len);
166  
167  	if (lhs_pmu_name_len != rhs_pmu_name_len || ret != 0 || lhs_pmu_name_len == 0)
168  		return ret;
169  
170  	return lhs_num < rhs_num ? -1 : (lhs_num > rhs_num ? 1 : 0);
171  }
172  
173  /* Add all pmus in sysfs to pmu list: */
pmu_read_sysfs(bool core_only)174  static void pmu_read_sysfs(bool core_only)
175  {
176  	int fd;
177  	DIR *dir;
178  	struct dirent *dent;
179  
180  	if (read_sysfs_all_pmus || (core_only && read_sysfs_core_pmus))
181  		return;
182  
183  	fd = perf_pmu__event_source_devices_fd();
184  	if (fd < 0)
185  		return;
186  
187  	dir = fdopendir(fd);
188  	if (!dir) {
189  		close(fd);
190  		return;
191  	}
192  
193  	while ((dent = readdir(dir))) {
194  		if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
195  			continue;
196  		if (core_only && !is_pmu_core(dent->d_name))
197  			continue;
198  		/* add to static LIST_HEAD(core_pmus) or LIST_HEAD(other_pmus): */
199  		perf_pmu__find2(fd, dent->d_name);
200  	}
201  
202  	closedir(dir);
203  	if (list_empty(&core_pmus)) {
204  		if (!perf_pmu__create_placeholder_core_pmu(&core_pmus))
205  			pr_err("Failure to set up any core PMUs\n");
206  	}
207  	list_sort(NULL, &core_pmus, pmus_cmp);
208  	list_sort(NULL, &other_pmus, pmus_cmp);
209  	if (!list_empty(&core_pmus)) {
210  		read_sysfs_core_pmus = true;
211  		if (!core_only)
212  			read_sysfs_all_pmus = true;
213  	}
214  }
215  
__perf_pmus__find_by_type(unsigned int type)216  static struct perf_pmu *__perf_pmus__find_by_type(unsigned int type)
217  {
218  	struct perf_pmu *pmu;
219  
220  	list_for_each_entry(pmu, &core_pmus, list) {
221  		if (pmu->type == type)
222  			return pmu;
223  	}
224  
225  	list_for_each_entry(pmu, &other_pmus, list) {
226  		if (pmu->type == type)
227  			return pmu;
228  	}
229  	return NULL;
230  }
231  
perf_pmus__find_by_type(unsigned int type)232  struct perf_pmu *perf_pmus__find_by_type(unsigned int type)
233  {
234  	struct perf_pmu *pmu = __perf_pmus__find_by_type(type);
235  
236  	if (pmu || read_sysfs_all_pmus)
237  		return pmu;
238  
239  	pmu_read_sysfs(/*core_only=*/false);
240  	pmu = __perf_pmus__find_by_type(type);
241  	return pmu;
242  }
243  
244  /*
245   * pmu iterator: If pmu is NULL, we start at the begin, otherwise return the
246   * next pmu. Returns NULL on end.
247   */
perf_pmus__scan(struct perf_pmu * pmu)248  struct perf_pmu *perf_pmus__scan(struct perf_pmu *pmu)
249  {
250  	bool use_core_pmus = !pmu || pmu->is_core;
251  
252  	if (!pmu) {
253  		pmu_read_sysfs(/*core_only=*/false);
254  		pmu = list_prepare_entry(pmu, &core_pmus, list);
255  	}
256  	if (use_core_pmus) {
257  		list_for_each_entry_continue(pmu, &core_pmus, list)
258  			return pmu;
259  
260  		pmu = NULL;
261  		pmu = list_prepare_entry(pmu, &other_pmus, list);
262  	}
263  	list_for_each_entry_continue(pmu, &other_pmus, list)
264  		return pmu;
265  	return NULL;
266  }
267  
perf_pmus__scan_core(struct perf_pmu * pmu)268  struct perf_pmu *perf_pmus__scan_core(struct perf_pmu *pmu)
269  {
270  	if (!pmu) {
271  		pmu_read_sysfs(/*core_only=*/true);
272  		return list_first_entry_or_null(&core_pmus, typeof(*pmu), list);
273  	}
274  	list_for_each_entry_continue(pmu, &core_pmus, list)
275  		return pmu;
276  
277  	return NULL;
278  }
279  
perf_pmus__scan_skip_duplicates(struct perf_pmu * pmu)280  static struct perf_pmu *perf_pmus__scan_skip_duplicates(struct perf_pmu *pmu)
281  {
282  	bool use_core_pmus = !pmu || pmu->is_core;
283  	int last_pmu_name_len = 0;
284  	const char *last_pmu_name = (pmu && pmu->name) ? pmu->name : "";
285  
286  	if (!pmu) {
287  		pmu_read_sysfs(/*core_only=*/false);
288  		pmu = list_prepare_entry(pmu, &core_pmus, list);
289  	} else
290  		last_pmu_name_len = pmu_name_len_no_suffix(pmu->name ?: "", NULL);
291  
292  	if (use_core_pmus) {
293  		list_for_each_entry_continue(pmu, &core_pmus, list) {
294  			int pmu_name_len = pmu_name_len_no_suffix(pmu->name ?: "", /*num=*/NULL);
295  
296  			if (last_pmu_name_len == pmu_name_len &&
297  			    !strncmp(last_pmu_name, pmu->name ?: "", pmu_name_len))
298  				continue;
299  
300  			return pmu;
301  		}
302  		pmu = NULL;
303  		pmu = list_prepare_entry(pmu, &other_pmus, list);
304  	}
305  	list_for_each_entry_continue(pmu, &other_pmus, list) {
306  		int pmu_name_len = pmu_name_len_no_suffix(pmu->name ?: "", /*num=*/NULL);
307  
308  		if (last_pmu_name_len == pmu_name_len &&
309  		    !strncmp(last_pmu_name, pmu->name ?: "", pmu_name_len))
310  			continue;
311  
312  		return pmu;
313  	}
314  	return NULL;
315  }
316  
perf_pmus__pmu_for_pmu_filter(const char * str)317  const struct perf_pmu *perf_pmus__pmu_for_pmu_filter(const char *str)
318  {
319  	struct perf_pmu *pmu = NULL;
320  
321  	while ((pmu = perf_pmus__scan(pmu)) != NULL) {
322  		if (!strcmp(pmu->name, str))
323  			return pmu;
324  		/* Ignore "uncore_" prefix. */
325  		if (!strncmp(pmu->name, "uncore_", 7)) {
326  			if (!strcmp(pmu->name + 7, str))
327  				return pmu;
328  		}
329  		/* Ignore "cpu_" prefix on Intel hybrid PMUs. */
330  		if (!strncmp(pmu->name, "cpu_", 4)) {
331  			if (!strcmp(pmu->name + 4, str))
332  				return pmu;
333  		}
334  	}
335  	return NULL;
336  }
337  
perf_pmus__num_mem_pmus(void)338  int __weak perf_pmus__num_mem_pmus(void)
339  {
340  	/* All core PMUs are for mem events. */
341  	return perf_pmus__num_core_pmus();
342  }
343  
344  /** Struct for ordering events as output in perf list. */
345  struct sevent {
346  	/** PMU for event. */
347  	const struct perf_pmu *pmu;
348  	const char *name;
349  	const char* alias;
350  	const char *scale_unit;
351  	const char *desc;
352  	const char *long_desc;
353  	const char *encoding_desc;
354  	const char *topic;
355  	const char *pmu_name;
356  	bool deprecated;
357  };
358  
cmp_sevent(const void * a,const void * b)359  static int cmp_sevent(const void *a, const void *b)
360  {
361  	const struct sevent *as = a;
362  	const struct sevent *bs = b;
363  	bool a_iscpu, b_iscpu;
364  	int ret;
365  
366  	/* Put extra events last. */
367  	if (!!as->desc != !!bs->desc)
368  		return !!as->desc - !!bs->desc;
369  
370  	/* Order by topics. */
371  	ret = strcmp(as->topic ?: "", bs->topic ?: "");
372  	if (ret)
373  		return ret;
374  
375  	/* Order CPU core events to be first */
376  	a_iscpu = as->pmu ? as->pmu->is_core : true;
377  	b_iscpu = bs->pmu ? bs->pmu->is_core : true;
378  	if (a_iscpu != b_iscpu)
379  		return a_iscpu ? -1 : 1;
380  
381  	/* Order by PMU name. */
382  	if (as->pmu != bs->pmu) {
383  		ret = strcmp(as->pmu_name ?: "", bs->pmu_name ?: "");
384  		if (ret)
385  			return ret;
386  	}
387  
388  	/* Order by event name. */
389  	return strcmp(as->name, bs->name);
390  }
391  
pmu_alias_is_duplicate(struct sevent * a,struct sevent * b)392  static bool pmu_alias_is_duplicate(struct sevent *a, struct sevent *b)
393  {
394  	/* Different names -> never duplicates */
395  	if (strcmp(a->name ?: "//", b->name ?: "//"))
396  		return false;
397  
398  	/* Don't remove duplicates for different PMUs */
399  	return strcmp(a->pmu_name, b->pmu_name) == 0;
400  }
401  
402  struct events_callback_state {
403  	struct sevent *aliases;
404  	size_t aliases_len;
405  	size_t index;
406  };
407  
perf_pmus__print_pmu_events__callback(void * vstate,struct pmu_event_info * info)408  static int perf_pmus__print_pmu_events__callback(void *vstate,
409  						struct pmu_event_info *info)
410  {
411  	struct events_callback_state *state = vstate;
412  	struct sevent *s;
413  
414  	if (state->index >= state->aliases_len) {
415  		pr_err("Unexpected event %s/%s/\n", info->pmu->name, info->name);
416  		return 1;
417  	}
418  	s = &state->aliases[state->index];
419  	s->pmu = info->pmu;
420  #define COPY_STR(str) s->str = info->str ? strdup(info->str) : NULL
421  	COPY_STR(name);
422  	COPY_STR(alias);
423  	COPY_STR(scale_unit);
424  	COPY_STR(desc);
425  	COPY_STR(long_desc);
426  	COPY_STR(encoding_desc);
427  	COPY_STR(topic);
428  	COPY_STR(pmu_name);
429  #undef COPY_STR
430  	s->deprecated = info->deprecated;
431  	state->index++;
432  	return 0;
433  }
434  
perf_pmus__print_pmu_events(const struct print_callbacks * print_cb,void * print_state)435  void perf_pmus__print_pmu_events(const struct print_callbacks *print_cb, void *print_state)
436  {
437  	struct perf_pmu *pmu;
438  	int printed = 0;
439  	int len;
440  	struct sevent *aliases;
441  	struct events_callback_state state;
442  	bool skip_duplicate_pmus = print_cb->skip_duplicate_pmus(print_state);
443  	struct perf_pmu *(*scan_fn)(struct perf_pmu *);
444  
445  	if (skip_duplicate_pmus)
446  		scan_fn = perf_pmus__scan_skip_duplicates;
447  	else
448  		scan_fn = perf_pmus__scan;
449  
450  	pmu = NULL;
451  	len = 0;
452  	while ((pmu = scan_fn(pmu)) != NULL)
453  		len += perf_pmu__num_events(pmu);
454  
455  	aliases = zalloc(sizeof(struct sevent) * len);
456  	if (!aliases) {
457  		pr_err("FATAL: not enough memory to print PMU events\n");
458  		return;
459  	}
460  	pmu = NULL;
461  	state = (struct events_callback_state) {
462  		.aliases = aliases,
463  		.aliases_len = len,
464  		.index = 0,
465  	};
466  	while ((pmu = scan_fn(pmu)) != NULL) {
467  		perf_pmu__for_each_event(pmu, skip_duplicate_pmus, &state,
468  					 perf_pmus__print_pmu_events__callback);
469  	}
470  	qsort(aliases, len, sizeof(struct sevent), cmp_sevent);
471  	for (int j = 0; j < len; j++) {
472  		/* Skip duplicates */
473  		if (j < len - 1 && pmu_alias_is_duplicate(&aliases[j], &aliases[j + 1]))
474  			goto free;
475  
476  		print_cb->print_event(print_state,
477  				aliases[j].topic,
478  				aliases[j].pmu_name,
479  				aliases[j].name,
480  				aliases[j].alias,
481  				aliases[j].scale_unit,
482  				aliases[j].deprecated,
483  				"Kernel PMU event",
484  				aliases[j].desc,
485  				aliases[j].long_desc,
486  				aliases[j].encoding_desc);
487  free:
488  		zfree(&aliases[j].name);
489  		zfree(&aliases[j].alias);
490  		zfree(&aliases[j].scale_unit);
491  		zfree(&aliases[j].desc);
492  		zfree(&aliases[j].long_desc);
493  		zfree(&aliases[j].encoding_desc);
494  		zfree(&aliases[j].topic);
495  		zfree(&aliases[j].pmu_name);
496  	}
497  	if (printed && pager_in_use())
498  		printf("\n");
499  
500  	zfree(&aliases);
501  }
502  
perf_pmus__have_event(const char * pname,const char * name)503  bool perf_pmus__have_event(const char *pname, const char *name)
504  {
505  	struct perf_pmu *pmu = perf_pmus__find(pname);
506  
507  	return pmu && perf_pmu__have_event(pmu, name);
508  }
509  
perf_pmus__num_core_pmus(void)510  int perf_pmus__num_core_pmus(void)
511  {
512  	static int count;
513  
514  	if (!count) {
515  		struct perf_pmu *pmu = NULL;
516  
517  		while ((pmu = perf_pmus__scan_core(pmu)) != NULL)
518  			count++;
519  	}
520  	return count;
521  }
522  
__perf_pmus__supports_extended_type(void)523  static bool __perf_pmus__supports_extended_type(void)
524  {
525  	struct perf_pmu *pmu = NULL;
526  
527  	if (perf_pmus__num_core_pmus() <= 1)
528  		return false;
529  
530  	while ((pmu = perf_pmus__scan_core(pmu)) != NULL) {
531  		if (!is_event_supported(PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES | ((__u64)pmu->type << PERF_PMU_TYPE_SHIFT)))
532  			return false;
533  	}
534  
535  	return true;
536  }
537  
538  static bool perf_pmus__do_support_extended_type;
539  
perf_pmus__init_supports_extended_type(void)540  static void perf_pmus__init_supports_extended_type(void)
541  {
542  	perf_pmus__do_support_extended_type = __perf_pmus__supports_extended_type();
543  }
544  
perf_pmus__supports_extended_type(void)545  bool perf_pmus__supports_extended_type(void)
546  {
547  	static pthread_once_t extended_type_once = PTHREAD_ONCE_INIT;
548  
549  	pthread_once(&extended_type_once, perf_pmus__init_supports_extended_type);
550  
551  	return perf_pmus__do_support_extended_type;
552  }
553  
perf_pmus__default_pmu_name(void)554  char *perf_pmus__default_pmu_name(void)
555  {
556  	int fd;
557  	DIR *dir;
558  	struct dirent *dent;
559  	char *result = NULL;
560  
561  	if (!list_empty(&core_pmus))
562  		return strdup(list_first_entry(&core_pmus, struct perf_pmu, list)->name);
563  
564  	fd = perf_pmu__event_source_devices_fd();
565  	if (fd < 0)
566  		return strdup("cpu");
567  
568  	dir = fdopendir(fd);
569  	if (!dir) {
570  		close(fd);
571  		return strdup("cpu");
572  	}
573  
574  	while ((dent = readdir(dir))) {
575  		if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
576  			continue;
577  		if (is_pmu_core(dent->d_name)) {
578  			result = strdup(dent->d_name);
579  			break;
580  		}
581  	}
582  
583  	closedir(dir);
584  	return result ?: strdup("cpu");
585  }
586  
evsel__find_pmu(const struct evsel * evsel)587  struct perf_pmu *evsel__find_pmu(const struct evsel *evsel)
588  {
589  	struct perf_pmu *pmu = evsel->pmu;
590  
591  	if (!pmu) {
592  		pmu = perf_pmus__find_by_type(evsel->core.attr.type);
593  		((struct evsel *)evsel)->pmu = pmu;
594  	}
595  	return pmu;
596  }
597  
perf_pmus__find_core_pmu(void)598  struct perf_pmu *perf_pmus__find_core_pmu(void)
599  {
600  	struct perf_pmu *pmu = NULL;
601  
602  	while ((pmu = perf_pmus__scan_core(pmu))) {
603  		/*
604  		 * The cpumap should cover all CPUs. Otherwise, some CPUs may
605  		 * not support some events or have different event IDs.
606  		 */
607  		if (RC_CHK_ACCESS(pmu->cpus)->nr != cpu__max_cpu().cpu)
608  			return NULL;
609  
610  		return pmu;
611  	}
612  	return NULL;
613  }
614