xref: /openbmc/linux/tools/perf/util/pmu.c (revision 5e51b0bb)
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>
189c3516d1SJiri Olsa #include <perf/cpumap.h>
195e51b0bbSArnaldo Carvalho de Melo #include "debug.h"
20cd82a32eSJiri Olsa #include "pmu.h"
21cd82a32eSJiri Olsa #include "parse-events.h"
227ae92e74SYan, Zheng #include "cpumap.h"
23933f82ffSSukadev Bhattiprolu #include "header.h"
24933f82ffSSukadev Bhattiprolu #include "pmu-events/pmu-events.h"
2561eb2eb4SAndi Kleen #include "cache.h"
26a067558eSArnaldo Carvalho de Melo #include "string2.h"
27cd82a32eSJiri Olsa 
28ab1bf653SArnaldo Carvalho de Melo struct perf_pmu_format {
29ab1bf653SArnaldo Carvalho de Melo 	char *name;
30ab1bf653SArnaldo Carvalho de Melo 	int value;
31ab1bf653SArnaldo Carvalho de Melo 	DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
32ab1bf653SArnaldo Carvalho de Melo 	struct list_head list;
33ab1bf653SArnaldo Carvalho de Melo };
34ab1bf653SArnaldo Carvalho de Melo 
35cd82a32eSJiri Olsa int perf_pmu_parse(struct list_head *list, char *name);
36cd82a32eSJiri Olsa extern FILE *perf_pmu_in;
37cd82a32eSJiri Olsa 
38cd82a32eSJiri Olsa static LIST_HEAD(pmus);
39cd82a32eSJiri Olsa 
40cd82a32eSJiri Olsa /*
41cd82a32eSJiri Olsa  * Parse & process all the sysfs attributes located under
42cd82a32eSJiri Olsa  * the directory specified in 'dir' parameter.
43cd82a32eSJiri Olsa  */
44cff7f956SJiri Olsa int perf_pmu__format_parse(char *dir, struct list_head *head)
45cd82a32eSJiri Olsa {
46cd82a32eSJiri Olsa 	struct dirent *evt_ent;
47cd82a32eSJiri Olsa 	DIR *format_dir;
48cd82a32eSJiri Olsa 	int ret = 0;
49cd82a32eSJiri Olsa 
50cd82a32eSJiri Olsa 	format_dir = opendir(dir);
51cd82a32eSJiri Olsa 	if (!format_dir)
52cd82a32eSJiri Olsa 		return -EINVAL;
53cd82a32eSJiri Olsa 
54cd82a32eSJiri Olsa 	while (!ret && (evt_ent = readdir(format_dir))) {
55cd82a32eSJiri Olsa 		char path[PATH_MAX];
56cd82a32eSJiri Olsa 		char *name = evt_ent->d_name;
57cd82a32eSJiri Olsa 		FILE *file;
58cd82a32eSJiri Olsa 
59cd82a32eSJiri Olsa 		if (!strcmp(name, ".") || !strcmp(name, ".."))
60cd82a32eSJiri Olsa 			continue;
61cd82a32eSJiri Olsa 
62cd82a32eSJiri Olsa 		snprintf(path, PATH_MAX, "%s/%s", dir, name);
63cd82a32eSJiri Olsa 
64cd82a32eSJiri Olsa 		ret = -EINVAL;
65cd82a32eSJiri Olsa 		file = fopen(path, "r");
66cd82a32eSJiri Olsa 		if (!file)
67cd82a32eSJiri Olsa 			break;
68cd82a32eSJiri Olsa 
69cd82a32eSJiri Olsa 		perf_pmu_in = file;
70cd82a32eSJiri Olsa 		ret = perf_pmu_parse(head, name);
71cd82a32eSJiri Olsa 		fclose(file);
72cd82a32eSJiri Olsa 	}
73cd82a32eSJiri Olsa 
74cd82a32eSJiri Olsa 	closedir(format_dir);
75cd82a32eSJiri Olsa 	return ret;
76cd82a32eSJiri Olsa }
77cd82a32eSJiri Olsa 
78cd82a32eSJiri Olsa /*
79cd82a32eSJiri Olsa  * Reading/parsing the default pmu format definition, which should be
80cd82a32eSJiri Olsa  * located at:
81cd82a32eSJiri Olsa  * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
82cd82a32eSJiri Olsa  */
83b6b96fb4SAdrian Hunter static int pmu_format(const char *name, struct list_head *format)
84cd82a32eSJiri Olsa {
85cd82a32eSJiri Olsa 	struct stat st;
86cd82a32eSJiri Olsa 	char path[PATH_MAX];
87cf38fadaSArnaldo Carvalho de Melo 	const char *sysfs = sysfs__mountpoint();
88cd82a32eSJiri Olsa 
89cd82a32eSJiri Olsa 	if (!sysfs)
90cd82a32eSJiri Olsa 		return -1;
91cd82a32eSJiri Olsa 
92cd82a32eSJiri Olsa 	snprintf(path, PATH_MAX,
9350a9667cSRobert Richter 		 "%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name);
94cd82a32eSJiri Olsa 
95cd82a32eSJiri Olsa 	if (stat(path, &st) < 0)
969bc8f9feSRobert Richter 		return 0;	/* no error if format does not exist */
97cd82a32eSJiri Olsa 
98cff7f956SJiri Olsa 	if (perf_pmu__format_parse(path, format))
99cd82a32eSJiri Olsa 		return -1;
100cd82a32eSJiri Olsa 
101cd82a32eSJiri Olsa 	return 0;
102cd82a32eSJiri Olsa }
103cd82a32eSJiri Olsa 
104d02fc6bcSAndi Kleen static int convert_scale(const char *scale, char **end, double *sval)
105d02fc6bcSAndi Kleen {
106d02fc6bcSAndi Kleen 	char *lc;
107d02fc6bcSAndi Kleen 	int ret = 0;
108d02fc6bcSAndi Kleen 
109d02fc6bcSAndi Kleen 	/*
110d02fc6bcSAndi Kleen 	 * save current locale
111d02fc6bcSAndi Kleen 	 */
112d02fc6bcSAndi Kleen 	lc = setlocale(LC_NUMERIC, NULL);
113d02fc6bcSAndi Kleen 
114d02fc6bcSAndi Kleen 	/*
115d02fc6bcSAndi Kleen 	 * The lc string may be allocated in static storage,
116d02fc6bcSAndi Kleen 	 * so get a dynamic copy to make it survive setlocale
117d02fc6bcSAndi Kleen 	 * call below.
118d02fc6bcSAndi Kleen 	 */
119d02fc6bcSAndi Kleen 	lc = strdup(lc);
120d02fc6bcSAndi Kleen 	if (!lc) {
121d02fc6bcSAndi Kleen 		ret = -ENOMEM;
122d02fc6bcSAndi Kleen 		goto out;
123d02fc6bcSAndi Kleen 	}
124d02fc6bcSAndi Kleen 
125d02fc6bcSAndi Kleen 	/*
126d02fc6bcSAndi Kleen 	 * force to C locale to ensure kernel
127d02fc6bcSAndi Kleen 	 * scale string is converted correctly.
128d02fc6bcSAndi Kleen 	 * kernel uses default C locale.
129d02fc6bcSAndi Kleen 	 */
130d02fc6bcSAndi Kleen 	setlocale(LC_NUMERIC, "C");
131d02fc6bcSAndi Kleen 
132d02fc6bcSAndi Kleen 	*sval = strtod(scale, end);
133d02fc6bcSAndi Kleen 
134d02fc6bcSAndi Kleen out:
135d02fc6bcSAndi Kleen 	/* restore locale */
136d02fc6bcSAndi Kleen 	setlocale(LC_NUMERIC, lc);
137d02fc6bcSAndi Kleen 	free(lc);
138d02fc6bcSAndi Kleen 	return ret;
139d02fc6bcSAndi Kleen }
140d02fc6bcSAndi Kleen 
141410136f5SStephane Eranian static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name)
142410136f5SStephane Eranian {
143410136f5SStephane Eranian 	struct stat st;
144410136f5SStephane Eranian 	ssize_t sret;
145410136f5SStephane Eranian 	char scale[128];
146410136f5SStephane Eranian 	int fd, ret = -1;
147410136f5SStephane Eranian 	char path[PATH_MAX];
148410136f5SStephane Eranian 
14911a64a05SBen Hutchings 	scnprintf(path, PATH_MAX, "%s/%s.scale", dir, name);
150410136f5SStephane Eranian 
151410136f5SStephane Eranian 	fd = open(path, O_RDONLY);
152410136f5SStephane Eranian 	if (fd == -1)
153410136f5SStephane Eranian 		return -1;
154410136f5SStephane Eranian 
155410136f5SStephane Eranian 	if (fstat(fd, &st) < 0)
156410136f5SStephane Eranian 		goto error;
157410136f5SStephane Eranian 
158410136f5SStephane Eranian 	sret = read(fd, scale, sizeof(scale)-1);
159410136f5SStephane Eranian 	if (sret < 0)
160410136f5SStephane Eranian 		goto error;
161410136f5SStephane Eranian 
1629ecae065SMadhavan Srinivasan 	if (scale[sret - 1] == '\n')
1639ecae065SMadhavan Srinivasan 		scale[sret - 1] = '\0';
1649ecae065SMadhavan Srinivasan 	else
165410136f5SStephane Eranian 		scale[sret] = '\0';
1669ecae065SMadhavan Srinivasan 
167d02fc6bcSAndi Kleen 	ret = convert_scale(scale, NULL, &alias->scale);
168410136f5SStephane Eranian error:
169410136f5SStephane Eranian 	close(fd);
170410136f5SStephane Eranian 	return ret;
171410136f5SStephane Eranian }
172410136f5SStephane Eranian 
173410136f5SStephane Eranian static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name)
174410136f5SStephane Eranian {
175410136f5SStephane Eranian 	char path[PATH_MAX];
176410136f5SStephane Eranian 	ssize_t sret;
177410136f5SStephane Eranian 	int fd;
178410136f5SStephane Eranian 
17911a64a05SBen Hutchings 	scnprintf(path, PATH_MAX, "%s/%s.unit", dir, name);
180410136f5SStephane Eranian 
181410136f5SStephane Eranian 	fd = open(path, O_RDONLY);
182410136f5SStephane Eranian 	if (fd == -1)
183410136f5SStephane Eranian 		return -1;
184410136f5SStephane Eranian 
185410136f5SStephane Eranian 	sret = read(fd, alias->unit, UNIT_MAX_LEN);
186410136f5SStephane Eranian 	if (sret < 0)
187410136f5SStephane Eranian 		goto error;
188410136f5SStephane Eranian 
189410136f5SStephane Eranian 	close(fd);
190410136f5SStephane Eranian 
1919ecae065SMadhavan Srinivasan 	if (alias->unit[sret - 1] == '\n')
1929ecae065SMadhavan Srinivasan 		alias->unit[sret - 1] = '\0';
1939ecae065SMadhavan Srinivasan 	else
194410136f5SStephane Eranian 		alias->unit[sret] = '\0';
195410136f5SStephane Eranian 
196410136f5SStephane Eranian 	return 0;
197410136f5SStephane Eranian error:
198410136f5SStephane Eranian 	close(fd);
199410136f5SStephane Eranian 	alias->unit[0] = '\0';
200410136f5SStephane Eranian 	return -1;
201410136f5SStephane Eranian }
202410136f5SStephane Eranian 
203044330c1SMatt Fleming static int
204044330c1SMatt Fleming perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *name)
205044330c1SMatt Fleming {
206044330c1SMatt Fleming 	char path[PATH_MAX];
207044330c1SMatt Fleming 	int fd;
208044330c1SMatt Fleming 
20911a64a05SBen Hutchings 	scnprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name);
210044330c1SMatt Fleming 
211044330c1SMatt Fleming 	fd = open(path, O_RDONLY);
212044330c1SMatt Fleming 	if (fd == -1)
213044330c1SMatt Fleming 		return -1;
214044330c1SMatt Fleming 
215044330c1SMatt Fleming 	close(fd);
216044330c1SMatt Fleming 
217044330c1SMatt Fleming 	alias->per_pkg = true;
218044330c1SMatt Fleming 	return 0;
219044330c1SMatt Fleming }
220044330c1SMatt Fleming 
2211d9e446bSJiri Olsa static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias,
2221d9e446bSJiri Olsa 				    char *dir, char *name)
2231d9e446bSJiri Olsa {
2241d9e446bSJiri Olsa 	char path[PATH_MAX];
2251d9e446bSJiri Olsa 	int fd;
2261d9e446bSJiri Olsa 
22711a64a05SBen Hutchings 	scnprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name);
2281d9e446bSJiri Olsa 
2291d9e446bSJiri Olsa 	fd = open(path, O_RDONLY);
2301d9e446bSJiri Olsa 	if (fd == -1)
2311d9e446bSJiri Olsa 		return -1;
2321d9e446bSJiri Olsa 
2331d9e446bSJiri Olsa 	alias->snapshot = true;
2341d9e446bSJiri Olsa 	close(fd);
2351d9e446bSJiri Olsa 	return 0;
2361d9e446bSJiri Olsa }
2371d9e446bSJiri Olsa 
2386dde6429SThomas Richter static void perf_pmu_assign_str(char *name, const char *field, char **old_str,
2396dde6429SThomas Richter 				char **new_str)
2406dde6429SThomas Richter {
2416dde6429SThomas Richter 	if (!*old_str)
2426dde6429SThomas Richter 		goto set_new;
2436dde6429SThomas Richter 
2446dde6429SThomas Richter 	if (*new_str) {	/* Have new string, check with old */
2456dde6429SThomas Richter 		if (strcasecmp(*old_str, *new_str))
2466dde6429SThomas Richter 			pr_debug("alias %s differs in field '%s'\n",
2476dde6429SThomas Richter 				 name, field);
2486dde6429SThomas Richter 		zfree(old_str);
2496dde6429SThomas Richter 	} else		/* Nothing new --> keep old string */
2506dde6429SThomas Richter 		return;
2516dde6429SThomas Richter set_new:
2526dde6429SThomas Richter 	*old_str = *new_str;
2536dde6429SThomas Richter 	*new_str = NULL;
2546dde6429SThomas Richter }
2556dde6429SThomas Richter 
2566dde6429SThomas Richter static void perf_pmu_update_alias(struct perf_pmu_alias *old,
2576dde6429SThomas Richter 				  struct perf_pmu_alias *newalias)
2586dde6429SThomas Richter {
2596dde6429SThomas Richter 	perf_pmu_assign_str(old->name, "desc", &old->desc, &newalias->desc);
2606dde6429SThomas Richter 	perf_pmu_assign_str(old->name, "long_desc", &old->long_desc,
2616dde6429SThomas Richter 			    &newalias->long_desc);
2626dde6429SThomas Richter 	perf_pmu_assign_str(old->name, "topic", &old->topic, &newalias->topic);
2636dde6429SThomas Richter 	perf_pmu_assign_str(old->name, "metric_expr", &old->metric_expr,
2646dde6429SThomas Richter 			    &newalias->metric_expr);
2656dde6429SThomas Richter 	perf_pmu_assign_str(old->name, "metric_name", &old->metric_name,
2666dde6429SThomas Richter 			    &newalias->metric_name);
2676dde6429SThomas Richter 	perf_pmu_assign_str(old->name, "value", &old->str, &newalias->str);
2686dde6429SThomas Richter 	old->scale = newalias->scale;
2696dde6429SThomas Richter 	old->per_pkg = newalias->per_pkg;
2706dde6429SThomas Richter 	old->snapshot = newalias->snapshot;
2716dde6429SThomas Richter 	memcpy(old->unit, newalias->unit, sizeof(old->unit));
2726dde6429SThomas Richter }
2736dde6429SThomas Richter 
2746dde6429SThomas Richter /* Delete an alias entry. */
2756dde6429SThomas Richter static void perf_pmu_free_alias(struct perf_pmu_alias *newalias)
2766dde6429SThomas Richter {
2776dde6429SThomas Richter 	zfree(&newalias->name);
2786dde6429SThomas Richter 	zfree(&newalias->desc);
2796dde6429SThomas Richter 	zfree(&newalias->long_desc);
2806dde6429SThomas Richter 	zfree(&newalias->topic);
2816dde6429SThomas Richter 	zfree(&newalias->str);
2826dde6429SThomas Richter 	zfree(&newalias->metric_expr);
2836dde6429SThomas Richter 	zfree(&newalias->metric_name);
2846dde6429SThomas Richter 	parse_events_terms__purge(&newalias->terms);
2856dde6429SThomas Richter 	free(newalias);
2866dde6429SThomas Richter }
2876dde6429SThomas Richter 
2886dde6429SThomas Richter /* Merge an alias, search in alias list. If this name is already
2896dde6429SThomas Richter  * present merge both of them to combine all information.
2906dde6429SThomas Richter  */
2916dde6429SThomas Richter static bool perf_pmu_merge_alias(struct perf_pmu_alias *newalias,
2926dde6429SThomas Richter 				 struct list_head *alist)
2936dde6429SThomas Richter {
2946dde6429SThomas Richter 	struct perf_pmu_alias *a;
2956dde6429SThomas Richter 
2966dde6429SThomas Richter 	list_for_each_entry(a, alist, list) {
2976dde6429SThomas Richter 		if (!strcasecmp(newalias->name, a->name)) {
2986dde6429SThomas Richter 			perf_pmu_update_alias(a, newalias);
2996dde6429SThomas Richter 			perf_pmu_free_alias(newalias);
3006dde6429SThomas Richter 			return true;
3016dde6429SThomas Richter 		}
3026dde6429SThomas Richter 	}
3036dde6429SThomas Richter 	return false;
3046dde6429SThomas Richter }
3056dde6429SThomas Richter 
30670c646e0SSukadev Bhattiprolu static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name,
307fedb2b51SAndi Kleen 				 char *desc, char *val,
308fedb2b51SAndi Kleen 				 char *long_desc, char *topic,
30900636c3bSAndi Kleen 				 char *unit, char *perpkg,
31096284814SAndi Kleen 				 char *metric_expr,
31196284814SAndi Kleen 				 char *metric_name)
312a6146d50SZheng Yan {
3130c24d6fbSThomas Richter 	struct parse_events_term *term;
3145c6ccc37SArnaldo Carvalho de Melo 	struct perf_pmu_alias *alias;
315a6146d50SZheng Yan 	int ret;
316fedb2b51SAndi Kleen 	int num;
3170c24d6fbSThomas Richter 	char newval[256];
318a6146d50SZheng Yan 
319a6146d50SZheng Yan 	alias = malloc(sizeof(*alias));
320a6146d50SZheng Yan 	if (!alias)
321a6146d50SZheng Yan 		return -ENOMEM;
322a6146d50SZheng Yan 
323a6146d50SZheng Yan 	INIT_LIST_HEAD(&alias->terms);
324410136f5SStephane Eranian 	alias->scale = 1.0;
325410136f5SStephane Eranian 	alias->unit[0] = '\0';
326044330c1SMatt Fleming 	alias->per_pkg = false;
32784530920SStephane Eranian 	alias->snapshot = false;
328410136f5SStephane Eranian 
32970c646e0SSukadev Bhattiprolu 	ret = parse_events_terms(&alias->terms, val);
330a6146d50SZheng Yan 	if (ret) {
33170c646e0SSukadev Bhattiprolu 		pr_err("Cannot parse alias %s: %d\n", val, ret);
332a6146d50SZheng Yan 		free(alias);
333a6146d50SZheng Yan 		return ret;
334a6146d50SZheng Yan 	}
335a6146d50SZheng Yan 
3360c24d6fbSThomas Richter 	/* Scan event and remove leading zeroes, spaces, newlines, some
3370c24d6fbSThomas Richter 	 * platforms have terms specified as
3380c24d6fbSThomas Richter 	 * event=0x0091 (read from files ../<PMU>/events/<FILE>
3390c24d6fbSThomas Richter 	 * and terms specified as event=0x91 (read from JSON files).
3400c24d6fbSThomas Richter 	 *
3410c24d6fbSThomas Richter 	 * Rebuild string to make alias->str member comparable.
3420c24d6fbSThomas Richter 	 */
3430c24d6fbSThomas Richter 	memset(newval, 0, sizeof(newval));
3440c24d6fbSThomas Richter 	ret = 0;
3450c24d6fbSThomas Richter 	list_for_each_entry(term, &alias->terms, list) {
3460c24d6fbSThomas Richter 		if (ret)
3470c24d6fbSThomas Richter 			ret += scnprintf(newval + ret, sizeof(newval) - ret,
3480c24d6fbSThomas Richter 					 ",");
3490c24d6fbSThomas Richter 		if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
3500c24d6fbSThomas Richter 			ret += scnprintf(newval + ret, sizeof(newval) - ret,
3510c24d6fbSThomas Richter 					 "%s=%#x", term->config, term->val.num);
3520c24d6fbSThomas Richter 		else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
3530c24d6fbSThomas Richter 			ret += scnprintf(newval + ret, sizeof(newval) - ret,
3540c24d6fbSThomas Richter 					 "%s=%s", term->config, term->val.str);
3550c24d6fbSThomas Richter 	}
3560c24d6fbSThomas Richter 
357a6146d50SZheng Yan 	alias->name = strdup(name);
35870c646e0SSukadev Bhattiprolu 	if (dir) {
359410136f5SStephane Eranian 		/*
360410136f5SStephane Eranian 		 * load unit name and scale if available
361410136f5SStephane Eranian 		 */
362410136f5SStephane Eranian 		perf_pmu__parse_unit(alias, dir, name);
363410136f5SStephane Eranian 		perf_pmu__parse_scale(alias, dir, name);
364044330c1SMatt Fleming 		perf_pmu__parse_per_pkg(alias, dir, name);
3651d9e446bSJiri Olsa 		perf_pmu__parse_snapshot(alias, dir, name);
36670c646e0SSukadev Bhattiprolu 	}
367410136f5SStephane Eranian 
36800636c3bSAndi Kleen 	alias->metric_expr = metric_expr ? strdup(metric_expr) : NULL;
36996284814SAndi Kleen 	alias->metric_name = metric_name ? strdup(metric_name): NULL;
37008e60ed1SAndi Kleen 	alias->desc = desc ? strdup(desc) : NULL;
371c8d6828aSSukadev Bhattiprolu 	alias->long_desc = long_desc ? strdup(long_desc) :
372c8d6828aSSukadev Bhattiprolu 				desc ? strdup(desc) : NULL;
373dd5f1036SAndi Kleen 	alias->topic = topic ? strdup(topic) : NULL;
374fedb2b51SAndi Kleen 	if (unit) {
375fedb2b51SAndi Kleen 		if (convert_scale(unit, &unit, &alias->scale) < 0)
376fedb2b51SAndi Kleen 			return -1;
377fedb2b51SAndi Kleen 		snprintf(alias->unit, sizeof(alias->unit), "%s", unit);
378fedb2b51SAndi Kleen 	}
379fedb2b51SAndi Kleen 	alias->per_pkg = perpkg && sscanf(perpkg, "%d", &num) == 1 && num == 1;
3800c24d6fbSThomas Richter 	alias->str = strdup(newval);
381f2361024SAndi Kleen 
3826dde6429SThomas Richter 	if (!perf_pmu_merge_alias(alias, list))
383a6146d50SZheng Yan 		list_add_tail(&alias->list, list);
384410136f5SStephane Eranian 
385a6146d50SZheng Yan 	return 0;
386a6146d50SZheng Yan }
387a6146d50SZheng Yan 
38870c646e0SSukadev Bhattiprolu static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file)
38970c646e0SSukadev Bhattiprolu {
39070c646e0SSukadev Bhattiprolu 	char buf[256];
39170c646e0SSukadev Bhattiprolu 	int ret;
39270c646e0SSukadev Bhattiprolu 
39370c646e0SSukadev Bhattiprolu 	ret = fread(buf, 1, sizeof(buf), file);
39470c646e0SSukadev Bhattiprolu 	if (ret == 0)
39570c646e0SSukadev Bhattiprolu 		return -EINVAL;
39670c646e0SSukadev Bhattiprolu 
39770c646e0SSukadev Bhattiprolu 	buf[ret] = 0;
39870c646e0SSukadev Bhattiprolu 
399ea23ac73SThomas Richter 	/* Remove trailing newline from sysfs file */
40013c230abSArnaldo Carvalho de Melo 	strim(buf);
401ea23ac73SThomas Richter 
402fedb2b51SAndi Kleen 	return __perf_pmu__new_alias(list, dir, name, NULL, buf, NULL, NULL, NULL,
40396284814SAndi Kleen 				     NULL, NULL, NULL);
40470c646e0SSukadev Bhattiprolu }
40570c646e0SSukadev Bhattiprolu 
40646441bdcSMatt Fleming static inline bool pmu_alias_info_file(char *name)
40746441bdcSMatt Fleming {
40846441bdcSMatt Fleming 	size_t len;
40946441bdcSMatt Fleming 
41046441bdcSMatt Fleming 	len = strlen(name);
41146441bdcSMatt Fleming 	if (len > 5 && !strcmp(name + len - 5, ".unit"))
41246441bdcSMatt Fleming 		return true;
41346441bdcSMatt Fleming 	if (len > 6 && !strcmp(name + len - 6, ".scale"))
41446441bdcSMatt Fleming 		return true;
415044330c1SMatt Fleming 	if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
416044330c1SMatt Fleming 		return true;
4171d9e446bSJiri Olsa 	if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
4181d9e446bSJiri Olsa 		return true;
41946441bdcSMatt Fleming 
42046441bdcSMatt Fleming 	return false;
42146441bdcSMatt Fleming }
42246441bdcSMatt Fleming 
423a6146d50SZheng Yan /*
424a6146d50SZheng Yan  * Process all the sysfs attributes located under the directory
425a6146d50SZheng Yan  * specified in 'dir' parameter.
426a6146d50SZheng Yan  */
427a6146d50SZheng Yan static int pmu_aliases_parse(char *dir, struct list_head *head)
428a6146d50SZheng Yan {
429a6146d50SZheng Yan 	struct dirent *evt_ent;
430a6146d50SZheng Yan 	DIR *event_dir;
431a6146d50SZheng Yan 
432a6146d50SZheng Yan 	event_dir = opendir(dir);
433a6146d50SZheng Yan 	if (!event_dir)
434a6146d50SZheng Yan 		return -EINVAL;
435a6146d50SZheng Yan 
436940db6dcSAndi Kleen 	while ((evt_ent = readdir(event_dir))) {
437a6146d50SZheng Yan 		char path[PATH_MAX];
438a6146d50SZheng Yan 		char *name = evt_ent->d_name;
439a6146d50SZheng Yan 		FILE *file;
440a6146d50SZheng Yan 
441a6146d50SZheng Yan 		if (!strcmp(name, ".") || !strcmp(name, ".."))
442a6146d50SZheng Yan 			continue;
443a6146d50SZheng Yan 
444410136f5SStephane Eranian 		/*
44546441bdcSMatt Fleming 		 * skip info files parsed in perf_pmu__new_alias()
446410136f5SStephane Eranian 		 */
44746441bdcSMatt Fleming 		if (pmu_alias_info_file(name))
448410136f5SStephane Eranian 			continue;
449410136f5SStephane Eranian 
45077f18153SJiri Olsa 		scnprintf(path, PATH_MAX, "%s/%s", dir, name);
451a6146d50SZheng Yan 
452a6146d50SZheng Yan 		file = fopen(path, "r");
453940db6dcSAndi Kleen 		if (!file) {
454940db6dcSAndi Kleen 			pr_debug("Cannot open %s\n", path);
455940db6dcSAndi Kleen 			continue;
456940db6dcSAndi Kleen 		}
457410136f5SStephane Eranian 
458940db6dcSAndi Kleen 		if (perf_pmu__new_alias(head, dir, name, file) < 0)
459940db6dcSAndi Kleen 			pr_debug("Cannot set up %s\n", name);
460a6146d50SZheng Yan 		fclose(file);
461a6146d50SZheng Yan 	}
462a6146d50SZheng Yan 
463a6146d50SZheng Yan 	closedir(event_dir);
464940db6dcSAndi Kleen 	return 0;
465a6146d50SZheng Yan }
466a6146d50SZheng Yan 
467a6146d50SZheng Yan /*
468a6146d50SZheng Yan  * Reading the pmu event aliases definition, which should be located at:
469a6146d50SZheng Yan  * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
470a6146d50SZheng Yan  */
471b6b96fb4SAdrian Hunter static int pmu_aliases(const char *name, struct list_head *head)
472a6146d50SZheng Yan {
473a6146d50SZheng Yan 	struct stat st;
474a6146d50SZheng Yan 	char path[PATH_MAX];
475cf38fadaSArnaldo Carvalho de Melo 	const char *sysfs = sysfs__mountpoint();
476a6146d50SZheng Yan 
477a6146d50SZheng Yan 	if (!sysfs)
478a6146d50SZheng Yan 		return -1;
479a6146d50SZheng Yan 
480a6146d50SZheng Yan 	snprintf(path, PATH_MAX,
481a6146d50SZheng Yan 		 "%s/bus/event_source/devices/%s/events", sysfs, name);
482a6146d50SZheng Yan 
483a6146d50SZheng Yan 	if (stat(path, &st) < 0)
4843fded963SJiri Olsa 		return 0;	 /* no error if 'events' does not exist */
485a6146d50SZheng Yan 
486a6146d50SZheng Yan 	if (pmu_aliases_parse(path, head))
487a6146d50SZheng Yan 		return -1;
488a6146d50SZheng Yan 
489a6146d50SZheng Yan 	return 0;
490a6146d50SZheng Yan }
491a6146d50SZheng Yan 
4925c6ccc37SArnaldo Carvalho de Melo static int pmu_alias_terms(struct perf_pmu_alias *alias,
493a6146d50SZheng Yan 			   struct list_head *terms)
494a6146d50SZheng Yan {
4957c2f8164SJiri Olsa 	struct parse_events_term *term, *cloned;
496a6146d50SZheng Yan 	LIST_HEAD(list);
497a6146d50SZheng Yan 	int ret;
498a6146d50SZheng Yan 
499a6146d50SZheng Yan 	list_for_each_entry(term, &alias->terms, list) {
5007c2f8164SJiri Olsa 		ret = parse_events_term__clone(&cloned, term);
501a6146d50SZheng Yan 		if (ret) {
502682dc24cSArnaldo Carvalho de Melo 			parse_events_terms__purge(&list);
503a6146d50SZheng Yan 			return ret;
504a6146d50SZheng Yan 		}
505c2f1ceadSAndi Kleen 		/*
506c2f1ceadSAndi Kleen 		 * Weak terms don't override command line options,
507c2f1ceadSAndi Kleen 		 * which we don't want for implicit terms in aliases.
508c2f1ceadSAndi Kleen 		 */
509c2f1ceadSAndi Kleen 		cloned->weak = true;
5107c2f8164SJiri Olsa 		list_add_tail(&cloned->list, &list);
511a6146d50SZheng Yan 	}
512a6146d50SZheng Yan 	list_splice(&list, terms);
513a6146d50SZheng Yan 	return 0;
514a6146d50SZheng Yan }
515a6146d50SZheng Yan 
516cd82a32eSJiri Olsa /*
517cd82a32eSJiri Olsa  * Reading/parsing the default pmu type value, which should be
518cd82a32eSJiri Olsa  * located at:
519cd82a32eSJiri Olsa  * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
520cd82a32eSJiri Olsa  */
521b6b96fb4SAdrian Hunter static int pmu_type(const char *name, __u32 *type)
522cd82a32eSJiri Olsa {
523cd82a32eSJiri Olsa 	struct stat st;
524cd82a32eSJiri Olsa 	char path[PATH_MAX];
525cd82a32eSJiri Olsa 	FILE *file;
526cd82a32eSJiri Olsa 	int ret = 0;
527cf38fadaSArnaldo Carvalho de Melo 	const char *sysfs = sysfs__mountpoint();
528cd82a32eSJiri Olsa 
529cd82a32eSJiri Olsa 	if (!sysfs)
530cd82a32eSJiri Olsa 		return -1;
531cd82a32eSJiri Olsa 
532cd82a32eSJiri Olsa 	snprintf(path, PATH_MAX,
53350a9667cSRobert Richter 		 "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name);
534cd82a32eSJiri Olsa 
535cd82a32eSJiri Olsa 	if (stat(path, &st) < 0)
536cd82a32eSJiri Olsa 		return -1;
537cd82a32eSJiri Olsa 
538cd82a32eSJiri Olsa 	file = fopen(path, "r");
539cd82a32eSJiri Olsa 	if (!file)
540cd82a32eSJiri Olsa 		return -EINVAL;
541cd82a32eSJiri Olsa 
542cd82a32eSJiri Olsa 	if (1 != fscanf(file, "%u", type))
543cd82a32eSJiri Olsa 		ret = -1;
544cd82a32eSJiri Olsa 
545cd82a32eSJiri Olsa 	fclose(file);
546cd82a32eSJiri Olsa 	return ret;
547cd82a32eSJiri Olsa }
548cd82a32eSJiri Olsa 
54950a9667cSRobert Richter /* Add all pmus in sysfs to pmu list: */
55050a9667cSRobert Richter static void pmu_read_sysfs(void)
55150a9667cSRobert Richter {
55250a9667cSRobert Richter 	char path[PATH_MAX];
55350a9667cSRobert Richter 	DIR *dir;
55450a9667cSRobert Richter 	struct dirent *dent;
555cf38fadaSArnaldo Carvalho de Melo 	const char *sysfs = sysfs__mountpoint();
55650a9667cSRobert Richter 
55750a9667cSRobert Richter 	if (!sysfs)
55850a9667cSRobert Richter 		return;
55950a9667cSRobert Richter 
56050a9667cSRobert Richter 	snprintf(path, PATH_MAX,
56150a9667cSRobert Richter 		 "%s" EVENT_SOURCE_DEVICE_PATH, sysfs);
56250a9667cSRobert Richter 
56350a9667cSRobert Richter 	dir = opendir(path);
56450a9667cSRobert Richter 	if (!dir)
56550a9667cSRobert Richter 		return;
56650a9667cSRobert Richter 
56750a9667cSRobert Richter 	while ((dent = readdir(dir))) {
56850a9667cSRobert Richter 		if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
56950a9667cSRobert Richter 			continue;
57050a9667cSRobert Richter 		/* add to static LIST_HEAD(pmus): */
57150a9667cSRobert Richter 		perf_pmu__find(dent->d_name);
57250a9667cSRobert Richter 	}
57350a9667cSRobert Richter 
57450a9667cSRobert Richter 	closedir(dir);
57550a9667cSRobert Richter }
57650a9667cSRobert Richter 
577f854839bSJiri Olsa static struct perf_cpu_map *__pmu_cpumask(const char *path)
57866ec1191SMark Rutland {
57966ec1191SMark Rutland 	FILE *file;
580f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
58166ec1191SMark Rutland 
58266ec1191SMark Rutland 	file = fopen(path, "r");
58366ec1191SMark Rutland 	if (!file)
58466ec1191SMark Rutland 		return NULL;
58566ec1191SMark Rutland 
5869c3516d1SJiri Olsa 	cpus = perf_cpu_map__read(file);
58766ec1191SMark Rutland 	fclose(file);
58866ec1191SMark Rutland 	return cpus;
58966ec1191SMark Rutland }
59066ec1191SMark Rutland 
59166ec1191SMark Rutland /*
59266ec1191SMark Rutland  * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64)
59366ec1191SMark Rutland  * may have a "cpus" file.
59466ec1191SMark Rutland  */
59566ec1191SMark Rutland #define CPUS_TEMPLATE_UNCORE	"%s/bus/event_source/devices/%s/cpumask"
59666ec1191SMark Rutland #define CPUS_TEMPLATE_CPU	"%s/bus/event_source/devices/%s/cpus"
59766ec1191SMark Rutland 
598f854839bSJiri Olsa static struct perf_cpu_map *pmu_cpumask(const char *name)
5997ae92e74SYan, Zheng {
6007ae92e74SYan, Zheng 	char path[PATH_MAX];
601f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
602cf38fadaSArnaldo Carvalho de Melo 	const char *sysfs = sysfs__mountpoint();
6037e3fcffeSMark Rutland 	const char *templates[] = {
60466ec1191SMark Rutland 		CPUS_TEMPLATE_UNCORE,
60566ec1191SMark Rutland 		CPUS_TEMPLATE_CPU,
6067e3fcffeSMark Rutland 		NULL
6077e3fcffeSMark Rutland 	};
6087e3fcffeSMark Rutland 	const char **template;
6097ae92e74SYan, Zheng 
6107ae92e74SYan, Zheng 	if (!sysfs)
6117ae92e74SYan, Zheng 		return NULL;
6127ae92e74SYan, Zheng 
6137e3fcffeSMark Rutland 	for (template = templates; *template; template++) {
6147e3fcffeSMark Rutland 		snprintf(path, PATH_MAX, *template, sysfs, name);
61566ec1191SMark Rutland 		cpus = __pmu_cpumask(path);
61666ec1191SMark Rutland 		if (cpus)
61766ec1191SMark Rutland 			return cpus;
6187e3fcffeSMark Rutland 	}
6197ae92e74SYan, Zheng 
6207ae92e74SYan, Zheng 	return NULL;
62166ec1191SMark Rutland }
6227ae92e74SYan, Zheng 
62366ec1191SMark Rutland static bool pmu_is_uncore(const char *name)
62466ec1191SMark Rutland {
62566ec1191SMark Rutland 	char path[PATH_MAX];
626f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
62766ec1191SMark Rutland 	const char *sysfs = sysfs__mountpoint();
6287ae92e74SYan, Zheng 
62966ec1191SMark Rutland 	snprintf(path, PATH_MAX, CPUS_TEMPLATE_UNCORE, sysfs, name);
63066ec1191SMark Rutland 	cpus = __pmu_cpumask(path);
63138f01d8dSJiri Olsa 	perf_cpu_map__put(cpus);
63266ec1191SMark Rutland 
63366ec1191SMark Rutland 	return !!cpus;
6347ae92e74SYan, Zheng }
6357ae92e74SYan, Zheng 
636933f82ffSSukadev Bhattiprolu /*
63714b22ae0SGanapatrao Kulkarni  *  PMU CORE devices have different name other than cpu in sysfs on some
638292c34c1SKan Liang  *  platforms.
639292c34c1SKan Liang  *  Looking for possible sysfs files to identify the arm core device.
64014b22ae0SGanapatrao Kulkarni  */
641292c34c1SKan Liang static int is_arm_pmu_core(const char *name)
64214b22ae0SGanapatrao Kulkarni {
64314b22ae0SGanapatrao Kulkarni 	struct stat st;
64414b22ae0SGanapatrao Kulkarni 	char path[PATH_MAX];
64514b22ae0SGanapatrao Kulkarni 	const char *sysfs = sysfs__mountpoint();
64614b22ae0SGanapatrao Kulkarni 
64714b22ae0SGanapatrao Kulkarni 	if (!sysfs)
64814b22ae0SGanapatrao Kulkarni 		return 0;
64914b22ae0SGanapatrao Kulkarni 
65014b22ae0SGanapatrao Kulkarni 	/* Look for cpu sysfs (specific to arm) */
65114b22ae0SGanapatrao Kulkarni 	scnprintf(path, PATH_MAX, "%s/bus/event_source/devices/%s/cpus",
65214b22ae0SGanapatrao Kulkarni 				sysfs, name);
65314b22ae0SGanapatrao Kulkarni 	if (stat(path, &st) == 0)
65414b22ae0SGanapatrao Kulkarni 		return 1;
65514b22ae0SGanapatrao Kulkarni 
65614b22ae0SGanapatrao Kulkarni 	return 0;
65714b22ae0SGanapatrao Kulkarni }
65814b22ae0SGanapatrao Kulkarni 
65954e32dc0SGanapatrao Kulkarni static char *perf_pmu__getcpuid(struct perf_pmu *pmu)
660d77ade9fSAndi Kleen {
661d77ade9fSAndi Kleen 	char *cpuid;
662d77ade9fSAndi Kleen 	static bool printed;
663d77ade9fSAndi Kleen 
664d77ade9fSAndi Kleen 	cpuid = getenv("PERF_CPUID");
665d77ade9fSAndi Kleen 	if (cpuid)
666d77ade9fSAndi Kleen 		cpuid = strdup(cpuid);
667d77ade9fSAndi Kleen 	if (!cpuid)
66854e32dc0SGanapatrao Kulkarni 		cpuid = get_cpuid_str(pmu);
669d77ade9fSAndi Kleen 	if (!cpuid)
670d77ade9fSAndi Kleen 		return NULL;
671d77ade9fSAndi Kleen 
672d77ade9fSAndi Kleen 	if (!printed) {
673d77ade9fSAndi Kleen 		pr_debug("Using CPUID %s\n", cpuid);
674d77ade9fSAndi Kleen 		printed = true;
675d77ade9fSAndi Kleen 	}
676d77ade9fSAndi Kleen 	return cpuid;
677d77ade9fSAndi Kleen }
678d77ade9fSAndi Kleen 
67954e32dc0SGanapatrao Kulkarni struct pmu_events_map *perf_pmu__find_map(struct perf_pmu *pmu)
680d77ade9fSAndi Kleen {
681d77ade9fSAndi Kleen 	struct pmu_events_map *map;
68254e32dc0SGanapatrao Kulkarni 	char *cpuid = perf_pmu__getcpuid(pmu);
683d77ade9fSAndi Kleen 	int i;
684d77ade9fSAndi Kleen 
685de3d0f12SGanapatrao Kulkarni 	/* on some platforms which uses cpus map, cpuid can be NULL for
686de3d0f12SGanapatrao Kulkarni 	 * PMUs other than CORE PMUs.
687de3d0f12SGanapatrao Kulkarni 	 */
688de3d0f12SGanapatrao Kulkarni 	if (!cpuid)
689de3d0f12SGanapatrao Kulkarni 		return NULL;
690de3d0f12SGanapatrao Kulkarni 
691d77ade9fSAndi Kleen 	i = 0;
692d77ade9fSAndi Kleen 	for (;;) {
693d77ade9fSAndi Kleen 		map = &pmu_events_map[i++];
694d77ade9fSAndi Kleen 		if (!map->table) {
695d77ade9fSAndi Kleen 			map = NULL;
696d77ade9fSAndi Kleen 			break;
697d77ade9fSAndi Kleen 		}
698d77ade9fSAndi Kleen 
6994cb7d3ecSThomas Richter 		if (!strcmp_cpuid_str(map->cpuid, cpuid))
700d77ade9fSAndi Kleen 			break;
701d77ade9fSAndi Kleen 	}
702d77ade9fSAndi Kleen 	free(cpuid);
703d77ade9fSAndi Kleen 	return map;
704d77ade9fSAndi Kleen }
705d77ade9fSAndi Kleen 
706730670b1SJohn Garry static bool pmu_uncore_alias_match(const char *pmu_name, const char *name)
707730670b1SJohn Garry {
708730670b1SJohn Garry 	char *tmp = NULL, *tok, *str;
709730670b1SJohn Garry 	bool res;
710730670b1SJohn Garry 
711730670b1SJohn Garry 	str = strdup(pmu_name);
712730670b1SJohn Garry 	if (!str)
713730670b1SJohn Garry 		return false;
714730670b1SJohn Garry 
715730670b1SJohn Garry 	/*
716730670b1SJohn Garry 	 * uncore alias may be from different PMU with common prefix
717730670b1SJohn Garry 	 */
718730670b1SJohn Garry 	tok = strtok_r(str, ",", &tmp);
719730670b1SJohn Garry 	if (strncmp(pmu_name, tok, strlen(tok))) {
720730670b1SJohn Garry 		res = false;
721730670b1SJohn Garry 		goto out;
722730670b1SJohn Garry 	}
723730670b1SJohn Garry 
724730670b1SJohn Garry 	/*
725730670b1SJohn Garry 	 * Match more complex aliases where the alias name is a comma-delimited
726730670b1SJohn Garry 	 * list of tokens, orderly contained in the matching PMU name.
727730670b1SJohn Garry 	 *
728730670b1SJohn Garry 	 * Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we
729730670b1SJohn Garry 	 *	    match "socket" in "socketX_pmunameY" and then "pmuname" in
730730670b1SJohn Garry 	 *	    "pmunameY".
731730670b1SJohn Garry 	 */
732730670b1SJohn Garry 	for (; tok; name += strlen(tok), tok = strtok_r(NULL, ",", &tmp)) {
733730670b1SJohn Garry 		name = strstr(name, tok);
734730670b1SJohn Garry 		if (!name) {
735730670b1SJohn Garry 			res = false;
736730670b1SJohn Garry 			goto out;
737730670b1SJohn Garry 		}
738730670b1SJohn Garry 	}
739730670b1SJohn Garry 
740730670b1SJohn Garry 	res = true;
741730670b1SJohn Garry out:
742730670b1SJohn Garry 	free(str);
743730670b1SJohn Garry 	return res;
744730670b1SJohn Garry }
745730670b1SJohn Garry 
746933f82ffSSukadev Bhattiprolu /*
747933f82ffSSukadev Bhattiprolu  * From the pmu_events_map, find the table of PMU events that corresponds
748933f82ffSSukadev Bhattiprolu  * to the current running CPU. Then, add all PMU events from that table
749933f82ffSSukadev Bhattiprolu  * as aliases.
750933f82ffSSukadev Bhattiprolu  */
75154e32dc0SGanapatrao Kulkarni static void pmu_add_cpu_aliases(struct list_head *head, struct perf_pmu *pmu)
752933f82ffSSukadev Bhattiprolu {
753933f82ffSSukadev Bhattiprolu 	int i;
754933f82ffSSukadev Bhattiprolu 	struct pmu_events_map *map;
75554e32dc0SGanapatrao Kulkarni 	const char *name = pmu->name;
756933f82ffSSukadev Bhattiprolu 
75754e32dc0SGanapatrao Kulkarni 	map = perf_pmu__find_map(pmu);
758d77ade9fSAndi Kleen 	if (!map)
759933f82ffSSukadev Bhattiprolu 		return;
760933f82ffSSukadev Bhattiprolu 
761933f82ffSSukadev Bhattiprolu 	/*
762933f82ffSSukadev Bhattiprolu 	 * Found a matching PMU events table. Create aliases
763933f82ffSSukadev Bhattiprolu 	 */
764933f82ffSSukadev Bhattiprolu 	i = 0;
765933f82ffSSukadev Bhattiprolu 	while (1) {
766599ee18fSJohn Garry 		const char *cpu_name = is_arm_pmu_core(name) ? name : "cpu";
767599ee18fSJohn Garry 		struct pmu_event *pe = &map->table[i++];
768599ee18fSJohn Garry 		const char *pname = pe->pmu ? pe->pmu : cpu_name;
769fedb2b51SAndi Kleen 
770b18f3e36SAndi Kleen 		if (!pe->name) {
771b18f3e36SAndi Kleen 			if (pe->metric_group || pe->metric_name)
772b18f3e36SAndi Kleen 				continue;
773933f82ffSSukadev Bhattiprolu 			break;
774b18f3e36SAndi Kleen 		}
775933f82ffSSukadev Bhattiprolu 
776e94d6b7fSKan Liang 		if (pmu_is_uncore(name) &&
777730670b1SJohn Garry 		    pmu_uncore_alias_match(pname, name))
778e94d6b7fSKan Liang 			goto new_alias;
779e94d6b7fSKan Liang 
780ea1fa48cSThomas Richter 		if (strcmp(pname, name))
78114b22ae0SGanapatrao Kulkarni 			continue;
782fedb2b51SAndi Kleen 
783e94d6b7fSKan Liang new_alias:
784933f82ffSSukadev Bhattiprolu 		/* need type casts to override 'const' */
785933f82ffSSukadev Bhattiprolu 		__perf_pmu__new_alias(head, NULL, (char *)pe->name,
786c8d6828aSSukadev Bhattiprolu 				(char *)pe->desc, (char *)pe->event,
787fedb2b51SAndi Kleen 				(char *)pe->long_desc, (char *)pe->topic,
78800636c3bSAndi Kleen 				(char *)pe->unit, (char *)pe->perpkg,
78996284814SAndi Kleen 				(char *)pe->metric_expr,
79096284814SAndi Kleen 				(char *)pe->metric_name);
791933f82ffSSukadev Bhattiprolu 	}
792933f82ffSSukadev Bhattiprolu }
793933f82ffSSukadev Bhattiprolu 
794c5de47f2SSukadev Bhattiprolu struct perf_event_attr * __weak
795dc0a6202SAdrian Hunter perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
796dc0a6202SAdrian Hunter {
797dc0a6202SAdrian Hunter 	return NULL;
798dc0a6202SAdrian Hunter }
799dc0a6202SAdrian Hunter 
80090a86bdeSJiri Olsa static int pmu_max_precise(const char *name)
80190a86bdeSJiri Olsa {
80290a86bdeSJiri Olsa 	char path[PATH_MAX];
80390a86bdeSJiri Olsa 	int max_precise = -1;
80490a86bdeSJiri Olsa 
80590a86bdeSJiri Olsa 	scnprintf(path, PATH_MAX,
80690a86bdeSJiri Olsa 		 "bus/event_source/devices/%s/caps/max_precise",
80790a86bdeSJiri Olsa 		 name);
80890a86bdeSJiri Olsa 
80990a86bdeSJiri Olsa 	sysfs__read_int(path, &max_precise);
81090a86bdeSJiri Olsa 	return max_precise;
81190a86bdeSJiri Olsa }
81290a86bdeSJiri Olsa 
813b6b96fb4SAdrian Hunter static struct perf_pmu *pmu_lookup(const char *name)
814cd82a32eSJiri Olsa {
815cd82a32eSJiri Olsa 	struct perf_pmu *pmu;
816cd82a32eSJiri Olsa 	LIST_HEAD(format);
817a6146d50SZheng Yan 	LIST_HEAD(aliases);
818cd82a32eSJiri Olsa 	__u32 type;
819cd82a32eSJiri Olsa 
820cd82a32eSJiri Olsa 	/*
821cd82a32eSJiri Olsa 	 * The pmu data we store & need consists of the pmu
822cd82a32eSJiri Olsa 	 * type value and format definitions. Load both right
823cd82a32eSJiri Olsa 	 * now.
824cd82a32eSJiri Olsa 	 */
825cd82a32eSJiri Olsa 	if (pmu_format(name, &format))
826cd82a32eSJiri Olsa 		return NULL;
827cd82a32eSJiri Olsa 
82815b22ed3SAndi Kleen 	/*
82915b22ed3SAndi Kleen 	 * Check the type first to avoid unnecessary work.
83015b22ed3SAndi Kleen 	 */
83115b22ed3SAndi Kleen 	if (pmu_type(name, &type))
83215b22ed3SAndi Kleen 		return NULL;
83315b22ed3SAndi Kleen 
8343fded963SJiri Olsa 	if (pmu_aliases(name, &aliases))
8353fded963SJiri Olsa 		return NULL;
8363fded963SJiri Olsa 
837cd82a32eSJiri Olsa 	pmu = zalloc(sizeof(*pmu));
838cd82a32eSJiri Olsa 	if (!pmu)
839cd82a32eSJiri Olsa 		return NULL;
840cd82a32eSJiri Olsa 
8417ae92e74SYan, Zheng 	pmu->cpus = pmu_cpumask(name);
84254e32dc0SGanapatrao Kulkarni 	pmu->name = strdup(name);
84354e32dc0SGanapatrao Kulkarni 	pmu->type = type;
84466ec1191SMark Rutland 	pmu->is_uncore = pmu_is_uncore(name);
84590a86bdeSJiri Olsa 	pmu->max_precise = pmu_max_precise(name);
84654e32dc0SGanapatrao Kulkarni 	pmu_add_cpu_aliases(&aliases, pmu);
84766ec1191SMark Rutland 
848cd82a32eSJiri Olsa 	INIT_LIST_HEAD(&pmu->format);
849a6146d50SZheng Yan 	INIT_LIST_HEAD(&pmu->aliases);
850cd82a32eSJiri Olsa 	list_splice(&format, &pmu->format);
851a6146d50SZheng Yan 	list_splice(&aliases, &pmu->aliases);
8529bc8f9feSRobert Richter 	list_add_tail(&pmu->list, &pmus);
853dc0a6202SAdrian Hunter 
854dc0a6202SAdrian Hunter 	pmu->default_config = perf_pmu__get_default_config(pmu);
855dc0a6202SAdrian Hunter 
856cd82a32eSJiri Olsa 	return pmu;
857cd82a32eSJiri Olsa }
858cd82a32eSJiri Olsa 
859b6b96fb4SAdrian Hunter static struct perf_pmu *pmu_find(const char *name)
860cd82a32eSJiri Olsa {
861cd82a32eSJiri Olsa 	struct perf_pmu *pmu;
862cd82a32eSJiri Olsa 
863cd82a32eSJiri Olsa 	list_for_each_entry(pmu, &pmus, list)
864cd82a32eSJiri Olsa 		if (!strcmp(pmu->name, name))
865cd82a32eSJiri Olsa 			return pmu;
866cd82a32eSJiri Olsa 
867cd82a32eSJiri Olsa 	return NULL;
868cd82a32eSJiri Olsa }
869cd82a32eSJiri Olsa 
87050a9667cSRobert Richter struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
87150a9667cSRobert Richter {
87250a9667cSRobert Richter 	/*
87350a9667cSRobert Richter 	 * pmu iterator: If pmu is NULL, we start at the begin,
87450a9667cSRobert Richter 	 * otherwise return the next pmu. Returns NULL on end.
87550a9667cSRobert Richter 	 */
87650a9667cSRobert Richter 	if (!pmu) {
87750a9667cSRobert Richter 		pmu_read_sysfs();
87850a9667cSRobert Richter 		pmu = list_prepare_entry(pmu, &pmus, list);
87950a9667cSRobert Richter 	}
88050a9667cSRobert Richter 	list_for_each_entry_continue(pmu, &pmus, list)
88150a9667cSRobert Richter 		return pmu;
88250a9667cSRobert Richter 	return NULL;
88350a9667cSRobert Richter }
88450a9667cSRobert Richter 
885b6b96fb4SAdrian Hunter struct perf_pmu *perf_pmu__find(const char *name)
886cd82a32eSJiri Olsa {
887cd82a32eSJiri Olsa 	struct perf_pmu *pmu;
888cd82a32eSJiri Olsa 
889cd82a32eSJiri Olsa 	/*
890cd82a32eSJiri Olsa 	 * Once PMU is loaded it stays in the list,
891cd82a32eSJiri Olsa 	 * so we keep us from multiple reading/parsing
892cd82a32eSJiri Olsa 	 * the pmu format definitions.
893cd82a32eSJiri Olsa 	 */
894cd82a32eSJiri Olsa 	pmu = pmu_find(name);
895cd82a32eSJiri Olsa 	if (pmu)
896cd82a32eSJiri Olsa 		return pmu;
897cd82a32eSJiri Olsa 
898cd82a32eSJiri Olsa 	return pmu_lookup(name);
899cd82a32eSJiri Olsa }
900cd82a32eSJiri Olsa 
9015c6ccc37SArnaldo Carvalho de Melo static struct perf_pmu_format *
90209ff6071SAdrian Hunter pmu_find_format(struct list_head *formats, const char *name)
903cd82a32eSJiri Olsa {
9045c6ccc37SArnaldo Carvalho de Melo 	struct perf_pmu_format *format;
905cd82a32eSJiri Olsa 
906cd82a32eSJiri Olsa 	list_for_each_entry(format, formats, list)
907cd82a32eSJiri Olsa 		if (!strcmp(format->name, name))
908cd82a32eSJiri Olsa 			return format;
909cd82a32eSJiri Olsa 
910cd82a32eSJiri Olsa 	return NULL;
911cd82a32eSJiri Olsa }
912cd82a32eSJiri Olsa 
91309ff6071SAdrian Hunter __u64 perf_pmu__format_bits(struct list_head *formats, const char *name)
91409ff6071SAdrian Hunter {
91509ff6071SAdrian Hunter 	struct perf_pmu_format *format = pmu_find_format(formats, name);
91609ff6071SAdrian Hunter 	__u64 bits = 0;
91709ff6071SAdrian Hunter 	int fbit;
91809ff6071SAdrian Hunter 
91909ff6071SAdrian Hunter 	if (!format)
92009ff6071SAdrian Hunter 		return 0;
92109ff6071SAdrian Hunter 
92209ff6071SAdrian Hunter 	for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
92309ff6071SAdrian Hunter 		bits |= 1ULL << fbit;
92409ff6071SAdrian Hunter 
92509ff6071SAdrian Hunter 	return bits;
92609ff6071SAdrian Hunter }
92709ff6071SAdrian Hunter 
928cd82a32eSJiri Olsa /*
929dc0a6202SAdrian Hunter  * Sets value based on the format definition (format parameter)
930cd82a32eSJiri Olsa  * and unformated value (value parameter).
931cd82a32eSJiri Olsa  */
932dc0a6202SAdrian Hunter static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
933dc0a6202SAdrian Hunter 			     bool zero)
934cd82a32eSJiri Olsa {
935cd82a32eSJiri Olsa 	unsigned long fbit, vbit;
936cd82a32eSJiri Olsa 
937cd82a32eSJiri Olsa 	for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
938cd82a32eSJiri Olsa 
939cd82a32eSJiri Olsa 		if (!test_bit(fbit, format))
940cd82a32eSJiri Olsa 			continue;
941cd82a32eSJiri Olsa 
942dc0a6202SAdrian Hunter 		if (value & (1llu << vbit++))
943dc0a6202SAdrian Hunter 			*v |= (1llu << fbit);
944dc0a6202SAdrian Hunter 		else if (zero)
945dc0a6202SAdrian Hunter 			*v &= ~(1llu << fbit);
946cd82a32eSJiri Olsa 	}
947cd82a32eSJiri Olsa }
948cd82a32eSJiri Olsa 
9490efe6b67SAdrian Hunter static __u64 pmu_format_max_value(const unsigned long *format)
9500efe6b67SAdrian Hunter {
9511b9caa10SJiri Olsa 	int w;
9520efe6b67SAdrian Hunter 
9531b9caa10SJiri Olsa 	w = bitmap_weight(format, PERF_PMU_FORMAT_BITS);
9541b9caa10SJiri Olsa 	if (!w)
9551b9caa10SJiri Olsa 		return 0;
9561b9caa10SJiri Olsa 	if (w < 64)
9571b9caa10SJiri Olsa 		return (1ULL << w) - 1;
9581b9caa10SJiri Olsa 	return -1;
9590efe6b67SAdrian Hunter }
9600efe6b67SAdrian Hunter 
961cd82a32eSJiri Olsa /*
962688d4dfcSCody P Schafer  * Term is a string term, and might be a param-term. Try to look up it's value
963688d4dfcSCody P Schafer  * in the remaining terms.
964688d4dfcSCody P Schafer  * - We have a term like "base-or-format-term=param-term",
965688d4dfcSCody P Schafer  * - We need to find the value supplied for "param-term" (with param-term named
966688d4dfcSCody P Schafer  *   in a config string) later on in the term list.
967688d4dfcSCody P Schafer  */
968688d4dfcSCody P Schafer static int pmu_resolve_param_term(struct parse_events_term *term,
969688d4dfcSCody P Schafer 				  struct list_head *head_terms,
970688d4dfcSCody P Schafer 				  __u64 *value)
971688d4dfcSCody P Schafer {
972688d4dfcSCody P Schafer 	struct parse_events_term *t;
973688d4dfcSCody P Schafer 
974688d4dfcSCody P Schafer 	list_for_each_entry(t, head_terms, list) {
975688d4dfcSCody P Schafer 		if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
976688d4dfcSCody P Schafer 			if (!strcmp(t->config, term->config)) {
977688d4dfcSCody P Schafer 				t->used = true;
978688d4dfcSCody P Schafer 				*value = t->val.num;
979688d4dfcSCody P Schafer 				return 0;
980688d4dfcSCody P Schafer 			}
981688d4dfcSCody P Schafer 		}
982688d4dfcSCody P Schafer 	}
983688d4dfcSCody P Schafer 
984bb963e16SNamhyung Kim 	if (verbose > 0)
985688d4dfcSCody P Schafer 		printf("Required parameter '%s' not specified\n", term->config);
986688d4dfcSCody P Schafer 
987688d4dfcSCody P Schafer 	return -1;
988688d4dfcSCody P Schafer }
989688d4dfcSCody P Schafer 
990ffeb883eSHe Kuang static char *pmu_formats_string(struct list_head *formats)
991e64b020bSJiri Olsa {
992e64b020bSJiri Olsa 	struct perf_pmu_format *format;
99311db4e29SMasami Hiramatsu 	char *str = NULL;
99411db4e29SMasami Hiramatsu 	struct strbuf buf = STRBUF_INIT;
995e64b020bSJiri Olsa 	unsigned i = 0;
996e64b020bSJiri Olsa 
997ffeb883eSHe Kuang 	if (!formats)
998e64b020bSJiri Olsa 		return NULL;
999e64b020bSJiri Olsa 
1000e64b020bSJiri Olsa 	/* sysfs exported terms */
1001ffeb883eSHe Kuang 	list_for_each_entry(format, formats, list)
100211db4e29SMasami Hiramatsu 		if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
100311db4e29SMasami Hiramatsu 			goto error;
1004e64b020bSJiri Olsa 
1005ffeb883eSHe Kuang 	str = strbuf_detach(&buf, NULL);
100611db4e29SMasami Hiramatsu error:
1007ffeb883eSHe Kuang 	strbuf_release(&buf);
1008e64b020bSJiri Olsa 
1009e64b020bSJiri Olsa 	return str;
1010e64b020bSJiri Olsa }
1011e64b020bSJiri Olsa 
1012688d4dfcSCody P Schafer /*
1013cd82a32eSJiri Olsa  * Setup one of config[12] attr members based on the
101488aca8d9SCody P Schafer  * user input data - term parameter.
1015cd82a32eSJiri Olsa  */
1016cd82a32eSJiri Olsa static int pmu_config_term(struct list_head *formats,
1017cd82a32eSJiri Olsa 			   struct perf_event_attr *attr,
1018dc0a6202SAdrian Hunter 			   struct parse_events_term *term,
1019688d4dfcSCody P Schafer 			   struct list_head *head_terms,
1020e64b020bSJiri Olsa 			   bool zero, struct parse_events_error *err)
1021cd82a32eSJiri Olsa {
10225c6ccc37SArnaldo Carvalho de Melo 	struct perf_pmu_format *format;
1023cd82a32eSJiri Olsa 	__u64 *vp;
10240efe6b67SAdrian Hunter 	__u64 val, max_val;
1025cd82a32eSJiri Olsa 
1026cd82a32eSJiri Olsa 	/*
1027688d4dfcSCody P Schafer 	 * If this is a parameter we've already used for parameterized-eval,
1028688d4dfcSCody P Schafer 	 * skip it in normal eval.
1029688d4dfcSCody P Schafer 	 */
1030688d4dfcSCody P Schafer 	if (term->used)
1031688d4dfcSCody P Schafer 		return 0;
1032688d4dfcSCody P Schafer 
1033688d4dfcSCody P Schafer 	/*
1034cd82a32eSJiri Olsa 	 * Hardcoded terms should be already in, so nothing
1035cd82a32eSJiri Olsa 	 * to be done for them.
1036cd82a32eSJiri Olsa 	 */
1037cd82a32eSJiri Olsa 	if (parse_events__is_hardcoded_term(term))
1038cd82a32eSJiri Olsa 		return 0;
1039cd82a32eSJiri Olsa 
1040cd82a32eSJiri Olsa 	format = pmu_find_format(formats, term->config);
1041688d4dfcSCody P Schafer 	if (!format) {
1042bb963e16SNamhyung Kim 		if (verbose > 0)
1043688d4dfcSCody P Schafer 			printf("Invalid event/parameter '%s'\n", term->config);
1044e64b020bSJiri Olsa 		if (err) {
1045ffeb883eSHe Kuang 			char *pmu_term = pmu_formats_string(formats);
1046ffeb883eSHe Kuang 
1047e64b020bSJiri Olsa 			err->idx  = term->err_term;
1048e64b020bSJiri Olsa 			err->str  = strdup("unknown term");
1049ffeb883eSHe Kuang 			err->help = parse_events_formats_error_string(pmu_term);
1050ffeb883eSHe Kuang 			free(pmu_term);
1051e64b020bSJiri Olsa 		}
1052cd82a32eSJiri Olsa 		return -EINVAL;
1053688d4dfcSCody P Schafer 	}
1054cd82a32eSJiri Olsa 
1055cd82a32eSJiri Olsa 	switch (format->value) {
1056cd82a32eSJiri Olsa 	case PERF_PMU_FORMAT_VALUE_CONFIG:
1057cd82a32eSJiri Olsa 		vp = &attr->config;
1058cd82a32eSJiri Olsa 		break;
1059cd82a32eSJiri Olsa 	case PERF_PMU_FORMAT_VALUE_CONFIG1:
1060cd82a32eSJiri Olsa 		vp = &attr->config1;
1061cd82a32eSJiri Olsa 		break;
1062cd82a32eSJiri Olsa 	case PERF_PMU_FORMAT_VALUE_CONFIG2:
1063cd82a32eSJiri Olsa 		vp = &attr->config2;
1064cd82a32eSJiri Olsa 		break;
1065cd82a32eSJiri Olsa 	default:
1066cd82a32eSJiri Olsa 		return -EINVAL;
1067cd82a32eSJiri Olsa 	}
1068cd82a32eSJiri Olsa 
106916fa7e82SJiri Olsa 	/*
1070688d4dfcSCody P Schafer 	 * Either directly use a numeric term, or try to translate string terms
1071688d4dfcSCody P Schafer 	 * using event parameters.
107216fa7e82SJiri Olsa 	 */
107399e7138eSJiri Olsa 	if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
107499e7138eSJiri Olsa 		if (term->no_value &&
107599e7138eSJiri Olsa 		    bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
107699e7138eSJiri Olsa 			if (err) {
107799e7138eSJiri Olsa 				err->idx = term->err_val;
107899e7138eSJiri Olsa 				err->str = strdup("no value assigned for term");
107999e7138eSJiri Olsa 			}
108099e7138eSJiri Olsa 			return -EINVAL;
108199e7138eSJiri Olsa 		}
108299e7138eSJiri Olsa 
1083688d4dfcSCody P Schafer 		val = term->val.num;
108499e7138eSJiri Olsa 	} else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1085688d4dfcSCody P Schafer 		if (strcmp(term->val.str, "?")) {
1086bb963e16SNamhyung Kim 			if (verbose > 0) {
1087688d4dfcSCody P Schafer 				pr_info("Invalid sysfs entry %s=%s\n",
1088688d4dfcSCody P Schafer 						term->config, term->val.str);
1089e64b020bSJiri Olsa 			}
1090e64b020bSJiri Olsa 			if (err) {
1091e64b020bSJiri Olsa 				err->idx = term->err_val;
1092e64b020bSJiri Olsa 				err->str = strdup("expected numeric value");
1093e64b020bSJiri Olsa 			}
1094688d4dfcSCody P Schafer 			return -EINVAL;
1095688d4dfcSCody P Schafer 		}
1096688d4dfcSCody P Schafer 
1097688d4dfcSCody P Schafer 		if (pmu_resolve_param_term(term, head_terms, &val))
1098688d4dfcSCody P Schafer 			return -EINVAL;
1099688d4dfcSCody P Schafer 	} else
1100688d4dfcSCody P Schafer 		return -EINVAL;
1101688d4dfcSCody P Schafer 
11020efe6b67SAdrian Hunter 	max_val = pmu_format_max_value(format->bits);
11030efe6b67SAdrian Hunter 	if (val > max_val) {
11040efe6b67SAdrian Hunter 		if (err) {
11050efe6b67SAdrian Hunter 			err->idx = term->err_val;
11060efe6b67SAdrian Hunter 			if (asprintf(&err->str,
11070efe6b67SAdrian Hunter 				     "value too big for format, maximum is %llu",
11080efe6b67SAdrian Hunter 				     (unsigned long long)max_val) < 0)
11090efe6b67SAdrian Hunter 				err->str = strdup("value too big for format");
11100efe6b67SAdrian Hunter 			return -EINVAL;
11110efe6b67SAdrian Hunter 		}
11120efe6b67SAdrian Hunter 		/*
11130efe6b67SAdrian Hunter 		 * Assume we don't care if !err, in which case the value will be
11140efe6b67SAdrian Hunter 		 * silently truncated.
11150efe6b67SAdrian Hunter 		 */
11160efe6b67SAdrian Hunter 	}
11170efe6b67SAdrian Hunter 
1118688d4dfcSCody P Schafer 	pmu_format_value(format->bits, val, vp, zero);
1119cd82a32eSJiri Olsa 	return 0;
1120cd82a32eSJiri Olsa }
1121cd82a32eSJiri Olsa 
1122cff7f956SJiri Olsa int perf_pmu__config_terms(struct list_head *formats,
1123cff7f956SJiri Olsa 			   struct perf_event_attr *attr,
1124dc0a6202SAdrian Hunter 			   struct list_head *head_terms,
1125e64b020bSJiri Olsa 			   bool zero, struct parse_events_error *err)
1126cd82a32eSJiri Olsa {
11276cee6cd3SArnaldo Carvalho de Melo 	struct parse_events_term *term;
1128cd82a32eSJiri Olsa 
1129688d4dfcSCody P Schafer 	list_for_each_entry(term, head_terms, list) {
1130e64b020bSJiri Olsa 		if (pmu_config_term(formats, attr, term, head_terms,
1131e64b020bSJiri Olsa 				    zero, err))
1132cd82a32eSJiri Olsa 			return -EINVAL;
1133688d4dfcSCody P Schafer 	}
1134cd82a32eSJiri Olsa 
1135cd82a32eSJiri Olsa 	return 0;
1136cd82a32eSJiri Olsa }
1137cd82a32eSJiri Olsa 
1138cd82a32eSJiri Olsa /*
1139cd82a32eSJiri Olsa  * Configures event's 'attr' parameter based on the:
1140cd82a32eSJiri Olsa  * 1) users input - specified in terms parameter
1141cd82a32eSJiri Olsa  * 2) pmu format definitions - specified by pmu parameter
1142cd82a32eSJiri Olsa  */
1143cd82a32eSJiri Olsa int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
1144e64b020bSJiri Olsa 		     struct list_head *head_terms,
1145e64b020bSJiri Olsa 		     struct parse_events_error *err)
1146cd82a32eSJiri Olsa {
1147dc0a6202SAdrian Hunter 	bool zero = !!pmu->default_config;
1148dc0a6202SAdrian Hunter 
1149cd82a32eSJiri Olsa 	attr->type = pmu->type;
1150e64b020bSJiri Olsa 	return perf_pmu__config_terms(&pmu->format, attr, head_terms,
1151e64b020bSJiri Olsa 				      zero, err);
1152cd82a32eSJiri Olsa }
1153cd82a32eSJiri Olsa 
11545c6ccc37SArnaldo Carvalho de Melo static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
11556cee6cd3SArnaldo Carvalho de Melo 					     struct parse_events_term *term)
1156a6146d50SZheng Yan {
11575c6ccc37SArnaldo Carvalho de Melo 	struct perf_pmu_alias *alias;
1158a6146d50SZheng Yan 	char *name;
1159a6146d50SZheng Yan 
1160a6146d50SZheng Yan 	if (parse_events__is_hardcoded_term(term))
1161a6146d50SZheng Yan 		return NULL;
1162a6146d50SZheng Yan 
1163a6146d50SZheng Yan 	if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1164a6146d50SZheng Yan 		if (term->val.num != 1)
1165a6146d50SZheng Yan 			return NULL;
1166a6146d50SZheng Yan 		if (pmu_find_format(&pmu->format, term->config))
1167a6146d50SZheng Yan 			return NULL;
1168a6146d50SZheng Yan 		name = term->config;
1169a6146d50SZheng Yan 	} else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1170a6146d50SZheng Yan 		if (strcasecmp(term->config, "event"))
1171a6146d50SZheng Yan 			return NULL;
1172a6146d50SZheng Yan 		name = term->val.str;
1173a6146d50SZheng Yan 	} else {
1174a6146d50SZheng Yan 		return NULL;
1175a6146d50SZheng Yan 	}
1176a6146d50SZheng Yan 
1177a6146d50SZheng Yan 	list_for_each_entry(alias, &pmu->aliases, list) {
1178a6146d50SZheng Yan 		if (!strcasecmp(alias->name, name))
1179a6146d50SZheng Yan 			return alias;
1180a6146d50SZheng Yan 	}
1181a6146d50SZheng Yan 	return NULL;
1182a6146d50SZheng Yan }
1183a6146d50SZheng Yan 
1184410136f5SStephane Eranian 
11851d9e446bSJiri Olsa static int check_info_data(struct perf_pmu_alias *alias,
11861d9e446bSJiri Olsa 			   struct perf_pmu_info *info)
1187410136f5SStephane Eranian {
1188410136f5SStephane Eranian 	/*
1189410136f5SStephane Eranian 	 * Only one term in event definition can
11901d9e446bSJiri Olsa 	 * define unit, scale and snapshot, fail
11911d9e446bSJiri Olsa 	 * if there's more than one.
1192410136f5SStephane Eranian 	 */
1193b30a7d1fSArnaldo Carvalho de Melo 	if ((info->unit && alias->unit[0]) ||
11941d9e446bSJiri Olsa 	    (info->scale && alias->scale) ||
11951d9e446bSJiri Olsa 	    (info->snapshot && alias->snapshot))
1196410136f5SStephane Eranian 		return -EINVAL;
1197410136f5SStephane Eranian 
1198b30a7d1fSArnaldo Carvalho de Melo 	if (alias->unit[0])
11991d9e446bSJiri Olsa 		info->unit = alias->unit;
1200410136f5SStephane Eranian 
1201410136f5SStephane Eranian 	if (alias->scale)
12021d9e446bSJiri Olsa 		info->scale = alias->scale;
12031d9e446bSJiri Olsa 
12041d9e446bSJiri Olsa 	if (alias->snapshot)
12051d9e446bSJiri Olsa 		info->snapshot = alias->snapshot;
1206410136f5SStephane Eranian 
1207410136f5SStephane Eranian 	return 0;
1208410136f5SStephane Eranian }
1209410136f5SStephane Eranian 
1210a6146d50SZheng Yan /*
1211a6146d50SZheng Yan  * Find alias in the terms list and replace it with the terms
1212a6146d50SZheng Yan  * defined for the alias
1213a6146d50SZheng Yan  */
1214410136f5SStephane Eranian int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
121546441bdcSMatt Fleming 			  struct perf_pmu_info *info)
1216a6146d50SZheng Yan {
12176cee6cd3SArnaldo Carvalho de Melo 	struct parse_events_term *term, *h;
12185c6ccc37SArnaldo Carvalho de Melo 	struct perf_pmu_alias *alias;
1219a6146d50SZheng Yan 	int ret;
1220a6146d50SZheng Yan 
1221044330c1SMatt Fleming 	info->per_pkg = false;
1222044330c1SMatt Fleming 
12238a398897SStephane Eranian 	/*
12248a398897SStephane Eranian 	 * Mark unit and scale as not set
12258a398897SStephane Eranian 	 * (different from default values, see below)
12268a398897SStephane Eranian 	 */
122746441bdcSMatt Fleming 	info->unit     = NULL;
122846441bdcSMatt Fleming 	info->scale    = 0.0;
12291d9e446bSJiri Olsa 	info->snapshot = false;
123037932c18SAndi Kleen 	info->metric_expr = NULL;
123196284814SAndi Kleen 	info->metric_name = NULL;
1232410136f5SStephane Eranian 
1233a6146d50SZheng Yan 	list_for_each_entry_safe(term, h, head_terms, list) {
1234a6146d50SZheng Yan 		alias = pmu_find_alias(pmu, term);
1235a6146d50SZheng Yan 		if (!alias)
1236a6146d50SZheng Yan 			continue;
1237a6146d50SZheng Yan 		ret = pmu_alias_terms(alias, &term->list);
1238a6146d50SZheng Yan 		if (ret)
1239a6146d50SZheng Yan 			return ret;
1240410136f5SStephane Eranian 
12411d9e446bSJiri Olsa 		ret = check_info_data(alias, info);
1242410136f5SStephane Eranian 		if (ret)
1243410136f5SStephane Eranian 			return ret;
1244410136f5SStephane Eranian 
1245044330c1SMatt Fleming 		if (alias->per_pkg)
1246044330c1SMatt Fleming 			info->per_pkg = true;
124737932c18SAndi Kleen 		info->metric_expr = alias->metric_expr;
124896284814SAndi Kleen 		info->metric_name = alias->metric_name;
1249044330c1SMatt Fleming 
1250e56fbc9dSArnaldo Carvalho de Melo 		list_del_init(&term->list);
1251a6146d50SZheng Yan 		free(term);
1252a6146d50SZheng Yan 	}
12538a398897SStephane Eranian 
12548a398897SStephane Eranian 	/*
12558a398897SStephane Eranian 	 * if no unit or scale foundin aliases, then
12568a398897SStephane Eranian 	 * set defaults as for evsel
12578a398897SStephane Eranian 	 * unit cannot left to NULL
12588a398897SStephane Eranian 	 */
125946441bdcSMatt Fleming 	if (info->unit == NULL)
126046441bdcSMatt Fleming 		info->unit   = "";
12618a398897SStephane Eranian 
126246441bdcSMatt Fleming 	if (info->scale == 0.0)
126346441bdcSMatt Fleming 		info->scale  = 1.0;
12648a398897SStephane Eranian 
1265a6146d50SZheng Yan 	return 0;
1266a6146d50SZheng Yan }
1267a6146d50SZheng Yan 
1268cd82a32eSJiri Olsa int perf_pmu__new_format(struct list_head *list, char *name,
1269cd82a32eSJiri Olsa 			 int config, unsigned long *bits)
1270cd82a32eSJiri Olsa {
12715c6ccc37SArnaldo Carvalho de Melo 	struct perf_pmu_format *format;
1272cd82a32eSJiri Olsa 
1273cd82a32eSJiri Olsa 	format = zalloc(sizeof(*format));
1274cd82a32eSJiri Olsa 	if (!format)
1275cd82a32eSJiri Olsa 		return -ENOMEM;
1276cd82a32eSJiri Olsa 
1277cd82a32eSJiri Olsa 	format->name = strdup(name);
1278cd82a32eSJiri Olsa 	format->value = config;
1279cd82a32eSJiri Olsa 	memcpy(format->bits, bits, sizeof(format->bits));
1280cd82a32eSJiri Olsa 
1281cd82a32eSJiri Olsa 	list_add_tail(&format->list, list);
1282cd82a32eSJiri Olsa 	return 0;
1283cd82a32eSJiri Olsa }
1284cd82a32eSJiri Olsa 
1285cd82a32eSJiri Olsa void perf_pmu__set_format(unsigned long *bits, long from, long to)
1286cd82a32eSJiri Olsa {
1287cd82a32eSJiri Olsa 	long b;
1288cd82a32eSJiri Olsa 
1289cd82a32eSJiri Olsa 	if (!to)
1290cd82a32eSJiri Olsa 		to = from;
1291cd82a32eSJiri Olsa 
129215268138SSukadev Bhattiprolu 	memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
1293cd82a32eSJiri Olsa 	for (b = from; b <= to; b++)
1294cd82a32eSJiri Olsa 		set_bit(b, bits);
1295cd82a32eSJiri Olsa }
1296dc098b35SAndi Kleen 
1297aaea3617SCody P Schafer static int sub_non_neg(int a, int b)
1298aaea3617SCody P Schafer {
1299aaea3617SCody P Schafer 	if (b > a)
1300aaea3617SCody P Schafer 		return 0;
1301aaea3617SCody P Schafer 	return a - b;
1302aaea3617SCody P Schafer }
1303aaea3617SCody P Schafer 
1304dc098b35SAndi Kleen static char *format_alias(char *buf, int len, struct perf_pmu *pmu,
1305dc098b35SAndi Kleen 			  struct perf_pmu_alias *alias)
1306dc098b35SAndi Kleen {
1307aaea3617SCody P Schafer 	struct parse_events_term *term;
1308aaea3617SCody P Schafer 	int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
1309aaea3617SCody P Schafer 
1310aaea3617SCody P Schafer 	list_for_each_entry(term, &alias->terms, list) {
1311aaea3617SCody P Schafer 		if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1312aaea3617SCody P Schafer 			used += snprintf(buf + used, sub_non_neg(len, used),
1313aaea3617SCody P Schafer 					",%s=%s", term->config,
1314aaea3617SCody P Schafer 					term->val.str);
1315aaea3617SCody P Schafer 	}
1316aaea3617SCody P Schafer 
1317aaea3617SCody P Schafer 	if (sub_non_neg(len, used) > 0) {
1318aaea3617SCody P Schafer 		buf[used] = '/';
1319aaea3617SCody P Schafer 		used++;
1320aaea3617SCody P Schafer 	}
1321aaea3617SCody P Schafer 	if (sub_non_neg(len, used) > 0) {
1322aaea3617SCody P Schafer 		buf[used] = '\0';
1323aaea3617SCody P Schafer 		used++;
1324aaea3617SCody P Schafer 	} else
1325aaea3617SCody P Schafer 		buf[len - 1] = '\0';
1326aaea3617SCody P Schafer 
1327dc098b35SAndi Kleen 	return buf;
1328dc098b35SAndi Kleen }
1329dc098b35SAndi Kleen 
1330dc098b35SAndi Kleen static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu,
1331dc098b35SAndi Kleen 			     struct perf_pmu_alias *alias)
1332dc098b35SAndi Kleen {
1333dc098b35SAndi Kleen 	snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name);
1334dc098b35SAndi Kleen 	return buf;
1335dc098b35SAndi Kleen }
1336dc098b35SAndi Kleen 
1337dd5f1036SAndi Kleen struct sevent {
133808e60ed1SAndi Kleen 	char *name;
133908e60ed1SAndi Kleen 	char *desc;
1340dd5f1036SAndi Kleen 	char *topic;
1341f2361024SAndi Kleen 	char *str;
1342f2361024SAndi Kleen 	char *pmu;
13437f372a63SAndi Kleen 	char *metric_expr;
134496284814SAndi Kleen 	char *metric_name;
134508e60ed1SAndi Kleen };
134608e60ed1SAndi Kleen 
1347dd5f1036SAndi Kleen static int cmp_sevent(const void *a, const void *b)
1348dc098b35SAndi Kleen {
1349dd5f1036SAndi Kleen 	const struct sevent *as = a;
1350dd5f1036SAndi Kleen 	const struct sevent *bs = b;
135108e60ed1SAndi Kleen 
135208e60ed1SAndi Kleen 	/* Put extra events last */
135308e60ed1SAndi Kleen 	if (!!as->desc != !!bs->desc)
135408e60ed1SAndi Kleen 		return !!as->desc - !!bs->desc;
1355dd5f1036SAndi Kleen 	if (as->topic && bs->topic) {
1356dd5f1036SAndi Kleen 		int n = strcmp(as->topic, bs->topic);
1357dd5f1036SAndi Kleen 
1358dd5f1036SAndi Kleen 		if (n)
1359dd5f1036SAndi Kleen 			return n;
1360dd5f1036SAndi Kleen 	}
136108e60ed1SAndi Kleen 	return strcmp(as->name, bs->name);
136208e60ed1SAndi Kleen }
136308e60ed1SAndi Kleen 
136408e60ed1SAndi Kleen static void wordwrap(char *s, int start, int max, int corr)
136508e60ed1SAndi Kleen {
136608e60ed1SAndi Kleen 	int column = start;
136708e60ed1SAndi Kleen 	int n;
136808e60ed1SAndi Kleen 
136908e60ed1SAndi Kleen 	while (*s) {
137008e60ed1SAndi Kleen 		int wlen = strcspn(s, " \t");
137108e60ed1SAndi Kleen 
137208e60ed1SAndi Kleen 		if (column + wlen >= max && column > start) {
137308e60ed1SAndi Kleen 			printf("\n%*s", start, "");
137408e60ed1SAndi Kleen 			column = start + corr;
137508e60ed1SAndi Kleen 		}
137608e60ed1SAndi Kleen 		n = printf("%s%.*s", column > start ? " " : "", wlen, s);
137708e60ed1SAndi Kleen 		if (n <= 0)
137808e60ed1SAndi Kleen 			break;
137908e60ed1SAndi Kleen 		s += wlen;
138008e60ed1SAndi Kleen 		column += n;
138132858480SArnaldo Carvalho de Melo 		s = skip_spaces(s);
138208e60ed1SAndi Kleen 	}
1383dc098b35SAndi Kleen }
1384dc098b35SAndi Kleen 
1385c8d6828aSSukadev Bhattiprolu void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag,
1386bf874fcfSAndi Kleen 			bool long_desc, bool details_flag)
1387dc098b35SAndi Kleen {
1388dc098b35SAndi Kleen 	struct perf_pmu *pmu;
1389dc098b35SAndi Kleen 	struct perf_pmu_alias *alias;
1390dc098b35SAndi Kleen 	char buf[1024];
1391dc098b35SAndi Kleen 	int printed = 0;
1392dc098b35SAndi Kleen 	int len, j;
1393dd5f1036SAndi Kleen 	struct sevent *aliases;
139408e60ed1SAndi Kleen 	int numdesc = 0;
139561eb2eb4SAndi Kleen 	int columns = pager_get_columns();
1396dd5f1036SAndi Kleen 	char *topic = NULL;
1397dc098b35SAndi Kleen 
1398dc098b35SAndi Kleen 	pmu = NULL;
1399dc098b35SAndi Kleen 	len = 0;
140042634bc7SAdrian Hunter 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1401dc098b35SAndi Kleen 		list_for_each_entry(alias, &pmu->aliases, list)
1402dc098b35SAndi Kleen 			len++;
140342634bc7SAdrian Hunter 		if (pmu->selectable)
140442634bc7SAdrian Hunter 			len++;
140542634bc7SAdrian Hunter 	}
1406dd5f1036SAndi Kleen 	aliases = zalloc(sizeof(struct sevent) * len);
1407dc098b35SAndi Kleen 	if (!aliases)
14087e4772dcSArnaldo Carvalho de Melo 		goto out_enomem;
1409dc098b35SAndi Kleen 	pmu = NULL;
1410dc098b35SAndi Kleen 	j = 0;
141142634bc7SAdrian Hunter 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1412dc098b35SAndi Kleen 		list_for_each_entry(alias, &pmu->aliases, list) {
141308e60ed1SAndi Kleen 			char *name = alias->desc ? alias->name :
141408e60ed1SAndi Kleen 				format_alias(buf, sizeof(buf), pmu, alias);
1415dc098b35SAndi Kleen 			bool is_cpu = !strcmp(pmu->name, "cpu");
1416dc098b35SAndi Kleen 
1417dc098b35SAndi Kleen 			if (event_glob != NULL &&
141838d14f0cSAndi Kleen 			    !(strglobmatch_nocase(name, event_glob) ||
141938d14f0cSAndi Kleen 			      (!is_cpu && strglobmatch_nocase(alias->name,
142067bdc35fSAndi Kleen 						       event_glob)) ||
142167bdc35fSAndi Kleen 			      (alias->topic &&
142267bdc35fSAndi Kleen 			       strglobmatch_nocase(alias->topic, event_glob))))
1423dc098b35SAndi Kleen 				continue;
14247e4772dcSArnaldo Carvalho de Melo 
142508e60ed1SAndi Kleen 			if (is_cpu && !name_only && !alias->desc)
14267e4772dcSArnaldo Carvalho de Melo 				name = format_alias_or(buf, sizeof(buf), pmu, alias);
14277e4772dcSArnaldo Carvalho de Melo 
142808e60ed1SAndi Kleen 			aliases[j].name = name;
142908e60ed1SAndi Kleen 			if (is_cpu && !name_only && !alias->desc)
143008e60ed1SAndi Kleen 				aliases[j].name = format_alias_or(buf,
143108e60ed1SAndi Kleen 								  sizeof(buf),
143208e60ed1SAndi Kleen 								  pmu, alias);
143308e60ed1SAndi Kleen 			aliases[j].name = strdup(aliases[j].name);
143408e60ed1SAndi Kleen 			if (!aliases[j].name)
14357e4772dcSArnaldo Carvalho de Melo 				goto out_enomem;
143608e60ed1SAndi Kleen 
1437c8d6828aSSukadev Bhattiprolu 			aliases[j].desc = long_desc ? alias->long_desc :
1438c8d6828aSSukadev Bhattiprolu 						alias->desc;
1439dd5f1036SAndi Kleen 			aliases[j].topic = alias->topic;
1440f2361024SAndi Kleen 			aliases[j].str = alias->str;
1441f2361024SAndi Kleen 			aliases[j].pmu = pmu->name;
14427f372a63SAndi Kleen 			aliases[j].metric_expr = alias->metric_expr;
144396284814SAndi Kleen 			aliases[j].metric_name = alias->metric_name;
1444dc098b35SAndi Kleen 			j++;
1445dc098b35SAndi Kleen 		}
1446fa52ceabSArnaldo Carvalho de Melo 		if (pmu->selectable &&
1447fa52ceabSArnaldo Carvalho de Melo 		    (event_glob == NULL || strglobmatch(pmu->name, event_glob))) {
14487e4772dcSArnaldo Carvalho de Melo 			char *s;
14497e4772dcSArnaldo Carvalho de Melo 			if (asprintf(&s, "%s//", pmu->name) < 0)
14507e4772dcSArnaldo Carvalho de Melo 				goto out_enomem;
145108e60ed1SAndi Kleen 			aliases[j].name = s;
145242634bc7SAdrian Hunter 			j++;
145342634bc7SAdrian Hunter 		}
145442634bc7SAdrian Hunter 	}
1455dc098b35SAndi Kleen 	len = j;
1456dd5f1036SAndi Kleen 	qsort(aliases, len, sizeof(struct sevent), cmp_sevent);
1457dc098b35SAndi Kleen 	for (j = 0; j < len; j++) {
145815b22ed3SAndi Kleen 		/* Skip duplicates */
145915b22ed3SAndi Kleen 		if (j > 0 && !strcmp(aliases[j].name, aliases[j - 1].name))
146015b22ed3SAndi Kleen 			continue;
1461dc098b35SAndi Kleen 		if (name_only) {
146208e60ed1SAndi Kleen 			printf("%s ", aliases[j].name);
1463dc098b35SAndi Kleen 			continue;
1464dc098b35SAndi Kleen 		}
14651c5f01feSAndi Kleen 		if (aliases[j].desc && !quiet_flag) {
146608e60ed1SAndi Kleen 			if (numdesc++ == 0)
146708e60ed1SAndi Kleen 				printf("\n");
1468dd5f1036SAndi Kleen 			if (aliases[j].topic && (!topic ||
1469dd5f1036SAndi Kleen 					strcmp(topic, aliases[j].topic))) {
1470dd5f1036SAndi Kleen 				printf("%s%s:\n", topic ? "\n" : "",
1471dd5f1036SAndi Kleen 						aliases[j].topic);
1472dd5f1036SAndi Kleen 				topic = aliases[j].topic;
1473dd5f1036SAndi Kleen 			}
147408e60ed1SAndi Kleen 			printf("  %-50s\n", aliases[j].name);
147508e60ed1SAndi Kleen 			printf("%*s", 8, "[");
147608e60ed1SAndi Kleen 			wordwrap(aliases[j].desc, 8, columns, 0);
147708e60ed1SAndi Kleen 			printf("]\n");
1478bf874fcfSAndi Kleen 			if (details_flag) {
14797f372a63SAndi Kleen 				printf("%*s%s/%s/ ", 8, "", aliases[j].pmu, aliases[j].str);
148096284814SAndi Kleen 				if (aliases[j].metric_name)
148196284814SAndi Kleen 					printf(" MetricName: %s", aliases[j].metric_name);
14827f372a63SAndi Kleen 				if (aliases[j].metric_expr)
14837f372a63SAndi Kleen 					printf(" MetricExpr: %s", aliases[j].metric_expr);
14847f372a63SAndi Kleen 				putchar('\n');
14857f372a63SAndi Kleen 			}
148608e60ed1SAndi Kleen 		} else
148708e60ed1SAndi Kleen 			printf("  %-50s [Kernel PMU event]\n", aliases[j].name);
1488dc098b35SAndi Kleen 		printed++;
1489dc098b35SAndi Kleen 	}
1490dfc431cbSArnaldo Carvalho de Melo 	if (printed && pager_in_use())
1491dc098b35SAndi Kleen 		printf("\n");
14927e4772dcSArnaldo Carvalho de Melo out_free:
14937e4772dcSArnaldo Carvalho de Melo 	for (j = 0; j < len; j++)
149408e60ed1SAndi Kleen 		zfree(&aliases[j].name);
14957e4772dcSArnaldo Carvalho de Melo 	zfree(&aliases);
14967e4772dcSArnaldo Carvalho de Melo 	return;
14977e4772dcSArnaldo Carvalho de Melo 
14987e4772dcSArnaldo Carvalho de Melo out_enomem:
14997e4772dcSArnaldo Carvalho de Melo 	printf("FATAL: not enough memory to print PMU events\n");
15007e4772dcSArnaldo Carvalho de Melo 	if (aliases)
15017e4772dcSArnaldo Carvalho de Melo 		goto out_free;
1502dc098b35SAndi Kleen }
15034cabc3d1SAndi Kleen 
15044cabc3d1SAndi Kleen bool pmu_have_event(const char *pname, const char *name)
15054cabc3d1SAndi Kleen {
15064cabc3d1SAndi Kleen 	struct perf_pmu *pmu;
15074cabc3d1SAndi Kleen 	struct perf_pmu_alias *alias;
15084cabc3d1SAndi Kleen 
15094cabc3d1SAndi Kleen 	pmu = NULL;
15104cabc3d1SAndi Kleen 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
15114cabc3d1SAndi Kleen 		if (strcmp(pname, pmu->name))
15124cabc3d1SAndi Kleen 			continue;
15134cabc3d1SAndi Kleen 		list_for_each_entry(alias, &pmu->aliases, list)
15144cabc3d1SAndi Kleen 			if (!strcmp(alias->name, name))
15154cabc3d1SAndi Kleen 				return true;
15164cabc3d1SAndi Kleen 	}
15174cabc3d1SAndi Kleen 	return false;
15184cabc3d1SAndi Kleen }
15197d4bdab5SAdrian Hunter 
15207d4bdab5SAdrian Hunter static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
15217d4bdab5SAdrian Hunter {
15227d4bdab5SAdrian Hunter 	struct stat st;
15237d4bdab5SAdrian Hunter 	char path[PATH_MAX];
15247d4bdab5SAdrian Hunter 	const char *sysfs;
15257d4bdab5SAdrian Hunter 
15267d4bdab5SAdrian Hunter 	sysfs = sysfs__mountpoint();
15277d4bdab5SAdrian Hunter 	if (!sysfs)
15287d4bdab5SAdrian Hunter 		return NULL;
15297d4bdab5SAdrian Hunter 
15307d4bdab5SAdrian Hunter 	snprintf(path, PATH_MAX,
15317d4bdab5SAdrian Hunter 		 "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name);
15327d4bdab5SAdrian Hunter 
15337d4bdab5SAdrian Hunter 	if (stat(path, &st) < 0)
15347d4bdab5SAdrian Hunter 		return NULL;
15357d4bdab5SAdrian Hunter 
15367d4bdab5SAdrian Hunter 	return fopen(path, "r");
15377d4bdab5SAdrian Hunter }
15387d4bdab5SAdrian Hunter 
15397d4bdab5SAdrian Hunter int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
15407d4bdab5SAdrian Hunter 			...)
15417d4bdab5SAdrian Hunter {
15427d4bdab5SAdrian Hunter 	va_list args;
15437d4bdab5SAdrian Hunter 	FILE *file;
15447d4bdab5SAdrian Hunter 	int ret = EOF;
15457d4bdab5SAdrian Hunter 
15467d4bdab5SAdrian Hunter 	va_start(args, fmt);
15477d4bdab5SAdrian Hunter 	file = perf_pmu__open_file(pmu, name);
15487d4bdab5SAdrian Hunter 	if (file) {
15497d4bdab5SAdrian Hunter 		ret = vfscanf(file, fmt, args);
15507d4bdab5SAdrian Hunter 		fclose(file);
15517d4bdab5SAdrian Hunter 	}
15527d4bdab5SAdrian Hunter 	va_end(args);
15537d4bdab5SAdrian Hunter 	return ret;
15547d4bdab5SAdrian Hunter }
1555