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 static bool perf_session__has_comm_exec(struct perf_session *session)
72 struct perf_evsel *evsel;
74 evlist__for_each(session->evlist, evsel) {
75 if (evsel->attr.comm_exec)
82 static void perf_session__set_comm_exec(struct perf_session *session)
84 bool comm_exec = perf_session__has_comm_exec(session);
86 machines__set_comm_exec(&session->machines, comm_exec);
89 struct perf_session *perf_session__new(struct perf_data_file *file,
90 bool repipe, struct perf_tool *tool)
92 struct perf_session *session = zalloc(sizeof(*session));
97 session->repipe = repipe;
98 ordered_events__init(&session->ordered_events);
99 machines__init(&session->machines);
102 if (perf_data_file__open(file))
105 session->file = file;
107 if (perf_data_file__is_read(file)) {
108 if (perf_session__open(session) < 0)
111 perf_session__set_id_hdr_size(session);
112 perf_session__set_comm_exec(session);
116 if (!file || perf_data_file__is_write(file)) {
118 * In O_RDONLY mode this will be performed when reading the
119 * kernel MMAP event, in perf_event__process_mmap().
121 if (perf_session__create_kernel_maps(session) < 0)
122 pr_warning("Cannot read kernel map\n");
125 if (tool && tool->ordering_requires_timestamps &&
126 tool->ordered_events && !perf_evlist__sample_id_all(session->evlist)) {
127 dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n");
128 tool->ordered_events = false;
134 perf_data_file__close(file);
136 perf_session__delete(session);
141 static void perf_session__delete_dead_threads(struct perf_session *session)
143 machine__delete_dead_threads(&session->machines.host);
146 static void perf_session__delete_threads(struct perf_session *session)
148 machine__delete_threads(&session->machines.host);
151 static void perf_session_env__delete(struct perf_session_env *env)
153 zfree(&env->hostname);
154 zfree(&env->os_release);
155 zfree(&env->version);
157 zfree(&env->cpu_desc);
160 zfree(&env->cmdline);
161 zfree(&env->sibling_cores);
162 zfree(&env->sibling_threads);
163 zfree(&env->numa_nodes);
164 zfree(&env->pmu_mappings);
167 void perf_session__delete(struct perf_session *session)
169 perf_session__destroy_kernel_maps(session);
170 perf_session__delete_dead_threads(session);
171 perf_session__delete_threads(session);
172 perf_session_env__delete(&session->header.env);
173 machines__exit(&session->machines);
175 perf_data_file__close(session->file);
179 static int process_event_synth_tracing_data_stub(struct perf_tool *tool
181 union perf_event *event
183 struct perf_session *session
186 dump_printf(": unhandled!\n");
190 static int process_event_synth_attr_stub(struct perf_tool *tool __maybe_unused,
191 union perf_event *event __maybe_unused,
192 struct perf_evlist **pevlist
195 dump_printf(": unhandled!\n");
199 static int process_event_sample_stub(struct perf_tool *tool __maybe_unused,
200 union perf_event *event __maybe_unused,
201 struct perf_sample *sample __maybe_unused,
202 struct perf_evsel *evsel __maybe_unused,
203 struct machine *machine __maybe_unused)
205 dump_printf(": unhandled!\n");
209 static int process_event_stub(struct perf_tool *tool __maybe_unused,
210 union perf_event *event __maybe_unused,
211 struct perf_sample *sample __maybe_unused,
212 struct machine *machine __maybe_unused)
214 dump_printf(": unhandled!\n");
218 static int process_finished_round_stub(struct perf_tool *tool __maybe_unused,
219 union perf_event *event __maybe_unused,
220 struct perf_session *perf_session
223 dump_printf(": unhandled!\n");
227 static int process_finished_round(struct perf_tool *tool,
228 union perf_event *event,
229 struct perf_session *session);
231 void perf_tool__fill_defaults(struct perf_tool *tool)
233 if (tool->sample == NULL)
234 tool->sample = process_event_sample_stub;
235 if (tool->mmap == NULL)
236 tool->mmap = process_event_stub;
237 if (tool->mmap2 == NULL)
238 tool->mmap2 = process_event_stub;
239 if (tool->comm == NULL)
240 tool->comm = process_event_stub;
241 if (tool->fork == NULL)
242 tool->fork = process_event_stub;
243 if (tool->exit == NULL)
244 tool->exit = process_event_stub;
245 if (tool->lost == NULL)
246 tool->lost = perf_event__process_lost;
247 if (tool->read == NULL)
248 tool->read = process_event_sample_stub;
249 if (tool->throttle == NULL)
250 tool->throttle = process_event_stub;
251 if (tool->unthrottle == NULL)
252 tool->unthrottle = process_event_stub;
253 if (tool->attr == NULL)
254 tool->attr = process_event_synth_attr_stub;
255 if (tool->tracing_data == NULL)
256 tool->tracing_data = process_event_synth_tracing_data_stub;
257 if (tool->build_id == NULL)
258 tool->build_id = process_finished_round_stub;
259 if (tool->finished_round == NULL) {
260 if (tool->ordered_events)
261 tool->finished_round = process_finished_round;
263 tool->finished_round = process_finished_round_stub;
267 static void swap_sample_id_all(union perf_event *event, void *data)
269 void *end = (void *) event + event->header.size;
270 int size = end - data;
272 BUG_ON(size % sizeof(u64));
273 mem_bswap_64(data, size);
276 static void perf_event__all64_swap(union perf_event *event,
277 bool sample_id_all __maybe_unused)
279 struct perf_event_header *hdr = &event->header;
280 mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr));
283 static void perf_event__comm_swap(union perf_event *event, bool sample_id_all)
285 event->comm.pid = bswap_32(event->comm.pid);
286 event->comm.tid = bswap_32(event->comm.tid);
289 void *data = &event->comm.comm;
291 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
292 swap_sample_id_all(event, data);
296 static void perf_event__mmap_swap(union perf_event *event,
299 event->mmap.pid = bswap_32(event->mmap.pid);
300 event->mmap.tid = bswap_32(event->mmap.tid);
301 event->mmap.start = bswap_64(event->mmap.start);
302 event->mmap.len = bswap_64(event->mmap.len);
303 event->mmap.pgoff = bswap_64(event->mmap.pgoff);
306 void *data = &event->mmap.filename;
308 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
309 swap_sample_id_all(event, data);
313 static void perf_event__mmap2_swap(union perf_event *event,
316 event->mmap2.pid = bswap_32(event->mmap2.pid);
317 event->mmap2.tid = bswap_32(event->mmap2.tid);
318 event->mmap2.start = bswap_64(event->mmap2.start);
319 event->mmap2.len = bswap_64(event->mmap2.len);
320 event->mmap2.pgoff = bswap_64(event->mmap2.pgoff);
321 event->mmap2.maj = bswap_32(event->mmap2.maj);
322 event->mmap2.min = bswap_32(event->mmap2.min);
323 event->mmap2.ino = bswap_64(event->mmap2.ino);
326 void *data = &event->mmap2.filename;
328 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
329 swap_sample_id_all(event, data);
332 static void perf_event__task_swap(union perf_event *event, bool sample_id_all)
334 event->fork.pid = bswap_32(event->fork.pid);
335 event->fork.tid = bswap_32(event->fork.tid);
336 event->fork.ppid = bswap_32(event->fork.ppid);
337 event->fork.ptid = bswap_32(event->fork.ptid);
338 event->fork.time = bswap_64(event->fork.time);
341 swap_sample_id_all(event, &event->fork + 1);
344 static void perf_event__read_swap(union perf_event *event, bool sample_id_all)
346 event->read.pid = bswap_32(event->read.pid);
347 event->read.tid = bswap_32(event->read.tid);
348 event->read.value = bswap_64(event->read.value);
349 event->read.time_enabled = bswap_64(event->read.time_enabled);
350 event->read.time_running = bswap_64(event->read.time_running);
351 event->read.id = bswap_64(event->read.id);
354 swap_sample_id_all(event, &event->read + 1);
357 static void perf_event__throttle_swap(union perf_event *event,
360 event->throttle.time = bswap_64(event->throttle.time);
361 event->throttle.id = bswap_64(event->throttle.id);
362 event->throttle.stream_id = bswap_64(event->throttle.stream_id);
365 swap_sample_id_all(event, &event->throttle + 1);
368 static u8 revbyte(u8 b)
370 int rev = (b >> 4) | ((b & 0xf) << 4);
371 rev = ((rev & 0xcc) >> 2) | ((rev & 0x33) << 2);
372 rev = ((rev & 0xaa) >> 1) | ((rev & 0x55) << 1);
377 * XXX this is hack in attempt to carry flags bitfield
378 * throught endian village. ABI says:
380 * Bit-fields are allocated from right to left (least to most significant)
381 * on little-endian implementations and from left to right (most to least
382 * significant) on big-endian implementations.
384 * The above seems to be byte specific, so we need to reverse each
385 * byte of the bitfield. 'Internet' also says this might be implementation
386 * specific and we probably need proper fix and carry perf_event_attr
387 * bitfield flags in separate data file FEAT_ section. Thought this seems
390 static void swap_bitfield(u8 *p, unsigned len)
394 for (i = 0; i < len; i++) {
400 /* exported for swapping attributes in file header */
401 void perf_event__attr_swap(struct perf_event_attr *attr)
403 attr->type = bswap_32(attr->type);
404 attr->size = bswap_32(attr->size);
405 attr->config = bswap_64(attr->config);
406 attr->sample_period = bswap_64(attr->sample_period);
407 attr->sample_type = bswap_64(attr->sample_type);
408 attr->read_format = bswap_64(attr->read_format);
409 attr->wakeup_events = bswap_32(attr->wakeup_events);
410 attr->bp_type = bswap_32(attr->bp_type);
411 attr->bp_addr = bswap_64(attr->bp_addr);
412 attr->bp_len = bswap_64(attr->bp_len);
413 attr->branch_sample_type = bswap_64(attr->branch_sample_type);
414 attr->sample_regs_user = bswap_64(attr->sample_regs_user);
415 attr->sample_stack_user = bswap_32(attr->sample_stack_user);
417 swap_bitfield((u8 *) (&attr->read_format + 1), sizeof(u64));
420 static void perf_event__hdr_attr_swap(union perf_event *event,
421 bool sample_id_all __maybe_unused)
425 perf_event__attr_swap(&event->attr.attr);
427 size = event->header.size;
428 size -= (void *)&event->attr.id - (void *)event;
429 mem_bswap_64(event->attr.id, size);
432 static void perf_event__event_type_swap(union perf_event *event,
433 bool sample_id_all __maybe_unused)
435 event->event_type.event_type.event_id =
436 bswap_64(event->event_type.event_type.event_id);
439 static void perf_event__tracing_data_swap(union perf_event *event,
440 bool sample_id_all __maybe_unused)
442 event->tracing_data.size = bswap_32(event->tracing_data.size);
445 typedef void (*perf_event__swap_op)(union perf_event *event,
448 static perf_event__swap_op perf_event__swap_ops[] = {
449 [PERF_RECORD_MMAP] = perf_event__mmap_swap,
450 [PERF_RECORD_MMAP2] = perf_event__mmap2_swap,
451 [PERF_RECORD_COMM] = perf_event__comm_swap,
452 [PERF_RECORD_FORK] = perf_event__task_swap,
453 [PERF_RECORD_EXIT] = perf_event__task_swap,
454 [PERF_RECORD_LOST] = perf_event__all64_swap,
455 [PERF_RECORD_READ] = perf_event__read_swap,
456 [PERF_RECORD_THROTTLE] = perf_event__throttle_swap,
457 [PERF_RECORD_UNTHROTTLE] = perf_event__throttle_swap,
458 [PERF_RECORD_SAMPLE] = perf_event__all64_swap,
459 [PERF_RECORD_HEADER_ATTR] = perf_event__hdr_attr_swap,
460 [PERF_RECORD_HEADER_EVENT_TYPE] = perf_event__event_type_swap,
461 [PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap,
462 [PERF_RECORD_HEADER_BUILD_ID] = NULL,
463 [PERF_RECORD_HEADER_MAX] = NULL,
467 * When perf record finishes a pass on every buffers, it records this pseudo
469 * We record the max timestamp t found in the pass n.
470 * Assuming these timestamps are monotonic across cpus, we know that if
471 * a buffer still has events with timestamps below t, they will be all
472 * available and then read in the pass n + 1.
473 * Hence when we start to read the pass n + 2, we can safely flush every
474 * events with timestamps below t.
476 * ============ PASS n =================
479 * cnt1 timestamps | cnt2 timestamps
482 * - | 4 <--- max recorded
484 * ============ PASS n + 1 ==============
487 * cnt1 timestamps | cnt2 timestamps
490 * 5 | 7 <---- max recorded
492 * Flush every events below timestamp 4
494 * ============ PASS n + 2 ==============
497 * cnt1 timestamps | cnt2 timestamps
502 * Flush every events below timestamp 7
505 static int process_finished_round(struct perf_tool *tool,
506 union perf_event *event __maybe_unused,
507 struct perf_session *session)
509 return ordered_events__flush(session, tool, OE_FLUSH__ROUND);
512 int perf_session_queue_event(struct perf_session *s, union perf_event *event,
513 struct perf_tool *tool, struct perf_sample *sample,
516 struct ordered_events *oe = &s->ordered_events;
517 u64 timestamp = sample->time;
518 struct ordered_event *new;
520 if (!timestamp || timestamp == ~0ULL)
523 if (timestamp < oe->last_flush) {
524 WARN_ONCE(1, "Timestamp below last timeslice flush\n");
526 pr_oe_time(timestamp, "out of order event");
527 pr_oe_time(oe->last_flush, "last flush, last_flush_type %d\n",
528 oe->last_flush_type);
530 /* We could get out of order messages after forced flush. */
531 if (oe->last_flush_type != OE_FLUSH__HALF)
535 new = ordered_events__new(oe, timestamp);
537 ordered_events__flush(s, tool, OE_FLUSH__HALF);
538 new = ordered_events__new(oe, timestamp);
544 new->file_offset = file_offset;
549 static void callchain__printf(struct perf_sample *sample)
553 printf("... chain: nr:%" PRIu64 "\n", sample->callchain->nr);
555 for (i = 0; i < sample->callchain->nr; i++)
556 printf("..... %2d: %016" PRIx64 "\n",
557 i, sample->callchain->ips[i]);
560 static void branch_stack__printf(struct perf_sample *sample)
564 printf("... branch stack: nr:%" PRIu64 "\n", sample->branch_stack->nr);
566 for (i = 0; i < sample->branch_stack->nr; i++)
567 printf("..... %2"PRIu64": %016" PRIx64 " -> %016" PRIx64 "\n",
568 i, sample->branch_stack->entries[i].from,
569 sample->branch_stack->entries[i].to);
572 static void regs_dump__printf(u64 mask, u64 *regs)
576 for_each_set_bit(rid, (unsigned long *) &mask, sizeof(mask) * 8) {
579 printf(".... %-5s 0x%" PRIx64 "\n",
580 perf_reg_name(rid), val);
584 static void regs_user__printf(struct perf_sample *sample)
586 struct regs_dump *user_regs = &sample->user_regs;
588 if (user_regs->regs) {
589 u64 mask = user_regs->mask;
590 printf("... user regs: mask 0x%" PRIx64 "\n", mask);
591 regs_dump__printf(mask, user_regs->regs);
595 static void stack_user__printf(struct stack_dump *dump)
597 printf("... ustack: size %" PRIu64 ", offset 0x%x\n",
598 dump->size, dump->offset);
601 static void perf_session__print_tstamp(struct perf_session *session,
602 union perf_event *event,
603 struct perf_sample *sample)
605 u64 sample_type = __perf_evlist__combined_sample_type(session->evlist);
607 if (event->header.type != PERF_RECORD_SAMPLE &&
608 !perf_evlist__sample_id_all(session->evlist)) {
609 fputs("-1 -1 ", stdout);
613 if ((sample_type & PERF_SAMPLE_CPU))
614 printf("%u ", sample->cpu);
616 if (sample_type & PERF_SAMPLE_TIME)
617 printf("%" PRIu64 " ", sample->time);
620 static void sample_read__printf(struct perf_sample *sample, u64 read_format)
622 printf("... sample_read:\n");
624 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
625 printf("...... time enabled %016" PRIx64 "\n",
626 sample->read.time_enabled);
628 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
629 printf("...... time running %016" PRIx64 "\n",
630 sample->read.time_running);
632 if (read_format & PERF_FORMAT_GROUP) {
635 printf(".... group nr %" PRIu64 "\n", sample->read.group.nr);
637 for (i = 0; i < sample->read.group.nr; i++) {
638 struct sample_read_value *value;
640 value = &sample->read.group.values[i];
641 printf("..... id %016" PRIx64
642 ", value %016" PRIx64 "\n",
643 value->id, value->value);
646 printf("..... id %016" PRIx64 ", value %016" PRIx64 "\n",
647 sample->read.one.id, sample->read.one.value);
650 static void dump_event(struct perf_session *session, union perf_event *event,
651 u64 file_offset, struct perf_sample *sample)
656 printf("\n%#" PRIx64 " [%#x]: event: %d\n",
657 file_offset, event->header.size, event->header.type);
662 perf_session__print_tstamp(session, event, sample);
664 printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset,
665 event->header.size, perf_event__name(event->header.type));
668 static void dump_sample(struct perf_evsel *evsel, union perf_event *event,
669 struct perf_sample *sample)
676 printf("(IP, 0x%x): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n",
677 event->header.misc, sample->pid, sample->tid, sample->ip,
678 sample->period, sample->addr);
680 sample_type = evsel->attr.sample_type;
682 if (sample_type & PERF_SAMPLE_CALLCHAIN)
683 callchain__printf(sample);
685 if (sample_type & PERF_SAMPLE_BRANCH_STACK)
686 branch_stack__printf(sample);
688 if (sample_type & PERF_SAMPLE_REGS_USER)
689 regs_user__printf(sample);
691 if (sample_type & PERF_SAMPLE_STACK_USER)
692 stack_user__printf(&sample->user_stack);
694 if (sample_type & PERF_SAMPLE_WEIGHT)
695 printf("... weight: %" PRIu64 "\n", sample->weight);
697 if (sample_type & PERF_SAMPLE_DATA_SRC)
698 printf(" . data_src: 0x%"PRIx64"\n", sample->data_src);
700 if (sample_type & PERF_SAMPLE_TRANSACTION)
701 printf("... transaction: %" PRIx64 "\n", sample->transaction);
703 if (sample_type & PERF_SAMPLE_READ)
704 sample_read__printf(sample, evsel->attr.read_format);
707 static struct machine *
708 perf_session__find_machine_for_cpumode(struct perf_session *session,
709 union perf_event *event,
710 struct perf_sample *sample)
712 const u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
713 struct machine *machine;
716 ((cpumode == PERF_RECORD_MISC_GUEST_KERNEL) ||
717 (cpumode == PERF_RECORD_MISC_GUEST_USER))) {
720 if (event->header.type == PERF_RECORD_MMAP
721 || event->header.type == PERF_RECORD_MMAP2)
722 pid = event->mmap.pid;
726 machine = perf_session__find_machine(session, pid);
728 machine = perf_session__findnew_machine(session,
729 DEFAULT_GUEST_KERNEL_ID);
733 return &session->machines.host;
736 static int deliver_sample_value(struct perf_session *session,
737 struct perf_tool *tool,
738 union perf_event *event,
739 struct perf_sample *sample,
740 struct sample_read_value *v,
741 struct machine *machine)
743 struct perf_sample_id *sid;
745 sid = perf_evlist__id2sid(session->evlist, v->id);
748 sample->period = v->value - sid->period;
749 sid->period = v->value;
752 if (!sid || sid->evsel == NULL) {
753 ++session->stats.nr_unknown_id;
757 return tool->sample(tool, event, sample, sid->evsel, machine);
760 static int deliver_sample_group(struct perf_session *session,
761 struct perf_tool *tool,
762 union perf_event *event,
763 struct perf_sample *sample,
764 struct machine *machine)
769 for (i = 0; i < sample->read.group.nr; i++) {
770 ret = deliver_sample_value(session, tool, event, sample,
771 &sample->read.group.values[i],
781 perf_session__deliver_sample(struct perf_session *session,
782 struct perf_tool *tool,
783 union perf_event *event,
784 struct perf_sample *sample,
785 struct perf_evsel *evsel,
786 struct machine *machine)
788 /* We know evsel != NULL. */
789 u64 sample_type = evsel->attr.sample_type;
790 u64 read_format = evsel->attr.read_format;
792 /* Standard sample delievery. */
793 if (!(sample_type & PERF_SAMPLE_READ))
794 return tool->sample(tool, event, sample, evsel, machine);
796 /* For PERF_SAMPLE_READ we have either single or group mode. */
797 if (read_format & PERF_FORMAT_GROUP)
798 return deliver_sample_group(session, tool, event, sample,
801 return deliver_sample_value(session, tool, event, sample,
802 &sample->read.one, machine);
805 int perf_session__deliver_event(struct perf_session *session,
806 union perf_event *event,
807 struct perf_sample *sample,
808 struct perf_tool *tool, u64 file_offset)
810 struct perf_evsel *evsel;
811 struct machine *machine;
813 dump_event(session, event, file_offset, sample);
815 evsel = perf_evlist__id2evsel(session->evlist, sample->id);
817 machine = perf_session__find_machine_for_cpumode(session, event,
820 switch (event->header.type) {
821 case PERF_RECORD_SAMPLE:
822 dump_sample(evsel, event, sample);
824 ++session->stats.nr_unknown_id;
827 if (machine == NULL) {
828 ++session->stats.nr_unprocessable_samples;
831 return perf_session__deliver_sample(session, tool, event,
832 sample, evsel, machine);
833 case PERF_RECORD_MMAP:
834 return tool->mmap(tool, event, sample, machine);
835 case PERF_RECORD_MMAP2:
836 return tool->mmap2(tool, event, sample, machine);
837 case PERF_RECORD_COMM:
838 return tool->comm(tool, event, sample, machine);
839 case PERF_RECORD_FORK:
840 return tool->fork(tool, event, sample, machine);
841 case PERF_RECORD_EXIT:
842 return tool->exit(tool, event, sample, machine);
843 case PERF_RECORD_LOST:
844 if (tool->lost == perf_event__process_lost)
845 session->stats.total_lost += event->lost.lost;
846 return tool->lost(tool, event, sample, machine);
847 case PERF_RECORD_READ:
848 return tool->read(tool, event, sample, evsel, machine);
849 case PERF_RECORD_THROTTLE:
850 return tool->throttle(tool, event, sample, machine);
851 case PERF_RECORD_UNTHROTTLE:
852 return tool->unthrottle(tool, event, sample, machine);
854 ++session->stats.nr_unknown_events;
859 static s64 perf_session__process_user_event(struct perf_session *session,
860 union perf_event *event,
861 struct perf_tool *tool,
864 int fd = perf_data_file__fd(session->file);
867 dump_event(session, event, file_offset, NULL);
869 /* These events are processed right away */
870 switch (event->header.type) {
871 case PERF_RECORD_HEADER_ATTR:
872 err = tool->attr(tool, event, &session->evlist);
874 perf_session__set_id_hdr_size(session);
875 perf_session__set_comm_exec(session);
878 case PERF_RECORD_HEADER_EVENT_TYPE:
880 * Depreceated, but we need to handle it for sake
881 * of old data files create in pipe mode.
884 case PERF_RECORD_HEADER_TRACING_DATA:
885 /* setup for reading amidst mmap */
886 lseek(fd, file_offset, SEEK_SET);
887 return tool->tracing_data(tool, event, session);
888 case PERF_RECORD_HEADER_BUILD_ID:
889 return tool->build_id(tool, event, session);
890 case PERF_RECORD_FINISHED_ROUND:
891 return tool->finished_round(tool, event, session);
897 static void event_swap(union perf_event *event, bool sample_id_all)
899 perf_event__swap_op swap;
901 swap = perf_event__swap_ops[event->header.type];
903 swap(event, sample_id_all);
906 int perf_session__peek_event(struct perf_session *session, off_t file_offset,
907 void *buf, size_t buf_sz,
908 union perf_event **event_ptr,
909 struct perf_sample *sample)
911 union perf_event *event;
915 if (session->one_mmap && !session->header.needs_swap) {
916 event = file_offset - session->one_mmap_offset +
917 session->one_mmap_addr;
918 goto out_parse_sample;
921 if (perf_data_file__is_pipe(session->file))
924 fd = perf_data_file__fd(session->file);
925 hdr_sz = sizeof(struct perf_event_header);
930 if (lseek(fd, file_offset, SEEK_SET) == (off_t)-1 ||
931 readn(fd, &buf, hdr_sz) != (ssize_t)hdr_sz)
934 event = (union perf_event *)buf;
936 if (session->header.needs_swap)
937 perf_event_header__bswap(&event->header);
939 if (event->header.size < hdr_sz)
942 rest = event->header.size - hdr_sz;
944 if (readn(fd, &buf, rest) != (ssize_t)rest)
947 if (session->header.needs_swap)
948 event_swap(event, perf_evlist__sample_id_all(session->evlist));
952 if (sample && event->header.type < PERF_RECORD_USER_TYPE_START &&
953 perf_evlist__parse_sample(session->evlist, event, sample))
961 static s64 perf_session__process_event(struct perf_session *session,
962 union perf_event *event,
963 struct perf_tool *tool,
966 struct perf_sample sample;
969 if (session->header.needs_swap)
970 event_swap(event, perf_evlist__sample_id_all(session->evlist));
972 if (event->header.type >= PERF_RECORD_HEADER_MAX)
975 events_stats__inc(&session->stats, event->header.type);
977 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
978 return perf_session__process_user_event(session, event, tool, file_offset);
981 * For all kernel events we get the sample data
983 ret = perf_evlist__parse_sample(session->evlist, event, &sample);
987 if (tool->ordered_events) {
988 ret = perf_session_queue_event(session, event, tool, &sample,
994 return perf_session__deliver_event(session, event, &sample, tool,
998 void perf_event_header__bswap(struct perf_event_header *hdr)
1000 hdr->type = bswap_32(hdr->type);
1001 hdr->misc = bswap_16(hdr->misc);
1002 hdr->size = bswap_16(hdr->size);
1005 struct thread *perf_session__findnew(struct perf_session *session, pid_t pid)
1007 return machine__findnew_thread(&session->machines.host, -1, pid);
1010 static struct thread *perf_session__register_idle_thread(struct perf_session *session)
1012 struct thread *thread;
1014 thread = machine__findnew_thread(&session->machines.host, 0, 0);
1015 if (thread == NULL || thread__set_comm(thread, "swapper", 0)) {
1016 pr_err("problem inserting idle task.\n");
1023 static void perf_session__warn_about_errors(const struct perf_session *session,
1024 const struct perf_tool *tool)
1026 if (tool->lost == perf_event__process_lost &&
1027 session->stats.nr_events[PERF_RECORD_LOST] != 0) {
1028 ui__warning("Processed %d events and lost %d chunks!\n\n"
1029 "Check IO/CPU overload!\n\n",
1030 session->stats.nr_events[0],
1031 session->stats.nr_events[PERF_RECORD_LOST]);
1034 if (session->stats.nr_unknown_events != 0) {
1035 ui__warning("Found %u unknown events!\n\n"
1036 "Is this an older tool processing a perf.data "
1037 "file generated by a more recent tool?\n\n"
1038 "If that is not the case, consider "
1039 "reporting to linux-kernel@vger.kernel.org.\n\n",
1040 session->stats.nr_unknown_events);
1043 if (session->stats.nr_unknown_id != 0) {
1044 ui__warning("%u samples with id not present in the header\n",
1045 session->stats.nr_unknown_id);
1048 if (session->stats.nr_invalid_chains != 0) {
1049 ui__warning("Found invalid callchains!\n\n"
1050 "%u out of %u events were discarded for this reason.\n\n"
1051 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
1052 session->stats.nr_invalid_chains,
1053 session->stats.nr_events[PERF_RECORD_SAMPLE]);
1056 if (session->stats.nr_unprocessable_samples != 0) {
1057 ui__warning("%u unprocessable samples recorded.\n"
1058 "Do you have a KVM guest running and not using 'perf kvm'?\n",
1059 session->stats.nr_unprocessable_samples);
1063 volatile int session_done;
1065 static int __perf_session__process_pipe_events(struct perf_session *session,
1066 struct perf_tool *tool)
1068 int fd = perf_data_file__fd(session->file);
1069 union perf_event *event;
1070 uint32_t size, cur_size = 0;
1077 perf_tool__fill_defaults(tool);
1080 cur_size = sizeof(union perf_event);
1082 buf = malloc(cur_size);
1087 err = readn(fd, event, sizeof(struct perf_event_header));
1092 pr_err("failed to read event header\n");
1096 if (session->header.needs_swap)
1097 perf_event_header__bswap(&event->header);
1099 size = event->header.size;
1100 if (size < sizeof(struct perf_event_header)) {
1101 pr_err("bad event header size\n");
1105 if (size > cur_size) {
1106 void *new = realloc(buf, size);
1108 pr_err("failed to allocate memory to read event\n");
1116 p += sizeof(struct perf_event_header);
1118 if (size - sizeof(struct perf_event_header)) {
1119 err = readn(fd, p, size - sizeof(struct perf_event_header));
1122 pr_err("unexpected end of event stream\n");
1126 pr_err("failed to read event data\n");
1131 if ((skip = perf_session__process_event(session, event, tool, head)) < 0) {
1132 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
1133 head, event->header.size, event->header.type);
1143 if (!session_done())
1146 /* do the final flush for ordered samples */
1147 err = ordered_events__flush(session, tool, OE_FLUSH__FINAL);
1150 perf_session__warn_about_errors(session, tool);
1151 ordered_events__free(&session->ordered_events);
1155 static union perf_event *
1156 fetch_mmaped_event(struct perf_session *session,
1157 u64 head, size_t mmap_size, char *buf)
1159 union perf_event *event;
1162 * Ensure we have enough space remaining to read
1163 * the size of the event in the headers.
1165 if (head + sizeof(event->header) > mmap_size)
1168 event = (union perf_event *)(buf + head);
1170 if (session->header.needs_swap)
1171 perf_event_header__bswap(&event->header);
1173 if (head + event->header.size > mmap_size) {
1174 /* We're not fetching the event so swap back again */
1175 if (session->header.needs_swap)
1176 perf_event_header__bswap(&event->header);
1184 * On 64bit we can mmap the data file in one go. No need for tiny mmap
1185 * slices. On 32bit we use 32MB.
1187 #if BITS_PER_LONG == 64
1188 #define MMAP_SIZE ULLONG_MAX
1191 #define MMAP_SIZE (32 * 1024 * 1024ULL)
1192 #define NUM_MMAPS 128
1195 int __perf_session__process_events(struct perf_session *session,
1196 u64 data_offset, u64 data_size,
1197 u64 file_size, struct perf_tool *tool)
1199 int fd = perf_data_file__fd(session->file);
1200 u64 head, page_offset, file_offset, file_pos, size;
1201 int err, mmap_prot, mmap_flags, map_idx = 0;
1203 char *buf, *mmaps[NUM_MMAPS];
1204 union perf_event *event;
1205 struct ui_progress prog;
1208 perf_tool__fill_defaults(tool);
1210 page_offset = page_size * (data_offset / page_size);
1211 file_offset = page_offset;
1212 head = data_offset - page_offset;
1214 if (data_size && (data_offset + data_size < file_size))
1215 file_size = data_offset + data_size;
1217 ui_progress__init(&prog, file_size, "Processing events...");
1219 mmap_size = MMAP_SIZE;
1220 if (mmap_size > file_size) {
1221 mmap_size = file_size;
1222 session->one_mmap = true;
1225 memset(mmaps, 0, sizeof(mmaps));
1227 mmap_prot = PROT_READ;
1228 mmap_flags = MAP_SHARED;
1230 if (session->header.needs_swap) {
1231 mmap_prot |= PROT_WRITE;
1232 mmap_flags = MAP_PRIVATE;
1235 buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, fd,
1237 if (buf == MAP_FAILED) {
1238 pr_err("failed to mmap file\n");
1242 mmaps[map_idx] = buf;
1243 map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
1244 file_pos = file_offset + head;
1245 if (session->one_mmap) {
1246 session->one_mmap_addr = buf;
1247 session->one_mmap_offset = file_offset;
1251 event = fetch_mmaped_event(session, head, mmap_size, buf);
1253 if (mmaps[map_idx]) {
1254 munmap(mmaps[map_idx], mmap_size);
1255 mmaps[map_idx] = NULL;
1258 page_offset = page_size * (head / page_size);
1259 file_offset += page_offset;
1260 head -= page_offset;
1264 size = event->header.size;
1266 if (size < sizeof(struct perf_event_header) ||
1267 (skip = perf_session__process_event(session, event, tool, file_pos))
1269 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
1270 file_offset + head, event->header.size,
1271 event->header.type);
1282 ui_progress__update(&prog, size);
1287 if (file_pos < file_size)
1291 /* do the final flush for ordered samples */
1292 err = ordered_events__flush(session, tool, OE_FLUSH__FINAL);
1294 ui_progress__finish();
1295 perf_session__warn_about_errors(session, tool);
1296 ordered_events__free(&session->ordered_events);
1297 session->one_mmap = false;
1301 int perf_session__process_events(struct perf_session *session,
1302 struct perf_tool *tool)
1304 u64 size = perf_data_file__size(session->file);
1307 if (perf_session__register_idle_thread(session) == NULL)
1310 if (!perf_data_file__is_pipe(session->file))
1311 err = __perf_session__process_events(session,
1312 session->header.data_offset,
1313 session->header.data_size,
1316 err = __perf_session__process_pipe_events(session, tool);
1321 bool perf_session__has_traces(struct perf_session *session, const char *msg)
1323 struct perf_evsel *evsel;
1325 evlist__for_each(session->evlist, evsel) {
1326 if (evsel->attr.type == PERF_TYPE_TRACEPOINT)
1330 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
1334 int maps__set_kallsyms_ref_reloc_sym(struct map **maps,
1335 const char *symbol_name, u64 addr)
1339 struct ref_reloc_sym *ref;
1341 ref = zalloc(sizeof(struct ref_reloc_sym));
1345 ref->name = strdup(symbol_name);
1346 if (ref->name == NULL) {
1351 bracket = strchr(ref->name, ']');
1357 for (i = 0; i < MAP__NR_TYPES; ++i) {
1358 struct kmap *kmap = map__kmap(maps[i]);
1359 kmap->ref_reloc_sym = ref;
1365 size_t perf_session__fprintf_dsos(struct perf_session *session, FILE *fp)
1367 return machines__fprintf_dsos(&session->machines, fp);
1370 size_t perf_session__fprintf_dsos_buildid(struct perf_session *session, FILE *fp,
1371 bool (skip)(struct dso *dso, int parm), int parm)
1373 return machines__fprintf_dsos_buildid(&session->machines, fp, skip, parm);
1376 size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp)
1378 struct perf_evsel *pos;
1379 size_t ret = fprintf(fp, "Aggregated stats:\n");
1381 ret += events_stats__fprintf(&session->stats, fp);
1383 evlist__for_each(session->evlist, pos) {
1384 ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
1385 ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
1391 size_t perf_session__fprintf(struct perf_session *session, FILE *fp)
1394 * FIXME: Here we have to actually print all the machines in this
1395 * session, not just the host...
1397 return machine__fprintf(&session->machines.host, fp);
1400 struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
1403 struct perf_evsel *pos;
1405 evlist__for_each(session->evlist, pos) {
1406 if (pos->attr.type == type)
1412 void perf_evsel__print_ip(struct perf_evsel *evsel, struct perf_sample *sample,
1413 struct addr_location *al,
1414 unsigned int print_opts, unsigned int stack_depth)
1416 struct callchain_cursor_node *node;
1417 int print_ip = print_opts & PRINT_IP_OPT_IP;
1418 int print_sym = print_opts & PRINT_IP_OPT_SYM;
1419 int print_dso = print_opts & PRINT_IP_OPT_DSO;
1420 int print_symoffset = print_opts & PRINT_IP_OPT_SYMOFFSET;
1421 int print_oneline = print_opts & PRINT_IP_OPT_ONELINE;
1422 int print_srcline = print_opts & PRINT_IP_OPT_SRCLINE;
1423 char s = print_oneline ? ' ' : '\t';
1425 if (symbol_conf.use_callchain && sample->callchain) {
1426 struct addr_location node_al;
1428 if (machine__resolve_callchain(al->machine, evsel, al->thread,
1430 PERF_MAX_STACK_DEPTH) != 0) {
1432 error("Failed to resolve callchain. Skipping\n");
1435 callchain_cursor_commit(&callchain_cursor);
1437 if (print_symoffset)
1440 while (stack_depth) {
1443 node = callchain_cursor_current(&callchain_cursor);
1447 if (node->sym && node->sym->ignore)
1451 printf("%c%16" PRIx64, s, node->ip);
1454 addr = node->map->map_ip(node->map, node->ip);
1458 if (print_symoffset) {
1459 node_al.addr = addr;
1460 node_al.map = node->map;
1461 symbol__fprintf_symname_offs(node->sym, &node_al, stdout);
1463 symbol__fprintf_symname(node->sym, stdout);
1468 map__fprintf_dsoname(node->map, stdout);
1473 map__fprintf_srcline(node->map, addr, "\n ",
1481 callchain_cursor_advance(&callchain_cursor);
1485 if (al->sym && al->sym->ignore)
1489 printf("%16" PRIx64, sample->ip);
1493 if (print_symoffset)
1494 symbol__fprintf_symname_offs(al->sym, al,
1497 symbol__fprintf_symname(al->sym, stdout);
1502 map__fprintf_dsoname(al->map, stdout);
1507 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
1511 int perf_session__cpu_bitmap(struct perf_session *session,
1512 const char *cpu_list, unsigned long *cpu_bitmap)
1515 struct cpu_map *map;
1517 for (i = 0; i < PERF_TYPE_MAX; ++i) {
1518 struct perf_evsel *evsel;
1520 evsel = perf_session__find_first_evtype(session, i);
1524 if (!(evsel->attr.sample_type & PERF_SAMPLE_CPU)) {
1525 pr_err("File does not contain CPU events. "
1526 "Remove -c option to proceed.\n");
1531 map = cpu_map__new(cpu_list);
1533 pr_err("Invalid cpu_list\n");
1537 for (i = 0; i < map->nr; i++) {
1538 int cpu = map->map[i];
1540 if (cpu >= MAX_NR_CPUS) {
1541 pr_err("Requested CPU %d too large. "
1542 "Consider raising MAX_NR_CPUS\n", cpu);
1543 goto out_delete_map;
1546 set_bit(cpu, cpu_bitmap);
1552 cpu_map__delete(map);
1556 void perf_session__fprintf_info(struct perf_session *session, FILE *fp,
1562 if (session == NULL || fp == NULL)
1565 fd = perf_data_file__fd(session->file);
1567 ret = fstat(fd, &st);
1571 fprintf(fp, "# ========\n");
1572 fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
1573 perf_header__fprintf_info(session, fp, full);
1574 fprintf(fp, "# ========\n#\n");
1578 int __perf_session__set_tracepoints_handlers(struct perf_session *session,
1579 const struct perf_evsel_str_handler *assocs,
1582 struct perf_evsel *evsel;
1586 for (i = 0; i < nr_assocs; i++) {
1588 * Adding a handler for an event not in the session,
1591 evsel = perf_evlist__find_tracepoint_by_name(session->evlist, assocs[i].name);
1596 if (evsel->handler != NULL)
1598 evsel->handler = assocs[i].handler;