xref: /openbmc/linux/tools/perf/util/pmu.c (revision 5af155f9)
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>
6c47a5599SJin Yao #include <linux/ctype.h>
7cd82a32eSJiri Olsa #include <sys/types.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>
13cd82a32eSJiri Olsa #include <dirent.h>
14cd0cfad7SBorislav Petkov #include <api/fs/fs.h>
15410136f5SStephane Eranian #include <locale.h>
16c47a5599SJin Yao #include <fnmatch.h>
17acef233bSJing Zhang #include <math.h>
185e51b0bbSArnaldo Carvalho de Melo #include "debug.h"
19e12ee9f7SAdrian Hunter #include "evsel.h"
20cd82a32eSJiri Olsa #include "pmu.h"
21336b92daSRavi Bangoria #include "pmus.h"
22c7e97f21SNamhyung Kim #include <util/pmu-bison.h>
23c7e97f21SNamhyung Kim #include <util/pmu-flex.h>
24cd82a32eSJiri Olsa #include "parse-events.h"
25e5c6109fSIan Rogers #include "print-events.h"
26933f82ffSSukadev Bhattiprolu #include "header.h"
27a067558eSArnaldo Carvalho de Melo #include "string2.h"
28fa0d9846SArnaldo Carvalho de Melo #include "strbuf.h"
29d9664582SAndi Kleen #include "fncache.h"
306593f019SJames Clark #include "util/evsel_config.h"
31c5aaeae2SJing Zhang #include <regex.h>
32cd82a32eSJiri Olsa 
33c091ee90SIan Rogers struct perf_pmu perf_pmu__fake = {
34c091ee90SIan Rogers 	.name = "fake",
35c091ee90SIan Rogers };
36e46fc8d9SArnaldo Carvalho de Melo 
37c3245d20SIan Rogers #define UNIT_MAX_LEN	31 /* max length for event unit name */
38c3245d20SIan Rogers 
395af155f9SIan Rogers enum event_source {
405af155f9SIan Rogers 	/* An event loaded from /sys/devices/<pmu>/events. */
415af155f9SIan Rogers 	EVENT_SRC_SYSFS,
425af155f9SIan Rogers 	/* An event loaded from a CPUID matched json file. */
435af155f9SIan Rogers 	EVENT_SRC_CPU_JSON,
445af155f9SIan Rogers 	/*
455af155f9SIan Rogers 	 * An event loaded from a /sys/devices/<pmu>/identifier matched json
465af155f9SIan Rogers 	 * file.
475af155f9SIan Rogers 	 */
485af155f9SIan Rogers 	EVENT_SRC_SYS_JSON,
495af155f9SIan Rogers };
505af155f9SIan Rogers 
51c3245d20SIan Rogers /**
52c3245d20SIan Rogers  * struct perf_pmu_alias - An event either read from sysfs or builtin in
53c3245d20SIan Rogers  * pmu-events.c, created by parsing the pmu-events json files.
54c3245d20SIan Rogers  */
55c3245d20SIan Rogers struct perf_pmu_alias {
56c3245d20SIan Rogers 	/** @name: Name of the event like "mem-loads". */
57c3245d20SIan Rogers 	char *name;
58c3245d20SIan Rogers 	/** @desc: Optional short description of the event. */
59c3245d20SIan Rogers 	char *desc;
60c3245d20SIan Rogers 	/** @long_desc: Optional long description. */
61c3245d20SIan Rogers 	char *long_desc;
62c3245d20SIan Rogers 	/**
63c3245d20SIan Rogers 	 * @topic: Optional topic such as cache or pipeline, particularly for
64c3245d20SIan Rogers 	 * json events.
65c3245d20SIan Rogers 	 */
66c3245d20SIan Rogers 	char *topic;
67c3245d20SIan Rogers 	/** @terms: Owned list of the original parsed parameters. */
68c3245d20SIan Rogers 	struct list_head terms;
69c3245d20SIan Rogers 	/** @list: List element of struct perf_pmu aliases. */
70c3245d20SIan Rogers 	struct list_head list;
717b723dbbSIan Rogers 	/**
727b723dbbSIan Rogers 	 * @pmu_name: The name copied from the json struct pmu_event. This can
737b723dbbSIan Rogers 	 * differ from the PMU name as it won't have suffixes.
747b723dbbSIan Rogers 	 */
757b723dbbSIan Rogers 	char *pmu_name;
76c3245d20SIan Rogers 	/** @unit: Units for the event, such as bytes or cache lines. */
77c3245d20SIan Rogers 	char unit[UNIT_MAX_LEN+1];
78c3245d20SIan Rogers 	/** @scale: Value to scale read counter values by. */
79c3245d20SIan Rogers 	double scale;
80c3245d20SIan Rogers 	/**
81c3245d20SIan Rogers 	 * @per_pkg: Does the file
82c3245d20SIan Rogers 	 * <sysfs>/bus/event_source/devices/<pmu_name>/events/<name>.per-pkg or
83c3245d20SIan Rogers 	 * equivalent json value exist and have the value 1.
84c3245d20SIan Rogers 	 */
85c3245d20SIan Rogers 	bool per_pkg;
86c3245d20SIan Rogers 	/**
87c3245d20SIan Rogers 	 * @snapshot: Does the file
88c3245d20SIan Rogers 	 * <sysfs>/bus/event_source/devices/<pmu_name>/events/<name>.snapshot
89c3245d20SIan Rogers 	 * exist and have the value 1.
90c3245d20SIan Rogers 	 */
91c3245d20SIan Rogers 	bool snapshot;
92c3245d20SIan Rogers 	/**
93c3245d20SIan Rogers 	 * @deprecated: Is the event hidden and so not shown in perf list by
94c3245d20SIan Rogers 	 * default.
95c3245d20SIan Rogers 	 */
96c3245d20SIan Rogers 	bool deprecated;
977b723dbbSIan Rogers 	/** @from_sysfs: Was the alias from sysfs or a json event? */
987b723dbbSIan Rogers 	bool from_sysfs;
997b723dbbSIan Rogers 	/** @info_loaded: Have the scale, unit and other values been read from disk? */
1007b723dbbSIan Rogers 	bool info_loaded;
101c3245d20SIan Rogers };
102c3245d20SIan Rogers 
103fe13d43dSIan Rogers /**
104fe13d43dSIan Rogers  * struct perf_pmu_format - Values from a format file read from
105fe13d43dSIan Rogers  * <sysfs>/devices/cpu/format/ held in struct perf_pmu.
106fe13d43dSIan Rogers  *
107fe13d43dSIan Rogers  * For example, the contents of <sysfs>/devices/cpu/format/event may be
108fe13d43dSIan Rogers  * "config:0-7" and will be represented here as name="event",
109fe13d43dSIan Rogers  * value=PERF_PMU_FORMAT_VALUE_CONFIG and bits 0 to 7 will be set.
110fe13d43dSIan Rogers  */
111ab1bf653SArnaldo Carvalho de Melo struct perf_pmu_format {
11250402641SIan Rogers 	/** @list: Element on list within struct perf_pmu. */
11350402641SIan Rogers 	struct list_head list;
11450402641SIan Rogers 	/** @bits: Which config bits are set by this format value. */
11550402641SIan Rogers 	DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
116fe13d43dSIan Rogers 	/** @name: The modifier/file name. */
117ab1bf653SArnaldo Carvalho de Melo 	char *name;
118fe13d43dSIan Rogers 	/**
119fe13d43dSIan Rogers 	 * @value : Which config value the format relates to. Supported values
120fe13d43dSIan Rogers 	 * are from PERF_PMU_FORMAT_VALUE_CONFIG to
121fe13d43dSIan Rogers 	 * PERF_PMU_FORMAT_VALUE_CONFIG_END.
122fe13d43dSIan Rogers 	 */
12350402641SIan Rogers 	u16 value;
12450402641SIan Rogers 	/** @loaded: Has the contents been loaded/parsed. */
12550402641SIan Rogers 	bool loaded;
126ab1bf653SArnaldo Carvalho de Melo };
127ab1bf653SArnaldo Carvalho de Melo 
1288d4b6d37SIan Rogers static int pmu_aliases_parse(struct perf_pmu *pmu);
1298d4b6d37SIan Rogers 
perf_pmu__new_format(struct list_head * list,char * name)13050402641SIan Rogers static struct perf_pmu_format *perf_pmu__new_format(struct list_head *list, char *name)
13150402641SIan Rogers {
13250402641SIan Rogers 	struct perf_pmu_format *format;
13350402641SIan Rogers 
13450402641SIan Rogers 	format = zalloc(sizeof(*format));
13550402641SIan Rogers 	if (!format)
13650402641SIan Rogers 		return NULL;
13750402641SIan Rogers 
13850402641SIan Rogers 	format->name = strdup(name);
13950402641SIan Rogers 	if (!format->name) {
14050402641SIan Rogers 		free(format);
14150402641SIan Rogers 		return NULL;
14250402641SIan Rogers 	}
14350402641SIan Rogers 	list_add_tail(&format->list, list);
14450402641SIan Rogers 	return format;
14550402641SIan Rogers }
14650402641SIan Rogers 
14750402641SIan Rogers /* Called at the end of parsing a format. */
perf_pmu_format__set_value(void * vformat,int config,unsigned long * bits)14850402641SIan Rogers void perf_pmu_format__set_value(void *vformat, int config, unsigned long *bits)
14950402641SIan Rogers {
15050402641SIan Rogers 	struct perf_pmu_format *format = vformat;
15150402641SIan Rogers 
15250402641SIan Rogers 	format->value = config;
15350402641SIan Rogers 	memcpy(format->bits, bits, sizeof(format->bits));
15450402641SIan Rogers }
15550402641SIan Rogers 
__perf_pmu_format__load(struct perf_pmu_format * format,FILE * file)15650402641SIan Rogers static void __perf_pmu_format__load(struct perf_pmu_format *format, FILE *file)
15750402641SIan Rogers {
15850402641SIan Rogers 	void *scanner;
15950402641SIan Rogers 	int ret;
16050402641SIan Rogers 
16150402641SIan Rogers 	ret = perf_pmu_lex_init(&scanner);
16250402641SIan Rogers 	if (ret)
16350402641SIan Rogers 		return;
16450402641SIan Rogers 
16550402641SIan Rogers 	perf_pmu_set_in(file, scanner);
16650402641SIan Rogers 	ret = perf_pmu_parse(format, scanner);
16750402641SIan Rogers 	perf_pmu_lex_destroy(scanner);
16850402641SIan Rogers 	format->loaded = true;
16950402641SIan Rogers }
17050402641SIan Rogers 
perf_pmu_format__load(struct perf_pmu * pmu,struct perf_pmu_format * format)17150402641SIan Rogers static void perf_pmu_format__load(struct perf_pmu *pmu, struct perf_pmu_format *format)
17250402641SIan Rogers {
17350402641SIan Rogers 	char path[PATH_MAX];
17450402641SIan Rogers 	FILE *file = NULL;
17550402641SIan Rogers 
17650402641SIan Rogers 	if (format->loaded)
17750402641SIan Rogers 		return;
17850402641SIan Rogers 
17950402641SIan Rogers 	if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, "format"))
18050402641SIan Rogers 		return;
18150402641SIan Rogers 
18250402641SIan Rogers 	assert(strlen(path) + strlen(format->name) + 2 < sizeof(path));
18350402641SIan Rogers 	strcat(path, "/");
18450402641SIan Rogers 	strcat(path, format->name);
18550402641SIan Rogers 
18650402641SIan Rogers 	file = fopen(path, "r");
18750402641SIan Rogers 	if (!file)
18850402641SIan Rogers 		return;
18950402641SIan Rogers 	__perf_pmu_format__load(format, file);
19050402641SIan Rogers 	fclose(file);
19150402641SIan Rogers }
19250402641SIan Rogers 
193cd82a32eSJiri Olsa /*
194cd82a32eSJiri Olsa  * Parse & process all the sysfs attributes located under
195cd82a32eSJiri Olsa  * the directory specified in 'dir' parameter.
196cd82a32eSJiri Olsa  */
perf_pmu__format_parse(struct perf_pmu * pmu,int dirfd,bool eager_load)19750402641SIan Rogers int perf_pmu__format_parse(struct perf_pmu *pmu, int dirfd, bool eager_load)
198cd82a32eSJiri Olsa {
199cd82a32eSJiri Olsa 	struct dirent *evt_ent;
200cd82a32eSJiri Olsa 	DIR *format_dir;
201cd82a32eSJiri Olsa 	int ret = 0;
202cd82a32eSJiri Olsa 
203e293a5e8SNamhyung Kim 	format_dir = fdopendir(dirfd);
204cd82a32eSJiri Olsa 	if (!format_dir)
205cd82a32eSJiri Olsa 		return -EINVAL;
206cd82a32eSJiri Olsa 
20750402641SIan Rogers 	while ((evt_ent = readdir(format_dir)) != NULL) {
20850402641SIan Rogers 		struct perf_pmu_format *format;
209cd82a32eSJiri Olsa 		char *name = evt_ent->d_name;
210cd82a32eSJiri Olsa 
211cd82a32eSJiri Olsa 		if (!strcmp(name, ".") || !strcmp(name, ".."))
212cd82a32eSJiri Olsa 			continue;
213cd82a32eSJiri Olsa 
21450402641SIan Rogers 		format = perf_pmu__new_format(&pmu->format, name);
21550402641SIan Rogers 		if (!format) {
21650402641SIan Rogers 			ret = -ENOMEM;
217cd82a32eSJiri Olsa 			break;
21850402641SIan Rogers 		}
219cd82a32eSJiri Olsa 
22050402641SIan Rogers 		if (eager_load) {
22150402641SIan Rogers 			FILE *file;
22250402641SIan Rogers 			int fd = openat(dirfd, name, O_RDONLY);
22350402641SIan Rogers 
22450402641SIan Rogers 			if (fd < 0) {
22550402641SIan Rogers 				ret = -errno;
22650402641SIan Rogers 				break;
22750402641SIan Rogers 			}
2283d88aec0SIan Rogers 			file = fdopen(fd, "r");
2293d88aec0SIan Rogers 			if (!file) {
2303d88aec0SIan Rogers 				close(fd);
2313d88aec0SIan Rogers 				break;
2323d88aec0SIan Rogers 			}
23350402641SIan Rogers 			__perf_pmu_format__load(format, file);
2343d88aec0SIan Rogers 			fclose(file);
2353d88aec0SIan Rogers 		}
236cd82a32eSJiri Olsa 	}
237cd82a32eSJiri Olsa 
238cd82a32eSJiri Olsa 	closedir(format_dir);
239cd82a32eSJiri Olsa 	return ret;
240cd82a32eSJiri Olsa }
241cd82a32eSJiri Olsa 
242cd82a32eSJiri Olsa /*
243cd82a32eSJiri Olsa  * Reading/parsing the default pmu format definition, which should be
244cd82a32eSJiri Olsa  * located at:
245cd82a32eSJiri Olsa  * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
246cd82a32eSJiri Olsa  */
pmu_format(struct perf_pmu * pmu,int dirfd,const char * name)247838a8c5fSIan Rogers static int pmu_format(struct perf_pmu *pmu, int dirfd, const char *name)
248cd82a32eSJiri Olsa {
249e293a5e8SNamhyung Kim 	int fd;
250cd82a32eSJiri Olsa 
251e293a5e8SNamhyung Kim 	fd = perf_pmu__pathname_fd(dirfd, name, "format", O_DIRECTORY);
252e293a5e8SNamhyung Kim 	if (fd < 0)
253d9664582SAndi Kleen 		return 0;
254cd82a32eSJiri Olsa 
255e293a5e8SNamhyung Kim 	/* it'll close the fd */
25650402641SIan Rogers 	if (perf_pmu__format_parse(pmu, fd, /*eager_load=*/false))
257cd82a32eSJiri Olsa 		return -1;
258cd82a32eSJiri Olsa 
259cd82a32eSJiri Olsa 	return 0;
260cd82a32eSJiri Olsa }
261cd82a32eSJiri Olsa 
perf_pmu__convert_scale(const char * scale,char ** end,double * sval)262a55ab7c4SJin Yao int perf_pmu__convert_scale(const char *scale, char **end, double *sval)
263d02fc6bcSAndi Kleen {
264d02fc6bcSAndi Kleen 	char *lc;
265d02fc6bcSAndi Kleen 	int ret = 0;
266d02fc6bcSAndi Kleen 
267d02fc6bcSAndi Kleen 	/*
268d02fc6bcSAndi Kleen 	 * save current locale
269d02fc6bcSAndi Kleen 	 */
270d02fc6bcSAndi Kleen 	lc = setlocale(LC_NUMERIC, NULL);
271d02fc6bcSAndi Kleen 
272d02fc6bcSAndi Kleen 	/*
273d02fc6bcSAndi Kleen 	 * The lc string may be allocated in static storage,
274d02fc6bcSAndi Kleen 	 * so get a dynamic copy to make it survive setlocale
275d02fc6bcSAndi Kleen 	 * call below.
276d02fc6bcSAndi Kleen 	 */
277d02fc6bcSAndi Kleen 	lc = strdup(lc);
278d02fc6bcSAndi Kleen 	if (!lc) {
279d02fc6bcSAndi Kleen 		ret = -ENOMEM;
280d02fc6bcSAndi Kleen 		goto out;
281d02fc6bcSAndi Kleen 	}
282d02fc6bcSAndi Kleen 
283d02fc6bcSAndi Kleen 	/*
284d02fc6bcSAndi Kleen 	 * force to C locale to ensure kernel
285d02fc6bcSAndi Kleen 	 * scale string is converted correctly.
286d02fc6bcSAndi Kleen 	 * kernel uses default C locale.
287d02fc6bcSAndi Kleen 	 */
288d02fc6bcSAndi Kleen 	setlocale(LC_NUMERIC, "C");
289d02fc6bcSAndi Kleen 
290d02fc6bcSAndi Kleen 	*sval = strtod(scale, end);
291d02fc6bcSAndi Kleen 
292d02fc6bcSAndi Kleen out:
293d02fc6bcSAndi Kleen 	/* restore locale */
294d02fc6bcSAndi Kleen 	setlocale(LC_NUMERIC, lc);
295d02fc6bcSAndi Kleen 	free(lc);
296d02fc6bcSAndi Kleen 	return ret;
297d02fc6bcSAndi Kleen }
298d02fc6bcSAndi Kleen 
perf_pmu__parse_scale(struct perf_pmu * pmu,struct perf_pmu_alias * alias)2997b723dbbSIan Rogers static int perf_pmu__parse_scale(struct perf_pmu *pmu, struct perf_pmu_alias *alias)
300410136f5SStephane Eranian {
301410136f5SStephane Eranian 	struct stat st;
302410136f5SStephane Eranian 	ssize_t sret;
3037b723dbbSIan Rogers 	size_t len;
304410136f5SStephane Eranian 	char scale[128];
305410136f5SStephane Eranian 	int fd, ret = -1;
306410136f5SStephane Eranian 	char path[PATH_MAX];
307410136f5SStephane Eranian 
3087b723dbbSIan Rogers 	len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
3097b723dbbSIan Rogers 	if (!len)
3107b723dbbSIan Rogers 		return 0;
31148a3adcfSWyes Karny 	scnprintf(path + len, sizeof(path) - len, "%s/events/%s.scale", pmu->name, alias->name);
312410136f5SStephane Eranian 
3137b723dbbSIan Rogers 	fd = open(path, O_RDONLY);
314410136f5SStephane Eranian 	if (fd == -1)
315410136f5SStephane Eranian 		return -1;
316410136f5SStephane Eranian 
317410136f5SStephane Eranian 	if (fstat(fd, &st) < 0)
318410136f5SStephane Eranian 		goto error;
319410136f5SStephane Eranian 
320410136f5SStephane Eranian 	sret = read(fd, scale, sizeof(scale)-1);
321410136f5SStephane Eranian 	if (sret < 0)
322410136f5SStephane Eranian 		goto error;
323410136f5SStephane Eranian 
3249ecae065SMadhavan Srinivasan 	if (scale[sret - 1] == '\n')
3259ecae065SMadhavan Srinivasan 		scale[sret - 1] = '\0';
3269ecae065SMadhavan Srinivasan 	else
327410136f5SStephane Eranian 		scale[sret] = '\0';
3289ecae065SMadhavan Srinivasan 
329a55ab7c4SJin Yao 	ret = perf_pmu__convert_scale(scale, NULL, &alias->scale);
330410136f5SStephane Eranian error:
331410136f5SStephane Eranian 	close(fd);
332410136f5SStephane Eranian 	return ret;
333410136f5SStephane Eranian }
334410136f5SStephane Eranian 
perf_pmu__parse_unit(struct perf_pmu * pmu,struct perf_pmu_alias * alias)3357b723dbbSIan Rogers static int perf_pmu__parse_unit(struct perf_pmu *pmu, struct perf_pmu_alias *alias)
336410136f5SStephane Eranian {
337410136f5SStephane Eranian 	char path[PATH_MAX];
3387b723dbbSIan Rogers 	size_t len;
339410136f5SStephane Eranian 	ssize_t sret;
340410136f5SStephane Eranian 	int fd;
341410136f5SStephane Eranian 
342410136f5SStephane Eranian 
3437b723dbbSIan Rogers 	len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
3447b723dbbSIan Rogers 	if (!len)
3457b723dbbSIan Rogers 		return 0;
34648a3adcfSWyes Karny 	scnprintf(path + len, sizeof(path) - len, "%s/events/%s.unit", pmu->name, alias->name);
3477b723dbbSIan Rogers 
3487b723dbbSIan Rogers 	fd = open(path, O_RDONLY);
349410136f5SStephane Eranian 	if (fd == -1)
350410136f5SStephane Eranian 		return -1;
351410136f5SStephane Eranian 
352410136f5SStephane Eranian 	sret = read(fd, alias->unit, UNIT_MAX_LEN);
353410136f5SStephane Eranian 	if (sret < 0)
354410136f5SStephane Eranian 		goto error;
355410136f5SStephane Eranian 
356410136f5SStephane Eranian 	close(fd);
357410136f5SStephane Eranian 
3589ecae065SMadhavan Srinivasan 	if (alias->unit[sret - 1] == '\n')
3599ecae065SMadhavan Srinivasan 		alias->unit[sret - 1] = '\0';
3609ecae065SMadhavan Srinivasan 	else
361410136f5SStephane Eranian 		alias->unit[sret] = '\0';
362410136f5SStephane Eranian 
363410136f5SStephane Eranian 	return 0;
364410136f5SStephane Eranian error:
365410136f5SStephane Eranian 	close(fd);
366410136f5SStephane Eranian 	alias->unit[0] = '\0';
367410136f5SStephane Eranian 	return -1;
368410136f5SStephane Eranian }
369410136f5SStephane Eranian 
370044330c1SMatt Fleming static int
perf_pmu__parse_per_pkg(struct perf_pmu * pmu,struct perf_pmu_alias * alias)3717b723dbbSIan Rogers perf_pmu__parse_per_pkg(struct perf_pmu *pmu, struct perf_pmu_alias *alias)
372044330c1SMatt Fleming {
373044330c1SMatt Fleming 	char path[PATH_MAX];
3747b723dbbSIan Rogers 	size_t len;
375044330c1SMatt Fleming 	int fd;
376044330c1SMatt Fleming 
3777b723dbbSIan Rogers 	len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
3787b723dbbSIan Rogers 	if (!len)
3797b723dbbSIan Rogers 		return 0;
38048a3adcfSWyes Karny 	scnprintf(path + len, sizeof(path) - len, "%s/events/%s.per-pkg", pmu->name, alias->name);
381044330c1SMatt Fleming 
3827b723dbbSIan Rogers 	fd = open(path, O_RDONLY);
383044330c1SMatt Fleming 	if (fd == -1)
384044330c1SMatt Fleming 		return -1;
385044330c1SMatt Fleming 
386044330c1SMatt Fleming 	close(fd);
387044330c1SMatt Fleming 
388044330c1SMatt Fleming 	alias->per_pkg = true;
389044330c1SMatt Fleming 	return 0;
390044330c1SMatt Fleming }
391044330c1SMatt Fleming 
perf_pmu__parse_snapshot(struct perf_pmu * pmu,struct perf_pmu_alias * alias)3927b723dbbSIan Rogers static int perf_pmu__parse_snapshot(struct perf_pmu *pmu, struct perf_pmu_alias *alias)
3931d9e446bSJiri Olsa {
3941d9e446bSJiri Olsa 	char path[PATH_MAX];
3957b723dbbSIan Rogers 	size_t len;
3961d9e446bSJiri Olsa 	int fd;
3971d9e446bSJiri Olsa 
3987b723dbbSIan Rogers 	len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
3997b723dbbSIan Rogers 	if (!len)
4007b723dbbSIan Rogers 		return 0;
40148a3adcfSWyes Karny 	scnprintf(path + len, sizeof(path) - len, "%s/events/%s.snapshot", pmu->name, alias->name);
4021d9e446bSJiri Olsa 
4037b723dbbSIan Rogers 	fd = open(path, O_RDONLY);
4041d9e446bSJiri Olsa 	if (fd == -1)
4051d9e446bSJiri Olsa 		return -1;
4061d9e446bSJiri Olsa 
4071d9e446bSJiri Olsa 	alias->snapshot = true;
4081d9e446bSJiri Olsa 	close(fd);
4091d9e446bSJiri Olsa 	return 0;
4101d9e446bSJiri Olsa }
4111d9e446bSJiri Olsa 
4126dde6429SThomas Richter /* Delete an alias entry. */
perf_pmu_free_alias(struct perf_pmu_alias * newalias)413c3245d20SIan Rogers static void perf_pmu_free_alias(struct perf_pmu_alias *newalias)
4146dde6429SThomas Richter {
4156dde6429SThomas Richter 	zfree(&newalias->name);
4166dde6429SThomas Richter 	zfree(&newalias->desc);
4176dde6429SThomas Richter 	zfree(&newalias->long_desc);
4186dde6429SThomas Richter 	zfree(&newalias->topic);
41932705de7SJin Yao 	zfree(&newalias->pmu_name);
4206dde6429SThomas Richter 	parse_events_terms__purge(&newalias->terms);
4216dde6429SThomas Richter 	free(newalias);
4226dde6429SThomas Richter }
4236dde6429SThomas Richter 
perf_pmu__del_aliases(struct perf_pmu * pmu)424eec11310SNamhyung Kim static void perf_pmu__del_aliases(struct perf_pmu *pmu)
425eec11310SNamhyung Kim {
426eec11310SNamhyung Kim 	struct perf_pmu_alias *alias, *tmp;
427eec11310SNamhyung Kim 
428eec11310SNamhyung Kim 	list_for_each_entry_safe(alias, tmp, &pmu->aliases, list) {
429eec11310SNamhyung Kim 		list_del(&alias->list);
430eec11310SNamhyung Kim 		perf_pmu_free_alias(alias);
431eec11310SNamhyung Kim 	}
432eec11310SNamhyung Kim }
433eec11310SNamhyung Kim 
perf_pmu__find_alias(struct perf_pmu * pmu,const char * name,bool load)4348d4b6d37SIan Rogers static struct perf_pmu_alias *perf_pmu__find_alias(struct perf_pmu *pmu,
4358d4b6d37SIan Rogers 						   const char *name,
4368d4b6d37SIan Rogers 						   bool load)
4376dde6429SThomas Richter {
438f63a536fSIan Rogers 	struct perf_pmu_alias *alias;
4396dde6429SThomas Richter 
4404ac93db3SIan Rogers 	if (load && !pmu->sysfs_aliases_loaded) {
4414ac93db3SIan Rogers 		bool has_sysfs_event;
4424ac93db3SIan Rogers 		char event_file_name[FILENAME_MAX + 8];
4434ac93db3SIan Rogers 
4444ac93db3SIan Rogers 		/*
4454ac93db3SIan Rogers 		 * Test if alias/event 'name' exists in the PMU's sysfs/events
4464ac93db3SIan Rogers 		 * directory. If not skip parsing the sysfs aliases. Sysfs event
4474ac93db3SIan Rogers 		 * name must be all lower or all upper case.
4484ac93db3SIan Rogers 		 */
4494ac93db3SIan Rogers 		scnprintf(event_file_name, sizeof(event_file_name), "events/%s", name);
4504ac93db3SIan Rogers 		for (size_t i = 7, n = 7 + strlen(name); i < n; i++)
4514ac93db3SIan Rogers 			event_file_name[i] = tolower(event_file_name[i]);
4524ac93db3SIan Rogers 
4534ac93db3SIan Rogers 		has_sysfs_event = perf_pmu__file_exists(pmu, event_file_name);
4544ac93db3SIan Rogers 		if (!has_sysfs_event) {
4554ac93db3SIan Rogers 			for (size_t i = 7, n = 7 + strlen(name); i < n; i++)
4564ac93db3SIan Rogers 				event_file_name[i] = toupper(event_file_name[i]);
4574ac93db3SIan Rogers 
4584ac93db3SIan Rogers 			has_sysfs_event = perf_pmu__file_exists(pmu, event_file_name);
4594ac93db3SIan Rogers 		}
4604ac93db3SIan Rogers 		if (has_sysfs_event)
4618d4b6d37SIan Rogers 			pmu_aliases_parse(pmu);
4628d4b6d37SIan Rogers 
4634ac93db3SIan Rogers 	}
464f63a536fSIan Rogers 	list_for_each_entry(alias, &pmu->aliases, list) {
465f63a536fSIan Rogers 		if (!strcasecmp(alias->name, name))
466f63a536fSIan Rogers 			return alias;
46732705de7SJin Yao 	}
468f63a536fSIan Rogers 	return NULL;
469f63a536fSIan Rogers }
470f63a536fSIan Rogers 
assign_str(const char * name,const char * field,char ** old_str,const char * new_str)471f63a536fSIan Rogers static bool assign_str(const char *name, const char *field, char **old_str,
472f63a536fSIan Rogers 				const char *new_str)
473f63a536fSIan Rogers {
474f63a536fSIan Rogers 	if (!*old_str && new_str) {
475f63a536fSIan Rogers 		*old_str = strdup(new_str);
4766dde6429SThomas Richter 		return true;
4776dde6429SThomas Richter 	}
478f63a536fSIan Rogers 
479f63a536fSIan Rogers 	if (!new_str || !strcasecmp(*old_str, new_str))
480f63a536fSIan Rogers 		return false; /* Nothing to update. */
481f63a536fSIan Rogers 
482f63a536fSIan Rogers 	pr_debug("alias %s differs in field '%s' ('%s' != '%s')\n",
483f63a536fSIan Rogers 		name, field, *old_str, new_str);
484f63a536fSIan Rogers 	zfree(old_str);
485f63a536fSIan Rogers 	*old_str = strdup(new_str);
486f63a536fSIan Rogers 	return true;
4876dde6429SThomas Richter }
488f63a536fSIan Rogers 
read_alias_info(struct perf_pmu * pmu,struct perf_pmu_alias * alias)4897b723dbbSIan Rogers static void read_alias_info(struct perf_pmu *pmu, struct perf_pmu_alias *alias)
4907b723dbbSIan Rogers {
4917b723dbbSIan Rogers 	if (!alias->from_sysfs || alias->info_loaded)
4927b723dbbSIan Rogers 		return;
4937b723dbbSIan Rogers 
4947b723dbbSIan Rogers 	/*
4957b723dbbSIan Rogers 	 * load unit name and scale if available
4967b723dbbSIan Rogers 	 */
4977b723dbbSIan Rogers 	perf_pmu__parse_unit(pmu, alias);
4987b723dbbSIan Rogers 	perf_pmu__parse_scale(pmu, alias);
4997b723dbbSIan Rogers 	perf_pmu__parse_per_pkg(pmu, alias);
5007b723dbbSIan Rogers 	perf_pmu__parse_snapshot(pmu, alias);
5017b723dbbSIan Rogers }
5027b723dbbSIan Rogers 
5037b723dbbSIan Rogers struct update_alias_data {
5047b723dbbSIan Rogers 	struct perf_pmu *pmu;
5057b723dbbSIan Rogers 	struct perf_pmu_alias *alias;
5067b723dbbSIan Rogers };
5077b723dbbSIan Rogers 
update_alias(const struct pmu_event * pe,const struct pmu_events_table * table __maybe_unused,void * vdata)508f63a536fSIan Rogers static int update_alias(const struct pmu_event *pe,
509f63a536fSIan Rogers 			const struct pmu_events_table *table __maybe_unused,
510f63a536fSIan Rogers 			void *vdata)
511f63a536fSIan Rogers {
5127b723dbbSIan Rogers 	struct update_alias_data *data = vdata;
513f63a536fSIan Rogers 	int ret = 0;
514f63a536fSIan Rogers 
5157b723dbbSIan Rogers 	read_alias_info(data->pmu, data->alias);
5167b723dbbSIan Rogers 	assign_str(pe->name, "desc", &data->alias->desc, pe->desc);
5177b723dbbSIan Rogers 	assign_str(pe->name, "long_desc", &data->alias->long_desc, pe->long_desc);
5187b723dbbSIan Rogers 	assign_str(pe->name, "topic", &data->alias->topic, pe->topic);
5197b723dbbSIan Rogers 	data->alias->per_pkg = pe->perpkg;
52030f0b435SIan Rogers 	if (pe->event) {
5217b723dbbSIan Rogers 		parse_events_terms__purge(&data->alias->terms);
5227b723dbbSIan Rogers 		ret = parse_events_terms(&data->alias->terms, pe->event, /*input=*/NULL);
523f63a536fSIan Rogers 	}
524f63a536fSIan Rogers 	if (!ret && pe->unit) {
525f63a536fSIan Rogers 		char *unit;
526f63a536fSIan Rogers 
5277b723dbbSIan Rogers 		ret = perf_pmu__convert_scale(pe->unit, &unit, &data->alias->scale);
528f63a536fSIan Rogers 		if (!ret)
5297b723dbbSIan Rogers 			snprintf(data->alias->unit, sizeof(data->alias->unit), "%s", unit);
530f63a536fSIan Rogers 	}
531f63a536fSIan Rogers 	return ret;
5326dde6429SThomas Richter }
5336dde6429SThomas Richter 
perf_pmu__new_alias(struct perf_pmu * pmu,const char * name,const char * desc,const char * val,FILE * val_fd,const struct pmu_event * pe,enum event_source src)5347b723dbbSIan Rogers static int perf_pmu__new_alias(struct perf_pmu *pmu, const char *name,
535edb217ffSIan Rogers 				const char *desc, const char *val, FILE *val_fd,
5365af155f9SIan Rogers 			        const struct pmu_event *pe, enum event_source src)
537a6146d50SZheng Yan {
5385c6ccc37SArnaldo Carvalho de Melo 	struct perf_pmu_alias *alias;
539a6146d50SZheng Yan 	int ret;
540330f40a0SIan Rogers 	const char *long_desc = NULL, *topic = NULL, *unit = NULL, *pmu_name = NULL;
541bd680861SIan Rogers 	bool deprecated = false, perpkg = false;
542eab35953SJin Yao 
5438d4b6d37SIan Rogers 	if (perf_pmu__find_alias(pmu, name, /*load=*/ false)) {
544f63a536fSIan Rogers 		/* Alias was already created/loaded. */
545f63a536fSIan Rogers 		return 0;
546f63a536fSIan Rogers 	}
547f63a536fSIan Rogers 
548eab35953SJin Yao 	if (pe) {
549330f40a0SIan Rogers 		long_desc = pe->long_desc;
550330f40a0SIan Rogers 		topic = pe->topic;
551330f40a0SIan Rogers 		unit = pe->unit;
552bd680861SIan Rogers 		perpkg = pe->perpkg;
5539ed8b7dcSIan Rogers 		deprecated = pe->deprecated;
554330f40a0SIan Rogers 		pmu_name = pe->pmu;
555eab35953SJin Yao 	}
556a6146d50SZheng Yan 
557eaaebb01SIan Rogers 	alias = zalloc(sizeof(*alias));
558a6146d50SZheng Yan 	if (!alias)
559a6146d50SZheng Yan 		return -ENOMEM;
560a6146d50SZheng Yan 
561a6146d50SZheng Yan 	INIT_LIST_HEAD(&alias->terms);
562410136f5SStephane Eranian 	alias->scale = 1.0;
563410136f5SStephane Eranian 	alias->unit[0] = '\0';
564bd680861SIan Rogers 	alias->per_pkg = perpkg;
56584530920SStephane Eranian 	alias->snapshot = false;
5669ed8b7dcSIan Rogers 	alias->deprecated = deprecated;
567410136f5SStephane Eranian 
568edb217ffSIan Rogers 	ret = parse_events_terms(&alias->terms, val, val_fd);
569a6146d50SZheng Yan 	if (ret) {
57070c646e0SSukadev Bhattiprolu 		pr_err("Cannot parse alias %s: %d\n", val, ret);
571a6146d50SZheng Yan 		free(alias);
572a6146d50SZheng Yan 		return ret;
573a6146d50SZheng Yan 	}
574a6146d50SZheng Yan 
575a6146d50SZheng Yan 	alias->name = strdup(name);
57608e60ed1SAndi Kleen 	alias->desc = desc ? strdup(desc) : NULL;
577c8d6828aSSukadev Bhattiprolu 	alias->long_desc = long_desc ? strdup(long_desc) :
578c8d6828aSSukadev Bhattiprolu 				desc ? strdup(desc) : NULL;
579dd5f1036SAndi Kleen 	alias->topic = topic ? strdup(topic) : NULL;
580f63a536fSIan Rogers 	alias->pmu_name = pmu_name ? strdup(pmu_name) : NULL;
581fedb2b51SAndi Kleen 	if (unit) {
582f63a536fSIan Rogers 		if (perf_pmu__convert_scale(unit, (char **)&unit, &alias->scale) < 0) {
583f63a536fSIan Rogers 			perf_pmu_free_alias(alias);
584fedb2b51SAndi Kleen 			return -1;
585f63a536fSIan Rogers 		}
586fedb2b51SAndi Kleen 		snprintf(alias->unit, sizeof(alias->unit), "%s", unit);
587fedb2b51SAndi Kleen 	}
5885af155f9SIan Rogers 	switch (src) {
5895af155f9SIan Rogers 	default:
5905af155f9SIan Rogers 	case EVENT_SRC_SYSFS:
5915af155f9SIan Rogers 		alias->from_sysfs = true;
5925af155f9SIan Rogers 		if (pmu->events_table) {
593f63a536fSIan Rogers 			/* Update an event from sysfs with json data. */
5947b723dbbSIan Rogers 			struct update_alias_data data = {
5957b723dbbSIan Rogers 				.pmu = pmu,
5967b723dbbSIan Rogers 				.alias = alias,
5977b723dbbSIan Rogers 			};
598e6ff1eedSIan Rogers 			if (pmu_events_table__find_event(pmu->events_table, pmu, name,
5997b723dbbSIan Rogers 							 update_alias, &data) == 0)
6005af155f9SIan Rogers 				pmu->cpu_json_aliases++;
6017c52f10cSIan Rogers 		}
602e6ff1eedSIan Rogers 		pmu->sysfs_aliases++;
6035af155f9SIan Rogers 		break;
6045af155f9SIan Rogers 	case  EVENT_SRC_CPU_JSON:
6055af155f9SIan Rogers 		pmu->cpu_json_aliases++;
6065af155f9SIan Rogers 		break;
6075af155f9SIan Rogers 	case  EVENT_SRC_SYS_JSON:
6085af155f9SIan Rogers 		pmu->sys_json_aliases++;
6095af155f9SIan Rogers 		break;
6105af155f9SIan Rogers 
6115af155f9SIan Rogers 	}
612f26d22f1SIan Rogers 	list_add_tail(&alias->list, &pmu->aliases);
613a6146d50SZheng Yan 	return 0;
614a6146d50SZheng Yan }
615a6146d50SZheng Yan 
pmu_alias_info_file(char * name)61646441bdcSMatt Fleming static inline bool pmu_alias_info_file(char *name)
61746441bdcSMatt Fleming {
61846441bdcSMatt Fleming 	size_t len;
61946441bdcSMatt Fleming 
62046441bdcSMatt Fleming 	len = strlen(name);
62146441bdcSMatt Fleming 	if (len > 5 && !strcmp(name + len - 5, ".unit"))
62246441bdcSMatt Fleming 		return true;
62346441bdcSMatt Fleming 	if (len > 6 && !strcmp(name + len - 6, ".scale"))
62446441bdcSMatt Fleming 		return true;
625044330c1SMatt Fleming 	if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
626044330c1SMatt Fleming 		return true;
6271d9e446bSJiri Olsa 	if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
6281d9e446bSJiri Olsa 		return true;
62946441bdcSMatt Fleming 
63046441bdcSMatt Fleming 	return false;
63146441bdcSMatt Fleming }
63246441bdcSMatt Fleming 
633a6146d50SZheng Yan /*
6348d4b6d37SIan Rogers  * Reading the pmu event aliases definition, which should be located at:
6358d4b6d37SIan Rogers  * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
636a6146d50SZheng Yan  */
pmu_aliases_parse(struct perf_pmu * pmu)6378d4b6d37SIan Rogers static int pmu_aliases_parse(struct perf_pmu *pmu)
638a6146d50SZheng Yan {
6398d4b6d37SIan Rogers 	char path[PATH_MAX];
640a6146d50SZheng Yan 	struct dirent *evt_ent;
641a6146d50SZheng Yan 	DIR *event_dir;
6428d4b6d37SIan Rogers 	size_t len;
6438d4b6d37SIan Rogers 	int fd, dir_fd;
644a6146d50SZheng Yan 
6458d4b6d37SIan Rogers 	len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
6468d4b6d37SIan Rogers 	if (!len)
6478d4b6d37SIan Rogers 		return 0;
6488d4b6d37SIan Rogers 	scnprintf(path + len, sizeof(path) - len, "%s/events", pmu->name);
6498d4b6d37SIan Rogers 
6508d4b6d37SIan Rogers 	dir_fd = open(path, O_DIRECTORY);
6518d4b6d37SIan Rogers 	if (dir_fd == -1) {
6528d4b6d37SIan Rogers 		pmu->sysfs_aliases_loaded = true;
6538d4b6d37SIan Rogers 		return 0;
6548d4b6d37SIan Rogers 	}
6558d4b6d37SIan Rogers 
6568d4b6d37SIan Rogers 	event_dir = fdopendir(dir_fd);
6578d4b6d37SIan Rogers 	if (!event_dir){
6588d4b6d37SIan Rogers 		close (dir_fd);
659a6146d50SZheng Yan 		return -EINVAL;
6608d4b6d37SIan Rogers 	}
661a6146d50SZheng Yan 
662940db6dcSAndi Kleen 	while ((evt_ent = readdir(event_dir))) {
663a6146d50SZheng Yan 		char *name = evt_ent->d_name;
664a6146d50SZheng Yan 		FILE *file;
665a6146d50SZheng Yan 
666a6146d50SZheng Yan 		if (!strcmp(name, ".") || !strcmp(name, ".."))
667a6146d50SZheng Yan 			continue;
668a6146d50SZheng Yan 
669410136f5SStephane Eranian 		/*
67046441bdcSMatt Fleming 		 * skip info files parsed in perf_pmu__new_alias()
671410136f5SStephane Eranian 		 */
67246441bdcSMatt Fleming 		if (pmu_alias_info_file(name))
673410136f5SStephane Eranian 			continue;
674410136f5SStephane Eranian 
6758d4b6d37SIan Rogers 		fd = openat(dir_fd, name, O_RDONLY);
6760ea8920eSIan Rogers 		if (fd == -1) {
6770ea8920eSIan Rogers 			pr_debug("Cannot open %s\n", name);
6780ea8920eSIan Rogers 			continue;
6790ea8920eSIan Rogers 		}
680e293a5e8SNamhyung Kim 		file = fdopen(fd, "r");
681940db6dcSAndi Kleen 		if (!file) {
6820ea8920eSIan Rogers 			close(fd);
683940db6dcSAndi Kleen 			continue;
684940db6dcSAndi Kleen 		}
685410136f5SStephane Eranian 
6867b723dbbSIan Rogers 		if (perf_pmu__new_alias(pmu, name, /*desc=*/ NULL,
6875af155f9SIan Rogers 					/*val=*/ NULL, file, /*pe=*/ NULL,
6885af155f9SIan Rogers 					EVENT_SRC_SYSFS) < 0)
689940db6dcSAndi Kleen 			pr_debug("Cannot set up %s\n", name);
690a6146d50SZheng Yan 		fclose(file);
691a6146d50SZheng Yan 	}
692a6146d50SZheng Yan 
693a6146d50SZheng Yan 	closedir(event_dir);
6948d4b6d37SIan Rogers 	close (dir_fd);
6958d4b6d37SIan Rogers 	pmu->sysfs_aliases_loaded = true;
696a6146d50SZheng Yan 	return 0;
697a6146d50SZheng Yan }
698a6146d50SZheng Yan 
pmu_alias_terms(struct perf_pmu_alias * alias,struct list_head * terms)6995c6ccc37SArnaldo Carvalho de Melo static int pmu_alias_terms(struct perf_pmu_alias *alias,
700a6146d50SZheng Yan 			   struct list_head *terms)
701a6146d50SZheng Yan {
7027c2f8164SJiri Olsa 	struct parse_events_term *term, *cloned;
703a6146d50SZheng Yan 	LIST_HEAD(list);
704a6146d50SZheng Yan 	int ret;
705a6146d50SZheng Yan 
706a6146d50SZheng Yan 	list_for_each_entry(term, &alias->terms, list) {
7077c2f8164SJiri Olsa 		ret = parse_events_term__clone(&cloned, term);
708a6146d50SZheng Yan 		if (ret) {
709682dc24cSArnaldo Carvalho de Melo 			parse_events_terms__purge(&list);
710a6146d50SZheng Yan 			return ret;
711a6146d50SZheng Yan 		}
712c2f1ceadSAndi Kleen 		/*
713c2f1ceadSAndi Kleen 		 * Weak terms don't override command line options,
714c2f1ceadSAndi Kleen 		 * which we don't want for implicit terms in aliases.
715c2f1ceadSAndi Kleen 		 */
716c2f1ceadSAndi Kleen 		cloned->weak = true;
7177c2f8164SJiri Olsa 		list_add_tail(&cloned->list, &list);
718a6146d50SZheng Yan 	}
719a6146d50SZheng Yan 	list_splice(&list, terms);
720a6146d50SZheng Yan 	return 0;
721a6146d50SZheng Yan }
722a6146d50SZheng Yan 
72366ec1191SMark Rutland /*
72466ec1191SMark Rutland  * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64)
72566ec1191SMark Rutland  * may have a "cpus" file.
72666ec1191SMark Rutland  */
pmu_cpumask(int dirfd,const char * name,bool is_core)727d06593aaSIan Rogers static struct perf_cpu_map *pmu_cpumask(int dirfd, const char *name, bool is_core)
7287ae92e74SYan, Zheng {
729f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
7307e3fcffeSMark Rutland 	const char *templates[] = {
731d50a79cdSJames Clark 		"cpumask",
732d50a79cdSJames Clark 		"cpus",
7337e3fcffeSMark Rutland 		NULL
7347e3fcffeSMark Rutland 	};
7357e3fcffeSMark Rutland 	const char **template;
736d50a79cdSJames Clark 	char pmu_name[PATH_MAX];
737d50a79cdSJames Clark 	struct perf_pmu pmu = {.name = pmu_name};
738d50a79cdSJames Clark 	FILE *file;
7397ae92e74SYan, Zheng 
740d50a79cdSJames Clark 	strlcpy(pmu_name, name, sizeof(pmu_name));
7417e3fcffeSMark Rutland 	for (template = templates; *template; template++) {
7423a69672eSNamhyung Kim 		file = perf_pmu__open_file_at(&pmu, dirfd, *template);
743d50a79cdSJames Clark 		if (!file)
744d50a79cdSJames Clark 			continue;
745d50a79cdSJames Clark 		cpus = perf_cpu_map__read(file);
7463a69672eSNamhyung Kim 		fclose(file);
74766ec1191SMark Rutland 		if (cpus)
74866ec1191SMark Rutland 			return cpus;
7497e3fcffeSMark Rutland 	}
7507ae92e74SYan, Zheng 
751d06593aaSIan Rogers 	/* Nothing found, for core PMUs assume this means all CPUs. */
752d06593aaSIan Rogers 	return is_core ? perf_cpu_map__get(cpu_map__online()) : NULL;
75366ec1191SMark Rutland }
7547ae92e74SYan, Zheng 
pmu_is_uncore(int dirfd,const char * name)7553a69672eSNamhyung Kim static bool pmu_is_uncore(int dirfd, const char *name)
75666ec1191SMark Rutland {
7573a69672eSNamhyung Kim 	int fd;
7587ae92e74SYan, Zheng 
7593a69672eSNamhyung Kim 	fd = perf_pmu__pathname_fd(dirfd, name, "cpumask", O_PATH);
7603a69672eSNamhyung Kim 	if (fd < 0)
7613a69672eSNamhyung Kim 		return false;
7623a69672eSNamhyung Kim 
7633a69672eSNamhyung Kim 	close(fd);
7643a69672eSNamhyung Kim 	return true;
7657ae92e74SYan, Zheng }
7667ae92e74SYan, Zheng 
pmu_id(const char * name)76751d54847SJohn Garry static char *pmu_id(const char *name)
76851d54847SJohn Garry {
76951d54847SJohn Garry 	char path[PATH_MAX], *str;
77051d54847SJohn Garry 	size_t len;
77151d54847SJohn Garry 
7725f2c8efaSJames Clark 	perf_pmu__pathname_scnprintf(path, sizeof(path), name, "identifier");
77351d54847SJohn Garry 
7745f2c8efaSJames Clark 	if (filename__read_str(path, &str, &len) < 0)
77551d54847SJohn Garry 		return NULL;
77651d54847SJohn Garry 
77751d54847SJohn Garry 	str[len - 1] = 0; /* remove line feed */
77851d54847SJohn Garry 
77951d54847SJohn Garry 	return str;
78051d54847SJohn Garry }
78151d54847SJohn Garry 
7824bf7e81aSIan Rogers /**
7834bf7e81aSIan Rogers  * is_sysfs_pmu_core() - PMU CORE devices have different name other than cpu in
7844bf7e81aSIan Rogers  *         sysfs on some platforms like ARM or Intel hybrid. Looking for
7854bf7e81aSIan Rogers  *         possible the cpus file in sysfs files to identify whether this is a
7864bf7e81aSIan Rogers  *         core device.
7874bf7e81aSIan Rogers  * @name: The PMU name such as "cpu_atom".
78814b22ae0SGanapatrao Kulkarni  */
is_sysfs_pmu_core(const char * name)7894bf7e81aSIan Rogers static int is_sysfs_pmu_core(const char *name)
79014b22ae0SGanapatrao Kulkarni {
79114b22ae0SGanapatrao Kulkarni 	char path[PATH_MAX];
79214b22ae0SGanapatrao Kulkarni 
793f8ad6018SJames Clark 	if (!perf_pmu__pathname_scnprintf(path, sizeof(path), name, "cpus"))
79414b22ae0SGanapatrao Kulkarni 		return 0;
795d9664582SAndi Kleen 	return file_available(path);
79614b22ae0SGanapatrao Kulkarni }
79714b22ae0SGanapatrao Kulkarni 
perf_pmu__getcpuid(struct perf_pmu * pmu)79829be2fe0SIan Rogers char *perf_pmu__getcpuid(struct perf_pmu *pmu)
799d77ade9fSAndi Kleen {
800d77ade9fSAndi Kleen 	char *cpuid;
801d77ade9fSAndi Kleen 	static bool printed;
802d77ade9fSAndi Kleen 
803d77ade9fSAndi Kleen 	cpuid = getenv("PERF_CPUID");
804d77ade9fSAndi Kleen 	if (cpuid)
805d77ade9fSAndi Kleen 		cpuid = strdup(cpuid);
806d77ade9fSAndi Kleen 	if (!cpuid)
80754e32dc0SGanapatrao Kulkarni 		cpuid = get_cpuid_str(pmu);
808d77ade9fSAndi Kleen 	if (!cpuid)
809d77ade9fSAndi Kleen 		return NULL;
810d77ade9fSAndi Kleen 
811d77ade9fSAndi Kleen 	if (!printed) {
812d77ade9fSAndi Kleen 		pr_debug("Using CPUID %s\n", cpuid);
813d77ade9fSAndi Kleen 		printed = true;
814d77ade9fSAndi Kleen 	}
815d77ade9fSAndi Kleen 	return cpuid;
816d77ade9fSAndi Kleen }
817d77ade9fSAndi Kleen 
pmu_events_table__find(void)8181ba3752aSIan Rogers __weak const struct pmu_events_table *pmu_events_table__find(void)
819e126bef5SJohn Garry {
82096d2a746SIan Rogers 	return perf_pmu__find_events_table(NULL);
821e126bef5SJohn Garry }
822e126bef5SJohn Garry 
pmu_metrics_table__find(void)823f8ea2c15SIan Rogers __weak const struct pmu_metrics_table *pmu_metrics_table__find(void)
824f8ea2c15SIan Rogers {
825f8ea2c15SIan Rogers 	return perf_pmu__find_metrics_table(NULL);
826f8ea2c15SIan Rogers }
827f8ea2c15SIan Rogers 
828240e6fd0SIan Rogers /**
829240e6fd0SIan Rogers  * perf_pmu__match_ignoring_suffix - Does the pmu_name match tok ignoring any
830240e6fd0SIan Rogers  *                                   trailing suffix? The Suffix must be in form
831240e6fd0SIan Rogers  *                                   tok_{digits}, or tok{digits}.
832240e6fd0SIan Rogers  * @pmu_name: The pmu_name with possible suffix.
833240e6fd0SIan Rogers  * @tok: The possible match to pmu_name without suffix.
834c07d5c92SJohn Garry  */
perf_pmu__match_ignoring_suffix(const char * pmu_name,const char * tok)835240e6fd0SIan Rogers static bool perf_pmu__match_ignoring_suffix(const char *pmu_name, const char *tok)
836c47a5599SJin Yao {
837c07d5c92SJohn Garry 	const char *p;
838c47a5599SJin Yao 
839c47a5599SJin Yao 	if (strncmp(pmu_name, tok, strlen(tok)))
840c47a5599SJin Yao 		return false;
841c47a5599SJin Yao 
842c47a5599SJin Yao 	p = pmu_name + strlen(tok);
843c47a5599SJin Yao 	if (*p == 0)
844c47a5599SJin Yao 		return true;
845c47a5599SJin Yao 
846c07d5c92SJohn Garry 	if (*p == '_')
847c47a5599SJin Yao 		++p;
848c07d5c92SJohn Garry 
849c07d5c92SJohn Garry 	/* Ensure we end in a number */
850c07d5c92SJohn Garry 	while (1) {
851c07d5c92SJohn Garry 		if (!isdigit(*p))
852c47a5599SJin Yao 			return false;
853c07d5c92SJohn Garry 		if (*(++p) == 0)
854c07d5c92SJohn Garry 			break;
855c07d5c92SJohn Garry 	}
856c47a5599SJin Yao 
857c47a5599SJin Yao 	return true;
858c47a5599SJin Yao }
859c47a5599SJin Yao 
860240e6fd0SIan Rogers /**
861240e6fd0SIan Rogers  * pmu_uncore_alias_match - does name match the PMU name?
862240e6fd0SIan Rogers  * @pmu_name: the json struct pmu_event name. This may lack a suffix (which
863240e6fd0SIan Rogers  *            matches) or be of the form "socket,pmuname" which will match
864240e6fd0SIan Rogers  *            "socketX_pmunameY".
865240e6fd0SIan Rogers  * @name: a real full PMU name as from sysfs.
866240e6fd0SIan Rogers  */
pmu_uncore_alias_match(const char * pmu_name,const char * name)867240e6fd0SIan Rogers static bool pmu_uncore_alias_match(const char *pmu_name, const char *name)
868730670b1SJohn Garry {
869730670b1SJohn Garry 	char *tmp = NULL, *tok, *str;
870730670b1SJohn Garry 	bool res;
871730670b1SJohn Garry 
872240e6fd0SIan Rogers 	if (strchr(pmu_name, ',') == NULL)
873240e6fd0SIan Rogers 		return perf_pmu__match_ignoring_suffix(name, pmu_name);
874240e6fd0SIan Rogers 
875730670b1SJohn Garry 	str = strdup(pmu_name);
876730670b1SJohn Garry 	if (!str)
877730670b1SJohn Garry 		return false;
878730670b1SJohn Garry 
879730670b1SJohn Garry 	/*
880730670b1SJohn Garry 	 * uncore alias may be from different PMU with common prefix
881730670b1SJohn Garry 	 */
882730670b1SJohn Garry 	tok = strtok_r(str, ",", &tmp);
883730670b1SJohn Garry 	if (strncmp(pmu_name, tok, strlen(tok))) {
884730670b1SJohn Garry 		res = false;
885730670b1SJohn Garry 		goto out;
886730670b1SJohn Garry 	}
887730670b1SJohn Garry 
888730670b1SJohn Garry 	/*
889730670b1SJohn Garry 	 * Match more complex aliases where the alias name is a comma-delimited
890730670b1SJohn Garry 	 * list of tokens, orderly contained in the matching PMU name.
891730670b1SJohn Garry 	 *
892730670b1SJohn Garry 	 * Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we
893730670b1SJohn Garry 	 *	    match "socket" in "socketX_pmunameY" and then "pmuname" in
894730670b1SJohn Garry 	 *	    "pmunameY".
895730670b1SJohn Garry 	 */
896c07d5c92SJohn Garry 	while (1) {
897c07d5c92SJohn Garry 		char *next_tok = strtok_r(NULL, ",", &tmp);
898c07d5c92SJohn Garry 
899730670b1SJohn Garry 		name = strstr(name, tok);
900c07d5c92SJohn Garry 		if (!name ||
901240e6fd0SIan Rogers 		    (!next_tok && !perf_pmu__match_ignoring_suffix(name, tok))) {
902730670b1SJohn Garry 			res = false;
903730670b1SJohn Garry 			goto out;
904730670b1SJohn Garry 		}
905c07d5c92SJohn Garry 		if (!next_tok)
906c07d5c92SJohn Garry 			break;
907c07d5c92SJohn Garry 		tok = next_tok;
908c07d5c92SJohn Garry 		name += strlen(tok);
909730670b1SJohn Garry 	}
910730670b1SJohn Garry 
911730670b1SJohn Garry 	res = true;
912730670b1SJohn Garry out:
913730670b1SJohn Garry 	free(str);
914730670b1SJohn Garry 	return res;
915730670b1SJohn Garry }
916730670b1SJohn Garry 
pmu_uncore_identifier_match(const char * compat,const char * id)917c5aaeae2SJing Zhang bool pmu_uncore_identifier_match(const char *compat, const char *id)
918c5aaeae2SJing Zhang {
919c5aaeae2SJing Zhang 	regex_t re;
920c5aaeae2SJing Zhang 	regmatch_t pmatch[1];
921c5aaeae2SJing Zhang 	int match;
922c5aaeae2SJing Zhang 
923c5aaeae2SJing Zhang 	if (regcomp(&re, compat, REG_EXTENDED) != 0) {
924c5aaeae2SJing Zhang 		/* Warn unable to generate match particular string. */
925c5aaeae2SJing Zhang 		pr_info("Invalid regular expression %s\n", compat);
926c5aaeae2SJing Zhang 		return false;
927c5aaeae2SJing Zhang 	}
928c5aaeae2SJing Zhang 
929c5aaeae2SJing Zhang 	match = !regexec(&re, id, 1, pmatch, 0);
930c5aaeae2SJing Zhang 	if (match) {
931c5aaeae2SJing Zhang 		/* Ensure a full match. */
932c5aaeae2SJing Zhang 		match = pmatch[0].rm_so == 0 && (size_t)pmatch[0].rm_eo == strlen(id);
933c5aaeae2SJing Zhang 	}
934c5aaeae2SJing Zhang 	regfree(&re);
935c5aaeae2SJing Zhang 
936c5aaeae2SJing Zhang 	return match;
937c5aaeae2SJing Zhang }
938c5aaeae2SJing Zhang 
pmu_add_cpu_aliases_map_callback(const struct pmu_event * pe,const struct pmu_events_table * table __maybe_unused,void * vdata)939660842e4SIan Rogers static int pmu_add_cpu_aliases_map_callback(const struct pmu_event *pe,
9401ba3752aSIan Rogers 					const struct pmu_events_table *table __maybe_unused,
941660842e4SIan Rogers 					void *vdata)
942660842e4SIan Rogers {
943f26d22f1SIan Rogers 	struct perf_pmu *pmu = vdata;
944660842e4SIan Rogers 
9455af155f9SIan Rogers 	perf_pmu__new_alias(pmu, pe->name, pe->desc, pe->event, /*val_fd=*/ NULL,
9465af155f9SIan Rogers 			    pe, EVENT_SRC_CPU_JSON);
947660842e4SIan Rogers 	return 0;
948660842e4SIan Rogers }
949660842e4SIan Rogers 
950933f82ffSSukadev Bhattiprolu /*
951d685819bSIan Rogers  * From the pmu_events_table, find the events that correspond to the given
952d685819bSIan Rogers  * PMU and add them to the list 'head'.
953933f82ffSSukadev Bhattiprolu  */
pmu_add_cpu_aliases_table(struct perf_pmu * pmu,const struct pmu_events_table * table)954838a8c5fSIan Rogers void pmu_add_cpu_aliases_table(struct perf_pmu *pmu, const struct pmu_events_table *table)
955933f82ffSSukadev Bhattiprolu {
956f26d22f1SIan Rogers 	pmu_events_table__for_each_event(table, pmu, pmu_add_cpu_aliases_map_callback, pmu);
957933f82ffSSukadev Bhattiprolu }
958933f82ffSSukadev Bhattiprolu 
pmu_add_cpu_aliases(struct perf_pmu * pmu)959838a8c5fSIan Rogers static void pmu_add_cpu_aliases(struct perf_pmu *pmu)
960e45ad701SJohn Garry {
9617c52f10cSIan Rogers 	if (!pmu->events_table)
962e45ad701SJohn Garry 		return;
963e45ad701SJohn Garry 
964e6ff1eedSIan Rogers 	if (pmu->cpu_aliases_added)
965e6ff1eedSIan Rogers 		return;
966e6ff1eedSIan Rogers 
9677c52f10cSIan Rogers 	pmu_add_cpu_aliases_table(pmu, pmu->events_table);
968e6ff1eedSIan Rogers 	pmu->cpu_aliases_added = true;
969e45ad701SJohn Garry }
970e45ad701SJohn Garry 
pmu_add_sys_aliases_iter_fn(const struct pmu_event * pe,const struct pmu_events_table * table __maybe_unused,void * vdata)97129be2fe0SIan Rogers static int pmu_add_sys_aliases_iter_fn(const struct pmu_event *pe,
9721ba3752aSIan Rogers 				       const struct pmu_events_table *table __maybe_unused,
973f26d22f1SIan Rogers 				       void *vdata)
9744513c719SJohn Garry {
975f26d22f1SIan Rogers 	struct perf_pmu *pmu = vdata;
9764513c719SJohn Garry 
9774513c719SJohn Garry 	if (!pe->compat || !pe->pmu)
9784513c719SJohn Garry 		return 0;
9794513c719SJohn Garry 
980c5aaeae2SJing Zhang 	if (pmu_uncore_alias_match(pe->pmu, pmu->name) &&
981c5aaeae2SJing Zhang 	    pmu_uncore_identifier_match(pe->compat, pmu->id)) {
9827b723dbbSIan Rogers 		perf_pmu__new_alias(pmu,
983edb217ffSIan Rogers 				pe->name,
984edb217ffSIan Rogers 				pe->desc,
985edb217ffSIan Rogers 				pe->event,
986edb217ffSIan Rogers 				/*val_fd=*/ NULL,
9875af155f9SIan Rogers 				pe,
9885af155f9SIan Rogers 				EVENT_SRC_SYS_JSON);
9894513c719SJohn Garry 	}
9904513c719SJohn Garry 
9914513c719SJohn Garry 	return 0;
9924513c719SJohn Garry }
9934513c719SJohn Garry 
pmu_add_sys_aliases(struct perf_pmu * pmu)994838a8c5fSIan Rogers void pmu_add_sys_aliases(struct perf_pmu *pmu)
9954513c719SJohn Garry {
9964513c719SJohn Garry 	if (!pmu->id)
9974513c719SJohn Garry 		return;
9984513c719SJohn Garry 
999f26d22f1SIan Rogers 	pmu_for_each_sys_event(pmu_add_sys_aliases_iter_fn, pmu);
10004513c719SJohn Garry }
10014513c719SJohn Garry 
1002c5de47f2SSukadev Bhattiprolu struct perf_event_attr * __weak
perf_pmu__get_default_config(struct perf_pmu * pmu __maybe_unused)1003dc0a6202SAdrian Hunter perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
1004dc0a6202SAdrian Hunter {
1005dc0a6202SAdrian Hunter 	return NULL;
1006dc0a6202SAdrian Hunter }
1007dc0a6202SAdrian Hunter 
1008c091ee90SIan Rogers const char * __weak
pmu_find_real_name(const char * name)100913d60ba0SKan Liang pmu_find_real_name(const char *name)
101013d60ba0SKan Liang {
1011c091ee90SIan Rogers 	return name;
101213d60ba0SKan Liang }
101313d60ba0SKan Liang 
1014c091ee90SIan Rogers const char * __weak
pmu_find_alias_name(const char * name __maybe_unused)101513d60ba0SKan Liang pmu_find_alias_name(const char *name __maybe_unused)
101613d60ba0SKan Liang {
101713d60ba0SKan Liang 	return NULL;
101813d60ba0SKan Liang }
101913d60ba0SKan Liang 
pmu_max_precise(int dirfd,struct perf_pmu * pmu)10203a69672eSNamhyung Kim static int pmu_max_precise(int dirfd, struct perf_pmu *pmu)
102190a86bdeSJiri Olsa {
102290a86bdeSJiri Olsa 	int max_precise = -1;
102390a86bdeSJiri Olsa 
10243a69672eSNamhyung Kim 	perf_pmu__scan_file_at(pmu, dirfd, "caps/max_precise", "%d", &max_precise);
102590a86bdeSJiri Olsa 	return max_precise;
102690a86bdeSJiri Olsa }
102790a86bdeSJiri Olsa 
perf_pmu__lookup(struct list_head * pmus,int dirfd,const char * lookup_name)10281eaf496eSIan Rogers struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char *lookup_name)
1029cd82a32eSJiri Olsa {
1030cd82a32eSJiri Olsa 	struct perf_pmu *pmu;
1031cd82a32eSJiri Olsa 	__u32 type;
1032c091ee90SIan Rogers 	const char *name = pmu_find_real_name(lookup_name);
1033c091ee90SIan Rogers 	const char *alias_name;
103449afa7f6SJin Yao 
1035cd82a32eSJiri Olsa 	pmu = zalloc(sizeof(*pmu));
1036cd82a32eSJiri Olsa 	if (!pmu)
1037cd82a32eSJiri Olsa 		return NULL;
1038cd82a32eSJiri Olsa 
1039f63a536fSIan Rogers 	pmu->name = strdup(name);
1040f63a536fSIan Rogers 	if (!pmu->name)
1041f63a536fSIan Rogers 		goto err;
104288ed9184SIan Rogers 
104388ed9184SIan Rogers 	/*
104488ed9184SIan Rogers 	 * Read type early to fail fast if a lookup name isn't a PMU. Ensure
104588ed9184SIan Rogers 	 * that type value is successfully assigned (return 1).
104688ed9184SIan Rogers 	 */
104788ed9184SIan Rogers 	if (perf_pmu__scan_file_at(pmu, dirfd, "type", "%u", &type) != 1)
104888ed9184SIan Rogers 		goto err;
104988ed9184SIan Rogers 
105088ed9184SIan Rogers 	INIT_LIST_HEAD(&pmu->format);
105188ed9184SIan Rogers 	INIT_LIST_HEAD(&pmu->aliases);
105288ed9184SIan Rogers 	INIT_LIST_HEAD(&pmu->caps);
105388ed9184SIan Rogers 
1054838a8c5fSIan Rogers 	/*
1055838a8c5fSIan Rogers 	 * The pmu data we store & need consists of the pmu
1056838a8c5fSIan Rogers 	 * type value and format definitions. Load both right
1057838a8c5fSIan Rogers 	 * now.
1058838a8c5fSIan Rogers 	 */
1059dfde5becSChristophe JAILLET 	if (pmu_format(pmu, dirfd, name))
1060dfde5becSChristophe JAILLET 		goto err;
1061dfde5becSChristophe JAILLET 
1062d06593aaSIan Rogers 	pmu->is_core = is_pmu_core(name);
1063d06593aaSIan Rogers 	pmu->cpus = pmu_cpumask(dirfd, name, pmu->is_core);
106413d60ba0SKan Liang 
106513d60ba0SKan Liang 	alias_name = pmu_find_alias_name(name);
106613d60ba0SKan Liang 	if (alias_name) {
106713d60ba0SKan Liang 		pmu->alias_name = strdup(alias_name);
106813d60ba0SKan Liang 		if (!pmu->alias_name)
106913d60ba0SKan Liang 			goto err;
107013d60ba0SKan Liang 	}
107113d60ba0SKan Liang 
107254e32dc0SGanapatrao Kulkarni 	pmu->type = type;
10733a69672eSNamhyung Kim 	pmu->is_uncore = pmu_is_uncore(dirfd, name);
107451d54847SJohn Garry 	if (pmu->is_uncore)
107551d54847SJohn Garry 		pmu->id = pmu_id(name);
10763a69672eSNamhyung Kim 	pmu->max_precise = pmu_max_precise(dirfd, pmu);
10777c52f10cSIan Rogers 	pmu->events_table = perf_pmu__find_events_table(pmu);
10785af155f9SIan Rogers 	/*
10795af155f9SIan Rogers 	 * Load the sys json events/aliases when loading the PMU as each event
10805af155f9SIan Rogers 	 * may have a different compat regular expression. We therefore can't
10815af155f9SIan Rogers 	 * know the number of sys json events/aliases without computing the
10825af155f9SIan Rogers 	 * regular expressions for them all.
10835af155f9SIan Rogers 	 */
1084838a8c5fSIan Rogers 	pmu_add_sys_aliases(pmu);
10851eaf496eSIan Rogers 	list_add_tail(&pmu->list, pmus);
1086dc0a6202SAdrian Hunter 
1087dc0a6202SAdrian Hunter 	pmu->default_config = perf_pmu__get_default_config(pmu);
1088dc0a6202SAdrian Hunter 
1089cd82a32eSJiri Olsa 	return pmu;
109013d60ba0SKan Liang err:
1091efe98a7aSArnaldo Carvalho de Melo 	zfree(&pmu->name);
109213d60ba0SKan Liang 	free(pmu);
109313d60ba0SKan Liang 	return NULL;
1094cd82a32eSJiri Olsa }
1095cd82a32eSJiri Olsa 
1096628eaa4eSIan Rogers /* Creates the PMU when sysfs scanning fails. */
perf_pmu__create_placeholder_core_pmu(struct list_head * core_pmus)1097628eaa4eSIan Rogers struct perf_pmu *perf_pmu__create_placeholder_core_pmu(struct list_head *core_pmus)
1098628eaa4eSIan Rogers {
1099628eaa4eSIan Rogers 	struct perf_pmu *pmu = zalloc(sizeof(*pmu));
1100628eaa4eSIan Rogers 
1101628eaa4eSIan Rogers 	if (!pmu)
1102628eaa4eSIan Rogers 		return NULL;
1103628eaa4eSIan Rogers 
1104628eaa4eSIan Rogers 	pmu->name = strdup("cpu");
1105628eaa4eSIan Rogers 	if (!pmu->name) {
1106628eaa4eSIan Rogers 		free(pmu);
1107628eaa4eSIan Rogers 		return NULL;
1108628eaa4eSIan Rogers 	}
1109628eaa4eSIan Rogers 
1110628eaa4eSIan Rogers 	pmu->is_core = true;
1111628eaa4eSIan Rogers 	pmu->type = PERF_TYPE_RAW;
1112628eaa4eSIan Rogers 	pmu->cpus = cpu_map__online();
1113628eaa4eSIan Rogers 
1114628eaa4eSIan Rogers 	INIT_LIST_HEAD(&pmu->format);
1115628eaa4eSIan Rogers 	INIT_LIST_HEAD(&pmu->aliases);
1116628eaa4eSIan Rogers 	INIT_LIST_HEAD(&pmu->caps);
1117628eaa4eSIan Rogers 	list_add_tail(&pmu->list, core_pmus);
1118628eaa4eSIan Rogers 	return pmu;
1119628eaa4eSIan Rogers }
1120628eaa4eSIan Rogers 
perf_pmu__warn_invalid_formats(struct perf_pmu * pmu)1121e552b7beSRob Herring void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu)
1122e552b7beSRob Herring {
1123e552b7beSRob Herring 	struct perf_pmu_format *format;
1124e552b7beSRob Herring 
112568c25043SIan Rogers 	if (pmu->formats_checked)
112668c25043SIan Rogers 		return;
112768c25043SIan Rogers 
112868c25043SIan Rogers 	pmu->formats_checked = true;
112968c25043SIan Rogers 
1130e552b7beSRob Herring 	/* fake pmu doesn't have format list */
1131e552b7beSRob Herring 	if (pmu == &perf_pmu__fake)
1132e552b7beSRob Herring 		return;
1133e552b7beSRob Herring 
113450402641SIan Rogers 	list_for_each_entry(format, &pmu->format, list) {
113550402641SIan Rogers 		perf_pmu_format__load(pmu, format);
1136e552b7beSRob Herring 		if (format->value >= PERF_PMU_FORMAT_VALUE_CONFIG_END) {
1137e552b7beSRob Herring 			pr_warning("WARNING: '%s' format '%s' requires 'perf_event_attr::config%d'"
1138e552b7beSRob Herring 				   "which is not supported by this version of perf!\n",
1139e552b7beSRob Herring 				   pmu->name, format->name, format->value);
1140e552b7beSRob Herring 			return;
1141e552b7beSRob Herring 		}
1142e552b7beSRob Herring 	}
114350402641SIan Rogers }
1144e552b7beSRob Herring 
evsel__is_aux_event(const struct evsel * evsel)1145c6d616feSIan Rogers bool evsel__is_aux_event(const struct evsel *evsel)
1146e12ee9f7SAdrian Hunter {
1147e76026bdSArnaldo Carvalho de Melo 	struct perf_pmu *pmu = evsel__find_pmu(evsel);
1148e12ee9f7SAdrian Hunter 
1149e12ee9f7SAdrian Hunter 	return pmu && pmu->auxtrace;
1150e12ee9f7SAdrian Hunter }
1151e12ee9f7SAdrian Hunter 
11526593f019SJames Clark /*
11536593f019SJames Clark  * Set @config_name to @val as long as the user hasn't already set or cleared it
11546593f019SJames Clark  * by passing a config term on the command line.
11556593f019SJames Clark  *
11566593f019SJames Clark  * @val is the value to put into the bits specified by @config_name rather than
11576593f019SJames Clark  * the bit pattern. It is shifted into position by this function, so to set
11586593f019SJames Clark  * something to true, pass 1 for val rather than a pre shifted value.
11596593f019SJames Clark  */
11606593f019SJames Clark #define field_prep(_mask, _val) (((_val) << (ffsll(_mask) - 1)) & (_mask))
evsel__set_config_if_unset(struct perf_pmu * pmu,struct evsel * evsel,const char * config_name,u64 val)11616593f019SJames Clark void evsel__set_config_if_unset(struct perf_pmu *pmu, struct evsel *evsel,
11626593f019SJames Clark 				const char *config_name, u64 val)
11636593f019SJames Clark {
11646593f019SJames Clark 	u64 user_bits = 0, bits;
11656593f019SJames Clark 	struct evsel_config_term *term = evsel__get_config_term(evsel, CFG_CHG);
11666593f019SJames Clark 
11676593f019SJames Clark 	if (term)
11686593f019SJames Clark 		user_bits = term->val.cfg_chg;
11696593f019SJames Clark 
1170da6a5afdSIan Rogers 	bits = perf_pmu__format_bits(pmu, config_name);
11716593f019SJames Clark 
11726593f019SJames Clark 	/* Do nothing if the user changed the value */
11736593f019SJames Clark 	if (bits & user_bits)
11746593f019SJames Clark 		return;
11756593f019SJames Clark 
11766593f019SJames Clark 	/* Otherwise replace it */
11776593f019SJames Clark 	evsel->core.attr.config &= ~bits;
11786593f019SJames Clark 	evsel->core.attr.config |= field_prep(bits, val);
11796593f019SJames Clark }
11806593f019SJames Clark 
11815c6ccc37SArnaldo Carvalho de Melo static struct perf_pmu_format *
pmu_find_format(struct list_head * formats,const char * name)118209ff6071SAdrian Hunter pmu_find_format(struct list_head *formats, const char *name)
1183cd82a32eSJiri Olsa {
11845c6ccc37SArnaldo Carvalho de Melo 	struct perf_pmu_format *format;
1185cd82a32eSJiri Olsa 
1186cd82a32eSJiri Olsa 	list_for_each_entry(format, formats, list)
1187cd82a32eSJiri Olsa 		if (!strcmp(format->name, name))
1188cd82a32eSJiri Olsa 			return format;
1189cd82a32eSJiri Olsa 
1190cd82a32eSJiri Olsa 	return NULL;
1191cd82a32eSJiri Olsa }
1192cd82a32eSJiri Olsa 
perf_pmu__format_bits(struct perf_pmu * pmu,const char * name)1193da6a5afdSIan Rogers __u64 perf_pmu__format_bits(struct perf_pmu *pmu, const char *name)
119409ff6071SAdrian Hunter {
1195da6a5afdSIan Rogers 	struct perf_pmu_format *format = pmu_find_format(&pmu->format, name);
119609ff6071SAdrian Hunter 	__u64 bits = 0;
119709ff6071SAdrian Hunter 	int fbit;
119809ff6071SAdrian Hunter 
119909ff6071SAdrian Hunter 	if (!format)
120009ff6071SAdrian Hunter 		return 0;
120109ff6071SAdrian Hunter 
120209ff6071SAdrian Hunter 	for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
120309ff6071SAdrian Hunter 		bits |= 1ULL << fbit;
120409ff6071SAdrian Hunter 
120509ff6071SAdrian Hunter 	return bits;
120609ff6071SAdrian Hunter }
120709ff6071SAdrian Hunter 
perf_pmu__format_type(struct perf_pmu * pmu,const char * name)12087eb54733SIan Rogers int perf_pmu__format_type(struct perf_pmu *pmu, const char *name)
1209a1ac7de6SAdrian Hunter {
12107eb54733SIan Rogers 	struct perf_pmu_format *format = pmu_find_format(&pmu->format, name);
1211a1ac7de6SAdrian Hunter 
1212a1ac7de6SAdrian Hunter 	if (!format)
1213a1ac7de6SAdrian Hunter 		return -1;
1214a1ac7de6SAdrian Hunter 
121550402641SIan Rogers 	perf_pmu_format__load(pmu, format);
1216a1ac7de6SAdrian Hunter 	return format->value;
1217a1ac7de6SAdrian Hunter }
1218a1ac7de6SAdrian Hunter 
1219cd82a32eSJiri Olsa /*
1220dc0a6202SAdrian Hunter  * Sets value based on the format definition (format parameter)
12214d39c89fSIngo Molnar  * and unformatted value (value parameter).
1222cd82a32eSJiri Olsa  */
pmu_format_value(unsigned long * format,__u64 value,__u64 * v,bool zero)1223dc0a6202SAdrian Hunter static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
1224dc0a6202SAdrian Hunter 			     bool zero)
1225cd82a32eSJiri Olsa {
1226cd82a32eSJiri Olsa 	unsigned long fbit, vbit;
1227cd82a32eSJiri Olsa 
1228cd82a32eSJiri Olsa 	for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
1229cd82a32eSJiri Olsa 
1230cd82a32eSJiri Olsa 		if (!test_bit(fbit, format))
1231cd82a32eSJiri Olsa 			continue;
1232cd82a32eSJiri Olsa 
1233dc0a6202SAdrian Hunter 		if (value & (1llu << vbit++))
1234dc0a6202SAdrian Hunter 			*v |= (1llu << fbit);
1235dc0a6202SAdrian Hunter 		else if (zero)
1236dc0a6202SAdrian Hunter 			*v &= ~(1llu << fbit);
1237cd82a32eSJiri Olsa 	}
1238cd82a32eSJiri Olsa }
1239cd82a32eSJiri Olsa 
pmu_format_max_value(const unsigned long * format)12400efe6b67SAdrian Hunter static __u64 pmu_format_max_value(const unsigned long *format)
12410efe6b67SAdrian Hunter {
12421b9caa10SJiri Olsa 	int w;
12430efe6b67SAdrian Hunter 
12441b9caa10SJiri Olsa 	w = bitmap_weight(format, PERF_PMU_FORMAT_BITS);
12451b9caa10SJiri Olsa 	if (!w)
12461b9caa10SJiri Olsa 		return 0;
12471b9caa10SJiri Olsa 	if (w < 64)
12481b9caa10SJiri Olsa 		return (1ULL << w) - 1;
12491b9caa10SJiri Olsa 	return -1;
12500efe6b67SAdrian Hunter }
12510efe6b67SAdrian Hunter 
1252cd82a32eSJiri Olsa /*
1253688d4dfcSCody P Schafer  * Term is a string term, and might be a param-term. Try to look up it's value
1254688d4dfcSCody P Schafer  * in the remaining terms.
1255688d4dfcSCody P Schafer  * - We have a term like "base-or-format-term=param-term",
1256688d4dfcSCody P Schafer  * - We need to find the value supplied for "param-term" (with param-term named
1257688d4dfcSCody P Schafer  *   in a config string) later on in the term list.
1258688d4dfcSCody P Schafer  */
pmu_resolve_param_term(struct parse_events_term * term,struct list_head * head_terms,__u64 * value)1259688d4dfcSCody P Schafer static int pmu_resolve_param_term(struct parse_events_term *term,
1260688d4dfcSCody P Schafer 				  struct list_head *head_terms,
1261688d4dfcSCody P Schafer 				  __u64 *value)
1262688d4dfcSCody P Schafer {
1263688d4dfcSCody P Schafer 	struct parse_events_term *t;
1264688d4dfcSCody P Schafer 
1265688d4dfcSCody P Schafer 	list_for_each_entry(t, head_terms, list) {
12662a3d252dSIan Rogers 		if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM &&
12672a3d252dSIan Rogers 		    t->config && !strcmp(t->config, term->config)) {
1268688d4dfcSCody P Schafer 			t->used = true;
1269688d4dfcSCody P Schafer 			*value = t->val.num;
1270688d4dfcSCody P Schafer 			return 0;
1271688d4dfcSCody P Schafer 		}
1272688d4dfcSCody P Schafer 	}
1273688d4dfcSCody P Schafer 
1274bb963e16SNamhyung Kim 	if (verbose > 0)
1275688d4dfcSCody P Schafer 		printf("Required parameter '%s' not specified\n", term->config);
1276688d4dfcSCody P Schafer 
1277688d4dfcSCody P Schafer 	return -1;
1278688d4dfcSCody P Schafer }
1279688d4dfcSCody P Schafer 
pmu_formats_string(struct list_head * formats)1280ffeb883eSHe Kuang static char *pmu_formats_string(struct list_head *formats)
1281e64b020bSJiri Olsa {
1282e64b020bSJiri Olsa 	struct perf_pmu_format *format;
128311db4e29SMasami Hiramatsu 	char *str = NULL;
128411db4e29SMasami Hiramatsu 	struct strbuf buf = STRBUF_INIT;
1285f1417ceaSXin Gao 	unsigned int i = 0;
1286e64b020bSJiri Olsa 
1287ffeb883eSHe Kuang 	if (!formats)
1288e64b020bSJiri Olsa 		return NULL;
1289e64b020bSJiri Olsa 
1290e64b020bSJiri Olsa 	/* sysfs exported terms */
1291ffeb883eSHe Kuang 	list_for_each_entry(format, formats, list)
129211db4e29SMasami Hiramatsu 		if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
129311db4e29SMasami Hiramatsu 			goto error;
1294e64b020bSJiri Olsa 
1295ffeb883eSHe Kuang 	str = strbuf_detach(&buf, NULL);
129611db4e29SMasami Hiramatsu error:
1297ffeb883eSHe Kuang 	strbuf_release(&buf);
1298e64b020bSJiri Olsa 
1299e64b020bSJiri Olsa 	return str;
1300e64b020bSJiri Olsa }
1301e64b020bSJiri Olsa 
1302688d4dfcSCody P Schafer /*
1303cd82a32eSJiri Olsa  * Setup one of config[12] attr members based on the
130488aca8d9SCody P Schafer  * user input data - term parameter.
1305cd82a32eSJiri Olsa  */
pmu_config_term(struct perf_pmu * pmu,struct perf_event_attr * attr,struct parse_events_term * term,struct list_head * head_terms,bool zero,struct parse_events_error * err)1306804fee5dSIan Rogers static int pmu_config_term(struct perf_pmu *pmu,
1307cd82a32eSJiri Olsa 			   struct perf_event_attr *attr,
1308dc0a6202SAdrian Hunter 			   struct parse_events_term *term,
1309688d4dfcSCody P Schafer 			   struct list_head *head_terms,
1310e64b020bSJiri Olsa 			   bool zero, struct parse_events_error *err)
1311cd82a32eSJiri Olsa {
13125c6ccc37SArnaldo Carvalho de Melo 	struct perf_pmu_format *format;
1313cd82a32eSJiri Olsa 	__u64 *vp;
13140efe6b67SAdrian Hunter 	__u64 val, max_val;
1315cd82a32eSJiri Olsa 
1316cd82a32eSJiri Olsa 	/*
1317688d4dfcSCody P Schafer 	 * If this is a parameter we've already used for parameterized-eval,
1318688d4dfcSCody P Schafer 	 * skip it in normal eval.
1319688d4dfcSCody P Schafer 	 */
1320688d4dfcSCody P Schafer 	if (term->used)
1321688d4dfcSCody P Schafer 		return 0;
1322688d4dfcSCody P Schafer 
1323688d4dfcSCody P Schafer 	/*
1324cd82a32eSJiri Olsa 	 * Hardcoded terms should be already in, so nothing
1325cd82a32eSJiri Olsa 	 * to be done for them.
1326cd82a32eSJiri Olsa 	 */
1327cd82a32eSJiri Olsa 	if (parse_events__is_hardcoded_term(term))
1328cd82a32eSJiri Olsa 		return 0;
1329cd82a32eSJiri Olsa 
1330804fee5dSIan Rogers 	format = pmu_find_format(&pmu->format, term->config);
1331688d4dfcSCody P Schafer 	if (!format) {
1332804fee5dSIan Rogers 		char *pmu_term = pmu_formats_string(&pmu->format);
13334ac22b48SIan Rogers 		char *unknown_term;
13344ac22b48SIan Rogers 		char *help_msg;
1335ffeb883eSHe Kuang 
13364ac22b48SIan Rogers 		if (asprintf(&unknown_term,
13374ac22b48SIan Rogers 				"unknown term '%s' for pmu '%s'",
1338804fee5dSIan Rogers 				term->config, pmu->name) < 0)
13394ac22b48SIan Rogers 			unknown_term = NULL;
13404ac22b48SIan Rogers 		help_msg = parse_events_formats_error_string(pmu_term);
13414ac22b48SIan Rogers 		if (err) {
13426c191289SIan Rogers 			parse_events_error__handle(err, term->err_term,
13434ac22b48SIan Rogers 						   unknown_term,
13444ac22b48SIan Rogers 						   help_msg);
13454ac22b48SIan Rogers 		} else {
13464ac22b48SIan Rogers 			pr_debug("%s (%s)\n", unknown_term, help_msg);
13474ac22b48SIan Rogers 			free(unknown_term);
1348e64b020bSJiri Olsa 		}
13494ac22b48SIan Rogers 		free(pmu_term);
1350cd82a32eSJiri Olsa 		return -EINVAL;
1351688d4dfcSCody P Schafer 	}
135250402641SIan Rogers 	perf_pmu_format__load(pmu, format);
1353cd82a32eSJiri Olsa 	switch (format->value) {
1354cd82a32eSJiri Olsa 	case PERF_PMU_FORMAT_VALUE_CONFIG:
1355cd82a32eSJiri Olsa 		vp = &attr->config;
1356cd82a32eSJiri Olsa 		break;
1357cd82a32eSJiri Olsa 	case PERF_PMU_FORMAT_VALUE_CONFIG1:
1358cd82a32eSJiri Olsa 		vp = &attr->config1;
1359cd82a32eSJiri Olsa 		break;
1360cd82a32eSJiri Olsa 	case PERF_PMU_FORMAT_VALUE_CONFIG2:
1361cd82a32eSJiri Olsa 		vp = &attr->config2;
1362cd82a32eSJiri Olsa 		break;
1363204e7c49SRob Herring 	case PERF_PMU_FORMAT_VALUE_CONFIG3:
1364204e7c49SRob Herring 		vp = &attr->config3;
1365204e7c49SRob Herring 		break;
1366cd82a32eSJiri Olsa 	default:
1367cd82a32eSJiri Olsa 		return -EINVAL;
1368cd82a32eSJiri Olsa 	}
1369cd82a32eSJiri Olsa 
137016fa7e82SJiri Olsa 	/*
1371688d4dfcSCody P Schafer 	 * Either directly use a numeric term, or try to translate string terms
1372688d4dfcSCody P Schafer 	 * using event parameters.
137316fa7e82SJiri Olsa 	 */
137499e7138eSJiri Olsa 	if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
137599e7138eSJiri Olsa 		if (term->no_value &&
137699e7138eSJiri Olsa 		    bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
137799e7138eSJiri Olsa 			if (err) {
13786c191289SIan Rogers 				parse_events_error__handle(err, term->err_val,
1379448d732cSIan Rogers 					   strdup("no value assigned for term"),
1380448d732cSIan Rogers 					   NULL);
138199e7138eSJiri Olsa 			}
138299e7138eSJiri Olsa 			return -EINVAL;
138399e7138eSJiri Olsa 		}
138499e7138eSJiri Olsa 
1385688d4dfcSCody P Schafer 		val = term->val.num;
138699e7138eSJiri Olsa 	} else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1387688d4dfcSCody P Schafer 		if (strcmp(term->val.str, "?")) {
1388bb963e16SNamhyung Kim 			if (verbose > 0) {
1389688d4dfcSCody P Schafer 				pr_info("Invalid sysfs entry %s=%s\n",
1390688d4dfcSCody P Schafer 						term->config, term->val.str);
1391e64b020bSJiri Olsa 			}
1392e64b020bSJiri Olsa 			if (err) {
13936c191289SIan Rogers 				parse_events_error__handle(err, term->err_val,
1394448d732cSIan Rogers 					strdup("expected numeric value"),
1395448d732cSIan Rogers 					NULL);
1396e64b020bSJiri Olsa 			}
1397688d4dfcSCody P Schafer 			return -EINVAL;
1398688d4dfcSCody P Schafer 		}
1399688d4dfcSCody P Schafer 
1400688d4dfcSCody P Schafer 		if (pmu_resolve_param_term(term, head_terms, &val))
1401688d4dfcSCody P Schafer 			return -EINVAL;
1402688d4dfcSCody P Schafer 	} else
1403688d4dfcSCody P Schafer 		return -EINVAL;
1404688d4dfcSCody P Schafer 
14050efe6b67SAdrian Hunter 	max_val = pmu_format_max_value(format->bits);
14060efe6b67SAdrian Hunter 	if (val > max_val) {
14070efe6b67SAdrian Hunter 		if (err) {
1408448d732cSIan Rogers 			char *err_str;
1409448d732cSIan Rogers 
14106c191289SIan Rogers 			parse_events_error__handle(err, term->err_val,
1411448d732cSIan Rogers 				asprintf(&err_str,
14120efe6b67SAdrian Hunter 				    "value too big for format, maximum is %llu",
1413448d732cSIan Rogers 				    (unsigned long long)max_val) < 0
1414448d732cSIan Rogers 				    ? strdup("value too big for format")
1415448d732cSIan Rogers 				    : err_str,
1416448d732cSIan Rogers 				    NULL);
14170efe6b67SAdrian Hunter 			return -EINVAL;
14180efe6b67SAdrian Hunter 		}
14190efe6b67SAdrian Hunter 		/*
14200efe6b67SAdrian Hunter 		 * Assume we don't care if !err, in which case the value will be
14210efe6b67SAdrian Hunter 		 * silently truncated.
14220efe6b67SAdrian Hunter 		 */
14230efe6b67SAdrian Hunter 	}
14240efe6b67SAdrian Hunter 
1425688d4dfcSCody P Schafer 	pmu_format_value(format->bits, val, vp, zero);
1426cd82a32eSJiri Olsa 	return 0;
1427cd82a32eSJiri Olsa }
1428cd82a32eSJiri Olsa 
perf_pmu__config_terms(struct perf_pmu * pmu,struct perf_event_attr * attr,struct list_head * head_terms,bool zero,struct parse_events_error * err)1429804fee5dSIan Rogers int perf_pmu__config_terms(struct perf_pmu *pmu,
1430cff7f956SJiri Olsa 			   struct perf_event_attr *attr,
1431dc0a6202SAdrian Hunter 			   struct list_head *head_terms,
1432e64b020bSJiri Olsa 			   bool zero, struct parse_events_error *err)
1433cd82a32eSJiri Olsa {
14346cee6cd3SArnaldo Carvalho de Melo 	struct parse_events_term *term;
1435cd82a32eSJiri Olsa 
1436688d4dfcSCody P Schafer 	list_for_each_entry(term, head_terms, list) {
1437804fee5dSIan Rogers 		if (pmu_config_term(pmu, attr, term, head_terms, zero, err))
1438cd82a32eSJiri Olsa 			return -EINVAL;
1439688d4dfcSCody P Schafer 	}
1440cd82a32eSJiri Olsa 
1441cd82a32eSJiri Olsa 	return 0;
1442cd82a32eSJiri Olsa }
1443cd82a32eSJiri Olsa 
1444cd82a32eSJiri Olsa /*
1445cd82a32eSJiri Olsa  * Configures event's 'attr' parameter based on the:
1446cd82a32eSJiri Olsa  * 1) users input - specified in terms parameter
1447cd82a32eSJiri Olsa  * 2) pmu format definitions - specified by pmu parameter
1448cd82a32eSJiri Olsa  */
perf_pmu__config(struct perf_pmu * pmu,struct perf_event_attr * attr,struct list_head * head_terms,struct parse_events_error * err)1449cd82a32eSJiri Olsa int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
1450e64b020bSJiri Olsa 		     struct list_head *head_terms,
1451e64b020bSJiri Olsa 		     struct parse_events_error *err)
1452cd82a32eSJiri Olsa {
1453dc0a6202SAdrian Hunter 	bool zero = !!pmu->default_config;
1454dc0a6202SAdrian Hunter 
1455804fee5dSIan Rogers 	return perf_pmu__config_terms(pmu, attr, head_terms, zero, err);
1456cd82a32eSJiri Olsa }
1457cd82a32eSJiri Olsa 
pmu_find_alias(struct perf_pmu * pmu,struct parse_events_term * term)14585c6ccc37SArnaldo Carvalho de Melo static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
14596cee6cd3SArnaldo Carvalho de Melo 					     struct parse_events_term *term)
1460a6146d50SZheng Yan {
1461e6ff1eedSIan Rogers 	struct perf_pmu_alias *alias;
1462970ef02eSIan Rogers 	const char *name;
1463a6146d50SZheng Yan 
1464a6146d50SZheng Yan 	if (parse_events__is_hardcoded_term(term))
1465a6146d50SZheng Yan 		return NULL;
1466a6146d50SZheng Yan 
1467a6146d50SZheng Yan 	if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
146864199ae4SIan Rogers 		if (!term->no_value)
1469a6146d50SZheng Yan 			return NULL;
1470a6146d50SZheng Yan 		if (pmu_find_format(&pmu->format, term->config))
1471a6146d50SZheng Yan 			return NULL;
1472a6146d50SZheng Yan 		name = term->config;
1473c3245d20SIan Rogers 
1474a6146d50SZheng Yan 	} else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1475a6146d50SZheng Yan 		if (strcasecmp(term->config, "event"))
1476a6146d50SZheng Yan 			return NULL;
1477a6146d50SZheng Yan 		name = term->val.str;
1478a6146d50SZheng Yan 	} else {
1479a6146d50SZheng Yan 		return NULL;
1480a6146d50SZheng Yan 	}
1481a6146d50SZheng Yan 
14828d4b6d37SIan Rogers 	alias = perf_pmu__find_alias(pmu, name, /*load=*/ true);
1483e6ff1eedSIan Rogers 	if (alias || pmu->cpu_aliases_added)
1484e6ff1eedSIan Rogers 		return alias;
1485e6ff1eedSIan Rogers 
1486e6ff1eedSIan Rogers 	/* Alias doesn't exist, try to get it from the json events. */
1487e6ff1eedSIan Rogers 	if (pmu->events_table &&
1488e6ff1eedSIan Rogers 	    pmu_events_table__find_event(pmu->events_table, pmu, name,
1489e6ff1eedSIan Rogers 				         pmu_add_cpu_aliases_map_callback,
1490e6ff1eedSIan Rogers 				         pmu) == 0) {
14918d4b6d37SIan Rogers 		alias = perf_pmu__find_alias(pmu, name, /*load=*/ false);
1492e6ff1eedSIan Rogers 	}
1493e6ff1eedSIan Rogers 	return alias;
1494a6146d50SZheng Yan }
1495a6146d50SZheng Yan 
1496410136f5SStephane Eranian 
check_info_data(struct perf_pmu * pmu,struct perf_pmu_alias * alias,struct perf_pmu_info * info,struct parse_events_error * err,int column)14977b723dbbSIan Rogers static int check_info_data(struct perf_pmu *pmu,
14987b723dbbSIan Rogers 			   struct perf_pmu_alias *alias,
14999d31cb93SIan Rogers 			   struct perf_pmu_info *info,
15009d31cb93SIan Rogers 			   struct parse_events_error *err,
15019d31cb93SIan Rogers 			   int column)
1502410136f5SStephane Eranian {
15037b723dbbSIan Rogers 	read_alias_info(pmu, alias);
1504410136f5SStephane Eranian 	/*
1505410136f5SStephane Eranian 	 * Only one term in event definition can
15061d9e446bSJiri Olsa 	 * define unit, scale and snapshot, fail
15071d9e446bSJiri Olsa 	 * if there's more than one.
1508410136f5SStephane Eranian 	 */
15099d31cb93SIan Rogers 	if (info->unit && alias->unit[0]) {
15109d31cb93SIan Rogers 		parse_events_error__handle(err, column,
15119d31cb93SIan Rogers 					strdup("Attempt to set event's unit twice"),
15129d31cb93SIan Rogers 					NULL);
1513410136f5SStephane Eranian 		return -EINVAL;
15149d31cb93SIan Rogers 	}
15159d31cb93SIan Rogers 	if (info->scale && alias->scale) {
15169d31cb93SIan Rogers 		parse_events_error__handle(err, column,
15179d31cb93SIan Rogers 					strdup("Attempt to set event's scale twice"),
15189d31cb93SIan Rogers 					NULL);
15199d31cb93SIan Rogers 		return -EINVAL;
15209d31cb93SIan Rogers 	}
15219d31cb93SIan Rogers 	if (info->snapshot && alias->snapshot) {
15229d31cb93SIan Rogers 		parse_events_error__handle(err, column,
15239d31cb93SIan Rogers 					strdup("Attempt to set event snapshot twice"),
15249d31cb93SIan Rogers 					NULL);
15259d31cb93SIan Rogers 		return -EINVAL;
15269d31cb93SIan Rogers 	}
1527410136f5SStephane Eranian 
1528b30a7d1fSArnaldo Carvalho de Melo 	if (alias->unit[0])
15291d9e446bSJiri Olsa 		info->unit = alias->unit;
1530410136f5SStephane Eranian 
1531410136f5SStephane Eranian 	if (alias->scale)
15321d9e446bSJiri Olsa 		info->scale = alias->scale;
15331d9e446bSJiri Olsa 
15341d9e446bSJiri Olsa 	if (alias->snapshot)
15351d9e446bSJiri Olsa 		info->snapshot = alias->snapshot;
1536410136f5SStephane Eranian 
1537410136f5SStephane Eranian 	return 0;
1538410136f5SStephane Eranian }
1539410136f5SStephane Eranian 
1540a6146d50SZheng Yan /*
1541a6146d50SZheng Yan  * Find alias in the terms list and replace it with the terms
1542a6146d50SZheng Yan  * defined for the alias
1543a6146d50SZheng Yan  */
perf_pmu__check_alias(struct perf_pmu * pmu,struct list_head * head_terms,struct perf_pmu_info * info,struct parse_events_error * err)1544410136f5SStephane Eranian int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
15459d31cb93SIan Rogers 			  struct perf_pmu_info *info, struct parse_events_error *err)
1546a6146d50SZheng Yan {
15476cee6cd3SArnaldo Carvalho de Melo 	struct parse_events_term *term, *h;
15485c6ccc37SArnaldo Carvalho de Melo 	struct perf_pmu_alias *alias;
1549a6146d50SZheng Yan 	int ret;
1550a6146d50SZheng Yan 
1551044330c1SMatt Fleming 	info->per_pkg = false;
1552044330c1SMatt Fleming 
15538a398897SStephane Eranian 	/*
15548a398897SStephane Eranian 	 * Mark unit and scale as not set
15558a398897SStephane Eranian 	 * (different from default values, see below)
15568a398897SStephane Eranian 	 */
155746441bdcSMatt Fleming 	info->unit     = NULL;
155846441bdcSMatt Fleming 	info->scale    = 0.0;
15591d9e446bSJiri Olsa 	info->snapshot = false;
1560410136f5SStephane Eranian 
1561a6146d50SZheng Yan 	list_for_each_entry_safe(term, h, head_terms, list) {
1562a6146d50SZheng Yan 		alias = pmu_find_alias(pmu, term);
1563a6146d50SZheng Yan 		if (!alias)
1564a6146d50SZheng Yan 			continue;
1565a6146d50SZheng Yan 		ret = pmu_alias_terms(alias, &term->list);
15669d31cb93SIan Rogers 		if (ret) {
15679d31cb93SIan Rogers 			parse_events_error__handle(err, term->err_term,
15689d31cb93SIan Rogers 						strdup("Failure to duplicate terms"),
15699d31cb93SIan Rogers 						NULL);
1570a6146d50SZheng Yan 			return ret;
15719d31cb93SIan Rogers 		}
1572410136f5SStephane Eranian 
15737b723dbbSIan Rogers 		ret = check_info_data(pmu, alias, info, err, term->err_term);
1574410136f5SStephane Eranian 		if (ret)
1575410136f5SStephane Eranian 			return ret;
1576410136f5SStephane Eranian 
1577044330c1SMatt Fleming 		if (alias->per_pkg)
1578044330c1SMatt Fleming 			info->per_pkg = true;
1579044330c1SMatt Fleming 
1580e56fbc9dSArnaldo Carvalho de Melo 		list_del_init(&term->list);
15811dc92556SIan Rogers 		parse_events_term__delete(term);
1582a6146d50SZheng Yan 	}
15838a398897SStephane Eranian 
15848a398897SStephane Eranian 	/*
15858a398897SStephane Eranian 	 * if no unit or scale found in aliases, then
15868a398897SStephane Eranian 	 * set defaults as for evsel
15878a398897SStephane Eranian 	 * unit cannot left to NULL
15888a398897SStephane Eranian 	 */
158946441bdcSMatt Fleming 	if (info->unit == NULL)
159046441bdcSMatt Fleming 		info->unit   = "";
15918a398897SStephane Eranian 
159246441bdcSMatt Fleming 	if (info->scale == 0.0)
159346441bdcSMatt Fleming 		info->scale  = 1.0;
15948a398897SStephane Eranian 
1595a6146d50SZheng Yan 	return 0;
1596a6146d50SZheng Yan }
1597a6146d50SZheng Yan 
1598c3245d20SIan Rogers struct find_event_args {
1599c3245d20SIan Rogers 	const char *event;
1600c3245d20SIan Rogers 	void *state;
1601c3245d20SIan Rogers 	pmu_event_callback cb;
1602c3245d20SIan Rogers };
1603c3245d20SIan Rogers 
find_event_callback(void * state,struct pmu_event_info * info)1604c3245d20SIan Rogers static int find_event_callback(void *state, struct pmu_event_info *info)
1605838a8c5fSIan Rogers {
1606c3245d20SIan Rogers 	struct find_event_args *args = state;
1607838a8c5fSIan Rogers 
1608c3245d20SIan Rogers 	if (!strcmp(args->event, info->name))
1609c3245d20SIan Rogers 		return args->cb(args->state, info);
1610838a8c5fSIan Rogers 
1611c3245d20SIan Rogers 	return 0;
1612838a8c5fSIan Rogers }
1613c3245d20SIan Rogers 
perf_pmu__find_event(struct perf_pmu * pmu,const char * event,void * state,pmu_event_callback cb)1614c3245d20SIan Rogers int perf_pmu__find_event(struct perf_pmu *pmu, const char *event, void *state, pmu_event_callback cb)
1615c3245d20SIan Rogers {
1616c3245d20SIan Rogers 	struct find_event_args args = {
1617c3245d20SIan Rogers 		.event = event,
1618c3245d20SIan Rogers 		.state = state,
1619c3245d20SIan Rogers 		.cb = cb,
1620c3245d20SIan Rogers 	};
1621c3245d20SIan Rogers 
1622cd4e1efbSIan Rogers 	/* Sub-optimal, but function is only used by tests. */
1623cd4e1efbSIan Rogers 	return perf_pmu__for_each_event(pmu, /*skip_duplicate_pmus=*/ false,
1624cd4e1efbSIan Rogers 					&args, find_event_callback);
1625c3245d20SIan Rogers }
1626c3245d20SIan Rogers 
perf_pmu__del_formats(struct list_head * formats)1627804fee5dSIan Rogers static void perf_pmu__del_formats(struct list_head *formats)
1628d26383dcSNamhyung Kim {
1629d26383dcSNamhyung Kim 	struct perf_pmu_format *fmt, *tmp;
1630d26383dcSNamhyung Kim 
1631d26383dcSNamhyung Kim 	list_for_each_entry_safe(fmt, tmp, formats, list) {
1632d26383dcSNamhyung Kim 		list_del(&fmt->list);
1633efe98a7aSArnaldo Carvalho de Melo 		zfree(&fmt->name);
1634d26383dcSNamhyung Kim 		free(fmt);
1635d26383dcSNamhyung Kim 	}
1636d26383dcSNamhyung Kim }
1637d26383dcSNamhyung Kim 
perf_pmu__has_format(const struct perf_pmu * pmu,const char * name)163807d2b820SIan Rogers bool perf_pmu__has_format(const struct perf_pmu *pmu, const char *name)
163907d2b820SIan Rogers {
164007d2b820SIan Rogers 	struct perf_pmu_format *format;
164107d2b820SIan Rogers 
164207d2b820SIan Rogers 	list_for_each_entry(format, &pmu->format, list) {
164307d2b820SIan Rogers 		if (!strcmp(format->name, name))
164407d2b820SIan Rogers 			return true;
164507d2b820SIan Rogers 	}
164607d2b820SIan Rogers 	return false;
164707d2b820SIan Rogers }
164807d2b820SIan Rogers 
is_pmu_core(const char * name)1649d504fae9SJohn Garry bool is_pmu_core(const char *name)
1650d504fae9SJohn Garry {
16516fbd67b0SThomas Richter 	return !strcmp(name, "cpu") || !strcmp(name, "cpum_cf") || is_sysfs_pmu_core(name);
16524bf7e81aSIan Rogers }
16534bf7e81aSIan Rogers 
perf_pmu__supports_legacy_cache(const struct perf_pmu * pmu)16546fd1e519SIan Rogers bool perf_pmu__supports_legacy_cache(const struct perf_pmu *pmu)
16556fd1e519SIan Rogers {
1656e20d1f2fSIan Rogers 	return pmu->is_core;
16576fd1e519SIan Rogers }
16586fd1e519SIan Rogers 
perf_pmu__auto_merge_stats(const struct perf_pmu * pmu)165952c7b4d3SIan Rogers bool perf_pmu__auto_merge_stats(const struct perf_pmu *pmu)
166052c7b4d3SIan Rogers {
1661710dffc9SIan Rogers 	return !pmu->is_core || perf_pmus__num_core_pmus() == 1;
166252c7b4d3SIan Rogers }
166352c7b4d3SIan Rogers 
perf_pmu__have_event(struct perf_pmu * pmu,const char * name)1664e6ff1eedSIan Rogers bool perf_pmu__have_event(struct perf_pmu *pmu, const char *name)
1665abe9544eSIan Rogers {
16668d4b6d37SIan Rogers 	if (perf_pmu__find_alias(pmu, name, /*load=*/ true) != NULL)
1667e6ff1eedSIan Rogers 		return true;
1668e6ff1eedSIan Rogers 	if (pmu->cpu_aliases_added || !pmu->events_table)
1669e6ff1eedSIan Rogers 		return false;
1670e6ff1eedSIan Rogers 	return pmu_events_table__find_event(pmu->events_table, pmu, name, NULL, NULL) == 0;
16714cabc3d1SAndi Kleen }
1672c3245d20SIan Rogers 
perf_pmu__num_events(struct perf_pmu * pmu)1673e6ff1eedSIan Rogers size_t perf_pmu__num_events(struct perf_pmu *pmu)
1674c3245d20SIan Rogers {
16758d4b6d37SIan Rogers 	size_t nr;
16768d4b6d37SIan Rogers 
16778d4b6d37SIan Rogers 	pmu_aliases_parse(pmu);
16785af155f9SIan Rogers 	nr = pmu->sysfs_aliases + pmu->sys_json_aliases;;
1679c3245d20SIan Rogers 
1680e6ff1eedSIan Rogers 	if (pmu->cpu_aliases_added)
16815af155f9SIan Rogers 		 nr += pmu->cpu_json_aliases;
1682e6ff1eedSIan Rogers 	else if (pmu->events_table)
16835af155f9SIan Rogers 		nr += pmu_events_table__num_events(pmu->events_table, pmu) - pmu->cpu_json_aliases;
16845af155f9SIan Rogers 	else
16855af155f9SIan Rogers 		assert(pmu->cpu_json_aliases == 0);
1686c3245d20SIan Rogers 
1687c3245d20SIan Rogers 	return pmu->selectable ? nr + 1 : nr;
1688c3245d20SIan Rogers }
1689c3245d20SIan Rogers 
sub_non_neg(int a,int b)1690c3245d20SIan Rogers static int sub_non_neg(int a, int b)
1691c3245d20SIan Rogers {
1692c3245d20SIan Rogers 	if (b > a)
1693c3245d20SIan Rogers 		return 0;
1694c3245d20SIan Rogers 	return a - b;
1695c3245d20SIan Rogers }
1696c3245d20SIan Rogers 
format_alias(char * buf,int len,const struct perf_pmu * pmu,const struct perf_pmu_alias * alias,bool skip_duplicate_pmus)1697c3245d20SIan Rogers static char *format_alias(char *buf, int len, const struct perf_pmu *pmu,
1698cd4e1efbSIan Rogers 			  const struct perf_pmu_alias *alias, bool skip_duplicate_pmus)
1699c3245d20SIan Rogers {
1700c3245d20SIan Rogers 	struct parse_events_term *term;
1701cd4e1efbSIan Rogers 	int pmu_name_len = skip_duplicate_pmus
1702cd4e1efbSIan Rogers 		? pmu_name_len_no_suffix(pmu->name, /*num=*/NULL)
1703cd4e1efbSIan Rogers 		: (int)strlen(pmu->name);
1704cd4e1efbSIan Rogers 	int used = snprintf(buf, len, "%.*s/%s", pmu_name_len, pmu->name, alias->name);
1705c3245d20SIan Rogers 
1706c3245d20SIan Rogers 	list_for_each_entry(term, &alias->terms, list) {
1707c3245d20SIan Rogers 		if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1708c3245d20SIan Rogers 			used += snprintf(buf + used, sub_non_neg(len, used),
1709c3245d20SIan Rogers 					",%s=%s", term->config,
1710c3245d20SIan Rogers 					term->val.str);
1711c3245d20SIan Rogers 	}
1712c3245d20SIan Rogers 
1713c3245d20SIan Rogers 	if (sub_non_neg(len, used) > 0) {
1714c3245d20SIan Rogers 		buf[used] = '/';
1715c3245d20SIan Rogers 		used++;
1716c3245d20SIan Rogers 	}
1717c3245d20SIan Rogers 	if (sub_non_neg(len, used) > 0) {
1718c3245d20SIan Rogers 		buf[used] = '\0';
1719c3245d20SIan Rogers 		used++;
1720c3245d20SIan Rogers 	} else
1721c3245d20SIan Rogers 		buf[len - 1] = '\0';
1722c3245d20SIan Rogers 
1723c3245d20SIan Rogers 	return buf;
1724c3245d20SIan Rogers }
1725c3245d20SIan Rogers 
perf_pmu__for_each_event(struct perf_pmu * pmu,bool skip_duplicate_pmus,void * state,pmu_event_callback cb)1726cd4e1efbSIan Rogers int perf_pmu__for_each_event(struct perf_pmu *pmu, bool skip_duplicate_pmus,
1727cd4e1efbSIan Rogers 			     void *state, pmu_event_callback cb)
1728c3245d20SIan Rogers {
1729c3245d20SIan Rogers 	char buf[1024];
1730c3245d20SIan Rogers 	struct perf_pmu_alias *event;
1731c3245d20SIan Rogers 	struct pmu_event_info info = {
1732c3245d20SIan Rogers 		.pmu = pmu,
1733c3245d20SIan Rogers 	};
1734c3245d20SIan Rogers 	int ret = 0;
173530f0b435SIan Rogers 	struct strbuf sb;
1736c3245d20SIan Rogers 
173730f0b435SIan Rogers 	strbuf_init(&sb, /*hint=*/ 0);
17384ac93db3SIan Rogers 	pmu_aliases_parse(pmu);
1739e6ff1eedSIan Rogers 	pmu_add_cpu_aliases(pmu);
1740c3245d20SIan Rogers 	list_for_each_entry(event, &pmu->aliases, list) {
1741c3245d20SIan Rogers 		size_t buf_used;
1742c3245d20SIan Rogers 
1743c3245d20SIan Rogers 		info.pmu_name = event->pmu_name ?: pmu->name;
1744c3245d20SIan Rogers 		info.alias = NULL;
1745c3245d20SIan Rogers 		if (event->desc) {
1746c3245d20SIan Rogers 			info.name = event->name;
1747c3245d20SIan Rogers 			buf_used = 0;
1748c3245d20SIan Rogers 		} else {
1749cd4e1efbSIan Rogers 			info.name = format_alias(buf, sizeof(buf), pmu, event,
1750cd4e1efbSIan Rogers 						 skip_duplicate_pmus);
1751c3245d20SIan Rogers 			if (pmu->is_core) {
1752c3245d20SIan Rogers 				info.alias = info.name;
1753c3245d20SIan Rogers 				info.name = event->name;
1754c3245d20SIan Rogers 			}
1755c3245d20SIan Rogers 			buf_used = strlen(buf) + 1;
1756c3245d20SIan Rogers 		}
1757c3245d20SIan Rogers 		info.scale_unit = NULL;
1758c3245d20SIan Rogers 		if (strlen(event->unit) || event->scale != 1.0) {
1759c3245d20SIan Rogers 			info.scale_unit = buf + buf_used;
1760c3245d20SIan Rogers 			buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used,
1761c3245d20SIan Rogers 					"%G%s", event->scale, event->unit) + 1;
1762c3245d20SIan Rogers 		}
1763c3245d20SIan Rogers 		info.desc = event->desc;
1764c3245d20SIan Rogers 		info.long_desc = event->long_desc;
1765c3245d20SIan Rogers 		info.encoding_desc = buf + buf_used;
176630f0b435SIan Rogers 		parse_events_term__to_strbuf(&event->terms, &sb);
1767c3245d20SIan Rogers 		buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used,
176830f0b435SIan Rogers 				"%s/%s/", info.pmu_name, sb.buf) + 1;
1769c3245d20SIan Rogers 		info.topic = event->topic;
177030f0b435SIan Rogers 		info.str = sb.buf;
1771c3245d20SIan Rogers 		info.deprecated = event->deprecated;
1772c3245d20SIan Rogers 		ret = cb(state, &info);
1773c3245d20SIan Rogers 		if (ret)
177430f0b435SIan Rogers 			goto out;
177530f0b435SIan Rogers 		strbuf_setlen(&sb, /*len=*/ 0);
1776c3245d20SIan Rogers 	}
1777c3245d20SIan Rogers 	if (pmu->selectable) {
1778c3245d20SIan Rogers 		info.name = buf;
1779c3245d20SIan Rogers 		snprintf(buf, sizeof(buf), "%s//", pmu->name);
1780c3245d20SIan Rogers 		info.alias = NULL;
1781c3245d20SIan Rogers 		info.scale_unit = NULL;
1782c3245d20SIan Rogers 		info.desc = NULL;
1783c3245d20SIan Rogers 		info.long_desc = NULL;
1784c3245d20SIan Rogers 		info.encoding_desc = NULL;
1785c3245d20SIan Rogers 		info.topic = NULL;
1786c3245d20SIan Rogers 		info.pmu_name = pmu->name;
1787c3245d20SIan Rogers 		info.deprecated = false;
1788c3245d20SIan Rogers 		ret = cb(state, &info);
1789c3245d20SIan Rogers 	}
179030f0b435SIan Rogers out:
179130f0b435SIan Rogers 	strbuf_release(&sb);
1792c3245d20SIan Rogers 	return ret;
17934cabc3d1SAndi Kleen }
17947d4bdab5SAdrian Hunter 
pmu__name_match(const struct perf_pmu * pmu,const char * pmu_name)1795e3edd6cfSIan Rogers bool pmu__name_match(const struct perf_pmu *pmu, const char *pmu_name)
1796e3edd6cfSIan Rogers {
1797e3edd6cfSIan Rogers 	return !strcmp(pmu->name, pmu_name) ||
1798d2045f87SIan Rogers 		(pmu->is_uncore && pmu_uncore_alias_match(pmu_name, pmu->name)) ||
1799d2045f87SIan Rogers 		/*
1800d2045f87SIan Rogers 		 * jevents and tests use default_core as a marker for any core
1801d2045f87SIan Rogers 		 * PMU as the PMU name varies across architectures.
1802d2045f87SIan Rogers 		 */
1803d2045f87SIan Rogers 	        (pmu->is_core && !strcmp(pmu_name, "default_core"));
1804e3edd6cfSIan Rogers }
1805e3edd6cfSIan Rogers 
perf_pmu__is_software(const struct perf_pmu * pmu)1806251aa040SIan Rogers bool perf_pmu__is_software(const struct perf_pmu *pmu)
1807251aa040SIan Rogers {
180869a0a42bSIan Rogers 	const char *known_sw_pmus[] = {
180969a0a42bSIan Rogers 		"kprobe",
181069a0a42bSIan Rogers 		"msr",
181169a0a42bSIan Rogers 		"uprobe",
181269a0a42bSIan Rogers 	};
181369a0a42bSIan Rogers 
1814251aa040SIan Rogers 	if (pmu->is_core || pmu->is_uncore || pmu->auxtrace)
1815251aa040SIan Rogers 		return false;
1816251aa040SIan Rogers 	switch (pmu->type) {
1817251aa040SIan Rogers 	case PERF_TYPE_HARDWARE:	return false;
1818251aa040SIan Rogers 	case PERF_TYPE_SOFTWARE:	return true;
1819251aa040SIan Rogers 	case PERF_TYPE_TRACEPOINT:	return true;
1820251aa040SIan Rogers 	case PERF_TYPE_HW_CACHE:	return false;
1821251aa040SIan Rogers 	case PERF_TYPE_RAW:		return false;
1822251aa040SIan Rogers 	case PERF_TYPE_BREAKPOINT:	return true;
1823251aa040SIan Rogers 	default: break;
1824251aa040SIan Rogers 	}
182569a0a42bSIan Rogers 	for (size_t i = 0; i < ARRAY_SIZE(known_sw_pmus); i++) {
182669a0a42bSIan Rogers 		if (!strcmp(pmu->name, known_sw_pmus[i]))
182769a0a42bSIan Rogers 			return true;
182869a0a42bSIan Rogers 	}
182969a0a42bSIan Rogers 	return false;
1830251aa040SIan Rogers }
1831251aa040SIan Rogers 
perf_pmu__open_file(struct perf_pmu * pmu,const char * name)1832d50a79cdSJames Clark FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
18337d4bdab5SAdrian Hunter {
18347d4bdab5SAdrian Hunter 	char path[PATH_MAX];
18357d4bdab5SAdrian Hunter 
1836f8ad6018SJames Clark 	if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name) ||
1837f8ad6018SJames Clark 	    !file_available(path))
18387d4bdab5SAdrian Hunter 		return NULL;
18397d4bdab5SAdrian Hunter 
18407d4bdab5SAdrian Hunter 	return fopen(path, "r");
18417d4bdab5SAdrian Hunter }
18427d4bdab5SAdrian Hunter 
perf_pmu__open_file_at(struct perf_pmu * pmu,int dirfd,const char * name)18433a69672eSNamhyung Kim FILE *perf_pmu__open_file_at(struct perf_pmu *pmu, int dirfd, const char *name)
18443a69672eSNamhyung Kim {
18453a69672eSNamhyung Kim 	int fd;
18463a69672eSNamhyung Kim 
18473a69672eSNamhyung Kim 	fd = perf_pmu__pathname_fd(dirfd, pmu->name, name, O_RDONLY);
18483a69672eSNamhyung Kim 	if (fd < 0)
18493a69672eSNamhyung Kim 		return NULL;
18503a69672eSNamhyung Kim 
18513a69672eSNamhyung Kim 	return fdopen(fd, "r");
18523a69672eSNamhyung Kim }
18533a69672eSNamhyung Kim 
perf_pmu__scan_file(struct perf_pmu * pmu,const char * name,const char * fmt,...)18547d4bdab5SAdrian Hunter int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
18557d4bdab5SAdrian Hunter 			...)
18567d4bdab5SAdrian Hunter {
18577d4bdab5SAdrian Hunter 	va_list args;
18587d4bdab5SAdrian Hunter 	FILE *file;
18597d4bdab5SAdrian Hunter 	int ret = EOF;
18607d4bdab5SAdrian Hunter 
18617d4bdab5SAdrian Hunter 	va_start(args, fmt);
18627d4bdab5SAdrian Hunter 	file = perf_pmu__open_file(pmu, name);
18637d4bdab5SAdrian Hunter 	if (file) {
18647d4bdab5SAdrian Hunter 		ret = vfscanf(file, fmt, args);
18657d4bdab5SAdrian Hunter 		fclose(file);
18667d4bdab5SAdrian Hunter 	}
18677d4bdab5SAdrian Hunter 	va_end(args);
18687d4bdab5SAdrian Hunter 	return ret;
18697d4bdab5SAdrian Hunter }
18709fbc61f8SKan Liang 
perf_pmu__scan_file_at(struct perf_pmu * pmu,int dirfd,const char * name,const char * fmt,...)18713a69672eSNamhyung Kim int perf_pmu__scan_file_at(struct perf_pmu *pmu, int dirfd, const char *name,
18723a69672eSNamhyung Kim 			   const char *fmt, ...)
18733a69672eSNamhyung Kim {
18743a69672eSNamhyung Kim 	va_list args;
18753a69672eSNamhyung Kim 	FILE *file;
18763a69672eSNamhyung Kim 	int ret = EOF;
18773a69672eSNamhyung Kim 
18783a69672eSNamhyung Kim 	va_start(args, fmt);
18793a69672eSNamhyung Kim 	file = perf_pmu__open_file_at(pmu, dirfd, name);
18803a69672eSNamhyung Kim 	if (file) {
18813a69672eSNamhyung Kim 		ret = vfscanf(file, fmt, args);
18823a69672eSNamhyung Kim 		fclose(file);
18833a69672eSNamhyung Kim 	}
18843a69672eSNamhyung Kim 	va_end(args);
18853a69672eSNamhyung Kim 	return ret;
18863a69672eSNamhyung Kim }
18873a69672eSNamhyung Kim 
perf_pmu__file_exists(struct perf_pmu * pmu,const char * name)1888c2b6a896SGerman Gomez bool perf_pmu__file_exists(struct perf_pmu *pmu, const char *name)
1889c2b6a896SGerman Gomez {
1890c2b6a896SGerman Gomez 	char path[PATH_MAX];
1891c2b6a896SGerman Gomez 
1892c2b6a896SGerman Gomez 	if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name))
1893c2b6a896SGerman Gomez 		return false;
1894c2b6a896SGerman Gomez 
1895c2b6a896SGerman Gomez 	return file_available(path);
1896c2b6a896SGerman Gomez }
1897c2b6a896SGerman Gomez 
perf_pmu__new_caps(struct list_head * list,char * name,char * value)18989fbc61f8SKan Liang static int perf_pmu__new_caps(struct list_head *list, char *name, char *value)
18999fbc61f8SKan Liang {
19009fbc61f8SKan Liang 	struct perf_pmu_caps *caps = zalloc(sizeof(*caps));
19019fbc61f8SKan Liang 
19029fbc61f8SKan Liang 	if (!caps)
19039fbc61f8SKan Liang 		return -ENOMEM;
19049fbc61f8SKan Liang 
19059fbc61f8SKan Liang 	caps->name = strdup(name);
19069fbc61f8SKan Liang 	if (!caps->name)
19079fbc61f8SKan Liang 		goto free_caps;
19089fbc61f8SKan Liang 	caps->value = strndup(value, strlen(value) - 1);
19099fbc61f8SKan Liang 	if (!caps->value)
19109fbc61f8SKan Liang 		goto free_name;
19119fbc61f8SKan Liang 	list_add_tail(&caps->list, list);
19129fbc61f8SKan Liang 	return 0;
19139fbc61f8SKan Liang 
19149fbc61f8SKan Liang free_name:
191557f14b5aSArnaldo Carvalho de Melo 	zfree(&caps->name);
19169fbc61f8SKan Liang free_caps:
19179fbc61f8SKan Liang 	free(caps);
19189fbc61f8SKan Liang 
19199fbc61f8SKan Liang 	return -ENOMEM;
19209fbc61f8SKan Liang }
19219fbc61f8SKan Liang 
perf_pmu__del_caps(struct perf_pmu * pmu)1922eec11310SNamhyung Kim static void perf_pmu__del_caps(struct perf_pmu *pmu)
1923eec11310SNamhyung Kim {
1924eec11310SNamhyung Kim 	struct perf_pmu_caps *caps, *tmp;
1925eec11310SNamhyung Kim 
1926eec11310SNamhyung Kim 	list_for_each_entry_safe(caps, tmp, &pmu->caps, list) {
1927eec11310SNamhyung Kim 		list_del(&caps->list);
1928efe98a7aSArnaldo Carvalho de Melo 		zfree(&caps->name);
1929efe98a7aSArnaldo Carvalho de Melo 		zfree(&caps->value);
1930eec11310SNamhyung Kim 		free(caps);
1931eec11310SNamhyung Kim 	}
1932eec11310SNamhyung Kim }
1933eec11310SNamhyung Kim 
19349fbc61f8SKan Liang /*
19359fbc61f8SKan Liang  * Reading/parsing the given pmu capabilities, which should be located at:
19369fbc61f8SKan Liang  * /sys/bus/event_source/devices/<dev>/caps as sysfs group attributes.
19379fbc61f8SKan Liang  * Return the number of capabilities
19389fbc61f8SKan Liang  */
perf_pmu__caps_parse(struct perf_pmu * pmu)19399fbc61f8SKan Liang int perf_pmu__caps_parse(struct perf_pmu *pmu)
19409fbc61f8SKan Liang {
19419fbc61f8SKan Liang 	struct stat st;
19429fbc61f8SKan Liang 	char caps_path[PATH_MAX];
19439fbc61f8SKan Liang 	DIR *caps_dir;
19449fbc61f8SKan Liang 	struct dirent *evt_ent;
1945b39094d3SNamhyung Kim 	int caps_fd;
19463339ec44SRavi Bangoria 
19473339ec44SRavi Bangoria 	if (pmu->caps_initialized)
19483339ec44SRavi Bangoria 		return pmu->nr_caps;
19493339ec44SRavi Bangoria 
19503339ec44SRavi Bangoria 	pmu->nr_caps = 0;
19519fbc61f8SKan Liang 
1952f8ad6018SJames Clark 	if (!perf_pmu__pathname_scnprintf(caps_path, sizeof(caps_path), pmu->name, "caps"))
19539fbc61f8SKan Liang 		return -1;
19549fbc61f8SKan Liang 
19553339ec44SRavi Bangoria 	if (stat(caps_path, &st) < 0) {
19563339ec44SRavi Bangoria 		pmu->caps_initialized = true;
19579fbc61f8SKan Liang 		return 0;	/* no error if caps does not exist */
19583339ec44SRavi Bangoria 	}
19599fbc61f8SKan Liang 
19609fbc61f8SKan Liang 	caps_dir = opendir(caps_path);
19619fbc61f8SKan Liang 	if (!caps_dir)
19629fbc61f8SKan Liang 		return -EINVAL;
19639fbc61f8SKan Liang 
1964b39094d3SNamhyung Kim 	caps_fd = dirfd(caps_dir);
1965b39094d3SNamhyung Kim 
19669fbc61f8SKan Liang 	while ((evt_ent = readdir(caps_dir)) != NULL) {
19679fbc61f8SKan Liang 		char *name = evt_ent->d_name;
19689fbc61f8SKan Liang 		char value[128];
19699fbc61f8SKan Liang 		FILE *file;
1970b39094d3SNamhyung Kim 		int fd;
19719fbc61f8SKan Liang 
19729fbc61f8SKan Liang 		if (!strcmp(name, ".") || !strcmp(name, ".."))
19739fbc61f8SKan Liang 			continue;
19749fbc61f8SKan Liang 
1975b39094d3SNamhyung Kim 		fd = openat(caps_fd, name, O_RDONLY);
19760ea8920eSIan Rogers 		if (fd == -1)
19779fbc61f8SKan Liang 			continue;
19780ea8920eSIan Rogers 		file = fdopen(fd, "r");
19790ea8920eSIan Rogers 		if (!file) {
19800ea8920eSIan Rogers 			close(fd);
19810ea8920eSIan Rogers 			continue;
19820ea8920eSIan Rogers 		}
19839fbc61f8SKan Liang 
19849fbc61f8SKan Liang 		if (!fgets(value, sizeof(value), file) ||
19859fbc61f8SKan Liang 		    (perf_pmu__new_caps(&pmu->caps, name, value) < 0)) {
19869fbc61f8SKan Liang 			fclose(file);
19879fbc61f8SKan Liang 			continue;
19889fbc61f8SKan Liang 		}
19899fbc61f8SKan Liang 
19903339ec44SRavi Bangoria 		pmu->nr_caps++;
19919fbc61f8SKan Liang 		fclose(file);
19929fbc61f8SKan Liang 	}
19939fbc61f8SKan Liang 
19949fbc61f8SKan Liang 	closedir(caps_dir);
19959fbc61f8SKan Liang 
19963339ec44SRavi Bangoria 	pmu->caps_initialized = true;
19973339ec44SRavi Bangoria 	return pmu->nr_caps;
19989fbc61f8SKan Liang }
1999e4064776SJin Yao 
perf_pmu__compute_config_masks(struct perf_pmu * pmu)2000b9f01032SIan Rogers static void perf_pmu__compute_config_masks(struct perf_pmu *pmu)
2001e4064776SJin Yao {
2002e4064776SJin Yao 	struct perf_pmu_format *format;
2003b9f01032SIan Rogers 
2004b9f01032SIan Rogers 	if (pmu->config_masks_computed)
2005b9f01032SIan Rogers 		return;
2006e4064776SJin Yao 
2007e4064776SJin Yao 	list_for_each_entry(format, &pmu->format, list)	{
2008b9f01032SIan Rogers 		unsigned int i;
2009b9f01032SIan Rogers 		__u64 *mask;
2010b9f01032SIan Rogers 
2011b9f01032SIan Rogers 		if (format->value >= PERF_PMU_FORMAT_VALUE_CONFIG_END)
2012e4064776SJin Yao 			continue;
2013e4064776SJin Yao 
2014b9f01032SIan Rogers 		pmu->config_masks_present = true;
2015b9f01032SIan Rogers 		mask = &pmu->config_masks[format->value];
2016b9f01032SIan Rogers 
2017e4064776SJin Yao 		for_each_set_bit(i, format->bits, PERF_PMU_FORMAT_BITS)
2018b9f01032SIan Rogers 			*mask |= 1ULL << i;
2019e4064776SJin Yao 	}
2020b9f01032SIan Rogers 	pmu->config_masks_computed = true;
2021b9f01032SIan Rogers }
2022b9f01032SIan Rogers 
perf_pmu__warn_invalid_config(struct perf_pmu * pmu,__u64 config,const char * name,int config_num,const char * config_name)2023b9f01032SIan Rogers void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config,
2024b9f01032SIan Rogers 				   const char *name, int config_num,
2025b9f01032SIan Rogers 				   const char *config_name)
2026b9f01032SIan Rogers {
2027b9f01032SIan Rogers 	__u64 bits;
2028b9f01032SIan Rogers 	char buf[100];
2029b9f01032SIan Rogers 
2030b9f01032SIan Rogers 	perf_pmu__compute_config_masks(pmu);
2031e4064776SJin Yao 
2032e4064776SJin Yao 	/*
2033e4064776SJin Yao 	 * Kernel doesn't export any valid format bits.
2034e4064776SJin Yao 	 */
2035b9f01032SIan Rogers 	if (!pmu->config_masks_present)
2036e4064776SJin Yao 		return;
2037e4064776SJin Yao 
2038b9f01032SIan Rogers 	bits = config & ~pmu->config_masks[config_num];
2039e4064776SJin Yao 	if (bits == 0)
2040e4064776SJin Yao 		return;
2041e4064776SJin Yao 
2042e4064776SJin Yao 	bitmap_scnprintf((unsigned long *)&bits, sizeof(bits) * 8, buf, sizeof(buf));
2043e4064776SJin Yao 
2044b9f01032SIan Rogers 	pr_warning("WARNING: event '%s' not valid (bits %s of %s "
2045e4064776SJin Yao 		   "'%llx' not supported by kernel)!\n",
2046b9f01032SIan Rogers 		   name ?: "N/A", buf, config_name, config);
2047e4064776SJin Yao }
2048c5a26ea4SJin Yao 
perf_pmu__match(const char * pattern,const char * name,const char * tok)2049c091ee90SIan Rogers int perf_pmu__match(const char *pattern, const char *name, const char *tok)
2050c47a5599SJin Yao {
205113d60ba0SKan Liang 	if (!name)
205213d60ba0SKan Liang 		return -1;
205313d60ba0SKan Liang 
2054c47a5599SJin Yao 	if (fnmatch(pattern, name, 0))
2055c47a5599SJin Yao 		return -1;
2056c47a5599SJin Yao 
2057240e6fd0SIan Rogers 	if (tok && !perf_pmu__match_ignoring_suffix(name, tok))
2058c47a5599SJin Yao 		return -1;
2059c47a5599SJin Yao 
2060c47a5599SJin Yao 	return 0;
2061c47a5599SJin Yao }
20621d3351e6SJin Yao 
perf_pmu__cpu_slots_per_cycle(void)2063acef233bSJing Zhang double __weak perf_pmu__cpu_slots_per_cycle(void)
2064acef233bSJing Zhang {
2065acef233bSJing Zhang 	return NAN;
2066acef233bSJing Zhang }
2067f8ad6018SJames Clark 
perf_pmu__event_source_devices_scnprintf(char * pathname,size_t size)2068f8ad6018SJames Clark int perf_pmu__event_source_devices_scnprintf(char *pathname, size_t size)
2069f8ad6018SJames Clark {
2070f8ad6018SJames Clark 	const char *sysfs = sysfs__mountpoint();
2071f8ad6018SJames Clark 
2072f8ad6018SJames Clark 	if (!sysfs)
2073f8ad6018SJames Clark 		return 0;
2074f8ad6018SJames Clark 	return scnprintf(pathname, size, "%s/bus/event_source/devices/", sysfs);
2075f8ad6018SJames Clark }
2076f8ad6018SJames Clark 
perf_pmu__event_source_devices_fd(void)2077e293a5e8SNamhyung Kim int perf_pmu__event_source_devices_fd(void)
2078e293a5e8SNamhyung Kim {
2079e293a5e8SNamhyung Kim 	char path[PATH_MAX];
2080e293a5e8SNamhyung Kim 	const char *sysfs = sysfs__mountpoint();
2081e293a5e8SNamhyung Kim 
2082e293a5e8SNamhyung Kim 	if (!sysfs)
2083e293a5e8SNamhyung Kim 		return -1;
2084e293a5e8SNamhyung Kim 
2085e293a5e8SNamhyung Kim 	scnprintf(path, sizeof(path), "%s/bus/event_source/devices/", sysfs);
2086e293a5e8SNamhyung Kim 	return open(path, O_DIRECTORY);
2087e293a5e8SNamhyung Kim }
2088e293a5e8SNamhyung Kim 
2089f8ad6018SJames Clark /*
2090f8ad6018SJames Clark  * Fill 'buf' with the path to a file or folder in 'pmu_name' in
2091f8ad6018SJames Clark  * sysfs. For example if pmu_name = "cs_etm" and 'filename' = "format"
2092f8ad6018SJames Clark  * then pathname will be filled with
2093f8ad6018SJames Clark  * "/sys/bus/event_source/devices/cs_etm/format"
2094f8ad6018SJames Clark  *
2095e1a3aad3SIan Rogers  * Return 0 if the sysfs mountpoint couldn't be found, if no characters were
2096e1a3aad3SIan Rogers  * written or if the buffer size is exceeded.
2097f8ad6018SJames Clark  */
perf_pmu__pathname_scnprintf(char * buf,size_t size,const char * pmu_name,const char * filename)2098f8ad6018SJames Clark int perf_pmu__pathname_scnprintf(char *buf, size_t size,
2099f8ad6018SJames Clark 				 const char *pmu_name, const char *filename)
2100f8ad6018SJames Clark {
2101e1a3aad3SIan Rogers 	size_t len;
2102f8ad6018SJames Clark 
2103e1a3aad3SIan Rogers 	len = perf_pmu__event_source_devices_scnprintf(buf, size);
2104e1a3aad3SIan Rogers 	if (!len || (len + strlen(pmu_name) + strlen(filename) + 1)  >= size)
2105f8ad6018SJames Clark 		return 0;
2106e1a3aad3SIan Rogers 
2107e1a3aad3SIan Rogers 	return scnprintf(buf + len, size - len, "%s/%s", pmu_name, filename);
2108f8ad6018SJames Clark }
2109eec11310SNamhyung Kim 
perf_pmu__pathname_fd(int dirfd,const char * pmu_name,const char * filename,int flags)2110e293a5e8SNamhyung Kim int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *filename, int flags)
2111e293a5e8SNamhyung Kim {
2112e293a5e8SNamhyung Kim 	char path[PATH_MAX];
2113e293a5e8SNamhyung Kim 
2114e293a5e8SNamhyung Kim 	scnprintf(path, sizeof(path), "%s/%s", pmu_name, filename);
2115e293a5e8SNamhyung Kim 	return openat(dirfd, path, flags);
2116e293a5e8SNamhyung Kim }
2117e293a5e8SNamhyung Kim 
perf_pmu__delete(struct perf_pmu * pmu)21181eaf496eSIan Rogers void perf_pmu__delete(struct perf_pmu *pmu)
2119eec11310SNamhyung Kim {
2120eec11310SNamhyung Kim 	perf_pmu__del_formats(&pmu->format);
2121eec11310SNamhyung Kim 	perf_pmu__del_aliases(pmu);
2122eec11310SNamhyung Kim 	perf_pmu__del_caps(pmu);
2123eec11310SNamhyung Kim 
2124eec11310SNamhyung Kim 	perf_cpu_map__put(pmu->cpus);
2125eec11310SNamhyung Kim 
2126efe98a7aSArnaldo Carvalho de Melo 	zfree(&pmu->default_config);
2127efe98a7aSArnaldo Carvalho de Melo 	zfree(&pmu->name);
2128efe98a7aSArnaldo Carvalho de Melo 	zfree(&pmu->alias_name);
2129b7823045SIan Rogers 	zfree(&pmu->id);
2130eec11310SNamhyung Kim 	free(pmu);
2131eec11310SNamhyung Kim }
2132930e16acSIan Rogers 
perf_pmu__name_from_config(struct perf_pmu * pmu,u64 config)2133930e16acSIan Rogers const char *perf_pmu__name_from_config(struct perf_pmu *pmu, u64 config)
2134930e16acSIan Rogers {
2135930e16acSIan Rogers 	struct perf_pmu_alias *event;
2136930e16acSIan Rogers 
2137930e16acSIan Rogers 	if (!pmu)
2138930e16acSIan Rogers 		return NULL;
2139930e16acSIan Rogers 
21404ac93db3SIan Rogers 	pmu_aliases_parse(pmu);
2141930e16acSIan Rogers 	pmu_add_cpu_aliases(pmu);
2142930e16acSIan Rogers 	list_for_each_entry(event, &pmu->aliases, list) {
2143930e16acSIan Rogers 		struct perf_event_attr attr = {.config = 0,};
2144930e16acSIan Rogers 		int ret = perf_pmu__config(pmu, &attr, &event->terms, NULL);
2145930e16acSIan Rogers 
2146930e16acSIan Rogers 		if (ret == 0 && config == attr.config)
2147930e16acSIan Rogers 			return event->name;
2148930e16acSIan Rogers 	}
2149930e16acSIan Rogers 	return NULL;
2150930e16acSIan Rogers }
2151