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