xref: /linux/tools/perf/util/pmu.c (revision 486021e04b24f8dffe54ec282ce5d4cc3e71133f)
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 parse_events_terms 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/%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/%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/%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/%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__exit(&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__exit(&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 = malloc(sizeof(*alias));
524 	if (!alias)
525 		return -ENOMEM;
526 
527 	parse_events_terms__init(&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, struct list_head *terms)
660 {
661 	struct parse_events_term *term, *cloned;
662 	struct parse_events_terms clone_terms;
663 
664 	parse_events_terms__init(&clone_terms);
665 	list_for_each_entry(term, &alias->terms.terms, list) {
666 		int ret = parse_events_term__clone(&cloned, term);
667 
668 		if (ret) {
669 			parse_events_terms__exit(&clone_terms);
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, &clone_terms.terms);
678 	}
679 	list_splice_init(&clone_terms.terms, terms);
680 	parse_events_terms__exit(&clone_terms);
681 	return 0;
682 }
683 
684 /*
685  * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64)
686  * may have a "cpus" file.
687  */
688 static struct perf_cpu_map *pmu_cpumask(int dirfd, const char *name, bool is_core)
689 {
690 	struct perf_cpu_map *cpus;
691 	const char *templates[] = {
692 		"cpumask",
693 		"cpus",
694 		NULL
695 	};
696 	const char **template;
697 	char pmu_name[PATH_MAX];
698 	struct perf_pmu pmu = {.name = pmu_name};
699 	FILE *file;
700 
701 	strlcpy(pmu_name, name, sizeof(pmu_name));
702 	for (template = templates; *template; template++) {
703 		file = perf_pmu__open_file_at(&pmu, dirfd, *template);
704 		if (!file)
705 			continue;
706 		cpus = perf_cpu_map__read(file);
707 		fclose(file);
708 		if (cpus)
709 			return cpus;
710 	}
711 
712 	/* Nothing found, for core PMUs assume this means all CPUs. */
713 	return is_core ? perf_cpu_map__get(cpu_map__online()) : NULL;
714 }
715 
716 static bool pmu_is_uncore(int dirfd, const char *name)
717 {
718 	int fd;
719 
720 	fd = perf_pmu__pathname_fd(dirfd, name, "cpumask", O_PATH);
721 	if (fd < 0)
722 		return false;
723 
724 	close(fd);
725 	return true;
726 }
727 
728 static char *pmu_id(const char *name)
729 {
730 	char path[PATH_MAX], *str;
731 	size_t len;
732 
733 	perf_pmu__pathname_scnprintf(path, sizeof(path), name, "identifier");
734 
735 	if (filename__read_str(path, &str, &len) < 0)
736 		return NULL;
737 
738 	str[len - 1] = 0; /* remove line feed */
739 
740 	return str;
741 }
742 
743 /**
744  * is_sysfs_pmu_core() - PMU CORE devices have different name other than cpu in
745  *         sysfs on some platforms like ARM or Intel hybrid. Looking for
746  *         possible the cpus file in sysfs files to identify whether this is a
747  *         core device.
748  * @name: The PMU name such as "cpu_atom".
749  */
750 static int is_sysfs_pmu_core(const char *name)
751 {
752 	char path[PATH_MAX];
753 
754 	if (!perf_pmu__pathname_scnprintf(path, sizeof(path), name, "cpus"))
755 		return 0;
756 	return file_available(path);
757 }
758 
759 char *perf_pmu__getcpuid(struct perf_pmu *pmu)
760 {
761 	char *cpuid;
762 	static bool printed;
763 
764 	cpuid = getenv("PERF_CPUID");
765 	if (cpuid)
766 		cpuid = strdup(cpuid);
767 	if (!cpuid)
768 		cpuid = get_cpuid_str(pmu);
769 	if (!cpuid)
770 		return NULL;
771 
772 	if (!printed) {
773 		pr_debug("Using CPUID %s\n", cpuid);
774 		printed = true;
775 	}
776 	return cpuid;
777 }
778 
779 __weak const struct pmu_metrics_table *pmu_metrics_table__find(void)
780 {
781 	return perf_pmu__find_metrics_table(NULL);
782 }
783 
784 /**
785  * perf_pmu__match_ignoring_suffix - Does the pmu_name match tok ignoring any
786  *                                   trailing suffix? The Suffix must be in form
787  *                                   tok_{digits}, or tok{digits}.
788  * @pmu_name: The pmu_name with possible suffix.
789  * @tok: The possible match to pmu_name without suffix.
790  */
791 static bool perf_pmu__match_ignoring_suffix(const char *pmu_name, const char *tok)
792 {
793 	const char *p;
794 
795 	if (strncmp(pmu_name, tok, strlen(tok)))
796 		return false;
797 
798 	p = pmu_name + strlen(tok);
799 	if (*p == 0)
800 		return true;
801 
802 	if (*p == '_')
803 		++p;
804 
805 	/* Ensure we end in a number */
806 	while (1) {
807 		if (!isdigit(*p))
808 			return false;
809 		if (*(++p) == 0)
810 			break;
811 	}
812 
813 	return true;
814 }
815 
816 /**
817  * pmu_uncore_alias_match - does name match the PMU name?
818  * @pmu_name: the json struct pmu_event name. This may lack a suffix (which
819  *            matches) or be of the form "socket,pmuname" which will match
820  *            "socketX_pmunameY".
821  * @name: a real full PMU name as from sysfs.
822  */
823 static bool pmu_uncore_alias_match(const char *pmu_name, const char *name)
824 {
825 	char *tmp = NULL, *tok, *str;
826 	bool res;
827 
828 	if (strchr(pmu_name, ',') == NULL)
829 		return perf_pmu__match_ignoring_suffix(name, pmu_name);
830 
831 	str = strdup(pmu_name);
832 	if (!str)
833 		return false;
834 
835 	/*
836 	 * uncore alias may be from different PMU with common prefix
837 	 */
838 	tok = strtok_r(str, ",", &tmp);
839 	if (strncmp(pmu_name, tok, strlen(tok))) {
840 		res = false;
841 		goto out;
842 	}
843 
844 	/*
845 	 * Match more complex aliases where the alias name is a comma-delimited
846 	 * list of tokens, orderly contained in the matching PMU name.
847 	 *
848 	 * Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we
849 	 *	    match "socket" in "socketX_pmunameY" and then "pmuname" in
850 	 *	    "pmunameY".
851 	 */
852 	while (1) {
853 		char *next_tok = strtok_r(NULL, ",", &tmp);
854 
855 		name = strstr(name, tok);
856 		if (!name ||
857 		    (!next_tok && !perf_pmu__match_ignoring_suffix(name, tok))) {
858 			res = false;
859 			goto out;
860 		}
861 		if (!next_tok)
862 			break;
863 		tok = next_tok;
864 		name += strlen(tok);
865 	}
866 
867 	res = true;
868 out:
869 	free(str);
870 	return res;
871 }
872 
873 static int pmu_add_cpu_aliases_map_callback(const struct pmu_event *pe,
874 					const struct pmu_events_table *table __maybe_unused,
875 					void *vdata)
876 {
877 	struct perf_pmu *pmu = vdata;
878 
879 	perf_pmu__new_alias(pmu, pe->name, pe->desc, pe->event, /*val_fd=*/ NULL, pe);
880 	return 0;
881 }
882 
883 /*
884  * From the pmu_events_table, find the events that correspond to the given
885  * PMU and add them to the list 'head'.
886  */
887 void pmu_add_cpu_aliases_table(struct perf_pmu *pmu, const struct pmu_events_table *table)
888 {
889 	pmu_events_table__for_each_event(table, pmu, pmu_add_cpu_aliases_map_callback, pmu);
890 }
891 
892 static void pmu_add_cpu_aliases(struct perf_pmu *pmu)
893 {
894 	if (!pmu->events_table)
895 		return;
896 
897 	if (pmu->cpu_aliases_added)
898 		return;
899 
900 	pmu_add_cpu_aliases_table(pmu, pmu->events_table);
901 	pmu->cpu_aliases_added = true;
902 }
903 
904 static int pmu_add_sys_aliases_iter_fn(const struct pmu_event *pe,
905 				       const struct pmu_events_table *table __maybe_unused,
906 				       void *vdata)
907 {
908 	struct perf_pmu *pmu = vdata;
909 
910 	if (!pe->compat || !pe->pmu)
911 		return 0;
912 
913 	if (!strcmp(pmu->id, pe->compat) &&
914 	    pmu_uncore_alias_match(pe->pmu, pmu->name)) {
915 		perf_pmu__new_alias(pmu,
916 				pe->name,
917 				pe->desc,
918 				pe->event,
919 				/*val_fd=*/ NULL,
920 				pe);
921 	}
922 
923 	return 0;
924 }
925 
926 void pmu_add_sys_aliases(struct perf_pmu *pmu)
927 {
928 	if (!pmu->id)
929 		return;
930 
931 	pmu_for_each_sys_event(pmu_add_sys_aliases_iter_fn, pmu);
932 }
933 
934 struct perf_event_attr * __weak
935 perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
936 {
937 	return NULL;
938 }
939 
940 const char * __weak
941 pmu_find_real_name(const char *name)
942 {
943 	return name;
944 }
945 
946 const char * __weak
947 pmu_find_alias_name(const char *name __maybe_unused)
948 {
949 	return NULL;
950 }
951 
952 static int pmu_max_precise(int dirfd, struct perf_pmu *pmu)
953 {
954 	int max_precise = -1;
955 
956 	perf_pmu__scan_file_at(pmu, dirfd, "caps/max_precise", "%d", &max_precise);
957 	return max_precise;
958 }
959 
960 struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char *lookup_name)
961 {
962 	struct perf_pmu *pmu;
963 	__u32 type;
964 	const char *name = pmu_find_real_name(lookup_name);
965 	const char *alias_name;
966 
967 	pmu = zalloc(sizeof(*pmu));
968 	if (!pmu)
969 		return NULL;
970 
971 	pmu->name = strdup(name);
972 	if (!pmu->name)
973 		goto err;
974 
975 	/*
976 	 * Read type early to fail fast if a lookup name isn't a PMU. Ensure
977 	 * that type value is successfully assigned (return 1).
978 	 */
979 	if (perf_pmu__scan_file_at(pmu, dirfd, "type", "%u", &type) != 1)
980 		goto err;
981 
982 	INIT_LIST_HEAD(&pmu->format);
983 	INIT_LIST_HEAD(&pmu->aliases);
984 	INIT_LIST_HEAD(&pmu->caps);
985 
986 	/*
987 	 * The pmu data we store & need consists of the pmu
988 	 * type value and format definitions. Load both right
989 	 * now.
990 	 */
991 	if (pmu_format(pmu, dirfd, name)) {
992 		free(pmu);
993 		return NULL;
994 	}
995 	pmu->is_core = is_pmu_core(name);
996 	pmu->cpus = pmu_cpumask(dirfd, name, pmu->is_core);
997 
998 	alias_name = pmu_find_alias_name(name);
999 	if (alias_name) {
1000 		pmu->alias_name = strdup(alias_name);
1001 		if (!pmu->alias_name)
1002 			goto err;
1003 	}
1004 
1005 	pmu->type = type;
1006 	pmu->is_uncore = pmu_is_uncore(dirfd, name);
1007 	if (pmu->is_uncore)
1008 		pmu->id = pmu_id(name);
1009 	pmu->max_precise = pmu_max_precise(dirfd, pmu);
1010 	pmu->events_table = perf_pmu__find_events_table(pmu);
1011 	pmu_add_sys_aliases(pmu);
1012 	list_add_tail(&pmu->list, pmus);
1013 
1014 	pmu->default_config = perf_pmu__get_default_config(pmu);
1015 
1016 	return pmu;
1017 err:
1018 	zfree(&pmu->name);
1019 	free(pmu);
1020 	return NULL;
1021 }
1022 
1023 /* Creates the PMU when sysfs scanning fails. */
1024 struct perf_pmu *perf_pmu__create_placeholder_core_pmu(struct list_head *core_pmus)
1025 {
1026 	struct perf_pmu *pmu = zalloc(sizeof(*pmu));
1027 
1028 	if (!pmu)
1029 		return NULL;
1030 
1031 	pmu->name = strdup("cpu");
1032 	if (!pmu->name) {
1033 		free(pmu);
1034 		return NULL;
1035 	}
1036 
1037 	pmu->is_core = true;
1038 	pmu->type = PERF_TYPE_RAW;
1039 	pmu->cpus = cpu_map__online();
1040 
1041 	INIT_LIST_HEAD(&pmu->format);
1042 	INIT_LIST_HEAD(&pmu->aliases);
1043 	INIT_LIST_HEAD(&pmu->caps);
1044 	list_add_tail(&pmu->list, core_pmus);
1045 	return pmu;
1046 }
1047 
1048 void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu)
1049 {
1050 	struct perf_pmu_format *format;
1051 
1052 	if (pmu->formats_checked)
1053 		return;
1054 
1055 	pmu->formats_checked = true;
1056 
1057 	/* fake pmu doesn't have format list */
1058 	if (pmu == &perf_pmu__fake)
1059 		return;
1060 
1061 	list_for_each_entry(format, &pmu->format, list) {
1062 		perf_pmu_format__load(pmu, format);
1063 		if (format->value >= PERF_PMU_FORMAT_VALUE_CONFIG_END) {
1064 			pr_warning("WARNING: '%s' format '%s' requires 'perf_event_attr::config%d'"
1065 				   "which is not supported by this version of perf!\n",
1066 				   pmu->name, format->name, format->value);
1067 			return;
1068 		}
1069 	}
1070 }
1071 
1072 bool evsel__is_aux_event(const struct evsel *evsel)
1073 {
1074 	struct perf_pmu *pmu = evsel__find_pmu(evsel);
1075 
1076 	return pmu && pmu->auxtrace;
1077 }
1078 
1079 /*
1080  * Set @config_name to @val as long as the user hasn't already set or cleared it
1081  * by passing a config term on the command line.
1082  *
1083  * @val is the value to put into the bits specified by @config_name rather than
1084  * the bit pattern. It is shifted into position by this function, so to set
1085  * something to true, pass 1 for val rather than a pre shifted value.
1086  */
1087 #define field_prep(_mask, _val) (((_val) << (ffsll(_mask) - 1)) & (_mask))
1088 void evsel__set_config_if_unset(struct perf_pmu *pmu, struct evsel *evsel,
1089 				const char *config_name, u64 val)
1090 {
1091 	u64 user_bits = 0, bits;
1092 	struct evsel_config_term *term = evsel__get_config_term(evsel, CFG_CHG);
1093 
1094 	if (term)
1095 		user_bits = term->val.cfg_chg;
1096 
1097 	bits = perf_pmu__format_bits(pmu, config_name);
1098 
1099 	/* Do nothing if the user changed the value */
1100 	if (bits & user_bits)
1101 		return;
1102 
1103 	/* Otherwise replace it */
1104 	evsel->core.attr.config &= ~bits;
1105 	evsel->core.attr.config |= field_prep(bits, val);
1106 }
1107 
1108 static struct perf_pmu_format *
1109 pmu_find_format(struct list_head *formats, const char *name)
1110 {
1111 	struct perf_pmu_format *format;
1112 
1113 	list_for_each_entry(format, formats, list)
1114 		if (!strcmp(format->name, name))
1115 			return format;
1116 
1117 	return NULL;
1118 }
1119 
1120 __u64 perf_pmu__format_bits(struct perf_pmu *pmu, const char *name)
1121 {
1122 	struct perf_pmu_format *format = pmu_find_format(&pmu->format, name);
1123 	__u64 bits = 0;
1124 	int fbit;
1125 
1126 	if (!format)
1127 		return 0;
1128 
1129 	for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
1130 		bits |= 1ULL << fbit;
1131 
1132 	return bits;
1133 }
1134 
1135 int perf_pmu__format_type(struct perf_pmu *pmu, const char *name)
1136 {
1137 	struct perf_pmu_format *format = pmu_find_format(&pmu->format, name);
1138 
1139 	if (!format)
1140 		return -1;
1141 
1142 	perf_pmu_format__load(pmu, format);
1143 	return format->value;
1144 }
1145 
1146 /*
1147  * Sets value based on the format definition (format parameter)
1148  * and unformatted value (value parameter).
1149  */
1150 static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
1151 			     bool zero)
1152 {
1153 	unsigned long fbit, vbit;
1154 
1155 	for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
1156 
1157 		if (!test_bit(fbit, format))
1158 			continue;
1159 
1160 		if (value & (1llu << vbit++))
1161 			*v |= (1llu << fbit);
1162 		else if (zero)
1163 			*v &= ~(1llu << fbit);
1164 	}
1165 }
1166 
1167 static __u64 pmu_format_max_value(const unsigned long *format)
1168 {
1169 	int w;
1170 
1171 	w = bitmap_weight(format, PERF_PMU_FORMAT_BITS);
1172 	if (!w)
1173 		return 0;
1174 	if (w < 64)
1175 		return (1ULL << w) - 1;
1176 	return -1;
1177 }
1178 
1179 /*
1180  * Term is a string term, and might be a param-term. Try to look up it's value
1181  * in the remaining terms.
1182  * - We have a term like "base-or-format-term=param-term",
1183  * - We need to find the value supplied for "param-term" (with param-term named
1184  *   in a config string) later on in the term list.
1185  */
1186 static int pmu_resolve_param_term(struct parse_events_term *term,
1187 				  struct parse_events_terms *head_terms,
1188 				  __u64 *value)
1189 {
1190 	struct parse_events_term *t;
1191 
1192 	list_for_each_entry(t, &head_terms->terms, list) {
1193 		if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM &&
1194 		    t->config && !strcmp(t->config, term->config)) {
1195 			t->used = true;
1196 			*value = t->val.num;
1197 			return 0;
1198 		}
1199 	}
1200 
1201 	if (verbose > 0)
1202 		printf("Required parameter '%s' not specified\n", term->config);
1203 
1204 	return -1;
1205 }
1206 
1207 static char *pmu_formats_string(struct list_head *formats)
1208 {
1209 	struct perf_pmu_format *format;
1210 	char *str = NULL;
1211 	struct strbuf buf = STRBUF_INIT;
1212 	unsigned int i = 0;
1213 
1214 	if (!formats)
1215 		return NULL;
1216 
1217 	/* sysfs exported terms */
1218 	list_for_each_entry(format, formats, list)
1219 		if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
1220 			goto error;
1221 
1222 	str = strbuf_detach(&buf, NULL);
1223 error:
1224 	strbuf_release(&buf);
1225 
1226 	return str;
1227 }
1228 
1229 /*
1230  * Setup one of config[12] attr members based on the
1231  * user input data - term parameter.
1232  */
1233 static int pmu_config_term(struct perf_pmu *pmu,
1234 			   struct perf_event_attr *attr,
1235 			   struct parse_events_term *term,
1236 			   struct parse_events_terms *head_terms,
1237 			   bool zero, struct parse_events_error *err)
1238 {
1239 	struct perf_pmu_format *format;
1240 	__u64 *vp;
1241 	__u64 val, max_val;
1242 
1243 	/*
1244 	 * If this is a parameter we've already used for parameterized-eval,
1245 	 * skip it in normal eval.
1246 	 */
1247 	if (term->used)
1248 		return 0;
1249 
1250 	/*
1251 	 * Hardcoded terms should be already in, so nothing
1252 	 * to be done for them.
1253 	 */
1254 	if (parse_events__is_hardcoded_term(term))
1255 		return 0;
1256 
1257 	format = pmu_find_format(&pmu->format, term->config);
1258 	if (!format) {
1259 		char *pmu_term = pmu_formats_string(&pmu->format);
1260 		char *unknown_term;
1261 		char *help_msg;
1262 
1263 		if (asprintf(&unknown_term,
1264 				"unknown term '%s' for pmu '%s'",
1265 				term->config, pmu->name) < 0)
1266 			unknown_term = NULL;
1267 		help_msg = parse_events_formats_error_string(pmu_term);
1268 		if (err) {
1269 			parse_events_error__handle(err, term->err_term,
1270 						   unknown_term,
1271 						   help_msg);
1272 		} else {
1273 			pr_debug("%s (%s)\n", unknown_term, help_msg);
1274 			free(unknown_term);
1275 		}
1276 		free(pmu_term);
1277 		return -EINVAL;
1278 	}
1279 	perf_pmu_format__load(pmu, format);
1280 	switch (format->value) {
1281 	case PERF_PMU_FORMAT_VALUE_CONFIG:
1282 		vp = &attr->config;
1283 		break;
1284 	case PERF_PMU_FORMAT_VALUE_CONFIG1:
1285 		vp = &attr->config1;
1286 		break;
1287 	case PERF_PMU_FORMAT_VALUE_CONFIG2:
1288 		vp = &attr->config2;
1289 		break;
1290 	case PERF_PMU_FORMAT_VALUE_CONFIG3:
1291 		vp = &attr->config3;
1292 		break;
1293 	default:
1294 		return -EINVAL;
1295 	}
1296 
1297 	/*
1298 	 * Either directly use a numeric term, or try to translate string terms
1299 	 * using event parameters.
1300 	 */
1301 	if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1302 		if (term->no_value &&
1303 		    bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
1304 			if (err) {
1305 				parse_events_error__handle(err, term->err_val,
1306 					   strdup("no value assigned for term"),
1307 					   NULL);
1308 			}
1309 			return -EINVAL;
1310 		}
1311 
1312 		val = term->val.num;
1313 	} else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1314 		if (strcmp(term->val.str, "?")) {
1315 			if (verbose > 0) {
1316 				pr_info("Invalid sysfs entry %s=%s\n",
1317 						term->config, term->val.str);
1318 			}
1319 			if (err) {
1320 				parse_events_error__handle(err, term->err_val,
1321 					strdup("expected numeric value"),
1322 					NULL);
1323 			}
1324 			return -EINVAL;
1325 		}
1326 
1327 		if (pmu_resolve_param_term(term, head_terms, &val))
1328 			return -EINVAL;
1329 	} else
1330 		return -EINVAL;
1331 
1332 	max_val = pmu_format_max_value(format->bits);
1333 	if (val > max_val) {
1334 		if (err) {
1335 			char *err_str;
1336 
1337 			parse_events_error__handle(err, term->err_val,
1338 				asprintf(&err_str,
1339 				    "value too big for format, maximum is %llu",
1340 				    (unsigned long long)max_val) < 0
1341 				    ? strdup("value too big for format")
1342 				    : err_str,
1343 				    NULL);
1344 			return -EINVAL;
1345 		}
1346 		/*
1347 		 * Assume we don't care if !err, in which case the value will be
1348 		 * silently truncated.
1349 		 */
1350 	}
1351 
1352 	pmu_format_value(format->bits, val, vp, zero);
1353 	return 0;
1354 }
1355 
1356 int perf_pmu__config_terms(struct perf_pmu *pmu,
1357 			   struct perf_event_attr *attr,
1358 			   struct parse_events_terms *terms,
1359 			   bool zero, struct parse_events_error *err)
1360 {
1361 	struct parse_events_term *term;
1362 
1363 	list_for_each_entry(term, &terms->terms, list) {
1364 		if (pmu_config_term(pmu, attr, term, terms, zero, err))
1365 			return -EINVAL;
1366 	}
1367 
1368 	return 0;
1369 }
1370 
1371 /*
1372  * Configures event's 'attr' parameter based on the:
1373  * 1) users input - specified in terms parameter
1374  * 2) pmu format definitions - specified by pmu parameter
1375  */
1376 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
1377 		     struct parse_events_terms *head_terms,
1378 		     struct parse_events_error *err)
1379 {
1380 	bool zero = !!pmu->default_config;
1381 
1382 	return perf_pmu__config_terms(pmu, attr, head_terms, zero, err);
1383 }
1384 
1385 static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
1386 					     struct parse_events_term *term)
1387 {
1388 	struct perf_pmu_alias *alias;
1389 	const char *name;
1390 
1391 	if (parse_events__is_hardcoded_term(term))
1392 		return NULL;
1393 
1394 	if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1395 		if (!term->no_value)
1396 			return NULL;
1397 		if (pmu_find_format(&pmu->format, term->config))
1398 			return NULL;
1399 		name = term->config;
1400 
1401 	} else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1402 		if (strcasecmp(term->config, "event"))
1403 			return NULL;
1404 		name = term->val.str;
1405 	} else {
1406 		return NULL;
1407 	}
1408 
1409 	alias = perf_pmu__find_alias(pmu, name, /*load=*/ true);
1410 	if (alias || pmu->cpu_aliases_added)
1411 		return alias;
1412 
1413 	/* Alias doesn't exist, try to get it from the json events. */
1414 	if (pmu->events_table &&
1415 	    pmu_events_table__find_event(pmu->events_table, pmu, name,
1416 				         pmu_add_cpu_aliases_map_callback,
1417 				         pmu) == 0) {
1418 		alias = perf_pmu__find_alias(pmu, name, /*load=*/ false);
1419 	}
1420 	return alias;
1421 }
1422 
1423 
1424 static int check_info_data(struct perf_pmu *pmu,
1425 			   struct perf_pmu_alias *alias,
1426 			   struct perf_pmu_info *info,
1427 			   struct parse_events_error *err,
1428 			   int column)
1429 {
1430 	read_alias_info(pmu, alias);
1431 	/*
1432 	 * Only one term in event definition can
1433 	 * define unit, scale and snapshot, fail
1434 	 * if there's more than one.
1435 	 */
1436 	if (info->unit && alias->unit[0]) {
1437 		parse_events_error__handle(err, column,
1438 					strdup("Attempt to set event's unit twice"),
1439 					NULL);
1440 		return -EINVAL;
1441 	}
1442 	if (info->scale && alias->scale) {
1443 		parse_events_error__handle(err, column,
1444 					strdup("Attempt to set event's scale twice"),
1445 					NULL);
1446 		return -EINVAL;
1447 	}
1448 	if (info->snapshot && alias->snapshot) {
1449 		parse_events_error__handle(err, column,
1450 					strdup("Attempt to set event snapshot twice"),
1451 					NULL);
1452 		return -EINVAL;
1453 	}
1454 
1455 	if (alias->unit[0])
1456 		info->unit = alias->unit;
1457 
1458 	if (alias->scale)
1459 		info->scale = alias->scale;
1460 
1461 	if (alias->snapshot)
1462 		info->snapshot = alias->snapshot;
1463 
1464 	return 0;
1465 }
1466 
1467 /*
1468  * Find alias in the terms list and replace it with the terms
1469  * defined for the alias
1470  */
1471 int perf_pmu__check_alias(struct perf_pmu *pmu, struct parse_events_terms *head_terms,
1472 			  struct perf_pmu_info *info, struct parse_events_error *err)
1473 {
1474 	struct parse_events_term *term, *h;
1475 	struct perf_pmu_alias *alias;
1476 	int ret;
1477 
1478 	info->per_pkg = false;
1479 
1480 	/*
1481 	 * Mark unit and scale as not set
1482 	 * (different from default values, see below)
1483 	 */
1484 	info->unit     = NULL;
1485 	info->scale    = 0.0;
1486 	info->snapshot = false;
1487 
1488 	list_for_each_entry_safe(term, h, &head_terms->terms, list) {
1489 		alias = pmu_find_alias(pmu, term);
1490 		if (!alias)
1491 			continue;
1492 		ret = pmu_alias_terms(alias, &term->list);
1493 		if (ret) {
1494 			parse_events_error__handle(err, term->err_term,
1495 						strdup("Failure to duplicate terms"),
1496 						NULL);
1497 			return ret;
1498 		}
1499 
1500 		ret = check_info_data(pmu, alias, info, err, term->err_term);
1501 		if (ret)
1502 			return ret;
1503 
1504 		if (alias->per_pkg)
1505 			info->per_pkg = true;
1506 
1507 		list_del_init(&term->list);
1508 		parse_events_term__delete(term);
1509 	}
1510 
1511 	/*
1512 	 * if no unit or scale found in aliases, then
1513 	 * set defaults as for evsel
1514 	 * unit cannot left to NULL
1515 	 */
1516 	if (info->unit == NULL)
1517 		info->unit   = "";
1518 
1519 	if (info->scale == 0.0)
1520 		info->scale  = 1.0;
1521 
1522 	return 0;
1523 }
1524 
1525 struct find_event_args {
1526 	const char *event;
1527 	void *state;
1528 	pmu_event_callback cb;
1529 };
1530 
1531 static int find_event_callback(void *state, struct pmu_event_info *info)
1532 {
1533 	struct find_event_args *args = state;
1534 
1535 	if (!strcmp(args->event, info->name))
1536 		return args->cb(args->state, info);
1537 
1538 	return 0;
1539 }
1540 
1541 int perf_pmu__find_event(struct perf_pmu *pmu, const char *event, void *state, pmu_event_callback cb)
1542 {
1543 	struct find_event_args args = {
1544 		.event = event,
1545 		.state = state,
1546 		.cb = cb,
1547 	};
1548 
1549 	/* Sub-optimal, but function is only used by tests. */
1550 	return perf_pmu__for_each_event(pmu, /*skip_duplicate_pmus=*/ false,
1551 					&args, find_event_callback);
1552 }
1553 
1554 static void perf_pmu__del_formats(struct list_head *formats)
1555 {
1556 	struct perf_pmu_format *fmt, *tmp;
1557 
1558 	list_for_each_entry_safe(fmt, tmp, formats, list) {
1559 		list_del(&fmt->list);
1560 		zfree(&fmt->name);
1561 		free(fmt);
1562 	}
1563 }
1564 
1565 bool perf_pmu__has_format(const struct perf_pmu *pmu, const char *name)
1566 {
1567 	struct perf_pmu_format *format;
1568 
1569 	list_for_each_entry(format, &pmu->format, list) {
1570 		if (!strcmp(format->name, name))
1571 			return true;
1572 	}
1573 	return false;
1574 }
1575 
1576 bool is_pmu_core(const char *name)
1577 {
1578 	return !strcmp(name, "cpu") || !strcmp(name, "cpum_cf") || is_sysfs_pmu_core(name);
1579 }
1580 
1581 bool perf_pmu__supports_legacy_cache(const struct perf_pmu *pmu)
1582 {
1583 	return pmu->is_core;
1584 }
1585 
1586 bool perf_pmu__auto_merge_stats(const struct perf_pmu *pmu)
1587 {
1588 	return !pmu->is_core || perf_pmus__num_core_pmus() == 1;
1589 }
1590 
1591 bool perf_pmu__have_event(struct perf_pmu *pmu, const char *name)
1592 {
1593 	if (perf_pmu__find_alias(pmu, name, /*load=*/ true) != NULL)
1594 		return true;
1595 	if (pmu->cpu_aliases_added || !pmu->events_table)
1596 		return false;
1597 	return pmu_events_table__find_event(pmu->events_table, pmu, name, NULL, NULL) == 0;
1598 }
1599 
1600 size_t perf_pmu__num_events(struct perf_pmu *pmu)
1601 {
1602 	size_t nr;
1603 
1604 	if (!pmu->sysfs_aliases_loaded)
1605 		pmu_aliases_parse(pmu);
1606 
1607 	nr = pmu->sysfs_aliases;
1608 
1609 	if (pmu->cpu_aliases_added)
1610 		 nr += pmu->loaded_json_aliases;
1611 	else if (pmu->events_table)
1612 		nr += pmu_events_table__num_events(pmu->events_table, pmu) - pmu->loaded_json_aliases;
1613 
1614 	return pmu->selectable ? nr + 1 : nr;
1615 }
1616 
1617 static int sub_non_neg(int a, int b)
1618 {
1619 	if (b > a)
1620 		return 0;
1621 	return a - b;
1622 }
1623 
1624 static char *format_alias(char *buf, int len, const struct perf_pmu *pmu,
1625 			  const struct perf_pmu_alias *alias, bool skip_duplicate_pmus)
1626 {
1627 	struct parse_events_term *term;
1628 	int pmu_name_len = skip_duplicate_pmus
1629 		? pmu_name_len_no_suffix(pmu->name, /*num=*/NULL)
1630 		: (int)strlen(pmu->name);
1631 	int used = snprintf(buf, len, "%.*s/%s", pmu_name_len, pmu->name, alias->name);
1632 
1633 	list_for_each_entry(term, &alias->terms.terms, list) {
1634 		if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1635 			used += snprintf(buf + used, sub_non_neg(len, used),
1636 					",%s=%s", term->config,
1637 					term->val.str);
1638 	}
1639 
1640 	if (sub_non_neg(len, used) > 0) {
1641 		buf[used] = '/';
1642 		used++;
1643 	}
1644 	if (sub_non_neg(len, used) > 0) {
1645 		buf[used] = '\0';
1646 		used++;
1647 	} else
1648 		buf[len - 1] = '\0';
1649 
1650 	return buf;
1651 }
1652 
1653 int perf_pmu__for_each_event(struct perf_pmu *pmu, bool skip_duplicate_pmus,
1654 			     void *state, pmu_event_callback cb)
1655 {
1656 	char buf[1024];
1657 	struct perf_pmu_alias *event;
1658 	struct pmu_event_info info = {
1659 		.pmu = pmu,
1660 	};
1661 	int ret = 0;
1662 	struct strbuf sb;
1663 
1664 	strbuf_init(&sb, /*hint=*/ 0);
1665 	pmu_add_cpu_aliases(pmu);
1666 	list_for_each_entry(event, &pmu->aliases, list) {
1667 		size_t buf_used;
1668 
1669 		info.pmu_name = event->pmu_name ?: pmu->name;
1670 		info.alias = NULL;
1671 		if (event->desc) {
1672 			info.name = event->name;
1673 			buf_used = 0;
1674 		} else {
1675 			info.name = format_alias(buf, sizeof(buf), pmu, event,
1676 						 skip_duplicate_pmus);
1677 			if (pmu->is_core) {
1678 				info.alias = info.name;
1679 				info.name = event->name;
1680 			}
1681 			buf_used = strlen(buf) + 1;
1682 		}
1683 		info.scale_unit = NULL;
1684 		if (strlen(event->unit) || event->scale != 1.0) {
1685 			info.scale_unit = buf + buf_used;
1686 			buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used,
1687 					"%G%s", event->scale, event->unit) + 1;
1688 		}
1689 		info.desc = event->desc;
1690 		info.long_desc = event->long_desc;
1691 		info.encoding_desc = buf + buf_used;
1692 		parse_events_terms__to_strbuf(&event->terms, &sb);
1693 		buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used,
1694 				"%s/%s/", info.pmu_name, sb.buf) + 1;
1695 		info.topic = event->topic;
1696 		info.str = sb.buf;
1697 		info.deprecated = event->deprecated;
1698 		ret = cb(state, &info);
1699 		if (ret)
1700 			goto out;
1701 		strbuf_setlen(&sb, /*len=*/ 0);
1702 	}
1703 	if (pmu->selectable) {
1704 		info.name = buf;
1705 		snprintf(buf, sizeof(buf), "%s//", pmu->name);
1706 		info.alias = NULL;
1707 		info.scale_unit = NULL;
1708 		info.desc = NULL;
1709 		info.long_desc = NULL;
1710 		info.encoding_desc = NULL;
1711 		info.topic = NULL;
1712 		info.pmu_name = pmu->name;
1713 		info.deprecated = false;
1714 		ret = cb(state, &info);
1715 	}
1716 out:
1717 	strbuf_release(&sb);
1718 	return ret;
1719 }
1720 
1721 bool pmu__name_match(const struct perf_pmu *pmu, const char *pmu_name)
1722 {
1723 	return !strcmp(pmu->name, pmu_name) ||
1724 		(pmu->is_uncore && pmu_uncore_alias_match(pmu_name, pmu->name)) ||
1725 		/*
1726 		 * jevents and tests use default_core as a marker for any core
1727 		 * PMU as the PMU name varies across architectures.
1728 		 */
1729 	        (pmu->is_core && !strcmp(pmu_name, "default_core"));
1730 }
1731 
1732 bool perf_pmu__is_software(const struct perf_pmu *pmu)
1733 {
1734 	if (pmu->is_core || pmu->is_uncore || pmu->auxtrace)
1735 		return false;
1736 	switch (pmu->type) {
1737 	case PERF_TYPE_HARDWARE:	return false;
1738 	case PERF_TYPE_SOFTWARE:	return true;
1739 	case PERF_TYPE_TRACEPOINT:	return true;
1740 	case PERF_TYPE_HW_CACHE:	return false;
1741 	case PERF_TYPE_RAW:		return false;
1742 	case PERF_TYPE_BREAKPOINT:	return true;
1743 	default: break;
1744 	}
1745 	return !strcmp(pmu->name, "kprobe") || !strcmp(pmu->name, "uprobe");
1746 }
1747 
1748 FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
1749 {
1750 	char path[PATH_MAX];
1751 
1752 	if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name) ||
1753 	    !file_available(path))
1754 		return NULL;
1755 
1756 	return fopen(path, "r");
1757 }
1758 
1759 FILE *perf_pmu__open_file_at(struct perf_pmu *pmu, int dirfd, const char *name)
1760 {
1761 	int fd;
1762 
1763 	fd = perf_pmu__pathname_fd(dirfd, pmu->name, name, O_RDONLY);
1764 	if (fd < 0)
1765 		return NULL;
1766 
1767 	return fdopen(fd, "r");
1768 }
1769 
1770 int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
1771 			...)
1772 {
1773 	va_list args;
1774 	FILE *file;
1775 	int ret = EOF;
1776 
1777 	va_start(args, fmt);
1778 	file = perf_pmu__open_file(pmu, name);
1779 	if (file) {
1780 		ret = vfscanf(file, fmt, args);
1781 		fclose(file);
1782 	}
1783 	va_end(args);
1784 	return ret;
1785 }
1786 
1787 int perf_pmu__scan_file_at(struct perf_pmu *pmu, int dirfd, const char *name,
1788 			   const char *fmt, ...)
1789 {
1790 	va_list args;
1791 	FILE *file;
1792 	int ret = EOF;
1793 
1794 	va_start(args, fmt);
1795 	file = perf_pmu__open_file_at(pmu, dirfd, name);
1796 	if (file) {
1797 		ret = vfscanf(file, fmt, args);
1798 		fclose(file);
1799 	}
1800 	va_end(args);
1801 	return ret;
1802 }
1803 
1804 bool perf_pmu__file_exists(struct perf_pmu *pmu, const char *name)
1805 {
1806 	char path[PATH_MAX];
1807 
1808 	if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name))
1809 		return false;
1810 
1811 	return file_available(path);
1812 }
1813 
1814 static int perf_pmu__new_caps(struct list_head *list, char *name, char *value)
1815 {
1816 	struct perf_pmu_caps *caps = zalloc(sizeof(*caps));
1817 
1818 	if (!caps)
1819 		return -ENOMEM;
1820 
1821 	caps->name = strdup(name);
1822 	if (!caps->name)
1823 		goto free_caps;
1824 	caps->value = strndup(value, strlen(value) - 1);
1825 	if (!caps->value)
1826 		goto free_name;
1827 	list_add_tail(&caps->list, list);
1828 	return 0;
1829 
1830 free_name:
1831 	zfree(&caps->name);
1832 free_caps:
1833 	free(caps);
1834 
1835 	return -ENOMEM;
1836 }
1837 
1838 static void perf_pmu__del_caps(struct perf_pmu *pmu)
1839 {
1840 	struct perf_pmu_caps *caps, *tmp;
1841 
1842 	list_for_each_entry_safe(caps, tmp, &pmu->caps, list) {
1843 		list_del(&caps->list);
1844 		zfree(&caps->name);
1845 		zfree(&caps->value);
1846 		free(caps);
1847 	}
1848 }
1849 
1850 /*
1851  * Reading/parsing the given pmu capabilities, which should be located at:
1852  * /sys/bus/event_source/devices/<dev>/caps as sysfs group attributes.
1853  * Return the number of capabilities
1854  */
1855 int perf_pmu__caps_parse(struct perf_pmu *pmu)
1856 {
1857 	struct stat st;
1858 	char caps_path[PATH_MAX];
1859 	DIR *caps_dir;
1860 	struct dirent *evt_ent;
1861 	int caps_fd;
1862 
1863 	if (pmu->caps_initialized)
1864 		return pmu->nr_caps;
1865 
1866 	pmu->nr_caps = 0;
1867 
1868 	if (!perf_pmu__pathname_scnprintf(caps_path, sizeof(caps_path), pmu->name, "caps"))
1869 		return -1;
1870 
1871 	if (stat(caps_path, &st) < 0) {
1872 		pmu->caps_initialized = true;
1873 		return 0;	/* no error if caps does not exist */
1874 	}
1875 
1876 	caps_dir = opendir(caps_path);
1877 	if (!caps_dir)
1878 		return -EINVAL;
1879 
1880 	caps_fd = dirfd(caps_dir);
1881 
1882 	while ((evt_ent = readdir(caps_dir)) != NULL) {
1883 		char *name = evt_ent->d_name;
1884 		char value[128];
1885 		FILE *file;
1886 		int fd;
1887 
1888 		if (!strcmp(name, ".") || !strcmp(name, ".."))
1889 			continue;
1890 
1891 		fd = openat(caps_fd, name, O_RDONLY);
1892 		if (fd == -1)
1893 			continue;
1894 		file = fdopen(fd, "r");
1895 		if (!file) {
1896 			close(fd);
1897 			continue;
1898 		}
1899 
1900 		if (!fgets(value, sizeof(value), file) ||
1901 		    (perf_pmu__new_caps(&pmu->caps, name, value) < 0)) {
1902 			fclose(file);
1903 			continue;
1904 		}
1905 
1906 		pmu->nr_caps++;
1907 		fclose(file);
1908 	}
1909 
1910 	closedir(caps_dir);
1911 
1912 	pmu->caps_initialized = true;
1913 	return pmu->nr_caps;
1914 }
1915 
1916 static void perf_pmu__compute_config_masks(struct perf_pmu *pmu)
1917 {
1918 	struct perf_pmu_format *format;
1919 
1920 	if (pmu->config_masks_computed)
1921 		return;
1922 
1923 	list_for_each_entry(format, &pmu->format, list)	{
1924 		unsigned int i;
1925 		__u64 *mask;
1926 
1927 		if (format->value >= PERF_PMU_FORMAT_VALUE_CONFIG_END)
1928 			continue;
1929 
1930 		pmu->config_masks_present = true;
1931 		mask = &pmu->config_masks[format->value];
1932 
1933 		for_each_set_bit(i, format->bits, PERF_PMU_FORMAT_BITS)
1934 			*mask |= 1ULL << i;
1935 	}
1936 	pmu->config_masks_computed = true;
1937 }
1938 
1939 void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config,
1940 				   const char *name, int config_num,
1941 				   const char *config_name)
1942 {
1943 	__u64 bits;
1944 	char buf[100];
1945 
1946 	perf_pmu__compute_config_masks(pmu);
1947 
1948 	/*
1949 	 * Kernel doesn't export any valid format bits.
1950 	 */
1951 	if (!pmu->config_masks_present)
1952 		return;
1953 
1954 	bits = config & ~pmu->config_masks[config_num];
1955 	if (bits == 0)
1956 		return;
1957 
1958 	bitmap_scnprintf((unsigned long *)&bits, sizeof(bits) * 8, buf, sizeof(buf));
1959 
1960 	pr_warning("WARNING: event '%s' not valid (bits %s of %s "
1961 		   "'%llx' not supported by kernel)!\n",
1962 		   name ?: "N/A", buf, config_name, config);
1963 }
1964 
1965 int perf_pmu__match(const char *pattern, const char *name, const char *tok)
1966 {
1967 	if (!name)
1968 		return -1;
1969 
1970 	if (fnmatch(pattern, name, 0))
1971 		return -1;
1972 
1973 	if (tok && !perf_pmu__match_ignoring_suffix(name, tok))
1974 		return -1;
1975 
1976 	return 0;
1977 }
1978 
1979 double __weak perf_pmu__cpu_slots_per_cycle(void)
1980 {
1981 	return NAN;
1982 }
1983 
1984 int perf_pmu__event_source_devices_scnprintf(char *pathname, size_t size)
1985 {
1986 	const char *sysfs = sysfs__mountpoint();
1987 
1988 	if (!sysfs)
1989 		return 0;
1990 	return scnprintf(pathname, size, "%s/bus/event_source/devices/", sysfs);
1991 }
1992 
1993 int perf_pmu__event_source_devices_fd(void)
1994 {
1995 	char path[PATH_MAX];
1996 	const char *sysfs = sysfs__mountpoint();
1997 
1998 	if (!sysfs)
1999 		return -1;
2000 
2001 	scnprintf(path, sizeof(path), "%s/bus/event_source/devices/", sysfs);
2002 	return open(path, O_DIRECTORY);
2003 }
2004 
2005 /*
2006  * Fill 'buf' with the path to a file or folder in 'pmu_name' in
2007  * sysfs. For example if pmu_name = "cs_etm" and 'filename' = "format"
2008  * then pathname will be filled with
2009  * "/sys/bus/event_source/devices/cs_etm/format"
2010  *
2011  * Return 0 if the sysfs mountpoint couldn't be found, if no characters were
2012  * written or if the buffer size is exceeded.
2013  */
2014 int perf_pmu__pathname_scnprintf(char *buf, size_t size,
2015 				 const char *pmu_name, const char *filename)
2016 {
2017 	size_t len;
2018 
2019 	len = perf_pmu__event_source_devices_scnprintf(buf, size);
2020 	if (!len || (len + strlen(pmu_name) + strlen(filename) + 1)  >= size)
2021 		return 0;
2022 
2023 	return scnprintf(buf + len, size - len, "%s/%s", pmu_name, filename);
2024 }
2025 
2026 int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *filename, int flags)
2027 {
2028 	char path[PATH_MAX];
2029 
2030 	scnprintf(path, sizeof(path), "%s/%s", pmu_name, filename);
2031 	return openat(dirfd, path, flags);
2032 }
2033 
2034 void perf_pmu__delete(struct perf_pmu *pmu)
2035 {
2036 	perf_pmu__del_formats(&pmu->format);
2037 	perf_pmu__del_aliases(pmu);
2038 	perf_pmu__del_caps(pmu);
2039 
2040 	perf_cpu_map__put(pmu->cpus);
2041 
2042 	zfree(&pmu->default_config);
2043 	zfree(&pmu->name);
2044 	zfree(&pmu->alias_name);
2045 	zfree(&pmu->id);
2046 	free(pmu);
2047 }
2048