xref: /openbmc/linux/tools/perf/util/pmu.c (revision 89cc9abe)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/list.h>
3 #include <linux/compiler.h>
4 #include <linux/string.h>
5 #include <linux/zalloc.h>
6 #include <linux/ctype.h>
7 #include <subcmd/pager.h>
8 #include <sys/types.h>
9 #include <errno.h>
10 #include <fcntl.h>
11 #include <sys/stat.h>
12 #include <unistd.h>
13 #include <stdio.h>
14 #include <stdbool.h>
15 #include <stdarg.h>
16 #include <dirent.h>
17 #include <api/fs/fs.h>
18 #include <locale.h>
19 #include <regex.h>
20 #include <perf/cpumap.h>
21 #include <fnmatch.h>
22 #include <math.h>
23 #include "debug.h"
24 #include "evsel.h"
25 #include "pmu.h"
26 #include "pmus.h"
27 #include "parse-events.h"
28 #include "print-events.h"
29 #include "header.h"
30 #include "string2.h"
31 #include "strbuf.h"
32 #include "fncache.h"
33 #include "pmu-hybrid.h"
34 
35 struct perf_pmu perf_pmu__fake;
36 
37 /**
38  * struct perf_pmu_format - Values from a format file read from
39  * <sysfs>/devices/cpu/format/ held in struct perf_pmu.
40  *
41  * For example, the contents of <sysfs>/devices/cpu/format/event may be
42  * "config:0-7" and will be represented here as name="event",
43  * value=PERF_PMU_FORMAT_VALUE_CONFIG and bits 0 to 7 will be set.
44  */
45 struct perf_pmu_format {
46 	/** @name: The modifier/file name. */
47 	char *name;
48 	/**
49 	 * @value : Which config value the format relates to. Supported values
50 	 * are from PERF_PMU_FORMAT_VALUE_CONFIG to
51 	 * PERF_PMU_FORMAT_VALUE_CONFIG_END.
52 	 */
53 	int value;
54 	/** @bits: Which config bits are set by this format value. */
55 	DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
56 	/** @list: Element on list within struct perf_pmu. */
57 	struct list_head list;
58 };
59 
60 int perf_pmu_parse(struct list_head *list, char *name);
61 extern FILE *perf_pmu_in;
62 
63 static bool hybrid_scanned;
64 
65 /*
66  * Parse & process all the sysfs attributes located under
67  * the directory specified in 'dir' parameter.
68  */
69 int perf_pmu__format_parse(char *dir, struct list_head *head)
70 {
71 	struct dirent *evt_ent;
72 	DIR *format_dir;
73 	int ret = 0;
74 
75 	format_dir = opendir(dir);
76 	if (!format_dir)
77 		return -EINVAL;
78 
79 	while (!ret && (evt_ent = readdir(format_dir))) {
80 		char path[PATH_MAX];
81 		char *name = evt_ent->d_name;
82 		FILE *file;
83 
84 		if (!strcmp(name, ".") || !strcmp(name, ".."))
85 			continue;
86 
87 		snprintf(path, PATH_MAX, "%s/%s", dir, name);
88 
89 		ret = -EINVAL;
90 		file = fopen(path, "r");
91 		if (!file)
92 			break;
93 
94 		perf_pmu_in = file;
95 		ret = perf_pmu_parse(head, name);
96 		fclose(file);
97 	}
98 
99 	closedir(format_dir);
100 	return ret;
101 }
102 
103 /*
104  * Reading/parsing the default pmu format definition, which should be
105  * located at:
106  * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
107  */
108 static int pmu_format(const char *name, struct list_head *format)
109 {
110 	char path[PATH_MAX];
111 
112 	if (!perf_pmu__pathname_scnprintf(path, sizeof(path), name, "format"))
113 		return -1;
114 
115 	if (!file_available(path))
116 		return 0;
117 
118 	if (perf_pmu__format_parse(path, format))
119 		return -1;
120 
121 	return 0;
122 }
123 
124 int perf_pmu__convert_scale(const char *scale, char **end, double *sval)
125 {
126 	char *lc;
127 	int ret = 0;
128 
129 	/*
130 	 * save current locale
131 	 */
132 	lc = setlocale(LC_NUMERIC, NULL);
133 
134 	/*
135 	 * The lc string may be allocated in static storage,
136 	 * so get a dynamic copy to make it survive setlocale
137 	 * call below.
138 	 */
139 	lc = strdup(lc);
140 	if (!lc) {
141 		ret = -ENOMEM;
142 		goto out;
143 	}
144 
145 	/*
146 	 * force to C locale to ensure kernel
147 	 * scale string is converted correctly.
148 	 * kernel uses default C locale.
149 	 */
150 	setlocale(LC_NUMERIC, "C");
151 
152 	*sval = strtod(scale, end);
153 
154 out:
155 	/* restore locale */
156 	setlocale(LC_NUMERIC, lc);
157 	free(lc);
158 	return ret;
159 }
160 
161 static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name)
162 {
163 	struct stat st;
164 	ssize_t sret;
165 	char scale[128];
166 	int fd, ret = -1;
167 	char path[PATH_MAX];
168 
169 	scnprintf(path, PATH_MAX, "%s/%s.scale", dir, name);
170 
171 	fd = open(path, O_RDONLY);
172 	if (fd == -1)
173 		return -1;
174 
175 	if (fstat(fd, &st) < 0)
176 		goto error;
177 
178 	sret = read(fd, scale, sizeof(scale)-1);
179 	if (sret < 0)
180 		goto error;
181 
182 	if (scale[sret - 1] == '\n')
183 		scale[sret - 1] = '\0';
184 	else
185 		scale[sret] = '\0';
186 
187 	ret = perf_pmu__convert_scale(scale, NULL, &alias->scale);
188 error:
189 	close(fd);
190 	return ret;
191 }
192 
193 static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name)
194 {
195 	char path[PATH_MAX];
196 	ssize_t sret;
197 	int fd;
198 
199 	scnprintf(path, PATH_MAX, "%s/%s.unit", dir, name);
200 
201 	fd = open(path, O_RDONLY);
202 	if (fd == -1)
203 		return -1;
204 
205 	sret = read(fd, alias->unit, UNIT_MAX_LEN);
206 	if (sret < 0)
207 		goto error;
208 
209 	close(fd);
210 
211 	if (alias->unit[sret - 1] == '\n')
212 		alias->unit[sret - 1] = '\0';
213 	else
214 		alias->unit[sret] = '\0';
215 
216 	return 0;
217 error:
218 	close(fd);
219 	alias->unit[0] = '\0';
220 	return -1;
221 }
222 
223 static int
224 perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *name)
225 {
226 	char path[PATH_MAX];
227 	int fd;
228 
229 	scnprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name);
230 
231 	fd = open(path, O_RDONLY);
232 	if (fd == -1)
233 		return -1;
234 
235 	close(fd);
236 
237 	alias->per_pkg = true;
238 	return 0;
239 }
240 
241 static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias,
242 				    char *dir, char *name)
243 {
244 	char path[PATH_MAX];
245 	int fd;
246 
247 	scnprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name);
248 
249 	fd = open(path, O_RDONLY);
250 	if (fd == -1)
251 		return -1;
252 
253 	alias->snapshot = true;
254 	close(fd);
255 	return 0;
256 }
257 
258 static void perf_pmu_assign_str(char *name, const char *field, char **old_str,
259 				char **new_str)
260 {
261 	if (!*old_str)
262 		goto set_new;
263 
264 	if (*new_str) {	/* Have new string, check with old */
265 		if (strcasecmp(*old_str, *new_str))
266 			pr_debug("alias %s differs in field '%s'\n",
267 				 name, field);
268 		zfree(old_str);
269 	} else		/* Nothing new --> keep old string */
270 		return;
271 set_new:
272 	*old_str = *new_str;
273 	*new_str = NULL;
274 }
275 
276 static void perf_pmu_update_alias(struct perf_pmu_alias *old,
277 				  struct perf_pmu_alias *newalias)
278 {
279 	perf_pmu_assign_str(old->name, "desc", &old->desc, &newalias->desc);
280 	perf_pmu_assign_str(old->name, "long_desc", &old->long_desc,
281 			    &newalias->long_desc);
282 	perf_pmu_assign_str(old->name, "topic", &old->topic, &newalias->topic);
283 	perf_pmu_assign_str(old->name, "value", &old->str, &newalias->str);
284 	old->scale = newalias->scale;
285 	old->per_pkg = newalias->per_pkg;
286 	old->snapshot = newalias->snapshot;
287 	memcpy(old->unit, newalias->unit, sizeof(old->unit));
288 }
289 
290 /* Delete an alias entry. */
291 void perf_pmu_free_alias(struct perf_pmu_alias *newalias)
292 {
293 	zfree(&newalias->name);
294 	zfree(&newalias->desc);
295 	zfree(&newalias->long_desc);
296 	zfree(&newalias->topic);
297 	zfree(&newalias->str);
298 	zfree(&newalias->pmu_name);
299 	parse_events_terms__purge(&newalias->terms);
300 	free(newalias);
301 }
302 
303 /* Merge an alias, search in alias list. If this name is already
304  * present merge both of them to combine all information.
305  */
306 static bool perf_pmu_merge_alias(struct perf_pmu_alias *newalias,
307 				 struct list_head *alist)
308 {
309 	struct perf_pmu_alias *a;
310 
311 	list_for_each_entry(a, alist, list) {
312 		if (!strcasecmp(newalias->name, a->name)) {
313 			if (newalias->pmu_name && a->pmu_name &&
314 			    !strcasecmp(newalias->pmu_name, a->pmu_name)) {
315 				continue;
316 			}
317 			perf_pmu_update_alias(a, newalias);
318 			perf_pmu_free_alias(newalias);
319 			return true;
320 		}
321 	}
322 	return false;
323 }
324 
325 static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name,
326 				 char *desc, char *val, const struct pmu_event *pe)
327 {
328 	struct parse_events_term *term;
329 	struct perf_pmu_alias *alias;
330 	int ret;
331 	int num;
332 	char newval[256];
333 	char *long_desc = NULL, *topic = NULL, *unit = NULL, *perpkg = NULL,
334 	     *deprecated = NULL, *pmu_name = NULL;
335 
336 	if (pe) {
337 		long_desc = (char *)pe->long_desc;
338 		topic = (char *)pe->topic;
339 		unit = (char *)pe->unit;
340 		perpkg = (char *)pe->perpkg;
341 		deprecated = (char *)pe->deprecated;
342 		pmu_name = (char *)pe->pmu;
343 	}
344 
345 	alias = malloc(sizeof(*alias));
346 	if (!alias)
347 		return -ENOMEM;
348 
349 	INIT_LIST_HEAD(&alias->terms);
350 	alias->scale = 1.0;
351 	alias->unit[0] = '\0';
352 	alias->per_pkg = false;
353 	alias->snapshot = false;
354 	alias->deprecated = false;
355 
356 	ret = parse_events_terms(&alias->terms, val);
357 	if (ret) {
358 		pr_err("Cannot parse alias %s: %d\n", val, ret);
359 		free(alias);
360 		return ret;
361 	}
362 
363 	/* Scan event and remove leading zeroes, spaces, newlines, some
364 	 * platforms have terms specified as
365 	 * event=0x0091 (read from files ../<PMU>/events/<FILE>
366 	 * and terms specified as event=0x91 (read from JSON files).
367 	 *
368 	 * Rebuild string to make alias->str member comparable.
369 	 */
370 	memset(newval, 0, sizeof(newval));
371 	ret = 0;
372 	list_for_each_entry(term, &alias->terms, list) {
373 		if (ret)
374 			ret += scnprintf(newval + ret, sizeof(newval) - ret,
375 					 ",");
376 		if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
377 			ret += scnprintf(newval + ret, sizeof(newval) - ret,
378 					 "%s=%#x", term->config, term->val.num);
379 		else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
380 			ret += scnprintf(newval + ret, sizeof(newval) - ret,
381 					 "%s=%s", term->config, term->val.str);
382 	}
383 
384 	alias->name = strdup(name);
385 	if (dir) {
386 		/*
387 		 * load unit name and scale if available
388 		 */
389 		perf_pmu__parse_unit(alias, dir, name);
390 		perf_pmu__parse_scale(alias, dir, name);
391 		perf_pmu__parse_per_pkg(alias, dir, name);
392 		perf_pmu__parse_snapshot(alias, dir, name);
393 	}
394 
395 	alias->desc = desc ? strdup(desc) : NULL;
396 	alias->long_desc = long_desc ? strdup(long_desc) :
397 				desc ? strdup(desc) : NULL;
398 	alias->topic = topic ? strdup(topic) : NULL;
399 	if (unit) {
400 		if (perf_pmu__convert_scale(unit, &unit, &alias->scale) < 0)
401 			return -1;
402 		snprintf(alias->unit, sizeof(alias->unit), "%s", unit);
403 	}
404 	alias->per_pkg = perpkg && sscanf(perpkg, "%d", &num) == 1 && num == 1;
405 	alias->str = strdup(newval);
406 	alias->pmu_name = pmu_name ? strdup(pmu_name) : NULL;
407 
408 	if (deprecated)
409 		alias->deprecated = true;
410 
411 	if (!perf_pmu_merge_alias(alias, list))
412 		list_add_tail(&alias->list, list);
413 
414 	return 0;
415 }
416 
417 static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file)
418 {
419 	char buf[256];
420 	int ret;
421 
422 	ret = fread(buf, 1, sizeof(buf), file);
423 	if (ret == 0)
424 		return -EINVAL;
425 
426 	buf[ret] = 0;
427 
428 	/* Remove trailing newline from sysfs file */
429 	strim(buf);
430 
431 	return __perf_pmu__new_alias(list, dir, name, NULL, buf, NULL);
432 }
433 
434 static inline bool pmu_alias_info_file(char *name)
435 {
436 	size_t len;
437 
438 	len = strlen(name);
439 	if (len > 5 && !strcmp(name + len - 5, ".unit"))
440 		return true;
441 	if (len > 6 && !strcmp(name + len - 6, ".scale"))
442 		return true;
443 	if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
444 		return true;
445 	if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
446 		return true;
447 
448 	return false;
449 }
450 
451 /*
452  * Process all the sysfs attributes located under the directory
453  * specified in 'dir' parameter.
454  */
455 static int pmu_aliases_parse(char *dir, struct list_head *head)
456 {
457 	struct dirent *evt_ent;
458 	DIR *event_dir;
459 
460 	event_dir = opendir(dir);
461 	if (!event_dir)
462 		return -EINVAL;
463 
464 	while ((evt_ent = readdir(event_dir))) {
465 		char path[PATH_MAX];
466 		char *name = evt_ent->d_name;
467 		FILE *file;
468 
469 		if (!strcmp(name, ".") || !strcmp(name, ".."))
470 			continue;
471 
472 		/*
473 		 * skip info files parsed in perf_pmu__new_alias()
474 		 */
475 		if (pmu_alias_info_file(name))
476 			continue;
477 
478 		scnprintf(path, PATH_MAX, "%s/%s", dir, name);
479 
480 		file = fopen(path, "r");
481 		if (!file) {
482 			pr_debug("Cannot open %s\n", path);
483 			continue;
484 		}
485 
486 		if (perf_pmu__new_alias(head, dir, name, file) < 0)
487 			pr_debug("Cannot set up %s\n", name);
488 		fclose(file);
489 	}
490 
491 	closedir(event_dir);
492 	return 0;
493 }
494 
495 /*
496  * Reading the pmu event aliases definition, which should be located at:
497  * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
498  */
499 static int pmu_aliases(const char *name, struct list_head *head)
500 {
501 	char path[PATH_MAX];
502 
503 	if (!perf_pmu__pathname_scnprintf(path, sizeof(path), name, "events"))
504 		return -1;
505 
506 	if (!file_available(path))
507 		return 0;
508 
509 	if (pmu_aliases_parse(path, head))
510 		return -1;
511 
512 	return 0;
513 }
514 
515 static int pmu_alias_terms(struct perf_pmu_alias *alias,
516 			   struct list_head *terms)
517 {
518 	struct parse_events_term *term, *cloned;
519 	LIST_HEAD(list);
520 	int ret;
521 
522 	list_for_each_entry(term, &alias->terms, list) {
523 		ret = parse_events_term__clone(&cloned, term);
524 		if (ret) {
525 			parse_events_terms__purge(&list);
526 			return ret;
527 		}
528 		/*
529 		 * Weak terms don't override command line options,
530 		 * which we don't want for implicit terms in aliases.
531 		 */
532 		cloned->weak = true;
533 		list_add_tail(&cloned->list, &list);
534 	}
535 	list_splice(&list, terms);
536 	return 0;
537 }
538 
539 /* Add all pmus in sysfs to pmu list: */
540 static void pmu_read_sysfs(void)
541 {
542 	char path[PATH_MAX];
543 	DIR *dir;
544 	struct dirent *dent;
545 
546 	if (!perf_pmu__event_source_devices_scnprintf(path, sizeof(path)))
547 		return;
548 
549 	dir = opendir(path);
550 	if (!dir)
551 		return;
552 
553 	while ((dent = readdir(dir))) {
554 		if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
555 			continue;
556 		/* add to static LIST_HEAD(pmus): */
557 		perf_pmu__find(dent->d_name);
558 	}
559 
560 	closedir(dir);
561 }
562 
563 /*
564  * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64)
565  * may have a "cpus" file.
566  */
567 static struct perf_cpu_map *pmu_cpumask(const char *name)
568 {
569 	struct perf_cpu_map *cpus;
570 	const char *templates[] = {
571 		"cpumask",
572 		"cpus",
573 		NULL
574 	};
575 	const char **template;
576 	char pmu_name[PATH_MAX];
577 	struct perf_pmu pmu = {.name = pmu_name};
578 	FILE *file;
579 
580 	strlcpy(pmu_name, name, sizeof(pmu_name));
581 	for (template = templates; *template; template++) {
582 		file = perf_pmu__open_file(&pmu, *template);
583 		if (!file)
584 			continue;
585 		cpus = perf_cpu_map__read(file);
586 		if (cpus)
587 			return cpus;
588 	}
589 
590 	return NULL;
591 }
592 
593 static bool pmu_is_uncore(const char *name)
594 {
595 	char path[PATH_MAX];
596 
597 	if (perf_pmu__hybrid_mounted(name))
598 		return false;
599 
600 	perf_pmu__pathname_scnprintf(path, sizeof(path), name, "cpumask");
601 	return file_available(path);
602 }
603 
604 static char *pmu_id(const char *name)
605 {
606 	char path[PATH_MAX], *str;
607 	size_t len;
608 
609 	perf_pmu__pathname_scnprintf(path, sizeof(path), name, "identifier");
610 
611 	if (filename__read_str(path, &str, &len) < 0)
612 		return NULL;
613 
614 	str[len - 1] = 0; /* remove line feed */
615 
616 	return str;
617 }
618 
619 /*
620  *  PMU CORE devices have different name other than cpu in sysfs on some
621  *  platforms.
622  *  Looking for possible sysfs files to identify the arm core device.
623  */
624 static int is_arm_pmu_core(const char *name)
625 {
626 	char path[PATH_MAX];
627 
628 	if (!perf_pmu__pathname_scnprintf(path, sizeof(path), name, "cpus"))
629 		return 0;
630 	return file_available(path);
631 }
632 
633 char *perf_pmu__getcpuid(struct perf_pmu *pmu)
634 {
635 	char *cpuid;
636 	static bool printed;
637 
638 	cpuid = getenv("PERF_CPUID");
639 	if (cpuid)
640 		cpuid = strdup(cpuid);
641 	if (!cpuid)
642 		cpuid = get_cpuid_str(pmu);
643 	if (!cpuid)
644 		return NULL;
645 
646 	if (!printed) {
647 		pr_debug("Using CPUID %s\n", cpuid);
648 		printed = true;
649 	}
650 	return cpuid;
651 }
652 
653 __weak const struct pmu_events_table *pmu_events_table__find(void)
654 {
655 	return perf_pmu__find_events_table(NULL);
656 }
657 
658 __weak const struct pmu_metrics_table *pmu_metrics_table__find(void)
659 {
660 	return perf_pmu__find_metrics_table(NULL);
661 }
662 
663 /*
664  * Suffix must be in form tok_{digits}, or tok{digits}, or same as pmu_name
665  * to be valid.
666  */
667 static bool perf_pmu__valid_suffix(const char *pmu_name, char *tok)
668 {
669 	const char *p;
670 
671 	if (strncmp(pmu_name, tok, strlen(tok)))
672 		return false;
673 
674 	p = pmu_name + strlen(tok);
675 	if (*p == 0)
676 		return true;
677 
678 	if (*p == '_')
679 		++p;
680 
681 	/* Ensure we end in a number */
682 	while (1) {
683 		if (!isdigit(*p))
684 			return false;
685 		if (*(++p) == 0)
686 			break;
687 	}
688 
689 	return true;
690 }
691 
692 bool pmu_uncore_alias_match(const char *pmu_name, const char *name)
693 {
694 	char *tmp = NULL, *tok, *str;
695 	bool res;
696 
697 	str = strdup(pmu_name);
698 	if (!str)
699 		return false;
700 
701 	/*
702 	 * uncore alias may be from different PMU with common prefix
703 	 */
704 	tok = strtok_r(str, ",", &tmp);
705 	if (strncmp(pmu_name, tok, strlen(tok))) {
706 		res = false;
707 		goto out;
708 	}
709 
710 	/*
711 	 * Match more complex aliases where the alias name is a comma-delimited
712 	 * list of tokens, orderly contained in the matching PMU name.
713 	 *
714 	 * Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we
715 	 *	    match "socket" in "socketX_pmunameY" and then "pmuname" in
716 	 *	    "pmunameY".
717 	 */
718 	while (1) {
719 		char *next_tok = strtok_r(NULL, ",", &tmp);
720 
721 		name = strstr(name, tok);
722 		if (!name ||
723 		    (!next_tok && !perf_pmu__valid_suffix(name, tok))) {
724 			res = false;
725 			goto out;
726 		}
727 		if (!next_tok)
728 			break;
729 		tok = next_tok;
730 		name += strlen(tok);
731 	}
732 
733 	res = true;
734 out:
735 	free(str);
736 	return res;
737 }
738 
739 struct pmu_add_cpu_aliases_map_data {
740 	struct list_head *head;
741 	const char *name;
742 	const char *cpu_name;
743 	struct perf_pmu *pmu;
744 };
745 
746 static int pmu_add_cpu_aliases_map_callback(const struct pmu_event *pe,
747 					const struct pmu_events_table *table __maybe_unused,
748 					void *vdata)
749 {
750 	struct pmu_add_cpu_aliases_map_data *data = vdata;
751 	const char *pname = pe->pmu ? pe->pmu : data->cpu_name;
752 
753 	if (data->pmu->is_uncore && pmu_uncore_alias_match(pname, data->name))
754 		goto new_alias;
755 
756 	if (strcmp(pname, data->name))
757 		return 0;
758 
759 new_alias:
760 	/* need type casts to override 'const' */
761 	__perf_pmu__new_alias(data->head, NULL, (char *)pe->name, (char *)pe->desc,
762 			      (char *)pe->event, pe);
763 	return 0;
764 }
765 
766 /*
767  * From the pmu_events_map, find the table of PMU events that corresponds
768  * to the current running CPU. Then, add all PMU events from that table
769  * as aliases.
770  */
771 void pmu_add_cpu_aliases_table(struct list_head *head, struct perf_pmu *pmu,
772 			       const struct pmu_events_table *table)
773 {
774 	struct pmu_add_cpu_aliases_map_data data = {
775 		.head = head,
776 		.name = pmu->name,
777 		.cpu_name = is_arm_pmu_core(pmu->name) ? pmu->name : "cpu",
778 		.pmu = pmu,
779 	};
780 
781 	pmu_events_table_for_each_event(table, pmu_add_cpu_aliases_map_callback, &data);
782 }
783 
784 static void pmu_add_cpu_aliases(struct list_head *head, struct perf_pmu *pmu)
785 {
786 	const struct pmu_events_table *table;
787 
788 	table = perf_pmu__find_events_table(pmu);
789 	if (!table)
790 		return;
791 
792 	pmu_add_cpu_aliases_table(head, pmu, table);
793 }
794 
795 struct pmu_sys_event_iter_data {
796 	struct list_head *head;
797 	struct perf_pmu *pmu;
798 };
799 
800 static int pmu_add_sys_aliases_iter_fn(const struct pmu_event *pe,
801 				       const struct pmu_events_table *table __maybe_unused,
802 				       void *data)
803 {
804 	struct pmu_sys_event_iter_data *idata = data;
805 	struct perf_pmu *pmu = idata->pmu;
806 
807 	if (!pe->compat || !pe->pmu)
808 		return 0;
809 
810 	if (!strcmp(pmu->id, pe->compat) &&
811 	    pmu_uncore_alias_match(pe->pmu, pmu->name)) {
812 		__perf_pmu__new_alias(idata->head, NULL,
813 				      (char *)pe->name,
814 				      (char *)pe->desc,
815 				      (char *)pe->event,
816 				      pe);
817 	}
818 
819 	return 0;
820 }
821 
822 void pmu_add_sys_aliases(struct list_head *head, struct perf_pmu *pmu)
823 {
824 	struct pmu_sys_event_iter_data idata = {
825 		.head = head,
826 		.pmu = pmu,
827 	};
828 
829 	if (!pmu->id)
830 		return;
831 
832 	pmu_for_each_sys_event(pmu_add_sys_aliases_iter_fn, &idata);
833 }
834 
835 struct perf_event_attr * __weak
836 perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
837 {
838 	return NULL;
839 }
840 
841 char * __weak
842 pmu_find_real_name(const char *name)
843 {
844 	return (char *)name;
845 }
846 
847 char * __weak
848 pmu_find_alias_name(const char *name __maybe_unused)
849 {
850 	return NULL;
851 }
852 
853 static int pmu_max_precise(struct perf_pmu *pmu)
854 {
855 	int max_precise = -1;
856 
857 	perf_pmu__scan_file(pmu, "caps/max_precise", "%d", &max_precise);
858 	return max_precise;
859 }
860 
861 static struct perf_pmu *pmu_lookup(const char *lookup_name)
862 {
863 	struct perf_pmu *pmu;
864 	LIST_HEAD(format);
865 	LIST_HEAD(aliases);
866 	__u32 type;
867 	char *name = pmu_find_real_name(lookup_name);
868 	bool is_hybrid = perf_pmu__hybrid_mounted(name);
869 	char *alias_name;
870 
871 	/*
872 	 * Check pmu name for hybrid and the pmu may be invalid in sysfs
873 	 */
874 	if (!strncmp(name, "cpu_", 4) && !is_hybrid)
875 		return NULL;
876 
877 	/*
878 	 * The pmu data we store & need consists of the pmu
879 	 * type value and format definitions. Load both right
880 	 * now.
881 	 */
882 	if (pmu_format(name, &format))
883 		return NULL;
884 
885 	/*
886 	 * Check the aliases first to avoid unnecessary work.
887 	 */
888 	if (pmu_aliases(name, &aliases))
889 		return NULL;
890 
891 	pmu = zalloc(sizeof(*pmu));
892 	if (!pmu)
893 		return NULL;
894 
895 	pmu->cpus = pmu_cpumask(name);
896 	pmu->name = strdup(name);
897 
898 	if (!pmu->name)
899 		goto err;
900 
901 	/* Read type, and ensure that type value is successfully assigned (return 1) */
902 	if (perf_pmu__scan_file(pmu, "type", "%u", &type) != 1)
903 		goto err;
904 
905 	alias_name = pmu_find_alias_name(name);
906 	if (alias_name) {
907 		pmu->alias_name = strdup(alias_name);
908 		if (!pmu->alias_name)
909 			goto err;
910 	}
911 
912 	pmu->type = type;
913 	pmu->is_uncore = pmu_is_uncore(name);
914 	if (pmu->is_uncore)
915 		pmu->id = pmu_id(name);
916 	pmu->max_precise = pmu_max_precise(pmu);
917 	pmu_add_cpu_aliases(&aliases, pmu);
918 	pmu_add_sys_aliases(&aliases, pmu);
919 
920 	INIT_LIST_HEAD(&pmu->format);
921 	INIT_LIST_HEAD(&pmu->aliases);
922 	INIT_LIST_HEAD(&pmu->caps);
923 	list_splice(&format, &pmu->format);
924 	list_splice(&aliases, &pmu->aliases);
925 	list_add_tail(&pmu->list, &pmus);
926 
927 	if (is_hybrid)
928 		list_add_tail(&pmu->hybrid_list, &perf_pmu__hybrid_pmus);
929 
930 	pmu->default_config = perf_pmu__get_default_config(pmu);
931 
932 	return pmu;
933 err:
934 	if (pmu->name)
935 		free(pmu->name);
936 	free(pmu);
937 	return NULL;
938 }
939 
940 void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu)
941 {
942 	struct perf_pmu_format *format;
943 
944 	/* fake pmu doesn't have format list */
945 	if (pmu == &perf_pmu__fake)
946 		return;
947 
948 	list_for_each_entry(format, &pmu->format, list)
949 		if (format->value >= PERF_PMU_FORMAT_VALUE_CONFIG_END) {
950 			pr_warning("WARNING: '%s' format '%s' requires 'perf_event_attr::config%d'"
951 				   "which is not supported by this version of perf!\n",
952 				   pmu->name, format->name, format->value);
953 			return;
954 		}
955 }
956 
957 static struct perf_pmu *pmu_find(const char *name)
958 {
959 	struct perf_pmu *pmu;
960 
961 	list_for_each_entry(pmu, &pmus, list) {
962 		if (!strcmp(pmu->name, name) ||
963 		    (pmu->alias_name && !strcmp(pmu->alias_name, name)))
964 			return pmu;
965 	}
966 
967 	return NULL;
968 }
969 
970 struct perf_pmu *perf_pmu__find_by_type(unsigned int type)
971 {
972 	struct perf_pmu *pmu;
973 
974 	list_for_each_entry(pmu, &pmus, list)
975 		if (pmu->type == type)
976 			return pmu;
977 
978 	return NULL;
979 }
980 
981 struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
982 {
983 	/*
984 	 * pmu iterator: If pmu is NULL, we start at the begin,
985 	 * otherwise return the next pmu. Returns NULL on end.
986 	 */
987 	if (!pmu) {
988 		pmu_read_sysfs();
989 		pmu = list_prepare_entry(pmu, &pmus, list);
990 	}
991 	list_for_each_entry_continue(pmu, &pmus, list)
992 		return pmu;
993 	return NULL;
994 }
995 
996 struct perf_pmu *evsel__find_pmu(struct evsel *evsel)
997 {
998 	struct perf_pmu *pmu = NULL;
999 
1000 	if (evsel->pmu)
1001 		return evsel->pmu;
1002 
1003 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1004 		if (pmu->type == evsel->core.attr.type)
1005 			break;
1006 	}
1007 
1008 	evsel->pmu = pmu;
1009 	return pmu;
1010 }
1011 
1012 bool evsel__is_aux_event(struct evsel *evsel)
1013 {
1014 	struct perf_pmu *pmu = evsel__find_pmu(evsel);
1015 
1016 	return pmu && pmu->auxtrace;
1017 }
1018 
1019 struct perf_pmu *perf_pmu__find(const char *name)
1020 {
1021 	struct perf_pmu *pmu;
1022 
1023 	/*
1024 	 * Once PMU is loaded it stays in the list,
1025 	 * so we keep us from multiple reading/parsing
1026 	 * the pmu format definitions.
1027 	 */
1028 	pmu = pmu_find(name);
1029 	if (pmu)
1030 		return pmu;
1031 
1032 	return pmu_lookup(name);
1033 }
1034 
1035 static struct perf_pmu_format *
1036 pmu_find_format(struct list_head *formats, const char *name)
1037 {
1038 	struct perf_pmu_format *format;
1039 
1040 	list_for_each_entry(format, formats, list)
1041 		if (!strcmp(format->name, name))
1042 			return format;
1043 
1044 	return NULL;
1045 }
1046 
1047 __u64 perf_pmu__format_bits(struct list_head *formats, const char *name)
1048 {
1049 	struct perf_pmu_format *format = pmu_find_format(formats, name);
1050 	__u64 bits = 0;
1051 	int fbit;
1052 
1053 	if (!format)
1054 		return 0;
1055 
1056 	for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
1057 		bits |= 1ULL << fbit;
1058 
1059 	return bits;
1060 }
1061 
1062 int perf_pmu__format_type(struct list_head *formats, const char *name)
1063 {
1064 	struct perf_pmu_format *format = pmu_find_format(formats, name);
1065 
1066 	if (!format)
1067 		return -1;
1068 
1069 	return format->value;
1070 }
1071 
1072 /*
1073  * Sets value based on the format definition (format parameter)
1074  * and unformatted value (value parameter).
1075  */
1076 static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
1077 			     bool zero)
1078 {
1079 	unsigned long fbit, vbit;
1080 
1081 	for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
1082 
1083 		if (!test_bit(fbit, format))
1084 			continue;
1085 
1086 		if (value & (1llu << vbit++))
1087 			*v |= (1llu << fbit);
1088 		else if (zero)
1089 			*v &= ~(1llu << fbit);
1090 	}
1091 }
1092 
1093 static __u64 pmu_format_max_value(const unsigned long *format)
1094 {
1095 	int w;
1096 
1097 	w = bitmap_weight(format, PERF_PMU_FORMAT_BITS);
1098 	if (!w)
1099 		return 0;
1100 	if (w < 64)
1101 		return (1ULL << w) - 1;
1102 	return -1;
1103 }
1104 
1105 /*
1106  * Term is a string term, and might be a param-term. Try to look up it's value
1107  * in the remaining terms.
1108  * - We have a term like "base-or-format-term=param-term",
1109  * - We need to find the value supplied for "param-term" (with param-term named
1110  *   in a config string) later on in the term list.
1111  */
1112 static int pmu_resolve_param_term(struct parse_events_term *term,
1113 				  struct list_head *head_terms,
1114 				  __u64 *value)
1115 {
1116 	struct parse_events_term *t;
1117 
1118 	list_for_each_entry(t, head_terms, list) {
1119 		if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM &&
1120 		    t->config && !strcmp(t->config, term->config)) {
1121 			t->used = true;
1122 			*value = t->val.num;
1123 			return 0;
1124 		}
1125 	}
1126 
1127 	if (verbose > 0)
1128 		printf("Required parameter '%s' not specified\n", term->config);
1129 
1130 	return -1;
1131 }
1132 
1133 static char *pmu_formats_string(struct list_head *formats)
1134 {
1135 	struct perf_pmu_format *format;
1136 	char *str = NULL;
1137 	struct strbuf buf = STRBUF_INIT;
1138 	unsigned int i = 0;
1139 
1140 	if (!formats)
1141 		return NULL;
1142 
1143 	/* sysfs exported terms */
1144 	list_for_each_entry(format, formats, list)
1145 		if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
1146 			goto error;
1147 
1148 	str = strbuf_detach(&buf, NULL);
1149 error:
1150 	strbuf_release(&buf);
1151 
1152 	return str;
1153 }
1154 
1155 /*
1156  * Setup one of config[12] attr members based on the
1157  * user input data - term parameter.
1158  */
1159 static int pmu_config_term(const char *pmu_name,
1160 			   struct list_head *formats,
1161 			   struct perf_event_attr *attr,
1162 			   struct parse_events_term *term,
1163 			   struct list_head *head_terms,
1164 			   bool zero, struct parse_events_error *err)
1165 {
1166 	struct perf_pmu_format *format;
1167 	__u64 *vp;
1168 	__u64 val, max_val;
1169 
1170 	/*
1171 	 * If this is a parameter we've already used for parameterized-eval,
1172 	 * skip it in normal eval.
1173 	 */
1174 	if (term->used)
1175 		return 0;
1176 
1177 	/*
1178 	 * Hardcoded terms should be already in, so nothing
1179 	 * to be done for them.
1180 	 */
1181 	if (parse_events__is_hardcoded_term(term))
1182 		return 0;
1183 
1184 	format = pmu_find_format(formats, term->config);
1185 	if (!format) {
1186 		char *pmu_term = pmu_formats_string(formats);
1187 		char *unknown_term;
1188 		char *help_msg;
1189 
1190 		if (asprintf(&unknown_term,
1191 				"unknown term '%s' for pmu '%s'",
1192 				term->config, pmu_name) < 0)
1193 			unknown_term = NULL;
1194 		help_msg = parse_events_formats_error_string(pmu_term);
1195 		if (err) {
1196 			parse_events_error__handle(err, term->err_term,
1197 						   unknown_term,
1198 						   help_msg);
1199 		} else {
1200 			pr_debug("%s (%s)\n", unknown_term, help_msg);
1201 			free(unknown_term);
1202 		}
1203 		free(pmu_term);
1204 		return -EINVAL;
1205 	}
1206 
1207 	switch (format->value) {
1208 	case PERF_PMU_FORMAT_VALUE_CONFIG:
1209 		vp = &attr->config;
1210 		break;
1211 	case PERF_PMU_FORMAT_VALUE_CONFIG1:
1212 		vp = &attr->config1;
1213 		break;
1214 	case PERF_PMU_FORMAT_VALUE_CONFIG2:
1215 		vp = &attr->config2;
1216 		break;
1217 	default:
1218 		return -EINVAL;
1219 	}
1220 
1221 	/*
1222 	 * Either directly use a numeric term, or try to translate string terms
1223 	 * using event parameters.
1224 	 */
1225 	if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1226 		if (term->no_value &&
1227 		    bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
1228 			if (err) {
1229 				parse_events_error__handle(err, term->err_val,
1230 					   strdup("no value assigned for term"),
1231 					   NULL);
1232 			}
1233 			return -EINVAL;
1234 		}
1235 
1236 		val = term->val.num;
1237 	} else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1238 		if (strcmp(term->val.str, "?")) {
1239 			if (verbose > 0) {
1240 				pr_info("Invalid sysfs entry %s=%s\n",
1241 						term->config, term->val.str);
1242 			}
1243 			if (err) {
1244 				parse_events_error__handle(err, term->err_val,
1245 					strdup("expected numeric value"),
1246 					NULL);
1247 			}
1248 			return -EINVAL;
1249 		}
1250 
1251 		if (pmu_resolve_param_term(term, head_terms, &val))
1252 			return -EINVAL;
1253 	} else
1254 		return -EINVAL;
1255 
1256 	max_val = pmu_format_max_value(format->bits);
1257 	if (val > max_val) {
1258 		if (err) {
1259 			char *err_str;
1260 
1261 			parse_events_error__handle(err, term->err_val,
1262 				asprintf(&err_str,
1263 				    "value too big for format, maximum is %llu",
1264 				    (unsigned long long)max_val) < 0
1265 				    ? strdup("value too big for format")
1266 				    : err_str,
1267 				    NULL);
1268 			return -EINVAL;
1269 		}
1270 		/*
1271 		 * Assume we don't care if !err, in which case the value will be
1272 		 * silently truncated.
1273 		 */
1274 	}
1275 
1276 	pmu_format_value(format->bits, val, vp, zero);
1277 	return 0;
1278 }
1279 
1280 int perf_pmu__config_terms(const char *pmu_name, struct list_head *formats,
1281 			   struct perf_event_attr *attr,
1282 			   struct list_head *head_terms,
1283 			   bool zero, struct parse_events_error *err)
1284 {
1285 	struct parse_events_term *term;
1286 
1287 	list_for_each_entry(term, head_terms, list) {
1288 		if (pmu_config_term(pmu_name, formats, attr, term, head_terms,
1289 				    zero, err))
1290 			return -EINVAL;
1291 	}
1292 
1293 	return 0;
1294 }
1295 
1296 /*
1297  * Configures event's 'attr' parameter based on the:
1298  * 1) users input - specified in terms parameter
1299  * 2) pmu format definitions - specified by pmu parameter
1300  */
1301 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
1302 		     struct list_head *head_terms,
1303 		     struct parse_events_error *err)
1304 {
1305 	bool zero = !!pmu->default_config;
1306 
1307 	attr->type = pmu->type;
1308 	return perf_pmu__config_terms(pmu->name, &pmu->format, attr,
1309 				      head_terms, zero, err);
1310 }
1311 
1312 static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
1313 					     struct parse_events_term *term)
1314 {
1315 	struct perf_pmu_alias *alias;
1316 	char *name;
1317 
1318 	if (parse_events__is_hardcoded_term(term))
1319 		return NULL;
1320 
1321 	if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1322 		if (term->val.num != 1)
1323 			return NULL;
1324 		if (pmu_find_format(&pmu->format, term->config))
1325 			return NULL;
1326 		name = term->config;
1327 	} else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1328 		if (strcasecmp(term->config, "event"))
1329 			return NULL;
1330 		name = term->val.str;
1331 	} else {
1332 		return NULL;
1333 	}
1334 
1335 	list_for_each_entry(alias, &pmu->aliases, list) {
1336 		if (!strcasecmp(alias->name, name))
1337 			return alias;
1338 	}
1339 	return NULL;
1340 }
1341 
1342 
1343 static int check_info_data(struct perf_pmu_alias *alias,
1344 			   struct perf_pmu_info *info)
1345 {
1346 	/*
1347 	 * Only one term in event definition can
1348 	 * define unit, scale and snapshot, fail
1349 	 * if there's more than one.
1350 	 */
1351 	if ((info->unit && alias->unit[0]) ||
1352 	    (info->scale && alias->scale) ||
1353 	    (info->snapshot && alias->snapshot))
1354 		return -EINVAL;
1355 
1356 	if (alias->unit[0])
1357 		info->unit = alias->unit;
1358 
1359 	if (alias->scale)
1360 		info->scale = alias->scale;
1361 
1362 	if (alias->snapshot)
1363 		info->snapshot = alias->snapshot;
1364 
1365 	return 0;
1366 }
1367 
1368 /*
1369  * Find alias in the terms list and replace it with the terms
1370  * defined for the alias
1371  */
1372 int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
1373 			  struct perf_pmu_info *info)
1374 {
1375 	struct parse_events_term *term, *h;
1376 	struct perf_pmu_alias *alias;
1377 	int ret;
1378 
1379 	info->per_pkg = false;
1380 
1381 	/*
1382 	 * Mark unit and scale as not set
1383 	 * (different from default values, see below)
1384 	 */
1385 	info->unit     = NULL;
1386 	info->scale    = 0.0;
1387 	info->snapshot = false;
1388 
1389 	list_for_each_entry_safe(term, h, head_terms, list) {
1390 		alias = pmu_find_alias(pmu, term);
1391 		if (!alias)
1392 			continue;
1393 		ret = pmu_alias_terms(alias, &term->list);
1394 		if (ret)
1395 			return ret;
1396 
1397 		ret = check_info_data(alias, info);
1398 		if (ret)
1399 			return ret;
1400 
1401 		if (alias->per_pkg)
1402 			info->per_pkg = true;
1403 
1404 		list_del_init(&term->list);
1405 		parse_events_term__delete(term);
1406 	}
1407 
1408 	/*
1409 	 * if no unit or scale found in aliases, then
1410 	 * set defaults as for evsel
1411 	 * unit cannot left to NULL
1412 	 */
1413 	if (info->unit == NULL)
1414 		info->unit   = "";
1415 
1416 	if (info->scale == 0.0)
1417 		info->scale  = 1.0;
1418 
1419 	return 0;
1420 }
1421 
1422 int perf_pmu__new_format(struct list_head *list, char *name,
1423 			 int config, unsigned long *bits)
1424 {
1425 	struct perf_pmu_format *format;
1426 
1427 	format = zalloc(sizeof(*format));
1428 	if (!format)
1429 		return -ENOMEM;
1430 
1431 	format->name = strdup(name);
1432 	format->value = config;
1433 	memcpy(format->bits, bits, sizeof(format->bits));
1434 
1435 	list_add_tail(&format->list, list);
1436 	return 0;
1437 }
1438 
1439 void perf_pmu__set_format(unsigned long *bits, long from, long to)
1440 {
1441 	long b;
1442 
1443 	if (!to)
1444 		to = from;
1445 
1446 	memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
1447 	for (b = from; b <= to; b++)
1448 		__set_bit(b, bits);
1449 }
1450 
1451 void perf_pmu__del_formats(struct list_head *formats)
1452 {
1453 	struct perf_pmu_format *fmt, *tmp;
1454 
1455 	list_for_each_entry_safe(fmt, tmp, formats, list) {
1456 		list_del(&fmt->list);
1457 		free(fmt->name);
1458 		free(fmt);
1459 	}
1460 }
1461 
1462 static int sub_non_neg(int a, int b)
1463 {
1464 	if (b > a)
1465 		return 0;
1466 	return a - b;
1467 }
1468 
1469 static char *format_alias(char *buf, int len, const struct perf_pmu *pmu,
1470 			  const struct perf_pmu_alias *alias)
1471 {
1472 	struct parse_events_term *term;
1473 	int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
1474 
1475 	list_for_each_entry(term, &alias->terms, list) {
1476 		if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1477 			used += snprintf(buf + used, sub_non_neg(len, used),
1478 					",%s=%s", term->config,
1479 					term->val.str);
1480 	}
1481 
1482 	if (sub_non_neg(len, used) > 0) {
1483 		buf[used] = '/';
1484 		used++;
1485 	}
1486 	if (sub_non_neg(len, used) > 0) {
1487 		buf[used] = '\0';
1488 		used++;
1489 	} else
1490 		buf[len - 1] = '\0';
1491 
1492 	return buf;
1493 }
1494 
1495 /** Struct for ordering events as output in perf list. */
1496 struct sevent {
1497 	/** PMU for event. */
1498 	const struct perf_pmu *pmu;
1499 	/**
1500 	 * Optional event for name, desc, etc. If not present then this is a
1501 	 * selectable PMU and the event name is shown as "//".
1502 	 */
1503 	const struct perf_pmu_alias *event;
1504 	/** Is the PMU for the CPU? */
1505 	bool is_cpu;
1506 };
1507 
1508 static int cmp_sevent(const void *a, const void *b)
1509 {
1510 	const struct sevent *as = a;
1511 	const struct sevent *bs = b;
1512 	const char *a_pmu_name, *b_pmu_name;
1513 	const char *a_name = "//", *a_desc = NULL, *a_topic = "";
1514 	const char *b_name = "//", *b_desc = NULL, *b_topic = "";
1515 	int ret;
1516 
1517 	if (as->event) {
1518 		a_name = as->event->name;
1519 		a_desc = as->event->desc;
1520 		a_topic = as->event->topic ?: "";
1521 	}
1522 	if (bs->event) {
1523 		b_name = bs->event->name;
1524 		b_desc = bs->event->desc;
1525 		b_topic = bs->event->topic ?: "";
1526 	}
1527 	/* Put extra events last. */
1528 	if (!!a_desc != !!b_desc)
1529 		return !!a_desc - !!b_desc;
1530 
1531 	/* Order by topics. */
1532 	ret = strcmp(a_topic, b_topic);
1533 	if (ret)
1534 		return ret;
1535 
1536 	/* Order CPU core events to be first */
1537 	if (as->is_cpu != bs->is_cpu)
1538 		return as->is_cpu ? -1 : 1;
1539 
1540 	/* Order by PMU name. */
1541 	a_pmu_name = as->pmu->name ?: "";
1542 	b_pmu_name = bs->pmu->name ?: "";
1543 	ret = strcmp(a_pmu_name, b_pmu_name);
1544 	if (ret)
1545 		return ret;
1546 
1547 	/* Order by event name. */
1548 	return strcmp(a_name, b_name);
1549 }
1550 
1551 bool is_pmu_core(const char *name)
1552 {
1553 	return !strcmp(name, "cpu") || is_arm_pmu_core(name);
1554 }
1555 
1556 static bool pmu_alias_is_duplicate(struct sevent *alias_a,
1557 				   struct sevent *alias_b)
1558 {
1559 	const char *a_pmu_name, *b_pmu_name;
1560 	const char *a_name = alias_a->event ? alias_a->event->name : "//";
1561 	const char *b_name = alias_b->event ? alias_b->event->name : "//";
1562 
1563 	/* Different names -> never duplicates */
1564 	if (strcmp(a_name, b_name))
1565 		return false;
1566 
1567 	/* Don't remove duplicates for different PMUs */
1568 	a_pmu_name = alias_a->pmu->name ?: "";
1569 	b_pmu_name = alias_b->pmu->name ?: "";
1570 	return strcmp(a_pmu_name, b_pmu_name) == 0;
1571 }
1572 
1573 void print_pmu_events(const struct print_callbacks *print_cb, void *print_state)
1574 {
1575 	struct perf_pmu *pmu;
1576 	struct perf_pmu_alias *event;
1577 	char buf[1024];
1578 	int printed = 0;
1579 	int len, j;
1580 	struct sevent *aliases;
1581 
1582 	pmu = NULL;
1583 	len = 0;
1584 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1585 		list_for_each_entry(event, &pmu->aliases, list)
1586 			len++;
1587 		if (pmu->selectable)
1588 			len++;
1589 	}
1590 	aliases = zalloc(sizeof(struct sevent) * len);
1591 	if (!aliases) {
1592 		pr_err("FATAL: not enough memory to print PMU events\n");
1593 		return;
1594 	}
1595 	pmu = NULL;
1596 	j = 0;
1597 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1598 		bool is_cpu = is_pmu_core(pmu->name) || perf_pmu__is_hybrid(pmu->name);
1599 
1600 		list_for_each_entry(event, &pmu->aliases, list) {
1601 			aliases[j].event = event;
1602 			aliases[j].pmu = pmu;
1603 			aliases[j].is_cpu = is_cpu;
1604 			j++;
1605 		}
1606 		if (pmu->selectable) {
1607 			aliases[j].event = NULL;
1608 			aliases[j].pmu = pmu;
1609 			aliases[j].is_cpu = is_cpu;
1610 			j++;
1611 		}
1612 	}
1613 	len = j;
1614 	qsort(aliases, len, sizeof(struct sevent), cmp_sevent);
1615 	for (j = 0; j < len; j++) {
1616 		const char *name, *alias = NULL, *scale_unit = NULL,
1617 			*desc = NULL, *long_desc = NULL,
1618 			*encoding_desc = NULL, *topic = NULL;
1619 		bool deprecated = false;
1620 		size_t buf_used;
1621 
1622 		/* Skip duplicates */
1623 		if (j > 0 && pmu_alias_is_duplicate(&aliases[j], &aliases[j - 1]))
1624 			continue;
1625 
1626 		if (!aliases[j].event) {
1627 			/* A selectable event. */
1628 			buf_used = snprintf(buf, sizeof(buf), "%s//", aliases[j].pmu->name) + 1;
1629 			name = buf;
1630 		} else {
1631 			if (aliases[j].event->desc) {
1632 				name = aliases[j].event->name;
1633 				buf_used = 0;
1634 			} else {
1635 				name = format_alias(buf, sizeof(buf), aliases[j].pmu,
1636 						    aliases[j].event);
1637 				if (aliases[j].is_cpu) {
1638 					alias = name;
1639 					name = aliases[j].event->name;
1640 				}
1641 				buf_used = strlen(buf) + 1;
1642 			}
1643 			if (strlen(aliases[j].event->unit) || aliases[j].event->scale != 1.0) {
1644 				scale_unit = buf + buf_used;
1645 				buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used,
1646 						"%G%s", aliases[j].event->scale,
1647 						aliases[j].event->unit) + 1;
1648 			}
1649 			desc = aliases[j].event->desc;
1650 			long_desc = aliases[j].event->long_desc;
1651 			topic = aliases[j].event->topic;
1652 			encoding_desc = buf + buf_used;
1653 			buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used,
1654 					"%s/%s/", aliases[j].pmu->name,
1655 					aliases[j].event->str) + 1;
1656 			deprecated = aliases[j].event->deprecated;
1657 		}
1658 		print_cb->print_event(print_state,
1659 				aliases[j].pmu->name,
1660 				topic,
1661 				name,
1662 				alias,
1663 				scale_unit,
1664 				deprecated,
1665 				"Kernel PMU event",
1666 				desc,
1667 				long_desc,
1668 				encoding_desc);
1669 	}
1670 	if (printed && pager_in_use())
1671 		printf("\n");
1672 
1673 	zfree(&aliases);
1674 	return;
1675 }
1676 
1677 bool pmu_have_event(const char *pname, const char *name)
1678 {
1679 	struct perf_pmu *pmu;
1680 	struct perf_pmu_alias *alias;
1681 
1682 	pmu = NULL;
1683 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1684 		if (strcmp(pname, pmu->name))
1685 			continue;
1686 		list_for_each_entry(alias, &pmu->aliases, list)
1687 			if (!strcmp(alias->name, name))
1688 				return true;
1689 	}
1690 	return false;
1691 }
1692 
1693 FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
1694 {
1695 	char path[PATH_MAX];
1696 
1697 	if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name) ||
1698 	    !file_available(path))
1699 		return NULL;
1700 
1701 	return fopen(path, "r");
1702 }
1703 
1704 int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
1705 			...)
1706 {
1707 	va_list args;
1708 	FILE *file;
1709 	int ret = EOF;
1710 
1711 	va_start(args, fmt);
1712 	file = perf_pmu__open_file(pmu, name);
1713 	if (file) {
1714 		ret = vfscanf(file, fmt, args);
1715 		fclose(file);
1716 	}
1717 	va_end(args);
1718 	return ret;
1719 }
1720 
1721 bool perf_pmu__file_exists(struct perf_pmu *pmu, const char *name)
1722 {
1723 	char path[PATH_MAX];
1724 
1725 	if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name))
1726 		return false;
1727 
1728 	return file_available(path);
1729 }
1730 
1731 static int perf_pmu__new_caps(struct list_head *list, char *name, char *value)
1732 {
1733 	struct perf_pmu_caps *caps = zalloc(sizeof(*caps));
1734 
1735 	if (!caps)
1736 		return -ENOMEM;
1737 
1738 	caps->name = strdup(name);
1739 	if (!caps->name)
1740 		goto free_caps;
1741 	caps->value = strndup(value, strlen(value) - 1);
1742 	if (!caps->value)
1743 		goto free_name;
1744 	list_add_tail(&caps->list, list);
1745 	return 0;
1746 
1747 free_name:
1748 	zfree(caps->name);
1749 free_caps:
1750 	free(caps);
1751 
1752 	return -ENOMEM;
1753 }
1754 
1755 /*
1756  * Reading/parsing the given pmu capabilities, which should be located at:
1757  * /sys/bus/event_source/devices/<dev>/caps as sysfs group attributes.
1758  * Return the number of capabilities
1759  */
1760 int perf_pmu__caps_parse(struct perf_pmu *pmu)
1761 {
1762 	struct stat st;
1763 	char caps_path[PATH_MAX];
1764 	DIR *caps_dir;
1765 	struct dirent *evt_ent;
1766 
1767 	if (pmu->caps_initialized)
1768 		return pmu->nr_caps;
1769 
1770 	pmu->nr_caps = 0;
1771 
1772 	if (!perf_pmu__pathname_scnprintf(caps_path, sizeof(caps_path), pmu->name, "caps"))
1773 		return -1;
1774 
1775 	if (stat(caps_path, &st) < 0) {
1776 		pmu->caps_initialized = true;
1777 		return 0;	/* no error if caps does not exist */
1778 	}
1779 
1780 	caps_dir = opendir(caps_path);
1781 	if (!caps_dir)
1782 		return -EINVAL;
1783 
1784 	while ((evt_ent = readdir(caps_dir)) != NULL) {
1785 		char path[PATH_MAX + NAME_MAX + 1];
1786 		char *name = evt_ent->d_name;
1787 		char value[128];
1788 		FILE *file;
1789 
1790 		if (!strcmp(name, ".") || !strcmp(name, ".."))
1791 			continue;
1792 
1793 		snprintf(path, sizeof(path), "%s/%s", caps_path, name);
1794 
1795 		file = fopen(path, "r");
1796 		if (!file)
1797 			continue;
1798 
1799 		if (!fgets(value, sizeof(value), file) ||
1800 		    (perf_pmu__new_caps(&pmu->caps, name, value) < 0)) {
1801 			fclose(file);
1802 			continue;
1803 		}
1804 
1805 		pmu->nr_caps++;
1806 		fclose(file);
1807 	}
1808 
1809 	closedir(caps_dir);
1810 
1811 	pmu->caps_initialized = true;
1812 	return pmu->nr_caps;
1813 }
1814 
1815 void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config,
1816 				   const char *name)
1817 {
1818 	struct perf_pmu_format *format;
1819 	__u64 masks = 0, bits;
1820 	char buf[100];
1821 	unsigned int i;
1822 
1823 	list_for_each_entry(format, &pmu->format, list)	{
1824 		if (format->value != PERF_PMU_FORMAT_VALUE_CONFIG)
1825 			continue;
1826 
1827 		for_each_set_bit(i, format->bits, PERF_PMU_FORMAT_BITS)
1828 			masks |= 1ULL << i;
1829 	}
1830 
1831 	/*
1832 	 * Kernel doesn't export any valid format bits.
1833 	 */
1834 	if (masks == 0)
1835 		return;
1836 
1837 	bits = config & ~masks;
1838 	if (bits == 0)
1839 		return;
1840 
1841 	bitmap_scnprintf((unsigned long *)&bits, sizeof(bits) * 8, buf, sizeof(buf));
1842 
1843 	pr_warning("WARNING: event '%s' not valid (bits %s of config "
1844 		   "'%llx' not supported by kernel)!\n",
1845 		   name ?: "N/A", buf, config);
1846 }
1847 
1848 bool perf_pmu__has_hybrid(void)
1849 {
1850 	if (!hybrid_scanned) {
1851 		hybrid_scanned = true;
1852 		perf_pmu__scan(NULL);
1853 	}
1854 
1855 	return !list_empty(&perf_pmu__hybrid_pmus);
1856 }
1857 
1858 int perf_pmu__match(char *pattern, char *name, char *tok)
1859 {
1860 	if (!name)
1861 		return -1;
1862 
1863 	if (fnmatch(pattern, name, 0))
1864 		return -1;
1865 
1866 	if (tok && !perf_pmu__valid_suffix(name, tok))
1867 		return -1;
1868 
1869 	return 0;
1870 }
1871 
1872 int perf_pmu__cpus_match(struct perf_pmu *pmu, struct perf_cpu_map *cpus,
1873 			 struct perf_cpu_map **mcpus_ptr,
1874 			 struct perf_cpu_map **ucpus_ptr)
1875 {
1876 	struct perf_cpu_map *pmu_cpus = pmu->cpus;
1877 	struct perf_cpu_map *matched_cpus, *unmatched_cpus;
1878 	struct perf_cpu cpu;
1879 	int i, matched_nr = 0, unmatched_nr = 0;
1880 
1881 	matched_cpus = perf_cpu_map__default_new();
1882 	if (!matched_cpus)
1883 		return -1;
1884 
1885 	unmatched_cpus = perf_cpu_map__default_new();
1886 	if (!unmatched_cpus) {
1887 		perf_cpu_map__put(matched_cpus);
1888 		return -1;
1889 	}
1890 
1891 	perf_cpu_map__for_each_cpu(cpu, i, cpus) {
1892 		if (!perf_cpu_map__has(pmu_cpus, cpu))
1893 			unmatched_cpus->map[unmatched_nr++] = cpu;
1894 		else
1895 			matched_cpus->map[matched_nr++] = cpu;
1896 	}
1897 
1898 	unmatched_cpus->nr = unmatched_nr;
1899 	matched_cpus->nr = matched_nr;
1900 	*mcpus_ptr = matched_cpus;
1901 	*ucpus_ptr = unmatched_cpus;
1902 	return 0;
1903 }
1904 
1905 double __weak perf_pmu__cpu_slots_per_cycle(void)
1906 {
1907 	return NAN;
1908 }
1909 
1910 int perf_pmu__event_source_devices_scnprintf(char *pathname, size_t size)
1911 {
1912 	const char *sysfs = sysfs__mountpoint();
1913 
1914 	if (!sysfs)
1915 		return 0;
1916 	return scnprintf(pathname, size, "%s/bus/event_source/devices/", sysfs);
1917 }
1918 
1919 /*
1920  * Fill 'buf' with the path to a file or folder in 'pmu_name' in
1921  * sysfs. For example if pmu_name = "cs_etm" and 'filename' = "format"
1922  * then pathname will be filled with
1923  * "/sys/bus/event_source/devices/cs_etm/format"
1924  *
1925  * Return 0 if the sysfs mountpoint couldn't be found or if no
1926  * characters were written.
1927  */
1928 int perf_pmu__pathname_scnprintf(char *buf, size_t size,
1929 				 const char *pmu_name, const char *filename)
1930 {
1931 	char base_path[PATH_MAX];
1932 
1933 	if (!perf_pmu__event_source_devices_scnprintf(base_path, sizeof(base_path)))
1934 		return 0;
1935 	return scnprintf(buf, size, "%s%s/%s", base_path, pmu_name, filename);
1936 }
1937