1 // SPDX-License-Identifier: GPL-2.0
4 #include <linux/kernel.h>
5 #include <traceevent/event-parse.h>
10 #include <sys/types.h>
21 #include "perf_regs.h"
25 #include "thread-stack.h"
27 #include "arch/common.h"
29 static int perf_session__deliver_event(struct perf_session
*session
,
30 union perf_event
*event
,
31 struct perf_tool
*tool
,
34 static int perf_session__open(struct perf_session
*session
)
36 struct perf_data
*data
= session
->data
;
38 if (perf_session__read_header(session
) < 0) {
39 pr_err("incompatible file format (rerun with -v to learn more)\n");
43 if (perf_data__is_pipe(data
))
46 if (perf_header__has_feat(&session
->header
, HEADER_STAT
))
49 if (!perf_evlist__valid_sample_type(session
->evlist
)) {
50 pr_err("non matching sample_type\n");
54 if (!perf_evlist__valid_sample_id_all(session
->evlist
)) {
55 pr_err("non matching sample_id_all\n");
59 if (!perf_evlist__valid_read_format(session
->evlist
)) {
60 pr_err("non matching read_format\n");
67 void perf_session__set_id_hdr_size(struct perf_session
*session
)
69 u16 id_hdr_size
= perf_evlist__id_hdr_size(session
->evlist
);
71 machines__set_id_hdr_size(&session
->machines
, id_hdr_size
);
74 int perf_session__create_kernel_maps(struct perf_session
*session
)
76 int ret
= machine__create_kernel_maps(&session
->machines
.host
);
79 ret
= machines__create_guest_kernel_maps(&session
->machines
);
83 static void perf_session__destroy_kernel_maps(struct perf_session
*session
)
85 machines__destroy_kernel_maps(&session
->machines
);
88 static bool perf_session__has_comm_exec(struct perf_session
*session
)
90 struct perf_evsel
*evsel
;
92 evlist__for_each_entry(session
->evlist
, evsel
) {
93 if (evsel
->attr
.comm_exec
)
100 static void perf_session__set_comm_exec(struct perf_session
*session
)
102 bool comm_exec
= perf_session__has_comm_exec(session
);
104 machines__set_comm_exec(&session
->machines
, comm_exec
);
107 static int ordered_events__deliver_event(struct ordered_events
*oe
,
108 struct ordered_event
*event
)
110 struct perf_session
*session
= container_of(oe
, struct perf_session
,
113 return perf_session__deliver_event(session
, event
->event
,
114 session
->tool
, event
->file_offset
);
117 struct perf_session
*perf_session__new(struct perf_data
*data
,
118 bool repipe
, struct perf_tool
*tool
)
120 struct perf_session
*session
= zalloc(sizeof(*session
));
125 session
->repipe
= repipe
;
126 session
->tool
= tool
;
127 INIT_LIST_HEAD(&session
->auxtrace_index
);
128 machines__init(&session
->machines
);
129 ordered_events__init(&session
->ordered_events
, ordered_events__deliver_event
);
132 if (perf_data__open(data
))
135 session
->data
= data
;
137 if (perf_data__is_read(data
)) {
138 if (perf_session__open(session
) < 0)
142 * set session attributes that are present in perf.data
143 * but not in pipe-mode.
145 if (!data
->is_pipe
) {
146 perf_session__set_id_hdr_size(session
);
147 perf_session__set_comm_exec(session
);
151 session
->machines
.host
.env
= &perf_env
;
154 session
->machines
.host
.single_address_space
=
155 perf_env__single_address_space(session
->machines
.host
.env
);
157 if (!data
|| perf_data__is_write(data
)) {
159 * In O_RDONLY mode this will be performed when reading the
160 * kernel MMAP event, in perf_event__process_mmap().
162 if (perf_session__create_kernel_maps(session
) < 0)
163 pr_warning("Cannot read kernel map\n");
167 * In pipe-mode, evlist is empty until PERF_RECORD_HEADER_ATTR is
168 * processed, so perf_evlist__sample_id_all is not meaningful here.
170 if ((!data
|| !data
->is_pipe
) && tool
&& tool
->ordering_requires_timestamps
&&
171 tool
->ordered_events
&& !perf_evlist__sample_id_all(session
->evlist
)) {
172 dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n");
173 tool
->ordered_events
= false;
179 perf_data__close(data
);
181 perf_session__delete(session
);
186 static void perf_session__delete_threads(struct perf_session
*session
)
188 machine__delete_threads(&session
->machines
.host
);
191 void perf_session__delete(struct perf_session
*session
)
195 auxtrace__free(session
);
196 auxtrace_index__free(&session
->auxtrace_index
);
197 perf_session__destroy_kernel_maps(session
);
198 perf_session__delete_threads(session
);
199 perf_env__exit(&session
->header
.env
);
200 machines__exit(&session
->machines
);
202 perf_data__close(session
->data
);
206 static int process_event_synth_tracing_data_stub(struct perf_tool
*tool
208 union perf_event
*event
210 struct perf_session
*session
213 dump_printf(": unhandled!\n");
217 static int process_event_synth_attr_stub(struct perf_tool
*tool __maybe_unused
,
218 union perf_event
*event __maybe_unused
,
219 struct perf_evlist
**pevlist
222 dump_printf(": unhandled!\n");
226 static int process_event_synth_event_update_stub(struct perf_tool
*tool __maybe_unused
,
227 union perf_event
*event __maybe_unused
,
228 struct perf_evlist
**pevlist
232 perf_event__fprintf_event_update(event
, stdout
);
234 dump_printf(": unhandled!\n");
238 static int process_event_sample_stub(struct perf_tool
*tool __maybe_unused
,
239 union perf_event
*event __maybe_unused
,
240 struct perf_sample
*sample __maybe_unused
,
241 struct perf_evsel
*evsel __maybe_unused
,
242 struct machine
*machine __maybe_unused
)
244 dump_printf(": unhandled!\n");
248 static int process_event_stub(struct perf_tool
*tool __maybe_unused
,
249 union perf_event
*event __maybe_unused
,
250 struct perf_sample
*sample __maybe_unused
,
251 struct machine
*machine __maybe_unused
)
253 dump_printf(": unhandled!\n");
257 static int process_finished_round_stub(struct perf_tool
*tool __maybe_unused
,
258 union perf_event
*event __maybe_unused
,
259 struct ordered_events
*oe __maybe_unused
)
261 dump_printf(": unhandled!\n");
265 static int process_finished_round(struct perf_tool
*tool
,
266 union perf_event
*event
,
267 struct ordered_events
*oe
);
269 static int skipn(int fd
, off_t n
)
275 ret
= read(fd
, buf
, min(n
, (off_t
)sizeof(buf
)));
284 static s64
process_event_auxtrace_stub(struct perf_tool
*tool __maybe_unused
,
285 union perf_event
*event
,
286 struct perf_session
*session
289 dump_printf(": unhandled!\n");
290 if (perf_data__is_pipe(session
->data
))
291 skipn(perf_data__fd(session
->data
), event
->auxtrace
.size
);
292 return event
->auxtrace
.size
;
295 static int process_event_op2_stub(struct perf_tool
*tool __maybe_unused
,
296 union perf_event
*event __maybe_unused
,
297 struct perf_session
*session __maybe_unused
)
299 dump_printf(": unhandled!\n");
305 int process_event_thread_map_stub(struct perf_tool
*tool __maybe_unused
,
306 union perf_event
*event __maybe_unused
,
307 struct perf_session
*session __maybe_unused
)
310 perf_event__fprintf_thread_map(event
, stdout
);
312 dump_printf(": unhandled!\n");
317 int process_event_cpu_map_stub(struct perf_tool
*tool __maybe_unused
,
318 union perf_event
*event __maybe_unused
,
319 struct perf_session
*session __maybe_unused
)
322 perf_event__fprintf_cpu_map(event
, stdout
);
324 dump_printf(": unhandled!\n");
329 int process_event_stat_config_stub(struct perf_tool
*tool __maybe_unused
,
330 union perf_event
*event __maybe_unused
,
331 struct perf_session
*session __maybe_unused
)
334 perf_event__fprintf_stat_config(event
, stdout
);
336 dump_printf(": unhandled!\n");
340 static int process_stat_stub(struct perf_tool
*tool __maybe_unused
,
341 union perf_event
*event __maybe_unused
,
342 struct perf_session
*perf_session
346 perf_event__fprintf_stat(event
, stdout
);
348 dump_printf(": unhandled!\n");
352 static int process_stat_round_stub(struct perf_tool
*tool __maybe_unused
,
353 union perf_event
*event __maybe_unused
,
354 struct perf_session
*perf_session
358 perf_event__fprintf_stat_round(event
, stdout
);
360 dump_printf(": unhandled!\n");
364 void perf_tool__fill_defaults(struct perf_tool
*tool
)
366 if (tool
->sample
== NULL
)
367 tool
->sample
= process_event_sample_stub
;
368 if (tool
->mmap
== NULL
)
369 tool
->mmap
= process_event_stub
;
370 if (tool
->mmap2
== NULL
)
371 tool
->mmap2
= process_event_stub
;
372 if (tool
->comm
== NULL
)
373 tool
->comm
= process_event_stub
;
374 if (tool
->namespaces
== NULL
)
375 tool
->namespaces
= process_event_stub
;
376 if (tool
->fork
== NULL
)
377 tool
->fork
= process_event_stub
;
378 if (tool
->exit
== NULL
)
379 tool
->exit
= process_event_stub
;
380 if (tool
->lost
== NULL
)
381 tool
->lost
= perf_event__process_lost
;
382 if (tool
->lost_samples
== NULL
)
383 tool
->lost_samples
= perf_event__process_lost_samples
;
384 if (tool
->aux
== NULL
)
385 tool
->aux
= perf_event__process_aux
;
386 if (tool
->itrace_start
== NULL
)
387 tool
->itrace_start
= perf_event__process_itrace_start
;
388 if (tool
->context_switch
== NULL
)
389 tool
->context_switch
= perf_event__process_switch
;
390 if (tool
->read
== NULL
)
391 tool
->read
= process_event_sample_stub
;
392 if (tool
->throttle
== NULL
)
393 tool
->throttle
= process_event_stub
;
394 if (tool
->unthrottle
== NULL
)
395 tool
->unthrottle
= process_event_stub
;
396 if (tool
->attr
== NULL
)
397 tool
->attr
= process_event_synth_attr_stub
;
398 if (tool
->event_update
== NULL
)
399 tool
->event_update
= process_event_synth_event_update_stub
;
400 if (tool
->tracing_data
== NULL
)
401 tool
->tracing_data
= process_event_synth_tracing_data_stub
;
402 if (tool
->build_id
== NULL
)
403 tool
->build_id
= process_event_op2_stub
;
404 if (tool
->finished_round
== NULL
) {
405 if (tool
->ordered_events
)
406 tool
->finished_round
= process_finished_round
;
408 tool
->finished_round
= process_finished_round_stub
;
410 if (tool
->id_index
== NULL
)
411 tool
->id_index
= process_event_op2_stub
;
412 if (tool
->auxtrace_info
== NULL
)
413 tool
->auxtrace_info
= process_event_op2_stub
;
414 if (tool
->auxtrace
== NULL
)
415 tool
->auxtrace
= process_event_auxtrace_stub
;
416 if (tool
->auxtrace_error
== NULL
)
417 tool
->auxtrace_error
= process_event_op2_stub
;
418 if (tool
->thread_map
== NULL
)
419 tool
->thread_map
= process_event_thread_map_stub
;
420 if (tool
->cpu_map
== NULL
)
421 tool
->cpu_map
= process_event_cpu_map_stub
;
422 if (tool
->stat_config
== NULL
)
423 tool
->stat_config
= process_event_stat_config_stub
;
424 if (tool
->stat
== NULL
)
425 tool
->stat
= process_stat_stub
;
426 if (tool
->stat_round
== NULL
)
427 tool
->stat_round
= process_stat_round_stub
;
428 if (tool
->time_conv
== NULL
)
429 tool
->time_conv
= process_event_op2_stub
;
430 if (tool
->feature
== NULL
)
431 tool
->feature
= process_event_op2_stub
;
434 static void swap_sample_id_all(union perf_event
*event
, void *data
)
436 void *end
= (void *) event
+ event
->header
.size
;
437 int size
= end
- data
;
439 BUG_ON(size
% sizeof(u64
));
440 mem_bswap_64(data
, size
);
443 static void perf_event__all64_swap(union perf_event
*event
,
444 bool sample_id_all __maybe_unused
)
446 struct perf_event_header
*hdr
= &event
->header
;
447 mem_bswap_64(hdr
+ 1, event
->header
.size
- sizeof(*hdr
));
450 static void perf_event__comm_swap(union perf_event
*event
, bool sample_id_all
)
452 event
->comm
.pid
= bswap_32(event
->comm
.pid
);
453 event
->comm
.tid
= bswap_32(event
->comm
.tid
);
456 void *data
= &event
->comm
.comm
;
458 data
+= PERF_ALIGN(strlen(data
) + 1, sizeof(u64
));
459 swap_sample_id_all(event
, data
);
463 static void perf_event__mmap_swap(union perf_event
*event
,
466 event
->mmap
.pid
= bswap_32(event
->mmap
.pid
);
467 event
->mmap
.tid
= bswap_32(event
->mmap
.tid
);
468 event
->mmap
.start
= bswap_64(event
->mmap
.start
);
469 event
->mmap
.len
= bswap_64(event
->mmap
.len
);
470 event
->mmap
.pgoff
= bswap_64(event
->mmap
.pgoff
);
473 void *data
= &event
->mmap
.filename
;
475 data
+= PERF_ALIGN(strlen(data
) + 1, sizeof(u64
));
476 swap_sample_id_all(event
, data
);
480 static void perf_event__mmap2_swap(union perf_event
*event
,
483 event
->mmap2
.pid
= bswap_32(event
->mmap2
.pid
);
484 event
->mmap2
.tid
= bswap_32(event
->mmap2
.tid
);
485 event
->mmap2
.start
= bswap_64(event
->mmap2
.start
);
486 event
->mmap2
.len
= bswap_64(event
->mmap2
.len
);
487 event
->mmap2
.pgoff
= bswap_64(event
->mmap2
.pgoff
);
488 event
->mmap2
.maj
= bswap_32(event
->mmap2
.maj
);
489 event
->mmap2
.min
= bswap_32(event
->mmap2
.min
);
490 event
->mmap2
.ino
= bswap_64(event
->mmap2
.ino
);
493 void *data
= &event
->mmap2
.filename
;
495 data
+= PERF_ALIGN(strlen(data
) + 1, sizeof(u64
));
496 swap_sample_id_all(event
, data
);
499 static void perf_event__task_swap(union perf_event
*event
, bool sample_id_all
)
501 event
->fork
.pid
= bswap_32(event
->fork
.pid
);
502 event
->fork
.tid
= bswap_32(event
->fork
.tid
);
503 event
->fork
.ppid
= bswap_32(event
->fork
.ppid
);
504 event
->fork
.ptid
= bswap_32(event
->fork
.ptid
);
505 event
->fork
.time
= bswap_64(event
->fork
.time
);
508 swap_sample_id_all(event
, &event
->fork
+ 1);
511 static void perf_event__read_swap(union perf_event
*event
, bool sample_id_all
)
513 event
->read
.pid
= bswap_32(event
->read
.pid
);
514 event
->read
.tid
= bswap_32(event
->read
.tid
);
515 event
->read
.value
= bswap_64(event
->read
.value
);
516 event
->read
.time_enabled
= bswap_64(event
->read
.time_enabled
);
517 event
->read
.time_running
= bswap_64(event
->read
.time_running
);
518 event
->read
.id
= bswap_64(event
->read
.id
);
521 swap_sample_id_all(event
, &event
->read
+ 1);
524 static void perf_event__aux_swap(union perf_event
*event
, bool sample_id_all
)
526 event
->aux
.aux_offset
= bswap_64(event
->aux
.aux_offset
);
527 event
->aux
.aux_size
= bswap_64(event
->aux
.aux_size
);
528 event
->aux
.flags
= bswap_64(event
->aux
.flags
);
531 swap_sample_id_all(event
, &event
->aux
+ 1);
534 static void perf_event__itrace_start_swap(union perf_event
*event
,
537 event
->itrace_start
.pid
= bswap_32(event
->itrace_start
.pid
);
538 event
->itrace_start
.tid
= bswap_32(event
->itrace_start
.tid
);
541 swap_sample_id_all(event
, &event
->itrace_start
+ 1);
544 static void perf_event__switch_swap(union perf_event
*event
, bool sample_id_all
)
546 if (event
->header
.type
== PERF_RECORD_SWITCH_CPU_WIDE
) {
547 event
->context_switch
.next_prev_pid
=
548 bswap_32(event
->context_switch
.next_prev_pid
);
549 event
->context_switch
.next_prev_tid
=
550 bswap_32(event
->context_switch
.next_prev_tid
);
554 swap_sample_id_all(event
, &event
->context_switch
+ 1);
557 static void perf_event__throttle_swap(union perf_event
*event
,
560 event
->throttle
.time
= bswap_64(event
->throttle
.time
);
561 event
->throttle
.id
= bswap_64(event
->throttle
.id
);
562 event
->throttle
.stream_id
= bswap_64(event
->throttle
.stream_id
);
565 swap_sample_id_all(event
, &event
->throttle
+ 1);
568 static u8
revbyte(u8 b
)
570 int rev
= (b
>> 4) | ((b
& 0xf) << 4);
571 rev
= ((rev
& 0xcc) >> 2) | ((rev
& 0x33) << 2);
572 rev
= ((rev
& 0xaa) >> 1) | ((rev
& 0x55) << 1);
577 * XXX this is hack in attempt to carry flags bitfield
578 * through endian village. ABI says:
580 * Bit-fields are allocated from right to left (least to most significant)
581 * on little-endian implementations and from left to right (most to least
582 * significant) on big-endian implementations.
584 * The above seems to be byte specific, so we need to reverse each
585 * byte of the bitfield. 'Internet' also says this might be implementation
586 * specific and we probably need proper fix and carry perf_event_attr
587 * bitfield flags in separate data file FEAT_ section. Thought this seems
590 static void swap_bitfield(u8
*p
, unsigned len
)
594 for (i
= 0; i
< len
; i
++) {
600 /* exported for swapping attributes in file header */
601 void perf_event__attr_swap(struct perf_event_attr
*attr
)
603 attr
->type
= bswap_32(attr
->type
);
604 attr
->size
= bswap_32(attr
->size
);
606 #define bswap_safe(f, n) \
607 (attr->size > (offsetof(struct perf_event_attr, f) + \
608 sizeof(attr->f) * (n)))
609 #define bswap_field(f, sz) \
611 if (bswap_safe(f, 0)) \
612 attr->f = bswap_##sz(attr->f); \
614 #define bswap_field_16(f) bswap_field(f, 16)
615 #define bswap_field_32(f) bswap_field(f, 32)
616 #define bswap_field_64(f) bswap_field(f, 64)
618 bswap_field_64(config
);
619 bswap_field_64(sample_period
);
620 bswap_field_64(sample_type
);
621 bswap_field_64(read_format
);
622 bswap_field_32(wakeup_events
);
623 bswap_field_32(bp_type
);
624 bswap_field_64(bp_addr
);
625 bswap_field_64(bp_len
);
626 bswap_field_64(branch_sample_type
);
627 bswap_field_64(sample_regs_user
);
628 bswap_field_32(sample_stack_user
);
629 bswap_field_32(aux_watermark
);
630 bswap_field_16(sample_max_stack
);
633 * After read_format are bitfields. Check read_format because
634 * we are unable to use offsetof on bitfield.
636 if (bswap_safe(read_format
, 1))
637 swap_bitfield((u8
*) (&attr
->read_format
+ 1),
639 #undef bswap_field_64
640 #undef bswap_field_32
645 static void perf_event__hdr_attr_swap(union perf_event
*event
,
646 bool sample_id_all __maybe_unused
)
650 perf_event__attr_swap(&event
->attr
.attr
);
652 size
= event
->header
.size
;
653 size
-= (void *)&event
->attr
.id
- (void *)event
;
654 mem_bswap_64(event
->attr
.id
, size
);
657 static void perf_event__event_update_swap(union perf_event
*event
,
658 bool sample_id_all __maybe_unused
)
660 event
->event_update
.type
= bswap_64(event
->event_update
.type
);
661 event
->event_update
.id
= bswap_64(event
->event_update
.id
);
664 static void perf_event__event_type_swap(union perf_event
*event
,
665 bool sample_id_all __maybe_unused
)
667 event
->event_type
.event_type
.event_id
=
668 bswap_64(event
->event_type
.event_type
.event_id
);
671 static void perf_event__tracing_data_swap(union perf_event
*event
,
672 bool sample_id_all __maybe_unused
)
674 event
->tracing_data
.size
= bswap_32(event
->tracing_data
.size
);
677 static void perf_event__auxtrace_info_swap(union perf_event
*event
,
678 bool sample_id_all __maybe_unused
)
682 event
->auxtrace_info
.type
= bswap_32(event
->auxtrace_info
.type
);
684 size
= event
->header
.size
;
685 size
-= (void *)&event
->auxtrace_info
.priv
- (void *)event
;
686 mem_bswap_64(event
->auxtrace_info
.priv
, size
);
689 static void perf_event__auxtrace_swap(union perf_event
*event
,
690 bool sample_id_all __maybe_unused
)
692 event
->auxtrace
.size
= bswap_64(event
->auxtrace
.size
);
693 event
->auxtrace
.offset
= bswap_64(event
->auxtrace
.offset
);
694 event
->auxtrace
.reference
= bswap_64(event
->auxtrace
.reference
);
695 event
->auxtrace
.idx
= bswap_32(event
->auxtrace
.idx
);
696 event
->auxtrace
.tid
= bswap_32(event
->auxtrace
.tid
);
697 event
->auxtrace
.cpu
= bswap_32(event
->auxtrace
.cpu
);
700 static void perf_event__auxtrace_error_swap(union perf_event
*event
,
701 bool sample_id_all __maybe_unused
)
703 event
->auxtrace_error
.type
= bswap_32(event
->auxtrace_error
.type
);
704 event
->auxtrace_error
.code
= bswap_32(event
->auxtrace_error
.code
);
705 event
->auxtrace_error
.cpu
= bswap_32(event
->auxtrace_error
.cpu
);
706 event
->auxtrace_error
.pid
= bswap_32(event
->auxtrace_error
.pid
);
707 event
->auxtrace_error
.tid
= bswap_32(event
->auxtrace_error
.tid
);
708 event
->auxtrace_error
.ip
= bswap_64(event
->auxtrace_error
.ip
);
711 static void perf_event__thread_map_swap(union perf_event
*event
,
712 bool sample_id_all __maybe_unused
)
716 event
->thread_map
.nr
= bswap_64(event
->thread_map
.nr
);
718 for (i
= 0; i
< event
->thread_map
.nr
; i
++)
719 event
->thread_map
.entries
[i
].pid
= bswap_64(event
->thread_map
.entries
[i
].pid
);
722 static void perf_event__cpu_map_swap(union perf_event
*event
,
723 bool sample_id_all __maybe_unused
)
725 struct cpu_map_data
*data
= &event
->cpu_map
.data
;
726 struct cpu_map_entries
*cpus
;
727 struct cpu_map_mask
*mask
;
730 data
->type
= bswap_64(data
->type
);
732 switch (data
->type
) {
733 case PERF_CPU_MAP__CPUS
:
734 cpus
= (struct cpu_map_entries
*)data
->data
;
736 cpus
->nr
= bswap_16(cpus
->nr
);
738 for (i
= 0; i
< cpus
->nr
; i
++)
739 cpus
->cpu
[i
] = bswap_16(cpus
->cpu
[i
]);
741 case PERF_CPU_MAP__MASK
:
742 mask
= (struct cpu_map_mask
*) data
->data
;
744 mask
->nr
= bswap_16(mask
->nr
);
745 mask
->long_size
= bswap_16(mask
->long_size
);
747 switch (mask
->long_size
) {
748 case 4: mem_bswap_32(&mask
->mask
, mask
->nr
); break;
749 case 8: mem_bswap_64(&mask
->mask
, mask
->nr
); break;
751 pr_err("cpu_map swap: unsupported long size\n");
758 static void perf_event__stat_config_swap(union perf_event
*event
,
759 bool sample_id_all __maybe_unused
)
763 size
= event
->stat_config
.nr
* sizeof(event
->stat_config
.data
[0]);
764 size
+= 1; /* nr item itself */
765 mem_bswap_64(&event
->stat_config
.nr
, size
);
768 static void perf_event__stat_swap(union perf_event
*event
,
769 bool sample_id_all __maybe_unused
)
771 event
->stat
.id
= bswap_64(event
->stat
.id
);
772 event
->stat
.thread
= bswap_32(event
->stat
.thread
);
773 event
->stat
.cpu
= bswap_32(event
->stat
.cpu
);
774 event
->stat
.val
= bswap_64(event
->stat
.val
);
775 event
->stat
.ena
= bswap_64(event
->stat
.ena
);
776 event
->stat
.run
= bswap_64(event
->stat
.run
);
779 static void perf_event__stat_round_swap(union perf_event
*event
,
780 bool sample_id_all __maybe_unused
)
782 event
->stat_round
.type
= bswap_64(event
->stat_round
.type
);
783 event
->stat_round
.time
= bswap_64(event
->stat_round
.time
);
786 typedef void (*perf_event__swap_op
)(union perf_event
*event
,
789 static perf_event__swap_op perf_event__swap_ops
[] = {
790 [PERF_RECORD_MMAP
] = perf_event__mmap_swap
,
791 [PERF_RECORD_MMAP2
] = perf_event__mmap2_swap
,
792 [PERF_RECORD_COMM
] = perf_event__comm_swap
,
793 [PERF_RECORD_FORK
] = perf_event__task_swap
,
794 [PERF_RECORD_EXIT
] = perf_event__task_swap
,
795 [PERF_RECORD_LOST
] = perf_event__all64_swap
,
796 [PERF_RECORD_READ
] = perf_event__read_swap
,
797 [PERF_RECORD_THROTTLE
] = perf_event__throttle_swap
,
798 [PERF_RECORD_UNTHROTTLE
] = perf_event__throttle_swap
,
799 [PERF_RECORD_SAMPLE
] = perf_event__all64_swap
,
800 [PERF_RECORD_AUX
] = perf_event__aux_swap
,
801 [PERF_RECORD_ITRACE_START
] = perf_event__itrace_start_swap
,
802 [PERF_RECORD_LOST_SAMPLES
] = perf_event__all64_swap
,
803 [PERF_RECORD_SWITCH
] = perf_event__switch_swap
,
804 [PERF_RECORD_SWITCH_CPU_WIDE
] = perf_event__switch_swap
,
805 [PERF_RECORD_HEADER_ATTR
] = perf_event__hdr_attr_swap
,
806 [PERF_RECORD_HEADER_EVENT_TYPE
] = perf_event__event_type_swap
,
807 [PERF_RECORD_HEADER_TRACING_DATA
] = perf_event__tracing_data_swap
,
808 [PERF_RECORD_HEADER_BUILD_ID
] = NULL
,
809 [PERF_RECORD_ID_INDEX
] = perf_event__all64_swap
,
810 [PERF_RECORD_AUXTRACE_INFO
] = perf_event__auxtrace_info_swap
,
811 [PERF_RECORD_AUXTRACE
] = perf_event__auxtrace_swap
,
812 [PERF_RECORD_AUXTRACE_ERROR
] = perf_event__auxtrace_error_swap
,
813 [PERF_RECORD_THREAD_MAP
] = perf_event__thread_map_swap
,
814 [PERF_RECORD_CPU_MAP
] = perf_event__cpu_map_swap
,
815 [PERF_RECORD_STAT_CONFIG
] = perf_event__stat_config_swap
,
816 [PERF_RECORD_STAT
] = perf_event__stat_swap
,
817 [PERF_RECORD_STAT_ROUND
] = perf_event__stat_round_swap
,
818 [PERF_RECORD_EVENT_UPDATE
] = perf_event__event_update_swap
,
819 [PERF_RECORD_TIME_CONV
] = perf_event__all64_swap
,
820 [PERF_RECORD_HEADER_MAX
] = NULL
,
824 * When perf record finishes a pass on every buffers, it records this pseudo
826 * We record the max timestamp t found in the pass n.
827 * Assuming these timestamps are monotonic across cpus, we know that if
828 * a buffer still has events with timestamps below t, they will be all
829 * available and then read in the pass n + 1.
830 * Hence when we start to read the pass n + 2, we can safely flush every
831 * events with timestamps below t.
833 * ============ PASS n =================
836 * cnt1 timestamps | cnt2 timestamps
839 * - | 4 <--- max recorded
841 * ============ PASS n + 1 ==============
844 * cnt1 timestamps | cnt2 timestamps
847 * 5 | 7 <---- max recorded
849 * Flush every events below timestamp 4
851 * ============ PASS n + 2 ==============
854 * cnt1 timestamps | cnt2 timestamps
859 * Flush every events below timestamp 7
862 static int process_finished_round(struct perf_tool
*tool __maybe_unused
,
863 union perf_event
*event __maybe_unused
,
864 struct ordered_events
*oe
)
867 fprintf(stdout
, "\n");
868 return ordered_events__flush(oe
, OE_FLUSH__ROUND
);
871 int perf_session__queue_event(struct perf_session
*s
, union perf_event
*event
,
872 u64 timestamp
, u64 file_offset
)
874 return ordered_events__queue(&s
->ordered_events
, event
, timestamp
, file_offset
);
877 static void callchain__lbr_callstack_printf(struct perf_sample
*sample
)
879 struct ip_callchain
*callchain
= sample
->callchain
;
880 struct branch_stack
*lbr_stack
= sample
->branch_stack
;
881 u64 kernel_callchain_nr
= callchain
->nr
;
884 for (i
= 0; i
< kernel_callchain_nr
; i
++) {
885 if (callchain
->ips
[i
] == PERF_CONTEXT_USER
)
889 if ((i
!= kernel_callchain_nr
) && lbr_stack
->nr
) {
892 * LBR callstack can only get user call chain,
893 * i is kernel call chain number,
894 * 1 is PERF_CONTEXT_USER.
896 * The user call chain is stored in LBR registers.
897 * LBR are pair registers. The caller is stored
898 * in "from" register, while the callee is stored
900 * For example, there is a call stack
901 * "A"->"B"->"C"->"D".
902 * The LBR registers will recorde like
903 * "C"->"D", "B"->"C", "A"->"B".
904 * So only the first "to" register and all "from"
905 * registers are needed to construct the whole stack.
907 total_nr
= i
+ 1 + lbr_stack
->nr
+ 1;
908 kernel_callchain_nr
= i
+ 1;
910 printf("... LBR call chain: nr:%" PRIu64
"\n", total_nr
);
912 for (i
= 0; i
< kernel_callchain_nr
; i
++)
913 printf("..... %2d: %016" PRIx64
"\n",
914 i
, callchain
->ips
[i
]);
916 printf("..... %2d: %016" PRIx64
"\n",
917 (int)(kernel_callchain_nr
), lbr_stack
->entries
[0].to
);
918 for (i
= 0; i
< lbr_stack
->nr
; i
++)
919 printf("..... %2d: %016" PRIx64
"\n",
920 (int)(i
+ kernel_callchain_nr
+ 1), lbr_stack
->entries
[i
].from
);
924 static void callchain__printf(struct perf_evsel
*evsel
,
925 struct perf_sample
*sample
)
928 struct ip_callchain
*callchain
= sample
->callchain
;
930 if (perf_evsel__has_branch_callstack(evsel
))
931 callchain__lbr_callstack_printf(sample
);
933 printf("... FP chain: nr:%" PRIu64
"\n", callchain
->nr
);
935 for (i
= 0; i
< callchain
->nr
; i
++)
936 printf("..... %2d: %016" PRIx64
"\n",
937 i
, callchain
->ips
[i
]);
940 static void branch_stack__printf(struct perf_sample
*sample
)
944 printf("... branch stack: nr:%" PRIu64
"\n", sample
->branch_stack
->nr
);
946 for (i
= 0; i
< sample
->branch_stack
->nr
; i
++) {
947 struct branch_entry
*e
= &sample
->branch_stack
->entries
[i
];
949 printf("..... %2"PRIu64
": %016" PRIx64
" -> %016" PRIx64
" %hu cycles %s%s%s%s %x\n",
951 (unsigned short)e
->flags
.cycles
,
952 e
->flags
.mispred
? "M" : " ",
953 e
->flags
.predicted
? "P" : " ",
954 e
->flags
.abort
? "A" : " ",
955 e
->flags
.in_tx
? "T" : " ",
956 (unsigned)e
->flags
.reserved
);
960 static void regs_dump__printf(u64 mask
, u64
*regs
)
964 for_each_set_bit(rid
, (unsigned long *) &mask
, sizeof(mask
) * 8) {
967 printf(".... %-5s 0x%" PRIx64
"\n",
968 perf_reg_name(rid
), val
);
972 static const char *regs_abi
[] = {
973 [PERF_SAMPLE_REGS_ABI_NONE
] = "none",
974 [PERF_SAMPLE_REGS_ABI_32
] = "32-bit",
975 [PERF_SAMPLE_REGS_ABI_64
] = "64-bit",
978 static inline const char *regs_dump_abi(struct regs_dump
*d
)
980 if (d
->abi
> PERF_SAMPLE_REGS_ABI_64
)
983 return regs_abi
[d
->abi
];
986 static void regs__printf(const char *type
, struct regs_dump
*regs
)
988 u64 mask
= regs
->mask
;
990 printf("... %s regs: mask 0x%" PRIx64
" ABI %s\n",
993 regs_dump_abi(regs
));
995 regs_dump__printf(mask
, regs
->regs
);
998 static void regs_user__printf(struct perf_sample
*sample
)
1000 struct regs_dump
*user_regs
= &sample
->user_regs
;
1002 if (user_regs
->regs
)
1003 regs__printf("user", user_regs
);
1006 static void regs_intr__printf(struct perf_sample
*sample
)
1008 struct regs_dump
*intr_regs
= &sample
->intr_regs
;
1010 if (intr_regs
->regs
)
1011 regs__printf("intr", intr_regs
);
1014 static void stack_user__printf(struct stack_dump
*dump
)
1016 printf("... ustack: size %" PRIu64
", offset 0x%x\n",
1017 dump
->size
, dump
->offset
);
1020 static void perf_evlist__print_tstamp(struct perf_evlist
*evlist
,
1021 union perf_event
*event
,
1022 struct perf_sample
*sample
)
1024 u64 sample_type
= __perf_evlist__combined_sample_type(evlist
);
1026 if (event
->header
.type
!= PERF_RECORD_SAMPLE
&&
1027 !perf_evlist__sample_id_all(evlist
)) {
1028 fputs("-1 -1 ", stdout
);
1032 if ((sample_type
& PERF_SAMPLE_CPU
))
1033 printf("%u ", sample
->cpu
);
1035 if (sample_type
& PERF_SAMPLE_TIME
)
1036 printf("%" PRIu64
" ", sample
->time
);
1039 static void sample_read__printf(struct perf_sample
*sample
, u64 read_format
)
1041 printf("... sample_read:\n");
1043 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
1044 printf("...... time enabled %016" PRIx64
"\n",
1045 sample
->read
.time_enabled
);
1047 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
1048 printf("...... time running %016" PRIx64
"\n",
1049 sample
->read
.time_running
);
1051 if (read_format
& PERF_FORMAT_GROUP
) {
1054 printf(".... group nr %" PRIu64
"\n", sample
->read
.group
.nr
);
1056 for (i
= 0; i
< sample
->read
.group
.nr
; i
++) {
1057 struct sample_read_value
*value
;
1059 value
= &sample
->read
.group
.values
[i
];
1060 printf("..... id %016" PRIx64
1061 ", value %016" PRIx64
"\n",
1062 value
->id
, value
->value
);
1065 printf("..... id %016" PRIx64
", value %016" PRIx64
"\n",
1066 sample
->read
.one
.id
, sample
->read
.one
.value
);
1069 static void dump_event(struct perf_evlist
*evlist
, union perf_event
*event
,
1070 u64 file_offset
, struct perf_sample
*sample
)
1075 printf("\n%#" PRIx64
" [%#x]: event: %d\n",
1076 file_offset
, event
->header
.size
, event
->header
.type
);
1081 perf_evlist__print_tstamp(evlist
, event
, sample
);
1083 printf("%#" PRIx64
" [%#x]: PERF_RECORD_%s", file_offset
,
1084 event
->header
.size
, perf_event__name(event
->header
.type
));
1087 static void dump_sample(struct perf_evsel
*evsel
, union perf_event
*event
,
1088 struct perf_sample
*sample
)
1095 printf("(IP, 0x%x): %d/%d: %#" PRIx64
" period: %" PRIu64
" addr: %#" PRIx64
"\n",
1096 event
->header
.misc
, sample
->pid
, sample
->tid
, sample
->ip
,
1097 sample
->period
, sample
->addr
);
1099 sample_type
= evsel
->attr
.sample_type
;
1101 if (evsel__has_callchain(evsel
))
1102 callchain__printf(evsel
, sample
);
1104 if ((sample_type
& PERF_SAMPLE_BRANCH_STACK
) && !perf_evsel__has_branch_callstack(evsel
))
1105 branch_stack__printf(sample
);
1107 if (sample_type
& PERF_SAMPLE_REGS_USER
)
1108 regs_user__printf(sample
);
1110 if (sample_type
& PERF_SAMPLE_REGS_INTR
)
1111 regs_intr__printf(sample
);
1113 if (sample_type
& PERF_SAMPLE_STACK_USER
)
1114 stack_user__printf(&sample
->user_stack
);
1116 if (sample_type
& PERF_SAMPLE_WEIGHT
)
1117 printf("... weight: %" PRIu64
"\n", sample
->weight
);
1119 if (sample_type
& PERF_SAMPLE_DATA_SRC
)
1120 printf(" . data_src: 0x%"PRIx64
"\n", sample
->data_src
);
1122 if (sample_type
& PERF_SAMPLE_PHYS_ADDR
)
1123 printf(" .. phys_addr: 0x%"PRIx64
"\n", sample
->phys_addr
);
1125 if (sample_type
& PERF_SAMPLE_TRANSACTION
)
1126 printf("... transaction: %" PRIx64
"\n", sample
->transaction
);
1128 if (sample_type
& PERF_SAMPLE_READ
)
1129 sample_read__printf(sample
, evsel
->attr
.read_format
);
1132 static void dump_read(struct perf_evsel
*evsel
, union perf_event
*event
)
1134 struct read_event
*read_event
= &event
->read
;
1140 printf(": %d %d %s %" PRIu64
"\n", event
->read
.pid
, event
->read
.tid
,
1141 evsel
? perf_evsel__name(evsel
) : "FAIL",
1144 read_format
= evsel
->attr
.read_format
;
1146 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
1147 printf("... time enabled : %" PRIu64
"\n", read_event
->time_enabled
);
1149 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
1150 printf("... time running : %" PRIu64
"\n", read_event
->time_running
);
1152 if (read_format
& PERF_FORMAT_ID
)
1153 printf("... id : %" PRIu64
"\n", read_event
->id
);
1156 static struct machine
*machines__find_for_cpumode(struct machines
*machines
,
1157 union perf_event
*event
,
1158 struct perf_sample
*sample
)
1160 struct machine
*machine
;
1163 ((sample
->cpumode
== PERF_RECORD_MISC_GUEST_KERNEL
) ||
1164 (sample
->cpumode
== PERF_RECORD_MISC_GUEST_USER
))) {
1167 if (event
->header
.type
== PERF_RECORD_MMAP
1168 || event
->header
.type
== PERF_RECORD_MMAP2
)
1169 pid
= event
->mmap
.pid
;
1173 machine
= machines__find(machines
, pid
);
1175 machine
= machines__findnew(machines
, DEFAULT_GUEST_KERNEL_ID
);
1179 return &machines
->host
;
1182 static int deliver_sample_value(struct perf_evlist
*evlist
,
1183 struct perf_tool
*tool
,
1184 union perf_event
*event
,
1185 struct perf_sample
*sample
,
1186 struct sample_read_value
*v
,
1187 struct machine
*machine
)
1189 struct perf_sample_id
*sid
= perf_evlist__id2sid(evlist
, v
->id
);
1193 sample
->period
= v
->value
- sid
->period
;
1194 sid
->period
= v
->value
;
1197 if (!sid
|| sid
->evsel
== NULL
) {
1198 ++evlist
->stats
.nr_unknown_id
;
1202 return tool
->sample(tool
, event
, sample
, sid
->evsel
, machine
);
1205 static int deliver_sample_group(struct perf_evlist
*evlist
,
1206 struct perf_tool
*tool
,
1207 union perf_event
*event
,
1208 struct perf_sample
*sample
,
1209 struct machine
*machine
)
1214 for (i
= 0; i
< sample
->read
.group
.nr
; i
++) {
1215 ret
= deliver_sample_value(evlist
, tool
, event
, sample
,
1216 &sample
->read
.group
.values
[i
],
1226 perf_evlist__deliver_sample(struct perf_evlist
*evlist
,
1227 struct perf_tool
*tool
,
1228 union perf_event
*event
,
1229 struct perf_sample
*sample
,
1230 struct perf_evsel
*evsel
,
1231 struct machine
*machine
)
1233 /* We know evsel != NULL. */
1234 u64 sample_type
= evsel
->attr
.sample_type
;
1235 u64 read_format
= evsel
->attr
.read_format
;
1237 /* Standard sample delivery. */
1238 if (!(sample_type
& PERF_SAMPLE_READ
))
1239 return tool
->sample(tool
, event
, sample
, evsel
, machine
);
1241 /* For PERF_SAMPLE_READ we have either single or group mode. */
1242 if (read_format
& PERF_FORMAT_GROUP
)
1243 return deliver_sample_group(evlist
, tool
, event
, sample
,
1246 return deliver_sample_value(evlist
, tool
, event
, sample
,
1247 &sample
->read
.one
, machine
);
1250 static int machines__deliver_event(struct machines
*machines
,
1251 struct perf_evlist
*evlist
,
1252 union perf_event
*event
,
1253 struct perf_sample
*sample
,
1254 struct perf_tool
*tool
, u64 file_offset
)
1256 struct perf_evsel
*evsel
;
1257 struct machine
*machine
;
1259 dump_event(evlist
, event
, file_offset
, sample
);
1261 evsel
= perf_evlist__id2evsel(evlist
, sample
->id
);
1263 machine
= machines__find_for_cpumode(machines
, event
, sample
);
1265 switch (event
->header
.type
) {
1266 case PERF_RECORD_SAMPLE
:
1267 if (evsel
== NULL
) {
1268 ++evlist
->stats
.nr_unknown_id
;
1271 dump_sample(evsel
, event
, sample
);
1272 if (machine
== NULL
) {
1273 ++evlist
->stats
.nr_unprocessable_samples
;
1276 return perf_evlist__deliver_sample(evlist
, tool
, event
, sample
, evsel
, machine
);
1277 case PERF_RECORD_MMAP
:
1278 return tool
->mmap(tool
, event
, sample
, machine
);
1279 case PERF_RECORD_MMAP2
:
1280 if (event
->header
.misc
& PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT
)
1281 ++evlist
->stats
.nr_proc_map_timeout
;
1282 return tool
->mmap2(tool
, event
, sample
, machine
);
1283 case PERF_RECORD_COMM
:
1284 return tool
->comm(tool
, event
, sample
, machine
);
1285 case PERF_RECORD_NAMESPACES
:
1286 return tool
->namespaces(tool
, event
, sample
, machine
);
1287 case PERF_RECORD_FORK
:
1288 return tool
->fork(tool
, event
, sample
, machine
);
1289 case PERF_RECORD_EXIT
:
1290 return tool
->exit(tool
, event
, sample
, machine
);
1291 case PERF_RECORD_LOST
:
1292 if (tool
->lost
== perf_event__process_lost
)
1293 evlist
->stats
.total_lost
+= event
->lost
.lost
;
1294 return tool
->lost(tool
, event
, sample
, machine
);
1295 case PERF_RECORD_LOST_SAMPLES
:
1296 if (tool
->lost_samples
== perf_event__process_lost_samples
)
1297 evlist
->stats
.total_lost_samples
+= event
->lost_samples
.lost
;
1298 return tool
->lost_samples(tool
, event
, sample
, machine
);
1299 case PERF_RECORD_READ
:
1300 dump_read(evsel
, event
);
1301 return tool
->read(tool
, event
, sample
, evsel
, machine
);
1302 case PERF_RECORD_THROTTLE
:
1303 return tool
->throttle(tool
, event
, sample
, machine
);
1304 case PERF_RECORD_UNTHROTTLE
:
1305 return tool
->unthrottle(tool
, event
, sample
, machine
);
1306 case PERF_RECORD_AUX
:
1307 if (tool
->aux
== perf_event__process_aux
) {
1308 if (event
->aux
.flags
& PERF_AUX_FLAG_TRUNCATED
)
1309 evlist
->stats
.total_aux_lost
+= 1;
1310 if (event
->aux
.flags
& PERF_AUX_FLAG_PARTIAL
)
1311 evlist
->stats
.total_aux_partial
+= 1;
1313 return tool
->aux(tool
, event
, sample
, machine
);
1314 case PERF_RECORD_ITRACE_START
:
1315 return tool
->itrace_start(tool
, event
, sample
, machine
);
1316 case PERF_RECORD_SWITCH
:
1317 case PERF_RECORD_SWITCH_CPU_WIDE
:
1318 return tool
->context_switch(tool
, event
, sample
, machine
);
1320 ++evlist
->stats
.nr_unknown_events
;
1325 static int perf_session__deliver_event(struct perf_session
*session
,
1326 union perf_event
*event
,
1327 struct perf_tool
*tool
,
1330 struct perf_sample sample
;
1333 ret
= perf_evlist__parse_sample(session
->evlist
, event
, &sample
);
1335 pr_err("Can't parse sample, err = %d\n", ret
);
1339 ret
= auxtrace__process_event(session
, event
, &sample
, tool
);
1345 return machines__deliver_event(&session
->machines
, session
->evlist
,
1346 event
, &sample
, tool
, file_offset
);
1349 static s64
perf_session__process_user_event(struct perf_session
*session
,
1350 union perf_event
*event
,
1353 struct ordered_events
*oe
= &session
->ordered_events
;
1354 struct perf_tool
*tool
= session
->tool
;
1355 struct perf_sample sample
= { .time
= 0, };
1356 int fd
= perf_data__fd(session
->data
);
1359 dump_event(session
->evlist
, event
, file_offset
, &sample
);
1361 /* These events are processed right away */
1362 switch (event
->header
.type
) {
1363 case PERF_RECORD_HEADER_ATTR
:
1364 err
= tool
->attr(tool
, event
, &session
->evlist
);
1366 perf_session__set_id_hdr_size(session
);
1367 perf_session__set_comm_exec(session
);
1370 case PERF_RECORD_EVENT_UPDATE
:
1371 return tool
->event_update(tool
, event
, &session
->evlist
);
1372 case PERF_RECORD_HEADER_EVENT_TYPE
:
1374 * Depreceated, but we need to handle it for sake
1375 * of old data files create in pipe mode.
1378 case PERF_RECORD_HEADER_TRACING_DATA
:
1379 /* setup for reading amidst mmap */
1380 lseek(fd
, file_offset
, SEEK_SET
);
1381 return tool
->tracing_data(tool
, event
, session
);
1382 case PERF_RECORD_HEADER_BUILD_ID
:
1383 return tool
->build_id(tool
, event
, session
);
1384 case PERF_RECORD_FINISHED_ROUND
:
1385 return tool
->finished_round(tool
, event
, oe
);
1386 case PERF_RECORD_ID_INDEX
:
1387 return tool
->id_index(tool
, event
, session
);
1388 case PERF_RECORD_AUXTRACE_INFO
:
1389 return tool
->auxtrace_info(tool
, event
, session
);
1390 case PERF_RECORD_AUXTRACE
:
1391 /* setup for reading amidst mmap */
1392 lseek(fd
, file_offset
+ event
->header
.size
, SEEK_SET
);
1393 return tool
->auxtrace(tool
, event
, session
);
1394 case PERF_RECORD_AUXTRACE_ERROR
:
1395 perf_session__auxtrace_error_inc(session
, event
);
1396 return tool
->auxtrace_error(tool
, event
, session
);
1397 case PERF_RECORD_THREAD_MAP
:
1398 return tool
->thread_map(tool
, event
, session
);
1399 case PERF_RECORD_CPU_MAP
:
1400 return tool
->cpu_map(tool
, event
, session
);
1401 case PERF_RECORD_STAT_CONFIG
:
1402 return tool
->stat_config(tool
, event
, session
);
1403 case PERF_RECORD_STAT
:
1404 return tool
->stat(tool
, event
, session
);
1405 case PERF_RECORD_STAT_ROUND
:
1406 return tool
->stat_round(tool
, event
, session
);
1407 case PERF_RECORD_TIME_CONV
:
1408 session
->time_conv
= event
->time_conv
;
1409 return tool
->time_conv(tool
, event
, session
);
1410 case PERF_RECORD_HEADER_FEATURE
:
1411 return tool
->feature(tool
, event
, session
);
1417 int perf_session__deliver_synth_event(struct perf_session
*session
,
1418 union perf_event
*event
,
1419 struct perf_sample
*sample
)
1421 struct perf_evlist
*evlist
= session
->evlist
;
1422 struct perf_tool
*tool
= session
->tool
;
1424 events_stats__inc(&evlist
->stats
, event
->header
.type
);
1426 if (event
->header
.type
>= PERF_RECORD_USER_TYPE_START
)
1427 return perf_session__process_user_event(session
, event
, 0);
1429 return machines__deliver_event(&session
->machines
, evlist
, event
, sample
, tool
, 0);
1432 static void event_swap(union perf_event
*event
, bool sample_id_all
)
1434 perf_event__swap_op swap
;
1436 swap
= perf_event__swap_ops
[event
->header
.type
];
1438 swap(event
, sample_id_all
);
1441 int perf_session__peek_event(struct perf_session
*session
, off_t file_offset
,
1442 void *buf
, size_t buf_sz
,
1443 union perf_event
**event_ptr
,
1444 struct perf_sample
*sample
)
1446 union perf_event
*event
;
1447 size_t hdr_sz
, rest
;
1450 if (session
->one_mmap
&& !session
->header
.needs_swap
) {
1451 event
= file_offset
- session
->one_mmap_offset
+
1452 session
->one_mmap_addr
;
1453 goto out_parse_sample
;
1456 if (perf_data__is_pipe(session
->data
))
1459 fd
= perf_data__fd(session
->data
);
1460 hdr_sz
= sizeof(struct perf_event_header
);
1462 if (buf_sz
< hdr_sz
)
1465 if (lseek(fd
, file_offset
, SEEK_SET
) == (off_t
)-1 ||
1466 readn(fd
, buf
, hdr_sz
) != (ssize_t
)hdr_sz
)
1469 event
= (union perf_event
*)buf
;
1471 if (session
->header
.needs_swap
)
1472 perf_event_header__bswap(&event
->header
);
1474 if (event
->header
.size
< hdr_sz
|| event
->header
.size
> buf_sz
)
1477 rest
= event
->header
.size
- hdr_sz
;
1479 if (readn(fd
, buf
, rest
) != (ssize_t
)rest
)
1482 if (session
->header
.needs_swap
)
1483 event_swap(event
, perf_evlist__sample_id_all(session
->evlist
));
1487 if (sample
&& event
->header
.type
< PERF_RECORD_USER_TYPE_START
&&
1488 perf_evlist__parse_sample(session
->evlist
, event
, sample
))
1496 static s64
perf_session__process_event(struct perf_session
*session
,
1497 union perf_event
*event
, u64 file_offset
)
1499 struct perf_evlist
*evlist
= session
->evlist
;
1500 struct perf_tool
*tool
= session
->tool
;
1503 if (session
->header
.needs_swap
)
1504 event_swap(event
, perf_evlist__sample_id_all(evlist
));
1506 if (event
->header
.type
>= PERF_RECORD_HEADER_MAX
)
1509 events_stats__inc(&evlist
->stats
, event
->header
.type
);
1511 if (event
->header
.type
>= PERF_RECORD_USER_TYPE_START
)
1512 return perf_session__process_user_event(session
, event
, file_offset
);
1514 if (tool
->ordered_events
) {
1515 u64 timestamp
= -1ULL;
1517 ret
= perf_evlist__parse_sample_timestamp(evlist
, event
, ×tamp
);
1518 if (ret
&& ret
!= -1)
1521 ret
= perf_session__queue_event(session
, event
, timestamp
, file_offset
);
1526 return perf_session__deliver_event(session
, event
, tool
, file_offset
);
1529 void perf_event_header__bswap(struct perf_event_header
*hdr
)
1531 hdr
->type
= bswap_32(hdr
->type
);
1532 hdr
->misc
= bswap_16(hdr
->misc
);
1533 hdr
->size
= bswap_16(hdr
->size
);
1536 struct thread
*perf_session__findnew(struct perf_session
*session
, pid_t pid
)
1538 return machine__findnew_thread(&session
->machines
.host
, -1, pid
);
1541 int perf_session__register_idle_thread(struct perf_session
*session
)
1543 struct thread
*thread
;
1546 thread
= machine__findnew_thread(&session
->machines
.host
, 0, 0);
1547 if (thread
== NULL
|| thread__set_comm(thread
, "swapper", 0)) {
1548 pr_err("problem inserting idle task.\n");
1552 if (thread
== NULL
|| thread__set_namespaces(thread
, 0, NULL
)) {
1553 pr_err("problem inserting idle task.\n");
1557 /* machine__findnew_thread() got the thread, so put it */
1558 thread__put(thread
);
1563 perf_session__warn_order(const struct perf_session
*session
)
1565 const struct ordered_events
*oe
= &session
->ordered_events
;
1566 struct perf_evsel
*evsel
;
1567 bool should_warn
= true;
1569 evlist__for_each_entry(session
->evlist
, evsel
) {
1570 if (evsel
->attr
.write_backward
)
1571 should_warn
= false;
1576 if (oe
->nr_unordered_events
!= 0)
1577 ui__warning("%u out of order events recorded.\n", oe
->nr_unordered_events
);
1580 static void perf_session__warn_about_errors(const struct perf_session
*session
)
1582 const struct events_stats
*stats
= &session
->evlist
->stats
;
1584 if (session
->tool
->lost
== perf_event__process_lost
&&
1585 stats
->nr_events
[PERF_RECORD_LOST
] != 0) {
1586 ui__warning("Processed %d events and lost %d chunks!\n\n"
1587 "Check IO/CPU overload!\n\n",
1588 stats
->nr_events
[0],
1589 stats
->nr_events
[PERF_RECORD_LOST
]);
1592 if (session
->tool
->lost_samples
== perf_event__process_lost_samples
) {
1595 drop_rate
= (double)stats
->total_lost_samples
/
1596 (double) (stats
->nr_events
[PERF_RECORD_SAMPLE
] + stats
->total_lost_samples
);
1597 if (drop_rate
> 0.05) {
1598 ui__warning("Processed %" PRIu64
" samples and lost %3.2f%%!\n\n",
1599 stats
->nr_events
[PERF_RECORD_SAMPLE
] + stats
->total_lost_samples
,
1604 if (session
->tool
->aux
== perf_event__process_aux
&&
1605 stats
->total_aux_lost
!= 0) {
1606 ui__warning("AUX data lost %" PRIu64
" times out of %u!\n\n",
1607 stats
->total_aux_lost
,
1608 stats
->nr_events
[PERF_RECORD_AUX
]);
1611 if (session
->tool
->aux
== perf_event__process_aux
&&
1612 stats
->total_aux_partial
!= 0) {
1613 bool vmm_exclusive
= false;
1615 (void)sysfs__read_bool("module/kvm_intel/parameters/vmm_exclusive",
1618 ui__warning("AUX data had gaps in it %" PRIu64
" times out of %u!\n\n"
1619 "Are you running a KVM guest in the background?%s\n\n",
1620 stats
->total_aux_partial
,
1621 stats
->nr_events
[PERF_RECORD_AUX
],
1623 "\nReloading kvm_intel module with vmm_exclusive=0\n"
1624 "will reduce the gaps to only guest's timeslices." :
1628 if (stats
->nr_unknown_events
!= 0) {
1629 ui__warning("Found %u unknown events!\n\n"
1630 "Is this an older tool processing a perf.data "
1631 "file generated by a more recent tool?\n\n"
1632 "If that is not the case, consider "
1633 "reporting to linux-kernel@vger.kernel.org.\n\n",
1634 stats
->nr_unknown_events
);
1637 if (stats
->nr_unknown_id
!= 0) {
1638 ui__warning("%u samples with id not present in the header\n",
1639 stats
->nr_unknown_id
);
1642 if (stats
->nr_invalid_chains
!= 0) {
1643 ui__warning("Found invalid callchains!\n\n"
1644 "%u out of %u events were discarded for this reason.\n\n"
1645 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
1646 stats
->nr_invalid_chains
,
1647 stats
->nr_events
[PERF_RECORD_SAMPLE
]);
1650 if (stats
->nr_unprocessable_samples
!= 0) {
1651 ui__warning("%u unprocessable samples recorded.\n"
1652 "Do you have a KVM guest running and not using 'perf kvm'?\n",
1653 stats
->nr_unprocessable_samples
);
1656 perf_session__warn_order(session
);
1658 events_stats__auxtrace_error_warn(stats
);
1660 if (stats
->nr_proc_map_timeout
!= 0) {
1661 ui__warning("%d map information files for pre-existing threads were\n"
1662 "not processed, if there are samples for addresses they\n"
1663 "will not be resolved, you may find out which are these\n"
1664 "threads by running with -v and redirecting the output\n"
1666 "The time limit to process proc map is too short?\n"
1667 "Increase it by --proc-map-timeout\n",
1668 stats
->nr_proc_map_timeout
);
1672 static int perf_session__flush_thread_stack(struct thread
*thread
,
1673 void *p __maybe_unused
)
1675 return thread_stack__flush(thread
);
1678 static int perf_session__flush_thread_stacks(struct perf_session
*session
)
1680 return machines__for_each_thread(&session
->machines
,
1681 perf_session__flush_thread_stack
,
1685 volatile int session_done
;
1687 static int __perf_session__process_pipe_events(struct perf_session
*session
)
1689 struct ordered_events
*oe
= &session
->ordered_events
;
1690 struct perf_tool
*tool
= session
->tool
;
1691 int fd
= perf_data__fd(session
->data
);
1692 union perf_event
*event
;
1693 uint32_t size
, cur_size
= 0;
1700 perf_tool__fill_defaults(tool
);
1703 cur_size
= sizeof(union perf_event
);
1705 buf
= malloc(cur_size
);
1708 ordered_events__set_copy_on_queue(oe
, true);
1711 err
= readn(fd
, event
, sizeof(struct perf_event_header
));
1716 pr_err("failed to read event header\n");
1720 if (session
->header
.needs_swap
)
1721 perf_event_header__bswap(&event
->header
);
1723 size
= event
->header
.size
;
1724 if (size
< sizeof(struct perf_event_header
)) {
1725 pr_err("bad event header size\n");
1729 if (size
> cur_size
) {
1730 void *new = realloc(buf
, size
);
1732 pr_err("failed to allocate memory to read event\n");
1740 p
+= sizeof(struct perf_event_header
);
1742 if (size
- sizeof(struct perf_event_header
)) {
1743 err
= readn(fd
, p
, size
- sizeof(struct perf_event_header
));
1746 pr_err("unexpected end of event stream\n");
1750 pr_err("failed to read event data\n");
1755 if ((skip
= perf_session__process_event(session
, event
, head
)) < 0) {
1756 pr_err("%#" PRIx64
" [%#x]: failed to process type: %d\n",
1757 head
, event
->header
.size
, event
->header
.type
);
1767 if (!session_done())
1770 /* do the final flush for ordered samples */
1771 err
= ordered_events__flush(oe
, OE_FLUSH__FINAL
);
1774 err
= auxtrace__flush_events(session
, tool
);
1777 err
= perf_session__flush_thread_stacks(session
);
1781 perf_session__warn_about_errors(session
);
1782 ordered_events__free(&session
->ordered_events
);
1783 auxtrace__free_events(session
);
1787 static union perf_event
*
1788 fetch_mmaped_event(struct perf_session
*session
,
1789 u64 head
, size_t mmap_size
, char *buf
)
1791 union perf_event
*event
;
1794 * Ensure we have enough space remaining to read
1795 * the size of the event in the headers.
1797 if (head
+ sizeof(event
->header
) > mmap_size
)
1800 event
= (union perf_event
*)(buf
+ head
);
1802 if (session
->header
.needs_swap
)
1803 perf_event_header__bswap(&event
->header
);
1805 if (head
+ event
->header
.size
> mmap_size
) {
1806 /* We're not fetching the event so swap back again */
1807 if (session
->header
.needs_swap
)
1808 perf_event_header__bswap(&event
->header
);
1816 * On 64bit we can mmap the data file in one go. No need for tiny mmap
1817 * slices. On 32bit we use 32MB.
1819 #if BITS_PER_LONG == 64
1820 #define MMAP_SIZE ULLONG_MAX
1823 #define MMAP_SIZE (32 * 1024 * 1024ULL)
1824 #define NUM_MMAPS 128
1827 static int __perf_session__process_events(struct perf_session
*session
,
1828 u64 data_offset
, u64 data_size
,
1831 struct ordered_events
*oe
= &session
->ordered_events
;
1832 struct perf_tool
*tool
= session
->tool
;
1833 int fd
= perf_data__fd(session
->data
);
1834 u64 head
, page_offset
, file_offset
, file_pos
, size
;
1835 int err
, mmap_prot
, mmap_flags
, map_idx
= 0;
1837 char *buf
, *mmaps
[NUM_MMAPS
];
1838 union perf_event
*event
;
1839 struct ui_progress prog
;
1842 perf_tool__fill_defaults(tool
);
1844 page_offset
= page_size
* (data_offset
/ page_size
);
1845 file_offset
= page_offset
;
1846 head
= data_offset
- page_offset
;
1851 if (data_offset
+ data_size
< file_size
)
1852 file_size
= data_offset
+ data_size
;
1854 ui_progress__init_size(&prog
, file_size
, "Processing events...");
1856 mmap_size
= MMAP_SIZE
;
1857 if (mmap_size
> file_size
) {
1858 mmap_size
= file_size
;
1859 session
->one_mmap
= true;
1862 memset(mmaps
, 0, sizeof(mmaps
));
1864 mmap_prot
= PROT_READ
;
1865 mmap_flags
= MAP_SHARED
;
1867 if (session
->header
.needs_swap
) {
1868 mmap_prot
|= PROT_WRITE
;
1869 mmap_flags
= MAP_PRIVATE
;
1872 buf
= mmap(NULL
, mmap_size
, mmap_prot
, mmap_flags
, fd
,
1874 if (buf
== MAP_FAILED
) {
1875 pr_err("failed to mmap file\n");
1879 mmaps
[map_idx
] = buf
;
1880 map_idx
= (map_idx
+ 1) & (ARRAY_SIZE(mmaps
) - 1);
1881 file_pos
= file_offset
+ head
;
1882 if (session
->one_mmap
) {
1883 session
->one_mmap_addr
= buf
;
1884 session
->one_mmap_offset
= file_offset
;
1888 event
= fetch_mmaped_event(session
, head
, mmap_size
, buf
);
1890 if (mmaps
[map_idx
]) {
1891 munmap(mmaps
[map_idx
], mmap_size
);
1892 mmaps
[map_idx
] = NULL
;
1895 page_offset
= page_size
* (head
/ page_size
);
1896 file_offset
+= page_offset
;
1897 head
-= page_offset
;
1901 size
= event
->header
.size
;
1903 if (size
< sizeof(struct perf_event_header
) ||
1904 (skip
= perf_session__process_event(session
, event
, file_pos
)) < 0) {
1905 pr_err("%#" PRIx64
" [%#x]: failed to process type: %d\n",
1906 file_offset
+ head
, event
->header
.size
,
1907 event
->header
.type
);
1918 ui_progress__update(&prog
, size
);
1923 if (file_pos
< file_size
)
1927 /* do the final flush for ordered samples */
1928 err
= ordered_events__flush(oe
, OE_FLUSH__FINAL
);
1931 err
= auxtrace__flush_events(session
, tool
);
1934 err
= perf_session__flush_thread_stacks(session
);
1936 ui_progress__finish();
1938 perf_session__warn_about_errors(session
);
1940 * We may switching perf.data output, make ordered_events
1943 ordered_events__reinit(&session
->ordered_events
);
1944 auxtrace__free_events(session
);
1945 session
->one_mmap
= false;
1949 int perf_session__process_events(struct perf_session
*session
)
1951 u64 size
= perf_data__size(session
->data
);
1954 if (perf_session__register_idle_thread(session
) < 0)
1957 if (!perf_data__is_pipe(session
->data
))
1958 err
= __perf_session__process_events(session
,
1959 session
->header
.data_offset
,
1960 session
->header
.data_size
, size
);
1962 err
= __perf_session__process_pipe_events(session
);
1967 bool perf_session__has_traces(struct perf_session
*session
, const char *msg
)
1969 struct perf_evsel
*evsel
;
1971 evlist__for_each_entry(session
->evlist
, evsel
) {
1972 if (evsel
->attr
.type
== PERF_TYPE_TRACEPOINT
)
1976 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg
);
1980 int map__set_kallsyms_ref_reloc_sym(struct map
*map
, const char *symbol_name
, u64 addr
)
1983 struct ref_reloc_sym
*ref
;
1986 ref
= zalloc(sizeof(struct ref_reloc_sym
));
1990 ref
->name
= strdup(symbol_name
);
1991 if (ref
->name
== NULL
) {
1996 bracket
= strchr(ref
->name
, ']');
2002 kmap
= map__kmap(map
);
2004 kmap
->ref_reloc_sym
= ref
;
2009 size_t perf_session__fprintf_dsos(struct perf_session
*session
, FILE *fp
)
2011 return machines__fprintf_dsos(&session
->machines
, fp
);
2014 size_t perf_session__fprintf_dsos_buildid(struct perf_session
*session
, FILE *fp
,
2015 bool (skip
)(struct dso
*dso
, int parm
), int parm
)
2017 return machines__fprintf_dsos_buildid(&session
->machines
, fp
, skip
, parm
);
2020 size_t perf_session__fprintf_nr_events(struct perf_session
*session
, FILE *fp
)
2023 const char *msg
= "";
2025 if (perf_header__has_feat(&session
->header
, HEADER_AUXTRACE
))
2026 msg
= " (excludes AUX area (e.g. instruction trace) decoded / synthesized events)";
2028 ret
= fprintf(fp
, "\nAggregated stats:%s\n", msg
);
2030 ret
+= events_stats__fprintf(&session
->evlist
->stats
, fp
);
2034 size_t perf_session__fprintf(struct perf_session
*session
, FILE *fp
)
2037 * FIXME: Here we have to actually print all the machines in this
2038 * session, not just the host...
2040 return machine__fprintf(&session
->machines
.host
, fp
);
2043 struct perf_evsel
*perf_session__find_first_evtype(struct perf_session
*session
,
2046 struct perf_evsel
*pos
;
2048 evlist__for_each_entry(session
->evlist
, pos
) {
2049 if (pos
->attr
.type
== type
)
2055 int perf_session__cpu_bitmap(struct perf_session
*session
,
2056 const char *cpu_list
, unsigned long *cpu_bitmap
)
2059 struct cpu_map
*map
;
2061 for (i
= 0; i
< PERF_TYPE_MAX
; ++i
) {
2062 struct perf_evsel
*evsel
;
2064 evsel
= perf_session__find_first_evtype(session
, i
);
2068 if (!(evsel
->attr
.sample_type
& PERF_SAMPLE_CPU
)) {
2069 pr_err("File does not contain CPU events. "
2070 "Remove -C option to proceed.\n");
2075 map
= cpu_map__new(cpu_list
);
2077 pr_err("Invalid cpu_list\n");
2081 for (i
= 0; i
< map
->nr
; i
++) {
2082 int cpu
= map
->map
[i
];
2084 if (cpu
>= MAX_NR_CPUS
) {
2085 pr_err("Requested CPU %d too large. "
2086 "Consider raising MAX_NR_CPUS\n", cpu
);
2087 goto out_delete_map
;
2090 set_bit(cpu
, cpu_bitmap
);
2100 void perf_session__fprintf_info(struct perf_session
*session
, FILE *fp
,
2103 if (session
== NULL
|| fp
== NULL
)
2106 fprintf(fp
, "# ========\n");
2107 perf_header__fprintf_info(session
, fp
, full
);
2108 fprintf(fp
, "# ========\n#\n");
2112 int __perf_session__set_tracepoints_handlers(struct perf_session
*session
,
2113 const struct perf_evsel_str_handler
*assocs
,
2116 struct perf_evsel
*evsel
;
2120 for (i
= 0; i
< nr_assocs
; i
++) {
2122 * Adding a handler for an event not in the session,
2125 evsel
= perf_evlist__find_tracepoint_by_name(session
->evlist
, assocs
[i
].name
);
2130 if (evsel
->handler
!= NULL
)
2132 evsel
->handler
= assocs
[i
].handler
;
2140 int perf_event__process_id_index(struct perf_tool
*tool __maybe_unused
,
2141 union perf_event
*event
,
2142 struct perf_session
*session
)
2144 struct perf_evlist
*evlist
= session
->evlist
;
2145 struct id_index_event
*ie
= &event
->id_index
;
2146 size_t i
, nr
, max_nr
;
2148 max_nr
= (ie
->header
.size
- sizeof(struct id_index_event
)) /
2149 sizeof(struct id_index_entry
);
2155 fprintf(stdout
, " nr: %zu\n", nr
);
2157 for (i
= 0; i
< nr
; i
++) {
2158 struct id_index_entry
*e
= &ie
->entries
[i
];
2159 struct perf_sample_id
*sid
;
2162 fprintf(stdout
, " ... id: %"PRIu64
, e
->id
);
2163 fprintf(stdout
, " idx: %"PRIu64
, e
->idx
);
2164 fprintf(stdout
, " cpu: %"PRId64
, e
->cpu
);
2165 fprintf(stdout
, " tid: %"PRId64
"\n", e
->tid
);
2168 sid
= perf_evlist__id2sid(evlist
, e
->id
);
2178 int perf_event__synthesize_id_index(struct perf_tool
*tool
,
2179 perf_event__handler_t process
,
2180 struct perf_evlist
*evlist
,
2181 struct machine
*machine
)
2183 union perf_event
*ev
;
2184 struct perf_evsel
*evsel
;
2185 size_t nr
= 0, i
= 0, sz
, max_nr
, n
;
2188 pr_debug2("Synthesizing id index\n");
2190 max_nr
= (UINT16_MAX
- sizeof(struct id_index_event
)) /
2191 sizeof(struct id_index_entry
);
2193 evlist__for_each_entry(evlist
, evsel
)
2196 n
= nr
> max_nr
? max_nr
: nr
;
2197 sz
= sizeof(struct id_index_event
) + n
* sizeof(struct id_index_entry
);
2202 ev
->id_index
.header
.type
= PERF_RECORD_ID_INDEX
;
2203 ev
->id_index
.header
.size
= sz
;
2204 ev
->id_index
.nr
= n
;
2206 evlist__for_each_entry(evlist
, evsel
) {
2209 for (j
= 0; j
< evsel
->ids
; j
++) {
2210 struct id_index_entry
*e
;
2211 struct perf_sample_id
*sid
;
2214 err
= process(tool
, ev
, NULL
, machine
);
2221 e
= &ev
->id_index
.entries
[i
++];
2223 e
->id
= evsel
->id
[j
];
2225 sid
= perf_evlist__id2sid(evlist
, e
->id
);
2237 sz
= sizeof(struct id_index_event
) + nr
* sizeof(struct id_index_entry
);
2238 ev
->id_index
.header
.size
= sz
;
2239 ev
->id_index
.nr
= nr
;
2241 err
= process(tool
, ev
, NULL
, machine
);