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