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