xref: /openbmc/linux/tools/perf/util/evsel.h (revision 07c7c6bf)
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 <stddef.h>
8 #include <linux/perf_event.h>
9 #include <linux/types.h>
10 #include "xyarray.h"
11 #include "symbol_conf.h"
12 #include "cpumap.h"
13 #include "counts.h"
14 
15 struct perf_evsel;
16 
17 /*
18  * Per fd, to map back from PERF_SAMPLE_ID to evsel, only used when there are
19  * more than one entry in the evlist.
20  */
21 struct perf_sample_id {
22 	struct hlist_node 	node;
23 	u64		 	id;
24 	struct perf_evsel	*evsel;
25 	int			idx;
26 	int			cpu;
27 	pid_t			tid;
28 
29 	/* Holds total ID period value for PERF_SAMPLE_READ processing. */
30 	u64			period;
31 };
32 
33 struct cgroup;
34 
35 /*
36  * The 'struct perf_evsel_config_term' is used to pass event
37  * specific configuration data to perf_evsel__config routine.
38  * It is allocated within event parsing and attached to
39  * perf_evsel::config_terms list head.
40 */
41 enum term_type {
42 	PERF_EVSEL__CONFIG_TERM_PERIOD,
43 	PERF_EVSEL__CONFIG_TERM_FREQ,
44 	PERF_EVSEL__CONFIG_TERM_TIME,
45 	PERF_EVSEL__CONFIG_TERM_CALLGRAPH,
46 	PERF_EVSEL__CONFIG_TERM_STACK_USER,
47 	PERF_EVSEL__CONFIG_TERM_INHERIT,
48 	PERF_EVSEL__CONFIG_TERM_MAX_STACK,
49 	PERF_EVSEL__CONFIG_TERM_MAX_EVENTS,
50 	PERF_EVSEL__CONFIG_TERM_OVERWRITE,
51 	PERF_EVSEL__CONFIG_TERM_DRV_CFG,
52 	PERF_EVSEL__CONFIG_TERM_BRANCH,
53 	PERF_EVSEL__CONFIG_TERM_PERCORE,
54 };
55 
56 struct perf_evsel_config_term {
57 	struct list_head	list;
58 	enum term_type	type;
59 	union {
60 		u64	period;
61 		u64	freq;
62 		bool	time;
63 		char	*callgraph;
64 		char	*drv_cfg;
65 		u64	stack_user;
66 		int	max_stack;
67 		bool	inherit;
68 		bool	overwrite;
69 		char	*branch;
70 		unsigned long max_events;
71 		bool	percore;
72 	} val;
73 	bool weak;
74 };
75 
76 struct perf_stat_evsel;
77 
78 typedef int (perf_evsel__sb_cb_t)(union perf_event *event, void *data);
79 
80 enum perf_tool_event {
81 	PERF_TOOL_NONE		= 0,
82 	PERF_TOOL_DURATION_TIME = 1,
83 };
84 
85 /** struct perf_evsel - event selector
86  *
87  * @evlist - evlist this evsel is in, if it is in one.
88  * @node - To insert it into evlist->entries or in other list_heads, say in
89  *         the event parsing routines.
90  * @name - Can be set to retain the original event name passed by the user,
91  *         so that when showing results in tools such as 'perf stat', we
92  *         show the name used, not some alias.
93  * @id_pos: the position of the event id (PERF_SAMPLE_ID or
94  *          PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of
95  *          struct sample_event
96  * @is_pos: the position (counting backwards) of the event id (PERF_SAMPLE_ID or
97  *          PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if sample_id_all
98  *          is used there is an id sample appended to non-sample events
99  * @priv:   And what is in its containing unnamed union are tool specific
100  */
101 struct perf_evsel {
102 	struct list_head	node;
103 	struct perf_evlist	*evlist;
104 	struct perf_event_attr	attr;
105 	char			*filter;
106 	struct xyarray		*fd;
107 	struct xyarray		*sample_id;
108 	u64			*id;
109 	struct perf_counts	*counts;
110 	struct perf_counts	*prev_raw_counts;
111 	int			idx;
112 	u32			ids;
113 	unsigned long		max_events;
114 	unsigned long		nr_events_printed;
115 	char			*name;
116 	double			scale;
117 	const char		*unit;
118 	struct tep_event	*tp_format;
119 	off_t			id_offset;
120 	struct perf_stat_evsel  *stats;
121 	void			*priv;
122 	u64			db_id;
123 	struct cgroup		*cgrp;
124 	void			*handler;
125 	struct cpu_map		*cpus;
126 	struct cpu_map		*own_cpus;
127 	struct thread_map	*threads;
128 	unsigned int		sample_size;
129 	int			id_pos;
130 	int			is_pos;
131 	enum perf_tool_event	tool_event;
132 	bool			uniquified_name;
133 	bool			snapshot;
134 	bool 			supported;
135 	bool 			needs_swap;
136 	bool 			disabled;
137 	bool			no_aux_samples;
138 	bool			immediate;
139 	bool			system_wide;
140 	bool			tracking;
141 	bool			per_pkg;
142 	bool			precise_max;
143 	bool			ignore_missing_thread;
144 	bool			forced_leader;
145 	bool			use_uncore_alias;
146 	/* parse modifier helper */
147 	int			exclude_GH;
148 	int			nr_members;
149 	int			sample_read;
150 	unsigned long		*per_pkg_mask;
151 	struct perf_evsel	*leader;
152 	char			*group_name;
153 	bool			cmdline_group_boundary;
154 	struct list_head	config_terms;
155 	int			bpf_fd;
156 	bool			auto_merge_stats;
157 	bool			merged_stat;
158 	const char *		metric_expr;
159 	const char *		metric_name;
160 	struct perf_evsel	**metric_events;
161 	bool			collect_stat;
162 	bool			weak_group;
163 	bool			percore;
164 	const char		*pmu_name;
165 	struct {
166 		perf_evsel__sb_cb_t	*cb;
167 		void			*data;
168 	} side_band;
169 };
170 
171 union u64_swap {
172 	u64 val64;
173 	u32 val32[2];
174 };
175 
176 struct perf_missing_features {
177 	bool sample_id_all;
178 	bool exclude_guest;
179 	bool mmap2;
180 	bool cloexec;
181 	bool clockid;
182 	bool clockid_wrong;
183 	bool lbr_flags;
184 	bool write_backward;
185 	bool group_read;
186 	bool ksymbol;
187 	bool bpf_event;
188 };
189 
190 extern struct perf_missing_features perf_missing_features;
191 
192 struct cpu_map;
193 struct target;
194 struct thread_map;
195 struct record_opts;
196 
197 static inline struct cpu_map *perf_evsel__cpus(struct perf_evsel *evsel)
198 {
199 	return evsel->cpus;
200 }
201 
202 static inline int perf_evsel__nr_cpus(struct perf_evsel *evsel)
203 {
204 	return perf_evsel__cpus(evsel)->nr;
205 }
206 
207 void perf_counts_values__scale(struct perf_counts_values *count,
208 			       bool scale, s8 *pscaled);
209 
210 void perf_evsel__compute_deltas(struct perf_evsel *evsel, int cpu, int thread,
211 				struct perf_counts_values *count);
212 
213 int perf_evsel__object_config(size_t object_size,
214 			      int (*init)(struct perf_evsel *evsel),
215 			      void (*fini)(struct perf_evsel *evsel));
216 
217 struct perf_evsel *perf_evsel__new_idx(struct perf_event_attr *attr, int idx);
218 
219 static inline struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr)
220 {
221 	return perf_evsel__new_idx(attr, 0);
222 }
223 
224 struct perf_evsel *perf_evsel__newtp_idx(const char *sys, const char *name, int idx);
225 
226 /*
227  * Returns pointer with encoded error via <linux/err.h> interface.
228  */
229 static inline struct perf_evsel *perf_evsel__newtp(const char *sys, const char *name)
230 {
231 	return perf_evsel__newtp_idx(sys, name, 0);
232 }
233 
234 struct perf_evsel *perf_evsel__new_cycles(bool precise);
235 
236 struct tep_event *event_format__new(const char *sys, const char *name);
237 
238 void perf_evsel__init(struct perf_evsel *evsel,
239 		      struct perf_event_attr *attr, int idx);
240 void perf_evsel__exit(struct perf_evsel *evsel);
241 void perf_evsel__delete(struct perf_evsel *evsel);
242 
243 struct callchain_param;
244 
245 void perf_evsel__config(struct perf_evsel *evsel,
246 			struct record_opts *opts,
247 			struct callchain_param *callchain);
248 void perf_evsel__config_callchain(struct perf_evsel *evsel,
249 				  struct record_opts *opts,
250 				  struct callchain_param *callchain);
251 
252 int __perf_evsel__sample_size(u64 sample_type);
253 void perf_evsel__calc_id_pos(struct perf_evsel *evsel);
254 
255 bool perf_evsel__is_cache_op_valid(u8 type, u8 op);
256 
257 #define PERF_EVSEL__MAX_ALIASES 8
258 
259 extern const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
260 				       [PERF_EVSEL__MAX_ALIASES];
261 extern const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
262 					  [PERF_EVSEL__MAX_ALIASES];
263 extern const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
264 					      [PERF_EVSEL__MAX_ALIASES];
265 extern const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX];
266 extern const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX];
267 int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
268 					    char *bf, size_t size);
269 const char *perf_evsel__name(struct perf_evsel *evsel);
270 
271 const char *perf_evsel__group_name(struct perf_evsel *evsel);
272 int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size);
273 
274 int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads);
275 void perf_evsel__close_fd(struct perf_evsel *evsel);
276 
277 void __perf_evsel__set_sample_bit(struct perf_evsel *evsel,
278 				  enum perf_event_sample_format bit);
279 void __perf_evsel__reset_sample_bit(struct perf_evsel *evsel,
280 				    enum perf_event_sample_format bit);
281 
282 #define perf_evsel__set_sample_bit(evsel, bit) \
283 	__perf_evsel__set_sample_bit(evsel, PERF_SAMPLE_##bit)
284 
285 #define perf_evsel__reset_sample_bit(evsel, bit) \
286 	__perf_evsel__reset_sample_bit(evsel, PERF_SAMPLE_##bit)
287 
288 void perf_evsel__set_sample_id(struct perf_evsel *evsel,
289 			       bool use_sample_identifier);
290 
291 int perf_evsel__set_filter(struct perf_evsel *evsel, const char *filter);
292 int perf_evsel__append_tp_filter(struct perf_evsel *evsel, const char *filter);
293 int perf_evsel__append_addr_filter(struct perf_evsel *evsel,
294 				   const char *filter);
295 int perf_evsel__apply_filter(struct perf_evsel *evsel, const char *filter);
296 int perf_evsel__enable(struct perf_evsel *evsel);
297 int perf_evsel__disable(struct perf_evsel *evsel);
298 
299 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
300 			     struct cpu_map *cpus);
301 int perf_evsel__open_per_thread(struct perf_evsel *evsel,
302 				struct thread_map *threads);
303 int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
304 		     struct thread_map *threads);
305 void perf_evsel__close(struct perf_evsel *evsel);
306 
307 struct perf_sample;
308 
309 void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
310 			 const char *name);
311 u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
312 		       const char *name);
313 
314 static inline char *perf_evsel__strval(struct perf_evsel *evsel,
315 				       struct perf_sample *sample,
316 				       const char *name)
317 {
318 	return perf_evsel__rawptr(evsel, sample, name);
319 }
320 
321 struct tep_format_field;
322 
323 u64 format_field__intval(struct tep_format_field *field, struct perf_sample *sample, bool needs_swap);
324 
325 struct tep_format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name);
326 
327 #define perf_evsel__match(evsel, t, c)		\
328 	(evsel->attr.type == PERF_TYPE_##t &&	\
329 	 evsel->attr.config == PERF_COUNT_##c)
330 
331 static inline bool perf_evsel__match2(struct perf_evsel *e1,
332 				      struct perf_evsel *e2)
333 {
334 	return (e1->attr.type == e2->attr.type) &&
335 	       (e1->attr.config == e2->attr.config);
336 }
337 
338 #define perf_evsel__cmp(a, b)			\
339 	((a) &&					\
340 	 (b) &&					\
341 	 (a)->attr.type == (b)->attr.type &&	\
342 	 (a)->attr.config == (b)->attr.config)
343 
344 int perf_evsel__read(struct perf_evsel *evsel, int cpu, int thread,
345 		     struct perf_counts_values *count);
346 
347 int perf_evsel__read_counter(struct perf_evsel *evsel, int cpu, int thread);
348 
349 int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
350 			      int cpu, int thread, bool scale);
351 
352 /**
353  * perf_evsel__read_on_cpu - Read out the results on a CPU and thread
354  *
355  * @evsel - event selector to read value
356  * @cpu - CPU of interest
357  * @thread - thread of interest
358  */
359 static inline int perf_evsel__read_on_cpu(struct perf_evsel *evsel,
360 					  int cpu, int thread)
361 {
362 	return __perf_evsel__read_on_cpu(evsel, cpu, thread, false);
363 }
364 
365 /**
366  * perf_evsel__read_on_cpu_scaled - Read out the results on a CPU and thread, scaled
367  *
368  * @evsel - event selector to read value
369  * @cpu - CPU of interest
370  * @thread - thread of interest
371  */
372 static inline int perf_evsel__read_on_cpu_scaled(struct perf_evsel *evsel,
373 						 int cpu, int thread)
374 {
375 	return __perf_evsel__read_on_cpu(evsel, cpu, thread, true);
376 }
377 
378 int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
379 			     struct perf_sample *sample);
380 
381 int perf_evsel__parse_sample_timestamp(struct perf_evsel *evsel,
382 				       union perf_event *event,
383 				       u64 *timestamp);
384 
385 static inline struct perf_evsel *perf_evsel__next(struct perf_evsel *evsel)
386 {
387 	return list_entry(evsel->node.next, struct perf_evsel, node);
388 }
389 
390 static inline struct perf_evsel *perf_evsel__prev(struct perf_evsel *evsel)
391 {
392 	return list_entry(evsel->node.prev, struct perf_evsel, node);
393 }
394 
395 /**
396  * perf_evsel__is_group_leader - Return whether given evsel is a leader event
397  *
398  * @evsel - evsel selector to be tested
399  *
400  * Return %true if @evsel is a group leader or a stand-alone event
401  */
402 static inline bool perf_evsel__is_group_leader(const struct perf_evsel *evsel)
403 {
404 	return evsel->leader == evsel;
405 }
406 
407 /**
408  * perf_evsel__is_group_event - Return whether given evsel is a group event
409  *
410  * @evsel - evsel selector to be tested
411  *
412  * Return %true iff event group view is enabled and @evsel is a actual group
413  * leader which has other members in the group
414  */
415 static inline bool perf_evsel__is_group_event(struct perf_evsel *evsel)
416 {
417 	if (!symbol_conf.event_group)
418 		return false;
419 
420 	return perf_evsel__is_group_leader(evsel) && evsel->nr_members > 1;
421 }
422 
423 bool perf_evsel__is_function_event(struct perf_evsel *evsel);
424 
425 static inline bool perf_evsel__is_bpf_output(struct perf_evsel *evsel)
426 {
427 	return perf_evsel__match(evsel, SOFTWARE, SW_BPF_OUTPUT);
428 }
429 
430 static inline bool perf_evsel__is_clock(struct perf_evsel *evsel)
431 {
432 	return perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
433 	       perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK);
434 }
435 
436 struct perf_attr_details {
437 	bool freq;
438 	bool verbose;
439 	bool event_group;
440 	bool force;
441 	bool trace_fields;
442 };
443 
444 int perf_evsel__fprintf(struct perf_evsel *evsel,
445 			struct perf_attr_details *details, FILE *fp);
446 
447 #define EVSEL__PRINT_IP			(1<<0)
448 #define EVSEL__PRINT_SYM		(1<<1)
449 #define EVSEL__PRINT_DSO		(1<<2)
450 #define EVSEL__PRINT_SYMOFFSET		(1<<3)
451 #define EVSEL__PRINT_ONELINE		(1<<4)
452 #define EVSEL__PRINT_SRCLINE		(1<<5)
453 #define EVSEL__PRINT_UNKNOWN_AS_ADDR	(1<<6)
454 #define EVSEL__PRINT_CALLCHAIN_ARROW	(1<<7)
455 #define EVSEL__PRINT_SKIP_IGNORED	(1<<8)
456 
457 struct callchain_cursor;
458 
459 int sample__fprintf_callchain(struct perf_sample *sample, int left_alignment,
460 			      unsigned int print_opts,
461 			      struct callchain_cursor *cursor, FILE *fp);
462 
463 int sample__fprintf_sym(struct perf_sample *sample, struct addr_location *al,
464 			int left_alignment, unsigned int print_opts,
465 			struct callchain_cursor *cursor, FILE *fp);
466 
467 bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
468 			  char *msg, size_t msgsize);
469 int perf_evsel__open_strerror(struct perf_evsel *evsel, struct target *target,
470 			      int err, char *msg, size_t size);
471 
472 static inline int perf_evsel__group_idx(struct perf_evsel *evsel)
473 {
474 	return evsel->idx - evsel->leader->idx;
475 }
476 
477 /* Iterates group WITHOUT the leader. */
478 #define for_each_group_member(_evsel, _leader) 					\
479 for ((_evsel) = list_entry((_leader)->node.next, struct perf_evsel, node); 	\
480      (_evsel) && (_evsel)->leader == (_leader);					\
481      (_evsel) = list_entry((_evsel)->node.next, struct perf_evsel, node))
482 
483 /* Iterates group WITH the leader. */
484 #define for_each_group_evsel(_evsel, _leader) 					\
485 for ((_evsel) = _leader; 							\
486      (_evsel) && (_evsel)->leader == (_leader);					\
487      (_evsel) = list_entry((_evsel)->node.next, struct perf_evsel, node))
488 
489 static inline bool perf_evsel__has_branch_callstack(const struct perf_evsel *evsel)
490 {
491 	return evsel->attr.branch_sample_type & PERF_SAMPLE_BRANCH_CALL_STACK;
492 }
493 
494 static inline bool evsel__has_callchain(const struct perf_evsel *evsel)
495 {
496 	return (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) != 0;
497 }
498 
499 typedef int (*attr__fprintf_f)(FILE *, const char *, const char *, void *);
500 
501 int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr,
502 			     attr__fprintf_f attr__fprintf, void *priv);
503 
504 struct perf_env *perf_evsel__env(struct perf_evsel *evsel);
505 
506 int perf_evsel__store_ids(struct perf_evsel *evsel, struct perf_evlist *evlist);
507 #endif /* __PERF_EVSEL_H */
508