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