1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com> 4 * 5 * Parts came from builtin-{top,stat,record}.c, see those files for further 6 * copyright notes. 7 */ 8 9 #include <byteswap.h> 10 #include <errno.h> 11 #include <inttypes.h> 12 #include <linux/bitops.h> 13 #include <api/fs/fs.h> 14 #include <api/fs/tracing_path.h> 15 #include <linux/hw_breakpoint.h> 16 #include <linux/perf_event.h> 17 #include <linux/compiler.h> 18 #include <linux/err.h> 19 #include <linux/zalloc.h> 20 #include <sys/ioctl.h> 21 #include <sys/resource.h> 22 #include <sys/types.h> 23 #include <dirent.h> 24 #include <stdlib.h> 25 #include <perf/evsel.h> 26 #include "asm/bug.h" 27 #include "bpf_counter.h" 28 #include "callchain.h" 29 #include "cgroup.h" 30 #include "counts.h" 31 #include "event.h" 32 #include "evsel.h" 33 #include "util/env.h" 34 #include "util/evsel_config.h" 35 #include "util/evsel_fprintf.h" 36 #include "evlist.h" 37 #include <perf/cpumap.h> 38 #include "thread_map.h" 39 #include "target.h" 40 #include "perf_regs.h" 41 #include "record.h" 42 #include "debug.h" 43 #include "trace-event.h" 44 #include "stat.h" 45 #include "string2.h" 46 #include "memswap.h" 47 #include "util.h" 48 #include "util/hashmap.h" 49 #include "pmu-hybrid.h" 50 #include "off_cpu.h" 51 #include "../perf-sys.h" 52 #include "util/parse-branch-options.h" 53 #include "util/bpf-filter.h" 54 #include <internal/xyarray.h> 55 #include <internal/lib.h> 56 #include <internal/threadmap.h> 57 58 #include <linux/ctype.h> 59 60 #ifdef HAVE_LIBTRACEEVENT 61 #include <traceevent/event-parse.h> 62 #endif 63 64 struct perf_missing_features perf_missing_features; 65 66 static clockid_t clockid; 67 68 static const char *const perf_tool_event__tool_names[PERF_TOOL_MAX] = { 69 NULL, 70 "duration_time", 71 "user_time", 72 "system_time", 73 }; 74 75 const char *perf_tool_event__to_str(enum perf_tool_event ev) 76 { 77 if (ev > PERF_TOOL_NONE && ev < PERF_TOOL_MAX) 78 return perf_tool_event__tool_names[ev]; 79 80 return NULL; 81 } 82 83 enum perf_tool_event perf_tool_event__from_str(const char *str) 84 { 85 int i; 86 87 perf_tool_event__for_each_event(i) { 88 if (!strcmp(str, perf_tool_event__tool_names[i])) 89 return i; 90 } 91 return PERF_TOOL_NONE; 92 } 93 94 95 static int evsel__no_extra_init(struct evsel *evsel __maybe_unused) 96 { 97 return 0; 98 } 99 100 void __weak test_attr__ready(void) { } 101 102 static void evsel__no_extra_fini(struct evsel *evsel __maybe_unused) 103 { 104 } 105 106 static struct { 107 size_t size; 108 int (*init)(struct evsel *evsel); 109 void (*fini)(struct evsel *evsel); 110 } perf_evsel__object = { 111 .size = sizeof(struct evsel), 112 .init = evsel__no_extra_init, 113 .fini = evsel__no_extra_fini, 114 }; 115 116 int evsel__object_config(size_t object_size, int (*init)(struct evsel *evsel), 117 void (*fini)(struct evsel *evsel)) 118 { 119 120 if (object_size == 0) 121 goto set_methods; 122 123 if (perf_evsel__object.size > object_size) 124 return -EINVAL; 125 126 perf_evsel__object.size = object_size; 127 128 set_methods: 129 if (init != NULL) 130 perf_evsel__object.init = init; 131 132 if (fini != NULL) 133 perf_evsel__object.fini = fini; 134 135 return 0; 136 } 137 138 #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y)) 139 140 int __evsel__sample_size(u64 sample_type) 141 { 142 u64 mask = sample_type & PERF_SAMPLE_MASK; 143 int size = 0; 144 int i; 145 146 for (i = 0; i < 64; i++) { 147 if (mask & (1ULL << i)) 148 size++; 149 } 150 151 size *= sizeof(u64); 152 153 return size; 154 } 155 156 /** 157 * __perf_evsel__calc_id_pos - calculate id_pos. 158 * @sample_type: sample type 159 * 160 * This function returns the position of the event id (PERF_SAMPLE_ID or 161 * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of struct 162 * perf_record_sample. 163 */ 164 static int __perf_evsel__calc_id_pos(u64 sample_type) 165 { 166 int idx = 0; 167 168 if (sample_type & PERF_SAMPLE_IDENTIFIER) 169 return 0; 170 171 if (!(sample_type & PERF_SAMPLE_ID)) 172 return -1; 173 174 if (sample_type & PERF_SAMPLE_IP) 175 idx += 1; 176 177 if (sample_type & PERF_SAMPLE_TID) 178 idx += 1; 179 180 if (sample_type & PERF_SAMPLE_TIME) 181 idx += 1; 182 183 if (sample_type & PERF_SAMPLE_ADDR) 184 idx += 1; 185 186 return idx; 187 } 188 189 /** 190 * __perf_evsel__calc_is_pos - calculate is_pos. 191 * @sample_type: sample type 192 * 193 * This function returns the position (counting backwards) of the event id 194 * (PERF_SAMPLE_ID or PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if 195 * sample_id_all is used there is an id sample appended to non-sample events. 196 */ 197 static int __perf_evsel__calc_is_pos(u64 sample_type) 198 { 199 int idx = 1; 200 201 if (sample_type & PERF_SAMPLE_IDENTIFIER) 202 return 1; 203 204 if (!(sample_type & PERF_SAMPLE_ID)) 205 return -1; 206 207 if (sample_type & PERF_SAMPLE_CPU) 208 idx += 1; 209 210 if (sample_type & PERF_SAMPLE_STREAM_ID) 211 idx += 1; 212 213 return idx; 214 } 215 216 void evsel__calc_id_pos(struct evsel *evsel) 217 { 218 evsel->id_pos = __perf_evsel__calc_id_pos(evsel->core.attr.sample_type); 219 evsel->is_pos = __perf_evsel__calc_is_pos(evsel->core.attr.sample_type); 220 } 221 222 void __evsel__set_sample_bit(struct evsel *evsel, 223 enum perf_event_sample_format bit) 224 { 225 if (!(evsel->core.attr.sample_type & bit)) { 226 evsel->core.attr.sample_type |= bit; 227 evsel->sample_size += sizeof(u64); 228 evsel__calc_id_pos(evsel); 229 } 230 } 231 232 void __evsel__reset_sample_bit(struct evsel *evsel, 233 enum perf_event_sample_format bit) 234 { 235 if (evsel->core.attr.sample_type & bit) { 236 evsel->core.attr.sample_type &= ~bit; 237 evsel->sample_size -= sizeof(u64); 238 evsel__calc_id_pos(evsel); 239 } 240 } 241 242 void evsel__set_sample_id(struct evsel *evsel, 243 bool can_sample_identifier) 244 { 245 if (can_sample_identifier) { 246 evsel__reset_sample_bit(evsel, ID); 247 evsel__set_sample_bit(evsel, IDENTIFIER); 248 } else { 249 evsel__set_sample_bit(evsel, ID); 250 } 251 evsel->core.attr.read_format |= PERF_FORMAT_ID; 252 } 253 254 /** 255 * evsel__is_function_event - Return whether given evsel is a function 256 * trace event 257 * 258 * @evsel - evsel selector to be tested 259 * 260 * Return %true if event is function trace event 261 */ 262 bool evsel__is_function_event(struct evsel *evsel) 263 { 264 #define FUNCTION_EVENT "ftrace:function" 265 266 return evsel->name && 267 !strncmp(FUNCTION_EVENT, evsel->name, sizeof(FUNCTION_EVENT)); 268 269 #undef FUNCTION_EVENT 270 } 271 272 void evsel__init(struct evsel *evsel, 273 struct perf_event_attr *attr, int idx) 274 { 275 perf_evsel__init(&evsel->core, attr, idx); 276 evsel->tracking = !idx; 277 evsel->unit = strdup(""); 278 evsel->scale = 1.0; 279 evsel->max_events = ULONG_MAX; 280 evsel->evlist = NULL; 281 evsel->bpf_obj = NULL; 282 evsel->bpf_fd = -1; 283 INIT_LIST_HEAD(&evsel->config_terms); 284 INIT_LIST_HEAD(&evsel->bpf_counter_list); 285 INIT_LIST_HEAD(&evsel->bpf_filters); 286 perf_evsel__object.init(evsel); 287 evsel->sample_size = __evsel__sample_size(attr->sample_type); 288 evsel__calc_id_pos(evsel); 289 evsel->cmdline_group_boundary = false; 290 evsel->metric_events = NULL; 291 evsel->per_pkg_mask = NULL; 292 evsel->collect_stat = false; 293 evsel->pmu_name = NULL; 294 evsel->skippable = false; 295 } 296 297 struct evsel *evsel__new_idx(struct perf_event_attr *attr, int idx) 298 { 299 struct evsel *evsel = zalloc(perf_evsel__object.size); 300 301 if (!evsel) 302 return NULL; 303 evsel__init(evsel, attr, idx); 304 305 if (evsel__is_bpf_output(evsel) && !attr->sample_type) { 306 evsel->core.attr.sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME | 307 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD), 308 evsel->core.attr.sample_period = 1; 309 } 310 311 if (evsel__is_clock(evsel)) { 312 free((char *)evsel->unit); 313 evsel->unit = strdup("msec"); 314 evsel->scale = 1e-6; 315 } 316 317 return evsel; 318 } 319 320 static bool perf_event_can_profile_kernel(void) 321 { 322 return perf_event_paranoid_check(1); 323 } 324 325 struct evsel *evsel__new_cycles(bool precise __maybe_unused, __u32 type, __u64 config) 326 { 327 struct perf_event_attr attr = { 328 .type = type, 329 .config = config, 330 .exclude_kernel = !perf_event_can_profile_kernel(), 331 }; 332 struct evsel *evsel; 333 334 event_attr_init(&attr); 335 336 /* 337 * Now let the usual logic to set up the perf_event_attr defaults 338 * to kick in when we return and before perf_evsel__open() is called. 339 */ 340 evsel = evsel__new(&attr); 341 if (evsel == NULL) 342 goto out; 343 344 arch_evsel__fixup_new_cycles(&evsel->core.attr); 345 346 evsel->precise_max = true; 347 348 /* use asprintf() because free(evsel) assumes name is allocated */ 349 if (asprintf(&evsel->name, "cycles%s%s%.*s", 350 (attr.precise_ip || attr.exclude_kernel) ? ":" : "", 351 attr.exclude_kernel ? "u" : "", 352 attr.precise_ip ? attr.precise_ip + 1 : 0, "ppp") < 0) 353 goto error_free; 354 out: 355 return evsel; 356 error_free: 357 evsel__delete(evsel); 358 evsel = NULL; 359 goto out; 360 } 361 362 int copy_config_terms(struct list_head *dst, struct list_head *src) 363 { 364 struct evsel_config_term *pos, *tmp; 365 366 list_for_each_entry(pos, src, list) { 367 tmp = malloc(sizeof(*tmp)); 368 if (tmp == NULL) 369 return -ENOMEM; 370 371 *tmp = *pos; 372 if (tmp->free_str) { 373 tmp->val.str = strdup(pos->val.str); 374 if (tmp->val.str == NULL) { 375 free(tmp); 376 return -ENOMEM; 377 } 378 } 379 list_add_tail(&tmp->list, dst); 380 } 381 return 0; 382 } 383 384 static int evsel__copy_config_terms(struct evsel *dst, struct evsel *src) 385 { 386 return copy_config_terms(&dst->config_terms, &src->config_terms); 387 } 388 389 /** 390 * evsel__clone - create a new evsel copied from @orig 391 * @orig: original evsel 392 * 393 * The assumption is that @orig is not configured nor opened yet. 394 * So we only care about the attributes that can be set while it's parsed. 395 */ 396 struct evsel *evsel__clone(struct evsel *orig) 397 { 398 struct evsel *evsel; 399 400 BUG_ON(orig->core.fd); 401 BUG_ON(orig->counts); 402 BUG_ON(orig->priv); 403 BUG_ON(orig->per_pkg_mask); 404 405 /* cannot handle BPF objects for now */ 406 if (orig->bpf_obj) 407 return NULL; 408 409 evsel = evsel__new(&orig->core.attr); 410 if (evsel == NULL) 411 return NULL; 412 413 evsel->core.cpus = perf_cpu_map__get(orig->core.cpus); 414 evsel->core.own_cpus = perf_cpu_map__get(orig->core.own_cpus); 415 evsel->core.threads = perf_thread_map__get(orig->core.threads); 416 evsel->core.nr_members = orig->core.nr_members; 417 evsel->core.system_wide = orig->core.system_wide; 418 evsel->core.requires_cpu = orig->core.requires_cpu; 419 420 if (orig->name) { 421 evsel->name = strdup(orig->name); 422 if (evsel->name == NULL) 423 goto out_err; 424 } 425 if (orig->group_name) { 426 evsel->group_name = strdup(orig->group_name); 427 if (evsel->group_name == NULL) 428 goto out_err; 429 } 430 if (orig->pmu_name) { 431 evsel->pmu_name = strdup(orig->pmu_name); 432 if (evsel->pmu_name == NULL) 433 goto out_err; 434 } 435 if (orig->filter) { 436 evsel->filter = strdup(orig->filter); 437 if (evsel->filter == NULL) 438 goto out_err; 439 } 440 if (orig->metric_id) { 441 evsel->metric_id = strdup(orig->metric_id); 442 if (evsel->metric_id == NULL) 443 goto out_err; 444 } 445 evsel->cgrp = cgroup__get(orig->cgrp); 446 #ifdef HAVE_LIBTRACEEVENT 447 evsel->tp_format = orig->tp_format; 448 #endif 449 evsel->handler = orig->handler; 450 evsel->core.leader = orig->core.leader; 451 452 evsel->max_events = orig->max_events; 453 evsel->tool_event = orig->tool_event; 454 free((char *)evsel->unit); 455 evsel->unit = strdup(orig->unit); 456 if (evsel->unit == NULL) 457 goto out_err; 458 459 evsel->scale = orig->scale; 460 evsel->snapshot = orig->snapshot; 461 evsel->per_pkg = orig->per_pkg; 462 evsel->percore = orig->percore; 463 evsel->precise_max = orig->precise_max; 464 evsel->is_libpfm_event = orig->is_libpfm_event; 465 466 evsel->exclude_GH = orig->exclude_GH; 467 evsel->sample_read = orig->sample_read; 468 evsel->auto_merge_stats = orig->auto_merge_stats; 469 evsel->collect_stat = orig->collect_stat; 470 evsel->weak_group = orig->weak_group; 471 evsel->use_config_name = orig->use_config_name; 472 evsel->pmu = orig->pmu; 473 474 if (evsel__copy_config_terms(evsel, orig) < 0) 475 goto out_err; 476 477 return evsel; 478 479 out_err: 480 evsel__delete(evsel); 481 return NULL; 482 } 483 484 /* 485 * Returns pointer with encoded error via <linux/err.h> interface. 486 */ 487 #ifdef HAVE_LIBTRACEEVENT 488 struct evsel *evsel__newtp_idx(const char *sys, const char *name, int idx) 489 { 490 struct evsel *evsel = zalloc(perf_evsel__object.size); 491 int err = -ENOMEM; 492 493 if (evsel == NULL) { 494 goto out_err; 495 } else { 496 struct perf_event_attr attr = { 497 .type = PERF_TYPE_TRACEPOINT, 498 .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME | 499 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD), 500 }; 501 502 if (asprintf(&evsel->name, "%s:%s", sys, name) < 0) 503 goto out_free; 504 505 evsel->tp_format = trace_event__tp_format(sys, name); 506 if (IS_ERR(evsel->tp_format)) { 507 err = PTR_ERR(evsel->tp_format); 508 goto out_free; 509 } 510 511 event_attr_init(&attr); 512 attr.config = evsel->tp_format->id; 513 attr.sample_period = 1; 514 evsel__init(evsel, &attr, idx); 515 } 516 517 return evsel; 518 519 out_free: 520 zfree(&evsel->name); 521 free(evsel); 522 out_err: 523 return ERR_PTR(err); 524 } 525 #endif 526 527 const char *const evsel__hw_names[PERF_COUNT_HW_MAX] = { 528 "cycles", 529 "instructions", 530 "cache-references", 531 "cache-misses", 532 "branches", 533 "branch-misses", 534 "bus-cycles", 535 "stalled-cycles-frontend", 536 "stalled-cycles-backend", 537 "ref-cycles", 538 }; 539 540 char *evsel__bpf_counter_events; 541 542 bool evsel__match_bpf_counter_events(const char *name) 543 { 544 int name_len; 545 bool match; 546 char *ptr; 547 548 if (!evsel__bpf_counter_events) 549 return false; 550 551 ptr = strstr(evsel__bpf_counter_events, name); 552 name_len = strlen(name); 553 554 /* check name matches a full token in evsel__bpf_counter_events */ 555 match = (ptr != NULL) && 556 ((ptr == evsel__bpf_counter_events) || (*(ptr - 1) == ',')) && 557 ((*(ptr + name_len) == ',') || (*(ptr + name_len) == '\0')); 558 559 return match; 560 } 561 562 static const char *__evsel__hw_name(u64 config) 563 { 564 if (config < PERF_COUNT_HW_MAX && evsel__hw_names[config]) 565 return evsel__hw_names[config]; 566 567 return "unknown-hardware"; 568 } 569 570 static int evsel__add_modifiers(struct evsel *evsel, char *bf, size_t size) 571 { 572 int colon = 0, r = 0; 573 struct perf_event_attr *attr = &evsel->core.attr; 574 bool exclude_guest_default = false; 575 576 #define MOD_PRINT(context, mod) do { \ 577 if (!attr->exclude_##context) { \ 578 if (!colon) colon = ++r; \ 579 r += scnprintf(bf + r, size - r, "%c", mod); \ 580 } } while(0) 581 582 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) { 583 MOD_PRINT(kernel, 'k'); 584 MOD_PRINT(user, 'u'); 585 MOD_PRINT(hv, 'h'); 586 exclude_guest_default = true; 587 } 588 589 if (attr->precise_ip) { 590 if (!colon) 591 colon = ++r; 592 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp"); 593 exclude_guest_default = true; 594 } 595 596 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) { 597 MOD_PRINT(host, 'H'); 598 MOD_PRINT(guest, 'G'); 599 } 600 #undef MOD_PRINT 601 if (colon) 602 bf[colon - 1] = ':'; 603 return r; 604 } 605 606 int __weak arch_evsel__hw_name(struct evsel *evsel, char *bf, size_t size) 607 { 608 return scnprintf(bf, size, "%s", __evsel__hw_name(evsel->core.attr.config)); 609 } 610 611 static int evsel__hw_name(struct evsel *evsel, char *bf, size_t size) 612 { 613 int r = arch_evsel__hw_name(evsel, bf, size); 614 return r + evsel__add_modifiers(evsel, bf + r, size - r); 615 } 616 617 const char *const evsel__sw_names[PERF_COUNT_SW_MAX] = { 618 "cpu-clock", 619 "task-clock", 620 "page-faults", 621 "context-switches", 622 "cpu-migrations", 623 "minor-faults", 624 "major-faults", 625 "alignment-faults", 626 "emulation-faults", 627 "dummy", 628 }; 629 630 static const char *__evsel__sw_name(u64 config) 631 { 632 if (config < PERF_COUNT_SW_MAX && evsel__sw_names[config]) 633 return evsel__sw_names[config]; 634 return "unknown-software"; 635 } 636 637 static int evsel__sw_name(struct evsel *evsel, char *bf, size_t size) 638 { 639 int r = scnprintf(bf, size, "%s", __evsel__sw_name(evsel->core.attr.config)); 640 return r + evsel__add_modifiers(evsel, bf + r, size - r); 641 } 642 643 static int evsel__tool_name(enum perf_tool_event ev, char *bf, size_t size) 644 { 645 return scnprintf(bf, size, "%s", perf_tool_event__to_str(ev)); 646 } 647 648 static int __evsel__bp_name(char *bf, size_t size, u64 addr, u64 type) 649 { 650 int r; 651 652 r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr); 653 654 if (type & HW_BREAKPOINT_R) 655 r += scnprintf(bf + r, size - r, "r"); 656 657 if (type & HW_BREAKPOINT_W) 658 r += scnprintf(bf + r, size - r, "w"); 659 660 if (type & HW_BREAKPOINT_X) 661 r += scnprintf(bf + r, size - r, "x"); 662 663 return r; 664 } 665 666 static int evsel__bp_name(struct evsel *evsel, char *bf, size_t size) 667 { 668 struct perf_event_attr *attr = &evsel->core.attr; 669 int r = __evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type); 670 return r + evsel__add_modifiers(evsel, bf + r, size - r); 671 } 672 673 const char *const evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX][EVSEL__MAX_ALIASES] = { 674 { "L1-dcache", "l1-d", "l1d", "L1-data", }, 675 { "L1-icache", "l1-i", "l1i", "L1-instruction", }, 676 { "LLC", "L2", }, 677 { "dTLB", "d-tlb", "Data-TLB", }, 678 { "iTLB", "i-tlb", "Instruction-TLB", }, 679 { "branch", "branches", "bpu", "btb", "bpc", }, 680 { "node", }, 681 }; 682 683 const char *const evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX][EVSEL__MAX_ALIASES] = { 684 { "load", "loads", "read", }, 685 { "store", "stores", "write", }, 686 { "prefetch", "prefetches", "speculative-read", "speculative-load", }, 687 }; 688 689 const char *const evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX][EVSEL__MAX_ALIASES] = { 690 { "refs", "Reference", "ops", "access", }, 691 { "misses", "miss", }, 692 }; 693 694 #define C(x) PERF_COUNT_HW_CACHE_##x 695 #define CACHE_READ (1 << C(OP_READ)) 696 #define CACHE_WRITE (1 << C(OP_WRITE)) 697 #define CACHE_PREFETCH (1 << C(OP_PREFETCH)) 698 #define COP(x) (1 << x) 699 700 /* 701 * cache operation stat 702 * L1I : Read and prefetch only 703 * ITLB and BPU : Read-only 704 */ 705 static const unsigned long evsel__hw_cache_stat[C(MAX)] = { 706 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH), 707 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH), 708 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH), 709 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH), 710 [C(ITLB)] = (CACHE_READ), 711 [C(BPU)] = (CACHE_READ), 712 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH), 713 }; 714 715 bool evsel__is_cache_op_valid(u8 type, u8 op) 716 { 717 if (evsel__hw_cache_stat[type] & COP(op)) 718 return true; /* valid */ 719 else 720 return false; /* invalid */ 721 } 722 723 int __evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result, char *bf, size_t size) 724 { 725 if (result) { 726 return scnprintf(bf, size, "%s-%s-%s", evsel__hw_cache[type][0], 727 evsel__hw_cache_op[op][0], 728 evsel__hw_cache_result[result][0]); 729 } 730 731 return scnprintf(bf, size, "%s-%s", evsel__hw_cache[type][0], 732 evsel__hw_cache_op[op][1]); 733 } 734 735 static int __evsel__hw_cache_name(u64 config, char *bf, size_t size) 736 { 737 u8 op, result, type = (config >> 0) & 0xff; 738 const char *err = "unknown-ext-hardware-cache-type"; 739 740 if (type >= PERF_COUNT_HW_CACHE_MAX) 741 goto out_err; 742 743 op = (config >> 8) & 0xff; 744 err = "unknown-ext-hardware-cache-op"; 745 if (op >= PERF_COUNT_HW_CACHE_OP_MAX) 746 goto out_err; 747 748 result = (config >> 16) & 0xff; 749 err = "unknown-ext-hardware-cache-result"; 750 if (result >= PERF_COUNT_HW_CACHE_RESULT_MAX) 751 goto out_err; 752 753 err = "invalid-cache"; 754 if (!evsel__is_cache_op_valid(type, op)) 755 goto out_err; 756 757 return __evsel__hw_cache_type_op_res_name(type, op, result, bf, size); 758 out_err: 759 return scnprintf(bf, size, "%s", err); 760 } 761 762 static int evsel__hw_cache_name(struct evsel *evsel, char *bf, size_t size) 763 { 764 int ret = __evsel__hw_cache_name(evsel->core.attr.config, bf, size); 765 return ret + evsel__add_modifiers(evsel, bf + ret, size - ret); 766 } 767 768 static int evsel__raw_name(struct evsel *evsel, char *bf, size_t size) 769 { 770 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->core.attr.config); 771 return ret + evsel__add_modifiers(evsel, bf + ret, size - ret); 772 } 773 774 const char *evsel__name(struct evsel *evsel) 775 { 776 char bf[128]; 777 778 if (!evsel) 779 goto out_unknown; 780 781 if (evsel->name) 782 return evsel->name; 783 784 switch (evsel->core.attr.type) { 785 case PERF_TYPE_RAW: 786 evsel__raw_name(evsel, bf, sizeof(bf)); 787 break; 788 789 case PERF_TYPE_HARDWARE: 790 evsel__hw_name(evsel, bf, sizeof(bf)); 791 break; 792 793 case PERF_TYPE_HW_CACHE: 794 evsel__hw_cache_name(evsel, bf, sizeof(bf)); 795 break; 796 797 case PERF_TYPE_SOFTWARE: 798 if (evsel__is_tool(evsel)) 799 evsel__tool_name(evsel->tool_event, bf, sizeof(bf)); 800 else 801 evsel__sw_name(evsel, bf, sizeof(bf)); 802 break; 803 804 case PERF_TYPE_TRACEPOINT: 805 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint"); 806 break; 807 808 case PERF_TYPE_BREAKPOINT: 809 evsel__bp_name(evsel, bf, sizeof(bf)); 810 break; 811 812 default: 813 scnprintf(bf, sizeof(bf), "unknown attr type: %d", 814 evsel->core.attr.type); 815 break; 816 } 817 818 evsel->name = strdup(bf); 819 820 if (evsel->name) 821 return evsel->name; 822 out_unknown: 823 return "unknown"; 824 } 825 826 bool evsel__name_is(struct evsel *evsel, const char *name) 827 { 828 return !strcmp(evsel__name(evsel), name); 829 } 830 831 const char *evsel__group_pmu_name(const struct evsel *evsel) 832 { 833 struct evsel *leader = evsel__leader(evsel); 834 struct evsel *pos; 835 836 /* 837 * Software events may be in a group with other uncore PMU events. Use 838 * the pmu_name of the first non-software event to avoid breaking the 839 * software event out of the group. 840 * 841 * Aux event leaders, like intel_pt, expect a group with events from 842 * other PMUs, so substitute the AUX event's PMU in this case. 843 */ 844 if (evsel->core.attr.type == PERF_TYPE_SOFTWARE || evsel__is_aux_event(leader)) { 845 /* Starting with the leader, find the first event with a named PMU. */ 846 for_each_group_evsel(pos, leader) { 847 if (pos->pmu_name) 848 return pos->pmu_name; 849 } 850 } 851 852 return evsel->pmu_name ?: "cpu"; 853 } 854 855 const char *evsel__metric_id(const struct evsel *evsel) 856 { 857 if (evsel->metric_id) 858 return evsel->metric_id; 859 860 if (evsel__is_tool(evsel)) 861 return perf_tool_event__to_str(evsel->tool_event); 862 863 return "unknown"; 864 } 865 866 const char *evsel__group_name(struct evsel *evsel) 867 { 868 return evsel->group_name ?: "anon group"; 869 } 870 871 /* 872 * Returns the group details for the specified leader, 873 * with following rules. 874 * 875 * For record -e '{cycles,instructions}' 876 * 'anon group { cycles:u, instructions:u }' 877 * 878 * For record -e 'cycles,instructions' and report --group 879 * 'cycles:u, instructions:u' 880 */ 881 int evsel__group_desc(struct evsel *evsel, char *buf, size_t size) 882 { 883 int ret = 0; 884 struct evsel *pos; 885 const char *group_name = evsel__group_name(evsel); 886 887 if (!evsel->forced_leader) 888 ret = scnprintf(buf, size, "%s { ", group_name); 889 890 ret += scnprintf(buf + ret, size - ret, "%s", evsel__name(evsel)); 891 892 for_each_group_member(pos, evsel) 893 ret += scnprintf(buf + ret, size - ret, ", %s", evsel__name(pos)); 894 895 if (!evsel->forced_leader) 896 ret += scnprintf(buf + ret, size - ret, " }"); 897 898 return ret; 899 } 900 901 static void __evsel__config_callchain(struct evsel *evsel, struct record_opts *opts, 902 struct callchain_param *param) 903 { 904 bool function = evsel__is_function_event(evsel); 905 struct perf_event_attr *attr = &evsel->core.attr; 906 907 evsel__set_sample_bit(evsel, CALLCHAIN); 908 909 attr->sample_max_stack = param->max_stack; 910 911 if (opts->kernel_callchains) 912 attr->exclude_callchain_user = 1; 913 if (opts->user_callchains) 914 attr->exclude_callchain_kernel = 1; 915 if (param->record_mode == CALLCHAIN_LBR) { 916 if (!opts->branch_stack) { 917 if (attr->exclude_user) { 918 pr_warning("LBR callstack option is only available " 919 "to get user callchain information. " 920 "Falling back to framepointers.\n"); 921 } else { 922 evsel__set_sample_bit(evsel, BRANCH_STACK); 923 attr->branch_sample_type = PERF_SAMPLE_BRANCH_USER | 924 PERF_SAMPLE_BRANCH_CALL_STACK | 925 PERF_SAMPLE_BRANCH_NO_CYCLES | 926 PERF_SAMPLE_BRANCH_NO_FLAGS | 927 PERF_SAMPLE_BRANCH_HW_INDEX; 928 } 929 } else 930 pr_warning("Cannot use LBR callstack with branch stack. " 931 "Falling back to framepointers.\n"); 932 } 933 934 if (param->record_mode == CALLCHAIN_DWARF) { 935 if (!function) { 936 evsel__set_sample_bit(evsel, REGS_USER); 937 evsel__set_sample_bit(evsel, STACK_USER); 938 if (opts->sample_user_regs && DWARF_MINIMAL_REGS != PERF_REGS_MASK) { 939 attr->sample_regs_user |= DWARF_MINIMAL_REGS; 940 pr_warning("WARNING: The use of --call-graph=dwarf may require all the user registers, " 941 "specifying a subset with --user-regs may render DWARF unwinding unreliable, " 942 "so the minimal registers set (IP, SP) is explicitly forced.\n"); 943 } else { 944 attr->sample_regs_user |= arch__user_reg_mask(); 945 } 946 attr->sample_stack_user = param->dump_size; 947 attr->exclude_callchain_user = 1; 948 } else { 949 pr_info("Cannot use DWARF unwind for function trace event," 950 " falling back to framepointers.\n"); 951 } 952 } 953 954 if (function) { 955 pr_info("Disabling user space callchains for function trace event.\n"); 956 attr->exclude_callchain_user = 1; 957 } 958 } 959 960 void evsel__config_callchain(struct evsel *evsel, struct record_opts *opts, 961 struct callchain_param *param) 962 { 963 if (param->enabled) 964 return __evsel__config_callchain(evsel, opts, param); 965 } 966 967 static void evsel__reset_callgraph(struct evsel *evsel, struct callchain_param *param) 968 { 969 struct perf_event_attr *attr = &evsel->core.attr; 970 971 evsel__reset_sample_bit(evsel, CALLCHAIN); 972 if (param->record_mode == CALLCHAIN_LBR) { 973 evsel__reset_sample_bit(evsel, BRANCH_STACK); 974 attr->branch_sample_type &= ~(PERF_SAMPLE_BRANCH_USER | 975 PERF_SAMPLE_BRANCH_CALL_STACK | 976 PERF_SAMPLE_BRANCH_HW_INDEX); 977 } 978 if (param->record_mode == CALLCHAIN_DWARF) { 979 evsel__reset_sample_bit(evsel, REGS_USER); 980 evsel__reset_sample_bit(evsel, STACK_USER); 981 } 982 } 983 984 static void evsel__apply_config_terms(struct evsel *evsel, 985 struct record_opts *opts, bool track) 986 { 987 struct evsel_config_term *term; 988 struct list_head *config_terms = &evsel->config_terms; 989 struct perf_event_attr *attr = &evsel->core.attr; 990 /* callgraph default */ 991 struct callchain_param param = { 992 .record_mode = callchain_param.record_mode, 993 }; 994 u32 dump_size = 0; 995 int max_stack = 0; 996 const char *callgraph_buf = NULL; 997 998 list_for_each_entry(term, config_terms, list) { 999 switch (term->type) { 1000 case EVSEL__CONFIG_TERM_PERIOD: 1001 if (!(term->weak && opts->user_interval != ULLONG_MAX)) { 1002 attr->sample_period = term->val.period; 1003 attr->freq = 0; 1004 evsel__reset_sample_bit(evsel, PERIOD); 1005 } 1006 break; 1007 case EVSEL__CONFIG_TERM_FREQ: 1008 if (!(term->weak && opts->user_freq != UINT_MAX)) { 1009 attr->sample_freq = term->val.freq; 1010 attr->freq = 1; 1011 evsel__set_sample_bit(evsel, PERIOD); 1012 } 1013 break; 1014 case EVSEL__CONFIG_TERM_TIME: 1015 if (term->val.time) 1016 evsel__set_sample_bit(evsel, TIME); 1017 else 1018 evsel__reset_sample_bit(evsel, TIME); 1019 break; 1020 case EVSEL__CONFIG_TERM_CALLGRAPH: 1021 callgraph_buf = term->val.str; 1022 break; 1023 case EVSEL__CONFIG_TERM_BRANCH: 1024 if (term->val.str && strcmp(term->val.str, "no")) { 1025 evsel__set_sample_bit(evsel, BRANCH_STACK); 1026 parse_branch_str(term->val.str, 1027 &attr->branch_sample_type); 1028 } else 1029 evsel__reset_sample_bit(evsel, BRANCH_STACK); 1030 break; 1031 case EVSEL__CONFIG_TERM_STACK_USER: 1032 dump_size = term->val.stack_user; 1033 break; 1034 case EVSEL__CONFIG_TERM_MAX_STACK: 1035 max_stack = term->val.max_stack; 1036 break; 1037 case EVSEL__CONFIG_TERM_MAX_EVENTS: 1038 evsel->max_events = term->val.max_events; 1039 break; 1040 case EVSEL__CONFIG_TERM_INHERIT: 1041 /* 1042 * attr->inherit should has already been set by 1043 * evsel__config. If user explicitly set 1044 * inherit using config terms, override global 1045 * opt->no_inherit setting. 1046 */ 1047 attr->inherit = term->val.inherit ? 1 : 0; 1048 break; 1049 case EVSEL__CONFIG_TERM_OVERWRITE: 1050 attr->write_backward = term->val.overwrite ? 1 : 0; 1051 break; 1052 case EVSEL__CONFIG_TERM_DRV_CFG: 1053 break; 1054 case EVSEL__CONFIG_TERM_PERCORE: 1055 break; 1056 case EVSEL__CONFIG_TERM_AUX_OUTPUT: 1057 attr->aux_output = term->val.aux_output ? 1 : 0; 1058 break; 1059 case EVSEL__CONFIG_TERM_AUX_SAMPLE_SIZE: 1060 /* Already applied by auxtrace */ 1061 break; 1062 case EVSEL__CONFIG_TERM_CFG_CHG: 1063 break; 1064 default: 1065 break; 1066 } 1067 } 1068 1069 /* User explicitly set per-event callgraph, clear the old setting and reset. */ 1070 if ((callgraph_buf != NULL) || (dump_size > 0) || max_stack) { 1071 bool sample_address = false; 1072 1073 if (max_stack) { 1074 param.max_stack = max_stack; 1075 if (callgraph_buf == NULL) 1076 callgraph_buf = "fp"; 1077 } 1078 1079 /* parse callgraph parameters */ 1080 if (callgraph_buf != NULL) { 1081 if (!strcmp(callgraph_buf, "no")) { 1082 param.enabled = false; 1083 param.record_mode = CALLCHAIN_NONE; 1084 } else { 1085 param.enabled = true; 1086 if (parse_callchain_record(callgraph_buf, ¶m)) { 1087 pr_err("per-event callgraph setting for %s failed. " 1088 "Apply callgraph global setting for it\n", 1089 evsel->name); 1090 return; 1091 } 1092 if (param.record_mode == CALLCHAIN_DWARF) 1093 sample_address = true; 1094 } 1095 } 1096 if (dump_size > 0) { 1097 dump_size = round_up(dump_size, sizeof(u64)); 1098 param.dump_size = dump_size; 1099 } 1100 1101 /* If global callgraph set, clear it */ 1102 if (callchain_param.enabled) 1103 evsel__reset_callgraph(evsel, &callchain_param); 1104 1105 /* set perf-event callgraph */ 1106 if (param.enabled) { 1107 if (sample_address) { 1108 evsel__set_sample_bit(evsel, ADDR); 1109 evsel__set_sample_bit(evsel, DATA_SRC); 1110 evsel->core.attr.mmap_data = track; 1111 } 1112 evsel__config_callchain(evsel, opts, ¶m); 1113 } 1114 } 1115 } 1116 1117 struct evsel_config_term *__evsel__get_config_term(struct evsel *evsel, enum evsel_term_type type) 1118 { 1119 struct evsel_config_term *term, *found_term = NULL; 1120 1121 list_for_each_entry(term, &evsel->config_terms, list) { 1122 if (term->type == type) 1123 found_term = term; 1124 } 1125 1126 return found_term; 1127 } 1128 1129 void __weak arch_evsel__set_sample_weight(struct evsel *evsel) 1130 { 1131 evsel__set_sample_bit(evsel, WEIGHT); 1132 } 1133 1134 void __weak arch_evsel__fixup_new_cycles(struct perf_event_attr *attr __maybe_unused) 1135 { 1136 } 1137 1138 void __weak arch__post_evsel_config(struct evsel *evsel __maybe_unused, 1139 struct perf_event_attr *attr __maybe_unused) 1140 { 1141 } 1142 1143 static void evsel__set_default_freq_period(struct record_opts *opts, 1144 struct perf_event_attr *attr) 1145 { 1146 if (opts->freq) { 1147 attr->freq = 1; 1148 attr->sample_freq = opts->freq; 1149 } else { 1150 attr->sample_period = opts->default_interval; 1151 } 1152 } 1153 1154 static bool evsel__is_offcpu_event(struct evsel *evsel) 1155 { 1156 return evsel__is_bpf_output(evsel) && evsel__name_is(evsel, OFFCPU_EVENT); 1157 } 1158 1159 /* 1160 * The enable_on_exec/disabled value strategy: 1161 * 1162 * 1) For any type of traced program: 1163 * - all independent events and group leaders are disabled 1164 * - all group members are enabled 1165 * 1166 * Group members are ruled by group leaders. They need to 1167 * be enabled, because the group scheduling relies on that. 1168 * 1169 * 2) For traced programs executed by perf: 1170 * - all independent events and group leaders have 1171 * enable_on_exec set 1172 * - we don't specifically enable or disable any event during 1173 * the record command 1174 * 1175 * Independent events and group leaders are initially disabled 1176 * and get enabled by exec. Group members are ruled by group 1177 * leaders as stated in 1). 1178 * 1179 * 3) For traced programs attached by perf (pid/tid): 1180 * - we specifically enable or disable all events during 1181 * the record command 1182 * 1183 * When attaching events to already running traced we 1184 * enable/disable events specifically, as there's no 1185 * initial traced exec call. 1186 */ 1187 void evsel__config(struct evsel *evsel, struct record_opts *opts, 1188 struct callchain_param *callchain) 1189 { 1190 struct evsel *leader = evsel__leader(evsel); 1191 struct perf_event_attr *attr = &evsel->core.attr; 1192 int track = evsel->tracking; 1193 bool per_cpu = opts->target.default_per_cpu && !opts->target.per_thread; 1194 1195 attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1; 1196 attr->inherit = !opts->no_inherit; 1197 attr->write_backward = opts->overwrite ? 1 : 0; 1198 attr->read_format = PERF_FORMAT_LOST; 1199 1200 evsel__set_sample_bit(evsel, IP); 1201 evsel__set_sample_bit(evsel, TID); 1202 1203 if (evsel->sample_read) { 1204 evsel__set_sample_bit(evsel, READ); 1205 1206 /* 1207 * We need ID even in case of single event, because 1208 * PERF_SAMPLE_READ process ID specific data. 1209 */ 1210 evsel__set_sample_id(evsel, false); 1211 1212 /* 1213 * Apply group format only if we belong to group 1214 * with more than one members. 1215 */ 1216 if (leader->core.nr_members > 1) { 1217 attr->read_format |= PERF_FORMAT_GROUP; 1218 attr->inherit = 0; 1219 } 1220 } 1221 1222 /* 1223 * We default some events to have a default interval. But keep 1224 * it a weak assumption overridable by the user. 1225 */ 1226 if ((evsel->is_libpfm_event && !attr->sample_period) || 1227 (!evsel->is_libpfm_event && (!attr->sample_period || 1228 opts->user_freq != UINT_MAX || 1229 opts->user_interval != ULLONG_MAX))) 1230 evsel__set_default_freq_period(opts, attr); 1231 1232 /* 1233 * If attr->freq was set (here or earlier), ask for period 1234 * to be sampled. 1235 */ 1236 if (attr->freq) 1237 evsel__set_sample_bit(evsel, PERIOD); 1238 1239 if (opts->no_samples) 1240 attr->sample_freq = 0; 1241 1242 if (opts->inherit_stat) { 1243 evsel->core.attr.read_format |= 1244 PERF_FORMAT_TOTAL_TIME_ENABLED | 1245 PERF_FORMAT_TOTAL_TIME_RUNNING | 1246 PERF_FORMAT_ID; 1247 attr->inherit_stat = 1; 1248 } 1249 1250 if (opts->sample_address) { 1251 evsel__set_sample_bit(evsel, ADDR); 1252 attr->mmap_data = track; 1253 } 1254 1255 /* 1256 * We don't allow user space callchains for function trace 1257 * event, due to issues with page faults while tracing page 1258 * fault handler and its overall trickiness nature. 1259 */ 1260 if (evsel__is_function_event(evsel)) 1261 evsel->core.attr.exclude_callchain_user = 1; 1262 1263 if (callchain && callchain->enabled && !evsel->no_aux_samples) 1264 evsel__config_callchain(evsel, opts, callchain); 1265 1266 if (opts->sample_intr_regs && !evsel->no_aux_samples && 1267 !evsel__is_dummy_event(evsel)) { 1268 attr->sample_regs_intr = opts->sample_intr_regs; 1269 evsel__set_sample_bit(evsel, REGS_INTR); 1270 } 1271 1272 if (opts->sample_user_regs && !evsel->no_aux_samples && 1273 !evsel__is_dummy_event(evsel)) { 1274 attr->sample_regs_user |= opts->sample_user_regs; 1275 evsel__set_sample_bit(evsel, REGS_USER); 1276 } 1277 1278 if (target__has_cpu(&opts->target) || opts->sample_cpu) 1279 evsel__set_sample_bit(evsel, CPU); 1280 1281 /* 1282 * When the user explicitly disabled time don't force it here. 1283 */ 1284 if (opts->sample_time && 1285 (!perf_missing_features.sample_id_all && 1286 (!opts->no_inherit || target__has_cpu(&opts->target) || per_cpu || 1287 opts->sample_time_set))) 1288 evsel__set_sample_bit(evsel, TIME); 1289 1290 if (opts->raw_samples && !evsel->no_aux_samples) { 1291 evsel__set_sample_bit(evsel, TIME); 1292 evsel__set_sample_bit(evsel, RAW); 1293 evsel__set_sample_bit(evsel, CPU); 1294 } 1295 1296 if (opts->sample_address) 1297 evsel__set_sample_bit(evsel, DATA_SRC); 1298 1299 if (opts->sample_phys_addr) 1300 evsel__set_sample_bit(evsel, PHYS_ADDR); 1301 1302 if (opts->no_buffering) { 1303 attr->watermark = 0; 1304 attr->wakeup_events = 1; 1305 } 1306 if (opts->branch_stack && !evsel->no_aux_samples) { 1307 evsel__set_sample_bit(evsel, BRANCH_STACK); 1308 attr->branch_sample_type = opts->branch_stack; 1309 } 1310 1311 if (opts->sample_weight) 1312 arch_evsel__set_sample_weight(evsel); 1313 1314 attr->task = track; 1315 attr->mmap = track; 1316 attr->mmap2 = track && !perf_missing_features.mmap2; 1317 attr->comm = track; 1318 attr->build_id = track && opts->build_id; 1319 1320 /* 1321 * ksymbol is tracked separately with text poke because it needs to be 1322 * system wide and enabled immediately. 1323 */ 1324 if (!opts->text_poke) 1325 attr->ksymbol = track && !perf_missing_features.ksymbol; 1326 attr->bpf_event = track && !opts->no_bpf_event && !perf_missing_features.bpf; 1327 1328 if (opts->record_namespaces) 1329 attr->namespaces = track; 1330 1331 if (opts->record_cgroup) { 1332 attr->cgroup = track && !perf_missing_features.cgroup; 1333 evsel__set_sample_bit(evsel, CGROUP); 1334 } 1335 1336 if (opts->sample_data_page_size) 1337 evsel__set_sample_bit(evsel, DATA_PAGE_SIZE); 1338 1339 if (opts->sample_code_page_size) 1340 evsel__set_sample_bit(evsel, CODE_PAGE_SIZE); 1341 1342 if (opts->record_switch_events) 1343 attr->context_switch = track; 1344 1345 if (opts->sample_transaction) 1346 evsel__set_sample_bit(evsel, TRANSACTION); 1347 1348 if (opts->running_time) { 1349 evsel->core.attr.read_format |= 1350 PERF_FORMAT_TOTAL_TIME_ENABLED | 1351 PERF_FORMAT_TOTAL_TIME_RUNNING; 1352 } 1353 1354 /* 1355 * XXX see the function comment above 1356 * 1357 * Disabling only independent events or group leaders, 1358 * keeping group members enabled. 1359 */ 1360 if (evsel__is_group_leader(evsel)) 1361 attr->disabled = 1; 1362 1363 /* 1364 * Setting enable_on_exec for independent events and 1365 * group leaders for traced executed by perf. 1366 */ 1367 if (target__none(&opts->target) && evsel__is_group_leader(evsel) && 1368 !opts->target.initial_delay) 1369 attr->enable_on_exec = 1; 1370 1371 if (evsel->immediate) { 1372 attr->disabled = 0; 1373 attr->enable_on_exec = 0; 1374 } 1375 1376 clockid = opts->clockid; 1377 if (opts->use_clockid) { 1378 attr->use_clockid = 1; 1379 attr->clockid = opts->clockid; 1380 } 1381 1382 if (evsel->precise_max) 1383 attr->precise_ip = 3; 1384 1385 if (opts->all_user) { 1386 attr->exclude_kernel = 1; 1387 attr->exclude_user = 0; 1388 } 1389 1390 if (opts->all_kernel) { 1391 attr->exclude_kernel = 0; 1392 attr->exclude_user = 1; 1393 } 1394 1395 if (evsel->core.own_cpus || evsel->unit) 1396 evsel->core.attr.read_format |= PERF_FORMAT_ID; 1397 1398 /* 1399 * Apply event specific term settings, 1400 * it overloads any global configuration. 1401 */ 1402 evsel__apply_config_terms(evsel, opts, track); 1403 1404 evsel->ignore_missing_thread = opts->ignore_missing_thread; 1405 1406 /* The --period option takes the precedence. */ 1407 if (opts->period_set) { 1408 if (opts->period) 1409 evsel__set_sample_bit(evsel, PERIOD); 1410 else 1411 evsel__reset_sample_bit(evsel, PERIOD); 1412 } 1413 1414 /* 1415 * A dummy event never triggers any actual counter and therefore 1416 * cannot be used with branch_stack. 1417 * 1418 * For initial_delay, a dummy event is added implicitly. 1419 * The software event will trigger -EOPNOTSUPP error out, 1420 * if BRANCH_STACK bit is set. 1421 */ 1422 if (evsel__is_dummy_event(evsel)) 1423 evsel__reset_sample_bit(evsel, BRANCH_STACK); 1424 1425 if (evsel__is_offcpu_event(evsel)) 1426 evsel->core.attr.sample_type &= OFFCPU_SAMPLE_TYPES; 1427 1428 arch__post_evsel_config(evsel, attr); 1429 } 1430 1431 int evsel__set_filter(struct evsel *evsel, const char *filter) 1432 { 1433 char *new_filter = strdup(filter); 1434 1435 if (new_filter != NULL) { 1436 free(evsel->filter); 1437 evsel->filter = new_filter; 1438 return 0; 1439 } 1440 1441 return -1; 1442 } 1443 1444 static int evsel__append_filter(struct evsel *evsel, const char *fmt, const char *filter) 1445 { 1446 char *new_filter; 1447 1448 if (evsel->filter == NULL) 1449 return evsel__set_filter(evsel, filter); 1450 1451 if (asprintf(&new_filter, fmt, evsel->filter, filter) > 0) { 1452 free(evsel->filter); 1453 evsel->filter = new_filter; 1454 return 0; 1455 } 1456 1457 return -1; 1458 } 1459 1460 int evsel__append_tp_filter(struct evsel *evsel, const char *filter) 1461 { 1462 return evsel__append_filter(evsel, "(%s) && (%s)", filter); 1463 } 1464 1465 int evsel__append_addr_filter(struct evsel *evsel, const char *filter) 1466 { 1467 return evsel__append_filter(evsel, "%s,%s", filter); 1468 } 1469 1470 /* Caller has to clear disabled after going through all CPUs. */ 1471 int evsel__enable_cpu(struct evsel *evsel, int cpu_map_idx) 1472 { 1473 return perf_evsel__enable_cpu(&evsel->core, cpu_map_idx); 1474 } 1475 1476 int evsel__enable(struct evsel *evsel) 1477 { 1478 int err = perf_evsel__enable(&evsel->core); 1479 1480 if (!err) 1481 evsel->disabled = false; 1482 return err; 1483 } 1484 1485 /* Caller has to set disabled after going through all CPUs. */ 1486 int evsel__disable_cpu(struct evsel *evsel, int cpu_map_idx) 1487 { 1488 return perf_evsel__disable_cpu(&evsel->core, cpu_map_idx); 1489 } 1490 1491 int evsel__disable(struct evsel *evsel) 1492 { 1493 int err = perf_evsel__disable(&evsel->core); 1494 /* 1495 * We mark it disabled here so that tools that disable a event can 1496 * ignore events after they disable it. I.e. the ring buffer may have 1497 * already a few more events queued up before the kernel got the stop 1498 * request. 1499 */ 1500 if (!err) 1501 evsel->disabled = true; 1502 1503 return err; 1504 } 1505 1506 void free_config_terms(struct list_head *config_terms) 1507 { 1508 struct evsel_config_term *term, *h; 1509 1510 list_for_each_entry_safe(term, h, config_terms, list) { 1511 list_del_init(&term->list); 1512 if (term->free_str) 1513 zfree(&term->val.str); 1514 free(term); 1515 } 1516 } 1517 1518 static void evsel__free_config_terms(struct evsel *evsel) 1519 { 1520 free_config_terms(&evsel->config_terms); 1521 } 1522 1523 void evsel__exit(struct evsel *evsel) 1524 { 1525 assert(list_empty(&evsel->core.node)); 1526 assert(evsel->evlist == NULL); 1527 bpf_counter__destroy(evsel); 1528 perf_bpf_filter__destroy(evsel); 1529 evsel__free_counts(evsel); 1530 perf_evsel__free_fd(&evsel->core); 1531 perf_evsel__free_id(&evsel->core); 1532 evsel__free_config_terms(evsel); 1533 cgroup__put(evsel->cgrp); 1534 perf_cpu_map__put(evsel->core.cpus); 1535 perf_cpu_map__put(evsel->core.own_cpus); 1536 perf_thread_map__put(evsel->core.threads); 1537 zfree(&evsel->group_name); 1538 zfree(&evsel->name); 1539 zfree(&evsel->pmu_name); 1540 zfree(&evsel->unit); 1541 zfree(&evsel->metric_id); 1542 evsel__zero_per_pkg(evsel); 1543 hashmap__free(evsel->per_pkg_mask); 1544 evsel->per_pkg_mask = NULL; 1545 zfree(&evsel->metric_events); 1546 perf_evsel__object.fini(evsel); 1547 } 1548 1549 void evsel__delete(struct evsel *evsel) 1550 { 1551 if (!evsel) 1552 return; 1553 1554 evsel__exit(evsel); 1555 free(evsel); 1556 } 1557 1558 void evsel__compute_deltas(struct evsel *evsel, int cpu_map_idx, int thread, 1559 struct perf_counts_values *count) 1560 { 1561 struct perf_counts_values tmp; 1562 1563 if (!evsel->prev_raw_counts) 1564 return; 1565 1566 tmp = *perf_counts(evsel->prev_raw_counts, cpu_map_idx, thread); 1567 *perf_counts(evsel->prev_raw_counts, cpu_map_idx, thread) = *count; 1568 1569 count->val = count->val - tmp.val; 1570 count->ena = count->ena - tmp.ena; 1571 count->run = count->run - tmp.run; 1572 } 1573 1574 static int evsel__read_one(struct evsel *evsel, int cpu_map_idx, int thread) 1575 { 1576 struct perf_counts_values *count = perf_counts(evsel->counts, cpu_map_idx, thread); 1577 1578 return perf_evsel__read(&evsel->core, cpu_map_idx, thread, count); 1579 } 1580 1581 static void evsel__set_count(struct evsel *counter, int cpu_map_idx, int thread, 1582 u64 val, u64 ena, u64 run, u64 lost) 1583 { 1584 struct perf_counts_values *count; 1585 1586 count = perf_counts(counter->counts, cpu_map_idx, thread); 1587 1588 count->val = val; 1589 count->ena = ena; 1590 count->run = run; 1591 count->lost = lost; 1592 1593 perf_counts__set_loaded(counter->counts, cpu_map_idx, thread, true); 1594 } 1595 1596 static int evsel__process_group_data(struct evsel *leader, int cpu_map_idx, int thread, u64 *data) 1597 { 1598 u64 read_format = leader->core.attr.read_format; 1599 struct sample_read_value *v; 1600 u64 nr, ena = 0, run = 0, lost = 0; 1601 1602 nr = *data++; 1603 1604 if (nr != (u64) leader->core.nr_members) 1605 return -EINVAL; 1606 1607 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 1608 ena = *data++; 1609 1610 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 1611 run = *data++; 1612 1613 v = (void *)data; 1614 sample_read_group__for_each(v, nr, read_format) { 1615 struct evsel *counter; 1616 1617 counter = evlist__id2evsel(leader->evlist, v->id); 1618 if (!counter) 1619 return -EINVAL; 1620 1621 if (read_format & PERF_FORMAT_LOST) 1622 lost = v->lost; 1623 1624 evsel__set_count(counter, cpu_map_idx, thread, v->value, ena, run, lost); 1625 } 1626 1627 return 0; 1628 } 1629 1630 static int evsel__read_group(struct evsel *leader, int cpu_map_idx, int thread) 1631 { 1632 struct perf_stat_evsel *ps = leader->stats; 1633 u64 read_format = leader->core.attr.read_format; 1634 int size = perf_evsel__read_size(&leader->core); 1635 u64 *data = ps->group_data; 1636 1637 if (!(read_format & PERF_FORMAT_ID)) 1638 return -EINVAL; 1639 1640 if (!evsel__is_group_leader(leader)) 1641 return -EINVAL; 1642 1643 if (!data) { 1644 data = zalloc(size); 1645 if (!data) 1646 return -ENOMEM; 1647 1648 ps->group_data = data; 1649 } 1650 1651 if (FD(leader, cpu_map_idx, thread) < 0) 1652 return -EINVAL; 1653 1654 if (readn(FD(leader, cpu_map_idx, thread), data, size) <= 0) 1655 return -errno; 1656 1657 return evsel__process_group_data(leader, cpu_map_idx, thread, data); 1658 } 1659 1660 int evsel__read_counter(struct evsel *evsel, int cpu_map_idx, int thread) 1661 { 1662 u64 read_format = evsel->core.attr.read_format; 1663 1664 if (read_format & PERF_FORMAT_GROUP) 1665 return evsel__read_group(evsel, cpu_map_idx, thread); 1666 1667 return evsel__read_one(evsel, cpu_map_idx, thread); 1668 } 1669 1670 int __evsel__read_on_cpu(struct evsel *evsel, int cpu_map_idx, int thread, bool scale) 1671 { 1672 struct perf_counts_values count; 1673 size_t nv = scale ? 3 : 1; 1674 1675 if (FD(evsel, cpu_map_idx, thread) < 0) 1676 return -EINVAL; 1677 1678 if (evsel->counts == NULL && evsel__alloc_counts(evsel) < 0) 1679 return -ENOMEM; 1680 1681 if (readn(FD(evsel, cpu_map_idx, thread), &count, nv * sizeof(u64)) <= 0) 1682 return -errno; 1683 1684 evsel__compute_deltas(evsel, cpu_map_idx, thread, &count); 1685 perf_counts_values__scale(&count, scale, NULL); 1686 *perf_counts(evsel->counts, cpu_map_idx, thread) = count; 1687 return 0; 1688 } 1689 1690 static int evsel__match_other_cpu(struct evsel *evsel, struct evsel *other, 1691 int cpu_map_idx) 1692 { 1693 struct perf_cpu cpu; 1694 1695 cpu = perf_cpu_map__cpu(evsel->core.cpus, cpu_map_idx); 1696 return perf_cpu_map__idx(other->core.cpus, cpu); 1697 } 1698 1699 static int evsel__hybrid_group_cpu_map_idx(struct evsel *evsel, int cpu_map_idx) 1700 { 1701 struct evsel *leader = evsel__leader(evsel); 1702 1703 if ((evsel__is_hybrid(evsel) && !evsel__is_hybrid(leader)) || 1704 (!evsel__is_hybrid(evsel) && evsel__is_hybrid(leader))) { 1705 return evsel__match_other_cpu(evsel, leader, cpu_map_idx); 1706 } 1707 1708 return cpu_map_idx; 1709 } 1710 1711 static int get_group_fd(struct evsel *evsel, int cpu_map_idx, int thread) 1712 { 1713 struct evsel *leader = evsel__leader(evsel); 1714 int fd; 1715 1716 if (evsel__is_group_leader(evsel)) 1717 return -1; 1718 1719 /* 1720 * Leader must be already processed/open, 1721 * if not it's a bug. 1722 */ 1723 BUG_ON(!leader->core.fd); 1724 1725 cpu_map_idx = evsel__hybrid_group_cpu_map_idx(evsel, cpu_map_idx); 1726 if (cpu_map_idx == -1) 1727 return -1; 1728 1729 fd = FD(leader, cpu_map_idx, thread); 1730 BUG_ON(fd == -1 && !leader->skippable); 1731 1732 /* 1733 * When the leader has been skipped, return -2 to distinguish from no 1734 * group leader case. 1735 */ 1736 return fd == -1 ? -2 : fd; 1737 } 1738 1739 static void evsel__remove_fd(struct evsel *pos, int nr_cpus, int nr_threads, int thread_idx) 1740 { 1741 for (int cpu = 0; cpu < nr_cpus; cpu++) 1742 for (int thread = thread_idx; thread < nr_threads - 1; thread++) 1743 FD(pos, cpu, thread) = FD(pos, cpu, thread + 1); 1744 } 1745 1746 static int update_fds(struct evsel *evsel, 1747 int nr_cpus, int cpu_map_idx, 1748 int nr_threads, int thread_idx) 1749 { 1750 struct evsel *pos; 1751 1752 if (cpu_map_idx >= nr_cpus || thread_idx >= nr_threads) 1753 return -EINVAL; 1754 1755 evlist__for_each_entry(evsel->evlist, pos) { 1756 nr_cpus = pos != evsel ? nr_cpus : cpu_map_idx; 1757 1758 evsel__remove_fd(pos, nr_cpus, nr_threads, thread_idx); 1759 1760 /* 1761 * Since fds for next evsel has not been created, 1762 * there is no need to iterate whole event list. 1763 */ 1764 if (pos == evsel) 1765 break; 1766 } 1767 return 0; 1768 } 1769 1770 static bool evsel__ignore_missing_thread(struct evsel *evsel, 1771 int nr_cpus, int cpu_map_idx, 1772 struct perf_thread_map *threads, 1773 int thread, int err) 1774 { 1775 pid_t ignore_pid = perf_thread_map__pid(threads, thread); 1776 1777 if (!evsel->ignore_missing_thread) 1778 return false; 1779 1780 /* The system wide setup does not work with threads. */ 1781 if (evsel->core.system_wide) 1782 return false; 1783 1784 /* The -ESRCH is perf event syscall errno for pid's not found. */ 1785 if (err != -ESRCH) 1786 return false; 1787 1788 /* If there's only one thread, let it fail. */ 1789 if (threads->nr == 1) 1790 return false; 1791 1792 /* 1793 * We should remove fd for missing_thread first 1794 * because thread_map__remove() will decrease threads->nr. 1795 */ 1796 if (update_fds(evsel, nr_cpus, cpu_map_idx, threads->nr, thread)) 1797 return false; 1798 1799 if (thread_map__remove(threads, thread)) 1800 return false; 1801 1802 pr_warning("WARNING: Ignored open failure for pid %d\n", 1803 ignore_pid); 1804 return true; 1805 } 1806 1807 static int __open_attr__fprintf(FILE *fp, const char *name, const char *val, 1808 void *priv __maybe_unused) 1809 { 1810 return fprintf(fp, " %-32s %s\n", name, val); 1811 } 1812 1813 static void display_attr(struct perf_event_attr *attr) 1814 { 1815 if (verbose >= 2 || debug_peo_args) { 1816 fprintf(stderr, "%.60s\n", graph_dotted_line); 1817 fprintf(stderr, "perf_event_attr:\n"); 1818 perf_event_attr__fprintf(stderr, attr, __open_attr__fprintf, NULL); 1819 fprintf(stderr, "%.60s\n", graph_dotted_line); 1820 } 1821 } 1822 1823 bool evsel__precise_ip_fallback(struct evsel *evsel) 1824 { 1825 /* Do not try less precise if not requested. */ 1826 if (!evsel->precise_max) 1827 return false; 1828 1829 /* 1830 * We tried all the precise_ip values, and it's 1831 * still failing, so leave it to standard fallback. 1832 */ 1833 if (!evsel->core.attr.precise_ip) { 1834 evsel->core.attr.precise_ip = evsel->precise_ip_original; 1835 return false; 1836 } 1837 1838 if (!evsel->precise_ip_original) 1839 evsel->precise_ip_original = evsel->core.attr.precise_ip; 1840 1841 evsel->core.attr.precise_ip--; 1842 pr_debug2_peo("decreasing precise_ip by one (%d)\n", evsel->core.attr.precise_ip); 1843 display_attr(&evsel->core.attr); 1844 return true; 1845 } 1846 1847 static struct perf_cpu_map *empty_cpu_map; 1848 static struct perf_thread_map *empty_thread_map; 1849 1850 static int __evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus, 1851 struct perf_thread_map *threads) 1852 { 1853 int nthreads = perf_thread_map__nr(threads); 1854 1855 if ((perf_missing_features.write_backward && evsel->core.attr.write_backward) || 1856 (perf_missing_features.aux_output && evsel->core.attr.aux_output)) 1857 return -EINVAL; 1858 1859 if (cpus == NULL) { 1860 if (empty_cpu_map == NULL) { 1861 empty_cpu_map = perf_cpu_map__dummy_new(); 1862 if (empty_cpu_map == NULL) 1863 return -ENOMEM; 1864 } 1865 1866 cpus = empty_cpu_map; 1867 } 1868 1869 if (threads == NULL) { 1870 if (empty_thread_map == NULL) { 1871 empty_thread_map = thread_map__new_by_tid(-1); 1872 if (empty_thread_map == NULL) 1873 return -ENOMEM; 1874 } 1875 1876 threads = empty_thread_map; 1877 } 1878 1879 if (evsel->core.fd == NULL && 1880 perf_evsel__alloc_fd(&evsel->core, perf_cpu_map__nr(cpus), nthreads) < 0) 1881 return -ENOMEM; 1882 1883 evsel->open_flags = PERF_FLAG_FD_CLOEXEC; 1884 if (evsel->cgrp) 1885 evsel->open_flags |= PERF_FLAG_PID_CGROUP; 1886 1887 return 0; 1888 } 1889 1890 static void evsel__disable_missing_features(struct evsel *evsel) 1891 { 1892 if (perf_missing_features.read_lost) 1893 evsel->core.attr.read_format &= ~PERF_FORMAT_LOST; 1894 if (perf_missing_features.weight_struct) { 1895 evsel__set_sample_bit(evsel, WEIGHT); 1896 evsel__reset_sample_bit(evsel, WEIGHT_STRUCT); 1897 } 1898 if (perf_missing_features.clockid_wrong) 1899 evsel->core.attr.clockid = CLOCK_MONOTONIC; /* should always work */ 1900 if (perf_missing_features.clockid) { 1901 evsel->core.attr.use_clockid = 0; 1902 evsel->core.attr.clockid = 0; 1903 } 1904 if (perf_missing_features.cloexec) 1905 evsel->open_flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC; 1906 if (perf_missing_features.mmap2) 1907 evsel->core.attr.mmap2 = 0; 1908 if (evsel->pmu && evsel->pmu->missing_features.exclude_guest) 1909 evsel->core.attr.exclude_guest = evsel->core.attr.exclude_host = 0; 1910 if (perf_missing_features.lbr_flags) 1911 evsel->core.attr.branch_sample_type &= ~(PERF_SAMPLE_BRANCH_NO_FLAGS | 1912 PERF_SAMPLE_BRANCH_NO_CYCLES); 1913 if (perf_missing_features.group_read && evsel->core.attr.inherit) 1914 evsel->core.attr.read_format &= ~(PERF_FORMAT_GROUP|PERF_FORMAT_ID); 1915 if (perf_missing_features.ksymbol) 1916 evsel->core.attr.ksymbol = 0; 1917 if (perf_missing_features.bpf) 1918 evsel->core.attr.bpf_event = 0; 1919 if (perf_missing_features.branch_hw_idx) 1920 evsel->core.attr.branch_sample_type &= ~PERF_SAMPLE_BRANCH_HW_INDEX; 1921 if (perf_missing_features.sample_id_all) 1922 evsel->core.attr.sample_id_all = 0; 1923 } 1924 1925 int evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus, 1926 struct perf_thread_map *threads) 1927 { 1928 int err; 1929 1930 err = __evsel__prepare_open(evsel, cpus, threads); 1931 if (err) 1932 return err; 1933 1934 evsel__disable_missing_features(evsel); 1935 1936 return err; 1937 } 1938 1939 bool evsel__detect_missing_features(struct evsel *evsel) 1940 { 1941 /* 1942 * Must probe features in the order they were added to the 1943 * perf_event_attr interface. 1944 */ 1945 if (!perf_missing_features.read_lost && 1946 (evsel->core.attr.read_format & PERF_FORMAT_LOST)) { 1947 perf_missing_features.read_lost = true; 1948 pr_debug2("switching off PERF_FORMAT_LOST support\n"); 1949 return true; 1950 } else if (!perf_missing_features.weight_struct && 1951 (evsel->core.attr.sample_type & PERF_SAMPLE_WEIGHT_STRUCT)) { 1952 perf_missing_features.weight_struct = true; 1953 pr_debug2("switching off weight struct support\n"); 1954 return true; 1955 } else if (!perf_missing_features.code_page_size && 1956 (evsel->core.attr.sample_type & PERF_SAMPLE_CODE_PAGE_SIZE)) { 1957 perf_missing_features.code_page_size = true; 1958 pr_debug2_peo("Kernel has no PERF_SAMPLE_CODE_PAGE_SIZE support, bailing out\n"); 1959 return false; 1960 } else if (!perf_missing_features.data_page_size && 1961 (evsel->core.attr.sample_type & PERF_SAMPLE_DATA_PAGE_SIZE)) { 1962 perf_missing_features.data_page_size = true; 1963 pr_debug2_peo("Kernel has no PERF_SAMPLE_DATA_PAGE_SIZE support, bailing out\n"); 1964 return false; 1965 } else if (!perf_missing_features.cgroup && evsel->core.attr.cgroup) { 1966 perf_missing_features.cgroup = true; 1967 pr_debug2_peo("Kernel has no cgroup sampling support, bailing out\n"); 1968 return false; 1969 } else if (!perf_missing_features.branch_hw_idx && 1970 (evsel->core.attr.branch_sample_type & PERF_SAMPLE_BRANCH_HW_INDEX)) { 1971 perf_missing_features.branch_hw_idx = true; 1972 pr_debug2("switching off branch HW index support\n"); 1973 return true; 1974 } else if (!perf_missing_features.aux_output && evsel->core.attr.aux_output) { 1975 perf_missing_features.aux_output = true; 1976 pr_debug2_peo("Kernel has no attr.aux_output support, bailing out\n"); 1977 return false; 1978 } else if (!perf_missing_features.bpf && evsel->core.attr.bpf_event) { 1979 perf_missing_features.bpf = true; 1980 pr_debug2_peo("switching off bpf_event\n"); 1981 return true; 1982 } else if (!perf_missing_features.ksymbol && evsel->core.attr.ksymbol) { 1983 perf_missing_features.ksymbol = true; 1984 pr_debug2_peo("switching off ksymbol\n"); 1985 return true; 1986 } else if (!perf_missing_features.write_backward && evsel->core.attr.write_backward) { 1987 perf_missing_features.write_backward = true; 1988 pr_debug2_peo("switching off write_backward\n"); 1989 return false; 1990 } else if (!perf_missing_features.clockid_wrong && evsel->core.attr.use_clockid) { 1991 perf_missing_features.clockid_wrong = true; 1992 pr_debug2_peo("switching off clockid\n"); 1993 return true; 1994 } else if (!perf_missing_features.clockid && evsel->core.attr.use_clockid) { 1995 perf_missing_features.clockid = true; 1996 pr_debug2_peo("switching off use_clockid\n"); 1997 return true; 1998 } else if (!perf_missing_features.cloexec && (evsel->open_flags & PERF_FLAG_FD_CLOEXEC)) { 1999 perf_missing_features.cloexec = true; 2000 pr_debug2_peo("switching off cloexec flag\n"); 2001 return true; 2002 } else if (!perf_missing_features.mmap2 && evsel->core.attr.mmap2) { 2003 perf_missing_features.mmap2 = true; 2004 pr_debug2_peo("switching off mmap2\n"); 2005 return true; 2006 } else if (evsel->core.attr.exclude_guest || evsel->core.attr.exclude_host) { 2007 if (evsel->pmu == NULL) 2008 evsel->pmu = evsel__find_pmu(evsel); 2009 2010 if (evsel->pmu) 2011 evsel->pmu->missing_features.exclude_guest = true; 2012 else { 2013 /* we cannot find PMU, disable attrs now */ 2014 evsel->core.attr.exclude_host = false; 2015 evsel->core.attr.exclude_guest = false; 2016 } 2017 2018 if (evsel->exclude_GH) { 2019 pr_debug2_peo("PMU has no exclude_host/guest support, bailing out\n"); 2020 return false; 2021 } 2022 if (!perf_missing_features.exclude_guest) { 2023 perf_missing_features.exclude_guest = true; 2024 pr_debug2_peo("switching off exclude_guest, exclude_host\n"); 2025 } 2026 return true; 2027 } else if (!perf_missing_features.sample_id_all) { 2028 perf_missing_features.sample_id_all = true; 2029 pr_debug2_peo("switching off sample_id_all\n"); 2030 return true; 2031 } else if (!perf_missing_features.lbr_flags && 2032 (evsel->core.attr.branch_sample_type & 2033 (PERF_SAMPLE_BRANCH_NO_CYCLES | 2034 PERF_SAMPLE_BRANCH_NO_FLAGS))) { 2035 perf_missing_features.lbr_flags = true; 2036 pr_debug2_peo("switching off branch sample type no (cycles/flags)\n"); 2037 return true; 2038 } else if (!perf_missing_features.group_read && 2039 evsel->core.attr.inherit && 2040 (evsel->core.attr.read_format & PERF_FORMAT_GROUP) && 2041 evsel__is_group_leader(evsel)) { 2042 perf_missing_features.group_read = true; 2043 pr_debug2_peo("switching off group read\n"); 2044 return true; 2045 } else { 2046 return false; 2047 } 2048 } 2049 2050 bool evsel__increase_rlimit(enum rlimit_action *set_rlimit) 2051 { 2052 int old_errno; 2053 struct rlimit l; 2054 2055 if (*set_rlimit < INCREASED_MAX) { 2056 old_errno = errno; 2057 2058 if (getrlimit(RLIMIT_NOFILE, &l) == 0) { 2059 if (*set_rlimit == NO_CHANGE) { 2060 l.rlim_cur = l.rlim_max; 2061 } else { 2062 l.rlim_cur = l.rlim_max + 1000; 2063 l.rlim_max = l.rlim_cur; 2064 } 2065 if (setrlimit(RLIMIT_NOFILE, &l) == 0) { 2066 (*set_rlimit) += 1; 2067 errno = old_errno; 2068 return true; 2069 } 2070 } 2071 errno = old_errno; 2072 } 2073 2074 return false; 2075 } 2076 2077 static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus, 2078 struct perf_thread_map *threads, 2079 int start_cpu_map_idx, int end_cpu_map_idx) 2080 { 2081 int idx, thread, nthreads; 2082 int pid = -1, err, old_errno; 2083 enum rlimit_action set_rlimit = NO_CHANGE; 2084 2085 err = __evsel__prepare_open(evsel, cpus, threads); 2086 if (err) 2087 return err; 2088 2089 if (cpus == NULL) 2090 cpus = empty_cpu_map; 2091 2092 if (threads == NULL) 2093 threads = empty_thread_map; 2094 2095 nthreads = perf_thread_map__nr(threads); 2096 2097 if (evsel->cgrp) 2098 pid = evsel->cgrp->fd; 2099 2100 fallback_missing_features: 2101 evsel__disable_missing_features(evsel); 2102 2103 display_attr(&evsel->core.attr); 2104 2105 for (idx = start_cpu_map_idx; idx < end_cpu_map_idx; idx++) { 2106 2107 for (thread = 0; thread < nthreads; thread++) { 2108 int fd, group_fd; 2109 retry_open: 2110 if (thread >= nthreads) 2111 break; 2112 2113 if (!evsel->cgrp && !evsel->core.system_wide) 2114 pid = perf_thread_map__pid(threads, thread); 2115 2116 group_fd = get_group_fd(evsel, idx, thread); 2117 2118 if (group_fd == -2) { 2119 pr_debug("broken group leader for %s\n", evsel->name); 2120 err = -EINVAL; 2121 goto out_close; 2122 } 2123 2124 test_attr__ready(); 2125 2126 /* Debug message used by test scripts */ 2127 pr_debug2_peo("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx", 2128 pid, perf_cpu_map__cpu(cpus, idx).cpu, group_fd, evsel->open_flags); 2129 2130 fd = sys_perf_event_open(&evsel->core.attr, pid, 2131 perf_cpu_map__cpu(cpus, idx).cpu, 2132 group_fd, evsel->open_flags); 2133 2134 FD(evsel, idx, thread) = fd; 2135 2136 if (fd < 0) { 2137 err = -errno; 2138 2139 pr_debug2_peo("\nsys_perf_event_open failed, error %d\n", 2140 err); 2141 goto try_fallback; 2142 } 2143 2144 bpf_counter__install_pe(evsel, idx, fd); 2145 2146 if (unlikely(test_attr__enabled)) { 2147 test_attr__open(&evsel->core.attr, pid, 2148 perf_cpu_map__cpu(cpus, idx), 2149 fd, group_fd, evsel->open_flags); 2150 } 2151 2152 /* Debug message used by test scripts */ 2153 pr_debug2_peo(" = %d\n", fd); 2154 2155 if (evsel->bpf_fd >= 0) { 2156 int evt_fd = fd; 2157 int bpf_fd = evsel->bpf_fd; 2158 2159 err = ioctl(evt_fd, 2160 PERF_EVENT_IOC_SET_BPF, 2161 bpf_fd); 2162 if (err && errno != EEXIST) { 2163 pr_err("failed to attach bpf fd %d: %s\n", 2164 bpf_fd, strerror(errno)); 2165 err = -EINVAL; 2166 goto out_close; 2167 } 2168 } 2169 2170 set_rlimit = NO_CHANGE; 2171 2172 /* 2173 * If we succeeded but had to kill clockid, fail and 2174 * have evsel__open_strerror() print us a nice error. 2175 */ 2176 if (perf_missing_features.clockid || 2177 perf_missing_features.clockid_wrong) { 2178 err = -EINVAL; 2179 goto out_close; 2180 } 2181 } 2182 } 2183 2184 return 0; 2185 2186 try_fallback: 2187 if (evsel__precise_ip_fallback(evsel)) 2188 goto retry_open; 2189 2190 if (evsel__ignore_missing_thread(evsel, perf_cpu_map__nr(cpus), 2191 idx, threads, thread, err)) { 2192 /* We just removed 1 thread, so lower the upper nthreads limit. */ 2193 nthreads--; 2194 2195 /* ... and pretend like nothing have happened. */ 2196 err = 0; 2197 goto retry_open; 2198 } 2199 /* 2200 * perf stat needs between 5 and 22 fds per CPU. When we run out 2201 * of them try to increase the limits. 2202 */ 2203 if (err == -EMFILE && evsel__increase_rlimit(&set_rlimit)) 2204 goto retry_open; 2205 2206 if (err != -EINVAL || idx > 0 || thread > 0) 2207 goto out_close; 2208 2209 if (evsel__detect_missing_features(evsel)) 2210 goto fallback_missing_features; 2211 out_close: 2212 if (err) 2213 threads->err_thread = thread; 2214 2215 old_errno = errno; 2216 do { 2217 while (--thread >= 0) { 2218 if (FD(evsel, idx, thread) >= 0) 2219 close(FD(evsel, idx, thread)); 2220 FD(evsel, idx, thread) = -1; 2221 } 2222 thread = nthreads; 2223 } while (--idx >= 0); 2224 errno = old_errno; 2225 return err; 2226 } 2227 2228 int evsel__open(struct evsel *evsel, struct perf_cpu_map *cpus, 2229 struct perf_thread_map *threads) 2230 { 2231 return evsel__open_cpu(evsel, cpus, threads, 0, perf_cpu_map__nr(cpus)); 2232 } 2233 2234 void evsel__close(struct evsel *evsel) 2235 { 2236 perf_evsel__close(&evsel->core); 2237 perf_evsel__free_id(&evsel->core); 2238 } 2239 2240 int evsel__open_per_cpu(struct evsel *evsel, struct perf_cpu_map *cpus, int cpu_map_idx) 2241 { 2242 if (cpu_map_idx == -1) 2243 return evsel__open_cpu(evsel, cpus, NULL, 0, perf_cpu_map__nr(cpus)); 2244 2245 return evsel__open_cpu(evsel, cpus, NULL, cpu_map_idx, cpu_map_idx + 1); 2246 } 2247 2248 int evsel__open_per_thread(struct evsel *evsel, struct perf_thread_map *threads) 2249 { 2250 return evsel__open(evsel, NULL, threads); 2251 } 2252 2253 static int perf_evsel__parse_id_sample(const struct evsel *evsel, 2254 const union perf_event *event, 2255 struct perf_sample *sample) 2256 { 2257 u64 type = evsel->core.attr.sample_type; 2258 const __u64 *array = event->sample.array; 2259 bool swapped = evsel->needs_swap; 2260 union u64_swap u; 2261 2262 array += ((event->header.size - 2263 sizeof(event->header)) / sizeof(u64)) - 1; 2264 2265 if (type & PERF_SAMPLE_IDENTIFIER) { 2266 sample->id = *array; 2267 array--; 2268 } 2269 2270 if (type & PERF_SAMPLE_CPU) { 2271 u.val64 = *array; 2272 if (swapped) { 2273 /* undo swap of u64, then swap on individual u32s */ 2274 u.val64 = bswap_64(u.val64); 2275 u.val32[0] = bswap_32(u.val32[0]); 2276 } 2277 2278 sample->cpu = u.val32[0]; 2279 array--; 2280 } 2281 2282 if (type & PERF_SAMPLE_STREAM_ID) { 2283 sample->stream_id = *array; 2284 array--; 2285 } 2286 2287 if (type & PERF_SAMPLE_ID) { 2288 sample->id = *array; 2289 array--; 2290 } 2291 2292 if (type & PERF_SAMPLE_TIME) { 2293 sample->time = *array; 2294 array--; 2295 } 2296 2297 if (type & PERF_SAMPLE_TID) { 2298 u.val64 = *array; 2299 if (swapped) { 2300 /* undo swap of u64, then swap on individual u32s */ 2301 u.val64 = bswap_64(u.val64); 2302 u.val32[0] = bswap_32(u.val32[0]); 2303 u.val32[1] = bswap_32(u.val32[1]); 2304 } 2305 2306 sample->pid = u.val32[0]; 2307 sample->tid = u.val32[1]; 2308 array--; 2309 } 2310 2311 return 0; 2312 } 2313 2314 static inline bool overflow(const void *endp, u16 max_size, const void *offset, 2315 u64 size) 2316 { 2317 return size > max_size || offset + size > endp; 2318 } 2319 2320 #define OVERFLOW_CHECK(offset, size, max_size) \ 2321 do { \ 2322 if (overflow(endp, (max_size), (offset), (size))) \ 2323 return -EFAULT; \ 2324 } while (0) 2325 2326 #define OVERFLOW_CHECK_u64(offset) \ 2327 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64)) 2328 2329 static int 2330 perf_event__check_size(union perf_event *event, unsigned int sample_size) 2331 { 2332 /* 2333 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes 2334 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to 2335 * check the format does not go past the end of the event. 2336 */ 2337 if (sample_size + sizeof(event->header) > event->header.size) 2338 return -EFAULT; 2339 2340 return 0; 2341 } 2342 2343 void __weak arch_perf_parse_sample_weight(struct perf_sample *data, 2344 const __u64 *array, 2345 u64 type __maybe_unused) 2346 { 2347 data->weight = *array; 2348 } 2349 2350 u64 evsel__bitfield_swap_branch_flags(u64 value) 2351 { 2352 u64 new_val = 0; 2353 2354 /* 2355 * branch_flags 2356 * union { 2357 * u64 values; 2358 * struct { 2359 * mispred:1 //target mispredicted 2360 * predicted:1 //target predicted 2361 * in_tx:1 //in transaction 2362 * abort:1 //transaction abort 2363 * cycles:16 //cycle count to last branch 2364 * type:4 //branch type 2365 * spec:2 //branch speculation info 2366 * new_type:4 //additional branch type 2367 * priv:3 //privilege level 2368 * reserved:31 2369 * } 2370 * } 2371 * 2372 * Avoid bswap64() the entire branch_flag.value, 2373 * as it has variable bit-field sizes. Instead the 2374 * macro takes the bit-field position/size, 2375 * swaps it based on the host endianness. 2376 */ 2377 if (host_is_bigendian()) { 2378 new_val = bitfield_swap(value, 0, 1); 2379 new_val |= bitfield_swap(value, 1, 1); 2380 new_val |= bitfield_swap(value, 2, 1); 2381 new_val |= bitfield_swap(value, 3, 1); 2382 new_val |= bitfield_swap(value, 4, 16); 2383 new_val |= bitfield_swap(value, 20, 4); 2384 new_val |= bitfield_swap(value, 24, 2); 2385 new_val |= bitfield_swap(value, 26, 4); 2386 new_val |= bitfield_swap(value, 30, 3); 2387 new_val |= bitfield_swap(value, 33, 31); 2388 } else { 2389 new_val = bitfield_swap(value, 63, 1); 2390 new_val |= bitfield_swap(value, 62, 1); 2391 new_val |= bitfield_swap(value, 61, 1); 2392 new_val |= bitfield_swap(value, 60, 1); 2393 new_val |= bitfield_swap(value, 44, 16); 2394 new_val |= bitfield_swap(value, 40, 4); 2395 new_val |= bitfield_swap(value, 38, 2); 2396 new_val |= bitfield_swap(value, 34, 4); 2397 new_val |= bitfield_swap(value, 31, 3); 2398 new_val |= bitfield_swap(value, 0, 31); 2399 } 2400 2401 return new_val; 2402 } 2403 2404 int evsel__parse_sample(struct evsel *evsel, union perf_event *event, 2405 struct perf_sample *data) 2406 { 2407 u64 type = evsel->core.attr.sample_type; 2408 bool swapped = evsel->needs_swap; 2409 const __u64 *array; 2410 u16 max_size = event->header.size; 2411 const void *endp = (void *)event + max_size; 2412 u64 sz; 2413 2414 /* 2415 * used for cross-endian analysis. See git commit 65014ab3 2416 * for why this goofiness is needed. 2417 */ 2418 union u64_swap u; 2419 2420 memset(data, 0, sizeof(*data)); 2421 data->cpu = data->pid = data->tid = -1; 2422 data->stream_id = data->id = data->time = -1ULL; 2423 data->period = evsel->core.attr.sample_period; 2424 data->cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; 2425 data->misc = event->header.misc; 2426 data->id = -1ULL; 2427 data->data_src = PERF_MEM_DATA_SRC_NONE; 2428 data->vcpu = -1; 2429 2430 if (event->header.type != PERF_RECORD_SAMPLE) { 2431 if (!evsel->core.attr.sample_id_all) 2432 return 0; 2433 return perf_evsel__parse_id_sample(evsel, event, data); 2434 } 2435 2436 array = event->sample.array; 2437 2438 if (perf_event__check_size(event, evsel->sample_size)) 2439 return -EFAULT; 2440 2441 if (type & PERF_SAMPLE_IDENTIFIER) { 2442 data->id = *array; 2443 array++; 2444 } 2445 2446 if (type & PERF_SAMPLE_IP) { 2447 data->ip = *array; 2448 array++; 2449 } 2450 2451 if (type & PERF_SAMPLE_TID) { 2452 u.val64 = *array; 2453 if (swapped) { 2454 /* undo swap of u64, then swap on individual u32s */ 2455 u.val64 = bswap_64(u.val64); 2456 u.val32[0] = bswap_32(u.val32[0]); 2457 u.val32[1] = bswap_32(u.val32[1]); 2458 } 2459 2460 data->pid = u.val32[0]; 2461 data->tid = u.val32[1]; 2462 array++; 2463 } 2464 2465 if (type & PERF_SAMPLE_TIME) { 2466 data->time = *array; 2467 array++; 2468 } 2469 2470 if (type & PERF_SAMPLE_ADDR) { 2471 data->addr = *array; 2472 array++; 2473 } 2474 2475 if (type & PERF_SAMPLE_ID) { 2476 data->id = *array; 2477 array++; 2478 } 2479 2480 if (type & PERF_SAMPLE_STREAM_ID) { 2481 data->stream_id = *array; 2482 array++; 2483 } 2484 2485 if (type & PERF_SAMPLE_CPU) { 2486 2487 u.val64 = *array; 2488 if (swapped) { 2489 /* undo swap of u64, then swap on individual u32s */ 2490 u.val64 = bswap_64(u.val64); 2491 u.val32[0] = bswap_32(u.val32[0]); 2492 } 2493 2494 data->cpu = u.val32[0]; 2495 array++; 2496 } 2497 2498 if (type & PERF_SAMPLE_PERIOD) { 2499 data->period = *array; 2500 array++; 2501 } 2502 2503 if (type & PERF_SAMPLE_READ) { 2504 u64 read_format = evsel->core.attr.read_format; 2505 2506 OVERFLOW_CHECK_u64(array); 2507 if (read_format & PERF_FORMAT_GROUP) 2508 data->read.group.nr = *array; 2509 else 2510 data->read.one.value = *array; 2511 2512 array++; 2513 2514 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { 2515 OVERFLOW_CHECK_u64(array); 2516 data->read.time_enabled = *array; 2517 array++; 2518 } 2519 2520 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { 2521 OVERFLOW_CHECK_u64(array); 2522 data->read.time_running = *array; 2523 array++; 2524 } 2525 2526 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */ 2527 if (read_format & PERF_FORMAT_GROUP) { 2528 const u64 max_group_nr = UINT64_MAX / 2529 sizeof(struct sample_read_value); 2530 2531 if (data->read.group.nr > max_group_nr) 2532 return -EFAULT; 2533 2534 sz = data->read.group.nr * sample_read_value_size(read_format); 2535 OVERFLOW_CHECK(array, sz, max_size); 2536 data->read.group.values = 2537 (struct sample_read_value *)array; 2538 array = (void *)array + sz; 2539 } else { 2540 OVERFLOW_CHECK_u64(array); 2541 data->read.one.id = *array; 2542 array++; 2543 2544 if (read_format & PERF_FORMAT_LOST) { 2545 OVERFLOW_CHECK_u64(array); 2546 data->read.one.lost = *array; 2547 array++; 2548 } 2549 } 2550 } 2551 2552 if (type & PERF_SAMPLE_CALLCHAIN) { 2553 const u64 max_callchain_nr = UINT64_MAX / sizeof(u64); 2554 2555 OVERFLOW_CHECK_u64(array); 2556 data->callchain = (struct ip_callchain *)array++; 2557 if (data->callchain->nr > max_callchain_nr) 2558 return -EFAULT; 2559 sz = data->callchain->nr * sizeof(u64); 2560 OVERFLOW_CHECK(array, sz, max_size); 2561 array = (void *)array + sz; 2562 } 2563 2564 if (type & PERF_SAMPLE_RAW) { 2565 OVERFLOW_CHECK_u64(array); 2566 u.val64 = *array; 2567 2568 /* 2569 * Undo swap of u64, then swap on individual u32s, 2570 * get the size of the raw area and undo all of the 2571 * swap. The pevent interface handles endianness by 2572 * itself. 2573 */ 2574 if (swapped) { 2575 u.val64 = bswap_64(u.val64); 2576 u.val32[0] = bswap_32(u.val32[0]); 2577 u.val32[1] = bswap_32(u.val32[1]); 2578 } 2579 data->raw_size = u.val32[0]; 2580 2581 /* 2582 * The raw data is aligned on 64bits including the 2583 * u32 size, so it's safe to use mem_bswap_64. 2584 */ 2585 if (swapped) 2586 mem_bswap_64((void *) array, data->raw_size); 2587 2588 array = (void *)array + sizeof(u32); 2589 2590 OVERFLOW_CHECK(array, data->raw_size, max_size); 2591 data->raw_data = (void *)array; 2592 array = (void *)array + data->raw_size; 2593 } 2594 2595 if (type & PERF_SAMPLE_BRANCH_STACK) { 2596 const u64 max_branch_nr = UINT64_MAX / 2597 sizeof(struct branch_entry); 2598 struct branch_entry *e; 2599 unsigned int i; 2600 2601 OVERFLOW_CHECK_u64(array); 2602 data->branch_stack = (struct branch_stack *)array++; 2603 2604 if (data->branch_stack->nr > max_branch_nr) 2605 return -EFAULT; 2606 2607 sz = data->branch_stack->nr * sizeof(struct branch_entry); 2608 if (evsel__has_branch_hw_idx(evsel)) { 2609 sz += sizeof(u64); 2610 e = &data->branch_stack->entries[0]; 2611 } else { 2612 data->no_hw_idx = true; 2613 /* 2614 * if the PERF_SAMPLE_BRANCH_HW_INDEX is not applied, 2615 * only nr and entries[] will be output by kernel. 2616 */ 2617 e = (struct branch_entry *)&data->branch_stack->hw_idx; 2618 } 2619 2620 if (swapped) { 2621 /* 2622 * struct branch_flag does not have endian 2623 * specific bit field definition. And bswap 2624 * will not resolve the issue, since these 2625 * are bit fields. 2626 * 2627 * evsel__bitfield_swap_branch_flags() uses a 2628 * bitfield_swap macro to swap the bit position 2629 * based on the host endians. 2630 */ 2631 for (i = 0; i < data->branch_stack->nr; i++, e++) 2632 e->flags.value = evsel__bitfield_swap_branch_flags(e->flags.value); 2633 } 2634 2635 OVERFLOW_CHECK(array, sz, max_size); 2636 array = (void *)array + sz; 2637 } 2638 2639 if (type & PERF_SAMPLE_REGS_USER) { 2640 OVERFLOW_CHECK_u64(array); 2641 data->user_regs.abi = *array; 2642 array++; 2643 2644 if (data->user_regs.abi) { 2645 u64 mask = evsel->core.attr.sample_regs_user; 2646 2647 sz = hweight64(mask) * sizeof(u64); 2648 OVERFLOW_CHECK(array, sz, max_size); 2649 data->user_regs.mask = mask; 2650 data->user_regs.regs = (u64 *)array; 2651 array = (void *)array + sz; 2652 } 2653 } 2654 2655 if (type & PERF_SAMPLE_STACK_USER) { 2656 OVERFLOW_CHECK_u64(array); 2657 sz = *array++; 2658 2659 data->user_stack.offset = ((char *)(array - 1) 2660 - (char *) event); 2661 2662 if (!sz) { 2663 data->user_stack.size = 0; 2664 } else { 2665 OVERFLOW_CHECK(array, sz, max_size); 2666 data->user_stack.data = (char *)array; 2667 array = (void *)array + sz; 2668 OVERFLOW_CHECK_u64(array); 2669 data->user_stack.size = *array++; 2670 if (WARN_ONCE(data->user_stack.size > sz, 2671 "user stack dump failure\n")) 2672 return -EFAULT; 2673 } 2674 } 2675 2676 if (type & PERF_SAMPLE_WEIGHT_TYPE) { 2677 OVERFLOW_CHECK_u64(array); 2678 arch_perf_parse_sample_weight(data, array, type); 2679 array++; 2680 } 2681 2682 if (type & PERF_SAMPLE_DATA_SRC) { 2683 OVERFLOW_CHECK_u64(array); 2684 data->data_src = *array; 2685 array++; 2686 } 2687 2688 if (type & PERF_SAMPLE_TRANSACTION) { 2689 OVERFLOW_CHECK_u64(array); 2690 data->transaction = *array; 2691 array++; 2692 } 2693 2694 data->intr_regs.abi = PERF_SAMPLE_REGS_ABI_NONE; 2695 if (type & PERF_SAMPLE_REGS_INTR) { 2696 OVERFLOW_CHECK_u64(array); 2697 data->intr_regs.abi = *array; 2698 array++; 2699 2700 if (data->intr_regs.abi != PERF_SAMPLE_REGS_ABI_NONE) { 2701 u64 mask = evsel->core.attr.sample_regs_intr; 2702 2703 sz = hweight64(mask) * sizeof(u64); 2704 OVERFLOW_CHECK(array, sz, max_size); 2705 data->intr_regs.mask = mask; 2706 data->intr_regs.regs = (u64 *)array; 2707 array = (void *)array + sz; 2708 } 2709 } 2710 2711 data->phys_addr = 0; 2712 if (type & PERF_SAMPLE_PHYS_ADDR) { 2713 data->phys_addr = *array; 2714 array++; 2715 } 2716 2717 data->cgroup = 0; 2718 if (type & PERF_SAMPLE_CGROUP) { 2719 data->cgroup = *array; 2720 array++; 2721 } 2722 2723 data->data_page_size = 0; 2724 if (type & PERF_SAMPLE_DATA_PAGE_SIZE) { 2725 data->data_page_size = *array; 2726 array++; 2727 } 2728 2729 data->code_page_size = 0; 2730 if (type & PERF_SAMPLE_CODE_PAGE_SIZE) { 2731 data->code_page_size = *array; 2732 array++; 2733 } 2734 2735 if (type & PERF_SAMPLE_AUX) { 2736 OVERFLOW_CHECK_u64(array); 2737 sz = *array++; 2738 2739 OVERFLOW_CHECK(array, sz, max_size); 2740 /* Undo swap of data */ 2741 if (swapped) 2742 mem_bswap_64((char *)array, sz); 2743 data->aux_sample.size = sz; 2744 data->aux_sample.data = (char *)array; 2745 array = (void *)array + sz; 2746 } 2747 2748 return 0; 2749 } 2750 2751 int evsel__parse_sample_timestamp(struct evsel *evsel, union perf_event *event, 2752 u64 *timestamp) 2753 { 2754 u64 type = evsel->core.attr.sample_type; 2755 const __u64 *array; 2756 2757 if (!(type & PERF_SAMPLE_TIME)) 2758 return -1; 2759 2760 if (event->header.type != PERF_RECORD_SAMPLE) { 2761 struct perf_sample data = { 2762 .time = -1ULL, 2763 }; 2764 2765 if (!evsel->core.attr.sample_id_all) 2766 return -1; 2767 if (perf_evsel__parse_id_sample(evsel, event, &data)) 2768 return -1; 2769 2770 *timestamp = data.time; 2771 return 0; 2772 } 2773 2774 array = event->sample.array; 2775 2776 if (perf_event__check_size(event, evsel->sample_size)) 2777 return -EFAULT; 2778 2779 if (type & PERF_SAMPLE_IDENTIFIER) 2780 array++; 2781 2782 if (type & PERF_SAMPLE_IP) 2783 array++; 2784 2785 if (type & PERF_SAMPLE_TID) 2786 array++; 2787 2788 if (type & PERF_SAMPLE_TIME) 2789 *timestamp = *array; 2790 2791 return 0; 2792 } 2793 2794 u16 evsel__id_hdr_size(struct evsel *evsel) 2795 { 2796 u64 sample_type = evsel->core.attr.sample_type; 2797 u16 size = 0; 2798 2799 if (sample_type & PERF_SAMPLE_TID) 2800 size += sizeof(u64); 2801 2802 if (sample_type & PERF_SAMPLE_TIME) 2803 size += sizeof(u64); 2804 2805 if (sample_type & PERF_SAMPLE_ID) 2806 size += sizeof(u64); 2807 2808 if (sample_type & PERF_SAMPLE_STREAM_ID) 2809 size += sizeof(u64); 2810 2811 if (sample_type & PERF_SAMPLE_CPU) 2812 size += sizeof(u64); 2813 2814 if (sample_type & PERF_SAMPLE_IDENTIFIER) 2815 size += sizeof(u64); 2816 2817 return size; 2818 } 2819 2820 #ifdef HAVE_LIBTRACEEVENT 2821 struct tep_format_field *evsel__field(struct evsel *evsel, const char *name) 2822 { 2823 return tep_find_field(evsel->tp_format, name); 2824 } 2825 2826 void *evsel__rawptr(struct evsel *evsel, struct perf_sample *sample, const char *name) 2827 { 2828 struct tep_format_field *field = evsel__field(evsel, name); 2829 int offset; 2830 2831 if (!field) 2832 return NULL; 2833 2834 offset = field->offset; 2835 2836 if (field->flags & TEP_FIELD_IS_DYNAMIC) { 2837 offset = *(int *)(sample->raw_data + field->offset); 2838 offset &= 0xffff; 2839 if (tep_field_is_relative(field->flags)) 2840 offset += field->offset + field->size; 2841 } 2842 2843 return sample->raw_data + offset; 2844 } 2845 2846 u64 format_field__intval(struct tep_format_field *field, struct perf_sample *sample, 2847 bool needs_swap) 2848 { 2849 u64 value; 2850 void *ptr = sample->raw_data + field->offset; 2851 2852 switch (field->size) { 2853 case 1: 2854 return *(u8 *)ptr; 2855 case 2: 2856 value = *(u16 *)ptr; 2857 break; 2858 case 4: 2859 value = *(u32 *)ptr; 2860 break; 2861 case 8: 2862 memcpy(&value, ptr, sizeof(u64)); 2863 break; 2864 default: 2865 return 0; 2866 } 2867 2868 if (!needs_swap) 2869 return value; 2870 2871 switch (field->size) { 2872 case 2: 2873 return bswap_16(value); 2874 case 4: 2875 return bswap_32(value); 2876 case 8: 2877 return bswap_64(value); 2878 default: 2879 return 0; 2880 } 2881 2882 return 0; 2883 } 2884 2885 u64 evsel__intval(struct evsel *evsel, struct perf_sample *sample, const char *name) 2886 { 2887 struct tep_format_field *field = evsel__field(evsel, name); 2888 2889 if (!field) 2890 return 0; 2891 2892 return field ? format_field__intval(field, sample, evsel->needs_swap) : 0; 2893 } 2894 #endif 2895 2896 bool evsel__fallback(struct evsel *evsel, int err, char *msg, size_t msgsize) 2897 { 2898 int paranoid; 2899 2900 if ((err == ENOENT || err == ENXIO || err == ENODEV) && 2901 evsel->core.attr.type == PERF_TYPE_HARDWARE && 2902 evsel->core.attr.config == PERF_COUNT_HW_CPU_CYCLES) { 2903 /* 2904 * If it's cycles then fall back to hrtimer based 2905 * cpu-clock-tick sw counter, which is always available even if 2906 * no PMU support. 2907 * 2908 * PPC returns ENXIO until 2.6.37 (behavior changed with commit 2909 * b0a873e). 2910 */ 2911 scnprintf(msg, msgsize, "%s", 2912 "The cycles event is not supported, trying to fall back to cpu-clock-ticks"); 2913 2914 evsel->core.attr.type = PERF_TYPE_SOFTWARE; 2915 evsel->core.attr.config = PERF_COUNT_SW_CPU_CLOCK; 2916 2917 zfree(&evsel->name); 2918 return true; 2919 } else if (err == EACCES && !evsel->core.attr.exclude_kernel && 2920 (paranoid = perf_event_paranoid()) > 1) { 2921 const char *name = evsel__name(evsel); 2922 char *new_name; 2923 const char *sep = ":"; 2924 2925 /* If event has exclude user then don't exclude kernel. */ 2926 if (evsel->core.attr.exclude_user) 2927 return false; 2928 2929 /* Is there already the separator in the name. */ 2930 if (strchr(name, '/') || 2931 (strchr(name, ':') && !evsel->is_libpfm_event)) 2932 sep = ""; 2933 2934 if (asprintf(&new_name, "%s%su", name, sep) < 0) 2935 return false; 2936 2937 free(evsel->name); 2938 evsel->name = new_name; 2939 scnprintf(msg, msgsize, "kernel.perf_event_paranoid=%d, trying " 2940 "to fall back to excluding kernel and hypervisor " 2941 " samples", paranoid); 2942 evsel->core.attr.exclude_kernel = 1; 2943 evsel->core.attr.exclude_hv = 1; 2944 2945 return true; 2946 } 2947 2948 return false; 2949 } 2950 2951 static bool find_process(const char *name) 2952 { 2953 size_t len = strlen(name); 2954 DIR *dir; 2955 struct dirent *d; 2956 int ret = -1; 2957 2958 dir = opendir(procfs__mountpoint()); 2959 if (!dir) 2960 return false; 2961 2962 /* Walk through the directory. */ 2963 while (ret && (d = readdir(dir)) != NULL) { 2964 char path[PATH_MAX]; 2965 char *data; 2966 size_t size; 2967 2968 if ((d->d_type != DT_DIR) || 2969 !strcmp(".", d->d_name) || 2970 !strcmp("..", d->d_name)) 2971 continue; 2972 2973 scnprintf(path, sizeof(path), "%s/%s/comm", 2974 procfs__mountpoint(), d->d_name); 2975 2976 if (filename__read_str(path, &data, &size)) 2977 continue; 2978 2979 ret = strncmp(name, data, len); 2980 free(data); 2981 } 2982 2983 closedir(dir); 2984 return ret ? false : true; 2985 } 2986 2987 static bool is_amd(const char *arch, const char *cpuid) 2988 { 2989 return arch && !strcmp("x86", arch) && cpuid && strstarts(cpuid, "AuthenticAMD"); 2990 } 2991 2992 static bool is_amd_ibs(struct evsel *evsel) 2993 { 2994 return evsel->core.attr.precise_ip 2995 || (evsel->pmu_name && !strncmp(evsel->pmu_name, "ibs", 3)); 2996 } 2997 2998 int evsel__open_strerror(struct evsel *evsel, struct target *target, 2999 int err, char *msg, size_t size) 3000 { 3001 struct perf_env *env = evsel__env(evsel); 3002 const char *arch = perf_env__arch(env); 3003 const char *cpuid = perf_env__cpuid(env); 3004 char sbuf[STRERR_BUFSIZE]; 3005 int printed = 0, enforced = 0; 3006 3007 switch (err) { 3008 case EPERM: 3009 case EACCES: 3010 printed += scnprintf(msg + printed, size - printed, 3011 "Access to performance monitoring and observability operations is limited.\n"); 3012 3013 if (!sysfs__read_int("fs/selinux/enforce", &enforced)) { 3014 if (enforced) { 3015 printed += scnprintf(msg + printed, size - printed, 3016 "Enforced MAC policy settings (SELinux) can limit access to performance\n" 3017 "monitoring and observability operations. Inspect system audit records for\n" 3018 "more perf_event access control information and adjusting the policy.\n"); 3019 } 3020 } 3021 3022 if (err == EPERM) 3023 printed += scnprintf(msg, size, 3024 "No permission to enable %s event.\n\n", evsel__name(evsel)); 3025 3026 return scnprintf(msg + printed, size - printed, 3027 "Consider adjusting /proc/sys/kernel/perf_event_paranoid setting to open\n" 3028 "access to performance monitoring and observability operations for processes\n" 3029 "without CAP_PERFMON, CAP_SYS_PTRACE or CAP_SYS_ADMIN Linux capability.\n" 3030 "More information can be found at 'Perf events and tool security' document:\n" 3031 "https://www.kernel.org/doc/html/latest/admin-guide/perf-security.html\n" 3032 "perf_event_paranoid setting is %d:\n" 3033 " -1: Allow use of (almost) all events by all users\n" 3034 " Ignore mlock limit after perf_event_mlock_kb without CAP_IPC_LOCK\n" 3035 ">= 0: Disallow raw and ftrace function tracepoint access\n" 3036 ">= 1: Disallow CPU event access\n" 3037 ">= 2: Disallow kernel profiling\n" 3038 "To make the adjusted perf_event_paranoid setting permanent preserve it\n" 3039 "in /etc/sysctl.conf (e.g. kernel.perf_event_paranoid = <setting>)", 3040 perf_event_paranoid()); 3041 case ENOENT: 3042 return scnprintf(msg, size, "The %s event is not supported.", evsel__name(evsel)); 3043 case EMFILE: 3044 return scnprintf(msg, size, "%s", 3045 "Too many events are opened.\n" 3046 "Probably the maximum number of open file descriptors has been reached.\n" 3047 "Hint: Try again after reducing the number of events.\n" 3048 "Hint: Try increasing the limit with 'ulimit -n <limit>'"); 3049 case ENOMEM: 3050 if (evsel__has_callchain(evsel) && 3051 access("/proc/sys/kernel/perf_event_max_stack", F_OK) == 0) 3052 return scnprintf(msg, size, 3053 "Not enough memory to setup event with callchain.\n" 3054 "Hint: Try tweaking /proc/sys/kernel/perf_event_max_stack\n" 3055 "Hint: Current value: %d", sysctl__max_stack()); 3056 break; 3057 case ENODEV: 3058 if (target->cpu_list) 3059 return scnprintf(msg, size, "%s", 3060 "No such device - did you specify an out-of-range profile CPU?"); 3061 break; 3062 case EOPNOTSUPP: 3063 if (evsel->core.attr.sample_type & PERF_SAMPLE_BRANCH_STACK) 3064 return scnprintf(msg, size, 3065 "%s: PMU Hardware or event type doesn't support branch stack sampling.", 3066 evsel__name(evsel)); 3067 if (evsel->core.attr.aux_output) 3068 return scnprintf(msg, size, 3069 "%s: PMU Hardware doesn't support 'aux_output' feature", 3070 evsel__name(evsel)); 3071 if (evsel->core.attr.sample_period != 0) 3072 return scnprintf(msg, size, 3073 "%s: PMU Hardware doesn't support sampling/overflow-interrupts. Try 'perf stat'", 3074 evsel__name(evsel)); 3075 if (evsel->core.attr.precise_ip) 3076 return scnprintf(msg, size, "%s", 3077 "\'precise\' request may not be supported. Try removing 'p' modifier."); 3078 #if defined(__i386__) || defined(__x86_64__) 3079 if (evsel->core.attr.type == PERF_TYPE_HARDWARE) 3080 return scnprintf(msg, size, "%s", 3081 "No hardware sampling interrupt available.\n"); 3082 #endif 3083 break; 3084 case EBUSY: 3085 if (find_process("oprofiled")) 3086 return scnprintf(msg, size, 3087 "The PMU counters are busy/taken by another profiler.\n" 3088 "We found oprofile daemon running, please stop it and try again."); 3089 break; 3090 case EINVAL: 3091 if (evsel->core.attr.sample_type & PERF_SAMPLE_CODE_PAGE_SIZE && perf_missing_features.code_page_size) 3092 return scnprintf(msg, size, "Asking for the code page size isn't supported by this kernel."); 3093 if (evsel->core.attr.sample_type & PERF_SAMPLE_DATA_PAGE_SIZE && perf_missing_features.data_page_size) 3094 return scnprintf(msg, size, "Asking for the data page size isn't supported by this kernel."); 3095 if (evsel->core.attr.write_backward && perf_missing_features.write_backward) 3096 return scnprintf(msg, size, "Reading from overwrite event is not supported by this kernel."); 3097 if (perf_missing_features.clockid) 3098 return scnprintf(msg, size, "clockid feature not supported."); 3099 if (perf_missing_features.clockid_wrong) 3100 return scnprintf(msg, size, "wrong clockid (%d).", clockid); 3101 if (perf_missing_features.aux_output) 3102 return scnprintf(msg, size, "The 'aux_output' feature is not supported, update the kernel."); 3103 if (!target__has_cpu(target)) 3104 return scnprintf(msg, size, 3105 "Invalid event (%s) in per-thread mode, enable system wide with '-a'.", 3106 evsel__name(evsel)); 3107 if (is_amd(arch, cpuid)) { 3108 if (is_amd_ibs(evsel)) { 3109 if (evsel->core.attr.exclude_kernel) 3110 return scnprintf(msg, size, 3111 "AMD IBS can't exclude kernel events. Try running at a higher privilege level."); 3112 if (!evsel->core.system_wide) 3113 return scnprintf(msg, size, 3114 "AMD IBS may only be available in system-wide/per-cpu mode. Try using -a, or -C and workload affinity"); 3115 } 3116 } 3117 3118 break; 3119 case ENODATA: 3120 return scnprintf(msg, size, "Cannot collect data source with the load latency event alone. " 3121 "Please add an auxiliary event in front of the load latency event."); 3122 default: 3123 break; 3124 } 3125 3126 return scnprintf(msg, size, 3127 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n" 3128 "/bin/dmesg | grep -i perf may provide additional information.\n", 3129 err, str_error_r(err, sbuf, sizeof(sbuf)), evsel__name(evsel)); 3130 } 3131 3132 struct perf_env *evsel__env(struct evsel *evsel) 3133 { 3134 if (evsel && evsel->evlist && evsel->evlist->env) 3135 return evsel->evlist->env; 3136 return &perf_env; 3137 } 3138 3139 static int store_evsel_ids(struct evsel *evsel, struct evlist *evlist) 3140 { 3141 int cpu_map_idx, thread; 3142 3143 for (cpu_map_idx = 0; cpu_map_idx < xyarray__max_x(evsel->core.fd); cpu_map_idx++) { 3144 for (thread = 0; thread < xyarray__max_y(evsel->core.fd); 3145 thread++) { 3146 int fd = FD(evsel, cpu_map_idx, thread); 3147 3148 if (perf_evlist__id_add_fd(&evlist->core, &evsel->core, 3149 cpu_map_idx, thread, fd) < 0) 3150 return -1; 3151 } 3152 } 3153 3154 return 0; 3155 } 3156 3157 int evsel__store_ids(struct evsel *evsel, struct evlist *evlist) 3158 { 3159 struct perf_cpu_map *cpus = evsel->core.cpus; 3160 struct perf_thread_map *threads = evsel->core.threads; 3161 3162 if (perf_evsel__alloc_id(&evsel->core, perf_cpu_map__nr(cpus), threads->nr)) 3163 return -ENOMEM; 3164 3165 return store_evsel_ids(evsel, evlist); 3166 } 3167 3168 void evsel__zero_per_pkg(struct evsel *evsel) 3169 { 3170 struct hashmap_entry *cur; 3171 size_t bkt; 3172 3173 if (evsel->per_pkg_mask) { 3174 hashmap__for_each_entry(evsel->per_pkg_mask, cur, bkt) 3175 zfree(&cur->pkey); 3176 3177 hashmap__clear(evsel->per_pkg_mask); 3178 } 3179 } 3180 3181 bool evsel__is_hybrid(const struct evsel *evsel) 3182 { 3183 return evsel->pmu_name && perf_pmu__is_hybrid(evsel->pmu_name); 3184 } 3185 3186 struct evsel *evsel__leader(const struct evsel *evsel) 3187 { 3188 return container_of(evsel->core.leader, struct evsel, core); 3189 } 3190 3191 bool evsel__has_leader(struct evsel *evsel, struct evsel *leader) 3192 { 3193 return evsel->core.leader == &leader->core; 3194 } 3195 3196 bool evsel__is_leader(struct evsel *evsel) 3197 { 3198 return evsel__has_leader(evsel, evsel); 3199 } 3200 3201 void evsel__set_leader(struct evsel *evsel, struct evsel *leader) 3202 { 3203 evsel->core.leader = &leader->core; 3204 } 3205 3206 int evsel__source_count(const struct evsel *evsel) 3207 { 3208 struct evsel *pos; 3209 int count = 0; 3210 3211 evlist__for_each_entry(evsel->evlist, pos) { 3212 if (pos->metric_leader == evsel) 3213 count++; 3214 } 3215 return count; 3216 } 3217 3218 bool __weak arch_evsel__must_be_in_group(const struct evsel *evsel __maybe_unused) 3219 { 3220 return false; 3221 } 3222 3223 /* 3224 * Remove an event from a given group (leader). 3225 * Some events, e.g., perf metrics Topdown events, 3226 * must always be grouped. Ignore the events. 3227 */ 3228 void evsel__remove_from_group(struct evsel *evsel, struct evsel *leader) 3229 { 3230 if (!arch_evsel__must_be_in_group(evsel) && evsel != leader) { 3231 evsel__set_leader(evsel, evsel); 3232 evsel->core.nr_members = 0; 3233 leader->core.nr_members--; 3234 } 3235 } 3236