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