1 #define _FILE_OFFSET_BITS 64
3 #include <linux/kernel.h>
14 static int perf_session__open(struct perf_session
*self
, bool force
)
16 struct stat input_stat
;
18 if (!strcmp(self
->filename
, "-")) {
20 self
->fd
= STDIN_FILENO
;
22 if (perf_header__read(self
, self
->fd
) < 0)
23 pr_err("incompatible file format");
28 self
->fd
= open(self
->filename
, O_RDONLY
);
32 pr_err("failed to open %s: %s", self
->filename
, strerror(err
));
33 if (err
== ENOENT
&& !strcmp(self
->filename
, "perf.data"))
34 pr_err(" (try 'perf record' first)");
39 if (fstat(self
->fd
, &input_stat
) < 0)
42 if (!force
&& input_stat
.st_uid
&& (input_stat
.st_uid
!= geteuid())) {
43 pr_err("file %s not owned by current user or root\n",
48 if (!input_stat
.st_size
) {
49 pr_info("zero-sized file (%s), nothing to do!\n",
54 if (perf_header__read(self
, self
->fd
) < 0) {
55 pr_err("incompatible file format");
59 self
->size
= input_stat
.st_size
;
68 static void perf_session__id_header_size(struct perf_session
*session
)
70 struct sample_data
*data
;
71 u64 sample_type
= session
->sample_type
;
74 if (!session
->sample_id_all
)
77 if (sample_type
& PERF_SAMPLE_TID
)
78 size
+= sizeof(data
->tid
) * 2;
80 if (sample_type
& PERF_SAMPLE_TIME
)
81 size
+= sizeof(data
->time
);
83 if (sample_type
& PERF_SAMPLE_ID
)
84 size
+= sizeof(data
->id
);
86 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
87 size
+= sizeof(data
->stream_id
);
89 if (sample_type
& PERF_SAMPLE_CPU
)
90 size
+= sizeof(data
->cpu
) * 2;
92 session
->id_hdr_size
= size
;
95 void perf_session__set_sample_id_all(struct perf_session
*session
, bool value
)
97 session
->sample_id_all
= value
;
98 perf_session__id_header_size(session
);
101 void perf_session__set_sample_type(struct perf_session
*session
, u64 type
)
103 session
->sample_type
= type
;
106 void perf_session__update_sample_type(struct perf_session
*self
)
108 self
->sample_type
= perf_header__sample_type(&self
->header
);
109 self
->sample_id_all
= perf_header__sample_id_all(&self
->header
);
110 perf_session__id_header_size(self
);
113 int perf_session__create_kernel_maps(struct perf_session
*self
)
115 int ret
= machine__create_kernel_maps(&self
->host_machine
);
118 ret
= machines__create_guest_kernel_maps(&self
->machines
);
122 static void perf_session__destroy_kernel_maps(struct perf_session
*self
)
124 machine__destroy_kernel_maps(&self
->host_machine
);
125 machines__destroy_guest_kernel_maps(&self
->machines
);
128 struct perf_session
*perf_session__new(const char *filename
, int mode
,
129 bool force
, bool repipe
,
130 struct perf_event_ops
*ops
)
132 size_t len
= filename
? strlen(filename
) + 1 : 0;
133 struct perf_session
*self
= zalloc(sizeof(*self
) + len
);
138 if (perf_header__init(&self
->header
) < 0)
141 memcpy(self
->filename
, filename
, len
);
142 self
->threads
= RB_ROOT
;
143 INIT_LIST_HEAD(&self
->dead_threads
);
144 self
->hists_tree
= RB_ROOT
;
145 self
->last_match
= NULL
;
147 * On 64bit we can mmap the data file in one go. No need for tiny mmap
148 * slices. On 32bit we use 32MB.
150 #if BITS_PER_LONG == 64
151 self
->mmap_window
= ULLONG_MAX
;
153 self
->mmap_window
= 32 * 1024 * 1024ULL;
155 self
->machines
= RB_ROOT
;
156 self
->repipe
= repipe
;
157 INIT_LIST_HEAD(&self
->ordered_samples
.samples
);
158 INIT_LIST_HEAD(&self
->ordered_samples
.sample_cache
);
159 INIT_LIST_HEAD(&self
->ordered_samples
.to_free
);
160 machine__init(&self
->host_machine
, "", HOST_KERNEL_ID
);
162 if (mode
== O_RDONLY
) {
163 if (perf_session__open(self
, force
) < 0)
165 } else if (mode
== O_WRONLY
) {
167 * In O_RDONLY mode this will be performed when reading the
168 * kernel MMAP event, in event__process_mmap().
170 if (perf_session__create_kernel_maps(self
) < 0)
174 perf_session__update_sample_type(self
);
176 if (ops
&& ops
->ordering_requires_timestamps
&&
177 ops
->ordered_samples
&& !self
->sample_id_all
) {
178 dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n");
179 ops
->ordered_samples
= false;
188 perf_session__delete(self
);
192 static void perf_session__delete_dead_threads(struct perf_session
*self
)
194 struct thread
*n
, *t
;
196 list_for_each_entry_safe(t
, n
, &self
->dead_threads
, node
) {
202 static void perf_session__delete_threads(struct perf_session
*self
)
204 struct rb_node
*nd
= rb_first(&self
->threads
);
207 struct thread
*t
= rb_entry(nd
, struct thread
, rb_node
);
209 rb_erase(&t
->rb_node
, &self
->threads
);
215 void perf_session__delete(struct perf_session
*self
)
217 perf_header__exit(&self
->header
);
218 perf_session__destroy_kernel_maps(self
);
219 perf_session__delete_dead_threads(self
);
220 perf_session__delete_threads(self
);
221 machine__exit(&self
->host_machine
);
226 void perf_session__remove_thread(struct perf_session
*self
, struct thread
*th
)
228 self
->last_match
= NULL
;
229 rb_erase(&th
->rb_node
, &self
->threads
);
231 * We may have references to this thread, for instance in some hist_entry
232 * instances, so just move them to a separate list.
234 list_add_tail(&th
->node
, &self
->dead_threads
);
237 static bool symbol__match_parent_regex(struct symbol
*sym
)
239 if (sym
->name
&& !regexec(&parent_regex
, sym
->name
, 0, NULL
, 0))
245 struct map_symbol
*perf_session__resolve_callchain(struct perf_session
*self
,
246 struct thread
*thread
,
247 struct ip_callchain
*chain
,
248 struct symbol
**parent
)
250 u8 cpumode
= PERF_RECORD_MISC_USER
;
252 struct map_symbol
*syms
= calloc(chain
->nr
, sizeof(*syms
));
257 for (i
= 0; i
< chain
->nr
; i
++) {
258 u64 ip
= chain
->ips
[i
];
259 struct addr_location al
;
261 if (ip
>= PERF_CONTEXT_MAX
) {
263 case PERF_CONTEXT_HV
:
264 cpumode
= PERF_RECORD_MISC_HYPERVISOR
; break;
265 case PERF_CONTEXT_KERNEL
:
266 cpumode
= PERF_RECORD_MISC_KERNEL
; break;
267 case PERF_CONTEXT_USER
:
268 cpumode
= PERF_RECORD_MISC_USER
; break;
276 thread__find_addr_location(thread
, self
, cpumode
,
277 MAP__FUNCTION
, thread
->pid
, ip
, &al
, NULL
);
278 if (al
.sym
!= NULL
) {
279 if (sort__has_parent
&& !*parent
&&
280 symbol__match_parent_regex(al
.sym
))
282 if (!symbol_conf
.use_callchain
)
284 syms
[i
].map
= al
.map
;
285 syms
[i
].sym
= al
.sym
;
292 static int process_event_synth_stub(event_t
*event __used
,
293 struct perf_session
*session __used
)
295 dump_printf(": unhandled!\n");
299 static int process_event_stub(event_t
*event __used
,
300 struct sample_data
*sample __used
,
301 struct perf_session
*session __used
)
303 dump_printf(": unhandled!\n");
307 static int process_finished_round_stub(event_t
*event __used
,
308 struct perf_session
*session __used
,
309 struct perf_event_ops
*ops __used
)
311 dump_printf(": unhandled!\n");
315 static int process_finished_round(event_t
*event
,
316 struct perf_session
*session
,
317 struct perf_event_ops
*ops
);
319 static void perf_event_ops__fill_defaults(struct perf_event_ops
*handler
)
321 if (handler
->sample
== NULL
)
322 handler
->sample
= process_event_stub
;
323 if (handler
->mmap
== NULL
)
324 handler
->mmap
= process_event_stub
;
325 if (handler
->comm
== NULL
)
326 handler
->comm
= process_event_stub
;
327 if (handler
->fork
== NULL
)
328 handler
->fork
= process_event_stub
;
329 if (handler
->exit
== NULL
)
330 handler
->exit
= process_event_stub
;
331 if (handler
->lost
== NULL
)
332 handler
->lost
= event__process_lost
;
333 if (handler
->read
== NULL
)
334 handler
->read
= process_event_stub
;
335 if (handler
->throttle
== NULL
)
336 handler
->throttle
= process_event_stub
;
337 if (handler
->unthrottle
== NULL
)
338 handler
->unthrottle
= process_event_stub
;
339 if (handler
->attr
== NULL
)
340 handler
->attr
= process_event_synth_stub
;
341 if (handler
->event_type
== NULL
)
342 handler
->event_type
= process_event_synth_stub
;
343 if (handler
->tracing_data
== NULL
)
344 handler
->tracing_data
= process_event_synth_stub
;
345 if (handler
->build_id
== NULL
)
346 handler
->build_id
= process_event_synth_stub
;
347 if (handler
->finished_round
== NULL
) {
348 if (handler
->ordered_samples
)
349 handler
->finished_round
= process_finished_round
;
351 handler
->finished_round
= process_finished_round_stub
;
355 void mem_bswap_64(void *src
, int byte_size
)
359 while (byte_size
> 0) {
361 byte_size
-= sizeof(u64
);
366 static void event__all64_swap(event_t
*self
)
368 struct perf_event_header
*hdr
= &self
->header
;
369 mem_bswap_64(hdr
+ 1, self
->header
.size
- sizeof(*hdr
));
372 static void event__comm_swap(event_t
*self
)
374 self
->comm
.pid
= bswap_32(self
->comm
.pid
);
375 self
->comm
.tid
= bswap_32(self
->comm
.tid
);
378 static void event__mmap_swap(event_t
*self
)
380 self
->mmap
.pid
= bswap_32(self
->mmap
.pid
);
381 self
->mmap
.tid
= bswap_32(self
->mmap
.tid
);
382 self
->mmap
.start
= bswap_64(self
->mmap
.start
);
383 self
->mmap
.len
= bswap_64(self
->mmap
.len
);
384 self
->mmap
.pgoff
= bswap_64(self
->mmap
.pgoff
);
387 static void event__task_swap(event_t
*self
)
389 self
->fork
.pid
= bswap_32(self
->fork
.pid
);
390 self
->fork
.tid
= bswap_32(self
->fork
.tid
);
391 self
->fork
.ppid
= bswap_32(self
->fork
.ppid
);
392 self
->fork
.ptid
= bswap_32(self
->fork
.ptid
);
393 self
->fork
.time
= bswap_64(self
->fork
.time
);
396 static void event__read_swap(event_t
*self
)
398 self
->read
.pid
= bswap_32(self
->read
.pid
);
399 self
->read
.tid
= bswap_32(self
->read
.tid
);
400 self
->read
.value
= bswap_64(self
->read
.value
);
401 self
->read
.time_enabled
= bswap_64(self
->read
.time_enabled
);
402 self
->read
.time_running
= bswap_64(self
->read
.time_running
);
403 self
->read
.id
= bswap_64(self
->read
.id
);
406 static void event__attr_swap(event_t
*self
)
410 self
->attr
.attr
.type
= bswap_32(self
->attr
.attr
.type
);
411 self
->attr
.attr
.size
= bswap_32(self
->attr
.attr
.size
);
412 self
->attr
.attr
.config
= bswap_64(self
->attr
.attr
.config
);
413 self
->attr
.attr
.sample_period
= bswap_64(self
->attr
.attr
.sample_period
);
414 self
->attr
.attr
.sample_type
= bswap_64(self
->attr
.attr
.sample_type
);
415 self
->attr
.attr
.read_format
= bswap_64(self
->attr
.attr
.read_format
);
416 self
->attr
.attr
.wakeup_events
= bswap_32(self
->attr
.attr
.wakeup_events
);
417 self
->attr
.attr
.bp_type
= bswap_32(self
->attr
.attr
.bp_type
);
418 self
->attr
.attr
.bp_addr
= bswap_64(self
->attr
.attr
.bp_addr
);
419 self
->attr
.attr
.bp_len
= bswap_64(self
->attr
.attr
.bp_len
);
421 size
= self
->header
.size
;
422 size
-= (void *)&self
->attr
.id
- (void *)self
;
423 mem_bswap_64(self
->attr
.id
, size
);
426 static void event__event_type_swap(event_t
*self
)
428 self
->event_type
.event_type
.event_id
=
429 bswap_64(self
->event_type
.event_type
.event_id
);
432 static void event__tracing_data_swap(event_t
*self
)
434 self
->tracing_data
.size
= bswap_32(self
->tracing_data
.size
);
437 typedef void (*event__swap_op
)(event_t
*self
);
439 static event__swap_op event__swap_ops
[] = {
440 [PERF_RECORD_MMAP
] = event__mmap_swap
,
441 [PERF_RECORD_COMM
] = event__comm_swap
,
442 [PERF_RECORD_FORK
] = event__task_swap
,
443 [PERF_RECORD_EXIT
] = event__task_swap
,
444 [PERF_RECORD_LOST
] = event__all64_swap
,
445 [PERF_RECORD_READ
] = event__read_swap
,
446 [PERF_RECORD_SAMPLE
] = event__all64_swap
,
447 [PERF_RECORD_HEADER_ATTR
] = event__attr_swap
,
448 [PERF_RECORD_HEADER_EVENT_TYPE
] = event__event_type_swap
,
449 [PERF_RECORD_HEADER_TRACING_DATA
] = event__tracing_data_swap
,
450 [PERF_RECORD_HEADER_BUILD_ID
] = NULL
,
451 [PERF_RECORD_HEADER_MAX
] = NULL
,
454 struct sample_queue
{
458 struct list_head list
;
461 static void perf_session_free_sample_buffers(struct perf_session
*session
)
463 struct ordered_samples
*os
= &session
->ordered_samples
;
465 while (!list_empty(&os
->to_free
)) {
466 struct sample_queue
*sq
;
468 sq
= list_entry(os
->to_free
.next
, struct sample_queue
, list
);
474 static int perf_session_deliver_event(struct perf_session
*session
,
476 struct sample_data
*sample
,
477 struct perf_event_ops
*ops
,
480 static void flush_sample_queue(struct perf_session
*s
,
481 struct perf_event_ops
*ops
)
483 struct ordered_samples
*os
= &s
->ordered_samples
;
484 struct list_head
*head
= &os
->samples
;
485 struct sample_queue
*tmp
, *iter
;
486 struct sample_data sample
;
487 u64 limit
= os
->next_flush
;
488 u64 last_ts
= os
->last_sample
? os
->last_sample
->timestamp
: 0ULL;
490 if (!ops
->ordered_samples
|| !limit
)
493 list_for_each_entry_safe(iter
, tmp
, head
, list
) {
494 if (iter
->timestamp
> limit
)
497 event__parse_sample(iter
->event
, s
, &sample
);
498 perf_session_deliver_event(s
, iter
->event
, &sample
, ops
,
501 os
->last_flush
= iter
->timestamp
;
502 list_del(&iter
->list
);
503 list_add(&iter
->list
, &os
->sample_cache
);
506 if (list_empty(head
)) {
507 os
->last_sample
= NULL
;
508 } else if (last_ts
<= limit
) {
510 list_entry(head
->prev
, struct sample_queue
, list
);
515 * When perf record finishes a pass on every buffers, it records this pseudo
517 * We record the max timestamp t found in the pass n.
518 * Assuming these timestamps are monotonic across cpus, we know that if
519 * a buffer still has events with timestamps below t, they will be all
520 * available and then read in the pass n + 1.
521 * Hence when we start to read the pass n + 2, we can safely flush every
522 * events with timestamps below t.
524 * ============ PASS n =================
527 * cnt1 timestamps | cnt2 timestamps
530 * - | 4 <--- max recorded
532 * ============ PASS n + 1 ==============
535 * cnt1 timestamps | cnt2 timestamps
538 * 5 | 7 <---- max recorded
540 * Flush every events below timestamp 4
542 * ============ PASS n + 2 ==============
545 * cnt1 timestamps | cnt2 timestamps
550 * Flush every events below timestamp 7
553 static int process_finished_round(event_t
*event __used
,
554 struct perf_session
*session
,
555 struct perf_event_ops
*ops
)
557 flush_sample_queue(session
, ops
);
558 session
->ordered_samples
.next_flush
= session
->ordered_samples
.max_timestamp
;
563 /* The queue is ordered by time */
564 static void __queue_event(struct sample_queue
*new, struct perf_session
*s
)
566 struct ordered_samples
*os
= &s
->ordered_samples
;
567 struct sample_queue
*sample
= os
->last_sample
;
568 u64 timestamp
= new->timestamp
;
571 os
->last_sample
= new;
574 list_add(&new->list
, &os
->samples
);
575 os
->max_timestamp
= timestamp
;
580 * last_sample might point to some random place in the list as it's
581 * the last queued event. We expect that the new event is close to
584 if (sample
->timestamp
<= timestamp
) {
585 while (sample
->timestamp
<= timestamp
) {
586 p
= sample
->list
.next
;
587 if (p
== &os
->samples
) {
588 list_add_tail(&new->list
, &os
->samples
);
589 os
->max_timestamp
= timestamp
;
592 sample
= list_entry(p
, struct sample_queue
, list
);
594 list_add_tail(&new->list
, &sample
->list
);
596 while (sample
->timestamp
> timestamp
) {
597 p
= sample
->list
.prev
;
598 if (p
== &os
->samples
) {
599 list_add(&new->list
, &os
->samples
);
602 sample
= list_entry(p
, struct sample_queue
, list
);
604 list_add(&new->list
, &sample
->list
);
608 #define MAX_SAMPLE_BUFFER (64 * 1024 / sizeof(struct sample_queue))
610 static int perf_session_queue_event(struct perf_session
*s
, event_t
*event
,
611 struct sample_data
*data
, u64 file_offset
)
613 struct ordered_samples
*os
= &s
->ordered_samples
;
614 struct list_head
*sc
= &os
->sample_cache
;
615 u64 timestamp
= data
->time
;
616 struct sample_queue
*new;
618 if (!timestamp
|| timestamp
== ~0ULL)
621 if (timestamp
< s
->ordered_samples
.last_flush
) {
622 printf("Warning: Timestamp below last timeslice flush\n");
626 if (!list_empty(sc
)) {
627 new = list_entry(sc
->next
, struct sample_queue
, list
);
628 list_del(&new->list
);
629 } else if (os
->sample_buffer
) {
630 new = os
->sample_buffer
+ os
->sample_buffer_idx
;
631 if (++os
->sample_buffer_idx
== MAX_SAMPLE_BUFFER
)
632 os
->sample_buffer
= NULL
;
634 os
->sample_buffer
= malloc(MAX_SAMPLE_BUFFER
* sizeof(*new));
635 if (!os
->sample_buffer
)
637 list_add(&os
->sample_buffer
->list
, &os
->to_free
);
638 os
->sample_buffer_idx
= 2;
639 new = os
->sample_buffer
+ 1;
642 new->timestamp
= timestamp
;
643 new->file_offset
= file_offset
;
646 __queue_event(new, s
);
651 static void callchain__printf(struct sample_data
*sample
)
655 printf("... chain: nr:%" PRIu64
"\n", sample
->callchain
->nr
);
657 for (i
= 0; i
< sample
->callchain
->nr
; i
++)
658 printf("..... %2d: %016" PRIx64
"\n",
659 i
, sample
->callchain
->ips
[i
]);
662 static void perf_session__print_tstamp(struct perf_session
*session
,
664 struct sample_data
*sample
)
666 if (event
->header
.type
!= PERF_RECORD_SAMPLE
&&
667 !session
->sample_id_all
) {
668 fputs("-1 -1 ", stdout
);
672 if ((session
->sample_type
& PERF_SAMPLE_CPU
))
673 printf("%u ", sample
->cpu
);
675 if (session
->sample_type
& PERF_SAMPLE_TIME
)
676 printf("%" PRIu64
" ", sample
->time
);
679 static void dump_event(struct perf_session
*session
, event_t
*event
,
680 u64 file_offset
, struct sample_data
*sample
)
685 printf("\n%#" PRIx64
" [%#x]: event: %d\n",
686 file_offset
, event
->header
.size
, event
->header
.type
);
691 perf_session__print_tstamp(session
, event
, sample
);
693 printf("%#" PRIx64
" [%#x]: PERF_RECORD_%s", file_offset
,
694 event
->header
.size
, event__get_event_name(event
->header
.type
));
697 static void dump_sample(struct perf_session
*session
, event_t
*event
,
698 struct sample_data
*sample
)
703 printf("(IP, %d): %d/%d: %#" PRIx64
" period: %" PRIu64
"\n",
704 event
->header
.misc
, sample
->pid
, sample
->tid
, sample
->ip
,
707 if (session
->sample_type
& PERF_SAMPLE_CALLCHAIN
)
708 callchain__printf(sample
);
711 static int perf_session_deliver_event(struct perf_session
*session
,
713 struct sample_data
*sample
,
714 struct perf_event_ops
*ops
,
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 return ops
->sample(event
, sample
, 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 event_t
*event
, struct sample_data
*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
, event_t
*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
,
787 struct perf_event_ops
*ops
,
790 struct sample_data sample
;
793 if (session
->header
.needs_swap
&& event__swap_ops
[event
->header
.type
])
794 event__swap_ops
[event
->header
.type
](event
);
796 if (event
->header
.type
>= PERF_RECORD_HEADER_MAX
)
799 hists__inc_nr_events(&session
->hists
, event
->header
.type
);
801 if (event
->header
.type
>= PERF_RECORD_USER_TYPE_START
)
802 return perf_session__process_user_event(session
, event
, ops
, file_offset
);
805 * For all kernel events we get the sample data
807 event__parse_sample(event
, session
, &sample
);
809 /* Preprocess sample records - precheck callchains */
810 if (perf_session__preprocess_sample(session
, event
, &sample
))
813 if (ops
->ordered_samples
) {
814 ret
= perf_session_queue_event(session
, event
, &sample
,
820 return perf_session_deliver_event(session
, event
, &sample
, ops
,
824 void perf_event_header__bswap(struct perf_event_header
*self
)
826 self
->type
= bswap_32(self
->type
);
827 self
->misc
= bswap_16(self
->misc
);
828 self
->size
= bswap_16(self
->size
);
831 static struct thread
*perf_session__register_idle_thread(struct perf_session
*self
)
833 struct thread
*thread
= perf_session__findnew(self
, 0);
835 if (thread
== NULL
|| thread__set_comm(thread
, "swapper")) {
836 pr_err("problem inserting idle task.\n");
843 static void perf_session__warn_about_errors(const struct perf_session
*session
,
844 const struct perf_event_ops
*ops
)
846 if (ops
->lost
== event__process_lost
&&
847 session
->hists
.stats
.total_lost
!= 0) {
848 ui__warning("Processed %" PRIu64
" events and LOST %" PRIu64
849 "!\n\nCheck IO/CPU overload!\n\n",
850 session
->hists
.stats
.total_period
,
851 session
->hists
.stats
.total_lost
);
854 if (session
->hists
.stats
.nr_unknown_events
!= 0) {
855 ui__warning("Found %u unknown events!\n\n"
856 "Is this an older tool processing a perf.data "
857 "file generated by a more recent tool?\n\n"
858 "If that is not the case, consider "
859 "reporting to linux-kernel@vger.kernel.org.\n\n",
860 session
->hists
.stats
.nr_unknown_events
);
863 if (session
->hists
.stats
.nr_invalid_chains
!= 0) {
864 ui__warning("Found invalid callchains!\n\n"
865 "%u out of %u events were discarded for this reason.\n\n"
866 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
867 session
->hists
.stats
.nr_invalid_chains
,
868 session
->hists
.stats
.nr_events
[PERF_RECORD_SAMPLE
]);
872 #define session_done() (*(volatile int *)(&session_done))
873 volatile int session_done
;
875 static int __perf_session__process_pipe_events(struct perf_session
*self
,
876 struct perf_event_ops
*ops
)
885 perf_event_ops__fill_defaults(ops
);
889 err
= readn(self
->fd
, &event
, sizeof(struct perf_event_header
));
894 pr_err("failed to read event header\n");
898 if (self
->header
.needs_swap
)
899 perf_event_header__bswap(&event
.header
);
901 size
= event
.header
.size
;
906 p
+= sizeof(struct perf_event_header
);
908 if (size
- sizeof(struct perf_event_header
)) {
909 err
= readn(self
->fd
, p
, size
- sizeof(struct perf_event_header
));
912 pr_err("unexpected end of event stream\n");
916 pr_err("failed to read event data\n");
922 (skip
= perf_session__process_event(self
, &event
, ops
, head
)) < 0) {
923 dump_printf("%#" PRIx64
" [%#x]: skipping unknown header type: %d\n",
924 head
, event
.header
.size
, event
.header
.type
);
926 * assume we lost track of the stream, check alignment, and
927 * increment a single u64 in the hope to catch on again 'soon'.
929 if (unlikely(head
& 7))
945 perf_session__warn_about_errors(self
, ops
);
946 perf_session_free_sample_buffers(self
);
950 int __perf_session__process_events(struct perf_session
*session
,
951 u64 data_offset
, u64 data_size
,
952 u64 file_size
, struct perf_event_ops
*ops
)
954 u64 head
, page_offset
, file_offset
, file_pos
, progress_next
;
955 int err
, mmap_prot
, mmap_flags
, map_idx
= 0;
956 struct ui_progress
*progress
;
957 size_t page_size
, mmap_size
;
958 char *buf
, *mmaps
[8];
962 perf_event_ops__fill_defaults(ops
);
964 page_size
= sysconf(_SC_PAGESIZE
);
966 page_offset
= page_size
* (data_offset
/ page_size
);
967 file_offset
= page_offset
;
968 head
= data_offset
- page_offset
;
970 if (data_offset
+ data_size
< file_size
)
971 file_size
= data_offset
+ data_size
;
973 progress_next
= file_size
/ 16;
974 progress
= ui_progress__new("Processing events...", file_size
);
975 if (progress
== NULL
)
978 mmap_size
= session
->mmap_window
;
979 if (mmap_size
> file_size
)
980 mmap_size
= file_size
;
982 memset(mmaps
, 0, sizeof(mmaps
));
984 mmap_prot
= PROT_READ
;
985 mmap_flags
= MAP_SHARED
;
987 if (session
->header
.needs_swap
) {
988 mmap_prot
|= PROT_WRITE
;
989 mmap_flags
= MAP_PRIVATE
;
992 buf
= mmap(NULL
, mmap_size
, mmap_prot
, mmap_flags
, session
->fd
,
994 if (buf
== MAP_FAILED
) {
995 pr_err("failed to mmap file\n");
999 mmaps
[map_idx
] = buf
;
1000 map_idx
= (map_idx
+ 1) & (ARRAY_SIZE(mmaps
) - 1);
1001 file_pos
= file_offset
+ head
;
1004 event
= (event_t
*)(buf
+ head
);
1006 if (session
->header
.needs_swap
)
1007 perf_event_header__bswap(&event
->header
);
1008 size
= event
->header
.size
;
1012 if (head
+ event
->header
.size
> mmap_size
) {
1013 if (mmaps
[map_idx
]) {
1014 munmap(mmaps
[map_idx
], mmap_size
);
1015 mmaps
[map_idx
] = NULL
;
1018 page_offset
= page_size
* (head
/ page_size
);
1019 file_offset
+= page_offset
;
1020 head
-= page_offset
;
1024 size
= event
->header
.size
;
1027 perf_session__process_event(session
, event
, ops
, file_pos
) < 0) {
1028 dump_printf("%#" PRIx64
" [%#x]: skipping unknown header type: %d\n",
1029 file_offset
+ head
, event
->header
.size
,
1030 event
->header
.type
);
1032 * assume we lost track of the stream, check alignment, and
1033 * increment a single u64 in the hope to catch on again 'soon'.
1035 if (unlikely(head
& 7))
1044 if (file_pos
>= progress_next
) {
1045 progress_next
+= file_size
/ 16;
1046 ui_progress__update(progress
, file_pos
);
1049 if (file_pos
< file_size
)
1053 /* do the final flush for ordered samples */
1054 session
->ordered_samples
.next_flush
= ULLONG_MAX
;
1055 flush_sample_queue(session
, ops
);
1057 ui_progress__delete(progress
);
1058 perf_session__warn_about_errors(session
, ops
);
1059 perf_session_free_sample_buffers(session
);
1063 int perf_session__process_events(struct perf_session
*self
,
1064 struct perf_event_ops
*ops
)
1068 if (perf_session__register_idle_thread(self
) == NULL
)
1072 err
= __perf_session__process_events(self
,
1073 self
->header
.data_offset
,
1074 self
->header
.data_size
,
1077 err
= __perf_session__process_pipe_events(self
, ops
);
1082 bool perf_session__has_traces(struct perf_session
*self
, const char *msg
)
1084 if (!(self
->sample_type
& PERF_SAMPLE_RAW
)) {
1085 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg
);
1092 int perf_session__set_kallsyms_ref_reloc_sym(struct map
**maps
,
1093 const char *symbol_name
,
1098 struct ref_reloc_sym
*ref
;
1100 ref
= zalloc(sizeof(struct ref_reloc_sym
));
1104 ref
->name
= strdup(symbol_name
);
1105 if (ref
->name
== NULL
) {
1110 bracket
= strchr(ref
->name
, ']');
1116 for (i
= 0; i
< MAP__NR_TYPES
; ++i
) {
1117 struct kmap
*kmap
= map__kmap(maps
[i
]);
1118 kmap
->ref_reloc_sym
= ref
;
1124 size_t perf_session__fprintf_dsos(struct perf_session
*self
, FILE *fp
)
1126 return __dsos__fprintf(&self
->host_machine
.kernel_dsos
, fp
) +
1127 __dsos__fprintf(&self
->host_machine
.user_dsos
, fp
) +
1128 machines__fprintf_dsos(&self
->machines
, fp
);
1131 size_t perf_session__fprintf_dsos_buildid(struct perf_session
*self
, FILE *fp
,
1134 size_t ret
= machine__fprintf_dsos_buildid(&self
->host_machine
, fp
, with_hits
);
1135 return ret
+ machines__fprintf_dsos_buildid(&self
->machines
, fp
, with_hits
);