xref: /linux/tools/perf/util/pmu.c (revision 9c3516d1b850ea938b074df33e4c86d721c77720)
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>
18*9c3516d1SJiri Olsa #include <perf/cpumap.h>
19cd82a32eSJiri Olsa #include "pmu.h"
20cd82a32eSJiri Olsa #include "parse-events.h"
217ae92e74SYan, Zheng #include "cpumap.h"
22933f82ffSSukadev Bhattiprolu #include "header.h"
23933f82ffSSukadev Bhattiprolu #include "pmu-events/pmu-events.h"
2461eb2eb4SAndi Kleen #include "cache.h"
25a067558eSArnaldo Carvalho de Melo #include "string2.h"
26cd82a32eSJiri Olsa 
27ab1bf653SArnaldo Carvalho de Melo struct perf_pmu_format {
28ab1bf653SArnaldo Carvalho de Melo 	char *name;
29ab1bf653SArnaldo Carvalho de Melo 	int value;
30ab1bf653SArnaldo Carvalho de Melo 	DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
31ab1bf653SArnaldo Carvalho de Melo 	struct list_head list;
32ab1bf653SArnaldo Carvalho de Melo };
33ab1bf653SArnaldo Carvalho de Melo 
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 
14811a64a05SBen Hutchings 	scnprintf(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 
17811a64a05SBen Hutchings 	scnprintf(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 
20811a64a05SBen Hutchings 	scnprintf(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 
22611a64a05SBen Hutchings 	scnprintf(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 */
39913c230abSArnaldo Carvalho de Melo 	strim(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 
576f854839bSJiri Olsa static struct perf_cpu_map *__pmu_cpumask(const char *path)
57766ec1191SMark Rutland {
57866ec1191SMark Rutland 	FILE *file;
579f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
58066ec1191SMark Rutland 
58166ec1191SMark Rutland 	file = fopen(path, "r");
58266ec1191SMark Rutland 	if (!file)
58366ec1191SMark Rutland 		return NULL;
58466ec1191SMark Rutland 
585*9c3516d1SJiri Olsa 	cpus = perf_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 
597f854839bSJiri Olsa static struct perf_cpu_map *pmu_cpumask(const char *name)
5987ae92e74SYan, Zheng {
5997ae92e74SYan, Zheng 	char path[PATH_MAX];
600f854839bSJiri Olsa 	struct perf_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];
625f854839bSJiri Olsa 	struct perf_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);
63038f01d8dSJiri Olsa 	perf_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 
65514b22ae0SGanapatrao Kulkarni 	return 0;
65614b22ae0SGanapatrao Kulkarni }
65714b22ae0SGanapatrao Kulkarni 
65854e32dc0SGanapatrao Kulkarni static char *perf_pmu__getcpuid(struct perf_pmu *pmu)
659d77ade9fSAndi Kleen {
660d77ade9fSAndi Kleen 	char *cpuid;
661d77ade9fSAndi Kleen 	static bool printed;
662d77ade9fSAndi Kleen 
663d77ade9fSAndi Kleen 	cpuid = getenv("PERF_CPUID");
664d77ade9fSAndi Kleen 	if (cpuid)
665d77ade9fSAndi Kleen 		cpuid = strdup(cpuid);
666d77ade9fSAndi Kleen 	if (!cpuid)
66754e32dc0SGanapatrao Kulkarni 		cpuid = get_cpuid_str(pmu);
668d77ade9fSAndi Kleen 	if (!cpuid)
669d77ade9fSAndi Kleen 		return NULL;
670d77ade9fSAndi Kleen 
671d77ade9fSAndi Kleen 	if (!printed) {
672d77ade9fSAndi Kleen 		pr_debug("Using CPUID %s\n", cpuid);
673d77ade9fSAndi Kleen 		printed = true;
674d77ade9fSAndi Kleen 	}
675d77ade9fSAndi Kleen 	return cpuid;
676d77ade9fSAndi Kleen }
677d77ade9fSAndi Kleen 
67854e32dc0SGanapatrao Kulkarni struct pmu_events_map *perf_pmu__find_map(struct perf_pmu *pmu)
679d77ade9fSAndi Kleen {
680d77ade9fSAndi Kleen 	struct pmu_events_map *map;
68154e32dc0SGanapatrao Kulkarni 	char *cpuid = perf_pmu__getcpuid(pmu);
682d77ade9fSAndi Kleen 	int i;
683d77ade9fSAndi Kleen 
684de3d0f12SGanapatrao Kulkarni 	/* on some platforms which uses cpus map, cpuid can be NULL for
685de3d0f12SGanapatrao Kulkarni 	 * PMUs other than CORE PMUs.
686de3d0f12SGanapatrao Kulkarni 	 */
687de3d0f12SGanapatrao Kulkarni 	if (!cpuid)
688de3d0f12SGanapatrao Kulkarni 		return NULL;
689de3d0f12SGanapatrao Kulkarni 
690d77ade9fSAndi Kleen 	i = 0;
691d77ade9fSAndi Kleen 	for (;;) {
692d77ade9fSAndi Kleen 		map = &pmu_events_map[i++];
693d77ade9fSAndi Kleen 		if (!map->table) {
694d77ade9fSAndi Kleen 			map = NULL;
695d77ade9fSAndi Kleen 			break;
696d77ade9fSAndi Kleen 		}
697d77ade9fSAndi Kleen 
6984cb7d3ecSThomas Richter 		if (!strcmp_cpuid_str(map->cpuid, cpuid))
699d77ade9fSAndi Kleen 			break;
700d77ade9fSAndi Kleen 	}
701d77ade9fSAndi Kleen 	free(cpuid);
702d77ade9fSAndi Kleen 	return map;
703d77ade9fSAndi Kleen }
704d77ade9fSAndi Kleen 
705730670b1SJohn Garry static bool pmu_uncore_alias_match(const char *pmu_name, const char *name)
706730670b1SJohn Garry {
707730670b1SJohn Garry 	char *tmp = NULL, *tok, *str;
708730670b1SJohn Garry 	bool res;
709730670b1SJohn Garry 
710730670b1SJohn Garry 	str = strdup(pmu_name);
711730670b1SJohn Garry 	if (!str)
712730670b1SJohn Garry 		return false;
713730670b1SJohn Garry 
714730670b1SJohn Garry 	/*
715730670b1SJohn Garry 	 * uncore alias may be from different PMU with common prefix
716730670b1SJohn Garry 	 */
717730670b1SJohn Garry 	tok = strtok_r(str, ",", &tmp);
718730670b1SJohn Garry 	if (strncmp(pmu_name, tok, strlen(tok))) {
719730670b1SJohn Garry 		res = false;
720730670b1SJohn Garry 		goto out;
721730670b1SJohn Garry 	}
722730670b1SJohn Garry 
723730670b1SJohn Garry 	/*
724730670b1SJohn Garry 	 * Match more complex aliases where the alias name is a comma-delimited
725730670b1SJohn Garry 	 * list of tokens, orderly contained in the matching PMU name.
726730670b1SJohn Garry 	 *
727730670b1SJohn Garry 	 * Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we
728730670b1SJohn Garry 	 *	    match "socket" in "socketX_pmunameY" and then "pmuname" in
729730670b1SJohn Garry 	 *	    "pmunameY".
730730670b1SJohn Garry 	 */
731730670b1SJohn Garry 	for (; tok; name += strlen(tok), tok = strtok_r(NULL, ",", &tmp)) {
732730670b1SJohn Garry 		name = strstr(name, tok);
733730670b1SJohn Garry 		if (!name) {
734730670b1SJohn Garry 			res = false;
735730670b1SJohn Garry 			goto out;
736730670b1SJohn Garry 		}
737730670b1SJohn Garry 	}
738730670b1SJohn Garry 
739730670b1SJohn Garry 	res = true;
740730670b1SJohn Garry out:
741730670b1SJohn Garry 	free(str);
742730670b1SJohn Garry 	return res;
743730670b1SJohn Garry }
744730670b1SJohn Garry 
745933f82ffSSukadev Bhattiprolu /*
746933f82ffSSukadev Bhattiprolu  * From the pmu_events_map, find the table of PMU events that corresponds
747933f82ffSSukadev Bhattiprolu  * to the current running CPU. Then, add all PMU events from that table
748933f82ffSSukadev Bhattiprolu  * as aliases.
749933f82ffSSukadev Bhattiprolu  */
75054e32dc0SGanapatrao Kulkarni static void pmu_add_cpu_aliases(struct list_head *head, struct perf_pmu *pmu)
751933f82ffSSukadev Bhattiprolu {
752933f82ffSSukadev Bhattiprolu 	int i;
753933f82ffSSukadev Bhattiprolu 	struct pmu_events_map *map;
75454e32dc0SGanapatrao Kulkarni 	const char *name = pmu->name;
755933f82ffSSukadev Bhattiprolu 
75654e32dc0SGanapatrao Kulkarni 	map = perf_pmu__find_map(pmu);
757d77ade9fSAndi Kleen 	if (!map)
758933f82ffSSukadev Bhattiprolu 		return;
759933f82ffSSukadev Bhattiprolu 
760933f82ffSSukadev Bhattiprolu 	/*
761933f82ffSSukadev Bhattiprolu 	 * Found a matching PMU events table. Create aliases
762933f82ffSSukadev Bhattiprolu 	 */
763933f82ffSSukadev Bhattiprolu 	i = 0;
764933f82ffSSukadev Bhattiprolu 	while (1) {
765599ee18fSJohn Garry 		const char *cpu_name = is_arm_pmu_core(name) ? name : "cpu";
766599ee18fSJohn Garry 		struct pmu_event *pe = &map->table[i++];
767599ee18fSJohn Garry 		const char *pname = pe->pmu ? pe->pmu : cpu_name;
768fedb2b51SAndi Kleen 
769b18f3e36SAndi Kleen 		if (!pe->name) {
770b18f3e36SAndi Kleen 			if (pe->metric_group || pe->metric_name)
771b18f3e36SAndi Kleen 				continue;
772933f82ffSSukadev Bhattiprolu 			break;
773b18f3e36SAndi Kleen 		}
774933f82ffSSukadev Bhattiprolu 
775e94d6b7fSKan Liang 		if (pmu_is_uncore(name) &&
776730670b1SJohn Garry 		    pmu_uncore_alias_match(pname, name))
777e94d6b7fSKan Liang 			goto new_alias;
778e94d6b7fSKan Liang 
779ea1fa48cSThomas Richter 		if (strcmp(pname, name))
78014b22ae0SGanapatrao Kulkarni 			continue;
781fedb2b51SAndi Kleen 
782e94d6b7fSKan Liang new_alias:
783933f82ffSSukadev Bhattiprolu 		/* need type casts to override 'const' */
784933f82ffSSukadev Bhattiprolu 		__perf_pmu__new_alias(head, NULL, (char *)pe->name,
785c8d6828aSSukadev Bhattiprolu 				(char *)pe->desc, (char *)pe->event,
786fedb2b51SAndi Kleen 				(char *)pe->long_desc, (char *)pe->topic,
78700636c3bSAndi Kleen 				(char *)pe->unit, (char *)pe->perpkg,
78896284814SAndi Kleen 				(char *)pe->metric_expr,
78996284814SAndi Kleen 				(char *)pe->metric_name);
790933f82ffSSukadev Bhattiprolu 	}
791933f82ffSSukadev Bhattiprolu }
792933f82ffSSukadev Bhattiprolu 
793c5de47f2SSukadev Bhattiprolu struct perf_event_attr * __weak
794dc0a6202SAdrian Hunter perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
795dc0a6202SAdrian Hunter {
796dc0a6202SAdrian Hunter 	return NULL;
797dc0a6202SAdrian Hunter }
798dc0a6202SAdrian Hunter 
79990a86bdeSJiri Olsa static int pmu_max_precise(const char *name)
80090a86bdeSJiri Olsa {
80190a86bdeSJiri Olsa 	char path[PATH_MAX];
80290a86bdeSJiri Olsa 	int max_precise = -1;
80390a86bdeSJiri Olsa 
80490a86bdeSJiri Olsa 	scnprintf(path, PATH_MAX,
80590a86bdeSJiri Olsa 		 "bus/event_source/devices/%s/caps/max_precise",
80690a86bdeSJiri Olsa 		 name);
80790a86bdeSJiri Olsa 
80890a86bdeSJiri Olsa 	sysfs__read_int(path, &max_precise);
80990a86bdeSJiri Olsa 	return max_precise;
81090a86bdeSJiri Olsa }
81190a86bdeSJiri Olsa 
812b6b96fb4SAdrian Hunter static struct perf_pmu *pmu_lookup(const char *name)
813cd82a32eSJiri Olsa {
814cd82a32eSJiri Olsa 	struct perf_pmu *pmu;
815cd82a32eSJiri Olsa 	LIST_HEAD(format);
816a6146d50SZheng Yan 	LIST_HEAD(aliases);
817cd82a32eSJiri Olsa 	__u32 type;
818cd82a32eSJiri Olsa 
819cd82a32eSJiri Olsa 	/*
820cd82a32eSJiri Olsa 	 * The pmu data we store & need consists of the pmu
821cd82a32eSJiri Olsa 	 * type value and format definitions. Load both right
822cd82a32eSJiri Olsa 	 * now.
823cd82a32eSJiri Olsa 	 */
824cd82a32eSJiri Olsa 	if (pmu_format(name, &format))
825cd82a32eSJiri Olsa 		return NULL;
826cd82a32eSJiri Olsa 
82715b22ed3SAndi Kleen 	/*
82815b22ed3SAndi Kleen 	 * Check the type first to avoid unnecessary work.
82915b22ed3SAndi Kleen 	 */
83015b22ed3SAndi Kleen 	if (pmu_type(name, &type))
83115b22ed3SAndi Kleen 		return NULL;
83215b22ed3SAndi Kleen 
8333fded963SJiri Olsa 	if (pmu_aliases(name, &aliases))
8343fded963SJiri Olsa 		return NULL;
8353fded963SJiri Olsa 
836cd82a32eSJiri Olsa 	pmu = zalloc(sizeof(*pmu));
837cd82a32eSJiri Olsa 	if (!pmu)
838cd82a32eSJiri Olsa 		return NULL;
839cd82a32eSJiri Olsa 
8407ae92e74SYan, Zheng 	pmu->cpus = pmu_cpumask(name);
84154e32dc0SGanapatrao Kulkarni 	pmu->name = strdup(name);
84254e32dc0SGanapatrao Kulkarni 	pmu->type = type;
84366ec1191SMark Rutland 	pmu->is_uncore = pmu_is_uncore(name);
84490a86bdeSJiri Olsa 	pmu->max_precise = pmu_max_precise(name);
84554e32dc0SGanapatrao Kulkarni 	pmu_add_cpu_aliases(&aliases, pmu);
84666ec1191SMark Rutland 
847cd82a32eSJiri Olsa 	INIT_LIST_HEAD(&pmu->format);
848a6146d50SZheng Yan 	INIT_LIST_HEAD(&pmu->aliases);
849cd82a32eSJiri Olsa 	list_splice(&format, &pmu->format);
850a6146d50SZheng Yan 	list_splice(&aliases, &pmu->aliases);
8519bc8f9feSRobert Richter 	list_add_tail(&pmu->list, &pmus);
852dc0a6202SAdrian Hunter 
853dc0a6202SAdrian Hunter 	pmu->default_config = perf_pmu__get_default_config(pmu);
854dc0a6202SAdrian Hunter 
855cd82a32eSJiri Olsa 	return pmu;
856cd82a32eSJiri Olsa }
857cd82a32eSJiri Olsa 
858b6b96fb4SAdrian Hunter static struct perf_pmu *pmu_find(const char *name)
859cd82a32eSJiri Olsa {
860cd82a32eSJiri Olsa 	struct perf_pmu *pmu;
861cd82a32eSJiri Olsa 
862cd82a32eSJiri Olsa 	list_for_each_entry(pmu, &pmus, list)
863cd82a32eSJiri Olsa 		if (!strcmp(pmu->name, name))
864cd82a32eSJiri Olsa 			return pmu;
865cd82a32eSJiri Olsa 
866cd82a32eSJiri Olsa 	return NULL;
867cd82a32eSJiri Olsa }
868cd82a32eSJiri Olsa 
86950a9667cSRobert Richter struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
87050a9667cSRobert Richter {
87150a9667cSRobert Richter 	/*
87250a9667cSRobert Richter 	 * pmu iterator: If pmu is NULL, we start at the begin,
87350a9667cSRobert Richter 	 * otherwise return the next pmu. Returns NULL on end.
87450a9667cSRobert Richter 	 */
87550a9667cSRobert Richter 	if (!pmu) {
87650a9667cSRobert Richter 		pmu_read_sysfs();
87750a9667cSRobert Richter 		pmu = list_prepare_entry(pmu, &pmus, list);
87850a9667cSRobert Richter 	}
87950a9667cSRobert Richter 	list_for_each_entry_continue(pmu, &pmus, list)
88050a9667cSRobert Richter 		return pmu;
88150a9667cSRobert Richter 	return NULL;
88250a9667cSRobert Richter }
88350a9667cSRobert Richter 
884b6b96fb4SAdrian Hunter struct perf_pmu *perf_pmu__find(const char *name)
885cd82a32eSJiri Olsa {
886cd82a32eSJiri Olsa 	struct perf_pmu *pmu;
887cd82a32eSJiri Olsa 
888cd82a32eSJiri Olsa 	/*
889cd82a32eSJiri Olsa 	 * Once PMU is loaded it stays in the list,
890cd82a32eSJiri Olsa 	 * so we keep us from multiple reading/parsing
891cd82a32eSJiri Olsa 	 * the pmu format definitions.
892cd82a32eSJiri Olsa 	 */
893cd82a32eSJiri Olsa 	pmu = pmu_find(name);
894cd82a32eSJiri Olsa 	if (pmu)
895cd82a32eSJiri Olsa 		return pmu;
896cd82a32eSJiri Olsa 
897cd82a32eSJiri Olsa 	return pmu_lookup(name);
898cd82a32eSJiri Olsa }
899cd82a32eSJiri Olsa 
9005c6ccc37SArnaldo Carvalho de Melo static struct perf_pmu_format *
90109ff6071SAdrian Hunter pmu_find_format(struct list_head *formats, const char *name)
902cd82a32eSJiri Olsa {
9035c6ccc37SArnaldo Carvalho de Melo 	struct perf_pmu_format *format;
904cd82a32eSJiri Olsa 
905cd82a32eSJiri Olsa 	list_for_each_entry(format, formats, list)
906cd82a32eSJiri Olsa 		if (!strcmp(format->name, name))
907cd82a32eSJiri Olsa 			return format;
908cd82a32eSJiri Olsa 
909cd82a32eSJiri Olsa 	return NULL;
910cd82a32eSJiri Olsa }
911cd82a32eSJiri Olsa 
91209ff6071SAdrian Hunter __u64 perf_pmu__format_bits(struct list_head *formats, const char *name)
91309ff6071SAdrian Hunter {
91409ff6071SAdrian Hunter 	struct perf_pmu_format *format = pmu_find_format(formats, name);
91509ff6071SAdrian Hunter 	__u64 bits = 0;
91609ff6071SAdrian Hunter 	int fbit;
91709ff6071SAdrian Hunter 
91809ff6071SAdrian Hunter 	if (!format)
91909ff6071SAdrian Hunter 		return 0;
92009ff6071SAdrian Hunter 
92109ff6071SAdrian Hunter 	for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
92209ff6071SAdrian Hunter 		bits |= 1ULL << fbit;
92309ff6071SAdrian Hunter 
92409ff6071SAdrian Hunter 	return bits;
92509ff6071SAdrian Hunter }
92609ff6071SAdrian Hunter 
927cd82a32eSJiri Olsa /*
928dc0a6202SAdrian Hunter  * Sets value based on the format definition (format parameter)
929cd82a32eSJiri Olsa  * and unformated value (value parameter).
930cd82a32eSJiri Olsa  */
931dc0a6202SAdrian Hunter static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
932dc0a6202SAdrian Hunter 			     bool zero)
933cd82a32eSJiri Olsa {
934cd82a32eSJiri Olsa 	unsigned long fbit, vbit;
935cd82a32eSJiri Olsa 
936cd82a32eSJiri Olsa 	for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
937cd82a32eSJiri Olsa 
938cd82a32eSJiri Olsa 		if (!test_bit(fbit, format))
939cd82a32eSJiri Olsa 			continue;
940cd82a32eSJiri Olsa 
941dc0a6202SAdrian Hunter 		if (value & (1llu << vbit++))
942dc0a6202SAdrian Hunter 			*v |= (1llu << fbit);
943dc0a6202SAdrian Hunter 		else if (zero)
944dc0a6202SAdrian Hunter 			*v &= ~(1llu << fbit);
945cd82a32eSJiri Olsa 	}
946cd82a32eSJiri Olsa }
947cd82a32eSJiri Olsa 
9480efe6b67SAdrian Hunter static __u64 pmu_format_max_value(const unsigned long *format)
9490efe6b67SAdrian Hunter {
9501b9caa10SJiri Olsa 	int w;
9510efe6b67SAdrian Hunter 
9521b9caa10SJiri Olsa 	w = bitmap_weight(format, PERF_PMU_FORMAT_BITS);
9531b9caa10SJiri Olsa 	if (!w)
9541b9caa10SJiri Olsa 		return 0;
9551b9caa10SJiri Olsa 	if (w < 64)
9561b9caa10SJiri Olsa 		return (1ULL << w) - 1;
9571b9caa10SJiri Olsa 	return -1;
9580efe6b67SAdrian Hunter }
9590efe6b67SAdrian Hunter 
960cd82a32eSJiri Olsa /*
961688d4dfcSCody P Schafer  * Term is a string term, and might be a param-term. Try to look up it's value
962688d4dfcSCody P Schafer  * in the remaining terms.
963688d4dfcSCody P Schafer  * - We have a term like "base-or-format-term=param-term",
964688d4dfcSCody P Schafer  * - We need to find the value supplied for "param-term" (with param-term named
965688d4dfcSCody P Schafer  *   in a config string) later on in the term list.
966688d4dfcSCody P Schafer  */
967688d4dfcSCody P Schafer static int pmu_resolve_param_term(struct parse_events_term *term,
968688d4dfcSCody P Schafer 				  struct list_head *head_terms,
969688d4dfcSCody P Schafer 				  __u64 *value)
970688d4dfcSCody P Schafer {
971688d4dfcSCody P Schafer 	struct parse_events_term *t;
972688d4dfcSCody P Schafer 
973688d4dfcSCody P Schafer 	list_for_each_entry(t, head_terms, list) {
974688d4dfcSCody P Schafer 		if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
975688d4dfcSCody P Schafer 			if (!strcmp(t->config, term->config)) {
976688d4dfcSCody P Schafer 				t->used = true;
977688d4dfcSCody P Schafer 				*value = t->val.num;
978688d4dfcSCody P Schafer 				return 0;
979688d4dfcSCody P Schafer 			}
980688d4dfcSCody P Schafer 		}
981688d4dfcSCody P Schafer 	}
982688d4dfcSCody P Schafer 
983bb963e16SNamhyung Kim 	if (verbose > 0)
984688d4dfcSCody P Schafer 		printf("Required parameter '%s' not specified\n", term->config);
985688d4dfcSCody P Schafer 
986688d4dfcSCody P Schafer 	return -1;
987688d4dfcSCody P Schafer }
988688d4dfcSCody P Schafer 
989ffeb883eSHe Kuang static char *pmu_formats_string(struct list_head *formats)
990e64b020bSJiri Olsa {
991e64b020bSJiri Olsa 	struct perf_pmu_format *format;
99211db4e29SMasami Hiramatsu 	char *str = NULL;
99311db4e29SMasami Hiramatsu 	struct strbuf buf = STRBUF_INIT;
994e64b020bSJiri Olsa 	unsigned i = 0;
995e64b020bSJiri Olsa 
996ffeb883eSHe Kuang 	if (!formats)
997e64b020bSJiri Olsa 		return NULL;
998e64b020bSJiri Olsa 
999e64b020bSJiri Olsa 	/* sysfs exported terms */
1000ffeb883eSHe Kuang 	list_for_each_entry(format, formats, list)
100111db4e29SMasami Hiramatsu 		if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
100211db4e29SMasami Hiramatsu 			goto error;
1003e64b020bSJiri Olsa 
1004ffeb883eSHe Kuang 	str = strbuf_detach(&buf, NULL);
100511db4e29SMasami Hiramatsu error:
1006ffeb883eSHe Kuang 	strbuf_release(&buf);
1007e64b020bSJiri Olsa 
1008e64b020bSJiri Olsa 	return str;
1009e64b020bSJiri Olsa }
1010e64b020bSJiri Olsa 
1011688d4dfcSCody P Schafer /*
1012cd82a32eSJiri Olsa  * Setup one of config[12] attr members based on the
101388aca8d9SCody P Schafer  * user input data - term parameter.
1014cd82a32eSJiri Olsa  */
1015cd82a32eSJiri Olsa static int pmu_config_term(struct list_head *formats,
1016cd82a32eSJiri Olsa 			   struct perf_event_attr *attr,
1017dc0a6202SAdrian Hunter 			   struct parse_events_term *term,
1018688d4dfcSCody P Schafer 			   struct list_head *head_terms,
1019e64b020bSJiri Olsa 			   bool zero, struct parse_events_error *err)
1020cd82a32eSJiri Olsa {
10215c6ccc37SArnaldo Carvalho de Melo 	struct perf_pmu_format *format;
1022cd82a32eSJiri Olsa 	__u64 *vp;
10230efe6b67SAdrian Hunter 	__u64 val, max_val;
1024cd82a32eSJiri Olsa 
1025cd82a32eSJiri Olsa 	/*
1026688d4dfcSCody P Schafer 	 * If this is a parameter we've already used for parameterized-eval,
1027688d4dfcSCody P Schafer 	 * skip it in normal eval.
1028688d4dfcSCody P Schafer 	 */
1029688d4dfcSCody P Schafer 	if (term->used)
1030688d4dfcSCody P Schafer 		return 0;
1031688d4dfcSCody P Schafer 
1032688d4dfcSCody P Schafer 	/*
1033cd82a32eSJiri Olsa 	 * Hardcoded terms should be already in, so nothing
1034cd82a32eSJiri Olsa 	 * to be done for them.
1035cd82a32eSJiri Olsa 	 */
1036cd82a32eSJiri Olsa 	if (parse_events__is_hardcoded_term(term))
1037cd82a32eSJiri Olsa 		return 0;
1038cd82a32eSJiri Olsa 
1039cd82a32eSJiri Olsa 	format = pmu_find_format(formats, term->config);
1040688d4dfcSCody P Schafer 	if (!format) {
1041bb963e16SNamhyung Kim 		if (verbose > 0)
1042688d4dfcSCody P Schafer 			printf("Invalid event/parameter '%s'\n", term->config);
1043e64b020bSJiri Olsa 		if (err) {
1044ffeb883eSHe Kuang 			char *pmu_term = pmu_formats_string(formats);
1045ffeb883eSHe Kuang 
1046e64b020bSJiri Olsa 			err->idx  = term->err_term;
1047e64b020bSJiri Olsa 			err->str  = strdup("unknown term");
1048ffeb883eSHe Kuang 			err->help = parse_events_formats_error_string(pmu_term);
1049ffeb883eSHe Kuang 			free(pmu_term);
1050e64b020bSJiri Olsa 		}
1051cd82a32eSJiri Olsa 		return -EINVAL;
1052688d4dfcSCody P Schafer 	}
1053cd82a32eSJiri Olsa 
1054cd82a32eSJiri Olsa 	switch (format->value) {
1055cd82a32eSJiri Olsa 	case PERF_PMU_FORMAT_VALUE_CONFIG:
1056cd82a32eSJiri Olsa 		vp = &attr->config;
1057cd82a32eSJiri Olsa 		break;
1058cd82a32eSJiri Olsa 	case PERF_PMU_FORMAT_VALUE_CONFIG1:
1059cd82a32eSJiri Olsa 		vp = &attr->config1;
1060cd82a32eSJiri Olsa 		break;
1061cd82a32eSJiri Olsa 	case PERF_PMU_FORMAT_VALUE_CONFIG2:
1062cd82a32eSJiri Olsa 		vp = &attr->config2;
1063cd82a32eSJiri Olsa 		break;
1064cd82a32eSJiri Olsa 	default:
1065cd82a32eSJiri Olsa 		return -EINVAL;
1066cd82a32eSJiri Olsa 	}
1067cd82a32eSJiri Olsa 
106816fa7e82SJiri Olsa 	/*
1069688d4dfcSCody P Schafer 	 * Either directly use a numeric term, or try to translate string terms
1070688d4dfcSCody P Schafer 	 * using event parameters.
107116fa7e82SJiri Olsa 	 */
107299e7138eSJiri Olsa 	if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
107399e7138eSJiri Olsa 		if (term->no_value &&
107499e7138eSJiri Olsa 		    bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
107599e7138eSJiri Olsa 			if (err) {
107699e7138eSJiri Olsa 				err->idx = term->err_val;
107799e7138eSJiri Olsa 				err->str = strdup("no value assigned for term");
107899e7138eSJiri Olsa 			}
107999e7138eSJiri Olsa 			return -EINVAL;
108099e7138eSJiri Olsa 		}
108199e7138eSJiri Olsa 
1082688d4dfcSCody P Schafer 		val = term->val.num;
108399e7138eSJiri Olsa 	} else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1084688d4dfcSCody P Schafer 		if (strcmp(term->val.str, "?")) {
1085bb963e16SNamhyung Kim 			if (verbose > 0) {
1086688d4dfcSCody P Schafer 				pr_info("Invalid sysfs entry %s=%s\n",
1087688d4dfcSCody P Schafer 						term->config, term->val.str);
1088e64b020bSJiri Olsa 			}
1089e64b020bSJiri Olsa 			if (err) {
1090e64b020bSJiri Olsa 				err->idx = term->err_val;
1091e64b020bSJiri Olsa 				err->str = strdup("expected numeric value");
1092e64b020bSJiri Olsa 			}
1093688d4dfcSCody P Schafer 			return -EINVAL;
1094688d4dfcSCody P Schafer 		}
1095688d4dfcSCody P Schafer 
1096688d4dfcSCody P Schafer 		if (pmu_resolve_param_term(term, head_terms, &val))
1097688d4dfcSCody P Schafer 			return -EINVAL;
1098688d4dfcSCody P Schafer 	} else
1099688d4dfcSCody P Schafer 		return -EINVAL;
1100688d4dfcSCody P Schafer 
11010efe6b67SAdrian Hunter 	max_val = pmu_format_max_value(format->bits);
11020efe6b67SAdrian Hunter 	if (val > max_val) {
11030efe6b67SAdrian Hunter 		if (err) {
11040efe6b67SAdrian Hunter 			err->idx = term->err_val;
11050efe6b67SAdrian Hunter 			if (asprintf(&err->str,
11060efe6b67SAdrian Hunter 				     "value too big for format, maximum is %llu",
11070efe6b67SAdrian Hunter 				     (unsigned long long)max_val) < 0)
11080efe6b67SAdrian Hunter 				err->str = strdup("value too big for format");
11090efe6b67SAdrian Hunter 			return -EINVAL;
11100efe6b67SAdrian Hunter 		}
11110efe6b67SAdrian Hunter 		/*
11120efe6b67SAdrian Hunter 		 * Assume we don't care if !err, in which case the value will be
11130efe6b67SAdrian Hunter 		 * silently truncated.
11140efe6b67SAdrian Hunter 		 */
11150efe6b67SAdrian Hunter 	}
11160efe6b67SAdrian Hunter 
1117688d4dfcSCody P Schafer 	pmu_format_value(format->bits, val, vp, zero);
1118cd82a32eSJiri Olsa 	return 0;
1119cd82a32eSJiri Olsa }
1120cd82a32eSJiri Olsa 
1121cff7f956SJiri Olsa int perf_pmu__config_terms(struct list_head *formats,
1122cff7f956SJiri Olsa 			   struct perf_event_attr *attr,
1123dc0a6202SAdrian Hunter 			   struct list_head *head_terms,
1124e64b020bSJiri Olsa 			   bool zero, struct parse_events_error *err)
1125cd82a32eSJiri Olsa {
11266cee6cd3SArnaldo Carvalho de Melo 	struct parse_events_term *term;
1127cd82a32eSJiri Olsa 
1128688d4dfcSCody P Schafer 	list_for_each_entry(term, head_terms, list) {
1129e64b020bSJiri Olsa 		if (pmu_config_term(formats, attr, term, head_terms,
1130e64b020bSJiri Olsa 				    zero, err))
1131cd82a32eSJiri Olsa 			return -EINVAL;
1132688d4dfcSCody P Schafer 	}
1133cd82a32eSJiri Olsa 
1134cd82a32eSJiri Olsa 	return 0;
1135cd82a32eSJiri Olsa }
1136cd82a32eSJiri Olsa 
1137cd82a32eSJiri Olsa /*
1138cd82a32eSJiri Olsa  * Configures event's 'attr' parameter based on the:
1139cd82a32eSJiri Olsa  * 1) users input - specified in terms parameter
1140cd82a32eSJiri Olsa  * 2) pmu format definitions - specified by pmu parameter
1141cd82a32eSJiri Olsa  */
1142cd82a32eSJiri Olsa int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
1143e64b020bSJiri Olsa 		     struct list_head *head_terms,
1144e64b020bSJiri Olsa 		     struct parse_events_error *err)
1145cd82a32eSJiri Olsa {
1146dc0a6202SAdrian Hunter 	bool zero = !!pmu->default_config;
1147dc0a6202SAdrian Hunter 
1148cd82a32eSJiri Olsa 	attr->type = pmu->type;
1149e64b020bSJiri Olsa 	return perf_pmu__config_terms(&pmu->format, attr, head_terms,
1150e64b020bSJiri Olsa 				      zero, err);
1151cd82a32eSJiri Olsa }
1152cd82a32eSJiri Olsa 
11535c6ccc37SArnaldo Carvalho de Melo static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
11546cee6cd3SArnaldo Carvalho de Melo 					     struct parse_events_term *term)
1155a6146d50SZheng Yan {
11565c6ccc37SArnaldo Carvalho de Melo 	struct perf_pmu_alias *alias;
1157a6146d50SZheng Yan 	char *name;
1158a6146d50SZheng Yan 
1159a6146d50SZheng Yan 	if (parse_events__is_hardcoded_term(term))
1160a6146d50SZheng Yan 		return NULL;
1161a6146d50SZheng Yan 
1162a6146d50SZheng Yan 	if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
1163a6146d50SZheng Yan 		if (term->val.num != 1)
1164a6146d50SZheng Yan 			return NULL;
1165a6146d50SZheng Yan 		if (pmu_find_format(&pmu->format, term->config))
1166a6146d50SZheng Yan 			return NULL;
1167a6146d50SZheng Yan 		name = term->config;
1168a6146d50SZheng Yan 	} else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1169a6146d50SZheng Yan 		if (strcasecmp(term->config, "event"))
1170a6146d50SZheng Yan 			return NULL;
1171a6146d50SZheng Yan 		name = term->val.str;
1172a6146d50SZheng Yan 	} else {
1173a6146d50SZheng Yan 		return NULL;
1174a6146d50SZheng Yan 	}
1175a6146d50SZheng Yan 
1176a6146d50SZheng Yan 	list_for_each_entry(alias, &pmu->aliases, list) {
1177a6146d50SZheng Yan 		if (!strcasecmp(alias->name, name))
1178a6146d50SZheng Yan 			return alias;
1179a6146d50SZheng Yan 	}
1180a6146d50SZheng Yan 	return NULL;
1181a6146d50SZheng Yan }
1182a6146d50SZheng Yan 
1183410136f5SStephane Eranian 
11841d9e446bSJiri Olsa static int check_info_data(struct perf_pmu_alias *alias,
11851d9e446bSJiri Olsa 			   struct perf_pmu_info *info)
1186410136f5SStephane Eranian {
1187410136f5SStephane Eranian 	/*
1188410136f5SStephane Eranian 	 * Only one term in event definition can
11891d9e446bSJiri Olsa 	 * define unit, scale and snapshot, fail
11901d9e446bSJiri Olsa 	 * if there's more than one.
1191410136f5SStephane Eranian 	 */
1192b30a7d1fSArnaldo Carvalho de Melo 	if ((info->unit && alias->unit[0]) ||
11931d9e446bSJiri Olsa 	    (info->scale && alias->scale) ||
11941d9e446bSJiri Olsa 	    (info->snapshot && alias->snapshot))
1195410136f5SStephane Eranian 		return -EINVAL;
1196410136f5SStephane Eranian 
1197b30a7d1fSArnaldo Carvalho de Melo 	if (alias->unit[0])
11981d9e446bSJiri Olsa 		info->unit = alias->unit;
1199410136f5SStephane Eranian 
1200410136f5SStephane Eranian 	if (alias->scale)
12011d9e446bSJiri Olsa 		info->scale = alias->scale;
12021d9e446bSJiri Olsa 
12031d9e446bSJiri Olsa 	if (alias->snapshot)
12041d9e446bSJiri Olsa 		info->snapshot = alias->snapshot;
1205410136f5SStephane Eranian 
1206410136f5SStephane Eranian 	return 0;
1207410136f5SStephane Eranian }
1208410136f5SStephane Eranian 
1209a6146d50SZheng Yan /*
1210a6146d50SZheng Yan  * Find alias in the terms list and replace it with the terms
1211a6146d50SZheng Yan  * defined for the alias
1212a6146d50SZheng Yan  */
1213410136f5SStephane Eranian int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
121446441bdcSMatt Fleming 			  struct perf_pmu_info *info)
1215a6146d50SZheng Yan {
12166cee6cd3SArnaldo Carvalho de Melo 	struct parse_events_term *term, *h;
12175c6ccc37SArnaldo Carvalho de Melo 	struct perf_pmu_alias *alias;
1218a6146d50SZheng Yan 	int ret;
1219a6146d50SZheng Yan 
1220044330c1SMatt Fleming 	info->per_pkg = false;
1221044330c1SMatt Fleming 
12228a398897SStephane Eranian 	/*
12238a398897SStephane Eranian 	 * Mark unit and scale as not set
12248a398897SStephane Eranian 	 * (different from default values, see below)
12258a398897SStephane Eranian 	 */
122646441bdcSMatt Fleming 	info->unit     = NULL;
122746441bdcSMatt Fleming 	info->scale    = 0.0;
12281d9e446bSJiri Olsa 	info->snapshot = false;
122937932c18SAndi Kleen 	info->metric_expr = NULL;
123096284814SAndi Kleen 	info->metric_name = NULL;
1231410136f5SStephane Eranian 
1232a6146d50SZheng Yan 	list_for_each_entry_safe(term, h, head_terms, list) {
1233a6146d50SZheng Yan 		alias = pmu_find_alias(pmu, term);
1234a6146d50SZheng Yan 		if (!alias)
1235a6146d50SZheng Yan 			continue;
1236a6146d50SZheng Yan 		ret = pmu_alias_terms(alias, &term->list);
1237a6146d50SZheng Yan 		if (ret)
1238a6146d50SZheng Yan 			return ret;
1239410136f5SStephane Eranian 
12401d9e446bSJiri Olsa 		ret = check_info_data(alias, info);
1241410136f5SStephane Eranian 		if (ret)
1242410136f5SStephane Eranian 			return ret;
1243410136f5SStephane Eranian 
1244044330c1SMatt Fleming 		if (alias->per_pkg)
1245044330c1SMatt Fleming 			info->per_pkg = true;
124637932c18SAndi Kleen 		info->metric_expr = alias->metric_expr;
124796284814SAndi Kleen 		info->metric_name = alias->metric_name;
1248044330c1SMatt Fleming 
1249e56fbc9dSArnaldo Carvalho de Melo 		list_del_init(&term->list);
1250a6146d50SZheng Yan 		free(term);
1251a6146d50SZheng Yan 	}
12528a398897SStephane Eranian 
12538a398897SStephane Eranian 	/*
12548a398897SStephane Eranian 	 * if no unit or scale foundin aliases, then
12558a398897SStephane Eranian 	 * set defaults as for evsel
12568a398897SStephane Eranian 	 * unit cannot left to NULL
12578a398897SStephane Eranian 	 */
125846441bdcSMatt Fleming 	if (info->unit == NULL)
125946441bdcSMatt Fleming 		info->unit   = "";
12608a398897SStephane Eranian 
126146441bdcSMatt Fleming 	if (info->scale == 0.0)
126246441bdcSMatt Fleming 		info->scale  = 1.0;
12638a398897SStephane Eranian 
1264a6146d50SZheng Yan 	return 0;
1265a6146d50SZheng Yan }
1266a6146d50SZheng Yan 
1267cd82a32eSJiri Olsa int perf_pmu__new_format(struct list_head *list, char *name,
1268cd82a32eSJiri Olsa 			 int config, unsigned long *bits)
1269cd82a32eSJiri Olsa {
12705c6ccc37SArnaldo Carvalho de Melo 	struct perf_pmu_format *format;
1271cd82a32eSJiri Olsa 
1272cd82a32eSJiri Olsa 	format = zalloc(sizeof(*format));
1273cd82a32eSJiri Olsa 	if (!format)
1274cd82a32eSJiri Olsa 		return -ENOMEM;
1275cd82a32eSJiri Olsa 
1276cd82a32eSJiri Olsa 	format->name = strdup(name);
1277cd82a32eSJiri Olsa 	format->value = config;
1278cd82a32eSJiri Olsa 	memcpy(format->bits, bits, sizeof(format->bits));
1279cd82a32eSJiri Olsa 
1280cd82a32eSJiri Olsa 	list_add_tail(&format->list, list);
1281cd82a32eSJiri Olsa 	return 0;
1282cd82a32eSJiri Olsa }
1283cd82a32eSJiri Olsa 
1284cd82a32eSJiri Olsa void perf_pmu__set_format(unsigned long *bits, long from, long to)
1285cd82a32eSJiri Olsa {
1286cd82a32eSJiri Olsa 	long b;
1287cd82a32eSJiri Olsa 
1288cd82a32eSJiri Olsa 	if (!to)
1289cd82a32eSJiri Olsa 		to = from;
1290cd82a32eSJiri Olsa 
129115268138SSukadev Bhattiprolu 	memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
1292cd82a32eSJiri Olsa 	for (b = from; b <= to; b++)
1293cd82a32eSJiri Olsa 		set_bit(b, bits);
1294cd82a32eSJiri Olsa }
1295dc098b35SAndi Kleen 
1296aaea3617SCody P Schafer static int sub_non_neg(int a, int b)
1297aaea3617SCody P Schafer {
1298aaea3617SCody P Schafer 	if (b > a)
1299aaea3617SCody P Schafer 		return 0;
1300aaea3617SCody P Schafer 	return a - b;
1301aaea3617SCody P Schafer }
1302aaea3617SCody P Schafer 
1303dc098b35SAndi Kleen static char *format_alias(char *buf, int len, struct perf_pmu *pmu,
1304dc098b35SAndi Kleen 			  struct perf_pmu_alias *alias)
1305dc098b35SAndi Kleen {
1306aaea3617SCody P Schafer 	struct parse_events_term *term;
1307aaea3617SCody P Schafer 	int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
1308aaea3617SCody P Schafer 
1309aaea3617SCody P Schafer 	list_for_each_entry(term, &alias->terms, list) {
1310aaea3617SCody P Schafer 		if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1311aaea3617SCody P Schafer 			used += snprintf(buf + used, sub_non_neg(len, used),
1312aaea3617SCody P Schafer 					",%s=%s", term->config,
1313aaea3617SCody P Schafer 					term->val.str);
1314aaea3617SCody P Schafer 	}
1315aaea3617SCody P Schafer 
1316aaea3617SCody P Schafer 	if (sub_non_neg(len, used) > 0) {
1317aaea3617SCody P Schafer 		buf[used] = '/';
1318aaea3617SCody P Schafer 		used++;
1319aaea3617SCody P Schafer 	}
1320aaea3617SCody P Schafer 	if (sub_non_neg(len, used) > 0) {
1321aaea3617SCody P Schafer 		buf[used] = '\0';
1322aaea3617SCody P Schafer 		used++;
1323aaea3617SCody P Schafer 	} else
1324aaea3617SCody P Schafer 		buf[len - 1] = '\0';
1325aaea3617SCody P Schafer 
1326dc098b35SAndi Kleen 	return buf;
1327dc098b35SAndi Kleen }
1328dc098b35SAndi Kleen 
1329dc098b35SAndi Kleen static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu,
1330dc098b35SAndi Kleen 			     struct perf_pmu_alias *alias)
1331dc098b35SAndi Kleen {
1332dc098b35SAndi Kleen 	snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name);
1333dc098b35SAndi Kleen 	return buf;
1334dc098b35SAndi Kleen }
1335dc098b35SAndi Kleen 
1336dd5f1036SAndi Kleen struct sevent {
133708e60ed1SAndi Kleen 	char *name;
133808e60ed1SAndi Kleen 	char *desc;
1339dd5f1036SAndi Kleen 	char *topic;
1340f2361024SAndi Kleen 	char *str;
1341f2361024SAndi Kleen 	char *pmu;
13427f372a63SAndi Kleen 	char *metric_expr;
134396284814SAndi Kleen 	char *metric_name;
134408e60ed1SAndi Kleen };
134508e60ed1SAndi Kleen 
1346dd5f1036SAndi Kleen static int cmp_sevent(const void *a, const void *b)
1347dc098b35SAndi Kleen {
1348dd5f1036SAndi Kleen 	const struct sevent *as = a;
1349dd5f1036SAndi Kleen 	const struct sevent *bs = b;
135008e60ed1SAndi Kleen 
135108e60ed1SAndi Kleen 	/* Put extra events last */
135208e60ed1SAndi Kleen 	if (!!as->desc != !!bs->desc)
135308e60ed1SAndi Kleen 		return !!as->desc - !!bs->desc;
1354dd5f1036SAndi Kleen 	if (as->topic && bs->topic) {
1355dd5f1036SAndi Kleen 		int n = strcmp(as->topic, bs->topic);
1356dd5f1036SAndi Kleen 
1357dd5f1036SAndi Kleen 		if (n)
1358dd5f1036SAndi Kleen 			return n;
1359dd5f1036SAndi Kleen 	}
136008e60ed1SAndi Kleen 	return strcmp(as->name, bs->name);
136108e60ed1SAndi Kleen }
136208e60ed1SAndi Kleen 
136308e60ed1SAndi Kleen static void wordwrap(char *s, int start, int max, int corr)
136408e60ed1SAndi Kleen {
136508e60ed1SAndi Kleen 	int column = start;
136608e60ed1SAndi Kleen 	int n;
136708e60ed1SAndi Kleen 
136808e60ed1SAndi Kleen 	while (*s) {
136908e60ed1SAndi Kleen 		int wlen = strcspn(s, " \t");
137008e60ed1SAndi Kleen 
137108e60ed1SAndi Kleen 		if (column + wlen >= max && column > start) {
137208e60ed1SAndi Kleen 			printf("\n%*s", start, "");
137308e60ed1SAndi Kleen 			column = start + corr;
137408e60ed1SAndi Kleen 		}
137508e60ed1SAndi Kleen 		n = printf("%s%.*s", column > start ? " " : "", wlen, s);
137608e60ed1SAndi Kleen 		if (n <= 0)
137708e60ed1SAndi Kleen 			break;
137808e60ed1SAndi Kleen 		s += wlen;
137908e60ed1SAndi Kleen 		column += n;
138032858480SArnaldo Carvalho de Melo 		s = skip_spaces(s);
138108e60ed1SAndi Kleen 	}
1382dc098b35SAndi Kleen }
1383dc098b35SAndi Kleen 
1384c8d6828aSSukadev Bhattiprolu void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag,
1385bf874fcfSAndi Kleen 			bool long_desc, bool details_flag)
1386dc098b35SAndi Kleen {
1387dc098b35SAndi Kleen 	struct perf_pmu *pmu;
1388dc098b35SAndi Kleen 	struct perf_pmu_alias *alias;
1389dc098b35SAndi Kleen 	char buf[1024];
1390dc098b35SAndi Kleen 	int printed = 0;
1391dc098b35SAndi Kleen 	int len, j;
1392dd5f1036SAndi Kleen 	struct sevent *aliases;
139308e60ed1SAndi Kleen 	int numdesc = 0;
139461eb2eb4SAndi Kleen 	int columns = pager_get_columns();
1395dd5f1036SAndi Kleen 	char *topic = NULL;
1396dc098b35SAndi Kleen 
1397dc098b35SAndi Kleen 	pmu = NULL;
1398dc098b35SAndi Kleen 	len = 0;
139942634bc7SAdrian Hunter 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1400dc098b35SAndi Kleen 		list_for_each_entry(alias, &pmu->aliases, list)
1401dc098b35SAndi Kleen 			len++;
140242634bc7SAdrian Hunter 		if (pmu->selectable)
140342634bc7SAdrian Hunter 			len++;
140442634bc7SAdrian Hunter 	}
1405dd5f1036SAndi Kleen 	aliases = zalloc(sizeof(struct sevent) * len);
1406dc098b35SAndi Kleen 	if (!aliases)
14077e4772dcSArnaldo Carvalho de Melo 		goto out_enomem;
1408dc098b35SAndi Kleen 	pmu = NULL;
1409dc098b35SAndi Kleen 	j = 0;
141042634bc7SAdrian Hunter 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1411dc098b35SAndi Kleen 		list_for_each_entry(alias, &pmu->aliases, list) {
141208e60ed1SAndi Kleen 			char *name = alias->desc ? alias->name :
141308e60ed1SAndi Kleen 				format_alias(buf, sizeof(buf), pmu, alias);
1414dc098b35SAndi Kleen 			bool is_cpu = !strcmp(pmu->name, "cpu");
1415dc098b35SAndi Kleen 
1416dc098b35SAndi Kleen 			if (event_glob != NULL &&
141738d14f0cSAndi Kleen 			    !(strglobmatch_nocase(name, event_glob) ||
141838d14f0cSAndi Kleen 			      (!is_cpu && strglobmatch_nocase(alias->name,
141967bdc35fSAndi Kleen 						       event_glob)) ||
142067bdc35fSAndi Kleen 			      (alias->topic &&
142167bdc35fSAndi Kleen 			       strglobmatch_nocase(alias->topic, event_glob))))
1422dc098b35SAndi Kleen 				continue;
14237e4772dcSArnaldo Carvalho de Melo 
142408e60ed1SAndi Kleen 			if (is_cpu && !name_only && !alias->desc)
14257e4772dcSArnaldo Carvalho de Melo 				name = format_alias_or(buf, sizeof(buf), pmu, alias);
14267e4772dcSArnaldo Carvalho de Melo 
142708e60ed1SAndi Kleen 			aliases[j].name = name;
142808e60ed1SAndi Kleen 			if (is_cpu && !name_only && !alias->desc)
142908e60ed1SAndi Kleen 				aliases[j].name = format_alias_or(buf,
143008e60ed1SAndi Kleen 								  sizeof(buf),
143108e60ed1SAndi Kleen 								  pmu, alias);
143208e60ed1SAndi Kleen 			aliases[j].name = strdup(aliases[j].name);
143308e60ed1SAndi Kleen 			if (!aliases[j].name)
14347e4772dcSArnaldo Carvalho de Melo 				goto out_enomem;
143508e60ed1SAndi Kleen 
1436c8d6828aSSukadev Bhattiprolu 			aliases[j].desc = long_desc ? alias->long_desc :
1437c8d6828aSSukadev Bhattiprolu 						alias->desc;
1438dd5f1036SAndi Kleen 			aliases[j].topic = alias->topic;
1439f2361024SAndi Kleen 			aliases[j].str = alias->str;
1440f2361024SAndi Kleen 			aliases[j].pmu = pmu->name;
14417f372a63SAndi Kleen 			aliases[j].metric_expr = alias->metric_expr;
144296284814SAndi Kleen 			aliases[j].metric_name = alias->metric_name;
1443dc098b35SAndi Kleen 			j++;
1444dc098b35SAndi Kleen 		}
1445fa52ceabSArnaldo Carvalho de Melo 		if (pmu->selectable &&
1446fa52ceabSArnaldo Carvalho de Melo 		    (event_glob == NULL || strglobmatch(pmu->name, event_glob))) {
14477e4772dcSArnaldo Carvalho de Melo 			char *s;
14487e4772dcSArnaldo Carvalho de Melo 			if (asprintf(&s, "%s//", pmu->name) < 0)
14497e4772dcSArnaldo Carvalho de Melo 				goto out_enomem;
145008e60ed1SAndi Kleen 			aliases[j].name = s;
145142634bc7SAdrian Hunter 			j++;
145242634bc7SAdrian Hunter 		}
145342634bc7SAdrian Hunter 	}
1454dc098b35SAndi Kleen 	len = j;
1455dd5f1036SAndi Kleen 	qsort(aliases, len, sizeof(struct sevent), cmp_sevent);
1456dc098b35SAndi Kleen 	for (j = 0; j < len; j++) {
145715b22ed3SAndi Kleen 		/* Skip duplicates */
145815b22ed3SAndi Kleen 		if (j > 0 && !strcmp(aliases[j].name, aliases[j - 1].name))
145915b22ed3SAndi Kleen 			continue;
1460dc098b35SAndi Kleen 		if (name_only) {
146108e60ed1SAndi Kleen 			printf("%s ", aliases[j].name);
1462dc098b35SAndi Kleen 			continue;
1463dc098b35SAndi Kleen 		}
14641c5f01feSAndi Kleen 		if (aliases[j].desc && !quiet_flag) {
146508e60ed1SAndi Kleen 			if (numdesc++ == 0)
146608e60ed1SAndi Kleen 				printf("\n");
1467dd5f1036SAndi Kleen 			if (aliases[j].topic && (!topic ||
1468dd5f1036SAndi Kleen 					strcmp(topic, aliases[j].topic))) {
1469dd5f1036SAndi Kleen 				printf("%s%s:\n", topic ? "\n" : "",
1470dd5f1036SAndi Kleen 						aliases[j].topic);
1471dd5f1036SAndi Kleen 				topic = aliases[j].topic;
1472dd5f1036SAndi Kleen 			}
147308e60ed1SAndi Kleen 			printf("  %-50s\n", aliases[j].name);
147408e60ed1SAndi Kleen 			printf("%*s", 8, "[");
147508e60ed1SAndi Kleen 			wordwrap(aliases[j].desc, 8, columns, 0);
147608e60ed1SAndi Kleen 			printf("]\n");
1477bf874fcfSAndi Kleen 			if (details_flag) {
14787f372a63SAndi Kleen 				printf("%*s%s/%s/ ", 8, "", aliases[j].pmu, aliases[j].str);
147996284814SAndi Kleen 				if (aliases[j].metric_name)
148096284814SAndi Kleen 					printf(" MetricName: %s", aliases[j].metric_name);
14817f372a63SAndi Kleen 				if (aliases[j].metric_expr)
14827f372a63SAndi Kleen 					printf(" MetricExpr: %s", aliases[j].metric_expr);
14837f372a63SAndi Kleen 				putchar('\n');
14847f372a63SAndi Kleen 			}
148508e60ed1SAndi Kleen 		} else
148608e60ed1SAndi Kleen 			printf("  %-50s [Kernel PMU event]\n", aliases[j].name);
1487dc098b35SAndi Kleen 		printed++;
1488dc098b35SAndi Kleen 	}
1489dfc431cbSArnaldo Carvalho de Melo 	if (printed && pager_in_use())
1490dc098b35SAndi Kleen 		printf("\n");
14917e4772dcSArnaldo Carvalho de Melo out_free:
14927e4772dcSArnaldo Carvalho de Melo 	for (j = 0; j < len; j++)
149308e60ed1SAndi Kleen 		zfree(&aliases[j].name);
14947e4772dcSArnaldo Carvalho de Melo 	zfree(&aliases);
14957e4772dcSArnaldo Carvalho de Melo 	return;
14967e4772dcSArnaldo Carvalho de Melo 
14977e4772dcSArnaldo Carvalho de Melo out_enomem:
14987e4772dcSArnaldo Carvalho de Melo 	printf("FATAL: not enough memory to print PMU events\n");
14997e4772dcSArnaldo Carvalho de Melo 	if (aliases)
15007e4772dcSArnaldo Carvalho de Melo 		goto out_free;
1501dc098b35SAndi Kleen }
15024cabc3d1SAndi Kleen 
15034cabc3d1SAndi Kleen bool pmu_have_event(const char *pname, const char *name)
15044cabc3d1SAndi Kleen {
15054cabc3d1SAndi Kleen 	struct perf_pmu *pmu;
15064cabc3d1SAndi Kleen 	struct perf_pmu_alias *alias;
15074cabc3d1SAndi Kleen 
15084cabc3d1SAndi Kleen 	pmu = NULL;
15094cabc3d1SAndi Kleen 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
15104cabc3d1SAndi Kleen 		if (strcmp(pname, pmu->name))
15114cabc3d1SAndi Kleen 			continue;
15124cabc3d1SAndi Kleen 		list_for_each_entry(alias, &pmu->aliases, list)
15134cabc3d1SAndi Kleen 			if (!strcmp(alias->name, name))
15144cabc3d1SAndi Kleen 				return true;
15154cabc3d1SAndi Kleen 	}
15164cabc3d1SAndi Kleen 	return false;
15174cabc3d1SAndi Kleen }
15187d4bdab5SAdrian Hunter 
15197d4bdab5SAdrian Hunter static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
15207d4bdab5SAdrian Hunter {
15217d4bdab5SAdrian Hunter 	struct stat st;
15227d4bdab5SAdrian Hunter 	char path[PATH_MAX];
15237d4bdab5SAdrian Hunter 	const char *sysfs;
15247d4bdab5SAdrian Hunter 
15257d4bdab5SAdrian Hunter 	sysfs = sysfs__mountpoint();
15267d4bdab5SAdrian Hunter 	if (!sysfs)
15277d4bdab5SAdrian Hunter 		return NULL;
15287d4bdab5SAdrian Hunter 
15297d4bdab5SAdrian Hunter 	snprintf(path, PATH_MAX,
15307d4bdab5SAdrian Hunter 		 "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name);
15317d4bdab5SAdrian Hunter 
15327d4bdab5SAdrian Hunter 	if (stat(path, &st) < 0)
15337d4bdab5SAdrian Hunter 		return NULL;
15347d4bdab5SAdrian Hunter 
15357d4bdab5SAdrian Hunter 	return fopen(path, "r");
15367d4bdab5SAdrian Hunter }
15377d4bdab5SAdrian Hunter 
15387d4bdab5SAdrian Hunter int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
15397d4bdab5SAdrian Hunter 			...)
15407d4bdab5SAdrian Hunter {
15417d4bdab5SAdrian Hunter 	va_list args;
15427d4bdab5SAdrian Hunter 	FILE *file;
15437d4bdab5SAdrian Hunter 	int ret = EOF;
15447d4bdab5SAdrian Hunter 
15457d4bdab5SAdrian Hunter 	va_start(args, fmt);
15467d4bdab5SAdrian Hunter 	file = perf_pmu__open_file(pmu, name);
15477d4bdab5SAdrian Hunter 	if (file) {
15487d4bdab5SAdrian Hunter 		ret = vfscanf(file, fmt, args);
15497d4bdab5SAdrian Hunter 		fclose(file);
15507d4bdab5SAdrian Hunter 	}
15517d4bdab5SAdrian Hunter 	va_end(args);
15527d4bdab5SAdrian Hunter 	return ret;
15537d4bdab5SAdrian Hunter }
1554