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