xref: /openbmc/linux/tools/perf/util/pmu.c (revision 3f980eab)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2cd82a32eSJiri Olsa #include <linux/list.h>
3c5de47f2SSukadev Bhattiprolu #include <linux/compiler.h>
432858480SArnaldo Carvalho de Melo #include <linux/string.h>
57f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h>
6c47a5599SJin Yao #include <linux/ctype.h>
7fa0d9846SArnaldo Carvalho de Melo #include <subcmd/pager.h>
8cd82a32eSJiri Olsa #include <sys/types.h>
9a43783aeSArnaldo Carvalho de Melo #include <errno.h>
10c23c2a0fSArnaldo Carvalho de Melo #include <fcntl.h>
117a8ef4c4SArnaldo Carvalho de Melo #include <sys/stat.h>
12cd82a32eSJiri Olsa #include <unistd.h>
13cd82a32eSJiri Olsa #include <stdio.h>
14dc0a6202SAdrian Hunter #include <stdbool.h>
157d4bdab5SAdrian Hunter #include <stdarg.h>
16cd82a32eSJiri Olsa #include <dirent.h>
17cd0cfad7SBorislav Petkov #include <api/fs/fs.h>
18410136f5SStephane Eranian #include <locale.h>
19fbc2844eSWilliam Cohen #include <regex.h>
209c3516d1SJiri Olsa #include <perf/cpumap.h>
21c47a5599SJin Yao #include <fnmatch.h>
22acef233bSJing Zhang #include <math.h>
235e51b0bbSArnaldo Carvalho de Melo #include "debug.h"
24e12ee9f7SAdrian Hunter #include "evsel.h"
25cd82a32eSJiri Olsa #include "pmu.h"
26336b92daSRavi Bangoria #include "pmus.h"
273d88aec0SIan Rogers #include "pmu-bison.h"
283d88aec0SIan Rogers #include "pmu-flex.h"
29cd82a32eSJiri Olsa #include "parse-events.h"
30e5c6109fSIan Rogers #include "print-events.h"
31933f82ffSSukadev Bhattiprolu #include "header.h"
32a067558eSArnaldo Carvalho de Melo #include "string2.h"
33fa0d9846SArnaldo Carvalho de Melo #include "strbuf.h"
34d9664582SAndi Kleen #include "fncache.h"
3544462430SJin Yao #include "pmu-hybrid.h"
36cd82a32eSJiri Olsa 
37e46fc8d9SArnaldo Carvalho de Melo struct perf_pmu perf_pmu__fake;
38e46fc8d9SArnaldo Carvalho de Melo 
39fe13d43dSIan Rogers /**
40fe13d43dSIan Rogers  * struct perf_pmu_format - Values from a format file read from
41fe13d43dSIan Rogers  * <sysfs>/devices/cpu/format/ held in struct perf_pmu.
42fe13d43dSIan Rogers  *
43fe13d43dSIan Rogers  * For example, the contents of <sysfs>/devices/cpu/format/event may be
44fe13d43dSIan Rogers  * "config:0-7" and will be represented here as name="event",
45fe13d43dSIan Rogers  * value=PERF_PMU_FORMAT_VALUE_CONFIG and bits 0 to 7 will be set.
46fe13d43dSIan Rogers  */
47ab1bf653SArnaldo Carvalho de Melo struct perf_pmu_format {
48fe13d43dSIan Rogers 	/** @name: The modifier/file name. */
49ab1bf653SArnaldo Carvalho de Melo 	char *name;
50fe13d43dSIan Rogers 	/**
51fe13d43dSIan Rogers 	 * @value : Which config value the format relates to. Supported values
52fe13d43dSIan Rogers 	 * are from PERF_PMU_FORMAT_VALUE_CONFIG to
53fe13d43dSIan Rogers 	 * PERF_PMU_FORMAT_VALUE_CONFIG_END.
54fe13d43dSIan Rogers 	 */
55ab1bf653SArnaldo Carvalho de Melo 	int value;
56fe13d43dSIan Rogers 	/** @bits: Which config bits are set by this format value. */
57ab1bf653SArnaldo Carvalho de Melo 	DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
58fe13d43dSIan Rogers 	/** @list: Element on list within struct perf_pmu. */
59ab1bf653SArnaldo Carvalho de Melo 	struct list_head list;
60ab1bf653SArnaldo Carvalho de Melo };
61ab1bf653SArnaldo Carvalho de Melo 
62c5a26ea4SJin Yao static bool hybrid_scanned;
63cd82a32eSJiri Olsa 
64e293a5e8SNamhyung Kim static struct perf_pmu *perf_pmu__find2(int dirfd, const char *name);
65e293a5e8SNamhyung Kim 
66cd82a32eSJiri Olsa /*
67cd82a32eSJiri Olsa  * Parse & process all the sysfs attributes located under
68cd82a32eSJiri Olsa  * the directory specified in 'dir' parameter.
69cd82a32eSJiri Olsa  */
70e293a5e8SNamhyung Kim int perf_pmu__format_parse(int dirfd, struct list_head *head)
71cd82a32eSJiri Olsa {
72cd82a32eSJiri Olsa 	struct dirent *evt_ent;
73cd82a32eSJiri Olsa 	DIR *format_dir;
74cd82a32eSJiri Olsa 	int ret = 0;
75cd82a32eSJiri Olsa 
76e293a5e8SNamhyung Kim 	format_dir = fdopendir(dirfd);
77cd82a32eSJiri Olsa 	if (!format_dir)
78cd82a32eSJiri Olsa 		return -EINVAL;
79cd82a32eSJiri Olsa 
80cd82a32eSJiri Olsa 	while (!ret && (evt_ent = readdir(format_dir))) {
81cd82a32eSJiri Olsa 		char *name = evt_ent->d_name;
82e293a5e8SNamhyung Kim 		int fd;
833d88aec0SIan Rogers 		void *scanner;
843d88aec0SIan Rogers 		FILE *file;
85cd82a32eSJiri Olsa 
86cd82a32eSJiri Olsa 		if (!strcmp(name, ".") || !strcmp(name, ".."))
87cd82a32eSJiri Olsa 			continue;
88cd82a32eSJiri Olsa 
89cd82a32eSJiri Olsa 
90cd82a32eSJiri Olsa 		ret = -EINVAL;
91e293a5e8SNamhyung Kim 		fd = openat(dirfd, name, O_RDONLY);
92e293a5e8SNamhyung Kim 		if (fd < 0)
93cd82a32eSJiri Olsa 			break;
94cd82a32eSJiri Olsa 
953d88aec0SIan Rogers 		file = fdopen(fd, "r");
963d88aec0SIan Rogers 		if (!file) {
973d88aec0SIan Rogers 			close(fd);
983d88aec0SIan Rogers 			break;
993d88aec0SIan Rogers 		}
1003d88aec0SIan Rogers 
1013d88aec0SIan Rogers 		ret = perf_pmu_lex_init(&scanner);
1023d88aec0SIan Rogers 		if (ret) {
1033d88aec0SIan Rogers 			fclose(file);
1043d88aec0SIan Rogers 			break;
1053d88aec0SIan Rogers 		}
1063d88aec0SIan Rogers 
1073d88aec0SIan Rogers 		perf_pmu_set_in(file, scanner);
1083d88aec0SIan Rogers 		ret = perf_pmu_parse(head, name, scanner);
1093d88aec0SIan Rogers 		perf_pmu_lex_destroy(scanner);
1103d88aec0SIan Rogers 		fclose(file);
111cd82a32eSJiri Olsa 	}
112cd82a32eSJiri Olsa 
113cd82a32eSJiri Olsa 	closedir(format_dir);
114cd82a32eSJiri Olsa 	return ret;
115cd82a32eSJiri Olsa }
116cd82a32eSJiri Olsa 
117cd82a32eSJiri Olsa /*
118cd82a32eSJiri Olsa  * Reading/parsing the default pmu format definition, which should be
119cd82a32eSJiri Olsa  * located at:
120cd82a32eSJiri Olsa  * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
121cd82a32eSJiri Olsa  */
122e293a5e8SNamhyung Kim static int pmu_format(int dirfd, const char *name, struct list_head *format)
123cd82a32eSJiri Olsa {
124e293a5e8SNamhyung Kim 	int fd;
125cd82a32eSJiri Olsa 
126e293a5e8SNamhyung Kim 	fd = perf_pmu__pathname_fd(dirfd, name, "format", O_DIRECTORY);
127e293a5e8SNamhyung Kim 	if (fd < 0)
128d9664582SAndi Kleen 		return 0;
129cd82a32eSJiri Olsa 
130e293a5e8SNamhyung Kim 	/* it'll close the fd */
131e293a5e8SNamhyung Kim 	if (perf_pmu__format_parse(fd, format))
132cd82a32eSJiri Olsa 		return -1;
133cd82a32eSJiri Olsa 
134cd82a32eSJiri Olsa 	return 0;
135cd82a32eSJiri Olsa }
136cd82a32eSJiri Olsa 
137a55ab7c4SJin Yao int perf_pmu__convert_scale(const char *scale, char **end, double *sval)
138d02fc6bcSAndi Kleen {
139d02fc6bcSAndi Kleen 	char *lc;
140d02fc6bcSAndi Kleen 	int ret = 0;
141d02fc6bcSAndi Kleen 
142d02fc6bcSAndi Kleen 	/*
143d02fc6bcSAndi Kleen 	 * save current locale
144d02fc6bcSAndi Kleen 	 */
145d02fc6bcSAndi Kleen 	lc = setlocale(LC_NUMERIC, NULL);
146d02fc6bcSAndi Kleen 
147d02fc6bcSAndi Kleen 	/*
148d02fc6bcSAndi Kleen 	 * The lc string may be allocated in static storage,
149d02fc6bcSAndi Kleen 	 * so get a dynamic copy to make it survive setlocale
150d02fc6bcSAndi Kleen 	 * call below.
151d02fc6bcSAndi Kleen 	 */
152d02fc6bcSAndi Kleen 	lc = strdup(lc);
153d02fc6bcSAndi Kleen 	if (!lc) {
154d02fc6bcSAndi Kleen 		ret = -ENOMEM;
155d02fc6bcSAndi Kleen 		goto out;
156d02fc6bcSAndi Kleen 	}
157d02fc6bcSAndi Kleen 
158d02fc6bcSAndi Kleen 	/*
159d02fc6bcSAndi Kleen 	 * force to C locale to ensure kernel
160d02fc6bcSAndi Kleen 	 * scale string is converted correctly.
161d02fc6bcSAndi Kleen 	 * kernel uses default C locale.
162d02fc6bcSAndi Kleen 	 */
163d02fc6bcSAndi Kleen 	setlocale(LC_NUMERIC, "C");
164d02fc6bcSAndi Kleen 
165d02fc6bcSAndi Kleen 	*sval = strtod(scale, end);
166d02fc6bcSAndi Kleen 
167d02fc6bcSAndi Kleen out:
168d02fc6bcSAndi Kleen 	/* restore locale */
169d02fc6bcSAndi Kleen 	setlocale(LC_NUMERIC, lc);
170d02fc6bcSAndi Kleen 	free(lc);
171d02fc6bcSAndi Kleen 	return ret;
172d02fc6bcSAndi Kleen }
173d02fc6bcSAndi Kleen 
174e293a5e8SNamhyung Kim static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, int dirfd, char *name)
175410136f5SStephane Eranian {
176410136f5SStephane Eranian 	struct stat st;
177410136f5SStephane Eranian 	ssize_t sret;
178410136f5SStephane Eranian 	char scale[128];
179410136f5SStephane Eranian 	int fd, ret = -1;
180410136f5SStephane Eranian 	char path[PATH_MAX];
181410136f5SStephane Eranian 
182e293a5e8SNamhyung Kim 	scnprintf(path, PATH_MAX, "%s.scale", name);
183410136f5SStephane Eranian 
184e293a5e8SNamhyung Kim 	fd = openat(dirfd, path, O_RDONLY);
185410136f5SStephane Eranian 	if (fd == -1)
186410136f5SStephane Eranian 		return -1;
187410136f5SStephane Eranian 
188410136f5SStephane Eranian 	if (fstat(fd, &st) < 0)
189410136f5SStephane Eranian 		goto error;
190410136f5SStephane Eranian 
191410136f5SStephane Eranian 	sret = read(fd, scale, sizeof(scale)-1);
192410136f5SStephane Eranian 	if (sret < 0)
193410136f5SStephane Eranian 		goto error;
194410136f5SStephane Eranian 
1959ecae065SMadhavan Srinivasan 	if (scale[sret - 1] == '\n')
1969ecae065SMadhavan Srinivasan 		scale[sret - 1] = '\0';
1979ecae065SMadhavan Srinivasan 	else
198410136f5SStephane Eranian 		scale[sret] = '\0';
1999ecae065SMadhavan Srinivasan 
200a55ab7c4SJin Yao 	ret = perf_pmu__convert_scale(scale, NULL, &alias->scale);
201410136f5SStephane Eranian error:
202410136f5SStephane Eranian 	close(fd);
203410136f5SStephane Eranian 	return ret;
204410136f5SStephane Eranian }
205410136f5SStephane Eranian 
206e293a5e8SNamhyung Kim static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, int dirfd, char *name)
207410136f5SStephane Eranian {
208410136f5SStephane Eranian 	char path[PATH_MAX];
209410136f5SStephane Eranian 	ssize_t sret;
210410136f5SStephane Eranian 	int fd;
211410136f5SStephane Eranian 
212e293a5e8SNamhyung Kim 	scnprintf(path, PATH_MAX, "%s.unit", name);
213410136f5SStephane Eranian 
214e293a5e8SNamhyung Kim 	fd = openat(dirfd, path, O_RDONLY);
215410136f5SStephane Eranian 	if (fd == -1)
216410136f5SStephane Eranian 		return -1;
217410136f5SStephane Eranian 
218410136f5SStephane Eranian 	sret = read(fd, alias->unit, UNIT_MAX_LEN);
219410136f5SStephane Eranian 	if (sret < 0)
220410136f5SStephane Eranian 		goto error;
221410136f5SStephane Eranian 
222410136f5SStephane Eranian 	close(fd);
223410136f5SStephane Eranian 
2249ecae065SMadhavan Srinivasan 	if (alias->unit[sret - 1] == '\n')
2259ecae065SMadhavan Srinivasan 		alias->unit[sret - 1] = '\0';
2269ecae065SMadhavan Srinivasan 	else
227410136f5SStephane Eranian 		alias->unit[sret] = '\0';
228410136f5SStephane Eranian 
229410136f5SStephane Eranian 	return 0;
230410136f5SStephane Eranian error:
231410136f5SStephane Eranian 	close(fd);
232410136f5SStephane Eranian 	alias->unit[0] = '\0';
233410136f5SStephane Eranian 	return -1;
234410136f5SStephane Eranian }
235410136f5SStephane Eranian 
236044330c1SMatt Fleming static int
237e293a5e8SNamhyung Kim perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, int dirfd, char *name)
238044330c1SMatt Fleming {
239044330c1SMatt Fleming 	char path[PATH_MAX];
240044330c1SMatt Fleming 	int fd;
241044330c1SMatt Fleming 
242e293a5e8SNamhyung Kim 	scnprintf(path, PATH_MAX, "%s.per-pkg", name);
243044330c1SMatt Fleming 
244e293a5e8SNamhyung Kim 	fd = openat(dirfd, path, O_RDONLY);
245044330c1SMatt Fleming 	if (fd == -1)
246044330c1SMatt Fleming 		return -1;
247044330c1SMatt Fleming 
248044330c1SMatt Fleming 	close(fd);
249044330c1SMatt Fleming 
250044330c1SMatt Fleming 	alias->per_pkg = true;
251044330c1SMatt Fleming 	return 0;
252044330c1SMatt Fleming }
253044330c1SMatt Fleming 
2541d9e446bSJiri Olsa static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias,
255e293a5e8SNamhyung Kim 				    int dirfd, char *name)
2561d9e446bSJiri Olsa {
2571d9e446bSJiri Olsa 	char path[PATH_MAX];
2581d9e446bSJiri Olsa 	int fd;
2591d9e446bSJiri Olsa 
260e293a5e8SNamhyung Kim 	scnprintf(path, PATH_MAX, "%s.snapshot", name);
2611d9e446bSJiri Olsa 
262e293a5e8SNamhyung Kim 	fd = openat(dirfd, path, O_RDONLY);
2631d9e446bSJiri Olsa 	if (fd == -1)
2641d9e446bSJiri Olsa 		return -1;
2651d9e446bSJiri Olsa 
2661d9e446bSJiri Olsa 	alias->snapshot = true;
2671d9e446bSJiri Olsa 	close(fd);
2681d9e446bSJiri Olsa 	return 0;
2691d9e446bSJiri Olsa }
2701d9e446bSJiri Olsa 
2716dde6429SThomas Richter static void perf_pmu_assign_str(char *name, const char *field, char **old_str,
2726dde6429SThomas Richter 				char **new_str)
2736dde6429SThomas Richter {
2746dde6429SThomas Richter 	if (!*old_str)
2756dde6429SThomas Richter 		goto set_new;
2766dde6429SThomas Richter 
2776dde6429SThomas Richter 	if (*new_str) {	/* Have new string, check with old */
2786dde6429SThomas Richter 		if (strcasecmp(*old_str, *new_str))
2796dde6429SThomas Richter 			pr_debug("alias %s differs in field '%s'\n",
2806dde6429SThomas Richter 				 name, field);
2816dde6429SThomas Richter 		zfree(old_str);
2826dde6429SThomas Richter 	} else		/* Nothing new --> keep old string */
2836dde6429SThomas Richter 		return;
2846dde6429SThomas Richter set_new:
2856dde6429SThomas Richter 	*old_str = *new_str;
2866dde6429SThomas Richter 	*new_str = NULL;
2876dde6429SThomas Richter }
2886dde6429SThomas Richter 
2896dde6429SThomas Richter static void perf_pmu_update_alias(struct perf_pmu_alias *old,
2906dde6429SThomas Richter 				  struct perf_pmu_alias *newalias)
2916dde6429SThomas Richter {
2926dde6429SThomas Richter 	perf_pmu_assign_str(old->name, "desc", &old->desc, &newalias->desc);
2936dde6429SThomas Richter 	perf_pmu_assign_str(old->name, "long_desc", &old->long_desc,
2946dde6429SThomas Richter 			    &newalias->long_desc);
2956dde6429SThomas Richter 	perf_pmu_assign_str(old->name, "topic", &old->topic, &newalias->topic);
2966dde6429SThomas Richter 	perf_pmu_assign_str(old->name, "value", &old->str, &newalias->str);
2976dde6429SThomas Richter 	old->scale = newalias->scale;
2986dde6429SThomas Richter 	old->per_pkg = newalias->per_pkg;
2996dde6429SThomas Richter 	old->snapshot = newalias->snapshot;
3006dde6429SThomas Richter 	memcpy(old->unit, newalias->unit, sizeof(old->unit));
3016dde6429SThomas Richter }
3026dde6429SThomas Richter 
3036dde6429SThomas Richter /* Delete an alias entry. */
30422fe5a25SNamhyung Kim void perf_pmu_free_alias(struct perf_pmu_alias *newalias)
3056dde6429SThomas Richter {
3066dde6429SThomas Richter 	zfree(&newalias->name);
3076dde6429SThomas Richter 	zfree(&newalias->desc);
3086dde6429SThomas Richter 	zfree(&newalias->long_desc);
3096dde6429SThomas Richter 	zfree(&newalias->topic);
3106dde6429SThomas Richter 	zfree(&newalias->str);
31132705de7SJin Yao 	zfree(&newalias->pmu_name);
3126dde6429SThomas Richter 	parse_events_terms__purge(&newalias->terms);
3136dde6429SThomas Richter 	free(newalias);
3146dde6429SThomas Richter }
3156dde6429SThomas Richter 
316eec11310SNamhyung Kim static void perf_pmu__del_aliases(struct perf_pmu *pmu)
317eec11310SNamhyung Kim {
318eec11310SNamhyung Kim 	struct perf_pmu_alias *alias, *tmp;
319eec11310SNamhyung Kim 
320eec11310SNamhyung Kim 	list_for_each_entry_safe(alias, tmp, &pmu->aliases, list) {
321eec11310SNamhyung Kim 		list_del(&alias->list);
322eec11310SNamhyung Kim 		perf_pmu_free_alias(alias);
323eec11310SNamhyung Kim 	}
324eec11310SNamhyung Kim }
325eec11310SNamhyung Kim 
3266dde6429SThomas Richter /* Merge an alias, search in alias list. If this name is already
3276dde6429SThomas Richter  * present merge both of them to combine all information.
3286dde6429SThomas Richter  */
3296dde6429SThomas Richter static bool perf_pmu_merge_alias(struct perf_pmu_alias *newalias,
3306dde6429SThomas Richter 				 struct list_head *alist)
3316dde6429SThomas Richter {
3326dde6429SThomas Richter 	struct perf_pmu_alias *a;
3336dde6429SThomas Richter 
3346dde6429SThomas Richter 	list_for_each_entry(a, alist, list) {
3356dde6429SThomas Richter 		if (!strcasecmp(newalias->name, a->name)) {
33632705de7SJin Yao 			if (newalias->pmu_name && a->pmu_name &&
33732705de7SJin Yao 			    !strcasecmp(newalias->pmu_name, a->pmu_name)) {
33832705de7SJin Yao 				continue;
33932705de7SJin Yao 			}
3406dde6429SThomas Richter 			perf_pmu_update_alias(a, newalias);
3416dde6429SThomas Richter 			perf_pmu_free_alias(newalias);
3426dde6429SThomas Richter 			return true;
3436dde6429SThomas Richter 		}
3446dde6429SThomas Richter 	}
3456dde6429SThomas Richter 	return false;
3466dde6429SThomas Richter }
3476dde6429SThomas Richter 
348e293a5e8SNamhyung Kim static int __perf_pmu__new_alias(struct list_head *list, int dirfd, char *name,
34947f572aaSIan Rogers 				 char *desc, char *val, const struct pmu_event *pe)
350a6146d50SZheng Yan {
3510c24d6fbSThomas Richter 	struct parse_events_term *term;
3525c6ccc37SArnaldo Carvalho de Melo 	struct perf_pmu_alias *alias;
353a6146d50SZheng Yan 	int ret;
3540c24d6fbSThomas Richter 	char newval[256];
355330f40a0SIan Rogers 	const char *long_desc = NULL, *topic = NULL, *unit = NULL, *pmu_name = NULL;
356bd680861SIan Rogers 	bool deprecated = false, perpkg = false;
357eab35953SJin Yao 
358eab35953SJin Yao 	if (pe) {
359330f40a0SIan Rogers 		long_desc = pe->long_desc;
360330f40a0SIan Rogers 		topic = pe->topic;
361330f40a0SIan Rogers 		unit = pe->unit;
362bd680861SIan Rogers 		perpkg = pe->perpkg;
3639ed8b7dcSIan Rogers 		deprecated = pe->deprecated;
364330f40a0SIan Rogers 		pmu_name = pe->pmu;
365eab35953SJin Yao 	}
366a6146d50SZheng Yan 
367a6146d50SZheng Yan 	alias = malloc(sizeof(*alias));
368a6146d50SZheng Yan 	if (!alias)
369a6146d50SZheng Yan 		return -ENOMEM;
370a6146d50SZheng Yan 
371a6146d50SZheng Yan 	INIT_LIST_HEAD(&alias->terms);
372410136f5SStephane Eranian 	alias->scale = 1.0;
373410136f5SStephane Eranian 	alias->unit[0] = '\0';
374bd680861SIan Rogers 	alias->per_pkg = perpkg;
37584530920SStephane Eranian 	alias->snapshot = false;
3769ed8b7dcSIan Rogers 	alias->deprecated = deprecated;
377410136f5SStephane Eranian 
37870c646e0SSukadev Bhattiprolu 	ret = parse_events_terms(&alias->terms, val);
379a6146d50SZheng Yan 	if (ret) {
38070c646e0SSukadev Bhattiprolu 		pr_err("Cannot parse alias %s: %d\n", val, ret);
381a6146d50SZheng Yan 		free(alias);
382a6146d50SZheng Yan 		return ret;
383a6146d50SZheng Yan 	}
384a6146d50SZheng Yan 
3850c24d6fbSThomas Richter 	/* Scan event and remove leading zeroes, spaces, newlines, some
3860c24d6fbSThomas Richter 	 * platforms have terms specified as
3870c24d6fbSThomas Richter 	 * event=0x0091 (read from files ../<PMU>/events/<FILE>
3880c24d6fbSThomas Richter 	 * and terms specified as event=0x91 (read from JSON files).
3890c24d6fbSThomas Richter 	 *
3900c24d6fbSThomas Richter 	 * Rebuild string to make alias->str member comparable.
3910c24d6fbSThomas Richter 	 */
3920c24d6fbSThomas Richter 	memset(newval, 0, sizeof(newval));
3930c24d6fbSThomas Richter 	ret = 0;
3940c24d6fbSThomas Richter 	list_for_each_entry(term, &alias->terms, list) {
3950c24d6fbSThomas Richter 		if (ret)
3960c24d6fbSThomas Richter 			ret += scnprintf(newval + ret, sizeof(newval) - ret,
3970c24d6fbSThomas Richter 					 ",");
3980c24d6fbSThomas Richter 		if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
3990c24d6fbSThomas Richter 			ret += scnprintf(newval + ret, sizeof(newval) - ret,
4000c24d6fbSThomas Richter 					 "%s=%#x", term->config, term->val.num);
4010c24d6fbSThomas Richter 		else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
4020c24d6fbSThomas Richter 			ret += scnprintf(newval + ret, sizeof(newval) - ret,
4030c24d6fbSThomas Richter 					 "%s=%s", term->config, term->val.str);
4040c24d6fbSThomas Richter 	}
4050c24d6fbSThomas Richter 
406a6146d50SZheng Yan 	alias->name = strdup(name);
407e293a5e8SNamhyung Kim 	if (dirfd >= 0) {
408410136f5SStephane Eranian 		/*
409410136f5SStephane Eranian 		 * load unit name and scale if available
410410136f5SStephane Eranian 		 */
411e293a5e8SNamhyung Kim 		perf_pmu__parse_unit(alias, dirfd, name);
412e293a5e8SNamhyung Kim 		perf_pmu__parse_scale(alias, dirfd, name);
413e293a5e8SNamhyung Kim 		perf_pmu__parse_per_pkg(alias, dirfd, name);
414e293a5e8SNamhyung Kim 		perf_pmu__parse_snapshot(alias, dirfd, name);
41570c646e0SSukadev Bhattiprolu 	}
416410136f5SStephane Eranian 
41708e60ed1SAndi Kleen 	alias->desc = desc ? strdup(desc) : NULL;
418c8d6828aSSukadev Bhattiprolu 	alias->long_desc = long_desc ? strdup(long_desc) :
419c8d6828aSSukadev Bhattiprolu 				desc ? strdup(desc) : NULL;
420dd5f1036SAndi Kleen 	alias->topic = topic ? strdup(topic) : NULL;
421fedb2b51SAndi Kleen 	if (unit) {
422330f40a0SIan Rogers 		if (perf_pmu__convert_scale(unit, (char **)&unit, &alias->scale) < 0)
423fedb2b51SAndi Kleen 			return -1;
424fedb2b51SAndi Kleen 		snprintf(alias->unit, sizeof(alias->unit), "%s", unit);
425fedb2b51SAndi Kleen 	}
4260c24d6fbSThomas Richter 	alias->str = strdup(newval);
42732705de7SJin Yao 	alias->pmu_name = pmu_name ? strdup(pmu_name) : NULL;
428f2361024SAndi Kleen 
4296dde6429SThomas Richter 	if (!perf_pmu_merge_alias(alias, list))
430a6146d50SZheng Yan 		list_add_tail(&alias->list, list);
431410136f5SStephane Eranian 
432a6146d50SZheng Yan 	return 0;
433a6146d50SZheng Yan }
434a6146d50SZheng Yan 
435e293a5e8SNamhyung Kim static int perf_pmu__new_alias(struct list_head *list, int dirfd, char *name, FILE *file)
43670c646e0SSukadev Bhattiprolu {
43770c646e0SSukadev Bhattiprolu 	char buf[256];
43870c646e0SSukadev Bhattiprolu 	int ret;
43970c646e0SSukadev Bhattiprolu 
44070c646e0SSukadev Bhattiprolu 	ret = fread(buf, 1, sizeof(buf), file);
44170c646e0SSukadev Bhattiprolu 	if (ret == 0)
44270c646e0SSukadev Bhattiprolu 		return -EINVAL;
44370c646e0SSukadev Bhattiprolu 
44470c646e0SSukadev Bhattiprolu 	buf[ret] = 0;
44570c646e0SSukadev Bhattiprolu 
446ea23ac73SThomas Richter 	/* Remove trailing newline from sysfs file */
44713c230abSArnaldo Carvalho de Melo 	strim(buf);
448ea23ac73SThomas Richter 
449e293a5e8SNamhyung Kim 	return __perf_pmu__new_alias(list, dirfd, name, NULL, buf, NULL);
45070c646e0SSukadev Bhattiprolu }
45170c646e0SSukadev Bhattiprolu 
45246441bdcSMatt Fleming static inline bool pmu_alias_info_file(char *name)
45346441bdcSMatt Fleming {
45446441bdcSMatt Fleming 	size_t len;
45546441bdcSMatt Fleming 
45646441bdcSMatt Fleming 	len = strlen(name);
45746441bdcSMatt Fleming 	if (len > 5 && !strcmp(name + len - 5, ".unit"))
45846441bdcSMatt Fleming 		return true;
45946441bdcSMatt Fleming 	if (len > 6 && !strcmp(name + len - 6, ".scale"))
46046441bdcSMatt Fleming 		return true;
461044330c1SMatt Fleming 	if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
462044330c1SMatt Fleming 		return true;
4631d9e446bSJiri Olsa 	if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
4641d9e446bSJiri Olsa 		return true;
46546441bdcSMatt Fleming 
46646441bdcSMatt Fleming 	return false;
46746441bdcSMatt Fleming }
46846441bdcSMatt Fleming 
469a6146d50SZheng Yan /*
470a6146d50SZheng Yan  * Process all the sysfs attributes located under the directory
471a6146d50SZheng Yan  * specified in 'dir' parameter.
472a6146d50SZheng Yan  */
473e293a5e8SNamhyung Kim static int pmu_aliases_parse(int dirfd, struct list_head *head)
474a6146d50SZheng Yan {
475a6146d50SZheng Yan 	struct dirent *evt_ent;
476a6146d50SZheng Yan 	DIR *event_dir;
477e293a5e8SNamhyung Kim 	int fd;
478a6146d50SZheng Yan 
479e293a5e8SNamhyung Kim 	event_dir = fdopendir(dirfd);
480a6146d50SZheng Yan 	if (!event_dir)
481a6146d50SZheng Yan 		return -EINVAL;
482a6146d50SZheng Yan 
483940db6dcSAndi Kleen 	while ((evt_ent = readdir(event_dir))) {
484a6146d50SZheng Yan 		char *name = evt_ent->d_name;
485a6146d50SZheng Yan 		FILE *file;
486a6146d50SZheng Yan 
487a6146d50SZheng Yan 		if (!strcmp(name, ".") || !strcmp(name, ".."))
488a6146d50SZheng Yan 			continue;
489a6146d50SZheng Yan 
490410136f5SStephane Eranian 		/*
49146441bdcSMatt Fleming 		 * skip info files parsed in perf_pmu__new_alias()
492410136f5SStephane Eranian 		 */
49346441bdcSMatt Fleming 		if (pmu_alias_info_file(name))
494410136f5SStephane Eranian 			continue;
495410136f5SStephane Eranian 
496e293a5e8SNamhyung Kim 		fd = openat(dirfd, name, O_RDONLY);
4970ea8920eSIan Rogers 		if (fd == -1) {
4980ea8920eSIan Rogers 			pr_debug("Cannot open %s\n", name);
4990ea8920eSIan Rogers 			continue;
5000ea8920eSIan Rogers 		}
501e293a5e8SNamhyung Kim 		file = fdopen(fd, "r");
502940db6dcSAndi Kleen 		if (!file) {
5030ea8920eSIan Rogers 			close(fd);
504940db6dcSAndi Kleen 			continue;
505940db6dcSAndi Kleen 		}
506410136f5SStephane Eranian 
507e293a5e8SNamhyung Kim 		if (perf_pmu__new_alias(head, dirfd, name, file) < 0)
508940db6dcSAndi Kleen 			pr_debug("Cannot set up %s\n", name);
509a6146d50SZheng Yan 		fclose(file);
510a6146d50SZheng Yan 	}
511a6146d50SZheng Yan 
512a6146d50SZheng Yan 	closedir(event_dir);
513940db6dcSAndi Kleen 	return 0;
514a6146d50SZheng Yan }
515a6146d50SZheng Yan 
516a6146d50SZheng Yan /*
517a6146d50SZheng Yan  * Reading the pmu event aliases definition, which should be located at:
518a6146d50SZheng Yan  * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
519a6146d50SZheng Yan  */
520e293a5e8SNamhyung Kim static int pmu_aliases(int dirfd, const char *name, struct list_head *head)
521a6146d50SZheng Yan {
522e293a5e8SNamhyung Kim 	int fd;
523a6146d50SZheng Yan 
524e293a5e8SNamhyung Kim 	fd = perf_pmu__pathname_fd(dirfd, name, "events", O_DIRECTORY);
525e293a5e8SNamhyung Kim 	if (fd < 0)
526d9664582SAndi Kleen 		return 0;
527a6146d50SZheng Yan 
528e293a5e8SNamhyung Kim 	/* it'll close the fd */
529e293a5e8SNamhyung Kim 	if (pmu_aliases_parse(fd, head))
530a6146d50SZheng Yan 		return -1;
531a6146d50SZheng Yan 
532a6146d50SZheng Yan 	return 0;
533a6146d50SZheng Yan }
534a6146d50SZheng Yan 
5355c6ccc37SArnaldo Carvalho de Melo static int pmu_alias_terms(struct perf_pmu_alias *alias,
536a6146d50SZheng Yan 			   struct list_head *terms)
537a6146d50SZheng Yan {
5387c2f8164SJiri Olsa 	struct parse_events_term *term, *cloned;
539a6146d50SZheng Yan 	LIST_HEAD(list);
540a6146d50SZheng Yan 	int ret;
541a6146d50SZheng Yan 
542a6146d50SZheng Yan 	list_for_each_entry(term, &alias->terms, list) {
5437c2f8164SJiri Olsa 		ret = parse_events_term__clone(&cloned, term);
544a6146d50SZheng Yan 		if (ret) {
545682dc24cSArnaldo Carvalho de Melo 			parse_events_terms__purge(&list);
546a6146d50SZheng Yan 			return ret;
547a6146d50SZheng Yan 		}
548c2f1ceadSAndi Kleen 		/*
549c2f1ceadSAndi Kleen 		 * Weak terms don't override command line options,
550c2f1ceadSAndi Kleen 		 * which we don't want for implicit terms in aliases.
551c2f1ceadSAndi Kleen 		 */
552c2f1ceadSAndi Kleen 		cloned->weak = true;
5537c2f8164SJiri Olsa 		list_add_tail(&cloned->list, &list);
554a6146d50SZheng Yan 	}
555a6146d50SZheng Yan 	list_splice(&list, terms);
556a6146d50SZheng Yan 	return 0;
557a6146d50SZheng Yan }
558a6146d50SZheng Yan 
55950a9667cSRobert Richter /* Add all pmus in sysfs to pmu list: */
56050a9667cSRobert Richter static void pmu_read_sysfs(void)
56150a9667cSRobert Richter {
562e293a5e8SNamhyung Kim 	int fd;
56350a9667cSRobert Richter 	DIR *dir;
56450a9667cSRobert Richter 	struct dirent *dent;
56550a9667cSRobert Richter 
566e293a5e8SNamhyung Kim 	fd = perf_pmu__event_source_devices_fd();
567e293a5e8SNamhyung Kim 	if (fd < 0)
56850a9667cSRobert Richter 		return;
56950a9667cSRobert Richter 
570e293a5e8SNamhyung Kim 	dir = fdopendir(fd);
57150a9667cSRobert Richter 	if (!dir)
57250a9667cSRobert Richter 		return;
57350a9667cSRobert Richter 
57450a9667cSRobert Richter 	while ((dent = readdir(dir))) {
57550a9667cSRobert Richter 		if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
57650a9667cSRobert Richter 			continue;
57750a9667cSRobert Richter 		/* add to static LIST_HEAD(pmus): */
578e293a5e8SNamhyung Kim 		perf_pmu__find2(fd, dent->d_name);
57950a9667cSRobert Richter 	}
58050a9667cSRobert Richter 
58150a9667cSRobert Richter 	closedir(dir);
58250a9667cSRobert Richter }
58350a9667cSRobert Richter 
58466ec1191SMark Rutland /*
58566ec1191SMark Rutland  * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64)
58666ec1191SMark Rutland  * may have a "cpus" file.
58766ec1191SMark Rutland  */
5883a69672eSNamhyung Kim static struct perf_cpu_map *pmu_cpumask(int dirfd, const char *name)
5897ae92e74SYan, Zheng {
590f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
5917e3fcffeSMark Rutland 	const char *templates[] = {
592d50a79cdSJames Clark 		"cpumask",
593d50a79cdSJames Clark 		"cpus",
5947e3fcffeSMark Rutland 		NULL
5957e3fcffeSMark Rutland 	};
5967e3fcffeSMark Rutland 	const char **template;
597d50a79cdSJames Clark 	char pmu_name[PATH_MAX];
598d50a79cdSJames Clark 	struct perf_pmu pmu = {.name = pmu_name};
599d50a79cdSJames Clark 	FILE *file;
6007ae92e74SYan, Zheng 
601d50a79cdSJames Clark 	strlcpy(pmu_name, name, sizeof(pmu_name));
6027e3fcffeSMark Rutland 	for (template = templates; *template; template++) {
6033a69672eSNamhyung Kim 		file = perf_pmu__open_file_at(&pmu, dirfd, *template);
604d50a79cdSJames Clark 		if (!file)
605d50a79cdSJames Clark 			continue;
606d50a79cdSJames Clark 		cpus = perf_cpu_map__read(file);
6073a69672eSNamhyung Kim 		fclose(file);
60866ec1191SMark Rutland 		if (cpus)
60966ec1191SMark Rutland 			return cpus;
6107e3fcffeSMark Rutland 	}
6117ae92e74SYan, Zheng 
6127ae92e74SYan, Zheng 	return NULL;
61366ec1191SMark Rutland }
6147ae92e74SYan, Zheng 
6153a69672eSNamhyung Kim static bool pmu_is_uncore(int dirfd, const char *name)
61666ec1191SMark Rutland {
6173a69672eSNamhyung Kim 	int fd;
6187ae92e74SYan, Zheng 
61944462430SJin Yao 	if (perf_pmu__hybrid_mounted(name))
62044462430SJin Yao 		return false;
62144462430SJin Yao 
6223a69672eSNamhyung Kim 	fd = perf_pmu__pathname_fd(dirfd, name, "cpumask", O_PATH);
6233a69672eSNamhyung Kim 	if (fd < 0)
6243a69672eSNamhyung Kim 		return false;
6253a69672eSNamhyung Kim 
6263a69672eSNamhyung Kim 	close(fd);
6273a69672eSNamhyung Kim 	return true;
6287ae92e74SYan, Zheng }
6297ae92e74SYan, Zheng 
63051d54847SJohn Garry static char *pmu_id(const char *name)
63151d54847SJohn Garry {
63251d54847SJohn Garry 	char path[PATH_MAX], *str;
63351d54847SJohn Garry 	size_t len;
63451d54847SJohn Garry 
6355f2c8efaSJames Clark 	perf_pmu__pathname_scnprintf(path, sizeof(path), name, "identifier");
63651d54847SJohn Garry 
6375f2c8efaSJames Clark 	if (filename__read_str(path, &str, &len) < 0)
63851d54847SJohn Garry 		return NULL;
63951d54847SJohn Garry 
64051d54847SJohn Garry 	str[len - 1] = 0; /* remove line feed */
64151d54847SJohn Garry 
64251d54847SJohn Garry 	return str;
64351d54847SJohn Garry }
64451d54847SJohn Garry 
645933f82ffSSukadev Bhattiprolu /*
64614b22ae0SGanapatrao Kulkarni  *  PMU CORE devices have different name other than cpu in sysfs on some
647292c34c1SKan Liang  *  platforms.
648292c34c1SKan Liang  *  Looking for possible sysfs files to identify the arm core device.
64914b22ae0SGanapatrao Kulkarni  */
650292c34c1SKan Liang static int is_arm_pmu_core(const char *name)
65114b22ae0SGanapatrao Kulkarni {
65214b22ae0SGanapatrao Kulkarni 	char path[PATH_MAX];
65314b22ae0SGanapatrao Kulkarni 
654f8ad6018SJames Clark 	if (!perf_pmu__pathname_scnprintf(path, sizeof(path), name, "cpus"))
65514b22ae0SGanapatrao Kulkarni 		return 0;
656d9664582SAndi Kleen 	return file_available(path);
65714b22ae0SGanapatrao Kulkarni }
65814b22ae0SGanapatrao Kulkarni 
65929be2fe0SIan Rogers char *perf_pmu__getcpuid(struct perf_pmu *pmu)
660d77ade9fSAndi Kleen {
661d77ade9fSAndi Kleen 	char *cpuid;
662d77ade9fSAndi Kleen 	static bool printed;
663d77ade9fSAndi Kleen 
664d77ade9fSAndi Kleen 	cpuid = getenv("PERF_CPUID");
665d77ade9fSAndi Kleen 	if (cpuid)
666d77ade9fSAndi Kleen 		cpuid = strdup(cpuid);
667d77ade9fSAndi Kleen 	if (!cpuid)
66854e32dc0SGanapatrao Kulkarni 		cpuid = get_cpuid_str(pmu);
669d77ade9fSAndi Kleen 	if (!cpuid)
670d77ade9fSAndi Kleen 		return NULL;
671d77ade9fSAndi Kleen 
672d77ade9fSAndi Kleen 	if (!printed) {
673d77ade9fSAndi Kleen 		pr_debug("Using CPUID %s\n", cpuid);
674d77ade9fSAndi Kleen 		printed = true;
675d77ade9fSAndi Kleen 	}
676d77ade9fSAndi Kleen 	return cpuid;
677d77ade9fSAndi Kleen }
678d77ade9fSAndi Kleen 
6791ba3752aSIan Rogers __weak const struct pmu_events_table *pmu_events_table__find(void)
680e126bef5SJohn Garry {
68196d2a746SIan Rogers 	return perf_pmu__find_events_table(NULL);
682e126bef5SJohn Garry }
683e126bef5SJohn Garry 
684f8ea2c15SIan Rogers __weak const struct pmu_metrics_table *pmu_metrics_table__find(void)
685f8ea2c15SIan Rogers {
686f8ea2c15SIan Rogers 	return perf_pmu__find_metrics_table(NULL);
687f8ea2c15SIan Rogers }
688f8ea2c15SIan Rogers 
689240e6fd0SIan Rogers /**
690240e6fd0SIan Rogers  * perf_pmu__match_ignoring_suffix - Does the pmu_name match tok ignoring any
691240e6fd0SIan Rogers  *                                   trailing suffix? The Suffix must be in form
692240e6fd0SIan Rogers  *                                   tok_{digits}, or tok{digits}.
693240e6fd0SIan Rogers  * @pmu_name: The pmu_name with possible suffix.
694240e6fd0SIan Rogers  * @tok: The possible match to pmu_name without suffix.
695c07d5c92SJohn Garry  */
696240e6fd0SIan Rogers static bool perf_pmu__match_ignoring_suffix(const char *pmu_name, const char *tok)
697c47a5599SJin Yao {
698c07d5c92SJohn Garry 	const char *p;
699c47a5599SJin Yao 
700c47a5599SJin Yao 	if (strncmp(pmu_name, tok, strlen(tok)))
701c47a5599SJin Yao 		return false;
702c47a5599SJin Yao 
703c47a5599SJin Yao 	p = pmu_name + strlen(tok);
704c47a5599SJin Yao 	if (*p == 0)
705c47a5599SJin Yao 		return true;
706c47a5599SJin Yao 
707c07d5c92SJohn Garry 	if (*p == '_')
708c47a5599SJin Yao 		++p;
709c07d5c92SJohn Garry 
710c07d5c92SJohn Garry 	/* Ensure we end in a number */
711c07d5c92SJohn Garry 	while (1) {
712c07d5c92SJohn Garry 		if (!isdigit(*p))
713c47a5599SJin Yao 			return false;
714c07d5c92SJohn Garry 		if (*(++p) == 0)
715c07d5c92SJohn Garry 			break;
716c07d5c92SJohn Garry 	}
717c47a5599SJin Yao 
718c47a5599SJin Yao 	return true;
719c47a5599SJin Yao }
720c47a5599SJin Yao 
721240e6fd0SIan Rogers /**
722240e6fd0SIan Rogers  * pmu_uncore_alias_match - does name match the PMU name?
723240e6fd0SIan Rogers  * @pmu_name: the json struct pmu_event name. This may lack a suffix (which
724240e6fd0SIan Rogers  *            matches) or be of the form "socket,pmuname" which will match
725240e6fd0SIan Rogers  *            "socketX_pmunameY".
726240e6fd0SIan Rogers  * @name: a real full PMU name as from sysfs.
727240e6fd0SIan Rogers  */
728240e6fd0SIan Rogers static bool pmu_uncore_alias_match(const char *pmu_name, const char *name)
729730670b1SJohn Garry {
730730670b1SJohn Garry 	char *tmp = NULL, *tok, *str;
731730670b1SJohn Garry 	bool res;
732730670b1SJohn Garry 
733240e6fd0SIan Rogers 	if (strchr(pmu_name, ',') == NULL)
734240e6fd0SIan Rogers 		return perf_pmu__match_ignoring_suffix(name, pmu_name);
735240e6fd0SIan Rogers 
736730670b1SJohn Garry 	str = strdup(pmu_name);
737730670b1SJohn Garry 	if (!str)
738730670b1SJohn Garry 		return false;
739730670b1SJohn Garry 
740730670b1SJohn Garry 	/*
741730670b1SJohn Garry 	 * uncore alias may be from different PMU with common prefix
742730670b1SJohn Garry 	 */
743730670b1SJohn Garry 	tok = strtok_r(str, ",", &tmp);
744730670b1SJohn Garry 	if (strncmp(pmu_name, tok, strlen(tok))) {
745730670b1SJohn Garry 		res = false;
746730670b1SJohn Garry 		goto out;
747730670b1SJohn Garry 	}
748730670b1SJohn Garry 
749730670b1SJohn Garry 	/*
750730670b1SJohn Garry 	 * Match more complex aliases where the alias name is a comma-delimited
751730670b1SJohn Garry 	 * list of tokens, orderly contained in the matching PMU name.
752730670b1SJohn Garry 	 *
753730670b1SJohn Garry 	 * Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we
754730670b1SJohn Garry 	 *	    match "socket" in "socketX_pmunameY" and then "pmuname" in
755730670b1SJohn Garry 	 *	    "pmunameY".
756730670b1SJohn Garry 	 */
757c07d5c92SJohn Garry 	while (1) {
758c07d5c92SJohn Garry 		char *next_tok = strtok_r(NULL, ",", &tmp);
759c07d5c92SJohn Garry 
760730670b1SJohn Garry 		name = strstr(name, tok);
761c07d5c92SJohn Garry 		if (!name ||
762240e6fd0SIan Rogers 		    (!next_tok && !perf_pmu__match_ignoring_suffix(name, tok))) {
763730670b1SJohn Garry 			res = false;
764730670b1SJohn Garry 			goto out;
765730670b1SJohn Garry 		}
766c07d5c92SJohn Garry 		if (!next_tok)
767c07d5c92SJohn Garry 			break;
768c07d5c92SJohn Garry 		tok = next_tok;
769c07d5c92SJohn Garry 		name += strlen(tok);
770730670b1SJohn Garry 	}
771730670b1SJohn Garry 
772730670b1SJohn Garry 	res = true;
773730670b1SJohn Garry out:
774730670b1SJohn Garry 	free(str);
775730670b1SJohn Garry 	return res;
776730670b1SJohn Garry }
777730670b1SJohn Garry 
778660842e4SIan Rogers struct pmu_add_cpu_aliases_map_data {
779660842e4SIan Rogers 	struct list_head *head;
780660842e4SIan Rogers 	const char *name;
781660842e4SIan Rogers 	const char *cpu_name;
782660842e4SIan Rogers 	struct perf_pmu *pmu;
783660842e4SIan Rogers };
784660842e4SIan Rogers 
785660842e4SIan Rogers static int pmu_add_cpu_aliases_map_callback(const struct pmu_event *pe,
7861ba3752aSIan Rogers 					const struct pmu_events_table *table __maybe_unused,
787660842e4SIan Rogers 					void *vdata)
788660842e4SIan Rogers {
789660842e4SIan Rogers 	struct pmu_add_cpu_aliases_map_data *data = vdata;
790660842e4SIan Rogers 	const char *pname = pe->pmu ? pe->pmu : data->cpu_name;
791660842e4SIan Rogers 
792660842e4SIan Rogers 	if (data->pmu->is_uncore && pmu_uncore_alias_match(pname, data->name))
793660842e4SIan Rogers 		goto new_alias;
794660842e4SIan Rogers 
795660842e4SIan Rogers 	if (strcmp(pname, data->name))
796660842e4SIan Rogers 		return 0;
797660842e4SIan Rogers 
798660842e4SIan Rogers new_alias:
799660842e4SIan Rogers 	/* need type casts to override 'const' */
800e293a5e8SNamhyung Kim 	__perf_pmu__new_alias(data->head, -1, (char *)pe->name, (char *)pe->desc,
801660842e4SIan Rogers 			      (char *)pe->event, pe);
802660842e4SIan Rogers 	return 0;
803660842e4SIan Rogers }
804660842e4SIan Rogers 
805933f82ffSSukadev Bhattiprolu /*
806933f82ffSSukadev Bhattiprolu  * From the pmu_events_map, find the table of PMU events that corresponds
807933f82ffSSukadev Bhattiprolu  * to the current running CPU. Then, add all PMU events from that table
808933f82ffSSukadev Bhattiprolu  * as aliases.
809933f82ffSSukadev Bhattiprolu  */
810eeac7730SIan Rogers void pmu_add_cpu_aliases_table(struct list_head *head, struct perf_pmu *pmu,
8111ba3752aSIan Rogers 			       const struct pmu_events_table *table)
812933f82ffSSukadev Bhattiprolu {
813660842e4SIan Rogers 	struct pmu_add_cpu_aliases_map_data data = {
814660842e4SIan Rogers 		.head = head,
815660842e4SIan Rogers 		.name = pmu->name,
816660842e4SIan Rogers 		.cpu_name = is_arm_pmu_core(pmu->name) ? pmu->name : "cpu",
817660842e4SIan Rogers 		.pmu = pmu,
818660842e4SIan Rogers 	};
819fedb2b51SAndi Kleen 
820660842e4SIan Rogers 	pmu_events_table_for_each_event(table, pmu_add_cpu_aliases_map_callback, &data);
821933f82ffSSukadev Bhattiprolu }
822933f82ffSSukadev Bhattiprolu 
823e45ad701SJohn Garry static void pmu_add_cpu_aliases(struct list_head *head, struct perf_pmu *pmu)
824e45ad701SJohn Garry {
8251ba3752aSIan Rogers 	const struct pmu_events_table *table;
826e45ad701SJohn Garry 
82796d2a746SIan Rogers 	table = perf_pmu__find_events_table(pmu);
828eeac7730SIan Rogers 	if (!table)
829e45ad701SJohn Garry 		return;
830e45ad701SJohn Garry 
831eeac7730SIan Rogers 	pmu_add_cpu_aliases_table(head, pmu, table);
832e45ad701SJohn Garry }
833e45ad701SJohn Garry 
8344513c719SJohn Garry struct pmu_sys_event_iter_data {
8354513c719SJohn Garry 	struct list_head *head;
8364513c719SJohn Garry 	struct perf_pmu *pmu;
8374513c719SJohn Garry };
8384513c719SJohn Garry 
83929be2fe0SIan Rogers static int pmu_add_sys_aliases_iter_fn(const struct pmu_event *pe,
8401ba3752aSIan Rogers 				       const struct pmu_events_table *table __maybe_unused,
84129be2fe0SIan Rogers 				       void *data)
8424513c719SJohn Garry {
8434513c719SJohn Garry 	struct pmu_sys_event_iter_data *idata = data;
8444513c719SJohn Garry 	struct perf_pmu *pmu = idata->pmu;
8454513c719SJohn Garry 
8464513c719SJohn Garry 	if (!pe->compat || !pe->pmu)
8474513c719SJohn Garry 		return 0;
8484513c719SJohn Garry 
8494513c719SJohn Garry 	if (!strcmp(pmu->id, pe->compat) &&
8504513c719SJohn Garry 	    pmu_uncore_alias_match(pe->pmu, pmu->name)) {
851e293a5e8SNamhyung Kim 		__perf_pmu__new_alias(idata->head, -1,
8524513c719SJohn Garry 				      (char *)pe->name,
8534513c719SJohn Garry 				      (char *)pe->desc,
8544513c719SJohn Garry 				      (char *)pe->event,
855eab35953SJin Yao 				      pe);
8564513c719SJohn Garry 	}
8574513c719SJohn Garry 
8584513c719SJohn Garry 	return 0;
8594513c719SJohn Garry }
8604513c719SJohn Garry 
861e199f47fSJohn Garry void pmu_add_sys_aliases(struct list_head *head, struct perf_pmu *pmu)
8624513c719SJohn Garry {
8634513c719SJohn Garry 	struct pmu_sys_event_iter_data idata = {
8644513c719SJohn Garry 		.head = head,
8654513c719SJohn Garry 		.pmu = pmu,
8664513c719SJohn Garry 	};
8674513c719SJohn Garry 
8684513c719SJohn Garry 	if (!pmu->id)
8694513c719SJohn Garry 		return;
8704513c719SJohn Garry 
8714513c719SJohn Garry 	pmu_for_each_sys_event(pmu_add_sys_aliases_iter_fn, &idata);
8724513c719SJohn Garry }
8734513c719SJohn Garry 
874c5de47f2SSukadev Bhattiprolu struct perf_event_attr * __weak
875dc0a6202SAdrian Hunter perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
876dc0a6202SAdrian Hunter {
877dc0a6202SAdrian Hunter 	return NULL;
878dc0a6202SAdrian Hunter }
879dc0a6202SAdrian Hunter 
88013d60ba0SKan Liang char * __weak
88113d60ba0SKan Liang pmu_find_real_name(const char *name)
88213d60ba0SKan Liang {
88313d60ba0SKan Liang 	return (char *)name;
88413d60ba0SKan Liang }
88513d60ba0SKan Liang 
88613d60ba0SKan Liang char * __weak
88713d60ba0SKan Liang pmu_find_alias_name(const char *name __maybe_unused)
88813d60ba0SKan Liang {
88913d60ba0SKan Liang 	return NULL;
89013d60ba0SKan Liang }
89113d60ba0SKan Liang 
8923a69672eSNamhyung Kim static int pmu_max_precise(int dirfd, struct perf_pmu *pmu)
89390a86bdeSJiri Olsa {
89490a86bdeSJiri Olsa 	int max_precise = -1;
89590a86bdeSJiri Olsa 
8963a69672eSNamhyung Kim 	perf_pmu__scan_file_at(pmu, dirfd, "caps/max_precise", "%d", &max_precise);
89790a86bdeSJiri Olsa 	return max_precise;
89890a86bdeSJiri Olsa }
89990a86bdeSJiri Olsa 
900e293a5e8SNamhyung Kim static struct perf_pmu *pmu_lookup(int dirfd, const char *lookup_name)
901cd82a32eSJiri Olsa {
902cd82a32eSJiri Olsa 	struct perf_pmu *pmu;
903cd82a32eSJiri Olsa 	LIST_HEAD(format);
904a6146d50SZheng Yan 	LIST_HEAD(aliases);
905cd82a32eSJiri Olsa 	__u32 type;
90613d60ba0SKan Liang 	char *name = pmu_find_real_name(lookup_name);
90749afa7f6SJin Yao 	bool is_hybrid = perf_pmu__hybrid_mounted(name);
90813d60ba0SKan Liang 	char *alias_name;
90949afa7f6SJin Yao 
91049afa7f6SJin Yao 	/*
91149afa7f6SJin Yao 	 * Check pmu name for hybrid and the pmu may be invalid in sysfs
91249afa7f6SJin Yao 	 */
91349afa7f6SJin Yao 	if (!strncmp(name, "cpu_", 4) && !is_hybrid)
91449afa7f6SJin Yao 		return NULL;
915cd82a32eSJiri Olsa 
916cd82a32eSJiri Olsa 	/*
917cd82a32eSJiri Olsa 	 * The pmu data we store & need consists of the pmu
918cd82a32eSJiri Olsa 	 * type value and format definitions. Load both right
919cd82a32eSJiri Olsa 	 * now.
920cd82a32eSJiri Olsa 	 */
921e293a5e8SNamhyung Kim 	if (pmu_format(dirfd, name, &format))
922cd82a32eSJiri Olsa 		return NULL;
923cd82a32eSJiri Olsa 
92415b22ed3SAndi Kleen 	/*
925f8ad6018SJames Clark 	 * Check the aliases first to avoid unnecessary work.
92615b22ed3SAndi Kleen 	 */
927e293a5e8SNamhyung Kim 	if (pmu_aliases(dirfd, name, &aliases))
9283fded963SJiri Olsa 		return NULL;
9293fded963SJiri Olsa 
930cd82a32eSJiri Olsa 	pmu = zalloc(sizeof(*pmu));
931cd82a32eSJiri Olsa 	if (!pmu)
932cd82a32eSJiri Olsa 		return NULL;
933cd82a32eSJiri Olsa 
9343a69672eSNamhyung Kim 	pmu->cpus = pmu_cpumask(dirfd, name);
93554e32dc0SGanapatrao Kulkarni 	pmu->name = strdup(name);
936f8ad6018SJames Clark 
93713d60ba0SKan Liang 	if (!pmu->name)
93813d60ba0SKan Liang 		goto err;
93913d60ba0SKan Liang 
940f8ad6018SJames Clark 	/* Read type, and ensure that type value is successfully assigned (return 1) */
9413a69672eSNamhyung Kim 	if (perf_pmu__scan_file_at(pmu, dirfd, "type", "%u", &type) != 1)
942f8ad6018SJames Clark 		goto err;
943f8ad6018SJames Clark 
94413d60ba0SKan Liang 	alias_name = pmu_find_alias_name(name);
94513d60ba0SKan Liang 	if (alias_name) {
94613d60ba0SKan Liang 		pmu->alias_name = strdup(alias_name);
94713d60ba0SKan Liang 		if (!pmu->alias_name)
94813d60ba0SKan Liang 			goto err;
94913d60ba0SKan Liang 	}
95013d60ba0SKan Liang 
95154e32dc0SGanapatrao Kulkarni 	pmu->type = type;
9523a69672eSNamhyung Kim 	pmu->is_uncore = pmu_is_uncore(dirfd, name);
95351d54847SJohn Garry 	if (pmu->is_uncore)
95451d54847SJohn Garry 		pmu->id = pmu_id(name);
9553a69672eSNamhyung Kim 	pmu->max_precise = pmu_max_precise(dirfd, pmu);
95654e32dc0SGanapatrao Kulkarni 	pmu_add_cpu_aliases(&aliases, pmu);
9574513c719SJohn Garry 	pmu_add_sys_aliases(&aliases, pmu);
95866ec1191SMark Rutland 
959cd82a32eSJiri Olsa 	INIT_LIST_HEAD(&pmu->format);
960a6146d50SZheng Yan 	INIT_LIST_HEAD(&pmu->aliases);
9619fbc61f8SKan Liang 	INIT_LIST_HEAD(&pmu->caps);
962cd82a32eSJiri Olsa 	list_splice(&format, &pmu->format);
963a6146d50SZheng Yan 	list_splice(&aliases, &pmu->aliases);
9649bc8f9feSRobert Richter 	list_add_tail(&pmu->list, &pmus);
965dc0a6202SAdrian Hunter 
966e5f4afbeSIan Rogers 	if (is_hybrid)
96744462430SJin Yao 		list_add_tail(&pmu->hybrid_list, &perf_pmu__hybrid_pmus);
968eec11310SNamhyung Kim 	else
969eec11310SNamhyung Kim 		INIT_LIST_HEAD(&pmu->hybrid_list);
97044462430SJin Yao 
971dc0a6202SAdrian Hunter 	pmu->default_config = perf_pmu__get_default_config(pmu);
972dc0a6202SAdrian Hunter 
973cd82a32eSJiri Olsa 	return pmu;
97413d60ba0SKan Liang err:
97513d60ba0SKan Liang 	if (pmu->name)
97613d60ba0SKan Liang 		free(pmu->name);
97713d60ba0SKan Liang 	free(pmu);
97813d60ba0SKan Liang 	return NULL;
979cd82a32eSJiri Olsa }
980cd82a32eSJiri Olsa 
981e552b7beSRob Herring void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu)
982e552b7beSRob Herring {
983e552b7beSRob Herring 	struct perf_pmu_format *format;
984e552b7beSRob Herring 
985e552b7beSRob Herring 	/* fake pmu doesn't have format list */
986e552b7beSRob Herring 	if (pmu == &perf_pmu__fake)
987e552b7beSRob Herring 		return;
988e552b7beSRob Herring 
989e552b7beSRob Herring 	list_for_each_entry(format, &pmu->format, list)
990e552b7beSRob Herring 		if (format->value >= PERF_PMU_FORMAT_VALUE_CONFIG_END) {
991e552b7beSRob Herring 			pr_warning("WARNING: '%s' format '%s' requires 'perf_event_attr::config%d'"
992e552b7beSRob Herring 				   "which is not supported by this version of perf!\n",
993e552b7beSRob Herring 				   pmu->name, format->name, format->value);
994e552b7beSRob Herring 			return;
995e552b7beSRob Herring 		}
996e552b7beSRob Herring }
997e552b7beSRob Herring 
998b6b96fb4SAdrian Hunter static struct perf_pmu *pmu_find(const char *name)
999cd82a32eSJiri Olsa {
1000cd82a32eSJiri Olsa 	struct perf_pmu *pmu;
1001cd82a32eSJiri Olsa 
100213d60ba0SKan Liang 	list_for_each_entry(pmu, &pmus, list) {
100313d60ba0SKan Liang 		if (!strcmp(pmu->name, name) ||
100413d60ba0SKan Liang 		    (pmu->alias_name && !strcmp(pmu->alias_name, name)))
1005cd82a32eSJiri Olsa 			return pmu;
100613d60ba0SKan Liang 	}
1007cd82a32eSJiri Olsa 
1008cd82a32eSJiri Olsa 	return NULL;
1009cd82a32eSJiri Olsa }
1010cd82a32eSJiri Olsa 
10113a50dc76SStephane Eranian struct perf_pmu *perf_pmu__find_by_type(unsigned int type)
10123a50dc76SStephane Eranian {
10133a50dc76SStephane Eranian 	struct perf_pmu *pmu;
10143a50dc76SStephane Eranian 
10153a50dc76SStephane Eranian 	list_for_each_entry(pmu, &pmus, list)
10163a50dc76SStephane Eranian 		if (pmu->type == type)
10173a50dc76SStephane Eranian 			return pmu;
10183a50dc76SStephane Eranian 
10193a50dc76SStephane Eranian 	return NULL;
10203a50dc76SStephane Eranian }
10213a50dc76SStephane Eranian 
102250a9667cSRobert Richter struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
102350a9667cSRobert Richter {
102450a9667cSRobert Richter 	/*
102550a9667cSRobert Richter 	 * pmu iterator: If pmu is NULL, we start at the begin,
102650a9667cSRobert Richter 	 * otherwise return the next pmu. Returns NULL on end.
102750a9667cSRobert Richter 	 */
102850a9667cSRobert Richter 	if (!pmu) {
102950a9667cSRobert Richter 		pmu_read_sysfs();
103050a9667cSRobert Richter 		pmu = list_prepare_entry(pmu, &pmus, list);
103150a9667cSRobert Richter 	}
103250a9667cSRobert Richter 	list_for_each_entry_continue(pmu, &pmus, list)
103350a9667cSRobert Richter 		return pmu;
103450a9667cSRobert Richter 	return NULL;
103550a9667cSRobert Richter }
103650a9667cSRobert Richter 
1037c6d616feSIan Rogers struct perf_pmu *evsel__find_pmu(const struct evsel *evsel)
1038e12ee9f7SAdrian Hunter {
1039e12ee9f7SAdrian Hunter 	struct perf_pmu *pmu = NULL;
1040e12ee9f7SAdrian Hunter 
1041f7400262SNamhyung Kim 	if (evsel->pmu)
1042f7400262SNamhyung Kim 		return evsel->pmu;
1043f7400262SNamhyung Kim 
1044e12ee9f7SAdrian Hunter 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1045e12ee9f7SAdrian Hunter 		if (pmu->type == evsel->core.attr.type)
1046e12ee9f7SAdrian Hunter 			break;
1047e12ee9f7SAdrian Hunter 	}
1048e12ee9f7SAdrian Hunter 
1049c6d616feSIan Rogers 	((struct evsel *)evsel)->pmu = pmu;
1050e12ee9f7SAdrian Hunter 	return pmu;
1051e12ee9f7SAdrian Hunter }
1052e12ee9f7SAdrian Hunter 
1053c6d616feSIan Rogers bool evsel__is_aux_event(const struct evsel *evsel)
1054e12ee9f7SAdrian Hunter {
1055e76026bdSArnaldo Carvalho de Melo 	struct perf_pmu *pmu = evsel__find_pmu(evsel);
1056e12ee9f7SAdrian Hunter 
1057e12ee9f7SAdrian Hunter 	return pmu && pmu->auxtrace;
1058e12ee9f7SAdrian Hunter }
1059e12ee9f7SAdrian Hunter 
1060b6b96fb4SAdrian Hunter struct perf_pmu *perf_pmu__find(const char *name)
1061cd82a32eSJiri Olsa {
1062cd82a32eSJiri Olsa 	struct perf_pmu *pmu;
1063e293a5e8SNamhyung Kim 	int dirfd;
1064cd82a32eSJiri Olsa 
1065cd82a32eSJiri Olsa 	/*
1066cd82a32eSJiri Olsa 	 * Once PMU is loaded it stays in the list,
1067cd82a32eSJiri Olsa 	 * so we keep us from multiple reading/parsing
1068cd82a32eSJiri Olsa 	 * the pmu format definitions.
1069cd82a32eSJiri Olsa 	 */
1070cd82a32eSJiri Olsa 	pmu = pmu_find(name);
1071cd82a32eSJiri Olsa 	if (pmu)
1072cd82a32eSJiri Olsa 		return pmu;
1073cd82a32eSJiri Olsa 
1074e293a5e8SNamhyung Kim 	dirfd = perf_pmu__event_source_devices_fd();
1075e293a5e8SNamhyung Kim 	pmu = pmu_lookup(dirfd, name);
1076e293a5e8SNamhyung Kim 	close(dirfd);
1077e293a5e8SNamhyung Kim 
1078e293a5e8SNamhyung Kim 	return pmu;
1079e293a5e8SNamhyung Kim }
1080e293a5e8SNamhyung Kim 
1081e293a5e8SNamhyung Kim static struct perf_pmu *perf_pmu__find2(int dirfd, const char *name)
1082e293a5e8SNamhyung Kim {
1083e293a5e8SNamhyung Kim 	struct perf_pmu *pmu;
1084e293a5e8SNamhyung Kim 
1085e293a5e8SNamhyung Kim 	/*
1086e293a5e8SNamhyung Kim 	 * Once PMU is loaded it stays in the list,
1087e293a5e8SNamhyung Kim 	 * so we keep us from multiple reading/parsing
1088e293a5e8SNamhyung Kim 	 * the pmu format definitions.
1089e293a5e8SNamhyung Kim 	 */
1090e293a5e8SNamhyung Kim 	pmu = pmu_find(name);
1091e293a5e8SNamhyung Kim 	if (pmu)
1092e293a5e8SNamhyung Kim 		return pmu;
1093e293a5e8SNamhyung Kim 
1094e293a5e8SNamhyung Kim 	return pmu_lookup(dirfd, name);
1095cd82a32eSJiri Olsa }
1096cd82a32eSJiri Olsa 
10975c6ccc37SArnaldo Carvalho de Melo static struct perf_pmu_format *
109809ff6071SAdrian Hunter pmu_find_format(struct list_head *formats, const char *name)
1099cd82a32eSJiri Olsa {
11005c6ccc37SArnaldo Carvalho de Melo 	struct perf_pmu_format *format;
1101cd82a32eSJiri Olsa 
1102cd82a32eSJiri Olsa 	list_for_each_entry(format, formats, list)
1103cd82a32eSJiri Olsa 		if (!strcmp(format->name, name))
1104cd82a32eSJiri Olsa 			return format;
1105cd82a32eSJiri Olsa 
1106cd82a32eSJiri Olsa 	return NULL;
1107cd82a32eSJiri Olsa }
1108cd82a32eSJiri Olsa 
110909ff6071SAdrian Hunter __u64 perf_pmu__format_bits(struct list_head *formats, const char *name)
111009ff6071SAdrian Hunter {
111109ff6071SAdrian Hunter 	struct perf_pmu_format *format = pmu_find_format(formats, name);
111209ff6071SAdrian Hunter 	__u64 bits = 0;
111309ff6071SAdrian Hunter 	int fbit;
111409ff6071SAdrian Hunter 
111509ff6071SAdrian Hunter 	if (!format)
111609ff6071SAdrian Hunter 		return 0;
111709ff6071SAdrian Hunter 
111809ff6071SAdrian Hunter 	for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
111909ff6071SAdrian Hunter 		bits |= 1ULL << fbit;
112009ff6071SAdrian Hunter 
112109ff6071SAdrian Hunter 	return bits;
112209ff6071SAdrian Hunter }
112309ff6071SAdrian Hunter 
1124a1ac7de6SAdrian Hunter int perf_pmu__format_type(struct list_head *formats, const char *name)
1125a1ac7de6SAdrian Hunter {
1126a1ac7de6SAdrian Hunter 	struct perf_pmu_format *format = pmu_find_format(formats, name);
1127a1ac7de6SAdrian Hunter 
1128a1ac7de6SAdrian Hunter 	if (!format)
1129a1ac7de6SAdrian Hunter 		return -1;
1130a1ac7de6SAdrian Hunter 
1131a1ac7de6SAdrian Hunter 	return format->value;
1132a1ac7de6SAdrian Hunter }
1133a1ac7de6SAdrian Hunter 
1134cd82a32eSJiri Olsa /*
1135dc0a6202SAdrian Hunter  * Sets value based on the format definition (format parameter)
11364d39c89fSIngo Molnar  * and unformatted value (value parameter).
1137cd82a32eSJiri Olsa  */
1138dc0a6202SAdrian Hunter static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
1139dc0a6202SAdrian Hunter 			     bool zero)
1140cd82a32eSJiri Olsa {
1141cd82a32eSJiri Olsa 	unsigned long fbit, vbit;
1142cd82a32eSJiri Olsa 
1143cd82a32eSJiri Olsa 	for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
1144cd82a32eSJiri Olsa 
1145cd82a32eSJiri Olsa 		if (!test_bit(fbit, format))
1146cd82a32eSJiri Olsa 			continue;
1147cd82a32eSJiri Olsa 
1148dc0a6202SAdrian Hunter 		if (value & (1llu << vbit++))
1149dc0a6202SAdrian Hunter 			*v |= (1llu << fbit);
1150dc0a6202SAdrian Hunter 		else if (zero)
1151dc0a6202SAdrian Hunter 			*v &= ~(1llu << fbit);
1152cd82a32eSJiri Olsa 	}
1153cd82a32eSJiri Olsa }
1154cd82a32eSJiri Olsa 
11550efe6b67SAdrian Hunter static __u64 pmu_format_max_value(const unsigned long *format)
11560efe6b67SAdrian Hunter {
11571b9caa10SJiri Olsa 	int w;
11580efe6b67SAdrian Hunter 
11591b9caa10SJiri Olsa 	w = bitmap_weight(format, PERF_PMU_FORMAT_BITS);
11601b9caa10SJiri Olsa 	if (!w)
11611b9caa10SJiri Olsa 		return 0;
11621b9caa10SJiri Olsa 	if (w < 64)
11631b9caa10SJiri Olsa 		return (1ULL << w) - 1;
11641b9caa10SJiri Olsa 	return -1;
11650efe6b67SAdrian Hunter }
11660efe6b67SAdrian Hunter 
1167cd82a32eSJiri Olsa /*
1168688d4dfcSCody P Schafer  * Term is a string term, and might be a param-term. Try to look up it's value
1169688d4dfcSCody P Schafer  * in the remaining terms.
1170688d4dfcSCody P Schafer  * - We have a term like "base-or-format-term=param-term",
1171688d4dfcSCody P Schafer  * - We need to find the value supplied for "param-term" (with param-term named
1172688d4dfcSCody P Schafer  *   in a config string) later on in the term list.
1173688d4dfcSCody P Schafer  */
1174688d4dfcSCody P Schafer static int pmu_resolve_param_term(struct parse_events_term *term,
1175688d4dfcSCody P Schafer 				  struct list_head *head_terms,
1176688d4dfcSCody P Schafer 				  __u64 *value)
1177688d4dfcSCody P Schafer {
1178688d4dfcSCody P Schafer 	struct parse_events_term *t;
1179688d4dfcSCody P Schafer 
1180688d4dfcSCody P Schafer 	list_for_each_entry(t, head_terms, list) {
11812a3d252dSIan Rogers 		if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM &&
11822a3d252dSIan Rogers 		    t->config && !strcmp(t->config, term->config)) {
1183688d4dfcSCody P Schafer 			t->used = true;
1184688d4dfcSCody P Schafer 			*value = t->val.num;
1185688d4dfcSCody P Schafer 			return 0;
1186688d4dfcSCody P Schafer 		}
1187688d4dfcSCody P Schafer 	}
1188688d4dfcSCody P Schafer 
1189bb963e16SNamhyung Kim 	if (verbose > 0)
1190688d4dfcSCody P Schafer 		printf("Required parameter '%s' not specified\n", term->config);
1191688d4dfcSCody P Schafer 
1192688d4dfcSCody P Schafer 	return -1;
1193688d4dfcSCody P Schafer }
1194688d4dfcSCody P Schafer 
1195ffeb883eSHe Kuang static char *pmu_formats_string(struct list_head *formats)
1196e64b020bSJiri Olsa {
1197e64b020bSJiri Olsa 	struct perf_pmu_format *format;
119811db4e29SMasami Hiramatsu 	char *str = NULL;
119911db4e29SMasami Hiramatsu 	struct strbuf buf = STRBUF_INIT;
1200f1417ceaSXin Gao 	unsigned int i = 0;
1201e64b020bSJiri Olsa 
1202ffeb883eSHe Kuang 	if (!formats)
1203e64b020bSJiri Olsa 		return NULL;
1204e64b020bSJiri Olsa 
1205e64b020bSJiri Olsa 	/* sysfs exported terms */
1206ffeb883eSHe Kuang 	list_for_each_entry(format, formats, list)
120711db4e29SMasami Hiramatsu 		if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
120811db4e29SMasami Hiramatsu 			goto error;
1209e64b020bSJiri Olsa 
1210ffeb883eSHe Kuang 	str = strbuf_detach(&buf, NULL);
121111db4e29SMasami Hiramatsu error:
1212ffeb883eSHe Kuang 	strbuf_release(&buf);
1213e64b020bSJiri Olsa 
1214e64b020bSJiri Olsa 	return str;
1215e64b020bSJiri Olsa }
1216e64b020bSJiri Olsa 
1217688d4dfcSCody P Schafer /*
1218cd82a32eSJiri Olsa  * Setup one of config[12] attr members based on the
121988aca8d9SCody P Schafer  * user input data - term parameter.
1220cd82a32eSJiri Olsa  */
12214ac22b48SIan Rogers static int pmu_config_term(const char *pmu_name,
12224ac22b48SIan Rogers 			   struct list_head *formats,
1223cd82a32eSJiri Olsa 			   struct perf_event_attr *attr,
1224dc0a6202SAdrian Hunter 			   struct parse_events_term *term,
1225688d4dfcSCody P Schafer 			   struct list_head *head_terms,
1226e64b020bSJiri Olsa 			   bool zero, struct parse_events_error *err)
1227cd82a32eSJiri Olsa {
12285c6ccc37SArnaldo Carvalho de Melo 	struct perf_pmu_format *format;
1229cd82a32eSJiri Olsa 	__u64 *vp;
12300efe6b67SAdrian Hunter 	__u64 val, max_val;
1231cd82a32eSJiri Olsa 
1232cd82a32eSJiri Olsa 	/*
1233688d4dfcSCody P Schafer 	 * If this is a parameter we've already used for parameterized-eval,
1234688d4dfcSCody P Schafer 	 * skip it in normal eval.
1235688d4dfcSCody P Schafer 	 */
1236688d4dfcSCody P Schafer 	if (term->used)
1237688d4dfcSCody P Schafer 		return 0;
1238688d4dfcSCody P Schafer 
1239688d4dfcSCody P Schafer 	/*
1240cd82a32eSJiri Olsa 	 * Hardcoded terms should be already in, so nothing
1241cd82a32eSJiri Olsa 	 * to be done for them.
1242cd82a32eSJiri Olsa 	 */
1243cd82a32eSJiri Olsa 	if (parse_events__is_hardcoded_term(term))
1244cd82a32eSJiri Olsa 		return 0;
1245cd82a32eSJiri Olsa 
1246cd82a32eSJiri Olsa 	format = pmu_find_format(formats, term->config);
1247688d4dfcSCody P Schafer 	if (!format) {
1248ffeb883eSHe Kuang 		char *pmu_term = pmu_formats_string(formats);
12494ac22b48SIan Rogers 		char *unknown_term;
12504ac22b48SIan Rogers 		char *help_msg;
1251ffeb883eSHe Kuang 
12524ac22b48SIan Rogers 		if (asprintf(&unknown_term,
12534ac22b48SIan Rogers 				"unknown term '%s' for pmu '%s'",
12544ac22b48SIan Rogers 				term->config, pmu_name) < 0)
12554ac22b48SIan Rogers 			unknown_term = NULL;
12564ac22b48SIan Rogers 		help_msg = parse_events_formats_error_string(pmu_term);
12574ac22b48SIan Rogers 		if (err) {
12586c191289SIan Rogers 			parse_events_error__handle(err, term->err_term,
12594ac22b48SIan Rogers 						   unknown_term,
12604ac22b48SIan Rogers 						   help_msg);
12614ac22b48SIan Rogers 		} else {
12624ac22b48SIan Rogers 			pr_debug("%s (%s)\n", unknown_term, help_msg);
12634ac22b48SIan Rogers 			free(unknown_term);
1264e64b020bSJiri Olsa 		}
12654ac22b48SIan Rogers 		free(pmu_term);
1266cd82a32eSJiri Olsa 		return -EINVAL;
1267688d4dfcSCody P Schafer 	}
1268cd82a32eSJiri Olsa 
1269cd82a32eSJiri Olsa 	switch (format->value) {
1270cd82a32eSJiri Olsa 	case PERF_PMU_FORMAT_VALUE_CONFIG:
1271cd82a32eSJiri Olsa 		vp = &attr->config;
1272cd82a32eSJiri Olsa 		break;
1273cd82a32eSJiri Olsa 	case PERF_PMU_FORMAT_VALUE_CONFIG1:
1274cd82a32eSJiri Olsa 		vp = &attr->config1;
1275cd82a32eSJiri Olsa 		break;
1276cd82a32eSJiri Olsa 	case PERF_PMU_FORMAT_VALUE_CONFIG2:
1277cd82a32eSJiri Olsa 		vp = &attr->config2;
1278cd82a32eSJiri Olsa 		break;
1279204e7c49SRob Herring 	case PERF_PMU_FORMAT_VALUE_CONFIG3:
1280204e7c49SRob Herring 		vp = &attr->config3;
1281204e7c49SRob Herring 		break;
1282cd82a32eSJiri Olsa 	default:
1283cd82a32eSJiri Olsa 		return -EINVAL;
1284cd82a32eSJiri Olsa 	}
1285cd82a32eSJiri Olsa 
128616fa7e82SJiri Olsa 	/*
1287688d4dfcSCody P Schafer 	 * Either directly use a numeric term, or try to translate string terms
1288688d4dfcSCody P Schafer 	 * using event parameters.
128916fa7e82SJiri Olsa 	 */
129099e7138eSJiri Olsa 	if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
129199e7138eSJiri Olsa 		if (term->no_value &&
129299e7138eSJiri Olsa 		    bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
129399e7138eSJiri Olsa 			if (err) {
12946c191289SIan Rogers 				parse_events_error__handle(err, term->err_val,
1295448d732cSIan Rogers 					   strdup("no value assigned for term"),
1296448d732cSIan Rogers 					   NULL);
129799e7138eSJiri Olsa 			}
129899e7138eSJiri Olsa 			return -EINVAL;
129999e7138eSJiri Olsa 		}
130099e7138eSJiri Olsa 
1301688d4dfcSCody P Schafer 		val = term->val.num;
130299e7138eSJiri Olsa 	} else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1303688d4dfcSCody P Schafer 		if (strcmp(term->val.str, "?")) {
1304bb963e16SNamhyung Kim 			if (verbose > 0) {
1305688d4dfcSCody P Schafer 				pr_info("Invalid sysfs entry %s=%s\n",
1306688d4dfcSCody P Schafer 						term->config, term->val.str);
1307e64b020bSJiri Olsa 			}
1308e64b020bSJiri Olsa 			if (err) {
13096c191289SIan Rogers 				parse_events_error__handle(err, term->err_val,
1310448d732cSIan Rogers 					strdup("expected numeric value"),
1311448d732cSIan Rogers 					NULL);
1312e64b020bSJiri Olsa 			}
1313688d4dfcSCody P Schafer 			return -EINVAL;
1314688d4dfcSCody P Schafer 		}
1315688d4dfcSCody P Schafer 
1316688d4dfcSCody P Schafer 		if (pmu_resolve_param_term(term, head_terms, &val))
1317688d4dfcSCody P Schafer 			return -EINVAL;
1318688d4dfcSCody P Schafer 	} else
1319688d4dfcSCody P Schafer 		return -EINVAL;
1320688d4dfcSCody P Schafer 
13210efe6b67SAdrian Hunter 	max_val = pmu_format_max_value(format->bits);
13220efe6b67SAdrian Hunter 	if (val > max_val) {
13230efe6b67SAdrian Hunter 		if (err) {
1324448d732cSIan Rogers 			char *err_str;
1325448d732cSIan Rogers 
13266c191289SIan Rogers 			parse_events_error__handle(err, term->err_val,
1327448d732cSIan Rogers 				asprintf(&err_str,
13280efe6b67SAdrian Hunter 				    "value too big for format, maximum is %llu",
1329448d732cSIan Rogers 				    (unsigned long long)max_val) < 0
1330448d732cSIan Rogers 				    ? strdup("value too big for format")
1331448d732cSIan Rogers 				    : err_str,
1332448d732cSIan Rogers 				    NULL);
13330efe6b67SAdrian Hunter 			return -EINVAL;
13340efe6b67SAdrian Hunter 		}
13350efe6b67SAdrian Hunter 		/*
13360efe6b67SAdrian Hunter 		 * Assume we don't care if !err, in which case the value will be
13370efe6b67SAdrian Hunter 		 * silently truncated.
13380efe6b67SAdrian Hunter 		 */
13390efe6b67SAdrian Hunter 	}
13400efe6b67SAdrian Hunter 
1341688d4dfcSCody P Schafer 	pmu_format_value(format->bits, val, vp, zero);
1342cd82a32eSJiri Olsa 	return 0;
1343cd82a32eSJiri Olsa }
1344cd82a32eSJiri Olsa 
13454ac22b48SIan Rogers int perf_pmu__config_terms(const char *pmu_name, struct list_head *formats,
1346cff7f956SJiri Olsa 			   struct perf_event_attr *attr,
1347dc0a6202SAdrian Hunter 			   struct list_head *head_terms,
1348e64b020bSJiri Olsa 			   bool zero, struct parse_events_error *err)
1349cd82a32eSJiri Olsa {
13506cee6cd3SArnaldo Carvalho de Melo 	struct parse_events_term *term;
1351cd82a32eSJiri Olsa 
1352688d4dfcSCody P Schafer 	list_for_each_entry(term, head_terms, list) {
13534ac22b48SIan Rogers 		if (pmu_config_term(pmu_name, formats, attr, term, head_terms,
1354e64b020bSJiri Olsa 				    zero, err))
1355cd82a32eSJiri Olsa 			return -EINVAL;
1356688d4dfcSCody P Schafer 	}
1357cd82a32eSJiri Olsa 
1358cd82a32eSJiri Olsa 	return 0;
1359cd82a32eSJiri Olsa }
1360cd82a32eSJiri Olsa 
1361cd82a32eSJiri Olsa /*
1362cd82a32eSJiri Olsa  * Configures event's 'attr' parameter based on the:
1363cd82a32eSJiri Olsa  * 1) users input - specified in terms parameter
1364cd82a32eSJiri Olsa  * 2) pmu format definitions - specified by pmu parameter
1365cd82a32eSJiri Olsa  */
1366cd82a32eSJiri Olsa int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
1367e64b020bSJiri Olsa 		     struct list_head *head_terms,
1368e64b020bSJiri Olsa 		     struct parse_events_error *err)
1369cd82a32eSJiri Olsa {
1370dc0a6202SAdrian Hunter 	bool zero = !!pmu->default_config;
1371dc0a6202SAdrian Hunter 
1372cd82a32eSJiri Olsa 	attr->type = pmu->type;
13734ac22b48SIan Rogers 	return perf_pmu__config_terms(pmu->name, &pmu->format, attr,
13744ac22b48SIan Rogers 				      head_terms, zero, err);
1375cd82a32eSJiri Olsa }
1376cd82a32eSJiri Olsa 
13775c6ccc37SArnaldo Carvalho de Melo static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
13786cee6cd3SArnaldo Carvalho de Melo 					     struct parse_events_term *term)
1379a6146d50SZheng Yan {
13805c6ccc37SArnaldo Carvalho de Melo 	struct perf_pmu_alias *alias;
1381a6146d50SZheng Yan 	char *name;
1382a6146d50SZheng Yan 
1383a6146d50SZheng Yan 	if (parse_events__is_hardcoded_term(term))
1384a6146d50SZheng Yan 		return NULL;
1385a6146d50SZheng Yan 
1386a6146d50SZheng Yan 	if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1387a6146d50SZheng Yan 		if (term->val.num != 1)
1388a6146d50SZheng Yan 			return NULL;
1389a6146d50SZheng Yan 		if (pmu_find_format(&pmu->format, term->config))
1390a6146d50SZheng Yan 			return NULL;
1391a6146d50SZheng Yan 		name = term->config;
1392a6146d50SZheng Yan 	} else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1393a6146d50SZheng Yan 		if (strcasecmp(term->config, "event"))
1394a6146d50SZheng Yan 			return NULL;
1395a6146d50SZheng Yan 		name = term->val.str;
1396a6146d50SZheng Yan 	} else {
1397a6146d50SZheng Yan 		return NULL;
1398a6146d50SZheng Yan 	}
1399a6146d50SZheng Yan 
1400a6146d50SZheng Yan 	list_for_each_entry(alias, &pmu->aliases, list) {
1401a6146d50SZheng Yan 		if (!strcasecmp(alias->name, name))
1402a6146d50SZheng Yan 			return alias;
1403a6146d50SZheng Yan 	}
1404a6146d50SZheng Yan 	return NULL;
1405a6146d50SZheng Yan }
1406a6146d50SZheng Yan 
1407410136f5SStephane Eranian 
14081d9e446bSJiri Olsa static int check_info_data(struct perf_pmu_alias *alias,
14091d9e446bSJiri Olsa 			   struct perf_pmu_info *info)
1410410136f5SStephane Eranian {
1411410136f5SStephane Eranian 	/*
1412410136f5SStephane Eranian 	 * Only one term in event definition can
14131d9e446bSJiri Olsa 	 * define unit, scale and snapshot, fail
14141d9e446bSJiri Olsa 	 * if there's more than one.
1415410136f5SStephane Eranian 	 */
1416b30a7d1fSArnaldo Carvalho de Melo 	if ((info->unit && alias->unit[0]) ||
14171d9e446bSJiri Olsa 	    (info->scale && alias->scale) ||
14181d9e446bSJiri Olsa 	    (info->snapshot && alias->snapshot))
1419410136f5SStephane Eranian 		return -EINVAL;
1420410136f5SStephane Eranian 
1421b30a7d1fSArnaldo Carvalho de Melo 	if (alias->unit[0])
14221d9e446bSJiri Olsa 		info->unit = alias->unit;
1423410136f5SStephane Eranian 
1424410136f5SStephane Eranian 	if (alias->scale)
14251d9e446bSJiri Olsa 		info->scale = alias->scale;
14261d9e446bSJiri Olsa 
14271d9e446bSJiri Olsa 	if (alias->snapshot)
14281d9e446bSJiri Olsa 		info->snapshot = alias->snapshot;
1429410136f5SStephane Eranian 
1430410136f5SStephane Eranian 	return 0;
1431410136f5SStephane Eranian }
1432410136f5SStephane Eranian 
1433a6146d50SZheng Yan /*
1434a6146d50SZheng Yan  * Find alias in the terms list and replace it with the terms
1435a6146d50SZheng Yan  * defined for the alias
1436a6146d50SZheng Yan  */
1437410136f5SStephane Eranian int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
143846441bdcSMatt Fleming 			  struct perf_pmu_info *info)
1439a6146d50SZheng Yan {
14406cee6cd3SArnaldo Carvalho de Melo 	struct parse_events_term *term, *h;
14415c6ccc37SArnaldo Carvalho de Melo 	struct perf_pmu_alias *alias;
1442a6146d50SZheng Yan 	int ret;
1443a6146d50SZheng Yan 
1444044330c1SMatt Fleming 	info->per_pkg = false;
1445044330c1SMatt Fleming 
14468a398897SStephane Eranian 	/*
14478a398897SStephane Eranian 	 * Mark unit and scale as not set
14488a398897SStephane Eranian 	 * (different from default values, see below)
14498a398897SStephane Eranian 	 */
145046441bdcSMatt Fleming 	info->unit     = NULL;
145146441bdcSMatt Fleming 	info->scale    = 0.0;
14521d9e446bSJiri Olsa 	info->snapshot = false;
1453410136f5SStephane Eranian 
1454a6146d50SZheng Yan 	list_for_each_entry_safe(term, h, head_terms, list) {
1455a6146d50SZheng Yan 		alias = pmu_find_alias(pmu, term);
1456a6146d50SZheng Yan 		if (!alias)
1457a6146d50SZheng Yan 			continue;
1458a6146d50SZheng Yan 		ret = pmu_alias_terms(alias, &term->list);
1459a6146d50SZheng Yan 		if (ret)
1460a6146d50SZheng Yan 			return ret;
1461410136f5SStephane Eranian 
14621d9e446bSJiri Olsa 		ret = check_info_data(alias, info);
1463410136f5SStephane Eranian 		if (ret)
1464410136f5SStephane Eranian 			return ret;
1465410136f5SStephane Eranian 
1466044330c1SMatt Fleming 		if (alias->per_pkg)
1467044330c1SMatt Fleming 			info->per_pkg = true;
1468044330c1SMatt Fleming 
1469e56fbc9dSArnaldo Carvalho de Melo 		list_del_init(&term->list);
14701dc92556SIan Rogers 		parse_events_term__delete(term);
1471a6146d50SZheng Yan 	}
14728a398897SStephane Eranian 
14738a398897SStephane Eranian 	/*
14748a398897SStephane Eranian 	 * if no unit or scale found in aliases, then
14758a398897SStephane Eranian 	 * set defaults as for evsel
14768a398897SStephane Eranian 	 * unit cannot left to NULL
14778a398897SStephane Eranian 	 */
147846441bdcSMatt Fleming 	if (info->unit == NULL)
147946441bdcSMatt Fleming 		info->unit   = "";
14808a398897SStephane Eranian 
148146441bdcSMatt Fleming 	if (info->scale == 0.0)
148246441bdcSMatt Fleming 		info->scale  = 1.0;
14838a398897SStephane Eranian 
1484a6146d50SZheng Yan 	return 0;
1485a6146d50SZheng Yan }
1486a6146d50SZheng Yan 
1487cd82a32eSJiri Olsa int perf_pmu__new_format(struct list_head *list, char *name,
1488cd82a32eSJiri Olsa 			 int config, unsigned long *bits)
1489cd82a32eSJiri Olsa {
14905c6ccc37SArnaldo Carvalho de Melo 	struct perf_pmu_format *format;
1491cd82a32eSJiri Olsa 
1492cd82a32eSJiri Olsa 	format = zalloc(sizeof(*format));
1493cd82a32eSJiri Olsa 	if (!format)
1494cd82a32eSJiri Olsa 		return -ENOMEM;
1495cd82a32eSJiri Olsa 
1496cd82a32eSJiri Olsa 	format->name = strdup(name);
1497cd82a32eSJiri Olsa 	format->value = config;
1498cd82a32eSJiri Olsa 	memcpy(format->bits, bits, sizeof(format->bits));
1499cd82a32eSJiri Olsa 
1500cd82a32eSJiri Olsa 	list_add_tail(&format->list, list);
1501cd82a32eSJiri Olsa 	return 0;
1502cd82a32eSJiri Olsa }
1503cd82a32eSJiri Olsa 
1504cd82a32eSJiri Olsa void perf_pmu__set_format(unsigned long *bits, long from, long to)
1505cd82a32eSJiri Olsa {
1506cd82a32eSJiri Olsa 	long b;
1507cd82a32eSJiri Olsa 
1508cd82a32eSJiri Olsa 	if (!to)
1509cd82a32eSJiri Olsa 		to = from;
1510cd82a32eSJiri Olsa 
151115268138SSukadev Bhattiprolu 	memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
1512cd82a32eSJiri Olsa 	for (b = from; b <= to; b++)
151349bd97c2SSean Christopherson 		__set_bit(b, bits);
1514cd82a32eSJiri Olsa }
1515dc098b35SAndi Kleen 
1516d26383dcSNamhyung Kim void perf_pmu__del_formats(struct list_head *formats)
1517d26383dcSNamhyung Kim {
1518d26383dcSNamhyung Kim 	struct perf_pmu_format *fmt, *tmp;
1519d26383dcSNamhyung Kim 
1520d26383dcSNamhyung Kim 	list_for_each_entry_safe(fmt, tmp, formats, list) {
1521d26383dcSNamhyung Kim 		list_del(&fmt->list);
1522d26383dcSNamhyung Kim 		free(fmt->name);
1523d26383dcSNamhyung Kim 		free(fmt);
1524d26383dcSNamhyung Kim 	}
1525d26383dcSNamhyung Kim }
1526d26383dcSNamhyung Kim 
1527aaea3617SCody P Schafer static int sub_non_neg(int a, int b)
1528aaea3617SCody P Schafer {
1529aaea3617SCody P Schafer 	if (b > a)
1530aaea3617SCody P Schafer 		return 0;
1531aaea3617SCody P Schafer 	return a - b;
1532aaea3617SCody P Schafer }
1533aaea3617SCody P Schafer 
1534eb2d4514SIan Rogers static char *format_alias(char *buf, int len, const struct perf_pmu *pmu,
1535eb2d4514SIan Rogers 			  const struct perf_pmu_alias *alias)
1536dc098b35SAndi Kleen {
1537aaea3617SCody P Schafer 	struct parse_events_term *term;
1538aaea3617SCody P Schafer 	int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
1539aaea3617SCody P Schafer 
1540aaea3617SCody P Schafer 	list_for_each_entry(term, &alias->terms, list) {
1541aaea3617SCody P Schafer 		if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1542aaea3617SCody P Schafer 			used += snprintf(buf + used, sub_non_neg(len, used),
1543aaea3617SCody P Schafer 					",%s=%s", term->config,
1544aaea3617SCody P Schafer 					term->val.str);
1545aaea3617SCody P Schafer 	}
1546aaea3617SCody P Schafer 
1547aaea3617SCody P Schafer 	if (sub_non_neg(len, used) > 0) {
1548aaea3617SCody P Schafer 		buf[used] = '/';
1549aaea3617SCody P Schafer 		used++;
1550aaea3617SCody P Schafer 	}
1551aaea3617SCody P Schafer 	if (sub_non_neg(len, used) > 0) {
1552aaea3617SCody P Schafer 		buf[used] = '\0';
1553aaea3617SCody P Schafer 		used++;
1554aaea3617SCody P Schafer 	} else
1555aaea3617SCody P Schafer 		buf[len - 1] = '\0';
1556aaea3617SCody P Schafer 
1557dc098b35SAndi Kleen 	return buf;
1558dc098b35SAndi Kleen }
1559dc098b35SAndi Kleen 
1560eb2d4514SIan Rogers /** Struct for ordering events as output in perf list. */
1561dd5f1036SAndi Kleen struct sevent {
1562eb2d4514SIan Rogers 	/** PMU for event. */
1563eb2d4514SIan Rogers 	const struct perf_pmu *pmu;
1564eb2d4514SIan Rogers 	/**
1565eb2d4514SIan Rogers 	 * Optional event for name, desc, etc. If not present then this is a
1566eb2d4514SIan Rogers 	 * selectable PMU and the event name is shown as "//".
1567eb2d4514SIan Rogers 	 */
1568eb2d4514SIan Rogers 	const struct perf_pmu_alias *event;
1569eb2d4514SIan Rogers 	/** Is the PMU for the CPU? */
1570eb2d4514SIan Rogers 	bool is_cpu;
157108e60ed1SAndi Kleen };
157208e60ed1SAndi Kleen 
1573dd5f1036SAndi Kleen static int cmp_sevent(const void *a, const void *b)
1574dc098b35SAndi Kleen {
1575dd5f1036SAndi Kleen 	const struct sevent *as = a;
1576dd5f1036SAndi Kleen 	const struct sevent *bs = b;
1577*3f980eabSIan Rogers 	const char *a_pmu_name = NULL, *b_pmu_name = NULL;
1578eb2d4514SIan Rogers 	const char *a_name = "//", *a_desc = NULL, *a_topic = "";
1579eb2d4514SIan Rogers 	const char *b_name = "//", *b_desc = NULL, *b_topic = "";
15800e0ae874SJin Yao 	int ret;
158108e60ed1SAndi Kleen 
1582eb2d4514SIan Rogers 	if (as->event) {
1583eb2d4514SIan Rogers 		a_name = as->event->name;
1584eb2d4514SIan Rogers 		a_desc = as->event->desc;
1585eb2d4514SIan Rogers 		a_topic = as->event->topic ?: "";
1586*3f980eabSIan Rogers 		a_pmu_name = as->event->pmu_name;
1587dd5f1036SAndi Kleen 	}
1588eb2d4514SIan Rogers 	if (bs->event) {
1589eb2d4514SIan Rogers 		b_name = bs->event->name;
1590eb2d4514SIan Rogers 		b_desc = bs->event->desc;
1591eb2d4514SIan Rogers 		b_topic = bs->event->topic ?: "";
1592*3f980eabSIan Rogers 		b_pmu_name = bs->event->pmu_name;
1593eb2d4514SIan Rogers 	}
1594eb2d4514SIan Rogers 	/* Put extra events last. */
1595eb2d4514SIan Rogers 	if (!!a_desc != !!b_desc)
1596eb2d4514SIan Rogers 		return !!a_desc - !!b_desc;
1597eb2d4514SIan Rogers 
1598eb2d4514SIan Rogers 	/* Order by topics. */
1599eb2d4514SIan Rogers 	ret = strcmp(a_topic, b_topic);
1600eb2d4514SIan Rogers 	if (ret)
1601eb2d4514SIan Rogers 		return ret;
1602ce0dc7d2SJohn Garry 
1603ce0dc7d2SJohn Garry 	/* Order CPU core events to be first */
1604ce0dc7d2SJohn Garry 	if (as->is_cpu != bs->is_cpu)
1605e5c6109fSIan Rogers 		return as->is_cpu ? -1 : 1;
1606ce0dc7d2SJohn Garry 
1607eb2d4514SIan Rogers 	/* Order by PMU name. */
1608*3f980eabSIan Rogers 	if (as->pmu != bs->pmu) {
1609*3f980eabSIan Rogers 		a_pmu_name = a_pmu_name ?: (as->pmu->name ?: "");
1610*3f980eabSIan Rogers 		b_pmu_name = b_pmu_name ?: (bs->pmu->name ?: "");
1611eb2d4514SIan Rogers 		ret = strcmp(a_pmu_name, b_pmu_name);
1612eb2d4514SIan Rogers 		if (ret)
16130e0ae874SJin Yao 			return ret;
1614*3f980eabSIan Rogers 	}
1615eb2d4514SIan Rogers 
1616eb2d4514SIan Rogers 	/* Order by event name. */
1617eb2d4514SIan Rogers 	return strcmp(a_name, b_name);
161808e60ed1SAndi Kleen }
161908e60ed1SAndi Kleen 
1620d504fae9SJohn Garry bool is_pmu_core(const char *name)
1621d504fae9SJohn Garry {
1622d504fae9SJohn Garry 	return !strcmp(name, "cpu") || is_arm_pmu_core(name);
1623d504fae9SJohn Garry }
1624d504fae9SJohn Garry 
1625e0257a01SJohn Garry static bool pmu_alias_is_duplicate(struct sevent *alias_a,
1626e0257a01SJohn Garry 				   struct sevent *alias_b)
1627e0257a01SJohn Garry {
1628*3f980eabSIan Rogers 	const char *a_pmu_name = NULL, *b_pmu_name = NULL;
1629*3f980eabSIan Rogers 	const char *a_name = "//", *b_name = "//";
1630*3f980eabSIan Rogers 
1631*3f980eabSIan Rogers 
1632*3f980eabSIan Rogers 	if (alias_a->event) {
1633*3f980eabSIan Rogers 		a_name = alias_a->event->name;
1634*3f980eabSIan Rogers 		a_pmu_name = alias_a->event->pmu_name;
1635*3f980eabSIan Rogers 	}
1636*3f980eabSIan Rogers 	if (alias_b->event) {
1637*3f980eabSIan Rogers 		b_name = alias_b->event->name;
1638*3f980eabSIan Rogers 		b_pmu_name = alias_b->event->pmu_name;
1639*3f980eabSIan Rogers 	}
1640eb2d4514SIan Rogers 
1641e0257a01SJohn Garry 	/* Different names -> never duplicates */
1642eb2d4514SIan Rogers 	if (strcmp(a_name, b_name))
1643e0257a01SJohn Garry 		return false;
1644e0257a01SJohn Garry 
1645eb2d4514SIan Rogers 	/* Don't remove duplicates for different PMUs */
1646*3f980eabSIan Rogers 	a_pmu_name = a_pmu_name ?: (alias_a->pmu->name ?: "");
1647*3f980eabSIan Rogers 	b_pmu_name = b_pmu_name ?: (alias_b->pmu->name ?: "");
1648eb2d4514SIan Rogers 	return strcmp(a_pmu_name, b_pmu_name) == 0;
1649e0257a01SJohn Garry }
1650e0257a01SJohn Garry 
1651e5c6109fSIan Rogers void print_pmu_events(const struct print_callbacks *print_cb, void *print_state)
1652dc098b35SAndi Kleen {
1653dc098b35SAndi Kleen 	struct perf_pmu *pmu;
1654e5c6109fSIan Rogers 	struct perf_pmu_alias *event;
1655dc098b35SAndi Kleen 	char buf[1024];
1656dc098b35SAndi Kleen 	int printed = 0;
1657dc098b35SAndi Kleen 	int len, j;
1658dd5f1036SAndi Kleen 	struct sevent *aliases;
1659dc098b35SAndi Kleen 
1660dc098b35SAndi Kleen 	pmu = NULL;
1661dc098b35SAndi Kleen 	len = 0;
166242634bc7SAdrian Hunter 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1663e5c6109fSIan Rogers 		list_for_each_entry(event, &pmu->aliases, list)
1664dc098b35SAndi Kleen 			len++;
166542634bc7SAdrian Hunter 		if (pmu->selectable)
166642634bc7SAdrian Hunter 			len++;
166742634bc7SAdrian Hunter 	}
1668dd5f1036SAndi Kleen 	aliases = zalloc(sizeof(struct sevent) * len);
1669eb2d4514SIan Rogers 	if (!aliases) {
1670eb2d4514SIan Rogers 		pr_err("FATAL: not enough memory to print PMU events\n");
1671eb2d4514SIan Rogers 		return;
1672eb2d4514SIan Rogers 	}
1673dc098b35SAndi Kleen 	pmu = NULL;
1674dc098b35SAndi Kleen 	j = 0;
167542634bc7SAdrian Hunter 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1676e5c6109fSIan Rogers 		bool is_cpu = is_pmu_core(pmu->name) || perf_pmu__is_hybrid(pmu->name);
1677eb2d4514SIan Rogers 
1678e5c6109fSIan Rogers 		list_for_each_entry(event, &pmu->aliases, list) {
1679e5c6109fSIan Rogers 			aliases[j].event = event;
1680eb2d4514SIan Rogers 			aliases[j].pmu = pmu;
1681ce0dc7d2SJohn Garry 			aliases[j].is_cpu = is_cpu;
1682dc098b35SAndi Kleen 			j++;
1683dc098b35SAndi Kleen 		}
1684e5c6109fSIan Rogers 		if (pmu->selectable) {
1685eb2d4514SIan Rogers 			aliases[j].event = NULL;
1686eb2d4514SIan Rogers 			aliases[j].pmu = pmu;
1687eb2d4514SIan Rogers 			aliases[j].is_cpu = is_cpu;
168842634bc7SAdrian Hunter 			j++;
168942634bc7SAdrian Hunter 		}
169042634bc7SAdrian Hunter 	}
1691dc098b35SAndi Kleen 	len = j;
1692dd5f1036SAndi Kleen 	qsort(aliases, len, sizeof(struct sevent), cmp_sevent);
1693dc098b35SAndi Kleen 	for (j = 0; j < len; j++) {
1694e5c6109fSIan Rogers 		const char *name, *alias = NULL, *scale_unit = NULL,
1695e5c6109fSIan Rogers 			*desc = NULL, *long_desc = NULL,
1696*3f980eabSIan Rogers 			*encoding_desc = NULL, *topic = NULL,
1697*3f980eabSIan Rogers 			*pmu_name = NULL;
1698e5c6109fSIan Rogers 		bool deprecated = false;
1699e5c6109fSIan Rogers 		size_t buf_used;
1700eb2d4514SIan Rogers 
170115b22ed3SAndi Kleen 		/* Skip duplicates */
1702e0257a01SJohn Garry 		if (j > 0 && pmu_alias_is_duplicate(&aliases[j], &aliases[j - 1]))
170315b22ed3SAndi Kleen 			continue;
17040e0ae874SJin Yao 
1705eb2d4514SIan Rogers 		if (!aliases[j].event) {
1706eb2d4514SIan Rogers 			/* A selectable event. */
1707*3f980eabSIan Rogers 			pmu_name = aliases[j].pmu->name;
1708*3f980eabSIan Rogers 			buf_used = snprintf(buf, sizeof(buf), "%s//", pmu_name) + 1;
1709eb2d4514SIan Rogers 			name = buf;
1710eb2d4514SIan Rogers 		} else {
1711e5c6109fSIan Rogers 			if (aliases[j].event->desc) {
1712e5c6109fSIan Rogers 				name = aliases[j].event->name;
1713e5c6109fSIan Rogers 				buf_used = 0;
1714eb2d4514SIan Rogers 			} else {
1715eb2d4514SIan Rogers 				name = format_alias(buf, sizeof(buf), aliases[j].pmu,
1716eb2d4514SIan Rogers 						    aliases[j].event);
1717e5c6109fSIan Rogers 				if (aliases[j].is_cpu) {
1718e5c6109fSIan Rogers 					alias = name;
1719e5c6109fSIan Rogers 					name = aliases[j].event->name;
1720eb2d4514SIan Rogers 				}
1721e5c6109fSIan Rogers 				buf_used = strlen(buf) + 1;
1722eb2d4514SIan Rogers 			}
1723*3f980eabSIan Rogers 			pmu_name = aliases[j].event->pmu_name ?: (aliases[j].pmu->name ?: "");
1724e5c6109fSIan Rogers 			if (strlen(aliases[j].event->unit) || aliases[j].event->scale != 1.0) {
1725e5c6109fSIan Rogers 				scale_unit = buf + buf_used;
1726e5c6109fSIan Rogers 				buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used,
1727e5c6109fSIan Rogers 						"%G%s", aliases[j].event->scale,
1728e5c6109fSIan Rogers 						aliases[j].event->unit) + 1;
1729dc098b35SAndi Kleen 			}
1730e5c6109fSIan Rogers 			desc = aliases[j].event->desc;
1731e5c6109fSIan Rogers 			long_desc = aliases[j].event->long_desc;
1732eb2d4514SIan Rogers 			topic = aliases[j].event->topic;
1733e5c6109fSIan Rogers 			encoding_desc = buf + buf_used;
1734e5c6109fSIan Rogers 			buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used,
1735*3f980eabSIan Rogers 					"%s/%s/", pmu_name, aliases[j].event->str) + 1;
1736e5c6109fSIan Rogers 			deprecated = aliases[j].event->deprecated;
1737dd5f1036SAndi Kleen 		}
1738e5c6109fSIan Rogers 		print_cb->print_event(print_state,
1739*3f980eabSIan Rogers 				pmu_name,
1740e5c6109fSIan Rogers 				topic,
1741e5c6109fSIan Rogers 				name,
1742e5c6109fSIan Rogers 				alias,
1743e5c6109fSIan Rogers 				scale_unit,
1744e5c6109fSIan Rogers 				deprecated,
1745e5c6109fSIan Rogers 				"Kernel PMU event",
1746e5c6109fSIan Rogers 				desc,
1747e5c6109fSIan Rogers 				long_desc,
1748d9dc8874SIan Rogers 				encoding_desc);
1749dc098b35SAndi Kleen 	}
1750dfc431cbSArnaldo Carvalho de Melo 	if (printed && pager_in_use())
1751dc098b35SAndi Kleen 		printf("\n");
1752eb2d4514SIan Rogers 
17537e4772dcSArnaldo Carvalho de Melo 	zfree(&aliases);
17547e4772dcSArnaldo Carvalho de Melo 	return;
1755dc098b35SAndi Kleen }
17564cabc3d1SAndi Kleen 
17574cabc3d1SAndi Kleen bool pmu_have_event(const char *pname, const char *name)
17584cabc3d1SAndi Kleen {
17594cabc3d1SAndi Kleen 	struct perf_pmu *pmu;
17604cabc3d1SAndi Kleen 	struct perf_pmu_alias *alias;
17614cabc3d1SAndi Kleen 
17624cabc3d1SAndi Kleen 	pmu = NULL;
17634cabc3d1SAndi Kleen 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
17644cabc3d1SAndi Kleen 		if (strcmp(pname, pmu->name))
17654cabc3d1SAndi Kleen 			continue;
17664cabc3d1SAndi Kleen 		list_for_each_entry(alias, &pmu->aliases, list)
17674cabc3d1SAndi Kleen 			if (!strcmp(alias->name, name))
17684cabc3d1SAndi Kleen 				return true;
17694cabc3d1SAndi Kleen 	}
17704cabc3d1SAndi Kleen 	return false;
17714cabc3d1SAndi Kleen }
17727d4bdab5SAdrian Hunter 
1773d50a79cdSJames Clark FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
17747d4bdab5SAdrian Hunter {
17757d4bdab5SAdrian Hunter 	char path[PATH_MAX];
17767d4bdab5SAdrian Hunter 
1777f8ad6018SJames Clark 	if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name) ||
1778f8ad6018SJames Clark 	    !file_available(path))
17797d4bdab5SAdrian Hunter 		return NULL;
17807d4bdab5SAdrian Hunter 
17817d4bdab5SAdrian Hunter 	return fopen(path, "r");
17827d4bdab5SAdrian Hunter }
17837d4bdab5SAdrian Hunter 
17843a69672eSNamhyung Kim FILE *perf_pmu__open_file_at(struct perf_pmu *pmu, int dirfd, const char *name)
17853a69672eSNamhyung Kim {
17863a69672eSNamhyung Kim 	int fd;
17873a69672eSNamhyung Kim 
17883a69672eSNamhyung Kim 	fd = perf_pmu__pathname_fd(dirfd, pmu->name, name, O_RDONLY);
17893a69672eSNamhyung Kim 	if (fd < 0)
17903a69672eSNamhyung Kim 		return NULL;
17913a69672eSNamhyung Kim 
17923a69672eSNamhyung Kim 	return fdopen(fd, "r");
17933a69672eSNamhyung Kim }
17943a69672eSNamhyung Kim 
17957d4bdab5SAdrian Hunter int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
17967d4bdab5SAdrian Hunter 			...)
17977d4bdab5SAdrian Hunter {
17987d4bdab5SAdrian Hunter 	va_list args;
17997d4bdab5SAdrian Hunter 	FILE *file;
18007d4bdab5SAdrian Hunter 	int ret = EOF;
18017d4bdab5SAdrian Hunter 
18027d4bdab5SAdrian Hunter 	va_start(args, fmt);
18037d4bdab5SAdrian Hunter 	file = perf_pmu__open_file(pmu, name);
18047d4bdab5SAdrian Hunter 	if (file) {
18057d4bdab5SAdrian Hunter 		ret = vfscanf(file, fmt, args);
18067d4bdab5SAdrian Hunter 		fclose(file);
18077d4bdab5SAdrian Hunter 	}
18087d4bdab5SAdrian Hunter 	va_end(args);
18097d4bdab5SAdrian Hunter 	return ret;
18107d4bdab5SAdrian Hunter }
18119fbc61f8SKan Liang 
18123a69672eSNamhyung Kim int perf_pmu__scan_file_at(struct perf_pmu *pmu, int dirfd, const char *name,
18133a69672eSNamhyung Kim 			   const char *fmt, ...)
18143a69672eSNamhyung Kim {
18153a69672eSNamhyung Kim 	va_list args;
18163a69672eSNamhyung Kim 	FILE *file;
18173a69672eSNamhyung Kim 	int ret = EOF;
18183a69672eSNamhyung Kim 
18193a69672eSNamhyung Kim 	va_start(args, fmt);
18203a69672eSNamhyung Kim 	file = perf_pmu__open_file_at(pmu, dirfd, name);
18213a69672eSNamhyung Kim 	if (file) {
18223a69672eSNamhyung Kim 		ret = vfscanf(file, fmt, args);
18233a69672eSNamhyung Kim 		fclose(file);
18243a69672eSNamhyung Kim 	}
18253a69672eSNamhyung Kim 	va_end(args);
18263a69672eSNamhyung Kim 	return ret;
18273a69672eSNamhyung Kim }
18283a69672eSNamhyung Kim 
1829c2b6a896SGerman Gomez bool perf_pmu__file_exists(struct perf_pmu *pmu, const char *name)
1830c2b6a896SGerman Gomez {
1831c2b6a896SGerman Gomez 	char path[PATH_MAX];
1832c2b6a896SGerman Gomez 
1833c2b6a896SGerman Gomez 	if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name))
1834c2b6a896SGerman Gomez 		return false;
1835c2b6a896SGerman Gomez 
1836c2b6a896SGerman Gomez 	return file_available(path);
1837c2b6a896SGerman Gomez }
1838c2b6a896SGerman Gomez 
18399fbc61f8SKan Liang static int perf_pmu__new_caps(struct list_head *list, char *name, char *value)
18409fbc61f8SKan Liang {
18419fbc61f8SKan Liang 	struct perf_pmu_caps *caps = zalloc(sizeof(*caps));
18429fbc61f8SKan Liang 
18439fbc61f8SKan Liang 	if (!caps)
18449fbc61f8SKan Liang 		return -ENOMEM;
18459fbc61f8SKan Liang 
18469fbc61f8SKan Liang 	caps->name = strdup(name);
18479fbc61f8SKan Liang 	if (!caps->name)
18489fbc61f8SKan Liang 		goto free_caps;
18499fbc61f8SKan Liang 	caps->value = strndup(value, strlen(value) - 1);
18509fbc61f8SKan Liang 	if (!caps->value)
18519fbc61f8SKan Liang 		goto free_name;
18529fbc61f8SKan Liang 	list_add_tail(&caps->list, list);
18539fbc61f8SKan Liang 	return 0;
18549fbc61f8SKan Liang 
18559fbc61f8SKan Liang free_name:
18569fbc61f8SKan Liang 	zfree(caps->name);
18579fbc61f8SKan Liang free_caps:
18589fbc61f8SKan Liang 	free(caps);
18599fbc61f8SKan Liang 
18609fbc61f8SKan Liang 	return -ENOMEM;
18619fbc61f8SKan Liang }
18629fbc61f8SKan Liang 
1863eec11310SNamhyung Kim static void perf_pmu__del_caps(struct perf_pmu *pmu)
1864eec11310SNamhyung Kim {
1865eec11310SNamhyung Kim 	struct perf_pmu_caps *caps, *tmp;
1866eec11310SNamhyung Kim 
1867eec11310SNamhyung Kim 	list_for_each_entry_safe(caps, tmp, &pmu->caps, list) {
1868eec11310SNamhyung Kim 		list_del(&caps->list);
1869eec11310SNamhyung Kim 		free(caps->name);
1870eec11310SNamhyung Kim 		free(caps->value);
1871eec11310SNamhyung Kim 		free(caps);
1872eec11310SNamhyung Kim 	}
1873eec11310SNamhyung Kim }
1874eec11310SNamhyung Kim 
18759fbc61f8SKan Liang /*
18769fbc61f8SKan Liang  * Reading/parsing the given pmu capabilities, which should be located at:
18779fbc61f8SKan Liang  * /sys/bus/event_source/devices/<dev>/caps as sysfs group attributes.
18789fbc61f8SKan Liang  * Return the number of capabilities
18799fbc61f8SKan Liang  */
18809fbc61f8SKan Liang int perf_pmu__caps_parse(struct perf_pmu *pmu)
18819fbc61f8SKan Liang {
18829fbc61f8SKan Liang 	struct stat st;
18839fbc61f8SKan Liang 	char caps_path[PATH_MAX];
18849fbc61f8SKan Liang 	DIR *caps_dir;
18859fbc61f8SKan Liang 	struct dirent *evt_ent;
1886b39094d3SNamhyung Kim 	int caps_fd;
18873339ec44SRavi Bangoria 
18883339ec44SRavi Bangoria 	if (pmu->caps_initialized)
18893339ec44SRavi Bangoria 		return pmu->nr_caps;
18903339ec44SRavi Bangoria 
18913339ec44SRavi Bangoria 	pmu->nr_caps = 0;
18929fbc61f8SKan Liang 
1893f8ad6018SJames Clark 	if (!perf_pmu__pathname_scnprintf(caps_path, sizeof(caps_path), pmu->name, "caps"))
18949fbc61f8SKan Liang 		return -1;
18959fbc61f8SKan Liang 
18963339ec44SRavi Bangoria 	if (stat(caps_path, &st) < 0) {
18973339ec44SRavi Bangoria 		pmu->caps_initialized = true;
18989fbc61f8SKan Liang 		return 0;	/* no error if caps does not exist */
18993339ec44SRavi Bangoria 	}
19009fbc61f8SKan Liang 
19019fbc61f8SKan Liang 	caps_dir = opendir(caps_path);
19029fbc61f8SKan Liang 	if (!caps_dir)
19039fbc61f8SKan Liang 		return -EINVAL;
19049fbc61f8SKan Liang 
1905b39094d3SNamhyung Kim 	caps_fd = dirfd(caps_dir);
1906b39094d3SNamhyung Kim 
19079fbc61f8SKan Liang 	while ((evt_ent = readdir(caps_dir)) != NULL) {
19089fbc61f8SKan Liang 		char *name = evt_ent->d_name;
19099fbc61f8SKan Liang 		char value[128];
19109fbc61f8SKan Liang 		FILE *file;
1911b39094d3SNamhyung Kim 		int fd;
19129fbc61f8SKan Liang 
19139fbc61f8SKan Liang 		if (!strcmp(name, ".") || !strcmp(name, ".."))
19149fbc61f8SKan Liang 			continue;
19159fbc61f8SKan Liang 
1916b39094d3SNamhyung Kim 		fd = openat(caps_fd, name, O_RDONLY);
19170ea8920eSIan Rogers 		if (fd == -1)
19189fbc61f8SKan Liang 			continue;
19190ea8920eSIan Rogers 		file = fdopen(fd, "r");
19200ea8920eSIan Rogers 		if (!file) {
19210ea8920eSIan Rogers 			close(fd);
19220ea8920eSIan Rogers 			continue;
19230ea8920eSIan Rogers 		}
19249fbc61f8SKan Liang 
19259fbc61f8SKan Liang 		if (!fgets(value, sizeof(value), file) ||
19269fbc61f8SKan Liang 		    (perf_pmu__new_caps(&pmu->caps, name, value) < 0)) {
19279fbc61f8SKan Liang 			fclose(file);
19289fbc61f8SKan Liang 			continue;
19299fbc61f8SKan Liang 		}
19309fbc61f8SKan Liang 
19313339ec44SRavi Bangoria 		pmu->nr_caps++;
19329fbc61f8SKan Liang 		fclose(file);
19339fbc61f8SKan Liang 	}
19349fbc61f8SKan Liang 
19359fbc61f8SKan Liang 	closedir(caps_dir);
19369fbc61f8SKan Liang 
19373339ec44SRavi Bangoria 	pmu->caps_initialized = true;
19383339ec44SRavi Bangoria 	return pmu->nr_caps;
19399fbc61f8SKan Liang }
1940e4064776SJin Yao 
1941e4064776SJin Yao void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config,
19428e8bbfb3SIan Rogers 				   const char *name)
1943e4064776SJin Yao {
1944e4064776SJin Yao 	struct perf_pmu_format *format;
1945e4064776SJin Yao 	__u64 masks = 0, bits;
1946e4064776SJin Yao 	char buf[100];
1947e4064776SJin Yao 	unsigned int i;
1948e4064776SJin Yao 
1949e4064776SJin Yao 	list_for_each_entry(format, &pmu->format, list)	{
1950e4064776SJin Yao 		if (format->value != PERF_PMU_FORMAT_VALUE_CONFIG)
1951e4064776SJin Yao 			continue;
1952e4064776SJin Yao 
1953e4064776SJin Yao 		for_each_set_bit(i, format->bits, PERF_PMU_FORMAT_BITS)
1954e4064776SJin Yao 			masks |= 1ULL << i;
1955e4064776SJin Yao 	}
1956e4064776SJin Yao 
1957e4064776SJin Yao 	/*
1958e4064776SJin Yao 	 * Kernel doesn't export any valid format bits.
1959e4064776SJin Yao 	 */
1960e4064776SJin Yao 	if (masks == 0)
1961e4064776SJin Yao 		return;
1962e4064776SJin Yao 
1963e4064776SJin Yao 	bits = config & ~masks;
1964e4064776SJin Yao 	if (bits == 0)
1965e4064776SJin Yao 		return;
1966e4064776SJin Yao 
1967e4064776SJin Yao 	bitmap_scnprintf((unsigned long *)&bits, sizeof(bits) * 8, buf, sizeof(buf));
1968e4064776SJin Yao 
1969e4064776SJin Yao 	pr_warning("WARNING: event '%s' not valid (bits %s of config "
1970e4064776SJin Yao 		   "'%llx' not supported by kernel)!\n",
1971e4064776SJin Yao 		   name ?: "N/A", buf, config);
1972e4064776SJin Yao }
1973c5a26ea4SJin Yao 
1974c5a26ea4SJin Yao bool perf_pmu__has_hybrid(void)
1975c5a26ea4SJin Yao {
1976c5a26ea4SJin Yao 	if (!hybrid_scanned) {
1977c5a26ea4SJin Yao 		hybrid_scanned = true;
1978c5a26ea4SJin Yao 		perf_pmu__scan(NULL);
1979c5a26ea4SJin Yao 	}
1980c5a26ea4SJin Yao 
1981c5a26ea4SJin Yao 	return !list_empty(&perf_pmu__hybrid_pmus);
1982c5a26ea4SJin Yao }
1983c47a5599SJin Yao 
1984c47a5599SJin Yao int perf_pmu__match(char *pattern, char *name, char *tok)
1985c47a5599SJin Yao {
198613d60ba0SKan Liang 	if (!name)
198713d60ba0SKan Liang 		return -1;
198813d60ba0SKan Liang 
1989c47a5599SJin Yao 	if (fnmatch(pattern, name, 0))
1990c47a5599SJin Yao 		return -1;
1991c47a5599SJin Yao 
1992240e6fd0SIan Rogers 	if (tok && !perf_pmu__match_ignoring_suffix(name, tok))
1993c47a5599SJin Yao 		return -1;
1994c47a5599SJin Yao 
1995c47a5599SJin Yao 	return 0;
1996c47a5599SJin Yao }
19971d3351e6SJin Yao 
19981d3351e6SJin Yao int perf_pmu__cpus_match(struct perf_pmu *pmu, struct perf_cpu_map *cpus,
19991d3351e6SJin Yao 			 struct perf_cpu_map **mcpus_ptr,
20001d3351e6SJin Yao 			 struct perf_cpu_map **ucpus_ptr)
20011d3351e6SJin Yao {
20021d3351e6SJin Yao 	struct perf_cpu_map *pmu_cpus = pmu->cpus;
20031d3351e6SJin Yao 	struct perf_cpu_map *matched_cpus, *unmatched_cpus;
20046a12a63eSIan Rogers 	struct perf_cpu cpu;
20056a12a63eSIan Rogers 	int i, matched_nr = 0, unmatched_nr = 0;
20061d3351e6SJin Yao 
20071d3351e6SJin Yao 	matched_cpus = perf_cpu_map__default_new();
20081d3351e6SJin Yao 	if (!matched_cpus)
20091d3351e6SJin Yao 		return -1;
20101d3351e6SJin Yao 
20111d3351e6SJin Yao 	unmatched_cpus = perf_cpu_map__default_new();
20121d3351e6SJin Yao 	if (!unmatched_cpus) {
20131d3351e6SJin Yao 		perf_cpu_map__put(matched_cpus);
20141d3351e6SJin Yao 		return -1;
20151d3351e6SJin Yao 	}
20161d3351e6SJin Yao 
20176a12a63eSIan Rogers 	perf_cpu_map__for_each_cpu(cpu, i, cpus) {
20186a12a63eSIan Rogers 		if (!perf_cpu_map__has(pmu_cpus, cpu))
20196a12a63eSIan Rogers 			unmatched_cpus->map[unmatched_nr++] = cpu;
20201d3351e6SJin Yao 		else
20216a12a63eSIan Rogers 			matched_cpus->map[matched_nr++] = cpu;
20221d3351e6SJin Yao 	}
20231d3351e6SJin Yao 
20241d3351e6SJin Yao 	unmatched_cpus->nr = unmatched_nr;
20251d3351e6SJin Yao 	matched_cpus->nr = matched_nr;
20261d3351e6SJin Yao 	*mcpus_ptr = matched_cpus;
20271d3351e6SJin Yao 	*ucpus_ptr = unmatched_cpus;
20281d3351e6SJin Yao 	return 0;
20291d3351e6SJin Yao }
2030acef233bSJing Zhang 
2031acef233bSJing Zhang double __weak perf_pmu__cpu_slots_per_cycle(void)
2032acef233bSJing Zhang {
2033acef233bSJing Zhang 	return NAN;
2034acef233bSJing Zhang }
2035f8ad6018SJames Clark 
2036f8ad6018SJames Clark int perf_pmu__event_source_devices_scnprintf(char *pathname, size_t size)
2037f8ad6018SJames Clark {
2038f8ad6018SJames Clark 	const char *sysfs = sysfs__mountpoint();
2039f8ad6018SJames Clark 
2040f8ad6018SJames Clark 	if (!sysfs)
2041f8ad6018SJames Clark 		return 0;
2042f8ad6018SJames Clark 	return scnprintf(pathname, size, "%s/bus/event_source/devices/", sysfs);
2043f8ad6018SJames Clark }
2044f8ad6018SJames Clark 
2045e293a5e8SNamhyung Kim int perf_pmu__event_source_devices_fd(void)
2046e293a5e8SNamhyung Kim {
2047e293a5e8SNamhyung Kim 	char path[PATH_MAX];
2048e293a5e8SNamhyung Kim 	const char *sysfs = sysfs__mountpoint();
2049e293a5e8SNamhyung Kim 
2050e293a5e8SNamhyung Kim 	if (!sysfs)
2051e293a5e8SNamhyung Kim 		return -1;
2052e293a5e8SNamhyung Kim 
2053e293a5e8SNamhyung Kim 	scnprintf(path, sizeof(path), "%s/bus/event_source/devices/", sysfs);
2054e293a5e8SNamhyung Kim 	return open(path, O_DIRECTORY);
2055e293a5e8SNamhyung Kim }
2056e293a5e8SNamhyung Kim 
2057f8ad6018SJames Clark /*
2058f8ad6018SJames Clark  * Fill 'buf' with the path to a file or folder in 'pmu_name' in
2059f8ad6018SJames Clark  * sysfs. For example if pmu_name = "cs_etm" and 'filename' = "format"
2060f8ad6018SJames Clark  * then pathname will be filled with
2061f8ad6018SJames Clark  * "/sys/bus/event_source/devices/cs_etm/format"
2062f8ad6018SJames Clark  *
2063f8ad6018SJames Clark  * Return 0 if the sysfs mountpoint couldn't be found or if no
2064f8ad6018SJames Clark  * characters were written.
2065f8ad6018SJames Clark  */
2066f8ad6018SJames Clark int perf_pmu__pathname_scnprintf(char *buf, size_t size,
2067f8ad6018SJames Clark 				 const char *pmu_name, const char *filename)
2068f8ad6018SJames Clark {
2069f8ad6018SJames Clark 	char base_path[PATH_MAX];
2070f8ad6018SJames Clark 
2071f8ad6018SJames Clark 	if (!perf_pmu__event_source_devices_scnprintf(base_path, sizeof(base_path)))
2072f8ad6018SJames Clark 		return 0;
2073f8ad6018SJames Clark 	return scnprintf(buf, size, "%s%s/%s", base_path, pmu_name, filename);
2074f8ad6018SJames Clark }
2075eec11310SNamhyung Kim 
2076e293a5e8SNamhyung Kim int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *filename, int flags)
2077e293a5e8SNamhyung Kim {
2078e293a5e8SNamhyung Kim 	char path[PATH_MAX];
2079e293a5e8SNamhyung Kim 
2080e293a5e8SNamhyung Kim 	scnprintf(path, sizeof(path), "%s/%s", pmu_name, filename);
2081e293a5e8SNamhyung Kim 	return openat(dirfd, path, flags);
2082e293a5e8SNamhyung Kim }
2083e293a5e8SNamhyung Kim 
2084eec11310SNamhyung Kim static void perf_pmu__delete(struct perf_pmu *pmu)
2085eec11310SNamhyung Kim {
2086eec11310SNamhyung Kim 	perf_pmu__del_formats(&pmu->format);
2087eec11310SNamhyung Kim 	perf_pmu__del_aliases(pmu);
2088eec11310SNamhyung Kim 	perf_pmu__del_caps(pmu);
2089eec11310SNamhyung Kim 
2090eec11310SNamhyung Kim 	perf_cpu_map__put(pmu->cpus);
2091eec11310SNamhyung Kim 
2092eec11310SNamhyung Kim 	free(pmu->default_config);
2093eec11310SNamhyung Kim 	free(pmu->name);
2094eec11310SNamhyung Kim 	free(pmu->alias_name);
2095eec11310SNamhyung Kim 	free(pmu);
2096eec11310SNamhyung Kim }
2097eec11310SNamhyung Kim 
2098eec11310SNamhyung Kim void perf_pmu__destroy(void)
2099eec11310SNamhyung Kim {
2100eec11310SNamhyung Kim 	struct perf_pmu *pmu, *tmp;
2101eec11310SNamhyung Kim 
2102eec11310SNamhyung Kim 	list_for_each_entry_safe(pmu, tmp, &pmus, list) {
2103eec11310SNamhyung Kim 		list_del(&pmu->list);
2104eec11310SNamhyung Kim 		list_del(&pmu->hybrid_list);
2105eec11310SNamhyung Kim 
2106eec11310SNamhyung Kim 		perf_pmu__delete(pmu);
2107eec11310SNamhyung Kim 	}
2108eec11310SNamhyung Kim }
2109