xref: /openbmc/linux/tools/perf/util/pmu.c (revision 0c6dfa75)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/list.h>
3 #include <linux/compiler.h>
4 #include <linux/string.h>
5 #include <linux/zalloc.h>
6 #include <linux/ctype.h>
7 #include <sys/types.h>
8 #include <fcntl.h>
9 #include <sys/stat.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <stdbool.h>
13 #include <dirent.h>
14 #include <api/fs/fs.h>
15 #include <locale.h>
16 #include <fnmatch.h>
17 #include <math.h>
18 #include "debug.h"
19 #include "evsel.h"
20 #include "pmu.h"
21 #include "pmus.h"
22 #include <util/pmu-bison.h>
23 #include <util/pmu-flex.h>
24 #include "parse-events.h"
25 #include "print-events.h"
26 #include "header.h"
27 #include "string2.h"
28 #include "strbuf.h"
29 #include "fncache.h"
30 #include "util/evsel_config.h"
31 
32 struct perf_pmu perf_pmu__fake = {
33 	.name = "fake",
34 };
35 
36 #define UNIT_MAX_LEN	31 /* max length for event unit name */
37 
38 /**
39  * struct perf_pmu_alias - An event either read from sysfs or builtin in
40  * pmu-events.c, created by parsing the pmu-events json files.
41  */
42 struct perf_pmu_alias {
43 	/** @name: Name of the event like "mem-loads". */
44 	char *name;
45 	/** @desc: Optional short description of the event. */
46 	char *desc;
47 	/** @long_desc: Optional long description. */
48 	char *long_desc;
49 	/**
50 	 * @topic: Optional topic such as cache or pipeline, particularly for
51 	 * json events.
52 	 */
53 	char *topic;
54 	/** @terms: Owned list of the original parsed parameters. */
55 	struct list_head terms;
56 	/** @list: List element of struct perf_pmu aliases. */
57 	struct list_head list;
58 	/**
59 	 * @pmu_name: The name copied from the json struct pmu_event. This can
60 	 * differ from the PMU name as it won't have suffixes.
61 	 */
62 	char *pmu_name;
63 	/** @unit: Units for the event, such as bytes or cache lines. */
64 	char unit[UNIT_MAX_LEN+1];
65 	/** @scale: Value to scale read counter values by. */
66 	double scale;
67 	/**
68 	 * @per_pkg: Does the file
69 	 * <sysfs>/bus/event_source/devices/<pmu_name>/events/<name>.per-pkg or
70 	 * equivalent json value exist and have the value 1.
71 	 */
72 	bool per_pkg;
73 	/**
74 	 * @snapshot: Does the file
75 	 * <sysfs>/bus/event_source/devices/<pmu_name>/events/<name>.snapshot
76 	 * exist and have the value 1.
77 	 */
78 	bool snapshot;
79 	/**
80 	 * @deprecated: Is the event hidden and so not shown in perf list by
81 	 * default.
82 	 */
83 	bool deprecated;
84 	/** @from_sysfs: Was the alias from sysfs or a json event? */
85 	bool from_sysfs;
86 	/** @info_loaded: Have the scale, unit and other values been read from disk? */
87 	bool info_loaded;
88 };
89 
90 /**
91  * struct perf_pmu_format - Values from a format file read from
92  * <sysfs>/devices/cpu/format/ held in struct perf_pmu.
93  *
94  * For example, the contents of <sysfs>/devices/cpu/format/event may be
95  * "config:0-7" and will be represented here as name="event",
96  * value=PERF_PMU_FORMAT_VALUE_CONFIG and bits 0 to 7 will be set.
97  */
98 struct perf_pmu_format {
99 	/** @list: Element on list within struct perf_pmu. */
100 	struct list_head list;
101 	/** @bits: Which config bits are set by this format value. */
102 	DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
103 	/** @name: The modifier/file name. */
104 	char *name;
105 	/**
106 	 * @value : Which config value the format relates to. Supported values
107 	 * are from PERF_PMU_FORMAT_VALUE_CONFIG to
108 	 * PERF_PMU_FORMAT_VALUE_CONFIG_END.
109 	 */
110 	u16 value;
111 	/** @loaded: Has the contents been loaded/parsed. */
112 	bool loaded;
113 };
114 
115 static int pmu_aliases_parse(struct perf_pmu *pmu);
116 
117 static struct perf_pmu_format *perf_pmu__new_format(struct list_head *list, char *name)
118 {
119 	struct perf_pmu_format *format;
120 
121 	format = zalloc(sizeof(*format));
122 	if (!format)
123 		return NULL;
124 
125 	format->name = strdup(name);
126 	if (!format->name) {
127 		free(format);
128 		return NULL;
129 	}
130 	list_add_tail(&format->list, list);
131 	return format;
132 }
133 
134 /* Called at the end of parsing a format. */
135 void perf_pmu_format__set_value(void *vformat, int config, unsigned long *bits)
136 {
137 	struct perf_pmu_format *format = vformat;
138 
139 	format->value = config;
140 	memcpy(format->bits, bits, sizeof(format->bits));
141 }
142 
143 static void __perf_pmu_format__load(struct perf_pmu_format *format, FILE *file)
144 {
145 	void *scanner;
146 	int ret;
147 
148 	ret = perf_pmu_lex_init(&scanner);
149 	if (ret)
150 		return;
151 
152 	perf_pmu_set_in(file, scanner);
153 	ret = perf_pmu_parse(format, scanner);
154 	perf_pmu_lex_destroy(scanner);
155 	format->loaded = true;
156 }
157 
158 static void perf_pmu_format__load(struct perf_pmu *pmu, struct perf_pmu_format *format)
159 {
160 	char path[PATH_MAX];
161 	FILE *file = NULL;
162 
163 	if (format->loaded)
164 		return;
165 
166 	if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, "format"))
167 		return;
168 
169 	assert(strlen(path) + strlen(format->name) + 2 < sizeof(path));
170 	strcat(path, "/");
171 	strcat(path, format->name);
172 
173 	file = fopen(path, "r");
174 	if (!file)
175 		return;
176 	__perf_pmu_format__load(format, file);
177 	fclose(file);
178 }
179 
180 /*
181  * Parse & process all the sysfs attributes located under
182  * the directory specified in 'dir' parameter.
183  */
184 int perf_pmu__format_parse(struct perf_pmu *pmu, int dirfd, bool eager_load)
185 {
186 	struct dirent *evt_ent;
187 	DIR *format_dir;
188 	int ret = 0;
189 
190 	format_dir = fdopendir(dirfd);
191 	if (!format_dir)
192 		return -EINVAL;
193 
194 	while ((evt_ent = readdir(format_dir)) != NULL) {
195 		struct perf_pmu_format *format;
196 		char *name = evt_ent->d_name;
197 
198 		if (!strcmp(name, ".") || !strcmp(name, ".."))
199 			continue;
200 
201 		format = perf_pmu__new_format(&pmu->format, name);
202 		if (!format) {
203 			ret = -ENOMEM;
204 			break;
205 		}
206 
207 		if (eager_load) {
208 			FILE *file;
209 			int fd = openat(dirfd, name, O_RDONLY);
210 
211 			if (fd < 0) {
212 				ret = -errno;
213 				break;
214 			}
215 			file = fdopen(fd, "r");
216 			if (!file) {
217 				close(fd);
218 				break;
219 			}
220 			__perf_pmu_format__load(format, file);
221 			fclose(file);
222 		}
223 	}
224 
225 	closedir(format_dir);
226 	return ret;
227 }
228 
229 /*
230  * Reading/parsing the default pmu format definition, which should be
231  * located at:
232  * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
233  */
234 static int pmu_format(struct perf_pmu *pmu, int dirfd, const char *name)
235 {
236 	int fd;
237 
238 	fd = perf_pmu__pathname_fd(dirfd, name, "format", O_DIRECTORY);
239 	if (fd < 0)
240 		return 0;
241 
242 	/* it'll close the fd */
243 	if (perf_pmu__format_parse(pmu, fd, /*eager_load=*/false))
244 		return -1;
245 
246 	return 0;
247 }
248 
249 int perf_pmu__convert_scale(const char *scale, char **end, double *sval)
250 {
251 	char *lc;
252 	int ret = 0;
253 
254 	/*
255 	 * save current locale
256 	 */
257 	lc = setlocale(LC_NUMERIC, NULL);
258 
259 	/*
260 	 * The lc string may be allocated in static storage,
261 	 * so get a dynamic copy to make it survive setlocale
262 	 * call below.
263 	 */
264 	lc = strdup(lc);
265 	if (!lc) {
266 		ret = -ENOMEM;
267 		goto out;
268 	}
269 
270 	/*
271 	 * force to C locale to ensure kernel
272 	 * scale string is converted correctly.
273 	 * kernel uses default C locale.
274 	 */
275 	setlocale(LC_NUMERIC, "C");
276 
277 	*sval = strtod(scale, end);
278 
279 out:
280 	/* restore locale */
281 	setlocale(LC_NUMERIC, lc);
282 	free(lc);
283 	return ret;
284 }
285 
286 static int perf_pmu__parse_scale(struct perf_pmu *pmu, struct perf_pmu_alias *alias)
287 {
288 	struct stat st;
289 	ssize_t sret;
290 	size_t len;
291 	char scale[128];
292 	int fd, ret = -1;
293 	char path[PATH_MAX];
294 
295 	len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
296 	if (!len)
297 		return 0;
298 	scnprintf(path + len, sizeof(path) - len, "%s/events/%s.scale", pmu->name, alias->name);
299 
300 	fd = open(path, O_RDONLY);
301 	if (fd == -1)
302 		return -1;
303 
304 	if (fstat(fd, &st) < 0)
305 		goto error;
306 
307 	sret = read(fd, scale, sizeof(scale)-1);
308 	if (sret < 0)
309 		goto error;
310 
311 	if (scale[sret - 1] == '\n')
312 		scale[sret - 1] = '\0';
313 	else
314 		scale[sret] = '\0';
315 
316 	ret = perf_pmu__convert_scale(scale, NULL, &alias->scale);
317 error:
318 	close(fd);
319 	return ret;
320 }
321 
322 static int perf_pmu__parse_unit(struct perf_pmu *pmu, struct perf_pmu_alias *alias)
323 {
324 	char path[PATH_MAX];
325 	size_t len;
326 	ssize_t sret;
327 	int fd;
328 
329 
330 	len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
331 	if (!len)
332 		return 0;
333 	scnprintf(path + len, sizeof(path) - len, "%s/events/%s.unit", pmu->name, alias->name);
334 
335 	fd = open(path, O_RDONLY);
336 	if (fd == -1)
337 		return -1;
338 
339 	sret = read(fd, alias->unit, UNIT_MAX_LEN);
340 	if (sret < 0)
341 		goto error;
342 
343 	close(fd);
344 
345 	if (alias->unit[sret - 1] == '\n')
346 		alias->unit[sret - 1] = '\0';
347 	else
348 		alias->unit[sret] = '\0';
349 
350 	return 0;
351 error:
352 	close(fd);
353 	alias->unit[0] = '\0';
354 	return -1;
355 }
356 
357 static int
358 perf_pmu__parse_per_pkg(struct perf_pmu *pmu, struct perf_pmu_alias *alias)
359 {
360 	char path[PATH_MAX];
361 	size_t len;
362 	int fd;
363 
364 	len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
365 	if (!len)
366 		return 0;
367 	scnprintf(path + len, sizeof(path) - len, "%s/events/%s.per-pkg", pmu->name, alias->name);
368 
369 	fd = open(path, O_RDONLY);
370 	if (fd == -1)
371 		return -1;
372 
373 	close(fd);
374 
375 	alias->per_pkg = true;
376 	return 0;
377 }
378 
379 static int perf_pmu__parse_snapshot(struct perf_pmu *pmu, struct perf_pmu_alias *alias)
380 {
381 	char path[PATH_MAX];
382 	size_t len;
383 	int fd;
384 
385 	len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
386 	if (!len)
387 		return 0;
388 	scnprintf(path + len, sizeof(path) - len, "%s/events/%s.snapshot", pmu->name, alias->name);
389 
390 	fd = open(path, O_RDONLY);
391 	if (fd == -1)
392 		return -1;
393 
394 	alias->snapshot = true;
395 	close(fd);
396 	return 0;
397 }
398 
399 /* Delete an alias entry. */
400 static void perf_pmu_free_alias(struct perf_pmu_alias *newalias)
401 {
402 	zfree(&newalias->name);
403 	zfree(&newalias->desc);
404 	zfree(&newalias->long_desc);
405 	zfree(&newalias->topic);
406 	zfree(&newalias->pmu_name);
407 	parse_events_terms__purge(&newalias->terms);
408 	free(newalias);
409 }
410 
411 static void perf_pmu__del_aliases(struct perf_pmu *pmu)
412 {
413 	struct perf_pmu_alias *alias, *tmp;
414 
415 	list_for_each_entry_safe(alias, tmp, &pmu->aliases, list) {
416 		list_del(&alias->list);
417 		perf_pmu_free_alias(alias);
418 	}
419 }
420 
421 static struct perf_pmu_alias *perf_pmu__find_alias(struct perf_pmu *pmu,
422 						   const char *name,
423 						   bool load)
424 {
425 	struct perf_pmu_alias *alias;
426 
427 	if (load && !pmu->sysfs_aliases_loaded)
428 		pmu_aliases_parse(pmu);
429 
430 	list_for_each_entry(alias, &pmu->aliases, list) {
431 		if (!strcasecmp(alias->name, name))
432 			return alias;
433 	}
434 	return NULL;
435 }
436 
437 static bool assign_str(const char *name, const char *field, char **old_str,
438 				const char *new_str)
439 {
440 	if (!*old_str && new_str) {
441 		*old_str = strdup(new_str);
442 		return true;
443 	}
444 
445 	if (!new_str || !strcasecmp(*old_str, new_str))
446 		return false; /* Nothing to update. */
447 
448 	pr_debug("alias %s differs in field '%s' ('%s' != '%s')\n",
449 		name, field, *old_str, new_str);
450 	zfree(old_str);
451 	*old_str = strdup(new_str);
452 	return true;
453 }
454 
455 static void read_alias_info(struct perf_pmu *pmu, struct perf_pmu_alias *alias)
456 {
457 	if (!alias->from_sysfs || alias->info_loaded)
458 		return;
459 
460 	/*
461 	 * load unit name and scale if available
462 	 */
463 	perf_pmu__parse_unit(pmu, alias);
464 	perf_pmu__parse_scale(pmu, alias);
465 	perf_pmu__parse_per_pkg(pmu, alias);
466 	perf_pmu__parse_snapshot(pmu, alias);
467 }
468 
469 struct update_alias_data {
470 	struct perf_pmu *pmu;
471 	struct perf_pmu_alias *alias;
472 };
473 
474 static int update_alias(const struct pmu_event *pe,
475 			const struct pmu_events_table *table __maybe_unused,
476 			void *vdata)
477 {
478 	struct update_alias_data *data = vdata;
479 	int ret = 0;
480 
481 	read_alias_info(data->pmu, data->alias);
482 	assign_str(pe->name, "desc", &data->alias->desc, pe->desc);
483 	assign_str(pe->name, "long_desc", &data->alias->long_desc, pe->long_desc);
484 	assign_str(pe->name, "topic", &data->alias->topic, pe->topic);
485 	data->alias->per_pkg = pe->perpkg;
486 	if (pe->event) {
487 		parse_events_terms__purge(&data->alias->terms);
488 		ret = parse_events_terms(&data->alias->terms, pe->event, /*input=*/NULL);
489 	}
490 	if (!ret && pe->unit) {
491 		char *unit;
492 
493 		ret = perf_pmu__convert_scale(pe->unit, &unit, &data->alias->scale);
494 		if (!ret)
495 			snprintf(data->alias->unit, sizeof(data->alias->unit), "%s", unit);
496 	}
497 	return ret;
498 }
499 
500 static int perf_pmu__new_alias(struct perf_pmu *pmu, const char *name,
501 				const char *desc, const char *val, FILE *val_fd,
502 				const struct pmu_event *pe)
503 {
504 	struct perf_pmu_alias *alias;
505 	int ret;
506 	const char *long_desc = NULL, *topic = NULL, *unit = NULL, *pmu_name = NULL;
507 	bool deprecated = false, perpkg = false;
508 
509 	if (perf_pmu__find_alias(pmu, name, /*load=*/ false)) {
510 		/* Alias was already created/loaded. */
511 		return 0;
512 	}
513 
514 	if (pe) {
515 		long_desc = pe->long_desc;
516 		topic = pe->topic;
517 		unit = pe->unit;
518 		perpkg = pe->perpkg;
519 		deprecated = pe->deprecated;
520 		pmu_name = pe->pmu;
521 	}
522 
523 	alias = zalloc(sizeof(*alias));
524 	if (!alias)
525 		return -ENOMEM;
526 
527 	INIT_LIST_HEAD(&alias->terms);
528 	alias->scale = 1.0;
529 	alias->unit[0] = '\0';
530 	alias->per_pkg = perpkg;
531 	alias->snapshot = false;
532 	alias->deprecated = deprecated;
533 
534 	ret = parse_events_terms(&alias->terms, val, val_fd);
535 	if (ret) {
536 		pr_err("Cannot parse alias %s: %d\n", val, ret);
537 		free(alias);
538 		return ret;
539 	}
540 
541 	alias->name = strdup(name);
542 	alias->desc = desc ? strdup(desc) : NULL;
543 	alias->long_desc = long_desc ? strdup(long_desc) :
544 				desc ? strdup(desc) : NULL;
545 	alias->topic = topic ? strdup(topic) : NULL;
546 	alias->pmu_name = pmu_name ? strdup(pmu_name) : NULL;
547 	if (unit) {
548 		if (perf_pmu__convert_scale(unit, (char **)&unit, &alias->scale) < 0) {
549 			perf_pmu_free_alias(alias);
550 			return -1;
551 		}
552 		snprintf(alias->unit, sizeof(alias->unit), "%s", unit);
553 	}
554 	if (!pe) {
555 		/* Update an event from sysfs with json data. */
556 		struct update_alias_data data = {
557 			.pmu = pmu,
558 			.alias = alias,
559 		};
560 
561 		alias->from_sysfs = true;
562 		if (pmu->events_table) {
563 			if (pmu_events_table__find_event(pmu->events_table, pmu, name,
564 							 update_alias, &data) == 0)
565 				pmu->loaded_json_aliases++;
566 		}
567 	}
568 
569 	if (!pe)
570 		pmu->sysfs_aliases++;
571 	else
572 		pmu->loaded_json_aliases++;
573 	list_add_tail(&alias->list, &pmu->aliases);
574 	return 0;
575 }
576 
577 static inline bool pmu_alias_info_file(char *name)
578 {
579 	size_t len;
580 
581 	len = strlen(name);
582 	if (len > 5 && !strcmp(name + len - 5, ".unit"))
583 		return true;
584 	if (len > 6 && !strcmp(name + len - 6, ".scale"))
585 		return true;
586 	if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
587 		return true;
588 	if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
589 		return true;
590 
591 	return false;
592 }
593 
594 /*
595  * Reading the pmu event aliases definition, which should be located at:
596  * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
597  */
598 static int pmu_aliases_parse(struct perf_pmu *pmu)
599 {
600 	char path[PATH_MAX];
601 	struct dirent *evt_ent;
602 	DIR *event_dir;
603 	size_t len;
604 	int fd, dir_fd;
605 
606 	len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
607 	if (!len)
608 		return 0;
609 	scnprintf(path + len, sizeof(path) - len, "%s/events", pmu->name);
610 
611 	dir_fd = open(path, O_DIRECTORY);
612 	if (dir_fd == -1) {
613 		pmu->sysfs_aliases_loaded = true;
614 		return 0;
615 	}
616 
617 	event_dir = fdopendir(dir_fd);
618 	if (!event_dir){
619 		close (dir_fd);
620 		return -EINVAL;
621 	}
622 
623 	while ((evt_ent = readdir(event_dir))) {
624 		char *name = evt_ent->d_name;
625 		FILE *file;
626 
627 		if (!strcmp(name, ".") || !strcmp(name, ".."))
628 			continue;
629 
630 		/*
631 		 * skip info files parsed in perf_pmu__new_alias()
632 		 */
633 		if (pmu_alias_info_file(name))
634 			continue;
635 
636 		fd = openat(dir_fd, name, O_RDONLY);
637 		if (fd == -1) {
638 			pr_debug("Cannot open %s\n", name);
639 			continue;
640 		}
641 		file = fdopen(fd, "r");
642 		if (!file) {
643 			close(fd);
644 			continue;
645 		}
646 
647 		if (perf_pmu__new_alias(pmu, name, /*desc=*/ NULL,
648 					/*val=*/ NULL, file, /*pe=*/ NULL) < 0)
649 			pr_debug("Cannot set up %s\n", name);
650 		fclose(file);
651 	}
652 
653 	closedir(event_dir);
654 	close (dir_fd);
655 	pmu->sysfs_aliases_loaded = true;
656 	return 0;
657 }
658 
659 static int pmu_alias_terms(struct perf_pmu_alias *alias,
660 			   struct list_head *terms)
661 {
662 	struct parse_events_term *term, *cloned;
663 	LIST_HEAD(list);
664 	int ret;
665 
666 	list_for_each_entry(term, &alias->terms, list) {
667 		ret = parse_events_term__clone(&cloned, term);
668 		if (ret) {
669 			parse_events_terms__purge(&list);
670 			return ret;
671 		}
672 		/*
673 		 * Weak terms don't override command line options,
674 		 * which we don't want for implicit terms in aliases.
675 		 */
676 		cloned->weak = true;
677 		list_add_tail(&cloned->list, &list);
678 	}
679 	list_splice(&list, terms);
680 	return 0;
681 }
682 
683 /*
684  * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64)
685  * may have a "cpus" file.
686  */
687 static struct perf_cpu_map *pmu_cpumask(int dirfd, const char *name, bool is_core)
688 {
689 	struct perf_cpu_map *cpus;
690 	const char *templates[] = {
691 		"cpumask",
692 		"cpus",
693 		NULL
694 	};
695 	const char **template;
696 	char pmu_name[PATH_MAX];
697 	struct perf_pmu pmu = {.name = pmu_name};
698 	FILE *file;
699 
700 	strlcpy(pmu_name, name, sizeof(pmu_name));
701 	for (template = templates; *template; template++) {
702 		file = perf_pmu__open_file_at(&pmu, dirfd, *template);
703 		if (!file)
704 			continue;
705 		cpus = perf_cpu_map__read(file);
706 		fclose(file);
707 		if (cpus)
708 			return cpus;
709 	}
710 
711 	/* Nothing found, for core PMUs assume this means all CPUs. */
712 	return is_core ? perf_cpu_map__get(cpu_map__online()) : NULL;
713 }
714 
715 static bool pmu_is_uncore(int dirfd, const char *name)
716 {
717 	int fd;
718 
719 	fd = perf_pmu__pathname_fd(dirfd, name, "cpumask", O_PATH);
720 	if (fd < 0)
721 		return false;
722 
723 	close(fd);
724 	return true;
725 }
726 
727 static char *pmu_id(const char *name)
728 {
729 	char path[PATH_MAX], *str;
730 	size_t len;
731 
732 	perf_pmu__pathname_scnprintf(path, sizeof(path), name, "identifier");
733 
734 	if (filename__read_str(path, &str, &len) < 0)
735 		return NULL;
736 
737 	str[len - 1] = 0; /* remove line feed */
738 
739 	return str;
740 }
741 
742 /**
743  * is_sysfs_pmu_core() - PMU CORE devices have different name other than cpu in
744  *         sysfs on some platforms like ARM or Intel hybrid. Looking for
745  *         possible the cpus file in sysfs files to identify whether this is a
746  *         core device.
747  * @name: The PMU name such as "cpu_atom".
748  */
749 static int is_sysfs_pmu_core(const char *name)
750 {
751 	char path[PATH_MAX];
752 
753 	if (!perf_pmu__pathname_scnprintf(path, sizeof(path), name, "cpus"))
754 		return 0;
755 	return file_available(path);
756 }
757 
758 char *perf_pmu__getcpuid(struct perf_pmu *pmu)
759 {
760 	char *cpuid;
761 	static bool printed;
762 
763 	cpuid = getenv("PERF_CPUID");
764 	if (cpuid)
765 		cpuid = strdup(cpuid);
766 	if (!cpuid)
767 		cpuid = get_cpuid_str(pmu);
768 	if (!cpuid)
769 		return NULL;
770 
771 	if (!printed) {
772 		pr_debug("Using CPUID %s\n", cpuid);
773 		printed = true;
774 	}
775 	return cpuid;
776 }
777 
778 __weak const struct pmu_events_table *pmu_events_table__find(void)
779 {
780 	return perf_pmu__find_events_table(NULL);
781 }
782 
783 __weak const struct pmu_metrics_table *pmu_metrics_table__find(void)
784 {
785 	return perf_pmu__find_metrics_table(NULL);
786 }
787 
788 /**
789  * perf_pmu__match_ignoring_suffix - Does the pmu_name match tok ignoring any
790  *                                   trailing suffix? The Suffix must be in form
791  *                                   tok_{digits}, or tok{digits}.
792  * @pmu_name: The pmu_name with possible suffix.
793  * @tok: The possible match to pmu_name without suffix.
794  */
795 static bool perf_pmu__match_ignoring_suffix(const char *pmu_name, const char *tok)
796 {
797 	const char *p;
798 
799 	if (strncmp(pmu_name, tok, strlen(tok)))
800 		return false;
801 
802 	p = pmu_name + strlen(tok);
803 	if (*p == 0)
804 		return true;
805 
806 	if (*p == '_')
807 		++p;
808 
809 	/* Ensure we end in a number */
810 	while (1) {
811 		if (!isdigit(*p))
812 			return false;
813 		if (*(++p) == 0)
814 			break;
815 	}
816 
817 	return true;
818 }
819 
820 /**
821  * pmu_uncore_alias_match - does name match the PMU name?
822  * @pmu_name: the json struct pmu_event name. This may lack a suffix (which
823  *            matches) or be of the form "socket,pmuname" which will match
824  *            "socketX_pmunameY".
825  * @name: a real full PMU name as from sysfs.
826  */
827 static bool pmu_uncore_alias_match(const char *pmu_name, const char *name)
828 {
829 	char *tmp = NULL, *tok, *str;
830 	bool res;
831 
832 	if (strchr(pmu_name, ',') == NULL)
833 		return perf_pmu__match_ignoring_suffix(name, pmu_name);
834 
835 	str = strdup(pmu_name);
836 	if (!str)
837 		return false;
838 
839 	/*
840 	 * uncore alias may be from different PMU with common prefix
841 	 */
842 	tok = strtok_r(str, ",", &tmp);
843 	if (strncmp(pmu_name, tok, strlen(tok))) {
844 		res = false;
845 		goto out;
846 	}
847 
848 	/*
849 	 * Match more complex aliases where the alias name is a comma-delimited
850 	 * list of tokens, orderly contained in the matching PMU name.
851 	 *
852 	 * Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we
853 	 *	    match "socket" in "socketX_pmunameY" and then "pmuname" in
854 	 *	    "pmunameY".
855 	 */
856 	while (1) {
857 		char *next_tok = strtok_r(NULL, ",", &tmp);
858 
859 		name = strstr(name, tok);
860 		if (!name ||
861 		    (!next_tok && !perf_pmu__match_ignoring_suffix(name, tok))) {
862 			res = false;
863 			goto out;
864 		}
865 		if (!next_tok)
866 			break;
867 		tok = next_tok;
868 		name += strlen(tok);
869 	}
870 
871 	res = true;
872 out:
873 	free(str);
874 	return res;
875 }
876 
877 static int pmu_add_cpu_aliases_map_callback(const struct pmu_event *pe,
878 					const struct pmu_events_table *table __maybe_unused,
879 					void *vdata)
880 {
881 	struct perf_pmu *pmu = vdata;
882 
883 	perf_pmu__new_alias(pmu, pe->name, pe->desc, pe->event, /*val_fd=*/ NULL, pe);
884 	return 0;
885 }
886 
887 /*
888  * From the pmu_events_table, find the events that correspond to the given
889  * PMU and add them to the list 'head'.
890  */
891 void pmu_add_cpu_aliases_table(struct perf_pmu *pmu, const struct pmu_events_table *table)
892 {
893 	pmu_events_table__for_each_event(table, pmu, pmu_add_cpu_aliases_map_callback, pmu);
894 }
895 
896 static void pmu_add_cpu_aliases(struct perf_pmu *pmu)
897 {
898 	if (!pmu->events_table)
899 		return;
900 
901 	if (pmu->cpu_aliases_added)
902 		return;
903 
904 	pmu_add_cpu_aliases_table(pmu, pmu->events_table);
905 	pmu->cpu_aliases_added = true;
906 }
907 
908 static int pmu_add_sys_aliases_iter_fn(const struct pmu_event *pe,
909 				       const struct pmu_events_table *table __maybe_unused,
910 				       void *vdata)
911 {
912 	struct perf_pmu *pmu = vdata;
913 
914 	if (!pe->compat || !pe->pmu)
915 		return 0;
916 
917 	if (!strcmp(pmu->id, pe->compat) &&
918 	    pmu_uncore_alias_match(pe->pmu, pmu->name)) {
919 		perf_pmu__new_alias(pmu,
920 				pe->name,
921 				pe->desc,
922 				pe->event,
923 				/*val_fd=*/ NULL,
924 				pe);
925 	}
926 
927 	return 0;
928 }
929 
930 void pmu_add_sys_aliases(struct perf_pmu *pmu)
931 {
932 	if (!pmu->id)
933 		return;
934 
935 	pmu_for_each_sys_event(pmu_add_sys_aliases_iter_fn, pmu);
936 }
937 
938 struct perf_event_attr * __weak
939 perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
940 {
941 	return NULL;
942 }
943 
944 const char * __weak
945 pmu_find_real_name(const char *name)
946 {
947 	return name;
948 }
949 
950 const char * __weak
951 pmu_find_alias_name(const char *name __maybe_unused)
952 {
953 	return NULL;
954 }
955 
956 static int pmu_max_precise(int dirfd, struct perf_pmu *pmu)
957 {
958 	int max_precise = -1;
959 
960 	perf_pmu__scan_file_at(pmu, dirfd, "caps/max_precise", "%d", &max_precise);
961 	return max_precise;
962 }
963 
964 struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char *lookup_name)
965 {
966 	struct perf_pmu *pmu;
967 	__u32 type;
968 	const char *name = pmu_find_real_name(lookup_name);
969 	const char *alias_name;
970 
971 	pmu = zalloc(sizeof(*pmu));
972 	if (!pmu)
973 		return NULL;
974 
975 	pmu->name = strdup(name);
976 	if (!pmu->name)
977 		goto err;
978 
979 	/*
980 	 * Read type early to fail fast if a lookup name isn't a PMU. Ensure
981 	 * that type value is successfully assigned (return 1).
982 	 */
983 	if (perf_pmu__scan_file_at(pmu, dirfd, "type", "%u", &type) != 1)
984 		goto err;
985 
986 	INIT_LIST_HEAD(&pmu->format);
987 	INIT_LIST_HEAD(&pmu->aliases);
988 	INIT_LIST_HEAD(&pmu->caps);
989 
990 	/*
991 	 * The pmu data we store & need consists of the pmu
992 	 * type value and format definitions. Load both right
993 	 * now.
994 	 */
995 	if (pmu_format(pmu, dirfd, name)) {
996 		free(pmu);
997 		return NULL;
998 	}
999 	pmu->is_core = is_pmu_core(name);
1000 	pmu->cpus = pmu_cpumask(dirfd, name, pmu->is_core);
1001 
1002 	alias_name = pmu_find_alias_name(name);
1003 	if (alias_name) {
1004 		pmu->alias_name = strdup(alias_name);
1005 		if (!pmu->alias_name)
1006 			goto err;
1007 	}
1008 
1009 	pmu->type = type;
1010 	pmu->is_uncore = pmu_is_uncore(dirfd, name);
1011 	if (pmu->is_uncore)
1012 		pmu->id = pmu_id(name);
1013 	pmu->max_precise = pmu_max_precise(dirfd, pmu);
1014 	pmu->events_table = perf_pmu__find_events_table(pmu);
1015 	pmu_add_sys_aliases(pmu);
1016 	list_add_tail(&pmu->list, pmus);
1017 
1018 	pmu->default_config = perf_pmu__get_default_config(pmu);
1019 
1020 	return pmu;
1021 err:
1022 	zfree(&pmu->name);
1023 	free(pmu);
1024 	return NULL;
1025 }
1026 
1027 /* Creates the PMU when sysfs scanning fails. */
1028 struct perf_pmu *perf_pmu__create_placeholder_core_pmu(struct list_head *core_pmus)
1029 {
1030 	struct perf_pmu *pmu = zalloc(sizeof(*pmu));
1031 
1032 	if (!pmu)
1033 		return NULL;
1034 
1035 	pmu->name = strdup("cpu");
1036 	if (!pmu->name) {
1037 		free(pmu);
1038 		return NULL;
1039 	}
1040 
1041 	pmu->is_core = true;
1042 	pmu->type = PERF_TYPE_RAW;
1043 	pmu->cpus = cpu_map__online();
1044 
1045 	INIT_LIST_HEAD(&pmu->format);
1046 	INIT_LIST_HEAD(&pmu->aliases);
1047 	INIT_LIST_HEAD(&pmu->caps);
1048 	list_add_tail(&pmu->list, core_pmus);
1049 	return pmu;
1050 }
1051 
1052 void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu)
1053 {
1054 	struct perf_pmu_format *format;
1055 
1056 	if (pmu->formats_checked)
1057 		return;
1058 
1059 	pmu->formats_checked = true;
1060 
1061 	/* fake pmu doesn't have format list */
1062 	if (pmu == &perf_pmu__fake)
1063 		return;
1064 
1065 	list_for_each_entry(format, &pmu->format, list) {
1066 		perf_pmu_format__load(pmu, format);
1067 		if (format->value >= PERF_PMU_FORMAT_VALUE_CONFIG_END) {
1068 			pr_warning("WARNING: '%s' format '%s' requires 'perf_event_attr::config%d'"
1069 				   "which is not supported by this version of perf!\n",
1070 				   pmu->name, format->name, format->value);
1071 			return;
1072 		}
1073 	}
1074 }
1075 
1076 bool evsel__is_aux_event(const struct evsel *evsel)
1077 {
1078 	struct perf_pmu *pmu = evsel__find_pmu(evsel);
1079 
1080 	return pmu && pmu->auxtrace;
1081 }
1082 
1083 /*
1084  * Set @config_name to @val as long as the user hasn't already set or cleared it
1085  * by passing a config term on the command line.
1086  *
1087  * @val is the value to put into the bits specified by @config_name rather than
1088  * the bit pattern. It is shifted into position by this function, so to set
1089  * something to true, pass 1 for val rather than a pre shifted value.
1090  */
1091 #define field_prep(_mask, _val) (((_val) << (ffsll(_mask) - 1)) & (_mask))
1092 void evsel__set_config_if_unset(struct perf_pmu *pmu, struct evsel *evsel,
1093 				const char *config_name, u64 val)
1094 {
1095 	u64 user_bits = 0, bits;
1096 	struct evsel_config_term *term = evsel__get_config_term(evsel, CFG_CHG);
1097 
1098 	if (term)
1099 		user_bits = term->val.cfg_chg;
1100 
1101 	bits = perf_pmu__format_bits(pmu, config_name);
1102 
1103 	/* Do nothing if the user changed the value */
1104 	if (bits & user_bits)
1105 		return;
1106 
1107 	/* Otherwise replace it */
1108 	evsel->core.attr.config &= ~bits;
1109 	evsel->core.attr.config |= field_prep(bits, val);
1110 }
1111 
1112 static struct perf_pmu_format *
1113 pmu_find_format(struct list_head *formats, const char *name)
1114 {
1115 	struct perf_pmu_format *format;
1116 
1117 	list_for_each_entry(format, formats, list)
1118 		if (!strcmp(format->name, name))
1119 			return format;
1120 
1121 	return NULL;
1122 }
1123 
1124 __u64 perf_pmu__format_bits(struct perf_pmu *pmu, const char *name)
1125 {
1126 	struct perf_pmu_format *format = pmu_find_format(&pmu->format, name);
1127 	__u64 bits = 0;
1128 	int fbit;
1129 
1130 	if (!format)
1131 		return 0;
1132 
1133 	for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
1134 		bits |= 1ULL << fbit;
1135 
1136 	return bits;
1137 }
1138 
1139 int perf_pmu__format_type(struct perf_pmu *pmu, const char *name)
1140 {
1141 	struct perf_pmu_format *format = pmu_find_format(&pmu->format, name);
1142 
1143 	if (!format)
1144 		return -1;
1145 
1146 	perf_pmu_format__load(pmu, format);
1147 	return format->value;
1148 }
1149 
1150 /*
1151  * Sets value based on the format definition (format parameter)
1152  * and unformatted value (value parameter).
1153  */
1154 static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
1155 			     bool zero)
1156 {
1157 	unsigned long fbit, vbit;
1158 
1159 	for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
1160 
1161 		if (!test_bit(fbit, format))
1162 			continue;
1163 
1164 		if (value & (1llu << vbit++))
1165 			*v |= (1llu << fbit);
1166 		else if (zero)
1167 			*v &= ~(1llu << fbit);
1168 	}
1169 }
1170 
1171 static __u64 pmu_format_max_value(const unsigned long *format)
1172 {
1173 	int w;
1174 
1175 	w = bitmap_weight(format, PERF_PMU_FORMAT_BITS);
1176 	if (!w)
1177 		return 0;
1178 	if (w < 64)
1179 		return (1ULL << w) - 1;
1180 	return -1;
1181 }
1182 
1183 /*
1184  * Term is a string term, and might be a param-term. Try to look up it's value
1185  * in the remaining terms.
1186  * - We have a term like "base-or-format-term=param-term",
1187  * - We need to find the value supplied for "param-term" (with param-term named
1188  *   in a config string) later on in the term list.
1189  */
1190 static int pmu_resolve_param_term(struct parse_events_term *term,
1191 				  struct list_head *head_terms,
1192 				  __u64 *value)
1193 {
1194 	struct parse_events_term *t;
1195 
1196 	list_for_each_entry(t, head_terms, list) {
1197 		if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM &&
1198 		    t->config && !strcmp(t->config, term->config)) {
1199 			t->used = true;
1200 			*value = t->val.num;
1201 			return 0;
1202 		}
1203 	}
1204 
1205 	if (verbose > 0)
1206 		printf("Required parameter '%s' not specified\n", term->config);
1207 
1208 	return -1;
1209 }
1210 
1211 static char *pmu_formats_string(struct list_head *formats)
1212 {
1213 	struct perf_pmu_format *format;
1214 	char *str = NULL;
1215 	struct strbuf buf = STRBUF_INIT;
1216 	unsigned int i = 0;
1217 
1218 	if (!formats)
1219 		return NULL;
1220 
1221 	/* sysfs exported terms */
1222 	list_for_each_entry(format, formats, list)
1223 		if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
1224 			goto error;
1225 
1226 	str = strbuf_detach(&buf, NULL);
1227 error:
1228 	strbuf_release(&buf);
1229 
1230 	return str;
1231 }
1232 
1233 /*
1234  * Setup one of config[12] attr members based on the
1235  * user input data - term parameter.
1236  */
1237 static int pmu_config_term(struct perf_pmu *pmu,
1238 			   struct perf_event_attr *attr,
1239 			   struct parse_events_term *term,
1240 			   struct list_head *head_terms,
1241 			   bool zero, struct parse_events_error *err)
1242 {
1243 	struct perf_pmu_format *format;
1244 	__u64 *vp;
1245 	__u64 val, max_val;
1246 
1247 	/*
1248 	 * If this is a parameter we've already used for parameterized-eval,
1249 	 * skip it in normal eval.
1250 	 */
1251 	if (term->used)
1252 		return 0;
1253 
1254 	/*
1255 	 * Hardcoded terms should be already in, so nothing
1256 	 * to be done for them.
1257 	 */
1258 	if (parse_events__is_hardcoded_term(term))
1259 		return 0;
1260 
1261 	format = pmu_find_format(&pmu->format, term->config);
1262 	if (!format) {
1263 		char *pmu_term = pmu_formats_string(&pmu->format);
1264 		char *unknown_term;
1265 		char *help_msg;
1266 
1267 		if (asprintf(&unknown_term,
1268 				"unknown term '%s' for pmu '%s'",
1269 				term->config, pmu->name) < 0)
1270 			unknown_term = NULL;
1271 		help_msg = parse_events_formats_error_string(pmu_term);
1272 		if (err) {
1273 			parse_events_error__handle(err, term->err_term,
1274 						   unknown_term,
1275 						   help_msg);
1276 		} else {
1277 			pr_debug("%s (%s)\n", unknown_term, help_msg);
1278 			free(unknown_term);
1279 		}
1280 		free(pmu_term);
1281 		return -EINVAL;
1282 	}
1283 	perf_pmu_format__load(pmu, format);
1284 	switch (format->value) {
1285 	case PERF_PMU_FORMAT_VALUE_CONFIG:
1286 		vp = &attr->config;
1287 		break;
1288 	case PERF_PMU_FORMAT_VALUE_CONFIG1:
1289 		vp = &attr->config1;
1290 		break;
1291 	case PERF_PMU_FORMAT_VALUE_CONFIG2:
1292 		vp = &attr->config2;
1293 		break;
1294 	case PERF_PMU_FORMAT_VALUE_CONFIG3:
1295 		vp = &attr->config3;
1296 		break;
1297 	default:
1298 		return -EINVAL;
1299 	}
1300 
1301 	/*
1302 	 * Either directly use a numeric term, or try to translate string terms
1303 	 * using event parameters.
1304 	 */
1305 	if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1306 		if (term->no_value &&
1307 		    bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
1308 			if (err) {
1309 				parse_events_error__handle(err, term->err_val,
1310 					   strdup("no value assigned for term"),
1311 					   NULL);
1312 			}
1313 			return -EINVAL;
1314 		}
1315 
1316 		val = term->val.num;
1317 	} else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1318 		if (strcmp(term->val.str, "?")) {
1319 			if (verbose > 0) {
1320 				pr_info("Invalid sysfs entry %s=%s\n",
1321 						term->config, term->val.str);
1322 			}
1323 			if (err) {
1324 				parse_events_error__handle(err, term->err_val,
1325 					strdup("expected numeric value"),
1326 					NULL);
1327 			}
1328 			return -EINVAL;
1329 		}
1330 
1331 		if (pmu_resolve_param_term(term, head_terms, &val))
1332 			return -EINVAL;
1333 	} else
1334 		return -EINVAL;
1335 
1336 	max_val = pmu_format_max_value(format->bits);
1337 	if (val > max_val) {
1338 		if (err) {
1339 			char *err_str;
1340 
1341 			parse_events_error__handle(err, term->err_val,
1342 				asprintf(&err_str,
1343 				    "value too big for format, maximum is %llu",
1344 				    (unsigned long long)max_val) < 0
1345 				    ? strdup("value too big for format")
1346 				    : err_str,
1347 				    NULL);
1348 			return -EINVAL;
1349 		}
1350 		/*
1351 		 * Assume we don't care if !err, in which case the value will be
1352 		 * silently truncated.
1353 		 */
1354 	}
1355 
1356 	pmu_format_value(format->bits, val, vp, zero);
1357 	return 0;
1358 }
1359 
1360 int perf_pmu__config_terms(struct perf_pmu *pmu,
1361 			   struct perf_event_attr *attr,
1362 			   struct list_head *head_terms,
1363 			   bool zero, struct parse_events_error *err)
1364 {
1365 	struct parse_events_term *term;
1366 
1367 	list_for_each_entry(term, head_terms, list) {
1368 		if (pmu_config_term(pmu, attr, term, head_terms, zero, err))
1369 			return -EINVAL;
1370 	}
1371 
1372 	return 0;
1373 }
1374 
1375 /*
1376  * Configures event's 'attr' parameter based on the:
1377  * 1) users input - specified in terms parameter
1378  * 2) pmu format definitions - specified by pmu parameter
1379  */
1380 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
1381 		     struct list_head *head_terms,
1382 		     struct parse_events_error *err)
1383 {
1384 	bool zero = !!pmu->default_config;
1385 
1386 	return perf_pmu__config_terms(pmu, attr, head_terms, zero, err);
1387 }
1388 
1389 static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
1390 					     struct parse_events_term *term)
1391 {
1392 	struct perf_pmu_alias *alias;
1393 	const char *name;
1394 
1395 	if (parse_events__is_hardcoded_term(term))
1396 		return NULL;
1397 
1398 	if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1399 		if (!term->no_value)
1400 			return NULL;
1401 		if (pmu_find_format(&pmu->format, term->config))
1402 			return NULL;
1403 		name = term->config;
1404 
1405 	} else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1406 		if (strcasecmp(term->config, "event"))
1407 			return NULL;
1408 		name = term->val.str;
1409 	} else {
1410 		return NULL;
1411 	}
1412 
1413 	alias = perf_pmu__find_alias(pmu, name, /*load=*/ true);
1414 	if (alias || pmu->cpu_aliases_added)
1415 		return alias;
1416 
1417 	/* Alias doesn't exist, try to get it from the json events. */
1418 	if (pmu->events_table &&
1419 	    pmu_events_table__find_event(pmu->events_table, pmu, name,
1420 				         pmu_add_cpu_aliases_map_callback,
1421 				         pmu) == 0) {
1422 		alias = perf_pmu__find_alias(pmu, name, /*load=*/ false);
1423 	}
1424 	return alias;
1425 }
1426 
1427 
1428 static int check_info_data(struct perf_pmu *pmu,
1429 			   struct perf_pmu_alias *alias,
1430 			   struct perf_pmu_info *info,
1431 			   struct parse_events_error *err,
1432 			   int column)
1433 {
1434 	read_alias_info(pmu, alias);
1435 	/*
1436 	 * Only one term in event definition can
1437 	 * define unit, scale and snapshot, fail
1438 	 * if there's more than one.
1439 	 */
1440 	if (info->unit && alias->unit[0]) {
1441 		parse_events_error__handle(err, column,
1442 					strdup("Attempt to set event's unit twice"),
1443 					NULL);
1444 		return -EINVAL;
1445 	}
1446 	if (info->scale && alias->scale) {
1447 		parse_events_error__handle(err, column,
1448 					strdup("Attempt to set event's scale twice"),
1449 					NULL);
1450 		return -EINVAL;
1451 	}
1452 	if (info->snapshot && alias->snapshot) {
1453 		parse_events_error__handle(err, column,
1454 					strdup("Attempt to set event snapshot twice"),
1455 					NULL);
1456 		return -EINVAL;
1457 	}
1458 
1459 	if (alias->unit[0])
1460 		info->unit = alias->unit;
1461 
1462 	if (alias->scale)
1463 		info->scale = alias->scale;
1464 
1465 	if (alias->snapshot)
1466 		info->snapshot = alias->snapshot;
1467 
1468 	return 0;
1469 }
1470 
1471 /*
1472  * Find alias in the terms list and replace it with the terms
1473  * defined for the alias
1474  */
1475 int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
1476 			  struct perf_pmu_info *info, struct parse_events_error *err)
1477 {
1478 	struct parse_events_term *term, *h;
1479 	struct perf_pmu_alias *alias;
1480 	int ret;
1481 
1482 	info->per_pkg = false;
1483 
1484 	/*
1485 	 * Mark unit and scale as not set
1486 	 * (different from default values, see below)
1487 	 */
1488 	info->unit     = NULL;
1489 	info->scale    = 0.0;
1490 	info->snapshot = false;
1491 
1492 	list_for_each_entry_safe(term, h, head_terms, list) {
1493 		alias = pmu_find_alias(pmu, term);
1494 		if (!alias)
1495 			continue;
1496 		ret = pmu_alias_terms(alias, &term->list);
1497 		if (ret) {
1498 			parse_events_error__handle(err, term->err_term,
1499 						strdup("Failure to duplicate terms"),
1500 						NULL);
1501 			return ret;
1502 		}
1503 
1504 		ret = check_info_data(pmu, alias, info, err, term->err_term);
1505 		if (ret)
1506 			return ret;
1507 
1508 		if (alias->per_pkg)
1509 			info->per_pkg = true;
1510 
1511 		list_del_init(&term->list);
1512 		parse_events_term__delete(term);
1513 	}
1514 
1515 	/*
1516 	 * if no unit or scale found in aliases, then
1517 	 * set defaults as for evsel
1518 	 * unit cannot left to NULL
1519 	 */
1520 	if (info->unit == NULL)
1521 		info->unit   = "";
1522 
1523 	if (info->scale == 0.0)
1524 		info->scale  = 1.0;
1525 
1526 	return 0;
1527 }
1528 
1529 struct find_event_args {
1530 	const char *event;
1531 	void *state;
1532 	pmu_event_callback cb;
1533 };
1534 
1535 static int find_event_callback(void *state, struct pmu_event_info *info)
1536 {
1537 	struct find_event_args *args = state;
1538 
1539 	if (!strcmp(args->event, info->name))
1540 		return args->cb(args->state, info);
1541 
1542 	return 0;
1543 }
1544 
1545 int perf_pmu__find_event(struct perf_pmu *pmu, const char *event, void *state, pmu_event_callback cb)
1546 {
1547 	struct find_event_args args = {
1548 		.event = event,
1549 		.state = state,
1550 		.cb = cb,
1551 	};
1552 
1553 	/* Sub-optimal, but function is only used by tests. */
1554 	return perf_pmu__for_each_event(pmu, /*skip_duplicate_pmus=*/ false,
1555 					&args, find_event_callback);
1556 }
1557 
1558 static void perf_pmu__del_formats(struct list_head *formats)
1559 {
1560 	struct perf_pmu_format *fmt, *tmp;
1561 
1562 	list_for_each_entry_safe(fmt, tmp, formats, list) {
1563 		list_del(&fmt->list);
1564 		zfree(&fmt->name);
1565 		free(fmt);
1566 	}
1567 }
1568 
1569 bool perf_pmu__has_format(const struct perf_pmu *pmu, const char *name)
1570 {
1571 	struct perf_pmu_format *format;
1572 
1573 	list_for_each_entry(format, &pmu->format, list) {
1574 		if (!strcmp(format->name, name))
1575 			return true;
1576 	}
1577 	return false;
1578 }
1579 
1580 bool is_pmu_core(const char *name)
1581 {
1582 	return !strcmp(name, "cpu") || !strcmp(name, "cpum_cf") || is_sysfs_pmu_core(name);
1583 }
1584 
1585 bool perf_pmu__supports_legacy_cache(const struct perf_pmu *pmu)
1586 {
1587 	return pmu->is_core;
1588 }
1589 
1590 bool perf_pmu__auto_merge_stats(const struct perf_pmu *pmu)
1591 {
1592 	return !pmu->is_core || perf_pmus__num_core_pmus() == 1;
1593 }
1594 
1595 bool perf_pmu__have_event(struct perf_pmu *pmu, const char *name)
1596 {
1597 	if (perf_pmu__find_alias(pmu, name, /*load=*/ true) != NULL)
1598 		return true;
1599 	if (pmu->cpu_aliases_added || !pmu->events_table)
1600 		return false;
1601 	return pmu_events_table__find_event(pmu->events_table, pmu, name, NULL, NULL) == 0;
1602 }
1603 
1604 size_t perf_pmu__num_events(struct perf_pmu *pmu)
1605 {
1606 	size_t nr;
1607 
1608 	if (!pmu->sysfs_aliases_loaded)
1609 		pmu_aliases_parse(pmu);
1610 
1611 	nr = pmu->sysfs_aliases;
1612 
1613 	if (pmu->cpu_aliases_added)
1614 		 nr += pmu->loaded_json_aliases;
1615 	else if (pmu->events_table)
1616 		nr += pmu_events_table__num_events(pmu->events_table, pmu) - pmu->loaded_json_aliases;
1617 
1618 	return pmu->selectable ? nr + 1 : nr;
1619 }
1620 
1621 static int sub_non_neg(int a, int b)
1622 {
1623 	if (b > a)
1624 		return 0;
1625 	return a - b;
1626 }
1627 
1628 static char *format_alias(char *buf, int len, const struct perf_pmu *pmu,
1629 			  const struct perf_pmu_alias *alias, bool skip_duplicate_pmus)
1630 {
1631 	struct parse_events_term *term;
1632 	int pmu_name_len = skip_duplicate_pmus
1633 		? pmu_name_len_no_suffix(pmu->name, /*num=*/NULL)
1634 		: (int)strlen(pmu->name);
1635 	int used = snprintf(buf, len, "%.*s/%s", pmu_name_len, pmu->name, alias->name);
1636 
1637 	list_for_each_entry(term, &alias->terms, list) {
1638 		if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1639 			used += snprintf(buf + used, sub_non_neg(len, used),
1640 					",%s=%s", term->config,
1641 					term->val.str);
1642 	}
1643 
1644 	if (sub_non_neg(len, used) > 0) {
1645 		buf[used] = '/';
1646 		used++;
1647 	}
1648 	if (sub_non_neg(len, used) > 0) {
1649 		buf[used] = '\0';
1650 		used++;
1651 	} else
1652 		buf[len - 1] = '\0';
1653 
1654 	return buf;
1655 }
1656 
1657 int perf_pmu__for_each_event(struct perf_pmu *pmu, bool skip_duplicate_pmus,
1658 			     void *state, pmu_event_callback cb)
1659 {
1660 	char buf[1024];
1661 	struct perf_pmu_alias *event;
1662 	struct pmu_event_info info = {
1663 		.pmu = pmu,
1664 	};
1665 	int ret = 0;
1666 	struct strbuf sb;
1667 
1668 	strbuf_init(&sb, /*hint=*/ 0);
1669 	pmu_add_cpu_aliases(pmu);
1670 	list_for_each_entry(event, &pmu->aliases, list) {
1671 		size_t buf_used;
1672 
1673 		info.pmu_name = event->pmu_name ?: pmu->name;
1674 		info.alias = NULL;
1675 		if (event->desc) {
1676 			info.name = event->name;
1677 			buf_used = 0;
1678 		} else {
1679 			info.name = format_alias(buf, sizeof(buf), pmu, event,
1680 						 skip_duplicate_pmus);
1681 			if (pmu->is_core) {
1682 				info.alias = info.name;
1683 				info.name = event->name;
1684 			}
1685 			buf_used = strlen(buf) + 1;
1686 		}
1687 		info.scale_unit = NULL;
1688 		if (strlen(event->unit) || event->scale != 1.0) {
1689 			info.scale_unit = buf + buf_used;
1690 			buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used,
1691 					"%G%s", event->scale, event->unit) + 1;
1692 		}
1693 		info.desc = event->desc;
1694 		info.long_desc = event->long_desc;
1695 		info.encoding_desc = buf + buf_used;
1696 		parse_events_term__to_strbuf(&event->terms, &sb);
1697 		buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used,
1698 				"%s/%s/", info.pmu_name, sb.buf) + 1;
1699 		info.topic = event->topic;
1700 		info.str = sb.buf;
1701 		info.deprecated = event->deprecated;
1702 		ret = cb(state, &info);
1703 		if (ret)
1704 			goto out;
1705 		strbuf_setlen(&sb, /*len=*/ 0);
1706 	}
1707 	if (pmu->selectable) {
1708 		info.name = buf;
1709 		snprintf(buf, sizeof(buf), "%s//", pmu->name);
1710 		info.alias = NULL;
1711 		info.scale_unit = NULL;
1712 		info.desc = NULL;
1713 		info.long_desc = NULL;
1714 		info.encoding_desc = NULL;
1715 		info.topic = NULL;
1716 		info.pmu_name = pmu->name;
1717 		info.deprecated = false;
1718 		ret = cb(state, &info);
1719 	}
1720 out:
1721 	strbuf_release(&sb);
1722 	return ret;
1723 }
1724 
1725 bool pmu__name_match(const struct perf_pmu *pmu, const char *pmu_name)
1726 {
1727 	return !strcmp(pmu->name, pmu_name) ||
1728 		(pmu->is_uncore && pmu_uncore_alias_match(pmu_name, pmu->name)) ||
1729 		/*
1730 		 * jevents and tests use default_core as a marker for any core
1731 		 * PMU as the PMU name varies across architectures.
1732 		 */
1733 	        (pmu->is_core && !strcmp(pmu_name, "default_core"));
1734 }
1735 
1736 bool perf_pmu__is_software(const struct perf_pmu *pmu)
1737 {
1738 	if (pmu->is_core || pmu->is_uncore || pmu->auxtrace)
1739 		return false;
1740 	switch (pmu->type) {
1741 	case PERF_TYPE_HARDWARE:	return false;
1742 	case PERF_TYPE_SOFTWARE:	return true;
1743 	case PERF_TYPE_TRACEPOINT:	return true;
1744 	case PERF_TYPE_HW_CACHE:	return false;
1745 	case PERF_TYPE_RAW:		return false;
1746 	case PERF_TYPE_BREAKPOINT:	return true;
1747 	default: break;
1748 	}
1749 	return !strcmp(pmu->name, "kprobe") || !strcmp(pmu->name, "uprobe");
1750 }
1751 
1752 FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
1753 {
1754 	char path[PATH_MAX];
1755 
1756 	if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name) ||
1757 	    !file_available(path))
1758 		return NULL;
1759 
1760 	return fopen(path, "r");
1761 }
1762 
1763 FILE *perf_pmu__open_file_at(struct perf_pmu *pmu, int dirfd, const char *name)
1764 {
1765 	int fd;
1766 
1767 	fd = perf_pmu__pathname_fd(dirfd, pmu->name, name, O_RDONLY);
1768 	if (fd < 0)
1769 		return NULL;
1770 
1771 	return fdopen(fd, "r");
1772 }
1773 
1774 int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
1775 			...)
1776 {
1777 	va_list args;
1778 	FILE *file;
1779 	int ret = EOF;
1780 
1781 	va_start(args, fmt);
1782 	file = perf_pmu__open_file(pmu, name);
1783 	if (file) {
1784 		ret = vfscanf(file, fmt, args);
1785 		fclose(file);
1786 	}
1787 	va_end(args);
1788 	return ret;
1789 }
1790 
1791 int perf_pmu__scan_file_at(struct perf_pmu *pmu, int dirfd, const char *name,
1792 			   const char *fmt, ...)
1793 {
1794 	va_list args;
1795 	FILE *file;
1796 	int ret = EOF;
1797 
1798 	va_start(args, fmt);
1799 	file = perf_pmu__open_file_at(pmu, dirfd, name);
1800 	if (file) {
1801 		ret = vfscanf(file, fmt, args);
1802 		fclose(file);
1803 	}
1804 	va_end(args);
1805 	return ret;
1806 }
1807 
1808 bool perf_pmu__file_exists(struct perf_pmu *pmu, const char *name)
1809 {
1810 	char path[PATH_MAX];
1811 
1812 	if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name))
1813 		return false;
1814 
1815 	return file_available(path);
1816 }
1817 
1818 static int perf_pmu__new_caps(struct list_head *list, char *name, char *value)
1819 {
1820 	struct perf_pmu_caps *caps = zalloc(sizeof(*caps));
1821 
1822 	if (!caps)
1823 		return -ENOMEM;
1824 
1825 	caps->name = strdup(name);
1826 	if (!caps->name)
1827 		goto free_caps;
1828 	caps->value = strndup(value, strlen(value) - 1);
1829 	if (!caps->value)
1830 		goto free_name;
1831 	list_add_tail(&caps->list, list);
1832 	return 0;
1833 
1834 free_name:
1835 	zfree(&caps->name);
1836 free_caps:
1837 	free(caps);
1838 
1839 	return -ENOMEM;
1840 }
1841 
1842 static void perf_pmu__del_caps(struct perf_pmu *pmu)
1843 {
1844 	struct perf_pmu_caps *caps, *tmp;
1845 
1846 	list_for_each_entry_safe(caps, tmp, &pmu->caps, list) {
1847 		list_del(&caps->list);
1848 		zfree(&caps->name);
1849 		zfree(&caps->value);
1850 		free(caps);
1851 	}
1852 }
1853 
1854 /*
1855  * Reading/parsing the given pmu capabilities, which should be located at:
1856  * /sys/bus/event_source/devices/<dev>/caps as sysfs group attributes.
1857  * Return the number of capabilities
1858  */
1859 int perf_pmu__caps_parse(struct perf_pmu *pmu)
1860 {
1861 	struct stat st;
1862 	char caps_path[PATH_MAX];
1863 	DIR *caps_dir;
1864 	struct dirent *evt_ent;
1865 	int caps_fd;
1866 
1867 	if (pmu->caps_initialized)
1868 		return pmu->nr_caps;
1869 
1870 	pmu->nr_caps = 0;
1871 
1872 	if (!perf_pmu__pathname_scnprintf(caps_path, sizeof(caps_path), pmu->name, "caps"))
1873 		return -1;
1874 
1875 	if (stat(caps_path, &st) < 0) {
1876 		pmu->caps_initialized = true;
1877 		return 0;	/* no error if caps does not exist */
1878 	}
1879 
1880 	caps_dir = opendir(caps_path);
1881 	if (!caps_dir)
1882 		return -EINVAL;
1883 
1884 	caps_fd = dirfd(caps_dir);
1885 
1886 	while ((evt_ent = readdir(caps_dir)) != NULL) {
1887 		char *name = evt_ent->d_name;
1888 		char value[128];
1889 		FILE *file;
1890 		int fd;
1891 
1892 		if (!strcmp(name, ".") || !strcmp(name, ".."))
1893 			continue;
1894 
1895 		fd = openat(caps_fd, name, O_RDONLY);
1896 		if (fd == -1)
1897 			continue;
1898 		file = fdopen(fd, "r");
1899 		if (!file) {
1900 			close(fd);
1901 			continue;
1902 		}
1903 
1904 		if (!fgets(value, sizeof(value), file) ||
1905 		    (perf_pmu__new_caps(&pmu->caps, name, value) < 0)) {
1906 			fclose(file);
1907 			continue;
1908 		}
1909 
1910 		pmu->nr_caps++;
1911 		fclose(file);
1912 	}
1913 
1914 	closedir(caps_dir);
1915 
1916 	pmu->caps_initialized = true;
1917 	return pmu->nr_caps;
1918 }
1919 
1920 static void perf_pmu__compute_config_masks(struct perf_pmu *pmu)
1921 {
1922 	struct perf_pmu_format *format;
1923 
1924 	if (pmu->config_masks_computed)
1925 		return;
1926 
1927 	list_for_each_entry(format, &pmu->format, list)	{
1928 		unsigned int i;
1929 		__u64 *mask;
1930 
1931 		if (format->value >= PERF_PMU_FORMAT_VALUE_CONFIG_END)
1932 			continue;
1933 
1934 		pmu->config_masks_present = true;
1935 		mask = &pmu->config_masks[format->value];
1936 
1937 		for_each_set_bit(i, format->bits, PERF_PMU_FORMAT_BITS)
1938 			*mask |= 1ULL << i;
1939 	}
1940 	pmu->config_masks_computed = true;
1941 }
1942 
1943 void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config,
1944 				   const char *name, int config_num,
1945 				   const char *config_name)
1946 {
1947 	__u64 bits;
1948 	char buf[100];
1949 
1950 	perf_pmu__compute_config_masks(pmu);
1951 
1952 	/*
1953 	 * Kernel doesn't export any valid format bits.
1954 	 */
1955 	if (!pmu->config_masks_present)
1956 		return;
1957 
1958 	bits = config & ~pmu->config_masks[config_num];
1959 	if (bits == 0)
1960 		return;
1961 
1962 	bitmap_scnprintf((unsigned long *)&bits, sizeof(bits) * 8, buf, sizeof(buf));
1963 
1964 	pr_warning("WARNING: event '%s' not valid (bits %s of %s "
1965 		   "'%llx' not supported by kernel)!\n",
1966 		   name ?: "N/A", buf, config_name, config);
1967 }
1968 
1969 int perf_pmu__match(const char *pattern, const char *name, const char *tok)
1970 {
1971 	if (!name)
1972 		return -1;
1973 
1974 	if (fnmatch(pattern, name, 0))
1975 		return -1;
1976 
1977 	if (tok && !perf_pmu__match_ignoring_suffix(name, tok))
1978 		return -1;
1979 
1980 	return 0;
1981 }
1982 
1983 double __weak perf_pmu__cpu_slots_per_cycle(void)
1984 {
1985 	return NAN;
1986 }
1987 
1988 int perf_pmu__event_source_devices_scnprintf(char *pathname, size_t size)
1989 {
1990 	const char *sysfs = sysfs__mountpoint();
1991 
1992 	if (!sysfs)
1993 		return 0;
1994 	return scnprintf(pathname, size, "%s/bus/event_source/devices/", sysfs);
1995 }
1996 
1997 int perf_pmu__event_source_devices_fd(void)
1998 {
1999 	char path[PATH_MAX];
2000 	const char *sysfs = sysfs__mountpoint();
2001 
2002 	if (!sysfs)
2003 		return -1;
2004 
2005 	scnprintf(path, sizeof(path), "%s/bus/event_source/devices/", sysfs);
2006 	return open(path, O_DIRECTORY);
2007 }
2008 
2009 /*
2010  * Fill 'buf' with the path to a file or folder in 'pmu_name' in
2011  * sysfs. For example if pmu_name = "cs_etm" and 'filename' = "format"
2012  * then pathname will be filled with
2013  * "/sys/bus/event_source/devices/cs_etm/format"
2014  *
2015  * Return 0 if the sysfs mountpoint couldn't be found, if no characters were
2016  * written or if the buffer size is exceeded.
2017  */
2018 int perf_pmu__pathname_scnprintf(char *buf, size_t size,
2019 				 const char *pmu_name, const char *filename)
2020 {
2021 	size_t len;
2022 
2023 	len = perf_pmu__event_source_devices_scnprintf(buf, size);
2024 	if (!len || (len + strlen(pmu_name) + strlen(filename) + 1)  >= size)
2025 		return 0;
2026 
2027 	return scnprintf(buf + len, size - len, "%s/%s", pmu_name, filename);
2028 }
2029 
2030 int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *filename, int flags)
2031 {
2032 	char path[PATH_MAX];
2033 
2034 	scnprintf(path, sizeof(path), "%s/%s", pmu_name, filename);
2035 	return openat(dirfd, path, flags);
2036 }
2037 
2038 void perf_pmu__delete(struct perf_pmu *pmu)
2039 {
2040 	perf_pmu__del_formats(&pmu->format);
2041 	perf_pmu__del_aliases(pmu);
2042 	perf_pmu__del_caps(pmu);
2043 
2044 	perf_cpu_map__put(pmu->cpus);
2045 
2046 	zfree(&pmu->default_config);
2047 	zfree(&pmu->name);
2048 	zfree(&pmu->alias_name);
2049 	zfree(&pmu->id);
2050 	free(pmu);
2051 }
2052 
2053 struct perf_pmu *pmu__find_core_pmu(void)
2054 {
2055 	struct perf_pmu *pmu = NULL;
2056 
2057 	while ((pmu = perf_pmus__scan_core(pmu))) {
2058 		/*
2059 		 * The cpumap should cover all CPUs. Otherwise, some CPUs may
2060 		 * not support some events or have different event IDs.
2061 		 */
2062 		if (RC_CHK_ACCESS(pmu->cpus)->nr != cpu__max_cpu().cpu)
2063 			return NULL;
2064 
2065 		return pmu;
2066 	}
2067 	return NULL;
2068 }
2069