xref: /openbmc/linux/tools/perf/util/evlist.h (revision ecc23d0a422a3118fcf6e4f0a46e17a6c2047b02)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __PERF_EVLIST_H
3 #define __PERF_EVLIST_H 1
4 
5 #include <linux/compiler.h>
6 #include <linux/kernel.h>
7 #include <linux/refcount.h>
8 #include <linux/list.h>
9 #include <api/fd/array.h>
10 #include <internal/evlist.h>
11 #include <internal/evsel.h>
12 #include <perf/evlist.h>
13 #include "events_stats.h"
14 #include "evsel.h"
15 #include <pthread.h>
16 #include <signal.h>
17 #include <unistd.h>
18 
19 struct pollfd;
20 struct thread_map;
21 struct perf_cpu_map;
22 struct record_opts;
23 
24 /*
25  * State machine of bkw_mmap_state:
26  *
27  *                     .________________(forbid)_____________.
28  *                     |                                     V
29  * NOTREADY --(0)--> RUNNING --(1)--> DATA_PENDING --(2)--> EMPTY
30  *                     ^  ^              |   ^               |
31  *                     |  |__(forbid)____/   |___(forbid)___/|
32  *                     |                                     |
33  *                      \_________________(3)_______________/
34  *
35  * NOTREADY     : Backward ring buffers are not ready
36  * RUNNING      : Backward ring buffers are recording
37  * DATA_PENDING : We are required to collect data from backward ring buffers
38  * EMPTY        : We have collected data from backward ring buffers.
39  *
40  * (0): Setup backward ring buffer
41  * (1): Pause ring buffers for reading
42  * (2): Read from ring buffers
43  * (3): Resume ring buffers for recording
44  */
45 enum bkw_mmap_state {
46 	BKW_MMAP_NOTREADY,
47 	BKW_MMAP_RUNNING,
48 	BKW_MMAP_DATA_PENDING,
49 	BKW_MMAP_EMPTY,
50 };
51 
52 struct event_enable_timer;
53 
54 struct evlist {
55 	struct perf_evlist core;
56 	bool		 enabled;
57 	int		 id_pos;
58 	int		 is_pos;
59 	u64		 combined_sample_type;
60 	enum bkw_mmap_state bkw_mmap_state;
61 	struct {
62 		int	cork_fd;
63 		pid_t	pid;
64 	} workload;
65 	struct mmap *mmap;
66 	struct mmap *overwrite_mmap;
67 	struct evsel *selected;
68 	struct events_stats stats;
69 	struct perf_env	*env;
70 	void (*trace_event_sample_raw)(struct evlist *evlist,
71 				       union perf_event *event,
72 				       struct perf_sample *sample);
73 	u64		first_sample_time;
74 	u64		last_sample_time;
75 	struct {
76 		pthread_t		th;
77 		volatile int		done;
78 	} thread;
79 	struct {
80 		int	fd;	/* control file descriptor */
81 		int	ack;	/* ack file descriptor for control commands */
82 		int	pos;	/* index at evlist core object to check signals */
83 	} ctl_fd;
84 	struct event_enable_timer *eet;
85 };
86 
87 struct evsel_str_handler {
88 	const char *name;
89 	void	   *handler;
90 };
91 
92 struct evlist *evlist__new(void);
93 struct evlist *evlist__new_default(void);
94 struct evlist *evlist__new_dummy(void);
95 void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus,
96 		  struct perf_thread_map *threads);
97 void evlist__exit(struct evlist *evlist);
98 void evlist__delete(struct evlist *evlist);
99 
100 void evlist__add(struct evlist *evlist, struct evsel *entry);
101 void evlist__remove(struct evlist *evlist, struct evsel *evsel);
102 
103 int evlist__add_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs);
104 
105 int __evlist__add_default_attrs(struct evlist *evlist,
106 				     struct perf_event_attr *attrs, size_t nr_attrs);
107 
108 int arch_evlist__add_default_attrs(struct evlist *evlist,
109 				   struct perf_event_attr *attrs,
110 				   size_t nr_attrs);
111 
112 #define evlist__add_default_attrs(evlist, array) \
113 	arch_evlist__add_default_attrs(evlist, array, ARRAY_SIZE(array))
114 
115 int arch_evlist__cmp(const struct evsel *lhs, const struct evsel *rhs);
116 
117 int evlist__add_dummy(struct evlist *evlist);
118 struct evsel *evlist__add_aux_dummy(struct evlist *evlist, bool system_wide);
evlist__add_dummy_on_all_cpus(struct evlist * evlist)119 static inline struct evsel *evlist__add_dummy_on_all_cpus(struct evlist *evlist)
120 {
121 	return evlist__add_aux_dummy(evlist, true);
122 }
123 #ifdef HAVE_LIBTRACEEVENT
124 struct evsel *evlist__add_sched_switch(struct evlist *evlist, bool system_wide);
125 #endif
126 
127 int evlist__add_sb_event(struct evlist *evlist, struct perf_event_attr *attr,
128 			 evsel__sb_cb_t cb, void *data);
129 void evlist__set_cb(struct evlist *evlist, evsel__sb_cb_t cb, void *data);
130 int evlist__start_sb_thread(struct evlist *evlist, struct target *target);
131 void evlist__stop_sb_thread(struct evlist *evlist);
132 
133 #ifdef HAVE_LIBTRACEEVENT
134 int evlist__add_newtp(struct evlist *evlist, const char *sys, const char *name, void *handler);
135 #endif
136 
137 int __evlist__set_tracepoints_handlers(struct evlist *evlist,
138 				       const struct evsel_str_handler *assocs,
139 				       size_t nr_assocs);
140 
141 #define evlist__set_tracepoints_handlers(evlist, array) \
142 	__evlist__set_tracepoints_handlers(evlist, array, ARRAY_SIZE(array))
143 
144 int evlist__set_tp_filter(struct evlist *evlist, const char *filter);
145 int evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid);
146 int evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids);
147 
148 int evlist__append_tp_filter(struct evlist *evlist, const char *filter);
149 
150 int evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid);
151 int evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids);
152 
153 struct evsel *evlist__find_tracepoint_by_id(struct evlist *evlist, int id);
154 struct evsel *evlist__find_tracepoint_by_name(struct evlist *evlist, const char *name);
155 
156 int evlist__add_pollfd(struct evlist *evlist, int fd);
157 int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask);
158 
159 #ifdef HAVE_EVENTFD_SUPPORT
160 int evlist__add_wakeup_eventfd(struct evlist *evlist, int fd);
161 #endif
162 
163 int evlist__poll(struct evlist *evlist, int timeout);
164 
165 struct evsel *evlist__id2evsel(struct evlist *evlist, u64 id);
166 struct evsel *evlist__id2evsel_strict(struct evlist *evlist, u64 id);
167 
168 struct perf_sample_id *evlist__id2sid(struct evlist *evlist, u64 id);
169 
170 void evlist__toggle_bkw_mmap(struct evlist *evlist, enum bkw_mmap_state state);
171 
172 void evlist__mmap_consume(struct evlist *evlist, int idx);
173 
174 int evlist__open(struct evlist *evlist);
175 void evlist__close(struct evlist *evlist);
176 
177 struct callchain_param;
178 
179 void evlist__set_id_pos(struct evlist *evlist);
180 void evlist__config(struct evlist *evlist, struct record_opts *opts, struct callchain_param *callchain);
181 int record_opts__config(struct record_opts *opts);
182 
183 int evlist__prepare_workload(struct evlist *evlist, struct target *target,
184 			     const char *argv[], bool pipe_output,
185 			     void (*exec_error)(int signo, siginfo_t *info, void *ucontext));
186 int evlist__start_workload(struct evlist *evlist);
187 void evlist__cancel_workload(struct evlist *evlist);
188 
189 struct option;
190 
191 int __evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str);
192 int evlist__parse_mmap_pages(const struct option *opt, const char *str, int unset);
193 
194 unsigned long perf_event_mlock_kb_in_pages(void);
195 
196 int evlist__mmap_ex(struct evlist *evlist, unsigned int pages,
197 			 unsigned int auxtrace_pages,
198 			 bool auxtrace_overwrite, int nr_cblocks,
199 			 int affinity, int flush, int comp_level);
200 int evlist__mmap(struct evlist *evlist, unsigned int pages);
201 void evlist__munmap(struct evlist *evlist);
202 
203 size_t evlist__mmap_size(unsigned long pages);
204 
205 void evlist__disable(struct evlist *evlist);
206 void evlist__enable(struct evlist *evlist);
207 void evlist__toggle_enable(struct evlist *evlist);
208 void evlist__disable_evsel(struct evlist *evlist, char *evsel_name);
209 void evlist__enable_evsel(struct evlist *evlist, char *evsel_name);
210 void evlist__disable_non_dummy(struct evlist *evlist);
211 void evlist__enable_non_dummy(struct evlist *evlist);
212 
213 void evlist__set_selected(struct evlist *evlist, struct evsel *evsel);
214 
215 int evlist__create_maps(struct evlist *evlist, struct target *target);
216 int evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel);
217 
218 u64 __evlist__combined_sample_type(struct evlist *evlist);
219 u64 evlist__combined_sample_type(struct evlist *evlist);
220 u64 evlist__combined_branch_type(struct evlist *evlist);
221 bool evlist__sample_id_all(struct evlist *evlist);
222 u16 evlist__id_hdr_size(struct evlist *evlist);
223 
224 int evlist__parse_sample(struct evlist *evlist, union perf_event *event, struct perf_sample *sample);
225 int evlist__parse_sample_timestamp(struct evlist *evlist, union perf_event *event, u64 *timestamp);
226 
227 bool evlist__valid_sample_type(struct evlist *evlist);
228 bool evlist__valid_sample_id_all(struct evlist *evlist);
229 bool evlist__valid_read_format(struct evlist *evlist);
230 
231 void evlist__splice_list_tail(struct evlist *evlist, struct list_head *list);
232 
evlist__empty(struct evlist * evlist)233 static inline bool evlist__empty(struct evlist *evlist)
234 {
235 	return list_empty(&evlist->core.entries);
236 }
237 
evlist__first(struct evlist * evlist)238 static inline struct evsel *evlist__first(struct evlist *evlist)
239 {
240 	struct perf_evsel *evsel = perf_evlist__first(&evlist->core);
241 
242 	return container_of(evsel, struct evsel, core);
243 }
244 
evlist__last(struct evlist * evlist)245 static inline struct evsel *evlist__last(struct evlist *evlist)
246 {
247 	struct perf_evsel *evsel = perf_evlist__last(&evlist->core);
248 
249 	return container_of(evsel, struct evsel, core);
250 }
251 
evlist__nr_groups(struct evlist * evlist)252 static inline int evlist__nr_groups(struct evlist *evlist)
253 {
254 	return perf_evlist__nr_groups(&evlist->core);
255 }
256 
257 int evlist__strerror_open(struct evlist *evlist, int err, char *buf, size_t size);
258 int evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size);
259 
260 bool evlist__can_select_event(struct evlist *evlist, const char *str);
261 void evlist__to_front(struct evlist *evlist, struct evsel *move_evsel);
262 
263 /**
264  * __evlist__for_each_entry - iterate thru all the evsels
265  * @list: list_head instance to iterate
266  * @evsel: struct evsel iterator
267  */
268 #define __evlist__for_each_entry(list, evsel) \
269         list_for_each_entry(evsel, list, core.node)
270 
271 /**
272  * evlist__for_each_entry - iterate thru all the evsels
273  * @evlist: evlist instance to iterate
274  * @evsel: struct evsel iterator
275  */
276 #define evlist__for_each_entry(evlist, evsel) \
277 	__evlist__for_each_entry(&(evlist)->core.entries, evsel)
278 
279 /**
280  * __evlist__for_each_entry_continue - continue iteration thru all the evsels
281  * @list: list_head instance to iterate
282  * @evsel: struct evsel iterator
283  */
284 #define __evlist__for_each_entry_continue(list, evsel) \
285         list_for_each_entry_continue(evsel, list, core.node)
286 
287 /**
288  * evlist__for_each_entry_continue - continue iteration thru all the evsels
289  * @evlist: evlist instance to iterate
290  * @evsel: struct evsel iterator
291  */
292 #define evlist__for_each_entry_continue(evlist, evsel) \
293 	__evlist__for_each_entry_continue(&(evlist)->core.entries, evsel)
294 
295 /**
296  * __evlist__for_each_entry_from - continue iteration from @evsel (included)
297  * @list: list_head instance to iterate
298  * @evsel: struct evsel iterator
299  */
300 #define __evlist__for_each_entry_from(list, evsel) \
301 	list_for_each_entry_from(evsel, list, core.node)
302 
303 /**
304  * evlist__for_each_entry_from - continue iteration from @evsel (included)
305  * @evlist: evlist instance to iterate
306  * @evsel: struct evsel iterator
307  */
308 #define evlist__for_each_entry_from(evlist, evsel) \
309 	__evlist__for_each_entry_from(&(evlist)->core.entries, evsel)
310 
311 /**
312  * __evlist__for_each_entry_reverse - iterate thru all the evsels in reverse order
313  * @list: list_head instance to iterate
314  * @evsel: struct evsel iterator
315  */
316 #define __evlist__for_each_entry_reverse(list, evsel) \
317         list_for_each_entry_reverse(evsel, list, core.node)
318 
319 /**
320  * evlist__for_each_entry_reverse - iterate thru all the evsels in reverse order
321  * @evlist: evlist instance to iterate
322  * @evsel: struct evsel iterator
323  */
324 #define evlist__for_each_entry_reverse(evlist, evsel) \
325 	__evlist__for_each_entry_reverse(&(evlist)->core.entries, evsel)
326 
327 /**
328  * __evlist__for_each_entry_safe - safely iterate thru all the evsels
329  * @list: list_head instance to iterate
330  * @tmp: struct evsel temp iterator
331  * @evsel: struct evsel iterator
332  */
333 #define __evlist__for_each_entry_safe(list, tmp, evsel) \
334         list_for_each_entry_safe(evsel, tmp, list, core.node)
335 
336 /**
337  * evlist__for_each_entry_safe - safely iterate thru all the evsels
338  * @evlist: evlist instance to iterate
339  * @evsel: struct evsel iterator
340  * @tmp: struct evsel temp iterator
341  */
342 #define evlist__for_each_entry_safe(evlist, tmp, evsel) \
343 	__evlist__for_each_entry_safe(&(evlist)->core.entries, tmp, evsel)
344 
345 /** Iterator state for evlist__for_each_cpu */
346 struct evlist_cpu_iterator {
347 	/** The list being iterated through. */
348 	struct evlist *container;
349 	/** The current evsel of the iterator. */
350 	struct evsel *evsel;
351 	/** The CPU map index corresponding to the evsel->core.cpus for the current CPU. */
352 	int cpu_map_idx;
353 	/**
354 	 * The CPU map index corresponding to evlist->core.all_cpus for the
355 	 * current CPU.  Distinct from cpu_map_idx as the evsel's cpu map may
356 	 * contain fewer entries.
357 	 */
358 	int evlist_cpu_map_idx;
359 	/** The number of CPU map entries in evlist->core.all_cpus. */
360 	int evlist_cpu_map_nr;
361 	/** The current CPU of the iterator. */
362 	struct perf_cpu cpu;
363 	/** If present, used to set the affinity when switching between CPUs. */
364 	struct affinity *affinity;
365 };
366 
367 /**
368  * evlist__for_each_cpu - without affinity, iterate over the evlist. With
369  *                        affinity, iterate over all CPUs and then the evlist
370  *                        for each evsel on that CPU. When switching between
371  *                        CPUs the affinity is set to the CPU to avoid IPIs
372  *                        during syscalls.
373  * @evlist_cpu_itr: the iterator instance.
374  * @evlist: evlist instance to iterate.
375  * @affinity: NULL or used to set the affinity to the current CPU.
376  */
377 #define evlist__for_each_cpu(evlist_cpu_itr, evlist, affinity)		\
378 	for ((evlist_cpu_itr) = evlist__cpu_begin(evlist, affinity);	\
379 	     !evlist_cpu_iterator__end(&evlist_cpu_itr);		\
380 	     evlist_cpu_iterator__next(&evlist_cpu_itr))
381 
382 /** Returns an iterator set to the first CPU/evsel of evlist. */
383 struct evlist_cpu_iterator evlist__cpu_begin(struct evlist *evlist, struct affinity *affinity);
384 /** Move to next element in iterator, updating CPU, evsel and the affinity. */
385 void evlist_cpu_iterator__next(struct evlist_cpu_iterator *evlist_cpu_itr);
386 /** Returns true when iterator is at the end of the CPUs and evlist. */
387 bool evlist_cpu_iterator__end(const struct evlist_cpu_iterator *evlist_cpu_itr);
388 
389 struct evsel *evlist__get_tracking_event(struct evlist *evlist);
390 void evlist__set_tracking_event(struct evlist *evlist, struct evsel *tracking_evsel);
391 struct evsel *evlist__findnew_tracking_event(struct evlist *evlist, bool system_wide);
392 
393 struct evsel *evlist__find_evsel_by_str(struct evlist *evlist, const char *str);
394 
395 struct evsel *evlist__event2evsel(struct evlist *evlist, union perf_event *event);
396 
397 bool evlist__exclude_kernel(struct evlist *evlist);
398 
399 void evlist__force_leader(struct evlist *evlist);
400 
401 struct evsel *evlist__reset_weak_group(struct evlist *evlist, struct evsel *evsel, bool close);
402 
403 #define EVLIST_CTL_CMD_ENABLE_TAG  "enable"
404 #define EVLIST_CTL_CMD_DISABLE_TAG "disable"
405 #define EVLIST_CTL_CMD_ACK_TAG     "ack\n"
406 #define EVLIST_CTL_CMD_SNAPSHOT_TAG "snapshot"
407 #define EVLIST_CTL_CMD_EVLIST_TAG "evlist"
408 #define EVLIST_CTL_CMD_STOP_TAG "stop"
409 #define EVLIST_CTL_CMD_PING_TAG "ping"
410 
411 #define EVLIST_CTL_CMD_MAX_LEN 64
412 
413 enum evlist_ctl_cmd {
414 	EVLIST_CTL_CMD_UNSUPPORTED = 0,
415 	EVLIST_CTL_CMD_ENABLE,
416 	EVLIST_CTL_CMD_DISABLE,
417 	EVLIST_CTL_CMD_ACK,
418 	EVLIST_CTL_CMD_SNAPSHOT,
419 	EVLIST_CTL_CMD_EVLIST,
420 	EVLIST_CTL_CMD_STOP,
421 	EVLIST_CTL_CMD_PING,
422 };
423 
424 int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close);
425 void evlist__close_control(int ctl_fd, int ctl_fd_ack, bool *ctl_fd_close);
426 int evlist__initialize_ctlfd(struct evlist *evlist, int ctl_fd, int ctl_fd_ack);
427 int evlist__finalize_ctlfd(struct evlist *evlist);
428 bool evlist__ctlfd_initialized(struct evlist *evlist);
429 int evlist__ctlfd_process(struct evlist *evlist, enum evlist_ctl_cmd *cmd);
430 int evlist__ctlfd_ack(struct evlist *evlist);
431 
432 #define EVLIST_ENABLED_MSG "Events enabled\n"
433 #define EVLIST_DISABLED_MSG "Events disabled\n"
434 
435 int evlist__parse_event_enable_time(struct evlist *evlist, struct record_opts *opts,
436 				    const char *str, int unset);
437 int event_enable_timer__start(struct event_enable_timer *eet);
438 void event_enable_timer__exit(struct event_enable_timer **ep);
439 int event_enable_timer__process(struct event_enable_timer *eet);
440 
441 struct evsel *evlist__find_evsel(struct evlist *evlist, int idx);
442 
443 int evlist__scnprintf_evsels(struct evlist *evlist, size_t size, char *bf);
444 void evlist__check_mem_load_aux(struct evlist *evlist);
445 void evlist__warn_user_requested_cpus(struct evlist *evlist, const char *cpu_list);
446 void evlist__uniquify_name(struct evlist *evlist);
447 
448 #endif /* __PERF_EVLIST_H */
449