xref: /openbmc/linux/tools/perf/util/pmu.c (revision 680ef72a)
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 		list_add_tail(&cloned->list, &list);
409 	}
410 	list_splice(&list, terms);
411 	return 0;
412 }
413 
414 /*
415  * Reading/parsing the default pmu type value, which should be
416  * located at:
417  * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
418  */
419 static int pmu_type(const char *name, __u32 *type)
420 {
421 	struct stat st;
422 	char path[PATH_MAX];
423 	FILE *file;
424 	int ret = 0;
425 	const char *sysfs = sysfs__mountpoint();
426 
427 	if (!sysfs)
428 		return -1;
429 
430 	snprintf(path, PATH_MAX,
431 		 "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name);
432 
433 	if (stat(path, &st) < 0)
434 		return -1;
435 
436 	file = fopen(path, "r");
437 	if (!file)
438 		return -EINVAL;
439 
440 	if (1 != fscanf(file, "%u", type))
441 		ret = -1;
442 
443 	fclose(file);
444 	return ret;
445 }
446 
447 /* Add all pmus in sysfs to pmu list: */
448 static void pmu_read_sysfs(void)
449 {
450 	char path[PATH_MAX];
451 	DIR *dir;
452 	struct dirent *dent;
453 	const char *sysfs = sysfs__mountpoint();
454 
455 	if (!sysfs)
456 		return;
457 
458 	snprintf(path, PATH_MAX,
459 		 "%s" EVENT_SOURCE_DEVICE_PATH, sysfs);
460 
461 	dir = opendir(path);
462 	if (!dir)
463 		return;
464 
465 	while ((dent = readdir(dir))) {
466 		if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
467 			continue;
468 		/* add to static LIST_HEAD(pmus): */
469 		perf_pmu__find(dent->d_name);
470 	}
471 
472 	closedir(dir);
473 }
474 
475 static struct cpu_map *__pmu_cpumask(const char *path)
476 {
477 	FILE *file;
478 	struct cpu_map *cpus;
479 
480 	file = fopen(path, "r");
481 	if (!file)
482 		return NULL;
483 
484 	cpus = cpu_map__read(file);
485 	fclose(file);
486 	return cpus;
487 }
488 
489 /*
490  * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64)
491  * may have a "cpus" file.
492  */
493 #define CPUS_TEMPLATE_UNCORE	"%s/bus/event_source/devices/%s/cpumask"
494 #define CPUS_TEMPLATE_CPU	"%s/bus/event_source/devices/%s/cpus"
495 
496 static struct cpu_map *pmu_cpumask(const char *name)
497 {
498 	char path[PATH_MAX];
499 	struct cpu_map *cpus;
500 	const char *sysfs = sysfs__mountpoint();
501 	const char *templates[] = {
502 		CPUS_TEMPLATE_UNCORE,
503 		CPUS_TEMPLATE_CPU,
504 		NULL
505 	};
506 	const char **template;
507 
508 	if (!sysfs)
509 		return NULL;
510 
511 	for (template = templates; *template; template++) {
512 		snprintf(path, PATH_MAX, *template, sysfs, name);
513 		cpus = __pmu_cpumask(path);
514 		if (cpus)
515 			return cpus;
516 	}
517 
518 	return NULL;
519 }
520 
521 static bool pmu_is_uncore(const char *name)
522 {
523 	char path[PATH_MAX];
524 	struct cpu_map *cpus;
525 	const char *sysfs = sysfs__mountpoint();
526 
527 	snprintf(path, PATH_MAX, CPUS_TEMPLATE_UNCORE, sysfs, name);
528 	cpus = __pmu_cpumask(path);
529 	cpu_map__put(cpus);
530 
531 	return !!cpus;
532 }
533 
534 /*
535  * Return the CPU id as a raw string.
536  *
537  * Each architecture should provide a more precise id string that
538  * can be use to match the architecture's "mapfile".
539  */
540 char * __weak get_cpuid_str(void)
541 {
542 	return NULL;
543 }
544 
545 static char *perf_pmu__getcpuid(void)
546 {
547 	char *cpuid;
548 	static bool printed;
549 
550 	cpuid = getenv("PERF_CPUID");
551 	if (cpuid)
552 		cpuid = strdup(cpuid);
553 	if (!cpuid)
554 		cpuid = get_cpuid_str();
555 	if (!cpuid)
556 		return NULL;
557 
558 	if (!printed) {
559 		pr_debug("Using CPUID %s\n", cpuid);
560 		printed = true;
561 	}
562 	return cpuid;
563 }
564 
565 struct pmu_events_map *perf_pmu__find_map(void)
566 {
567 	struct pmu_events_map *map;
568 	char *cpuid = perf_pmu__getcpuid();
569 	int i;
570 
571 	i = 0;
572 	for (;;) {
573 		map = &pmu_events_map[i++];
574 		if (!map->table) {
575 			map = NULL;
576 			break;
577 		}
578 
579 		if (!strcmp(map->cpuid, cpuid))
580 			break;
581 	}
582 	free(cpuid);
583 	return map;
584 }
585 
586 /*
587  * From the pmu_events_map, find the table of PMU events that corresponds
588  * to the current running CPU. Then, add all PMU events from that table
589  * as aliases.
590  */
591 static void pmu_add_cpu_aliases(struct list_head *head, const char *name)
592 {
593 	int i;
594 	struct pmu_events_map *map;
595 	struct pmu_event *pe;
596 
597 	map = perf_pmu__find_map();
598 	if (!map)
599 		return;
600 
601 	/*
602 	 * Found a matching PMU events table. Create aliases
603 	 */
604 	i = 0;
605 	while (1) {
606 		const char *pname;
607 
608 		pe = &map->table[i++];
609 		if (!pe->name) {
610 			if (pe->metric_group || pe->metric_name)
611 				continue;
612 			break;
613 		}
614 
615 		pname = pe->pmu ? pe->pmu : "cpu";
616 		if (strncmp(pname, name, strlen(pname)))
617 			continue;
618 
619 		/* need type casts to override 'const' */
620 		__perf_pmu__new_alias(head, NULL, (char *)pe->name,
621 				(char *)pe->desc, (char *)pe->event,
622 				(char *)pe->long_desc, (char *)pe->topic,
623 				(char *)pe->unit, (char *)pe->perpkg,
624 				(char *)pe->metric_expr,
625 				(char *)pe->metric_name);
626 	}
627 }
628 
629 struct perf_event_attr * __weak
630 perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
631 {
632 	return NULL;
633 }
634 
635 static struct perf_pmu *pmu_lookup(const char *name)
636 {
637 	struct perf_pmu *pmu;
638 	LIST_HEAD(format);
639 	LIST_HEAD(aliases);
640 	__u32 type;
641 
642 	/*
643 	 * The pmu data we store & need consists of the pmu
644 	 * type value and format definitions. Load both right
645 	 * now.
646 	 */
647 	if (pmu_format(name, &format))
648 		return NULL;
649 
650 	/*
651 	 * Check the type first to avoid unnecessary work.
652 	 */
653 	if (pmu_type(name, &type))
654 		return NULL;
655 
656 	if (pmu_aliases(name, &aliases))
657 		return NULL;
658 
659 	pmu_add_cpu_aliases(&aliases, name);
660 	pmu = zalloc(sizeof(*pmu));
661 	if (!pmu)
662 		return NULL;
663 
664 	pmu->cpus = pmu_cpumask(name);
665 
666 	pmu->is_uncore = pmu_is_uncore(name);
667 
668 	INIT_LIST_HEAD(&pmu->format);
669 	INIT_LIST_HEAD(&pmu->aliases);
670 	list_splice(&format, &pmu->format);
671 	list_splice(&aliases, &pmu->aliases);
672 	pmu->name = strdup(name);
673 	pmu->type = type;
674 	list_add_tail(&pmu->list, &pmus);
675 
676 	pmu->default_config = perf_pmu__get_default_config(pmu);
677 
678 	return pmu;
679 }
680 
681 static struct perf_pmu *pmu_find(const char *name)
682 {
683 	struct perf_pmu *pmu;
684 
685 	list_for_each_entry(pmu, &pmus, list)
686 		if (!strcmp(pmu->name, name))
687 			return pmu;
688 
689 	return NULL;
690 }
691 
692 struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
693 {
694 	/*
695 	 * pmu iterator: If pmu is NULL, we start at the begin,
696 	 * otherwise return the next pmu. Returns NULL on end.
697 	 */
698 	if (!pmu) {
699 		pmu_read_sysfs();
700 		pmu = list_prepare_entry(pmu, &pmus, list);
701 	}
702 	list_for_each_entry_continue(pmu, &pmus, list)
703 		return pmu;
704 	return NULL;
705 }
706 
707 struct perf_pmu *perf_pmu__find(const char *name)
708 {
709 	struct perf_pmu *pmu;
710 
711 	/*
712 	 * Once PMU is loaded it stays in the list,
713 	 * so we keep us from multiple reading/parsing
714 	 * the pmu format definitions.
715 	 */
716 	pmu = pmu_find(name);
717 	if (pmu)
718 		return pmu;
719 
720 	return pmu_lookup(name);
721 }
722 
723 static struct perf_pmu_format *
724 pmu_find_format(struct list_head *formats, const char *name)
725 {
726 	struct perf_pmu_format *format;
727 
728 	list_for_each_entry(format, formats, list)
729 		if (!strcmp(format->name, name))
730 			return format;
731 
732 	return NULL;
733 }
734 
735 __u64 perf_pmu__format_bits(struct list_head *formats, const char *name)
736 {
737 	struct perf_pmu_format *format = pmu_find_format(formats, name);
738 	__u64 bits = 0;
739 	int fbit;
740 
741 	if (!format)
742 		return 0;
743 
744 	for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
745 		bits |= 1ULL << fbit;
746 
747 	return bits;
748 }
749 
750 /*
751  * Sets value based on the format definition (format parameter)
752  * and unformated value (value parameter).
753  */
754 static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
755 			     bool zero)
756 {
757 	unsigned long fbit, vbit;
758 
759 	for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
760 
761 		if (!test_bit(fbit, format))
762 			continue;
763 
764 		if (value & (1llu << vbit++))
765 			*v |= (1llu << fbit);
766 		else if (zero)
767 			*v &= ~(1llu << fbit);
768 	}
769 }
770 
771 static __u64 pmu_format_max_value(const unsigned long *format)
772 {
773 	__u64 w = 0;
774 	int fbit;
775 
776 	for_each_set_bit(fbit, format, PERF_PMU_FORMAT_BITS)
777 		w |= (1ULL << fbit);
778 
779 	return w;
780 }
781 
782 /*
783  * Term is a string term, and might be a param-term. Try to look up it's value
784  * in the remaining terms.
785  * - We have a term like "base-or-format-term=param-term",
786  * - We need to find the value supplied for "param-term" (with param-term named
787  *   in a config string) later on in the term list.
788  */
789 static int pmu_resolve_param_term(struct parse_events_term *term,
790 				  struct list_head *head_terms,
791 				  __u64 *value)
792 {
793 	struct parse_events_term *t;
794 
795 	list_for_each_entry(t, head_terms, list) {
796 		if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
797 			if (!strcmp(t->config, term->config)) {
798 				t->used = true;
799 				*value = t->val.num;
800 				return 0;
801 			}
802 		}
803 	}
804 
805 	if (verbose > 0)
806 		printf("Required parameter '%s' not specified\n", term->config);
807 
808 	return -1;
809 }
810 
811 static char *pmu_formats_string(struct list_head *formats)
812 {
813 	struct perf_pmu_format *format;
814 	char *str = NULL;
815 	struct strbuf buf = STRBUF_INIT;
816 	unsigned i = 0;
817 
818 	if (!formats)
819 		return NULL;
820 
821 	/* sysfs exported terms */
822 	list_for_each_entry(format, formats, list)
823 		if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
824 			goto error;
825 
826 	str = strbuf_detach(&buf, NULL);
827 error:
828 	strbuf_release(&buf);
829 
830 	return str;
831 }
832 
833 /*
834  * Setup one of config[12] attr members based on the
835  * user input data - term parameter.
836  */
837 static int pmu_config_term(struct list_head *formats,
838 			   struct perf_event_attr *attr,
839 			   struct parse_events_term *term,
840 			   struct list_head *head_terms,
841 			   bool zero, struct parse_events_error *err)
842 {
843 	struct perf_pmu_format *format;
844 	__u64 *vp;
845 	__u64 val, max_val;
846 
847 	/*
848 	 * If this is a parameter we've already used for parameterized-eval,
849 	 * skip it in normal eval.
850 	 */
851 	if (term->used)
852 		return 0;
853 
854 	/*
855 	 * Hardcoded terms should be already in, so nothing
856 	 * to be done for them.
857 	 */
858 	if (parse_events__is_hardcoded_term(term))
859 		return 0;
860 
861 	format = pmu_find_format(formats, term->config);
862 	if (!format) {
863 		if (verbose > 0)
864 			printf("Invalid event/parameter '%s'\n", term->config);
865 		if (err) {
866 			char *pmu_term = pmu_formats_string(formats);
867 
868 			err->idx  = term->err_term;
869 			err->str  = strdup("unknown term");
870 			err->help = parse_events_formats_error_string(pmu_term);
871 			free(pmu_term);
872 		}
873 		return -EINVAL;
874 	}
875 
876 	switch (format->value) {
877 	case PERF_PMU_FORMAT_VALUE_CONFIG:
878 		vp = &attr->config;
879 		break;
880 	case PERF_PMU_FORMAT_VALUE_CONFIG1:
881 		vp = &attr->config1;
882 		break;
883 	case PERF_PMU_FORMAT_VALUE_CONFIG2:
884 		vp = &attr->config2;
885 		break;
886 	default:
887 		return -EINVAL;
888 	}
889 
890 	/*
891 	 * Either directly use a numeric term, or try to translate string terms
892 	 * using event parameters.
893 	 */
894 	if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
895 		if (term->no_value &&
896 		    bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
897 			if (err) {
898 				err->idx = term->err_val;
899 				err->str = strdup("no value assigned for term");
900 			}
901 			return -EINVAL;
902 		}
903 
904 		val = term->val.num;
905 	} else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
906 		if (strcmp(term->val.str, "?")) {
907 			if (verbose > 0) {
908 				pr_info("Invalid sysfs entry %s=%s\n",
909 						term->config, term->val.str);
910 			}
911 			if (err) {
912 				err->idx = term->err_val;
913 				err->str = strdup("expected numeric value");
914 			}
915 			return -EINVAL;
916 		}
917 
918 		if (pmu_resolve_param_term(term, head_terms, &val))
919 			return -EINVAL;
920 	} else
921 		return -EINVAL;
922 
923 	max_val = pmu_format_max_value(format->bits);
924 	if (val > max_val) {
925 		if (err) {
926 			err->idx = term->err_val;
927 			if (asprintf(&err->str,
928 				     "value too big for format, maximum is %llu",
929 				     (unsigned long long)max_val) < 0)
930 				err->str = strdup("value too big for format");
931 			return -EINVAL;
932 		}
933 		/*
934 		 * Assume we don't care if !err, in which case the value will be
935 		 * silently truncated.
936 		 */
937 	}
938 
939 	pmu_format_value(format->bits, val, vp, zero);
940 	return 0;
941 }
942 
943 int perf_pmu__config_terms(struct list_head *formats,
944 			   struct perf_event_attr *attr,
945 			   struct list_head *head_terms,
946 			   bool zero, struct parse_events_error *err)
947 {
948 	struct parse_events_term *term;
949 
950 	list_for_each_entry(term, head_terms, list) {
951 		if (pmu_config_term(formats, attr, term, head_terms,
952 				    zero, err))
953 			return -EINVAL;
954 	}
955 
956 	return 0;
957 }
958 
959 /*
960  * Configures event's 'attr' parameter based on the:
961  * 1) users input - specified in terms parameter
962  * 2) pmu format definitions - specified by pmu parameter
963  */
964 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
965 		     struct list_head *head_terms,
966 		     struct parse_events_error *err)
967 {
968 	bool zero = !!pmu->default_config;
969 
970 	attr->type = pmu->type;
971 	return perf_pmu__config_terms(&pmu->format, attr, head_terms,
972 				      zero, err);
973 }
974 
975 static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
976 					     struct parse_events_term *term)
977 {
978 	struct perf_pmu_alias *alias;
979 	char *name;
980 
981 	if (parse_events__is_hardcoded_term(term))
982 		return NULL;
983 
984 	if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
985 		if (term->val.num != 1)
986 			return NULL;
987 		if (pmu_find_format(&pmu->format, term->config))
988 			return NULL;
989 		name = term->config;
990 	} else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
991 		if (strcasecmp(term->config, "event"))
992 			return NULL;
993 		name = term->val.str;
994 	} else {
995 		return NULL;
996 	}
997 
998 	list_for_each_entry(alias, &pmu->aliases, list) {
999 		if (!strcasecmp(alias->name, name))
1000 			return alias;
1001 	}
1002 	return NULL;
1003 }
1004 
1005 
1006 static int check_info_data(struct perf_pmu_alias *alias,
1007 			   struct perf_pmu_info *info)
1008 {
1009 	/*
1010 	 * Only one term in event definition can
1011 	 * define unit, scale and snapshot, fail
1012 	 * if there's more than one.
1013 	 */
1014 	if ((info->unit && alias->unit[0]) ||
1015 	    (info->scale && alias->scale) ||
1016 	    (info->snapshot && alias->snapshot))
1017 		return -EINVAL;
1018 
1019 	if (alias->unit[0])
1020 		info->unit = alias->unit;
1021 
1022 	if (alias->scale)
1023 		info->scale = alias->scale;
1024 
1025 	if (alias->snapshot)
1026 		info->snapshot = alias->snapshot;
1027 
1028 	return 0;
1029 }
1030 
1031 /*
1032  * Find alias in the terms list and replace it with the terms
1033  * defined for the alias
1034  */
1035 int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
1036 			  struct perf_pmu_info *info)
1037 {
1038 	struct parse_events_term *term, *h;
1039 	struct perf_pmu_alias *alias;
1040 	int ret;
1041 
1042 	info->per_pkg = false;
1043 
1044 	/*
1045 	 * Mark unit and scale as not set
1046 	 * (different from default values, see below)
1047 	 */
1048 	info->unit     = NULL;
1049 	info->scale    = 0.0;
1050 	info->snapshot = false;
1051 	info->metric_expr = NULL;
1052 	info->metric_name = NULL;
1053 
1054 	list_for_each_entry_safe(term, h, head_terms, list) {
1055 		alias = pmu_find_alias(pmu, term);
1056 		if (!alias)
1057 			continue;
1058 		ret = pmu_alias_terms(alias, &term->list);
1059 		if (ret)
1060 			return ret;
1061 
1062 		ret = check_info_data(alias, info);
1063 		if (ret)
1064 			return ret;
1065 
1066 		if (alias->per_pkg)
1067 			info->per_pkg = true;
1068 		info->metric_expr = alias->metric_expr;
1069 		info->metric_name = alias->metric_name;
1070 
1071 		list_del(&term->list);
1072 		free(term);
1073 	}
1074 
1075 	/*
1076 	 * if no unit or scale foundin aliases, then
1077 	 * set defaults as for evsel
1078 	 * unit cannot left to NULL
1079 	 */
1080 	if (info->unit == NULL)
1081 		info->unit   = "";
1082 
1083 	if (info->scale == 0.0)
1084 		info->scale  = 1.0;
1085 
1086 	return 0;
1087 }
1088 
1089 int perf_pmu__new_format(struct list_head *list, char *name,
1090 			 int config, unsigned long *bits)
1091 {
1092 	struct perf_pmu_format *format;
1093 
1094 	format = zalloc(sizeof(*format));
1095 	if (!format)
1096 		return -ENOMEM;
1097 
1098 	format->name = strdup(name);
1099 	format->value = config;
1100 	memcpy(format->bits, bits, sizeof(format->bits));
1101 
1102 	list_add_tail(&format->list, list);
1103 	return 0;
1104 }
1105 
1106 void perf_pmu__set_format(unsigned long *bits, long from, long to)
1107 {
1108 	long b;
1109 
1110 	if (!to)
1111 		to = from;
1112 
1113 	memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
1114 	for (b = from; b <= to; b++)
1115 		set_bit(b, bits);
1116 }
1117 
1118 static int sub_non_neg(int a, int b)
1119 {
1120 	if (b > a)
1121 		return 0;
1122 	return a - b;
1123 }
1124 
1125 static char *format_alias(char *buf, int len, struct perf_pmu *pmu,
1126 			  struct perf_pmu_alias *alias)
1127 {
1128 	struct parse_events_term *term;
1129 	int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
1130 
1131 	list_for_each_entry(term, &alias->terms, list) {
1132 		if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1133 			used += snprintf(buf + used, sub_non_neg(len, used),
1134 					",%s=%s", term->config,
1135 					term->val.str);
1136 	}
1137 
1138 	if (sub_non_neg(len, used) > 0) {
1139 		buf[used] = '/';
1140 		used++;
1141 	}
1142 	if (sub_non_neg(len, used) > 0) {
1143 		buf[used] = '\0';
1144 		used++;
1145 	} else
1146 		buf[len - 1] = '\0';
1147 
1148 	return buf;
1149 }
1150 
1151 static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu,
1152 			     struct perf_pmu_alias *alias)
1153 {
1154 	snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name);
1155 	return buf;
1156 }
1157 
1158 struct sevent {
1159 	char *name;
1160 	char *desc;
1161 	char *topic;
1162 	char *str;
1163 	char *pmu;
1164 	char *metric_expr;
1165 	char *metric_name;
1166 };
1167 
1168 static int cmp_sevent(const void *a, const void *b)
1169 {
1170 	const struct sevent *as = a;
1171 	const struct sevent *bs = b;
1172 
1173 	/* Put extra events last */
1174 	if (!!as->desc != !!bs->desc)
1175 		return !!as->desc - !!bs->desc;
1176 	if (as->topic && bs->topic) {
1177 		int n = strcmp(as->topic, bs->topic);
1178 
1179 		if (n)
1180 			return n;
1181 	}
1182 	return strcmp(as->name, bs->name);
1183 }
1184 
1185 static void wordwrap(char *s, int start, int max, int corr)
1186 {
1187 	int column = start;
1188 	int n;
1189 
1190 	while (*s) {
1191 		int wlen = strcspn(s, " \t");
1192 
1193 		if (column + wlen >= max && column > start) {
1194 			printf("\n%*s", start, "");
1195 			column = start + corr;
1196 		}
1197 		n = printf("%s%.*s", column > start ? " " : "", wlen, s);
1198 		if (n <= 0)
1199 			break;
1200 		s += wlen;
1201 		column += n;
1202 		s = ltrim(s);
1203 	}
1204 }
1205 
1206 void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag,
1207 			bool long_desc, bool details_flag)
1208 {
1209 	struct perf_pmu *pmu;
1210 	struct perf_pmu_alias *alias;
1211 	char buf[1024];
1212 	int printed = 0;
1213 	int len, j;
1214 	struct sevent *aliases;
1215 	int numdesc = 0;
1216 	int columns = pager_get_columns();
1217 	char *topic = NULL;
1218 
1219 	pmu = NULL;
1220 	len = 0;
1221 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1222 		list_for_each_entry(alias, &pmu->aliases, list)
1223 			len++;
1224 		if (pmu->selectable)
1225 			len++;
1226 	}
1227 	aliases = zalloc(sizeof(struct sevent) * len);
1228 	if (!aliases)
1229 		goto out_enomem;
1230 	pmu = NULL;
1231 	j = 0;
1232 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1233 		list_for_each_entry(alias, &pmu->aliases, list) {
1234 			char *name = alias->desc ? alias->name :
1235 				format_alias(buf, sizeof(buf), pmu, alias);
1236 			bool is_cpu = !strcmp(pmu->name, "cpu");
1237 
1238 			if (event_glob != NULL &&
1239 			    !(strglobmatch_nocase(name, event_glob) ||
1240 			      (!is_cpu && strglobmatch_nocase(alias->name,
1241 						       event_glob)) ||
1242 			      (alias->topic &&
1243 			       strglobmatch_nocase(alias->topic, event_glob))))
1244 				continue;
1245 
1246 			if (is_cpu && !name_only && !alias->desc)
1247 				name = format_alias_or(buf, sizeof(buf), pmu, alias);
1248 
1249 			aliases[j].name = name;
1250 			if (is_cpu && !name_only && !alias->desc)
1251 				aliases[j].name = format_alias_or(buf,
1252 								  sizeof(buf),
1253 								  pmu, alias);
1254 			aliases[j].name = strdup(aliases[j].name);
1255 			if (!aliases[j].name)
1256 				goto out_enomem;
1257 
1258 			aliases[j].desc = long_desc ? alias->long_desc :
1259 						alias->desc;
1260 			aliases[j].topic = alias->topic;
1261 			aliases[j].str = alias->str;
1262 			aliases[j].pmu = pmu->name;
1263 			aliases[j].metric_expr = alias->metric_expr;
1264 			aliases[j].metric_name = alias->metric_name;
1265 			j++;
1266 		}
1267 		if (pmu->selectable &&
1268 		    (event_glob == NULL || strglobmatch(pmu->name, event_glob))) {
1269 			char *s;
1270 			if (asprintf(&s, "%s//", pmu->name) < 0)
1271 				goto out_enomem;
1272 			aliases[j].name = s;
1273 			j++;
1274 		}
1275 	}
1276 	len = j;
1277 	qsort(aliases, len, sizeof(struct sevent), cmp_sevent);
1278 	for (j = 0; j < len; j++) {
1279 		/* Skip duplicates */
1280 		if (j > 0 && !strcmp(aliases[j].name, aliases[j - 1].name))
1281 			continue;
1282 		if (name_only) {
1283 			printf("%s ", aliases[j].name);
1284 			continue;
1285 		}
1286 		if (aliases[j].desc && !quiet_flag) {
1287 			if (numdesc++ == 0)
1288 				printf("\n");
1289 			if (aliases[j].topic && (!topic ||
1290 					strcmp(topic, aliases[j].topic))) {
1291 				printf("%s%s:\n", topic ? "\n" : "",
1292 						aliases[j].topic);
1293 				topic = aliases[j].topic;
1294 			}
1295 			printf("  %-50s\n", aliases[j].name);
1296 			printf("%*s", 8, "[");
1297 			wordwrap(aliases[j].desc, 8, columns, 0);
1298 			printf("]\n");
1299 			if (details_flag) {
1300 				printf("%*s%s/%s/ ", 8, "", aliases[j].pmu, aliases[j].str);
1301 				if (aliases[j].metric_name)
1302 					printf(" MetricName: %s", aliases[j].metric_name);
1303 				if (aliases[j].metric_expr)
1304 					printf(" MetricExpr: %s", aliases[j].metric_expr);
1305 				putchar('\n');
1306 			}
1307 		} else
1308 			printf("  %-50s [Kernel PMU event]\n", aliases[j].name);
1309 		printed++;
1310 	}
1311 	if (printed && pager_in_use())
1312 		printf("\n");
1313 out_free:
1314 	for (j = 0; j < len; j++)
1315 		zfree(&aliases[j].name);
1316 	zfree(&aliases);
1317 	return;
1318 
1319 out_enomem:
1320 	printf("FATAL: not enough memory to print PMU events\n");
1321 	if (aliases)
1322 		goto out_free;
1323 }
1324 
1325 bool pmu_have_event(const char *pname, const char *name)
1326 {
1327 	struct perf_pmu *pmu;
1328 	struct perf_pmu_alias *alias;
1329 
1330 	pmu = NULL;
1331 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1332 		if (strcmp(pname, pmu->name))
1333 			continue;
1334 		list_for_each_entry(alias, &pmu->aliases, list)
1335 			if (!strcmp(alias->name, name))
1336 				return true;
1337 	}
1338 	return false;
1339 }
1340 
1341 static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
1342 {
1343 	struct stat st;
1344 	char path[PATH_MAX];
1345 	const char *sysfs;
1346 
1347 	sysfs = sysfs__mountpoint();
1348 	if (!sysfs)
1349 		return NULL;
1350 
1351 	snprintf(path, PATH_MAX,
1352 		 "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name);
1353 
1354 	if (stat(path, &st) < 0)
1355 		return NULL;
1356 
1357 	return fopen(path, "r");
1358 }
1359 
1360 int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
1361 			...)
1362 {
1363 	va_list args;
1364 	FILE *file;
1365 	int ret = EOF;
1366 
1367 	va_start(args, fmt);
1368 	file = perf_pmu__open_file(pmu, name);
1369 	if (file) {
1370 		ret = vfscanf(file, fmt, args);
1371 		fclose(file);
1372 	}
1373 	va_end(args);
1374 	return ret;
1375 }
1376