xref: /linux/tools/perf/util/pmu.h (revision 1504b6f97bad166b484d6f27dc99746fdca5f467)
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 "parse-events.h"
11 #include "pmu-events/pmu-events.h"
12 
13 struct evsel_config_term;
14 struct perf_cpu_map;
15 
16 enum {
17 	PERF_PMU_FORMAT_VALUE_CONFIG,
18 	PERF_PMU_FORMAT_VALUE_CONFIG1,
19 	PERF_PMU_FORMAT_VALUE_CONFIG2,
20 	PERF_PMU_FORMAT_VALUE_CONFIG_END,
21 };
22 
23 #define PERF_PMU_FORMAT_BITS 64
24 #define EVENT_SOURCE_DEVICE_PATH "/bus/event_source/devices/"
25 #define CPUS_TEMPLATE_CPU	"%s/bus/event_source/devices/%s/cpus"
26 #define MAX_PMU_NAME_LEN 128
27 
28 struct perf_event_attr;
29 
30 struct perf_pmu_caps {
31 	char *name;
32 	char *value;
33 	struct list_head list;
34 };
35 
36 /**
37  * struct perf_pmu - hi
38  */
39 struct perf_pmu {
40 	/** @name: The name of the PMU such as "cpu". */
41 	char *name;
42 	/**
43 	 * @alias_name: Optional alternate name for the PMU determined in
44 	 * architecture specific code.
45 	 */
46 	char *alias_name;
47 	/**
48 	 * @id: Optional PMU identifier read from
49 	 * <sysfs>/bus/event_source/devices/<name>/identifier.
50 	 */
51 	char *id;
52 	/**
53 	 * @type: Perf event attributed type value, read from
54 	 * <sysfs>/bus/event_source/devices/<name>/type.
55 	 */
56 	__u32 type;
57 	/**
58 	 * @selectable: Can the PMU name be selected as if it were an event?
59 	 */
60 	bool selectable;
61 	/**
62 	 * @is_uncore: Is the PMU not within the CPU core? Determined by the
63 	 * presence of <sysfs>/bus/event_source/devices/<name>/cpumask.
64 	 */
65 	bool is_uncore;
66 	/**
67 	 * @auxtrace: Are events auxiliary events? Determined in architecture
68 	 * specific code.
69 	 */
70 	bool auxtrace;
71 	/**
72 	 * @max_precise: Number of levels of :ppp precision supported by the
73 	 * PMU, read from
74 	 * <sysfs>/bus/event_source/devices/<name>/caps/max_precise.
75 	 */
76 	int max_precise;
77 	/**
78 	 * @default_config: Optional default perf_event_attr determined in
79 	 * architecture specific code.
80 	 */
81 	struct perf_event_attr *default_config;
82 	/**
83 	 * @cpus: Empty or the contents of either of:
84 	 * <sysfs>/bus/event_source/devices/<name>/cpumask.
85 	 * <sysfs>/bus/event_source/devices/<cpu>/cpus.
86 	 */
87 	struct perf_cpu_map *cpus;
88 	/**
89 	 * @format: Holds the contents of files read from
90 	 * <sysfs>/bus/event_source/devices/<name>/format/. The contents specify
91 	 * which event parameter changes what config, config1 or config2 bits.
92 	 */
93 	struct list_head format;
94 	/**
95 	 * @aliases: List of struct perf_pmu_alias. Each alias corresponds to an
96 	 * event read from <sysfs>/bus/event_source/devices/<name>/events/ or
97 	 * from json events in pmu-events.c.
98 	 */
99 	struct list_head aliases;
100 	/** @caps_initialized: Has the list caps been initialized? */
101 	bool caps_initialized;
102 	/** @nr_caps: The length of the list caps. */
103 	u32 nr_caps;
104 	/**
105 	 * @caps: Holds the contents of files read from
106 	 * <sysfs>/bus/event_source/devices/<name>/caps/.
107 	 *
108 	 * The contents are pairs of the filename with the value of its
109 	 * contents, for example, max_precise (see above) may have a value of 3.
110 	 */
111 	struct list_head caps;
112 	/** @list: Element on pmus list in pmu.c. */
113 	struct list_head list;
114 	/** @hybrid_list: Element on perf_pmu__hybrid_pmus. */
115 	struct list_head hybrid_list;
116 
117 	/**
118 	 * @missing_features: Features to inhibit when events on this PMU are
119 	 * opened.
120 	 */
121 	struct {
122 		/**
123 		 * @exclude_guest: Disables perf_event_attr exclude_guest and
124 		 * exclude_host.
125 		 */
126 		bool exclude_guest;
127 	} missing_features;
128 };
129 
130 /** @perf_pmu__fake: A special global PMU used for testing. */
131 extern struct perf_pmu perf_pmu__fake;
132 
133 struct perf_pmu_info {
134 	const char *unit;
135 	const char *metric_expr;
136 	const char *metric_name;
137 	double scale;
138 	bool per_pkg;
139 	bool snapshot;
140 };
141 
142 #define UNIT_MAX_LEN	31 /* max length for event unit name */
143 
144 /**
145  * struct perf_pmu_alias - An event either read from sysfs or builtin in
146  * pmu-events.c, created by parsing the pmu-events json files.
147  */
148 struct perf_pmu_alias {
149 	/** @name: Name of the event like "mem-loads". */
150 	char *name;
151 	/** @desc: Optional short description of the event. */
152 	char *desc;
153 	/** @long_desc: Optional long description. */
154 	char *long_desc;
155 	/**
156 	 * @topic: Optional topic such as cache or pipeline, particularly for
157 	 * json events.
158 	 */
159 	char *topic;
160 	/**
161 	 * @str: Comma separated parameter list like
162 	 * "event=0xcd,umask=0x1,ldlat=0x3".
163 	 */
164 	char *str;
165 	/** @terms: Owned list of the original parsed parameters. */
166 	struct list_head terms;
167 	/** @list: List element of struct perf_pmu aliases. */
168 	struct list_head list;
169 	/** @unit: Units for the event, such as bytes or cache lines. */
170 	char unit[UNIT_MAX_LEN+1];
171 	/** @scale: Value to scale read counter values by. */
172 	double scale;
173 	/**
174 	 * @per_pkg: Does the file
175 	 * <sysfs>/bus/event_source/devices/<pmu_name>/events/<name>.per-pkg or
176 	 * equivalent json value exist and have the value 1.
177 	 */
178 	bool per_pkg;
179 	/**
180 	 * @snapshot: Does the file
181 	 * <sysfs>/bus/event_source/devices/<pmu_name>/events/<name>.snapshot
182 	 * exist and have the value 1.
183 	 */
184 	bool snapshot;
185 	/**
186 	 * @deprecated: Is the event hidden and so not shown in perf list by
187 	 * default.
188 	 */
189 	bool deprecated;
190 	/**
191 	 * @metric_expr: A metric expression associated with an event. Doing
192 	 * this makes little sense due to scale and unit applying to both.
193 	 */
194 	char *metric_expr;
195 	/** @metric_name: A name for the metric. unit applying to both. */
196 	char *metric_name;
197 	/** @pmu_name: The name copied from struct perf_pmu. */
198 	char *pmu_name;
199 };
200 
201 struct perf_pmu *perf_pmu__find(const char *name);
202 struct perf_pmu *perf_pmu__find_by_type(unsigned int type);
203 void pmu_add_sys_aliases(struct list_head *head, struct perf_pmu *pmu);
204 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
205 		     struct list_head *head_terms,
206 		     struct parse_events_error *error);
207 int perf_pmu__config_terms(const char *pmu_name, struct list_head *formats,
208 			   struct perf_event_attr *attr,
209 			   struct list_head *head_terms,
210 			   bool zero, struct parse_events_error *error);
211 __u64 perf_pmu__format_bits(struct list_head *formats, const char *name);
212 int perf_pmu__format_type(struct list_head *formats, const char *name);
213 int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
214 			  struct perf_pmu_info *info);
215 struct list_head *perf_pmu__alias(struct perf_pmu *pmu,
216 				  struct list_head *head_terms);
217 void perf_pmu_error(struct list_head *list, char *name, char const *msg);
218 
219 int perf_pmu__new_format(struct list_head *list, char *name,
220 			 int config, unsigned long *bits);
221 void perf_pmu__set_format(unsigned long *bits, long from, long to);
222 int perf_pmu__format_parse(char *dir, struct list_head *head);
223 void perf_pmu__del_formats(struct list_head *formats);
224 
225 struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu);
226 
227 bool is_pmu_core(const char *name);
228 void print_pmu_events(const char *event_glob, bool name_only, bool quiet,
229 		      bool long_desc, bool details_flag,
230 		      bool deprecated, const char *pmu_name);
231 bool pmu_have_event(const char *pname, const char *name);
232 
233 int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt, ...) __scanf(3, 4);
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 bool pmu_uncore_alias_match(const char *pmu_name, const char *name);
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 #endif /* __PMU_H */
264