xref: /openbmc/linux/tools/perf/util/evlist.h (revision 66c98360)
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 	const char *hybrid_pmu_name;
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_default(struct evlist *evlist, bool precise);
105 
106 static inline int evlist__add_default(struct evlist *evlist)
107 {
108 	return __evlist__add_default(evlist, true);
109 }
110 
111 int evlist__add_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs);
112 
113 int __evlist__add_default_attrs(struct evlist *evlist,
114 				     struct perf_event_attr *attrs, size_t nr_attrs);
115 
116 int arch_evlist__add_default_attrs(struct evlist *evlist,
117 				   struct perf_event_attr *attrs,
118 				   size_t nr_attrs);
119 
120 #define evlist__add_default_attrs(evlist, array) \
121 	arch_evlist__add_default_attrs(evlist, array, ARRAY_SIZE(array))
122 
123 int arch_evlist__cmp(const struct evsel *lhs, const struct evsel *rhs);
124 
125 int evlist__add_dummy(struct evlist *evlist);
126 struct evsel *evlist__add_aux_dummy(struct evlist *evlist, bool system_wide);
127 static inline struct evsel *evlist__add_dummy_on_all_cpus(struct evlist *evlist)
128 {
129 	return evlist__add_aux_dummy(evlist, true);
130 }
131 #ifdef HAVE_LIBTRACEEVENT
132 struct evsel *evlist__add_sched_switch(struct evlist *evlist, bool system_wide);
133 #endif
134 
135 int evlist__add_sb_event(struct evlist *evlist, struct perf_event_attr *attr,
136 			 evsel__sb_cb_t cb, void *data);
137 void evlist__set_cb(struct evlist *evlist, evsel__sb_cb_t cb, void *data);
138 int evlist__start_sb_thread(struct evlist *evlist, struct target *target);
139 void evlist__stop_sb_thread(struct evlist *evlist);
140 
141 #ifdef HAVE_LIBTRACEEVENT
142 int evlist__add_newtp(struct evlist *evlist, const char *sys, const char *name, void *handler);
143 #endif
144 
145 int __evlist__set_tracepoints_handlers(struct evlist *evlist,
146 				       const struct evsel_str_handler *assocs,
147 				       size_t nr_assocs);
148 
149 #define evlist__set_tracepoints_handlers(evlist, array) \
150 	__evlist__set_tracepoints_handlers(evlist, array, ARRAY_SIZE(array))
151 
152 int evlist__set_tp_filter(struct evlist *evlist, const char *filter);
153 int evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid);
154 int evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids);
155 
156 int evlist__append_tp_filter(struct evlist *evlist, const char *filter);
157 
158 int evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid);
159 int evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids);
160 
161 struct evsel *evlist__find_tracepoint_by_id(struct evlist *evlist, int id);
162 struct evsel *evlist__find_tracepoint_by_name(struct evlist *evlist, const char *name);
163 
164 int evlist__add_pollfd(struct evlist *evlist, int fd);
165 int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask);
166 
167 #ifdef HAVE_EVENTFD_SUPPORT
168 int evlist__add_wakeup_eventfd(struct evlist *evlist, int fd);
169 #endif
170 
171 int evlist__poll(struct evlist *evlist, int timeout);
172 
173 struct evsel *evlist__id2evsel(struct evlist *evlist, u64 id);
174 struct evsel *evlist__id2evsel_strict(struct evlist *evlist, u64 id);
175 
176 struct perf_sample_id *evlist__id2sid(struct evlist *evlist, u64 id);
177 
178 void evlist__toggle_bkw_mmap(struct evlist *evlist, enum bkw_mmap_state state);
179 
180 void evlist__mmap_consume(struct evlist *evlist, int idx);
181 
182 int evlist__open(struct evlist *evlist);
183 void evlist__close(struct evlist *evlist);
184 
185 struct callchain_param;
186 
187 void evlist__set_id_pos(struct evlist *evlist);
188 void evlist__config(struct evlist *evlist, struct record_opts *opts, struct callchain_param *callchain);
189 int record_opts__config(struct record_opts *opts);
190 
191 int evlist__prepare_workload(struct evlist *evlist, struct target *target,
192 			     const char *argv[], bool pipe_output,
193 			     void (*exec_error)(int signo, siginfo_t *info, void *ucontext));
194 int evlist__start_workload(struct evlist *evlist);
195 
196 struct option;
197 
198 int __evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str);
199 int evlist__parse_mmap_pages(const struct option *opt, const char *str, int unset);
200 
201 unsigned long perf_event_mlock_kb_in_pages(void);
202 
203 int evlist__mmap_ex(struct evlist *evlist, unsigned int pages,
204 			 unsigned int auxtrace_pages,
205 			 bool auxtrace_overwrite, int nr_cblocks,
206 			 int affinity, int flush, int comp_level);
207 int evlist__mmap(struct evlist *evlist, unsigned int pages);
208 void evlist__munmap(struct evlist *evlist);
209 
210 size_t evlist__mmap_size(unsigned long pages);
211 
212 void evlist__disable(struct evlist *evlist);
213 void evlist__enable(struct evlist *evlist);
214 void evlist__toggle_enable(struct evlist *evlist);
215 void evlist__disable_evsel(struct evlist *evlist, char *evsel_name);
216 void evlist__enable_evsel(struct evlist *evlist, char *evsel_name);
217 void evlist__disable_non_dummy(struct evlist *evlist);
218 void evlist__enable_non_dummy(struct evlist *evlist);
219 
220 void evlist__set_selected(struct evlist *evlist, struct evsel *evsel);
221 
222 int evlist__create_maps(struct evlist *evlist, struct target *target);
223 int evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel);
224 
225 u64 __evlist__combined_sample_type(struct evlist *evlist);
226 u64 evlist__combined_sample_type(struct evlist *evlist);
227 u64 evlist__combined_branch_type(struct evlist *evlist);
228 bool evlist__sample_id_all(struct evlist *evlist);
229 u16 evlist__id_hdr_size(struct evlist *evlist);
230 
231 int evlist__parse_sample(struct evlist *evlist, union perf_event *event, struct perf_sample *sample);
232 int evlist__parse_sample_timestamp(struct evlist *evlist, union perf_event *event, u64 *timestamp);
233 
234 bool evlist__valid_sample_type(struct evlist *evlist);
235 bool evlist__valid_sample_id_all(struct evlist *evlist);
236 bool evlist__valid_read_format(struct evlist *evlist);
237 
238 void evlist__splice_list_tail(struct evlist *evlist, struct list_head *list);
239 
240 static inline bool evlist__empty(struct evlist *evlist)
241 {
242 	return list_empty(&evlist->core.entries);
243 }
244 
245 static inline struct evsel *evlist__first(struct evlist *evlist)
246 {
247 	struct perf_evsel *evsel = perf_evlist__first(&evlist->core);
248 
249 	return container_of(evsel, struct evsel, core);
250 }
251 
252 static inline struct evsel *evlist__last(struct evlist *evlist)
253 {
254 	struct perf_evsel *evsel = perf_evlist__last(&evlist->core);
255 
256 	return container_of(evsel, struct evsel, core);
257 }
258 
259 static inline int evlist__nr_groups(struct evlist *evlist)
260 {
261 	return perf_evlist__nr_groups(&evlist->core);
262 }
263 
264 int evlist__strerror_open(struct evlist *evlist, int err, char *buf, size_t size);
265 int evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size);
266 
267 bool evlist__can_select_event(struct evlist *evlist, const char *str);
268 void evlist__to_front(struct evlist *evlist, struct evsel *move_evsel);
269 
270 /**
271  * __evlist__for_each_entry - iterate thru all the evsels
272  * @list: list_head instance to iterate
273  * @evsel: struct evsel iterator
274  */
275 #define __evlist__for_each_entry(list, evsel) \
276         list_for_each_entry(evsel, list, core.node)
277 
278 /**
279  * evlist__for_each_entry - iterate thru all the evsels
280  * @evlist: evlist instance to iterate
281  * @evsel: struct evsel iterator
282  */
283 #define evlist__for_each_entry(evlist, evsel) \
284 	__evlist__for_each_entry(&(evlist)->core.entries, evsel)
285 
286 /**
287  * __evlist__for_each_entry_continue - continue iteration thru all the evsels
288  * @list: list_head instance to iterate
289  * @evsel: struct evsel iterator
290  */
291 #define __evlist__for_each_entry_continue(list, evsel) \
292         list_for_each_entry_continue(evsel, list, core.node)
293 
294 /**
295  * evlist__for_each_entry_continue - continue iteration thru all the evsels
296  * @evlist: evlist instance to iterate
297  * @evsel: struct evsel iterator
298  */
299 #define evlist__for_each_entry_continue(evlist, evsel) \
300 	__evlist__for_each_entry_continue(&(evlist)->core.entries, evsel)
301 
302 /**
303  * __evlist__for_each_entry_from - continue iteration from @evsel (included)
304  * @list: list_head instance to iterate
305  * @evsel: struct evsel iterator
306  */
307 #define __evlist__for_each_entry_from(list, evsel) \
308 	list_for_each_entry_from(evsel, list, core.node)
309 
310 /**
311  * evlist__for_each_entry_from - continue iteration from @evsel (included)
312  * @evlist: evlist instance to iterate
313  * @evsel: struct evsel iterator
314  */
315 #define evlist__for_each_entry_from(evlist, evsel) \
316 	__evlist__for_each_entry_from(&(evlist)->core.entries, evsel)
317 
318 /**
319  * __evlist__for_each_entry_reverse - iterate thru all the evsels in reverse order
320  * @list: list_head instance to iterate
321  * @evsel: struct evsel iterator
322  */
323 #define __evlist__for_each_entry_reverse(list, evsel) \
324         list_for_each_entry_reverse(evsel, list, core.node)
325 
326 /**
327  * evlist__for_each_entry_reverse - iterate thru all the evsels in reverse order
328  * @evlist: evlist instance to iterate
329  * @evsel: struct evsel iterator
330  */
331 #define evlist__for_each_entry_reverse(evlist, evsel) \
332 	__evlist__for_each_entry_reverse(&(evlist)->core.entries, evsel)
333 
334 /**
335  * __evlist__for_each_entry_safe - safely iterate thru all the evsels
336  * @list: list_head instance to iterate
337  * @tmp: struct evsel temp iterator
338  * @evsel: struct evsel iterator
339  */
340 #define __evlist__for_each_entry_safe(list, tmp, evsel) \
341         list_for_each_entry_safe(evsel, tmp, list, core.node)
342 
343 /**
344  * evlist__for_each_entry_safe - safely iterate thru all the evsels
345  * @evlist: evlist instance to iterate
346  * @evsel: struct evsel iterator
347  * @tmp: struct evsel temp iterator
348  */
349 #define evlist__for_each_entry_safe(evlist, tmp, evsel) \
350 	__evlist__for_each_entry_safe(&(evlist)->core.entries, tmp, evsel)
351 
352 /** Iterator state for evlist__for_each_cpu */
353 struct evlist_cpu_iterator {
354 	/** The list being iterated through. */
355 	struct evlist *container;
356 	/** The current evsel of the iterator. */
357 	struct evsel *evsel;
358 	/** The CPU map index corresponding to the evsel->core.cpus for the current CPU. */
359 	int cpu_map_idx;
360 	/**
361 	 * The CPU map index corresponding to evlist->core.all_cpus for the
362 	 * current CPU.  Distinct from cpu_map_idx as the evsel's cpu map may
363 	 * contain fewer entries.
364 	 */
365 	int evlist_cpu_map_idx;
366 	/** The number of CPU map entries in evlist->core.all_cpus. */
367 	int evlist_cpu_map_nr;
368 	/** The current CPU of the iterator. */
369 	struct perf_cpu cpu;
370 	/** If present, used to set the affinity when switching between CPUs. */
371 	struct affinity *affinity;
372 };
373 
374 /**
375  * evlist__for_each_cpu - without affinity, iterate over the evlist. With
376  *                        affinity, iterate over all CPUs and then the evlist
377  *                        for each evsel on that CPU. When switching between
378  *                        CPUs the affinity is set to the CPU to avoid IPIs
379  *                        during syscalls.
380  * @evlist_cpu_itr: the iterator instance.
381  * @evlist: evlist instance to iterate.
382  * @affinity: NULL or used to set the affinity to the current CPU.
383  */
384 #define evlist__for_each_cpu(evlist_cpu_itr, evlist, affinity)		\
385 	for ((evlist_cpu_itr) = evlist__cpu_begin(evlist, affinity);	\
386 	     !evlist_cpu_iterator__end(&evlist_cpu_itr);		\
387 	     evlist_cpu_iterator__next(&evlist_cpu_itr))
388 
389 /** Returns an iterator set to the first CPU/evsel of evlist. */
390 struct evlist_cpu_iterator evlist__cpu_begin(struct evlist *evlist, struct affinity *affinity);
391 /** Move to next element in iterator, updating CPU, evsel and the affinity. */
392 void evlist_cpu_iterator__next(struct evlist_cpu_iterator *evlist_cpu_itr);
393 /** Returns true when iterator is at the end of the CPUs and evlist. */
394 bool evlist_cpu_iterator__end(const struct evlist_cpu_iterator *evlist_cpu_itr);
395 
396 struct evsel *evlist__get_tracking_event(struct evlist *evlist);
397 void evlist__set_tracking_event(struct evlist *evlist, struct evsel *tracking_evsel);
398 
399 struct evsel *evlist__find_evsel_by_str(struct evlist *evlist, const char *str);
400 
401 struct evsel *evlist__event2evsel(struct evlist *evlist, union perf_event *event);
402 
403 bool evlist__exclude_kernel(struct evlist *evlist);
404 
405 void evlist__force_leader(struct evlist *evlist);
406 
407 struct evsel *evlist__reset_weak_group(struct evlist *evlist, struct evsel *evsel, bool close);
408 
409 #define EVLIST_CTL_CMD_ENABLE_TAG  "enable"
410 #define EVLIST_CTL_CMD_DISABLE_TAG "disable"
411 #define EVLIST_CTL_CMD_ACK_TAG     "ack\n"
412 #define EVLIST_CTL_CMD_SNAPSHOT_TAG "snapshot"
413 #define EVLIST_CTL_CMD_EVLIST_TAG "evlist"
414 #define EVLIST_CTL_CMD_STOP_TAG "stop"
415 #define EVLIST_CTL_CMD_PING_TAG "ping"
416 
417 #define EVLIST_CTL_CMD_MAX_LEN 64
418 
419 enum evlist_ctl_cmd {
420 	EVLIST_CTL_CMD_UNSUPPORTED = 0,
421 	EVLIST_CTL_CMD_ENABLE,
422 	EVLIST_CTL_CMD_DISABLE,
423 	EVLIST_CTL_CMD_ACK,
424 	EVLIST_CTL_CMD_SNAPSHOT,
425 	EVLIST_CTL_CMD_EVLIST,
426 	EVLIST_CTL_CMD_STOP,
427 	EVLIST_CTL_CMD_PING,
428 };
429 
430 int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close);
431 void evlist__close_control(int ctl_fd, int ctl_fd_ack, bool *ctl_fd_close);
432 int evlist__initialize_ctlfd(struct evlist *evlist, int ctl_fd, int ctl_fd_ack);
433 int evlist__finalize_ctlfd(struct evlist *evlist);
434 bool evlist__ctlfd_initialized(struct evlist *evlist);
435 int evlist__ctlfd_process(struct evlist *evlist, enum evlist_ctl_cmd *cmd);
436 int evlist__ctlfd_ack(struct evlist *evlist);
437 
438 #define EVLIST_ENABLED_MSG "Events enabled\n"
439 #define EVLIST_DISABLED_MSG "Events disabled\n"
440 
441 int evlist__parse_event_enable_time(struct evlist *evlist, struct record_opts *opts,
442 				    const char *str, int unset);
443 int event_enable_timer__start(struct event_enable_timer *eet);
444 void event_enable_timer__exit(struct event_enable_timer **ep);
445 int event_enable_timer__process(struct event_enable_timer *eet);
446 
447 struct evsel *evlist__find_evsel(struct evlist *evlist, int idx);
448 
449 int evlist__scnprintf_evsels(struct evlist *evlist, size_t size, char *bf);
450 void evlist__check_mem_load_aux(struct evlist *evlist);
451 #endif /* __PERF_EVLIST_H */
452