xref: /openbmc/linux/tools/perf/util/pmu.h (revision b181f7029bd71238ac2754ce7052dffd69432085)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2cd82a32eSJiri Olsa #ifndef __PMU_H
3cd82a32eSJiri Olsa #define __PMU_H
4cd82a32eSJiri Olsa 
5d944c4eeSBorislav Petkov #include <linux/bitmap.h>
63ee350fbSArnaldo Carvalho de Melo #include <linux/compiler.h>
7d2709c7cSDavid Howells #include <linux/perf_event.h>
844462430SJin Yao #include <linux/list.h>
9dc098b35SAndi Kleen #include <stdbool.h>
10d50a79cdSJames Clark #include <stdio.h>
11e64b020bSJiri Olsa #include "parse-events.h"
12e45ad701SJohn Garry #include "pmu-events/pmu-events.h"
13cd82a32eSJiri Olsa 
1435ac0cadSArnaldo Carvalho de Melo struct evsel_config_term;
151d3351e6SJin Yao struct perf_cpu_map;
16e5c6109fSIan Rogers struct print_callbacks;
17eb563d66SArnaldo Carvalho de Melo 
18cd82a32eSJiri Olsa enum {
19cd82a32eSJiri Olsa 	PERF_PMU_FORMAT_VALUE_CONFIG,
20cd82a32eSJiri Olsa 	PERF_PMU_FORMAT_VALUE_CONFIG1,
21cd82a32eSJiri Olsa 	PERF_PMU_FORMAT_VALUE_CONFIG2,
22204e7c49SRob Herring 	PERF_PMU_FORMAT_VALUE_CONFIG3,
23e552b7beSRob Herring 	PERF_PMU_FORMAT_VALUE_CONFIG_END,
24cd82a32eSJiri Olsa };
25cd82a32eSJiri Olsa 
26cd82a32eSJiri Olsa #define PERF_PMU_FORMAT_BITS 64
2713d60ba0SKan Liang #define MAX_PMU_NAME_LEN 128
28cd82a32eSJiri Olsa 
29dc0a6202SAdrian Hunter struct perf_event_attr;
30dc0a6202SAdrian Hunter 
319fbc61f8SKan Liang struct perf_pmu_caps {
329fbc61f8SKan Liang 	char *name;
339fbc61f8SKan Liang 	char *value;
349fbc61f8SKan Liang 	struct list_head list;
359fbc61f8SKan Liang };
369fbc61f8SKan Liang 
37fe13d43dSIan Rogers /**
38240e6fd0SIan Rogers  * struct perf_pmu
39fe13d43dSIan Rogers  */
40cd82a32eSJiri Olsa struct perf_pmu {
41fe13d43dSIan Rogers 	/** @name: The name of the PMU such as "cpu". */
42c091ee90SIan Rogers 	const char *name;
43fe13d43dSIan Rogers 	/**
44fe13d43dSIan Rogers 	 * @alias_name: Optional alternate name for the PMU determined in
45fe13d43dSIan Rogers 	 * architecture specific code.
46fe13d43dSIan Rogers 	 */
4713d60ba0SKan Liang 	char *alias_name;
48fe13d43dSIan Rogers 	/**
49fe13d43dSIan Rogers 	 * @id: Optional PMU identifier read from
50fe13d43dSIan Rogers 	 * <sysfs>/bus/event_source/devices/<name>/identifier.
51fe13d43dSIan Rogers 	 */
52b7823045SIan Rogers 	const char *id;
53fe13d43dSIan Rogers 	/**
54fe13d43dSIan Rogers 	 * @type: Perf event attributed type value, read from
55fe13d43dSIan Rogers 	 * <sysfs>/bus/event_source/devices/<name>/type.
56fe13d43dSIan Rogers 	 */
57cd82a32eSJiri Olsa 	__u32 type;
58fe13d43dSIan Rogers 	/**
59fe13d43dSIan Rogers 	 * @selectable: Can the PMU name be selected as if it were an event?
60fe13d43dSIan Rogers 	 */
6142634bc7SAdrian Hunter 	bool selectable;
62fe13d43dSIan Rogers 	/**
63e20d1f2fSIan Rogers 	 * @is_core: Is the PMU the core CPU PMU? Determined by the name being
64e20d1f2fSIan Rogers 	 * "cpu" or by the presence of
65e20d1f2fSIan Rogers 	 * <sysfs>/bus/event_source/devices/<name>/cpus. There may be >1 core
66e20d1f2fSIan Rogers 	 * PMU on systems like Intel hybrid.
67e20d1f2fSIan Rogers 	 */
68e20d1f2fSIan Rogers 	bool is_core;
69e20d1f2fSIan Rogers 	/**
70fe13d43dSIan Rogers 	 * @is_uncore: Is the PMU not within the CPU core? Determined by the
71fe13d43dSIan Rogers 	 * presence of <sysfs>/bus/event_source/devices/<name>/cpumask.
72fe13d43dSIan Rogers 	 */
7366ec1191SMark Rutland 	bool is_uncore;
74fe13d43dSIan Rogers 	/**
75fe13d43dSIan Rogers 	 * @auxtrace: Are events auxiliary events? Determined in architecture
76fe13d43dSIan Rogers 	 * specific code.
77fe13d43dSIan Rogers 	 */
78f0bb7ee8SAdrian Hunter 	bool auxtrace;
79fe13d43dSIan Rogers 	/**
8068c25043SIan Rogers 	 * @formats_checked: Only check PMU's formats are valid for
8168c25043SIan Rogers 	 * perf_event_attr once.
8268c25043SIan Rogers 	 */
8368c25043SIan Rogers 	bool formats_checked;
84b9f01032SIan Rogers 	/** @config_masks_present: Are there config format values? */
85b9f01032SIan Rogers 	bool config_masks_present;
86b9f01032SIan Rogers 	/** @config_masks_computed: Set when masks are lazily computed. */
87b9f01032SIan Rogers 	bool config_masks_computed;
8868c25043SIan Rogers 	/**
89fe13d43dSIan Rogers 	 * @max_precise: Number of levels of :ppp precision supported by the
90fe13d43dSIan Rogers 	 * PMU, read from
91fe13d43dSIan Rogers 	 * <sysfs>/bus/event_source/devices/<name>/caps/max_precise.
92fe13d43dSIan Rogers 	 */
9390a86bdeSJiri Olsa 	int max_precise;
94fe13d43dSIan Rogers 	/**
95fe13d43dSIan Rogers 	 * @default_config: Optional default perf_event_attr determined in
96fe13d43dSIan Rogers 	 * architecture specific code.
97fe13d43dSIan Rogers 	 */
98dc0a6202SAdrian Hunter 	struct perf_event_attr *default_config;
99fe13d43dSIan Rogers 	/**
100fe13d43dSIan Rogers 	 * @cpus: Empty or the contents of either of:
101fe13d43dSIan Rogers 	 * <sysfs>/bus/event_source/devices/<name>/cpumask.
102fe13d43dSIan Rogers 	 * <sysfs>/bus/event_source/devices/<cpu>/cpus.
103fe13d43dSIan Rogers 	 */
104f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
105fe13d43dSIan Rogers 	/**
106fe13d43dSIan Rogers 	 * @format: Holds the contents of files read from
107fe13d43dSIan Rogers 	 * <sysfs>/bus/event_source/devices/<name>/format/. The contents specify
108fe13d43dSIan Rogers 	 * which event parameter changes what config, config1 or config2 bits.
109fe13d43dSIan Rogers 	 */
110fe13d43dSIan Rogers 	struct list_head format;
111fe13d43dSIan Rogers 	/**
112fe13d43dSIan Rogers 	 * @aliases: List of struct perf_pmu_alias. Each alias corresponds to an
113fe13d43dSIan Rogers 	 * event read from <sysfs>/bus/event_source/devices/<name>/events/ or
114fe13d43dSIan Rogers 	 * from json events in pmu-events.c.
115fe13d43dSIan Rogers 	 */
116fe13d43dSIan Rogers 	struct list_head aliases;
1177c52f10cSIan Rogers 	/**
1187c52f10cSIan Rogers 	 * @events_table: The events table for json events in pmu-events.c.
1197c52f10cSIan Rogers 	 */
1207c52f10cSIan Rogers 	const struct pmu_events_table *events_table;
121e6ff1eedSIan Rogers 	/** @sysfs_aliases: Number of sysfs aliases loaded. */
122e6ff1eedSIan Rogers 	uint32_t sysfs_aliases;
123*5af155f9SIan Rogers 	/** @cpu_json_aliases: Number of json event aliases loaded specific to the CPUID. */
124*5af155f9SIan Rogers 	uint32_t cpu_json_aliases;
125*5af155f9SIan Rogers 	/** @sys_json_aliases: Number of json event aliases loaded matching the PMU's identifier. */
126*5af155f9SIan Rogers 	uint32_t sys_json_aliases;
1278d4b6d37SIan Rogers 	/** @sysfs_aliases_loaded: Are sysfs aliases loaded from disk? */
1288d4b6d37SIan Rogers 	bool sysfs_aliases_loaded;
129e6ff1eedSIan Rogers 	/**
130e6ff1eedSIan Rogers 	 * @cpu_aliases_added: Have all json events table entries for the PMU
131e6ff1eedSIan Rogers 	 * been added?
132e6ff1eedSIan Rogers 	 */
133e6ff1eedSIan Rogers 	bool cpu_aliases_added;
134fe13d43dSIan Rogers 	/** @caps_initialized: Has the list caps been initialized? */
1353339ec44SRavi Bangoria 	bool caps_initialized;
136fe13d43dSIan Rogers 	/** @nr_caps: The length of the list caps. */
1373339ec44SRavi Bangoria 	u32 nr_caps;
138fe13d43dSIan Rogers 	/**
139fe13d43dSIan Rogers 	 * @caps: Holds the contents of files read from
140fe13d43dSIan Rogers 	 * <sysfs>/bus/event_source/devices/<name>/caps/.
141fe13d43dSIan Rogers 	 *
142fe13d43dSIan Rogers 	 * The contents are pairs of the filename with the value of its
143fe13d43dSIan Rogers 	 * contents, for example, max_precise (see above) may have a value of 3.
144fe13d43dSIan Rogers 	 */
145fe13d43dSIan Rogers 	struct list_head caps;
146fe13d43dSIan Rogers 	/** @list: Element on pmus list in pmu.c. */
147fe13d43dSIan Rogers 	struct list_head list;
1483500eeebSNamhyung Kim 
149fe13d43dSIan Rogers 	/**
150b9f01032SIan Rogers 	 * @config_masks: Derived from the PMU's format data, bits that are
151b9f01032SIan Rogers 	 * valid within the config value.
152b9f01032SIan Rogers 	 */
153b9f01032SIan Rogers 	__u64 config_masks[PERF_PMU_FORMAT_VALUE_CONFIG_END];
154b9f01032SIan Rogers 
155b9f01032SIan Rogers 	/**
156fe13d43dSIan Rogers 	 * @missing_features: Features to inhibit when events on this PMU are
157fe13d43dSIan Rogers 	 * opened.
158fe13d43dSIan Rogers 	 */
1593500eeebSNamhyung Kim 	struct {
160fe13d43dSIan Rogers 		/**
161fe13d43dSIan Rogers 		 * @exclude_guest: Disables perf_event_attr exclude_guest and
162fe13d43dSIan Rogers 		 * exclude_host.
163fe13d43dSIan Rogers 		 */
1643500eeebSNamhyung Kim 		bool exclude_guest;
1653500eeebSNamhyung Kim 	} missing_features;
166cd82a32eSJiri Olsa };
167cd82a32eSJiri Olsa 
168fe13d43dSIan Rogers /** @perf_pmu__fake: A special global PMU used for testing. */
169e46fc8d9SArnaldo Carvalho de Melo extern struct perf_pmu perf_pmu__fake;
170e46fc8d9SArnaldo Carvalho de Melo 
17146441bdcSMatt Fleming struct perf_pmu_info {
17246441bdcSMatt Fleming 	const char *unit;
17346441bdcSMatt Fleming 	double scale;
174044330c1SMatt Fleming 	bool per_pkg;
1751d9e446bSJiri Olsa 	bool snapshot;
17646441bdcSMatt Fleming };
17746441bdcSMatt Fleming 
178c3245d20SIan Rogers struct pmu_event_info {
179c3245d20SIan Rogers 	const struct perf_pmu *pmu;
180c3245d20SIan Rogers 	const char *name;
181c3245d20SIan Rogers 	const char* alias;
182c3245d20SIan Rogers 	const char *scale_unit;
183c3245d20SIan Rogers 	const char *desc;
184c3245d20SIan Rogers 	const char *long_desc;
185c3245d20SIan Rogers 	const char *encoding_desc;
186c3245d20SIan Rogers 	const char *topic;
187c3245d20SIan Rogers 	const char *pmu_name;
188c3245d20SIan Rogers 	const char *str;
189a7f6c8c8SJin Yao 	bool deprecated;
190dcb4e102SKan Liang };
191dcb4e102SKan Liang 
192c3245d20SIan Rogers typedef int (*pmu_event_callback)(void *state, struct pmu_event_info *info);
193c3245d20SIan Rogers 
194838a8c5fSIan Rogers void pmu_add_sys_aliases(struct perf_pmu *pmu);
195cd82a32eSJiri Olsa int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
196e64b020bSJiri Olsa 		     struct list_head *head_terms,
197e64b020bSJiri Olsa 		     struct parse_events_error *error);
198804fee5dSIan Rogers int perf_pmu__config_terms(struct perf_pmu *pmu,
199cff7f956SJiri Olsa 			   struct perf_event_attr *attr,
200dc0a6202SAdrian Hunter 			   struct list_head *head_terms,
201e64b020bSJiri Olsa 			   bool zero, struct parse_events_error *error);
202da6a5afdSIan Rogers __u64 perf_pmu__format_bits(struct perf_pmu *pmu, const char *name);
2037eb54733SIan Rogers int perf_pmu__format_type(struct perf_pmu *pmu, const char *name);
204410136f5SStephane Eranian int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
2059d31cb93SIan Rogers 			  struct perf_pmu_info *info, struct parse_events_error *err);
206c3245d20SIan Rogers int perf_pmu__find_event(struct perf_pmu *pmu, const char *event, void *state, pmu_event_callback cb);
207cd82a32eSJiri Olsa 
20850402641SIan Rogers int perf_pmu__format_parse(struct perf_pmu *pmu, int dirfd, bool eager_load);
20950402641SIan Rogers void perf_pmu_format__set_value(void *format, int config, unsigned long *bits);
21007d2b820SIan Rogers bool perf_pmu__has_format(const struct perf_pmu *pmu, const char *name);
211cd82a32eSJiri Olsa 
212d504fae9SJohn Garry bool is_pmu_core(const char *name);
2136fd1e519SIan Rogers bool perf_pmu__supports_legacy_cache(const struct perf_pmu *pmu);
21452c7b4d3SIan Rogers bool perf_pmu__auto_merge_stats(const struct perf_pmu *pmu);
215e6ff1eedSIan Rogers bool perf_pmu__have_event(struct perf_pmu *pmu, const char *name);
216e6ff1eedSIan Rogers size_t perf_pmu__num_events(struct perf_pmu *pmu);
217cd4e1efbSIan Rogers int perf_pmu__for_each_event(struct perf_pmu *pmu, bool skip_duplicate_pmus,
218cd4e1efbSIan Rogers 			     void *state, pmu_event_callback cb);
219e3edd6cfSIan Rogers bool pmu__name_match(const struct perf_pmu *pmu, const char *pmu_name);
220c3245d20SIan Rogers 
221251aa040SIan Rogers /**
222251aa040SIan Rogers  * perf_pmu_is_software - is the PMU a software PMU as in it uses the
223251aa040SIan Rogers  *                        perf_sw_context in the kernel?
224251aa040SIan Rogers  */
225251aa040SIan Rogers bool perf_pmu__is_software(const struct perf_pmu *pmu);
226dc098b35SAndi Kleen 
2273a69672eSNamhyung Kim FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name);
2283a69672eSNamhyung Kim FILE *perf_pmu__open_file_at(struct perf_pmu *pmu, int dirfd, const char *name);
2293a69672eSNamhyung Kim 
2303ee350fbSArnaldo Carvalho de Melo int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt, ...) __scanf(3, 4);
2313a69672eSNamhyung Kim int perf_pmu__scan_file_at(struct perf_pmu *pmu, int dirfd, const char *name,
2323a69672eSNamhyung Kim 			   const char *fmt, ...) __scanf(4, 5);
2337d4bdab5SAdrian Hunter 
234c2b6a896SGerman Gomez bool perf_pmu__file_exists(struct perf_pmu *pmu, const char *name);
235c2b6a896SGerman Gomez 
236cd82a32eSJiri Olsa int perf_pmu__test(void);
237dc0a6202SAdrian Hunter 
238dc0a6202SAdrian Hunter struct perf_event_attr *perf_pmu__get_default_config(struct perf_pmu *pmu);
239838a8c5fSIan Rogers void pmu_add_cpu_aliases_table(struct perf_pmu *pmu,
2401ba3752aSIan Rogers 			       const struct pmu_events_table *table);
241dc0a6202SAdrian Hunter 
24229be2fe0SIan Rogers char *perf_pmu__getcpuid(struct perf_pmu *pmu);
2431ba3752aSIan Rogers const struct pmu_events_table *pmu_events_table__find(void);
244f8ea2c15SIan Rogers const struct pmu_metrics_table *pmu_metrics_table__find(void);
245c5aaeae2SJing Zhang bool pmu_uncore_identifier_match(const char *compat, const char *id);
246d77ade9fSAndi Kleen 
247a55ab7c4SJin Yao int perf_pmu__convert_scale(const char *scale, char **end, double *sval);
248a55ab7c4SJin Yao 
2499fbc61f8SKan Liang int perf_pmu__caps_parse(struct perf_pmu *pmu);
2509fbc61f8SKan Liang 
251e4064776SJin Yao void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config,
252b9f01032SIan Rogers 				   const char *name, int config_num,
253b9f01032SIan Rogers 				   const char *config_name);
254e552b7beSRob Herring void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu);
255e4064776SJin Yao 
256c091ee90SIan Rogers int perf_pmu__match(const char *pattern, const char *name, const char *tok);
257c5a26ea4SJin Yao 
258c091ee90SIan Rogers const char *pmu_find_real_name(const char *name);
259c091ee90SIan Rogers const char *pmu_find_alias_name(const char *name);
260acef233bSJing Zhang double perf_pmu__cpu_slots_per_cycle(void);
261f8ad6018SJames Clark int perf_pmu__event_source_devices_scnprintf(char *pathname, size_t size);
262f8ad6018SJames Clark int perf_pmu__pathname_scnprintf(char *buf, size_t size,
263f8ad6018SJames Clark 				 const char *pmu_name, const char *filename);
264e293a5e8SNamhyung Kim int perf_pmu__event_source_devices_fd(void);
265e293a5e8SNamhyung Kim int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *filename, int flags);
266f8ad6018SJames Clark 
2671eaf496eSIan Rogers struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char *lookup_name);
268628eaa4eSIan Rogers struct perf_pmu *perf_pmu__create_placeholder_core_pmu(struct list_head *core_pmus);
2691eaf496eSIan Rogers void perf_pmu__delete(struct perf_pmu *pmu);
270f989dc00SJames Clark struct perf_pmu *perf_pmus__find_core_pmu(void);
271930e16acSIan Rogers const char *perf_pmu__name_from_config(struct perf_pmu *pmu, u64 config);
272eec11310SNamhyung Kim 
273cd82a32eSJiri Olsa #endif /* __PMU_H */
274