1 #define _FILE_OFFSET_BITS 64 2 3 #include <linux/kernel.h> 4 5 #include <byteswap.h> 6 #include <unistd.h> 7 #include <sys/types.h> 8 #include <sys/mman.h> 9 10 #include "evlist.h" 11 #include "evsel.h" 12 #include "session.h" 13 #include "tool.h" 14 #include "sort.h" 15 #include "util.h" 16 #include "cpumap.h" 17 #include "event-parse.h" 18 #include "perf_regs.h" 19 #include "unwind.h" 20 21 static int perf_session__open(struct perf_session *self, bool force) 22 { 23 struct stat input_stat; 24 25 if (!strcmp(self->filename, "-")) { 26 self->fd_pipe = true; 27 self->fd = STDIN_FILENO; 28 29 if (perf_session__read_header(self, self->fd) < 0) 30 pr_err("incompatible file format (rerun with -v to learn more)"); 31 32 return 0; 33 } 34 35 self->fd = open(self->filename, O_RDONLY); 36 if (self->fd < 0) { 37 int err = errno; 38 39 pr_err("failed to open %s: %s", self->filename, strerror(err)); 40 if (err == ENOENT && !strcmp(self->filename, "perf.data")) 41 pr_err(" (try 'perf record' first)"); 42 pr_err("\n"); 43 return -errno; 44 } 45 46 if (fstat(self->fd, &input_stat) < 0) 47 goto out_close; 48 49 if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) { 50 pr_err("file %s not owned by current user or root\n", 51 self->filename); 52 goto out_close; 53 } 54 55 if (!input_stat.st_size) { 56 pr_info("zero-sized file (%s), nothing to do!\n", 57 self->filename); 58 goto out_close; 59 } 60 61 if (perf_session__read_header(self, self->fd) < 0) { 62 pr_err("incompatible file format (rerun with -v to learn more)"); 63 goto out_close; 64 } 65 66 if (!perf_evlist__valid_sample_type(self->evlist)) { 67 pr_err("non matching sample_type"); 68 goto out_close; 69 } 70 71 if (!perf_evlist__valid_sample_id_all(self->evlist)) { 72 pr_err("non matching sample_id_all"); 73 goto out_close; 74 } 75 76 self->size = input_stat.st_size; 77 return 0; 78 79 out_close: 80 close(self->fd); 81 self->fd = -1; 82 return -1; 83 } 84 85 void perf_session__set_id_hdr_size(struct perf_session *session) 86 { 87 u16 id_hdr_size = perf_evlist__id_hdr_size(session->evlist); 88 89 session->host_machine.id_hdr_size = id_hdr_size; 90 machines__set_id_hdr_size(&session->machines, id_hdr_size); 91 } 92 93 int perf_session__create_kernel_maps(struct perf_session *self) 94 { 95 int ret = machine__create_kernel_maps(&self->host_machine); 96 97 if (ret >= 0) 98 ret = machines__create_guest_kernel_maps(&self->machines); 99 return ret; 100 } 101 102 static void perf_session__destroy_kernel_maps(struct perf_session *self) 103 { 104 machine__destroy_kernel_maps(&self->host_machine); 105 machines__destroy_guest_kernel_maps(&self->machines); 106 } 107 108 struct perf_session *perf_session__new(const char *filename, int mode, 109 bool force, bool repipe, 110 struct perf_tool *tool) 111 { 112 struct perf_session *self; 113 struct stat st; 114 size_t len; 115 116 if (!filename || !strlen(filename)) { 117 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode)) 118 filename = "-"; 119 else 120 filename = "perf.data"; 121 } 122 123 len = strlen(filename); 124 self = zalloc(sizeof(*self) + len); 125 126 if (self == NULL) 127 goto out; 128 129 memcpy(self->filename, filename, len); 130 /* 131 * On 64bit we can mmap the data file in one go. No need for tiny mmap 132 * slices. On 32bit we use 32MB. 133 */ 134 #if BITS_PER_LONG == 64 135 self->mmap_window = ULLONG_MAX; 136 #else 137 self->mmap_window = 32 * 1024 * 1024ULL; 138 #endif 139 self->machines = RB_ROOT; 140 self->repipe = repipe; 141 INIT_LIST_HEAD(&self->ordered_samples.samples); 142 INIT_LIST_HEAD(&self->ordered_samples.sample_cache); 143 INIT_LIST_HEAD(&self->ordered_samples.to_free); 144 machine__init(&self->host_machine, "", HOST_KERNEL_ID); 145 hists__init(&self->hists); 146 147 if (mode == O_RDONLY) { 148 if (perf_session__open(self, force) < 0) 149 goto out_delete; 150 perf_session__set_id_hdr_size(self); 151 } else if (mode == O_WRONLY) { 152 /* 153 * In O_RDONLY mode this will be performed when reading the 154 * kernel MMAP event, in perf_event__process_mmap(). 155 */ 156 if (perf_session__create_kernel_maps(self) < 0) 157 goto out_delete; 158 } 159 160 if (tool && tool->ordering_requires_timestamps && 161 tool->ordered_samples && !perf_evlist__sample_id_all(self->evlist)) { 162 dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n"); 163 tool->ordered_samples = false; 164 } 165 166 out: 167 return self; 168 out_delete: 169 perf_session__delete(self); 170 return NULL; 171 } 172 173 static void machine__delete_dead_threads(struct machine *machine) 174 { 175 struct thread *n, *t; 176 177 list_for_each_entry_safe(t, n, &machine->dead_threads, node) { 178 list_del(&t->node); 179 thread__delete(t); 180 } 181 } 182 183 static void perf_session__delete_dead_threads(struct perf_session *session) 184 { 185 machine__delete_dead_threads(&session->host_machine); 186 } 187 188 static void machine__delete_threads(struct machine *self) 189 { 190 struct rb_node *nd = rb_first(&self->threads); 191 192 while (nd) { 193 struct thread *t = rb_entry(nd, struct thread, rb_node); 194 195 rb_erase(&t->rb_node, &self->threads); 196 nd = rb_next(nd); 197 thread__delete(t); 198 } 199 } 200 201 static void perf_session__delete_threads(struct perf_session *session) 202 { 203 machine__delete_threads(&session->host_machine); 204 } 205 206 void perf_session__delete(struct perf_session *self) 207 { 208 perf_session__destroy_kernel_maps(self); 209 perf_session__delete_dead_threads(self); 210 perf_session__delete_threads(self); 211 machine__exit(&self->host_machine); 212 close(self->fd); 213 free(self); 214 } 215 216 void machine__remove_thread(struct machine *self, struct thread *th) 217 { 218 self->last_match = NULL; 219 rb_erase(&th->rb_node, &self->threads); 220 /* 221 * We may have references to this thread, for instance in some hist_entry 222 * instances, so just move them to a separate list. 223 */ 224 list_add_tail(&th->node, &self->dead_threads); 225 } 226 227 static bool symbol__match_parent_regex(struct symbol *sym) 228 { 229 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0)) 230 return 1; 231 232 return 0; 233 } 234 235 static const u8 cpumodes[] = { 236 PERF_RECORD_MISC_USER, 237 PERF_RECORD_MISC_KERNEL, 238 PERF_RECORD_MISC_GUEST_USER, 239 PERF_RECORD_MISC_GUEST_KERNEL 240 }; 241 #define NCPUMODES (sizeof(cpumodes)/sizeof(u8)) 242 243 static void ip__resolve_ams(struct machine *self, struct thread *thread, 244 struct addr_map_symbol *ams, 245 u64 ip) 246 { 247 struct addr_location al; 248 size_t i; 249 u8 m; 250 251 memset(&al, 0, sizeof(al)); 252 253 for (i = 0; i < NCPUMODES; i++) { 254 m = cpumodes[i]; 255 /* 256 * We cannot use the header.misc hint to determine whether a 257 * branch stack address is user, kernel, guest, hypervisor. 258 * Branches may straddle the kernel/user/hypervisor boundaries. 259 * Thus, we have to try consecutively until we find a match 260 * or else, the symbol is unknown 261 */ 262 thread__find_addr_location(thread, self, m, MAP__FUNCTION, 263 ip, &al, NULL); 264 if (al.sym) 265 goto found; 266 } 267 found: 268 ams->addr = ip; 269 ams->al_addr = al.addr; 270 ams->sym = al.sym; 271 ams->map = al.map; 272 } 273 274 struct branch_info *machine__resolve_bstack(struct machine *self, 275 struct thread *thr, 276 struct branch_stack *bs) 277 { 278 struct branch_info *bi; 279 unsigned int i; 280 281 bi = calloc(bs->nr, sizeof(struct branch_info)); 282 if (!bi) 283 return NULL; 284 285 for (i = 0; i < bs->nr; i++) { 286 ip__resolve_ams(self, thr, &bi[i].to, bs->entries[i].to); 287 ip__resolve_ams(self, thr, &bi[i].from, bs->entries[i].from); 288 bi[i].flags = bs->entries[i].flags; 289 } 290 return bi; 291 } 292 293 static int machine__resolve_callchain_sample(struct machine *machine, 294 struct thread *thread, 295 struct ip_callchain *chain, 296 struct symbol **parent) 297 298 { 299 u8 cpumode = PERF_RECORD_MISC_USER; 300 unsigned int i; 301 int err; 302 303 callchain_cursor_reset(&callchain_cursor); 304 305 if (chain->nr > PERF_MAX_STACK_DEPTH) { 306 pr_warning("corrupted callchain. skipping...\n"); 307 return 0; 308 } 309 310 for (i = 0; i < chain->nr; i++) { 311 u64 ip; 312 struct addr_location al; 313 314 if (callchain_param.order == ORDER_CALLEE) 315 ip = chain->ips[i]; 316 else 317 ip = chain->ips[chain->nr - i - 1]; 318 319 if (ip >= PERF_CONTEXT_MAX) { 320 switch (ip) { 321 case PERF_CONTEXT_HV: 322 cpumode = PERF_RECORD_MISC_HYPERVISOR; 323 break; 324 case PERF_CONTEXT_KERNEL: 325 cpumode = PERF_RECORD_MISC_KERNEL; 326 break; 327 case PERF_CONTEXT_USER: 328 cpumode = PERF_RECORD_MISC_USER; 329 break; 330 default: 331 pr_debug("invalid callchain context: " 332 "%"PRId64"\n", (s64) ip); 333 /* 334 * It seems the callchain is corrupted. 335 * Discard all. 336 */ 337 callchain_cursor_reset(&callchain_cursor); 338 return 0; 339 } 340 continue; 341 } 342 343 al.filtered = false; 344 thread__find_addr_location(thread, machine, cpumode, 345 MAP__FUNCTION, ip, &al, NULL); 346 if (al.sym != NULL) { 347 if (sort__has_parent && !*parent && 348 symbol__match_parent_regex(al.sym)) 349 *parent = al.sym; 350 if (!symbol_conf.use_callchain) 351 break; 352 } 353 354 err = callchain_cursor_append(&callchain_cursor, 355 ip, al.map, al.sym); 356 if (err) 357 return err; 358 } 359 360 return 0; 361 } 362 363 static int unwind_entry(struct unwind_entry *entry, void *arg) 364 { 365 struct callchain_cursor *cursor = arg; 366 return callchain_cursor_append(cursor, entry->ip, 367 entry->map, entry->sym); 368 } 369 370 int machine__resolve_callchain(struct machine *machine, 371 struct perf_evsel *evsel, 372 struct thread *thread, 373 struct perf_sample *sample, 374 struct symbol **parent) 375 376 { 377 int ret; 378 379 callchain_cursor_reset(&callchain_cursor); 380 381 ret = machine__resolve_callchain_sample(machine, thread, 382 sample->callchain, parent); 383 if (ret) 384 return ret; 385 386 /* Can we do dwarf post unwind? */ 387 if (!((evsel->attr.sample_type & PERF_SAMPLE_REGS_USER) && 388 (evsel->attr.sample_type & PERF_SAMPLE_STACK_USER))) 389 return 0; 390 391 /* Bail out if nothing was captured. */ 392 if ((!sample->user_regs.regs) || 393 (!sample->user_stack.size)) 394 return 0; 395 396 return unwind__get_entries(unwind_entry, &callchain_cursor, machine, 397 thread, evsel->attr.sample_regs_user, 398 sample); 399 400 } 401 402 static int process_event_synth_tracing_data_stub(union perf_event *event __used, 403 struct perf_session *session __used) 404 { 405 dump_printf(": unhandled!\n"); 406 return 0; 407 } 408 409 static int process_event_synth_attr_stub(union perf_event *event __used, 410 struct perf_evlist **pevlist __used) 411 { 412 dump_printf(": unhandled!\n"); 413 return 0; 414 } 415 416 static int process_event_sample_stub(struct perf_tool *tool __used, 417 union perf_event *event __used, 418 struct perf_sample *sample __used, 419 struct perf_evsel *evsel __used, 420 struct machine *machine __used) 421 { 422 dump_printf(": unhandled!\n"); 423 return 0; 424 } 425 426 static int process_event_stub(struct perf_tool *tool __used, 427 union perf_event *event __used, 428 struct perf_sample *sample __used, 429 struct machine *machine __used) 430 { 431 dump_printf(": unhandled!\n"); 432 return 0; 433 } 434 435 static int process_finished_round_stub(struct perf_tool *tool __used, 436 union perf_event *event __used, 437 struct perf_session *perf_session __used) 438 { 439 dump_printf(": unhandled!\n"); 440 return 0; 441 } 442 443 static int process_event_type_stub(struct perf_tool *tool __used, 444 union perf_event *event __used) 445 { 446 dump_printf(": unhandled!\n"); 447 return 0; 448 } 449 450 static int process_finished_round(struct perf_tool *tool, 451 union perf_event *event, 452 struct perf_session *session); 453 454 static void perf_tool__fill_defaults(struct perf_tool *tool) 455 { 456 if (tool->sample == NULL) 457 tool->sample = process_event_sample_stub; 458 if (tool->mmap == NULL) 459 tool->mmap = process_event_stub; 460 if (tool->comm == NULL) 461 tool->comm = process_event_stub; 462 if (tool->fork == NULL) 463 tool->fork = process_event_stub; 464 if (tool->exit == NULL) 465 tool->exit = process_event_stub; 466 if (tool->lost == NULL) 467 tool->lost = perf_event__process_lost; 468 if (tool->read == NULL) 469 tool->read = process_event_sample_stub; 470 if (tool->throttle == NULL) 471 tool->throttle = process_event_stub; 472 if (tool->unthrottle == NULL) 473 tool->unthrottle = process_event_stub; 474 if (tool->attr == NULL) 475 tool->attr = process_event_synth_attr_stub; 476 if (tool->event_type == NULL) 477 tool->event_type = process_event_type_stub; 478 if (tool->tracing_data == NULL) 479 tool->tracing_data = process_event_synth_tracing_data_stub; 480 if (tool->build_id == NULL) 481 tool->build_id = process_finished_round_stub; 482 if (tool->finished_round == NULL) { 483 if (tool->ordered_samples) 484 tool->finished_round = process_finished_round; 485 else 486 tool->finished_round = process_finished_round_stub; 487 } 488 } 489 490 void mem_bswap_32(void *src, int byte_size) 491 { 492 u32 *m = src; 493 while (byte_size > 0) { 494 *m = bswap_32(*m); 495 byte_size -= sizeof(u32); 496 ++m; 497 } 498 } 499 500 void mem_bswap_64(void *src, int byte_size) 501 { 502 u64 *m = src; 503 504 while (byte_size > 0) { 505 *m = bswap_64(*m); 506 byte_size -= sizeof(u64); 507 ++m; 508 } 509 } 510 511 static void swap_sample_id_all(union perf_event *event, void *data) 512 { 513 void *end = (void *) event + event->header.size; 514 int size = end - data; 515 516 BUG_ON(size % sizeof(u64)); 517 mem_bswap_64(data, size); 518 } 519 520 static void perf_event__all64_swap(union perf_event *event, 521 bool sample_id_all __used) 522 { 523 struct perf_event_header *hdr = &event->header; 524 mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr)); 525 } 526 527 static void perf_event__comm_swap(union perf_event *event, bool sample_id_all) 528 { 529 event->comm.pid = bswap_32(event->comm.pid); 530 event->comm.tid = bswap_32(event->comm.tid); 531 532 if (sample_id_all) { 533 void *data = &event->comm.comm; 534 535 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64)); 536 swap_sample_id_all(event, data); 537 } 538 } 539 540 static void perf_event__mmap_swap(union perf_event *event, 541 bool sample_id_all) 542 { 543 event->mmap.pid = bswap_32(event->mmap.pid); 544 event->mmap.tid = bswap_32(event->mmap.tid); 545 event->mmap.start = bswap_64(event->mmap.start); 546 event->mmap.len = bswap_64(event->mmap.len); 547 event->mmap.pgoff = bswap_64(event->mmap.pgoff); 548 549 if (sample_id_all) { 550 void *data = &event->mmap.filename; 551 552 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64)); 553 swap_sample_id_all(event, data); 554 } 555 } 556 557 static void perf_event__task_swap(union perf_event *event, bool sample_id_all) 558 { 559 event->fork.pid = bswap_32(event->fork.pid); 560 event->fork.tid = bswap_32(event->fork.tid); 561 event->fork.ppid = bswap_32(event->fork.ppid); 562 event->fork.ptid = bswap_32(event->fork.ptid); 563 event->fork.time = bswap_64(event->fork.time); 564 565 if (sample_id_all) 566 swap_sample_id_all(event, &event->fork + 1); 567 } 568 569 static void perf_event__read_swap(union perf_event *event, bool sample_id_all) 570 { 571 event->read.pid = bswap_32(event->read.pid); 572 event->read.tid = bswap_32(event->read.tid); 573 event->read.value = bswap_64(event->read.value); 574 event->read.time_enabled = bswap_64(event->read.time_enabled); 575 event->read.time_running = bswap_64(event->read.time_running); 576 event->read.id = bswap_64(event->read.id); 577 578 if (sample_id_all) 579 swap_sample_id_all(event, &event->read + 1); 580 } 581 582 static u8 revbyte(u8 b) 583 { 584 int rev = (b >> 4) | ((b & 0xf) << 4); 585 rev = ((rev & 0xcc) >> 2) | ((rev & 0x33) << 2); 586 rev = ((rev & 0xaa) >> 1) | ((rev & 0x55) << 1); 587 return (u8) rev; 588 } 589 590 /* 591 * XXX this is hack in attempt to carry flags bitfield 592 * throught endian village. ABI says: 593 * 594 * Bit-fields are allocated from right to left (least to most significant) 595 * on little-endian implementations and from left to right (most to least 596 * significant) on big-endian implementations. 597 * 598 * The above seems to be byte specific, so we need to reverse each 599 * byte of the bitfield. 'Internet' also says this might be implementation 600 * specific and we probably need proper fix and carry perf_event_attr 601 * bitfield flags in separate data file FEAT_ section. Thought this seems 602 * to work for now. 603 */ 604 static void swap_bitfield(u8 *p, unsigned len) 605 { 606 unsigned i; 607 608 for (i = 0; i < len; i++) { 609 *p = revbyte(*p); 610 p++; 611 } 612 } 613 614 /* exported for swapping attributes in file header */ 615 void perf_event__attr_swap(struct perf_event_attr *attr) 616 { 617 attr->type = bswap_32(attr->type); 618 attr->size = bswap_32(attr->size); 619 attr->config = bswap_64(attr->config); 620 attr->sample_period = bswap_64(attr->sample_period); 621 attr->sample_type = bswap_64(attr->sample_type); 622 attr->read_format = bswap_64(attr->read_format); 623 attr->wakeup_events = bswap_32(attr->wakeup_events); 624 attr->bp_type = bswap_32(attr->bp_type); 625 attr->bp_addr = bswap_64(attr->bp_addr); 626 attr->bp_len = bswap_64(attr->bp_len); 627 628 swap_bitfield((u8 *) (&attr->read_format + 1), sizeof(u64)); 629 } 630 631 static void perf_event__hdr_attr_swap(union perf_event *event, 632 bool sample_id_all __used) 633 { 634 size_t size; 635 636 perf_event__attr_swap(&event->attr.attr); 637 638 size = event->header.size; 639 size -= (void *)&event->attr.id - (void *)event; 640 mem_bswap_64(event->attr.id, size); 641 } 642 643 static void perf_event__event_type_swap(union perf_event *event, 644 bool sample_id_all __used) 645 { 646 event->event_type.event_type.event_id = 647 bswap_64(event->event_type.event_type.event_id); 648 } 649 650 static void perf_event__tracing_data_swap(union perf_event *event, 651 bool sample_id_all __used) 652 { 653 event->tracing_data.size = bswap_32(event->tracing_data.size); 654 } 655 656 typedef void (*perf_event__swap_op)(union perf_event *event, 657 bool sample_id_all); 658 659 static perf_event__swap_op perf_event__swap_ops[] = { 660 [PERF_RECORD_MMAP] = perf_event__mmap_swap, 661 [PERF_RECORD_COMM] = perf_event__comm_swap, 662 [PERF_RECORD_FORK] = perf_event__task_swap, 663 [PERF_RECORD_EXIT] = perf_event__task_swap, 664 [PERF_RECORD_LOST] = perf_event__all64_swap, 665 [PERF_RECORD_READ] = perf_event__read_swap, 666 [PERF_RECORD_SAMPLE] = perf_event__all64_swap, 667 [PERF_RECORD_HEADER_ATTR] = perf_event__hdr_attr_swap, 668 [PERF_RECORD_HEADER_EVENT_TYPE] = perf_event__event_type_swap, 669 [PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap, 670 [PERF_RECORD_HEADER_BUILD_ID] = NULL, 671 [PERF_RECORD_HEADER_MAX] = NULL, 672 }; 673 674 struct sample_queue { 675 u64 timestamp; 676 u64 file_offset; 677 union perf_event *event; 678 struct list_head list; 679 }; 680 681 static void perf_session_free_sample_buffers(struct perf_session *session) 682 { 683 struct ordered_samples *os = &session->ordered_samples; 684 685 while (!list_empty(&os->to_free)) { 686 struct sample_queue *sq; 687 688 sq = list_entry(os->to_free.next, struct sample_queue, list); 689 list_del(&sq->list); 690 free(sq); 691 } 692 } 693 694 static int perf_session_deliver_event(struct perf_session *session, 695 union perf_event *event, 696 struct perf_sample *sample, 697 struct perf_tool *tool, 698 u64 file_offset); 699 700 static int flush_sample_queue(struct perf_session *s, 701 struct perf_tool *tool) 702 { 703 struct ordered_samples *os = &s->ordered_samples; 704 struct list_head *head = &os->samples; 705 struct sample_queue *tmp, *iter; 706 struct perf_sample sample; 707 u64 limit = os->next_flush; 708 u64 last_ts = os->last_sample ? os->last_sample->timestamp : 0ULL; 709 unsigned idx = 0, progress_next = os->nr_samples / 16; 710 int ret; 711 712 if (!tool->ordered_samples || !limit) 713 return 0; 714 715 list_for_each_entry_safe(iter, tmp, head, list) { 716 if (iter->timestamp > limit) 717 break; 718 719 ret = perf_evlist__parse_sample(s->evlist, iter->event, &sample, 720 s->header.needs_swap); 721 if (ret) 722 pr_err("Can't parse sample, err = %d\n", ret); 723 else { 724 ret = perf_session_deliver_event(s, iter->event, &sample, tool, 725 iter->file_offset); 726 if (ret) 727 return ret; 728 } 729 730 os->last_flush = iter->timestamp; 731 list_del(&iter->list); 732 list_add(&iter->list, &os->sample_cache); 733 if (++idx >= progress_next) { 734 progress_next += os->nr_samples / 16; 735 ui_progress__update(idx, os->nr_samples, 736 "Processing time ordered events..."); 737 } 738 } 739 740 if (list_empty(head)) { 741 os->last_sample = NULL; 742 } else if (last_ts <= limit) { 743 os->last_sample = 744 list_entry(head->prev, struct sample_queue, list); 745 } 746 747 os->nr_samples = 0; 748 749 return 0; 750 } 751 752 /* 753 * When perf record finishes a pass on every buffers, it records this pseudo 754 * event. 755 * We record the max timestamp t found in the pass n. 756 * Assuming these timestamps are monotonic across cpus, we know that if 757 * a buffer still has events with timestamps below t, they will be all 758 * available and then read in the pass n + 1. 759 * Hence when we start to read the pass n + 2, we can safely flush every 760 * events with timestamps below t. 761 * 762 * ============ PASS n ================= 763 * CPU 0 | CPU 1 764 * | 765 * cnt1 timestamps | cnt2 timestamps 766 * 1 | 2 767 * 2 | 3 768 * - | 4 <--- max recorded 769 * 770 * ============ PASS n + 1 ============== 771 * CPU 0 | CPU 1 772 * | 773 * cnt1 timestamps | cnt2 timestamps 774 * 3 | 5 775 * 4 | 6 776 * 5 | 7 <---- max recorded 777 * 778 * Flush every events below timestamp 4 779 * 780 * ============ PASS n + 2 ============== 781 * CPU 0 | CPU 1 782 * | 783 * cnt1 timestamps | cnt2 timestamps 784 * 6 | 8 785 * 7 | 9 786 * - | 10 787 * 788 * Flush every events below timestamp 7 789 * etc... 790 */ 791 static int process_finished_round(struct perf_tool *tool, 792 union perf_event *event __used, 793 struct perf_session *session) 794 { 795 int ret = flush_sample_queue(session, tool); 796 if (!ret) 797 session->ordered_samples.next_flush = session->ordered_samples.max_timestamp; 798 799 return ret; 800 } 801 802 /* The queue is ordered by time */ 803 static void __queue_event(struct sample_queue *new, struct perf_session *s) 804 { 805 struct ordered_samples *os = &s->ordered_samples; 806 struct sample_queue *sample = os->last_sample; 807 u64 timestamp = new->timestamp; 808 struct list_head *p; 809 810 ++os->nr_samples; 811 os->last_sample = new; 812 813 if (!sample) { 814 list_add(&new->list, &os->samples); 815 os->max_timestamp = timestamp; 816 return; 817 } 818 819 /* 820 * last_sample might point to some random place in the list as it's 821 * the last queued event. We expect that the new event is close to 822 * this. 823 */ 824 if (sample->timestamp <= timestamp) { 825 while (sample->timestamp <= timestamp) { 826 p = sample->list.next; 827 if (p == &os->samples) { 828 list_add_tail(&new->list, &os->samples); 829 os->max_timestamp = timestamp; 830 return; 831 } 832 sample = list_entry(p, struct sample_queue, list); 833 } 834 list_add_tail(&new->list, &sample->list); 835 } else { 836 while (sample->timestamp > timestamp) { 837 p = sample->list.prev; 838 if (p == &os->samples) { 839 list_add(&new->list, &os->samples); 840 return; 841 } 842 sample = list_entry(p, struct sample_queue, list); 843 } 844 list_add(&new->list, &sample->list); 845 } 846 } 847 848 #define MAX_SAMPLE_BUFFER (64 * 1024 / sizeof(struct sample_queue)) 849 850 static int perf_session_queue_event(struct perf_session *s, union perf_event *event, 851 struct perf_sample *sample, u64 file_offset) 852 { 853 struct ordered_samples *os = &s->ordered_samples; 854 struct list_head *sc = &os->sample_cache; 855 u64 timestamp = sample->time; 856 struct sample_queue *new; 857 858 if (!timestamp || timestamp == ~0ULL) 859 return -ETIME; 860 861 if (timestamp < s->ordered_samples.last_flush) { 862 printf("Warning: Timestamp below last timeslice flush\n"); 863 return -EINVAL; 864 } 865 866 if (!list_empty(sc)) { 867 new = list_entry(sc->next, struct sample_queue, list); 868 list_del(&new->list); 869 } else if (os->sample_buffer) { 870 new = os->sample_buffer + os->sample_buffer_idx; 871 if (++os->sample_buffer_idx == MAX_SAMPLE_BUFFER) 872 os->sample_buffer = NULL; 873 } else { 874 os->sample_buffer = malloc(MAX_SAMPLE_BUFFER * sizeof(*new)); 875 if (!os->sample_buffer) 876 return -ENOMEM; 877 list_add(&os->sample_buffer->list, &os->to_free); 878 os->sample_buffer_idx = 2; 879 new = os->sample_buffer + 1; 880 } 881 882 new->timestamp = timestamp; 883 new->file_offset = file_offset; 884 new->event = event; 885 886 __queue_event(new, s); 887 888 return 0; 889 } 890 891 static void callchain__printf(struct perf_sample *sample) 892 { 893 unsigned int i; 894 895 printf("... chain: nr:%" PRIu64 "\n", sample->callchain->nr); 896 897 for (i = 0; i < sample->callchain->nr; i++) 898 printf("..... %2d: %016" PRIx64 "\n", 899 i, sample->callchain->ips[i]); 900 } 901 902 static void branch_stack__printf(struct perf_sample *sample) 903 { 904 uint64_t i; 905 906 printf("... branch stack: nr:%" PRIu64 "\n", sample->branch_stack->nr); 907 908 for (i = 0; i < sample->branch_stack->nr; i++) 909 printf("..... %2"PRIu64": %016" PRIx64 " -> %016" PRIx64 "\n", 910 i, sample->branch_stack->entries[i].from, 911 sample->branch_stack->entries[i].to); 912 } 913 914 static void regs_dump__printf(u64 mask, u64 *regs) 915 { 916 unsigned rid, i = 0; 917 918 for_each_set_bit(rid, (unsigned long *) &mask, sizeof(mask) * 8) { 919 u64 val = regs[i++]; 920 921 printf(".... %-5s 0x%" PRIx64 "\n", 922 perf_reg_name(rid), val); 923 } 924 } 925 926 static void regs_user__printf(struct perf_sample *sample, u64 mask) 927 { 928 struct regs_dump *user_regs = &sample->user_regs; 929 930 if (user_regs->regs) { 931 printf("... user regs: mask 0x%" PRIx64 "\n", mask); 932 regs_dump__printf(mask, user_regs->regs); 933 } 934 } 935 936 static void stack_user__printf(struct stack_dump *dump) 937 { 938 printf("... ustack: size %" PRIu64 ", offset 0x%x\n", 939 dump->size, dump->offset); 940 } 941 942 static void perf_session__print_tstamp(struct perf_session *session, 943 union perf_event *event, 944 struct perf_sample *sample) 945 { 946 u64 sample_type = perf_evlist__sample_type(session->evlist); 947 948 if (event->header.type != PERF_RECORD_SAMPLE && 949 !perf_evlist__sample_id_all(session->evlist)) { 950 fputs("-1 -1 ", stdout); 951 return; 952 } 953 954 if ((sample_type & PERF_SAMPLE_CPU)) 955 printf("%u ", sample->cpu); 956 957 if (sample_type & PERF_SAMPLE_TIME) 958 printf("%" PRIu64 " ", sample->time); 959 } 960 961 static void dump_event(struct perf_session *session, union perf_event *event, 962 u64 file_offset, struct perf_sample *sample) 963 { 964 if (!dump_trace) 965 return; 966 967 printf("\n%#" PRIx64 " [%#x]: event: %d\n", 968 file_offset, event->header.size, event->header.type); 969 970 trace_event(event); 971 972 if (sample) 973 perf_session__print_tstamp(session, event, sample); 974 975 printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset, 976 event->header.size, perf_event__name(event->header.type)); 977 } 978 979 static void dump_sample(struct perf_evsel *evsel, union perf_event *event, 980 struct perf_sample *sample) 981 { 982 u64 sample_type; 983 984 if (!dump_trace) 985 return; 986 987 printf("(IP, %d): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n", 988 event->header.misc, sample->pid, sample->tid, sample->ip, 989 sample->period, sample->addr); 990 991 sample_type = evsel->attr.sample_type; 992 993 if (sample_type & PERF_SAMPLE_CALLCHAIN) 994 callchain__printf(sample); 995 996 if (sample_type & PERF_SAMPLE_BRANCH_STACK) 997 branch_stack__printf(sample); 998 999 if (sample_type & PERF_SAMPLE_REGS_USER) 1000 regs_user__printf(sample, evsel->attr.sample_regs_user); 1001 1002 if (sample_type & PERF_SAMPLE_STACK_USER) 1003 stack_user__printf(&sample->user_stack); 1004 } 1005 1006 static struct machine * 1007 perf_session__find_machine_for_cpumode(struct perf_session *session, 1008 union perf_event *event) 1009 { 1010 const u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; 1011 1012 if (perf_guest && 1013 ((cpumode == PERF_RECORD_MISC_GUEST_KERNEL) || 1014 (cpumode == PERF_RECORD_MISC_GUEST_USER))) { 1015 u32 pid; 1016 1017 if (event->header.type == PERF_RECORD_MMAP) 1018 pid = event->mmap.pid; 1019 else 1020 pid = event->ip.pid; 1021 1022 return perf_session__findnew_machine(session, pid); 1023 } 1024 1025 return perf_session__find_host_machine(session); 1026 } 1027 1028 static int perf_session_deliver_event(struct perf_session *session, 1029 union perf_event *event, 1030 struct perf_sample *sample, 1031 struct perf_tool *tool, 1032 u64 file_offset) 1033 { 1034 struct perf_evsel *evsel; 1035 struct machine *machine; 1036 1037 dump_event(session, event, file_offset, sample); 1038 1039 evsel = perf_evlist__id2evsel(session->evlist, sample->id); 1040 if (evsel != NULL && event->header.type != PERF_RECORD_SAMPLE) { 1041 /* 1042 * XXX We're leaving PERF_RECORD_SAMPLE unnacounted here 1043 * because the tools right now may apply filters, discarding 1044 * some of the samples. For consistency, in the future we 1045 * should have something like nr_filtered_samples and remove 1046 * the sample->period from total_sample_period, etc, KISS for 1047 * now tho. 1048 * 1049 * Also testing against NULL allows us to handle files without 1050 * attr.sample_id_all and/or without PERF_SAMPLE_ID. In the 1051 * future probably it'll be a good idea to restrict event 1052 * processing via perf_session to files with both set. 1053 */ 1054 hists__inc_nr_events(&evsel->hists, event->header.type); 1055 } 1056 1057 machine = perf_session__find_machine_for_cpumode(session, event); 1058 1059 switch (event->header.type) { 1060 case PERF_RECORD_SAMPLE: 1061 dump_sample(evsel, event, sample); 1062 if (evsel == NULL) { 1063 ++session->hists.stats.nr_unknown_id; 1064 return 0; 1065 } 1066 if (machine == NULL) { 1067 ++session->hists.stats.nr_unprocessable_samples; 1068 return 0; 1069 } 1070 return tool->sample(tool, event, sample, evsel, machine); 1071 case PERF_RECORD_MMAP: 1072 return tool->mmap(tool, event, sample, machine); 1073 case PERF_RECORD_COMM: 1074 return tool->comm(tool, event, sample, machine); 1075 case PERF_RECORD_FORK: 1076 return tool->fork(tool, event, sample, machine); 1077 case PERF_RECORD_EXIT: 1078 return tool->exit(tool, event, sample, machine); 1079 case PERF_RECORD_LOST: 1080 if (tool->lost == perf_event__process_lost) 1081 session->hists.stats.total_lost += event->lost.lost; 1082 return tool->lost(tool, event, sample, machine); 1083 case PERF_RECORD_READ: 1084 return tool->read(tool, event, sample, evsel, machine); 1085 case PERF_RECORD_THROTTLE: 1086 return tool->throttle(tool, event, sample, machine); 1087 case PERF_RECORD_UNTHROTTLE: 1088 return tool->unthrottle(tool, event, sample, machine); 1089 default: 1090 ++session->hists.stats.nr_unknown_events; 1091 return -1; 1092 } 1093 } 1094 1095 static int perf_session__preprocess_sample(struct perf_session *session, 1096 union perf_event *event, struct perf_sample *sample) 1097 { 1098 if (event->header.type != PERF_RECORD_SAMPLE || 1099 !(perf_evlist__sample_type(session->evlist) & PERF_SAMPLE_CALLCHAIN)) 1100 return 0; 1101 1102 if (!ip_callchain__valid(sample->callchain, event)) { 1103 pr_debug("call-chain problem with event, skipping it.\n"); 1104 ++session->hists.stats.nr_invalid_chains; 1105 session->hists.stats.total_invalid_chains += sample->period; 1106 return -EINVAL; 1107 } 1108 return 0; 1109 } 1110 1111 static int perf_session__process_user_event(struct perf_session *session, union perf_event *event, 1112 struct perf_tool *tool, u64 file_offset) 1113 { 1114 int err; 1115 1116 dump_event(session, event, file_offset, NULL); 1117 1118 /* These events are processed right away */ 1119 switch (event->header.type) { 1120 case PERF_RECORD_HEADER_ATTR: 1121 err = tool->attr(event, &session->evlist); 1122 if (err == 0) 1123 perf_session__set_id_hdr_size(session); 1124 return err; 1125 case PERF_RECORD_HEADER_EVENT_TYPE: 1126 return tool->event_type(tool, event); 1127 case PERF_RECORD_HEADER_TRACING_DATA: 1128 /* setup for reading amidst mmap */ 1129 lseek(session->fd, file_offset, SEEK_SET); 1130 return tool->tracing_data(event, session); 1131 case PERF_RECORD_HEADER_BUILD_ID: 1132 return tool->build_id(tool, event, session); 1133 case PERF_RECORD_FINISHED_ROUND: 1134 return tool->finished_round(tool, event, session); 1135 default: 1136 return -EINVAL; 1137 } 1138 } 1139 1140 static void event_swap(union perf_event *event, bool sample_id_all) 1141 { 1142 perf_event__swap_op swap; 1143 1144 swap = perf_event__swap_ops[event->header.type]; 1145 if (swap) 1146 swap(event, sample_id_all); 1147 } 1148 1149 static int perf_session__process_event(struct perf_session *session, 1150 union perf_event *event, 1151 struct perf_tool *tool, 1152 u64 file_offset) 1153 { 1154 struct perf_sample sample; 1155 int ret; 1156 1157 if (session->header.needs_swap) 1158 event_swap(event, perf_evlist__sample_id_all(session->evlist)); 1159 1160 if (event->header.type >= PERF_RECORD_HEADER_MAX) 1161 return -EINVAL; 1162 1163 hists__inc_nr_events(&session->hists, event->header.type); 1164 1165 if (event->header.type >= PERF_RECORD_USER_TYPE_START) 1166 return perf_session__process_user_event(session, event, tool, file_offset); 1167 1168 /* 1169 * For all kernel events we get the sample data 1170 */ 1171 ret = perf_evlist__parse_sample(session->evlist, event, &sample, 1172 session->header.needs_swap); 1173 if (ret) 1174 return ret; 1175 1176 /* Preprocess sample records - precheck callchains */ 1177 if (perf_session__preprocess_sample(session, event, &sample)) 1178 return 0; 1179 1180 if (tool->ordered_samples) { 1181 ret = perf_session_queue_event(session, event, &sample, 1182 file_offset); 1183 if (ret != -ETIME) 1184 return ret; 1185 } 1186 1187 return perf_session_deliver_event(session, event, &sample, tool, 1188 file_offset); 1189 } 1190 1191 void perf_event_header__bswap(struct perf_event_header *self) 1192 { 1193 self->type = bswap_32(self->type); 1194 self->misc = bswap_16(self->misc); 1195 self->size = bswap_16(self->size); 1196 } 1197 1198 struct thread *perf_session__findnew(struct perf_session *session, pid_t pid) 1199 { 1200 return machine__findnew_thread(&session->host_machine, pid); 1201 } 1202 1203 static struct thread *perf_session__register_idle_thread(struct perf_session *self) 1204 { 1205 struct thread *thread = perf_session__findnew(self, 0); 1206 1207 if (thread == NULL || thread__set_comm(thread, "swapper")) { 1208 pr_err("problem inserting idle task.\n"); 1209 thread = NULL; 1210 } 1211 1212 return thread; 1213 } 1214 1215 static void perf_session__warn_about_errors(const struct perf_session *session, 1216 const struct perf_tool *tool) 1217 { 1218 if (tool->lost == perf_event__process_lost && 1219 session->hists.stats.nr_events[PERF_RECORD_LOST] != 0) { 1220 ui__warning("Processed %d events and lost %d chunks!\n\n" 1221 "Check IO/CPU overload!\n\n", 1222 session->hists.stats.nr_events[0], 1223 session->hists.stats.nr_events[PERF_RECORD_LOST]); 1224 } 1225 1226 if (session->hists.stats.nr_unknown_events != 0) { 1227 ui__warning("Found %u unknown events!\n\n" 1228 "Is this an older tool processing a perf.data " 1229 "file generated by a more recent tool?\n\n" 1230 "If that is not the case, consider " 1231 "reporting to linux-kernel@vger.kernel.org.\n\n", 1232 session->hists.stats.nr_unknown_events); 1233 } 1234 1235 if (session->hists.stats.nr_unknown_id != 0) { 1236 ui__warning("%u samples with id not present in the header\n", 1237 session->hists.stats.nr_unknown_id); 1238 } 1239 1240 if (session->hists.stats.nr_invalid_chains != 0) { 1241 ui__warning("Found invalid callchains!\n\n" 1242 "%u out of %u events were discarded for this reason.\n\n" 1243 "Consider reporting to linux-kernel@vger.kernel.org.\n\n", 1244 session->hists.stats.nr_invalid_chains, 1245 session->hists.stats.nr_events[PERF_RECORD_SAMPLE]); 1246 } 1247 1248 if (session->hists.stats.nr_unprocessable_samples != 0) { 1249 ui__warning("%u unprocessable samples recorded.\n" 1250 "Do you have a KVM guest running and not using 'perf kvm'?\n", 1251 session->hists.stats.nr_unprocessable_samples); 1252 } 1253 } 1254 1255 #define session_done() (*(volatile int *)(&session_done)) 1256 volatile int session_done; 1257 1258 static int __perf_session__process_pipe_events(struct perf_session *self, 1259 struct perf_tool *tool) 1260 { 1261 union perf_event *event; 1262 uint32_t size, cur_size = 0; 1263 void *buf = NULL; 1264 int skip = 0; 1265 u64 head; 1266 int err; 1267 void *p; 1268 1269 perf_tool__fill_defaults(tool); 1270 1271 head = 0; 1272 cur_size = sizeof(union perf_event); 1273 1274 buf = malloc(cur_size); 1275 if (!buf) 1276 return -errno; 1277 more: 1278 event = buf; 1279 err = readn(self->fd, event, sizeof(struct perf_event_header)); 1280 if (err <= 0) { 1281 if (err == 0) 1282 goto done; 1283 1284 pr_err("failed to read event header\n"); 1285 goto out_err; 1286 } 1287 1288 if (self->header.needs_swap) 1289 perf_event_header__bswap(&event->header); 1290 1291 size = event->header.size; 1292 if (size == 0) 1293 size = 8; 1294 1295 if (size > cur_size) { 1296 void *new = realloc(buf, size); 1297 if (!new) { 1298 pr_err("failed to allocate memory to read event\n"); 1299 goto out_err; 1300 } 1301 buf = new; 1302 cur_size = size; 1303 event = buf; 1304 } 1305 p = event; 1306 p += sizeof(struct perf_event_header); 1307 1308 if (size - sizeof(struct perf_event_header)) { 1309 err = readn(self->fd, p, size - sizeof(struct perf_event_header)); 1310 if (err <= 0) { 1311 if (err == 0) { 1312 pr_err("unexpected end of event stream\n"); 1313 goto done; 1314 } 1315 1316 pr_err("failed to read event data\n"); 1317 goto out_err; 1318 } 1319 } 1320 1321 if ((skip = perf_session__process_event(self, event, tool, head)) < 0) { 1322 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n", 1323 head, event->header.size, event->header.type); 1324 err = -EINVAL; 1325 goto out_err; 1326 } 1327 1328 head += size; 1329 1330 if (skip > 0) 1331 head += skip; 1332 1333 if (!session_done()) 1334 goto more; 1335 done: 1336 err = 0; 1337 out_err: 1338 free(buf); 1339 perf_session__warn_about_errors(self, tool); 1340 perf_session_free_sample_buffers(self); 1341 return err; 1342 } 1343 1344 static union perf_event * 1345 fetch_mmaped_event(struct perf_session *session, 1346 u64 head, size_t mmap_size, char *buf) 1347 { 1348 union perf_event *event; 1349 1350 /* 1351 * Ensure we have enough space remaining to read 1352 * the size of the event in the headers. 1353 */ 1354 if (head + sizeof(event->header) > mmap_size) 1355 return NULL; 1356 1357 event = (union perf_event *)(buf + head); 1358 1359 if (session->header.needs_swap) 1360 perf_event_header__bswap(&event->header); 1361 1362 if (head + event->header.size > mmap_size) 1363 return NULL; 1364 1365 return event; 1366 } 1367 1368 int __perf_session__process_events(struct perf_session *session, 1369 u64 data_offset, u64 data_size, 1370 u64 file_size, struct perf_tool *tool) 1371 { 1372 u64 head, page_offset, file_offset, file_pos, progress_next; 1373 int err, mmap_prot, mmap_flags, map_idx = 0; 1374 size_t page_size, mmap_size; 1375 char *buf, *mmaps[8]; 1376 union perf_event *event; 1377 uint32_t size; 1378 1379 perf_tool__fill_defaults(tool); 1380 1381 page_size = sysconf(_SC_PAGESIZE); 1382 1383 page_offset = page_size * (data_offset / page_size); 1384 file_offset = page_offset; 1385 head = data_offset - page_offset; 1386 1387 if (data_offset + data_size < file_size) 1388 file_size = data_offset + data_size; 1389 1390 progress_next = file_size / 16; 1391 1392 mmap_size = session->mmap_window; 1393 if (mmap_size > file_size) 1394 mmap_size = file_size; 1395 1396 memset(mmaps, 0, sizeof(mmaps)); 1397 1398 mmap_prot = PROT_READ; 1399 mmap_flags = MAP_SHARED; 1400 1401 if (session->header.needs_swap) { 1402 mmap_prot |= PROT_WRITE; 1403 mmap_flags = MAP_PRIVATE; 1404 } 1405 remap: 1406 buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, session->fd, 1407 file_offset); 1408 if (buf == MAP_FAILED) { 1409 pr_err("failed to mmap file\n"); 1410 err = -errno; 1411 goto out_err; 1412 } 1413 mmaps[map_idx] = buf; 1414 map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1); 1415 file_pos = file_offset + head; 1416 1417 more: 1418 event = fetch_mmaped_event(session, head, mmap_size, buf); 1419 if (!event) { 1420 if (mmaps[map_idx]) { 1421 munmap(mmaps[map_idx], mmap_size); 1422 mmaps[map_idx] = NULL; 1423 } 1424 1425 page_offset = page_size * (head / page_size); 1426 file_offset += page_offset; 1427 head -= page_offset; 1428 goto remap; 1429 } 1430 1431 size = event->header.size; 1432 1433 if (size == 0 || 1434 perf_session__process_event(session, event, tool, file_pos) < 0) { 1435 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n", 1436 file_offset + head, event->header.size, 1437 event->header.type); 1438 err = -EINVAL; 1439 goto out_err; 1440 } 1441 1442 head += size; 1443 file_pos += size; 1444 1445 if (file_pos >= progress_next) { 1446 progress_next += file_size / 16; 1447 ui_progress__update(file_pos, file_size, 1448 "Processing events..."); 1449 } 1450 1451 if (file_pos < file_size) 1452 goto more; 1453 1454 err = 0; 1455 /* do the final flush for ordered samples */ 1456 session->ordered_samples.next_flush = ULLONG_MAX; 1457 err = flush_sample_queue(session, tool); 1458 out_err: 1459 perf_session__warn_about_errors(session, tool); 1460 perf_session_free_sample_buffers(session); 1461 return err; 1462 } 1463 1464 int perf_session__process_events(struct perf_session *self, 1465 struct perf_tool *tool) 1466 { 1467 int err; 1468 1469 if (perf_session__register_idle_thread(self) == NULL) 1470 return -ENOMEM; 1471 1472 if (!self->fd_pipe) 1473 err = __perf_session__process_events(self, 1474 self->header.data_offset, 1475 self->header.data_size, 1476 self->size, tool); 1477 else 1478 err = __perf_session__process_pipe_events(self, tool); 1479 1480 return err; 1481 } 1482 1483 bool perf_session__has_traces(struct perf_session *session, const char *msg) 1484 { 1485 if (!(perf_evlist__sample_type(session->evlist) & PERF_SAMPLE_RAW)) { 1486 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg); 1487 return false; 1488 } 1489 1490 return true; 1491 } 1492 1493 int maps__set_kallsyms_ref_reloc_sym(struct map **maps, 1494 const char *symbol_name, u64 addr) 1495 { 1496 char *bracket; 1497 enum map_type i; 1498 struct ref_reloc_sym *ref; 1499 1500 ref = zalloc(sizeof(struct ref_reloc_sym)); 1501 if (ref == NULL) 1502 return -ENOMEM; 1503 1504 ref->name = strdup(symbol_name); 1505 if (ref->name == NULL) { 1506 free(ref); 1507 return -ENOMEM; 1508 } 1509 1510 bracket = strchr(ref->name, ']'); 1511 if (bracket) 1512 *bracket = '\0'; 1513 1514 ref->addr = addr; 1515 1516 for (i = 0; i < MAP__NR_TYPES; ++i) { 1517 struct kmap *kmap = map__kmap(maps[i]); 1518 kmap->ref_reloc_sym = ref; 1519 } 1520 1521 return 0; 1522 } 1523 1524 size_t perf_session__fprintf_dsos(struct perf_session *self, FILE *fp) 1525 { 1526 return __dsos__fprintf(&self->host_machine.kernel_dsos, fp) + 1527 __dsos__fprintf(&self->host_machine.user_dsos, fp) + 1528 machines__fprintf_dsos(&self->machines, fp); 1529 } 1530 1531 size_t perf_session__fprintf_dsos_buildid(struct perf_session *self, FILE *fp, 1532 bool with_hits) 1533 { 1534 size_t ret = machine__fprintf_dsos_buildid(&self->host_machine, fp, with_hits); 1535 return ret + machines__fprintf_dsos_buildid(&self->machines, fp, with_hits); 1536 } 1537 1538 size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp) 1539 { 1540 struct perf_evsel *pos; 1541 size_t ret = fprintf(fp, "Aggregated stats:\n"); 1542 1543 ret += hists__fprintf_nr_events(&session->hists, fp); 1544 1545 list_for_each_entry(pos, &session->evlist->entries, node) { 1546 ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos)); 1547 ret += hists__fprintf_nr_events(&pos->hists, fp); 1548 } 1549 1550 return ret; 1551 } 1552 1553 size_t perf_session__fprintf(struct perf_session *session, FILE *fp) 1554 { 1555 /* 1556 * FIXME: Here we have to actually print all the machines in this 1557 * session, not just the host... 1558 */ 1559 return machine__fprintf(&session->host_machine, fp); 1560 } 1561 1562 void perf_session__remove_thread(struct perf_session *session, 1563 struct thread *th) 1564 { 1565 /* 1566 * FIXME: This one makes no sense, we need to remove the thread from 1567 * the machine it belongs to, perf_session can have many machines, so 1568 * doing it always on ->host_machine is wrong. Fix when auditing all 1569 * the 'perf kvm' code. 1570 */ 1571 machine__remove_thread(&session->host_machine, th); 1572 } 1573 1574 struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session, 1575 unsigned int type) 1576 { 1577 struct perf_evsel *pos; 1578 1579 list_for_each_entry(pos, &session->evlist->entries, node) { 1580 if (pos->attr.type == type) 1581 return pos; 1582 } 1583 return NULL; 1584 } 1585 1586 void perf_evsel__print_ip(struct perf_evsel *evsel, union perf_event *event, 1587 struct perf_sample *sample, struct machine *machine, 1588 int print_sym, int print_dso, int print_symoffset) 1589 { 1590 struct addr_location al; 1591 struct callchain_cursor_node *node; 1592 1593 if (perf_event__preprocess_sample(event, machine, &al, sample, 1594 NULL) < 0) { 1595 error("problem processing %d event, skipping it.\n", 1596 event->header.type); 1597 return; 1598 } 1599 1600 if (symbol_conf.use_callchain && sample->callchain) { 1601 1602 1603 if (machine__resolve_callchain(machine, evsel, al.thread, 1604 sample, NULL) != 0) { 1605 if (verbose) 1606 error("Failed to resolve callchain. Skipping\n"); 1607 return; 1608 } 1609 callchain_cursor_commit(&callchain_cursor); 1610 1611 while (1) { 1612 node = callchain_cursor_current(&callchain_cursor); 1613 if (!node) 1614 break; 1615 1616 printf("\t%16" PRIx64, node->ip); 1617 if (print_sym) { 1618 printf(" "); 1619 symbol__fprintf_symname(node->sym, stdout); 1620 } 1621 if (print_dso) { 1622 printf(" ("); 1623 map__fprintf_dsoname(node->map, stdout); 1624 printf(")"); 1625 } 1626 printf("\n"); 1627 1628 callchain_cursor_advance(&callchain_cursor); 1629 } 1630 1631 } else { 1632 printf("%16" PRIx64, sample->ip); 1633 if (print_sym) { 1634 printf(" "); 1635 if (print_symoffset) 1636 symbol__fprintf_symname_offs(al.sym, &al, 1637 stdout); 1638 else 1639 symbol__fprintf_symname(al.sym, stdout); 1640 } 1641 1642 if (print_dso) { 1643 printf(" ("); 1644 map__fprintf_dsoname(al.map, stdout); 1645 printf(")"); 1646 } 1647 } 1648 } 1649 1650 int perf_session__cpu_bitmap(struct perf_session *session, 1651 const char *cpu_list, unsigned long *cpu_bitmap) 1652 { 1653 int i; 1654 struct cpu_map *map; 1655 1656 for (i = 0; i < PERF_TYPE_MAX; ++i) { 1657 struct perf_evsel *evsel; 1658 1659 evsel = perf_session__find_first_evtype(session, i); 1660 if (!evsel) 1661 continue; 1662 1663 if (!(evsel->attr.sample_type & PERF_SAMPLE_CPU)) { 1664 pr_err("File does not contain CPU events. " 1665 "Remove -c option to proceed.\n"); 1666 return -1; 1667 } 1668 } 1669 1670 map = cpu_map__new(cpu_list); 1671 if (map == NULL) { 1672 pr_err("Invalid cpu_list\n"); 1673 return -1; 1674 } 1675 1676 for (i = 0; i < map->nr; i++) { 1677 int cpu = map->map[i]; 1678 1679 if (cpu >= MAX_NR_CPUS) { 1680 pr_err("Requested CPU %d too large. " 1681 "Consider raising MAX_NR_CPUS\n", cpu); 1682 return -1; 1683 } 1684 1685 set_bit(cpu, cpu_bitmap); 1686 } 1687 1688 return 0; 1689 } 1690 1691 void perf_session__fprintf_info(struct perf_session *session, FILE *fp, 1692 bool full) 1693 { 1694 struct stat st; 1695 int ret; 1696 1697 if (session == NULL || fp == NULL) 1698 return; 1699 1700 ret = fstat(session->fd, &st); 1701 if (ret == -1) 1702 return; 1703 1704 fprintf(fp, "# ========\n"); 1705 fprintf(fp, "# captured on: %s", ctime(&st.st_ctime)); 1706 perf_header__fprintf_info(session, fp, full); 1707 fprintf(fp, "# ========\n#\n"); 1708 } 1709 1710 1711 int __perf_session__set_tracepoints_handlers(struct perf_session *session, 1712 const struct perf_evsel_str_handler *assocs, 1713 size_t nr_assocs) 1714 { 1715 struct perf_evlist *evlist = session->evlist; 1716 struct event_format *format; 1717 struct perf_evsel *evsel; 1718 char *tracepoint, *name; 1719 size_t i; 1720 int err; 1721 1722 for (i = 0; i < nr_assocs; i++) { 1723 err = -ENOMEM; 1724 tracepoint = strdup(assocs[i].name); 1725 if (tracepoint == NULL) 1726 goto out; 1727 1728 err = -ENOENT; 1729 name = strchr(tracepoint, ':'); 1730 if (name == NULL) 1731 goto out_free; 1732 1733 *name++ = '\0'; 1734 format = pevent_find_event_by_name(session->pevent, 1735 tracepoint, name); 1736 if (format == NULL) { 1737 /* 1738 * Adding a handler for an event not in the session, 1739 * just ignore it. 1740 */ 1741 goto next; 1742 } 1743 1744 evsel = perf_evlist__find_tracepoint_by_id(evlist, format->id); 1745 if (evsel == NULL) 1746 goto next; 1747 1748 err = -EEXIST; 1749 if (evsel->handler.func != NULL) 1750 goto out_free; 1751 evsel->handler.func = assocs[i].handler; 1752 next: 1753 free(tracepoint); 1754 } 1755 1756 err = 0; 1757 out: 1758 return err; 1759 1760 out_free: 1761 free(tracepoint); 1762 goto out; 1763 } 1764