xref: /linux/tools/perf/util/pmu.h (revision a90e8608eb0ed93d31ac0feb055f77ce59512542)
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 
14 struct evsel_config_term;
15 struct perf_cpu_map;
16 struct print_callbacks;
17 
18 enum {
19 	PERF_PMU_FORMAT_VALUE_CONFIG,
20 	PERF_PMU_FORMAT_VALUE_CONFIG1,
21 	PERF_PMU_FORMAT_VALUE_CONFIG2,
22 	PERF_PMU_FORMAT_VALUE_CONFIG3,
23 	PERF_PMU_FORMAT_VALUE_CONFIG_END,
24 };
25 
26 #define PERF_PMU_FORMAT_BITS 64
27 #define MAX_PMU_NAME_LEN 128
28 
29 struct perf_event_attr;
30 
31 struct perf_pmu_caps {
32 	char *name;
33 	char *value;
34 	struct list_head list;
35 };
36 
37 /**
38  * struct perf_pmu
39  */
40 struct perf_pmu {
41 	/** @name: The name of the PMU such as "cpu". */
42 	char *name;
43 	/**
44 	 * @alias_name: Optional alternate name for the PMU determined in
45 	 * architecture specific code.
46 	 */
47 	char *alias_name;
48 	/**
49 	 * @id: Optional PMU identifier read from
50 	 * <sysfs>/bus/event_source/devices/<name>/identifier.
51 	 */
52 	char *id;
53 	/**
54 	 * @type: Perf event attributed type value, read from
55 	 * <sysfs>/bus/event_source/devices/<name>/type.
56 	 */
57 	__u32 type;
58 	/**
59 	 * @selectable: Can the PMU name be selected as if it were an event?
60 	 */
61 	bool selectable;
62 	/**
63 	 * @is_uncore: Is the PMU not within the CPU core? Determined by the
64 	 * presence of <sysfs>/bus/event_source/devices/<name>/cpumask.
65 	 */
66 	bool is_uncore;
67 	/**
68 	 * @auxtrace: Are events auxiliary events? Determined in architecture
69 	 * specific code.
70 	 */
71 	bool auxtrace;
72 	/**
73 	 * @max_precise: Number of levels of :ppp precision supported by the
74 	 * PMU, read from
75 	 * <sysfs>/bus/event_source/devices/<name>/caps/max_precise.
76 	 */
77 	int max_precise;
78 	/**
79 	 * @default_config: Optional default perf_event_attr determined in
80 	 * architecture specific code.
81 	 */
82 	struct perf_event_attr *default_config;
83 	/**
84 	 * @cpus: Empty or the contents of either of:
85 	 * <sysfs>/bus/event_source/devices/<name>/cpumask.
86 	 * <sysfs>/bus/event_source/devices/<cpu>/cpus.
87 	 */
88 	struct perf_cpu_map *cpus;
89 	/**
90 	 * @format: Holds the contents of files read from
91 	 * <sysfs>/bus/event_source/devices/<name>/format/. The contents specify
92 	 * which event parameter changes what config, config1 or config2 bits.
93 	 */
94 	struct list_head format;
95 	/**
96 	 * @aliases: List of struct perf_pmu_alias. Each alias corresponds to an
97 	 * event read from <sysfs>/bus/event_source/devices/<name>/events/ or
98 	 * from json events in pmu-events.c.
99 	 */
100 	struct list_head aliases;
101 	/** @caps_initialized: Has the list caps been initialized? */
102 	bool caps_initialized;
103 	/** @nr_caps: The length of the list caps. */
104 	u32 nr_caps;
105 	/**
106 	 * @caps: Holds the contents of files read from
107 	 * <sysfs>/bus/event_source/devices/<name>/caps/.
108 	 *
109 	 * The contents are pairs of the filename with the value of its
110 	 * contents, for example, max_precise (see above) may have a value of 3.
111 	 */
112 	struct list_head caps;
113 	/** @list: Element on pmus list in pmu.c. */
114 	struct list_head list;
115 	/** @hybrid_list: Element on perf_pmu__hybrid_pmus. */
116 	struct list_head hybrid_list;
117 
118 	/**
119 	 * @missing_features: Features to inhibit when events on this PMU are
120 	 * opened.
121 	 */
122 	struct {
123 		/**
124 		 * @exclude_guest: Disables perf_event_attr exclude_guest and
125 		 * exclude_host.
126 		 */
127 		bool exclude_guest;
128 	} missing_features;
129 };
130 
131 /** @perf_pmu__fake: A special global PMU used for testing. */
132 extern struct perf_pmu perf_pmu__fake;
133 
134 struct perf_pmu_info {
135 	const char *unit;
136 	double scale;
137 	bool per_pkg;
138 	bool snapshot;
139 };
140 
141 #define UNIT_MAX_LEN	31 /* max length for event unit name */
142 
143 /**
144  * struct perf_pmu_alias - An event either read from sysfs or builtin in
145  * pmu-events.c, created by parsing the pmu-events json files.
146  */
147 struct perf_pmu_alias {
148 	/** @name: Name of the event like "mem-loads". */
149 	char *name;
150 	/** @desc: Optional short description of the event. */
151 	char *desc;
152 	/** @long_desc: Optional long description. */
153 	char *long_desc;
154 	/**
155 	 * @topic: Optional topic such as cache or pipeline, particularly for
156 	 * json events.
157 	 */
158 	char *topic;
159 	/**
160 	 * @str: Comma separated parameter list like
161 	 * "event=0xcd,umask=0x1,ldlat=0x3".
162 	 */
163 	char *str;
164 	/** @terms: Owned list of the original parsed parameters. */
165 	struct list_head terms;
166 	/** @list: List element of struct perf_pmu aliases. */
167 	struct list_head list;
168 	/** @unit: Units for the event, such as bytes or cache lines. */
169 	char unit[UNIT_MAX_LEN+1];
170 	/** @scale: Value to scale read counter values by. */
171 	double scale;
172 	/**
173 	 * @per_pkg: Does the file
174 	 * <sysfs>/bus/event_source/devices/<pmu_name>/events/<name>.per-pkg or
175 	 * equivalent json value exist and have the value 1.
176 	 */
177 	bool per_pkg;
178 	/**
179 	 * @snapshot: Does the file
180 	 * <sysfs>/bus/event_source/devices/<pmu_name>/events/<name>.snapshot
181 	 * exist and have the value 1.
182 	 */
183 	bool snapshot;
184 	/**
185 	 * @deprecated: Is the event hidden and so not shown in perf list by
186 	 * default.
187 	 */
188 	bool deprecated;
189 	/**
190 	 * @pmu_name: The name copied from the json struct pmu_event. This can
191 	 * differ from the PMU name as it won't have suffixes.
192 	 */
193 	char *pmu_name;
194 };
195 
196 struct perf_pmu *perf_pmu__find(const char *name);
197 struct perf_pmu *perf_pmu__find_by_type(unsigned int type);
198 void pmu_add_sys_aliases(struct list_head *head, struct perf_pmu *pmu);
199 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
200 		     struct list_head *head_terms,
201 		     struct parse_events_error *error);
202 int perf_pmu__config_terms(const char *pmu_name, struct list_head *formats,
203 			   struct perf_event_attr *attr,
204 			   struct list_head *head_terms,
205 			   bool zero, struct parse_events_error *error);
206 __u64 perf_pmu__format_bits(struct list_head *formats, const char *name);
207 int perf_pmu__format_type(struct list_head *formats, const char *name);
208 int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
209 			  struct perf_pmu_info *info);
210 struct list_head *perf_pmu__alias(struct perf_pmu *pmu,
211 				  struct list_head *head_terms);
212 void perf_pmu_error(struct list_head *list, char *name, void *scanner, char const *msg);
213 
214 int perf_pmu__new_format(struct list_head *list, char *name,
215 			 int config, unsigned long *bits);
216 void perf_pmu__set_format(unsigned long *bits, long from, long to);
217 int perf_pmu__format_parse(int dirfd, struct list_head *head);
218 void perf_pmu__del_formats(struct list_head *formats);
219 
220 struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu);
221 
222 bool is_pmu_core(const char *name);
223 void print_pmu_events(const struct print_callbacks *print_cb, void *print_state);
224 bool pmu_have_event(const char *pname, const char *name);
225 
226 FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name);
227 FILE *perf_pmu__open_file_at(struct perf_pmu *pmu, int dirfd, const char *name);
228 
229 int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt, ...) __scanf(3, 4);
230 int perf_pmu__scan_file_at(struct perf_pmu *pmu, int dirfd, const char *name,
231 			   const char *fmt, ...) __scanf(4, 5);
232 
233 bool perf_pmu__file_exists(struct perf_pmu *pmu, const char *name);
234 
235 int perf_pmu__test(void);
236 
237 struct perf_event_attr *perf_pmu__get_default_config(struct perf_pmu *pmu);
238 void pmu_add_cpu_aliases_table(struct list_head *head, struct perf_pmu *pmu,
239 			       const struct pmu_events_table *table);
240 
241 char *perf_pmu__getcpuid(struct perf_pmu *pmu);
242 const struct pmu_events_table *pmu_events_table__find(void);
243 const struct pmu_metrics_table *pmu_metrics_table__find(void);
244 void perf_pmu_free_alias(struct perf_pmu_alias *alias);
245 
246 int perf_pmu__convert_scale(const char *scale, char **end, double *sval);
247 
248 int perf_pmu__caps_parse(struct perf_pmu *pmu);
249 
250 void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config,
251 				   const char *name);
252 void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu);
253 
254 bool perf_pmu__has_hybrid(void);
255 int perf_pmu__match(char *pattern, char *name, char *tok);
256 
257 int perf_pmu__cpus_match(struct perf_pmu *pmu, struct perf_cpu_map *cpus,
258 			 struct perf_cpu_map **mcpus_ptr,
259 			 struct perf_cpu_map **ucpus_ptr);
260 
261 char *pmu_find_real_name(const char *name);
262 char *pmu_find_alias_name(const char *name);
263 double perf_pmu__cpu_slots_per_cycle(void);
264 int perf_pmu__event_source_devices_scnprintf(char *pathname, size_t size);
265 int perf_pmu__pathname_scnprintf(char *buf, size_t size,
266 				 const char *pmu_name, const char *filename);
267 int perf_pmu__event_source_devices_fd(void);
268 int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *filename, int flags);
269 
270 void perf_pmu__destroy(void);
271 
272 #endif /* __PMU_H */
273