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