• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 struct target;
24 
25 /*
26  * State machine of bkw_mmap_state:
27  *
28  *                     .________________(forbid)_____________.
29  *                     |                                     V
30  * NOTREADY --(0)--> RUNNING --(1)--> DATA_PENDING --(2)--> EMPTY
31  *                     ^  ^              |   ^               |
32  *                     |  |__(forbid)____/   |___(forbid)___/|
33  *                     |                                     |
34  *                      \_________________(3)_______________/
35  *
36  * NOTREADY     : Backward ring buffers are not ready
37  * RUNNING      : Backward ring buffers are recording
38  * DATA_PENDING : We are required to collect data from backward ring buffers
39  * EMPTY        : We have collected data from backward ring buffers.
40  *
41  * (0): Setup backward ring buffer
42  * (1): Pause ring buffers for reading
43  * (2): Read from ring buffers
44  * (3): Resume ring buffers for recording
45  */
46 enum bkw_mmap_state {
47 	BKW_MMAP_NOTREADY,
48 	BKW_MMAP_RUNNING,
49 	BKW_MMAP_DATA_PENDING,
50 	BKW_MMAP_EMPTY,
51 };
52 
53 struct event_enable_timer;
54 
55 struct evlist {
56 	struct perf_evlist core;
57 	bool		 enabled;
58 	int		 id_pos;
59 	int		 is_pos;
60 	int		 nr_br_cntr;
61 	u64		 combined_sample_type;
62 	enum bkw_mmap_state bkw_mmap_state;
63 	struct {
64 		int	cork_fd;
65 		pid_t	pid;
66 	} workload;
67 	struct mmap *mmap;
68 	struct mmap *overwrite_mmap;
69 	struct evsel *selected;
70 	struct events_stats stats;
71 	struct perf_env	*env;
72 	void (*trace_event_sample_raw)(struct evlist *evlist,
73 				       union perf_event *event,
74 				       struct perf_sample *sample);
75 	u64		first_sample_time;
76 	u64		last_sample_time;
77 	struct {
78 		pthread_t		th;
79 		volatile int		done;
80 	} thread;
81 	struct {
82 		int	fd;	/* control file descriptor */
83 		int	ack;	/* ack file descriptor for control commands */
84 		int	pos;	/* index at evlist core object to check signals */
85 	} ctl_fd;
86 	struct event_enable_timer *eet;
87 };
88 
89 struct evsel_str_handler {
90 	const char *name;
91 	void	   *handler;
92 };
93 
94 struct evlist *evlist__new(void);
95 struct evlist *evlist__new_default(void);
96 struct evlist *evlist__new_dummy(void);
97 void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus,
98 		  struct perf_thread_map *threads);
99 void evlist__exit(struct evlist *evlist);
100 void evlist__delete(struct evlist *evlist);
101 
102 void evlist__add(struct evlist *evlist, struct evsel *entry);
103 void evlist__remove(struct evlist *evlist, struct evsel *evsel);
104 
105 int evlist__add_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs);
106 
107 int __evlist__add_default_attrs(struct evlist *evlist,
108 				     struct perf_event_attr *attrs, size_t nr_attrs);
109 
110 int arch_evlist__add_default_attrs(struct evlist *evlist,
111 				   struct perf_event_attr *attrs,
112 				   size_t nr_attrs);
113 
114 #define evlist__add_default_attrs(evlist, array) \
115 	arch_evlist__add_default_attrs(evlist, array, ARRAY_SIZE(array))
116 
117 int arch_evlist__cmp(const struct evsel *lhs, const struct evsel *rhs);
118 
119 int evlist__add_dummy(struct evlist *evlist);
120 struct evsel *evlist__add_aux_dummy(struct evlist *evlist, bool system_wide);
evlist__add_dummy_on_all_cpus(struct evlist * evlist)121 static inline struct evsel *evlist__add_dummy_on_all_cpus(struct evlist *evlist)
122 {
123 	return evlist__add_aux_dummy(evlist, true);
124 }
125 #ifdef HAVE_LIBTRACEEVENT
126 struct evsel *evlist__add_sched_switch(struct evlist *evlist, bool system_wide);
127 #endif
128 
129 int evlist__add_sb_event(struct evlist *evlist, struct perf_event_attr *attr,
130 			 evsel__sb_cb_t cb, void *data);
131 void evlist__set_cb(struct evlist *evlist, evsel__sb_cb_t cb, void *data);
132 int evlist__start_sb_thread(struct evlist *evlist, struct target *target);
133 void evlist__stop_sb_thread(struct evlist *evlist);
134 
135 #ifdef HAVE_LIBTRACEEVENT
136 int evlist__add_newtp(struct evlist *evlist, const char *sys, const char *name, void *handler);
137 #endif
138 
139 int __evlist__set_tracepoints_handlers(struct evlist *evlist,
140 				       const struct evsel_str_handler *assocs,
141 				       size_t nr_assocs);
142 
143 #define evlist__set_tracepoints_handlers(evlist, array) \
144 	__evlist__set_tracepoints_handlers(evlist, array, ARRAY_SIZE(array))
145 
146 int evlist__set_tp_filter(struct evlist *evlist, const char *filter);
147 int evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid);
148 int evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids);
149 
150 int evlist__append_tp_filter(struct evlist *evlist, const char *filter);
151 
152 int evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid);
153 int evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids);
154 
155 struct evsel *evlist__find_tracepoint_by_id(struct evlist *evlist, int id);
156 struct evsel *evlist__find_tracepoint_by_name(struct evlist *evlist, const char *name);
157 
158 int evlist__add_pollfd(struct evlist *evlist, int fd);
159 int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask);
160 
161 #ifdef HAVE_EVENTFD_SUPPORT
162 int evlist__add_wakeup_eventfd(struct evlist *evlist, int fd);
163 #endif
164 
165 int evlist__poll(struct evlist *evlist, int timeout);
166 
167 struct evsel *evlist__id2evsel(struct evlist *evlist, u64 id);
168 struct evsel *evlist__id2evsel_strict(struct evlist *evlist, u64 id);
169 
170 struct perf_sample_id *evlist__id2sid(struct evlist *evlist, u64 id);
171 
172 void evlist__toggle_bkw_mmap(struct evlist *evlist, enum bkw_mmap_state state);
173 
174 void evlist__mmap_consume(struct evlist *evlist, int idx);
175 
176 int evlist__open(struct evlist *evlist);
177 void evlist__close(struct evlist *evlist);
178 
179 struct callchain_param;
180 
181 void evlist__set_id_pos(struct evlist *evlist);
182 void evlist__config(struct evlist *evlist, struct record_opts *opts, struct callchain_param *callchain);
183 int record_opts__config(struct record_opts *opts);
184 
185 int evlist__prepare_workload(struct evlist *evlist, struct target *target,
186 			     const char *argv[], bool pipe_output,
187 			     void (*exec_error)(int signo, siginfo_t *info, void *ucontext));
188 int evlist__start_workload(struct evlist *evlist);
189 void evlist__cancel_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 			  struct target *target);
220 
221 u64 __evlist__combined_sample_type(struct evlist *evlist);
222 u64 evlist__combined_sample_type(struct evlist *evlist);
223 u64 evlist__combined_branch_type(struct evlist *evlist);
224 void evlist__update_br_cntr(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 
evlist__empty(struct evlist * evlist)237 static inline bool evlist__empty(struct evlist *evlist)
238 {
239 	return list_empty(&evlist->core.entries);
240 }
241 
evlist__first(struct evlist * evlist)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 
evlist__last(struct evlist * evlist)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 
evlist__nr_groups(struct evlist * evlist)256 static inline int evlist__nr_groups(struct evlist *evlist)
257 {
258 	return perf_evlist__nr_groups(&evlist->core);
259 }
260 
261 int evlist__strerror_open(struct evlist *evlist, int err, char *buf, size_t size);
262 int evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size);
263 
264 bool evlist__can_select_event(struct evlist *evlist, const char *str);
265 void evlist__to_front(struct evlist *evlist, struct evsel *move_evsel);
266 
267 /**
268  * __evlist__for_each_entry - iterate thru all the evsels
269  * @list: list_head instance to iterate
270  * @evsel: struct evsel iterator
271  */
272 #define __evlist__for_each_entry(list, evsel) \
273         list_for_each_entry(evsel, list, core.node)
274 
275 /**
276  * evlist__for_each_entry - iterate thru all the evsels
277  * @evlist: evlist instance to iterate
278  * @evsel: struct evsel iterator
279  */
280 #define evlist__for_each_entry(evlist, evsel) \
281 	__evlist__for_each_entry(&(evlist)->core.entries, evsel)
282 
283 /**
284  * __evlist__for_each_entry_continue - continue iteration thru all the evsels
285  * @list: list_head instance to iterate
286  * @evsel: struct evsel iterator
287  */
288 #define __evlist__for_each_entry_continue(list, evsel) \
289         list_for_each_entry_continue(evsel, list, core.node)
290 
291 /**
292  * evlist__for_each_entry_continue - continue iteration thru all the evsels
293  * @evlist: evlist instance to iterate
294  * @evsel: struct evsel iterator
295  */
296 #define evlist__for_each_entry_continue(evlist, evsel) \
297 	__evlist__for_each_entry_continue(&(evlist)->core.entries, evsel)
298 
299 /**
300  * __evlist__for_each_entry_from - continue iteration from @evsel (included)
301  * @list: list_head instance to iterate
302  * @evsel: struct evsel iterator
303  */
304 #define __evlist__for_each_entry_from(list, evsel) \
305 	list_for_each_entry_from(evsel, list, core.node)
306 
307 /**
308  * evlist__for_each_entry_from - continue iteration from @evsel (included)
309  * @evlist: evlist instance to iterate
310  * @evsel: struct evsel iterator
311  */
312 #define evlist__for_each_entry_from(evlist, evsel) \
313 	__evlist__for_each_entry_from(&(evlist)->core.entries, evsel)
314 
315 /**
316  * __evlist__for_each_entry_reverse - iterate thru all the evsels in reverse order
317  * @list: list_head instance to iterate
318  * @evsel: struct evsel iterator
319  */
320 #define __evlist__for_each_entry_reverse(list, evsel) \
321         list_for_each_entry_reverse(evsel, list, core.node)
322 
323 /**
324  * evlist__for_each_entry_reverse - iterate thru all the evsels in reverse order
325  * @evlist: evlist instance to iterate
326  * @evsel: struct evsel iterator
327  */
328 #define evlist__for_each_entry_reverse(evlist, evsel) \
329 	__evlist__for_each_entry_reverse(&(evlist)->core.entries, evsel)
330 
331 /**
332  * __evlist__for_each_entry_safe - safely iterate thru all the evsels
333  * @list: list_head instance to iterate
334  * @tmp: struct evsel temp iterator
335  * @evsel: struct evsel iterator
336  */
337 #define __evlist__for_each_entry_safe(list, tmp, evsel) \
338         list_for_each_entry_safe(evsel, tmp, list, core.node)
339 
340 /**
341  * evlist__for_each_entry_safe - safely iterate thru all the evsels
342  * @evlist: evlist instance to iterate
343  * @evsel: struct evsel iterator
344  * @tmp: struct evsel temp iterator
345  */
346 #define evlist__for_each_entry_safe(evlist, tmp, evsel) \
347 	__evlist__for_each_entry_safe(&(evlist)->core.entries, tmp, evsel)
348 
349 /** Iterator state for evlist__for_each_cpu */
350 struct evlist_cpu_iterator {
351 	/** The list being iterated through. */
352 	struct evlist *container;
353 	/** The current evsel of the iterator. */
354 	struct evsel *evsel;
355 	/** The CPU map index corresponding to the evsel->core.cpus for the current CPU. */
356 	int cpu_map_idx;
357 	/**
358 	 * The CPU map index corresponding to evlist->core.all_cpus for the
359 	 * current CPU.  Distinct from cpu_map_idx as the evsel's cpu map may
360 	 * contain fewer entries.
361 	 */
362 	int evlist_cpu_map_idx;
363 	/** The number of CPU map entries in evlist->core.all_cpus. */
364 	int evlist_cpu_map_nr;
365 	/** The current CPU of the iterator. */
366 	struct perf_cpu cpu;
367 	/** If present, used to set the affinity when switching between CPUs. */
368 	struct affinity *affinity;
369 };
370 
371 /**
372  * evlist__for_each_cpu - without affinity, iterate over the evlist. With
373  *                        affinity, iterate over all CPUs and then the evlist
374  *                        for each evsel on that CPU. When switching between
375  *                        CPUs the affinity is set to the CPU to avoid IPIs
376  *                        during syscalls.
377  * @evlist_cpu_itr: the iterator instance.
378  * @evlist: evlist instance to iterate.
379  * @affinity: NULL or used to set the affinity to the current CPU.
380  */
381 #define evlist__for_each_cpu(evlist_cpu_itr, evlist, affinity)		\
382 	for ((evlist_cpu_itr) = evlist__cpu_begin(evlist, affinity);	\
383 	     !evlist_cpu_iterator__end(&evlist_cpu_itr);		\
384 	     evlist_cpu_iterator__next(&evlist_cpu_itr))
385 
386 /** Returns an iterator set to the first CPU/evsel of evlist. */
387 struct evlist_cpu_iterator evlist__cpu_begin(struct evlist *evlist, struct affinity *affinity);
388 /** Move to next element in iterator, updating CPU, evsel and the affinity. */
389 void evlist_cpu_iterator__next(struct evlist_cpu_iterator *evlist_cpu_itr);
390 /** Returns true when iterator is at the end of the CPUs and evlist. */
391 bool evlist_cpu_iterator__end(const struct evlist_cpu_iterator *evlist_cpu_itr);
392 
393 struct evsel *evlist__get_tracking_event(struct evlist *evlist);
394 void evlist__set_tracking_event(struct evlist *evlist, struct evsel *tracking_evsel);
395 struct evsel *evlist__findnew_tracking_event(struct evlist *evlist, bool system_wide);
396 
397 struct evsel *evlist__find_evsel_by_str(struct evlist *evlist, const char *str);
398 
399 struct evsel *evlist__event2evsel(struct evlist *evlist, union perf_event *event);
400 
401 bool evlist__exclude_kernel(struct evlist *evlist);
402 
403 void evlist__force_leader(struct evlist *evlist);
404 
405 struct evsel *evlist__reset_weak_group(struct evlist *evlist, struct evsel *evsel, bool close);
406 
407 #define EVLIST_CTL_CMD_ENABLE_TAG  "enable"
408 #define EVLIST_CTL_CMD_DISABLE_TAG "disable"
409 #define EVLIST_CTL_CMD_ACK_TAG     "ack\n"
410 #define EVLIST_CTL_CMD_SNAPSHOT_TAG "snapshot"
411 #define EVLIST_CTL_CMD_EVLIST_TAG "evlist"
412 #define EVLIST_CTL_CMD_STOP_TAG "stop"
413 #define EVLIST_CTL_CMD_PING_TAG "ping"
414 
415 #define EVLIST_CTL_CMD_MAX_LEN 64
416 
417 enum evlist_ctl_cmd {
418 	EVLIST_CTL_CMD_UNSUPPORTED = 0,
419 	EVLIST_CTL_CMD_ENABLE,
420 	EVLIST_CTL_CMD_DISABLE,
421 	EVLIST_CTL_CMD_ACK,
422 	EVLIST_CTL_CMD_SNAPSHOT,
423 	EVLIST_CTL_CMD_EVLIST,
424 	EVLIST_CTL_CMD_STOP,
425 	EVLIST_CTL_CMD_PING,
426 };
427 
428 int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close);
429 void evlist__close_control(int ctl_fd, int ctl_fd_ack, bool *ctl_fd_close);
430 int evlist__initialize_ctlfd(struct evlist *evlist, int ctl_fd, int ctl_fd_ack);
431 int evlist__finalize_ctlfd(struct evlist *evlist);
432 bool evlist__ctlfd_initialized(struct evlist *evlist);
433 int evlist__ctlfd_process(struct evlist *evlist, enum evlist_ctl_cmd *cmd);
434 int evlist__ctlfd_ack(struct evlist *evlist);
435 
436 #define EVLIST_ENABLED_MSG "Events enabled\n"
437 #define EVLIST_DISABLED_MSG "Events disabled\n"
438 
439 int evlist__parse_event_enable_time(struct evlist *evlist, struct record_opts *opts,
440 				    const char *str, int unset);
441 int event_enable_timer__start(struct event_enable_timer *eet);
442 void event_enable_timer__exit(struct event_enable_timer **ep);
443 int event_enable_timer__process(struct event_enable_timer *eet);
444 
445 struct evsel *evlist__find_evsel(struct evlist *evlist, int idx);
446 
447 int evlist__scnprintf_evsels(struct evlist *evlist, size_t size, char *bf);
448 void evlist__check_mem_load_aux(struct evlist *evlist);
449 void evlist__warn_user_requested_cpus(struct evlist *evlist, const char *cpu_list);
450 void evlist__uniquify_name(struct evlist *evlist);
451 bool evlist__has_bpf_output(struct evlist *evlist);
452 
453 #endif /* __PERF_EVLIST_H */
454