xref: /openbmc/linux/tools/perf/util/pmu.c (revision eaaebb01)
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"
31cd82a32eSJiri Olsa 
32c091ee90SIan Rogers struct perf_pmu perf_pmu__fake = {
33c091ee90SIan Rogers 	.name = "fake",
34c091ee90SIan Rogers };
35e46fc8d9SArnaldo Carvalho de Melo 
36c3245d20SIan Rogers #define UNIT_MAX_LEN	31 /* max length for event unit name */
37c3245d20SIan Rogers 
38c3245d20SIan Rogers /**
39c3245d20SIan Rogers  * struct perf_pmu_alias - An event either read from sysfs or builtin in
40c3245d20SIan Rogers  * pmu-events.c, created by parsing the pmu-events json files.
41c3245d20SIan Rogers  */
42c3245d20SIan Rogers struct perf_pmu_alias {
43c3245d20SIan Rogers 	/** @name: Name of the event like "mem-loads". */
44c3245d20SIan Rogers 	char *name;
45c3245d20SIan Rogers 	/** @desc: Optional short description of the event. */
46c3245d20SIan Rogers 	char *desc;
47c3245d20SIan Rogers 	/** @long_desc: Optional long description. */
48c3245d20SIan Rogers 	char *long_desc;
49c3245d20SIan Rogers 	/**
50c3245d20SIan Rogers 	 * @topic: Optional topic such as cache or pipeline, particularly for
51c3245d20SIan Rogers 	 * json events.
52c3245d20SIan Rogers 	 */
53c3245d20SIan Rogers 	char *topic;
54c3245d20SIan Rogers 	/** @terms: Owned list of the original parsed parameters. */
55c3245d20SIan Rogers 	struct list_head terms;
56c3245d20SIan Rogers 	/** @list: List element of struct perf_pmu aliases. */
57c3245d20SIan Rogers 	struct list_head list;
587b723dbbSIan Rogers 	/**
597b723dbbSIan Rogers 	 * @pmu_name: The name copied from the json struct pmu_event. This can
607b723dbbSIan Rogers 	 * differ from the PMU name as it won't have suffixes.
617b723dbbSIan Rogers 	 */
627b723dbbSIan Rogers 	char *pmu_name;
63c3245d20SIan Rogers 	/** @unit: Units for the event, such as bytes or cache lines. */
64c3245d20SIan Rogers 	char unit[UNIT_MAX_LEN+1];
65c3245d20SIan Rogers 	/** @scale: Value to scale read counter values by. */
66c3245d20SIan Rogers 	double scale;
67c3245d20SIan Rogers 	/**
68c3245d20SIan Rogers 	 * @per_pkg: Does the file
69c3245d20SIan Rogers 	 * <sysfs>/bus/event_source/devices/<pmu_name>/events/<name>.per-pkg or
70c3245d20SIan Rogers 	 * equivalent json value exist and have the value 1.
71c3245d20SIan Rogers 	 */
72c3245d20SIan Rogers 	bool per_pkg;
73c3245d20SIan Rogers 	/**
74c3245d20SIan Rogers 	 * @snapshot: Does the file
75c3245d20SIan Rogers 	 * <sysfs>/bus/event_source/devices/<pmu_name>/events/<name>.snapshot
76c3245d20SIan Rogers 	 * exist and have the value 1.
77c3245d20SIan Rogers 	 */
78c3245d20SIan Rogers 	bool snapshot;
79c3245d20SIan Rogers 	/**
80c3245d20SIan Rogers 	 * @deprecated: Is the event hidden and so not shown in perf list by
81c3245d20SIan Rogers 	 * default.
82c3245d20SIan Rogers 	 */
83c3245d20SIan Rogers 	bool deprecated;
847b723dbbSIan Rogers 	/** @from_sysfs: Was the alias from sysfs or a json event? */
857b723dbbSIan Rogers 	bool from_sysfs;
867b723dbbSIan Rogers 	/** @info_loaded: Have the scale, unit and other values been read from disk? */
877b723dbbSIan Rogers 	bool info_loaded;
88c3245d20SIan Rogers };
89c3245d20SIan Rogers 
90fe13d43dSIan Rogers /**
91fe13d43dSIan Rogers  * struct perf_pmu_format - Values from a format file read from
92fe13d43dSIan Rogers  * <sysfs>/devices/cpu/format/ held in struct perf_pmu.
93fe13d43dSIan Rogers  *
94fe13d43dSIan Rogers  * For example, the contents of <sysfs>/devices/cpu/format/event may be
95fe13d43dSIan Rogers  * "config:0-7" and will be represented here as name="event",
96fe13d43dSIan Rogers  * value=PERF_PMU_FORMAT_VALUE_CONFIG and bits 0 to 7 will be set.
97fe13d43dSIan Rogers  */
98ab1bf653SArnaldo Carvalho de Melo struct perf_pmu_format {
9950402641SIan Rogers 	/** @list: Element on list within struct perf_pmu. */
10050402641SIan Rogers 	struct list_head list;
10150402641SIan Rogers 	/** @bits: Which config bits are set by this format value. */
10250402641SIan Rogers 	DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
103fe13d43dSIan Rogers 	/** @name: The modifier/file name. */
104ab1bf653SArnaldo Carvalho de Melo 	char *name;
105fe13d43dSIan Rogers 	/**
106fe13d43dSIan Rogers 	 * @value : Which config value the format relates to. Supported values
107fe13d43dSIan Rogers 	 * are from PERF_PMU_FORMAT_VALUE_CONFIG to
108fe13d43dSIan Rogers 	 * PERF_PMU_FORMAT_VALUE_CONFIG_END.
109fe13d43dSIan Rogers 	 */
11050402641SIan Rogers 	u16 value;
11150402641SIan Rogers 	/** @loaded: Has the contents been loaded/parsed. */
11250402641SIan Rogers 	bool loaded;
113ab1bf653SArnaldo Carvalho de Melo };
114ab1bf653SArnaldo Carvalho de Melo 
1158d4b6d37SIan Rogers static int pmu_aliases_parse(struct perf_pmu *pmu);
1168d4b6d37SIan Rogers 
11750402641SIan Rogers static struct perf_pmu_format *perf_pmu__new_format(struct list_head *list, char *name)
11850402641SIan Rogers {
11950402641SIan Rogers 	struct perf_pmu_format *format;
12050402641SIan Rogers 
12150402641SIan Rogers 	format = zalloc(sizeof(*format));
12250402641SIan Rogers 	if (!format)
12350402641SIan Rogers 		return NULL;
12450402641SIan Rogers 
12550402641SIan Rogers 	format->name = strdup(name);
12650402641SIan Rogers 	if (!format->name) {
12750402641SIan Rogers 		free(format);
12850402641SIan Rogers 		return NULL;
12950402641SIan Rogers 	}
13050402641SIan Rogers 	list_add_tail(&format->list, list);
13150402641SIan Rogers 	return format;
13250402641SIan Rogers }
13350402641SIan Rogers 
13450402641SIan Rogers /* Called at the end of parsing a format. */
13550402641SIan Rogers void perf_pmu_format__set_value(void *vformat, int config, unsigned long *bits)
13650402641SIan Rogers {
13750402641SIan Rogers 	struct perf_pmu_format *format = vformat;
13850402641SIan Rogers 
13950402641SIan Rogers 	format->value = config;
14050402641SIan Rogers 	memcpy(format->bits, bits, sizeof(format->bits));
14150402641SIan Rogers }
14250402641SIan Rogers 
14350402641SIan Rogers static void __perf_pmu_format__load(struct perf_pmu_format *format, FILE *file)
14450402641SIan Rogers {
14550402641SIan Rogers 	void *scanner;
14650402641SIan Rogers 	int ret;
14750402641SIan Rogers 
14850402641SIan Rogers 	ret = perf_pmu_lex_init(&scanner);
14950402641SIan Rogers 	if (ret)
15050402641SIan Rogers 		return;
15150402641SIan Rogers 
15250402641SIan Rogers 	perf_pmu_set_in(file, scanner);
15350402641SIan Rogers 	ret = perf_pmu_parse(format, scanner);
15450402641SIan Rogers 	perf_pmu_lex_destroy(scanner);
15550402641SIan Rogers 	format->loaded = true;
15650402641SIan Rogers }
15750402641SIan Rogers 
15850402641SIan Rogers static void perf_pmu_format__load(struct perf_pmu *pmu, struct perf_pmu_format *format)
15950402641SIan Rogers {
16050402641SIan Rogers 	char path[PATH_MAX];
16150402641SIan Rogers 	FILE *file = NULL;
16250402641SIan Rogers 
16350402641SIan Rogers 	if (format->loaded)
16450402641SIan Rogers 		return;
16550402641SIan Rogers 
16650402641SIan Rogers 	if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, "format"))
16750402641SIan Rogers 		return;
16850402641SIan Rogers 
16950402641SIan Rogers 	assert(strlen(path) + strlen(format->name) + 2 < sizeof(path));
17050402641SIan Rogers 	strcat(path, "/");
17150402641SIan Rogers 	strcat(path, format->name);
17250402641SIan Rogers 
17350402641SIan Rogers 	file = fopen(path, "r");
17450402641SIan Rogers 	if (!file)
17550402641SIan Rogers 		return;
17650402641SIan Rogers 	__perf_pmu_format__load(format, file);
17750402641SIan Rogers 	fclose(file);
17850402641SIan Rogers }
17950402641SIan Rogers 
180cd82a32eSJiri Olsa /*
181cd82a32eSJiri Olsa  * Parse & process all the sysfs attributes located under
182cd82a32eSJiri Olsa  * the directory specified in 'dir' parameter.
183cd82a32eSJiri Olsa  */
18450402641SIan Rogers int perf_pmu__format_parse(struct perf_pmu *pmu, int dirfd, bool eager_load)
185cd82a32eSJiri Olsa {
186cd82a32eSJiri Olsa 	struct dirent *evt_ent;
187cd82a32eSJiri Olsa 	DIR *format_dir;
188cd82a32eSJiri Olsa 	int ret = 0;
189cd82a32eSJiri Olsa 
190e293a5e8SNamhyung Kim 	format_dir = fdopendir(dirfd);
191cd82a32eSJiri Olsa 	if (!format_dir)
192cd82a32eSJiri Olsa 		return -EINVAL;
193cd82a32eSJiri Olsa 
19450402641SIan Rogers 	while ((evt_ent = readdir(format_dir)) != NULL) {
19550402641SIan Rogers 		struct perf_pmu_format *format;
196cd82a32eSJiri Olsa 		char *name = evt_ent->d_name;
197cd82a32eSJiri Olsa 
198cd82a32eSJiri Olsa 		if (!strcmp(name, ".") || !strcmp(name, ".."))
199cd82a32eSJiri Olsa 			continue;
200cd82a32eSJiri Olsa 
20150402641SIan Rogers 		format = perf_pmu__new_format(&pmu->format, name);
20250402641SIan Rogers 		if (!format) {
20350402641SIan Rogers 			ret = -ENOMEM;
204cd82a32eSJiri Olsa 			break;
20550402641SIan Rogers 		}
206cd82a32eSJiri Olsa 
20750402641SIan Rogers 		if (eager_load) {
20850402641SIan Rogers 			FILE *file;
20950402641SIan Rogers 			int fd = openat(dirfd, name, O_RDONLY);
21050402641SIan Rogers 
21150402641SIan Rogers 			if (fd < 0) {
21250402641SIan Rogers 				ret = -errno;
21350402641SIan Rogers 				break;
21450402641SIan Rogers 			}
2153d88aec0SIan Rogers 			file = fdopen(fd, "r");
2163d88aec0SIan Rogers 			if (!file) {
2173d88aec0SIan Rogers 				close(fd);
2183d88aec0SIan Rogers 				break;
2193d88aec0SIan Rogers 			}
22050402641SIan Rogers 			__perf_pmu_format__load(format, file);
2213d88aec0SIan Rogers 			fclose(file);
2223d88aec0SIan Rogers 		}
223cd82a32eSJiri Olsa 	}
224cd82a32eSJiri Olsa 
225cd82a32eSJiri Olsa 	closedir(format_dir);
226cd82a32eSJiri Olsa 	return ret;
227cd82a32eSJiri Olsa }
228cd82a32eSJiri Olsa 
229cd82a32eSJiri Olsa /*
230cd82a32eSJiri Olsa  * Reading/parsing the default pmu format definition, which should be
231cd82a32eSJiri Olsa  * located at:
232cd82a32eSJiri Olsa  * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
233cd82a32eSJiri Olsa  */
234838a8c5fSIan Rogers static int pmu_format(struct perf_pmu *pmu, int dirfd, const char *name)
235cd82a32eSJiri Olsa {
236e293a5e8SNamhyung Kim 	int fd;
237cd82a32eSJiri Olsa 
238e293a5e8SNamhyung Kim 	fd = perf_pmu__pathname_fd(dirfd, name, "format", O_DIRECTORY);
239e293a5e8SNamhyung Kim 	if (fd < 0)
240d9664582SAndi Kleen 		return 0;
241cd82a32eSJiri Olsa 
242e293a5e8SNamhyung Kim 	/* it'll close the fd */
24350402641SIan Rogers 	if (perf_pmu__format_parse(pmu, fd, /*eager_load=*/false))
244cd82a32eSJiri Olsa 		return -1;
245cd82a32eSJiri Olsa 
246cd82a32eSJiri Olsa 	return 0;
247cd82a32eSJiri Olsa }
248cd82a32eSJiri Olsa 
249a55ab7c4SJin Yao int perf_pmu__convert_scale(const char *scale, char **end, double *sval)
250d02fc6bcSAndi Kleen {
251d02fc6bcSAndi Kleen 	char *lc;
252d02fc6bcSAndi Kleen 	int ret = 0;
253d02fc6bcSAndi Kleen 
254d02fc6bcSAndi Kleen 	/*
255d02fc6bcSAndi Kleen 	 * save current locale
256d02fc6bcSAndi Kleen 	 */
257d02fc6bcSAndi Kleen 	lc = setlocale(LC_NUMERIC, NULL);
258d02fc6bcSAndi Kleen 
259d02fc6bcSAndi Kleen 	/*
260d02fc6bcSAndi Kleen 	 * The lc string may be allocated in static storage,
261d02fc6bcSAndi Kleen 	 * so get a dynamic copy to make it survive setlocale
262d02fc6bcSAndi Kleen 	 * call below.
263d02fc6bcSAndi Kleen 	 */
264d02fc6bcSAndi Kleen 	lc = strdup(lc);
265d02fc6bcSAndi Kleen 	if (!lc) {
266d02fc6bcSAndi Kleen 		ret = -ENOMEM;
267d02fc6bcSAndi Kleen 		goto out;
268d02fc6bcSAndi Kleen 	}
269d02fc6bcSAndi Kleen 
270d02fc6bcSAndi Kleen 	/*
271d02fc6bcSAndi Kleen 	 * force to C locale to ensure kernel
272d02fc6bcSAndi Kleen 	 * scale string is converted correctly.
273d02fc6bcSAndi Kleen 	 * kernel uses default C locale.
274d02fc6bcSAndi Kleen 	 */
275d02fc6bcSAndi Kleen 	setlocale(LC_NUMERIC, "C");
276d02fc6bcSAndi Kleen 
277d02fc6bcSAndi Kleen 	*sval = strtod(scale, end);
278d02fc6bcSAndi Kleen 
279d02fc6bcSAndi Kleen out:
280d02fc6bcSAndi Kleen 	/* restore locale */
281d02fc6bcSAndi Kleen 	setlocale(LC_NUMERIC, lc);
282d02fc6bcSAndi Kleen 	free(lc);
283d02fc6bcSAndi Kleen 	return ret;
284d02fc6bcSAndi Kleen }
285d02fc6bcSAndi Kleen 
2867b723dbbSIan Rogers static int perf_pmu__parse_scale(struct perf_pmu *pmu, struct perf_pmu_alias *alias)
287410136f5SStephane Eranian {
288410136f5SStephane Eranian 	struct stat st;
289410136f5SStephane Eranian 	ssize_t sret;
2907b723dbbSIan Rogers 	size_t len;
291410136f5SStephane Eranian 	char scale[128];
292410136f5SStephane Eranian 	int fd, ret = -1;
293410136f5SStephane Eranian 	char path[PATH_MAX];
294410136f5SStephane Eranian 
2957b723dbbSIan Rogers 	len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
2967b723dbbSIan Rogers 	if (!len)
2977b723dbbSIan Rogers 		return 0;
2987b723dbbSIan Rogers 	scnprintf(path + len, sizeof(path) - len, "%s/%s.scale", pmu->name, alias->name);
299410136f5SStephane Eranian 
3007b723dbbSIan Rogers 	fd = open(path, O_RDONLY);
301410136f5SStephane Eranian 	if (fd == -1)
302410136f5SStephane Eranian 		return -1;
303410136f5SStephane Eranian 
304410136f5SStephane Eranian 	if (fstat(fd, &st) < 0)
305410136f5SStephane Eranian 		goto error;
306410136f5SStephane Eranian 
307410136f5SStephane Eranian 	sret = read(fd, scale, sizeof(scale)-1);
308410136f5SStephane Eranian 	if (sret < 0)
309410136f5SStephane Eranian 		goto error;
310410136f5SStephane Eranian 
3119ecae065SMadhavan Srinivasan 	if (scale[sret - 1] == '\n')
3129ecae065SMadhavan Srinivasan 		scale[sret - 1] = '\0';
3139ecae065SMadhavan Srinivasan 	else
314410136f5SStephane Eranian 		scale[sret] = '\0';
3159ecae065SMadhavan Srinivasan 
316a55ab7c4SJin Yao 	ret = perf_pmu__convert_scale(scale, NULL, &alias->scale);
317410136f5SStephane Eranian error:
318410136f5SStephane Eranian 	close(fd);
319410136f5SStephane Eranian 	return ret;
320410136f5SStephane Eranian }
321410136f5SStephane Eranian 
3227b723dbbSIan Rogers static int perf_pmu__parse_unit(struct perf_pmu *pmu, struct perf_pmu_alias *alias)
323410136f5SStephane Eranian {
324410136f5SStephane Eranian 	char path[PATH_MAX];
3257b723dbbSIan Rogers 	size_t len;
326410136f5SStephane Eranian 	ssize_t sret;
327410136f5SStephane Eranian 	int fd;
328410136f5SStephane Eranian 
329410136f5SStephane Eranian 
3307b723dbbSIan Rogers 	len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
3317b723dbbSIan Rogers 	if (!len)
3327b723dbbSIan Rogers 		return 0;
3337b723dbbSIan Rogers 	scnprintf(path + len, sizeof(path) - len, "%s/%s.unit", pmu->name, alias->name);
3347b723dbbSIan Rogers 
3357b723dbbSIan Rogers 	fd = open(path, O_RDONLY);
336410136f5SStephane Eranian 	if (fd == -1)
337410136f5SStephane Eranian 		return -1;
338410136f5SStephane Eranian 
339410136f5SStephane Eranian 	sret = read(fd, alias->unit, UNIT_MAX_LEN);
340410136f5SStephane Eranian 	if (sret < 0)
341410136f5SStephane Eranian 		goto error;
342410136f5SStephane Eranian 
343410136f5SStephane Eranian 	close(fd);
344410136f5SStephane Eranian 
3459ecae065SMadhavan Srinivasan 	if (alias->unit[sret - 1] == '\n')
3469ecae065SMadhavan Srinivasan 		alias->unit[sret - 1] = '\0';
3479ecae065SMadhavan Srinivasan 	else
348410136f5SStephane Eranian 		alias->unit[sret] = '\0';
349410136f5SStephane Eranian 
350410136f5SStephane Eranian 	return 0;
351410136f5SStephane Eranian error:
352410136f5SStephane Eranian 	close(fd);
353410136f5SStephane Eranian 	alias->unit[0] = '\0';
354410136f5SStephane Eranian 	return -1;
355410136f5SStephane Eranian }
356410136f5SStephane Eranian 
357044330c1SMatt Fleming static int
3587b723dbbSIan Rogers perf_pmu__parse_per_pkg(struct perf_pmu *pmu, struct perf_pmu_alias *alias)
359044330c1SMatt Fleming {
360044330c1SMatt Fleming 	char path[PATH_MAX];
3617b723dbbSIan Rogers 	size_t len;
362044330c1SMatt Fleming 	int fd;
363044330c1SMatt Fleming 
3647b723dbbSIan Rogers 	len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
3657b723dbbSIan Rogers 	if (!len)
3667b723dbbSIan Rogers 		return 0;
3677b723dbbSIan Rogers 	scnprintf(path + len, sizeof(path) - len, "%s/%s.per-pkg", pmu->name, alias->name);
368044330c1SMatt Fleming 
3697b723dbbSIan Rogers 	fd = open(path, O_RDONLY);
370044330c1SMatt Fleming 	if (fd == -1)
371044330c1SMatt Fleming 		return -1;
372044330c1SMatt Fleming 
373044330c1SMatt Fleming 	close(fd);
374044330c1SMatt Fleming 
375044330c1SMatt Fleming 	alias->per_pkg = true;
376044330c1SMatt Fleming 	return 0;
377044330c1SMatt Fleming }
378044330c1SMatt Fleming 
3797b723dbbSIan Rogers static int perf_pmu__parse_snapshot(struct perf_pmu *pmu, struct perf_pmu_alias *alias)
3801d9e446bSJiri Olsa {
3811d9e446bSJiri Olsa 	char path[PATH_MAX];
3827b723dbbSIan Rogers 	size_t len;
3831d9e446bSJiri Olsa 	int fd;
3841d9e446bSJiri Olsa 
3857b723dbbSIan Rogers 	len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
3867b723dbbSIan Rogers 	if (!len)
3877b723dbbSIan Rogers 		return 0;
3887b723dbbSIan Rogers 	scnprintf(path + len, sizeof(path) - len, "%s/%s.snapshot", pmu->name, alias->name);
3891d9e446bSJiri Olsa 
3907b723dbbSIan Rogers 	fd = open(path, O_RDONLY);
3911d9e446bSJiri Olsa 	if (fd == -1)
3921d9e446bSJiri Olsa 		return -1;
3931d9e446bSJiri Olsa 
3941d9e446bSJiri Olsa 	alias->snapshot = true;
3951d9e446bSJiri Olsa 	close(fd);
3961d9e446bSJiri Olsa 	return 0;
3971d9e446bSJiri Olsa }
3981d9e446bSJiri Olsa 
3996dde6429SThomas Richter /* Delete an alias entry. */
400c3245d20SIan Rogers static void perf_pmu_free_alias(struct perf_pmu_alias *newalias)
4016dde6429SThomas Richter {
4026dde6429SThomas Richter 	zfree(&newalias->name);
4036dde6429SThomas Richter 	zfree(&newalias->desc);
4046dde6429SThomas Richter 	zfree(&newalias->long_desc);
4056dde6429SThomas Richter 	zfree(&newalias->topic);
40632705de7SJin Yao 	zfree(&newalias->pmu_name);
4076dde6429SThomas Richter 	parse_events_terms__purge(&newalias->terms);
4086dde6429SThomas Richter 	free(newalias);
4096dde6429SThomas Richter }
4106dde6429SThomas Richter 
411eec11310SNamhyung Kim static void perf_pmu__del_aliases(struct perf_pmu *pmu)
412eec11310SNamhyung Kim {
413eec11310SNamhyung Kim 	struct perf_pmu_alias *alias, *tmp;
414eec11310SNamhyung Kim 
415eec11310SNamhyung Kim 	list_for_each_entry_safe(alias, tmp, &pmu->aliases, list) {
416eec11310SNamhyung Kim 		list_del(&alias->list);
417eec11310SNamhyung Kim 		perf_pmu_free_alias(alias);
418eec11310SNamhyung Kim 	}
419eec11310SNamhyung Kim }
420eec11310SNamhyung Kim 
4218d4b6d37SIan Rogers static struct perf_pmu_alias *perf_pmu__find_alias(struct perf_pmu *pmu,
4228d4b6d37SIan Rogers 						   const char *name,
4238d4b6d37SIan Rogers 						   bool load)
4246dde6429SThomas Richter {
425f63a536fSIan Rogers 	struct perf_pmu_alias *alias;
4266dde6429SThomas Richter 
4278d4b6d37SIan Rogers 	if (load && !pmu->sysfs_aliases_loaded)
4288d4b6d37SIan Rogers 		pmu_aliases_parse(pmu);
4298d4b6d37SIan Rogers 
430f63a536fSIan Rogers 	list_for_each_entry(alias, &pmu->aliases, list) {
431f63a536fSIan Rogers 		if (!strcasecmp(alias->name, name))
432f63a536fSIan Rogers 			return alias;
43332705de7SJin Yao 	}
434f63a536fSIan Rogers 	return NULL;
435f63a536fSIan Rogers }
436f63a536fSIan Rogers 
437f63a536fSIan Rogers static bool assign_str(const char *name, const char *field, char **old_str,
438f63a536fSIan Rogers 				const char *new_str)
439f63a536fSIan Rogers {
440f63a536fSIan Rogers 	if (!*old_str && new_str) {
441f63a536fSIan Rogers 		*old_str = strdup(new_str);
4426dde6429SThomas Richter 		return true;
4436dde6429SThomas Richter 	}
444f63a536fSIan Rogers 
445f63a536fSIan Rogers 	if (!new_str || !strcasecmp(*old_str, new_str))
446f63a536fSIan Rogers 		return false; /* Nothing to update. */
447f63a536fSIan Rogers 
448f63a536fSIan Rogers 	pr_debug("alias %s differs in field '%s' ('%s' != '%s')\n",
449f63a536fSIan Rogers 		name, field, *old_str, new_str);
450f63a536fSIan Rogers 	zfree(old_str);
451f63a536fSIan Rogers 	*old_str = strdup(new_str);
452f63a536fSIan Rogers 	return true;
4536dde6429SThomas Richter }
454f63a536fSIan Rogers 
4557b723dbbSIan Rogers static void read_alias_info(struct perf_pmu *pmu, struct perf_pmu_alias *alias)
4567b723dbbSIan Rogers {
4577b723dbbSIan Rogers 	if (!alias->from_sysfs || alias->info_loaded)
4587b723dbbSIan Rogers 		return;
4597b723dbbSIan Rogers 
4607b723dbbSIan Rogers 	/*
4617b723dbbSIan Rogers 	 * load unit name and scale if available
4627b723dbbSIan Rogers 	 */
4637b723dbbSIan Rogers 	perf_pmu__parse_unit(pmu, alias);
4647b723dbbSIan Rogers 	perf_pmu__parse_scale(pmu, alias);
4657b723dbbSIan Rogers 	perf_pmu__parse_per_pkg(pmu, alias);
4667b723dbbSIan Rogers 	perf_pmu__parse_snapshot(pmu, alias);
4677b723dbbSIan Rogers }
4687b723dbbSIan Rogers 
4697b723dbbSIan Rogers struct update_alias_data {
4707b723dbbSIan Rogers 	struct perf_pmu *pmu;
4717b723dbbSIan Rogers 	struct perf_pmu_alias *alias;
4727b723dbbSIan Rogers };
4737b723dbbSIan Rogers 
474f63a536fSIan Rogers static int update_alias(const struct pmu_event *pe,
475f63a536fSIan Rogers 			const struct pmu_events_table *table __maybe_unused,
476f63a536fSIan Rogers 			void *vdata)
477f63a536fSIan Rogers {
4787b723dbbSIan Rogers 	struct update_alias_data *data = vdata;
479f63a536fSIan Rogers 	int ret = 0;
480f63a536fSIan Rogers 
4817b723dbbSIan Rogers 	read_alias_info(data->pmu, data->alias);
4827b723dbbSIan Rogers 	assign_str(pe->name, "desc", &data->alias->desc, pe->desc);
4837b723dbbSIan Rogers 	assign_str(pe->name, "long_desc", &data->alias->long_desc, pe->long_desc);
4847b723dbbSIan Rogers 	assign_str(pe->name, "topic", &data->alias->topic, pe->topic);
4857b723dbbSIan Rogers 	data->alias->per_pkg = pe->perpkg;
48630f0b435SIan Rogers 	if (pe->event) {
4877b723dbbSIan Rogers 		parse_events_terms__purge(&data->alias->terms);
4887b723dbbSIan Rogers 		ret = parse_events_terms(&data->alias->terms, pe->event, /*input=*/NULL);
489f63a536fSIan Rogers 	}
490f63a536fSIan Rogers 	if (!ret && pe->unit) {
491f63a536fSIan Rogers 		char *unit;
492f63a536fSIan Rogers 
4937b723dbbSIan Rogers 		ret = perf_pmu__convert_scale(pe->unit, &unit, &data->alias->scale);
494f63a536fSIan Rogers 		if (!ret)
4957b723dbbSIan Rogers 			snprintf(data->alias->unit, sizeof(data->alias->unit), "%s", unit);
496f63a536fSIan Rogers 	}
497f63a536fSIan Rogers 	return ret;
4986dde6429SThomas Richter }
4996dde6429SThomas Richter 
5007b723dbbSIan Rogers static int perf_pmu__new_alias(struct perf_pmu *pmu, const char *name,
501edb217ffSIan Rogers 				const char *desc, const char *val, FILE *val_fd,
502edb217ffSIan Rogers 				const struct pmu_event *pe)
503a6146d50SZheng Yan {
5045c6ccc37SArnaldo Carvalho de Melo 	struct perf_pmu_alias *alias;
505a6146d50SZheng Yan 	int ret;
506330f40a0SIan Rogers 	const char *long_desc = NULL, *topic = NULL, *unit = NULL, *pmu_name = NULL;
507bd680861SIan Rogers 	bool deprecated = false, perpkg = false;
508eab35953SJin Yao 
5098d4b6d37SIan Rogers 	if (perf_pmu__find_alias(pmu, name, /*load=*/ false)) {
510f63a536fSIan Rogers 		/* Alias was already created/loaded. */
511f63a536fSIan Rogers 		return 0;
512f63a536fSIan Rogers 	}
513f63a536fSIan Rogers 
514eab35953SJin Yao 	if (pe) {
515330f40a0SIan Rogers 		long_desc = pe->long_desc;
516330f40a0SIan Rogers 		topic = pe->topic;
517330f40a0SIan Rogers 		unit = pe->unit;
518bd680861SIan Rogers 		perpkg = pe->perpkg;
5199ed8b7dcSIan Rogers 		deprecated = pe->deprecated;
520330f40a0SIan Rogers 		pmu_name = pe->pmu;
521eab35953SJin Yao 	}
522a6146d50SZheng Yan 
523*eaaebb01SIan Rogers 	alias = zalloc(sizeof(*alias));
524a6146d50SZheng Yan 	if (!alias)
525a6146d50SZheng Yan 		return -ENOMEM;
526a6146d50SZheng Yan 
527a6146d50SZheng Yan 	INIT_LIST_HEAD(&alias->terms);
528410136f5SStephane Eranian 	alias->scale = 1.0;
529410136f5SStephane Eranian 	alias->unit[0] = '\0';
530bd680861SIan Rogers 	alias->per_pkg = perpkg;
53184530920SStephane Eranian 	alias->snapshot = false;
5329ed8b7dcSIan Rogers 	alias->deprecated = deprecated;
533410136f5SStephane Eranian 
534edb217ffSIan Rogers 	ret = parse_events_terms(&alias->terms, val, val_fd);
535a6146d50SZheng Yan 	if (ret) {
53670c646e0SSukadev Bhattiprolu 		pr_err("Cannot parse alias %s: %d\n", val, ret);
537a6146d50SZheng Yan 		free(alias);
538a6146d50SZheng Yan 		return ret;
539a6146d50SZheng Yan 	}
540a6146d50SZheng Yan 
541a6146d50SZheng Yan 	alias->name = strdup(name);
54208e60ed1SAndi Kleen 	alias->desc = desc ? strdup(desc) : NULL;
543c8d6828aSSukadev Bhattiprolu 	alias->long_desc = long_desc ? strdup(long_desc) :
544c8d6828aSSukadev Bhattiprolu 				desc ? strdup(desc) : NULL;
545dd5f1036SAndi Kleen 	alias->topic = topic ? strdup(topic) : NULL;
546f63a536fSIan Rogers 	alias->pmu_name = pmu_name ? strdup(pmu_name) : NULL;
547fedb2b51SAndi Kleen 	if (unit) {
548f63a536fSIan Rogers 		if (perf_pmu__convert_scale(unit, (char **)&unit, &alias->scale) < 0) {
549f63a536fSIan Rogers 			perf_pmu_free_alias(alias);
550fedb2b51SAndi Kleen 			return -1;
551f63a536fSIan Rogers 		}
552fedb2b51SAndi Kleen 		snprintf(alias->unit, sizeof(alias->unit), "%s", unit);
553fedb2b51SAndi Kleen 	}
554f63a536fSIan Rogers 	if (!pe) {
555f63a536fSIan Rogers 		/* Update an event from sysfs with json data. */
5567b723dbbSIan Rogers 		struct update_alias_data data = {
5577b723dbbSIan Rogers 			.pmu = pmu,
5587b723dbbSIan Rogers 			.alias = alias,
5597b723dbbSIan Rogers 		};
5607b723dbbSIan Rogers 
5617b723dbbSIan Rogers 		alias->from_sysfs = true;
5627c52f10cSIan Rogers 		if (pmu->events_table) {
563e6ff1eedSIan Rogers 			if (pmu_events_table__find_event(pmu->events_table, pmu, name,
5647b723dbbSIan Rogers 							 update_alias, &data) == 0)
565e6ff1eedSIan Rogers 				pmu->loaded_json_aliases++;
5667c52f10cSIan Rogers 		}
567f63a536fSIan Rogers 	}
568f63a536fSIan Rogers 
569e6ff1eedSIan Rogers 	if (!pe)
570e6ff1eedSIan Rogers 		pmu->sysfs_aliases++;
571e6ff1eedSIan Rogers 	else
572e6ff1eedSIan Rogers 		pmu->loaded_json_aliases++;
573f26d22f1SIan Rogers 	list_add_tail(&alias->list, &pmu->aliases);
574a6146d50SZheng Yan 	return 0;
575a6146d50SZheng Yan }
576a6146d50SZheng Yan 
57746441bdcSMatt Fleming static inline bool pmu_alias_info_file(char *name)
57846441bdcSMatt Fleming {
57946441bdcSMatt Fleming 	size_t len;
58046441bdcSMatt Fleming 
58146441bdcSMatt Fleming 	len = strlen(name);
58246441bdcSMatt Fleming 	if (len > 5 && !strcmp(name + len - 5, ".unit"))
58346441bdcSMatt Fleming 		return true;
58446441bdcSMatt Fleming 	if (len > 6 && !strcmp(name + len - 6, ".scale"))
58546441bdcSMatt Fleming 		return true;
586044330c1SMatt Fleming 	if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
587044330c1SMatt Fleming 		return true;
5881d9e446bSJiri Olsa 	if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
5891d9e446bSJiri Olsa 		return true;
59046441bdcSMatt Fleming 
59146441bdcSMatt Fleming 	return false;
59246441bdcSMatt Fleming }
59346441bdcSMatt Fleming 
594a6146d50SZheng Yan /*
5958d4b6d37SIan Rogers  * Reading the pmu event aliases definition, which should be located at:
5968d4b6d37SIan Rogers  * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
597a6146d50SZheng Yan  */
5988d4b6d37SIan Rogers static int pmu_aliases_parse(struct perf_pmu *pmu)
599a6146d50SZheng Yan {
6008d4b6d37SIan Rogers 	char path[PATH_MAX];
601a6146d50SZheng Yan 	struct dirent *evt_ent;
602a6146d50SZheng Yan 	DIR *event_dir;
6038d4b6d37SIan Rogers 	size_t len;
6048d4b6d37SIan Rogers 	int fd, dir_fd;
605a6146d50SZheng Yan 
6068d4b6d37SIan Rogers 	len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
6078d4b6d37SIan Rogers 	if (!len)
6088d4b6d37SIan Rogers 		return 0;
6098d4b6d37SIan Rogers 	scnprintf(path + len, sizeof(path) - len, "%s/events", pmu->name);
6108d4b6d37SIan Rogers 
6118d4b6d37SIan Rogers 	dir_fd = open(path, O_DIRECTORY);
6128d4b6d37SIan Rogers 	if (dir_fd == -1) {
6138d4b6d37SIan Rogers 		pmu->sysfs_aliases_loaded = true;
6148d4b6d37SIan Rogers 		return 0;
6158d4b6d37SIan Rogers 	}
6168d4b6d37SIan Rogers 
6178d4b6d37SIan Rogers 	event_dir = fdopendir(dir_fd);
6188d4b6d37SIan Rogers 	if (!event_dir){
6198d4b6d37SIan Rogers 		close (dir_fd);
620a6146d50SZheng Yan 		return -EINVAL;
6218d4b6d37SIan Rogers 	}
622a6146d50SZheng Yan 
623940db6dcSAndi Kleen 	while ((evt_ent = readdir(event_dir))) {
624a6146d50SZheng Yan 		char *name = evt_ent->d_name;
625a6146d50SZheng Yan 		FILE *file;
626a6146d50SZheng Yan 
627a6146d50SZheng Yan 		if (!strcmp(name, ".") || !strcmp(name, ".."))
628a6146d50SZheng Yan 			continue;
629a6146d50SZheng Yan 
630410136f5SStephane Eranian 		/*
63146441bdcSMatt Fleming 		 * skip info files parsed in perf_pmu__new_alias()
632410136f5SStephane Eranian 		 */
63346441bdcSMatt Fleming 		if (pmu_alias_info_file(name))
634410136f5SStephane Eranian 			continue;
635410136f5SStephane Eranian 
6368d4b6d37SIan Rogers 		fd = openat(dir_fd, name, O_RDONLY);
6370ea8920eSIan Rogers 		if (fd == -1) {
6380ea8920eSIan Rogers 			pr_debug("Cannot open %s\n", name);
6390ea8920eSIan Rogers 			continue;
6400ea8920eSIan Rogers 		}
641e293a5e8SNamhyung Kim 		file = fdopen(fd, "r");
642940db6dcSAndi Kleen 		if (!file) {
6430ea8920eSIan Rogers 			close(fd);
644940db6dcSAndi Kleen 			continue;
645940db6dcSAndi Kleen 		}
646410136f5SStephane Eranian 
6477b723dbbSIan Rogers 		if (perf_pmu__new_alias(pmu, name, /*desc=*/ NULL,
648edb217ffSIan Rogers 					/*val=*/ NULL, file, /*pe=*/ NULL) < 0)
649940db6dcSAndi Kleen 			pr_debug("Cannot set up %s\n", name);
650a6146d50SZheng Yan 		fclose(file);
651a6146d50SZheng Yan 	}
652a6146d50SZheng Yan 
653a6146d50SZheng Yan 	closedir(event_dir);
6548d4b6d37SIan Rogers 	close (dir_fd);
6558d4b6d37SIan Rogers 	pmu->sysfs_aliases_loaded = true;
656a6146d50SZheng Yan 	return 0;
657a6146d50SZheng Yan }
658a6146d50SZheng Yan 
6595c6ccc37SArnaldo Carvalho de Melo static int pmu_alias_terms(struct perf_pmu_alias *alias,
660a6146d50SZheng Yan 			   struct list_head *terms)
661a6146d50SZheng Yan {
6627c2f8164SJiri Olsa 	struct parse_events_term *term, *cloned;
663a6146d50SZheng Yan 	LIST_HEAD(list);
664a6146d50SZheng Yan 	int ret;
665a6146d50SZheng Yan 
666a6146d50SZheng Yan 	list_for_each_entry(term, &alias->terms, list) {
6677c2f8164SJiri Olsa 		ret = parse_events_term__clone(&cloned, term);
668a6146d50SZheng Yan 		if (ret) {
669682dc24cSArnaldo Carvalho de Melo 			parse_events_terms__purge(&list);
670a6146d50SZheng Yan 			return ret;
671a6146d50SZheng Yan 		}
672c2f1ceadSAndi Kleen 		/*
673c2f1ceadSAndi Kleen 		 * Weak terms don't override command line options,
674c2f1ceadSAndi Kleen 		 * which we don't want for implicit terms in aliases.
675c2f1ceadSAndi Kleen 		 */
676c2f1ceadSAndi Kleen 		cloned->weak = true;
6777c2f8164SJiri Olsa 		list_add_tail(&cloned->list, &list);
678a6146d50SZheng Yan 	}
679a6146d50SZheng Yan 	list_splice(&list, terms);
680a6146d50SZheng Yan 	return 0;
681a6146d50SZheng Yan }
682a6146d50SZheng Yan 
68366ec1191SMark Rutland /*
68466ec1191SMark Rutland  * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64)
68566ec1191SMark Rutland  * may have a "cpus" file.
68666ec1191SMark Rutland  */
687d06593aaSIan Rogers static struct perf_cpu_map *pmu_cpumask(int dirfd, const char *name, bool is_core)
6887ae92e74SYan, Zheng {
689f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
6907e3fcffeSMark Rutland 	const char *templates[] = {
691d50a79cdSJames Clark 		"cpumask",
692d50a79cdSJames Clark 		"cpus",
6937e3fcffeSMark Rutland 		NULL
6947e3fcffeSMark Rutland 	};
6957e3fcffeSMark Rutland 	const char **template;
696d50a79cdSJames Clark 	char pmu_name[PATH_MAX];
697d50a79cdSJames Clark 	struct perf_pmu pmu = {.name = pmu_name};
698d50a79cdSJames Clark 	FILE *file;
6997ae92e74SYan, Zheng 
700d50a79cdSJames Clark 	strlcpy(pmu_name, name, sizeof(pmu_name));
7017e3fcffeSMark Rutland 	for (template = templates; *template; template++) {
7023a69672eSNamhyung Kim 		file = perf_pmu__open_file_at(&pmu, dirfd, *template);
703d50a79cdSJames Clark 		if (!file)
704d50a79cdSJames Clark 			continue;
705d50a79cdSJames Clark 		cpus = perf_cpu_map__read(file);
7063a69672eSNamhyung Kim 		fclose(file);
70766ec1191SMark Rutland 		if (cpus)
70866ec1191SMark Rutland 			return cpus;
7097e3fcffeSMark Rutland 	}
7107ae92e74SYan, Zheng 
711d06593aaSIan Rogers 	/* Nothing found, for core PMUs assume this means all CPUs. */
712d06593aaSIan Rogers 	return is_core ? perf_cpu_map__get(cpu_map__online()) : NULL;
71366ec1191SMark Rutland }
7147ae92e74SYan, Zheng 
7153a69672eSNamhyung Kim static bool pmu_is_uncore(int dirfd, const char *name)
71666ec1191SMark Rutland {
7173a69672eSNamhyung Kim 	int fd;
7187ae92e74SYan, Zheng 
7193a69672eSNamhyung Kim 	fd = perf_pmu__pathname_fd(dirfd, name, "cpumask", O_PATH);
7203a69672eSNamhyung Kim 	if (fd < 0)
7213a69672eSNamhyung Kim 		return false;
7223a69672eSNamhyung Kim 
7233a69672eSNamhyung Kim 	close(fd);
7243a69672eSNamhyung Kim 	return true;
7257ae92e74SYan, Zheng }
7267ae92e74SYan, Zheng 
72751d54847SJohn Garry static char *pmu_id(const char *name)
72851d54847SJohn Garry {
72951d54847SJohn Garry 	char path[PATH_MAX], *str;
73051d54847SJohn Garry 	size_t len;
73151d54847SJohn Garry 
7325f2c8efaSJames Clark 	perf_pmu__pathname_scnprintf(path, sizeof(path), name, "identifier");
73351d54847SJohn Garry 
7345f2c8efaSJames Clark 	if (filename__read_str(path, &str, &len) < 0)
73551d54847SJohn Garry 		return NULL;
73651d54847SJohn Garry 
73751d54847SJohn Garry 	str[len - 1] = 0; /* remove line feed */
73851d54847SJohn Garry 
73951d54847SJohn Garry 	return str;
74051d54847SJohn Garry }
74151d54847SJohn Garry 
7424bf7e81aSIan Rogers /**
7434bf7e81aSIan Rogers  * is_sysfs_pmu_core() - PMU CORE devices have different name other than cpu in
7444bf7e81aSIan Rogers  *         sysfs on some platforms like ARM or Intel hybrid. Looking for
7454bf7e81aSIan Rogers  *         possible the cpus file in sysfs files to identify whether this is a
7464bf7e81aSIan Rogers  *         core device.
7474bf7e81aSIan Rogers  * @name: The PMU name such as "cpu_atom".
74814b22ae0SGanapatrao Kulkarni  */
7494bf7e81aSIan Rogers static int is_sysfs_pmu_core(const char *name)
75014b22ae0SGanapatrao Kulkarni {
75114b22ae0SGanapatrao Kulkarni 	char path[PATH_MAX];
75214b22ae0SGanapatrao Kulkarni 
753f8ad6018SJames Clark 	if (!perf_pmu__pathname_scnprintf(path, sizeof(path), name, "cpus"))
75414b22ae0SGanapatrao Kulkarni 		return 0;
755d9664582SAndi Kleen 	return file_available(path);
75614b22ae0SGanapatrao Kulkarni }
75714b22ae0SGanapatrao Kulkarni 
75829be2fe0SIan Rogers char *perf_pmu__getcpuid(struct perf_pmu *pmu)
759d77ade9fSAndi Kleen {
760d77ade9fSAndi Kleen 	char *cpuid;
761d77ade9fSAndi Kleen 	static bool printed;
762d77ade9fSAndi Kleen 
763d77ade9fSAndi Kleen 	cpuid = getenv("PERF_CPUID");
764d77ade9fSAndi Kleen 	if (cpuid)
765d77ade9fSAndi Kleen 		cpuid = strdup(cpuid);
766d77ade9fSAndi Kleen 	if (!cpuid)
76754e32dc0SGanapatrao Kulkarni 		cpuid = get_cpuid_str(pmu);
768d77ade9fSAndi Kleen 	if (!cpuid)
769d77ade9fSAndi Kleen 		return NULL;
770d77ade9fSAndi Kleen 
771d77ade9fSAndi Kleen 	if (!printed) {
772d77ade9fSAndi Kleen 		pr_debug("Using CPUID %s\n", cpuid);
773d77ade9fSAndi Kleen 		printed = true;
774d77ade9fSAndi Kleen 	}
775d77ade9fSAndi Kleen 	return cpuid;
776d77ade9fSAndi Kleen }
777d77ade9fSAndi Kleen 
7781ba3752aSIan Rogers __weak const struct pmu_events_table *pmu_events_table__find(void)
779e126bef5SJohn Garry {
78096d2a746SIan Rogers 	return perf_pmu__find_events_table(NULL);
781e126bef5SJohn Garry }
782e126bef5SJohn Garry 
783f8ea2c15SIan Rogers __weak const struct pmu_metrics_table *pmu_metrics_table__find(void)
784f8ea2c15SIan Rogers {
785f8ea2c15SIan Rogers 	return perf_pmu__find_metrics_table(NULL);
786f8ea2c15SIan Rogers }
787f8ea2c15SIan Rogers 
788240e6fd0SIan Rogers /**
789240e6fd0SIan Rogers  * perf_pmu__match_ignoring_suffix - Does the pmu_name match tok ignoring any
790240e6fd0SIan Rogers  *                                   trailing suffix? The Suffix must be in form
791240e6fd0SIan Rogers  *                                   tok_{digits}, or tok{digits}.
792240e6fd0SIan Rogers  * @pmu_name: The pmu_name with possible suffix.
793240e6fd0SIan Rogers  * @tok: The possible match to pmu_name without suffix.
794c07d5c92SJohn Garry  */
795240e6fd0SIan Rogers static bool perf_pmu__match_ignoring_suffix(const char *pmu_name, const char *tok)
796c47a5599SJin Yao {
797c07d5c92SJohn Garry 	const char *p;
798c47a5599SJin Yao 
799c47a5599SJin Yao 	if (strncmp(pmu_name, tok, strlen(tok)))
800c47a5599SJin Yao 		return false;
801c47a5599SJin Yao 
802c47a5599SJin Yao 	p = pmu_name + strlen(tok);
803c47a5599SJin Yao 	if (*p == 0)
804c47a5599SJin Yao 		return true;
805c47a5599SJin Yao 
806c07d5c92SJohn Garry 	if (*p == '_')
807c47a5599SJin Yao 		++p;
808c07d5c92SJohn Garry 
809c07d5c92SJohn Garry 	/* Ensure we end in a number */
810c07d5c92SJohn Garry 	while (1) {
811c07d5c92SJohn Garry 		if (!isdigit(*p))
812c47a5599SJin Yao 			return false;
813c07d5c92SJohn Garry 		if (*(++p) == 0)
814c07d5c92SJohn Garry 			break;
815c07d5c92SJohn Garry 	}
816c47a5599SJin Yao 
817c47a5599SJin Yao 	return true;
818c47a5599SJin Yao }
819c47a5599SJin Yao 
820240e6fd0SIan Rogers /**
821240e6fd0SIan Rogers  * pmu_uncore_alias_match - does name match the PMU name?
822240e6fd0SIan Rogers  * @pmu_name: the json struct pmu_event name. This may lack a suffix (which
823240e6fd0SIan Rogers  *            matches) or be of the form "socket,pmuname" which will match
824240e6fd0SIan Rogers  *            "socketX_pmunameY".
825240e6fd0SIan Rogers  * @name: a real full PMU name as from sysfs.
826240e6fd0SIan Rogers  */
827240e6fd0SIan Rogers static bool pmu_uncore_alias_match(const char *pmu_name, const char *name)
828730670b1SJohn Garry {
829730670b1SJohn Garry 	char *tmp = NULL, *tok, *str;
830730670b1SJohn Garry 	bool res;
831730670b1SJohn Garry 
832240e6fd0SIan Rogers 	if (strchr(pmu_name, ',') == NULL)
833240e6fd0SIan Rogers 		return perf_pmu__match_ignoring_suffix(name, pmu_name);
834240e6fd0SIan Rogers 
835730670b1SJohn Garry 	str = strdup(pmu_name);
836730670b1SJohn Garry 	if (!str)
837730670b1SJohn Garry 		return false;
838730670b1SJohn Garry 
839730670b1SJohn Garry 	/*
840730670b1SJohn Garry 	 * uncore alias may be from different PMU with common prefix
841730670b1SJohn Garry 	 */
842730670b1SJohn Garry 	tok = strtok_r(str, ",", &tmp);
843730670b1SJohn Garry 	if (strncmp(pmu_name, tok, strlen(tok))) {
844730670b1SJohn Garry 		res = false;
845730670b1SJohn Garry 		goto out;
846730670b1SJohn Garry 	}
847730670b1SJohn Garry 
848730670b1SJohn Garry 	/*
849730670b1SJohn Garry 	 * Match more complex aliases where the alias name is a comma-delimited
850730670b1SJohn Garry 	 * list of tokens, orderly contained in the matching PMU name.
851730670b1SJohn Garry 	 *
852730670b1SJohn Garry 	 * Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we
853730670b1SJohn Garry 	 *	    match "socket" in "socketX_pmunameY" and then "pmuname" in
854730670b1SJohn Garry 	 *	    "pmunameY".
855730670b1SJohn Garry 	 */
856c07d5c92SJohn Garry 	while (1) {
857c07d5c92SJohn Garry 		char *next_tok = strtok_r(NULL, ",", &tmp);
858c07d5c92SJohn Garry 
859730670b1SJohn Garry 		name = strstr(name, tok);
860c07d5c92SJohn Garry 		if (!name ||
861240e6fd0SIan Rogers 		    (!next_tok && !perf_pmu__match_ignoring_suffix(name, tok))) {
862730670b1SJohn Garry 			res = false;
863730670b1SJohn Garry 			goto out;
864730670b1SJohn Garry 		}
865c07d5c92SJohn Garry 		if (!next_tok)
866c07d5c92SJohn Garry 			break;
867c07d5c92SJohn Garry 		tok = next_tok;
868c07d5c92SJohn Garry 		name += strlen(tok);
869730670b1SJohn Garry 	}
870730670b1SJohn Garry 
871730670b1SJohn Garry 	res = true;
872730670b1SJohn Garry out:
873730670b1SJohn Garry 	free(str);
874730670b1SJohn Garry 	return res;
875730670b1SJohn Garry }
876730670b1SJohn Garry 
877660842e4SIan Rogers static int pmu_add_cpu_aliases_map_callback(const struct pmu_event *pe,
8781ba3752aSIan Rogers 					const struct pmu_events_table *table __maybe_unused,
879660842e4SIan Rogers 					void *vdata)
880660842e4SIan Rogers {
881f26d22f1SIan Rogers 	struct perf_pmu *pmu = vdata;
882660842e4SIan Rogers 
8837b723dbbSIan Rogers 	perf_pmu__new_alias(pmu, pe->name, pe->desc, pe->event, /*val_fd=*/ NULL, pe);
884660842e4SIan Rogers 	return 0;
885660842e4SIan Rogers }
886660842e4SIan Rogers 
887933f82ffSSukadev Bhattiprolu /*
888d685819bSIan Rogers  * From the pmu_events_table, find the events that correspond to the given
889d685819bSIan Rogers  * PMU and add them to the list 'head'.
890933f82ffSSukadev Bhattiprolu  */
891838a8c5fSIan Rogers void pmu_add_cpu_aliases_table(struct perf_pmu *pmu, const struct pmu_events_table *table)
892933f82ffSSukadev Bhattiprolu {
893f26d22f1SIan Rogers 	pmu_events_table__for_each_event(table, pmu, pmu_add_cpu_aliases_map_callback, pmu);
894933f82ffSSukadev Bhattiprolu }
895933f82ffSSukadev Bhattiprolu 
896838a8c5fSIan Rogers static void pmu_add_cpu_aliases(struct perf_pmu *pmu)
897e45ad701SJohn Garry {
8987c52f10cSIan Rogers 	if (!pmu->events_table)
899e45ad701SJohn Garry 		return;
900e45ad701SJohn Garry 
901e6ff1eedSIan Rogers 	if (pmu->cpu_aliases_added)
902e6ff1eedSIan Rogers 		return;
903e6ff1eedSIan Rogers 
9047c52f10cSIan Rogers 	pmu_add_cpu_aliases_table(pmu, pmu->events_table);
905e6ff1eedSIan Rogers 	pmu->cpu_aliases_added = true;
906e45ad701SJohn Garry }
907e45ad701SJohn Garry 
90829be2fe0SIan Rogers static int pmu_add_sys_aliases_iter_fn(const struct pmu_event *pe,
9091ba3752aSIan Rogers 				       const struct pmu_events_table *table __maybe_unused,
910f26d22f1SIan Rogers 				       void *vdata)
9114513c719SJohn Garry {
912f26d22f1SIan Rogers 	struct perf_pmu *pmu = vdata;
9134513c719SJohn Garry 
9144513c719SJohn Garry 	if (!pe->compat || !pe->pmu)
9154513c719SJohn Garry 		return 0;
9164513c719SJohn Garry 
9174513c719SJohn Garry 	if (!strcmp(pmu->id, pe->compat) &&
9184513c719SJohn Garry 	    pmu_uncore_alias_match(pe->pmu, pmu->name)) {
9197b723dbbSIan Rogers 		perf_pmu__new_alias(pmu,
920edb217ffSIan Rogers 				pe->name,
921edb217ffSIan Rogers 				pe->desc,
922edb217ffSIan Rogers 				pe->event,
923edb217ffSIan Rogers 				/*val_fd=*/ NULL,
924eab35953SJin Yao 				pe);
9254513c719SJohn Garry 	}
9264513c719SJohn Garry 
9274513c719SJohn Garry 	return 0;
9284513c719SJohn Garry }
9294513c719SJohn Garry 
930838a8c5fSIan Rogers void pmu_add_sys_aliases(struct perf_pmu *pmu)
9314513c719SJohn Garry {
9324513c719SJohn Garry 	if (!pmu->id)
9334513c719SJohn Garry 		return;
9344513c719SJohn Garry 
935f26d22f1SIan Rogers 	pmu_for_each_sys_event(pmu_add_sys_aliases_iter_fn, pmu);
9364513c719SJohn Garry }
9374513c719SJohn Garry 
938c5de47f2SSukadev Bhattiprolu struct perf_event_attr * __weak
939dc0a6202SAdrian Hunter perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
940dc0a6202SAdrian Hunter {
941dc0a6202SAdrian Hunter 	return NULL;
942dc0a6202SAdrian Hunter }
943dc0a6202SAdrian Hunter 
944c091ee90SIan Rogers const char * __weak
94513d60ba0SKan Liang pmu_find_real_name(const char *name)
94613d60ba0SKan Liang {
947c091ee90SIan Rogers 	return name;
94813d60ba0SKan Liang }
94913d60ba0SKan Liang 
950c091ee90SIan Rogers const char * __weak
95113d60ba0SKan Liang pmu_find_alias_name(const char *name __maybe_unused)
95213d60ba0SKan Liang {
95313d60ba0SKan Liang 	return NULL;
95413d60ba0SKan Liang }
95513d60ba0SKan Liang 
9563a69672eSNamhyung Kim static int pmu_max_precise(int dirfd, struct perf_pmu *pmu)
95790a86bdeSJiri Olsa {
95890a86bdeSJiri Olsa 	int max_precise = -1;
95990a86bdeSJiri Olsa 
9603a69672eSNamhyung Kim 	perf_pmu__scan_file_at(pmu, dirfd, "caps/max_precise", "%d", &max_precise);
96190a86bdeSJiri Olsa 	return max_precise;
96290a86bdeSJiri Olsa }
96390a86bdeSJiri Olsa 
9641eaf496eSIan Rogers struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char *lookup_name)
965cd82a32eSJiri Olsa {
966cd82a32eSJiri Olsa 	struct perf_pmu *pmu;
967cd82a32eSJiri Olsa 	__u32 type;
968c091ee90SIan Rogers 	const char *name = pmu_find_real_name(lookup_name);
969c091ee90SIan Rogers 	const char *alias_name;
97049afa7f6SJin Yao 
971cd82a32eSJiri Olsa 	pmu = zalloc(sizeof(*pmu));
972cd82a32eSJiri Olsa 	if (!pmu)
973cd82a32eSJiri Olsa 		return NULL;
974cd82a32eSJiri Olsa 
975f63a536fSIan Rogers 	pmu->name = strdup(name);
976f63a536fSIan Rogers 	if (!pmu->name)
977f63a536fSIan Rogers 		goto err;
97888ed9184SIan Rogers 
97988ed9184SIan Rogers 	/*
98088ed9184SIan Rogers 	 * Read type early to fail fast if a lookup name isn't a PMU. Ensure
98188ed9184SIan Rogers 	 * that type value is successfully assigned (return 1).
98288ed9184SIan Rogers 	 */
98388ed9184SIan Rogers 	if (perf_pmu__scan_file_at(pmu, dirfd, "type", "%u", &type) != 1)
98488ed9184SIan Rogers 		goto err;
98588ed9184SIan Rogers 
98688ed9184SIan Rogers 	INIT_LIST_HEAD(&pmu->format);
98788ed9184SIan Rogers 	INIT_LIST_HEAD(&pmu->aliases);
98888ed9184SIan Rogers 	INIT_LIST_HEAD(&pmu->caps);
98988ed9184SIan Rogers 
990838a8c5fSIan Rogers 	/*
991838a8c5fSIan Rogers 	 * The pmu data we store & need consists of the pmu
992838a8c5fSIan Rogers 	 * type value and format definitions. Load both right
993838a8c5fSIan Rogers 	 * now.
994838a8c5fSIan Rogers 	 */
995838a8c5fSIan Rogers 	if (pmu_format(pmu, dirfd, name)) {
996838a8c5fSIan Rogers 		free(pmu);
997838a8c5fSIan Rogers 		return NULL;
998838a8c5fSIan Rogers 	}
999d06593aaSIan Rogers 	pmu->is_core = is_pmu_core(name);
1000d06593aaSIan Rogers 	pmu->cpus = pmu_cpumask(dirfd, name, pmu->is_core);
100113d60ba0SKan Liang 
100213d60ba0SKan Liang 	alias_name = pmu_find_alias_name(name);
100313d60ba0SKan Liang 	if (alias_name) {
100413d60ba0SKan Liang 		pmu->alias_name = strdup(alias_name);
100513d60ba0SKan Liang 		if (!pmu->alias_name)
100613d60ba0SKan Liang 			goto err;
100713d60ba0SKan Liang 	}
100813d60ba0SKan Liang 
100954e32dc0SGanapatrao Kulkarni 	pmu->type = type;
10103a69672eSNamhyung Kim 	pmu->is_uncore = pmu_is_uncore(dirfd, name);
101151d54847SJohn Garry 	if (pmu->is_uncore)
101251d54847SJohn Garry 		pmu->id = pmu_id(name);
10133a69672eSNamhyung Kim 	pmu->max_precise = pmu_max_precise(dirfd, pmu);
10147c52f10cSIan Rogers 	pmu->events_table = perf_pmu__find_events_table(pmu);
1015838a8c5fSIan Rogers 	pmu_add_sys_aliases(pmu);
10161eaf496eSIan Rogers 	list_add_tail(&pmu->list, pmus);
1017dc0a6202SAdrian Hunter 
1018dc0a6202SAdrian Hunter 	pmu->default_config = perf_pmu__get_default_config(pmu);
1019dc0a6202SAdrian Hunter 
1020cd82a32eSJiri Olsa 	return pmu;
102113d60ba0SKan Liang err:
1022efe98a7aSArnaldo Carvalho de Melo 	zfree(&pmu->name);
102313d60ba0SKan Liang 	free(pmu);
102413d60ba0SKan Liang 	return NULL;
1025cd82a32eSJiri Olsa }
1026cd82a32eSJiri Olsa 
1027628eaa4eSIan Rogers /* Creates the PMU when sysfs scanning fails. */
1028628eaa4eSIan Rogers struct perf_pmu *perf_pmu__create_placeholder_core_pmu(struct list_head *core_pmus)
1029628eaa4eSIan Rogers {
1030628eaa4eSIan Rogers 	struct perf_pmu *pmu = zalloc(sizeof(*pmu));
1031628eaa4eSIan Rogers 
1032628eaa4eSIan Rogers 	if (!pmu)
1033628eaa4eSIan Rogers 		return NULL;
1034628eaa4eSIan Rogers 
1035628eaa4eSIan Rogers 	pmu->name = strdup("cpu");
1036628eaa4eSIan Rogers 	if (!pmu->name) {
1037628eaa4eSIan Rogers 		free(pmu);
1038628eaa4eSIan Rogers 		return NULL;
1039628eaa4eSIan Rogers 	}
1040628eaa4eSIan Rogers 
1041628eaa4eSIan Rogers 	pmu->is_core = true;
1042628eaa4eSIan Rogers 	pmu->type = PERF_TYPE_RAW;
1043628eaa4eSIan Rogers 	pmu->cpus = cpu_map__online();
1044628eaa4eSIan Rogers 
1045628eaa4eSIan Rogers 	INIT_LIST_HEAD(&pmu->format);
1046628eaa4eSIan Rogers 	INIT_LIST_HEAD(&pmu->aliases);
1047628eaa4eSIan Rogers 	INIT_LIST_HEAD(&pmu->caps);
1048628eaa4eSIan Rogers 	list_add_tail(&pmu->list, core_pmus);
1049628eaa4eSIan Rogers 	return pmu;
1050628eaa4eSIan Rogers }
1051628eaa4eSIan Rogers 
1052e552b7beSRob Herring void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu)
1053e552b7beSRob Herring {
1054e552b7beSRob Herring 	struct perf_pmu_format *format;
1055e552b7beSRob Herring 
105668c25043SIan Rogers 	if (pmu->formats_checked)
105768c25043SIan Rogers 		return;
105868c25043SIan Rogers 
105968c25043SIan Rogers 	pmu->formats_checked = true;
106068c25043SIan Rogers 
1061e552b7beSRob Herring 	/* fake pmu doesn't have format list */
1062e552b7beSRob Herring 	if (pmu == &perf_pmu__fake)
1063e552b7beSRob Herring 		return;
1064e552b7beSRob Herring 
106550402641SIan Rogers 	list_for_each_entry(format, &pmu->format, list) {
106650402641SIan Rogers 		perf_pmu_format__load(pmu, format);
1067e552b7beSRob Herring 		if (format->value >= PERF_PMU_FORMAT_VALUE_CONFIG_END) {
1068e552b7beSRob Herring 			pr_warning("WARNING: '%s' format '%s' requires 'perf_event_attr::config%d'"
1069e552b7beSRob Herring 				   "which is not supported by this version of perf!\n",
1070e552b7beSRob Herring 				   pmu->name, format->name, format->value);
1071e552b7beSRob Herring 			return;
1072e552b7beSRob Herring 		}
1073e552b7beSRob Herring 	}
107450402641SIan Rogers }
1075e552b7beSRob Herring 
1076c6d616feSIan Rogers bool evsel__is_aux_event(const struct evsel *evsel)
1077e12ee9f7SAdrian Hunter {
1078e76026bdSArnaldo Carvalho de Melo 	struct perf_pmu *pmu = evsel__find_pmu(evsel);
1079e12ee9f7SAdrian Hunter 
1080e12ee9f7SAdrian Hunter 	return pmu && pmu->auxtrace;
1081e12ee9f7SAdrian Hunter }
1082e12ee9f7SAdrian Hunter 
10836593f019SJames Clark /*
10846593f019SJames Clark  * Set @config_name to @val as long as the user hasn't already set or cleared it
10856593f019SJames Clark  * by passing a config term on the command line.
10866593f019SJames Clark  *
10876593f019SJames Clark  * @val is the value to put into the bits specified by @config_name rather than
10886593f019SJames Clark  * the bit pattern. It is shifted into position by this function, so to set
10896593f019SJames Clark  * something to true, pass 1 for val rather than a pre shifted value.
10906593f019SJames Clark  */
10916593f019SJames Clark #define field_prep(_mask, _val) (((_val) << (ffsll(_mask) - 1)) & (_mask))
10926593f019SJames Clark void evsel__set_config_if_unset(struct perf_pmu *pmu, struct evsel *evsel,
10936593f019SJames Clark 				const char *config_name, u64 val)
10946593f019SJames Clark {
10956593f019SJames Clark 	u64 user_bits = 0, bits;
10966593f019SJames Clark 	struct evsel_config_term *term = evsel__get_config_term(evsel, CFG_CHG);
10976593f019SJames Clark 
10986593f019SJames Clark 	if (term)
10996593f019SJames Clark 		user_bits = term->val.cfg_chg;
11006593f019SJames Clark 
1101da6a5afdSIan Rogers 	bits = perf_pmu__format_bits(pmu, config_name);
11026593f019SJames Clark 
11036593f019SJames Clark 	/* Do nothing if the user changed the value */
11046593f019SJames Clark 	if (bits & user_bits)
11056593f019SJames Clark 		return;
11066593f019SJames Clark 
11076593f019SJames Clark 	/* Otherwise replace it */
11086593f019SJames Clark 	evsel->core.attr.config &= ~bits;
11096593f019SJames Clark 	evsel->core.attr.config |= field_prep(bits, val);
11106593f019SJames Clark }
11116593f019SJames Clark 
11125c6ccc37SArnaldo Carvalho de Melo static struct perf_pmu_format *
111309ff6071SAdrian Hunter pmu_find_format(struct list_head *formats, const char *name)
1114cd82a32eSJiri Olsa {
11155c6ccc37SArnaldo Carvalho de Melo 	struct perf_pmu_format *format;
1116cd82a32eSJiri Olsa 
1117cd82a32eSJiri Olsa 	list_for_each_entry(format, formats, list)
1118cd82a32eSJiri Olsa 		if (!strcmp(format->name, name))
1119cd82a32eSJiri Olsa 			return format;
1120cd82a32eSJiri Olsa 
1121cd82a32eSJiri Olsa 	return NULL;
1122cd82a32eSJiri Olsa }
1123cd82a32eSJiri Olsa 
1124da6a5afdSIan Rogers __u64 perf_pmu__format_bits(struct perf_pmu *pmu, const char *name)
112509ff6071SAdrian Hunter {
1126da6a5afdSIan Rogers 	struct perf_pmu_format *format = pmu_find_format(&pmu->format, name);
112709ff6071SAdrian Hunter 	__u64 bits = 0;
112809ff6071SAdrian Hunter 	int fbit;
112909ff6071SAdrian Hunter 
113009ff6071SAdrian Hunter 	if (!format)
113109ff6071SAdrian Hunter 		return 0;
113209ff6071SAdrian Hunter 
113309ff6071SAdrian Hunter 	for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
113409ff6071SAdrian Hunter 		bits |= 1ULL << fbit;
113509ff6071SAdrian Hunter 
113609ff6071SAdrian Hunter 	return bits;
113709ff6071SAdrian Hunter }
113809ff6071SAdrian Hunter 
11397eb54733SIan Rogers int perf_pmu__format_type(struct perf_pmu *pmu, const char *name)
1140a1ac7de6SAdrian Hunter {
11417eb54733SIan Rogers 	struct perf_pmu_format *format = pmu_find_format(&pmu->format, name);
1142a1ac7de6SAdrian Hunter 
1143a1ac7de6SAdrian Hunter 	if (!format)
1144a1ac7de6SAdrian Hunter 		return -1;
1145a1ac7de6SAdrian Hunter 
114650402641SIan Rogers 	perf_pmu_format__load(pmu, format);
1147a1ac7de6SAdrian Hunter 	return format->value;
1148a1ac7de6SAdrian Hunter }
1149a1ac7de6SAdrian Hunter 
1150cd82a32eSJiri Olsa /*
1151dc0a6202SAdrian Hunter  * Sets value based on the format definition (format parameter)
11524d39c89fSIngo Molnar  * and unformatted value (value parameter).
1153cd82a32eSJiri Olsa  */
1154dc0a6202SAdrian Hunter static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
1155dc0a6202SAdrian Hunter 			     bool zero)
1156cd82a32eSJiri Olsa {
1157cd82a32eSJiri Olsa 	unsigned long fbit, vbit;
1158cd82a32eSJiri Olsa 
1159cd82a32eSJiri Olsa 	for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
1160cd82a32eSJiri Olsa 
1161cd82a32eSJiri Olsa 		if (!test_bit(fbit, format))
1162cd82a32eSJiri Olsa 			continue;
1163cd82a32eSJiri Olsa 
1164dc0a6202SAdrian Hunter 		if (value & (1llu << vbit++))
1165dc0a6202SAdrian Hunter 			*v |= (1llu << fbit);
1166dc0a6202SAdrian Hunter 		else if (zero)
1167dc0a6202SAdrian Hunter 			*v &= ~(1llu << fbit);
1168cd82a32eSJiri Olsa 	}
1169cd82a32eSJiri Olsa }
1170cd82a32eSJiri Olsa 
11710efe6b67SAdrian Hunter static __u64 pmu_format_max_value(const unsigned long *format)
11720efe6b67SAdrian Hunter {
11731b9caa10SJiri Olsa 	int w;
11740efe6b67SAdrian Hunter 
11751b9caa10SJiri Olsa 	w = bitmap_weight(format, PERF_PMU_FORMAT_BITS);
11761b9caa10SJiri Olsa 	if (!w)
11771b9caa10SJiri Olsa 		return 0;
11781b9caa10SJiri Olsa 	if (w < 64)
11791b9caa10SJiri Olsa 		return (1ULL << w) - 1;
11801b9caa10SJiri Olsa 	return -1;
11810efe6b67SAdrian Hunter }
11820efe6b67SAdrian Hunter 
1183cd82a32eSJiri Olsa /*
1184688d4dfcSCody P Schafer  * Term is a string term, and might be a param-term. Try to look up it's value
1185688d4dfcSCody P Schafer  * in the remaining terms.
1186688d4dfcSCody P Schafer  * - We have a term like "base-or-format-term=param-term",
1187688d4dfcSCody P Schafer  * - We need to find the value supplied for "param-term" (with param-term named
1188688d4dfcSCody P Schafer  *   in a config string) later on in the term list.
1189688d4dfcSCody P Schafer  */
1190688d4dfcSCody P Schafer static int pmu_resolve_param_term(struct parse_events_term *term,
1191688d4dfcSCody P Schafer 				  struct list_head *head_terms,
1192688d4dfcSCody P Schafer 				  __u64 *value)
1193688d4dfcSCody P Schafer {
1194688d4dfcSCody P Schafer 	struct parse_events_term *t;
1195688d4dfcSCody P Schafer 
1196688d4dfcSCody P Schafer 	list_for_each_entry(t, head_terms, list) {
11972a3d252dSIan Rogers 		if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM &&
11982a3d252dSIan Rogers 		    t->config && !strcmp(t->config, term->config)) {
1199688d4dfcSCody P Schafer 			t->used = true;
1200688d4dfcSCody P Schafer 			*value = t->val.num;
1201688d4dfcSCody P Schafer 			return 0;
1202688d4dfcSCody P Schafer 		}
1203688d4dfcSCody P Schafer 	}
1204688d4dfcSCody P Schafer 
1205bb963e16SNamhyung Kim 	if (verbose > 0)
1206688d4dfcSCody P Schafer 		printf("Required parameter '%s' not specified\n", term->config);
1207688d4dfcSCody P Schafer 
1208688d4dfcSCody P Schafer 	return -1;
1209688d4dfcSCody P Schafer }
1210688d4dfcSCody P Schafer 
1211ffeb883eSHe Kuang static char *pmu_formats_string(struct list_head *formats)
1212e64b020bSJiri Olsa {
1213e64b020bSJiri Olsa 	struct perf_pmu_format *format;
121411db4e29SMasami Hiramatsu 	char *str = NULL;
121511db4e29SMasami Hiramatsu 	struct strbuf buf = STRBUF_INIT;
1216f1417ceaSXin Gao 	unsigned int i = 0;
1217e64b020bSJiri Olsa 
1218ffeb883eSHe Kuang 	if (!formats)
1219e64b020bSJiri Olsa 		return NULL;
1220e64b020bSJiri Olsa 
1221e64b020bSJiri Olsa 	/* sysfs exported terms */
1222ffeb883eSHe Kuang 	list_for_each_entry(format, formats, list)
122311db4e29SMasami Hiramatsu 		if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
122411db4e29SMasami Hiramatsu 			goto error;
1225e64b020bSJiri Olsa 
1226ffeb883eSHe Kuang 	str = strbuf_detach(&buf, NULL);
122711db4e29SMasami Hiramatsu error:
1228ffeb883eSHe Kuang 	strbuf_release(&buf);
1229e64b020bSJiri Olsa 
1230e64b020bSJiri Olsa 	return str;
1231e64b020bSJiri Olsa }
1232e64b020bSJiri Olsa 
1233688d4dfcSCody P Schafer /*
1234cd82a32eSJiri Olsa  * Setup one of config[12] attr members based on the
123588aca8d9SCody P Schafer  * user input data - term parameter.
1236cd82a32eSJiri Olsa  */
1237804fee5dSIan Rogers static int pmu_config_term(struct perf_pmu *pmu,
1238cd82a32eSJiri Olsa 			   struct perf_event_attr *attr,
1239dc0a6202SAdrian Hunter 			   struct parse_events_term *term,
1240688d4dfcSCody P Schafer 			   struct list_head *head_terms,
1241e64b020bSJiri Olsa 			   bool zero, struct parse_events_error *err)
1242cd82a32eSJiri Olsa {
12435c6ccc37SArnaldo Carvalho de Melo 	struct perf_pmu_format *format;
1244cd82a32eSJiri Olsa 	__u64 *vp;
12450efe6b67SAdrian Hunter 	__u64 val, max_val;
1246cd82a32eSJiri Olsa 
1247cd82a32eSJiri Olsa 	/*
1248688d4dfcSCody P Schafer 	 * If this is a parameter we've already used for parameterized-eval,
1249688d4dfcSCody P Schafer 	 * skip it in normal eval.
1250688d4dfcSCody P Schafer 	 */
1251688d4dfcSCody P Schafer 	if (term->used)
1252688d4dfcSCody P Schafer 		return 0;
1253688d4dfcSCody P Schafer 
1254688d4dfcSCody P Schafer 	/*
1255cd82a32eSJiri Olsa 	 * Hardcoded terms should be already in, so nothing
1256cd82a32eSJiri Olsa 	 * to be done for them.
1257cd82a32eSJiri Olsa 	 */
1258cd82a32eSJiri Olsa 	if (parse_events__is_hardcoded_term(term))
1259cd82a32eSJiri Olsa 		return 0;
1260cd82a32eSJiri Olsa 
1261804fee5dSIan Rogers 	format = pmu_find_format(&pmu->format, term->config);
1262688d4dfcSCody P Schafer 	if (!format) {
1263804fee5dSIan Rogers 		char *pmu_term = pmu_formats_string(&pmu->format);
12644ac22b48SIan Rogers 		char *unknown_term;
12654ac22b48SIan Rogers 		char *help_msg;
1266ffeb883eSHe Kuang 
12674ac22b48SIan Rogers 		if (asprintf(&unknown_term,
12684ac22b48SIan Rogers 				"unknown term '%s' for pmu '%s'",
1269804fee5dSIan Rogers 				term->config, pmu->name) < 0)
12704ac22b48SIan Rogers 			unknown_term = NULL;
12714ac22b48SIan Rogers 		help_msg = parse_events_formats_error_string(pmu_term);
12724ac22b48SIan Rogers 		if (err) {
12736c191289SIan Rogers 			parse_events_error__handle(err, term->err_term,
12744ac22b48SIan Rogers 						   unknown_term,
12754ac22b48SIan Rogers 						   help_msg);
12764ac22b48SIan Rogers 		} else {
12774ac22b48SIan Rogers 			pr_debug("%s (%s)\n", unknown_term, help_msg);
12784ac22b48SIan Rogers 			free(unknown_term);
1279e64b020bSJiri Olsa 		}
12804ac22b48SIan Rogers 		free(pmu_term);
1281cd82a32eSJiri Olsa 		return -EINVAL;
1282688d4dfcSCody P Schafer 	}
128350402641SIan Rogers 	perf_pmu_format__load(pmu, format);
1284cd82a32eSJiri Olsa 	switch (format->value) {
1285cd82a32eSJiri Olsa 	case PERF_PMU_FORMAT_VALUE_CONFIG:
1286cd82a32eSJiri Olsa 		vp = &attr->config;
1287cd82a32eSJiri Olsa 		break;
1288cd82a32eSJiri Olsa 	case PERF_PMU_FORMAT_VALUE_CONFIG1:
1289cd82a32eSJiri Olsa 		vp = &attr->config1;
1290cd82a32eSJiri Olsa 		break;
1291cd82a32eSJiri Olsa 	case PERF_PMU_FORMAT_VALUE_CONFIG2:
1292cd82a32eSJiri Olsa 		vp = &attr->config2;
1293cd82a32eSJiri Olsa 		break;
1294204e7c49SRob Herring 	case PERF_PMU_FORMAT_VALUE_CONFIG3:
1295204e7c49SRob Herring 		vp = &attr->config3;
1296204e7c49SRob Herring 		break;
1297cd82a32eSJiri Olsa 	default:
1298cd82a32eSJiri Olsa 		return -EINVAL;
1299cd82a32eSJiri Olsa 	}
1300cd82a32eSJiri Olsa 
130116fa7e82SJiri Olsa 	/*
1302688d4dfcSCody P Schafer 	 * Either directly use a numeric term, or try to translate string terms
1303688d4dfcSCody P Schafer 	 * using event parameters.
130416fa7e82SJiri Olsa 	 */
130599e7138eSJiri Olsa 	if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
130699e7138eSJiri Olsa 		if (term->no_value &&
130799e7138eSJiri Olsa 		    bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
130899e7138eSJiri Olsa 			if (err) {
13096c191289SIan Rogers 				parse_events_error__handle(err, term->err_val,
1310448d732cSIan Rogers 					   strdup("no value assigned for term"),
1311448d732cSIan Rogers 					   NULL);
131299e7138eSJiri Olsa 			}
131399e7138eSJiri Olsa 			return -EINVAL;
131499e7138eSJiri Olsa 		}
131599e7138eSJiri Olsa 
1316688d4dfcSCody P Schafer 		val = term->val.num;
131799e7138eSJiri Olsa 	} else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1318688d4dfcSCody P Schafer 		if (strcmp(term->val.str, "?")) {
1319bb963e16SNamhyung Kim 			if (verbose > 0) {
1320688d4dfcSCody P Schafer 				pr_info("Invalid sysfs entry %s=%s\n",
1321688d4dfcSCody P Schafer 						term->config, term->val.str);
1322e64b020bSJiri Olsa 			}
1323e64b020bSJiri Olsa 			if (err) {
13246c191289SIan Rogers 				parse_events_error__handle(err, term->err_val,
1325448d732cSIan Rogers 					strdup("expected numeric value"),
1326448d732cSIan Rogers 					NULL);
1327e64b020bSJiri Olsa 			}
1328688d4dfcSCody P Schafer 			return -EINVAL;
1329688d4dfcSCody P Schafer 		}
1330688d4dfcSCody P Schafer 
1331688d4dfcSCody P Schafer 		if (pmu_resolve_param_term(term, head_terms, &val))
1332688d4dfcSCody P Schafer 			return -EINVAL;
1333688d4dfcSCody P Schafer 	} else
1334688d4dfcSCody P Schafer 		return -EINVAL;
1335688d4dfcSCody P Schafer 
13360efe6b67SAdrian Hunter 	max_val = pmu_format_max_value(format->bits);
13370efe6b67SAdrian Hunter 	if (val > max_val) {
13380efe6b67SAdrian Hunter 		if (err) {
1339448d732cSIan Rogers 			char *err_str;
1340448d732cSIan Rogers 
13416c191289SIan Rogers 			parse_events_error__handle(err, term->err_val,
1342448d732cSIan Rogers 				asprintf(&err_str,
13430efe6b67SAdrian Hunter 				    "value too big for format, maximum is %llu",
1344448d732cSIan Rogers 				    (unsigned long long)max_val) < 0
1345448d732cSIan Rogers 				    ? strdup("value too big for format")
1346448d732cSIan Rogers 				    : err_str,
1347448d732cSIan Rogers 				    NULL);
13480efe6b67SAdrian Hunter 			return -EINVAL;
13490efe6b67SAdrian Hunter 		}
13500efe6b67SAdrian Hunter 		/*
13510efe6b67SAdrian Hunter 		 * Assume we don't care if !err, in which case the value will be
13520efe6b67SAdrian Hunter 		 * silently truncated.
13530efe6b67SAdrian Hunter 		 */
13540efe6b67SAdrian Hunter 	}
13550efe6b67SAdrian Hunter 
1356688d4dfcSCody P Schafer 	pmu_format_value(format->bits, val, vp, zero);
1357cd82a32eSJiri Olsa 	return 0;
1358cd82a32eSJiri Olsa }
1359cd82a32eSJiri Olsa 
1360804fee5dSIan Rogers int perf_pmu__config_terms(struct perf_pmu *pmu,
1361cff7f956SJiri Olsa 			   struct perf_event_attr *attr,
1362dc0a6202SAdrian Hunter 			   struct list_head *head_terms,
1363e64b020bSJiri Olsa 			   bool zero, struct parse_events_error *err)
1364cd82a32eSJiri Olsa {
13656cee6cd3SArnaldo Carvalho de Melo 	struct parse_events_term *term;
1366cd82a32eSJiri Olsa 
1367688d4dfcSCody P Schafer 	list_for_each_entry(term, head_terms, list) {
1368804fee5dSIan Rogers 		if (pmu_config_term(pmu, attr, term, head_terms, zero, err))
1369cd82a32eSJiri Olsa 			return -EINVAL;
1370688d4dfcSCody P Schafer 	}
1371cd82a32eSJiri Olsa 
1372cd82a32eSJiri Olsa 	return 0;
1373cd82a32eSJiri Olsa }
1374cd82a32eSJiri Olsa 
1375cd82a32eSJiri Olsa /*
1376cd82a32eSJiri Olsa  * Configures event's 'attr' parameter based on the:
1377cd82a32eSJiri Olsa  * 1) users input - specified in terms parameter
1378cd82a32eSJiri Olsa  * 2) pmu format definitions - specified by pmu parameter
1379cd82a32eSJiri Olsa  */
1380cd82a32eSJiri Olsa int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
1381e64b020bSJiri Olsa 		     struct list_head *head_terms,
1382e64b020bSJiri Olsa 		     struct parse_events_error *err)
1383cd82a32eSJiri Olsa {
1384dc0a6202SAdrian Hunter 	bool zero = !!pmu->default_config;
1385dc0a6202SAdrian Hunter 
1386804fee5dSIan Rogers 	return perf_pmu__config_terms(pmu, attr, head_terms, zero, err);
1387cd82a32eSJiri Olsa }
1388cd82a32eSJiri Olsa 
13895c6ccc37SArnaldo Carvalho de Melo static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
13906cee6cd3SArnaldo Carvalho de Melo 					     struct parse_events_term *term)
1391a6146d50SZheng Yan {
1392e6ff1eedSIan Rogers 	struct perf_pmu_alias *alias;
1393970ef02eSIan Rogers 	const char *name;
1394a6146d50SZheng Yan 
1395a6146d50SZheng Yan 	if (parse_events__is_hardcoded_term(term))
1396a6146d50SZheng Yan 		return NULL;
1397a6146d50SZheng Yan 
1398a6146d50SZheng Yan 	if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
139964199ae4SIan Rogers 		if (!term->no_value)
1400a6146d50SZheng Yan 			return NULL;
1401a6146d50SZheng Yan 		if (pmu_find_format(&pmu->format, term->config))
1402a6146d50SZheng Yan 			return NULL;
1403a6146d50SZheng Yan 		name = term->config;
1404c3245d20SIan Rogers 
1405a6146d50SZheng Yan 	} else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
1406a6146d50SZheng Yan 		if (strcasecmp(term->config, "event"))
1407a6146d50SZheng Yan 			return NULL;
1408a6146d50SZheng Yan 		name = term->val.str;
1409a6146d50SZheng Yan 	} else {
1410a6146d50SZheng Yan 		return NULL;
1411a6146d50SZheng Yan 	}
1412a6146d50SZheng Yan 
14138d4b6d37SIan Rogers 	alias = perf_pmu__find_alias(pmu, name, /*load=*/ true);
1414e6ff1eedSIan Rogers 	if (alias || pmu->cpu_aliases_added)
1415e6ff1eedSIan Rogers 		return alias;
1416e6ff1eedSIan Rogers 
1417e6ff1eedSIan Rogers 	/* Alias doesn't exist, try to get it from the json events. */
1418e6ff1eedSIan Rogers 	if (pmu->events_table &&
1419e6ff1eedSIan Rogers 	    pmu_events_table__find_event(pmu->events_table, pmu, name,
1420e6ff1eedSIan Rogers 				         pmu_add_cpu_aliases_map_callback,
1421e6ff1eedSIan Rogers 				         pmu) == 0) {
14228d4b6d37SIan Rogers 		alias = perf_pmu__find_alias(pmu, name, /*load=*/ false);
1423e6ff1eedSIan Rogers 	}
1424e6ff1eedSIan Rogers 	return alias;
1425a6146d50SZheng Yan }
1426a6146d50SZheng Yan 
1427410136f5SStephane Eranian 
14287b723dbbSIan Rogers static int check_info_data(struct perf_pmu *pmu,
14297b723dbbSIan Rogers 			   struct perf_pmu_alias *alias,
14309d31cb93SIan Rogers 			   struct perf_pmu_info *info,
14319d31cb93SIan Rogers 			   struct parse_events_error *err,
14329d31cb93SIan Rogers 			   int column)
1433410136f5SStephane Eranian {
14347b723dbbSIan Rogers 	read_alias_info(pmu, alias);
1435410136f5SStephane Eranian 	/*
1436410136f5SStephane Eranian 	 * Only one term in event definition can
14371d9e446bSJiri Olsa 	 * define unit, scale and snapshot, fail
14381d9e446bSJiri Olsa 	 * if there's more than one.
1439410136f5SStephane Eranian 	 */
14409d31cb93SIan Rogers 	if (info->unit && alias->unit[0]) {
14419d31cb93SIan Rogers 		parse_events_error__handle(err, column,
14429d31cb93SIan Rogers 					strdup("Attempt to set event's unit twice"),
14439d31cb93SIan Rogers 					NULL);
1444410136f5SStephane Eranian 		return -EINVAL;
14459d31cb93SIan Rogers 	}
14469d31cb93SIan Rogers 	if (info->scale && alias->scale) {
14479d31cb93SIan Rogers 		parse_events_error__handle(err, column,
14489d31cb93SIan Rogers 					strdup("Attempt to set event's scale twice"),
14499d31cb93SIan Rogers 					NULL);
14509d31cb93SIan Rogers 		return -EINVAL;
14519d31cb93SIan Rogers 	}
14529d31cb93SIan Rogers 	if (info->snapshot && alias->snapshot) {
14539d31cb93SIan Rogers 		parse_events_error__handle(err, column,
14549d31cb93SIan Rogers 					strdup("Attempt to set event snapshot twice"),
14559d31cb93SIan Rogers 					NULL);
14569d31cb93SIan Rogers 		return -EINVAL;
14579d31cb93SIan Rogers 	}
1458410136f5SStephane Eranian 
1459b30a7d1fSArnaldo Carvalho de Melo 	if (alias->unit[0])
14601d9e446bSJiri Olsa 		info->unit = alias->unit;
1461410136f5SStephane Eranian 
1462410136f5SStephane Eranian 	if (alias->scale)
14631d9e446bSJiri Olsa 		info->scale = alias->scale;
14641d9e446bSJiri Olsa 
14651d9e446bSJiri Olsa 	if (alias->snapshot)
14661d9e446bSJiri Olsa 		info->snapshot = alias->snapshot;
1467410136f5SStephane Eranian 
1468410136f5SStephane Eranian 	return 0;
1469410136f5SStephane Eranian }
1470410136f5SStephane Eranian 
1471a6146d50SZheng Yan /*
1472a6146d50SZheng Yan  * Find alias in the terms list and replace it with the terms
1473a6146d50SZheng Yan  * defined for the alias
1474a6146d50SZheng Yan  */
1475410136f5SStephane Eranian int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
14769d31cb93SIan Rogers 			  struct perf_pmu_info *info, struct parse_events_error *err)
1477a6146d50SZheng Yan {
14786cee6cd3SArnaldo Carvalho de Melo 	struct parse_events_term *term, *h;
14795c6ccc37SArnaldo Carvalho de Melo 	struct perf_pmu_alias *alias;
1480a6146d50SZheng Yan 	int ret;
1481a6146d50SZheng Yan 
1482044330c1SMatt Fleming 	info->per_pkg = false;
1483044330c1SMatt Fleming 
14848a398897SStephane Eranian 	/*
14858a398897SStephane Eranian 	 * Mark unit and scale as not set
14868a398897SStephane Eranian 	 * (different from default values, see below)
14878a398897SStephane Eranian 	 */
148846441bdcSMatt Fleming 	info->unit     = NULL;
148946441bdcSMatt Fleming 	info->scale    = 0.0;
14901d9e446bSJiri Olsa 	info->snapshot = false;
1491410136f5SStephane Eranian 
1492a6146d50SZheng Yan 	list_for_each_entry_safe(term, h, head_terms, list) {
1493a6146d50SZheng Yan 		alias = pmu_find_alias(pmu, term);
1494a6146d50SZheng Yan 		if (!alias)
1495a6146d50SZheng Yan 			continue;
1496a6146d50SZheng Yan 		ret = pmu_alias_terms(alias, &term->list);
14979d31cb93SIan Rogers 		if (ret) {
14989d31cb93SIan Rogers 			parse_events_error__handle(err, term->err_term,
14999d31cb93SIan Rogers 						strdup("Failure to duplicate terms"),
15009d31cb93SIan Rogers 						NULL);
1501a6146d50SZheng Yan 			return ret;
15029d31cb93SIan Rogers 		}
1503410136f5SStephane Eranian 
15047b723dbbSIan Rogers 		ret = check_info_data(pmu, alias, info, err, term->err_term);
1505410136f5SStephane Eranian 		if (ret)
1506410136f5SStephane Eranian 			return ret;
1507410136f5SStephane Eranian 
1508044330c1SMatt Fleming 		if (alias->per_pkg)
1509044330c1SMatt Fleming 			info->per_pkg = true;
1510044330c1SMatt Fleming 
1511e56fbc9dSArnaldo Carvalho de Melo 		list_del_init(&term->list);
15121dc92556SIan Rogers 		parse_events_term__delete(term);
1513a6146d50SZheng Yan 	}
15148a398897SStephane Eranian 
15158a398897SStephane Eranian 	/*
15168a398897SStephane Eranian 	 * if no unit or scale found in aliases, then
15178a398897SStephane Eranian 	 * set defaults as for evsel
15188a398897SStephane Eranian 	 * unit cannot left to NULL
15198a398897SStephane Eranian 	 */
152046441bdcSMatt Fleming 	if (info->unit == NULL)
152146441bdcSMatt Fleming 		info->unit   = "";
15228a398897SStephane Eranian 
152346441bdcSMatt Fleming 	if (info->scale == 0.0)
152446441bdcSMatt Fleming 		info->scale  = 1.0;
15258a398897SStephane Eranian 
1526a6146d50SZheng Yan 	return 0;
1527a6146d50SZheng Yan }
1528a6146d50SZheng Yan 
1529c3245d20SIan Rogers struct find_event_args {
1530c3245d20SIan Rogers 	const char *event;
1531c3245d20SIan Rogers 	void *state;
1532c3245d20SIan Rogers 	pmu_event_callback cb;
1533c3245d20SIan Rogers };
1534c3245d20SIan Rogers 
1535c3245d20SIan Rogers static int find_event_callback(void *state, struct pmu_event_info *info)
1536838a8c5fSIan Rogers {
1537c3245d20SIan Rogers 	struct find_event_args *args = state;
1538838a8c5fSIan Rogers 
1539c3245d20SIan Rogers 	if (!strcmp(args->event, info->name))
1540c3245d20SIan Rogers 		return args->cb(args->state, info);
1541838a8c5fSIan Rogers 
1542c3245d20SIan Rogers 	return 0;
1543838a8c5fSIan Rogers }
1544c3245d20SIan Rogers 
1545c3245d20SIan Rogers int perf_pmu__find_event(struct perf_pmu *pmu, const char *event, void *state, pmu_event_callback cb)
1546c3245d20SIan Rogers {
1547c3245d20SIan Rogers 	struct find_event_args args = {
1548c3245d20SIan Rogers 		.event = event,
1549c3245d20SIan Rogers 		.state = state,
1550c3245d20SIan Rogers 		.cb = cb,
1551c3245d20SIan Rogers 	};
1552c3245d20SIan Rogers 
1553cd4e1efbSIan Rogers 	/* Sub-optimal, but function is only used by tests. */
1554cd4e1efbSIan Rogers 	return perf_pmu__for_each_event(pmu, /*skip_duplicate_pmus=*/ false,
1555cd4e1efbSIan Rogers 					&args, find_event_callback);
1556c3245d20SIan Rogers }
1557c3245d20SIan Rogers 
1558804fee5dSIan Rogers static void perf_pmu__del_formats(struct list_head *formats)
1559d26383dcSNamhyung Kim {
1560d26383dcSNamhyung Kim 	struct perf_pmu_format *fmt, *tmp;
1561d26383dcSNamhyung Kim 
1562d26383dcSNamhyung Kim 	list_for_each_entry_safe(fmt, tmp, formats, list) {
1563d26383dcSNamhyung Kim 		list_del(&fmt->list);
1564efe98a7aSArnaldo Carvalho de Melo 		zfree(&fmt->name);
1565d26383dcSNamhyung Kim 		free(fmt);
1566d26383dcSNamhyung Kim 	}
1567d26383dcSNamhyung Kim }
1568d26383dcSNamhyung Kim 
156907d2b820SIan Rogers bool perf_pmu__has_format(const struct perf_pmu *pmu, const char *name)
157007d2b820SIan Rogers {
157107d2b820SIan Rogers 	struct perf_pmu_format *format;
157207d2b820SIan Rogers 
157307d2b820SIan Rogers 	list_for_each_entry(format, &pmu->format, list) {
157407d2b820SIan Rogers 		if (!strcmp(format->name, name))
157507d2b820SIan Rogers 			return true;
157607d2b820SIan Rogers 	}
157707d2b820SIan Rogers 	return false;
157807d2b820SIan Rogers }
157907d2b820SIan Rogers 
1580d504fae9SJohn Garry bool is_pmu_core(const char *name)
1581d504fae9SJohn Garry {
15826fbd67b0SThomas Richter 	return !strcmp(name, "cpu") || !strcmp(name, "cpum_cf") || is_sysfs_pmu_core(name);
15834bf7e81aSIan Rogers }
15844bf7e81aSIan Rogers 
15856fd1e519SIan Rogers bool perf_pmu__supports_legacy_cache(const struct perf_pmu *pmu)
15866fd1e519SIan Rogers {
1587e20d1f2fSIan Rogers 	return pmu->is_core;
15886fd1e519SIan Rogers }
15896fd1e519SIan Rogers 
159052c7b4d3SIan Rogers bool perf_pmu__auto_merge_stats(const struct perf_pmu *pmu)
159152c7b4d3SIan Rogers {
1592710dffc9SIan Rogers 	return !pmu->is_core || perf_pmus__num_core_pmus() == 1;
159352c7b4d3SIan Rogers }
159452c7b4d3SIan Rogers 
1595e6ff1eedSIan Rogers bool perf_pmu__have_event(struct perf_pmu *pmu, const char *name)
1596abe9544eSIan Rogers {
15978d4b6d37SIan Rogers 	if (perf_pmu__find_alias(pmu, name, /*load=*/ true) != NULL)
1598e6ff1eedSIan Rogers 		return true;
1599e6ff1eedSIan Rogers 	if (pmu->cpu_aliases_added || !pmu->events_table)
1600e6ff1eedSIan Rogers 		return false;
1601e6ff1eedSIan Rogers 	return pmu_events_table__find_event(pmu->events_table, pmu, name, NULL, NULL) == 0;
16024cabc3d1SAndi Kleen }
1603c3245d20SIan Rogers 
1604e6ff1eedSIan Rogers size_t perf_pmu__num_events(struct perf_pmu *pmu)
1605c3245d20SIan Rogers {
16068d4b6d37SIan Rogers 	size_t nr;
16078d4b6d37SIan Rogers 
16088d4b6d37SIan Rogers 	if (!pmu->sysfs_aliases_loaded)
16098d4b6d37SIan Rogers 		pmu_aliases_parse(pmu);
16108d4b6d37SIan Rogers 
16118d4b6d37SIan Rogers 	nr = pmu->sysfs_aliases;
1612c3245d20SIan Rogers 
1613e6ff1eedSIan Rogers 	if (pmu->cpu_aliases_added)
1614e6ff1eedSIan Rogers 		 nr += pmu->loaded_json_aliases;
1615e6ff1eedSIan Rogers 	else if (pmu->events_table)
1616e6ff1eedSIan Rogers 		nr += pmu_events_table__num_events(pmu->events_table, pmu) - pmu->loaded_json_aliases;
1617c3245d20SIan Rogers 
1618c3245d20SIan Rogers 	return pmu->selectable ? nr + 1 : nr;
1619c3245d20SIan Rogers }
1620c3245d20SIan Rogers 
1621c3245d20SIan Rogers static int sub_non_neg(int a, int b)
1622c3245d20SIan Rogers {
1623c3245d20SIan Rogers 	if (b > a)
1624c3245d20SIan Rogers 		return 0;
1625c3245d20SIan Rogers 	return a - b;
1626c3245d20SIan Rogers }
1627c3245d20SIan Rogers 
1628c3245d20SIan Rogers static char *format_alias(char *buf, int len, const struct perf_pmu *pmu,
1629cd4e1efbSIan Rogers 			  const struct perf_pmu_alias *alias, bool skip_duplicate_pmus)
1630c3245d20SIan Rogers {
1631c3245d20SIan Rogers 	struct parse_events_term *term;
1632cd4e1efbSIan Rogers 	int pmu_name_len = skip_duplicate_pmus
1633cd4e1efbSIan Rogers 		? pmu_name_len_no_suffix(pmu->name, /*num=*/NULL)
1634cd4e1efbSIan Rogers 		: (int)strlen(pmu->name);
1635cd4e1efbSIan Rogers 	int used = snprintf(buf, len, "%.*s/%s", pmu_name_len, pmu->name, alias->name);
1636c3245d20SIan Rogers 
1637c3245d20SIan Rogers 	list_for_each_entry(term, &alias->terms, list) {
1638c3245d20SIan Rogers 		if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
1639c3245d20SIan Rogers 			used += snprintf(buf + used, sub_non_neg(len, used),
1640c3245d20SIan Rogers 					",%s=%s", term->config,
1641c3245d20SIan Rogers 					term->val.str);
1642c3245d20SIan Rogers 	}
1643c3245d20SIan Rogers 
1644c3245d20SIan Rogers 	if (sub_non_neg(len, used) > 0) {
1645c3245d20SIan Rogers 		buf[used] = '/';
1646c3245d20SIan Rogers 		used++;
1647c3245d20SIan Rogers 	}
1648c3245d20SIan Rogers 	if (sub_non_neg(len, used) > 0) {
1649c3245d20SIan Rogers 		buf[used] = '\0';
1650c3245d20SIan Rogers 		used++;
1651c3245d20SIan Rogers 	} else
1652c3245d20SIan Rogers 		buf[len - 1] = '\0';
1653c3245d20SIan Rogers 
1654c3245d20SIan Rogers 	return buf;
1655c3245d20SIan Rogers }
1656c3245d20SIan Rogers 
1657cd4e1efbSIan Rogers int perf_pmu__for_each_event(struct perf_pmu *pmu, bool skip_duplicate_pmus,
1658cd4e1efbSIan Rogers 			     void *state, pmu_event_callback cb)
1659c3245d20SIan Rogers {
1660c3245d20SIan Rogers 	char buf[1024];
1661c3245d20SIan Rogers 	struct perf_pmu_alias *event;
1662c3245d20SIan Rogers 	struct pmu_event_info info = {
1663c3245d20SIan Rogers 		.pmu = pmu,
1664c3245d20SIan Rogers 	};
1665c3245d20SIan Rogers 	int ret = 0;
166630f0b435SIan Rogers 	struct strbuf sb;
1667c3245d20SIan Rogers 
166830f0b435SIan Rogers 	strbuf_init(&sb, /*hint=*/ 0);
1669e6ff1eedSIan Rogers 	pmu_add_cpu_aliases(pmu);
1670c3245d20SIan Rogers 	list_for_each_entry(event, &pmu->aliases, list) {
1671c3245d20SIan Rogers 		size_t buf_used;
1672c3245d20SIan Rogers 
1673c3245d20SIan Rogers 		info.pmu_name = event->pmu_name ?: pmu->name;
1674c3245d20SIan Rogers 		info.alias = NULL;
1675c3245d20SIan Rogers 		if (event->desc) {
1676c3245d20SIan Rogers 			info.name = event->name;
1677c3245d20SIan Rogers 			buf_used = 0;
1678c3245d20SIan Rogers 		} else {
1679cd4e1efbSIan Rogers 			info.name = format_alias(buf, sizeof(buf), pmu, event,
1680cd4e1efbSIan Rogers 						 skip_duplicate_pmus);
1681c3245d20SIan Rogers 			if (pmu->is_core) {
1682c3245d20SIan Rogers 				info.alias = info.name;
1683c3245d20SIan Rogers 				info.name = event->name;
1684c3245d20SIan Rogers 			}
1685c3245d20SIan Rogers 			buf_used = strlen(buf) + 1;
1686c3245d20SIan Rogers 		}
1687c3245d20SIan Rogers 		info.scale_unit = NULL;
1688c3245d20SIan Rogers 		if (strlen(event->unit) || event->scale != 1.0) {
1689c3245d20SIan Rogers 			info.scale_unit = buf + buf_used;
1690c3245d20SIan Rogers 			buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used,
1691c3245d20SIan Rogers 					"%G%s", event->scale, event->unit) + 1;
1692c3245d20SIan Rogers 		}
1693c3245d20SIan Rogers 		info.desc = event->desc;
1694c3245d20SIan Rogers 		info.long_desc = event->long_desc;
1695c3245d20SIan Rogers 		info.encoding_desc = buf + buf_used;
169630f0b435SIan Rogers 		parse_events_term__to_strbuf(&event->terms, &sb);
1697c3245d20SIan Rogers 		buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used,
169830f0b435SIan Rogers 				"%s/%s/", info.pmu_name, sb.buf) + 1;
1699c3245d20SIan Rogers 		info.topic = event->topic;
170030f0b435SIan Rogers 		info.str = sb.buf;
1701c3245d20SIan Rogers 		info.deprecated = event->deprecated;
1702c3245d20SIan Rogers 		ret = cb(state, &info);
1703c3245d20SIan Rogers 		if (ret)
170430f0b435SIan Rogers 			goto out;
170530f0b435SIan Rogers 		strbuf_setlen(&sb, /*len=*/ 0);
1706c3245d20SIan Rogers 	}
1707c3245d20SIan Rogers 	if (pmu->selectable) {
1708c3245d20SIan Rogers 		info.name = buf;
1709c3245d20SIan Rogers 		snprintf(buf, sizeof(buf), "%s//", pmu->name);
1710c3245d20SIan Rogers 		info.alias = NULL;
1711c3245d20SIan Rogers 		info.scale_unit = NULL;
1712c3245d20SIan Rogers 		info.desc = NULL;
1713c3245d20SIan Rogers 		info.long_desc = NULL;
1714c3245d20SIan Rogers 		info.encoding_desc = NULL;
1715c3245d20SIan Rogers 		info.topic = NULL;
1716c3245d20SIan Rogers 		info.pmu_name = pmu->name;
1717c3245d20SIan Rogers 		info.deprecated = false;
1718c3245d20SIan Rogers 		ret = cb(state, &info);
1719c3245d20SIan Rogers 	}
172030f0b435SIan Rogers out:
172130f0b435SIan Rogers 	strbuf_release(&sb);
1722c3245d20SIan Rogers 	return ret;
17234cabc3d1SAndi Kleen }
17247d4bdab5SAdrian Hunter 
1725e3edd6cfSIan Rogers bool pmu__name_match(const struct perf_pmu *pmu, const char *pmu_name)
1726e3edd6cfSIan Rogers {
1727e3edd6cfSIan Rogers 	return !strcmp(pmu->name, pmu_name) ||
1728d2045f87SIan Rogers 		(pmu->is_uncore && pmu_uncore_alias_match(pmu_name, pmu->name)) ||
1729d2045f87SIan Rogers 		/*
1730d2045f87SIan Rogers 		 * jevents and tests use default_core as a marker for any core
1731d2045f87SIan Rogers 		 * PMU as the PMU name varies across architectures.
1732d2045f87SIan Rogers 		 */
1733d2045f87SIan Rogers 	        (pmu->is_core && !strcmp(pmu_name, "default_core"));
1734e3edd6cfSIan Rogers }
1735e3edd6cfSIan Rogers 
1736251aa040SIan Rogers bool perf_pmu__is_software(const struct perf_pmu *pmu)
1737251aa040SIan Rogers {
1738251aa040SIan Rogers 	if (pmu->is_core || pmu->is_uncore || pmu->auxtrace)
1739251aa040SIan Rogers 		return false;
1740251aa040SIan Rogers 	switch (pmu->type) {
1741251aa040SIan Rogers 	case PERF_TYPE_HARDWARE:	return false;
1742251aa040SIan Rogers 	case PERF_TYPE_SOFTWARE:	return true;
1743251aa040SIan Rogers 	case PERF_TYPE_TRACEPOINT:	return true;
1744251aa040SIan Rogers 	case PERF_TYPE_HW_CACHE:	return false;
1745251aa040SIan Rogers 	case PERF_TYPE_RAW:		return false;
1746251aa040SIan Rogers 	case PERF_TYPE_BREAKPOINT:	return true;
1747251aa040SIan Rogers 	default: break;
1748251aa040SIan Rogers 	}
1749251aa040SIan Rogers 	return !strcmp(pmu->name, "kprobe") || !strcmp(pmu->name, "uprobe");
1750251aa040SIan Rogers }
1751251aa040SIan Rogers 
1752d50a79cdSJames Clark FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
17537d4bdab5SAdrian Hunter {
17547d4bdab5SAdrian Hunter 	char path[PATH_MAX];
17557d4bdab5SAdrian Hunter 
1756f8ad6018SJames Clark 	if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name) ||
1757f8ad6018SJames Clark 	    !file_available(path))
17587d4bdab5SAdrian Hunter 		return NULL;
17597d4bdab5SAdrian Hunter 
17607d4bdab5SAdrian Hunter 	return fopen(path, "r");
17617d4bdab5SAdrian Hunter }
17627d4bdab5SAdrian Hunter 
17633a69672eSNamhyung Kim FILE *perf_pmu__open_file_at(struct perf_pmu *pmu, int dirfd, const char *name)
17643a69672eSNamhyung Kim {
17653a69672eSNamhyung Kim 	int fd;
17663a69672eSNamhyung Kim 
17673a69672eSNamhyung Kim 	fd = perf_pmu__pathname_fd(dirfd, pmu->name, name, O_RDONLY);
17683a69672eSNamhyung Kim 	if (fd < 0)
17693a69672eSNamhyung Kim 		return NULL;
17703a69672eSNamhyung Kim 
17713a69672eSNamhyung Kim 	return fdopen(fd, "r");
17723a69672eSNamhyung Kim }
17733a69672eSNamhyung Kim 
17747d4bdab5SAdrian Hunter int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
17757d4bdab5SAdrian Hunter 			...)
17767d4bdab5SAdrian Hunter {
17777d4bdab5SAdrian Hunter 	va_list args;
17787d4bdab5SAdrian Hunter 	FILE *file;
17797d4bdab5SAdrian Hunter 	int ret = EOF;
17807d4bdab5SAdrian Hunter 
17817d4bdab5SAdrian Hunter 	va_start(args, fmt);
17827d4bdab5SAdrian Hunter 	file = perf_pmu__open_file(pmu, name);
17837d4bdab5SAdrian Hunter 	if (file) {
17847d4bdab5SAdrian Hunter 		ret = vfscanf(file, fmt, args);
17857d4bdab5SAdrian Hunter 		fclose(file);
17867d4bdab5SAdrian Hunter 	}
17877d4bdab5SAdrian Hunter 	va_end(args);
17887d4bdab5SAdrian Hunter 	return ret;
17897d4bdab5SAdrian Hunter }
17909fbc61f8SKan Liang 
17913a69672eSNamhyung Kim int perf_pmu__scan_file_at(struct perf_pmu *pmu, int dirfd, const char *name,
17923a69672eSNamhyung Kim 			   const char *fmt, ...)
17933a69672eSNamhyung Kim {
17943a69672eSNamhyung Kim 	va_list args;
17953a69672eSNamhyung Kim 	FILE *file;
17963a69672eSNamhyung Kim 	int ret = EOF;
17973a69672eSNamhyung Kim 
17983a69672eSNamhyung Kim 	va_start(args, fmt);
17993a69672eSNamhyung Kim 	file = perf_pmu__open_file_at(pmu, dirfd, name);
18003a69672eSNamhyung Kim 	if (file) {
18013a69672eSNamhyung Kim 		ret = vfscanf(file, fmt, args);
18023a69672eSNamhyung Kim 		fclose(file);
18033a69672eSNamhyung Kim 	}
18043a69672eSNamhyung Kim 	va_end(args);
18053a69672eSNamhyung Kim 	return ret;
18063a69672eSNamhyung Kim }
18073a69672eSNamhyung Kim 
1808c2b6a896SGerman Gomez bool perf_pmu__file_exists(struct perf_pmu *pmu, const char *name)
1809c2b6a896SGerman Gomez {
1810c2b6a896SGerman Gomez 	char path[PATH_MAX];
1811c2b6a896SGerman Gomez 
1812c2b6a896SGerman Gomez 	if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name))
1813c2b6a896SGerman Gomez 		return false;
1814c2b6a896SGerman Gomez 
1815c2b6a896SGerman Gomez 	return file_available(path);
1816c2b6a896SGerman Gomez }
1817c2b6a896SGerman Gomez 
18189fbc61f8SKan Liang static int perf_pmu__new_caps(struct list_head *list, char *name, char *value)
18199fbc61f8SKan Liang {
18209fbc61f8SKan Liang 	struct perf_pmu_caps *caps = zalloc(sizeof(*caps));
18219fbc61f8SKan Liang 
18229fbc61f8SKan Liang 	if (!caps)
18239fbc61f8SKan Liang 		return -ENOMEM;
18249fbc61f8SKan Liang 
18259fbc61f8SKan Liang 	caps->name = strdup(name);
18269fbc61f8SKan Liang 	if (!caps->name)
18279fbc61f8SKan Liang 		goto free_caps;
18289fbc61f8SKan Liang 	caps->value = strndup(value, strlen(value) - 1);
18299fbc61f8SKan Liang 	if (!caps->value)
18309fbc61f8SKan Liang 		goto free_name;
18319fbc61f8SKan Liang 	list_add_tail(&caps->list, list);
18329fbc61f8SKan Liang 	return 0;
18339fbc61f8SKan Liang 
18349fbc61f8SKan Liang free_name:
183557f14b5aSArnaldo Carvalho de Melo 	zfree(&caps->name);
18369fbc61f8SKan Liang free_caps:
18379fbc61f8SKan Liang 	free(caps);
18389fbc61f8SKan Liang 
18399fbc61f8SKan Liang 	return -ENOMEM;
18409fbc61f8SKan Liang }
18419fbc61f8SKan Liang 
1842eec11310SNamhyung Kim static void perf_pmu__del_caps(struct perf_pmu *pmu)
1843eec11310SNamhyung Kim {
1844eec11310SNamhyung Kim 	struct perf_pmu_caps *caps, *tmp;
1845eec11310SNamhyung Kim 
1846eec11310SNamhyung Kim 	list_for_each_entry_safe(caps, tmp, &pmu->caps, list) {
1847eec11310SNamhyung Kim 		list_del(&caps->list);
1848efe98a7aSArnaldo Carvalho de Melo 		zfree(&caps->name);
1849efe98a7aSArnaldo Carvalho de Melo 		zfree(&caps->value);
1850eec11310SNamhyung Kim 		free(caps);
1851eec11310SNamhyung Kim 	}
1852eec11310SNamhyung Kim }
1853eec11310SNamhyung Kim 
18549fbc61f8SKan Liang /*
18559fbc61f8SKan Liang  * Reading/parsing the given pmu capabilities, which should be located at:
18569fbc61f8SKan Liang  * /sys/bus/event_source/devices/<dev>/caps as sysfs group attributes.
18579fbc61f8SKan Liang  * Return the number of capabilities
18589fbc61f8SKan Liang  */
18599fbc61f8SKan Liang int perf_pmu__caps_parse(struct perf_pmu *pmu)
18609fbc61f8SKan Liang {
18619fbc61f8SKan Liang 	struct stat st;
18629fbc61f8SKan Liang 	char caps_path[PATH_MAX];
18639fbc61f8SKan Liang 	DIR *caps_dir;
18649fbc61f8SKan Liang 	struct dirent *evt_ent;
1865b39094d3SNamhyung Kim 	int caps_fd;
18663339ec44SRavi Bangoria 
18673339ec44SRavi Bangoria 	if (pmu->caps_initialized)
18683339ec44SRavi Bangoria 		return pmu->nr_caps;
18693339ec44SRavi Bangoria 
18703339ec44SRavi Bangoria 	pmu->nr_caps = 0;
18719fbc61f8SKan Liang 
1872f8ad6018SJames Clark 	if (!perf_pmu__pathname_scnprintf(caps_path, sizeof(caps_path), pmu->name, "caps"))
18739fbc61f8SKan Liang 		return -1;
18749fbc61f8SKan Liang 
18753339ec44SRavi Bangoria 	if (stat(caps_path, &st) < 0) {
18763339ec44SRavi Bangoria 		pmu->caps_initialized = true;
18779fbc61f8SKan Liang 		return 0;	/* no error if caps does not exist */
18783339ec44SRavi Bangoria 	}
18799fbc61f8SKan Liang 
18809fbc61f8SKan Liang 	caps_dir = opendir(caps_path);
18819fbc61f8SKan Liang 	if (!caps_dir)
18829fbc61f8SKan Liang 		return -EINVAL;
18839fbc61f8SKan Liang 
1884b39094d3SNamhyung Kim 	caps_fd = dirfd(caps_dir);
1885b39094d3SNamhyung Kim 
18869fbc61f8SKan Liang 	while ((evt_ent = readdir(caps_dir)) != NULL) {
18879fbc61f8SKan Liang 		char *name = evt_ent->d_name;
18889fbc61f8SKan Liang 		char value[128];
18899fbc61f8SKan Liang 		FILE *file;
1890b39094d3SNamhyung Kim 		int fd;
18919fbc61f8SKan Liang 
18929fbc61f8SKan Liang 		if (!strcmp(name, ".") || !strcmp(name, ".."))
18939fbc61f8SKan Liang 			continue;
18949fbc61f8SKan Liang 
1895b39094d3SNamhyung Kim 		fd = openat(caps_fd, name, O_RDONLY);
18960ea8920eSIan Rogers 		if (fd == -1)
18979fbc61f8SKan Liang 			continue;
18980ea8920eSIan Rogers 		file = fdopen(fd, "r");
18990ea8920eSIan Rogers 		if (!file) {
19000ea8920eSIan Rogers 			close(fd);
19010ea8920eSIan Rogers 			continue;
19020ea8920eSIan Rogers 		}
19039fbc61f8SKan Liang 
19049fbc61f8SKan Liang 		if (!fgets(value, sizeof(value), file) ||
19059fbc61f8SKan Liang 		    (perf_pmu__new_caps(&pmu->caps, name, value) < 0)) {
19069fbc61f8SKan Liang 			fclose(file);
19079fbc61f8SKan Liang 			continue;
19089fbc61f8SKan Liang 		}
19099fbc61f8SKan Liang 
19103339ec44SRavi Bangoria 		pmu->nr_caps++;
19119fbc61f8SKan Liang 		fclose(file);
19129fbc61f8SKan Liang 	}
19139fbc61f8SKan Liang 
19149fbc61f8SKan Liang 	closedir(caps_dir);
19159fbc61f8SKan Liang 
19163339ec44SRavi Bangoria 	pmu->caps_initialized = true;
19173339ec44SRavi Bangoria 	return pmu->nr_caps;
19189fbc61f8SKan Liang }
1919e4064776SJin Yao 
1920b9f01032SIan Rogers static void perf_pmu__compute_config_masks(struct perf_pmu *pmu)
1921e4064776SJin Yao {
1922e4064776SJin Yao 	struct perf_pmu_format *format;
1923b9f01032SIan Rogers 
1924b9f01032SIan Rogers 	if (pmu->config_masks_computed)
1925b9f01032SIan Rogers 		return;
1926e4064776SJin Yao 
1927e4064776SJin Yao 	list_for_each_entry(format, &pmu->format, list)	{
1928b9f01032SIan Rogers 		unsigned int i;
1929b9f01032SIan Rogers 		__u64 *mask;
1930b9f01032SIan Rogers 
1931b9f01032SIan Rogers 		if (format->value >= PERF_PMU_FORMAT_VALUE_CONFIG_END)
1932e4064776SJin Yao 			continue;
1933e4064776SJin Yao 
1934b9f01032SIan Rogers 		pmu->config_masks_present = true;
1935b9f01032SIan Rogers 		mask = &pmu->config_masks[format->value];
1936b9f01032SIan Rogers 
1937e4064776SJin Yao 		for_each_set_bit(i, format->bits, PERF_PMU_FORMAT_BITS)
1938b9f01032SIan Rogers 			*mask |= 1ULL << i;
1939e4064776SJin Yao 	}
1940b9f01032SIan Rogers 	pmu->config_masks_computed = true;
1941b9f01032SIan Rogers }
1942b9f01032SIan Rogers 
1943b9f01032SIan Rogers void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config,
1944b9f01032SIan Rogers 				   const char *name, int config_num,
1945b9f01032SIan Rogers 				   const char *config_name)
1946b9f01032SIan Rogers {
1947b9f01032SIan Rogers 	__u64 bits;
1948b9f01032SIan Rogers 	char buf[100];
1949b9f01032SIan Rogers 
1950b9f01032SIan Rogers 	perf_pmu__compute_config_masks(pmu);
1951e4064776SJin Yao 
1952e4064776SJin Yao 	/*
1953e4064776SJin Yao 	 * Kernel doesn't export any valid format bits.
1954e4064776SJin Yao 	 */
1955b9f01032SIan Rogers 	if (!pmu->config_masks_present)
1956e4064776SJin Yao 		return;
1957e4064776SJin Yao 
1958b9f01032SIan Rogers 	bits = config & ~pmu->config_masks[config_num];
1959e4064776SJin Yao 	if (bits == 0)
1960e4064776SJin Yao 		return;
1961e4064776SJin Yao 
1962e4064776SJin Yao 	bitmap_scnprintf((unsigned long *)&bits, sizeof(bits) * 8, buf, sizeof(buf));
1963e4064776SJin Yao 
1964b9f01032SIan Rogers 	pr_warning("WARNING: event '%s' not valid (bits %s of %s "
1965e4064776SJin Yao 		   "'%llx' not supported by kernel)!\n",
1966b9f01032SIan Rogers 		   name ?: "N/A", buf, config_name, config);
1967e4064776SJin Yao }
1968c5a26ea4SJin Yao 
1969c091ee90SIan Rogers int perf_pmu__match(const char *pattern, const char *name, const char *tok)
1970c47a5599SJin Yao {
197113d60ba0SKan Liang 	if (!name)
197213d60ba0SKan Liang 		return -1;
197313d60ba0SKan Liang 
1974c47a5599SJin Yao 	if (fnmatch(pattern, name, 0))
1975c47a5599SJin Yao 		return -1;
1976c47a5599SJin Yao 
1977240e6fd0SIan Rogers 	if (tok && !perf_pmu__match_ignoring_suffix(name, tok))
1978c47a5599SJin Yao 		return -1;
1979c47a5599SJin Yao 
1980c47a5599SJin Yao 	return 0;
1981c47a5599SJin Yao }
19821d3351e6SJin Yao 
1983acef233bSJing Zhang double __weak perf_pmu__cpu_slots_per_cycle(void)
1984acef233bSJing Zhang {
1985acef233bSJing Zhang 	return NAN;
1986acef233bSJing Zhang }
1987f8ad6018SJames Clark 
1988f8ad6018SJames Clark int perf_pmu__event_source_devices_scnprintf(char *pathname, size_t size)
1989f8ad6018SJames Clark {
1990f8ad6018SJames Clark 	const char *sysfs = sysfs__mountpoint();
1991f8ad6018SJames Clark 
1992f8ad6018SJames Clark 	if (!sysfs)
1993f8ad6018SJames Clark 		return 0;
1994f8ad6018SJames Clark 	return scnprintf(pathname, size, "%s/bus/event_source/devices/", sysfs);
1995f8ad6018SJames Clark }
1996f8ad6018SJames Clark 
1997e293a5e8SNamhyung Kim int perf_pmu__event_source_devices_fd(void)
1998e293a5e8SNamhyung Kim {
1999e293a5e8SNamhyung Kim 	char path[PATH_MAX];
2000e293a5e8SNamhyung Kim 	const char *sysfs = sysfs__mountpoint();
2001e293a5e8SNamhyung Kim 
2002e293a5e8SNamhyung Kim 	if (!sysfs)
2003e293a5e8SNamhyung Kim 		return -1;
2004e293a5e8SNamhyung Kim 
2005e293a5e8SNamhyung Kim 	scnprintf(path, sizeof(path), "%s/bus/event_source/devices/", sysfs);
2006e293a5e8SNamhyung Kim 	return open(path, O_DIRECTORY);
2007e293a5e8SNamhyung Kim }
2008e293a5e8SNamhyung Kim 
2009f8ad6018SJames Clark /*
2010f8ad6018SJames Clark  * Fill 'buf' with the path to a file or folder in 'pmu_name' in
2011f8ad6018SJames Clark  * sysfs. For example if pmu_name = "cs_etm" and 'filename' = "format"
2012f8ad6018SJames Clark  * then pathname will be filled with
2013f8ad6018SJames Clark  * "/sys/bus/event_source/devices/cs_etm/format"
2014f8ad6018SJames Clark  *
2015e1a3aad3SIan Rogers  * Return 0 if the sysfs mountpoint couldn't be found, if no characters were
2016e1a3aad3SIan Rogers  * written or if the buffer size is exceeded.
2017f8ad6018SJames Clark  */
2018f8ad6018SJames Clark int perf_pmu__pathname_scnprintf(char *buf, size_t size,
2019f8ad6018SJames Clark 				 const char *pmu_name, const char *filename)
2020f8ad6018SJames Clark {
2021e1a3aad3SIan Rogers 	size_t len;
2022f8ad6018SJames Clark 
2023e1a3aad3SIan Rogers 	len = perf_pmu__event_source_devices_scnprintf(buf, size);
2024e1a3aad3SIan Rogers 	if (!len || (len + strlen(pmu_name) + strlen(filename) + 1)  >= size)
2025f8ad6018SJames Clark 		return 0;
2026e1a3aad3SIan Rogers 
2027e1a3aad3SIan Rogers 	return scnprintf(buf + len, size - len, "%s/%s", pmu_name, filename);
2028f8ad6018SJames Clark }
2029eec11310SNamhyung Kim 
2030e293a5e8SNamhyung Kim int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *filename, int flags)
2031e293a5e8SNamhyung Kim {
2032e293a5e8SNamhyung Kim 	char path[PATH_MAX];
2033e293a5e8SNamhyung Kim 
2034e293a5e8SNamhyung Kim 	scnprintf(path, sizeof(path), "%s/%s", pmu_name, filename);
2035e293a5e8SNamhyung Kim 	return openat(dirfd, path, flags);
2036e293a5e8SNamhyung Kim }
2037e293a5e8SNamhyung Kim 
20381eaf496eSIan Rogers void perf_pmu__delete(struct perf_pmu *pmu)
2039eec11310SNamhyung Kim {
2040eec11310SNamhyung Kim 	perf_pmu__del_formats(&pmu->format);
2041eec11310SNamhyung Kim 	perf_pmu__del_aliases(pmu);
2042eec11310SNamhyung Kim 	perf_pmu__del_caps(pmu);
2043eec11310SNamhyung Kim 
2044eec11310SNamhyung Kim 	perf_cpu_map__put(pmu->cpus);
2045eec11310SNamhyung Kim 
2046efe98a7aSArnaldo Carvalho de Melo 	zfree(&pmu->default_config);
2047efe98a7aSArnaldo Carvalho de Melo 	zfree(&pmu->name);
2048efe98a7aSArnaldo Carvalho de Melo 	zfree(&pmu->alias_name);
2049b7823045SIan Rogers 	zfree(&pmu->id);
2050eec11310SNamhyung Kim 	free(pmu);
2051eec11310SNamhyung Kim }
20529d5da30eSJames Clark 
20539d5da30eSJames Clark struct perf_pmu *pmu__find_core_pmu(void)
20549d5da30eSJames Clark {
20559d5da30eSJames Clark 	struct perf_pmu *pmu = NULL;
20569d5da30eSJames Clark 
20579d5da30eSJames Clark 	while ((pmu = perf_pmus__scan_core(pmu))) {
20589d5da30eSJames Clark 		/*
20599d5da30eSJames Clark 		 * The cpumap should cover all CPUs. Otherwise, some CPUs may
20609d5da30eSJames Clark 		 * not support some events or have different event IDs.
20619d5da30eSJames Clark 		 */
20629d5da30eSJames Clark 		if (RC_CHK_ACCESS(pmu->cpus)->nr != cpu__max_cpu().cpu)
20639d5da30eSJames Clark 			return NULL;
20649d5da30eSJames Clark 
20659d5da30eSJames Clark 		return pmu;
20669d5da30eSJames Clark 	}
20679d5da30eSJames Clark 	return NULL;
20689d5da30eSJames Clark }
2069