xref: /openbmc/linux/tools/perf/util/evsel.h (revision 31e67366)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __PERF_EVSEL_H
3 #define __PERF_EVSEL_H 1
4 
5 #include <linux/list.h>
6 #include <stdbool.h>
7 #include <sys/types.h>
8 #include <linux/perf_event.h>
9 #include <linux/types.h>
10 #include <internal/evsel.h>
11 #include <perf/evsel.h>
12 #include "symbol_conf.h"
13 #include <internal/cpumap.h>
14 
15 struct bpf_object;
16 struct cgroup;
17 struct perf_counts;
18 struct perf_stat_evsel;
19 union perf_event;
20 struct bpf_counter_ops;
21 struct target;
22 
23 typedef int (evsel__sb_cb_t)(union perf_event *event, void *data);
24 
25 enum perf_tool_event {
26 	PERF_TOOL_NONE		= 0,
27 	PERF_TOOL_DURATION_TIME = 1,
28 };
29 
30 /** struct evsel - event selector
31  *
32  * @evlist - evlist this evsel is in, if it is in one.
33  * @core - libperf evsel object
34  * @name - Can be set to retain the original event name passed by the user,
35  *         so that when showing results in tools such as 'perf stat', we
36  *         show the name used, not some alias.
37  * @id_pos: the position of the event id (PERF_SAMPLE_ID or
38  *          PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of
39  *          struct perf_record_sample
40  * @is_pos: the position (counting backwards) of the event id (PERF_SAMPLE_ID or
41  *          PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if sample_id_all
42  *          is used there is an id sample appended to non-sample events
43  * @priv:   And what is in its containing unnamed union are tool specific
44  */
45 struct evsel {
46 	struct perf_evsel	core;
47 	struct evlist		*evlist;
48 	off_t			id_offset;
49 	int			idx;
50 	int			id_pos;
51 	int			is_pos;
52 	unsigned int		sample_size;
53 
54 	/*
55 	 * These fields can be set in the parse-events code or similar.
56 	 * Please check evsel__clone() to copy them properly so that
57 	 * they can be released properly.
58 	 */
59 	struct {
60 		char			*name;
61 		char			*group_name;
62 		const char		*pmu_name;
63 		struct tep_event	*tp_format;
64 		char			*filter;
65 		unsigned long		max_events;
66 		double			scale;
67 		const char		*unit;
68 		struct cgroup		*cgrp;
69 		enum perf_tool_event	tool_event;
70 		/* parse modifier helper */
71 		int			exclude_GH;
72 		int			sample_read;
73 		bool			snapshot;
74 		bool			per_pkg;
75 		bool			percore;
76 		bool			precise_max;
77 		bool			use_uncore_alias;
78 		bool			is_libpfm_event;
79 		bool			auto_merge_stats;
80 		bool			collect_stat;
81 		bool			weak_group;
82 		int			bpf_fd;
83 		struct bpf_object	*bpf_obj;
84 	};
85 
86 	/*
87 	 * metric fields are similar, but needs more care as they can have
88 	 * references to other metric (evsel).
89 	 */
90 	const char *		metric_expr;
91 	const char *		metric_name;
92 	struct evsel		**metric_events;
93 	struct evsel		*metric_leader;
94 
95 	void			*handler;
96 	struct perf_counts	*counts;
97 	struct perf_counts	*prev_raw_counts;
98 	unsigned long		nr_events_printed;
99 	struct perf_stat_evsel  *stats;
100 	void			*priv;
101 	u64			db_id;
102 	bool			uniquified_name;
103 	bool 			supported;
104 	bool 			needs_swap;
105 	bool 			disabled;
106 	bool			no_aux_samples;
107 	bool			immediate;
108 	bool			tracking;
109 	bool			ignore_missing_thread;
110 	bool			forced_leader;
111 	bool			cmdline_group_boundary;
112 	bool			merged_stat;
113 	bool			reset_group;
114 	bool			errored;
115 	unsigned long		*per_pkg_mask;
116 	struct evsel		*leader;
117 	struct list_head	config_terms;
118 	int			err;
119 	int			cpu_iter;
120 	struct {
121 		evsel__sb_cb_t	*cb;
122 		void		*data;
123 	} side_band;
124 	/*
125 	 * For reporting purposes, an evsel sample can have a callchain
126 	 * synthesized from AUX area data. Keep track of synthesized sample
127 	 * types here. Note, the recorded sample_type cannot be changed because
128 	 * it is needed to continue to parse events.
129 	 * See also evsel__has_callchain().
130 	 */
131 	__u64			synth_sample_type;
132 	struct list_head	bpf_counter_list;
133 	struct bpf_counter_ops	*bpf_counter_ops;
134 };
135 
136 struct perf_missing_features {
137 	bool sample_id_all;
138 	bool exclude_guest;
139 	bool mmap2;
140 	bool cloexec;
141 	bool clockid;
142 	bool clockid_wrong;
143 	bool lbr_flags;
144 	bool write_backward;
145 	bool group_read;
146 	bool ksymbol;
147 	bool bpf;
148 	bool aux_output;
149 	bool branch_hw_idx;
150 	bool cgroup;
151 	bool data_page_size;
152 	bool code_page_size;
153 	bool weight_struct;
154 };
155 
156 extern struct perf_missing_features perf_missing_features;
157 
158 struct perf_cpu_map;
159 struct target;
160 struct thread_map;
161 struct record_opts;
162 
163 static inline struct perf_cpu_map *evsel__cpus(struct evsel *evsel)
164 {
165 	return perf_evsel__cpus(&evsel->core);
166 }
167 
168 static inline int evsel__nr_cpus(struct evsel *evsel)
169 {
170 	return evsel__cpus(evsel)->nr;
171 }
172 
173 void perf_counts_values__scale(struct perf_counts_values *count,
174 			       bool scale, s8 *pscaled);
175 
176 void evsel__compute_deltas(struct evsel *evsel, int cpu, int thread,
177 			   struct perf_counts_values *count);
178 
179 int evsel__object_config(size_t object_size,
180 			 int (*init)(struct evsel *evsel),
181 			 void (*fini)(struct evsel *evsel));
182 
183 struct perf_pmu *evsel__find_pmu(struct evsel *evsel);
184 bool evsel__is_aux_event(struct evsel *evsel);
185 
186 struct evsel *evsel__new_idx(struct perf_event_attr *attr, int idx);
187 
188 static inline struct evsel *evsel__new(struct perf_event_attr *attr)
189 {
190 	return evsel__new_idx(attr, 0);
191 }
192 
193 struct evsel *evsel__clone(struct evsel *orig);
194 struct evsel *evsel__newtp_idx(const char *sys, const char *name, int idx);
195 
196 /*
197  * Returns pointer with encoded error via <linux/err.h> interface.
198  */
199 static inline struct evsel *evsel__newtp(const char *sys, const char *name)
200 {
201 	return evsel__newtp_idx(sys, name, 0);
202 }
203 
204 struct evsel *evsel__new_cycles(bool precise);
205 
206 struct tep_event *event_format__new(const char *sys, const char *name);
207 
208 void evsel__init(struct evsel *evsel, struct perf_event_attr *attr, int idx);
209 void evsel__exit(struct evsel *evsel);
210 void evsel__delete(struct evsel *evsel);
211 
212 struct callchain_param;
213 
214 void evsel__config(struct evsel *evsel, struct record_opts *opts,
215 		   struct callchain_param *callchain);
216 void evsel__config_callchain(struct evsel *evsel, struct record_opts *opts,
217 			     struct callchain_param *callchain);
218 
219 int __evsel__sample_size(u64 sample_type);
220 void evsel__calc_id_pos(struct evsel *evsel);
221 
222 bool evsel__is_cache_op_valid(u8 type, u8 op);
223 
224 #define EVSEL__MAX_ALIASES 8
225 
226 extern const char *evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX][EVSEL__MAX_ALIASES];
227 extern const char *evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX][EVSEL__MAX_ALIASES];
228 extern const char *evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX][EVSEL__MAX_ALIASES];
229 extern const char *evsel__hw_names[PERF_COUNT_HW_MAX];
230 extern const char *evsel__sw_names[PERF_COUNT_SW_MAX];
231 int __evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result, char *bf, size_t size);
232 const char *evsel__name(struct evsel *evsel);
233 
234 const char *evsel__group_name(struct evsel *evsel);
235 int evsel__group_desc(struct evsel *evsel, char *buf, size_t size);
236 
237 void __evsel__set_sample_bit(struct evsel *evsel, enum perf_event_sample_format bit);
238 void __evsel__reset_sample_bit(struct evsel *evsel, enum perf_event_sample_format bit);
239 
240 #define evsel__set_sample_bit(evsel, bit) \
241 	__evsel__set_sample_bit(evsel, PERF_SAMPLE_##bit)
242 
243 #define evsel__reset_sample_bit(evsel, bit) \
244 	__evsel__reset_sample_bit(evsel, PERF_SAMPLE_##bit)
245 
246 void evsel__set_sample_id(struct evsel *evsel, bool use_sample_identifier);
247 
248 void arch_evsel__set_sample_weight(struct evsel *evsel);
249 
250 int evsel__set_filter(struct evsel *evsel, const char *filter);
251 int evsel__append_tp_filter(struct evsel *evsel, const char *filter);
252 int evsel__append_addr_filter(struct evsel *evsel, const char *filter);
253 int evsel__enable_cpu(struct evsel *evsel, int cpu);
254 int evsel__enable(struct evsel *evsel);
255 int evsel__disable(struct evsel *evsel);
256 int evsel__disable_cpu(struct evsel *evsel, int cpu);
257 
258 int evsel__open_per_cpu(struct evsel *evsel, struct perf_cpu_map *cpus, int cpu);
259 int evsel__open_per_thread(struct evsel *evsel, struct perf_thread_map *threads);
260 int evsel__open(struct evsel *evsel, struct perf_cpu_map *cpus,
261 		struct perf_thread_map *threads);
262 void evsel__close(struct evsel *evsel);
263 
264 struct perf_sample;
265 
266 void *evsel__rawptr(struct evsel *evsel, struct perf_sample *sample, const char *name);
267 u64 evsel__intval(struct evsel *evsel, struct perf_sample *sample, const char *name);
268 
269 static inline char *evsel__strval(struct evsel *evsel, struct perf_sample *sample, const char *name)
270 {
271 	return evsel__rawptr(evsel, sample, name);
272 }
273 
274 struct tep_format_field;
275 
276 u64 format_field__intval(struct tep_format_field *field, struct perf_sample *sample, bool needs_swap);
277 
278 struct tep_format_field *evsel__field(struct evsel *evsel, const char *name);
279 
280 #define evsel__match(evsel, t, c)		\
281 	(evsel->core.attr.type == PERF_TYPE_##t &&	\
282 	 evsel->core.attr.config == PERF_COUNT_##c)
283 
284 static inline bool evsel__match2(struct evsel *e1, struct evsel *e2)
285 {
286 	return (e1->core.attr.type == e2->core.attr.type) &&
287 	       (e1->core.attr.config == e2->core.attr.config);
288 }
289 
290 int evsel__read_counter(struct evsel *evsel, int cpu, int thread);
291 
292 int __evsel__read_on_cpu(struct evsel *evsel, int cpu, int thread, bool scale);
293 
294 /**
295  * evsel__read_on_cpu - Read out the results on a CPU and thread
296  *
297  * @evsel - event selector to read value
298  * @cpu - CPU of interest
299  * @thread - thread of interest
300  */
301 static inline int evsel__read_on_cpu(struct evsel *evsel, int cpu, int thread)
302 {
303 	return __evsel__read_on_cpu(evsel, cpu, thread, false);
304 }
305 
306 /**
307  * evsel__read_on_cpu_scaled - Read out the results on a CPU and thread, scaled
308  *
309  * @evsel - event selector to read value
310  * @cpu - CPU of interest
311  * @thread - thread of interest
312  */
313 static inline int evsel__read_on_cpu_scaled(struct evsel *evsel, int cpu, int thread)
314 {
315 	return __evsel__read_on_cpu(evsel, cpu, thread, true);
316 }
317 
318 int evsel__parse_sample(struct evsel *evsel, union perf_event *event,
319 			struct perf_sample *sample);
320 
321 int evsel__parse_sample_timestamp(struct evsel *evsel, union perf_event *event,
322 				  u64 *timestamp);
323 
324 static inline struct evsel *evsel__next(struct evsel *evsel)
325 {
326 	return list_entry(evsel->core.node.next, struct evsel, core.node);
327 }
328 
329 static inline struct evsel *evsel__prev(struct evsel *evsel)
330 {
331 	return list_entry(evsel->core.node.prev, struct evsel, core.node);
332 }
333 
334 /**
335  * evsel__is_group_leader - Return whether given evsel is a leader event
336  *
337  * @evsel - evsel selector to be tested
338  *
339  * Return %true if @evsel is a group leader or a stand-alone event
340  */
341 static inline bool evsel__is_group_leader(const struct evsel *evsel)
342 {
343 	return evsel->leader == evsel;
344 }
345 
346 /**
347  * evsel__is_group_event - Return whether given evsel is a group event
348  *
349  * @evsel - evsel selector to be tested
350  *
351  * Return %true iff event group view is enabled and @evsel is a actual group
352  * leader which has other members in the group
353  */
354 static inline bool evsel__is_group_event(struct evsel *evsel)
355 {
356 	if (!symbol_conf.event_group)
357 		return false;
358 
359 	return evsel__is_group_leader(evsel) && evsel->core.nr_members > 1;
360 }
361 
362 bool evsel__is_function_event(struct evsel *evsel);
363 
364 static inline bool evsel__is_bpf_output(struct evsel *evsel)
365 {
366 	return evsel__match(evsel, SOFTWARE, SW_BPF_OUTPUT);
367 }
368 
369 static inline bool evsel__is_clock(struct evsel *evsel)
370 {
371 	return evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
372 	       evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK);
373 }
374 
375 bool evsel__fallback(struct evsel *evsel, int err, char *msg, size_t msgsize);
376 int evsel__open_strerror(struct evsel *evsel, struct target *target,
377 			 int err, char *msg, size_t size);
378 
379 static inline int evsel__group_idx(struct evsel *evsel)
380 {
381 	return evsel->idx - evsel->leader->idx;
382 }
383 
384 /* Iterates group WITHOUT the leader. */
385 #define for_each_group_member(_evsel, _leader) 					\
386 for ((_evsel) = list_entry((_leader)->core.node.next, struct evsel, core.node); \
387      (_evsel) && (_evsel)->leader == (_leader);					\
388      (_evsel) = list_entry((_evsel)->core.node.next, struct evsel, core.node))
389 
390 /* Iterates group WITH the leader. */
391 #define for_each_group_evsel(_evsel, _leader) 					\
392 for ((_evsel) = _leader; 							\
393      (_evsel) && (_evsel)->leader == (_leader);					\
394      (_evsel) = list_entry((_evsel)->core.node.next, struct evsel, core.node))
395 
396 static inline bool evsel__has_branch_callstack(const struct evsel *evsel)
397 {
398 	return evsel->core.attr.branch_sample_type & PERF_SAMPLE_BRANCH_CALL_STACK;
399 }
400 
401 static inline bool evsel__has_branch_hw_idx(const struct evsel *evsel)
402 {
403 	return evsel->core.attr.branch_sample_type & PERF_SAMPLE_BRANCH_HW_INDEX;
404 }
405 
406 static inline bool evsel__has_callchain(const struct evsel *evsel)
407 {
408 	/*
409 	 * For reporting purposes, an evsel sample can have a recorded callchain
410 	 * or a callchain synthesized from AUX area data.
411 	 */
412 	return evsel->core.attr.sample_type & PERF_SAMPLE_CALLCHAIN ||
413 	       evsel->synth_sample_type & PERF_SAMPLE_CALLCHAIN;
414 }
415 
416 static inline bool evsel__has_br_stack(const struct evsel *evsel)
417 {
418 	/*
419 	 * For reporting purposes, an evsel sample can have a recorded branch
420 	 * stack or a branch stack synthesized from AUX area data.
421 	 */
422 	return evsel->core.attr.sample_type & PERF_SAMPLE_BRANCH_STACK ||
423 	       evsel->synth_sample_type & PERF_SAMPLE_BRANCH_STACK;
424 }
425 
426 static inline bool evsel__is_dummy_event(struct evsel *evsel)
427 {
428 	return (evsel->core.attr.type == PERF_TYPE_SOFTWARE) &&
429 	       (evsel->core.attr.config == PERF_COUNT_SW_DUMMY);
430 }
431 
432 struct perf_env *evsel__env(struct evsel *evsel);
433 
434 int evsel__store_ids(struct evsel *evsel, struct evlist *evlist);
435 
436 #endif /* __PERF_EVSEL_H */
437