1 #define _FILE_OFFSET_BITS 64
3 #include <linux/kernel.h>
16 static int perf_session__open(struct perf_session
*self
, bool force
)
18 struct stat input_stat
;
20 if (!strcmp(self
->filename
, "-")) {
22 self
->fd
= STDIN_FILENO
;
24 if (perf_session__read_header(self
, self
->fd
) < 0)
25 pr_err("incompatible file format");
30 self
->fd
= open(self
->filename
, O_RDONLY
);
34 pr_err("failed to open %s: %s", self
->filename
, strerror(err
));
35 if (err
== ENOENT
&& !strcmp(self
->filename
, "perf.data"))
36 pr_err(" (try 'perf record' first)");
41 if (fstat(self
->fd
, &input_stat
) < 0)
44 if (!force
&& input_stat
.st_uid
&& (input_stat
.st_uid
!= geteuid())) {
45 pr_err("file %s not owned by current user or root\n",
50 if (!input_stat
.st_size
) {
51 pr_info("zero-sized file (%s), nothing to do!\n",
56 if (perf_session__read_header(self
, self
->fd
) < 0) {
57 pr_err("incompatible file format");
61 self
->size
= input_stat
.st_size
;
70 static void perf_session__id_header_size(struct perf_session
*session
)
72 struct perf_sample
*data
;
73 u64 sample_type
= session
->sample_type
;
76 if (!session
->sample_id_all
)
79 if (sample_type
& PERF_SAMPLE_TID
)
80 size
+= sizeof(data
->tid
) * 2;
82 if (sample_type
& PERF_SAMPLE_TIME
)
83 size
+= sizeof(data
->time
);
85 if (sample_type
& PERF_SAMPLE_ID
)
86 size
+= sizeof(data
->id
);
88 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
89 size
+= sizeof(data
->stream_id
);
91 if (sample_type
& PERF_SAMPLE_CPU
)
92 size
+= sizeof(data
->cpu
) * 2;
94 session
->id_hdr_size
= size
;
97 void perf_session__update_sample_type(struct perf_session
*self
)
99 self
->sample_type
= perf_evlist__sample_type(self
->evlist
);
100 self
->sample_id_all
= perf_evlist__sample_id_all(self
->evlist
);
101 perf_session__id_header_size(self
);
104 int perf_session__create_kernel_maps(struct perf_session
*self
)
106 int ret
= machine__create_kernel_maps(&self
->host_machine
);
109 ret
= machines__create_guest_kernel_maps(&self
->machines
);
113 static void perf_session__destroy_kernel_maps(struct perf_session
*self
)
115 machine__destroy_kernel_maps(&self
->host_machine
);
116 machines__destroy_guest_kernel_maps(&self
->machines
);
119 struct perf_session
*perf_session__new(const char *filename
, int mode
,
120 bool force
, bool repipe
,
121 struct perf_event_ops
*ops
)
123 size_t len
= filename
? strlen(filename
) + 1 : 0;
124 struct perf_session
*self
= zalloc(sizeof(*self
) + len
);
129 memcpy(self
->filename
, filename
, len
);
130 self
->threads
= RB_ROOT
;
131 INIT_LIST_HEAD(&self
->dead_threads
);
132 self
->last_match
= NULL
;
134 * On 64bit we can mmap the data file in one go. No need for tiny mmap
135 * slices. On 32bit we use 32MB.
137 #if BITS_PER_LONG == 64
138 self
->mmap_window
= ULLONG_MAX
;
140 self
->mmap_window
= 32 * 1024 * 1024ULL;
142 self
->machines
= RB_ROOT
;
143 self
->repipe
= repipe
;
144 INIT_LIST_HEAD(&self
->ordered_samples
.samples
);
145 INIT_LIST_HEAD(&self
->ordered_samples
.sample_cache
);
146 INIT_LIST_HEAD(&self
->ordered_samples
.to_free
);
147 machine__init(&self
->host_machine
, "", HOST_KERNEL_ID
);
149 if (mode
== O_RDONLY
) {
150 if (perf_session__open(self
, force
) < 0)
152 perf_session__update_sample_type(self
);
153 } else if (mode
== O_WRONLY
) {
155 * In O_RDONLY mode this will be performed when reading the
156 * kernel MMAP event, in perf_event__process_mmap().
158 if (perf_session__create_kernel_maps(self
) < 0)
162 if (ops
&& ops
->ordering_requires_timestamps
&&
163 ops
->ordered_samples
&& !self
->sample_id_all
) {
164 dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n");
165 ops
->ordered_samples
= false;
171 perf_session__delete(self
);
175 static void perf_session__delete_dead_threads(struct perf_session
*self
)
177 struct thread
*n
, *t
;
179 list_for_each_entry_safe(t
, n
, &self
->dead_threads
, node
) {
185 static void perf_session__delete_threads(struct perf_session
*self
)
187 struct rb_node
*nd
= rb_first(&self
->threads
);
190 struct thread
*t
= rb_entry(nd
, struct thread
, rb_node
);
192 rb_erase(&t
->rb_node
, &self
->threads
);
198 void perf_session__delete(struct perf_session
*self
)
200 perf_session__destroy_kernel_maps(self
);
201 perf_session__delete_dead_threads(self
);
202 perf_session__delete_threads(self
);
203 machine__exit(&self
->host_machine
);
208 void perf_session__remove_thread(struct perf_session
*self
, struct thread
*th
)
210 self
->last_match
= NULL
;
211 rb_erase(&th
->rb_node
, &self
->threads
);
213 * We may have references to this thread, for instance in some hist_entry
214 * instances, so just move them to a separate list.
216 list_add_tail(&th
->node
, &self
->dead_threads
);
219 static bool symbol__match_parent_regex(struct symbol
*sym
)
221 if (sym
->name
&& !regexec(&parent_regex
, sym
->name
, 0, NULL
, 0))
227 int perf_session__resolve_callchain(struct perf_session
*self
,
228 struct thread
*thread
,
229 struct ip_callchain
*chain
,
230 struct symbol
**parent
)
232 u8 cpumode
= PERF_RECORD_MISC_USER
;
236 callchain_cursor_reset(&self
->callchain_cursor
);
238 for (i
= 0; i
< chain
->nr
; i
++) {
239 u64 ip
= chain
->ips
[i
];
240 struct addr_location al
;
242 if (ip
>= PERF_CONTEXT_MAX
) {
244 case PERF_CONTEXT_HV
:
245 cpumode
= PERF_RECORD_MISC_HYPERVISOR
; break;
246 case PERF_CONTEXT_KERNEL
:
247 cpumode
= PERF_RECORD_MISC_KERNEL
; break;
248 case PERF_CONTEXT_USER
:
249 cpumode
= PERF_RECORD_MISC_USER
; break;
257 thread__find_addr_location(thread
, self
, cpumode
,
258 MAP__FUNCTION
, thread
->pid
, ip
, &al
, NULL
);
259 if (al
.sym
!= NULL
) {
260 if (sort__has_parent
&& !*parent
&&
261 symbol__match_parent_regex(al
.sym
))
263 if (!symbol_conf
.use_callchain
)
267 err
= callchain_cursor_append(&self
->callchain_cursor
,
276 static int process_event_synth_stub(union perf_event
*event __used
,
277 struct perf_session
*session __used
)
279 dump_printf(": unhandled!\n");
283 static int process_event_sample_stub(union perf_event
*event __used
,
284 struct perf_sample
*sample __used
,
285 struct perf_evsel
*evsel __used
,
286 struct perf_session
*session __used
)
288 dump_printf(": unhandled!\n");
292 static int process_event_stub(union perf_event
*event __used
,
293 struct perf_sample
*sample __used
,
294 struct perf_session
*session __used
)
296 dump_printf(": unhandled!\n");
300 static int process_finished_round_stub(union perf_event
*event __used
,
301 struct perf_session
*session __used
,
302 struct perf_event_ops
*ops __used
)
304 dump_printf(": unhandled!\n");
308 static int process_finished_round(union perf_event
*event
,
309 struct perf_session
*session
,
310 struct perf_event_ops
*ops
);
312 static void perf_event_ops__fill_defaults(struct perf_event_ops
*handler
)
314 if (handler
->sample
== NULL
)
315 handler
->sample
= process_event_sample_stub
;
316 if (handler
->mmap
== NULL
)
317 handler
->mmap
= process_event_stub
;
318 if (handler
->comm
== NULL
)
319 handler
->comm
= process_event_stub
;
320 if (handler
->fork
== NULL
)
321 handler
->fork
= process_event_stub
;
322 if (handler
->exit
== NULL
)
323 handler
->exit
= process_event_stub
;
324 if (handler
->lost
== NULL
)
325 handler
->lost
= perf_event__process_lost
;
326 if (handler
->read
== NULL
)
327 handler
->read
= process_event_stub
;
328 if (handler
->throttle
== NULL
)
329 handler
->throttle
= process_event_stub
;
330 if (handler
->unthrottle
== NULL
)
331 handler
->unthrottle
= process_event_stub
;
332 if (handler
->attr
== NULL
)
333 handler
->attr
= process_event_synth_stub
;
334 if (handler
->event_type
== NULL
)
335 handler
->event_type
= process_event_synth_stub
;
336 if (handler
->tracing_data
== NULL
)
337 handler
->tracing_data
= process_event_synth_stub
;
338 if (handler
->build_id
== NULL
)
339 handler
->build_id
= process_event_synth_stub
;
340 if (handler
->finished_round
== NULL
) {
341 if (handler
->ordered_samples
)
342 handler
->finished_round
= process_finished_round
;
344 handler
->finished_round
= process_finished_round_stub
;
348 void mem_bswap_64(void *src
, int byte_size
)
352 while (byte_size
> 0) {
354 byte_size
-= sizeof(u64
);
359 static void perf_event__all64_swap(union perf_event
*event
)
361 struct perf_event_header
*hdr
= &event
->header
;
362 mem_bswap_64(hdr
+ 1, event
->header
.size
- sizeof(*hdr
));
365 static void perf_event__comm_swap(union perf_event
*event
)
367 event
->comm
.pid
= bswap_32(event
->comm
.pid
);
368 event
->comm
.tid
= bswap_32(event
->comm
.tid
);
371 static void perf_event__mmap_swap(union perf_event
*event
)
373 event
->mmap
.pid
= bswap_32(event
->mmap
.pid
);
374 event
->mmap
.tid
= bswap_32(event
->mmap
.tid
);
375 event
->mmap
.start
= bswap_64(event
->mmap
.start
);
376 event
->mmap
.len
= bswap_64(event
->mmap
.len
);
377 event
->mmap
.pgoff
= bswap_64(event
->mmap
.pgoff
);
380 static void perf_event__task_swap(union perf_event
*event
)
382 event
->fork
.pid
= bswap_32(event
->fork
.pid
);
383 event
->fork
.tid
= bswap_32(event
->fork
.tid
);
384 event
->fork
.ppid
= bswap_32(event
->fork
.ppid
);
385 event
->fork
.ptid
= bswap_32(event
->fork
.ptid
);
386 event
->fork
.time
= bswap_64(event
->fork
.time
);
389 static void perf_event__read_swap(union perf_event
*event
)
391 event
->read
.pid
= bswap_32(event
->read
.pid
);
392 event
->read
.tid
= bswap_32(event
->read
.tid
);
393 event
->read
.value
= bswap_64(event
->read
.value
);
394 event
->read
.time_enabled
= bswap_64(event
->read
.time_enabled
);
395 event
->read
.time_running
= bswap_64(event
->read
.time_running
);
396 event
->read
.id
= bswap_64(event
->read
.id
);
399 static void perf_event__attr_swap(union perf_event
*event
)
403 event
->attr
.attr
.type
= bswap_32(event
->attr
.attr
.type
);
404 event
->attr
.attr
.size
= bswap_32(event
->attr
.attr
.size
);
405 event
->attr
.attr
.config
= bswap_64(event
->attr
.attr
.config
);
406 event
->attr
.attr
.sample_period
= bswap_64(event
->attr
.attr
.sample_period
);
407 event
->attr
.attr
.sample_type
= bswap_64(event
->attr
.attr
.sample_type
);
408 event
->attr
.attr
.read_format
= bswap_64(event
->attr
.attr
.read_format
);
409 event
->attr
.attr
.wakeup_events
= bswap_32(event
->attr
.attr
.wakeup_events
);
410 event
->attr
.attr
.bp_type
= bswap_32(event
->attr
.attr
.bp_type
);
411 event
->attr
.attr
.bp_addr
= bswap_64(event
->attr
.attr
.bp_addr
);
412 event
->attr
.attr
.bp_len
= bswap_64(event
->attr
.attr
.bp_len
);
414 size
= event
->header
.size
;
415 size
-= (void *)&event
->attr
.id
- (void *)event
;
416 mem_bswap_64(event
->attr
.id
, size
);
419 static void perf_event__event_type_swap(union perf_event
*event
)
421 event
->event_type
.event_type
.event_id
=
422 bswap_64(event
->event_type
.event_type
.event_id
);
425 static void perf_event__tracing_data_swap(union perf_event
*event
)
427 event
->tracing_data
.size
= bswap_32(event
->tracing_data
.size
);
430 typedef void (*perf_event__swap_op
)(union perf_event
*event
);
432 static perf_event__swap_op perf_event__swap_ops
[] = {
433 [PERF_RECORD_MMAP
] = perf_event__mmap_swap
,
434 [PERF_RECORD_COMM
] = perf_event__comm_swap
,
435 [PERF_RECORD_FORK
] = perf_event__task_swap
,
436 [PERF_RECORD_EXIT
] = perf_event__task_swap
,
437 [PERF_RECORD_LOST
] = perf_event__all64_swap
,
438 [PERF_RECORD_READ
] = perf_event__read_swap
,
439 [PERF_RECORD_SAMPLE
] = perf_event__all64_swap
,
440 [PERF_RECORD_HEADER_ATTR
] = perf_event__attr_swap
,
441 [PERF_RECORD_HEADER_EVENT_TYPE
] = perf_event__event_type_swap
,
442 [PERF_RECORD_HEADER_TRACING_DATA
] = perf_event__tracing_data_swap
,
443 [PERF_RECORD_HEADER_BUILD_ID
] = NULL
,
444 [PERF_RECORD_HEADER_MAX
] = NULL
,
447 struct sample_queue
{
450 union perf_event
*event
;
451 struct list_head list
;
454 static void perf_session_free_sample_buffers(struct perf_session
*session
)
456 struct ordered_samples
*os
= &session
->ordered_samples
;
458 while (!list_empty(&os
->to_free
)) {
459 struct sample_queue
*sq
;
461 sq
= list_entry(os
->to_free
.next
, struct sample_queue
, list
);
467 static int perf_session_deliver_event(struct perf_session
*session
,
468 union perf_event
*event
,
469 struct perf_sample
*sample
,
470 struct perf_event_ops
*ops
,
473 static void flush_sample_queue(struct perf_session
*s
,
474 struct perf_event_ops
*ops
)
476 struct ordered_samples
*os
= &s
->ordered_samples
;
477 struct list_head
*head
= &os
->samples
;
478 struct sample_queue
*tmp
, *iter
;
479 struct perf_sample sample
;
480 u64 limit
= os
->next_flush
;
481 u64 last_ts
= os
->last_sample
? os
->last_sample
->timestamp
: 0ULL;
483 if (!ops
->ordered_samples
|| !limit
)
486 list_for_each_entry_safe(iter
, tmp
, head
, list
) {
487 if (iter
->timestamp
> limit
)
490 perf_session__parse_sample(s
, iter
->event
, &sample
);
491 perf_session_deliver_event(s
, iter
->event
, &sample
, ops
,
494 os
->last_flush
= iter
->timestamp
;
495 list_del(&iter
->list
);
496 list_add(&iter
->list
, &os
->sample_cache
);
499 if (list_empty(head
)) {
500 os
->last_sample
= NULL
;
501 } else if (last_ts
<= limit
) {
503 list_entry(head
->prev
, struct sample_queue
, list
);
508 * When perf record finishes a pass on every buffers, it records this pseudo
510 * We record the max timestamp t found in the pass n.
511 * Assuming these timestamps are monotonic across cpus, we know that if
512 * a buffer still has events with timestamps below t, they will be all
513 * available and then read in the pass n + 1.
514 * Hence when we start to read the pass n + 2, we can safely flush every
515 * events with timestamps below t.
517 * ============ PASS n =================
520 * cnt1 timestamps | cnt2 timestamps
523 * - | 4 <--- max recorded
525 * ============ PASS n + 1 ==============
528 * cnt1 timestamps | cnt2 timestamps
531 * 5 | 7 <---- max recorded
533 * Flush every events below timestamp 4
535 * ============ PASS n + 2 ==============
538 * cnt1 timestamps | cnt2 timestamps
543 * Flush every events below timestamp 7
546 static int process_finished_round(union perf_event
*event __used
,
547 struct perf_session
*session
,
548 struct perf_event_ops
*ops
)
550 flush_sample_queue(session
, ops
);
551 session
->ordered_samples
.next_flush
= session
->ordered_samples
.max_timestamp
;
556 /* The queue is ordered by time */
557 static void __queue_event(struct sample_queue
*new, struct perf_session
*s
)
559 struct ordered_samples
*os
= &s
->ordered_samples
;
560 struct sample_queue
*sample
= os
->last_sample
;
561 u64 timestamp
= new->timestamp
;
564 os
->last_sample
= new;
567 list_add(&new->list
, &os
->samples
);
568 os
->max_timestamp
= timestamp
;
573 * last_sample might point to some random place in the list as it's
574 * the last queued event. We expect that the new event is close to
577 if (sample
->timestamp
<= timestamp
) {
578 while (sample
->timestamp
<= timestamp
) {
579 p
= sample
->list
.next
;
580 if (p
== &os
->samples
) {
581 list_add_tail(&new->list
, &os
->samples
);
582 os
->max_timestamp
= timestamp
;
585 sample
= list_entry(p
, struct sample_queue
, list
);
587 list_add_tail(&new->list
, &sample
->list
);
589 while (sample
->timestamp
> timestamp
) {
590 p
= sample
->list
.prev
;
591 if (p
== &os
->samples
) {
592 list_add(&new->list
, &os
->samples
);
595 sample
= list_entry(p
, struct sample_queue
, list
);
597 list_add(&new->list
, &sample
->list
);
601 #define MAX_SAMPLE_BUFFER (64 * 1024 / sizeof(struct sample_queue))
603 static int perf_session_queue_event(struct perf_session
*s
, union perf_event
*event
,
604 struct perf_sample
*sample
, u64 file_offset
)
606 struct ordered_samples
*os
= &s
->ordered_samples
;
607 struct list_head
*sc
= &os
->sample_cache
;
608 u64 timestamp
= sample
->time
;
609 struct sample_queue
*new;
611 if (!timestamp
|| timestamp
== ~0ULL)
614 if (timestamp
< s
->ordered_samples
.last_flush
) {
615 printf("Warning: Timestamp below last timeslice flush\n");
619 if (!list_empty(sc
)) {
620 new = list_entry(sc
->next
, struct sample_queue
, list
);
621 list_del(&new->list
);
622 } else if (os
->sample_buffer
) {
623 new = os
->sample_buffer
+ os
->sample_buffer_idx
;
624 if (++os
->sample_buffer_idx
== MAX_SAMPLE_BUFFER
)
625 os
->sample_buffer
= NULL
;
627 os
->sample_buffer
= malloc(MAX_SAMPLE_BUFFER
* sizeof(*new));
628 if (!os
->sample_buffer
)
630 list_add(&os
->sample_buffer
->list
, &os
->to_free
);
631 os
->sample_buffer_idx
= 2;
632 new = os
->sample_buffer
+ 1;
635 new->timestamp
= timestamp
;
636 new->file_offset
= file_offset
;
639 __queue_event(new, s
);
644 static void callchain__printf(struct perf_sample
*sample
)
648 printf("... chain: nr:%" PRIu64
"\n", sample
->callchain
->nr
);
650 for (i
= 0; i
< sample
->callchain
->nr
; i
++)
651 printf("..... %2d: %016" PRIx64
"\n",
652 i
, sample
->callchain
->ips
[i
]);
655 static void perf_session__print_tstamp(struct perf_session
*session
,
656 union perf_event
*event
,
657 struct perf_sample
*sample
)
659 if (event
->header
.type
!= PERF_RECORD_SAMPLE
&&
660 !session
->sample_id_all
) {
661 fputs("-1 -1 ", stdout
);
665 if ((session
->sample_type
& PERF_SAMPLE_CPU
))
666 printf("%u ", sample
->cpu
);
668 if (session
->sample_type
& PERF_SAMPLE_TIME
)
669 printf("%" PRIu64
" ", sample
->time
);
672 static void dump_event(struct perf_session
*session
, union perf_event
*event
,
673 u64 file_offset
, struct perf_sample
*sample
)
678 printf("\n%#" PRIx64
" [%#x]: event: %d\n",
679 file_offset
, event
->header
.size
, event
->header
.type
);
684 perf_session__print_tstamp(session
, event
, sample
);
686 printf("%#" PRIx64
" [%#x]: PERF_RECORD_%s", file_offset
,
687 event
->header
.size
, perf_event__name(event
->header
.type
));
690 static void dump_sample(struct perf_session
*session
, union perf_event
*event
,
691 struct perf_sample
*sample
)
696 printf("(IP, %d): %d/%d: %#" PRIx64
" period: %" PRIu64
"\n",
697 event
->header
.misc
, sample
->pid
, sample
->tid
, sample
->ip
,
700 if (session
->sample_type
& PERF_SAMPLE_CALLCHAIN
)
701 callchain__printf(sample
);
704 static int perf_session_deliver_event(struct perf_session
*session
,
705 union perf_event
*event
,
706 struct perf_sample
*sample
,
707 struct perf_event_ops
*ops
,
710 struct perf_evsel
*evsel
;
712 dump_event(session
, event
, file_offset
, sample
);
714 switch (event
->header
.type
) {
715 case PERF_RECORD_SAMPLE
:
716 dump_sample(session
, event
, sample
);
717 evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
719 ++session
->hists
.stats
.nr_unknown_id
;
722 return ops
->sample(event
, sample
, evsel
, session
);
723 case PERF_RECORD_MMAP
:
724 return ops
->mmap(event
, sample
, session
);
725 case PERF_RECORD_COMM
:
726 return ops
->comm(event
, sample
, session
);
727 case PERF_RECORD_FORK
:
728 return ops
->fork(event
, sample
, session
);
729 case PERF_RECORD_EXIT
:
730 return ops
->exit(event
, sample
, session
);
731 case PERF_RECORD_LOST
:
732 return ops
->lost(event
, sample
, session
);
733 case PERF_RECORD_READ
:
734 return ops
->read(event
, sample
, session
);
735 case PERF_RECORD_THROTTLE
:
736 return ops
->throttle(event
, sample
, session
);
737 case PERF_RECORD_UNTHROTTLE
:
738 return ops
->unthrottle(event
, sample
, session
);
740 ++session
->hists
.stats
.nr_unknown_events
;
745 static int perf_session__preprocess_sample(struct perf_session
*session
,
746 union perf_event
*event
, struct perf_sample
*sample
)
748 if (event
->header
.type
!= PERF_RECORD_SAMPLE
||
749 !(session
->sample_type
& PERF_SAMPLE_CALLCHAIN
))
752 if (!ip_callchain__valid(sample
->callchain
, event
)) {
753 pr_debug("call-chain problem with event, skipping it.\n");
754 ++session
->hists
.stats
.nr_invalid_chains
;
755 session
->hists
.stats
.total_invalid_chains
+= sample
->period
;
761 static int perf_session__process_user_event(struct perf_session
*session
, union perf_event
*event
,
762 struct perf_event_ops
*ops
, u64 file_offset
)
764 dump_event(session
, event
, file_offset
, NULL
);
766 /* These events are processed right away */
767 switch (event
->header
.type
) {
768 case PERF_RECORD_HEADER_ATTR
:
769 return ops
->attr(event
, session
);
770 case PERF_RECORD_HEADER_EVENT_TYPE
:
771 return ops
->event_type(event
, session
);
772 case PERF_RECORD_HEADER_TRACING_DATA
:
773 /* setup for reading amidst mmap */
774 lseek(session
->fd
, file_offset
, SEEK_SET
);
775 return ops
->tracing_data(event
, session
);
776 case PERF_RECORD_HEADER_BUILD_ID
:
777 return ops
->build_id(event
, session
);
778 case PERF_RECORD_FINISHED_ROUND
:
779 return ops
->finished_round(event
, session
, ops
);
785 static int perf_session__process_event(struct perf_session
*session
,
786 union perf_event
*event
,
787 struct perf_event_ops
*ops
,
790 struct perf_sample sample
;
793 if (session
->header
.needs_swap
&&
794 perf_event__swap_ops
[event
->header
.type
])
795 perf_event__swap_ops
[event
->header
.type
](event
);
797 if (event
->header
.type
>= PERF_RECORD_HEADER_MAX
)
800 hists__inc_nr_events(&session
->hists
, event
->header
.type
);
802 if (event
->header
.type
>= PERF_RECORD_USER_TYPE_START
)
803 return perf_session__process_user_event(session
, event
, ops
, file_offset
);
806 * For all kernel events we get the sample data
808 perf_session__parse_sample(session
, event
, &sample
);
810 /* Preprocess sample records - precheck callchains */
811 if (perf_session__preprocess_sample(session
, event
, &sample
))
814 if (ops
->ordered_samples
) {
815 ret
= perf_session_queue_event(session
, event
, &sample
,
821 return perf_session_deliver_event(session
, event
, &sample
, ops
,
825 void perf_event_header__bswap(struct perf_event_header
*self
)
827 self
->type
= bswap_32(self
->type
);
828 self
->misc
= bswap_16(self
->misc
);
829 self
->size
= bswap_16(self
->size
);
832 static struct thread
*perf_session__register_idle_thread(struct perf_session
*self
)
834 struct thread
*thread
= perf_session__findnew(self
, 0);
836 if (thread
== NULL
|| thread__set_comm(thread
, "swapper")) {
837 pr_err("problem inserting idle task.\n");
844 static void perf_session__warn_about_errors(const struct perf_session
*session
,
845 const struct perf_event_ops
*ops
)
847 if (ops
->lost
== perf_event__process_lost
&&
848 session
->hists
.stats
.total_lost
!= 0) {
849 ui__warning("Processed %" PRIu64
" events and LOST %" PRIu64
850 "!\n\nCheck IO/CPU overload!\n\n",
851 session
->hists
.stats
.total_period
,
852 session
->hists
.stats
.total_lost
);
855 if (session
->hists
.stats
.nr_unknown_events
!= 0) {
856 ui__warning("Found %u unknown events!\n\n"
857 "Is this an older tool processing a perf.data "
858 "file generated by a more recent tool?\n\n"
859 "If that is not the case, consider "
860 "reporting to linux-kernel@vger.kernel.org.\n\n",
861 session
->hists
.stats
.nr_unknown_events
);
864 if (session
->hists
.stats
.nr_unknown_id
!= 0) {
865 ui__warning("%u samples with id not present in the header\n",
866 session
->hists
.stats
.nr_unknown_id
);
869 if (session
->hists
.stats
.nr_invalid_chains
!= 0) {
870 ui__warning("Found invalid callchains!\n\n"
871 "%u out of %u events were discarded for this reason.\n\n"
872 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
873 session
->hists
.stats
.nr_invalid_chains
,
874 session
->hists
.stats
.nr_events
[PERF_RECORD_SAMPLE
]);
878 #define session_done() (*(volatile int *)(&session_done))
879 volatile int session_done
;
881 static int __perf_session__process_pipe_events(struct perf_session
*self
,
882 struct perf_event_ops
*ops
)
884 union perf_event event
;
891 perf_event_ops__fill_defaults(ops
);
895 err
= readn(self
->fd
, &event
, sizeof(struct perf_event_header
));
900 pr_err("failed to read event header\n");
904 if (self
->header
.needs_swap
)
905 perf_event_header__bswap(&event
.header
);
907 size
= event
.header
.size
;
912 p
+= sizeof(struct perf_event_header
);
914 if (size
- sizeof(struct perf_event_header
)) {
915 err
= readn(self
->fd
, p
, size
- sizeof(struct perf_event_header
));
918 pr_err("unexpected end of event stream\n");
922 pr_err("failed to read event data\n");
928 (skip
= perf_session__process_event(self
, &event
, ops
, head
)) < 0) {
929 dump_printf("%#" PRIx64
" [%#x]: skipping unknown header type: %d\n",
930 head
, event
.header
.size
, event
.header
.type
);
932 * assume we lost track of the stream, check alignment, and
933 * increment a single u64 in the hope to catch on again 'soon'.
935 if (unlikely(head
& 7))
951 perf_session__warn_about_errors(self
, ops
);
952 perf_session_free_sample_buffers(self
);
956 int __perf_session__process_events(struct perf_session
*session
,
957 u64 data_offset
, u64 data_size
,
958 u64 file_size
, struct perf_event_ops
*ops
)
960 u64 head
, page_offset
, file_offset
, file_pos
, progress_next
;
961 int err
, mmap_prot
, mmap_flags
, map_idx
= 0;
962 struct ui_progress
*progress
;
963 size_t page_size
, mmap_size
;
964 char *buf
, *mmaps
[8];
965 union perf_event
*event
;
968 perf_event_ops__fill_defaults(ops
);
970 page_size
= sysconf(_SC_PAGESIZE
);
972 page_offset
= page_size
* (data_offset
/ page_size
);
973 file_offset
= page_offset
;
974 head
= data_offset
- page_offset
;
976 if (data_offset
+ data_size
< file_size
)
977 file_size
= data_offset
+ data_size
;
979 progress_next
= file_size
/ 16;
980 progress
= ui_progress__new("Processing events...", file_size
);
981 if (progress
== NULL
)
984 mmap_size
= session
->mmap_window
;
985 if (mmap_size
> file_size
)
986 mmap_size
= file_size
;
988 memset(mmaps
, 0, sizeof(mmaps
));
990 mmap_prot
= PROT_READ
;
991 mmap_flags
= MAP_SHARED
;
993 if (session
->header
.needs_swap
) {
994 mmap_prot
|= PROT_WRITE
;
995 mmap_flags
= MAP_PRIVATE
;
998 buf
= mmap(NULL
, mmap_size
, mmap_prot
, mmap_flags
, session
->fd
,
1000 if (buf
== MAP_FAILED
) {
1001 pr_err("failed to mmap file\n");
1005 mmaps
[map_idx
] = buf
;
1006 map_idx
= (map_idx
+ 1) & (ARRAY_SIZE(mmaps
) - 1);
1007 file_pos
= file_offset
+ head
;
1010 event
= (union perf_event
*)(buf
+ head
);
1012 if (session
->header
.needs_swap
)
1013 perf_event_header__bswap(&event
->header
);
1014 size
= event
->header
.size
;
1018 if (head
+ event
->header
.size
> mmap_size
) {
1019 if (mmaps
[map_idx
]) {
1020 munmap(mmaps
[map_idx
], mmap_size
);
1021 mmaps
[map_idx
] = NULL
;
1024 page_offset
= page_size
* (head
/ page_size
);
1025 file_offset
+= page_offset
;
1026 head
-= page_offset
;
1030 size
= event
->header
.size
;
1033 perf_session__process_event(session
, event
, ops
, file_pos
) < 0) {
1034 dump_printf("%#" PRIx64
" [%#x]: skipping unknown header type: %d\n",
1035 file_offset
+ head
, event
->header
.size
,
1036 event
->header
.type
);
1038 * assume we lost track of the stream, check alignment, and
1039 * increment a single u64 in the hope to catch on again 'soon'.
1041 if (unlikely(head
& 7))
1050 if (file_pos
>= progress_next
) {
1051 progress_next
+= file_size
/ 16;
1052 ui_progress__update(progress
, file_pos
);
1055 if (file_pos
< file_size
)
1059 /* do the final flush for ordered samples */
1060 session
->ordered_samples
.next_flush
= ULLONG_MAX
;
1061 flush_sample_queue(session
, ops
);
1063 ui_progress__delete(progress
);
1064 perf_session__warn_about_errors(session
, ops
);
1065 perf_session_free_sample_buffers(session
);
1069 int perf_session__process_events(struct perf_session
*self
,
1070 struct perf_event_ops
*ops
)
1074 if (perf_session__register_idle_thread(self
) == NULL
)
1078 err
= __perf_session__process_events(self
,
1079 self
->header
.data_offset
,
1080 self
->header
.data_size
,
1083 err
= __perf_session__process_pipe_events(self
, ops
);
1088 bool perf_session__has_traces(struct perf_session
*self
, const char *msg
)
1090 if (!(self
->sample_type
& PERF_SAMPLE_RAW
)) {
1091 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg
);
1098 int perf_session__set_kallsyms_ref_reloc_sym(struct map
**maps
,
1099 const char *symbol_name
,
1104 struct ref_reloc_sym
*ref
;
1106 ref
= zalloc(sizeof(struct ref_reloc_sym
));
1110 ref
->name
= strdup(symbol_name
);
1111 if (ref
->name
== NULL
) {
1116 bracket
= strchr(ref
->name
, ']');
1122 for (i
= 0; i
< MAP__NR_TYPES
; ++i
) {
1123 struct kmap
*kmap
= map__kmap(maps
[i
]);
1124 kmap
->ref_reloc_sym
= ref
;
1130 size_t perf_session__fprintf_dsos(struct perf_session
*self
, FILE *fp
)
1132 return __dsos__fprintf(&self
->host_machine
.kernel_dsos
, fp
) +
1133 __dsos__fprintf(&self
->host_machine
.user_dsos
, fp
) +
1134 machines__fprintf_dsos(&self
->machines
, fp
);
1137 size_t perf_session__fprintf_dsos_buildid(struct perf_session
*self
, FILE *fp
,
1140 size_t ret
= machine__fprintf_dsos_buildid(&self
->host_machine
, fp
, with_hits
);
1141 return ret
+ machines__fprintf_dsos_buildid(&self
->machines
, fp
, with_hits
);
1144 size_t perf_session__fprintf_nr_events(struct perf_session
*session
, FILE *fp
)
1146 struct perf_evsel
*pos
;
1147 size_t ret
= fprintf(fp
, "Aggregated stats:\n");
1149 ret
+= hists__fprintf_nr_events(&session
->hists
, fp
);
1151 list_for_each_entry(pos
, &session
->evlist
->entries
, node
) {
1152 ret
+= fprintf(fp
, "%s stats:\n", event_name(pos
));
1153 ret
+= hists__fprintf_nr_events(&pos
->hists
, fp
);
1159 void perf_session__print_symbols(union perf_event
*event
,
1160 struct perf_sample
*sample
,
1161 struct perf_session
*session
)
1163 struct addr_location al
;
1164 const char *symname
, *dsoname
;
1165 struct callchain_cursor
*cursor
= &session
->callchain_cursor
;
1166 struct callchain_cursor_node
*node
;
1168 if (perf_event__preprocess_sample(event
, session
, &al
, sample
,
1170 error("problem processing %d event, skipping it.\n",
1171 event
->header
.type
);
1175 if (symbol_conf
.use_callchain
&& sample
->callchain
) {
1177 if (perf_session__resolve_callchain(session
, al
.thread
,
1178 sample
->callchain
, NULL
) != 0) {
1180 error("Failed to resolve callchain. Skipping\n");
1183 callchain_cursor_commit(cursor
);
1186 node
= callchain_cursor_current(cursor
);
1190 if (node
->sym
&& node
->sym
->name
)
1191 symname
= node
->sym
->name
;
1195 if (node
->map
&& node
->map
->dso
&& node
->map
->dso
->name
)
1196 dsoname
= node
->map
->dso
->name
;
1200 printf("\t%16" PRIx64
" %s (%s)\n", node
->ip
, symname
, dsoname
);
1202 callchain_cursor_advance(cursor
);
1206 if (al
.sym
&& al
.sym
->name
)
1207 symname
= al
.sym
->name
;
1211 if (al
.map
&& al
.map
->dso
&& al
.map
->dso
->name
)
1212 dsoname
= al
.map
->dso
->name
;
1216 printf("%16" PRIx64
" %s (%s)", al
.addr
, symname
, dsoname
);