1 #define _FILE_OFFSET_BITS 64
3 #include <linux/kernel.h>
17 #include "event-parse.h"
19 int perf_session__parse_sample(struct perf_session
*session
,
20 const union perf_event
*event
,
21 struct perf_sample
*sample
)
23 struct perf_evsel
*first
;
24 first
= list_entry(session
->evlist
->entries
.next
, struct perf_evsel
, node
);
26 return perf_event__parse_sample(event
, session
->sample_type
,
28 session
->sample_id_all
, sample
,
29 session
->header
.needs_swap
);
32 static int perf_session__open(struct perf_session
*self
, bool force
)
34 struct stat input_stat
;
36 if (!strcmp(self
->filename
, "-")) {
38 self
->fd
= STDIN_FILENO
;
40 if (perf_session__read_header(self
, self
->fd
) < 0)
41 pr_err("incompatible file format (rerun with -v to learn more)");
46 self
->fd
= open(self
->filename
, O_RDONLY
);
50 pr_err("failed to open %s: %s", self
->filename
, strerror(err
));
51 if (err
== ENOENT
&& !strcmp(self
->filename
, "perf.data"))
52 pr_err(" (try 'perf record' first)");
57 if (fstat(self
->fd
, &input_stat
) < 0)
60 if (!force
&& input_stat
.st_uid
&& (input_stat
.st_uid
!= geteuid())) {
61 pr_err("file %s not owned by current user or root\n",
66 if (!input_stat
.st_size
) {
67 pr_info("zero-sized file (%s), nothing to do!\n",
72 if (perf_session__read_header(self
, self
->fd
) < 0) {
73 pr_err("incompatible file format (rerun with -v to learn more)");
77 if (!perf_evlist__valid_sample_type(self
->evlist
)) {
78 pr_err("non matching sample_type");
82 if (!perf_evlist__valid_sample_id_all(self
->evlist
)) {
83 pr_err("non matching sample_id_all");
87 self
->size
= input_stat
.st_size
;
96 void perf_session__update_sample_type(struct perf_session
*self
)
98 self
->sample_type
= perf_evlist__sample_type(self
->evlist
);
99 self
->sample_id_all
= perf_evlist__sample_id_all(self
->evlist
);
100 self
->id_hdr_size
= perf_evlist__id_hdr_size(self
->evlist
);
101 self
->host_machine
.id_hdr_size
= self
->id_hdr_size
;
102 machines__set_id_hdr_size(&self
->machines
, self
->id_hdr_size
);
105 int perf_session__create_kernel_maps(struct perf_session
*self
)
107 int ret
= machine__create_kernel_maps(&self
->host_machine
);
110 ret
= machines__create_guest_kernel_maps(&self
->machines
);
114 static void perf_session__destroy_kernel_maps(struct perf_session
*self
)
116 machine__destroy_kernel_maps(&self
->host_machine
);
117 machines__destroy_guest_kernel_maps(&self
->machines
);
120 struct perf_session
*perf_session__new(const char *filename
, int mode
,
121 bool force
, bool repipe
,
122 struct perf_tool
*tool
)
124 struct perf_session
*self
;
128 if (!filename
|| !strlen(filename
)) {
129 if (!fstat(STDIN_FILENO
, &st
) && S_ISFIFO(st
.st_mode
))
132 filename
= "perf.data";
135 len
= strlen(filename
);
136 self
= zalloc(sizeof(*self
) + len
);
141 memcpy(self
->filename
, filename
, len
);
143 * On 64bit we can mmap the data file in one go. No need for tiny mmap
144 * slices. On 32bit we use 32MB.
146 #if BITS_PER_LONG == 64
147 self
->mmap_window
= ULLONG_MAX
;
149 self
->mmap_window
= 32 * 1024 * 1024ULL;
151 self
->machines
= RB_ROOT
;
152 self
->repipe
= repipe
;
153 INIT_LIST_HEAD(&self
->ordered_samples
.samples
);
154 INIT_LIST_HEAD(&self
->ordered_samples
.sample_cache
);
155 INIT_LIST_HEAD(&self
->ordered_samples
.to_free
);
156 machine__init(&self
->host_machine
, "", HOST_KERNEL_ID
);
157 hists__init(&self
->hists
);
159 if (mode
== O_RDONLY
) {
160 if (perf_session__open(self
, force
) < 0)
162 perf_session__update_sample_type(self
);
163 } else if (mode
== O_WRONLY
) {
165 * In O_RDONLY mode this will be performed when reading the
166 * kernel MMAP event, in perf_event__process_mmap().
168 if (perf_session__create_kernel_maps(self
) < 0)
172 if (tool
&& tool
->ordering_requires_timestamps
&&
173 tool
->ordered_samples
&& !self
->sample_id_all
) {
174 dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n");
175 tool
->ordered_samples
= false;
181 perf_session__delete(self
);
185 static void machine__delete_dead_threads(struct machine
*machine
)
187 struct thread
*n
, *t
;
189 list_for_each_entry_safe(t
, n
, &machine
->dead_threads
, node
) {
195 static void perf_session__delete_dead_threads(struct perf_session
*session
)
197 machine__delete_dead_threads(&session
->host_machine
);
200 static void machine__delete_threads(struct machine
*self
)
202 struct rb_node
*nd
= rb_first(&self
->threads
);
205 struct thread
*t
= rb_entry(nd
, struct thread
, rb_node
);
207 rb_erase(&t
->rb_node
, &self
->threads
);
213 static void perf_session__delete_threads(struct perf_session
*session
)
215 machine__delete_threads(&session
->host_machine
);
218 void perf_session__delete(struct perf_session
*self
)
220 perf_session__destroy_kernel_maps(self
);
221 perf_session__delete_dead_threads(self
);
222 perf_session__delete_threads(self
);
223 machine__exit(&self
->host_machine
);
228 void machine__remove_thread(struct machine
*self
, struct thread
*th
)
230 self
->last_match
= NULL
;
231 rb_erase(&th
->rb_node
, &self
->threads
);
233 * We may have references to this thread, for instance in some hist_entry
234 * instances, so just move them to a separate list.
236 list_add_tail(&th
->node
, &self
->dead_threads
);
239 static bool symbol__match_parent_regex(struct symbol
*sym
)
241 if (sym
->name
&& !regexec(&parent_regex
, sym
->name
, 0, NULL
, 0))
247 static const u8 cpumodes
[] = {
248 PERF_RECORD_MISC_USER
,
249 PERF_RECORD_MISC_KERNEL
,
250 PERF_RECORD_MISC_GUEST_USER
,
251 PERF_RECORD_MISC_GUEST_KERNEL
253 #define NCPUMODES (sizeof(cpumodes)/sizeof(u8))
255 static void ip__resolve_ams(struct machine
*self
, struct thread
*thread
,
256 struct addr_map_symbol
*ams
,
259 struct addr_location al
;
263 memset(&al
, 0, sizeof(al
));
265 for (i
= 0; i
< NCPUMODES
; i
++) {
268 * We cannot use the header.misc hint to determine whether a
269 * branch stack address is user, kernel, guest, hypervisor.
270 * Branches may straddle the kernel/user/hypervisor boundaries.
271 * Thus, we have to try consecutively until we find a match
272 * or else, the symbol is unknown
274 thread__find_addr_location(thread
, self
, m
, MAP__FUNCTION
,
281 ams
->al_addr
= al
.addr
;
286 struct branch_info
*machine__resolve_bstack(struct machine
*self
,
288 struct branch_stack
*bs
)
290 struct branch_info
*bi
;
293 bi
= calloc(bs
->nr
, sizeof(struct branch_info
));
297 for (i
= 0; i
< bs
->nr
; i
++) {
298 ip__resolve_ams(self
, thr
, &bi
[i
].to
, bs
->entries
[i
].to
);
299 ip__resolve_ams(self
, thr
, &bi
[i
].from
, bs
->entries
[i
].from
);
300 bi
[i
].flags
= bs
->entries
[i
].flags
;
305 int machine__resolve_callchain(struct machine
*self
,
306 struct thread
*thread
,
307 struct ip_callchain
*chain
,
308 struct symbol
**parent
)
310 u8 cpumode
= PERF_RECORD_MISC_USER
;
314 callchain_cursor_reset(&callchain_cursor
);
316 if (chain
->nr
> PERF_MAX_STACK_DEPTH
) {
317 pr_warning("corrupted callchain. skipping...\n");
321 for (i
= 0; i
< chain
->nr
; i
++) {
323 struct addr_location al
;
325 if (callchain_param
.order
== ORDER_CALLEE
)
328 ip
= chain
->ips
[chain
->nr
- i
- 1];
330 if (ip
>= PERF_CONTEXT_MAX
) {
332 case PERF_CONTEXT_HV
:
333 cpumode
= PERF_RECORD_MISC_HYPERVISOR
; break;
334 case PERF_CONTEXT_KERNEL
:
335 cpumode
= PERF_RECORD_MISC_KERNEL
; break;
336 case PERF_CONTEXT_USER
:
337 cpumode
= PERF_RECORD_MISC_USER
; break;
339 pr_debug("invalid callchain context: "
340 "%"PRId64
"\n", (s64
) ip
);
342 * It seems the callchain is corrupted.
345 callchain_cursor_reset(&callchain_cursor
);
352 thread__find_addr_location(thread
, self
, cpumode
,
353 MAP__FUNCTION
, ip
, &al
, NULL
);
354 if (al
.sym
!= NULL
) {
355 if (sort__has_parent
&& !*parent
&&
356 symbol__match_parent_regex(al
.sym
))
358 if (!symbol_conf
.use_callchain
)
362 err
= callchain_cursor_append(&callchain_cursor
,
371 static int process_event_synth_tracing_data_stub(union perf_event
*event __used
,
372 struct perf_session
*session __used
)
374 dump_printf(": unhandled!\n");
378 static int process_event_synth_attr_stub(union perf_event
*event __used
,
379 struct perf_evlist
**pevlist __used
)
381 dump_printf(": unhandled!\n");
385 static int process_event_sample_stub(struct perf_tool
*tool __used
,
386 union perf_event
*event __used
,
387 struct perf_sample
*sample __used
,
388 struct perf_evsel
*evsel __used
,
389 struct machine
*machine __used
)
391 dump_printf(": unhandled!\n");
395 static int process_event_stub(struct perf_tool
*tool __used
,
396 union perf_event
*event __used
,
397 struct perf_sample
*sample __used
,
398 struct machine
*machine __used
)
400 dump_printf(": unhandled!\n");
404 static int process_finished_round_stub(struct perf_tool
*tool __used
,
405 union perf_event
*event __used
,
406 struct perf_session
*perf_session __used
)
408 dump_printf(": unhandled!\n");
412 static int process_event_type_stub(struct perf_tool
*tool __used
,
413 union perf_event
*event __used
)
415 dump_printf(": unhandled!\n");
419 static int process_finished_round(struct perf_tool
*tool
,
420 union perf_event
*event
,
421 struct perf_session
*session
);
423 static void perf_tool__fill_defaults(struct perf_tool
*tool
)
425 if (tool
->sample
== NULL
)
426 tool
->sample
= process_event_sample_stub
;
427 if (tool
->mmap
== NULL
)
428 tool
->mmap
= process_event_stub
;
429 if (tool
->comm
== NULL
)
430 tool
->comm
= process_event_stub
;
431 if (tool
->fork
== NULL
)
432 tool
->fork
= process_event_stub
;
433 if (tool
->exit
== NULL
)
434 tool
->exit
= process_event_stub
;
435 if (tool
->lost
== NULL
)
436 tool
->lost
= perf_event__process_lost
;
437 if (tool
->read
== NULL
)
438 tool
->read
= process_event_sample_stub
;
439 if (tool
->throttle
== NULL
)
440 tool
->throttle
= process_event_stub
;
441 if (tool
->unthrottle
== NULL
)
442 tool
->unthrottle
= process_event_stub
;
443 if (tool
->attr
== NULL
)
444 tool
->attr
= process_event_synth_attr_stub
;
445 if (tool
->event_type
== NULL
)
446 tool
->event_type
= process_event_type_stub
;
447 if (tool
->tracing_data
== NULL
)
448 tool
->tracing_data
= process_event_synth_tracing_data_stub
;
449 if (tool
->build_id
== NULL
)
450 tool
->build_id
= process_finished_round_stub
;
451 if (tool
->finished_round
== NULL
) {
452 if (tool
->ordered_samples
)
453 tool
->finished_round
= process_finished_round
;
455 tool
->finished_round
= process_finished_round_stub
;
459 void mem_bswap_32(void *src
, int byte_size
)
462 while (byte_size
> 0) {
464 byte_size
-= sizeof(u32
);
469 void mem_bswap_64(void *src
, int byte_size
)
473 while (byte_size
> 0) {
475 byte_size
-= sizeof(u64
);
480 static void swap_sample_id_all(union perf_event
*event
, void *data
)
482 void *end
= (void *) event
+ event
->header
.size
;
483 int size
= end
- data
;
485 BUG_ON(size
% sizeof(u64
));
486 mem_bswap_64(data
, size
);
489 static void perf_event__all64_swap(union perf_event
*event
,
490 bool sample_id_all __used
)
492 struct perf_event_header
*hdr
= &event
->header
;
493 mem_bswap_64(hdr
+ 1, event
->header
.size
- sizeof(*hdr
));
496 static void perf_event__comm_swap(union perf_event
*event
, bool sample_id_all
)
498 event
->comm
.pid
= bswap_32(event
->comm
.pid
);
499 event
->comm
.tid
= bswap_32(event
->comm
.tid
);
502 void *data
= &event
->comm
.comm
;
504 data
+= ALIGN(strlen(data
) + 1, sizeof(u64
));
505 swap_sample_id_all(event
, data
);
509 static void perf_event__mmap_swap(union perf_event
*event
,
512 event
->mmap
.pid
= bswap_32(event
->mmap
.pid
);
513 event
->mmap
.tid
= bswap_32(event
->mmap
.tid
);
514 event
->mmap
.start
= bswap_64(event
->mmap
.start
);
515 event
->mmap
.len
= bswap_64(event
->mmap
.len
);
516 event
->mmap
.pgoff
= bswap_64(event
->mmap
.pgoff
);
519 void *data
= &event
->mmap
.filename
;
521 data
+= ALIGN(strlen(data
) + 1, sizeof(u64
));
522 swap_sample_id_all(event
, data
);
526 static void perf_event__task_swap(union perf_event
*event
, bool sample_id_all
)
528 event
->fork
.pid
= bswap_32(event
->fork
.pid
);
529 event
->fork
.tid
= bswap_32(event
->fork
.tid
);
530 event
->fork
.ppid
= bswap_32(event
->fork
.ppid
);
531 event
->fork
.ptid
= bswap_32(event
->fork
.ptid
);
532 event
->fork
.time
= bswap_64(event
->fork
.time
);
535 swap_sample_id_all(event
, &event
->fork
+ 1);
538 static void perf_event__read_swap(union perf_event
*event
, bool sample_id_all
)
540 event
->read
.pid
= bswap_32(event
->read
.pid
);
541 event
->read
.tid
= bswap_32(event
->read
.tid
);
542 event
->read
.value
= bswap_64(event
->read
.value
);
543 event
->read
.time_enabled
= bswap_64(event
->read
.time_enabled
);
544 event
->read
.time_running
= bswap_64(event
->read
.time_running
);
545 event
->read
.id
= bswap_64(event
->read
.id
);
548 swap_sample_id_all(event
, &event
->read
+ 1);
551 static u8
revbyte(u8 b
)
553 int rev
= (b
>> 4) | ((b
& 0xf) << 4);
554 rev
= ((rev
& 0xcc) >> 2) | ((rev
& 0x33) << 2);
555 rev
= ((rev
& 0xaa) >> 1) | ((rev
& 0x55) << 1);
560 * XXX this is hack in attempt to carry flags bitfield
561 * throught endian village. ABI says:
563 * Bit-fields are allocated from right to left (least to most significant)
564 * on little-endian implementations and from left to right (most to least
565 * significant) on big-endian implementations.
567 * The above seems to be byte specific, so we need to reverse each
568 * byte of the bitfield. 'Internet' also says this might be implementation
569 * specific and we probably need proper fix and carry perf_event_attr
570 * bitfield flags in separate data file FEAT_ section. Thought this seems
573 static void swap_bitfield(u8
*p
, unsigned len
)
577 for (i
= 0; i
< len
; i
++) {
583 /* exported for swapping attributes in file header */
584 void perf_event__attr_swap(struct perf_event_attr
*attr
)
586 attr
->type
= bswap_32(attr
->type
);
587 attr
->size
= bswap_32(attr
->size
);
588 attr
->config
= bswap_64(attr
->config
);
589 attr
->sample_period
= bswap_64(attr
->sample_period
);
590 attr
->sample_type
= bswap_64(attr
->sample_type
);
591 attr
->read_format
= bswap_64(attr
->read_format
);
592 attr
->wakeup_events
= bswap_32(attr
->wakeup_events
);
593 attr
->bp_type
= bswap_32(attr
->bp_type
);
594 attr
->bp_addr
= bswap_64(attr
->bp_addr
);
595 attr
->bp_len
= bswap_64(attr
->bp_len
);
597 swap_bitfield((u8
*) (&attr
->read_format
+ 1), sizeof(u64
));
600 static void perf_event__hdr_attr_swap(union perf_event
*event
,
601 bool sample_id_all __used
)
605 perf_event__attr_swap(&event
->attr
.attr
);
607 size
= event
->header
.size
;
608 size
-= (void *)&event
->attr
.id
- (void *)event
;
609 mem_bswap_64(event
->attr
.id
, size
);
612 static void perf_event__event_type_swap(union perf_event
*event
,
613 bool sample_id_all __used
)
615 event
->event_type
.event_type
.event_id
=
616 bswap_64(event
->event_type
.event_type
.event_id
);
619 static void perf_event__tracing_data_swap(union perf_event
*event
,
620 bool sample_id_all __used
)
622 event
->tracing_data
.size
= bswap_32(event
->tracing_data
.size
);
625 typedef void (*perf_event__swap_op
)(union perf_event
*event
,
628 static perf_event__swap_op perf_event__swap_ops
[] = {
629 [PERF_RECORD_MMAP
] = perf_event__mmap_swap
,
630 [PERF_RECORD_COMM
] = perf_event__comm_swap
,
631 [PERF_RECORD_FORK
] = perf_event__task_swap
,
632 [PERF_RECORD_EXIT
] = perf_event__task_swap
,
633 [PERF_RECORD_LOST
] = perf_event__all64_swap
,
634 [PERF_RECORD_READ
] = perf_event__read_swap
,
635 [PERF_RECORD_SAMPLE
] = perf_event__all64_swap
,
636 [PERF_RECORD_HEADER_ATTR
] = perf_event__hdr_attr_swap
,
637 [PERF_RECORD_HEADER_EVENT_TYPE
] = perf_event__event_type_swap
,
638 [PERF_RECORD_HEADER_TRACING_DATA
] = perf_event__tracing_data_swap
,
639 [PERF_RECORD_HEADER_BUILD_ID
] = NULL
,
640 [PERF_RECORD_HEADER_MAX
] = NULL
,
643 struct sample_queue
{
646 union perf_event
*event
;
647 struct list_head list
;
650 static void perf_session_free_sample_buffers(struct perf_session
*session
)
652 struct ordered_samples
*os
= &session
->ordered_samples
;
654 while (!list_empty(&os
->to_free
)) {
655 struct sample_queue
*sq
;
657 sq
= list_entry(os
->to_free
.next
, struct sample_queue
, list
);
663 static int perf_session_deliver_event(struct perf_session
*session
,
664 union perf_event
*event
,
665 struct perf_sample
*sample
,
666 struct perf_tool
*tool
,
669 static void flush_sample_queue(struct perf_session
*s
,
670 struct perf_tool
*tool
)
672 struct ordered_samples
*os
= &s
->ordered_samples
;
673 struct list_head
*head
= &os
->samples
;
674 struct sample_queue
*tmp
, *iter
;
675 struct perf_sample sample
;
676 u64 limit
= os
->next_flush
;
677 u64 last_ts
= os
->last_sample
? os
->last_sample
->timestamp
: 0ULL;
678 unsigned idx
= 0, progress_next
= os
->nr_samples
/ 16;
681 if (!tool
->ordered_samples
|| !limit
)
684 list_for_each_entry_safe(iter
, tmp
, head
, list
) {
685 if (iter
->timestamp
> limit
)
688 ret
= perf_session__parse_sample(s
, iter
->event
, &sample
);
690 pr_err("Can't parse sample, err = %d\n", ret
);
692 perf_session_deliver_event(s
, iter
->event
, &sample
, tool
,
695 os
->last_flush
= iter
->timestamp
;
696 list_del(&iter
->list
);
697 list_add(&iter
->list
, &os
->sample_cache
);
698 if (++idx
>= progress_next
) {
699 progress_next
+= os
->nr_samples
/ 16;
700 ui_progress__update(idx
, os
->nr_samples
,
701 "Processing time ordered events...");
705 if (list_empty(head
)) {
706 os
->last_sample
= NULL
;
707 } else if (last_ts
<= limit
) {
709 list_entry(head
->prev
, struct sample_queue
, list
);
716 * When perf record finishes a pass on every buffers, it records this pseudo
718 * We record the max timestamp t found in the pass n.
719 * Assuming these timestamps are monotonic across cpus, we know that if
720 * a buffer still has events with timestamps below t, they will be all
721 * available and then read in the pass n + 1.
722 * Hence when we start to read the pass n + 2, we can safely flush every
723 * events with timestamps below t.
725 * ============ PASS n =================
728 * cnt1 timestamps | cnt2 timestamps
731 * - | 4 <--- max recorded
733 * ============ PASS n + 1 ==============
736 * cnt1 timestamps | cnt2 timestamps
739 * 5 | 7 <---- max recorded
741 * Flush every events below timestamp 4
743 * ============ PASS n + 2 ==============
746 * cnt1 timestamps | cnt2 timestamps
751 * Flush every events below timestamp 7
754 static int process_finished_round(struct perf_tool
*tool
,
755 union perf_event
*event __used
,
756 struct perf_session
*session
)
758 flush_sample_queue(session
, tool
);
759 session
->ordered_samples
.next_flush
= session
->ordered_samples
.max_timestamp
;
764 /* The queue is ordered by time */
765 static void __queue_event(struct sample_queue
*new, struct perf_session
*s
)
767 struct ordered_samples
*os
= &s
->ordered_samples
;
768 struct sample_queue
*sample
= os
->last_sample
;
769 u64 timestamp
= new->timestamp
;
773 os
->last_sample
= new;
776 list_add(&new->list
, &os
->samples
);
777 os
->max_timestamp
= timestamp
;
782 * last_sample might point to some random place in the list as it's
783 * the last queued event. We expect that the new event is close to
786 if (sample
->timestamp
<= timestamp
) {
787 while (sample
->timestamp
<= timestamp
) {
788 p
= sample
->list
.next
;
789 if (p
== &os
->samples
) {
790 list_add_tail(&new->list
, &os
->samples
);
791 os
->max_timestamp
= timestamp
;
794 sample
= list_entry(p
, struct sample_queue
, list
);
796 list_add_tail(&new->list
, &sample
->list
);
798 while (sample
->timestamp
> timestamp
) {
799 p
= sample
->list
.prev
;
800 if (p
== &os
->samples
) {
801 list_add(&new->list
, &os
->samples
);
804 sample
= list_entry(p
, struct sample_queue
, list
);
806 list_add(&new->list
, &sample
->list
);
810 #define MAX_SAMPLE_BUFFER (64 * 1024 / sizeof(struct sample_queue))
812 static int perf_session_queue_event(struct perf_session
*s
, union perf_event
*event
,
813 struct perf_sample
*sample
, u64 file_offset
)
815 struct ordered_samples
*os
= &s
->ordered_samples
;
816 struct list_head
*sc
= &os
->sample_cache
;
817 u64 timestamp
= sample
->time
;
818 struct sample_queue
*new;
820 if (!timestamp
|| timestamp
== ~0ULL)
823 if (timestamp
< s
->ordered_samples
.last_flush
) {
824 printf("Warning: Timestamp below last timeslice flush\n");
828 if (!list_empty(sc
)) {
829 new = list_entry(sc
->next
, struct sample_queue
, list
);
830 list_del(&new->list
);
831 } else if (os
->sample_buffer
) {
832 new = os
->sample_buffer
+ os
->sample_buffer_idx
;
833 if (++os
->sample_buffer_idx
== MAX_SAMPLE_BUFFER
)
834 os
->sample_buffer
= NULL
;
836 os
->sample_buffer
= malloc(MAX_SAMPLE_BUFFER
* sizeof(*new));
837 if (!os
->sample_buffer
)
839 list_add(&os
->sample_buffer
->list
, &os
->to_free
);
840 os
->sample_buffer_idx
= 2;
841 new = os
->sample_buffer
+ 1;
844 new->timestamp
= timestamp
;
845 new->file_offset
= file_offset
;
848 __queue_event(new, s
);
853 static void callchain__printf(struct perf_sample
*sample
)
857 printf("... chain: nr:%" PRIu64
"\n", sample
->callchain
->nr
);
859 for (i
= 0; i
< sample
->callchain
->nr
; i
++)
860 printf("..... %2d: %016" PRIx64
"\n",
861 i
, sample
->callchain
->ips
[i
]);
864 static void branch_stack__printf(struct perf_sample
*sample
)
868 printf("... branch stack: nr:%" PRIu64
"\n", sample
->branch_stack
->nr
);
870 for (i
= 0; i
< sample
->branch_stack
->nr
; i
++)
871 printf("..... %2"PRIu64
": %016" PRIx64
" -> %016" PRIx64
"\n",
872 i
, sample
->branch_stack
->entries
[i
].from
,
873 sample
->branch_stack
->entries
[i
].to
);
876 static void perf_session__print_tstamp(struct perf_session
*session
,
877 union perf_event
*event
,
878 struct perf_sample
*sample
)
880 if (event
->header
.type
!= PERF_RECORD_SAMPLE
&&
881 !session
->sample_id_all
) {
882 fputs("-1 -1 ", stdout
);
886 if ((session
->sample_type
& PERF_SAMPLE_CPU
))
887 printf("%u ", sample
->cpu
);
889 if (session
->sample_type
& PERF_SAMPLE_TIME
)
890 printf("%" PRIu64
" ", sample
->time
);
893 static void dump_event(struct perf_session
*session
, union perf_event
*event
,
894 u64 file_offset
, struct perf_sample
*sample
)
899 printf("\n%#" PRIx64
" [%#x]: event: %d\n",
900 file_offset
, event
->header
.size
, event
->header
.type
);
905 perf_session__print_tstamp(session
, event
, sample
);
907 printf("%#" PRIx64
" [%#x]: PERF_RECORD_%s", file_offset
,
908 event
->header
.size
, perf_event__name(event
->header
.type
));
911 static void dump_sample(struct perf_session
*session
, union perf_event
*event
,
912 struct perf_sample
*sample
)
917 printf("(IP, %d): %d/%d: %#" PRIx64
" period: %" PRIu64
" addr: %#" PRIx64
"\n",
918 event
->header
.misc
, sample
->pid
, sample
->tid
, sample
->ip
,
919 sample
->period
, sample
->addr
);
921 if (session
->sample_type
& PERF_SAMPLE_CALLCHAIN
)
922 callchain__printf(sample
);
924 if (session
->sample_type
& PERF_SAMPLE_BRANCH_STACK
)
925 branch_stack__printf(sample
);
928 static struct machine
*
929 perf_session__find_machine_for_cpumode(struct perf_session
*session
,
930 union perf_event
*event
)
932 const u8 cpumode
= event
->header
.misc
& PERF_RECORD_MISC_CPUMODE_MASK
;
935 ((cpumode
== PERF_RECORD_MISC_GUEST_KERNEL
) ||
936 (cpumode
== PERF_RECORD_MISC_GUEST_USER
))) {
939 if (event
->header
.type
== PERF_RECORD_MMAP
)
940 pid
= event
->mmap
.pid
;
944 return perf_session__findnew_machine(session
, pid
);
947 return perf_session__find_host_machine(session
);
950 static int perf_session_deliver_event(struct perf_session
*session
,
951 union perf_event
*event
,
952 struct perf_sample
*sample
,
953 struct perf_tool
*tool
,
956 struct perf_evsel
*evsel
;
957 struct machine
*machine
;
959 dump_event(session
, event
, file_offset
, sample
);
961 evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
962 if (evsel
!= NULL
&& event
->header
.type
!= PERF_RECORD_SAMPLE
) {
964 * XXX We're leaving PERF_RECORD_SAMPLE unnacounted here
965 * because the tools right now may apply filters, discarding
966 * some of the samples. For consistency, in the future we
967 * should have something like nr_filtered_samples and remove
968 * the sample->period from total_sample_period, etc, KISS for
971 * Also testing against NULL allows us to handle files without
972 * attr.sample_id_all and/or without PERF_SAMPLE_ID. In the
973 * future probably it'll be a good idea to restrict event
974 * processing via perf_session to files with both set.
976 hists__inc_nr_events(&evsel
->hists
, event
->header
.type
);
979 machine
= perf_session__find_machine_for_cpumode(session
, event
);
981 switch (event
->header
.type
) {
982 case PERF_RECORD_SAMPLE
:
983 dump_sample(session
, event
, sample
);
985 ++session
->hists
.stats
.nr_unknown_id
;
988 if (machine
== NULL
) {
989 ++session
->hists
.stats
.nr_unprocessable_samples
;
992 return tool
->sample(tool
, event
, sample
, evsel
, machine
);
993 case PERF_RECORD_MMAP
:
994 return tool
->mmap(tool
, event
, sample
, machine
);
995 case PERF_RECORD_COMM
:
996 return tool
->comm(tool
, event
, sample
, machine
);
997 case PERF_RECORD_FORK
:
998 return tool
->fork(tool
, event
, sample
, machine
);
999 case PERF_RECORD_EXIT
:
1000 return tool
->exit(tool
, event
, sample
, machine
);
1001 case PERF_RECORD_LOST
:
1002 if (tool
->lost
== perf_event__process_lost
)
1003 session
->hists
.stats
.total_lost
+= event
->lost
.lost
;
1004 return tool
->lost(tool
, event
, sample
, machine
);
1005 case PERF_RECORD_READ
:
1006 return tool
->read(tool
, event
, sample
, evsel
, machine
);
1007 case PERF_RECORD_THROTTLE
:
1008 return tool
->throttle(tool
, event
, sample
, machine
);
1009 case PERF_RECORD_UNTHROTTLE
:
1010 return tool
->unthrottle(tool
, event
, sample
, machine
);
1012 ++session
->hists
.stats
.nr_unknown_events
;
1017 static int perf_session__preprocess_sample(struct perf_session
*session
,
1018 union perf_event
*event
, struct perf_sample
*sample
)
1020 if (event
->header
.type
!= PERF_RECORD_SAMPLE
||
1021 !(session
->sample_type
& PERF_SAMPLE_CALLCHAIN
))
1024 if (!ip_callchain__valid(sample
->callchain
, event
)) {
1025 pr_debug("call-chain problem with event, skipping it.\n");
1026 ++session
->hists
.stats
.nr_invalid_chains
;
1027 session
->hists
.stats
.total_invalid_chains
+= sample
->period
;
1033 static int perf_session__process_user_event(struct perf_session
*session
, union perf_event
*event
,
1034 struct perf_tool
*tool
, u64 file_offset
)
1038 dump_event(session
, event
, file_offset
, NULL
);
1040 /* These events are processed right away */
1041 switch (event
->header
.type
) {
1042 case PERF_RECORD_HEADER_ATTR
:
1043 err
= tool
->attr(event
, &session
->evlist
);
1045 perf_session__update_sample_type(session
);
1047 case PERF_RECORD_HEADER_EVENT_TYPE
:
1048 return tool
->event_type(tool
, event
);
1049 case PERF_RECORD_HEADER_TRACING_DATA
:
1050 /* setup for reading amidst mmap */
1051 lseek(session
->fd
, file_offset
, SEEK_SET
);
1052 return tool
->tracing_data(event
, session
);
1053 case PERF_RECORD_HEADER_BUILD_ID
:
1054 return tool
->build_id(tool
, event
, session
);
1055 case PERF_RECORD_FINISHED_ROUND
:
1056 return tool
->finished_round(tool
, event
, session
);
1062 static void event_swap(union perf_event
*event
, bool sample_id_all
)
1064 perf_event__swap_op swap
;
1066 swap
= perf_event__swap_ops
[event
->header
.type
];
1068 swap(event
, sample_id_all
);
1071 static int perf_session__process_event(struct perf_session
*session
,
1072 union perf_event
*event
,
1073 struct perf_tool
*tool
,
1076 struct perf_sample sample
;
1079 if (session
->header
.needs_swap
)
1080 event_swap(event
, session
->sample_id_all
);
1082 if (event
->header
.type
>= PERF_RECORD_HEADER_MAX
)
1085 hists__inc_nr_events(&session
->hists
, event
->header
.type
);
1087 if (event
->header
.type
>= PERF_RECORD_USER_TYPE_START
)
1088 return perf_session__process_user_event(session
, event
, tool
, file_offset
);
1091 * For all kernel events we get the sample data
1093 ret
= perf_session__parse_sample(session
, event
, &sample
);
1097 /* Preprocess sample records - precheck callchains */
1098 if (perf_session__preprocess_sample(session
, event
, &sample
))
1101 if (tool
->ordered_samples
) {
1102 ret
= perf_session_queue_event(session
, event
, &sample
,
1108 return perf_session_deliver_event(session
, event
, &sample
, tool
,
1112 void perf_event_header__bswap(struct perf_event_header
*self
)
1114 self
->type
= bswap_32(self
->type
);
1115 self
->misc
= bswap_16(self
->misc
);
1116 self
->size
= bswap_16(self
->size
);
1119 struct thread
*perf_session__findnew(struct perf_session
*session
, pid_t pid
)
1121 return machine__findnew_thread(&session
->host_machine
, pid
);
1124 static struct thread
*perf_session__register_idle_thread(struct perf_session
*self
)
1126 struct thread
*thread
= perf_session__findnew(self
, 0);
1128 if (thread
== NULL
|| thread__set_comm(thread
, "swapper")) {
1129 pr_err("problem inserting idle task.\n");
1136 static void perf_session__warn_about_errors(const struct perf_session
*session
,
1137 const struct perf_tool
*tool
)
1139 if (tool
->lost
== perf_event__process_lost
&&
1140 session
->hists
.stats
.nr_events
[PERF_RECORD_LOST
] != 0) {
1141 ui__warning("Processed %d events and lost %d chunks!\n\n"
1142 "Check IO/CPU overload!\n\n",
1143 session
->hists
.stats
.nr_events
[0],
1144 session
->hists
.stats
.nr_events
[PERF_RECORD_LOST
]);
1147 if (session
->hists
.stats
.nr_unknown_events
!= 0) {
1148 ui__warning("Found %u unknown events!\n\n"
1149 "Is this an older tool processing a perf.data "
1150 "file generated by a more recent tool?\n\n"
1151 "If that is not the case, consider "
1152 "reporting to linux-kernel@vger.kernel.org.\n\n",
1153 session
->hists
.stats
.nr_unknown_events
);
1156 if (session
->hists
.stats
.nr_unknown_id
!= 0) {
1157 ui__warning("%u samples with id not present in the header\n",
1158 session
->hists
.stats
.nr_unknown_id
);
1161 if (session
->hists
.stats
.nr_invalid_chains
!= 0) {
1162 ui__warning("Found invalid callchains!\n\n"
1163 "%u out of %u events were discarded for this reason.\n\n"
1164 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
1165 session
->hists
.stats
.nr_invalid_chains
,
1166 session
->hists
.stats
.nr_events
[PERF_RECORD_SAMPLE
]);
1169 if (session
->hists
.stats
.nr_unprocessable_samples
!= 0) {
1170 ui__warning("%u unprocessable samples recorded.\n"
1171 "Do you have a KVM guest running and not using 'perf kvm'?\n",
1172 session
->hists
.stats
.nr_unprocessable_samples
);
1176 #define session_done() (*(volatile int *)(&session_done))
1177 volatile int session_done
;
1179 static int __perf_session__process_pipe_events(struct perf_session
*self
,
1180 struct perf_tool
*tool
)
1182 union perf_event
*event
;
1183 uint32_t size
, cur_size
= 0;
1190 perf_tool__fill_defaults(tool
);
1193 cur_size
= sizeof(union perf_event
);
1195 buf
= malloc(cur_size
);
1200 err
= readn(self
->fd
, event
, sizeof(struct perf_event_header
));
1205 pr_err("failed to read event header\n");
1209 if (self
->header
.needs_swap
)
1210 perf_event_header__bswap(&event
->header
);
1212 size
= event
->header
.size
;
1216 if (size
> cur_size
) {
1217 void *new = realloc(buf
, size
);
1219 pr_err("failed to allocate memory to read event\n");
1227 p
+= sizeof(struct perf_event_header
);
1229 if (size
- sizeof(struct perf_event_header
)) {
1230 err
= readn(self
->fd
, p
, size
- sizeof(struct perf_event_header
));
1233 pr_err("unexpected end of event stream\n");
1237 pr_err("failed to read event data\n");
1242 if ((skip
= perf_session__process_event(self
, event
, tool
, head
)) < 0) {
1243 pr_err("%#" PRIx64
" [%#x]: failed to process type: %d\n",
1244 head
, event
->header
.size
, event
->header
.type
);
1254 if (!session_done())
1260 perf_session__warn_about_errors(self
, tool
);
1261 perf_session_free_sample_buffers(self
);
1265 static union perf_event
*
1266 fetch_mmaped_event(struct perf_session
*session
,
1267 u64 head
, size_t mmap_size
, char *buf
)
1269 union perf_event
*event
;
1272 * Ensure we have enough space remaining to read
1273 * the size of the event in the headers.
1275 if (head
+ sizeof(event
->header
) > mmap_size
)
1278 event
= (union perf_event
*)(buf
+ head
);
1280 if (session
->header
.needs_swap
)
1281 perf_event_header__bswap(&event
->header
);
1283 if (head
+ event
->header
.size
> mmap_size
)
1289 int __perf_session__process_events(struct perf_session
*session
,
1290 u64 data_offset
, u64 data_size
,
1291 u64 file_size
, struct perf_tool
*tool
)
1293 u64 head
, page_offset
, file_offset
, file_pos
, progress_next
;
1294 int err
, mmap_prot
, mmap_flags
, map_idx
= 0;
1295 size_t page_size
, mmap_size
;
1296 char *buf
, *mmaps
[8];
1297 union perf_event
*event
;
1300 perf_tool__fill_defaults(tool
);
1302 page_size
= sysconf(_SC_PAGESIZE
);
1304 page_offset
= page_size
* (data_offset
/ page_size
);
1305 file_offset
= page_offset
;
1306 head
= data_offset
- page_offset
;
1308 if (data_offset
+ data_size
< file_size
)
1309 file_size
= data_offset
+ data_size
;
1311 progress_next
= file_size
/ 16;
1313 mmap_size
= session
->mmap_window
;
1314 if (mmap_size
> file_size
)
1315 mmap_size
= file_size
;
1317 memset(mmaps
, 0, sizeof(mmaps
));
1319 mmap_prot
= PROT_READ
;
1320 mmap_flags
= MAP_SHARED
;
1322 if (session
->header
.needs_swap
) {
1323 mmap_prot
|= PROT_WRITE
;
1324 mmap_flags
= MAP_PRIVATE
;
1327 buf
= mmap(NULL
, mmap_size
, mmap_prot
, mmap_flags
, session
->fd
,
1329 if (buf
== MAP_FAILED
) {
1330 pr_err("failed to mmap file\n");
1334 mmaps
[map_idx
] = buf
;
1335 map_idx
= (map_idx
+ 1) & (ARRAY_SIZE(mmaps
) - 1);
1336 file_pos
= file_offset
+ head
;
1339 event
= fetch_mmaped_event(session
, head
, mmap_size
, buf
);
1341 if (mmaps
[map_idx
]) {
1342 munmap(mmaps
[map_idx
], mmap_size
);
1343 mmaps
[map_idx
] = NULL
;
1346 page_offset
= page_size
* (head
/ page_size
);
1347 file_offset
+= page_offset
;
1348 head
-= page_offset
;
1352 size
= event
->header
.size
;
1355 perf_session__process_event(session
, event
, tool
, file_pos
) < 0) {
1356 pr_err("%#" PRIx64
" [%#x]: failed to process type: %d\n",
1357 file_offset
+ head
, event
->header
.size
,
1358 event
->header
.type
);
1366 if (file_pos
>= progress_next
) {
1367 progress_next
+= file_size
/ 16;
1368 ui_progress__update(file_pos
, file_size
,
1369 "Processing events...");
1372 if (file_pos
< file_size
)
1376 /* do the final flush for ordered samples */
1377 session
->ordered_samples
.next_flush
= ULLONG_MAX
;
1378 flush_sample_queue(session
, tool
);
1380 perf_session__warn_about_errors(session
, tool
);
1381 perf_session_free_sample_buffers(session
);
1385 int perf_session__process_events(struct perf_session
*self
,
1386 struct perf_tool
*tool
)
1390 if (perf_session__register_idle_thread(self
) == NULL
)
1394 err
= __perf_session__process_events(self
,
1395 self
->header
.data_offset
,
1396 self
->header
.data_size
,
1399 err
= __perf_session__process_pipe_events(self
, tool
);
1404 bool perf_session__has_traces(struct perf_session
*self
, const char *msg
)
1406 if (!(self
->sample_type
& PERF_SAMPLE_RAW
)) {
1407 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg
);
1414 int maps__set_kallsyms_ref_reloc_sym(struct map
**maps
,
1415 const char *symbol_name
, u64 addr
)
1419 struct ref_reloc_sym
*ref
;
1421 ref
= zalloc(sizeof(struct ref_reloc_sym
));
1425 ref
->name
= strdup(symbol_name
);
1426 if (ref
->name
== NULL
) {
1431 bracket
= strchr(ref
->name
, ']');
1437 for (i
= 0; i
< MAP__NR_TYPES
; ++i
) {
1438 struct kmap
*kmap
= map__kmap(maps
[i
]);
1439 kmap
->ref_reloc_sym
= ref
;
1445 size_t perf_session__fprintf_dsos(struct perf_session
*self
, FILE *fp
)
1447 return __dsos__fprintf(&self
->host_machine
.kernel_dsos
, fp
) +
1448 __dsos__fprintf(&self
->host_machine
.user_dsos
, fp
) +
1449 machines__fprintf_dsos(&self
->machines
, fp
);
1452 size_t perf_session__fprintf_dsos_buildid(struct perf_session
*self
, FILE *fp
,
1455 size_t ret
= machine__fprintf_dsos_buildid(&self
->host_machine
, fp
, with_hits
);
1456 return ret
+ machines__fprintf_dsos_buildid(&self
->machines
, fp
, with_hits
);
1459 size_t perf_session__fprintf_nr_events(struct perf_session
*session
, FILE *fp
)
1461 struct perf_evsel
*pos
;
1462 size_t ret
= fprintf(fp
, "Aggregated stats:\n");
1464 ret
+= hists__fprintf_nr_events(&session
->hists
, fp
);
1466 list_for_each_entry(pos
, &session
->evlist
->entries
, node
) {
1467 ret
+= fprintf(fp
, "%s stats:\n", perf_evsel__name(pos
));
1468 ret
+= hists__fprintf_nr_events(&pos
->hists
, fp
);
1474 size_t perf_session__fprintf(struct perf_session
*session
, FILE *fp
)
1477 * FIXME: Here we have to actually print all the machines in this
1478 * session, not just the host...
1480 return machine__fprintf(&session
->host_machine
, fp
);
1483 void perf_session__remove_thread(struct perf_session
*session
,
1487 * FIXME: This one makes no sense, we need to remove the thread from
1488 * the machine it belongs to, perf_session can have many machines, so
1489 * doing it always on ->host_machine is wrong. Fix when auditing all
1490 * the 'perf kvm' code.
1492 machine__remove_thread(&session
->host_machine
, th
);
1495 struct perf_evsel
*perf_session__find_first_evtype(struct perf_session
*session
,
1498 struct perf_evsel
*pos
;
1500 list_for_each_entry(pos
, &session
->evlist
->entries
, node
) {
1501 if (pos
->attr
.type
== type
)
1507 void perf_event__print_ip(union perf_event
*event
, struct perf_sample
*sample
,
1508 struct machine
*machine
, int print_sym
,
1509 int print_dso
, int print_symoffset
)
1511 struct addr_location al
;
1512 struct callchain_cursor_node
*node
;
1514 if (perf_event__preprocess_sample(event
, machine
, &al
, sample
,
1516 error("problem processing %d event, skipping it.\n",
1517 event
->header
.type
);
1521 if (symbol_conf
.use_callchain
&& sample
->callchain
) {
1523 if (machine__resolve_callchain(machine
, al
.thread
,
1524 sample
->callchain
, NULL
) != 0) {
1526 error("Failed to resolve callchain. Skipping\n");
1529 callchain_cursor_commit(&callchain_cursor
);
1532 node
= callchain_cursor_current(&callchain_cursor
);
1536 printf("\t%16" PRIx64
, node
->ip
);
1539 symbol__fprintf_symname(node
->sym
, stdout
);
1543 map__fprintf_dsoname(node
->map
, stdout
);
1548 callchain_cursor_advance(&callchain_cursor
);
1552 printf("%16" PRIx64
, sample
->ip
);
1555 if (print_symoffset
)
1556 symbol__fprintf_symname_offs(al
.sym
, &al
,
1559 symbol__fprintf_symname(al
.sym
, stdout
);
1564 map__fprintf_dsoname(al
.map
, stdout
);
1570 int perf_session__cpu_bitmap(struct perf_session
*session
,
1571 const char *cpu_list
, unsigned long *cpu_bitmap
)
1574 struct cpu_map
*map
;
1576 for (i
= 0; i
< PERF_TYPE_MAX
; ++i
) {
1577 struct perf_evsel
*evsel
;
1579 evsel
= perf_session__find_first_evtype(session
, i
);
1583 if (!(evsel
->attr
.sample_type
& PERF_SAMPLE_CPU
)) {
1584 pr_err("File does not contain CPU events. "
1585 "Remove -c option to proceed.\n");
1590 map
= cpu_map__new(cpu_list
);
1592 pr_err("Invalid cpu_list\n");
1596 for (i
= 0; i
< map
->nr
; i
++) {
1597 int cpu
= map
->map
[i
];
1599 if (cpu
>= MAX_NR_CPUS
) {
1600 pr_err("Requested CPU %d too large. "
1601 "Consider raising MAX_NR_CPUS\n", cpu
);
1605 set_bit(cpu
, cpu_bitmap
);
1611 void perf_session__fprintf_info(struct perf_session
*session
, FILE *fp
,
1617 if (session
== NULL
|| fp
== NULL
)
1620 ret
= fstat(session
->fd
, &st
);
1624 fprintf(fp
, "# ========\n");
1625 fprintf(fp
, "# captured on: %s", ctime(&st
.st_ctime
));
1626 perf_header__fprintf_info(session
, fp
, full
);
1627 fprintf(fp
, "# ========\n#\n");
1631 int __perf_session__set_tracepoints_handlers(struct perf_session
*session
,
1632 const struct perf_evsel_str_handler
*assocs
,
1635 struct perf_evlist
*evlist
= session
->evlist
;
1636 struct event_format
*format
;
1637 struct perf_evsel
*evsel
;
1638 char *tracepoint
, *name
;
1642 for (i
= 0; i
< nr_assocs
; i
++) {
1644 tracepoint
= strdup(assocs
[i
].name
);
1645 if (tracepoint
== NULL
)
1649 name
= strchr(tracepoint
, ':');
1654 format
= pevent_find_event_by_name(session
->pevent
,
1656 if (format
== NULL
) {
1658 * Adding a handler for an event not in the session,
1664 evsel
= perf_evlist__find_tracepoint_by_id(evlist
, format
->id
);
1669 if (evsel
->handler
.func
!= NULL
)
1671 evsel
->handler
.func
= assocs
[i
].handler
;