xref: /openbmc/linux/tools/perf/util/evlist.h (revision 1c2dd16a)
1 #ifndef __PERF_EVLIST_H
2 #define __PERF_EVLIST_H 1
3 
4 #include <linux/kernel.h>
5 #include <linux/refcount.h>
6 #include <linux/list.h>
7 #include <api/fd/array.h>
8 #include <stdio.h>
9 #include "../perf.h"
10 #include "event.h"
11 #include "evsel.h"
12 #include "util.h"
13 #include "auxtrace.h"
14 #include <signal.h>
15 #include <unistd.h>
16 
17 struct pollfd;
18 struct thread_map;
19 struct cpu_map;
20 struct record_opts;
21 
22 #define PERF_EVLIST__HLIST_BITS 8
23 #define PERF_EVLIST__HLIST_SIZE (1 << PERF_EVLIST__HLIST_BITS)
24 
25 /**
26  * struct perf_mmap - perf's ring buffer mmap details
27  *
28  * @refcnt - e.g. code using PERF_EVENT_IOC_SET_OUTPUT to share this
29  */
30 struct perf_mmap {
31 	void		 *base;
32 	int		 mask;
33 	int		 fd;
34 	refcount_t	 refcnt;
35 	u64		 prev;
36 	struct auxtrace_mmap auxtrace_mmap;
37 	char		 event_copy[PERF_SAMPLE_MAX_SIZE] __attribute__((aligned(8)));
38 };
39 
40 static inline size_t
41 perf_mmap__mmap_len(struct perf_mmap *map)
42 {
43 	return map->mask + 1 + page_size;
44 }
45 
46 /*
47  * State machine of bkw_mmap_state:
48  *
49  *                     .________________(forbid)_____________.
50  *                     |                                     V
51  * NOTREADY --(0)--> RUNNING --(1)--> DATA_PENDING --(2)--> EMPTY
52  *                     ^  ^              |   ^               |
53  *                     |  |__(forbid)____/   |___(forbid)___/|
54  *                     |                                     |
55  *                      \_________________(3)_______________/
56  *
57  * NOTREADY     : Backward ring buffers are not ready
58  * RUNNING      : Backward ring buffers are recording
59  * DATA_PENDING : We are required to collect data from backward ring buffers
60  * EMPTY        : We have collected data from backward ring buffers.
61  *
62  * (0): Setup backward ring buffer
63  * (1): Pause ring buffers for reading
64  * (2): Read from ring buffers
65  * (3): Resume ring buffers for recording
66  */
67 enum bkw_mmap_state {
68 	BKW_MMAP_NOTREADY,
69 	BKW_MMAP_RUNNING,
70 	BKW_MMAP_DATA_PENDING,
71 	BKW_MMAP_EMPTY,
72 };
73 
74 struct perf_evlist {
75 	struct list_head entries;
76 	struct hlist_head heads[PERF_EVLIST__HLIST_SIZE];
77 	int		 nr_entries;
78 	int		 nr_groups;
79 	int		 nr_mmaps;
80 	bool		 overwrite;
81 	bool		 enabled;
82 	bool		 has_user_cpus;
83 	size_t		 mmap_len;
84 	int		 id_pos;
85 	int		 is_pos;
86 	u64		 combined_sample_type;
87 	enum bkw_mmap_state bkw_mmap_state;
88 	struct {
89 		int	cork_fd;
90 		pid_t	pid;
91 	} workload;
92 	struct fdarray	 pollfd;
93 	struct perf_mmap *mmap;
94 	struct perf_mmap *backward_mmap;
95 	struct thread_map *threads;
96 	struct cpu_map	  *cpus;
97 	struct perf_evsel *selected;
98 	struct events_stats stats;
99 	struct perf_env	*env;
100 };
101 
102 struct perf_evsel_str_handler {
103 	const char *name;
104 	void	   *handler;
105 };
106 
107 struct perf_evlist *perf_evlist__new(void);
108 struct perf_evlist *perf_evlist__new_default(void);
109 struct perf_evlist *perf_evlist__new_dummy(void);
110 void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus,
111 		       struct thread_map *threads);
112 void perf_evlist__exit(struct perf_evlist *evlist);
113 void perf_evlist__delete(struct perf_evlist *evlist);
114 
115 void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry);
116 void perf_evlist__remove(struct perf_evlist *evlist, struct perf_evsel *evsel);
117 int perf_evlist__add_default(struct perf_evlist *evlist);
118 int __perf_evlist__add_default_attrs(struct perf_evlist *evlist,
119 				     struct perf_event_attr *attrs, size_t nr_attrs);
120 
121 #define perf_evlist__add_default_attrs(evlist, array) \
122 	__perf_evlist__add_default_attrs(evlist, array, ARRAY_SIZE(array))
123 
124 int perf_evlist__add_dummy(struct perf_evlist *evlist);
125 
126 int perf_evlist__add_newtp(struct perf_evlist *evlist,
127 			   const char *sys, const char *name, void *handler);
128 
129 void __perf_evlist__set_sample_bit(struct perf_evlist *evlist,
130 				   enum perf_event_sample_format bit);
131 void __perf_evlist__reset_sample_bit(struct perf_evlist *evlist,
132 				     enum perf_event_sample_format bit);
133 
134 #define perf_evlist__set_sample_bit(evlist, bit) \
135 	__perf_evlist__set_sample_bit(evlist, PERF_SAMPLE_##bit)
136 
137 #define perf_evlist__reset_sample_bit(evlist, bit) \
138 	__perf_evlist__reset_sample_bit(evlist, PERF_SAMPLE_##bit)
139 
140 int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter);
141 int perf_evlist__set_filter_pid(struct perf_evlist *evlist, pid_t pid);
142 int perf_evlist__set_filter_pids(struct perf_evlist *evlist, size_t npids, pid_t *pids);
143 
144 struct perf_evsel *
145 perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id);
146 
147 struct perf_evsel *
148 perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist,
149 				     const char *name);
150 
151 void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
152 			 int cpu, int thread, u64 id);
153 int perf_evlist__id_add_fd(struct perf_evlist *evlist,
154 			   struct perf_evsel *evsel,
155 			   int cpu, int thread, int fd);
156 
157 int perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd);
158 int perf_evlist__alloc_pollfd(struct perf_evlist *evlist);
159 int perf_evlist__filter_pollfd(struct perf_evlist *evlist, short revents_and_mask);
160 
161 int perf_evlist__poll(struct perf_evlist *evlist, int timeout);
162 
163 struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id);
164 struct perf_evsel *perf_evlist__id2evsel_strict(struct perf_evlist *evlist,
165 						u64 id);
166 
167 struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id);
168 
169 void perf_evlist__toggle_bkw_mmap(struct perf_evlist *evlist, enum bkw_mmap_state state);
170 
171 union perf_event *perf_mmap__read_forward(struct perf_mmap *map, bool check_messup);
172 union perf_event *perf_mmap__read_backward(struct perf_mmap *map);
173 
174 void perf_mmap__read_catchup(struct perf_mmap *md);
175 void perf_mmap__consume(struct perf_mmap *md, bool overwrite);
176 
177 union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx);
178 
179 union perf_event *perf_evlist__mmap_read_forward(struct perf_evlist *evlist,
180 						 int idx);
181 union perf_event *perf_evlist__mmap_read_backward(struct perf_evlist *evlist,
182 						  int idx);
183 void perf_evlist__mmap_read_catchup(struct perf_evlist *evlist, int idx);
184 
185 void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx);
186 
187 int perf_evlist__open(struct perf_evlist *evlist);
188 void perf_evlist__close(struct perf_evlist *evlist);
189 
190 struct callchain_param;
191 
192 void perf_evlist__set_id_pos(struct perf_evlist *evlist);
193 bool perf_can_sample_identifier(void);
194 bool perf_can_record_switch_events(void);
195 bool perf_can_record_cpu_wide(void);
196 void perf_evlist__config(struct perf_evlist *evlist, struct record_opts *opts,
197 			 struct callchain_param *callchain);
198 int record_opts__config(struct record_opts *opts);
199 
200 int perf_evlist__prepare_workload(struct perf_evlist *evlist,
201 				  struct target *target,
202 				  const char *argv[], bool pipe_output,
203 				  void (*exec_error)(int signo, siginfo_t *info,
204 						     void *ucontext));
205 int perf_evlist__start_workload(struct perf_evlist *evlist);
206 
207 struct option;
208 
209 int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str);
210 int perf_evlist__parse_mmap_pages(const struct option *opt,
211 				  const char *str,
212 				  int unset);
213 
214 unsigned long perf_event_mlock_kb_in_pages(void);
215 
216 int perf_evlist__mmap_ex(struct perf_evlist *evlist, unsigned int pages,
217 			 bool overwrite, unsigned int auxtrace_pages,
218 			 bool auxtrace_overwrite);
219 int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
220 		      bool overwrite);
221 void perf_evlist__munmap(struct perf_evlist *evlist);
222 
223 size_t perf_evlist__mmap_size(unsigned long pages);
224 
225 void perf_evlist__disable(struct perf_evlist *evlist);
226 void perf_evlist__enable(struct perf_evlist *evlist);
227 void perf_evlist__toggle_enable(struct perf_evlist *evlist);
228 
229 int perf_evlist__enable_event_idx(struct perf_evlist *evlist,
230 				  struct perf_evsel *evsel, int idx);
231 
232 void perf_evlist__set_selected(struct perf_evlist *evlist,
233 			       struct perf_evsel *evsel);
234 
235 void perf_evlist__set_maps(struct perf_evlist *evlist, struct cpu_map *cpus,
236 			   struct thread_map *threads);
237 int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target);
238 int perf_evlist__apply_filters(struct perf_evlist *evlist, struct perf_evsel **err_evsel);
239 
240 void __perf_evlist__set_leader(struct list_head *list);
241 void perf_evlist__set_leader(struct perf_evlist *evlist);
242 
243 u64 perf_evlist__read_format(struct perf_evlist *evlist);
244 u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist);
245 u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist);
246 u64 perf_evlist__combined_branch_type(struct perf_evlist *evlist);
247 bool perf_evlist__sample_id_all(struct perf_evlist *evlist);
248 u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist);
249 
250 int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
251 			      struct perf_sample *sample);
252 
253 bool perf_evlist__valid_sample_type(struct perf_evlist *evlist);
254 bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist);
255 bool perf_evlist__valid_read_format(struct perf_evlist *evlist);
256 
257 void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
258 				   struct list_head *list);
259 
260 static inline struct perf_evsel *perf_evlist__first(struct perf_evlist *evlist)
261 {
262 	return list_entry(evlist->entries.next, struct perf_evsel, node);
263 }
264 
265 static inline struct perf_evsel *perf_evlist__last(struct perf_evlist *evlist)
266 {
267 	return list_entry(evlist->entries.prev, struct perf_evsel, node);
268 }
269 
270 size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp);
271 
272 int perf_evlist__strerror_open(struct perf_evlist *evlist, int err, char *buf, size_t size);
273 int perf_evlist__strerror_mmap(struct perf_evlist *evlist, int err, char *buf, size_t size);
274 
275 static inline u64 perf_mmap__read_head(struct perf_mmap *mm)
276 {
277 	struct perf_event_mmap_page *pc = mm->base;
278 	u64 head = ACCESS_ONCE(pc->data_head);
279 	rmb();
280 	return head;
281 }
282 
283 static inline void perf_mmap__write_tail(struct perf_mmap *md, u64 tail)
284 {
285 	struct perf_event_mmap_page *pc = md->base;
286 
287 	/*
288 	 * ensure all reads are done before we write the tail out.
289 	 */
290 	mb();
291 	pc->data_tail = tail;
292 }
293 
294 bool perf_evlist__can_select_event(struct perf_evlist *evlist, const char *str);
295 void perf_evlist__to_front(struct perf_evlist *evlist,
296 			   struct perf_evsel *move_evsel);
297 
298 /**
299  * __evlist__for_each_entry - iterate thru all the evsels
300  * @list: list_head instance to iterate
301  * @evsel: struct evsel iterator
302  */
303 #define __evlist__for_each_entry(list, evsel) \
304         list_for_each_entry(evsel, list, node)
305 
306 /**
307  * evlist__for_each_entry - iterate thru all the evsels
308  * @evlist: evlist instance to iterate
309  * @evsel: struct evsel iterator
310  */
311 #define evlist__for_each_entry(evlist, evsel) \
312 	__evlist__for_each_entry(&(evlist)->entries, evsel)
313 
314 /**
315  * __evlist__for_each_entry_continue - continue iteration thru all the evsels
316  * @list: list_head instance to iterate
317  * @evsel: struct evsel iterator
318  */
319 #define __evlist__for_each_entry_continue(list, evsel) \
320         list_for_each_entry_continue(evsel, list, node)
321 
322 /**
323  * evlist__for_each_entry_continue - continue iteration thru all the evsels
324  * @evlist: evlist instance to iterate
325  * @evsel: struct evsel iterator
326  */
327 #define evlist__for_each_entry_continue(evlist, evsel) \
328 	__evlist__for_each_entry_continue(&(evlist)->entries, evsel)
329 
330 /**
331  * __evlist__for_each_entry_reverse - iterate thru all the evsels in reverse order
332  * @list: list_head instance to iterate
333  * @evsel: struct evsel iterator
334  */
335 #define __evlist__for_each_entry_reverse(list, evsel) \
336         list_for_each_entry_reverse(evsel, list, node)
337 
338 /**
339  * evlist__for_each_entry_reverse - iterate thru all the evsels in reverse order
340  * @evlist: evlist instance to iterate
341  * @evsel: struct evsel iterator
342  */
343 #define evlist__for_each_entry_reverse(evlist, evsel) \
344 	__evlist__for_each_entry_reverse(&(evlist)->entries, evsel)
345 
346 /**
347  * __evlist__for_each_entry_safe - safely iterate thru all the evsels
348  * @list: list_head instance to iterate
349  * @tmp: struct evsel temp iterator
350  * @evsel: struct evsel iterator
351  */
352 #define __evlist__for_each_entry_safe(list, tmp, evsel) \
353         list_for_each_entry_safe(evsel, tmp, list, node)
354 
355 /**
356  * evlist__for_each_entry_safe - safely iterate thru all the evsels
357  * @evlist: evlist instance to iterate
358  * @evsel: struct evsel iterator
359  * @tmp: struct evsel temp iterator
360  */
361 #define evlist__for_each_entry_safe(evlist, tmp, evsel) \
362 	__evlist__for_each_entry_safe(&(evlist)->entries, tmp, evsel)
363 
364 void perf_evlist__set_tracking_event(struct perf_evlist *evlist,
365 				     struct perf_evsel *tracking_evsel);
366 
367 void perf_event_attr__set_max_precise_ip(struct perf_event_attr *attr);
368 
369 struct perf_evsel *
370 perf_evlist__find_evsel_by_str(struct perf_evlist *evlist, const char *str);
371 
372 struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist,
373 					    union perf_event *event);
374 #endif /* __PERF_EVLIST_H */
375