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