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/fs/fs.h> 14 #include <api/fs/tracing_path.h> 15 #include <traceevent/event-parse.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 "util/env.h" 35 #include "util/evsel_config.h" 36 #include "util/evsel_fprintf.h" 37 #include "evlist.h" 38 #include <perf/cpumap.h> 39 #include "thread_map.h" 40 #include "target.h" 41 #include "perf_regs.h" 42 #include "record.h" 43 #include "debug.h" 44 #include "trace-event.h" 45 #include "stat.h" 46 #include "string2.h" 47 #include "memswap.h" 48 #include "util.h" 49 #include "../perf-sys.h" 50 #include "util/parse-branch-options.h" 51 #include <internal/xyarray.h> 52 #include <internal/lib.h> 53 54 #include <linux/ctype.h> 55 56 struct perf_missing_features perf_missing_features; 57 58 static clockid_t clockid; 59 60 static int evsel__no_extra_init(struct evsel *evsel __maybe_unused) 61 { 62 return 0; 63 } 64 65 void __weak test_attr__ready(void) { } 66 67 static void evsel__no_extra_fini(struct evsel *evsel __maybe_unused) 68 { 69 } 70 71 static struct { 72 size_t size; 73 int (*init)(struct evsel *evsel); 74 void (*fini)(struct evsel *evsel); 75 } perf_evsel__object = { 76 .size = sizeof(struct evsel), 77 .init = evsel__no_extra_init, 78 .fini = evsel__no_extra_fini, 79 }; 80 81 int evsel__object_config(size_t object_size, int (*init)(struct evsel *evsel), 82 void (*fini)(struct evsel *evsel)) 83 { 84 85 if (object_size == 0) 86 goto set_methods; 87 88 if (perf_evsel__object.size > object_size) 89 return -EINVAL; 90 91 perf_evsel__object.size = object_size; 92 93 set_methods: 94 if (init != NULL) 95 perf_evsel__object.init = init; 96 97 if (fini != NULL) 98 perf_evsel__object.fini = fini; 99 100 return 0; 101 } 102 103 #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y)) 104 105 int __evsel__sample_size(u64 sample_type) 106 { 107 u64 mask = sample_type & PERF_SAMPLE_MASK; 108 int size = 0; 109 int i; 110 111 for (i = 0; i < 64; i++) { 112 if (mask & (1ULL << i)) 113 size++; 114 } 115 116 size *= sizeof(u64); 117 118 return size; 119 } 120 121 /** 122 * __perf_evsel__calc_id_pos - calculate id_pos. 123 * @sample_type: sample type 124 * 125 * This function returns the position of the event id (PERF_SAMPLE_ID or 126 * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of struct 127 * perf_record_sample. 128 */ 129 static int __perf_evsel__calc_id_pos(u64 sample_type) 130 { 131 int idx = 0; 132 133 if (sample_type & PERF_SAMPLE_IDENTIFIER) 134 return 0; 135 136 if (!(sample_type & PERF_SAMPLE_ID)) 137 return -1; 138 139 if (sample_type & PERF_SAMPLE_IP) 140 idx += 1; 141 142 if (sample_type & PERF_SAMPLE_TID) 143 idx += 1; 144 145 if (sample_type & PERF_SAMPLE_TIME) 146 idx += 1; 147 148 if (sample_type & PERF_SAMPLE_ADDR) 149 idx += 1; 150 151 return idx; 152 } 153 154 /** 155 * __perf_evsel__calc_is_pos - calculate is_pos. 156 * @sample_type: sample type 157 * 158 * This function returns the position (counting backwards) of the event id 159 * (PERF_SAMPLE_ID or PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if 160 * sample_id_all is used there is an id sample appended to non-sample events. 161 */ 162 static int __perf_evsel__calc_is_pos(u64 sample_type) 163 { 164 int idx = 1; 165 166 if (sample_type & PERF_SAMPLE_IDENTIFIER) 167 return 1; 168 169 if (!(sample_type & PERF_SAMPLE_ID)) 170 return -1; 171 172 if (sample_type & PERF_SAMPLE_CPU) 173 idx += 1; 174 175 if (sample_type & PERF_SAMPLE_STREAM_ID) 176 idx += 1; 177 178 return idx; 179 } 180 181 void evsel__calc_id_pos(struct evsel *evsel) 182 { 183 evsel->id_pos = __perf_evsel__calc_id_pos(evsel->core.attr.sample_type); 184 evsel->is_pos = __perf_evsel__calc_is_pos(evsel->core.attr.sample_type); 185 } 186 187 void __evsel__set_sample_bit(struct evsel *evsel, 188 enum perf_event_sample_format bit) 189 { 190 if (!(evsel->core.attr.sample_type & bit)) { 191 evsel->core.attr.sample_type |= bit; 192 evsel->sample_size += sizeof(u64); 193 evsel__calc_id_pos(evsel); 194 } 195 } 196 197 void __evsel__reset_sample_bit(struct evsel *evsel, 198 enum perf_event_sample_format bit) 199 { 200 if (evsel->core.attr.sample_type & bit) { 201 evsel->core.attr.sample_type &= ~bit; 202 evsel->sample_size -= sizeof(u64); 203 evsel__calc_id_pos(evsel); 204 } 205 } 206 207 void evsel__set_sample_id(struct evsel *evsel, 208 bool can_sample_identifier) 209 { 210 if (can_sample_identifier) { 211 evsel__reset_sample_bit(evsel, ID); 212 evsel__set_sample_bit(evsel, IDENTIFIER); 213 } else { 214 evsel__set_sample_bit(evsel, ID); 215 } 216 evsel->core.attr.read_format |= PERF_FORMAT_ID; 217 } 218 219 /** 220 * evsel__is_function_event - Return whether given evsel is a function 221 * trace event 222 * 223 * @evsel - evsel selector to be tested 224 * 225 * Return %true if event is function trace event 226 */ 227 bool evsel__is_function_event(struct evsel *evsel) 228 { 229 #define FUNCTION_EVENT "ftrace:function" 230 231 return evsel->name && 232 !strncmp(FUNCTION_EVENT, evsel->name, sizeof(FUNCTION_EVENT)); 233 234 #undef FUNCTION_EVENT 235 } 236 237 void evsel__init(struct evsel *evsel, 238 struct perf_event_attr *attr, int idx) 239 { 240 perf_evsel__init(&evsel->core, attr); 241 evsel->idx = idx; 242 evsel->tracking = !idx; 243 evsel->leader = evsel; 244 evsel->unit = ""; 245 evsel->scale = 1.0; 246 evsel->max_events = ULONG_MAX; 247 evsel->evlist = NULL; 248 evsel->bpf_obj = NULL; 249 evsel->bpf_fd = -1; 250 INIT_LIST_HEAD(&evsel->config_terms); 251 INIT_LIST_HEAD(&evsel->bpf_counter_list); 252 perf_evsel__object.init(evsel); 253 evsel->sample_size = __evsel__sample_size(attr->sample_type); 254 evsel__calc_id_pos(evsel); 255 evsel->cmdline_group_boundary = false; 256 evsel->metric_expr = NULL; 257 evsel->metric_name = NULL; 258 evsel->metric_events = NULL; 259 evsel->per_pkg_mask = NULL; 260 evsel->collect_stat = false; 261 evsel->pmu_name = NULL; 262 } 263 264 struct evsel *evsel__new_idx(struct perf_event_attr *attr, int idx) 265 { 266 struct evsel *evsel = zalloc(perf_evsel__object.size); 267 268 if (!evsel) 269 return NULL; 270 evsel__init(evsel, attr, idx); 271 272 if (evsel__is_bpf_output(evsel)) { 273 evsel->core.attr.sample_type |= (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME | 274 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD), 275 evsel->core.attr.sample_period = 1; 276 } 277 278 if (evsel__is_clock(evsel)) { 279 /* 280 * The evsel->unit points to static alias->unit 281 * so it's ok to use static string in here. 282 */ 283 static const char *unit = "msec"; 284 285 evsel->unit = unit; 286 evsel->scale = 1e-6; 287 } 288 289 return evsel; 290 } 291 292 static bool perf_event_can_profile_kernel(void) 293 { 294 return perf_event_paranoid_check(1); 295 } 296 297 struct evsel *evsel__new_cycles(bool precise) 298 { 299 struct perf_event_attr attr = { 300 .type = PERF_TYPE_HARDWARE, 301 .config = PERF_COUNT_HW_CPU_CYCLES, 302 .exclude_kernel = !perf_event_can_profile_kernel(), 303 }; 304 struct evsel *evsel; 305 306 event_attr_init(&attr); 307 308 if (!precise) 309 goto new_event; 310 311 /* 312 * Now let the usual logic to set up the perf_event_attr defaults 313 * to kick in when we return and before perf_evsel__open() is called. 314 */ 315 new_event: 316 evsel = evsel__new(&attr); 317 if (evsel == NULL) 318 goto out; 319 320 evsel->precise_max = true; 321 322 /* use asprintf() because free(evsel) assumes name is allocated */ 323 if (asprintf(&evsel->name, "cycles%s%s%.*s", 324 (attr.precise_ip || attr.exclude_kernel) ? ":" : "", 325 attr.exclude_kernel ? "u" : "", 326 attr.precise_ip ? attr.precise_ip + 1 : 0, "ppp") < 0) 327 goto error_free; 328 out: 329 return evsel; 330 error_free: 331 evsel__delete(evsel); 332 evsel = NULL; 333 goto out; 334 } 335 336 static int evsel__copy_config_terms(struct evsel *dst, struct evsel *src) 337 { 338 struct evsel_config_term *pos, *tmp; 339 340 list_for_each_entry(pos, &src->config_terms, list) { 341 tmp = malloc(sizeof(*tmp)); 342 if (tmp == NULL) 343 return -ENOMEM; 344 345 *tmp = *pos; 346 if (tmp->free_str) { 347 tmp->val.str = strdup(pos->val.str); 348 if (tmp->val.str == NULL) { 349 free(tmp); 350 return -ENOMEM; 351 } 352 } 353 list_add_tail(&tmp->list, &dst->config_terms); 354 } 355 return 0; 356 } 357 358 /** 359 * evsel__clone - create a new evsel copied from @orig 360 * @orig: original evsel 361 * 362 * The assumption is that @orig is not configured nor opened yet. 363 * So we only care about the attributes that can be set while it's parsed. 364 */ 365 struct evsel *evsel__clone(struct evsel *orig) 366 { 367 struct evsel *evsel; 368 369 BUG_ON(orig->core.fd); 370 BUG_ON(orig->counts); 371 BUG_ON(orig->priv); 372 BUG_ON(orig->per_pkg_mask); 373 374 /* cannot handle BPF objects for now */ 375 if (orig->bpf_obj) 376 return NULL; 377 378 evsel = evsel__new(&orig->core.attr); 379 if (evsel == NULL) 380 return NULL; 381 382 evsel->core.cpus = perf_cpu_map__get(orig->core.cpus); 383 evsel->core.own_cpus = perf_cpu_map__get(orig->core.own_cpus); 384 evsel->core.threads = perf_thread_map__get(orig->core.threads); 385 evsel->core.nr_members = orig->core.nr_members; 386 evsel->core.system_wide = orig->core.system_wide; 387 388 if (orig->name) { 389 evsel->name = strdup(orig->name); 390 if (evsel->name == NULL) 391 goto out_err; 392 } 393 if (orig->group_name) { 394 evsel->group_name = strdup(orig->group_name); 395 if (evsel->group_name == NULL) 396 goto out_err; 397 } 398 if (orig->pmu_name) { 399 evsel->pmu_name = strdup(orig->pmu_name); 400 if (evsel->pmu_name == NULL) 401 goto out_err; 402 } 403 if (orig->filter) { 404 evsel->filter = strdup(orig->filter); 405 if (evsel->filter == NULL) 406 goto out_err; 407 } 408 evsel->cgrp = cgroup__get(orig->cgrp); 409 evsel->tp_format = orig->tp_format; 410 evsel->handler = orig->handler; 411 evsel->leader = orig->leader; 412 413 evsel->max_events = orig->max_events; 414 evsel->tool_event = orig->tool_event; 415 evsel->unit = orig->unit; 416 evsel->scale = orig->scale; 417 evsel->snapshot = orig->snapshot; 418 evsel->per_pkg = orig->per_pkg; 419 evsel->percore = orig->percore; 420 evsel->precise_max = orig->precise_max; 421 evsel->use_uncore_alias = orig->use_uncore_alias; 422 evsel->is_libpfm_event = orig->is_libpfm_event; 423 424 evsel->exclude_GH = orig->exclude_GH; 425 evsel->sample_read = orig->sample_read; 426 evsel->auto_merge_stats = orig->auto_merge_stats; 427 evsel->collect_stat = orig->collect_stat; 428 evsel->weak_group = orig->weak_group; 429 430 if (evsel__copy_config_terms(evsel, orig) < 0) 431 goto out_err; 432 433 return evsel; 434 435 out_err: 436 evsel__delete(evsel); 437 return NULL; 438 } 439 440 /* 441 * Returns pointer with encoded error via <linux/err.h> interface. 442 */ 443 struct evsel *evsel__newtp_idx(const char *sys, const char *name, int idx) 444 { 445 struct evsel *evsel = zalloc(perf_evsel__object.size); 446 int err = -ENOMEM; 447 448 if (evsel == NULL) { 449 goto out_err; 450 } else { 451 struct perf_event_attr attr = { 452 .type = PERF_TYPE_TRACEPOINT, 453 .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME | 454 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD), 455 }; 456 457 if (asprintf(&evsel->name, "%s:%s", sys, name) < 0) 458 goto out_free; 459 460 evsel->tp_format = trace_event__tp_format(sys, name); 461 if (IS_ERR(evsel->tp_format)) { 462 err = PTR_ERR(evsel->tp_format); 463 goto out_free; 464 } 465 466 event_attr_init(&attr); 467 attr.config = evsel->tp_format->id; 468 attr.sample_period = 1; 469 evsel__init(evsel, &attr, idx); 470 } 471 472 return evsel; 473 474 out_free: 475 zfree(&evsel->name); 476 free(evsel); 477 out_err: 478 return ERR_PTR(err); 479 } 480 481 const char *evsel__hw_names[PERF_COUNT_HW_MAX] = { 482 "cycles", 483 "instructions", 484 "cache-references", 485 "cache-misses", 486 "branches", 487 "branch-misses", 488 "bus-cycles", 489 "stalled-cycles-frontend", 490 "stalled-cycles-backend", 491 "ref-cycles", 492 }; 493 494 static const char *__evsel__hw_name(u64 config) 495 { 496 if (config < PERF_COUNT_HW_MAX && evsel__hw_names[config]) 497 return evsel__hw_names[config]; 498 499 return "unknown-hardware"; 500 } 501 502 static int evsel__add_modifiers(struct evsel *evsel, char *bf, size_t size) 503 { 504 int colon = 0, r = 0; 505 struct perf_event_attr *attr = &evsel->core.attr; 506 bool exclude_guest_default = false; 507 508 #define MOD_PRINT(context, mod) do { \ 509 if (!attr->exclude_##context) { \ 510 if (!colon) colon = ++r; \ 511 r += scnprintf(bf + r, size - r, "%c", mod); \ 512 } } while(0) 513 514 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) { 515 MOD_PRINT(kernel, 'k'); 516 MOD_PRINT(user, 'u'); 517 MOD_PRINT(hv, 'h'); 518 exclude_guest_default = true; 519 } 520 521 if (attr->precise_ip) { 522 if (!colon) 523 colon = ++r; 524 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp"); 525 exclude_guest_default = true; 526 } 527 528 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) { 529 MOD_PRINT(host, 'H'); 530 MOD_PRINT(guest, 'G'); 531 } 532 #undef MOD_PRINT 533 if (colon) 534 bf[colon - 1] = ':'; 535 return r; 536 } 537 538 static int evsel__hw_name(struct evsel *evsel, char *bf, size_t size) 539 { 540 int r = scnprintf(bf, size, "%s", __evsel__hw_name(evsel->core.attr.config)); 541 return r + evsel__add_modifiers(evsel, bf + r, size - r); 542 } 543 544 const char *evsel__sw_names[PERF_COUNT_SW_MAX] = { 545 "cpu-clock", 546 "task-clock", 547 "page-faults", 548 "context-switches", 549 "cpu-migrations", 550 "minor-faults", 551 "major-faults", 552 "alignment-faults", 553 "emulation-faults", 554 "dummy", 555 }; 556 557 static const char *__evsel__sw_name(u64 config) 558 { 559 if (config < PERF_COUNT_SW_MAX && evsel__sw_names[config]) 560 return evsel__sw_names[config]; 561 return "unknown-software"; 562 } 563 564 static int evsel__sw_name(struct evsel *evsel, char *bf, size_t size) 565 { 566 int r = scnprintf(bf, size, "%s", __evsel__sw_name(evsel->core.attr.config)); 567 return r + evsel__add_modifiers(evsel, bf + r, size - r); 568 } 569 570 static int __evsel__bp_name(char *bf, size_t size, u64 addr, u64 type) 571 { 572 int r; 573 574 r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr); 575 576 if (type & HW_BREAKPOINT_R) 577 r += scnprintf(bf + r, size - r, "r"); 578 579 if (type & HW_BREAKPOINT_W) 580 r += scnprintf(bf + r, size - r, "w"); 581 582 if (type & HW_BREAKPOINT_X) 583 r += scnprintf(bf + r, size - r, "x"); 584 585 return r; 586 } 587 588 static int evsel__bp_name(struct evsel *evsel, char *bf, size_t size) 589 { 590 struct perf_event_attr *attr = &evsel->core.attr; 591 int r = __evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type); 592 return r + evsel__add_modifiers(evsel, bf + r, size - r); 593 } 594 595 const char *evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX][EVSEL__MAX_ALIASES] = { 596 { "L1-dcache", "l1-d", "l1d", "L1-data", }, 597 { "L1-icache", "l1-i", "l1i", "L1-instruction", }, 598 { "LLC", "L2", }, 599 { "dTLB", "d-tlb", "Data-TLB", }, 600 { "iTLB", "i-tlb", "Instruction-TLB", }, 601 { "branch", "branches", "bpu", "btb", "bpc", }, 602 { "node", }, 603 }; 604 605 const char *evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX][EVSEL__MAX_ALIASES] = { 606 { "load", "loads", "read", }, 607 { "store", "stores", "write", }, 608 { "prefetch", "prefetches", "speculative-read", "speculative-load", }, 609 }; 610 611 const char *evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX][EVSEL__MAX_ALIASES] = { 612 { "refs", "Reference", "ops", "access", }, 613 { "misses", "miss", }, 614 }; 615 616 #define C(x) PERF_COUNT_HW_CACHE_##x 617 #define CACHE_READ (1 << C(OP_READ)) 618 #define CACHE_WRITE (1 << C(OP_WRITE)) 619 #define CACHE_PREFETCH (1 << C(OP_PREFETCH)) 620 #define COP(x) (1 << x) 621 622 /* 623 * cache operartion stat 624 * L1I : Read and prefetch only 625 * ITLB and BPU : Read-only 626 */ 627 static unsigned long evsel__hw_cache_stat[C(MAX)] = { 628 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH), 629 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH), 630 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH), 631 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH), 632 [C(ITLB)] = (CACHE_READ), 633 [C(BPU)] = (CACHE_READ), 634 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH), 635 }; 636 637 bool evsel__is_cache_op_valid(u8 type, u8 op) 638 { 639 if (evsel__hw_cache_stat[type] & COP(op)) 640 return true; /* valid */ 641 else 642 return false; /* invalid */ 643 } 644 645 int __evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result, char *bf, size_t size) 646 { 647 if (result) { 648 return scnprintf(bf, size, "%s-%s-%s", evsel__hw_cache[type][0], 649 evsel__hw_cache_op[op][0], 650 evsel__hw_cache_result[result][0]); 651 } 652 653 return scnprintf(bf, size, "%s-%s", evsel__hw_cache[type][0], 654 evsel__hw_cache_op[op][1]); 655 } 656 657 static int __evsel__hw_cache_name(u64 config, char *bf, size_t size) 658 { 659 u8 op, result, type = (config >> 0) & 0xff; 660 const char *err = "unknown-ext-hardware-cache-type"; 661 662 if (type >= PERF_COUNT_HW_CACHE_MAX) 663 goto out_err; 664 665 op = (config >> 8) & 0xff; 666 err = "unknown-ext-hardware-cache-op"; 667 if (op >= PERF_COUNT_HW_CACHE_OP_MAX) 668 goto out_err; 669 670 result = (config >> 16) & 0xff; 671 err = "unknown-ext-hardware-cache-result"; 672 if (result >= PERF_COUNT_HW_CACHE_RESULT_MAX) 673 goto out_err; 674 675 err = "invalid-cache"; 676 if (!evsel__is_cache_op_valid(type, op)) 677 goto out_err; 678 679 return __evsel__hw_cache_type_op_res_name(type, op, result, bf, size); 680 out_err: 681 return scnprintf(bf, size, "%s", err); 682 } 683 684 static int evsel__hw_cache_name(struct evsel *evsel, char *bf, size_t size) 685 { 686 int ret = __evsel__hw_cache_name(evsel->core.attr.config, bf, size); 687 return ret + evsel__add_modifiers(evsel, bf + ret, size - ret); 688 } 689 690 static int evsel__raw_name(struct evsel *evsel, char *bf, size_t size) 691 { 692 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->core.attr.config); 693 return ret + evsel__add_modifiers(evsel, bf + ret, size - ret); 694 } 695 696 static int evsel__tool_name(char *bf, size_t size) 697 { 698 int ret = scnprintf(bf, size, "duration_time"); 699 return ret; 700 } 701 702 const char *evsel__name(struct evsel *evsel) 703 { 704 char bf[128]; 705 706 if (!evsel) 707 goto out_unknown; 708 709 if (evsel->name) 710 return evsel->name; 711 712 switch (evsel->core.attr.type) { 713 case PERF_TYPE_RAW: 714 evsel__raw_name(evsel, bf, sizeof(bf)); 715 break; 716 717 case PERF_TYPE_HARDWARE: 718 evsel__hw_name(evsel, bf, sizeof(bf)); 719 break; 720 721 case PERF_TYPE_HW_CACHE: 722 evsel__hw_cache_name(evsel, bf, sizeof(bf)); 723 break; 724 725 case PERF_TYPE_SOFTWARE: 726 if (evsel->tool_event) 727 evsel__tool_name(bf, sizeof(bf)); 728 else 729 evsel__sw_name(evsel, bf, sizeof(bf)); 730 break; 731 732 case PERF_TYPE_TRACEPOINT: 733 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint"); 734 break; 735 736 case PERF_TYPE_BREAKPOINT: 737 evsel__bp_name(evsel, bf, sizeof(bf)); 738 break; 739 740 default: 741 scnprintf(bf, sizeof(bf), "unknown attr type: %d", 742 evsel->core.attr.type); 743 break; 744 } 745 746 evsel->name = strdup(bf); 747 748 if (evsel->name) 749 return evsel->name; 750 out_unknown: 751 return "unknown"; 752 } 753 754 const char *evsel__group_name(struct evsel *evsel) 755 { 756 return evsel->group_name ?: "anon group"; 757 } 758 759 /* 760 * Returns the group details for the specified leader, 761 * with following rules. 762 * 763 * For record -e '{cycles,instructions}' 764 * 'anon group { cycles:u, instructions:u }' 765 * 766 * For record -e 'cycles,instructions' and report --group 767 * 'cycles:u, instructions:u' 768 */ 769 int evsel__group_desc(struct evsel *evsel, char *buf, size_t size) 770 { 771 int ret = 0; 772 struct evsel *pos; 773 const char *group_name = evsel__group_name(evsel); 774 775 if (!evsel->forced_leader) 776 ret = scnprintf(buf, size, "%s { ", group_name); 777 778 ret += scnprintf(buf + ret, size - ret, "%s", evsel__name(evsel)); 779 780 for_each_group_member(pos, evsel) 781 ret += scnprintf(buf + ret, size - ret, ", %s", evsel__name(pos)); 782 783 if (!evsel->forced_leader) 784 ret += scnprintf(buf + ret, size - ret, " }"); 785 786 return ret; 787 } 788 789 static void __evsel__config_callchain(struct evsel *evsel, struct record_opts *opts, 790 struct callchain_param *param) 791 { 792 bool function = evsel__is_function_event(evsel); 793 struct perf_event_attr *attr = &evsel->core.attr; 794 795 evsel__set_sample_bit(evsel, CALLCHAIN); 796 797 attr->sample_max_stack = param->max_stack; 798 799 if (opts->kernel_callchains) 800 attr->exclude_callchain_user = 1; 801 if (opts->user_callchains) 802 attr->exclude_callchain_kernel = 1; 803 if (param->record_mode == CALLCHAIN_LBR) { 804 if (!opts->branch_stack) { 805 if (attr->exclude_user) { 806 pr_warning("LBR callstack option is only available " 807 "to get user callchain information. " 808 "Falling back to framepointers.\n"); 809 } else { 810 evsel__set_sample_bit(evsel, BRANCH_STACK); 811 attr->branch_sample_type = PERF_SAMPLE_BRANCH_USER | 812 PERF_SAMPLE_BRANCH_CALL_STACK | 813 PERF_SAMPLE_BRANCH_NO_CYCLES | 814 PERF_SAMPLE_BRANCH_NO_FLAGS | 815 PERF_SAMPLE_BRANCH_HW_INDEX; 816 } 817 } else 818 pr_warning("Cannot use LBR callstack with branch stack. " 819 "Falling back to framepointers.\n"); 820 } 821 822 if (param->record_mode == CALLCHAIN_DWARF) { 823 if (!function) { 824 evsel__set_sample_bit(evsel, REGS_USER); 825 evsel__set_sample_bit(evsel, STACK_USER); 826 if (opts->sample_user_regs && DWARF_MINIMAL_REGS != PERF_REGS_MASK) { 827 attr->sample_regs_user |= DWARF_MINIMAL_REGS; 828 pr_warning("WARNING: The use of --call-graph=dwarf may require all the user registers, " 829 "specifying a subset with --user-regs may render DWARF unwinding unreliable, " 830 "so the minimal registers set (IP, SP) is explicitly forced.\n"); 831 } else { 832 attr->sample_regs_user |= PERF_REGS_MASK; 833 } 834 attr->sample_stack_user = param->dump_size; 835 attr->exclude_callchain_user = 1; 836 } else { 837 pr_info("Cannot use DWARF unwind for function trace event," 838 " falling back to framepointers.\n"); 839 } 840 } 841 842 if (function) { 843 pr_info("Disabling user space callchains for function trace event.\n"); 844 attr->exclude_callchain_user = 1; 845 } 846 } 847 848 void evsel__config_callchain(struct evsel *evsel, struct record_opts *opts, 849 struct callchain_param *param) 850 { 851 if (param->enabled) 852 return __evsel__config_callchain(evsel, opts, param); 853 } 854 855 static void evsel__reset_callgraph(struct evsel *evsel, struct callchain_param *param) 856 { 857 struct perf_event_attr *attr = &evsel->core.attr; 858 859 evsel__reset_sample_bit(evsel, CALLCHAIN); 860 if (param->record_mode == CALLCHAIN_LBR) { 861 evsel__reset_sample_bit(evsel, BRANCH_STACK); 862 attr->branch_sample_type &= ~(PERF_SAMPLE_BRANCH_USER | 863 PERF_SAMPLE_BRANCH_CALL_STACK | 864 PERF_SAMPLE_BRANCH_HW_INDEX); 865 } 866 if (param->record_mode == CALLCHAIN_DWARF) { 867 evsel__reset_sample_bit(evsel, REGS_USER); 868 evsel__reset_sample_bit(evsel, STACK_USER); 869 } 870 } 871 872 static void evsel__apply_config_terms(struct evsel *evsel, 873 struct record_opts *opts, bool track) 874 { 875 struct evsel_config_term *term; 876 struct list_head *config_terms = &evsel->config_terms; 877 struct perf_event_attr *attr = &evsel->core.attr; 878 /* callgraph default */ 879 struct callchain_param param = { 880 .record_mode = callchain_param.record_mode, 881 }; 882 u32 dump_size = 0; 883 int max_stack = 0; 884 const char *callgraph_buf = NULL; 885 886 list_for_each_entry(term, config_terms, list) { 887 switch (term->type) { 888 case EVSEL__CONFIG_TERM_PERIOD: 889 if (!(term->weak && opts->user_interval != ULLONG_MAX)) { 890 attr->sample_period = term->val.period; 891 attr->freq = 0; 892 evsel__reset_sample_bit(evsel, PERIOD); 893 } 894 break; 895 case EVSEL__CONFIG_TERM_FREQ: 896 if (!(term->weak && opts->user_freq != UINT_MAX)) { 897 attr->sample_freq = term->val.freq; 898 attr->freq = 1; 899 evsel__set_sample_bit(evsel, PERIOD); 900 } 901 break; 902 case EVSEL__CONFIG_TERM_TIME: 903 if (term->val.time) 904 evsel__set_sample_bit(evsel, TIME); 905 else 906 evsel__reset_sample_bit(evsel, TIME); 907 break; 908 case EVSEL__CONFIG_TERM_CALLGRAPH: 909 callgraph_buf = term->val.str; 910 break; 911 case EVSEL__CONFIG_TERM_BRANCH: 912 if (term->val.str && strcmp(term->val.str, "no")) { 913 evsel__set_sample_bit(evsel, BRANCH_STACK); 914 parse_branch_str(term->val.str, 915 &attr->branch_sample_type); 916 } else 917 evsel__reset_sample_bit(evsel, BRANCH_STACK); 918 break; 919 case EVSEL__CONFIG_TERM_STACK_USER: 920 dump_size = term->val.stack_user; 921 break; 922 case EVSEL__CONFIG_TERM_MAX_STACK: 923 max_stack = term->val.max_stack; 924 break; 925 case EVSEL__CONFIG_TERM_MAX_EVENTS: 926 evsel->max_events = term->val.max_events; 927 break; 928 case EVSEL__CONFIG_TERM_INHERIT: 929 /* 930 * attr->inherit should has already been set by 931 * evsel__config. If user explicitly set 932 * inherit using config terms, override global 933 * opt->no_inherit setting. 934 */ 935 attr->inherit = term->val.inherit ? 1 : 0; 936 break; 937 case EVSEL__CONFIG_TERM_OVERWRITE: 938 attr->write_backward = term->val.overwrite ? 1 : 0; 939 break; 940 case EVSEL__CONFIG_TERM_DRV_CFG: 941 break; 942 case EVSEL__CONFIG_TERM_PERCORE: 943 break; 944 case EVSEL__CONFIG_TERM_AUX_OUTPUT: 945 attr->aux_output = term->val.aux_output ? 1 : 0; 946 break; 947 case EVSEL__CONFIG_TERM_AUX_SAMPLE_SIZE: 948 /* Already applied by auxtrace */ 949 break; 950 case EVSEL__CONFIG_TERM_CFG_CHG: 951 break; 952 default: 953 break; 954 } 955 } 956 957 /* User explicitly set per-event callgraph, clear the old setting and reset. */ 958 if ((callgraph_buf != NULL) || (dump_size > 0) || max_stack) { 959 bool sample_address = false; 960 961 if (max_stack) { 962 param.max_stack = max_stack; 963 if (callgraph_buf == NULL) 964 callgraph_buf = "fp"; 965 } 966 967 /* parse callgraph parameters */ 968 if (callgraph_buf != NULL) { 969 if (!strcmp(callgraph_buf, "no")) { 970 param.enabled = false; 971 param.record_mode = CALLCHAIN_NONE; 972 } else { 973 param.enabled = true; 974 if (parse_callchain_record(callgraph_buf, ¶m)) { 975 pr_err("per-event callgraph setting for %s failed. " 976 "Apply callgraph global setting for it\n", 977 evsel->name); 978 return; 979 } 980 if (param.record_mode == CALLCHAIN_DWARF) 981 sample_address = true; 982 } 983 } 984 if (dump_size > 0) { 985 dump_size = round_up(dump_size, sizeof(u64)); 986 param.dump_size = dump_size; 987 } 988 989 /* If global callgraph set, clear it */ 990 if (callchain_param.enabled) 991 evsel__reset_callgraph(evsel, &callchain_param); 992 993 /* set perf-event callgraph */ 994 if (param.enabled) { 995 if (sample_address) { 996 evsel__set_sample_bit(evsel, ADDR); 997 evsel__set_sample_bit(evsel, DATA_SRC); 998 evsel->core.attr.mmap_data = track; 999 } 1000 evsel__config_callchain(evsel, opts, ¶m); 1001 } 1002 } 1003 } 1004 1005 struct evsel_config_term *__evsel__get_config_term(struct evsel *evsel, enum evsel_term_type type) 1006 { 1007 struct evsel_config_term *term, *found_term = NULL; 1008 1009 list_for_each_entry(term, &evsel->config_terms, list) { 1010 if (term->type == type) 1011 found_term = term; 1012 } 1013 1014 return found_term; 1015 } 1016 1017 void __weak arch_evsel__set_sample_weight(struct evsel *evsel) 1018 { 1019 evsel__set_sample_bit(evsel, WEIGHT); 1020 } 1021 1022 /* 1023 * The enable_on_exec/disabled value strategy: 1024 * 1025 * 1) For any type of traced program: 1026 * - all independent events and group leaders are disabled 1027 * - all group members are enabled 1028 * 1029 * Group members are ruled by group leaders. They need to 1030 * be enabled, because the group scheduling relies on that. 1031 * 1032 * 2) For traced programs executed by perf: 1033 * - all independent events and group leaders have 1034 * enable_on_exec set 1035 * - we don't specifically enable or disable any event during 1036 * the record command 1037 * 1038 * Independent events and group leaders are initially disabled 1039 * and get enabled by exec. Group members are ruled by group 1040 * leaders as stated in 1). 1041 * 1042 * 3) For traced programs attached by perf (pid/tid): 1043 * - we specifically enable or disable all events during 1044 * the record command 1045 * 1046 * When attaching events to already running traced we 1047 * enable/disable events specifically, as there's no 1048 * initial traced exec call. 1049 */ 1050 void evsel__config(struct evsel *evsel, struct record_opts *opts, 1051 struct callchain_param *callchain) 1052 { 1053 struct evsel *leader = evsel->leader; 1054 struct perf_event_attr *attr = &evsel->core.attr; 1055 int track = evsel->tracking; 1056 bool per_cpu = opts->target.default_per_cpu && !opts->target.per_thread; 1057 1058 attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1; 1059 attr->inherit = !opts->no_inherit; 1060 attr->write_backward = opts->overwrite ? 1 : 0; 1061 1062 evsel__set_sample_bit(evsel, IP); 1063 evsel__set_sample_bit(evsel, TID); 1064 1065 if (evsel->sample_read) { 1066 evsel__set_sample_bit(evsel, READ); 1067 1068 /* 1069 * We need ID even in case of single event, because 1070 * PERF_SAMPLE_READ process ID specific data. 1071 */ 1072 evsel__set_sample_id(evsel, false); 1073 1074 /* 1075 * Apply group format only if we belong to group 1076 * with more than one members. 1077 */ 1078 if (leader->core.nr_members > 1) { 1079 attr->read_format |= PERF_FORMAT_GROUP; 1080 attr->inherit = 0; 1081 } 1082 } 1083 1084 /* 1085 * We default some events to have a default interval. But keep 1086 * it a weak assumption overridable by the user. 1087 */ 1088 if (!attr->sample_period) { 1089 if (opts->freq) { 1090 attr->freq = 1; 1091 attr->sample_freq = opts->freq; 1092 } else { 1093 attr->sample_period = opts->default_interval; 1094 } 1095 } 1096 /* 1097 * If attr->freq was set (here or earlier), ask for period 1098 * to be sampled. 1099 */ 1100 if (attr->freq) 1101 evsel__set_sample_bit(evsel, PERIOD); 1102 1103 if (opts->no_samples) 1104 attr->sample_freq = 0; 1105 1106 if (opts->inherit_stat) { 1107 evsel->core.attr.read_format |= 1108 PERF_FORMAT_TOTAL_TIME_ENABLED | 1109 PERF_FORMAT_TOTAL_TIME_RUNNING | 1110 PERF_FORMAT_ID; 1111 attr->inherit_stat = 1; 1112 } 1113 1114 if (opts->sample_address) { 1115 evsel__set_sample_bit(evsel, ADDR); 1116 attr->mmap_data = track; 1117 } 1118 1119 /* 1120 * We don't allow user space callchains for function trace 1121 * event, due to issues with page faults while tracing page 1122 * fault handler and its overall trickiness nature. 1123 */ 1124 if (evsel__is_function_event(evsel)) 1125 evsel->core.attr.exclude_callchain_user = 1; 1126 1127 if (callchain && callchain->enabled && !evsel->no_aux_samples) 1128 evsel__config_callchain(evsel, opts, callchain); 1129 1130 if (opts->sample_intr_regs && !evsel->no_aux_samples && 1131 !evsel__is_dummy_event(evsel)) { 1132 attr->sample_regs_intr = opts->sample_intr_regs; 1133 evsel__set_sample_bit(evsel, REGS_INTR); 1134 } 1135 1136 if (opts->sample_user_regs && !evsel->no_aux_samples && 1137 !evsel__is_dummy_event(evsel)) { 1138 attr->sample_regs_user |= opts->sample_user_regs; 1139 evsel__set_sample_bit(evsel, REGS_USER); 1140 } 1141 1142 if (target__has_cpu(&opts->target) || opts->sample_cpu) 1143 evsel__set_sample_bit(evsel, CPU); 1144 1145 /* 1146 * When the user explicitly disabled time don't force it here. 1147 */ 1148 if (opts->sample_time && 1149 (!perf_missing_features.sample_id_all && 1150 (!opts->no_inherit || target__has_cpu(&opts->target) || per_cpu || 1151 opts->sample_time_set))) 1152 evsel__set_sample_bit(evsel, TIME); 1153 1154 if (opts->raw_samples && !evsel->no_aux_samples) { 1155 evsel__set_sample_bit(evsel, TIME); 1156 evsel__set_sample_bit(evsel, RAW); 1157 evsel__set_sample_bit(evsel, CPU); 1158 } 1159 1160 if (opts->sample_address) 1161 evsel__set_sample_bit(evsel, DATA_SRC); 1162 1163 if (opts->sample_phys_addr) 1164 evsel__set_sample_bit(evsel, PHYS_ADDR); 1165 1166 if (opts->no_buffering) { 1167 attr->watermark = 0; 1168 attr->wakeup_events = 1; 1169 } 1170 if (opts->branch_stack && !evsel->no_aux_samples) { 1171 evsel__set_sample_bit(evsel, BRANCH_STACK); 1172 attr->branch_sample_type = opts->branch_stack; 1173 } 1174 1175 if (opts->sample_weight) 1176 arch_evsel__set_sample_weight(evsel); 1177 1178 attr->task = track; 1179 attr->mmap = track; 1180 attr->mmap2 = track && !perf_missing_features.mmap2; 1181 attr->comm = track; 1182 attr->build_id = track && opts->build_id; 1183 1184 /* 1185 * ksymbol is tracked separately with text poke because it needs to be 1186 * system wide and enabled immediately. 1187 */ 1188 if (!opts->text_poke) 1189 attr->ksymbol = track && !perf_missing_features.ksymbol; 1190 attr->bpf_event = track && !opts->no_bpf_event && !perf_missing_features.bpf; 1191 1192 if (opts->record_namespaces) 1193 attr->namespaces = track; 1194 1195 if (opts->record_cgroup) { 1196 attr->cgroup = track && !perf_missing_features.cgroup; 1197 evsel__set_sample_bit(evsel, CGROUP); 1198 } 1199 1200 if (opts->sample_data_page_size) 1201 evsel__set_sample_bit(evsel, DATA_PAGE_SIZE); 1202 1203 if (opts->sample_code_page_size) 1204 evsel__set_sample_bit(evsel, CODE_PAGE_SIZE); 1205 1206 if (opts->record_switch_events) 1207 attr->context_switch = track; 1208 1209 if (opts->sample_transaction) 1210 evsel__set_sample_bit(evsel, TRANSACTION); 1211 1212 if (opts->running_time) { 1213 evsel->core.attr.read_format |= 1214 PERF_FORMAT_TOTAL_TIME_ENABLED | 1215 PERF_FORMAT_TOTAL_TIME_RUNNING; 1216 } 1217 1218 /* 1219 * XXX see the function comment above 1220 * 1221 * Disabling only independent events or group leaders, 1222 * keeping group members enabled. 1223 */ 1224 if (evsel__is_group_leader(evsel)) 1225 attr->disabled = 1; 1226 1227 /* 1228 * Setting enable_on_exec for independent events and 1229 * group leaders for traced executed by perf. 1230 */ 1231 if (target__none(&opts->target) && evsel__is_group_leader(evsel) && 1232 !opts->initial_delay) 1233 attr->enable_on_exec = 1; 1234 1235 if (evsel->immediate) { 1236 attr->disabled = 0; 1237 attr->enable_on_exec = 0; 1238 } 1239 1240 clockid = opts->clockid; 1241 if (opts->use_clockid) { 1242 attr->use_clockid = 1; 1243 attr->clockid = opts->clockid; 1244 } 1245 1246 if (evsel->precise_max) 1247 attr->precise_ip = 3; 1248 1249 if (opts->all_user) { 1250 attr->exclude_kernel = 1; 1251 attr->exclude_user = 0; 1252 } 1253 1254 if (opts->all_kernel) { 1255 attr->exclude_kernel = 0; 1256 attr->exclude_user = 1; 1257 } 1258 1259 if (evsel->core.own_cpus || evsel->unit) 1260 evsel->core.attr.read_format |= PERF_FORMAT_ID; 1261 1262 /* 1263 * Apply event specific term settings, 1264 * it overloads any global configuration. 1265 */ 1266 evsel__apply_config_terms(evsel, opts, track); 1267 1268 evsel->ignore_missing_thread = opts->ignore_missing_thread; 1269 1270 /* The --period option takes the precedence. */ 1271 if (opts->period_set) { 1272 if (opts->period) 1273 evsel__set_sample_bit(evsel, PERIOD); 1274 else 1275 evsel__reset_sample_bit(evsel, PERIOD); 1276 } 1277 1278 /* 1279 * A dummy event never triggers any actual counter and therefore 1280 * cannot be used with branch_stack. 1281 * 1282 * For initial_delay, a dummy event is added implicitly. 1283 * The software event will trigger -EOPNOTSUPP error out, 1284 * if BRANCH_STACK bit is set. 1285 */ 1286 if (evsel__is_dummy_event(evsel)) 1287 evsel__reset_sample_bit(evsel, BRANCH_STACK); 1288 } 1289 1290 int evsel__set_filter(struct evsel *evsel, const char *filter) 1291 { 1292 char *new_filter = strdup(filter); 1293 1294 if (new_filter != NULL) { 1295 free(evsel->filter); 1296 evsel->filter = new_filter; 1297 return 0; 1298 } 1299 1300 return -1; 1301 } 1302 1303 static int evsel__append_filter(struct evsel *evsel, const char *fmt, const char *filter) 1304 { 1305 char *new_filter; 1306 1307 if (evsel->filter == NULL) 1308 return evsel__set_filter(evsel, filter); 1309 1310 if (asprintf(&new_filter, fmt, evsel->filter, filter) > 0) { 1311 free(evsel->filter); 1312 evsel->filter = new_filter; 1313 return 0; 1314 } 1315 1316 return -1; 1317 } 1318 1319 int evsel__append_tp_filter(struct evsel *evsel, const char *filter) 1320 { 1321 return evsel__append_filter(evsel, "(%s) && (%s)", filter); 1322 } 1323 1324 int evsel__append_addr_filter(struct evsel *evsel, const char *filter) 1325 { 1326 return evsel__append_filter(evsel, "%s,%s", filter); 1327 } 1328 1329 /* Caller has to clear disabled after going through all CPUs. */ 1330 int evsel__enable_cpu(struct evsel *evsel, int cpu) 1331 { 1332 return perf_evsel__enable_cpu(&evsel->core, cpu); 1333 } 1334 1335 int evsel__enable(struct evsel *evsel) 1336 { 1337 int err = perf_evsel__enable(&evsel->core); 1338 1339 if (!err) 1340 evsel->disabled = false; 1341 return err; 1342 } 1343 1344 /* Caller has to set disabled after going through all CPUs. */ 1345 int evsel__disable_cpu(struct evsel *evsel, int cpu) 1346 { 1347 return perf_evsel__disable_cpu(&evsel->core, cpu); 1348 } 1349 1350 int evsel__disable(struct evsel *evsel) 1351 { 1352 int err = perf_evsel__disable(&evsel->core); 1353 /* 1354 * We mark it disabled here so that tools that disable a event can 1355 * ignore events after they disable it. I.e. the ring buffer may have 1356 * already a few more events queued up before the kernel got the stop 1357 * request. 1358 */ 1359 if (!err) 1360 evsel->disabled = true; 1361 1362 return err; 1363 } 1364 1365 static void evsel__free_config_terms(struct evsel *evsel) 1366 { 1367 struct evsel_config_term *term, *h; 1368 1369 list_for_each_entry_safe(term, h, &evsel->config_terms, list) { 1370 list_del_init(&term->list); 1371 if (term->free_str) 1372 zfree(&term->val.str); 1373 free(term); 1374 } 1375 } 1376 1377 void evsel__exit(struct evsel *evsel) 1378 { 1379 assert(list_empty(&evsel->core.node)); 1380 assert(evsel->evlist == NULL); 1381 bpf_counter__destroy(evsel); 1382 evsel__free_counts(evsel); 1383 perf_evsel__free_fd(&evsel->core); 1384 perf_evsel__free_id(&evsel->core); 1385 evsel__free_config_terms(evsel); 1386 cgroup__put(evsel->cgrp); 1387 perf_cpu_map__put(evsel->core.cpus); 1388 perf_cpu_map__put(evsel->core.own_cpus); 1389 perf_thread_map__put(evsel->core.threads); 1390 zfree(&evsel->group_name); 1391 zfree(&evsel->name); 1392 zfree(&evsel->pmu_name); 1393 zfree(&evsel->per_pkg_mask); 1394 zfree(&evsel->metric_events); 1395 perf_evsel__object.fini(evsel); 1396 } 1397 1398 void evsel__delete(struct evsel *evsel) 1399 { 1400 evsel__exit(evsel); 1401 free(evsel); 1402 } 1403 1404 void evsel__compute_deltas(struct evsel *evsel, int cpu, int thread, 1405 struct perf_counts_values *count) 1406 { 1407 struct perf_counts_values tmp; 1408 1409 if (!evsel->prev_raw_counts) 1410 return; 1411 1412 if (cpu == -1) { 1413 tmp = evsel->prev_raw_counts->aggr; 1414 evsel->prev_raw_counts->aggr = *count; 1415 } else { 1416 tmp = *perf_counts(evsel->prev_raw_counts, cpu, thread); 1417 *perf_counts(evsel->prev_raw_counts, cpu, thread) = *count; 1418 } 1419 1420 count->val = count->val - tmp.val; 1421 count->ena = count->ena - tmp.ena; 1422 count->run = count->run - tmp.run; 1423 } 1424 1425 void perf_counts_values__scale(struct perf_counts_values *count, 1426 bool scale, s8 *pscaled) 1427 { 1428 s8 scaled = 0; 1429 1430 if (scale) { 1431 if (count->run == 0) { 1432 scaled = -1; 1433 count->val = 0; 1434 } else if (count->run < count->ena) { 1435 scaled = 1; 1436 count->val = (u64)((double) count->val * count->ena / count->run); 1437 } 1438 } 1439 1440 if (pscaled) 1441 *pscaled = scaled; 1442 } 1443 1444 static int evsel__read_one(struct evsel *evsel, int cpu, int thread) 1445 { 1446 struct perf_counts_values *count = perf_counts(evsel->counts, cpu, thread); 1447 1448 return perf_evsel__read(&evsel->core, cpu, thread, count); 1449 } 1450 1451 static void evsel__set_count(struct evsel *counter, int cpu, int thread, u64 val, u64 ena, u64 run) 1452 { 1453 struct perf_counts_values *count; 1454 1455 count = perf_counts(counter->counts, cpu, thread); 1456 1457 count->val = val; 1458 count->ena = ena; 1459 count->run = run; 1460 1461 perf_counts__set_loaded(counter->counts, cpu, thread, true); 1462 } 1463 1464 static int evsel__process_group_data(struct evsel *leader, int cpu, int thread, u64 *data) 1465 { 1466 u64 read_format = leader->core.attr.read_format; 1467 struct sample_read_value *v; 1468 u64 nr, ena = 0, run = 0, i; 1469 1470 nr = *data++; 1471 1472 if (nr != (u64) leader->core.nr_members) 1473 return -EINVAL; 1474 1475 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 1476 ena = *data++; 1477 1478 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 1479 run = *data++; 1480 1481 v = (struct sample_read_value *) data; 1482 1483 evsel__set_count(leader, cpu, thread, v[0].value, ena, run); 1484 1485 for (i = 1; i < nr; i++) { 1486 struct evsel *counter; 1487 1488 counter = evlist__id2evsel(leader->evlist, v[i].id); 1489 if (!counter) 1490 return -EINVAL; 1491 1492 evsel__set_count(counter, cpu, thread, v[i].value, ena, run); 1493 } 1494 1495 return 0; 1496 } 1497 1498 static int evsel__read_group(struct evsel *leader, int cpu, int thread) 1499 { 1500 struct perf_stat_evsel *ps = leader->stats; 1501 u64 read_format = leader->core.attr.read_format; 1502 int size = perf_evsel__read_size(&leader->core); 1503 u64 *data = ps->group_data; 1504 1505 if (!(read_format & PERF_FORMAT_ID)) 1506 return -EINVAL; 1507 1508 if (!evsel__is_group_leader(leader)) 1509 return -EINVAL; 1510 1511 if (!data) { 1512 data = zalloc(size); 1513 if (!data) 1514 return -ENOMEM; 1515 1516 ps->group_data = data; 1517 } 1518 1519 if (FD(leader, cpu, thread) < 0) 1520 return -EINVAL; 1521 1522 if (readn(FD(leader, cpu, thread), data, size) <= 0) 1523 return -errno; 1524 1525 return evsel__process_group_data(leader, cpu, thread, data); 1526 } 1527 1528 int evsel__read_counter(struct evsel *evsel, int cpu, int thread) 1529 { 1530 u64 read_format = evsel->core.attr.read_format; 1531 1532 if (read_format & PERF_FORMAT_GROUP) 1533 return evsel__read_group(evsel, cpu, thread); 1534 1535 return evsel__read_one(evsel, cpu, thread); 1536 } 1537 1538 int __evsel__read_on_cpu(struct evsel *evsel, int cpu, int thread, bool scale) 1539 { 1540 struct perf_counts_values count; 1541 size_t nv = scale ? 3 : 1; 1542 1543 if (FD(evsel, cpu, thread) < 0) 1544 return -EINVAL; 1545 1546 if (evsel->counts == NULL && evsel__alloc_counts(evsel, cpu + 1, thread + 1) < 0) 1547 return -ENOMEM; 1548 1549 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) <= 0) 1550 return -errno; 1551 1552 evsel__compute_deltas(evsel, cpu, thread, &count); 1553 perf_counts_values__scale(&count, scale, NULL); 1554 *perf_counts(evsel->counts, cpu, thread) = count; 1555 return 0; 1556 } 1557 1558 static int get_group_fd(struct evsel *evsel, int cpu, int thread) 1559 { 1560 struct evsel *leader = evsel->leader; 1561 int fd; 1562 1563 if (evsel__is_group_leader(evsel)) 1564 return -1; 1565 1566 /* 1567 * Leader must be already processed/open, 1568 * if not it's a bug. 1569 */ 1570 BUG_ON(!leader->core.fd); 1571 1572 fd = FD(leader, cpu, thread); 1573 BUG_ON(fd == -1); 1574 1575 return fd; 1576 } 1577 1578 static void evsel__remove_fd(struct evsel *pos, int nr_cpus, int nr_threads, int thread_idx) 1579 { 1580 for (int cpu = 0; cpu < nr_cpus; cpu++) 1581 for (int thread = thread_idx; thread < nr_threads - 1; thread++) 1582 FD(pos, cpu, thread) = FD(pos, cpu, thread + 1); 1583 } 1584 1585 static int update_fds(struct evsel *evsel, 1586 int nr_cpus, int cpu_idx, 1587 int nr_threads, int thread_idx) 1588 { 1589 struct evsel *pos; 1590 1591 if (cpu_idx >= nr_cpus || thread_idx >= nr_threads) 1592 return -EINVAL; 1593 1594 evlist__for_each_entry(evsel->evlist, pos) { 1595 nr_cpus = pos != evsel ? nr_cpus : cpu_idx; 1596 1597 evsel__remove_fd(pos, nr_cpus, nr_threads, thread_idx); 1598 1599 /* 1600 * Since fds for next evsel has not been created, 1601 * there is no need to iterate whole event list. 1602 */ 1603 if (pos == evsel) 1604 break; 1605 } 1606 return 0; 1607 } 1608 1609 static bool ignore_missing_thread(struct evsel *evsel, 1610 int nr_cpus, int cpu, 1611 struct perf_thread_map *threads, 1612 int thread, int err) 1613 { 1614 pid_t ignore_pid = perf_thread_map__pid(threads, thread); 1615 1616 if (!evsel->ignore_missing_thread) 1617 return false; 1618 1619 /* The system wide setup does not work with threads. */ 1620 if (evsel->core.system_wide) 1621 return false; 1622 1623 /* The -ESRCH is perf event syscall errno for pid's not found. */ 1624 if (err != -ESRCH) 1625 return false; 1626 1627 /* If there's only one thread, let it fail. */ 1628 if (threads->nr == 1) 1629 return false; 1630 1631 /* 1632 * We should remove fd for missing_thread first 1633 * because thread_map__remove() will decrease threads->nr. 1634 */ 1635 if (update_fds(evsel, nr_cpus, cpu, threads->nr, thread)) 1636 return false; 1637 1638 if (thread_map__remove(threads, thread)) 1639 return false; 1640 1641 pr_warning("WARNING: Ignored open failure for pid %d\n", 1642 ignore_pid); 1643 return true; 1644 } 1645 1646 static int __open_attr__fprintf(FILE *fp, const char *name, const char *val, 1647 void *priv __maybe_unused) 1648 { 1649 return fprintf(fp, " %-32s %s\n", name, val); 1650 } 1651 1652 static void display_attr(struct perf_event_attr *attr) 1653 { 1654 if (verbose >= 2 || debug_peo_args) { 1655 fprintf(stderr, "%.60s\n", graph_dotted_line); 1656 fprintf(stderr, "perf_event_attr:\n"); 1657 perf_event_attr__fprintf(stderr, attr, __open_attr__fprintf, NULL); 1658 fprintf(stderr, "%.60s\n", graph_dotted_line); 1659 } 1660 } 1661 1662 static int perf_event_open(struct evsel *evsel, 1663 pid_t pid, int cpu, int group_fd, 1664 unsigned long flags) 1665 { 1666 int precise_ip = evsel->core.attr.precise_ip; 1667 int fd; 1668 1669 while (1) { 1670 pr_debug2_peo("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx", 1671 pid, cpu, group_fd, flags); 1672 1673 fd = sys_perf_event_open(&evsel->core.attr, pid, cpu, group_fd, flags); 1674 if (fd >= 0) 1675 break; 1676 1677 /* Do not try less precise if not requested. */ 1678 if (!evsel->precise_max) 1679 break; 1680 1681 /* 1682 * We tried all the precise_ip values, and it's 1683 * still failing, so leave it to standard fallback. 1684 */ 1685 if (!evsel->core.attr.precise_ip) { 1686 evsel->core.attr.precise_ip = precise_ip; 1687 break; 1688 } 1689 1690 pr_debug2_peo("\nsys_perf_event_open failed, error %d\n", -ENOTSUP); 1691 evsel->core.attr.precise_ip--; 1692 pr_debug2_peo("decreasing precise_ip by one (%d)\n", evsel->core.attr.precise_ip); 1693 display_attr(&evsel->core.attr); 1694 } 1695 1696 return fd; 1697 } 1698 1699 static int evsel__open_cpu(struct evsel *evsel, struct perf_cpu_map *cpus, 1700 struct perf_thread_map *threads, 1701 int start_cpu, int end_cpu) 1702 { 1703 int cpu, thread, nthreads; 1704 unsigned long flags = PERF_FLAG_FD_CLOEXEC; 1705 int pid = -1, err, old_errno; 1706 enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE; 1707 1708 if ((perf_missing_features.write_backward && evsel->core.attr.write_backward) || 1709 (perf_missing_features.aux_output && evsel->core.attr.aux_output)) 1710 return -EINVAL; 1711 1712 if (cpus == NULL) { 1713 static struct perf_cpu_map *empty_cpu_map; 1714 1715 if (empty_cpu_map == NULL) { 1716 empty_cpu_map = perf_cpu_map__dummy_new(); 1717 if (empty_cpu_map == NULL) 1718 return -ENOMEM; 1719 } 1720 1721 cpus = empty_cpu_map; 1722 } 1723 1724 if (threads == NULL) { 1725 static struct perf_thread_map *empty_thread_map; 1726 1727 if (empty_thread_map == NULL) { 1728 empty_thread_map = thread_map__new_by_tid(-1); 1729 if (empty_thread_map == NULL) 1730 return -ENOMEM; 1731 } 1732 1733 threads = empty_thread_map; 1734 } 1735 1736 if (evsel->core.system_wide) 1737 nthreads = 1; 1738 else 1739 nthreads = threads->nr; 1740 1741 if (evsel->core.fd == NULL && 1742 perf_evsel__alloc_fd(&evsel->core, cpus->nr, nthreads) < 0) 1743 return -ENOMEM; 1744 1745 if (evsel->cgrp) { 1746 flags |= PERF_FLAG_PID_CGROUP; 1747 pid = evsel->cgrp->fd; 1748 } 1749 1750 fallback_missing_features: 1751 if (perf_missing_features.weight_struct) { 1752 evsel__set_sample_bit(evsel, WEIGHT); 1753 evsel__reset_sample_bit(evsel, WEIGHT_STRUCT); 1754 } 1755 if (perf_missing_features.clockid_wrong) 1756 evsel->core.attr.clockid = CLOCK_MONOTONIC; /* should always work */ 1757 if (perf_missing_features.clockid) { 1758 evsel->core.attr.use_clockid = 0; 1759 evsel->core.attr.clockid = 0; 1760 } 1761 if (perf_missing_features.cloexec) 1762 flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC; 1763 if (perf_missing_features.mmap2) 1764 evsel->core.attr.mmap2 = 0; 1765 if (perf_missing_features.exclude_guest) 1766 evsel->core.attr.exclude_guest = evsel->core.attr.exclude_host = 0; 1767 if (perf_missing_features.lbr_flags) 1768 evsel->core.attr.branch_sample_type &= ~(PERF_SAMPLE_BRANCH_NO_FLAGS | 1769 PERF_SAMPLE_BRANCH_NO_CYCLES); 1770 if (perf_missing_features.group_read && evsel->core.attr.inherit) 1771 evsel->core.attr.read_format &= ~(PERF_FORMAT_GROUP|PERF_FORMAT_ID); 1772 if (perf_missing_features.ksymbol) 1773 evsel->core.attr.ksymbol = 0; 1774 if (perf_missing_features.bpf) 1775 evsel->core.attr.bpf_event = 0; 1776 if (perf_missing_features.branch_hw_idx) 1777 evsel->core.attr.branch_sample_type &= ~PERF_SAMPLE_BRANCH_HW_INDEX; 1778 retry_sample_id: 1779 if (perf_missing_features.sample_id_all) 1780 evsel->core.attr.sample_id_all = 0; 1781 1782 display_attr(&evsel->core.attr); 1783 1784 for (cpu = start_cpu; cpu < end_cpu; cpu++) { 1785 1786 for (thread = 0; thread < nthreads; thread++) { 1787 int fd, group_fd; 1788 1789 if (!evsel->cgrp && !evsel->core.system_wide) 1790 pid = perf_thread_map__pid(threads, thread); 1791 1792 group_fd = get_group_fd(evsel, cpu, thread); 1793 retry_open: 1794 test_attr__ready(); 1795 1796 fd = perf_event_open(evsel, pid, cpus->map[cpu], 1797 group_fd, flags); 1798 1799 FD(evsel, cpu, thread) = fd; 1800 1801 bpf_counter__install_pe(evsel, cpu, fd); 1802 1803 if (unlikely(test_attr__enabled)) { 1804 test_attr__open(&evsel->core.attr, pid, cpus->map[cpu], 1805 fd, group_fd, flags); 1806 } 1807 1808 if (fd < 0) { 1809 err = -errno; 1810 1811 if (ignore_missing_thread(evsel, cpus->nr, cpu, threads, thread, err)) { 1812 /* 1813 * We just removed 1 thread, so take a step 1814 * back on thread index and lower the upper 1815 * nthreads limit. 1816 */ 1817 nthreads--; 1818 thread--; 1819 1820 /* ... and pretend like nothing have happened. */ 1821 err = 0; 1822 continue; 1823 } 1824 1825 pr_debug2_peo("\nsys_perf_event_open failed, error %d\n", 1826 err); 1827 goto try_fallback; 1828 } 1829 1830 pr_debug2_peo(" = %d\n", fd); 1831 1832 if (evsel->bpf_fd >= 0) { 1833 int evt_fd = fd; 1834 int bpf_fd = evsel->bpf_fd; 1835 1836 err = ioctl(evt_fd, 1837 PERF_EVENT_IOC_SET_BPF, 1838 bpf_fd); 1839 if (err && errno != EEXIST) { 1840 pr_err("failed to attach bpf fd %d: %s\n", 1841 bpf_fd, strerror(errno)); 1842 err = -EINVAL; 1843 goto out_close; 1844 } 1845 } 1846 1847 set_rlimit = NO_CHANGE; 1848 1849 /* 1850 * If we succeeded but had to kill clockid, fail and 1851 * have evsel__open_strerror() print us a nice error. 1852 */ 1853 if (perf_missing_features.clockid || 1854 perf_missing_features.clockid_wrong) { 1855 err = -EINVAL; 1856 goto out_close; 1857 } 1858 } 1859 } 1860 1861 return 0; 1862 1863 try_fallback: 1864 /* 1865 * perf stat needs between 5 and 22 fds per CPU. When we run out 1866 * of them try to increase the limits. 1867 */ 1868 if (err == -EMFILE && set_rlimit < INCREASED_MAX) { 1869 struct rlimit l; 1870 1871 old_errno = errno; 1872 if (getrlimit(RLIMIT_NOFILE, &l) == 0) { 1873 if (set_rlimit == NO_CHANGE) 1874 l.rlim_cur = l.rlim_max; 1875 else { 1876 l.rlim_cur = l.rlim_max + 1000; 1877 l.rlim_max = l.rlim_cur; 1878 } 1879 if (setrlimit(RLIMIT_NOFILE, &l) == 0) { 1880 set_rlimit++; 1881 errno = old_errno; 1882 goto retry_open; 1883 } 1884 } 1885 errno = old_errno; 1886 } 1887 1888 if (err != -EINVAL || cpu > 0 || thread > 0) 1889 goto out_close; 1890 1891 /* 1892 * Must probe features in the order they were added to the 1893 * perf_event_attr interface. 1894 */ 1895 if (!perf_missing_features.weight_struct && 1896 (evsel->core.attr.sample_type & PERF_SAMPLE_WEIGHT_STRUCT)) { 1897 perf_missing_features.weight_struct = true; 1898 pr_debug2("switching off weight struct support\n"); 1899 goto fallback_missing_features; 1900 } else if (!perf_missing_features.code_page_size && 1901 (evsel->core.attr.sample_type & PERF_SAMPLE_CODE_PAGE_SIZE)) { 1902 perf_missing_features.code_page_size = true; 1903 pr_debug2_peo("Kernel has no PERF_SAMPLE_CODE_PAGE_SIZE support, bailing out\n"); 1904 goto out_close; 1905 } else if (!perf_missing_features.data_page_size && 1906 (evsel->core.attr.sample_type & PERF_SAMPLE_DATA_PAGE_SIZE)) { 1907 perf_missing_features.data_page_size = true; 1908 pr_debug2_peo("Kernel has no PERF_SAMPLE_DATA_PAGE_SIZE support, bailing out\n"); 1909 goto out_close; 1910 } else if (!perf_missing_features.cgroup && evsel->core.attr.cgroup) { 1911 perf_missing_features.cgroup = true; 1912 pr_debug2_peo("Kernel has no cgroup sampling support, bailing out\n"); 1913 goto out_close; 1914 } else if (!perf_missing_features.branch_hw_idx && 1915 (evsel->core.attr.branch_sample_type & PERF_SAMPLE_BRANCH_HW_INDEX)) { 1916 perf_missing_features.branch_hw_idx = true; 1917 pr_debug2("switching off branch HW index support\n"); 1918 goto fallback_missing_features; 1919 } else if (!perf_missing_features.aux_output && evsel->core.attr.aux_output) { 1920 perf_missing_features.aux_output = true; 1921 pr_debug2_peo("Kernel has no attr.aux_output support, bailing out\n"); 1922 goto out_close; 1923 } else if (!perf_missing_features.bpf && evsel->core.attr.bpf_event) { 1924 perf_missing_features.bpf = true; 1925 pr_debug2_peo("switching off bpf_event\n"); 1926 goto fallback_missing_features; 1927 } else if (!perf_missing_features.ksymbol && evsel->core.attr.ksymbol) { 1928 perf_missing_features.ksymbol = true; 1929 pr_debug2_peo("switching off ksymbol\n"); 1930 goto fallback_missing_features; 1931 } else if (!perf_missing_features.write_backward && evsel->core.attr.write_backward) { 1932 perf_missing_features.write_backward = true; 1933 pr_debug2_peo("switching off write_backward\n"); 1934 goto out_close; 1935 } else if (!perf_missing_features.clockid_wrong && evsel->core.attr.use_clockid) { 1936 perf_missing_features.clockid_wrong = true; 1937 pr_debug2_peo("switching off clockid\n"); 1938 goto fallback_missing_features; 1939 } else if (!perf_missing_features.clockid && evsel->core.attr.use_clockid) { 1940 perf_missing_features.clockid = true; 1941 pr_debug2_peo("switching off use_clockid\n"); 1942 goto fallback_missing_features; 1943 } else if (!perf_missing_features.cloexec && (flags & PERF_FLAG_FD_CLOEXEC)) { 1944 perf_missing_features.cloexec = true; 1945 pr_debug2_peo("switching off cloexec flag\n"); 1946 goto fallback_missing_features; 1947 } else if (!perf_missing_features.mmap2 && evsel->core.attr.mmap2) { 1948 perf_missing_features.mmap2 = true; 1949 pr_debug2_peo("switching off mmap2\n"); 1950 goto fallback_missing_features; 1951 } else if (!perf_missing_features.exclude_guest && 1952 (evsel->core.attr.exclude_guest || evsel->core.attr.exclude_host)) { 1953 perf_missing_features.exclude_guest = true; 1954 pr_debug2_peo("switching off exclude_guest, exclude_host\n"); 1955 goto fallback_missing_features; 1956 } else if (!perf_missing_features.sample_id_all) { 1957 perf_missing_features.sample_id_all = true; 1958 pr_debug2_peo("switching off sample_id_all\n"); 1959 goto retry_sample_id; 1960 } else if (!perf_missing_features.lbr_flags && 1961 (evsel->core.attr.branch_sample_type & 1962 (PERF_SAMPLE_BRANCH_NO_CYCLES | 1963 PERF_SAMPLE_BRANCH_NO_FLAGS))) { 1964 perf_missing_features.lbr_flags = true; 1965 pr_debug2_peo("switching off branch sample type no (cycles/flags)\n"); 1966 goto fallback_missing_features; 1967 } else if (!perf_missing_features.group_read && 1968 evsel->core.attr.inherit && 1969 (evsel->core.attr.read_format & PERF_FORMAT_GROUP) && 1970 evsel__is_group_leader(evsel)) { 1971 perf_missing_features.group_read = true; 1972 pr_debug2_peo("switching off group read\n"); 1973 goto fallback_missing_features; 1974 } 1975 out_close: 1976 if (err) 1977 threads->err_thread = thread; 1978 1979 old_errno = errno; 1980 do { 1981 while (--thread >= 0) { 1982 if (FD(evsel, cpu, thread) >= 0) 1983 close(FD(evsel, cpu, thread)); 1984 FD(evsel, cpu, thread) = -1; 1985 } 1986 thread = nthreads; 1987 } while (--cpu >= 0); 1988 errno = old_errno; 1989 return err; 1990 } 1991 1992 int evsel__open(struct evsel *evsel, struct perf_cpu_map *cpus, 1993 struct perf_thread_map *threads) 1994 { 1995 return evsel__open_cpu(evsel, cpus, threads, 0, cpus ? cpus->nr : 1); 1996 } 1997 1998 void evsel__close(struct evsel *evsel) 1999 { 2000 perf_evsel__close(&evsel->core); 2001 perf_evsel__free_id(&evsel->core); 2002 } 2003 2004 int evsel__open_per_cpu(struct evsel *evsel, struct perf_cpu_map *cpus, int cpu) 2005 { 2006 if (cpu == -1) 2007 return evsel__open_cpu(evsel, cpus, NULL, 0, 2008 cpus ? cpus->nr : 1); 2009 2010 return evsel__open_cpu(evsel, cpus, NULL, cpu, cpu + 1); 2011 } 2012 2013 int evsel__open_per_thread(struct evsel *evsel, struct perf_thread_map *threads) 2014 { 2015 return evsel__open(evsel, NULL, threads); 2016 } 2017 2018 static int perf_evsel__parse_id_sample(const struct evsel *evsel, 2019 const union perf_event *event, 2020 struct perf_sample *sample) 2021 { 2022 u64 type = evsel->core.attr.sample_type; 2023 const __u64 *array = event->sample.array; 2024 bool swapped = evsel->needs_swap; 2025 union u64_swap u; 2026 2027 array += ((event->header.size - 2028 sizeof(event->header)) / sizeof(u64)) - 1; 2029 2030 if (type & PERF_SAMPLE_IDENTIFIER) { 2031 sample->id = *array; 2032 array--; 2033 } 2034 2035 if (type & PERF_SAMPLE_CPU) { 2036 u.val64 = *array; 2037 if (swapped) { 2038 /* undo swap of u64, then swap on individual u32s */ 2039 u.val64 = bswap_64(u.val64); 2040 u.val32[0] = bswap_32(u.val32[0]); 2041 } 2042 2043 sample->cpu = u.val32[0]; 2044 array--; 2045 } 2046 2047 if (type & PERF_SAMPLE_STREAM_ID) { 2048 sample->stream_id = *array; 2049 array--; 2050 } 2051 2052 if (type & PERF_SAMPLE_ID) { 2053 sample->id = *array; 2054 array--; 2055 } 2056 2057 if (type & PERF_SAMPLE_TIME) { 2058 sample->time = *array; 2059 array--; 2060 } 2061 2062 if (type & PERF_SAMPLE_TID) { 2063 u.val64 = *array; 2064 if (swapped) { 2065 /* undo swap of u64, then swap on individual u32s */ 2066 u.val64 = bswap_64(u.val64); 2067 u.val32[0] = bswap_32(u.val32[0]); 2068 u.val32[1] = bswap_32(u.val32[1]); 2069 } 2070 2071 sample->pid = u.val32[0]; 2072 sample->tid = u.val32[1]; 2073 array--; 2074 } 2075 2076 return 0; 2077 } 2078 2079 static inline bool overflow(const void *endp, u16 max_size, const void *offset, 2080 u64 size) 2081 { 2082 return size > max_size || offset + size > endp; 2083 } 2084 2085 #define OVERFLOW_CHECK(offset, size, max_size) \ 2086 do { \ 2087 if (overflow(endp, (max_size), (offset), (size))) \ 2088 return -EFAULT; \ 2089 } while (0) 2090 2091 #define OVERFLOW_CHECK_u64(offset) \ 2092 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64)) 2093 2094 static int 2095 perf_event__check_size(union perf_event *event, unsigned int sample_size) 2096 { 2097 /* 2098 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes 2099 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to 2100 * check the format does not go past the end of the event. 2101 */ 2102 if (sample_size + sizeof(event->header) > event->header.size) 2103 return -EFAULT; 2104 2105 return 0; 2106 } 2107 2108 void __weak arch_perf_parse_sample_weight(struct perf_sample *data, 2109 const __u64 *array, 2110 u64 type __maybe_unused) 2111 { 2112 data->weight = *array; 2113 } 2114 2115 int evsel__parse_sample(struct evsel *evsel, union perf_event *event, 2116 struct perf_sample *data) 2117 { 2118 u64 type = evsel->core.attr.sample_type; 2119 bool swapped = evsel->needs_swap; 2120 const __u64 *array; 2121 u16 max_size = event->header.size; 2122 const void *endp = (void *)event + max_size; 2123 u64 sz; 2124 2125 /* 2126 * used for cross-endian analysis. See git commit 65014ab3 2127 * for why this goofiness is needed. 2128 */ 2129 union u64_swap u; 2130 2131 memset(data, 0, sizeof(*data)); 2132 data->cpu = data->pid = data->tid = -1; 2133 data->stream_id = data->id = data->time = -1ULL; 2134 data->period = evsel->core.attr.sample_period; 2135 data->cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; 2136 data->misc = event->header.misc; 2137 data->id = -1ULL; 2138 data->data_src = PERF_MEM_DATA_SRC_NONE; 2139 2140 if (event->header.type != PERF_RECORD_SAMPLE) { 2141 if (!evsel->core.attr.sample_id_all) 2142 return 0; 2143 return perf_evsel__parse_id_sample(evsel, event, data); 2144 } 2145 2146 array = event->sample.array; 2147 2148 if (perf_event__check_size(event, evsel->sample_size)) 2149 return -EFAULT; 2150 2151 if (type & PERF_SAMPLE_IDENTIFIER) { 2152 data->id = *array; 2153 array++; 2154 } 2155 2156 if (type & PERF_SAMPLE_IP) { 2157 data->ip = *array; 2158 array++; 2159 } 2160 2161 if (type & PERF_SAMPLE_TID) { 2162 u.val64 = *array; 2163 if (swapped) { 2164 /* undo swap of u64, then swap on individual u32s */ 2165 u.val64 = bswap_64(u.val64); 2166 u.val32[0] = bswap_32(u.val32[0]); 2167 u.val32[1] = bswap_32(u.val32[1]); 2168 } 2169 2170 data->pid = u.val32[0]; 2171 data->tid = u.val32[1]; 2172 array++; 2173 } 2174 2175 if (type & PERF_SAMPLE_TIME) { 2176 data->time = *array; 2177 array++; 2178 } 2179 2180 if (type & PERF_SAMPLE_ADDR) { 2181 data->addr = *array; 2182 array++; 2183 } 2184 2185 if (type & PERF_SAMPLE_ID) { 2186 data->id = *array; 2187 array++; 2188 } 2189 2190 if (type & PERF_SAMPLE_STREAM_ID) { 2191 data->stream_id = *array; 2192 array++; 2193 } 2194 2195 if (type & PERF_SAMPLE_CPU) { 2196 2197 u.val64 = *array; 2198 if (swapped) { 2199 /* undo swap of u64, then swap on individual u32s */ 2200 u.val64 = bswap_64(u.val64); 2201 u.val32[0] = bswap_32(u.val32[0]); 2202 } 2203 2204 data->cpu = u.val32[0]; 2205 array++; 2206 } 2207 2208 if (type & PERF_SAMPLE_PERIOD) { 2209 data->period = *array; 2210 array++; 2211 } 2212 2213 if (type & PERF_SAMPLE_READ) { 2214 u64 read_format = evsel->core.attr.read_format; 2215 2216 OVERFLOW_CHECK_u64(array); 2217 if (read_format & PERF_FORMAT_GROUP) 2218 data->read.group.nr = *array; 2219 else 2220 data->read.one.value = *array; 2221 2222 array++; 2223 2224 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { 2225 OVERFLOW_CHECK_u64(array); 2226 data->read.time_enabled = *array; 2227 array++; 2228 } 2229 2230 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { 2231 OVERFLOW_CHECK_u64(array); 2232 data->read.time_running = *array; 2233 array++; 2234 } 2235 2236 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */ 2237 if (read_format & PERF_FORMAT_GROUP) { 2238 const u64 max_group_nr = UINT64_MAX / 2239 sizeof(struct sample_read_value); 2240 2241 if (data->read.group.nr > max_group_nr) 2242 return -EFAULT; 2243 sz = data->read.group.nr * 2244 sizeof(struct sample_read_value); 2245 OVERFLOW_CHECK(array, sz, max_size); 2246 data->read.group.values = 2247 (struct sample_read_value *)array; 2248 array = (void *)array + sz; 2249 } else { 2250 OVERFLOW_CHECK_u64(array); 2251 data->read.one.id = *array; 2252 array++; 2253 } 2254 } 2255 2256 if (type & PERF_SAMPLE_CALLCHAIN) { 2257 const u64 max_callchain_nr = UINT64_MAX / sizeof(u64); 2258 2259 OVERFLOW_CHECK_u64(array); 2260 data->callchain = (struct ip_callchain *)array++; 2261 if (data->callchain->nr > max_callchain_nr) 2262 return -EFAULT; 2263 sz = data->callchain->nr * sizeof(u64); 2264 OVERFLOW_CHECK(array, sz, max_size); 2265 array = (void *)array + sz; 2266 } 2267 2268 if (type & PERF_SAMPLE_RAW) { 2269 OVERFLOW_CHECK_u64(array); 2270 u.val64 = *array; 2271 2272 /* 2273 * Undo swap of u64, then swap on individual u32s, 2274 * get the size of the raw area and undo all of the 2275 * swap. The pevent interface handles endianity by 2276 * itself. 2277 */ 2278 if (swapped) { 2279 u.val64 = bswap_64(u.val64); 2280 u.val32[0] = bswap_32(u.val32[0]); 2281 u.val32[1] = bswap_32(u.val32[1]); 2282 } 2283 data->raw_size = u.val32[0]; 2284 2285 /* 2286 * The raw data is aligned on 64bits including the 2287 * u32 size, so it's safe to use mem_bswap_64. 2288 */ 2289 if (swapped) 2290 mem_bswap_64((void *) array, data->raw_size); 2291 2292 array = (void *)array + sizeof(u32); 2293 2294 OVERFLOW_CHECK(array, data->raw_size, max_size); 2295 data->raw_data = (void *)array; 2296 array = (void *)array + data->raw_size; 2297 } 2298 2299 if (type & PERF_SAMPLE_BRANCH_STACK) { 2300 const u64 max_branch_nr = UINT64_MAX / 2301 sizeof(struct branch_entry); 2302 2303 OVERFLOW_CHECK_u64(array); 2304 data->branch_stack = (struct branch_stack *)array++; 2305 2306 if (data->branch_stack->nr > max_branch_nr) 2307 return -EFAULT; 2308 2309 sz = data->branch_stack->nr * sizeof(struct branch_entry); 2310 if (evsel__has_branch_hw_idx(evsel)) 2311 sz += sizeof(u64); 2312 else 2313 data->no_hw_idx = true; 2314 OVERFLOW_CHECK(array, sz, max_size); 2315 array = (void *)array + sz; 2316 } 2317 2318 if (type & PERF_SAMPLE_REGS_USER) { 2319 OVERFLOW_CHECK_u64(array); 2320 data->user_regs.abi = *array; 2321 array++; 2322 2323 if (data->user_regs.abi) { 2324 u64 mask = evsel->core.attr.sample_regs_user; 2325 2326 sz = hweight64(mask) * sizeof(u64); 2327 OVERFLOW_CHECK(array, sz, max_size); 2328 data->user_regs.mask = mask; 2329 data->user_regs.regs = (u64 *)array; 2330 array = (void *)array + sz; 2331 } 2332 } 2333 2334 if (type & PERF_SAMPLE_STACK_USER) { 2335 OVERFLOW_CHECK_u64(array); 2336 sz = *array++; 2337 2338 data->user_stack.offset = ((char *)(array - 1) 2339 - (char *) event); 2340 2341 if (!sz) { 2342 data->user_stack.size = 0; 2343 } else { 2344 OVERFLOW_CHECK(array, sz, max_size); 2345 data->user_stack.data = (char *)array; 2346 array = (void *)array + sz; 2347 OVERFLOW_CHECK_u64(array); 2348 data->user_stack.size = *array++; 2349 if (WARN_ONCE(data->user_stack.size > sz, 2350 "user stack dump failure\n")) 2351 return -EFAULT; 2352 } 2353 } 2354 2355 if (type & PERF_SAMPLE_WEIGHT_TYPE) { 2356 OVERFLOW_CHECK_u64(array); 2357 arch_perf_parse_sample_weight(data, array, type); 2358 array++; 2359 } 2360 2361 if (type & PERF_SAMPLE_DATA_SRC) { 2362 OVERFLOW_CHECK_u64(array); 2363 data->data_src = *array; 2364 array++; 2365 } 2366 2367 if (type & PERF_SAMPLE_TRANSACTION) { 2368 OVERFLOW_CHECK_u64(array); 2369 data->transaction = *array; 2370 array++; 2371 } 2372 2373 data->intr_regs.abi = PERF_SAMPLE_REGS_ABI_NONE; 2374 if (type & PERF_SAMPLE_REGS_INTR) { 2375 OVERFLOW_CHECK_u64(array); 2376 data->intr_regs.abi = *array; 2377 array++; 2378 2379 if (data->intr_regs.abi != PERF_SAMPLE_REGS_ABI_NONE) { 2380 u64 mask = evsel->core.attr.sample_regs_intr; 2381 2382 sz = hweight64(mask) * sizeof(u64); 2383 OVERFLOW_CHECK(array, sz, max_size); 2384 data->intr_regs.mask = mask; 2385 data->intr_regs.regs = (u64 *)array; 2386 array = (void *)array + sz; 2387 } 2388 } 2389 2390 data->phys_addr = 0; 2391 if (type & PERF_SAMPLE_PHYS_ADDR) { 2392 data->phys_addr = *array; 2393 array++; 2394 } 2395 2396 data->cgroup = 0; 2397 if (type & PERF_SAMPLE_CGROUP) { 2398 data->cgroup = *array; 2399 array++; 2400 } 2401 2402 data->data_page_size = 0; 2403 if (type & PERF_SAMPLE_DATA_PAGE_SIZE) { 2404 data->data_page_size = *array; 2405 array++; 2406 } 2407 2408 data->code_page_size = 0; 2409 if (type & PERF_SAMPLE_CODE_PAGE_SIZE) { 2410 data->code_page_size = *array; 2411 array++; 2412 } 2413 2414 if (type & PERF_SAMPLE_AUX) { 2415 OVERFLOW_CHECK_u64(array); 2416 sz = *array++; 2417 2418 OVERFLOW_CHECK(array, sz, max_size); 2419 /* Undo swap of data */ 2420 if (swapped) 2421 mem_bswap_64((char *)array, sz); 2422 data->aux_sample.size = sz; 2423 data->aux_sample.data = (char *)array; 2424 array = (void *)array + sz; 2425 } 2426 2427 return 0; 2428 } 2429 2430 int evsel__parse_sample_timestamp(struct evsel *evsel, union perf_event *event, 2431 u64 *timestamp) 2432 { 2433 u64 type = evsel->core.attr.sample_type; 2434 const __u64 *array; 2435 2436 if (!(type & PERF_SAMPLE_TIME)) 2437 return -1; 2438 2439 if (event->header.type != PERF_RECORD_SAMPLE) { 2440 struct perf_sample data = { 2441 .time = -1ULL, 2442 }; 2443 2444 if (!evsel->core.attr.sample_id_all) 2445 return -1; 2446 if (perf_evsel__parse_id_sample(evsel, event, &data)) 2447 return -1; 2448 2449 *timestamp = data.time; 2450 return 0; 2451 } 2452 2453 array = event->sample.array; 2454 2455 if (perf_event__check_size(event, evsel->sample_size)) 2456 return -EFAULT; 2457 2458 if (type & PERF_SAMPLE_IDENTIFIER) 2459 array++; 2460 2461 if (type & PERF_SAMPLE_IP) 2462 array++; 2463 2464 if (type & PERF_SAMPLE_TID) 2465 array++; 2466 2467 if (type & PERF_SAMPLE_TIME) 2468 *timestamp = *array; 2469 2470 return 0; 2471 } 2472 2473 struct tep_format_field *evsel__field(struct evsel *evsel, const char *name) 2474 { 2475 return tep_find_field(evsel->tp_format, name); 2476 } 2477 2478 void *evsel__rawptr(struct evsel *evsel, struct perf_sample *sample, const char *name) 2479 { 2480 struct tep_format_field *field = evsel__field(evsel, name); 2481 int offset; 2482 2483 if (!field) 2484 return NULL; 2485 2486 offset = field->offset; 2487 2488 if (field->flags & TEP_FIELD_IS_DYNAMIC) { 2489 offset = *(int *)(sample->raw_data + field->offset); 2490 offset &= 0xffff; 2491 } 2492 2493 return sample->raw_data + offset; 2494 } 2495 2496 u64 format_field__intval(struct tep_format_field *field, struct perf_sample *sample, 2497 bool needs_swap) 2498 { 2499 u64 value; 2500 void *ptr = sample->raw_data + field->offset; 2501 2502 switch (field->size) { 2503 case 1: 2504 return *(u8 *)ptr; 2505 case 2: 2506 value = *(u16 *)ptr; 2507 break; 2508 case 4: 2509 value = *(u32 *)ptr; 2510 break; 2511 case 8: 2512 memcpy(&value, ptr, sizeof(u64)); 2513 break; 2514 default: 2515 return 0; 2516 } 2517 2518 if (!needs_swap) 2519 return value; 2520 2521 switch (field->size) { 2522 case 2: 2523 return bswap_16(value); 2524 case 4: 2525 return bswap_32(value); 2526 case 8: 2527 return bswap_64(value); 2528 default: 2529 return 0; 2530 } 2531 2532 return 0; 2533 } 2534 2535 u64 evsel__intval(struct evsel *evsel, struct perf_sample *sample, const char *name) 2536 { 2537 struct tep_format_field *field = evsel__field(evsel, name); 2538 2539 if (!field) 2540 return 0; 2541 2542 return field ? format_field__intval(field, sample, evsel->needs_swap) : 0; 2543 } 2544 2545 bool evsel__fallback(struct evsel *evsel, int err, char *msg, size_t msgsize) 2546 { 2547 int paranoid; 2548 2549 if ((err == ENOENT || err == ENXIO || err == ENODEV) && 2550 evsel->core.attr.type == PERF_TYPE_HARDWARE && 2551 evsel->core.attr.config == PERF_COUNT_HW_CPU_CYCLES) { 2552 /* 2553 * If it's cycles then fall back to hrtimer based 2554 * cpu-clock-tick sw counter, which is always available even if 2555 * no PMU support. 2556 * 2557 * PPC returns ENXIO until 2.6.37 (behavior changed with commit 2558 * b0a873e). 2559 */ 2560 scnprintf(msg, msgsize, "%s", 2561 "The cycles event is not supported, trying to fall back to cpu-clock-ticks"); 2562 2563 evsel->core.attr.type = PERF_TYPE_SOFTWARE; 2564 evsel->core.attr.config = PERF_COUNT_SW_CPU_CLOCK; 2565 2566 zfree(&evsel->name); 2567 return true; 2568 } else if (err == EACCES && !evsel->core.attr.exclude_kernel && 2569 (paranoid = perf_event_paranoid()) > 1) { 2570 const char *name = evsel__name(evsel); 2571 char *new_name; 2572 const char *sep = ":"; 2573 2574 /* If event has exclude user then don't exclude kernel. */ 2575 if (evsel->core.attr.exclude_user) 2576 return false; 2577 2578 /* Is there already the separator in the name. */ 2579 if (strchr(name, '/') || 2580 (strchr(name, ':') && !evsel->is_libpfm_event)) 2581 sep = ""; 2582 2583 if (asprintf(&new_name, "%s%su", name, sep) < 0) 2584 return false; 2585 2586 if (evsel->name) 2587 free(evsel->name); 2588 evsel->name = new_name; 2589 scnprintf(msg, msgsize, "kernel.perf_event_paranoid=%d, trying " 2590 "to fall back to excluding kernel and hypervisor " 2591 " samples", paranoid); 2592 evsel->core.attr.exclude_kernel = 1; 2593 evsel->core.attr.exclude_hv = 1; 2594 2595 return true; 2596 } 2597 2598 return false; 2599 } 2600 2601 static bool find_process(const char *name) 2602 { 2603 size_t len = strlen(name); 2604 DIR *dir; 2605 struct dirent *d; 2606 int ret = -1; 2607 2608 dir = opendir(procfs__mountpoint()); 2609 if (!dir) 2610 return false; 2611 2612 /* Walk through the directory. */ 2613 while (ret && (d = readdir(dir)) != NULL) { 2614 char path[PATH_MAX]; 2615 char *data; 2616 size_t size; 2617 2618 if ((d->d_type != DT_DIR) || 2619 !strcmp(".", d->d_name) || 2620 !strcmp("..", d->d_name)) 2621 continue; 2622 2623 scnprintf(path, sizeof(path), "%s/%s/comm", 2624 procfs__mountpoint(), d->d_name); 2625 2626 if (filename__read_str(path, &data, &size)) 2627 continue; 2628 2629 ret = strncmp(name, data, len); 2630 free(data); 2631 } 2632 2633 closedir(dir); 2634 return ret ? false : true; 2635 } 2636 2637 int evsel__open_strerror(struct evsel *evsel, struct target *target, 2638 int err, char *msg, size_t size) 2639 { 2640 char sbuf[STRERR_BUFSIZE]; 2641 int printed = 0, enforced = 0; 2642 2643 switch (err) { 2644 case EPERM: 2645 case EACCES: 2646 printed += scnprintf(msg + printed, size - printed, 2647 "Access to performance monitoring and observability operations is limited.\n"); 2648 2649 if (!sysfs__read_int("fs/selinux/enforce", &enforced)) { 2650 if (enforced) { 2651 printed += scnprintf(msg + printed, size - printed, 2652 "Enforced MAC policy settings (SELinux) can limit access to performance\n" 2653 "monitoring and observability operations. Inspect system audit records for\n" 2654 "more perf_event access control information and adjusting the policy.\n"); 2655 } 2656 } 2657 2658 if (err == EPERM) 2659 printed += scnprintf(msg, size, 2660 "No permission to enable %s event.\n\n", evsel__name(evsel)); 2661 2662 return scnprintf(msg + printed, size - printed, 2663 "Consider adjusting /proc/sys/kernel/perf_event_paranoid setting to open\n" 2664 "access to performance monitoring and observability operations for processes\n" 2665 "without CAP_PERFMON, CAP_SYS_PTRACE or CAP_SYS_ADMIN Linux capability.\n" 2666 "More information can be found at 'Perf events and tool security' document:\n" 2667 "https://www.kernel.org/doc/html/latest/admin-guide/perf-security.html\n" 2668 "perf_event_paranoid setting is %d:\n" 2669 " -1: Allow use of (almost) all events by all users\n" 2670 " Ignore mlock limit after perf_event_mlock_kb without CAP_IPC_LOCK\n" 2671 ">= 0: Disallow raw and ftrace function tracepoint access\n" 2672 ">= 1: Disallow CPU event access\n" 2673 ">= 2: Disallow kernel profiling\n" 2674 "To make the adjusted perf_event_paranoid setting permanent preserve it\n" 2675 "in /etc/sysctl.conf (e.g. kernel.perf_event_paranoid = <setting>)", 2676 perf_event_paranoid()); 2677 case ENOENT: 2678 return scnprintf(msg, size, "The %s event is not supported.", evsel__name(evsel)); 2679 case EMFILE: 2680 return scnprintf(msg, size, "%s", 2681 "Too many events are opened.\n" 2682 "Probably the maximum number of open file descriptors has been reached.\n" 2683 "Hint: Try again after reducing the number of events.\n" 2684 "Hint: Try increasing the limit with 'ulimit -n <limit>'"); 2685 case ENOMEM: 2686 if (evsel__has_callchain(evsel) && 2687 access("/proc/sys/kernel/perf_event_max_stack", F_OK) == 0) 2688 return scnprintf(msg, size, 2689 "Not enough memory to setup event with callchain.\n" 2690 "Hint: Try tweaking /proc/sys/kernel/perf_event_max_stack\n" 2691 "Hint: Current value: %d", sysctl__max_stack()); 2692 break; 2693 case ENODEV: 2694 if (target->cpu_list) 2695 return scnprintf(msg, size, "%s", 2696 "No such device - did you specify an out-of-range profile CPU?"); 2697 break; 2698 case EOPNOTSUPP: 2699 if (evsel->core.attr.aux_output) 2700 return scnprintf(msg, size, 2701 "%s: PMU Hardware doesn't support 'aux_output' feature", 2702 evsel__name(evsel)); 2703 if (evsel->core.attr.sample_period != 0) 2704 return scnprintf(msg, size, 2705 "%s: PMU Hardware doesn't support sampling/overflow-interrupts. Try 'perf stat'", 2706 evsel__name(evsel)); 2707 if (evsel->core.attr.precise_ip) 2708 return scnprintf(msg, size, "%s", 2709 "\'precise\' request may not be supported. Try removing 'p' modifier."); 2710 #if defined(__i386__) || defined(__x86_64__) 2711 if (evsel->core.attr.type == PERF_TYPE_HARDWARE) 2712 return scnprintf(msg, size, "%s", 2713 "No hardware sampling interrupt available.\n"); 2714 #endif 2715 break; 2716 case EBUSY: 2717 if (find_process("oprofiled")) 2718 return scnprintf(msg, size, 2719 "The PMU counters are busy/taken by another profiler.\n" 2720 "We found oprofile daemon running, please stop it and try again."); 2721 break; 2722 case EINVAL: 2723 if (evsel->core.attr.sample_type & PERF_SAMPLE_CODE_PAGE_SIZE && perf_missing_features.code_page_size) 2724 return scnprintf(msg, size, "Asking for the code page size isn't supported by this kernel."); 2725 if (evsel->core.attr.sample_type & PERF_SAMPLE_DATA_PAGE_SIZE && perf_missing_features.data_page_size) 2726 return scnprintf(msg, size, "Asking for the data page size isn't supported by this kernel."); 2727 if (evsel->core.attr.write_backward && perf_missing_features.write_backward) 2728 return scnprintf(msg, size, "Reading from overwrite event is not supported by this kernel."); 2729 if (perf_missing_features.clockid) 2730 return scnprintf(msg, size, "clockid feature not supported."); 2731 if (perf_missing_features.clockid_wrong) 2732 return scnprintf(msg, size, "wrong clockid (%d).", clockid); 2733 if (perf_missing_features.aux_output) 2734 return scnprintf(msg, size, "The 'aux_output' feature is not supported, update the kernel."); 2735 break; 2736 case ENODATA: 2737 return scnprintf(msg, size, "Cannot collect data source with the load latency event alone. " 2738 "Please add an auxiliary event in front of the load latency event."); 2739 default: 2740 break; 2741 } 2742 2743 return scnprintf(msg, size, 2744 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n" 2745 "/bin/dmesg | grep -i perf may provide additional information.\n", 2746 err, str_error_r(err, sbuf, sizeof(sbuf)), evsel__name(evsel)); 2747 } 2748 2749 struct perf_env *evsel__env(struct evsel *evsel) 2750 { 2751 if (evsel && evsel->evlist) 2752 return evsel->evlist->env; 2753 return &perf_env; 2754 } 2755 2756 static int store_evsel_ids(struct evsel *evsel, struct evlist *evlist) 2757 { 2758 int cpu, thread; 2759 2760 for (cpu = 0; cpu < xyarray__max_x(evsel->core.fd); cpu++) { 2761 for (thread = 0; thread < xyarray__max_y(evsel->core.fd); 2762 thread++) { 2763 int fd = FD(evsel, cpu, thread); 2764 2765 if (perf_evlist__id_add_fd(&evlist->core, &evsel->core, 2766 cpu, thread, fd) < 0) 2767 return -1; 2768 } 2769 } 2770 2771 return 0; 2772 } 2773 2774 int evsel__store_ids(struct evsel *evsel, struct evlist *evlist) 2775 { 2776 struct perf_cpu_map *cpus = evsel->core.cpus; 2777 struct perf_thread_map *threads = evsel->core.threads; 2778 2779 if (perf_evsel__alloc_id(&evsel->core, cpus->nr, threads->nr)) 2780 return -ENOMEM; 2781 2782 return store_evsel_ids(evsel, evlist); 2783 } 2784