xref: /linux/tools/perf/util/pmu.c (revision a43783aeec5fac8ef372ff8c0a5bbb3056fc0604)
1cd82a32eSJiri Olsa #include <linux/list.h>
2c5de47f2SSukadev Bhattiprolu #include <linux/compiler.h>
3cd82a32eSJiri Olsa #include <sys/types.h>
4*a43783aeSArnaldo Carvalho de Melo #include <errno.h>
5cd82a32eSJiri Olsa #include <unistd.h>
6cd82a32eSJiri Olsa #include <stdio.h>
7dc0a6202SAdrian Hunter #include <stdbool.h>
87d4bdab5SAdrian Hunter #include <stdarg.h>
9cd82a32eSJiri Olsa #include <dirent.h>
10cd0cfad7SBorislav Petkov #include <api/fs/fs.h>
11410136f5SStephane Eranian #include <locale.h>
12cd82a32eSJiri Olsa #include "util.h"
13cd82a32eSJiri Olsa #include "pmu.h"
14cd82a32eSJiri Olsa #include "parse-events.h"
157ae92e74SYan, Zheng #include "cpumap.h"
16933f82ffSSukadev Bhattiprolu #include "header.h"
17933f82ffSSukadev Bhattiprolu #include "pmu-events/pmu-events.h"
1861eb2eb4SAndi Kleen #include "cache.h"
19a067558eSArnaldo Carvalho de Melo #include "string2.h"
20cd82a32eSJiri Olsa 
21ab1bf653SArnaldo Carvalho de Melo struct perf_pmu_format {
22ab1bf653SArnaldo Carvalho de Melo 	char *name;
23ab1bf653SArnaldo Carvalho de Melo 	int value;
24ab1bf653SArnaldo Carvalho de Melo 	DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
25ab1bf653SArnaldo Carvalho de Melo 	struct list_head list;
26ab1bf653SArnaldo Carvalho de Melo };
27ab1bf653SArnaldo Carvalho de Melo 
2850a9667cSRobert Richter #define EVENT_SOURCE_DEVICE_PATH "/bus/event_source/devices/"
2950a9667cSRobert Richter 
30cd82a32eSJiri Olsa int perf_pmu_parse(struct list_head *list, char *name);
31cd82a32eSJiri Olsa extern FILE *perf_pmu_in;
32cd82a32eSJiri Olsa 
33cd82a32eSJiri Olsa static LIST_HEAD(pmus);
34cd82a32eSJiri Olsa 
35cd82a32eSJiri Olsa /*
36cd82a32eSJiri Olsa  * Parse & process all the sysfs attributes located under
37cd82a32eSJiri Olsa  * the directory specified in 'dir' parameter.
38cd82a32eSJiri Olsa  */
39cff7f956SJiri Olsa int perf_pmu__format_parse(char *dir, struct list_head *head)
40cd82a32eSJiri Olsa {
41cd82a32eSJiri Olsa 	struct dirent *evt_ent;
42cd82a32eSJiri Olsa 	DIR *format_dir;
43cd82a32eSJiri Olsa 	int ret = 0;
44cd82a32eSJiri Olsa 
45cd82a32eSJiri Olsa 	format_dir = opendir(dir);
46cd82a32eSJiri Olsa 	if (!format_dir)
47cd82a32eSJiri Olsa 		return -EINVAL;
48cd82a32eSJiri Olsa 
49cd82a32eSJiri Olsa 	while (!ret && (evt_ent = readdir(format_dir))) {
50cd82a32eSJiri Olsa 		char path[PATH_MAX];
51cd82a32eSJiri Olsa 		char *name = evt_ent->d_name;
52cd82a32eSJiri Olsa 		FILE *file;
53cd82a32eSJiri Olsa 
54cd82a32eSJiri Olsa 		if (!strcmp(name, ".") || !strcmp(name, ".."))
55cd82a32eSJiri Olsa 			continue;
56cd82a32eSJiri Olsa 
57cd82a32eSJiri Olsa 		snprintf(path, PATH_MAX, "%s/%s", dir, name);
58cd82a32eSJiri Olsa 
59cd82a32eSJiri Olsa 		ret = -EINVAL;
60cd82a32eSJiri Olsa 		file = fopen(path, "r");
61cd82a32eSJiri Olsa 		if (!file)
62cd82a32eSJiri Olsa 			break;
63cd82a32eSJiri Olsa 
64cd82a32eSJiri Olsa 		perf_pmu_in = file;
65cd82a32eSJiri Olsa 		ret = perf_pmu_parse(head, name);
66cd82a32eSJiri Olsa 		fclose(file);
67cd82a32eSJiri Olsa 	}
68cd82a32eSJiri Olsa 
69cd82a32eSJiri Olsa 	closedir(format_dir);
70cd82a32eSJiri Olsa 	return ret;
71cd82a32eSJiri Olsa }
72cd82a32eSJiri Olsa 
73cd82a32eSJiri Olsa /*
74cd82a32eSJiri Olsa  * Reading/parsing the default pmu format definition, which should be
75cd82a32eSJiri Olsa  * located at:
76cd82a32eSJiri Olsa  * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
77cd82a32eSJiri Olsa  */
78b6b96fb4SAdrian Hunter static int pmu_format(const char *name, struct list_head *format)
79cd82a32eSJiri Olsa {
80cd82a32eSJiri Olsa 	struct stat st;
81cd82a32eSJiri Olsa 	char path[PATH_MAX];
82cf38fadaSArnaldo Carvalho de Melo 	const char *sysfs = sysfs__mountpoint();
83cd82a32eSJiri Olsa 
84cd82a32eSJiri Olsa 	if (!sysfs)
85cd82a32eSJiri Olsa 		return -1;
86cd82a32eSJiri Olsa 
87cd82a32eSJiri Olsa 	snprintf(path, PATH_MAX,
8850a9667cSRobert Richter 		 "%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name);
89cd82a32eSJiri Olsa 
90cd82a32eSJiri Olsa 	if (stat(path, &st) < 0)
919bc8f9feSRobert Richter 		return 0;	/* no error if format does not exist */
92cd82a32eSJiri Olsa 
93cff7f956SJiri Olsa 	if (perf_pmu__format_parse(path, format))
94cd82a32eSJiri Olsa 		return -1;
95cd82a32eSJiri Olsa 
96cd82a32eSJiri Olsa 	return 0;
97cd82a32eSJiri Olsa }
98cd82a32eSJiri Olsa 
99d02fc6bcSAndi Kleen static int convert_scale(const char *scale, char **end, double *sval)
100d02fc6bcSAndi Kleen {
101d02fc6bcSAndi Kleen 	char *lc;
102d02fc6bcSAndi Kleen 	int ret = 0;
103d02fc6bcSAndi Kleen 
104d02fc6bcSAndi Kleen 	/*
105d02fc6bcSAndi Kleen 	 * save current locale
106d02fc6bcSAndi Kleen 	 */
107d02fc6bcSAndi Kleen 	lc = setlocale(LC_NUMERIC, NULL);
108d02fc6bcSAndi Kleen 
109d02fc6bcSAndi Kleen 	/*
110d02fc6bcSAndi Kleen 	 * The lc string may be allocated in static storage,
111d02fc6bcSAndi Kleen 	 * so get a dynamic copy to make it survive setlocale
112d02fc6bcSAndi Kleen 	 * call below.
113d02fc6bcSAndi Kleen 	 */
114d02fc6bcSAndi Kleen 	lc = strdup(lc);
115d02fc6bcSAndi Kleen 	if (!lc) {
116d02fc6bcSAndi Kleen 		ret = -ENOMEM;
117d02fc6bcSAndi Kleen 		goto out;
118d02fc6bcSAndi Kleen 	}
119d02fc6bcSAndi Kleen 
120d02fc6bcSAndi Kleen 	/*
121d02fc6bcSAndi Kleen 	 * force to C locale to ensure kernel
122d02fc6bcSAndi Kleen 	 * scale string is converted correctly.
123d02fc6bcSAndi Kleen 	 * kernel uses default C locale.
124d02fc6bcSAndi Kleen 	 */
125d02fc6bcSAndi Kleen 	setlocale(LC_NUMERIC, "C");
126d02fc6bcSAndi Kleen 
127d02fc6bcSAndi Kleen 	*sval = strtod(scale, end);
128d02fc6bcSAndi Kleen 
129d02fc6bcSAndi Kleen out:
130d02fc6bcSAndi Kleen 	/* restore locale */
131d02fc6bcSAndi Kleen 	setlocale(LC_NUMERIC, lc);
132d02fc6bcSAndi Kleen 	free(lc);
133d02fc6bcSAndi Kleen 	return ret;
134d02fc6bcSAndi Kleen }
135d02fc6bcSAndi Kleen 
136410136f5SStephane Eranian static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name)
137410136f5SStephane Eranian {
138410136f5SStephane Eranian 	struct stat st;
139410136f5SStephane Eranian 	ssize_t sret;
140410136f5SStephane Eranian 	char scale[128];
141410136f5SStephane Eranian 	int fd, ret = -1;
142410136f5SStephane Eranian 	char path[PATH_MAX];
143410136f5SStephane Eranian 
144410136f5SStephane Eranian 	snprintf(path, PATH_MAX, "%s/%s.scale", dir, name);
145410136f5SStephane Eranian 
146410136f5SStephane Eranian 	fd = open(path, O_RDONLY);
147410136f5SStephane Eranian 	if (fd == -1)
148410136f5SStephane Eranian 		return -1;
149410136f5SStephane Eranian 
150410136f5SStephane Eranian 	if (fstat(fd, &st) < 0)
151410136f5SStephane Eranian 		goto error;
152410136f5SStephane Eranian 
153410136f5SStephane Eranian 	sret = read(fd, scale, sizeof(scale)-1);
154410136f5SStephane Eranian 	if (sret < 0)
155410136f5SStephane Eranian 		goto error;
156410136f5SStephane Eranian 
1579ecae065SMadhavan Srinivasan 	if (scale[sret - 1] == '\n')
1589ecae065SMadhavan Srinivasan 		scale[sret - 1] = '\0';
1599ecae065SMadhavan Srinivasan 	else
160410136f5SStephane Eranian 		scale[sret] = '\0';
1619ecae065SMadhavan Srinivasan 
162d02fc6bcSAndi Kleen 	ret = convert_scale(scale, NULL, &alias->scale);
163410136f5SStephane Eranian error:
164410136f5SStephane Eranian 	close(fd);
165410136f5SStephane Eranian 	return ret;
166410136f5SStephane Eranian }
167410136f5SStephane Eranian 
168410136f5SStephane Eranian static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name)
169410136f5SStephane Eranian {
170410136f5SStephane Eranian 	char path[PATH_MAX];
171410136f5SStephane Eranian 	ssize_t sret;
172410136f5SStephane Eranian 	int fd;
173410136f5SStephane Eranian 
174410136f5SStephane Eranian 	snprintf(path, PATH_MAX, "%s/%s.unit", dir, name);
175410136f5SStephane Eranian 
176410136f5SStephane Eranian 	fd = open(path, O_RDONLY);
177410136f5SStephane Eranian 	if (fd == -1)
178410136f5SStephane Eranian 		return -1;
179410136f5SStephane Eranian 
180410136f5SStephane Eranian 	sret = read(fd, alias->unit, UNIT_MAX_LEN);
181410136f5SStephane Eranian 	if (sret < 0)
182410136f5SStephane Eranian 		goto error;
183410136f5SStephane Eranian 
184410136f5SStephane Eranian 	close(fd);
185410136f5SStephane Eranian 
1869ecae065SMadhavan Srinivasan 	if (alias->unit[sret - 1] == '\n')
1879ecae065SMadhavan Srinivasan 		alias->unit[sret - 1] = '\0';
1889ecae065SMadhavan Srinivasan 	else
189410136f5SStephane Eranian 		alias->unit[sret] = '\0';
190410136f5SStephane Eranian 
191410136f5SStephane Eranian 	return 0;
192410136f5SStephane Eranian error:
193410136f5SStephane Eranian 	close(fd);
194410136f5SStephane Eranian 	alias->unit[0] = '\0';
195410136f5SStephane Eranian 	return -1;
196410136f5SStephane Eranian }
197410136f5SStephane Eranian 
198044330c1SMatt Fleming static int
199044330c1SMatt Fleming perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *name)
200044330c1SMatt Fleming {
201044330c1SMatt Fleming 	char path[PATH_MAX];
202044330c1SMatt Fleming 	int fd;
203044330c1SMatt Fleming 
204044330c1SMatt Fleming 	snprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name);
205044330c1SMatt Fleming 
206044330c1SMatt Fleming 	fd = open(path, O_RDONLY);
207044330c1SMatt Fleming 	if (fd == -1)
208044330c1SMatt Fleming 		return -1;
209044330c1SMatt Fleming 
210044330c1SMatt Fleming 	close(fd);
211044330c1SMatt Fleming 
212044330c1SMatt Fleming 	alias->per_pkg = true;
213044330c1SMatt Fleming 	return 0;
214044330c1SMatt Fleming }
215044330c1SMatt Fleming 
2161d9e446bSJiri Olsa static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias,
2171d9e446bSJiri Olsa 				    char *dir, char *name)
2181d9e446bSJiri Olsa {
2191d9e446bSJiri Olsa 	char path[PATH_MAX];
2201d9e446bSJiri Olsa 	int fd;
2211d9e446bSJiri Olsa 
2221d9e446bSJiri Olsa 	snprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name);
2231d9e446bSJiri Olsa 
2241d9e446bSJiri Olsa 	fd = open(path, O_RDONLY);
2251d9e446bSJiri Olsa 	if (fd == -1)
2261d9e446bSJiri Olsa 		return -1;
2271d9e446bSJiri Olsa 
2281d9e446bSJiri Olsa 	alias->snapshot = true;
2291d9e446bSJiri Olsa 	close(fd);
2301d9e446bSJiri Olsa 	return 0;
2311d9e446bSJiri Olsa }
2321d9e446bSJiri Olsa 
23370c646e0SSukadev Bhattiprolu static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name,
234fedb2b51SAndi Kleen 				 char *desc, char *val,
235fedb2b51SAndi Kleen 				 char *long_desc, char *topic,
23600636c3bSAndi Kleen 				 char *unit, char *perpkg,
23796284814SAndi Kleen 				 char *metric_expr,
23896284814SAndi Kleen 				 char *metric_name)
239a6146d50SZheng Yan {
2405c6ccc37SArnaldo Carvalho de Melo 	struct perf_pmu_alias *alias;
241a6146d50SZheng Yan 	int ret;
242fedb2b51SAndi Kleen 	int num;
243a6146d50SZheng Yan 
244a6146d50SZheng Yan 	alias = malloc(sizeof(*alias));
245a6146d50SZheng Yan 	if (!alias)
246a6146d50SZheng Yan 		return -ENOMEM;
247a6146d50SZheng Yan 
248a6146d50SZheng Yan 	INIT_LIST_HEAD(&alias->terms);
249410136f5SStephane Eranian 	alias->scale = 1.0;
250410136f5SStephane Eranian 	alias->unit[0] = '\0';
251044330c1SMatt Fleming 	alias->per_pkg = false;
25284530920SStephane Eranian 	alias->snapshot = false;
253410136f5SStephane Eranian 
25470c646e0SSukadev Bhattiprolu 	ret = parse_events_terms(&alias->terms, val);
255a6146d50SZheng Yan 	if (ret) {
25670c646e0SSukadev Bhattiprolu 		pr_err("Cannot parse alias %s: %d\n", val, ret);
257a6146d50SZheng Yan 		free(alias);
258a6146d50SZheng Yan 		return ret;
259a6146d50SZheng Yan 	}
260a6146d50SZheng Yan 
261a6146d50SZheng Yan 	alias->name = strdup(name);
26270c646e0SSukadev Bhattiprolu 	if (dir) {
263410136f5SStephane Eranian 		/*
264410136f5SStephane Eranian 		 * load unit name and scale if available
265410136f5SStephane Eranian 		 */
266410136f5SStephane Eranian 		perf_pmu__parse_unit(alias, dir, name);
267410136f5SStephane Eranian 		perf_pmu__parse_scale(alias, dir, name);
268044330c1SMatt Fleming 		perf_pmu__parse_per_pkg(alias, dir, name);
2691d9e446bSJiri Olsa 		perf_pmu__parse_snapshot(alias, dir, name);
27070c646e0SSukadev Bhattiprolu 	}
271410136f5SStephane Eranian 
27200636c3bSAndi Kleen 	alias->metric_expr = metric_expr ? strdup(metric_expr) : NULL;
27396284814SAndi Kleen 	alias->metric_name = metric_name ? strdup(metric_name): NULL;
27408e60ed1SAndi Kleen 	alias->desc = desc ? strdup(desc) : NULL;
275c8d6828aSSukadev Bhattiprolu 	alias->long_desc = long_desc ? strdup(long_desc) :
276c8d6828aSSukadev Bhattiprolu 				desc ? strdup(desc) : NULL;
277dd5f1036SAndi Kleen 	alias->topic = topic ? strdup(topic) : NULL;
278fedb2b51SAndi Kleen 	if (unit) {
279fedb2b51SAndi Kleen 		if (convert_scale(unit, &unit, &alias->scale) < 0)
280fedb2b51SAndi Kleen 			return -1;
281fedb2b51SAndi Kleen 		snprintf(alias->unit, sizeof(alias->unit), "%s", unit);
282fedb2b51SAndi Kleen 	}
283fedb2b51SAndi Kleen 	alias->per_pkg = perpkg && sscanf(perpkg, "%d", &num) == 1 && num == 1;
284f2361024SAndi Kleen 	alias->str = strdup(val);
285f2361024SAndi Kleen 
286a6146d50SZheng Yan 	list_add_tail(&alias->list, list);
287410136f5SStephane Eranian 
288a6146d50SZheng Yan 	return 0;
289a6146d50SZheng Yan }
290a6146d50SZheng Yan 
29170c646e0SSukadev Bhattiprolu static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file)
29270c646e0SSukadev Bhattiprolu {
29370c646e0SSukadev Bhattiprolu 	char buf[256];
29470c646e0SSukadev Bhattiprolu 	int ret;
29570c646e0SSukadev Bhattiprolu 
29670c646e0SSukadev Bhattiprolu 	ret = fread(buf, 1, sizeof(buf), file);
29770c646e0SSukadev Bhattiprolu 	if (ret == 0)
29870c646e0SSukadev Bhattiprolu 		return -EINVAL;
29970c646e0SSukadev Bhattiprolu 
30070c646e0SSukadev Bhattiprolu 	buf[ret] = 0;
30170c646e0SSukadev Bhattiprolu 
302fedb2b51SAndi Kleen 	return __perf_pmu__new_alias(list, dir, name, NULL, buf, NULL, NULL, NULL,
30396284814SAndi Kleen 				     NULL, NULL, NULL);
30470c646e0SSukadev Bhattiprolu }
30570c646e0SSukadev Bhattiprolu 
30646441bdcSMatt Fleming static inline bool pmu_alias_info_file(char *name)
30746441bdcSMatt Fleming {
30846441bdcSMatt Fleming 	size_t len;
30946441bdcSMatt Fleming 
31046441bdcSMatt Fleming 	len = strlen(name);
31146441bdcSMatt Fleming 	if (len > 5 && !strcmp(name + len - 5, ".unit"))
31246441bdcSMatt Fleming 		return true;
31346441bdcSMatt Fleming 	if (len > 6 && !strcmp(name + len - 6, ".scale"))
31446441bdcSMatt Fleming 		return true;
315044330c1SMatt Fleming 	if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
316044330c1SMatt Fleming 		return true;
3171d9e446bSJiri Olsa 	if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
3181d9e446bSJiri Olsa 		return true;
31946441bdcSMatt Fleming 
32046441bdcSMatt Fleming 	return false;
32146441bdcSMatt Fleming }
32246441bdcSMatt Fleming 
323a6146d50SZheng Yan /*
324a6146d50SZheng Yan  * Process all the sysfs attributes located under the directory
325a6146d50SZheng Yan  * specified in 'dir' parameter.
326a6146d50SZheng Yan  */
327a6146d50SZheng Yan static int pmu_aliases_parse(char *dir, struct list_head *head)
328a6146d50SZheng Yan {
329a6146d50SZheng Yan 	struct dirent *evt_ent;
330a6146d50SZheng Yan 	DIR *event_dir;
331a6146d50SZheng Yan 
332a6146d50SZheng Yan 	event_dir = opendir(dir);
333a6146d50SZheng Yan 	if (!event_dir)
334a6146d50SZheng Yan 		return -EINVAL;
335a6146d50SZheng Yan 
336940db6dcSAndi Kleen 	while ((evt_ent = readdir(event_dir))) {
337a6146d50SZheng Yan 		char path[PATH_MAX];
338a6146d50SZheng Yan 		char *name = evt_ent->d_name;
339a6146d50SZheng Yan 		FILE *file;
340a6146d50SZheng Yan 
341a6146d50SZheng Yan 		if (!strcmp(name, ".") || !strcmp(name, ".."))
342a6146d50SZheng Yan 			continue;
343a6146d50SZheng Yan 
344410136f5SStephane Eranian 		/*
34546441bdcSMatt Fleming 		 * skip info files parsed in perf_pmu__new_alias()
346410136f5SStephane Eranian 		 */
34746441bdcSMatt Fleming 		if (pmu_alias_info_file(name))
348410136f5SStephane Eranian 			continue;
349410136f5SStephane Eranian 
350a6146d50SZheng Yan 		snprintf(path, PATH_MAX, "%s/%s", dir, name);
351a6146d50SZheng Yan 
352a6146d50SZheng Yan 		file = fopen(path, "r");
353940db6dcSAndi Kleen 		if (!file) {
354940db6dcSAndi Kleen 			pr_debug("Cannot open %s\n", path);
355940db6dcSAndi Kleen 			continue;
356940db6dcSAndi Kleen 		}
357410136f5SStephane Eranian 
358940db6dcSAndi Kleen 		if (perf_pmu__new_alias(head, dir, name, file) < 0)
359940db6dcSAndi Kleen 			pr_debug("Cannot set up %s\n", name);
360a6146d50SZheng Yan 		fclose(file);
361a6146d50SZheng Yan 	}
362a6146d50SZheng Yan 
363a6146d50SZheng Yan 	closedir(event_dir);
364940db6dcSAndi Kleen 	return 0;
365a6146d50SZheng Yan }
366a6146d50SZheng Yan 
367a6146d50SZheng Yan /*
368a6146d50SZheng Yan  * Reading the pmu event aliases definition, which should be located at:
369a6146d50SZheng Yan  * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
370a6146d50SZheng Yan  */
371b6b96fb4SAdrian Hunter static int pmu_aliases(const char *name, struct list_head *head)
372a6146d50SZheng Yan {
373a6146d50SZheng Yan 	struct stat st;
374a6146d50SZheng Yan 	char path[PATH_MAX];
375cf38fadaSArnaldo Carvalho de Melo 	const char *sysfs = sysfs__mountpoint();
376a6146d50SZheng Yan 
377a6146d50SZheng Yan 	if (!sysfs)
378a6146d50SZheng Yan 		return -1;
379a6146d50SZheng Yan 
380a6146d50SZheng Yan 	snprintf(path, PATH_MAX,
381a6146d50SZheng Yan 		 "%s/bus/event_source/devices/%s/events", sysfs, name);
382a6146d50SZheng Yan 
383a6146d50SZheng Yan 	if (stat(path, &st) < 0)
3843fded963SJiri Olsa 		return 0;	 /* no error if 'events' does not exist */
385a6146d50SZheng Yan 
386a6146d50SZheng Yan 	if (pmu_aliases_parse(path, head))
387a6146d50SZheng Yan 		return -1;
388a6146d50SZheng Yan 
389a6146d50SZheng Yan 	return 0;
390a6146d50SZheng Yan }
391a6146d50SZheng Yan 
3925c6ccc37SArnaldo Carvalho de Melo static int pmu_alias_terms(struct perf_pmu_alias *alias,
393a6146d50SZheng Yan 			   struct list_head *terms)
394a6146d50SZheng Yan {
3957c2f8164SJiri Olsa 	struct parse_events_term *term, *cloned;
396a6146d50SZheng Yan 	LIST_HEAD(list);
397a6146d50SZheng Yan 	int ret;
398a6146d50SZheng Yan 
399a6146d50SZheng Yan 	list_for_each_entry(term, &alias->terms, list) {
4007c2f8164SJiri Olsa 		ret = parse_events_term__clone(&cloned, term);
401a6146d50SZheng Yan 		if (ret) {
402682dc24cSArnaldo Carvalho de Melo 			parse_events_terms__purge(&list);
403a6146d50SZheng Yan 			return ret;
404a6146d50SZheng Yan 		}
4057c2f8164SJiri Olsa 		list_add_tail(&cloned->list, &list);
406a6146d50SZheng Yan 	}
407a6146d50SZheng Yan 	list_splice(&list, terms);
408a6146d50SZheng Yan 	return 0;
409a6146d50SZheng Yan }
410a6146d50SZheng Yan 
411cd82a32eSJiri Olsa /*
412cd82a32eSJiri Olsa  * Reading/parsing the default pmu type value, which should be
413cd82a32eSJiri Olsa  * located at:
414cd82a32eSJiri Olsa  * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
415cd82a32eSJiri Olsa  */
416b6b96fb4SAdrian Hunter static int pmu_type(const char *name, __u32 *type)
417cd82a32eSJiri Olsa {
418cd82a32eSJiri Olsa 	struct stat st;
419cd82a32eSJiri Olsa 	char path[PATH_MAX];
420cd82a32eSJiri Olsa 	FILE *file;
421cd82a32eSJiri Olsa 	int ret = 0;
422cf38fadaSArnaldo Carvalho de Melo 	const char *sysfs = sysfs__mountpoint();
423cd82a32eSJiri Olsa 
424cd82a32eSJiri Olsa 	if (!sysfs)
425cd82a32eSJiri Olsa 		return -1;
426cd82a32eSJiri Olsa 
427cd82a32eSJiri Olsa 	snprintf(path, PATH_MAX,
42850a9667cSRobert Richter 		 "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name);
429cd82a32eSJiri Olsa 
430cd82a32eSJiri Olsa 	if (stat(path, &st) < 0)
431cd82a32eSJiri Olsa 		return -1;
432cd82a32eSJiri Olsa 
433cd82a32eSJiri Olsa 	file = fopen(path, "r");
434cd82a32eSJiri Olsa 	if (!file)
435cd82a32eSJiri Olsa 		return -EINVAL;
436cd82a32eSJiri Olsa 
437cd82a32eSJiri Olsa 	if (1 != fscanf(file, "%u", type))
438cd82a32eSJiri Olsa 		ret = -1;
439cd82a32eSJiri Olsa 
440cd82a32eSJiri Olsa 	fclose(file);
441cd82a32eSJiri Olsa 	return ret;
442cd82a32eSJiri Olsa }
443cd82a32eSJiri Olsa 
44450a9667cSRobert Richter /* Add all pmus in sysfs to pmu list: */
44550a9667cSRobert Richter static void pmu_read_sysfs(void)
44650a9667cSRobert Richter {
44750a9667cSRobert Richter 	char path[PATH_MAX];
44850a9667cSRobert Richter 	DIR *dir;
44950a9667cSRobert Richter 	struct dirent *dent;
450cf38fadaSArnaldo Carvalho de Melo 	const char *sysfs = sysfs__mountpoint();
45150a9667cSRobert Richter 
45250a9667cSRobert Richter 	if (!sysfs)
45350a9667cSRobert Richter 		return;
45450a9667cSRobert Richter 
45550a9667cSRobert Richter 	snprintf(path, PATH_MAX,
45650a9667cSRobert Richter 		 "%s" EVENT_SOURCE_DEVICE_PATH, sysfs);
45750a9667cSRobert Richter 
45850a9667cSRobert Richter 	dir = opendir(path);
45950a9667cSRobert Richter 	if (!dir)
46050a9667cSRobert Richter 		return;
46150a9667cSRobert Richter 
46250a9667cSRobert Richter 	while ((dent = readdir(dir))) {
46350a9667cSRobert Richter 		if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
46450a9667cSRobert Richter 			continue;
46550a9667cSRobert Richter 		/* add to static LIST_HEAD(pmus): */
46650a9667cSRobert Richter 		perf_pmu__find(dent->d_name);
46750a9667cSRobert Richter 	}
46850a9667cSRobert Richter 
46950a9667cSRobert Richter 	closedir(dir);
47050a9667cSRobert Richter }
47150a9667cSRobert Richter 
472b6b96fb4SAdrian Hunter static struct cpu_map *pmu_cpumask(const char *name)
4737ae92e74SYan, Zheng {
4747ae92e74SYan, Zheng 	struct stat st;
4757ae92e74SYan, Zheng 	char path[PATH_MAX];
4767ae92e74SYan, Zheng 	FILE *file;
4777ae92e74SYan, Zheng 	struct cpu_map *cpus;
478cf38fadaSArnaldo Carvalho de Melo 	const char *sysfs = sysfs__mountpoint();
4797e3fcffeSMark Rutland 	const char *templates[] = {
4807e3fcffeSMark Rutland 		 "%s/bus/event_source/devices/%s/cpumask",
4817e3fcffeSMark Rutland 		 "%s/bus/event_source/devices/%s/cpus",
4827e3fcffeSMark Rutland 		 NULL
4837e3fcffeSMark Rutland 	};
4847e3fcffeSMark Rutland 	const char **template;
4857ae92e74SYan, Zheng 
4867ae92e74SYan, Zheng 	if (!sysfs)
4877ae92e74SYan, Zheng 		return NULL;
4887ae92e74SYan, Zheng 
4897e3fcffeSMark Rutland 	for (template = templates; *template; template++) {
4907e3fcffeSMark Rutland 		snprintf(path, PATH_MAX, *template, sysfs, name);
4917e3fcffeSMark Rutland 		if (stat(path, &st) == 0)
4927e3fcffeSMark Rutland 			break;
4937e3fcffeSMark Rutland 	}
4947ae92e74SYan, Zheng 
4957e3fcffeSMark Rutland 	if (!*template)
4967ae92e74SYan, Zheng 		return NULL;
4977ae92e74SYan, Zheng 
4987ae92e74SYan, Zheng 	file = fopen(path, "r");
4997ae92e74SYan, Zheng 	if (!file)
5007ae92e74SYan, Zheng 		return NULL;
5017ae92e74SYan, Zheng 
5027ae92e74SYan, Zheng 	cpus = cpu_map__read(file);
5037ae92e74SYan, Zheng 	fclose(file);
5047ae92e74SYan, Zheng 	return cpus;
5057ae92e74SYan, Zheng }
5067ae92e74SYan, Zheng 
507933f82ffSSukadev Bhattiprolu /*
508933f82ffSSukadev Bhattiprolu  * Return the CPU id as a raw string.
509933f82ffSSukadev Bhattiprolu  *
510933f82ffSSukadev Bhattiprolu  * Each architecture should provide a more precise id string that
511933f82ffSSukadev Bhattiprolu  * can be use to match the architecture's "mapfile".
512933f82ffSSukadev Bhattiprolu  */
513933f82ffSSukadev Bhattiprolu char * __weak get_cpuid_str(void)
514933f82ffSSukadev Bhattiprolu {
515933f82ffSSukadev Bhattiprolu 	return NULL;
516933f82ffSSukadev Bhattiprolu }
517933f82ffSSukadev Bhattiprolu 
518933f82ffSSukadev Bhattiprolu /*
519933f82ffSSukadev Bhattiprolu  * From the pmu_events_map, find the table of PMU events that corresponds
520933f82ffSSukadev Bhattiprolu  * to the current running CPU. Then, add all PMU events from that table
521933f82ffSSukadev Bhattiprolu  * as aliases.
522933f82ffSSukadev Bhattiprolu  */
523fedb2b51SAndi Kleen static void pmu_add_cpu_aliases(struct list_head *head, const char *name)
524933f82ffSSukadev Bhattiprolu {
525933f82ffSSukadev Bhattiprolu 	int i;
526933f82ffSSukadev Bhattiprolu 	struct pmu_events_map *map;
527933f82ffSSukadev Bhattiprolu 	struct pmu_event *pe;
528933f82ffSSukadev Bhattiprolu 	char *cpuid;
529fb967063SAndi Kleen 	static bool printed;
530933f82ffSSukadev Bhattiprolu 
531fc06e2a5SAndi Kleen 	cpuid = getenv("PERF_CPUID");
532fc06e2a5SAndi Kleen 	if (cpuid)
533fc06e2a5SAndi Kleen 		cpuid = strdup(cpuid);
534fc06e2a5SAndi Kleen 	if (!cpuid)
535933f82ffSSukadev Bhattiprolu 		cpuid = get_cpuid_str();
536933f82ffSSukadev Bhattiprolu 	if (!cpuid)
537933f82ffSSukadev Bhattiprolu 		return;
538933f82ffSSukadev Bhattiprolu 
539fb967063SAndi Kleen 	if (!printed) {
540fc06e2a5SAndi Kleen 		pr_debug("Using CPUID %s\n", cpuid);
541fb967063SAndi Kleen 		printed = true;
542fb967063SAndi Kleen 	}
543fc06e2a5SAndi Kleen 
544933f82ffSSukadev Bhattiprolu 	i = 0;
545933f82ffSSukadev Bhattiprolu 	while (1) {
546933f82ffSSukadev Bhattiprolu 		map = &pmu_events_map[i++];
547933f82ffSSukadev Bhattiprolu 		if (!map->table)
548933f82ffSSukadev Bhattiprolu 			goto out;
549933f82ffSSukadev Bhattiprolu 
550933f82ffSSukadev Bhattiprolu 		if (!strcmp(map->cpuid, cpuid))
551933f82ffSSukadev Bhattiprolu 			break;
552933f82ffSSukadev Bhattiprolu 	}
553933f82ffSSukadev Bhattiprolu 
554933f82ffSSukadev Bhattiprolu 	/*
555933f82ffSSukadev Bhattiprolu 	 * Found a matching PMU events table. Create aliases
556933f82ffSSukadev Bhattiprolu 	 */
557933f82ffSSukadev Bhattiprolu 	i = 0;
558933f82ffSSukadev Bhattiprolu 	while (1) {
559fedb2b51SAndi Kleen 		const char *pname;
560fedb2b51SAndi Kleen 
561933f82ffSSukadev Bhattiprolu 		pe = &map->table[i++];
562933f82ffSSukadev Bhattiprolu 		if (!pe->name)
563933f82ffSSukadev Bhattiprolu 			break;
564933f82ffSSukadev Bhattiprolu 
565fedb2b51SAndi Kleen 		pname = pe->pmu ? pe->pmu : "cpu";
566fedb2b51SAndi Kleen 		if (strncmp(pname, name, strlen(pname)))
567fedb2b51SAndi Kleen 			continue;
568fedb2b51SAndi Kleen 
569933f82ffSSukadev Bhattiprolu 		/* need type casts to override 'const' */
570933f82ffSSukadev Bhattiprolu 		__perf_pmu__new_alias(head, NULL, (char *)pe->name,
571c8d6828aSSukadev Bhattiprolu 				(char *)pe->desc, (char *)pe->event,
572fedb2b51SAndi Kleen 				(char *)pe->long_desc, (char *)pe->topic,
57300636c3bSAndi Kleen 				(char *)pe->unit, (char *)pe->perpkg,
57496284814SAndi Kleen 				(char *)pe->metric_expr,
57596284814SAndi Kleen 				(char *)pe->metric_name);
576933f82ffSSukadev Bhattiprolu 	}
577933f82ffSSukadev Bhattiprolu 
578933f82ffSSukadev Bhattiprolu out:
579933f82ffSSukadev Bhattiprolu 	free(cpuid);
580933f82ffSSukadev Bhattiprolu }
581933f82ffSSukadev Bhattiprolu 
582c5de47f2SSukadev Bhattiprolu struct perf_event_attr * __weak
583dc0a6202SAdrian Hunter perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
584dc0a6202SAdrian Hunter {
585dc0a6202SAdrian Hunter 	return NULL;
586dc0a6202SAdrian Hunter }
587dc0a6202SAdrian Hunter 
588b6b96fb4SAdrian Hunter static struct perf_pmu *pmu_lookup(const char *name)
589cd82a32eSJiri Olsa {
590cd82a32eSJiri Olsa 	struct perf_pmu *pmu;
591cd82a32eSJiri Olsa 	LIST_HEAD(format);
592a6146d50SZheng Yan 	LIST_HEAD(aliases);
593cd82a32eSJiri Olsa 	__u32 type;
594cd82a32eSJiri Olsa 
595cd82a32eSJiri Olsa 	/*
596cd82a32eSJiri Olsa 	 * The pmu data we store & need consists of the pmu
597cd82a32eSJiri Olsa 	 * type value and format definitions. Load both right
598cd82a32eSJiri Olsa 	 * now.
599cd82a32eSJiri Olsa 	 */
600cd82a32eSJiri Olsa 	if (pmu_format(name, &format))
601cd82a32eSJiri Olsa 		return NULL;
602cd82a32eSJiri Olsa 
60315b22ed3SAndi Kleen 	/*
60415b22ed3SAndi Kleen 	 * Check the type first to avoid unnecessary work.
60515b22ed3SAndi Kleen 	 */
60615b22ed3SAndi Kleen 	if (pmu_type(name, &type))
60715b22ed3SAndi Kleen 		return NULL;
60815b22ed3SAndi Kleen 
6093fded963SJiri Olsa 	if (pmu_aliases(name, &aliases))
6103fded963SJiri Olsa 		return NULL;
6113fded963SJiri Olsa 
612fedb2b51SAndi Kleen 	pmu_add_cpu_aliases(&aliases, name);
613cd82a32eSJiri Olsa 	pmu = zalloc(sizeof(*pmu));
614cd82a32eSJiri Olsa 	if (!pmu)
615cd82a32eSJiri Olsa 		return NULL;
616cd82a32eSJiri Olsa 
6177ae92e74SYan, Zheng 	pmu->cpus = pmu_cpumask(name);
6187ae92e74SYan, Zheng 
619cd82a32eSJiri Olsa 	INIT_LIST_HEAD(&pmu->format);
620a6146d50SZheng Yan 	INIT_LIST_HEAD(&pmu->aliases);
621cd82a32eSJiri Olsa 	list_splice(&format, &pmu->format);
622a6146d50SZheng Yan 	list_splice(&aliases, &pmu->aliases);
623cd82a32eSJiri Olsa 	pmu->name = strdup(name);
624cd82a32eSJiri Olsa 	pmu->type = type;
6259bc8f9feSRobert Richter 	list_add_tail(&pmu->list, &pmus);
626dc0a6202SAdrian Hunter 
627dc0a6202SAdrian Hunter 	pmu->default_config = perf_pmu__get_default_config(pmu);
628dc0a6202SAdrian Hunter 
629cd82a32eSJiri Olsa 	return pmu;
630cd82a32eSJiri Olsa }
631cd82a32eSJiri Olsa 
632b6b96fb4SAdrian Hunter static struct perf_pmu *pmu_find(const char *name)
633cd82a32eSJiri Olsa {
634cd82a32eSJiri Olsa 	struct perf_pmu *pmu;
635cd82a32eSJiri Olsa 
636cd82a32eSJiri Olsa 	list_for_each_entry(pmu, &pmus, list)
637cd82a32eSJiri Olsa 		if (!strcmp(pmu->name, name))
638cd82a32eSJiri Olsa 			return pmu;
639cd82a32eSJiri Olsa 
640cd82a32eSJiri Olsa 	return NULL;
641cd82a32eSJiri Olsa }
642cd82a32eSJiri Olsa 
64350a9667cSRobert Richter struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
64450a9667cSRobert Richter {
64550a9667cSRobert Richter 	/*
64650a9667cSRobert Richter 	 * pmu iterator: If pmu is NULL, we start at the begin,
64750a9667cSRobert Richter 	 * otherwise return the next pmu. Returns NULL on end.
64850a9667cSRobert Richter 	 */
64950a9667cSRobert Richter 	if (!pmu) {
65050a9667cSRobert Richter 		pmu_read_sysfs();
65150a9667cSRobert Richter 		pmu = list_prepare_entry(pmu, &pmus, list);
65250a9667cSRobert Richter 	}
65350a9667cSRobert Richter 	list_for_each_entry_continue(pmu, &pmus, list)
65450a9667cSRobert Richter 		return pmu;
65550a9667cSRobert Richter 	return NULL;
65650a9667cSRobert Richter }
65750a9667cSRobert Richter 
658b6b96fb4SAdrian Hunter struct perf_pmu *perf_pmu__find(const char *name)
659cd82a32eSJiri Olsa {
660cd82a32eSJiri Olsa 	struct perf_pmu *pmu;
661cd82a32eSJiri Olsa 
662cd82a32eSJiri Olsa 	/*
663cd82a32eSJiri Olsa 	 * Once PMU is loaded it stays in the list,
664cd82a32eSJiri Olsa 	 * so we keep us from multiple reading/parsing
665cd82a32eSJiri Olsa 	 * the pmu format definitions.
666cd82a32eSJiri Olsa 	 */
667cd82a32eSJiri Olsa 	pmu = pmu_find(name);
668cd82a32eSJiri Olsa 	if (pmu)
669cd82a32eSJiri Olsa 		return pmu;
670cd82a32eSJiri Olsa 
671cd82a32eSJiri Olsa 	return pmu_lookup(name);
672cd82a32eSJiri Olsa }
673cd82a32eSJiri Olsa 
6745c6ccc37SArnaldo Carvalho de Melo static struct perf_pmu_format *
67509ff6071SAdrian Hunter pmu_find_format(struct list_head *formats, const char *name)
676cd82a32eSJiri Olsa {
6775c6ccc37SArnaldo Carvalho de Melo 	struct perf_pmu_format *format;
678cd82a32eSJiri Olsa 
679cd82a32eSJiri Olsa 	list_for_each_entry(format, formats, list)
680cd82a32eSJiri Olsa 		if (!strcmp(format->name, name))
681cd82a32eSJiri Olsa 			return format;
682cd82a32eSJiri Olsa 
683cd82a32eSJiri Olsa 	return NULL;
684cd82a32eSJiri Olsa }
685cd82a32eSJiri Olsa 
68609ff6071SAdrian Hunter __u64 perf_pmu__format_bits(struct list_head *formats, const char *name)
68709ff6071SAdrian Hunter {
68809ff6071SAdrian Hunter 	struct perf_pmu_format *format = pmu_find_format(formats, name);
68909ff6071SAdrian Hunter 	__u64 bits = 0;
69009ff6071SAdrian Hunter 	int fbit;
69109ff6071SAdrian Hunter 
69209ff6071SAdrian Hunter 	if (!format)
69309ff6071SAdrian Hunter 		return 0;
69409ff6071SAdrian Hunter 
69509ff6071SAdrian Hunter 	for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
69609ff6071SAdrian Hunter 		bits |= 1ULL << fbit;
69709ff6071SAdrian Hunter 
69809ff6071SAdrian Hunter 	return bits;
69909ff6071SAdrian Hunter }
70009ff6071SAdrian Hunter 
701cd82a32eSJiri Olsa /*
702dc0a6202SAdrian Hunter  * Sets value based on the format definition (format parameter)
703cd82a32eSJiri Olsa  * and unformated value (value parameter).
704cd82a32eSJiri Olsa  */
705dc0a6202SAdrian Hunter static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
706dc0a6202SAdrian Hunter 			     bool zero)
707cd82a32eSJiri Olsa {
708cd82a32eSJiri Olsa 	unsigned long fbit, vbit;
709cd82a32eSJiri Olsa 
710cd82a32eSJiri Olsa 	for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
711cd82a32eSJiri Olsa 
712cd82a32eSJiri Olsa 		if (!test_bit(fbit, format))
713cd82a32eSJiri Olsa 			continue;
714cd82a32eSJiri Olsa 
715dc0a6202SAdrian Hunter 		if (value & (1llu << vbit++))
716dc0a6202SAdrian Hunter 			*v |= (1llu << fbit);
717dc0a6202SAdrian Hunter 		else if (zero)
718dc0a6202SAdrian Hunter 			*v &= ~(1llu << fbit);
719cd82a32eSJiri Olsa 	}
720cd82a32eSJiri Olsa }
721cd82a32eSJiri Olsa 
7220efe6b67SAdrian Hunter static __u64 pmu_format_max_value(const unsigned long *format)
7230efe6b67SAdrian Hunter {
724ac0e2cd5SKan Liang 	__u64 w = 0;
725ac0e2cd5SKan Liang 	int fbit;
7260efe6b67SAdrian Hunter 
727ac0e2cd5SKan Liang 	for_each_set_bit(fbit, format, PERF_PMU_FORMAT_BITS)
728ac0e2cd5SKan Liang 		w |= (1ULL << fbit);
729ac0e2cd5SKan Liang 
730ac0e2cd5SKan Liang 	return w;
7310efe6b67SAdrian Hunter }
7320efe6b67SAdrian Hunter 
733cd82a32eSJiri Olsa /*
734688d4dfcSCody P Schafer  * Term is a string term, and might be a param-term. Try to look up it's value
735688d4dfcSCody P Schafer  * in the remaining terms.
736688d4dfcSCody P Schafer  * - We have a term like "base-or-format-term=param-term",
737688d4dfcSCody P Schafer  * - We need to find the value supplied for "param-term" (with param-term named
738688d4dfcSCody P Schafer  *   in a config string) later on in the term list.
739688d4dfcSCody P Schafer  */
740688d4dfcSCody P Schafer static int pmu_resolve_param_term(struct parse_events_term *term,
741688d4dfcSCody P Schafer 				  struct list_head *head_terms,
742688d4dfcSCody P Schafer 				  __u64 *value)
743688d4dfcSCody P Schafer {
744688d4dfcSCody P Schafer 	struct parse_events_term *t;
745688d4dfcSCody P Schafer 
746688d4dfcSCody P Schafer 	list_for_each_entry(t, head_terms, list) {
747688d4dfcSCody P Schafer 		if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
748688d4dfcSCody P Schafer 			if (!strcmp(t->config, term->config)) {
749688d4dfcSCody P Schafer 				t->used = true;
750688d4dfcSCody P Schafer 				*value = t->val.num;
751688d4dfcSCody P Schafer 				return 0;
752688d4dfcSCody P Schafer 			}
753688d4dfcSCody P Schafer 		}
754688d4dfcSCody P Schafer 	}
755688d4dfcSCody P Schafer 
756bb963e16SNamhyung Kim 	if (verbose > 0)
757688d4dfcSCody P Schafer 		printf("Required parameter '%s' not specified\n", term->config);
758688d4dfcSCody P Schafer 
759688d4dfcSCody P Schafer 	return -1;
760688d4dfcSCody P Schafer }
761688d4dfcSCody P Schafer 
762ffeb883eSHe Kuang static char *pmu_formats_string(struct list_head *formats)
763e64b020bSJiri Olsa {
764e64b020bSJiri Olsa 	struct perf_pmu_format *format;
76511db4e29SMasami Hiramatsu 	char *str = NULL;
76611db4e29SMasami Hiramatsu 	struct strbuf buf = STRBUF_INIT;
767e64b020bSJiri Olsa 	unsigned i = 0;
768e64b020bSJiri Olsa 
769ffeb883eSHe Kuang 	if (!formats)
770e64b020bSJiri Olsa 		return NULL;
771e64b020bSJiri Olsa 
772e64b020bSJiri Olsa 	/* sysfs exported terms */
773ffeb883eSHe Kuang 	list_for_each_entry(format, formats, list)
77411db4e29SMasami Hiramatsu 		if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
77511db4e29SMasami Hiramatsu 			goto error;
776e64b020bSJiri Olsa 
777ffeb883eSHe Kuang 	str = strbuf_detach(&buf, NULL);
77811db4e29SMasami Hiramatsu error:
779ffeb883eSHe Kuang 	strbuf_release(&buf);
780e64b020bSJiri Olsa 
781e64b020bSJiri Olsa 	return str;
782e64b020bSJiri Olsa }
783e64b020bSJiri Olsa 
784688d4dfcSCody P Schafer /*
785cd82a32eSJiri Olsa  * Setup one of config[12] attr members based on the
78688aca8d9SCody P Schafer  * user input data - term parameter.
787cd82a32eSJiri Olsa  */
788cd82a32eSJiri Olsa static int pmu_config_term(struct list_head *formats,
789cd82a32eSJiri Olsa 			   struct perf_event_attr *attr,
790dc0a6202SAdrian Hunter 			   struct parse_events_term *term,
791688d4dfcSCody P Schafer 			   struct list_head *head_terms,
792e64b020bSJiri Olsa 			   bool zero, struct parse_events_error *err)
793cd82a32eSJiri Olsa {
7945c6ccc37SArnaldo Carvalho de Melo 	struct perf_pmu_format *format;
795cd82a32eSJiri Olsa 	__u64 *vp;
7960efe6b67SAdrian Hunter 	__u64 val, max_val;
797cd82a32eSJiri Olsa 
798cd82a32eSJiri Olsa 	/*
799688d4dfcSCody P Schafer 	 * If this is a parameter we've already used for parameterized-eval,
800688d4dfcSCody P Schafer 	 * skip it in normal eval.
801688d4dfcSCody P Schafer 	 */
802688d4dfcSCody P Schafer 	if (term->used)
803688d4dfcSCody P Schafer 		return 0;
804688d4dfcSCody P Schafer 
805688d4dfcSCody P Schafer 	/*
806cd82a32eSJiri Olsa 	 * Hardcoded terms should be already in, so nothing
807cd82a32eSJiri Olsa 	 * to be done for them.
808cd82a32eSJiri Olsa 	 */
809cd82a32eSJiri Olsa 	if (parse_events__is_hardcoded_term(term))
810cd82a32eSJiri Olsa 		return 0;
811cd82a32eSJiri Olsa 
812cd82a32eSJiri Olsa 	format = pmu_find_format(formats, term->config);
813688d4dfcSCody P Schafer 	if (!format) {
814bb963e16SNamhyung Kim 		if (verbose > 0)
815688d4dfcSCody P Schafer 			printf("Invalid event/parameter '%s'\n", term->config);
816e64b020bSJiri Olsa 		if (err) {
817ffeb883eSHe Kuang 			char *pmu_term = pmu_formats_string(formats);
818ffeb883eSHe Kuang 
819e64b020bSJiri Olsa 			err->idx  = term->err_term;
820e64b020bSJiri Olsa 			err->str  = strdup("unknown term");
821ffeb883eSHe Kuang 			err->help = parse_events_formats_error_string(pmu_term);
822ffeb883eSHe Kuang 			free(pmu_term);
823e64b020bSJiri Olsa 		}
824cd82a32eSJiri Olsa 		return -EINVAL;
825688d4dfcSCody P Schafer 	}
826cd82a32eSJiri Olsa 
827cd82a32eSJiri Olsa 	switch (format->value) {
828cd82a32eSJiri Olsa 	case PERF_PMU_FORMAT_VALUE_CONFIG:
829cd82a32eSJiri Olsa 		vp = &attr->config;
830cd82a32eSJiri Olsa 		break;
831cd82a32eSJiri Olsa 	case PERF_PMU_FORMAT_VALUE_CONFIG1:
832cd82a32eSJiri Olsa 		vp = &attr->config1;
833cd82a32eSJiri Olsa 		break;
834cd82a32eSJiri Olsa 	case PERF_PMU_FORMAT_VALUE_CONFIG2:
835cd82a32eSJiri Olsa 		vp = &attr->config2;
836cd82a32eSJiri Olsa 		break;
837cd82a32eSJiri Olsa 	default:
838cd82a32eSJiri Olsa 		return -EINVAL;
839cd82a32eSJiri Olsa 	}
840cd82a32eSJiri Olsa 
84116fa7e82SJiri Olsa 	/*
842688d4dfcSCody P Schafer 	 * Either directly use a numeric term, or try to translate string terms
843688d4dfcSCody P Schafer 	 * using event parameters.
84416fa7e82SJiri Olsa 	 */
84599e7138eSJiri Olsa 	if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
84699e7138eSJiri Olsa 		if (term->no_value &&
84799e7138eSJiri Olsa 		    bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
84899e7138eSJiri Olsa 			if (err) {
84999e7138eSJiri Olsa 				err->idx = term->err_val;
85099e7138eSJiri Olsa 				err->str = strdup("no value assigned for term");
85199e7138eSJiri Olsa 			}
85299e7138eSJiri Olsa 			return -EINVAL;
85399e7138eSJiri Olsa 		}
85499e7138eSJiri Olsa 
855688d4dfcSCody P Schafer 		val = term->val.num;
85699e7138eSJiri Olsa 	} else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
857688d4dfcSCody P Schafer 		if (strcmp(term->val.str, "?")) {
858bb963e16SNamhyung Kim 			if (verbose > 0) {
859688d4dfcSCody P Schafer 				pr_info("Invalid sysfs entry %s=%s\n",
860688d4dfcSCody P Schafer 						term->config, term->val.str);
861e64b020bSJiri Olsa 			}
862e64b020bSJiri Olsa 			if (err) {
863e64b020bSJiri Olsa 				err->idx = term->err_val;
864e64b020bSJiri Olsa 				err->str = strdup("expected numeric value");
865e64b020bSJiri Olsa 			}
866688d4dfcSCody P Schafer 			return -EINVAL;
867688d4dfcSCody P Schafer 		}
868688d4dfcSCody P Schafer 
869688d4dfcSCody P Schafer 		if (pmu_resolve_param_term(term, head_terms, &val))
870688d4dfcSCody P Schafer 			return -EINVAL;
871688d4dfcSCody P Schafer 	} else
872688d4dfcSCody P Schafer 		return -EINVAL;
873688d4dfcSCody P Schafer 
8740efe6b67SAdrian Hunter 	max_val = pmu_format_max_value(format->bits);
8750efe6b67SAdrian Hunter 	if (val > max_val) {
8760efe6b67SAdrian Hunter 		if (err) {
8770efe6b67SAdrian Hunter 			err->idx = term->err_val;
8780efe6b67SAdrian Hunter 			if (asprintf(&err->str,
8790efe6b67SAdrian Hunter 				     "value too big for format, maximum is %llu",
8800efe6b67SAdrian Hunter 				     (unsigned long long)max_val) < 0)
8810efe6b67SAdrian Hunter 				err->str = strdup("value too big for format");
8820efe6b67SAdrian Hunter 			return -EINVAL;
8830efe6b67SAdrian Hunter 		}
8840efe6b67SAdrian Hunter 		/*
8850efe6b67SAdrian Hunter 		 * Assume we don't care if !err, in which case the value will be
8860efe6b67SAdrian Hunter 		 * silently truncated.
8870efe6b67SAdrian Hunter 		 */
8880efe6b67SAdrian Hunter 	}
8890efe6b67SAdrian Hunter 
890688d4dfcSCody P Schafer 	pmu_format_value(format->bits, val, vp, zero);
891cd82a32eSJiri Olsa 	return 0;
892cd82a32eSJiri Olsa }
893cd82a32eSJiri Olsa 
894cff7f956SJiri Olsa int perf_pmu__config_terms(struct list_head *formats,
895cff7f956SJiri Olsa 			   struct perf_event_attr *attr,
896dc0a6202SAdrian Hunter 			   struct list_head *head_terms,
897e64b020bSJiri Olsa 			   bool zero, struct parse_events_error *err)
898cd82a32eSJiri Olsa {
8996cee6cd3SArnaldo Carvalho de Melo 	struct parse_events_term *term;
900cd82a32eSJiri Olsa 
901688d4dfcSCody P Schafer 	list_for_each_entry(term, head_terms, list) {
902e64b020bSJiri Olsa 		if (pmu_config_term(formats, attr, term, head_terms,
903e64b020bSJiri Olsa 				    zero, err))
904cd82a32eSJiri Olsa 			return -EINVAL;
905688d4dfcSCody P Schafer 	}
906cd82a32eSJiri Olsa 
907cd82a32eSJiri Olsa 	return 0;
908cd82a32eSJiri Olsa }
909cd82a32eSJiri Olsa 
910cd82a32eSJiri Olsa /*
911cd82a32eSJiri Olsa  * Configures event's 'attr' parameter based on the:
912cd82a32eSJiri Olsa  * 1) users input - specified in terms parameter
913cd82a32eSJiri Olsa  * 2) pmu format definitions - specified by pmu parameter
914cd82a32eSJiri Olsa  */
915cd82a32eSJiri Olsa int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
916e64b020bSJiri Olsa 		     struct list_head *head_terms,
917e64b020bSJiri Olsa 		     struct parse_events_error *err)
918cd82a32eSJiri Olsa {
919dc0a6202SAdrian Hunter 	bool zero = !!pmu->default_config;
920dc0a6202SAdrian Hunter 
921cd82a32eSJiri Olsa 	attr->type = pmu->type;
922e64b020bSJiri Olsa 	return perf_pmu__config_terms(&pmu->format, attr, head_terms,
923e64b020bSJiri Olsa 				      zero, err);
924cd82a32eSJiri Olsa }
925cd82a32eSJiri Olsa 
9265c6ccc37SArnaldo Carvalho de Melo static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
9276cee6cd3SArnaldo Carvalho de Melo 					     struct parse_events_term *term)
928a6146d50SZheng Yan {
9295c6ccc37SArnaldo Carvalho de Melo 	struct perf_pmu_alias *alias;
930a6146d50SZheng Yan 	char *name;
931a6146d50SZheng Yan 
932a6146d50SZheng Yan 	if (parse_events__is_hardcoded_term(term))
933a6146d50SZheng Yan 		return NULL;
934a6146d50SZheng Yan 
935a6146d50SZheng Yan 	if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
936a6146d50SZheng Yan 		if (term->val.num != 1)
937a6146d50SZheng Yan 			return NULL;
938a6146d50SZheng Yan 		if (pmu_find_format(&pmu->format, term->config))
939a6146d50SZheng Yan 			return NULL;
940a6146d50SZheng Yan 		name = term->config;
941a6146d50SZheng Yan 	} else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
942a6146d50SZheng Yan 		if (strcasecmp(term->config, "event"))
943a6146d50SZheng Yan 			return NULL;
944a6146d50SZheng Yan 		name = term->val.str;
945a6146d50SZheng Yan 	} else {
946a6146d50SZheng Yan 		return NULL;
947a6146d50SZheng Yan 	}
948a6146d50SZheng Yan 
949a6146d50SZheng Yan 	list_for_each_entry(alias, &pmu->aliases, list) {
950a6146d50SZheng Yan 		if (!strcasecmp(alias->name, name))
951a6146d50SZheng Yan 			return alias;
952a6146d50SZheng Yan 	}
953a6146d50SZheng Yan 	return NULL;
954a6146d50SZheng Yan }
955a6146d50SZheng Yan 
956410136f5SStephane Eranian 
9571d9e446bSJiri Olsa static int check_info_data(struct perf_pmu_alias *alias,
9581d9e446bSJiri Olsa 			   struct perf_pmu_info *info)
959410136f5SStephane Eranian {
960410136f5SStephane Eranian 	/*
961410136f5SStephane Eranian 	 * Only one term in event definition can
9621d9e446bSJiri Olsa 	 * define unit, scale and snapshot, fail
9631d9e446bSJiri Olsa 	 * if there's more than one.
964410136f5SStephane Eranian 	 */
965b30a7d1fSArnaldo Carvalho de Melo 	if ((info->unit && alias->unit[0]) ||
9661d9e446bSJiri Olsa 	    (info->scale && alias->scale) ||
9671d9e446bSJiri Olsa 	    (info->snapshot && alias->snapshot))
968410136f5SStephane Eranian 		return -EINVAL;
969410136f5SStephane Eranian 
970b30a7d1fSArnaldo Carvalho de Melo 	if (alias->unit[0])
9711d9e446bSJiri Olsa 		info->unit = alias->unit;
972410136f5SStephane Eranian 
973410136f5SStephane Eranian 	if (alias->scale)
9741d9e446bSJiri Olsa 		info->scale = alias->scale;
9751d9e446bSJiri Olsa 
9761d9e446bSJiri Olsa 	if (alias->snapshot)
9771d9e446bSJiri Olsa 		info->snapshot = alias->snapshot;
978410136f5SStephane Eranian 
979410136f5SStephane Eranian 	return 0;
980410136f5SStephane Eranian }
981410136f5SStephane Eranian 
982a6146d50SZheng Yan /*
983a6146d50SZheng Yan  * Find alias in the terms list and replace it with the terms
984a6146d50SZheng Yan  * defined for the alias
985a6146d50SZheng Yan  */
986410136f5SStephane Eranian int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
98746441bdcSMatt Fleming 			  struct perf_pmu_info *info)
988a6146d50SZheng Yan {
9896cee6cd3SArnaldo Carvalho de Melo 	struct parse_events_term *term, *h;
9905c6ccc37SArnaldo Carvalho de Melo 	struct perf_pmu_alias *alias;
991a6146d50SZheng Yan 	int ret;
992a6146d50SZheng Yan 
993044330c1SMatt Fleming 	info->per_pkg = false;
994044330c1SMatt Fleming 
9958a398897SStephane Eranian 	/*
9968a398897SStephane Eranian 	 * Mark unit and scale as not set
9978a398897SStephane Eranian 	 * (different from default values, see below)
9988a398897SStephane Eranian 	 */
99946441bdcSMatt Fleming 	info->unit     = NULL;
100046441bdcSMatt Fleming 	info->scale    = 0.0;
10011d9e446bSJiri Olsa 	info->snapshot = false;
100237932c18SAndi Kleen 	info->metric_expr = NULL;
100396284814SAndi Kleen 	info->metric_name = NULL;
1004410136f5SStephane Eranian 
1005a6146d50SZheng Yan 	list_for_each_entry_safe(term, h, head_terms, list) {
1006a6146d50SZheng Yan 		alias = pmu_find_alias(pmu, term);
1007a6146d50SZheng Yan 		if (!alias)
1008a6146d50SZheng Yan 			continue;
1009a6146d50SZheng Yan 		ret = pmu_alias_terms(alias, &term->list);
1010a6146d50SZheng Yan 		if (ret)
1011a6146d50SZheng Yan 			return ret;
1012410136f5SStephane Eranian 
10131d9e446bSJiri Olsa 		ret = check_info_data(alias, info);
1014410136f5SStephane Eranian 		if (ret)
1015410136f5SStephane Eranian 			return ret;
1016410136f5SStephane Eranian 
1017044330c1SMatt Fleming 		if (alias->per_pkg)
1018044330c1SMatt Fleming 			info->per_pkg = true;
101937932c18SAndi Kleen 		info->metric_expr = alias->metric_expr;
102096284814SAndi Kleen 		info->metric_name = alias->metric_name;
1021044330c1SMatt Fleming 
1022a6146d50SZheng Yan 		list_del(&term->list);
1023a6146d50SZheng Yan 		free(term);
1024a6146d50SZheng Yan 	}
10258a398897SStephane Eranian 
10268a398897SStephane Eranian 	/*
10278a398897SStephane Eranian 	 * if no unit or scale foundin aliases, then
10288a398897SStephane Eranian 	 * set defaults as for evsel
10298a398897SStephane Eranian 	 * unit cannot left to NULL
10308a398897SStephane Eranian 	 */
103146441bdcSMatt Fleming 	if (info->unit == NULL)
103246441bdcSMatt Fleming 		info->unit   = "";
10338a398897SStephane Eranian 
103446441bdcSMatt Fleming 	if (info->scale == 0.0)
103546441bdcSMatt Fleming 		info->scale  = 1.0;
10368a398897SStephane Eranian 
1037a6146d50SZheng Yan 	return 0;
1038a6146d50SZheng Yan }
1039a6146d50SZheng Yan 
1040cd82a32eSJiri Olsa int perf_pmu__new_format(struct list_head *list, char *name,
1041cd82a32eSJiri Olsa 			 int config, unsigned long *bits)
1042cd82a32eSJiri Olsa {
10435c6ccc37SArnaldo Carvalho de Melo 	struct perf_pmu_format *format;
1044cd82a32eSJiri Olsa 
1045cd82a32eSJiri Olsa 	format = zalloc(sizeof(*format));
1046cd82a32eSJiri Olsa 	if (!format)
1047cd82a32eSJiri Olsa 		return -ENOMEM;
1048cd82a32eSJiri Olsa 
1049cd82a32eSJiri Olsa 	format->name = strdup(name);
1050cd82a32eSJiri Olsa 	format->value = config;
1051cd82a32eSJiri Olsa 	memcpy(format->bits, bits, sizeof(format->bits));
1052cd82a32eSJiri Olsa 
1053cd82a32eSJiri Olsa 	list_add_tail(&format->list, list);
1054cd82a32eSJiri Olsa 	return 0;
1055cd82a32eSJiri Olsa }
1056cd82a32eSJiri Olsa 
1057cd82a32eSJiri Olsa void perf_pmu__set_format(unsigned long *bits, long from, long to)
1058cd82a32eSJiri Olsa {
1059cd82a32eSJiri Olsa 	long b;
1060cd82a32eSJiri Olsa 
1061cd82a32eSJiri Olsa 	if (!to)
1062cd82a32eSJiri Olsa 		to = from;
1063cd82a32eSJiri Olsa 
106415268138SSukadev Bhattiprolu 	memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
1065cd82a32eSJiri Olsa 	for (b = from; b <= to; b++)
1066cd82a32eSJiri Olsa 		set_bit(b, bits);
1067cd82a32eSJiri Olsa }
1068dc098b35SAndi Kleen 
1069aaea3617SCody P Schafer static int sub_non_neg(int a, int b)
1070aaea3617SCody P Schafer {
1071aaea3617SCody P Schafer 	if (b > a)
1072aaea3617SCody P Schafer 		return 0;
1073aaea3617SCody P Schafer 	return a - b;
1074aaea3617SCody P Schafer }
1075aaea3617SCody P Schafer 
1076dc098b35SAndi Kleen static char *format_alias(char *buf, int len, struct perf_pmu *pmu,
1077dc098b35SAndi Kleen 			  struct perf_pmu_alias *alias)
1078dc098b35SAndi Kleen {
1079aaea3617SCody P Schafer 	struct parse_events_term *term;
1080aaea3617SCody P Schafer 	int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
1081aaea3617SCody P Schafer 
1082aaea3617SCody P Schafer 	list_for_each_entry(term, &alias->terms, list) {
1083aaea3617SCody P Schafer 		if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1084aaea3617SCody P Schafer 			used += snprintf(buf + used, sub_non_neg(len, used),
1085aaea3617SCody P Schafer 					",%s=%s", term->config,
1086aaea3617SCody P Schafer 					term->val.str);
1087aaea3617SCody P Schafer 	}
1088aaea3617SCody P Schafer 
1089aaea3617SCody P Schafer 	if (sub_non_neg(len, used) > 0) {
1090aaea3617SCody P Schafer 		buf[used] = '/';
1091aaea3617SCody P Schafer 		used++;
1092aaea3617SCody P Schafer 	}
1093aaea3617SCody P Schafer 	if (sub_non_neg(len, used) > 0) {
1094aaea3617SCody P Schafer 		buf[used] = '\0';
1095aaea3617SCody P Schafer 		used++;
1096aaea3617SCody P Schafer 	} else
1097aaea3617SCody P Schafer 		buf[len - 1] = '\0';
1098aaea3617SCody P Schafer 
1099dc098b35SAndi Kleen 	return buf;
1100dc098b35SAndi Kleen }
1101dc098b35SAndi Kleen 
1102dc098b35SAndi Kleen static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu,
1103dc098b35SAndi Kleen 			     struct perf_pmu_alias *alias)
1104dc098b35SAndi Kleen {
1105dc098b35SAndi Kleen 	snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name);
1106dc098b35SAndi Kleen 	return buf;
1107dc098b35SAndi Kleen }
1108dc098b35SAndi Kleen 
1109dd5f1036SAndi Kleen struct sevent {
111008e60ed1SAndi Kleen 	char *name;
111108e60ed1SAndi Kleen 	char *desc;
1112dd5f1036SAndi Kleen 	char *topic;
1113f2361024SAndi Kleen 	char *str;
1114f2361024SAndi Kleen 	char *pmu;
11157f372a63SAndi Kleen 	char *metric_expr;
111696284814SAndi Kleen 	char *metric_name;
111708e60ed1SAndi Kleen };
111808e60ed1SAndi Kleen 
1119dd5f1036SAndi Kleen static int cmp_sevent(const void *a, const void *b)
1120dc098b35SAndi Kleen {
1121dd5f1036SAndi Kleen 	const struct sevent *as = a;
1122dd5f1036SAndi Kleen 	const struct sevent *bs = b;
112308e60ed1SAndi Kleen 
112408e60ed1SAndi Kleen 	/* Put extra events last */
112508e60ed1SAndi Kleen 	if (!!as->desc != !!bs->desc)
112608e60ed1SAndi Kleen 		return !!as->desc - !!bs->desc;
1127dd5f1036SAndi Kleen 	if (as->topic && bs->topic) {
1128dd5f1036SAndi Kleen 		int n = strcmp(as->topic, bs->topic);
1129dd5f1036SAndi Kleen 
1130dd5f1036SAndi Kleen 		if (n)
1131dd5f1036SAndi Kleen 			return n;
1132dd5f1036SAndi Kleen 	}
113308e60ed1SAndi Kleen 	return strcmp(as->name, bs->name);
113408e60ed1SAndi Kleen }
113508e60ed1SAndi Kleen 
113608e60ed1SAndi Kleen static void wordwrap(char *s, int start, int max, int corr)
113708e60ed1SAndi Kleen {
113808e60ed1SAndi Kleen 	int column = start;
113908e60ed1SAndi Kleen 	int n;
114008e60ed1SAndi Kleen 
114108e60ed1SAndi Kleen 	while (*s) {
114208e60ed1SAndi Kleen 		int wlen = strcspn(s, " \t");
114308e60ed1SAndi Kleen 
114408e60ed1SAndi Kleen 		if (column + wlen >= max && column > start) {
114508e60ed1SAndi Kleen 			printf("\n%*s", start, "");
114608e60ed1SAndi Kleen 			column = start + corr;
114708e60ed1SAndi Kleen 		}
114808e60ed1SAndi Kleen 		n = printf("%s%.*s", column > start ? " " : "", wlen, s);
114908e60ed1SAndi Kleen 		if (n <= 0)
115008e60ed1SAndi Kleen 			break;
115108e60ed1SAndi Kleen 		s += wlen;
115208e60ed1SAndi Kleen 		column += n;
1153aa4beb10STaeung Song 		s = ltrim(s);
115408e60ed1SAndi Kleen 	}
1155dc098b35SAndi Kleen }
1156dc098b35SAndi Kleen 
1157c8d6828aSSukadev Bhattiprolu void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag,
1158bf874fcfSAndi Kleen 			bool long_desc, bool details_flag)
1159dc098b35SAndi Kleen {
1160dc098b35SAndi Kleen 	struct perf_pmu *pmu;
1161dc098b35SAndi Kleen 	struct perf_pmu_alias *alias;
1162dc098b35SAndi Kleen 	char buf[1024];
1163dc098b35SAndi Kleen 	int printed = 0;
1164dc098b35SAndi Kleen 	int len, j;
1165dd5f1036SAndi Kleen 	struct sevent *aliases;
116608e60ed1SAndi Kleen 	int numdesc = 0;
116761eb2eb4SAndi Kleen 	int columns = pager_get_columns();
1168dd5f1036SAndi Kleen 	char *topic = NULL;
1169dc098b35SAndi Kleen 
1170dc098b35SAndi Kleen 	pmu = NULL;
1171dc098b35SAndi Kleen 	len = 0;
117242634bc7SAdrian Hunter 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1173dc098b35SAndi Kleen 		list_for_each_entry(alias, &pmu->aliases, list)
1174dc098b35SAndi Kleen 			len++;
117542634bc7SAdrian Hunter 		if (pmu->selectable)
117642634bc7SAdrian Hunter 			len++;
117742634bc7SAdrian Hunter 	}
1178dd5f1036SAndi Kleen 	aliases = zalloc(sizeof(struct sevent) * len);
1179dc098b35SAndi Kleen 	if (!aliases)
11807e4772dcSArnaldo Carvalho de Melo 		goto out_enomem;
1181dc098b35SAndi Kleen 	pmu = NULL;
1182dc098b35SAndi Kleen 	j = 0;
118342634bc7SAdrian Hunter 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1184dc098b35SAndi Kleen 		list_for_each_entry(alias, &pmu->aliases, list) {
118508e60ed1SAndi Kleen 			char *name = alias->desc ? alias->name :
118608e60ed1SAndi Kleen 				format_alias(buf, sizeof(buf), pmu, alias);
1187dc098b35SAndi Kleen 			bool is_cpu = !strcmp(pmu->name, "cpu");
1188dc098b35SAndi Kleen 
1189dc098b35SAndi Kleen 			if (event_glob != NULL &&
119038d14f0cSAndi Kleen 			    !(strglobmatch_nocase(name, event_glob) ||
119138d14f0cSAndi Kleen 			      (!is_cpu && strglobmatch_nocase(alias->name,
119267bdc35fSAndi Kleen 						       event_glob)) ||
119367bdc35fSAndi Kleen 			      (alias->topic &&
119467bdc35fSAndi Kleen 			       strglobmatch_nocase(alias->topic, event_glob))))
1195dc098b35SAndi Kleen 				continue;
11967e4772dcSArnaldo Carvalho de Melo 
119708e60ed1SAndi Kleen 			if (is_cpu && !name_only && !alias->desc)
11987e4772dcSArnaldo Carvalho de Melo 				name = format_alias_or(buf, sizeof(buf), pmu, alias);
11997e4772dcSArnaldo Carvalho de Melo 
120008e60ed1SAndi Kleen 			aliases[j].name = name;
120108e60ed1SAndi Kleen 			if (is_cpu && !name_only && !alias->desc)
120208e60ed1SAndi Kleen 				aliases[j].name = format_alias_or(buf,
120308e60ed1SAndi Kleen 								  sizeof(buf),
120408e60ed1SAndi Kleen 								  pmu, alias);
120508e60ed1SAndi Kleen 			aliases[j].name = strdup(aliases[j].name);
120608e60ed1SAndi Kleen 			if (!aliases[j].name)
12077e4772dcSArnaldo Carvalho de Melo 				goto out_enomem;
120808e60ed1SAndi Kleen 
1209c8d6828aSSukadev Bhattiprolu 			aliases[j].desc = long_desc ? alias->long_desc :
1210c8d6828aSSukadev Bhattiprolu 						alias->desc;
1211dd5f1036SAndi Kleen 			aliases[j].topic = alias->topic;
1212f2361024SAndi Kleen 			aliases[j].str = alias->str;
1213f2361024SAndi Kleen 			aliases[j].pmu = pmu->name;
12147f372a63SAndi Kleen 			aliases[j].metric_expr = alias->metric_expr;
121596284814SAndi Kleen 			aliases[j].metric_name = alias->metric_name;
1216dc098b35SAndi Kleen 			j++;
1217dc098b35SAndi Kleen 		}
1218fa52ceabSArnaldo Carvalho de Melo 		if (pmu->selectable &&
1219fa52ceabSArnaldo Carvalho de Melo 		    (event_glob == NULL || strglobmatch(pmu->name, event_glob))) {
12207e4772dcSArnaldo Carvalho de Melo 			char *s;
12217e4772dcSArnaldo Carvalho de Melo 			if (asprintf(&s, "%s//", pmu->name) < 0)
12227e4772dcSArnaldo Carvalho de Melo 				goto out_enomem;
122308e60ed1SAndi Kleen 			aliases[j].name = s;
122442634bc7SAdrian Hunter 			j++;
122542634bc7SAdrian Hunter 		}
122642634bc7SAdrian Hunter 	}
1227dc098b35SAndi Kleen 	len = j;
1228dd5f1036SAndi Kleen 	qsort(aliases, len, sizeof(struct sevent), cmp_sevent);
1229dc098b35SAndi Kleen 	for (j = 0; j < len; j++) {
123015b22ed3SAndi Kleen 		/* Skip duplicates */
123115b22ed3SAndi Kleen 		if (j > 0 && !strcmp(aliases[j].name, aliases[j - 1].name))
123215b22ed3SAndi Kleen 			continue;
1233dc098b35SAndi Kleen 		if (name_only) {
123408e60ed1SAndi Kleen 			printf("%s ", aliases[j].name);
1235dc098b35SAndi Kleen 			continue;
1236dc098b35SAndi Kleen 		}
12371c5f01feSAndi Kleen 		if (aliases[j].desc && !quiet_flag) {
123808e60ed1SAndi Kleen 			if (numdesc++ == 0)
123908e60ed1SAndi Kleen 				printf("\n");
1240dd5f1036SAndi Kleen 			if (aliases[j].topic && (!topic ||
1241dd5f1036SAndi Kleen 					strcmp(topic, aliases[j].topic))) {
1242dd5f1036SAndi Kleen 				printf("%s%s:\n", topic ? "\n" : "",
1243dd5f1036SAndi Kleen 						aliases[j].topic);
1244dd5f1036SAndi Kleen 				topic = aliases[j].topic;
1245dd5f1036SAndi Kleen 			}
124608e60ed1SAndi Kleen 			printf("  %-50s\n", aliases[j].name);
124708e60ed1SAndi Kleen 			printf("%*s", 8, "[");
124808e60ed1SAndi Kleen 			wordwrap(aliases[j].desc, 8, columns, 0);
124908e60ed1SAndi Kleen 			printf("]\n");
1250bf874fcfSAndi Kleen 			if (details_flag) {
12517f372a63SAndi Kleen 				printf("%*s%s/%s/ ", 8, "", aliases[j].pmu, aliases[j].str);
125296284814SAndi Kleen 				if (aliases[j].metric_name)
125396284814SAndi Kleen 					printf(" MetricName: %s", aliases[j].metric_name);
12547f372a63SAndi Kleen 				if (aliases[j].metric_expr)
12557f372a63SAndi Kleen 					printf(" MetricExpr: %s", aliases[j].metric_expr);
12567f372a63SAndi Kleen 				putchar('\n');
12577f372a63SAndi Kleen 			}
125808e60ed1SAndi Kleen 		} else
125908e60ed1SAndi Kleen 			printf("  %-50s [Kernel PMU event]\n", aliases[j].name);
1260dc098b35SAndi Kleen 		printed++;
1261dc098b35SAndi Kleen 	}
1262dfc431cbSArnaldo Carvalho de Melo 	if (printed && pager_in_use())
1263dc098b35SAndi Kleen 		printf("\n");
12647e4772dcSArnaldo Carvalho de Melo out_free:
12657e4772dcSArnaldo Carvalho de Melo 	for (j = 0; j < len; j++)
126608e60ed1SAndi Kleen 		zfree(&aliases[j].name);
12677e4772dcSArnaldo Carvalho de Melo 	zfree(&aliases);
12687e4772dcSArnaldo Carvalho de Melo 	return;
12697e4772dcSArnaldo Carvalho de Melo 
12707e4772dcSArnaldo Carvalho de Melo out_enomem:
12717e4772dcSArnaldo Carvalho de Melo 	printf("FATAL: not enough memory to print PMU events\n");
12727e4772dcSArnaldo Carvalho de Melo 	if (aliases)
12737e4772dcSArnaldo Carvalho de Melo 		goto out_free;
1274dc098b35SAndi Kleen }
12754cabc3d1SAndi Kleen 
12764cabc3d1SAndi Kleen bool pmu_have_event(const char *pname, const char *name)
12774cabc3d1SAndi Kleen {
12784cabc3d1SAndi Kleen 	struct perf_pmu *pmu;
12794cabc3d1SAndi Kleen 	struct perf_pmu_alias *alias;
12804cabc3d1SAndi Kleen 
12814cabc3d1SAndi Kleen 	pmu = NULL;
12824cabc3d1SAndi Kleen 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
12834cabc3d1SAndi Kleen 		if (strcmp(pname, pmu->name))
12844cabc3d1SAndi Kleen 			continue;
12854cabc3d1SAndi Kleen 		list_for_each_entry(alias, &pmu->aliases, list)
12864cabc3d1SAndi Kleen 			if (!strcmp(alias->name, name))
12874cabc3d1SAndi Kleen 				return true;
12884cabc3d1SAndi Kleen 	}
12894cabc3d1SAndi Kleen 	return false;
12904cabc3d1SAndi Kleen }
12917d4bdab5SAdrian Hunter 
12927d4bdab5SAdrian Hunter static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
12937d4bdab5SAdrian Hunter {
12947d4bdab5SAdrian Hunter 	struct stat st;
12957d4bdab5SAdrian Hunter 	char path[PATH_MAX];
12967d4bdab5SAdrian Hunter 	const char *sysfs;
12977d4bdab5SAdrian Hunter 
12987d4bdab5SAdrian Hunter 	sysfs = sysfs__mountpoint();
12997d4bdab5SAdrian Hunter 	if (!sysfs)
13007d4bdab5SAdrian Hunter 		return NULL;
13017d4bdab5SAdrian Hunter 
13027d4bdab5SAdrian Hunter 	snprintf(path, PATH_MAX,
13037d4bdab5SAdrian Hunter 		 "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name);
13047d4bdab5SAdrian Hunter 
13057d4bdab5SAdrian Hunter 	if (stat(path, &st) < 0)
13067d4bdab5SAdrian Hunter 		return NULL;
13077d4bdab5SAdrian Hunter 
13087d4bdab5SAdrian Hunter 	return fopen(path, "r");
13097d4bdab5SAdrian Hunter }
13107d4bdab5SAdrian Hunter 
13117d4bdab5SAdrian Hunter int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
13127d4bdab5SAdrian Hunter 			...)
13137d4bdab5SAdrian Hunter {
13147d4bdab5SAdrian Hunter 	va_list args;
13157d4bdab5SAdrian Hunter 	FILE *file;
13167d4bdab5SAdrian Hunter 	int ret = EOF;
13177d4bdab5SAdrian Hunter 
13187d4bdab5SAdrian Hunter 	va_start(args, fmt);
13197d4bdab5SAdrian Hunter 	file = perf_pmu__open_file(pmu, name);
13207d4bdab5SAdrian Hunter 	if (file) {
13217d4bdab5SAdrian Hunter 		ret = vfscanf(file, fmt, args);
13227d4bdab5SAdrian Hunter 		fclose(file);
13237d4bdab5SAdrian Hunter 	}
13247d4bdab5SAdrian Hunter 	va_end(args);
13257d4bdab5SAdrian Hunter 	return ret;
13267d4bdab5SAdrian Hunter }
1327