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