Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / tools / perf / util / session.c
blob507e6cba95458c7de8e0f037b9ef50d9ec4544b2
1 // SPDX-License-Identifier: GPL-2.0
2 #include <errno.h>
3 #include <signal.h>
4 #include <inttypes.h>
5 #include <linux/err.h>
6 #include <linux/kernel.h>
7 #include <linux/zalloc.h>
8 #include <api/fs/fs.h>
10 #include <byteswap.h>
11 #include <unistd.h>
12 #include <sys/types.h>
13 #include <sys/mman.h>
14 #include <perf/cpumap.h>
16 #include "map_symbol.h"
17 #include "branch.h"
18 #include "debug.h"
19 #include "env.h"
20 #include "evlist.h"
21 #include "evsel.h"
22 #include "memswap.h"
23 #include "map.h"
24 #include "symbol.h"
25 #include "session.h"
26 #include "tool.h"
27 #include "perf_regs.h"
28 #include "asm/bug.h"
29 #include "auxtrace.h"
30 #include "thread.h"
31 #include "thread-stack.h"
32 #include "sample-raw.h"
33 #include "stat.h"
34 #include "tsc.h"
35 #include "ui/progress.h"
36 #include "util.h"
37 #include "arch/common.h"
38 #include "units.h"
39 #include "annotate.h"
40 #include <internal/lib.h>
42 static int perf_session__deliver_event(struct perf_session *session,
43 union perf_event *event,
44 const struct perf_tool *tool,
45 u64 file_offset,
46 const char *file_path);
48 static int perf_session__open(struct perf_session *session)
50 struct perf_data *data = session->data;
52 if (perf_session__read_header(session) < 0) {
53 pr_err("incompatible file format (rerun with -v to learn more)\n");
54 return -1;
57 if (perf_header__has_feat(&session->header, HEADER_AUXTRACE)) {
58 /* Auxiliary events may reference exited threads, hold onto dead ones. */
59 symbol_conf.keep_exited_threads = true;
62 if (perf_data__is_pipe(data))
63 return 0;
65 if (perf_header__has_feat(&session->header, HEADER_STAT))
66 return 0;
68 if (!evlist__valid_sample_type(session->evlist)) {
69 pr_err("non matching sample_type\n");
70 return -1;
73 if (!evlist__valid_sample_id_all(session->evlist)) {
74 pr_err("non matching sample_id_all\n");
75 return -1;
78 if (!evlist__valid_read_format(session->evlist)) {
79 pr_err("non matching read_format\n");
80 return -1;
83 return 0;
86 void perf_session__set_id_hdr_size(struct perf_session *session)
88 u16 id_hdr_size = evlist__id_hdr_size(session->evlist);
90 machines__set_id_hdr_size(&session->machines, id_hdr_size);
93 int perf_session__create_kernel_maps(struct perf_session *session)
95 int ret = machine__create_kernel_maps(&session->machines.host);
97 if (ret >= 0)
98 ret = machines__create_guest_kernel_maps(&session->machines);
99 return ret;
102 static void perf_session__destroy_kernel_maps(struct perf_session *session)
104 machines__destroy_kernel_maps(&session->machines);
107 static bool perf_session__has_comm_exec(struct perf_session *session)
109 struct evsel *evsel;
111 evlist__for_each_entry(session->evlist, evsel) {
112 if (evsel->core.attr.comm_exec)
113 return true;
116 return false;
119 static void perf_session__set_comm_exec(struct perf_session *session)
121 bool comm_exec = perf_session__has_comm_exec(session);
123 machines__set_comm_exec(&session->machines, comm_exec);
126 static int ordered_events__deliver_event(struct ordered_events *oe,
127 struct ordered_event *event)
129 struct perf_session *session = container_of(oe, struct perf_session,
130 ordered_events);
132 return perf_session__deliver_event(session, event->event,
133 session->tool, event->file_offset,
134 event->file_path);
137 struct perf_session *__perf_session__new(struct perf_data *data,
138 struct perf_tool *tool,
139 bool trace_event_repipe)
141 int ret = -ENOMEM;
142 struct perf_session *session = zalloc(sizeof(*session));
144 if (!session)
145 goto out;
147 session->trace_event_repipe = trace_event_repipe;
148 session->tool = tool;
149 session->decomp_data.zstd_decomp = &session->zstd_data;
150 session->active_decomp = &session->decomp_data;
151 INIT_LIST_HEAD(&session->auxtrace_index);
152 machines__init(&session->machines);
153 ordered_events__init(&session->ordered_events,
154 ordered_events__deliver_event, NULL);
156 perf_env__init(&session->header.env);
157 if (data) {
158 ret = perf_data__open(data);
159 if (ret < 0)
160 goto out_delete;
162 session->data = data;
164 if (perf_data__is_read(data)) {
165 ret = perf_session__open(session);
166 if (ret < 0)
167 goto out_delete;
170 * set session attributes that are present in perf.data
171 * but not in pipe-mode.
173 if (!data->is_pipe) {
174 perf_session__set_id_hdr_size(session);
175 perf_session__set_comm_exec(session);
178 evlist__init_trace_event_sample_raw(session->evlist);
180 /* Open the directory data. */
181 if (data->is_dir) {
182 ret = perf_data__open_dir(data);
183 if (ret)
184 goto out_delete;
187 if (!symbol_conf.kallsyms_name &&
188 !symbol_conf.vmlinux_name)
189 symbol_conf.kallsyms_name = perf_data__kallsyms_name(data);
191 } else {
192 session->machines.host.env = &perf_env;
195 session->machines.host.single_address_space =
196 perf_env__single_address_space(session->machines.host.env);
198 if (!data || perf_data__is_write(data)) {
200 * In O_RDONLY mode this will be performed when reading the
201 * kernel MMAP event, in perf_event__process_mmap().
203 if (perf_session__create_kernel_maps(session) < 0)
204 pr_warning("Cannot read kernel map\n");
208 * In pipe-mode, evlist is empty until PERF_RECORD_HEADER_ATTR is
209 * processed, so evlist__sample_id_all is not meaningful here.
211 if ((!data || !data->is_pipe) && tool && tool->ordering_requires_timestamps &&
212 tool->ordered_events && !evlist__sample_id_all(session->evlist)) {
213 dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n");
214 tool->ordered_events = false;
217 return session;
219 out_delete:
220 perf_session__delete(session);
221 out:
222 return ERR_PTR(ret);
225 static void perf_decomp__release_events(struct decomp *next)
227 struct decomp *decomp;
228 size_t mmap_len;
230 do {
231 decomp = next;
232 if (decomp == NULL)
233 break;
234 next = decomp->next;
235 mmap_len = decomp->mmap_len;
236 munmap(decomp, mmap_len);
237 } while (1);
240 void perf_session__delete(struct perf_session *session)
242 if (session == NULL)
243 return;
244 auxtrace__free(session);
245 auxtrace_index__free(&session->auxtrace_index);
246 debuginfo_cache__delete();
247 perf_session__destroy_kernel_maps(session);
248 perf_decomp__release_events(session->decomp_data.decomp);
249 perf_env__exit(&session->header.env);
250 machines__exit(&session->machines);
251 if (session->data) {
252 if (perf_data__is_read(session->data))
253 evlist__delete(session->evlist);
254 perf_data__close(session->data);
256 #ifdef HAVE_LIBTRACEEVENT
257 trace_event__cleanup(&session->tevent);
258 #endif
259 free(session);
262 static void swap_sample_id_all(union perf_event *event, void *data)
264 void *end = (void *) event + event->header.size;
265 int size = end - data;
267 BUG_ON(size % sizeof(u64));
268 mem_bswap_64(data, size);
271 static void perf_event__all64_swap(union perf_event *event,
272 bool sample_id_all __maybe_unused)
274 struct perf_event_header *hdr = &event->header;
275 mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr));
278 static void perf_event__comm_swap(union perf_event *event, bool sample_id_all)
280 event->comm.pid = bswap_32(event->comm.pid);
281 event->comm.tid = bswap_32(event->comm.tid);
283 if (sample_id_all) {
284 void *data = &event->comm.comm;
286 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
287 swap_sample_id_all(event, data);
291 static void perf_event__mmap_swap(union perf_event *event,
292 bool sample_id_all)
294 event->mmap.pid = bswap_32(event->mmap.pid);
295 event->mmap.tid = bswap_32(event->mmap.tid);
296 event->mmap.start = bswap_64(event->mmap.start);
297 event->mmap.len = bswap_64(event->mmap.len);
298 event->mmap.pgoff = bswap_64(event->mmap.pgoff);
300 if (sample_id_all) {
301 void *data = &event->mmap.filename;
303 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
304 swap_sample_id_all(event, data);
308 static void perf_event__mmap2_swap(union perf_event *event,
309 bool sample_id_all)
311 event->mmap2.pid = bswap_32(event->mmap2.pid);
312 event->mmap2.tid = bswap_32(event->mmap2.tid);
313 event->mmap2.start = bswap_64(event->mmap2.start);
314 event->mmap2.len = bswap_64(event->mmap2.len);
315 event->mmap2.pgoff = bswap_64(event->mmap2.pgoff);
317 if (!(event->header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID)) {
318 event->mmap2.maj = bswap_32(event->mmap2.maj);
319 event->mmap2.min = bswap_32(event->mmap2.min);
320 event->mmap2.ino = bswap_64(event->mmap2.ino);
321 event->mmap2.ino_generation = bswap_64(event->mmap2.ino_generation);
324 if (sample_id_all) {
325 void *data = &event->mmap2.filename;
327 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
328 swap_sample_id_all(event, data);
331 static void perf_event__task_swap(union perf_event *event, bool sample_id_all)
333 event->fork.pid = bswap_32(event->fork.pid);
334 event->fork.tid = bswap_32(event->fork.tid);
335 event->fork.ppid = bswap_32(event->fork.ppid);
336 event->fork.ptid = bswap_32(event->fork.ptid);
337 event->fork.time = bswap_64(event->fork.time);
339 if (sample_id_all)
340 swap_sample_id_all(event, &event->fork + 1);
343 static void perf_event__read_swap(union perf_event *event, bool sample_id_all)
345 event->read.pid = bswap_32(event->read.pid);
346 event->read.tid = bswap_32(event->read.tid);
347 event->read.value = bswap_64(event->read.value);
348 event->read.time_enabled = bswap_64(event->read.time_enabled);
349 event->read.time_running = bswap_64(event->read.time_running);
350 event->read.id = bswap_64(event->read.id);
352 if (sample_id_all)
353 swap_sample_id_all(event, &event->read + 1);
356 static void perf_event__aux_swap(union perf_event *event, bool sample_id_all)
358 event->aux.aux_offset = bswap_64(event->aux.aux_offset);
359 event->aux.aux_size = bswap_64(event->aux.aux_size);
360 event->aux.flags = bswap_64(event->aux.flags);
362 if (sample_id_all)
363 swap_sample_id_all(event, &event->aux + 1);
366 static void perf_event__itrace_start_swap(union perf_event *event,
367 bool sample_id_all)
369 event->itrace_start.pid = bswap_32(event->itrace_start.pid);
370 event->itrace_start.tid = bswap_32(event->itrace_start.tid);
372 if (sample_id_all)
373 swap_sample_id_all(event, &event->itrace_start + 1);
376 static void perf_event__switch_swap(union perf_event *event, bool sample_id_all)
378 if (event->header.type == PERF_RECORD_SWITCH_CPU_WIDE) {
379 event->context_switch.next_prev_pid =
380 bswap_32(event->context_switch.next_prev_pid);
381 event->context_switch.next_prev_tid =
382 bswap_32(event->context_switch.next_prev_tid);
385 if (sample_id_all)
386 swap_sample_id_all(event, &event->context_switch + 1);
389 static void perf_event__text_poke_swap(union perf_event *event, bool sample_id_all)
391 event->text_poke.addr = bswap_64(event->text_poke.addr);
392 event->text_poke.old_len = bswap_16(event->text_poke.old_len);
393 event->text_poke.new_len = bswap_16(event->text_poke.new_len);
395 if (sample_id_all) {
396 size_t len = sizeof(event->text_poke.old_len) +
397 sizeof(event->text_poke.new_len) +
398 event->text_poke.old_len +
399 event->text_poke.new_len;
400 void *data = &event->text_poke.old_len;
402 data += PERF_ALIGN(len, sizeof(u64));
403 swap_sample_id_all(event, data);
407 static void perf_event__throttle_swap(union perf_event *event,
408 bool sample_id_all)
410 event->throttle.time = bswap_64(event->throttle.time);
411 event->throttle.id = bswap_64(event->throttle.id);
412 event->throttle.stream_id = bswap_64(event->throttle.stream_id);
414 if (sample_id_all)
415 swap_sample_id_all(event, &event->throttle + 1);
418 static void perf_event__namespaces_swap(union perf_event *event,
419 bool sample_id_all)
421 u64 i;
423 event->namespaces.pid = bswap_32(event->namespaces.pid);
424 event->namespaces.tid = bswap_32(event->namespaces.tid);
425 event->namespaces.nr_namespaces = bswap_64(event->namespaces.nr_namespaces);
427 for (i = 0; i < event->namespaces.nr_namespaces; i++) {
428 struct perf_ns_link_info *ns = &event->namespaces.link_info[i];
430 ns->dev = bswap_64(ns->dev);
431 ns->ino = bswap_64(ns->ino);
434 if (sample_id_all)
435 swap_sample_id_all(event, &event->namespaces.link_info[i]);
438 static void perf_event__cgroup_swap(union perf_event *event, bool sample_id_all)
440 event->cgroup.id = bswap_64(event->cgroup.id);
442 if (sample_id_all) {
443 void *data = &event->cgroup.path;
445 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
446 swap_sample_id_all(event, data);
450 static u8 revbyte(u8 b)
452 int rev = (b >> 4) | ((b & 0xf) << 4);
453 rev = ((rev & 0xcc) >> 2) | ((rev & 0x33) << 2);
454 rev = ((rev & 0xaa) >> 1) | ((rev & 0x55) << 1);
455 return (u8) rev;
459 * XXX this is hack in attempt to carry flags bitfield
460 * through endian village. ABI says:
462 * Bit-fields are allocated from right to left (least to most significant)
463 * on little-endian implementations and from left to right (most to least
464 * significant) on big-endian implementations.
466 * The above seems to be byte specific, so we need to reverse each
467 * byte of the bitfield. 'Internet' also says this might be implementation
468 * specific and we probably need proper fix and carry perf_event_attr
469 * bitfield flags in separate data file FEAT_ section. Thought this seems
470 * to work for now.
472 static void swap_bitfield(u8 *p, unsigned len)
474 unsigned i;
476 for (i = 0; i < len; i++) {
477 *p = revbyte(*p);
478 p++;
482 /* exported for swapping attributes in file header */
483 void perf_event__attr_swap(struct perf_event_attr *attr)
485 attr->type = bswap_32(attr->type);
486 attr->size = bswap_32(attr->size);
488 #define bswap_safe(f, n) \
489 (attr->size > (offsetof(struct perf_event_attr, f) + \
490 sizeof(attr->f) * (n)))
491 #define bswap_field(f, sz) \
492 do { \
493 if (bswap_safe(f, 0)) \
494 attr->f = bswap_##sz(attr->f); \
495 } while(0)
496 #define bswap_field_16(f) bswap_field(f, 16)
497 #define bswap_field_32(f) bswap_field(f, 32)
498 #define bswap_field_64(f) bswap_field(f, 64)
500 bswap_field_64(config);
501 bswap_field_64(sample_period);
502 bswap_field_64(sample_type);
503 bswap_field_64(read_format);
504 bswap_field_32(wakeup_events);
505 bswap_field_32(bp_type);
506 bswap_field_64(bp_addr);
507 bswap_field_64(bp_len);
508 bswap_field_64(branch_sample_type);
509 bswap_field_64(sample_regs_user);
510 bswap_field_32(sample_stack_user);
511 bswap_field_32(aux_watermark);
512 bswap_field_16(sample_max_stack);
513 bswap_field_32(aux_sample_size);
516 * After read_format are bitfields. Check read_format because
517 * we are unable to use offsetof on bitfield.
519 if (bswap_safe(read_format, 1))
520 swap_bitfield((u8 *) (&attr->read_format + 1),
521 sizeof(u64));
522 #undef bswap_field_64
523 #undef bswap_field_32
524 #undef bswap_field
525 #undef bswap_safe
528 static void perf_event__hdr_attr_swap(union perf_event *event,
529 bool sample_id_all __maybe_unused)
531 size_t size;
533 perf_event__attr_swap(&event->attr.attr);
535 size = event->header.size;
536 size -= perf_record_header_attr_id(event) - (void *)event;
537 mem_bswap_64(perf_record_header_attr_id(event), size);
540 static void perf_event__event_update_swap(union perf_event *event,
541 bool sample_id_all __maybe_unused)
543 event->event_update.type = bswap_64(event->event_update.type);
544 event->event_update.id = bswap_64(event->event_update.id);
547 static void perf_event__event_type_swap(union perf_event *event,
548 bool sample_id_all __maybe_unused)
550 event->event_type.event_type.event_id =
551 bswap_64(event->event_type.event_type.event_id);
554 static void perf_event__tracing_data_swap(union perf_event *event,
555 bool sample_id_all __maybe_unused)
557 event->tracing_data.size = bswap_32(event->tracing_data.size);
560 static void perf_event__auxtrace_info_swap(union perf_event *event,
561 bool sample_id_all __maybe_unused)
563 size_t size;
565 event->auxtrace_info.type = bswap_32(event->auxtrace_info.type);
567 size = event->header.size;
568 size -= (void *)&event->auxtrace_info.priv - (void *)event;
569 mem_bswap_64(event->auxtrace_info.priv, size);
572 static void perf_event__auxtrace_swap(union perf_event *event,
573 bool sample_id_all __maybe_unused)
575 event->auxtrace.size = bswap_64(event->auxtrace.size);
576 event->auxtrace.offset = bswap_64(event->auxtrace.offset);
577 event->auxtrace.reference = bswap_64(event->auxtrace.reference);
578 event->auxtrace.idx = bswap_32(event->auxtrace.idx);
579 event->auxtrace.tid = bswap_32(event->auxtrace.tid);
580 event->auxtrace.cpu = bswap_32(event->auxtrace.cpu);
583 static void perf_event__auxtrace_error_swap(union perf_event *event,
584 bool sample_id_all __maybe_unused)
586 event->auxtrace_error.type = bswap_32(event->auxtrace_error.type);
587 event->auxtrace_error.code = bswap_32(event->auxtrace_error.code);
588 event->auxtrace_error.cpu = bswap_32(event->auxtrace_error.cpu);
589 event->auxtrace_error.pid = bswap_32(event->auxtrace_error.pid);
590 event->auxtrace_error.tid = bswap_32(event->auxtrace_error.tid);
591 event->auxtrace_error.fmt = bswap_32(event->auxtrace_error.fmt);
592 event->auxtrace_error.ip = bswap_64(event->auxtrace_error.ip);
593 if (event->auxtrace_error.fmt)
594 event->auxtrace_error.time = bswap_64(event->auxtrace_error.time);
595 if (event->auxtrace_error.fmt >= 2) {
596 event->auxtrace_error.machine_pid = bswap_32(event->auxtrace_error.machine_pid);
597 event->auxtrace_error.vcpu = bswap_32(event->auxtrace_error.vcpu);
601 static void perf_event__thread_map_swap(union perf_event *event,
602 bool sample_id_all __maybe_unused)
604 unsigned i;
606 event->thread_map.nr = bswap_64(event->thread_map.nr);
608 for (i = 0; i < event->thread_map.nr; i++)
609 event->thread_map.entries[i].pid = bswap_64(event->thread_map.entries[i].pid);
612 static void perf_event__cpu_map_swap(union perf_event *event,
613 bool sample_id_all __maybe_unused)
615 struct perf_record_cpu_map_data *data = &event->cpu_map.data;
617 data->type = bswap_16(data->type);
619 switch (data->type) {
620 case PERF_CPU_MAP__CPUS:
621 data->cpus_data.nr = bswap_16(data->cpus_data.nr);
623 for (unsigned i = 0; i < data->cpus_data.nr; i++)
624 data->cpus_data.cpu[i] = bswap_16(data->cpus_data.cpu[i]);
625 break;
626 case PERF_CPU_MAP__MASK:
627 data->mask32_data.long_size = bswap_16(data->mask32_data.long_size);
629 switch (data->mask32_data.long_size) {
630 case 4:
631 data->mask32_data.nr = bswap_16(data->mask32_data.nr);
632 for (unsigned i = 0; i < data->mask32_data.nr; i++)
633 data->mask32_data.mask[i] = bswap_32(data->mask32_data.mask[i]);
634 break;
635 case 8:
636 data->mask64_data.nr = bswap_16(data->mask64_data.nr);
637 for (unsigned i = 0; i < data->mask64_data.nr; i++)
638 data->mask64_data.mask[i] = bswap_64(data->mask64_data.mask[i]);
639 break;
640 default:
641 pr_err("cpu_map swap: unsupported long size\n");
643 break;
644 case PERF_CPU_MAP__RANGE_CPUS:
645 data->range_cpu_data.start_cpu = bswap_16(data->range_cpu_data.start_cpu);
646 data->range_cpu_data.end_cpu = bswap_16(data->range_cpu_data.end_cpu);
647 break;
648 default:
649 break;
653 static void perf_event__stat_config_swap(union perf_event *event,
654 bool sample_id_all __maybe_unused)
656 u64 size;
658 size = bswap_64(event->stat_config.nr) * sizeof(event->stat_config.data[0]);
659 size += 1; /* nr item itself */
660 mem_bswap_64(&event->stat_config.nr, size);
663 static void perf_event__stat_swap(union perf_event *event,
664 bool sample_id_all __maybe_unused)
666 event->stat.id = bswap_64(event->stat.id);
667 event->stat.thread = bswap_32(event->stat.thread);
668 event->stat.cpu = bswap_32(event->stat.cpu);
669 event->stat.val = bswap_64(event->stat.val);
670 event->stat.ena = bswap_64(event->stat.ena);
671 event->stat.run = bswap_64(event->stat.run);
674 static void perf_event__stat_round_swap(union perf_event *event,
675 bool sample_id_all __maybe_unused)
677 event->stat_round.type = bswap_64(event->stat_round.type);
678 event->stat_round.time = bswap_64(event->stat_round.time);
681 static void perf_event__time_conv_swap(union perf_event *event,
682 bool sample_id_all __maybe_unused)
684 event->time_conv.time_shift = bswap_64(event->time_conv.time_shift);
685 event->time_conv.time_mult = bswap_64(event->time_conv.time_mult);
686 event->time_conv.time_zero = bswap_64(event->time_conv.time_zero);
688 if (event_contains(event->time_conv, time_cycles)) {
689 event->time_conv.time_cycles = bswap_64(event->time_conv.time_cycles);
690 event->time_conv.time_mask = bswap_64(event->time_conv.time_mask);
694 typedef void (*perf_event__swap_op)(union perf_event *event,
695 bool sample_id_all);
697 static perf_event__swap_op perf_event__swap_ops[] = {
698 [PERF_RECORD_MMAP] = perf_event__mmap_swap,
699 [PERF_RECORD_MMAP2] = perf_event__mmap2_swap,
700 [PERF_RECORD_COMM] = perf_event__comm_swap,
701 [PERF_RECORD_FORK] = perf_event__task_swap,
702 [PERF_RECORD_EXIT] = perf_event__task_swap,
703 [PERF_RECORD_LOST] = perf_event__all64_swap,
704 [PERF_RECORD_READ] = perf_event__read_swap,
705 [PERF_RECORD_THROTTLE] = perf_event__throttle_swap,
706 [PERF_RECORD_UNTHROTTLE] = perf_event__throttle_swap,
707 [PERF_RECORD_SAMPLE] = perf_event__all64_swap,
708 [PERF_RECORD_AUX] = perf_event__aux_swap,
709 [PERF_RECORD_ITRACE_START] = perf_event__itrace_start_swap,
710 [PERF_RECORD_LOST_SAMPLES] = perf_event__all64_swap,
711 [PERF_RECORD_SWITCH] = perf_event__switch_swap,
712 [PERF_RECORD_SWITCH_CPU_WIDE] = perf_event__switch_swap,
713 [PERF_RECORD_NAMESPACES] = perf_event__namespaces_swap,
714 [PERF_RECORD_CGROUP] = perf_event__cgroup_swap,
715 [PERF_RECORD_TEXT_POKE] = perf_event__text_poke_swap,
716 [PERF_RECORD_AUX_OUTPUT_HW_ID] = perf_event__all64_swap,
717 [PERF_RECORD_HEADER_ATTR] = perf_event__hdr_attr_swap,
718 [PERF_RECORD_HEADER_EVENT_TYPE] = perf_event__event_type_swap,
719 [PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap,
720 [PERF_RECORD_HEADER_BUILD_ID] = NULL,
721 [PERF_RECORD_ID_INDEX] = perf_event__all64_swap,
722 [PERF_RECORD_AUXTRACE_INFO] = perf_event__auxtrace_info_swap,
723 [PERF_RECORD_AUXTRACE] = perf_event__auxtrace_swap,
724 [PERF_RECORD_AUXTRACE_ERROR] = perf_event__auxtrace_error_swap,
725 [PERF_RECORD_THREAD_MAP] = perf_event__thread_map_swap,
726 [PERF_RECORD_CPU_MAP] = perf_event__cpu_map_swap,
727 [PERF_RECORD_STAT_CONFIG] = perf_event__stat_config_swap,
728 [PERF_RECORD_STAT] = perf_event__stat_swap,
729 [PERF_RECORD_STAT_ROUND] = perf_event__stat_round_swap,
730 [PERF_RECORD_EVENT_UPDATE] = perf_event__event_update_swap,
731 [PERF_RECORD_TIME_CONV] = perf_event__time_conv_swap,
732 [PERF_RECORD_HEADER_MAX] = NULL,
736 * When perf record finishes a pass on every buffers, it records this pseudo
737 * event.
738 * We record the max timestamp t found in the pass n.
739 * Assuming these timestamps are monotonic across cpus, we know that if
740 * a buffer still has events with timestamps below t, they will be all
741 * available and then read in the pass n + 1.
742 * Hence when we start to read the pass n + 2, we can safely flush every
743 * events with timestamps below t.
745 * ============ PASS n =================
746 * CPU 0 | CPU 1
748 * cnt1 timestamps | cnt2 timestamps
749 * 1 | 2
750 * 2 | 3
751 * - | 4 <--- max recorded
753 * ============ PASS n + 1 ==============
754 * CPU 0 | CPU 1
756 * cnt1 timestamps | cnt2 timestamps
757 * 3 | 5
758 * 4 | 6
759 * 5 | 7 <---- max recorded
761 * Flush every events below timestamp 4
763 * ============ PASS n + 2 ==============
764 * CPU 0 | CPU 1
766 * cnt1 timestamps | cnt2 timestamps
767 * 6 | 8
768 * 7 | 9
769 * - | 10
771 * Flush every events below timestamp 7
772 * etc...
774 int perf_event__process_finished_round(const struct perf_tool *tool __maybe_unused,
775 union perf_event *event __maybe_unused,
776 struct ordered_events *oe)
778 if (dump_trace)
779 fprintf(stdout, "\n");
780 return ordered_events__flush(oe, OE_FLUSH__ROUND);
783 int perf_session__queue_event(struct perf_session *s, union perf_event *event,
784 u64 timestamp, u64 file_offset, const char *file_path)
786 return ordered_events__queue(&s->ordered_events, event, timestamp, file_offset, file_path);
789 static void callchain__lbr_callstack_printf(struct perf_sample *sample)
791 struct ip_callchain *callchain = sample->callchain;
792 struct branch_stack *lbr_stack = sample->branch_stack;
793 struct branch_entry *entries = perf_sample__branch_entries(sample);
794 u64 kernel_callchain_nr = callchain->nr;
795 unsigned int i;
797 for (i = 0; i < kernel_callchain_nr; i++) {
798 if (callchain->ips[i] == PERF_CONTEXT_USER)
799 break;
802 if ((i != kernel_callchain_nr) && lbr_stack->nr) {
803 u64 total_nr;
805 * LBR callstack can only get user call chain,
806 * i is kernel call chain number,
807 * 1 is PERF_CONTEXT_USER.
809 * The user call chain is stored in LBR registers.
810 * LBR are pair registers. The caller is stored
811 * in "from" register, while the callee is stored
812 * in "to" register.
813 * For example, there is a call stack
814 * "A"->"B"->"C"->"D".
815 * The LBR registers will be recorded like
816 * "C"->"D", "B"->"C", "A"->"B".
817 * So only the first "to" register and all "from"
818 * registers are needed to construct the whole stack.
820 total_nr = i + 1 + lbr_stack->nr + 1;
821 kernel_callchain_nr = i + 1;
823 printf("... LBR call chain: nr:%" PRIu64 "\n", total_nr);
825 for (i = 0; i < kernel_callchain_nr; i++)
826 printf("..... %2d: %016" PRIx64 "\n",
827 i, callchain->ips[i]);
829 printf("..... %2d: %016" PRIx64 "\n",
830 (int)(kernel_callchain_nr), entries[0].to);
831 for (i = 0; i < lbr_stack->nr; i++)
832 printf("..... %2d: %016" PRIx64 "\n",
833 (int)(i + kernel_callchain_nr + 1), entries[i].from);
837 static void callchain__printf(struct evsel *evsel,
838 struct perf_sample *sample)
840 unsigned int i;
841 struct ip_callchain *callchain = sample->callchain;
843 if (evsel__has_branch_callstack(evsel))
844 callchain__lbr_callstack_printf(sample);
846 printf("... FP chain: nr:%" PRIu64 "\n", callchain->nr);
848 for (i = 0; i < callchain->nr; i++)
849 printf("..... %2d: %016" PRIx64 "\n",
850 i, callchain->ips[i]);
853 static void branch_stack__printf(struct perf_sample *sample,
854 struct evsel *evsel)
856 struct branch_entry *entries = perf_sample__branch_entries(sample);
857 bool callstack = evsel__has_branch_callstack(evsel);
858 u64 *branch_stack_cntr = sample->branch_stack_cntr;
859 uint64_t i;
861 if (!callstack) {
862 printf("%s: nr:%" PRIu64 "\n", "... branch stack", sample->branch_stack->nr);
863 } else {
864 /* the reason of adding 1 to nr is because after expanding
865 * branch stack it generates nr + 1 callstack records. e.g.,
866 * B()->C()
867 * A()->B()
868 * the final callstack should be:
869 * C()
870 * B()
871 * A()
873 printf("%s: nr:%" PRIu64 "\n", "... branch callstack", sample->branch_stack->nr+1);
876 for (i = 0; i < sample->branch_stack->nr; i++) {
877 struct branch_entry *e = &entries[i];
879 if (!callstack) {
880 printf("..... %2"PRIu64": %016" PRIx64 " -> %016" PRIx64 " %hu cycles %s%s%s%s %x %s %s\n",
881 i, e->from, e->to,
882 (unsigned short)e->flags.cycles,
883 e->flags.mispred ? "M" : " ",
884 e->flags.predicted ? "P" : " ",
885 e->flags.abort ? "A" : " ",
886 e->flags.in_tx ? "T" : " ",
887 (unsigned)e->flags.reserved,
888 get_branch_type(e),
889 e->flags.spec ? branch_spec_desc(e->flags.spec) : "");
890 } else {
891 if (i == 0) {
892 printf("..... %2"PRIu64": %016" PRIx64 "\n"
893 "..... %2"PRIu64": %016" PRIx64 "\n",
894 i, e->to, i+1, e->from);
895 } else {
896 printf("..... %2"PRIu64": %016" PRIx64 "\n", i+1, e->from);
901 if (branch_stack_cntr) {
902 unsigned int br_cntr_width, br_cntr_nr;
904 perf_env__find_br_cntr_info(evsel__env(evsel), &br_cntr_nr, &br_cntr_width);
905 printf("... branch stack counters: nr:%" PRIu64 " (counter width: %u max counter nr:%u)\n",
906 sample->branch_stack->nr, br_cntr_width, br_cntr_nr);
907 for (i = 0; i < sample->branch_stack->nr; i++)
908 printf("..... %2"PRIu64": %016" PRIx64 "\n", i, branch_stack_cntr[i]);
912 static void regs_dump__printf(u64 mask, u64 *regs, const char *arch)
914 unsigned rid, i = 0;
916 for_each_set_bit(rid, (unsigned long *) &mask, sizeof(mask) * 8) {
917 u64 val = regs[i++];
919 printf(".... %-5s 0x%016" PRIx64 "\n",
920 perf_reg_name(rid, arch), val);
924 static const char *regs_abi[] = {
925 [PERF_SAMPLE_REGS_ABI_NONE] = "none",
926 [PERF_SAMPLE_REGS_ABI_32] = "32-bit",
927 [PERF_SAMPLE_REGS_ABI_64] = "64-bit",
930 static inline const char *regs_dump_abi(struct regs_dump *d)
932 if (d->abi > PERF_SAMPLE_REGS_ABI_64)
933 return "unknown";
935 return regs_abi[d->abi];
938 static void regs__printf(const char *type, struct regs_dump *regs, const char *arch)
940 u64 mask = regs->mask;
942 printf("... %s regs: mask 0x%" PRIx64 " ABI %s\n",
943 type,
944 mask,
945 regs_dump_abi(regs));
947 regs_dump__printf(mask, regs->regs, arch);
950 static void regs_user__printf(struct perf_sample *sample, const char *arch)
952 struct regs_dump *user_regs = &sample->user_regs;
954 if (user_regs->regs)
955 regs__printf("user", user_regs, arch);
958 static void regs_intr__printf(struct perf_sample *sample, const char *arch)
960 struct regs_dump *intr_regs = &sample->intr_regs;
962 if (intr_regs->regs)
963 regs__printf("intr", intr_regs, arch);
966 static void stack_user__printf(struct stack_dump *dump)
968 printf("... ustack: size %" PRIu64 ", offset 0x%x\n",
969 dump->size, dump->offset);
972 static void evlist__print_tstamp(struct evlist *evlist, union perf_event *event, struct perf_sample *sample)
974 u64 sample_type = __evlist__combined_sample_type(evlist);
976 if (event->header.type != PERF_RECORD_SAMPLE &&
977 !evlist__sample_id_all(evlist)) {
978 fputs("-1 -1 ", stdout);
979 return;
982 if ((sample_type & PERF_SAMPLE_CPU))
983 printf("%u ", sample->cpu);
985 if (sample_type & PERF_SAMPLE_TIME)
986 printf("%" PRIu64 " ", sample->time);
989 static void sample_read__printf(struct perf_sample *sample, u64 read_format)
991 printf("... sample_read:\n");
993 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
994 printf("...... time enabled %016" PRIx64 "\n",
995 sample->read.time_enabled);
997 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
998 printf("...... time running %016" PRIx64 "\n",
999 sample->read.time_running);
1001 if (read_format & PERF_FORMAT_GROUP) {
1002 struct sample_read_value *value = sample->read.group.values;
1004 printf(".... group nr %" PRIu64 "\n", sample->read.group.nr);
1006 sample_read_group__for_each(value, sample->read.group.nr, read_format) {
1007 printf("..... id %016" PRIx64
1008 ", value %016" PRIx64,
1009 value->id, value->value);
1010 if (read_format & PERF_FORMAT_LOST)
1011 printf(", lost %" PRIu64, value->lost);
1012 printf("\n");
1014 } else {
1015 printf("..... id %016" PRIx64 ", value %016" PRIx64,
1016 sample->read.one.id, sample->read.one.value);
1017 if (read_format & PERF_FORMAT_LOST)
1018 printf(", lost %" PRIu64, sample->read.one.lost);
1019 printf("\n");
1023 static void dump_event(struct evlist *evlist, union perf_event *event,
1024 u64 file_offset, struct perf_sample *sample,
1025 const char *file_path)
1027 if (!dump_trace)
1028 return;
1030 printf("\n%#" PRIx64 "@%s [%#x]: event: %d\n",
1031 file_offset, file_path, event->header.size, event->header.type);
1033 trace_event(event);
1034 if (event->header.type == PERF_RECORD_SAMPLE && evlist->trace_event_sample_raw)
1035 evlist->trace_event_sample_raw(evlist, event, sample);
1037 if (sample)
1038 evlist__print_tstamp(evlist, event, sample);
1040 printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset,
1041 event->header.size, perf_event__name(event->header.type));
1044 char *get_page_size_name(u64 size, char *str)
1046 if (!size || !unit_number__scnprintf(str, PAGE_SIZE_NAME_LEN, size))
1047 snprintf(str, PAGE_SIZE_NAME_LEN, "%s", "N/A");
1049 return str;
1052 static void dump_sample(struct evsel *evsel, union perf_event *event,
1053 struct perf_sample *sample, const char *arch)
1055 u64 sample_type;
1056 char str[PAGE_SIZE_NAME_LEN];
1058 if (!dump_trace)
1059 return;
1061 printf("(IP, 0x%x): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n",
1062 event->header.misc, sample->pid, sample->tid, sample->ip,
1063 sample->period, sample->addr);
1065 sample_type = evsel->core.attr.sample_type;
1067 if (evsel__has_callchain(evsel))
1068 callchain__printf(evsel, sample);
1070 if (evsel__has_br_stack(evsel))
1071 branch_stack__printf(sample, evsel);
1073 if (sample_type & PERF_SAMPLE_REGS_USER)
1074 regs_user__printf(sample, arch);
1076 if (sample_type & PERF_SAMPLE_REGS_INTR)
1077 regs_intr__printf(sample, arch);
1079 if (sample_type & PERF_SAMPLE_STACK_USER)
1080 stack_user__printf(&sample->user_stack);
1082 if (sample_type & PERF_SAMPLE_WEIGHT_TYPE) {
1083 printf("... weight: %" PRIu64 "", sample->weight);
1084 if (sample_type & PERF_SAMPLE_WEIGHT_STRUCT) {
1085 printf(",0x%"PRIx16"", sample->ins_lat);
1086 printf(",0x%"PRIx16"", sample->p_stage_cyc);
1088 printf("\n");
1091 if (sample_type & PERF_SAMPLE_DATA_SRC)
1092 printf(" . data_src: 0x%"PRIx64"\n", sample->data_src);
1094 if (sample_type & PERF_SAMPLE_PHYS_ADDR)
1095 printf(" .. phys_addr: 0x%"PRIx64"\n", sample->phys_addr);
1097 if (sample_type & PERF_SAMPLE_DATA_PAGE_SIZE)
1098 printf(" .. data page size: %s\n", get_page_size_name(sample->data_page_size, str));
1100 if (sample_type & PERF_SAMPLE_CODE_PAGE_SIZE)
1101 printf(" .. code page size: %s\n", get_page_size_name(sample->code_page_size, str));
1103 if (sample_type & PERF_SAMPLE_TRANSACTION)
1104 printf("... transaction: %" PRIx64 "\n", sample->transaction);
1106 if (sample_type & PERF_SAMPLE_READ)
1107 sample_read__printf(sample, evsel->core.attr.read_format);
1110 static void dump_read(struct evsel *evsel, union perf_event *event)
1112 struct perf_record_read *read_event = &event->read;
1113 u64 read_format;
1115 if (!dump_trace)
1116 return;
1118 printf(": %d %d %s %" PRI_lu64 "\n", event->read.pid, event->read.tid,
1119 evsel__name(evsel), event->read.value);
1121 if (!evsel)
1122 return;
1124 read_format = evsel->core.attr.read_format;
1126 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1127 printf("... time enabled : %" PRI_lu64 "\n", read_event->time_enabled);
1129 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1130 printf("... time running : %" PRI_lu64 "\n", read_event->time_running);
1132 if (read_format & PERF_FORMAT_ID)
1133 printf("... id : %" PRI_lu64 "\n", read_event->id);
1135 if (read_format & PERF_FORMAT_LOST)
1136 printf("... lost : %" PRI_lu64 "\n", read_event->lost);
1139 static struct machine *machines__find_for_cpumode(struct machines *machines,
1140 union perf_event *event,
1141 struct perf_sample *sample)
1143 if (perf_guest &&
1144 ((sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL) ||
1145 (sample->cpumode == PERF_RECORD_MISC_GUEST_USER))) {
1146 u32 pid;
1148 if (sample->machine_pid)
1149 pid = sample->machine_pid;
1150 else if (event->header.type == PERF_RECORD_MMAP
1151 || event->header.type == PERF_RECORD_MMAP2)
1152 pid = event->mmap.pid;
1153 else
1154 pid = sample->pid;
1157 * Guest code machine is created as needed and does not use
1158 * DEFAULT_GUEST_KERNEL_ID.
1160 if (symbol_conf.guest_code)
1161 return machines__findnew(machines, pid);
1163 return machines__find_guest(machines, pid);
1166 return &machines->host;
1169 static int deliver_sample_value(struct evlist *evlist,
1170 const struct perf_tool *tool,
1171 union perf_event *event,
1172 struct perf_sample *sample,
1173 struct sample_read_value *v,
1174 struct machine *machine,
1175 bool per_thread)
1177 struct perf_sample_id *sid = evlist__id2sid(evlist, v->id);
1178 struct evsel *evsel;
1179 u64 *storage = NULL;
1181 if (sid) {
1182 storage = perf_sample_id__get_period_storage(sid, sample->tid, per_thread);
1185 if (storage) {
1186 sample->id = v->id;
1187 sample->period = v->value - *storage;
1188 *storage = v->value;
1191 if (!storage || sid->evsel == NULL) {
1192 ++evlist->stats.nr_unknown_id;
1193 return 0;
1197 * There's no reason to deliver sample
1198 * for zero period, bail out.
1200 if (!sample->period)
1201 return 0;
1203 evsel = container_of(sid->evsel, struct evsel, core);
1204 return tool->sample(tool, event, sample, evsel, machine);
1207 static int deliver_sample_group(struct evlist *evlist,
1208 const struct perf_tool *tool,
1209 union perf_event *event,
1210 struct perf_sample *sample,
1211 struct machine *machine,
1212 u64 read_format,
1213 bool per_thread)
1215 int ret = -EINVAL;
1216 struct sample_read_value *v = sample->read.group.values;
1218 if (tool->dont_split_sample_group)
1219 return deliver_sample_value(evlist, tool, event, sample, v, machine,
1220 per_thread);
1222 sample_read_group__for_each(v, sample->read.group.nr, read_format) {
1223 ret = deliver_sample_value(evlist, tool, event, sample, v,
1224 machine, per_thread);
1225 if (ret)
1226 break;
1229 return ret;
1232 static int evlist__deliver_sample(struct evlist *evlist, const struct perf_tool *tool,
1233 union perf_event *event, struct perf_sample *sample,
1234 struct evsel *evsel, struct machine *machine)
1236 /* We know evsel != NULL. */
1237 u64 sample_type = evsel->core.attr.sample_type;
1238 u64 read_format = evsel->core.attr.read_format;
1239 bool per_thread = perf_evsel__attr_has_per_thread_sample_period(&evsel->core);
1241 /* Standard sample delivery. */
1242 if (!(sample_type & PERF_SAMPLE_READ))
1243 return tool->sample(tool, event, sample, evsel, machine);
1245 /* For PERF_SAMPLE_READ we have either single or group mode. */
1246 if (read_format & PERF_FORMAT_GROUP)
1247 return deliver_sample_group(evlist, tool, event, sample,
1248 machine, read_format, per_thread);
1249 else
1250 return deliver_sample_value(evlist, tool, event, sample,
1251 &sample->read.one, machine,
1252 per_thread);
1255 static int machines__deliver_event(struct machines *machines,
1256 struct evlist *evlist,
1257 union perf_event *event,
1258 struct perf_sample *sample,
1259 const struct perf_tool *tool, u64 file_offset,
1260 const char *file_path)
1262 struct evsel *evsel;
1263 struct machine *machine;
1265 dump_event(evlist, event, file_offset, sample, file_path);
1267 evsel = evlist__id2evsel(evlist, sample->id);
1269 machine = machines__find_for_cpumode(machines, event, sample);
1271 switch (event->header.type) {
1272 case PERF_RECORD_SAMPLE:
1273 if (evsel == NULL) {
1274 ++evlist->stats.nr_unknown_id;
1275 return 0;
1277 if (machine == NULL) {
1278 ++evlist->stats.nr_unprocessable_samples;
1279 dump_sample(evsel, event, sample, perf_env__arch(NULL));
1280 return 0;
1282 dump_sample(evsel, event, sample, perf_env__arch(machine->env));
1283 return evlist__deliver_sample(evlist, tool, event, sample, evsel, machine);
1284 case PERF_RECORD_MMAP:
1285 return tool->mmap(tool, event, sample, machine);
1286 case PERF_RECORD_MMAP2:
1287 if (event->header.misc & PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT)
1288 ++evlist->stats.nr_proc_map_timeout;
1289 return tool->mmap2(tool, event, sample, machine);
1290 case PERF_RECORD_COMM:
1291 return tool->comm(tool, event, sample, machine);
1292 case PERF_RECORD_NAMESPACES:
1293 return tool->namespaces(tool, event, sample, machine);
1294 case PERF_RECORD_CGROUP:
1295 return tool->cgroup(tool, event, sample, machine);
1296 case PERF_RECORD_FORK:
1297 return tool->fork(tool, event, sample, machine);
1298 case PERF_RECORD_EXIT:
1299 return tool->exit(tool, event, sample, machine);
1300 case PERF_RECORD_LOST:
1301 if (tool->lost == perf_event__process_lost)
1302 evlist->stats.total_lost += event->lost.lost;
1303 return tool->lost(tool, event, sample, machine);
1304 case PERF_RECORD_LOST_SAMPLES:
1305 if (event->header.misc & PERF_RECORD_MISC_LOST_SAMPLES_BPF)
1306 evlist->stats.total_dropped_samples += event->lost_samples.lost;
1307 else if (tool->lost_samples == perf_event__process_lost_samples)
1308 evlist->stats.total_lost_samples += event->lost_samples.lost;
1309 return tool->lost_samples(tool, event, sample, machine);
1310 case PERF_RECORD_READ:
1311 dump_read(evsel, event);
1312 return tool->read(tool, event, sample, evsel, machine);
1313 case PERF_RECORD_THROTTLE:
1314 return tool->throttle(tool, event, sample, machine);
1315 case PERF_RECORD_UNTHROTTLE:
1316 return tool->unthrottle(tool, event, sample, machine);
1317 case PERF_RECORD_AUX:
1318 if (tool->aux == perf_event__process_aux) {
1319 if (event->aux.flags & PERF_AUX_FLAG_TRUNCATED)
1320 evlist->stats.total_aux_lost += 1;
1321 if (event->aux.flags & PERF_AUX_FLAG_PARTIAL)
1322 evlist->stats.total_aux_partial += 1;
1323 if (event->aux.flags & PERF_AUX_FLAG_COLLISION)
1324 evlist->stats.total_aux_collision += 1;
1326 return tool->aux(tool, event, sample, machine);
1327 case PERF_RECORD_ITRACE_START:
1328 return tool->itrace_start(tool, event, sample, machine);
1329 case PERF_RECORD_SWITCH:
1330 case PERF_RECORD_SWITCH_CPU_WIDE:
1331 return tool->context_switch(tool, event, sample, machine);
1332 case PERF_RECORD_KSYMBOL:
1333 return tool->ksymbol(tool, event, sample, machine);
1334 case PERF_RECORD_BPF_EVENT:
1335 return tool->bpf(tool, event, sample, machine);
1336 case PERF_RECORD_TEXT_POKE:
1337 return tool->text_poke(tool, event, sample, machine);
1338 case PERF_RECORD_AUX_OUTPUT_HW_ID:
1339 return tool->aux_output_hw_id(tool, event, sample, machine);
1340 default:
1341 ++evlist->stats.nr_unknown_events;
1342 return -1;
1346 static int perf_session__deliver_event(struct perf_session *session,
1347 union perf_event *event,
1348 const struct perf_tool *tool,
1349 u64 file_offset,
1350 const char *file_path)
1352 struct perf_sample sample;
1353 int ret = evlist__parse_sample(session->evlist, event, &sample);
1355 if (ret) {
1356 pr_err("Can't parse sample, err = %d\n", ret);
1357 return ret;
1360 ret = auxtrace__process_event(session, event, &sample, tool);
1361 if (ret < 0)
1362 return ret;
1363 if (ret > 0)
1364 return 0;
1366 ret = machines__deliver_event(&session->machines, session->evlist,
1367 event, &sample, tool, file_offset, file_path);
1369 if (dump_trace && sample.aux_sample.size)
1370 auxtrace__dump_auxtrace_sample(session, &sample);
1372 return ret;
1375 static s64 perf_session__process_user_event(struct perf_session *session,
1376 union perf_event *event,
1377 u64 file_offset,
1378 const char *file_path)
1380 struct ordered_events *oe = &session->ordered_events;
1381 const struct perf_tool *tool = session->tool;
1382 struct perf_sample sample = { .time = 0, };
1383 int fd = perf_data__fd(session->data);
1384 int err;
1386 if (event->header.type != PERF_RECORD_COMPRESSED || perf_tool__compressed_is_stub(tool))
1387 dump_event(session->evlist, event, file_offset, &sample, file_path);
1389 /* These events are processed right away */
1390 switch (event->header.type) {
1391 case PERF_RECORD_HEADER_ATTR:
1392 err = tool->attr(tool, event, &session->evlist);
1393 if (err == 0) {
1394 perf_session__set_id_hdr_size(session);
1395 perf_session__set_comm_exec(session);
1397 return err;
1398 case PERF_RECORD_EVENT_UPDATE:
1399 return tool->event_update(tool, event, &session->evlist);
1400 case PERF_RECORD_HEADER_EVENT_TYPE:
1402 * Deprecated, but we need to handle it for sake
1403 * of old data files create in pipe mode.
1405 return 0;
1406 case PERF_RECORD_HEADER_TRACING_DATA:
1408 * Setup for reading amidst mmap, but only when we
1409 * are in 'file' mode. The 'pipe' fd is in proper
1410 * place already.
1412 if (!perf_data__is_pipe(session->data))
1413 lseek(fd, file_offset, SEEK_SET);
1414 return tool->tracing_data(session, event);
1415 case PERF_RECORD_HEADER_BUILD_ID:
1416 return tool->build_id(session, event);
1417 case PERF_RECORD_FINISHED_ROUND:
1418 return tool->finished_round(tool, event, oe);
1419 case PERF_RECORD_ID_INDEX:
1420 return tool->id_index(session, event);
1421 case PERF_RECORD_AUXTRACE_INFO:
1422 return tool->auxtrace_info(session, event);
1423 case PERF_RECORD_AUXTRACE:
1425 * Setup for reading amidst mmap, but only when we
1426 * are in 'file' mode. The 'pipe' fd is in proper
1427 * place already.
1429 if (!perf_data__is_pipe(session->data))
1430 lseek(fd, file_offset + event->header.size, SEEK_SET);
1431 return tool->auxtrace(session, event);
1432 case PERF_RECORD_AUXTRACE_ERROR:
1433 perf_session__auxtrace_error_inc(session, event);
1434 return tool->auxtrace_error(session, event);
1435 case PERF_RECORD_THREAD_MAP:
1436 return tool->thread_map(session, event);
1437 case PERF_RECORD_CPU_MAP:
1438 return tool->cpu_map(session, event);
1439 case PERF_RECORD_STAT_CONFIG:
1440 return tool->stat_config(session, event);
1441 case PERF_RECORD_STAT:
1442 return tool->stat(session, event);
1443 case PERF_RECORD_STAT_ROUND:
1444 return tool->stat_round(session, event);
1445 case PERF_RECORD_TIME_CONV:
1446 session->time_conv = event->time_conv;
1447 return tool->time_conv(session, event);
1448 case PERF_RECORD_HEADER_FEATURE:
1449 return tool->feature(session, event);
1450 case PERF_RECORD_COMPRESSED:
1451 err = tool->compressed(session, event, file_offset, file_path);
1452 if (err)
1453 dump_event(session->evlist, event, file_offset, &sample, file_path);
1454 return err;
1455 case PERF_RECORD_FINISHED_INIT:
1456 return tool->finished_init(session, event);
1457 default:
1458 return -EINVAL;
1462 int perf_session__deliver_synth_event(struct perf_session *session,
1463 union perf_event *event,
1464 struct perf_sample *sample)
1466 struct evlist *evlist = session->evlist;
1467 const struct perf_tool *tool = session->tool;
1469 events_stats__inc(&evlist->stats, event->header.type);
1471 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
1472 return perf_session__process_user_event(session, event, 0, NULL);
1474 return machines__deliver_event(&session->machines, evlist, event, sample, tool, 0, NULL);
1477 int perf_session__deliver_synth_attr_event(struct perf_session *session,
1478 const struct perf_event_attr *attr,
1479 u64 id)
1481 union {
1482 struct {
1483 struct perf_record_header_attr attr;
1484 u64 ids[1];
1485 } attr_id;
1486 union perf_event ev;
1487 } ev = {
1488 .attr_id.attr.header.type = PERF_RECORD_HEADER_ATTR,
1489 .attr_id.attr.header.size = sizeof(ev.attr_id),
1490 .attr_id.ids[0] = id,
1493 if (attr->size != sizeof(ev.attr_id.attr.attr)) {
1494 pr_debug("Unexpected perf_event_attr size\n");
1495 return -EINVAL;
1497 ev.attr_id.attr.attr = *attr;
1498 return perf_session__deliver_synth_event(session, &ev.ev, NULL);
1501 static void event_swap(union perf_event *event, bool sample_id_all)
1503 perf_event__swap_op swap;
1505 swap = perf_event__swap_ops[event->header.type];
1506 if (swap)
1507 swap(event, sample_id_all);
1510 int perf_session__peek_event(struct perf_session *session, off_t file_offset,
1511 void *buf, size_t buf_sz,
1512 union perf_event **event_ptr,
1513 struct perf_sample *sample)
1515 union perf_event *event;
1516 size_t hdr_sz, rest;
1517 int fd;
1519 if (session->one_mmap && !session->header.needs_swap) {
1520 event = file_offset - session->one_mmap_offset +
1521 session->one_mmap_addr;
1522 goto out_parse_sample;
1525 if (perf_data__is_pipe(session->data))
1526 return -1;
1528 fd = perf_data__fd(session->data);
1529 hdr_sz = sizeof(struct perf_event_header);
1531 if (buf_sz < hdr_sz)
1532 return -1;
1534 if (lseek(fd, file_offset, SEEK_SET) == (off_t)-1 ||
1535 readn(fd, buf, hdr_sz) != (ssize_t)hdr_sz)
1536 return -1;
1538 event = (union perf_event *)buf;
1540 if (session->header.needs_swap)
1541 perf_event_header__bswap(&event->header);
1543 if (event->header.size < hdr_sz || event->header.size > buf_sz)
1544 return -1;
1546 buf += hdr_sz;
1547 rest = event->header.size - hdr_sz;
1549 if (readn(fd, buf, rest) != (ssize_t)rest)
1550 return -1;
1552 if (session->header.needs_swap)
1553 event_swap(event, evlist__sample_id_all(session->evlist));
1555 out_parse_sample:
1557 if (sample && event->header.type < PERF_RECORD_USER_TYPE_START &&
1558 evlist__parse_sample(session->evlist, event, sample))
1559 return -1;
1561 *event_ptr = event;
1563 return 0;
1566 int perf_session__peek_events(struct perf_session *session, u64 offset,
1567 u64 size, peek_events_cb_t cb, void *data)
1569 u64 max_offset = offset + size;
1570 char buf[PERF_SAMPLE_MAX_SIZE];
1571 union perf_event *event;
1572 int err;
1574 do {
1575 err = perf_session__peek_event(session, offset, buf,
1576 PERF_SAMPLE_MAX_SIZE, &event,
1577 NULL);
1578 if (err)
1579 return err;
1581 err = cb(session, event, offset, data);
1582 if (err)
1583 return err;
1585 offset += event->header.size;
1586 if (event->header.type == PERF_RECORD_AUXTRACE)
1587 offset += event->auxtrace.size;
1589 } while (offset < max_offset);
1591 return err;
1594 static s64 perf_session__process_event(struct perf_session *session,
1595 union perf_event *event, u64 file_offset,
1596 const char *file_path)
1598 struct evlist *evlist = session->evlist;
1599 const struct perf_tool *tool = session->tool;
1600 int ret;
1602 if (session->header.needs_swap)
1603 event_swap(event, evlist__sample_id_all(evlist));
1605 if (event->header.type >= PERF_RECORD_HEADER_MAX)
1606 return -EINVAL;
1608 events_stats__inc(&evlist->stats, event->header.type);
1610 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
1611 return perf_session__process_user_event(session, event, file_offset, file_path);
1613 if (tool->ordered_events) {
1614 u64 timestamp = -1ULL;
1616 ret = evlist__parse_sample_timestamp(evlist, event, &timestamp);
1617 if (ret && ret != -1)
1618 return ret;
1620 ret = perf_session__queue_event(session, event, timestamp, file_offset, file_path);
1621 if (ret != -ETIME)
1622 return ret;
1625 return perf_session__deliver_event(session, event, tool, file_offset, file_path);
1628 void perf_event_header__bswap(struct perf_event_header *hdr)
1630 hdr->type = bswap_32(hdr->type);
1631 hdr->misc = bswap_16(hdr->misc);
1632 hdr->size = bswap_16(hdr->size);
1635 struct thread *perf_session__findnew(struct perf_session *session, pid_t pid)
1637 return machine__findnew_thread(&session->machines.host, -1, pid);
1640 int perf_session__register_idle_thread(struct perf_session *session)
1642 struct thread *thread = machine__idle_thread(&session->machines.host);
1644 /* machine__idle_thread() got the thread, so put it */
1645 thread__put(thread);
1646 return thread ? 0 : -1;
1649 static void
1650 perf_session__warn_order(const struct perf_session *session)
1652 const struct ordered_events *oe = &session->ordered_events;
1653 struct evsel *evsel;
1654 bool should_warn = true;
1656 evlist__for_each_entry(session->evlist, evsel) {
1657 if (evsel->core.attr.write_backward)
1658 should_warn = false;
1661 if (!should_warn)
1662 return;
1663 if (oe->nr_unordered_events != 0)
1664 ui__warning("%u out of order events recorded.\n", oe->nr_unordered_events);
1667 static void perf_session__warn_about_errors(const struct perf_session *session)
1669 const struct events_stats *stats = &session->evlist->stats;
1671 if (session->tool->lost == perf_event__process_lost &&
1672 stats->nr_events[PERF_RECORD_LOST] != 0) {
1673 ui__warning("Processed %d events and lost %d chunks!\n\n"
1674 "Check IO/CPU overload!\n\n",
1675 stats->nr_events[0],
1676 stats->nr_events[PERF_RECORD_LOST]);
1679 if (session->tool->lost_samples == perf_event__process_lost_samples) {
1680 double drop_rate;
1682 drop_rate = (double)stats->total_lost_samples /
1683 (double) (stats->nr_events[PERF_RECORD_SAMPLE] + stats->total_lost_samples);
1684 if (drop_rate > 0.05) {
1685 ui__warning("Processed %" PRIu64 " samples and lost %3.2f%%!\n\n",
1686 stats->nr_events[PERF_RECORD_SAMPLE] + stats->total_lost_samples,
1687 drop_rate * 100.0);
1691 if (session->tool->aux == perf_event__process_aux &&
1692 stats->total_aux_lost != 0) {
1693 ui__warning("AUX data lost %" PRIu64 " times out of %u!\n\n",
1694 stats->total_aux_lost,
1695 stats->nr_events[PERF_RECORD_AUX]);
1698 if (session->tool->aux == perf_event__process_aux &&
1699 stats->total_aux_partial != 0) {
1700 bool vmm_exclusive = false;
1702 (void)sysfs__read_bool("module/kvm_intel/parameters/vmm_exclusive",
1703 &vmm_exclusive);
1705 ui__warning("AUX data had gaps in it %" PRIu64 " times out of %u!\n\n"
1706 "Are you running a KVM guest in the background?%s\n\n",
1707 stats->total_aux_partial,
1708 stats->nr_events[PERF_RECORD_AUX],
1709 vmm_exclusive ?
1710 "\nReloading kvm_intel module with vmm_exclusive=0\n"
1711 "will reduce the gaps to only guest's timeslices." :
1712 "");
1715 if (session->tool->aux == perf_event__process_aux &&
1716 stats->total_aux_collision != 0) {
1717 ui__warning("AUX data detected collision %" PRIu64 " times out of %u!\n\n",
1718 stats->total_aux_collision,
1719 stats->nr_events[PERF_RECORD_AUX]);
1722 if (stats->nr_unknown_events != 0) {
1723 ui__warning("Found %u unknown events!\n\n"
1724 "Is this an older tool processing a perf.data "
1725 "file generated by a more recent tool?\n\n"
1726 "If that is not the case, consider "
1727 "reporting to linux-kernel@vger.kernel.org.\n\n",
1728 stats->nr_unknown_events);
1731 if (stats->nr_unknown_id != 0) {
1732 ui__warning("%u samples with id not present in the header\n",
1733 stats->nr_unknown_id);
1736 if (stats->nr_invalid_chains != 0) {
1737 ui__warning("Found invalid callchains!\n\n"
1738 "%u out of %u events were discarded for this reason.\n\n"
1739 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
1740 stats->nr_invalid_chains,
1741 stats->nr_events[PERF_RECORD_SAMPLE]);
1744 if (stats->nr_unprocessable_samples != 0) {
1745 ui__warning("%u unprocessable samples recorded.\n"
1746 "Do you have a KVM guest running and not using 'perf kvm'?\n",
1747 stats->nr_unprocessable_samples);
1750 perf_session__warn_order(session);
1752 events_stats__auxtrace_error_warn(stats);
1754 if (stats->nr_proc_map_timeout != 0) {
1755 ui__warning("%d map information files for pre-existing threads were\n"
1756 "not processed, if there are samples for addresses they\n"
1757 "will not be resolved, you may find out which are these\n"
1758 "threads by running with -v and redirecting the output\n"
1759 "to a file.\n"
1760 "The time limit to process proc map is too short?\n"
1761 "Increase it by --proc-map-timeout\n",
1762 stats->nr_proc_map_timeout);
1766 static int perf_session__flush_thread_stack(struct thread *thread,
1767 void *p __maybe_unused)
1769 return thread_stack__flush(thread);
1772 static int perf_session__flush_thread_stacks(struct perf_session *session)
1774 return machines__for_each_thread(&session->machines,
1775 perf_session__flush_thread_stack,
1776 NULL);
1779 volatile sig_atomic_t session_done;
1781 static int __perf_session__process_decomp_events(struct perf_session *session);
1783 static int __perf_session__process_pipe_events(struct perf_session *session)
1785 struct ordered_events *oe = &session->ordered_events;
1786 const struct perf_tool *tool = session->tool;
1787 struct ui_progress prog;
1788 union perf_event *event;
1789 uint32_t size, cur_size = 0;
1790 void *buf = NULL;
1791 s64 skip = 0;
1792 u64 head;
1793 ssize_t err;
1794 void *p;
1795 bool update_prog = false;
1798 * If it's from a file saving pipe data (by redirection), it would have
1799 * a file name other than "-". Then we can get the total size and show
1800 * the progress.
1802 if (strcmp(session->data->path, "-") && session->data->file.size) {
1803 ui_progress__init_size(&prog, session->data->file.size,
1804 "Processing events...");
1805 update_prog = true;
1808 head = 0;
1809 cur_size = sizeof(union perf_event);
1811 buf = malloc(cur_size);
1812 if (!buf)
1813 return -errno;
1814 ordered_events__set_copy_on_queue(oe, true);
1815 more:
1816 event = buf;
1817 err = perf_data__read(session->data, event,
1818 sizeof(struct perf_event_header));
1819 if (err <= 0) {
1820 if (err == 0)
1821 goto done;
1823 pr_err("failed to read event header\n");
1824 goto out_err;
1827 if (session->header.needs_swap)
1828 perf_event_header__bswap(&event->header);
1830 size = event->header.size;
1831 if (size < sizeof(struct perf_event_header)) {
1832 pr_err("bad event header size\n");
1833 goto out_err;
1836 if (size > cur_size) {
1837 void *new = realloc(buf, size);
1838 if (!new) {
1839 pr_err("failed to allocate memory to read event\n");
1840 goto out_err;
1842 buf = new;
1843 cur_size = size;
1844 event = buf;
1846 p = event;
1847 p += sizeof(struct perf_event_header);
1849 if (size - sizeof(struct perf_event_header)) {
1850 err = perf_data__read(session->data, p,
1851 size - sizeof(struct perf_event_header));
1852 if (err <= 0) {
1853 if (err == 0) {
1854 pr_err("unexpected end of event stream\n");
1855 goto done;
1858 pr_err("failed to read event data\n");
1859 goto out_err;
1863 if ((skip = perf_session__process_event(session, event, head, "pipe")) < 0) {
1864 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
1865 head, event->header.size, event->header.type);
1866 err = -EINVAL;
1867 goto out_err;
1870 head += size;
1872 if (skip > 0)
1873 head += skip;
1875 err = __perf_session__process_decomp_events(session);
1876 if (err)
1877 goto out_err;
1879 if (update_prog)
1880 ui_progress__update(&prog, size);
1882 if (!session_done())
1883 goto more;
1884 done:
1885 /* do the final flush for ordered samples */
1886 err = ordered_events__flush(oe, OE_FLUSH__FINAL);
1887 if (err)
1888 goto out_err;
1889 err = auxtrace__flush_events(session, tool);
1890 if (err)
1891 goto out_err;
1892 err = perf_session__flush_thread_stacks(session);
1893 out_err:
1894 free(buf);
1895 if (update_prog)
1896 ui_progress__finish();
1897 if (!tool->no_warn)
1898 perf_session__warn_about_errors(session);
1899 ordered_events__free(&session->ordered_events);
1900 auxtrace__free_events(session);
1901 return err;
1904 static union perf_event *
1905 prefetch_event(char *buf, u64 head, size_t mmap_size,
1906 bool needs_swap, union perf_event *error)
1908 union perf_event *event;
1909 u16 event_size;
1912 * Ensure we have enough space remaining to read
1913 * the size of the event in the headers.
1915 if (head + sizeof(event->header) > mmap_size)
1916 return NULL;
1918 event = (union perf_event *)(buf + head);
1919 if (needs_swap)
1920 perf_event_header__bswap(&event->header);
1922 event_size = event->header.size;
1923 if (head + event_size <= mmap_size)
1924 return event;
1926 /* We're not fetching the event so swap back again */
1927 if (needs_swap)
1928 perf_event_header__bswap(&event->header);
1930 /* Check if the event fits into the next mmapped buf. */
1931 if (event_size <= mmap_size - head % page_size) {
1932 /* Remap buf and fetch again. */
1933 return NULL;
1936 /* Invalid input. Event size should never exceed mmap_size. */
1937 pr_debug("%s: head=%#" PRIx64 " event->header.size=%#x, mmap_size=%#zx:"
1938 " fuzzed or compressed perf.data?\n", __func__, head, event_size, mmap_size);
1940 return error;
1943 static union perf_event *
1944 fetch_mmaped_event(u64 head, size_t mmap_size, char *buf, bool needs_swap)
1946 return prefetch_event(buf, head, mmap_size, needs_swap, ERR_PTR(-EINVAL));
1949 static union perf_event *
1950 fetch_decomp_event(u64 head, size_t mmap_size, char *buf, bool needs_swap)
1952 return prefetch_event(buf, head, mmap_size, needs_swap, NULL);
1955 static int __perf_session__process_decomp_events(struct perf_session *session)
1957 s64 skip;
1958 u64 size;
1959 struct decomp *decomp = session->active_decomp->decomp_last;
1961 if (!decomp)
1962 return 0;
1964 while (decomp->head < decomp->size && !session_done()) {
1965 union perf_event *event = fetch_decomp_event(decomp->head, decomp->size, decomp->data,
1966 session->header.needs_swap);
1968 if (!event)
1969 break;
1971 size = event->header.size;
1973 if (size < sizeof(struct perf_event_header) ||
1974 (skip = perf_session__process_event(session, event, decomp->file_pos,
1975 decomp->file_path)) < 0) {
1976 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
1977 decomp->file_pos + decomp->head, event->header.size, event->header.type);
1978 return -EINVAL;
1981 if (skip)
1982 size += skip;
1984 decomp->head += size;
1987 return 0;
1991 * On 64bit we can mmap the data file in one go. No need for tiny mmap
1992 * slices. On 32bit we use 32MB.
1994 #if BITS_PER_LONG == 64
1995 #define MMAP_SIZE ULLONG_MAX
1996 #define NUM_MMAPS 1
1997 #else
1998 #define MMAP_SIZE (32 * 1024 * 1024ULL)
1999 #define NUM_MMAPS 128
2000 #endif
2002 struct reader;
2004 typedef s64 (*reader_cb_t)(struct perf_session *session,
2005 union perf_event *event,
2006 u64 file_offset,
2007 const char *file_path);
2009 struct reader {
2010 int fd;
2011 const char *path;
2012 u64 data_size;
2013 u64 data_offset;
2014 reader_cb_t process;
2015 bool in_place_update;
2016 char *mmaps[NUM_MMAPS];
2017 size_t mmap_size;
2018 int mmap_idx;
2019 char *mmap_cur;
2020 u64 file_pos;
2021 u64 file_offset;
2022 u64 head;
2023 u64 size;
2024 bool done;
2025 struct zstd_data zstd_data;
2026 struct decomp_data decomp_data;
2029 static int
2030 reader__init(struct reader *rd, bool *one_mmap)
2032 u64 data_size = rd->data_size;
2033 char **mmaps = rd->mmaps;
2035 rd->head = rd->data_offset;
2036 data_size += rd->data_offset;
2038 rd->mmap_size = MMAP_SIZE;
2039 if (rd->mmap_size > data_size) {
2040 rd->mmap_size = data_size;
2041 if (one_mmap)
2042 *one_mmap = true;
2045 memset(mmaps, 0, sizeof(rd->mmaps));
2047 if (zstd_init(&rd->zstd_data, 0))
2048 return -1;
2049 rd->decomp_data.zstd_decomp = &rd->zstd_data;
2051 return 0;
2054 static void
2055 reader__release_decomp(struct reader *rd)
2057 perf_decomp__release_events(rd->decomp_data.decomp);
2058 zstd_fini(&rd->zstd_data);
2061 static int
2062 reader__mmap(struct reader *rd, struct perf_session *session)
2064 int mmap_prot, mmap_flags;
2065 char *buf, **mmaps = rd->mmaps;
2066 u64 page_offset;
2068 mmap_prot = PROT_READ;
2069 mmap_flags = MAP_SHARED;
2071 if (rd->in_place_update) {
2072 mmap_prot |= PROT_WRITE;
2073 } else if (session->header.needs_swap) {
2074 mmap_prot |= PROT_WRITE;
2075 mmap_flags = MAP_PRIVATE;
2078 if (mmaps[rd->mmap_idx]) {
2079 munmap(mmaps[rd->mmap_idx], rd->mmap_size);
2080 mmaps[rd->mmap_idx] = NULL;
2083 page_offset = page_size * (rd->head / page_size);
2084 rd->file_offset += page_offset;
2085 rd->head -= page_offset;
2087 buf = mmap(NULL, rd->mmap_size, mmap_prot, mmap_flags, rd->fd,
2088 rd->file_offset);
2089 if (buf == MAP_FAILED) {
2090 pr_err("failed to mmap file\n");
2091 return -errno;
2093 mmaps[rd->mmap_idx] = rd->mmap_cur = buf;
2094 rd->mmap_idx = (rd->mmap_idx + 1) & (ARRAY_SIZE(rd->mmaps) - 1);
2095 rd->file_pos = rd->file_offset + rd->head;
2096 if (session->one_mmap) {
2097 session->one_mmap_addr = buf;
2098 session->one_mmap_offset = rd->file_offset;
2101 return 0;
2104 enum {
2105 READER_OK,
2106 READER_NODATA,
2109 static int
2110 reader__read_event(struct reader *rd, struct perf_session *session,
2111 struct ui_progress *prog)
2113 u64 size;
2114 int err = READER_OK;
2115 union perf_event *event;
2116 s64 skip;
2118 event = fetch_mmaped_event(rd->head, rd->mmap_size, rd->mmap_cur,
2119 session->header.needs_swap);
2120 if (IS_ERR(event))
2121 return PTR_ERR(event);
2123 if (!event)
2124 return READER_NODATA;
2126 size = event->header.size;
2128 skip = -EINVAL;
2130 if (size < sizeof(struct perf_event_header) ||
2131 (skip = rd->process(session, event, rd->file_pos, rd->path)) < 0) {
2132 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d [%s]\n",
2133 rd->file_offset + rd->head, event->header.size,
2134 event->header.type, strerror(-skip));
2135 err = skip;
2136 goto out;
2139 if (skip)
2140 size += skip;
2142 rd->size += size;
2143 rd->head += size;
2144 rd->file_pos += size;
2146 err = __perf_session__process_decomp_events(session);
2147 if (err)
2148 goto out;
2150 ui_progress__update(prog, size);
2152 out:
2153 return err;
2156 static inline bool
2157 reader__eof(struct reader *rd)
2159 return (rd->file_pos >= rd->data_size + rd->data_offset);
2162 static int
2163 reader__process_events(struct reader *rd, struct perf_session *session,
2164 struct ui_progress *prog)
2166 int err;
2168 err = reader__init(rd, &session->one_mmap);
2169 if (err)
2170 goto out;
2172 session->active_decomp = &rd->decomp_data;
2174 remap:
2175 err = reader__mmap(rd, session);
2176 if (err)
2177 goto out;
2179 more:
2180 err = reader__read_event(rd, session, prog);
2181 if (err < 0)
2182 goto out;
2183 else if (err == READER_NODATA)
2184 goto remap;
2186 if (session_done())
2187 goto out;
2189 if (!reader__eof(rd))
2190 goto more;
2192 out:
2193 session->active_decomp = &session->decomp_data;
2194 return err;
2197 static s64 process_simple(struct perf_session *session,
2198 union perf_event *event,
2199 u64 file_offset,
2200 const char *file_path)
2202 return perf_session__process_event(session, event, file_offset, file_path);
2205 static int __perf_session__process_events(struct perf_session *session)
2207 struct reader rd = {
2208 .fd = perf_data__fd(session->data),
2209 .path = session->data->file.path,
2210 .data_size = session->header.data_size,
2211 .data_offset = session->header.data_offset,
2212 .process = process_simple,
2213 .in_place_update = session->data->in_place_update,
2215 struct ordered_events *oe = &session->ordered_events;
2216 const struct perf_tool *tool = session->tool;
2217 struct ui_progress prog;
2218 int err;
2220 if (rd.data_size == 0)
2221 return -1;
2223 ui_progress__init_size(&prog, rd.data_size, "Processing events...");
2225 err = reader__process_events(&rd, session, &prog);
2226 if (err)
2227 goto out_err;
2228 /* do the final flush for ordered samples */
2229 err = ordered_events__flush(oe, OE_FLUSH__FINAL);
2230 if (err)
2231 goto out_err;
2232 err = auxtrace__flush_events(session, tool);
2233 if (err)
2234 goto out_err;
2235 err = perf_session__flush_thread_stacks(session);
2236 out_err:
2237 ui_progress__finish();
2238 if (!tool->no_warn)
2239 perf_session__warn_about_errors(session);
2241 * We may switching perf.data output, make ordered_events
2242 * reusable.
2244 ordered_events__reinit(&session->ordered_events);
2245 auxtrace__free_events(session);
2246 reader__release_decomp(&rd);
2247 session->one_mmap = false;
2248 return err;
2252 * Processing 2 MB of data from each reader in sequence,
2253 * because that's the way the ordered events sorting works
2254 * most efficiently.
2256 #define READER_MAX_SIZE (2 * 1024 * 1024)
2259 * This function reads, merge and process directory data.
2260 * It assumens the version 1 of directory data, where each
2261 * data file holds per-cpu data, already sorted by kernel.
2263 static int __perf_session__process_dir_events(struct perf_session *session)
2265 struct perf_data *data = session->data;
2266 const struct perf_tool *tool = session->tool;
2267 int i, ret, readers, nr_readers;
2268 struct ui_progress prog;
2269 u64 total_size = perf_data__size(session->data);
2270 struct reader *rd;
2272 ui_progress__init_size(&prog, total_size, "Processing events...");
2274 nr_readers = 1;
2275 for (i = 0; i < data->dir.nr; i++) {
2276 if (data->dir.files[i].size)
2277 nr_readers++;
2280 rd = zalloc(nr_readers * sizeof(struct reader));
2281 if (!rd)
2282 return -ENOMEM;
2284 rd[0] = (struct reader) {
2285 .fd = perf_data__fd(session->data),
2286 .path = session->data->file.path,
2287 .data_size = session->header.data_size,
2288 .data_offset = session->header.data_offset,
2289 .process = process_simple,
2290 .in_place_update = session->data->in_place_update,
2292 ret = reader__init(&rd[0], NULL);
2293 if (ret)
2294 goto out_err;
2295 ret = reader__mmap(&rd[0], session);
2296 if (ret)
2297 goto out_err;
2298 readers = 1;
2300 for (i = 0; i < data->dir.nr; i++) {
2301 if (!data->dir.files[i].size)
2302 continue;
2303 rd[readers] = (struct reader) {
2304 .fd = data->dir.files[i].fd,
2305 .path = data->dir.files[i].path,
2306 .data_size = data->dir.files[i].size,
2307 .data_offset = 0,
2308 .process = process_simple,
2309 .in_place_update = session->data->in_place_update,
2311 ret = reader__init(&rd[readers], NULL);
2312 if (ret)
2313 goto out_err;
2314 ret = reader__mmap(&rd[readers], session);
2315 if (ret)
2316 goto out_err;
2317 readers++;
2320 i = 0;
2321 while (readers) {
2322 if (session_done())
2323 break;
2325 if (rd[i].done) {
2326 i = (i + 1) % nr_readers;
2327 continue;
2329 if (reader__eof(&rd[i])) {
2330 rd[i].done = true;
2331 readers--;
2332 continue;
2335 session->active_decomp = &rd[i].decomp_data;
2336 ret = reader__read_event(&rd[i], session, &prog);
2337 if (ret < 0) {
2338 goto out_err;
2339 } else if (ret == READER_NODATA) {
2340 ret = reader__mmap(&rd[i], session);
2341 if (ret)
2342 goto out_err;
2345 if (rd[i].size >= READER_MAX_SIZE) {
2346 rd[i].size = 0;
2347 i = (i + 1) % nr_readers;
2351 ret = ordered_events__flush(&session->ordered_events, OE_FLUSH__FINAL);
2352 if (ret)
2353 goto out_err;
2355 ret = perf_session__flush_thread_stacks(session);
2356 out_err:
2357 ui_progress__finish();
2359 if (!tool->no_warn)
2360 perf_session__warn_about_errors(session);
2363 * We may switching perf.data output, make ordered_events
2364 * reusable.
2366 ordered_events__reinit(&session->ordered_events);
2368 session->one_mmap = false;
2370 session->active_decomp = &session->decomp_data;
2371 for (i = 0; i < nr_readers; i++)
2372 reader__release_decomp(&rd[i]);
2373 zfree(&rd);
2375 return ret;
2378 int perf_session__process_events(struct perf_session *session)
2380 if (perf_session__register_idle_thread(session) < 0)
2381 return -ENOMEM;
2383 if (perf_data__is_pipe(session->data))
2384 return __perf_session__process_pipe_events(session);
2386 if (perf_data__is_dir(session->data) && session->data->dir.nr)
2387 return __perf_session__process_dir_events(session);
2389 return __perf_session__process_events(session);
2392 bool perf_session__has_traces(struct perf_session *session, const char *msg)
2394 struct evsel *evsel;
2396 evlist__for_each_entry(session->evlist, evsel) {
2397 if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT)
2398 return true;
2401 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
2402 return false;
2405 int map__set_kallsyms_ref_reloc_sym(struct map *map, const char *symbol_name, u64 addr)
2407 char *bracket;
2408 struct ref_reloc_sym *ref;
2409 struct kmap *kmap;
2411 ref = zalloc(sizeof(struct ref_reloc_sym));
2412 if (ref == NULL)
2413 return -ENOMEM;
2415 ref->name = strdup(symbol_name);
2416 if (ref->name == NULL) {
2417 free(ref);
2418 return -ENOMEM;
2421 bracket = strchr(ref->name, ']');
2422 if (bracket)
2423 *bracket = '\0';
2425 ref->addr = addr;
2427 kmap = map__kmap(map);
2428 if (kmap)
2429 kmap->ref_reloc_sym = ref;
2431 return 0;
2434 size_t perf_session__fprintf_dsos(struct perf_session *session, FILE *fp)
2436 return machines__fprintf_dsos(&session->machines, fp);
2439 size_t perf_session__fprintf_dsos_buildid(struct perf_session *session, FILE *fp,
2440 bool (skip)(struct dso *dso, int parm), int parm)
2442 return machines__fprintf_dsos_buildid(&session->machines, fp, skip, parm);
2445 size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp)
2447 size_t ret;
2448 const char *msg = "";
2450 if (perf_header__has_feat(&session->header, HEADER_AUXTRACE))
2451 msg = " (excludes AUX area (e.g. instruction trace) decoded / synthesized events)";
2453 ret = fprintf(fp, "\nAggregated stats:%s\n", msg);
2455 ret += events_stats__fprintf(&session->evlist->stats, fp);
2456 return ret;
2459 size_t perf_session__fprintf(struct perf_session *session, FILE *fp)
2462 * FIXME: Here we have to actually print all the machines in this
2463 * session, not just the host...
2465 return machine__fprintf(&session->machines.host, fp);
2468 void perf_session__dump_kmaps(struct perf_session *session)
2470 int save_verbose = verbose;
2472 fflush(stdout);
2473 fprintf(stderr, "Kernel and module maps:\n");
2474 verbose = 0; /* Suppress verbose to print a summary only */
2475 maps__fprintf(machine__kernel_maps(&session->machines.host), stderr);
2476 verbose = save_verbose;
2479 struct evsel *perf_session__find_first_evtype(struct perf_session *session,
2480 unsigned int type)
2482 struct evsel *pos;
2484 evlist__for_each_entry(session->evlist, pos) {
2485 if (pos->core.attr.type == type)
2486 return pos;
2488 return NULL;
2491 int perf_session__cpu_bitmap(struct perf_session *session,
2492 const char *cpu_list, unsigned long *cpu_bitmap)
2494 int i, err = -1;
2495 struct perf_cpu_map *map;
2496 int nr_cpus = min(session->header.env.nr_cpus_avail, MAX_NR_CPUS);
2497 struct perf_cpu cpu;
2499 for (i = 0; i < PERF_TYPE_MAX; ++i) {
2500 struct evsel *evsel;
2502 evsel = perf_session__find_first_evtype(session, i);
2503 if (!evsel)
2504 continue;
2506 if (!(evsel->core.attr.sample_type & PERF_SAMPLE_CPU)) {
2507 pr_err("File does not contain CPU events. "
2508 "Remove -C option to proceed.\n");
2509 return -1;
2513 map = perf_cpu_map__new(cpu_list);
2514 if (map == NULL) {
2515 pr_err("Invalid cpu_list\n");
2516 return -1;
2519 perf_cpu_map__for_each_cpu(cpu, i, map) {
2520 if (cpu.cpu >= nr_cpus) {
2521 pr_err("Requested CPU %d too large. "
2522 "Consider raising MAX_NR_CPUS\n", cpu.cpu);
2523 goto out_delete_map;
2526 __set_bit(cpu.cpu, cpu_bitmap);
2529 err = 0;
2531 out_delete_map:
2532 perf_cpu_map__put(map);
2533 return err;
2536 void perf_session__fprintf_info(struct perf_session *session, FILE *fp,
2537 bool full)
2539 if (session == NULL || fp == NULL)
2540 return;
2542 fprintf(fp, "# ========\n");
2543 perf_header__fprintf_info(session, fp, full);
2544 fprintf(fp, "# ========\n#\n");
2547 static int perf_session__register_guest(struct perf_session *session, pid_t machine_pid)
2549 struct machine *machine = machines__findnew(&session->machines, machine_pid);
2550 struct thread *thread;
2552 if (!machine)
2553 return -ENOMEM;
2555 machine->single_address_space = session->machines.host.single_address_space;
2557 thread = machine__idle_thread(machine);
2558 if (!thread)
2559 return -ENOMEM;
2560 thread__put(thread);
2562 machine->kallsyms_filename = perf_data__guest_kallsyms_name(session->data, machine_pid);
2564 return 0;
2567 static int perf_session__set_guest_cpu(struct perf_session *session, pid_t pid,
2568 pid_t tid, int guest_cpu)
2570 struct machine *machine = &session->machines.host;
2571 struct thread *thread = machine__findnew_thread(machine, pid, tid);
2573 if (!thread)
2574 return -ENOMEM;
2575 thread__set_guest_cpu(thread, guest_cpu);
2576 thread__put(thread);
2578 return 0;
2581 int perf_event__process_id_index(struct perf_session *session,
2582 union perf_event *event)
2584 struct evlist *evlist = session->evlist;
2585 struct perf_record_id_index *ie = &event->id_index;
2586 size_t sz = ie->header.size - sizeof(*ie);
2587 size_t i, nr, max_nr;
2588 size_t e1_sz = sizeof(struct id_index_entry);
2589 size_t e2_sz = sizeof(struct id_index_entry_2);
2590 size_t etot_sz = e1_sz + e2_sz;
2591 struct id_index_entry_2 *e2;
2592 pid_t last_pid = 0;
2594 max_nr = sz / e1_sz;
2595 nr = ie->nr;
2596 if (nr > max_nr) {
2597 printf("Too big: nr %zu max_nr %zu\n", nr, max_nr);
2598 return -EINVAL;
2601 if (sz >= nr * etot_sz) {
2602 max_nr = sz / etot_sz;
2603 if (nr > max_nr) {
2604 printf("Too big2: nr %zu max_nr %zu\n", nr, max_nr);
2605 return -EINVAL;
2607 e2 = (void *)ie + sizeof(*ie) + nr * e1_sz;
2608 } else {
2609 e2 = NULL;
2612 if (dump_trace)
2613 fprintf(stdout, " nr: %zu\n", nr);
2615 for (i = 0; i < nr; i++, (e2 ? e2++ : 0)) {
2616 struct id_index_entry *e = &ie->entries[i];
2617 struct perf_sample_id *sid;
2618 int ret;
2620 if (dump_trace) {
2621 fprintf(stdout, " ... id: %"PRI_lu64, e->id);
2622 fprintf(stdout, " idx: %"PRI_lu64, e->idx);
2623 fprintf(stdout, " cpu: %"PRI_ld64, e->cpu);
2624 fprintf(stdout, " tid: %"PRI_ld64, e->tid);
2625 if (e2) {
2626 fprintf(stdout, " machine_pid: %"PRI_ld64, e2->machine_pid);
2627 fprintf(stdout, " vcpu: %"PRI_lu64"\n", e2->vcpu);
2628 } else {
2629 fprintf(stdout, "\n");
2633 sid = evlist__id2sid(evlist, e->id);
2634 if (!sid)
2635 return -ENOENT;
2637 sid->idx = e->idx;
2638 sid->cpu.cpu = e->cpu;
2639 sid->tid = e->tid;
2641 if (!e2)
2642 continue;
2644 sid->machine_pid = e2->machine_pid;
2645 sid->vcpu.cpu = e2->vcpu;
2647 if (!sid->machine_pid)
2648 continue;
2650 if (sid->machine_pid != last_pid) {
2651 ret = perf_session__register_guest(session, sid->machine_pid);
2652 if (ret)
2653 return ret;
2654 last_pid = sid->machine_pid;
2655 perf_guest = true;
2658 ret = perf_session__set_guest_cpu(session, sid->machine_pid, e->tid, e2->vcpu);
2659 if (ret)
2660 return ret;
2662 return 0;
2665 int perf_session__dsos_hit_all(struct perf_session *session)
2667 struct rb_node *nd;
2668 int err;
2670 err = machine__hit_all_dsos(&session->machines.host);
2671 if (err)
2672 return err;
2674 for (nd = rb_first_cached(&session->machines.guests); nd;
2675 nd = rb_next(nd)) {
2676 struct machine *pos = rb_entry(nd, struct machine, rb_node);
2678 err = machine__hit_all_dsos(pos);
2679 if (err)
2680 return err;
2683 return 0;