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