xref: /linux/tools/perf/util/pmus.c (revision 86f5536004a61a0c797c14a248fc976f03f55cd5)
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 "hwmon_pmu.h"
19 #include "tool_pmu.h"
20 #include "print-events.h"
21 #include "strbuf.h"
22 
23 /*
24  * core_pmus:  A PMU belongs to core_pmus if it's name is "cpu" or it's sysfs
25  *             directory contains "cpus" file. All PMUs belonging to core_pmus
26  *             must have pmu->is_core=1. If there are more than one PMU in
27  *             this list, perf interprets it as a heterogeneous platform.
28  *             (FWIW, certain ARM platforms having heterogeneous cores uses
29  *             homogeneous PMU, and thus they are treated as homogeneous
30  *             platform by perf because core_pmus will have only one entry)
31  * other_pmus: All other PMUs which are not part of core_pmus list. It doesn't
32  *             matter whether PMU is present per SMT-thread or outside of the
33  *             core in the hw. For e.g., an instance of AMD ibs_fetch// and
34  *             ibs_op// PMUs is present in each hw SMT thread, however they
35  *             are captured under other_pmus. PMUs belonging to other_pmus
36  *             must have pmu->is_core=0 but pmu->is_uncore could be 0 or 1.
37  */
38 static LIST_HEAD(core_pmus);
39 static LIST_HEAD(other_pmus);
40 enum perf_tool_pmu_type {
41 	PERF_TOOL_PMU_TYPE_PE_CORE,
42 	PERF_TOOL_PMU_TYPE_PE_OTHER,
43 	PERF_TOOL_PMU_TYPE_TOOL,
44 	PERF_TOOL_PMU_TYPE_HWMON,
45 
46 #define PERF_TOOL_PMU_TYPE_PE_CORE_MASK (1 << PERF_TOOL_PMU_TYPE_PE_CORE)
47 #define PERF_TOOL_PMU_TYPE_PE_OTHER_MASK (1 << PERF_TOOL_PMU_TYPE_PE_OTHER)
48 #define PERF_TOOL_PMU_TYPE_TOOL_MASK (1 << PERF_TOOL_PMU_TYPE_TOOL)
49 #define PERF_TOOL_PMU_TYPE_HWMON_MASK (1 << PERF_TOOL_PMU_TYPE_HWMON)
50 
51 #define PERF_TOOL_PMU_TYPE_ALL_MASK (PERF_TOOL_PMU_TYPE_PE_CORE_MASK |	\
52 					PERF_TOOL_PMU_TYPE_PE_OTHER_MASK | \
53 					PERF_TOOL_PMU_TYPE_TOOL_MASK |	\
54 					PERF_TOOL_PMU_TYPE_HWMON_MASK)
55 };
56 static unsigned int read_pmu_types;
57 
58 static void pmu_read_sysfs(unsigned int to_read_pmus);
59 
60 size_t pmu_name_len_no_suffix(const char *str)
61 {
62 	int orig_len, len;
63 	bool has_hex_digits = false;
64 
65 	orig_len = len = strlen(str);
66 
67 	/* Count trailing digits. */
68 	while (len > 0 && isxdigit(str[len - 1])) {
69 		if (!isdigit(str[len - 1]))
70 			has_hex_digits = true;
71 		len--;
72 	}
73 
74 	if (len > 0 && len != orig_len && str[len - 1] == '_') {
75 		/*
76 		 * There is a '_{num}' suffix. For decimal suffixes any length
77 		 * will do, for hexadecimal ensure more than 2 hex digits so
78 		 * that S390's cpum_cf PMU doesn't match.
79 		 */
80 		if (!has_hex_digits || (orig_len - len) > 2)
81 			return len - 1;
82 	}
83 	/* Use the full length. */
84 	return orig_len;
85 }
86 
87 int pmu_name_cmp(const char *lhs_pmu_name, const char *rhs_pmu_name)
88 {
89 	unsigned long long lhs_num = 0, rhs_num = 0;
90 	size_t lhs_pmu_name_len = pmu_name_len_no_suffix(lhs_pmu_name);
91 	size_t rhs_pmu_name_len = pmu_name_len_no_suffix(rhs_pmu_name);
92 	int ret = strncmp(lhs_pmu_name, rhs_pmu_name,
93 			lhs_pmu_name_len < rhs_pmu_name_len ? lhs_pmu_name_len : rhs_pmu_name_len);
94 
95 	if (lhs_pmu_name_len != rhs_pmu_name_len || ret != 0 || lhs_pmu_name_len == 0)
96 		return ret;
97 
98 	if (lhs_pmu_name_len + 1 < strlen(lhs_pmu_name))
99 		lhs_num = strtoull(&lhs_pmu_name[lhs_pmu_name_len + 1], NULL, 16);
100 	if (rhs_pmu_name_len + 1 < strlen(rhs_pmu_name))
101 		rhs_num = strtoull(&rhs_pmu_name[rhs_pmu_name_len + 1], NULL, 16);
102 
103 	return lhs_num < rhs_num ? -1 : (lhs_num > rhs_num ? 1 : 0);
104 }
105 
106 void perf_pmus__destroy(void)
107 {
108 	struct perf_pmu *pmu, *tmp;
109 
110 	list_for_each_entry_safe(pmu, tmp, &core_pmus, list) {
111 		list_del(&pmu->list);
112 
113 		perf_pmu__delete(pmu);
114 	}
115 	list_for_each_entry_safe(pmu, tmp, &other_pmus, list) {
116 		list_del(&pmu->list);
117 
118 		perf_pmu__delete(pmu);
119 	}
120 	read_pmu_types = 0;
121 }
122 
123 static struct perf_pmu *pmu_find(const char *name)
124 {
125 	struct perf_pmu *pmu;
126 
127 	list_for_each_entry(pmu, &core_pmus, list) {
128 		if (!strcmp(pmu->name, name) ||
129 		    (pmu->alias_name && !strcmp(pmu->alias_name, name)))
130 			return pmu;
131 	}
132 	list_for_each_entry(pmu, &other_pmus, list) {
133 		if (!strcmp(pmu->name, name) ||
134 		    (pmu->alias_name && !strcmp(pmu->alias_name, name)))
135 			return pmu;
136 	}
137 
138 	return NULL;
139 }
140 
141 struct perf_pmu *perf_pmus__find(const char *name)
142 {
143 	struct perf_pmu *pmu;
144 	int dirfd;
145 	bool core_pmu;
146 	unsigned int to_read_pmus = 0;
147 
148 	/*
149 	 * Once PMU is loaded it stays in the list,
150 	 * so we keep us from multiple reading/parsing
151 	 * the pmu format definitions.
152 	 */
153 	pmu = pmu_find(name);
154 	if (pmu)
155 		return pmu;
156 
157 	if (read_pmu_types == PERF_TOOL_PMU_TYPE_ALL_MASK)
158 		return NULL;
159 
160 	core_pmu = is_pmu_core(name);
161 	if (core_pmu && (read_pmu_types & PERF_TOOL_PMU_TYPE_PE_CORE_MASK))
162 		return NULL;
163 
164 	dirfd = perf_pmu__event_source_devices_fd();
165 	pmu = perf_pmu__lookup(core_pmu ? &core_pmus : &other_pmus, dirfd, name,
166 			       /*eager_load=*/false);
167 	close(dirfd);
168 
169 	if (pmu)
170 		return pmu;
171 
172 	/* Looking up an individual perf event PMU failed, check if a tool PMU should be read. */
173 	if (!strncmp(name, "hwmon_", 6))
174 		to_read_pmus |= PERF_TOOL_PMU_TYPE_HWMON_MASK;
175 	else if (!strcmp(name, "tool"))
176 		to_read_pmus |= PERF_TOOL_PMU_TYPE_TOOL_MASK;
177 
178 	if (to_read_pmus) {
179 		pmu_read_sysfs(to_read_pmus);
180 		pmu = pmu_find(name);
181 		if (pmu)
182 			return pmu;
183 	}
184 	/* Read all necessary PMUs from sysfs and see if the PMU is found. */
185 	to_read_pmus = PERF_TOOL_PMU_TYPE_PE_CORE_MASK;
186 	if (!core_pmu)
187 		to_read_pmus |= PERF_TOOL_PMU_TYPE_PE_OTHER_MASK;
188 	pmu_read_sysfs(to_read_pmus);
189 	return pmu_find(name);
190 }
191 
192 static struct perf_pmu *perf_pmu__find2(int dirfd, const char *name)
193 {
194 	struct perf_pmu *pmu;
195 	bool core_pmu;
196 
197 	/*
198 	 * Once PMU is loaded it stays in the list,
199 	 * so we keep us from multiple reading/parsing
200 	 * the pmu format definitions.
201 	 */
202 	pmu = pmu_find(name);
203 	if (pmu)
204 		return pmu;
205 
206 	if (read_pmu_types == PERF_TOOL_PMU_TYPE_ALL_MASK)
207 		return NULL;
208 
209 	core_pmu = is_pmu_core(name);
210 	if (core_pmu && (read_pmu_types & PERF_TOOL_PMU_TYPE_PE_CORE_MASK))
211 		return NULL;
212 
213 	return perf_pmu__lookup(core_pmu ? &core_pmus : &other_pmus, dirfd, name,
214 				/*eager_load=*/false);
215 }
216 
217 static int pmus_cmp(void *priv __maybe_unused,
218 		    const struct list_head *lhs, const struct list_head *rhs)
219 {
220 	struct perf_pmu *lhs_pmu = container_of(lhs, struct perf_pmu, list);
221 	struct perf_pmu *rhs_pmu = container_of(rhs, struct perf_pmu, list);
222 
223 	return pmu_name_cmp(lhs_pmu->name ?: "", rhs_pmu->name ?: "");
224 }
225 
226 /* Add all pmus in sysfs to pmu list: */
227 static void pmu_read_sysfs(unsigned int to_read_types)
228 {
229 	struct perf_pmu *tool_pmu;
230 
231 	if ((read_pmu_types & to_read_types) == to_read_types) {
232 		/* All requested PMU types have been read. */
233 		return;
234 	}
235 
236 	if (to_read_types & (PERF_TOOL_PMU_TYPE_PE_CORE_MASK | PERF_TOOL_PMU_TYPE_PE_OTHER_MASK)) {
237 		int fd = perf_pmu__event_source_devices_fd();
238 		DIR *dir;
239 		struct dirent *dent;
240 		bool core_only = (to_read_types & PERF_TOOL_PMU_TYPE_PE_OTHER_MASK) == 0;
241 
242 		if (fd < 0)
243 			goto skip_pe_pmus;
244 
245 		dir = fdopendir(fd);
246 		if (!dir) {
247 			close(fd);
248 			goto skip_pe_pmus;
249 		}
250 
251 		while ((dent = readdir(dir))) {
252 			if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
253 				continue;
254 			if (core_only && !is_pmu_core(dent->d_name))
255 				continue;
256 			/* add to static LIST_HEAD(core_pmus) or LIST_HEAD(other_pmus): */
257 			perf_pmu__find2(fd, dent->d_name);
258 		}
259 
260 		closedir(dir);
261 	}
262 skip_pe_pmus:
263 	if ((to_read_types & PERF_TOOL_PMU_TYPE_PE_CORE_MASK) && list_empty(&core_pmus)) {
264 		if (!perf_pmu__create_placeholder_core_pmu(&core_pmus))
265 			pr_err("Failure to set up any core PMUs\n");
266 	}
267 	list_sort(NULL, &core_pmus, pmus_cmp);
268 
269 	if ((to_read_types & PERF_TOOL_PMU_TYPE_TOOL_MASK) != 0 &&
270 	    (read_pmu_types & PERF_TOOL_PMU_TYPE_TOOL_MASK) == 0) {
271 		tool_pmu = perf_pmus__tool_pmu();
272 		list_add_tail(&tool_pmu->list, &other_pmus);
273 	}
274 	if ((to_read_types & PERF_TOOL_PMU_TYPE_HWMON_MASK) != 0 &&
275 	    (read_pmu_types & PERF_TOOL_PMU_TYPE_HWMON_MASK) == 0)
276 		perf_pmus__read_hwmon_pmus(&other_pmus);
277 
278 	list_sort(NULL, &other_pmus, pmus_cmp);
279 
280 	read_pmu_types |= to_read_types;
281 }
282 
283 static struct perf_pmu *__perf_pmus__find_by_type(unsigned int type)
284 {
285 	struct perf_pmu *pmu;
286 
287 	list_for_each_entry(pmu, &core_pmus, list) {
288 		if (pmu->type == type)
289 			return pmu;
290 	}
291 
292 	list_for_each_entry(pmu, &other_pmus, list) {
293 		if (pmu->type == type)
294 			return pmu;
295 	}
296 	return NULL;
297 }
298 
299 struct perf_pmu *perf_pmus__find_by_type(unsigned int type)
300 {
301 	unsigned int to_read_pmus;
302 	struct perf_pmu *pmu = __perf_pmus__find_by_type(type);
303 
304 	if (pmu || (read_pmu_types == PERF_TOOL_PMU_TYPE_ALL_MASK))
305 		return pmu;
306 
307 	if (type >= PERF_PMU_TYPE_PE_START && type <= PERF_PMU_TYPE_PE_END) {
308 		to_read_pmus = PERF_TOOL_PMU_TYPE_PE_CORE_MASK |
309 			PERF_TOOL_PMU_TYPE_PE_OTHER_MASK;
310 	} else if (type >= PERF_PMU_TYPE_HWMON_START && type <= PERF_PMU_TYPE_HWMON_END) {
311 		to_read_pmus = PERF_TOOL_PMU_TYPE_HWMON_MASK;
312 	} else {
313 		to_read_pmus = PERF_TOOL_PMU_TYPE_TOOL_MASK;
314 	}
315 	pmu_read_sysfs(to_read_pmus);
316 	pmu = __perf_pmus__find_by_type(type);
317 	return pmu;
318 }
319 
320 /*
321  * pmu iterator: If pmu is NULL, we start at the begin, otherwise return the
322  * next pmu. Returns NULL on end.
323  */
324 struct perf_pmu *perf_pmus__scan(struct perf_pmu *pmu)
325 {
326 	bool use_core_pmus = !pmu || pmu->is_core;
327 
328 	if (!pmu) {
329 		pmu_read_sysfs(PERF_TOOL_PMU_TYPE_ALL_MASK);
330 		pmu = list_prepare_entry(pmu, &core_pmus, list);
331 	}
332 	if (use_core_pmus) {
333 		list_for_each_entry_continue(pmu, &core_pmus, list)
334 			return pmu;
335 
336 		pmu = NULL;
337 		pmu = list_prepare_entry(pmu, &other_pmus, list);
338 	}
339 	list_for_each_entry_continue(pmu, &other_pmus, list)
340 		return pmu;
341 	return NULL;
342 }
343 
344 struct perf_pmu *perf_pmus__scan_core(struct perf_pmu *pmu)
345 {
346 	if (!pmu) {
347 		pmu_read_sysfs(PERF_TOOL_PMU_TYPE_PE_CORE_MASK);
348 		return list_first_entry_or_null(&core_pmus, typeof(*pmu), list);
349 	}
350 	list_for_each_entry_continue(pmu, &core_pmus, list)
351 		return pmu;
352 
353 	return NULL;
354 }
355 
356 static struct perf_pmu *perf_pmus__scan_skip_duplicates(struct perf_pmu *pmu)
357 {
358 	bool use_core_pmus = !pmu || pmu->is_core;
359 	int last_pmu_name_len = 0;
360 	const char *last_pmu_name = (pmu && pmu->name) ? pmu->name : "";
361 
362 	if (!pmu) {
363 		pmu_read_sysfs(PERF_TOOL_PMU_TYPE_ALL_MASK);
364 		pmu = list_prepare_entry(pmu, &core_pmus, list);
365 	} else
366 		last_pmu_name_len = pmu_name_len_no_suffix(pmu->name ?: "");
367 
368 	if (use_core_pmus) {
369 		list_for_each_entry_continue(pmu, &core_pmus, list) {
370 			int pmu_name_len = pmu_name_len_no_suffix(pmu->name ?: "");
371 
372 			if (last_pmu_name_len == pmu_name_len &&
373 			    !strncmp(last_pmu_name, pmu->name ?: "", pmu_name_len))
374 				continue;
375 
376 			return pmu;
377 		}
378 		pmu = NULL;
379 		pmu = list_prepare_entry(pmu, &other_pmus, list);
380 	}
381 	list_for_each_entry_continue(pmu, &other_pmus, list) {
382 		int pmu_name_len = pmu_name_len_no_suffix(pmu->name ?: "");
383 
384 		if (last_pmu_name_len == pmu_name_len &&
385 		    !strncmp(last_pmu_name, pmu->name ?: "", pmu_name_len))
386 			continue;
387 
388 		return pmu;
389 	}
390 	return NULL;
391 }
392 
393 const struct perf_pmu *perf_pmus__pmu_for_pmu_filter(const char *str)
394 {
395 	struct perf_pmu *pmu = NULL;
396 
397 	while ((pmu = perf_pmus__scan(pmu)) != NULL) {
398 		if (!strcmp(pmu->name, str))
399 			return pmu;
400 		/* Ignore "uncore_" prefix. */
401 		if (!strncmp(pmu->name, "uncore_", 7)) {
402 			if (!strcmp(pmu->name + 7, str))
403 				return pmu;
404 		}
405 		/* Ignore "cpu_" prefix on Intel hybrid PMUs. */
406 		if (!strncmp(pmu->name, "cpu_", 4)) {
407 			if (!strcmp(pmu->name + 4, str))
408 				return pmu;
409 		}
410 	}
411 	return NULL;
412 }
413 
414 /** Struct for ordering events as output in perf list. */
415 struct sevent {
416 	/** PMU for event. */
417 	const struct perf_pmu *pmu;
418 	const char *name;
419 	const char* alias;
420 	const char *scale_unit;
421 	const char *desc;
422 	const char *long_desc;
423 	const char *encoding_desc;
424 	const char *topic;
425 	const char *pmu_name;
426 	const char *event_type_desc;
427 	bool deprecated;
428 };
429 
430 static int cmp_sevent(const void *a, const void *b)
431 {
432 	const struct sevent *as = a;
433 	const struct sevent *bs = b;
434 	bool a_iscpu, b_iscpu;
435 	int ret;
436 
437 	/* Put extra events last. */
438 	if (!!as->desc != !!bs->desc)
439 		return !!as->desc - !!bs->desc;
440 
441 	/* Order by topics. */
442 	ret = strcmp(as->topic ?: "", bs->topic ?: "");
443 	if (ret)
444 		return ret;
445 
446 	/* Order CPU core events to be first */
447 	a_iscpu = as->pmu ? as->pmu->is_core : true;
448 	b_iscpu = bs->pmu ? bs->pmu->is_core : true;
449 	if (a_iscpu != b_iscpu)
450 		return a_iscpu ? -1 : 1;
451 
452 	/* Order by PMU name. */
453 	if (as->pmu != bs->pmu) {
454 		ret = strcmp(as->pmu_name ?: "", bs->pmu_name ?: "");
455 		if (ret)
456 			return ret;
457 	}
458 
459 	/* Order by event name. */
460 	return strcmp(as->name, bs->name);
461 }
462 
463 static bool pmu_alias_is_duplicate(struct sevent *a, struct sevent *b)
464 {
465 	/* Different names -> never duplicates */
466 	if (strcmp(a->name ?: "//", b->name ?: "//"))
467 		return false;
468 
469 	/* Don't remove duplicates for different PMUs */
470 	return strcmp(a->pmu_name, b->pmu_name) == 0;
471 }
472 
473 struct events_callback_state {
474 	struct sevent *aliases;
475 	size_t aliases_len;
476 	size_t index;
477 };
478 
479 static int perf_pmus__print_pmu_events__callback(void *vstate,
480 						struct pmu_event_info *info)
481 {
482 	struct events_callback_state *state = vstate;
483 	struct sevent *s;
484 
485 	if (state->index >= state->aliases_len) {
486 		pr_err("Unexpected event %s/%s/\n", info->pmu->name, info->name);
487 		return 1;
488 	}
489 	assert(info->pmu != NULL || info->name != NULL);
490 	s = &state->aliases[state->index];
491 	s->pmu = info->pmu;
492 #define COPY_STR(str) s->str = info->str ? strdup(info->str) : NULL
493 	COPY_STR(name);
494 	COPY_STR(alias);
495 	COPY_STR(scale_unit);
496 	COPY_STR(desc);
497 	COPY_STR(long_desc);
498 	COPY_STR(encoding_desc);
499 	COPY_STR(topic);
500 	COPY_STR(pmu_name);
501 	COPY_STR(event_type_desc);
502 #undef COPY_STR
503 	s->deprecated = info->deprecated;
504 	state->index++;
505 	return 0;
506 }
507 
508 void perf_pmus__print_pmu_events(const struct print_callbacks *print_cb, void *print_state)
509 {
510 	struct perf_pmu *pmu;
511 	int printed = 0;
512 	int len;
513 	struct sevent *aliases;
514 	struct events_callback_state state;
515 	bool skip_duplicate_pmus = print_cb->skip_duplicate_pmus(print_state);
516 	struct perf_pmu *(*scan_fn)(struct perf_pmu *);
517 
518 	if (skip_duplicate_pmus)
519 		scan_fn = perf_pmus__scan_skip_duplicates;
520 	else
521 		scan_fn = perf_pmus__scan;
522 
523 	pmu = NULL;
524 	len = 0;
525 	while ((pmu = scan_fn(pmu)) != NULL)
526 		len += perf_pmu__num_events(pmu);
527 
528 	aliases = zalloc(sizeof(struct sevent) * len);
529 	if (!aliases) {
530 		pr_err("FATAL: not enough memory to print PMU events\n");
531 		return;
532 	}
533 	pmu = NULL;
534 	state = (struct events_callback_state) {
535 		.aliases = aliases,
536 		.aliases_len = len,
537 		.index = 0,
538 	};
539 	while ((pmu = scan_fn(pmu)) != NULL) {
540 		perf_pmu__for_each_event(pmu, skip_duplicate_pmus, &state,
541 					 perf_pmus__print_pmu_events__callback);
542 	}
543 	qsort(aliases, len, sizeof(struct sevent), cmp_sevent);
544 	for (int j = 0; j < len; j++) {
545 		/* Skip duplicates */
546 		if (j < len - 1 && pmu_alias_is_duplicate(&aliases[j], &aliases[j + 1]))
547 			goto free;
548 
549 		print_cb->print_event(print_state,
550 				aliases[j].topic,
551 				aliases[j].pmu_name,
552 				aliases[j].name,
553 				aliases[j].alias,
554 				aliases[j].scale_unit,
555 				aliases[j].deprecated,
556 				aliases[j].event_type_desc,
557 				aliases[j].desc,
558 				aliases[j].long_desc,
559 				aliases[j].encoding_desc);
560 free:
561 		zfree(&aliases[j].name);
562 		zfree(&aliases[j].alias);
563 		zfree(&aliases[j].scale_unit);
564 		zfree(&aliases[j].desc);
565 		zfree(&aliases[j].long_desc);
566 		zfree(&aliases[j].encoding_desc);
567 		zfree(&aliases[j].topic);
568 		zfree(&aliases[j].pmu_name);
569 		zfree(&aliases[j].event_type_desc);
570 	}
571 	if (printed && pager_in_use())
572 		printf("\n");
573 
574 	zfree(&aliases);
575 }
576 
577 struct build_format_string_args {
578 	struct strbuf short_string;
579 	struct strbuf long_string;
580 	int num_formats;
581 };
582 
583 static int build_format_string(void *state, const char *name, int config,
584 			       const unsigned long *bits)
585 {
586 	struct build_format_string_args *args = state;
587 	unsigned int num_bits;
588 	int ret1, ret2 = 0;
589 
590 	(void)config;
591 	args->num_formats++;
592 	if (args->num_formats > 1) {
593 		strbuf_addch(&args->long_string, ',');
594 		if (args->num_formats < 4)
595 			strbuf_addch(&args->short_string, ',');
596 	}
597 	num_bits = bits ? bitmap_weight(bits, PERF_PMU_FORMAT_BITS) : 0;
598 	if (num_bits <= 1) {
599 		ret1 = strbuf_addf(&args->long_string, "%s", name);
600 		if (args->num_formats < 4)
601 			ret2 = strbuf_addf(&args->short_string, "%s", name);
602 	} else if (num_bits > 8) {
603 		ret1 = strbuf_addf(&args->long_string, "%s=0..0x%llx", name,
604 				   ULLONG_MAX >> (64 - num_bits));
605 		if (args->num_formats < 4) {
606 			ret2 = strbuf_addf(&args->short_string, "%s=0..0x%llx", name,
607 					   ULLONG_MAX >> (64 - num_bits));
608 		}
609 	} else {
610 		ret1 = strbuf_addf(&args->long_string, "%s=0..%llu", name,
611 				  ULLONG_MAX >> (64 - num_bits));
612 		if (args->num_formats < 4) {
613 			ret2 = strbuf_addf(&args->short_string, "%s=0..%llu", name,
614 					   ULLONG_MAX >> (64 - num_bits));
615 		}
616 	}
617 	return ret1 < 0 ? ret1 : (ret2 < 0 ? ret2 : 0);
618 }
619 
620 void perf_pmus__print_raw_pmu_events(const struct print_callbacks *print_cb, void *print_state)
621 {
622 	bool skip_duplicate_pmus = print_cb->skip_duplicate_pmus(print_state);
623 	struct perf_pmu *(*scan_fn)(struct perf_pmu *);
624 	struct perf_pmu *pmu = NULL;
625 
626 	if (skip_duplicate_pmus)
627 		scan_fn = perf_pmus__scan_skip_duplicates;
628 	else
629 		scan_fn = perf_pmus__scan;
630 
631 	while ((pmu = scan_fn(pmu)) != NULL) {
632 		struct build_format_string_args format_args = {
633 			.short_string = STRBUF_INIT,
634 			.long_string = STRBUF_INIT,
635 			.num_formats = 0,
636 		};
637 		int len = pmu_name_len_no_suffix(pmu->name);
638 		const char *desc = "(see 'man perf-list' or 'man perf-record' on how to encode it)";
639 
640 		if (!pmu->is_core)
641 			desc = NULL;
642 
643 		strbuf_addf(&format_args.short_string, "%.*s/", len, pmu->name);
644 		strbuf_addf(&format_args.long_string, "%.*s/", len, pmu->name);
645 		perf_pmu__for_each_format(pmu, &format_args, build_format_string);
646 
647 		if (format_args.num_formats > 3)
648 			strbuf_addf(&format_args.short_string, ",.../modifier");
649 		else
650 			strbuf_addf(&format_args.short_string, "/modifier");
651 
652 		strbuf_addf(&format_args.long_string, "/modifier");
653 		print_cb->print_event(print_state,
654 				/*topic=*/NULL,
655 				/*pmu_name=*/NULL,
656 				format_args.short_string.buf,
657 				/*event_alias=*/NULL,
658 				/*scale_unit=*/NULL,
659 				/*deprecated=*/false,
660 				"Raw event descriptor",
661 				desc,
662 				/*long_desc=*/NULL,
663 				format_args.long_string.buf);
664 
665 		strbuf_release(&format_args.short_string);
666 		strbuf_release(&format_args.long_string);
667 	}
668 }
669 
670 bool perf_pmus__have_event(const char *pname, const char *name)
671 {
672 	struct perf_pmu *pmu = perf_pmus__find(pname);
673 
674 	return pmu && perf_pmu__have_event(pmu, name);
675 }
676 
677 int perf_pmus__num_core_pmus(void)
678 {
679 	static int count;
680 
681 	if (!count) {
682 		struct perf_pmu *pmu = NULL;
683 
684 		while ((pmu = perf_pmus__scan_core(pmu)) != NULL)
685 			count++;
686 	}
687 	return count;
688 }
689 
690 static bool __perf_pmus__supports_extended_type(void)
691 {
692 	struct perf_pmu *pmu = NULL;
693 
694 	if (perf_pmus__num_core_pmus() <= 1)
695 		return false;
696 
697 	while ((pmu = perf_pmus__scan_core(pmu)) != NULL) {
698 		if (!is_event_supported(PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES | ((__u64)pmu->type << PERF_PMU_TYPE_SHIFT)))
699 			return false;
700 	}
701 
702 	return true;
703 }
704 
705 static bool perf_pmus__do_support_extended_type;
706 
707 static void perf_pmus__init_supports_extended_type(void)
708 {
709 	perf_pmus__do_support_extended_type = __perf_pmus__supports_extended_type();
710 }
711 
712 bool perf_pmus__supports_extended_type(void)
713 {
714 	static pthread_once_t extended_type_once = PTHREAD_ONCE_INIT;
715 
716 	pthread_once(&extended_type_once, perf_pmus__init_supports_extended_type);
717 
718 	return perf_pmus__do_support_extended_type;
719 }
720 
721 char *perf_pmus__default_pmu_name(void)
722 {
723 	int fd;
724 	DIR *dir;
725 	struct dirent *dent;
726 	char *result = NULL;
727 
728 	if (!list_empty(&core_pmus))
729 		return strdup(list_first_entry(&core_pmus, struct perf_pmu, list)->name);
730 
731 	fd = perf_pmu__event_source_devices_fd();
732 	if (fd < 0)
733 		return strdup("cpu");
734 
735 	dir = fdopendir(fd);
736 	if (!dir) {
737 		close(fd);
738 		return strdup("cpu");
739 	}
740 
741 	while ((dent = readdir(dir))) {
742 		if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
743 			continue;
744 		if (is_pmu_core(dent->d_name)) {
745 			result = strdup(dent->d_name);
746 			break;
747 		}
748 	}
749 
750 	closedir(dir);
751 	return result ?: strdup("cpu");
752 }
753 
754 struct perf_pmu *evsel__find_pmu(const struct evsel *evsel)
755 {
756 	struct perf_pmu *pmu = evsel->pmu;
757 	bool legacy_core_type;
758 
759 	if (pmu)
760 		return pmu;
761 
762 	pmu = perf_pmus__find_by_type(evsel->core.attr.type);
763 	legacy_core_type =
764 		evsel->core.attr.type == PERF_TYPE_HARDWARE ||
765 		evsel->core.attr.type == PERF_TYPE_HW_CACHE;
766 	if (!pmu && legacy_core_type) {
767 		if (perf_pmus__supports_extended_type()) {
768 			u32 type = evsel->core.attr.config >> PERF_PMU_TYPE_SHIFT;
769 
770 			pmu = perf_pmus__find_by_type(type);
771 		} else {
772 			pmu = perf_pmus__find_core_pmu();
773 		}
774 	}
775 	((struct evsel *)evsel)->pmu = pmu;
776 	return pmu;
777 }
778 
779 struct perf_pmu *perf_pmus__find_core_pmu(void)
780 {
781 	return perf_pmus__scan_core(NULL);
782 }
783 
784 struct perf_pmu *perf_pmus__add_test_pmu(int test_sysfs_dirfd, const char *name)
785 {
786 	/*
787 	 * Some PMU functions read from the sysfs mount point, so care is
788 	 * needed, hence passing the eager_load flag to load things like the
789 	 * format files.
790 	 */
791 	return perf_pmu__lookup(&other_pmus, test_sysfs_dirfd, name, /*eager_load=*/true);
792 }
793 
794 struct perf_pmu *perf_pmus__add_test_hwmon_pmu(int hwmon_dir,
795 					       const char *sysfs_name,
796 					       const char *name)
797 {
798 	return hwmon_pmu__new(&other_pmus, hwmon_dir, sysfs_name, name);
799 }
800 
801 struct perf_pmu *perf_pmus__fake_pmu(void)
802 {
803 	static struct perf_pmu fake = {
804 		.name = "fake",
805 		.type = PERF_PMU_TYPE_FAKE,
806 		.format = LIST_HEAD_INIT(fake.format),
807 	};
808 
809 	return &fake;
810 }
811