1 #define _FILE_OFFSET_BITS 64
3 #include <linux/kernel.h>
18 static int perf_session__open(struct perf_session
*self
, bool force
)
20 struct stat input_stat
;
22 if (!strcmp(self
->filename
, "-")) {
24 self
->fd
= STDIN_FILENO
;
26 if (perf_session__read_header(self
, self
->fd
) < 0)
27 pr_err("incompatible file format (rerun with -v to learn more)");
32 self
->fd
= open(self
->filename
, O_RDONLY
);
36 pr_err("failed to open %s: %s", self
->filename
, strerror(err
));
37 if (err
== ENOENT
&& !strcmp(self
->filename
, "perf.data"))
38 pr_err(" (try 'perf record' first)");
43 if (fstat(self
->fd
, &input_stat
) < 0)
46 if (!force
&& input_stat
.st_uid
&& (input_stat
.st_uid
!= geteuid())) {
47 pr_err("file %s not owned by current user or root\n",
52 if (!input_stat
.st_size
) {
53 pr_info("zero-sized file (%s), nothing to do!\n",
58 if (perf_session__read_header(self
, self
->fd
) < 0) {
59 pr_err("incompatible file format (rerun with -v to learn more)");
63 if (!perf_evlist__valid_sample_type(self
->evlist
)) {
64 pr_err("non matching sample_type");
68 if (!perf_evlist__valid_sample_id_all(self
->evlist
)) {
69 pr_err("non matching sample_id_all");
73 self
->size
= input_stat
.st_size
;
82 void perf_session__update_sample_type(struct perf_session
*self
)
84 self
->sample_type
= perf_evlist__sample_type(self
->evlist
);
85 self
->sample_size
= __perf_evsel__sample_size(self
->sample_type
);
86 self
->sample_id_all
= perf_evlist__sample_id_all(self
->evlist
);
87 self
->id_hdr_size
= perf_evlist__id_hdr_size(self
->evlist
);
88 self
->host_machine
.id_hdr_size
= self
->id_hdr_size
;
91 int perf_session__create_kernel_maps(struct perf_session
*self
)
93 int ret
= machine__create_kernel_maps(&self
->host_machine
);
96 ret
= machines__create_guest_kernel_maps(&self
->machines
);
100 static void perf_session__destroy_kernel_maps(struct perf_session
*self
)
102 machine__destroy_kernel_maps(&self
->host_machine
);
103 machines__destroy_guest_kernel_maps(&self
->machines
);
106 struct perf_session
*perf_session__new(const char *filename
, int mode
,
107 bool force
, bool repipe
,
108 struct perf_tool
*tool
)
110 struct perf_session
*self
;
114 if (!filename
|| !strlen(filename
)) {
115 if (!fstat(STDIN_FILENO
, &st
) && S_ISFIFO(st
.st_mode
))
118 filename
= "perf.data";
121 len
= strlen(filename
);
122 self
= zalloc(sizeof(*self
) + len
);
127 memcpy(self
->filename
, filename
, len
);
129 * On 64bit we can mmap the data file in one go. No need for tiny mmap
130 * slices. On 32bit we use 32MB.
132 #if BITS_PER_LONG == 64
133 self
->mmap_window
= ULLONG_MAX
;
135 self
->mmap_window
= 32 * 1024 * 1024ULL;
137 self
->machines
= RB_ROOT
;
138 self
->repipe
= repipe
;
139 INIT_LIST_HEAD(&self
->ordered_samples
.samples
);
140 INIT_LIST_HEAD(&self
->ordered_samples
.sample_cache
);
141 INIT_LIST_HEAD(&self
->ordered_samples
.to_free
);
142 machine__init(&self
->host_machine
, "", HOST_KERNEL_ID
);
143 hists__init(&self
->hists
);
145 if (mode
== O_RDONLY
) {
146 if (perf_session__open(self
, force
) < 0)
148 perf_session__update_sample_type(self
);
149 } else if (mode
== O_WRONLY
) {
151 * In O_RDONLY mode this will be performed when reading the
152 * kernel MMAP event, in perf_event__process_mmap().
154 if (perf_session__create_kernel_maps(self
) < 0)
158 if (tool
&& tool
->ordering_requires_timestamps
&&
159 tool
->ordered_samples
&& !self
->sample_id_all
) {
160 dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n");
161 tool
->ordered_samples
= false;
167 perf_session__delete(self
);
171 static void machine__delete_dead_threads(struct machine
*machine
)
173 struct thread
*n
, *t
;
175 list_for_each_entry_safe(t
, n
, &machine
->dead_threads
, node
) {
181 static void perf_session__delete_dead_threads(struct perf_session
*session
)
183 machine__delete_dead_threads(&session
->host_machine
);
186 static void machine__delete_threads(struct machine
*self
)
188 struct rb_node
*nd
= rb_first(&self
->threads
);
191 struct thread
*t
= rb_entry(nd
, struct thread
, rb_node
);
193 rb_erase(&t
->rb_node
, &self
->threads
);
199 static void perf_session__delete_threads(struct perf_session
*session
)
201 machine__delete_threads(&session
->host_machine
);
204 void perf_session__delete(struct perf_session
*self
)
206 perf_session__destroy_kernel_maps(self
);
207 perf_session__delete_dead_threads(self
);
208 perf_session__delete_threads(self
);
209 machine__exit(&self
->host_machine
);
214 void machine__remove_thread(struct machine
*self
, struct thread
*th
)
216 self
->last_match
= NULL
;
217 rb_erase(&th
->rb_node
, &self
->threads
);
219 * We may have references to this thread, for instance in some hist_entry
220 * instances, so just move them to a separate list.
222 list_add_tail(&th
->node
, &self
->dead_threads
);
225 static bool symbol__match_parent_regex(struct symbol
*sym
)
227 if (sym
->name
&& !regexec(&parent_regex
, sym
->name
, 0, NULL
, 0))
233 static const u8 cpumodes
[] = {
234 PERF_RECORD_MISC_USER
,
235 PERF_RECORD_MISC_KERNEL
,
236 PERF_RECORD_MISC_GUEST_USER
,
237 PERF_RECORD_MISC_GUEST_KERNEL
239 #define NCPUMODES (sizeof(cpumodes)/sizeof(u8))
241 static void ip__resolve_ams(struct machine
*self
, struct thread
*thread
,
242 struct addr_map_symbol
*ams
,
245 struct addr_location al
;
249 memset(&al
, 0, sizeof(al
));
251 for (i
= 0; i
< NCPUMODES
; i
++) {
254 * We cannot use the header.misc hint to determine whether a
255 * branch stack address is user, kernel, guest, hypervisor.
256 * Branches may straddle the kernel/user/hypervisor boundaries.
257 * Thus, we have to try consecutively until we find a match
258 * or else, the symbol is unknown
260 thread__find_addr_location(thread
, self
, m
, MAP__FUNCTION
,
267 ams
->al_addr
= al
.addr
;
272 struct branch_info
*machine__resolve_bstack(struct machine
*self
,
274 struct branch_stack
*bs
)
276 struct branch_info
*bi
;
279 bi
= calloc(bs
->nr
, sizeof(struct branch_info
));
283 for (i
= 0; i
< bs
->nr
; i
++) {
284 ip__resolve_ams(self
, thr
, &bi
[i
].to
, bs
->entries
[i
].to
);
285 ip__resolve_ams(self
, thr
, &bi
[i
].from
, bs
->entries
[i
].from
);
286 bi
[i
].flags
= bs
->entries
[i
].flags
;
291 int machine__resolve_callchain(struct machine
*self
,
292 struct thread
*thread
,
293 struct ip_callchain
*chain
,
294 struct symbol
**parent
)
296 u8 cpumode
= PERF_RECORD_MISC_USER
;
300 callchain_cursor_reset(&callchain_cursor
);
302 if (chain
->nr
> PERF_MAX_STACK_DEPTH
) {
303 pr_warning("corrupted callchain. skipping...\n");
307 for (i
= 0; i
< chain
->nr
; i
++) {
309 struct addr_location al
;
311 if (callchain_param
.order
== ORDER_CALLEE
)
314 ip
= chain
->ips
[chain
->nr
- i
- 1];
316 if (ip
>= PERF_CONTEXT_MAX
) {
318 case PERF_CONTEXT_HV
:
319 cpumode
= PERF_RECORD_MISC_HYPERVISOR
; break;
320 case PERF_CONTEXT_KERNEL
:
321 cpumode
= PERF_RECORD_MISC_KERNEL
; break;
322 case PERF_CONTEXT_USER
:
323 cpumode
= PERF_RECORD_MISC_USER
; break;
325 pr_debug("invalid callchain context: "
326 "%"PRId64
"\n", (s64
) ip
);
328 * It seems the callchain is corrupted.
331 callchain_cursor_reset(&callchain_cursor
);
338 thread__find_addr_location(thread
, self
, cpumode
,
339 MAP__FUNCTION
, ip
, &al
, NULL
);
340 if (al
.sym
!= NULL
) {
341 if (sort__has_parent
&& !*parent
&&
342 symbol__match_parent_regex(al
.sym
))
344 if (!symbol_conf
.use_callchain
)
348 err
= callchain_cursor_append(&callchain_cursor
,
357 static int process_event_synth_tracing_data_stub(union perf_event
*event __used
,
358 struct perf_session
*session __used
)
360 dump_printf(": unhandled!\n");
364 static int process_event_synth_attr_stub(union perf_event
*event __used
,
365 struct perf_evlist
**pevlist __used
)
367 dump_printf(": unhandled!\n");
371 static int process_event_sample_stub(struct perf_tool
*tool __used
,
372 union perf_event
*event __used
,
373 struct perf_sample
*sample __used
,
374 struct perf_evsel
*evsel __used
,
375 struct machine
*machine __used
)
377 dump_printf(": unhandled!\n");
381 static int process_event_stub(struct perf_tool
*tool __used
,
382 union perf_event
*event __used
,
383 struct perf_sample
*sample __used
,
384 struct machine
*machine __used
)
386 dump_printf(": unhandled!\n");
390 static int process_finished_round_stub(struct perf_tool
*tool __used
,
391 union perf_event
*event __used
,
392 struct perf_session
*perf_session __used
)
394 dump_printf(": unhandled!\n");
398 static int process_event_type_stub(struct perf_tool
*tool __used
,
399 union perf_event
*event __used
)
401 dump_printf(": unhandled!\n");
405 static int process_finished_round(struct perf_tool
*tool
,
406 union perf_event
*event
,
407 struct perf_session
*session
);
409 static void perf_tool__fill_defaults(struct perf_tool
*tool
)
411 if (tool
->sample
== NULL
)
412 tool
->sample
= process_event_sample_stub
;
413 if (tool
->mmap
== NULL
)
414 tool
->mmap
= process_event_stub
;
415 if (tool
->comm
== NULL
)
416 tool
->comm
= process_event_stub
;
417 if (tool
->fork
== NULL
)
418 tool
->fork
= process_event_stub
;
419 if (tool
->exit
== NULL
)
420 tool
->exit
= process_event_stub
;
421 if (tool
->lost
== NULL
)
422 tool
->lost
= perf_event__process_lost
;
423 if (tool
->read
== NULL
)
424 tool
->read
= process_event_sample_stub
;
425 if (tool
->throttle
== NULL
)
426 tool
->throttle
= process_event_stub
;
427 if (tool
->unthrottle
== NULL
)
428 tool
->unthrottle
= process_event_stub
;
429 if (tool
->attr
== NULL
)
430 tool
->attr
= process_event_synth_attr_stub
;
431 if (tool
->event_type
== NULL
)
432 tool
->event_type
= process_event_type_stub
;
433 if (tool
->tracing_data
== NULL
)
434 tool
->tracing_data
= process_event_synth_tracing_data_stub
;
435 if (tool
->build_id
== NULL
)
436 tool
->build_id
= process_finished_round_stub
;
437 if (tool
->finished_round
== NULL
) {
438 if (tool
->ordered_samples
)
439 tool
->finished_round
= process_finished_round
;
441 tool
->finished_round
= process_finished_round_stub
;
445 void mem_bswap_32(void *src
, int byte_size
)
448 while (byte_size
> 0) {
450 byte_size
-= sizeof(u32
);
455 void mem_bswap_64(void *src
, int byte_size
)
459 while (byte_size
> 0) {
461 byte_size
-= sizeof(u64
);
466 static void swap_sample_id_all(union perf_event
*event
, void *data
)
468 void *end
= (void *) event
+ event
->header
.size
;
469 int size
= end
- data
;
471 BUG_ON(size
% sizeof(u64
));
472 mem_bswap_64(data
, size
);
475 static void perf_event__all64_swap(union perf_event
*event
,
476 bool sample_id_all __used
)
478 struct perf_event_header
*hdr
= &event
->header
;
479 mem_bswap_64(hdr
+ 1, event
->header
.size
- sizeof(*hdr
));
482 static void perf_event__comm_swap(union perf_event
*event
, bool sample_id_all
)
484 event
->comm
.pid
= bswap_32(event
->comm
.pid
);
485 event
->comm
.tid
= bswap_32(event
->comm
.tid
);
488 void *data
= &event
->comm
.comm
;
490 data
+= ALIGN(strlen(data
) + 1, sizeof(u64
));
491 swap_sample_id_all(event
, data
);
495 static void perf_event__mmap_swap(union perf_event
*event
,
498 event
->mmap
.pid
= bswap_32(event
->mmap
.pid
);
499 event
->mmap
.tid
= bswap_32(event
->mmap
.tid
);
500 event
->mmap
.start
= bswap_64(event
->mmap
.start
);
501 event
->mmap
.len
= bswap_64(event
->mmap
.len
);
502 event
->mmap
.pgoff
= bswap_64(event
->mmap
.pgoff
);
505 void *data
= &event
->mmap
.filename
;
507 data
+= ALIGN(strlen(data
) + 1, sizeof(u64
));
508 swap_sample_id_all(event
, data
);
512 static void perf_event__task_swap(union perf_event
*event
, bool sample_id_all
)
514 event
->fork
.pid
= bswap_32(event
->fork
.pid
);
515 event
->fork
.tid
= bswap_32(event
->fork
.tid
);
516 event
->fork
.ppid
= bswap_32(event
->fork
.ppid
);
517 event
->fork
.ptid
= bswap_32(event
->fork
.ptid
);
518 event
->fork
.time
= bswap_64(event
->fork
.time
);
521 swap_sample_id_all(event
, &event
->fork
+ 1);
524 static void perf_event__read_swap(union perf_event
*event
, bool sample_id_all
)
526 event
->read
.pid
= bswap_32(event
->read
.pid
);
527 event
->read
.tid
= bswap_32(event
->read
.tid
);
528 event
->read
.value
= bswap_64(event
->read
.value
);
529 event
->read
.time_enabled
= bswap_64(event
->read
.time_enabled
);
530 event
->read
.time_running
= bswap_64(event
->read
.time_running
);
531 event
->read
.id
= bswap_64(event
->read
.id
);
534 swap_sample_id_all(event
, &event
->read
+ 1);
537 static u8
revbyte(u8 b
)
539 int rev
= (b
>> 4) | ((b
& 0xf) << 4);
540 rev
= ((rev
& 0xcc) >> 2) | ((rev
& 0x33) << 2);
541 rev
= ((rev
& 0xaa) >> 1) | ((rev
& 0x55) << 1);
546 * XXX this is hack in attempt to carry flags bitfield
547 * throught endian village. ABI says:
549 * Bit-fields are allocated from right to left (least to most significant)
550 * on little-endian implementations and from left to right (most to least
551 * significant) on big-endian implementations.
553 * The above seems to be byte specific, so we need to reverse each
554 * byte of the bitfield. 'Internet' also says this might be implementation
555 * specific and we probably need proper fix and carry perf_event_attr
556 * bitfield flags in separate data file FEAT_ section. Thought this seems
559 static void swap_bitfield(u8
*p
, unsigned len
)
563 for (i
= 0; i
< len
; i
++) {
569 /* exported for swapping attributes in file header */
570 void perf_event__attr_swap(struct perf_event_attr
*attr
)
572 attr
->type
= bswap_32(attr
->type
);
573 attr
->size
= bswap_32(attr
->size
);
574 attr
->config
= bswap_64(attr
->config
);
575 attr
->sample_period
= bswap_64(attr
->sample_period
);
576 attr
->sample_type
= bswap_64(attr
->sample_type
);
577 attr
->read_format
= bswap_64(attr
->read_format
);
578 attr
->wakeup_events
= bswap_32(attr
->wakeup_events
);
579 attr
->bp_type
= bswap_32(attr
->bp_type
);
580 attr
->bp_addr
= bswap_64(attr
->bp_addr
);
581 attr
->bp_len
= bswap_64(attr
->bp_len
);
583 swap_bitfield((u8
*) (&attr
->read_format
+ 1), sizeof(u64
));
586 static void perf_event__hdr_attr_swap(union perf_event
*event
,
587 bool sample_id_all __used
)
591 perf_event__attr_swap(&event
->attr
.attr
);
593 size
= event
->header
.size
;
594 size
-= (void *)&event
->attr
.id
- (void *)event
;
595 mem_bswap_64(event
->attr
.id
, size
);
598 static void perf_event__event_type_swap(union perf_event
*event
,
599 bool sample_id_all __used
)
601 event
->event_type
.event_type
.event_id
=
602 bswap_64(event
->event_type
.event_type
.event_id
);
605 static void perf_event__tracing_data_swap(union perf_event
*event
,
606 bool sample_id_all __used
)
608 event
->tracing_data
.size
= bswap_32(event
->tracing_data
.size
);
611 typedef void (*perf_event__swap_op
)(union perf_event
*event
,
614 static perf_event__swap_op perf_event__swap_ops
[] = {
615 [PERF_RECORD_MMAP
] = perf_event__mmap_swap
,
616 [PERF_RECORD_COMM
] = perf_event__comm_swap
,
617 [PERF_RECORD_FORK
] = perf_event__task_swap
,
618 [PERF_RECORD_EXIT
] = perf_event__task_swap
,
619 [PERF_RECORD_LOST
] = perf_event__all64_swap
,
620 [PERF_RECORD_READ
] = perf_event__read_swap
,
621 [PERF_RECORD_SAMPLE
] = perf_event__all64_swap
,
622 [PERF_RECORD_HEADER_ATTR
] = perf_event__hdr_attr_swap
,
623 [PERF_RECORD_HEADER_EVENT_TYPE
] = perf_event__event_type_swap
,
624 [PERF_RECORD_HEADER_TRACING_DATA
] = perf_event__tracing_data_swap
,
625 [PERF_RECORD_HEADER_BUILD_ID
] = NULL
,
626 [PERF_RECORD_HEADER_MAX
] = NULL
,
629 struct sample_queue
{
632 union perf_event
*event
;
633 struct list_head list
;
636 static void perf_session_free_sample_buffers(struct perf_session
*session
)
638 struct ordered_samples
*os
= &session
->ordered_samples
;
640 while (!list_empty(&os
->to_free
)) {
641 struct sample_queue
*sq
;
643 sq
= list_entry(os
->to_free
.next
, struct sample_queue
, list
);
649 static int perf_session_deliver_event(struct perf_session
*session
,
650 union perf_event
*event
,
651 struct perf_sample
*sample
,
652 struct perf_tool
*tool
,
655 static void flush_sample_queue(struct perf_session
*s
,
656 struct perf_tool
*tool
)
658 struct ordered_samples
*os
= &s
->ordered_samples
;
659 struct list_head
*head
= &os
->samples
;
660 struct sample_queue
*tmp
, *iter
;
661 struct perf_sample sample
;
662 u64 limit
= os
->next_flush
;
663 u64 last_ts
= os
->last_sample
? os
->last_sample
->timestamp
: 0ULL;
664 unsigned idx
= 0, progress_next
= os
->nr_samples
/ 16;
667 if (!tool
->ordered_samples
|| !limit
)
670 list_for_each_entry_safe(iter
, tmp
, head
, list
) {
671 if (iter
->timestamp
> limit
)
674 ret
= perf_session__parse_sample(s
, iter
->event
, &sample
);
676 pr_err("Can't parse sample, err = %d\n", ret
);
678 perf_session_deliver_event(s
, iter
->event
, &sample
, tool
,
681 os
->last_flush
= iter
->timestamp
;
682 list_del(&iter
->list
);
683 list_add(&iter
->list
, &os
->sample_cache
);
684 if (++idx
>= progress_next
) {
685 progress_next
+= os
->nr_samples
/ 16;
686 ui_progress__update(idx
, os
->nr_samples
,
687 "Processing time ordered events...");
691 if (list_empty(head
)) {
692 os
->last_sample
= NULL
;
693 } else if (last_ts
<= limit
) {
695 list_entry(head
->prev
, struct sample_queue
, list
);
702 * When perf record finishes a pass on every buffers, it records this pseudo
704 * We record the max timestamp t found in the pass n.
705 * Assuming these timestamps are monotonic across cpus, we know that if
706 * a buffer still has events with timestamps below t, they will be all
707 * available and then read in the pass n + 1.
708 * Hence when we start to read the pass n + 2, we can safely flush every
709 * events with timestamps below t.
711 * ============ PASS n =================
714 * cnt1 timestamps | cnt2 timestamps
717 * - | 4 <--- max recorded
719 * ============ PASS n + 1 ==============
722 * cnt1 timestamps | cnt2 timestamps
725 * 5 | 7 <---- max recorded
727 * Flush every events below timestamp 4
729 * ============ PASS n + 2 ==============
732 * cnt1 timestamps | cnt2 timestamps
737 * Flush every events below timestamp 7
740 static int process_finished_round(struct perf_tool
*tool
,
741 union perf_event
*event __used
,
742 struct perf_session
*session
)
744 flush_sample_queue(session
, tool
);
745 session
->ordered_samples
.next_flush
= session
->ordered_samples
.max_timestamp
;
750 /* The queue is ordered by time */
751 static void __queue_event(struct sample_queue
*new, struct perf_session
*s
)
753 struct ordered_samples
*os
= &s
->ordered_samples
;
754 struct sample_queue
*sample
= os
->last_sample
;
755 u64 timestamp
= new->timestamp
;
759 os
->last_sample
= new;
762 list_add(&new->list
, &os
->samples
);
763 os
->max_timestamp
= timestamp
;
768 * last_sample might point to some random place in the list as it's
769 * the last queued event. We expect that the new event is close to
772 if (sample
->timestamp
<= timestamp
) {
773 while (sample
->timestamp
<= timestamp
) {
774 p
= sample
->list
.next
;
775 if (p
== &os
->samples
) {
776 list_add_tail(&new->list
, &os
->samples
);
777 os
->max_timestamp
= timestamp
;
780 sample
= list_entry(p
, struct sample_queue
, list
);
782 list_add_tail(&new->list
, &sample
->list
);
784 while (sample
->timestamp
> timestamp
) {
785 p
= sample
->list
.prev
;
786 if (p
== &os
->samples
) {
787 list_add(&new->list
, &os
->samples
);
790 sample
= list_entry(p
, struct sample_queue
, list
);
792 list_add(&new->list
, &sample
->list
);
796 #define MAX_SAMPLE_BUFFER (64 * 1024 / sizeof(struct sample_queue))
798 static int perf_session_queue_event(struct perf_session
*s
, union perf_event
*event
,
799 struct perf_sample
*sample
, u64 file_offset
)
801 struct ordered_samples
*os
= &s
->ordered_samples
;
802 struct list_head
*sc
= &os
->sample_cache
;
803 u64 timestamp
= sample
->time
;
804 struct sample_queue
*new;
806 if (!timestamp
|| timestamp
== ~0ULL)
809 if (timestamp
< s
->ordered_samples
.last_flush
) {
810 printf("Warning: Timestamp below last timeslice flush\n");
814 if (!list_empty(sc
)) {
815 new = list_entry(sc
->next
, struct sample_queue
, list
);
816 list_del(&new->list
);
817 } else if (os
->sample_buffer
) {
818 new = os
->sample_buffer
+ os
->sample_buffer_idx
;
819 if (++os
->sample_buffer_idx
== MAX_SAMPLE_BUFFER
)
820 os
->sample_buffer
= NULL
;
822 os
->sample_buffer
= malloc(MAX_SAMPLE_BUFFER
* sizeof(*new));
823 if (!os
->sample_buffer
)
825 list_add(&os
->sample_buffer
->list
, &os
->to_free
);
826 os
->sample_buffer_idx
= 2;
827 new = os
->sample_buffer
+ 1;
830 new->timestamp
= timestamp
;
831 new->file_offset
= file_offset
;
834 __queue_event(new, s
);
839 static void callchain__printf(struct perf_sample
*sample
)
843 printf("... chain: nr:%" PRIu64
"\n", sample
->callchain
->nr
);
845 for (i
= 0; i
< sample
->callchain
->nr
; i
++)
846 printf("..... %2d: %016" PRIx64
"\n",
847 i
, sample
->callchain
->ips
[i
]);
850 static void branch_stack__printf(struct perf_sample
*sample
)
854 printf("... branch stack: nr:%" PRIu64
"\n", sample
->branch_stack
->nr
);
856 for (i
= 0; i
< sample
->branch_stack
->nr
; i
++)
857 printf("..... %2"PRIu64
": %016" PRIx64
" -> %016" PRIx64
"\n",
858 i
, sample
->branch_stack
->entries
[i
].from
,
859 sample
->branch_stack
->entries
[i
].to
);
862 static void perf_session__print_tstamp(struct perf_session
*session
,
863 union perf_event
*event
,
864 struct perf_sample
*sample
)
866 if (event
->header
.type
!= PERF_RECORD_SAMPLE
&&
867 !session
->sample_id_all
) {
868 fputs("-1 -1 ", stdout
);
872 if ((session
->sample_type
& PERF_SAMPLE_CPU
))
873 printf("%u ", sample
->cpu
);
875 if (session
->sample_type
& PERF_SAMPLE_TIME
)
876 printf("%" PRIu64
" ", sample
->time
);
879 static void dump_event(struct perf_session
*session
, union perf_event
*event
,
880 u64 file_offset
, struct perf_sample
*sample
)
885 printf("\n%#" PRIx64
" [%#x]: event: %d\n",
886 file_offset
, event
->header
.size
, event
->header
.type
);
891 perf_session__print_tstamp(session
, event
, sample
);
893 printf("%#" PRIx64
" [%#x]: PERF_RECORD_%s", file_offset
,
894 event
->header
.size
, perf_event__name(event
->header
.type
));
897 static void dump_sample(struct perf_session
*session
, union perf_event
*event
,
898 struct perf_sample
*sample
)
903 printf("(IP, %d): %d/%d: %#" PRIx64
" period: %" PRIu64
" addr: %#" PRIx64
"\n",
904 event
->header
.misc
, sample
->pid
, sample
->tid
, sample
->ip
,
905 sample
->period
, sample
->addr
);
907 if (session
->sample_type
& PERF_SAMPLE_CALLCHAIN
)
908 callchain__printf(sample
);
910 if (session
->sample_type
& PERF_SAMPLE_BRANCH_STACK
)
911 branch_stack__printf(sample
);
914 static struct machine
*
915 perf_session__find_machine_for_cpumode(struct perf_session
*session
,
916 union perf_event
*event
)
918 const u8 cpumode
= event
->header
.misc
& PERF_RECORD_MISC_CPUMODE_MASK
;
920 if (cpumode
== PERF_RECORD_MISC_GUEST_KERNEL
&& perf_guest
) {
923 if (event
->header
.type
== PERF_RECORD_MMAP
)
924 pid
= event
->mmap
.pid
;
928 return perf_session__find_machine(session
, pid
);
931 return perf_session__find_host_machine(session
);
934 static int perf_session_deliver_event(struct perf_session
*session
,
935 union perf_event
*event
,
936 struct perf_sample
*sample
,
937 struct perf_tool
*tool
,
940 struct perf_evsel
*evsel
;
941 struct machine
*machine
;
943 dump_event(session
, event
, file_offset
, sample
);
945 evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
946 if (evsel
!= NULL
&& event
->header
.type
!= PERF_RECORD_SAMPLE
) {
948 * XXX We're leaving PERF_RECORD_SAMPLE unnacounted here
949 * because the tools right now may apply filters, discarding
950 * some of the samples. For consistency, in the future we
951 * should have something like nr_filtered_samples and remove
952 * the sample->period from total_sample_period, etc, KISS for
955 * Also testing against NULL allows us to handle files without
956 * attr.sample_id_all and/or without PERF_SAMPLE_ID. In the
957 * future probably it'll be a good idea to restrict event
958 * processing via perf_session to files with both set.
960 hists__inc_nr_events(&evsel
->hists
, event
->header
.type
);
963 machine
= perf_session__find_machine_for_cpumode(session
, event
);
965 switch (event
->header
.type
) {
966 case PERF_RECORD_SAMPLE
:
967 dump_sample(session
, event
, sample
);
969 ++session
->hists
.stats
.nr_unknown_id
;
972 if (machine
== NULL
) {
973 ++session
->hists
.stats
.nr_unprocessable_samples
;
976 return tool
->sample(tool
, event
, sample
, evsel
, machine
);
977 case PERF_RECORD_MMAP
:
978 return tool
->mmap(tool
, event
, sample
, machine
);
979 case PERF_RECORD_COMM
:
980 return tool
->comm(tool
, event
, sample
, machine
);
981 case PERF_RECORD_FORK
:
982 return tool
->fork(tool
, event
, sample
, machine
);
983 case PERF_RECORD_EXIT
:
984 return tool
->exit(tool
, event
, sample
, machine
);
985 case PERF_RECORD_LOST
:
986 if (tool
->lost
== perf_event__process_lost
)
987 session
->hists
.stats
.total_lost
+= event
->lost
.lost
;
988 return tool
->lost(tool
, event
, sample
, machine
);
989 case PERF_RECORD_READ
:
990 return tool
->read(tool
, event
, sample
, evsel
, machine
);
991 case PERF_RECORD_THROTTLE
:
992 return tool
->throttle(tool
, event
, sample
, machine
);
993 case PERF_RECORD_UNTHROTTLE
:
994 return tool
->unthrottle(tool
, event
, sample
, machine
);
996 ++session
->hists
.stats
.nr_unknown_events
;
1001 static int perf_session__preprocess_sample(struct perf_session
*session
,
1002 union perf_event
*event
, struct perf_sample
*sample
)
1004 if (event
->header
.type
!= PERF_RECORD_SAMPLE
||
1005 !(session
->sample_type
& PERF_SAMPLE_CALLCHAIN
))
1008 if (!ip_callchain__valid(sample
->callchain
, event
)) {
1009 pr_debug("call-chain problem with event, skipping it.\n");
1010 ++session
->hists
.stats
.nr_invalid_chains
;
1011 session
->hists
.stats
.total_invalid_chains
+= sample
->period
;
1017 static int perf_session__process_user_event(struct perf_session
*session
, union perf_event
*event
,
1018 struct perf_tool
*tool
, u64 file_offset
)
1022 dump_event(session
, event
, file_offset
, NULL
);
1024 /* These events are processed right away */
1025 switch (event
->header
.type
) {
1026 case PERF_RECORD_HEADER_ATTR
:
1027 err
= tool
->attr(event
, &session
->evlist
);
1029 perf_session__update_sample_type(session
);
1031 case PERF_RECORD_HEADER_EVENT_TYPE
:
1032 return tool
->event_type(tool
, event
);
1033 case PERF_RECORD_HEADER_TRACING_DATA
:
1034 /* setup for reading amidst mmap */
1035 lseek(session
->fd
, file_offset
, SEEK_SET
);
1036 return tool
->tracing_data(event
, session
);
1037 case PERF_RECORD_HEADER_BUILD_ID
:
1038 return tool
->build_id(tool
, event
, session
);
1039 case PERF_RECORD_FINISHED_ROUND
:
1040 return tool
->finished_round(tool
, event
, session
);
1046 static void event_swap(union perf_event
*event
, bool sample_id_all
)
1048 perf_event__swap_op swap
;
1050 swap
= perf_event__swap_ops
[event
->header
.type
];
1052 swap(event
, sample_id_all
);
1055 static int perf_session__process_event(struct perf_session
*session
,
1056 union perf_event
*event
,
1057 struct perf_tool
*tool
,
1060 struct perf_sample sample
;
1063 if (session
->header
.needs_swap
)
1064 event_swap(event
, session
->sample_id_all
);
1066 if (event
->header
.type
>= PERF_RECORD_HEADER_MAX
)
1069 hists__inc_nr_events(&session
->hists
, event
->header
.type
);
1071 if (event
->header
.type
>= PERF_RECORD_USER_TYPE_START
)
1072 return perf_session__process_user_event(session
, event
, tool
, file_offset
);
1075 * For all kernel events we get the sample data
1077 ret
= perf_session__parse_sample(session
, event
, &sample
);
1081 /* Preprocess sample records - precheck callchains */
1082 if (perf_session__preprocess_sample(session
, event
, &sample
))
1085 if (tool
->ordered_samples
) {
1086 ret
= perf_session_queue_event(session
, event
, &sample
,
1092 return perf_session_deliver_event(session
, event
, &sample
, tool
,
1096 void perf_event_header__bswap(struct perf_event_header
*self
)
1098 self
->type
= bswap_32(self
->type
);
1099 self
->misc
= bswap_16(self
->misc
);
1100 self
->size
= bswap_16(self
->size
);
1103 struct thread
*perf_session__findnew(struct perf_session
*session
, pid_t pid
)
1105 return machine__findnew_thread(&session
->host_machine
, pid
);
1108 static struct thread
*perf_session__register_idle_thread(struct perf_session
*self
)
1110 struct thread
*thread
= perf_session__findnew(self
, 0);
1112 if (thread
== NULL
|| thread__set_comm(thread
, "swapper")) {
1113 pr_err("problem inserting idle task.\n");
1120 static void perf_session__warn_about_errors(const struct perf_session
*session
,
1121 const struct perf_tool
*tool
)
1123 if (tool
->lost
== perf_event__process_lost
&&
1124 session
->hists
.stats
.nr_events
[PERF_RECORD_LOST
] != 0) {
1125 ui__warning("Processed %d events and lost %d chunks!\n\n"
1126 "Check IO/CPU overload!\n\n",
1127 session
->hists
.stats
.nr_events
[0],
1128 session
->hists
.stats
.nr_events
[PERF_RECORD_LOST
]);
1131 if (session
->hists
.stats
.nr_unknown_events
!= 0) {
1132 ui__warning("Found %u unknown events!\n\n"
1133 "Is this an older tool processing a perf.data "
1134 "file generated by a more recent tool?\n\n"
1135 "If that is not the case, consider "
1136 "reporting to linux-kernel@vger.kernel.org.\n\n",
1137 session
->hists
.stats
.nr_unknown_events
);
1140 if (session
->hists
.stats
.nr_unknown_id
!= 0) {
1141 ui__warning("%u samples with id not present in the header\n",
1142 session
->hists
.stats
.nr_unknown_id
);
1145 if (session
->hists
.stats
.nr_invalid_chains
!= 0) {
1146 ui__warning("Found invalid callchains!\n\n"
1147 "%u out of %u events were discarded for this reason.\n\n"
1148 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
1149 session
->hists
.stats
.nr_invalid_chains
,
1150 session
->hists
.stats
.nr_events
[PERF_RECORD_SAMPLE
]);
1153 if (session
->hists
.stats
.nr_unprocessable_samples
!= 0) {
1154 ui__warning("%u unprocessable samples recorded.\n"
1155 "Do you have a KVM guest running and not using 'perf kvm'?\n",
1156 session
->hists
.stats
.nr_unprocessable_samples
);
1160 #define session_done() (*(volatile int *)(&session_done))
1161 volatile int session_done
;
1163 static int __perf_session__process_pipe_events(struct perf_session
*self
,
1164 struct perf_tool
*tool
)
1166 union perf_event
*event
;
1167 uint32_t size
, cur_size
= 0;
1174 perf_tool__fill_defaults(tool
);
1177 cur_size
= sizeof(union perf_event
);
1179 buf
= malloc(cur_size
);
1184 err
= readn(self
->fd
, event
, sizeof(struct perf_event_header
));
1189 pr_err("failed to read event header\n");
1193 if (self
->header
.needs_swap
)
1194 perf_event_header__bswap(&event
->header
);
1196 size
= event
->header
.size
;
1200 if (size
> cur_size
) {
1201 void *new = realloc(buf
, size
);
1203 pr_err("failed to allocate memory to read event\n");
1211 p
+= sizeof(struct perf_event_header
);
1213 if (size
- sizeof(struct perf_event_header
)) {
1214 err
= readn(self
->fd
, p
, size
- sizeof(struct perf_event_header
));
1217 pr_err("unexpected end of event stream\n");
1221 pr_err("failed to read event data\n");
1226 if ((skip
= perf_session__process_event(self
, event
, tool
, head
)) < 0) {
1227 pr_err("%#" PRIx64
" [%#x]: failed to process type: %d\n",
1228 head
, event
->header
.size
, event
->header
.type
);
1238 if (!session_done())
1244 perf_session__warn_about_errors(self
, tool
);
1245 perf_session_free_sample_buffers(self
);
1249 static union perf_event
*
1250 fetch_mmaped_event(struct perf_session
*session
,
1251 u64 head
, size_t mmap_size
, char *buf
)
1253 union perf_event
*event
;
1256 * Ensure we have enough space remaining to read
1257 * the size of the event in the headers.
1259 if (head
+ sizeof(event
->header
) > mmap_size
)
1262 event
= (union perf_event
*)(buf
+ head
);
1264 if (session
->header
.needs_swap
)
1265 perf_event_header__bswap(&event
->header
);
1267 if (head
+ event
->header
.size
> mmap_size
)
1273 int __perf_session__process_events(struct perf_session
*session
,
1274 u64 data_offset
, u64 data_size
,
1275 u64 file_size
, struct perf_tool
*tool
)
1277 u64 head
, page_offset
, file_offset
, file_pos
, progress_next
;
1278 int err
, mmap_prot
, mmap_flags
, map_idx
= 0;
1279 size_t page_size
, mmap_size
;
1280 char *buf
, *mmaps
[8];
1281 union perf_event
*event
;
1284 perf_tool__fill_defaults(tool
);
1286 page_size
= sysconf(_SC_PAGESIZE
);
1288 page_offset
= page_size
* (data_offset
/ page_size
);
1289 file_offset
= page_offset
;
1290 head
= data_offset
- page_offset
;
1292 if (data_offset
+ data_size
< file_size
)
1293 file_size
= data_offset
+ data_size
;
1295 progress_next
= file_size
/ 16;
1297 mmap_size
= session
->mmap_window
;
1298 if (mmap_size
> file_size
)
1299 mmap_size
= file_size
;
1301 memset(mmaps
, 0, sizeof(mmaps
));
1303 mmap_prot
= PROT_READ
;
1304 mmap_flags
= MAP_SHARED
;
1306 if (session
->header
.needs_swap
) {
1307 mmap_prot
|= PROT_WRITE
;
1308 mmap_flags
= MAP_PRIVATE
;
1311 buf
= mmap(NULL
, mmap_size
, mmap_prot
, mmap_flags
, session
->fd
,
1313 if (buf
== MAP_FAILED
) {
1314 pr_err("failed to mmap file\n");
1318 mmaps
[map_idx
] = buf
;
1319 map_idx
= (map_idx
+ 1) & (ARRAY_SIZE(mmaps
) - 1);
1320 file_pos
= file_offset
+ head
;
1323 event
= fetch_mmaped_event(session
, head
, mmap_size
, buf
);
1325 if (mmaps
[map_idx
]) {
1326 munmap(mmaps
[map_idx
], mmap_size
);
1327 mmaps
[map_idx
] = NULL
;
1330 page_offset
= page_size
* (head
/ page_size
);
1331 file_offset
+= page_offset
;
1332 head
-= page_offset
;
1336 size
= event
->header
.size
;
1339 perf_session__process_event(session
, event
, tool
, file_pos
) < 0) {
1340 pr_err("%#" PRIx64
" [%#x]: failed to process type: %d\n",
1341 file_offset
+ head
, event
->header
.size
,
1342 event
->header
.type
);
1350 if (file_pos
>= progress_next
) {
1351 progress_next
+= file_size
/ 16;
1352 ui_progress__update(file_pos
, file_size
,
1353 "Processing events...");
1356 if (file_pos
< file_size
)
1360 /* do the final flush for ordered samples */
1361 session
->ordered_samples
.next_flush
= ULLONG_MAX
;
1362 flush_sample_queue(session
, tool
);
1364 perf_session__warn_about_errors(session
, tool
);
1365 perf_session_free_sample_buffers(session
);
1369 int perf_session__process_events(struct perf_session
*self
,
1370 struct perf_tool
*tool
)
1374 if (perf_session__register_idle_thread(self
) == NULL
)
1378 err
= __perf_session__process_events(self
,
1379 self
->header
.data_offset
,
1380 self
->header
.data_size
,
1383 err
= __perf_session__process_pipe_events(self
, tool
);
1388 bool perf_session__has_traces(struct perf_session
*self
, const char *msg
)
1390 if (!(self
->sample_type
& PERF_SAMPLE_RAW
)) {
1391 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg
);
1398 int maps__set_kallsyms_ref_reloc_sym(struct map
**maps
,
1399 const char *symbol_name
, u64 addr
)
1403 struct ref_reloc_sym
*ref
;
1405 ref
= zalloc(sizeof(struct ref_reloc_sym
));
1409 ref
->name
= strdup(symbol_name
);
1410 if (ref
->name
== NULL
) {
1415 bracket
= strchr(ref
->name
, ']');
1421 for (i
= 0; i
< MAP__NR_TYPES
; ++i
) {
1422 struct kmap
*kmap
= map__kmap(maps
[i
]);
1423 kmap
->ref_reloc_sym
= ref
;
1429 size_t perf_session__fprintf_dsos(struct perf_session
*self
, FILE *fp
)
1431 return __dsos__fprintf(&self
->host_machine
.kernel_dsos
, fp
) +
1432 __dsos__fprintf(&self
->host_machine
.user_dsos
, fp
) +
1433 machines__fprintf_dsos(&self
->machines
, fp
);
1436 size_t perf_session__fprintf_dsos_buildid(struct perf_session
*self
, FILE *fp
,
1439 size_t ret
= machine__fprintf_dsos_buildid(&self
->host_machine
, fp
, with_hits
);
1440 return ret
+ machines__fprintf_dsos_buildid(&self
->machines
, fp
, with_hits
);
1443 size_t perf_session__fprintf_nr_events(struct perf_session
*session
, FILE *fp
)
1445 struct perf_evsel
*pos
;
1446 size_t ret
= fprintf(fp
, "Aggregated stats:\n");
1448 ret
+= hists__fprintf_nr_events(&session
->hists
, fp
);
1450 list_for_each_entry(pos
, &session
->evlist
->entries
, node
) {
1451 ret
+= fprintf(fp
, "%s stats:\n", perf_evsel__name(pos
));
1452 ret
+= hists__fprintf_nr_events(&pos
->hists
, fp
);
1458 size_t perf_session__fprintf(struct perf_session
*session
, FILE *fp
)
1461 * FIXME: Here we have to actually print all the machines in this
1462 * session, not just the host...
1464 return machine__fprintf(&session
->host_machine
, fp
);
1467 void perf_session__remove_thread(struct perf_session
*session
,
1471 * FIXME: This one makes no sense, we need to remove the thread from
1472 * the machine it belongs to, perf_session can have many machines, so
1473 * doing it always on ->host_machine is wrong. Fix when auditing all
1474 * the 'perf kvm' code.
1476 machine__remove_thread(&session
->host_machine
, th
);
1479 struct perf_evsel
*perf_session__find_first_evtype(struct perf_session
*session
,
1482 struct perf_evsel
*pos
;
1484 list_for_each_entry(pos
, &session
->evlist
->entries
, node
) {
1485 if (pos
->attr
.type
== type
)
1491 void perf_event__print_ip(union perf_event
*event
, struct perf_sample
*sample
,
1492 struct machine
*machine
, int print_sym
,
1493 int print_dso
, int print_symoffset
)
1495 struct addr_location al
;
1496 struct callchain_cursor_node
*node
;
1498 if (perf_event__preprocess_sample(event
, machine
, &al
, sample
,
1500 error("problem processing %d event, skipping it.\n",
1501 event
->header
.type
);
1505 if (symbol_conf
.use_callchain
&& sample
->callchain
) {
1507 if (machine__resolve_callchain(machine
, al
.thread
,
1508 sample
->callchain
, NULL
) != 0) {
1510 error("Failed to resolve callchain. Skipping\n");
1513 callchain_cursor_commit(&callchain_cursor
);
1516 node
= callchain_cursor_current(&callchain_cursor
);
1520 printf("\t%16" PRIx64
, node
->ip
);
1523 symbol__fprintf_symname(node
->sym
, stdout
);
1527 map__fprintf_dsoname(node
->map
, stdout
);
1532 callchain_cursor_advance(&callchain_cursor
);
1536 printf("%16" PRIx64
, sample
->ip
);
1539 if (print_symoffset
)
1540 symbol__fprintf_symname_offs(al
.sym
, &al
,
1543 symbol__fprintf_symname(al
.sym
, stdout
);
1548 map__fprintf_dsoname(al
.map
, stdout
);
1554 int perf_session__cpu_bitmap(struct perf_session
*session
,
1555 const char *cpu_list
, unsigned long *cpu_bitmap
)
1558 struct cpu_map
*map
;
1560 for (i
= 0; i
< PERF_TYPE_MAX
; ++i
) {
1561 struct perf_evsel
*evsel
;
1563 evsel
= perf_session__find_first_evtype(session
, i
);
1567 if (!(evsel
->attr
.sample_type
& PERF_SAMPLE_CPU
)) {
1568 pr_err("File does not contain CPU events. "
1569 "Remove -c option to proceed.\n");
1574 map
= cpu_map__new(cpu_list
);
1576 pr_err("Invalid cpu_list\n");
1580 for (i
= 0; i
< map
->nr
; i
++) {
1581 int cpu
= map
->map
[i
];
1583 if (cpu
>= MAX_NR_CPUS
) {
1584 pr_err("Requested CPU %d too large. "
1585 "Consider raising MAX_NR_CPUS\n", cpu
);
1589 set_bit(cpu
, cpu_bitmap
);
1595 void perf_session__fprintf_info(struct perf_session
*session
, FILE *fp
,
1601 if (session
== NULL
|| fp
== NULL
)
1604 ret
= fstat(session
->fd
, &st
);
1608 fprintf(fp
, "# ========\n");
1609 fprintf(fp
, "# captured on: %s", ctime(&st
.st_ctime
));
1610 perf_header__fprintf_info(session
, fp
, full
);
1611 fprintf(fp
, "# ========\n#\n");