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