xref: /linux/tools/perf/util/evlist.h (revision 8632987380765dee716d460640aa58d58d52998e)
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 evlist {
52 	struct perf_evlist core;
53 	bool		 enabled;
54 	int		 id_pos;
55 	int		 is_pos;
56 	u64		 combined_sample_type;
57 	enum bkw_mmap_state bkw_mmap_state;
58 	struct {
59 		int	cork_fd;
60 		pid_t	pid;
61 	} workload;
62 	struct mmap *mmap;
63 	struct mmap *overwrite_mmap;
64 	struct evsel *selected;
65 	struct events_stats stats;
66 	struct perf_env	*env;
67 	void (*trace_event_sample_raw)(struct evlist *evlist,
68 				       union perf_event *event,
69 				       struct perf_sample *sample);
70 	u64		first_sample_time;
71 	u64		last_sample_time;
72 	struct {
73 		pthread_t		th;
74 		volatile int		done;
75 	} thread;
76 	struct {
77 		int	fd;	/* control file descriptor */
78 		int	ack;	/* ack file descriptor for control commands */
79 		int	pos;	/* index at evlist core object to check signals */
80 	} ctl_fd;
81 };
82 
83 struct evsel_str_handler {
84 	const char *name;
85 	void	   *handler;
86 };
87 
88 struct evlist *evlist__new(void);
89 struct evlist *evlist__new_default(void);
90 struct evlist *evlist__new_dummy(void);
91 void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus,
92 		  struct perf_thread_map *threads);
93 void evlist__exit(struct evlist *evlist);
94 void evlist__delete(struct evlist *evlist);
95 
96 void evlist__add(struct evlist *evlist, struct evsel *entry);
97 void evlist__remove(struct evlist *evlist, struct evsel *evsel);
98 
99 int __evlist__add_default(struct evlist *evlist, bool precise);
100 
101 static inline int evlist__add_default(struct evlist *evlist)
102 {
103 	return __evlist__add_default(evlist, true);
104 }
105 
106 int __evlist__add_default_attrs(struct evlist *evlist,
107 				     struct perf_event_attr *attrs, size_t nr_attrs);
108 
109 #define evlist__add_default_attrs(evlist, array) \
110 	__evlist__add_default_attrs(evlist, array, ARRAY_SIZE(array))
111 
112 int arch_evlist__add_default_attrs(struct evlist *evlist);
113 
114 int evlist__add_dummy(struct evlist *evlist);
115 
116 int evlist__add_sb_event(struct evlist *evlist, struct perf_event_attr *attr,
117 			 evsel__sb_cb_t cb, void *data);
118 void evlist__set_cb(struct evlist *evlist, evsel__sb_cb_t cb, void *data);
119 int evlist__start_sb_thread(struct evlist *evlist, struct target *target);
120 void evlist__stop_sb_thread(struct evlist *evlist);
121 
122 int evlist__add_newtp(struct evlist *evlist, const char *sys, const char *name, void *handler);
123 
124 int __evlist__set_tracepoints_handlers(struct evlist *evlist,
125 				       const struct evsel_str_handler *assocs,
126 				       size_t nr_assocs);
127 
128 #define evlist__set_tracepoints_handlers(evlist, array) \
129 	__evlist__set_tracepoints_handlers(evlist, array, ARRAY_SIZE(array))
130 
131 int evlist__set_tp_filter(struct evlist *evlist, const char *filter);
132 int evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid);
133 int evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids);
134 
135 int evlist__append_tp_filter(struct evlist *evlist, const char *filter);
136 
137 int evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid);
138 int evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids);
139 
140 struct evsel *evlist__find_tracepoint_by_id(struct evlist *evlist, int id);
141 struct evsel *evlist__find_tracepoint_by_name(struct evlist *evlist, const char *name);
142 
143 int evlist__add_pollfd(struct evlist *evlist, int fd);
144 int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask);
145 
146 #ifdef HAVE_EVENTFD_SUPPORT
147 int evlist__add_wakeup_eventfd(struct evlist *evlist, int fd);
148 #endif
149 
150 int evlist__poll(struct evlist *evlist, int timeout);
151 
152 struct evsel *evlist__id2evsel(struct evlist *evlist, u64 id);
153 struct evsel *evlist__id2evsel_strict(struct evlist *evlist, u64 id);
154 
155 struct perf_sample_id *evlist__id2sid(struct evlist *evlist, u64 id);
156 
157 void evlist__toggle_bkw_mmap(struct evlist *evlist, enum bkw_mmap_state state);
158 
159 void evlist__mmap_consume(struct evlist *evlist, int idx);
160 
161 int evlist__open(struct evlist *evlist);
162 void evlist__close(struct evlist *evlist);
163 
164 struct callchain_param;
165 
166 void evlist__set_id_pos(struct evlist *evlist);
167 void evlist__config(struct evlist *evlist, struct record_opts *opts, struct callchain_param *callchain);
168 int record_opts__config(struct record_opts *opts);
169 
170 int evlist__prepare_workload(struct evlist *evlist, struct target *target,
171 			     const char *argv[], bool pipe_output,
172 			     void (*exec_error)(int signo, siginfo_t *info, void *ucontext));
173 int evlist__start_workload(struct evlist *evlist);
174 
175 struct option;
176 
177 int __evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str);
178 int evlist__parse_mmap_pages(const struct option *opt, const char *str, int unset);
179 
180 unsigned long perf_event_mlock_kb_in_pages(void);
181 
182 int evlist__mmap_ex(struct evlist *evlist, unsigned int pages,
183 			 unsigned int auxtrace_pages,
184 			 bool auxtrace_overwrite, int nr_cblocks,
185 			 int affinity, int flush, int comp_level);
186 int evlist__mmap(struct evlist *evlist, unsigned int pages);
187 void evlist__munmap(struct evlist *evlist);
188 
189 size_t evlist__mmap_size(unsigned long pages);
190 
191 void evlist__disable(struct evlist *evlist);
192 void evlist__enable(struct evlist *evlist);
193 void evlist__toggle_enable(struct evlist *evlist);
194 void evlist__disable_evsel(struct evlist *evlist, char *evsel_name);
195 void evlist__enable_evsel(struct evlist *evlist, char *evsel_name);
196 
197 int evlist__enable_event_idx(struct evlist *evlist, struct evsel *evsel, int idx);
198 
199 void evlist__set_selected(struct evlist *evlist, struct evsel *evsel);
200 
201 int evlist__create_maps(struct evlist *evlist, struct target *target);
202 int evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel);
203 
204 void evlist__set_leader(struct evlist *evlist);
205 
206 u64 __evlist__combined_sample_type(struct evlist *evlist);
207 u64 evlist__combined_sample_type(struct evlist *evlist);
208 u64 evlist__combined_branch_type(struct evlist *evlist);
209 bool evlist__sample_id_all(struct evlist *evlist);
210 u16 evlist__id_hdr_size(struct evlist *evlist);
211 
212 int evlist__parse_sample(struct evlist *evlist, union perf_event *event, struct perf_sample *sample);
213 int evlist__parse_sample_timestamp(struct evlist *evlist, union perf_event *event, u64 *timestamp);
214 
215 bool evlist__valid_sample_type(struct evlist *evlist);
216 bool evlist__valid_sample_id_all(struct evlist *evlist);
217 bool evlist__valid_read_format(struct evlist *evlist);
218 
219 void evlist__splice_list_tail(struct evlist *evlist, struct list_head *list);
220 
221 static inline bool evlist__empty(struct evlist *evlist)
222 {
223 	return list_empty(&evlist->core.entries);
224 }
225 
226 static inline struct evsel *evlist__first(struct evlist *evlist)
227 {
228 	struct perf_evsel *evsel = perf_evlist__first(&evlist->core);
229 
230 	return container_of(evsel, struct evsel, core);
231 }
232 
233 static inline struct evsel *evlist__last(struct evlist *evlist)
234 {
235 	struct perf_evsel *evsel = perf_evlist__last(&evlist->core);
236 
237 	return container_of(evsel, struct evsel, core);
238 }
239 
240 int evlist__strerror_open(struct evlist *evlist, int err, char *buf, size_t size);
241 int evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size);
242 
243 bool evlist__can_select_event(struct evlist *evlist, const char *str);
244 void evlist__to_front(struct evlist *evlist, struct evsel *move_evsel);
245 
246 /**
247  * __evlist__for_each_entry - iterate thru all the evsels
248  * @list: list_head instance to iterate
249  * @evsel: struct evsel iterator
250  */
251 #define __evlist__for_each_entry(list, evsel) \
252         list_for_each_entry(evsel, list, core.node)
253 
254 /**
255  * evlist__for_each_entry - iterate thru all the evsels
256  * @evlist: evlist instance to iterate
257  * @evsel: struct evsel iterator
258  */
259 #define evlist__for_each_entry(evlist, evsel) \
260 	__evlist__for_each_entry(&(evlist)->core.entries, evsel)
261 
262 /**
263  * __evlist__for_each_entry_continue - continue iteration thru all the evsels
264  * @list: list_head instance to iterate
265  * @evsel: struct evsel iterator
266  */
267 #define __evlist__for_each_entry_continue(list, evsel) \
268         list_for_each_entry_continue(evsel, list, core.node)
269 
270 /**
271  * evlist__for_each_entry_continue - continue iteration thru all the evsels
272  * @evlist: evlist instance to iterate
273  * @evsel: struct evsel iterator
274  */
275 #define evlist__for_each_entry_continue(evlist, evsel) \
276 	__evlist__for_each_entry_continue(&(evlist)->core.entries, evsel)
277 
278 /**
279  * __evlist__for_each_entry_from - continue iteration from @evsel (included)
280  * @list: list_head instance to iterate
281  * @evsel: struct evsel iterator
282  */
283 #define __evlist__for_each_entry_from(list, evsel) \
284 	list_for_each_entry_from(evsel, list, core.node)
285 
286 /**
287  * evlist__for_each_entry_from - continue iteration from @evsel (included)
288  * @evlist: evlist instance to iterate
289  * @evsel: struct evsel iterator
290  */
291 #define evlist__for_each_entry_from(evlist, evsel) \
292 	__evlist__for_each_entry_from(&(evlist)->core.entries, evsel)
293 
294 /**
295  * __evlist__for_each_entry_reverse - iterate thru all the evsels in reverse order
296  * @list: list_head instance to iterate
297  * @evsel: struct evsel iterator
298  */
299 #define __evlist__for_each_entry_reverse(list, evsel) \
300         list_for_each_entry_reverse(evsel, list, core.node)
301 
302 /**
303  * evlist__for_each_entry_reverse - iterate thru all the evsels in reverse order
304  * @evlist: evlist instance to iterate
305  * @evsel: struct evsel iterator
306  */
307 #define evlist__for_each_entry_reverse(evlist, evsel) \
308 	__evlist__for_each_entry_reverse(&(evlist)->core.entries, evsel)
309 
310 /**
311  * __evlist__for_each_entry_safe - safely iterate thru all the evsels
312  * @list: list_head instance to iterate
313  * @tmp: struct evsel temp iterator
314  * @evsel: struct evsel iterator
315  */
316 #define __evlist__for_each_entry_safe(list, tmp, evsel) \
317         list_for_each_entry_safe(evsel, tmp, list, core.node)
318 
319 /**
320  * evlist__for_each_entry_safe - safely iterate thru all the evsels
321  * @evlist: evlist instance to iterate
322  * @evsel: struct evsel iterator
323  * @tmp: struct evsel temp iterator
324  */
325 #define evlist__for_each_entry_safe(evlist, tmp, evsel) \
326 	__evlist__for_each_entry_safe(&(evlist)->core.entries, tmp, evsel)
327 
328 #define evlist__for_each_cpu(evlist, index, cpu)	\
329 	evlist__cpu_iter_start(evlist);			\
330 	perf_cpu_map__for_each_cpu (cpu, index, (evlist)->core.all_cpus)
331 
332 struct evsel *evlist__get_tracking_event(struct evlist *evlist);
333 void evlist__set_tracking_event(struct evlist *evlist, struct evsel *tracking_evsel);
334 
335 void evlist__cpu_iter_start(struct evlist *evlist);
336 bool evsel__cpu_iter_skip(struct evsel *ev, int cpu);
337 bool evsel__cpu_iter_skip_no_inc(struct evsel *ev, int cpu);
338 
339 struct evsel *evlist__find_evsel_by_str(struct evlist *evlist, const char *str);
340 
341 struct evsel *evlist__event2evsel(struct evlist *evlist, union perf_event *event);
342 
343 bool evlist__exclude_kernel(struct evlist *evlist);
344 
345 void evlist__force_leader(struct evlist *evlist);
346 
347 struct evsel *evlist__reset_weak_group(struct evlist *evlist, struct evsel *evsel, bool close);
348 
349 #define EVLIST_CTL_CMD_ENABLE_TAG  "enable"
350 #define EVLIST_CTL_CMD_DISABLE_TAG "disable"
351 #define EVLIST_CTL_CMD_ACK_TAG     "ack\n"
352 #define EVLIST_CTL_CMD_SNAPSHOT_TAG "snapshot"
353 #define EVLIST_CTL_CMD_EVLIST_TAG "evlist"
354 #define EVLIST_CTL_CMD_STOP_TAG "stop"
355 #define EVLIST_CTL_CMD_PING_TAG "ping"
356 
357 #define EVLIST_CTL_CMD_MAX_LEN 64
358 
359 enum evlist_ctl_cmd {
360 	EVLIST_CTL_CMD_UNSUPPORTED = 0,
361 	EVLIST_CTL_CMD_ENABLE,
362 	EVLIST_CTL_CMD_DISABLE,
363 	EVLIST_CTL_CMD_ACK,
364 	EVLIST_CTL_CMD_SNAPSHOT,
365 	EVLIST_CTL_CMD_EVLIST,
366 	EVLIST_CTL_CMD_STOP,
367 	EVLIST_CTL_CMD_PING,
368 };
369 
370 int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close);
371 void evlist__close_control(int ctl_fd, int ctl_fd_ack, bool *ctl_fd_close);
372 int evlist__initialize_ctlfd(struct evlist *evlist, int ctl_fd, int ctl_fd_ack);
373 int evlist__finalize_ctlfd(struct evlist *evlist);
374 bool evlist__ctlfd_initialized(struct evlist *evlist);
375 int evlist__ctlfd_process(struct evlist *evlist, enum evlist_ctl_cmd *cmd);
376 int evlist__ctlfd_ack(struct evlist *evlist);
377 
378 #define EVLIST_ENABLED_MSG "Events enabled\n"
379 #define EVLIST_DISABLED_MSG "Events disabled\n"
380 
381 struct evsel *evlist__find_evsel(struct evlist *evlist, int idx);
382 
383 int evlist__scnprintf_evsels(struct evlist *evlist, size_t size, char *bf);
384 void evlist__check_mem_load_aux(struct evlist *evlist);
385 #endif /* __PERF_EVLIST_H */
386