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