1 #define _FILE_OFFSET_BITS 64
3 #include <linux/kernel.h>
18 static int perf_session__open(struct perf_session *self, bool force)
20 struct stat input_stat;
22 if (!strcmp(self->filename, "-")) {
24 self->fd = STDIN_FILENO;
26 if (perf_session__read_header(self, self->fd) < 0)
27 pr_err("incompatible file format (rerun with -v to learn more)");
32 self->fd = open(self->filename, O_RDONLY);
36 pr_err("failed to open %s: %s", self->filename, strerror(err));
37 if (err == ENOENT && !strcmp(self->filename, "perf.data"))
38 pr_err(" (try 'perf record' first)");
43 if (fstat(self->fd, &input_stat) < 0)
46 if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
47 pr_err("file %s not owned by current user or root\n",
52 if (!input_stat.st_size) {
53 pr_info("zero-sized file (%s), nothing to do!\n",
58 if (perf_session__read_header(self, self->fd) < 0) {
59 pr_err("incompatible file format (rerun with -v to learn more)");
63 if (!perf_evlist__valid_sample_type(self->evlist)) {
64 pr_err("non matching sample_type");
68 if (!perf_evlist__valid_sample_id_all(self->evlist)) {
69 pr_err("non matching sample_id_all");
73 self->size = input_stat.st_size;
82 void perf_session__update_sample_type(struct perf_session *self)
84 self->sample_type = perf_evlist__sample_type(self->evlist);
85 self->sample_size = __perf_evsel__sample_size(self->sample_type);
86 self->sample_id_all = perf_evlist__sample_id_all(self->evlist);
87 self->id_hdr_size = perf_evlist__id_hdr_size(self->evlist);
88 self->host_machine.id_hdr_size = self->id_hdr_size;
91 int perf_session__create_kernel_maps(struct perf_session *self)
93 int ret = machine__create_kernel_maps(&self->host_machine);
96 ret = machines__create_guest_kernel_maps(&self->machines);
100 static void perf_session__destroy_kernel_maps(struct perf_session *self)
102 machine__destroy_kernel_maps(&self->host_machine);
103 machines__destroy_guest_kernel_maps(&self->machines);
106 struct perf_session *perf_session__new(const char *filename, int mode,
107 bool force, bool repipe,
108 struct perf_tool *tool)
110 struct perf_session *self;
114 if (!filename || !strlen(filename)) {
115 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
118 filename = "perf.data";
121 len = strlen(filename);
122 self = zalloc(sizeof(*self) + len);
127 memcpy(self->filename, filename, len);
129 * On 64bit we can mmap the data file in one go. No need for tiny mmap
130 * slices. On 32bit we use 32MB.
132 #if BITS_PER_LONG == 64
133 self->mmap_window = ULLONG_MAX;
135 self->mmap_window = 32 * 1024 * 1024ULL;
137 self->machines = RB_ROOT;
138 self->repipe = repipe;
139 INIT_LIST_HEAD(&self->ordered_samples.samples);
140 INIT_LIST_HEAD(&self->ordered_samples.sample_cache);
141 INIT_LIST_HEAD(&self->ordered_samples.to_free);
142 machine__init(&self->host_machine, "", HOST_KERNEL_ID);
143 hists__init(&self->hists);
145 if (mode == O_RDONLY) {
146 if (perf_session__open(self, force) < 0)
148 perf_session__update_sample_type(self);
149 } else if (mode == O_WRONLY) {
151 * In O_RDONLY mode this will be performed when reading the
152 * kernel MMAP event, in perf_event__process_mmap().
154 if (perf_session__create_kernel_maps(self) < 0)
158 if (tool && tool->ordering_requires_timestamps &&
159 tool->ordered_samples && !self->sample_id_all) {
160 dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n");
161 tool->ordered_samples = false;
167 perf_session__delete(self);
171 static void machine__delete_dead_threads(struct machine *machine)
173 struct thread *n, *t;
175 list_for_each_entry_safe(t, n, &machine->dead_threads, node) {
181 static void perf_session__delete_dead_threads(struct perf_session *session)
183 machine__delete_dead_threads(&session->host_machine);
186 static void machine__delete_threads(struct machine *self)
188 struct rb_node *nd = rb_first(&self->threads);
191 struct thread *t = rb_entry(nd, struct thread, rb_node);
193 rb_erase(&t->rb_node, &self->threads);
199 static void perf_session__delete_threads(struct perf_session *session)
201 machine__delete_threads(&session->host_machine);
204 void perf_session__delete(struct perf_session *self)
206 perf_session__destroy_kernel_maps(self);
207 perf_session__delete_dead_threads(self);
208 perf_session__delete_threads(self);
209 machine__exit(&self->host_machine);
214 void machine__remove_thread(struct machine *self, struct thread *th)
216 self->last_match = NULL;
217 rb_erase(&th->rb_node, &self->threads);
219 * We may have references to this thread, for instance in some hist_entry
220 * instances, so just move them to a separate list.
222 list_add_tail(&th->node, &self->dead_threads);
225 static bool symbol__match_parent_regex(struct symbol *sym)
227 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
233 static const u8 cpumodes[] = {
234 PERF_RECORD_MISC_USER,
235 PERF_RECORD_MISC_KERNEL,
236 PERF_RECORD_MISC_GUEST_USER,
237 PERF_RECORD_MISC_GUEST_KERNEL
239 #define NCPUMODES (sizeof(cpumodes)/sizeof(u8))
241 static void ip__resolve_ams(struct machine *self, struct thread *thread,
242 struct addr_map_symbol *ams,
245 struct addr_location al;
249 memset(&al, 0, sizeof(al));
251 for (i = 0; i < NCPUMODES; i++) {
254 * We cannot use the header.misc hint to determine whether a
255 * branch stack address is user, kernel, guest, hypervisor.
256 * Branches may straddle the kernel/user/hypervisor boundaries.
257 * Thus, we have to try consecutively until we find a match
258 * or else, the symbol is unknown
260 thread__find_addr_location(thread, self, m, MAP__FUNCTION,
267 ams->al_addr = al.addr;
272 struct branch_info *machine__resolve_bstack(struct machine *self,
274 struct branch_stack *bs)
276 struct branch_info *bi;
279 bi = calloc(bs->nr, sizeof(struct branch_info));
283 for (i = 0; i < bs->nr; i++) {
284 ip__resolve_ams(self, thr, &bi[i].to, bs->entries[i].to);
285 ip__resolve_ams(self, thr, &bi[i].from, bs->entries[i].from);
286 bi[i].flags = bs->entries[i].flags;
291 int machine__resolve_callchain(struct machine *self,
292 struct perf_evsel *evsel __used,
293 struct thread *thread,
294 struct ip_callchain *chain,
295 struct symbol **parent)
297 u8 cpumode = PERF_RECORD_MISC_USER;
301 callchain_cursor_reset(&callchain_cursor);
303 if (chain->nr > PERF_MAX_STACK_DEPTH) {
304 pr_warning("corrupted callchain. skipping...\n");
308 for (i = 0; i < chain->nr; i++) {
310 struct addr_location al;
312 if (callchain_param.order == ORDER_CALLEE)
315 ip = chain->ips[chain->nr - i - 1];
317 if (ip >= PERF_CONTEXT_MAX) {
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;
326 pr_debug("invalid callchain context: "
327 "%"PRId64"\n", (s64) ip);
329 * It seems the callchain is corrupted.
332 callchain_cursor_reset(&callchain_cursor);
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))
345 if (!symbol_conf.use_callchain)
349 err = callchain_cursor_append(&callchain_cursor,
358 static int process_event_synth_tracing_data_stub(union perf_event *event __used,
359 struct perf_session *session __used)
361 dump_printf(": unhandled!\n");
365 static int process_event_synth_attr_stub(union perf_event *event __used,
366 struct perf_evlist **pevlist __used)
368 dump_printf(": unhandled!\n");
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)
378 dump_printf(": unhandled!\n");
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)
387 dump_printf(": unhandled!\n");
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)
395 dump_printf(": unhandled!\n");
399 static int process_event_type_stub(struct perf_tool *tool __used,
400 union perf_event *event __used)
402 dump_printf(": unhandled!\n");
406 static int process_finished_round(struct perf_tool *tool,
407 union perf_event *event,
408 struct perf_session *session);
410 static void perf_tool__fill_defaults(struct perf_tool *tool)
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;
442 tool->finished_round = process_finished_round_stub;
446 void mem_bswap_64(void *src, int byte_size)
450 while (byte_size > 0) {
452 byte_size -= sizeof(u64);
457 static void perf_event__all64_swap(union perf_event *event)
459 struct perf_event_header *hdr = &event->header;
460 mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr));
463 static void perf_event__comm_swap(union perf_event *event)
465 event->comm.pid = bswap_32(event->comm.pid);
466 event->comm.tid = bswap_32(event->comm.tid);
469 static void perf_event__mmap_swap(union perf_event *event)
471 event->mmap.pid = bswap_32(event->mmap.pid);
472 event->mmap.tid = bswap_32(event->mmap.tid);
473 event->mmap.start = bswap_64(event->mmap.start);
474 event->mmap.len = bswap_64(event->mmap.len);
475 event->mmap.pgoff = bswap_64(event->mmap.pgoff);
478 static void perf_event__task_swap(union perf_event *event)
480 event->fork.pid = bswap_32(event->fork.pid);
481 event->fork.tid = bswap_32(event->fork.tid);
482 event->fork.ppid = bswap_32(event->fork.ppid);
483 event->fork.ptid = bswap_32(event->fork.ptid);
484 event->fork.time = bswap_64(event->fork.time);
487 static void perf_event__read_swap(union perf_event *event)
489 event->read.pid = bswap_32(event->read.pid);
490 event->read.tid = bswap_32(event->read.tid);
491 event->read.value = bswap_64(event->read.value);
492 event->read.time_enabled = bswap_64(event->read.time_enabled);
493 event->read.time_running = bswap_64(event->read.time_running);
494 event->read.id = bswap_64(event->read.id);
497 static u8 revbyte(u8 b)
499 int rev = (b >> 4) | ((b & 0xf) << 4);
500 rev = ((rev & 0xcc) >> 2) | ((rev & 0x33) << 2);
501 rev = ((rev & 0xaa) >> 1) | ((rev & 0x55) << 1);
506 * XXX this is hack in attempt to carry flags bitfield
507 * throught endian village. ABI says:
509 * Bit-fields are allocated from right to left (least to most significant)
510 * on little-endian implementations and from left to right (most to least
511 * significant) on big-endian implementations.
513 * The above seems to be byte specific, so we need to reverse each
514 * byte of the bitfield. 'Internet' also says this might be implementation
515 * specific and we probably need proper fix and carry perf_event_attr
516 * bitfield flags in separate data file FEAT_ section. Thought this seems
519 static void swap_bitfield(u8 *p, unsigned len)
523 for (i = 0; i < len; i++) {
529 /* exported for swapping attributes in file header */
530 void perf_event__attr_swap(struct perf_event_attr *attr)
532 attr->type = bswap_32(attr->type);
533 attr->size = bswap_32(attr->size);
534 attr->config = bswap_64(attr->config);
535 attr->sample_period = bswap_64(attr->sample_period);
536 attr->sample_type = bswap_64(attr->sample_type);
537 attr->read_format = bswap_64(attr->read_format);
538 attr->wakeup_events = bswap_32(attr->wakeup_events);
539 attr->bp_type = bswap_32(attr->bp_type);
540 attr->bp_addr = bswap_64(attr->bp_addr);
541 attr->bp_len = bswap_64(attr->bp_len);
543 swap_bitfield((u8 *) (&attr->read_format + 1), sizeof(u64));
546 static void perf_event__hdr_attr_swap(union perf_event *event)
550 perf_event__attr_swap(&event->attr.attr);
552 size = event->header.size;
553 size -= (void *)&event->attr.id - (void *)event;
554 mem_bswap_64(event->attr.id, size);
557 static void perf_event__event_type_swap(union perf_event *event)
559 event->event_type.event_type.event_id =
560 bswap_64(event->event_type.event_type.event_id);
563 static void perf_event__tracing_data_swap(union perf_event *event)
565 event->tracing_data.size = bswap_32(event->tracing_data.size);
568 typedef void (*perf_event__swap_op)(union perf_event *event);
570 static perf_event__swap_op perf_event__swap_ops[] = {
571 [PERF_RECORD_MMAP] = perf_event__mmap_swap,
572 [PERF_RECORD_COMM] = perf_event__comm_swap,
573 [PERF_RECORD_FORK] = perf_event__task_swap,
574 [PERF_RECORD_EXIT] = perf_event__task_swap,
575 [PERF_RECORD_LOST] = perf_event__all64_swap,
576 [PERF_RECORD_READ] = perf_event__read_swap,
577 [PERF_RECORD_SAMPLE] = perf_event__all64_swap,
578 [PERF_RECORD_HEADER_ATTR] = perf_event__hdr_attr_swap,
579 [PERF_RECORD_HEADER_EVENT_TYPE] = perf_event__event_type_swap,
580 [PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap,
581 [PERF_RECORD_HEADER_BUILD_ID] = NULL,
582 [PERF_RECORD_HEADER_MAX] = NULL,
585 struct sample_queue {
588 union perf_event *event;
589 struct list_head list;
592 static void perf_session_free_sample_buffers(struct perf_session *session)
594 struct ordered_samples *os = &session->ordered_samples;
596 while (!list_empty(&os->to_free)) {
597 struct sample_queue *sq;
599 sq = list_entry(os->to_free.next, struct sample_queue, list);
605 static int perf_session_deliver_event(struct perf_session *session,
606 union perf_event *event,
607 struct perf_sample *sample,
608 struct perf_tool *tool,
611 static void flush_sample_queue(struct perf_session *s,
612 struct perf_tool *tool)
614 struct ordered_samples *os = &s->ordered_samples;
615 struct list_head *head = &os->samples;
616 struct sample_queue *tmp, *iter;
617 struct perf_sample sample;
618 u64 limit = os->next_flush;
619 u64 last_ts = os->last_sample ? os->last_sample->timestamp : 0ULL;
620 unsigned idx = 0, progress_next = os->nr_samples / 16;
623 if (!tool->ordered_samples || !limit)
626 list_for_each_entry_safe(iter, tmp, head, list) {
627 if (iter->timestamp > limit)
630 ret = perf_session__parse_sample(s, iter->event, &sample);
632 pr_err("Can't parse sample, err = %d\n", ret);
634 perf_session_deliver_event(s, iter->event, &sample, tool,
637 os->last_flush = iter->timestamp;
638 list_del(&iter->list);
639 list_add(&iter->list, &os->sample_cache);
640 if (++idx >= progress_next) {
641 progress_next += os->nr_samples / 16;
642 ui_progress__update(idx, os->nr_samples,
643 "Processing time ordered events...");
647 if (list_empty(head)) {
648 os->last_sample = NULL;
649 } else if (last_ts <= limit) {
651 list_entry(head->prev, struct sample_queue, list);
658 * When perf record finishes a pass on every buffers, it records this pseudo
660 * We record the max timestamp t found in the pass n.
661 * Assuming these timestamps are monotonic across cpus, we know that if
662 * a buffer still has events with timestamps below t, they will be all
663 * available and then read in the pass n + 1.
664 * Hence when we start to read the pass n + 2, we can safely flush every
665 * events with timestamps below t.
667 * ============ PASS n =================
670 * cnt1 timestamps | cnt2 timestamps
673 * - | 4 <--- max recorded
675 * ============ PASS n + 1 ==============
678 * cnt1 timestamps | cnt2 timestamps
681 * 5 | 7 <---- max recorded
683 * Flush every events below timestamp 4
685 * ============ PASS n + 2 ==============
688 * cnt1 timestamps | cnt2 timestamps
693 * Flush every events below timestamp 7
696 static int process_finished_round(struct perf_tool *tool,
697 union perf_event *event __used,
698 struct perf_session *session)
700 flush_sample_queue(session, tool);
701 session->ordered_samples.next_flush = session->ordered_samples.max_timestamp;
706 /* The queue is ordered by time */
707 static void __queue_event(struct sample_queue *new, struct perf_session *s)
709 struct ordered_samples *os = &s->ordered_samples;
710 struct sample_queue *sample = os->last_sample;
711 u64 timestamp = new->timestamp;
715 os->last_sample = new;
718 list_add(&new->list, &os->samples);
719 os->max_timestamp = timestamp;
724 * last_sample might point to some random place in the list as it's
725 * the last queued event. We expect that the new event is close to
728 if (sample->timestamp <= timestamp) {
729 while (sample->timestamp <= timestamp) {
730 p = sample->list.next;
731 if (p == &os->samples) {
732 list_add_tail(&new->list, &os->samples);
733 os->max_timestamp = timestamp;
736 sample = list_entry(p, struct sample_queue, list);
738 list_add_tail(&new->list, &sample->list);
740 while (sample->timestamp > timestamp) {
741 p = sample->list.prev;
742 if (p == &os->samples) {
743 list_add(&new->list, &os->samples);
746 sample = list_entry(p, struct sample_queue, list);
748 list_add(&new->list, &sample->list);
752 #define MAX_SAMPLE_BUFFER (64 * 1024 / sizeof(struct sample_queue))
754 static int perf_session_queue_event(struct perf_session *s, union perf_event *event,
755 struct perf_sample *sample, u64 file_offset)
757 struct ordered_samples *os = &s->ordered_samples;
758 struct list_head *sc = &os->sample_cache;
759 u64 timestamp = sample->time;
760 struct sample_queue *new;
762 if (!timestamp || timestamp == ~0ULL)
765 if (timestamp < s->ordered_samples.last_flush) {
766 printf("Warning: Timestamp below last timeslice flush\n");
770 if (!list_empty(sc)) {
771 new = list_entry(sc->next, struct sample_queue, list);
772 list_del(&new->list);
773 } else if (os->sample_buffer) {
774 new = os->sample_buffer + os->sample_buffer_idx;
775 if (++os->sample_buffer_idx == MAX_SAMPLE_BUFFER)
776 os->sample_buffer = NULL;
778 os->sample_buffer = malloc(MAX_SAMPLE_BUFFER * sizeof(*new));
779 if (!os->sample_buffer)
781 list_add(&os->sample_buffer->list, &os->to_free);
782 os->sample_buffer_idx = 2;
783 new = os->sample_buffer + 1;
786 new->timestamp = timestamp;
787 new->file_offset = file_offset;
790 __queue_event(new, s);
795 static void callchain__printf(struct perf_sample *sample)
799 printf("... chain: nr:%" PRIu64 "\n", sample->callchain->nr);
801 for (i = 0; i < sample->callchain->nr; i++)
802 printf("..... %2d: %016" PRIx64 "\n",
803 i, sample->callchain->ips[i]);
806 static void branch_stack__printf(struct perf_sample *sample)
810 printf("... branch stack: nr:%" PRIu64 "\n", sample->branch_stack->nr);
812 for (i = 0; i < sample->branch_stack->nr; i++)
813 printf("..... %2"PRIu64": %016" PRIx64 " -> %016" PRIx64 "\n",
814 i, sample->branch_stack->entries[i].from,
815 sample->branch_stack->entries[i].to);
818 static void perf_session__print_tstamp(struct perf_session *session,
819 union perf_event *event,
820 struct perf_sample *sample)
822 if (event->header.type != PERF_RECORD_SAMPLE &&
823 !session->sample_id_all) {
824 fputs("-1 -1 ", stdout);
828 if ((session->sample_type & PERF_SAMPLE_CPU))
829 printf("%u ", sample->cpu);
831 if (session->sample_type & PERF_SAMPLE_TIME)
832 printf("%" PRIu64 " ", sample->time);
835 static void dump_event(struct perf_session *session, union perf_event *event,
836 u64 file_offset, struct perf_sample *sample)
841 printf("\n%#" PRIx64 " [%#x]: event: %d\n",
842 file_offset, event->header.size, event->header.type);
847 perf_session__print_tstamp(session, event, sample);
849 printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset,
850 event->header.size, perf_event__name(event->header.type));
853 static void dump_sample(struct perf_session *session, union perf_event *event,
854 struct perf_sample *sample)
859 printf("(IP, %d): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n",
860 event->header.misc, sample->pid, sample->tid, sample->ip,
861 sample->period, sample->addr);
863 if (session->sample_type & PERF_SAMPLE_CALLCHAIN)
864 callchain__printf(sample);
866 if (session->sample_type & PERF_SAMPLE_BRANCH_STACK)
867 branch_stack__printf(sample);
870 static struct machine *
871 perf_session__find_machine_for_cpumode(struct perf_session *session,
872 union perf_event *event)
874 const u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
876 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
879 if (event->header.type == PERF_RECORD_MMAP)
880 pid = event->mmap.pid;
884 return perf_session__find_machine(session, pid);
887 return perf_session__find_host_machine(session);
890 static int perf_session_deliver_event(struct perf_session *session,
891 union perf_event *event,
892 struct perf_sample *sample,
893 struct perf_tool *tool,
896 struct perf_evsel *evsel;
897 struct machine *machine;
899 dump_event(session, event, file_offset, sample);
901 evsel = perf_evlist__id2evsel(session->evlist, sample->id);
902 if (evsel != NULL && event->header.type != PERF_RECORD_SAMPLE) {
904 * XXX We're leaving PERF_RECORD_SAMPLE unnacounted here
905 * because the tools right now may apply filters, discarding
906 * some of the samples. For consistency, in the future we
907 * should have something like nr_filtered_samples and remove
908 * the sample->period from total_sample_period, etc, KISS for
911 * Also testing against NULL allows us to handle files without
912 * attr.sample_id_all and/or without PERF_SAMPLE_ID. In the
913 * future probably it'll be a good idea to restrict event
914 * processing via perf_session to files with both set.
916 hists__inc_nr_events(&evsel->hists, event->header.type);
919 machine = perf_session__find_machine_for_cpumode(session, event);
921 switch (event->header.type) {
922 case PERF_RECORD_SAMPLE:
923 dump_sample(session, event, sample);
925 ++session->hists.stats.nr_unknown_id;
928 if (machine == NULL) {
929 ++session->hists.stats.nr_unprocessable_samples;
932 return tool->sample(tool, event, sample, evsel, machine);
933 case PERF_RECORD_MMAP:
934 return tool->mmap(tool, event, sample, machine);
935 case PERF_RECORD_COMM:
936 return tool->comm(tool, event, sample, machine);
937 case PERF_RECORD_FORK:
938 return tool->fork(tool, event, sample, machine);
939 case PERF_RECORD_EXIT:
940 return tool->exit(tool, event, sample, machine);
941 case PERF_RECORD_LOST:
942 if (tool->lost == perf_event__process_lost)
943 session->hists.stats.total_lost += event->lost.lost;
944 return tool->lost(tool, event, sample, machine);
945 case PERF_RECORD_READ:
946 return tool->read(tool, event, sample, evsel, machine);
947 case PERF_RECORD_THROTTLE:
948 return tool->throttle(tool, event, sample, machine);
949 case PERF_RECORD_UNTHROTTLE:
950 return tool->unthrottle(tool, event, sample, machine);
952 ++session->hists.stats.nr_unknown_events;
957 static int perf_session__preprocess_sample(struct perf_session *session,
958 union perf_event *event, struct perf_sample *sample)
960 if (event->header.type != PERF_RECORD_SAMPLE ||
961 !(session->sample_type & PERF_SAMPLE_CALLCHAIN))
964 if (!ip_callchain__valid(sample->callchain, event)) {
965 pr_debug("call-chain problem with event, skipping it.\n");
966 ++session->hists.stats.nr_invalid_chains;
967 session->hists.stats.total_invalid_chains += sample->period;
973 static int perf_session__process_user_event(struct perf_session *session, union perf_event *event,
974 struct perf_tool *tool, u64 file_offset)
978 dump_event(session, event, file_offset, NULL);
980 /* These events are processed right away */
981 switch (event->header.type) {
982 case PERF_RECORD_HEADER_ATTR:
983 err = tool->attr(event, &session->evlist);
985 perf_session__update_sample_type(session);
987 case PERF_RECORD_HEADER_EVENT_TYPE:
988 return tool->event_type(tool, event);
989 case PERF_RECORD_HEADER_TRACING_DATA:
990 /* setup for reading amidst mmap */
991 lseek(session->fd, file_offset, SEEK_SET);
992 return tool->tracing_data(event, session);
993 case PERF_RECORD_HEADER_BUILD_ID:
994 return tool->build_id(tool, event, session);
995 case PERF_RECORD_FINISHED_ROUND:
996 return tool->finished_round(tool, event, session);
1002 static int perf_session__process_event(struct perf_session *session,
1003 union perf_event *event,
1004 struct perf_tool *tool,
1007 struct perf_sample sample;
1010 if (session->header.needs_swap &&
1011 perf_event__swap_ops[event->header.type])
1012 perf_event__swap_ops[event->header.type](event);
1014 if (event->header.type >= PERF_RECORD_HEADER_MAX)
1017 hists__inc_nr_events(&session->hists, event->header.type);
1019 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
1020 return perf_session__process_user_event(session, event, tool, file_offset);
1023 * For all kernel events we get the sample data
1025 ret = perf_session__parse_sample(session, event, &sample);
1029 /* Preprocess sample records - precheck callchains */
1030 if (perf_session__preprocess_sample(session, event, &sample))
1033 if (tool->ordered_samples) {
1034 ret = perf_session_queue_event(session, event, &sample,
1040 return perf_session_deliver_event(session, event, &sample, tool,
1044 void perf_event_header__bswap(struct perf_event_header *self)
1046 self->type = bswap_32(self->type);
1047 self->misc = bswap_16(self->misc);
1048 self->size = bswap_16(self->size);
1051 struct thread *perf_session__findnew(struct perf_session *session, pid_t pid)
1053 return machine__findnew_thread(&session->host_machine, pid);
1056 static struct thread *perf_session__register_idle_thread(struct perf_session *self)
1058 struct thread *thread = perf_session__findnew(self, 0);
1060 if (thread == NULL || thread__set_comm(thread, "swapper")) {
1061 pr_err("problem inserting idle task.\n");
1068 static void perf_session__warn_about_errors(const struct perf_session *session,
1069 const struct perf_tool *tool)
1071 if (tool->lost == perf_event__process_lost &&
1072 session->hists.stats.nr_events[PERF_RECORD_LOST] != 0) {
1073 ui__warning("Processed %d events and lost %d chunks!\n\n"
1074 "Check IO/CPU overload!\n\n",
1075 session->hists.stats.nr_events[0],
1076 session->hists.stats.nr_events[PERF_RECORD_LOST]);
1079 if (session->hists.stats.nr_unknown_events != 0) {
1080 ui__warning("Found %u unknown events!\n\n"
1081 "Is this an older tool processing a perf.data "
1082 "file generated by a more recent tool?\n\n"
1083 "If that is not the case, consider "
1084 "reporting to linux-kernel@vger.kernel.org.\n\n",
1085 session->hists.stats.nr_unknown_events);
1088 if (session->hists.stats.nr_unknown_id != 0) {
1089 ui__warning("%u samples with id not present in the header\n",
1090 session->hists.stats.nr_unknown_id);
1093 if (session->hists.stats.nr_invalid_chains != 0) {
1094 ui__warning("Found invalid callchains!\n\n"
1095 "%u out of %u events were discarded for this reason.\n\n"
1096 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
1097 session->hists.stats.nr_invalid_chains,
1098 session->hists.stats.nr_events[PERF_RECORD_SAMPLE]);
1101 if (session->hists.stats.nr_unprocessable_samples != 0) {
1102 ui__warning("%u unprocessable samples recorded.\n"
1103 "Do you have a KVM guest running and not using 'perf kvm'?\n",
1104 session->hists.stats.nr_unprocessable_samples);
1108 #define session_done() (*(volatile int *)(&session_done))
1109 volatile int session_done;
1111 static int __perf_session__process_pipe_events(struct perf_session *self,
1112 struct perf_tool *tool)
1114 union perf_event *event;
1115 uint32_t size, cur_size = 0;
1122 perf_tool__fill_defaults(tool);
1125 cur_size = sizeof(union perf_event);
1127 buf = malloc(cur_size);
1132 err = readn(self->fd, event, sizeof(struct perf_event_header));
1137 pr_err("failed to read event header\n");
1141 if (self->header.needs_swap)
1142 perf_event_header__bswap(&event->header);
1144 size = event->header.size;
1148 if (size > cur_size) {
1149 void *new = realloc(buf, size);
1151 pr_err("failed to allocate memory to read event\n");
1159 p += sizeof(struct perf_event_header);
1161 if (size - sizeof(struct perf_event_header)) {
1162 err = readn(self->fd, p, size - sizeof(struct perf_event_header));
1165 pr_err("unexpected end of event stream\n");
1169 pr_err("failed to read event data\n");
1174 if ((skip = perf_session__process_event(self, event, tool, head)) < 0) {
1175 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
1176 head, event->header.size, event->header.type);
1186 if (!session_done())
1192 perf_session__warn_about_errors(self, tool);
1193 perf_session_free_sample_buffers(self);
1197 static union perf_event *
1198 fetch_mmaped_event(struct perf_session *session,
1199 u64 head, size_t mmap_size, char *buf)
1201 union perf_event *event;
1204 * Ensure we have enough space remaining to read
1205 * the size of the event in the headers.
1207 if (head + sizeof(event->header) > mmap_size)
1210 event = (union perf_event *)(buf + head);
1212 if (session->header.needs_swap)
1213 perf_event_header__bswap(&event->header);
1215 if (head + event->header.size > mmap_size)
1221 int __perf_session__process_events(struct perf_session *session,
1222 u64 data_offset, u64 data_size,
1223 u64 file_size, struct perf_tool *tool)
1225 u64 head, page_offset, file_offset, file_pos, progress_next;
1226 int err, mmap_prot, mmap_flags, map_idx = 0;
1227 size_t page_size, mmap_size;
1228 char *buf, *mmaps[8];
1229 union perf_event *event;
1232 perf_tool__fill_defaults(tool);
1234 page_size = sysconf(_SC_PAGESIZE);
1236 page_offset = page_size * (data_offset / page_size);
1237 file_offset = page_offset;
1238 head = data_offset - page_offset;
1240 if (data_offset + data_size < file_size)
1241 file_size = data_offset + data_size;
1243 progress_next = file_size / 16;
1245 mmap_size = session->mmap_window;
1246 if (mmap_size > file_size)
1247 mmap_size = file_size;
1249 memset(mmaps, 0, sizeof(mmaps));
1251 mmap_prot = PROT_READ;
1252 mmap_flags = MAP_SHARED;
1254 if (session->header.needs_swap) {
1255 mmap_prot |= PROT_WRITE;
1256 mmap_flags = MAP_PRIVATE;
1259 buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, session->fd,
1261 if (buf == MAP_FAILED) {
1262 pr_err("failed to mmap file\n");
1266 mmaps[map_idx] = buf;
1267 map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
1268 file_pos = file_offset + head;
1271 event = fetch_mmaped_event(session, head, mmap_size, buf);
1273 if (mmaps[map_idx]) {
1274 munmap(mmaps[map_idx], mmap_size);
1275 mmaps[map_idx] = NULL;
1278 page_offset = page_size * (head / page_size);
1279 file_offset += page_offset;
1280 head -= page_offset;
1284 size = event->header.size;
1287 perf_session__process_event(session, event, tool, file_pos) < 0) {
1288 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
1289 file_offset + head, event->header.size,
1290 event->header.type);
1298 if (file_pos >= progress_next) {
1299 progress_next += file_size / 16;
1300 ui_progress__update(file_pos, file_size,
1301 "Processing events...");
1304 if (file_pos < file_size)
1308 /* do the final flush for ordered samples */
1309 session->ordered_samples.next_flush = ULLONG_MAX;
1310 flush_sample_queue(session, tool);
1312 perf_session__warn_about_errors(session, tool);
1313 perf_session_free_sample_buffers(session);
1317 int perf_session__process_events(struct perf_session *self,
1318 struct perf_tool *tool)
1322 if (perf_session__register_idle_thread(self) == NULL)
1326 err = __perf_session__process_events(self,
1327 self->header.data_offset,
1328 self->header.data_size,
1331 err = __perf_session__process_pipe_events(self, tool);
1336 bool perf_session__has_traces(struct perf_session *self, const char *msg)
1338 if (!(self->sample_type & PERF_SAMPLE_RAW)) {
1339 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
1346 int maps__set_kallsyms_ref_reloc_sym(struct map **maps,
1347 const char *symbol_name, u64 addr)
1351 struct ref_reloc_sym *ref;
1353 ref = zalloc(sizeof(struct ref_reloc_sym));
1357 ref->name = strdup(symbol_name);
1358 if (ref->name == NULL) {
1363 bracket = strchr(ref->name, ']');
1369 for (i = 0; i < MAP__NR_TYPES; ++i) {
1370 struct kmap *kmap = map__kmap(maps[i]);
1371 kmap->ref_reloc_sym = ref;
1377 size_t perf_session__fprintf_dsos(struct perf_session *self, FILE *fp)
1379 return __dsos__fprintf(&self->host_machine.kernel_dsos, fp) +
1380 __dsos__fprintf(&self->host_machine.user_dsos, fp) +
1381 machines__fprintf_dsos(&self->machines, fp);
1384 size_t perf_session__fprintf_dsos_buildid(struct perf_session *self, FILE *fp,
1387 size_t ret = machine__fprintf_dsos_buildid(&self->host_machine, fp, with_hits);
1388 return ret + machines__fprintf_dsos_buildid(&self->machines, fp, with_hits);
1391 size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp)
1393 struct perf_evsel *pos;
1394 size_t ret = fprintf(fp, "Aggregated stats:\n");
1396 ret += hists__fprintf_nr_events(&session->hists, fp);
1398 list_for_each_entry(pos, &session->evlist->entries, node) {
1399 ret += fprintf(fp, "%s stats:\n", event_name(pos));
1400 ret += hists__fprintf_nr_events(&pos->hists, fp);
1406 size_t perf_session__fprintf(struct perf_session *session, FILE *fp)
1409 * FIXME: Here we have to actually print all the machines in this
1410 * session, not just the host...
1412 return machine__fprintf(&session->host_machine, fp);
1415 void perf_session__remove_thread(struct perf_session *session,
1419 * FIXME: This one makes no sense, we need to remove the thread from
1420 * the machine it belongs to, perf_session can have many machines, so
1421 * doing it always on ->host_machine is wrong. Fix when auditing all
1422 * the 'perf kvm' code.
1424 machine__remove_thread(&session->host_machine, th);
1427 struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
1430 struct perf_evsel *pos;
1432 list_for_each_entry(pos, &session->evlist->entries, node) {
1433 if (pos->attr.type == type)
1439 void perf_event__print_ip(union perf_event *event, struct perf_sample *sample,
1440 struct machine *machine, struct perf_evsel *evsel,
1441 int print_sym, int print_dso, int print_symoffset)
1443 struct addr_location al;
1444 struct callchain_cursor_node *node;
1446 if (perf_event__preprocess_sample(event, machine, &al, sample,
1448 error("problem processing %d event, skipping it.\n",
1449 event->header.type);
1453 if (symbol_conf.use_callchain && sample->callchain) {
1455 if (machine__resolve_callchain(machine, evsel, al.thread,
1456 sample->callchain, NULL) != 0) {
1458 error("Failed to resolve callchain. Skipping\n");
1461 callchain_cursor_commit(&callchain_cursor);
1464 node = callchain_cursor_current(&callchain_cursor);
1468 printf("\t%16" PRIx64, node->ip);
1471 symbol__fprintf_symname(node->sym, stdout);
1475 map__fprintf_dsoname(node->map, stdout);
1480 callchain_cursor_advance(&callchain_cursor);
1484 printf("%16" PRIx64, sample->ip);
1487 if (print_symoffset)
1488 symbol__fprintf_symname_offs(al.sym, &al,
1491 symbol__fprintf_symname(al.sym, stdout);
1496 map__fprintf_dsoname(al.map, stdout);
1502 int perf_session__cpu_bitmap(struct perf_session *session,
1503 const char *cpu_list, unsigned long *cpu_bitmap)
1506 struct cpu_map *map;
1508 for (i = 0; i < PERF_TYPE_MAX; ++i) {
1509 struct perf_evsel *evsel;
1511 evsel = perf_session__find_first_evtype(session, i);
1515 if (!(evsel->attr.sample_type & PERF_SAMPLE_CPU)) {
1516 pr_err("File does not contain CPU events. "
1517 "Remove -c option to proceed.\n");
1522 map = cpu_map__new(cpu_list);
1524 pr_err("Invalid cpu_list\n");
1528 for (i = 0; i < map->nr; i++) {
1529 int cpu = map->map[i];
1531 if (cpu >= MAX_NR_CPUS) {
1532 pr_err("Requested CPU %d too large. "
1533 "Consider raising MAX_NR_CPUS\n", cpu);
1537 set_bit(cpu, cpu_bitmap);
1543 void perf_session__fprintf_info(struct perf_session *session, FILE *fp,
1549 if (session == NULL || fp == NULL)
1552 ret = fstat(session->fd, &st);
1556 fprintf(fp, "# ========\n");
1557 fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
1558 perf_header__fprintf_info(session, fp, full);
1559 fprintf(fp, "# ========\n#\n");