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