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);
816 if (evsel != NULL && event->header.type != PERF_RECORD_SAMPLE) {
818 * XXX We're leaving PERF_RECORD_SAMPLE unnacounted here
819 * because the tools right now may apply filters, discarding
820 * some of the samples. For consistency, in the future we
821 * should have something like nr_filtered_samples and remove
822 * the sample->period from total_sample_period, etc, KISS for
825 * Also testing against NULL allows us to handle files without
826 * attr.sample_id_all and/or without PERF_SAMPLE_ID. In the
827 * future probably it'll be a good idea to restrict event
828 * processing via perf_session to files with both set.
830 hists__inc_nr_events(&evsel->hists, event->header.type);
833 machine = perf_session__find_machine_for_cpumode(session, event,
836 switch (event->header.type) {
837 case PERF_RECORD_SAMPLE:
838 dump_sample(evsel, event, sample);
840 ++session->stats.nr_unknown_id;
843 if (machine == NULL) {
844 ++session->stats.nr_unprocessable_samples;
847 return perf_session__deliver_sample(session, tool, event,
848 sample, evsel, machine);
849 case PERF_RECORD_MMAP:
850 return tool->mmap(tool, event, sample, machine);
851 case PERF_RECORD_MMAP2:
852 return tool->mmap2(tool, event, sample, machine);
853 case PERF_RECORD_COMM:
854 return tool->comm(tool, event, sample, machine);
855 case PERF_RECORD_FORK:
856 return tool->fork(tool, event, sample, machine);
857 case PERF_RECORD_EXIT:
858 return tool->exit(tool, event, sample, machine);
859 case PERF_RECORD_LOST:
860 if (tool->lost == perf_event__process_lost)
861 session->stats.total_lost += event->lost.lost;
862 return tool->lost(tool, event, sample, machine);
863 case PERF_RECORD_READ:
864 return tool->read(tool, event, sample, evsel, machine);
865 case PERF_RECORD_THROTTLE:
866 return tool->throttle(tool, event, sample, machine);
867 case PERF_RECORD_UNTHROTTLE:
868 return tool->unthrottle(tool, event, sample, machine);
870 ++session->stats.nr_unknown_events;
875 static s64 perf_session__process_user_event(struct perf_session *session,
876 union perf_event *event,
877 struct perf_tool *tool,
880 int fd = perf_data_file__fd(session->file);
883 dump_event(session, event, file_offset, NULL);
885 /* These events are processed right away */
886 switch (event->header.type) {
887 case PERF_RECORD_HEADER_ATTR:
888 err = tool->attr(tool, event, &session->evlist);
890 perf_session__set_id_hdr_size(session);
891 perf_session__set_comm_exec(session);
894 case PERF_RECORD_HEADER_EVENT_TYPE:
896 * Depreceated, but we need to handle it for sake
897 * of old data files create in pipe mode.
900 case PERF_RECORD_HEADER_TRACING_DATA:
901 /* setup for reading amidst mmap */
902 lseek(fd, file_offset, SEEK_SET);
903 return tool->tracing_data(tool, event, session);
904 case PERF_RECORD_HEADER_BUILD_ID:
905 return tool->build_id(tool, event, session);
906 case PERF_RECORD_FINISHED_ROUND:
907 return tool->finished_round(tool, event, session);
913 static void event_swap(union perf_event *event, bool sample_id_all)
915 perf_event__swap_op swap;
917 swap = perf_event__swap_ops[event->header.type];
919 swap(event, sample_id_all);
922 int perf_session__peek_event(struct perf_session *session, off_t file_offset,
923 void *buf, size_t buf_sz,
924 union perf_event **event_ptr,
925 struct perf_sample *sample)
927 union perf_event *event;
931 if (session->one_mmap && !session->header.needs_swap) {
932 event = file_offset - session->one_mmap_offset +
933 session->one_mmap_addr;
934 goto out_parse_sample;
937 if (perf_data_file__is_pipe(session->file))
940 fd = perf_data_file__fd(session->file);
941 hdr_sz = sizeof(struct perf_event_header);
946 if (lseek(fd, file_offset, SEEK_SET) == (off_t)-1 ||
947 readn(fd, &buf, hdr_sz) != (ssize_t)hdr_sz)
950 event = (union perf_event *)buf;
952 if (session->header.needs_swap)
953 perf_event_header__bswap(&event->header);
955 if (event->header.size < hdr_sz)
958 rest = event->header.size - hdr_sz;
960 if (readn(fd, &buf, rest) != (ssize_t)rest)
963 if (session->header.needs_swap)
964 event_swap(event, perf_evlist__sample_id_all(session->evlist));
968 if (sample && event->header.type < PERF_RECORD_USER_TYPE_START &&
969 perf_evlist__parse_sample(session->evlist, event, sample))
977 static s64 perf_session__process_event(struct perf_session *session,
978 union perf_event *event,
979 struct perf_tool *tool,
982 struct perf_sample sample;
985 if (session->header.needs_swap)
986 event_swap(event, perf_evlist__sample_id_all(session->evlist));
988 if (event->header.type >= PERF_RECORD_HEADER_MAX)
991 events_stats__inc(&session->stats, event->header.type);
993 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
994 return perf_session__process_user_event(session, event, tool, file_offset);
997 * For all kernel events we get the sample data
999 ret = perf_evlist__parse_sample(session->evlist, event, &sample);
1003 if (tool->ordered_events) {
1004 ret = perf_session_queue_event(session, event, tool, &sample,
1010 return perf_session__deliver_event(session, event, &sample, tool,
1014 void perf_event_header__bswap(struct perf_event_header *hdr)
1016 hdr->type = bswap_32(hdr->type);
1017 hdr->misc = bswap_16(hdr->misc);
1018 hdr->size = bswap_16(hdr->size);
1021 struct thread *perf_session__findnew(struct perf_session *session, pid_t pid)
1023 return machine__findnew_thread(&session->machines.host, -1, pid);
1026 static struct thread *perf_session__register_idle_thread(struct perf_session *session)
1028 struct thread *thread;
1030 thread = machine__findnew_thread(&session->machines.host, 0, 0);
1031 if (thread == NULL || thread__set_comm(thread, "swapper", 0)) {
1032 pr_err("problem inserting idle task.\n");
1039 static void perf_session__warn_about_errors(const struct perf_session *session,
1040 const struct perf_tool *tool)
1042 if (tool->lost == perf_event__process_lost &&
1043 session->stats.nr_events[PERF_RECORD_LOST] != 0) {
1044 ui__warning("Processed %d events and lost %d chunks!\n\n"
1045 "Check IO/CPU overload!\n\n",
1046 session->stats.nr_events[0],
1047 session->stats.nr_events[PERF_RECORD_LOST]);
1050 if (session->stats.nr_unknown_events != 0) {
1051 ui__warning("Found %u unknown events!\n\n"
1052 "Is this an older tool processing a perf.data "
1053 "file generated by a more recent tool?\n\n"
1054 "If that is not the case, consider "
1055 "reporting to linux-kernel@vger.kernel.org.\n\n",
1056 session->stats.nr_unknown_events);
1059 if (session->stats.nr_unknown_id != 0) {
1060 ui__warning("%u samples with id not present in the header\n",
1061 session->stats.nr_unknown_id);
1064 if (session->stats.nr_invalid_chains != 0) {
1065 ui__warning("Found invalid callchains!\n\n"
1066 "%u out of %u events were discarded for this reason.\n\n"
1067 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
1068 session->stats.nr_invalid_chains,
1069 session->stats.nr_events[PERF_RECORD_SAMPLE]);
1072 if (session->stats.nr_unprocessable_samples != 0) {
1073 ui__warning("%u unprocessable samples recorded.\n"
1074 "Do you have a KVM guest running and not using 'perf kvm'?\n",
1075 session->stats.nr_unprocessable_samples);
1079 volatile int session_done;
1081 static int __perf_session__process_pipe_events(struct perf_session *session,
1082 struct perf_tool *tool)
1084 int fd = perf_data_file__fd(session->file);
1085 union perf_event *event;
1086 uint32_t size, cur_size = 0;
1093 perf_tool__fill_defaults(tool);
1096 cur_size = sizeof(union perf_event);
1098 buf = malloc(cur_size);
1103 err = readn(fd, event, sizeof(struct perf_event_header));
1108 pr_err("failed to read event header\n");
1112 if (session->header.needs_swap)
1113 perf_event_header__bswap(&event->header);
1115 size = event->header.size;
1116 if (size < sizeof(struct perf_event_header)) {
1117 pr_err("bad event header size\n");
1121 if (size > cur_size) {
1122 void *new = realloc(buf, size);
1124 pr_err("failed to allocate memory to read event\n");
1132 p += sizeof(struct perf_event_header);
1134 if (size - sizeof(struct perf_event_header)) {
1135 err = readn(fd, p, size - sizeof(struct perf_event_header));
1138 pr_err("unexpected end of event stream\n");
1142 pr_err("failed to read event data\n");
1147 if ((skip = perf_session__process_event(session, event, tool, head)) < 0) {
1148 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
1149 head, event->header.size, event->header.type);
1159 if (!session_done())
1162 /* do the final flush for ordered samples */
1163 err = ordered_events__flush(session, tool, OE_FLUSH__FINAL);
1166 perf_session__warn_about_errors(session, tool);
1167 ordered_events__free(&session->ordered_events);
1171 static union perf_event *
1172 fetch_mmaped_event(struct perf_session *session,
1173 u64 head, size_t mmap_size, char *buf)
1175 union perf_event *event;
1178 * Ensure we have enough space remaining to read
1179 * the size of the event in the headers.
1181 if (head + sizeof(event->header) > mmap_size)
1184 event = (union perf_event *)(buf + head);
1186 if (session->header.needs_swap)
1187 perf_event_header__bswap(&event->header);
1189 if (head + event->header.size > mmap_size) {
1190 /* We're not fetching the event so swap back again */
1191 if (session->header.needs_swap)
1192 perf_event_header__bswap(&event->header);
1200 * On 64bit we can mmap the data file in one go. No need for tiny mmap
1201 * slices. On 32bit we use 32MB.
1203 #if BITS_PER_LONG == 64
1204 #define MMAP_SIZE ULLONG_MAX
1207 #define MMAP_SIZE (32 * 1024 * 1024ULL)
1208 #define NUM_MMAPS 128
1211 int __perf_session__process_events(struct perf_session *session,
1212 u64 data_offset, u64 data_size,
1213 u64 file_size, struct perf_tool *tool)
1215 int fd = perf_data_file__fd(session->file);
1216 u64 head, page_offset, file_offset, file_pos, size;
1217 int err, mmap_prot, mmap_flags, map_idx = 0;
1219 char *buf, *mmaps[NUM_MMAPS];
1220 union perf_event *event;
1221 struct ui_progress prog;
1224 perf_tool__fill_defaults(tool);
1226 page_offset = page_size * (data_offset / page_size);
1227 file_offset = page_offset;
1228 head = data_offset - page_offset;
1230 if (data_size && (data_offset + data_size < file_size))
1231 file_size = data_offset + data_size;
1233 ui_progress__init(&prog, file_size, "Processing events...");
1235 mmap_size = MMAP_SIZE;
1236 if (mmap_size > file_size) {
1237 mmap_size = file_size;
1238 session->one_mmap = true;
1241 memset(mmaps, 0, sizeof(mmaps));
1243 mmap_prot = PROT_READ;
1244 mmap_flags = MAP_SHARED;
1246 if (session->header.needs_swap) {
1247 mmap_prot |= PROT_WRITE;
1248 mmap_flags = MAP_PRIVATE;
1251 buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, fd,
1253 if (buf == MAP_FAILED) {
1254 pr_err("failed to mmap file\n");
1258 mmaps[map_idx] = buf;
1259 map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
1260 file_pos = file_offset + head;
1261 if (session->one_mmap) {
1262 session->one_mmap_addr = buf;
1263 session->one_mmap_offset = file_offset;
1267 event = fetch_mmaped_event(session, head, mmap_size, buf);
1269 if (mmaps[map_idx]) {
1270 munmap(mmaps[map_idx], mmap_size);
1271 mmaps[map_idx] = NULL;
1274 page_offset = page_size * (head / page_size);
1275 file_offset += page_offset;
1276 head -= page_offset;
1280 size = event->header.size;
1282 if (size < sizeof(struct perf_event_header) ||
1283 (skip = perf_session__process_event(session, event, tool, file_pos))
1285 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
1286 file_offset + head, event->header.size,
1287 event->header.type);
1298 ui_progress__update(&prog, size);
1303 if (file_pos < file_size)
1307 /* do the final flush for ordered samples */
1308 err = ordered_events__flush(session, tool, OE_FLUSH__FINAL);
1310 ui_progress__finish();
1311 perf_session__warn_about_errors(session, tool);
1312 ordered_events__free(&session->ordered_events);
1313 session->one_mmap = false;
1317 int perf_session__process_events(struct perf_session *session,
1318 struct perf_tool *tool)
1320 u64 size = perf_data_file__size(session->file);
1323 if (perf_session__register_idle_thread(session) == NULL)
1326 if (!perf_data_file__is_pipe(session->file))
1327 err = __perf_session__process_events(session,
1328 session->header.data_offset,
1329 session->header.data_size,
1332 err = __perf_session__process_pipe_events(session, tool);
1337 bool perf_session__has_traces(struct perf_session *session, const char *msg)
1339 struct perf_evsel *evsel;
1341 evlist__for_each(session->evlist, evsel) {
1342 if (evsel->attr.type == PERF_TYPE_TRACEPOINT)
1346 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
1350 int maps__set_kallsyms_ref_reloc_sym(struct map **maps,
1351 const char *symbol_name, u64 addr)
1355 struct ref_reloc_sym *ref;
1357 ref = zalloc(sizeof(struct ref_reloc_sym));
1361 ref->name = strdup(symbol_name);
1362 if (ref->name == NULL) {
1367 bracket = strchr(ref->name, ']');
1373 for (i = 0; i < MAP__NR_TYPES; ++i) {
1374 struct kmap *kmap = map__kmap(maps[i]);
1375 kmap->ref_reloc_sym = ref;
1381 size_t perf_session__fprintf_dsos(struct perf_session *session, FILE *fp)
1383 return machines__fprintf_dsos(&session->machines, fp);
1386 size_t perf_session__fprintf_dsos_buildid(struct perf_session *session, FILE *fp,
1387 bool (skip)(struct dso *dso, int parm), int parm)
1389 return machines__fprintf_dsos_buildid(&session->machines, fp, skip, parm);
1392 size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp)
1394 struct perf_evsel *pos;
1395 size_t ret = fprintf(fp, "Aggregated stats:\n");
1397 ret += events_stats__fprintf(&session->stats, fp);
1399 evlist__for_each(session->evlist, pos) {
1400 ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
1401 ret += events_stats__fprintf(&pos->hists.stats, fp);
1407 size_t perf_session__fprintf(struct perf_session *session, FILE *fp)
1410 * FIXME: Here we have to actually print all the machines in this
1411 * session, not just the host...
1413 return machine__fprintf(&session->machines.host, fp);
1416 struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
1419 struct perf_evsel *pos;
1421 evlist__for_each(session->evlist, pos) {
1422 if (pos->attr.type == type)
1428 void perf_evsel__print_ip(struct perf_evsel *evsel, struct perf_sample *sample,
1429 struct addr_location *al,
1430 unsigned int print_opts, unsigned int stack_depth)
1432 struct callchain_cursor_node *node;
1433 int print_ip = print_opts & PRINT_IP_OPT_IP;
1434 int print_sym = print_opts & PRINT_IP_OPT_SYM;
1435 int print_dso = print_opts & PRINT_IP_OPT_DSO;
1436 int print_symoffset = print_opts & PRINT_IP_OPT_SYMOFFSET;
1437 int print_oneline = print_opts & PRINT_IP_OPT_ONELINE;
1438 int print_srcline = print_opts & PRINT_IP_OPT_SRCLINE;
1439 char s = print_oneline ? ' ' : '\t';
1441 if (symbol_conf.use_callchain && sample->callchain) {
1442 struct addr_location node_al;
1444 if (machine__resolve_callchain(al->machine, evsel, al->thread,
1446 PERF_MAX_STACK_DEPTH) != 0) {
1448 error("Failed to resolve callchain. Skipping\n");
1451 callchain_cursor_commit(&callchain_cursor);
1453 if (print_symoffset)
1456 while (stack_depth) {
1459 node = callchain_cursor_current(&callchain_cursor);
1463 if (node->sym && node->sym->ignore)
1467 printf("%c%16" PRIx64, s, node->ip);
1470 addr = node->map->map_ip(node->map, node->ip);
1474 if (print_symoffset) {
1475 node_al.addr = addr;
1476 node_al.map = node->map;
1477 symbol__fprintf_symname_offs(node->sym, &node_al, stdout);
1479 symbol__fprintf_symname(node->sym, stdout);
1484 map__fprintf_dsoname(node->map, stdout);
1489 map__fprintf_srcline(node->map, addr, "\n ",
1497 callchain_cursor_advance(&callchain_cursor);
1501 if (al->sym && al->sym->ignore)
1505 printf("%16" PRIx64, sample->ip);
1509 if (print_symoffset)
1510 symbol__fprintf_symname_offs(al->sym, al,
1513 symbol__fprintf_symname(al->sym, stdout);
1518 map__fprintf_dsoname(al->map, stdout);
1523 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
1527 int perf_session__cpu_bitmap(struct perf_session *session,
1528 const char *cpu_list, unsigned long *cpu_bitmap)
1531 struct cpu_map *map;
1533 for (i = 0; i < PERF_TYPE_MAX; ++i) {
1534 struct perf_evsel *evsel;
1536 evsel = perf_session__find_first_evtype(session, i);
1540 if (!(evsel->attr.sample_type & PERF_SAMPLE_CPU)) {
1541 pr_err("File does not contain CPU events. "
1542 "Remove -c option to proceed.\n");
1547 map = cpu_map__new(cpu_list);
1549 pr_err("Invalid cpu_list\n");
1553 for (i = 0; i < map->nr; i++) {
1554 int cpu = map->map[i];
1556 if (cpu >= MAX_NR_CPUS) {
1557 pr_err("Requested CPU %d too large. "
1558 "Consider raising MAX_NR_CPUS\n", cpu);
1559 goto out_delete_map;
1562 set_bit(cpu, cpu_bitmap);
1568 cpu_map__delete(map);
1572 void perf_session__fprintf_info(struct perf_session *session, FILE *fp,
1578 if (session == NULL || fp == NULL)
1581 fd = perf_data_file__fd(session->file);
1583 ret = fstat(fd, &st);
1587 fprintf(fp, "# ========\n");
1588 fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
1589 perf_header__fprintf_info(session, fp, full);
1590 fprintf(fp, "# ========\n#\n");
1594 int __perf_session__set_tracepoints_handlers(struct perf_session *session,
1595 const struct perf_evsel_str_handler *assocs,
1598 struct perf_evsel *evsel;
1602 for (i = 0; i < nr_assocs; i++) {
1604 * Adding a handler for an event not in the session,
1607 evsel = perf_evlist__find_tracepoint_by_name(session->evlist, assocs[i].name);
1612 if (evsel->handler != NULL)
1614 evsel->handler = assocs[i].handler;