xref: /linux/tools/perf/util/pmu.h (revision ce7948448d99fcb5e8a564c46fbc96a9282b78eb)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __PMU_H
3 #define __PMU_H
4 
5 #include <linux/bitmap.h>
6 #include <linux/compiler.h>
7 #include <linux/perf_event.h>
8 #include <linux/list.h>
9 #include <stdbool.h>
10 #include <stdio.h>
11 #include "parse-events.h"
12 #include "pmu-events/pmu-events.h"
13 #include "map_symbol.h"
14 #include "mem-events.h"
15 
16 struct evsel_config_term;
17 struct hashmap;
18 struct perf_cpu_map;
19 struct perf_env;
20 struct print_callbacks;
21 
22 enum {
23 	PERF_PMU_FORMAT_VALUE_CONFIG,
24 	PERF_PMU_FORMAT_VALUE_CONFIG1,
25 	PERF_PMU_FORMAT_VALUE_CONFIG2,
26 	PERF_PMU_FORMAT_VALUE_CONFIG3,
27 	PERF_PMU_FORMAT_VALUE_CONFIG4,
28 	PERF_PMU_FORMAT_VALUE_CONFIG_END,
29 };
30 
31 #define PERF_PMU_FORMAT_BITS 64
32 #define MAX_PMU_NAME_LEN 128
33 
34 struct perf_event_attr;
35 
36 struct perf_pmu_caps {
37 	char *name;
38 	char *value;
39 	struct list_head list;
40 };
41 
42 enum pmu_kind {
43 	/* A perf event syscall PMU. */
44 	PERF_PMU_KIND_PE,
45 	/* A perf tool provided DRM PMU. */
46 	PERF_PMU_KIND_DRM,
47 	/* A perf tool provided HWMON PMU. */
48 	PERF_PMU_KIND_HWMON,
49 	/* Perf tool provided PMU for tool events like time. */
50 	PERF_PMU_KIND_TOOL,
51 	/* A testing PMU kind. */
52 	PERF_PMU_KIND_FAKE
53 };
54 
55 enum {
56 	PERF_PMU_TYPE_PE_START    = 0,
57 	PERF_PMU_TYPE_PE_END      = 0xFFFDFFFF,
58 	PERF_PMU_TYPE_DRM_START   = 0xFFFE0000,
59 	PERF_PMU_TYPE_DRM_END     = 0xFFFEFFFF,
60 	PERF_PMU_TYPE_HWMON_START = 0xFFFF0000,
61 	PERF_PMU_TYPE_HWMON_END   = 0xFFFFFFFD,
62 	PERF_PMU_TYPE_TOOL = 0xFFFFFFFE,
63 	PERF_PMU_TYPE_FAKE = 0xFFFFFFFF,
64 };
65 
66 /**
67  * struct perf_pmu
68  */
69 struct perf_pmu {
70 	/** @name: The name of the PMU such as "cpu". */
71 	const char *name;
72 	/**
73 	 * @alias_name: Optional alternate name for the PMU determined in
74 	 * architecture specific code.
75 	 */
76 	char *alias_name;
77 	/**
78 	 * @id: Optional PMU identifier read from
79 	 * <sysfs>/bus/event_source/devices/<name>/identifier.
80 	 */
81 	const char *id;
82 	/**
83 	 * @type: Perf event attributed type value, read from
84 	 * <sysfs>/bus/event_source/devices/<name>/type.
85 	 */
86 	__u32 type;
87 	/**
88 	 * @selectable: Can the PMU name be selected as if it were an event?
89 	 */
90 	bool selectable;
91 	/**
92 	 * @is_core: Is the PMU the core CPU PMU? Determined by the name being
93 	 * "cpu" or by the presence of
94 	 * <sysfs>/bus/event_source/devices/<name>/cpus. There may be >1 core
95 	 * PMU on systems like Intel hybrid.
96 	 */
97 	bool is_core;
98 	/**
99 	 * @is_uncore: Is the PMU not within the CPU core? Determined by the
100 	 * presence of <sysfs>/bus/event_source/devices/<name>/cpumask.
101 	 */
102 	bool is_uncore;
103 	/**
104 	 * @auxtrace: Are events auxiliary events? Determined in architecture
105 	 * specific code.
106 	 */
107 	bool auxtrace;
108 	/**
109 	 * @formats_checked: Only check PMU's formats are valid for
110 	 * perf_event_attr once.
111 	 */
112 	bool formats_checked;
113 	/** @config_masks_present: Are there config format values? */
114 	bool config_masks_present;
115 	/** @config_masks_computed: Set when masks are lazily computed. */
116 	bool config_masks_computed;
117 	/**
118 	 * @max_precise: Number of levels of :ppp precision supported by the
119 	 * PMU, read from
120 	 * <sysfs>/bus/event_source/devices/<name>/caps/max_precise.
121 	 */
122 	int max_precise;
123 	/**
124 	 * @perf_event_attr_init_default: Optional function to default
125 	 * initialize PMU specific parts of the perf_event_attr.
126 	 */
127 	void (*perf_event_attr_init_default)(const struct perf_pmu *pmu,
128 					     struct perf_event_attr *attr);
129 	/**
130 	 * @cpus: Empty or the contents of either of:
131 	 * <sysfs>/bus/event_source/devices/<name>/cpumask.
132 	 * <sysfs>/bus/event_source/devices/<cpu>/cpus.
133 	 */
134 	struct perf_cpu_map *cpus;
135 	/**
136 	 * @format: Holds the contents of files read from
137 	 * <sysfs>/bus/event_source/devices/<name>/format/. The contents specify
138 	 * which event parameter changes what config, config1 or config2 bits.
139 	 */
140 	struct list_head format;
141 	/**
142 	 * @aliases: List of struct perf_pmu_alias. Each alias corresponds to an
143 	 * event read from <sysfs>/bus/event_source/devices/<name>/events/ or
144 	 * from json events in pmu-events.c.
145 	 */
146 	struct hashmap *aliases;
147 	/**
148 	 * @events_table: The events table for json events in pmu-events.c.
149 	 */
150 	const struct pmu_events_table *events_table;
151 	/** @sysfs_aliases: Number of sysfs aliases loaded. */
152 	uint32_t sysfs_aliases;
153 	/** @cpu_json_aliases: Number of json event aliases loaded specific to the CPUID. */
154 	uint32_t cpu_json_aliases;
155 	/** @sys_json_aliases: Number of json event aliases loaded matching the PMU's identifier. */
156 	uint32_t sys_json_aliases;
157 	/**
158 	 * @cpu_common_json_aliases: Number of json events that overlapped with sysfs when
159 	 * loading all sysfs events.
160 	 */
161 	uint32_t cpu_common_json_aliases;
162 	/** @sysfs_aliases_loaded: Are sysfs aliases loaded from disk? */
163 	bool sysfs_aliases_loaded;
164 	/**
165 	 * @cpu_aliases_added: Have all json events table entries for the PMU
166 	 * been added?
167 	 */
168 	bool cpu_aliases_added;
169 	/** @caps_initialized: Has the list caps been initialized? */
170 	bool caps_initialized;
171 	/** @nr_caps: The length of the list caps. */
172 	u32 nr_caps;
173 	/**
174 	 * @caps: Holds the contents of files read from
175 	 * <sysfs>/bus/event_source/devices/<name>/caps/.
176 	 *
177 	 * The contents are pairs of the filename with the value of its
178 	 * contents, for example, max_precise (see above) may have a value of 3.
179 	 */
180 	struct list_head caps;
181 	/** @list: Element on pmus list in pmu.c. */
182 	struct list_head list;
183 
184 	/**
185 	 * @config_masks: Derived from the PMU's format data, bits that are
186 	 * valid within the config value.
187 	 */
188 	__u64 config_masks[PERF_PMU_FORMAT_VALUE_CONFIG_END];
189 
190 	/**
191 	 * @missing_features: Features to inhibit when events on this PMU are
192 	 * opened.
193 	 */
194 	struct {
195 		/**
196 		 * @exclude_guest: Disables perf_event_attr exclude_guest and
197 		 * exclude_host.
198 		 */
199 		bool exclude_guest;
200 		/**
201 		 * @checked: Are the missing features checked?
202 		 */
203 		bool checked;
204 	} missing_features;
205 
206 	/**
207 	 * @mem_events: List of the supported mem events
208 	 */
209 	struct perf_mem_event *mem_events;
210 };
211 
212 struct perf_pmu_info {
213 	const char *unit;
214 	double scale;
215 	double retirement_latency_mean;
216 	double retirement_latency_min;
217 	double retirement_latency_max;
218 	bool per_pkg;
219 	bool snapshot;
220 };
221 
222 struct pmu_event_info {
223 	const struct perf_pmu *pmu;
224 	const char *name;
225 	const char* alias;
226 	const char *scale_unit;
227 	const char *desc;
228 	const char *long_desc;
229 	const char *encoding_desc;
230 	const char *topic;
231 	const char *pmu_name;
232 	const char *event_type_desc;
233 	const char *str;
234 	bool deprecated;
235 };
236 
237 /**
238  * struct perf_pmu_format - Values from a format file read from
239  * <sysfs>/devices/cpu/format/ held in struct perf_pmu.
240  *
241  * For example, the contents of <sysfs>/devices/cpu/format/event may be
242  * "config:0-7" and will be represented here as name="event",
243  * value=PERF_PMU_FORMAT_VALUE_CONFIG and bits 0 to 7 will be set.
244  */
245 struct perf_pmu_format {
246 	/** @list: Element on list within struct perf_pmu. */
247 	struct list_head list;
248 	/** @bits: Which config bits are set by this format value. */
249 	DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
250 	/** @name: The modifier/file name. */
251 	char *name;
252 	/**
253 	 * @value : Which config value the format relates to. Supported values
254 	 * are from PERF_PMU_FORMAT_VALUE_CONFIG to
255 	 * PERF_PMU_FORMAT_VALUE_CONFIG_END.
256 	 */
257 	u16 value;
258 	/** @loaded: Has the contents been loaded/parsed. */
259 	bool loaded;
260 };
261 
262 typedef int (*pmu_event_callback)(void *state, struct pmu_event_info *info);
263 typedef int (*pmu_format_callback)(void *state, const char *name, int config,
264 				   const unsigned long *bits);
265 
266 void pmu_add_sys_aliases(struct perf_pmu *pmu);
267 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
268 		     struct parse_events_terms *head_terms,
269 		     bool apply_hardcoded,
270 		     struct parse_events_error *error);
271 int perf_pmu__config_terms(const struct perf_pmu *pmu,
272 			   struct perf_event_attr *attr,
273 			   struct parse_events_terms *terms,
274 			   bool zero, bool apply_hardcoded,
275 			   struct parse_events_error *error);
276 __u64 perf_pmu__format_bits(const struct perf_pmu *pmu, const char *name);
277 int perf_pmu__format_type(const struct perf_pmu *pmu, const char *name);
278 int perf_pmu__check_alias(struct perf_pmu *pmu, struct parse_events_terms *head_terms,
279 			  struct perf_pmu_info *info, bool *rewrote_terms,
280 			  u64 *alternate_hw_config, struct parse_events_error *err);
281 int perf_pmu__find_event(struct perf_pmu *pmu, const char *event, void *state, pmu_event_callback cb);
282 
283 void perf_pmu__format_pack(unsigned long *format, __u64 value, __u64 *v,
284 			   bool zero);
285 struct perf_pmu_format *pmu_find_format(const struct list_head *formats,
286 					const char *name);
287 void perf_pmu_format__set_value(void *format, int config, unsigned long *bits);
288 bool perf_pmu__has_format(const struct perf_pmu *pmu, const char *name);
289 int perf_pmu__for_each_format(struct perf_pmu *pmu, void *state, pmu_format_callback cb);
290 u64 perf_pmu__format_unpack(unsigned long *format, u64 config_val);
291 
292 bool is_pmu_core(const char *name);
293 bool is_pmu_core_len(struct perf_env *env, const char *name, size_t len);
294 
295 bool perf_pmu__supports_legacy_cache(const struct perf_pmu *pmu);
296 bool perf_pmu__auto_merge_stats(const struct perf_pmu *pmu);
297 bool perf_pmu__have_event(struct perf_pmu *pmu, const char *name);
298 size_t perf_pmu__num_events(struct perf_pmu *pmu);
299 int perf_pmu__for_each_event(struct perf_pmu *pmu, bool skip_duplicate_pmus,
300 			     void *state, pmu_event_callback cb);
301 bool perf_pmu__name_wildcard_match(const struct perf_pmu *pmu, const char *to_match);
302 bool perf_pmu__name_no_suffix_match(const struct perf_pmu *pmu, const char *to_match);
303 
304 /**
305  * perf_pmu_is_software - is the PMU a software PMU as in it uses the
306  *                        perf_sw_context in the kernel?
307  */
308 bool perf_pmu__is_software(const struct perf_pmu *pmu);
309 bool perf_pmu__benefits_from_affinity(struct perf_pmu *pmu);
310 
311 FILE *perf_pmu__open_file(const struct perf_pmu *pmu, const char *name);
312 FILE *perf_pmu__open_file_at(const struct perf_pmu *pmu, int dirfd, const char *name);
313 
314 int perf_pmu__scan_file(const struct perf_pmu *pmu, const char *name, const char *fmt, ...)
315 	__scanf(3, 4);
316 int perf_pmu__scan_file_at(const struct perf_pmu *pmu, int dirfd, const char *name,
317 			   const char *fmt, ...) __scanf(4, 5);
318 
319 bool perf_pmu__file_exists(const struct perf_pmu *pmu, const char *name);
320 
321 int perf_pmu__test(void);
322 
323 void perf_pmu__arch_init(struct perf_pmu *pmu);
324 void pmu_add_cpu_aliases_table(struct perf_pmu *pmu,
325 			       const struct pmu_events_table *table);
326 
327 bool pmu_uncore_identifier_match(const char *compat, const char *id);
328 
329 int perf_pmu__convert_scale(const char *scale, char **end, double *sval);
330 
331 struct perf_pmu_caps *perf_pmu__get_cap(struct perf_pmu *pmu, const char *name);
332 
333 int perf_pmu__caps_parse(struct perf_pmu *pmu);
334 
335 void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config,
336 				   const char *name, int config_num,
337 				   const char *config_name);
338 void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu);
339 
340 bool perf_pmu__wildcard_match(const struct perf_pmu *pmu, const char *wildcard_to_match);
341 
342 int perf_pmu__event_source_devices_scnprintf(char *pathname, size_t size);
343 int perf_pmu__pathname_scnprintf(char *buf, size_t size,
344 				 const char *pmu_name, const char *filename);
345 int perf_pmu__event_source_devices_fd(void);
346 int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *filename, int flags);
347 
348 int perf_pmu__init(struct perf_pmu *pmu, __u32 type, const char *name);
349 struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char *lookup_name,
350 				  bool eager_load);
351 struct perf_pmu *perf_pmu__create_placeholder_core_pmu(struct list_head *core_pmus);
352 void perf_pmu__delete(struct perf_pmu *pmu);
353 
354 const char *perf_pmu__name_from_config(struct perf_pmu *pmu, u64 config);
355 bool perf_pmu__is_fake(const struct perf_pmu *pmu);
356 
357 bool perf_pmu__reads_only_on_cpu_idx0(const struct perf_event_attr *attr);
358 
359 static inline enum pmu_kind perf_pmu__kind(const struct perf_pmu *pmu)
360 {
361 	__u32 type;
362 
363 	if (!pmu)
364 		return PERF_PMU_KIND_PE;
365 
366 	type = pmu->type;
367 	if (type <= PERF_PMU_TYPE_PE_END)
368 		return PERF_PMU_KIND_PE;
369 	if (type <= PERF_PMU_TYPE_DRM_END)
370 		return PERF_PMU_KIND_DRM;
371 	if (type <= PERF_PMU_TYPE_HWMON_END)
372 		return PERF_PMU_KIND_HWMON;
373 	if (type == PERF_PMU_TYPE_TOOL)
374 		return PERF_PMU_KIND_TOOL;
375 	return PERF_PMU_KIND_FAKE;
376 }
377 
378 #endif /* __PMU_H */
379