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