gma500: move the i2c code
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / tools / perf / util / session.c
blobf5a8fbdd3f76b51f9b5abf0bd551c964c3d301ed
1 #define _FILE_OFFSET_BITS 64
3 #include <linux/kernel.h>
5 #include <byteswap.h>
6 #include <unistd.h>
7 #include <sys/types.h>
8 #include <sys/mman.h>
10 #include "evlist.h"
11 #include "evsel.h"
12 #include "session.h"
13 #include "sort.h"
14 #include "util.h"
16 static int perf_session__open(struct perf_session *self, bool force)
18 struct stat input_stat;
20 if (!strcmp(self->filename, "-")) {
21 self->fd_pipe = true;
22 self->fd = STDIN_FILENO;
24 if (perf_session__read_header(self, self->fd) < 0)
25 pr_err("incompatible file format");
27 return 0;
30 self->fd = open(self->filename, O_RDONLY);
31 if (self->fd < 0) {
32 int err = errno;
34 pr_err("failed to open %s: %s", self->filename, strerror(err));
35 if (err == ENOENT && !strcmp(self->filename, "perf.data"))
36 pr_err(" (try 'perf record' first)");
37 pr_err("\n");
38 return -errno;
41 if (fstat(self->fd, &input_stat) < 0)
42 goto out_close;
44 if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
45 pr_err("file %s not owned by current user or root\n",
46 self->filename);
47 goto out_close;
50 if (!input_stat.st_size) {
51 pr_info("zero-sized file (%s), nothing to do!\n",
52 self->filename);
53 goto out_close;
56 if (perf_session__read_header(self, self->fd) < 0) {
57 pr_err("incompatible file format");
58 goto out_close;
61 if (!perf_evlist__valid_sample_type(self->evlist)) {
62 pr_err("non matching sample_type");
63 goto out_close;
66 if (!perf_evlist__valid_sample_id_all(self->evlist)) {
67 pr_err("non matching sample_id_all");
68 goto out_close;
71 self->size = input_stat.st_size;
72 return 0;
74 out_close:
75 close(self->fd);
76 self->fd = -1;
77 return -1;
80 static void perf_session__id_header_size(struct perf_session *session)
82 struct perf_sample *data;
83 u64 sample_type = session->sample_type;
84 u16 size = 0;
86 if (!session->sample_id_all)
87 goto out;
89 if (sample_type & PERF_SAMPLE_TID)
90 size += sizeof(data->tid) * 2;
92 if (sample_type & PERF_SAMPLE_TIME)
93 size += sizeof(data->time);
95 if (sample_type & PERF_SAMPLE_ID)
96 size += sizeof(data->id);
98 if (sample_type & PERF_SAMPLE_STREAM_ID)
99 size += sizeof(data->stream_id);
101 if (sample_type & PERF_SAMPLE_CPU)
102 size += sizeof(data->cpu) * 2;
103 out:
104 session->id_hdr_size = size;
107 void perf_session__update_sample_type(struct perf_session *self)
109 self->sample_type = perf_evlist__sample_type(self->evlist);
110 self->sample_size = __perf_evsel__sample_size(self->sample_type);
111 self->sample_id_all = perf_evlist__sample_id_all(self->evlist);
112 perf_session__id_header_size(self);
115 int perf_session__create_kernel_maps(struct perf_session *self)
117 int ret = machine__create_kernel_maps(&self->host_machine);
119 if (ret >= 0)
120 ret = machines__create_guest_kernel_maps(&self->machines);
121 return ret;
124 static void perf_session__destroy_kernel_maps(struct perf_session *self)
126 machine__destroy_kernel_maps(&self->host_machine);
127 machines__destroy_guest_kernel_maps(&self->machines);
130 struct perf_session *perf_session__new(const char *filename, int mode,
131 bool force, bool repipe,
132 struct perf_event_ops *ops)
134 size_t len = filename ? strlen(filename) + 1 : 0;
135 struct perf_session *self = zalloc(sizeof(*self) + len);
137 if (self == NULL)
138 goto out;
140 memcpy(self->filename, filename, len);
141 self->threads = RB_ROOT;
142 INIT_LIST_HEAD(&self->dead_threads);
143 self->last_match = NULL;
145 * On 64bit we can mmap the data file in one go. No need for tiny mmap
146 * slices. On 32bit we use 32MB.
148 #if BITS_PER_LONG == 64
149 self->mmap_window = ULLONG_MAX;
150 #else
151 self->mmap_window = 32 * 1024 * 1024ULL;
152 #endif
153 self->machines = RB_ROOT;
154 self->repipe = repipe;
155 INIT_LIST_HEAD(&self->ordered_samples.samples);
156 INIT_LIST_HEAD(&self->ordered_samples.sample_cache);
157 INIT_LIST_HEAD(&self->ordered_samples.to_free);
158 machine__init(&self->host_machine, "", HOST_KERNEL_ID);
160 if (mode == O_RDONLY) {
161 if (perf_session__open(self, force) < 0)
162 goto out_delete;
163 perf_session__update_sample_type(self);
164 } else if (mode == O_WRONLY) {
166 * In O_RDONLY mode this will be performed when reading the
167 * kernel MMAP event, in perf_event__process_mmap().
169 if (perf_session__create_kernel_maps(self) < 0)
170 goto out_delete;
173 if (ops && ops->ordering_requires_timestamps &&
174 ops->ordered_samples && !self->sample_id_all) {
175 dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n");
176 ops->ordered_samples = false;
179 out:
180 return self;
181 out_delete:
182 perf_session__delete(self);
183 return NULL;
186 static void perf_session__delete_dead_threads(struct perf_session *self)
188 struct thread *n, *t;
190 list_for_each_entry_safe(t, n, &self->dead_threads, node) {
191 list_del(&t->node);
192 thread__delete(t);
196 static void perf_session__delete_threads(struct perf_session *self)
198 struct rb_node *nd = rb_first(&self->threads);
200 while (nd) {
201 struct thread *t = rb_entry(nd, struct thread, rb_node);
203 rb_erase(&t->rb_node, &self->threads);
204 nd = rb_next(nd);
205 thread__delete(t);
209 void perf_session__delete(struct perf_session *self)
211 perf_session__destroy_kernel_maps(self);
212 perf_session__delete_dead_threads(self);
213 perf_session__delete_threads(self);
214 machine__exit(&self->host_machine);
215 close(self->fd);
216 free(self);
219 void perf_session__remove_thread(struct perf_session *self, struct thread *th)
221 self->last_match = NULL;
222 rb_erase(&th->rb_node, &self->threads);
224 * We may have references to this thread, for instance in some hist_entry
225 * instances, so just move them to a separate list.
227 list_add_tail(&th->node, &self->dead_threads);
230 static bool symbol__match_parent_regex(struct symbol *sym)
232 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
233 return 1;
235 return 0;
238 int perf_session__resolve_callchain(struct perf_session *self,
239 struct thread *thread,
240 struct ip_callchain *chain,
241 struct symbol **parent)
243 u8 cpumode = PERF_RECORD_MISC_USER;
244 unsigned int i;
245 int err;
247 callchain_cursor_reset(&self->callchain_cursor);
249 for (i = 0; i < chain->nr; i++) {
250 u64 ip = chain->ips[i];
251 struct addr_location al;
253 if (ip >= PERF_CONTEXT_MAX) {
254 switch (ip) {
255 case PERF_CONTEXT_HV:
256 cpumode = PERF_RECORD_MISC_HYPERVISOR; break;
257 case PERF_CONTEXT_KERNEL:
258 cpumode = PERF_RECORD_MISC_KERNEL; break;
259 case PERF_CONTEXT_USER:
260 cpumode = PERF_RECORD_MISC_USER; break;
261 default:
262 break;
264 continue;
267 al.filtered = false;
268 thread__find_addr_location(thread, self, cpumode,
269 MAP__FUNCTION, thread->pid, ip, &al, NULL);
270 if (al.sym != NULL) {
271 if (sort__has_parent && !*parent &&
272 symbol__match_parent_regex(al.sym))
273 *parent = al.sym;
274 if (!symbol_conf.use_callchain)
275 break;
278 err = callchain_cursor_append(&self->callchain_cursor,
279 ip, al.map, al.sym);
280 if (err)
281 return err;
284 return 0;
287 static int process_event_synth_stub(union perf_event *event __used,
288 struct perf_session *session __used)
290 dump_printf(": unhandled!\n");
291 return 0;
294 static int process_event_sample_stub(union perf_event *event __used,
295 struct perf_sample *sample __used,
296 struct perf_evsel *evsel __used,
297 struct perf_session *session __used)
299 dump_printf(": unhandled!\n");
300 return 0;
303 static int process_event_stub(union perf_event *event __used,
304 struct perf_sample *sample __used,
305 struct perf_session *session __used)
307 dump_printf(": unhandled!\n");
308 return 0;
311 static int process_finished_round_stub(union perf_event *event __used,
312 struct perf_session *session __used,
313 struct perf_event_ops *ops __used)
315 dump_printf(": unhandled!\n");
316 return 0;
319 static int process_finished_round(union perf_event *event,
320 struct perf_session *session,
321 struct perf_event_ops *ops);
323 static void perf_event_ops__fill_defaults(struct perf_event_ops *handler)
325 if (handler->sample == NULL)
326 handler->sample = process_event_sample_stub;
327 if (handler->mmap == NULL)
328 handler->mmap = process_event_stub;
329 if (handler->comm == NULL)
330 handler->comm = process_event_stub;
331 if (handler->fork == NULL)
332 handler->fork = process_event_stub;
333 if (handler->exit == NULL)
334 handler->exit = process_event_stub;
335 if (handler->lost == NULL)
336 handler->lost = perf_event__process_lost;
337 if (handler->read == NULL)
338 handler->read = process_event_stub;
339 if (handler->throttle == NULL)
340 handler->throttle = process_event_stub;
341 if (handler->unthrottle == NULL)
342 handler->unthrottle = process_event_stub;
343 if (handler->attr == NULL)
344 handler->attr = process_event_synth_stub;
345 if (handler->event_type == NULL)
346 handler->event_type = process_event_synth_stub;
347 if (handler->tracing_data == NULL)
348 handler->tracing_data = process_event_synth_stub;
349 if (handler->build_id == NULL)
350 handler->build_id = process_event_synth_stub;
351 if (handler->finished_round == NULL) {
352 if (handler->ordered_samples)
353 handler->finished_round = process_finished_round;
354 else
355 handler->finished_round = process_finished_round_stub;
359 void mem_bswap_64(void *src, int byte_size)
361 u64 *m = src;
363 while (byte_size > 0) {
364 *m = bswap_64(*m);
365 byte_size -= sizeof(u64);
366 ++m;
370 static void perf_event__all64_swap(union perf_event *event)
372 struct perf_event_header *hdr = &event->header;
373 mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr));
376 static void perf_event__comm_swap(union perf_event *event)
378 event->comm.pid = bswap_32(event->comm.pid);
379 event->comm.tid = bswap_32(event->comm.tid);
382 static void perf_event__mmap_swap(union perf_event *event)
384 event->mmap.pid = bswap_32(event->mmap.pid);
385 event->mmap.tid = bswap_32(event->mmap.tid);
386 event->mmap.start = bswap_64(event->mmap.start);
387 event->mmap.len = bswap_64(event->mmap.len);
388 event->mmap.pgoff = bswap_64(event->mmap.pgoff);
391 static void perf_event__task_swap(union perf_event *event)
393 event->fork.pid = bswap_32(event->fork.pid);
394 event->fork.tid = bswap_32(event->fork.tid);
395 event->fork.ppid = bswap_32(event->fork.ppid);
396 event->fork.ptid = bswap_32(event->fork.ptid);
397 event->fork.time = bswap_64(event->fork.time);
400 static void perf_event__read_swap(union perf_event *event)
402 event->read.pid = bswap_32(event->read.pid);
403 event->read.tid = bswap_32(event->read.tid);
404 event->read.value = bswap_64(event->read.value);
405 event->read.time_enabled = bswap_64(event->read.time_enabled);
406 event->read.time_running = bswap_64(event->read.time_running);
407 event->read.id = bswap_64(event->read.id);
410 static void perf_event__attr_swap(union perf_event *event)
412 size_t size;
414 event->attr.attr.type = bswap_32(event->attr.attr.type);
415 event->attr.attr.size = bswap_32(event->attr.attr.size);
416 event->attr.attr.config = bswap_64(event->attr.attr.config);
417 event->attr.attr.sample_period = bswap_64(event->attr.attr.sample_period);
418 event->attr.attr.sample_type = bswap_64(event->attr.attr.sample_type);
419 event->attr.attr.read_format = bswap_64(event->attr.attr.read_format);
420 event->attr.attr.wakeup_events = bswap_32(event->attr.attr.wakeup_events);
421 event->attr.attr.bp_type = bswap_32(event->attr.attr.bp_type);
422 event->attr.attr.bp_addr = bswap_64(event->attr.attr.bp_addr);
423 event->attr.attr.bp_len = bswap_64(event->attr.attr.bp_len);
425 size = event->header.size;
426 size -= (void *)&event->attr.id - (void *)event;
427 mem_bswap_64(event->attr.id, size);
430 static void perf_event__event_type_swap(union perf_event *event)
432 event->event_type.event_type.event_id =
433 bswap_64(event->event_type.event_type.event_id);
436 static void perf_event__tracing_data_swap(union perf_event *event)
438 event->tracing_data.size = bswap_32(event->tracing_data.size);
441 typedef void (*perf_event__swap_op)(union perf_event *event);
443 static perf_event__swap_op perf_event__swap_ops[] = {
444 [PERF_RECORD_MMAP] = perf_event__mmap_swap,
445 [PERF_RECORD_COMM] = perf_event__comm_swap,
446 [PERF_RECORD_FORK] = perf_event__task_swap,
447 [PERF_RECORD_EXIT] = perf_event__task_swap,
448 [PERF_RECORD_LOST] = perf_event__all64_swap,
449 [PERF_RECORD_READ] = perf_event__read_swap,
450 [PERF_RECORD_SAMPLE] = perf_event__all64_swap,
451 [PERF_RECORD_HEADER_ATTR] = perf_event__attr_swap,
452 [PERF_RECORD_HEADER_EVENT_TYPE] = perf_event__event_type_swap,
453 [PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap,
454 [PERF_RECORD_HEADER_BUILD_ID] = NULL,
455 [PERF_RECORD_HEADER_MAX] = NULL,
458 struct sample_queue {
459 u64 timestamp;
460 u64 file_offset;
461 union perf_event *event;
462 struct list_head list;
465 static void perf_session_free_sample_buffers(struct perf_session *session)
467 struct ordered_samples *os = &session->ordered_samples;
469 while (!list_empty(&os->to_free)) {
470 struct sample_queue *sq;
472 sq = list_entry(os->to_free.next, struct sample_queue, list);
473 list_del(&sq->list);
474 free(sq);
478 static int perf_session_deliver_event(struct perf_session *session,
479 union perf_event *event,
480 struct perf_sample *sample,
481 struct perf_event_ops *ops,
482 u64 file_offset);
484 static void flush_sample_queue(struct perf_session *s,
485 struct perf_event_ops *ops)
487 struct ordered_samples *os = &s->ordered_samples;
488 struct list_head *head = &os->samples;
489 struct sample_queue *tmp, *iter;
490 struct perf_sample sample;
491 u64 limit = os->next_flush;
492 u64 last_ts = os->last_sample ? os->last_sample->timestamp : 0ULL;
493 int ret;
495 if (!ops->ordered_samples || !limit)
496 return;
498 list_for_each_entry_safe(iter, tmp, head, list) {
499 if (iter->timestamp > limit)
500 break;
502 ret = perf_session__parse_sample(s, iter->event, &sample);
503 if (ret)
504 pr_err("Can't parse sample, err = %d\n", ret);
505 else
506 perf_session_deliver_event(s, iter->event, &sample, ops,
507 iter->file_offset);
509 os->last_flush = iter->timestamp;
510 list_del(&iter->list);
511 list_add(&iter->list, &os->sample_cache);
514 if (list_empty(head)) {
515 os->last_sample = NULL;
516 } else if (last_ts <= limit) {
517 os->last_sample =
518 list_entry(head->prev, struct sample_queue, list);
523 * When perf record finishes a pass on every buffers, it records this pseudo
524 * event.
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 =================
533 * CPU 0 | CPU 1
535 * cnt1 timestamps | cnt2 timestamps
536 * 1 | 2
537 * 2 | 3
538 * - | 4 <--- max recorded
540 * ============ PASS n + 1 ==============
541 * CPU 0 | CPU 1
543 * cnt1 timestamps | cnt2 timestamps
544 * 3 | 5
545 * 4 | 6
546 * 5 | 7 <---- max recorded
548 * Flush every events below timestamp 4
550 * ============ PASS n + 2 ==============
551 * CPU 0 | CPU 1
553 * cnt1 timestamps | cnt2 timestamps
554 * 6 | 8
555 * 7 | 9
556 * - | 10
558 * Flush every events below timestamp 7
559 * etc...
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;
568 return 0;
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;
577 struct list_head *p;
579 os->last_sample = new;
581 if (!sample) {
582 list_add(&new->list, &os->samples);
583 os->max_timestamp = timestamp;
584 return;
588 * last_sample might point to some random place in the list as it's
589 * the last queued event. We expect that the new event is close to
590 * this.
592 if (sample->timestamp <= timestamp) {
593 while (sample->timestamp <= timestamp) {
594 p = sample->list.next;
595 if (p == &os->samples) {
596 list_add_tail(&new->list, &os->samples);
597 os->max_timestamp = timestamp;
598 return;
600 sample = list_entry(p, struct sample_queue, list);
602 list_add_tail(&new->list, &sample->list);
603 } else {
604 while (sample->timestamp > timestamp) {
605 p = sample->list.prev;
606 if (p == &os->samples) {
607 list_add(&new->list, &os->samples);
608 return;
610 sample = list_entry(p, struct sample_queue, list);
612 list_add(&new->list, &sample->list);
616 #define MAX_SAMPLE_BUFFER (64 * 1024 / sizeof(struct sample_queue))
618 static int perf_session_queue_event(struct perf_session *s, union perf_event *event,
619 struct perf_sample *sample, u64 file_offset)
621 struct ordered_samples *os = &s->ordered_samples;
622 struct list_head *sc = &os->sample_cache;
623 u64 timestamp = sample->time;
624 struct sample_queue *new;
626 if (!timestamp || timestamp == ~0ULL)
627 return -ETIME;
629 if (timestamp < s->ordered_samples.last_flush) {
630 printf("Warning: Timestamp below last timeslice flush\n");
631 return -EINVAL;
634 if (!list_empty(sc)) {
635 new = list_entry(sc->next, struct sample_queue, list);
636 list_del(&new->list);
637 } else if (os->sample_buffer) {
638 new = os->sample_buffer + os->sample_buffer_idx;
639 if (++os->sample_buffer_idx == MAX_SAMPLE_BUFFER)
640 os->sample_buffer = NULL;
641 } else {
642 os->sample_buffer = malloc(MAX_SAMPLE_BUFFER * sizeof(*new));
643 if (!os->sample_buffer)
644 return -ENOMEM;
645 list_add(&os->sample_buffer->list, &os->to_free);
646 os->sample_buffer_idx = 2;
647 new = os->sample_buffer + 1;
650 new->timestamp = timestamp;
651 new->file_offset = file_offset;
652 new->event = event;
654 __queue_event(new, s);
656 return 0;
659 static void callchain__printf(struct perf_sample *sample)
661 unsigned int i;
663 printf("... chain: nr:%" PRIu64 "\n", sample->callchain->nr);
665 for (i = 0; i < sample->callchain->nr; i++)
666 printf("..... %2d: %016" PRIx64 "\n",
667 i, sample->callchain->ips[i]);
670 static void perf_session__print_tstamp(struct perf_session *session,
671 union perf_event *event,
672 struct perf_sample *sample)
674 if (event->header.type != PERF_RECORD_SAMPLE &&
675 !session->sample_id_all) {
676 fputs("-1 -1 ", stdout);
677 return;
680 if ((session->sample_type & PERF_SAMPLE_CPU))
681 printf("%u ", sample->cpu);
683 if (session->sample_type & PERF_SAMPLE_TIME)
684 printf("%" PRIu64 " ", sample->time);
687 static void dump_event(struct perf_session *session, union perf_event *event,
688 u64 file_offset, struct perf_sample *sample)
690 if (!dump_trace)
691 return;
693 printf("\n%#" PRIx64 " [%#x]: event: %d\n",
694 file_offset, event->header.size, event->header.type);
696 trace_event(event);
698 if (sample)
699 perf_session__print_tstamp(session, event, sample);
701 printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset,
702 event->header.size, perf_event__name(event->header.type));
705 static void dump_sample(struct perf_session *session, union perf_event *event,
706 struct perf_sample *sample)
708 if (!dump_trace)
709 return;
711 printf("(IP, %d): %d/%d: %#" PRIx64 " period: %" PRIu64 "\n",
712 event->header.misc, sample->pid, sample->tid, sample->ip,
713 sample->period);
715 if (session->sample_type & PERF_SAMPLE_CALLCHAIN)
716 callchain__printf(sample);
719 static int perf_session_deliver_event(struct perf_session *session,
720 union perf_event *event,
721 struct perf_sample *sample,
722 struct perf_event_ops *ops,
723 u64 file_offset)
725 struct perf_evsel *evsel;
727 dump_event(session, event, file_offset, sample);
729 switch (event->header.type) {
730 case PERF_RECORD_SAMPLE:
731 dump_sample(session, event, sample);
732 evsel = perf_evlist__id2evsel(session->evlist, sample->id);
733 if (evsel == NULL) {
734 ++session->hists.stats.nr_unknown_id;
735 return -1;
737 return ops->sample(event, sample, evsel, session);
738 case PERF_RECORD_MMAP:
739 return ops->mmap(event, sample, session);
740 case PERF_RECORD_COMM:
741 return ops->comm(event, sample, session);
742 case PERF_RECORD_FORK:
743 return ops->fork(event, sample, session);
744 case PERF_RECORD_EXIT:
745 return ops->exit(event, sample, session);
746 case PERF_RECORD_LOST:
747 return ops->lost(event, sample, session);
748 case PERF_RECORD_READ:
749 return ops->read(event, sample, session);
750 case PERF_RECORD_THROTTLE:
751 return ops->throttle(event, sample, session);
752 case PERF_RECORD_UNTHROTTLE:
753 return ops->unthrottle(event, sample, session);
754 default:
755 ++session->hists.stats.nr_unknown_events;
756 return -1;
760 static int perf_session__preprocess_sample(struct perf_session *session,
761 union perf_event *event, struct perf_sample *sample)
763 if (event->header.type != PERF_RECORD_SAMPLE ||
764 !(session->sample_type & PERF_SAMPLE_CALLCHAIN))
765 return 0;
767 if (!ip_callchain__valid(sample->callchain, event)) {
768 pr_debug("call-chain problem with event, skipping it.\n");
769 ++session->hists.stats.nr_invalid_chains;
770 session->hists.stats.total_invalid_chains += sample->period;
771 return -EINVAL;
773 return 0;
776 static int perf_session__process_user_event(struct perf_session *session, union perf_event *event,
777 struct perf_event_ops *ops, u64 file_offset)
779 dump_event(session, event, file_offset, NULL);
781 /* These events are processed right away */
782 switch (event->header.type) {
783 case PERF_RECORD_HEADER_ATTR:
784 return ops->attr(event, session);
785 case PERF_RECORD_HEADER_EVENT_TYPE:
786 return ops->event_type(event, session);
787 case PERF_RECORD_HEADER_TRACING_DATA:
788 /* setup for reading amidst mmap */
789 lseek(session->fd, file_offset, SEEK_SET);
790 return ops->tracing_data(event, session);
791 case PERF_RECORD_HEADER_BUILD_ID:
792 return ops->build_id(event, session);
793 case PERF_RECORD_FINISHED_ROUND:
794 return ops->finished_round(event, session, ops);
795 default:
796 return -EINVAL;
800 static int perf_session__process_event(struct perf_session *session,
801 union perf_event *event,
802 struct perf_event_ops *ops,
803 u64 file_offset)
805 struct perf_sample sample;
806 int ret;
808 if (session->header.needs_swap &&
809 perf_event__swap_ops[event->header.type])
810 perf_event__swap_ops[event->header.type](event);
812 if (event->header.type >= PERF_RECORD_HEADER_MAX)
813 return -EINVAL;
815 hists__inc_nr_events(&session->hists, event->header.type);
817 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
818 return perf_session__process_user_event(session, event, ops, file_offset);
821 * For all kernel events we get the sample data
823 ret = perf_session__parse_sample(session, event, &sample);
824 if (ret)
825 return ret;
827 /* Preprocess sample records - precheck callchains */
828 if (perf_session__preprocess_sample(session, event, &sample))
829 return 0;
831 if (ops->ordered_samples) {
832 ret = perf_session_queue_event(session, event, &sample,
833 file_offset);
834 if (ret != -ETIME)
835 return ret;
838 return perf_session_deliver_event(session, event, &sample, ops,
839 file_offset);
842 void perf_event_header__bswap(struct perf_event_header *self)
844 self->type = bswap_32(self->type);
845 self->misc = bswap_16(self->misc);
846 self->size = bswap_16(self->size);
849 static struct thread *perf_session__register_idle_thread(struct perf_session *self)
851 struct thread *thread = perf_session__findnew(self, 0);
853 if (thread == NULL || thread__set_comm(thread, "swapper")) {
854 pr_err("problem inserting idle task.\n");
855 thread = NULL;
858 return thread;
861 static void perf_session__warn_about_errors(const struct perf_session *session,
862 const struct perf_event_ops *ops)
864 if (ops->lost == perf_event__process_lost &&
865 session->hists.stats.total_lost != 0) {
866 ui__warning("Processed %" PRIu64 " events and LOST %" PRIu64
867 "!\n\nCheck IO/CPU overload!\n\n",
868 session->hists.stats.total_period,
869 session->hists.stats.total_lost);
872 if (session->hists.stats.nr_unknown_events != 0) {
873 ui__warning("Found %u unknown events!\n\n"
874 "Is this an older tool processing a perf.data "
875 "file generated by a more recent tool?\n\n"
876 "If that is not the case, consider "
877 "reporting to linux-kernel@vger.kernel.org.\n\n",
878 session->hists.stats.nr_unknown_events);
881 if (session->hists.stats.nr_unknown_id != 0) {
882 ui__warning("%u samples with id not present in the header\n",
883 session->hists.stats.nr_unknown_id);
886 if (session->hists.stats.nr_invalid_chains != 0) {
887 ui__warning("Found invalid callchains!\n\n"
888 "%u out of %u events were discarded for this reason.\n\n"
889 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
890 session->hists.stats.nr_invalid_chains,
891 session->hists.stats.nr_events[PERF_RECORD_SAMPLE]);
895 #define session_done() (*(volatile int *)(&session_done))
896 volatile int session_done;
898 static int __perf_session__process_pipe_events(struct perf_session *self,
899 struct perf_event_ops *ops)
901 union perf_event event;
902 uint32_t size;
903 int skip = 0;
904 u64 head;
905 int err;
906 void *p;
908 perf_event_ops__fill_defaults(ops);
910 head = 0;
911 more:
912 err = readn(self->fd, &event, sizeof(struct perf_event_header));
913 if (err <= 0) {
914 if (err == 0)
915 goto done;
917 pr_err("failed to read event header\n");
918 goto out_err;
921 if (self->header.needs_swap)
922 perf_event_header__bswap(&event.header);
924 size = event.header.size;
925 if (size == 0)
926 size = 8;
928 p = &event;
929 p += sizeof(struct perf_event_header);
931 if (size - sizeof(struct perf_event_header)) {
932 err = readn(self->fd, p, size - sizeof(struct perf_event_header));
933 if (err <= 0) {
934 if (err == 0) {
935 pr_err("unexpected end of event stream\n");
936 goto done;
939 pr_err("failed to read event data\n");
940 goto out_err;
944 if (size == 0 ||
945 (skip = perf_session__process_event(self, &event, ops, head)) < 0) {
946 dump_printf("%#" PRIx64 " [%#x]: skipping unknown header type: %d\n",
947 head, event.header.size, event.header.type);
949 * assume we lost track of the stream, check alignment, and
950 * increment a single u64 in the hope to catch on again 'soon'.
952 if (unlikely(head & 7))
953 head &= ~7ULL;
955 size = 8;
958 head += size;
960 if (skip > 0)
961 head += skip;
963 if (!session_done())
964 goto more;
965 done:
966 err = 0;
967 out_err:
968 perf_session__warn_about_errors(self, ops);
969 perf_session_free_sample_buffers(self);
970 return err;
973 static union perf_event *
974 fetch_mmaped_event(struct perf_session *session,
975 u64 head, size_t mmap_size, char *buf)
977 union perf_event *event;
980 * Ensure we have enough space remaining to read
981 * the size of the event in the headers.
983 if (head + sizeof(event->header) > mmap_size)
984 return NULL;
986 event = (union perf_event *)(buf + head);
988 if (session->header.needs_swap)
989 perf_event_header__bswap(&event->header);
991 if (head + event->header.size > mmap_size)
992 return NULL;
994 return event;
997 int __perf_session__process_events(struct perf_session *session,
998 u64 data_offset, u64 data_size,
999 u64 file_size, struct perf_event_ops *ops)
1001 u64 head, page_offset, file_offset, file_pos, progress_next;
1002 int err, mmap_prot, mmap_flags, map_idx = 0;
1003 struct ui_progress *progress;
1004 size_t page_size, mmap_size;
1005 char *buf, *mmaps[8];
1006 union perf_event *event;
1007 uint32_t size;
1009 perf_event_ops__fill_defaults(ops);
1011 page_size = sysconf(_SC_PAGESIZE);
1013 page_offset = page_size * (data_offset / page_size);
1014 file_offset = page_offset;
1015 head = data_offset - page_offset;
1017 if (data_offset + data_size < file_size)
1018 file_size = data_offset + data_size;
1020 progress_next = file_size / 16;
1021 progress = ui_progress__new("Processing events...", file_size);
1022 if (progress == NULL)
1023 return -1;
1025 mmap_size = session->mmap_window;
1026 if (mmap_size > file_size)
1027 mmap_size = file_size;
1029 memset(mmaps, 0, sizeof(mmaps));
1031 mmap_prot = PROT_READ;
1032 mmap_flags = MAP_SHARED;
1034 if (session->header.needs_swap) {
1035 mmap_prot |= PROT_WRITE;
1036 mmap_flags = MAP_PRIVATE;
1038 remap:
1039 buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, session->fd,
1040 file_offset);
1041 if (buf == MAP_FAILED) {
1042 pr_err("failed to mmap file\n");
1043 err = -errno;
1044 goto out_err;
1046 mmaps[map_idx] = buf;
1047 map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
1048 file_pos = file_offset + head;
1050 more:
1051 event = fetch_mmaped_event(session, head, mmap_size, buf);
1052 if (!event) {
1053 if (mmaps[map_idx]) {
1054 munmap(mmaps[map_idx], mmap_size);
1055 mmaps[map_idx] = NULL;
1058 page_offset = page_size * (head / page_size);
1059 file_offset += page_offset;
1060 head -= page_offset;
1061 goto remap;
1064 size = event->header.size;
1066 if (size == 0 ||
1067 perf_session__process_event(session, event, ops, file_pos) < 0) {
1068 dump_printf("%#" PRIx64 " [%#x]: skipping unknown header type: %d\n",
1069 file_offset + head, event->header.size,
1070 event->header.type);
1072 * assume we lost track of the stream, check alignment, and
1073 * increment a single u64 in the hope to catch on again 'soon'.
1075 if (unlikely(head & 7))
1076 head &= ~7ULL;
1078 size = 8;
1081 head += size;
1082 file_pos += size;
1084 if (file_pos >= progress_next) {
1085 progress_next += file_size / 16;
1086 ui_progress__update(progress, file_pos);
1089 if (file_pos < file_size)
1090 goto more;
1092 err = 0;
1093 /* do the final flush for ordered samples */
1094 session->ordered_samples.next_flush = ULLONG_MAX;
1095 flush_sample_queue(session, ops);
1096 out_err:
1097 ui_progress__delete(progress);
1098 perf_session__warn_about_errors(session, ops);
1099 perf_session_free_sample_buffers(session);
1100 return err;
1103 int perf_session__process_events(struct perf_session *self,
1104 struct perf_event_ops *ops)
1106 int err;
1108 if (perf_session__register_idle_thread(self) == NULL)
1109 return -ENOMEM;
1111 if (!self->fd_pipe)
1112 err = __perf_session__process_events(self,
1113 self->header.data_offset,
1114 self->header.data_size,
1115 self->size, ops);
1116 else
1117 err = __perf_session__process_pipe_events(self, ops);
1119 return err;
1122 bool perf_session__has_traces(struct perf_session *self, const char *msg)
1124 if (!(self->sample_type & PERF_SAMPLE_RAW)) {
1125 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
1126 return false;
1129 return true;
1132 int perf_session__set_kallsyms_ref_reloc_sym(struct map **maps,
1133 const char *symbol_name,
1134 u64 addr)
1136 char *bracket;
1137 enum map_type i;
1138 struct ref_reloc_sym *ref;
1140 ref = zalloc(sizeof(struct ref_reloc_sym));
1141 if (ref == NULL)
1142 return -ENOMEM;
1144 ref->name = strdup(symbol_name);
1145 if (ref->name == NULL) {
1146 free(ref);
1147 return -ENOMEM;
1150 bracket = strchr(ref->name, ']');
1151 if (bracket)
1152 *bracket = '\0';
1154 ref->addr = addr;
1156 for (i = 0; i < MAP__NR_TYPES; ++i) {
1157 struct kmap *kmap = map__kmap(maps[i]);
1158 kmap->ref_reloc_sym = ref;
1161 return 0;
1164 size_t perf_session__fprintf_dsos(struct perf_session *self, FILE *fp)
1166 return __dsos__fprintf(&self->host_machine.kernel_dsos, fp) +
1167 __dsos__fprintf(&self->host_machine.user_dsos, fp) +
1168 machines__fprintf_dsos(&self->machines, fp);
1171 size_t perf_session__fprintf_dsos_buildid(struct perf_session *self, FILE *fp,
1172 bool with_hits)
1174 size_t ret = machine__fprintf_dsos_buildid(&self->host_machine, fp, with_hits);
1175 return ret + machines__fprintf_dsos_buildid(&self->machines, fp, with_hits);
1178 size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp)
1180 struct perf_evsel *pos;
1181 size_t ret = fprintf(fp, "Aggregated stats:\n");
1183 ret += hists__fprintf_nr_events(&session->hists, fp);
1185 list_for_each_entry(pos, &session->evlist->entries, node) {
1186 ret += fprintf(fp, "%s stats:\n", event_name(pos));
1187 ret += hists__fprintf_nr_events(&pos->hists, fp);
1190 return ret;
1193 struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
1194 unsigned int type)
1196 struct perf_evsel *pos;
1198 list_for_each_entry(pos, &session->evlist->entries, node) {
1199 if (pos->attr.type == type)
1200 return pos;
1202 return NULL;
1205 void perf_session__print_symbols(union perf_event *event,
1206 struct perf_sample *sample,
1207 struct perf_session *session)
1209 struct addr_location al;
1210 const char *symname, *dsoname;
1211 struct callchain_cursor *cursor = &session->callchain_cursor;
1212 struct callchain_cursor_node *node;
1214 if (perf_event__preprocess_sample(event, session, &al, sample,
1215 NULL) < 0) {
1216 error("problem processing %d event, skipping it.\n",
1217 event->header.type);
1218 return;
1221 if (symbol_conf.use_callchain && sample->callchain) {
1223 if (perf_session__resolve_callchain(session, al.thread,
1224 sample->callchain, NULL) != 0) {
1225 if (verbose)
1226 error("Failed to resolve callchain. Skipping\n");
1227 return;
1229 callchain_cursor_commit(cursor);
1231 while (1) {
1232 node = callchain_cursor_current(cursor);
1233 if (!node)
1234 break;
1236 if (node->sym && node->sym->name)
1237 symname = node->sym->name;
1238 else
1239 symname = "";
1241 if (node->map && node->map->dso && node->map->dso->name)
1242 dsoname = node->map->dso->name;
1243 else
1244 dsoname = "";
1246 printf("\t%16" PRIx64 " %s (%s)\n", node->ip, symname, dsoname);
1248 callchain_cursor_advance(cursor);
1251 } else {
1252 if (al.sym && al.sym->name)
1253 symname = al.sym->name;
1254 else
1255 symname = "";
1257 if (al.map && al.map->dso && al.map->dso->name)
1258 dsoname = al.map->dso->name;
1259 else
1260 dsoname = "";
1262 printf("%16" PRIx64 " %s (%s)", al.addr, symname, dsoname);