xref: /openbmc/linux/tools/perf/util/pmu.c (revision 940db6dc)
1 #include <linux/list.h>
2 #include <linux/compiler.h>
3 #include <sys/types.h>
4 #include <unistd.h>
5 #include <stdio.h>
6 #include <stdbool.h>
7 #include <stdarg.h>
8 #include <dirent.h>
9 #include <api/fs/fs.h>
10 #include <locale.h>
11 #include "util.h"
12 #include "pmu.h"
13 #include "parse-events.h"
14 #include "cpumap.h"
15 
16 struct perf_pmu_format {
17 	char *name;
18 	int value;
19 	DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
20 	struct list_head list;
21 };
22 
23 #define EVENT_SOURCE_DEVICE_PATH "/bus/event_source/devices/"
24 
25 int perf_pmu_parse(struct list_head *list, char *name);
26 extern FILE *perf_pmu_in;
27 
28 static LIST_HEAD(pmus);
29 
30 /*
31  * Parse & process all the sysfs attributes located under
32  * the directory specified in 'dir' parameter.
33  */
34 int perf_pmu__format_parse(char *dir, struct list_head *head)
35 {
36 	struct dirent *evt_ent;
37 	DIR *format_dir;
38 	int ret = 0;
39 
40 	format_dir = opendir(dir);
41 	if (!format_dir)
42 		return -EINVAL;
43 
44 	while (!ret && (evt_ent = readdir(format_dir))) {
45 		char path[PATH_MAX];
46 		char *name = evt_ent->d_name;
47 		FILE *file;
48 
49 		if (!strcmp(name, ".") || !strcmp(name, ".."))
50 			continue;
51 
52 		snprintf(path, PATH_MAX, "%s/%s", dir, name);
53 
54 		ret = -EINVAL;
55 		file = fopen(path, "r");
56 		if (!file)
57 			break;
58 
59 		perf_pmu_in = file;
60 		ret = perf_pmu_parse(head, name);
61 		fclose(file);
62 	}
63 
64 	closedir(format_dir);
65 	return ret;
66 }
67 
68 /*
69  * Reading/parsing the default pmu format definition, which should be
70  * located at:
71  * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
72  */
73 static int pmu_format(const char *name, struct list_head *format)
74 {
75 	struct stat st;
76 	char path[PATH_MAX];
77 	const char *sysfs = sysfs__mountpoint();
78 
79 	if (!sysfs)
80 		return -1;
81 
82 	snprintf(path, PATH_MAX,
83 		 "%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name);
84 
85 	if (stat(path, &st) < 0)
86 		return 0;	/* no error if format does not exist */
87 
88 	if (perf_pmu__format_parse(path, format))
89 		return -1;
90 
91 	return 0;
92 }
93 
94 static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name)
95 {
96 	struct stat st;
97 	ssize_t sret;
98 	char scale[128];
99 	int fd, ret = -1;
100 	char path[PATH_MAX];
101 	const char *lc;
102 
103 	snprintf(path, PATH_MAX, "%s/%s.scale", dir, name);
104 
105 	fd = open(path, O_RDONLY);
106 	if (fd == -1)
107 		return -1;
108 
109 	if (fstat(fd, &st) < 0)
110 		goto error;
111 
112 	sret = read(fd, scale, sizeof(scale)-1);
113 	if (sret < 0)
114 		goto error;
115 
116 	if (scale[sret - 1] == '\n')
117 		scale[sret - 1] = '\0';
118 	else
119 		scale[sret] = '\0';
120 
121 	/*
122 	 * save current locale
123 	 */
124 	lc = setlocale(LC_NUMERIC, NULL);
125 
126 	/*
127 	 * force to C locale to ensure kernel
128 	 * scale string is converted correctly.
129 	 * kernel uses default C locale.
130 	 */
131 	setlocale(LC_NUMERIC, "C");
132 
133 	alias->scale = strtod(scale, NULL);
134 
135 	/* restore locale */
136 	setlocale(LC_NUMERIC, lc);
137 
138 	ret = 0;
139 error:
140 	close(fd);
141 	return ret;
142 }
143 
144 static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name)
145 {
146 	char path[PATH_MAX];
147 	ssize_t sret;
148 	int fd;
149 
150 	snprintf(path, PATH_MAX, "%s/%s.unit", dir, name);
151 
152 	fd = open(path, O_RDONLY);
153 	if (fd == -1)
154 		return -1;
155 
156 	sret = read(fd, alias->unit, UNIT_MAX_LEN);
157 	if (sret < 0)
158 		goto error;
159 
160 	close(fd);
161 
162 	if (alias->unit[sret - 1] == '\n')
163 		alias->unit[sret - 1] = '\0';
164 	else
165 		alias->unit[sret] = '\0';
166 
167 	return 0;
168 error:
169 	close(fd);
170 	alias->unit[0] = '\0';
171 	return -1;
172 }
173 
174 static int
175 perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *name)
176 {
177 	char path[PATH_MAX];
178 	int fd;
179 
180 	snprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name);
181 
182 	fd = open(path, O_RDONLY);
183 	if (fd == -1)
184 		return -1;
185 
186 	close(fd);
187 
188 	alias->per_pkg = true;
189 	return 0;
190 }
191 
192 static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias,
193 				    char *dir, char *name)
194 {
195 	char path[PATH_MAX];
196 	int fd;
197 
198 	snprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name);
199 
200 	fd = open(path, O_RDONLY);
201 	if (fd == -1)
202 		return -1;
203 
204 	alias->snapshot = true;
205 	close(fd);
206 	return 0;
207 }
208 
209 static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name,
210 				 char *desc __maybe_unused, char *val)
211 {
212 	struct perf_pmu_alias *alias;
213 	int ret;
214 
215 	alias = malloc(sizeof(*alias));
216 	if (!alias)
217 		return -ENOMEM;
218 
219 	INIT_LIST_HEAD(&alias->terms);
220 	alias->scale = 1.0;
221 	alias->unit[0] = '\0';
222 	alias->per_pkg = false;
223 	alias->snapshot = false;
224 
225 	ret = parse_events_terms(&alias->terms, val);
226 	if (ret) {
227 		pr_err("Cannot parse alias %s: %d\n", val, ret);
228 		free(alias);
229 		return ret;
230 	}
231 
232 	alias->name = strdup(name);
233 	if (dir) {
234 		/*
235 		 * load unit name and scale if available
236 		 */
237 		perf_pmu__parse_unit(alias, dir, name);
238 		perf_pmu__parse_scale(alias, dir, name);
239 		perf_pmu__parse_per_pkg(alias, dir, name);
240 		perf_pmu__parse_snapshot(alias, dir, name);
241 	}
242 
243 	list_add_tail(&alias->list, list);
244 
245 	return 0;
246 }
247 
248 static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file)
249 {
250 	char buf[256];
251 	int ret;
252 
253 	ret = fread(buf, 1, sizeof(buf), file);
254 	if (ret == 0)
255 		return -EINVAL;
256 
257 	buf[ret] = 0;
258 
259 	return __perf_pmu__new_alias(list, dir, name, NULL, buf);
260 }
261 
262 static inline bool pmu_alias_info_file(char *name)
263 {
264 	size_t len;
265 
266 	len = strlen(name);
267 	if (len > 5 && !strcmp(name + len - 5, ".unit"))
268 		return true;
269 	if (len > 6 && !strcmp(name + len - 6, ".scale"))
270 		return true;
271 	if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
272 		return true;
273 	if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
274 		return true;
275 
276 	return false;
277 }
278 
279 /*
280  * Process all the sysfs attributes located under the directory
281  * specified in 'dir' parameter.
282  */
283 static int pmu_aliases_parse(char *dir, struct list_head *head)
284 {
285 	struct dirent *evt_ent;
286 	DIR *event_dir;
287 
288 	event_dir = opendir(dir);
289 	if (!event_dir)
290 		return -EINVAL;
291 
292 	while ((evt_ent = readdir(event_dir))) {
293 		char path[PATH_MAX];
294 		char *name = evt_ent->d_name;
295 		FILE *file;
296 
297 		if (!strcmp(name, ".") || !strcmp(name, ".."))
298 			continue;
299 
300 		/*
301 		 * skip info files parsed in perf_pmu__new_alias()
302 		 */
303 		if (pmu_alias_info_file(name))
304 			continue;
305 
306 		snprintf(path, PATH_MAX, "%s/%s", dir, name);
307 
308 		file = fopen(path, "r");
309 		if (!file) {
310 			pr_debug("Cannot open %s\n", path);
311 			continue;
312 		}
313 
314 		if (perf_pmu__new_alias(head, dir, name, file) < 0)
315 			pr_debug("Cannot set up %s\n", name);
316 		fclose(file);
317 	}
318 
319 	closedir(event_dir);
320 	return 0;
321 }
322 
323 /*
324  * Reading the pmu event aliases definition, which should be located at:
325  * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
326  */
327 static int pmu_aliases(const char *name, struct list_head *head)
328 {
329 	struct stat st;
330 	char path[PATH_MAX];
331 	const char *sysfs = sysfs__mountpoint();
332 
333 	if (!sysfs)
334 		return -1;
335 
336 	snprintf(path, PATH_MAX,
337 		 "%s/bus/event_source/devices/%s/events", sysfs, name);
338 
339 	if (stat(path, &st) < 0)
340 		return 0;	 /* no error if 'events' does not exist */
341 
342 	if (pmu_aliases_parse(path, head))
343 		return -1;
344 
345 	return 0;
346 }
347 
348 static int pmu_alias_terms(struct perf_pmu_alias *alias,
349 			   struct list_head *terms)
350 {
351 	struct parse_events_term *term, *cloned;
352 	LIST_HEAD(list);
353 	int ret;
354 
355 	list_for_each_entry(term, &alias->terms, list) {
356 		ret = parse_events_term__clone(&cloned, term);
357 		if (ret) {
358 			parse_events_terms__purge(&list);
359 			return ret;
360 		}
361 		list_add_tail(&cloned->list, &list);
362 	}
363 	list_splice(&list, terms);
364 	return 0;
365 }
366 
367 /*
368  * Reading/parsing the default pmu type value, which should be
369  * located at:
370  * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
371  */
372 static int pmu_type(const char *name, __u32 *type)
373 {
374 	struct stat st;
375 	char path[PATH_MAX];
376 	FILE *file;
377 	int ret = 0;
378 	const char *sysfs = sysfs__mountpoint();
379 
380 	if (!sysfs)
381 		return -1;
382 
383 	snprintf(path, PATH_MAX,
384 		 "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name);
385 
386 	if (stat(path, &st) < 0)
387 		return -1;
388 
389 	file = fopen(path, "r");
390 	if (!file)
391 		return -EINVAL;
392 
393 	if (1 != fscanf(file, "%u", type))
394 		ret = -1;
395 
396 	fclose(file);
397 	return ret;
398 }
399 
400 /* Add all pmus in sysfs to pmu list: */
401 static void pmu_read_sysfs(void)
402 {
403 	char path[PATH_MAX];
404 	DIR *dir;
405 	struct dirent *dent;
406 	const char *sysfs = sysfs__mountpoint();
407 
408 	if (!sysfs)
409 		return;
410 
411 	snprintf(path, PATH_MAX,
412 		 "%s" EVENT_SOURCE_DEVICE_PATH, sysfs);
413 
414 	dir = opendir(path);
415 	if (!dir)
416 		return;
417 
418 	while ((dent = readdir(dir))) {
419 		if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
420 			continue;
421 		/* add to static LIST_HEAD(pmus): */
422 		perf_pmu__find(dent->d_name);
423 	}
424 
425 	closedir(dir);
426 }
427 
428 static struct cpu_map *pmu_cpumask(const char *name)
429 {
430 	struct stat st;
431 	char path[PATH_MAX];
432 	FILE *file;
433 	struct cpu_map *cpus;
434 	const char *sysfs = sysfs__mountpoint();
435 
436 	if (!sysfs)
437 		return NULL;
438 
439 	snprintf(path, PATH_MAX,
440 		 "%s/bus/event_source/devices/%s/cpumask", sysfs, name);
441 
442 	if (stat(path, &st) < 0)
443 		return NULL;
444 
445 	file = fopen(path, "r");
446 	if (!file)
447 		return NULL;
448 
449 	cpus = cpu_map__read(file);
450 	fclose(file);
451 	return cpus;
452 }
453 
454 struct perf_event_attr * __weak
455 perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
456 {
457 	return NULL;
458 }
459 
460 static struct perf_pmu *pmu_lookup(const char *name)
461 {
462 	struct perf_pmu *pmu;
463 	LIST_HEAD(format);
464 	LIST_HEAD(aliases);
465 	__u32 type;
466 
467 	/*
468 	 * The pmu data we store & need consists of the pmu
469 	 * type value and format definitions. Load both right
470 	 * now.
471 	 */
472 	if (pmu_format(name, &format))
473 		return NULL;
474 
475 	if (pmu_aliases(name, &aliases))
476 		return NULL;
477 
478 	if (pmu_type(name, &type))
479 		return NULL;
480 
481 	pmu = zalloc(sizeof(*pmu));
482 	if (!pmu)
483 		return NULL;
484 
485 	pmu->cpus = pmu_cpumask(name);
486 
487 	INIT_LIST_HEAD(&pmu->format);
488 	INIT_LIST_HEAD(&pmu->aliases);
489 	list_splice(&format, &pmu->format);
490 	list_splice(&aliases, &pmu->aliases);
491 	pmu->name = strdup(name);
492 	pmu->type = type;
493 	list_add_tail(&pmu->list, &pmus);
494 
495 	pmu->default_config = perf_pmu__get_default_config(pmu);
496 
497 	return pmu;
498 }
499 
500 static struct perf_pmu *pmu_find(const char *name)
501 {
502 	struct perf_pmu *pmu;
503 
504 	list_for_each_entry(pmu, &pmus, list)
505 		if (!strcmp(pmu->name, name))
506 			return pmu;
507 
508 	return NULL;
509 }
510 
511 struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
512 {
513 	/*
514 	 * pmu iterator: If pmu is NULL, we start at the begin,
515 	 * otherwise return the next pmu. Returns NULL on end.
516 	 */
517 	if (!pmu) {
518 		pmu_read_sysfs();
519 		pmu = list_prepare_entry(pmu, &pmus, list);
520 	}
521 	list_for_each_entry_continue(pmu, &pmus, list)
522 		return pmu;
523 	return NULL;
524 }
525 
526 struct perf_pmu *perf_pmu__find(const char *name)
527 {
528 	struct perf_pmu *pmu;
529 
530 	/*
531 	 * Once PMU is loaded it stays in the list,
532 	 * so we keep us from multiple reading/parsing
533 	 * the pmu format definitions.
534 	 */
535 	pmu = pmu_find(name);
536 	if (pmu)
537 		return pmu;
538 
539 	return pmu_lookup(name);
540 }
541 
542 static struct perf_pmu_format *
543 pmu_find_format(struct list_head *formats, const char *name)
544 {
545 	struct perf_pmu_format *format;
546 
547 	list_for_each_entry(format, formats, list)
548 		if (!strcmp(format->name, name))
549 			return format;
550 
551 	return NULL;
552 }
553 
554 __u64 perf_pmu__format_bits(struct list_head *formats, const char *name)
555 {
556 	struct perf_pmu_format *format = pmu_find_format(formats, name);
557 	__u64 bits = 0;
558 	int fbit;
559 
560 	if (!format)
561 		return 0;
562 
563 	for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
564 		bits |= 1ULL << fbit;
565 
566 	return bits;
567 }
568 
569 /*
570  * Sets value based on the format definition (format parameter)
571  * and unformated value (value parameter).
572  */
573 static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
574 			     bool zero)
575 {
576 	unsigned long fbit, vbit;
577 
578 	for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
579 
580 		if (!test_bit(fbit, format))
581 			continue;
582 
583 		if (value & (1llu << vbit++))
584 			*v |= (1llu << fbit);
585 		else if (zero)
586 			*v &= ~(1llu << fbit);
587 	}
588 }
589 
590 static __u64 pmu_format_max_value(const unsigned long *format)
591 {
592 	int w;
593 
594 	w = bitmap_weight(format, PERF_PMU_FORMAT_BITS);
595 	if (!w)
596 		return 0;
597 	if (w < 64)
598 		return (1ULL << w) - 1;
599 	return -1;
600 }
601 
602 /*
603  * Term is a string term, and might be a param-term. Try to look up it's value
604  * in the remaining terms.
605  * - We have a term like "base-or-format-term=param-term",
606  * - We need to find the value supplied for "param-term" (with param-term named
607  *   in a config string) later on in the term list.
608  */
609 static int pmu_resolve_param_term(struct parse_events_term *term,
610 				  struct list_head *head_terms,
611 				  __u64 *value)
612 {
613 	struct parse_events_term *t;
614 
615 	list_for_each_entry(t, head_terms, list) {
616 		if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
617 			if (!strcmp(t->config, term->config)) {
618 				t->used = true;
619 				*value = t->val.num;
620 				return 0;
621 			}
622 		}
623 	}
624 
625 	if (verbose)
626 		printf("Required parameter '%s' not specified\n", term->config);
627 
628 	return -1;
629 }
630 
631 static char *pmu_formats_string(struct list_head *formats)
632 {
633 	struct perf_pmu_format *format;
634 	char *str;
635 	struct strbuf buf;
636 	unsigned i = 0;
637 
638 	if (!formats)
639 		return NULL;
640 
641 	strbuf_init(&buf, 0);
642 	/* sysfs exported terms */
643 	list_for_each_entry(format, formats, list)
644 		strbuf_addf(&buf, i++ ? ",%s" : "%s",
645 			    format->name);
646 
647 	str = strbuf_detach(&buf, NULL);
648 	strbuf_release(&buf);
649 
650 	return str;
651 }
652 
653 /*
654  * Setup one of config[12] attr members based on the
655  * user input data - term parameter.
656  */
657 static int pmu_config_term(struct list_head *formats,
658 			   struct perf_event_attr *attr,
659 			   struct parse_events_term *term,
660 			   struct list_head *head_terms,
661 			   bool zero, struct parse_events_error *err)
662 {
663 	struct perf_pmu_format *format;
664 	__u64 *vp;
665 	__u64 val, max_val;
666 
667 	/*
668 	 * If this is a parameter we've already used for parameterized-eval,
669 	 * skip it in normal eval.
670 	 */
671 	if (term->used)
672 		return 0;
673 
674 	/*
675 	 * Hardcoded terms should be already in, so nothing
676 	 * to be done for them.
677 	 */
678 	if (parse_events__is_hardcoded_term(term))
679 		return 0;
680 
681 	format = pmu_find_format(formats, term->config);
682 	if (!format) {
683 		if (verbose)
684 			printf("Invalid event/parameter '%s'\n", term->config);
685 		if (err) {
686 			char *pmu_term = pmu_formats_string(formats);
687 
688 			err->idx  = term->err_term;
689 			err->str  = strdup("unknown term");
690 			err->help = parse_events_formats_error_string(pmu_term);
691 			free(pmu_term);
692 		}
693 		return -EINVAL;
694 	}
695 
696 	switch (format->value) {
697 	case PERF_PMU_FORMAT_VALUE_CONFIG:
698 		vp = &attr->config;
699 		break;
700 	case PERF_PMU_FORMAT_VALUE_CONFIG1:
701 		vp = &attr->config1;
702 		break;
703 	case PERF_PMU_FORMAT_VALUE_CONFIG2:
704 		vp = &attr->config2;
705 		break;
706 	default:
707 		return -EINVAL;
708 	}
709 
710 	/*
711 	 * Either directly use a numeric term, or try to translate string terms
712 	 * using event parameters.
713 	 */
714 	if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
715 		val = term->val.num;
716 	else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
717 		if (strcmp(term->val.str, "?")) {
718 			if (verbose) {
719 				pr_info("Invalid sysfs entry %s=%s\n",
720 						term->config, term->val.str);
721 			}
722 			if (err) {
723 				err->idx = term->err_val;
724 				err->str = strdup("expected numeric value");
725 			}
726 			return -EINVAL;
727 		}
728 
729 		if (pmu_resolve_param_term(term, head_terms, &val))
730 			return -EINVAL;
731 	} else
732 		return -EINVAL;
733 
734 	max_val = pmu_format_max_value(format->bits);
735 	if (val > max_val) {
736 		if (err) {
737 			err->idx = term->err_val;
738 			if (asprintf(&err->str,
739 				     "value too big for format, maximum is %llu",
740 				     (unsigned long long)max_val) < 0)
741 				err->str = strdup("value too big for format");
742 			return -EINVAL;
743 		}
744 		/*
745 		 * Assume we don't care if !err, in which case the value will be
746 		 * silently truncated.
747 		 */
748 	}
749 
750 	pmu_format_value(format->bits, val, vp, zero);
751 	return 0;
752 }
753 
754 int perf_pmu__config_terms(struct list_head *formats,
755 			   struct perf_event_attr *attr,
756 			   struct list_head *head_terms,
757 			   bool zero, struct parse_events_error *err)
758 {
759 	struct parse_events_term *term;
760 
761 	list_for_each_entry(term, head_terms, list) {
762 		if (pmu_config_term(formats, attr, term, head_terms,
763 				    zero, err))
764 			return -EINVAL;
765 	}
766 
767 	return 0;
768 }
769 
770 /*
771  * Configures event's 'attr' parameter based on the:
772  * 1) users input - specified in terms parameter
773  * 2) pmu format definitions - specified by pmu parameter
774  */
775 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
776 		     struct list_head *head_terms,
777 		     struct parse_events_error *err)
778 {
779 	bool zero = !!pmu->default_config;
780 
781 	attr->type = pmu->type;
782 	return perf_pmu__config_terms(&pmu->format, attr, head_terms,
783 				      zero, err);
784 }
785 
786 static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
787 					     struct parse_events_term *term)
788 {
789 	struct perf_pmu_alias *alias;
790 	char *name;
791 
792 	if (parse_events__is_hardcoded_term(term))
793 		return NULL;
794 
795 	if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
796 		if (term->val.num != 1)
797 			return NULL;
798 		if (pmu_find_format(&pmu->format, term->config))
799 			return NULL;
800 		name = term->config;
801 	} else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
802 		if (strcasecmp(term->config, "event"))
803 			return NULL;
804 		name = term->val.str;
805 	} else {
806 		return NULL;
807 	}
808 
809 	list_for_each_entry(alias, &pmu->aliases, list) {
810 		if (!strcasecmp(alias->name, name))
811 			return alias;
812 	}
813 	return NULL;
814 }
815 
816 
817 static int check_info_data(struct perf_pmu_alias *alias,
818 			   struct perf_pmu_info *info)
819 {
820 	/*
821 	 * Only one term in event definition can
822 	 * define unit, scale and snapshot, fail
823 	 * if there's more than one.
824 	 */
825 	if ((info->unit && alias->unit) ||
826 	    (info->scale && alias->scale) ||
827 	    (info->snapshot && alias->snapshot))
828 		return -EINVAL;
829 
830 	if (alias->unit)
831 		info->unit = alias->unit;
832 
833 	if (alias->scale)
834 		info->scale = alias->scale;
835 
836 	if (alias->snapshot)
837 		info->snapshot = alias->snapshot;
838 
839 	return 0;
840 }
841 
842 /*
843  * Find alias in the terms list and replace it with the terms
844  * defined for the alias
845  */
846 int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
847 			  struct perf_pmu_info *info)
848 {
849 	struct parse_events_term *term, *h;
850 	struct perf_pmu_alias *alias;
851 	int ret;
852 
853 	info->per_pkg = false;
854 
855 	/*
856 	 * Mark unit and scale as not set
857 	 * (different from default values, see below)
858 	 */
859 	info->unit     = NULL;
860 	info->scale    = 0.0;
861 	info->snapshot = false;
862 
863 	list_for_each_entry_safe(term, h, head_terms, list) {
864 		alias = pmu_find_alias(pmu, term);
865 		if (!alias)
866 			continue;
867 		ret = pmu_alias_terms(alias, &term->list);
868 		if (ret)
869 			return ret;
870 
871 		ret = check_info_data(alias, info);
872 		if (ret)
873 			return ret;
874 
875 		if (alias->per_pkg)
876 			info->per_pkg = true;
877 
878 		list_del(&term->list);
879 		free(term);
880 	}
881 
882 	/*
883 	 * if no unit or scale foundin aliases, then
884 	 * set defaults as for evsel
885 	 * unit cannot left to NULL
886 	 */
887 	if (info->unit == NULL)
888 		info->unit   = "";
889 
890 	if (info->scale == 0.0)
891 		info->scale  = 1.0;
892 
893 	return 0;
894 }
895 
896 int perf_pmu__new_format(struct list_head *list, char *name,
897 			 int config, unsigned long *bits)
898 {
899 	struct perf_pmu_format *format;
900 
901 	format = zalloc(sizeof(*format));
902 	if (!format)
903 		return -ENOMEM;
904 
905 	format->name = strdup(name);
906 	format->value = config;
907 	memcpy(format->bits, bits, sizeof(format->bits));
908 
909 	list_add_tail(&format->list, list);
910 	return 0;
911 }
912 
913 void perf_pmu__set_format(unsigned long *bits, long from, long to)
914 {
915 	long b;
916 
917 	if (!to)
918 		to = from;
919 
920 	memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
921 	for (b = from; b <= to; b++)
922 		set_bit(b, bits);
923 }
924 
925 static int sub_non_neg(int a, int b)
926 {
927 	if (b > a)
928 		return 0;
929 	return a - b;
930 }
931 
932 static char *format_alias(char *buf, int len, struct perf_pmu *pmu,
933 			  struct perf_pmu_alias *alias)
934 {
935 	struct parse_events_term *term;
936 	int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
937 
938 	list_for_each_entry(term, &alias->terms, list) {
939 		if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
940 			used += snprintf(buf + used, sub_non_neg(len, used),
941 					",%s=%s", term->config,
942 					term->val.str);
943 	}
944 
945 	if (sub_non_neg(len, used) > 0) {
946 		buf[used] = '/';
947 		used++;
948 	}
949 	if (sub_non_neg(len, used) > 0) {
950 		buf[used] = '\0';
951 		used++;
952 	} else
953 		buf[len - 1] = '\0';
954 
955 	return buf;
956 }
957 
958 static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu,
959 			     struct perf_pmu_alias *alias)
960 {
961 	snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name);
962 	return buf;
963 }
964 
965 static int cmp_string(const void *a, const void *b)
966 {
967 	const char * const *as = a;
968 	const char * const *bs = b;
969 	return strcmp(*as, *bs);
970 }
971 
972 void print_pmu_events(const char *event_glob, bool name_only)
973 {
974 	struct perf_pmu *pmu;
975 	struct perf_pmu_alias *alias;
976 	char buf[1024];
977 	int printed = 0;
978 	int len, j;
979 	char **aliases;
980 
981 	pmu = NULL;
982 	len = 0;
983 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
984 		list_for_each_entry(alias, &pmu->aliases, list)
985 			len++;
986 		if (pmu->selectable)
987 			len++;
988 	}
989 	aliases = zalloc(sizeof(char *) * len);
990 	if (!aliases)
991 		goto out_enomem;
992 	pmu = NULL;
993 	j = 0;
994 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
995 		list_for_each_entry(alias, &pmu->aliases, list) {
996 			char *name = format_alias(buf, sizeof(buf), pmu, alias);
997 			bool is_cpu = !strcmp(pmu->name, "cpu");
998 
999 			if (event_glob != NULL &&
1000 			    !(strglobmatch(name, event_glob) ||
1001 			      (!is_cpu && strglobmatch(alias->name,
1002 						       event_glob))))
1003 				continue;
1004 
1005 			if (is_cpu && !name_only)
1006 				name = format_alias_or(buf, sizeof(buf), pmu, alias);
1007 
1008 			aliases[j] = strdup(name);
1009 			if (aliases[j] == NULL)
1010 				goto out_enomem;
1011 			j++;
1012 		}
1013 		if (pmu->selectable &&
1014 		    (event_glob == NULL || strglobmatch(pmu->name, event_glob))) {
1015 			char *s;
1016 			if (asprintf(&s, "%s//", pmu->name) < 0)
1017 				goto out_enomem;
1018 			aliases[j] = s;
1019 			j++;
1020 		}
1021 	}
1022 	len = j;
1023 	qsort(aliases, len, sizeof(char *), cmp_string);
1024 	for (j = 0; j < len; j++) {
1025 		if (name_only) {
1026 			printf("%s ", aliases[j]);
1027 			continue;
1028 		}
1029 		printf("  %-50s [Kernel PMU event]\n", aliases[j]);
1030 		printed++;
1031 	}
1032 	if (printed && pager_in_use())
1033 		printf("\n");
1034 out_free:
1035 	for (j = 0; j < len; j++)
1036 		zfree(&aliases[j]);
1037 	zfree(&aliases);
1038 	return;
1039 
1040 out_enomem:
1041 	printf("FATAL: not enough memory to print PMU events\n");
1042 	if (aliases)
1043 		goto out_free;
1044 }
1045 
1046 bool pmu_have_event(const char *pname, const char *name)
1047 {
1048 	struct perf_pmu *pmu;
1049 	struct perf_pmu_alias *alias;
1050 
1051 	pmu = NULL;
1052 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1053 		if (strcmp(pname, pmu->name))
1054 			continue;
1055 		list_for_each_entry(alias, &pmu->aliases, list)
1056 			if (!strcmp(alias->name, name))
1057 				return true;
1058 	}
1059 	return false;
1060 }
1061 
1062 static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
1063 {
1064 	struct stat st;
1065 	char path[PATH_MAX];
1066 	const char *sysfs;
1067 
1068 	sysfs = sysfs__mountpoint();
1069 	if (!sysfs)
1070 		return NULL;
1071 
1072 	snprintf(path, PATH_MAX,
1073 		 "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name);
1074 
1075 	if (stat(path, &st) < 0)
1076 		return NULL;
1077 
1078 	return fopen(path, "r");
1079 }
1080 
1081 int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
1082 			...)
1083 {
1084 	va_list args;
1085 	FILE *file;
1086 	int ret = EOF;
1087 
1088 	va_start(args, fmt);
1089 	file = perf_pmu__open_file(pmu, name);
1090 	if (file) {
1091 		ret = vfscanf(file, fmt, args);
1092 		fclose(file);
1093 	}
1094 	va_end(args);
1095 	return ret;
1096 }
1097