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->metric_events = NULL; 406 evsel->per_pkg_mask = NULL; 407 evsel->collect_stat = false; 408 evsel->group_pmu_name = NULL; 409 evsel->skippable = false; 410 evsel->supported = true; 411 evsel->alternate_hw_config = PERF_COUNT_HW_MAX; 412 evsel->script_output_type = -1; // FIXME: OUTPUT_TYPE_UNSET, see builtin-script.c 413 } 414 415 struct evsel *evsel__new_idx(struct perf_event_attr *attr, int idx) 416 { 417 struct evsel *evsel = zalloc(perf_evsel__object.size); 418 419 if (!evsel) 420 return NULL; 421 evsel__init(evsel, attr, idx); 422 423 if (evsel__is_bpf_output(evsel) && !attr->sample_type) { 424 evsel->core.attr.sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME | 425 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD), 426 evsel->core.attr.sample_period = 1; 427 } 428 429 if (evsel__is_clock(evsel)) { 430 free((char *)evsel->unit); 431 evsel->unit = strdup("msec"); 432 evsel->scale = 1e-6; 433 } 434 435 return evsel; 436 } 437 438 int copy_config_terms(struct list_head *dst, struct list_head *src) 439 { 440 struct evsel_config_term *pos, *tmp; 441 442 list_for_each_entry(pos, src, list) { 443 tmp = malloc(sizeof(*tmp)); 444 if (tmp == NULL) 445 return -ENOMEM; 446 447 *tmp = *pos; 448 if (tmp->free_str) { 449 tmp->val.str = strdup(pos->val.str); 450 if (tmp->val.str == NULL) { 451 free(tmp); 452 return -ENOMEM; 453 } 454 } 455 list_add_tail(&tmp->list, dst); 456 } 457 return 0; 458 } 459 460 static int evsel__copy_config_terms(struct evsel *dst, struct evsel *src) 461 { 462 return copy_config_terms(&dst->config_terms, &src->config_terms); 463 } 464 465 /** 466 * evsel__clone - create a new evsel copied from @orig 467 * @orig: original evsel 468 * 469 * The assumption is that @orig is not configured nor opened yet. 470 * So we only care about the attributes that can be set while it's parsed. 471 */ 472 struct evsel *evsel__clone(struct evsel *dest, struct evsel *orig) 473 { 474 struct evsel *evsel; 475 476 BUG_ON(orig->core.fd); 477 BUG_ON(orig->counts); 478 BUG_ON(orig->priv); 479 BUG_ON(orig->per_pkg_mask); 480 481 /* cannot handle BPF objects for now */ 482 if (orig->bpf_obj) 483 return NULL; 484 485 if (dest) 486 evsel = dest; 487 else 488 evsel = evsel__new(&orig->core.attr); 489 490 if (evsel == NULL) 491 return NULL; 492 493 evsel->core.cpus = perf_cpu_map__get(orig->core.cpus); 494 evsel->core.pmu_cpus = perf_cpu_map__get(orig->core.pmu_cpus); 495 evsel->core.threads = perf_thread_map__get(orig->core.threads); 496 evsel->core.nr_members = orig->core.nr_members; 497 evsel->core.system_wide = orig->core.system_wide; 498 evsel->core.requires_cpu = orig->core.requires_cpu; 499 evsel->core.is_pmu_core = orig->core.is_pmu_core; 500 501 if (orig->name) { 502 evsel->name = strdup(orig->name); 503 if (evsel->name == NULL) 504 goto out_err; 505 } 506 if (orig->group_name) { 507 evsel->group_name = strdup(orig->group_name); 508 if (evsel->group_name == NULL) 509 goto out_err; 510 } 511 if (orig->group_pmu_name) { 512 evsel->group_pmu_name = strdup(orig->group_pmu_name); 513 if (evsel->group_pmu_name == NULL) 514 goto out_err; 515 } 516 if (orig->filter) { 517 evsel->filter = strdup(orig->filter); 518 if (evsel->filter == NULL) 519 goto out_err; 520 } 521 if (orig->metric_id) { 522 evsel->metric_id = strdup(orig->metric_id); 523 if (evsel->metric_id == NULL) 524 goto out_err; 525 } 526 evsel->cgrp = cgroup__get(orig->cgrp); 527 #ifdef HAVE_LIBTRACEEVENT 528 if (orig->tp_sys) { 529 evsel->tp_sys = strdup(orig->tp_sys); 530 if (evsel->tp_sys == NULL) 531 goto out_err; 532 } 533 if (orig->tp_name) { 534 evsel->tp_name = strdup(orig->tp_name); 535 if (evsel->tp_name == NULL) 536 goto out_err; 537 } 538 evsel->tp_format = orig->tp_format; 539 #endif 540 evsel->handler = orig->handler; 541 evsel->core.leader = orig->core.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 zfree(&evsel->metric_events); 1758 if (evsel__priv_destructor) 1759 evsel__priv_destructor(evsel->priv); 1760 perf_evsel__object.fini(evsel); 1761 if (evsel__tool_event(evsel) == TOOL_PMU__EVENT_SYSTEM_TIME || 1762 evsel__tool_event(evsel) == TOOL_PMU__EVENT_USER_TIME) 1763 xyarray__delete(evsel->start_times); 1764 } 1765 1766 void evsel__delete(struct evsel *evsel) 1767 { 1768 if (!evsel) 1769 return; 1770 1771 evsel__exit(evsel); 1772 free(evsel); 1773 } 1774 1775 void evsel__compute_deltas(struct evsel *evsel, int cpu_map_idx, int thread, 1776 struct perf_counts_values *count) 1777 { 1778 struct perf_counts_values tmp; 1779 1780 if (!evsel->prev_raw_counts) 1781 return; 1782 1783 tmp = *perf_counts(evsel->prev_raw_counts, cpu_map_idx, thread); 1784 *perf_counts(evsel->prev_raw_counts, cpu_map_idx, thread) = *count; 1785 1786 count->val = count->val - tmp.val; 1787 count->ena = count->ena - tmp.ena; 1788 count->run = count->run - tmp.run; 1789 } 1790 1791 static int evsel__read_one(struct evsel *evsel, int cpu_map_idx, int thread) 1792 { 1793 struct perf_counts_values *count = perf_counts(evsel->counts, cpu_map_idx, thread); 1794 1795 return perf_evsel__read(&evsel->core, cpu_map_idx, thread, count); 1796 } 1797 1798 static void evsel__set_count(struct evsel *counter, int cpu_map_idx, int thread, 1799 u64 val, u64 ena, u64 run, u64 lost) 1800 { 1801 struct perf_counts_values *count; 1802 1803 count = perf_counts(counter->counts, cpu_map_idx, thread); 1804 1805 if (evsel__is_retire_lat(counter)) { 1806 evsel__tpebs_read(counter, cpu_map_idx, thread); 1807 perf_counts__set_loaded(counter->counts, cpu_map_idx, thread, true); 1808 return; 1809 } 1810 1811 count->val = val; 1812 count->ena = ena; 1813 count->run = run; 1814 count->lost = lost; 1815 1816 perf_counts__set_loaded(counter->counts, cpu_map_idx, thread, true); 1817 } 1818 1819 static bool evsel__group_has_tpebs(struct evsel *leader) 1820 { 1821 struct evsel *evsel; 1822 1823 for_each_group_evsel(evsel, leader) { 1824 if (evsel__is_retire_lat(evsel)) 1825 return true; 1826 } 1827 return false; 1828 } 1829 1830 static u64 evsel__group_read_nr_members(struct evsel *leader) 1831 { 1832 u64 nr = leader->core.nr_members; 1833 struct evsel *evsel; 1834 1835 for_each_group_evsel(evsel, leader) { 1836 if (evsel__is_retire_lat(evsel)) 1837 nr--; 1838 } 1839 return nr; 1840 } 1841 1842 static u64 evsel__group_read_size(struct evsel *leader) 1843 { 1844 u64 read_format = leader->core.attr.read_format; 1845 int entry = sizeof(u64); /* value */ 1846 int size = 0; 1847 int nr = 1; 1848 1849 if (!evsel__group_has_tpebs(leader)) 1850 return perf_evsel__read_size(&leader->core); 1851 1852 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 1853 size += sizeof(u64); 1854 1855 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 1856 size += sizeof(u64); 1857 1858 if (read_format & PERF_FORMAT_ID) 1859 entry += sizeof(u64); 1860 1861 if (read_format & PERF_FORMAT_LOST) 1862 entry += sizeof(u64); 1863 1864 if (read_format & PERF_FORMAT_GROUP) { 1865 nr = evsel__group_read_nr_members(leader); 1866 size += sizeof(u64); 1867 } 1868 1869 size += entry * nr; 1870 return size; 1871 } 1872 1873 static int evsel__process_group_data(struct evsel *leader, int cpu_map_idx, int thread, u64 *data) 1874 { 1875 u64 read_format = leader->core.attr.read_format; 1876 struct sample_read_value *v; 1877 u64 nr, ena = 0, run = 0, lost = 0; 1878 1879 nr = *data++; 1880 1881 if (nr != evsel__group_read_nr_members(leader)) 1882 return -EINVAL; 1883 1884 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 1885 ena = *data++; 1886 1887 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 1888 run = *data++; 1889 1890 v = (void *)data; 1891 sample_read_group__for_each(v, nr, read_format) { 1892 struct evsel *counter; 1893 1894 counter = evlist__id2evsel(leader->evlist, v->id); 1895 if (!counter) 1896 return -EINVAL; 1897 1898 if (read_format & PERF_FORMAT_LOST) 1899 lost = v->lost; 1900 1901 evsel__set_count(counter, cpu_map_idx, thread, v->value, ena, run, lost); 1902 } 1903 1904 return 0; 1905 } 1906 1907 static int evsel__read_group(struct evsel *leader, int cpu_map_idx, int thread) 1908 { 1909 struct perf_stat_evsel *ps = leader->stats; 1910 u64 read_format = leader->core.attr.read_format; 1911 int size = evsel__group_read_size(leader); 1912 u64 *data = ps->group_data; 1913 1914 if (!(read_format & PERF_FORMAT_ID)) 1915 return -EINVAL; 1916 1917 if (!evsel__is_group_leader(leader)) 1918 return -EINVAL; 1919 1920 if (!data) { 1921 data = zalloc(size); 1922 if (!data) 1923 return -ENOMEM; 1924 1925 ps->group_data = data; 1926 } 1927 1928 if (FD(leader, cpu_map_idx, thread) < 0) 1929 return -EINVAL; 1930 1931 if (readn(FD(leader, cpu_map_idx, thread), data, size) <= 0) 1932 return -errno; 1933 1934 return evsel__process_group_data(leader, cpu_map_idx, thread, data); 1935 } 1936 1937 bool __evsel__match(const struct evsel *evsel, u32 type, u64 config) 1938 { 1939 1940 u32 e_type = evsel->core.attr.type; 1941 u64 e_config = evsel->core.attr.config; 1942 1943 if (e_type != type) { 1944 return type == PERF_TYPE_HARDWARE && evsel->pmu && evsel->pmu->is_core && 1945 evsel->alternate_hw_config == config; 1946 } 1947 1948 if ((type == PERF_TYPE_HARDWARE || type == PERF_TYPE_HW_CACHE) && 1949 perf_pmus__supports_extended_type()) 1950 e_config &= PERF_HW_EVENT_MASK; 1951 1952 return e_config == config; 1953 } 1954 1955 int evsel__read_counter(struct evsel *evsel, int cpu_map_idx, int thread) 1956 { 1957 if (evsel__is_tool(evsel)) 1958 return evsel__tool_pmu_read(evsel, cpu_map_idx, thread); 1959 1960 if (evsel__is_hwmon(evsel)) 1961 return evsel__hwmon_pmu_read(evsel, cpu_map_idx, thread); 1962 1963 if (evsel__is_drm(evsel)) 1964 return evsel__drm_pmu_read(evsel, cpu_map_idx, thread); 1965 1966 if (evsel__is_retire_lat(evsel)) 1967 return evsel__tpebs_read(evsel, cpu_map_idx, thread); 1968 1969 if (evsel->core.attr.read_format & PERF_FORMAT_GROUP) 1970 return evsel__read_group(evsel, cpu_map_idx, thread); 1971 1972 return evsel__read_one(evsel, cpu_map_idx, thread); 1973 } 1974 1975 int __evsel__read_on_cpu(struct evsel *evsel, int cpu_map_idx, int thread, bool scale) 1976 { 1977 struct perf_counts_values count; 1978 size_t nv = scale ? 3 : 1; 1979 1980 if (FD(evsel, cpu_map_idx, thread) < 0) 1981 return -EINVAL; 1982 1983 if (evsel->counts == NULL && evsel__alloc_counts(evsel) < 0) 1984 return -ENOMEM; 1985 1986 if (readn(FD(evsel, cpu_map_idx, thread), &count, nv * sizeof(u64)) <= 0) 1987 return -errno; 1988 1989 evsel__compute_deltas(evsel, cpu_map_idx, thread, &count); 1990 perf_counts_values__scale(&count, scale, NULL); 1991 *perf_counts(evsel->counts, cpu_map_idx, thread) = count; 1992 return 0; 1993 } 1994 1995 static int evsel__match_other_cpu(struct evsel *evsel, struct evsel *other, 1996 int cpu_map_idx) 1997 { 1998 struct perf_cpu cpu; 1999 2000 cpu = perf_cpu_map__cpu(evsel->core.cpus, cpu_map_idx); 2001 return perf_cpu_map__idx(other->core.cpus, cpu); 2002 } 2003 2004 static int evsel__hybrid_group_cpu_map_idx(struct evsel *evsel, int cpu_map_idx) 2005 { 2006 struct evsel *leader = evsel__leader(evsel); 2007 2008 if ((evsel__is_hybrid(evsel) && !evsel__is_hybrid(leader)) || 2009 (!evsel__is_hybrid(evsel) && evsel__is_hybrid(leader))) { 2010 return evsel__match_other_cpu(evsel, leader, cpu_map_idx); 2011 } 2012 2013 return cpu_map_idx; 2014 } 2015 2016 static int get_group_fd(struct evsel *evsel, int cpu_map_idx, int thread) 2017 { 2018 struct evsel *leader = evsel__leader(evsel); 2019 int fd; 2020 2021 if (!evsel->supported || evsel__is_group_leader(evsel)) 2022 return -1; 2023 2024 /* 2025 * Leader must be already processed/open, 2026 * if not it's a bug. 2027 */ 2028 BUG_ON(!leader->core.fd); 2029 2030 cpu_map_idx = evsel__hybrid_group_cpu_map_idx(evsel, cpu_map_idx); 2031 if (cpu_map_idx == -1) 2032 return -1; 2033 2034 fd = FD(leader, cpu_map_idx, thread); 2035 BUG_ON(fd == -1 && leader->supported); 2036 2037 /* 2038 * When the leader has been skipped, return -2 to distinguish from no 2039 * group leader case. 2040 */ 2041 return fd == -1 ? -2 : fd; 2042 } 2043 2044 static void evsel__remove_fd(struct evsel *pos, int nr_cpus, int nr_threads, int thread_idx) 2045 { 2046 for (int cpu = 0; cpu < nr_cpus; cpu++) 2047 for (int thread = thread_idx; thread < nr_threads - 1; thread++) 2048 FD(pos, cpu, thread) = FD(pos, cpu, thread + 1); 2049 } 2050 2051 static int update_fds(struct evsel *evsel, 2052 int nr_cpus, int cpu_map_idx, 2053 int nr_threads, int thread_idx) 2054 { 2055 struct evsel *pos; 2056 2057 if (cpu_map_idx >= nr_cpus || thread_idx >= nr_threads) 2058 return -EINVAL; 2059 2060 evlist__for_each_entry(evsel->evlist, pos) { 2061 nr_cpus = pos != evsel ? nr_cpus : cpu_map_idx; 2062 2063 evsel__remove_fd(pos, nr_cpus, nr_threads, thread_idx); 2064 2065 /* 2066 * Since fds for next evsel has not been created, 2067 * there is no need to iterate whole event list. 2068 */ 2069 if (pos == evsel) 2070 break; 2071 } 2072 return 0; 2073 } 2074 2075 static bool evsel__ignore_missing_thread(struct evsel *evsel, 2076 int nr_cpus, int cpu_map_idx, 2077 struct perf_thread_map *threads, 2078 int thread, int err) 2079 { 2080 pid_t ignore_pid = perf_thread_map__pid(threads, thread); 2081 2082 if (!evsel->ignore_missing_thread) 2083 return false; 2084 2085 /* The system wide setup does not work with threads. */ 2086 if (evsel->core.system_wide) 2087 return false; 2088 2089 /* The -ESRCH is perf event syscall errno for pid's not found. */ 2090 if (err != -ESRCH) 2091 return false; 2092 2093 /* If there's only one thread, let it fail. */ 2094 if (threads->nr == 1) 2095 return false; 2096 2097 /* 2098 * We should remove fd for missing_thread first 2099 * because thread_map__remove() will decrease threads->nr. 2100 */ 2101 if (update_fds(evsel, nr_cpus, cpu_map_idx, threads->nr, thread)) 2102 return false; 2103 2104 if (thread_map__remove(threads, thread)) 2105 return false; 2106 2107 pr_warning("WARNING: Ignored open failure for pid %d\n", 2108 ignore_pid); 2109 return true; 2110 } 2111 2112 static int __open_attr__fprintf(FILE *fp, const char *name, const char *val, 2113 void *priv __maybe_unused) 2114 { 2115 return fprintf(fp, " %-32s %s\n", name, val); 2116 } 2117 2118 static void display_attr(struct perf_event_attr *attr) 2119 { 2120 if (verbose >= 2 || debug_peo_args) { 2121 fprintf(stderr, "%.60s\n", graph_dotted_line); 2122 fprintf(stderr, "perf_event_attr:\n"); 2123 perf_event_attr__fprintf(stderr, attr, __open_attr__fprintf, NULL); 2124 fprintf(stderr, "%.60s\n", graph_dotted_line); 2125 } 2126 } 2127 2128 bool evsel__precise_ip_fallback(struct evsel *evsel) 2129 { 2130 /* Do not try less precise if not requested. */ 2131 if (!evsel->precise_max) 2132 return false; 2133 2134 /* 2135 * We tried all the precise_ip values, and it's 2136 * still failing, so leave it to standard fallback. 2137 */ 2138 if (!evsel->core.attr.precise_ip) { 2139 evsel->core.attr.precise_ip = evsel->precise_ip_original; 2140 return false; 2141 } 2142 2143 if (!evsel->precise_ip_original) 2144 evsel->precise_ip_original = evsel->core.attr.precise_ip; 2145 2146 evsel->core.attr.precise_ip--; 2147 pr_debug2_peo("decreasing precise_ip by one (%d)\n", evsel->core.attr.precise_ip); 2148 display_attr(&evsel->core.attr); 2149 return true; 2150 } 2151 2152 static struct perf_cpu_map *empty_cpu_map; 2153 static struct perf_thread_map *empty_thread_map; 2154 2155 static int __evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus, 2156 struct perf_thread_map *threads) 2157 { 2158 int ret = 0; 2159 int nthreads = perf_thread_map__nr(threads); 2160 2161 if ((perf_missing_features.write_backward && evsel->core.attr.write_backward) || 2162 (perf_missing_features.aux_output && evsel->core.attr.aux_output)) 2163 return -EINVAL; 2164 2165 if (cpus == NULL) { 2166 if (empty_cpu_map == NULL) { 2167 empty_cpu_map = perf_cpu_map__new_any_cpu(); 2168 if (empty_cpu_map == NULL) 2169 return -ENOMEM; 2170 } 2171 2172 cpus = empty_cpu_map; 2173 } 2174 2175 if (threads == NULL) { 2176 if (empty_thread_map == NULL) { 2177 empty_thread_map = thread_map__new_by_tid(-1); 2178 if (empty_thread_map == NULL) 2179 return -ENOMEM; 2180 } 2181 2182 threads = empty_thread_map; 2183 } 2184 2185 if (evsel->core.fd == NULL && 2186 perf_evsel__alloc_fd(&evsel->core, perf_cpu_map__nr(cpus), nthreads) < 0) 2187 return -ENOMEM; 2188 2189 if (evsel__is_tool(evsel)) 2190 ret = evsel__tool_pmu_prepare_open(evsel, cpus, nthreads); 2191 2192 evsel->open_flags = PERF_FLAG_FD_CLOEXEC; 2193 if (evsel->cgrp) 2194 evsel->open_flags |= PERF_FLAG_PID_CGROUP; 2195 2196 return ret; 2197 } 2198 2199 static void evsel__disable_missing_features(struct evsel *evsel) 2200 { 2201 if (perf_missing_features.inherit_sample_read && evsel->core.attr.inherit && 2202 (evsel->core.attr.sample_type & PERF_SAMPLE_READ)) 2203 evsel->core.attr.inherit = 0; 2204 if (perf_missing_features.branch_counters) 2205 evsel->core.attr.branch_sample_type &= ~PERF_SAMPLE_BRANCH_COUNTERS; 2206 if (perf_missing_features.read_lost) 2207 evsel->core.attr.read_format &= ~PERF_FORMAT_LOST; 2208 if (perf_missing_features.weight_struct) { 2209 evsel__set_sample_bit(evsel, WEIGHT); 2210 evsel__reset_sample_bit(evsel, WEIGHT_STRUCT); 2211 } 2212 if (perf_missing_features.clockid_wrong) 2213 evsel->core.attr.clockid = CLOCK_MONOTONIC; /* should always work */ 2214 if (perf_missing_features.clockid) { 2215 evsel->core.attr.use_clockid = 0; 2216 evsel->core.attr.clockid = 0; 2217 } 2218 if (perf_missing_features.cloexec) 2219 evsel->open_flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC; 2220 if (perf_missing_features.mmap2) 2221 evsel->core.attr.mmap2 = 0; 2222 if (evsel->pmu && evsel->pmu->missing_features.exclude_guest) 2223 evsel->core.attr.exclude_guest = evsel->core.attr.exclude_host = 0; 2224 if (perf_missing_features.lbr_flags) 2225 evsel->core.attr.branch_sample_type &= ~(PERF_SAMPLE_BRANCH_NO_FLAGS | 2226 PERF_SAMPLE_BRANCH_NO_CYCLES); 2227 if (perf_missing_features.group_read && evsel->core.attr.inherit) 2228 evsel->core.attr.read_format &= ~(PERF_FORMAT_GROUP|PERF_FORMAT_ID); 2229 if (perf_missing_features.ksymbol) 2230 evsel->core.attr.ksymbol = 0; 2231 if (perf_missing_features.bpf) 2232 evsel->core.attr.bpf_event = 0; 2233 if (perf_missing_features.branch_hw_idx) 2234 evsel->core.attr.branch_sample_type &= ~PERF_SAMPLE_BRANCH_HW_INDEX; 2235 if (perf_missing_features.sample_id_all) 2236 evsel->core.attr.sample_id_all = 0; 2237 } 2238 2239 int evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus, 2240 struct perf_thread_map *threads) 2241 { 2242 int err; 2243 2244 err = __evsel__prepare_open(evsel, cpus, threads); 2245 if (err) 2246 return err; 2247 2248 evsel__disable_missing_features(evsel); 2249 2250 return err; 2251 } 2252 2253 static bool __has_attr_feature(struct perf_event_attr *attr, 2254 struct perf_cpu cpu, unsigned long flags) 2255 { 2256 int fd = syscall(SYS_perf_event_open, attr, /*pid=*/0, cpu.cpu, 2257 /*group_fd=*/-1, flags); 2258 close(fd); 2259 2260 if (fd < 0) { 2261 attr->exclude_kernel = 1; 2262 2263 fd = syscall(SYS_perf_event_open, attr, /*pid=*/0, cpu.cpu, 2264 /*group_fd=*/-1, flags); 2265 close(fd); 2266 } 2267 2268 if (fd < 0) { 2269 attr->exclude_hv = 1; 2270 2271 fd = syscall(SYS_perf_event_open, attr, /*pid=*/0, cpu.cpu, 2272 /*group_fd=*/-1, flags); 2273 close(fd); 2274 } 2275 2276 if (fd < 0) { 2277 attr->exclude_guest = 1; 2278 2279 fd = syscall(SYS_perf_event_open, attr, /*pid=*/0, cpu.cpu, 2280 /*group_fd=*/-1, flags); 2281 close(fd); 2282 } 2283 2284 attr->exclude_kernel = 0; 2285 attr->exclude_guest = 0; 2286 attr->exclude_hv = 0; 2287 2288 return fd >= 0; 2289 } 2290 2291 static bool has_attr_feature(struct perf_event_attr *attr, unsigned long flags) 2292 { 2293 struct perf_cpu cpu = {.cpu = -1}; 2294 2295 return __has_attr_feature(attr, cpu, flags); 2296 } 2297 2298 static void evsel__detect_missing_pmu_features(struct evsel *evsel) 2299 { 2300 struct perf_event_attr attr = { 2301 .type = evsel->core.attr.type, 2302 .config = evsel->core.attr.config, 2303 .disabled = 1, 2304 }; 2305 struct perf_pmu *pmu = evsel->pmu; 2306 int old_errno; 2307 2308 old_errno = errno; 2309 2310 if (pmu == NULL) 2311 pmu = evsel->pmu = evsel__find_pmu(evsel); 2312 2313 if (pmu == NULL || pmu->missing_features.checked) 2314 goto out; 2315 2316 /* 2317 * Must probe features in the order they were added to the 2318 * perf_event_attr interface. These are kernel core limitation but 2319 * specific to PMUs with branch stack. So we can detect with the given 2320 * hardware event and stop on the first one succeeded. 2321 */ 2322 2323 /* Please add new feature detection here. */ 2324 2325 attr.exclude_guest = 1; 2326 if (has_attr_feature(&attr, /*flags=*/0)) 2327 goto found; 2328 pmu->missing_features.exclude_guest = true; 2329 pr_debug2("switching off exclude_guest for PMU %s\n", pmu->name); 2330 2331 found: 2332 pmu->missing_features.checked = true; 2333 out: 2334 errno = old_errno; 2335 } 2336 2337 static void evsel__detect_missing_brstack_features(struct evsel *evsel) 2338 { 2339 static bool detection_done = false; 2340 struct perf_event_attr attr = { 2341 .type = evsel->core.attr.type, 2342 .config = evsel->core.attr.config, 2343 .disabled = 1, 2344 .sample_type = PERF_SAMPLE_BRANCH_STACK, 2345 .sample_period = 1000, 2346 }; 2347 int old_errno; 2348 2349 if (detection_done) 2350 return; 2351 2352 old_errno = errno; 2353 2354 /* 2355 * Must probe features in the order they were added to the 2356 * perf_event_attr interface. These are PMU specific limitation 2357 * so we can detect with the given hardware event and stop on the 2358 * first one succeeded. 2359 */ 2360 2361 /* Please add new feature detection here. */ 2362 2363 attr.branch_sample_type = PERF_SAMPLE_BRANCH_COUNTERS; 2364 if (has_attr_feature(&attr, /*flags=*/0)) 2365 goto found; 2366 perf_missing_features.branch_counters = true; 2367 pr_debug2("switching off branch counters support\n"); 2368 2369 attr.branch_sample_type = PERF_SAMPLE_BRANCH_HW_INDEX; 2370 if (has_attr_feature(&attr, /*flags=*/0)) 2371 goto found; 2372 perf_missing_features.branch_hw_idx = true; 2373 pr_debug2("switching off branch HW index support\n"); 2374 2375 attr.branch_sample_type = PERF_SAMPLE_BRANCH_NO_CYCLES | PERF_SAMPLE_BRANCH_NO_FLAGS; 2376 if (has_attr_feature(&attr, /*flags=*/0)) 2377 goto found; 2378 perf_missing_features.lbr_flags = true; 2379 pr_debug2_peo("switching off branch sample type no (cycles/flags)\n"); 2380 2381 found: 2382 detection_done = true; 2383 errno = old_errno; 2384 } 2385 2386 static bool evsel__probe_aux_action(struct evsel *evsel, struct perf_cpu cpu) 2387 { 2388 struct perf_event_attr attr = evsel->core.attr; 2389 int old_errno = errno; 2390 2391 attr.disabled = 1; 2392 attr.aux_start_paused = 1; 2393 2394 if (__has_attr_feature(&attr, cpu, /*flags=*/0)) { 2395 errno = old_errno; 2396 return true; 2397 } 2398 2399 /* 2400 * EOPNOTSUPP means the kernel supports the feature but the PMU does 2401 * not, so keep that distinction if possible. 2402 */ 2403 if (errno != EOPNOTSUPP) 2404 errno = old_errno; 2405 2406 return false; 2407 } 2408 2409 static void evsel__detect_missing_aux_action_feature(struct evsel *evsel, struct perf_cpu cpu) 2410 { 2411 static bool detection_done; 2412 struct evsel *leader; 2413 2414 /* 2415 * Don't bother probing aux_action if it is not being used or has been 2416 * probed before. 2417 */ 2418 if (!evsel->core.attr.aux_action || detection_done) 2419 return; 2420 2421 detection_done = true; 2422 2423 /* 2424 * The leader is an AUX area event. If it has failed, assume the feature 2425 * is not supported. 2426 */ 2427 leader = evsel__leader(evsel); 2428 if (evsel == leader) { 2429 perf_missing_features.aux_action = true; 2430 return; 2431 } 2432 2433 /* 2434 * AUX area event with aux_action must have been opened successfully 2435 * already, so feature is supported. 2436 */ 2437 if (leader->core.attr.aux_action) 2438 return; 2439 2440 if (!evsel__probe_aux_action(leader, cpu)) 2441 perf_missing_features.aux_action = true; 2442 } 2443 2444 static bool evsel__detect_missing_features(struct evsel *evsel, struct perf_cpu cpu) 2445 { 2446 static bool detection_done = false; 2447 struct perf_event_attr attr = { 2448 .type = PERF_TYPE_SOFTWARE, 2449 .config = PERF_COUNT_SW_TASK_CLOCK, 2450 .disabled = 1, 2451 }; 2452 int old_errno; 2453 2454 evsel__detect_missing_aux_action_feature(evsel, cpu); 2455 2456 evsel__detect_missing_pmu_features(evsel); 2457 2458 if (evsel__has_br_stack(evsel)) 2459 evsel__detect_missing_brstack_features(evsel); 2460 2461 if (detection_done) 2462 goto check; 2463 2464 old_errno = errno; 2465 2466 /* 2467 * Must probe features in the order they were added to the 2468 * perf_event_attr interface. These are kernel core limitation 2469 * not PMU-specific so we can detect with a software event and 2470 * stop on the first one succeeded. 2471 */ 2472 2473 /* Please add new feature detection here. */ 2474 2475 attr.inherit = true; 2476 attr.sample_type = PERF_SAMPLE_READ; 2477 if (has_attr_feature(&attr, /*flags=*/0)) 2478 goto found; 2479 perf_missing_features.inherit_sample_read = true; 2480 pr_debug2("Using PERF_SAMPLE_READ / :S modifier is not compatible with inherit, falling back to no-inherit.\n"); 2481 attr.inherit = false; 2482 attr.sample_type = 0; 2483 2484 attr.read_format = PERF_FORMAT_LOST; 2485 if (has_attr_feature(&attr, /*flags=*/0)) 2486 goto found; 2487 perf_missing_features.read_lost = true; 2488 pr_debug2("switching off PERF_FORMAT_LOST support\n"); 2489 attr.read_format = 0; 2490 2491 attr.sample_type = PERF_SAMPLE_WEIGHT_STRUCT; 2492 if (has_attr_feature(&attr, /*flags=*/0)) 2493 goto found; 2494 perf_missing_features.weight_struct = true; 2495 pr_debug2("switching off weight struct support\n"); 2496 attr.sample_type = 0; 2497 2498 attr.sample_type = PERF_SAMPLE_CODE_PAGE_SIZE; 2499 if (has_attr_feature(&attr, /*flags=*/0)) 2500 goto found; 2501 perf_missing_features.code_page_size = true; 2502 pr_debug2_peo("Kernel has no PERF_SAMPLE_CODE_PAGE_SIZE support\n"); 2503 attr.sample_type = 0; 2504 2505 attr.sample_type = PERF_SAMPLE_DATA_PAGE_SIZE; 2506 if (has_attr_feature(&attr, /*flags=*/0)) 2507 goto found; 2508 perf_missing_features.data_page_size = true; 2509 pr_debug2_peo("Kernel has no PERF_SAMPLE_DATA_PAGE_SIZE support\n"); 2510 attr.sample_type = 0; 2511 2512 attr.cgroup = 1; 2513 if (has_attr_feature(&attr, /*flags=*/0)) 2514 goto found; 2515 perf_missing_features.cgroup = true; 2516 pr_debug2_peo("Kernel has no cgroup sampling support\n"); 2517 attr.cgroup = 0; 2518 2519 attr.aux_output = 1; 2520 if (has_attr_feature(&attr, /*flags=*/0)) 2521 goto found; 2522 perf_missing_features.aux_output = true; 2523 pr_debug2_peo("Kernel has no attr.aux_output support\n"); 2524 attr.aux_output = 0; 2525 2526 attr.bpf_event = 1; 2527 if (has_attr_feature(&attr, /*flags=*/0)) 2528 goto found; 2529 perf_missing_features.bpf = true; 2530 pr_debug2_peo("switching off bpf_event\n"); 2531 attr.bpf_event = 0; 2532 2533 attr.ksymbol = 1; 2534 if (has_attr_feature(&attr, /*flags=*/0)) 2535 goto found; 2536 perf_missing_features.ksymbol = true; 2537 pr_debug2_peo("switching off ksymbol\n"); 2538 attr.ksymbol = 0; 2539 2540 attr.write_backward = 1; 2541 if (has_attr_feature(&attr, /*flags=*/0)) 2542 goto found; 2543 perf_missing_features.write_backward = true; 2544 pr_debug2_peo("switching off write_backward\n"); 2545 attr.write_backward = 0; 2546 2547 attr.use_clockid = 1; 2548 attr.clockid = CLOCK_MONOTONIC; 2549 if (has_attr_feature(&attr, /*flags=*/0)) 2550 goto found; 2551 perf_missing_features.clockid = true; 2552 pr_debug2_peo("switching off clockid\n"); 2553 attr.use_clockid = 0; 2554 attr.clockid = 0; 2555 2556 if (has_attr_feature(&attr, /*flags=*/PERF_FLAG_FD_CLOEXEC)) 2557 goto found; 2558 perf_missing_features.cloexec = true; 2559 pr_debug2_peo("switching off cloexec flag\n"); 2560 2561 attr.mmap2 = 1; 2562 if (has_attr_feature(&attr, /*flags=*/0)) 2563 goto found; 2564 perf_missing_features.mmap2 = true; 2565 pr_debug2_peo("switching off mmap2\n"); 2566 attr.mmap2 = 0; 2567 2568 /* set this unconditionally? */ 2569 perf_missing_features.sample_id_all = true; 2570 pr_debug2_peo("switching off sample_id_all\n"); 2571 2572 attr.inherit = 1; 2573 attr.read_format = PERF_FORMAT_GROUP; 2574 if (has_attr_feature(&attr, /*flags=*/0)) 2575 goto found; 2576 perf_missing_features.group_read = true; 2577 pr_debug2_peo("switching off group read\n"); 2578 attr.inherit = 0; 2579 attr.read_format = 0; 2580 2581 found: 2582 detection_done = true; 2583 errno = old_errno; 2584 2585 check: 2586 if (evsel->core.attr.inherit && 2587 (evsel->core.attr.sample_type & PERF_SAMPLE_READ) && 2588 perf_missing_features.inherit_sample_read) 2589 return true; 2590 2591 if ((evsel->core.attr.branch_sample_type & PERF_SAMPLE_BRANCH_COUNTERS) && 2592 perf_missing_features.branch_counters) 2593 return true; 2594 2595 if ((evsel->core.attr.read_format & PERF_FORMAT_LOST) && 2596 perf_missing_features.read_lost) 2597 return true; 2598 2599 if ((evsel->core.attr.sample_type & PERF_SAMPLE_WEIGHT_STRUCT) && 2600 perf_missing_features.weight_struct) 2601 return true; 2602 2603 if (evsel->core.attr.use_clockid && evsel->core.attr.clockid != CLOCK_MONOTONIC && 2604 !perf_missing_features.clockid) { 2605 perf_missing_features.clockid_wrong = true; 2606 return true; 2607 } 2608 2609 if (evsel->core.attr.use_clockid && perf_missing_features.clockid) 2610 return true; 2611 2612 if ((evsel->open_flags & PERF_FLAG_FD_CLOEXEC) && 2613 perf_missing_features.cloexec) 2614 return true; 2615 2616 if (evsel->core.attr.mmap2 && perf_missing_features.mmap2) 2617 return true; 2618 2619 if ((evsel->core.attr.branch_sample_type & (PERF_SAMPLE_BRANCH_NO_FLAGS | 2620 PERF_SAMPLE_BRANCH_NO_CYCLES)) && 2621 perf_missing_features.lbr_flags) 2622 return true; 2623 2624 if (evsel->core.attr.inherit && (evsel->core.attr.read_format & PERF_FORMAT_GROUP) && 2625 perf_missing_features.group_read) 2626 return true; 2627 2628 if (evsel->core.attr.ksymbol && perf_missing_features.ksymbol) 2629 return true; 2630 2631 if (evsel->core.attr.bpf_event && perf_missing_features.bpf) 2632 return true; 2633 2634 if ((evsel->core.attr.branch_sample_type & PERF_SAMPLE_BRANCH_HW_INDEX) && 2635 perf_missing_features.branch_hw_idx) 2636 return true; 2637 2638 if (evsel->core.attr.sample_id_all && perf_missing_features.sample_id_all) 2639 return true; 2640 2641 return false; 2642 } 2643 2644 static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus, 2645 struct perf_thread_map *threads, 2646 int start_cpu_map_idx, int end_cpu_map_idx) 2647 { 2648 int idx, thread, nthreads; 2649 int pid = -1, err, old_errno; 2650 enum rlimit_action set_rlimit = NO_CHANGE; 2651 struct perf_cpu cpu; 2652 2653 if (evsel__is_retire_lat(evsel)) { 2654 err = evsel__tpebs_open(evsel); 2655 goto out; 2656 } 2657 2658 err = __evsel__prepare_open(evsel, cpus, threads); 2659 if (err) 2660 goto out; 2661 2662 if (cpus == NULL) 2663 cpus = empty_cpu_map; 2664 2665 if (threads == NULL) 2666 threads = empty_thread_map; 2667 2668 nthreads = perf_thread_map__nr(threads); 2669 2670 if (evsel->cgrp) 2671 pid = evsel->cgrp->fd; 2672 2673 fallback_missing_features: 2674 evsel__disable_missing_features(evsel); 2675 2676 pr_debug3("Opening: %s\n", evsel__name(evsel)); 2677 display_attr(&evsel->core.attr); 2678 2679 if (evsel__is_tool(evsel)) { 2680 err = evsel__tool_pmu_open(evsel, threads, 2681 start_cpu_map_idx, 2682 end_cpu_map_idx); 2683 goto out; 2684 } 2685 if (evsel__is_hwmon(evsel)) { 2686 err = evsel__hwmon_pmu_open(evsel, threads, 2687 start_cpu_map_idx, 2688 end_cpu_map_idx); 2689 goto out; 2690 } 2691 if (evsel__is_drm(evsel)) { 2692 err = evsel__drm_pmu_open(evsel, threads, 2693 start_cpu_map_idx, 2694 end_cpu_map_idx); 2695 goto out; 2696 } 2697 2698 for (idx = start_cpu_map_idx; idx < end_cpu_map_idx; idx++) { 2699 cpu = perf_cpu_map__cpu(cpus, idx); 2700 2701 for (thread = 0; thread < nthreads; thread++) { 2702 int fd, group_fd; 2703 retry_open: 2704 if (thread >= nthreads) 2705 break; 2706 2707 if (!evsel->cgrp && !evsel->core.system_wide) 2708 pid = perf_thread_map__pid(threads, thread); 2709 2710 group_fd = get_group_fd(evsel, idx, thread); 2711 2712 if (group_fd == -2) { 2713 pr_debug("broken group leader for %s\n", evsel->name); 2714 err = -EINVAL; 2715 goto out_close; 2716 } 2717 2718 /* Debug message used by test scripts */ 2719 pr_debug2_peo("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx", 2720 pid, cpu.cpu, group_fd, evsel->open_flags); 2721 2722 fd = sys_perf_event_open(&evsel->core.attr, pid, cpu.cpu, 2723 group_fd, evsel->open_flags); 2724 2725 FD(evsel, idx, thread) = fd; 2726 2727 if (fd < 0) { 2728 err = -errno; 2729 2730 pr_debug2_peo("\nsys_perf_event_open failed, error %d\n", 2731 err); 2732 goto try_fallback; 2733 } 2734 2735 bpf_counter__install_pe(evsel, idx, fd); 2736 2737 if (unlikely(test_attr__enabled())) { 2738 test_attr__open(&evsel->core.attr, pid, cpu, 2739 fd, group_fd, evsel->open_flags); 2740 } 2741 2742 /* Debug message used by test scripts */ 2743 pr_debug2_peo(" = %d\n", fd); 2744 2745 if (evsel->bpf_fd >= 0) { 2746 int evt_fd = fd; 2747 int bpf_fd = evsel->bpf_fd; 2748 2749 err = ioctl(evt_fd, 2750 PERF_EVENT_IOC_SET_BPF, 2751 bpf_fd); 2752 if (err && errno != EEXIST) { 2753 pr_err("failed to attach bpf fd %d: %s\n", 2754 bpf_fd, strerror(errno)); 2755 err = -EINVAL; 2756 goto out_close; 2757 } 2758 } 2759 2760 set_rlimit = NO_CHANGE; 2761 2762 /* 2763 * If we succeeded but had to kill clockid, fail and 2764 * have evsel__open_strerror() print us a nice error. 2765 */ 2766 if (perf_missing_features.clockid || 2767 perf_missing_features.clockid_wrong) { 2768 err = -EINVAL; 2769 goto out_close; 2770 } 2771 } 2772 } 2773 2774 err = 0; 2775 goto out; 2776 2777 try_fallback: 2778 if (evsel__ignore_missing_thread(evsel, perf_cpu_map__nr(cpus), 2779 idx, threads, thread, err)) { 2780 /* We just removed 1 thread, so lower the upper nthreads limit. */ 2781 nthreads--; 2782 2783 /* ... and pretend like nothing have happened. */ 2784 err = 0; 2785 goto retry_open; 2786 } 2787 /* 2788 * perf stat needs between 5 and 22 fds per CPU. When we run out 2789 * of them try to increase the limits. 2790 */ 2791 if (err == -EMFILE && rlimit__increase_nofile(&set_rlimit)) 2792 goto retry_open; 2793 2794 if (err == -EINVAL && evsel__detect_missing_features(evsel, cpu)) 2795 goto fallback_missing_features; 2796 2797 if (evsel__precise_ip_fallback(evsel)) 2798 goto retry_open; 2799 2800 out_close: 2801 if (err) 2802 threads->err_thread = thread; 2803 2804 old_errno = errno; 2805 do { 2806 while (--thread >= 0) { 2807 if (FD(evsel, idx, thread) >= 0) 2808 close(FD(evsel, idx, thread)); 2809 FD(evsel, idx, thread) = -1; 2810 } 2811 thread = nthreads; 2812 } while (--idx >= 0); 2813 errno = old_errno; 2814 out: 2815 if (err) 2816 evsel->supported = false; 2817 return err; 2818 } 2819 2820 int evsel__open(struct evsel *evsel, struct perf_cpu_map *cpus, 2821 struct perf_thread_map *threads) 2822 { 2823 return evsel__open_cpu(evsel, cpus, threads, 0, perf_cpu_map__nr(cpus)); 2824 } 2825 2826 void evsel__close(struct evsel *evsel) 2827 { 2828 if (evsel__is_retire_lat(evsel)) 2829 evsel__tpebs_close(evsel); 2830 perf_evsel__close(&evsel->core); 2831 perf_evsel__free_id(&evsel->core); 2832 } 2833 2834 int evsel__open_per_cpu_and_thread(struct evsel *evsel, 2835 struct perf_cpu_map *cpus, int cpu_map_idx, 2836 struct perf_thread_map *threads) 2837 { 2838 if (cpu_map_idx == -1) 2839 return evsel__open_cpu(evsel, cpus, threads, 0, perf_cpu_map__nr(cpus)); 2840 2841 return evsel__open_cpu(evsel, cpus, threads, cpu_map_idx, cpu_map_idx + 1); 2842 } 2843 2844 int evsel__open_per_cpu(struct evsel *evsel, struct perf_cpu_map *cpus, int cpu_map_idx) 2845 { 2846 struct perf_thread_map *threads = thread_map__new_by_tid(-1); 2847 int ret = evsel__open_per_cpu_and_thread(evsel, cpus, cpu_map_idx, threads); 2848 2849 perf_thread_map__put(threads); 2850 return ret; 2851 } 2852 2853 int evsel__open_per_thread(struct evsel *evsel, struct perf_thread_map *threads) 2854 { 2855 struct perf_cpu_map *cpus = perf_cpu_map__new_any_cpu(); 2856 int ret = evsel__open_per_cpu_and_thread(evsel, cpus, -1, threads); 2857 2858 perf_cpu_map__put(cpus); 2859 return ret; 2860 } 2861 2862 static int perf_evsel__parse_id_sample(const struct evsel *evsel, 2863 const union perf_event *event, 2864 struct perf_sample *sample) 2865 { 2866 u64 type = evsel->core.attr.sample_type; 2867 const __u64 *array = event->sample.array; 2868 bool swapped = evsel->needs_swap; 2869 union u64_swap u; 2870 2871 array += ((event->header.size - 2872 sizeof(event->header)) / sizeof(u64)) - 1; 2873 2874 if (type & PERF_SAMPLE_IDENTIFIER) { 2875 sample->id = *array; 2876 array--; 2877 } 2878 2879 if (type & PERF_SAMPLE_CPU) { 2880 u.val64 = *array; 2881 if (swapped) { 2882 /* undo swap of u64, then swap on individual u32s */ 2883 u.val64 = bswap_64(u.val64); 2884 u.val32[0] = bswap_32(u.val32[0]); 2885 } 2886 2887 sample->cpu = u.val32[0]; 2888 array--; 2889 } 2890 2891 if (type & PERF_SAMPLE_STREAM_ID) { 2892 sample->stream_id = *array; 2893 array--; 2894 } 2895 2896 if (type & PERF_SAMPLE_ID) { 2897 sample->id = *array; 2898 array--; 2899 } 2900 2901 if (type & PERF_SAMPLE_TIME) { 2902 sample->time = *array; 2903 array--; 2904 } 2905 2906 if (type & PERF_SAMPLE_TID) { 2907 u.val64 = *array; 2908 if (swapped) { 2909 /* undo swap of u64, then swap on individual u32s */ 2910 u.val64 = bswap_64(u.val64); 2911 u.val32[0] = bswap_32(u.val32[0]); 2912 u.val32[1] = bswap_32(u.val32[1]); 2913 } 2914 2915 sample->pid = u.val32[0]; 2916 sample->tid = u.val32[1]; 2917 array--; 2918 } 2919 2920 return 0; 2921 } 2922 2923 static inline bool overflow(const void *endp, u16 max_size, const void *offset, 2924 u64 size) 2925 { 2926 return size > max_size || offset + size > endp; 2927 } 2928 2929 #define OVERFLOW_CHECK(offset, size, max_size) \ 2930 do { \ 2931 if (overflow(endp, (max_size), (offset), (size))) \ 2932 return -EFAULT; \ 2933 } while (0) 2934 2935 #define OVERFLOW_CHECK_u64(offset) \ 2936 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64)) 2937 2938 static int 2939 perf_event__check_size(union perf_event *event, unsigned int sample_size) 2940 { 2941 /* 2942 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes 2943 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to 2944 * check the format does not go past the end of the event. 2945 */ 2946 if (sample_size + sizeof(event->header) > event->header.size) 2947 return -EFAULT; 2948 2949 return 0; 2950 } 2951 2952 static void perf_parse_sample_weight(struct perf_sample *data, const __u64 *array, u64 type) 2953 { 2954 union perf_sample_weight weight; 2955 2956 weight.full = *array; 2957 if (type & PERF_SAMPLE_WEIGHT_STRUCT) { 2958 data->weight = weight.var1_dw; 2959 data->ins_lat = weight.var2_w; 2960 data->weight3 = weight.var3_w; 2961 } else { 2962 data->weight = weight.full; 2963 } 2964 } 2965 2966 u64 evsel__bitfield_swap_branch_flags(u64 value) 2967 { 2968 u64 new_val = 0; 2969 2970 /* 2971 * branch_flags 2972 * union { 2973 * u64 values; 2974 * struct { 2975 * mispred:1 //target mispredicted 2976 * predicted:1 //target predicted 2977 * in_tx:1 //in transaction 2978 * abort:1 //transaction abort 2979 * cycles:16 //cycle count to last branch 2980 * type:4 //branch type 2981 * spec:2 //branch speculation info 2982 * new_type:4 //additional branch type 2983 * priv:3 //privilege level 2984 * reserved:31 2985 * } 2986 * } 2987 * 2988 * Avoid bswap64() the entire branch_flag.value, 2989 * as it has variable bit-field sizes. Instead the 2990 * macro takes the bit-field position/size, 2991 * swaps it based on the host endianness. 2992 */ 2993 if (host_is_bigendian()) { 2994 new_val = bitfield_swap(value, 0, 1); 2995 new_val |= bitfield_swap(value, 1, 1); 2996 new_val |= bitfield_swap(value, 2, 1); 2997 new_val |= bitfield_swap(value, 3, 1); 2998 new_val |= bitfield_swap(value, 4, 16); 2999 new_val |= bitfield_swap(value, 20, 4); 3000 new_val |= bitfield_swap(value, 24, 2); 3001 new_val |= bitfield_swap(value, 26, 4); 3002 new_val |= bitfield_swap(value, 30, 3); 3003 new_val |= bitfield_swap(value, 33, 31); 3004 } else { 3005 new_val = bitfield_swap(value, 63, 1); 3006 new_val |= bitfield_swap(value, 62, 1); 3007 new_val |= bitfield_swap(value, 61, 1); 3008 new_val |= bitfield_swap(value, 60, 1); 3009 new_val |= bitfield_swap(value, 44, 16); 3010 new_val |= bitfield_swap(value, 40, 4); 3011 new_val |= bitfield_swap(value, 38, 2); 3012 new_val |= bitfield_swap(value, 34, 4); 3013 new_val |= bitfield_swap(value, 31, 3); 3014 new_val |= bitfield_swap(value, 0, 31); 3015 } 3016 3017 return new_val; 3018 } 3019 3020 static inline bool evsel__has_branch_counters(const struct evsel *evsel) 3021 { 3022 struct evsel *leader = evsel__leader(evsel); 3023 3024 /* The branch counters feature only supports group */ 3025 if (!leader || !evsel->evlist) 3026 return false; 3027 3028 if (evsel->evlist->nr_br_cntr < 0) 3029 evlist__update_br_cntr(evsel->evlist); 3030 3031 if (leader->br_cntr_nr > 0) 3032 return true; 3033 3034 return false; 3035 } 3036 3037 static int __set_offcpu_sample(struct perf_sample *data) 3038 { 3039 u64 *array = data->raw_data; 3040 u32 max_size = data->raw_size, *p32; 3041 const void *endp = (void *)array + max_size; 3042 3043 if (array == NULL) 3044 return -EFAULT; 3045 3046 OVERFLOW_CHECK_u64(array); 3047 p32 = (void *)array++; 3048 data->pid = p32[0]; 3049 data->tid = p32[1]; 3050 3051 OVERFLOW_CHECK_u64(array); 3052 data->period = *array++; 3053 3054 OVERFLOW_CHECK_u64(array); 3055 data->callchain = (struct ip_callchain *)array++; 3056 OVERFLOW_CHECK(array, data->callchain->nr * sizeof(u64), max_size); 3057 data->ip = data->callchain->ips[1]; 3058 array += data->callchain->nr; 3059 3060 OVERFLOW_CHECK_u64(array); 3061 data->cgroup = *array; 3062 3063 return 0; 3064 } 3065 3066 int evsel__parse_sample(struct evsel *evsel, union perf_event *event, 3067 struct perf_sample *data) 3068 { 3069 u64 type = evsel->core.attr.sample_type; 3070 bool swapped = evsel->needs_swap; 3071 const __u64 *array; 3072 u16 max_size = event->header.size; 3073 const void *endp = (void *)event + max_size; 3074 u64 sz; 3075 3076 /* 3077 * used for cross-endian analysis. See git commit 65014ab3 3078 * for why this goofiness is needed. 3079 */ 3080 union u64_swap u; 3081 3082 memset(data, 0, sizeof(*data)); 3083 data->cpu = data->pid = data->tid = -1; 3084 data->stream_id = data->id = data->time = -1ULL; 3085 data->period = evsel->core.attr.sample_period; 3086 data->cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; 3087 data->misc = event->header.misc; 3088 data->data_src = PERF_MEM_DATA_SRC_NONE; 3089 data->vcpu = -1; 3090 3091 if (event->header.type != PERF_RECORD_SAMPLE) { 3092 if (!evsel->core.attr.sample_id_all) 3093 return 0; 3094 return perf_evsel__parse_id_sample(evsel, event, data); 3095 } 3096 3097 array = event->sample.array; 3098 3099 if (perf_event__check_size(event, evsel->sample_size)) 3100 return -EFAULT; 3101 3102 if (type & PERF_SAMPLE_IDENTIFIER) { 3103 data->id = *array; 3104 array++; 3105 } 3106 3107 if (type & PERF_SAMPLE_IP) { 3108 data->ip = *array; 3109 array++; 3110 } 3111 3112 if (type & PERF_SAMPLE_TID) { 3113 u.val64 = *array; 3114 if (swapped) { 3115 /* undo swap of u64, then swap on individual u32s */ 3116 u.val64 = bswap_64(u.val64); 3117 u.val32[0] = bswap_32(u.val32[0]); 3118 u.val32[1] = bswap_32(u.val32[1]); 3119 } 3120 3121 data->pid = u.val32[0]; 3122 data->tid = u.val32[1]; 3123 array++; 3124 } 3125 3126 if (type & PERF_SAMPLE_TIME) { 3127 data->time = *array; 3128 array++; 3129 } 3130 3131 if (type & PERF_SAMPLE_ADDR) { 3132 data->addr = *array; 3133 array++; 3134 } 3135 3136 if (type & PERF_SAMPLE_ID) { 3137 data->id = *array; 3138 array++; 3139 } 3140 3141 if (type & PERF_SAMPLE_STREAM_ID) { 3142 data->stream_id = *array; 3143 array++; 3144 } 3145 3146 if (type & PERF_SAMPLE_CPU) { 3147 3148 u.val64 = *array; 3149 if (swapped) { 3150 /* undo swap of u64, then swap on individual u32s */ 3151 u.val64 = bswap_64(u.val64); 3152 u.val32[0] = bswap_32(u.val32[0]); 3153 } 3154 3155 data->cpu = u.val32[0]; 3156 array++; 3157 } 3158 3159 if (type & PERF_SAMPLE_PERIOD) { 3160 data->period = *array; 3161 array++; 3162 } 3163 3164 if (type & PERF_SAMPLE_READ) { 3165 u64 read_format = evsel->core.attr.read_format; 3166 3167 OVERFLOW_CHECK_u64(array); 3168 if (read_format & PERF_FORMAT_GROUP) 3169 data->read.group.nr = *array; 3170 else 3171 data->read.one.value = *array; 3172 3173 array++; 3174 3175 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { 3176 OVERFLOW_CHECK_u64(array); 3177 data->read.time_enabled = *array; 3178 array++; 3179 } 3180 3181 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { 3182 OVERFLOW_CHECK_u64(array); 3183 data->read.time_running = *array; 3184 array++; 3185 } 3186 3187 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */ 3188 if (read_format & PERF_FORMAT_GROUP) { 3189 const u64 max_group_nr = UINT64_MAX / 3190 sizeof(struct sample_read_value); 3191 3192 if (data->read.group.nr > max_group_nr) 3193 return -EFAULT; 3194 3195 sz = data->read.group.nr * sample_read_value_size(read_format); 3196 OVERFLOW_CHECK(array, sz, max_size); 3197 data->read.group.values = 3198 (struct sample_read_value *)array; 3199 array = (void *)array + sz; 3200 } else { 3201 OVERFLOW_CHECK_u64(array); 3202 data->read.one.id = *array; 3203 array++; 3204 3205 if (read_format & PERF_FORMAT_LOST) { 3206 OVERFLOW_CHECK_u64(array); 3207 data->read.one.lost = *array; 3208 array++; 3209 } 3210 } 3211 } 3212 3213 if (type & PERF_SAMPLE_CALLCHAIN) { 3214 const u64 max_callchain_nr = UINT64_MAX / sizeof(u64); 3215 3216 OVERFLOW_CHECK_u64(array); 3217 data->callchain = (struct ip_callchain *)array++; 3218 if (data->callchain->nr > max_callchain_nr) 3219 return -EFAULT; 3220 sz = data->callchain->nr * sizeof(u64); 3221 OVERFLOW_CHECK(array, sz, max_size); 3222 array = (void *)array + sz; 3223 } 3224 3225 if (type & PERF_SAMPLE_RAW) { 3226 OVERFLOW_CHECK_u64(array); 3227 u.val64 = *array; 3228 3229 /* 3230 * Undo swap of u64, then swap on individual u32s, 3231 * get the size of the raw area and undo all of the 3232 * swap. The pevent interface handles endianness by 3233 * itself. 3234 */ 3235 if (swapped) { 3236 u.val64 = bswap_64(u.val64); 3237 u.val32[0] = bswap_32(u.val32[0]); 3238 u.val32[1] = bswap_32(u.val32[1]); 3239 } 3240 data->raw_size = u.val32[0]; 3241 3242 /* 3243 * The raw data is aligned on 64bits including the 3244 * u32 size, so it's safe to use mem_bswap_64. 3245 */ 3246 if (swapped) 3247 mem_bswap_64((void *) array, data->raw_size); 3248 3249 array = (void *)array + sizeof(u32); 3250 3251 OVERFLOW_CHECK(array, data->raw_size, max_size); 3252 data->raw_data = (void *)array; 3253 array = (void *)array + data->raw_size; 3254 } 3255 3256 if (type & PERF_SAMPLE_BRANCH_STACK) { 3257 const u64 max_branch_nr = UINT64_MAX / 3258 sizeof(struct branch_entry); 3259 struct branch_entry *e; 3260 unsigned int i; 3261 3262 OVERFLOW_CHECK_u64(array); 3263 data->branch_stack = (struct branch_stack *)array++; 3264 3265 if (data->branch_stack->nr > max_branch_nr) 3266 return -EFAULT; 3267 3268 sz = data->branch_stack->nr * sizeof(struct branch_entry); 3269 if (evsel__has_branch_hw_idx(evsel)) { 3270 sz += sizeof(u64); 3271 e = &data->branch_stack->entries[0]; 3272 } else { 3273 data->no_hw_idx = true; 3274 /* 3275 * if the PERF_SAMPLE_BRANCH_HW_INDEX is not applied, 3276 * only nr and entries[] will be output by kernel. 3277 */ 3278 e = (struct branch_entry *)&data->branch_stack->hw_idx; 3279 } 3280 3281 if (swapped) { 3282 /* 3283 * struct branch_flag does not have endian 3284 * specific bit field definition. And bswap 3285 * will not resolve the issue, since these 3286 * are bit fields. 3287 * 3288 * evsel__bitfield_swap_branch_flags() uses a 3289 * bitfield_swap macro to swap the bit position 3290 * based on the host endians. 3291 */ 3292 for (i = 0; i < data->branch_stack->nr; i++, e++) 3293 e->flags.value = evsel__bitfield_swap_branch_flags(e->flags.value); 3294 } 3295 3296 OVERFLOW_CHECK(array, sz, max_size); 3297 array = (void *)array + sz; 3298 3299 if (evsel__has_branch_counters(evsel)) { 3300 data->branch_stack_cntr = (u64 *)array; 3301 sz = data->branch_stack->nr * sizeof(u64); 3302 3303 OVERFLOW_CHECK(array, sz, max_size); 3304 array = (void *)array + sz; 3305 } 3306 } 3307 3308 if (type & PERF_SAMPLE_REGS_USER) { 3309 struct regs_dump *regs = perf_sample__user_regs(data); 3310 3311 OVERFLOW_CHECK_u64(array); 3312 regs->abi = *array; 3313 array++; 3314 3315 if (regs->abi) { 3316 u64 mask = evsel->core.attr.sample_regs_user; 3317 3318 sz = hweight64(mask) * sizeof(u64); 3319 OVERFLOW_CHECK(array, sz, max_size); 3320 regs->mask = mask; 3321 regs->regs = (u64 *)array; 3322 array = (void *)array + sz; 3323 } 3324 } 3325 3326 if (type & PERF_SAMPLE_STACK_USER) { 3327 OVERFLOW_CHECK_u64(array); 3328 sz = *array++; 3329 3330 data->user_stack.offset = ((char *)(array - 1) 3331 - (char *) event); 3332 3333 if (!sz) { 3334 data->user_stack.size = 0; 3335 } else { 3336 OVERFLOW_CHECK(array, sz, max_size); 3337 data->user_stack.data = (char *)array; 3338 array = (void *)array + sz; 3339 OVERFLOW_CHECK_u64(array); 3340 data->user_stack.size = *array++; 3341 if (WARN_ONCE(data->user_stack.size > sz, 3342 "user stack dump failure\n")) 3343 return -EFAULT; 3344 } 3345 } 3346 3347 if (type & PERF_SAMPLE_WEIGHT_TYPE) { 3348 OVERFLOW_CHECK_u64(array); 3349 perf_parse_sample_weight(data, array, type); 3350 array++; 3351 } 3352 3353 if (type & PERF_SAMPLE_DATA_SRC) { 3354 OVERFLOW_CHECK_u64(array); 3355 data->data_src = *array; 3356 array++; 3357 } 3358 3359 if (type & PERF_SAMPLE_TRANSACTION) { 3360 OVERFLOW_CHECK_u64(array); 3361 data->transaction = *array; 3362 array++; 3363 } 3364 3365 if (type & PERF_SAMPLE_REGS_INTR) { 3366 struct regs_dump *regs = perf_sample__intr_regs(data); 3367 3368 OVERFLOW_CHECK_u64(array); 3369 regs->abi = *array; 3370 array++; 3371 3372 if (regs->abi != PERF_SAMPLE_REGS_ABI_NONE) { 3373 u64 mask = evsel->core.attr.sample_regs_intr; 3374 3375 sz = hweight64(mask) * sizeof(u64); 3376 OVERFLOW_CHECK(array, sz, max_size); 3377 regs->mask = mask; 3378 regs->regs = (u64 *)array; 3379 array = (void *)array + sz; 3380 } 3381 } 3382 3383 data->phys_addr = 0; 3384 if (type & PERF_SAMPLE_PHYS_ADDR) { 3385 data->phys_addr = *array; 3386 array++; 3387 } 3388 3389 data->cgroup = 0; 3390 if (type & PERF_SAMPLE_CGROUP) { 3391 data->cgroup = *array; 3392 array++; 3393 } 3394 3395 data->data_page_size = 0; 3396 if (type & PERF_SAMPLE_DATA_PAGE_SIZE) { 3397 data->data_page_size = *array; 3398 array++; 3399 } 3400 3401 data->code_page_size = 0; 3402 if (type & PERF_SAMPLE_CODE_PAGE_SIZE) { 3403 data->code_page_size = *array; 3404 array++; 3405 } 3406 3407 if (type & PERF_SAMPLE_AUX) { 3408 OVERFLOW_CHECK_u64(array); 3409 sz = *array++; 3410 3411 OVERFLOW_CHECK(array, sz, max_size); 3412 /* Undo swap of data */ 3413 if (swapped) 3414 mem_bswap_64((char *)array, sz); 3415 data->aux_sample.size = sz; 3416 data->aux_sample.data = (char *)array; 3417 array = (void *)array + sz; 3418 } 3419 3420 if (evsel__is_offcpu_event(evsel)) 3421 return __set_offcpu_sample(data); 3422 3423 return 0; 3424 } 3425 3426 int evsel__parse_sample_timestamp(struct evsel *evsel, union perf_event *event, 3427 u64 *timestamp) 3428 { 3429 u64 type = evsel->core.attr.sample_type; 3430 const __u64 *array; 3431 3432 if (!(type & PERF_SAMPLE_TIME)) 3433 return -1; 3434 3435 if (event->header.type != PERF_RECORD_SAMPLE) { 3436 struct perf_sample data = { 3437 .time = -1ULL, 3438 }; 3439 3440 if (!evsel->core.attr.sample_id_all) 3441 return -1; 3442 if (perf_evsel__parse_id_sample(evsel, event, &data)) 3443 return -1; 3444 3445 *timestamp = data.time; 3446 return 0; 3447 } 3448 3449 array = event->sample.array; 3450 3451 if (perf_event__check_size(event, evsel->sample_size)) 3452 return -EFAULT; 3453 3454 if (type & PERF_SAMPLE_IDENTIFIER) 3455 array++; 3456 3457 if (type & PERF_SAMPLE_IP) 3458 array++; 3459 3460 if (type & PERF_SAMPLE_TID) 3461 array++; 3462 3463 if (type & PERF_SAMPLE_TIME) 3464 *timestamp = *array; 3465 3466 return 0; 3467 } 3468 3469 u16 evsel__id_hdr_size(const struct evsel *evsel) 3470 { 3471 u64 sample_type = evsel->core.attr.sample_type; 3472 u16 size = 0; 3473 3474 if (sample_type & PERF_SAMPLE_TID) 3475 size += sizeof(u64); 3476 3477 if (sample_type & PERF_SAMPLE_TIME) 3478 size += sizeof(u64); 3479 3480 if (sample_type & PERF_SAMPLE_ID) 3481 size += sizeof(u64); 3482 3483 if (sample_type & PERF_SAMPLE_STREAM_ID) 3484 size += sizeof(u64); 3485 3486 if (sample_type & PERF_SAMPLE_CPU) 3487 size += sizeof(u64); 3488 3489 if (sample_type & PERF_SAMPLE_IDENTIFIER) 3490 size += sizeof(u64); 3491 3492 return size; 3493 } 3494 3495 #ifdef HAVE_LIBTRACEEVENT 3496 struct tep_format_field *evsel__field(struct evsel *evsel, const char *name) 3497 { 3498 struct tep_event *tp_format = evsel__tp_format(evsel); 3499 3500 return tp_format ? tep_find_field(tp_format, name) : NULL; 3501 } 3502 3503 struct tep_format_field *evsel__common_field(struct evsel *evsel, const char *name) 3504 { 3505 struct tep_event *tp_format = evsel__tp_format(evsel); 3506 3507 return tp_format ? tep_find_common_field(tp_format, name) : NULL; 3508 } 3509 3510 void *evsel__rawptr(struct evsel *evsel, struct perf_sample *sample, const char *name) 3511 { 3512 struct tep_format_field *field = evsel__field(evsel, name); 3513 int offset; 3514 3515 if (!field) 3516 return NULL; 3517 3518 offset = field->offset; 3519 3520 if (field->flags & TEP_FIELD_IS_DYNAMIC) { 3521 offset = *(int *)(sample->raw_data + field->offset); 3522 offset &= 0xffff; 3523 if (tep_field_is_relative(field->flags)) 3524 offset += field->offset + field->size; 3525 } 3526 3527 return sample->raw_data + offset; 3528 } 3529 3530 u64 format_field__intval(struct tep_format_field *field, struct perf_sample *sample, 3531 bool needs_swap) 3532 { 3533 u64 value; 3534 void *ptr = sample->raw_data + field->offset; 3535 3536 switch (field->size) { 3537 case 1: 3538 return *(u8 *)ptr; 3539 case 2: 3540 value = *(u16 *)ptr; 3541 break; 3542 case 4: 3543 value = *(u32 *)ptr; 3544 break; 3545 case 8: 3546 memcpy(&value, ptr, sizeof(u64)); 3547 break; 3548 default: 3549 return 0; 3550 } 3551 3552 if (!needs_swap) 3553 return value; 3554 3555 switch (field->size) { 3556 case 2: 3557 return bswap_16(value); 3558 case 4: 3559 return bswap_32(value); 3560 case 8: 3561 return bswap_64(value); 3562 default: 3563 return 0; 3564 } 3565 3566 return 0; 3567 } 3568 3569 u64 evsel__intval(struct evsel *evsel, struct perf_sample *sample, const char *name) 3570 { 3571 struct tep_format_field *field = evsel__field(evsel, name); 3572 3573 return field ? format_field__intval(field, sample, evsel->needs_swap) : 0; 3574 } 3575 3576 u64 evsel__intval_common(struct evsel *evsel, struct perf_sample *sample, const char *name) 3577 { 3578 struct tep_format_field *field = evsel__common_field(evsel, name); 3579 3580 return field ? format_field__intval(field, sample, evsel->needs_swap) : 0; 3581 } 3582 3583 char evsel__taskstate(struct evsel *evsel, struct perf_sample *sample, const char *name) 3584 { 3585 static struct tep_format_field *prev_state_field; 3586 static const char *states; 3587 struct tep_format_field *field; 3588 unsigned long long val; 3589 unsigned int bit; 3590 char state = '?'; /* '?' denotes unknown task state */ 3591 3592 field = evsel__field(evsel, name); 3593 3594 if (!field) 3595 return state; 3596 3597 if (!states || field != prev_state_field) { 3598 states = parse_task_states(field); 3599 if (!states) 3600 return state; 3601 prev_state_field = field; 3602 } 3603 3604 /* 3605 * Note since the kernel exposes TASK_REPORT_MAX to userspace 3606 * to denote the 'preempted' state, we might as welll report 3607 * 'R' for this case, which make senses to users as well. 3608 * 3609 * We can change this if we have a good reason in the future. 3610 */ 3611 val = evsel__intval(evsel, sample, name); 3612 bit = val ? ffs(val) : 0; 3613 state = (!bit || bit > strlen(states)) ? 'R' : states[bit-1]; 3614 return state; 3615 } 3616 #endif 3617 3618 bool evsel__fallback(struct evsel *evsel, struct target *target, int err, 3619 char *msg, size_t msgsize) 3620 { 3621 int paranoid; 3622 3623 if ((err == ENOENT || err == ENXIO || err == ENODEV) && 3624 evsel->core.attr.type == PERF_TYPE_HARDWARE && 3625 evsel->core.attr.config == PERF_COUNT_HW_CPU_CYCLES) { 3626 /* 3627 * If it's cycles then fall back to hrtimer based cpu-clock sw 3628 * counter, which is always available even if no PMU support. 3629 * 3630 * PPC returns ENXIO until 2.6.37 (behavior changed with commit 3631 * b0a873e). 3632 */ 3633 evsel->core.attr.type = PERF_TYPE_SOFTWARE; 3634 evsel->core.attr.config = target__has_cpu(target) 3635 ? PERF_COUNT_SW_CPU_CLOCK 3636 : PERF_COUNT_SW_TASK_CLOCK; 3637 scnprintf(msg, msgsize, 3638 "The cycles event is not supported, trying to fall back to %s", 3639 target__has_cpu(target) ? "cpu-clock" : "task-clock"); 3640 3641 zfree(&evsel->name); 3642 return true; 3643 } else if (err == EACCES && !evsel->core.attr.exclude_kernel && 3644 (paranoid = perf_event_paranoid()) > 1) { 3645 const char *name = evsel__name(evsel); 3646 char *new_name; 3647 const char *sep = ":"; 3648 3649 /* If event has exclude user then don't exclude kernel. */ 3650 if (evsel->core.attr.exclude_user) 3651 goto no_fallback; 3652 3653 /* Is there already the separator in the name. */ 3654 if (strchr(name, '/') || 3655 (strchr(name, ':') && !evsel->is_libpfm_event)) 3656 sep = ""; 3657 3658 if (asprintf(&new_name, "%s%su", name, sep) < 0) 3659 goto no_fallback; 3660 3661 free(evsel->name); 3662 evsel->name = new_name; 3663 scnprintf(msg, msgsize, "kernel.perf_event_paranoid=%d, trying " 3664 "to fall back to excluding kernel and hypervisor " 3665 " samples", paranoid); 3666 evsel->core.attr.exclude_kernel = 1; 3667 evsel->core.attr.exclude_hv = 1; 3668 3669 return true; 3670 } else if (err == EOPNOTSUPP && !evsel->core.attr.exclude_guest && 3671 !evsel->exclude_GH) { 3672 const char *name = evsel__name(evsel); 3673 char *new_name; 3674 const char *sep = ":"; 3675 3676 /* Is there already the separator in the name. */ 3677 if (strchr(name, '/') || 3678 (strchr(name, ':') && !evsel->is_libpfm_event)) 3679 sep = ""; 3680 3681 if (asprintf(&new_name, "%s%sH", name, sep) < 0) 3682 goto no_fallback; 3683 3684 free(evsel->name); 3685 evsel->name = new_name; 3686 /* Apple M1 requires exclude_guest */ 3687 scnprintf(msg, msgsize, "Trying to fall back to excluding guest samples"); 3688 evsel->core.attr.exclude_guest = 1; 3689 3690 return true; 3691 } 3692 no_fallback: 3693 scnprintf(msg, msgsize, "No fallback found for '%s' for error %d", 3694 evsel__name(evsel), err); 3695 return false; 3696 } 3697 3698 static bool find_process(const char *name) 3699 { 3700 size_t len = strlen(name); 3701 DIR *dir; 3702 struct dirent *d; 3703 int ret = -1; 3704 3705 dir = opendir(procfs__mountpoint()); 3706 if (!dir) 3707 return false; 3708 3709 /* Walk through the directory. */ 3710 while (ret && (d = readdir(dir)) != NULL) { 3711 char path[PATH_MAX]; 3712 char *data; 3713 size_t size; 3714 3715 if ((d->d_type != DT_DIR) || 3716 !strcmp(".", d->d_name) || 3717 !strcmp("..", d->d_name)) 3718 continue; 3719 3720 scnprintf(path, sizeof(path), "%s/%s/comm", 3721 procfs__mountpoint(), d->d_name); 3722 3723 if (filename__read_str(path, &data, &size)) 3724 continue; 3725 3726 ret = strncmp(name, data, len); 3727 free(data); 3728 } 3729 3730 closedir(dir); 3731 return ret ? false : true; 3732 } 3733 3734 static int dump_perf_event_processes(char *msg, size_t size) 3735 { 3736 DIR *proc_dir; 3737 struct dirent *proc_entry; 3738 int printed = 0; 3739 3740 proc_dir = opendir(procfs__mountpoint()); 3741 if (!proc_dir) 3742 return 0; 3743 3744 /* Walk through the /proc directory. */ 3745 while ((proc_entry = readdir(proc_dir)) != NULL) { 3746 char buf[256]; 3747 DIR *fd_dir; 3748 struct dirent *fd_entry; 3749 int fd_dir_fd; 3750 3751 if (proc_entry->d_type != DT_DIR || 3752 !isdigit(proc_entry->d_name[0]) || 3753 strlen(proc_entry->d_name) > sizeof(buf) - 4) 3754 continue; 3755 3756 scnprintf(buf, sizeof(buf), "%s/fd", proc_entry->d_name); 3757 fd_dir_fd = openat(dirfd(proc_dir), buf, O_DIRECTORY); 3758 if (fd_dir_fd == -1) 3759 continue; 3760 fd_dir = fdopendir(fd_dir_fd); 3761 if (!fd_dir) { 3762 close(fd_dir_fd); 3763 continue; 3764 } 3765 while ((fd_entry = readdir(fd_dir)) != NULL) { 3766 ssize_t link_size; 3767 3768 if (fd_entry->d_type != DT_LNK) 3769 continue; 3770 link_size = readlinkat(fd_dir_fd, fd_entry->d_name, buf, sizeof(buf)); 3771 if (link_size < 0) 3772 continue; 3773 /* Take care as readlink doesn't null terminate the string. */ 3774 if (!strncmp(buf, "anon_inode:[perf_event]", link_size)) { 3775 int cmdline_fd; 3776 ssize_t cmdline_size; 3777 3778 scnprintf(buf, sizeof(buf), "%s/cmdline", proc_entry->d_name); 3779 cmdline_fd = openat(dirfd(proc_dir), buf, O_RDONLY); 3780 if (cmdline_fd == -1) 3781 continue; 3782 cmdline_size = read(cmdline_fd, buf, sizeof(buf) - 1); 3783 close(cmdline_fd); 3784 if (cmdline_size < 0) 3785 continue; 3786 buf[cmdline_size] = '\0'; 3787 for (ssize_t i = 0; i < cmdline_size; i++) { 3788 if (buf[i] == '\0') 3789 buf[i] = ' '; 3790 } 3791 3792 if (printed == 0) 3793 printed += scnprintf(msg, size, "Possible processes:\n"); 3794 3795 printed += scnprintf(msg + printed, size - printed, 3796 "%s %s\n", proc_entry->d_name, buf); 3797 break; 3798 } 3799 } 3800 closedir(fd_dir); 3801 } 3802 closedir(proc_dir); 3803 return printed; 3804 } 3805 3806 int __weak arch_evsel__open_strerror(struct evsel *evsel __maybe_unused, 3807 int err __maybe_unused, 3808 char *msg __maybe_unused, 3809 size_t size __maybe_unused) 3810 { 3811 return 0; 3812 } 3813 3814 int evsel__open_strerror(struct evsel *evsel, struct target *target, 3815 int err, char *msg, size_t size) 3816 { 3817 struct perf_pmu *pmu; 3818 char sbuf[STRERR_BUFSIZE]; 3819 int printed = 0, enforced = 0; 3820 int ret; 3821 3822 switch (err) { 3823 case EPERM: 3824 case EACCES: 3825 printed += scnprintf(msg + printed, size - printed, 3826 "Access to performance monitoring and observability operations is limited.\n"); 3827 3828 if (!sysfs__read_int("fs/selinux/enforce", &enforced)) { 3829 if (enforced) { 3830 printed += scnprintf(msg + printed, size - printed, 3831 "Enforced MAC policy settings (SELinux) can limit access to performance\n" 3832 "monitoring and observability operations. Inspect system audit records for\n" 3833 "more perf_event access control information and adjusting the policy.\n"); 3834 } 3835 } 3836 3837 if (err == EPERM) 3838 printed += scnprintf(msg, size, 3839 "No permission to enable %s event.\n\n", evsel__name(evsel)); 3840 3841 return printed + scnprintf(msg + printed, size - printed, 3842 "Consider adjusting /proc/sys/kernel/perf_event_paranoid setting to open\n" 3843 "access to performance monitoring and observability operations for processes\n" 3844 "without CAP_PERFMON, CAP_SYS_PTRACE or CAP_SYS_ADMIN Linux capability.\n" 3845 "More information can be found at 'Perf events and tool security' document:\n" 3846 "https://www.kernel.org/doc/html/latest/admin-guide/perf-security.html\n" 3847 "perf_event_paranoid setting is %d:\n" 3848 " -1: Allow use of (almost) all events by all users\n" 3849 " Ignore mlock limit after perf_event_mlock_kb without CAP_IPC_LOCK\n" 3850 ">= 0: Disallow raw and ftrace function tracepoint access\n" 3851 ">= 1: Disallow CPU event access\n" 3852 ">= 2: Disallow kernel profiling\n" 3853 "To make the adjusted perf_event_paranoid setting permanent preserve it\n" 3854 "in /etc/sysctl.conf (e.g. kernel.perf_event_paranoid = <setting>)", 3855 perf_event_paranoid()); 3856 case ENOENT: 3857 return scnprintf(msg, size, "The %s event is not supported.", evsel__name(evsel)); 3858 case EMFILE: 3859 return scnprintf(msg, size, "%s", 3860 "Too many events are opened.\n" 3861 "Probably the maximum number of open file descriptors has been reached.\n" 3862 "Hint: Try again after reducing the number of events.\n" 3863 "Hint: Try increasing the limit with 'ulimit -n <limit>'"); 3864 case ENOMEM: 3865 if (evsel__has_callchain(evsel) && 3866 access("/proc/sys/kernel/perf_event_max_stack", F_OK) == 0) 3867 return scnprintf(msg, size, 3868 "Not enough memory to setup event with callchain.\n" 3869 "Hint: Try tweaking /proc/sys/kernel/perf_event_max_stack\n" 3870 "Hint: Current value: %d", sysctl__max_stack()); 3871 break; 3872 case ENODEV: 3873 if (target->cpu_list) 3874 return scnprintf(msg, size, "%s", 3875 "No such device - did you specify an out-of-range profile CPU?"); 3876 break; 3877 case EOPNOTSUPP: 3878 if (evsel->core.attr.sample_type & PERF_SAMPLE_BRANCH_STACK) 3879 return scnprintf(msg, size, 3880 "%s: PMU Hardware or event type doesn't support branch stack sampling.", 3881 evsel__name(evsel)); 3882 if (evsel->core.attr.aux_output) 3883 return scnprintf(msg, size, 3884 "%s: PMU Hardware doesn't support 'aux_output' feature", 3885 evsel__name(evsel)); 3886 if (evsel->core.attr.aux_action) 3887 return scnprintf(msg, size, 3888 "%s: PMU Hardware doesn't support 'aux_action' feature", 3889 evsel__name(evsel)); 3890 if (evsel->core.attr.sample_period != 0) 3891 return scnprintf(msg, size, 3892 "%s: PMU Hardware doesn't support sampling/overflow-interrupts. Try 'perf stat'", 3893 evsel__name(evsel)); 3894 if (evsel->core.attr.precise_ip) 3895 return scnprintf(msg, size, "%s", 3896 "\'precise\' request may not be supported. Try removing 'p' modifier."); 3897 #if defined(__i386__) || defined(__x86_64__) 3898 if (evsel->core.attr.type == PERF_TYPE_HARDWARE) 3899 return scnprintf(msg, size, "%s", 3900 "No hardware sampling interrupt available.\n"); 3901 #endif 3902 if (!target__has_cpu(target)) 3903 return scnprintf(msg, size, 3904 "Unsupported event (%s) in per-thread mode, enable system wide with '-a'.", 3905 evsel__name(evsel)); 3906 break; 3907 case EBUSY: 3908 if (find_process("oprofiled")) 3909 return scnprintf(msg, size, 3910 "The PMU counters are busy/taken by another profiler.\n" 3911 "We found oprofile daemon running, please stop it and try again."); 3912 printed += scnprintf( 3913 msg, size, 3914 "The PMU %s counters are busy and in use by another process.\n", 3915 evsel->pmu ? evsel->pmu->name : ""); 3916 return printed + dump_perf_event_processes(msg + printed, size - printed); 3917 break; 3918 case EINVAL: 3919 if (evsel->core.attr.sample_type & PERF_SAMPLE_CODE_PAGE_SIZE && perf_missing_features.code_page_size) 3920 return scnprintf(msg, size, "Asking for the code page size isn't supported by this kernel."); 3921 if (evsel->core.attr.sample_type & PERF_SAMPLE_DATA_PAGE_SIZE && perf_missing_features.data_page_size) 3922 return scnprintf(msg, size, "Asking for the data page size isn't supported by this kernel."); 3923 if (evsel->core.attr.write_backward && perf_missing_features.write_backward) 3924 return scnprintf(msg, size, "Reading from overwrite event is not supported by this kernel."); 3925 if (perf_missing_features.clockid) 3926 return scnprintf(msg, size, "clockid feature not supported."); 3927 if (perf_missing_features.clockid_wrong) 3928 return scnprintf(msg, size, "wrong clockid (%d).", clockid); 3929 if (perf_missing_features.aux_action) 3930 return scnprintf(msg, size, "The 'aux_action' feature is not supported, update the kernel."); 3931 if (perf_missing_features.aux_output) 3932 return scnprintf(msg, size, "The 'aux_output' feature is not supported, update the kernel."); 3933 pmu = evsel__find_pmu(evsel); 3934 if (!pmu->is_core && !target__has_cpu(target)) 3935 return scnprintf(msg, size, 3936 "Invalid event (%s) in per-thread mode, enable system wide with '-a'.", 3937 evsel__name(evsel)); 3938 3939 break; 3940 case ENODATA: 3941 return scnprintf(msg, size, "Cannot collect data source with the load latency event alone. " 3942 "Please add an auxiliary event in front of the load latency event."); 3943 default: 3944 break; 3945 } 3946 3947 ret = arch_evsel__open_strerror(evsel, err, msg, size); 3948 if (ret) 3949 return ret; 3950 3951 return scnprintf(msg, size, 3952 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n" 3953 "\"dmesg | grep -i perf\" may provide additional information.\n", 3954 err, str_error_r(err, sbuf, sizeof(sbuf)), evsel__name(evsel)); 3955 } 3956 3957 struct perf_session *evsel__session(struct evsel *evsel) 3958 { 3959 return evsel && evsel->evlist ? evsel->evlist->session : NULL; 3960 } 3961 3962 struct perf_env *evsel__env(struct evsel *evsel) 3963 { 3964 struct perf_session *session = evsel__session(evsel); 3965 3966 return session ? perf_session__env(session) : NULL; 3967 } 3968 3969 static int store_evsel_ids(struct evsel *evsel, struct evlist *evlist) 3970 { 3971 int cpu_map_idx, thread; 3972 3973 if (evsel__is_retire_lat(evsel)) 3974 return 0; 3975 3976 for (cpu_map_idx = 0; cpu_map_idx < xyarray__max_x(evsel->core.fd); cpu_map_idx++) { 3977 for (thread = 0; thread < xyarray__max_y(evsel->core.fd); 3978 thread++) { 3979 int fd = FD(evsel, cpu_map_idx, thread); 3980 3981 if (perf_evlist__id_add_fd(&evlist->core, &evsel->core, 3982 cpu_map_idx, thread, fd) < 0) 3983 return -1; 3984 } 3985 } 3986 3987 return 0; 3988 } 3989 3990 int evsel__store_ids(struct evsel *evsel, struct evlist *evlist) 3991 { 3992 struct perf_cpu_map *cpus = evsel->core.cpus; 3993 struct perf_thread_map *threads = evsel->core.threads; 3994 3995 if (perf_evsel__alloc_id(&evsel->core, perf_cpu_map__nr(cpus), threads->nr)) 3996 return -ENOMEM; 3997 3998 return store_evsel_ids(evsel, evlist); 3999 } 4000 4001 void evsel__zero_per_pkg(struct evsel *evsel) 4002 { 4003 struct hashmap_entry *cur; 4004 size_t bkt; 4005 4006 if (evsel->per_pkg_mask) { 4007 hashmap__for_each_entry(evsel->per_pkg_mask, cur, bkt) 4008 zfree(&cur->pkey); 4009 4010 hashmap__clear(evsel->per_pkg_mask); 4011 } 4012 } 4013 4014 /** 4015 * evsel__is_hybrid - does the evsel have a known PMU that is hybrid. Note, this 4016 * will be false on hybrid systems for hardware and legacy 4017 * cache events. 4018 */ 4019 bool evsel__is_hybrid(const struct evsel *evsel) 4020 { 4021 if (!evsel->core.is_pmu_core) 4022 return false; 4023 4024 return perf_pmus__num_core_pmus() > 1; 4025 } 4026 4027 struct evsel *evsel__leader(const struct evsel *evsel) 4028 { 4029 if (evsel->core.leader == NULL) 4030 return NULL; 4031 return container_of(evsel->core.leader, struct evsel, core); 4032 } 4033 4034 bool evsel__has_leader(struct evsel *evsel, struct evsel *leader) 4035 { 4036 return evsel->core.leader == &leader->core; 4037 } 4038 4039 bool evsel__is_leader(struct evsel *evsel) 4040 { 4041 return evsel__has_leader(evsel, evsel); 4042 } 4043 4044 void evsel__set_leader(struct evsel *evsel, struct evsel *leader) 4045 { 4046 evsel->core.leader = &leader->core; 4047 } 4048 4049 int evsel__source_count(const struct evsel *evsel) 4050 { 4051 struct evsel *pos; 4052 int count = 0; 4053 4054 evlist__for_each_entry(evsel->evlist, pos) { 4055 if (pos->metric_leader == evsel) 4056 count++; 4057 } 4058 return count; 4059 } 4060 4061 bool __weak arch_evsel__must_be_in_group(const struct evsel *evsel __maybe_unused) 4062 { 4063 return false; 4064 } 4065 4066 /* 4067 * Remove an event from a given group (leader). 4068 * Some events, e.g., perf metrics Topdown events, 4069 * must always be grouped. Ignore the events. 4070 */ 4071 void evsel__remove_from_group(struct evsel *evsel, struct evsel *leader) 4072 { 4073 if (!arch_evsel__must_be_in_group(evsel) && evsel != leader) { 4074 evsel__set_leader(evsel, evsel); 4075 evsel->core.nr_members = 0; 4076 leader->core.nr_members--; 4077 } 4078 } 4079 4080 bool evsel__set_needs_uniquify(struct evsel *counter, const struct perf_stat_config *config) 4081 { 4082 struct evsel *evsel; 4083 4084 if (counter->needs_uniquify) { 4085 /* Already set. */ 4086 return true; 4087 } 4088 4089 if (counter->use_config_name || counter->is_libpfm_event) { 4090 /* Original name will be used. */ 4091 return false; 4092 } 4093 4094 if (!config->hybrid_merge && evsel__is_hybrid(counter)) { 4095 /* Unique hybrid counters necessary. */ 4096 counter->needs_uniquify = true; 4097 return true; 4098 } 4099 4100 if (counter->core.attr.type < PERF_TYPE_MAX && counter->core.attr.type != PERF_TYPE_RAW) { 4101 /* Legacy event, don't uniquify. */ 4102 return false; 4103 } 4104 4105 if (counter->pmu && counter->pmu->is_core && 4106 counter->alternate_hw_config != PERF_COUNT_HW_MAX) { 4107 /* A sysfs or json event replacing a legacy event, don't uniquify. */ 4108 return false; 4109 } 4110 4111 if (config->aggr_mode == AGGR_NONE) { 4112 /* Always unique with no aggregation. */ 4113 counter->needs_uniquify = true; 4114 return true; 4115 } 4116 4117 if (counter->first_wildcard_match != NULL) { 4118 /* 4119 * If stats are merged then only the first_wildcard_match is 4120 * displayed, there is no need to uniquify this evsel as the 4121 * name won't be shown. 4122 */ 4123 return false; 4124 } 4125 4126 /* 4127 * Do other non-merged events in the evlist have the same name? If so 4128 * uniquify is necessary. 4129 */ 4130 evlist__for_each_entry(counter->evlist, evsel) { 4131 if (evsel == counter || evsel->first_wildcard_match || evsel->pmu == counter->pmu) 4132 continue; 4133 4134 if (evsel__name_is(counter, evsel__name(evsel))) { 4135 counter->needs_uniquify = true; 4136 return true; 4137 } 4138 } 4139 return false; 4140 } 4141 4142 void evsel__uniquify_counter(struct evsel *counter) 4143 { 4144 const char *name, *pmu_name, *config; 4145 char *new_name; 4146 int len, ret; 4147 4148 /* No uniquification necessary. */ 4149 if (!counter->needs_uniquify) 4150 return; 4151 4152 /* The evsel was already uniquified. */ 4153 if (counter->uniquified_name) 4154 return; 4155 4156 /* Avoid checking to uniquify twice. */ 4157 counter->uniquified_name = true; 4158 4159 name = evsel__name(counter); 4160 config = strchr(name, '/'); 4161 pmu_name = counter->pmu->name; 4162 4163 /* Already prefixed by the PMU name? */ 4164 len = pmu_name_len_no_suffix(pmu_name); 4165 4166 if (!strncmp(name, pmu_name, len)) { 4167 /* 4168 * If the PMU name is there, then there is no sense in not 4169 * having a slash. Do this for robustness. 4170 */ 4171 if (config == NULL) 4172 config = name - 1; 4173 4174 ret = asprintf(&new_name, "%s/%s", pmu_name, config + 1); 4175 } else if (config) { 4176 len = config - name; 4177 if (config[1] == '/') { 4178 /* case: event// */ 4179 ret = asprintf(&new_name, "%s/%.*s/%s", pmu_name, len, name, config + 2); 4180 } else { 4181 /* case: event/.../ */ 4182 ret = asprintf(&new_name, "%s/%.*s,%s", pmu_name, len, name, config + 1); 4183 } 4184 } else { 4185 config = strchr(name, ':'); 4186 if (config) { 4187 /* case: event:.. */ 4188 len = config - name; 4189 4190 ret = asprintf(&new_name, "%s/%.*s/%s", pmu_name, len, name, config + 1); 4191 } else { 4192 /* case: event */ 4193 ret = asprintf(&new_name, "%s/%s/", pmu_name, name); 4194 } 4195 } 4196 if (ret > 0) { 4197 free(counter->name); 4198 counter->name = new_name; 4199 } else { 4200 /* ENOMEM from asprintf. */ 4201 counter->uniquified_name = false; 4202 } 4203 } 4204 4205 void evsel__warn_user_requested_cpus(struct evsel *evsel, struct perf_cpu_map *user_requested_cpus) 4206 { 4207 struct perf_cpu_map *intersect, *online = NULL; 4208 const struct perf_pmu *pmu = evsel__find_pmu(evsel); 4209 4210 if (pmu && pmu->is_core) { 4211 intersect = perf_cpu_map__intersect(pmu->cpus, user_requested_cpus); 4212 } else { 4213 online = cpu_map__online(); 4214 intersect = perf_cpu_map__intersect(online, user_requested_cpus); 4215 } 4216 if (!perf_cpu_map__equal(intersect, user_requested_cpus)) { 4217 char buf1[128]; 4218 char buf2[128]; 4219 4220 cpu_map__snprint(user_requested_cpus, buf1, sizeof(buf1)); 4221 cpu_map__snprint(online ?: pmu->cpus, buf2, sizeof(buf2)); 4222 pr_warning("WARNING: A requested CPU in '%s' is not supported by PMU '%s' (CPUs %s) for event '%s'\n", 4223 buf1, pmu ? pmu->name : "cpu", buf2, evsel__name(evsel)); 4224 } 4225 perf_cpu_map__put(intersect); 4226 perf_cpu_map__put(online); 4227 } 4228