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