1 #define _FILE_OFFSET_BITS 64
3 #include <linux/kernel.h>
17 static int perf_session__open(struct perf_session *self, bool force)
19 struct stat input_stat;
21 if (!strcmp(self->filename, "-")) {
23 self->fd = STDIN_FILENO;
25 if (perf_session__read_header(self, self->fd) < 0)
26 pr_err("incompatible file format");
31 self->fd = open(self->filename, O_RDONLY);
35 pr_err("failed to open %s: %s", self->filename, strerror(err));
36 if (err == ENOENT && !strcmp(self->filename, "perf.data"))
37 pr_err(" (try 'perf record' first)");
42 if (fstat(self->fd, &input_stat) < 0)
45 if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
46 pr_err("file %s not owned by current user or root\n",
51 if (!input_stat.st_size) {
52 pr_info("zero-sized file (%s), nothing to do!\n",
57 if (perf_session__read_header(self, self->fd) < 0) {
58 pr_err("incompatible file format");
62 if (!perf_evlist__valid_sample_type(self->evlist)) {
63 pr_err("non matching sample_type");
67 if (!perf_evlist__valid_sample_id_all(self->evlist)) {
68 pr_err("non matching sample_id_all");
72 self->size = input_stat.st_size;
81 void perf_session__update_sample_type(struct perf_session *self)
83 self->sample_type = perf_evlist__sample_type(self->evlist);
84 self->sample_size = __perf_evsel__sample_size(self->sample_type);
85 self->sample_id_all = perf_evlist__sample_id_all(self->evlist);
86 self->id_hdr_size = perf_evlist__id_hdr_size(self->evlist);
89 int perf_session__create_kernel_maps(struct perf_session *self)
91 int ret = machine__create_kernel_maps(&self->host_machine);
94 ret = machines__create_guest_kernel_maps(&self->machines);
98 static void perf_session__destroy_kernel_maps(struct perf_session *self)
100 machine__destroy_kernel_maps(&self->host_machine);
101 machines__destroy_guest_kernel_maps(&self->machines);
104 struct perf_session *perf_session__new(const char *filename, int mode,
105 bool force, bool repipe,
106 struct perf_event_ops *ops)
108 size_t len = filename ? strlen(filename) + 1 : 0;
109 struct perf_session *self = zalloc(sizeof(*self) + len);
114 memcpy(self->filename, filename, len);
116 * On 64bit we can mmap the data file in one go. No need for tiny mmap
117 * slices. On 32bit we use 32MB.
119 #if BITS_PER_LONG == 64
120 self->mmap_window = ULLONG_MAX;
122 self->mmap_window = 32 * 1024 * 1024ULL;
124 self->machines = RB_ROOT;
125 self->repipe = repipe;
126 INIT_LIST_HEAD(&self->ordered_samples.samples);
127 INIT_LIST_HEAD(&self->ordered_samples.sample_cache);
128 INIT_LIST_HEAD(&self->ordered_samples.to_free);
129 machine__init(&self->host_machine, "", HOST_KERNEL_ID);
131 if (mode == O_RDONLY) {
132 if (perf_session__open(self, force) < 0)
134 perf_session__update_sample_type(self);
135 } else if (mode == O_WRONLY) {
137 * In O_RDONLY mode this will be performed when reading the
138 * kernel MMAP event, in perf_event__process_mmap().
140 if (perf_session__create_kernel_maps(self) < 0)
144 if (ops && ops->ordering_requires_timestamps &&
145 ops->ordered_samples && !self->sample_id_all) {
146 dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n");
147 ops->ordered_samples = false;
153 perf_session__delete(self);
157 static void machine__delete_dead_threads(struct machine *machine)
159 struct thread *n, *t;
161 list_for_each_entry_safe(t, n, &machine->dead_threads, node) {
167 static void perf_session__delete_dead_threads(struct perf_session *session)
169 machine__delete_dead_threads(&session->host_machine);
172 static void machine__delete_threads(struct machine *self)
174 struct rb_node *nd = rb_first(&self->threads);
177 struct thread *t = rb_entry(nd, struct thread, rb_node);
179 rb_erase(&t->rb_node, &self->threads);
185 static void perf_session__delete_threads(struct perf_session *session)
187 machine__delete_threads(&session->host_machine);
190 void perf_session__delete(struct perf_session *self)
192 perf_session__destroy_kernel_maps(self);
193 perf_session__delete_dead_threads(self);
194 perf_session__delete_threads(self);
195 machine__exit(&self->host_machine);
200 void machine__remove_thread(struct machine *self, struct thread *th)
202 self->last_match = NULL;
203 rb_erase(&th->rb_node, &self->threads);
205 * We may have references to this thread, for instance in some hist_entry
206 * instances, so just move them to a separate list.
208 list_add_tail(&th->node, &self->dead_threads);
211 static bool symbol__match_parent_regex(struct symbol *sym)
213 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
219 int perf_session__resolve_callchain(struct perf_session *self,
220 struct thread *thread,
221 struct ip_callchain *chain,
222 struct symbol **parent)
224 u8 cpumode = PERF_RECORD_MISC_USER;
228 callchain_cursor_reset(&self->callchain_cursor);
230 for (i = 0; i < chain->nr; i++) {
232 struct addr_location al;
234 if (callchain_param.order == ORDER_CALLEE)
237 ip = chain->ips[chain->nr - i - 1];
239 if (ip >= PERF_CONTEXT_MAX) {
241 case PERF_CONTEXT_HV:
242 cpumode = PERF_RECORD_MISC_HYPERVISOR; break;
243 case PERF_CONTEXT_KERNEL:
244 cpumode = PERF_RECORD_MISC_KERNEL; break;
245 case PERF_CONTEXT_USER:
246 cpumode = PERF_RECORD_MISC_USER; break;
254 thread__find_addr_location(thread, self, cpumode,
255 MAP__FUNCTION, thread->pid, ip, &al, NULL);
256 if (al.sym != NULL) {
257 if (sort__has_parent && !*parent &&
258 symbol__match_parent_regex(al.sym))
260 if (!symbol_conf.use_callchain)
264 err = callchain_cursor_append(&self->callchain_cursor,
273 static int process_event_synth_stub(union perf_event *event __used,
274 struct perf_session *session __used)
276 dump_printf(": unhandled!\n");
280 static int process_event_sample_stub(union perf_event *event __used,
281 struct perf_sample *sample __used,
282 struct perf_evsel *evsel __used,
283 struct perf_session *session __used)
285 dump_printf(": unhandled!\n");
289 static int process_event_stub(union perf_event *event __used,
290 struct perf_sample *sample __used,
291 struct perf_session *session __used)
293 dump_printf(": unhandled!\n");
297 static int process_finished_round_stub(union perf_event *event __used,
298 struct perf_session *session __used,
299 struct perf_event_ops *ops __used)
301 dump_printf(": unhandled!\n");
305 static int process_finished_round(union perf_event *event,
306 struct perf_session *session,
307 struct perf_event_ops *ops);
309 static void perf_event_ops__fill_defaults(struct perf_event_ops *handler)
311 if (handler->sample == NULL)
312 handler->sample = process_event_sample_stub;
313 if (handler->mmap == NULL)
314 handler->mmap = process_event_stub;
315 if (handler->comm == NULL)
316 handler->comm = process_event_stub;
317 if (handler->fork == NULL)
318 handler->fork = process_event_stub;
319 if (handler->exit == NULL)
320 handler->exit = process_event_stub;
321 if (handler->lost == NULL)
322 handler->lost = perf_event__process_lost;
323 if (handler->read == NULL)
324 handler->read = process_event_stub;
325 if (handler->throttle == NULL)
326 handler->throttle = process_event_stub;
327 if (handler->unthrottle == NULL)
328 handler->unthrottle = process_event_stub;
329 if (handler->attr == NULL)
330 handler->attr = process_event_synth_stub;
331 if (handler->event_type == NULL)
332 handler->event_type = process_event_synth_stub;
333 if (handler->tracing_data == NULL)
334 handler->tracing_data = process_event_synth_stub;
335 if (handler->build_id == NULL)
336 handler->build_id = process_event_synth_stub;
337 if (handler->finished_round == NULL) {
338 if (handler->ordered_samples)
339 handler->finished_round = process_finished_round;
341 handler->finished_round = process_finished_round_stub;
345 void mem_bswap_64(void *src, int byte_size)
349 while (byte_size > 0) {
351 byte_size -= sizeof(u64);
356 static void perf_event__all64_swap(union perf_event *event)
358 struct perf_event_header *hdr = &event->header;
359 mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr));
362 static void perf_event__comm_swap(union perf_event *event)
364 event->comm.pid = bswap_32(event->comm.pid);
365 event->comm.tid = bswap_32(event->comm.tid);
368 static void perf_event__mmap_swap(union perf_event *event)
370 event->mmap.pid = bswap_32(event->mmap.pid);
371 event->mmap.tid = bswap_32(event->mmap.tid);
372 event->mmap.start = bswap_64(event->mmap.start);
373 event->mmap.len = bswap_64(event->mmap.len);
374 event->mmap.pgoff = bswap_64(event->mmap.pgoff);
377 static void perf_event__task_swap(union perf_event *event)
379 event->fork.pid = bswap_32(event->fork.pid);
380 event->fork.tid = bswap_32(event->fork.tid);
381 event->fork.ppid = bswap_32(event->fork.ppid);
382 event->fork.ptid = bswap_32(event->fork.ptid);
383 event->fork.time = bswap_64(event->fork.time);
386 static void perf_event__read_swap(union perf_event *event)
388 event->read.pid = bswap_32(event->read.pid);
389 event->read.tid = bswap_32(event->read.tid);
390 event->read.value = bswap_64(event->read.value);
391 event->read.time_enabled = bswap_64(event->read.time_enabled);
392 event->read.time_running = bswap_64(event->read.time_running);
393 event->read.id = bswap_64(event->read.id);
396 /* exported for swapping attributes in file header */
397 void perf_event__attr_swap(struct perf_event_attr *attr)
399 attr->type = bswap_32(attr->type);
400 attr->size = bswap_32(attr->size);
401 attr->config = bswap_64(attr->config);
402 attr->sample_period = bswap_64(attr->sample_period);
403 attr->sample_type = bswap_64(attr->sample_type);
404 attr->read_format = bswap_64(attr->read_format);
405 attr->wakeup_events = bswap_32(attr->wakeup_events);
406 attr->bp_type = bswap_32(attr->bp_type);
407 attr->bp_addr = bswap_64(attr->bp_addr);
408 attr->bp_len = bswap_64(attr->bp_len);
411 static void perf_event__hdr_attr_swap(union perf_event *event)
415 perf_event__attr_swap(&event->attr.attr);
417 size = event->header.size;
418 size -= (void *)&event->attr.id - (void *)event;
419 mem_bswap_64(event->attr.id, size);
422 static void perf_event__event_type_swap(union perf_event *event)
424 event->event_type.event_type.event_id =
425 bswap_64(event->event_type.event_type.event_id);
428 static void perf_event__tracing_data_swap(union perf_event *event)
430 event->tracing_data.size = bswap_32(event->tracing_data.size);
433 typedef void (*perf_event__swap_op)(union perf_event *event);
435 static perf_event__swap_op perf_event__swap_ops[] = {
436 [PERF_RECORD_MMAP] = perf_event__mmap_swap,
437 [PERF_RECORD_COMM] = perf_event__comm_swap,
438 [PERF_RECORD_FORK] = perf_event__task_swap,
439 [PERF_RECORD_EXIT] = perf_event__task_swap,
440 [PERF_RECORD_LOST] = perf_event__all64_swap,
441 [PERF_RECORD_READ] = perf_event__read_swap,
442 [PERF_RECORD_SAMPLE] = perf_event__all64_swap,
443 [PERF_RECORD_HEADER_ATTR] = perf_event__hdr_attr_swap,
444 [PERF_RECORD_HEADER_EVENT_TYPE] = perf_event__event_type_swap,
445 [PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap,
446 [PERF_RECORD_HEADER_BUILD_ID] = NULL,
447 [PERF_RECORD_HEADER_MAX] = NULL,
450 struct sample_queue {
453 union perf_event *event;
454 struct list_head list;
457 static void perf_session_free_sample_buffers(struct perf_session *session)
459 struct ordered_samples *os = &session->ordered_samples;
461 while (!list_empty(&os->to_free)) {
462 struct sample_queue *sq;
464 sq = list_entry(os->to_free.next, struct sample_queue, list);
470 static int perf_session_deliver_event(struct perf_session *session,
471 union perf_event *event,
472 struct perf_sample *sample,
473 struct perf_event_ops *ops,
476 static void flush_sample_queue(struct perf_session *s,
477 struct perf_event_ops *ops)
479 struct ordered_samples *os = &s->ordered_samples;
480 struct list_head *head = &os->samples;
481 struct sample_queue *tmp, *iter;
482 struct perf_sample sample;
483 u64 limit = os->next_flush;
484 u64 last_ts = os->last_sample ? os->last_sample->timestamp : 0ULL;
485 unsigned idx = 0, progress_next = os->nr_samples / 16;
488 if (!ops->ordered_samples || !limit)
491 list_for_each_entry_safe(iter, tmp, head, list) {
492 if (iter->timestamp > limit)
495 ret = perf_session__parse_sample(s, iter->event, &sample);
497 pr_err("Can't parse sample, err = %d\n", ret);
499 perf_session_deliver_event(s, iter->event, &sample, ops,
502 os->last_flush = iter->timestamp;
503 list_del(&iter->list);
504 list_add(&iter->list, &os->sample_cache);
505 if (++idx >= progress_next) {
506 progress_next += os->nr_samples / 16;
507 ui_progress__update(idx, os->nr_samples,
508 "Processing time ordered events...");
512 if (list_empty(head)) {
513 os->last_sample = NULL;
514 } else if (last_ts <= limit) {
516 list_entry(head->prev, struct sample_queue, list);
523 * When perf record finishes a pass on every buffers, it records this pseudo
525 * We record the max timestamp t found in the pass n.
526 * Assuming these timestamps are monotonic across cpus, we know that if
527 * a buffer still has events with timestamps below t, they will be all
528 * available and then read in the pass n + 1.
529 * Hence when we start to read the pass n + 2, we can safely flush every
530 * events with timestamps below t.
532 * ============ PASS n =================
535 * cnt1 timestamps | cnt2 timestamps
538 * - | 4 <--- max recorded
540 * ============ PASS n + 1 ==============
543 * cnt1 timestamps | cnt2 timestamps
546 * 5 | 7 <---- max recorded
548 * Flush every events below timestamp 4
550 * ============ PASS n + 2 ==============
553 * cnt1 timestamps | cnt2 timestamps
558 * Flush every events below timestamp 7
561 static int process_finished_round(union perf_event *event __used,
562 struct perf_session *session,
563 struct perf_event_ops *ops)
565 flush_sample_queue(session, ops);
566 session->ordered_samples.next_flush = session->ordered_samples.max_timestamp;
571 /* The queue is ordered by time */
572 static void __queue_event(struct sample_queue *new, struct perf_session *s)
574 struct ordered_samples *os = &s->ordered_samples;
575 struct sample_queue *sample = os->last_sample;
576 u64 timestamp = new->timestamp;
580 os->last_sample = new;
583 list_add(&new->list, &os->samples);
584 os->max_timestamp = timestamp;
589 * last_sample might point to some random place in the list as it's
590 * the last queued event. We expect that the new event is close to
593 if (sample->timestamp <= timestamp) {
594 while (sample->timestamp <= timestamp) {
595 p = sample->list.next;
596 if (p == &os->samples) {
597 list_add_tail(&new->list, &os->samples);
598 os->max_timestamp = timestamp;
601 sample = list_entry(p, struct sample_queue, list);
603 list_add_tail(&new->list, &sample->list);
605 while (sample->timestamp > timestamp) {
606 p = sample->list.prev;
607 if (p == &os->samples) {
608 list_add(&new->list, &os->samples);
611 sample = list_entry(p, struct sample_queue, list);
613 list_add(&new->list, &sample->list);
617 #define MAX_SAMPLE_BUFFER (64 * 1024 / sizeof(struct sample_queue))
619 static int perf_session_queue_event(struct perf_session *s, union perf_event *event,
620 struct perf_sample *sample, u64 file_offset)
622 struct ordered_samples *os = &s->ordered_samples;
623 struct list_head *sc = &os->sample_cache;
624 u64 timestamp = sample->time;
625 struct sample_queue *new;
627 if (!timestamp || timestamp == ~0ULL)
630 if (timestamp < s->ordered_samples.last_flush) {
631 printf("Warning: Timestamp below last timeslice flush\n");
635 if (!list_empty(sc)) {
636 new = list_entry(sc->next, struct sample_queue, list);
637 list_del(&new->list);
638 } else if (os->sample_buffer) {
639 new = os->sample_buffer + os->sample_buffer_idx;
640 if (++os->sample_buffer_idx == MAX_SAMPLE_BUFFER)
641 os->sample_buffer = NULL;
643 os->sample_buffer = malloc(MAX_SAMPLE_BUFFER * sizeof(*new));
644 if (!os->sample_buffer)
646 list_add(&os->sample_buffer->list, &os->to_free);
647 os->sample_buffer_idx = 2;
648 new = os->sample_buffer + 1;
651 new->timestamp = timestamp;
652 new->file_offset = file_offset;
655 __queue_event(new, s);
660 static void callchain__printf(struct perf_sample *sample)
664 printf("... chain: nr:%" PRIu64 "\n", sample->callchain->nr);
666 for (i = 0; i < sample->callchain->nr; i++)
667 printf("..... %2d: %016" PRIx64 "\n",
668 i, sample->callchain->ips[i]);
671 static void perf_session__print_tstamp(struct perf_session *session,
672 union perf_event *event,
673 struct perf_sample *sample)
675 if (event->header.type != PERF_RECORD_SAMPLE &&
676 !session->sample_id_all) {
677 fputs("-1 -1 ", stdout);
681 if ((session->sample_type & PERF_SAMPLE_CPU))
682 printf("%u ", sample->cpu);
684 if (session->sample_type & PERF_SAMPLE_TIME)
685 printf("%" PRIu64 " ", sample->time);
688 static void dump_event(struct perf_session *session, union perf_event *event,
689 u64 file_offset, struct perf_sample *sample)
694 printf("\n%#" PRIx64 " [%#x]: event: %d\n",
695 file_offset, event->header.size, event->header.type);
700 perf_session__print_tstamp(session, event, sample);
702 printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset,
703 event->header.size, perf_event__name(event->header.type));
706 static void dump_sample(struct perf_session *session, union perf_event *event,
707 struct perf_sample *sample)
712 printf("(IP, %d): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n",
713 event->header.misc, sample->pid, sample->tid, sample->ip,
714 sample->period, sample->addr);
716 if (session->sample_type & PERF_SAMPLE_CALLCHAIN)
717 callchain__printf(sample);
720 static int perf_session_deliver_event(struct perf_session *session,
721 union perf_event *event,
722 struct perf_sample *sample,
723 struct perf_event_ops *ops,
726 struct perf_evsel *evsel;
728 dump_event(session, event, file_offset, sample);
730 evsel = perf_evlist__id2evsel(session->evlist, sample->id);
731 if (evsel != NULL && event->header.type != PERF_RECORD_SAMPLE) {
733 * XXX We're leaving PERF_RECORD_SAMPLE unnacounted here
734 * because the tools right now may apply filters, discarding
735 * some of the samples. For consistency, in the future we
736 * should have something like nr_filtered_samples and remove
737 * the sample->period from total_sample_period, etc, KISS for
740 * Also testing against NULL allows us to handle files without
741 * attr.sample_id_all and/or without PERF_SAMPLE_ID. In the
742 * future probably it'll be a good idea to restrict event
743 * processing via perf_session to files with both set.
745 hists__inc_nr_events(&evsel->hists, event->header.type);
748 switch (event->header.type) {
749 case PERF_RECORD_SAMPLE:
750 dump_sample(session, event, sample);
752 ++session->hists.stats.nr_unknown_id;
755 return ops->sample(event, sample, evsel, session);
756 case PERF_RECORD_MMAP:
757 return ops->mmap(event, sample, session);
758 case PERF_RECORD_COMM:
759 return ops->comm(event, sample, session);
760 case PERF_RECORD_FORK:
761 return ops->fork(event, sample, session);
762 case PERF_RECORD_EXIT:
763 return ops->exit(event, sample, session);
764 case PERF_RECORD_LOST:
765 return ops->lost(event, sample, session);
766 case PERF_RECORD_READ:
767 return ops->read(event, sample, session);
768 case PERF_RECORD_THROTTLE:
769 return ops->throttle(event, sample, session);
770 case PERF_RECORD_UNTHROTTLE:
771 return ops->unthrottle(event, sample, session);
773 ++session->hists.stats.nr_unknown_events;
778 static int perf_session__preprocess_sample(struct perf_session *session,
779 union perf_event *event, struct perf_sample *sample)
781 if (event->header.type != PERF_RECORD_SAMPLE ||
782 !(session->sample_type & PERF_SAMPLE_CALLCHAIN))
785 if (!ip_callchain__valid(sample->callchain, event)) {
786 pr_debug("call-chain problem with event, skipping it.\n");
787 ++session->hists.stats.nr_invalid_chains;
788 session->hists.stats.total_invalid_chains += sample->period;
794 static int perf_session__process_user_event(struct perf_session *session, union perf_event *event,
795 struct perf_event_ops *ops, u64 file_offset)
797 dump_event(session, event, file_offset, NULL);
799 /* These events are processed right away */
800 switch (event->header.type) {
801 case PERF_RECORD_HEADER_ATTR:
802 return ops->attr(event, session);
803 case PERF_RECORD_HEADER_EVENT_TYPE:
804 return ops->event_type(event, session);
805 case PERF_RECORD_HEADER_TRACING_DATA:
806 /* setup for reading amidst mmap */
807 lseek(session->fd, file_offset, SEEK_SET);
808 return ops->tracing_data(event, session);
809 case PERF_RECORD_HEADER_BUILD_ID:
810 return ops->build_id(event, session);
811 case PERF_RECORD_FINISHED_ROUND:
812 return ops->finished_round(event, session, ops);
818 static int perf_session__process_event(struct perf_session *session,
819 union perf_event *event,
820 struct perf_event_ops *ops,
823 struct perf_sample sample;
826 if (session->header.needs_swap &&
827 perf_event__swap_ops[event->header.type])
828 perf_event__swap_ops[event->header.type](event);
830 if (event->header.type >= PERF_RECORD_HEADER_MAX)
833 hists__inc_nr_events(&session->hists, event->header.type);
835 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
836 return perf_session__process_user_event(session, event, ops, file_offset);
839 * For all kernel events we get the sample data
841 ret = perf_session__parse_sample(session, event, &sample);
845 /* Preprocess sample records - precheck callchains */
846 if (perf_session__preprocess_sample(session, event, &sample))
849 if (ops->ordered_samples) {
850 ret = perf_session_queue_event(session, event, &sample,
856 return perf_session_deliver_event(session, event, &sample, ops,
860 void perf_event_header__bswap(struct perf_event_header *self)
862 self->type = bswap_32(self->type);
863 self->misc = bswap_16(self->misc);
864 self->size = bswap_16(self->size);
867 struct thread *perf_session__findnew(struct perf_session *session, pid_t pid)
869 return machine__findnew_thread(&session->host_machine, pid);
872 static struct thread *perf_session__register_idle_thread(struct perf_session *self)
874 struct thread *thread = perf_session__findnew(self, 0);
876 if (thread == NULL || thread__set_comm(thread, "swapper")) {
877 pr_err("problem inserting idle task.\n");
884 static void perf_session__warn_about_errors(const struct perf_session *session,
885 const struct perf_event_ops *ops)
887 if (ops->lost == perf_event__process_lost &&
888 session->hists.stats.nr_events[PERF_RECORD_LOST] != 0) {
889 ui__warning("Processed %d events and lost %d chunks!\n\n"
890 "Check IO/CPU overload!\n\n",
891 session->hists.stats.nr_events[0],
892 session->hists.stats.nr_events[PERF_RECORD_LOST]);
895 if (session->hists.stats.nr_unknown_events != 0) {
896 ui__warning("Found %u unknown events!\n\n"
897 "Is this an older tool processing a perf.data "
898 "file generated by a more recent tool?\n\n"
899 "If that is not the case, consider "
900 "reporting to linux-kernel@vger.kernel.org.\n\n",
901 session->hists.stats.nr_unknown_events);
904 if (session->hists.stats.nr_unknown_id != 0) {
905 ui__warning("%u samples with id not present in the header\n",
906 session->hists.stats.nr_unknown_id);
909 if (session->hists.stats.nr_invalid_chains != 0) {
910 ui__warning("Found invalid callchains!\n\n"
911 "%u out of %u events were discarded for this reason.\n\n"
912 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
913 session->hists.stats.nr_invalid_chains,
914 session->hists.stats.nr_events[PERF_RECORD_SAMPLE]);
918 #define session_done() (*(volatile int *)(&session_done))
919 volatile int session_done;
921 static int __perf_session__process_pipe_events(struct perf_session *self,
922 struct perf_event_ops *ops)
924 union perf_event event;
931 perf_event_ops__fill_defaults(ops);
935 err = readn(self->fd, &event, sizeof(struct perf_event_header));
940 pr_err("failed to read event header\n");
944 if (self->header.needs_swap)
945 perf_event_header__bswap(&event.header);
947 size = event.header.size;
952 p += sizeof(struct perf_event_header);
954 if (size - sizeof(struct perf_event_header)) {
955 err = readn(self->fd, p, size - sizeof(struct perf_event_header));
958 pr_err("unexpected end of event stream\n");
962 pr_err("failed to read event data\n");
968 (skip = perf_session__process_event(self, &event, ops, head)) < 0) {
969 dump_printf("%#" PRIx64 " [%#x]: skipping unknown header type: %d\n",
970 head, event.header.size, event.header.type);
972 * assume we lost track of the stream, check alignment, and
973 * increment a single u64 in the hope to catch on again 'soon'.
975 if (unlikely(head & 7))
991 perf_session__warn_about_errors(self, ops);
992 perf_session_free_sample_buffers(self);
996 static union perf_event *
997 fetch_mmaped_event(struct perf_session *session,
998 u64 head, size_t mmap_size, char *buf)
1000 union perf_event *event;
1003 * Ensure we have enough space remaining to read
1004 * the size of the event in the headers.
1006 if (head + sizeof(event->header) > mmap_size)
1009 event = (union perf_event *)(buf + head);
1011 if (session->header.needs_swap)
1012 perf_event_header__bswap(&event->header);
1014 if (head + event->header.size > mmap_size)
1020 int __perf_session__process_events(struct perf_session *session,
1021 u64 data_offset, u64 data_size,
1022 u64 file_size, struct perf_event_ops *ops)
1024 u64 head, page_offset, file_offset, file_pos, progress_next;
1025 int err, mmap_prot, mmap_flags, map_idx = 0;
1026 size_t page_size, mmap_size;
1027 char *buf, *mmaps[8];
1028 union perf_event *event;
1031 perf_event_ops__fill_defaults(ops);
1033 page_size = sysconf(_SC_PAGESIZE);
1035 page_offset = page_size * (data_offset / page_size);
1036 file_offset = page_offset;
1037 head = data_offset - page_offset;
1039 if (data_offset + data_size < file_size)
1040 file_size = data_offset + data_size;
1042 progress_next = file_size / 16;
1044 mmap_size = session->mmap_window;
1045 if (mmap_size > file_size)
1046 mmap_size = file_size;
1048 memset(mmaps, 0, sizeof(mmaps));
1050 mmap_prot = PROT_READ;
1051 mmap_flags = MAP_SHARED;
1053 if (session->header.needs_swap) {
1054 mmap_prot |= PROT_WRITE;
1055 mmap_flags = MAP_PRIVATE;
1058 buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, session->fd,
1060 if (buf == MAP_FAILED) {
1061 pr_err("failed to mmap file\n");
1065 mmaps[map_idx] = buf;
1066 map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
1067 file_pos = file_offset + head;
1070 event = fetch_mmaped_event(session, head, mmap_size, buf);
1072 if (mmaps[map_idx]) {
1073 munmap(mmaps[map_idx], mmap_size);
1074 mmaps[map_idx] = NULL;
1077 page_offset = page_size * (head / page_size);
1078 file_offset += page_offset;
1079 head -= page_offset;
1083 size = event->header.size;
1086 perf_session__process_event(session, event, ops, file_pos) < 0) {
1087 dump_printf("%#" PRIx64 " [%#x]: skipping unknown header type: %d\n",
1088 file_offset + head, event->header.size,
1089 event->header.type);
1091 * assume we lost track of the stream, check alignment, and
1092 * increment a single u64 in the hope to catch on again 'soon'.
1094 if (unlikely(head & 7))
1103 if (file_pos >= progress_next) {
1104 progress_next += file_size / 16;
1105 ui_progress__update(file_pos, file_size,
1106 "Processing events...");
1109 if (file_pos < file_size)
1113 /* do the final flush for ordered samples */
1114 session->ordered_samples.next_flush = ULLONG_MAX;
1115 flush_sample_queue(session, ops);
1117 perf_session__warn_about_errors(session, ops);
1118 perf_session_free_sample_buffers(session);
1122 int perf_session__process_events(struct perf_session *self,
1123 struct perf_event_ops *ops)
1127 if (perf_session__register_idle_thread(self) == NULL)
1131 err = __perf_session__process_events(self,
1132 self->header.data_offset,
1133 self->header.data_size,
1136 err = __perf_session__process_pipe_events(self, ops);
1141 bool perf_session__has_traces(struct perf_session *self, const char *msg)
1143 if (!(self->sample_type & PERF_SAMPLE_RAW)) {
1144 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
1151 int perf_session__set_kallsyms_ref_reloc_sym(struct map **maps,
1152 const char *symbol_name,
1157 struct ref_reloc_sym *ref;
1159 ref = zalloc(sizeof(struct ref_reloc_sym));
1163 ref->name = strdup(symbol_name);
1164 if (ref->name == NULL) {
1169 bracket = strchr(ref->name, ']');
1175 for (i = 0; i < MAP__NR_TYPES; ++i) {
1176 struct kmap *kmap = map__kmap(maps[i]);
1177 kmap->ref_reloc_sym = ref;
1183 size_t perf_session__fprintf_dsos(struct perf_session *self, FILE *fp)
1185 return __dsos__fprintf(&self->host_machine.kernel_dsos, fp) +
1186 __dsos__fprintf(&self->host_machine.user_dsos, fp) +
1187 machines__fprintf_dsos(&self->machines, fp);
1190 size_t perf_session__fprintf_dsos_buildid(struct perf_session *self, FILE *fp,
1193 size_t ret = machine__fprintf_dsos_buildid(&self->host_machine, fp, with_hits);
1194 return ret + machines__fprintf_dsos_buildid(&self->machines, fp, with_hits);
1197 size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp)
1199 struct perf_evsel *pos;
1200 size_t ret = fprintf(fp, "Aggregated stats:\n");
1202 ret += hists__fprintf_nr_events(&session->hists, fp);
1204 list_for_each_entry(pos, &session->evlist->entries, node) {
1205 ret += fprintf(fp, "%s stats:\n", event_name(pos));
1206 ret += hists__fprintf_nr_events(&pos->hists, fp);
1212 size_t perf_session__fprintf(struct perf_session *session, FILE *fp)
1215 * FIXME: Here we have to actually print all the machines in this
1216 * session, not just the host...
1218 return machine__fprintf(&session->host_machine, fp);
1221 void perf_session__remove_thread(struct perf_session *session,
1225 * FIXME: This one makes no sense, we need to remove the thread from
1226 * the machine it belongs to, perf_session can have many machines, so
1227 * doing it always on ->host_machine is wrong. Fix when auditing all
1228 * the 'perf kvm' code.
1230 machine__remove_thread(&session->host_machine, th);
1233 struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
1236 struct perf_evsel *pos;
1238 list_for_each_entry(pos, &session->evlist->entries, node) {
1239 if (pos->attr.type == type)
1245 void perf_session__print_ip(union perf_event *event,
1246 struct perf_sample *sample,
1247 struct perf_session *session,
1248 int print_sym, int print_dso)
1250 struct addr_location al;
1251 const char *symname, *dsoname;
1252 struct callchain_cursor *cursor = &session->callchain_cursor;
1253 struct callchain_cursor_node *node;
1255 if (perf_event__preprocess_sample(event, session, &al, sample,
1257 error("problem processing %d event, skipping it.\n",
1258 event->header.type);
1262 if (symbol_conf.use_callchain && sample->callchain) {
1264 if (perf_session__resolve_callchain(session, al.thread,
1265 sample->callchain, NULL) != 0) {
1267 error("Failed to resolve callchain. Skipping\n");
1270 callchain_cursor_commit(cursor);
1273 node = callchain_cursor_current(cursor);
1277 printf("\t%16" PRIx64, node->ip);
1279 if (node->sym && node->sym->name)
1280 symname = node->sym->name;
1284 printf(" %s", symname);
1287 if (node->map && node->map->dso && node->map->dso->name)
1288 dsoname = node->map->dso->name;
1292 printf(" (%s)", dsoname);
1296 callchain_cursor_advance(cursor);
1300 printf("%16" PRIx64, sample->ip);
1302 if (al.sym && al.sym->name)
1303 symname = al.sym->name;
1307 printf(" %s", symname);
1311 if (al.map && al.map->dso && al.map->dso->name)
1312 dsoname = al.map->dso->name;
1316 printf(" (%s)", dsoname);
1321 int perf_session__cpu_bitmap(struct perf_session *session,
1322 const char *cpu_list, unsigned long *cpu_bitmap)
1325 struct cpu_map *map;
1327 for (i = 0; i < PERF_TYPE_MAX; ++i) {
1328 struct perf_evsel *evsel;
1330 evsel = perf_session__find_first_evtype(session, i);
1334 if (!(evsel->attr.sample_type & PERF_SAMPLE_CPU)) {
1335 pr_err("File does not contain CPU events. "
1336 "Remove -c option to proceed.\n");
1341 map = cpu_map__new(cpu_list);
1343 for (i = 0; i < map->nr; i++) {
1344 int cpu = map->map[i];
1346 if (cpu >= MAX_NR_CPUS) {
1347 pr_err("Requested CPU %d too large. "
1348 "Consider raising MAX_NR_CPUS\n", cpu);
1352 set_bit(cpu, cpu_bitmap);
1358 void perf_session__fprintf_info(struct perf_session *session, FILE *fp,
1364 if (session == NULL || fp == NULL)
1367 ret = fstat(session->fd, &st);
1371 fprintf(fp, "# ========\n");
1372 fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
1373 perf_header__fprintf_info(session, fp, full);
1374 fprintf(fp, "# ========\n#\n");