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