xref: /openbmc/linux/tools/perf/util/session.c (revision a93fbb00)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <errno.h>
3 #include <inttypes.h>
4 #include <linux/err.h>
5 #include <linux/kernel.h>
6 #include <linux/zalloc.h>
7 #include <api/fs/fs.h>
8 
9 #include <byteswap.h>
10 #include <unistd.h>
11 #include <sys/types.h>
12 #include <sys/mman.h>
13 #include <perf/cpumap.h>
14 
15 #include "map_symbol.h"
16 #include "branch.h"
17 #include "debug.h"
18 #include "env.h"
19 #include "evlist.h"
20 #include "evsel.h"
21 #include "memswap.h"
22 #include "map.h"
23 #include "symbol.h"
24 #include "session.h"
25 #include "tool.h"
26 #include "perf_regs.h"
27 #include "asm/bug.h"
28 #include "auxtrace.h"
29 #include "thread.h"
30 #include "thread-stack.h"
31 #include "sample-raw.h"
32 #include "stat.h"
33 #include "tsc.h"
34 #include "ui/progress.h"
35 #include "../perf.h"
36 #include "arch/common.h"
37 #include "units.h"
38 #include <internal/lib.h>
39 
40 #ifdef HAVE_ZSTD_SUPPORT
41 static int perf_session__process_compressed_event(struct perf_session *session,
42 						  union perf_event *event, u64 file_offset,
43 						  const char *file_path)
44 {
45 	void *src;
46 	size_t decomp_size, src_size;
47 	u64 decomp_last_rem = 0;
48 	size_t mmap_len, decomp_len = session->header.env.comp_mmap_len;
49 	struct decomp *decomp, *decomp_last = session->active_decomp->decomp_last;
50 
51 	if (decomp_last) {
52 		decomp_last_rem = decomp_last->size - decomp_last->head;
53 		decomp_len += decomp_last_rem;
54 	}
55 
56 	mmap_len = sizeof(struct decomp) + decomp_len;
57 	decomp = mmap(NULL, mmap_len, PROT_READ|PROT_WRITE,
58 		      MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
59 	if (decomp == MAP_FAILED) {
60 		pr_err("Couldn't allocate memory for decompression\n");
61 		return -1;
62 	}
63 
64 	decomp->file_pos = file_offset;
65 	decomp->file_path = file_path;
66 	decomp->mmap_len = mmap_len;
67 	decomp->head = 0;
68 
69 	if (decomp_last_rem) {
70 		memcpy(decomp->data, &(decomp_last->data[decomp_last->head]), decomp_last_rem);
71 		decomp->size = decomp_last_rem;
72 	}
73 
74 	src = (void *)event + sizeof(struct perf_record_compressed);
75 	src_size = event->pack.header.size - sizeof(struct perf_record_compressed);
76 
77 	decomp_size = zstd_decompress_stream(session->active_decomp->zstd_decomp, src, src_size,
78 				&(decomp->data[decomp_last_rem]), decomp_len - decomp_last_rem);
79 	if (!decomp_size) {
80 		munmap(decomp, mmap_len);
81 		pr_err("Couldn't decompress data\n");
82 		return -1;
83 	}
84 
85 	decomp->size += decomp_size;
86 
87 	if (session->active_decomp->decomp == NULL)
88 		session->active_decomp->decomp = decomp;
89 	else
90 		session->active_decomp->decomp_last->next = decomp;
91 
92 	session->active_decomp->decomp_last = decomp;
93 
94 	pr_debug("decomp (B): %zd to %zd\n", src_size, decomp_size);
95 
96 	return 0;
97 }
98 #else /* !HAVE_ZSTD_SUPPORT */
99 #define perf_session__process_compressed_event perf_session__process_compressed_event_stub
100 #endif
101 
102 static int perf_session__deliver_event(struct perf_session *session,
103 				       union perf_event *event,
104 				       struct perf_tool *tool,
105 				       u64 file_offset,
106 				       const char *file_path);
107 
108 static int perf_session__open(struct perf_session *session, int repipe_fd)
109 {
110 	struct perf_data *data = session->data;
111 
112 	if (perf_session__read_header(session, repipe_fd) < 0) {
113 		pr_err("incompatible file format (rerun with -v to learn more)\n");
114 		return -1;
115 	}
116 
117 	if (perf_data__is_pipe(data))
118 		return 0;
119 
120 	if (perf_header__has_feat(&session->header, HEADER_STAT))
121 		return 0;
122 
123 	if (!evlist__valid_sample_type(session->evlist)) {
124 		pr_err("non matching sample_type\n");
125 		return -1;
126 	}
127 
128 	if (!evlist__valid_sample_id_all(session->evlist)) {
129 		pr_err("non matching sample_id_all\n");
130 		return -1;
131 	}
132 
133 	if (!evlist__valid_read_format(session->evlist)) {
134 		pr_err("non matching read_format\n");
135 		return -1;
136 	}
137 
138 	return 0;
139 }
140 
141 void perf_session__set_id_hdr_size(struct perf_session *session)
142 {
143 	u16 id_hdr_size = evlist__id_hdr_size(session->evlist);
144 
145 	machines__set_id_hdr_size(&session->machines, id_hdr_size);
146 }
147 
148 int perf_session__create_kernel_maps(struct perf_session *session)
149 {
150 	int ret = machine__create_kernel_maps(&session->machines.host);
151 
152 	if (ret >= 0)
153 		ret = machines__create_guest_kernel_maps(&session->machines);
154 	return ret;
155 }
156 
157 static void perf_session__destroy_kernel_maps(struct perf_session *session)
158 {
159 	machines__destroy_kernel_maps(&session->machines);
160 }
161 
162 static bool perf_session__has_comm_exec(struct perf_session *session)
163 {
164 	struct evsel *evsel;
165 
166 	evlist__for_each_entry(session->evlist, evsel) {
167 		if (evsel->core.attr.comm_exec)
168 			return true;
169 	}
170 
171 	return false;
172 }
173 
174 static void perf_session__set_comm_exec(struct perf_session *session)
175 {
176 	bool comm_exec = perf_session__has_comm_exec(session);
177 
178 	machines__set_comm_exec(&session->machines, comm_exec);
179 }
180 
181 static int ordered_events__deliver_event(struct ordered_events *oe,
182 					 struct ordered_event *event)
183 {
184 	struct perf_session *session = container_of(oe, struct perf_session,
185 						    ordered_events);
186 
187 	return perf_session__deliver_event(session, event->event,
188 					   session->tool, event->file_offset,
189 					   event->file_path);
190 }
191 
192 struct perf_session *__perf_session__new(struct perf_data *data,
193 					 bool repipe, int repipe_fd,
194 					 struct perf_tool *tool)
195 {
196 	int ret = -ENOMEM;
197 	struct perf_session *session = zalloc(sizeof(*session));
198 
199 	if (!session)
200 		goto out;
201 
202 	session->repipe = repipe;
203 	session->tool   = tool;
204 	session->decomp_data.zstd_decomp = &session->zstd_data;
205 	session->active_decomp = &session->decomp_data;
206 	INIT_LIST_HEAD(&session->auxtrace_index);
207 	machines__init(&session->machines);
208 	ordered_events__init(&session->ordered_events,
209 			     ordered_events__deliver_event, NULL);
210 
211 	perf_env__init(&session->header.env);
212 	if (data) {
213 		ret = perf_data__open(data);
214 		if (ret < 0)
215 			goto out_delete;
216 
217 		session->data = data;
218 
219 		if (perf_data__is_read(data)) {
220 			ret = perf_session__open(session, repipe_fd);
221 			if (ret < 0)
222 				goto out_delete;
223 
224 			/*
225 			 * set session attributes that are present in perf.data
226 			 * but not in pipe-mode.
227 			 */
228 			if (!data->is_pipe) {
229 				perf_session__set_id_hdr_size(session);
230 				perf_session__set_comm_exec(session);
231 			}
232 
233 			evlist__init_trace_event_sample_raw(session->evlist);
234 
235 			/* Open the directory data. */
236 			if (data->is_dir) {
237 				ret = perf_data__open_dir(data);
238 				if (ret)
239 					goto out_delete;
240 			}
241 
242 			if (!symbol_conf.kallsyms_name &&
243 			    !symbol_conf.vmlinux_name)
244 				symbol_conf.kallsyms_name = perf_data__kallsyms_name(data);
245 		}
246 	} else  {
247 		session->machines.host.env = &perf_env;
248 	}
249 
250 	session->machines.host.single_address_space =
251 		perf_env__single_address_space(session->machines.host.env);
252 
253 	if (!data || perf_data__is_write(data)) {
254 		/*
255 		 * In O_RDONLY mode this will be performed when reading the
256 		 * kernel MMAP event, in perf_event__process_mmap().
257 		 */
258 		if (perf_session__create_kernel_maps(session) < 0)
259 			pr_warning("Cannot read kernel map\n");
260 	}
261 
262 	/*
263 	 * In pipe-mode, evlist is empty until PERF_RECORD_HEADER_ATTR is
264 	 * processed, so evlist__sample_id_all is not meaningful here.
265 	 */
266 	if ((!data || !data->is_pipe) && tool && tool->ordering_requires_timestamps &&
267 	    tool->ordered_events && !evlist__sample_id_all(session->evlist)) {
268 		dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n");
269 		tool->ordered_events = false;
270 	}
271 
272 	return session;
273 
274  out_delete:
275 	perf_session__delete(session);
276  out:
277 	return ERR_PTR(ret);
278 }
279 
280 static void perf_session__delete_threads(struct perf_session *session)
281 {
282 	machine__delete_threads(&session->machines.host);
283 }
284 
285 static void perf_decomp__release_events(struct decomp *next)
286 {
287 	struct decomp *decomp;
288 	size_t mmap_len;
289 
290 	do {
291 		decomp = next;
292 		if (decomp == NULL)
293 			break;
294 		next = decomp->next;
295 		mmap_len = decomp->mmap_len;
296 		munmap(decomp, mmap_len);
297 	} while (1);
298 }
299 
300 void perf_session__delete(struct perf_session *session)
301 {
302 	if (session == NULL)
303 		return;
304 	auxtrace__free(session);
305 	auxtrace_index__free(&session->auxtrace_index);
306 	perf_session__destroy_kernel_maps(session);
307 	perf_session__delete_threads(session);
308 	perf_decomp__release_events(session->decomp_data.decomp);
309 	perf_env__exit(&session->header.env);
310 	machines__exit(&session->machines);
311 	if (session->data) {
312 		if (perf_data__is_read(session->data))
313 			evlist__delete(session->evlist);
314 		perf_data__close(session->data);
315 	}
316 	trace_event__cleanup(&session->tevent);
317 	free(session);
318 }
319 
320 static int process_event_synth_tracing_data_stub(struct perf_session *session
321 						 __maybe_unused,
322 						 union perf_event *event
323 						 __maybe_unused)
324 {
325 	dump_printf(": unhandled!\n");
326 	return 0;
327 }
328 
329 static int process_event_synth_attr_stub(struct perf_tool *tool __maybe_unused,
330 					 union perf_event *event __maybe_unused,
331 					 struct evlist **pevlist
332 					 __maybe_unused)
333 {
334 	dump_printf(": unhandled!\n");
335 	return 0;
336 }
337 
338 static int process_event_synth_event_update_stub(struct perf_tool *tool __maybe_unused,
339 						 union perf_event *event __maybe_unused,
340 						 struct evlist **pevlist
341 						 __maybe_unused)
342 {
343 	if (dump_trace)
344 		perf_event__fprintf_event_update(event, stdout);
345 
346 	dump_printf(": unhandled!\n");
347 	return 0;
348 }
349 
350 static int process_event_sample_stub(struct perf_tool *tool __maybe_unused,
351 				     union perf_event *event __maybe_unused,
352 				     struct perf_sample *sample __maybe_unused,
353 				     struct evsel *evsel __maybe_unused,
354 				     struct machine *machine __maybe_unused)
355 {
356 	dump_printf(": unhandled!\n");
357 	return 0;
358 }
359 
360 static int process_event_stub(struct perf_tool *tool __maybe_unused,
361 			      union perf_event *event __maybe_unused,
362 			      struct perf_sample *sample __maybe_unused,
363 			      struct machine *machine __maybe_unused)
364 {
365 	dump_printf(": unhandled!\n");
366 	return 0;
367 }
368 
369 static int process_finished_round_stub(struct perf_tool *tool __maybe_unused,
370 				       union perf_event *event __maybe_unused,
371 				       struct ordered_events *oe __maybe_unused)
372 {
373 	dump_printf(": unhandled!\n");
374 	return 0;
375 }
376 
377 static int process_finished_round(struct perf_tool *tool,
378 				  union perf_event *event,
379 				  struct ordered_events *oe);
380 
381 static int skipn(int fd, off_t n)
382 {
383 	char buf[4096];
384 	ssize_t ret;
385 
386 	while (n > 0) {
387 		ret = read(fd, buf, min(n, (off_t)sizeof(buf)));
388 		if (ret <= 0)
389 			return ret;
390 		n -= ret;
391 	}
392 
393 	return 0;
394 }
395 
396 static s64 process_event_auxtrace_stub(struct perf_session *session __maybe_unused,
397 				       union perf_event *event)
398 {
399 	dump_printf(": unhandled!\n");
400 	if (perf_data__is_pipe(session->data))
401 		skipn(perf_data__fd(session->data), event->auxtrace.size);
402 	return event->auxtrace.size;
403 }
404 
405 static int process_event_op2_stub(struct perf_session *session __maybe_unused,
406 				  union perf_event *event __maybe_unused)
407 {
408 	dump_printf(": unhandled!\n");
409 	return 0;
410 }
411 
412 
413 static
414 int process_event_thread_map_stub(struct perf_session *session __maybe_unused,
415 				  union perf_event *event __maybe_unused)
416 {
417 	if (dump_trace)
418 		perf_event__fprintf_thread_map(event, stdout);
419 
420 	dump_printf(": unhandled!\n");
421 	return 0;
422 }
423 
424 static
425 int process_event_cpu_map_stub(struct perf_session *session __maybe_unused,
426 			       union perf_event *event __maybe_unused)
427 {
428 	if (dump_trace)
429 		perf_event__fprintf_cpu_map(event, stdout);
430 
431 	dump_printf(": unhandled!\n");
432 	return 0;
433 }
434 
435 static
436 int process_event_stat_config_stub(struct perf_session *session __maybe_unused,
437 				   union perf_event *event __maybe_unused)
438 {
439 	if (dump_trace)
440 		perf_event__fprintf_stat_config(event, stdout);
441 
442 	dump_printf(": unhandled!\n");
443 	return 0;
444 }
445 
446 static int process_stat_stub(struct perf_session *perf_session __maybe_unused,
447 			     union perf_event *event)
448 {
449 	if (dump_trace)
450 		perf_event__fprintf_stat(event, stdout);
451 
452 	dump_printf(": unhandled!\n");
453 	return 0;
454 }
455 
456 static int process_stat_round_stub(struct perf_session *perf_session __maybe_unused,
457 				   union perf_event *event)
458 {
459 	if (dump_trace)
460 		perf_event__fprintf_stat_round(event, stdout);
461 
462 	dump_printf(": unhandled!\n");
463 	return 0;
464 }
465 
466 static int process_event_time_conv_stub(struct perf_session *perf_session __maybe_unused,
467 					union perf_event *event)
468 {
469 	if (dump_trace)
470 		perf_event__fprintf_time_conv(event, stdout);
471 
472 	dump_printf(": unhandled!\n");
473 	return 0;
474 }
475 
476 static int perf_session__process_compressed_event_stub(struct perf_session *session __maybe_unused,
477 						       union perf_event *event __maybe_unused,
478 						       u64 file_offset __maybe_unused,
479 						       const char *file_path __maybe_unused)
480 {
481        dump_printf(": unhandled!\n");
482        return 0;
483 }
484 
485 void perf_tool__fill_defaults(struct perf_tool *tool)
486 {
487 	if (tool->sample == NULL)
488 		tool->sample = process_event_sample_stub;
489 	if (tool->mmap == NULL)
490 		tool->mmap = process_event_stub;
491 	if (tool->mmap2 == NULL)
492 		tool->mmap2 = process_event_stub;
493 	if (tool->comm == NULL)
494 		tool->comm = process_event_stub;
495 	if (tool->namespaces == NULL)
496 		tool->namespaces = process_event_stub;
497 	if (tool->cgroup == NULL)
498 		tool->cgroup = process_event_stub;
499 	if (tool->fork == NULL)
500 		tool->fork = process_event_stub;
501 	if (tool->exit == NULL)
502 		tool->exit = process_event_stub;
503 	if (tool->lost == NULL)
504 		tool->lost = perf_event__process_lost;
505 	if (tool->lost_samples == NULL)
506 		tool->lost_samples = perf_event__process_lost_samples;
507 	if (tool->aux == NULL)
508 		tool->aux = perf_event__process_aux;
509 	if (tool->itrace_start == NULL)
510 		tool->itrace_start = perf_event__process_itrace_start;
511 	if (tool->context_switch == NULL)
512 		tool->context_switch = perf_event__process_switch;
513 	if (tool->ksymbol == NULL)
514 		tool->ksymbol = perf_event__process_ksymbol;
515 	if (tool->bpf == NULL)
516 		tool->bpf = perf_event__process_bpf;
517 	if (tool->text_poke == NULL)
518 		tool->text_poke = perf_event__process_text_poke;
519 	if (tool->aux_output_hw_id == NULL)
520 		tool->aux_output_hw_id = perf_event__process_aux_output_hw_id;
521 	if (tool->read == NULL)
522 		tool->read = process_event_sample_stub;
523 	if (tool->throttle == NULL)
524 		tool->throttle = process_event_stub;
525 	if (tool->unthrottle == NULL)
526 		tool->unthrottle = process_event_stub;
527 	if (tool->attr == NULL)
528 		tool->attr = process_event_synth_attr_stub;
529 	if (tool->event_update == NULL)
530 		tool->event_update = process_event_synth_event_update_stub;
531 	if (tool->tracing_data == NULL)
532 		tool->tracing_data = process_event_synth_tracing_data_stub;
533 	if (tool->build_id == NULL)
534 		tool->build_id = process_event_op2_stub;
535 	if (tool->finished_round == NULL) {
536 		if (tool->ordered_events)
537 			tool->finished_round = process_finished_round;
538 		else
539 			tool->finished_round = process_finished_round_stub;
540 	}
541 	if (tool->id_index == NULL)
542 		tool->id_index = process_event_op2_stub;
543 	if (tool->auxtrace_info == NULL)
544 		tool->auxtrace_info = process_event_op2_stub;
545 	if (tool->auxtrace == NULL)
546 		tool->auxtrace = process_event_auxtrace_stub;
547 	if (tool->auxtrace_error == NULL)
548 		tool->auxtrace_error = process_event_op2_stub;
549 	if (tool->thread_map == NULL)
550 		tool->thread_map = process_event_thread_map_stub;
551 	if (tool->cpu_map == NULL)
552 		tool->cpu_map = process_event_cpu_map_stub;
553 	if (tool->stat_config == NULL)
554 		tool->stat_config = process_event_stat_config_stub;
555 	if (tool->stat == NULL)
556 		tool->stat = process_stat_stub;
557 	if (tool->stat_round == NULL)
558 		tool->stat_round = process_stat_round_stub;
559 	if (tool->time_conv == NULL)
560 		tool->time_conv = process_event_time_conv_stub;
561 	if (tool->feature == NULL)
562 		tool->feature = process_event_op2_stub;
563 	if (tool->compressed == NULL)
564 		tool->compressed = perf_session__process_compressed_event;
565 }
566 
567 static void swap_sample_id_all(union perf_event *event, void *data)
568 {
569 	void *end = (void *) event + event->header.size;
570 	int size = end - data;
571 
572 	BUG_ON(size % sizeof(u64));
573 	mem_bswap_64(data, size);
574 }
575 
576 static void perf_event__all64_swap(union perf_event *event,
577 				   bool sample_id_all __maybe_unused)
578 {
579 	struct perf_event_header *hdr = &event->header;
580 	mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr));
581 }
582 
583 static void perf_event__comm_swap(union perf_event *event, bool sample_id_all)
584 {
585 	event->comm.pid = bswap_32(event->comm.pid);
586 	event->comm.tid = bswap_32(event->comm.tid);
587 
588 	if (sample_id_all) {
589 		void *data = &event->comm.comm;
590 
591 		data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
592 		swap_sample_id_all(event, data);
593 	}
594 }
595 
596 static void perf_event__mmap_swap(union perf_event *event,
597 				  bool sample_id_all)
598 {
599 	event->mmap.pid	  = bswap_32(event->mmap.pid);
600 	event->mmap.tid	  = bswap_32(event->mmap.tid);
601 	event->mmap.start = bswap_64(event->mmap.start);
602 	event->mmap.len	  = bswap_64(event->mmap.len);
603 	event->mmap.pgoff = bswap_64(event->mmap.pgoff);
604 
605 	if (sample_id_all) {
606 		void *data = &event->mmap.filename;
607 
608 		data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
609 		swap_sample_id_all(event, data);
610 	}
611 }
612 
613 static void perf_event__mmap2_swap(union perf_event *event,
614 				  bool sample_id_all)
615 {
616 	event->mmap2.pid   = bswap_32(event->mmap2.pid);
617 	event->mmap2.tid   = bswap_32(event->mmap2.tid);
618 	event->mmap2.start = bswap_64(event->mmap2.start);
619 	event->mmap2.len   = bswap_64(event->mmap2.len);
620 	event->mmap2.pgoff = bswap_64(event->mmap2.pgoff);
621 
622 	if (!(event->header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID)) {
623 		event->mmap2.maj   = bswap_32(event->mmap2.maj);
624 		event->mmap2.min   = bswap_32(event->mmap2.min);
625 		event->mmap2.ino   = bswap_64(event->mmap2.ino);
626 		event->mmap2.ino_generation = bswap_64(event->mmap2.ino_generation);
627 	}
628 
629 	if (sample_id_all) {
630 		void *data = &event->mmap2.filename;
631 
632 		data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
633 		swap_sample_id_all(event, data);
634 	}
635 }
636 static void perf_event__task_swap(union perf_event *event, bool sample_id_all)
637 {
638 	event->fork.pid	 = bswap_32(event->fork.pid);
639 	event->fork.tid	 = bswap_32(event->fork.tid);
640 	event->fork.ppid = bswap_32(event->fork.ppid);
641 	event->fork.ptid = bswap_32(event->fork.ptid);
642 	event->fork.time = bswap_64(event->fork.time);
643 
644 	if (sample_id_all)
645 		swap_sample_id_all(event, &event->fork + 1);
646 }
647 
648 static void perf_event__read_swap(union perf_event *event, bool sample_id_all)
649 {
650 	event->read.pid		 = bswap_32(event->read.pid);
651 	event->read.tid		 = bswap_32(event->read.tid);
652 	event->read.value	 = bswap_64(event->read.value);
653 	event->read.time_enabled = bswap_64(event->read.time_enabled);
654 	event->read.time_running = bswap_64(event->read.time_running);
655 	event->read.id		 = bswap_64(event->read.id);
656 
657 	if (sample_id_all)
658 		swap_sample_id_all(event, &event->read + 1);
659 }
660 
661 static void perf_event__aux_swap(union perf_event *event, bool sample_id_all)
662 {
663 	event->aux.aux_offset = bswap_64(event->aux.aux_offset);
664 	event->aux.aux_size   = bswap_64(event->aux.aux_size);
665 	event->aux.flags      = bswap_64(event->aux.flags);
666 
667 	if (sample_id_all)
668 		swap_sample_id_all(event, &event->aux + 1);
669 }
670 
671 static void perf_event__itrace_start_swap(union perf_event *event,
672 					  bool sample_id_all)
673 {
674 	event->itrace_start.pid	 = bswap_32(event->itrace_start.pid);
675 	event->itrace_start.tid	 = bswap_32(event->itrace_start.tid);
676 
677 	if (sample_id_all)
678 		swap_sample_id_all(event, &event->itrace_start + 1);
679 }
680 
681 static void perf_event__switch_swap(union perf_event *event, bool sample_id_all)
682 {
683 	if (event->header.type == PERF_RECORD_SWITCH_CPU_WIDE) {
684 		event->context_switch.next_prev_pid =
685 				bswap_32(event->context_switch.next_prev_pid);
686 		event->context_switch.next_prev_tid =
687 				bswap_32(event->context_switch.next_prev_tid);
688 	}
689 
690 	if (sample_id_all)
691 		swap_sample_id_all(event, &event->context_switch + 1);
692 }
693 
694 static void perf_event__text_poke_swap(union perf_event *event, bool sample_id_all)
695 {
696 	event->text_poke.addr    = bswap_64(event->text_poke.addr);
697 	event->text_poke.old_len = bswap_16(event->text_poke.old_len);
698 	event->text_poke.new_len = bswap_16(event->text_poke.new_len);
699 
700 	if (sample_id_all) {
701 		size_t len = sizeof(event->text_poke.old_len) +
702 			     sizeof(event->text_poke.new_len) +
703 			     event->text_poke.old_len +
704 			     event->text_poke.new_len;
705 		void *data = &event->text_poke.old_len;
706 
707 		data += PERF_ALIGN(len, sizeof(u64));
708 		swap_sample_id_all(event, data);
709 	}
710 }
711 
712 static void perf_event__throttle_swap(union perf_event *event,
713 				      bool sample_id_all)
714 {
715 	event->throttle.time	  = bswap_64(event->throttle.time);
716 	event->throttle.id	  = bswap_64(event->throttle.id);
717 	event->throttle.stream_id = bswap_64(event->throttle.stream_id);
718 
719 	if (sample_id_all)
720 		swap_sample_id_all(event, &event->throttle + 1);
721 }
722 
723 static void perf_event__namespaces_swap(union perf_event *event,
724 					bool sample_id_all)
725 {
726 	u64 i;
727 
728 	event->namespaces.pid		= bswap_32(event->namespaces.pid);
729 	event->namespaces.tid		= bswap_32(event->namespaces.tid);
730 	event->namespaces.nr_namespaces	= bswap_64(event->namespaces.nr_namespaces);
731 
732 	for (i = 0; i < event->namespaces.nr_namespaces; i++) {
733 		struct perf_ns_link_info *ns = &event->namespaces.link_info[i];
734 
735 		ns->dev = bswap_64(ns->dev);
736 		ns->ino = bswap_64(ns->ino);
737 	}
738 
739 	if (sample_id_all)
740 		swap_sample_id_all(event, &event->namespaces.link_info[i]);
741 }
742 
743 static void perf_event__cgroup_swap(union perf_event *event, bool sample_id_all)
744 {
745 	event->cgroup.id = bswap_64(event->cgroup.id);
746 
747 	if (sample_id_all) {
748 		void *data = &event->cgroup.path;
749 
750 		data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
751 		swap_sample_id_all(event, data);
752 	}
753 }
754 
755 static u8 revbyte(u8 b)
756 {
757 	int rev = (b >> 4) | ((b & 0xf) << 4);
758 	rev = ((rev & 0xcc) >> 2) | ((rev & 0x33) << 2);
759 	rev = ((rev & 0xaa) >> 1) | ((rev & 0x55) << 1);
760 	return (u8) rev;
761 }
762 
763 /*
764  * XXX this is hack in attempt to carry flags bitfield
765  * through endian village. ABI says:
766  *
767  * Bit-fields are allocated from right to left (least to most significant)
768  * on little-endian implementations and from left to right (most to least
769  * significant) on big-endian implementations.
770  *
771  * The above seems to be byte specific, so we need to reverse each
772  * byte of the bitfield. 'Internet' also says this might be implementation
773  * specific and we probably need proper fix and carry perf_event_attr
774  * bitfield flags in separate data file FEAT_ section. Thought this seems
775  * to work for now.
776  */
777 static void swap_bitfield(u8 *p, unsigned len)
778 {
779 	unsigned i;
780 
781 	for (i = 0; i < len; i++) {
782 		*p = revbyte(*p);
783 		p++;
784 	}
785 }
786 
787 /* exported for swapping attributes in file header */
788 void perf_event__attr_swap(struct perf_event_attr *attr)
789 {
790 	attr->type		= bswap_32(attr->type);
791 	attr->size		= bswap_32(attr->size);
792 
793 #define bswap_safe(f, n) 					\
794 	(attr->size > (offsetof(struct perf_event_attr, f) + 	\
795 		       sizeof(attr->f) * (n)))
796 #define bswap_field(f, sz) 			\
797 do { 						\
798 	if (bswap_safe(f, 0))			\
799 		attr->f = bswap_##sz(attr->f);	\
800 } while(0)
801 #define bswap_field_16(f) bswap_field(f, 16)
802 #define bswap_field_32(f) bswap_field(f, 32)
803 #define bswap_field_64(f) bswap_field(f, 64)
804 
805 	bswap_field_64(config);
806 	bswap_field_64(sample_period);
807 	bswap_field_64(sample_type);
808 	bswap_field_64(read_format);
809 	bswap_field_32(wakeup_events);
810 	bswap_field_32(bp_type);
811 	bswap_field_64(bp_addr);
812 	bswap_field_64(bp_len);
813 	bswap_field_64(branch_sample_type);
814 	bswap_field_64(sample_regs_user);
815 	bswap_field_32(sample_stack_user);
816 	bswap_field_32(aux_watermark);
817 	bswap_field_16(sample_max_stack);
818 	bswap_field_32(aux_sample_size);
819 
820 	/*
821 	 * After read_format are bitfields. Check read_format because
822 	 * we are unable to use offsetof on bitfield.
823 	 */
824 	if (bswap_safe(read_format, 1))
825 		swap_bitfield((u8 *) (&attr->read_format + 1),
826 			      sizeof(u64));
827 #undef bswap_field_64
828 #undef bswap_field_32
829 #undef bswap_field
830 #undef bswap_safe
831 }
832 
833 static void perf_event__hdr_attr_swap(union perf_event *event,
834 				      bool sample_id_all __maybe_unused)
835 {
836 	size_t size;
837 
838 	perf_event__attr_swap(&event->attr.attr);
839 
840 	size = event->header.size;
841 	size -= (void *)&event->attr.id - (void *)event;
842 	mem_bswap_64(event->attr.id, size);
843 }
844 
845 static void perf_event__event_update_swap(union perf_event *event,
846 					  bool sample_id_all __maybe_unused)
847 {
848 	event->event_update.type = bswap_64(event->event_update.type);
849 	event->event_update.id   = bswap_64(event->event_update.id);
850 }
851 
852 static void perf_event__event_type_swap(union perf_event *event,
853 					bool sample_id_all __maybe_unused)
854 {
855 	event->event_type.event_type.event_id =
856 		bswap_64(event->event_type.event_type.event_id);
857 }
858 
859 static void perf_event__tracing_data_swap(union perf_event *event,
860 					  bool sample_id_all __maybe_unused)
861 {
862 	event->tracing_data.size = bswap_32(event->tracing_data.size);
863 }
864 
865 static void perf_event__auxtrace_info_swap(union perf_event *event,
866 					   bool sample_id_all __maybe_unused)
867 {
868 	size_t size;
869 
870 	event->auxtrace_info.type = bswap_32(event->auxtrace_info.type);
871 
872 	size = event->header.size;
873 	size -= (void *)&event->auxtrace_info.priv - (void *)event;
874 	mem_bswap_64(event->auxtrace_info.priv, size);
875 }
876 
877 static void perf_event__auxtrace_swap(union perf_event *event,
878 				      bool sample_id_all __maybe_unused)
879 {
880 	event->auxtrace.size      = bswap_64(event->auxtrace.size);
881 	event->auxtrace.offset    = bswap_64(event->auxtrace.offset);
882 	event->auxtrace.reference = bswap_64(event->auxtrace.reference);
883 	event->auxtrace.idx       = bswap_32(event->auxtrace.idx);
884 	event->auxtrace.tid       = bswap_32(event->auxtrace.tid);
885 	event->auxtrace.cpu       = bswap_32(event->auxtrace.cpu);
886 }
887 
888 static void perf_event__auxtrace_error_swap(union perf_event *event,
889 					    bool sample_id_all __maybe_unused)
890 {
891 	event->auxtrace_error.type = bswap_32(event->auxtrace_error.type);
892 	event->auxtrace_error.code = bswap_32(event->auxtrace_error.code);
893 	event->auxtrace_error.cpu  = bswap_32(event->auxtrace_error.cpu);
894 	event->auxtrace_error.pid  = bswap_32(event->auxtrace_error.pid);
895 	event->auxtrace_error.tid  = bswap_32(event->auxtrace_error.tid);
896 	event->auxtrace_error.fmt  = bswap_32(event->auxtrace_error.fmt);
897 	event->auxtrace_error.ip   = bswap_64(event->auxtrace_error.ip);
898 	if (event->auxtrace_error.fmt)
899 		event->auxtrace_error.time = bswap_64(event->auxtrace_error.time);
900 }
901 
902 static void perf_event__thread_map_swap(union perf_event *event,
903 					bool sample_id_all __maybe_unused)
904 {
905 	unsigned i;
906 
907 	event->thread_map.nr = bswap_64(event->thread_map.nr);
908 
909 	for (i = 0; i < event->thread_map.nr; i++)
910 		event->thread_map.entries[i].pid = bswap_64(event->thread_map.entries[i].pid);
911 }
912 
913 static void perf_event__cpu_map_swap(union perf_event *event,
914 				     bool sample_id_all __maybe_unused)
915 {
916 	struct perf_record_cpu_map_data *data = &event->cpu_map.data;
917 	struct cpu_map_entries *cpus;
918 	struct perf_record_record_cpu_map *mask;
919 	unsigned i;
920 
921 	data->type = bswap_16(data->type);
922 
923 	switch (data->type) {
924 	case PERF_CPU_MAP__CPUS:
925 		cpus = (struct cpu_map_entries *)data->data;
926 
927 		cpus->nr = bswap_16(cpus->nr);
928 
929 		for (i = 0; i < cpus->nr; i++)
930 			cpus->cpu[i] = bswap_16(cpus->cpu[i]);
931 		break;
932 	case PERF_CPU_MAP__MASK:
933 		mask = (struct perf_record_record_cpu_map *)data->data;
934 
935 		mask->nr = bswap_16(mask->nr);
936 		mask->long_size = bswap_16(mask->long_size);
937 
938 		switch (mask->long_size) {
939 		case 4: mem_bswap_32(&mask->mask, mask->nr); break;
940 		case 8: mem_bswap_64(&mask->mask, mask->nr); break;
941 		default:
942 			pr_err("cpu_map swap: unsupported long size\n");
943 		}
944 	default:
945 		break;
946 	}
947 }
948 
949 static void perf_event__stat_config_swap(union perf_event *event,
950 					 bool sample_id_all __maybe_unused)
951 {
952 	u64 size;
953 
954 	size  = bswap_64(event->stat_config.nr) * sizeof(event->stat_config.data[0]);
955 	size += 1; /* nr item itself */
956 	mem_bswap_64(&event->stat_config.nr, size);
957 }
958 
959 static void perf_event__stat_swap(union perf_event *event,
960 				  bool sample_id_all __maybe_unused)
961 {
962 	event->stat.id     = bswap_64(event->stat.id);
963 	event->stat.thread = bswap_32(event->stat.thread);
964 	event->stat.cpu    = bswap_32(event->stat.cpu);
965 	event->stat.val    = bswap_64(event->stat.val);
966 	event->stat.ena    = bswap_64(event->stat.ena);
967 	event->stat.run    = bswap_64(event->stat.run);
968 }
969 
970 static void perf_event__stat_round_swap(union perf_event *event,
971 					bool sample_id_all __maybe_unused)
972 {
973 	event->stat_round.type = bswap_64(event->stat_round.type);
974 	event->stat_round.time = bswap_64(event->stat_round.time);
975 }
976 
977 static void perf_event__time_conv_swap(union perf_event *event,
978 				       bool sample_id_all __maybe_unused)
979 {
980 	event->time_conv.time_shift = bswap_64(event->time_conv.time_shift);
981 	event->time_conv.time_mult  = bswap_64(event->time_conv.time_mult);
982 	event->time_conv.time_zero  = bswap_64(event->time_conv.time_zero);
983 
984 	if (event_contains(event->time_conv, time_cycles)) {
985 		event->time_conv.time_cycles = bswap_64(event->time_conv.time_cycles);
986 		event->time_conv.time_mask = bswap_64(event->time_conv.time_mask);
987 	}
988 }
989 
990 typedef void (*perf_event__swap_op)(union perf_event *event,
991 				    bool sample_id_all);
992 
993 static perf_event__swap_op perf_event__swap_ops[] = {
994 	[PERF_RECORD_MMAP]		  = perf_event__mmap_swap,
995 	[PERF_RECORD_MMAP2]		  = perf_event__mmap2_swap,
996 	[PERF_RECORD_COMM]		  = perf_event__comm_swap,
997 	[PERF_RECORD_FORK]		  = perf_event__task_swap,
998 	[PERF_RECORD_EXIT]		  = perf_event__task_swap,
999 	[PERF_RECORD_LOST]		  = perf_event__all64_swap,
1000 	[PERF_RECORD_READ]		  = perf_event__read_swap,
1001 	[PERF_RECORD_THROTTLE]		  = perf_event__throttle_swap,
1002 	[PERF_RECORD_UNTHROTTLE]	  = perf_event__throttle_swap,
1003 	[PERF_RECORD_SAMPLE]		  = perf_event__all64_swap,
1004 	[PERF_RECORD_AUX]		  = perf_event__aux_swap,
1005 	[PERF_RECORD_ITRACE_START]	  = perf_event__itrace_start_swap,
1006 	[PERF_RECORD_LOST_SAMPLES]	  = perf_event__all64_swap,
1007 	[PERF_RECORD_SWITCH]		  = perf_event__switch_swap,
1008 	[PERF_RECORD_SWITCH_CPU_WIDE]	  = perf_event__switch_swap,
1009 	[PERF_RECORD_NAMESPACES]	  = perf_event__namespaces_swap,
1010 	[PERF_RECORD_CGROUP]		  = perf_event__cgroup_swap,
1011 	[PERF_RECORD_TEXT_POKE]		  = perf_event__text_poke_swap,
1012 	[PERF_RECORD_AUX_OUTPUT_HW_ID]	  = perf_event__all64_swap,
1013 	[PERF_RECORD_HEADER_ATTR]	  = perf_event__hdr_attr_swap,
1014 	[PERF_RECORD_HEADER_EVENT_TYPE]	  = perf_event__event_type_swap,
1015 	[PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap,
1016 	[PERF_RECORD_HEADER_BUILD_ID]	  = NULL,
1017 	[PERF_RECORD_ID_INDEX]		  = perf_event__all64_swap,
1018 	[PERF_RECORD_AUXTRACE_INFO]	  = perf_event__auxtrace_info_swap,
1019 	[PERF_RECORD_AUXTRACE]		  = perf_event__auxtrace_swap,
1020 	[PERF_RECORD_AUXTRACE_ERROR]	  = perf_event__auxtrace_error_swap,
1021 	[PERF_RECORD_THREAD_MAP]	  = perf_event__thread_map_swap,
1022 	[PERF_RECORD_CPU_MAP]		  = perf_event__cpu_map_swap,
1023 	[PERF_RECORD_STAT_CONFIG]	  = perf_event__stat_config_swap,
1024 	[PERF_RECORD_STAT]		  = perf_event__stat_swap,
1025 	[PERF_RECORD_STAT_ROUND]	  = perf_event__stat_round_swap,
1026 	[PERF_RECORD_EVENT_UPDATE]	  = perf_event__event_update_swap,
1027 	[PERF_RECORD_TIME_CONV]		  = perf_event__time_conv_swap,
1028 	[PERF_RECORD_HEADER_MAX]	  = NULL,
1029 };
1030 
1031 /*
1032  * When perf record finishes a pass on every buffers, it records this pseudo
1033  * event.
1034  * We record the max timestamp t found in the pass n.
1035  * Assuming these timestamps are monotonic across cpus, we know that if
1036  * a buffer still has events with timestamps below t, they will be all
1037  * available and then read in the pass n + 1.
1038  * Hence when we start to read the pass n + 2, we can safely flush every
1039  * events with timestamps below t.
1040  *
1041  *    ============ PASS n =================
1042  *       CPU 0         |   CPU 1
1043  *                     |
1044  *    cnt1 timestamps  |   cnt2 timestamps
1045  *          1          |         2
1046  *          2          |         3
1047  *          -          |         4  <--- max recorded
1048  *
1049  *    ============ PASS n + 1 ==============
1050  *       CPU 0         |   CPU 1
1051  *                     |
1052  *    cnt1 timestamps  |   cnt2 timestamps
1053  *          3          |         5
1054  *          4          |         6
1055  *          5          |         7 <---- max recorded
1056  *
1057  *      Flush every events below timestamp 4
1058  *
1059  *    ============ PASS n + 2 ==============
1060  *       CPU 0         |   CPU 1
1061  *                     |
1062  *    cnt1 timestamps  |   cnt2 timestamps
1063  *          6          |         8
1064  *          7          |         9
1065  *          -          |         10
1066  *
1067  *      Flush every events below timestamp 7
1068  *      etc...
1069  */
1070 static int process_finished_round(struct perf_tool *tool __maybe_unused,
1071 				  union perf_event *event __maybe_unused,
1072 				  struct ordered_events *oe)
1073 {
1074 	if (dump_trace)
1075 		fprintf(stdout, "\n");
1076 	return ordered_events__flush(oe, OE_FLUSH__ROUND);
1077 }
1078 
1079 int perf_session__queue_event(struct perf_session *s, union perf_event *event,
1080 			      u64 timestamp, u64 file_offset, const char *file_path)
1081 {
1082 	return ordered_events__queue(&s->ordered_events, event, timestamp, file_offset, file_path);
1083 }
1084 
1085 static void callchain__lbr_callstack_printf(struct perf_sample *sample)
1086 {
1087 	struct ip_callchain *callchain = sample->callchain;
1088 	struct branch_stack *lbr_stack = sample->branch_stack;
1089 	struct branch_entry *entries = perf_sample__branch_entries(sample);
1090 	u64 kernel_callchain_nr = callchain->nr;
1091 	unsigned int i;
1092 
1093 	for (i = 0; i < kernel_callchain_nr; i++) {
1094 		if (callchain->ips[i] == PERF_CONTEXT_USER)
1095 			break;
1096 	}
1097 
1098 	if ((i != kernel_callchain_nr) && lbr_stack->nr) {
1099 		u64 total_nr;
1100 		/*
1101 		 * LBR callstack can only get user call chain,
1102 		 * i is kernel call chain number,
1103 		 * 1 is PERF_CONTEXT_USER.
1104 		 *
1105 		 * The user call chain is stored in LBR registers.
1106 		 * LBR are pair registers. The caller is stored
1107 		 * in "from" register, while the callee is stored
1108 		 * in "to" register.
1109 		 * For example, there is a call stack
1110 		 * "A"->"B"->"C"->"D".
1111 		 * The LBR registers will be recorded like
1112 		 * "C"->"D", "B"->"C", "A"->"B".
1113 		 * So only the first "to" register and all "from"
1114 		 * registers are needed to construct the whole stack.
1115 		 */
1116 		total_nr = i + 1 + lbr_stack->nr + 1;
1117 		kernel_callchain_nr = i + 1;
1118 
1119 		printf("... LBR call chain: nr:%" PRIu64 "\n", total_nr);
1120 
1121 		for (i = 0; i < kernel_callchain_nr; i++)
1122 			printf("..... %2d: %016" PRIx64 "\n",
1123 			       i, callchain->ips[i]);
1124 
1125 		printf("..... %2d: %016" PRIx64 "\n",
1126 		       (int)(kernel_callchain_nr), entries[0].to);
1127 		for (i = 0; i < lbr_stack->nr; i++)
1128 			printf("..... %2d: %016" PRIx64 "\n",
1129 			       (int)(i + kernel_callchain_nr + 1), entries[i].from);
1130 	}
1131 }
1132 
1133 static void callchain__printf(struct evsel *evsel,
1134 			      struct perf_sample *sample)
1135 {
1136 	unsigned int i;
1137 	struct ip_callchain *callchain = sample->callchain;
1138 
1139 	if (evsel__has_branch_callstack(evsel))
1140 		callchain__lbr_callstack_printf(sample);
1141 
1142 	printf("... FP chain: nr:%" PRIu64 "\n", callchain->nr);
1143 
1144 	for (i = 0; i < callchain->nr; i++)
1145 		printf("..... %2d: %016" PRIx64 "\n",
1146 		       i, callchain->ips[i]);
1147 }
1148 
1149 static void branch_stack__printf(struct perf_sample *sample, bool callstack)
1150 {
1151 	struct branch_entry *entries = perf_sample__branch_entries(sample);
1152 	uint64_t i;
1153 
1154 	printf("%s: nr:%" PRIu64 "\n",
1155 		!callstack ? "... branch stack" : "... branch callstack",
1156 		sample->branch_stack->nr);
1157 
1158 	for (i = 0; i < sample->branch_stack->nr; i++) {
1159 		struct branch_entry *e = &entries[i];
1160 
1161 		if (!callstack) {
1162 			printf("..... %2"PRIu64": %016" PRIx64 " -> %016" PRIx64 " %hu cycles %s%s%s%s %x %s\n",
1163 				i, e->from, e->to,
1164 				(unsigned short)e->flags.cycles,
1165 				e->flags.mispred ? "M" : " ",
1166 				e->flags.predicted ? "P" : " ",
1167 				e->flags.abort ? "A" : " ",
1168 				e->flags.in_tx ? "T" : " ",
1169 				(unsigned)e->flags.reserved,
1170 				e->flags.type ? branch_type_name(e->flags.type) : "");
1171 		} else {
1172 			printf("..... %2"PRIu64": %016" PRIx64 "\n",
1173 				i, i > 0 ? e->from : e->to);
1174 		}
1175 	}
1176 }
1177 
1178 static void regs_dump__printf(u64 mask, u64 *regs, const char *arch)
1179 {
1180 	unsigned rid, i = 0;
1181 
1182 	for_each_set_bit(rid, (unsigned long *) &mask, sizeof(mask) * 8) {
1183 		u64 val = regs[i++];
1184 
1185 		printf(".... %-5s 0x%016" PRIx64 "\n",
1186 		       perf_reg_name(rid, arch), val);
1187 	}
1188 }
1189 
1190 static const char *regs_abi[] = {
1191 	[PERF_SAMPLE_REGS_ABI_NONE] = "none",
1192 	[PERF_SAMPLE_REGS_ABI_32] = "32-bit",
1193 	[PERF_SAMPLE_REGS_ABI_64] = "64-bit",
1194 };
1195 
1196 static inline const char *regs_dump_abi(struct regs_dump *d)
1197 {
1198 	if (d->abi > PERF_SAMPLE_REGS_ABI_64)
1199 		return "unknown";
1200 
1201 	return regs_abi[d->abi];
1202 }
1203 
1204 static void regs__printf(const char *type, struct regs_dump *regs, const char *arch)
1205 {
1206 	u64 mask = regs->mask;
1207 
1208 	printf("... %s regs: mask 0x%" PRIx64 " ABI %s\n",
1209 	       type,
1210 	       mask,
1211 	       regs_dump_abi(regs));
1212 
1213 	regs_dump__printf(mask, regs->regs, arch);
1214 }
1215 
1216 static void regs_user__printf(struct perf_sample *sample, const char *arch)
1217 {
1218 	struct regs_dump *user_regs = &sample->user_regs;
1219 
1220 	if (user_regs->regs)
1221 		regs__printf("user", user_regs, arch);
1222 }
1223 
1224 static void regs_intr__printf(struct perf_sample *sample, const char *arch)
1225 {
1226 	struct regs_dump *intr_regs = &sample->intr_regs;
1227 
1228 	if (intr_regs->regs)
1229 		regs__printf("intr", intr_regs, arch);
1230 }
1231 
1232 static void stack_user__printf(struct stack_dump *dump)
1233 {
1234 	printf("... ustack: size %" PRIu64 ", offset 0x%x\n",
1235 	       dump->size, dump->offset);
1236 }
1237 
1238 static void evlist__print_tstamp(struct evlist *evlist, union perf_event *event, struct perf_sample *sample)
1239 {
1240 	u64 sample_type = __evlist__combined_sample_type(evlist);
1241 
1242 	if (event->header.type != PERF_RECORD_SAMPLE &&
1243 	    !evlist__sample_id_all(evlist)) {
1244 		fputs("-1 -1 ", stdout);
1245 		return;
1246 	}
1247 
1248 	if ((sample_type & PERF_SAMPLE_CPU))
1249 		printf("%u ", sample->cpu);
1250 
1251 	if (sample_type & PERF_SAMPLE_TIME)
1252 		printf("%" PRIu64 " ", sample->time);
1253 }
1254 
1255 static void sample_read__printf(struct perf_sample *sample, u64 read_format)
1256 {
1257 	printf("... sample_read:\n");
1258 
1259 	if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1260 		printf("...... time enabled %016" PRIx64 "\n",
1261 		       sample->read.time_enabled);
1262 
1263 	if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1264 		printf("...... time running %016" PRIx64 "\n",
1265 		       sample->read.time_running);
1266 
1267 	if (read_format & PERF_FORMAT_GROUP) {
1268 		u64 i;
1269 
1270 		printf(".... group nr %" PRIu64 "\n", sample->read.group.nr);
1271 
1272 		for (i = 0; i < sample->read.group.nr; i++) {
1273 			struct sample_read_value *value;
1274 
1275 			value = &sample->read.group.values[i];
1276 			printf("..... id %016" PRIx64
1277 			       ", value %016" PRIx64 "\n",
1278 			       value->id, value->value);
1279 		}
1280 	} else
1281 		printf("..... id %016" PRIx64 ", value %016" PRIx64 "\n",
1282 			sample->read.one.id, sample->read.one.value);
1283 }
1284 
1285 static void dump_event(struct evlist *evlist, union perf_event *event,
1286 		       u64 file_offset, struct perf_sample *sample,
1287 		       const char *file_path)
1288 {
1289 	if (!dump_trace)
1290 		return;
1291 
1292 	printf("\n%#" PRIx64 "@%s [%#x]: event: %d\n",
1293 	       file_offset, file_path, event->header.size, event->header.type);
1294 
1295 	trace_event(event);
1296 	if (event->header.type == PERF_RECORD_SAMPLE && evlist->trace_event_sample_raw)
1297 		evlist->trace_event_sample_raw(evlist, event, sample);
1298 
1299 	if (sample)
1300 		evlist__print_tstamp(evlist, event, sample);
1301 
1302 	printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset,
1303 	       event->header.size, perf_event__name(event->header.type));
1304 }
1305 
1306 char *get_page_size_name(u64 size, char *str)
1307 {
1308 	if (!size || !unit_number__scnprintf(str, PAGE_SIZE_NAME_LEN, size))
1309 		snprintf(str, PAGE_SIZE_NAME_LEN, "%s", "N/A");
1310 
1311 	return str;
1312 }
1313 
1314 static void dump_sample(struct evsel *evsel, union perf_event *event,
1315 			struct perf_sample *sample, const char *arch)
1316 {
1317 	u64 sample_type;
1318 	char str[PAGE_SIZE_NAME_LEN];
1319 
1320 	if (!dump_trace)
1321 		return;
1322 
1323 	printf("(IP, 0x%x): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n",
1324 	       event->header.misc, sample->pid, sample->tid, sample->ip,
1325 	       sample->period, sample->addr);
1326 
1327 	sample_type = evsel->core.attr.sample_type;
1328 
1329 	if (evsel__has_callchain(evsel))
1330 		callchain__printf(evsel, sample);
1331 
1332 	if (evsel__has_br_stack(evsel))
1333 		branch_stack__printf(sample, evsel__has_branch_callstack(evsel));
1334 
1335 	if (sample_type & PERF_SAMPLE_REGS_USER)
1336 		regs_user__printf(sample, arch);
1337 
1338 	if (sample_type & PERF_SAMPLE_REGS_INTR)
1339 		regs_intr__printf(sample, arch);
1340 
1341 	if (sample_type & PERF_SAMPLE_STACK_USER)
1342 		stack_user__printf(&sample->user_stack);
1343 
1344 	if (sample_type & PERF_SAMPLE_WEIGHT_TYPE) {
1345 		printf("... weight: %" PRIu64 "", sample->weight);
1346 			if (sample_type & PERF_SAMPLE_WEIGHT_STRUCT) {
1347 				printf(",0x%"PRIx16"", sample->ins_lat);
1348 				printf(",0x%"PRIx16"", sample->p_stage_cyc);
1349 			}
1350 		printf("\n");
1351 	}
1352 
1353 	if (sample_type & PERF_SAMPLE_DATA_SRC)
1354 		printf(" . data_src: 0x%"PRIx64"\n", sample->data_src);
1355 
1356 	if (sample_type & PERF_SAMPLE_PHYS_ADDR)
1357 		printf(" .. phys_addr: 0x%"PRIx64"\n", sample->phys_addr);
1358 
1359 	if (sample_type & PERF_SAMPLE_DATA_PAGE_SIZE)
1360 		printf(" .. data page size: %s\n", get_page_size_name(sample->data_page_size, str));
1361 
1362 	if (sample_type & PERF_SAMPLE_CODE_PAGE_SIZE)
1363 		printf(" .. code page size: %s\n", get_page_size_name(sample->code_page_size, str));
1364 
1365 	if (sample_type & PERF_SAMPLE_TRANSACTION)
1366 		printf("... transaction: %" PRIx64 "\n", sample->transaction);
1367 
1368 	if (sample_type & PERF_SAMPLE_READ)
1369 		sample_read__printf(sample, evsel->core.attr.read_format);
1370 }
1371 
1372 static void dump_read(struct evsel *evsel, union perf_event *event)
1373 {
1374 	struct perf_record_read *read_event = &event->read;
1375 	u64 read_format;
1376 
1377 	if (!dump_trace)
1378 		return;
1379 
1380 	printf(": %d %d %s %" PRI_lu64 "\n", event->read.pid, event->read.tid,
1381 	       evsel__name(evsel), event->read.value);
1382 
1383 	if (!evsel)
1384 		return;
1385 
1386 	read_format = evsel->core.attr.read_format;
1387 
1388 	if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1389 		printf("... time enabled : %" PRI_lu64 "\n", read_event->time_enabled);
1390 
1391 	if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1392 		printf("... time running : %" PRI_lu64 "\n", read_event->time_running);
1393 
1394 	if (read_format & PERF_FORMAT_ID)
1395 		printf("... id           : %" PRI_lu64 "\n", read_event->id);
1396 }
1397 
1398 static struct machine *machines__find_for_cpumode(struct machines *machines,
1399 					       union perf_event *event,
1400 					       struct perf_sample *sample)
1401 {
1402 	if (perf_guest &&
1403 	    ((sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL) ||
1404 	     (sample->cpumode == PERF_RECORD_MISC_GUEST_USER))) {
1405 		u32 pid;
1406 
1407 		if (event->header.type == PERF_RECORD_MMAP
1408 		    || event->header.type == PERF_RECORD_MMAP2)
1409 			pid = event->mmap.pid;
1410 		else
1411 			pid = sample->pid;
1412 
1413 		return machines__find_guest(machines, pid);
1414 	}
1415 
1416 	return &machines->host;
1417 }
1418 
1419 static int deliver_sample_value(struct evlist *evlist,
1420 				struct perf_tool *tool,
1421 				union perf_event *event,
1422 				struct perf_sample *sample,
1423 				struct sample_read_value *v,
1424 				struct machine *machine)
1425 {
1426 	struct perf_sample_id *sid = evlist__id2sid(evlist, v->id);
1427 	struct evsel *evsel;
1428 
1429 	if (sid) {
1430 		sample->id     = v->id;
1431 		sample->period = v->value - sid->period;
1432 		sid->period    = v->value;
1433 	}
1434 
1435 	if (!sid || sid->evsel == NULL) {
1436 		++evlist->stats.nr_unknown_id;
1437 		return 0;
1438 	}
1439 
1440 	/*
1441 	 * There's no reason to deliver sample
1442 	 * for zero period, bail out.
1443 	 */
1444 	if (!sample->period)
1445 		return 0;
1446 
1447 	evsel = container_of(sid->evsel, struct evsel, core);
1448 	return tool->sample(tool, event, sample, evsel, machine);
1449 }
1450 
1451 static int deliver_sample_group(struct evlist *evlist,
1452 				struct perf_tool *tool,
1453 				union  perf_event *event,
1454 				struct perf_sample *sample,
1455 				struct machine *machine)
1456 {
1457 	int ret = -EINVAL;
1458 	u64 i;
1459 
1460 	for (i = 0; i < sample->read.group.nr; i++) {
1461 		ret = deliver_sample_value(evlist, tool, event, sample,
1462 					   &sample->read.group.values[i],
1463 					   machine);
1464 		if (ret)
1465 			break;
1466 	}
1467 
1468 	return ret;
1469 }
1470 
1471 static int evlist__deliver_sample(struct evlist *evlist, struct perf_tool *tool,
1472 				  union  perf_event *event, struct perf_sample *sample,
1473 				  struct evsel *evsel, struct machine *machine)
1474 {
1475 	/* We know evsel != NULL. */
1476 	u64 sample_type = evsel->core.attr.sample_type;
1477 	u64 read_format = evsel->core.attr.read_format;
1478 
1479 	/* Standard sample delivery. */
1480 	if (!(sample_type & PERF_SAMPLE_READ))
1481 		return tool->sample(tool, event, sample, evsel, machine);
1482 
1483 	/* For PERF_SAMPLE_READ we have either single or group mode. */
1484 	if (read_format & PERF_FORMAT_GROUP)
1485 		return deliver_sample_group(evlist, tool, event, sample,
1486 					    machine);
1487 	else
1488 		return deliver_sample_value(evlist, tool, event, sample,
1489 					    &sample->read.one, machine);
1490 }
1491 
1492 static int machines__deliver_event(struct machines *machines,
1493 				   struct evlist *evlist,
1494 				   union perf_event *event,
1495 				   struct perf_sample *sample,
1496 				   struct perf_tool *tool, u64 file_offset,
1497 				   const char *file_path)
1498 {
1499 	struct evsel *evsel;
1500 	struct machine *machine;
1501 
1502 	dump_event(evlist, event, file_offset, sample, file_path);
1503 
1504 	evsel = evlist__id2evsel(evlist, sample->id);
1505 
1506 	machine = machines__find_for_cpumode(machines, event, sample);
1507 
1508 	switch (event->header.type) {
1509 	case PERF_RECORD_SAMPLE:
1510 		if (evsel == NULL) {
1511 			++evlist->stats.nr_unknown_id;
1512 			return 0;
1513 		}
1514 		if (machine == NULL) {
1515 			++evlist->stats.nr_unprocessable_samples;
1516 			dump_sample(evsel, event, sample, perf_env__arch(NULL));
1517 			return 0;
1518 		}
1519 		dump_sample(evsel, event, sample, perf_env__arch(machine->env));
1520 		return evlist__deliver_sample(evlist, tool, event, sample, evsel, machine);
1521 	case PERF_RECORD_MMAP:
1522 		return tool->mmap(tool, event, sample, machine);
1523 	case PERF_RECORD_MMAP2:
1524 		if (event->header.misc & PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT)
1525 			++evlist->stats.nr_proc_map_timeout;
1526 		return tool->mmap2(tool, event, sample, machine);
1527 	case PERF_RECORD_COMM:
1528 		return tool->comm(tool, event, sample, machine);
1529 	case PERF_RECORD_NAMESPACES:
1530 		return tool->namespaces(tool, event, sample, machine);
1531 	case PERF_RECORD_CGROUP:
1532 		return tool->cgroup(tool, event, sample, machine);
1533 	case PERF_RECORD_FORK:
1534 		return tool->fork(tool, event, sample, machine);
1535 	case PERF_RECORD_EXIT:
1536 		return tool->exit(tool, event, sample, machine);
1537 	case PERF_RECORD_LOST:
1538 		if (tool->lost == perf_event__process_lost)
1539 			evlist->stats.total_lost += event->lost.lost;
1540 		return tool->lost(tool, event, sample, machine);
1541 	case PERF_RECORD_LOST_SAMPLES:
1542 		if (tool->lost_samples == perf_event__process_lost_samples)
1543 			evlist->stats.total_lost_samples += event->lost_samples.lost;
1544 		return tool->lost_samples(tool, event, sample, machine);
1545 	case PERF_RECORD_READ:
1546 		dump_read(evsel, event);
1547 		return tool->read(tool, event, sample, evsel, machine);
1548 	case PERF_RECORD_THROTTLE:
1549 		return tool->throttle(tool, event, sample, machine);
1550 	case PERF_RECORD_UNTHROTTLE:
1551 		return tool->unthrottle(tool, event, sample, machine);
1552 	case PERF_RECORD_AUX:
1553 		if (tool->aux == perf_event__process_aux) {
1554 			if (event->aux.flags & PERF_AUX_FLAG_TRUNCATED)
1555 				evlist->stats.total_aux_lost += 1;
1556 			if (event->aux.flags & PERF_AUX_FLAG_PARTIAL)
1557 				evlist->stats.total_aux_partial += 1;
1558 			if (event->aux.flags & PERF_AUX_FLAG_COLLISION)
1559 				evlist->stats.total_aux_collision += 1;
1560 		}
1561 		return tool->aux(tool, event, sample, machine);
1562 	case PERF_RECORD_ITRACE_START:
1563 		return tool->itrace_start(tool, event, sample, machine);
1564 	case PERF_RECORD_SWITCH:
1565 	case PERF_RECORD_SWITCH_CPU_WIDE:
1566 		return tool->context_switch(tool, event, sample, machine);
1567 	case PERF_RECORD_KSYMBOL:
1568 		return tool->ksymbol(tool, event, sample, machine);
1569 	case PERF_RECORD_BPF_EVENT:
1570 		return tool->bpf(tool, event, sample, machine);
1571 	case PERF_RECORD_TEXT_POKE:
1572 		return tool->text_poke(tool, event, sample, machine);
1573 	case PERF_RECORD_AUX_OUTPUT_HW_ID:
1574 		return tool->aux_output_hw_id(tool, event, sample, machine);
1575 	default:
1576 		++evlist->stats.nr_unknown_events;
1577 		return -1;
1578 	}
1579 }
1580 
1581 static int perf_session__deliver_event(struct perf_session *session,
1582 				       union perf_event *event,
1583 				       struct perf_tool *tool,
1584 				       u64 file_offset,
1585 				       const char *file_path)
1586 {
1587 	struct perf_sample sample;
1588 	int ret = evlist__parse_sample(session->evlist, event, &sample);
1589 
1590 	if (ret) {
1591 		pr_err("Can't parse sample, err = %d\n", ret);
1592 		return ret;
1593 	}
1594 
1595 	ret = auxtrace__process_event(session, event, &sample, tool);
1596 	if (ret < 0)
1597 		return ret;
1598 	if (ret > 0)
1599 		return 0;
1600 
1601 	ret = machines__deliver_event(&session->machines, session->evlist,
1602 				      event, &sample, tool, file_offset, file_path);
1603 
1604 	if (dump_trace && sample.aux_sample.size)
1605 		auxtrace__dump_auxtrace_sample(session, &sample);
1606 
1607 	return ret;
1608 }
1609 
1610 static s64 perf_session__process_user_event(struct perf_session *session,
1611 					    union perf_event *event,
1612 					    u64 file_offset,
1613 					    const char *file_path)
1614 {
1615 	struct ordered_events *oe = &session->ordered_events;
1616 	struct perf_tool *tool = session->tool;
1617 	struct perf_sample sample = { .time = 0, };
1618 	int fd = perf_data__fd(session->data);
1619 	int err;
1620 
1621 	if (event->header.type != PERF_RECORD_COMPRESSED ||
1622 	    tool->compressed == perf_session__process_compressed_event_stub)
1623 		dump_event(session->evlist, event, file_offset, &sample, file_path);
1624 
1625 	/* These events are processed right away */
1626 	switch (event->header.type) {
1627 	case PERF_RECORD_HEADER_ATTR:
1628 		err = tool->attr(tool, event, &session->evlist);
1629 		if (err == 0) {
1630 			perf_session__set_id_hdr_size(session);
1631 			perf_session__set_comm_exec(session);
1632 		}
1633 		return err;
1634 	case PERF_RECORD_EVENT_UPDATE:
1635 		return tool->event_update(tool, event, &session->evlist);
1636 	case PERF_RECORD_HEADER_EVENT_TYPE:
1637 		/*
1638 		 * Deprecated, but we need to handle it for sake
1639 		 * of old data files create in pipe mode.
1640 		 */
1641 		return 0;
1642 	case PERF_RECORD_HEADER_TRACING_DATA:
1643 		/*
1644 		 * Setup for reading amidst mmap, but only when we
1645 		 * are in 'file' mode. The 'pipe' fd is in proper
1646 		 * place already.
1647 		 */
1648 		if (!perf_data__is_pipe(session->data))
1649 			lseek(fd, file_offset, SEEK_SET);
1650 		return tool->tracing_data(session, event);
1651 	case PERF_RECORD_HEADER_BUILD_ID:
1652 		return tool->build_id(session, event);
1653 	case PERF_RECORD_FINISHED_ROUND:
1654 		return tool->finished_round(tool, event, oe);
1655 	case PERF_RECORD_ID_INDEX:
1656 		return tool->id_index(session, event);
1657 	case PERF_RECORD_AUXTRACE_INFO:
1658 		return tool->auxtrace_info(session, event);
1659 	case PERF_RECORD_AUXTRACE:
1660 		/* setup for reading amidst mmap */
1661 		lseek(fd, file_offset + event->header.size, SEEK_SET);
1662 		return tool->auxtrace(session, event);
1663 	case PERF_RECORD_AUXTRACE_ERROR:
1664 		perf_session__auxtrace_error_inc(session, event);
1665 		return tool->auxtrace_error(session, event);
1666 	case PERF_RECORD_THREAD_MAP:
1667 		return tool->thread_map(session, event);
1668 	case PERF_RECORD_CPU_MAP:
1669 		return tool->cpu_map(session, event);
1670 	case PERF_RECORD_STAT_CONFIG:
1671 		return tool->stat_config(session, event);
1672 	case PERF_RECORD_STAT:
1673 		return tool->stat(session, event);
1674 	case PERF_RECORD_STAT_ROUND:
1675 		return tool->stat_round(session, event);
1676 	case PERF_RECORD_TIME_CONV:
1677 		session->time_conv = event->time_conv;
1678 		return tool->time_conv(session, event);
1679 	case PERF_RECORD_HEADER_FEATURE:
1680 		return tool->feature(session, event);
1681 	case PERF_RECORD_COMPRESSED:
1682 		err = tool->compressed(session, event, file_offset, file_path);
1683 		if (err)
1684 			dump_event(session->evlist, event, file_offset, &sample, file_path);
1685 		return err;
1686 	default:
1687 		return -EINVAL;
1688 	}
1689 }
1690 
1691 int perf_session__deliver_synth_event(struct perf_session *session,
1692 				      union perf_event *event,
1693 				      struct perf_sample *sample)
1694 {
1695 	struct evlist *evlist = session->evlist;
1696 	struct perf_tool *tool = session->tool;
1697 
1698 	events_stats__inc(&evlist->stats, event->header.type);
1699 
1700 	if (event->header.type >= PERF_RECORD_USER_TYPE_START)
1701 		return perf_session__process_user_event(session, event, 0, NULL);
1702 
1703 	return machines__deliver_event(&session->machines, evlist, event, sample, tool, 0, NULL);
1704 }
1705 
1706 static void event_swap(union perf_event *event, bool sample_id_all)
1707 {
1708 	perf_event__swap_op swap;
1709 
1710 	swap = perf_event__swap_ops[event->header.type];
1711 	if (swap)
1712 		swap(event, sample_id_all);
1713 }
1714 
1715 int perf_session__peek_event(struct perf_session *session, off_t file_offset,
1716 			     void *buf, size_t buf_sz,
1717 			     union perf_event **event_ptr,
1718 			     struct perf_sample *sample)
1719 {
1720 	union perf_event *event;
1721 	size_t hdr_sz, rest;
1722 	int fd;
1723 
1724 	if (session->one_mmap && !session->header.needs_swap) {
1725 		event = file_offset - session->one_mmap_offset +
1726 			session->one_mmap_addr;
1727 		goto out_parse_sample;
1728 	}
1729 
1730 	if (perf_data__is_pipe(session->data))
1731 		return -1;
1732 
1733 	fd = perf_data__fd(session->data);
1734 	hdr_sz = sizeof(struct perf_event_header);
1735 
1736 	if (buf_sz < hdr_sz)
1737 		return -1;
1738 
1739 	if (lseek(fd, file_offset, SEEK_SET) == (off_t)-1 ||
1740 	    readn(fd, buf, hdr_sz) != (ssize_t)hdr_sz)
1741 		return -1;
1742 
1743 	event = (union perf_event *)buf;
1744 
1745 	if (session->header.needs_swap)
1746 		perf_event_header__bswap(&event->header);
1747 
1748 	if (event->header.size < hdr_sz || event->header.size > buf_sz)
1749 		return -1;
1750 
1751 	buf += hdr_sz;
1752 	rest = event->header.size - hdr_sz;
1753 
1754 	if (readn(fd, buf, rest) != (ssize_t)rest)
1755 		return -1;
1756 
1757 	if (session->header.needs_swap)
1758 		event_swap(event, evlist__sample_id_all(session->evlist));
1759 
1760 out_parse_sample:
1761 
1762 	if (sample && event->header.type < PERF_RECORD_USER_TYPE_START &&
1763 	    evlist__parse_sample(session->evlist, event, sample))
1764 		return -1;
1765 
1766 	*event_ptr = event;
1767 
1768 	return 0;
1769 }
1770 
1771 int perf_session__peek_events(struct perf_session *session, u64 offset,
1772 			      u64 size, peek_events_cb_t cb, void *data)
1773 {
1774 	u64 max_offset = offset + size;
1775 	char buf[PERF_SAMPLE_MAX_SIZE];
1776 	union perf_event *event;
1777 	int err;
1778 
1779 	do {
1780 		err = perf_session__peek_event(session, offset, buf,
1781 					       PERF_SAMPLE_MAX_SIZE, &event,
1782 					       NULL);
1783 		if (err)
1784 			return err;
1785 
1786 		err = cb(session, event, offset, data);
1787 		if (err)
1788 			return err;
1789 
1790 		offset += event->header.size;
1791 		if (event->header.type == PERF_RECORD_AUXTRACE)
1792 			offset += event->auxtrace.size;
1793 
1794 	} while (offset < max_offset);
1795 
1796 	return err;
1797 }
1798 
1799 static s64 perf_session__process_event(struct perf_session *session,
1800 				       union perf_event *event, u64 file_offset,
1801 				       const char *file_path)
1802 {
1803 	struct evlist *evlist = session->evlist;
1804 	struct perf_tool *tool = session->tool;
1805 	int ret;
1806 
1807 	if (session->header.needs_swap)
1808 		event_swap(event, evlist__sample_id_all(evlist));
1809 
1810 	if (event->header.type >= PERF_RECORD_HEADER_MAX)
1811 		return -EINVAL;
1812 
1813 	events_stats__inc(&evlist->stats, event->header.type);
1814 
1815 	if (event->header.type >= PERF_RECORD_USER_TYPE_START)
1816 		return perf_session__process_user_event(session, event, file_offset, file_path);
1817 
1818 	if (tool->ordered_events) {
1819 		u64 timestamp = -1ULL;
1820 
1821 		ret = evlist__parse_sample_timestamp(evlist, event, &timestamp);
1822 		if (ret && ret != -1)
1823 			return ret;
1824 
1825 		ret = perf_session__queue_event(session, event, timestamp, file_offset, file_path);
1826 		if (ret != -ETIME)
1827 			return ret;
1828 	}
1829 
1830 	return perf_session__deliver_event(session, event, tool, file_offset, file_path);
1831 }
1832 
1833 void perf_event_header__bswap(struct perf_event_header *hdr)
1834 {
1835 	hdr->type = bswap_32(hdr->type);
1836 	hdr->misc = bswap_16(hdr->misc);
1837 	hdr->size = bswap_16(hdr->size);
1838 }
1839 
1840 struct thread *perf_session__findnew(struct perf_session *session, pid_t pid)
1841 {
1842 	return machine__findnew_thread(&session->machines.host, -1, pid);
1843 }
1844 
1845 int perf_session__register_idle_thread(struct perf_session *session)
1846 {
1847 	struct thread *thread = machine__idle_thread(&session->machines.host);
1848 
1849 	/* machine__idle_thread() got the thread, so put it */
1850 	thread__put(thread);
1851 	return thread ? 0 : -1;
1852 }
1853 
1854 static void
1855 perf_session__warn_order(const struct perf_session *session)
1856 {
1857 	const struct ordered_events *oe = &session->ordered_events;
1858 	struct evsel *evsel;
1859 	bool should_warn = true;
1860 
1861 	evlist__for_each_entry(session->evlist, evsel) {
1862 		if (evsel->core.attr.write_backward)
1863 			should_warn = false;
1864 	}
1865 
1866 	if (!should_warn)
1867 		return;
1868 	if (oe->nr_unordered_events != 0)
1869 		ui__warning("%u out of order events recorded.\n", oe->nr_unordered_events);
1870 }
1871 
1872 static void perf_session__warn_about_errors(const struct perf_session *session)
1873 {
1874 	const struct events_stats *stats = &session->evlist->stats;
1875 
1876 	if (session->tool->lost == perf_event__process_lost &&
1877 	    stats->nr_events[PERF_RECORD_LOST] != 0) {
1878 		ui__warning("Processed %d events and lost %d chunks!\n\n"
1879 			    "Check IO/CPU overload!\n\n",
1880 			    stats->nr_events[0],
1881 			    stats->nr_events[PERF_RECORD_LOST]);
1882 	}
1883 
1884 	if (session->tool->lost_samples == perf_event__process_lost_samples) {
1885 		double drop_rate;
1886 
1887 		drop_rate = (double)stats->total_lost_samples /
1888 			    (double) (stats->nr_events[PERF_RECORD_SAMPLE] + stats->total_lost_samples);
1889 		if (drop_rate > 0.05) {
1890 			ui__warning("Processed %" PRIu64 " samples and lost %3.2f%%!\n\n",
1891 				    stats->nr_events[PERF_RECORD_SAMPLE] + stats->total_lost_samples,
1892 				    drop_rate * 100.0);
1893 		}
1894 	}
1895 
1896 	if (session->tool->aux == perf_event__process_aux &&
1897 	    stats->total_aux_lost != 0) {
1898 		ui__warning("AUX data lost %" PRIu64 " times out of %u!\n\n",
1899 			    stats->total_aux_lost,
1900 			    stats->nr_events[PERF_RECORD_AUX]);
1901 	}
1902 
1903 	if (session->tool->aux == perf_event__process_aux &&
1904 	    stats->total_aux_partial != 0) {
1905 		bool vmm_exclusive = false;
1906 
1907 		(void)sysfs__read_bool("module/kvm_intel/parameters/vmm_exclusive",
1908 		                       &vmm_exclusive);
1909 
1910 		ui__warning("AUX data had gaps in it %" PRIu64 " times out of %u!\n\n"
1911 		            "Are you running a KVM guest in the background?%s\n\n",
1912 			    stats->total_aux_partial,
1913 			    stats->nr_events[PERF_RECORD_AUX],
1914 			    vmm_exclusive ?
1915 			    "\nReloading kvm_intel module with vmm_exclusive=0\n"
1916 			    "will reduce the gaps to only guest's timeslices." :
1917 			    "");
1918 	}
1919 
1920 	if (session->tool->aux == perf_event__process_aux &&
1921 	    stats->total_aux_collision != 0) {
1922 		ui__warning("AUX data detected collision  %" PRIu64 " times out of %u!\n\n",
1923 			    stats->total_aux_collision,
1924 			    stats->nr_events[PERF_RECORD_AUX]);
1925 	}
1926 
1927 	if (stats->nr_unknown_events != 0) {
1928 		ui__warning("Found %u unknown events!\n\n"
1929 			    "Is this an older tool processing a perf.data "
1930 			    "file generated by a more recent tool?\n\n"
1931 			    "If that is not the case, consider "
1932 			    "reporting to linux-kernel@vger.kernel.org.\n\n",
1933 			    stats->nr_unknown_events);
1934 	}
1935 
1936 	if (stats->nr_unknown_id != 0) {
1937 		ui__warning("%u samples with id not present in the header\n",
1938 			    stats->nr_unknown_id);
1939 	}
1940 
1941 	if (stats->nr_invalid_chains != 0) {
1942 		ui__warning("Found invalid callchains!\n\n"
1943 			    "%u out of %u events were discarded for this reason.\n\n"
1944 			    "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
1945 			    stats->nr_invalid_chains,
1946 			    stats->nr_events[PERF_RECORD_SAMPLE]);
1947 	}
1948 
1949 	if (stats->nr_unprocessable_samples != 0) {
1950 		ui__warning("%u unprocessable samples recorded.\n"
1951 			    "Do you have a KVM guest running and not using 'perf kvm'?\n",
1952 			    stats->nr_unprocessable_samples);
1953 	}
1954 
1955 	perf_session__warn_order(session);
1956 
1957 	events_stats__auxtrace_error_warn(stats);
1958 
1959 	if (stats->nr_proc_map_timeout != 0) {
1960 		ui__warning("%d map information files for pre-existing threads were\n"
1961 			    "not processed, if there are samples for addresses they\n"
1962 			    "will not be resolved, you may find out which are these\n"
1963 			    "threads by running with -v and redirecting the output\n"
1964 			    "to a file.\n"
1965 			    "The time limit to process proc map is too short?\n"
1966 			    "Increase it by --proc-map-timeout\n",
1967 			    stats->nr_proc_map_timeout);
1968 	}
1969 }
1970 
1971 static int perf_session__flush_thread_stack(struct thread *thread,
1972 					    void *p __maybe_unused)
1973 {
1974 	return thread_stack__flush(thread);
1975 }
1976 
1977 static int perf_session__flush_thread_stacks(struct perf_session *session)
1978 {
1979 	return machines__for_each_thread(&session->machines,
1980 					 perf_session__flush_thread_stack,
1981 					 NULL);
1982 }
1983 
1984 volatile int session_done;
1985 
1986 static int __perf_session__process_decomp_events(struct perf_session *session);
1987 
1988 static int __perf_session__process_pipe_events(struct perf_session *session)
1989 {
1990 	struct ordered_events *oe = &session->ordered_events;
1991 	struct perf_tool *tool = session->tool;
1992 	union perf_event *event;
1993 	uint32_t size, cur_size = 0;
1994 	void *buf = NULL;
1995 	s64 skip = 0;
1996 	u64 head;
1997 	ssize_t err;
1998 	void *p;
1999 
2000 	perf_tool__fill_defaults(tool);
2001 
2002 	head = 0;
2003 	cur_size = sizeof(union perf_event);
2004 
2005 	buf = malloc(cur_size);
2006 	if (!buf)
2007 		return -errno;
2008 	ordered_events__set_copy_on_queue(oe, true);
2009 more:
2010 	event = buf;
2011 	err = perf_data__read(session->data, event,
2012 			      sizeof(struct perf_event_header));
2013 	if (err <= 0) {
2014 		if (err == 0)
2015 			goto done;
2016 
2017 		pr_err("failed to read event header\n");
2018 		goto out_err;
2019 	}
2020 
2021 	if (session->header.needs_swap)
2022 		perf_event_header__bswap(&event->header);
2023 
2024 	size = event->header.size;
2025 	if (size < sizeof(struct perf_event_header)) {
2026 		pr_err("bad event header size\n");
2027 		goto out_err;
2028 	}
2029 
2030 	if (size > cur_size) {
2031 		void *new = realloc(buf, size);
2032 		if (!new) {
2033 			pr_err("failed to allocate memory to read event\n");
2034 			goto out_err;
2035 		}
2036 		buf = new;
2037 		cur_size = size;
2038 		event = buf;
2039 	}
2040 	p = event;
2041 	p += sizeof(struct perf_event_header);
2042 
2043 	if (size - sizeof(struct perf_event_header)) {
2044 		err = perf_data__read(session->data, p,
2045 				      size - sizeof(struct perf_event_header));
2046 		if (err <= 0) {
2047 			if (err == 0) {
2048 				pr_err("unexpected end of event stream\n");
2049 				goto done;
2050 			}
2051 
2052 			pr_err("failed to read event data\n");
2053 			goto out_err;
2054 		}
2055 	}
2056 
2057 	if ((skip = perf_session__process_event(session, event, head, "pipe")) < 0) {
2058 		pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
2059 		       head, event->header.size, event->header.type);
2060 		err = -EINVAL;
2061 		goto out_err;
2062 	}
2063 
2064 	head += size;
2065 
2066 	if (skip > 0)
2067 		head += skip;
2068 
2069 	err = __perf_session__process_decomp_events(session);
2070 	if (err)
2071 		goto out_err;
2072 
2073 	if (!session_done())
2074 		goto more;
2075 done:
2076 	/* do the final flush for ordered samples */
2077 	err = ordered_events__flush(oe, OE_FLUSH__FINAL);
2078 	if (err)
2079 		goto out_err;
2080 	err = auxtrace__flush_events(session, tool);
2081 	if (err)
2082 		goto out_err;
2083 	err = perf_session__flush_thread_stacks(session);
2084 out_err:
2085 	free(buf);
2086 	if (!tool->no_warn)
2087 		perf_session__warn_about_errors(session);
2088 	ordered_events__free(&session->ordered_events);
2089 	auxtrace__free_events(session);
2090 	return err;
2091 }
2092 
2093 static union perf_event *
2094 prefetch_event(char *buf, u64 head, size_t mmap_size,
2095 	       bool needs_swap, union perf_event *error)
2096 {
2097 	union perf_event *event;
2098 
2099 	/*
2100 	 * Ensure we have enough space remaining to read
2101 	 * the size of the event in the headers.
2102 	 */
2103 	if (head + sizeof(event->header) > mmap_size)
2104 		return NULL;
2105 
2106 	event = (union perf_event *)(buf + head);
2107 	if (needs_swap)
2108 		perf_event_header__bswap(&event->header);
2109 
2110 	if (head + event->header.size <= mmap_size)
2111 		return event;
2112 
2113 	/* We're not fetching the event so swap back again */
2114 	if (needs_swap)
2115 		perf_event_header__bswap(&event->header);
2116 
2117 	pr_debug("%s: head=%#" PRIx64 " event->header_size=%#x, mmap_size=%#zx:"
2118 		 " fuzzed or compressed perf.data?\n",__func__, head, event->header.size, mmap_size);
2119 
2120 	return error;
2121 }
2122 
2123 static union perf_event *
2124 fetch_mmaped_event(u64 head, size_t mmap_size, char *buf, bool needs_swap)
2125 {
2126 	return prefetch_event(buf, head, mmap_size, needs_swap, ERR_PTR(-EINVAL));
2127 }
2128 
2129 static union perf_event *
2130 fetch_decomp_event(u64 head, size_t mmap_size, char *buf, bool needs_swap)
2131 {
2132 	return prefetch_event(buf, head, mmap_size, needs_swap, NULL);
2133 }
2134 
2135 static int __perf_session__process_decomp_events(struct perf_session *session)
2136 {
2137 	s64 skip;
2138 	u64 size;
2139 	struct decomp *decomp = session->active_decomp->decomp_last;
2140 
2141 	if (!decomp)
2142 		return 0;
2143 
2144 	while (decomp->head < decomp->size && !session_done()) {
2145 		union perf_event *event = fetch_decomp_event(decomp->head, decomp->size, decomp->data,
2146 							     session->header.needs_swap);
2147 
2148 		if (!event)
2149 			break;
2150 
2151 		size = event->header.size;
2152 
2153 		if (size < sizeof(struct perf_event_header) ||
2154 		    (skip = perf_session__process_event(session, event, decomp->file_pos,
2155 							decomp->file_path)) < 0) {
2156 			pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
2157 				decomp->file_pos + decomp->head, event->header.size, event->header.type);
2158 			return -EINVAL;
2159 		}
2160 
2161 		if (skip)
2162 			size += skip;
2163 
2164 		decomp->head += size;
2165 	}
2166 
2167 	return 0;
2168 }
2169 
2170 /*
2171  * On 64bit we can mmap the data file in one go. No need for tiny mmap
2172  * slices. On 32bit we use 32MB.
2173  */
2174 #if BITS_PER_LONG == 64
2175 #define MMAP_SIZE ULLONG_MAX
2176 #define NUM_MMAPS 1
2177 #else
2178 #define MMAP_SIZE (32 * 1024 * 1024ULL)
2179 #define NUM_MMAPS 128
2180 #endif
2181 
2182 struct reader;
2183 
2184 typedef s64 (*reader_cb_t)(struct perf_session *session,
2185 			   union perf_event *event,
2186 			   u64 file_offset,
2187 			   const char *file_path);
2188 
2189 struct reader {
2190 	int		 fd;
2191 	const char	 *path;
2192 	u64		 data_size;
2193 	u64		 data_offset;
2194 	reader_cb_t	 process;
2195 	bool		 in_place_update;
2196 	char		 *mmaps[NUM_MMAPS];
2197 	size_t		 mmap_size;
2198 	int		 mmap_idx;
2199 	char		 *mmap_cur;
2200 	u64		 file_pos;
2201 	u64		 file_offset;
2202 	u64		 head;
2203 	u64		 size;
2204 	bool		 done;
2205 	struct zstd_data   zstd_data;
2206 	struct decomp_data decomp_data;
2207 };
2208 
2209 static int
2210 reader__init(struct reader *rd, bool *one_mmap)
2211 {
2212 	u64 data_size = rd->data_size;
2213 	char **mmaps = rd->mmaps;
2214 
2215 	rd->head = rd->data_offset;
2216 	data_size += rd->data_offset;
2217 
2218 	rd->mmap_size = MMAP_SIZE;
2219 	if (rd->mmap_size > data_size) {
2220 		rd->mmap_size = data_size;
2221 		if (one_mmap)
2222 			*one_mmap = true;
2223 	}
2224 
2225 	memset(mmaps, 0, sizeof(rd->mmaps));
2226 
2227 	if (zstd_init(&rd->zstd_data, 0))
2228 		return -1;
2229 	rd->decomp_data.zstd_decomp = &rd->zstd_data;
2230 
2231 	return 0;
2232 }
2233 
2234 static void
2235 reader__release_decomp(struct reader *rd)
2236 {
2237 	perf_decomp__release_events(rd->decomp_data.decomp);
2238 	zstd_fini(&rd->zstd_data);
2239 }
2240 
2241 static int
2242 reader__mmap(struct reader *rd, struct perf_session *session)
2243 {
2244 	int mmap_prot, mmap_flags;
2245 	char *buf, **mmaps = rd->mmaps;
2246 	u64 page_offset;
2247 
2248 	mmap_prot  = PROT_READ;
2249 	mmap_flags = MAP_SHARED;
2250 
2251 	if (rd->in_place_update) {
2252 		mmap_prot  |= PROT_WRITE;
2253 	} else if (session->header.needs_swap) {
2254 		mmap_prot  |= PROT_WRITE;
2255 		mmap_flags = MAP_PRIVATE;
2256 	}
2257 
2258 	if (mmaps[rd->mmap_idx]) {
2259 		munmap(mmaps[rd->mmap_idx], rd->mmap_size);
2260 		mmaps[rd->mmap_idx] = NULL;
2261 	}
2262 
2263 	page_offset = page_size * (rd->head / page_size);
2264 	rd->file_offset += page_offset;
2265 	rd->head -= page_offset;
2266 
2267 	buf = mmap(NULL, rd->mmap_size, mmap_prot, mmap_flags, rd->fd,
2268 		   rd->file_offset);
2269 	if (buf == MAP_FAILED) {
2270 		pr_err("failed to mmap file\n");
2271 		return -errno;
2272 	}
2273 	mmaps[rd->mmap_idx] = rd->mmap_cur = buf;
2274 	rd->mmap_idx = (rd->mmap_idx + 1) & (ARRAY_SIZE(rd->mmaps) - 1);
2275 	rd->file_pos = rd->file_offset + rd->head;
2276 	if (session->one_mmap) {
2277 		session->one_mmap_addr = buf;
2278 		session->one_mmap_offset = rd->file_offset;
2279 	}
2280 
2281 	return 0;
2282 }
2283 
2284 enum {
2285 	READER_OK,
2286 	READER_NODATA,
2287 };
2288 
2289 static int
2290 reader__read_event(struct reader *rd, struct perf_session *session,
2291 		   struct ui_progress *prog)
2292 {
2293 	u64 size;
2294 	int err = READER_OK;
2295 	union perf_event *event;
2296 	s64 skip;
2297 
2298 	event = fetch_mmaped_event(rd->head, rd->mmap_size, rd->mmap_cur,
2299 				   session->header.needs_swap);
2300 	if (IS_ERR(event))
2301 		return PTR_ERR(event);
2302 
2303 	if (!event)
2304 		return READER_NODATA;
2305 
2306 	size = event->header.size;
2307 
2308 	skip = -EINVAL;
2309 
2310 	if (size < sizeof(struct perf_event_header) ||
2311 	    (skip = rd->process(session, event, rd->file_pos, rd->path)) < 0) {
2312 		pr_err("%#" PRIx64 " [%#x]: failed to process type: %d [%s]\n",
2313 		       rd->file_offset + rd->head, event->header.size,
2314 		       event->header.type, strerror(-skip));
2315 		err = skip;
2316 		goto out;
2317 	}
2318 
2319 	if (skip)
2320 		size += skip;
2321 
2322 	rd->size += size;
2323 	rd->head += size;
2324 	rd->file_pos += size;
2325 
2326 	err = __perf_session__process_decomp_events(session);
2327 	if (err)
2328 		goto out;
2329 
2330 	ui_progress__update(prog, size);
2331 
2332 out:
2333 	return err;
2334 }
2335 
2336 static inline bool
2337 reader__eof(struct reader *rd)
2338 {
2339 	return (rd->file_pos >= rd->data_size + rd->data_offset);
2340 }
2341 
2342 static int
2343 reader__process_events(struct reader *rd, struct perf_session *session,
2344 		       struct ui_progress *prog)
2345 {
2346 	int err;
2347 
2348 	err = reader__init(rd, &session->one_mmap);
2349 	if (err)
2350 		goto out;
2351 
2352 	session->active_decomp = &rd->decomp_data;
2353 
2354 remap:
2355 	err = reader__mmap(rd, session);
2356 	if (err)
2357 		goto out;
2358 
2359 more:
2360 	err = reader__read_event(rd, session, prog);
2361 	if (err < 0)
2362 		goto out;
2363 	else if (err == READER_NODATA)
2364 		goto remap;
2365 
2366 	if (session_done())
2367 		goto out;
2368 
2369 	if (!reader__eof(rd))
2370 		goto more;
2371 
2372 out:
2373 	session->active_decomp = &session->decomp_data;
2374 	return err;
2375 }
2376 
2377 static s64 process_simple(struct perf_session *session,
2378 			  union perf_event *event,
2379 			  u64 file_offset,
2380 			  const char *file_path)
2381 {
2382 	return perf_session__process_event(session, event, file_offset, file_path);
2383 }
2384 
2385 static int __perf_session__process_events(struct perf_session *session)
2386 {
2387 	struct reader rd = {
2388 		.fd		= perf_data__fd(session->data),
2389 		.path		= session->data->file.path,
2390 		.data_size	= session->header.data_size,
2391 		.data_offset	= session->header.data_offset,
2392 		.process	= process_simple,
2393 		.in_place_update = session->data->in_place_update,
2394 	};
2395 	struct ordered_events *oe = &session->ordered_events;
2396 	struct perf_tool *tool = session->tool;
2397 	struct ui_progress prog;
2398 	int err;
2399 
2400 	perf_tool__fill_defaults(tool);
2401 
2402 	if (rd.data_size == 0)
2403 		return -1;
2404 
2405 	ui_progress__init_size(&prog, rd.data_size, "Processing events...");
2406 
2407 	err = reader__process_events(&rd, session, &prog);
2408 	if (err)
2409 		goto out_err;
2410 	/* do the final flush for ordered samples */
2411 	err = ordered_events__flush(oe, OE_FLUSH__FINAL);
2412 	if (err)
2413 		goto out_err;
2414 	err = auxtrace__flush_events(session, tool);
2415 	if (err)
2416 		goto out_err;
2417 	err = perf_session__flush_thread_stacks(session);
2418 out_err:
2419 	ui_progress__finish();
2420 	if (!tool->no_warn)
2421 		perf_session__warn_about_errors(session);
2422 	/*
2423 	 * We may switching perf.data output, make ordered_events
2424 	 * reusable.
2425 	 */
2426 	ordered_events__reinit(&session->ordered_events);
2427 	auxtrace__free_events(session);
2428 	reader__release_decomp(&rd);
2429 	session->one_mmap = false;
2430 	return err;
2431 }
2432 
2433 /*
2434  * Processing 2 MB of data from each reader in sequence,
2435  * because that's the way the ordered events sorting works
2436  * most efficiently.
2437  */
2438 #define READER_MAX_SIZE (2 * 1024 * 1024)
2439 
2440 /*
2441  * This function reads, merge and process directory data.
2442  * It assumens the version 1 of directory data, where each
2443  * data file holds per-cpu data, already sorted by kernel.
2444  */
2445 static int __perf_session__process_dir_events(struct perf_session *session)
2446 {
2447 	struct perf_data *data = session->data;
2448 	struct perf_tool *tool = session->tool;
2449 	int i, ret, readers, nr_readers;
2450 	struct ui_progress prog;
2451 	u64 total_size = perf_data__size(session->data);
2452 	struct reader *rd;
2453 
2454 	perf_tool__fill_defaults(tool);
2455 
2456 	ui_progress__init_size(&prog, total_size, "Sorting events...");
2457 
2458 	nr_readers = 1;
2459 	for (i = 0; i < data->dir.nr; i++) {
2460 		if (data->dir.files[i].size)
2461 			nr_readers++;
2462 	}
2463 
2464 	rd = zalloc(nr_readers * sizeof(struct reader));
2465 	if (!rd)
2466 		return -ENOMEM;
2467 
2468 	rd[0] = (struct reader) {
2469 		.fd		 = perf_data__fd(session->data),
2470 		.path		 = session->data->file.path,
2471 		.data_size	 = session->header.data_size,
2472 		.data_offset	 = session->header.data_offset,
2473 		.process	 = process_simple,
2474 		.in_place_update = session->data->in_place_update,
2475 	};
2476 	ret = reader__init(&rd[0], NULL);
2477 	if (ret)
2478 		goto out_err;
2479 	ret = reader__mmap(&rd[0], session);
2480 	if (ret)
2481 		goto out_err;
2482 	readers = 1;
2483 
2484 	for (i = 0; i < data->dir.nr; i++) {
2485 		if (!data->dir.files[i].size)
2486 			continue;
2487 		rd[readers] = (struct reader) {
2488 			.fd		 = data->dir.files[i].fd,
2489 			.path		 = data->dir.files[i].path,
2490 			.data_size	 = data->dir.files[i].size,
2491 			.data_offset	 = 0,
2492 			.process	 = process_simple,
2493 			.in_place_update = session->data->in_place_update,
2494 		};
2495 		ret = reader__init(&rd[readers], NULL);
2496 		if (ret)
2497 			goto out_err;
2498 		ret = reader__mmap(&rd[readers], session);
2499 		if (ret)
2500 			goto out_err;
2501 		readers++;
2502 	}
2503 
2504 	i = 0;
2505 	while (readers) {
2506 		if (session_done())
2507 			break;
2508 
2509 		if (rd[i].done) {
2510 			i = (i + 1) % nr_readers;
2511 			continue;
2512 		}
2513 		if (reader__eof(&rd[i])) {
2514 			rd[i].done = true;
2515 			readers--;
2516 			continue;
2517 		}
2518 
2519 		session->active_decomp = &rd[i].decomp_data;
2520 		ret = reader__read_event(&rd[i], session, &prog);
2521 		if (ret < 0) {
2522 			goto out_err;
2523 		} else if (ret == READER_NODATA) {
2524 			ret = reader__mmap(&rd[i], session);
2525 			if (ret)
2526 				goto out_err;
2527 		}
2528 
2529 		if (rd[i].size >= READER_MAX_SIZE) {
2530 			rd[i].size = 0;
2531 			i = (i + 1) % nr_readers;
2532 		}
2533 	}
2534 
2535 	ret = ordered_events__flush(&session->ordered_events, OE_FLUSH__FINAL);
2536 	if (ret)
2537 		goto out_err;
2538 
2539 	ret = perf_session__flush_thread_stacks(session);
2540 out_err:
2541 	ui_progress__finish();
2542 
2543 	if (!tool->no_warn)
2544 		perf_session__warn_about_errors(session);
2545 
2546 	/*
2547 	 * We may switching perf.data output, make ordered_events
2548 	 * reusable.
2549 	 */
2550 	ordered_events__reinit(&session->ordered_events);
2551 
2552 	session->one_mmap = false;
2553 
2554 	session->active_decomp = &session->decomp_data;
2555 	for (i = 0; i < nr_readers; i++)
2556 		reader__release_decomp(&rd[i]);
2557 	zfree(&rd);
2558 
2559 	return ret;
2560 }
2561 
2562 int perf_session__process_events(struct perf_session *session)
2563 {
2564 	if (perf_session__register_idle_thread(session) < 0)
2565 		return -ENOMEM;
2566 
2567 	if (perf_data__is_pipe(session->data))
2568 		return __perf_session__process_pipe_events(session);
2569 
2570 	if (perf_data__is_dir(session->data))
2571 		return __perf_session__process_dir_events(session);
2572 
2573 	return __perf_session__process_events(session);
2574 }
2575 
2576 bool perf_session__has_traces(struct perf_session *session, const char *msg)
2577 {
2578 	struct evsel *evsel;
2579 
2580 	evlist__for_each_entry(session->evlist, evsel) {
2581 		if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT)
2582 			return true;
2583 	}
2584 
2585 	pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
2586 	return false;
2587 }
2588 
2589 int map__set_kallsyms_ref_reloc_sym(struct map *map, const char *symbol_name, u64 addr)
2590 {
2591 	char *bracket;
2592 	struct ref_reloc_sym *ref;
2593 	struct kmap *kmap;
2594 
2595 	ref = zalloc(sizeof(struct ref_reloc_sym));
2596 	if (ref == NULL)
2597 		return -ENOMEM;
2598 
2599 	ref->name = strdup(symbol_name);
2600 	if (ref->name == NULL) {
2601 		free(ref);
2602 		return -ENOMEM;
2603 	}
2604 
2605 	bracket = strchr(ref->name, ']');
2606 	if (bracket)
2607 		*bracket = '\0';
2608 
2609 	ref->addr = addr;
2610 
2611 	kmap = map__kmap(map);
2612 	if (kmap)
2613 		kmap->ref_reloc_sym = ref;
2614 
2615 	return 0;
2616 }
2617 
2618 size_t perf_session__fprintf_dsos(struct perf_session *session, FILE *fp)
2619 {
2620 	return machines__fprintf_dsos(&session->machines, fp);
2621 }
2622 
2623 size_t perf_session__fprintf_dsos_buildid(struct perf_session *session, FILE *fp,
2624 					  bool (skip)(struct dso *dso, int parm), int parm)
2625 {
2626 	return machines__fprintf_dsos_buildid(&session->machines, fp, skip, parm);
2627 }
2628 
2629 size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp,
2630 				       bool skip_empty)
2631 {
2632 	size_t ret;
2633 	const char *msg = "";
2634 
2635 	if (perf_header__has_feat(&session->header, HEADER_AUXTRACE))
2636 		msg = " (excludes AUX area (e.g. instruction trace) decoded / synthesized events)";
2637 
2638 	ret = fprintf(fp, "\nAggregated stats:%s\n", msg);
2639 
2640 	ret += events_stats__fprintf(&session->evlist->stats, fp, skip_empty);
2641 	return ret;
2642 }
2643 
2644 size_t perf_session__fprintf(struct perf_session *session, FILE *fp)
2645 {
2646 	/*
2647 	 * FIXME: Here we have to actually print all the machines in this
2648 	 * session, not just the host...
2649 	 */
2650 	return machine__fprintf(&session->machines.host, fp);
2651 }
2652 
2653 struct evsel *perf_session__find_first_evtype(struct perf_session *session,
2654 					      unsigned int type)
2655 {
2656 	struct evsel *pos;
2657 
2658 	evlist__for_each_entry(session->evlist, pos) {
2659 		if (pos->core.attr.type == type)
2660 			return pos;
2661 	}
2662 	return NULL;
2663 }
2664 
2665 int perf_session__cpu_bitmap(struct perf_session *session,
2666 			     const char *cpu_list, unsigned long *cpu_bitmap)
2667 {
2668 	int i, err = -1;
2669 	struct perf_cpu_map *map;
2670 	int nr_cpus = min(session->header.env.nr_cpus_avail, MAX_NR_CPUS);
2671 
2672 	for (i = 0; i < PERF_TYPE_MAX; ++i) {
2673 		struct evsel *evsel;
2674 
2675 		evsel = perf_session__find_first_evtype(session, i);
2676 		if (!evsel)
2677 			continue;
2678 
2679 		if (!(evsel->core.attr.sample_type & PERF_SAMPLE_CPU)) {
2680 			pr_err("File does not contain CPU events. "
2681 			       "Remove -C option to proceed.\n");
2682 			return -1;
2683 		}
2684 	}
2685 
2686 	map = perf_cpu_map__new(cpu_list);
2687 	if (map == NULL) {
2688 		pr_err("Invalid cpu_list\n");
2689 		return -1;
2690 	}
2691 
2692 	for (i = 0; i < perf_cpu_map__nr(map); i++) {
2693 		struct perf_cpu cpu = perf_cpu_map__cpu(map, i);
2694 
2695 		if (cpu.cpu >= nr_cpus) {
2696 			pr_err("Requested CPU %d too large. "
2697 			       "Consider raising MAX_NR_CPUS\n", cpu.cpu);
2698 			goto out_delete_map;
2699 		}
2700 
2701 		set_bit(cpu.cpu, cpu_bitmap);
2702 	}
2703 
2704 	err = 0;
2705 
2706 out_delete_map:
2707 	perf_cpu_map__put(map);
2708 	return err;
2709 }
2710 
2711 void perf_session__fprintf_info(struct perf_session *session, FILE *fp,
2712 				bool full)
2713 {
2714 	if (session == NULL || fp == NULL)
2715 		return;
2716 
2717 	fprintf(fp, "# ========\n");
2718 	perf_header__fprintf_info(session, fp, full);
2719 	fprintf(fp, "# ========\n#\n");
2720 }
2721 
2722 int perf_event__process_id_index(struct perf_session *session,
2723 				 union perf_event *event)
2724 {
2725 	struct evlist *evlist = session->evlist;
2726 	struct perf_record_id_index *ie = &event->id_index;
2727 	size_t i, nr, max_nr;
2728 
2729 	max_nr = (ie->header.size - sizeof(struct perf_record_id_index)) /
2730 		 sizeof(struct id_index_entry);
2731 	nr = ie->nr;
2732 	if (nr > max_nr)
2733 		return -EINVAL;
2734 
2735 	if (dump_trace)
2736 		fprintf(stdout, " nr: %zu\n", nr);
2737 
2738 	for (i = 0; i < nr; i++) {
2739 		struct id_index_entry *e = &ie->entries[i];
2740 		struct perf_sample_id *sid;
2741 
2742 		if (dump_trace) {
2743 			fprintf(stdout,	" ... id: %"PRI_lu64, e->id);
2744 			fprintf(stdout,	"  idx: %"PRI_lu64, e->idx);
2745 			fprintf(stdout,	"  cpu: %"PRI_ld64, e->cpu);
2746 			fprintf(stdout,	"  tid: %"PRI_ld64"\n", e->tid);
2747 		}
2748 
2749 		sid = evlist__id2sid(evlist, e->id);
2750 		if (!sid)
2751 			return -ENOENT;
2752 		sid->idx = e->idx;
2753 		sid->cpu.cpu = e->cpu;
2754 		sid->tid = e->tid;
2755 	}
2756 	return 0;
2757 }
2758