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