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