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