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