xref: /openbmc/linux/tools/perf/util/pmu.c (revision bd4af432)
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 <subcmd/pager.h>
7 #include <sys/types.h>
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <sys/stat.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <stdbool.h>
14 #include <stdarg.h>
15 #include <dirent.h>
16 #include <api/fs/fs.h>
17 #include <locale.h>
18 #include <regex.h>
19 #include <perf/cpumap.h>
20 #include "debug.h"
21 #include "pmu.h"
22 #include "parse-events.h"
23 #include "header.h"
24 #include "string2.h"
25 #include "strbuf.h"
26 #include "fncache.h"
27 
28 struct perf_pmu_format {
29 	char *name;
30 	int value;
31 	DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
32 	struct list_head list;
33 };
34 
35 int perf_pmu_parse(struct list_head *list, char *name);
36 extern FILE *perf_pmu_in;
37 
38 static LIST_HEAD(pmus);
39 
40 /*
41  * Parse & process all the sysfs attributes located under
42  * the directory specified in 'dir' parameter.
43  */
44 int perf_pmu__format_parse(char *dir, struct list_head *head)
45 {
46 	struct dirent *evt_ent;
47 	DIR *format_dir;
48 	int ret = 0;
49 
50 	format_dir = opendir(dir);
51 	if (!format_dir)
52 		return -EINVAL;
53 
54 	while (!ret && (evt_ent = readdir(format_dir))) {
55 		char path[PATH_MAX];
56 		char *name = evt_ent->d_name;
57 		FILE *file;
58 
59 		if (!strcmp(name, ".") || !strcmp(name, ".."))
60 			continue;
61 
62 		snprintf(path, PATH_MAX, "%s/%s", dir, name);
63 
64 		ret = -EINVAL;
65 		file = fopen(path, "r");
66 		if (!file)
67 			break;
68 
69 		perf_pmu_in = file;
70 		ret = perf_pmu_parse(head, name);
71 		fclose(file);
72 	}
73 
74 	closedir(format_dir);
75 	return ret;
76 }
77 
78 /*
79  * Reading/parsing the default pmu format definition, which should be
80  * located at:
81  * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
82  */
83 static int pmu_format(const char *name, struct list_head *format)
84 {
85 	char path[PATH_MAX];
86 	const char *sysfs = sysfs__mountpoint();
87 
88 	if (!sysfs)
89 		return -1;
90 
91 	snprintf(path, PATH_MAX,
92 		 "%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name);
93 
94 	if (!file_available(path))
95 		return 0;
96 
97 	if (perf_pmu__format_parse(path, format))
98 		return -1;
99 
100 	return 0;
101 }
102 
103 int perf_pmu__convert_scale(const char *scale, char **end, double *sval)
104 {
105 	char *lc;
106 	int ret = 0;
107 
108 	/*
109 	 * save current locale
110 	 */
111 	lc = setlocale(LC_NUMERIC, NULL);
112 
113 	/*
114 	 * The lc string may be allocated in static storage,
115 	 * so get a dynamic copy to make it survive setlocale
116 	 * call below.
117 	 */
118 	lc = strdup(lc);
119 	if (!lc) {
120 		ret = -ENOMEM;
121 		goto out;
122 	}
123 
124 	/*
125 	 * force to C locale to ensure kernel
126 	 * scale string is converted correctly.
127 	 * kernel uses default C locale.
128 	 */
129 	setlocale(LC_NUMERIC, "C");
130 
131 	*sval = strtod(scale, end);
132 
133 out:
134 	/* restore locale */
135 	setlocale(LC_NUMERIC, lc);
136 	free(lc);
137 	return ret;
138 }
139 
140 static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name)
141 {
142 	struct stat st;
143 	ssize_t sret;
144 	char scale[128];
145 	int fd, ret = -1;
146 	char path[PATH_MAX];
147 
148 	scnprintf(path, PATH_MAX, "%s/%s.scale", dir, name);
149 
150 	fd = open(path, O_RDONLY);
151 	if (fd == -1)
152 		return -1;
153 
154 	if (fstat(fd, &st) < 0)
155 		goto error;
156 
157 	sret = read(fd, scale, sizeof(scale)-1);
158 	if (sret < 0)
159 		goto error;
160 
161 	if (scale[sret - 1] == '\n')
162 		scale[sret - 1] = '\0';
163 	else
164 		scale[sret] = '\0';
165 
166 	ret = perf_pmu__convert_scale(scale, NULL, &alias->scale);
167 error:
168 	close(fd);
169 	return ret;
170 }
171 
172 static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name)
173 {
174 	char path[PATH_MAX];
175 	ssize_t sret;
176 	int fd;
177 
178 	scnprintf(path, PATH_MAX, "%s/%s.unit", dir, name);
179 
180 	fd = open(path, O_RDONLY);
181 	if (fd == -1)
182 		return -1;
183 
184 	sret = read(fd, alias->unit, UNIT_MAX_LEN);
185 	if (sret < 0)
186 		goto error;
187 
188 	close(fd);
189 
190 	if (alias->unit[sret - 1] == '\n')
191 		alias->unit[sret - 1] = '\0';
192 	else
193 		alias->unit[sret] = '\0';
194 
195 	return 0;
196 error:
197 	close(fd);
198 	alias->unit[0] = '\0';
199 	return -1;
200 }
201 
202 static int
203 perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *name)
204 {
205 	char path[PATH_MAX];
206 	int fd;
207 
208 	scnprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name);
209 
210 	fd = open(path, O_RDONLY);
211 	if (fd == -1)
212 		return -1;
213 
214 	close(fd);
215 
216 	alias->per_pkg = true;
217 	return 0;
218 }
219 
220 static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias,
221 				    char *dir, char *name)
222 {
223 	char path[PATH_MAX];
224 	int fd;
225 
226 	scnprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name);
227 
228 	fd = open(path, O_RDONLY);
229 	if (fd == -1)
230 		return -1;
231 
232 	alias->snapshot = true;
233 	close(fd);
234 	return 0;
235 }
236 
237 static void perf_pmu_assign_str(char *name, const char *field, char **old_str,
238 				char **new_str)
239 {
240 	if (!*old_str)
241 		goto set_new;
242 
243 	if (*new_str) {	/* Have new string, check with old */
244 		if (strcasecmp(*old_str, *new_str))
245 			pr_debug("alias %s differs in field '%s'\n",
246 				 name, field);
247 		zfree(old_str);
248 	} else		/* Nothing new --> keep old string */
249 		return;
250 set_new:
251 	*old_str = *new_str;
252 	*new_str = NULL;
253 }
254 
255 static void perf_pmu_update_alias(struct perf_pmu_alias *old,
256 				  struct perf_pmu_alias *newalias)
257 {
258 	perf_pmu_assign_str(old->name, "desc", &old->desc, &newalias->desc);
259 	perf_pmu_assign_str(old->name, "long_desc", &old->long_desc,
260 			    &newalias->long_desc);
261 	perf_pmu_assign_str(old->name, "topic", &old->topic, &newalias->topic);
262 	perf_pmu_assign_str(old->name, "metric_expr", &old->metric_expr,
263 			    &newalias->metric_expr);
264 	perf_pmu_assign_str(old->name, "metric_name", &old->metric_name,
265 			    &newalias->metric_name);
266 	perf_pmu_assign_str(old->name, "value", &old->str, &newalias->str);
267 	old->scale = newalias->scale;
268 	old->per_pkg = newalias->per_pkg;
269 	old->snapshot = newalias->snapshot;
270 	memcpy(old->unit, newalias->unit, sizeof(old->unit));
271 }
272 
273 /* Delete an alias entry. */
274 static void perf_pmu_free_alias(struct perf_pmu_alias *newalias)
275 {
276 	zfree(&newalias->name);
277 	zfree(&newalias->desc);
278 	zfree(&newalias->long_desc);
279 	zfree(&newalias->topic);
280 	zfree(&newalias->str);
281 	zfree(&newalias->metric_expr);
282 	zfree(&newalias->metric_name);
283 	parse_events_terms__purge(&newalias->terms);
284 	free(newalias);
285 }
286 
287 /* Merge an alias, search in alias list. If this name is already
288  * present merge both of them to combine all information.
289  */
290 static bool perf_pmu_merge_alias(struct perf_pmu_alias *newalias,
291 				 struct list_head *alist)
292 {
293 	struct perf_pmu_alias *a;
294 
295 	list_for_each_entry(a, alist, list) {
296 		if (!strcasecmp(newalias->name, a->name)) {
297 			perf_pmu_update_alias(a, newalias);
298 			perf_pmu_free_alias(newalias);
299 			return true;
300 		}
301 	}
302 	return false;
303 }
304 
305 static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name,
306 				 char *desc, char *val,
307 				 char *long_desc, char *topic,
308 				 char *unit, char *perpkg,
309 				 char *metric_expr,
310 				 char *metric_name,
311 				 char *deprecated)
312 {
313 	struct parse_events_term *term;
314 	struct perf_pmu_alias *alias;
315 	int ret;
316 	int num;
317 	char newval[256];
318 
319 	alias = malloc(sizeof(*alias));
320 	if (!alias)
321 		return -ENOMEM;
322 
323 	INIT_LIST_HEAD(&alias->terms);
324 	alias->scale = 1.0;
325 	alias->unit[0] = '\0';
326 	alias->per_pkg = false;
327 	alias->snapshot = false;
328 	alias->deprecated = false;
329 
330 	ret = parse_events_terms(&alias->terms, val);
331 	if (ret) {
332 		pr_err("Cannot parse alias %s: %d\n", val, ret);
333 		free(alias);
334 		return ret;
335 	}
336 
337 	/* Scan event and remove leading zeroes, spaces, newlines, some
338 	 * platforms have terms specified as
339 	 * event=0x0091 (read from files ../<PMU>/events/<FILE>
340 	 * and terms specified as event=0x91 (read from JSON files).
341 	 *
342 	 * Rebuild string to make alias->str member comparable.
343 	 */
344 	memset(newval, 0, sizeof(newval));
345 	ret = 0;
346 	list_for_each_entry(term, &alias->terms, list) {
347 		if (ret)
348 			ret += scnprintf(newval + ret, sizeof(newval) - ret,
349 					 ",");
350 		if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
351 			ret += scnprintf(newval + ret, sizeof(newval) - ret,
352 					 "%s=%#x", term->config, term->val.num);
353 		else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
354 			ret += scnprintf(newval + ret, sizeof(newval) - ret,
355 					 "%s=%s", term->config, term->val.str);
356 	}
357 
358 	alias->name = strdup(name);
359 	if (dir) {
360 		/*
361 		 * load unit name and scale if available
362 		 */
363 		perf_pmu__parse_unit(alias, dir, name);
364 		perf_pmu__parse_scale(alias, dir, name);
365 		perf_pmu__parse_per_pkg(alias, dir, name);
366 		perf_pmu__parse_snapshot(alias, dir, name);
367 	}
368 
369 	alias->metric_expr = metric_expr ? strdup(metric_expr) : NULL;
370 	alias->metric_name = metric_name ? strdup(metric_name): NULL;
371 	alias->desc = desc ? strdup(desc) : NULL;
372 	alias->long_desc = long_desc ? strdup(long_desc) :
373 				desc ? strdup(desc) : NULL;
374 	alias->topic = topic ? strdup(topic) : NULL;
375 	if (unit) {
376 		if (perf_pmu__convert_scale(unit, &unit, &alias->scale) < 0)
377 			return -1;
378 		snprintf(alias->unit, sizeof(alias->unit), "%s", unit);
379 	}
380 	alias->per_pkg = perpkg && sscanf(perpkg, "%d", &num) == 1 && num == 1;
381 	alias->str = strdup(newval);
382 
383 	if (deprecated)
384 		alias->deprecated = true;
385 
386 	if (!perf_pmu_merge_alias(alias, list))
387 		list_add_tail(&alias->list, list);
388 
389 	return 0;
390 }
391 
392 static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file)
393 {
394 	char buf[256];
395 	int ret;
396 
397 	ret = fread(buf, 1, sizeof(buf), file);
398 	if (ret == 0)
399 		return -EINVAL;
400 
401 	buf[ret] = 0;
402 
403 	/* Remove trailing newline from sysfs file */
404 	strim(buf);
405 
406 	return __perf_pmu__new_alias(list, dir, name, NULL, buf, NULL, NULL, NULL,
407 				     NULL, NULL, NULL, NULL);
408 }
409 
410 static inline bool pmu_alias_info_file(char *name)
411 {
412 	size_t len;
413 
414 	len = strlen(name);
415 	if (len > 5 && !strcmp(name + len - 5, ".unit"))
416 		return true;
417 	if (len > 6 && !strcmp(name + len - 6, ".scale"))
418 		return true;
419 	if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
420 		return true;
421 	if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
422 		return true;
423 
424 	return false;
425 }
426 
427 /*
428  * Process all the sysfs attributes located under the directory
429  * specified in 'dir' parameter.
430  */
431 static int pmu_aliases_parse(char *dir, struct list_head *head)
432 {
433 	struct dirent *evt_ent;
434 	DIR *event_dir;
435 
436 	event_dir = opendir(dir);
437 	if (!event_dir)
438 		return -EINVAL;
439 
440 	while ((evt_ent = readdir(event_dir))) {
441 		char path[PATH_MAX];
442 		char *name = evt_ent->d_name;
443 		FILE *file;
444 
445 		if (!strcmp(name, ".") || !strcmp(name, ".."))
446 			continue;
447 
448 		/*
449 		 * skip info files parsed in perf_pmu__new_alias()
450 		 */
451 		if (pmu_alias_info_file(name))
452 			continue;
453 
454 		scnprintf(path, PATH_MAX, "%s/%s", dir, name);
455 
456 		file = fopen(path, "r");
457 		if (!file) {
458 			pr_debug("Cannot open %s\n", path);
459 			continue;
460 		}
461 
462 		if (perf_pmu__new_alias(head, dir, name, file) < 0)
463 			pr_debug("Cannot set up %s\n", name);
464 		fclose(file);
465 	}
466 
467 	closedir(event_dir);
468 	return 0;
469 }
470 
471 /*
472  * Reading the pmu event aliases definition, which should be located at:
473  * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
474  */
475 static int pmu_aliases(const char *name, struct list_head *head)
476 {
477 	char path[PATH_MAX];
478 	const char *sysfs = sysfs__mountpoint();
479 
480 	if (!sysfs)
481 		return -1;
482 
483 	snprintf(path, PATH_MAX,
484 		 "%s/bus/event_source/devices/%s/events", sysfs, name);
485 
486 	if (!file_available(path))
487 		return 0;
488 
489 	if (pmu_aliases_parse(path, head))
490 		return -1;
491 
492 	return 0;
493 }
494 
495 static int pmu_alias_terms(struct perf_pmu_alias *alias,
496 			   struct list_head *terms)
497 {
498 	struct parse_events_term *term, *cloned;
499 	LIST_HEAD(list);
500 	int ret;
501 
502 	list_for_each_entry(term, &alias->terms, list) {
503 		ret = parse_events_term__clone(&cloned, term);
504 		if (ret) {
505 			parse_events_terms__purge(&list);
506 			return ret;
507 		}
508 		/*
509 		 * Weak terms don't override command line options,
510 		 * which we don't want for implicit terms in aliases.
511 		 */
512 		cloned->weak = true;
513 		list_add_tail(&cloned->list, &list);
514 	}
515 	list_splice(&list, terms);
516 	return 0;
517 }
518 
519 /*
520  * Reading/parsing the default pmu type value, which should be
521  * located at:
522  * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
523  */
524 static int pmu_type(const char *name, __u32 *type)
525 {
526 	char path[PATH_MAX];
527 	FILE *file;
528 	int ret = 0;
529 	const char *sysfs = sysfs__mountpoint();
530 
531 	if (!sysfs)
532 		return -1;
533 
534 	snprintf(path, PATH_MAX,
535 		 "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name);
536 
537 	if (access(path, R_OK) < 0)
538 		return -1;
539 
540 	file = fopen(path, "r");
541 	if (!file)
542 		return -EINVAL;
543 
544 	if (1 != fscanf(file, "%u", type))
545 		ret = -1;
546 
547 	fclose(file);
548 	return ret;
549 }
550 
551 /* Add all pmus in sysfs to pmu list: */
552 static void pmu_read_sysfs(void)
553 {
554 	char path[PATH_MAX];
555 	DIR *dir;
556 	struct dirent *dent;
557 	const char *sysfs = sysfs__mountpoint();
558 
559 	if (!sysfs)
560 		return;
561 
562 	snprintf(path, PATH_MAX,
563 		 "%s" EVENT_SOURCE_DEVICE_PATH, sysfs);
564 
565 	dir = opendir(path);
566 	if (!dir)
567 		return;
568 
569 	while ((dent = readdir(dir))) {
570 		if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
571 			continue;
572 		/* add to static LIST_HEAD(pmus): */
573 		perf_pmu__find(dent->d_name);
574 	}
575 
576 	closedir(dir);
577 }
578 
579 static struct perf_cpu_map *__pmu_cpumask(const char *path)
580 {
581 	FILE *file;
582 	struct perf_cpu_map *cpus;
583 
584 	file = fopen(path, "r");
585 	if (!file)
586 		return NULL;
587 
588 	cpus = perf_cpu_map__read(file);
589 	fclose(file);
590 	return cpus;
591 }
592 
593 /*
594  * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64)
595  * may have a "cpus" file.
596  */
597 #define CPUS_TEMPLATE_UNCORE	"%s/bus/event_source/devices/%s/cpumask"
598 #define CPUS_TEMPLATE_CPU	"%s/bus/event_source/devices/%s/cpus"
599 
600 static struct perf_cpu_map *pmu_cpumask(const char *name)
601 {
602 	char path[PATH_MAX];
603 	struct perf_cpu_map *cpus;
604 	const char *sysfs = sysfs__mountpoint();
605 	const char *templates[] = {
606 		CPUS_TEMPLATE_UNCORE,
607 		CPUS_TEMPLATE_CPU,
608 		NULL
609 	};
610 	const char **template;
611 
612 	if (!sysfs)
613 		return NULL;
614 
615 	for (template = templates; *template; template++) {
616 		snprintf(path, PATH_MAX, *template, sysfs, name);
617 		cpus = __pmu_cpumask(path);
618 		if (cpus)
619 			return cpus;
620 	}
621 
622 	return NULL;
623 }
624 
625 static bool pmu_is_uncore(const char *name)
626 {
627 	char path[PATH_MAX];
628 	const char *sysfs;
629 
630 	sysfs = sysfs__mountpoint();
631 	snprintf(path, PATH_MAX, CPUS_TEMPLATE_UNCORE, sysfs, name);
632 	return file_available(path);
633 }
634 
635 /*
636  *  PMU CORE devices have different name other than cpu in sysfs on some
637  *  platforms.
638  *  Looking for possible sysfs files to identify the arm core device.
639  */
640 static int is_arm_pmu_core(const char *name)
641 {
642 	char path[PATH_MAX];
643 	const char *sysfs = sysfs__mountpoint();
644 
645 	if (!sysfs)
646 		return 0;
647 
648 	/* Look for cpu sysfs (specific to arm) */
649 	scnprintf(path, PATH_MAX, "%s/bus/event_source/devices/%s/cpus",
650 				sysfs, name);
651 	return file_available(path);
652 }
653 
654 static char *perf_pmu__getcpuid(struct perf_pmu *pmu)
655 {
656 	char *cpuid;
657 	static bool printed;
658 
659 	cpuid = getenv("PERF_CPUID");
660 	if (cpuid)
661 		cpuid = strdup(cpuid);
662 	if (!cpuid)
663 		cpuid = get_cpuid_str(pmu);
664 	if (!cpuid)
665 		return NULL;
666 
667 	if (!printed) {
668 		pr_debug("Using CPUID %s\n", cpuid);
669 		printed = true;
670 	}
671 	return cpuid;
672 }
673 
674 struct pmu_events_map *perf_pmu__find_map(struct perf_pmu *pmu)
675 {
676 	struct pmu_events_map *map;
677 	char *cpuid = perf_pmu__getcpuid(pmu);
678 	int i;
679 
680 	/* on some platforms which uses cpus map, cpuid can be NULL for
681 	 * PMUs other than CORE PMUs.
682 	 */
683 	if (!cpuid)
684 		return NULL;
685 
686 	i = 0;
687 	for (;;) {
688 		map = &pmu_events_map[i++];
689 		if (!map->table) {
690 			map = NULL;
691 			break;
692 		}
693 
694 		if (!strcmp_cpuid_str(map->cpuid, cpuid))
695 			break;
696 	}
697 	free(cpuid);
698 	return map;
699 }
700 
701 bool pmu_uncore_alias_match(const char *pmu_name, const char *name)
702 {
703 	char *tmp = NULL, *tok, *str;
704 	bool res;
705 
706 	str = strdup(pmu_name);
707 	if (!str)
708 		return false;
709 
710 	/*
711 	 * uncore alias may be from different PMU with common prefix
712 	 */
713 	tok = strtok_r(str, ",", &tmp);
714 	if (strncmp(pmu_name, tok, strlen(tok))) {
715 		res = false;
716 		goto out;
717 	}
718 
719 	/*
720 	 * Match more complex aliases where the alias name is a comma-delimited
721 	 * list of tokens, orderly contained in the matching PMU name.
722 	 *
723 	 * Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we
724 	 *	    match "socket" in "socketX_pmunameY" and then "pmuname" in
725 	 *	    "pmunameY".
726 	 */
727 	for (; tok; name += strlen(tok), tok = strtok_r(NULL, ",", &tmp)) {
728 		name = strstr(name, tok);
729 		if (!name) {
730 			res = false;
731 			goto out;
732 		}
733 	}
734 
735 	res = true;
736 out:
737 	free(str);
738 	return res;
739 }
740 
741 /*
742  * From the pmu_events_map, find the table of PMU events that corresponds
743  * to the current running CPU. Then, add all PMU events from that table
744  * as aliases.
745  */
746 void pmu_add_cpu_aliases_map(struct list_head *head, struct perf_pmu *pmu,
747 			     struct pmu_events_map *map)
748 {
749 	int i;
750 	const char *name = pmu->name;
751 	/*
752 	 * Found a matching PMU events table. Create aliases
753 	 */
754 	i = 0;
755 	while (1) {
756 		const char *cpu_name = is_arm_pmu_core(name) ? name : "cpu";
757 		struct pmu_event *pe = &map->table[i++];
758 		const char *pname = pe->pmu ? pe->pmu : cpu_name;
759 
760 		if (!pe->name) {
761 			if (pe->metric_group || pe->metric_name)
762 				continue;
763 			break;
764 		}
765 
766 		if (pmu_is_uncore(name) &&
767 		    pmu_uncore_alias_match(pname, name))
768 			goto new_alias;
769 
770 		if (strcmp(pname, name))
771 			continue;
772 
773 new_alias:
774 		/* need type casts to override 'const' */
775 		__perf_pmu__new_alias(head, NULL, (char *)pe->name,
776 				(char *)pe->desc, (char *)pe->event,
777 				(char *)pe->long_desc, (char *)pe->topic,
778 				(char *)pe->unit, (char *)pe->perpkg,
779 				(char *)pe->metric_expr,
780 				(char *)pe->metric_name,
781 				(char *)pe->deprecated);
782 	}
783 }
784 
785 static void pmu_add_cpu_aliases(struct list_head *head, struct perf_pmu *pmu)
786 {
787 	struct pmu_events_map *map;
788 
789 	map = perf_pmu__find_map(pmu);
790 	if (!map)
791 		return;
792 
793 	pmu_add_cpu_aliases_map(head, pmu, map);
794 }
795 
796 struct perf_event_attr * __weak
797 perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
798 {
799 	return NULL;
800 }
801 
802 static int pmu_max_precise(const char *name)
803 {
804 	char path[PATH_MAX];
805 	int max_precise = -1;
806 
807 	scnprintf(path, PATH_MAX,
808 		 "bus/event_source/devices/%s/caps/max_precise",
809 		 name);
810 
811 	sysfs__read_int(path, &max_precise);
812 	return max_precise;
813 }
814 
815 static struct perf_pmu *pmu_lookup(const char *name)
816 {
817 	struct perf_pmu *pmu;
818 	LIST_HEAD(format);
819 	LIST_HEAD(aliases);
820 	__u32 type;
821 
822 	/*
823 	 * The pmu data we store & need consists of the pmu
824 	 * type value and format definitions. Load both right
825 	 * now.
826 	 */
827 	if (pmu_format(name, &format))
828 		return NULL;
829 
830 	/*
831 	 * Check the type first to avoid unnecessary work.
832 	 */
833 	if (pmu_type(name, &type))
834 		return NULL;
835 
836 	if (pmu_aliases(name, &aliases))
837 		return NULL;
838 
839 	pmu = zalloc(sizeof(*pmu));
840 	if (!pmu)
841 		return NULL;
842 
843 	pmu->cpus = pmu_cpumask(name);
844 	pmu->name = strdup(name);
845 	pmu->type = type;
846 	pmu->is_uncore = pmu_is_uncore(name);
847 	pmu->max_precise = pmu_max_precise(name);
848 	pmu_add_cpu_aliases(&aliases, pmu);
849 
850 	INIT_LIST_HEAD(&pmu->format);
851 	INIT_LIST_HEAD(&pmu->aliases);
852 	list_splice(&format, &pmu->format);
853 	list_splice(&aliases, &pmu->aliases);
854 	list_add_tail(&pmu->list, &pmus);
855 
856 	pmu->default_config = perf_pmu__get_default_config(pmu);
857 
858 	return pmu;
859 }
860 
861 static struct perf_pmu *pmu_find(const char *name)
862 {
863 	struct perf_pmu *pmu;
864 
865 	list_for_each_entry(pmu, &pmus, list)
866 		if (!strcmp(pmu->name, name))
867 			return pmu;
868 
869 	return NULL;
870 }
871 
872 struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
873 {
874 	/*
875 	 * pmu iterator: If pmu is NULL, we start at the begin,
876 	 * otherwise return the next pmu. Returns NULL on end.
877 	 */
878 	if (!pmu) {
879 		pmu_read_sysfs();
880 		pmu = list_prepare_entry(pmu, &pmus, list);
881 	}
882 	list_for_each_entry_continue(pmu, &pmus, list)
883 		return pmu;
884 	return NULL;
885 }
886 
887 struct perf_pmu *perf_pmu__find(const char *name)
888 {
889 	struct perf_pmu *pmu;
890 
891 	/*
892 	 * Once PMU is loaded it stays in the list,
893 	 * so we keep us from multiple reading/parsing
894 	 * the pmu format definitions.
895 	 */
896 	pmu = pmu_find(name);
897 	if (pmu)
898 		return pmu;
899 
900 	return pmu_lookup(name);
901 }
902 
903 static struct perf_pmu_format *
904 pmu_find_format(struct list_head *formats, const char *name)
905 {
906 	struct perf_pmu_format *format;
907 
908 	list_for_each_entry(format, formats, list)
909 		if (!strcmp(format->name, name))
910 			return format;
911 
912 	return NULL;
913 }
914 
915 __u64 perf_pmu__format_bits(struct list_head *formats, const char *name)
916 {
917 	struct perf_pmu_format *format = pmu_find_format(formats, name);
918 	__u64 bits = 0;
919 	int fbit;
920 
921 	if (!format)
922 		return 0;
923 
924 	for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
925 		bits |= 1ULL << fbit;
926 
927 	return bits;
928 }
929 
930 int perf_pmu__format_type(struct list_head *formats, const char *name)
931 {
932 	struct perf_pmu_format *format = pmu_find_format(formats, name);
933 
934 	if (!format)
935 		return -1;
936 
937 	return format->value;
938 }
939 
940 /*
941  * Sets value based on the format definition (format parameter)
942  * and unformated value (value parameter).
943  */
944 static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
945 			     bool zero)
946 {
947 	unsigned long fbit, vbit;
948 
949 	for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
950 
951 		if (!test_bit(fbit, format))
952 			continue;
953 
954 		if (value & (1llu << vbit++))
955 			*v |= (1llu << fbit);
956 		else if (zero)
957 			*v &= ~(1llu << fbit);
958 	}
959 }
960 
961 static __u64 pmu_format_max_value(const unsigned long *format)
962 {
963 	int w;
964 
965 	w = bitmap_weight(format, PERF_PMU_FORMAT_BITS);
966 	if (!w)
967 		return 0;
968 	if (w < 64)
969 		return (1ULL << w) - 1;
970 	return -1;
971 }
972 
973 /*
974  * Term is a string term, and might be a param-term. Try to look up it's value
975  * in the remaining terms.
976  * - We have a term like "base-or-format-term=param-term",
977  * - We need to find the value supplied for "param-term" (with param-term named
978  *   in a config string) later on in the term list.
979  */
980 static int pmu_resolve_param_term(struct parse_events_term *term,
981 				  struct list_head *head_terms,
982 				  __u64 *value)
983 {
984 	struct parse_events_term *t;
985 
986 	list_for_each_entry(t, head_terms, list) {
987 		if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM &&
988 		    t->config && !strcmp(t->config, term->config)) {
989 			t->used = true;
990 			*value = t->val.num;
991 			return 0;
992 		}
993 	}
994 
995 	if (verbose > 0)
996 		printf("Required parameter '%s' not specified\n", term->config);
997 
998 	return -1;
999 }
1000 
1001 static char *pmu_formats_string(struct list_head *formats)
1002 {
1003 	struct perf_pmu_format *format;
1004 	char *str = NULL;
1005 	struct strbuf buf = STRBUF_INIT;
1006 	unsigned i = 0;
1007 
1008 	if (!formats)
1009 		return NULL;
1010 
1011 	/* sysfs exported terms */
1012 	list_for_each_entry(format, formats, list)
1013 		if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
1014 			goto error;
1015 
1016 	str = strbuf_detach(&buf, NULL);
1017 error:
1018 	strbuf_release(&buf);
1019 
1020 	return str;
1021 }
1022 
1023 /*
1024  * Setup one of config[12] attr members based on the
1025  * user input data - term parameter.
1026  */
1027 static int pmu_config_term(struct list_head *formats,
1028 			   struct perf_event_attr *attr,
1029 			   struct parse_events_term *term,
1030 			   struct list_head *head_terms,
1031 			   bool zero, struct parse_events_error *err)
1032 {
1033 	struct perf_pmu_format *format;
1034 	__u64 *vp;
1035 	__u64 val, max_val;
1036 
1037 	/*
1038 	 * If this is a parameter we've already used for parameterized-eval,
1039 	 * skip it in normal eval.
1040 	 */
1041 	if (term->used)
1042 		return 0;
1043 
1044 	/*
1045 	 * Hardcoded terms should be already in, so nothing
1046 	 * to be done for them.
1047 	 */
1048 	if (parse_events__is_hardcoded_term(term))
1049 		return 0;
1050 
1051 	format = pmu_find_format(formats, term->config);
1052 	if (!format) {
1053 		if (verbose > 0)
1054 			printf("Invalid event/parameter '%s'\n", term->config);
1055 		if (err) {
1056 			char *pmu_term = pmu_formats_string(formats);
1057 
1058 			parse_events__handle_error(err, term->err_term,
1059 				strdup("unknown term"),
1060 				parse_events_formats_error_string(pmu_term));
1061 			free(pmu_term);
1062 		}
1063 		return -EINVAL;
1064 	}
1065 
1066 	switch (format->value) {
1067 	case PERF_PMU_FORMAT_VALUE_CONFIG:
1068 		vp = &attr->config;
1069 		break;
1070 	case PERF_PMU_FORMAT_VALUE_CONFIG1:
1071 		vp = &attr->config1;
1072 		break;
1073 	case PERF_PMU_FORMAT_VALUE_CONFIG2:
1074 		vp = &attr->config2;
1075 		break;
1076 	default:
1077 		return -EINVAL;
1078 	}
1079 
1080 	/*
1081 	 * Either directly use a numeric term, or try to translate string terms
1082 	 * using event parameters.
1083 	 */
1084 	if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1085 		if (term->no_value &&
1086 		    bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
1087 			if (err) {
1088 				parse_events__handle_error(err, term->err_val,
1089 					   strdup("no value assigned for term"),
1090 					   NULL);
1091 			}
1092 			return -EINVAL;
1093 		}
1094 
1095 		val = term->val.num;
1096 	} else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1097 		if (strcmp(term->val.str, "?")) {
1098 			if (verbose > 0) {
1099 				pr_info("Invalid sysfs entry %s=%s\n",
1100 						term->config, term->val.str);
1101 			}
1102 			if (err) {
1103 				parse_events__handle_error(err, term->err_val,
1104 					strdup("expected numeric value"),
1105 					NULL);
1106 			}
1107 			return -EINVAL;
1108 		}
1109 
1110 		if (pmu_resolve_param_term(term, head_terms, &val))
1111 			return -EINVAL;
1112 	} else
1113 		return -EINVAL;
1114 
1115 	max_val = pmu_format_max_value(format->bits);
1116 	if (val > max_val) {
1117 		if (err) {
1118 			char *err_str;
1119 
1120 			parse_events__handle_error(err, term->err_val,
1121 				asprintf(&err_str,
1122 				    "value too big for format, maximum is %llu",
1123 				    (unsigned long long)max_val) < 0
1124 				    ? strdup("value too big for format")
1125 				    : err_str,
1126 				    NULL);
1127 			return -EINVAL;
1128 		}
1129 		/*
1130 		 * Assume we don't care if !err, in which case the value will be
1131 		 * silently truncated.
1132 		 */
1133 	}
1134 
1135 	pmu_format_value(format->bits, val, vp, zero);
1136 	return 0;
1137 }
1138 
1139 int perf_pmu__config_terms(struct list_head *formats,
1140 			   struct perf_event_attr *attr,
1141 			   struct list_head *head_terms,
1142 			   bool zero, struct parse_events_error *err)
1143 {
1144 	struct parse_events_term *term;
1145 
1146 	list_for_each_entry(term, head_terms, list) {
1147 		if (pmu_config_term(formats, attr, term, head_terms,
1148 				    zero, err))
1149 			return -EINVAL;
1150 	}
1151 
1152 	return 0;
1153 }
1154 
1155 /*
1156  * Configures event's 'attr' parameter based on the:
1157  * 1) users input - specified in terms parameter
1158  * 2) pmu format definitions - specified by pmu parameter
1159  */
1160 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
1161 		     struct list_head *head_terms,
1162 		     struct parse_events_error *err)
1163 {
1164 	bool zero = !!pmu->default_config;
1165 
1166 	attr->type = pmu->type;
1167 	return perf_pmu__config_terms(&pmu->format, attr, head_terms,
1168 				      zero, err);
1169 }
1170 
1171 static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
1172 					     struct parse_events_term *term)
1173 {
1174 	struct perf_pmu_alias *alias;
1175 	char *name;
1176 
1177 	if (parse_events__is_hardcoded_term(term))
1178 		return NULL;
1179 
1180 	if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1181 		if (term->val.num != 1)
1182 			return NULL;
1183 		if (pmu_find_format(&pmu->format, term->config))
1184 			return NULL;
1185 		name = term->config;
1186 	} else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1187 		if (strcasecmp(term->config, "event"))
1188 			return NULL;
1189 		name = term->val.str;
1190 	} else {
1191 		return NULL;
1192 	}
1193 
1194 	list_for_each_entry(alias, &pmu->aliases, list) {
1195 		if (!strcasecmp(alias->name, name))
1196 			return alias;
1197 	}
1198 	return NULL;
1199 }
1200 
1201 
1202 static int check_info_data(struct perf_pmu_alias *alias,
1203 			   struct perf_pmu_info *info)
1204 {
1205 	/*
1206 	 * Only one term in event definition can
1207 	 * define unit, scale and snapshot, fail
1208 	 * if there's more than one.
1209 	 */
1210 	if ((info->unit && alias->unit[0]) ||
1211 	    (info->scale && alias->scale) ||
1212 	    (info->snapshot && alias->snapshot))
1213 		return -EINVAL;
1214 
1215 	if (alias->unit[0])
1216 		info->unit = alias->unit;
1217 
1218 	if (alias->scale)
1219 		info->scale = alias->scale;
1220 
1221 	if (alias->snapshot)
1222 		info->snapshot = alias->snapshot;
1223 
1224 	return 0;
1225 }
1226 
1227 /*
1228  * Find alias in the terms list and replace it with the terms
1229  * defined for the alias
1230  */
1231 int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
1232 			  struct perf_pmu_info *info)
1233 {
1234 	struct parse_events_term *term, *h;
1235 	struct perf_pmu_alias *alias;
1236 	int ret;
1237 
1238 	info->per_pkg = false;
1239 
1240 	/*
1241 	 * Mark unit and scale as not set
1242 	 * (different from default values, see below)
1243 	 */
1244 	info->unit     = NULL;
1245 	info->scale    = 0.0;
1246 	info->snapshot = false;
1247 	info->metric_expr = NULL;
1248 	info->metric_name = NULL;
1249 
1250 	list_for_each_entry_safe(term, h, head_terms, list) {
1251 		alias = pmu_find_alias(pmu, term);
1252 		if (!alias)
1253 			continue;
1254 		ret = pmu_alias_terms(alias, &term->list);
1255 		if (ret)
1256 			return ret;
1257 
1258 		ret = check_info_data(alias, info);
1259 		if (ret)
1260 			return ret;
1261 
1262 		if (alias->per_pkg)
1263 			info->per_pkg = true;
1264 		info->metric_expr = alias->metric_expr;
1265 		info->metric_name = alias->metric_name;
1266 
1267 		list_del_init(&term->list);
1268 		parse_events_term__delete(term);
1269 	}
1270 
1271 	/*
1272 	 * if no unit or scale foundin aliases, then
1273 	 * set defaults as for evsel
1274 	 * unit cannot left to NULL
1275 	 */
1276 	if (info->unit == NULL)
1277 		info->unit   = "";
1278 
1279 	if (info->scale == 0.0)
1280 		info->scale  = 1.0;
1281 
1282 	return 0;
1283 }
1284 
1285 int perf_pmu__new_format(struct list_head *list, char *name,
1286 			 int config, unsigned long *bits)
1287 {
1288 	struct perf_pmu_format *format;
1289 
1290 	format = zalloc(sizeof(*format));
1291 	if (!format)
1292 		return -ENOMEM;
1293 
1294 	format->name = strdup(name);
1295 	format->value = config;
1296 	memcpy(format->bits, bits, sizeof(format->bits));
1297 
1298 	list_add_tail(&format->list, list);
1299 	return 0;
1300 }
1301 
1302 void perf_pmu__set_format(unsigned long *bits, long from, long to)
1303 {
1304 	long b;
1305 
1306 	if (!to)
1307 		to = from;
1308 
1309 	memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
1310 	for (b = from; b <= to; b++)
1311 		set_bit(b, bits);
1312 }
1313 
1314 static int sub_non_neg(int a, int b)
1315 {
1316 	if (b > a)
1317 		return 0;
1318 	return a - b;
1319 }
1320 
1321 static char *format_alias(char *buf, int len, struct perf_pmu *pmu,
1322 			  struct perf_pmu_alias *alias)
1323 {
1324 	struct parse_events_term *term;
1325 	int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
1326 
1327 	list_for_each_entry(term, &alias->terms, list) {
1328 		if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1329 			used += snprintf(buf + used, sub_non_neg(len, used),
1330 					",%s=%s", term->config,
1331 					term->val.str);
1332 	}
1333 
1334 	if (sub_non_neg(len, used) > 0) {
1335 		buf[used] = '/';
1336 		used++;
1337 	}
1338 	if (sub_non_neg(len, used) > 0) {
1339 		buf[used] = '\0';
1340 		used++;
1341 	} else
1342 		buf[len - 1] = '\0';
1343 
1344 	return buf;
1345 }
1346 
1347 static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu,
1348 			     struct perf_pmu_alias *alias)
1349 {
1350 	snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name);
1351 	return buf;
1352 }
1353 
1354 struct sevent {
1355 	char *name;
1356 	char *desc;
1357 	char *topic;
1358 	char *str;
1359 	char *pmu;
1360 	char *metric_expr;
1361 	char *metric_name;
1362 };
1363 
1364 static int cmp_sevent(const void *a, const void *b)
1365 {
1366 	const struct sevent *as = a;
1367 	const struct sevent *bs = b;
1368 
1369 	/* Put extra events last */
1370 	if (!!as->desc != !!bs->desc)
1371 		return !!as->desc - !!bs->desc;
1372 	if (as->topic && bs->topic) {
1373 		int n = strcmp(as->topic, bs->topic);
1374 
1375 		if (n)
1376 			return n;
1377 	}
1378 	return strcmp(as->name, bs->name);
1379 }
1380 
1381 static void wordwrap(char *s, int start, int max, int corr)
1382 {
1383 	int column = start;
1384 	int n;
1385 
1386 	while (*s) {
1387 		int wlen = strcspn(s, " \t");
1388 
1389 		if (column + wlen >= max && column > start) {
1390 			printf("\n%*s", start, "");
1391 			column = start + corr;
1392 		}
1393 		n = printf("%s%.*s", column > start ? " " : "", wlen, s);
1394 		if (n <= 0)
1395 			break;
1396 		s += wlen;
1397 		column += n;
1398 		s = skip_spaces(s);
1399 	}
1400 }
1401 
1402 bool is_pmu_core(const char *name)
1403 {
1404 	return !strcmp(name, "cpu") || is_arm_pmu_core(name);
1405 }
1406 
1407 void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag,
1408 			bool long_desc, bool details_flag, bool deprecated)
1409 {
1410 	struct perf_pmu *pmu;
1411 	struct perf_pmu_alias *alias;
1412 	char buf[1024];
1413 	int printed = 0;
1414 	int len, j;
1415 	struct sevent *aliases;
1416 	int numdesc = 0;
1417 	int columns = pager_get_columns();
1418 	char *topic = NULL;
1419 
1420 	pmu = NULL;
1421 	len = 0;
1422 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1423 		list_for_each_entry(alias, &pmu->aliases, list)
1424 			len++;
1425 		if (pmu->selectable)
1426 			len++;
1427 	}
1428 	aliases = zalloc(sizeof(struct sevent) * len);
1429 	if (!aliases)
1430 		goto out_enomem;
1431 	pmu = NULL;
1432 	j = 0;
1433 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1434 		list_for_each_entry(alias, &pmu->aliases, list) {
1435 			char *name = alias->desc ? alias->name :
1436 				format_alias(buf, sizeof(buf), pmu, alias);
1437 			bool is_cpu = !strcmp(pmu->name, "cpu");
1438 
1439 			if (alias->deprecated && !deprecated)
1440 				continue;
1441 
1442 			if (event_glob != NULL &&
1443 			    !(strglobmatch_nocase(name, event_glob) ||
1444 			      (!is_cpu && strglobmatch_nocase(alias->name,
1445 						       event_glob)) ||
1446 			      (alias->topic &&
1447 			       strglobmatch_nocase(alias->topic, event_glob))))
1448 				continue;
1449 
1450 			if (is_cpu && !name_only && !alias->desc)
1451 				name = format_alias_or(buf, sizeof(buf), pmu, alias);
1452 
1453 			aliases[j].name = name;
1454 			if (is_cpu && !name_only && !alias->desc)
1455 				aliases[j].name = format_alias_or(buf,
1456 								  sizeof(buf),
1457 								  pmu, alias);
1458 			aliases[j].name = strdup(aliases[j].name);
1459 			if (!aliases[j].name)
1460 				goto out_enomem;
1461 
1462 			aliases[j].desc = long_desc ? alias->long_desc :
1463 						alias->desc;
1464 			aliases[j].topic = alias->topic;
1465 			aliases[j].str = alias->str;
1466 			aliases[j].pmu = pmu->name;
1467 			aliases[j].metric_expr = alias->metric_expr;
1468 			aliases[j].metric_name = alias->metric_name;
1469 			j++;
1470 		}
1471 		if (pmu->selectable &&
1472 		    (event_glob == NULL || strglobmatch(pmu->name, event_glob))) {
1473 			char *s;
1474 			if (asprintf(&s, "%s//", pmu->name) < 0)
1475 				goto out_enomem;
1476 			aliases[j].name = s;
1477 			j++;
1478 		}
1479 	}
1480 	len = j;
1481 	qsort(aliases, len, sizeof(struct sevent), cmp_sevent);
1482 	for (j = 0; j < len; j++) {
1483 		/* Skip duplicates */
1484 		if (j > 0 && !strcmp(aliases[j].name, aliases[j - 1].name))
1485 			continue;
1486 		if (name_only) {
1487 			printf("%s ", aliases[j].name);
1488 			continue;
1489 		}
1490 		if (aliases[j].desc && !quiet_flag) {
1491 			if (numdesc++ == 0)
1492 				printf("\n");
1493 			if (aliases[j].topic && (!topic ||
1494 					strcmp(topic, aliases[j].topic))) {
1495 				printf("%s%s:\n", topic ? "\n" : "",
1496 						aliases[j].topic);
1497 				topic = aliases[j].topic;
1498 			}
1499 			printf("  %-50s\n", aliases[j].name);
1500 			printf("%*s", 8, "[");
1501 			wordwrap(aliases[j].desc, 8, columns, 0);
1502 			printf("]\n");
1503 			if (details_flag) {
1504 				printf("%*s%s/%s/ ", 8, "", aliases[j].pmu, aliases[j].str);
1505 				if (aliases[j].metric_name)
1506 					printf(" MetricName: %s", aliases[j].metric_name);
1507 				if (aliases[j].metric_expr)
1508 					printf(" MetricExpr: %s", aliases[j].metric_expr);
1509 				putchar('\n');
1510 			}
1511 		} else
1512 			printf("  %-50s [Kernel PMU event]\n", aliases[j].name);
1513 		printed++;
1514 	}
1515 	if (printed && pager_in_use())
1516 		printf("\n");
1517 out_free:
1518 	for (j = 0; j < len; j++)
1519 		zfree(&aliases[j].name);
1520 	zfree(&aliases);
1521 	return;
1522 
1523 out_enomem:
1524 	printf("FATAL: not enough memory to print PMU events\n");
1525 	if (aliases)
1526 		goto out_free;
1527 }
1528 
1529 bool pmu_have_event(const char *pname, const char *name)
1530 {
1531 	struct perf_pmu *pmu;
1532 	struct perf_pmu_alias *alias;
1533 
1534 	pmu = NULL;
1535 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1536 		if (strcmp(pname, pmu->name))
1537 			continue;
1538 		list_for_each_entry(alias, &pmu->aliases, list)
1539 			if (!strcmp(alias->name, name))
1540 				return true;
1541 	}
1542 	return false;
1543 }
1544 
1545 static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
1546 {
1547 	char path[PATH_MAX];
1548 	const char *sysfs;
1549 
1550 	sysfs = sysfs__mountpoint();
1551 	if (!sysfs)
1552 		return NULL;
1553 
1554 	snprintf(path, PATH_MAX,
1555 		 "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name);
1556 	if (!file_available(path))
1557 		return NULL;
1558 	return fopen(path, "r");
1559 }
1560 
1561 int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
1562 			...)
1563 {
1564 	va_list args;
1565 	FILE *file;
1566 	int ret = EOF;
1567 
1568 	va_start(args, fmt);
1569 	file = perf_pmu__open_file(pmu, name);
1570 	if (file) {
1571 		ret = vfscanf(file, fmt, args);
1572 		fclose(file);
1573 	}
1574 	va_end(args);
1575 	return ret;
1576 }
1577