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, struct perf_evsel *evsel,
220 struct thread *thread,
221 struct ip_callchain *chain,
222 struct symbol **parent)
224 u8 cpumode = PERF_RECORD_MISC_USER;
228 callchain_cursor_reset(&evsel->hists.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(&evsel->hists.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_synth_attr_stub(union perf_event *event __used,
281 struct perf_evlist **pevlist __used)
283 dump_printf(": unhandled!\n");
287 static int process_event_sample_stub(union perf_event *event __used,
288 struct perf_sample *sample __used,
289 struct perf_evsel *evsel __used,
290 struct perf_session *session __used)
292 dump_printf(": unhandled!\n");
296 static int process_event_stub(union perf_event *event __used,
297 struct perf_sample *sample __used,
298 struct perf_session *session __used)
300 dump_printf(": unhandled!\n");
304 static int process_finished_round_stub(union perf_event *event __used,
305 struct perf_session *session __used,
306 struct perf_event_ops *ops __used)
308 dump_printf(": unhandled!\n");
312 static int process_finished_round(union perf_event *event,
313 struct perf_session *session,
314 struct perf_event_ops *ops);
316 static void perf_event_ops__fill_defaults(struct perf_event_ops *handler)
318 if (handler->sample == NULL)
319 handler->sample = process_event_sample_stub;
320 if (handler->mmap == NULL)
321 handler->mmap = process_event_stub;
322 if (handler->comm == NULL)
323 handler->comm = process_event_stub;
324 if (handler->fork == NULL)
325 handler->fork = process_event_stub;
326 if (handler->exit == NULL)
327 handler->exit = process_event_stub;
328 if (handler->lost == NULL)
329 handler->lost = perf_event__process_lost;
330 if (handler->read == NULL)
331 handler->read = process_event_stub;
332 if (handler->throttle == NULL)
333 handler->throttle = process_event_stub;
334 if (handler->unthrottle == NULL)
335 handler->unthrottle = process_event_stub;
336 if (handler->attr == NULL)
337 handler->attr = process_event_synth_attr_stub;
338 if (handler->event_type == NULL)
339 handler->event_type = process_event_synth_stub;
340 if (handler->tracing_data == NULL)
341 handler->tracing_data = process_event_synth_stub;
342 if (handler->build_id == NULL)
343 handler->build_id = process_event_synth_stub;
344 if (handler->finished_round == NULL) {
345 if (handler->ordered_samples)
346 handler->finished_round = process_finished_round;
348 handler->finished_round = process_finished_round_stub;
352 void mem_bswap_64(void *src, int byte_size)
356 while (byte_size > 0) {
358 byte_size -= sizeof(u64);
363 static void perf_event__all64_swap(union perf_event *event)
365 struct perf_event_header *hdr = &event->header;
366 mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr));
369 static void perf_event__comm_swap(union perf_event *event)
371 event->comm.pid = bswap_32(event->comm.pid);
372 event->comm.tid = bswap_32(event->comm.tid);
375 static void perf_event__mmap_swap(union perf_event *event)
377 event->mmap.pid = bswap_32(event->mmap.pid);
378 event->mmap.tid = bswap_32(event->mmap.tid);
379 event->mmap.start = bswap_64(event->mmap.start);
380 event->mmap.len = bswap_64(event->mmap.len);
381 event->mmap.pgoff = bswap_64(event->mmap.pgoff);
384 static void perf_event__task_swap(union perf_event *event)
386 event->fork.pid = bswap_32(event->fork.pid);
387 event->fork.tid = bswap_32(event->fork.tid);
388 event->fork.ppid = bswap_32(event->fork.ppid);
389 event->fork.ptid = bswap_32(event->fork.ptid);
390 event->fork.time = bswap_64(event->fork.time);
393 static void perf_event__read_swap(union perf_event *event)
395 event->read.pid = bswap_32(event->read.pid);
396 event->read.tid = bswap_32(event->read.tid);
397 event->read.value = bswap_64(event->read.value);
398 event->read.time_enabled = bswap_64(event->read.time_enabled);
399 event->read.time_running = bswap_64(event->read.time_running);
400 event->read.id = bswap_64(event->read.id);
403 /* exported for swapping attributes in file header */
404 void perf_event__attr_swap(struct perf_event_attr *attr)
406 attr->type = bswap_32(attr->type);
407 attr->size = bswap_32(attr->size);
408 attr->config = bswap_64(attr->config);
409 attr->sample_period = bswap_64(attr->sample_period);
410 attr->sample_type = bswap_64(attr->sample_type);
411 attr->read_format = bswap_64(attr->read_format);
412 attr->wakeup_events = bswap_32(attr->wakeup_events);
413 attr->bp_type = bswap_32(attr->bp_type);
414 attr->bp_addr = bswap_64(attr->bp_addr);
415 attr->bp_len = bswap_64(attr->bp_len);
418 static void perf_event__hdr_attr_swap(union perf_event *event)
422 perf_event__attr_swap(&event->attr.attr);
424 size = event->header.size;
425 size -= (void *)&event->attr.id - (void *)event;
426 mem_bswap_64(event->attr.id, size);
429 static void perf_event__event_type_swap(union perf_event *event)
431 event->event_type.event_type.event_id =
432 bswap_64(event->event_type.event_type.event_id);
435 static void perf_event__tracing_data_swap(union perf_event *event)
437 event->tracing_data.size = bswap_32(event->tracing_data.size);
440 typedef void (*perf_event__swap_op)(union perf_event *event);
442 static perf_event__swap_op perf_event__swap_ops[] = {
443 [PERF_RECORD_MMAP] = perf_event__mmap_swap,
444 [PERF_RECORD_COMM] = perf_event__comm_swap,
445 [PERF_RECORD_FORK] = perf_event__task_swap,
446 [PERF_RECORD_EXIT] = perf_event__task_swap,
447 [PERF_RECORD_LOST] = perf_event__all64_swap,
448 [PERF_RECORD_READ] = perf_event__read_swap,
449 [PERF_RECORD_SAMPLE] = perf_event__all64_swap,
450 [PERF_RECORD_HEADER_ATTR] = perf_event__hdr_attr_swap,
451 [PERF_RECORD_HEADER_EVENT_TYPE] = perf_event__event_type_swap,
452 [PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap,
453 [PERF_RECORD_HEADER_BUILD_ID] = NULL,
454 [PERF_RECORD_HEADER_MAX] = NULL,
457 struct sample_queue {
460 union perf_event *event;
461 struct list_head list;
464 static void perf_session_free_sample_buffers(struct perf_session *session)
466 struct ordered_samples *os = &session->ordered_samples;
468 while (!list_empty(&os->to_free)) {
469 struct sample_queue *sq;
471 sq = list_entry(os->to_free.next, struct sample_queue, list);
477 static int perf_session_deliver_event(struct perf_session *session,
478 union perf_event *event,
479 struct perf_sample *sample,
480 struct perf_event_ops *ops,
483 static void flush_sample_queue(struct perf_session *s,
484 struct perf_event_ops *ops)
486 struct ordered_samples *os = &s->ordered_samples;
487 struct list_head *head = &os->samples;
488 struct sample_queue *tmp, *iter;
489 struct perf_sample sample;
490 u64 limit = os->next_flush;
491 u64 last_ts = os->last_sample ? os->last_sample->timestamp : 0ULL;
492 unsigned idx = 0, progress_next = os->nr_samples / 16;
495 if (!ops->ordered_samples || !limit)
498 list_for_each_entry_safe(iter, tmp, head, list) {
499 if (iter->timestamp > limit)
502 ret = perf_session__parse_sample(s, iter->event, &sample);
504 pr_err("Can't parse sample, err = %d\n", ret);
506 perf_session_deliver_event(s, iter->event, &sample, ops,
509 os->last_flush = iter->timestamp;
510 list_del(&iter->list);
511 list_add(&iter->list, &os->sample_cache);
512 if (++idx >= progress_next) {
513 progress_next += os->nr_samples / 16;
514 ui_progress__update(idx, os->nr_samples,
515 "Processing time ordered events...");
519 if (list_empty(head)) {
520 os->last_sample = NULL;
521 } else if (last_ts <= limit) {
523 list_entry(head->prev, struct sample_queue, list);
530 * When perf record finishes a pass on every buffers, it records this pseudo
532 * We record the max timestamp t found in the pass n.
533 * Assuming these timestamps are monotonic across cpus, we know that if
534 * a buffer still has events with timestamps below t, they will be all
535 * available and then read in the pass n + 1.
536 * Hence when we start to read the pass n + 2, we can safely flush every
537 * events with timestamps below t.
539 * ============ PASS n =================
542 * cnt1 timestamps | cnt2 timestamps
545 * - | 4 <--- max recorded
547 * ============ PASS n + 1 ==============
550 * cnt1 timestamps | cnt2 timestamps
553 * 5 | 7 <---- max recorded
555 * Flush every events below timestamp 4
557 * ============ PASS n + 2 ==============
560 * cnt1 timestamps | cnt2 timestamps
565 * Flush every events below timestamp 7
568 static int process_finished_round(union perf_event *event __used,
569 struct perf_session *session,
570 struct perf_event_ops *ops)
572 flush_sample_queue(session, ops);
573 session->ordered_samples.next_flush = session->ordered_samples.max_timestamp;
578 /* The queue is ordered by time */
579 static void __queue_event(struct sample_queue *new, struct perf_session *s)
581 struct ordered_samples *os = &s->ordered_samples;
582 struct sample_queue *sample = os->last_sample;
583 u64 timestamp = new->timestamp;
587 os->last_sample = new;
590 list_add(&new->list, &os->samples);
591 os->max_timestamp = timestamp;
596 * last_sample might point to some random place in the list as it's
597 * the last queued event. We expect that the new event is close to
600 if (sample->timestamp <= timestamp) {
601 while (sample->timestamp <= timestamp) {
602 p = sample->list.next;
603 if (p == &os->samples) {
604 list_add_tail(&new->list, &os->samples);
605 os->max_timestamp = timestamp;
608 sample = list_entry(p, struct sample_queue, list);
610 list_add_tail(&new->list, &sample->list);
612 while (sample->timestamp > timestamp) {
613 p = sample->list.prev;
614 if (p == &os->samples) {
615 list_add(&new->list, &os->samples);
618 sample = list_entry(p, struct sample_queue, list);
620 list_add(&new->list, &sample->list);
624 #define MAX_SAMPLE_BUFFER (64 * 1024 / sizeof(struct sample_queue))
626 static int perf_session_queue_event(struct perf_session *s, union perf_event *event,
627 struct perf_sample *sample, u64 file_offset)
629 struct ordered_samples *os = &s->ordered_samples;
630 struct list_head *sc = &os->sample_cache;
631 u64 timestamp = sample->time;
632 struct sample_queue *new;
634 if (!timestamp || timestamp == ~0ULL)
637 if (timestamp < s->ordered_samples.last_flush) {
638 printf("Warning: Timestamp below last timeslice flush\n");
642 if (!list_empty(sc)) {
643 new = list_entry(sc->next, struct sample_queue, list);
644 list_del(&new->list);
645 } else if (os->sample_buffer) {
646 new = os->sample_buffer + os->sample_buffer_idx;
647 if (++os->sample_buffer_idx == MAX_SAMPLE_BUFFER)
648 os->sample_buffer = NULL;
650 os->sample_buffer = malloc(MAX_SAMPLE_BUFFER * sizeof(*new));
651 if (!os->sample_buffer)
653 list_add(&os->sample_buffer->list, &os->to_free);
654 os->sample_buffer_idx = 2;
655 new = os->sample_buffer + 1;
658 new->timestamp = timestamp;
659 new->file_offset = file_offset;
662 __queue_event(new, s);
667 static void callchain__printf(struct perf_sample *sample)
671 printf("... chain: nr:%" PRIu64 "\n", sample->callchain->nr);
673 for (i = 0; i < sample->callchain->nr; i++)
674 printf("..... %2d: %016" PRIx64 "\n",
675 i, sample->callchain->ips[i]);
678 static void perf_session__print_tstamp(struct perf_session *session,
679 union perf_event *event,
680 struct perf_sample *sample)
682 if (event->header.type != PERF_RECORD_SAMPLE &&
683 !session->sample_id_all) {
684 fputs("-1 -1 ", stdout);
688 if ((session->sample_type & PERF_SAMPLE_CPU))
689 printf("%u ", sample->cpu);
691 if (session->sample_type & PERF_SAMPLE_TIME)
692 printf("%" PRIu64 " ", sample->time);
695 static void dump_event(struct perf_session *session, union perf_event *event,
696 u64 file_offset, struct perf_sample *sample)
701 printf("\n%#" PRIx64 " [%#x]: event: %d\n",
702 file_offset, event->header.size, event->header.type);
707 perf_session__print_tstamp(session, event, sample);
709 printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset,
710 event->header.size, perf_event__name(event->header.type));
713 static void dump_sample(struct perf_session *session, union perf_event *event,
714 struct perf_sample *sample)
719 printf("(IP, %d): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n",
720 event->header.misc, sample->pid, sample->tid, sample->ip,
721 sample->period, sample->addr);
723 if (session->sample_type & PERF_SAMPLE_CALLCHAIN)
724 callchain__printf(sample);
727 static int perf_session_deliver_event(struct perf_session *session,
728 union perf_event *event,
729 struct perf_sample *sample,
730 struct perf_event_ops *ops,
733 struct perf_evsel *evsel;
735 dump_event(session, event, file_offset, sample);
737 evsel = perf_evlist__id2evsel(session->evlist, sample->id);
738 if (evsel != NULL && event->header.type != PERF_RECORD_SAMPLE) {
740 * XXX We're leaving PERF_RECORD_SAMPLE unnacounted here
741 * because the tools right now may apply filters, discarding
742 * some of the samples. For consistency, in the future we
743 * should have something like nr_filtered_samples and remove
744 * the sample->period from total_sample_period, etc, KISS for
747 * Also testing against NULL allows us to handle files without
748 * attr.sample_id_all and/or without PERF_SAMPLE_ID. In the
749 * future probably it'll be a good idea to restrict event
750 * processing via perf_session to files with both set.
752 hists__inc_nr_events(&evsel->hists, event->header.type);
755 switch (event->header.type) {
756 case PERF_RECORD_SAMPLE:
757 dump_sample(session, event, sample);
759 ++session->hists.stats.nr_unknown_id;
762 return ops->sample(event, sample, evsel, session);
763 case PERF_RECORD_MMAP:
764 return ops->mmap(event, sample, session);
765 case PERF_RECORD_COMM:
766 return ops->comm(event, sample, session);
767 case PERF_RECORD_FORK:
768 return ops->fork(event, sample, session);
769 case PERF_RECORD_EXIT:
770 return ops->exit(event, sample, session);
771 case PERF_RECORD_LOST:
772 return ops->lost(event, sample, session);
773 case PERF_RECORD_READ:
774 return ops->read(event, sample, session);
775 case PERF_RECORD_THROTTLE:
776 return ops->throttle(event, sample, session);
777 case PERF_RECORD_UNTHROTTLE:
778 return ops->unthrottle(event, sample, session);
780 ++session->hists.stats.nr_unknown_events;
785 static int perf_session__preprocess_sample(struct perf_session *session,
786 union perf_event *event, struct perf_sample *sample)
788 if (event->header.type != PERF_RECORD_SAMPLE ||
789 !(session->sample_type & PERF_SAMPLE_CALLCHAIN))
792 if (!ip_callchain__valid(sample->callchain, event)) {
793 pr_debug("call-chain problem with event, skipping it.\n");
794 ++session->hists.stats.nr_invalid_chains;
795 session->hists.stats.total_invalid_chains += sample->period;
801 static int perf_session__process_user_event(struct perf_session *session, union perf_event *event,
802 struct perf_event_ops *ops, u64 file_offset)
806 dump_event(session, event, file_offset, NULL);
808 /* These events are processed right away */
809 switch (event->header.type) {
810 case PERF_RECORD_HEADER_ATTR:
811 err = ops->attr(event, &session->evlist);
813 perf_session__update_sample_type(session);
815 case PERF_RECORD_HEADER_EVENT_TYPE:
816 return ops->event_type(event, session);
817 case PERF_RECORD_HEADER_TRACING_DATA:
818 /* setup for reading amidst mmap */
819 lseek(session->fd, file_offset, SEEK_SET);
820 return ops->tracing_data(event, session);
821 case PERF_RECORD_HEADER_BUILD_ID:
822 return ops->build_id(event, session);
823 case PERF_RECORD_FINISHED_ROUND:
824 return ops->finished_round(event, session, ops);
830 static int perf_session__process_event(struct perf_session *session,
831 union perf_event *event,
832 struct perf_event_ops *ops,
835 struct perf_sample sample;
838 if (session->header.needs_swap &&
839 perf_event__swap_ops[event->header.type])
840 perf_event__swap_ops[event->header.type](event);
842 if (event->header.type >= PERF_RECORD_HEADER_MAX)
845 hists__inc_nr_events(&session->hists, event->header.type);
847 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
848 return perf_session__process_user_event(session, event, ops, file_offset);
851 * For all kernel events we get the sample data
853 ret = perf_session__parse_sample(session, event, &sample);
857 /* Preprocess sample records - precheck callchains */
858 if (perf_session__preprocess_sample(session, event, &sample))
861 if (ops->ordered_samples) {
862 ret = perf_session_queue_event(session, event, &sample,
868 return perf_session_deliver_event(session, event, &sample, ops,
872 void perf_event_header__bswap(struct perf_event_header *self)
874 self->type = bswap_32(self->type);
875 self->misc = bswap_16(self->misc);
876 self->size = bswap_16(self->size);
879 struct thread *perf_session__findnew(struct perf_session *session, pid_t pid)
881 return machine__findnew_thread(&session->host_machine, pid);
884 static struct thread *perf_session__register_idle_thread(struct perf_session *self)
886 struct thread *thread = perf_session__findnew(self, 0);
888 if (thread == NULL || thread__set_comm(thread, "swapper")) {
889 pr_err("problem inserting idle task.\n");
896 static void perf_session__warn_about_errors(const struct perf_session *session,
897 const struct perf_event_ops *ops)
899 if (ops->lost == perf_event__process_lost &&
900 session->hists.stats.nr_events[PERF_RECORD_LOST] != 0) {
901 ui__warning("Processed %d events and lost %d chunks!\n\n"
902 "Check IO/CPU overload!\n\n",
903 session->hists.stats.nr_events[0],
904 session->hists.stats.nr_events[PERF_RECORD_LOST]);
907 if (session->hists.stats.nr_unknown_events != 0) {
908 ui__warning("Found %u unknown events!\n\n"
909 "Is this an older tool processing a perf.data "
910 "file generated by a more recent tool?\n\n"
911 "If that is not the case, consider "
912 "reporting to linux-kernel@vger.kernel.org.\n\n",
913 session->hists.stats.nr_unknown_events);
916 if (session->hists.stats.nr_unknown_id != 0) {
917 ui__warning("%u samples with id not present in the header\n",
918 session->hists.stats.nr_unknown_id);
921 if (session->hists.stats.nr_invalid_chains != 0) {
922 ui__warning("Found invalid callchains!\n\n"
923 "%u out of %u events were discarded for this reason.\n\n"
924 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
925 session->hists.stats.nr_invalid_chains,
926 session->hists.stats.nr_events[PERF_RECORD_SAMPLE]);
930 #define session_done() (*(volatile int *)(&session_done))
931 volatile int session_done;
933 static int __perf_session__process_pipe_events(struct perf_session *self,
934 struct perf_event_ops *ops)
936 union perf_event event;
943 perf_event_ops__fill_defaults(ops);
947 err = readn(self->fd, &event, sizeof(struct perf_event_header));
952 pr_err("failed to read event header\n");
956 if (self->header.needs_swap)
957 perf_event_header__bswap(&event.header);
959 size = event.header.size;
964 p += sizeof(struct perf_event_header);
966 if (size - sizeof(struct perf_event_header)) {
967 err = readn(self->fd, p, size - sizeof(struct perf_event_header));
970 pr_err("unexpected end of event stream\n");
974 pr_err("failed to read event data\n");
980 (skip = perf_session__process_event(self, &event, ops, head)) < 0) {
981 dump_printf("%#" PRIx64 " [%#x]: skipping unknown header type: %d\n",
982 head, event.header.size, event.header.type);
984 * assume we lost track of the stream, check alignment, and
985 * increment a single u64 in the hope to catch on again 'soon'.
987 if (unlikely(head & 7))
1003 perf_session__warn_about_errors(self, ops);
1004 perf_session_free_sample_buffers(self);
1008 static union perf_event *
1009 fetch_mmaped_event(struct perf_session *session,
1010 u64 head, size_t mmap_size, char *buf)
1012 union perf_event *event;
1015 * Ensure we have enough space remaining to read
1016 * the size of the event in the headers.
1018 if (head + sizeof(event->header) > mmap_size)
1021 event = (union perf_event *)(buf + head);
1023 if (session->header.needs_swap)
1024 perf_event_header__bswap(&event->header);
1026 if (head + event->header.size > mmap_size)
1032 int __perf_session__process_events(struct perf_session *session,
1033 u64 data_offset, u64 data_size,
1034 u64 file_size, struct perf_event_ops *ops)
1036 u64 head, page_offset, file_offset, file_pos, progress_next;
1037 int err, mmap_prot, mmap_flags, map_idx = 0;
1038 size_t page_size, mmap_size;
1039 char *buf, *mmaps[8];
1040 union perf_event *event;
1043 perf_event_ops__fill_defaults(ops);
1045 page_size = sysconf(_SC_PAGESIZE);
1047 page_offset = page_size * (data_offset / page_size);
1048 file_offset = page_offset;
1049 head = data_offset - page_offset;
1051 if (data_offset + data_size < file_size)
1052 file_size = data_offset + data_size;
1054 progress_next = file_size / 16;
1056 mmap_size = session->mmap_window;
1057 if (mmap_size > file_size)
1058 mmap_size = file_size;
1060 memset(mmaps, 0, sizeof(mmaps));
1062 mmap_prot = PROT_READ;
1063 mmap_flags = MAP_SHARED;
1065 if (session->header.needs_swap) {
1066 mmap_prot |= PROT_WRITE;
1067 mmap_flags = MAP_PRIVATE;
1070 buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, session->fd,
1072 if (buf == MAP_FAILED) {
1073 pr_err("failed to mmap file\n");
1077 mmaps[map_idx] = buf;
1078 map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
1079 file_pos = file_offset + head;
1082 event = fetch_mmaped_event(session, head, mmap_size, buf);
1084 if (mmaps[map_idx]) {
1085 munmap(mmaps[map_idx], mmap_size);
1086 mmaps[map_idx] = NULL;
1089 page_offset = page_size * (head / page_size);
1090 file_offset += page_offset;
1091 head -= page_offset;
1095 size = event->header.size;
1098 perf_session__process_event(session, event, ops, file_pos) < 0) {
1099 dump_printf("%#" PRIx64 " [%#x]: skipping unknown header type: %d\n",
1100 file_offset + head, event->header.size,
1101 event->header.type);
1103 * assume we lost track of the stream, check alignment, and
1104 * increment a single u64 in the hope to catch on again 'soon'.
1106 if (unlikely(head & 7))
1115 if (file_pos >= progress_next) {
1116 progress_next += file_size / 16;
1117 ui_progress__update(file_pos, file_size,
1118 "Processing events...");
1121 if (file_pos < file_size)
1125 /* do the final flush for ordered samples */
1126 session->ordered_samples.next_flush = ULLONG_MAX;
1127 flush_sample_queue(session, ops);
1129 perf_session__warn_about_errors(session, ops);
1130 perf_session_free_sample_buffers(session);
1134 int perf_session__process_events(struct perf_session *self,
1135 struct perf_event_ops *ops)
1139 if (perf_session__register_idle_thread(self) == NULL)
1143 err = __perf_session__process_events(self,
1144 self->header.data_offset,
1145 self->header.data_size,
1148 err = __perf_session__process_pipe_events(self, ops);
1153 bool perf_session__has_traces(struct perf_session *self, const char *msg)
1155 if (!(self->sample_type & PERF_SAMPLE_RAW)) {
1156 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
1163 int perf_session__set_kallsyms_ref_reloc_sym(struct map **maps,
1164 const char *symbol_name,
1169 struct ref_reloc_sym *ref;
1171 ref = zalloc(sizeof(struct ref_reloc_sym));
1175 ref->name = strdup(symbol_name);
1176 if (ref->name == NULL) {
1181 bracket = strchr(ref->name, ']');
1187 for (i = 0; i < MAP__NR_TYPES; ++i) {
1188 struct kmap *kmap = map__kmap(maps[i]);
1189 kmap->ref_reloc_sym = ref;
1195 size_t perf_session__fprintf_dsos(struct perf_session *self, FILE *fp)
1197 return __dsos__fprintf(&self->host_machine.kernel_dsos, fp) +
1198 __dsos__fprintf(&self->host_machine.user_dsos, fp) +
1199 machines__fprintf_dsos(&self->machines, fp);
1202 size_t perf_session__fprintf_dsos_buildid(struct perf_session *self, FILE *fp,
1205 size_t ret = machine__fprintf_dsos_buildid(&self->host_machine, fp, with_hits);
1206 return ret + machines__fprintf_dsos_buildid(&self->machines, fp, with_hits);
1209 size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp)
1211 struct perf_evsel *pos;
1212 size_t ret = fprintf(fp, "Aggregated stats:\n");
1214 ret += hists__fprintf_nr_events(&session->hists, fp);
1216 list_for_each_entry(pos, &session->evlist->entries, node) {
1217 ret += fprintf(fp, "%s stats:\n", event_name(pos));
1218 ret += hists__fprintf_nr_events(&pos->hists, fp);
1224 size_t perf_session__fprintf(struct perf_session *session, FILE *fp)
1227 * FIXME: Here we have to actually print all the machines in this
1228 * session, not just the host...
1230 return machine__fprintf(&session->host_machine, fp);
1233 void perf_session__remove_thread(struct perf_session *session,
1237 * FIXME: This one makes no sense, we need to remove the thread from
1238 * the machine it belongs to, perf_session can have many machines, so
1239 * doing it always on ->host_machine is wrong. Fix when auditing all
1240 * the 'perf kvm' code.
1242 machine__remove_thread(&session->host_machine, th);
1245 struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
1248 struct perf_evsel *pos;
1250 list_for_each_entry(pos, &session->evlist->entries, node) {
1251 if (pos->attr.type == type)
1257 void perf_session__print_ip(union perf_event *event, struct perf_evsel *evsel,
1258 struct perf_sample *sample,
1259 struct perf_session *session,
1260 int print_sym, int print_dso)
1262 struct addr_location al;
1263 const char *symname, *dsoname;
1264 struct callchain_cursor *cursor = &evsel->hists.callchain_cursor;
1265 struct callchain_cursor_node *node;
1267 if (perf_event__preprocess_sample(event, session, &al, sample,
1269 error("problem processing %d event, skipping it.\n",
1270 event->header.type);
1274 if (symbol_conf.use_callchain && sample->callchain) {
1276 if (perf_session__resolve_callchain(session, evsel, al.thread,
1277 sample->callchain, NULL) != 0) {
1279 error("Failed to resolve callchain. Skipping\n");
1282 callchain_cursor_commit(cursor);
1285 node = callchain_cursor_current(cursor);
1289 printf("\t%16" PRIx64, node->ip);
1291 if (node->sym && node->sym->name)
1292 symname = node->sym->name;
1296 printf(" %s", symname);
1299 if (node->map && node->map->dso && node->map->dso->name)
1300 dsoname = node->map->dso->name;
1304 printf(" (%s)", dsoname);
1308 callchain_cursor_advance(cursor);
1312 printf("%16" PRIx64, sample->ip);
1314 if (al.sym && al.sym->name)
1315 symname = al.sym->name;
1319 printf(" %s", symname);
1323 if (al.map && al.map->dso && al.map->dso->name)
1324 dsoname = al.map->dso->name;
1328 printf(" (%s)", dsoname);
1333 int perf_session__cpu_bitmap(struct perf_session *session,
1334 const char *cpu_list, unsigned long *cpu_bitmap)
1337 struct cpu_map *map;
1339 for (i = 0; i < PERF_TYPE_MAX; ++i) {
1340 struct perf_evsel *evsel;
1342 evsel = perf_session__find_first_evtype(session, i);
1346 if (!(evsel->attr.sample_type & PERF_SAMPLE_CPU)) {
1347 pr_err("File does not contain CPU events. "
1348 "Remove -c option to proceed.\n");
1353 map = cpu_map__new(cpu_list);
1355 for (i = 0; i < map->nr; i++) {
1356 int cpu = map->map[i];
1358 if (cpu >= MAX_NR_CPUS) {
1359 pr_err("Requested CPU %d too large. "
1360 "Consider raising MAX_NR_CPUS\n", cpu);
1364 set_bit(cpu, cpu_bitmap);
1370 void perf_session__fprintf_info(struct perf_session *session, FILE *fp,
1376 if (session == NULL || fp == NULL)
1379 ret = fstat(session->fd, &st);
1383 fprintf(fp, "# ========\n");
1384 fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
1385 perf_header__fprintf_info(session, fp, full);
1386 fprintf(fp, "# ========\n#\n");