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");
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");
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
);
144 if (mode
== O_RDONLY
) {
145 if (perf_session__open(self
, force
) < 0)
147 perf_session__update_sample_type(self
);
148 } else if (mode
== O_WRONLY
) {
150 * In O_RDONLY mode this will be performed when reading the
151 * kernel MMAP event, in perf_event__process_mmap().
153 if (perf_session__create_kernel_maps(self
) < 0)
157 if (tool
&& tool
->ordering_requires_timestamps
&&
158 tool
->ordered_samples
&& !self
->sample_id_all
) {
159 dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n");
160 tool
->ordered_samples
= false;
166 perf_session__delete(self
);
170 static void machine__delete_dead_threads(struct machine
*machine
)
172 struct thread
*n
, *t
;
174 list_for_each_entry_safe(t
, n
, &machine
->dead_threads
, node
) {
180 static void perf_session__delete_dead_threads(struct perf_session
*session
)
182 machine__delete_dead_threads(&session
->host_machine
);
185 static void machine__delete_threads(struct machine
*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 static void perf_session__delete_threads(struct perf_session
*session
)
200 machine__delete_threads(&session
->host_machine
);
203 void perf_session__delete(struct perf_session
*self
)
205 perf_session__destroy_kernel_maps(self
);
206 perf_session__delete_dead_threads(self
);
207 perf_session__delete_threads(self
);
208 machine__exit(&self
->host_machine
);
213 void machine__remove_thread(struct machine
*self
, struct thread
*th
)
215 self
->last_match
= NULL
;
216 rb_erase(&th
->rb_node
, &self
->threads
);
218 * We may have references to this thread, for instance in some hist_entry
219 * instances, so just move them to a separate list.
221 list_add_tail(&th
->node
, &self
->dead_threads
);
224 static bool symbol__match_parent_regex(struct symbol
*sym
)
226 if (sym
->name
&& !regexec(&parent_regex
, sym
->name
, 0, NULL
, 0))
232 int machine__resolve_callchain(struct machine
*self
, struct perf_evsel
*evsel
,
233 struct thread
*thread
,
234 struct ip_callchain
*chain
,
235 struct symbol
**parent
)
237 u8 cpumode
= PERF_RECORD_MISC_USER
;
241 callchain_cursor_reset(&evsel
->hists
.callchain_cursor
);
243 for (i
= 0; i
< chain
->nr
; i
++) {
245 struct addr_location al
;
247 if (callchain_param
.order
== ORDER_CALLEE
)
250 ip
= chain
->ips
[chain
->nr
- i
- 1];
252 if (ip
>= PERF_CONTEXT_MAX
) {
254 case PERF_CONTEXT_HV
:
255 cpumode
= PERF_RECORD_MISC_HYPERVISOR
; break;
256 case PERF_CONTEXT_KERNEL
:
257 cpumode
= PERF_RECORD_MISC_KERNEL
; break;
258 case PERF_CONTEXT_USER
:
259 cpumode
= PERF_RECORD_MISC_USER
; break;
267 thread__find_addr_location(thread
, self
, cpumode
,
268 MAP__FUNCTION
, ip
, &al
, NULL
);
269 if (al
.sym
!= NULL
) {
270 if (sort__has_parent
&& !*parent
&&
271 symbol__match_parent_regex(al
.sym
))
273 if (!symbol_conf
.use_callchain
)
277 err
= callchain_cursor_append(&evsel
->hists
.callchain_cursor
,
286 static int process_event_synth_tracing_data_stub(union perf_event
*event __used
,
287 struct perf_session
*session __used
)
289 dump_printf(": unhandled!\n");
293 static int process_event_synth_attr_stub(union perf_event
*event __used
,
294 struct perf_evlist
**pevlist __used
)
296 dump_printf(": unhandled!\n");
300 static int process_event_sample_stub(struct perf_tool
*tool __used
,
301 union perf_event
*event __used
,
302 struct perf_sample
*sample __used
,
303 struct perf_evsel
*evsel __used
,
304 struct machine
*machine __used
)
306 dump_printf(": unhandled!\n");
310 static int process_event_stub(struct perf_tool
*tool __used
,
311 union perf_event
*event __used
,
312 struct perf_sample
*sample __used
,
313 struct machine
*machine __used
)
315 dump_printf(": unhandled!\n");
319 static int process_finished_round_stub(struct perf_tool
*tool __used
,
320 union perf_event
*event __used
,
321 struct perf_session
*perf_session __used
)
323 dump_printf(": unhandled!\n");
327 static int process_event_type_stub(struct perf_tool
*tool __used
,
328 union perf_event
*event __used
)
330 dump_printf(": unhandled!\n");
334 static int process_finished_round(struct perf_tool
*tool
,
335 union perf_event
*event
,
336 struct perf_session
*session
);
338 static void perf_tool__fill_defaults(struct perf_tool
*tool
)
340 if (tool
->sample
== NULL
)
341 tool
->sample
= process_event_sample_stub
;
342 if (tool
->mmap
== NULL
)
343 tool
->mmap
= process_event_stub
;
344 if (tool
->comm
== NULL
)
345 tool
->comm
= process_event_stub
;
346 if (tool
->fork
== NULL
)
347 tool
->fork
= process_event_stub
;
348 if (tool
->exit
== NULL
)
349 tool
->exit
= process_event_stub
;
350 if (tool
->lost
== NULL
)
351 tool
->lost
= perf_event__process_lost
;
352 if (tool
->read
== NULL
)
353 tool
->read
= process_event_sample_stub
;
354 if (tool
->throttle
== NULL
)
355 tool
->throttle
= process_event_stub
;
356 if (tool
->unthrottle
== NULL
)
357 tool
->unthrottle
= process_event_stub
;
358 if (tool
->attr
== NULL
)
359 tool
->attr
= process_event_synth_attr_stub
;
360 if (tool
->event_type
== NULL
)
361 tool
->event_type
= process_event_type_stub
;
362 if (tool
->tracing_data
== NULL
)
363 tool
->tracing_data
= process_event_synth_tracing_data_stub
;
364 if (tool
->build_id
== NULL
)
365 tool
->build_id
= process_finished_round_stub
;
366 if (tool
->finished_round
== NULL
) {
367 if (tool
->ordered_samples
)
368 tool
->finished_round
= process_finished_round
;
370 tool
->finished_round
= process_finished_round_stub
;
374 void mem_bswap_64(void *src
, int byte_size
)
378 while (byte_size
> 0) {
380 byte_size
-= sizeof(u64
);
385 static void perf_event__all64_swap(union perf_event
*event
)
387 struct perf_event_header
*hdr
= &event
->header
;
388 mem_bswap_64(hdr
+ 1, event
->header
.size
- sizeof(*hdr
));
391 static void perf_event__comm_swap(union perf_event
*event
)
393 event
->comm
.pid
= bswap_32(event
->comm
.pid
);
394 event
->comm
.tid
= bswap_32(event
->comm
.tid
);
397 static void perf_event__mmap_swap(union perf_event
*event
)
399 event
->mmap
.pid
= bswap_32(event
->mmap
.pid
);
400 event
->mmap
.tid
= bswap_32(event
->mmap
.tid
);
401 event
->mmap
.start
= bswap_64(event
->mmap
.start
);
402 event
->mmap
.len
= bswap_64(event
->mmap
.len
);
403 event
->mmap
.pgoff
= bswap_64(event
->mmap
.pgoff
);
406 static void perf_event__task_swap(union perf_event
*event
)
408 event
->fork
.pid
= bswap_32(event
->fork
.pid
);
409 event
->fork
.tid
= bswap_32(event
->fork
.tid
);
410 event
->fork
.ppid
= bswap_32(event
->fork
.ppid
);
411 event
->fork
.ptid
= bswap_32(event
->fork
.ptid
);
412 event
->fork
.time
= bswap_64(event
->fork
.time
);
415 static void perf_event__read_swap(union perf_event
*event
)
417 event
->read
.pid
= bswap_32(event
->read
.pid
);
418 event
->read
.tid
= bswap_32(event
->read
.tid
);
419 event
->read
.value
= bswap_64(event
->read
.value
);
420 event
->read
.time_enabled
= bswap_64(event
->read
.time_enabled
);
421 event
->read
.time_running
= bswap_64(event
->read
.time_running
);
422 event
->read
.id
= bswap_64(event
->read
.id
);
425 /* exported for swapping attributes in file header */
426 void perf_event__attr_swap(struct perf_event_attr
*attr
)
428 attr
->type
= bswap_32(attr
->type
);
429 attr
->size
= bswap_32(attr
->size
);
430 attr
->config
= bswap_64(attr
->config
);
431 attr
->sample_period
= bswap_64(attr
->sample_period
);
432 attr
->sample_type
= bswap_64(attr
->sample_type
);
433 attr
->read_format
= bswap_64(attr
->read_format
);
434 attr
->wakeup_events
= bswap_32(attr
->wakeup_events
);
435 attr
->bp_type
= bswap_32(attr
->bp_type
);
436 attr
->bp_addr
= bswap_64(attr
->bp_addr
);
437 attr
->bp_len
= bswap_64(attr
->bp_len
);
440 static void perf_event__hdr_attr_swap(union perf_event
*event
)
444 perf_event__attr_swap(&event
->attr
.attr
);
446 size
= event
->header
.size
;
447 size
-= (void *)&event
->attr
.id
- (void *)event
;
448 mem_bswap_64(event
->attr
.id
, size
);
451 static void perf_event__event_type_swap(union perf_event
*event
)
453 event
->event_type
.event_type
.event_id
=
454 bswap_64(event
->event_type
.event_type
.event_id
);
457 static void perf_event__tracing_data_swap(union perf_event
*event
)
459 event
->tracing_data
.size
= bswap_32(event
->tracing_data
.size
);
462 typedef void (*perf_event__swap_op
)(union perf_event
*event
);
464 static perf_event__swap_op perf_event__swap_ops
[] = {
465 [PERF_RECORD_MMAP
] = perf_event__mmap_swap
,
466 [PERF_RECORD_COMM
] = perf_event__comm_swap
,
467 [PERF_RECORD_FORK
] = perf_event__task_swap
,
468 [PERF_RECORD_EXIT
] = perf_event__task_swap
,
469 [PERF_RECORD_LOST
] = perf_event__all64_swap
,
470 [PERF_RECORD_READ
] = perf_event__read_swap
,
471 [PERF_RECORD_SAMPLE
] = perf_event__all64_swap
,
472 [PERF_RECORD_HEADER_ATTR
] = perf_event__hdr_attr_swap
,
473 [PERF_RECORD_HEADER_EVENT_TYPE
] = perf_event__event_type_swap
,
474 [PERF_RECORD_HEADER_TRACING_DATA
] = perf_event__tracing_data_swap
,
475 [PERF_RECORD_HEADER_BUILD_ID
] = NULL
,
476 [PERF_RECORD_HEADER_MAX
] = NULL
,
479 struct sample_queue
{
482 union perf_event
*event
;
483 struct list_head list
;
486 static void perf_session_free_sample_buffers(struct perf_session
*session
)
488 struct ordered_samples
*os
= &session
->ordered_samples
;
490 while (!list_empty(&os
->to_free
)) {
491 struct sample_queue
*sq
;
493 sq
= list_entry(os
->to_free
.next
, struct sample_queue
, list
);
499 static int perf_session_deliver_event(struct perf_session
*session
,
500 union perf_event
*event
,
501 struct perf_sample
*sample
,
502 struct perf_tool
*tool
,
505 static void flush_sample_queue(struct perf_session
*s
,
506 struct perf_tool
*tool
)
508 struct ordered_samples
*os
= &s
->ordered_samples
;
509 struct list_head
*head
= &os
->samples
;
510 struct sample_queue
*tmp
, *iter
;
511 struct perf_sample sample
;
512 u64 limit
= os
->next_flush
;
513 u64 last_ts
= os
->last_sample
? os
->last_sample
->timestamp
: 0ULL;
514 unsigned idx
= 0, progress_next
= os
->nr_samples
/ 16;
517 if (!tool
->ordered_samples
|| !limit
)
520 list_for_each_entry_safe(iter
, tmp
, head
, list
) {
521 if (iter
->timestamp
> limit
)
524 ret
= perf_session__parse_sample(s
, iter
->event
, &sample
);
526 pr_err("Can't parse sample, err = %d\n", ret
);
528 perf_session_deliver_event(s
, iter
->event
, &sample
, tool
,
531 os
->last_flush
= iter
->timestamp
;
532 list_del(&iter
->list
);
533 list_add(&iter
->list
, &os
->sample_cache
);
534 if (++idx
>= progress_next
) {
535 progress_next
+= os
->nr_samples
/ 16;
536 ui_progress__update(idx
, os
->nr_samples
,
537 "Processing time ordered events...");
541 if (list_empty(head
)) {
542 os
->last_sample
= NULL
;
543 } else if (last_ts
<= limit
) {
545 list_entry(head
->prev
, struct sample_queue
, list
);
552 * When perf record finishes a pass on every buffers, it records this pseudo
554 * We record the max timestamp t found in the pass n.
555 * Assuming these timestamps are monotonic across cpus, we know that if
556 * a buffer still has events with timestamps below t, they will be all
557 * available and then read in the pass n + 1.
558 * Hence when we start to read the pass n + 2, we can safely flush every
559 * events with timestamps below t.
561 * ============ PASS n =================
564 * cnt1 timestamps | cnt2 timestamps
567 * - | 4 <--- max recorded
569 * ============ PASS n + 1 ==============
572 * cnt1 timestamps | cnt2 timestamps
575 * 5 | 7 <---- max recorded
577 * Flush every events below timestamp 4
579 * ============ PASS n + 2 ==============
582 * cnt1 timestamps | cnt2 timestamps
587 * Flush every events below timestamp 7
590 static int process_finished_round(struct perf_tool
*tool
,
591 union perf_event
*event __used
,
592 struct perf_session
*session
)
594 flush_sample_queue(session
, tool
);
595 session
->ordered_samples
.next_flush
= session
->ordered_samples
.max_timestamp
;
600 /* The queue is ordered by time */
601 static void __queue_event(struct sample_queue
*new, struct perf_session
*s
)
603 struct ordered_samples
*os
= &s
->ordered_samples
;
604 struct sample_queue
*sample
= os
->last_sample
;
605 u64 timestamp
= new->timestamp
;
609 os
->last_sample
= new;
612 list_add(&new->list
, &os
->samples
);
613 os
->max_timestamp
= timestamp
;
618 * last_sample might point to some random place in the list as it's
619 * the last queued event. We expect that the new event is close to
622 if (sample
->timestamp
<= timestamp
) {
623 while (sample
->timestamp
<= timestamp
) {
624 p
= sample
->list
.next
;
625 if (p
== &os
->samples
) {
626 list_add_tail(&new->list
, &os
->samples
);
627 os
->max_timestamp
= timestamp
;
630 sample
= list_entry(p
, struct sample_queue
, list
);
632 list_add_tail(&new->list
, &sample
->list
);
634 while (sample
->timestamp
> timestamp
) {
635 p
= sample
->list
.prev
;
636 if (p
== &os
->samples
) {
637 list_add(&new->list
, &os
->samples
);
640 sample
= list_entry(p
, struct sample_queue
, list
);
642 list_add(&new->list
, &sample
->list
);
646 #define MAX_SAMPLE_BUFFER (64 * 1024 / sizeof(struct sample_queue))
648 static int perf_session_queue_event(struct perf_session
*s
, union perf_event
*event
,
649 struct perf_sample
*sample
, u64 file_offset
)
651 struct ordered_samples
*os
= &s
->ordered_samples
;
652 struct list_head
*sc
= &os
->sample_cache
;
653 u64 timestamp
= sample
->time
;
654 struct sample_queue
*new;
656 if (!timestamp
|| timestamp
== ~0ULL)
659 if (timestamp
< s
->ordered_samples
.last_flush
) {
660 printf("Warning: Timestamp below last timeslice flush\n");
664 if (!list_empty(sc
)) {
665 new = list_entry(sc
->next
, struct sample_queue
, list
);
666 list_del(&new->list
);
667 } else if (os
->sample_buffer
) {
668 new = os
->sample_buffer
+ os
->sample_buffer_idx
;
669 if (++os
->sample_buffer_idx
== MAX_SAMPLE_BUFFER
)
670 os
->sample_buffer
= NULL
;
672 os
->sample_buffer
= malloc(MAX_SAMPLE_BUFFER
* sizeof(*new));
673 if (!os
->sample_buffer
)
675 list_add(&os
->sample_buffer
->list
, &os
->to_free
);
676 os
->sample_buffer_idx
= 2;
677 new = os
->sample_buffer
+ 1;
680 new->timestamp
= timestamp
;
681 new->file_offset
= file_offset
;
684 __queue_event(new, s
);
689 static void callchain__printf(struct perf_sample
*sample
)
693 printf("... chain: nr:%" PRIu64
"\n", sample
->callchain
->nr
);
695 for (i
= 0; i
< sample
->callchain
->nr
; i
++)
696 printf("..... %2d: %016" PRIx64
"\n",
697 i
, sample
->callchain
->ips
[i
]);
700 static void perf_session__print_tstamp(struct perf_session
*session
,
701 union perf_event
*event
,
702 struct perf_sample
*sample
)
704 if (event
->header
.type
!= PERF_RECORD_SAMPLE
&&
705 !session
->sample_id_all
) {
706 fputs("-1 -1 ", stdout
);
710 if ((session
->sample_type
& PERF_SAMPLE_CPU
))
711 printf("%u ", sample
->cpu
);
713 if (session
->sample_type
& PERF_SAMPLE_TIME
)
714 printf("%" PRIu64
" ", sample
->time
);
717 static void dump_event(struct perf_session
*session
, union perf_event
*event
,
718 u64 file_offset
, struct perf_sample
*sample
)
723 printf("\n%#" PRIx64
" [%#x]: event: %d\n",
724 file_offset
, event
->header
.size
, event
->header
.type
);
729 perf_session__print_tstamp(session
, event
, sample
);
731 printf("%#" PRIx64
" [%#x]: PERF_RECORD_%s", file_offset
,
732 event
->header
.size
, perf_event__name(event
->header
.type
));
735 static void dump_sample(struct perf_session
*session
, union perf_event
*event
,
736 struct perf_sample
*sample
)
741 printf("(IP, %d): %d/%d: %#" PRIx64
" period: %" PRIu64
" addr: %#" PRIx64
"\n",
742 event
->header
.misc
, sample
->pid
, sample
->tid
, sample
->ip
,
743 sample
->period
, sample
->addr
);
745 if (session
->sample_type
& PERF_SAMPLE_CALLCHAIN
)
746 callchain__printf(sample
);
749 static struct machine
*
750 perf_session__find_machine_for_cpumode(struct perf_session
*session
,
751 union perf_event
*event
)
753 const u8 cpumode
= event
->header
.misc
& PERF_RECORD_MISC_CPUMODE_MASK
;
755 if (cpumode
== PERF_RECORD_MISC_GUEST_KERNEL
&& perf_guest
)
756 return perf_session__find_machine(session
, event
->ip
.pid
);
758 return perf_session__find_host_machine(session
);
761 static int perf_session_deliver_event(struct perf_session
*session
,
762 union perf_event
*event
,
763 struct perf_sample
*sample
,
764 struct perf_tool
*tool
,
767 struct perf_evsel
*evsel
;
768 struct machine
*machine
;
770 dump_event(session
, event
, file_offset
, sample
);
772 evsel
= perf_evlist__id2evsel(session
->evlist
, sample
->id
);
773 if (evsel
!= NULL
&& event
->header
.type
!= PERF_RECORD_SAMPLE
) {
775 * XXX We're leaving PERF_RECORD_SAMPLE unnacounted here
776 * because the tools right now may apply filters, discarding
777 * some of the samples. For consistency, in the future we
778 * should have something like nr_filtered_samples and remove
779 * the sample->period from total_sample_period, etc, KISS for
782 * Also testing against NULL allows us to handle files without
783 * attr.sample_id_all and/or without PERF_SAMPLE_ID. In the
784 * future probably it'll be a good idea to restrict event
785 * processing via perf_session to files with both set.
787 hists__inc_nr_events(&evsel
->hists
, event
->header
.type
);
790 machine
= perf_session__find_machine_for_cpumode(session
, event
);
792 switch (event
->header
.type
) {
793 case PERF_RECORD_SAMPLE
:
794 dump_sample(session
, event
, sample
);
796 ++session
->hists
.stats
.nr_unknown_id
;
799 return tool
->sample(tool
, event
, sample
, evsel
, machine
);
800 case PERF_RECORD_MMAP
:
801 return tool
->mmap(tool
, event
, sample
, machine
);
802 case PERF_RECORD_COMM
:
803 return tool
->comm(tool
, event
, sample
, machine
);
804 case PERF_RECORD_FORK
:
805 return tool
->fork(tool
, event
, sample
, machine
);
806 case PERF_RECORD_EXIT
:
807 return tool
->exit(tool
, event
, sample
, machine
);
808 case PERF_RECORD_LOST
:
809 if (tool
->lost
== perf_event__process_lost
)
810 session
->hists
.stats
.total_lost
+= event
->lost
.lost
;
811 return tool
->lost(tool
, event
, sample
, machine
);
812 case PERF_RECORD_READ
:
813 return tool
->read(tool
, event
, sample
, evsel
, machine
);
814 case PERF_RECORD_THROTTLE
:
815 return tool
->throttle(tool
, event
, sample
, machine
);
816 case PERF_RECORD_UNTHROTTLE
:
817 return tool
->unthrottle(tool
, event
, sample
, machine
);
819 ++session
->hists
.stats
.nr_unknown_events
;
824 static int perf_session__preprocess_sample(struct perf_session
*session
,
825 union perf_event
*event
, struct perf_sample
*sample
)
827 if (event
->header
.type
!= PERF_RECORD_SAMPLE
||
828 !(session
->sample_type
& PERF_SAMPLE_CALLCHAIN
))
831 if (!ip_callchain__valid(sample
->callchain
, event
)) {
832 pr_debug("call-chain problem with event, skipping it.\n");
833 ++session
->hists
.stats
.nr_invalid_chains
;
834 session
->hists
.stats
.total_invalid_chains
+= sample
->period
;
840 static int perf_session__process_user_event(struct perf_session
*session
, union perf_event
*event
,
841 struct perf_tool
*tool
, u64 file_offset
)
845 dump_event(session
, event
, file_offset
, NULL
);
847 /* These events are processed right away */
848 switch (event
->header
.type
) {
849 case PERF_RECORD_HEADER_ATTR
:
850 err
= tool
->attr(event
, &session
->evlist
);
852 perf_session__update_sample_type(session
);
854 case PERF_RECORD_HEADER_EVENT_TYPE
:
855 return tool
->event_type(tool
, event
);
856 case PERF_RECORD_HEADER_TRACING_DATA
:
857 /* setup for reading amidst mmap */
858 lseek(session
->fd
, file_offset
, SEEK_SET
);
859 return tool
->tracing_data(event
, session
);
860 case PERF_RECORD_HEADER_BUILD_ID
:
861 return tool
->build_id(tool
, event
, session
);
862 case PERF_RECORD_FINISHED_ROUND
:
863 return tool
->finished_round(tool
, event
, session
);
869 static int perf_session__process_event(struct perf_session
*session
,
870 union perf_event
*event
,
871 struct perf_tool
*tool
,
874 struct perf_sample sample
;
877 if (session
->header
.needs_swap
&&
878 perf_event__swap_ops
[event
->header
.type
])
879 perf_event__swap_ops
[event
->header
.type
](event
);
881 if (event
->header
.type
>= PERF_RECORD_HEADER_MAX
)
884 hists__inc_nr_events(&session
->hists
, event
->header
.type
);
886 if (event
->header
.type
>= PERF_RECORD_USER_TYPE_START
)
887 return perf_session__process_user_event(session
, event
, tool
, file_offset
);
890 * For all kernel events we get the sample data
892 ret
= perf_session__parse_sample(session
, event
, &sample
);
896 /* Preprocess sample records - precheck callchains */
897 if (perf_session__preprocess_sample(session
, event
, &sample
))
900 if (tool
->ordered_samples
) {
901 ret
= perf_session_queue_event(session
, event
, &sample
,
907 return perf_session_deliver_event(session
, event
, &sample
, tool
,
911 void perf_event_header__bswap(struct perf_event_header
*self
)
913 self
->type
= bswap_32(self
->type
);
914 self
->misc
= bswap_16(self
->misc
);
915 self
->size
= bswap_16(self
->size
);
918 struct thread
*perf_session__findnew(struct perf_session
*session
, pid_t pid
)
920 return machine__findnew_thread(&session
->host_machine
, pid
);
923 static struct thread
*perf_session__register_idle_thread(struct perf_session
*self
)
925 struct thread
*thread
= perf_session__findnew(self
, 0);
927 if (thread
== NULL
|| thread__set_comm(thread
, "swapper")) {
928 pr_err("problem inserting idle task.\n");
935 static void perf_session__warn_about_errors(const struct perf_session
*session
,
936 const struct perf_tool
*tool
)
938 if (tool
->lost
== perf_event__process_lost
&&
939 session
->hists
.stats
.nr_events
[PERF_RECORD_LOST
] != 0) {
940 ui__warning("Processed %d events and lost %d chunks!\n\n"
941 "Check IO/CPU overload!\n\n",
942 session
->hists
.stats
.nr_events
[0],
943 session
->hists
.stats
.nr_events
[PERF_RECORD_LOST
]);
946 if (session
->hists
.stats
.nr_unknown_events
!= 0) {
947 ui__warning("Found %u unknown events!\n\n"
948 "Is this an older tool processing a perf.data "
949 "file generated by a more recent tool?\n\n"
950 "If that is not the case, consider "
951 "reporting to linux-kernel@vger.kernel.org.\n\n",
952 session
->hists
.stats
.nr_unknown_events
);
955 if (session
->hists
.stats
.nr_unknown_id
!= 0) {
956 ui__warning("%u samples with id not present in the header\n",
957 session
->hists
.stats
.nr_unknown_id
);
960 if (session
->hists
.stats
.nr_invalid_chains
!= 0) {
961 ui__warning("Found invalid callchains!\n\n"
962 "%u out of %u events were discarded for this reason.\n\n"
963 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
964 session
->hists
.stats
.nr_invalid_chains
,
965 session
->hists
.stats
.nr_events
[PERF_RECORD_SAMPLE
]);
969 #define session_done() (*(volatile int *)(&session_done))
970 volatile int session_done
;
972 static int __perf_session__process_pipe_events(struct perf_session
*self
,
973 struct perf_tool
*tool
)
975 union perf_event event
;
982 perf_tool__fill_defaults(tool
);
986 err
= readn(self
->fd
, &event
, sizeof(struct perf_event_header
));
991 pr_err("failed to read event header\n");
995 if (self
->header
.needs_swap
)
996 perf_event_header__bswap(&event
.header
);
998 size
= event
.header
.size
;
1003 p
+= sizeof(struct perf_event_header
);
1005 if (size
- sizeof(struct perf_event_header
)) {
1006 err
= readn(self
->fd
, p
, size
- sizeof(struct perf_event_header
));
1009 pr_err("unexpected end of event stream\n");
1013 pr_err("failed to read event data\n");
1018 if ((skip
= perf_session__process_event(self
, &event
, tool
, head
)) < 0) {
1019 dump_printf("%#" PRIx64
" [%#x]: skipping unknown header type: %d\n",
1020 head
, event
.header
.size
, event
.header
.type
);
1022 * assume we lost track of the stream, check alignment, and
1023 * increment a single u64 in the hope to catch on again 'soon'.
1025 if (unlikely(head
& 7))
1036 if (!session_done())
1041 perf_session__warn_about_errors(self
, tool
);
1042 perf_session_free_sample_buffers(self
);
1046 static union perf_event
*
1047 fetch_mmaped_event(struct perf_session
*session
,
1048 u64 head
, size_t mmap_size
, char *buf
)
1050 union perf_event
*event
;
1053 * Ensure we have enough space remaining to read
1054 * the size of the event in the headers.
1056 if (head
+ sizeof(event
->header
) > mmap_size
)
1059 event
= (union perf_event
*)(buf
+ head
);
1061 if (session
->header
.needs_swap
)
1062 perf_event_header__bswap(&event
->header
);
1064 if (head
+ event
->header
.size
> mmap_size
)
1070 int __perf_session__process_events(struct perf_session
*session
,
1071 u64 data_offset
, u64 data_size
,
1072 u64 file_size
, struct perf_tool
*tool
)
1074 u64 head
, page_offset
, file_offset
, file_pos
, progress_next
;
1075 int err
, mmap_prot
, mmap_flags
, map_idx
= 0;
1076 size_t page_size
, mmap_size
;
1077 char *buf
, *mmaps
[8];
1078 union perf_event
*event
;
1081 perf_tool__fill_defaults(tool
);
1083 page_size
= sysconf(_SC_PAGESIZE
);
1085 page_offset
= page_size
* (data_offset
/ page_size
);
1086 file_offset
= page_offset
;
1087 head
= data_offset
- page_offset
;
1089 if (data_offset
+ data_size
< file_size
)
1090 file_size
= data_offset
+ data_size
;
1092 progress_next
= file_size
/ 16;
1094 mmap_size
= session
->mmap_window
;
1095 if (mmap_size
> file_size
)
1096 mmap_size
= file_size
;
1098 memset(mmaps
, 0, sizeof(mmaps
));
1100 mmap_prot
= PROT_READ
;
1101 mmap_flags
= MAP_SHARED
;
1103 if (session
->header
.needs_swap
) {
1104 mmap_prot
|= PROT_WRITE
;
1105 mmap_flags
= MAP_PRIVATE
;
1108 buf
= mmap(NULL
, mmap_size
, mmap_prot
, mmap_flags
, session
->fd
,
1110 if (buf
== MAP_FAILED
) {
1111 pr_err("failed to mmap file\n");
1115 mmaps
[map_idx
] = buf
;
1116 map_idx
= (map_idx
+ 1) & (ARRAY_SIZE(mmaps
) - 1);
1117 file_pos
= file_offset
+ head
;
1120 event
= fetch_mmaped_event(session
, head
, mmap_size
, buf
);
1122 if (mmaps
[map_idx
]) {
1123 munmap(mmaps
[map_idx
], mmap_size
);
1124 mmaps
[map_idx
] = NULL
;
1127 page_offset
= page_size
* (head
/ page_size
);
1128 file_offset
+= page_offset
;
1129 head
-= page_offset
;
1133 size
= event
->header
.size
;
1136 perf_session__process_event(session
, event
, tool
, file_pos
) < 0) {
1137 dump_printf("%#" PRIx64
" [%#x]: skipping unknown header type: %d\n",
1138 file_offset
+ head
, event
->header
.size
,
1139 event
->header
.type
);
1141 * assume we lost track of the stream, check alignment, and
1142 * increment a single u64 in the hope to catch on again 'soon'.
1144 if (unlikely(head
& 7))
1153 if (file_pos
>= progress_next
) {
1154 progress_next
+= file_size
/ 16;
1155 ui_progress__update(file_pos
, file_size
,
1156 "Processing events...");
1159 if (file_pos
< file_size
)
1163 /* do the final flush for ordered samples */
1164 session
->ordered_samples
.next_flush
= ULLONG_MAX
;
1165 flush_sample_queue(session
, tool
);
1167 perf_session__warn_about_errors(session
, tool
);
1168 perf_session_free_sample_buffers(session
);
1172 int perf_session__process_events(struct perf_session
*self
,
1173 struct perf_tool
*tool
)
1177 if (perf_session__register_idle_thread(self
) == NULL
)
1181 err
= __perf_session__process_events(self
,
1182 self
->header
.data_offset
,
1183 self
->header
.data_size
,
1186 err
= __perf_session__process_pipe_events(self
, tool
);
1191 bool perf_session__has_traces(struct perf_session
*self
, const char *msg
)
1193 if (!(self
->sample_type
& PERF_SAMPLE_RAW
)) {
1194 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg
);
1201 int maps__set_kallsyms_ref_reloc_sym(struct map
**maps
,
1202 const char *symbol_name
, u64 addr
)
1206 struct ref_reloc_sym
*ref
;
1208 ref
= zalloc(sizeof(struct ref_reloc_sym
));
1212 ref
->name
= strdup(symbol_name
);
1213 if (ref
->name
== NULL
) {
1218 bracket
= strchr(ref
->name
, ']');
1224 for (i
= 0; i
< MAP__NR_TYPES
; ++i
) {
1225 struct kmap
*kmap
= map__kmap(maps
[i
]);
1226 kmap
->ref_reloc_sym
= ref
;
1232 size_t perf_session__fprintf_dsos(struct perf_session
*self
, FILE *fp
)
1234 return __dsos__fprintf(&self
->host_machine
.kernel_dsos
, fp
) +
1235 __dsos__fprintf(&self
->host_machine
.user_dsos
, fp
) +
1236 machines__fprintf_dsos(&self
->machines
, fp
);
1239 size_t perf_session__fprintf_dsos_buildid(struct perf_session
*self
, FILE *fp
,
1242 size_t ret
= machine__fprintf_dsos_buildid(&self
->host_machine
, fp
, with_hits
);
1243 return ret
+ machines__fprintf_dsos_buildid(&self
->machines
, fp
, with_hits
);
1246 size_t perf_session__fprintf_nr_events(struct perf_session
*session
, FILE *fp
)
1248 struct perf_evsel
*pos
;
1249 size_t ret
= fprintf(fp
, "Aggregated stats:\n");
1251 ret
+= hists__fprintf_nr_events(&session
->hists
, fp
);
1253 list_for_each_entry(pos
, &session
->evlist
->entries
, node
) {
1254 ret
+= fprintf(fp
, "%s stats:\n", event_name(pos
));
1255 ret
+= hists__fprintf_nr_events(&pos
->hists
, fp
);
1261 size_t perf_session__fprintf(struct perf_session
*session
, FILE *fp
)
1264 * FIXME: Here we have to actually print all the machines in this
1265 * session, not just the host...
1267 return machine__fprintf(&session
->host_machine
, fp
);
1270 void perf_session__remove_thread(struct perf_session
*session
,
1274 * FIXME: This one makes no sense, we need to remove the thread from
1275 * the machine it belongs to, perf_session can have many machines, so
1276 * doing it always on ->host_machine is wrong. Fix when auditing all
1277 * the 'perf kvm' code.
1279 machine__remove_thread(&session
->host_machine
, th
);
1282 struct perf_evsel
*perf_session__find_first_evtype(struct perf_session
*session
,
1285 struct perf_evsel
*pos
;
1287 list_for_each_entry(pos
, &session
->evlist
->entries
, node
) {
1288 if (pos
->attr
.type
== type
)
1294 void perf_event__print_ip(union perf_event
*event
, struct perf_sample
*sample
,
1295 struct machine
*machine
, struct perf_evsel
*evsel
,
1296 int print_sym
, int print_dso
)
1298 struct addr_location al
;
1299 const char *symname
, *dsoname
;
1300 struct callchain_cursor
*cursor
= &evsel
->hists
.callchain_cursor
;
1301 struct callchain_cursor_node
*node
;
1303 if (perf_event__preprocess_sample(event
, machine
, &al
, sample
,
1305 error("problem processing %d event, skipping it.\n",
1306 event
->header
.type
);
1310 if (symbol_conf
.use_callchain
&& sample
->callchain
) {
1312 if (machine__resolve_callchain(machine
, evsel
, al
.thread
,
1313 sample
->callchain
, NULL
) != 0) {
1315 error("Failed to resolve callchain. Skipping\n");
1318 callchain_cursor_commit(cursor
);
1321 node
= callchain_cursor_current(cursor
);
1325 printf("\t%16" PRIx64
, node
->ip
);
1327 if (node
->sym
&& node
->sym
->name
)
1328 symname
= node
->sym
->name
;
1332 printf(" %s", symname
);
1335 if (node
->map
&& node
->map
->dso
&& node
->map
->dso
->name
)
1336 dsoname
= node
->map
->dso
->name
;
1340 printf(" (%s)", dsoname
);
1344 callchain_cursor_advance(cursor
);
1348 printf("%16" PRIx64
, sample
->ip
);
1350 if (al
.sym
&& al
.sym
->name
)
1351 symname
= al
.sym
->name
;
1355 printf(" %s", symname
);
1359 if (al
.map
&& al
.map
->dso
&& al
.map
->dso
->name
)
1360 dsoname
= al
.map
->dso
->name
;
1364 printf(" (%s)", dsoname
);
1369 int perf_session__cpu_bitmap(struct perf_session
*session
,
1370 const char *cpu_list
, unsigned long *cpu_bitmap
)
1373 struct cpu_map
*map
;
1375 for (i
= 0; i
< PERF_TYPE_MAX
; ++i
) {
1376 struct perf_evsel
*evsel
;
1378 evsel
= perf_session__find_first_evtype(session
, i
);
1382 if (!(evsel
->attr
.sample_type
& PERF_SAMPLE_CPU
)) {
1383 pr_err("File does not contain CPU events. "
1384 "Remove -c option to proceed.\n");
1389 map
= cpu_map__new(cpu_list
);
1391 pr_err("Invalid cpu_list\n");
1395 for (i
= 0; i
< map
->nr
; i
++) {
1396 int cpu
= map
->map
[i
];
1398 if (cpu
>= MAX_NR_CPUS
) {
1399 pr_err("Requested CPU %d too large. "
1400 "Consider raising MAX_NR_CPUS\n", cpu
);
1404 set_bit(cpu
, cpu_bitmap
);
1410 void perf_session__fprintf_info(struct perf_session
*session
, FILE *fp
,
1416 if (session
== NULL
|| fp
== NULL
)
1419 ret
= fstat(session
->fd
, &st
);
1423 fprintf(fp
, "# ========\n");
1424 fprintf(fp
, "# captured on: %s", ctime(&st
.st_ctime
));
1425 perf_header__fprintf_info(session
, fp
, full
);
1426 fprintf(fp
, "# ========\n#\n");