1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef __PERF_EVSEL_H 3 #define __PERF_EVSEL_H 1 4 5 #include <stdbool.h> 6 7 #include <linux/list.h> 8 #include <linux/perf_event.h> 9 #include <linux/refcount.h> 10 #include <linux/types.h> 11 #include <sys/types.h> 12 13 #include <internal/evsel.h> 14 #include <perf/evsel.h> 15 16 #include "symbol_conf.h" 17 18 struct bperf_follower_bpf; 19 struct bperf_leader_bpf; 20 struct bpf_counter_ops; 21 struct bpf_object; 22 struct cgroup; 23 struct hashmap; 24 struct perf_counts; 25 struct perf_pmu; 26 struct perf_stat_config; 27 struct perf_stat_evsel; 28 struct target; 29 union perf_event; 30 31 typedef int (evsel__sb_cb_t)(union perf_event *event, void *data); 32 33 /** struct evsel - event selector 34 * 35 * @evlist - evlist this evsel is in, if it is in one. 36 * @core - libperf evsel object 37 * @name - Can be set to retain the original event name passed by the user, 38 * so that when showing results in tools such as 'perf stat', we 39 * show the name used, not some alias. 40 * @id_pos: the position of the event id (PERF_SAMPLE_ID or 41 * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of 42 * struct perf_record_sample 43 * @is_pos: the position (counting backwards) of the event id (PERF_SAMPLE_ID or 44 * PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if sample_id_all 45 * is used there is an id sample appended to non-sample events 46 * @priv: And what is in its containing unnamed union are tool specific 47 */ 48 struct evsel { 49 struct perf_evsel core; 50 struct evlist *evlist; 51 refcount_t refcnt; 52 off_t id_offset; 53 int id_pos; 54 int is_pos; 55 unsigned int sample_size; 56 57 /* 58 * These fields can be set in the parse-events code or similar. 59 * Please check evsel__clone() to copy them properly so that 60 * they can be released properly. 61 */ 62 struct { 63 char *name; 64 char *group_name; 65 const char *group_pmu_name; 66 #ifdef HAVE_LIBTRACEEVENT 67 char *tp_sys; 68 char *tp_name; 69 struct tep_event *tp_format; 70 #endif 71 char *filter; 72 unsigned long max_events; 73 double scale; 74 const char *unit; 75 struct cgroup *cgrp; 76 const char *metric_id; 77 78 /* The PMU the event is from. Used for missing_features, PMU name, etc. */ 79 struct perf_pmu *pmu; 80 81 /* 82 * This point to the first evsel with the same name, intended to store the 83 * aggregated counts in aggregation mode. 84 */ 85 struct evsel *first_wildcard_match; 86 /* parse modifier helper */ 87 int exclude_GH; 88 int sample_read; 89 bool snapshot; 90 bool per_pkg; 91 bool percore; 92 bool precise_max; 93 bool is_libpfm_event; 94 bool collect_stat; 95 bool weak_group; 96 bool bpf_counter; 97 bool use_config_name; 98 bool skippable; 99 bool retire_lat; 100 bool dont_regroup; 101 bool default_metricgroup; /* A member of the Default metricgroup */ 102 bool default_show_events; /* If a default group member, show the event */ 103 struct list_head config_terms; 104 u64 alternate_hw_config; 105 }; 106 107 /* 108 * metric fields are similar, but needs more care as they can have 109 * references to other metric (evsel). 110 */ 111 struct evsel *metric_leader; 112 113 void *handler; 114 struct perf_counts *counts; 115 struct perf_counts *prev_raw_counts; 116 unsigned long nr_events_printed; 117 struct perf_stat_evsel *stats; 118 void *priv; 119 u64 db_id; 120 bool uniquified_name; 121 bool supported; 122 bool needs_swap; 123 bool disabled; 124 bool no_aux_samples; 125 bool immediate; 126 bool tracking; 127 bool ignore_missing_thread; 128 bool forced_leader; 129 bool cmdline_group_boundary; 130 bool reset_group; 131 bool needs_auxtrace_mmap; 132 bool needs_uniquify; 133 bool fallenback_eacces; 134 bool fallenback_eopnotsupp; 135 u8 probe_type:3; 136 struct hashmap *per_pkg_mask; 137 int err; 138 int script_output_type; 139 struct { 140 evsel__sb_cb_t *cb; 141 void *data; 142 } side_band; 143 /* 144 * For reporting purposes, an evsel sample can have a callchain 145 * synthesized from AUX area data. Keep track of synthesized sample 146 * types here. Note, the recorded sample_type cannot be changed because 147 * it is needed to continue to parse events. 148 * See also evsel__has_callchain(). 149 */ 150 __u64 synth_sample_type; 151 152 /* 153 * Store the branch counter related information. 154 * br_cntr_idx: The idx of the branch counter event in the evlist 155 * br_cntr_nr: The number of the branch counter event in the group 156 * (Only available for the leader event) 157 * abbr_name: The abbreviation name assigned to an event which is 158 * logged by the branch counter. 159 * The abbr name is from A to Z9. NA is applied if out 160 * of the range. 161 */ 162 int br_cntr_idx; 163 int br_cntr_nr; 164 char abbr_name[3]; 165 166 /* 167 * bpf_counter_ops serves two use cases: 168 * 1. perf-stat -b counting events used byBPF programs 169 * 2. perf-stat --use-bpf use BPF programs to aggregate counts 170 */ 171 struct bpf_counter_ops *bpf_counter_ops; 172 173 struct list_head bpf_counter_list; /* for perf-stat -b */ 174 struct list_head bpf_filters; /* for perf-record --filter */ 175 176 /* for perf-stat --use-bpf */ 177 int bperf_leader_prog_fd; 178 int bperf_leader_link_fd; 179 union { 180 struct bperf_leader_bpf *leader_skel; 181 struct bperf_follower_bpf *follower_skel; 182 void *bpf_skel; 183 }; 184 unsigned long open_flags; 185 int precise_ip_original; 186 187 /* For tool events */ 188 /* Beginning time subtracted when the counter is read. */ 189 union { 190 /* Defaults for retirement latency events. */ 191 struct _retirement_latency { 192 double mean; 193 double min; 194 double max; 195 } retirement_latency; 196 /* duration_time is a single global time. */ 197 struct { 198 __u64 start_time; 199 __u64 accumulated_time; 200 } duration_time; 201 /* 202 * user_time and system_time read an initial value potentially 203 * per-CPU or per-pid. 204 */ 205 struct { 206 struct xyarray *start_times; 207 struct xyarray *accumulated_times; 208 } process_time; 209 }; 210 /* Is the tool's fd for /proc/pid/stat or /proc/stat. */ 211 bool pid_stat; 212 }; 213 214 struct perf_missing_features { 215 bool sample_id_all; 216 bool exclude_guest; 217 bool mmap2; 218 bool cloexec; 219 bool clockid; 220 bool clockid_wrong; 221 bool lbr_flags; 222 bool write_backward; 223 bool group_read; 224 bool ksymbol; 225 bool bpf; 226 bool aux_output; 227 bool branch_hw_idx; 228 bool cgroup; 229 bool data_page_size; 230 bool code_page_size; 231 bool weight_struct; 232 bool read_lost; 233 bool branch_counters; 234 bool aux_action; 235 bool inherit_sample_read; 236 bool defer_callchain; 237 }; 238 239 extern struct perf_missing_features perf_missing_features; 240 241 struct perf_cpu_map; 242 struct thread_map; 243 struct record_opts; 244 245 static inline struct perf_cpu_map *evsel__cpus(struct evsel *evsel) 246 { 247 return perf_evsel__cpus(&evsel->core); 248 } 249 250 static inline int evsel__nr_cpus(struct evsel *evsel) 251 { 252 return perf_cpu_map__nr(evsel__cpus(evsel)); 253 } 254 255 void evsel__compute_deltas(struct evsel *evsel, int cpu, int thread, 256 struct perf_counts_values *count); 257 258 int evsel__object_config(size_t object_size, 259 int (*init)(struct evsel *evsel), 260 void (*fini)(struct evsel *evsel)); 261 262 struct perf_pmu *evsel__find_pmu(const struct evsel *evsel); 263 const char *evsel__pmu_name(const struct evsel *evsel); 264 bool evsel__is_aux_event(const struct evsel *evsel); 265 266 bool evsel__is_probe(struct evsel *evsel); 267 bool evsel__is_kprobe(struct evsel *evsel); 268 bool evsel__is_uprobe(struct evsel *evsel); 269 270 struct evsel *evsel__new_idx(struct perf_event_attr *attr, int idx); 271 272 static inline struct evsel *evsel__new(struct perf_event_attr *attr) 273 { 274 return evsel__new_idx(attr, 0); 275 } 276 277 struct evsel *evsel__clone(struct evsel *orig); 278 279 int copy_config_terms(struct list_head *dst, struct list_head *src); 280 void free_config_terms(struct list_head *config_terms); 281 282 283 /* 284 * Returns pointer with encoded error via <linux/err.h> interface. 285 */ 286 struct evsel *evsel__newtp_idx(const char *sys, const char *name, int idx, bool format); 287 static inline struct evsel *evsel__newtp(const char *sys, const char *name) 288 { 289 return evsel__newtp_idx(sys, name, 0, true); 290 } 291 292 struct evsel *evsel__get(struct evsel *evsel); 293 void evsel__put(struct evsel *evsel); 294 295 #ifdef HAVE_LIBTRACEEVENT 296 struct tep_event *evsel__tp_format(struct evsel *evsel); 297 #endif 298 299 void evsel__set_priv_destructor(void (*destructor)(void *priv)); 300 301 struct callchain_param; 302 303 void evsel__config(struct evsel *evsel, const struct record_opts *opts, 304 const struct callchain_param *callchain); 305 void evsel__config_callchain(struct evsel *evsel, const struct record_opts *opts, 306 const struct callchain_param *callchain); 307 308 int __evsel__sample_size(u64 sample_type); 309 void evsel__calc_id_pos(struct evsel *evsel); 310 311 bool evsel__is_cache_op_valid(u8 type, u8 op); 312 313 static inline bool evsel__is_bpf(struct evsel *evsel) 314 { 315 return evsel->bpf_counter_ops != NULL; 316 } 317 318 static inline bool evsel__is_bperf(struct evsel *evsel) 319 { 320 return evsel->bpf_counter_ops != NULL && list_empty(&evsel->bpf_counter_list); 321 } 322 323 #define EVSEL__MAX_ALIASES 8 324 325 extern const char *const evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX][EVSEL__MAX_ALIASES]; 326 extern const char *const evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX][EVSEL__MAX_ALIASES]; 327 extern const char *const evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX][EVSEL__MAX_ALIASES]; 328 extern const char *const evsel__hw_names[PERF_COUNT_HW_MAX]; 329 extern const char *const evsel__sw_names[PERF_COUNT_SW_MAX]; 330 extern char *evsel__bpf_counter_events; 331 bool evsel__match_bpf_counter_events(const char *name); 332 int arch_evsel__hw_name(struct evsel *evsel, char *bf, size_t size); 333 334 int __evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result, char *bf, size_t size); 335 const char *evsel__name(struct evsel *evsel); 336 bool evsel__name_is(struct evsel *evsel, const char *name); 337 const char *evsel__metric_id(const struct evsel *evsel); 338 339 static inline bool evsel__is_retire_lat(const struct evsel *evsel) 340 { 341 return evsel->retire_lat; 342 } 343 344 const char *evsel__group_name(struct evsel *evsel); 345 int evsel__group_desc(struct evsel *evsel, char *buf, size_t size); 346 347 void __evsel__set_sample_bit(struct evsel *evsel, enum perf_event_sample_format bit); 348 void __evsel__reset_sample_bit(struct evsel *evsel, enum perf_event_sample_format bit); 349 350 #define evsel__set_sample_bit(evsel, bit) \ 351 __evsel__set_sample_bit(evsel, PERF_SAMPLE_##bit) 352 353 #define evsel__reset_sample_bit(evsel, bit) \ 354 __evsel__reset_sample_bit(evsel, PERF_SAMPLE_##bit) 355 356 void evsel__set_sample_id(struct evsel *evsel, bool use_sample_identifier); 357 358 void arch_evsel__set_sample_weight(struct evsel *evsel); 359 void arch__post_evsel_config(struct evsel *evsel, struct perf_event_attr *attr); 360 int arch_evsel__open_strerror(struct evsel *evsel, int err, char *msg, size_t size); 361 void arch_evsel__apply_ratio_to_prev(struct evsel *evsel, struct perf_event_attr *attr); 362 363 int evsel__set_filter(struct evsel *evsel, const char *filter); 364 int evsel__append_tp_filter(struct evsel *evsel, const char *filter); 365 int evsel__append_addr_filter(struct evsel *evsel, const char *filter); 366 367 int evsel__enable_cpu(struct evsel *evsel, int cpu_map_idx); 368 int evsel__enable(struct evsel *evsel); 369 int evsel__disable(struct evsel *evsel); 370 int evsel__disable_cpu(struct evsel *evsel, int cpu_map_idx); 371 372 int evsel__open_per_cpu_and_thread(struct evsel *evsel, 373 struct perf_cpu_map *cpus, int cpu_map_idx, 374 struct perf_thread_map *threads); 375 int evsel__open_per_cpu(struct evsel *evsel, struct perf_cpu_map *cpus, int cpu_map_idx); 376 int evsel__open_per_thread(struct evsel *evsel, struct perf_thread_map *threads); 377 int evsel__open(struct evsel *evsel, struct perf_cpu_map *cpus, 378 struct perf_thread_map *threads); 379 void evsel__close(struct evsel *evsel); 380 int evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus, 381 struct perf_thread_map *threads); 382 383 bool evsel__precise_ip_fallback(struct evsel *evsel); 384 385 struct perf_sample; 386 387 #ifdef HAVE_LIBTRACEEVENT 388 void *perf_sample__rawptr(struct perf_sample *sample, const char *name); 389 u64 perf_sample__intval(struct perf_sample *sample, const char *name); 390 u64 perf_sample__intval_common(struct perf_sample *sample, const char *name); 391 char perf_sample__taskstate(struct perf_sample *sample, const char *name); 392 393 static inline char *perf_sample__strval(struct perf_sample *sample, const char *name) 394 { 395 return perf_sample__rawptr(sample, name); 396 } 397 #endif 398 399 struct tep_format_field; 400 401 void *format_field__get_raw_data(struct tep_format_field *field, 402 struct perf_sample *sample, 403 bool needs_swap, u16 *len_out); 404 unsigned long *format_field__get_cpumask(struct tep_format_field *field, 405 struct perf_sample *sample, 406 bool needs_swap, u16 *len_out); 407 u64 format_field__intval(struct tep_format_field *field, struct perf_sample *sample, bool needs_swap); 408 409 #ifdef HAVE_LIBTRACEEVENT 410 struct tep_format_field *evsel__field(struct evsel *evsel, const char *name); 411 struct tep_format_field *evsel__common_field(struct evsel *evsel, const char *name); 412 #else 413 static inline struct tep_format_field * 414 evsel__field(struct evsel *evsel __maybe_unused, const char *name __maybe_unused) 415 { 416 return NULL; 417 } 418 419 static inline struct tep_format_field * 420 evsel__common_field(struct evsel *evsel __maybe_unused, const char *name __maybe_unused) 421 { 422 return NULL; 423 } 424 #endif 425 426 bool __evsel__match(const struct evsel *evsel, u32 type, u64 config); 427 428 #define evsel__match(evsel, t, c) __evsel__match(evsel, PERF_TYPE_##t, PERF_COUNT_##c) 429 430 int evsel__read_counter(struct evsel *evsel, int cpu_map_idx, int thread); 431 432 int __evsel__read_on_cpu(struct evsel *evsel, int cpu_map_idx, int thread, bool scale); 433 434 /** 435 * evsel__read_on_cpu - Read out the results on a CPU and thread 436 * 437 * @evsel - event selector to read value 438 * @cpu_map_idx - CPU of interest 439 * @thread - thread of interest 440 */ 441 static inline int evsel__read_on_cpu(struct evsel *evsel, int cpu_map_idx, int thread) 442 { 443 return __evsel__read_on_cpu(evsel, cpu_map_idx, thread, false); 444 } 445 446 /** 447 * evsel__read_on_cpu_scaled - Read out the results on a CPU and thread, scaled 448 * 449 * @evsel - event selector to read value 450 * @cpu_map_idx - CPU of interest 451 * @thread - thread of interest 452 */ 453 static inline int evsel__read_on_cpu_scaled(struct evsel *evsel, int cpu_map_idx, int thread) 454 { 455 return __evsel__read_on_cpu(evsel, cpu_map_idx, thread, true); 456 } 457 458 int __evsel__parse_sample(struct evsel *evsel, union perf_event *event, 459 struct perf_sample *data, bool needs_swap); 460 461 static inline int evsel__parse_sample(struct evsel *evsel, union perf_event *event, 462 struct perf_sample *data) 463 { 464 return __evsel__parse_sample(evsel, event, data, evsel->needs_swap); 465 } 466 467 int evsel__parse_sample_timestamp(struct evsel *evsel, union perf_event *event, 468 u64 *timestamp); 469 470 u16 evsel__id_hdr_size(const struct evsel *evsel); 471 472 static inline struct evsel *evsel__next(struct evsel *evsel) 473 { 474 return list_entry(evsel->core.node.next, struct evsel, core.node); 475 } 476 477 static inline struct evsel *evsel__prev(struct evsel *evsel) 478 { 479 return list_entry(evsel->core.node.prev, struct evsel, core.node); 480 } 481 482 /** 483 * evsel__is_group_leader - Return whether given evsel is a leader event 484 * 485 * @evsel - evsel selector to be tested 486 * 487 * Return %true if @evsel is a group leader or a stand-alone event 488 */ 489 static inline bool evsel__is_group_leader(const struct evsel *evsel) 490 { 491 return evsel->core.leader == &evsel->core; 492 } 493 494 /** 495 * evsel__is_group_event - Return whether given evsel is a group event 496 * 497 * @evsel - evsel selector to be tested 498 * 499 * Return %true iff event group view is enabled and @evsel is a actual group 500 * leader which has other members in the group 501 */ 502 static inline bool evsel__is_group_event(struct evsel *evsel) 503 { 504 if (!symbol_conf.event_group) 505 return false; 506 507 return evsel__is_group_leader(evsel) && evsel->core.nr_members > 1; 508 } 509 510 bool evsel__is_non_perf_event_open_pmu(const struct evsel *evsel); 511 bool evsel__is_function_event(struct evsel *evsel); 512 513 static inline bool evsel__is_bpf_output(struct evsel *evsel) 514 { 515 return evsel__match(evsel, SOFTWARE, SW_BPF_OUTPUT); 516 } 517 518 static inline bool evsel__is_clock(const struct evsel *evsel) 519 { 520 return evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) || 521 evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK); 522 } 523 524 bool evsel__fallback(struct evsel *evsel, struct target *target, int err, 525 char *msg, size_t msgsize); 526 int evsel__open_strerror(struct evsel *evsel, struct target *target, 527 int err, char *msg, size_t size); 528 529 static inline int evsel__group_idx(struct evsel *evsel) 530 { 531 return evsel->core.idx - evsel->core.leader->idx; 532 } 533 534 /* Iterates group WITHOUT the leader. */ 535 #define for_each_group_member_head(_evsel, _leader, _head) \ 536 for ((_evsel) = list_entry((_leader)->core.node.next, struct evsel, core.node); \ 537 (_evsel) && &(_evsel)->core.node != (_head) && \ 538 (_evsel)->core.leader == &(_leader)->core; \ 539 (_evsel) = list_entry((_evsel)->core.node.next, struct evsel, core.node)) 540 541 #define for_each_group_member(_evsel, _leader) \ 542 for_each_group_member_head(_evsel, _leader, &evlist__core((_leader)->evlist)->entries) 543 544 /* Iterates group WITH the leader. */ 545 #define for_each_group_evsel_head(_evsel, _leader, _head) \ 546 for ((_evsel) = _leader; \ 547 (_evsel) && &(_evsel)->core.node != (_head) && \ 548 (_evsel)->core.leader == &(_leader)->core; \ 549 (_evsel) = list_entry((_evsel)->core.node.next, struct evsel, core.node)) 550 551 #define for_each_group_evsel(_evsel, _leader) \ 552 for_each_group_evsel_head(_evsel, _leader, &evlist__core((_leader)->evlist)->entries) 553 554 static inline bool evsel__has_branch_callstack(const struct evsel *evsel) 555 { 556 return evsel->core.attr.branch_sample_type & PERF_SAMPLE_BRANCH_CALL_STACK; 557 } 558 559 static inline bool evsel__has_branch_hw_idx(const struct evsel *evsel) 560 { 561 return evsel->core.attr.branch_sample_type & PERF_SAMPLE_BRANCH_HW_INDEX; 562 } 563 564 static inline bool evsel__has_callchain(const struct evsel *evsel) 565 { 566 /* 567 * For reporting purposes, an evsel sample can have a recorded callchain 568 * or a callchain synthesized from AUX area data. 569 */ 570 return evsel->core.attr.sample_type & PERF_SAMPLE_CALLCHAIN || 571 evsel->synth_sample_type & PERF_SAMPLE_CALLCHAIN; 572 } 573 574 static inline bool evsel__has_br_stack(const struct evsel *evsel) 575 { 576 /* 577 * For reporting purposes, an evsel sample can have a recorded branch 578 * stack or a branch stack synthesized from AUX area data. 579 */ 580 return evsel->core.attr.sample_type & PERF_SAMPLE_BRANCH_STACK || 581 evsel->synth_sample_type & PERF_SAMPLE_BRANCH_STACK; 582 } 583 584 static inline bool evsel__is_dummy_event(struct evsel *evsel) 585 { 586 return (evsel->core.attr.type == PERF_TYPE_SOFTWARE) && 587 (evsel->core.attr.config == PERF_COUNT_SW_DUMMY); 588 } 589 590 struct perf_session *evsel__session(struct evsel *evsel); 591 struct perf_env *evsel__env(struct evsel *evsel); 592 uint16_t evsel__e_machine(struct evsel *evsel, uint32_t *e_flags); 593 594 int evsel__store_ids(struct evsel *evsel, struct evlist *evlist); 595 596 void evsel__zero_per_pkg(struct evsel *evsel); 597 bool evsel__is_hybrid(const struct evsel *evsel); 598 struct evsel *evsel__leader(const struct evsel *evsel); 599 bool evsel__has_leader(struct evsel *evsel, struct evsel *leader); 600 bool evsel__is_leader(struct evsel *evsel); 601 void evsel__set_leader(struct evsel *evsel, struct evsel *leader); 602 int evsel__source_count(const struct evsel *evsel); 603 void evsel__remove_from_group(struct evsel *evsel, struct evsel *leader); 604 605 bool arch_evsel__must_be_in_group(const struct evsel *evsel); 606 607 bool evsel__set_needs_uniquify(struct evsel *counter, const struct perf_stat_config *config); 608 void evsel__uniquify_counter(struct evsel *counter); 609 610 /* 611 * Macro to swap the bit-field postition and size. 612 * Used when, 613 * - dont need to swap the entire u64 && 614 * - when u64 has variable bit-field sizes && 615 * - when presented in a host endian which is different 616 * than the source endian of the perf.data file 617 */ 618 #define bitfield_swap(src, pos, size) \ 619 ((((src) >> (pos)) & ((1ull << (size)) - 1)) << (63 - ((pos) + (size) - 1))) 620 621 u64 evsel__bitfield_swap_branch_flags(u64 value); 622 bool evsel__config_exists(const struct evsel *evsel, const char *config_name); 623 int evsel__get_config_val(const struct evsel *evsel, const char *config_name, 624 u64 *val); 625 void evsel__set_config_if_unset(struct evsel *evsel, const char *config_name, 626 u64 val); 627 628 bool evsel__is_offcpu_event(struct evsel *evsel); 629 630 void evsel__warn_user_requested_cpus(struct evsel *evsel, struct perf_cpu_map *user_requested_cpus); 631 632 #endif /* __PERF_EVSEL_H */ 633