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