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_size
= perf_sample_size(self
->sample_type
);
101 self
->sample_id_all
= perf_evlist__sample_id_all(self
->evlist
);
102 perf_session__id_header_size(self
);
105 int perf_session__create_kernel_maps(struct perf_session
*self
)
107 int ret
= machine__create_kernel_maps(&self
->host_machine
);
110 ret
= machines__create_guest_kernel_maps(&self
->machines
);
114 static void perf_session__destroy_kernel_maps(struct perf_session
*self
)
116 machine__destroy_kernel_maps(&self
->host_machine
);
117 machines__destroy_guest_kernel_maps(&self
->machines
);
120 struct perf_session
*perf_session__new(const char *filename
, int mode
,
121 bool force
, bool repipe
,
122 struct perf_event_ops
*ops
)
124 size_t len
= filename
? strlen(filename
) + 1 : 0;
125 struct perf_session
*self
= zalloc(sizeof(*self
) + len
);
130 memcpy(self
->filename
, filename
, len
);
131 self
->threads
= RB_ROOT
;
132 INIT_LIST_HEAD(&self
->dead_threads
);
133 self
->last_match
= NULL
;
135 * On 64bit we can mmap the data file in one go. No need for tiny mmap
136 * slices. On 32bit we use 32MB.
138 #if BITS_PER_LONG == 64
139 self
->mmap_window
= ULLONG_MAX
;
141 self
->mmap_window
= 32 * 1024 * 1024ULL;
143 self
->machines
= RB_ROOT
;
144 self
->repipe
= repipe
;
145 INIT_LIST_HEAD(&self
->ordered_samples
.samples
);
146 INIT_LIST_HEAD(&self
->ordered_samples
.sample_cache
);
147 INIT_LIST_HEAD(&self
->ordered_samples
.to_free
);
148 machine__init(&self
->host_machine
, "", HOST_KERNEL_ID
);
150 if (mode
== O_RDONLY
) {
151 if (perf_session__open(self
, force
) < 0)
153 perf_session__update_sample_type(self
);
154 } else if (mode
== O_WRONLY
) {
156 * In O_RDONLY mode this will be performed when reading the
157 * kernel MMAP event, in perf_event__process_mmap().
159 if (perf_session__create_kernel_maps(self
) < 0)
163 if (ops
&& ops
->ordering_requires_timestamps
&&
164 ops
->ordered_samples
&& !self
->sample_id_all
) {
165 dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n");
166 ops
->ordered_samples
= false;
172 perf_session__delete(self
);
176 static void perf_session__delete_dead_threads(struct perf_session
*self
)
178 struct thread
*n
, *t
;
180 list_for_each_entry_safe(t
, n
, &self
->dead_threads
, node
) {
186 static void perf_session__delete_threads(struct perf_session
*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 void perf_session__delete(struct perf_session
*self
)
201 perf_session__destroy_kernel_maps(self
);
202 perf_session__delete_dead_threads(self
);
203 perf_session__delete_threads(self
);
204 machine__exit(&self
->host_machine
);
209 void perf_session__remove_thread(struct perf_session
*self
, struct thread
*th
)
211 self
->last_match
= NULL
;
212 rb_erase(&th
->rb_node
, &self
->threads
);
214 * We may have references to this thread, for instance in some hist_entry
215 * instances, so just move them to a separate list.
217 list_add_tail(&th
->node
, &self
->dead_threads
);
220 static bool symbol__match_parent_regex(struct symbol
*sym
)
222 if (sym
->name
&& !regexec(&parent_regex
, sym
->name
, 0, NULL
, 0))
228 int perf_session__resolve_callchain(struct perf_session
*self
,
229 struct thread
*thread
,
230 struct ip_callchain
*chain
,
231 struct symbol
**parent
)
233 u8 cpumode
= PERF_RECORD_MISC_USER
;
237 callchain_cursor_reset(&self
->callchain_cursor
);
239 for (i
= 0; i
< chain
->nr
; i
++) {
240 u64 ip
= chain
->ips
[i
];
241 struct addr_location al
;
243 if (ip
>= PERF_CONTEXT_MAX
) {
245 case PERF_CONTEXT_HV
:
246 cpumode
= PERF_RECORD_MISC_HYPERVISOR
; break;
247 case PERF_CONTEXT_KERNEL
:
248 cpumode
= PERF_RECORD_MISC_KERNEL
; break;
249 case PERF_CONTEXT_USER
:
250 cpumode
= PERF_RECORD_MISC_USER
; break;
258 thread__find_addr_location(thread
, self
, cpumode
,
259 MAP__FUNCTION
, thread
->pid
, ip
, &al
, NULL
);
260 if (al
.sym
!= NULL
) {
261 if (sort__has_parent
&& !*parent
&&
262 symbol__match_parent_regex(al
.sym
))
264 if (!symbol_conf
.use_callchain
)
268 err
= callchain_cursor_append(&self
->callchain_cursor
,
277 static int process_event_synth_stub(union perf_event
*event __used
,
278 struct perf_session
*session __used
)
280 dump_printf(": unhandled!\n");
284 static int process_event_sample_stub(union perf_event
*event __used
,
285 struct perf_sample
*sample __used
,
286 struct perf_evsel
*evsel __used
,
287 struct perf_session
*session __used
)
289 dump_printf(": unhandled!\n");
293 static int process_event_stub(union perf_event
*event __used
,
294 struct perf_sample
*sample __used
,
295 struct perf_session
*session __used
)
297 dump_printf(": unhandled!\n");
301 static int process_finished_round_stub(union perf_event
*event __used
,
302 struct perf_session
*session __used
,
303 struct perf_event_ops
*ops __used
)
305 dump_printf(": unhandled!\n");
309 static int process_finished_round(union perf_event
*event
,
310 struct perf_session
*session
,
311 struct perf_event_ops
*ops
);
313 static void perf_event_ops__fill_defaults(struct perf_event_ops
*handler
)
315 if (handler
->sample
== NULL
)
316 handler
->sample
= process_event_sample_stub
;
317 if (handler
->mmap
== NULL
)
318 handler
->mmap
= process_event_stub
;
319 if (handler
->comm
== NULL
)
320 handler
->comm
= process_event_stub
;
321 if (handler
->fork
== NULL
)
322 handler
->fork
= process_event_stub
;
323 if (handler
->exit
== NULL
)
324 handler
->exit
= process_event_stub
;
325 if (handler
->lost
== NULL
)
326 handler
->lost
= perf_event__process_lost
;
327 if (handler
->read
== NULL
)
328 handler
->read
= process_event_stub
;
329 if (handler
->throttle
== NULL
)
330 handler
->throttle
= process_event_stub
;
331 if (handler
->unthrottle
== NULL
)
332 handler
->unthrottle
= process_event_stub
;
333 if (handler
->attr
== NULL
)
334 handler
->attr
= process_event_synth_stub
;
335 if (handler
->event_type
== NULL
)
336 handler
->event_type
= process_event_synth_stub
;
337 if (handler
->tracing_data
== NULL
)
338 handler
->tracing_data
= process_event_synth_stub
;
339 if (handler
->build_id
== NULL
)
340 handler
->build_id
= process_event_synth_stub
;
341 if (handler
->finished_round
== NULL
) {
342 if (handler
->ordered_samples
)
343 handler
->finished_round
= process_finished_round
;
345 handler
->finished_round
= process_finished_round_stub
;
349 void mem_bswap_64(void *src
, int byte_size
)
353 while (byte_size
> 0) {
355 byte_size
-= sizeof(u64
);
360 static void perf_event__all64_swap(union perf_event
*event
)
362 struct perf_event_header
*hdr
= &event
->header
;
363 mem_bswap_64(hdr
+ 1, event
->header
.size
- sizeof(*hdr
));
366 static void perf_event__comm_swap(union perf_event
*event
)
368 event
->comm
.pid
= bswap_32(event
->comm
.pid
);
369 event
->comm
.tid
= bswap_32(event
->comm
.tid
);
372 static void perf_event__mmap_swap(union perf_event
*event
)
374 event
->mmap
.pid
= bswap_32(event
->mmap
.pid
);
375 event
->mmap
.tid
= bswap_32(event
->mmap
.tid
);
376 event
->mmap
.start
= bswap_64(event
->mmap
.start
);
377 event
->mmap
.len
= bswap_64(event
->mmap
.len
);
378 event
->mmap
.pgoff
= bswap_64(event
->mmap
.pgoff
);
381 static void perf_event__task_swap(union perf_event
*event
)
383 event
->fork
.pid
= bswap_32(event
->fork
.pid
);
384 event
->fork
.tid
= bswap_32(event
->fork
.tid
);
385 event
->fork
.ppid
= bswap_32(event
->fork
.ppid
);
386 event
->fork
.ptid
= bswap_32(event
->fork
.ptid
);
387 event
->fork
.time
= bswap_64(event
->fork
.time
);
390 static void perf_event__read_swap(union perf_event
*event
)
392 event
->read
.pid
= bswap_32(event
->read
.pid
);
393 event
->read
.tid
= bswap_32(event
->read
.tid
);
394 event
->read
.value
= bswap_64(event
->read
.value
);
395 event
->read
.time_enabled
= bswap_64(event
->read
.time_enabled
);
396 event
->read
.time_running
= bswap_64(event
->read
.time_running
);
397 event
->read
.id
= bswap_64(event
->read
.id
);
400 static void perf_event__attr_swap(union perf_event
*event
)
404 event
->attr
.attr
.type
= bswap_32(event
->attr
.attr
.type
);
405 event
->attr
.attr
.size
= bswap_32(event
->attr
.attr
.size
);
406 event
->attr
.attr
.config
= bswap_64(event
->attr
.attr
.config
);
407 event
->attr
.attr
.sample_period
= bswap_64(event
->attr
.attr
.sample_period
);
408 event
->attr
.attr
.sample_type
= bswap_64(event
->attr
.attr
.sample_type
);
409 event
->attr
.attr
.read_format
= bswap_64(event
->attr
.attr
.read_format
);
410 event
->attr
.attr
.wakeup_events
= bswap_32(event
->attr
.attr
.wakeup_events
);
411 event
->attr
.attr
.bp_type
= bswap_32(event
->attr
.attr
.bp_type
);
412 event
->attr
.attr
.bp_addr
= bswap_64(event
->attr
.attr
.bp_addr
);
413 event
->attr
.attr
.bp_len
= bswap_64(event
->attr
.attr
.bp_len
);
415 size
= event
->header
.size
;
416 size
-= (void *)&event
->attr
.id
- (void *)event
;
417 mem_bswap_64(event
->attr
.id
, size
);
420 static void perf_event__event_type_swap(union perf_event
*event
)
422 event
->event_type
.event_type
.event_id
=
423 bswap_64(event
->event_type
.event_type
.event_id
);
426 static void perf_event__tracing_data_swap(union perf_event
*event
)
428 event
->tracing_data
.size
= bswap_32(event
->tracing_data
.size
);
431 typedef void (*perf_event__swap_op
)(union perf_event
*event
);
433 static perf_event__swap_op perf_event__swap_ops
[] = {
434 [PERF_RECORD_MMAP
] = perf_event__mmap_swap
,
435 [PERF_RECORD_COMM
] = perf_event__comm_swap
,
436 [PERF_RECORD_FORK
] = perf_event__task_swap
,
437 [PERF_RECORD_EXIT
] = perf_event__task_swap
,
438 [PERF_RECORD_LOST
] = perf_event__all64_swap
,
439 [PERF_RECORD_READ
] = perf_event__read_swap
,
440 [PERF_RECORD_SAMPLE
] = perf_event__all64_swap
,
441 [PERF_RECORD_HEADER_ATTR
] = perf_event__attr_swap
,
442 [PERF_RECORD_HEADER_EVENT_TYPE
] = perf_event__event_type_swap
,
443 [PERF_RECORD_HEADER_TRACING_DATA
] = perf_event__tracing_data_swap
,
444 [PERF_RECORD_HEADER_BUILD_ID
] = NULL
,
445 [PERF_RECORD_HEADER_MAX
] = NULL
,
448 struct sample_queue
{
451 union perf_event
*event
;
452 struct list_head list
;
455 static void perf_session_free_sample_buffers(struct perf_session
*session
)
457 struct ordered_samples
*os
= &session
->ordered_samples
;
459 while (!list_empty(&os
->to_free
)) {
460 struct sample_queue
*sq
;
462 sq
= list_entry(os
->to_free
.next
, struct sample_queue
, list
);
468 static int perf_session_deliver_event(struct perf_session
*session
,
469 union perf_event
*event
,
470 struct perf_sample
*sample
,
471 struct perf_event_ops
*ops
,
474 static void flush_sample_queue(struct perf_session
*s
,
475 struct perf_event_ops
*ops
)
477 struct ordered_samples
*os
= &s
->ordered_samples
;
478 struct list_head
*head
= &os
->samples
;
479 struct sample_queue
*tmp
, *iter
;
480 struct perf_sample sample
;
481 u64 limit
= os
->next_flush
;
482 u64 last_ts
= os
->last_sample
? os
->last_sample
->timestamp
: 0ULL;
485 if (!ops
->ordered_samples
|| !limit
)
488 list_for_each_entry_safe(iter
, tmp
, head
, list
) {
489 if (iter
->timestamp
> limit
)
492 ret
= perf_session__parse_sample(s
, iter
->event
, &sample
);
494 pr_err("Can't parse sample, err = %d\n", ret
);
496 perf_session_deliver_event(s
, iter
->event
, &sample
, ops
,
499 os
->last_flush
= iter
->timestamp
;
500 list_del(&iter
->list
);
501 list_add(&iter
->list
, &os
->sample_cache
);
504 if (list_empty(head
)) {
505 os
->last_sample
= NULL
;
506 } else if (last_ts
<= limit
) {
508 list_entry(head
->prev
, struct sample_queue
, list
);
513 * When perf record finishes a pass on every buffers, it records this pseudo
515 * We record the max timestamp t found in the pass n.
516 * Assuming these timestamps are monotonic across cpus, we know that if
517 * a buffer still has events with timestamps below t, they will be all
518 * available and then read in the pass n + 1.
519 * Hence when we start to read the pass n + 2, we can safely flush every
520 * events with timestamps below t.
522 * ============ PASS n =================
525 * cnt1 timestamps | cnt2 timestamps
528 * - | 4 <--- max recorded
530 * ============ PASS n + 1 ==============
533 * cnt1 timestamps | cnt2 timestamps
536 * 5 | 7 <---- max recorded
538 * Flush every events below timestamp 4
540 * ============ PASS n + 2 ==============
543 * cnt1 timestamps | cnt2 timestamps
548 * Flush every events below timestamp 7
551 static int process_finished_round(union perf_event
*event __used
,
552 struct perf_session
*session
,
553 struct perf_event_ops
*ops
)
555 flush_sample_queue(session
, ops
);
556 session
->ordered_samples
.next_flush
= session
->ordered_samples
.max_timestamp
;
561 /* The queue is ordered by time */
562 static void __queue_event(struct sample_queue
*new, struct perf_session
*s
)
564 struct ordered_samples
*os
= &s
->ordered_samples
;
565 struct sample_queue
*sample
= os
->last_sample
;
566 u64 timestamp
= new->timestamp
;
569 os
->last_sample
= new;
572 list_add(&new->list
, &os
->samples
);
573 os
->max_timestamp
= timestamp
;
578 * last_sample might point to some random place in the list as it's
579 * the last queued event. We expect that the new event is close to
582 if (sample
->timestamp
<= timestamp
) {
583 while (sample
->timestamp
<= timestamp
) {
584 p
= sample
->list
.next
;
585 if (p
== &os
->samples
) {
586 list_add_tail(&new->list
, &os
->samples
);
587 os
->max_timestamp
= timestamp
;
590 sample
= list_entry(p
, struct sample_queue
, list
);
592 list_add_tail(&new->list
, &sample
->list
);
594 while (sample
->timestamp
> timestamp
) {
595 p
= sample
->list
.prev
;
596 if (p
== &os
->samples
) {
597 list_add(&new->list
, &os
->samples
);
600 sample
= list_entry(p
, struct sample_queue
, list
);
602 list_add(&new->list
, &sample
->list
);
606 #define MAX_SAMPLE_BUFFER (64 * 1024 / sizeof(struct sample_queue))
608 static int perf_session_queue_event(struct perf_session
*s
, union perf_event
*event
,
609 struct perf_sample
*sample
, u64 file_offset
)
611 struct ordered_samples
*os
= &s
->ordered_samples
;
612 struct list_head
*sc
= &os
->sample_cache
;
613 u64 timestamp
= sample
->time
;
614 struct sample_queue
*new;
616 if (!timestamp
|| timestamp
== ~0ULL)
619 if (timestamp
< s
->ordered_samples
.last_flush
) {
620 printf("Warning: Timestamp below last timeslice flush\n");
624 if (!list_empty(sc
)) {
625 new = list_entry(sc
->next
, struct sample_queue
, list
);
626 list_del(&new->list
);
627 } else if (os
->sample_buffer
) {
628 new = os
->sample_buffer
+ os
->sample_buffer_idx
;
629 if (++os
->sample_buffer_idx
== MAX_SAMPLE_BUFFER
)
630 os
->sample_buffer
= NULL
;
632 os
->sample_buffer
= malloc(MAX_SAMPLE_BUFFER
* sizeof(*new));
633 if (!os
->sample_buffer
)
635 list_add(&os
->sample_buffer
->list
, &os
->to_free
);
636 os
->sample_buffer_idx
= 2;
637 new = os
->sample_buffer
+ 1;
640 new->timestamp
= timestamp
;
641 new->file_offset
= file_offset
;
644 __queue_event(new, s
);
649 static void callchain__printf(struct perf_sample
*sample
)
653 printf("... chain: nr:%" PRIu64
"\n", sample
->callchain
->nr
);
655 for (i
= 0; i
< sample
->callchain
->nr
; i
++)
656 printf("..... %2d: %016" PRIx64
"\n",
657 i
, sample
->callchain
->ips
[i
]);
660 static void perf_session__print_tstamp(struct perf_session
*session
,
661 union perf_event
*event
,
662 struct perf_sample
*sample
)
664 if (event
->header
.type
!= PERF_RECORD_SAMPLE
&&
665 !session
->sample_id_all
) {
666 fputs("-1 -1 ", stdout
);
670 if ((session
->sample_type
& PERF_SAMPLE_CPU
))
671 printf("%u ", sample
->cpu
);
673 if (session
->sample_type
& PERF_SAMPLE_TIME
)
674 printf("%" PRIu64
" ", sample
->time
);
677 static void dump_event(struct perf_session
*session
, union perf_event
*event
,
678 u64 file_offset
, struct perf_sample
*sample
)
683 printf("\n%#" PRIx64
" [%#x]: event: %d\n",
684 file_offset
, event
->header
.size
, event
->header
.type
);
689 perf_session__print_tstamp(session
, event
, sample
);
691 printf("%#" PRIx64
" [%#x]: PERF_RECORD_%s", file_offset
,
692 event
->header
.size
, perf_event__name(event
->header
.type
));
695 static void dump_sample(struct perf_session
*session
, union perf_event
*event
,
696 struct perf_sample
*sample
)
701 printf("(IP, %d): %d/%d: %#" PRIx64
" period: %" PRIu64
"\n",
702 event
->header
.misc
, sample
->pid
, sample
->tid
, sample
->ip
,
705 if (session
->sample_type
& PERF_SAMPLE_CALLCHAIN
)
706 callchain__printf(sample
);
709 static int perf_session_deliver_event(struct perf_session
*session
,
710 union perf_event
*event
,
711 struct perf_sample
*sample
,
712 struct perf_event_ops
*ops
,
715 struct perf_evsel
*evsel
;
717 dump_event(session
, event
, file_offset
, sample
);
719 switch (event
->header
.type
) {
720 case PERF_RECORD_SAMPLE
:
721 dump_sample(session
, event
, sample
);
722 evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
724 ++session
->hists
.stats
.nr_unknown_id
;
727 return ops
->sample(event
, sample
, evsel
, session
);
728 case PERF_RECORD_MMAP
:
729 return ops
->mmap(event
, sample
, session
);
730 case PERF_RECORD_COMM
:
731 return ops
->comm(event
, sample
, session
);
732 case PERF_RECORD_FORK
:
733 return ops
->fork(event
, sample
, session
);
734 case PERF_RECORD_EXIT
:
735 return ops
->exit(event
, sample
, session
);
736 case PERF_RECORD_LOST
:
737 return ops
->lost(event
, sample
, session
);
738 case PERF_RECORD_READ
:
739 return ops
->read(event
, sample
, session
);
740 case PERF_RECORD_THROTTLE
:
741 return ops
->throttle(event
, sample
, session
);
742 case PERF_RECORD_UNTHROTTLE
:
743 return ops
->unthrottle(event
, sample
, session
);
745 ++session
->hists
.stats
.nr_unknown_events
;
750 static int perf_session__preprocess_sample(struct perf_session
*session
,
751 union perf_event
*event
, struct perf_sample
*sample
)
753 if (event
->header
.type
!= PERF_RECORD_SAMPLE
||
754 !(session
->sample_type
& PERF_SAMPLE_CALLCHAIN
))
757 if (!ip_callchain__valid(sample
->callchain
, event
)) {
758 pr_debug("call-chain problem with event, skipping it.\n");
759 ++session
->hists
.stats
.nr_invalid_chains
;
760 session
->hists
.stats
.total_invalid_chains
+= sample
->period
;
766 static int perf_session__process_user_event(struct perf_session
*session
, union perf_event
*event
,
767 struct perf_event_ops
*ops
, u64 file_offset
)
769 dump_event(session
, event
, file_offset
, NULL
);
771 /* These events are processed right away */
772 switch (event
->header
.type
) {
773 case PERF_RECORD_HEADER_ATTR
:
774 return ops
->attr(event
, session
);
775 case PERF_RECORD_HEADER_EVENT_TYPE
:
776 return ops
->event_type(event
, session
);
777 case PERF_RECORD_HEADER_TRACING_DATA
:
778 /* setup for reading amidst mmap */
779 lseek(session
->fd
, file_offset
, SEEK_SET
);
780 return ops
->tracing_data(event
, session
);
781 case PERF_RECORD_HEADER_BUILD_ID
:
782 return ops
->build_id(event
, session
);
783 case PERF_RECORD_FINISHED_ROUND
:
784 return ops
->finished_round(event
, session
, ops
);
790 static int perf_session__process_event(struct perf_session
*session
,
791 union perf_event
*event
,
792 struct perf_event_ops
*ops
,
795 struct perf_sample sample
;
798 if (session
->header
.needs_swap
&&
799 perf_event__swap_ops
[event
->header
.type
])
800 perf_event__swap_ops
[event
->header
.type
](event
);
802 if (event
->header
.type
>= PERF_RECORD_HEADER_MAX
)
805 hists__inc_nr_events(&session
->hists
, event
->header
.type
);
807 if (event
->header
.type
>= PERF_RECORD_USER_TYPE_START
)
808 return perf_session__process_user_event(session
, event
, ops
, file_offset
);
811 * For all kernel events we get the sample data
813 ret
= perf_session__parse_sample(session
, event
, &sample
);
817 /* Preprocess sample records - precheck callchains */
818 if (perf_session__preprocess_sample(session
, event
, &sample
))
821 if (ops
->ordered_samples
) {
822 ret
= perf_session_queue_event(session
, event
, &sample
,
828 return perf_session_deliver_event(session
, event
, &sample
, ops
,
832 void perf_event_header__bswap(struct perf_event_header
*self
)
834 self
->type
= bswap_32(self
->type
);
835 self
->misc
= bswap_16(self
->misc
);
836 self
->size
= bswap_16(self
->size
);
839 static struct thread
*perf_session__register_idle_thread(struct perf_session
*self
)
841 struct thread
*thread
= perf_session__findnew(self
, 0);
843 if (thread
== NULL
|| thread__set_comm(thread
, "swapper")) {
844 pr_err("problem inserting idle task.\n");
851 static void perf_session__warn_about_errors(const struct perf_session
*session
,
852 const struct perf_event_ops
*ops
)
854 if (ops
->lost
== perf_event__process_lost
&&
855 session
->hists
.stats
.total_lost
!= 0) {
856 ui__warning("Processed %" PRIu64
" events and LOST %" PRIu64
857 "!\n\nCheck IO/CPU overload!\n\n",
858 session
->hists
.stats
.total_period
,
859 session
->hists
.stats
.total_lost
);
862 if (session
->hists
.stats
.nr_unknown_events
!= 0) {
863 ui__warning("Found %u unknown events!\n\n"
864 "Is this an older tool processing a perf.data "
865 "file generated by a more recent tool?\n\n"
866 "If that is not the case, consider "
867 "reporting to linux-kernel@vger.kernel.org.\n\n",
868 session
->hists
.stats
.nr_unknown_events
);
871 if (session
->hists
.stats
.nr_unknown_id
!= 0) {
872 ui__warning("%u samples with id not present in the header\n",
873 session
->hists
.stats
.nr_unknown_id
);
876 if (session
->hists
.stats
.nr_invalid_chains
!= 0) {
877 ui__warning("Found invalid callchains!\n\n"
878 "%u out of %u events were discarded for this reason.\n\n"
879 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
880 session
->hists
.stats
.nr_invalid_chains
,
881 session
->hists
.stats
.nr_events
[PERF_RECORD_SAMPLE
]);
885 #define session_done() (*(volatile int *)(&session_done))
886 volatile int session_done
;
888 static int __perf_session__process_pipe_events(struct perf_session
*self
,
889 struct perf_event_ops
*ops
)
891 union perf_event event
;
898 perf_event_ops__fill_defaults(ops
);
902 err
= readn(self
->fd
, &event
, sizeof(struct perf_event_header
));
907 pr_err("failed to read event header\n");
911 if (self
->header
.needs_swap
)
912 perf_event_header__bswap(&event
.header
);
914 size
= event
.header
.size
;
919 p
+= sizeof(struct perf_event_header
);
921 if (size
- sizeof(struct perf_event_header
)) {
922 err
= readn(self
->fd
, p
, size
- sizeof(struct perf_event_header
));
925 pr_err("unexpected end of event stream\n");
929 pr_err("failed to read event data\n");
935 (skip
= perf_session__process_event(self
, &event
, ops
, head
)) < 0) {
936 dump_printf("%#" PRIx64
" [%#x]: skipping unknown header type: %d\n",
937 head
, event
.header
.size
, event
.header
.type
);
939 * assume we lost track of the stream, check alignment, and
940 * increment a single u64 in the hope to catch on again 'soon'.
942 if (unlikely(head
& 7))
958 perf_session__warn_about_errors(self
, ops
);
959 perf_session_free_sample_buffers(self
);
963 static union perf_event
*
964 fetch_mmaped_event(struct perf_session
*session
,
965 u64 head
, size_t mmap_size
, char *buf
)
967 union perf_event
*event
;
970 * Ensure we have enough space remaining to read
971 * the size of the event in the headers.
973 if (head
+ sizeof(event
->header
) > mmap_size
)
976 event
= (union perf_event
*)(buf
+ head
);
978 if (session
->header
.needs_swap
)
979 perf_event_header__bswap(&event
->header
);
981 if (head
+ event
->header
.size
> mmap_size
)
987 int __perf_session__process_events(struct perf_session
*session
,
988 u64 data_offset
, u64 data_size
,
989 u64 file_size
, struct perf_event_ops
*ops
)
991 u64 head
, page_offset
, file_offset
, file_pos
, progress_next
;
992 int err
, mmap_prot
, mmap_flags
, map_idx
= 0;
993 struct ui_progress
*progress
;
994 size_t page_size
, mmap_size
;
995 char *buf
, *mmaps
[8];
996 union perf_event
*event
;
999 perf_event_ops__fill_defaults(ops
);
1001 page_size
= sysconf(_SC_PAGESIZE
);
1003 page_offset
= page_size
* (data_offset
/ page_size
);
1004 file_offset
= page_offset
;
1005 head
= data_offset
- page_offset
;
1007 if (data_offset
+ data_size
< file_size
)
1008 file_size
= data_offset
+ data_size
;
1010 progress_next
= file_size
/ 16;
1011 progress
= ui_progress__new("Processing events...", file_size
);
1012 if (progress
== NULL
)
1015 mmap_size
= session
->mmap_window
;
1016 if (mmap_size
> file_size
)
1017 mmap_size
= file_size
;
1019 memset(mmaps
, 0, sizeof(mmaps
));
1021 mmap_prot
= PROT_READ
;
1022 mmap_flags
= MAP_SHARED
;
1024 if (session
->header
.needs_swap
) {
1025 mmap_prot
|= PROT_WRITE
;
1026 mmap_flags
= MAP_PRIVATE
;
1029 buf
= mmap(NULL
, mmap_size
, mmap_prot
, mmap_flags
, session
->fd
,
1031 if (buf
== MAP_FAILED
) {
1032 pr_err("failed to mmap file\n");
1036 mmaps
[map_idx
] = buf
;
1037 map_idx
= (map_idx
+ 1) & (ARRAY_SIZE(mmaps
) - 1);
1038 file_pos
= file_offset
+ head
;
1041 event
= fetch_mmaped_event(session
, head
, mmap_size
, buf
);
1043 if (mmaps
[map_idx
]) {
1044 munmap(mmaps
[map_idx
], mmap_size
);
1045 mmaps
[map_idx
] = NULL
;
1048 page_offset
= page_size
* (head
/ page_size
);
1049 file_offset
+= page_offset
;
1050 head
-= page_offset
;
1054 size
= event
->header
.size
;
1057 perf_session__process_event(session
, event
, ops
, file_pos
) < 0) {
1058 dump_printf("%#" PRIx64
" [%#x]: skipping unknown header type: %d\n",
1059 file_offset
+ head
, event
->header
.size
,
1060 event
->header
.type
);
1062 * assume we lost track of the stream, check alignment, and
1063 * increment a single u64 in the hope to catch on again 'soon'.
1065 if (unlikely(head
& 7))
1074 if (file_pos
>= progress_next
) {
1075 progress_next
+= file_size
/ 16;
1076 ui_progress__update(progress
, file_pos
);
1079 if (file_pos
< file_size
)
1083 /* do the final flush for ordered samples */
1084 session
->ordered_samples
.next_flush
= ULLONG_MAX
;
1085 flush_sample_queue(session
, ops
);
1087 ui_progress__delete(progress
);
1088 perf_session__warn_about_errors(session
, ops
);
1089 perf_session_free_sample_buffers(session
);
1093 int perf_session__process_events(struct perf_session
*self
,
1094 struct perf_event_ops
*ops
)
1098 if (perf_session__register_idle_thread(self
) == NULL
)
1102 err
= __perf_session__process_events(self
,
1103 self
->header
.data_offset
,
1104 self
->header
.data_size
,
1107 err
= __perf_session__process_pipe_events(self
, ops
);
1112 bool perf_session__has_traces(struct perf_session
*self
, const char *msg
)
1114 if (!(self
->sample_type
& PERF_SAMPLE_RAW
)) {
1115 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg
);
1122 int perf_session__set_kallsyms_ref_reloc_sym(struct map
**maps
,
1123 const char *symbol_name
,
1128 struct ref_reloc_sym
*ref
;
1130 ref
= zalloc(sizeof(struct ref_reloc_sym
));
1134 ref
->name
= strdup(symbol_name
);
1135 if (ref
->name
== NULL
) {
1140 bracket
= strchr(ref
->name
, ']');
1146 for (i
= 0; i
< MAP__NR_TYPES
; ++i
) {
1147 struct kmap
*kmap
= map__kmap(maps
[i
]);
1148 kmap
->ref_reloc_sym
= ref
;
1154 size_t perf_session__fprintf_dsos(struct perf_session
*self
, FILE *fp
)
1156 return __dsos__fprintf(&self
->host_machine
.kernel_dsos
, fp
) +
1157 __dsos__fprintf(&self
->host_machine
.user_dsos
, fp
) +
1158 machines__fprintf_dsos(&self
->machines
, fp
);
1161 size_t perf_session__fprintf_dsos_buildid(struct perf_session
*self
, FILE *fp
,
1164 size_t ret
= machine__fprintf_dsos_buildid(&self
->host_machine
, fp
, with_hits
);
1165 return ret
+ machines__fprintf_dsos_buildid(&self
->machines
, fp
, with_hits
);
1168 size_t perf_session__fprintf_nr_events(struct perf_session
*session
, FILE *fp
)
1170 struct perf_evsel
*pos
;
1171 size_t ret
= fprintf(fp
, "Aggregated stats:\n");
1173 ret
+= hists__fprintf_nr_events(&session
->hists
, fp
);
1175 list_for_each_entry(pos
, &session
->evlist
->entries
, node
) {
1176 ret
+= fprintf(fp
, "%s stats:\n", event_name(pos
));
1177 ret
+= hists__fprintf_nr_events(&pos
->hists
, fp
);
1183 struct perf_evsel
*perf_session__find_first_evtype(struct perf_session
*session
,
1186 struct perf_evsel
*pos
;
1188 list_for_each_entry(pos
, &session
->evlist
->entries
, node
) {
1189 if (pos
->attr
.type
== type
)
1195 void perf_session__print_symbols(union perf_event
*event
,
1196 struct perf_sample
*sample
,
1197 struct perf_session
*session
)
1199 struct addr_location al
;
1200 const char *symname
, *dsoname
;
1201 struct callchain_cursor
*cursor
= &session
->callchain_cursor
;
1202 struct callchain_cursor_node
*node
;
1204 if (perf_event__preprocess_sample(event
, session
, &al
, sample
,
1206 error("problem processing %d event, skipping it.\n",
1207 event
->header
.type
);
1211 if (symbol_conf
.use_callchain
&& sample
->callchain
) {
1213 if (perf_session__resolve_callchain(session
, al
.thread
,
1214 sample
->callchain
, NULL
) != 0) {
1216 error("Failed to resolve callchain. Skipping\n");
1219 callchain_cursor_commit(cursor
);
1222 node
= callchain_cursor_current(cursor
);
1226 if (node
->sym
&& node
->sym
->name
)
1227 symname
= node
->sym
->name
;
1231 if (node
->map
&& node
->map
->dso
&& node
->map
->dso
->name
)
1232 dsoname
= node
->map
->dso
->name
;
1236 printf("\t%16" PRIx64
" %s (%s)\n", node
->ip
, symname
, dsoname
);
1238 callchain_cursor_advance(cursor
);
1242 if (al
.sym
&& al
.sym
->name
)
1243 symname
= al
.sym
->name
;
1247 if (al
.map
&& al
.map
->dso
&& al
.map
->dso
->name
)
1248 dsoname
= al
.map
->dso
->name
;
1252 printf("%16" PRIx64
" %s (%s)", al
.addr
, symname
, dsoname
);