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