xref: /linux/tools/perf/util/evlist.h (revision e0fcfb086fbbb6233de1062d4b2f05e9afedab3b)
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 <stdio.h>
11 #include <internal/evlist.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 #define PERF_EVLIST__HLIST_BITS 8
52 #define PERF_EVLIST__HLIST_SIZE (1 << PERF_EVLIST__HLIST_BITS)
53 
54 struct evlist {
55 	struct perf_evlist core;
56 	struct hlist_head heads[PERF_EVLIST__HLIST_SIZE];
57 	int		 nr_groups;
58 	int		 nr_mmaps;
59 	bool		 enabled;
60 	size_t		 mmap_len;
61 	int		 id_pos;
62 	int		 is_pos;
63 	u64		 combined_sample_type;
64 	enum bkw_mmap_state bkw_mmap_state;
65 	struct {
66 		int	cork_fd;
67 		pid_t	pid;
68 	} workload;
69 	struct fdarray	 pollfd;
70 	struct mmap *mmap;
71 	struct mmap *overwrite_mmap;
72 	struct evsel *selected;
73 	struct events_stats stats;
74 	struct perf_env	*env;
75 	void (*trace_event_sample_raw)(struct evlist *evlist,
76 				       union perf_event *event,
77 				       struct perf_sample *sample);
78 	u64		first_sample_time;
79 	u64		last_sample_time;
80 	struct {
81 		pthread_t		th;
82 		volatile int		done;
83 	} thread;
84 };
85 
86 struct evsel_str_handler {
87 	const char *name;
88 	void	   *handler;
89 };
90 
91 struct evlist *evlist__new(void);
92 struct evlist *perf_evlist__new_default(void);
93 struct evlist *perf_evlist__new_dummy(void);
94 void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus,
95 		  struct perf_thread_map *threads);
96 void evlist__exit(struct evlist *evlist);
97 void evlist__delete(struct evlist *evlist);
98 
99 void evlist__add(struct evlist *evlist, struct evsel *entry);
100 void evlist__remove(struct evlist *evlist, struct evsel *evsel);
101 
102 int __perf_evlist__add_default(struct evlist *evlist, bool precise);
103 
104 static inline int perf_evlist__add_default(struct evlist *evlist)
105 {
106 	return __perf_evlist__add_default(evlist, true);
107 }
108 
109 int __perf_evlist__add_default_attrs(struct evlist *evlist,
110 				     struct perf_event_attr *attrs, size_t nr_attrs);
111 
112 #define perf_evlist__add_default_attrs(evlist, array) \
113 	__perf_evlist__add_default_attrs(evlist, array, ARRAY_SIZE(array))
114 
115 int perf_evlist__add_dummy(struct evlist *evlist);
116 
117 int perf_evlist__add_sb_event(struct evlist **evlist,
118 			      struct perf_event_attr *attr,
119 			      perf_evsel__sb_cb_t cb,
120 			      void *data);
121 int perf_evlist__start_sb_thread(struct evlist *evlist,
122 				 struct target *target);
123 void perf_evlist__stop_sb_thread(struct evlist *evlist);
124 
125 int perf_evlist__add_newtp(struct evlist *evlist,
126 			   const char *sys, const char *name, void *handler);
127 
128 void __perf_evlist__set_sample_bit(struct evlist *evlist,
129 				   enum perf_event_sample_format bit);
130 void __perf_evlist__reset_sample_bit(struct evlist *evlist,
131 				     enum perf_event_sample_format bit);
132 
133 #define perf_evlist__set_sample_bit(evlist, bit) \
134 	__perf_evlist__set_sample_bit(evlist, PERF_SAMPLE_##bit)
135 
136 #define perf_evlist__reset_sample_bit(evlist, bit) \
137 	__perf_evlist__reset_sample_bit(evlist, PERF_SAMPLE_##bit)
138 
139 int perf_evlist__set_tp_filter(struct evlist *evlist, const char *filter);
140 int perf_evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid);
141 int perf_evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids);
142 
143 struct evsel *
144 perf_evlist__find_tracepoint_by_id(struct evlist *evlist, int id);
145 
146 struct evsel *
147 perf_evlist__find_tracepoint_by_name(struct evlist *evlist,
148 				     const char *name);
149 
150 void perf_evlist__id_add(struct evlist *evlist, struct evsel *evsel,
151 			 int cpu, int thread, u64 id);
152 int perf_evlist__id_add_fd(struct evlist *evlist,
153 			   struct evsel *evsel,
154 			   int cpu, int thread, int fd);
155 
156 int perf_evlist__add_pollfd(struct evlist *evlist, int fd);
157 int perf_evlist__alloc_pollfd(struct evlist *evlist);
158 int perf_evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask);
159 
160 int perf_evlist__poll(struct evlist *evlist, int timeout);
161 
162 struct evsel *perf_evlist__id2evsel(struct evlist *evlist, u64 id);
163 struct evsel *perf_evlist__id2evsel_strict(struct evlist *evlist,
164 						u64 id);
165 
166 struct perf_sample_id *perf_evlist__id2sid(struct evlist *evlist, u64 id);
167 
168 void perf_evlist__toggle_bkw_mmap(struct evlist *evlist, enum bkw_mmap_state state);
169 
170 void evlist__mmap_consume(struct evlist *evlist, int idx);
171 
172 int evlist__open(struct evlist *evlist);
173 void evlist__close(struct evlist *evlist);
174 
175 struct callchain_param;
176 
177 void perf_evlist__set_id_pos(struct evlist *evlist);
178 bool perf_can_sample_identifier(void);
179 bool perf_can_record_switch_events(void);
180 bool perf_can_record_cpu_wide(void);
181 void perf_evlist__config(struct evlist *evlist, struct record_opts *opts,
182 			 struct callchain_param *callchain);
183 int record_opts__config(struct record_opts *opts);
184 
185 int perf_evlist__prepare_workload(struct evlist *evlist,
186 				  struct target *target,
187 				  const char *argv[], bool pipe_output,
188 				  void (*exec_error)(int signo, siginfo_t *info,
189 						     void *ucontext));
190 int perf_evlist__start_workload(struct evlist *evlist);
191 
192 struct option;
193 
194 int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str);
195 int perf_evlist__parse_mmap_pages(const struct option *opt,
196 				  const char *str,
197 				  int unset);
198 
199 unsigned long perf_event_mlock_kb_in_pages(void);
200 
201 int evlist__mmap_ex(struct evlist *evlist, unsigned int pages,
202 			 unsigned int auxtrace_pages,
203 			 bool auxtrace_overwrite, int nr_cblocks,
204 			 int affinity, int flush, int comp_level);
205 int evlist__mmap(struct evlist *evlist, unsigned int pages);
206 void evlist__munmap(struct evlist *evlist);
207 
208 size_t evlist__mmap_size(unsigned long pages);
209 
210 void evlist__disable(struct evlist *evlist);
211 void evlist__enable(struct evlist *evlist);
212 void perf_evlist__toggle_enable(struct evlist *evlist);
213 
214 int perf_evlist__enable_event_idx(struct evlist *evlist,
215 				  struct evsel *evsel, int idx);
216 
217 void perf_evlist__set_selected(struct evlist *evlist,
218 			       struct evsel *evsel);
219 
220 int perf_evlist__create_maps(struct evlist *evlist, struct target *target);
221 int perf_evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel);
222 
223 void __perf_evlist__set_leader(struct list_head *list);
224 void perf_evlist__set_leader(struct evlist *evlist);
225 
226 u64 perf_evlist__read_format(struct evlist *evlist);
227 u64 __perf_evlist__combined_sample_type(struct evlist *evlist);
228 u64 perf_evlist__combined_sample_type(struct evlist *evlist);
229 u64 perf_evlist__combined_branch_type(struct evlist *evlist);
230 bool perf_evlist__sample_id_all(struct evlist *evlist);
231 u16 perf_evlist__id_hdr_size(struct evlist *evlist);
232 
233 int perf_evlist__parse_sample(struct evlist *evlist, union perf_event *event,
234 			      struct perf_sample *sample);
235 
236 int perf_evlist__parse_sample_timestamp(struct evlist *evlist,
237 					union perf_event *event,
238 					u64 *timestamp);
239 
240 bool perf_evlist__valid_sample_type(struct evlist *evlist);
241 bool perf_evlist__valid_sample_id_all(struct evlist *evlist);
242 bool perf_evlist__valid_read_format(struct evlist *evlist);
243 
244 void perf_evlist__splice_list_tail(struct evlist *evlist,
245 				   struct list_head *list);
246 
247 static inline bool perf_evlist__empty(struct evlist *evlist)
248 {
249 	return list_empty(&evlist->core.entries);
250 }
251 
252 static inline struct evsel *perf_evlist__first(struct evlist *evlist)
253 {
254 	return list_entry(evlist->core.entries.next, struct evsel, core.node);
255 }
256 
257 static inline struct evsel *perf_evlist__last(struct evlist *evlist)
258 {
259 	return list_entry(evlist->core.entries.prev, struct evsel, core.node);
260 }
261 
262 size_t perf_evlist__fprintf(struct evlist *evlist, FILE *fp);
263 
264 int perf_evlist__strerror_open(struct evlist *evlist, int err, char *buf, size_t size);
265 int perf_evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size);
266 
267 bool perf_evlist__can_select_event(struct evlist *evlist, const char *str);
268 void perf_evlist__to_front(struct evlist *evlist,
269 			   struct evsel *move_evsel);
270 
271 /**
272  * __evlist__for_each_entry - iterate thru all the evsels
273  * @list: list_head instance to iterate
274  * @evsel: struct evsel iterator
275  */
276 #define __evlist__for_each_entry(list, evsel) \
277         list_for_each_entry(evsel, list, core.node)
278 
279 /**
280  * evlist__for_each_entry - iterate thru all the evsels
281  * @evlist: evlist instance to iterate
282  * @evsel: struct evsel iterator
283  */
284 #define evlist__for_each_entry(evlist, evsel) \
285 	__evlist__for_each_entry(&(evlist)->core.entries, evsel)
286 
287 /**
288  * __evlist__for_each_entry_continue - continue iteration thru all the evsels
289  * @list: list_head instance to iterate
290  * @evsel: struct evsel iterator
291  */
292 #define __evlist__for_each_entry_continue(list, evsel) \
293         list_for_each_entry_continue(evsel, list, core.node)
294 
295 /**
296  * evlist__for_each_entry_continue - continue iteration thru all the evsels
297  * @evlist: evlist instance to iterate
298  * @evsel: struct evsel iterator
299  */
300 #define evlist__for_each_entry_continue(evlist, evsel) \
301 	__evlist__for_each_entry_continue(&(evlist)->core.entries, evsel)
302 
303 /**
304  * __evlist__for_each_entry_reverse - iterate thru all the evsels in reverse order
305  * @list: list_head instance to iterate
306  * @evsel: struct evsel iterator
307  */
308 #define __evlist__for_each_entry_reverse(list, evsel) \
309         list_for_each_entry_reverse(evsel, list, core.node)
310 
311 /**
312  * evlist__for_each_entry_reverse - iterate thru all the evsels in reverse order
313  * @evlist: evlist instance to iterate
314  * @evsel: struct evsel iterator
315  */
316 #define evlist__for_each_entry_reverse(evlist, evsel) \
317 	__evlist__for_each_entry_reverse(&(evlist)->core.entries, evsel)
318 
319 /**
320  * __evlist__for_each_entry_safe - safely iterate thru all the evsels
321  * @list: list_head instance to iterate
322  * @tmp: struct evsel temp iterator
323  * @evsel: struct evsel iterator
324  */
325 #define __evlist__for_each_entry_safe(list, tmp, evsel) \
326         list_for_each_entry_safe(evsel, tmp, list, core.node)
327 
328 /**
329  * evlist__for_each_entry_safe - safely iterate thru all the evsels
330  * @evlist: evlist instance to iterate
331  * @evsel: struct evsel iterator
332  * @tmp: struct evsel temp iterator
333  */
334 #define evlist__for_each_entry_safe(evlist, tmp, evsel) \
335 	__evlist__for_each_entry_safe(&(evlist)->core.entries, tmp, evsel)
336 
337 void perf_evlist__set_tracking_event(struct evlist *evlist,
338 				     struct evsel *tracking_evsel);
339 
340 struct evsel *
341 perf_evlist__find_evsel_by_str(struct evlist *evlist, const char *str);
342 
343 struct evsel *perf_evlist__event2evsel(struct evlist *evlist,
344 					    union perf_event *event);
345 
346 bool perf_evlist__exclude_kernel(struct evlist *evlist);
347 
348 void perf_evlist__force_leader(struct evlist *evlist);
349 
350 struct evsel *perf_evlist__reset_weak_group(struct evlist *evlist,
351 						 struct evsel *evsel);
352 #endif /* __PERF_EVLIST_H */
353