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