1 #include <linux/kernel.h>
2 #include <traceevent/event-parse.h>
16 #include "perf_regs.h"
19 static int perf_session__open(struct perf_session *session)
21 struct perf_data_file *file = session->file;
23 if (perf_session__read_header(session) < 0) {
24 pr_err("incompatible file format (rerun with -v to learn more)");
28 if (perf_data_file__is_pipe(file))
31 if (!perf_evlist__valid_sample_type(session->evlist)) {
32 pr_err("non matching sample_type");
36 if (!perf_evlist__valid_sample_id_all(session->evlist)) {
37 pr_err("non matching sample_id_all");
41 if (!perf_evlist__valid_read_format(session->evlist)) {
42 pr_err("non matching read_format");
49 void perf_session__set_id_hdr_size(struct perf_session *session)
51 u16 id_hdr_size = perf_evlist__id_hdr_size(session->evlist);
53 machines__set_id_hdr_size(&session->machines, id_hdr_size);
56 int perf_session__create_kernel_maps(struct perf_session *session)
58 int ret = machine__create_kernel_maps(&session->machines.host);
61 ret = machines__create_guest_kernel_maps(&session->machines);
65 static void perf_session__destroy_kernel_maps(struct perf_session *session)
67 machines__destroy_kernel_maps(&session->machines);
70 struct perf_session *perf_session__new(struct perf_data_file *file,
71 bool repipe, struct perf_tool *tool)
73 struct perf_session *session = zalloc(sizeof(*session));
78 session->repipe = repipe;
79 INIT_LIST_HEAD(&session->ordered_events.events);
80 INIT_LIST_HEAD(&session->ordered_events.cache);
81 INIT_LIST_HEAD(&session->ordered_events.to_free);
82 session->ordered_events.max_alloc_size = (u64) -1;
83 session->ordered_events.cur_alloc_size = 0;
84 machines__init(&session->machines);
87 if (perf_data_file__open(file))
92 if (perf_data_file__is_read(file)) {
93 if (perf_session__open(session) < 0)
96 perf_session__set_id_hdr_size(session);
100 if (!file || perf_data_file__is_write(file)) {
102 * In O_RDONLY mode this will be performed when reading the
103 * kernel MMAP event, in perf_event__process_mmap().
105 if (perf_session__create_kernel_maps(session) < 0)
109 if (tool && tool->ordering_requires_timestamps &&
110 tool->ordered_events && !perf_evlist__sample_id_all(session->evlist)) {
111 dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n");
112 tool->ordered_events = false;
118 perf_data_file__close(file);
120 perf_session__delete(session);
125 static void perf_session__delete_dead_threads(struct perf_session *session)
127 machine__delete_dead_threads(&session->machines.host);
130 static void perf_session__delete_threads(struct perf_session *session)
132 machine__delete_threads(&session->machines.host);
135 static void perf_session_env__delete(struct perf_session_env *env)
137 zfree(&env->hostname);
138 zfree(&env->os_release);
139 zfree(&env->version);
141 zfree(&env->cpu_desc);
144 zfree(&env->cmdline);
145 zfree(&env->sibling_cores);
146 zfree(&env->sibling_threads);
147 zfree(&env->numa_nodes);
148 zfree(&env->pmu_mappings);
151 void perf_session__delete(struct perf_session *session)
153 perf_session__destroy_kernel_maps(session);
154 perf_session__delete_dead_threads(session);
155 perf_session__delete_threads(session);
156 perf_session_env__delete(&session->header.env);
157 machines__exit(&session->machines);
159 perf_data_file__close(session->file);
163 static int process_event_synth_tracing_data_stub(struct perf_tool *tool
165 union perf_event *event
167 struct perf_session *session
170 dump_printf(": unhandled!\n");
174 static int process_event_synth_attr_stub(struct perf_tool *tool __maybe_unused,
175 union perf_event *event __maybe_unused,
176 struct perf_evlist **pevlist
179 dump_printf(": unhandled!\n");
183 static int process_event_sample_stub(struct perf_tool *tool __maybe_unused,
184 union perf_event *event __maybe_unused,
185 struct perf_sample *sample __maybe_unused,
186 struct perf_evsel *evsel __maybe_unused,
187 struct machine *machine __maybe_unused)
189 dump_printf(": unhandled!\n");
193 static int process_event_stub(struct perf_tool *tool __maybe_unused,
194 union perf_event *event __maybe_unused,
195 struct perf_sample *sample __maybe_unused,
196 struct machine *machine __maybe_unused)
198 dump_printf(": unhandled!\n");
202 static int process_finished_round_stub(struct perf_tool *tool __maybe_unused,
203 union perf_event *event __maybe_unused,
204 struct perf_session *perf_session
207 dump_printf(": unhandled!\n");
211 static int process_finished_round(struct perf_tool *tool,
212 union perf_event *event,
213 struct perf_session *session);
215 void perf_tool__fill_defaults(struct perf_tool *tool)
217 if (tool->sample == NULL)
218 tool->sample = process_event_sample_stub;
219 if (tool->mmap == NULL)
220 tool->mmap = process_event_stub;
221 if (tool->mmap2 == NULL)
222 tool->mmap2 = process_event_stub;
223 if (tool->comm == NULL)
224 tool->comm = process_event_stub;
225 if (tool->fork == NULL)
226 tool->fork = process_event_stub;
227 if (tool->exit == NULL)
228 tool->exit = process_event_stub;
229 if (tool->lost == NULL)
230 tool->lost = perf_event__process_lost;
231 if (tool->read == NULL)
232 tool->read = process_event_sample_stub;
233 if (tool->throttle == NULL)
234 tool->throttle = process_event_stub;
235 if (tool->unthrottle == NULL)
236 tool->unthrottle = process_event_stub;
237 if (tool->attr == NULL)
238 tool->attr = process_event_synth_attr_stub;
239 if (tool->tracing_data == NULL)
240 tool->tracing_data = process_event_synth_tracing_data_stub;
241 if (tool->build_id == NULL)
242 tool->build_id = process_finished_round_stub;
243 if (tool->finished_round == NULL) {
244 if (tool->ordered_events)
245 tool->finished_round = process_finished_round;
247 tool->finished_round = process_finished_round_stub;
251 static void swap_sample_id_all(union perf_event *event, void *data)
253 void *end = (void *) event + event->header.size;
254 int size = end - data;
256 BUG_ON(size % sizeof(u64));
257 mem_bswap_64(data, size);
260 static void perf_event__all64_swap(union perf_event *event,
261 bool sample_id_all __maybe_unused)
263 struct perf_event_header *hdr = &event->header;
264 mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr));
267 static void perf_event__comm_swap(union perf_event *event, bool sample_id_all)
269 event->comm.pid = bswap_32(event->comm.pid);
270 event->comm.tid = bswap_32(event->comm.tid);
273 void *data = &event->comm.comm;
275 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
276 swap_sample_id_all(event, data);
280 static void perf_event__mmap_swap(union perf_event *event,
283 event->mmap.pid = bswap_32(event->mmap.pid);
284 event->mmap.tid = bswap_32(event->mmap.tid);
285 event->mmap.start = bswap_64(event->mmap.start);
286 event->mmap.len = bswap_64(event->mmap.len);
287 event->mmap.pgoff = bswap_64(event->mmap.pgoff);
290 void *data = &event->mmap.filename;
292 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
293 swap_sample_id_all(event, data);
297 static void perf_event__mmap2_swap(union perf_event *event,
300 event->mmap2.pid = bswap_32(event->mmap2.pid);
301 event->mmap2.tid = bswap_32(event->mmap2.tid);
302 event->mmap2.start = bswap_64(event->mmap2.start);
303 event->mmap2.len = bswap_64(event->mmap2.len);
304 event->mmap2.pgoff = bswap_64(event->mmap2.pgoff);
305 event->mmap2.maj = bswap_32(event->mmap2.maj);
306 event->mmap2.min = bswap_32(event->mmap2.min);
307 event->mmap2.ino = bswap_64(event->mmap2.ino);
310 void *data = &event->mmap2.filename;
312 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
313 swap_sample_id_all(event, data);
316 static void perf_event__task_swap(union perf_event *event, bool sample_id_all)
318 event->fork.pid = bswap_32(event->fork.pid);
319 event->fork.tid = bswap_32(event->fork.tid);
320 event->fork.ppid = bswap_32(event->fork.ppid);
321 event->fork.ptid = bswap_32(event->fork.ptid);
322 event->fork.time = bswap_64(event->fork.time);
325 swap_sample_id_all(event, &event->fork + 1);
328 static void perf_event__read_swap(union perf_event *event, bool sample_id_all)
330 event->read.pid = bswap_32(event->read.pid);
331 event->read.tid = bswap_32(event->read.tid);
332 event->read.value = bswap_64(event->read.value);
333 event->read.time_enabled = bswap_64(event->read.time_enabled);
334 event->read.time_running = bswap_64(event->read.time_running);
335 event->read.id = bswap_64(event->read.id);
338 swap_sample_id_all(event, &event->read + 1);
341 static void perf_event__throttle_swap(union perf_event *event,
344 event->throttle.time = bswap_64(event->throttle.time);
345 event->throttle.id = bswap_64(event->throttle.id);
346 event->throttle.stream_id = bswap_64(event->throttle.stream_id);
349 swap_sample_id_all(event, &event->throttle + 1);
352 static u8 revbyte(u8 b)
354 int rev = (b >> 4) | ((b & 0xf) << 4);
355 rev = ((rev & 0xcc) >> 2) | ((rev & 0x33) << 2);
356 rev = ((rev & 0xaa) >> 1) | ((rev & 0x55) << 1);
361 * XXX this is hack in attempt to carry flags bitfield
362 * throught endian village. ABI says:
364 * Bit-fields are allocated from right to left (least to most significant)
365 * on little-endian implementations and from left to right (most to least
366 * significant) on big-endian implementations.
368 * The above seems to be byte specific, so we need to reverse each
369 * byte of the bitfield. 'Internet' also says this might be implementation
370 * specific and we probably need proper fix and carry perf_event_attr
371 * bitfield flags in separate data file FEAT_ section. Thought this seems
374 static void swap_bitfield(u8 *p, unsigned len)
378 for (i = 0; i < len; i++) {
384 /* exported for swapping attributes in file header */
385 void perf_event__attr_swap(struct perf_event_attr *attr)
387 attr->type = bswap_32(attr->type);
388 attr->size = bswap_32(attr->size);
389 attr->config = bswap_64(attr->config);
390 attr->sample_period = bswap_64(attr->sample_period);
391 attr->sample_type = bswap_64(attr->sample_type);
392 attr->read_format = bswap_64(attr->read_format);
393 attr->wakeup_events = bswap_32(attr->wakeup_events);
394 attr->bp_type = bswap_32(attr->bp_type);
395 attr->bp_addr = bswap_64(attr->bp_addr);
396 attr->bp_len = bswap_64(attr->bp_len);
397 attr->branch_sample_type = bswap_64(attr->branch_sample_type);
398 attr->sample_regs_user = bswap_64(attr->sample_regs_user);
399 attr->sample_stack_user = bswap_32(attr->sample_stack_user);
401 swap_bitfield((u8 *) (&attr->read_format + 1), sizeof(u64));
404 static void perf_event__hdr_attr_swap(union perf_event *event,
405 bool sample_id_all __maybe_unused)
409 perf_event__attr_swap(&event->attr.attr);
411 size = event->header.size;
412 size -= (void *)&event->attr.id - (void *)event;
413 mem_bswap_64(event->attr.id, size);
416 static void perf_event__event_type_swap(union perf_event *event,
417 bool sample_id_all __maybe_unused)
419 event->event_type.event_type.event_id =
420 bswap_64(event->event_type.event_type.event_id);
423 static void perf_event__tracing_data_swap(union perf_event *event,
424 bool sample_id_all __maybe_unused)
426 event->tracing_data.size = bswap_32(event->tracing_data.size);
429 typedef void (*perf_event__swap_op)(union perf_event *event,
432 static perf_event__swap_op perf_event__swap_ops[] = {
433 [PERF_RECORD_MMAP] = perf_event__mmap_swap,
434 [PERF_RECORD_MMAP2] = perf_event__mmap2_swap,
435 [PERF_RECORD_COMM] = perf_event__comm_swap,
436 [PERF_RECORD_FORK] = perf_event__task_swap,
437 [PERF_RECORD_EXIT] = perf_event__task_swap,
438 [PERF_RECORD_LOST] = perf_event__all64_swap,
439 [PERF_RECORD_READ] = perf_event__read_swap,
440 [PERF_RECORD_THROTTLE] = perf_event__throttle_swap,
441 [PERF_RECORD_UNTHROTTLE] = perf_event__throttle_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 ordered_event {
453 union perf_event *event;
454 struct list_head list;
463 static void perf_session_free_sample_buffers(struct perf_session *session)
465 struct ordered_events *oe = &session->ordered_events;
467 while (!list_empty(&oe->to_free)) {
468 struct ordered_event *event;
470 event = list_entry(oe->to_free.next, struct ordered_event, list);
471 list_del(&event->list);
476 /* The queue is ordered by time */
477 static void queue_event(struct ordered_events *oe, struct ordered_event *new)
479 struct ordered_event *last = oe->last;
480 u64 timestamp = new->timestamp;
487 list_add(&new->list, &oe->events);
488 oe->max_timestamp = timestamp;
493 * last event might point to some random place in the list as it's
494 * the last queued event. We expect that the new event is close to
497 if (last->timestamp <= timestamp) {
498 while (last->timestamp <= timestamp) {
500 if (p == &oe->events) {
501 list_add_tail(&new->list, &oe->events);
502 oe->max_timestamp = timestamp;
505 last = list_entry(p, struct ordered_event, list);
507 list_add_tail(&new->list, &last->list);
509 while (last->timestamp > timestamp) {
511 if (p == &oe->events) {
512 list_add(&new->list, &oe->events);
515 last = list_entry(p, struct ordered_event, list);
517 list_add(&new->list, &last->list);
521 #define MAX_SAMPLE_BUFFER (64 * 1024 / sizeof(struct ordered_event))
522 static struct ordered_event *alloc_event(struct ordered_events *oe)
524 struct list_head *cache = &oe->cache;
525 struct ordered_event *new = NULL;
527 if (!list_empty(cache)) {
528 new = list_entry(cache->next, struct ordered_event, list);
529 list_del(&new->list);
530 } else if (oe->buffer) {
531 new = oe->buffer + oe->buffer_idx;
532 if (++oe->buffer_idx == MAX_SAMPLE_BUFFER)
534 } else if (oe->cur_alloc_size < oe->max_alloc_size) {
535 size_t size = MAX_SAMPLE_BUFFER * sizeof(*new);
537 oe->buffer = malloc(size);
541 oe->cur_alloc_size += size;
542 list_add(&oe->buffer->list, &oe->to_free);
544 /* First entry is abused to maintain the to_free list. */
546 new = oe->buffer + 1;
552 static struct ordered_event *
553 ordered_events__new(struct ordered_events *oe, u64 timestamp)
555 struct ordered_event *new;
557 new = alloc_event(oe);
559 new->timestamp = timestamp;
560 queue_event(oe, new);
567 ordered_events__delete(struct ordered_events *oe, struct ordered_event *event)
569 list_del(&event->list);
570 list_add(&event->list, &oe->cache);
574 static int __ordered_events__flush(struct perf_session *s,
575 struct perf_tool *tool)
577 struct ordered_events *oe = &s->ordered_events;
578 struct list_head *head = &oe->events;
579 struct ordered_event *tmp, *iter;
580 struct perf_sample sample;
581 u64 limit = oe->next_flush;
582 u64 last_ts = oe->last ? oe->last->timestamp : 0ULL;
583 bool show_progress = limit == ULLONG_MAX;
584 struct ui_progress prog;
587 if (!tool->ordered_events || !limit)
591 ui_progress__init(&prog, oe->nr_events, "Processing time ordered events...");
593 list_for_each_entry_safe(iter, tmp, head, list) {
597 if (iter->timestamp > limit)
600 ret = perf_evlist__parse_sample(s->evlist, iter->event, &sample);
602 pr_err("Can't parse sample, err = %d\n", ret);
604 ret = perf_session__deliver_event(s, iter->event, &sample, tool,
610 ordered_events__delete(oe, iter);
611 oe->last_flush = iter->timestamp;
614 ui_progress__update(&prog, 1);
617 if (list_empty(head))
619 else if (last_ts <= limit)
620 oe->last = list_entry(head->prev, struct ordered_event, list);
625 static int ordered_events__flush(struct perf_session *s, struct perf_tool *tool,
628 struct ordered_events *oe = &s->ordered_events;
632 case OE_FLUSH__FINAL:
633 oe->next_flush = ULLONG_MAX;
638 struct ordered_event *first, *last;
639 struct list_head *head = &oe->events;
641 first = list_entry(head->next, struct ordered_event, list);
644 /* Warn if we are called before any event got allocated. */
645 if (WARN_ONCE(!last || list_empty(head), "empty queue"))
648 oe->next_flush = first->timestamp;
649 oe->next_flush += (last->timestamp - first->timestamp) / 2;
653 case OE_FLUSH__ROUND:
658 err = __ordered_events__flush(s, tool);
661 if (how == OE_FLUSH__ROUND)
662 oe->next_flush = oe->max_timestamp;
669 * When perf record finishes a pass on every buffers, it records this pseudo
671 * We record the max timestamp t found in the pass n.
672 * Assuming these timestamps are monotonic across cpus, we know that if
673 * a buffer still has events with timestamps below t, they will be all
674 * available and then read in the pass n + 1.
675 * Hence when we start to read the pass n + 2, we can safely flush every
676 * events with timestamps below t.
678 * ============ PASS n =================
681 * cnt1 timestamps | cnt2 timestamps
684 * - | 4 <--- max recorded
686 * ============ PASS n + 1 ==============
689 * cnt1 timestamps | cnt2 timestamps
692 * 5 | 7 <---- max recorded
694 * Flush every events below timestamp 4
696 * ============ PASS n + 2 ==============
699 * cnt1 timestamps | cnt2 timestamps
704 * Flush every events below timestamp 7
707 static int process_finished_round(struct perf_tool *tool,
708 union perf_event *event __maybe_unused,
709 struct perf_session *session)
711 return ordered_events__flush(session, tool, OE_FLUSH__ROUND);
714 int perf_session_queue_event(struct perf_session *s, union perf_event *event,
715 struct perf_tool *tool, struct perf_sample *sample,
718 struct ordered_events *oe = &s->ordered_events;
719 u64 timestamp = sample->time;
720 struct ordered_event *new;
722 if (!timestamp || timestamp == ~0ULL)
725 if (timestamp < s->ordered_events.last_flush) {
726 printf("Warning: Timestamp below last timeslice flush\n");
730 new = ordered_events__new(oe, timestamp);
732 ordered_events__flush(s, tool, OE_FLUSH__HALF);
733 new = ordered_events__new(oe, timestamp);
739 new->file_offset = file_offset;
744 static void callchain__printf(struct perf_sample *sample)
748 printf("... chain: nr:%" PRIu64 "\n", sample->callchain->nr);
750 for (i = 0; i < sample->callchain->nr; i++)
751 printf("..... %2d: %016" PRIx64 "\n",
752 i, sample->callchain->ips[i]);
755 static void branch_stack__printf(struct perf_sample *sample)
759 printf("... branch stack: nr:%" PRIu64 "\n", sample->branch_stack->nr);
761 for (i = 0; i < sample->branch_stack->nr; i++)
762 printf("..... %2"PRIu64": %016" PRIx64 " -> %016" PRIx64 "\n",
763 i, sample->branch_stack->entries[i].from,
764 sample->branch_stack->entries[i].to);
767 static void regs_dump__printf(u64 mask, u64 *regs)
771 for_each_set_bit(rid, (unsigned long *) &mask, sizeof(mask) * 8) {
774 printf(".... %-5s 0x%" PRIx64 "\n",
775 perf_reg_name(rid), val);
779 static void regs_user__printf(struct perf_sample *sample)
781 struct regs_dump *user_regs = &sample->user_regs;
783 if (user_regs->regs) {
784 u64 mask = user_regs->mask;
785 printf("... user regs: mask 0x%" PRIx64 "\n", mask);
786 regs_dump__printf(mask, user_regs->regs);
790 static void stack_user__printf(struct stack_dump *dump)
792 printf("... ustack: size %" PRIu64 ", offset 0x%x\n",
793 dump->size, dump->offset);
796 static void perf_session__print_tstamp(struct perf_session *session,
797 union perf_event *event,
798 struct perf_sample *sample)
800 u64 sample_type = __perf_evlist__combined_sample_type(session->evlist);
802 if (event->header.type != PERF_RECORD_SAMPLE &&
803 !perf_evlist__sample_id_all(session->evlist)) {
804 fputs("-1 -1 ", stdout);
808 if ((sample_type & PERF_SAMPLE_CPU))
809 printf("%u ", sample->cpu);
811 if (sample_type & PERF_SAMPLE_TIME)
812 printf("%" PRIu64 " ", sample->time);
815 static void sample_read__printf(struct perf_sample *sample, u64 read_format)
817 printf("... sample_read:\n");
819 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
820 printf("...... time enabled %016" PRIx64 "\n",
821 sample->read.time_enabled);
823 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
824 printf("...... time running %016" PRIx64 "\n",
825 sample->read.time_running);
827 if (read_format & PERF_FORMAT_GROUP) {
830 printf(".... group nr %" PRIu64 "\n", sample->read.group.nr);
832 for (i = 0; i < sample->read.group.nr; i++) {
833 struct sample_read_value *value;
835 value = &sample->read.group.values[i];
836 printf("..... id %016" PRIx64
837 ", value %016" PRIx64 "\n",
838 value->id, value->value);
841 printf("..... id %016" PRIx64 ", value %016" PRIx64 "\n",
842 sample->read.one.id, sample->read.one.value);
845 static void dump_event(struct perf_session *session, union perf_event *event,
846 u64 file_offset, struct perf_sample *sample)
851 printf("\n%#" PRIx64 " [%#x]: event: %d\n",
852 file_offset, event->header.size, event->header.type);
857 perf_session__print_tstamp(session, event, sample);
859 printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset,
860 event->header.size, perf_event__name(event->header.type));
863 static void dump_sample(struct perf_evsel *evsel, union perf_event *event,
864 struct perf_sample *sample)
871 printf("(IP, 0x%x): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n",
872 event->header.misc, sample->pid, sample->tid, sample->ip,
873 sample->period, sample->addr);
875 sample_type = evsel->attr.sample_type;
877 if (sample_type & PERF_SAMPLE_CALLCHAIN)
878 callchain__printf(sample);
880 if (sample_type & PERF_SAMPLE_BRANCH_STACK)
881 branch_stack__printf(sample);
883 if (sample_type & PERF_SAMPLE_REGS_USER)
884 regs_user__printf(sample);
886 if (sample_type & PERF_SAMPLE_STACK_USER)
887 stack_user__printf(&sample->user_stack);
889 if (sample_type & PERF_SAMPLE_WEIGHT)
890 printf("... weight: %" PRIu64 "\n", sample->weight);
892 if (sample_type & PERF_SAMPLE_DATA_SRC)
893 printf(" . data_src: 0x%"PRIx64"\n", sample->data_src);
895 if (sample_type & PERF_SAMPLE_TRANSACTION)
896 printf("... transaction: %" PRIx64 "\n", sample->transaction);
898 if (sample_type & PERF_SAMPLE_READ)
899 sample_read__printf(sample, evsel->attr.read_format);
902 static struct machine *
903 perf_session__find_machine_for_cpumode(struct perf_session *session,
904 union perf_event *event,
905 struct perf_sample *sample)
907 const u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
908 struct machine *machine;
911 ((cpumode == PERF_RECORD_MISC_GUEST_KERNEL) ||
912 (cpumode == PERF_RECORD_MISC_GUEST_USER))) {
915 if (event->header.type == PERF_RECORD_MMAP
916 || event->header.type == PERF_RECORD_MMAP2)
917 pid = event->mmap.pid;
921 machine = perf_session__find_machine(session, pid);
923 machine = perf_session__findnew_machine(session,
924 DEFAULT_GUEST_KERNEL_ID);
928 return &session->machines.host;
931 static int deliver_sample_value(struct perf_session *session,
932 struct perf_tool *tool,
933 union perf_event *event,
934 struct perf_sample *sample,
935 struct sample_read_value *v,
936 struct machine *machine)
938 struct perf_sample_id *sid;
940 sid = perf_evlist__id2sid(session->evlist, v->id);
943 sample->period = v->value - sid->period;
944 sid->period = v->value;
947 if (!sid || sid->evsel == NULL) {
948 ++session->stats.nr_unknown_id;
952 return tool->sample(tool, event, sample, sid->evsel, machine);
955 static int deliver_sample_group(struct perf_session *session,
956 struct perf_tool *tool,
957 union perf_event *event,
958 struct perf_sample *sample,
959 struct machine *machine)
964 for (i = 0; i < sample->read.group.nr; i++) {
965 ret = deliver_sample_value(session, tool, event, sample,
966 &sample->read.group.values[i],
976 perf_session__deliver_sample(struct perf_session *session,
977 struct perf_tool *tool,
978 union perf_event *event,
979 struct perf_sample *sample,
980 struct perf_evsel *evsel,
981 struct machine *machine)
983 /* We know evsel != NULL. */
984 u64 sample_type = evsel->attr.sample_type;
985 u64 read_format = evsel->attr.read_format;
987 /* Standard sample delievery. */
988 if (!(sample_type & PERF_SAMPLE_READ))
989 return tool->sample(tool, event, sample, evsel, machine);
991 /* For PERF_SAMPLE_READ we have either single or group mode. */
992 if (read_format & PERF_FORMAT_GROUP)
993 return deliver_sample_group(session, tool, event, sample,
996 return deliver_sample_value(session, tool, event, sample,
997 &sample->read.one, machine);
1000 int perf_session__deliver_event(struct perf_session *session,
1001 union perf_event *event,
1002 struct perf_sample *sample,
1003 struct perf_tool *tool, u64 file_offset)
1005 struct perf_evsel *evsel;
1006 struct machine *machine;
1008 dump_event(session, event, file_offset, sample);
1010 evsel = perf_evlist__id2evsel(session->evlist, sample->id);
1011 if (evsel != NULL && event->header.type != PERF_RECORD_SAMPLE) {
1013 * XXX We're leaving PERF_RECORD_SAMPLE unnacounted here
1014 * because the tools right now may apply filters, discarding
1015 * some of the samples. For consistency, in the future we
1016 * should have something like nr_filtered_samples and remove
1017 * the sample->period from total_sample_period, etc, KISS for
1020 * Also testing against NULL allows us to handle files without
1021 * attr.sample_id_all and/or without PERF_SAMPLE_ID. In the
1022 * future probably it'll be a good idea to restrict event
1023 * processing via perf_session to files with both set.
1025 hists__inc_nr_events(&evsel->hists, event->header.type);
1028 machine = perf_session__find_machine_for_cpumode(session, event,
1031 switch (event->header.type) {
1032 case PERF_RECORD_SAMPLE:
1033 dump_sample(evsel, event, sample);
1034 if (evsel == NULL) {
1035 ++session->stats.nr_unknown_id;
1038 if (machine == NULL) {
1039 ++session->stats.nr_unprocessable_samples;
1042 return perf_session__deliver_sample(session, tool, event,
1043 sample, evsel, machine);
1044 case PERF_RECORD_MMAP:
1045 return tool->mmap(tool, event, sample, machine);
1046 case PERF_RECORD_MMAP2:
1047 return tool->mmap2(tool, event, sample, machine);
1048 case PERF_RECORD_COMM:
1049 return tool->comm(tool, event, sample, machine);
1050 case PERF_RECORD_FORK:
1051 return tool->fork(tool, event, sample, machine);
1052 case PERF_RECORD_EXIT:
1053 return tool->exit(tool, event, sample, machine);
1054 case PERF_RECORD_LOST:
1055 if (tool->lost == perf_event__process_lost)
1056 session->stats.total_lost += event->lost.lost;
1057 return tool->lost(tool, event, sample, machine);
1058 case PERF_RECORD_READ:
1059 return tool->read(tool, event, sample, evsel, machine);
1060 case PERF_RECORD_THROTTLE:
1061 return tool->throttle(tool, event, sample, machine);
1062 case PERF_RECORD_UNTHROTTLE:
1063 return tool->unthrottle(tool, event, sample, machine);
1065 ++session->stats.nr_unknown_events;
1070 static s64 perf_session__process_user_event(struct perf_session *session,
1071 union perf_event *event,
1072 struct perf_tool *tool,
1075 int fd = perf_data_file__fd(session->file);
1078 dump_event(session, event, file_offset, NULL);
1080 /* These events are processed right away */
1081 switch (event->header.type) {
1082 case PERF_RECORD_HEADER_ATTR:
1083 err = tool->attr(tool, event, &session->evlist);
1085 perf_session__set_id_hdr_size(session);
1087 case PERF_RECORD_HEADER_EVENT_TYPE:
1089 * Depreceated, but we need to handle it for sake
1090 * of old data files create in pipe mode.
1093 case PERF_RECORD_HEADER_TRACING_DATA:
1094 /* setup for reading amidst mmap */
1095 lseek(fd, file_offset, SEEK_SET);
1096 return tool->tracing_data(tool, event, session);
1097 case PERF_RECORD_HEADER_BUILD_ID:
1098 return tool->build_id(tool, event, session);
1099 case PERF_RECORD_FINISHED_ROUND:
1100 return tool->finished_round(tool, event, session);
1106 static void event_swap(union perf_event *event, bool sample_id_all)
1108 perf_event__swap_op swap;
1110 swap = perf_event__swap_ops[event->header.type];
1112 swap(event, sample_id_all);
1115 static s64 perf_session__process_event(struct perf_session *session,
1116 union perf_event *event,
1117 struct perf_tool *tool,
1120 struct perf_sample sample;
1123 if (session->header.needs_swap)
1124 event_swap(event, perf_evlist__sample_id_all(session->evlist));
1126 if (event->header.type >= PERF_RECORD_HEADER_MAX)
1129 events_stats__inc(&session->stats, event->header.type);
1131 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
1132 return perf_session__process_user_event(session, event, tool, file_offset);
1135 * For all kernel events we get the sample data
1137 ret = perf_evlist__parse_sample(session->evlist, event, &sample);
1141 if (tool->ordered_events) {
1142 ret = perf_session_queue_event(session, event, tool, &sample,
1148 return perf_session__deliver_event(session, event, &sample, tool,
1152 void perf_event_header__bswap(struct perf_event_header *hdr)
1154 hdr->type = bswap_32(hdr->type);
1155 hdr->misc = bswap_16(hdr->misc);
1156 hdr->size = bswap_16(hdr->size);
1159 struct thread *perf_session__findnew(struct perf_session *session, pid_t pid)
1161 return machine__findnew_thread(&session->machines.host, -1, pid);
1164 static struct thread *perf_session__register_idle_thread(struct perf_session *session)
1166 struct thread *thread;
1168 thread = machine__findnew_thread(&session->machines.host, 0, 0);
1169 if (thread == NULL || thread__set_comm(thread, "swapper", 0)) {
1170 pr_err("problem inserting idle task.\n");
1177 static void perf_session__warn_about_errors(const struct perf_session *session,
1178 const struct perf_tool *tool)
1180 if (tool->lost == perf_event__process_lost &&
1181 session->stats.nr_events[PERF_RECORD_LOST] != 0) {
1182 ui__warning("Processed %d events and lost %d chunks!\n\n"
1183 "Check IO/CPU overload!\n\n",
1184 session->stats.nr_events[0],
1185 session->stats.nr_events[PERF_RECORD_LOST]);
1188 if (session->stats.nr_unknown_events != 0) {
1189 ui__warning("Found %u unknown events!\n\n"
1190 "Is this an older tool processing a perf.data "
1191 "file generated by a more recent tool?\n\n"
1192 "If that is not the case, consider "
1193 "reporting to linux-kernel@vger.kernel.org.\n\n",
1194 session->stats.nr_unknown_events);
1197 if (session->stats.nr_unknown_id != 0) {
1198 ui__warning("%u samples with id not present in the header\n",
1199 session->stats.nr_unknown_id);
1202 if (session->stats.nr_invalid_chains != 0) {
1203 ui__warning("Found invalid callchains!\n\n"
1204 "%u out of %u events were discarded for this reason.\n\n"
1205 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
1206 session->stats.nr_invalid_chains,
1207 session->stats.nr_events[PERF_RECORD_SAMPLE]);
1210 if (session->stats.nr_unprocessable_samples != 0) {
1211 ui__warning("%u unprocessable samples recorded.\n"
1212 "Do you have a KVM guest running and not using 'perf kvm'?\n",
1213 session->stats.nr_unprocessable_samples);
1217 volatile int session_done;
1219 static int __perf_session__process_pipe_events(struct perf_session *session,
1220 struct perf_tool *tool)
1222 int fd = perf_data_file__fd(session->file);
1223 union perf_event *event;
1224 uint32_t size, cur_size = 0;
1231 perf_tool__fill_defaults(tool);
1234 cur_size = sizeof(union perf_event);
1236 buf = malloc(cur_size);
1241 err = readn(fd, event, sizeof(struct perf_event_header));
1246 pr_err("failed to read event header\n");
1250 if (session->header.needs_swap)
1251 perf_event_header__bswap(&event->header);
1253 size = event->header.size;
1254 if (size < sizeof(struct perf_event_header)) {
1255 pr_err("bad event header size\n");
1259 if (size > cur_size) {
1260 void *new = realloc(buf, size);
1262 pr_err("failed to allocate memory to read event\n");
1270 p += sizeof(struct perf_event_header);
1272 if (size - sizeof(struct perf_event_header)) {
1273 err = readn(fd, p, size - sizeof(struct perf_event_header));
1276 pr_err("unexpected end of event stream\n");
1280 pr_err("failed to read event data\n");
1285 if ((skip = perf_session__process_event(session, event, tool, head)) < 0) {
1286 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
1287 head, event->header.size, event->header.type);
1297 if (!session_done())
1300 /* do the final flush for ordered samples */
1301 err = ordered_events__flush(session, tool, OE_FLUSH__FINAL);
1304 perf_session__warn_about_errors(session, tool);
1305 perf_session_free_sample_buffers(session);
1309 static union perf_event *
1310 fetch_mmaped_event(struct perf_session *session,
1311 u64 head, size_t mmap_size, char *buf)
1313 union perf_event *event;
1316 * Ensure we have enough space remaining to read
1317 * the size of the event in the headers.
1319 if (head + sizeof(event->header) > mmap_size)
1322 event = (union perf_event *)(buf + head);
1324 if (session->header.needs_swap)
1325 perf_event_header__bswap(&event->header);
1327 if (head + event->header.size > mmap_size) {
1328 /* We're not fetching the event so swap back again */
1329 if (session->header.needs_swap)
1330 perf_event_header__bswap(&event->header);
1338 * On 64bit we can mmap the data file in one go. No need for tiny mmap
1339 * slices. On 32bit we use 32MB.
1341 #if BITS_PER_LONG == 64
1342 #define MMAP_SIZE ULLONG_MAX
1345 #define MMAP_SIZE (32 * 1024 * 1024ULL)
1346 #define NUM_MMAPS 128
1349 int __perf_session__process_events(struct perf_session *session,
1350 u64 data_offset, u64 data_size,
1351 u64 file_size, struct perf_tool *tool)
1353 int fd = perf_data_file__fd(session->file);
1354 u64 head, page_offset, file_offset, file_pos, size;
1355 int err, mmap_prot, mmap_flags, map_idx = 0;
1357 char *buf, *mmaps[NUM_MMAPS];
1358 union perf_event *event;
1359 struct ui_progress prog;
1362 perf_tool__fill_defaults(tool);
1364 page_offset = page_size * (data_offset / page_size);
1365 file_offset = page_offset;
1366 head = data_offset - page_offset;
1368 if (data_size && (data_offset + data_size < file_size))
1369 file_size = data_offset + data_size;
1371 ui_progress__init(&prog, file_size, "Processing events...");
1373 mmap_size = MMAP_SIZE;
1374 if (mmap_size > file_size) {
1375 mmap_size = file_size;
1376 session->one_mmap = true;
1379 memset(mmaps, 0, sizeof(mmaps));
1381 mmap_prot = PROT_READ;
1382 mmap_flags = MAP_SHARED;
1384 if (session->header.needs_swap) {
1385 mmap_prot |= PROT_WRITE;
1386 mmap_flags = MAP_PRIVATE;
1389 buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, fd,
1391 if (buf == MAP_FAILED) {
1392 pr_err("failed to mmap file\n");
1396 mmaps[map_idx] = buf;
1397 map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
1398 file_pos = file_offset + head;
1399 if (session->one_mmap) {
1400 session->one_mmap_addr = buf;
1401 session->one_mmap_offset = file_offset;
1405 event = fetch_mmaped_event(session, head, mmap_size, buf);
1407 if (mmaps[map_idx]) {
1408 munmap(mmaps[map_idx], mmap_size);
1409 mmaps[map_idx] = NULL;
1412 page_offset = page_size * (head / page_size);
1413 file_offset += page_offset;
1414 head -= page_offset;
1418 size = event->header.size;
1420 if (size < sizeof(struct perf_event_header) ||
1421 (skip = perf_session__process_event(session, event, tool, file_pos))
1423 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
1424 file_offset + head, event->header.size,
1425 event->header.type);
1436 ui_progress__update(&prog, size);
1441 if (file_pos < file_size)
1445 /* do the final flush for ordered samples */
1446 err = ordered_events__flush(session, tool, OE_FLUSH__FINAL);
1448 ui_progress__finish();
1449 perf_session__warn_about_errors(session, tool);
1450 perf_session_free_sample_buffers(session);
1451 session->one_mmap = false;
1455 int perf_session__process_events(struct perf_session *session,
1456 struct perf_tool *tool)
1458 u64 size = perf_data_file__size(session->file);
1461 if (perf_session__register_idle_thread(session) == NULL)
1464 if (!perf_data_file__is_pipe(session->file))
1465 err = __perf_session__process_events(session,
1466 session->header.data_offset,
1467 session->header.data_size,
1470 err = __perf_session__process_pipe_events(session, tool);
1475 bool perf_session__has_traces(struct perf_session *session, const char *msg)
1477 struct perf_evsel *evsel;
1479 evlist__for_each(session->evlist, evsel) {
1480 if (evsel->attr.type == PERF_TYPE_TRACEPOINT)
1484 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
1488 int maps__set_kallsyms_ref_reloc_sym(struct map **maps,
1489 const char *symbol_name, u64 addr)
1493 struct ref_reloc_sym *ref;
1495 ref = zalloc(sizeof(struct ref_reloc_sym));
1499 ref->name = strdup(symbol_name);
1500 if (ref->name == NULL) {
1505 bracket = strchr(ref->name, ']');
1511 for (i = 0; i < MAP__NR_TYPES; ++i) {
1512 struct kmap *kmap = map__kmap(maps[i]);
1513 kmap->ref_reloc_sym = ref;
1519 size_t perf_session__fprintf_dsos(struct perf_session *session, FILE *fp)
1521 return machines__fprintf_dsos(&session->machines, fp);
1524 size_t perf_session__fprintf_dsos_buildid(struct perf_session *session, FILE *fp,
1525 bool (skip)(struct dso *dso, int parm), int parm)
1527 return machines__fprintf_dsos_buildid(&session->machines, fp, skip, parm);
1530 size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp)
1532 struct perf_evsel *pos;
1533 size_t ret = fprintf(fp, "Aggregated stats:\n");
1535 ret += events_stats__fprintf(&session->stats, fp);
1537 evlist__for_each(session->evlist, pos) {
1538 ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
1539 ret += events_stats__fprintf(&pos->hists.stats, fp);
1545 size_t perf_session__fprintf(struct perf_session *session, FILE *fp)
1548 * FIXME: Here we have to actually print all the machines in this
1549 * session, not just the host...
1551 return machine__fprintf(&session->machines.host, fp);
1554 struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
1557 struct perf_evsel *pos;
1559 evlist__for_each(session->evlist, pos) {
1560 if (pos->attr.type == type)
1566 void perf_evsel__print_ip(struct perf_evsel *evsel, struct perf_sample *sample,
1567 struct addr_location *al,
1568 unsigned int print_opts, unsigned int stack_depth)
1570 struct callchain_cursor_node *node;
1571 int print_ip = print_opts & PRINT_IP_OPT_IP;
1572 int print_sym = print_opts & PRINT_IP_OPT_SYM;
1573 int print_dso = print_opts & PRINT_IP_OPT_DSO;
1574 int print_symoffset = print_opts & PRINT_IP_OPT_SYMOFFSET;
1575 int print_oneline = print_opts & PRINT_IP_OPT_ONELINE;
1576 int print_srcline = print_opts & PRINT_IP_OPT_SRCLINE;
1577 char s = print_oneline ? ' ' : '\t';
1579 if (symbol_conf.use_callchain && sample->callchain) {
1580 struct addr_location node_al;
1582 if (machine__resolve_callchain(al->machine, evsel, al->thread,
1584 PERF_MAX_STACK_DEPTH) != 0) {
1586 error("Failed to resolve callchain. Skipping\n");
1589 callchain_cursor_commit(&callchain_cursor);
1591 if (print_symoffset)
1594 while (stack_depth) {
1597 node = callchain_cursor_current(&callchain_cursor);
1601 if (node->sym && node->sym->ignore)
1605 printf("%c%16" PRIx64, s, node->ip);
1608 addr = node->map->map_ip(node->map, node->ip);
1612 if (print_symoffset) {
1613 node_al.addr = addr;
1614 node_al.map = node->map;
1615 symbol__fprintf_symname_offs(node->sym, &node_al, stdout);
1617 symbol__fprintf_symname(node->sym, stdout);
1622 map__fprintf_dsoname(node->map, stdout);
1627 map__fprintf_srcline(node->map, addr, "\n ",
1635 callchain_cursor_advance(&callchain_cursor);
1639 if (al->sym && al->sym->ignore)
1643 printf("%16" PRIx64, sample->ip);
1647 if (print_symoffset)
1648 symbol__fprintf_symname_offs(al->sym, al,
1651 symbol__fprintf_symname(al->sym, stdout);
1656 map__fprintf_dsoname(al->map, stdout);
1661 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
1665 int perf_session__cpu_bitmap(struct perf_session *session,
1666 const char *cpu_list, unsigned long *cpu_bitmap)
1669 struct cpu_map *map;
1671 for (i = 0; i < PERF_TYPE_MAX; ++i) {
1672 struct perf_evsel *evsel;
1674 evsel = perf_session__find_first_evtype(session, i);
1678 if (!(evsel->attr.sample_type & PERF_SAMPLE_CPU)) {
1679 pr_err("File does not contain CPU events. "
1680 "Remove -c option to proceed.\n");
1685 map = cpu_map__new(cpu_list);
1687 pr_err("Invalid cpu_list\n");
1691 for (i = 0; i < map->nr; i++) {
1692 int cpu = map->map[i];
1694 if (cpu >= MAX_NR_CPUS) {
1695 pr_err("Requested CPU %d too large. "
1696 "Consider raising MAX_NR_CPUS\n", cpu);
1697 goto out_delete_map;
1700 set_bit(cpu, cpu_bitmap);
1706 cpu_map__delete(map);
1710 void perf_session__fprintf_info(struct perf_session *session, FILE *fp,
1716 if (session == NULL || fp == NULL)
1719 fd = perf_data_file__fd(session->file);
1721 ret = fstat(fd, &st);
1725 fprintf(fp, "# ========\n");
1726 fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
1727 perf_header__fprintf_info(session, fp, full);
1728 fprintf(fp, "# ========\n#\n");
1732 int __perf_session__set_tracepoints_handlers(struct perf_session *session,
1733 const struct perf_evsel_str_handler *assocs,
1736 struct perf_evsel *evsel;
1740 for (i = 0; i < nr_assocs; i++) {
1742 * Adding a handler for an event not in the session,
1745 evsel = perf_evlist__find_tracepoint_by_name(session->evlist, assocs[i].name);
1750 if (evsel->handler != NULL)
1752 evsel->handler = assocs[i].handler;