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