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