xref: /openbmc/linux/tools/perf/util/pmu.c (revision e56fbc9d)
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>
6cd82a32eSJiri Olsa #include <sys/types.h>
7a43783aeSArnaldo Carvalho de Melo #include <errno.h>
8c23c2a0fSArnaldo Carvalho de Melo #include <fcntl.h>
97a8ef4c4SArnaldo Carvalho de Melo #include <sys/stat.h>
10cd82a32eSJiri Olsa #include <unistd.h>
11cd82a32eSJiri Olsa #include <stdio.h>
12dc0a6202SAdrian Hunter #include <stdbool.h>
137d4bdab5SAdrian Hunter #include <stdarg.h>
14cd82a32eSJiri Olsa #include <dirent.h>
15cd0cfad7SBorislav Petkov #include <api/fs/fs.h>
16410136f5SStephane Eranian #include <locale.h>
17fbc2844eSWilliam Cohen #include <regex.h>
18cd82a32eSJiri Olsa #include "pmu.h"
19cd82a32eSJiri Olsa #include "parse-events.h"
207ae92e74SYan, Zheng #include "cpumap.h"
21933f82ffSSukadev Bhattiprolu #include "header.h"
22933f82ffSSukadev Bhattiprolu #include "pmu-events/pmu-events.h"
2361eb2eb4SAndi Kleen #include "cache.h"
24a067558eSArnaldo Carvalho de Melo #include "string2.h"
25cd82a32eSJiri Olsa 
26ab1bf653SArnaldo Carvalho de Melo struct perf_pmu_format {
27ab1bf653SArnaldo Carvalho de Melo 	char *name;
28ab1bf653SArnaldo Carvalho de Melo 	int value;
29ab1bf653SArnaldo Carvalho de Melo 	DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
30ab1bf653SArnaldo Carvalho de Melo 	struct list_head list;
31ab1bf653SArnaldo Carvalho de Melo };
32ab1bf653SArnaldo Carvalho de Melo 
33cd82a32eSJiri Olsa int perf_pmu_parse(struct list_head *list, char *name);
34cd82a32eSJiri Olsa extern FILE *perf_pmu_in;
35cd82a32eSJiri Olsa 
36cd82a32eSJiri Olsa static LIST_HEAD(pmus);
37cd82a32eSJiri Olsa 
38cd82a32eSJiri Olsa /*
39cd82a32eSJiri Olsa  * Parse & process all the sysfs attributes located under
40cd82a32eSJiri Olsa  * the directory specified in 'dir' parameter.
41cd82a32eSJiri Olsa  */
42cff7f956SJiri Olsa int perf_pmu__format_parse(char *dir, struct list_head *head)
43cd82a32eSJiri Olsa {
44cd82a32eSJiri Olsa 	struct dirent *evt_ent;
45cd82a32eSJiri Olsa 	DIR *format_dir;
46cd82a32eSJiri Olsa 	int ret = 0;
47cd82a32eSJiri Olsa 
48cd82a32eSJiri Olsa 	format_dir = opendir(dir);
49cd82a32eSJiri Olsa 	if (!format_dir)
50cd82a32eSJiri Olsa 		return -EINVAL;
51cd82a32eSJiri Olsa 
52cd82a32eSJiri Olsa 	while (!ret && (evt_ent = readdir(format_dir))) {
53cd82a32eSJiri Olsa 		char path[PATH_MAX];
54cd82a32eSJiri Olsa 		char *name = evt_ent->d_name;
55cd82a32eSJiri Olsa 		FILE *file;
56cd82a32eSJiri Olsa 
57cd82a32eSJiri Olsa 		if (!strcmp(name, ".") || !strcmp(name, ".."))
58cd82a32eSJiri Olsa 			continue;
59cd82a32eSJiri Olsa 
60cd82a32eSJiri Olsa 		snprintf(path, PATH_MAX, "%s/%s", dir, name);
61cd82a32eSJiri Olsa 
62cd82a32eSJiri Olsa 		ret = -EINVAL;
63cd82a32eSJiri Olsa 		file = fopen(path, "r");
64cd82a32eSJiri Olsa 		if (!file)
65cd82a32eSJiri Olsa 			break;
66cd82a32eSJiri Olsa 
67cd82a32eSJiri Olsa 		perf_pmu_in = file;
68cd82a32eSJiri Olsa 		ret = perf_pmu_parse(head, name);
69cd82a32eSJiri Olsa 		fclose(file);
70cd82a32eSJiri Olsa 	}
71cd82a32eSJiri Olsa 
72cd82a32eSJiri Olsa 	closedir(format_dir);
73cd82a32eSJiri Olsa 	return ret;
74cd82a32eSJiri Olsa }
75cd82a32eSJiri Olsa 
76cd82a32eSJiri Olsa /*
77cd82a32eSJiri Olsa  * Reading/parsing the default pmu format definition, which should be
78cd82a32eSJiri Olsa  * located at:
79cd82a32eSJiri Olsa  * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
80cd82a32eSJiri Olsa  */
81b6b96fb4SAdrian Hunter static int pmu_format(const char *name, struct list_head *format)
82cd82a32eSJiri Olsa {
83cd82a32eSJiri Olsa 	struct stat st;
84cd82a32eSJiri Olsa 	char path[PATH_MAX];
85cf38fadaSArnaldo Carvalho de Melo 	const char *sysfs = sysfs__mountpoint();
86cd82a32eSJiri Olsa 
87cd82a32eSJiri Olsa 	if (!sysfs)
88cd82a32eSJiri Olsa 		return -1;
89cd82a32eSJiri Olsa 
90cd82a32eSJiri Olsa 	snprintf(path, PATH_MAX,
9150a9667cSRobert Richter 		 "%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name);
92cd82a32eSJiri Olsa 
93cd82a32eSJiri Olsa 	if (stat(path, &st) < 0)
949bc8f9feSRobert Richter 		return 0;	/* no error if format does not exist */
95cd82a32eSJiri Olsa 
96cff7f956SJiri Olsa 	if (perf_pmu__format_parse(path, format))
97cd82a32eSJiri Olsa 		return -1;
98cd82a32eSJiri Olsa 
99cd82a32eSJiri Olsa 	return 0;
100cd82a32eSJiri Olsa }
101cd82a32eSJiri Olsa 
102d02fc6bcSAndi Kleen static int convert_scale(const char *scale, char **end, double *sval)
103d02fc6bcSAndi Kleen {
104d02fc6bcSAndi Kleen 	char *lc;
105d02fc6bcSAndi Kleen 	int ret = 0;
106d02fc6bcSAndi Kleen 
107d02fc6bcSAndi Kleen 	/*
108d02fc6bcSAndi Kleen 	 * save current locale
109d02fc6bcSAndi Kleen 	 */
110d02fc6bcSAndi Kleen 	lc = setlocale(LC_NUMERIC, NULL);
111d02fc6bcSAndi Kleen 
112d02fc6bcSAndi Kleen 	/*
113d02fc6bcSAndi Kleen 	 * The lc string may be allocated in static storage,
114d02fc6bcSAndi Kleen 	 * so get a dynamic copy to make it survive setlocale
115d02fc6bcSAndi Kleen 	 * call below.
116d02fc6bcSAndi Kleen 	 */
117d02fc6bcSAndi Kleen 	lc = strdup(lc);
118d02fc6bcSAndi Kleen 	if (!lc) {
119d02fc6bcSAndi Kleen 		ret = -ENOMEM;
120d02fc6bcSAndi Kleen 		goto out;
121d02fc6bcSAndi Kleen 	}
122d02fc6bcSAndi Kleen 
123d02fc6bcSAndi Kleen 	/*
124d02fc6bcSAndi Kleen 	 * force to C locale to ensure kernel
125d02fc6bcSAndi Kleen 	 * scale string is converted correctly.
126d02fc6bcSAndi Kleen 	 * kernel uses default C locale.
127d02fc6bcSAndi Kleen 	 */
128d02fc6bcSAndi Kleen 	setlocale(LC_NUMERIC, "C");
129d02fc6bcSAndi Kleen 
130d02fc6bcSAndi Kleen 	*sval = strtod(scale, end);
131d02fc6bcSAndi Kleen 
132d02fc6bcSAndi Kleen out:
133d02fc6bcSAndi Kleen 	/* restore locale */
134d02fc6bcSAndi Kleen 	setlocale(LC_NUMERIC, lc);
135d02fc6bcSAndi Kleen 	free(lc);
136d02fc6bcSAndi Kleen 	return ret;
137d02fc6bcSAndi Kleen }
138d02fc6bcSAndi Kleen 
139410136f5SStephane Eranian static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name)
140410136f5SStephane Eranian {
141410136f5SStephane Eranian 	struct stat st;
142410136f5SStephane Eranian 	ssize_t sret;
143410136f5SStephane Eranian 	char scale[128];
144410136f5SStephane Eranian 	int fd, ret = -1;
145410136f5SStephane Eranian 	char path[PATH_MAX];
146410136f5SStephane Eranian 
14711a64a05SBen Hutchings 	scnprintf(path, PATH_MAX, "%s/%s.scale", dir, name);
148410136f5SStephane Eranian 
149410136f5SStephane Eranian 	fd = open(path, O_RDONLY);
150410136f5SStephane Eranian 	if (fd == -1)
151410136f5SStephane Eranian 		return -1;
152410136f5SStephane Eranian 
153410136f5SStephane Eranian 	if (fstat(fd, &st) < 0)
154410136f5SStephane Eranian 		goto error;
155410136f5SStephane Eranian 
156410136f5SStephane Eranian 	sret = read(fd, scale, sizeof(scale)-1);
157410136f5SStephane Eranian 	if (sret < 0)
158410136f5SStephane Eranian 		goto error;
159410136f5SStephane Eranian 
1609ecae065SMadhavan Srinivasan 	if (scale[sret - 1] == '\n')
1619ecae065SMadhavan Srinivasan 		scale[sret - 1] = '\0';
1629ecae065SMadhavan Srinivasan 	else
163410136f5SStephane Eranian 		scale[sret] = '\0';
1649ecae065SMadhavan Srinivasan 
165d02fc6bcSAndi Kleen 	ret = convert_scale(scale, NULL, &alias->scale);
166410136f5SStephane Eranian error:
167410136f5SStephane Eranian 	close(fd);
168410136f5SStephane Eranian 	return ret;
169410136f5SStephane Eranian }
170410136f5SStephane Eranian 
171410136f5SStephane Eranian static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name)
172410136f5SStephane Eranian {
173410136f5SStephane Eranian 	char path[PATH_MAX];
174410136f5SStephane Eranian 	ssize_t sret;
175410136f5SStephane Eranian 	int fd;
176410136f5SStephane Eranian 
17711a64a05SBen Hutchings 	scnprintf(path, PATH_MAX, "%s/%s.unit", dir, name);
178410136f5SStephane Eranian 
179410136f5SStephane Eranian 	fd = open(path, O_RDONLY);
180410136f5SStephane Eranian 	if (fd == -1)
181410136f5SStephane Eranian 		return -1;
182410136f5SStephane Eranian 
183410136f5SStephane Eranian 	sret = read(fd, alias->unit, UNIT_MAX_LEN);
184410136f5SStephane Eranian 	if (sret < 0)
185410136f5SStephane Eranian 		goto error;
186410136f5SStephane Eranian 
187410136f5SStephane Eranian 	close(fd);
188410136f5SStephane Eranian 
1899ecae065SMadhavan Srinivasan 	if (alias->unit[sret - 1] == '\n')
1909ecae065SMadhavan Srinivasan 		alias->unit[sret - 1] = '\0';
1919ecae065SMadhavan Srinivasan 	else
192410136f5SStephane Eranian 		alias->unit[sret] = '\0';
193410136f5SStephane Eranian 
194410136f5SStephane Eranian 	return 0;
195410136f5SStephane Eranian error:
196410136f5SStephane Eranian 	close(fd);
197410136f5SStephane Eranian 	alias->unit[0] = '\0';
198410136f5SStephane Eranian 	return -1;
199410136f5SStephane Eranian }
200410136f5SStephane Eranian 
201044330c1SMatt Fleming static int
202044330c1SMatt Fleming perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *name)
203044330c1SMatt Fleming {
204044330c1SMatt Fleming 	char path[PATH_MAX];
205044330c1SMatt Fleming 	int fd;
206044330c1SMatt Fleming 
20711a64a05SBen Hutchings 	scnprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name);
208044330c1SMatt Fleming 
209044330c1SMatt Fleming 	fd = open(path, O_RDONLY);
210044330c1SMatt Fleming 	if (fd == -1)
211044330c1SMatt Fleming 		return -1;
212044330c1SMatt Fleming 
213044330c1SMatt Fleming 	close(fd);
214044330c1SMatt Fleming 
215044330c1SMatt Fleming 	alias->per_pkg = true;
216044330c1SMatt Fleming 	return 0;
217044330c1SMatt Fleming }
218044330c1SMatt Fleming 
2191d9e446bSJiri Olsa static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias,
2201d9e446bSJiri Olsa 				    char *dir, char *name)
2211d9e446bSJiri Olsa {
2221d9e446bSJiri Olsa 	char path[PATH_MAX];
2231d9e446bSJiri Olsa 	int fd;
2241d9e446bSJiri Olsa 
22511a64a05SBen Hutchings 	scnprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name);
2261d9e446bSJiri Olsa 
2271d9e446bSJiri Olsa 	fd = open(path, O_RDONLY);
2281d9e446bSJiri Olsa 	if (fd == -1)
2291d9e446bSJiri Olsa 		return -1;
2301d9e446bSJiri Olsa 
2311d9e446bSJiri Olsa 	alias->snapshot = true;
2321d9e446bSJiri Olsa 	close(fd);
2331d9e446bSJiri Olsa 	return 0;
2341d9e446bSJiri Olsa }
2351d9e446bSJiri Olsa 
2366dde6429SThomas Richter static void perf_pmu_assign_str(char *name, const char *field, char **old_str,
2376dde6429SThomas Richter 				char **new_str)
2386dde6429SThomas Richter {
2396dde6429SThomas Richter 	if (!*old_str)
2406dde6429SThomas Richter 		goto set_new;
2416dde6429SThomas Richter 
2426dde6429SThomas Richter 	if (*new_str) {	/* Have new string, check with old */
2436dde6429SThomas Richter 		if (strcasecmp(*old_str, *new_str))
2446dde6429SThomas Richter 			pr_debug("alias %s differs in field '%s'\n",
2456dde6429SThomas Richter 				 name, field);
2466dde6429SThomas Richter 		zfree(old_str);
2476dde6429SThomas Richter 	} else		/* Nothing new --> keep old string */
2486dde6429SThomas Richter 		return;
2496dde6429SThomas Richter set_new:
2506dde6429SThomas Richter 	*old_str = *new_str;
2516dde6429SThomas Richter 	*new_str = NULL;
2526dde6429SThomas Richter }
2536dde6429SThomas Richter 
2546dde6429SThomas Richter static void perf_pmu_update_alias(struct perf_pmu_alias *old,
2556dde6429SThomas Richter 				  struct perf_pmu_alias *newalias)
2566dde6429SThomas Richter {
2576dde6429SThomas Richter 	perf_pmu_assign_str(old->name, "desc", &old->desc, &newalias->desc);
2586dde6429SThomas Richter 	perf_pmu_assign_str(old->name, "long_desc", &old->long_desc,
2596dde6429SThomas Richter 			    &newalias->long_desc);
2606dde6429SThomas Richter 	perf_pmu_assign_str(old->name, "topic", &old->topic, &newalias->topic);
2616dde6429SThomas Richter 	perf_pmu_assign_str(old->name, "metric_expr", &old->metric_expr,
2626dde6429SThomas Richter 			    &newalias->metric_expr);
2636dde6429SThomas Richter 	perf_pmu_assign_str(old->name, "metric_name", &old->metric_name,
2646dde6429SThomas Richter 			    &newalias->metric_name);
2656dde6429SThomas Richter 	perf_pmu_assign_str(old->name, "value", &old->str, &newalias->str);
2666dde6429SThomas Richter 	old->scale = newalias->scale;
2676dde6429SThomas Richter 	old->per_pkg = newalias->per_pkg;
2686dde6429SThomas Richter 	old->snapshot = newalias->snapshot;
2696dde6429SThomas Richter 	memcpy(old->unit, newalias->unit, sizeof(old->unit));
2706dde6429SThomas Richter }
2716dde6429SThomas Richter 
2726dde6429SThomas Richter /* Delete an alias entry. */
2736dde6429SThomas Richter static void perf_pmu_free_alias(struct perf_pmu_alias *newalias)
2746dde6429SThomas Richter {
2756dde6429SThomas Richter 	zfree(&newalias->name);
2766dde6429SThomas Richter 	zfree(&newalias->desc);
2776dde6429SThomas Richter 	zfree(&newalias->long_desc);
2786dde6429SThomas Richter 	zfree(&newalias->topic);
2796dde6429SThomas Richter 	zfree(&newalias->str);
2806dde6429SThomas Richter 	zfree(&newalias->metric_expr);
2816dde6429SThomas Richter 	zfree(&newalias->metric_name);
2826dde6429SThomas Richter 	parse_events_terms__purge(&newalias->terms);
2836dde6429SThomas Richter 	free(newalias);
2846dde6429SThomas Richter }
2856dde6429SThomas Richter 
2866dde6429SThomas Richter /* Merge an alias, search in alias list. If this name is already
2876dde6429SThomas Richter  * present merge both of them to combine all information.
2886dde6429SThomas Richter  */
2896dde6429SThomas Richter static bool perf_pmu_merge_alias(struct perf_pmu_alias *newalias,
2906dde6429SThomas Richter 				 struct list_head *alist)
2916dde6429SThomas Richter {
2926dde6429SThomas Richter 	struct perf_pmu_alias *a;
2936dde6429SThomas Richter 
2946dde6429SThomas Richter 	list_for_each_entry(a, alist, list) {
2956dde6429SThomas Richter 		if (!strcasecmp(newalias->name, a->name)) {
2966dde6429SThomas Richter 			perf_pmu_update_alias(a, newalias);
2976dde6429SThomas Richter 			perf_pmu_free_alias(newalias);
2986dde6429SThomas Richter 			return true;
2996dde6429SThomas Richter 		}
3006dde6429SThomas Richter 	}
3016dde6429SThomas Richter 	return false;
3026dde6429SThomas Richter }
3036dde6429SThomas Richter 
30470c646e0SSukadev Bhattiprolu static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name,
305fedb2b51SAndi Kleen 				 char *desc, char *val,
306fedb2b51SAndi Kleen 				 char *long_desc, char *topic,
30700636c3bSAndi Kleen 				 char *unit, char *perpkg,
30896284814SAndi Kleen 				 char *metric_expr,
30996284814SAndi Kleen 				 char *metric_name)
310a6146d50SZheng Yan {
3110c24d6fbSThomas Richter 	struct parse_events_term *term;
3125c6ccc37SArnaldo Carvalho de Melo 	struct perf_pmu_alias *alias;
313a6146d50SZheng Yan 	int ret;
314fedb2b51SAndi Kleen 	int num;
3150c24d6fbSThomas Richter 	char newval[256];
316a6146d50SZheng Yan 
317a6146d50SZheng Yan 	alias = malloc(sizeof(*alias));
318a6146d50SZheng Yan 	if (!alias)
319a6146d50SZheng Yan 		return -ENOMEM;
320a6146d50SZheng Yan 
321a6146d50SZheng Yan 	INIT_LIST_HEAD(&alias->terms);
322410136f5SStephane Eranian 	alias->scale = 1.0;
323410136f5SStephane Eranian 	alias->unit[0] = '\0';
324044330c1SMatt Fleming 	alias->per_pkg = false;
32584530920SStephane Eranian 	alias->snapshot = false;
326410136f5SStephane Eranian 
32770c646e0SSukadev Bhattiprolu 	ret = parse_events_terms(&alias->terms, val);
328a6146d50SZheng Yan 	if (ret) {
32970c646e0SSukadev Bhattiprolu 		pr_err("Cannot parse alias %s: %d\n", val, ret);
330a6146d50SZheng Yan 		free(alias);
331a6146d50SZheng Yan 		return ret;
332a6146d50SZheng Yan 	}
333a6146d50SZheng Yan 
3340c24d6fbSThomas Richter 	/* Scan event and remove leading zeroes, spaces, newlines, some
3350c24d6fbSThomas Richter 	 * platforms have terms specified as
3360c24d6fbSThomas Richter 	 * event=0x0091 (read from files ../<PMU>/events/<FILE>
3370c24d6fbSThomas Richter 	 * and terms specified as event=0x91 (read from JSON files).
3380c24d6fbSThomas Richter 	 *
3390c24d6fbSThomas Richter 	 * Rebuild string to make alias->str member comparable.
3400c24d6fbSThomas Richter 	 */
3410c24d6fbSThomas Richter 	memset(newval, 0, sizeof(newval));
3420c24d6fbSThomas Richter 	ret = 0;
3430c24d6fbSThomas Richter 	list_for_each_entry(term, &alias->terms, list) {
3440c24d6fbSThomas Richter 		if (ret)
3450c24d6fbSThomas Richter 			ret += scnprintf(newval + ret, sizeof(newval) - ret,
3460c24d6fbSThomas Richter 					 ",");
3470c24d6fbSThomas Richter 		if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
3480c24d6fbSThomas Richter 			ret += scnprintf(newval + ret, sizeof(newval) - ret,
3490c24d6fbSThomas Richter 					 "%s=%#x", term->config, term->val.num);
3500c24d6fbSThomas Richter 		else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
3510c24d6fbSThomas Richter 			ret += scnprintf(newval + ret, sizeof(newval) - ret,
3520c24d6fbSThomas Richter 					 "%s=%s", term->config, term->val.str);
3530c24d6fbSThomas Richter 	}
3540c24d6fbSThomas Richter 
355a6146d50SZheng Yan 	alias->name = strdup(name);
35670c646e0SSukadev Bhattiprolu 	if (dir) {
357410136f5SStephane Eranian 		/*
358410136f5SStephane Eranian 		 * load unit name and scale if available
359410136f5SStephane Eranian 		 */
360410136f5SStephane Eranian 		perf_pmu__parse_unit(alias, dir, name);
361410136f5SStephane Eranian 		perf_pmu__parse_scale(alias, dir, name);
362044330c1SMatt Fleming 		perf_pmu__parse_per_pkg(alias, dir, name);
3631d9e446bSJiri Olsa 		perf_pmu__parse_snapshot(alias, dir, name);
36470c646e0SSukadev Bhattiprolu 	}
365410136f5SStephane Eranian 
36600636c3bSAndi Kleen 	alias->metric_expr = metric_expr ? strdup(metric_expr) : NULL;
36796284814SAndi Kleen 	alias->metric_name = metric_name ? strdup(metric_name): NULL;
36808e60ed1SAndi Kleen 	alias->desc = desc ? strdup(desc) : NULL;
369c8d6828aSSukadev Bhattiprolu 	alias->long_desc = long_desc ? strdup(long_desc) :
370c8d6828aSSukadev Bhattiprolu 				desc ? strdup(desc) : NULL;
371dd5f1036SAndi Kleen 	alias->topic = topic ? strdup(topic) : NULL;
372fedb2b51SAndi Kleen 	if (unit) {
373fedb2b51SAndi Kleen 		if (convert_scale(unit, &unit, &alias->scale) < 0)
374fedb2b51SAndi Kleen 			return -1;
375fedb2b51SAndi Kleen 		snprintf(alias->unit, sizeof(alias->unit), "%s", unit);
376fedb2b51SAndi Kleen 	}
377fedb2b51SAndi Kleen 	alias->per_pkg = perpkg && sscanf(perpkg, "%d", &num) == 1 && num == 1;
3780c24d6fbSThomas Richter 	alias->str = strdup(newval);
379f2361024SAndi Kleen 
3806dde6429SThomas Richter 	if (!perf_pmu_merge_alias(alias, list))
381a6146d50SZheng Yan 		list_add_tail(&alias->list, list);
382410136f5SStephane Eranian 
383a6146d50SZheng Yan 	return 0;
384a6146d50SZheng Yan }
385a6146d50SZheng Yan 
38670c646e0SSukadev Bhattiprolu static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file)
38770c646e0SSukadev Bhattiprolu {
38870c646e0SSukadev Bhattiprolu 	char buf[256];
38970c646e0SSukadev Bhattiprolu 	int ret;
39070c646e0SSukadev Bhattiprolu 
39170c646e0SSukadev Bhattiprolu 	ret = fread(buf, 1, sizeof(buf), file);
39270c646e0SSukadev Bhattiprolu 	if (ret == 0)
39370c646e0SSukadev Bhattiprolu 		return -EINVAL;
39470c646e0SSukadev Bhattiprolu 
39570c646e0SSukadev Bhattiprolu 	buf[ret] = 0;
39670c646e0SSukadev Bhattiprolu 
397ea23ac73SThomas Richter 	/* Remove trailing newline from sysfs file */
39813c230abSArnaldo Carvalho de Melo 	strim(buf);
399ea23ac73SThomas Richter 
400fedb2b51SAndi Kleen 	return __perf_pmu__new_alias(list, dir, name, NULL, buf, NULL, NULL, NULL,
40196284814SAndi Kleen 				     NULL, NULL, NULL);
40270c646e0SSukadev Bhattiprolu }
40370c646e0SSukadev Bhattiprolu 
40446441bdcSMatt Fleming static inline bool pmu_alias_info_file(char *name)
40546441bdcSMatt Fleming {
40646441bdcSMatt Fleming 	size_t len;
40746441bdcSMatt Fleming 
40846441bdcSMatt Fleming 	len = strlen(name);
40946441bdcSMatt Fleming 	if (len > 5 && !strcmp(name + len - 5, ".unit"))
41046441bdcSMatt Fleming 		return true;
41146441bdcSMatt Fleming 	if (len > 6 && !strcmp(name + len - 6, ".scale"))
41246441bdcSMatt Fleming 		return true;
413044330c1SMatt Fleming 	if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
414044330c1SMatt Fleming 		return true;
4151d9e446bSJiri Olsa 	if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
4161d9e446bSJiri Olsa 		return true;
41746441bdcSMatt Fleming 
41846441bdcSMatt Fleming 	return false;
41946441bdcSMatt Fleming }
42046441bdcSMatt Fleming 
421a6146d50SZheng Yan /*
422a6146d50SZheng Yan  * Process all the sysfs attributes located under the directory
423a6146d50SZheng Yan  * specified in 'dir' parameter.
424a6146d50SZheng Yan  */
425a6146d50SZheng Yan static int pmu_aliases_parse(char *dir, struct list_head *head)
426a6146d50SZheng Yan {
427a6146d50SZheng Yan 	struct dirent *evt_ent;
428a6146d50SZheng Yan 	DIR *event_dir;
429a6146d50SZheng Yan 
430a6146d50SZheng Yan 	event_dir = opendir(dir);
431a6146d50SZheng Yan 	if (!event_dir)
432a6146d50SZheng Yan 		return -EINVAL;
433a6146d50SZheng Yan 
434940db6dcSAndi Kleen 	while ((evt_ent = readdir(event_dir))) {
435a6146d50SZheng Yan 		char path[PATH_MAX];
436a6146d50SZheng Yan 		char *name = evt_ent->d_name;
437a6146d50SZheng Yan 		FILE *file;
438a6146d50SZheng Yan 
439a6146d50SZheng Yan 		if (!strcmp(name, ".") || !strcmp(name, ".."))
440a6146d50SZheng Yan 			continue;
441a6146d50SZheng Yan 
442410136f5SStephane Eranian 		/*
44346441bdcSMatt Fleming 		 * skip info files parsed in perf_pmu__new_alias()
444410136f5SStephane Eranian 		 */
44546441bdcSMatt Fleming 		if (pmu_alias_info_file(name))
446410136f5SStephane Eranian 			continue;
447410136f5SStephane Eranian 
44877f18153SJiri Olsa 		scnprintf(path, PATH_MAX, "%s/%s", dir, name);
449a6146d50SZheng Yan 
450a6146d50SZheng Yan 		file = fopen(path, "r");
451940db6dcSAndi Kleen 		if (!file) {
452940db6dcSAndi Kleen 			pr_debug("Cannot open %s\n", path);
453940db6dcSAndi Kleen 			continue;
454940db6dcSAndi Kleen 		}
455410136f5SStephane Eranian 
456940db6dcSAndi Kleen 		if (perf_pmu__new_alias(head, dir, name, file) < 0)
457940db6dcSAndi Kleen 			pr_debug("Cannot set up %s\n", name);
458a6146d50SZheng Yan 		fclose(file);
459a6146d50SZheng Yan 	}
460a6146d50SZheng Yan 
461a6146d50SZheng Yan 	closedir(event_dir);
462940db6dcSAndi Kleen 	return 0;
463a6146d50SZheng Yan }
464a6146d50SZheng Yan 
465a6146d50SZheng Yan /*
466a6146d50SZheng Yan  * Reading the pmu event aliases definition, which should be located at:
467a6146d50SZheng Yan  * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
468a6146d50SZheng Yan  */
469b6b96fb4SAdrian Hunter static int pmu_aliases(const char *name, struct list_head *head)
470a6146d50SZheng Yan {
471a6146d50SZheng Yan 	struct stat st;
472a6146d50SZheng Yan 	char path[PATH_MAX];
473cf38fadaSArnaldo Carvalho de Melo 	const char *sysfs = sysfs__mountpoint();
474a6146d50SZheng Yan 
475a6146d50SZheng Yan 	if (!sysfs)
476a6146d50SZheng Yan 		return -1;
477a6146d50SZheng Yan 
478a6146d50SZheng Yan 	snprintf(path, PATH_MAX,
479a6146d50SZheng Yan 		 "%s/bus/event_source/devices/%s/events", sysfs, name);
480a6146d50SZheng Yan 
481a6146d50SZheng Yan 	if (stat(path, &st) < 0)
4823fded963SJiri Olsa 		return 0;	 /* no error if 'events' does not exist */
483a6146d50SZheng Yan 
484a6146d50SZheng Yan 	if (pmu_aliases_parse(path, head))
485a6146d50SZheng Yan 		return -1;
486a6146d50SZheng Yan 
487a6146d50SZheng Yan 	return 0;
488a6146d50SZheng Yan }
489a6146d50SZheng Yan 
4905c6ccc37SArnaldo Carvalho de Melo static int pmu_alias_terms(struct perf_pmu_alias *alias,
491a6146d50SZheng Yan 			   struct list_head *terms)
492a6146d50SZheng Yan {
4937c2f8164SJiri Olsa 	struct parse_events_term *term, *cloned;
494a6146d50SZheng Yan 	LIST_HEAD(list);
495a6146d50SZheng Yan 	int ret;
496a6146d50SZheng Yan 
497a6146d50SZheng Yan 	list_for_each_entry(term, &alias->terms, list) {
4987c2f8164SJiri Olsa 		ret = parse_events_term__clone(&cloned, term);
499a6146d50SZheng Yan 		if (ret) {
500682dc24cSArnaldo Carvalho de Melo 			parse_events_terms__purge(&list);
501a6146d50SZheng Yan 			return ret;
502a6146d50SZheng Yan 		}
503c2f1ceadSAndi Kleen 		/*
504c2f1ceadSAndi Kleen 		 * Weak terms don't override command line options,
505c2f1ceadSAndi Kleen 		 * which we don't want for implicit terms in aliases.
506c2f1ceadSAndi Kleen 		 */
507c2f1ceadSAndi Kleen 		cloned->weak = true;
5087c2f8164SJiri Olsa 		list_add_tail(&cloned->list, &list);
509a6146d50SZheng Yan 	}
510a6146d50SZheng Yan 	list_splice(&list, terms);
511a6146d50SZheng Yan 	return 0;
512a6146d50SZheng Yan }
513a6146d50SZheng Yan 
514cd82a32eSJiri Olsa /*
515cd82a32eSJiri Olsa  * Reading/parsing the default pmu type value, which should be
516cd82a32eSJiri Olsa  * located at:
517cd82a32eSJiri Olsa  * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
518cd82a32eSJiri Olsa  */
519b6b96fb4SAdrian Hunter static int pmu_type(const char *name, __u32 *type)
520cd82a32eSJiri Olsa {
521cd82a32eSJiri Olsa 	struct stat st;
522cd82a32eSJiri Olsa 	char path[PATH_MAX];
523cd82a32eSJiri Olsa 	FILE *file;
524cd82a32eSJiri Olsa 	int ret = 0;
525cf38fadaSArnaldo Carvalho de Melo 	const char *sysfs = sysfs__mountpoint();
526cd82a32eSJiri Olsa 
527cd82a32eSJiri Olsa 	if (!sysfs)
528cd82a32eSJiri Olsa 		return -1;
529cd82a32eSJiri Olsa 
530cd82a32eSJiri Olsa 	snprintf(path, PATH_MAX,
53150a9667cSRobert Richter 		 "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name);
532cd82a32eSJiri Olsa 
533cd82a32eSJiri Olsa 	if (stat(path, &st) < 0)
534cd82a32eSJiri Olsa 		return -1;
535cd82a32eSJiri Olsa 
536cd82a32eSJiri Olsa 	file = fopen(path, "r");
537cd82a32eSJiri Olsa 	if (!file)
538cd82a32eSJiri Olsa 		return -EINVAL;
539cd82a32eSJiri Olsa 
540cd82a32eSJiri Olsa 	if (1 != fscanf(file, "%u", type))
541cd82a32eSJiri Olsa 		ret = -1;
542cd82a32eSJiri Olsa 
543cd82a32eSJiri Olsa 	fclose(file);
544cd82a32eSJiri Olsa 	return ret;
545cd82a32eSJiri Olsa }
546cd82a32eSJiri Olsa 
54750a9667cSRobert Richter /* Add all pmus in sysfs to pmu list: */
54850a9667cSRobert Richter static void pmu_read_sysfs(void)
54950a9667cSRobert Richter {
55050a9667cSRobert Richter 	char path[PATH_MAX];
55150a9667cSRobert Richter 	DIR *dir;
55250a9667cSRobert Richter 	struct dirent *dent;
553cf38fadaSArnaldo Carvalho de Melo 	const char *sysfs = sysfs__mountpoint();
55450a9667cSRobert Richter 
55550a9667cSRobert Richter 	if (!sysfs)
55650a9667cSRobert Richter 		return;
55750a9667cSRobert Richter 
55850a9667cSRobert Richter 	snprintf(path, PATH_MAX,
55950a9667cSRobert Richter 		 "%s" EVENT_SOURCE_DEVICE_PATH, sysfs);
56050a9667cSRobert Richter 
56150a9667cSRobert Richter 	dir = opendir(path);
56250a9667cSRobert Richter 	if (!dir)
56350a9667cSRobert Richter 		return;
56450a9667cSRobert Richter 
56550a9667cSRobert Richter 	while ((dent = readdir(dir))) {
56650a9667cSRobert Richter 		if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
56750a9667cSRobert Richter 			continue;
56850a9667cSRobert Richter 		/* add to static LIST_HEAD(pmus): */
56950a9667cSRobert Richter 		perf_pmu__find(dent->d_name);
57050a9667cSRobert Richter 	}
57150a9667cSRobert Richter 
57250a9667cSRobert Richter 	closedir(dir);
57350a9667cSRobert Richter }
57450a9667cSRobert Richter 
57566ec1191SMark Rutland static struct cpu_map *__pmu_cpumask(const char *path)
57666ec1191SMark Rutland {
57766ec1191SMark Rutland 	FILE *file;
57866ec1191SMark Rutland 	struct cpu_map *cpus;
57966ec1191SMark Rutland 
58066ec1191SMark Rutland 	file = fopen(path, "r");
58166ec1191SMark Rutland 	if (!file)
58266ec1191SMark Rutland 		return NULL;
58366ec1191SMark Rutland 
58466ec1191SMark Rutland 	cpus = cpu_map__read(file);
58566ec1191SMark Rutland 	fclose(file);
58666ec1191SMark Rutland 	return cpus;
58766ec1191SMark Rutland }
58866ec1191SMark Rutland 
58966ec1191SMark Rutland /*
59066ec1191SMark Rutland  * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64)
59166ec1191SMark Rutland  * may have a "cpus" file.
59266ec1191SMark Rutland  */
59366ec1191SMark Rutland #define CPUS_TEMPLATE_UNCORE	"%s/bus/event_source/devices/%s/cpumask"
59466ec1191SMark Rutland #define CPUS_TEMPLATE_CPU	"%s/bus/event_source/devices/%s/cpus"
59566ec1191SMark Rutland 
596b6b96fb4SAdrian Hunter static struct cpu_map *pmu_cpumask(const char *name)
5977ae92e74SYan, Zheng {
5987ae92e74SYan, Zheng 	char path[PATH_MAX];
5997ae92e74SYan, Zheng 	struct cpu_map *cpus;
600cf38fadaSArnaldo Carvalho de Melo 	const char *sysfs = sysfs__mountpoint();
6017e3fcffeSMark Rutland 	const char *templates[] = {
60266ec1191SMark Rutland 		CPUS_TEMPLATE_UNCORE,
60366ec1191SMark Rutland 		CPUS_TEMPLATE_CPU,
6047e3fcffeSMark Rutland 		NULL
6057e3fcffeSMark Rutland 	};
6067e3fcffeSMark Rutland 	const char **template;
6077ae92e74SYan, Zheng 
6087ae92e74SYan, Zheng 	if (!sysfs)
6097ae92e74SYan, Zheng 		return NULL;
6107ae92e74SYan, Zheng 
6117e3fcffeSMark Rutland 	for (template = templates; *template; template++) {
6127e3fcffeSMark Rutland 		snprintf(path, PATH_MAX, *template, sysfs, name);
61366ec1191SMark Rutland 		cpus = __pmu_cpumask(path);
61466ec1191SMark Rutland 		if (cpus)
61566ec1191SMark Rutland 			return cpus;
6167e3fcffeSMark Rutland 	}
6177ae92e74SYan, Zheng 
6187ae92e74SYan, Zheng 	return NULL;
61966ec1191SMark Rutland }
6207ae92e74SYan, Zheng 
62166ec1191SMark Rutland static bool pmu_is_uncore(const char *name)
62266ec1191SMark Rutland {
62366ec1191SMark Rutland 	char path[PATH_MAX];
62466ec1191SMark Rutland 	struct cpu_map *cpus;
62566ec1191SMark Rutland 	const char *sysfs = sysfs__mountpoint();
6267ae92e74SYan, Zheng 
62766ec1191SMark Rutland 	snprintf(path, PATH_MAX, CPUS_TEMPLATE_UNCORE, sysfs, name);
62866ec1191SMark Rutland 	cpus = __pmu_cpumask(path);
62966ec1191SMark Rutland 	cpu_map__put(cpus);
63066ec1191SMark Rutland 
63166ec1191SMark Rutland 	return !!cpus;
6327ae92e74SYan, Zheng }
6337ae92e74SYan, Zheng 
634933f82ffSSukadev Bhattiprolu /*
63514b22ae0SGanapatrao Kulkarni  *  PMU CORE devices have different name other than cpu in sysfs on some
636292c34c1SKan Liang  *  platforms.
637292c34c1SKan Liang  *  Looking for possible sysfs files to identify the arm core device.
63814b22ae0SGanapatrao Kulkarni  */
639292c34c1SKan Liang static int is_arm_pmu_core(const char *name)
64014b22ae0SGanapatrao Kulkarni {
64114b22ae0SGanapatrao Kulkarni 	struct stat st;
64214b22ae0SGanapatrao Kulkarni 	char path[PATH_MAX];
64314b22ae0SGanapatrao Kulkarni 	const char *sysfs = sysfs__mountpoint();
64414b22ae0SGanapatrao Kulkarni 
64514b22ae0SGanapatrao Kulkarni 	if (!sysfs)
64614b22ae0SGanapatrao Kulkarni 		return 0;
64714b22ae0SGanapatrao Kulkarni 
64814b22ae0SGanapatrao Kulkarni 	/* Look for cpu sysfs (specific to arm) */
64914b22ae0SGanapatrao Kulkarni 	scnprintf(path, PATH_MAX, "%s/bus/event_source/devices/%s/cpus",
65014b22ae0SGanapatrao Kulkarni 				sysfs, name);
65114b22ae0SGanapatrao Kulkarni 	if (stat(path, &st) == 0)
65214b22ae0SGanapatrao Kulkarni 		return 1;
65314b22ae0SGanapatrao Kulkarni 
65414b22ae0SGanapatrao Kulkarni 	return 0;
65514b22ae0SGanapatrao Kulkarni }
65614b22ae0SGanapatrao Kulkarni 
65754e32dc0SGanapatrao Kulkarni static char *perf_pmu__getcpuid(struct perf_pmu *pmu)
658d77ade9fSAndi Kleen {
659d77ade9fSAndi Kleen 	char *cpuid;
660d77ade9fSAndi Kleen 	static bool printed;
661d77ade9fSAndi Kleen 
662d77ade9fSAndi Kleen 	cpuid = getenv("PERF_CPUID");
663d77ade9fSAndi Kleen 	if (cpuid)
664d77ade9fSAndi Kleen 		cpuid = strdup(cpuid);
665d77ade9fSAndi Kleen 	if (!cpuid)
66654e32dc0SGanapatrao Kulkarni 		cpuid = get_cpuid_str(pmu);
667d77ade9fSAndi Kleen 	if (!cpuid)
668d77ade9fSAndi Kleen 		return NULL;
669d77ade9fSAndi Kleen 
670d77ade9fSAndi Kleen 	if (!printed) {
671d77ade9fSAndi Kleen 		pr_debug("Using CPUID %s\n", cpuid);
672d77ade9fSAndi Kleen 		printed = true;
673d77ade9fSAndi Kleen 	}
674d77ade9fSAndi Kleen 	return cpuid;
675d77ade9fSAndi Kleen }
676d77ade9fSAndi Kleen 
67754e32dc0SGanapatrao Kulkarni struct pmu_events_map *perf_pmu__find_map(struct perf_pmu *pmu)
678d77ade9fSAndi Kleen {
679d77ade9fSAndi Kleen 	struct pmu_events_map *map;
68054e32dc0SGanapatrao Kulkarni 	char *cpuid = perf_pmu__getcpuid(pmu);
681d77ade9fSAndi Kleen 	int i;
682d77ade9fSAndi Kleen 
683de3d0f12SGanapatrao Kulkarni 	/* on some platforms which uses cpus map, cpuid can be NULL for
684de3d0f12SGanapatrao Kulkarni 	 * PMUs other than CORE PMUs.
685de3d0f12SGanapatrao Kulkarni 	 */
686de3d0f12SGanapatrao Kulkarni 	if (!cpuid)
687de3d0f12SGanapatrao Kulkarni 		return NULL;
688de3d0f12SGanapatrao Kulkarni 
689d77ade9fSAndi Kleen 	i = 0;
690d77ade9fSAndi Kleen 	for (;;) {
691d77ade9fSAndi Kleen 		map = &pmu_events_map[i++];
692d77ade9fSAndi Kleen 		if (!map->table) {
693d77ade9fSAndi Kleen 			map = NULL;
694d77ade9fSAndi Kleen 			break;
695d77ade9fSAndi Kleen 		}
696d77ade9fSAndi Kleen 
6974cb7d3ecSThomas Richter 		if (!strcmp_cpuid_str(map->cpuid, cpuid))
698d77ade9fSAndi Kleen 			break;
699d77ade9fSAndi Kleen 	}
700d77ade9fSAndi Kleen 	free(cpuid);
701d77ade9fSAndi Kleen 	return map;
702d77ade9fSAndi Kleen }
703d77ade9fSAndi Kleen 
704730670b1SJohn Garry static bool pmu_uncore_alias_match(const char *pmu_name, const char *name)
705730670b1SJohn Garry {
706730670b1SJohn Garry 	char *tmp = NULL, *tok, *str;
707730670b1SJohn Garry 	bool res;
708730670b1SJohn Garry 
709730670b1SJohn Garry 	str = strdup(pmu_name);
710730670b1SJohn Garry 	if (!str)
711730670b1SJohn Garry 		return false;
712730670b1SJohn Garry 
713730670b1SJohn Garry 	/*
714730670b1SJohn Garry 	 * uncore alias may be from different PMU with common prefix
715730670b1SJohn Garry 	 */
716730670b1SJohn Garry 	tok = strtok_r(str, ",", &tmp);
717730670b1SJohn Garry 	if (strncmp(pmu_name, tok, strlen(tok))) {
718730670b1SJohn Garry 		res = false;
719730670b1SJohn Garry 		goto out;
720730670b1SJohn Garry 	}
721730670b1SJohn Garry 
722730670b1SJohn Garry 	/*
723730670b1SJohn Garry 	 * Match more complex aliases where the alias name is a comma-delimited
724730670b1SJohn Garry 	 * list of tokens, orderly contained in the matching PMU name.
725730670b1SJohn Garry 	 *
726730670b1SJohn Garry 	 * Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we
727730670b1SJohn Garry 	 *	    match "socket" in "socketX_pmunameY" and then "pmuname" in
728730670b1SJohn Garry 	 *	    "pmunameY".
729730670b1SJohn Garry 	 */
730730670b1SJohn Garry 	for (; tok; name += strlen(tok), tok = strtok_r(NULL, ",", &tmp)) {
731730670b1SJohn Garry 		name = strstr(name, tok);
732730670b1SJohn Garry 		if (!name) {
733730670b1SJohn Garry 			res = false;
734730670b1SJohn Garry 			goto out;
735730670b1SJohn Garry 		}
736730670b1SJohn Garry 	}
737730670b1SJohn Garry 
738730670b1SJohn Garry 	res = true;
739730670b1SJohn Garry out:
740730670b1SJohn Garry 	free(str);
741730670b1SJohn Garry 	return res;
742730670b1SJohn Garry }
743730670b1SJohn Garry 
744933f82ffSSukadev Bhattiprolu /*
745933f82ffSSukadev Bhattiprolu  * From the pmu_events_map, find the table of PMU events that corresponds
746933f82ffSSukadev Bhattiprolu  * to the current running CPU. Then, add all PMU events from that table
747933f82ffSSukadev Bhattiprolu  * as aliases.
748933f82ffSSukadev Bhattiprolu  */
74954e32dc0SGanapatrao Kulkarni static void pmu_add_cpu_aliases(struct list_head *head, struct perf_pmu *pmu)
750933f82ffSSukadev Bhattiprolu {
751933f82ffSSukadev Bhattiprolu 	int i;
752933f82ffSSukadev Bhattiprolu 	struct pmu_events_map *map;
75354e32dc0SGanapatrao Kulkarni 	const char *name = pmu->name;
754933f82ffSSukadev Bhattiprolu 
75554e32dc0SGanapatrao Kulkarni 	map = perf_pmu__find_map(pmu);
756d77ade9fSAndi Kleen 	if (!map)
757933f82ffSSukadev Bhattiprolu 		return;
758933f82ffSSukadev Bhattiprolu 
759933f82ffSSukadev Bhattiprolu 	/*
760933f82ffSSukadev Bhattiprolu 	 * Found a matching PMU events table. Create aliases
761933f82ffSSukadev Bhattiprolu 	 */
762933f82ffSSukadev Bhattiprolu 	i = 0;
763933f82ffSSukadev Bhattiprolu 	while (1) {
764599ee18fSJohn Garry 		const char *cpu_name = is_arm_pmu_core(name) ? name : "cpu";
765599ee18fSJohn Garry 		struct pmu_event *pe = &map->table[i++];
766599ee18fSJohn Garry 		const char *pname = pe->pmu ? pe->pmu : cpu_name;
767fedb2b51SAndi Kleen 
768b18f3e36SAndi Kleen 		if (!pe->name) {
769b18f3e36SAndi Kleen 			if (pe->metric_group || pe->metric_name)
770b18f3e36SAndi Kleen 				continue;
771933f82ffSSukadev Bhattiprolu 			break;
772b18f3e36SAndi Kleen 		}
773933f82ffSSukadev Bhattiprolu 
774e94d6b7fSKan Liang 		if (pmu_is_uncore(name) &&
775730670b1SJohn Garry 		    pmu_uncore_alias_match(pname, name))
776e94d6b7fSKan Liang 			goto new_alias;
777e94d6b7fSKan Liang 
778ea1fa48cSThomas Richter 		if (strcmp(pname, name))
77914b22ae0SGanapatrao Kulkarni 			continue;
780fedb2b51SAndi Kleen 
781e94d6b7fSKan Liang new_alias:
782933f82ffSSukadev Bhattiprolu 		/* need type casts to override 'const' */
783933f82ffSSukadev Bhattiprolu 		__perf_pmu__new_alias(head, NULL, (char *)pe->name,
784c8d6828aSSukadev Bhattiprolu 				(char *)pe->desc, (char *)pe->event,
785fedb2b51SAndi Kleen 				(char *)pe->long_desc, (char *)pe->topic,
78600636c3bSAndi Kleen 				(char *)pe->unit, (char *)pe->perpkg,
78796284814SAndi Kleen 				(char *)pe->metric_expr,
78896284814SAndi Kleen 				(char *)pe->metric_name);
789933f82ffSSukadev Bhattiprolu 	}
790933f82ffSSukadev Bhattiprolu }
791933f82ffSSukadev Bhattiprolu 
792c5de47f2SSukadev Bhattiprolu struct perf_event_attr * __weak
793dc0a6202SAdrian Hunter perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
794dc0a6202SAdrian Hunter {
795dc0a6202SAdrian Hunter 	return NULL;
796dc0a6202SAdrian Hunter }
797dc0a6202SAdrian Hunter 
79890a86bdeSJiri Olsa static int pmu_max_precise(const char *name)
79990a86bdeSJiri Olsa {
80090a86bdeSJiri Olsa 	char path[PATH_MAX];
80190a86bdeSJiri Olsa 	int max_precise = -1;
80290a86bdeSJiri Olsa 
80390a86bdeSJiri Olsa 	scnprintf(path, PATH_MAX,
80490a86bdeSJiri Olsa 		 "bus/event_source/devices/%s/caps/max_precise",
80590a86bdeSJiri Olsa 		 name);
80690a86bdeSJiri Olsa 
80790a86bdeSJiri Olsa 	sysfs__read_int(path, &max_precise);
80890a86bdeSJiri Olsa 	return max_precise;
80990a86bdeSJiri Olsa }
81090a86bdeSJiri Olsa 
811b6b96fb4SAdrian Hunter static struct perf_pmu *pmu_lookup(const char *name)
812cd82a32eSJiri Olsa {
813cd82a32eSJiri Olsa 	struct perf_pmu *pmu;
814cd82a32eSJiri Olsa 	LIST_HEAD(format);
815a6146d50SZheng Yan 	LIST_HEAD(aliases);
816cd82a32eSJiri Olsa 	__u32 type;
817cd82a32eSJiri Olsa 
818cd82a32eSJiri Olsa 	/*
819cd82a32eSJiri Olsa 	 * The pmu data we store & need consists of the pmu
820cd82a32eSJiri Olsa 	 * type value and format definitions. Load both right
821cd82a32eSJiri Olsa 	 * now.
822cd82a32eSJiri Olsa 	 */
823cd82a32eSJiri Olsa 	if (pmu_format(name, &format))
824cd82a32eSJiri Olsa 		return NULL;
825cd82a32eSJiri Olsa 
82615b22ed3SAndi Kleen 	/*
82715b22ed3SAndi Kleen 	 * Check the type first to avoid unnecessary work.
82815b22ed3SAndi Kleen 	 */
82915b22ed3SAndi Kleen 	if (pmu_type(name, &type))
83015b22ed3SAndi Kleen 		return NULL;
83115b22ed3SAndi Kleen 
8323fded963SJiri Olsa 	if (pmu_aliases(name, &aliases))
8333fded963SJiri Olsa 		return NULL;
8343fded963SJiri Olsa 
835cd82a32eSJiri Olsa 	pmu = zalloc(sizeof(*pmu));
836cd82a32eSJiri Olsa 	if (!pmu)
837cd82a32eSJiri Olsa 		return NULL;
838cd82a32eSJiri Olsa 
8397ae92e74SYan, Zheng 	pmu->cpus = pmu_cpumask(name);
84054e32dc0SGanapatrao Kulkarni 	pmu->name = strdup(name);
84154e32dc0SGanapatrao Kulkarni 	pmu->type = type;
84266ec1191SMark Rutland 	pmu->is_uncore = pmu_is_uncore(name);
84390a86bdeSJiri Olsa 	pmu->max_precise = pmu_max_precise(name);
84454e32dc0SGanapatrao Kulkarni 	pmu_add_cpu_aliases(&aliases, pmu);
84566ec1191SMark Rutland 
846cd82a32eSJiri Olsa 	INIT_LIST_HEAD(&pmu->format);
847a6146d50SZheng Yan 	INIT_LIST_HEAD(&pmu->aliases);
848cd82a32eSJiri Olsa 	list_splice(&format, &pmu->format);
849a6146d50SZheng Yan 	list_splice(&aliases, &pmu->aliases);
8509bc8f9feSRobert Richter 	list_add_tail(&pmu->list, &pmus);
851dc0a6202SAdrian Hunter 
852dc0a6202SAdrian Hunter 	pmu->default_config = perf_pmu__get_default_config(pmu);
853dc0a6202SAdrian Hunter 
854cd82a32eSJiri Olsa 	return pmu;
855cd82a32eSJiri Olsa }
856cd82a32eSJiri Olsa 
857b6b96fb4SAdrian Hunter static struct perf_pmu *pmu_find(const char *name)
858cd82a32eSJiri Olsa {
859cd82a32eSJiri Olsa 	struct perf_pmu *pmu;
860cd82a32eSJiri Olsa 
861cd82a32eSJiri Olsa 	list_for_each_entry(pmu, &pmus, list)
862cd82a32eSJiri Olsa 		if (!strcmp(pmu->name, name))
863cd82a32eSJiri Olsa 			return pmu;
864cd82a32eSJiri Olsa 
865cd82a32eSJiri Olsa 	return NULL;
866cd82a32eSJiri Olsa }
867cd82a32eSJiri Olsa 
86850a9667cSRobert Richter struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
86950a9667cSRobert Richter {
87050a9667cSRobert Richter 	/*
87150a9667cSRobert Richter 	 * pmu iterator: If pmu is NULL, we start at the begin,
87250a9667cSRobert Richter 	 * otherwise return the next pmu. Returns NULL on end.
87350a9667cSRobert Richter 	 */
87450a9667cSRobert Richter 	if (!pmu) {
87550a9667cSRobert Richter 		pmu_read_sysfs();
87650a9667cSRobert Richter 		pmu = list_prepare_entry(pmu, &pmus, list);
87750a9667cSRobert Richter 	}
87850a9667cSRobert Richter 	list_for_each_entry_continue(pmu, &pmus, list)
87950a9667cSRobert Richter 		return pmu;
88050a9667cSRobert Richter 	return NULL;
88150a9667cSRobert Richter }
88250a9667cSRobert Richter 
883b6b96fb4SAdrian Hunter struct perf_pmu *perf_pmu__find(const char *name)
884cd82a32eSJiri Olsa {
885cd82a32eSJiri Olsa 	struct perf_pmu *pmu;
886cd82a32eSJiri Olsa 
887cd82a32eSJiri Olsa 	/*
888cd82a32eSJiri Olsa 	 * Once PMU is loaded it stays in the list,
889cd82a32eSJiri Olsa 	 * so we keep us from multiple reading/parsing
890cd82a32eSJiri Olsa 	 * the pmu format definitions.
891cd82a32eSJiri Olsa 	 */
892cd82a32eSJiri Olsa 	pmu = pmu_find(name);
893cd82a32eSJiri Olsa 	if (pmu)
894cd82a32eSJiri Olsa 		return pmu;
895cd82a32eSJiri Olsa 
896cd82a32eSJiri Olsa 	return pmu_lookup(name);
897cd82a32eSJiri Olsa }
898cd82a32eSJiri Olsa 
8995c6ccc37SArnaldo Carvalho de Melo static struct perf_pmu_format *
90009ff6071SAdrian Hunter pmu_find_format(struct list_head *formats, const char *name)
901cd82a32eSJiri Olsa {
9025c6ccc37SArnaldo Carvalho de Melo 	struct perf_pmu_format *format;
903cd82a32eSJiri Olsa 
904cd82a32eSJiri Olsa 	list_for_each_entry(format, formats, list)
905cd82a32eSJiri Olsa 		if (!strcmp(format->name, name))
906cd82a32eSJiri Olsa 			return format;
907cd82a32eSJiri Olsa 
908cd82a32eSJiri Olsa 	return NULL;
909cd82a32eSJiri Olsa }
910cd82a32eSJiri Olsa 
91109ff6071SAdrian Hunter __u64 perf_pmu__format_bits(struct list_head *formats, const char *name)
91209ff6071SAdrian Hunter {
91309ff6071SAdrian Hunter 	struct perf_pmu_format *format = pmu_find_format(formats, name);
91409ff6071SAdrian Hunter 	__u64 bits = 0;
91509ff6071SAdrian Hunter 	int fbit;
91609ff6071SAdrian Hunter 
91709ff6071SAdrian Hunter 	if (!format)
91809ff6071SAdrian Hunter 		return 0;
91909ff6071SAdrian Hunter 
92009ff6071SAdrian Hunter 	for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
92109ff6071SAdrian Hunter 		bits |= 1ULL << fbit;
92209ff6071SAdrian Hunter 
92309ff6071SAdrian Hunter 	return bits;
92409ff6071SAdrian Hunter }
92509ff6071SAdrian Hunter 
926cd82a32eSJiri Olsa /*
927dc0a6202SAdrian Hunter  * Sets value based on the format definition (format parameter)
928cd82a32eSJiri Olsa  * and unformated value (value parameter).
929cd82a32eSJiri Olsa  */
930dc0a6202SAdrian Hunter static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
931dc0a6202SAdrian Hunter 			     bool zero)
932cd82a32eSJiri Olsa {
933cd82a32eSJiri Olsa 	unsigned long fbit, vbit;
934cd82a32eSJiri Olsa 
935cd82a32eSJiri Olsa 	for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
936cd82a32eSJiri Olsa 
937cd82a32eSJiri Olsa 		if (!test_bit(fbit, format))
938cd82a32eSJiri Olsa 			continue;
939cd82a32eSJiri Olsa 
940dc0a6202SAdrian Hunter 		if (value & (1llu << vbit++))
941dc0a6202SAdrian Hunter 			*v |= (1llu << fbit);
942dc0a6202SAdrian Hunter 		else if (zero)
943dc0a6202SAdrian Hunter 			*v &= ~(1llu << fbit);
944cd82a32eSJiri Olsa 	}
945cd82a32eSJiri Olsa }
946cd82a32eSJiri Olsa 
9470efe6b67SAdrian Hunter static __u64 pmu_format_max_value(const unsigned long *format)
9480efe6b67SAdrian Hunter {
9491b9caa10SJiri Olsa 	int w;
9500efe6b67SAdrian Hunter 
9511b9caa10SJiri Olsa 	w = bitmap_weight(format, PERF_PMU_FORMAT_BITS);
9521b9caa10SJiri Olsa 	if (!w)
9531b9caa10SJiri Olsa 		return 0;
9541b9caa10SJiri Olsa 	if (w < 64)
9551b9caa10SJiri Olsa 		return (1ULL << w) - 1;
9561b9caa10SJiri Olsa 	return -1;
9570efe6b67SAdrian Hunter }
9580efe6b67SAdrian Hunter 
959cd82a32eSJiri Olsa /*
960688d4dfcSCody P Schafer  * Term is a string term, and might be a param-term. Try to look up it's value
961688d4dfcSCody P Schafer  * in the remaining terms.
962688d4dfcSCody P Schafer  * - We have a term like "base-or-format-term=param-term",
963688d4dfcSCody P Schafer  * - We need to find the value supplied for "param-term" (with param-term named
964688d4dfcSCody P Schafer  *   in a config string) later on in the term list.
965688d4dfcSCody P Schafer  */
966688d4dfcSCody P Schafer static int pmu_resolve_param_term(struct parse_events_term *term,
967688d4dfcSCody P Schafer 				  struct list_head *head_terms,
968688d4dfcSCody P Schafer 				  __u64 *value)
969688d4dfcSCody P Schafer {
970688d4dfcSCody P Schafer 	struct parse_events_term *t;
971688d4dfcSCody P Schafer 
972688d4dfcSCody P Schafer 	list_for_each_entry(t, head_terms, list) {
973688d4dfcSCody P Schafer 		if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
974688d4dfcSCody P Schafer 			if (!strcmp(t->config, term->config)) {
975688d4dfcSCody P Schafer 				t->used = true;
976688d4dfcSCody P Schafer 				*value = t->val.num;
977688d4dfcSCody P Schafer 				return 0;
978688d4dfcSCody P Schafer 			}
979688d4dfcSCody P Schafer 		}
980688d4dfcSCody P Schafer 	}
981688d4dfcSCody P Schafer 
982bb963e16SNamhyung Kim 	if (verbose > 0)
983688d4dfcSCody P Schafer 		printf("Required parameter '%s' not specified\n", term->config);
984688d4dfcSCody P Schafer 
985688d4dfcSCody P Schafer 	return -1;
986688d4dfcSCody P Schafer }
987688d4dfcSCody P Schafer 
988ffeb883eSHe Kuang static char *pmu_formats_string(struct list_head *formats)
989e64b020bSJiri Olsa {
990e64b020bSJiri Olsa 	struct perf_pmu_format *format;
99111db4e29SMasami Hiramatsu 	char *str = NULL;
99211db4e29SMasami Hiramatsu 	struct strbuf buf = STRBUF_INIT;
993e64b020bSJiri Olsa 	unsigned i = 0;
994e64b020bSJiri Olsa 
995ffeb883eSHe Kuang 	if (!formats)
996e64b020bSJiri Olsa 		return NULL;
997e64b020bSJiri Olsa 
998e64b020bSJiri Olsa 	/* sysfs exported terms */
999ffeb883eSHe Kuang 	list_for_each_entry(format, formats, list)
100011db4e29SMasami Hiramatsu 		if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
100111db4e29SMasami Hiramatsu 			goto error;
1002e64b020bSJiri Olsa 
1003ffeb883eSHe Kuang 	str = strbuf_detach(&buf, NULL);
100411db4e29SMasami Hiramatsu error:
1005ffeb883eSHe Kuang 	strbuf_release(&buf);
1006e64b020bSJiri Olsa 
1007e64b020bSJiri Olsa 	return str;
1008e64b020bSJiri Olsa }
1009e64b020bSJiri Olsa 
1010688d4dfcSCody P Schafer /*
1011cd82a32eSJiri Olsa  * Setup one of config[12] attr members based on the
101288aca8d9SCody P Schafer  * user input data - term parameter.
1013cd82a32eSJiri Olsa  */
1014cd82a32eSJiri Olsa static int pmu_config_term(struct list_head *formats,
1015cd82a32eSJiri Olsa 			   struct perf_event_attr *attr,
1016dc0a6202SAdrian Hunter 			   struct parse_events_term *term,
1017688d4dfcSCody P Schafer 			   struct list_head *head_terms,
1018e64b020bSJiri Olsa 			   bool zero, struct parse_events_error *err)
1019cd82a32eSJiri Olsa {
10205c6ccc37SArnaldo Carvalho de Melo 	struct perf_pmu_format *format;
1021cd82a32eSJiri Olsa 	__u64 *vp;
10220efe6b67SAdrian Hunter 	__u64 val, max_val;
1023cd82a32eSJiri Olsa 
1024cd82a32eSJiri Olsa 	/*
1025688d4dfcSCody P Schafer 	 * If this is a parameter we've already used for parameterized-eval,
1026688d4dfcSCody P Schafer 	 * skip it in normal eval.
1027688d4dfcSCody P Schafer 	 */
1028688d4dfcSCody P Schafer 	if (term->used)
1029688d4dfcSCody P Schafer 		return 0;
1030688d4dfcSCody P Schafer 
1031688d4dfcSCody P Schafer 	/*
1032cd82a32eSJiri Olsa 	 * Hardcoded terms should be already in, so nothing
1033cd82a32eSJiri Olsa 	 * to be done for them.
1034cd82a32eSJiri Olsa 	 */
1035cd82a32eSJiri Olsa 	if (parse_events__is_hardcoded_term(term))
1036cd82a32eSJiri Olsa 		return 0;
1037cd82a32eSJiri Olsa 
1038cd82a32eSJiri Olsa 	format = pmu_find_format(formats, term->config);
1039688d4dfcSCody P Schafer 	if (!format) {
1040bb963e16SNamhyung Kim 		if (verbose > 0)
1041688d4dfcSCody P Schafer 			printf("Invalid event/parameter '%s'\n", term->config);
1042e64b020bSJiri Olsa 		if (err) {
1043ffeb883eSHe Kuang 			char *pmu_term = pmu_formats_string(formats);
1044ffeb883eSHe Kuang 
1045e64b020bSJiri Olsa 			err->idx  = term->err_term;
1046e64b020bSJiri Olsa 			err->str  = strdup("unknown term");
1047ffeb883eSHe Kuang 			err->help = parse_events_formats_error_string(pmu_term);
1048ffeb883eSHe Kuang 			free(pmu_term);
1049e64b020bSJiri Olsa 		}
1050cd82a32eSJiri Olsa 		return -EINVAL;
1051688d4dfcSCody P Schafer 	}
1052cd82a32eSJiri Olsa 
1053cd82a32eSJiri Olsa 	switch (format->value) {
1054cd82a32eSJiri Olsa 	case PERF_PMU_FORMAT_VALUE_CONFIG:
1055cd82a32eSJiri Olsa 		vp = &attr->config;
1056cd82a32eSJiri Olsa 		break;
1057cd82a32eSJiri Olsa 	case PERF_PMU_FORMAT_VALUE_CONFIG1:
1058cd82a32eSJiri Olsa 		vp = &attr->config1;
1059cd82a32eSJiri Olsa 		break;
1060cd82a32eSJiri Olsa 	case PERF_PMU_FORMAT_VALUE_CONFIG2:
1061cd82a32eSJiri Olsa 		vp = &attr->config2;
1062cd82a32eSJiri Olsa 		break;
1063cd82a32eSJiri Olsa 	default:
1064cd82a32eSJiri Olsa 		return -EINVAL;
1065cd82a32eSJiri Olsa 	}
1066cd82a32eSJiri Olsa 
106716fa7e82SJiri Olsa 	/*
1068688d4dfcSCody P Schafer 	 * Either directly use a numeric term, or try to translate string terms
1069688d4dfcSCody P Schafer 	 * using event parameters.
107016fa7e82SJiri Olsa 	 */
107199e7138eSJiri Olsa 	if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
107299e7138eSJiri Olsa 		if (term->no_value &&
107399e7138eSJiri Olsa 		    bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
107499e7138eSJiri Olsa 			if (err) {
107599e7138eSJiri Olsa 				err->idx = term->err_val;
107699e7138eSJiri Olsa 				err->str = strdup("no value assigned for term");
107799e7138eSJiri Olsa 			}
107899e7138eSJiri Olsa 			return -EINVAL;
107999e7138eSJiri Olsa 		}
108099e7138eSJiri Olsa 
1081688d4dfcSCody P Schafer 		val = term->val.num;
108299e7138eSJiri Olsa 	} else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1083688d4dfcSCody P Schafer 		if (strcmp(term->val.str, "?")) {
1084bb963e16SNamhyung Kim 			if (verbose > 0) {
1085688d4dfcSCody P Schafer 				pr_info("Invalid sysfs entry %s=%s\n",
1086688d4dfcSCody P Schafer 						term->config, term->val.str);
1087e64b020bSJiri Olsa 			}
1088e64b020bSJiri Olsa 			if (err) {
1089e64b020bSJiri Olsa 				err->idx = term->err_val;
1090e64b020bSJiri Olsa 				err->str = strdup("expected numeric value");
1091e64b020bSJiri Olsa 			}
1092688d4dfcSCody P Schafer 			return -EINVAL;
1093688d4dfcSCody P Schafer 		}
1094688d4dfcSCody P Schafer 
1095688d4dfcSCody P Schafer 		if (pmu_resolve_param_term(term, head_terms, &val))
1096688d4dfcSCody P Schafer 			return -EINVAL;
1097688d4dfcSCody P Schafer 	} else
1098688d4dfcSCody P Schafer 		return -EINVAL;
1099688d4dfcSCody P Schafer 
11000efe6b67SAdrian Hunter 	max_val = pmu_format_max_value(format->bits);
11010efe6b67SAdrian Hunter 	if (val > max_val) {
11020efe6b67SAdrian Hunter 		if (err) {
11030efe6b67SAdrian Hunter 			err->idx = term->err_val;
11040efe6b67SAdrian Hunter 			if (asprintf(&err->str,
11050efe6b67SAdrian Hunter 				     "value too big for format, maximum is %llu",
11060efe6b67SAdrian Hunter 				     (unsigned long long)max_val) < 0)
11070efe6b67SAdrian Hunter 				err->str = strdup("value too big for format");
11080efe6b67SAdrian Hunter 			return -EINVAL;
11090efe6b67SAdrian Hunter 		}
11100efe6b67SAdrian Hunter 		/*
11110efe6b67SAdrian Hunter 		 * Assume we don't care if !err, in which case the value will be
11120efe6b67SAdrian Hunter 		 * silently truncated.
11130efe6b67SAdrian Hunter 		 */
11140efe6b67SAdrian Hunter 	}
11150efe6b67SAdrian Hunter 
1116688d4dfcSCody P Schafer 	pmu_format_value(format->bits, val, vp, zero);
1117cd82a32eSJiri Olsa 	return 0;
1118cd82a32eSJiri Olsa }
1119cd82a32eSJiri Olsa 
1120cff7f956SJiri Olsa int perf_pmu__config_terms(struct list_head *formats,
1121cff7f956SJiri Olsa 			   struct perf_event_attr *attr,
1122dc0a6202SAdrian Hunter 			   struct list_head *head_terms,
1123e64b020bSJiri Olsa 			   bool zero, struct parse_events_error *err)
1124cd82a32eSJiri Olsa {
11256cee6cd3SArnaldo Carvalho de Melo 	struct parse_events_term *term;
1126cd82a32eSJiri Olsa 
1127688d4dfcSCody P Schafer 	list_for_each_entry(term, head_terms, list) {
1128e64b020bSJiri Olsa 		if (pmu_config_term(formats, attr, term, head_terms,
1129e64b020bSJiri Olsa 				    zero, err))
1130cd82a32eSJiri Olsa 			return -EINVAL;
1131688d4dfcSCody P Schafer 	}
1132cd82a32eSJiri Olsa 
1133cd82a32eSJiri Olsa 	return 0;
1134cd82a32eSJiri Olsa }
1135cd82a32eSJiri Olsa 
1136cd82a32eSJiri Olsa /*
1137cd82a32eSJiri Olsa  * Configures event's 'attr' parameter based on the:
1138cd82a32eSJiri Olsa  * 1) users input - specified in terms parameter
1139cd82a32eSJiri Olsa  * 2) pmu format definitions - specified by pmu parameter
1140cd82a32eSJiri Olsa  */
1141cd82a32eSJiri Olsa int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
1142e64b020bSJiri Olsa 		     struct list_head *head_terms,
1143e64b020bSJiri Olsa 		     struct parse_events_error *err)
1144cd82a32eSJiri Olsa {
1145dc0a6202SAdrian Hunter 	bool zero = !!pmu->default_config;
1146dc0a6202SAdrian Hunter 
1147cd82a32eSJiri Olsa 	attr->type = pmu->type;
1148e64b020bSJiri Olsa 	return perf_pmu__config_terms(&pmu->format, attr, head_terms,
1149e64b020bSJiri Olsa 				      zero, err);
1150cd82a32eSJiri Olsa }
1151cd82a32eSJiri Olsa 
11525c6ccc37SArnaldo Carvalho de Melo static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
11536cee6cd3SArnaldo Carvalho de Melo 					     struct parse_events_term *term)
1154a6146d50SZheng Yan {
11555c6ccc37SArnaldo Carvalho de Melo 	struct perf_pmu_alias *alias;
1156a6146d50SZheng Yan 	char *name;
1157a6146d50SZheng Yan 
1158a6146d50SZheng Yan 	if (parse_events__is_hardcoded_term(term))
1159a6146d50SZheng Yan 		return NULL;
1160a6146d50SZheng Yan 
1161a6146d50SZheng Yan 	if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1162a6146d50SZheng Yan 		if (term->val.num != 1)
1163a6146d50SZheng Yan 			return NULL;
1164a6146d50SZheng Yan 		if (pmu_find_format(&pmu->format, term->config))
1165a6146d50SZheng Yan 			return NULL;
1166a6146d50SZheng Yan 		name = term->config;
1167a6146d50SZheng Yan 	} else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1168a6146d50SZheng Yan 		if (strcasecmp(term->config, "event"))
1169a6146d50SZheng Yan 			return NULL;
1170a6146d50SZheng Yan 		name = term->val.str;
1171a6146d50SZheng Yan 	} else {
1172a6146d50SZheng Yan 		return NULL;
1173a6146d50SZheng Yan 	}
1174a6146d50SZheng Yan 
1175a6146d50SZheng Yan 	list_for_each_entry(alias, &pmu->aliases, list) {
1176a6146d50SZheng Yan 		if (!strcasecmp(alias->name, name))
1177a6146d50SZheng Yan 			return alias;
1178a6146d50SZheng Yan 	}
1179a6146d50SZheng Yan 	return NULL;
1180a6146d50SZheng Yan }
1181a6146d50SZheng Yan 
1182410136f5SStephane Eranian 
11831d9e446bSJiri Olsa static int check_info_data(struct perf_pmu_alias *alias,
11841d9e446bSJiri Olsa 			   struct perf_pmu_info *info)
1185410136f5SStephane Eranian {
1186410136f5SStephane Eranian 	/*
1187410136f5SStephane Eranian 	 * Only one term in event definition can
11881d9e446bSJiri Olsa 	 * define unit, scale and snapshot, fail
11891d9e446bSJiri Olsa 	 * if there's more than one.
1190410136f5SStephane Eranian 	 */
1191b30a7d1fSArnaldo Carvalho de Melo 	if ((info->unit && alias->unit[0]) ||
11921d9e446bSJiri Olsa 	    (info->scale && alias->scale) ||
11931d9e446bSJiri Olsa 	    (info->snapshot && alias->snapshot))
1194410136f5SStephane Eranian 		return -EINVAL;
1195410136f5SStephane Eranian 
1196b30a7d1fSArnaldo Carvalho de Melo 	if (alias->unit[0])
11971d9e446bSJiri Olsa 		info->unit = alias->unit;
1198410136f5SStephane Eranian 
1199410136f5SStephane Eranian 	if (alias->scale)
12001d9e446bSJiri Olsa 		info->scale = alias->scale;
12011d9e446bSJiri Olsa 
12021d9e446bSJiri Olsa 	if (alias->snapshot)
12031d9e446bSJiri Olsa 		info->snapshot = alias->snapshot;
1204410136f5SStephane Eranian 
1205410136f5SStephane Eranian 	return 0;
1206410136f5SStephane Eranian }
1207410136f5SStephane Eranian 
1208a6146d50SZheng Yan /*
1209a6146d50SZheng Yan  * Find alias in the terms list and replace it with the terms
1210a6146d50SZheng Yan  * defined for the alias
1211a6146d50SZheng Yan  */
1212410136f5SStephane Eranian int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
121346441bdcSMatt Fleming 			  struct perf_pmu_info *info)
1214a6146d50SZheng Yan {
12156cee6cd3SArnaldo Carvalho de Melo 	struct parse_events_term *term, *h;
12165c6ccc37SArnaldo Carvalho de Melo 	struct perf_pmu_alias *alias;
1217a6146d50SZheng Yan 	int ret;
1218a6146d50SZheng Yan 
1219044330c1SMatt Fleming 	info->per_pkg = false;
1220044330c1SMatt Fleming 
12218a398897SStephane Eranian 	/*
12228a398897SStephane Eranian 	 * Mark unit and scale as not set
12238a398897SStephane Eranian 	 * (different from default values, see below)
12248a398897SStephane Eranian 	 */
122546441bdcSMatt Fleming 	info->unit     = NULL;
122646441bdcSMatt Fleming 	info->scale    = 0.0;
12271d9e446bSJiri Olsa 	info->snapshot = false;
122837932c18SAndi Kleen 	info->metric_expr = NULL;
122996284814SAndi Kleen 	info->metric_name = NULL;
1230410136f5SStephane Eranian 
1231a6146d50SZheng Yan 	list_for_each_entry_safe(term, h, head_terms, list) {
1232a6146d50SZheng Yan 		alias = pmu_find_alias(pmu, term);
1233a6146d50SZheng Yan 		if (!alias)
1234a6146d50SZheng Yan 			continue;
1235a6146d50SZheng Yan 		ret = pmu_alias_terms(alias, &term->list);
1236a6146d50SZheng Yan 		if (ret)
1237a6146d50SZheng Yan 			return ret;
1238410136f5SStephane Eranian 
12391d9e446bSJiri Olsa 		ret = check_info_data(alias, info);
1240410136f5SStephane Eranian 		if (ret)
1241410136f5SStephane Eranian 			return ret;
1242410136f5SStephane Eranian 
1243044330c1SMatt Fleming 		if (alias->per_pkg)
1244044330c1SMatt Fleming 			info->per_pkg = true;
124537932c18SAndi Kleen 		info->metric_expr = alias->metric_expr;
124696284814SAndi Kleen 		info->metric_name = alias->metric_name;
1247044330c1SMatt Fleming 
1248e56fbc9dSArnaldo Carvalho de Melo 		list_del_init(&term->list);
1249a6146d50SZheng Yan 		free(term);
1250a6146d50SZheng Yan 	}
12518a398897SStephane Eranian 
12528a398897SStephane Eranian 	/*
12538a398897SStephane Eranian 	 * if no unit or scale foundin aliases, then
12548a398897SStephane Eranian 	 * set defaults as for evsel
12558a398897SStephane Eranian 	 * unit cannot left to NULL
12568a398897SStephane Eranian 	 */
125746441bdcSMatt Fleming 	if (info->unit == NULL)
125846441bdcSMatt Fleming 		info->unit   = "";
12598a398897SStephane Eranian 
126046441bdcSMatt Fleming 	if (info->scale == 0.0)
126146441bdcSMatt Fleming 		info->scale  = 1.0;
12628a398897SStephane Eranian 
1263a6146d50SZheng Yan 	return 0;
1264a6146d50SZheng Yan }
1265a6146d50SZheng Yan 
1266cd82a32eSJiri Olsa int perf_pmu__new_format(struct list_head *list, char *name,
1267cd82a32eSJiri Olsa 			 int config, unsigned long *bits)
1268cd82a32eSJiri Olsa {
12695c6ccc37SArnaldo Carvalho de Melo 	struct perf_pmu_format *format;
1270cd82a32eSJiri Olsa 
1271cd82a32eSJiri Olsa 	format = zalloc(sizeof(*format));
1272cd82a32eSJiri Olsa 	if (!format)
1273cd82a32eSJiri Olsa 		return -ENOMEM;
1274cd82a32eSJiri Olsa 
1275cd82a32eSJiri Olsa 	format->name = strdup(name);
1276cd82a32eSJiri Olsa 	format->value = config;
1277cd82a32eSJiri Olsa 	memcpy(format->bits, bits, sizeof(format->bits));
1278cd82a32eSJiri Olsa 
1279cd82a32eSJiri Olsa 	list_add_tail(&format->list, list);
1280cd82a32eSJiri Olsa 	return 0;
1281cd82a32eSJiri Olsa }
1282cd82a32eSJiri Olsa 
1283cd82a32eSJiri Olsa void perf_pmu__set_format(unsigned long *bits, long from, long to)
1284cd82a32eSJiri Olsa {
1285cd82a32eSJiri Olsa 	long b;
1286cd82a32eSJiri Olsa 
1287cd82a32eSJiri Olsa 	if (!to)
1288cd82a32eSJiri Olsa 		to = from;
1289cd82a32eSJiri Olsa 
129015268138SSukadev Bhattiprolu 	memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
1291cd82a32eSJiri Olsa 	for (b = from; b <= to; b++)
1292cd82a32eSJiri Olsa 		set_bit(b, bits);
1293cd82a32eSJiri Olsa }
1294dc098b35SAndi Kleen 
1295aaea3617SCody P Schafer static int sub_non_neg(int a, int b)
1296aaea3617SCody P Schafer {
1297aaea3617SCody P Schafer 	if (b > a)
1298aaea3617SCody P Schafer 		return 0;
1299aaea3617SCody P Schafer 	return a - b;
1300aaea3617SCody P Schafer }
1301aaea3617SCody P Schafer 
1302dc098b35SAndi Kleen static char *format_alias(char *buf, int len, struct perf_pmu *pmu,
1303dc098b35SAndi Kleen 			  struct perf_pmu_alias *alias)
1304dc098b35SAndi Kleen {
1305aaea3617SCody P Schafer 	struct parse_events_term *term;
1306aaea3617SCody P Schafer 	int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
1307aaea3617SCody P Schafer 
1308aaea3617SCody P Schafer 	list_for_each_entry(term, &alias->terms, list) {
1309aaea3617SCody P Schafer 		if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1310aaea3617SCody P Schafer 			used += snprintf(buf + used, sub_non_neg(len, used),
1311aaea3617SCody P Schafer 					",%s=%s", term->config,
1312aaea3617SCody P Schafer 					term->val.str);
1313aaea3617SCody P Schafer 	}
1314aaea3617SCody P Schafer 
1315aaea3617SCody P Schafer 	if (sub_non_neg(len, used) > 0) {
1316aaea3617SCody P Schafer 		buf[used] = '/';
1317aaea3617SCody P Schafer 		used++;
1318aaea3617SCody P Schafer 	}
1319aaea3617SCody P Schafer 	if (sub_non_neg(len, used) > 0) {
1320aaea3617SCody P Schafer 		buf[used] = '\0';
1321aaea3617SCody P Schafer 		used++;
1322aaea3617SCody P Schafer 	} else
1323aaea3617SCody P Schafer 		buf[len - 1] = '\0';
1324aaea3617SCody P Schafer 
1325dc098b35SAndi Kleen 	return buf;
1326dc098b35SAndi Kleen }
1327dc098b35SAndi Kleen 
1328dc098b35SAndi Kleen static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu,
1329dc098b35SAndi Kleen 			     struct perf_pmu_alias *alias)
1330dc098b35SAndi Kleen {
1331dc098b35SAndi Kleen 	snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name);
1332dc098b35SAndi Kleen 	return buf;
1333dc098b35SAndi Kleen }
1334dc098b35SAndi Kleen 
1335dd5f1036SAndi Kleen struct sevent {
133608e60ed1SAndi Kleen 	char *name;
133708e60ed1SAndi Kleen 	char *desc;
1338dd5f1036SAndi Kleen 	char *topic;
1339f2361024SAndi Kleen 	char *str;
1340f2361024SAndi Kleen 	char *pmu;
13417f372a63SAndi Kleen 	char *metric_expr;
134296284814SAndi Kleen 	char *metric_name;
134308e60ed1SAndi Kleen };
134408e60ed1SAndi Kleen 
1345dd5f1036SAndi Kleen static int cmp_sevent(const void *a, const void *b)
1346dc098b35SAndi Kleen {
1347dd5f1036SAndi Kleen 	const struct sevent *as = a;
1348dd5f1036SAndi Kleen 	const struct sevent *bs = b;
134908e60ed1SAndi Kleen 
135008e60ed1SAndi Kleen 	/* Put extra events last */
135108e60ed1SAndi Kleen 	if (!!as->desc != !!bs->desc)
135208e60ed1SAndi Kleen 		return !!as->desc - !!bs->desc;
1353dd5f1036SAndi Kleen 	if (as->topic && bs->topic) {
1354dd5f1036SAndi Kleen 		int n = strcmp(as->topic, bs->topic);
1355dd5f1036SAndi Kleen 
1356dd5f1036SAndi Kleen 		if (n)
1357dd5f1036SAndi Kleen 			return n;
1358dd5f1036SAndi Kleen 	}
135908e60ed1SAndi Kleen 	return strcmp(as->name, bs->name);
136008e60ed1SAndi Kleen }
136108e60ed1SAndi Kleen 
136208e60ed1SAndi Kleen static void wordwrap(char *s, int start, int max, int corr)
136308e60ed1SAndi Kleen {
136408e60ed1SAndi Kleen 	int column = start;
136508e60ed1SAndi Kleen 	int n;
136608e60ed1SAndi Kleen 
136708e60ed1SAndi Kleen 	while (*s) {
136808e60ed1SAndi Kleen 		int wlen = strcspn(s, " \t");
136908e60ed1SAndi Kleen 
137008e60ed1SAndi Kleen 		if (column + wlen >= max && column > start) {
137108e60ed1SAndi Kleen 			printf("\n%*s", start, "");
137208e60ed1SAndi Kleen 			column = start + corr;
137308e60ed1SAndi Kleen 		}
137408e60ed1SAndi Kleen 		n = printf("%s%.*s", column > start ? " " : "", wlen, s);
137508e60ed1SAndi Kleen 		if (n <= 0)
137608e60ed1SAndi Kleen 			break;
137708e60ed1SAndi Kleen 		s += wlen;
137808e60ed1SAndi Kleen 		column += n;
137932858480SArnaldo Carvalho de Melo 		s = skip_spaces(s);
138008e60ed1SAndi Kleen 	}
1381dc098b35SAndi Kleen }
1382dc098b35SAndi Kleen 
1383c8d6828aSSukadev Bhattiprolu void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag,
1384bf874fcfSAndi Kleen 			bool long_desc, bool details_flag)
1385dc098b35SAndi Kleen {
1386dc098b35SAndi Kleen 	struct perf_pmu *pmu;
1387dc098b35SAndi Kleen 	struct perf_pmu_alias *alias;
1388dc098b35SAndi Kleen 	char buf[1024];
1389dc098b35SAndi Kleen 	int printed = 0;
1390dc098b35SAndi Kleen 	int len, j;
1391dd5f1036SAndi Kleen 	struct sevent *aliases;
139208e60ed1SAndi Kleen 	int numdesc = 0;
139361eb2eb4SAndi Kleen 	int columns = pager_get_columns();
1394dd5f1036SAndi Kleen 	char *topic = NULL;
1395dc098b35SAndi Kleen 
1396dc098b35SAndi Kleen 	pmu = NULL;
1397dc098b35SAndi Kleen 	len = 0;
139842634bc7SAdrian Hunter 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1399dc098b35SAndi Kleen 		list_for_each_entry(alias, &pmu->aliases, list)
1400dc098b35SAndi Kleen 			len++;
140142634bc7SAdrian Hunter 		if (pmu->selectable)
140242634bc7SAdrian Hunter 			len++;
140342634bc7SAdrian Hunter 	}
1404dd5f1036SAndi Kleen 	aliases = zalloc(sizeof(struct sevent) * len);
1405dc098b35SAndi Kleen 	if (!aliases)
14067e4772dcSArnaldo Carvalho de Melo 		goto out_enomem;
1407dc098b35SAndi Kleen 	pmu = NULL;
1408dc098b35SAndi Kleen 	j = 0;
140942634bc7SAdrian Hunter 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1410dc098b35SAndi Kleen 		list_for_each_entry(alias, &pmu->aliases, list) {
141108e60ed1SAndi Kleen 			char *name = alias->desc ? alias->name :
141208e60ed1SAndi Kleen 				format_alias(buf, sizeof(buf), pmu, alias);
1413dc098b35SAndi Kleen 			bool is_cpu = !strcmp(pmu->name, "cpu");
1414dc098b35SAndi Kleen 
1415dc098b35SAndi Kleen 			if (event_glob != NULL &&
141638d14f0cSAndi Kleen 			    !(strglobmatch_nocase(name, event_glob) ||
141738d14f0cSAndi Kleen 			      (!is_cpu && strglobmatch_nocase(alias->name,
141867bdc35fSAndi Kleen 						       event_glob)) ||
141967bdc35fSAndi Kleen 			      (alias->topic &&
142067bdc35fSAndi Kleen 			       strglobmatch_nocase(alias->topic, event_glob))))
1421dc098b35SAndi Kleen 				continue;
14227e4772dcSArnaldo Carvalho de Melo 
142308e60ed1SAndi Kleen 			if (is_cpu && !name_only && !alias->desc)
14247e4772dcSArnaldo Carvalho de Melo 				name = format_alias_or(buf, sizeof(buf), pmu, alias);
14257e4772dcSArnaldo Carvalho de Melo 
142608e60ed1SAndi Kleen 			aliases[j].name = name;
142708e60ed1SAndi Kleen 			if (is_cpu && !name_only && !alias->desc)
142808e60ed1SAndi Kleen 				aliases[j].name = format_alias_or(buf,
142908e60ed1SAndi Kleen 								  sizeof(buf),
143008e60ed1SAndi Kleen 								  pmu, alias);
143108e60ed1SAndi Kleen 			aliases[j].name = strdup(aliases[j].name);
143208e60ed1SAndi Kleen 			if (!aliases[j].name)
14337e4772dcSArnaldo Carvalho de Melo 				goto out_enomem;
143408e60ed1SAndi Kleen 
1435c8d6828aSSukadev Bhattiprolu 			aliases[j].desc = long_desc ? alias->long_desc :
1436c8d6828aSSukadev Bhattiprolu 						alias->desc;
1437dd5f1036SAndi Kleen 			aliases[j].topic = alias->topic;
1438f2361024SAndi Kleen 			aliases[j].str = alias->str;
1439f2361024SAndi Kleen 			aliases[j].pmu = pmu->name;
14407f372a63SAndi Kleen 			aliases[j].metric_expr = alias->metric_expr;
144196284814SAndi Kleen 			aliases[j].metric_name = alias->metric_name;
1442dc098b35SAndi Kleen 			j++;
1443dc098b35SAndi Kleen 		}
1444fa52ceabSArnaldo Carvalho de Melo 		if (pmu->selectable &&
1445fa52ceabSArnaldo Carvalho de Melo 		    (event_glob == NULL || strglobmatch(pmu->name, event_glob))) {
14467e4772dcSArnaldo Carvalho de Melo 			char *s;
14477e4772dcSArnaldo Carvalho de Melo 			if (asprintf(&s, "%s//", pmu->name) < 0)
14487e4772dcSArnaldo Carvalho de Melo 				goto out_enomem;
144908e60ed1SAndi Kleen 			aliases[j].name = s;
145042634bc7SAdrian Hunter 			j++;
145142634bc7SAdrian Hunter 		}
145242634bc7SAdrian Hunter 	}
1453dc098b35SAndi Kleen 	len = j;
1454dd5f1036SAndi Kleen 	qsort(aliases, len, sizeof(struct sevent), cmp_sevent);
1455dc098b35SAndi Kleen 	for (j = 0; j < len; j++) {
145615b22ed3SAndi Kleen 		/* Skip duplicates */
145715b22ed3SAndi Kleen 		if (j > 0 && !strcmp(aliases[j].name, aliases[j - 1].name))
145815b22ed3SAndi Kleen 			continue;
1459dc098b35SAndi Kleen 		if (name_only) {
146008e60ed1SAndi Kleen 			printf("%s ", aliases[j].name);
1461dc098b35SAndi Kleen 			continue;
1462dc098b35SAndi Kleen 		}
14631c5f01feSAndi Kleen 		if (aliases[j].desc && !quiet_flag) {
146408e60ed1SAndi Kleen 			if (numdesc++ == 0)
146508e60ed1SAndi Kleen 				printf("\n");
1466dd5f1036SAndi Kleen 			if (aliases[j].topic && (!topic ||
1467dd5f1036SAndi Kleen 					strcmp(topic, aliases[j].topic))) {
1468dd5f1036SAndi Kleen 				printf("%s%s:\n", topic ? "\n" : "",
1469dd5f1036SAndi Kleen 						aliases[j].topic);
1470dd5f1036SAndi Kleen 				topic = aliases[j].topic;
1471dd5f1036SAndi Kleen 			}
147208e60ed1SAndi Kleen 			printf("  %-50s\n", aliases[j].name);
147308e60ed1SAndi Kleen 			printf("%*s", 8, "[");
147408e60ed1SAndi Kleen 			wordwrap(aliases[j].desc, 8, columns, 0);
147508e60ed1SAndi Kleen 			printf("]\n");
1476bf874fcfSAndi Kleen 			if (details_flag) {
14777f372a63SAndi Kleen 				printf("%*s%s/%s/ ", 8, "", aliases[j].pmu, aliases[j].str);
147896284814SAndi Kleen 				if (aliases[j].metric_name)
147996284814SAndi Kleen 					printf(" MetricName: %s", aliases[j].metric_name);
14807f372a63SAndi Kleen 				if (aliases[j].metric_expr)
14817f372a63SAndi Kleen 					printf(" MetricExpr: %s", aliases[j].metric_expr);
14827f372a63SAndi Kleen 				putchar('\n');
14837f372a63SAndi Kleen 			}
148408e60ed1SAndi Kleen 		} else
148508e60ed1SAndi Kleen 			printf("  %-50s [Kernel PMU event]\n", aliases[j].name);
1486dc098b35SAndi Kleen 		printed++;
1487dc098b35SAndi Kleen 	}
1488dfc431cbSArnaldo Carvalho de Melo 	if (printed && pager_in_use())
1489dc098b35SAndi Kleen 		printf("\n");
14907e4772dcSArnaldo Carvalho de Melo out_free:
14917e4772dcSArnaldo Carvalho de Melo 	for (j = 0; j < len; j++)
149208e60ed1SAndi Kleen 		zfree(&aliases[j].name);
14937e4772dcSArnaldo Carvalho de Melo 	zfree(&aliases);
14947e4772dcSArnaldo Carvalho de Melo 	return;
14957e4772dcSArnaldo Carvalho de Melo 
14967e4772dcSArnaldo Carvalho de Melo out_enomem:
14977e4772dcSArnaldo Carvalho de Melo 	printf("FATAL: not enough memory to print PMU events\n");
14987e4772dcSArnaldo Carvalho de Melo 	if (aliases)
14997e4772dcSArnaldo Carvalho de Melo 		goto out_free;
1500dc098b35SAndi Kleen }
15014cabc3d1SAndi Kleen 
15024cabc3d1SAndi Kleen bool pmu_have_event(const char *pname, const char *name)
15034cabc3d1SAndi Kleen {
15044cabc3d1SAndi Kleen 	struct perf_pmu *pmu;
15054cabc3d1SAndi Kleen 	struct perf_pmu_alias *alias;
15064cabc3d1SAndi Kleen 
15074cabc3d1SAndi Kleen 	pmu = NULL;
15084cabc3d1SAndi Kleen 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
15094cabc3d1SAndi Kleen 		if (strcmp(pname, pmu->name))
15104cabc3d1SAndi Kleen 			continue;
15114cabc3d1SAndi Kleen 		list_for_each_entry(alias, &pmu->aliases, list)
15124cabc3d1SAndi Kleen 			if (!strcmp(alias->name, name))
15134cabc3d1SAndi Kleen 				return true;
15144cabc3d1SAndi Kleen 	}
15154cabc3d1SAndi Kleen 	return false;
15164cabc3d1SAndi Kleen }
15177d4bdab5SAdrian Hunter 
15187d4bdab5SAdrian Hunter static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
15197d4bdab5SAdrian Hunter {
15207d4bdab5SAdrian Hunter 	struct stat st;
15217d4bdab5SAdrian Hunter 	char path[PATH_MAX];
15227d4bdab5SAdrian Hunter 	const char *sysfs;
15237d4bdab5SAdrian Hunter 
15247d4bdab5SAdrian Hunter 	sysfs = sysfs__mountpoint();
15257d4bdab5SAdrian Hunter 	if (!sysfs)
15267d4bdab5SAdrian Hunter 		return NULL;
15277d4bdab5SAdrian Hunter 
15287d4bdab5SAdrian Hunter 	snprintf(path, PATH_MAX,
15297d4bdab5SAdrian Hunter 		 "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name);
15307d4bdab5SAdrian Hunter 
15317d4bdab5SAdrian Hunter 	if (stat(path, &st) < 0)
15327d4bdab5SAdrian Hunter 		return NULL;
15337d4bdab5SAdrian Hunter 
15347d4bdab5SAdrian Hunter 	return fopen(path, "r");
15357d4bdab5SAdrian Hunter }
15367d4bdab5SAdrian Hunter 
15377d4bdab5SAdrian Hunter int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
15387d4bdab5SAdrian Hunter 			...)
15397d4bdab5SAdrian Hunter {
15407d4bdab5SAdrian Hunter 	va_list args;
15417d4bdab5SAdrian Hunter 	FILE *file;
15427d4bdab5SAdrian Hunter 	int ret = EOF;
15437d4bdab5SAdrian Hunter 
15447d4bdab5SAdrian Hunter 	va_start(args, fmt);
15457d4bdab5SAdrian Hunter 	file = perf_pmu__open_file(pmu, name);
15467d4bdab5SAdrian Hunter 	if (file) {
15477d4bdab5SAdrian Hunter 		ret = vfscanf(file, fmt, args);
15487d4bdab5SAdrian Hunter 		fclose(file);
15497d4bdab5SAdrian Hunter 	}
15507d4bdab5SAdrian Hunter 	va_end(args);
15517d4bdab5SAdrian Hunter 	return ret;
15527d4bdab5SAdrian Hunter }
1553