Revert "perf augmented_syscalls: Drop 'write', 'poll' for testing without self pid...
[linux/fpc-iii.git] / tools / perf / util / session.c
blob7d2c8ce6cfadb2f7c22841a671214b5bdc41c08a
1 // SPDX-License-Identifier: GPL-2.0
2 #include <errno.h>
3 #include <inttypes.h>
4 #include <linux/kernel.h>
5 #include <traceevent/event-parse.h>
6 #include <api/fs/fs.h>
8 #include <byteswap.h>
9 #include <unistd.h>
10 #include <sys/types.h>
11 #include <sys/mman.h>
13 #include "evlist.h"
14 #include "evsel.h"
15 #include "memswap.h"
16 #include "session.h"
17 #include "tool.h"
18 #include "sort.h"
19 #include "util.h"
20 #include "cpumap.h"
21 #include "perf_regs.h"
22 #include "asm/bug.h"
23 #include "auxtrace.h"
24 #include "thread.h"
25 #include "thread-stack.h"
26 #include "stat.h"
28 static int perf_session__deliver_event(struct perf_session *session,
29 union perf_event *event,
30 struct perf_tool *tool,
31 u64 file_offset);
33 static int perf_session__open(struct perf_session *session)
35 struct perf_data *data = session->data;
37 if (perf_session__read_header(session) < 0) {
38 pr_err("incompatible file format (rerun with -v to learn more)\n");
39 return -1;
42 if (perf_data__is_pipe(data))
43 return 0;
45 if (perf_header__has_feat(&session->header, HEADER_STAT))
46 return 0;
48 if (!perf_evlist__valid_sample_type(session->evlist)) {
49 pr_err("non matching sample_type\n");
50 return -1;
53 if (!perf_evlist__valid_sample_id_all(session->evlist)) {
54 pr_err("non matching sample_id_all\n");
55 return -1;
58 if (!perf_evlist__valid_read_format(session->evlist)) {
59 pr_err("non matching read_format\n");
60 return -1;
63 return 0;
66 void perf_session__set_id_hdr_size(struct perf_session *session)
68 u16 id_hdr_size = perf_evlist__id_hdr_size(session->evlist);
70 machines__set_id_hdr_size(&session->machines, id_hdr_size);
73 int perf_session__create_kernel_maps(struct perf_session *session)
75 int ret = machine__create_kernel_maps(&session->machines.host);
77 if (ret >= 0)
78 ret = machines__create_guest_kernel_maps(&session->machines);
79 return ret;
82 static void perf_session__destroy_kernel_maps(struct perf_session *session)
84 machines__destroy_kernel_maps(&session->machines);
87 static bool perf_session__has_comm_exec(struct perf_session *session)
89 struct perf_evsel *evsel;
91 evlist__for_each_entry(session->evlist, evsel) {
92 if (evsel->attr.comm_exec)
93 return true;
96 return false;
99 static void perf_session__set_comm_exec(struct perf_session *session)
101 bool comm_exec = perf_session__has_comm_exec(session);
103 machines__set_comm_exec(&session->machines, comm_exec);
106 static int ordered_events__deliver_event(struct ordered_events *oe,
107 struct ordered_event *event)
109 struct perf_session *session = container_of(oe, struct perf_session,
110 ordered_events);
112 return perf_session__deliver_event(session, event->event,
113 session->tool, event->file_offset);
116 struct perf_session *perf_session__new(struct perf_data *data,
117 bool repipe, struct perf_tool *tool)
119 struct perf_session *session = zalloc(sizeof(*session));
121 if (!session)
122 goto out;
124 session->repipe = repipe;
125 session->tool = tool;
126 INIT_LIST_HEAD(&session->auxtrace_index);
127 machines__init(&session->machines);
128 ordered_events__init(&session->ordered_events, ordered_events__deliver_event);
130 if (data) {
131 if (perf_data__open(data))
132 goto out_delete;
134 session->data = data;
136 if (perf_data__is_read(data)) {
137 if (perf_session__open(session) < 0)
138 goto out_close;
141 * set session attributes that are present in perf.data
142 * but not in pipe-mode.
144 if (!data->is_pipe) {
145 perf_session__set_id_hdr_size(session);
146 perf_session__set_comm_exec(session);
149 } else {
150 session->machines.host.env = &perf_env;
153 if (!data || perf_data__is_write(data)) {
155 * In O_RDONLY mode this will be performed when reading the
156 * kernel MMAP event, in perf_event__process_mmap().
158 if (perf_session__create_kernel_maps(session) < 0)
159 pr_warning("Cannot read kernel map\n");
163 * In pipe-mode, evlist is empty until PERF_RECORD_HEADER_ATTR is
164 * processed, so perf_evlist__sample_id_all is not meaningful here.
166 if ((!data || !data->is_pipe) && tool && tool->ordering_requires_timestamps &&
167 tool->ordered_events && !perf_evlist__sample_id_all(session->evlist)) {
168 dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n");
169 tool->ordered_events = false;
172 return session;
174 out_close:
175 perf_data__close(data);
176 out_delete:
177 perf_session__delete(session);
178 out:
179 return NULL;
182 static void perf_session__delete_threads(struct perf_session *session)
184 machine__delete_threads(&session->machines.host);
187 void perf_session__delete(struct perf_session *session)
189 if (session == NULL)
190 return;
191 auxtrace__free(session);
192 auxtrace_index__free(&session->auxtrace_index);
193 perf_session__destroy_kernel_maps(session);
194 perf_session__delete_threads(session);
195 perf_env__exit(&session->header.env);
196 machines__exit(&session->machines);
197 if (session->data)
198 perf_data__close(session->data);
199 free(session);
202 static int process_event_synth_tracing_data_stub(struct perf_session *session
203 __maybe_unused,
204 union perf_event *event
205 __maybe_unused)
207 dump_printf(": unhandled!\n");
208 return 0;
211 static int process_event_synth_attr_stub(struct perf_tool *tool __maybe_unused,
212 union perf_event *event __maybe_unused,
213 struct perf_evlist **pevlist
214 __maybe_unused)
216 dump_printf(": unhandled!\n");
217 return 0;
220 static int process_event_synth_event_update_stub(struct perf_tool *tool __maybe_unused,
221 union perf_event *event __maybe_unused,
222 struct perf_evlist **pevlist
223 __maybe_unused)
225 if (dump_trace)
226 perf_event__fprintf_event_update(event, stdout);
228 dump_printf(": unhandled!\n");
229 return 0;
232 static int process_event_sample_stub(struct perf_tool *tool __maybe_unused,
233 union perf_event *event __maybe_unused,
234 struct perf_sample *sample __maybe_unused,
235 struct perf_evsel *evsel __maybe_unused,
236 struct machine *machine __maybe_unused)
238 dump_printf(": unhandled!\n");
239 return 0;
242 static int process_event_stub(struct perf_tool *tool __maybe_unused,
243 union perf_event *event __maybe_unused,
244 struct perf_sample *sample __maybe_unused,
245 struct machine *machine __maybe_unused)
247 dump_printf(": unhandled!\n");
248 return 0;
251 static int process_finished_round_stub(struct perf_tool *tool __maybe_unused,
252 union perf_event *event __maybe_unused,
253 struct ordered_events *oe __maybe_unused)
255 dump_printf(": unhandled!\n");
256 return 0;
259 static int process_finished_round(struct perf_tool *tool,
260 union perf_event *event,
261 struct ordered_events *oe);
263 static int skipn(int fd, off_t n)
265 char buf[4096];
266 ssize_t ret;
268 while (n > 0) {
269 ret = read(fd, buf, min(n, (off_t)sizeof(buf)));
270 if (ret <= 0)
271 return ret;
272 n -= ret;
275 return 0;
278 static s64 process_event_auxtrace_stub(struct perf_session *session __maybe_unused,
279 union perf_event *event)
281 dump_printf(": unhandled!\n");
282 if (perf_data__is_pipe(session->data))
283 skipn(perf_data__fd(session->data), event->auxtrace.size);
284 return event->auxtrace.size;
287 static int process_event_op2_stub(struct perf_session *session __maybe_unused,
288 union perf_event *event __maybe_unused)
290 dump_printf(": unhandled!\n");
291 return 0;
295 static
296 int process_event_thread_map_stub(struct perf_session *session __maybe_unused,
297 union perf_event *event __maybe_unused)
299 if (dump_trace)
300 perf_event__fprintf_thread_map(event, stdout);
302 dump_printf(": unhandled!\n");
303 return 0;
306 static
307 int process_event_cpu_map_stub(struct perf_session *session __maybe_unused,
308 union perf_event *event __maybe_unused)
310 if (dump_trace)
311 perf_event__fprintf_cpu_map(event, stdout);
313 dump_printf(": unhandled!\n");
314 return 0;
317 static
318 int process_event_stat_config_stub(struct perf_session *session __maybe_unused,
319 union perf_event *event __maybe_unused)
321 if (dump_trace)
322 perf_event__fprintf_stat_config(event, stdout);
324 dump_printf(": unhandled!\n");
325 return 0;
328 static int process_stat_stub(struct perf_session *perf_session __maybe_unused,
329 union perf_event *event)
331 if (dump_trace)
332 perf_event__fprintf_stat(event, stdout);
334 dump_printf(": unhandled!\n");
335 return 0;
338 static int process_stat_round_stub(struct perf_session *perf_session __maybe_unused,
339 union perf_event *event)
341 if (dump_trace)
342 perf_event__fprintf_stat_round(event, stdout);
344 dump_printf(": unhandled!\n");
345 return 0;
348 void perf_tool__fill_defaults(struct perf_tool *tool)
350 if (tool->sample == NULL)
351 tool->sample = process_event_sample_stub;
352 if (tool->mmap == NULL)
353 tool->mmap = process_event_stub;
354 if (tool->mmap2 == NULL)
355 tool->mmap2 = process_event_stub;
356 if (tool->comm == NULL)
357 tool->comm = process_event_stub;
358 if (tool->namespaces == NULL)
359 tool->namespaces = process_event_stub;
360 if (tool->fork == NULL)
361 tool->fork = process_event_stub;
362 if (tool->exit == NULL)
363 tool->exit = process_event_stub;
364 if (tool->lost == NULL)
365 tool->lost = perf_event__process_lost;
366 if (tool->lost_samples == NULL)
367 tool->lost_samples = perf_event__process_lost_samples;
368 if (tool->aux == NULL)
369 tool->aux = perf_event__process_aux;
370 if (tool->itrace_start == NULL)
371 tool->itrace_start = perf_event__process_itrace_start;
372 if (tool->context_switch == NULL)
373 tool->context_switch = perf_event__process_switch;
374 if (tool->read == NULL)
375 tool->read = process_event_sample_stub;
376 if (tool->throttle == NULL)
377 tool->throttle = process_event_stub;
378 if (tool->unthrottle == NULL)
379 tool->unthrottle = process_event_stub;
380 if (tool->attr == NULL)
381 tool->attr = process_event_synth_attr_stub;
382 if (tool->event_update == NULL)
383 tool->event_update = process_event_synth_event_update_stub;
384 if (tool->tracing_data == NULL)
385 tool->tracing_data = process_event_synth_tracing_data_stub;
386 if (tool->build_id == NULL)
387 tool->build_id = process_event_op2_stub;
388 if (tool->finished_round == NULL) {
389 if (tool->ordered_events)
390 tool->finished_round = process_finished_round;
391 else
392 tool->finished_round = process_finished_round_stub;
394 if (tool->id_index == NULL)
395 tool->id_index = process_event_op2_stub;
396 if (tool->auxtrace_info == NULL)
397 tool->auxtrace_info = process_event_op2_stub;
398 if (tool->auxtrace == NULL)
399 tool->auxtrace = process_event_auxtrace_stub;
400 if (tool->auxtrace_error == NULL)
401 tool->auxtrace_error = process_event_op2_stub;
402 if (tool->thread_map == NULL)
403 tool->thread_map = process_event_thread_map_stub;
404 if (tool->cpu_map == NULL)
405 tool->cpu_map = process_event_cpu_map_stub;
406 if (tool->stat_config == NULL)
407 tool->stat_config = process_event_stat_config_stub;
408 if (tool->stat == NULL)
409 tool->stat = process_stat_stub;
410 if (tool->stat_round == NULL)
411 tool->stat_round = process_stat_round_stub;
412 if (tool->time_conv == NULL)
413 tool->time_conv = process_event_op2_stub;
414 if (tool->feature == NULL)
415 tool->feature = process_event_op2_stub;
418 static void swap_sample_id_all(union perf_event *event, void *data)
420 void *end = (void *) event + event->header.size;
421 int size = end - data;
423 BUG_ON(size % sizeof(u64));
424 mem_bswap_64(data, size);
427 static void perf_event__all64_swap(union perf_event *event,
428 bool sample_id_all __maybe_unused)
430 struct perf_event_header *hdr = &event->header;
431 mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr));
434 static void perf_event__comm_swap(union perf_event *event, bool sample_id_all)
436 event->comm.pid = bswap_32(event->comm.pid);
437 event->comm.tid = bswap_32(event->comm.tid);
439 if (sample_id_all) {
440 void *data = &event->comm.comm;
442 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
443 swap_sample_id_all(event, data);
447 static void perf_event__mmap_swap(union perf_event *event,
448 bool sample_id_all)
450 event->mmap.pid = bswap_32(event->mmap.pid);
451 event->mmap.tid = bswap_32(event->mmap.tid);
452 event->mmap.start = bswap_64(event->mmap.start);
453 event->mmap.len = bswap_64(event->mmap.len);
454 event->mmap.pgoff = bswap_64(event->mmap.pgoff);
456 if (sample_id_all) {
457 void *data = &event->mmap.filename;
459 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
460 swap_sample_id_all(event, data);
464 static void perf_event__mmap2_swap(union perf_event *event,
465 bool sample_id_all)
467 event->mmap2.pid = bswap_32(event->mmap2.pid);
468 event->mmap2.tid = bswap_32(event->mmap2.tid);
469 event->mmap2.start = bswap_64(event->mmap2.start);
470 event->mmap2.len = bswap_64(event->mmap2.len);
471 event->mmap2.pgoff = bswap_64(event->mmap2.pgoff);
472 event->mmap2.maj = bswap_32(event->mmap2.maj);
473 event->mmap2.min = bswap_32(event->mmap2.min);
474 event->mmap2.ino = bswap_64(event->mmap2.ino);
476 if (sample_id_all) {
477 void *data = &event->mmap2.filename;
479 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
480 swap_sample_id_all(event, data);
483 static void perf_event__task_swap(union perf_event *event, bool sample_id_all)
485 event->fork.pid = bswap_32(event->fork.pid);
486 event->fork.tid = bswap_32(event->fork.tid);
487 event->fork.ppid = bswap_32(event->fork.ppid);
488 event->fork.ptid = bswap_32(event->fork.ptid);
489 event->fork.time = bswap_64(event->fork.time);
491 if (sample_id_all)
492 swap_sample_id_all(event, &event->fork + 1);
495 static void perf_event__read_swap(union perf_event *event, bool sample_id_all)
497 event->read.pid = bswap_32(event->read.pid);
498 event->read.tid = bswap_32(event->read.tid);
499 event->read.value = bswap_64(event->read.value);
500 event->read.time_enabled = bswap_64(event->read.time_enabled);
501 event->read.time_running = bswap_64(event->read.time_running);
502 event->read.id = bswap_64(event->read.id);
504 if (sample_id_all)
505 swap_sample_id_all(event, &event->read + 1);
508 static void perf_event__aux_swap(union perf_event *event, bool sample_id_all)
510 event->aux.aux_offset = bswap_64(event->aux.aux_offset);
511 event->aux.aux_size = bswap_64(event->aux.aux_size);
512 event->aux.flags = bswap_64(event->aux.flags);
514 if (sample_id_all)
515 swap_sample_id_all(event, &event->aux + 1);
518 static void perf_event__itrace_start_swap(union perf_event *event,
519 bool sample_id_all)
521 event->itrace_start.pid = bswap_32(event->itrace_start.pid);
522 event->itrace_start.tid = bswap_32(event->itrace_start.tid);
524 if (sample_id_all)
525 swap_sample_id_all(event, &event->itrace_start + 1);
528 static void perf_event__switch_swap(union perf_event *event, bool sample_id_all)
530 if (event->header.type == PERF_RECORD_SWITCH_CPU_WIDE) {
531 event->context_switch.next_prev_pid =
532 bswap_32(event->context_switch.next_prev_pid);
533 event->context_switch.next_prev_tid =
534 bswap_32(event->context_switch.next_prev_tid);
537 if (sample_id_all)
538 swap_sample_id_all(event, &event->context_switch + 1);
541 static void perf_event__throttle_swap(union perf_event *event,
542 bool sample_id_all)
544 event->throttle.time = bswap_64(event->throttle.time);
545 event->throttle.id = bswap_64(event->throttle.id);
546 event->throttle.stream_id = bswap_64(event->throttle.stream_id);
548 if (sample_id_all)
549 swap_sample_id_all(event, &event->throttle + 1);
552 static u8 revbyte(u8 b)
554 int rev = (b >> 4) | ((b & 0xf) << 4);
555 rev = ((rev & 0xcc) >> 2) | ((rev & 0x33) << 2);
556 rev = ((rev & 0xaa) >> 1) | ((rev & 0x55) << 1);
557 return (u8) rev;
561 * XXX this is hack in attempt to carry flags bitfield
562 * through endian village. ABI says:
564 * Bit-fields are allocated from right to left (least to most significant)
565 * on little-endian implementations and from left to right (most to least
566 * significant) on big-endian implementations.
568 * The above seems to be byte specific, so we need to reverse each
569 * byte of the bitfield. 'Internet' also says this might be implementation
570 * specific and we probably need proper fix and carry perf_event_attr
571 * bitfield flags in separate data file FEAT_ section. Thought this seems
572 * to work for now.
574 static void swap_bitfield(u8 *p, unsigned len)
576 unsigned i;
578 for (i = 0; i < len; i++) {
579 *p = revbyte(*p);
580 p++;
584 /* exported for swapping attributes in file header */
585 void perf_event__attr_swap(struct perf_event_attr *attr)
587 attr->type = bswap_32(attr->type);
588 attr->size = bswap_32(attr->size);
590 #define bswap_safe(f, n) \
591 (attr->size > (offsetof(struct perf_event_attr, f) + \
592 sizeof(attr->f) * (n)))
593 #define bswap_field(f, sz) \
594 do { \
595 if (bswap_safe(f, 0)) \
596 attr->f = bswap_##sz(attr->f); \
597 } while(0)
598 #define bswap_field_16(f) bswap_field(f, 16)
599 #define bswap_field_32(f) bswap_field(f, 32)
600 #define bswap_field_64(f) bswap_field(f, 64)
602 bswap_field_64(config);
603 bswap_field_64(sample_period);
604 bswap_field_64(sample_type);
605 bswap_field_64(read_format);
606 bswap_field_32(wakeup_events);
607 bswap_field_32(bp_type);
608 bswap_field_64(bp_addr);
609 bswap_field_64(bp_len);
610 bswap_field_64(branch_sample_type);
611 bswap_field_64(sample_regs_user);
612 bswap_field_32(sample_stack_user);
613 bswap_field_32(aux_watermark);
614 bswap_field_16(sample_max_stack);
617 * After read_format are bitfields. Check read_format because
618 * we are unable to use offsetof on bitfield.
620 if (bswap_safe(read_format, 1))
621 swap_bitfield((u8 *) (&attr->read_format + 1),
622 sizeof(u64));
623 #undef bswap_field_64
624 #undef bswap_field_32
625 #undef bswap_field
626 #undef bswap_safe
629 static void perf_event__hdr_attr_swap(union perf_event *event,
630 bool sample_id_all __maybe_unused)
632 size_t size;
634 perf_event__attr_swap(&event->attr.attr);
636 size = event->header.size;
637 size -= (void *)&event->attr.id - (void *)event;
638 mem_bswap_64(event->attr.id, size);
641 static void perf_event__event_update_swap(union perf_event *event,
642 bool sample_id_all __maybe_unused)
644 event->event_update.type = bswap_64(event->event_update.type);
645 event->event_update.id = bswap_64(event->event_update.id);
648 static void perf_event__event_type_swap(union perf_event *event,
649 bool sample_id_all __maybe_unused)
651 event->event_type.event_type.event_id =
652 bswap_64(event->event_type.event_type.event_id);
655 static void perf_event__tracing_data_swap(union perf_event *event,
656 bool sample_id_all __maybe_unused)
658 event->tracing_data.size = bswap_32(event->tracing_data.size);
661 static void perf_event__auxtrace_info_swap(union perf_event *event,
662 bool sample_id_all __maybe_unused)
664 size_t size;
666 event->auxtrace_info.type = bswap_32(event->auxtrace_info.type);
668 size = event->header.size;
669 size -= (void *)&event->auxtrace_info.priv - (void *)event;
670 mem_bswap_64(event->auxtrace_info.priv, size);
673 static void perf_event__auxtrace_swap(union perf_event *event,
674 bool sample_id_all __maybe_unused)
676 event->auxtrace.size = bswap_64(event->auxtrace.size);
677 event->auxtrace.offset = bswap_64(event->auxtrace.offset);
678 event->auxtrace.reference = bswap_64(event->auxtrace.reference);
679 event->auxtrace.idx = bswap_32(event->auxtrace.idx);
680 event->auxtrace.tid = bswap_32(event->auxtrace.tid);
681 event->auxtrace.cpu = bswap_32(event->auxtrace.cpu);
684 static void perf_event__auxtrace_error_swap(union perf_event *event,
685 bool sample_id_all __maybe_unused)
687 event->auxtrace_error.type = bswap_32(event->auxtrace_error.type);
688 event->auxtrace_error.code = bswap_32(event->auxtrace_error.code);
689 event->auxtrace_error.cpu = bswap_32(event->auxtrace_error.cpu);
690 event->auxtrace_error.pid = bswap_32(event->auxtrace_error.pid);
691 event->auxtrace_error.tid = bswap_32(event->auxtrace_error.tid);
692 event->auxtrace_error.ip = bswap_64(event->auxtrace_error.ip);
695 static void perf_event__thread_map_swap(union perf_event *event,
696 bool sample_id_all __maybe_unused)
698 unsigned i;
700 event->thread_map.nr = bswap_64(event->thread_map.nr);
702 for (i = 0; i < event->thread_map.nr; i++)
703 event->thread_map.entries[i].pid = bswap_64(event->thread_map.entries[i].pid);
706 static void perf_event__cpu_map_swap(union perf_event *event,
707 bool sample_id_all __maybe_unused)
709 struct cpu_map_data *data = &event->cpu_map.data;
710 struct cpu_map_entries *cpus;
711 struct cpu_map_mask *mask;
712 unsigned i;
714 data->type = bswap_64(data->type);
716 switch (data->type) {
717 case PERF_CPU_MAP__CPUS:
718 cpus = (struct cpu_map_entries *)data->data;
720 cpus->nr = bswap_16(cpus->nr);
722 for (i = 0; i < cpus->nr; i++)
723 cpus->cpu[i] = bswap_16(cpus->cpu[i]);
724 break;
725 case PERF_CPU_MAP__MASK:
726 mask = (struct cpu_map_mask *) data->data;
728 mask->nr = bswap_16(mask->nr);
729 mask->long_size = bswap_16(mask->long_size);
731 switch (mask->long_size) {
732 case 4: mem_bswap_32(&mask->mask, mask->nr); break;
733 case 8: mem_bswap_64(&mask->mask, mask->nr); break;
734 default:
735 pr_err("cpu_map swap: unsupported long size\n");
737 default:
738 break;
742 static void perf_event__stat_config_swap(union perf_event *event,
743 bool sample_id_all __maybe_unused)
745 u64 size;
747 size = event->stat_config.nr * sizeof(event->stat_config.data[0]);
748 size += 1; /* nr item itself */
749 mem_bswap_64(&event->stat_config.nr, size);
752 static void perf_event__stat_swap(union perf_event *event,
753 bool sample_id_all __maybe_unused)
755 event->stat.id = bswap_64(event->stat.id);
756 event->stat.thread = bswap_32(event->stat.thread);
757 event->stat.cpu = bswap_32(event->stat.cpu);
758 event->stat.val = bswap_64(event->stat.val);
759 event->stat.ena = bswap_64(event->stat.ena);
760 event->stat.run = bswap_64(event->stat.run);
763 static void perf_event__stat_round_swap(union perf_event *event,
764 bool sample_id_all __maybe_unused)
766 event->stat_round.type = bswap_64(event->stat_round.type);
767 event->stat_round.time = bswap_64(event->stat_round.time);
770 typedef void (*perf_event__swap_op)(union perf_event *event,
771 bool sample_id_all);
773 static perf_event__swap_op perf_event__swap_ops[] = {
774 [PERF_RECORD_MMAP] = perf_event__mmap_swap,
775 [PERF_RECORD_MMAP2] = perf_event__mmap2_swap,
776 [PERF_RECORD_COMM] = perf_event__comm_swap,
777 [PERF_RECORD_FORK] = perf_event__task_swap,
778 [PERF_RECORD_EXIT] = perf_event__task_swap,
779 [PERF_RECORD_LOST] = perf_event__all64_swap,
780 [PERF_RECORD_READ] = perf_event__read_swap,
781 [PERF_RECORD_THROTTLE] = perf_event__throttle_swap,
782 [PERF_RECORD_UNTHROTTLE] = perf_event__throttle_swap,
783 [PERF_RECORD_SAMPLE] = perf_event__all64_swap,
784 [PERF_RECORD_AUX] = perf_event__aux_swap,
785 [PERF_RECORD_ITRACE_START] = perf_event__itrace_start_swap,
786 [PERF_RECORD_LOST_SAMPLES] = perf_event__all64_swap,
787 [PERF_RECORD_SWITCH] = perf_event__switch_swap,
788 [PERF_RECORD_SWITCH_CPU_WIDE] = perf_event__switch_swap,
789 [PERF_RECORD_HEADER_ATTR] = perf_event__hdr_attr_swap,
790 [PERF_RECORD_HEADER_EVENT_TYPE] = perf_event__event_type_swap,
791 [PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap,
792 [PERF_RECORD_HEADER_BUILD_ID] = NULL,
793 [PERF_RECORD_ID_INDEX] = perf_event__all64_swap,
794 [PERF_RECORD_AUXTRACE_INFO] = perf_event__auxtrace_info_swap,
795 [PERF_RECORD_AUXTRACE] = perf_event__auxtrace_swap,
796 [PERF_RECORD_AUXTRACE_ERROR] = perf_event__auxtrace_error_swap,
797 [PERF_RECORD_THREAD_MAP] = perf_event__thread_map_swap,
798 [PERF_RECORD_CPU_MAP] = perf_event__cpu_map_swap,
799 [PERF_RECORD_STAT_CONFIG] = perf_event__stat_config_swap,
800 [PERF_RECORD_STAT] = perf_event__stat_swap,
801 [PERF_RECORD_STAT_ROUND] = perf_event__stat_round_swap,
802 [PERF_RECORD_EVENT_UPDATE] = perf_event__event_update_swap,
803 [PERF_RECORD_TIME_CONV] = perf_event__all64_swap,
804 [PERF_RECORD_HEADER_MAX] = NULL,
808 * When perf record finishes a pass on every buffers, it records this pseudo
809 * event.
810 * We record the max timestamp t found in the pass n.
811 * Assuming these timestamps are monotonic across cpus, we know that if
812 * a buffer still has events with timestamps below t, they will be all
813 * available and then read in the pass n + 1.
814 * Hence when we start to read the pass n + 2, we can safely flush every
815 * events with timestamps below t.
817 * ============ PASS n =================
818 * CPU 0 | CPU 1
820 * cnt1 timestamps | cnt2 timestamps
821 * 1 | 2
822 * 2 | 3
823 * - | 4 <--- max recorded
825 * ============ PASS n + 1 ==============
826 * CPU 0 | CPU 1
828 * cnt1 timestamps | cnt2 timestamps
829 * 3 | 5
830 * 4 | 6
831 * 5 | 7 <---- max recorded
833 * Flush every events below timestamp 4
835 * ============ PASS n + 2 ==============
836 * CPU 0 | CPU 1
838 * cnt1 timestamps | cnt2 timestamps
839 * 6 | 8
840 * 7 | 9
841 * - | 10
843 * Flush every events below timestamp 7
844 * etc...
846 static int process_finished_round(struct perf_tool *tool __maybe_unused,
847 union perf_event *event __maybe_unused,
848 struct ordered_events *oe)
850 if (dump_trace)
851 fprintf(stdout, "\n");
852 return ordered_events__flush(oe, OE_FLUSH__ROUND);
855 int perf_session__queue_event(struct perf_session *s, union perf_event *event,
856 u64 timestamp, u64 file_offset)
858 return ordered_events__queue(&s->ordered_events, event, timestamp, file_offset);
861 static void callchain__lbr_callstack_printf(struct perf_sample *sample)
863 struct ip_callchain *callchain = sample->callchain;
864 struct branch_stack *lbr_stack = sample->branch_stack;
865 u64 kernel_callchain_nr = callchain->nr;
866 unsigned int i;
868 for (i = 0; i < kernel_callchain_nr; i++) {
869 if (callchain->ips[i] == PERF_CONTEXT_USER)
870 break;
873 if ((i != kernel_callchain_nr) && lbr_stack->nr) {
874 u64 total_nr;
876 * LBR callstack can only get user call chain,
877 * i is kernel call chain number,
878 * 1 is PERF_CONTEXT_USER.
880 * The user call chain is stored in LBR registers.
881 * LBR are pair registers. The caller is stored
882 * in "from" register, while the callee is stored
883 * in "to" register.
884 * For example, there is a call stack
885 * "A"->"B"->"C"->"D".
886 * The LBR registers will recorde like
887 * "C"->"D", "B"->"C", "A"->"B".
888 * So only the first "to" register and all "from"
889 * registers are needed to construct the whole stack.
891 total_nr = i + 1 + lbr_stack->nr + 1;
892 kernel_callchain_nr = i + 1;
894 printf("... LBR call chain: nr:%" PRIu64 "\n", total_nr);
896 for (i = 0; i < kernel_callchain_nr; i++)
897 printf("..... %2d: %016" PRIx64 "\n",
898 i, callchain->ips[i]);
900 printf("..... %2d: %016" PRIx64 "\n",
901 (int)(kernel_callchain_nr), lbr_stack->entries[0].to);
902 for (i = 0; i < lbr_stack->nr; i++)
903 printf("..... %2d: %016" PRIx64 "\n",
904 (int)(i + kernel_callchain_nr + 1), lbr_stack->entries[i].from);
908 static void callchain__printf(struct perf_evsel *evsel,
909 struct perf_sample *sample)
911 unsigned int i;
912 struct ip_callchain *callchain = sample->callchain;
914 if (perf_evsel__has_branch_callstack(evsel))
915 callchain__lbr_callstack_printf(sample);
917 printf("... FP chain: nr:%" PRIu64 "\n", callchain->nr);
919 for (i = 0; i < callchain->nr; i++)
920 printf("..... %2d: %016" PRIx64 "\n",
921 i, callchain->ips[i]);
924 static void branch_stack__printf(struct perf_sample *sample)
926 uint64_t i;
928 printf("... branch stack: nr:%" PRIu64 "\n", sample->branch_stack->nr);
930 for (i = 0; i < sample->branch_stack->nr; i++) {
931 struct branch_entry *e = &sample->branch_stack->entries[i];
933 printf("..... %2"PRIu64": %016" PRIx64 " -> %016" PRIx64 " %hu cycles %s%s%s%s %x\n",
934 i, e->from, e->to,
935 (unsigned short)e->flags.cycles,
936 e->flags.mispred ? "M" : " ",
937 e->flags.predicted ? "P" : " ",
938 e->flags.abort ? "A" : " ",
939 e->flags.in_tx ? "T" : " ",
940 (unsigned)e->flags.reserved);
944 static void regs_dump__printf(u64 mask, u64 *regs)
946 unsigned rid, i = 0;
948 for_each_set_bit(rid, (unsigned long *) &mask, sizeof(mask) * 8) {
949 u64 val = regs[i++];
951 printf(".... %-5s 0x%" PRIx64 "\n",
952 perf_reg_name(rid), val);
956 static const char *regs_abi[] = {
957 [PERF_SAMPLE_REGS_ABI_NONE] = "none",
958 [PERF_SAMPLE_REGS_ABI_32] = "32-bit",
959 [PERF_SAMPLE_REGS_ABI_64] = "64-bit",
962 static inline const char *regs_dump_abi(struct regs_dump *d)
964 if (d->abi > PERF_SAMPLE_REGS_ABI_64)
965 return "unknown";
967 return regs_abi[d->abi];
970 static void regs__printf(const char *type, struct regs_dump *regs)
972 u64 mask = regs->mask;
974 printf("... %s regs: mask 0x%" PRIx64 " ABI %s\n",
975 type,
976 mask,
977 regs_dump_abi(regs));
979 regs_dump__printf(mask, regs->regs);
982 static void regs_user__printf(struct perf_sample *sample)
984 struct regs_dump *user_regs = &sample->user_regs;
986 if (user_regs->regs)
987 regs__printf("user", user_regs);
990 static void regs_intr__printf(struct perf_sample *sample)
992 struct regs_dump *intr_regs = &sample->intr_regs;
994 if (intr_regs->regs)
995 regs__printf("intr", intr_regs);
998 static void stack_user__printf(struct stack_dump *dump)
1000 printf("... ustack: size %" PRIu64 ", offset 0x%x\n",
1001 dump->size, dump->offset);
1004 static void perf_evlist__print_tstamp(struct perf_evlist *evlist,
1005 union perf_event *event,
1006 struct perf_sample *sample)
1008 u64 sample_type = __perf_evlist__combined_sample_type(evlist);
1010 if (event->header.type != PERF_RECORD_SAMPLE &&
1011 !perf_evlist__sample_id_all(evlist)) {
1012 fputs("-1 -1 ", stdout);
1013 return;
1016 if ((sample_type & PERF_SAMPLE_CPU))
1017 printf("%u ", sample->cpu);
1019 if (sample_type & PERF_SAMPLE_TIME)
1020 printf("%" PRIu64 " ", sample->time);
1023 static void sample_read__printf(struct perf_sample *sample, u64 read_format)
1025 printf("... sample_read:\n");
1027 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1028 printf("...... time enabled %016" PRIx64 "\n",
1029 sample->read.time_enabled);
1031 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1032 printf("...... time running %016" PRIx64 "\n",
1033 sample->read.time_running);
1035 if (read_format & PERF_FORMAT_GROUP) {
1036 u64 i;
1038 printf(".... group nr %" PRIu64 "\n", sample->read.group.nr);
1040 for (i = 0; i < sample->read.group.nr; i++) {
1041 struct sample_read_value *value;
1043 value = &sample->read.group.values[i];
1044 printf("..... id %016" PRIx64
1045 ", value %016" PRIx64 "\n",
1046 value->id, value->value);
1048 } else
1049 printf("..... id %016" PRIx64 ", value %016" PRIx64 "\n",
1050 sample->read.one.id, sample->read.one.value);
1053 static void dump_event(struct perf_evlist *evlist, union perf_event *event,
1054 u64 file_offset, struct perf_sample *sample)
1056 if (!dump_trace)
1057 return;
1059 printf("\n%#" PRIx64 " [%#x]: event: %d\n",
1060 file_offset, event->header.size, event->header.type);
1062 trace_event(event);
1064 if (sample)
1065 perf_evlist__print_tstamp(evlist, event, sample);
1067 printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset,
1068 event->header.size, perf_event__name(event->header.type));
1071 static void dump_sample(struct perf_evsel *evsel, union perf_event *event,
1072 struct perf_sample *sample)
1074 u64 sample_type;
1076 if (!dump_trace)
1077 return;
1079 printf("(IP, 0x%x): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n",
1080 event->header.misc, sample->pid, sample->tid, sample->ip,
1081 sample->period, sample->addr);
1083 sample_type = evsel->attr.sample_type;
1085 if (evsel__has_callchain(evsel))
1086 callchain__printf(evsel, sample);
1088 if ((sample_type & PERF_SAMPLE_BRANCH_STACK) && !perf_evsel__has_branch_callstack(evsel))
1089 branch_stack__printf(sample);
1091 if (sample_type & PERF_SAMPLE_REGS_USER)
1092 regs_user__printf(sample);
1094 if (sample_type & PERF_SAMPLE_REGS_INTR)
1095 regs_intr__printf(sample);
1097 if (sample_type & PERF_SAMPLE_STACK_USER)
1098 stack_user__printf(&sample->user_stack);
1100 if (sample_type & PERF_SAMPLE_WEIGHT)
1101 printf("... weight: %" PRIu64 "\n", sample->weight);
1103 if (sample_type & PERF_SAMPLE_DATA_SRC)
1104 printf(" . data_src: 0x%"PRIx64"\n", sample->data_src);
1106 if (sample_type & PERF_SAMPLE_PHYS_ADDR)
1107 printf(" .. phys_addr: 0x%"PRIx64"\n", sample->phys_addr);
1109 if (sample_type & PERF_SAMPLE_TRANSACTION)
1110 printf("... transaction: %" PRIx64 "\n", sample->transaction);
1112 if (sample_type & PERF_SAMPLE_READ)
1113 sample_read__printf(sample, evsel->attr.read_format);
1116 static void dump_read(struct perf_evsel *evsel, union perf_event *event)
1118 struct read_event *read_event = &event->read;
1119 u64 read_format;
1121 if (!dump_trace)
1122 return;
1124 printf(": %d %d %s %" PRIu64 "\n", event->read.pid, event->read.tid,
1125 evsel ? perf_evsel__name(evsel) : "FAIL",
1126 event->read.value);
1128 read_format = evsel->attr.read_format;
1130 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1131 printf("... time enabled : %" PRIu64 "\n", read_event->time_enabled);
1133 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1134 printf("... time running : %" PRIu64 "\n", read_event->time_running);
1136 if (read_format & PERF_FORMAT_ID)
1137 printf("... id : %" PRIu64 "\n", read_event->id);
1140 static struct machine *machines__find_for_cpumode(struct machines *machines,
1141 union perf_event *event,
1142 struct perf_sample *sample)
1144 struct machine *machine;
1146 if (perf_guest &&
1147 ((sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL) ||
1148 (sample->cpumode == PERF_RECORD_MISC_GUEST_USER))) {
1149 u32 pid;
1151 if (event->header.type == PERF_RECORD_MMAP
1152 || event->header.type == PERF_RECORD_MMAP2)
1153 pid = event->mmap.pid;
1154 else
1155 pid = sample->pid;
1157 machine = machines__find(machines, pid);
1158 if (!machine)
1159 machine = machines__findnew(machines, DEFAULT_GUEST_KERNEL_ID);
1160 return machine;
1163 return &machines->host;
1166 static int deliver_sample_value(struct perf_evlist *evlist,
1167 struct perf_tool *tool,
1168 union perf_event *event,
1169 struct perf_sample *sample,
1170 struct sample_read_value *v,
1171 struct machine *machine)
1173 struct perf_sample_id *sid = perf_evlist__id2sid(evlist, v->id);
1175 if (sid) {
1176 sample->id = v->id;
1177 sample->period = v->value - sid->period;
1178 sid->period = v->value;
1181 if (!sid || sid->evsel == NULL) {
1182 ++evlist->stats.nr_unknown_id;
1183 return 0;
1186 return tool->sample(tool, event, sample, sid->evsel, machine);
1189 static int deliver_sample_group(struct perf_evlist *evlist,
1190 struct perf_tool *tool,
1191 union perf_event *event,
1192 struct perf_sample *sample,
1193 struct machine *machine)
1195 int ret = -EINVAL;
1196 u64 i;
1198 for (i = 0; i < sample->read.group.nr; i++) {
1199 ret = deliver_sample_value(evlist, tool, event, sample,
1200 &sample->read.group.values[i],
1201 machine);
1202 if (ret)
1203 break;
1206 return ret;
1209 static int
1210 perf_evlist__deliver_sample(struct perf_evlist *evlist,
1211 struct perf_tool *tool,
1212 union perf_event *event,
1213 struct perf_sample *sample,
1214 struct perf_evsel *evsel,
1215 struct machine *machine)
1217 /* We know evsel != NULL. */
1218 u64 sample_type = evsel->attr.sample_type;
1219 u64 read_format = evsel->attr.read_format;
1221 /* Standard sample delivery. */
1222 if (!(sample_type & PERF_SAMPLE_READ))
1223 return tool->sample(tool, event, sample, evsel, machine);
1225 /* For PERF_SAMPLE_READ we have either single or group mode. */
1226 if (read_format & PERF_FORMAT_GROUP)
1227 return deliver_sample_group(evlist, tool, event, sample,
1228 machine);
1229 else
1230 return deliver_sample_value(evlist, tool, event, sample,
1231 &sample->read.one, machine);
1234 static int machines__deliver_event(struct machines *machines,
1235 struct perf_evlist *evlist,
1236 union perf_event *event,
1237 struct perf_sample *sample,
1238 struct perf_tool *tool, u64 file_offset)
1240 struct perf_evsel *evsel;
1241 struct machine *machine;
1243 dump_event(evlist, event, file_offset, sample);
1245 evsel = perf_evlist__id2evsel(evlist, sample->id);
1247 machine = machines__find_for_cpumode(machines, event, sample);
1249 switch (event->header.type) {
1250 case PERF_RECORD_SAMPLE:
1251 if (evsel == NULL) {
1252 ++evlist->stats.nr_unknown_id;
1253 return 0;
1255 dump_sample(evsel, event, sample);
1256 if (machine == NULL) {
1257 ++evlist->stats.nr_unprocessable_samples;
1258 return 0;
1260 return perf_evlist__deliver_sample(evlist, tool, event, sample, evsel, machine);
1261 case PERF_RECORD_MMAP:
1262 return tool->mmap(tool, event, sample, machine);
1263 case PERF_RECORD_MMAP2:
1264 if (event->header.misc & PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT)
1265 ++evlist->stats.nr_proc_map_timeout;
1266 return tool->mmap2(tool, event, sample, machine);
1267 case PERF_RECORD_COMM:
1268 return tool->comm(tool, event, sample, machine);
1269 case PERF_RECORD_NAMESPACES:
1270 return tool->namespaces(tool, event, sample, machine);
1271 case PERF_RECORD_FORK:
1272 return tool->fork(tool, event, sample, machine);
1273 case PERF_RECORD_EXIT:
1274 return tool->exit(tool, event, sample, machine);
1275 case PERF_RECORD_LOST:
1276 if (tool->lost == perf_event__process_lost)
1277 evlist->stats.total_lost += event->lost.lost;
1278 return tool->lost(tool, event, sample, machine);
1279 case PERF_RECORD_LOST_SAMPLES:
1280 if (tool->lost_samples == perf_event__process_lost_samples)
1281 evlist->stats.total_lost_samples += event->lost_samples.lost;
1282 return tool->lost_samples(tool, event, sample, machine);
1283 case PERF_RECORD_READ:
1284 dump_read(evsel, event);
1285 return tool->read(tool, event, sample, evsel, machine);
1286 case PERF_RECORD_THROTTLE:
1287 return tool->throttle(tool, event, sample, machine);
1288 case PERF_RECORD_UNTHROTTLE:
1289 return tool->unthrottle(tool, event, sample, machine);
1290 case PERF_RECORD_AUX:
1291 if (tool->aux == perf_event__process_aux) {
1292 if (event->aux.flags & PERF_AUX_FLAG_TRUNCATED)
1293 evlist->stats.total_aux_lost += 1;
1294 if (event->aux.flags & PERF_AUX_FLAG_PARTIAL)
1295 evlist->stats.total_aux_partial += 1;
1297 return tool->aux(tool, event, sample, machine);
1298 case PERF_RECORD_ITRACE_START:
1299 return tool->itrace_start(tool, event, sample, machine);
1300 case PERF_RECORD_SWITCH:
1301 case PERF_RECORD_SWITCH_CPU_WIDE:
1302 return tool->context_switch(tool, event, sample, machine);
1303 default:
1304 ++evlist->stats.nr_unknown_events;
1305 return -1;
1309 static int perf_session__deliver_event(struct perf_session *session,
1310 union perf_event *event,
1311 struct perf_tool *tool,
1312 u64 file_offset)
1314 struct perf_sample sample;
1315 int ret;
1317 ret = perf_evlist__parse_sample(session->evlist, event, &sample);
1318 if (ret) {
1319 pr_err("Can't parse sample, err = %d\n", ret);
1320 return ret;
1323 ret = auxtrace__process_event(session, event, &sample, tool);
1324 if (ret < 0)
1325 return ret;
1326 if (ret > 0)
1327 return 0;
1329 return machines__deliver_event(&session->machines, session->evlist,
1330 event, &sample, tool, file_offset);
1333 static s64 perf_session__process_user_event(struct perf_session *session,
1334 union perf_event *event,
1335 u64 file_offset)
1337 struct ordered_events *oe = &session->ordered_events;
1338 struct perf_tool *tool = session->tool;
1339 struct perf_sample sample = { .time = 0, };
1340 int fd = perf_data__fd(session->data);
1341 int err;
1343 dump_event(session->evlist, event, file_offset, &sample);
1345 /* These events are processed right away */
1346 switch (event->header.type) {
1347 case PERF_RECORD_HEADER_ATTR:
1348 err = tool->attr(tool, event, &session->evlist);
1349 if (err == 0) {
1350 perf_session__set_id_hdr_size(session);
1351 perf_session__set_comm_exec(session);
1353 return err;
1354 case PERF_RECORD_EVENT_UPDATE:
1355 return tool->event_update(tool, event, &session->evlist);
1356 case PERF_RECORD_HEADER_EVENT_TYPE:
1358 * Depreceated, but we need to handle it for sake
1359 * of old data files create in pipe mode.
1361 return 0;
1362 case PERF_RECORD_HEADER_TRACING_DATA:
1363 /* setup for reading amidst mmap */
1364 lseek(fd, file_offset, SEEK_SET);
1365 return tool->tracing_data(session, event);
1366 case PERF_RECORD_HEADER_BUILD_ID:
1367 return tool->build_id(session, event);
1368 case PERF_RECORD_FINISHED_ROUND:
1369 return tool->finished_round(tool, event, oe);
1370 case PERF_RECORD_ID_INDEX:
1371 return tool->id_index(session, event);
1372 case PERF_RECORD_AUXTRACE_INFO:
1373 return tool->auxtrace_info(session, event);
1374 case PERF_RECORD_AUXTRACE:
1375 /* setup for reading amidst mmap */
1376 lseek(fd, file_offset + event->header.size, SEEK_SET);
1377 return tool->auxtrace(session, event);
1378 case PERF_RECORD_AUXTRACE_ERROR:
1379 perf_session__auxtrace_error_inc(session, event);
1380 return tool->auxtrace_error(session, event);
1381 case PERF_RECORD_THREAD_MAP:
1382 return tool->thread_map(session, event);
1383 case PERF_RECORD_CPU_MAP:
1384 return tool->cpu_map(session, event);
1385 case PERF_RECORD_STAT_CONFIG:
1386 return tool->stat_config(session, event);
1387 case PERF_RECORD_STAT:
1388 return tool->stat(session, event);
1389 case PERF_RECORD_STAT_ROUND:
1390 return tool->stat_round(session, event);
1391 case PERF_RECORD_TIME_CONV:
1392 session->time_conv = event->time_conv;
1393 return tool->time_conv(session, event);
1394 case PERF_RECORD_HEADER_FEATURE:
1395 return tool->feature(session, event);
1396 default:
1397 return -EINVAL;
1401 int perf_session__deliver_synth_event(struct perf_session *session,
1402 union perf_event *event,
1403 struct perf_sample *sample)
1405 struct perf_evlist *evlist = session->evlist;
1406 struct perf_tool *tool = session->tool;
1408 events_stats__inc(&evlist->stats, event->header.type);
1410 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
1411 return perf_session__process_user_event(session, event, 0);
1413 return machines__deliver_event(&session->machines, evlist, event, sample, tool, 0);
1416 static void event_swap(union perf_event *event, bool sample_id_all)
1418 perf_event__swap_op swap;
1420 swap = perf_event__swap_ops[event->header.type];
1421 if (swap)
1422 swap(event, sample_id_all);
1425 int perf_session__peek_event(struct perf_session *session, off_t file_offset,
1426 void *buf, size_t buf_sz,
1427 union perf_event **event_ptr,
1428 struct perf_sample *sample)
1430 union perf_event *event;
1431 size_t hdr_sz, rest;
1432 int fd;
1434 if (session->one_mmap && !session->header.needs_swap) {
1435 event = file_offset - session->one_mmap_offset +
1436 session->one_mmap_addr;
1437 goto out_parse_sample;
1440 if (perf_data__is_pipe(session->data))
1441 return -1;
1443 fd = perf_data__fd(session->data);
1444 hdr_sz = sizeof(struct perf_event_header);
1446 if (buf_sz < hdr_sz)
1447 return -1;
1449 if (lseek(fd, file_offset, SEEK_SET) == (off_t)-1 ||
1450 readn(fd, buf, hdr_sz) != (ssize_t)hdr_sz)
1451 return -1;
1453 event = (union perf_event *)buf;
1455 if (session->header.needs_swap)
1456 perf_event_header__bswap(&event->header);
1458 if (event->header.size < hdr_sz || event->header.size > buf_sz)
1459 return -1;
1461 rest = event->header.size - hdr_sz;
1463 if (readn(fd, buf, rest) != (ssize_t)rest)
1464 return -1;
1466 if (session->header.needs_swap)
1467 event_swap(event, perf_evlist__sample_id_all(session->evlist));
1469 out_parse_sample:
1471 if (sample && event->header.type < PERF_RECORD_USER_TYPE_START &&
1472 perf_evlist__parse_sample(session->evlist, event, sample))
1473 return -1;
1475 *event_ptr = event;
1477 return 0;
1480 static s64 perf_session__process_event(struct perf_session *session,
1481 union perf_event *event, u64 file_offset)
1483 struct perf_evlist *evlist = session->evlist;
1484 struct perf_tool *tool = session->tool;
1485 int ret;
1487 if (session->header.needs_swap)
1488 event_swap(event, perf_evlist__sample_id_all(evlist));
1490 if (event->header.type >= PERF_RECORD_HEADER_MAX)
1491 return -EINVAL;
1493 events_stats__inc(&evlist->stats, event->header.type);
1495 if (event->header.type >= PERF_RECORD_USER_TYPE_START)
1496 return perf_session__process_user_event(session, event, file_offset);
1498 if (tool->ordered_events) {
1499 u64 timestamp = -1ULL;
1501 ret = perf_evlist__parse_sample_timestamp(evlist, event, &timestamp);
1502 if (ret && ret != -1)
1503 return ret;
1505 ret = perf_session__queue_event(session, event, timestamp, file_offset);
1506 if (ret != -ETIME)
1507 return ret;
1510 return perf_session__deliver_event(session, event, tool, file_offset);
1513 void perf_event_header__bswap(struct perf_event_header *hdr)
1515 hdr->type = bswap_32(hdr->type);
1516 hdr->misc = bswap_16(hdr->misc);
1517 hdr->size = bswap_16(hdr->size);
1520 struct thread *perf_session__findnew(struct perf_session *session, pid_t pid)
1522 return machine__findnew_thread(&session->machines.host, -1, pid);
1525 int perf_session__register_idle_thread(struct perf_session *session)
1527 struct thread *thread;
1528 int err = 0;
1530 thread = machine__findnew_thread(&session->machines.host, 0, 0);
1531 if (thread == NULL || thread__set_comm(thread, "swapper", 0)) {
1532 pr_err("problem inserting idle task.\n");
1533 err = -1;
1536 if (thread == NULL || thread__set_namespaces(thread, 0, NULL)) {
1537 pr_err("problem inserting idle task.\n");
1538 err = -1;
1541 /* machine__findnew_thread() got the thread, so put it */
1542 thread__put(thread);
1543 return err;
1546 static void
1547 perf_session__warn_order(const struct perf_session *session)
1549 const struct ordered_events *oe = &session->ordered_events;
1550 struct perf_evsel *evsel;
1551 bool should_warn = true;
1553 evlist__for_each_entry(session->evlist, evsel) {
1554 if (evsel->attr.write_backward)
1555 should_warn = false;
1558 if (!should_warn)
1559 return;
1560 if (oe->nr_unordered_events != 0)
1561 ui__warning("%u out of order events recorded.\n", oe->nr_unordered_events);
1564 static void perf_session__warn_about_errors(const struct perf_session *session)
1566 const struct events_stats *stats = &session->evlist->stats;
1568 if (session->tool->lost == perf_event__process_lost &&
1569 stats->nr_events[PERF_RECORD_LOST] != 0) {
1570 ui__warning("Processed %d events and lost %d chunks!\n\n"
1571 "Check IO/CPU overload!\n\n",
1572 stats->nr_events[0],
1573 stats->nr_events[PERF_RECORD_LOST]);
1576 if (session->tool->lost_samples == perf_event__process_lost_samples) {
1577 double drop_rate;
1579 drop_rate = (double)stats->total_lost_samples /
1580 (double) (stats->nr_events[PERF_RECORD_SAMPLE] + stats->total_lost_samples);
1581 if (drop_rate > 0.05) {
1582 ui__warning("Processed %" PRIu64 " samples and lost %3.2f%%!\n\n",
1583 stats->nr_events[PERF_RECORD_SAMPLE] + stats->total_lost_samples,
1584 drop_rate * 100.0);
1588 if (session->tool->aux == perf_event__process_aux &&
1589 stats->total_aux_lost != 0) {
1590 ui__warning("AUX data lost %" PRIu64 " times out of %u!\n\n",
1591 stats->total_aux_lost,
1592 stats->nr_events[PERF_RECORD_AUX]);
1595 if (session->tool->aux == perf_event__process_aux &&
1596 stats->total_aux_partial != 0) {
1597 bool vmm_exclusive = false;
1599 (void)sysfs__read_bool("module/kvm_intel/parameters/vmm_exclusive",
1600 &vmm_exclusive);
1602 ui__warning("AUX data had gaps in it %" PRIu64 " times out of %u!\n\n"
1603 "Are you running a KVM guest in the background?%s\n\n",
1604 stats->total_aux_partial,
1605 stats->nr_events[PERF_RECORD_AUX],
1606 vmm_exclusive ?
1607 "\nReloading kvm_intel module with vmm_exclusive=0\n"
1608 "will reduce the gaps to only guest's timeslices." :
1609 "");
1612 if (stats->nr_unknown_events != 0) {
1613 ui__warning("Found %u unknown events!\n\n"
1614 "Is this an older tool processing a perf.data "
1615 "file generated by a more recent tool?\n\n"
1616 "If that is not the case, consider "
1617 "reporting to linux-kernel@vger.kernel.org.\n\n",
1618 stats->nr_unknown_events);
1621 if (stats->nr_unknown_id != 0) {
1622 ui__warning("%u samples with id not present in the header\n",
1623 stats->nr_unknown_id);
1626 if (stats->nr_invalid_chains != 0) {
1627 ui__warning("Found invalid callchains!\n\n"
1628 "%u out of %u events were discarded for this reason.\n\n"
1629 "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
1630 stats->nr_invalid_chains,
1631 stats->nr_events[PERF_RECORD_SAMPLE]);
1634 if (stats->nr_unprocessable_samples != 0) {
1635 ui__warning("%u unprocessable samples recorded.\n"
1636 "Do you have a KVM guest running and not using 'perf kvm'?\n",
1637 stats->nr_unprocessable_samples);
1640 perf_session__warn_order(session);
1642 events_stats__auxtrace_error_warn(stats);
1644 if (stats->nr_proc_map_timeout != 0) {
1645 ui__warning("%d map information files for pre-existing threads were\n"
1646 "not processed, if there are samples for addresses they\n"
1647 "will not be resolved, you may find out which are these\n"
1648 "threads by running with -v and redirecting the output\n"
1649 "to a file.\n"
1650 "The time limit to process proc map is too short?\n"
1651 "Increase it by --proc-map-timeout\n",
1652 stats->nr_proc_map_timeout);
1656 static int perf_session__flush_thread_stack(struct thread *thread,
1657 void *p __maybe_unused)
1659 return thread_stack__flush(thread);
1662 static int perf_session__flush_thread_stacks(struct perf_session *session)
1664 return machines__for_each_thread(&session->machines,
1665 perf_session__flush_thread_stack,
1666 NULL);
1669 volatile int session_done;
1671 static int __perf_session__process_pipe_events(struct perf_session *session)
1673 struct ordered_events *oe = &session->ordered_events;
1674 struct perf_tool *tool = session->tool;
1675 int fd = perf_data__fd(session->data);
1676 union perf_event *event;
1677 uint32_t size, cur_size = 0;
1678 void *buf = NULL;
1679 s64 skip = 0;
1680 u64 head;
1681 ssize_t err;
1682 void *p;
1684 perf_tool__fill_defaults(tool);
1686 head = 0;
1687 cur_size = sizeof(union perf_event);
1689 buf = malloc(cur_size);
1690 if (!buf)
1691 return -errno;
1692 ordered_events__set_copy_on_queue(oe, true);
1693 more:
1694 event = buf;
1695 err = readn(fd, event, sizeof(struct perf_event_header));
1696 if (err <= 0) {
1697 if (err == 0)
1698 goto done;
1700 pr_err("failed to read event header\n");
1701 goto out_err;
1704 if (session->header.needs_swap)
1705 perf_event_header__bswap(&event->header);
1707 size = event->header.size;
1708 if (size < sizeof(struct perf_event_header)) {
1709 pr_err("bad event header size\n");
1710 goto out_err;
1713 if (size > cur_size) {
1714 void *new = realloc(buf, size);
1715 if (!new) {
1716 pr_err("failed to allocate memory to read event\n");
1717 goto out_err;
1719 buf = new;
1720 cur_size = size;
1721 event = buf;
1723 p = event;
1724 p += sizeof(struct perf_event_header);
1726 if (size - sizeof(struct perf_event_header)) {
1727 err = readn(fd, p, size - sizeof(struct perf_event_header));
1728 if (err <= 0) {
1729 if (err == 0) {
1730 pr_err("unexpected end of event stream\n");
1731 goto done;
1734 pr_err("failed to read event data\n");
1735 goto out_err;
1739 if ((skip = perf_session__process_event(session, event, head)) < 0) {
1740 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
1741 head, event->header.size, event->header.type);
1742 err = -EINVAL;
1743 goto out_err;
1746 head += size;
1748 if (skip > 0)
1749 head += skip;
1751 if (!session_done())
1752 goto more;
1753 done:
1754 /* do the final flush for ordered samples */
1755 err = ordered_events__flush(oe, OE_FLUSH__FINAL);
1756 if (err)
1757 goto out_err;
1758 err = auxtrace__flush_events(session, tool);
1759 if (err)
1760 goto out_err;
1761 err = perf_session__flush_thread_stacks(session);
1762 out_err:
1763 free(buf);
1764 if (!tool->no_warn)
1765 perf_session__warn_about_errors(session);
1766 ordered_events__free(&session->ordered_events);
1767 auxtrace__free_events(session);
1768 return err;
1771 static union perf_event *
1772 fetch_mmaped_event(struct perf_session *session,
1773 u64 head, size_t mmap_size, char *buf)
1775 union perf_event *event;
1778 * Ensure we have enough space remaining to read
1779 * the size of the event in the headers.
1781 if (head + sizeof(event->header) > mmap_size)
1782 return NULL;
1784 event = (union perf_event *)(buf + head);
1786 if (session->header.needs_swap)
1787 perf_event_header__bswap(&event->header);
1789 if (head + event->header.size > mmap_size) {
1790 /* We're not fetching the event so swap back again */
1791 if (session->header.needs_swap)
1792 perf_event_header__bswap(&event->header);
1793 return NULL;
1796 return event;
1800 * On 64bit we can mmap the data file in one go. No need for tiny mmap
1801 * slices. On 32bit we use 32MB.
1803 #if BITS_PER_LONG == 64
1804 #define MMAP_SIZE ULLONG_MAX
1805 #define NUM_MMAPS 1
1806 #else
1807 #define MMAP_SIZE (32 * 1024 * 1024ULL)
1808 #define NUM_MMAPS 128
1809 #endif
1811 static int __perf_session__process_events(struct perf_session *session,
1812 u64 data_offset, u64 data_size,
1813 u64 file_size)
1815 struct ordered_events *oe = &session->ordered_events;
1816 struct perf_tool *tool = session->tool;
1817 int fd = perf_data__fd(session->data);
1818 u64 head, page_offset, file_offset, file_pos, size;
1819 int err, mmap_prot, mmap_flags, map_idx = 0;
1820 size_t mmap_size;
1821 char *buf, *mmaps[NUM_MMAPS];
1822 union perf_event *event;
1823 struct ui_progress prog;
1824 s64 skip;
1826 perf_tool__fill_defaults(tool);
1828 page_offset = page_size * (data_offset / page_size);
1829 file_offset = page_offset;
1830 head = data_offset - page_offset;
1832 if (data_size == 0)
1833 goto out;
1835 if (data_offset + data_size < file_size)
1836 file_size = data_offset + data_size;
1838 ui_progress__init_size(&prog, file_size, "Processing events...");
1840 mmap_size = MMAP_SIZE;
1841 if (mmap_size > file_size) {
1842 mmap_size = file_size;
1843 session->one_mmap = true;
1846 memset(mmaps, 0, sizeof(mmaps));
1848 mmap_prot = PROT_READ;
1849 mmap_flags = MAP_SHARED;
1851 if (session->header.needs_swap) {
1852 mmap_prot |= PROT_WRITE;
1853 mmap_flags = MAP_PRIVATE;
1855 remap:
1856 buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, fd,
1857 file_offset);
1858 if (buf == MAP_FAILED) {
1859 pr_err("failed to mmap file\n");
1860 err = -errno;
1861 goto out_err;
1863 mmaps[map_idx] = buf;
1864 map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
1865 file_pos = file_offset + head;
1866 if (session->one_mmap) {
1867 session->one_mmap_addr = buf;
1868 session->one_mmap_offset = file_offset;
1871 more:
1872 event = fetch_mmaped_event(session, head, mmap_size, buf);
1873 if (!event) {
1874 if (mmaps[map_idx]) {
1875 munmap(mmaps[map_idx], mmap_size);
1876 mmaps[map_idx] = NULL;
1879 page_offset = page_size * (head / page_size);
1880 file_offset += page_offset;
1881 head -= page_offset;
1882 goto remap;
1885 size = event->header.size;
1887 if (size < sizeof(struct perf_event_header) ||
1888 (skip = perf_session__process_event(session, event, file_pos)) < 0) {
1889 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
1890 file_offset + head, event->header.size,
1891 event->header.type);
1892 err = -EINVAL;
1893 goto out_err;
1896 if (skip)
1897 size += skip;
1899 head += size;
1900 file_pos += size;
1902 ui_progress__update(&prog, size);
1904 if (session_done())
1905 goto out;
1907 if (file_pos < file_size)
1908 goto more;
1910 out:
1911 /* do the final flush for ordered samples */
1912 err = ordered_events__flush(oe, OE_FLUSH__FINAL);
1913 if (err)
1914 goto out_err;
1915 err = auxtrace__flush_events(session, tool);
1916 if (err)
1917 goto out_err;
1918 err = perf_session__flush_thread_stacks(session);
1919 out_err:
1920 ui_progress__finish();
1921 if (!tool->no_warn)
1922 perf_session__warn_about_errors(session);
1924 * We may switching perf.data output, make ordered_events
1925 * reusable.
1927 ordered_events__reinit(&session->ordered_events);
1928 auxtrace__free_events(session);
1929 session->one_mmap = false;
1930 return err;
1933 int perf_session__process_events(struct perf_session *session)
1935 u64 size = perf_data__size(session->data);
1936 int err;
1938 if (perf_session__register_idle_thread(session) < 0)
1939 return -ENOMEM;
1941 if (!perf_data__is_pipe(session->data))
1942 err = __perf_session__process_events(session,
1943 session->header.data_offset,
1944 session->header.data_size, size);
1945 else
1946 err = __perf_session__process_pipe_events(session);
1948 return err;
1951 bool perf_session__has_traces(struct perf_session *session, const char *msg)
1953 struct perf_evsel *evsel;
1955 evlist__for_each_entry(session->evlist, evsel) {
1956 if (evsel->attr.type == PERF_TYPE_TRACEPOINT)
1957 return true;
1960 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
1961 return false;
1964 int map__set_kallsyms_ref_reloc_sym(struct map *map, const char *symbol_name, u64 addr)
1966 char *bracket;
1967 struct ref_reloc_sym *ref;
1968 struct kmap *kmap;
1970 ref = zalloc(sizeof(struct ref_reloc_sym));
1971 if (ref == NULL)
1972 return -ENOMEM;
1974 ref->name = strdup(symbol_name);
1975 if (ref->name == NULL) {
1976 free(ref);
1977 return -ENOMEM;
1980 bracket = strchr(ref->name, ']');
1981 if (bracket)
1982 *bracket = '\0';
1984 ref->addr = addr;
1986 kmap = map__kmap(map);
1987 if (kmap)
1988 kmap->ref_reloc_sym = ref;
1990 return 0;
1993 size_t perf_session__fprintf_dsos(struct perf_session *session, FILE *fp)
1995 return machines__fprintf_dsos(&session->machines, fp);
1998 size_t perf_session__fprintf_dsos_buildid(struct perf_session *session, FILE *fp,
1999 bool (skip)(struct dso *dso, int parm), int parm)
2001 return machines__fprintf_dsos_buildid(&session->machines, fp, skip, parm);
2004 size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp)
2006 size_t ret;
2007 const char *msg = "";
2009 if (perf_header__has_feat(&session->header, HEADER_AUXTRACE))
2010 msg = " (excludes AUX area (e.g. instruction trace) decoded / synthesized events)";
2012 ret = fprintf(fp, "\nAggregated stats:%s\n", msg);
2014 ret += events_stats__fprintf(&session->evlist->stats, fp);
2015 return ret;
2018 size_t perf_session__fprintf(struct perf_session *session, FILE *fp)
2021 * FIXME: Here we have to actually print all the machines in this
2022 * session, not just the host...
2024 return machine__fprintf(&session->machines.host, fp);
2027 struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
2028 unsigned int type)
2030 struct perf_evsel *pos;
2032 evlist__for_each_entry(session->evlist, pos) {
2033 if (pos->attr.type == type)
2034 return pos;
2036 return NULL;
2039 int perf_session__cpu_bitmap(struct perf_session *session,
2040 const char *cpu_list, unsigned long *cpu_bitmap)
2042 int i, err = -1;
2043 struct cpu_map *map;
2045 for (i = 0; i < PERF_TYPE_MAX; ++i) {
2046 struct perf_evsel *evsel;
2048 evsel = perf_session__find_first_evtype(session, i);
2049 if (!evsel)
2050 continue;
2052 if (!(evsel->attr.sample_type & PERF_SAMPLE_CPU)) {
2053 pr_err("File does not contain CPU events. "
2054 "Remove -C option to proceed.\n");
2055 return -1;
2059 map = cpu_map__new(cpu_list);
2060 if (map == NULL) {
2061 pr_err("Invalid cpu_list\n");
2062 return -1;
2065 for (i = 0; i < map->nr; i++) {
2066 int cpu = map->map[i];
2068 if (cpu >= MAX_NR_CPUS) {
2069 pr_err("Requested CPU %d too large. "
2070 "Consider raising MAX_NR_CPUS\n", cpu);
2071 goto out_delete_map;
2074 set_bit(cpu, cpu_bitmap);
2077 err = 0;
2079 out_delete_map:
2080 cpu_map__put(map);
2081 return err;
2084 void perf_session__fprintf_info(struct perf_session *session, FILE *fp,
2085 bool full)
2087 if (session == NULL || fp == NULL)
2088 return;
2090 fprintf(fp, "# ========\n");
2091 perf_header__fprintf_info(session, fp, full);
2092 fprintf(fp, "# ========\n#\n");
2096 int __perf_session__set_tracepoints_handlers(struct perf_session *session,
2097 const struct perf_evsel_str_handler *assocs,
2098 size_t nr_assocs)
2100 struct perf_evsel *evsel;
2101 size_t i;
2102 int err;
2104 for (i = 0; i < nr_assocs; i++) {
2106 * Adding a handler for an event not in the session,
2107 * just ignore it.
2109 evsel = perf_evlist__find_tracepoint_by_name(session->evlist, assocs[i].name);
2110 if (evsel == NULL)
2111 continue;
2113 err = -EEXIST;
2114 if (evsel->handler != NULL)
2115 goto out;
2116 evsel->handler = assocs[i].handler;
2119 err = 0;
2120 out:
2121 return err;
2124 int perf_event__process_id_index(struct perf_session *session,
2125 union perf_event *event)
2127 struct perf_evlist *evlist = session->evlist;
2128 struct id_index_event *ie = &event->id_index;
2129 size_t i, nr, max_nr;
2131 max_nr = (ie->header.size - sizeof(struct id_index_event)) /
2132 sizeof(struct id_index_entry);
2133 nr = ie->nr;
2134 if (nr > max_nr)
2135 return -EINVAL;
2137 if (dump_trace)
2138 fprintf(stdout, " nr: %zu\n", nr);
2140 for (i = 0; i < nr; i++) {
2141 struct id_index_entry *e = &ie->entries[i];
2142 struct perf_sample_id *sid;
2144 if (dump_trace) {
2145 fprintf(stdout, " ... id: %"PRIu64, e->id);
2146 fprintf(stdout, " idx: %"PRIu64, e->idx);
2147 fprintf(stdout, " cpu: %"PRId64, e->cpu);
2148 fprintf(stdout, " tid: %"PRId64"\n", e->tid);
2151 sid = perf_evlist__id2sid(evlist, e->id);
2152 if (!sid)
2153 return -ENOENT;
2154 sid->idx = e->idx;
2155 sid->cpu = e->cpu;
2156 sid->tid = e->tid;
2158 return 0;
2161 int perf_event__synthesize_id_index(struct perf_tool *tool,
2162 perf_event__handler_t process,
2163 struct perf_evlist *evlist,
2164 struct machine *machine)
2166 union perf_event *ev;
2167 struct perf_evsel *evsel;
2168 size_t nr = 0, i = 0, sz, max_nr, n;
2169 int err;
2171 pr_debug2("Synthesizing id index\n");
2173 max_nr = (UINT16_MAX - sizeof(struct id_index_event)) /
2174 sizeof(struct id_index_entry);
2176 evlist__for_each_entry(evlist, evsel)
2177 nr += evsel->ids;
2179 n = nr > max_nr ? max_nr : nr;
2180 sz = sizeof(struct id_index_event) + n * sizeof(struct id_index_entry);
2181 ev = zalloc(sz);
2182 if (!ev)
2183 return -ENOMEM;
2185 ev->id_index.header.type = PERF_RECORD_ID_INDEX;
2186 ev->id_index.header.size = sz;
2187 ev->id_index.nr = n;
2189 evlist__for_each_entry(evlist, evsel) {
2190 u32 j;
2192 for (j = 0; j < evsel->ids; j++) {
2193 struct id_index_entry *e;
2194 struct perf_sample_id *sid;
2196 if (i >= n) {
2197 err = process(tool, ev, NULL, machine);
2198 if (err)
2199 goto out_err;
2200 nr -= n;
2201 i = 0;
2204 e = &ev->id_index.entries[i++];
2206 e->id = evsel->id[j];
2208 sid = perf_evlist__id2sid(evlist, e->id);
2209 if (!sid) {
2210 free(ev);
2211 return -ENOENT;
2214 e->idx = sid->idx;
2215 e->cpu = sid->cpu;
2216 e->tid = sid->tid;
2220 sz = sizeof(struct id_index_event) + nr * sizeof(struct id_index_entry);
2221 ev->id_index.header.size = sz;
2222 ev->id_index.nr = nr;
2224 err = process(tool, ev, NULL, machine);
2225 out_err:
2226 free(ev);
2228 return err;