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