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