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