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