1 // SPDX-License-Identifier: GPL-2.0 2 #include <errno.h> 3 #include <inttypes.h> 4 #include <linux/err.h> 5 #include <linux/kernel.h> 6 #include <linux/zalloc.h> 7 #include <api/fs/fs.h> 8 9 #include <byteswap.h> 10 #include <unistd.h> 11 #include <sys/types.h> 12 #include <sys/mman.h> 13 #include <perf/cpumap.h> 14 15 #include "map_symbol.h" 16 #include "branch.h" 17 #include "debug.h" 18 #include "evlist.h" 19 #include "evsel.h" 20 #include "memswap.h" 21 #include "map.h" 22 #include "symbol.h" 23 #include "session.h" 24 #include "tool.h" 25 #include "perf_regs.h" 26 #include "asm/bug.h" 27 #include "auxtrace.h" 28 #include "thread.h" 29 #include "thread-stack.h" 30 #include "sample-raw.h" 31 #include "stat.h" 32 #include "tsc.h" 33 #include "ui/progress.h" 34 #include "../perf.h" 35 #include "arch/common.h" 36 #include "units.h" 37 #include <internal/lib.h> 38 39 #ifdef HAVE_ZSTD_SUPPORT 40 static int perf_session__process_compressed_event(struct perf_session *session, 41 union perf_event *event, u64 file_offset) 42 { 43 void *src; 44 size_t decomp_size, src_size; 45 u64 decomp_last_rem = 0; 46 size_t mmap_len, decomp_len = session->header.env.comp_mmap_len; 47 struct decomp *decomp, *decomp_last = session->active_decomp->decomp_last; 48 49 if (decomp_last) { 50 decomp_last_rem = decomp_last->size - decomp_last->head; 51 decomp_len += decomp_last_rem; 52 } 53 54 mmap_len = sizeof(struct decomp) + decomp_len; 55 decomp = mmap(NULL, mmap_len, PROT_READ|PROT_WRITE, 56 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); 57 if (decomp == MAP_FAILED) { 58 pr_err("Couldn't allocate memory for decompression\n"); 59 return -1; 60 } 61 62 decomp->file_pos = file_offset; 63 decomp->mmap_len = mmap_len; 64 decomp->head = 0; 65 66 if (decomp_last_rem) { 67 memcpy(decomp->data, &(decomp_last->data[decomp_last->head]), decomp_last_rem); 68 decomp->size = decomp_last_rem; 69 } 70 71 src = (void *)event + sizeof(struct perf_record_compressed); 72 src_size = event->pack.header.size - sizeof(struct perf_record_compressed); 73 74 decomp_size = zstd_decompress_stream(session->active_decomp->zstd_decomp, src, src_size, 75 &(decomp->data[decomp_last_rem]), decomp_len - decomp_last_rem); 76 if (!decomp_size) { 77 munmap(decomp, mmap_len); 78 pr_err("Couldn't decompress data\n"); 79 return -1; 80 } 81 82 decomp->size += decomp_size; 83 84 if (session->active_decomp->decomp == NULL) 85 session->active_decomp->decomp = decomp; 86 else 87 session->active_decomp->decomp_last->next = decomp; 88 89 session->active_decomp->decomp_last = decomp; 90 91 pr_debug("decomp (B): %zd to %zd\n", src_size, decomp_size); 92 93 return 0; 94 } 95 #else /* !HAVE_ZSTD_SUPPORT */ 96 #define perf_session__process_compressed_event perf_session__process_compressed_event_stub 97 #endif 98 99 static int perf_session__deliver_event(struct perf_session *session, 100 union perf_event *event, 101 struct perf_tool *tool, 102 u64 file_offset); 103 104 static int perf_session__open(struct perf_session *session, int repipe_fd) 105 { 106 struct perf_data *data = session->data; 107 108 if (perf_session__read_header(session, repipe_fd) < 0) { 109 pr_err("incompatible file format (rerun with -v to learn more)\n"); 110 return -1; 111 } 112 113 if (perf_data__is_pipe(data)) 114 return 0; 115 116 if (perf_header__has_feat(&session->header, HEADER_STAT)) 117 return 0; 118 119 if (!evlist__valid_sample_type(session->evlist)) { 120 pr_err("non matching sample_type\n"); 121 return -1; 122 } 123 124 if (!evlist__valid_sample_id_all(session->evlist)) { 125 pr_err("non matching sample_id_all\n"); 126 return -1; 127 } 128 129 if (!evlist__valid_read_format(session->evlist)) { 130 pr_err("non matching read_format\n"); 131 return -1; 132 } 133 134 return 0; 135 } 136 137 void perf_session__set_id_hdr_size(struct perf_session *session) 138 { 139 u16 id_hdr_size = evlist__id_hdr_size(session->evlist); 140 141 machines__set_id_hdr_size(&session->machines, id_hdr_size); 142 } 143 144 int perf_session__create_kernel_maps(struct perf_session *session) 145 { 146 int ret = machine__create_kernel_maps(&session->machines.host); 147 148 if (ret >= 0) 149 ret = machines__create_guest_kernel_maps(&session->machines); 150 return ret; 151 } 152 153 static void perf_session__destroy_kernel_maps(struct perf_session *session) 154 { 155 machines__destroy_kernel_maps(&session->machines); 156 } 157 158 static bool perf_session__has_comm_exec(struct perf_session *session) 159 { 160 struct evsel *evsel; 161 162 evlist__for_each_entry(session->evlist, evsel) { 163 if (evsel->core.attr.comm_exec) 164 return true; 165 } 166 167 return false; 168 } 169 170 static void perf_session__set_comm_exec(struct perf_session *session) 171 { 172 bool comm_exec = perf_session__has_comm_exec(session); 173 174 machines__set_comm_exec(&session->machines, comm_exec); 175 } 176 177 static int ordered_events__deliver_event(struct ordered_events *oe, 178 struct ordered_event *event) 179 { 180 struct perf_session *session = container_of(oe, struct perf_session, 181 ordered_events); 182 183 return perf_session__deliver_event(session, event->event, 184 session->tool, event->file_offset); 185 } 186 187 struct perf_session *__perf_session__new(struct perf_data *data, 188 bool repipe, int repipe_fd, 189 struct perf_tool *tool) 190 { 191 int ret = -ENOMEM; 192 struct perf_session *session = zalloc(sizeof(*session)); 193 194 if (!session) 195 goto out; 196 197 session->repipe = repipe; 198 session->tool = tool; 199 session->decomp_data.zstd_decomp = &session->zstd_data; 200 session->active_decomp = &session->decomp_data; 201 INIT_LIST_HEAD(&session->auxtrace_index); 202 machines__init(&session->machines); 203 ordered_events__init(&session->ordered_events, 204 ordered_events__deliver_event, NULL); 205 206 perf_env__init(&session->header.env); 207 if (data) { 208 ret = perf_data__open(data); 209 if (ret < 0) 210 goto out_delete; 211 212 session->data = data; 213 214 if (perf_data__is_read(data)) { 215 ret = perf_session__open(session, repipe_fd); 216 if (ret < 0) 217 goto out_delete; 218 219 /* 220 * set session attributes that are present in perf.data 221 * but not in pipe-mode. 222 */ 223 if (!data->is_pipe) { 224 perf_session__set_id_hdr_size(session); 225 perf_session__set_comm_exec(session); 226 } 227 228 evlist__init_trace_event_sample_raw(session->evlist); 229 230 /* Open the directory data. */ 231 if (data->is_dir) { 232 ret = perf_data__open_dir(data); 233 if (ret) 234 goto out_delete; 235 } 236 237 if (!symbol_conf.kallsyms_name && 238 !symbol_conf.vmlinux_name) 239 symbol_conf.kallsyms_name = perf_data__kallsyms_name(data); 240 } 241 } else { 242 session->machines.host.env = &perf_env; 243 } 244 245 session->machines.host.single_address_space = 246 perf_env__single_address_space(session->machines.host.env); 247 248 if (!data || perf_data__is_write(data)) { 249 /* 250 * In O_RDONLY mode this will be performed when reading the 251 * kernel MMAP event, in perf_event__process_mmap(). 252 */ 253 if (perf_session__create_kernel_maps(session) < 0) 254 pr_warning("Cannot read kernel map\n"); 255 } 256 257 /* 258 * In pipe-mode, evlist is empty until PERF_RECORD_HEADER_ATTR is 259 * processed, so evlist__sample_id_all is not meaningful here. 260 */ 261 if ((!data || !data->is_pipe) && tool && tool->ordering_requires_timestamps && 262 tool->ordered_events && !evlist__sample_id_all(session->evlist)) { 263 dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n"); 264 tool->ordered_events = false; 265 } 266 267 return session; 268 269 out_delete: 270 perf_session__delete(session); 271 out: 272 return ERR_PTR(ret); 273 } 274 275 static void perf_session__delete_threads(struct perf_session *session) 276 { 277 machine__delete_threads(&session->machines.host); 278 } 279 280 static void perf_decomp__release_events(struct decomp *next) 281 { 282 struct decomp *decomp; 283 size_t mmap_len; 284 285 do { 286 decomp = next; 287 if (decomp == NULL) 288 break; 289 next = decomp->next; 290 mmap_len = decomp->mmap_len; 291 munmap(decomp, mmap_len); 292 } while (1); 293 } 294 295 void perf_session__delete(struct perf_session *session) 296 { 297 if (session == NULL) 298 return; 299 auxtrace__free(session); 300 auxtrace_index__free(&session->auxtrace_index); 301 perf_session__destroy_kernel_maps(session); 302 perf_session__delete_threads(session); 303 perf_decomp__release_events(session->decomp_data.decomp); 304 perf_env__exit(&session->header.env); 305 machines__exit(&session->machines); 306 if (session->data) { 307 if (perf_data__is_read(session->data)) 308 evlist__delete(session->evlist); 309 perf_data__close(session->data); 310 } 311 trace_event__cleanup(&session->tevent); 312 free(session); 313 } 314 315 static int process_event_synth_tracing_data_stub(struct perf_session *session 316 __maybe_unused, 317 union perf_event *event 318 __maybe_unused) 319 { 320 dump_printf(": unhandled!\n"); 321 return 0; 322 } 323 324 static int process_event_synth_attr_stub(struct perf_tool *tool __maybe_unused, 325 union perf_event *event __maybe_unused, 326 struct evlist **pevlist 327 __maybe_unused) 328 { 329 dump_printf(": unhandled!\n"); 330 return 0; 331 } 332 333 static int process_event_synth_event_update_stub(struct perf_tool *tool __maybe_unused, 334 union perf_event *event __maybe_unused, 335 struct evlist **pevlist 336 __maybe_unused) 337 { 338 if (dump_trace) 339 perf_event__fprintf_event_update(event, stdout); 340 341 dump_printf(": unhandled!\n"); 342 return 0; 343 } 344 345 static int process_event_sample_stub(struct perf_tool *tool __maybe_unused, 346 union perf_event *event __maybe_unused, 347 struct perf_sample *sample __maybe_unused, 348 struct evsel *evsel __maybe_unused, 349 struct machine *machine __maybe_unused) 350 { 351 dump_printf(": unhandled!\n"); 352 return 0; 353 } 354 355 static int process_event_stub(struct perf_tool *tool __maybe_unused, 356 union perf_event *event __maybe_unused, 357 struct perf_sample *sample __maybe_unused, 358 struct machine *machine __maybe_unused) 359 { 360 dump_printf(": unhandled!\n"); 361 return 0; 362 } 363 364 static int process_finished_round_stub(struct perf_tool *tool __maybe_unused, 365 union perf_event *event __maybe_unused, 366 struct ordered_events *oe __maybe_unused) 367 { 368 dump_printf(": unhandled!\n"); 369 return 0; 370 } 371 372 static int process_finished_round(struct perf_tool *tool, 373 union perf_event *event, 374 struct ordered_events *oe); 375 376 static int skipn(int fd, off_t n) 377 { 378 char buf[4096]; 379 ssize_t ret; 380 381 while (n > 0) { 382 ret = read(fd, buf, min(n, (off_t)sizeof(buf))); 383 if (ret <= 0) 384 return ret; 385 n -= ret; 386 } 387 388 return 0; 389 } 390 391 static s64 process_event_auxtrace_stub(struct perf_session *session __maybe_unused, 392 union perf_event *event) 393 { 394 dump_printf(": unhandled!\n"); 395 if (perf_data__is_pipe(session->data)) 396 skipn(perf_data__fd(session->data), event->auxtrace.size); 397 return event->auxtrace.size; 398 } 399 400 static int process_event_op2_stub(struct perf_session *session __maybe_unused, 401 union perf_event *event __maybe_unused) 402 { 403 dump_printf(": unhandled!\n"); 404 return 0; 405 } 406 407 408 static 409 int process_event_thread_map_stub(struct perf_session *session __maybe_unused, 410 union perf_event *event __maybe_unused) 411 { 412 if (dump_trace) 413 perf_event__fprintf_thread_map(event, stdout); 414 415 dump_printf(": unhandled!\n"); 416 return 0; 417 } 418 419 static 420 int process_event_cpu_map_stub(struct perf_session *session __maybe_unused, 421 union perf_event *event __maybe_unused) 422 { 423 if (dump_trace) 424 perf_event__fprintf_cpu_map(event, stdout); 425 426 dump_printf(": unhandled!\n"); 427 return 0; 428 } 429 430 static 431 int process_event_stat_config_stub(struct perf_session *session __maybe_unused, 432 union perf_event *event __maybe_unused) 433 { 434 if (dump_trace) 435 perf_event__fprintf_stat_config(event, stdout); 436 437 dump_printf(": unhandled!\n"); 438 return 0; 439 } 440 441 static int process_stat_stub(struct perf_session *perf_session __maybe_unused, 442 union perf_event *event) 443 { 444 if (dump_trace) 445 perf_event__fprintf_stat(event, stdout); 446 447 dump_printf(": unhandled!\n"); 448 return 0; 449 } 450 451 static int process_stat_round_stub(struct perf_session *perf_session __maybe_unused, 452 union perf_event *event) 453 { 454 if (dump_trace) 455 perf_event__fprintf_stat_round(event, stdout); 456 457 dump_printf(": unhandled!\n"); 458 return 0; 459 } 460 461 static int process_event_time_conv_stub(struct perf_session *perf_session __maybe_unused, 462 union perf_event *event) 463 { 464 if (dump_trace) 465 perf_event__fprintf_time_conv(event, stdout); 466 467 dump_printf(": unhandled!\n"); 468 return 0; 469 } 470 471 static int perf_session__process_compressed_event_stub(struct perf_session *session __maybe_unused, 472 union perf_event *event __maybe_unused, 473 u64 file_offset __maybe_unused) 474 { 475 dump_printf(": unhandled!\n"); 476 return 0; 477 } 478 479 void perf_tool__fill_defaults(struct perf_tool *tool) 480 { 481 if (tool->sample == NULL) 482 tool->sample = process_event_sample_stub; 483 if (tool->mmap == NULL) 484 tool->mmap = process_event_stub; 485 if (tool->mmap2 == NULL) 486 tool->mmap2 = process_event_stub; 487 if (tool->comm == NULL) 488 tool->comm = process_event_stub; 489 if (tool->namespaces == NULL) 490 tool->namespaces = process_event_stub; 491 if (tool->cgroup == NULL) 492 tool->cgroup = process_event_stub; 493 if (tool->fork == NULL) 494 tool->fork = process_event_stub; 495 if (tool->exit == NULL) 496 tool->exit = process_event_stub; 497 if (tool->lost == NULL) 498 tool->lost = perf_event__process_lost; 499 if (tool->lost_samples == NULL) 500 tool->lost_samples = perf_event__process_lost_samples; 501 if (tool->aux == NULL) 502 tool->aux = perf_event__process_aux; 503 if (tool->itrace_start == NULL) 504 tool->itrace_start = perf_event__process_itrace_start; 505 if (tool->context_switch == NULL) 506 tool->context_switch = perf_event__process_switch; 507 if (tool->ksymbol == NULL) 508 tool->ksymbol = perf_event__process_ksymbol; 509 if (tool->bpf == NULL) 510 tool->bpf = perf_event__process_bpf; 511 if (tool->text_poke == NULL) 512 tool->text_poke = perf_event__process_text_poke; 513 if (tool->aux_output_hw_id == NULL) 514 tool->aux_output_hw_id = perf_event__process_aux_output_hw_id; 515 if (tool->read == NULL) 516 tool->read = process_event_sample_stub; 517 if (tool->throttle == NULL) 518 tool->throttle = process_event_stub; 519 if (tool->unthrottle == NULL) 520 tool->unthrottle = process_event_stub; 521 if (tool->attr == NULL) 522 tool->attr = process_event_synth_attr_stub; 523 if (tool->event_update == NULL) 524 tool->event_update = process_event_synth_event_update_stub; 525 if (tool->tracing_data == NULL) 526 tool->tracing_data = process_event_synth_tracing_data_stub; 527 if (tool->build_id == NULL) 528 tool->build_id = process_event_op2_stub; 529 if (tool->finished_round == NULL) { 530 if (tool->ordered_events) 531 tool->finished_round = process_finished_round; 532 else 533 tool->finished_round = process_finished_round_stub; 534 } 535 if (tool->id_index == NULL) 536 tool->id_index = process_event_op2_stub; 537 if (tool->auxtrace_info == NULL) 538 tool->auxtrace_info = process_event_op2_stub; 539 if (tool->auxtrace == NULL) 540 tool->auxtrace = process_event_auxtrace_stub; 541 if (tool->auxtrace_error == NULL) 542 tool->auxtrace_error = process_event_op2_stub; 543 if (tool->thread_map == NULL) 544 tool->thread_map = process_event_thread_map_stub; 545 if (tool->cpu_map == NULL) 546 tool->cpu_map = process_event_cpu_map_stub; 547 if (tool->stat_config == NULL) 548 tool->stat_config = process_event_stat_config_stub; 549 if (tool->stat == NULL) 550 tool->stat = process_stat_stub; 551 if (tool->stat_round == NULL) 552 tool->stat_round = process_stat_round_stub; 553 if (tool->time_conv == NULL) 554 tool->time_conv = process_event_time_conv_stub; 555 if (tool->feature == NULL) 556 tool->feature = process_event_op2_stub; 557 if (tool->compressed == NULL) 558 tool->compressed = perf_session__process_compressed_event; 559 } 560 561 static void swap_sample_id_all(union perf_event *event, void *data) 562 { 563 void *end = (void *) event + event->header.size; 564 int size = end - data; 565 566 BUG_ON(size % sizeof(u64)); 567 mem_bswap_64(data, size); 568 } 569 570 static void perf_event__all64_swap(union perf_event *event, 571 bool sample_id_all __maybe_unused) 572 { 573 struct perf_event_header *hdr = &event->header; 574 mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr)); 575 } 576 577 static void perf_event__comm_swap(union perf_event *event, bool sample_id_all) 578 { 579 event->comm.pid = bswap_32(event->comm.pid); 580 event->comm.tid = bswap_32(event->comm.tid); 581 582 if (sample_id_all) { 583 void *data = &event->comm.comm; 584 585 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64)); 586 swap_sample_id_all(event, data); 587 } 588 } 589 590 static void perf_event__mmap_swap(union perf_event *event, 591 bool sample_id_all) 592 { 593 event->mmap.pid = bswap_32(event->mmap.pid); 594 event->mmap.tid = bswap_32(event->mmap.tid); 595 event->mmap.start = bswap_64(event->mmap.start); 596 event->mmap.len = bswap_64(event->mmap.len); 597 event->mmap.pgoff = bswap_64(event->mmap.pgoff); 598 599 if (sample_id_all) { 600 void *data = &event->mmap.filename; 601 602 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64)); 603 swap_sample_id_all(event, data); 604 } 605 } 606 607 static void perf_event__mmap2_swap(union perf_event *event, 608 bool sample_id_all) 609 { 610 event->mmap2.pid = bswap_32(event->mmap2.pid); 611 event->mmap2.tid = bswap_32(event->mmap2.tid); 612 event->mmap2.start = bswap_64(event->mmap2.start); 613 event->mmap2.len = bswap_64(event->mmap2.len); 614 event->mmap2.pgoff = bswap_64(event->mmap2.pgoff); 615 616 if (!(event->header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID)) { 617 event->mmap2.maj = bswap_32(event->mmap2.maj); 618 event->mmap2.min = bswap_32(event->mmap2.min); 619 event->mmap2.ino = bswap_64(event->mmap2.ino); 620 event->mmap2.ino_generation = bswap_64(event->mmap2.ino_generation); 621 } 622 623 if (sample_id_all) { 624 void *data = &event->mmap2.filename; 625 626 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64)); 627 swap_sample_id_all(event, data); 628 } 629 } 630 static void perf_event__task_swap(union perf_event *event, bool sample_id_all) 631 { 632 event->fork.pid = bswap_32(event->fork.pid); 633 event->fork.tid = bswap_32(event->fork.tid); 634 event->fork.ppid = bswap_32(event->fork.ppid); 635 event->fork.ptid = bswap_32(event->fork.ptid); 636 event->fork.time = bswap_64(event->fork.time); 637 638 if (sample_id_all) 639 swap_sample_id_all(event, &event->fork + 1); 640 } 641 642 static void perf_event__read_swap(union perf_event *event, bool sample_id_all) 643 { 644 event->read.pid = bswap_32(event->read.pid); 645 event->read.tid = bswap_32(event->read.tid); 646 event->read.value = bswap_64(event->read.value); 647 event->read.time_enabled = bswap_64(event->read.time_enabled); 648 event->read.time_running = bswap_64(event->read.time_running); 649 event->read.id = bswap_64(event->read.id); 650 651 if (sample_id_all) 652 swap_sample_id_all(event, &event->read + 1); 653 } 654 655 static void perf_event__aux_swap(union perf_event *event, bool sample_id_all) 656 { 657 event->aux.aux_offset = bswap_64(event->aux.aux_offset); 658 event->aux.aux_size = bswap_64(event->aux.aux_size); 659 event->aux.flags = bswap_64(event->aux.flags); 660 661 if (sample_id_all) 662 swap_sample_id_all(event, &event->aux + 1); 663 } 664 665 static void perf_event__itrace_start_swap(union perf_event *event, 666 bool sample_id_all) 667 { 668 event->itrace_start.pid = bswap_32(event->itrace_start.pid); 669 event->itrace_start.tid = bswap_32(event->itrace_start.tid); 670 671 if (sample_id_all) 672 swap_sample_id_all(event, &event->itrace_start + 1); 673 } 674 675 static void perf_event__switch_swap(union perf_event *event, bool sample_id_all) 676 { 677 if (event->header.type == PERF_RECORD_SWITCH_CPU_WIDE) { 678 event->context_switch.next_prev_pid = 679 bswap_32(event->context_switch.next_prev_pid); 680 event->context_switch.next_prev_tid = 681 bswap_32(event->context_switch.next_prev_tid); 682 } 683 684 if (sample_id_all) 685 swap_sample_id_all(event, &event->context_switch + 1); 686 } 687 688 static void perf_event__text_poke_swap(union perf_event *event, bool sample_id_all) 689 { 690 event->text_poke.addr = bswap_64(event->text_poke.addr); 691 event->text_poke.old_len = bswap_16(event->text_poke.old_len); 692 event->text_poke.new_len = bswap_16(event->text_poke.new_len); 693 694 if (sample_id_all) { 695 size_t len = sizeof(event->text_poke.old_len) + 696 sizeof(event->text_poke.new_len) + 697 event->text_poke.old_len + 698 event->text_poke.new_len; 699 void *data = &event->text_poke.old_len; 700 701 data += PERF_ALIGN(len, sizeof(u64)); 702 swap_sample_id_all(event, data); 703 } 704 } 705 706 static void perf_event__throttle_swap(union perf_event *event, 707 bool sample_id_all) 708 { 709 event->throttle.time = bswap_64(event->throttle.time); 710 event->throttle.id = bswap_64(event->throttle.id); 711 event->throttle.stream_id = bswap_64(event->throttle.stream_id); 712 713 if (sample_id_all) 714 swap_sample_id_all(event, &event->throttle + 1); 715 } 716 717 static void perf_event__namespaces_swap(union perf_event *event, 718 bool sample_id_all) 719 { 720 u64 i; 721 722 event->namespaces.pid = bswap_32(event->namespaces.pid); 723 event->namespaces.tid = bswap_32(event->namespaces.tid); 724 event->namespaces.nr_namespaces = bswap_64(event->namespaces.nr_namespaces); 725 726 for (i = 0; i < event->namespaces.nr_namespaces; i++) { 727 struct perf_ns_link_info *ns = &event->namespaces.link_info[i]; 728 729 ns->dev = bswap_64(ns->dev); 730 ns->ino = bswap_64(ns->ino); 731 } 732 733 if (sample_id_all) 734 swap_sample_id_all(event, &event->namespaces.link_info[i]); 735 } 736 737 static void perf_event__cgroup_swap(union perf_event *event, bool sample_id_all) 738 { 739 event->cgroup.id = bswap_64(event->cgroup.id); 740 741 if (sample_id_all) { 742 void *data = &event->cgroup.path; 743 744 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64)); 745 swap_sample_id_all(event, data); 746 } 747 } 748 749 static u8 revbyte(u8 b) 750 { 751 int rev = (b >> 4) | ((b & 0xf) << 4); 752 rev = ((rev & 0xcc) >> 2) | ((rev & 0x33) << 2); 753 rev = ((rev & 0xaa) >> 1) | ((rev & 0x55) << 1); 754 return (u8) rev; 755 } 756 757 /* 758 * XXX this is hack in attempt to carry flags bitfield 759 * through endian village. ABI says: 760 * 761 * Bit-fields are allocated from right to left (least to most significant) 762 * on little-endian implementations and from left to right (most to least 763 * significant) on big-endian implementations. 764 * 765 * The above seems to be byte specific, so we need to reverse each 766 * byte of the bitfield. 'Internet' also says this might be implementation 767 * specific and we probably need proper fix and carry perf_event_attr 768 * bitfield flags in separate data file FEAT_ section. Thought this seems 769 * to work for now. 770 */ 771 static void swap_bitfield(u8 *p, unsigned len) 772 { 773 unsigned i; 774 775 for (i = 0; i < len; i++) { 776 *p = revbyte(*p); 777 p++; 778 } 779 } 780 781 /* exported for swapping attributes in file header */ 782 void perf_event__attr_swap(struct perf_event_attr *attr) 783 { 784 attr->type = bswap_32(attr->type); 785 attr->size = bswap_32(attr->size); 786 787 #define bswap_safe(f, n) \ 788 (attr->size > (offsetof(struct perf_event_attr, f) + \ 789 sizeof(attr->f) * (n))) 790 #define bswap_field(f, sz) \ 791 do { \ 792 if (bswap_safe(f, 0)) \ 793 attr->f = bswap_##sz(attr->f); \ 794 } while(0) 795 #define bswap_field_16(f) bswap_field(f, 16) 796 #define bswap_field_32(f) bswap_field(f, 32) 797 #define bswap_field_64(f) bswap_field(f, 64) 798 799 bswap_field_64(config); 800 bswap_field_64(sample_period); 801 bswap_field_64(sample_type); 802 bswap_field_64(read_format); 803 bswap_field_32(wakeup_events); 804 bswap_field_32(bp_type); 805 bswap_field_64(bp_addr); 806 bswap_field_64(bp_len); 807 bswap_field_64(branch_sample_type); 808 bswap_field_64(sample_regs_user); 809 bswap_field_32(sample_stack_user); 810 bswap_field_32(aux_watermark); 811 bswap_field_16(sample_max_stack); 812 bswap_field_32(aux_sample_size); 813 814 /* 815 * After read_format are bitfields. Check read_format because 816 * we are unable to use offsetof on bitfield. 817 */ 818 if (bswap_safe(read_format, 1)) 819 swap_bitfield((u8 *) (&attr->read_format + 1), 820 sizeof(u64)); 821 #undef bswap_field_64 822 #undef bswap_field_32 823 #undef bswap_field 824 #undef bswap_safe 825 } 826 827 static void perf_event__hdr_attr_swap(union perf_event *event, 828 bool sample_id_all __maybe_unused) 829 { 830 size_t size; 831 832 perf_event__attr_swap(&event->attr.attr); 833 834 size = event->header.size; 835 size -= (void *)&event->attr.id - (void *)event; 836 mem_bswap_64(event->attr.id, size); 837 } 838 839 static void perf_event__event_update_swap(union perf_event *event, 840 bool sample_id_all __maybe_unused) 841 { 842 event->event_update.type = bswap_64(event->event_update.type); 843 event->event_update.id = bswap_64(event->event_update.id); 844 } 845 846 static void perf_event__event_type_swap(union perf_event *event, 847 bool sample_id_all __maybe_unused) 848 { 849 event->event_type.event_type.event_id = 850 bswap_64(event->event_type.event_type.event_id); 851 } 852 853 static void perf_event__tracing_data_swap(union perf_event *event, 854 bool sample_id_all __maybe_unused) 855 { 856 event->tracing_data.size = bswap_32(event->tracing_data.size); 857 } 858 859 static void perf_event__auxtrace_info_swap(union perf_event *event, 860 bool sample_id_all __maybe_unused) 861 { 862 size_t size; 863 864 event->auxtrace_info.type = bswap_32(event->auxtrace_info.type); 865 866 size = event->header.size; 867 size -= (void *)&event->auxtrace_info.priv - (void *)event; 868 mem_bswap_64(event->auxtrace_info.priv, size); 869 } 870 871 static void perf_event__auxtrace_swap(union perf_event *event, 872 bool sample_id_all __maybe_unused) 873 { 874 event->auxtrace.size = bswap_64(event->auxtrace.size); 875 event->auxtrace.offset = bswap_64(event->auxtrace.offset); 876 event->auxtrace.reference = bswap_64(event->auxtrace.reference); 877 event->auxtrace.idx = bswap_32(event->auxtrace.idx); 878 event->auxtrace.tid = bswap_32(event->auxtrace.tid); 879 event->auxtrace.cpu = bswap_32(event->auxtrace.cpu); 880 } 881 882 static void perf_event__auxtrace_error_swap(union perf_event *event, 883 bool sample_id_all __maybe_unused) 884 { 885 event->auxtrace_error.type = bswap_32(event->auxtrace_error.type); 886 event->auxtrace_error.code = bswap_32(event->auxtrace_error.code); 887 event->auxtrace_error.cpu = bswap_32(event->auxtrace_error.cpu); 888 event->auxtrace_error.pid = bswap_32(event->auxtrace_error.pid); 889 event->auxtrace_error.tid = bswap_32(event->auxtrace_error.tid); 890 event->auxtrace_error.fmt = bswap_32(event->auxtrace_error.fmt); 891 event->auxtrace_error.ip = bswap_64(event->auxtrace_error.ip); 892 if (event->auxtrace_error.fmt) 893 event->auxtrace_error.time = bswap_64(event->auxtrace_error.time); 894 } 895 896 static void perf_event__thread_map_swap(union perf_event *event, 897 bool sample_id_all __maybe_unused) 898 { 899 unsigned i; 900 901 event->thread_map.nr = bswap_64(event->thread_map.nr); 902 903 for (i = 0; i < event->thread_map.nr; i++) 904 event->thread_map.entries[i].pid = bswap_64(event->thread_map.entries[i].pid); 905 } 906 907 static void perf_event__cpu_map_swap(union perf_event *event, 908 bool sample_id_all __maybe_unused) 909 { 910 struct perf_record_cpu_map_data *data = &event->cpu_map.data; 911 struct cpu_map_entries *cpus; 912 struct perf_record_record_cpu_map *mask; 913 unsigned i; 914 915 data->type = bswap_16(data->type); 916 917 switch (data->type) { 918 case PERF_CPU_MAP__CPUS: 919 cpus = (struct cpu_map_entries *)data->data; 920 921 cpus->nr = bswap_16(cpus->nr); 922 923 for (i = 0; i < cpus->nr; i++) 924 cpus->cpu[i] = bswap_16(cpus->cpu[i]); 925 break; 926 case PERF_CPU_MAP__MASK: 927 mask = (struct perf_record_record_cpu_map *)data->data; 928 929 mask->nr = bswap_16(mask->nr); 930 mask->long_size = bswap_16(mask->long_size); 931 932 switch (mask->long_size) { 933 case 4: mem_bswap_32(&mask->mask, mask->nr); break; 934 case 8: mem_bswap_64(&mask->mask, mask->nr); break; 935 default: 936 pr_err("cpu_map swap: unsupported long size\n"); 937 } 938 default: 939 break; 940 } 941 } 942 943 static void perf_event__stat_config_swap(union perf_event *event, 944 bool sample_id_all __maybe_unused) 945 { 946 u64 size; 947 948 size = bswap_64(event->stat_config.nr) * sizeof(event->stat_config.data[0]); 949 size += 1; /* nr item itself */ 950 mem_bswap_64(&event->stat_config.nr, size); 951 } 952 953 static void perf_event__stat_swap(union perf_event *event, 954 bool sample_id_all __maybe_unused) 955 { 956 event->stat.id = bswap_64(event->stat.id); 957 event->stat.thread = bswap_32(event->stat.thread); 958 event->stat.cpu = bswap_32(event->stat.cpu); 959 event->stat.val = bswap_64(event->stat.val); 960 event->stat.ena = bswap_64(event->stat.ena); 961 event->stat.run = bswap_64(event->stat.run); 962 } 963 964 static void perf_event__stat_round_swap(union perf_event *event, 965 bool sample_id_all __maybe_unused) 966 { 967 event->stat_round.type = bswap_64(event->stat_round.type); 968 event->stat_round.time = bswap_64(event->stat_round.time); 969 } 970 971 static void perf_event__time_conv_swap(union perf_event *event, 972 bool sample_id_all __maybe_unused) 973 { 974 event->time_conv.time_shift = bswap_64(event->time_conv.time_shift); 975 event->time_conv.time_mult = bswap_64(event->time_conv.time_mult); 976 event->time_conv.time_zero = bswap_64(event->time_conv.time_zero); 977 978 if (event_contains(event->time_conv, time_cycles)) { 979 event->time_conv.time_cycles = bswap_64(event->time_conv.time_cycles); 980 event->time_conv.time_mask = bswap_64(event->time_conv.time_mask); 981 } 982 } 983 984 typedef void (*perf_event__swap_op)(union perf_event *event, 985 bool sample_id_all); 986 987 static perf_event__swap_op perf_event__swap_ops[] = { 988 [PERF_RECORD_MMAP] = perf_event__mmap_swap, 989 [PERF_RECORD_MMAP2] = perf_event__mmap2_swap, 990 [PERF_RECORD_COMM] = perf_event__comm_swap, 991 [PERF_RECORD_FORK] = perf_event__task_swap, 992 [PERF_RECORD_EXIT] = perf_event__task_swap, 993 [PERF_RECORD_LOST] = perf_event__all64_swap, 994 [PERF_RECORD_READ] = perf_event__read_swap, 995 [PERF_RECORD_THROTTLE] = perf_event__throttle_swap, 996 [PERF_RECORD_UNTHROTTLE] = perf_event__throttle_swap, 997 [PERF_RECORD_SAMPLE] = perf_event__all64_swap, 998 [PERF_RECORD_AUX] = perf_event__aux_swap, 999 [PERF_RECORD_ITRACE_START] = perf_event__itrace_start_swap, 1000 [PERF_RECORD_LOST_SAMPLES] = perf_event__all64_swap, 1001 [PERF_RECORD_SWITCH] = perf_event__switch_swap, 1002 [PERF_RECORD_SWITCH_CPU_WIDE] = perf_event__switch_swap, 1003 [PERF_RECORD_NAMESPACES] = perf_event__namespaces_swap, 1004 [PERF_RECORD_CGROUP] = perf_event__cgroup_swap, 1005 [PERF_RECORD_TEXT_POKE] = perf_event__text_poke_swap, 1006 [PERF_RECORD_AUX_OUTPUT_HW_ID] = perf_event__all64_swap, 1007 [PERF_RECORD_HEADER_ATTR] = perf_event__hdr_attr_swap, 1008 [PERF_RECORD_HEADER_EVENT_TYPE] = perf_event__event_type_swap, 1009 [PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap, 1010 [PERF_RECORD_HEADER_BUILD_ID] = NULL, 1011 [PERF_RECORD_ID_INDEX] = perf_event__all64_swap, 1012 [PERF_RECORD_AUXTRACE_INFO] = perf_event__auxtrace_info_swap, 1013 [PERF_RECORD_AUXTRACE] = perf_event__auxtrace_swap, 1014 [PERF_RECORD_AUXTRACE_ERROR] = perf_event__auxtrace_error_swap, 1015 [PERF_RECORD_THREAD_MAP] = perf_event__thread_map_swap, 1016 [PERF_RECORD_CPU_MAP] = perf_event__cpu_map_swap, 1017 [PERF_RECORD_STAT_CONFIG] = perf_event__stat_config_swap, 1018 [PERF_RECORD_STAT] = perf_event__stat_swap, 1019 [PERF_RECORD_STAT_ROUND] = perf_event__stat_round_swap, 1020 [PERF_RECORD_EVENT_UPDATE] = perf_event__event_update_swap, 1021 [PERF_RECORD_TIME_CONV] = perf_event__time_conv_swap, 1022 [PERF_RECORD_HEADER_MAX] = NULL, 1023 }; 1024 1025 /* 1026 * When perf record finishes a pass on every buffers, it records this pseudo 1027 * event. 1028 * We record the max timestamp t found in the pass n. 1029 * Assuming these timestamps are monotonic across cpus, we know that if 1030 * a buffer still has events with timestamps below t, they will be all 1031 * available and then read in the pass n + 1. 1032 * Hence when we start to read the pass n + 2, we can safely flush every 1033 * events with timestamps below t. 1034 * 1035 * ============ PASS n ================= 1036 * CPU 0 | CPU 1 1037 * | 1038 * cnt1 timestamps | cnt2 timestamps 1039 * 1 | 2 1040 * 2 | 3 1041 * - | 4 <--- max recorded 1042 * 1043 * ============ PASS n + 1 ============== 1044 * CPU 0 | CPU 1 1045 * | 1046 * cnt1 timestamps | cnt2 timestamps 1047 * 3 | 5 1048 * 4 | 6 1049 * 5 | 7 <---- max recorded 1050 * 1051 * Flush every events below timestamp 4 1052 * 1053 * ============ PASS n + 2 ============== 1054 * CPU 0 | CPU 1 1055 * | 1056 * cnt1 timestamps | cnt2 timestamps 1057 * 6 | 8 1058 * 7 | 9 1059 * - | 10 1060 * 1061 * Flush every events below timestamp 7 1062 * etc... 1063 */ 1064 static int process_finished_round(struct perf_tool *tool __maybe_unused, 1065 union perf_event *event __maybe_unused, 1066 struct ordered_events *oe) 1067 { 1068 if (dump_trace) 1069 fprintf(stdout, "\n"); 1070 return ordered_events__flush(oe, OE_FLUSH__ROUND); 1071 } 1072 1073 int perf_session__queue_event(struct perf_session *s, union perf_event *event, 1074 u64 timestamp, u64 file_offset) 1075 { 1076 return ordered_events__queue(&s->ordered_events, event, timestamp, file_offset); 1077 } 1078 1079 static void callchain__lbr_callstack_printf(struct perf_sample *sample) 1080 { 1081 struct ip_callchain *callchain = sample->callchain; 1082 struct branch_stack *lbr_stack = sample->branch_stack; 1083 struct branch_entry *entries = perf_sample__branch_entries(sample); 1084 u64 kernel_callchain_nr = callchain->nr; 1085 unsigned int i; 1086 1087 for (i = 0; i < kernel_callchain_nr; i++) { 1088 if (callchain->ips[i] == PERF_CONTEXT_USER) 1089 break; 1090 } 1091 1092 if ((i != kernel_callchain_nr) && lbr_stack->nr) { 1093 u64 total_nr; 1094 /* 1095 * LBR callstack can only get user call chain, 1096 * i is kernel call chain number, 1097 * 1 is PERF_CONTEXT_USER. 1098 * 1099 * The user call chain is stored in LBR registers. 1100 * LBR are pair registers. The caller is stored 1101 * in "from" register, while the callee is stored 1102 * in "to" register. 1103 * For example, there is a call stack 1104 * "A"->"B"->"C"->"D". 1105 * The LBR registers will be recorded like 1106 * "C"->"D", "B"->"C", "A"->"B". 1107 * So only the first "to" register and all "from" 1108 * registers are needed to construct the whole stack. 1109 */ 1110 total_nr = i + 1 + lbr_stack->nr + 1; 1111 kernel_callchain_nr = i + 1; 1112 1113 printf("... LBR call chain: nr:%" PRIu64 "\n", total_nr); 1114 1115 for (i = 0; i < kernel_callchain_nr; i++) 1116 printf("..... %2d: %016" PRIx64 "\n", 1117 i, callchain->ips[i]); 1118 1119 printf("..... %2d: %016" PRIx64 "\n", 1120 (int)(kernel_callchain_nr), entries[0].to); 1121 for (i = 0; i < lbr_stack->nr; i++) 1122 printf("..... %2d: %016" PRIx64 "\n", 1123 (int)(i + kernel_callchain_nr + 1), entries[i].from); 1124 } 1125 } 1126 1127 static void callchain__printf(struct evsel *evsel, 1128 struct perf_sample *sample) 1129 { 1130 unsigned int i; 1131 struct ip_callchain *callchain = sample->callchain; 1132 1133 if (evsel__has_branch_callstack(evsel)) 1134 callchain__lbr_callstack_printf(sample); 1135 1136 printf("... FP chain: nr:%" PRIu64 "\n", callchain->nr); 1137 1138 for (i = 0; i < callchain->nr; i++) 1139 printf("..... %2d: %016" PRIx64 "\n", 1140 i, callchain->ips[i]); 1141 } 1142 1143 static void branch_stack__printf(struct perf_sample *sample, bool callstack) 1144 { 1145 struct branch_entry *entries = perf_sample__branch_entries(sample); 1146 uint64_t i; 1147 1148 printf("%s: nr:%" PRIu64 "\n", 1149 !callstack ? "... branch stack" : "... branch callstack", 1150 sample->branch_stack->nr); 1151 1152 for (i = 0; i < sample->branch_stack->nr; i++) { 1153 struct branch_entry *e = &entries[i]; 1154 1155 if (!callstack) { 1156 printf("..... %2"PRIu64": %016" PRIx64 " -> %016" PRIx64 " %hu cycles %s%s%s%s %x\n", 1157 i, e->from, e->to, 1158 (unsigned short)e->flags.cycles, 1159 e->flags.mispred ? "M" : " ", 1160 e->flags.predicted ? "P" : " ", 1161 e->flags.abort ? "A" : " ", 1162 e->flags.in_tx ? "T" : " ", 1163 (unsigned)e->flags.reserved); 1164 } else { 1165 printf("..... %2"PRIu64": %016" PRIx64 "\n", 1166 i, i > 0 ? e->from : e->to); 1167 } 1168 } 1169 } 1170 1171 static void regs_dump__printf(u64 mask, u64 *regs) 1172 { 1173 unsigned rid, i = 0; 1174 1175 for_each_set_bit(rid, (unsigned long *) &mask, sizeof(mask) * 8) { 1176 u64 val = regs[i++]; 1177 1178 printf(".... %-5s 0x%016" PRIx64 "\n", 1179 perf_reg_name(rid), val); 1180 } 1181 } 1182 1183 static const char *regs_abi[] = { 1184 [PERF_SAMPLE_REGS_ABI_NONE] = "none", 1185 [PERF_SAMPLE_REGS_ABI_32] = "32-bit", 1186 [PERF_SAMPLE_REGS_ABI_64] = "64-bit", 1187 }; 1188 1189 static inline const char *regs_dump_abi(struct regs_dump *d) 1190 { 1191 if (d->abi > PERF_SAMPLE_REGS_ABI_64) 1192 return "unknown"; 1193 1194 return regs_abi[d->abi]; 1195 } 1196 1197 static void regs__printf(const char *type, struct regs_dump *regs) 1198 { 1199 u64 mask = regs->mask; 1200 1201 printf("... %s regs: mask 0x%" PRIx64 " ABI %s\n", 1202 type, 1203 mask, 1204 regs_dump_abi(regs)); 1205 1206 regs_dump__printf(mask, regs->regs); 1207 } 1208 1209 static void regs_user__printf(struct perf_sample *sample) 1210 { 1211 struct regs_dump *user_regs = &sample->user_regs; 1212 1213 if (user_regs->regs) 1214 regs__printf("user", user_regs); 1215 } 1216 1217 static void regs_intr__printf(struct perf_sample *sample) 1218 { 1219 struct regs_dump *intr_regs = &sample->intr_regs; 1220 1221 if (intr_regs->regs) 1222 regs__printf("intr", intr_regs); 1223 } 1224 1225 static void stack_user__printf(struct stack_dump *dump) 1226 { 1227 printf("... ustack: size %" PRIu64 ", offset 0x%x\n", 1228 dump->size, dump->offset); 1229 } 1230 1231 static void evlist__print_tstamp(struct evlist *evlist, union perf_event *event, struct perf_sample *sample) 1232 { 1233 u64 sample_type = __evlist__combined_sample_type(evlist); 1234 1235 if (event->header.type != PERF_RECORD_SAMPLE && 1236 !evlist__sample_id_all(evlist)) { 1237 fputs("-1 -1 ", stdout); 1238 return; 1239 } 1240 1241 if ((sample_type & PERF_SAMPLE_CPU)) 1242 printf("%u ", sample->cpu); 1243 1244 if (sample_type & PERF_SAMPLE_TIME) 1245 printf("%" PRIu64 " ", sample->time); 1246 } 1247 1248 static void sample_read__printf(struct perf_sample *sample, u64 read_format) 1249 { 1250 printf("... sample_read:\n"); 1251 1252 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 1253 printf("...... time enabled %016" PRIx64 "\n", 1254 sample->read.time_enabled); 1255 1256 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 1257 printf("...... time running %016" PRIx64 "\n", 1258 sample->read.time_running); 1259 1260 if (read_format & PERF_FORMAT_GROUP) { 1261 u64 i; 1262 1263 printf(".... group nr %" PRIu64 "\n", sample->read.group.nr); 1264 1265 for (i = 0; i < sample->read.group.nr; i++) { 1266 struct sample_read_value *value; 1267 1268 value = &sample->read.group.values[i]; 1269 printf("..... id %016" PRIx64 1270 ", value %016" PRIx64 "\n", 1271 value->id, value->value); 1272 } 1273 } else 1274 printf("..... id %016" PRIx64 ", value %016" PRIx64 "\n", 1275 sample->read.one.id, sample->read.one.value); 1276 } 1277 1278 static void dump_event(struct evlist *evlist, union perf_event *event, 1279 u64 file_offset, struct perf_sample *sample) 1280 { 1281 if (!dump_trace) 1282 return; 1283 1284 printf("\n%#" PRIx64 " [%#x]: event: %d\n", 1285 file_offset, event->header.size, event->header.type); 1286 1287 trace_event(event); 1288 if (event->header.type == PERF_RECORD_SAMPLE && evlist->trace_event_sample_raw) 1289 evlist->trace_event_sample_raw(evlist, event, sample); 1290 1291 if (sample) 1292 evlist__print_tstamp(evlist, event, sample); 1293 1294 printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset, 1295 event->header.size, perf_event__name(event->header.type)); 1296 } 1297 1298 char *get_page_size_name(u64 size, char *str) 1299 { 1300 if (!size || !unit_number__scnprintf(str, PAGE_SIZE_NAME_LEN, size)) 1301 snprintf(str, PAGE_SIZE_NAME_LEN, "%s", "N/A"); 1302 1303 return str; 1304 } 1305 1306 static void dump_sample(struct evsel *evsel, union perf_event *event, 1307 struct perf_sample *sample) 1308 { 1309 u64 sample_type; 1310 char str[PAGE_SIZE_NAME_LEN]; 1311 1312 if (!dump_trace) 1313 return; 1314 1315 printf("(IP, 0x%x): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n", 1316 event->header.misc, sample->pid, sample->tid, sample->ip, 1317 sample->period, sample->addr); 1318 1319 sample_type = evsel->core.attr.sample_type; 1320 1321 if (evsel__has_callchain(evsel)) 1322 callchain__printf(evsel, sample); 1323 1324 if (evsel__has_br_stack(evsel)) 1325 branch_stack__printf(sample, evsel__has_branch_callstack(evsel)); 1326 1327 if (sample_type & PERF_SAMPLE_REGS_USER) 1328 regs_user__printf(sample); 1329 1330 if (sample_type & PERF_SAMPLE_REGS_INTR) 1331 regs_intr__printf(sample); 1332 1333 if (sample_type & PERF_SAMPLE_STACK_USER) 1334 stack_user__printf(&sample->user_stack); 1335 1336 if (sample_type & PERF_SAMPLE_WEIGHT_TYPE) { 1337 printf("... weight: %" PRIu64 "", sample->weight); 1338 if (sample_type & PERF_SAMPLE_WEIGHT_STRUCT) { 1339 printf(",0x%"PRIx16"", sample->ins_lat); 1340 printf(",0x%"PRIx16"", sample->p_stage_cyc); 1341 } 1342 printf("\n"); 1343 } 1344 1345 if (sample_type & PERF_SAMPLE_DATA_SRC) 1346 printf(" . data_src: 0x%"PRIx64"\n", sample->data_src); 1347 1348 if (sample_type & PERF_SAMPLE_PHYS_ADDR) 1349 printf(" .. phys_addr: 0x%"PRIx64"\n", sample->phys_addr); 1350 1351 if (sample_type & PERF_SAMPLE_DATA_PAGE_SIZE) 1352 printf(" .. data page size: %s\n", get_page_size_name(sample->data_page_size, str)); 1353 1354 if (sample_type & PERF_SAMPLE_CODE_PAGE_SIZE) 1355 printf(" .. code page size: %s\n", get_page_size_name(sample->code_page_size, str)); 1356 1357 if (sample_type & PERF_SAMPLE_TRANSACTION) 1358 printf("... transaction: %" PRIx64 "\n", sample->transaction); 1359 1360 if (sample_type & PERF_SAMPLE_READ) 1361 sample_read__printf(sample, evsel->core.attr.read_format); 1362 } 1363 1364 static void dump_read(struct evsel *evsel, union perf_event *event) 1365 { 1366 struct perf_record_read *read_event = &event->read; 1367 u64 read_format; 1368 1369 if (!dump_trace) 1370 return; 1371 1372 printf(": %d %d %s %" PRI_lu64 "\n", event->read.pid, event->read.tid, 1373 evsel__name(evsel), event->read.value); 1374 1375 if (!evsel) 1376 return; 1377 1378 read_format = evsel->core.attr.read_format; 1379 1380 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 1381 printf("... time enabled : %" PRI_lu64 "\n", read_event->time_enabled); 1382 1383 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 1384 printf("... time running : %" PRI_lu64 "\n", read_event->time_running); 1385 1386 if (read_format & PERF_FORMAT_ID) 1387 printf("... id : %" PRI_lu64 "\n", read_event->id); 1388 } 1389 1390 static struct machine *machines__find_for_cpumode(struct machines *machines, 1391 union perf_event *event, 1392 struct perf_sample *sample) 1393 { 1394 if (perf_guest && 1395 ((sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL) || 1396 (sample->cpumode == PERF_RECORD_MISC_GUEST_USER))) { 1397 u32 pid; 1398 1399 if (event->header.type == PERF_RECORD_MMAP 1400 || event->header.type == PERF_RECORD_MMAP2) 1401 pid = event->mmap.pid; 1402 else 1403 pid = sample->pid; 1404 1405 return machines__find_guest(machines, pid); 1406 } 1407 1408 return &machines->host; 1409 } 1410 1411 static int deliver_sample_value(struct evlist *evlist, 1412 struct perf_tool *tool, 1413 union perf_event *event, 1414 struct perf_sample *sample, 1415 struct sample_read_value *v, 1416 struct machine *machine) 1417 { 1418 struct perf_sample_id *sid = evlist__id2sid(evlist, v->id); 1419 struct evsel *evsel; 1420 1421 if (sid) { 1422 sample->id = v->id; 1423 sample->period = v->value - sid->period; 1424 sid->period = v->value; 1425 } 1426 1427 if (!sid || sid->evsel == NULL) { 1428 ++evlist->stats.nr_unknown_id; 1429 return 0; 1430 } 1431 1432 /* 1433 * There's no reason to deliver sample 1434 * for zero period, bail out. 1435 */ 1436 if (!sample->period) 1437 return 0; 1438 1439 evsel = container_of(sid->evsel, struct evsel, core); 1440 return tool->sample(tool, event, sample, evsel, machine); 1441 } 1442 1443 static int deliver_sample_group(struct evlist *evlist, 1444 struct perf_tool *tool, 1445 union perf_event *event, 1446 struct perf_sample *sample, 1447 struct machine *machine) 1448 { 1449 int ret = -EINVAL; 1450 u64 i; 1451 1452 for (i = 0; i < sample->read.group.nr; i++) { 1453 ret = deliver_sample_value(evlist, tool, event, sample, 1454 &sample->read.group.values[i], 1455 machine); 1456 if (ret) 1457 break; 1458 } 1459 1460 return ret; 1461 } 1462 1463 static int evlist__deliver_sample(struct evlist *evlist, struct perf_tool *tool, 1464 union perf_event *event, struct perf_sample *sample, 1465 struct evsel *evsel, struct machine *machine) 1466 { 1467 /* We know evsel != NULL. */ 1468 u64 sample_type = evsel->core.attr.sample_type; 1469 u64 read_format = evsel->core.attr.read_format; 1470 1471 /* Standard sample delivery. */ 1472 if (!(sample_type & PERF_SAMPLE_READ)) 1473 return tool->sample(tool, event, sample, evsel, machine); 1474 1475 /* For PERF_SAMPLE_READ we have either single or group mode. */ 1476 if (read_format & PERF_FORMAT_GROUP) 1477 return deliver_sample_group(evlist, tool, event, sample, 1478 machine); 1479 else 1480 return deliver_sample_value(evlist, tool, event, sample, 1481 &sample->read.one, machine); 1482 } 1483 1484 static int machines__deliver_event(struct machines *machines, 1485 struct evlist *evlist, 1486 union perf_event *event, 1487 struct perf_sample *sample, 1488 struct perf_tool *tool, u64 file_offset) 1489 { 1490 struct evsel *evsel; 1491 struct machine *machine; 1492 1493 dump_event(evlist, event, file_offset, sample); 1494 1495 evsel = evlist__id2evsel(evlist, sample->id); 1496 1497 machine = machines__find_for_cpumode(machines, event, sample); 1498 1499 switch (event->header.type) { 1500 case PERF_RECORD_SAMPLE: 1501 if (evsel == NULL) { 1502 ++evlist->stats.nr_unknown_id; 1503 return 0; 1504 } 1505 dump_sample(evsel, event, sample); 1506 if (machine == NULL) { 1507 ++evlist->stats.nr_unprocessable_samples; 1508 return 0; 1509 } 1510 return evlist__deliver_sample(evlist, tool, event, sample, evsel, machine); 1511 case PERF_RECORD_MMAP: 1512 return tool->mmap(tool, event, sample, machine); 1513 case PERF_RECORD_MMAP2: 1514 if (event->header.misc & PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT) 1515 ++evlist->stats.nr_proc_map_timeout; 1516 return tool->mmap2(tool, event, sample, machine); 1517 case PERF_RECORD_COMM: 1518 return tool->comm(tool, event, sample, machine); 1519 case PERF_RECORD_NAMESPACES: 1520 return tool->namespaces(tool, event, sample, machine); 1521 case PERF_RECORD_CGROUP: 1522 return tool->cgroup(tool, event, sample, machine); 1523 case PERF_RECORD_FORK: 1524 return tool->fork(tool, event, sample, machine); 1525 case PERF_RECORD_EXIT: 1526 return tool->exit(tool, event, sample, machine); 1527 case PERF_RECORD_LOST: 1528 if (tool->lost == perf_event__process_lost) 1529 evlist->stats.total_lost += event->lost.lost; 1530 return tool->lost(tool, event, sample, machine); 1531 case PERF_RECORD_LOST_SAMPLES: 1532 if (tool->lost_samples == perf_event__process_lost_samples) 1533 evlist->stats.total_lost_samples += event->lost_samples.lost; 1534 return tool->lost_samples(tool, event, sample, machine); 1535 case PERF_RECORD_READ: 1536 dump_read(evsel, event); 1537 return tool->read(tool, event, sample, evsel, machine); 1538 case PERF_RECORD_THROTTLE: 1539 return tool->throttle(tool, event, sample, machine); 1540 case PERF_RECORD_UNTHROTTLE: 1541 return tool->unthrottle(tool, event, sample, machine); 1542 case PERF_RECORD_AUX: 1543 if (tool->aux == perf_event__process_aux) { 1544 if (event->aux.flags & PERF_AUX_FLAG_TRUNCATED) 1545 evlist->stats.total_aux_lost += 1; 1546 if (event->aux.flags & PERF_AUX_FLAG_PARTIAL) 1547 evlist->stats.total_aux_partial += 1; 1548 if (event->aux.flags & PERF_AUX_FLAG_COLLISION) 1549 evlist->stats.total_aux_collision += 1; 1550 } 1551 return tool->aux(tool, event, sample, machine); 1552 case PERF_RECORD_ITRACE_START: 1553 return tool->itrace_start(tool, event, sample, machine); 1554 case PERF_RECORD_SWITCH: 1555 case PERF_RECORD_SWITCH_CPU_WIDE: 1556 return tool->context_switch(tool, event, sample, machine); 1557 case PERF_RECORD_KSYMBOL: 1558 return tool->ksymbol(tool, event, sample, machine); 1559 case PERF_RECORD_BPF_EVENT: 1560 return tool->bpf(tool, event, sample, machine); 1561 case PERF_RECORD_TEXT_POKE: 1562 return tool->text_poke(tool, event, sample, machine); 1563 case PERF_RECORD_AUX_OUTPUT_HW_ID: 1564 return tool->aux_output_hw_id(tool, event, sample, machine); 1565 default: 1566 ++evlist->stats.nr_unknown_events; 1567 return -1; 1568 } 1569 } 1570 1571 static int perf_session__deliver_event(struct perf_session *session, 1572 union perf_event *event, 1573 struct perf_tool *tool, 1574 u64 file_offset) 1575 { 1576 struct perf_sample sample; 1577 int ret = evlist__parse_sample(session->evlist, event, &sample); 1578 1579 if (ret) { 1580 pr_err("Can't parse sample, err = %d\n", ret); 1581 return ret; 1582 } 1583 1584 ret = auxtrace__process_event(session, event, &sample, tool); 1585 if (ret < 0) 1586 return ret; 1587 if (ret > 0) 1588 return 0; 1589 1590 ret = machines__deliver_event(&session->machines, session->evlist, 1591 event, &sample, tool, file_offset); 1592 1593 if (dump_trace && sample.aux_sample.size) 1594 auxtrace__dump_auxtrace_sample(session, &sample); 1595 1596 return ret; 1597 } 1598 1599 static s64 perf_session__process_user_event(struct perf_session *session, 1600 union perf_event *event, 1601 u64 file_offset) 1602 { 1603 struct ordered_events *oe = &session->ordered_events; 1604 struct perf_tool *tool = session->tool; 1605 struct perf_sample sample = { .time = 0, }; 1606 int fd = perf_data__fd(session->data); 1607 int err; 1608 1609 if (event->header.type != PERF_RECORD_COMPRESSED || 1610 tool->compressed == perf_session__process_compressed_event_stub) 1611 dump_event(session->evlist, event, file_offset, &sample); 1612 1613 /* These events are processed right away */ 1614 switch (event->header.type) { 1615 case PERF_RECORD_HEADER_ATTR: 1616 err = tool->attr(tool, event, &session->evlist); 1617 if (err == 0) { 1618 perf_session__set_id_hdr_size(session); 1619 perf_session__set_comm_exec(session); 1620 } 1621 return err; 1622 case PERF_RECORD_EVENT_UPDATE: 1623 return tool->event_update(tool, event, &session->evlist); 1624 case PERF_RECORD_HEADER_EVENT_TYPE: 1625 /* 1626 * Deprecated, but we need to handle it for sake 1627 * of old data files create in pipe mode. 1628 */ 1629 return 0; 1630 case PERF_RECORD_HEADER_TRACING_DATA: 1631 /* 1632 * Setup for reading amidst mmap, but only when we 1633 * are in 'file' mode. The 'pipe' fd is in proper 1634 * place already. 1635 */ 1636 if (!perf_data__is_pipe(session->data)) 1637 lseek(fd, file_offset, SEEK_SET); 1638 return tool->tracing_data(session, event); 1639 case PERF_RECORD_HEADER_BUILD_ID: 1640 return tool->build_id(session, event); 1641 case PERF_RECORD_FINISHED_ROUND: 1642 return tool->finished_round(tool, event, oe); 1643 case PERF_RECORD_ID_INDEX: 1644 return tool->id_index(session, event); 1645 case PERF_RECORD_AUXTRACE_INFO: 1646 return tool->auxtrace_info(session, event); 1647 case PERF_RECORD_AUXTRACE: 1648 /* setup for reading amidst mmap */ 1649 lseek(fd, file_offset + event->header.size, SEEK_SET); 1650 return tool->auxtrace(session, event); 1651 case PERF_RECORD_AUXTRACE_ERROR: 1652 perf_session__auxtrace_error_inc(session, event); 1653 return tool->auxtrace_error(session, event); 1654 case PERF_RECORD_THREAD_MAP: 1655 return tool->thread_map(session, event); 1656 case PERF_RECORD_CPU_MAP: 1657 return tool->cpu_map(session, event); 1658 case PERF_RECORD_STAT_CONFIG: 1659 return tool->stat_config(session, event); 1660 case PERF_RECORD_STAT: 1661 return tool->stat(session, event); 1662 case PERF_RECORD_STAT_ROUND: 1663 return tool->stat_round(session, event); 1664 case PERF_RECORD_TIME_CONV: 1665 session->time_conv = event->time_conv; 1666 return tool->time_conv(session, event); 1667 case PERF_RECORD_HEADER_FEATURE: 1668 return tool->feature(session, event); 1669 case PERF_RECORD_COMPRESSED: 1670 err = tool->compressed(session, event, file_offset); 1671 if (err) 1672 dump_event(session->evlist, event, file_offset, &sample); 1673 return err; 1674 default: 1675 return -EINVAL; 1676 } 1677 } 1678 1679 int perf_session__deliver_synth_event(struct perf_session *session, 1680 union perf_event *event, 1681 struct perf_sample *sample) 1682 { 1683 struct evlist *evlist = session->evlist; 1684 struct perf_tool *tool = session->tool; 1685 1686 events_stats__inc(&evlist->stats, event->header.type); 1687 1688 if (event->header.type >= PERF_RECORD_USER_TYPE_START) 1689 return perf_session__process_user_event(session, event, 0); 1690 1691 return machines__deliver_event(&session->machines, evlist, event, sample, tool, 0); 1692 } 1693 1694 static void event_swap(union perf_event *event, bool sample_id_all) 1695 { 1696 perf_event__swap_op swap; 1697 1698 swap = perf_event__swap_ops[event->header.type]; 1699 if (swap) 1700 swap(event, sample_id_all); 1701 } 1702 1703 int perf_session__peek_event(struct perf_session *session, off_t file_offset, 1704 void *buf, size_t buf_sz, 1705 union perf_event **event_ptr, 1706 struct perf_sample *sample) 1707 { 1708 union perf_event *event; 1709 size_t hdr_sz, rest; 1710 int fd; 1711 1712 if (session->one_mmap && !session->header.needs_swap) { 1713 event = file_offset - session->one_mmap_offset + 1714 session->one_mmap_addr; 1715 goto out_parse_sample; 1716 } 1717 1718 if (perf_data__is_pipe(session->data)) 1719 return -1; 1720 1721 fd = perf_data__fd(session->data); 1722 hdr_sz = sizeof(struct perf_event_header); 1723 1724 if (buf_sz < hdr_sz) 1725 return -1; 1726 1727 if (lseek(fd, file_offset, SEEK_SET) == (off_t)-1 || 1728 readn(fd, buf, hdr_sz) != (ssize_t)hdr_sz) 1729 return -1; 1730 1731 event = (union perf_event *)buf; 1732 1733 if (session->header.needs_swap) 1734 perf_event_header__bswap(&event->header); 1735 1736 if (event->header.size < hdr_sz || event->header.size > buf_sz) 1737 return -1; 1738 1739 buf += hdr_sz; 1740 rest = event->header.size - hdr_sz; 1741 1742 if (readn(fd, buf, rest) != (ssize_t)rest) 1743 return -1; 1744 1745 if (session->header.needs_swap) 1746 event_swap(event, evlist__sample_id_all(session->evlist)); 1747 1748 out_parse_sample: 1749 1750 if (sample && event->header.type < PERF_RECORD_USER_TYPE_START && 1751 evlist__parse_sample(session->evlist, event, sample)) 1752 return -1; 1753 1754 *event_ptr = event; 1755 1756 return 0; 1757 } 1758 1759 int perf_session__peek_events(struct perf_session *session, u64 offset, 1760 u64 size, peek_events_cb_t cb, void *data) 1761 { 1762 u64 max_offset = offset + size; 1763 char buf[PERF_SAMPLE_MAX_SIZE]; 1764 union perf_event *event; 1765 int err; 1766 1767 do { 1768 err = perf_session__peek_event(session, offset, buf, 1769 PERF_SAMPLE_MAX_SIZE, &event, 1770 NULL); 1771 if (err) 1772 return err; 1773 1774 err = cb(session, event, offset, data); 1775 if (err) 1776 return err; 1777 1778 offset += event->header.size; 1779 if (event->header.type == PERF_RECORD_AUXTRACE) 1780 offset += event->auxtrace.size; 1781 1782 } while (offset < max_offset); 1783 1784 return err; 1785 } 1786 1787 static s64 perf_session__process_event(struct perf_session *session, 1788 union perf_event *event, u64 file_offset) 1789 { 1790 struct evlist *evlist = session->evlist; 1791 struct perf_tool *tool = session->tool; 1792 int ret; 1793 1794 if (session->header.needs_swap) 1795 event_swap(event, evlist__sample_id_all(evlist)); 1796 1797 if (event->header.type >= PERF_RECORD_HEADER_MAX) 1798 return -EINVAL; 1799 1800 events_stats__inc(&evlist->stats, event->header.type); 1801 1802 if (event->header.type >= PERF_RECORD_USER_TYPE_START) 1803 return perf_session__process_user_event(session, event, file_offset); 1804 1805 if (tool->ordered_events) { 1806 u64 timestamp = -1ULL; 1807 1808 ret = evlist__parse_sample_timestamp(evlist, event, ×tamp); 1809 if (ret && ret != -1) 1810 return ret; 1811 1812 ret = perf_session__queue_event(session, event, timestamp, file_offset); 1813 if (ret != -ETIME) 1814 return ret; 1815 } 1816 1817 return perf_session__deliver_event(session, event, tool, file_offset); 1818 } 1819 1820 void perf_event_header__bswap(struct perf_event_header *hdr) 1821 { 1822 hdr->type = bswap_32(hdr->type); 1823 hdr->misc = bswap_16(hdr->misc); 1824 hdr->size = bswap_16(hdr->size); 1825 } 1826 1827 struct thread *perf_session__findnew(struct perf_session *session, pid_t pid) 1828 { 1829 return machine__findnew_thread(&session->machines.host, -1, pid); 1830 } 1831 1832 int perf_session__register_idle_thread(struct perf_session *session) 1833 { 1834 struct thread *thread = machine__idle_thread(&session->machines.host); 1835 1836 /* machine__idle_thread() got the thread, so put it */ 1837 thread__put(thread); 1838 return thread ? 0 : -1; 1839 } 1840 1841 static void 1842 perf_session__warn_order(const struct perf_session *session) 1843 { 1844 const struct ordered_events *oe = &session->ordered_events; 1845 struct evsel *evsel; 1846 bool should_warn = true; 1847 1848 evlist__for_each_entry(session->evlist, evsel) { 1849 if (evsel->core.attr.write_backward) 1850 should_warn = false; 1851 } 1852 1853 if (!should_warn) 1854 return; 1855 if (oe->nr_unordered_events != 0) 1856 ui__warning("%u out of order events recorded.\n", oe->nr_unordered_events); 1857 } 1858 1859 static void perf_session__warn_about_errors(const struct perf_session *session) 1860 { 1861 const struct events_stats *stats = &session->evlist->stats; 1862 1863 if (session->tool->lost == perf_event__process_lost && 1864 stats->nr_events[PERF_RECORD_LOST] != 0) { 1865 ui__warning("Processed %d events and lost %d chunks!\n\n" 1866 "Check IO/CPU overload!\n\n", 1867 stats->nr_events[0], 1868 stats->nr_events[PERF_RECORD_LOST]); 1869 } 1870 1871 if (session->tool->lost_samples == perf_event__process_lost_samples) { 1872 double drop_rate; 1873 1874 drop_rate = (double)stats->total_lost_samples / 1875 (double) (stats->nr_events[PERF_RECORD_SAMPLE] + stats->total_lost_samples); 1876 if (drop_rate > 0.05) { 1877 ui__warning("Processed %" PRIu64 " samples and lost %3.2f%%!\n\n", 1878 stats->nr_events[PERF_RECORD_SAMPLE] + stats->total_lost_samples, 1879 drop_rate * 100.0); 1880 } 1881 } 1882 1883 if (session->tool->aux == perf_event__process_aux && 1884 stats->total_aux_lost != 0) { 1885 ui__warning("AUX data lost %" PRIu64 " times out of %u!\n\n", 1886 stats->total_aux_lost, 1887 stats->nr_events[PERF_RECORD_AUX]); 1888 } 1889 1890 if (session->tool->aux == perf_event__process_aux && 1891 stats->total_aux_partial != 0) { 1892 bool vmm_exclusive = false; 1893 1894 (void)sysfs__read_bool("module/kvm_intel/parameters/vmm_exclusive", 1895 &vmm_exclusive); 1896 1897 ui__warning("AUX data had gaps in it %" PRIu64 " times out of %u!\n\n" 1898 "Are you running a KVM guest in the background?%s\n\n", 1899 stats->total_aux_partial, 1900 stats->nr_events[PERF_RECORD_AUX], 1901 vmm_exclusive ? 1902 "\nReloading kvm_intel module with vmm_exclusive=0\n" 1903 "will reduce the gaps to only guest's timeslices." : 1904 ""); 1905 } 1906 1907 if (session->tool->aux == perf_event__process_aux && 1908 stats->total_aux_collision != 0) { 1909 ui__warning("AUX data detected collision %" PRIu64 " times out of %u!\n\n", 1910 stats->total_aux_collision, 1911 stats->nr_events[PERF_RECORD_AUX]); 1912 } 1913 1914 if (stats->nr_unknown_events != 0) { 1915 ui__warning("Found %u unknown events!\n\n" 1916 "Is this an older tool processing a perf.data " 1917 "file generated by a more recent tool?\n\n" 1918 "If that is not the case, consider " 1919 "reporting to linux-kernel@vger.kernel.org.\n\n", 1920 stats->nr_unknown_events); 1921 } 1922 1923 if (stats->nr_unknown_id != 0) { 1924 ui__warning("%u samples with id not present in the header\n", 1925 stats->nr_unknown_id); 1926 } 1927 1928 if (stats->nr_invalid_chains != 0) { 1929 ui__warning("Found invalid callchains!\n\n" 1930 "%u out of %u events were discarded for this reason.\n\n" 1931 "Consider reporting to linux-kernel@vger.kernel.org.\n\n", 1932 stats->nr_invalid_chains, 1933 stats->nr_events[PERF_RECORD_SAMPLE]); 1934 } 1935 1936 if (stats->nr_unprocessable_samples != 0) { 1937 ui__warning("%u unprocessable samples recorded.\n" 1938 "Do you have a KVM guest running and not using 'perf kvm'?\n", 1939 stats->nr_unprocessable_samples); 1940 } 1941 1942 perf_session__warn_order(session); 1943 1944 events_stats__auxtrace_error_warn(stats); 1945 1946 if (stats->nr_proc_map_timeout != 0) { 1947 ui__warning("%d map information files for pre-existing threads were\n" 1948 "not processed, if there are samples for addresses they\n" 1949 "will not be resolved, you may find out which are these\n" 1950 "threads by running with -v and redirecting the output\n" 1951 "to a file.\n" 1952 "The time limit to process proc map is too short?\n" 1953 "Increase it by --proc-map-timeout\n", 1954 stats->nr_proc_map_timeout); 1955 } 1956 } 1957 1958 static int perf_session__flush_thread_stack(struct thread *thread, 1959 void *p __maybe_unused) 1960 { 1961 return thread_stack__flush(thread); 1962 } 1963 1964 static int perf_session__flush_thread_stacks(struct perf_session *session) 1965 { 1966 return machines__for_each_thread(&session->machines, 1967 perf_session__flush_thread_stack, 1968 NULL); 1969 } 1970 1971 volatile int session_done; 1972 1973 static int __perf_session__process_decomp_events(struct perf_session *session); 1974 1975 static int __perf_session__process_pipe_events(struct perf_session *session) 1976 { 1977 struct ordered_events *oe = &session->ordered_events; 1978 struct perf_tool *tool = session->tool; 1979 union perf_event *event; 1980 uint32_t size, cur_size = 0; 1981 void *buf = NULL; 1982 s64 skip = 0; 1983 u64 head; 1984 ssize_t err; 1985 void *p; 1986 1987 perf_tool__fill_defaults(tool); 1988 1989 head = 0; 1990 cur_size = sizeof(union perf_event); 1991 1992 buf = malloc(cur_size); 1993 if (!buf) 1994 return -errno; 1995 ordered_events__set_copy_on_queue(oe, true); 1996 more: 1997 event = buf; 1998 err = perf_data__read(session->data, event, 1999 sizeof(struct perf_event_header)); 2000 if (err <= 0) { 2001 if (err == 0) 2002 goto done; 2003 2004 pr_err("failed to read event header\n"); 2005 goto out_err; 2006 } 2007 2008 if (session->header.needs_swap) 2009 perf_event_header__bswap(&event->header); 2010 2011 size = event->header.size; 2012 if (size < sizeof(struct perf_event_header)) { 2013 pr_err("bad event header size\n"); 2014 goto out_err; 2015 } 2016 2017 if (size > cur_size) { 2018 void *new = realloc(buf, size); 2019 if (!new) { 2020 pr_err("failed to allocate memory to read event\n"); 2021 goto out_err; 2022 } 2023 buf = new; 2024 cur_size = size; 2025 event = buf; 2026 } 2027 p = event; 2028 p += sizeof(struct perf_event_header); 2029 2030 if (size - sizeof(struct perf_event_header)) { 2031 err = perf_data__read(session->data, p, 2032 size - sizeof(struct perf_event_header)); 2033 if (err <= 0) { 2034 if (err == 0) { 2035 pr_err("unexpected end of event stream\n"); 2036 goto done; 2037 } 2038 2039 pr_err("failed to read event data\n"); 2040 goto out_err; 2041 } 2042 } 2043 2044 if ((skip = perf_session__process_event(session, event, head)) < 0) { 2045 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n", 2046 head, event->header.size, event->header.type); 2047 err = -EINVAL; 2048 goto out_err; 2049 } 2050 2051 head += size; 2052 2053 if (skip > 0) 2054 head += skip; 2055 2056 err = __perf_session__process_decomp_events(session); 2057 if (err) 2058 goto out_err; 2059 2060 if (!session_done()) 2061 goto more; 2062 done: 2063 /* do the final flush for ordered samples */ 2064 err = ordered_events__flush(oe, OE_FLUSH__FINAL); 2065 if (err) 2066 goto out_err; 2067 err = auxtrace__flush_events(session, tool); 2068 if (err) 2069 goto out_err; 2070 err = perf_session__flush_thread_stacks(session); 2071 out_err: 2072 free(buf); 2073 if (!tool->no_warn) 2074 perf_session__warn_about_errors(session); 2075 ordered_events__free(&session->ordered_events); 2076 auxtrace__free_events(session); 2077 return err; 2078 } 2079 2080 static union perf_event * 2081 prefetch_event(char *buf, u64 head, size_t mmap_size, 2082 bool needs_swap, union perf_event *error) 2083 { 2084 union perf_event *event; 2085 2086 /* 2087 * Ensure we have enough space remaining to read 2088 * the size of the event in the headers. 2089 */ 2090 if (head + sizeof(event->header) > mmap_size) 2091 return NULL; 2092 2093 event = (union perf_event *)(buf + head); 2094 if (needs_swap) 2095 perf_event_header__bswap(&event->header); 2096 2097 if (head + event->header.size <= mmap_size) 2098 return event; 2099 2100 /* We're not fetching the event so swap back again */ 2101 if (needs_swap) 2102 perf_event_header__bswap(&event->header); 2103 2104 pr_debug("%s: head=%#" PRIx64 " event->header_size=%#x, mmap_size=%#zx:" 2105 " fuzzed or compressed perf.data?\n",__func__, head, event->header.size, mmap_size); 2106 2107 return error; 2108 } 2109 2110 static union perf_event * 2111 fetch_mmaped_event(u64 head, size_t mmap_size, char *buf, bool needs_swap) 2112 { 2113 return prefetch_event(buf, head, mmap_size, needs_swap, ERR_PTR(-EINVAL)); 2114 } 2115 2116 static union perf_event * 2117 fetch_decomp_event(u64 head, size_t mmap_size, char *buf, bool needs_swap) 2118 { 2119 return prefetch_event(buf, head, mmap_size, needs_swap, NULL); 2120 } 2121 2122 static int __perf_session__process_decomp_events(struct perf_session *session) 2123 { 2124 s64 skip; 2125 u64 size; 2126 struct decomp *decomp = session->active_decomp->decomp_last; 2127 2128 if (!decomp) 2129 return 0; 2130 2131 while (decomp->head < decomp->size && !session_done()) { 2132 union perf_event *event = fetch_decomp_event(decomp->head, decomp->size, decomp->data, 2133 session->header.needs_swap); 2134 2135 if (!event) 2136 break; 2137 2138 size = event->header.size; 2139 2140 if (size < sizeof(struct perf_event_header) || 2141 (skip = perf_session__process_event(session, event, decomp->file_pos)) < 0) { 2142 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n", 2143 decomp->file_pos + decomp->head, event->header.size, event->header.type); 2144 return -EINVAL; 2145 } 2146 2147 if (skip) 2148 size += skip; 2149 2150 decomp->head += size; 2151 } 2152 2153 return 0; 2154 } 2155 2156 /* 2157 * On 64bit we can mmap the data file in one go. No need for tiny mmap 2158 * slices. On 32bit we use 32MB. 2159 */ 2160 #if BITS_PER_LONG == 64 2161 #define MMAP_SIZE ULLONG_MAX 2162 #define NUM_MMAPS 1 2163 #else 2164 #define MMAP_SIZE (32 * 1024 * 1024ULL) 2165 #define NUM_MMAPS 128 2166 #endif 2167 2168 struct reader; 2169 2170 typedef s64 (*reader_cb_t)(struct perf_session *session, 2171 union perf_event *event, 2172 u64 file_offset); 2173 2174 struct reader { 2175 int fd; 2176 u64 data_size; 2177 u64 data_offset; 2178 reader_cb_t process; 2179 bool in_place_update; 2180 char *mmaps[NUM_MMAPS]; 2181 size_t mmap_size; 2182 int mmap_idx; 2183 char *mmap_cur; 2184 u64 file_pos; 2185 u64 file_offset; 2186 u64 head; 2187 struct zstd_data zstd_data; 2188 struct decomp_data decomp_data; 2189 }; 2190 2191 static int 2192 reader__init(struct reader *rd, bool *one_mmap) 2193 { 2194 u64 data_size = rd->data_size; 2195 char **mmaps = rd->mmaps; 2196 2197 rd->head = rd->data_offset; 2198 data_size += rd->data_offset; 2199 2200 rd->mmap_size = MMAP_SIZE; 2201 if (rd->mmap_size > data_size) { 2202 rd->mmap_size = data_size; 2203 if (one_mmap) 2204 *one_mmap = true; 2205 } 2206 2207 memset(mmaps, 0, sizeof(rd->mmaps)); 2208 2209 if (zstd_init(&rd->zstd_data, 0)) 2210 return -1; 2211 rd->decomp_data.zstd_decomp = &rd->zstd_data; 2212 2213 return 0; 2214 } 2215 2216 static void 2217 reader__release_decomp(struct reader *rd) 2218 { 2219 perf_decomp__release_events(rd->decomp_data.decomp); 2220 zstd_fini(&rd->zstd_data); 2221 } 2222 2223 static int 2224 reader__mmap(struct reader *rd, struct perf_session *session) 2225 { 2226 int mmap_prot, mmap_flags; 2227 char *buf, **mmaps = rd->mmaps; 2228 u64 page_offset; 2229 2230 mmap_prot = PROT_READ; 2231 mmap_flags = MAP_SHARED; 2232 2233 if (rd->in_place_update) { 2234 mmap_prot |= PROT_WRITE; 2235 } else if (session->header.needs_swap) { 2236 mmap_prot |= PROT_WRITE; 2237 mmap_flags = MAP_PRIVATE; 2238 } 2239 2240 if (mmaps[rd->mmap_idx]) { 2241 munmap(mmaps[rd->mmap_idx], rd->mmap_size); 2242 mmaps[rd->mmap_idx] = NULL; 2243 } 2244 2245 page_offset = page_size * (rd->head / page_size); 2246 rd->file_offset += page_offset; 2247 rd->head -= page_offset; 2248 2249 buf = mmap(NULL, rd->mmap_size, mmap_prot, mmap_flags, rd->fd, 2250 rd->file_offset); 2251 if (buf == MAP_FAILED) { 2252 pr_err("failed to mmap file\n"); 2253 return -errno; 2254 } 2255 mmaps[rd->mmap_idx] = rd->mmap_cur = buf; 2256 rd->mmap_idx = (rd->mmap_idx + 1) & (ARRAY_SIZE(rd->mmaps) - 1); 2257 rd->file_pos = rd->file_offset + rd->head; 2258 if (session->one_mmap) { 2259 session->one_mmap_addr = buf; 2260 session->one_mmap_offset = rd->file_offset; 2261 } 2262 2263 return 0; 2264 } 2265 2266 enum { 2267 READER_OK, 2268 READER_NODATA, 2269 }; 2270 2271 static int 2272 reader__read_event(struct reader *rd, struct perf_session *session, 2273 struct ui_progress *prog) 2274 { 2275 u64 size; 2276 int err = READER_OK; 2277 union perf_event *event; 2278 s64 skip; 2279 2280 event = fetch_mmaped_event(rd->head, rd->mmap_size, rd->mmap_cur, 2281 session->header.needs_swap); 2282 if (IS_ERR(event)) 2283 return PTR_ERR(event); 2284 2285 if (!event) 2286 return READER_NODATA; 2287 2288 size = event->header.size; 2289 2290 skip = -EINVAL; 2291 2292 if (size < sizeof(struct perf_event_header) || 2293 (skip = rd->process(session, event, rd->file_pos)) < 0) { 2294 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d [%s]\n", 2295 rd->file_offset + rd->head, event->header.size, 2296 event->header.type, strerror(-skip)); 2297 err = skip; 2298 goto out; 2299 } 2300 2301 if (skip) 2302 size += skip; 2303 2304 rd->head += size; 2305 rd->file_pos += size; 2306 2307 err = __perf_session__process_decomp_events(session); 2308 if (err) 2309 goto out; 2310 2311 ui_progress__update(prog, size); 2312 2313 out: 2314 return err; 2315 } 2316 2317 static inline bool 2318 reader__eof(struct reader *rd) 2319 { 2320 return (rd->file_pos >= rd->data_size + rd->data_offset); 2321 } 2322 2323 static int 2324 reader__process_events(struct reader *rd, struct perf_session *session, 2325 struct ui_progress *prog) 2326 { 2327 int err; 2328 2329 err = reader__init(rd, &session->one_mmap); 2330 if (err) 2331 goto out; 2332 2333 session->active_decomp = &rd->decomp_data; 2334 2335 remap: 2336 err = reader__mmap(rd, session); 2337 if (err) 2338 goto out; 2339 2340 more: 2341 err = reader__read_event(rd, session, prog); 2342 if (err < 0) 2343 goto out; 2344 else if (err == READER_NODATA) 2345 goto remap; 2346 2347 if (session_done()) 2348 goto out; 2349 2350 if (!reader__eof(rd)) 2351 goto more; 2352 2353 out: 2354 session->active_decomp = &session->decomp_data; 2355 return err; 2356 } 2357 2358 static s64 process_simple(struct perf_session *session, 2359 union perf_event *event, 2360 u64 file_offset) 2361 { 2362 return perf_session__process_event(session, event, file_offset); 2363 } 2364 2365 static int __perf_session__process_events(struct perf_session *session) 2366 { 2367 struct reader rd = { 2368 .fd = perf_data__fd(session->data), 2369 .data_size = session->header.data_size, 2370 .data_offset = session->header.data_offset, 2371 .process = process_simple, 2372 .in_place_update = session->data->in_place_update, 2373 }; 2374 struct ordered_events *oe = &session->ordered_events; 2375 struct perf_tool *tool = session->tool; 2376 struct ui_progress prog; 2377 int err; 2378 2379 perf_tool__fill_defaults(tool); 2380 2381 if (rd.data_size == 0) 2382 return -1; 2383 2384 ui_progress__init_size(&prog, rd.data_size, "Processing events..."); 2385 2386 err = reader__process_events(&rd, session, &prog); 2387 if (err) 2388 goto out_err; 2389 /* do the final flush for ordered samples */ 2390 err = ordered_events__flush(oe, OE_FLUSH__FINAL); 2391 if (err) 2392 goto out_err; 2393 err = auxtrace__flush_events(session, tool); 2394 if (err) 2395 goto out_err; 2396 err = perf_session__flush_thread_stacks(session); 2397 out_err: 2398 ui_progress__finish(); 2399 if (!tool->no_warn) 2400 perf_session__warn_about_errors(session); 2401 /* 2402 * We may switching perf.data output, make ordered_events 2403 * reusable. 2404 */ 2405 ordered_events__reinit(&session->ordered_events); 2406 auxtrace__free_events(session); 2407 reader__release_decomp(&rd); 2408 session->one_mmap = false; 2409 return err; 2410 } 2411 2412 int perf_session__process_events(struct perf_session *session) 2413 { 2414 if (perf_session__register_idle_thread(session) < 0) 2415 return -ENOMEM; 2416 2417 if (perf_data__is_pipe(session->data)) 2418 return __perf_session__process_pipe_events(session); 2419 2420 return __perf_session__process_events(session); 2421 } 2422 2423 bool perf_session__has_traces(struct perf_session *session, const char *msg) 2424 { 2425 struct evsel *evsel; 2426 2427 evlist__for_each_entry(session->evlist, evsel) { 2428 if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT) 2429 return true; 2430 } 2431 2432 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg); 2433 return false; 2434 } 2435 2436 int map__set_kallsyms_ref_reloc_sym(struct map *map, const char *symbol_name, u64 addr) 2437 { 2438 char *bracket; 2439 struct ref_reloc_sym *ref; 2440 struct kmap *kmap; 2441 2442 ref = zalloc(sizeof(struct ref_reloc_sym)); 2443 if (ref == NULL) 2444 return -ENOMEM; 2445 2446 ref->name = strdup(symbol_name); 2447 if (ref->name == NULL) { 2448 free(ref); 2449 return -ENOMEM; 2450 } 2451 2452 bracket = strchr(ref->name, ']'); 2453 if (bracket) 2454 *bracket = '\0'; 2455 2456 ref->addr = addr; 2457 2458 kmap = map__kmap(map); 2459 if (kmap) 2460 kmap->ref_reloc_sym = ref; 2461 2462 return 0; 2463 } 2464 2465 size_t perf_session__fprintf_dsos(struct perf_session *session, FILE *fp) 2466 { 2467 return machines__fprintf_dsos(&session->machines, fp); 2468 } 2469 2470 size_t perf_session__fprintf_dsos_buildid(struct perf_session *session, FILE *fp, 2471 bool (skip)(struct dso *dso, int parm), int parm) 2472 { 2473 return machines__fprintf_dsos_buildid(&session->machines, fp, skip, parm); 2474 } 2475 2476 size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp, 2477 bool skip_empty) 2478 { 2479 size_t ret; 2480 const char *msg = ""; 2481 2482 if (perf_header__has_feat(&session->header, HEADER_AUXTRACE)) 2483 msg = " (excludes AUX area (e.g. instruction trace) decoded / synthesized events)"; 2484 2485 ret = fprintf(fp, "\nAggregated stats:%s\n", msg); 2486 2487 ret += events_stats__fprintf(&session->evlist->stats, fp, skip_empty); 2488 return ret; 2489 } 2490 2491 size_t perf_session__fprintf(struct perf_session *session, FILE *fp) 2492 { 2493 /* 2494 * FIXME: Here we have to actually print all the machines in this 2495 * session, not just the host... 2496 */ 2497 return machine__fprintf(&session->machines.host, fp); 2498 } 2499 2500 struct evsel *perf_session__find_first_evtype(struct perf_session *session, 2501 unsigned int type) 2502 { 2503 struct evsel *pos; 2504 2505 evlist__for_each_entry(session->evlist, pos) { 2506 if (pos->core.attr.type == type) 2507 return pos; 2508 } 2509 return NULL; 2510 } 2511 2512 int perf_session__cpu_bitmap(struct perf_session *session, 2513 const char *cpu_list, unsigned long *cpu_bitmap) 2514 { 2515 int i, err = -1; 2516 struct perf_cpu_map *map; 2517 int nr_cpus = min(session->header.env.nr_cpus_avail, MAX_NR_CPUS); 2518 2519 for (i = 0; i < PERF_TYPE_MAX; ++i) { 2520 struct evsel *evsel; 2521 2522 evsel = perf_session__find_first_evtype(session, i); 2523 if (!evsel) 2524 continue; 2525 2526 if (!(evsel->core.attr.sample_type & PERF_SAMPLE_CPU)) { 2527 pr_err("File does not contain CPU events. " 2528 "Remove -C option to proceed.\n"); 2529 return -1; 2530 } 2531 } 2532 2533 map = perf_cpu_map__new(cpu_list); 2534 if (map == NULL) { 2535 pr_err("Invalid cpu_list\n"); 2536 return -1; 2537 } 2538 2539 for (i = 0; i < map->nr; i++) { 2540 int cpu = map->map[i]; 2541 2542 if (cpu >= nr_cpus) { 2543 pr_err("Requested CPU %d too large. " 2544 "Consider raising MAX_NR_CPUS\n", cpu); 2545 goto out_delete_map; 2546 } 2547 2548 set_bit(cpu, cpu_bitmap); 2549 } 2550 2551 err = 0; 2552 2553 out_delete_map: 2554 perf_cpu_map__put(map); 2555 return err; 2556 } 2557 2558 void perf_session__fprintf_info(struct perf_session *session, FILE *fp, 2559 bool full) 2560 { 2561 if (session == NULL || fp == NULL) 2562 return; 2563 2564 fprintf(fp, "# ========\n"); 2565 perf_header__fprintf_info(session, fp, full); 2566 fprintf(fp, "# ========\n#\n"); 2567 } 2568 2569 int perf_event__process_id_index(struct perf_session *session, 2570 union perf_event *event) 2571 { 2572 struct evlist *evlist = session->evlist; 2573 struct perf_record_id_index *ie = &event->id_index; 2574 size_t i, nr, max_nr; 2575 2576 max_nr = (ie->header.size - sizeof(struct perf_record_id_index)) / 2577 sizeof(struct id_index_entry); 2578 nr = ie->nr; 2579 if (nr > max_nr) 2580 return -EINVAL; 2581 2582 if (dump_trace) 2583 fprintf(stdout, " nr: %zu\n", nr); 2584 2585 for (i = 0; i < nr; i++) { 2586 struct id_index_entry *e = &ie->entries[i]; 2587 struct perf_sample_id *sid; 2588 2589 if (dump_trace) { 2590 fprintf(stdout, " ... id: %"PRI_lu64, e->id); 2591 fprintf(stdout, " idx: %"PRI_lu64, e->idx); 2592 fprintf(stdout, " cpu: %"PRI_ld64, e->cpu); 2593 fprintf(stdout, " tid: %"PRI_ld64"\n", e->tid); 2594 } 2595 2596 sid = evlist__id2sid(evlist, e->id); 2597 if (!sid) 2598 return -ENOENT; 2599 sid->idx = e->idx; 2600 sid->cpu = e->cpu; 2601 sid->tid = e->tid; 2602 } 2603 return 0; 2604 } 2605