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