1 #include <linux/kernel.h> 2 #include <traceevent/event-parse.h> 3 4 #include <byteswap.h> 5 #include <unistd.h> 6 #include <sys/types.h> 7 #include <sys/mman.h> 8 9 #include "evlist.h" 10 #include "evsel.h" 11 #include "session.h" 12 #include "tool.h" 13 #include "sort.h" 14 #include "util.h" 15 #include "cpumap.h" 16 #include "perf_regs.h" 17 #include "asm/bug.h" 18 #include "auxtrace.h" 19 #include "thread-stack.h" 20 21 static int perf_session__deliver_event(struct perf_session *session, 22 union perf_event *event, 23 struct perf_sample *sample, 24 struct perf_tool *tool, 25 u64 file_offset); 26 27 static int perf_session__open(struct perf_session *session) 28 { 29 struct perf_data_file *file = session->file; 30 31 if (perf_session__read_header(session) < 0) { 32 pr_err("incompatible file format (rerun with -v to learn more)"); 33 return -1; 34 } 35 36 if (perf_data_file__is_pipe(file)) 37 return 0; 38 39 if (!perf_evlist__valid_sample_type(session->evlist)) { 40 pr_err("non matching sample_type"); 41 return -1; 42 } 43 44 if (!perf_evlist__valid_sample_id_all(session->evlist)) { 45 pr_err("non matching sample_id_all"); 46 return -1; 47 } 48 49 if (!perf_evlist__valid_read_format(session->evlist)) { 50 pr_err("non matching read_format"); 51 return -1; 52 } 53 54 return 0; 55 } 56 57 void perf_session__set_id_hdr_size(struct perf_session *session) 58 { 59 u16 id_hdr_size = perf_evlist__id_hdr_size(session->evlist); 60 61 machines__set_id_hdr_size(&session->machines, id_hdr_size); 62 } 63 64 int perf_session__create_kernel_maps(struct perf_session *session) 65 { 66 int ret = machine__create_kernel_maps(&session->machines.host); 67 68 if (ret >= 0) 69 ret = machines__create_guest_kernel_maps(&session->machines); 70 return ret; 71 } 72 73 static void perf_session__destroy_kernel_maps(struct perf_session *session) 74 { 75 machines__destroy_kernel_maps(&session->machines); 76 } 77 78 static bool perf_session__has_comm_exec(struct perf_session *session) 79 { 80 struct perf_evsel *evsel; 81 82 evlist__for_each(session->evlist, evsel) { 83 if (evsel->attr.comm_exec) 84 return true; 85 } 86 87 return false; 88 } 89 90 static void perf_session__set_comm_exec(struct perf_session *session) 91 { 92 bool comm_exec = perf_session__has_comm_exec(session); 93 94 machines__set_comm_exec(&session->machines, comm_exec); 95 } 96 97 static int ordered_events__deliver_event(struct ordered_events *oe, 98 struct ordered_event *event) 99 { 100 struct perf_sample sample; 101 struct perf_session *session = container_of(oe, struct perf_session, 102 ordered_events); 103 int ret = perf_evlist__parse_sample(session->evlist, event->event, &sample); 104 105 if (ret) { 106 pr_err("Can't parse sample, err = %d\n", ret); 107 return ret; 108 } 109 110 return perf_session__deliver_event(session, event->event, &sample, 111 session->tool, event->file_offset); 112 } 113 114 struct perf_session *perf_session__new(struct perf_data_file *file, 115 bool repipe, struct perf_tool *tool) 116 { 117 struct perf_session *session = zalloc(sizeof(*session)); 118 119 if (!session) 120 goto out; 121 122 session->repipe = repipe; 123 session->tool = tool; 124 INIT_LIST_HEAD(&session->auxtrace_index); 125 machines__init(&session->machines); 126 ordered_events__init(&session->ordered_events, ordered_events__deliver_event); 127 128 if (file) { 129 if (perf_data_file__open(file)) 130 goto out_delete; 131 132 session->file = file; 133 134 if (perf_data_file__is_read(file)) { 135 if (perf_session__open(session) < 0) 136 goto out_close; 137 138 perf_session__set_id_hdr_size(session); 139 perf_session__set_comm_exec(session); 140 } 141 } 142 143 if (!file || perf_data_file__is_write(file)) { 144 /* 145 * In O_RDONLY mode this will be performed when reading the 146 * kernel MMAP event, in perf_event__process_mmap(). 147 */ 148 if (perf_session__create_kernel_maps(session) < 0) 149 pr_warning("Cannot read kernel map\n"); 150 } 151 152 if (tool && tool->ordering_requires_timestamps && 153 tool->ordered_events && !perf_evlist__sample_id_all(session->evlist)) { 154 dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n"); 155 tool->ordered_events = false; 156 } 157 158 return session; 159 160 out_close: 161 perf_data_file__close(file); 162 out_delete: 163 perf_session__delete(session); 164 out: 165 return NULL; 166 } 167 168 static void perf_session__delete_threads(struct perf_session *session) 169 { 170 machine__delete_threads(&session->machines.host); 171 } 172 173 static void perf_session_env__exit(struct perf_env *env) 174 { 175 zfree(&env->hostname); 176 zfree(&env->os_release); 177 zfree(&env->version); 178 zfree(&env->arch); 179 zfree(&env->cpu_desc); 180 zfree(&env->cpuid); 181 182 zfree(&env->cmdline); 183 zfree(&env->cmdline_argv); 184 zfree(&env->sibling_cores); 185 zfree(&env->sibling_threads); 186 zfree(&env->numa_nodes); 187 zfree(&env->pmu_mappings); 188 } 189 190 void perf_session__delete(struct perf_session *session) 191 { 192 auxtrace__free(session); 193 auxtrace_index__free(&session->auxtrace_index); 194 perf_session__destroy_kernel_maps(session); 195 perf_session__delete_threads(session); 196 perf_session_env__exit(&session->header.env); 197 machines__exit(&session->machines); 198 if (session->file) 199 perf_data_file__close(session->file); 200 free(session); 201 } 202 203 static int process_event_synth_tracing_data_stub(struct perf_tool *tool 204 __maybe_unused, 205 union perf_event *event 206 __maybe_unused, 207 struct perf_session *session 208 __maybe_unused) 209 { 210 dump_printf(": unhandled!\n"); 211 return 0; 212 } 213 214 static int process_event_synth_attr_stub(struct perf_tool *tool __maybe_unused, 215 union perf_event *event __maybe_unused, 216 struct perf_evlist **pevlist 217 __maybe_unused) 218 { 219 dump_printf(": unhandled!\n"); 220 return 0; 221 } 222 223 static int process_event_sample_stub(struct perf_tool *tool __maybe_unused, 224 union perf_event *event __maybe_unused, 225 struct perf_sample *sample __maybe_unused, 226 struct perf_evsel *evsel __maybe_unused, 227 struct machine *machine __maybe_unused) 228 { 229 dump_printf(": unhandled!\n"); 230 return 0; 231 } 232 233 static int process_event_stub(struct perf_tool *tool __maybe_unused, 234 union perf_event *event __maybe_unused, 235 struct perf_sample *sample __maybe_unused, 236 struct machine *machine __maybe_unused) 237 { 238 dump_printf(": unhandled!\n"); 239 return 0; 240 } 241 242 static int process_build_id_stub(struct perf_tool *tool __maybe_unused, 243 union perf_event *event __maybe_unused, 244 struct perf_session *session __maybe_unused) 245 { 246 dump_printf(": unhandled!\n"); 247 return 0; 248 } 249 250 static int process_finished_round_stub(struct perf_tool *tool __maybe_unused, 251 union perf_event *event __maybe_unused, 252 struct ordered_events *oe __maybe_unused) 253 { 254 dump_printf(": unhandled!\n"); 255 return 0; 256 } 257 258 static int process_finished_round(struct perf_tool *tool, 259 union perf_event *event, 260 struct ordered_events *oe); 261 262 static int process_id_index_stub(struct perf_tool *tool __maybe_unused, 263 union perf_event *event __maybe_unused, 264 struct perf_session *perf_session 265 __maybe_unused) 266 { 267 dump_printf(": unhandled!\n"); 268 return 0; 269 } 270 271 static int process_event_auxtrace_info_stub(struct perf_tool *tool __maybe_unused, 272 union perf_event *event __maybe_unused, 273 struct perf_session *session __maybe_unused) 274 { 275 dump_printf(": unhandled!\n"); 276 return 0; 277 } 278 279 static int skipn(int fd, off_t n) 280 { 281 char buf[4096]; 282 ssize_t ret; 283 284 while (n > 0) { 285 ret = read(fd, buf, min(n, (off_t)sizeof(buf))); 286 if (ret <= 0) 287 return ret; 288 n -= ret; 289 } 290 291 return 0; 292 } 293 294 static s64 process_event_auxtrace_stub(struct perf_tool *tool __maybe_unused, 295 union perf_event *event, 296 struct perf_session *session 297 __maybe_unused) 298 { 299 dump_printf(": unhandled!\n"); 300 if (perf_data_file__is_pipe(session->file)) 301 skipn(perf_data_file__fd(session->file), event->auxtrace.size); 302 return event->auxtrace.size; 303 } 304 305 static 306 int process_event_auxtrace_error_stub(struct perf_tool *tool __maybe_unused, 307 union perf_event *event __maybe_unused, 308 struct perf_session *session __maybe_unused) 309 { 310 dump_printf(": unhandled!\n"); 311 return 0; 312 } 313 314 void perf_tool__fill_defaults(struct perf_tool *tool) 315 { 316 if (tool->sample == NULL) 317 tool->sample = process_event_sample_stub; 318 if (tool->mmap == NULL) 319 tool->mmap = process_event_stub; 320 if (tool->mmap2 == NULL) 321 tool->mmap2 = process_event_stub; 322 if (tool->comm == NULL) 323 tool->comm = process_event_stub; 324 if (tool->fork == NULL) 325 tool->fork = process_event_stub; 326 if (tool->exit == NULL) 327 tool->exit = process_event_stub; 328 if (tool->lost == NULL) 329 tool->lost = perf_event__process_lost; 330 if (tool->lost_samples == NULL) 331 tool->lost_samples = perf_event__process_lost_samples; 332 if (tool->aux == NULL) 333 tool->aux = perf_event__process_aux; 334 if (tool->itrace_start == NULL) 335 tool->itrace_start = perf_event__process_itrace_start; 336 if (tool->context_switch == NULL) 337 tool->context_switch = perf_event__process_switch; 338 if (tool->read == NULL) 339 tool->read = process_event_sample_stub; 340 if (tool->throttle == NULL) 341 tool->throttle = process_event_stub; 342 if (tool->unthrottle == NULL) 343 tool->unthrottle = process_event_stub; 344 if (tool->attr == NULL) 345 tool->attr = process_event_synth_attr_stub; 346 if (tool->tracing_data == NULL) 347 tool->tracing_data = process_event_synth_tracing_data_stub; 348 if (tool->build_id == NULL) 349 tool->build_id = process_build_id_stub; 350 if (tool->finished_round == NULL) { 351 if (tool->ordered_events) 352 tool->finished_round = process_finished_round; 353 else 354 tool->finished_round = process_finished_round_stub; 355 } 356 if (tool->id_index == NULL) 357 tool->id_index = process_id_index_stub; 358 if (tool->auxtrace_info == NULL) 359 tool->auxtrace_info = process_event_auxtrace_info_stub; 360 if (tool->auxtrace == NULL) 361 tool->auxtrace = process_event_auxtrace_stub; 362 if (tool->auxtrace_error == NULL) 363 tool->auxtrace_error = process_event_auxtrace_error_stub; 364 } 365 366 static void swap_sample_id_all(union perf_event *event, void *data) 367 { 368 void *end = (void *) event + event->header.size; 369 int size = end - data; 370 371 BUG_ON(size % sizeof(u64)); 372 mem_bswap_64(data, size); 373 } 374 375 static void perf_event__all64_swap(union perf_event *event, 376 bool sample_id_all __maybe_unused) 377 { 378 struct perf_event_header *hdr = &event->header; 379 mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr)); 380 } 381 382 static void perf_event__comm_swap(union perf_event *event, bool sample_id_all) 383 { 384 event->comm.pid = bswap_32(event->comm.pid); 385 event->comm.tid = bswap_32(event->comm.tid); 386 387 if (sample_id_all) { 388 void *data = &event->comm.comm; 389 390 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64)); 391 swap_sample_id_all(event, data); 392 } 393 } 394 395 static void perf_event__mmap_swap(union perf_event *event, 396 bool sample_id_all) 397 { 398 event->mmap.pid = bswap_32(event->mmap.pid); 399 event->mmap.tid = bswap_32(event->mmap.tid); 400 event->mmap.start = bswap_64(event->mmap.start); 401 event->mmap.len = bswap_64(event->mmap.len); 402 event->mmap.pgoff = bswap_64(event->mmap.pgoff); 403 404 if (sample_id_all) { 405 void *data = &event->mmap.filename; 406 407 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64)); 408 swap_sample_id_all(event, data); 409 } 410 } 411 412 static void perf_event__mmap2_swap(union perf_event *event, 413 bool sample_id_all) 414 { 415 event->mmap2.pid = bswap_32(event->mmap2.pid); 416 event->mmap2.tid = bswap_32(event->mmap2.tid); 417 event->mmap2.start = bswap_64(event->mmap2.start); 418 event->mmap2.len = bswap_64(event->mmap2.len); 419 event->mmap2.pgoff = bswap_64(event->mmap2.pgoff); 420 event->mmap2.maj = bswap_32(event->mmap2.maj); 421 event->mmap2.min = bswap_32(event->mmap2.min); 422 event->mmap2.ino = bswap_64(event->mmap2.ino); 423 424 if (sample_id_all) { 425 void *data = &event->mmap2.filename; 426 427 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64)); 428 swap_sample_id_all(event, data); 429 } 430 } 431 static void perf_event__task_swap(union perf_event *event, bool sample_id_all) 432 { 433 event->fork.pid = bswap_32(event->fork.pid); 434 event->fork.tid = bswap_32(event->fork.tid); 435 event->fork.ppid = bswap_32(event->fork.ppid); 436 event->fork.ptid = bswap_32(event->fork.ptid); 437 event->fork.time = bswap_64(event->fork.time); 438 439 if (sample_id_all) 440 swap_sample_id_all(event, &event->fork + 1); 441 } 442 443 static void perf_event__read_swap(union perf_event *event, bool sample_id_all) 444 { 445 event->read.pid = bswap_32(event->read.pid); 446 event->read.tid = bswap_32(event->read.tid); 447 event->read.value = bswap_64(event->read.value); 448 event->read.time_enabled = bswap_64(event->read.time_enabled); 449 event->read.time_running = bswap_64(event->read.time_running); 450 event->read.id = bswap_64(event->read.id); 451 452 if (sample_id_all) 453 swap_sample_id_all(event, &event->read + 1); 454 } 455 456 static void perf_event__aux_swap(union perf_event *event, bool sample_id_all) 457 { 458 event->aux.aux_offset = bswap_64(event->aux.aux_offset); 459 event->aux.aux_size = bswap_64(event->aux.aux_size); 460 event->aux.flags = bswap_64(event->aux.flags); 461 462 if (sample_id_all) 463 swap_sample_id_all(event, &event->aux + 1); 464 } 465 466 static void perf_event__itrace_start_swap(union perf_event *event, 467 bool sample_id_all) 468 { 469 event->itrace_start.pid = bswap_32(event->itrace_start.pid); 470 event->itrace_start.tid = bswap_32(event->itrace_start.tid); 471 472 if (sample_id_all) 473 swap_sample_id_all(event, &event->itrace_start + 1); 474 } 475 476 static void perf_event__switch_swap(union perf_event *event, bool sample_id_all) 477 { 478 if (event->header.type == PERF_RECORD_SWITCH_CPU_WIDE) { 479 event->context_switch.next_prev_pid = 480 bswap_32(event->context_switch.next_prev_pid); 481 event->context_switch.next_prev_tid = 482 bswap_32(event->context_switch.next_prev_tid); 483 } 484 485 if (sample_id_all) 486 swap_sample_id_all(event, &event->context_switch + 1); 487 } 488 489 static void perf_event__throttle_swap(union perf_event *event, 490 bool sample_id_all) 491 { 492 event->throttle.time = bswap_64(event->throttle.time); 493 event->throttle.id = bswap_64(event->throttle.id); 494 event->throttle.stream_id = bswap_64(event->throttle.stream_id); 495 496 if (sample_id_all) 497 swap_sample_id_all(event, &event->throttle + 1); 498 } 499 500 static u8 revbyte(u8 b) 501 { 502 int rev = (b >> 4) | ((b & 0xf) << 4); 503 rev = ((rev & 0xcc) >> 2) | ((rev & 0x33) << 2); 504 rev = ((rev & 0xaa) >> 1) | ((rev & 0x55) << 1); 505 return (u8) rev; 506 } 507 508 /* 509 * XXX this is hack in attempt to carry flags bitfield 510 * throught endian village. ABI says: 511 * 512 * Bit-fields are allocated from right to left (least to most significant) 513 * on little-endian implementations and from left to right (most to least 514 * significant) on big-endian implementations. 515 * 516 * The above seems to be byte specific, so we need to reverse each 517 * byte of the bitfield. 'Internet' also says this might be implementation 518 * specific and we probably need proper fix and carry perf_event_attr 519 * bitfield flags in separate data file FEAT_ section. Thought this seems 520 * to work for now. 521 */ 522 static void swap_bitfield(u8 *p, unsigned len) 523 { 524 unsigned i; 525 526 for (i = 0; i < len; i++) { 527 *p = revbyte(*p); 528 p++; 529 } 530 } 531 532 /* exported for swapping attributes in file header */ 533 void perf_event__attr_swap(struct perf_event_attr *attr) 534 { 535 attr->type = bswap_32(attr->type); 536 attr->size = bswap_32(attr->size); 537 538 #define bswap_safe(f, n) \ 539 (attr->size > (offsetof(struct perf_event_attr, f) + \ 540 sizeof(attr->f) * (n))) 541 #define bswap_field(f, sz) \ 542 do { \ 543 if (bswap_safe(f, 0)) \ 544 attr->f = bswap_##sz(attr->f); \ 545 } while(0) 546 #define bswap_field_32(f) bswap_field(f, 32) 547 #define bswap_field_64(f) bswap_field(f, 64) 548 549 bswap_field_64(config); 550 bswap_field_64(sample_period); 551 bswap_field_64(sample_type); 552 bswap_field_64(read_format); 553 bswap_field_32(wakeup_events); 554 bswap_field_32(bp_type); 555 bswap_field_64(bp_addr); 556 bswap_field_64(bp_len); 557 bswap_field_64(branch_sample_type); 558 bswap_field_64(sample_regs_user); 559 bswap_field_32(sample_stack_user); 560 bswap_field_32(aux_watermark); 561 562 /* 563 * After read_format are bitfields. Check read_format because 564 * we are unable to use offsetof on bitfield. 565 */ 566 if (bswap_safe(read_format, 1)) 567 swap_bitfield((u8 *) (&attr->read_format + 1), 568 sizeof(u64)); 569 #undef bswap_field_64 570 #undef bswap_field_32 571 #undef bswap_field 572 #undef bswap_safe 573 } 574 575 static void perf_event__hdr_attr_swap(union perf_event *event, 576 bool sample_id_all __maybe_unused) 577 { 578 size_t size; 579 580 perf_event__attr_swap(&event->attr.attr); 581 582 size = event->header.size; 583 size -= (void *)&event->attr.id - (void *)event; 584 mem_bswap_64(event->attr.id, size); 585 } 586 587 static void perf_event__event_type_swap(union perf_event *event, 588 bool sample_id_all __maybe_unused) 589 { 590 event->event_type.event_type.event_id = 591 bswap_64(event->event_type.event_type.event_id); 592 } 593 594 static void perf_event__tracing_data_swap(union perf_event *event, 595 bool sample_id_all __maybe_unused) 596 { 597 event->tracing_data.size = bswap_32(event->tracing_data.size); 598 } 599 600 static void perf_event__auxtrace_info_swap(union perf_event *event, 601 bool sample_id_all __maybe_unused) 602 { 603 size_t size; 604 605 event->auxtrace_info.type = bswap_32(event->auxtrace_info.type); 606 607 size = event->header.size; 608 size -= (void *)&event->auxtrace_info.priv - (void *)event; 609 mem_bswap_64(event->auxtrace_info.priv, size); 610 } 611 612 static void perf_event__auxtrace_swap(union perf_event *event, 613 bool sample_id_all __maybe_unused) 614 { 615 event->auxtrace.size = bswap_64(event->auxtrace.size); 616 event->auxtrace.offset = bswap_64(event->auxtrace.offset); 617 event->auxtrace.reference = bswap_64(event->auxtrace.reference); 618 event->auxtrace.idx = bswap_32(event->auxtrace.idx); 619 event->auxtrace.tid = bswap_32(event->auxtrace.tid); 620 event->auxtrace.cpu = bswap_32(event->auxtrace.cpu); 621 } 622 623 static void perf_event__auxtrace_error_swap(union perf_event *event, 624 bool sample_id_all __maybe_unused) 625 { 626 event->auxtrace_error.type = bswap_32(event->auxtrace_error.type); 627 event->auxtrace_error.code = bswap_32(event->auxtrace_error.code); 628 event->auxtrace_error.cpu = bswap_32(event->auxtrace_error.cpu); 629 event->auxtrace_error.pid = bswap_32(event->auxtrace_error.pid); 630 event->auxtrace_error.tid = bswap_32(event->auxtrace_error.tid); 631 event->auxtrace_error.ip = bswap_64(event->auxtrace_error.ip); 632 } 633 634 typedef void (*perf_event__swap_op)(union perf_event *event, 635 bool sample_id_all); 636 637 static perf_event__swap_op perf_event__swap_ops[] = { 638 [PERF_RECORD_MMAP] = perf_event__mmap_swap, 639 [PERF_RECORD_MMAP2] = perf_event__mmap2_swap, 640 [PERF_RECORD_COMM] = perf_event__comm_swap, 641 [PERF_RECORD_FORK] = perf_event__task_swap, 642 [PERF_RECORD_EXIT] = perf_event__task_swap, 643 [PERF_RECORD_LOST] = perf_event__all64_swap, 644 [PERF_RECORD_READ] = perf_event__read_swap, 645 [PERF_RECORD_THROTTLE] = perf_event__throttle_swap, 646 [PERF_RECORD_UNTHROTTLE] = perf_event__throttle_swap, 647 [PERF_RECORD_SAMPLE] = perf_event__all64_swap, 648 [PERF_RECORD_AUX] = perf_event__aux_swap, 649 [PERF_RECORD_ITRACE_START] = perf_event__itrace_start_swap, 650 [PERF_RECORD_LOST_SAMPLES] = perf_event__all64_swap, 651 [PERF_RECORD_SWITCH] = perf_event__switch_swap, 652 [PERF_RECORD_SWITCH_CPU_WIDE] = perf_event__switch_swap, 653 [PERF_RECORD_HEADER_ATTR] = perf_event__hdr_attr_swap, 654 [PERF_RECORD_HEADER_EVENT_TYPE] = perf_event__event_type_swap, 655 [PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap, 656 [PERF_RECORD_HEADER_BUILD_ID] = NULL, 657 [PERF_RECORD_ID_INDEX] = perf_event__all64_swap, 658 [PERF_RECORD_AUXTRACE_INFO] = perf_event__auxtrace_info_swap, 659 [PERF_RECORD_AUXTRACE] = perf_event__auxtrace_swap, 660 [PERF_RECORD_AUXTRACE_ERROR] = perf_event__auxtrace_error_swap, 661 [PERF_RECORD_HEADER_MAX] = NULL, 662 }; 663 664 /* 665 * When perf record finishes a pass on every buffers, it records this pseudo 666 * event. 667 * We record the max timestamp t found in the pass n. 668 * Assuming these timestamps are monotonic across cpus, we know that if 669 * a buffer still has events with timestamps below t, they will be all 670 * available and then read in the pass n + 1. 671 * Hence when we start to read the pass n + 2, we can safely flush every 672 * events with timestamps below t. 673 * 674 * ============ PASS n ================= 675 * CPU 0 | CPU 1 676 * | 677 * cnt1 timestamps | cnt2 timestamps 678 * 1 | 2 679 * 2 | 3 680 * - | 4 <--- max recorded 681 * 682 * ============ PASS n + 1 ============== 683 * CPU 0 | CPU 1 684 * | 685 * cnt1 timestamps | cnt2 timestamps 686 * 3 | 5 687 * 4 | 6 688 * 5 | 7 <---- max recorded 689 * 690 * Flush every events below timestamp 4 691 * 692 * ============ PASS n + 2 ============== 693 * CPU 0 | CPU 1 694 * | 695 * cnt1 timestamps | cnt2 timestamps 696 * 6 | 8 697 * 7 | 9 698 * - | 10 699 * 700 * Flush every events below timestamp 7 701 * etc... 702 */ 703 static int process_finished_round(struct perf_tool *tool __maybe_unused, 704 union perf_event *event __maybe_unused, 705 struct ordered_events *oe) 706 { 707 if (dump_trace) 708 fprintf(stdout, "\n"); 709 return ordered_events__flush(oe, OE_FLUSH__ROUND); 710 } 711 712 int perf_session__queue_event(struct perf_session *s, union perf_event *event, 713 struct perf_sample *sample, u64 file_offset) 714 { 715 return ordered_events__queue(&s->ordered_events, event, sample, file_offset); 716 } 717 718 static void callchain__lbr_callstack_printf(struct perf_sample *sample) 719 { 720 struct ip_callchain *callchain = sample->callchain; 721 struct branch_stack *lbr_stack = sample->branch_stack; 722 u64 kernel_callchain_nr = callchain->nr; 723 unsigned int i; 724 725 for (i = 0; i < kernel_callchain_nr; i++) { 726 if (callchain->ips[i] == PERF_CONTEXT_USER) 727 break; 728 } 729 730 if ((i != kernel_callchain_nr) && lbr_stack->nr) { 731 u64 total_nr; 732 /* 733 * LBR callstack can only get user call chain, 734 * i is kernel call chain number, 735 * 1 is PERF_CONTEXT_USER. 736 * 737 * The user call chain is stored in LBR registers. 738 * LBR are pair registers. The caller is stored 739 * in "from" register, while the callee is stored 740 * in "to" register. 741 * For example, there is a call stack 742 * "A"->"B"->"C"->"D". 743 * The LBR registers will recorde like 744 * "C"->"D", "B"->"C", "A"->"B". 745 * So only the first "to" register and all "from" 746 * registers are needed to construct the whole stack. 747 */ 748 total_nr = i + 1 + lbr_stack->nr + 1; 749 kernel_callchain_nr = i + 1; 750 751 printf("... LBR call chain: nr:%" PRIu64 "\n", total_nr); 752 753 for (i = 0; i < kernel_callchain_nr; i++) 754 printf("..... %2d: %016" PRIx64 "\n", 755 i, callchain->ips[i]); 756 757 printf("..... %2d: %016" PRIx64 "\n", 758 (int)(kernel_callchain_nr), lbr_stack->entries[0].to); 759 for (i = 0; i < lbr_stack->nr; i++) 760 printf("..... %2d: %016" PRIx64 "\n", 761 (int)(i + kernel_callchain_nr + 1), lbr_stack->entries[i].from); 762 } 763 } 764 765 static void callchain__printf(struct perf_evsel *evsel, 766 struct perf_sample *sample) 767 { 768 unsigned int i; 769 struct ip_callchain *callchain = sample->callchain; 770 771 if (has_branch_callstack(evsel)) 772 callchain__lbr_callstack_printf(sample); 773 774 printf("... FP chain: nr:%" PRIu64 "\n", callchain->nr); 775 776 for (i = 0; i < callchain->nr; i++) 777 printf("..... %2d: %016" PRIx64 "\n", 778 i, callchain->ips[i]); 779 } 780 781 static void branch_stack__printf(struct perf_sample *sample) 782 { 783 uint64_t i; 784 785 printf("... branch stack: nr:%" PRIu64 "\n", sample->branch_stack->nr); 786 787 for (i = 0; i < sample->branch_stack->nr; i++) { 788 struct branch_entry *e = &sample->branch_stack->entries[i]; 789 790 printf("..... %2"PRIu64": %016" PRIx64 " -> %016" PRIx64 " %hu cycles %s%s%s%s %x\n", 791 i, e->from, e->to, 792 e->flags.cycles, 793 e->flags.mispred ? "M" : " ", 794 e->flags.predicted ? "P" : " ", 795 e->flags.abort ? "A" : " ", 796 e->flags.in_tx ? "T" : " ", 797 (unsigned)e->flags.reserved); 798 } 799 } 800 801 static void regs_dump__printf(u64 mask, u64 *regs) 802 { 803 unsigned rid, i = 0; 804 805 for_each_set_bit(rid, (unsigned long *) &mask, sizeof(mask) * 8) { 806 u64 val = regs[i++]; 807 808 printf(".... %-5s 0x%" PRIx64 "\n", 809 perf_reg_name(rid), val); 810 } 811 } 812 813 static const char *regs_abi[] = { 814 [PERF_SAMPLE_REGS_ABI_NONE] = "none", 815 [PERF_SAMPLE_REGS_ABI_32] = "32-bit", 816 [PERF_SAMPLE_REGS_ABI_64] = "64-bit", 817 }; 818 819 static inline const char *regs_dump_abi(struct regs_dump *d) 820 { 821 if (d->abi > PERF_SAMPLE_REGS_ABI_64) 822 return "unknown"; 823 824 return regs_abi[d->abi]; 825 } 826 827 static void regs__printf(const char *type, struct regs_dump *regs) 828 { 829 u64 mask = regs->mask; 830 831 printf("... %s regs: mask 0x%" PRIx64 " ABI %s\n", 832 type, 833 mask, 834 regs_dump_abi(regs)); 835 836 regs_dump__printf(mask, regs->regs); 837 } 838 839 static void regs_user__printf(struct perf_sample *sample) 840 { 841 struct regs_dump *user_regs = &sample->user_regs; 842 843 if (user_regs->regs) 844 regs__printf("user", user_regs); 845 } 846 847 static void regs_intr__printf(struct perf_sample *sample) 848 { 849 struct regs_dump *intr_regs = &sample->intr_regs; 850 851 if (intr_regs->regs) 852 regs__printf("intr", intr_regs); 853 } 854 855 static void stack_user__printf(struct stack_dump *dump) 856 { 857 printf("... ustack: size %" PRIu64 ", offset 0x%x\n", 858 dump->size, dump->offset); 859 } 860 861 static void perf_evlist__print_tstamp(struct perf_evlist *evlist, 862 union perf_event *event, 863 struct perf_sample *sample) 864 { 865 u64 sample_type = __perf_evlist__combined_sample_type(evlist); 866 867 if (event->header.type != PERF_RECORD_SAMPLE && 868 !perf_evlist__sample_id_all(evlist)) { 869 fputs("-1 -1 ", stdout); 870 return; 871 } 872 873 if ((sample_type & PERF_SAMPLE_CPU)) 874 printf("%u ", sample->cpu); 875 876 if (sample_type & PERF_SAMPLE_TIME) 877 printf("%" PRIu64 " ", sample->time); 878 } 879 880 static void sample_read__printf(struct perf_sample *sample, u64 read_format) 881 { 882 printf("... sample_read:\n"); 883 884 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 885 printf("...... time enabled %016" PRIx64 "\n", 886 sample->read.time_enabled); 887 888 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 889 printf("...... time running %016" PRIx64 "\n", 890 sample->read.time_running); 891 892 if (read_format & PERF_FORMAT_GROUP) { 893 u64 i; 894 895 printf(".... group nr %" PRIu64 "\n", sample->read.group.nr); 896 897 for (i = 0; i < sample->read.group.nr; i++) { 898 struct sample_read_value *value; 899 900 value = &sample->read.group.values[i]; 901 printf("..... id %016" PRIx64 902 ", value %016" PRIx64 "\n", 903 value->id, value->value); 904 } 905 } else 906 printf("..... id %016" PRIx64 ", value %016" PRIx64 "\n", 907 sample->read.one.id, sample->read.one.value); 908 } 909 910 static void dump_event(struct perf_evlist *evlist, union perf_event *event, 911 u64 file_offset, struct perf_sample *sample) 912 { 913 if (!dump_trace) 914 return; 915 916 printf("\n%#" PRIx64 " [%#x]: event: %d\n", 917 file_offset, event->header.size, event->header.type); 918 919 trace_event(event); 920 921 if (sample) 922 perf_evlist__print_tstamp(evlist, event, sample); 923 924 printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset, 925 event->header.size, perf_event__name(event->header.type)); 926 } 927 928 static void dump_sample(struct perf_evsel *evsel, union perf_event *event, 929 struct perf_sample *sample) 930 { 931 u64 sample_type; 932 933 if (!dump_trace) 934 return; 935 936 printf("(IP, 0x%x): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n", 937 event->header.misc, sample->pid, sample->tid, sample->ip, 938 sample->period, sample->addr); 939 940 sample_type = evsel->attr.sample_type; 941 942 if (sample_type & PERF_SAMPLE_CALLCHAIN) 943 callchain__printf(evsel, sample); 944 945 if ((sample_type & PERF_SAMPLE_BRANCH_STACK) && !has_branch_callstack(evsel)) 946 branch_stack__printf(sample); 947 948 if (sample_type & PERF_SAMPLE_REGS_USER) 949 regs_user__printf(sample); 950 951 if (sample_type & PERF_SAMPLE_REGS_INTR) 952 regs_intr__printf(sample); 953 954 if (sample_type & PERF_SAMPLE_STACK_USER) 955 stack_user__printf(&sample->user_stack); 956 957 if (sample_type & PERF_SAMPLE_WEIGHT) 958 printf("... weight: %" PRIu64 "\n", sample->weight); 959 960 if (sample_type & PERF_SAMPLE_DATA_SRC) 961 printf(" . data_src: 0x%"PRIx64"\n", sample->data_src); 962 963 if (sample_type & PERF_SAMPLE_TRANSACTION) 964 printf("... transaction: %" PRIx64 "\n", sample->transaction); 965 966 if (sample_type & PERF_SAMPLE_READ) 967 sample_read__printf(sample, evsel->attr.read_format); 968 } 969 970 static struct machine *machines__find_for_cpumode(struct machines *machines, 971 union perf_event *event, 972 struct perf_sample *sample) 973 { 974 const u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; 975 struct machine *machine; 976 977 if (perf_guest && 978 ((cpumode == PERF_RECORD_MISC_GUEST_KERNEL) || 979 (cpumode == PERF_RECORD_MISC_GUEST_USER))) { 980 u32 pid; 981 982 if (event->header.type == PERF_RECORD_MMAP 983 || event->header.type == PERF_RECORD_MMAP2) 984 pid = event->mmap.pid; 985 else 986 pid = sample->pid; 987 988 machine = machines__find(machines, pid); 989 if (!machine) 990 machine = machines__find(machines, DEFAULT_GUEST_KERNEL_ID); 991 return machine; 992 } 993 994 return &machines->host; 995 } 996 997 static int deliver_sample_value(struct perf_evlist *evlist, 998 struct perf_tool *tool, 999 union perf_event *event, 1000 struct perf_sample *sample, 1001 struct sample_read_value *v, 1002 struct machine *machine) 1003 { 1004 struct perf_sample_id *sid = perf_evlist__id2sid(evlist, v->id); 1005 1006 if (sid) { 1007 sample->id = v->id; 1008 sample->period = v->value - sid->period; 1009 sid->period = v->value; 1010 } 1011 1012 if (!sid || sid->evsel == NULL) { 1013 ++evlist->stats.nr_unknown_id; 1014 return 0; 1015 } 1016 1017 return tool->sample(tool, event, sample, sid->evsel, machine); 1018 } 1019 1020 static int deliver_sample_group(struct perf_evlist *evlist, 1021 struct perf_tool *tool, 1022 union perf_event *event, 1023 struct perf_sample *sample, 1024 struct machine *machine) 1025 { 1026 int ret = -EINVAL; 1027 u64 i; 1028 1029 for (i = 0; i < sample->read.group.nr; i++) { 1030 ret = deliver_sample_value(evlist, tool, event, sample, 1031 &sample->read.group.values[i], 1032 machine); 1033 if (ret) 1034 break; 1035 } 1036 1037 return ret; 1038 } 1039 1040 static int 1041 perf_evlist__deliver_sample(struct perf_evlist *evlist, 1042 struct perf_tool *tool, 1043 union perf_event *event, 1044 struct perf_sample *sample, 1045 struct perf_evsel *evsel, 1046 struct machine *machine) 1047 { 1048 /* We know evsel != NULL. */ 1049 u64 sample_type = evsel->attr.sample_type; 1050 u64 read_format = evsel->attr.read_format; 1051 1052 /* Standard sample delievery. */ 1053 if (!(sample_type & PERF_SAMPLE_READ)) 1054 return tool->sample(tool, event, sample, evsel, machine); 1055 1056 /* For PERF_SAMPLE_READ we have either single or group mode. */ 1057 if (read_format & PERF_FORMAT_GROUP) 1058 return deliver_sample_group(evlist, tool, event, sample, 1059 machine); 1060 else 1061 return deliver_sample_value(evlist, tool, event, sample, 1062 &sample->read.one, machine); 1063 } 1064 1065 static int machines__deliver_event(struct machines *machines, 1066 struct perf_evlist *evlist, 1067 union perf_event *event, 1068 struct perf_sample *sample, 1069 struct perf_tool *tool, u64 file_offset) 1070 { 1071 struct perf_evsel *evsel; 1072 struct machine *machine; 1073 1074 dump_event(evlist, event, file_offset, sample); 1075 1076 evsel = perf_evlist__id2evsel(evlist, sample->id); 1077 1078 machine = machines__find_for_cpumode(machines, event, sample); 1079 1080 switch (event->header.type) { 1081 case PERF_RECORD_SAMPLE: 1082 dump_sample(evsel, event, sample); 1083 if (evsel == NULL) { 1084 ++evlist->stats.nr_unknown_id; 1085 return 0; 1086 } 1087 if (machine == NULL) { 1088 ++evlist->stats.nr_unprocessable_samples; 1089 return 0; 1090 } 1091 return perf_evlist__deliver_sample(evlist, tool, event, sample, evsel, machine); 1092 case PERF_RECORD_MMAP: 1093 return tool->mmap(tool, event, sample, machine); 1094 case PERF_RECORD_MMAP2: 1095 if (event->header.misc & PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT) 1096 ++evlist->stats.nr_proc_map_timeout; 1097 return tool->mmap2(tool, event, sample, machine); 1098 case PERF_RECORD_COMM: 1099 return tool->comm(tool, event, sample, machine); 1100 case PERF_RECORD_FORK: 1101 return tool->fork(tool, event, sample, machine); 1102 case PERF_RECORD_EXIT: 1103 return tool->exit(tool, event, sample, machine); 1104 case PERF_RECORD_LOST: 1105 if (tool->lost == perf_event__process_lost) 1106 evlist->stats.total_lost += event->lost.lost; 1107 return tool->lost(tool, event, sample, machine); 1108 case PERF_RECORD_LOST_SAMPLES: 1109 if (tool->lost_samples == perf_event__process_lost_samples) 1110 evlist->stats.total_lost_samples += event->lost_samples.lost; 1111 return tool->lost_samples(tool, event, sample, machine); 1112 case PERF_RECORD_READ: 1113 return tool->read(tool, event, sample, evsel, machine); 1114 case PERF_RECORD_THROTTLE: 1115 return tool->throttle(tool, event, sample, machine); 1116 case PERF_RECORD_UNTHROTTLE: 1117 return tool->unthrottle(tool, event, sample, machine); 1118 case PERF_RECORD_AUX: 1119 return tool->aux(tool, event, sample, machine); 1120 case PERF_RECORD_ITRACE_START: 1121 return tool->itrace_start(tool, event, sample, machine); 1122 case PERF_RECORD_SWITCH: 1123 case PERF_RECORD_SWITCH_CPU_WIDE: 1124 return tool->context_switch(tool, event, sample, machine); 1125 default: 1126 ++evlist->stats.nr_unknown_events; 1127 return -1; 1128 } 1129 } 1130 1131 static int perf_session__deliver_event(struct perf_session *session, 1132 union perf_event *event, 1133 struct perf_sample *sample, 1134 struct perf_tool *tool, 1135 u64 file_offset) 1136 { 1137 int ret; 1138 1139 ret = auxtrace__process_event(session, event, sample, tool); 1140 if (ret < 0) 1141 return ret; 1142 if (ret > 0) 1143 return 0; 1144 1145 return machines__deliver_event(&session->machines, session->evlist, 1146 event, sample, tool, file_offset); 1147 } 1148 1149 static s64 perf_session__process_user_event(struct perf_session *session, 1150 union perf_event *event, 1151 u64 file_offset) 1152 { 1153 struct ordered_events *oe = &session->ordered_events; 1154 struct perf_tool *tool = session->tool; 1155 int fd = perf_data_file__fd(session->file); 1156 int err; 1157 1158 dump_event(session->evlist, event, file_offset, NULL); 1159 1160 /* These events are processed right away */ 1161 switch (event->header.type) { 1162 case PERF_RECORD_HEADER_ATTR: 1163 err = tool->attr(tool, event, &session->evlist); 1164 if (err == 0) { 1165 perf_session__set_id_hdr_size(session); 1166 perf_session__set_comm_exec(session); 1167 } 1168 return err; 1169 case PERF_RECORD_HEADER_EVENT_TYPE: 1170 /* 1171 * Depreceated, but we need to handle it for sake 1172 * of old data files create in pipe mode. 1173 */ 1174 return 0; 1175 case PERF_RECORD_HEADER_TRACING_DATA: 1176 /* setup for reading amidst mmap */ 1177 lseek(fd, file_offset, SEEK_SET); 1178 return tool->tracing_data(tool, event, session); 1179 case PERF_RECORD_HEADER_BUILD_ID: 1180 return tool->build_id(tool, event, session); 1181 case PERF_RECORD_FINISHED_ROUND: 1182 return tool->finished_round(tool, event, oe); 1183 case PERF_RECORD_ID_INDEX: 1184 return tool->id_index(tool, event, session); 1185 case PERF_RECORD_AUXTRACE_INFO: 1186 return tool->auxtrace_info(tool, event, session); 1187 case PERF_RECORD_AUXTRACE: 1188 /* setup for reading amidst mmap */ 1189 lseek(fd, file_offset + event->header.size, SEEK_SET); 1190 return tool->auxtrace(tool, event, session); 1191 case PERF_RECORD_AUXTRACE_ERROR: 1192 perf_session__auxtrace_error_inc(session, event); 1193 return tool->auxtrace_error(tool, event, session); 1194 default: 1195 return -EINVAL; 1196 } 1197 } 1198 1199 int perf_session__deliver_synth_event(struct perf_session *session, 1200 union perf_event *event, 1201 struct perf_sample *sample) 1202 { 1203 struct perf_evlist *evlist = session->evlist; 1204 struct perf_tool *tool = session->tool; 1205 1206 events_stats__inc(&evlist->stats, event->header.type); 1207 1208 if (event->header.type >= PERF_RECORD_USER_TYPE_START) 1209 return perf_session__process_user_event(session, event, 0); 1210 1211 return machines__deliver_event(&session->machines, evlist, event, sample, tool, 0); 1212 } 1213 1214 static void event_swap(union perf_event *event, bool sample_id_all) 1215 { 1216 perf_event__swap_op swap; 1217 1218 swap = perf_event__swap_ops[event->header.type]; 1219 if (swap) 1220 swap(event, sample_id_all); 1221 } 1222 1223 int perf_session__peek_event(struct perf_session *session, off_t file_offset, 1224 void *buf, size_t buf_sz, 1225 union perf_event **event_ptr, 1226 struct perf_sample *sample) 1227 { 1228 union perf_event *event; 1229 size_t hdr_sz, rest; 1230 int fd; 1231 1232 if (session->one_mmap && !session->header.needs_swap) { 1233 event = file_offset - session->one_mmap_offset + 1234 session->one_mmap_addr; 1235 goto out_parse_sample; 1236 } 1237 1238 if (perf_data_file__is_pipe(session->file)) 1239 return -1; 1240 1241 fd = perf_data_file__fd(session->file); 1242 hdr_sz = sizeof(struct perf_event_header); 1243 1244 if (buf_sz < hdr_sz) 1245 return -1; 1246 1247 if (lseek(fd, file_offset, SEEK_SET) == (off_t)-1 || 1248 readn(fd, buf, hdr_sz) != (ssize_t)hdr_sz) 1249 return -1; 1250 1251 event = (union perf_event *)buf; 1252 1253 if (session->header.needs_swap) 1254 perf_event_header__bswap(&event->header); 1255 1256 if (event->header.size < hdr_sz || event->header.size > buf_sz) 1257 return -1; 1258 1259 rest = event->header.size - hdr_sz; 1260 1261 if (readn(fd, buf, rest) != (ssize_t)rest) 1262 return -1; 1263 1264 if (session->header.needs_swap) 1265 event_swap(event, perf_evlist__sample_id_all(session->evlist)); 1266 1267 out_parse_sample: 1268 1269 if (sample && event->header.type < PERF_RECORD_USER_TYPE_START && 1270 perf_evlist__parse_sample(session->evlist, event, sample)) 1271 return -1; 1272 1273 *event_ptr = event; 1274 1275 return 0; 1276 } 1277 1278 static s64 perf_session__process_event(struct perf_session *session, 1279 union perf_event *event, u64 file_offset) 1280 { 1281 struct perf_evlist *evlist = session->evlist; 1282 struct perf_tool *tool = session->tool; 1283 struct perf_sample sample; 1284 int ret; 1285 1286 if (session->header.needs_swap) 1287 event_swap(event, perf_evlist__sample_id_all(evlist)); 1288 1289 if (event->header.type >= PERF_RECORD_HEADER_MAX) 1290 return -EINVAL; 1291 1292 events_stats__inc(&evlist->stats, event->header.type); 1293 1294 if (event->header.type >= PERF_RECORD_USER_TYPE_START) 1295 return perf_session__process_user_event(session, event, file_offset); 1296 1297 /* 1298 * For all kernel events we get the sample data 1299 */ 1300 ret = perf_evlist__parse_sample(evlist, event, &sample); 1301 if (ret) 1302 return ret; 1303 1304 if (tool->ordered_events) { 1305 ret = perf_session__queue_event(session, event, &sample, file_offset); 1306 if (ret != -ETIME) 1307 return ret; 1308 } 1309 1310 return perf_session__deliver_event(session, event, &sample, tool, 1311 file_offset); 1312 } 1313 1314 void perf_event_header__bswap(struct perf_event_header *hdr) 1315 { 1316 hdr->type = bswap_32(hdr->type); 1317 hdr->misc = bswap_16(hdr->misc); 1318 hdr->size = bswap_16(hdr->size); 1319 } 1320 1321 struct thread *perf_session__findnew(struct perf_session *session, pid_t pid) 1322 { 1323 return machine__findnew_thread(&session->machines.host, -1, pid); 1324 } 1325 1326 static struct thread *perf_session__register_idle_thread(struct perf_session *session) 1327 { 1328 struct thread *thread; 1329 1330 thread = machine__findnew_thread(&session->machines.host, 0, 0); 1331 if (thread == NULL || thread__set_comm(thread, "swapper", 0)) { 1332 pr_err("problem inserting idle task.\n"); 1333 thread = NULL; 1334 } 1335 1336 return thread; 1337 } 1338 1339 static void perf_session__warn_about_errors(const struct perf_session *session) 1340 { 1341 const struct events_stats *stats = &session->evlist->stats; 1342 const struct ordered_events *oe = &session->ordered_events; 1343 1344 if (session->tool->lost == perf_event__process_lost && 1345 stats->nr_events[PERF_RECORD_LOST] != 0) { 1346 ui__warning("Processed %d events and lost %d chunks!\n\n" 1347 "Check IO/CPU overload!\n\n", 1348 stats->nr_events[0], 1349 stats->nr_events[PERF_RECORD_LOST]); 1350 } 1351 1352 if (session->tool->lost_samples == perf_event__process_lost_samples) { 1353 double drop_rate; 1354 1355 drop_rate = (double)stats->total_lost_samples / 1356 (double) (stats->nr_events[PERF_RECORD_SAMPLE] + stats->total_lost_samples); 1357 if (drop_rate > 0.05) { 1358 ui__warning("Processed %" PRIu64 " samples and lost %3.2f%% samples!\n\n", 1359 stats->nr_events[PERF_RECORD_SAMPLE] + stats->total_lost_samples, 1360 drop_rate * 100.0); 1361 } 1362 } 1363 1364 if (stats->nr_unknown_events != 0) { 1365 ui__warning("Found %u unknown events!\n\n" 1366 "Is this an older tool processing a perf.data " 1367 "file generated by a more recent tool?\n\n" 1368 "If that is not the case, consider " 1369 "reporting to linux-kernel@vger.kernel.org.\n\n", 1370 stats->nr_unknown_events); 1371 } 1372 1373 if (stats->nr_unknown_id != 0) { 1374 ui__warning("%u samples with id not present in the header\n", 1375 stats->nr_unknown_id); 1376 } 1377 1378 if (stats->nr_invalid_chains != 0) { 1379 ui__warning("Found invalid callchains!\n\n" 1380 "%u out of %u events were discarded for this reason.\n\n" 1381 "Consider reporting to linux-kernel@vger.kernel.org.\n\n", 1382 stats->nr_invalid_chains, 1383 stats->nr_events[PERF_RECORD_SAMPLE]); 1384 } 1385 1386 if (stats->nr_unprocessable_samples != 0) { 1387 ui__warning("%u unprocessable samples recorded.\n" 1388 "Do you have a KVM guest running and not using 'perf kvm'?\n", 1389 stats->nr_unprocessable_samples); 1390 } 1391 1392 if (oe->nr_unordered_events != 0) 1393 ui__warning("%u out of order events recorded.\n", oe->nr_unordered_events); 1394 1395 events_stats__auxtrace_error_warn(stats); 1396 1397 if (stats->nr_proc_map_timeout != 0) { 1398 ui__warning("%d map information files for pre-existing threads were\n" 1399 "not processed, if there are samples for addresses they\n" 1400 "will not be resolved, you may find out which are these\n" 1401 "threads by running with -v and redirecting the output\n" 1402 "to a file.\n" 1403 "The time limit to process proc map is too short?\n" 1404 "Increase it by --proc-map-timeout\n", 1405 stats->nr_proc_map_timeout); 1406 } 1407 } 1408 1409 static int perf_session__flush_thread_stack(struct thread *thread, 1410 void *p __maybe_unused) 1411 { 1412 return thread_stack__flush(thread); 1413 } 1414 1415 static int perf_session__flush_thread_stacks(struct perf_session *session) 1416 { 1417 return machines__for_each_thread(&session->machines, 1418 perf_session__flush_thread_stack, 1419 NULL); 1420 } 1421 1422 volatile int session_done; 1423 1424 static int __perf_session__process_pipe_events(struct perf_session *session) 1425 { 1426 struct ordered_events *oe = &session->ordered_events; 1427 struct perf_tool *tool = session->tool; 1428 int fd = perf_data_file__fd(session->file); 1429 union perf_event *event; 1430 uint32_t size, cur_size = 0; 1431 void *buf = NULL; 1432 s64 skip = 0; 1433 u64 head; 1434 ssize_t err; 1435 void *p; 1436 1437 perf_tool__fill_defaults(tool); 1438 1439 head = 0; 1440 cur_size = sizeof(union perf_event); 1441 1442 buf = malloc(cur_size); 1443 if (!buf) 1444 return -errno; 1445 more: 1446 event = buf; 1447 err = readn(fd, event, sizeof(struct perf_event_header)); 1448 if (err <= 0) { 1449 if (err == 0) 1450 goto done; 1451 1452 pr_err("failed to read event header\n"); 1453 goto out_err; 1454 } 1455 1456 if (session->header.needs_swap) 1457 perf_event_header__bswap(&event->header); 1458 1459 size = event->header.size; 1460 if (size < sizeof(struct perf_event_header)) { 1461 pr_err("bad event header size\n"); 1462 goto out_err; 1463 } 1464 1465 if (size > cur_size) { 1466 void *new = realloc(buf, size); 1467 if (!new) { 1468 pr_err("failed to allocate memory to read event\n"); 1469 goto out_err; 1470 } 1471 buf = new; 1472 cur_size = size; 1473 event = buf; 1474 } 1475 p = event; 1476 p += sizeof(struct perf_event_header); 1477 1478 if (size - sizeof(struct perf_event_header)) { 1479 err = readn(fd, p, size - sizeof(struct perf_event_header)); 1480 if (err <= 0) { 1481 if (err == 0) { 1482 pr_err("unexpected end of event stream\n"); 1483 goto done; 1484 } 1485 1486 pr_err("failed to read event data\n"); 1487 goto out_err; 1488 } 1489 } 1490 1491 if ((skip = perf_session__process_event(session, event, head)) < 0) { 1492 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n", 1493 head, event->header.size, event->header.type); 1494 err = -EINVAL; 1495 goto out_err; 1496 } 1497 1498 head += size; 1499 1500 if (skip > 0) 1501 head += skip; 1502 1503 if (!session_done()) 1504 goto more; 1505 done: 1506 /* do the final flush for ordered samples */ 1507 err = ordered_events__flush(oe, OE_FLUSH__FINAL); 1508 if (err) 1509 goto out_err; 1510 err = auxtrace__flush_events(session, tool); 1511 if (err) 1512 goto out_err; 1513 err = perf_session__flush_thread_stacks(session); 1514 out_err: 1515 free(buf); 1516 perf_session__warn_about_errors(session); 1517 ordered_events__free(&session->ordered_events); 1518 auxtrace__free_events(session); 1519 return err; 1520 } 1521 1522 static union perf_event * 1523 fetch_mmaped_event(struct perf_session *session, 1524 u64 head, size_t mmap_size, char *buf) 1525 { 1526 union perf_event *event; 1527 1528 /* 1529 * Ensure we have enough space remaining to read 1530 * the size of the event in the headers. 1531 */ 1532 if (head + sizeof(event->header) > mmap_size) 1533 return NULL; 1534 1535 event = (union perf_event *)(buf + head); 1536 1537 if (session->header.needs_swap) 1538 perf_event_header__bswap(&event->header); 1539 1540 if (head + event->header.size > mmap_size) { 1541 /* We're not fetching the event so swap back again */ 1542 if (session->header.needs_swap) 1543 perf_event_header__bswap(&event->header); 1544 return NULL; 1545 } 1546 1547 return event; 1548 } 1549 1550 /* 1551 * On 64bit we can mmap the data file in one go. No need for tiny mmap 1552 * slices. On 32bit we use 32MB. 1553 */ 1554 #if BITS_PER_LONG == 64 1555 #define MMAP_SIZE ULLONG_MAX 1556 #define NUM_MMAPS 1 1557 #else 1558 #define MMAP_SIZE (32 * 1024 * 1024ULL) 1559 #define NUM_MMAPS 128 1560 #endif 1561 1562 static int __perf_session__process_events(struct perf_session *session, 1563 u64 data_offset, u64 data_size, 1564 u64 file_size) 1565 { 1566 struct ordered_events *oe = &session->ordered_events; 1567 struct perf_tool *tool = session->tool; 1568 int fd = perf_data_file__fd(session->file); 1569 u64 head, page_offset, file_offset, file_pos, size; 1570 int err, mmap_prot, mmap_flags, map_idx = 0; 1571 size_t mmap_size; 1572 char *buf, *mmaps[NUM_MMAPS]; 1573 union perf_event *event; 1574 struct ui_progress prog; 1575 s64 skip; 1576 1577 perf_tool__fill_defaults(tool); 1578 1579 page_offset = page_size * (data_offset / page_size); 1580 file_offset = page_offset; 1581 head = data_offset - page_offset; 1582 1583 if (data_size == 0) 1584 goto out; 1585 1586 if (data_offset + data_size < file_size) 1587 file_size = data_offset + data_size; 1588 1589 ui_progress__init(&prog, file_size, "Processing events..."); 1590 1591 mmap_size = MMAP_SIZE; 1592 if (mmap_size > file_size) { 1593 mmap_size = file_size; 1594 session->one_mmap = true; 1595 } 1596 1597 memset(mmaps, 0, sizeof(mmaps)); 1598 1599 mmap_prot = PROT_READ; 1600 mmap_flags = MAP_SHARED; 1601 1602 if (session->header.needs_swap) { 1603 mmap_prot |= PROT_WRITE; 1604 mmap_flags = MAP_PRIVATE; 1605 } 1606 remap: 1607 buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, fd, 1608 file_offset); 1609 if (buf == MAP_FAILED) { 1610 pr_err("failed to mmap file\n"); 1611 err = -errno; 1612 goto out_err; 1613 } 1614 mmaps[map_idx] = buf; 1615 map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1); 1616 file_pos = file_offset + head; 1617 if (session->one_mmap) { 1618 session->one_mmap_addr = buf; 1619 session->one_mmap_offset = file_offset; 1620 } 1621 1622 more: 1623 event = fetch_mmaped_event(session, head, mmap_size, buf); 1624 if (!event) { 1625 if (mmaps[map_idx]) { 1626 munmap(mmaps[map_idx], mmap_size); 1627 mmaps[map_idx] = NULL; 1628 } 1629 1630 page_offset = page_size * (head / page_size); 1631 file_offset += page_offset; 1632 head -= page_offset; 1633 goto remap; 1634 } 1635 1636 size = event->header.size; 1637 1638 if (size < sizeof(struct perf_event_header) || 1639 (skip = perf_session__process_event(session, event, file_pos)) < 0) { 1640 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n", 1641 file_offset + head, event->header.size, 1642 event->header.type); 1643 err = -EINVAL; 1644 goto out_err; 1645 } 1646 1647 if (skip) 1648 size += skip; 1649 1650 head += size; 1651 file_pos += size; 1652 1653 ui_progress__update(&prog, size); 1654 1655 if (session_done()) 1656 goto out; 1657 1658 if (file_pos < file_size) 1659 goto more; 1660 1661 out: 1662 /* do the final flush for ordered samples */ 1663 err = ordered_events__flush(oe, OE_FLUSH__FINAL); 1664 if (err) 1665 goto out_err; 1666 err = auxtrace__flush_events(session, tool); 1667 if (err) 1668 goto out_err; 1669 err = perf_session__flush_thread_stacks(session); 1670 out_err: 1671 ui_progress__finish(); 1672 perf_session__warn_about_errors(session); 1673 ordered_events__free(&session->ordered_events); 1674 auxtrace__free_events(session); 1675 session->one_mmap = false; 1676 return err; 1677 } 1678 1679 int perf_session__process_events(struct perf_session *session) 1680 { 1681 u64 size = perf_data_file__size(session->file); 1682 int err; 1683 1684 if (perf_session__register_idle_thread(session) == NULL) 1685 return -ENOMEM; 1686 1687 if (!perf_data_file__is_pipe(session->file)) 1688 err = __perf_session__process_events(session, 1689 session->header.data_offset, 1690 session->header.data_size, size); 1691 else 1692 err = __perf_session__process_pipe_events(session); 1693 1694 return err; 1695 } 1696 1697 bool perf_session__has_traces(struct perf_session *session, const char *msg) 1698 { 1699 struct perf_evsel *evsel; 1700 1701 evlist__for_each(session->evlist, evsel) { 1702 if (evsel->attr.type == PERF_TYPE_TRACEPOINT) 1703 return true; 1704 } 1705 1706 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg); 1707 return false; 1708 } 1709 1710 int maps__set_kallsyms_ref_reloc_sym(struct map **maps, 1711 const char *symbol_name, u64 addr) 1712 { 1713 char *bracket; 1714 enum map_type i; 1715 struct ref_reloc_sym *ref; 1716 1717 ref = zalloc(sizeof(struct ref_reloc_sym)); 1718 if (ref == NULL) 1719 return -ENOMEM; 1720 1721 ref->name = strdup(symbol_name); 1722 if (ref->name == NULL) { 1723 free(ref); 1724 return -ENOMEM; 1725 } 1726 1727 bracket = strchr(ref->name, ']'); 1728 if (bracket) 1729 *bracket = '\0'; 1730 1731 ref->addr = addr; 1732 1733 for (i = 0; i < MAP__NR_TYPES; ++i) { 1734 struct kmap *kmap = map__kmap(maps[i]); 1735 1736 if (!kmap) 1737 continue; 1738 kmap->ref_reloc_sym = ref; 1739 } 1740 1741 return 0; 1742 } 1743 1744 size_t perf_session__fprintf_dsos(struct perf_session *session, FILE *fp) 1745 { 1746 return machines__fprintf_dsos(&session->machines, fp); 1747 } 1748 1749 size_t perf_session__fprintf_dsos_buildid(struct perf_session *session, FILE *fp, 1750 bool (skip)(struct dso *dso, int parm), int parm) 1751 { 1752 return machines__fprintf_dsos_buildid(&session->machines, fp, skip, parm); 1753 } 1754 1755 size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp) 1756 { 1757 size_t ret; 1758 const char *msg = ""; 1759 1760 if (perf_header__has_feat(&session->header, HEADER_AUXTRACE)) 1761 msg = " (excludes AUX area (e.g. instruction trace) decoded / synthesized events)"; 1762 1763 ret = fprintf(fp, "\nAggregated stats:%s\n", msg); 1764 1765 ret += events_stats__fprintf(&session->evlist->stats, fp); 1766 return ret; 1767 } 1768 1769 size_t perf_session__fprintf(struct perf_session *session, FILE *fp) 1770 { 1771 /* 1772 * FIXME: Here we have to actually print all the machines in this 1773 * session, not just the host... 1774 */ 1775 return machine__fprintf(&session->machines.host, fp); 1776 } 1777 1778 struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session, 1779 unsigned int type) 1780 { 1781 struct perf_evsel *pos; 1782 1783 evlist__for_each(session->evlist, pos) { 1784 if (pos->attr.type == type) 1785 return pos; 1786 } 1787 return NULL; 1788 } 1789 1790 void perf_evsel__print_ip(struct perf_evsel *evsel, struct perf_sample *sample, 1791 struct addr_location *al, 1792 unsigned int print_opts, unsigned int stack_depth) 1793 { 1794 struct callchain_cursor_node *node; 1795 int print_ip = print_opts & PRINT_IP_OPT_IP; 1796 int print_sym = print_opts & PRINT_IP_OPT_SYM; 1797 int print_dso = print_opts & PRINT_IP_OPT_DSO; 1798 int print_symoffset = print_opts & PRINT_IP_OPT_SYMOFFSET; 1799 int print_oneline = print_opts & PRINT_IP_OPT_ONELINE; 1800 int print_srcline = print_opts & PRINT_IP_OPT_SRCLINE; 1801 char s = print_oneline ? ' ' : '\t'; 1802 1803 if (symbol_conf.use_callchain && sample->callchain) { 1804 struct addr_location node_al; 1805 1806 if (thread__resolve_callchain(al->thread, evsel, 1807 sample, NULL, NULL, 1808 PERF_MAX_STACK_DEPTH) != 0) { 1809 if (verbose) 1810 error("Failed to resolve callchain. Skipping\n"); 1811 return; 1812 } 1813 callchain_cursor_commit(&callchain_cursor); 1814 1815 if (print_symoffset) 1816 node_al = *al; 1817 1818 while (stack_depth) { 1819 u64 addr = 0; 1820 1821 node = callchain_cursor_current(&callchain_cursor); 1822 if (!node) 1823 break; 1824 1825 if (node->sym && node->sym->ignore) 1826 goto next; 1827 1828 if (print_ip) 1829 printf("%c%16" PRIx64, s, node->ip); 1830 1831 if (node->map) 1832 addr = node->map->map_ip(node->map, node->ip); 1833 1834 if (print_sym) { 1835 printf(" "); 1836 if (print_symoffset) { 1837 node_al.addr = addr; 1838 node_al.map = node->map; 1839 symbol__fprintf_symname_offs(node->sym, &node_al, stdout); 1840 } else 1841 symbol__fprintf_symname(node->sym, stdout); 1842 } 1843 1844 if (print_dso) { 1845 printf(" ("); 1846 map__fprintf_dsoname(node->map, stdout); 1847 printf(")"); 1848 } 1849 1850 if (print_srcline) 1851 map__fprintf_srcline(node->map, addr, "\n ", 1852 stdout); 1853 1854 if (!print_oneline) 1855 printf("\n"); 1856 1857 stack_depth--; 1858 next: 1859 callchain_cursor_advance(&callchain_cursor); 1860 } 1861 1862 } else { 1863 if (al->sym && al->sym->ignore) 1864 return; 1865 1866 if (print_ip) 1867 printf("%16" PRIx64, sample->ip); 1868 1869 if (print_sym) { 1870 printf(" "); 1871 if (print_symoffset) 1872 symbol__fprintf_symname_offs(al->sym, al, 1873 stdout); 1874 else 1875 symbol__fprintf_symname(al->sym, stdout); 1876 } 1877 1878 if (print_dso) { 1879 printf(" ("); 1880 map__fprintf_dsoname(al->map, stdout); 1881 printf(")"); 1882 } 1883 1884 if (print_srcline) 1885 map__fprintf_srcline(al->map, al->addr, "\n ", stdout); 1886 } 1887 } 1888 1889 int perf_session__cpu_bitmap(struct perf_session *session, 1890 const char *cpu_list, unsigned long *cpu_bitmap) 1891 { 1892 int i, err = -1; 1893 struct cpu_map *map; 1894 1895 for (i = 0; i < PERF_TYPE_MAX; ++i) { 1896 struct perf_evsel *evsel; 1897 1898 evsel = perf_session__find_first_evtype(session, i); 1899 if (!evsel) 1900 continue; 1901 1902 if (!(evsel->attr.sample_type & PERF_SAMPLE_CPU)) { 1903 pr_err("File does not contain CPU events. " 1904 "Remove -c option to proceed.\n"); 1905 return -1; 1906 } 1907 } 1908 1909 map = cpu_map__new(cpu_list); 1910 if (map == NULL) { 1911 pr_err("Invalid cpu_list\n"); 1912 return -1; 1913 } 1914 1915 for (i = 0; i < map->nr; i++) { 1916 int cpu = map->map[i]; 1917 1918 if (cpu >= MAX_NR_CPUS) { 1919 pr_err("Requested CPU %d too large. " 1920 "Consider raising MAX_NR_CPUS\n", cpu); 1921 goto out_delete_map; 1922 } 1923 1924 set_bit(cpu, cpu_bitmap); 1925 } 1926 1927 err = 0; 1928 1929 out_delete_map: 1930 cpu_map__put(map); 1931 return err; 1932 } 1933 1934 void perf_session__fprintf_info(struct perf_session *session, FILE *fp, 1935 bool full) 1936 { 1937 struct stat st; 1938 int fd, ret; 1939 1940 if (session == NULL || fp == NULL) 1941 return; 1942 1943 fd = perf_data_file__fd(session->file); 1944 1945 ret = fstat(fd, &st); 1946 if (ret == -1) 1947 return; 1948 1949 fprintf(fp, "# ========\n"); 1950 fprintf(fp, "# captured on: %s", ctime(&st.st_ctime)); 1951 perf_header__fprintf_info(session, fp, full); 1952 fprintf(fp, "# ========\n#\n"); 1953 } 1954 1955 1956 int __perf_session__set_tracepoints_handlers(struct perf_session *session, 1957 const struct perf_evsel_str_handler *assocs, 1958 size_t nr_assocs) 1959 { 1960 struct perf_evsel *evsel; 1961 size_t i; 1962 int err; 1963 1964 for (i = 0; i < nr_assocs; i++) { 1965 /* 1966 * Adding a handler for an event not in the session, 1967 * just ignore it. 1968 */ 1969 evsel = perf_evlist__find_tracepoint_by_name(session->evlist, assocs[i].name); 1970 if (evsel == NULL) 1971 continue; 1972 1973 err = -EEXIST; 1974 if (evsel->handler != NULL) 1975 goto out; 1976 evsel->handler = assocs[i].handler; 1977 } 1978 1979 err = 0; 1980 out: 1981 return err; 1982 } 1983 1984 int perf_event__process_id_index(struct perf_tool *tool __maybe_unused, 1985 union perf_event *event, 1986 struct perf_session *session) 1987 { 1988 struct perf_evlist *evlist = session->evlist; 1989 struct id_index_event *ie = &event->id_index; 1990 size_t i, nr, max_nr; 1991 1992 max_nr = (ie->header.size - sizeof(struct id_index_event)) / 1993 sizeof(struct id_index_entry); 1994 nr = ie->nr; 1995 if (nr > max_nr) 1996 return -EINVAL; 1997 1998 if (dump_trace) 1999 fprintf(stdout, " nr: %zu\n", nr); 2000 2001 for (i = 0; i < nr; i++) { 2002 struct id_index_entry *e = &ie->entries[i]; 2003 struct perf_sample_id *sid; 2004 2005 if (dump_trace) { 2006 fprintf(stdout, " ... id: %"PRIu64, e->id); 2007 fprintf(stdout, " idx: %"PRIu64, e->idx); 2008 fprintf(stdout, " cpu: %"PRId64, e->cpu); 2009 fprintf(stdout, " tid: %"PRId64"\n", e->tid); 2010 } 2011 2012 sid = perf_evlist__id2sid(evlist, e->id); 2013 if (!sid) 2014 return -ENOENT; 2015 sid->idx = e->idx; 2016 sid->cpu = e->cpu; 2017 sid->tid = e->tid; 2018 } 2019 return 0; 2020 } 2021 2022 int perf_event__synthesize_id_index(struct perf_tool *tool, 2023 perf_event__handler_t process, 2024 struct perf_evlist *evlist, 2025 struct machine *machine) 2026 { 2027 union perf_event *ev; 2028 struct perf_evsel *evsel; 2029 size_t nr = 0, i = 0, sz, max_nr, n; 2030 int err; 2031 2032 pr_debug2("Synthesizing id index\n"); 2033 2034 max_nr = (UINT16_MAX - sizeof(struct id_index_event)) / 2035 sizeof(struct id_index_entry); 2036 2037 evlist__for_each(evlist, evsel) 2038 nr += evsel->ids; 2039 2040 n = nr > max_nr ? max_nr : nr; 2041 sz = sizeof(struct id_index_event) + n * sizeof(struct id_index_entry); 2042 ev = zalloc(sz); 2043 if (!ev) 2044 return -ENOMEM; 2045 2046 ev->id_index.header.type = PERF_RECORD_ID_INDEX; 2047 ev->id_index.header.size = sz; 2048 ev->id_index.nr = n; 2049 2050 evlist__for_each(evlist, evsel) { 2051 u32 j; 2052 2053 for (j = 0; j < evsel->ids; j++) { 2054 struct id_index_entry *e; 2055 struct perf_sample_id *sid; 2056 2057 if (i >= n) { 2058 err = process(tool, ev, NULL, machine); 2059 if (err) 2060 goto out_err; 2061 nr -= n; 2062 i = 0; 2063 } 2064 2065 e = &ev->id_index.entries[i++]; 2066 2067 e->id = evsel->id[j]; 2068 2069 sid = perf_evlist__id2sid(evlist, e->id); 2070 if (!sid) { 2071 free(ev); 2072 return -ENOENT; 2073 } 2074 2075 e->idx = sid->idx; 2076 e->cpu = sid->cpu; 2077 e->tid = sid->tid; 2078 } 2079 } 2080 2081 sz = sizeof(struct id_index_event) + nr * sizeof(struct id_index_entry); 2082 ev->id_index.header.size = sz; 2083 ev->id_index.nr = nr; 2084 2085 err = process(tool, ev, NULL, machine); 2086 out_err: 2087 free(ev); 2088 2089 return err; 2090 } 2091