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_threads(struct perf_session *session)
143 machine__delete_threads(&session->machines.host);
146 static void perf_session_env__delete(struct perf_session_env *env)
148 zfree(&env->hostname);
149 zfree(&env->os_release);
150 zfree(&env->version);
152 zfree(&env->cpu_desc);
155 zfree(&env->cmdline);
156 zfree(&env->sibling_cores);
157 zfree(&env->sibling_threads);
158 zfree(&env->numa_nodes);
159 zfree(&env->pmu_mappings);
162 void perf_session__delete(struct perf_session *session)
164 perf_session__destroy_kernel_maps(session);
165 perf_session__delete_threads(session);
166 perf_session_env__delete(&session->header.env);
167 machines__exit(&session->machines);
169 perf_data_file__close(session->file);
173 static int process_event_synth_tracing_data_stub(struct perf_tool *tool
175 union perf_event *event
177 struct perf_session *session
180 dump_printf(": unhandled!\n");
184 static int process_event_synth_attr_stub(struct perf_tool *tool __maybe_unused,
185 union perf_event *event __maybe_unused,
186 struct perf_evlist **pevlist
189 dump_printf(": unhandled!\n");
193 static int process_event_sample_stub(struct perf_tool *tool __maybe_unused,
194 union perf_event *event __maybe_unused,
195 struct perf_sample *sample __maybe_unused,
196 struct perf_evsel *evsel __maybe_unused,
197 struct machine *machine __maybe_unused)
199 dump_printf(": unhandled!\n");
203 static int process_event_stub(struct perf_tool *tool __maybe_unused,
204 union perf_event *event __maybe_unused,
205 struct perf_sample *sample __maybe_unused,
206 struct machine *machine __maybe_unused)
208 dump_printf(": unhandled!\n");
212 static int process_finished_round_stub(struct perf_tool *tool __maybe_unused,
213 union perf_event *event __maybe_unused,
214 struct perf_session *perf_session
217 dump_printf(": unhandled!\n");
221 static int process_finished_round(struct perf_tool *tool,
222 union perf_event *event,
223 struct perf_session *session);
225 static int process_id_index_stub(struct perf_tool *tool __maybe_unused,
226 union perf_event *event __maybe_unused,
227 struct perf_session *perf_session
230 dump_printf(": unhandled!\n");
234 void perf_tool__fill_defaults(struct perf_tool *tool)
236 if (tool->sample == NULL)
237 tool->sample = process_event_sample_stub;
238 if (tool->mmap == NULL)
239 tool->mmap = process_event_stub;
240 if (tool->mmap2 == NULL)
241 tool->mmap2 = process_event_stub;
242 if (tool->comm == NULL)
243 tool->comm = process_event_stub;
244 if (tool->fork == NULL)
245 tool->fork = process_event_stub;
246 if (tool->exit == NULL)
247 tool->exit = process_event_stub;
248 if (tool->lost == NULL)
249 tool->lost = perf_event__process_lost;
250 if (tool->read == NULL)
251 tool->read = process_event_sample_stub;
252 if (tool->throttle == NULL)
253 tool->throttle = process_event_stub;
254 if (tool->unthrottle == NULL)
255 tool->unthrottle = process_event_stub;
256 if (tool->attr == NULL)
257 tool->attr = process_event_synth_attr_stub;
258 if (tool->tracing_data == NULL)
259 tool->tracing_data = process_event_synth_tracing_data_stub;
260 if (tool->build_id == NULL)
261 tool->build_id = process_finished_round_stub;
262 if (tool->finished_round == NULL) {
263 if (tool->ordered_events)
264 tool->finished_round = process_finished_round;
266 tool->finished_round = process_finished_round_stub;
268 if (tool->id_index == NULL)
269 tool->id_index = process_id_index_stub;
272 static void swap_sample_id_all(union perf_event *event, void *data)
274 void *end = (void *) event + event->header.size;
275 int size = end - data;
277 BUG_ON(size % sizeof(u64));
278 mem_bswap_64(data, size);
281 static void perf_event__all64_swap(union perf_event *event,
282 bool sample_id_all __maybe_unused)
284 struct perf_event_header *hdr = &event->header;
285 mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr));
288 static void perf_event__comm_swap(union perf_event *event, bool sample_id_all)
290 event->comm.pid = bswap_32(event->comm.pid);
291 event->comm.tid = bswap_32(event->comm.tid);
294 void *data = &event->comm.comm;
296 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
297 swap_sample_id_all(event, data);
301 static void perf_event__mmap_swap(union perf_event *event,
304 event->mmap.pid = bswap_32(event->mmap.pid);
305 event->mmap.tid = bswap_32(event->mmap.tid);
306 event->mmap.start = bswap_64(event->mmap.start);
307 event->mmap.len = bswap_64(event->mmap.len);
308 event->mmap.pgoff = bswap_64(event->mmap.pgoff);
311 void *data = &event->mmap.filename;
313 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
314 swap_sample_id_all(event, data);
318 static void perf_event__mmap2_swap(union perf_event *event,
321 event->mmap2.pid = bswap_32(event->mmap2.pid);
322 event->mmap2.tid = bswap_32(event->mmap2.tid);
323 event->mmap2.start = bswap_64(event->mmap2.start);
324 event->mmap2.len = bswap_64(event->mmap2.len);
325 event->mmap2.pgoff = bswap_64(event->mmap2.pgoff);
326 event->mmap2.maj = bswap_32(event->mmap2.maj);
327 event->mmap2.min = bswap_32(event->mmap2.min);
328 event->mmap2.ino = bswap_64(event->mmap2.ino);
331 void *data = &event->mmap2.filename;
333 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
334 swap_sample_id_all(event, data);
337 static void perf_event__task_swap(union perf_event *event, bool sample_id_all)
339 event->fork.pid = bswap_32(event->fork.pid);
340 event->fork.tid = bswap_32(event->fork.tid);
341 event->fork.ppid = bswap_32(event->fork.ppid);
342 event->fork.ptid = bswap_32(event->fork.ptid);
343 event->fork.time = bswap_64(event->fork.time);
346 swap_sample_id_all(event, &event->fork + 1);
349 static void perf_event__read_swap(union perf_event *event, bool sample_id_all)
351 event->read.pid = bswap_32(event->read.pid);
352 event->read.tid = bswap_32(event->read.tid);
353 event->read.value = bswap_64(event->read.value);
354 event->read.time_enabled = bswap_64(event->read.time_enabled);
355 event->read.time_running = bswap_64(event->read.time_running);
356 event->read.id = bswap_64(event->read.id);
359 swap_sample_id_all(event, &event->read + 1);
362 static void perf_event__throttle_swap(union perf_event *event,
365 event->throttle.time = bswap_64(event->throttle.time);
366 event->throttle.id = bswap_64(event->throttle.id);
367 event->throttle.stream_id = bswap_64(event->throttle.stream_id);
370 swap_sample_id_all(event, &event->throttle + 1);
373 static u8 revbyte(u8 b)
375 int rev = (b >> 4) | ((b & 0xf) << 4);
376 rev = ((rev & 0xcc) >> 2) | ((rev & 0x33) << 2);
377 rev = ((rev & 0xaa) >> 1) | ((rev & 0x55) << 1);
382 * XXX this is hack in attempt to carry flags bitfield
383 * throught endian village. ABI says:
385 * Bit-fields are allocated from right to left (least to most significant)
386 * on little-endian implementations and from left to right (most to least
387 * significant) on big-endian implementations.
389 * The above seems to be byte specific, so we need to reverse each
390 * byte of the bitfield. 'Internet' also says this might be implementation
391 * specific and we probably need proper fix and carry perf_event_attr
392 * bitfield flags in separate data file FEAT_ section. Thought this seems
395 static void swap_bitfield(u8 *p, unsigned len)
399 for (i = 0; i < len; i++) {
405 /* exported for swapping attributes in file header */
406 void perf_event__attr_swap(struct perf_event_attr *attr)
408 attr->type = bswap_32(attr->type);
409 attr->size = bswap_32(attr->size);
410 attr->config = bswap_64(attr->config);
411 attr->sample_period = bswap_64(attr->sample_period);
412 attr->sample_type = bswap_64(attr->sample_type);
413 attr->read_format = bswap_64(attr->read_format);
414 attr->wakeup_events = bswap_32(attr->wakeup_events);
415 attr->bp_type = bswap_32(attr->bp_type);
416 attr->bp_addr = bswap_64(attr->bp_addr);
417 attr->bp_len = bswap_64(attr->bp_len);
418 attr->branch_sample_type = bswap_64(attr->branch_sample_type);
419 attr->sample_regs_user = bswap_64(attr->sample_regs_user);
420 attr->sample_stack_user = bswap_32(attr->sample_stack_user);
422 swap_bitfield((u8 *) (&attr->read_format + 1), sizeof(u64));
425 static void perf_event__hdr_attr_swap(union perf_event *event,
426 bool sample_id_all __maybe_unused)
430 perf_event__attr_swap(&event->attr.attr);
432 size = event->header.size;
433 size -= (void *)&event->attr.id - (void *)event;
434 mem_bswap_64(event->attr.id, size);
437 static void perf_event__event_type_swap(union perf_event *event,
438 bool sample_id_all __maybe_unused)
440 event->event_type.event_type.event_id =
441 bswap_64(event->event_type.event_type.event_id);
444 static void perf_event__tracing_data_swap(union perf_event *event,
445 bool sample_id_all __maybe_unused)
447 event->tracing_data.size = bswap_32(event->tracing_data.size);
450 typedef void (*perf_event__swap_op)(union perf_event *event,
453 static perf_event__swap_op perf_event__swap_ops[] = {
454 [PERF_RECORD_MMAP] = perf_event__mmap_swap,
455 [PERF_RECORD_MMAP2] = perf_event__mmap2_swap,
456 [PERF_RECORD_COMM] = perf_event__comm_swap,
457 [PERF_RECORD_FORK] = perf_event__task_swap,
458 [PERF_RECORD_EXIT] = perf_event__task_swap,
459 [PERF_RECORD_LOST] = perf_event__all64_swap,
460 [PERF_RECORD_READ] = perf_event__read_swap,
461 [PERF_RECORD_THROTTLE] = perf_event__throttle_swap,
462 [PERF_RECORD_UNTHROTTLE] = perf_event__throttle_swap,
463 [PERF_RECORD_SAMPLE] = perf_event__all64_swap,
464 [PERF_RECORD_HEADER_ATTR] = perf_event__hdr_attr_swap,
465 [PERF_RECORD_HEADER_EVENT_TYPE] = perf_event__event_type_swap,
466 [PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap,
467 [PERF_RECORD_HEADER_BUILD_ID] = NULL,
468 [PERF_RECORD_ID_INDEX] = perf_event__all64_swap,
469 [PERF_RECORD_HEADER_MAX] = NULL,
473 * When perf record finishes a pass on every buffers, it records this pseudo
475 * We record the max timestamp t found in the pass n.
476 * Assuming these timestamps are monotonic across cpus, we know that if
477 * a buffer still has events with timestamps below t, they will be all
478 * available and then read in the pass n + 1.
479 * Hence when we start to read the pass n + 2, we can safely flush every
480 * events with timestamps below t.
482 * ============ PASS n =================
485 * cnt1 timestamps | cnt2 timestamps
488 * - | 4 <--- max recorded
490 * ============ PASS n + 1 ==============
493 * cnt1 timestamps | cnt2 timestamps
496 * 5 | 7 <---- max recorded
498 * Flush every events below timestamp 4
500 * ============ PASS n + 2 ==============
503 * cnt1 timestamps | cnt2 timestamps
508 * Flush every events below timestamp 7
511 static int process_finished_round(struct perf_tool *tool,
512 union perf_event *event __maybe_unused,
513 struct perf_session *session)
515 struct ordered_events *oe = &session->ordered_events;
516 struct perf_evlist *evlist = session->evlist;
517 struct machines *machines = &session->machines;
519 return ordered_events__flush(oe, machines, evlist, tool, OE_FLUSH__ROUND);
522 int perf_session_queue_event(struct perf_session *s, union perf_event *event,
523 struct perf_tool *tool, struct perf_sample *sample,
526 struct ordered_events *oe = &s->ordered_events;
527 struct perf_evlist *evlist = s->evlist;
528 struct machines *machines = &s->machines;
530 u64 timestamp = sample->time;
531 struct ordered_event *new;
533 if (!timestamp || timestamp == ~0ULL)
536 if (timestamp < oe->last_flush) {
537 pr_oe_time(timestamp, "out of order event\n");
538 pr_oe_time(oe->last_flush, "last flush, last_flush_type %d\n",
539 oe->last_flush_type);
541 s->evlist->stats.nr_unordered_events++;
544 new = ordered_events__new(oe, timestamp, event);
546 ordered_events__flush(oe, machines, evlist, tool, OE_FLUSH__HALF);
547 new = ordered_events__new(oe, timestamp, event);
553 new->file_offset = file_offset;
557 static void callchain__lbr_callstack_printf(struct perf_sample *sample)
559 struct ip_callchain *callchain = sample->callchain;
560 struct branch_stack *lbr_stack = sample->branch_stack;
561 u64 kernel_callchain_nr = callchain->nr;
564 for (i = 0; i < kernel_callchain_nr; i++) {
565 if (callchain->ips[i] == PERF_CONTEXT_USER)
569 if ((i != kernel_callchain_nr) && lbr_stack->nr) {
572 * LBR callstack can only get user call chain,
573 * i is kernel call chain number,
574 * 1 is PERF_CONTEXT_USER.
576 * The user call chain is stored in LBR registers.
577 * LBR are pair registers. The caller is stored
578 * in "from" register, while the callee is stored
580 * For example, there is a call stack
581 * "A"->"B"->"C"->"D".
582 * The LBR registers will recorde like
583 * "C"->"D", "B"->"C", "A"->"B".
584 * So only the first "to" register and all "from"
585 * registers are needed to construct the whole stack.
587 total_nr = i + 1 + lbr_stack->nr + 1;
588 kernel_callchain_nr = i + 1;
590 printf("... LBR call chain: nr:%" PRIu64 "\n", total_nr);
592 for (i = 0; i < kernel_callchain_nr; i++)
593 printf("..... %2d: %016" PRIx64 "\n",
594 i, callchain->ips[i]);
596 printf("..... %2d: %016" PRIx64 "\n",
597 (int)(kernel_callchain_nr), lbr_stack->entries[0].to);
598 for (i = 0; i < lbr_stack->nr; i++)
599 printf("..... %2d: %016" PRIx64 "\n",
600 (int)(i + kernel_callchain_nr + 1), lbr_stack->entries[i].from);
604 static void callchain__printf(struct perf_evsel *evsel,
605 struct perf_sample *sample)
608 struct ip_callchain *callchain = sample->callchain;
610 if (has_branch_callstack(evsel))
611 callchain__lbr_callstack_printf(sample);
613 printf("... FP chain: nr:%" PRIu64 "\n", callchain->nr);
615 for (i = 0; i < callchain->nr; i++)
616 printf("..... %2d: %016" PRIx64 "\n",
617 i, callchain->ips[i]);
620 static void branch_stack__printf(struct perf_sample *sample)
624 printf("... branch stack: nr:%" PRIu64 "\n", sample->branch_stack->nr);
626 for (i = 0; i < sample->branch_stack->nr; i++)
627 printf("..... %2"PRIu64": %016" PRIx64 " -> %016" PRIx64 "\n",
628 i, sample->branch_stack->entries[i].from,
629 sample->branch_stack->entries[i].to);
632 static void regs_dump__printf(u64 mask, u64 *regs)
636 for_each_set_bit(rid, (unsigned long *) &mask, sizeof(mask) * 8) {
639 printf(".... %-5s 0x%" PRIx64 "\n",
640 perf_reg_name(rid), val);
644 static const char *regs_abi[] = {
645 [PERF_SAMPLE_REGS_ABI_NONE] = "none",
646 [PERF_SAMPLE_REGS_ABI_32] = "32-bit",
647 [PERF_SAMPLE_REGS_ABI_64] = "64-bit",
650 static inline const char *regs_dump_abi(struct regs_dump *d)
652 if (d->abi > PERF_SAMPLE_REGS_ABI_64)
655 return regs_abi[d->abi];
658 static void regs__printf(const char *type, struct regs_dump *regs)
660 u64 mask = regs->mask;
662 printf("... %s regs: mask 0x%" PRIx64 " ABI %s\n",
665 regs_dump_abi(regs));
667 regs_dump__printf(mask, regs->regs);
670 static void regs_user__printf(struct perf_sample *sample)
672 struct regs_dump *user_regs = &sample->user_regs;
675 regs__printf("user", user_regs);
678 static void regs_intr__printf(struct perf_sample *sample)
680 struct regs_dump *intr_regs = &sample->intr_regs;
683 regs__printf("intr", intr_regs);
686 static void stack_user__printf(struct stack_dump *dump)
688 printf("... ustack: size %" PRIu64 ", offset 0x%x\n",
689 dump->size, dump->offset);
692 static void perf_evlist__print_tstamp(struct perf_evlist *evlist,
693 union perf_event *event,
694 struct perf_sample *sample)
696 u64 sample_type = __perf_evlist__combined_sample_type(evlist);
698 if (event->header.type != PERF_RECORD_SAMPLE &&
699 !perf_evlist__sample_id_all(evlist)) {
700 fputs("-1 -1 ", stdout);
704 if ((sample_type & PERF_SAMPLE_CPU))
705 printf("%u ", sample->cpu);
707 if (sample_type & PERF_SAMPLE_TIME)
708 printf("%" PRIu64 " ", sample->time);
711 static void sample_read__printf(struct perf_sample *sample, u64 read_format)
713 printf("... sample_read:\n");
715 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
716 printf("...... time enabled %016" PRIx64 "\n",
717 sample->read.time_enabled);
719 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
720 printf("...... time running %016" PRIx64 "\n",
721 sample->read.time_running);
723 if (read_format & PERF_FORMAT_GROUP) {
726 printf(".... group nr %" PRIu64 "\n", sample->read.group.nr);
728 for (i = 0; i < sample->read.group.nr; i++) {
729 struct sample_read_value *value;
731 value = &sample->read.group.values[i];
732 printf("..... id %016" PRIx64
733 ", value %016" PRIx64 "\n",
734 value->id, value->value);
737 printf("..... id %016" PRIx64 ", value %016" PRIx64 "\n",
738 sample->read.one.id, sample->read.one.value);
741 static void dump_event(struct perf_evlist *evlist, union perf_event *event,
742 u64 file_offset, struct perf_sample *sample)
747 printf("\n%#" PRIx64 " [%#x]: event: %d\n",
748 file_offset, event->header.size, event->header.type);
753 perf_evlist__print_tstamp(evlist, event, sample);
755 printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset,
756 event->header.size, perf_event__name(event->header.type));
759 static void dump_sample(struct perf_evsel *evsel, union perf_event *event,
760 struct perf_sample *sample)
767 printf("(IP, 0x%x): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n",
768 event->header.misc, sample->pid, sample->tid, sample->ip,
769 sample->period, sample->addr);
771 sample_type = evsel->attr.sample_type;
773 if (sample_type & PERF_SAMPLE_CALLCHAIN)
774 callchain__printf(evsel, sample);
776 if ((sample_type & PERF_SAMPLE_BRANCH_STACK) && !has_branch_callstack(evsel))
777 branch_stack__printf(sample);
779 if (sample_type & PERF_SAMPLE_REGS_USER)
780 regs_user__printf(sample);
782 if (sample_type & PERF_SAMPLE_REGS_INTR)
783 regs_intr__printf(sample);
785 if (sample_type & PERF_SAMPLE_STACK_USER)
786 stack_user__printf(&sample->user_stack);
788 if (sample_type & PERF_SAMPLE_WEIGHT)
789 printf("... weight: %" PRIu64 "\n", sample->weight);
791 if (sample_type & PERF_SAMPLE_DATA_SRC)
792 printf(" . data_src: 0x%"PRIx64"\n", sample->data_src);
794 if (sample_type & PERF_SAMPLE_TRANSACTION)
795 printf("... transaction: %" PRIx64 "\n", sample->transaction);
797 if (sample_type & PERF_SAMPLE_READ)
798 sample_read__printf(sample, evsel->attr.read_format);
801 static struct machine *machines__find_for_cpumode(struct machines *machines,
802 union perf_event *event,
803 struct perf_sample *sample)
805 const u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
806 struct machine *machine;
809 ((cpumode == PERF_RECORD_MISC_GUEST_KERNEL) ||
810 (cpumode == PERF_RECORD_MISC_GUEST_USER))) {
813 if (event->header.type == PERF_RECORD_MMAP
814 || event->header.type == PERF_RECORD_MMAP2)
815 pid = event->mmap.pid;
819 machine = machines__find(machines, pid);
821 machine = machines__find(machines, DEFAULT_GUEST_KERNEL_ID);
825 return &machines->host;
828 static int deliver_sample_value(struct perf_evlist *evlist,
829 struct perf_tool *tool,
830 union perf_event *event,
831 struct perf_sample *sample,
832 struct sample_read_value *v,
833 struct machine *machine)
835 struct perf_sample_id *sid = perf_evlist__id2sid(evlist, v->id);
839 sample->period = v->value - sid->period;
840 sid->period = v->value;
843 if (!sid || sid->evsel == NULL) {
844 ++evlist->stats.nr_unknown_id;
848 return tool->sample(tool, event, sample, sid->evsel, machine);
851 static int deliver_sample_group(struct perf_evlist *evlist,
852 struct perf_tool *tool,
853 union perf_event *event,
854 struct perf_sample *sample,
855 struct machine *machine)
860 for (i = 0; i < sample->read.group.nr; i++) {
861 ret = deliver_sample_value(evlist, tool, event, sample,
862 &sample->read.group.values[i],
872 perf_evlist__deliver_sample(struct perf_evlist *evlist,
873 struct perf_tool *tool,
874 union perf_event *event,
875 struct perf_sample *sample,
876 struct perf_evsel *evsel,
877 struct machine *machine)
879 /* We know evsel != NULL. */
880 u64 sample_type = evsel->attr.sample_type;
881 u64 read_format = evsel->attr.read_format;
883 /* Standard sample delievery. */
884 if (!(sample_type & PERF_SAMPLE_READ))
885 return tool->sample(tool, event, sample, evsel, machine);
887 /* For PERF_SAMPLE_READ we have either single or group mode. */
888 if (read_format & PERF_FORMAT_GROUP)
889 return deliver_sample_group(evlist, tool, event, sample,
892 return deliver_sample_value(evlist, tool, event, sample,
893 &sample->read.one, machine);
896 int machines__deliver_event(struct machines *machines,
897 struct perf_evlist *evlist,
898 union perf_event *event,
899 struct perf_sample *sample,
900 struct perf_tool *tool, u64 file_offset)
902 struct perf_evsel *evsel;
903 struct machine *machine;
905 dump_event(evlist, event, file_offset, sample);
907 evsel = perf_evlist__id2evsel(evlist, sample->id);
909 machine = machines__find_for_cpumode(machines, event, sample);
911 switch (event->header.type) {
912 case PERF_RECORD_SAMPLE:
913 dump_sample(evsel, event, sample);
915 ++evlist->stats.nr_unknown_id;
918 if (machine == NULL) {
919 ++evlist->stats.nr_unprocessable_samples;
922 return perf_evlist__deliver_sample(evlist, tool, event, sample, evsel, machine);
923 case PERF_RECORD_MMAP:
924 return tool->mmap(tool, event, sample, machine);
925 case PERF_RECORD_MMAP2:
926 return tool->mmap2(tool, event, sample, machine);
927 case PERF_RECORD_COMM:
928 return tool->comm(tool, event, sample, machine);
929 case PERF_RECORD_FORK:
930 return tool->fork(tool, event, sample, machine);
931 case PERF_RECORD_EXIT:
932 return tool->exit(tool, event, sample, machine);
933 case PERF_RECORD_LOST:
934 if (tool->lost == perf_event__process_lost)
935 evlist->stats.total_lost += event->lost.lost;
936 return tool->lost(tool, event, sample, machine);
937 case PERF_RECORD_READ:
938 return tool->read(tool, event, sample, evsel, machine);
939 case PERF_RECORD_THROTTLE:
940 return tool->throttle(tool, event, sample, machine);
941 case PERF_RECORD_UNTHROTTLE:
942 return tool->unthrottle(tool, event, sample, machine);
944 ++evlist->stats.nr_unknown_events;
949 static s64 perf_session__process_user_event(struct perf_session *session,
950 union perf_event *event,
951 struct perf_tool *tool,
954 int fd = perf_data_file__fd(session->file);
957 dump_event(session->evlist, event, file_offset, NULL);
959 /* These events are processed right away */
960 switch (event->header.type) {
961 case PERF_RECORD_HEADER_ATTR:
962 err = tool->attr(tool, event, &session->evlist);
964 perf_session__set_id_hdr_size(session);
965 perf_session__set_comm_exec(session);
968 case PERF_RECORD_HEADER_EVENT_TYPE:
970 * Depreceated, but we need to handle it for sake
971 * of old data files create in pipe mode.
974 case PERF_RECORD_HEADER_TRACING_DATA:
975 /* setup for reading amidst mmap */
976 lseek(fd, file_offset, SEEK_SET);
977 return tool->tracing_data(tool, event, session);
978 case PERF_RECORD_HEADER_BUILD_ID:
979 return tool->build_id(tool, event, session);
980 case PERF_RECORD_FINISHED_ROUND:
981 return tool->finished_round(tool, event, session);
982 case PERF_RECORD_ID_INDEX:
983 return tool->id_index(tool, event, session);
989 int perf_session__deliver_synth_event(struct perf_session *session,
990 union perf_event *event,
991 struct perf_sample *sample,
992 struct perf_tool *tool)
994 struct perf_evlist *evlist = session->evlist;
996 events_stats__inc(&evlist->stats, event->header.type);
998 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
999 return perf_session__process_user_event(session, event, tool, 0);
1001 return machines__deliver_event(&session->machines, evlist, event, sample, tool, 0);
1004 static void event_swap(union perf_event *event, bool sample_id_all)
1006 perf_event__swap_op swap;
1008 swap = perf_event__swap_ops[event->header.type];
1010 swap(event, sample_id_all);
1013 int perf_session__peek_event(struct perf_session *session, off_t file_offset,
1014 void *buf, size_t buf_sz,
1015 union perf_event **event_ptr,
1016 struct perf_sample *sample)
1018 union perf_event *event;
1019 size_t hdr_sz, rest;
1022 if (session->one_mmap && !session->header.needs_swap) {
1023 event = file_offset - session->one_mmap_offset +
1024 session->one_mmap_addr;
1025 goto out_parse_sample;
1028 if (perf_data_file__is_pipe(session->file))
1031 fd = perf_data_file__fd(session->file);
1032 hdr_sz = sizeof(struct perf_event_header);
1034 if (buf_sz < hdr_sz)
1037 if (lseek(fd, file_offset, SEEK_SET) == (off_t)-1 ||
1038 readn(fd, &buf, hdr_sz) != (ssize_t)hdr_sz)
1041 event = (union perf_event *)buf;
1043 if (session->header.needs_swap)
1044 perf_event_header__bswap(&event->header);
1046 if (event->header.size < hdr_sz)
1049 rest = event->header.size - hdr_sz;
1051 if (readn(fd, &buf, rest) != (ssize_t)rest)
1054 if (session->header.needs_swap)
1055 event_swap(event, perf_evlist__sample_id_all(session->evlist));
1059 if (sample && event->header.type < PERF_RECORD_USER_TYPE_START &&
1060 perf_evlist__parse_sample(session->evlist, event, sample))
1068 static s64 perf_session__process_event(struct perf_session *session,
1069 union perf_event *event,
1070 struct perf_tool *tool,
1073 struct perf_evlist *evlist = session->evlist;
1074 struct perf_sample sample;
1077 if (session->header.needs_swap)
1078 event_swap(event, perf_evlist__sample_id_all(evlist));
1080 if (event->header.type >= PERF_RECORD_HEADER_MAX)
1083 events_stats__inc(&evlist->stats, event->header.type);
1085 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
1086 return perf_session__process_user_event(session, event, tool, file_offset);
1089 * For all kernel events we get the sample data
1091 ret = perf_evlist__parse_sample(evlist, event, &sample);
1095 if (tool->ordered_events) {
1096 ret = perf_session_queue_event(session, event, tool, &sample,
1102 return machines__deliver_event(&session->machines, evlist, event,
1103 &sample, tool, file_offset);
1106 void perf_event_header__bswap(struct perf_event_header *hdr)
1108 hdr->type = bswap_32(hdr->type);
1109 hdr->misc = bswap_16(hdr->misc);
1110 hdr->size = bswap_16(hdr->size);
1113 struct thread *perf_session__findnew(struct perf_session *session, pid_t pid)
1115 return machine__findnew_thread(&session->machines.host, -1, pid);
1118 static struct thread *perf_session__register_idle_thread(struct perf_session *session)
1120 struct thread *thread;
1122 thread = machine__findnew_thread(&session->machines.host, 0, 0);
1123 if (thread == NULL || thread__set_comm(thread, "swapper", 0)) {
1124 pr_err("problem inserting idle task.\n");
1131 static void perf_tool__warn_about_errors(const struct perf_tool *tool,
1132 const struct events_stats *stats)
1134 if (tool->lost == perf_event__process_lost &&
1135 stats->nr_events[PERF_RECORD_LOST] != 0) {
1136 ui__warning("Processed %d events and lost %d chunks!\n\n"
1137 "Check IO/CPU overload!\n\n",
1138 stats->nr_events[0],
1139 stats->nr_events[PERF_RECORD_LOST]);
1142 if (stats->nr_unknown_events != 0) {
1143 ui__warning("Found %u unknown events!\n\n"
1144 "Is this an older tool processing a perf.data "
1145 "file generated by a more recent tool?\n\n"
1146 "If that is not the case, consider "
1147 "reporting to linux-kernel@vger.kernel.org.\n\n",
1148 stats->nr_unknown_events);
1151 if (stats->nr_unknown_id != 0) {
1152 ui__warning("%u samples with id not present in the header\n",
1153 stats->nr_unknown_id);
1156 if (stats->nr_invalid_chains != 0) {
1157 ui__warning("Found invalid callchains!\n\n"
1158 "%u out of %u events were discarded for this reason.\n\n"
1159 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
1160 stats->nr_invalid_chains,
1161 stats->nr_events[PERF_RECORD_SAMPLE]);
1164 if (stats->nr_unprocessable_samples != 0) {
1165 ui__warning("%u unprocessable samples recorded.\n"
1166 "Do you have a KVM guest running and not using 'perf kvm'?\n",
1167 stats->nr_unprocessable_samples);
1170 if (stats->nr_unordered_events != 0)
1171 ui__warning("%u out of order events recorded.\n", stats->nr_unordered_events);
1174 volatile int session_done;
1176 static int __perf_session__process_pipe_events(struct perf_session *session,
1177 struct perf_tool *tool)
1179 struct ordered_events *oe = &session->ordered_events;
1180 struct perf_evlist *evlist = session->evlist;
1181 struct machines *machines = &session->machines;
1182 int fd = perf_data_file__fd(session->file);
1183 union perf_event *event;
1184 uint32_t size, cur_size = 0;
1191 perf_tool__fill_defaults(tool);
1194 cur_size = sizeof(union perf_event);
1196 buf = malloc(cur_size);
1201 err = readn(fd, event, sizeof(struct perf_event_header));
1206 pr_err("failed to read event header\n");
1210 if (session->header.needs_swap)
1211 perf_event_header__bswap(&event->header);
1213 size = event->header.size;
1214 if (size < sizeof(struct perf_event_header)) {
1215 pr_err("bad event header size\n");
1219 if (size > cur_size) {
1220 void *new = realloc(buf, size);
1222 pr_err("failed to allocate memory to read event\n");
1230 p += sizeof(struct perf_event_header);
1232 if (size - sizeof(struct perf_event_header)) {
1233 err = readn(fd, p, size - sizeof(struct perf_event_header));
1236 pr_err("unexpected end of event stream\n");
1240 pr_err("failed to read event data\n");
1245 if ((skip = perf_session__process_event(session, event, tool, head)) < 0) {
1246 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
1247 head, event->header.size, event->header.type);
1257 if (!session_done())
1260 /* do the final flush for ordered samples */
1261 err = ordered_events__flush(oe, machines, evlist, tool, OE_FLUSH__FINAL);
1264 perf_tool__warn_about_errors(tool, &session->evlist->stats);
1265 ordered_events__free(&session->ordered_events);
1269 static union perf_event *
1270 fetch_mmaped_event(struct perf_session *session,
1271 u64 head, size_t mmap_size, char *buf)
1273 union perf_event *event;
1276 * Ensure we have enough space remaining to read
1277 * the size of the event in the headers.
1279 if (head + sizeof(event->header) > mmap_size)
1282 event = (union perf_event *)(buf + head);
1284 if (session->header.needs_swap)
1285 perf_event_header__bswap(&event->header);
1287 if (head + event->header.size > mmap_size) {
1288 /* We're not fetching the event so swap back again */
1289 if (session->header.needs_swap)
1290 perf_event_header__bswap(&event->header);
1298 * On 64bit we can mmap the data file in one go. No need for tiny mmap
1299 * slices. On 32bit we use 32MB.
1301 #if BITS_PER_LONG == 64
1302 #define MMAP_SIZE ULLONG_MAX
1305 #define MMAP_SIZE (32 * 1024 * 1024ULL)
1306 #define NUM_MMAPS 128
1309 static int __perf_session__process_events(struct perf_session *session,
1310 u64 data_offset, u64 data_size,
1311 u64 file_size, struct perf_tool *tool)
1313 struct ordered_events *oe = &session->ordered_events;
1314 struct perf_evlist *evlist = session->evlist;
1315 struct machines *machines = &session->machines;
1316 int fd = perf_data_file__fd(session->file);
1317 u64 head, page_offset, file_offset, file_pos, size;
1318 int err, mmap_prot, mmap_flags, map_idx = 0;
1320 char *buf, *mmaps[NUM_MMAPS];
1321 union perf_event *event;
1322 struct ui_progress prog;
1325 perf_tool__fill_defaults(tool);
1327 page_offset = page_size * (data_offset / page_size);
1328 file_offset = page_offset;
1329 head = data_offset - page_offset;
1331 if (data_size && (data_offset + data_size < file_size))
1332 file_size = data_offset + data_size;
1334 ui_progress__init(&prog, file_size, "Processing events...");
1336 mmap_size = MMAP_SIZE;
1337 if (mmap_size > file_size) {
1338 mmap_size = file_size;
1339 session->one_mmap = true;
1342 memset(mmaps, 0, sizeof(mmaps));
1344 mmap_prot = PROT_READ;
1345 mmap_flags = MAP_SHARED;
1347 if (session->header.needs_swap) {
1348 mmap_prot |= PROT_WRITE;
1349 mmap_flags = MAP_PRIVATE;
1352 buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, fd,
1354 if (buf == MAP_FAILED) {
1355 pr_err("failed to mmap file\n");
1359 mmaps[map_idx] = buf;
1360 map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
1361 file_pos = file_offset + head;
1362 if (session->one_mmap) {
1363 session->one_mmap_addr = buf;
1364 session->one_mmap_offset = file_offset;
1368 event = fetch_mmaped_event(session, head, mmap_size, buf);
1370 if (mmaps[map_idx]) {
1371 munmap(mmaps[map_idx], mmap_size);
1372 mmaps[map_idx] = NULL;
1375 page_offset = page_size * (head / page_size);
1376 file_offset += page_offset;
1377 head -= page_offset;
1381 size = event->header.size;
1383 if (size < sizeof(struct perf_event_header) ||
1384 (skip = perf_session__process_event(session, event, tool, file_pos))
1386 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
1387 file_offset + head, event->header.size,
1388 event->header.type);
1399 ui_progress__update(&prog, size);
1404 if (file_pos < file_size)
1408 /* do the final flush for ordered samples */
1409 err = ordered_events__flush(oe, machines, evlist, tool, OE_FLUSH__FINAL);
1411 ui_progress__finish();
1412 perf_tool__warn_about_errors(tool, &session->evlist->stats);
1413 ordered_events__free(&session->ordered_events);
1414 session->one_mmap = false;
1418 int perf_session__process_events(struct perf_session *session,
1419 struct perf_tool *tool)
1421 u64 size = perf_data_file__size(session->file);
1424 if (perf_session__register_idle_thread(session) == NULL)
1427 if (!perf_data_file__is_pipe(session->file))
1428 err = __perf_session__process_events(session,
1429 session->header.data_offset,
1430 session->header.data_size,
1433 err = __perf_session__process_pipe_events(session, tool);
1438 bool perf_session__has_traces(struct perf_session *session, const char *msg)
1440 struct perf_evsel *evsel;
1442 evlist__for_each(session->evlist, evsel) {
1443 if (evsel->attr.type == PERF_TYPE_TRACEPOINT)
1447 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
1451 int maps__set_kallsyms_ref_reloc_sym(struct map **maps,
1452 const char *symbol_name, u64 addr)
1456 struct ref_reloc_sym *ref;
1458 ref = zalloc(sizeof(struct ref_reloc_sym));
1462 ref->name = strdup(symbol_name);
1463 if (ref->name == NULL) {
1468 bracket = strchr(ref->name, ']');
1474 for (i = 0; i < MAP__NR_TYPES; ++i) {
1475 struct kmap *kmap = map__kmap(maps[i]);
1476 kmap->ref_reloc_sym = ref;
1482 size_t perf_session__fprintf_dsos(struct perf_session *session, FILE *fp)
1484 return machines__fprintf_dsos(&session->machines, fp);
1487 size_t perf_session__fprintf_dsos_buildid(struct perf_session *session, FILE *fp,
1488 bool (skip)(struct dso *dso, int parm), int parm)
1490 return machines__fprintf_dsos_buildid(&session->machines, fp, skip, parm);
1493 size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp)
1495 size_t ret = fprintf(fp, "Aggregated stats:\n");
1497 ret += events_stats__fprintf(&session->evlist->stats, fp);
1501 size_t perf_session__fprintf(struct perf_session *session, FILE *fp)
1504 * FIXME: Here we have to actually print all the machines in this
1505 * session, not just the host...
1507 return machine__fprintf(&session->machines.host, fp);
1510 struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
1513 struct perf_evsel *pos;
1515 evlist__for_each(session->evlist, pos) {
1516 if (pos->attr.type == type)
1522 void perf_evsel__print_ip(struct perf_evsel *evsel, struct perf_sample *sample,
1523 struct addr_location *al,
1524 unsigned int print_opts, unsigned int stack_depth)
1526 struct callchain_cursor_node *node;
1527 int print_ip = print_opts & PRINT_IP_OPT_IP;
1528 int print_sym = print_opts & PRINT_IP_OPT_SYM;
1529 int print_dso = print_opts & PRINT_IP_OPT_DSO;
1530 int print_symoffset = print_opts & PRINT_IP_OPT_SYMOFFSET;
1531 int print_oneline = print_opts & PRINT_IP_OPT_ONELINE;
1532 int print_srcline = print_opts & PRINT_IP_OPT_SRCLINE;
1533 char s = print_oneline ? ' ' : '\t';
1535 if (symbol_conf.use_callchain && sample->callchain) {
1536 struct addr_location node_al;
1538 if (thread__resolve_callchain(al->thread, evsel,
1540 PERF_MAX_STACK_DEPTH) != 0) {
1542 error("Failed to resolve callchain. Skipping\n");
1545 callchain_cursor_commit(&callchain_cursor);
1547 if (print_symoffset)
1550 while (stack_depth) {
1553 node = callchain_cursor_current(&callchain_cursor);
1557 if (node->sym && node->sym->ignore)
1561 printf("%c%16" PRIx64, s, node->ip);
1564 addr = node->map->map_ip(node->map, node->ip);
1568 if (print_symoffset) {
1569 node_al.addr = addr;
1570 node_al.map = node->map;
1571 symbol__fprintf_symname_offs(node->sym, &node_al, stdout);
1573 symbol__fprintf_symname(node->sym, stdout);
1578 map__fprintf_dsoname(node->map, stdout);
1583 map__fprintf_srcline(node->map, addr, "\n ",
1591 callchain_cursor_advance(&callchain_cursor);
1595 if (al->sym && al->sym->ignore)
1599 printf("%16" PRIx64, sample->ip);
1603 if (print_symoffset)
1604 symbol__fprintf_symname_offs(al->sym, al,
1607 symbol__fprintf_symname(al->sym, stdout);
1612 map__fprintf_dsoname(al->map, stdout);
1617 map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
1621 int perf_session__cpu_bitmap(struct perf_session *session,
1622 const char *cpu_list, unsigned long *cpu_bitmap)
1625 struct cpu_map *map;
1627 for (i = 0; i < PERF_TYPE_MAX; ++i) {
1628 struct perf_evsel *evsel;
1630 evsel = perf_session__find_first_evtype(session, i);
1634 if (!(evsel->attr.sample_type & PERF_SAMPLE_CPU)) {
1635 pr_err("File does not contain CPU events. "
1636 "Remove -c option to proceed.\n");
1641 map = cpu_map__new(cpu_list);
1643 pr_err("Invalid cpu_list\n");
1647 for (i = 0; i < map->nr; i++) {
1648 int cpu = map->map[i];
1650 if (cpu >= MAX_NR_CPUS) {
1651 pr_err("Requested CPU %d too large. "
1652 "Consider raising MAX_NR_CPUS\n", cpu);
1653 goto out_delete_map;
1656 set_bit(cpu, cpu_bitmap);
1662 cpu_map__delete(map);
1666 void perf_session__fprintf_info(struct perf_session *session, FILE *fp,
1672 if (session == NULL || fp == NULL)
1675 fd = perf_data_file__fd(session->file);
1677 ret = fstat(fd, &st);
1681 fprintf(fp, "# ========\n");
1682 fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
1683 perf_header__fprintf_info(session, fp, full);
1684 fprintf(fp, "# ========\n#\n");
1688 int __perf_session__set_tracepoints_handlers(struct perf_session *session,
1689 const struct perf_evsel_str_handler *assocs,
1692 struct perf_evsel *evsel;
1696 for (i = 0; i < nr_assocs; i++) {
1698 * Adding a handler for an event not in the session,
1701 evsel = perf_evlist__find_tracepoint_by_name(session->evlist, assocs[i].name);
1706 if (evsel->handler != NULL)
1708 evsel->handler = assocs[i].handler;
1716 int perf_event__process_id_index(struct perf_tool *tool __maybe_unused,
1717 union perf_event *event,
1718 struct perf_session *session)
1720 struct perf_evlist *evlist = session->evlist;
1721 struct id_index_event *ie = &event->id_index;
1722 size_t i, nr, max_nr;
1724 max_nr = (ie->header.size - sizeof(struct id_index_event)) /
1725 sizeof(struct id_index_entry);
1731 fprintf(stdout, " nr: %zu\n", nr);
1733 for (i = 0; i < nr; i++) {
1734 struct id_index_entry *e = &ie->entries[i];
1735 struct perf_sample_id *sid;
1738 fprintf(stdout, " ... id: %"PRIu64, e->id);
1739 fprintf(stdout, " idx: %"PRIu64, e->idx);
1740 fprintf(stdout, " cpu: %"PRId64, e->cpu);
1741 fprintf(stdout, " tid: %"PRId64"\n", e->tid);
1744 sid = perf_evlist__id2sid(evlist, e->id);
1754 int perf_event__synthesize_id_index(struct perf_tool *tool,
1755 perf_event__handler_t process,
1756 struct perf_evlist *evlist,
1757 struct machine *machine)
1759 union perf_event *ev;
1760 struct perf_evsel *evsel;
1761 size_t nr = 0, i = 0, sz, max_nr, n;
1764 pr_debug2("Synthesizing id index\n");
1766 max_nr = (UINT16_MAX - sizeof(struct id_index_event)) /
1767 sizeof(struct id_index_entry);
1769 evlist__for_each(evlist, evsel)
1772 n = nr > max_nr ? max_nr : nr;
1773 sz = sizeof(struct id_index_event) + n * sizeof(struct id_index_entry);
1778 ev->id_index.header.type = PERF_RECORD_ID_INDEX;
1779 ev->id_index.header.size = sz;
1780 ev->id_index.nr = n;
1782 evlist__for_each(evlist, evsel) {
1785 for (j = 0; j < evsel->ids; j++) {
1786 struct id_index_entry *e;
1787 struct perf_sample_id *sid;
1790 err = process(tool, ev, NULL, machine);
1797 e = &ev->id_index.entries[i++];
1799 e->id = evsel->id[j];
1801 sid = perf_evlist__id2sid(evlist, e->id);
1813 sz = sizeof(struct id_index_event) + nr * sizeof(struct id_index_entry);
1814 ev->id_index.header.size = sz;
1815 ev->id_index.nr = nr;
1817 err = process(tool, ev, NULL, machine);