xref: /openbmc/linux/tools/perf/util/pmu.c (revision 6dde6429)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2cd82a32eSJiri Olsa #include <linux/list.h>
3c5de47f2SSukadev Bhattiprolu #include <linux/compiler.h>
4cd82a32eSJiri Olsa #include <sys/types.h>
5a43783aeSArnaldo Carvalho de Melo #include <errno.h>
6c23c2a0fSArnaldo Carvalho de Melo #include <fcntl.h>
77a8ef4c4SArnaldo Carvalho de Melo #include <sys/stat.h>
8cd82a32eSJiri Olsa #include <unistd.h>
9cd82a32eSJiri Olsa #include <stdio.h>
10dc0a6202SAdrian Hunter #include <stdbool.h>
117d4bdab5SAdrian Hunter #include <stdarg.h>
12cd82a32eSJiri Olsa #include <dirent.h>
13cd0cfad7SBorislav Petkov #include <api/fs/fs.h>
14410136f5SStephane Eranian #include <locale.h>
15fbc2844eSWilliam Cohen #include <regex.h>
16cd82a32eSJiri Olsa #include "util.h"
17cd82a32eSJiri Olsa #include "pmu.h"
18cd82a32eSJiri Olsa #include "parse-events.h"
197ae92e74SYan, Zheng #include "cpumap.h"
20933f82ffSSukadev Bhattiprolu #include "header.h"
21933f82ffSSukadev Bhattiprolu #include "pmu-events/pmu-events.h"
2261eb2eb4SAndi Kleen #include "cache.h"
23a067558eSArnaldo Carvalho de Melo #include "string2.h"
24cd82a32eSJiri Olsa 
25ab1bf653SArnaldo Carvalho de Melo struct perf_pmu_format {
26ab1bf653SArnaldo Carvalho de Melo 	char *name;
27ab1bf653SArnaldo Carvalho de Melo 	int value;
28ab1bf653SArnaldo Carvalho de Melo 	DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
29ab1bf653SArnaldo Carvalho de Melo 	struct list_head list;
30ab1bf653SArnaldo Carvalho de Melo };
31ab1bf653SArnaldo Carvalho de Melo 
3250a9667cSRobert Richter #define EVENT_SOURCE_DEVICE_PATH "/bus/event_source/devices/"
3350a9667cSRobert Richter 
34cd82a32eSJiri Olsa int perf_pmu_parse(struct list_head *list, char *name);
35cd82a32eSJiri Olsa extern FILE *perf_pmu_in;
36cd82a32eSJiri Olsa 
37cd82a32eSJiri Olsa static LIST_HEAD(pmus);
38cd82a32eSJiri Olsa 
39cd82a32eSJiri Olsa /*
40cd82a32eSJiri Olsa  * Parse & process all the sysfs attributes located under
41cd82a32eSJiri Olsa  * the directory specified in 'dir' parameter.
42cd82a32eSJiri Olsa  */
43cff7f956SJiri Olsa int perf_pmu__format_parse(char *dir, struct list_head *head)
44cd82a32eSJiri Olsa {
45cd82a32eSJiri Olsa 	struct dirent *evt_ent;
46cd82a32eSJiri Olsa 	DIR *format_dir;
47cd82a32eSJiri Olsa 	int ret = 0;
48cd82a32eSJiri Olsa 
49cd82a32eSJiri Olsa 	format_dir = opendir(dir);
50cd82a32eSJiri Olsa 	if (!format_dir)
51cd82a32eSJiri Olsa 		return -EINVAL;
52cd82a32eSJiri Olsa 
53cd82a32eSJiri Olsa 	while (!ret && (evt_ent = readdir(format_dir))) {
54cd82a32eSJiri Olsa 		char path[PATH_MAX];
55cd82a32eSJiri Olsa 		char *name = evt_ent->d_name;
56cd82a32eSJiri Olsa 		FILE *file;
57cd82a32eSJiri Olsa 
58cd82a32eSJiri Olsa 		if (!strcmp(name, ".") || !strcmp(name, ".."))
59cd82a32eSJiri Olsa 			continue;
60cd82a32eSJiri Olsa 
61cd82a32eSJiri Olsa 		snprintf(path, PATH_MAX, "%s/%s", dir, name);
62cd82a32eSJiri Olsa 
63cd82a32eSJiri Olsa 		ret = -EINVAL;
64cd82a32eSJiri Olsa 		file = fopen(path, "r");
65cd82a32eSJiri Olsa 		if (!file)
66cd82a32eSJiri Olsa 			break;
67cd82a32eSJiri Olsa 
68cd82a32eSJiri Olsa 		perf_pmu_in = file;
69cd82a32eSJiri Olsa 		ret = perf_pmu_parse(head, name);
70cd82a32eSJiri Olsa 		fclose(file);
71cd82a32eSJiri Olsa 	}
72cd82a32eSJiri Olsa 
73cd82a32eSJiri Olsa 	closedir(format_dir);
74cd82a32eSJiri Olsa 	return ret;
75cd82a32eSJiri Olsa }
76cd82a32eSJiri Olsa 
77cd82a32eSJiri Olsa /*
78cd82a32eSJiri Olsa  * Reading/parsing the default pmu format definition, which should be
79cd82a32eSJiri Olsa  * located at:
80cd82a32eSJiri Olsa  * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
81cd82a32eSJiri Olsa  */
82b6b96fb4SAdrian Hunter static int pmu_format(const char *name, struct list_head *format)
83cd82a32eSJiri Olsa {
84cd82a32eSJiri Olsa 	struct stat st;
85cd82a32eSJiri Olsa 	char path[PATH_MAX];
86cf38fadaSArnaldo Carvalho de Melo 	const char *sysfs = sysfs__mountpoint();
87cd82a32eSJiri Olsa 
88cd82a32eSJiri Olsa 	if (!sysfs)
89cd82a32eSJiri Olsa 		return -1;
90cd82a32eSJiri Olsa 
91cd82a32eSJiri Olsa 	snprintf(path, PATH_MAX,
9250a9667cSRobert Richter 		 "%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name);
93cd82a32eSJiri Olsa 
94cd82a32eSJiri Olsa 	if (stat(path, &st) < 0)
959bc8f9feSRobert Richter 		return 0;	/* no error if format does not exist */
96cd82a32eSJiri Olsa 
97cff7f956SJiri Olsa 	if (perf_pmu__format_parse(path, format))
98cd82a32eSJiri Olsa 		return -1;
99cd82a32eSJiri Olsa 
100cd82a32eSJiri Olsa 	return 0;
101cd82a32eSJiri Olsa }
102cd82a32eSJiri Olsa 
103d02fc6bcSAndi Kleen static int convert_scale(const char *scale, char **end, double *sval)
104d02fc6bcSAndi Kleen {
105d02fc6bcSAndi Kleen 	char *lc;
106d02fc6bcSAndi Kleen 	int ret = 0;
107d02fc6bcSAndi Kleen 
108d02fc6bcSAndi Kleen 	/*
109d02fc6bcSAndi Kleen 	 * save current locale
110d02fc6bcSAndi Kleen 	 */
111d02fc6bcSAndi Kleen 	lc = setlocale(LC_NUMERIC, NULL);
112d02fc6bcSAndi Kleen 
113d02fc6bcSAndi Kleen 	/*
114d02fc6bcSAndi Kleen 	 * The lc string may be allocated in static storage,
115d02fc6bcSAndi Kleen 	 * so get a dynamic copy to make it survive setlocale
116d02fc6bcSAndi Kleen 	 * call below.
117d02fc6bcSAndi Kleen 	 */
118d02fc6bcSAndi Kleen 	lc = strdup(lc);
119d02fc6bcSAndi Kleen 	if (!lc) {
120d02fc6bcSAndi Kleen 		ret = -ENOMEM;
121d02fc6bcSAndi Kleen 		goto out;
122d02fc6bcSAndi Kleen 	}
123d02fc6bcSAndi Kleen 
124d02fc6bcSAndi Kleen 	/*
125d02fc6bcSAndi Kleen 	 * force to C locale to ensure kernel
126d02fc6bcSAndi Kleen 	 * scale string is converted correctly.
127d02fc6bcSAndi Kleen 	 * kernel uses default C locale.
128d02fc6bcSAndi Kleen 	 */
129d02fc6bcSAndi Kleen 	setlocale(LC_NUMERIC, "C");
130d02fc6bcSAndi Kleen 
131d02fc6bcSAndi Kleen 	*sval = strtod(scale, end);
132d02fc6bcSAndi Kleen 
133d02fc6bcSAndi Kleen out:
134d02fc6bcSAndi Kleen 	/* restore locale */
135d02fc6bcSAndi Kleen 	setlocale(LC_NUMERIC, lc);
136d02fc6bcSAndi Kleen 	free(lc);
137d02fc6bcSAndi Kleen 	return ret;
138d02fc6bcSAndi Kleen }
139d02fc6bcSAndi Kleen 
140410136f5SStephane Eranian static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name)
141410136f5SStephane Eranian {
142410136f5SStephane Eranian 	struct stat st;
143410136f5SStephane Eranian 	ssize_t sret;
144410136f5SStephane Eranian 	char scale[128];
145410136f5SStephane Eranian 	int fd, ret = -1;
146410136f5SStephane Eranian 	char path[PATH_MAX];
147410136f5SStephane Eranian 
148410136f5SStephane Eranian 	snprintf(path, PATH_MAX, "%s/%s.scale", dir, name);
149410136f5SStephane Eranian 
150410136f5SStephane Eranian 	fd = open(path, O_RDONLY);
151410136f5SStephane Eranian 	if (fd == -1)
152410136f5SStephane Eranian 		return -1;
153410136f5SStephane Eranian 
154410136f5SStephane Eranian 	if (fstat(fd, &st) < 0)
155410136f5SStephane Eranian 		goto error;
156410136f5SStephane Eranian 
157410136f5SStephane Eranian 	sret = read(fd, scale, sizeof(scale)-1);
158410136f5SStephane Eranian 	if (sret < 0)
159410136f5SStephane Eranian 		goto error;
160410136f5SStephane Eranian 
1619ecae065SMadhavan Srinivasan 	if (scale[sret - 1] == '\n')
1629ecae065SMadhavan Srinivasan 		scale[sret - 1] = '\0';
1639ecae065SMadhavan Srinivasan 	else
164410136f5SStephane Eranian 		scale[sret] = '\0';
1659ecae065SMadhavan Srinivasan 
166d02fc6bcSAndi Kleen 	ret = convert_scale(scale, NULL, &alias->scale);
167410136f5SStephane Eranian error:
168410136f5SStephane Eranian 	close(fd);
169410136f5SStephane Eranian 	return ret;
170410136f5SStephane Eranian }
171410136f5SStephane Eranian 
172410136f5SStephane Eranian static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name)
173410136f5SStephane Eranian {
174410136f5SStephane Eranian 	char path[PATH_MAX];
175410136f5SStephane Eranian 	ssize_t sret;
176410136f5SStephane Eranian 	int fd;
177410136f5SStephane Eranian 
178410136f5SStephane Eranian 	snprintf(path, PATH_MAX, "%s/%s.unit", dir, name);
179410136f5SStephane Eranian 
180410136f5SStephane Eranian 	fd = open(path, O_RDONLY);
181410136f5SStephane Eranian 	if (fd == -1)
182410136f5SStephane Eranian 		return -1;
183410136f5SStephane Eranian 
184410136f5SStephane Eranian 	sret = read(fd, alias->unit, UNIT_MAX_LEN);
185410136f5SStephane Eranian 	if (sret < 0)
186410136f5SStephane Eranian 		goto error;
187410136f5SStephane Eranian 
188410136f5SStephane Eranian 	close(fd);
189410136f5SStephane Eranian 
1909ecae065SMadhavan Srinivasan 	if (alias->unit[sret - 1] == '\n')
1919ecae065SMadhavan Srinivasan 		alias->unit[sret - 1] = '\0';
1929ecae065SMadhavan Srinivasan 	else
193410136f5SStephane Eranian 		alias->unit[sret] = '\0';
194410136f5SStephane Eranian 
195410136f5SStephane Eranian 	return 0;
196410136f5SStephane Eranian error:
197410136f5SStephane Eranian 	close(fd);
198410136f5SStephane Eranian 	alias->unit[0] = '\0';
199410136f5SStephane Eranian 	return -1;
200410136f5SStephane Eranian }
201410136f5SStephane Eranian 
202044330c1SMatt Fleming static int
203044330c1SMatt Fleming perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *name)
204044330c1SMatt Fleming {
205044330c1SMatt Fleming 	char path[PATH_MAX];
206044330c1SMatt Fleming 	int fd;
207044330c1SMatt Fleming 
208044330c1SMatt Fleming 	snprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name);
209044330c1SMatt Fleming 
210044330c1SMatt Fleming 	fd = open(path, O_RDONLY);
211044330c1SMatt Fleming 	if (fd == -1)
212044330c1SMatt Fleming 		return -1;
213044330c1SMatt Fleming 
214044330c1SMatt Fleming 	close(fd);
215044330c1SMatt Fleming 
216044330c1SMatt Fleming 	alias->per_pkg = true;
217044330c1SMatt Fleming 	return 0;
218044330c1SMatt Fleming }
219044330c1SMatt Fleming 
2201d9e446bSJiri Olsa static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias,
2211d9e446bSJiri Olsa 				    char *dir, char *name)
2221d9e446bSJiri Olsa {
2231d9e446bSJiri Olsa 	char path[PATH_MAX];
2241d9e446bSJiri Olsa 	int fd;
2251d9e446bSJiri Olsa 
2261d9e446bSJiri Olsa 	snprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name);
2271d9e446bSJiri Olsa 
2281d9e446bSJiri Olsa 	fd = open(path, O_RDONLY);
2291d9e446bSJiri Olsa 	if (fd == -1)
2301d9e446bSJiri Olsa 		return -1;
2311d9e446bSJiri Olsa 
2321d9e446bSJiri Olsa 	alias->snapshot = true;
2331d9e446bSJiri Olsa 	close(fd);
2341d9e446bSJiri Olsa 	return 0;
2351d9e446bSJiri Olsa }
2361d9e446bSJiri Olsa 
2376dde6429SThomas Richter static void perf_pmu_assign_str(char *name, const char *field, char **old_str,
2386dde6429SThomas Richter 				char **new_str)
2396dde6429SThomas Richter {
2406dde6429SThomas Richter 	if (!*old_str)
2416dde6429SThomas Richter 		goto set_new;
2426dde6429SThomas Richter 
2436dde6429SThomas Richter 	if (*new_str) {	/* Have new string, check with old */
2446dde6429SThomas Richter 		if (strcasecmp(*old_str, *new_str))
2456dde6429SThomas Richter 			pr_debug("alias %s differs in field '%s'\n",
2466dde6429SThomas Richter 				 name, field);
2476dde6429SThomas Richter 		zfree(old_str);
2486dde6429SThomas Richter 	} else		/* Nothing new --> keep old string */
2496dde6429SThomas Richter 		return;
2506dde6429SThomas Richter set_new:
2516dde6429SThomas Richter 	*old_str = *new_str;
2526dde6429SThomas Richter 	*new_str = NULL;
2536dde6429SThomas Richter }
2546dde6429SThomas Richter 
2556dde6429SThomas Richter static void perf_pmu_update_alias(struct perf_pmu_alias *old,
2566dde6429SThomas Richter 				  struct perf_pmu_alias *newalias)
2576dde6429SThomas Richter {
2586dde6429SThomas Richter 	perf_pmu_assign_str(old->name, "desc", &old->desc, &newalias->desc);
2596dde6429SThomas Richter 	perf_pmu_assign_str(old->name, "long_desc", &old->long_desc,
2606dde6429SThomas Richter 			    &newalias->long_desc);
2616dde6429SThomas Richter 	perf_pmu_assign_str(old->name, "topic", &old->topic, &newalias->topic);
2626dde6429SThomas Richter 	perf_pmu_assign_str(old->name, "metric_expr", &old->metric_expr,
2636dde6429SThomas Richter 			    &newalias->metric_expr);
2646dde6429SThomas Richter 	perf_pmu_assign_str(old->name, "metric_name", &old->metric_name,
2656dde6429SThomas Richter 			    &newalias->metric_name);
2666dde6429SThomas Richter 	perf_pmu_assign_str(old->name, "value", &old->str, &newalias->str);
2676dde6429SThomas Richter 	old->scale = newalias->scale;
2686dde6429SThomas Richter 	old->per_pkg = newalias->per_pkg;
2696dde6429SThomas Richter 	old->snapshot = newalias->snapshot;
2706dde6429SThomas Richter 	memcpy(old->unit, newalias->unit, sizeof(old->unit));
2716dde6429SThomas Richter }
2726dde6429SThomas Richter 
2736dde6429SThomas Richter /* Delete an alias entry. */
2746dde6429SThomas Richter static void perf_pmu_free_alias(struct perf_pmu_alias *newalias)
2756dde6429SThomas Richter {
2766dde6429SThomas Richter 	zfree(&newalias->name);
2776dde6429SThomas Richter 	zfree(&newalias->desc);
2786dde6429SThomas Richter 	zfree(&newalias->long_desc);
2796dde6429SThomas Richter 	zfree(&newalias->topic);
2806dde6429SThomas Richter 	zfree(&newalias->str);
2816dde6429SThomas Richter 	zfree(&newalias->metric_expr);
2826dde6429SThomas Richter 	zfree(&newalias->metric_name);
2836dde6429SThomas Richter 	parse_events_terms__purge(&newalias->terms);
2846dde6429SThomas Richter 	free(newalias);
2856dde6429SThomas Richter }
2866dde6429SThomas Richter 
2876dde6429SThomas Richter /* Merge an alias, search in alias list. If this name is already
2886dde6429SThomas Richter  * present merge both of them to combine all information.
2896dde6429SThomas Richter  */
2906dde6429SThomas Richter static bool perf_pmu_merge_alias(struct perf_pmu_alias *newalias,
2916dde6429SThomas Richter 				 struct list_head *alist)
2926dde6429SThomas Richter {
2936dde6429SThomas Richter 	struct perf_pmu_alias *a;
2946dde6429SThomas Richter 
2956dde6429SThomas Richter 	list_for_each_entry(a, alist, list) {
2966dde6429SThomas Richter 		if (!strcasecmp(newalias->name, a->name)) {
2976dde6429SThomas Richter 			perf_pmu_update_alias(a, newalias);
2986dde6429SThomas Richter 			perf_pmu_free_alias(newalias);
2996dde6429SThomas Richter 			return true;
3006dde6429SThomas Richter 		}
3016dde6429SThomas Richter 	}
3026dde6429SThomas Richter 	return false;
3036dde6429SThomas Richter }
3046dde6429SThomas Richter 
30570c646e0SSukadev Bhattiprolu static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name,
306fedb2b51SAndi Kleen 				 char *desc, char *val,
307fedb2b51SAndi Kleen 				 char *long_desc, char *topic,
30800636c3bSAndi Kleen 				 char *unit, char *perpkg,
30996284814SAndi Kleen 				 char *metric_expr,
31096284814SAndi Kleen 				 char *metric_name)
311a6146d50SZheng Yan {
3120c24d6fbSThomas Richter 	struct parse_events_term *term;
3135c6ccc37SArnaldo Carvalho de Melo 	struct perf_pmu_alias *alias;
314a6146d50SZheng Yan 	int ret;
315fedb2b51SAndi Kleen 	int num;
3160c24d6fbSThomas Richter 	char newval[256];
317a6146d50SZheng Yan 
318a6146d50SZheng Yan 	alias = malloc(sizeof(*alias));
319a6146d50SZheng Yan 	if (!alias)
320a6146d50SZheng Yan 		return -ENOMEM;
321a6146d50SZheng Yan 
322a6146d50SZheng Yan 	INIT_LIST_HEAD(&alias->terms);
323410136f5SStephane Eranian 	alias->scale = 1.0;
324410136f5SStephane Eranian 	alias->unit[0] = '\0';
325044330c1SMatt Fleming 	alias->per_pkg = false;
32684530920SStephane Eranian 	alias->snapshot = false;
327410136f5SStephane Eranian 
32870c646e0SSukadev Bhattiprolu 	ret = parse_events_terms(&alias->terms, val);
329a6146d50SZheng Yan 	if (ret) {
33070c646e0SSukadev Bhattiprolu 		pr_err("Cannot parse alias %s: %d\n", val, ret);
331a6146d50SZheng Yan 		free(alias);
332a6146d50SZheng Yan 		return ret;
333a6146d50SZheng Yan 	}
334a6146d50SZheng Yan 
3350c24d6fbSThomas Richter 	/* Scan event and remove leading zeroes, spaces, newlines, some
3360c24d6fbSThomas Richter 	 * platforms have terms specified as
3370c24d6fbSThomas Richter 	 * event=0x0091 (read from files ../<PMU>/events/<FILE>
3380c24d6fbSThomas Richter 	 * and terms specified as event=0x91 (read from JSON files).
3390c24d6fbSThomas Richter 	 *
3400c24d6fbSThomas Richter 	 * Rebuild string to make alias->str member comparable.
3410c24d6fbSThomas Richter 	 */
3420c24d6fbSThomas Richter 	memset(newval, 0, sizeof(newval));
3430c24d6fbSThomas Richter 	ret = 0;
3440c24d6fbSThomas Richter 	list_for_each_entry(term, &alias->terms, list) {
3450c24d6fbSThomas Richter 		if (ret)
3460c24d6fbSThomas Richter 			ret += scnprintf(newval + ret, sizeof(newval) - ret,
3470c24d6fbSThomas Richter 					 ",");
3480c24d6fbSThomas Richter 		if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
3490c24d6fbSThomas Richter 			ret += scnprintf(newval + ret, sizeof(newval) - ret,
3500c24d6fbSThomas Richter 					 "%s=%#x", term->config, term->val.num);
3510c24d6fbSThomas Richter 		else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
3520c24d6fbSThomas Richter 			ret += scnprintf(newval + ret, sizeof(newval) - ret,
3530c24d6fbSThomas Richter 					 "%s=%s", term->config, term->val.str);
3540c24d6fbSThomas Richter 	}
3550c24d6fbSThomas Richter 
356a6146d50SZheng Yan 	alias->name = strdup(name);
35770c646e0SSukadev Bhattiprolu 	if (dir) {
358410136f5SStephane Eranian 		/*
359410136f5SStephane Eranian 		 * load unit name and scale if available
360410136f5SStephane Eranian 		 */
361410136f5SStephane Eranian 		perf_pmu__parse_unit(alias, dir, name);
362410136f5SStephane Eranian 		perf_pmu__parse_scale(alias, dir, name);
363044330c1SMatt Fleming 		perf_pmu__parse_per_pkg(alias, dir, name);
3641d9e446bSJiri Olsa 		perf_pmu__parse_snapshot(alias, dir, name);
36570c646e0SSukadev Bhattiprolu 	}
366410136f5SStephane Eranian 
36700636c3bSAndi Kleen 	alias->metric_expr = metric_expr ? strdup(metric_expr) : NULL;
36896284814SAndi Kleen 	alias->metric_name = metric_name ? strdup(metric_name): NULL;
36908e60ed1SAndi Kleen 	alias->desc = desc ? strdup(desc) : NULL;
370c8d6828aSSukadev Bhattiprolu 	alias->long_desc = long_desc ? strdup(long_desc) :
371c8d6828aSSukadev Bhattiprolu 				desc ? strdup(desc) : NULL;
372dd5f1036SAndi Kleen 	alias->topic = topic ? strdup(topic) : NULL;
373fedb2b51SAndi Kleen 	if (unit) {
374fedb2b51SAndi Kleen 		if (convert_scale(unit, &unit, &alias->scale) < 0)
375fedb2b51SAndi Kleen 			return -1;
376fedb2b51SAndi Kleen 		snprintf(alias->unit, sizeof(alias->unit), "%s", unit);
377fedb2b51SAndi Kleen 	}
378fedb2b51SAndi Kleen 	alias->per_pkg = perpkg && sscanf(perpkg, "%d", &num) == 1 && num == 1;
3790c24d6fbSThomas Richter 	alias->str = strdup(newval);
380f2361024SAndi Kleen 
3816dde6429SThomas Richter 	if (!perf_pmu_merge_alias(alias, list))
382a6146d50SZheng Yan 		list_add_tail(&alias->list, list);
383410136f5SStephane Eranian 
384a6146d50SZheng Yan 	return 0;
385a6146d50SZheng Yan }
386a6146d50SZheng Yan 
38770c646e0SSukadev Bhattiprolu static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file)
38870c646e0SSukadev Bhattiprolu {
38970c646e0SSukadev Bhattiprolu 	char buf[256];
39070c646e0SSukadev Bhattiprolu 	int ret;
39170c646e0SSukadev Bhattiprolu 
39270c646e0SSukadev Bhattiprolu 	ret = fread(buf, 1, sizeof(buf), file);
39370c646e0SSukadev Bhattiprolu 	if (ret == 0)
39470c646e0SSukadev Bhattiprolu 		return -EINVAL;
39570c646e0SSukadev Bhattiprolu 
39670c646e0SSukadev Bhattiprolu 	buf[ret] = 0;
39770c646e0SSukadev Bhattiprolu 
398ea23ac73SThomas Richter 	/* Remove trailing newline from sysfs file */
399ea23ac73SThomas Richter 	rtrim(buf);
400ea23ac73SThomas Richter 
401fedb2b51SAndi Kleen 	return __perf_pmu__new_alias(list, dir, name, NULL, buf, NULL, NULL, NULL,
40296284814SAndi Kleen 				     NULL, NULL, NULL);
40370c646e0SSukadev Bhattiprolu }
40470c646e0SSukadev Bhattiprolu 
40546441bdcSMatt Fleming static inline bool pmu_alias_info_file(char *name)
40646441bdcSMatt Fleming {
40746441bdcSMatt Fleming 	size_t len;
40846441bdcSMatt Fleming 
40946441bdcSMatt Fleming 	len = strlen(name);
41046441bdcSMatt Fleming 	if (len > 5 && !strcmp(name + len - 5, ".unit"))
41146441bdcSMatt Fleming 		return true;
41246441bdcSMatt Fleming 	if (len > 6 && !strcmp(name + len - 6, ".scale"))
41346441bdcSMatt Fleming 		return true;
414044330c1SMatt Fleming 	if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
415044330c1SMatt Fleming 		return true;
4161d9e446bSJiri Olsa 	if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
4171d9e446bSJiri Olsa 		return true;
41846441bdcSMatt Fleming 
41946441bdcSMatt Fleming 	return false;
42046441bdcSMatt Fleming }
42146441bdcSMatt Fleming 
422a6146d50SZheng Yan /*
423a6146d50SZheng Yan  * Process all the sysfs attributes located under the directory
424a6146d50SZheng Yan  * specified in 'dir' parameter.
425a6146d50SZheng Yan  */
426a6146d50SZheng Yan static int pmu_aliases_parse(char *dir, struct list_head *head)
427a6146d50SZheng Yan {
428a6146d50SZheng Yan 	struct dirent *evt_ent;
429a6146d50SZheng Yan 	DIR *event_dir;
430a6146d50SZheng Yan 
431a6146d50SZheng Yan 	event_dir = opendir(dir);
432a6146d50SZheng Yan 	if (!event_dir)
433a6146d50SZheng Yan 		return -EINVAL;
434a6146d50SZheng Yan 
435940db6dcSAndi Kleen 	while ((evt_ent = readdir(event_dir))) {
436a6146d50SZheng Yan 		char path[PATH_MAX];
437a6146d50SZheng Yan 		char *name = evt_ent->d_name;
438a6146d50SZheng Yan 		FILE *file;
439a6146d50SZheng Yan 
440a6146d50SZheng Yan 		if (!strcmp(name, ".") || !strcmp(name, ".."))
441a6146d50SZheng Yan 			continue;
442a6146d50SZheng Yan 
443410136f5SStephane Eranian 		/*
44446441bdcSMatt Fleming 		 * skip info files parsed in perf_pmu__new_alias()
445410136f5SStephane Eranian 		 */
44646441bdcSMatt Fleming 		if (pmu_alias_info_file(name))
447410136f5SStephane Eranian 			continue;
448410136f5SStephane Eranian 
44977f18153SJiri Olsa 		scnprintf(path, PATH_MAX, "%s/%s", dir, name);
450a6146d50SZheng Yan 
451a6146d50SZheng Yan 		file = fopen(path, "r");
452940db6dcSAndi Kleen 		if (!file) {
453940db6dcSAndi Kleen 			pr_debug("Cannot open %s\n", path);
454940db6dcSAndi Kleen 			continue;
455940db6dcSAndi Kleen 		}
456410136f5SStephane Eranian 
457940db6dcSAndi Kleen 		if (perf_pmu__new_alias(head, dir, name, file) < 0)
458940db6dcSAndi Kleen 			pr_debug("Cannot set up %s\n", name);
459a6146d50SZheng Yan 		fclose(file);
460a6146d50SZheng Yan 	}
461a6146d50SZheng Yan 
462a6146d50SZheng Yan 	closedir(event_dir);
463940db6dcSAndi Kleen 	return 0;
464a6146d50SZheng Yan }
465a6146d50SZheng Yan 
466a6146d50SZheng Yan /*
467a6146d50SZheng Yan  * Reading the pmu event aliases definition, which should be located at:
468a6146d50SZheng Yan  * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
469a6146d50SZheng Yan  */
470b6b96fb4SAdrian Hunter static int pmu_aliases(const char *name, struct list_head *head)
471a6146d50SZheng Yan {
472a6146d50SZheng Yan 	struct stat st;
473a6146d50SZheng Yan 	char path[PATH_MAX];
474cf38fadaSArnaldo Carvalho de Melo 	const char *sysfs = sysfs__mountpoint();
475a6146d50SZheng Yan 
476a6146d50SZheng Yan 	if (!sysfs)
477a6146d50SZheng Yan 		return -1;
478a6146d50SZheng Yan 
479a6146d50SZheng Yan 	snprintf(path, PATH_MAX,
480a6146d50SZheng Yan 		 "%s/bus/event_source/devices/%s/events", sysfs, name);
481a6146d50SZheng Yan 
482a6146d50SZheng Yan 	if (stat(path, &st) < 0)
4833fded963SJiri Olsa 		return 0;	 /* no error if 'events' does not exist */
484a6146d50SZheng Yan 
485a6146d50SZheng Yan 	if (pmu_aliases_parse(path, head))
486a6146d50SZheng Yan 		return -1;
487a6146d50SZheng Yan 
488a6146d50SZheng Yan 	return 0;
489a6146d50SZheng Yan }
490a6146d50SZheng Yan 
4915c6ccc37SArnaldo Carvalho de Melo static int pmu_alias_terms(struct perf_pmu_alias *alias,
492a6146d50SZheng Yan 			   struct list_head *terms)
493a6146d50SZheng Yan {
4947c2f8164SJiri Olsa 	struct parse_events_term *term, *cloned;
495a6146d50SZheng Yan 	LIST_HEAD(list);
496a6146d50SZheng Yan 	int ret;
497a6146d50SZheng Yan 
498a6146d50SZheng Yan 	list_for_each_entry(term, &alias->terms, list) {
4997c2f8164SJiri Olsa 		ret = parse_events_term__clone(&cloned, term);
500a6146d50SZheng Yan 		if (ret) {
501682dc24cSArnaldo Carvalho de Melo 			parse_events_terms__purge(&list);
502a6146d50SZheng Yan 			return ret;
503a6146d50SZheng Yan 		}
504c2f1ceadSAndi Kleen 		/*
505c2f1ceadSAndi Kleen 		 * Weak terms don't override command line options,
506c2f1ceadSAndi Kleen 		 * which we don't want for implicit terms in aliases.
507c2f1ceadSAndi Kleen 		 */
508c2f1ceadSAndi Kleen 		cloned->weak = true;
5097c2f8164SJiri Olsa 		list_add_tail(&cloned->list, &list);
510a6146d50SZheng Yan 	}
511a6146d50SZheng Yan 	list_splice(&list, terms);
512a6146d50SZheng Yan 	return 0;
513a6146d50SZheng Yan }
514a6146d50SZheng Yan 
515cd82a32eSJiri Olsa /*
516cd82a32eSJiri Olsa  * Reading/parsing the default pmu type value, which should be
517cd82a32eSJiri Olsa  * located at:
518cd82a32eSJiri Olsa  * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
519cd82a32eSJiri Olsa  */
520b6b96fb4SAdrian Hunter static int pmu_type(const char *name, __u32 *type)
521cd82a32eSJiri Olsa {
522cd82a32eSJiri Olsa 	struct stat st;
523cd82a32eSJiri Olsa 	char path[PATH_MAX];
524cd82a32eSJiri Olsa 	FILE *file;
525cd82a32eSJiri Olsa 	int ret = 0;
526cf38fadaSArnaldo Carvalho de Melo 	const char *sysfs = sysfs__mountpoint();
527cd82a32eSJiri Olsa 
528cd82a32eSJiri Olsa 	if (!sysfs)
529cd82a32eSJiri Olsa 		return -1;
530cd82a32eSJiri Olsa 
531cd82a32eSJiri Olsa 	snprintf(path, PATH_MAX,
53250a9667cSRobert Richter 		 "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name);
533cd82a32eSJiri Olsa 
534cd82a32eSJiri Olsa 	if (stat(path, &st) < 0)
535cd82a32eSJiri Olsa 		return -1;
536cd82a32eSJiri Olsa 
537cd82a32eSJiri Olsa 	file = fopen(path, "r");
538cd82a32eSJiri Olsa 	if (!file)
539cd82a32eSJiri Olsa 		return -EINVAL;
540cd82a32eSJiri Olsa 
541cd82a32eSJiri Olsa 	if (1 != fscanf(file, "%u", type))
542cd82a32eSJiri Olsa 		ret = -1;
543cd82a32eSJiri Olsa 
544cd82a32eSJiri Olsa 	fclose(file);
545cd82a32eSJiri Olsa 	return ret;
546cd82a32eSJiri Olsa }
547cd82a32eSJiri Olsa 
54850a9667cSRobert Richter /* Add all pmus in sysfs to pmu list: */
54950a9667cSRobert Richter static void pmu_read_sysfs(void)
55050a9667cSRobert Richter {
55150a9667cSRobert Richter 	char path[PATH_MAX];
55250a9667cSRobert Richter 	DIR *dir;
55350a9667cSRobert Richter 	struct dirent *dent;
554cf38fadaSArnaldo Carvalho de Melo 	const char *sysfs = sysfs__mountpoint();
55550a9667cSRobert Richter 
55650a9667cSRobert Richter 	if (!sysfs)
55750a9667cSRobert Richter 		return;
55850a9667cSRobert Richter 
55950a9667cSRobert Richter 	snprintf(path, PATH_MAX,
56050a9667cSRobert Richter 		 "%s" EVENT_SOURCE_DEVICE_PATH, sysfs);
56150a9667cSRobert Richter 
56250a9667cSRobert Richter 	dir = opendir(path);
56350a9667cSRobert Richter 	if (!dir)
56450a9667cSRobert Richter 		return;
56550a9667cSRobert Richter 
56650a9667cSRobert Richter 	while ((dent = readdir(dir))) {
56750a9667cSRobert Richter 		if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
56850a9667cSRobert Richter 			continue;
56950a9667cSRobert Richter 		/* add to static LIST_HEAD(pmus): */
57050a9667cSRobert Richter 		perf_pmu__find(dent->d_name);
57150a9667cSRobert Richter 	}
57250a9667cSRobert Richter 
57350a9667cSRobert Richter 	closedir(dir);
57450a9667cSRobert Richter }
57550a9667cSRobert Richter 
57666ec1191SMark Rutland static struct cpu_map *__pmu_cpumask(const char *path)
57766ec1191SMark Rutland {
57866ec1191SMark Rutland 	FILE *file;
57966ec1191SMark Rutland 	struct cpu_map *cpus;
58066ec1191SMark Rutland 
58166ec1191SMark Rutland 	file = fopen(path, "r");
58266ec1191SMark Rutland 	if (!file)
58366ec1191SMark Rutland 		return NULL;
58466ec1191SMark Rutland 
58566ec1191SMark Rutland 	cpus = cpu_map__read(file);
58666ec1191SMark Rutland 	fclose(file);
58766ec1191SMark Rutland 	return cpus;
58866ec1191SMark Rutland }
58966ec1191SMark Rutland 
59066ec1191SMark Rutland /*
59166ec1191SMark Rutland  * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64)
59266ec1191SMark Rutland  * may have a "cpus" file.
59366ec1191SMark Rutland  */
59466ec1191SMark Rutland #define CPUS_TEMPLATE_UNCORE	"%s/bus/event_source/devices/%s/cpumask"
59566ec1191SMark Rutland #define CPUS_TEMPLATE_CPU	"%s/bus/event_source/devices/%s/cpus"
59666ec1191SMark Rutland 
597b6b96fb4SAdrian Hunter static struct cpu_map *pmu_cpumask(const char *name)
5987ae92e74SYan, Zheng {
5997ae92e74SYan, Zheng 	char path[PATH_MAX];
6007ae92e74SYan, Zheng 	struct cpu_map *cpus;
601cf38fadaSArnaldo Carvalho de Melo 	const char *sysfs = sysfs__mountpoint();
6027e3fcffeSMark Rutland 	const char *templates[] = {
60366ec1191SMark Rutland 		CPUS_TEMPLATE_UNCORE,
60466ec1191SMark Rutland 		CPUS_TEMPLATE_CPU,
6057e3fcffeSMark Rutland 		NULL
6067e3fcffeSMark Rutland 	};
6077e3fcffeSMark Rutland 	const char **template;
6087ae92e74SYan, Zheng 
6097ae92e74SYan, Zheng 	if (!sysfs)
6107ae92e74SYan, Zheng 		return NULL;
6117ae92e74SYan, Zheng 
6127e3fcffeSMark Rutland 	for (template = templates; *template; template++) {
6137e3fcffeSMark Rutland 		snprintf(path, PATH_MAX, *template, sysfs, name);
61466ec1191SMark Rutland 		cpus = __pmu_cpumask(path);
61566ec1191SMark Rutland 		if (cpus)
61666ec1191SMark Rutland 			return cpus;
6177e3fcffeSMark Rutland 	}
6187ae92e74SYan, Zheng 
6197ae92e74SYan, Zheng 	return NULL;
62066ec1191SMark Rutland }
6217ae92e74SYan, Zheng 
62266ec1191SMark Rutland static bool pmu_is_uncore(const char *name)
62366ec1191SMark Rutland {
62466ec1191SMark Rutland 	char path[PATH_MAX];
62566ec1191SMark Rutland 	struct cpu_map *cpus;
62666ec1191SMark Rutland 	const char *sysfs = sysfs__mountpoint();
6277ae92e74SYan, Zheng 
62866ec1191SMark Rutland 	snprintf(path, PATH_MAX, CPUS_TEMPLATE_UNCORE, sysfs, name);
62966ec1191SMark Rutland 	cpus = __pmu_cpumask(path);
63066ec1191SMark Rutland 	cpu_map__put(cpus);
63166ec1191SMark Rutland 
63266ec1191SMark Rutland 	return !!cpus;
6337ae92e74SYan, Zheng }
6347ae92e74SYan, Zheng 
635933f82ffSSukadev Bhattiprolu /*
63614b22ae0SGanapatrao Kulkarni  *  PMU CORE devices have different name other than cpu in sysfs on some
637292c34c1SKan Liang  *  platforms.
638292c34c1SKan Liang  *  Looking for possible sysfs files to identify the arm core device.
63914b22ae0SGanapatrao Kulkarni  */
640292c34c1SKan Liang static int is_arm_pmu_core(const char *name)
64114b22ae0SGanapatrao Kulkarni {
64214b22ae0SGanapatrao Kulkarni 	struct stat st;
64314b22ae0SGanapatrao Kulkarni 	char path[PATH_MAX];
64414b22ae0SGanapatrao Kulkarni 	const char *sysfs = sysfs__mountpoint();
64514b22ae0SGanapatrao Kulkarni 
64614b22ae0SGanapatrao Kulkarni 	if (!sysfs)
64714b22ae0SGanapatrao Kulkarni 		return 0;
64814b22ae0SGanapatrao Kulkarni 
64914b22ae0SGanapatrao Kulkarni 	/* Look for cpu sysfs (specific to arm) */
65014b22ae0SGanapatrao Kulkarni 	scnprintf(path, PATH_MAX, "%s/bus/event_source/devices/%s/cpus",
65114b22ae0SGanapatrao Kulkarni 				sysfs, name);
65214b22ae0SGanapatrao Kulkarni 	if (stat(path, &st) == 0)
65314b22ae0SGanapatrao Kulkarni 		return 1;
65414b22ae0SGanapatrao Kulkarni 
655038586c3SThomas Richter 	/* Look for cpu sysfs (specific to s390) */
656038586c3SThomas Richter 	scnprintf(path, PATH_MAX, "%s/bus/event_source/devices/%s",
657038586c3SThomas Richter 		  sysfs, name);
658038586c3SThomas Richter 	if (stat(path, &st) == 0 && !strncmp(name, "cpum_", 5))
659038586c3SThomas Richter 		return 1;
660038586c3SThomas Richter 
66114b22ae0SGanapatrao Kulkarni 	return 0;
66214b22ae0SGanapatrao Kulkarni }
66314b22ae0SGanapatrao Kulkarni 
66414b22ae0SGanapatrao Kulkarni /*
665933f82ffSSukadev Bhattiprolu  * Return the CPU id as a raw string.
666933f82ffSSukadev Bhattiprolu  *
667933f82ffSSukadev Bhattiprolu  * Each architecture should provide a more precise id string that
668933f82ffSSukadev Bhattiprolu  * can be use to match the architecture's "mapfile".
669933f82ffSSukadev Bhattiprolu  */
67054e32dc0SGanapatrao Kulkarni char * __weak get_cpuid_str(struct perf_pmu *pmu __maybe_unused)
671933f82ffSSukadev Bhattiprolu {
672933f82ffSSukadev Bhattiprolu 	return NULL;
673933f82ffSSukadev Bhattiprolu }
674933f82ffSSukadev Bhattiprolu 
6754cb7d3ecSThomas Richter /* Return zero when the cpuid from the mapfile.csv matches the
6764cb7d3ecSThomas Richter  * cpuid string generated on this platform.
6774cb7d3ecSThomas Richter  * Otherwise return non-zero.
6784cb7d3ecSThomas Richter  */
679ce04abfbSThomas Richter int strcmp_cpuid_str(const char *mapcpuid, const char *cpuid)
6804cb7d3ecSThomas Richter {
6814cb7d3ecSThomas Richter 	regex_t re;
6824cb7d3ecSThomas Richter 	regmatch_t pmatch[1];
6834cb7d3ecSThomas Richter 	int match;
6844cb7d3ecSThomas Richter 
6854cb7d3ecSThomas Richter 	if (regcomp(&re, mapcpuid, REG_EXTENDED) != 0) {
6864cb7d3ecSThomas Richter 		/* Warn unable to generate match particular string. */
6874cb7d3ecSThomas Richter 		pr_info("Invalid regular expression %s\n", mapcpuid);
6884cb7d3ecSThomas Richter 		return 1;
6894cb7d3ecSThomas Richter 	}
6904cb7d3ecSThomas Richter 
6914cb7d3ecSThomas Richter 	match = !regexec(&re, cpuid, 1, pmatch, 0);
6924cb7d3ecSThomas Richter 	regfree(&re);
6934cb7d3ecSThomas Richter 	if (match) {
6944cb7d3ecSThomas Richter 		size_t match_len = (pmatch[0].rm_eo - pmatch[0].rm_so);
6954cb7d3ecSThomas Richter 
6964cb7d3ecSThomas Richter 		/* Verify the entire string matched. */
6974cb7d3ecSThomas Richter 		if (match_len == strlen(cpuid))
6984cb7d3ecSThomas Richter 			return 0;
6994cb7d3ecSThomas Richter 	}
7004cb7d3ecSThomas Richter 	return 1;
7014cb7d3ecSThomas Richter }
7024cb7d3ecSThomas Richter 
70354e32dc0SGanapatrao Kulkarni static char *perf_pmu__getcpuid(struct perf_pmu *pmu)
704d77ade9fSAndi Kleen {
705d77ade9fSAndi Kleen 	char *cpuid;
706d77ade9fSAndi Kleen 	static bool printed;
707d77ade9fSAndi Kleen 
708d77ade9fSAndi Kleen 	cpuid = getenv("PERF_CPUID");
709d77ade9fSAndi Kleen 	if (cpuid)
710d77ade9fSAndi Kleen 		cpuid = strdup(cpuid);
711d77ade9fSAndi Kleen 	if (!cpuid)
71254e32dc0SGanapatrao Kulkarni 		cpuid = get_cpuid_str(pmu);
713d77ade9fSAndi Kleen 	if (!cpuid)
714d77ade9fSAndi Kleen 		return NULL;
715d77ade9fSAndi Kleen 
716d77ade9fSAndi Kleen 	if (!printed) {
717d77ade9fSAndi Kleen 		pr_debug("Using CPUID %s\n", cpuid);
718d77ade9fSAndi Kleen 		printed = true;
719d77ade9fSAndi Kleen 	}
720d77ade9fSAndi Kleen 	return cpuid;
721d77ade9fSAndi Kleen }
722d77ade9fSAndi Kleen 
72354e32dc0SGanapatrao Kulkarni struct pmu_events_map *perf_pmu__find_map(struct perf_pmu *pmu)
724d77ade9fSAndi Kleen {
725d77ade9fSAndi Kleen 	struct pmu_events_map *map;
72654e32dc0SGanapatrao Kulkarni 	char *cpuid = perf_pmu__getcpuid(pmu);
727d77ade9fSAndi Kleen 	int i;
728d77ade9fSAndi Kleen 
729de3d0f12SGanapatrao Kulkarni 	/* on some platforms which uses cpus map, cpuid can be NULL for
730de3d0f12SGanapatrao Kulkarni 	 * PMUs other than CORE PMUs.
731de3d0f12SGanapatrao Kulkarni 	 */
732de3d0f12SGanapatrao Kulkarni 	if (!cpuid)
733de3d0f12SGanapatrao Kulkarni 		return NULL;
734de3d0f12SGanapatrao Kulkarni 
735d77ade9fSAndi Kleen 	i = 0;
736d77ade9fSAndi Kleen 	for (;;) {
737d77ade9fSAndi Kleen 		map = &pmu_events_map[i++];
738d77ade9fSAndi Kleen 		if (!map->table) {
739d77ade9fSAndi Kleen 			map = NULL;
740d77ade9fSAndi Kleen 			break;
741d77ade9fSAndi Kleen 		}
742d77ade9fSAndi Kleen 
7434cb7d3ecSThomas Richter 		if (!strcmp_cpuid_str(map->cpuid, cpuid))
744d77ade9fSAndi Kleen 			break;
745d77ade9fSAndi Kleen 	}
746d77ade9fSAndi Kleen 	free(cpuid);
747d77ade9fSAndi Kleen 	return map;
748d77ade9fSAndi Kleen }
749d77ade9fSAndi Kleen 
750933f82ffSSukadev Bhattiprolu /*
751933f82ffSSukadev Bhattiprolu  * From the pmu_events_map, find the table of PMU events that corresponds
752933f82ffSSukadev Bhattiprolu  * to the current running CPU. Then, add all PMU events from that table
753933f82ffSSukadev Bhattiprolu  * as aliases.
754933f82ffSSukadev Bhattiprolu  */
75554e32dc0SGanapatrao Kulkarni static void pmu_add_cpu_aliases(struct list_head *head, struct perf_pmu *pmu)
756933f82ffSSukadev Bhattiprolu {
757933f82ffSSukadev Bhattiprolu 	int i;
758933f82ffSSukadev Bhattiprolu 	struct pmu_events_map *map;
759933f82ffSSukadev Bhattiprolu 	struct pmu_event *pe;
76054e32dc0SGanapatrao Kulkarni 	const char *name = pmu->name;
761292c34c1SKan Liang 	const char *pname;
762933f82ffSSukadev Bhattiprolu 
76354e32dc0SGanapatrao Kulkarni 	map = perf_pmu__find_map(pmu);
764d77ade9fSAndi Kleen 	if (!map)
765933f82ffSSukadev Bhattiprolu 		return;
766933f82ffSSukadev Bhattiprolu 
767933f82ffSSukadev Bhattiprolu 	/*
768933f82ffSSukadev Bhattiprolu 	 * Found a matching PMU events table. Create aliases
769933f82ffSSukadev Bhattiprolu 	 */
770933f82ffSSukadev Bhattiprolu 	i = 0;
771933f82ffSSukadev Bhattiprolu 	while (1) {
772fedb2b51SAndi Kleen 
773933f82ffSSukadev Bhattiprolu 		pe = &map->table[i++];
774b18f3e36SAndi Kleen 		if (!pe->name) {
775b18f3e36SAndi Kleen 			if (pe->metric_group || pe->metric_name)
776b18f3e36SAndi Kleen 				continue;
777933f82ffSSukadev Bhattiprolu 			break;
778b18f3e36SAndi Kleen 		}
779933f82ffSSukadev Bhattiprolu 
780292c34c1SKan Liang 		if (!is_arm_pmu_core(name)) {
781292c34c1SKan Liang 			pname = pe->pmu ? pe->pmu : "cpu";
782292c34c1SKan Liang 			if (strncmp(pname, name, strlen(pname)))
78314b22ae0SGanapatrao Kulkarni 				continue;
78414b22ae0SGanapatrao Kulkarni 		}
785fedb2b51SAndi Kleen 
786933f82ffSSukadev Bhattiprolu 		/* need type casts to override 'const' */
787933f82ffSSukadev Bhattiprolu 		__perf_pmu__new_alias(head, NULL, (char *)pe->name,
788c8d6828aSSukadev Bhattiprolu 				(char *)pe->desc, (char *)pe->event,
789fedb2b51SAndi Kleen 				(char *)pe->long_desc, (char *)pe->topic,
79000636c3bSAndi Kleen 				(char *)pe->unit, (char *)pe->perpkg,
79196284814SAndi Kleen 				(char *)pe->metric_expr,
79296284814SAndi Kleen 				(char *)pe->metric_name);
793933f82ffSSukadev Bhattiprolu 	}
794933f82ffSSukadev Bhattiprolu }
795933f82ffSSukadev Bhattiprolu 
796c5de47f2SSukadev Bhattiprolu struct perf_event_attr * __weak
797dc0a6202SAdrian Hunter perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
798dc0a6202SAdrian Hunter {
799dc0a6202SAdrian Hunter 	return NULL;
800dc0a6202SAdrian Hunter }
801dc0a6202SAdrian Hunter 
802b6b96fb4SAdrian Hunter static struct perf_pmu *pmu_lookup(const char *name)
803cd82a32eSJiri Olsa {
804cd82a32eSJiri Olsa 	struct perf_pmu *pmu;
805cd82a32eSJiri Olsa 	LIST_HEAD(format);
806a6146d50SZheng Yan 	LIST_HEAD(aliases);
807cd82a32eSJiri Olsa 	__u32 type;
808cd82a32eSJiri Olsa 
809cd82a32eSJiri Olsa 	/*
810cd82a32eSJiri Olsa 	 * The pmu data we store & need consists of the pmu
811cd82a32eSJiri Olsa 	 * type value and format definitions. Load both right
812cd82a32eSJiri Olsa 	 * now.
813cd82a32eSJiri Olsa 	 */
814cd82a32eSJiri Olsa 	if (pmu_format(name, &format))
815cd82a32eSJiri Olsa 		return NULL;
816cd82a32eSJiri Olsa 
81715b22ed3SAndi Kleen 	/*
81815b22ed3SAndi Kleen 	 * Check the type first to avoid unnecessary work.
81915b22ed3SAndi Kleen 	 */
82015b22ed3SAndi Kleen 	if (pmu_type(name, &type))
82115b22ed3SAndi Kleen 		return NULL;
82215b22ed3SAndi Kleen 
8233fded963SJiri Olsa 	if (pmu_aliases(name, &aliases))
8243fded963SJiri Olsa 		return NULL;
8253fded963SJiri Olsa 
826cd82a32eSJiri Olsa 	pmu = zalloc(sizeof(*pmu));
827cd82a32eSJiri Olsa 	if (!pmu)
828cd82a32eSJiri Olsa 		return NULL;
829cd82a32eSJiri Olsa 
8307ae92e74SYan, Zheng 	pmu->cpus = pmu_cpumask(name);
83154e32dc0SGanapatrao Kulkarni 	pmu->name = strdup(name);
83254e32dc0SGanapatrao Kulkarni 	pmu->type = type;
83366ec1191SMark Rutland 	pmu->is_uncore = pmu_is_uncore(name);
83454e32dc0SGanapatrao Kulkarni 	pmu_add_cpu_aliases(&aliases, pmu);
83566ec1191SMark Rutland 
836cd82a32eSJiri Olsa 	INIT_LIST_HEAD(&pmu->format);
837a6146d50SZheng Yan 	INIT_LIST_HEAD(&pmu->aliases);
838cd82a32eSJiri Olsa 	list_splice(&format, &pmu->format);
839a6146d50SZheng Yan 	list_splice(&aliases, &pmu->aliases);
8409bc8f9feSRobert Richter 	list_add_tail(&pmu->list, &pmus);
841dc0a6202SAdrian Hunter 
842dc0a6202SAdrian Hunter 	pmu->default_config = perf_pmu__get_default_config(pmu);
843dc0a6202SAdrian Hunter 
844cd82a32eSJiri Olsa 	return pmu;
845cd82a32eSJiri Olsa }
846cd82a32eSJiri Olsa 
847b6b96fb4SAdrian Hunter static struct perf_pmu *pmu_find(const char *name)
848cd82a32eSJiri Olsa {
849cd82a32eSJiri Olsa 	struct perf_pmu *pmu;
850cd82a32eSJiri Olsa 
851cd82a32eSJiri Olsa 	list_for_each_entry(pmu, &pmus, list)
852cd82a32eSJiri Olsa 		if (!strcmp(pmu->name, name))
853cd82a32eSJiri Olsa 			return pmu;
854cd82a32eSJiri Olsa 
855cd82a32eSJiri Olsa 	return NULL;
856cd82a32eSJiri Olsa }
857cd82a32eSJiri Olsa 
85850a9667cSRobert Richter struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
85950a9667cSRobert Richter {
86050a9667cSRobert Richter 	/*
86150a9667cSRobert Richter 	 * pmu iterator: If pmu is NULL, we start at the begin,
86250a9667cSRobert Richter 	 * otherwise return the next pmu. Returns NULL on end.
86350a9667cSRobert Richter 	 */
86450a9667cSRobert Richter 	if (!pmu) {
86550a9667cSRobert Richter 		pmu_read_sysfs();
86650a9667cSRobert Richter 		pmu = list_prepare_entry(pmu, &pmus, list);
86750a9667cSRobert Richter 	}
86850a9667cSRobert Richter 	list_for_each_entry_continue(pmu, &pmus, list)
86950a9667cSRobert Richter 		return pmu;
87050a9667cSRobert Richter 	return NULL;
87150a9667cSRobert Richter }
87250a9667cSRobert Richter 
873b6b96fb4SAdrian Hunter struct perf_pmu *perf_pmu__find(const char *name)
874cd82a32eSJiri Olsa {
875cd82a32eSJiri Olsa 	struct perf_pmu *pmu;
876cd82a32eSJiri Olsa 
877cd82a32eSJiri Olsa 	/*
878cd82a32eSJiri Olsa 	 * Once PMU is loaded it stays in the list,
879cd82a32eSJiri Olsa 	 * so we keep us from multiple reading/parsing
880cd82a32eSJiri Olsa 	 * the pmu format definitions.
881cd82a32eSJiri Olsa 	 */
882cd82a32eSJiri Olsa 	pmu = pmu_find(name);
883cd82a32eSJiri Olsa 	if (pmu)
884cd82a32eSJiri Olsa 		return pmu;
885cd82a32eSJiri Olsa 
886cd82a32eSJiri Olsa 	return pmu_lookup(name);
887cd82a32eSJiri Olsa }
888cd82a32eSJiri Olsa 
8895c6ccc37SArnaldo Carvalho de Melo static struct perf_pmu_format *
89009ff6071SAdrian Hunter pmu_find_format(struct list_head *formats, const char *name)
891cd82a32eSJiri Olsa {
8925c6ccc37SArnaldo Carvalho de Melo 	struct perf_pmu_format *format;
893cd82a32eSJiri Olsa 
894cd82a32eSJiri Olsa 	list_for_each_entry(format, formats, list)
895cd82a32eSJiri Olsa 		if (!strcmp(format->name, name))
896cd82a32eSJiri Olsa 			return format;
897cd82a32eSJiri Olsa 
898cd82a32eSJiri Olsa 	return NULL;
899cd82a32eSJiri Olsa }
900cd82a32eSJiri Olsa 
90109ff6071SAdrian Hunter __u64 perf_pmu__format_bits(struct list_head *formats, const char *name)
90209ff6071SAdrian Hunter {
90309ff6071SAdrian Hunter 	struct perf_pmu_format *format = pmu_find_format(formats, name);
90409ff6071SAdrian Hunter 	__u64 bits = 0;
90509ff6071SAdrian Hunter 	int fbit;
90609ff6071SAdrian Hunter 
90709ff6071SAdrian Hunter 	if (!format)
90809ff6071SAdrian Hunter 		return 0;
90909ff6071SAdrian Hunter 
91009ff6071SAdrian Hunter 	for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
91109ff6071SAdrian Hunter 		bits |= 1ULL << fbit;
91209ff6071SAdrian Hunter 
91309ff6071SAdrian Hunter 	return bits;
91409ff6071SAdrian Hunter }
91509ff6071SAdrian Hunter 
916cd82a32eSJiri Olsa /*
917dc0a6202SAdrian Hunter  * Sets value based on the format definition (format parameter)
918cd82a32eSJiri Olsa  * and unformated value (value parameter).
919cd82a32eSJiri Olsa  */
920dc0a6202SAdrian Hunter static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
921dc0a6202SAdrian Hunter 			     bool zero)
922cd82a32eSJiri Olsa {
923cd82a32eSJiri Olsa 	unsigned long fbit, vbit;
924cd82a32eSJiri Olsa 
925cd82a32eSJiri Olsa 	for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
926cd82a32eSJiri Olsa 
927cd82a32eSJiri Olsa 		if (!test_bit(fbit, format))
928cd82a32eSJiri Olsa 			continue;
929cd82a32eSJiri Olsa 
930dc0a6202SAdrian Hunter 		if (value & (1llu << vbit++))
931dc0a6202SAdrian Hunter 			*v |= (1llu << fbit);
932dc0a6202SAdrian Hunter 		else if (zero)
933dc0a6202SAdrian Hunter 			*v &= ~(1llu << fbit);
934cd82a32eSJiri Olsa 	}
935cd82a32eSJiri Olsa }
936cd82a32eSJiri Olsa 
9370efe6b67SAdrian Hunter static __u64 pmu_format_max_value(const unsigned long *format)
9380efe6b67SAdrian Hunter {
939ac0e2cd5SKan Liang 	__u64 w = 0;
940ac0e2cd5SKan Liang 	int fbit;
9410efe6b67SAdrian Hunter 
942ac0e2cd5SKan Liang 	for_each_set_bit(fbit, format, PERF_PMU_FORMAT_BITS)
943ac0e2cd5SKan Liang 		w |= (1ULL << fbit);
944ac0e2cd5SKan Liang 
945ac0e2cd5SKan Liang 	return w;
9460efe6b67SAdrian Hunter }
9470efe6b67SAdrian Hunter 
948cd82a32eSJiri Olsa /*
949688d4dfcSCody P Schafer  * Term is a string term, and might be a param-term. Try to look up it's value
950688d4dfcSCody P Schafer  * in the remaining terms.
951688d4dfcSCody P Schafer  * - We have a term like "base-or-format-term=param-term",
952688d4dfcSCody P Schafer  * - We need to find the value supplied for "param-term" (with param-term named
953688d4dfcSCody P Schafer  *   in a config string) later on in the term list.
954688d4dfcSCody P Schafer  */
955688d4dfcSCody P Schafer static int pmu_resolve_param_term(struct parse_events_term *term,
956688d4dfcSCody P Schafer 				  struct list_head *head_terms,
957688d4dfcSCody P Schafer 				  __u64 *value)
958688d4dfcSCody P Schafer {
959688d4dfcSCody P Schafer 	struct parse_events_term *t;
960688d4dfcSCody P Schafer 
961688d4dfcSCody P Schafer 	list_for_each_entry(t, head_terms, list) {
962688d4dfcSCody P Schafer 		if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
963688d4dfcSCody P Schafer 			if (!strcmp(t->config, term->config)) {
964688d4dfcSCody P Schafer 				t->used = true;
965688d4dfcSCody P Schafer 				*value = t->val.num;
966688d4dfcSCody P Schafer 				return 0;
967688d4dfcSCody P Schafer 			}
968688d4dfcSCody P Schafer 		}
969688d4dfcSCody P Schafer 	}
970688d4dfcSCody P Schafer 
971bb963e16SNamhyung Kim 	if (verbose > 0)
972688d4dfcSCody P Schafer 		printf("Required parameter '%s' not specified\n", term->config);
973688d4dfcSCody P Schafer 
974688d4dfcSCody P Schafer 	return -1;
975688d4dfcSCody P Schafer }
976688d4dfcSCody P Schafer 
977ffeb883eSHe Kuang static char *pmu_formats_string(struct list_head *formats)
978e64b020bSJiri Olsa {
979e64b020bSJiri Olsa 	struct perf_pmu_format *format;
98011db4e29SMasami Hiramatsu 	char *str = NULL;
98111db4e29SMasami Hiramatsu 	struct strbuf buf = STRBUF_INIT;
982e64b020bSJiri Olsa 	unsigned i = 0;
983e64b020bSJiri Olsa 
984ffeb883eSHe Kuang 	if (!formats)
985e64b020bSJiri Olsa 		return NULL;
986e64b020bSJiri Olsa 
987e64b020bSJiri Olsa 	/* sysfs exported terms */
988ffeb883eSHe Kuang 	list_for_each_entry(format, formats, list)
98911db4e29SMasami Hiramatsu 		if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
99011db4e29SMasami Hiramatsu 			goto error;
991e64b020bSJiri Olsa 
992ffeb883eSHe Kuang 	str = strbuf_detach(&buf, NULL);
99311db4e29SMasami Hiramatsu error:
994ffeb883eSHe Kuang 	strbuf_release(&buf);
995e64b020bSJiri Olsa 
996e64b020bSJiri Olsa 	return str;
997e64b020bSJiri Olsa }
998e64b020bSJiri Olsa 
999688d4dfcSCody P Schafer /*
1000cd82a32eSJiri Olsa  * Setup one of config[12] attr members based on the
100188aca8d9SCody P Schafer  * user input data - term parameter.
1002cd82a32eSJiri Olsa  */
1003cd82a32eSJiri Olsa static int pmu_config_term(struct list_head *formats,
1004cd82a32eSJiri Olsa 			   struct perf_event_attr *attr,
1005dc0a6202SAdrian Hunter 			   struct parse_events_term *term,
1006688d4dfcSCody P Schafer 			   struct list_head *head_terms,
1007e64b020bSJiri Olsa 			   bool zero, struct parse_events_error *err)
1008cd82a32eSJiri Olsa {
10095c6ccc37SArnaldo Carvalho de Melo 	struct perf_pmu_format *format;
1010cd82a32eSJiri Olsa 	__u64 *vp;
10110efe6b67SAdrian Hunter 	__u64 val, max_val;
1012cd82a32eSJiri Olsa 
1013cd82a32eSJiri Olsa 	/*
1014688d4dfcSCody P Schafer 	 * If this is a parameter we've already used for parameterized-eval,
1015688d4dfcSCody P Schafer 	 * skip it in normal eval.
1016688d4dfcSCody P Schafer 	 */
1017688d4dfcSCody P Schafer 	if (term->used)
1018688d4dfcSCody P Schafer 		return 0;
1019688d4dfcSCody P Schafer 
1020688d4dfcSCody P Schafer 	/*
1021cd82a32eSJiri Olsa 	 * Hardcoded terms should be already in, so nothing
1022cd82a32eSJiri Olsa 	 * to be done for them.
1023cd82a32eSJiri Olsa 	 */
1024cd82a32eSJiri Olsa 	if (parse_events__is_hardcoded_term(term))
1025cd82a32eSJiri Olsa 		return 0;
1026cd82a32eSJiri Olsa 
1027cd82a32eSJiri Olsa 	format = pmu_find_format(formats, term->config);
1028688d4dfcSCody P Schafer 	if (!format) {
1029bb963e16SNamhyung Kim 		if (verbose > 0)
1030688d4dfcSCody P Schafer 			printf("Invalid event/parameter '%s'\n", term->config);
1031e64b020bSJiri Olsa 		if (err) {
1032ffeb883eSHe Kuang 			char *pmu_term = pmu_formats_string(formats);
1033ffeb883eSHe Kuang 
1034e64b020bSJiri Olsa 			err->idx  = term->err_term;
1035e64b020bSJiri Olsa 			err->str  = strdup("unknown term");
1036ffeb883eSHe Kuang 			err->help = parse_events_formats_error_string(pmu_term);
1037ffeb883eSHe Kuang 			free(pmu_term);
1038e64b020bSJiri Olsa 		}
1039cd82a32eSJiri Olsa 		return -EINVAL;
1040688d4dfcSCody P Schafer 	}
1041cd82a32eSJiri Olsa 
1042cd82a32eSJiri Olsa 	switch (format->value) {
1043cd82a32eSJiri Olsa 	case PERF_PMU_FORMAT_VALUE_CONFIG:
1044cd82a32eSJiri Olsa 		vp = &attr->config;
1045cd82a32eSJiri Olsa 		break;
1046cd82a32eSJiri Olsa 	case PERF_PMU_FORMAT_VALUE_CONFIG1:
1047cd82a32eSJiri Olsa 		vp = &attr->config1;
1048cd82a32eSJiri Olsa 		break;
1049cd82a32eSJiri Olsa 	case PERF_PMU_FORMAT_VALUE_CONFIG2:
1050cd82a32eSJiri Olsa 		vp = &attr->config2;
1051cd82a32eSJiri Olsa 		break;
1052cd82a32eSJiri Olsa 	default:
1053cd82a32eSJiri Olsa 		return -EINVAL;
1054cd82a32eSJiri Olsa 	}
1055cd82a32eSJiri Olsa 
105616fa7e82SJiri Olsa 	/*
1057688d4dfcSCody P Schafer 	 * Either directly use a numeric term, or try to translate string terms
1058688d4dfcSCody P Schafer 	 * using event parameters.
105916fa7e82SJiri Olsa 	 */
106099e7138eSJiri Olsa 	if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
106199e7138eSJiri Olsa 		if (term->no_value &&
106299e7138eSJiri Olsa 		    bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
106399e7138eSJiri Olsa 			if (err) {
106499e7138eSJiri Olsa 				err->idx = term->err_val;
106599e7138eSJiri Olsa 				err->str = strdup("no value assigned for term");
106699e7138eSJiri Olsa 			}
106799e7138eSJiri Olsa 			return -EINVAL;
106899e7138eSJiri Olsa 		}
106999e7138eSJiri Olsa 
1070688d4dfcSCody P Schafer 		val = term->val.num;
107199e7138eSJiri Olsa 	} else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1072688d4dfcSCody P Schafer 		if (strcmp(term->val.str, "?")) {
1073bb963e16SNamhyung Kim 			if (verbose > 0) {
1074688d4dfcSCody P Schafer 				pr_info("Invalid sysfs entry %s=%s\n",
1075688d4dfcSCody P Schafer 						term->config, term->val.str);
1076e64b020bSJiri Olsa 			}
1077e64b020bSJiri Olsa 			if (err) {
1078e64b020bSJiri Olsa 				err->idx = term->err_val;
1079e64b020bSJiri Olsa 				err->str = strdup("expected numeric value");
1080e64b020bSJiri Olsa 			}
1081688d4dfcSCody P Schafer 			return -EINVAL;
1082688d4dfcSCody P Schafer 		}
1083688d4dfcSCody P Schafer 
1084688d4dfcSCody P Schafer 		if (pmu_resolve_param_term(term, head_terms, &val))
1085688d4dfcSCody P Schafer 			return -EINVAL;
1086688d4dfcSCody P Schafer 	} else
1087688d4dfcSCody P Schafer 		return -EINVAL;
1088688d4dfcSCody P Schafer 
10890efe6b67SAdrian Hunter 	max_val = pmu_format_max_value(format->bits);
10900efe6b67SAdrian Hunter 	if (val > max_val) {
10910efe6b67SAdrian Hunter 		if (err) {
10920efe6b67SAdrian Hunter 			err->idx = term->err_val;
10930efe6b67SAdrian Hunter 			if (asprintf(&err->str,
10940efe6b67SAdrian Hunter 				     "value too big for format, maximum is %llu",
10950efe6b67SAdrian Hunter 				     (unsigned long long)max_val) < 0)
10960efe6b67SAdrian Hunter 				err->str = strdup("value too big for format");
10970efe6b67SAdrian Hunter 			return -EINVAL;
10980efe6b67SAdrian Hunter 		}
10990efe6b67SAdrian Hunter 		/*
11000efe6b67SAdrian Hunter 		 * Assume we don't care if !err, in which case the value will be
11010efe6b67SAdrian Hunter 		 * silently truncated.
11020efe6b67SAdrian Hunter 		 */
11030efe6b67SAdrian Hunter 	}
11040efe6b67SAdrian Hunter 
1105688d4dfcSCody P Schafer 	pmu_format_value(format->bits, val, vp, zero);
1106cd82a32eSJiri Olsa 	return 0;
1107cd82a32eSJiri Olsa }
1108cd82a32eSJiri Olsa 
1109cff7f956SJiri Olsa int perf_pmu__config_terms(struct list_head *formats,
1110cff7f956SJiri Olsa 			   struct perf_event_attr *attr,
1111dc0a6202SAdrian Hunter 			   struct list_head *head_terms,
1112e64b020bSJiri Olsa 			   bool zero, struct parse_events_error *err)
1113cd82a32eSJiri Olsa {
11146cee6cd3SArnaldo Carvalho de Melo 	struct parse_events_term *term;
1115cd82a32eSJiri Olsa 
1116688d4dfcSCody P Schafer 	list_for_each_entry(term, head_terms, list) {
1117e64b020bSJiri Olsa 		if (pmu_config_term(formats, attr, term, head_terms,
1118e64b020bSJiri Olsa 				    zero, err))
1119cd82a32eSJiri Olsa 			return -EINVAL;
1120688d4dfcSCody P Schafer 	}
1121cd82a32eSJiri Olsa 
1122cd82a32eSJiri Olsa 	return 0;
1123cd82a32eSJiri Olsa }
1124cd82a32eSJiri Olsa 
1125cd82a32eSJiri Olsa /*
1126cd82a32eSJiri Olsa  * Configures event's 'attr' parameter based on the:
1127cd82a32eSJiri Olsa  * 1) users input - specified in terms parameter
1128cd82a32eSJiri Olsa  * 2) pmu format definitions - specified by pmu parameter
1129cd82a32eSJiri Olsa  */
1130cd82a32eSJiri Olsa int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
1131e64b020bSJiri Olsa 		     struct list_head *head_terms,
1132e64b020bSJiri Olsa 		     struct parse_events_error *err)
1133cd82a32eSJiri Olsa {
1134dc0a6202SAdrian Hunter 	bool zero = !!pmu->default_config;
1135dc0a6202SAdrian Hunter 
1136cd82a32eSJiri Olsa 	attr->type = pmu->type;
1137e64b020bSJiri Olsa 	return perf_pmu__config_terms(&pmu->format, attr, head_terms,
1138e64b020bSJiri Olsa 				      zero, err);
1139cd82a32eSJiri Olsa }
1140cd82a32eSJiri Olsa 
11415c6ccc37SArnaldo Carvalho de Melo static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
11426cee6cd3SArnaldo Carvalho de Melo 					     struct parse_events_term *term)
1143a6146d50SZheng Yan {
11445c6ccc37SArnaldo Carvalho de Melo 	struct perf_pmu_alias *alias;
1145a6146d50SZheng Yan 	char *name;
1146a6146d50SZheng Yan 
1147a6146d50SZheng Yan 	if (parse_events__is_hardcoded_term(term))
1148a6146d50SZheng Yan 		return NULL;
1149a6146d50SZheng Yan 
1150a6146d50SZheng Yan 	if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1151a6146d50SZheng Yan 		if (term->val.num != 1)
1152a6146d50SZheng Yan 			return NULL;
1153a6146d50SZheng Yan 		if (pmu_find_format(&pmu->format, term->config))
1154a6146d50SZheng Yan 			return NULL;
1155a6146d50SZheng Yan 		name = term->config;
1156a6146d50SZheng Yan 	} else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1157a6146d50SZheng Yan 		if (strcasecmp(term->config, "event"))
1158a6146d50SZheng Yan 			return NULL;
1159a6146d50SZheng Yan 		name = term->val.str;
1160a6146d50SZheng Yan 	} else {
1161a6146d50SZheng Yan 		return NULL;
1162a6146d50SZheng Yan 	}
1163a6146d50SZheng Yan 
1164a6146d50SZheng Yan 	list_for_each_entry(alias, &pmu->aliases, list) {
1165a6146d50SZheng Yan 		if (!strcasecmp(alias->name, name))
1166a6146d50SZheng Yan 			return alias;
1167a6146d50SZheng Yan 	}
1168a6146d50SZheng Yan 	return NULL;
1169a6146d50SZheng Yan }
1170a6146d50SZheng Yan 
1171410136f5SStephane Eranian 
11721d9e446bSJiri Olsa static int check_info_data(struct perf_pmu_alias *alias,
11731d9e446bSJiri Olsa 			   struct perf_pmu_info *info)
1174410136f5SStephane Eranian {
1175410136f5SStephane Eranian 	/*
1176410136f5SStephane Eranian 	 * Only one term in event definition can
11771d9e446bSJiri Olsa 	 * define unit, scale and snapshot, fail
11781d9e446bSJiri Olsa 	 * if there's more than one.
1179410136f5SStephane Eranian 	 */
1180b30a7d1fSArnaldo Carvalho de Melo 	if ((info->unit && alias->unit[0]) ||
11811d9e446bSJiri Olsa 	    (info->scale && alias->scale) ||
11821d9e446bSJiri Olsa 	    (info->snapshot && alias->snapshot))
1183410136f5SStephane Eranian 		return -EINVAL;
1184410136f5SStephane Eranian 
1185b30a7d1fSArnaldo Carvalho de Melo 	if (alias->unit[0])
11861d9e446bSJiri Olsa 		info->unit = alias->unit;
1187410136f5SStephane Eranian 
1188410136f5SStephane Eranian 	if (alias->scale)
11891d9e446bSJiri Olsa 		info->scale = alias->scale;
11901d9e446bSJiri Olsa 
11911d9e446bSJiri Olsa 	if (alias->snapshot)
11921d9e446bSJiri Olsa 		info->snapshot = alias->snapshot;
1193410136f5SStephane Eranian 
1194410136f5SStephane Eranian 	return 0;
1195410136f5SStephane Eranian }
1196410136f5SStephane Eranian 
1197a6146d50SZheng Yan /*
1198a6146d50SZheng Yan  * Find alias in the terms list and replace it with the terms
1199a6146d50SZheng Yan  * defined for the alias
1200a6146d50SZheng Yan  */
1201410136f5SStephane Eranian int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
120246441bdcSMatt Fleming 			  struct perf_pmu_info *info)
1203a6146d50SZheng Yan {
12046cee6cd3SArnaldo Carvalho de Melo 	struct parse_events_term *term, *h;
12055c6ccc37SArnaldo Carvalho de Melo 	struct perf_pmu_alias *alias;
1206a6146d50SZheng Yan 	int ret;
1207a6146d50SZheng Yan 
1208044330c1SMatt Fleming 	info->per_pkg = false;
1209044330c1SMatt Fleming 
12108a398897SStephane Eranian 	/*
12118a398897SStephane Eranian 	 * Mark unit and scale as not set
12128a398897SStephane Eranian 	 * (different from default values, see below)
12138a398897SStephane Eranian 	 */
121446441bdcSMatt Fleming 	info->unit     = NULL;
121546441bdcSMatt Fleming 	info->scale    = 0.0;
12161d9e446bSJiri Olsa 	info->snapshot = false;
121737932c18SAndi Kleen 	info->metric_expr = NULL;
121896284814SAndi Kleen 	info->metric_name = NULL;
1219410136f5SStephane Eranian 
1220a6146d50SZheng Yan 	list_for_each_entry_safe(term, h, head_terms, list) {
1221a6146d50SZheng Yan 		alias = pmu_find_alias(pmu, term);
1222a6146d50SZheng Yan 		if (!alias)
1223a6146d50SZheng Yan 			continue;
1224a6146d50SZheng Yan 		ret = pmu_alias_terms(alias, &term->list);
1225a6146d50SZheng Yan 		if (ret)
1226a6146d50SZheng Yan 			return ret;
1227410136f5SStephane Eranian 
12281d9e446bSJiri Olsa 		ret = check_info_data(alias, info);
1229410136f5SStephane Eranian 		if (ret)
1230410136f5SStephane Eranian 			return ret;
1231410136f5SStephane Eranian 
1232044330c1SMatt Fleming 		if (alias->per_pkg)
1233044330c1SMatt Fleming 			info->per_pkg = true;
123437932c18SAndi Kleen 		info->metric_expr = alias->metric_expr;
123596284814SAndi Kleen 		info->metric_name = alias->metric_name;
1236044330c1SMatt Fleming 
1237a6146d50SZheng Yan 		list_del(&term->list);
1238a6146d50SZheng Yan 		free(term);
1239a6146d50SZheng Yan 	}
12408a398897SStephane Eranian 
12418a398897SStephane Eranian 	/*
12428a398897SStephane Eranian 	 * if no unit or scale foundin aliases, then
12438a398897SStephane Eranian 	 * set defaults as for evsel
12448a398897SStephane Eranian 	 * unit cannot left to NULL
12458a398897SStephane Eranian 	 */
124646441bdcSMatt Fleming 	if (info->unit == NULL)
124746441bdcSMatt Fleming 		info->unit   = "";
12488a398897SStephane Eranian 
124946441bdcSMatt Fleming 	if (info->scale == 0.0)
125046441bdcSMatt Fleming 		info->scale  = 1.0;
12518a398897SStephane Eranian 
1252a6146d50SZheng Yan 	return 0;
1253a6146d50SZheng Yan }
1254a6146d50SZheng Yan 
1255cd82a32eSJiri Olsa int perf_pmu__new_format(struct list_head *list, char *name,
1256cd82a32eSJiri Olsa 			 int config, unsigned long *bits)
1257cd82a32eSJiri Olsa {
12585c6ccc37SArnaldo Carvalho de Melo 	struct perf_pmu_format *format;
1259cd82a32eSJiri Olsa 
1260cd82a32eSJiri Olsa 	format = zalloc(sizeof(*format));
1261cd82a32eSJiri Olsa 	if (!format)
1262cd82a32eSJiri Olsa 		return -ENOMEM;
1263cd82a32eSJiri Olsa 
1264cd82a32eSJiri Olsa 	format->name = strdup(name);
1265cd82a32eSJiri Olsa 	format->value = config;
1266cd82a32eSJiri Olsa 	memcpy(format->bits, bits, sizeof(format->bits));
1267cd82a32eSJiri Olsa 
1268cd82a32eSJiri Olsa 	list_add_tail(&format->list, list);
1269cd82a32eSJiri Olsa 	return 0;
1270cd82a32eSJiri Olsa }
1271cd82a32eSJiri Olsa 
1272cd82a32eSJiri Olsa void perf_pmu__set_format(unsigned long *bits, long from, long to)
1273cd82a32eSJiri Olsa {
1274cd82a32eSJiri Olsa 	long b;
1275cd82a32eSJiri Olsa 
1276cd82a32eSJiri Olsa 	if (!to)
1277cd82a32eSJiri Olsa 		to = from;
1278cd82a32eSJiri Olsa 
127915268138SSukadev Bhattiprolu 	memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
1280cd82a32eSJiri Olsa 	for (b = from; b <= to; b++)
1281cd82a32eSJiri Olsa 		set_bit(b, bits);
1282cd82a32eSJiri Olsa }
1283dc098b35SAndi Kleen 
1284aaea3617SCody P Schafer static int sub_non_neg(int a, int b)
1285aaea3617SCody P Schafer {
1286aaea3617SCody P Schafer 	if (b > a)
1287aaea3617SCody P Schafer 		return 0;
1288aaea3617SCody P Schafer 	return a - b;
1289aaea3617SCody P Schafer }
1290aaea3617SCody P Schafer 
1291dc098b35SAndi Kleen static char *format_alias(char *buf, int len, struct perf_pmu *pmu,
1292dc098b35SAndi Kleen 			  struct perf_pmu_alias *alias)
1293dc098b35SAndi Kleen {
1294aaea3617SCody P Schafer 	struct parse_events_term *term;
1295aaea3617SCody P Schafer 	int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
1296aaea3617SCody P Schafer 
1297aaea3617SCody P Schafer 	list_for_each_entry(term, &alias->terms, list) {
1298aaea3617SCody P Schafer 		if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1299aaea3617SCody P Schafer 			used += snprintf(buf + used, sub_non_neg(len, used),
1300aaea3617SCody P Schafer 					",%s=%s", term->config,
1301aaea3617SCody P Schafer 					term->val.str);
1302aaea3617SCody P Schafer 	}
1303aaea3617SCody P Schafer 
1304aaea3617SCody P Schafer 	if (sub_non_neg(len, used) > 0) {
1305aaea3617SCody P Schafer 		buf[used] = '/';
1306aaea3617SCody P Schafer 		used++;
1307aaea3617SCody P Schafer 	}
1308aaea3617SCody P Schafer 	if (sub_non_neg(len, used) > 0) {
1309aaea3617SCody P Schafer 		buf[used] = '\0';
1310aaea3617SCody P Schafer 		used++;
1311aaea3617SCody P Schafer 	} else
1312aaea3617SCody P Schafer 		buf[len - 1] = '\0';
1313aaea3617SCody P Schafer 
1314dc098b35SAndi Kleen 	return buf;
1315dc098b35SAndi Kleen }
1316dc098b35SAndi Kleen 
1317dc098b35SAndi Kleen static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu,
1318dc098b35SAndi Kleen 			     struct perf_pmu_alias *alias)
1319dc098b35SAndi Kleen {
1320dc098b35SAndi Kleen 	snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name);
1321dc098b35SAndi Kleen 	return buf;
1322dc098b35SAndi Kleen }
1323dc098b35SAndi Kleen 
1324dd5f1036SAndi Kleen struct sevent {
132508e60ed1SAndi Kleen 	char *name;
132608e60ed1SAndi Kleen 	char *desc;
1327dd5f1036SAndi Kleen 	char *topic;
1328f2361024SAndi Kleen 	char *str;
1329f2361024SAndi Kleen 	char *pmu;
13307f372a63SAndi Kleen 	char *metric_expr;
133196284814SAndi Kleen 	char *metric_name;
133208e60ed1SAndi Kleen };
133308e60ed1SAndi Kleen 
1334dd5f1036SAndi Kleen static int cmp_sevent(const void *a, const void *b)
1335dc098b35SAndi Kleen {
1336dd5f1036SAndi Kleen 	const struct sevent *as = a;
1337dd5f1036SAndi Kleen 	const struct sevent *bs = b;
133808e60ed1SAndi Kleen 
133908e60ed1SAndi Kleen 	/* Put extra events last */
134008e60ed1SAndi Kleen 	if (!!as->desc != !!bs->desc)
134108e60ed1SAndi Kleen 		return !!as->desc - !!bs->desc;
1342dd5f1036SAndi Kleen 	if (as->topic && bs->topic) {
1343dd5f1036SAndi Kleen 		int n = strcmp(as->topic, bs->topic);
1344dd5f1036SAndi Kleen 
1345dd5f1036SAndi Kleen 		if (n)
1346dd5f1036SAndi Kleen 			return n;
1347dd5f1036SAndi Kleen 	}
134808e60ed1SAndi Kleen 	return strcmp(as->name, bs->name);
134908e60ed1SAndi Kleen }
135008e60ed1SAndi Kleen 
135108e60ed1SAndi Kleen static void wordwrap(char *s, int start, int max, int corr)
135208e60ed1SAndi Kleen {
135308e60ed1SAndi Kleen 	int column = start;
135408e60ed1SAndi Kleen 	int n;
135508e60ed1SAndi Kleen 
135608e60ed1SAndi Kleen 	while (*s) {
135708e60ed1SAndi Kleen 		int wlen = strcspn(s, " \t");
135808e60ed1SAndi Kleen 
135908e60ed1SAndi Kleen 		if (column + wlen >= max && column > start) {
136008e60ed1SAndi Kleen 			printf("\n%*s", start, "");
136108e60ed1SAndi Kleen 			column = start + corr;
136208e60ed1SAndi Kleen 		}
136308e60ed1SAndi Kleen 		n = printf("%s%.*s", column > start ? " " : "", wlen, s);
136408e60ed1SAndi Kleen 		if (n <= 0)
136508e60ed1SAndi Kleen 			break;
136608e60ed1SAndi Kleen 		s += wlen;
136708e60ed1SAndi Kleen 		column += n;
1368aa4beb10STaeung Song 		s = ltrim(s);
136908e60ed1SAndi Kleen 	}
1370dc098b35SAndi Kleen }
1371dc098b35SAndi Kleen 
1372c8d6828aSSukadev Bhattiprolu void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag,
1373bf874fcfSAndi Kleen 			bool long_desc, bool details_flag)
1374dc098b35SAndi Kleen {
1375dc098b35SAndi Kleen 	struct perf_pmu *pmu;
1376dc098b35SAndi Kleen 	struct perf_pmu_alias *alias;
1377dc098b35SAndi Kleen 	char buf[1024];
1378dc098b35SAndi Kleen 	int printed = 0;
1379dc098b35SAndi Kleen 	int len, j;
1380dd5f1036SAndi Kleen 	struct sevent *aliases;
138108e60ed1SAndi Kleen 	int numdesc = 0;
138261eb2eb4SAndi Kleen 	int columns = pager_get_columns();
1383dd5f1036SAndi Kleen 	char *topic = NULL;
1384dc098b35SAndi Kleen 
1385dc098b35SAndi Kleen 	pmu = NULL;
1386dc098b35SAndi Kleen 	len = 0;
138742634bc7SAdrian Hunter 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1388dc098b35SAndi Kleen 		list_for_each_entry(alias, &pmu->aliases, list)
1389dc098b35SAndi Kleen 			len++;
139042634bc7SAdrian Hunter 		if (pmu->selectable)
139142634bc7SAdrian Hunter 			len++;
139242634bc7SAdrian Hunter 	}
1393dd5f1036SAndi Kleen 	aliases = zalloc(sizeof(struct sevent) * len);
1394dc098b35SAndi Kleen 	if (!aliases)
13957e4772dcSArnaldo Carvalho de Melo 		goto out_enomem;
1396dc098b35SAndi Kleen 	pmu = NULL;
1397dc098b35SAndi Kleen 	j = 0;
139842634bc7SAdrian Hunter 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1399dc098b35SAndi Kleen 		list_for_each_entry(alias, &pmu->aliases, list) {
140008e60ed1SAndi Kleen 			char *name = alias->desc ? alias->name :
140108e60ed1SAndi Kleen 				format_alias(buf, sizeof(buf), pmu, alias);
1402dc098b35SAndi Kleen 			bool is_cpu = !strcmp(pmu->name, "cpu");
1403dc098b35SAndi Kleen 
1404dc098b35SAndi Kleen 			if (event_glob != NULL &&
140538d14f0cSAndi Kleen 			    !(strglobmatch_nocase(name, event_glob) ||
140638d14f0cSAndi Kleen 			      (!is_cpu && strglobmatch_nocase(alias->name,
140767bdc35fSAndi Kleen 						       event_glob)) ||
140867bdc35fSAndi Kleen 			      (alias->topic &&
140967bdc35fSAndi Kleen 			       strglobmatch_nocase(alias->topic, event_glob))))
1410dc098b35SAndi Kleen 				continue;
14117e4772dcSArnaldo Carvalho de Melo 
141208e60ed1SAndi Kleen 			if (is_cpu && !name_only && !alias->desc)
14137e4772dcSArnaldo Carvalho de Melo 				name = format_alias_or(buf, sizeof(buf), pmu, alias);
14147e4772dcSArnaldo Carvalho de Melo 
141508e60ed1SAndi Kleen 			aliases[j].name = name;
141608e60ed1SAndi Kleen 			if (is_cpu && !name_only && !alias->desc)
141708e60ed1SAndi Kleen 				aliases[j].name = format_alias_or(buf,
141808e60ed1SAndi Kleen 								  sizeof(buf),
141908e60ed1SAndi Kleen 								  pmu, alias);
142008e60ed1SAndi Kleen 			aliases[j].name = strdup(aliases[j].name);
142108e60ed1SAndi Kleen 			if (!aliases[j].name)
14227e4772dcSArnaldo Carvalho de Melo 				goto out_enomem;
142308e60ed1SAndi Kleen 
1424c8d6828aSSukadev Bhattiprolu 			aliases[j].desc = long_desc ? alias->long_desc :
1425c8d6828aSSukadev Bhattiprolu 						alias->desc;
1426dd5f1036SAndi Kleen 			aliases[j].topic = alias->topic;
1427f2361024SAndi Kleen 			aliases[j].str = alias->str;
1428f2361024SAndi Kleen 			aliases[j].pmu = pmu->name;
14297f372a63SAndi Kleen 			aliases[j].metric_expr = alias->metric_expr;
143096284814SAndi Kleen 			aliases[j].metric_name = alias->metric_name;
1431dc098b35SAndi Kleen 			j++;
1432dc098b35SAndi Kleen 		}
1433fa52ceabSArnaldo Carvalho de Melo 		if (pmu->selectable &&
1434fa52ceabSArnaldo Carvalho de Melo 		    (event_glob == NULL || strglobmatch(pmu->name, event_glob))) {
14357e4772dcSArnaldo Carvalho de Melo 			char *s;
14367e4772dcSArnaldo Carvalho de Melo 			if (asprintf(&s, "%s//", pmu->name) < 0)
14377e4772dcSArnaldo Carvalho de Melo 				goto out_enomem;
143808e60ed1SAndi Kleen 			aliases[j].name = s;
143942634bc7SAdrian Hunter 			j++;
144042634bc7SAdrian Hunter 		}
144142634bc7SAdrian Hunter 	}
1442dc098b35SAndi Kleen 	len = j;
1443dd5f1036SAndi Kleen 	qsort(aliases, len, sizeof(struct sevent), cmp_sevent);
1444dc098b35SAndi Kleen 	for (j = 0; j < len; j++) {
144515b22ed3SAndi Kleen 		/* Skip duplicates */
144615b22ed3SAndi Kleen 		if (j > 0 && !strcmp(aliases[j].name, aliases[j - 1].name))
144715b22ed3SAndi Kleen 			continue;
1448dc098b35SAndi Kleen 		if (name_only) {
144908e60ed1SAndi Kleen 			printf("%s ", aliases[j].name);
1450dc098b35SAndi Kleen 			continue;
1451dc098b35SAndi Kleen 		}
14521c5f01feSAndi Kleen 		if (aliases[j].desc && !quiet_flag) {
145308e60ed1SAndi Kleen 			if (numdesc++ == 0)
145408e60ed1SAndi Kleen 				printf("\n");
1455dd5f1036SAndi Kleen 			if (aliases[j].topic && (!topic ||
1456dd5f1036SAndi Kleen 					strcmp(topic, aliases[j].topic))) {
1457dd5f1036SAndi Kleen 				printf("%s%s:\n", topic ? "\n" : "",
1458dd5f1036SAndi Kleen 						aliases[j].topic);
1459dd5f1036SAndi Kleen 				topic = aliases[j].topic;
1460dd5f1036SAndi Kleen 			}
146108e60ed1SAndi Kleen 			printf("  %-50s\n", aliases[j].name);
146208e60ed1SAndi Kleen 			printf("%*s", 8, "[");
146308e60ed1SAndi Kleen 			wordwrap(aliases[j].desc, 8, columns, 0);
146408e60ed1SAndi Kleen 			printf("]\n");
1465bf874fcfSAndi Kleen 			if (details_flag) {
14667f372a63SAndi Kleen 				printf("%*s%s/%s/ ", 8, "", aliases[j].pmu, aliases[j].str);
146796284814SAndi Kleen 				if (aliases[j].metric_name)
146896284814SAndi Kleen 					printf(" MetricName: %s", aliases[j].metric_name);
14697f372a63SAndi Kleen 				if (aliases[j].metric_expr)
14707f372a63SAndi Kleen 					printf(" MetricExpr: %s", aliases[j].metric_expr);
14717f372a63SAndi Kleen 				putchar('\n');
14727f372a63SAndi Kleen 			}
147308e60ed1SAndi Kleen 		} else
147408e60ed1SAndi Kleen 			printf("  %-50s [Kernel PMU event]\n", aliases[j].name);
1475dc098b35SAndi Kleen 		printed++;
1476dc098b35SAndi Kleen 	}
1477dfc431cbSArnaldo Carvalho de Melo 	if (printed && pager_in_use())
1478dc098b35SAndi Kleen 		printf("\n");
14797e4772dcSArnaldo Carvalho de Melo out_free:
14807e4772dcSArnaldo Carvalho de Melo 	for (j = 0; j < len; j++)
148108e60ed1SAndi Kleen 		zfree(&aliases[j].name);
14827e4772dcSArnaldo Carvalho de Melo 	zfree(&aliases);
14837e4772dcSArnaldo Carvalho de Melo 	return;
14847e4772dcSArnaldo Carvalho de Melo 
14857e4772dcSArnaldo Carvalho de Melo out_enomem:
14867e4772dcSArnaldo Carvalho de Melo 	printf("FATAL: not enough memory to print PMU events\n");
14877e4772dcSArnaldo Carvalho de Melo 	if (aliases)
14887e4772dcSArnaldo Carvalho de Melo 		goto out_free;
1489dc098b35SAndi Kleen }
14904cabc3d1SAndi Kleen 
14914cabc3d1SAndi Kleen bool pmu_have_event(const char *pname, const char *name)
14924cabc3d1SAndi Kleen {
14934cabc3d1SAndi Kleen 	struct perf_pmu *pmu;
14944cabc3d1SAndi Kleen 	struct perf_pmu_alias *alias;
14954cabc3d1SAndi Kleen 
14964cabc3d1SAndi Kleen 	pmu = NULL;
14974cabc3d1SAndi Kleen 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
14984cabc3d1SAndi Kleen 		if (strcmp(pname, pmu->name))
14994cabc3d1SAndi Kleen 			continue;
15004cabc3d1SAndi Kleen 		list_for_each_entry(alias, &pmu->aliases, list)
15014cabc3d1SAndi Kleen 			if (!strcmp(alias->name, name))
15024cabc3d1SAndi Kleen 				return true;
15034cabc3d1SAndi Kleen 	}
15044cabc3d1SAndi Kleen 	return false;
15054cabc3d1SAndi Kleen }
15067d4bdab5SAdrian Hunter 
15077d4bdab5SAdrian Hunter static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
15087d4bdab5SAdrian Hunter {
15097d4bdab5SAdrian Hunter 	struct stat st;
15107d4bdab5SAdrian Hunter 	char path[PATH_MAX];
15117d4bdab5SAdrian Hunter 	const char *sysfs;
15127d4bdab5SAdrian Hunter 
15137d4bdab5SAdrian Hunter 	sysfs = sysfs__mountpoint();
15147d4bdab5SAdrian Hunter 	if (!sysfs)
15157d4bdab5SAdrian Hunter 		return NULL;
15167d4bdab5SAdrian Hunter 
15177d4bdab5SAdrian Hunter 	snprintf(path, PATH_MAX,
15187d4bdab5SAdrian Hunter 		 "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name);
15197d4bdab5SAdrian Hunter 
15207d4bdab5SAdrian Hunter 	if (stat(path, &st) < 0)
15217d4bdab5SAdrian Hunter 		return NULL;
15227d4bdab5SAdrian Hunter 
15237d4bdab5SAdrian Hunter 	return fopen(path, "r");
15247d4bdab5SAdrian Hunter }
15257d4bdab5SAdrian Hunter 
15267d4bdab5SAdrian Hunter int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
15277d4bdab5SAdrian Hunter 			...)
15287d4bdab5SAdrian Hunter {
15297d4bdab5SAdrian Hunter 	va_list args;
15307d4bdab5SAdrian Hunter 	FILE *file;
15317d4bdab5SAdrian Hunter 	int ret = EOF;
15327d4bdab5SAdrian Hunter 
15337d4bdab5SAdrian Hunter 	va_start(args, fmt);
15347d4bdab5SAdrian Hunter 	file = perf_pmu__open_file(pmu, name);
15357d4bdab5SAdrian Hunter 	if (file) {
15367d4bdab5SAdrian Hunter 		ret = vfscanf(file, fmt, args);
15377d4bdab5SAdrian Hunter 		fclose(file);
15387d4bdab5SAdrian Hunter 	}
15397d4bdab5SAdrian Hunter 	va_end(args);
15407d4bdab5SAdrian Hunter 	return ret;
15417d4bdab5SAdrian Hunter }
1542