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