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 * Powerpc needs __SANE_USERSPACE_TYPES__ before <linux/types.h> to select 10 * 'int-ll64.h' and avoid compile warnings when printing __u64 with %llu. 11 */ 12 #define __SANE_USERSPACE_TYPES__ 13 14 #include <byteswap.h> 15 #include <errno.h> 16 #include <inttypes.h> 17 #include <linux/bitops.h> 18 #include <api/fs/fs.h> 19 #include <api/fs/tracing_path.h> 20 #include <linux/hw_breakpoint.h> 21 #include <linux/perf_event.h> 22 #include <linux/compiler.h> 23 #include <linux/err.h> 24 #include <linux/zalloc.h> 25 #include <sys/ioctl.h> 26 #include <sys/resource.h> 27 #include <sys/syscall.h> 28 #include <sys/types.h> 29 #include <dirent.h> 30 #include <stdlib.h> 31 #include <perf/evsel.h> 32 #include "asm/bug.h" 33 #include "bpf_counter.h" 34 #include "callchain.h" 35 #include "cgroup.h" 36 #include "counts.h" 37 #include "event.h" 38 #include "evsel.h" 39 #include "time-utils.h" 40 #include "util/env.h" 41 #include "util/evsel_config.h" 42 #include "util/evsel_fprintf.h" 43 #include "evlist.h" 44 #include <perf/cpumap.h> 45 #include "thread_map.h" 46 #include "target.h" 47 #include "perf_regs.h" 48 #include "record.h" 49 #include "debug.h" 50 #include "trace-event.h" 51 #include "session.h" 52 #include "stat.h" 53 #include "string2.h" 54 #include "memswap.h" 55 #include "util.h" 56 #include "util/hashmap.h" 57 #include "off_cpu.h" 58 #include "pmu.h" 59 #include "pmus.h" 60 #include "drm_pmu.h" 61 #include "hwmon_pmu.h" 62 #include "tool_pmu.h" 63 #include "tp_pmu.h" 64 #include "rlimit.h" 65 #include "../perf-sys.h" 66 #include "util/parse-branch-options.h" 67 #include "util/bpf-filter.h" 68 #include "util/hist.h" 69 #include <internal/xyarray.h> 70 #include <internal/lib.h> 71 #include <internal/threadmap.h> 72 #include "util/intel-tpebs.h" 73 74 #include <linux/ctype.h> 75 76 #ifdef HAVE_LIBTRACEEVENT 77 #include <event-parse.h> 78 #endif 79 80 struct perf_missing_features perf_missing_features; 81 82 static clockid_t clockid; 83 84 static int evsel__no_extra_init(struct evsel *evsel __maybe_unused) 85 { 86 return 0; 87 } 88 89 static bool test_attr__enabled(void) 90 { 91 static bool test_attr__enabled; 92 static bool test_attr__enabled_tested; 93 94 if (!test_attr__enabled_tested) { 95 char *dir = getenv("PERF_TEST_ATTR"); 96 97 test_attr__enabled = (dir != NULL); 98 test_attr__enabled_tested = true; 99 } 100 return test_attr__enabled; 101 } 102 103 #define __WRITE_ASS(str, fmt, data) \ 104 do { \ 105 if (fprintf(file, #str "=%"fmt "\n", data) < 0) { \ 106 perror("test attr - failed to write event file"); \ 107 fclose(file); \ 108 return -1; \ 109 } \ 110 } while (0) 111 112 #define WRITE_ASS(field, fmt) __WRITE_ASS(field, fmt, attr->field) 113 114 static int store_event(struct perf_event_attr *attr, pid_t pid, struct perf_cpu cpu, 115 int fd, int group_fd, unsigned long flags) 116 { 117 FILE *file; 118 char path[PATH_MAX]; 119 char *dir = getenv("PERF_TEST_ATTR"); 120 121 snprintf(path, PATH_MAX, "%s/event-%d-%llu-%d", dir, 122 attr->type, attr->config, fd); 123 124 file = fopen(path, "w+"); 125 if (!file) { 126 perror("test attr - failed to open event file"); 127 return -1; 128 } 129 130 if (fprintf(file, "[event-%d-%llu-%d]\n", 131 attr->type, attr->config, fd) < 0) { 132 perror("test attr - failed to write event file"); 133 fclose(file); 134 return -1; 135 } 136 137 /* syscall arguments */ 138 __WRITE_ASS(fd, "d", fd); 139 __WRITE_ASS(group_fd, "d", group_fd); 140 __WRITE_ASS(cpu, "d", cpu.cpu); 141 __WRITE_ASS(pid, "d", pid); 142 __WRITE_ASS(flags, "lu", flags); 143 144 /* struct perf_event_attr */ 145 WRITE_ASS(type, PRIu32); 146 WRITE_ASS(size, PRIu32); 147 WRITE_ASS(config, "llu"); 148 WRITE_ASS(sample_period, "llu"); 149 WRITE_ASS(sample_type, "llu"); 150 WRITE_ASS(read_format, "llu"); 151 WRITE_ASS(disabled, "d"); 152 WRITE_ASS(inherit, "d"); 153 WRITE_ASS(pinned, "d"); 154 WRITE_ASS(exclusive, "d"); 155 WRITE_ASS(exclude_user, "d"); 156 WRITE_ASS(exclude_kernel, "d"); 157 WRITE_ASS(exclude_hv, "d"); 158 WRITE_ASS(exclude_idle, "d"); 159 WRITE_ASS(mmap, "d"); 160 WRITE_ASS(comm, "d"); 161 WRITE_ASS(freq, "d"); 162 WRITE_ASS(inherit_stat, "d"); 163 WRITE_ASS(enable_on_exec, "d"); 164 WRITE_ASS(task, "d"); 165 WRITE_ASS(watermark, "d"); 166 WRITE_ASS(precise_ip, "d"); 167 WRITE_ASS(mmap_data, "d"); 168 WRITE_ASS(sample_id_all, "d"); 169 WRITE_ASS(exclude_host, "d"); 170 WRITE_ASS(exclude_guest, "d"); 171 WRITE_ASS(exclude_callchain_kernel, "d"); 172 WRITE_ASS(exclude_callchain_user, "d"); 173 WRITE_ASS(mmap2, "d"); 174 WRITE_ASS(comm_exec, "d"); 175 WRITE_ASS(context_switch, "d"); 176 WRITE_ASS(write_backward, "d"); 177 WRITE_ASS(namespaces, "d"); 178 WRITE_ASS(use_clockid, "d"); 179 WRITE_ASS(wakeup_events, PRIu32); 180 WRITE_ASS(bp_type, PRIu32); 181 WRITE_ASS(config1, "llu"); 182 WRITE_ASS(config2, "llu"); 183 WRITE_ASS(branch_sample_type, "llu"); 184 WRITE_ASS(sample_regs_user, "llu"); 185 WRITE_ASS(sample_stack_user, PRIu32); 186 187 fclose(file); 188 return 0; 189 } 190 191 #undef __WRITE_ASS 192 #undef WRITE_ASS 193 194 static void test_attr__open(struct perf_event_attr *attr, pid_t pid, struct perf_cpu cpu, 195 int fd, int group_fd, unsigned long flags) 196 { 197 int errno_saved = errno; 198 199 if ((fd != -1) && store_event(attr, pid, cpu, fd, group_fd, flags)) { 200 pr_err("test attr FAILED"); 201 exit(128); 202 } 203 204 errno = errno_saved; 205 } 206 207 static void evsel__no_extra_fini(struct evsel *evsel __maybe_unused) 208 { 209 } 210 211 static struct { 212 size_t size; 213 int (*init)(struct evsel *evsel); 214 void (*fini)(struct evsel *evsel); 215 } perf_evsel__object = { 216 .size = sizeof(struct evsel), 217 .init = evsel__no_extra_init, 218 .fini = evsel__no_extra_fini, 219 }; 220 221 int evsel__object_config(size_t object_size, int (*init)(struct evsel *evsel), 222 void (*fini)(struct evsel *evsel)) 223 { 224 225 if (object_size == 0) 226 goto set_methods; 227 228 if (perf_evsel__object.size > object_size) 229 return -EINVAL; 230 231 perf_evsel__object.size = object_size; 232 233 set_methods: 234 if (init != NULL) 235 perf_evsel__object.init = init; 236 237 if (fini != NULL) 238 perf_evsel__object.fini = fini; 239 240 return 0; 241 } 242 243 const char *evsel__pmu_name(const struct evsel *evsel) 244 { 245 struct perf_pmu *pmu = evsel__find_pmu(evsel); 246 247 if (pmu) 248 return pmu->name; 249 250 return event_type(evsel->core.attr.type); 251 } 252 253 #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y)) 254 255 int __evsel__sample_size(u64 sample_type) 256 { 257 u64 mask = sample_type & PERF_SAMPLE_MASK; 258 int size = 0; 259 int i; 260 261 for (i = 0; i < 64; i++) { 262 if (mask & (1ULL << i)) 263 size++; 264 } 265 266 size *= sizeof(u64); 267 268 return size; 269 } 270 271 /** 272 * __perf_evsel__calc_id_pos - calculate id_pos. 273 * @sample_type: sample type 274 * 275 * This function returns the position of the event id (PERF_SAMPLE_ID or 276 * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of struct 277 * perf_record_sample. 278 */ 279 static int __perf_evsel__calc_id_pos(u64 sample_type) 280 { 281 int idx = 0; 282 283 if (sample_type & PERF_SAMPLE_IDENTIFIER) 284 return 0; 285 286 if (!(sample_type & PERF_SAMPLE_ID)) 287 return -1; 288 289 if (sample_type & PERF_SAMPLE_IP) 290 idx += 1; 291 292 if (sample_type & PERF_SAMPLE_TID) 293 idx += 1; 294 295 if (sample_type & PERF_SAMPLE_TIME) 296 idx += 1; 297 298 if (sample_type & PERF_SAMPLE_ADDR) 299 idx += 1; 300 301 return idx; 302 } 303 304 /** 305 * __perf_evsel__calc_is_pos - calculate is_pos. 306 * @sample_type: sample type 307 * 308 * This function returns the position (counting backwards) of the event id 309 * (PERF_SAMPLE_ID or PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if 310 * sample_id_all is used there is an id sample appended to non-sample events. 311 */ 312 static int __perf_evsel__calc_is_pos(u64 sample_type) 313 { 314 int idx = 1; 315 316 if (sample_type & PERF_SAMPLE_IDENTIFIER) 317 return 1; 318 319 if (!(sample_type & PERF_SAMPLE_ID)) 320 return -1; 321 322 if (sample_type & PERF_SAMPLE_CPU) 323 idx += 1; 324 325 if (sample_type & PERF_SAMPLE_STREAM_ID) 326 idx += 1; 327 328 return idx; 329 } 330 331 void evsel__calc_id_pos(struct evsel *evsel) 332 { 333 evsel->id_pos = __perf_evsel__calc_id_pos(evsel->core.attr.sample_type); 334 evsel->is_pos = __perf_evsel__calc_is_pos(evsel->core.attr.sample_type); 335 } 336 337 void __evsel__set_sample_bit(struct evsel *evsel, 338 enum perf_event_sample_format bit) 339 { 340 if (!(evsel->core.attr.sample_type & bit)) { 341 evsel->core.attr.sample_type |= bit; 342 evsel->sample_size += sizeof(u64); 343 evsel__calc_id_pos(evsel); 344 } 345 } 346 347 void __evsel__reset_sample_bit(struct evsel *evsel, 348 enum perf_event_sample_format bit) 349 { 350 if (evsel->core.attr.sample_type & bit) { 351 evsel->core.attr.sample_type &= ~bit; 352 evsel->sample_size -= sizeof(u64); 353 evsel__calc_id_pos(evsel); 354 } 355 } 356 357 void evsel__set_sample_id(struct evsel *evsel, 358 bool can_sample_identifier) 359 { 360 if (can_sample_identifier) { 361 evsel__reset_sample_bit(evsel, ID); 362 evsel__set_sample_bit(evsel, IDENTIFIER); 363 } else { 364 evsel__set_sample_bit(evsel, ID); 365 } 366 evsel->core.attr.read_format |= PERF_FORMAT_ID; 367 } 368 369 /** 370 * evsel__is_function_event - Return whether given evsel is a function 371 * trace event 372 * 373 * @evsel - evsel selector to be tested 374 * 375 * Return %true if event is function trace event 376 */ 377 bool evsel__is_function_event(struct evsel *evsel) 378 { 379 #define FUNCTION_EVENT "ftrace:function" 380 381 return evsel->name && 382 !strncmp(FUNCTION_EVENT, evsel->name, sizeof(FUNCTION_EVENT)); 383 384 #undef FUNCTION_EVENT 385 } 386 387 void evsel__init(struct evsel *evsel, 388 struct perf_event_attr *attr, int idx) 389 { 390 perf_evsel__init(&evsel->core, attr, idx); 391 evsel->tracking = !idx; 392 evsel->unit = strdup(""); 393 evsel->scale = 1.0; 394 evsel->max_events = ULONG_MAX; 395 evsel->evlist = NULL; 396 evsel->bpf_obj = NULL; 397 evsel->bpf_fd = -1; 398 INIT_LIST_HEAD(&evsel->config_terms); 399 INIT_LIST_HEAD(&evsel->bpf_counter_list); 400 INIT_LIST_HEAD(&evsel->bpf_filters); 401 perf_evsel__object.init(evsel); 402 evsel->sample_size = __evsel__sample_size(attr->sample_type); 403 evsel__calc_id_pos(evsel); 404 evsel->cmdline_group_boundary = false; 405 evsel->per_pkg_mask = NULL; 406 evsel->collect_stat = false; 407 evsel->group_pmu_name = NULL; 408 evsel->skippable = false; 409 evsel->supported = true; 410 evsel->alternate_hw_config = PERF_COUNT_HW_MAX; 411 evsel->script_output_type = -1; // FIXME: OUTPUT_TYPE_UNSET, see builtin-script.c 412 } 413 414 struct evsel *evsel__new_idx(struct perf_event_attr *attr, int idx) 415 { 416 struct evsel *evsel = zalloc(perf_evsel__object.size); 417 418 if (!evsel) 419 return NULL; 420 evsel__init(evsel, attr, idx); 421 422 if (evsel__is_bpf_output(evsel) && !attr->sample_type) { 423 evsel->core.attr.sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME | 424 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD), 425 evsel->core.attr.sample_period = 1; 426 } 427 428 if (evsel__is_clock(evsel)) { 429 free((char *)evsel->unit); 430 evsel->unit = strdup("msec"); 431 evsel->scale = 1e-6; 432 } 433 434 return evsel; 435 } 436 437 int copy_config_terms(struct list_head *dst, struct list_head *src) 438 { 439 struct evsel_config_term *pos, *tmp; 440 441 list_for_each_entry(pos, src, list) { 442 tmp = malloc(sizeof(*tmp)); 443 if (tmp == NULL) 444 return -ENOMEM; 445 446 *tmp = *pos; 447 if (tmp->free_str) { 448 tmp->val.str = strdup(pos->val.str); 449 if (tmp->val.str == NULL) { 450 free(tmp); 451 return -ENOMEM; 452 } 453 } 454 list_add_tail(&tmp->list, dst); 455 } 456 return 0; 457 } 458 459 static int evsel__copy_config_terms(struct evsel *dst, struct evsel *src) 460 { 461 return copy_config_terms(&dst->config_terms, &src->config_terms); 462 } 463 464 /** 465 * evsel__clone - create a new evsel copied from @orig 466 * @orig: original evsel 467 * 468 * The assumption is that @orig is not configured nor opened yet. 469 * So we only care about the attributes that can be set while it's parsed. 470 */ 471 struct evsel *evsel__clone(struct evsel *dest, struct evsel *orig) 472 { 473 struct evsel *evsel; 474 475 BUG_ON(orig->core.fd); 476 BUG_ON(orig->counts); 477 BUG_ON(orig->priv); 478 BUG_ON(orig->per_pkg_mask); 479 480 /* cannot handle BPF objects for now */ 481 if (orig->bpf_obj) 482 return NULL; 483 484 if (dest) 485 evsel = dest; 486 else 487 evsel = evsel__new(&orig->core.attr); 488 489 if (evsel == NULL) 490 return NULL; 491 492 evsel->core.cpus = perf_cpu_map__get(orig->core.cpus); 493 evsel->core.pmu_cpus = perf_cpu_map__get(orig->core.pmu_cpus); 494 evsel->core.threads = perf_thread_map__get(orig->core.threads); 495 evsel->core.nr_members = orig->core.nr_members; 496 evsel->core.system_wide = orig->core.system_wide; 497 evsel->core.requires_cpu = orig->core.requires_cpu; 498 evsel->core.is_pmu_core = orig->core.is_pmu_core; 499 500 if (orig->name) { 501 evsel->name = strdup(orig->name); 502 if (evsel->name == NULL) 503 goto out_err; 504 } 505 if (orig->group_name) { 506 evsel->group_name = strdup(orig->group_name); 507 if (evsel->group_name == NULL) 508 goto out_err; 509 } 510 if (orig->group_pmu_name) { 511 evsel->group_pmu_name = strdup(orig->group_pmu_name); 512 if (evsel->group_pmu_name == NULL) 513 goto out_err; 514 } 515 if (orig->filter) { 516 evsel->filter = strdup(orig->filter); 517 if (evsel->filter == NULL) 518 goto out_err; 519 } 520 if (orig->metric_id) { 521 evsel->metric_id = strdup(orig->metric_id); 522 if (evsel->metric_id == NULL) 523 goto out_err; 524 } 525 evsel->cgrp = cgroup__get(orig->cgrp); 526 #ifdef HAVE_LIBTRACEEVENT 527 if (orig->tp_sys) { 528 evsel->tp_sys = strdup(orig->tp_sys); 529 if (evsel->tp_sys == NULL) 530 goto out_err; 531 } 532 if (orig->tp_name) { 533 evsel->tp_name = strdup(orig->tp_name); 534 if (evsel->tp_name == NULL) 535 goto out_err; 536 } 537 evsel->tp_format = orig->tp_format; 538 #endif 539 evsel->handler = orig->handler; 540 evsel->core.leader = orig->core.leader; 541 evsel->metric_leader = orig->metric_leader; 542 543 evsel->max_events = orig->max_events; 544 zfree(&evsel->unit); 545 if (orig->unit) { 546 evsel->unit = strdup(orig->unit); 547 if (evsel->unit == NULL) 548 goto out_err; 549 } 550 evsel->scale = orig->scale; 551 evsel->snapshot = orig->snapshot; 552 evsel->per_pkg = orig->per_pkg; 553 evsel->percore = orig->percore; 554 evsel->precise_max = orig->precise_max; 555 evsel->is_libpfm_event = orig->is_libpfm_event; 556 557 evsel->exclude_GH = orig->exclude_GH; 558 evsel->sample_read = orig->sample_read; 559 evsel->collect_stat = orig->collect_stat; 560 evsel->weak_group = orig->weak_group; 561 evsel->use_config_name = orig->use_config_name; 562 evsel->pmu = orig->pmu; 563 evsel->first_wildcard_match = orig->first_wildcard_match; 564 565 if (evsel__copy_config_terms(evsel, orig) < 0) 566 goto out_err; 567 568 evsel->alternate_hw_config = orig->alternate_hw_config; 569 570 return evsel; 571 572 out_err: 573 evsel__delete(evsel); 574 return NULL; 575 } 576 577 /* 578 * Returns pointer with encoded error via <linux/err.h> interface. 579 */ 580 struct evsel *evsel__newtp_idx(const char *sys, const char *name, int idx, bool format) 581 { 582 struct perf_event_attr attr = { 583 .type = PERF_TYPE_TRACEPOINT, 584 .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME | 585 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD), 586 }; 587 struct evsel *evsel = zalloc(perf_evsel__object.size); 588 int err = -ENOMEM, id = -1; 589 590 if (evsel == NULL) 591 goto out_err; 592 593 594 if (asprintf(&evsel->name, "%s:%s", sys, name) < 0) 595 goto out_free; 596 597 #ifdef HAVE_LIBTRACEEVENT 598 evsel->tp_sys = strdup(sys); 599 if (!evsel->tp_sys) 600 goto out_free; 601 602 evsel->tp_name = strdup(name); 603 if (!evsel->tp_name) 604 goto out_free; 605 #endif 606 607 event_attr_init(&attr); 608 609 if (format) { 610 id = tp_pmu__id(sys, name); 611 if (id < 0) { 612 err = id; 613 goto out_free; 614 } 615 } 616 attr.config = (__u64)id; 617 attr.sample_period = 1; 618 evsel__init(evsel, &attr, idx); 619 return evsel; 620 621 out_free: 622 zfree(&evsel->name); 623 #ifdef HAVE_LIBTRACEEVENT 624 zfree(&evsel->tp_sys); 625 zfree(&evsel->tp_name); 626 #endif 627 free(evsel); 628 out_err: 629 return ERR_PTR(err); 630 } 631 632 #ifdef HAVE_LIBTRACEEVENT 633 struct tep_event *evsel__tp_format(struct evsel *evsel) 634 { 635 struct tep_event *tp_format = evsel->tp_format; 636 637 if (tp_format) 638 return tp_format; 639 640 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT) 641 return NULL; 642 643 if (!evsel->tp_sys) 644 tp_format = trace_event__tp_format_id(evsel->core.attr.config); 645 else 646 tp_format = trace_event__tp_format(evsel->tp_sys, evsel->tp_name); 647 648 if (IS_ERR(tp_format)) { 649 int err = -PTR_ERR(evsel->tp_format); 650 651 pr_err("Error getting tracepoint format '%s' '%s'(%d)\n", 652 evsel__name(evsel), strerror(err), err); 653 return NULL; 654 } 655 evsel->tp_format = tp_format; 656 return evsel->tp_format; 657 } 658 #endif 659 660 const char *const evsel__hw_names[PERF_COUNT_HW_MAX] = { 661 "cycles", 662 "instructions", 663 "cache-references", 664 "cache-misses", 665 "branches", 666 "branch-misses", 667 "bus-cycles", 668 "stalled-cycles-frontend", 669 "stalled-cycles-backend", 670 "ref-cycles", 671 }; 672 673 char *evsel__bpf_counter_events; 674 675 bool evsel__match_bpf_counter_events(const char *name) 676 { 677 int name_len; 678 bool match; 679 char *ptr; 680 681 if (!evsel__bpf_counter_events) 682 return false; 683 684 ptr = strstr(evsel__bpf_counter_events, name); 685 name_len = strlen(name); 686 687 /* check name matches a full token in evsel__bpf_counter_events */ 688 match = (ptr != NULL) && 689 ((ptr == evsel__bpf_counter_events) || (*(ptr - 1) == ',')) && 690 ((*(ptr + name_len) == ',') || (*(ptr + name_len) == '\0')); 691 692 return match; 693 } 694 695 static const char *__evsel__hw_name(u64 config) 696 { 697 if (config < PERF_COUNT_HW_MAX && evsel__hw_names[config]) 698 return evsel__hw_names[config]; 699 700 return "unknown-hardware"; 701 } 702 703 static int evsel__add_modifiers(struct evsel *evsel, char *bf, size_t size) 704 { 705 int colon = 0, r = 0; 706 struct perf_event_attr *attr = &evsel->core.attr; 707 708 #define MOD_PRINT(context, mod) do { \ 709 if (!attr->exclude_##context) { \ 710 if (!colon) colon = ++r; \ 711 r += scnprintf(bf + r, size - r, "%c", mod); \ 712 } } while(0) 713 714 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) { 715 MOD_PRINT(kernel, 'k'); 716 MOD_PRINT(user, 'u'); 717 MOD_PRINT(hv, 'h'); 718 } 719 720 if (attr->precise_ip) { 721 if (!colon) 722 colon = ++r; 723 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp"); 724 } 725 726 if (attr->exclude_host || attr->exclude_guest) { 727 MOD_PRINT(host, 'H'); 728 MOD_PRINT(guest, 'G'); 729 } 730 #undef MOD_PRINT 731 if (colon) 732 bf[colon - 1] = ':'; 733 return r; 734 } 735 736 int __weak arch_evsel__hw_name(struct evsel *evsel, char *bf, size_t size) 737 { 738 return scnprintf(bf, size, "%s", __evsel__hw_name(evsel->core.attr.config)); 739 } 740 741 static int evsel__hw_name(struct evsel *evsel, char *bf, size_t size) 742 { 743 int r = arch_evsel__hw_name(evsel, bf, size); 744 return r + evsel__add_modifiers(evsel, bf + r, size - r); 745 } 746 747 const char *const evsel__sw_names[PERF_COUNT_SW_MAX] = { 748 "cpu-clock", 749 "task-clock", 750 "page-faults", 751 "context-switches", 752 "cpu-migrations", 753 "minor-faults", 754 "major-faults", 755 "alignment-faults", 756 "emulation-faults", 757 "dummy", 758 }; 759 760 static const char *__evsel__sw_name(u64 config) 761 { 762 if (config < PERF_COUNT_SW_MAX && evsel__sw_names[config]) 763 return evsel__sw_names[config]; 764 return "unknown-software"; 765 } 766 767 static int evsel__sw_name(struct evsel *evsel, char *bf, size_t size) 768 { 769 int r = scnprintf(bf, size, "%s", __evsel__sw_name(evsel->core.attr.config)); 770 return r + evsel__add_modifiers(evsel, bf + r, size - r); 771 } 772 773 static int __evsel__bp_name(char *bf, size_t size, u64 addr, u64 type) 774 { 775 int r; 776 777 r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr); 778 779 if (type & HW_BREAKPOINT_R) 780 r += scnprintf(bf + r, size - r, "r"); 781 782 if (type & HW_BREAKPOINT_W) 783 r += scnprintf(bf + r, size - r, "w"); 784 785 if (type & HW_BREAKPOINT_X) 786 r += scnprintf(bf + r, size - r, "x"); 787 788 return r; 789 } 790 791 static int evsel__bp_name(struct evsel *evsel, char *bf, size_t size) 792 { 793 struct perf_event_attr *attr = &evsel->core.attr; 794 int r = __evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type); 795 return r + evsel__add_modifiers(evsel, bf + r, size - r); 796 } 797 798 const char *const evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX][EVSEL__MAX_ALIASES] = { 799 { "L1-dcache", "l1-d", "l1d", "L1-data", }, 800 { "L1-icache", "l1-i", "l1i", "L1-instruction", }, 801 { "LLC", "L2", }, 802 { "dTLB", "d-tlb", "Data-TLB", }, 803 { "iTLB", "i-tlb", "Instruction-TLB", }, 804 { "branch", "branches", "bpu", "btb", "bpc", }, 805 { "node", }, 806 }; 807 808 const char *const evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX][EVSEL__MAX_ALIASES] = { 809 { "load", "loads", "read", }, 810 { "store", "stores", "write", }, 811 { "prefetch", "prefetches", "speculative-read", "speculative-load", }, 812 }; 813 814 const char *const evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX][EVSEL__MAX_ALIASES] = { 815 { "refs", "Reference", "ops", "access", }, 816 { "misses", "miss", }, 817 }; 818 819 #define C(x) PERF_COUNT_HW_CACHE_##x 820 #define CACHE_READ (1 << C(OP_READ)) 821 #define CACHE_WRITE (1 << C(OP_WRITE)) 822 #define CACHE_PREFETCH (1 << C(OP_PREFETCH)) 823 #define COP(x) (1 << x) 824 825 /* 826 * cache operation stat 827 * L1I : Read and prefetch only 828 * ITLB and BPU : Read-only 829 */ 830 static const unsigned long evsel__hw_cache_stat[C(MAX)] = { 831 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH), 832 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH), 833 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH), 834 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH), 835 [C(ITLB)] = (CACHE_READ), 836 [C(BPU)] = (CACHE_READ), 837 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH), 838 }; 839 840 bool evsel__is_cache_op_valid(u8 type, u8 op) 841 { 842 if (evsel__hw_cache_stat[type] & COP(op)) 843 return true; /* valid */ 844 else 845 return false; /* invalid */ 846 } 847 848 int __evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result, char *bf, size_t size) 849 { 850 if (result) { 851 return scnprintf(bf, size, "%s-%s-%s", evsel__hw_cache[type][0], 852 evsel__hw_cache_op[op][0], 853 evsel__hw_cache_result[result][0]); 854 } 855 856 return scnprintf(bf, size, "%s-%s", evsel__hw_cache[type][0], 857 evsel__hw_cache_op[op][1]); 858 } 859 860 static int __evsel__hw_cache_name(u64 config, char *bf, size_t size) 861 { 862 u8 op, result, type = (config >> 0) & 0xff; 863 const char *err = "unknown-ext-hardware-cache-type"; 864 865 if (type >= PERF_COUNT_HW_CACHE_MAX) 866 goto out_err; 867 868 op = (config >> 8) & 0xff; 869 err = "unknown-ext-hardware-cache-op"; 870 if (op >= PERF_COUNT_HW_CACHE_OP_MAX) 871 goto out_err; 872 873 result = (config >> 16) & 0xff; 874 err = "unknown-ext-hardware-cache-result"; 875 if (result >= PERF_COUNT_HW_CACHE_RESULT_MAX) 876 goto out_err; 877 878 err = "invalid-cache"; 879 if (!evsel__is_cache_op_valid(type, op)) 880 goto out_err; 881 882 return __evsel__hw_cache_type_op_res_name(type, op, result, bf, size); 883 out_err: 884 return scnprintf(bf, size, "%s", err); 885 } 886 887 static int evsel__hw_cache_name(struct evsel *evsel, char *bf, size_t size) 888 { 889 int ret = __evsel__hw_cache_name(evsel->core.attr.config, bf, size); 890 return ret + evsel__add_modifiers(evsel, bf + ret, size - ret); 891 } 892 893 static int evsel__raw_name(struct evsel *evsel, char *bf, size_t size) 894 { 895 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->core.attr.config); 896 return ret + evsel__add_modifiers(evsel, bf + ret, size - ret); 897 } 898 899 const char *evsel__name(struct evsel *evsel) 900 { 901 char bf[128]; 902 903 if (!evsel) 904 goto out_unknown; 905 906 if (evsel->name) 907 return evsel->name; 908 909 switch (evsel->core.attr.type) { 910 case PERF_TYPE_RAW: 911 evsel__raw_name(evsel, bf, sizeof(bf)); 912 break; 913 914 case PERF_TYPE_HARDWARE: 915 evsel__hw_name(evsel, bf, sizeof(bf)); 916 break; 917 918 case PERF_TYPE_HW_CACHE: 919 evsel__hw_cache_name(evsel, bf, sizeof(bf)); 920 break; 921 922 case PERF_TYPE_SOFTWARE: 923 evsel__sw_name(evsel, bf, sizeof(bf)); 924 break; 925 926 case PERF_TYPE_TRACEPOINT: 927 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint"); 928 break; 929 930 case PERF_TYPE_BREAKPOINT: 931 evsel__bp_name(evsel, bf, sizeof(bf)); 932 break; 933 934 case PERF_PMU_TYPE_TOOL: 935 scnprintf(bf, sizeof(bf), "%s", evsel__tool_pmu_event_name(evsel)); 936 break; 937 938 default: 939 scnprintf(bf, sizeof(bf), "unknown attr type: %d", 940 evsel->core.attr.type); 941 break; 942 } 943 944 evsel->name = strdup(bf); 945 946 if (evsel->name) 947 return evsel->name; 948 out_unknown: 949 return "unknown"; 950 } 951 952 bool evsel__name_is(struct evsel *evsel, const char *name) 953 { 954 return !strcmp(evsel__name(evsel), name); 955 } 956 957 const char *evsel__metric_id(const struct evsel *evsel) 958 { 959 if (evsel->metric_id) 960 return evsel->metric_id; 961 962 if (evsel__is_tool(evsel)) 963 return evsel__tool_pmu_event_name(evsel); 964 965 return "unknown"; 966 } 967 968 const char *evsel__group_name(struct evsel *evsel) 969 { 970 return evsel->group_name ?: "anon group"; 971 } 972 973 /* 974 * Returns the group details for the specified leader, 975 * with following rules. 976 * 977 * For record -e '{cycles,instructions}' 978 * 'anon group { cycles:u, instructions:u }' 979 * 980 * For record -e 'cycles,instructions' and report --group 981 * 'cycles:u, instructions:u' 982 */ 983 int evsel__group_desc(struct evsel *evsel, char *buf, size_t size) 984 { 985 int ret = 0; 986 bool first = true; 987 struct evsel *pos; 988 const char *group_name = evsel__group_name(evsel); 989 990 if (!evsel->forced_leader) 991 ret = scnprintf(buf, size, "%s { ", group_name); 992 993 for_each_group_evsel(pos, evsel) { 994 if (symbol_conf.skip_empty && 995 evsel__hists(pos)->stats.nr_samples == 0) 996 continue; 997 998 ret += scnprintf(buf + ret, size - ret, "%s%s", 999 first ? "" : ", ", evsel__name(pos)); 1000 first = false; 1001 } 1002 1003 if (!evsel->forced_leader) 1004 ret += scnprintf(buf + ret, size - ret, " }"); 1005 1006 return ret; 1007 } 1008 1009 static void __evsel__config_callchain(struct evsel *evsel, struct record_opts *opts, 1010 struct callchain_param *param) 1011 { 1012 bool function = evsel__is_function_event(evsel); 1013 struct perf_event_attr *attr = &evsel->core.attr; 1014 1015 evsel__set_sample_bit(evsel, CALLCHAIN); 1016 1017 attr->sample_max_stack = param->max_stack; 1018 1019 if (opts->kernel_callchains) 1020 attr->exclude_callchain_user = 1; 1021 if (opts->user_callchains) 1022 attr->exclude_callchain_kernel = 1; 1023 if (param->record_mode == CALLCHAIN_LBR) { 1024 if (!opts->branch_stack) { 1025 if (attr->exclude_user) { 1026 pr_warning("LBR callstack option is only available " 1027 "to get user callchain information. " 1028 "Falling back to framepointers.\n"); 1029 } else { 1030 evsel__set_sample_bit(evsel, BRANCH_STACK); 1031 attr->branch_sample_type = PERF_SAMPLE_BRANCH_USER | 1032 PERF_SAMPLE_BRANCH_CALL_STACK | 1033 PERF_SAMPLE_BRANCH_NO_CYCLES | 1034 PERF_SAMPLE_BRANCH_NO_FLAGS | 1035 PERF_SAMPLE_BRANCH_HW_INDEX; 1036 } 1037 } else 1038 pr_warning("Cannot use LBR callstack with branch stack. " 1039 "Falling back to framepointers.\n"); 1040 } 1041 1042 if (param->record_mode == CALLCHAIN_DWARF) { 1043 if (!function) { 1044 const char *arch = perf_env__arch(evsel__env(evsel)); 1045 1046 evsel__set_sample_bit(evsel, REGS_USER); 1047 evsel__set_sample_bit(evsel, STACK_USER); 1048 if (opts->sample_user_regs && 1049 DWARF_MINIMAL_REGS(arch) != arch__user_reg_mask()) { 1050 attr->sample_regs_user |= DWARF_MINIMAL_REGS(arch); 1051 pr_warning("WARNING: The use of --call-graph=dwarf may require all the user registers, " 1052 "specifying a subset with --user-regs may render DWARF unwinding unreliable, " 1053 "so the minimal registers set (IP, SP) is explicitly forced.\n"); 1054 } else { 1055 attr->sample_regs_user |= arch__user_reg_mask(); 1056 } 1057 attr->sample_stack_user = param->dump_size; 1058 attr->exclude_callchain_user = 1; 1059 } else { 1060 pr_info("Cannot use DWARF unwind for function trace event," 1061 " falling back to framepointers.\n"); 1062 } 1063 } 1064 1065 if (function) { 1066 pr_info("Disabling user space callchains for function trace event.\n"); 1067 attr->exclude_callchain_user = 1; 1068 } 1069 } 1070 1071 void evsel__config_callchain(struct evsel *evsel, struct record_opts *opts, 1072 struct callchain_param *param) 1073 { 1074 if (param->enabled) 1075 return __evsel__config_callchain(evsel, opts, param); 1076 } 1077 1078 static void evsel__reset_callgraph(struct evsel *evsel, struct callchain_param *param) 1079 { 1080 struct perf_event_attr *attr = &evsel->core.attr; 1081 1082 evsel__reset_sample_bit(evsel, CALLCHAIN); 1083 if (param->record_mode == CALLCHAIN_LBR) { 1084 evsel__reset_sample_bit(evsel, BRANCH_STACK); 1085 attr->branch_sample_type &= ~(PERF_SAMPLE_BRANCH_USER | 1086 PERF_SAMPLE_BRANCH_CALL_STACK | 1087 PERF_SAMPLE_BRANCH_HW_INDEX); 1088 } 1089 if (param->record_mode == CALLCHAIN_DWARF) { 1090 evsel__reset_sample_bit(evsel, REGS_USER); 1091 evsel__reset_sample_bit(evsel, STACK_USER); 1092 } 1093 } 1094 1095 static void evsel__apply_ratio_to_prev(struct evsel *evsel, 1096 struct perf_event_attr *attr, 1097 struct record_opts *opts, 1098 const char *buf) 1099 { 1100 struct perf_event_attr *prev_attr = NULL; 1101 struct evsel *evsel_prev = NULL; 1102 u64 type = evsel->core.attr.sample_type; 1103 u64 prev_type = 0; 1104 double rtp; 1105 1106 rtp = strtod(buf, NULL); 1107 if (rtp <= 0) { 1108 pr_err("Invalid ratio-to-prev value %lf\n", rtp); 1109 return; 1110 } 1111 if (evsel == evsel__leader(evsel)) { 1112 pr_err("Invalid use of ratio-to-prev term without preceding element in group\n"); 1113 return; 1114 } 1115 if (!evsel->pmu->is_core) { 1116 pr_err("Event using ratio-to-prev term must have a core PMU\n"); 1117 return; 1118 } 1119 1120 evsel_prev = evsel__prev(evsel); 1121 if (!evsel_prev) { 1122 pr_err("Previous event does not exist.\n"); 1123 return; 1124 } 1125 1126 if (evsel_prev->pmu->type != evsel->pmu->type) { 1127 pr_err("Compared events (\"%s\", \"%s\") must have same PMU\n", 1128 evsel->name, evsel_prev->name); 1129 return; 1130 } 1131 1132 prev_attr = &evsel_prev->core.attr; 1133 prev_type = evsel_prev->core.attr.sample_type; 1134 1135 if (!(prev_type & PERF_SAMPLE_PERIOD)) { 1136 attr->sample_period = prev_attr->sample_period * rtp; 1137 attr->freq = 0; 1138 evsel__reset_sample_bit(evsel, PERIOD); 1139 } else if (!(type & PERF_SAMPLE_PERIOD)) { 1140 prev_attr->sample_period = attr->sample_period / rtp; 1141 prev_attr->freq = 0; 1142 evsel__reset_sample_bit(evsel_prev, PERIOD); 1143 } else { 1144 if (opts->user_interval != ULLONG_MAX) { 1145 prev_attr->sample_period = opts->user_interval; 1146 attr->sample_period = prev_attr->sample_period * rtp; 1147 prev_attr->freq = 0; 1148 attr->freq = 0; 1149 evsel__reset_sample_bit(evsel_prev, PERIOD); 1150 evsel__reset_sample_bit(evsel, PERIOD); 1151 } else { 1152 pr_err("Event period term or count (-c) must be set when using ratio-to-prev term.\n"); 1153 return; 1154 } 1155 } 1156 1157 arch_evsel__apply_ratio_to_prev(evsel, attr); 1158 } 1159 1160 static void evsel__apply_config_terms(struct evsel *evsel, 1161 struct record_opts *opts, bool track) 1162 { 1163 struct evsel_config_term *term; 1164 struct list_head *config_terms = &evsel->config_terms; 1165 struct perf_event_attr *attr = &evsel->core.attr; 1166 /* callgraph default */ 1167 struct callchain_param param = { 1168 .record_mode = callchain_param.record_mode, 1169 }; 1170 u32 dump_size = 0; 1171 int max_stack = 0; 1172 const char *callgraph_buf = NULL; 1173 const char *rtp_buf = NULL; 1174 1175 list_for_each_entry(term, config_terms, list) { 1176 switch (term->type) { 1177 case EVSEL__CONFIG_TERM_PERIOD: 1178 if (!(term->weak && opts->user_interval != ULLONG_MAX)) { 1179 attr->sample_period = term->val.period; 1180 attr->freq = 0; 1181 evsel__reset_sample_bit(evsel, PERIOD); 1182 } 1183 break; 1184 case EVSEL__CONFIG_TERM_FREQ: 1185 if (!(term->weak && opts->user_freq != UINT_MAX)) { 1186 attr->sample_freq = term->val.freq; 1187 attr->freq = 1; 1188 evsel__set_sample_bit(evsel, PERIOD); 1189 } 1190 break; 1191 case EVSEL__CONFIG_TERM_TIME: 1192 if (term->val.time) 1193 evsel__set_sample_bit(evsel, TIME); 1194 else 1195 evsel__reset_sample_bit(evsel, TIME); 1196 break; 1197 case EVSEL__CONFIG_TERM_CALLGRAPH: 1198 callgraph_buf = term->val.str; 1199 break; 1200 case EVSEL__CONFIG_TERM_BRANCH: 1201 if (term->val.str && strcmp(term->val.str, "no")) { 1202 evsel__set_sample_bit(evsel, BRANCH_STACK); 1203 parse_branch_str(term->val.str, 1204 &attr->branch_sample_type); 1205 } else 1206 evsel__reset_sample_bit(evsel, BRANCH_STACK); 1207 break; 1208 case EVSEL__CONFIG_TERM_STACK_USER: 1209 dump_size = term->val.stack_user; 1210 break; 1211 case EVSEL__CONFIG_TERM_MAX_STACK: 1212 max_stack = term->val.max_stack; 1213 break; 1214 case EVSEL__CONFIG_TERM_MAX_EVENTS: 1215 evsel->max_events = term->val.max_events; 1216 break; 1217 case EVSEL__CONFIG_TERM_INHERIT: 1218 /* 1219 * attr->inherit should has already been set by 1220 * evsel__config. If user explicitly set 1221 * inherit using config terms, override global 1222 * opt->no_inherit setting. 1223 */ 1224 attr->inherit = term->val.inherit ? 1 : 0; 1225 break; 1226 case EVSEL__CONFIG_TERM_OVERWRITE: 1227 attr->write_backward = term->val.overwrite ? 1 : 0; 1228 break; 1229 case EVSEL__CONFIG_TERM_DRV_CFG: 1230 break; 1231 case EVSEL__CONFIG_TERM_PERCORE: 1232 break; 1233 case EVSEL__CONFIG_TERM_AUX_OUTPUT: 1234 attr->aux_output = term->val.aux_output ? 1 : 0; 1235 break; 1236 case EVSEL__CONFIG_TERM_AUX_ACTION: 1237 /* Already applied by auxtrace */ 1238 break; 1239 case EVSEL__CONFIG_TERM_AUX_SAMPLE_SIZE: 1240 /* Already applied by auxtrace */ 1241 break; 1242 case EVSEL__CONFIG_TERM_CFG_CHG: 1243 break; 1244 case EVSEL__CONFIG_TERM_RATIO_TO_PREV: 1245 rtp_buf = term->val.str; 1246 break; 1247 default: 1248 break; 1249 } 1250 } 1251 1252 /* User explicitly set per-event callgraph, clear the old setting and reset. */ 1253 if ((callgraph_buf != NULL) || (dump_size > 0) || max_stack) { 1254 bool sample_address = false; 1255 1256 if (max_stack) { 1257 param.max_stack = max_stack; 1258 if (callgraph_buf == NULL) 1259 callgraph_buf = "fp"; 1260 } 1261 1262 /* parse callgraph parameters */ 1263 if (callgraph_buf != NULL) { 1264 if (!strcmp(callgraph_buf, "no")) { 1265 param.enabled = false; 1266 param.record_mode = CALLCHAIN_NONE; 1267 } else { 1268 param.enabled = true; 1269 if (parse_callchain_record(callgraph_buf, ¶m)) { 1270 pr_err("per-event callgraph setting for %s failed. " 1271 "Apply callgraph global setting for it\n", 1272 evsel->name); 1273 return; 1274 } 1275 if (param.record_mode == CALLCHAIN_DWARF) 1276 sample_address = true; 1277 } 1278 } 1279 if (dump_size > 0) { 1280 dump_size = round_up(dump_size, sizeof(u64)); 1281 param.dump_size = dump_size; 1282 } 1283 1284 /* If global callgraph set, clear it */ 1285 if (callchain_param.enabled) 1286 evsel__reset_callgraph(evsel, &callchain_param); 1287 1288 /* set perf-event callgraph */ 1289 if (param.enabled) { 1290 if (sample_address) { 1291 evsel__set_sample_bit(evsel, ADDR); 1292 evsel__set_sample_bit(evsel, DATA_SRC); 1293 evsel->core.attr.mmap_data = track; 1294 } 1295 evsel__config_callchain(evsel, opts, ¶m); 1296 } 1297 } 1298 if (rtp_buf) 1299 evsel__apply_ratio_to_prev(evsel, attr, opts, rtp_buf); 1300 } 1301 1302 struct evsel_config_term *__evsel__get_config_term(struct evsel *evsel, enum evsel_term_type type) 1303 { 1304 struct evsel_config_term *term, *found_term = NULL; 1305 1306 list_for_each_entry(term, &evsel->config_terms, list) { 1307 if (term->type == type) 1308 found_term = term; 1309 } 1310 1311 return found_term; 1312 } 1313 1314 void __weak arch_evsel__set_sample_weight(struct evsel *evsel) 1315 { 1316 evsel__set_sample_bit(evsel, WEIGHT); 1317 } 1318 1319 void __weak arch__post_evsel_config(struct evsel *evsel __maybe_unused, 1320 struct perf_event_attr *attr __maybe_unused) 1321 { 1322 } 1323 1324 void __weak arch_evsel__apply_ratio_to_prev(struct evsel *evsel __maybe_unused, 1325 struct perf_event_attr *attr __maybe_unused) 1326 { 1327 } 1328 1329 static void evsel__set_default_freq_period(struct record_opts *opts, 1330 struct perf_event_attr *attr) 1331 { 1332 if (opts->freq) { 1333 attr->freq = 1; 1334 attr->sample_freq = opts->freq; 1335 } else { 1336 attr->sample_period = opts->default_interval; 1337 } 1338 } 1339 1340 bool evsel__is_offcpu_event(struct evsel *evsel) 1341 { 1342 return evsel__is_bpf_output(evsel) && evsel__name_is(evsel, OFFCPU_EVENT) && 1343 evsel->core.attr.sample_type & PERF_SAMPLE_RAW; 1344 } 1345 1346 /* 1347 * The enable_on_exec/disabled value strategy: 1348 * 1349 * 1) For any type of traced program: 1350 * - all independent events and group leaders are disabled 1351 * - all group members are enabled 1352 * 1353 * Group members are ruled by group leaders. They need to 1354 * be enabled, because the group scheduling relies on that. 1355 * 1356 * 2) For traced programs executed by perf: 1357 * - all independent events and group leaders have 1358 * enable_on_exec set 1359 * - we don't specifically enable or disable any event during 1360 * the record command 1361 * 1362 * Independent events and group leaders are initially disabled 1363 * and get enabled by exec. Group members are ruled by group 1364 * leaders as stated in 1). 1365 * 1366 * 3) For traced programs attached by perf (pid/tid): 1367 * - we specifically enable or disable all events during 1368 * the record command 1369 * 1370 * When attaching events to already running traced we 1371 * enable/disable events specifically, as there's no 1372 * initial traced exec call. 1373 */ 1374 void evsel__config(struct evsel *evsel, struct record_opts *opts, 1375 struct callchain_param *callchain) 1376 { 1377 struct evsel *leader = evsel__leader(evsel); 1378 struct perf_event_attr *attr = &evsel->core.attr; 1379 int track = evsel->tracking; 1380 bool per_cpu = opts->target.default_per_cpu && !opts->target.per_thread; 1381 1382 attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1; 1383 attr->inherit = target__has_cpu(&opts->target) ? 0 : !opts->no_inherit; 1384 attr->write_backward = opts->overwrite ? 1 : 0; 1385 attr->read_format = PERF_FORMAT_LOST; 1386 1387 evsel__set_sample_bit(evsel, IP); 1388 evsel__set_sample_bit(evsel, TID); 1389 1390 if (evsel->sample_read) { 1391 evsel__set_sample_bit(evsel, READ); 1392 1393 /* 1394 * We need ID even in case of single event, because 1395 * PERF_SAMPLE_READ process ID specific data. 1396 */ 1397 evsel__set_sample_id(evsel, false); 1398 1399 /* 1400 * Apply group format only if we belong to group 1401 * with more than one members. 1402 */ 1403 if (leader->core.nr_members > 1) { 1404 attr->read_format |= PERF_FORMAT_GROUP; 1405 } 1406 1407 /* 1408 * Inherit + SAMPLE_READ requires SAMPLE_TID in the read_format 1409 */ 1410 if (attr->inherit) { 1411 evsel__set_sample_bit(evsel, TID); 1412 evsel->core.attr.read_format |= 1413 PERF_FORMAT_ID; 1414 } 1415 } 1416 1417 /* 1418 * We default some events to have a default interval. But keep 1419 * it a weak assumption overridable by the user. 1420 */ 1421 if ((evsel->is_libpfm_event && !attr->sample_period) || 1422 (!evsel->is_libpfm_event && (!attr->sample_period || 1423 opts->user_freq != UINT_MAX || 1424 opts->user_interval != ULLONG_MAX))) 1425 evsel__set_default_freq_period(opts, attr); 1426 1427 /* 1428 * If attr->freq was set (here or earlier), ask for period 1429 * to be sampled. 1430 */ 1431 if (attr->freq) 1432 evsel__set_sample_bit(evsel, PERIOD); 1433 1434 if (opts->no_samples) 1435 attr->sample_freq = 0; 1436 1437 if (opts->inherit_stat) { 1438 evsel->core.attr.read_format |= 1439 PERF_FORMAT_TOTAL_TIME_ENABLED | 1440 PERF_FORMAT_TOTAL_TIME_RUNNING | 1441 PERF_FORMAT_ID; 1442 attr->inherit_stat = 1; 1443 } 1444 1445 if (opts->sample_address) { 1446 evsel__set_sample_bit(evsel, ADDR); 1447 attr->mmap_data = track; 1448 } 1449 1450 /* 1451 * We don't allow user space callchains for function trace 1452 * event, due to issues with page faults while tracing page 1453 * fault handler and its overall trickiness nature. 1454 */ 1455 if (evsel__is_function_event(evsel)) 1456 evsel->core.attr.exclude_callchain_user = 1; 1457 1458 if (callchain && callchain->enabled && !evsel->no_aux_samples) 1459 evsel__config_callchain(evsel, opts, callchain); 1460 1461 if (opts->sample_intr_regs && !evsel->no_aux_samples && 1462 !evsel__is_dummy_event(evsel)) { 1463 attr->sample_regs_intr = opts->sample_intr_regs; 1464 evsel__set_sample_bit(evsel, REGS_INTR); 1465 } 1466 1467 if (opts->sample_user_regs && !evsel->no_aux_samples && 1468 !evsel__is_dummy_event(evsel)) { 1469 attr->sample_regs_user |= opts->sample_user_regs; 1470 evsel__set_sample_bit(evsel, REGS_USER); 1471 } 1472 1473 if (target__has_cpu(&opts->target) || opts->sample_cpu) 1474 evsel__set_sample_bit(evsel, CPU); 1475 1476 /* 1477 * When the user explicitly disabled time don't force it here. 1478 */ 1479 if (opts->sample_time && 1480 (!perf_missing_features.sample_id_all && 1481 (!opts->no_inherit || target__has_cpu(&opts->target) || per_cpu || 1482 opts->sample_time_set))) 1483 evsel__set_sample_bit(evsel, TIME); 1484 1485 if (opts->raw_samples && !evsel->no_aux_samples) { 1486 evsel__set_sample_bit(evsel, TIME); 1487 evsel__set_sample_bit(evsel, RAW); 1488 evsel__set_sample_bit(evsel, CPU); 1489 } 1490 1491 if (opts->sample_data_src) 1492 evsel__set_sample_bit(evsel, DATA_SRC); 1493 1494 if (opts->sample_phys_addr) 1495 evsel__set_sample_bit(evsel, PHYS_ADDR); 1496 1497 if (opts->no_buffering) { 1498 attr->watermark = 0; 1499 attr->wakeup_events = 1; 1500 } 1501 if (opts->branch_stack && !evsel->no_aux_samples) { 1502 evsel__set_sample_bit(evsel, BRANCH_STACK); 1503 attr->branch_sample_type = opts->branch_stack; 1504 } 1505 1506 if (opts->sample_weight || evsel->retire_lat) { 1507 arch_evsel__set_sample_weight(evsel); 1508 evsel->retire_lat = false; 1509 } 1510 attr->task = track; 1511 attr->mmap = track; 1512 attr->mmap2 = track && !perf_missing_features.mmap2; 1513 attr->comm = track; 1514 attr->build_id = track && opts->build_id; 1515 1516 /* 1517 * ksymbol is tracked separately with text poke because it needs to be 1518 * system wide and enabled immediately. 1519 */ 1520 if (!opts->text_poke) 1521 attr->ksymbol = track && !perf_missing_features.ksymbol; 1522 attr->bpf_event = track && !opts->no_bpf_event && !perf_missing_features.bpf; 1523 1524 if (opts->record_namespaces) 1525 attr->namespaces = track; 1526 1527 if (opts->record_cgroup) { 1528 attr->cgroup = track && !perf_missing_features.cgroup; 1529 evsel__set_sample_bit(evsel, CGROUP); 1530 } 1531 1532 if (opts->sample_data_page_size) 1533 evsel__set_sample_bit(evsel, DATA_PAGE_SIZE); 1534 1535 if (opts->sample_code_page_size) 1536 evsel__set_sample_bit(evsel, CODE_PAGE_SIZE); 1537 1538 if (opts->record_switch_events) 1539 attr->context_switch = track; 1540 1541 if (opts->sample_transaction) 1542 evsel__set_sample_bit(evsel, TRANSACTION); 1543 1544 if (opts->running_time) { 1545 evsel->core.attr.read_format |= 1546 PERF_FORMAT_TOTAL_TIME_ENABLED | 1547 PERF_FORMAT_TOTAL_TIME_RUNNING; 1548 } 1549 1550 /* 1551 * XXX see the function comment above 1552 * 1553 * Disabling only independent events or group leaders, 1554 * keeping group members enabled. 1555 */ 1556 if (evsel__is_group_leader(evsel)) 1557 attr->disabled = 1; 1558 1559 /* 1560 * Setting enable_on_exec for independent events and 1561 * group leaders for traced executed by perf. 1562 */ 1563 if (target__none(&opts->target) && evsel__is_group_leader(evsel) && 1564 !opts->target.initial_delay) 1565 attr->enable_on_exec = 1; 1566 1567 if (evsel->immediate) { 1568 attr->disabled = 0; 1569 attr->enable_on_exec = 0; 1570 } 1571 1572 clockid = opts->clockid; 1573 if (opts->use_clockid) { 1574 attr->use_clockid = 1; 1575 attr->clockid = opts->clockid; 1576 } 1577 1578 if (evsel->precise_max) 1579 attr->precise_ip = 3; 1580 1581 if (opts->all_user) { 1582 attr->exclude_kernel = 1; 1583 attr->exclude_user = 0; 1584 } 1585 1586 if (opts->all_kernel) { 1587 attr->exclude_kernel = 0; 1588 attr->exclude_user = 1; 1589 } 1590 1591 if (evsel->core.pmu_cpus || evsel->unit) 1592 evsel->core.attr.read_format |= PERF_FORMAT_ID; 1593 1594 /* 1595 * Apply event specific term settings, 1596 * it overloads any global configuration. 1597 */ 1598 evsel__apply_config_terms(evsel, opts, track); 1599 1600 evsel->ignore_missing_thread = opts->ignore_missing_thread; 1601 1602 /* The --period option takes the precedence. */ 1603 if (opts->period_set) { 1604 if (opts->period) 1605 evsel__set_sample_bit(evsel, PERIOD); 1606 else 1607 evsel__reset_sample_bit(evsel, PERIOD); 1608 } 1609 1610 /* 1611 * A dummy event never triggers any actual counter and therefore 1612 * cannot be used with branch_stack. 1613 * 1614 * For initial_delay, a dummy event is added implicitly. 1615 * The software event will trigger -EOPNOTSUPP error out, 1616 * if BRANCH_STACK bit is set. 1617 */ 1618 if (evsel__is_dummy_event(evsel)) 1619 evsel__reset_sample_bit(evsel, BRANCH_STACK); 1620 1621 if (evsel__is_offcpu_event(evsel)) { 1622 evsel->core.attr.sample_type &= OFFCPU_SAMPLE_TYPES; 1623 attr->inherit = 0; 1624 } 1625 1626 arch__post_evsel_config(evsel, attr); 1627 } 1628 1629 int evsel__set_filter(struct evsel *evsel, const char *filter) 1630 { 1631 char *new_filter = strdup(filter); 1632 1633 if (new_filter != NULL) { 1634 free(evsel->filter); 1635 evsel->filter = new_filter; 1636 return 0; 1637 } 1638 1639 return -1; 1640 } 1641 1642 static int evsel__append_filter(struct evsel *evsel, const char *fmt, const char *filter) 1643 { 1644 char *new_filter; 1645 1646 if (evsel->filter == NULL) 1647 return evsel__set_filter(evsel, filter); 1648 1649 if (asprintf(&new_filter, fmt, evsel->filter, filter) > 0) { 1650 free(evsel->filter); 1651 evsel->filter = new_filter; 1652 return 0; 1653 } 1654 1655 return -1; 1656 } 1657 1658 int evsel__append_tp_filter(struct evsel *evsel, const char *filter) 1659 { 1660 return evsel__append_filter(evsel, "(%s) && (%s)", filter); 1661 } 1662 1663 int evsel__append_addr_filter(struct evsel *evsel, const char *filter) 1664 { 1665 return evsel__append_filter(evsel, "%s,%s", filter); 1666 } 1667 1668 /* Caller has to clear disabled after going through all CPUs. */ 1669 int evsel__enable_cpu(struct evsel *evsel, int cpu_map_idx) 1670 { 1671 return perf_evsel__enable_cpu(&evsel->core, cpu_map_idx); 1672 } 1673 1674 int evsel__enable(struct evsel *evsel) 1675 { 1676 int err = perf_evsel__enable(&evsel->core); 1677 1678 if (!err) 1679 evsel->disabled = false; 1680 return err; 1681 } 1682 1683 /* Caller has to set disabled after going through all CPUs. */ 1684 int evsel__disable_cpu(struct evsel *evsel, int cpu_map_idx) 1685 { 1686 return perf_evsel__disable_cpu(&evsel->core, cpu_map_idx); 1687 } 1688 1689 int evsel__disable(struct evsel *evsel) 1690 { 1691 int err = perf_evsel__disable(&evsel->core); 1692 /* 1693 * We mark it disabled here so that tools that disable a event can 1694 * ignore events after they disable it. I.e. the ring buffer may have 1695 * already a few more events queued up before the kernel got the stop 1696 * request. 1697 */ 1698 if (!err) 1699 evsel->disabled = true; 1700 1701 return err; 1702 } 1703 1704 void free_config_terms(struct list_head *config_terms) 1705 { 1706 struct evsel_config_term *term, *h; 1707 1708 list_for_each_entry_safe(term, h, config_terms, list) { 1709 list_del_init(&term->list); 1710 if (term->free_str) 1711 zfree(&term->val.str); 1712 free(term); 1713 } 1714 } 1715 1716 static void evsel__free_config_terms(struct evsel *evsel) 1717 { 1718 free_config_terms(&evsel->config_terms); 1719 } 1720 1721 static void (*evsel__priv_destructor)(void *priv); 1722 1723 void evsel__set_priv_destructor(void (*destructor)(void *priv)) 1724 { 1725 assert(evsel__priv_destructor == NULL); 1726 1727 evsel__priv_destructor = destructor; 1728 } 1729 1730 void evsel__exit(struct evsel *evsel) 1731 { 1732 assert(list_empty(&evsel->core.node)); 1733 assert(evsel->evlist == NULL); 1734 if (evsel__is_retire_lat(evsel)) 1735 evsel__tpebs_close(evsel); 1736 bpf_counter__destroy(evsel); 1737 perf_bpf_filter__destroy(evsel); 1738 evsel__free_counts(evsel); 1739 perf_evsel__free_fd(&evsel->core); 1740 perf_evsel__free_id(&evsel->core); 1741 evsel__free_config_terms(evsel); 1742 cgroup__put(evsel->cgrp); 1743 perf_evsel__exit(&evsel->core); 1744 zfree(&evsel->group_name); 1745 zfree(&evsel->name); 1746 #ifdef HAVE_LIBTRACEEVENT 1747 zfree(&evsel->tp_sys); 1748 zfree(&evsel->tp_name); 1749 #endif 1750 zfree(&evsel->filter); 1751 zfree(&evsel->group_pmu_name); 1752 zfree(&evsel->unit); 1753 zfree(&evsel->metric_id); 1754 evsel__zero_per_pkg(evsel); 1755 hashmap__free(evsel->per_pkg_mask); 1756 evsel->per_pkg_mask = NULL; 1757 if (evsel__priv_destructor) 1758 evsel__priv_destructor(evsel->priv); 1759 perf_evsel__object.fini(evsel); 1760 if (evsel__tool_event(evsel) == TOOL_PMU__EVENT_SYSTEM_TIME || 1761 evsel__tool_event(evsel) == TOOL_PMU__EVENT_USER_TIME) 1762 xyarray__delete(evsel->start_times); 1763 } 1764 1765 void evsel__delete(struct evsel *evsel) 1766 { 1767 if (!evsel) 1768 return; 1769 1770 evsel__exit(evsel); 1771 free(evsel); 1772 } 1773 1774 void evsel__compute_deltas(struct evsel *evsel, int cpu_map_idx, int thread, 1775 struct perf_counts_values *count) 1776 { 1777 struct perf_counts_values tmp; 1778 1779 if (!evsel->prev_raw_counts) 1780 return; 1781 1782 tmp = *perf_counts(evsel->prev_raw_counts, cpu_map_idx, thread); 1783 *perf_counts(evsel->prev_raw_counts, cpu_map_idx, thread) = *count; 1784 1785 count->val = count->val - tmp.val; 1786 count->ena = count->ena - tmp.ena; 1787 count->run = count->run - tmp.run; 1788 } 1789 1790 static int evsel__read_one(struct evsel *evsel, int cpu_map_idx, int thread) 1791 { 1792 struct perf_counts_values *count = perf_counts(evsel->counts, cpu_map_idx, thread); 1793 1794 return perf_evsel__read(&evsel->core, cpu_map_idx, thread, count); 1795 } 1796 1797 static void evsel__set_count(struct evsel *counter, int cpu_map_idx, int thread, 1798 u64 val, u64 ena, u64 run, u64 lost) 1799 { 1800 struct perf_counts_values *count; 1801 1802 count = perf_counts(counter->counts, cpu_map_idx, thread); 1803 1804 if (evsel__is_retire_lat(counter)) { 1805 evsel__tpebs_read(counter, cpu_map_idx, thread); 1806 perf_counts__set_loaded(counter->counts, cpu_map_idx, thread, true); 1807 return; 1808 } 1809 1810 count->val = val; 1811 count->ena = ena; 1812 count->run = run; 1813 count->lost = lost; 1814 1815 perf_counts__set_loaded(counter->counts, cpu_map_idx, thread, true); 1816 } 1817 1818 static bool evsel__group_has_tpebs(struct evsel *leader) 1819 { 1820 struct evsel *evsel; 1821 1822 for_each_group_evsel(evsel, leader) { 1823 if (evsel__is_retire_lat(evsel)) 1824 return true; 1825 } 1826 return false; 1827 } 1828 1829 static u64 evsel__group_read_nr_members(struct evsel *leader) 1830 { 1831 u64 nr = leader->core.nr_members; 1832 struct evsel *evsel; 1833 1834 for_each_group_evsel(evsel, leader) { 1835 if (evsel__is_retire_lat(evsel)) 1836 nr--; 1837 } 1838 return nr; 1839 } 1840 1841 static u64 evsel__group_read_size(struct evsel *leader) 1842 { 1843 u64 read_format = leader->core.attr.read_format; 1844 int entry = sizeof(u64); /* value */ 1845 int size = 0; 1846 int nr = 1; 1847 1848 if (!evsel__group_has_tpebs(leader)) 1849 return perf_evsel__read_size(&leader->core); 1850 1851 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 1852 size += sizeof(u64); 1853 1854 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 1855 size += sizeof(u64); 1856 1857 if (read_format & PERF_FORMAT_ID) 1858 entry += sizeof(u64); 1859 1860 if (read_format & PERF_FORMAT_LOST) 1861 entry += sizeof(u64); 1862 1863 if (read_format & PERF_FORMAT_GROUP) { 1864 nr = evsel__group_read_nr_members(leader); 1865 size += sizeof(u64); 1866 } 1867 1868 size += entry * nr; 1869 return size; 1870 } 1871 1872 static int evsel__process_group_data(struct evsel *leader, int cpu_map_idx, int thread, u64 *data) 1873 { 1874 u64 read_format = leader->core.attr.read_format; 1875 struct sample_read_value *v; 1876 u64 nr, ena = 0, run = 0, lost = 0; 1877 1878 nr = *data++; 1879 1880 if (nr != evsel__group_read_nr_members(leader)) 1881 return -EINVAL; 1882 1883 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 1884 ena = *data++; 1885 1886 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 1887 run = *data++; 1888 1889 v = (void *)data; 1890 sample_read_group__for_each(v, nr, read_format) { 1891 struct evsel *counter; 1892 1893 counter = evlist__id2evsel(leader->evlist, v->id); 1894 if (!counter) 1895 return -EINVAL; 1896 1897 if (read_format & PERF_FORMAT_LOST) 1898 lost = v->lost; 1899 1900 evsel__set_count(counter, cpu_map_idx, thread, v->value, ena, run, lost); 1901 } 1902 1903 return 0; 1904 } 1905 1906 static int evsel__read_group(struct evsel *leader, int cpu_map_idx, int thread) 1907 { 1908 struct perf_stat_evsel *ps = leader->stats; 1909 u64 read_format = leader->core.attr.read_format; 1910 int size = evsel__group_read_size(leader); 1911 u64 *data = ps->group_data; 1912 1913 if (!(read_format & PERF_FORMAT_ID)) 1914 return -EINVAL; 1915 1916 if (!evsel__is_group_leader(leader)) 1917 return -EINVAL; 1918 1919 if (!data) { 1920 data = zalloc(size); 1921 if (!data) 1922 return -ENOMEM; 1923 1924 ps->group_data = data; 1925 } 1926 1927 if (FD(leader, cpu_map_idx, thread) < 0) 1928 return -EINVAL; 1929 1930 if (readn(FD(leader, cpu_map_idx, thread), data, size) <= 0) 1931 return -errno; 1932 1933 return evsel__process_group_data(leader, cpu_map_idx, thread, data); 1934 } 1935 1936 bool __evsel__match(const struct evsel *evsel, u32 type, u64 config) 1937 { 1938 1939 u32 e_type = evsel->core.attr.type; 1940 u64 e_config = evsel->core.attr.config; 1941 1942 if (e_type == type && e_config == config) 1943 return true; 1944 if (type != PERF_TYPE_HARDWARE && type != PERF_TYPE_HW_CACHE) 1945 return false; 1946 if ((e_type == PERF_TYPE_HARDWARE || e_type == PERF_TYPE_HW_CACHE) && 1947 perf_pmus__supports_extended_type()) 1948 e_config &= PERF_HW_EVENT_MASK; 1949 if (e_type == type && e_config == config) 1950 return true; 1951 if (type == PERF_TYPE_HARDWARE && evsel->pmu && evsel->pmu->is_core && 1952 evsel->alternate_hw_config == config) 1953 return true; 1954 return false; 1955 } 1956 1957 int evsel__read_counter(struct evsel *evsel, int cpu_map_idx, int thread) 1958 { 1959 if (evsel__is_tool(evsel)) 1960 return evsel__tool_pmu_read(evsel, cpu_map_idx, thread); 1961 1962 if (evsel__is_hwmon(evsel)) 1963 return evsel__hwmon_pmu_read(evsel, cpu_map_idx, thread); 1964 1965 if (evsel__is_drm(evsel)) 1966 return evsel__drm_pmu_read(evsel, cpu_map_idx, thread); 1967 1968 if (evsel__is_retire_lat(evsel)) 1969 return evsel__tpebs_read(evsel, cpu_map_idx, thread); 1970 1971 if (evsel->core.attr.read_format & PERF_FORMAT_GROUP) 1972 return evsel__read_group(evsel, cpu_map_idx, thread); 1973 1974 return evsel__read_one(evsel, cpu_map_idx, thread); 1975 } 1976 1977 int __evsel__read_on_cpu(struct evsel *evsel, int cpu_map_idx, int thread, bool scale) 1978 { 1979 struct perf_counts_values count; 1980 size_t nv = scale ? 3 : 1; 1981 1982 if (FD(evsel, cpu_map_idx, thread) < 0) 1983 return -EINVAL; 1984 1985 if (evsel->counts == NULL && evsel__alloc_counts(evsel) < 0) 1986 return -ENOMEM; 1987 1988 if (readn(FD(evsel, cpu_map_idx, thread), &count, nv * sizeof(u64)) <= 0) 1989 return -errno; 1990 1991 evsel__compute_deltas(evsel, cpu_map_idx, thread, &count); 1992 perf_counts_values__scale(&count, scale, NULL); 1993 *perf_counts(evsel->counts, cpu_map_idx, thread) = count; 1994 return 0; 1995 } 1996 1997 static int evsel__match_other_cpu(struct evsel *evsel, struct evsel *other, 1998 int cpu_map_idx) 1999 { 2000 struct perf_cpu cpu; 2001 2002 cpu = perf_cpu_map__cpu(evsel->core.cpus, cpu_map_idx); 2003 return perf_cpu_map__idx(other->core.cpus, cpu); 2004 } 2005 2006 static int evsel__hybrid_group_cpu_map_idx(struct evsel *evsel, int cpu_map_idx) 2007 { 2008 struct evsel *leader = evsel__leader(evsel); 2009 2010 if ((evsel__is_hybrid(evsel) && !evsel__is_hybrid(leader)) || 2011 (!evsel__is_hybrid(evsel) && evsel__is_hybrid(leader))) { 2012 return evsel__match_other_cpu(evsel, leader, cpu_map_idx); 2013 } 2014 2015 return cpu_map_idx; 2016 } 2017 2018 static int get_group_fd(struct evsel *evsel, int cpu_map_idx, int thread) 2019 { 2020 struct evsel *leader = evsel__leader(evsel); 2021 int fd; 2022 2023 if (!evsel->supported || evsel__is_group_leader(evsel)) 2024 return -1; 2025 2026 /* 2027 * Leader must be already processed/open, 2028 * if not it's a bug. 2029 */ 2030 BUG_ON(!leader->core.fd); 2031 2032 cpu_map_idx = evsel__hybrid_group_cpu_map_idx(evsel, cpu_map_idx); 2033 if (cpu_map_idx == -1) 2034 return -1; 2035 2036 fd = FD(leader, cpu_map_idx, thread); 2037 BUG_ON(fd == -1 && leader->supported); 2038 2039 /* 2040 * When the leader has been skipped, return -2 to distinguish from no 2041 * group leader case. 2042 */ 2043 return fd == -1 ? -2 : fd; 2044 } 2045 2046 static void evsel__remove_fd(struct evsel *pos, int nr_cpus, int nr_threads, int thread_idx) 2047 { 2048 for (int cpu = 0; cpu < nr_cpus; cpu++) 2049 for (int thread = thread_idx; thread < nr_threads - 1; thread++) 2050 FD(pos, cpu, thread) = FD(pos, cpu, thread + 1); 2051 } 2052 2053 static int update_fds(struct evsel *evsel, 2054 int nr_cpus, int cpu_map_idx, 2055 int nr_threads, int thread_idx) 2056 { 2057 struct evsel *pos; 2058 2059 if (cpu_map_idx >= nr_cpus || thread_idx >= nr_threads) 2060 return -EINVAL; 2061 2062 evlist__for_each_entry(evsel->evlist, pos) { 2063 nr_cpus = pos != evsel ? nr_cpus : cpu_map_idx; 2064 2065 evsel__remove_fd(pos, nr_cpus, nr_threads, thread_idx); 2066 2067 /* 2068 * Since fds for next evsel has not been created, 2069 * there is no need to iterate whole event list. 2070 */ 2071 if (pos == evsel) 2072 break; 2073 } 2074 return 0; 2075 } 2076 2077 static bool evsel__ignore_missing_thread(struct evsel *evsel, 2078 int nr_cpus, int cpu_map_idx, 2079 struct perf_thread_map *threads, 2080 int thread, int err) 2081 { 2082 pid_t ignore_pid = perf_thread_map__pid(threads, thread); 2083 2084 if (!evsel->ignore_missing_thread) 2085 return false; 2086 2087 /* The system wide setup does not work with threads. */ 2088 if (evsel->core.system_wide) 2089 return false; 2090 2091 /* The -ESRCH is perf event syscall errno for pid's not found. */ 2092 if (err != -ESRCH) 2093 return false; 2094 2095 /* If there's only one thread, let it fail. */ 2096 if (threads->nr == 1) 2097 return false; 2098 2099 /* 2100 * We should remove fd for missing_thread first 2101 * because thread_map__remove() will decrease threads->nr. 2102 */ 2103 if (update_fds(evsel, nr_cpus, cpu_map_idx, threads->nr, thread)) 2104 return false; 2105 2106 if (thread_map__remove(threads, thread)) 2107 return false; 2108 2109 pr_warning("WARNING: Ignored open failure for pid %d\n", 2110 ignore_pid); 2111 return true; 2112 } 2113 2114 static int __open_attr__fprintf(FILE *fp, const char *name, const char *val, 2115 void *priv __maybe_unused) 2116 { 2117 return fprintf(fp, " %-32s %s\n", name, val); 2118 } 2119 2120 static void display_attr(struct perf_event_attr *attr) 2121 { 2122 if (verbose >= 2 || debug_peo_args) { 2123 fprintf(stderr, "%.60s\n", graph_dotted_line); 2124 fprintf(stderr, "perf_event_attr:\n"); 2125 perf_event_attr__fprintf(stderr, attr, __open_attr__fprintf, NULL); 2126 fprintf(stderr, "%.60s\n", graph_dotted_line); 2127 } 2128 } 2129 2130 bool evsel__precise_ip_fallback(struct evsel *evsel) 2131 { 2132 /* Do not try less precise if not requested. */ 2133 if (!evsel->precise_max) 2134 return false; 2135 2136 /* 2137 * We tried all the precise_ip values, and it's 2138 * still failing, so leave it to standard fallback. 2139 */ 2140 if (!evsel->core.attr.precise_ip) { 2141 evsel->core.attr.precise_ip = evsel->precise_ip_original; 2142 return false; 2143 } 2144 2145 if (!evsel->precise_ip_original) 2146 evsel->precise_ip_original = evsel->core.attr.precise_ip; 2147 2148 evsel->core.attr.precise_ip--; 2149 pr_debug2_peo("decreasing precise_ip by one (%d)\n", evsel->core.attr.precise_ip); 2150 display_attr(&evsel->core.attr); 2151 return true; 2152 } 2153 2154 static struct perf_cpu_map *empty_cpu_map; 2155 static struct perf_thread_map *empty_thread_map; 2156 2157 static int __evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus, 2158 struct perf_thread_map *threads) 2159 { 2160 int ret = 0; 2161 int nthreads = perf_thread_map__nr(threads); 2162 2163 if ((perf_missing_features.write_backward && evsel->core.attr.write_backward) || 2164 (perf_missing_features.aux_output && evsel->core.attr.aux_output)) 2165 return -EINVAL; 2166 2167 if (cpus == NULL) { 2168 if (empty_cpu_map == NULL) { 2169 empty_cpu_map = perf_cpu_map__new_any_cpu(); 2170 if (empty_cpu_map == NULL) 2171 return -ENOMEM; 2172 } 2173 2174 cpus = empty_cpu_map; 2175 } 2176 2177 if (threads == NULL) { 2178 if (empty_thread_map == NULL) { 2179 empty_thread_map = thread_map__new_by_tid(-1); 2180 if (empty_thread_map == NULL) 2181 return -ENOMEM; 2182 } 2183 2184 threads = empty_thread_map; 2185 } 2186 2187 if (evsel->core.fd == NULL && 2188 perf_evsel__alloc_fd(&evsel->core, perf_cpu_map__nr(cpus), nthreads) < 0) 2189 return -ENOMEM; 2190 2191 if (evsel__is_tool(evsel)) 2192 ret = evsel__tool_pmu_prepare_open(evsel, cpus, nthreads); 2193 2194 evsel->open_flags = PERF_FLAG_FD_CLOEXEC; 2195 if (evsel->cgrp) 2196 evsel->open_flags |= PERF_FLAG_PID_CGROUP; 2197 2198 return ret; 2199 } 2200 2201 static void evsel__disable_missing_features(struct evsel *evsel) 2202 { 2203 if (perf_missing_features.inherit_sample_read && evsel->core.attr.inherit && 2204 (evsel->core.attr.sample_type & PERF_SAMPLE_READ)) 2205 evsel->core.attr.inherit = 0; 2206 if (perf_missing_features.branch_counters) 2207 evsel->core.attr.branch_sample_type &= ~PERF_SAMPLE_BRANCH_COUNTERS; 2208 if (perf_missing_features.read_lost) 2209 evsel->core.attr.read_format &= ~PERF_FORMAT_LOST; 2210 if (perf_missing_features.weight_struct) { 2211 evsel__set_sample_bit(evsel, WEIGHT); 2212 evsel__reset_sample_bit(evsel, WEIGHT_STRUCT); 2213 } 2214 if (perf_missing_features.clockid_wrong) 2215 evsel->core.attr.clockid = CLOCK_MONOTONIC; /* should always work */ 2216 if (perf_missing_features.clockid) { 2217 evsel->core.attr.use_clockid = 0; 2218 evsel->core.attr.clockid = 0; 2219 } 2220 if (perf_missing_features.cloexec) 2221 evsel->open_flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC; 2222 if (perf_missing_features.mmap2) 2223 evsel->core.attr.mmap2 = 0; 2224 if (evsel->pmu && evsel->pmu->missing_features.exclude_guest) 2225 evsel->core.attr.exclude_guest = evsel->core.attr.exclude_host = 0; 2226 if (perf_missing_features.lbr_flags) 2227 evsel->core.attr.branch_sample_type &= ~(PERF_SAMPLE_BRANCH_NO_FLAGS | 2228 PERF_SAMPLE_BRANCH_NO_CYCLES); 2229 if (perf_missing_features.group_read && evsel->core.attr.inherit) 2230 evsel->core.attr.read_format &= ~(PERF_FORMAT_GROUP|PERF_FORMAT_ID); 2231 if (perf_missing_features.ksymbol) 2232 evsel->core.attr.ksymbol = 0; 2233 if (perf_missing_features.bpf) 2234 evsel->core.attr.bpf_event = 0; 2235 if (perf_missing_features.branch_hw_idx) 2236 evsel->core.attr.branch_sample_type &= ~PERF_SAMPLE_BRANCH_HW_INDEX; 2237 if (perf_missing_features.sample_id_all) 2238 evsel->core.attr.sample_id_all = 0; 2239 } 2240 2241 int evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus, 2242 struct perf_thread_map *threads) 2243 { 2244 int err; 2245 2246 err = __evsel__prepare_open(evsel, cpus, threads); 2247 if (err) 2248 return err; 2249 2250 evsel__disable_missing_features(evsel); 2251 2252 return err; 2253 } 2254 2255 static bool __has_attr_feature(struct perf_event_attr *attr, 2256 struct perf_cpu cpu, unsigned long flags) 2257 { 2258 int fd = syscall(SYS_perf_event_open, attr, /*pid=*/0, cpu.cpu, 2259 /*group_fd=*/-1, flags); 2260 close(fd); 2261 2262 if (fd < 0) { 2263 attr->exclude_kernel = 1; 2264 2265 fd = syscall(SYS_perf_event_open, attr, /*pid=*/0, cpu.cpu, 2266 /*group_fd=*/-1, flags); 2267 close(fd); 2268 } 2269 2270 if (fd < 0) { 2271 attr->exclude_hv = 1; 2272 2273 fd = syscall(SYS_perf_event_open, attr, /*pid=*/0, cpu.cpu, 2274 /*group_fd=*/-1, flags); 2275 close(fd); 2276 } 2277 2278 if (fd < 0) { 2279 attr->exclude_guest = 1; 2280 2281 fd = syscall(SYS_perf_event_open, attr, /*pid=*/0, cpu.cpu, 2282 /*group_fd=*/-1, flags); 2283 close(fd); 2284 } 2285 2286 attr->exclude_kernel = 0; 2287 attr->exclude_guest = 0; 2288 attr->exclude_hv = 0; 2289 2290 return fd >= 0; 2291 } 2292 2293 static bool has_attr_feature(struct perf_event_attr *attr, unsigned long flags) 2294 { 2295 struct perf_cpu cpu = {.cpu = -1}; 2296 2297 return __has_attr_feature(attr, cpu, flags); 2298 } 2299 2300 static void evsel__detect_missing_pmu_features(struct evsel *evsel) 2301 { 2302 struct perf_event_attr attr = { 2303 .type = evsel->core.attr.type, 2304 .config = evsel->core.attr.config, 2305 .disabled = 1, 2306 }; 2307 struct perf_pmu *pmu = evsel->pmu; 2308 int old_errno; 2309 2310 old_errno = errno; 2311 2312 if (pmu == NULL) 2313 pmu = evsel->pmu = evsel__find_pmu(evsel); 2314 2315 if (pmu == NULL || pmu->missing_features.checked) 2316 goto out; 2317 2318 /* 2319 * Must probe features in the order they were added to the 2320 * perf_event_attr interface. These are kernel core limitation but 2321 * specific to PMUs with branch stack. So we can detect with the given 2322 * hardware event and stop on the first one succeeded. 2323 */ 2324 2325 /* Please add new feature detection here. */ 2326 2327 attr.exclude_guest = 1; 2328 if (has_attr_feature(&attr, /*flags=*/0)) 2329 goto found; 2330 pmu->missing_features.exclude_guest = true; 2331 pr_debug2("switching off exclude_guest for PMU %s\n", pmu->name); 2332 2333 found: 2334 pmu->missing_features.checked = true; 2335 out: 2336 errno = old_errno; 2337 } 2338 2339 static void evsel__detect_missing_brstack_features(struct evsel *evsel) 2340 { 2341 static bool detection_done = false; 2342 struct perf_event_attr attr = { 2343 .type = evsel->core.attr.type, 2344 .config = evsel->core.attr.config, 2345 .disabled = 1, 2346 .sample_type = PERF_SAMPLE_BRANCH_STACK, 2347 .sample_period = 1000, 2348 }; 2349 int old_errno; 2350 2351 if (detection_done) 2352 return; 2353 2354 old_errno = errno; 2355 2356 /* 2357 * Must probe features in the order they were added to the 2358 * perf_event_attr interface. These are PMU specific limitation 2359 * so we can detect with the given hardware event and stop on the 2360 * first one succeeded. 2361 */ 2362 2363 /* Please add new feature detection here. */ 2364 2365 attr.branch_sample_type = PERF_SAMPLE_BRANCH_COUNTERS; 2366 if (has_attr_feature(&attr, /*flags=*/0)) 2367 goto found; 2368 perf_missing_features.branch_counters = true; 2369 pr_debug2("switching off branch counters support\n"); 2370 2371 attr.branch_sample_type = PERF_SAMPLE_BRANCH_HW_INDEX; 2372 if (has_attr_feature(&attr, /*flags=*/0)) 2373 goto found; 2374 perf_missing_features.branch_hw_idx = true; 2375 pr_debug2("switching off branch HW index support\n"); 2376 2377 attr.branch_sample_type = PERF_SAMPLE_BRANCH_NO_CYCLES | PERF_SAMPLE_BRANCH_NO_FLAGS; 2378 if (has_attr_feature(&attr, /*flags=*/0)) 2379 goto found; 2380 perf_missing_features.lbr_flags = true; 2381 pr_debug2_peo("switching off branch sample type no (cycles/flags)\n"); 2382 2383 found: 2384 detection_done = true; 2385 errno = old_errno; 2386 } 2387 2388 static bool evsel__probe_aux_action(struct evsel *evsel, struct perf_cpu cpu) 2389 { 2390 struct perf_event_attr attr = evsel->core.attr; 2391 int old_errno = errno; 2392 2393 attr.disabled = 1; 2394 attr.aux_start_paused = 1; 2395 2396 if (__has_attr_feature(&attr, cpu, /*flags=*/0)) { 2397 errno = old_errno; 2398 return true; 2399 } 2400 2401 /* 2402 * EOPNOTSUPP means the kernel supports the feature but the PMU does 2403 * not, so keep that distinction if possible. 2404 */ 2405 if (errno != EOPNOTSUPP) 2406 errno = old_errno; 2407 2408 return false; 2409 } 2410 2411 static void evsel__detect_missing_aux_action_feature(struct evsel *evsel, struct perf_cpu cpu) 2412 { 2413 static bool detection_done; 2414 struct evsel *leader; 2415 2416 /* 2417 * Don't bother probing aux_action if it is not being used or has been 2418 * probed before. 2419 */ 2420 if (!evsel->core.attr.aux_action || detection_done) 2421 return; 2422 2423 detection_done = true; 2424 2425 /* 2426 * The leader is an AUX area event. If it has failed, assume the feature 2427 * is not supported. 2428 */ 2429 leader = evsel__leader(evsel); 2430 if (evsel == leader) { 2431 perf_missing_features.aux_action = true; 2432 return; 2433 } 2434 2435 /* 2436 * AUX area event with aux_action must have been opened successfully 2437 * already, so feature is supported. 2438 */ 2439 if (leader->core.attr.aux_action) 2440 return; 2441 2442 if (!evsel__probe_aux_action(leader, cpu)) 2443 perf_missing_features.aux_action = true; 2444 } 2445 2446 static bool evsel__detect_missing_features(struct evsel *evsel, struct perf_cpu cpu) 2447 { 2448 static bool detection_done = false; 2449 struct perf_event_attr attr = { 2450 .type = PERF_TYPE_SOFTWARE, 2451 .config = PERF_COUNT_SW_TASK_CLOCK, 2452 .disabled = 1, 2453 }; 2454 int old_errno; 2455 2456 evsel__detect_missing_aux_action_feature(evsel, cpu); 2457 2458 evsel__detect_missing_pmu_features(evsel); 2459 2460 if (evsel__has_br_stack(evsel)) 2461 evsel__detect_missing_brstack_features(evsel); 2462 2463 if (detection_done) 2464 goto check; 2465 2466 old_errno = errno; 2467 2468 /* 2469 * Must probe features in the order they were added to the 2470 * perf_event_attr interface. These are kernel core limitation 2471 * not PMU-specific so we can detect with a software event and 2472 * stop on the first one succeeded. 2473 */ 2474 2475 /* Please add new feature detection here. */ 2476 2477 attr.inherit = true; 2478 attr.sample_type = PERF_SAMPLE_READ | PERF_SAMPLE_TID; 2479 if (has_attr_feature(&attr, /*flags=*/0)) 2480 goto found; 2481 perf_missing_features.inherit_sample_read = true; 2482 pr_debug2("Using PERF_SAMPLE_READ / :S modifier is not compatible with inherit, falling back to no-inherit.\n"); 2483 attr.inherit = false; 2484 attr.sample_type = 0; 2485 2486 attr.read_format = PERF_FORMAT_LOST; 2487 if (has_attr_feature(&attr, /*flags=*/0)) 2488 goto found; 2489 perf_missing_features.read_lost = true; 2490 pr_debug2("switching off PERF_FORMAT_LOST support\n"); 2491 attr.read_format = 0; 2492 2493 attr.sample_type = PERF_SAMPLE_WEIGHT_STRUCT; 2494 if (has_attr_feature(&attr, /*flags=*/0)) 2495 goto found; 2496 perf_missing_features.weight_struct = true; 2497 pr_debug2("switching off weight struct support\n"); 2498 attr.sample_type = 0; 2499 2500 attr.sample_type = PERF_SAMPLE_CODE_PAGE_SIZE; 2501 if (has_attr_feature(&attr, /*flags=*/0)) 2502 goto found; 2503 perf_missing_features.code_page_size = true; 2504 pr_debug2_peo("Kernel has no PERF_SAMPLE_CODE_PAGE_SIZE support\n"); 2505 attr.sample_type = 0; 2506 2507 attr.sample_type = PERF_SAMPLE_DATA_PAGE_SIZE; 2508 if (has_attr_feature(&attr, /*flags=*/0)) 2509 goto found; 2510 perf_missing_features.data_page_size = true; 2511 pr_debug2_peo("Kernel has no PERF_SAMPLE_DATA_PAGE_SIZE support\n"); 2512 attr.sample_type = 0; 2513 2514 attr.cgroup = 1; 2515 if (has_attr_feature(&attr, /*flags=*/0)) 2516 goto found; 2517 perf_missing_features.cgroup = true; 2518 pr_debug2_peo("Kernel has no cgroup sampling support\n"); 2519 attr.cgroup = 0; 2520 2521 attr.aux_output = 1; 2522 if (has_attr_feature(&attr, /*flags=*/0)) 2523 goto found; 2524 perf_missing_features.aux_output = true; 2525 pr_debug2_peo("Kernel has no attr.aux_output support\n"); 2526 attr.aux_output = 0; 2527 2528 attr.bpf_event = 1; 2529 if (has_attr_feature(&attr, /*flags=*/0)) 2530 goto found; 2531 perf_missing_features.bpf = true; 2532 pr_debug2_peo("switching off bpf_event\n"); 2533 attr.bpf_event = 0; 2534 2535 attr.ksymbol = 1; 2536 if (has_attr_feature(&attr, /*flags=*/0)) 2537 goto found; 2538 perf_missing_features.ksymbol = true; 2539 pr_debug2_peo("switching off ksymbol\n"); 2540 attr.ksymbol = 0; 2541 2542 attr.write_backward = 1; 2543 if (has_attr_feature(&attr, /*flags=*/0)) 2544 goto found; 2545 perf_missing_features.write_backward = true; 2546 pr_debug2_peo("switching off write_backward\n"); 2547 attr.write_backward = 0; 2548 2549 attr.use_clockid = 1; 2550 attr.clockid = CLOCK_MONOTONIC; 2551 if (has_attr_feature(&attr, /*flags=*/0)) 2552 goto found; 2553 perf_missing_features.clockid = true; 2554 pr_debug2_peo("switching off clockid\n"); 2555 attr.use_clockid = 0; 2556 attr.clockid = 0; 2557 2558 if (has_attr_feature(&attr, /*flags=*/PERF_FLAG_FD_CLOEXEC)) 2559 goto found; 2560 perf_missing_features.cloexec = true; 2561 pr_debug2_peo("switching off cloexec flag\n"); 2562 2563 attr.mmap2 = 1; 2564 if (has_attr_feature(&attr, /*flags=*/0)) 2565 goto found; 2566 perf_missing_features.mmap2 = true; 2567 pr_debug2_peo("switching off mmap2\n"); 2568 attr.mmap2 = 0; 2569 2570 /* set this unconditionally? */ 2571 perf_missing_features.sample_id_all = true; 2572 pr_debug2_peo("switching off sample_id_all\n"); 2573 2574 attr.inherit = 1; 2575 attr.read_format = PERF_FORMAT_GROUP; 2576 if (has_attr_feature(&attr, /*flags=*/0)) 2577 goto found; 2578 perf_missing_features.group_read = true; 2579 pr_debug2_peo("switching off group read\n"); 2580 attr.inherit = 0; 2581 attr.read_format = 0; 2582 2583 found: 2584 detection_done = true; 2585 errno = old_errno; 2586 2587 check: 2588 if (evsel->core.attr.inherit && 2589 (evsel->core.attr.sample_type & PERF_SAMPLE_READ) && 2590 perf_missing_features.inherit_sample_read) 2591 return true; 2592 2593 if ((evsel->core.attr.branch_sample_type & PERF_SAMPLE_BRANCH_COUNTERS) && 2594 perf_missing_features.branch_counters) 2595 return true; 2596 2597 if ((evsel->core.attr.read_format & PERF_FORMAT_LOST) && 2598 perf_missing_features.read_lost) 2599 return true; 2600 2601 if ((evsel->core.attr.sample_type & PERF_SAMPLE_WEIGHT_STRUCT) && 2602 perf_missing_features.weight_struct) 2603 return true; 2604 2605 if (evsel->core.attr.use_clockid && evsel->core.attr.clockid != CLOCK_MONOTONIC && 2606 !perf_missing_features.clockid) { 2607 perf_missing_features.clockid_wrong = true; 2608 return true; 2609 } 2610 2611 if (evsel->core.attr.use_clockid && perf_missing_features.clockid) 2612 return true; 2613 2614 if ((evsel->open_flags & PERF_FLAG_FD_CLOEXEC) && 2615 perf_missing_features.cloexec) 2616 return true; 2617 2618 if (evsel->core.attr.mmap2 && perf_missing_features.mmap2) 2619 return true; 2620 2621 if ((evsel->core.attr.branch_sample_type & (PERF_SAMPLE_BRANCH_NO_FLAGS | 2622 PERF_SAMPLE_BRANCH_NO_CYCLES)) && 2623 perf_missing_features.lbr_flags) 2624 return true; 2625 2626 if (evsel->core.attr.inherit && (evsel->core.attr.read_format & PERF_FORMAT_GROUP) && 2627 perf_missing_features.group_read) 2628 return true; 2629 2630 if (evsel->core.attr.ksymbol && perf_missing_features.ksymbol) 2631 return true; 2632 2633 if (evsel->core.attr.bpf_event && perf_missing_features.bpf) 2634 return true; 2635 2636 if ((evsel->core.attr.branch_sample_type & PERF_SAMPLE_BRANCH_HW_INDEX) && 2637 perf_missing_features.branch_hw_idx) 2638 return true; 2639 2640 if (evsel->core.attr.sample_id_all && perf_missing_features.sample_id_all) 2641 return true; 2642 2643 return false; 2644 } 2645 2646 static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus, 2647 struct perf_thread_map *threads, 2648 int start_cpu_map_idx, int end_cpu_map_idx) 2649 { 2650 int idx, thread, nthreads; 2651 int pid = -1, err, old_errno; 2652 enum rlimit_action set_rlimit = NO_CHANGE; 2653 struct perf_cpu cpu; 2654 2655 if (evsel__is_retire_lat(evsel)) { 2656 err = evsel__tpebs_open(evsel); 2657 goto out; 2658 } 2659 2660 err = __evsel__prepare_open(evsel, cpus, threads); 2661 if (err) 2662 goto out; 2663 2664 if (cpus == NULL) 2665 cpus = empty_cpu_map; 2666 2667 if (threads == NULL) 2668 threads = empty_thread_map; 2669 2670 nthreads = perf_thread_map__nr(threads); 2671 2672 if (evsel->cgrp) 2673 pid = evsel->cgrp->fd; 2674 2675 fallback_missing_features: 2676 evsel__disable_missing_features(evsel); 2677 2678 pr_debug3("Opening: %s\n", evsel__name(evsel)); 2679 display_attr(&evsel->core.attr); 2680 2681 if (evsel__is_tool(evsel)) { 2682 err = evsel__tool_pmu_open(evsel, threads, 2683 start_cpu_map_idx, 2684 end_cpu_map_idx); 2685 goto out; 2686 } 2687 if (evsel__is_hwmon(evsel)) { 2688 err = evsel__hwmon_pmu_open(evsel, threads, 2689 start_cpu_map_idx, 2690 end_cpu_map_idx); 2691 goto out; 2692 } 2693 if (evsel__is_drm(evsel)) { 2694 err = evsel__drm_pmu_open(evsel, threads, 2695 start_cpu_map_idx, 2696 end_cpu_map_idx); 2697 goto out; 2698 } 2699 2700 for (idx = start_cpu_map_idx; idx < end_cpu_map_idx; idx++) { 2701 cpu = perf_cpu_map__cpu(cpus, idx); 2702 2703 for (thread = 0; thread < nthreads; thread++) { 2704 int fd, group_fd; 2705 retry_open: 2706 if (thread >= nthreads) 2707 break; 2708 2709 if (!evsel->cgrp && !evsel->core.system_wide) 2710 pid = perf_thread_map__pid(threads, thread); 2711 2712 group_fd = get_group_fd(evsel, idx, thread); 2713 2714 if (group_fd == -2) { 2715 pr_debug("broken group leader for %s\n", evsel->name); 2716 err = -EINVAL; 2717 goto out_close; 2718 } 2719 2720 /* Debug message used by test scripts */ 2721 pr_debug2_peo("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx", 2722 pid, cpu.cpu, group_fd, evsel->open_flags); 2723 2724 fd = sys_perf_event_open(&evsel->core.attr, pid, cpu.cpu, 2725 group_fd, evsel->open_flags); 2726 2727 FD(evsel, idx, thread) = fd; 2728 2729 if (fd < 0) { 2730 err = -errno; 2731 2732 pr_debug2_peo("\nsys_perf_event_open failed, error %d\n", 2733 err); 2734 goto try_fallback; 2735 } 2736 2737 bpf_counter__install_pe(evsel, idx, fd); 2738 2739 if (unlikely(test_attr__enabled())) { 2740 test_attr__open(&evsel->core.attr, pid, cpu, 2741 fd, group_fd, evsel->open_flags); 2742 } 2743 2744 /* Debug message used by test scripts */ 2745 pr_debug2_peo(" = %d\n", fd); 2746 2747 if (evsel->bpf_fd >= 0) { 2748 int evt_fd = fd; 2749 int bpf_fd = evsel->bpf_fd; 2750 2751 err = ioctl(evt_fd, 2752 PERF_EVENT_IOC_SET_BPF, 2753 bpf_fd); 2754 if (err && errno != EEXIST) { 2755 pr_err("failed to attach bpf fd %d: %s\n", 2756 bpf_fd, strerror(errno)); 2757 err = -EINVAL; 2758 goto out_close; 2759 } 2760 } 2761 2762 set_rlimit = NO_CHANGE; 2763 2764 /* 2765 * If we succeeded but had to kill clockid, fail and 2766 * have evsel__open_strerror() print us a nice error. 2767 */ 2768 if (perf_missing_features.clockid || 2769 perf_missing_features.clockid_wrong) { 2770 err = -EINVAL; 2771 goto out_close; 2772 } 2773 } 2774 } 2775 2776 err = 0; 2777 goto out; 2778 2779 try_fallback: 2780 if (evsel__ignore_missing_thread(evsel, perf_cpu_map__nr(cpus), 2781 idx, threads, thread, err)) { 2782 /* We just removed 1 thread, so lower the upper nthreads limit. */ 2783 nthreads--; 2784 2785 /* ... and pretend like nothing have happened. */ 2786 err = 0; 2787 goto retry_open; 2788 } 2789 /* 2790 * perf stat needs between 5 and 22 fds per CPU. When we run out 2791 * of them try to increase the limits. 2792 */ 2793 if (err == -EMFILE && rlimit__increase_nofile(&set_rlimit)) 2794 goto retry_open; 2795 2796 if (err == -EINVAL && evsel__detect_missing_features(evsel, cpu)) 2797 goto fallback_missing_features; 2798 2799 if (evsel__precise_ip_fallback(evsel)) 2800 goto retry_open; 2801 2802 out_close: 2803 if (err) 2804 threads->err_thread = thread; 2805 2806 old_errno = errno; 2807 do { 2808 while (--thread >= 0) { 2809 if (FD(evsel, idx, thread) >= 0) 2810 close(FD(evsel, idx, thread)); 2811 FD(evsel, idx, thread) = -1; 2812 } 2813 thread = nthreads; 2814 } while (--idx >= 0); 2815 errno = old_errno; 2816 out: 2817 if (err) 2818 evsel->supported = false; 2819 return err; 2820 } 2821 2822 int evsel__open(struct evsel *evsel, struct perf_cpu_map *cpus, 2823 struct perf_thread_map *threads) 2824 { 2825 return evsel__open_cpu(evsel, cpus, threads, 0, perf_cpu_map__nr(cpus)); 2826 } 2827 2828 void evsel__close(struct evsel *evsel) 2829 { 2830 if (evsel__is_retire_lat(evsel)) 2831 evsel__tpebs_close(evsel); 2832 perf_evsel__close(&evsel->core); 2833 perf_evsel__free_id(&evsel->core); 2834 } 2835 2836 int evsel__open_per_cpu_and_thread(struct evsel *evsel, 2837 struct perf_cpu_map *cpus, int cpu_map_idx, 2838 struct perf_thread_map *threads) 2839 { 2840 if (cpu_map_idx == -1) 2841 return evsel__open_cpu(evsel, cpus, threads, 0, perf_cpu_map__nr(cpus)); 2842 2843 return evsel__open_cpu(evsel, cpus, threads, cpu_map_idx, cpu_map_idx + 1); 2844 } 2845 2846 int evsel__open_per_cpu(struct evsel *evsel, struct perf_cpu_map *cpus, int cpu_map_idx) 2847 { 2848 struct perf_thread_map *threads = thread_map__new_by_tid(-1); 2849 int ret = evsel__open_per_cpu_and_thread(evsel, cpus, cpu_map_idx, threads); 2850 2851 perf_thread_map__put(threads); 2852 return ret; 2853 } 2854 2855 int evsel__open_per_thread(struct evsel *evsel, struct perf_thread_map *threads) 2856 { 2857 struct perf_cpu_map *cpus = perf_cpu_map__new_any_cpu(); 2858 int ret = evsel__open_per_cpu_and_thread(evsel, cpus, -1, threads); 2859 2860 perf_cpu_map__put(cpus); 2861 return ret; 2862 } 2863 2864 static int perf_evsel__parse_id_sample(const struct evsel *evsel, 2865 const union perf_event *event, 2866 struct perf_sample *sample) 2867 { 2868 u64 type = evsel->core.attr.sample_type; 2869 const __u64 *array = event->sample.array; 2870 bool swapped = evsel->needs_swap; 2871 union u64_swap u; 2872 2873 array += ((event->header.size - 2874 sizeof(event->header)) / sizeof(u64)) - 1; 2875 2876 if (type & PERF_SAMPLE_IDENTIFIER) { 2877 sample->id = *array; 2878 array--; 2879 } 2880 2881 if (type & PERF_SAMPLE_CPU) { 2882 u.val64 = *array; 2883 if (swapped) { 2884 /* undo swap of u64, then swap on individual u32s */ 2885 u.val64 = bswap_64(u.val64); 2886 u.val32[0] = bswap_32(u.val32[0]); 2887 } 2888 2889 sample->cpu = u.val32[0]; 2890 array--; 2891 } 2892 2893 if (type & PERF_SAMPLE_STREAM_ID) { 2894 sample->stream_id = *array; 2895 array--; 2896 } 2897 2898 if (type & PERF_SAMPLE_ID) { 2899 sample->id = *array; 2900 array--; 2901 } 2902 2903 if (type & PERF_SAMPLE_TIME) { 2904 sample->time = *array; 2905 array--; 2906 } 2907 2908 if (type & PERF_SAMPLE_TID) { 2909 u.val64 = *array; 2910 if (swapped) { 2911 /* undo swap of u64, then swap on individual u32s */ 2912 u.val64 = bswap_64(u.val64); 2913 u.val32[0] = bswap_32(u.val32[0]); 2914 u.val32[1] = bswap_32(u.val32[1]); 2915 } 2916 2917 sample->pid = u.val32[0]; 2918 sample->tid = u.val32[1]; 2919 array--; 2920 } 2921 2922 return 0; 2923 } 2924 2925 static inline bool overflow(const void *endp, u16 max_size, const void *offset, 2926 u64 size) 2927 { 2928 return size > max_size || offset + size > endp; 2929 } 2930 2931 #define OVERFLOW_CHECK(offset, size, max_size) \ 2932 do { \ 2933 if (overflow(endp, (max_size), (offset), (size))) \ 2934 return -EFAULT; \ 2935 } while (0) 2936 2937 #define OVERFLOW_CHECK_u64(offset) \ 2938 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64)) 2939 2940 static int 2941 perf_event__check_size(union perf_event *event, unsigned int sample_size) 2942 { 2943 /* 2944 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes 2945 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to 2946 * check the format does not go past the end of the event. 2947 */ 2948 if (sample_size + sizeof(event->header) > event->header.size) 2949 return -EFAULT; 2950 2951 return 0; 2952 } 2953 2954 static void perf_parse_sample_weight(struct perf_sample *data, const __u64 *array, u64 type) 2955 { 2956 union perf_sample_weight weight; 2957 2958 weight.full = *array; 2959 if (type & PERF_SAMPLE_WEIGHT_STRUCT) { 2960 data->weight = weight.var1_dw; 2961 data->ins_lat = weight.var2_w; 2962 data->weight3 = weight.var3_w; 2963 } else { 2964 data->weight = weight.full; 2965 } 2966 } 2967 2968 u64 evsel__bitfield_swap_branch_flags(u64 value) 2969 { 2970 u64 new_val = 0; 2971 2972 /* 2973 * branch_flags 2974 * union { 2975 * u64 values; 2976 * struct { 2977 * mispred:1 //target mispredicted 2978 * predicted:1 //target predicted 2979 * in_tx:1 //in transaction 2980 * abort:1 //transaction abort 2981 * cycles:16 //cycle count to last branch 2982 * type:4 //branch type 2983 * spec:2 //branch speculation info 2984 * new_type:4 //additional branch type 2985 * priv:3 //privilege level 2986 * reserved:31 2987 * } 2988 * } 2989 * 2990 * Avoid bswap64() the entire branch_flag.value, 2991 * as it has variable bit-field sizes. Instead the 2992 * macro takes the bit-field position/size, 2993 * swaps it based on the host endianness. 2994 */ 2995 if (host_is_bigendian()) { 2996 new_val = bitfield_swap(value, 0, 1); 2997 new_val |= bitfield_swap(value, 1, 1); 2998 new_val |= bitfield_swap(value, 2, 1); 2999 new_val |= bitfield_swap(value, 3, 1); 3000 new_val |= bitfield_swap(value, 4, 16); 3001 new_val |= bitfield_swap(value, 20, 4); 3002 new_val |= bitfield_swap(value, 24, 2); 3003 new_val |= bitfield_swap(value, 26, 4); 3004 new_val |= bitfield_swap(value, 30, 3); 3005 new_val |= bitfield_swap(value, 33, 31); 3006 } else { 3007 new_val = bitfield_swap(value, 63, 1); 3008 new_val |= bitfield_swap(value, 62, 1); 3009 new_val |= bitfield_swap(value, 61, 1); 3010 new_val |= bitfield_swap(value, 60, 1); 3011 new_val |= bitfield_swap(value, 44, 16); 3012 new_val |= bitfield_swap(value, 40, 4); 3013 new_val |= bitfield_swap(value, 38, 2); 3014 new_val |= bitfield_swap(value, 34, 4); 3015 new_val |= bitfield_swap(value, 31, 3); 3016 new_val |= bitfield_swap(value, 0, 31); 3017 } 3018 3019 return new_val; 3020 } 3021 3022 static inline bool evsel__has_branch_counters(const struct evsel *evsel) 3023 { 3024 struct evsel *leader = evsel__leader(evsel); 3025 3026 /* The branch counters feature only supports group */ 3027 if (!leader || !evsel->evlist) 3028 return false; 3029 3030 if (evsel->evlist->nr_br_cntr < 0) 3031 evlist__update_br_cntr(evsel->evlist); 3032 3033 if (leader->br_cntr_nr > 0) 3034 return true; 3035 3036 return false; 3037 } 3038 3039 static int __set_offcpu_sample(struct perf_sample *data) 3040 { 3041 u64 *array = data->raw_data; 3042 u32 max_size = data->raw_size, *p32; 3043 const void *endp = (void *)array + max_size; 3044 3045 if (array == NULL) 3046 return -EFAULT; 3047 3048 OVERFLOW_CHECK_u64(array); 3049 p32 = (void *)array++; 3050 data->pid = p32[0]; 3051 data->tid = p32[1]; 3052 3053 OVERFLOW_CHECK_u64(array); 3054 data->period = *array++; 3055 3056 OVERFLOW_CHECK_u64(array); 3057 data->callchain = (struct ip_callchain *)array++; 3058 OVERFLOW_CHECK(array, data->callchain->nr * sizeof(u64), max_size); 3059 data->ip = data->callchain->ips[1]; 3060 array += data->callchain->nr; 3061 3062 OVERFLOW_CHECK_u64(array); 3063 data->cgroup = *array; 3064 3065 return 0; 3066 } 3067 3068 int evsel__parse_sample(struct evsel *evsel, union perf_event *event, 3069 struct perf_sample *data) 3070 { 3071 u64 type = evsel->core.attr.sample_type; 3072 bool swapped = evsel->needs_swap; 3073 const __u64 *array; 3074 u16 max_size = event->header.size; 3075 const void *endp = (void *)event + max_size; 3076 u64 sz; 3077 3078 /* 3079 * used for cross-endian analysis. See git commit 65014ab3 3080 * for why this goofiness is needed. 3081 */ 3082 union u64_swap u; 3083 3084 memset(data, 0, sizeof(*data)); 3085 data->cpu = data->pid = data->tid = -1; 3086 data->stream_id = data->id = data->time = -1ULL; 3087 data->period = evsel->core.attr.sample_period; 3088 data->cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; 3089 data->misc = event->header.misc; 3090 data->data_src = PERF_MEM_DATA_SRC_NONE; 3091 data->vcpu = -1; 3092 3093 if (event->header.type == PERF_RECORD_CALLCHAIN_DEFERRED) { 3094 const u64 max_callchain_nr = UINT64_MAX / sizeof(u64); 3095 3096 data->callchain = (struct ip_callchain *)&event->callchain_deferred.nr; 3097 if (data->callchain->nr > max_callchain_nr) 3098 return -EFAULT; 3099 3100 data->deferred_cookie = event->callchain_deferred.cookie; 3101 3102 if (evsel->core.attr.sample_id_all) 3103 perf_evsel__parse_id_sample(evsel, event, data); 3104 return 0; 3105 } 3106 3107 if (event->header.type != PERF_RECORD_SAMPLE) { 3108 if (!evsel->core.attr.sample_id_all) 3109 return 0; 3110 return perf_evsel__parse_id_sample(evsel, event, data); 3111 } 3112 3113 array = event->sample.array; 3114 3115 if (perf_event__check_size(event, evsel->sample_size)) 3116 return -EFAULT; 3117 3118 if (type & PERF_SAMPLE_IDENTIFIER) { 3119 data->id = *array; 3120 array++; 3121 } 3122 3123 if (type & PERF_SAMPLE_IP) { 3124 data->ip = *array; 3125 array++; 3126 } 3127 3128 if (type & PERF_SAMPLE_TID) { 3129 u.val64 = *array; 3130 if (swapped) { 3131 /* undo swap of u64, then swap on individual u32s */ 3132 u.val64 = bswap_64(u.val64); 3133 u.val32[0] = bswap_32(u.val32[0]); 3134 u.val32[1] = bswap_32(u.val32[1]); 3135 } 3136 3137 data->pid = u.val32[0]; 3138 data->tid = u.val32[1]; 3139 array++; 3140 } 3141 3142 if (type & PERF_SAMPLE_TIME) { 3143 data->time = *array; 3144 array++; 3145 } 3146 3147 if (type & PERF_SAMPLE_ADDR) { 3148 data->addr = *array; 3149 array++; 3150 } 3151 3152 if (type & PERF_SAMPLE_ID) { 3153 data->id = *array; 3154 array++; 3155 } 3156 3157 if (type & PERF_SAMPLE_STREAM_ID) { 3158 data->stream_id = *array; 3159 array++; 3160 } 3161 3162 if (type & PERF_SAMPLE_CPU) { 3163 3164 u.val64 = *array; 3165 if (swapped) { 3166 /* undo swap of u64, then swap on individual u32s */ 3167 u.val64 = bswap_64(u.val64); 3168 u.val32[0] = bswap_32(u.val32[0]); 3169 } 3170 3171 data->cpu = u.val32[0]; 3172 array++; 3173 } 3174 3175 if (type & PERF_SAMPLE_PERIOD) { 3176 data->period = *array; 3177 array++; 3178 } 3179 3180 if (type & PERF_SAMPLE_READ) { 3181 u64 read_format = evsel->core.attr.read_format; 3182 3183 OVERFLOW_CHECK_u64(array); 3184 if (read_format & PERF_FORMAT_GROUP) 3185 data->read.group.nr = *array; 3186 else 3187 data->read.one.value = *array; 3188 3189 array++; 3190 3191 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { 3192 OVERFLOW_CHECK_u64(array); 3193 data->read.time_enabled = *array; 3194 array++; 3195 } 3196 3197 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { 3198 OVERFLOW_CHECK_u64(array); 3199 data->read.time_running = *array; 3200 array++; 3201 } 3202 3203 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */ 3204 if (read_format & PERF_FORMAT_GROUP) { 3205 const u64 max_group_nr = UINT64_MAX / 3206 sizeof(struct sample_read_value); 3207 3208 if (data->read.group.nr > max_group_nr) 3209 return -EFAULT; 3210 3211 sz = data->read.group.nr * sample_read_value_size(read_format); 3212 OVERFLOW_CHECK(array, sz, max_size); 3213 data->read.group.values = 3214 (struct sample_read_value *)array; 3215 array = (void *)array + sz; 3216 } else { 3217 OVERFLOW_CHECK_u64(array); 3218 data->read.one.id = *array; 3219 array++; 3220 3221 if (read_format & PERF_FORMAT_LOST) { 3222 OVERFLOW_CHECK_u64(array); 3223 data->read.one.lost = *array; 3224 array++; 3225 } 3226 } 3227 } 3228 3229 if (type & PERF_SAMPLE_CALLCHAIN) { 3230 const u64 max_callchain_nr = UINT64_MAX / sizeof(u64); 3231 u64 callchain_nr; 3232 3233 OVERFLOW_CHECK_u64(array); 3234 data->callchain = (struct ip_callchain *)array++; 3235 callchain_nr = data->callchain->nr; 3236 if (callchain_nr > max_callchain_nr) 3237 return -EFAULT; 3238 sz = callchain_nr * sizeof(u64); 3239 /* 3240 * Save the cookie for the deferred user callchain. The last 2 3241 * entries in the callchain should be the context marker and the 3242 * cookie. The cookie will be used to match PERF_RECORD_ 3243 * CALLCHAIN_DEFERRED later. 3244 */ 3245 if (evsel->core.attr.defer_callchain && callchain_nr >= 2 && 3246 data->callchain->ips[callchain_nr - 2] == PERF_CONTEXT_USER_DEFERRED) { 3247 data->deferred_cookie = data->callchain->ips[callchain_nr - 1]; 3248 data->deferred_callchain = true; 3249 } 3250 OVERFLOW_CHECK(array, sz, max_size); 3251 array = (void *)array + sz; 3252 } 3253 3254 if (type & PERF_SAMPLE_RAW) { 3255 OVERFLOW_CHECK_u64(array); 3256 u.val64 = *array; 3257 3258 /* 3259 * Undo swap of u64, then swap on individual u32s, 3260 * get the size of the raw area and undo all of the 3261 * swap. The pevent interface handles endianness by 3262 * itself. 3263 */ 3264 if (swapped) { 3265 u.val64 = bswap_64(u.val64); 3266 u.val32[0] = bswap_32(u.val32[0]); 3267 u.val32[1] = bswap_32(u.val32[1]); 3268 } 3269 data->raw_size = u.val32[0]; 3270 3271 /* 3272 * The raw data is aligned on 64bits including the 3273 * u32 size, so it's safe to use mem_bswap_64. 3274 */ 3275 if (swapped) 3276 mem_bswap_64((void *) array, data->raw_size); 3277 3278 array = (void *)array + sizeof(u32); 3279 3280 OVERFLOW_CHECK(array, data->raw_size, max_size); 3281 data->raw_data = (void *)array; 3282 array = (void *)array + data->raw_size; 3283 } 3284 3285 if (type & PERF_SAMPLE_BRANCH_STACK) { 3286 const u64 max_branch_nr = UINT64_MAX / 3287 sizeof(struct branch_entry); 3288 struct branch_entry *e; 3289 unsigned int i; 3290 3291 OVERFLOW_CHECK_u64(array); 3292 data->branch_stack = (struct branch_stack *)array++; 3293 3294 if (data->branch_stack->nr > max_branch_nr) 3295 return -EFAULT; 3296 3297 sz = data->branch_stack->nr * sizeof(struct branch_entry); 3298 if (evsel__has_branch_hw_idx(evsel)) { 3299 sz += sizeof(u64); 3300 e = &data->branch_stack->entries[0]; 3301 } else { 3302 data->no_hw_idx = true; 3303 /* 3304 * if the PERF_SAMPLE_BRANCH_HW_INDEX is not applied, 3305 * only nr and entries[] will be output by kernel. 3306 */ 3307 e = (struct branch_entry *)&data->branch_stack->hw_idx; 3308 } 3309 3310 if (swapped) { 3311 /* 3312 * struct branch_flag does not have endian 3313 * specific bit field definition. And bswap 3314 * will not resolve the issue, since these 3315 * are bit fields. 3316 * 3317 * evsel__bitfield_swap_branch_flags() uses a 3318 * bitfield_swap macro to swap the bit position 3319 * based on the host endians. 3320 */ 3321 for (i = 0; i < data->branch_stack->nr; i++, e++) 3322 e->flags.value = evsel__bitfield_swap_branch_flags(e->flags.value); 3323 } 3324 3325 OVERFLOW_CHECK(array, sz, max_size); 3326 array = (void *)array + sz; 3327 3328 if (evsel__has_branch_counters(evsel)) { 3329 data->branch_stack_cntr = (u64 *)array; 3330 sz = data->branch_stack->nr * sizeof(u64); 3331 3332 OVERFLOW_CHECK(array, sz, max_size); 3333 array = (void *)array + sz; 3334 } 3335 } 3336 3337 if (type & PERF_SAMPLE_REGS_USER) { 3338 struct regs_dump *regs = perf_sample__user_regs(data); 3339 3340 OVERFLOW_CHECK_u64(array); 3341 regs->abi = *array; 3342 array++; 3343 3344 if (regs->abi) { 3345 u64 mask = evsel->core.attr.sample_regs_user; 3346 3347 sz = hweight64(mask) * sizeof(u64); 3348 OVERFLOW_CHECK(array, sz, max_size); 3349 regs->mask = mask; 3350 regs->regs = (u64 *)array; 3351 array = (void *)array + sz; 3352 } 3353 } 3354 3355 if (type & PERF_SAMPLE_STACK_USER) { 3356 OVERFLOW_CHECK_u64(array); 3357 sz = *array++; 3358 3359 data->user_stack.offset = ((char *)(array - 1) 3360 - (char *) event); 3361 3362 if (!sz) { 3363 data->user_stack.size = 0; 3364 } else { 3365 OVERFLOW_CHECK(array, sz, max_size); 3366 data->user_stack.data = (char *)array; 3367 array = (void *)array + sz; 3368 OVERFLOW_CHECK_u64(array); 3369 data->user_stack.size = *array++; 3370 if (WARN_ONCE(data->user_stack.size > sz, 3371 "user stack dump failure\n")) 3372 return -EFAULT; 3373 } 3374 } 3375 3376 if (type & PERF_SAMPLE_WEIGHT_TYPE) { 3377 OVERFLOW_CHECK_u64(array); 3378 perf_parse_sample_weight(data, array, type); 3379 array++; 3380 } 3381 3382 if (type & PERF_SAMPLE_DATA_SRC) { 3383 OVERFLOW_CHECK_u64(array); 3384 data->data_src = *array; 3385 array++; 3386 } 3387 3388 if (type & PERF_SAMPLE_TRANSACTION) { 3389 OVERFLOW_CHECK_u64(array); 3390 data->transaction = *array; 3391 array++; 3392 } 3393 3394 if (type & PERF_SAMPLE_REGS_INTR) { 3395 struct regs_dump *regs = perf_sample__intr_regs(data); 3396 3397 OVERFLOW_CHECK_u64(array); 3398 regs->abi = *array; 3399 array++; 3400 3401 if (regs->abi != PERF_SAMPLE_REGS_ABI_NONE) { 3402 u64 mask = evsel->core.attr.sample_regs_intr; 3403 3404 sz = hweight64(mask) * sizeof(u64); 3405 OVERFLOW_CHECK(array, sz, max_size); 3406 regs->mask = mask; 3407 regs->regs = (u64 *)array; 3408 array = (void *)array + sz; 3409 } 3410 } 3411 3412 data->phys_addr = 0; 3413 if (type & PERF_SAMPLE_PHYS_ADDR) { 3414 data->phys_addr = *array; 3415 array++; 3416 } 3417 3418 data->cgroup = 0; 3419 if (type & PERF_SAMPLE_CGROUP) { 3420 data->cgroup = *array; 3421 array++; 3422 } 3423 3424 data->data_page_size = 0; 3425 if (type & PERF_SAMPLE_DATA_PAGE_SIZE) { 3426 data->data_page_size = *array; 3427 array++; 3428 } 3429 3430 data->code_page_size = 0; 3431 if (type & PERF_SAMPLE_CODE_PAGE_SIZE) { 3432 data->code_page_size = *array; 3433 array++; 3434 } 3435 3436 if (type & PERF_SAMPLE_AUX) { 3437 OVERFLOW_CHECK_u64(array); 3438 sz = *array++; 3439 3440 OVERFLOW_CHECK(array, sz, max_size); 3441 /* Undo swap of data */ 3442 if (swapped) 3443 mem_bswap_64((char *)array, sz); 3444 data->aux_sample.size = sz; 3445 data->aux_sample.data = (char *)array; 3446 array = (void *)array + sz; 3447 } 3448 3449 if (evsel__is_offcpu_event(evsel)) 3450 return __set_offcpu_sample(data); 3451 3452 return 0; 3453 } 3454 3455 int evsel__parse_sample_timestamp(struct evsel *evsel, union perf_event *event, 3456 u64 *timestamp) 3457 { 3458 u64 type = evsel->core.attr.sample_type; 3459 const __u64 *array; 3460 3461 if (!(type & PERF_SAMPLE_TIME)) 3462 return -1; 3463 3464 if (event->header.type != PERF_RECORD_SAMPLE) { 3465 struct perf_sample data = { 3466 .time = -1ULL, 3467 }; 3468 3469 if (!evsel->core.attr.sample_id_all) 3470 return -1; 3471 if (perf_evsel__parse_id_sample(evsel, event, &data)) 3472 return -1; 3473 3474 *timestamp = data.time; 3475 return 0; 3476 } 3477 3478 array = event->sample.array; 3479 3480 if (perf_event__check_size(event, evsel->sample_size)) 3481 return -EFAULT; 3482 3483 if (type & PERF_SAMPLE_IDENTIFIER) 3484 array++; 3485 3486 if (type & PERF_SAMPLE_IP) 3487 array++; 3488 3489 if (type & PERF_SAMPLE_TID) 3490 array++; 3491 3492 if (type & PERF_SAMPLE_TIME) 3493 *timestamp = *array; 3494 3495 return 0; 3496 } 3497 3498 u16 evsel__id_hdr_size(const struct evsel *evsel) 3499 { 3500 u64 sample_type = evsel->core.attr.sample_type; 3501 u16 size = 0; 3502 3503 if (sample_type & PERF_SAMPLE_TID) 3504 size += sizeof(u64); 3505 3506 if (sample_type & PERF_SAMPLE_TIME) 3507 size += sizeof(u64); 3508 3509 if (sample_type & PERF_SAMPLE_ID) 3510 size += sizeof(u64); 3511 3512 if (sample_type & PERF_SAMPLE_STREAM_ID) 3513 size += sizeof(u64); 3514 3515 if (sample_type & PERF_SAMPLE_CPU) 3516 size += sizeof(u64); 3517 3518 if (sample_type & PERF_SAMPLE_IDENTIFIER) 3519 size += sizeof(u64); 3520 3521 return size; 3522 } 3523 3524 #ifdef HAVE_LIBTRACEEVENT 3525 struct tep_format_field *evsel__field(struct evsel *evsel, const char *name) 3526 { 3527 struct tep_event *tp_format = evsel__tp_format(evsel); 3528 3529 return tp_format ? tep_find_field(tp_format, name) : NULL; 3530 } 3531 3532 struct tep_format_field *evsel__common_field(struct evsel *evsel, const char *name) 3533 { 3534 struct tep_event *tp_format = evsel__tp_format(evsel); 3535 3536 return tp_format ? tep_find_common_field(tp_format, name) : NULL; 3537 } 3538 3539 void *evsel__rawptr(struct evsel *evsel, struct perf_sample *sample, const char *name) 3540 { 3541 struct tep_format_field *field = evsel__field(evsel, name); 3542 int offset; 3543 3544 if (!field) 3545 return NULL; 3546 3547 offset = field->offset; 3548 3549 if (field->flags & TEP_FIELD_IS_DYNAMIC) { 3550 offset = *(int *)(sample->raw_data + field->offset); 3551 offset &= 0xffff; 3552 if (tep_field_is_relative(field->flags)) 3553 offset += field->offset + field->size; 3554 } 3555 3556 return sample->raw_data + offset; 3557 } 3558 3559 u64 format_field__intval(struct tep_format_field *field, struct perf_sample *sample, 3560 bool needs_swap) 3561 { 3562 u64 value; 3563 void *ptr = sample->raw_data + field->offset; 3564 3565 switch (field->size) { 3566 case 1: 3567 return *(u8 *)ptr; 3568 case 2: 3569 value = *(u16 *)ptr; 3570 break; 3571 case 4: 3572 value = *(u32 *)ptr; 3573 break; 3574 case 8: 3575 memcpy(&value, ptr, sizeof(u64)); 3576 break; 3577 default: 3578 return 0; 3579 } 3580 3581 if (!needs_swap) 3582 return value; 3583 3584 switch (field->size) { 3585 case 2: 3586 return bswap_16(value); 3587 case 4: 3588 return bswap_32(value); 3589 case 8: 3590 return bswap_64(value); 3591 default: 3592 return 0; 3593 } 3594 3595 return 0; 3596 } 3597 3598 u64 evsel__intval(struct evsel *evsel, struct perf_sample *sample, const char *name) 3599 { 3600 struct tep_format_field *field = evsel__field(evsel, name); 3601 3602 return field ? format_field__intval(field, sample, evsel->needs_swap) : 0; 3603 } 3604 3605 u64 evsel__intval_common(struct evsel *evsel, struct perf_sample *sample, const char *name) 3606 { 3607 struct tep_format_field *field = evsel__common_field(evsel, name); 3608 3609 return field ? format_field__intval(field, sample, evsel->needs_swap) : 0; 3610 } 3611 3612 char evsel__taskstate(struct evsel *evsel, struct perf_sample *sample, const char *name) 3613 { 3614 static struct tep_format_field *prev_state_field; 3615 static const char *states; 3616 struct tep_format_field *field; 3617 unsigned long long val; 3618 unsigned int bit; 3619 char state = '?'; /* '?' denotes unknown task state */ 3620 3621 field = evsel__field(evsel, name); 3622 3623 if (!field) 3624 return state; 3625 3626 if (!states || field != prev_state_field) { 3627 states = parse_task_states(field); 3628 if (!states) 3629 return state; 3630 prev_state_field = field; 3631 } 3632 3633 /* 3634 * Note since the kernel exposes TASK_REPORT_MAX to userspace 3635 * to denote the 'preempted' state, we might as welll report 3636 * 'R' for this case, which make senses to users as well. 3637 * 3638 * We can change this if we have a good reason in the future. 3639 */ 3640 val = evsel__intval(evsel, sample, name); 3641 bit = val ? ffs(val) : 0; 3642 state = (!bit || bit > strlen(states)) ? 'R' : states[bit-1]; 3643 return state; 3644 } 3645 #endif 3646 3647 bool evsel__fallback(struct evsel *evsel, struct target *target, int err, 3648 char *msg, size_t msgsize) 3649 { 3650 int paranoid; 3651 3652 if ((err == ENOENT || err == ENXIO || err == ENODEV) && 3653 evsel->core.attr.type == PERF_TYPE_HARDWARE && 3654 evsel->core.attr.config == PERF_COUNT_HW_CPU_CYCLES) { 3655 /* 3656 * If it's cycles then fall back to hrtimer based cpu-clock sw 3657 * counter, which is always available even if no PMU support. 3658 * 3659 * PPC returns ENXIO until 2.6.37 (behavior changed with commit 3660 * b0a873e). 3661 */ 3662 evsel->core.attr.type = PERF_TYPE_SOFTWARE; 3663 evsel->core.attr.config = target__has_cpu(target) 3664 ? PERF_COUNT_SW_CPU_CLOCK 3665 : PERF_COUNT_SW_TASK_CLOCK; 3666 scnprintf(msg, msgsize, 3667 "The cycles event is not supported, trying to fall back to %s", 3668 target__has_cpu(target) ? "cpu-clock" : "task-clock"); 3669 3670 zfree(&evsel->name); 3671 return true; 3672 } else if (err == EACCES && !evsel->core.attr.exclude_kernel && 3673 (paranoid = perf_event_paranoid()) > 1) { 3674 const char *name = evsel__name(evsel); 3675 char *new_name; 3676 const char *sep = ":"; 3677 3678 /* If event has exclude user then don't exclude kernel. */ 3679 if (evsel->core.attr.exclude_user) 3680 goto no_fallback; 3681 3682 /* Is there already the separator in the name. */ 3683 if (strchr(name, '/') || 3684 (strchr(name, ':') && !evsel->is_libpfm_event)) 3685 sep = ""; 3686 3687 if (asprintf(&new_name, "%s%su", name, sep) < 0) 3688 goto no_fallback; 3689 3690 free(evsel->name); 3691 evsel->name = new_name; 3692 scnprintf(msg, msgsize, "kernel.perf_event_paranoid=%d, trying " 3693 "to fall back to excluding kernel and hypervisor " 3694 " samples", paranoid); 3695 evsel->core.attr.exclude_kernel = 1; 3696 evsel->core.attr.exclude_hv = 1; 3697 3698 return true; 3699 } else if (err == EOPNOTSUPP && !evsel->core.attr.exclude_guest && 3700 !evsel->exclude_GH) { 3701 const char *name = evsel__name(evsel); 3702 char *new_name; 3703 const char *sep = ":"; 3704 3705 /* Is there already the separator in the name. */ 3706 if (strchr(name, '/') || 3707 (strchr(name, ':') && !evsel->is_libpfm_event)) 3708 sep = ""; 3709 3710 if (asprintf(&new_name, "%s%sH", name, sep) < 0) 3711 goto no_fallback; 3712 3713 free(evsel->name); 3714 evsel->name = new_name; 3715 /* Apple M1 requires exclude_guest */ 3716 scnprintf(msg, msgsize, "Trying to fall back to excluding guest samples"); 3717 evsel->core.attr.exclude_guest = 1; 3718 3719 return true; 3720 } 3721 no_fallback: 3722 scnprintf(msg, msgsize, "No fallback found for '%s' for error %d", 3723 evsel__name(evsel), err); 3724 return false; 3725 } 3726 3727 static bool find_process(const char *name) 3728 { 3729 size_t len = strlen(name); 3730 DIR *dir; 3731 struct dirent *d; 3732 int ret = -1; 3733 3734 dir = opendir(procfs__mountpoint()); 3735 if (!dir) 3736 return false; 3737 3738 /* Walk through the directory. */ 3739 while (ret && (d = readdir(dir)) != NULL) { 3740 char path[PATH_MAX]; 3741 char *data; 3742 size_t size; 3743 3744 if ((d->d_type != DT_DIR) || 3745 !strcmp(".", d->d_name) || 3746 !strcmp("..", d->d_name)) 3747 continue; 3748 3749 scnprintf(path, sizeof(path), "%s/%s/comm", 3750 procfs__mountpoint(), d->d_name); 3751 3752 if (filename__read_str(path, &data, &size)) 3753 continue; 3754 3755 ret = strncmp(name, data, len); 3756 free(data); 3757 } 3758 3759 closedir(dir); 3760 return ret ? false : true; 3761 } 3762 3763 static int dump_perf_event_processes(char *msg, size_t size) 3764 { 3765 DIR *proc_dir; 3766 struct dirent *proc_entry; 3767 int printed = 0; 3768 3769 proc_dir = opendir(procfs__mountpoint()); 3770 if (!proc_dir) 3771 return 0; 3772 3773 /* Walk through the /proc directory. */ 3774 while ((proc_entry = readdir(proc_dir)) != NULL) { 3775 char buf[256]; 3776 DIR *fd_dir; 3777 struct dirent *fd_entry; 3778 int fd_dir_fd; 3779 3780 if (proc_entry->d_type != DT_DIR || 3781 !isdigit(proc_entry->d_name[0]) || 3782 strlen(proc_entry->d_name) > sizeof(buf) - 4) 3783 continue; 3784 3785 scnprintf(buf, sizeof(buf), "%s/fd", proc_entry->d_name); 3786 fd_dir_fd = openat(dirfd(proc_dir), buf, O_DIRECTORY); 3787 if (fd_dir_fd == -1) 3788 continue; 3789 fd_dir = fdopendir(fd_dir_fd); 3790 if (!fd_dir) { 3791 close(fd_dir_fd); 3792 continue; 3793 } 3794 while ((fd_entry = readdir(fd_dir)) != NULL) { 3795 ssize_t link_size; 3796 3797 if (fd_entry->d_type != DT_LNK) 3798 continue; 3799 link_size = readlinkat(fd_dir_fd, fd_entry->d_name, buf, sizeof(buf)); 3800 if (link_size < 0) 3801 continue; 3802 /* Take care as readlink doesn't null terminate the string. */ 3803 if (!strncmp(buf, "anon_inode:[perf_event]", link_size)) { 3804 int cmdline_fd; 3805 ssize_t cmdline_size; 3806 3807 scnprintf(buf, sizeof(buf), "%s/cmdline", proc_entry->d_name); 3808 cmdline_fd = openat(dirfd(proc_dir), buf, O_RDONLY); 3809 if (cmdline_fd == -1) 3810 continue; 3811 cmdline_size = read(cmdline_fd, buf, sizeof(buf) - 1); 3812 close(cmdline_fd); 3813 if (cmdline_size < 0) 3814 continue; 3815 buf[cmdline_size] = '\0'; 3816 for (ssize_t i = 0; i < cmdline_size; i++) { 3817 if (buf[i] == '\0') 3818 buf[i] = ' '; 3819 } 3820 3821 if (printed == 0) 3822 printed += scnprintf(msg, size, "Possible processes:\n"); 3823 3824 printed += scnprintf(msg + printed, size - printed, 3825 "%s %s\n", proc_entry->d_name, buf); 3826 break; 3827 } 3828 } 3829 closedir(fd_dir); 3830 } 3831 closedir(proc_dir); 3832 return printed; 3833 } 3834 3835 int __weak arch_evsel__open_strerror(struct evsel *evsel __maybe_unused, 3836 int err __maybe_unused, 3837 char *msg __maybe_unused, 3838 size_t size __maybe_unused) 3839 { 3840 return 0; 3841 } 3842 3843 int evsel__open_strerror(struct evsel *evsel, struct target *target, 3844 int err, char *msg, size_t size) 3845 { 3846 struct perf_pmu *pmu; 3847 char sbuf[STRERR_BUFSIZE]; 3848 int printed = 0, enforced = 0; 3849 int ret; 3850 3851 switch (err) { 3852 case EPERM: 3853 case EACCES: 3854 printed += scnprintf(msg + printed, size - printed, 3855 "Access to performance monitoring and observability operations is limited.\n"); 3856 3857 if (!sysfs__read_int("fs/selinux/enforce", &enforced)) { 3858 if (enforced) { 3859 printed += scnprintf(msg + printed, size - printed, 3860 "Enforced MAC policy settings (SELinux) can limit access to performance\n" 3861 "monitoring and observability operations. Inspect system audit records for\n" 3862 "more perf_event access control information and adjusting the policy.\n"); 3863 } 3864 } 3865 3866 if (err == EPERM) 3867 printed += scnprintf(msg, size, 3868 "No permission to enable %s event.\n\n", evsel__name(evsel)); 3869 3870 return printed + scnprintf(msg + printed, size - printed, 3871 "Consider adjusting /proc/sys/kernel/perf_event_paranoid setting to open\n" 3872 "access to performance monitoring and observability operations for processes\n" 3873 "without CAP_PERFMON, CAP_SYS_PTRACE or CAP_SYS_ADMIN Linux capability.\n" 3874 "More information can be found at 'Perf events and tool security' document:\n" 3875 "https://www.kernel.org/doc/html/latest/admin-guide/perf-security.html\n" 3876 "perf_event_paranoid setting is %d:\n" 3877 " -1: Allow use of (almost) all events by all users\n" 3878 " Ignore mlock limit after perf_event_mlock_kb without CAP_IPC_LOCK\n" 3879 ">= 0: Disallow raw and ftrace function tracepoint access\n" 3880 ">= 1: Disallow CPU event access\n" 3881 ">= 2: Disallow kernel profiling\n" 3882 "To make the adjusted perf_event_paranoid setting permanent preserve it\n" 3883 "in /etc/sysctl.conf (e.g. kernel.perf_event_paranoid = <setting>)", 3884 perf_event_paranoid()); 3885 case ENOENT: 3886 return scnprintf(msg, size, "The %s event is not supported.", evsel__name(evsel)); 3887 case EMFILE: 3888 return scnprintf(msg, size, "%s", 3889 "Too many events are opened.\n" 3890 "Probably the maximum number of open file descriptors has been reached.\n" 3891 "Hint: Try again after reducing the number of events.\n" 3892 "Hint: Try increasing the limit with 'ulimit -n <limit>'"); 3893 case ENOMEM: 3894 if (evsel__has_callchain(evsel) && 3895 access("/proc/sys/kernel/perf_event_max_stack", F_OK) == 0) 3896 return scnprintf(msg, size, 3897 "Not enough memory to setup event with callchain.\n" 3898 "Hint: Try tweaking /proc/sys/kernel/perf_event_max_stack\n" 3899 "Hint: Current value: %d", sysctl__max_stack()); 3900 break; 3901 case ENODEV: 3902 if (target->cpu_list) 3903 return scnprintf(msg, size, "%s", 3904 "No such device - did you specify an out-of-range profile CPU?"); 3905 break; 3906 case EOPNOTSUPP: 3907 if (evsel->core.attr.sample_type & PERF_SAMPLE_BRANCH_STACK) 3908 return scnprintf(msg, size, 3909 "%s: PMU Hardware or event type doesn't support branch stack sampling.", 3910 evsel__name(evsel)); 3911 if (evsel->core.attr.aux_output) 3912 return scnprintf(msg, size, 3913 "%s: PMU Hardware doesn't support 'aux_output' feature", 3914 evsel__name(evsel)); 3915 if (evsel->core.attr.aux_action) 3916 return scnprintf(msg, size, 3917 "%s: PMU Hardware doesn't support 'aux_action' feature", 3918 evsel__name(evsel)); 3919 if (evsel->core.attr.sample_period != 0) 3920 return scnprintf(msg, size, 3921 "%s: PMU Hardware doesn't support sampling/overflow-interrupts. Try 'perf stat'", 3922 evsel__name(evsel)); 3923 if (evsel->core.attr.precise_ip) 3924 return scnprintf(msg, size, "%s", 3925 "\'precise\' request may not be supported. Try removing 'p' modifier."); 3926 #if defined(__i386__) || defined(__x86_64__) 3927 if (evsel->core.attr.type == PERF_TYPE_HARDWARE) 3928 return scnprintf(msg, size, "%s", 3929 "No hardware sampling interrupt available.\n"); 3930 #endif 3931 if (!target__has_cpu(target)) 3932 return scnprintf(msg, size, 3933 "Unsupported event (%s) in per-thread mode, enable system wide with '-a'.", 3934 evsel__name(evsel)); 3935 break; 3936 case EBUSY: 3937 if (find_process("oprofiled")) 3938 return scnprintf(msg, size, 3939 "The PMU counters are busy/taken by another profiler.\n" 3940 "We found oprofile daemon running, please stop it and try again."); 3941 printed += scnprintf( 3942 msg, size, 3943 "The PMU %s counters are busy and in use by another process.\n", 3944 evsel->pmu ? evsel->pmu->name : ""); 3945 return printed + dump_perf_event_processes(msg + printed, size - printed); 3946 break; 3947 case EINVAL: 3948 if (evsel->core.attr.sample_type & PERF_SAMPLE_CODE_PAGE_SIZE && perf_missing_features.code_page_size) 3949 return scnprintf(msg, size, "Asking for the code page size isn't supported by this kernel."); 3950 if (evsel->core.attr.sample_type & PERF_SAMPLE_DATA_PAGE_SIZE && perf_missing_features.data_page_size) 3951 return scnprintf(msg, size, "Asking for the data page size isn't supported by this kernel."); 3952 if (evsel->core.attr.write_backward && perf_missing_features.write_backward) 3953 return scnprintf(msg, size, "Reading from overwrite event is not supported by this kernel."); 3954 if (perf_missing_features.clockid) 3955 return scnprintf(msg, size, "clockid feature not supported."); 3956 if (perf_missing_features.clockid_wrong) 3957 return scnprintf(msg, size, "wrong clockid (%d).", clockid); 3958 if (perf_missing_features.aux_action) 3959 return scnprintf(msg, size, "The 'aux_action' feature is not supported, update the kernel."); 3960 if (perf_missing_features.aux_output) 3961 return scnprintf(msg, size, "The 'aux_output' feature is not supported, update the kernel."); 3962 pmu = evsel__find_pmu(evsel); 3963 if (!pmu->is_core && !target__has_cpu(target)) 3964 return scnprintf(msg, size, 3965 "Invalid event (%s) in per-thread mode, enable system wide with '-a'.", 3966 evsel__name(evsel)); 3967 3968 break; 3969 case ENODATA: 3970 return scnprintf(msg, size, "Cannot collect data source with the load latency event alone. " 3971 "Please add an auxiliary event in front of the load latency event."); 3972 default: 3973 break; 3974 } 3975 3976 ret = arch_evsel__open_strerror(evsel, err, msg, size); 3977 if (ret) 3978 return ret; 3979 3980 return scnprintf(msg, size, 3981 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n" 3982 "\"dmesg | grep -i perf\" may provide additional information.\n", 3983 err, str_error_r(err, sbuf, sizeof(sbuf)), evsel__name(evsel)); 3984 } 3985 3986 struct perf_session *evsel__session(struct evsel *evsel) 3987 { 3988 return evsel && evsel->evlist ? evsel->evlist->session : NULL; 3989 } 3990 3991 struct perf_env *evsel__env(struct evsel *evsel) 3992 { 3993 struct perf_session *session = evsel__session(evsel); 3994 3995 return session ? perf_session__env(session) : NULL; 3996 } 3997 3998 static int store_evsel_ids(struct evsel *evsel, struct evlist *evlist) 3999 { 4000 int cpu_map_idx, thread; 4001 4002 if (evsel__is_retire_lat(evsel)) 4003 return 0; 4004 4005 if (perf_pmu__kind(evsel->pmu) != PERF_PMU_KIND_PE) 4006 return 0; 4007 4008 for (cpu_map_idx = 0; cpu_map_idx < xyarray__max_x(evsel->core.fd); cpu_map_idx++) { 4009 for (thread = 0; thread < xyarray__max_y(evsel->core.fd); 4010 thread++) { 4011 int fd = FD(evsel, cpu_map_idx, thread); 4012 4013 if (perf_evlist__id_add_fd(&evlist->core, &evsel->core, 4014 cpu_map_idx, thread, fd) < 0) 4015 return -1; 4016 } 4017 } 4018 4019 return 0; 4020 } 4021 4022 int evsel__store_ids(struct evsel *evsel, struct evlist *evlist) 4023 { 4024 struct perf_cpu_map *cpus = evsel->core.cpus; 4025 struct perf_thread_map *threads = evsel->core.threads; 4026 4027 if (perf_evsel__alloc_id(&evsel->core, perf_cpu_map__nr(cpus), threads->nr)) 4028 return -ENOMEM; 4029 4030 return store_evsel_ids(evsel, evlist); 4031 } 4032 4033 void evsel__zero_per_pkg(struct evsel *evsel) 4034 { 4035 struct hashmap_entry *cur; 4036 size_t bkt; 4037 4038 if (evsel->per_pkg_mask) { 4039 hashmap__for_each_entry(evsel->per_pkg_mask, cur, bkt) 4040 zfree(&cur->pkey); 4041 4042 hashmap__clear(evsel->per_pkg_mask); 4043 } 4044 } 4045 4046 /** 4047 * evsel__is_hybrid - does the evsel have a known PMU that is hybrid. Note, this 4048 * will be false on hybrid systems for hardware and legacy 4049 * cache events. 4050 */ 4051 bool evsel__is_hybrid(const struct evsel *evsel) 4052 { 4053 if (!evsel->core.is_pmu_core) 4054 return false; 4055 4056 return perf_pmus__num_core_pmus() > 1; 4057 } 4058 4059 struct evsel *evsel__leader(const struct evsel *evsel) 4060 { 4061 if (evsel->core.leader == NULL) 4062 return NULL; 4063 return container_of(evsel->core.leader, struct evsel, core); 4064 } 4065 4066 bool evsel__has_leader(struct evsel *evsel, struct evsel *leader) 4067 { 4068 return evsel->core.leader == &leader->core; 4069 } 4070 4071 bool evsel__is_leader(struct evsel *evsel) 4072 { 4073 return evsel__has_leader(evsel, evsel); 4074 } 4075 4076 void evsel__set_leader(struct evsel *evsel, struct evsel *leader) 4077 { 4078 evsel->core.leader = &leader->core; 4079 } 4080 4081 int evsel__source_count(const struct evsel *evsel) 4082 { 4083 struct evsel *pos; 4084 int count = 0; 4085 4086 evlist__for_each_entry(evsel->evlist, pos) { 4087 if (pos->metric_leader == evsel) 4088 count++; 4089 } 4090 return count; 4091 } 4092 4093 bool __weak arch_evsel__must_be_in_group(const struct evsel *evsel __maybe_unused) 4094 { 4095 return false; 4096 } 4097 4098 /* 4099 * Remove an event from a given group (leader). 4100 * Some events, e.g., perf metrics Topdown events, 4101 * must always be grouped. Ignore the events. 4102 */ 4103 void evsel__remove_from_group(struct evsel *evsel, struct evsel *leader) 4104 { 4105 if (!arch_evsel__must_be_in_group(evsel) && evsel != leader) { 4106 evsel__set_leader(evsel, evsel); 4107 evsel->core.nr_members = 0; 4108 leader->core.nr_members--; 4109 } 4110 } 4111 4112 bool evsel__set_needs_uniquify(struct evsel *counter, const struct perf_stat_config *config) 4113 { 4114 struct evsel *evsel; 4115 4116 if (counter->needs_uniquify) { 4117 /* Already set. */ 4118 return true; 4119 } 4120 4121 if (counter->use_config_name || counter->is_libpfm_event) { 4122 /* Original name will be used. */ 4123 return false; 4124 } 4125 4126 if (!config->hybrid_merge && evsel__is_hybrid(counter)) { 4127 /* Unique hybrid counters necessary. */ 4128 counter->needs_uniquify = true; 4129 return true; 4130 } 4131 4132 if (counter->core.attr.type < PERF_TYPE_MAX && counter->core.attr.type != PERF_TYPE_RAW) { 4133 /* Legacy event, don't uniquify. */ 4134 return false; 4135 } 4136 4137 if (counter->pmu && counter->pmu->is_core && 4138 counter->alternate_hw_config != PERF_COUNT_HW_MAX) { 4139 /* A sysfs or json event replacing a legacy event, don't uniquify. */ 4140 return false; 4141 } 4142 4143 if (config->aggr_mode == AGGR_NONE) { 4144 /* Always unique with no aggregation. */ 4145 counter->needs_uniquify = true; 4146 return true; 4147 } 4148 4149 if (counter->first_wildcard_match != NULL) { 4150 /* 4151 * If stats are merged then only the first_wildcard_match is 4152 * displayed, there is no need to uniquify this evsel as the 4153 * name won't be shown. 4154 */ 4155 return false; 4156 } 4157 4158 /* 4159 * Do other non-merged events in the evlist have the same name? If so 4160 * uniquify is necessary. 4161 */ 4162 evlist__for_each_entry(counter->evlist, evsel) { 4163 if (evsel == counter || evsel->first_wildcard_match || evsel->pmu == counter->pmu) 4164 continue; 4165 4166 if (evsel__name_is(counter, evsel__name(evsel))) { 4167 counter->needs_uniquify = true; 4168 return true; 4169 } 4170 } 4171 return false; 4172 } 4173 4174 void evsel__uniquify_counter(struct evsel *counter) 4175 { 4176 const char *name, *pmu_name, *config; 4177 char *new_name; 4178 int len, ret; 4179 4180 /* No uniquification necessary. */ 4181 if (!counter->needs_uniquify) 4182 return; 4183 4184 /* The evsel was already uniquified. */ 4185 if (counter->uniquified_name) 4186 return; 4187 4188 /* Avoid checking to uniquify twice. */ 4189 counter->uniquified_name = true; 4190 4191 name = evsel__name(counter); 4192 config = strchr(name, '/'); 4193 pmu_name = counter->pmu->name; 4194 4195 /* Already prefixed by the PMU name? */ 4196 len = pmu_name_len_no_suffix(pmu_name); 4197 4198 if (!strncmp(name, pmu_name, len)) { 4199 /* 4200 * If the PMU name is there, then there is no sense in not 4201 * having a slash. Do this for robustness. 4202 */ 4203 if (config == NULL) 4204 config = name - 1; 4205 4206 ret = asprintf(&new_name, "%s/%s", pmu_name, config + 1); 4207 } else if (config) { 4208 len = config - name; 4209 if (config[1] == '/') { 4210 /* case: event// */ 4211 ret = asprintf(&new_name, "%s/%.*s/%s", pmu_name, len, name, config + 2); 4212 } else { 4213 /* case: event/.../ */ 4214 ret = asprintf(&new_name, "%s/%.*s,%s", pmu_name, len, name, config + 1); 4215 } 4216 } else { 4217 config = strchr(name, ':'); 4218 if (config) { 4219 /* case: event:.. */ 4220 len = config - name; 4221 4222 ret = asprintf(&new_name, "%s/%.*s/%s", pmu_name, len, name, config + 1); 4223 } else { 4224 /* case: event */ 4225 ret = asprintf(&new_name, "%s/%s/", pmu_name, name); 4226 } 4227 } 4228 if (ret > 0) { 4229 free(counter->name); 4230 counter->name = new_name; 4231 } else { 4232 /* ENOMEM from asprintf. */ 4233 counter->uniquified_name = false; 4234 } 4235 } 4236 4237 void evsel__warn_user_requested_cpus(struct evsel *evsel, struct perf_cpu_map *user_requested_cpus) 4238 { 4239 struct perf_cpu_map *intersect, *online = NULL; 4240 const struct perf_pmu *pmu = evsel__find_pmu(evsel); 4241 4242 if (pmu && pmu->is_core) { 4243 intersect = perf_cpu_map__intersect(pmu->cpus, user_requested_cpus); 4244 } else { 4245 online = cpu_map__online(); 4246 intersect = perf_cpu_map__intersect(online, user_requested_cpus); 4247 } 4248 if (!perf_cpu_map__equal(intersect, user_requested_cpus)) { 4249 char buf1[128]; 4250 char buf2[128]; 4251 4252 cpu_map__snprint(user_requested_cpus, buf1, sizeof(buf1)); 4253 cpu_map__snprint(online ?: pmu->cpus, buf2, sizeof(buf2)); 4254 pr_warning("WARNING: A requested CPU in '%s' is not supported by PMU '%s' (CPUs %s) for event '%s'\n", 4255 buf1, pmu ? pmu->name : "cpu", buf2, evsel__name(evsel)); 4256 } 4257 perf_cpu_map__put(intersect); 4258 perf_cpu_map__put(online); 4259 } 4260