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