1 #define _FILE_OFFSET_BITS 64
3 #include <linux/kernel.h>
17 #include "event-parse.h"
19 static int perf_session__open(struct perf_session
*self
, bool force
)
21 struct stat input_stat
;
23 if (!strcmp(self
->filename
, "-")) {
25 self
->fd
= STDIN_FILENO
;
27 if (perf_session__read_header(self
, self
->fd
) < 0)
28 pr_err("incompatible file format (rerun with -v to learn more)");
33 self
->fd
= open(self
->filename
, O_RDONLY
);
37 pr_err("failed to open %s: %s", self
->filename
, strerror(err
));
38 if (err
== ENOENT
&& !strcmp(self
->filename
, "perf.data"))
39 pr_err(" (try 'perf record' first)");
44 if (fstat(self
->fd
, &input_stat
) < 0)
47 if (!force
&& input_stat
.st_uid
&& (input_stat
.st_uid
!= geteuid())) {
48 pr_err("file %s not owned by current user or root\n",
53 if (!input_stat
.st_size
) {
54 pr_info("zero-sized file (%s), nothing to do!\n",
59 if (perf_session__read_header(self
, self
->fd
) < 0) {
60 pr_err("incompatible file format (rerun with -v to learn more)");
64 if (!perf_evlist__valid_sample_type(self
->evlist
)) {
65 pr_err("non matching sample_type");
69 if (!perf_evlist__valid_sample_id_all(self
->evlist
)) {
70 pr_err("non matching sample_id_all");
74 self
->size
= input_stat
.st_size
;
83 void perf_session__update_sample_type(struct perf_session
*self
)
85 self
->sample_type
= perf_evlist__sample_type(self
->evlist
);
86 self
->sample_size
= __perf_evsel__sample_size(self
->sample_type
);
87 self
->sample_id_all
= perf_evlist__sample_id_all(self
->evlist
);
88 self
->id_hdr_size
= perf_evlist__id_hdr_size(self
->evlist
);
89 self
->host_machine
.id_hdr_size
= self
->id_hdr_size
;
90 machines__set_id_hdr_size(&self
->machines
, self
->id_hdr_size
);
93 int perf_session__create_kernel_maps(struct perf_session
*self
)
95 int ret
= machine__create_kernel_maps(&self
->host_machine
);
98 ret
= machines__create_guest_kernel_maps(&self
->machines
);
102 static void perf_session__destroy_kernel_maps(struct perf_session
*self
)
104 machine__destroy_kernel_maps(&self
->host_machine
);
105 machines__destroy_guest_kernel_maps(&self
->machines
);
108 struct perf_session
*perf_session__new(const char *filename
, int mode
,
109 bool force
, bool repipe
,
110 struct perf_tool
*tool
)
112 struct perf_session
*self
;
116 if (!filename
|| !strlen(filename
)) {
117 if (!fstat(STDIN_FILENO
, &st
) && S_ISFIFO(st
.st_mode
))
120 filename
= "perf.data";
123 len
= strlen(filename
);
124 self
= zalloc(sizeof(*self
) + len
);
129 memcpy(self
->filename
, filename
, len
);
131 * On 64bit we can mmap the data file in one go. No need for tiny mmap
132 * slices. On 32bit we use 32MB.
134 #if BITS_PER_LONG == 64
135 self
->mmap_window
= ULLONG_MAX
;
137 self
->mmap_window
= 32 * 1024 * 1024ULL;
139 self
->machines
= RB_ROOT
;
140 self
->repipe
= repipe
;
141 INIT_LIST_HEAD(&self
->ordered_samples
.samples
);
142 INIT_LIST_HEAD(&self
->ordered_samples
.sample_cache
);
143 INIT_LIST_HEAD(&self
->ordered_samples
.to_free
);
144 machine__init(&self
->host_machine
, "", HOST_KERNEL_ID
);
145 hists__init(&self
->hists
);
147 if (mode
== O_RDONLY
) {
148 if (perf_session__open(self
, force
) < 0)
150 perf_session__update_sample_type(self
);
151 } else if (mode
== O_WRONLY
) {
153 * In O_RDONLY mode this will be performed when reading the
154 * kernel MMAP event, in perf_event__process_mmap().
156 if (perf_session__create_kernel_maps(self
) < 0)
160 if (tool
&& tool
->ordering_requires_timestamps
&&
161 tool
->ordered_samples
&& !self
->sample_id_all
) {
162 dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n");
163 tool
->ordered_samples
= false;
169 perf_session__delete(self
);
173 static void machine__delete_dead_threads(struct machine
*machine
)
175 struct thread
*n
, *t
;
177 list_for_each_entry_safe(t
, n
, &machine
->dead_threads
, node
) {
183 static void perf_session__delete_dead_threads(struct perf_session
*session
)
185 machine__delete_dead_threads(&session
->host_machine
);
188 static void machine__delete_threads(struct machine
*self
)
190 struct rb_node
*nd
= rb_first(&self
->threads
);
193 struct thread
*t
= rb_entry(nd
, struct thread
, rb_node
);
195 rb_erase(&t
->rb_node
, &self
->threads
);
201 static void perf_session__delete_threads(struct perf_session
*session
)
203 machine__delete_threads(&session
->host_machine
);
206 void perf_session__delete(struct perf_session
*self
)
208 perf_session__destroy_kernel_maps(self
);
209 perf_session__delete_dead_threads(self
);
210 perf_session__delete_threads(self
);
211 machine__exit(&self
->host_machine
);
216 void machine__remove_thread(struct machine
*self
, struct thread
*th
)
218 self
->last_match
= NULL
;
219 rb_erase(&th
->rb_node
, &self
->threads
);
221 * We may have references to this thread, for instance in some hist_entry
222 * instances, so just move them to a separate list.
224 list_add_tail(&th
->node
, &self
->dead_threads
);
227 static bool symbol__match_parent_regex(struct symbol
*sym
)
229 if (sym
->name
&& !regexec(&parent_regex
, sym
->name
, 0, NULL
, 0))
235 static const u8 cpumodes
[] = {
236 PERF_RECORD_MISC_USER
,
237 PERF_RECORD_MISC_KERNEL
,
238 PERF_RECORD_MISC_GUEST_USER
,
239 PERF_RECORD_MISC_GUEST_KERNEL
241 #define NCPUMODES (sizeof(cpumodes)/sizeof(u8))
243 static void ip__resolve_ams(struct machine
*self
, struct thread
*thread
,
244 struct addr_map_symbol
*ams
,
247 struct addr_location al
;
251 memset(&al
, 0, sizeof(al
));
253 for (i
= 0; i
< NCPUMODES
; i
++) {
256 * We cannot use the header.misc hint to determine whether a
257 * branch stack address is user, kernel, guest, hypervisor.
258 * Branches may straddle the kernel/user/hypervisor boundaries.
259 * Thus, we have to try consecutively until we find a match
260 * or else, the symbol is unknown
262 thread__find_addr_location(thread
, self
, m
, MAP__FUNCTION
,
269 ams
->al_addr
= al
.addr
;
274 struct branch_info
*machine__resolve_bstack(struct machine
*self
,
276 struct branch_stack
*bs
)
278 struct branch_info
*bi
;
281 bi
= calloc(bs
->nr
, sizeof(struct branch_info
));
285 for (i
= 0; i
< bs
->nr
; i
++) {
286 ip__resolve_ams(self
, thr
, &bi
[i
].to
, bs
->entries
[i
].to
);
287 ip__resolve_ams(self
, thr
, &bi
[i
].from
, bs
->entries
[i
].from
);
288 bi
[i
].flags
= bs
->entries
[i
].flags
;
293 int machine__resolve_callchain(struct machine
*self
,
294 struct thread
*thread
,
295 struct ip_callchain
*chain
,
296 struct symbol
**parent
)
298 u8 cpumode
= PERF_RECORD_MISC_USER
;
302 callchain_cursor_reset(&callchain_cursor
);
304 if (chain
->nr
> PERF_MAX_STACK_DEPTH
) {
305 pr_warning("corrupted callchain. skipping...\n");
309 for (i
= 0; i
< chain
->nr
; i
++) {
311 struct addr_location al
;
313 if (callchain_param
.order
== ORDER_CALLEE
)
316 ip
= chain
->ips
[chain
->nr
- i
- 1];
318 if (ip
>= PERF_CONTEXT_MAX
) {
320 case PERF_CONTEXT_HV
:
321 cpumode
= PERF_RECORD_MISC_HYPERVISOR
; break;
322 case PERF_CONTEXT_KERNEL
:
323 cpumode
= PERF_RECORD_MISC_KERNEL
; break;
324 case PERF_CONTEXT_USER
:
325 cpumode
= PERF_RECORD_MISC_USER
; break;
327 pr_debug("invalid callchain context: "
328 "%"PRId64
"\n", (s64
) ip
);
330 * It seems the callchain is corrupted.
333 callchain_cursor_reset(&callchain_cursor
);
340 thread__find_addr_location(thread
, self
, cpumode
,
341 MAP__FUNCTION
, ip
, &al
, NULL
);
342 if (al
.sym
!= NULL
) {
343 if (sort__has_parent
&& !*parent
&&
344 symbol__match_parent_regex(al
.sym
))
346 if (!symbol_conf
.use_callchain
)
350 err
= callchain_cursor_append(&callchain_cursor
,
359 static int process_event_synth_tracing_data_stub(union perf_event
*event __used
,
360 struct perf_session
*session __used
)
362 dump_printf(": unhandled!\n");
366 static int process_event_synth_attr_stub(union perf_event
*event __used
,
367 struct perf_evlist
**pevlist __used
)
369 dump_printf(": unhandled!\n");
373 static int process_event_sample_stub(struct perf_tool
*tool __used
,
374 union perf_event
*event __used
,
375 struct perf_sample
*sample __used
,
376 struct perf_evsel
*evsel __used
,
377 struct machine
*machine __used
)
379 dump_printf(": unhandled!\n");
383 static int process_event_stub(struct perf_tool
*tool __used
,
384 union perf_event
*event __used
,
385 struct perf_sample
*sample __used
,
386 struct machine
*machine __used
)
388 dump_printf(": unhandled!\n");
392 static int process_finished_round_stub(struct perf_tool
*tool __used
,
393 union perf_event
*event __used
,
394 struct perf_session
*perf_session __used
)
396 dump_printf(": unhandled!\n");
400 static int process_event_type_stub(struct perf_tool
*tool __used
,
401 union perf_event
*event __used
)
403 dump_printf(": unhandled!\n");
407 static int process_finished_round(struct perf_tool
*tool
,
408 union perf_event
*event
,
409 struct perf_session
*session
);
411 static void perf_tool__fill_defaults(struct perf_tool
*tool
)
413 if (tool
->sample
== NULL
)
414 tool
->sample
= process_event_sample_stub
;
415 if (tool
->mmap
== NULL
)
416 tool
->mmap
= process_event_stub
;
417 if (tool
->comm
== NULL
)
418 tool
->comm
= process_event_stub
;
419 if (tool
->fork
== NULL
)
420 tool
->fork
= process_event_stub
;
421 if (tool
->exit
== NULL
)
422 tool
->exit
= process_event_stub
;
423 if (tool
->lost
== NULL
)
424 tool
->lost
= perf_event__process_lost
;
425 if (tool
->read
== NULL
)
426 tool
->read
= process_event_sample_stub
;
427 if (tool
->throttle
== NULL
)
428 tool
->throttle
= process_event_stub
;
429 if (tool
->unthrottle
== NULL
)
430 tool
->unthrottle
= process_event_stub
;
431 if (tool
->attr
== NULL
)
432 tool
->attr
= process_event_synth_attr_stub
;
433 if (tool
->event_type
== NULL
)
434 tool
->event_type
= process_event_type_stub
;
435 if (tool
->tracing_data
== NULL
)
436 tool
->tracing_data
= process_event_synth_tracing_data_stub
;
437 if (tool
->build_id
== NULL
)
438 tool
->build_id
= process_finished_round_stub
;
439 if (tool
->finished_round
== NULL
) {
440 if (tool
->ordered_samples
)
441 tool
->finished_round
= process_finished_round
;
443 tool
->finished_round
= process_finished_round_stub
;
447 void mem_bswap_32(void *src
, int byte_size
)
450 while (byte_size
> 0) {
452 byte_size
-= sizeof(u32
);
457 void mem_bswap_64(void *src
, int byte_size
)
461 while (byte_size
> 0) {
463 byte_size
-= sizeof(u64
);
468 static void swap_sample_id_all(union perf_event
*event
, void *data
)
470 void *end
= (void *) event
+ event
->header
.size
;
471 int size
= end
- data
;
473 BUG_ON(size
% sizeof(u64
));
474 mem_bswap_64(data
, size
);
477 static void perf_event__all64_swap(union perf_event
*event
,
478 bool sample_id_all __used
)
480 struct perf_event_header
*hdr
= &event
->header
;
481 mem_bswap_64(hdr
+ 1, event
->header
.size
- sizeof(*hdr
));
484 static void perf_event__comm_swap(union perf_event
*event
, bool sample_id_all
)
486 event
->comm
.pid
= bswap_32(event
->comm
.pid
);
487 event
->comm
.tid
= bswap_32(event
->comm
.tid
);
490 void *data
= &event
->comm
.comm
;
492 data
+= ALIGN(strlen(data
) + 1, sizeof(u64
));
493 swap_sample_id_all(event
, data
);
497 static void perf_event__mmap_swap(union perf_event
*event
,
500 event
->mmap
.pid
= bswap_32(event
->mmap
.pid
);
501 event
->mmap
.tid
= bswap_32(event
->mmap
.tid
);
502 event
->mmap
.start
= bswap_64(event
->mmap
.start
);
503 event
->mmap
.len
= bswap_64(event
->mmap
.len
);
504 event
->mmap
.pgoff
= bswap_64(event
->mmap
.pgoff
);
507 void *data
= &event
->mmap
.filename
;
509 data
+= ALIGN(strlen(data
) + 1, sizeof(u64
));
510 swap_sample_id_all(event
, data
);
514 static void perf_event__task_swap(union perf_event
*event
, bool sample_id_all
)
516 event
->fork
.pid
= bswap_32(event
->fork
.pid
);
517 event
->fork
.tid
= bswap_32(event
->fork
.tid
);
518 event
->fork
.ppid
= bswap_32(event
->fork
.ppid
);
519 event
->fork
.ptid
= bswap_32(event
->fork
.ptid
);
520 event
->fork
.time
= bswap_64(event
->fork
.time
);
523 swap_sample_id_all(event
, &event
->fork
+ 1);
526 static void perf_event__read_swap(union perf_event
*event
, bool sample_id_all
)
528 event
->read
.pid
= bswap_32(event
->read
.pid
);
529 event
->read
.tid
= bswap_32(event
->read
.tid
);
530 event
->read
.value
= bswap_64(event
->read
.value
);
531 event
->read
.time_enabled
= bswap_64(event
->read
.time_enabled
);
532 event
->read
.time_running
= bswap_64(event
->read
.time_running
);
533 event
->read
.id
= bswap_64(event
->read
.id
);
536 swap_sample_id_all(event
, &event
->read
+ 1);
539 static u8
revbyte(u8 b
)
541 int rev
= (b
>> 4) | ((b
& 0xf) << 4);
542 rev
= ((rev
& 0xcc) >> 2) | ((rev
& 0x33) << 2);
543 rev
= ((rev
& 0xaa) >> 1) | ((rev
& 0x55) << 1);
548 * XXX this is hack in attempt to carry flags bitfield
549 * throught endian village. ABI says:
551 * Bit-fields are allocated from right to left (least to most significant)
552 * on little-endian implementations and from left to right (most to least
553 * significant) on big-endian implementations.
555 * The above seems to be byte specific, so we need to reverse each
556 * byte of the bitfield. 'Internet' also says this might be implementation
557 * specific and we probably need proper fix and carry perf_event_attr
558 * bitfield flags in separate data file FEAT_ section. Thought this seems
561 static void swap_bitfield(u8
*p
, unsigned len
)
565 for (i
= 0; i
< len
; i
++) {
571 /* exported for swapping attributes in file header */
572 void perf_event__attr_swap(struct perf_event_attr
*attr
)
574 attr
->type
= bswap_32(attr
->type
);
575 attr
->size
= bswap_32(attr
->size
);
576 attr
->config
= bswap_64(attr
->config
);
577 attr
->sample_period
= bswap_64(attr
->sample_period
);
578 attr
->sample_type
= bswap_64(attr
->sample_type
);
579 attr
->read_format
= bswap_64(attr
->read_format
);
580 attr
->wakeup_events
= bswap_32(attr
->wakeup_events
);
581 attr
->bp_type
= bswap_32(attr
->bp_type
);
582 attr
->bp_addr
= bswap_64(attr
->bp_addr
);
583 attr
->bp_len
= bswap_64(attr
->bp_len
);
585 swap_bitfield((u8
*) (&attr
->read_format
+ 1), sizeof(u64
));
588 static void perf_event__hdr_attr_swap(union perf_event
*event
,
589 bool sample_id_all __used
)
593 perf_event__attr_swap(&event
->attr
.attr
);
595 size
= event
->header
.size
;
596 size
-= (void *)&event
->attr
.id
- (void *)event
;
597 mem_bswap_64(event
->attr
.id
, size
);
600 static void perf_event__event_type_swap(union perf_event
*event
,
601 bool sample_id_all __used
)
603 event
->event_type
.event_type
.event_id
=
604 bswap_64(event
->event_type
.event_type
.event_id
);
607 static void perf_event__tracing_data_swap(union perf_event
*event
,
608 bool sample_id_all __used
)
610 event
->tracing_data
.size
= bswap_32(event
->tracing_data
.size
);
613 typedef void (*perf_event__swap_op
)(union perf_event
*event
,
616 static perf_event__swap_op perf_event__swap_ops
[] = {
617 [PERF_RECORD_MMAP
] = perf_event__mmap_swap
,
618 [PERF_RECORD_COMM
] = perf_event__comm_swap
,
619 [PERF_RECORD_FORK
] = perf_event__task_swap
,
620 [PERF_RECORD_EXIT
] = perf_event__task_swap
,
621 [PERF_RECORD_LOST
] = perf_event__all64_swap
,
622 [PERF_RECORD_READ
] = perf_event__read_swap
,
623 [PERF_RECORD_SAMPLE
] = perf_event__all64_swap
,
624 [PERF_RECORD_HEADER_ATTR
] = perf_event__hdr_attr_swap
,
625 [PERF_RECORD_HEADER_EVENT_TYPE
] = perf_event__event_type_swap
,
626 [PERF_RECORD_HEADER_TRACING_DATA
] = perf_event__tracing_data_swap
,
627 [PERF_RECORD_HEADER_BUILD_ID
] = NULL
,
628 [PERF_RECORD_HEADER_MAX
] = NULL
,
631 struct sample_queue
{
634 union perf_event
*event
;
635 struct list_head list
;
638 static void perf_session_free_sample_buffers(struct perf_session
*session
)
640 struct ordered_samples
*os
= &session
->ordered_samples
;
642 while (!list_empty(&os
->to_free
)) {
643 struct sample_queue
*sq
;
645 sq
= list_entry(os
->to_free
.next
, struct sample_queue
, list
);
651 static int perf_session_deliver_event(struct perf_session
*session
,
652 union perf_event
*event
,
653 struct perf_sample
*sample
,
654 struct perf_tool
*tool
,
657 static void flush_sample_queue(struct perf_session
*s
,
658 struct perf_tool
*tool
)
660 struct ordered_samples
*os
= &s
->ordered_samples
;
661 struct list_head
*head
= &os
->samples
;
662 struct sample_queue
*tmp
, *iter
;
663 struct perf_sample sample
;
664 u64 limit
= os
->next_flush
;
665 u64 last_ts
= os
->last_sample
? os
->last_sample
->timestamp
: 0ULL;
666 unsigned idx
= 0, progress_next
= os
->nr_samples
/ 16;
669 if (!tool
->ordered_samples
|| !limit
)
672 list_for_each_entry_safe(iter
, tmp
, head
, list
) {
673 if (iter
->timestamp
> limit
)
676 ret
= perf_session__parse_sample(s
, iter
->event
, &sample
);
678 pr_err("Can't parse sample, err = %d\n", ret
);
680 perf_session_deliver_event(s
, iter
->event
, &sample
, tool
,
683 os
->last_flush
= iter
->timestamp
;
684 list_del(&iter
->list
);
685 list_add(&iter
->list
, &os
->sample_cache
);
686 if (++idx
>= progress_next
) {
687 progress_next
+= os
->nr_samples
/ 16;
688 ui_progress__update(idx
, os
->nr_samples
,
689 "Processing time ordered events...");
693 if (list_empty(head
)) {
694 os
->last_sample
= NULL
;
695 } else if (last_ts
<= limit
) {
697 list_entry(head
->prev
, struct sample_queue
, list
);
704 * When perf record finishes a pass on every buffers, it records this pseudo
706 * We record the max timestamp t found in the pass n.
707 * Assuming these timestamps are monotonic across cpus, we know that if
708 * a buffer still has events with timestamps below t, they will be all
709 * available and then read in the pass n + 1.
710 * Hence when we start to read the pass n + 2, we can safely flush every
711 * events with timestamps below t.
713 * ============ PASS n =================
716 * cnt1 timestamps | cnt2 timestamps
719 * - | 4 <--- max recorded
721 * ============ PASS n + 1 ==============
724 * cnt1 timestamps | cnt2 timestamps
727 * 5 | 7 <---- max recorded
729 * Flush every events below timestamp 4
731 * ============ PASS n + 2 ==============
734 * cnt1 timestamps | cnt2 timestamps
739 * Flush every events below timestamp 7
742 static int process_finished_round(struct perf_tool
*tool
,
743 union perf_event
*event __used
,
744 struct perf_session
*session
)
746 flush_sample_queue(session
, tool
);
747 session
->ordered_samples
.next_flush
= session
->ordered_samples
.max_timestamp
;
752 /* The queue is ordered by time */
753 static void __queue_event(struct sample_queue
*new, struct perf_session
*s
)
755 struct ordered_samples
*os
= &s
->ordered_samples
;
756 struct sample_queue
*sample
= os
->last_sample
;
757 u64 timestamp
= new->timestamp
;
761 os
->last_sample
= new;
764 list_add(&new->list
, &os
->samples
);
765 os
->max_timestamp
= timestamp
;
770 * last_sample might point to some random place in the list as it's
771 * the last queued event. We expect that the new event is close to
774 if (sample
->timestamp
<= timestamp
) {
775 while (sample
->timestamp
<= timestamp
) {
776 p
= sample
->list
.next
;
777 if (p
== &os
->samples
) {
778 list_add_tail(&new->list
, &os
->samples
);
779 os
->max_timestamp
= timestamp
;
782 sample
= list_entry(p
, struct sample_queue
, list
);
784 list_add_tail(&new->list
, &sample
->list
);
786 while (sample
->timestamp
> timestamp
) {
787 p
= sample
->list
.prev
;
788 if (p
== &os
->samples
) {
789 list_add(&new->list
, &os
->samples
);
792 sample
= list_entry(p
, struct sample_queue
, list
);
794 list_add(&new->list
, &sample
->list
);
798 #define MAX_SAMPLE_BUFFER (64 * 1024 / sizeof(struct sample_queue))
800 static int perf_session_queue_event(struct perf_session
*s
, union perf_event
*event
,
801 struct perf_sample
*sample
, u64 file_offset
)
803 struct ordered_samples
*os
= &s
->ordered_samples
;
804 struct list_head
*sc
= &os
->sample_cache
;
805 u64 timestamp
= sample
->time
;
806 struct sample_queue
*new;
808 if (!timestamp
|| timestamp
== ~0ULL)
811 if (timestamp
< s
->ordered_samples
.last_flush
) {
812 printf("Warning: Timestamp below last timeslice flush\n");
816 if (!list_empty(sc
)) {
817 new = list_entry(sc
->next
, struct sample_queue
, list
);
818 list_del(&new->list
);
819 } else if (os
->sample_buffer
) {
820 new = os
->sample_buffer
+ os
->sample_buffer_idx
;
821 if (++os
->sample_buffer_idx
== MAX_SAMPLE_BUFFER
)
822 os
->sample_buffer
= NULL
;
824 os
->sample_buffer
= malloc(MAX_SAMPLE_BUFFER
* sizeof(*new));
825 if (!os
->sample_buffer
)
827 list_add(&os
->sample_buffer
->list
, &os
->to_free
);
828 os
->sample_buffer_idx
= 2;
829 new = os
->sample_buffer
+ 1;
832 new->timestamp
= timestamp
;
833 new->file_offset
= file_offset
;
836 __queue_event(new, s
);
841 static void callchain__printf(struct perf_sample
*sample
)
845 printf("... chain: nr:%" PRIu64
"\n", sample
->callchain
->nr
);
847 for (i
= 0; i
< sample
->callchain
->nr
; i
++)
848 printf("..... %2d: %016" PRIx64
"\n",
849 i
, sample
->callchain
->ips
[i
]);
852 static void branch_stack__printf(struct perf_sample
*sample
)
856 printf("... branch stack: nr:%" PRIu64
"\n", sample
->branch_stack
->nr
);
858 for (i
= 0; i
< sample
->branch_stack
->nr
; i
++)
859 printf("..... %2"PRIu64
": %016" PRIx64
" -> %016" PRIx64
"\n",
860 i
, sample
->branch_stack
->entries
[i
].from
,
861 sample
->branch_stack
->entries
[i
].to
);
864 static void perf_session__print_tstamp(struct perf_session
*session
,
865 union perf_event
*event
,
866 struct perf_sample
*sample
)
868 if (event
->header
.type
!= PERF_RECORD_SAMPLE
&&
869 !session
->sample_id_all
) {
870 fputs("-1 -1 ", stdout
);
874 if ((session
->sample_type
& PERF_SAMPLE_CPU
))
875 printf("%u ", sample
->cpu
);
877 if (session
->sample_type
& PERF_SAMPLE_TIME
)
878 printf("%" PRIu64
" ", sample
->time
);
881 static void dump_event(struct perf_session
*session
, union perf_event
*event
,
882 u64 file_offset
, struct perf_sample
*sample
)
887 printf("\n%#" PRIx64
" [%#x]: event: %d\n",
888 file_offset
, event
->header
.size
, event
->header
.type
);
893 perf_session__print_tstamp(session
, event
, sample
);
895 printf("%#" PRIx64
" [%#x]: PERF_RECORD_%s", file_offset
,
896 event
->header
.size
, perf_event__name(event
->header
.type
));
899 static void dump_sample(struct perf_session
*session
, union perf_event
*event
,
900 struct perf_sample
*sample
)
905 printf("(IP, %d): %d/%d: %#" PRIx64
" period: %" PRIu64
" addr: %#" PRIx64
"\n",
906 event
->header
.misc
, sample
->pid
, sample
->tid
, sample
->ip
,
907 sample
->period
, sample
->addr
);
909 if (session
->sample_type
& PERF_SAMPLE_CALLCHAIN
)
910 callchain__printf(sample
);
912 if (session
->sample_type
& PERF_SAMPLE_BRANCH_STACK
)
913 branch_stack__printf(sample
);
916 static struct machine
*
917 perf_session__find_machine_for_cpumode(struct perf_session
*session
,
918 union perf_event
*event
)
920 const u8 cpumode
= event
->header
.misc
& PERF_RECORD_MISC_CPUMODE_MASK
;
923 ((cpumode
== PERF_RECORD_MISC_GUEST_KERNEL
) ||
924 (cpumode
== PERF_RECORD_MISC_GUEST_USER
))) {
927 if (event
->header
.type
== PERF_RECORD_MMAP
)
928 pid
= event
->mmap
.pid
;
932 return perf_session__findnew_machine(session
, pid
);
935 return perf_session__find_host_machine(session
);
938 static int perf_session_deliver_event(struct perf_session
*session
,
939 union perf_event
*event
,
940 struct perf_sample
*sample
,
941 struct perf_tool
*tool
,
944 struct perf_evsel
*evsel
;
945 struct machine
*machine
;
947 dump_event(session
, event
, file_offset
, sample
);
949 evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
950 if (evsel
!= NULL
&& event
->header
.type
!= PERF_RECORD_SAMPLE
) {
952 * XXX We're leaving PERF_RECORD_SAMPLE unnacounted here
953 * because the tools right now may apply filters, discarding
954 * some of the samples. For consistency, in the future we
955 * should have something like nr_filtered_samples and remove
956 * the sample->period from total_sample_period, etc, KISS for
959 * Also testing against NULL allows us to handle files without
960 * attr.sample_id_all and/or without PERF_SAMPLE_ID. In the
961 * future probably it'll be a good idea to restrict event
962 * processing via perf_session to files with both set.
964 hists__inc_nr_events(&evsel
->hists
, event
->header
.type
);
967 machine
= perf_session__find_machine_for_cpumode(session
, event
);
969 switch (event
->header
.type
) {
970 case PERF_RECORD_SAMPLE
:
971 dump_sample(session
, event
, sample
);
973 ++session
->hists
.stats
.nr_unknown_id
;
976 if (machine
== NULL
) {
977 ++session
->hists
.stats
.nr_unprocessable_samples
;
980 return tool
->sample(tool
, event
, sample
, evsel
, machine
);
981 case PERF_RECORD_MMAP
:
982 return tool
->mmap(tool
, event
, sample
, machine
);
983 case PERF_RECORD_COMM
:
984 return tool
->comm(tool
, event
, sample
, machine
);
985 case PERF_RECORD_FORK
:
986 return tool
->fork(tool
, event
, sample
, machine
);
987 case PERF_RECORD_EXIT
:
988 return tool
->exit(tool
, event
, sample
, machine
);
989 case PERF_RECORD_LOST
:
990 if (tool
->lost
== perf_event__process_lost
)
991 session
->hists
.stats
.total_lost
+= event
->lost
.lost
;
992 return tool
->lost(tool
, event
, sample
, machine
);
993 case PERF_RECORD_READ
:
994 return tool
->read(tool
, event
, sample
, evsel
, machine
);
995 case PERF_RECORD_THROTTLE
:
996 return tool
->throttle(tool
, event
, sample
, machine
);
997 case PERF_RECORD_UNTHROTTLE
:
998 return tool
->unthrottle(tool
, event
, sample
, machine
);
1000 ++session
->hists
.stats
.nr_unknown_events
;
1005 static int perf_session__preprocess_sample(struct perf_session
*session
,
1006 union perf_event
*event
, struct perf_sample
*sample
)
1008 if (event
->header
.type
!= PERF_RECORD_SAMPLE
||
1009 !(session
->sample_type
& PERF_SAMPLE_CALLCHAIN
))
1012 if (!ip_callchain__valid(sample
->callchain
, event
)) {
1013 pr_debug("call-chain problem with event, skipping it.\n");
1014 ++session
->hists
.stats
.nr_invalid_chains
;
1015 session
->hists
.stats
.total_invalid_chains
+= sample
->period
;
1021 static int perf_session__process_user_event(struct perf_session
*session
, union perf_event
*event
,
1022 struct perf_tool
*tool
, u64 file_offset
)
1026 dump_event(session
, event
, file_offset
, NULL
);
1028 /* These events are processed right away */
1029 switch (event
->header
.type
) {
1030 case PERF_RECORD_HEADER_ATTR
:
1031 err
= tool
->attr(event
, &session
->evlist
);
1033 perf_session__update_sample_type(session
);
1035 case PERF_RECORD_HEADER_EVENT_TYPE
:
1036 return tool
->event_type(tool
, event
);
1037 case PERF_RECORD_HEADER_TRACING_DATA
:
1038 /* setup for reading amidst mmap */
1039 lseek(session
->fd
, file_offset
, SEEK_SET
);
1040 return tool
->tracing_data(event
, session
);
1041 case PERF_RECORD_HEADER_BUILD_ID
:
1042 return tool
->build_id(tool
, event
, session
);
1043 case PERF_RECORD_FINISHED_ROUND
:
1044 return tool
->finished_round(tool
, event
, session
);
1050 static void event_swap(union perf_event
*event
, bool sample_id_all
)
1052 perf_event__swap_op swap
;
1054 swap
= perf_event__swap_ops
[event
->header
.type
];
1056 swap(event
, sample_id_all
);
1059 static int perf_session__process_event(struct perf_session
*session
,
1060 union perf_event
*event
,
1061 struct perf_tool
*tool
,
1064 struct perf_sample sample
;
1067 if (session
->header
.needs_swap
)
1068 event_swap(event
, session
->sample_id_all
);
1070 if (event
->header
.type
>= PERF_RECORD_HEADER_MAX
)
1073 hists__inc_nr_events(&session
->hists
, event
->header
.type
);
1075 if (event
->header
.type
>= PERF_RECORD_USER_TYPE_START
)
1076 return perf_session__process_user_event(session
, event
, tool
, file_offset
);
1079 * For all kernel events we get the sample data
1081 ret
= perf_session__parse_sample(session
, event
, &sample
);
1085 /* Preprocess sample records - precheck callchains */
1086 if (perf_session__preprocess_sample(session
, event
, &sample
))
1089 if (tool
->ordered_samples
) {
1090 ret
= perf_session_queue_event(session
, event
, &sample
,
1096 return perf_session_deliver_event(session
, event
, &sample
, tool
,
1100 void perf_event_header__bswap(struct perf_event_header
*self
)
1102 self
->type
= bswap_32(self
->type
);
1103 self
->misc
= bswap_16(self
->misc
);
1104 self
->size
= bswap_16(self
->size
);
1107 struct thread
*perf_session__findnew(struct perf_session
*session
, pid_t pid
)
1109 return machine__findnew_thread(&session
->host_machine
, pid
);
1112 static struct thread
*perf_session__register_idle_thread(struct perf_session
*self
)
1114 struct thread
*thread
= perf_session__findnew(self
, 0);
1116 if (thread
== NULL
|| thread__set_comm(thread
, "swapper")) {
1117 pr_err("problem inserting idle task.\n");
1124 static void perf_session__warn_about_errors(const struct perf_session
*session
,
1125 const struct perf_tool
*tool
)
1127 if (tool
->lost
== perf_event__process_lost
&&
1128 session
->hists
.stats
.nr_events
[PERF_RECORD_LOST
] != 0) {
1129 ui__warning("Processed %d events and lost %d chunks!\n\n"
1130 "Check IO/CPU overload!\n\n",
1131 session
->hists
.stats
.nr_events
[0],
1132 session
->hists
.stats
.nr_events
[PERF_RECORD_LOST
]);
1135 if (session
->hists
.stats
.nr_unknown_events
!= 0) {
1136 ui__warning("Found %u unknown events!\n\n"
1137 "Is this an older tool processing a perf.data "
1138 "file generated by a more recent tool?\n\n"
1139 "If that is not the case, consider "
1140 "reporting to linux-kernel@vger.kernel.org.\n\n",
1141 session
->hists
.stats
.nr_unknown_events
);
1144 if (session
->hists
.stats
.nr_unknown_id
!= 0) {
1145 ui__warning("%u samples with id not present in the header\n",
1146 session
->hists
.stats
.nr_unknown_id
);
1149 if (session
->hists
.stats
.nr_invalid_chains
!= 0) {
1150 ui__warning("Found invalid callchains!\n\n"
1151 "%u out of %u events were discarded for this reason.\n\n"
1152 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
1153 session
->hists
.stats
.nr_invalid_chains
,
1154 session
->hists
.stats
.nr_events
[PERF_RECORD_SAMPLE
]);
1157 if (session
->hists
.stats
.nr_unprocessable_samples
!= 0) {
1158 ui__warning("%u unprocessable samples recorded.\n"
1159 "Do you have a KVM guest running and not using 'perf kvm'?\n",
1160 session
->hists
.stats
.nr_unprocessable_samples
);
1164 #define session_done() (*(volatile int *)(&session_done))
1165 volatile int session_done
;
1167 static int __perf_session__process_pipe_events(struct perf_session
*self
,
1168 struct perf_tool
*tool
)
1170 union perf_event
*event
;
1171 uint32_t size
, cur_size
= 0;
1178 perf_tool__fill_defaults(tool
);
1181 cur_size
= sizeof(union perf_event
);
1183 buf
= malloc(cur_size
);
1188 err
= readn(self
->fd
, event
, sizeof(struct perf_event_header
));
1193 pr_err("failed to read event header\n");
1197 if (self
->header
.needs_swap
)
1198 perf_event_header__bswap(&event
->header
);
1200 size
= event
->header
.size
;
1204 if (size
> cur_size
) {
1205 void *new = realloc(buf
, size
);
1207 pr_err("failed to allocate memory to read event\n");
1215 p
+= sizeof(struct perf_event_header
);
1217 if (size
- sizeof(struct perf_event_header
)) {
1218 err
= readn(self
->fd
, p
, size
- sizeof(struct perf_event_header
));
1221 pr_err("unexpected end of event stream\n");
1225 pr_err("failed to read event data\n");
1230 if ((skip
= perf_session__process_event(self
, event
, tool
, head
)) < 0) {
1231 pr_err("%#" PRIx64
" [%#x]: failed to process type: %d\n",
1232 head
, event
->header
.size
, event
->header
.type
);
1242 if (!session_done())
1248 perf_session__warn_about_errors(self
, tool
);
1249 perf_session_free_sample_buffers(self
);
1253 static union perf_event
*
1254 fetch_mmaped_event(struct perf_session
*session
,
1255 u64 head
, size_t mmap_size
, char *buf
)
1257 union perf_event
*event
;
1260 * Ensure we have enough space remaining to read
1261 * the size of the event in the headers.
1263 if (head
+ sizeof(event
->header
) > mmap_size
)
1266 event
= (union perf_event
*)(buf
+ head
);
1268 if (session
->header
.needs_swap
)
1269 perf_event_header__bswap(&event
->header
);
1271 if (head
+ event
->header
.size
> mmap_size
)
1277 int __perf_session__process_events(struct perf_session
*session
,
1278 u64 data_offset
, u64 data_size
,
1279 u64 file_size
, struct perf_tool
*tool
)
1281 u64 head
, page_offset
, file_offset
, file_pos
, progress_next
;
1282 int err
, mmap_prot
, mmap_flags
, map_idx
= 0;
1283 size_t page_size
, mmap_size
;
1284 char *buf
, *mmaps
[8];
1285 union perf_event
*event
;
1288 perf_tool__fill_defaults(tool
);
1290 page_size
= sysconf(_SC_PAGESIZE
);
1292 page_offset
= page_size
* (data_offset
/ page_size
);
1293 file_offset
= page_offset
;
1294 head
= data_offset
- page_offset
;
1296 if (data_offset
+ data_size
< file_size
)
1297 file_size
= data_offset
+ data_size
;
1299 progress_next
= file_size
/ 16;
1301 mmap_size
= session
->mmap_window
;
1302 if (mmap_size
> file_size
)
1303 mmap_size
= file_size
;
1305 memset(mmaps
, 0, sizeof(mmaps
));
1307 mmap_prot
= PROT_READ
;
1308 mmap_flags
= MAP_SHARED
;
1310 if (session
->header
.needs_swap
) {
1311 mmap_prot
|= PROT_WRITE
;
1312 mmap_flags
= MAP_PRIVATE
;
1315 buf
= mmap(NULL
, mmap_size
, mmap_prot
, mmap_flags
, session
->fd
,
1317 if (buf
== MAP_FAILED
) {
1318 pr_err("failed to mmap file\n");
1322 mmaps
[map_idx
] = buf
;
1323 map_idx
= (map_idx
+ 1) & (ARRAY_SIZE(mmaps
) - 1);
1324 file_pos
= file_offset
+ head
;
1327 event
= fetch_mmaped_event(session
, head
, mmap_size
, buf
);
1329 if (mmaps
[map_idx
]) {
1330 munmap(mmaps
[map_idx
], mmap_size
);
1331 mmaps
[map_idx
] = NULL
;
1334 page_offset
= page_size
* (head
/ page_size
);
1335 file_offset
+= page_offset
;
1336 head
-= page_offset
;
1340 size
= event
->header
.size
;
1343 perf_session__process_event(session
, event
, tool
, file_pos
) < 0) {
1344 pr_err("%#" PRIx64
" [%#x]: failed to process type: %d\n",
1345 file_offset
+ head
, event
->header
.size
,
1346 event
->header
.type
);
1354 if (file_pos
>= progress_next
) {
1355 progress_next
+= file_size
/ 16;
1356 ui_progress__update(file_pos
, file_size
,
1357 "Processing events...");
1360 if (file_pos
< file_size
)
1364 /* do the final flush for ordered samples */
1365 session
->ordered_samples
.next_flush
= ULLONG_MAX
;
1366 flush_sample_queue(session
, tool
);
1368 perf_session__warn_about_errors(session
, tool
);
1369 perf_session_free_sample_buffers(session
);
1373 int perf_session__process_events(struct perf_session
*self
,
1374 struct perf_tool
*tool
)
1378 if (perf_session__register_idle_thread(self
) == NULL
)
1382 err
= __perf_session__process_events(self
,
1383 self
->header
.data_offset
,
1384 self
->header
.data_size
,
1387 err
= __perf_session__process_pipe_events(self
, tool
);
1392 bool perf_session__has_traces(struct perf_session
*self
, const char *msg
)
1394 if (!(self
->sample_type
& PERF_SAMPLE_RAW
)) {
1395 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg
);
1402 int maps__set_kallsyms_ref_reloc_sym(struct map
**maps
,
1403 const char *symbol_name
, u64 addr
)
1407 struct ref_reloc_sym
*ref
;
1409 ref
= zalloc(sizeof(struct ref_reloc_sym
));
1413 ref
->name
= strdup(symbol_name
);
1414 if (ref
->name
== NULL
) {
1419 bracket
= strchr(ref
->name
, ']');
1425 for (i
= 0; i
< MAP__NR_TYPES
; ++i
) {
1426 struct kmap
*kmap
= map__kmap(maps
[i
]);
1427 kmap
->ref_reloc_sym
= ref
;
1433 size_t perf_session__fprintf_dsos(struct perf_session
*self
, FILE *fp
)
1435 return __dsos__fprintf(&self
->host_machine
.kernel_dsos
, fp
) +
1436 __dsos__fprintf(&self
->host_machine
.user_dsos
, fp
) +
1437 machines__fprintf_dsos(&self
->machines
, fp
);
1440 size_t perf_session__fprintf_dsos_buildid(struct perf_session
*self
, FILE *fp
,
1443 size_t ret
= machine__fprintf_dsos_buildid(&self
->host_machine
, fp
, with_hits
);
1444 return ret
+ machines__fprintf_dsos_buildid(&self
->machines
, fp
, with_hits
);
1447 size_t perf_session__fprintf_nr_events(struct perf_session
*session
, FILE *fp
)
1449 struct perf_evsel
*pos
;
1450 size_t ret
= fprintf(fp
, "Aggregated stats:\n");
1452 ret
+= hists__fprintf_nr_events(&session
->hists
, fp
);
1454 list_for_each_entry(pos
, &session
->evlist
->entries
, node
) {
1455 ret
+= fprintf(fp
, "%s stats:\n", perf_evsel__name(pos
));
1456 ret
+= hists__fprintf_nr_events(&pos
->hists
, fp
);
1462 size_t perf_session__fprintf(struct perf_session
*session
, FILE *fp
)
1465 * FIXME: Here we have to actually print all the machines in this
1466 * session, not just the host...
1468 return machine__fprintf(&session
->host_machine
, fp
);
1471 void perf_session__remove_thread(struct perf_session
*session
,
1475 * FIXME: This one makes no sense, we need to remove the thread from
1476 * the machine it belongs to, perf_session can have many machines, so
1477 * doing it always on ->host_machine is wrong. Fix when auditing all
1478 * the 'perf kvm' code.
1480 machine__remove_thread(&session
->host_machine
, th
);
1483 struct perf_evsel
*perf_session__find_first_evtype(struct perf_session
*session
,
1486 struct perf_evsel
*pos
;
1488 list_for_each_entry(pos
, &session
->evlist
->entries
, node
) {
1489 if (pos
->attr
.type
== type
)
1495 void perf_event__print_ip(union perf_event
*event
, struct perf_sample
*sample
,
1496 struct machine
*machine
, int print_sym
,
1497 int print_dso
, int print_symoffset
)
1499 struct addr_location al
;
1500 struct callchain_cursor_node
*node
;
1502 if (perf_event__preprocess_sample(event
, machine
, &al
, sample
,
1504 error("problem processing %d event, skipping it.\n",
1505 event
->header
.type
);
1509 if (symbol_conf
.use_callchain
&& sample
->callchain
) {
1511 if (machine__resolve_callchain(machine
, al
.thread
,
1512 sample
->callchain
, NULL
) != 0) {
1514 error("Failed to resolve callchain. Skipping\n");
1517 callchain_cursor_commit(&callchain_cursor
);
1520 node
= callchain_cursor_current(&callchain_cursor
);
1524 printf("\t%16" PRIx64
, node
->ip
);
1527 symbol__fprintf_symname(node
->sym
, stdout
);
1531 map__fprintf_dsoname(node
->map
, stdout
);
1536 callchain_cursor_advance(&callchain_cursor
);
1540 printf("%16" PRIx64
, sample
->ip
);
1543 if (print_symoffset
)
1544 symbol__fprintf_symname_offs(al
.sym
, &al
,
1547 symbol__fprintf_symname(al
.sym
, stdout
);
1552 map__fprintf_dsoname(al
.map
, stdout
);
1558 int perf_session__cpu_bitmap(struct perf_session
*session
,
1559 const char *cpu_list
, unsigned long *cpu_bitmap
)
1562 struct cpu_map
*map
;
1564 for (i
= 0; i
< PERF_TYPE_MAX
; ++i
) {
1565 struct perf_evsel
*evsel
;
1567 evsel
= perf_session__find_first_evtype(session
, i
);
1571 if (!(evsel
->attr
.sample_type
& PERF_SAMPLE_CPU
)) {
1572 pr_err("File does not contain CPU events. "
1573 "Remove -c option to proceed.\n");
1578 map
= cpu_map__new(cpu_list
);
1580 pr_err("Invalid cpu_list\n");
1584 for (i
= 0; i
< map
->nr
; i
++) {
1585 int cpu
= map
->map
[i
];
1587 if (cpu
>= MAX_NR_CPUS
) {
1588 pr_err("Requested CPU %d too large. "
1589 "Consider raising MAX_NR_CPUS\n", cpu
);
1593 set_bit(cpu
, cpu_bitmap
);
1599 void perf_session__fprintf_info(struct perf_session
*session
, FILE *fp
,
1605 if (session
== NULL
|| fp
== NULL
)
1608 ret
= fstat(session
->fd
, &st
);
1612 fprintf(fp
, "# ========\n");
1613 fprintf(fp
, "# captured on: %s", ctime(&st
.st_ctime
));
1614 perf_header__fprintf_info(session
, fp
, full
);
1615 fprintf(fp
, "# ========\n#\n");
1619 int __perf_session__set_tracepoints_handlers(struct perf_session
*session
,
1620 const struct perf_evsel_str_handler
*assocs
,
1623 struct perf_evlist
*evlist
= session
->evlist
;
1624 struct event_format
*format
;
1625 struct perf_evsel
*evsel
;
1626 char *tracepoint
, *name
;
1630 for (i
= 0; i
< nr_assocs
; i
++) {
1632 tracepoint
= strdup(assocs
[i
].name
);
1633 if (tracepoint
== NULL
)
1637 name
= strchr(tracepoint
, ':');
1642 format
= pevent_find_event_by_name(session
->pevent
,
1644 if (format
== NULL
) {
1646 * Adding a handler for an event not in the session,
1652 evsel
= perf_evlist__find_tracepoint_by_id(evlist
, format
->id
);
1657 if (evsel
->handler
.func
!= NULL
)
1659 evsel
->handler
.func
= assocs
[i
].handler
;