xref: /openbmc/linux/tools/perf/util/session.c (revision 827634ad)
1 #include <linux/kernel.h>
2 #include <traceevent/event-parse.h>
3 
4 #include <byteswap.h>
5 #include <unistd.h>
6 #include <sys/types.h>
7 #include <sys/mman.h>
8 
9 #include "evlist.h"
10 #include "evsel.h"
11 #include "session.h"
12 #include "tool.h"
13 #include "sort.h"
14 #include "util.h"
15 #include "cpumap.h"
16 #include "perf_regs.h"
17 #include "asm/bug.h"
18 #include "auxtrace.h"
19 
20 static int perf_session__deliver_event(struct perf_session *session,
21 				       union perf_event *event,
22 				       struct perf_sample *sample,
23 				       struct perf_tool *tool,
24 				       u64 file_offset);
25 
26 static int perf_session__open(struct perf_session *session)
27 {
28 	struct perf_data_file *file = session->file;
29 
30 	if (perf_session__read_header(session) < 0) {
31 		pr_err("incompatible file format (rerun with -v to learn more)");
32 		return -1;
33 	}
34 
35 	if (perf_data_file__is_pipe(file))
36 		return 0;
37 
38 	if (!perf_evlist__valid_sample_type(session->evlist)) {
39 		pr_err("non matching sample_type");
40 		return -1;
41 	}
42 
43 	if (!perf_evlist__valid_sample_id_all(session->evlist)) {
44 		pr_err("non matching sample_id_all");
45 		return -1;
46 	}
47 
48 	if (!perf_evlist__valid_read_format(session->evlist)) {
49 		pr_err("non matching read_format");
50 		return -1;
51 	}
52 
53 	return 0;
54 }
55 
56 void perf_session__set_id_hdr_size(struct perf_session *session)
57 {
58 	u16 id_hdr_size = perf_evlist__id_hdr_size(session->evlist);
59 
60 	machines__set_id_hdr_size(&session->machines, id_hdr_size);
61 }
62 
63 int perf_session__create_kernel_maps(struct perf_session *session)
64 {
65 	int ret = machine__create_kernel_maps(&session->machines.host);
66 
67 	if (ret >= 0)
68 		ret = machines__create_guest_kernel_maps(&session->machines);
69 	return ret;
70 }
71 
72 static void perf_session__destroy_kernel_maps(struct perf_session *session)
73 {
74 	machines__destroy_kernel_maps(&session->machines);
75 }
76 
77 static bool perf_session__has_comm_exec(struct perf_session *session)
78 {
79 	struct perf_evsel *evsel;
80 
81 	evlist__for_each(session->evlist, evsel) {
82 		if (evsel->attr.comm_exec)
83 			return true;
84 	}
85 
86 	return false;
87 }
88 
89 static void perf_session__set_comm_exec(struct perf_session *session)
90 {
91 	bool comm_exec = perf_session__has_comm_exec(session);
92 
93 	machines__set_comm_exec(&session->machines, comm_exec);
94 }
95 
96 static int ordered_events__deliver_event(struct ordered_events *oe,
97 					 struct ordered_event *event)
98 {
99 	struct perf_sample sample;
100 	struct perf_session *session = container_of(oe, struct perf_session,
101 						    ordered_events);
102 	int ret = perf_evlist__parse_sample(session->evlist, event->event, &sample);
103 
104 	if (ret) {
105 		pr_err("Can't parse sample, err = %d\n", ret);
106 		return ret;
107 	}
108 
109 	return perf_session__deliver_event(session, event->event, &sample,
110 					   session->tool, event->file_offset);
111 }
112 
113 struct perf_session *perf_session__new(struct perf_data_file *file,
114 				       bool repipe, struct perf_tool *tool)
115 {
116 	struct perf_session *session = zalloc(sizeof(*session));
117 
118 	if (!session)
119 		goto out;
120 
121 	session->repipe = repipe;
122 	session->tool   = tool;
123 	INIT_LIST_HEAD(&session->auxtrace_index);
124 	machines__init(&session->machines);
125 	ordered_events__init(&session->ordered_events, ordered_events__deliver_event);
126 
127 	if (file) {
128 		if (perf_data_file__open(file))
129 			goto out_delete;
130 
131 		session->file = file;
132 
133 		if (perf_data_file__is_read(file)) {
134 			if (perf_session__open(session) < 0)
135 				goto out_close;
136 
137 			perf_session__set_id_hdr_size(session);
138 			perf_session__set_comm_exec(session);
139 		}
140 	}
141 
142 	if (!file || perf_data_file__is_write(file)) {
143 		/*
144 		 * In O_RDONLY mode this will be performed when reading the
145 		 * kernel MMAP event, in perf_event__process_mmap().
146 		 */
147 		if (perf_session__create_kernel_maps(session) < 0)
148 			pr_warning("Cannot read kernel map\n");
149 	}
150 
151 	if (tool && tool->ordering_requires_timestamps &&
152 	    tool->ordered_events && !perf_evlist__sample_id_all(session->evlist)) {
153 		dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n");
154 		tool->ordered_events = false;
155 	}
156 
157 	return session;
158 
159  out_close:
160 	perf_data_file__close(file);
161  out_delete:
162 	perf_session__delete(session);
163  out:
164 	return NULL;
165 }
166 
167 static void perf_session__delete_threads(struct perf_session *session)
168 {
169 	machine__delete_threads(&session->machines.host);
170 }
171 
172 static void perf_session_env__delete(struct perf_session_env *env)
173 {
174 	zfree(&env->hostname);
175 	zfree(&env->os_release);
176 	zfree(&env->version);
177 	zfree(&env->arch);
178 	zfree(&env->cpu_desc);
179 	zfree(&env->cpuid);
180 
181 	zfree(&env->cmdline);
182 	zfree(&env->sibling_cores);
183 	zfree(&env->sibling_threads);
184 	zfree(&env->numa_nodes);
185 	zfree(&env->pmu_mappings);
186 }
187 
188 void perf_session__delete(struct perf_session *session)
189 {
190 	auxtrace__free(session);
191 	auxtrace_index__free(&session->auxtrace_index);
192 	perf_session__destroy_kernel_maps(session);
193 	perf_session__delete_threads(session);
194 	perf_session_env__delete(&session->header.env);
195 	machines__exit(&session->machines);
196 	if (session->file)
197 		perf_data_file__close(session->file);
198 	free(session);
199 }
200 
201 static int process_event_synth_tracing_data_stub(struct perf_tool *tool
202 						 __maybe_unused,
203 						 union perf_event *event
204 						 __maybe_unused,
205 						 struct perf_session *session
206 						__maybe_unused)
207 {
208 	dump_printf(": unhandled!\n");
209 	return 0;
210 }
211 
212 static int process_event_synth_attr_stub(struct perf_tool *tool __maybe_unused,
213 					 union perf_event *event __maybe_unused,
214 					 struct perf_evlist **pevlist
215 					 __maybe_unused)
216 {
217 	dump_printf(": unhandled!\n");
218 	return 0;
219 }
220 
221 static int process_event_sample_stub(struct perf_tool *tool __maybe_unused,
222 				     union perf_event *event __maybe_unused,
223 				     struct perf_sample *sample __maybe_unused,
224 				     struct perf_evsel *evsel __maybe_unused,
225 				     struct machine *machine __maybe_unused)
226 {
227 	dump_printf(": unhandled!\n");
228 	return 0;
229 }
230 
231 static int process_event_stub(struct perf_tool *tool __maybe_unused,
232 			      union perf_event *event __maybe_unused,
233 			      struct perf_sample *sample __maybe_unused,
234 			      struct machine *machine __maybe_unused)
235 {
236 	dump_printf(": unhandled!\n");
237 	return 0;
238 }
239 
240 static int process_build_id_stub(struct perf_tool *tool __maybe_unused,
241 				 union perf_event *event __maybe_unused,
242 				 struct perf_session *session __maybe_unused)
243 {
244 	dump_printf(": unhandled!\n");
245 	return 0;
246 }
247 
248 static int process_finished_round_stub(struct perf_tool *tool __maybe_unused,
249 				       union perf_event *event __maybe_unused,
250 				       struct ordered_events *oe __maybe_unused)
251 {
252 	dump_printf(": unhandled!\n");
253 	return 0;
254 }
255 
256 static int process_finished_round(struct perf_tool *tool,
257 				  union perf_event *event,
258 				  struct ordered_events *oe);
259 
260 static int process_id_index_stub(struct perf_tool *tool __maybe_unused,
261 				 union perf_event *event __maybe_unused,
262 				 struct perf_session *perf_session
263 				 __maybe_unused)
264 {
265 	dump_printf(": unhandled!\n");
266 	return 0;
267 }
268 
269 static int process_event_auxtrace_info_stub(struct perf_tool *tool __maybe_unused,
270 				union perf_event *event __maybe_unused,
271 				struct perf_session *session __maybe_unused)
272 {
273 	dump_printf(": unhandled!\n");
274 	return 0;
275 }
276 
277 static int skipn(int fd, off_t n)
278 {
279 	char buf[4096];
280 	ssize_t ret;
281 
282 	while (n > 0) {
283 		ret = read(fd, buf, min(n, (off_t)sizeof(buf)));
284 		if (ret <= 0)
285 			return ret;
286 		n -= ret;
287 	}
288 
289 	return 0;
290 }
291 
292 static s64 process_event_auxtrace_stub(struct perf_tool *tool __maybe_unused,
293 				       union perf_event *event,
294 				       struct perf_session *session
295 				       __maybe_unused)
296 {
297 	dump_printf(": unhandled!\n");
298 	if (perf_data_file__is_pipe(session->file))
299 		skipn(perf_data_file__fd(session->file), event->auxtrace.size);
300 	return event->auxtrace.size;
301 }
302 
303 static
304 int process_event_auxtrace_error_stub(struct perf_tool *tool __maybe_unused,
305 				      union perf_event *event __maybe_unused,
306 				      struct perf_session *session __maybe_unused)
307 {
308 	dump_printf(": unhandled!\n");
309 	return 0;
310 }
311 
312 void perf_tool__fill_defaults(struct perf_tool *tool)
313 {
314 	if (tool->sample == NULL)
315 		tool->sample = process_event_sample_stub;
316 	if (tool->mmap == NULL)
317 		tool->mmap = process_event_stub;
318 	if (tool->mmap2 == NULL)
319 		tool->mmap2 = process_event_stub;
320 	if (tool->comm == NULL)
321 		tool->comm = process_event_stub;
322 	if (tool->fork == NULL)
323 		tool->fork = process_event_stub;
324 	if (tool->exit == NULL)
325 		tool->exit = process_event_stub;
326 	if (tool->lost == NULL)
327 		tool->lost = perf_event__process_lost;
328 	if (tool->aux == NULL)
329 		tool->aux = perf_event__process_aux;
330 	if (tool->itrace_start == NULL)
331 		tool->itrace_start = perf_event__process_itrace_start;
332 	if (tool->read == NULL)
333 		tool->read = process_event_sample_stub;
334 	if (tool->throttle == NULL)
335 		tool->throttle = process_event_stub;
336 	if (tool->unthrottle == NULL)
337 		tool->unthrottle = process_event_stub;
338 	if (tool->attr == NULL)
339 		tool->attr = process_event_synth_attr_stub;
340 	if (tool->tracing_data == NULL)
341 		tool->tracing_data = process_event_synth_tracing_data_stub;
342 	if (tool->build_id == NULL)
343 		tool->build_id = process_build_id_stub;
344 	if (tool->finished_round == NULL) {
345 		if (tool->ordered_events)
346 			tool->finished_round = process_finished_round;
347 		else
348 			tool->finished_round = process_finished_round_stub;
349 	}
350 	if (tool->id_index == NULL)
351 		tool->id_index = process_id_index_stub;
352 	if (tool->auxtrace_info == NULL)
353 		tool->auxtrace_info = process_event_auxtrace_info_stub;
354 	if (tool->auxtrace == NULL)
355 		tool->auxtrace = process_event_auxtrace_stub;
356 	if (tool->auxtrace_error == NULL)
357 		tool->auxtrace_error = process_event_auxtrace_error_stub;
358 }
359 
360 static void swap_sample_id_all(union perf_event *event, void *data)
361 {
362 	void *end = (void *) event + event->header.size;
363 	int size = end - data;
364 
365 	BUG_ON(size % sizeof(u64));
366 	mem_bswap_64(data, size);
367 }
368 
369 static void perf_event__all64_swap(union perf_event *event,
370 				   bool sample_id_all __maybe_unused)
371 {
372 	struct perf_event_header *hdr = &event->header;
373 	mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr));
374 }
375 
376 static void perf_event__comm_swap(union perf_event *event, bool sample_id_all)
377 {
378 	event->comm.pid = bswap_32(event->comm.pid);
379 	event->comm.tid = bswap_32(event->comm.tid);
380 
381 	if (sample_id_all) {
382 		void *data = &event->comm.comm;
383 
384 		data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
385 		swap_sample_id_all(event, data);
386 	}
387 }
388 
389 static void perf_event__mmap_swap(union perf_event *event,
390 				  bool sample_id_all)
391 {
392 	event->mmap.pid	  = bswap_32(event->mmap.pid);
393 	event->mmap.tid	  = bswap_32(event->mmap.tid);
394 	event->mmap.start = bswap_64(event->mmap.start);
395 	event->mmap.len	  = bswap_64(event->mmap.len);
396 	event->mmap.pgoff = bswap_64(event->mmap.pgoff);
397 
398 	if (sample_id_all) {
399 		void *data = &event->mmap.filename;
400 
401 		data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
402 		swap_sample_id_all(event, data);
403 	}
404 }
405 
406 static void perf_event__mmap2_swap(union perf_event *event,
407 				  bool sample_id_all)
408 {
409 	event->mmap2.pid   = bswap_32(event->mmap2.pid);
410 	event->mmap2.tid   = bswap_32(event->mmap2.tid);
411 	event->mmap2.start = bswap_64(event->mmap2.start);
412 	event->mmap2.len   = bswap_64(event->mmap2.len);
413 	event->mmap2.pgoff = bswap_64(event->mmap2.pgoff);
414 	event->mmap2.maj   = bswap_32(event->mmap2.maj);
415 	event->mmap2.min   = bswap_32(event->mmap2.min);
416 	event->mmap2.ino   = bswap_64(event->mmap2.ino);
417 
418 	if (sample_id_all) {
419 		void *data = &event->mmap2.filename;
420 
421 		data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
422 		swap_sample_id_all(event, data);
423 	}
424 }
425 static void perf_event__task_swap(union perf_event *event, bool sample_id_all)
426 {
427 	event->fork.pid	 = bswap_32(event->fork.pid);
428 	event->fork.tid	 = bswap_32(event->fork.tid);
429 	event->fork.ppid = bswap_32(event->fork.ppid);
430 	event->fork.ptid = bswap_32(event->fork.ptid);
431 	event->fork.time = bswap_64(event->fork.time);
432 
433 	if (sample_id_all)
434 		swap_sample_id_all(event, &event->fork + 1);
435 }
436 
437 static void perf_event__read_swap(union perf_event *event, bool sample_id_all)
438 {
439 	event->read.pid		 = bswap_32(event->read.pid);
440 	event->read.tid		 = bswap_32(event->read.tid);
441 	event->read.value	 = bswap_64(event->read.value);
442 	event->read.time_enabled = bswap_64(event->read.time_enabled);
443 	event->read.time_running = bswap_64(event->read.time_running);
444 	event->read.id		 = bswap_64(event->read.id);
445 
446 	if (sample_id_all)
447 		swap_sample_id_all(event, &event->read + 1);
448 }
449 
450 static void perf_event__aux_swap(union perf_event *event, bool sample_id_all)
451 {
452 	event->aux.aux_offset = bswap_64(event->aux.aux_offset);
453 	event->aux.aux_size   = bswap_64(event->aux.aux_size);
454 	event->aux.flags      = bswap_64(event->aux.flags);
455 
456 	if (sample_id_all)
457 		swap_sample_id_all(event, &event->aux + 1);
458 }
459 
460 static void perf_event__itrace_start_swap(union perf_event *event,
461 					  bool sample_id_all)
462 {
463 	event->itrace_start.pid	 = bswap_32(event->itrace_start.pid);
464 	event->itrace_start.tid	 = bswap_32(event->itrace_start.tid);
465 
466 	if (sample_id_all)
467 		swap_sample_id_all(event, &event->itrace_start + 1);
468 }
469 
470 static void perf_event__throttle_swap(union perf_event *event,
471 				      bool sample_id_all)
472 {
473 	event->throttle.time	  = bswap_64(event->throttle.time);
474 	event->throttle.id	  = bswap_64(event->throttle.id);
475 	event->throttle.stream_id = bswap_64(event->throttle.stream_id);
476 
477 	if (sample_id_all)
478 		swap_sample_id_all(event, &event->throttle + 1);
479 }
480 
481 static u8 revbyte(u8 b)
482 {
483 	int rev = (b >> 4) | ((b & 0xf) << 4);
484 	rev = ((rev & 0xcc) >> 2) | ((rev & 0x33) << 2);
485 	rev = ((rev & 0xaa) >> 1) | ((rev & 0x55) << 1);
486 	return (u8) rev;
487 }
488 
489 /*
490  * XXX this is hack in attempt to carry flags bitfield
491  * throught endian village. ABI says:
492  *
493  * Bit-fields are allocated from right to left (least to most significant)
494  * on little-endian implementations and from left to right (most to least
495  * significant) on big-endian implementations.
496  *
497  * The above seems to be byte specific, so we need to reverse each
498  * byte of the bitfield. 'Internet' also says this might be implementation
499  * specific and we probably need proper fix and carry perf_event_attr
500  * bitfield flags in separate data file FEAT_ section. Thought this seems
501  * to work for now.
502  */
503 static void swap_bitfield(u8 *p, unsigned len)
504 {
505 	unsigned i;
506 
507 	for (i = 0; i < len; i++) {
508 		*p = revbyte(*p);
509 		p++;
510 	}
511 }
512 
513 /* exported for swapping attributes in file header */
514 void perf_event__attr_swap(struct perf_event_attr *attr)
515 {
516 	attr->type		= bswap_32(attr->type);
517 	attr->size		= bswap_32(attr->size);
518 	attr->config		= bswap_64(attr->config);
519 	attr->sample_period	= bswap_64(attr->sample_period);
520 	attr->sample_type	= bswap_64(attr->sample_type);
521 	attr->read_format	= bswap_64(attr->read_format);
522 	attr->wakeup_events	= bswap_32(attr->wakeup_events);
523 	attr->bp_type		= bswap_32(attr->bp_type);
524 	attr->bp_addr		= bswap_64(attr->bp_addr);
525 	attr->bp_len		= bswap_64(attr->bp_len);
526 	attr->branch_sample_type = bswap_64(attr->branch_sample_type);
527 	attr->sample_regs_user	 = bswap_64(attr->sample_regs_user);
528 	attr->sample_stack_user  = bswap_32(attr->sample_stack_user);
529 	attr->aux_watermark	 = bswap_32(attr->aux_watermark);
530 
531 	swap_bitfield((u8 *) (&attr->read_format + 1), sizeof(u64));
532 }
533 
534 static void perf_event__hdr_attr_swap(union perf_event *event,
535 				      bool sample_id_all __maybe_unused)
536 {
537 	size_t size;
538 
539 	perf_event__attr_swap(&event->attr.attr);
540 
541 	size = event->header.size;
542 	size -= (void *)&event->attr.id - (void *)event;
543 	mem_bswap_64(event->attr.id, size);
544 }
545 
546 static void perf_event__event_type_swap(union perf_event *event,
547 					bool sample_id_all __maybe_unused)
548 {
549 	event->event_type.event_type.event_id =
550 		bswap_64(event->event_type.event_type.event_id);
551 }
552 
553 static void perf_event__tracing_data_swap(union perf_event *event,
554 					  bool sample_id_all __maybe_unused)
555 {
556 	event->tracing_data.size = bswap_32(event->tracing_data.size);
557 }
558 
559 static void perf_event__auxtrace_info_swap(union perf_event *event,
560 					   bool sample_id_all __maybe_unused)
561 {
562 	size_t size;
563 
564 	event->auxtrace_info.type = bswap_32(event->auxtrace_info.type);
565 
566 	size = event->header.size;
567 	size -= (void *)&event->auxtrace_info.priv - (void *)event;
568 	mem_bswap_64(event->auxtrace_info.priv, size);
569 }
570 
571 static void perf_event__auxtrace_swap(union perf_event *event,
572 				      bool sample_id_all __maybe_unused)
573 {
574 	event->auxtrace.size      = bswap_64(event->auxtrace.size);
575 	event->auxtrace.offset    = bswap_64(event->auxtrace.offset);
576 	event->auxtrace.reference = bswap_64(event->auxtrace.reference);
577 	event->auxtrace.idx       = bswap_32(event->auxtrace.idx);
578 	event->auxtrace.tid       = bswap_32(event->auxtrace.tid);
579 	event->auxtrace.cpu       = bswap_32(event->auxtrace.cpu);
580 }
581 
582 static void perf_event__auxtrace_error_swap(union perf_event *event,
583 					    bool sample_id_all __maybe_unused)
584 {
585 	event->auxtrace_error.type = bswap_32(event->auxtrace_error.type);
586 	event->auxtrace_error.code = bswap_32(event->auxtrace_error.code);
587 	event->auxtrace_error.cpu  = bswap_32(event->auxtrace_error.cpu);
588 	event->auxtrace_error.pid  = bswap_32(event->auxtrace_error.pid);
589 	event->auxtrace_error.tid  = bswap_32(event->auxtrace_error.tid);
590 	event->auxtrace_error.ip   = bswap_64(event->auxtrace_error.ip);
591 }
592 
593 typedef void (*perf_event__swap_op)(union perf_event *event,
594 				    bool sample_id_all);
595 
596 static perf_event__swap_op perf_event__swap_ops[] = {
597 	[PERF_RECORD_MMAP]		  = perf_event__mmap_swap,
598 	[PERF_RECORD_MMAP2]		  = perf_event__mmap2_swap,
599 	[PERF_RECORD_COMM]		  = perf_event__comm_swap,
600 	[PERF_RECORD_FORK]		  = perf_event__task_swap,
601 	[PERF_RECORD_EXIT]		  = perf_event__task_swap,
602 	[PERF_RECORD_LOST]		  = perf_event__all64_swap,
603 	[PERF_RECORD_READ]		  = perf_event__read_swap,
604 	[PERF_RECORD_THROTTLE]		  = perf_event__throttle_swap,
605 	[PERF_RECORD_UNTHROTTLE]	  = perf_event__throttle_swap,
606 	[PERF_RECORD_SAMPLE]		  = perf_event__all64_swap,
607 	[PERF_RECORD_AUX]		  = perf_event__aux_swap,
608 	[PERF_RECORD_ITRACE_START]	  = perf_event__itrace_start_swap,
609 	[PERF_RECORD_HEADER_ATTR]	  = perf_event__hdr_attr_swap,
610 	[PERF_RECORD_HEADER_EVENT_TYPE]	  = perf_event__event_type_swap,
611 	[PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap,
612 	[PERF_RECORD_HEADER_BUILD_ID]	  = NULL,
613 	[PERF_RECORD_ID_INDEX]		  = perf_event__all64_swap,
614 	[PERF_RECORD_AUXTRACE_INFO]	  = perf_event__auxtrace_info_swap,
615 	[PERF_RECORD_AUXTRACE]		  = perf_event__auxtrace_swap,
616 	[PERF_RECORD_AUXTRACE_ERROR]	  = perf_event__auxtrace_error_swap,
617 	[PERF_RECORD_HEADER_MAX]	  = NULL,
618 };
619 
620 /*
621  * When perf record finishes a pass on every buffers, it records this pseudo
622  * event.
623  * We record the max timestamp t found in the pass n.
624  * Assuming these timestamps are monotonic across cpus, we know that if
625  * a buffer still has events with timestamps below t, they will be all
626  * available and then read in the pass n + 1.
627  * Hence when we start to read the pass n + 2, we can safely flush every
628  * events with timestamps below t.
629  *
630  *    ============ PASS n =================
631  *       CPU 0         |   CPU 1
632  *                     |
633  *    cnt1 timestamps  |   cnt2 timestamps
634  *          1          |         2
635  *          2          |         3
636  *          -          |         4  <--- max recorded
637  *
638  *    ============ PASS n + 1 ==============
639  *       CPU 0         |   CPU 1
640  *                     |
641  *    cnt1 timestamps  |   cnt2 timestamps
642  *          3          |         5
643  *          4          |         6
644  *          5          |         7 <---- max recorded
645  *
646  *      Flush every events below timestamp 4
647  *
648  *    ============ PASS n + 2 ==============
649  *       CPU 0         |   CPU 1
650  *                     |
651  *    cnt1 timestamps  |   cnt2 timestamps
652  *          6          |         8
653  *          7          |         9
654  *          -          |         10
655  *
656  *      Flush every events below timestamp 7
657  *      etc...
658  */
659 static int process_finished_round(struct perf_tool *tool __maybe_unused,
660 				  union perf_event *event __maybe_unused,
661 				  struct ordered_events *oe)
662 {
663 	return ordered_events__flush(oe, OE_FLUSH__ROUND);
664 }
665 
666 int perf_session__queue_event(struct perf_session *s, union perf_event *event,
667 			      struct perf_sample *sample, u64 file_offset)
668 {
669 	return ordered_events__queue(&s->ordered_events, event, sample, file_offset);
670 }
671 
672 static void callchain__lbr_callstack_printf(struct perf_sample *sample)
673 {
674 	struct ip_callchain *callchain = sample->callchain;
675 	struct branch_stack *lbr_stack = sample->branch_stack;
676 	u64 kernel_callchain_nr = callchain->nr;
677 	unsigned int i;
678 
679 	for (i = 0; i < kernel_callchain_nr; i++) {
680 		if (callchain->ips[i] == PERF_CONTEXT_USER)
681 			break;
682 	}
683 
684 	if ((i != kernel_callchain_nr) && lbr_stack->nr) {
685 		u64 total_nr;
686 		/*
687 		 * LBR callstack can only get user call chain,
688 		 * i is kernel call chain number,
689 		 * 1 is PERF_CONTEXT_USER.
690 		 *
691 		 * The user call chain is stored in LBR registers.
692 		 * LBR are pair registers. The caller is stored
693 		 * in "from" register, while the callee is stored
694 		 * in "to" register.
695 		 * For example, there is a call stack
696 		 * "A"->"B"->"C"->"D".
697 		 * The LBR registers will recorde like
698 		 * "C"->"D", "B"->"C", "A"->"B".
699 		 * So only the first "to" register and all "from"
700 		 * registers are needed to construct the whole stack.
701 		 */
702 		total_nr = i + 1 + lbr_stack->nr + 1;
703 		kernel_callchain_nr = i + 1;
704 
705 		printf("... LBR call chain: nr:%" PRIu64 "\n", total_nr);
706 
707 		for (i = 0; i < kernel_callchain_nr; i++)
708 			printf("..... %2d: %016" PRIx64 "\n",
709 			       i, callchain->ips[i]);
710 
711 		printf("..... %2d: %016" PRIx64 "\n",
712 		       (int)(kernel_callchain_nr), lbr_stack->entries[0].to);
713 		for (i = 0; i < lbr_stack->nr; i++)
714 			printf("..... %2d: %016" PRIx64 "\n",
715 			       (int)(i + kernel_callchain_nr + 1), lbr_stack->entries[i].from);
716 	}
717 }
718 
719 static void callchain__printf(struct perf_evsel *evsel,
720 			      struct perf_sample *sample)
721 {
722 	unsigned int i;
723 	struct ip_callchain *callchain = sample->callchain;
724 
725 	if (has_branch_callstack(evsel))
726 		callchain__lbr_callstack_printf(sample);
727 
728 	printf("... FP chain: nr:%" PRIu64 "\n", callchain->nr);
729 
730 	for (i = 0; i < callchain->nr; i++)
731 		printf("..... %2d: %016" PRIx64 "\n",
732 		       i, callchain->ips[i]);
733 }
734 
735 static void branch_stack__printf(struct perf_sample *sample)
736 {
737 	uint64_t i;
738 
739 	printf("... branch stack: nr:%" PRIu64 "\n", sample->branch_stack->nr);
740 
741 	for (i = 0; i < sample->branch_stack->nr; i++)
742 		printf("..... %2"PRIu64": %016" PRIx64 " -> %016" PRIx64 "\n",
743 			i, sample->branch_stack->entries[i].from,
744 			sample->branch_stack->entries[i].to);
745 }
746 
747 static void regs_dump__printf(u64 mask, u64 *regs)
748 {
749 	unsigned rid, i = 0;
750 
751 	for_each_set_bit(rid, (unsigned long *) &mask, sizeof(mask) * 8) {
752 		u64 val = regs[i++];
753 
754 		printf(".... %-5s 0x%" PRIx64 "\n",
755 		       perf_reg_name(rid), val);
756 	}
757 }
758 
759 static const char *regs_abi[] = {
760 	[PERF_SAMPLE_REGS_ABI_NONE] = "none",
761 	[PERF_SAMPLE_REGS_ABI_32] = "32-bit",
762 	[PERF_SAMPLE_REGS_ABI_64] = "64-bit",
763 };
764 
765 static inline const char *regs_dump_abi(struct regs_dump *d)
766 {
767 	if (d->abi > PERF_SAMPLE_REGS_ABI_64)
768 		return "unknown";
769 
770 	return regs_abi[d->abi];
771 }
772 
773 static void regs__printf(const char *type, struct regs_dump *regs)
774 {
775 	u64 mask = regs->mask;
776 
777 	printf("... %s regs: mask 0x%" PRIx64 " ABI %s\n",
778 	       type,
779 	       mask,
780 	       regs_dump_abi(regs));
781 
782 	regs_dump__printf(mask, regs->regs);
783 }
784 
785 static void regs_user__printf(struct perf_sample *sample)
786 {
787 	struct regs_dump *user_regs = &sample->user_regs;
788 
789 	if (user_regs->regs)
790 		regs__printf("user", user_regs);
791 }
792 
793 static void regs_intr__printf(struct perf_sample *sample)
794 {
795 	struct regs_dump *intr_regs = &sample->intr_regs;
796 
797 	if (intr_regs->regs)
798 		regs__printf("intr", intr_regs);
799 }
800 
801 static void stack_user__printf(struct stack_dump *dump)
802 {
803 	printf("... ustack: size %" PRIu64 ", offset 0x%x\n",
804 	       dump->size, dump->offset);
805 }
806 
807 static void perf_evlist__print_tstamp(struct perf_evlist *evlist,
808 				       union perf_event *event,
809 				       struct perf_sample *sample)
810 {
811 	u64 sample_type = __perf_evlist__combined_sample_type(evlist);
812 
813 	if (event->header.type != PERF_RECORD_SAMPLE &&
814 	    !perf_evlist__sample_id_all(evlist)) {
815 		fputs("-1 -1 ", stdout);
816 		return;
817 	}
818 
819 	if ((sample_type & PERF_SAMPLE_CPU))
820 		printf("%u ", sample->cpu);
821 
822 	if (sample_type & PERF_SAMPLE_TIME)
823 		printf("%" PRIu64 " ", sample->time);
824 }
825 
826 static void sample_read__printf(struct perf_sample *sample, u64 read_format)
827 {
828 	printf("... sample_read:\n");
829 
830 	if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
831 		printf("...... time enabled %016" PRIx64 "\n",
832 		       sample->read.time_enabled);
833 
834 	if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
835 		printf("...... time running %016" PRIx64 "\n",
836 		       sample->read.time_running);
837 
838 	if (read_format & PERF_FORMAT_GROUP) {
839 		u64 i;
840 
841 		printf(".... group nr %" PRIu64 "\n", sample->read.group.nr);
842 
843 		for (i = 0; i < sample->read.group.nr; i++) {
844 			struct sample_read_value *value;
845 
846 			value = &sample->read.group.values[i];
847 			printf("..... id %016" PRIx64
848 			       ", value %016" PRIx64 "\n",
849 			       value->id, value->value);
850 		}
851 	} else
852 		printf("..... id %016" PRIx64 ", value %016" PRIx64 "\n",
853 			sample->read.one.id, sample->read.one.value);
854 }
855 
856 static void dump_event(struct perf_evlist *evlist, union perf_event *event,
857 		       u64 file_offset, struct perf_sample *sample)
858 {
859 	if (!dump_trace)
860 		return;
861 
862 	printf("\n%#" PRIx64 " [%#x]: event: %d\n",
863 	       file_offset, event->header.size, event->header.type);
864 
865 	trace_event(event);
866 
867 	if (sample)
868 		perf_evlist__print_tstamp(evlist, event, sample);
869 
870 	printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset,
871 	       event->header.size, perf_event__name(event->header.type));
872 }
873 
874 static void dump_sample(struct perf_evsel *evsel, union perf_event *event,
875 			struct perf_sample *sample)
876 {
877 	u64 sample_type;
878 
879 	if (!dump_trace)
880 		return;
881 
882 	printf("(IP, 0x%x): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n",
883 	       event->header.misc, sample->pid, sample->tid, sample->ip,
884 	       sample->period, sample->addr);
885 
886 	sample_type = evsel->attr.sample_type;
887 
888 	if (sample_type & PERF_SAMPLE_CALLCHAIN)
889 		callchain__printf(evsel, sample);
890 
891 	if ((sample_type & PERF_SAMPLE_BRANCH_STACK) && !has_branch_callstack(evsel))
892 		branch_stack__printf(sample);
893 
894 	if (sample_type & PERF_SAMPLE_REGS_USER)
895 		regs_user__printf(sample);
896 
897 	if (sample_type & PERF_SAMPLE_REGS_INTR)
898 		regs_intr__printf(sample);
899 
900 	if (sample_type & PERF_SAMPLE_STACK_USER)
901 		stack_user__printf(&sample->user_stack);
902 
903 	if (sample_type & PERF_SAMPLE_WEIGHT)
904 		printf("... weight: %" PRIu64 "\n", sample->weight);
905 
906 	if (sample_type & PERF_SAMPLE_DATA_SRC)
907 		printf(" . data_src: 0x%"PRIx64"\n", sample->data_src);
908 
909 	if (sample_type & PERF_SAMPLE_TRANSACTION)
910 		printf("... transaction: %" PRIx64 "\n", sample->transaction);
911 
912 	if (sample_type & PERF_SAMPLE_READ)
913 		sample_read__printf(sample, evsel->attr.read_format);
914 }
915 
916 static struct machine *machines__find_for_cpumode(struct machines *machines,
917 					       union perf_event *event,
918 					       struct perf_sample *sample)
919 {
920 	const u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
921 	struct machine *machine;
922 
923 	if (perf_guest &&
924 	    ((cpumode == PERF_RECORD_MISC_GUEST_KERNEL) ||
925 	     (cpumode == PERF_RECORD_MISC_GUEST_USER))) {
926 		u32 pid;
927 
928 		if (event->header.type == PERF_RECORD_MMAP
929 		    || event->header.type == PERF_RECORD_MMAP2)
930 			pid = event->mmap.pid;
931 		else
932 			pid = sample->pid;
933 
934 		machine = machines__find(machines, pid);
935 		if (!machine)
936 			machine = machines__find(machines, DEFAULT_GUEST_KERNEL_ID);
937 		return machine;
938 	}
939 
940 	return &machines->host;
941 }
942 
943 static int deliver_sample_value(struct perf_evlist *evlist,
944 				struct perf_tool *tool,
945 				union perf_event *event,
946 				struct perf_sample *sample,
947 				struct sample_read_value *v,
948 				struct machine *machine)
949 {
950 	struct perf_sample_id *sid = perf_evlist__id2sid(evlist, v->id);
951 
952 	if (sid) {
953 		sample->id     = v->id;
954 		sample->period = v->value - sid->period;
955 		sid->period    = v->value;
956 	}
957 
958 	if (!sid || sid->evsel == NULL) {
959 		++evlist->stats.nr_unknown_id;
960 		return 0;
961 	}
962 
963 	return tool->sample(tool, event, sample, sid->evsel, machine);
964 }
965 
966 static int deliver_sample_group(struct perf_evlist *evlist,
967 				struct perf_tool *tool,
968 				union  perf_event *event,
969 				struct perf_sample *sample,
970 				struct machine *machine)
971 {
972 	int ret = -EINVAL;
973 	u64 i;
974 
975 	for (i = 0; i < sample->read.group.nr; i++) {
976 		ret = deliver_sample_value(evlist, tool, event, sample,
977 					   &sample->read.group.values[i],
978 					   machine);
979 		if (ret)
980 			break;
981 	}
982 
983 	return ret;
984 }
985 
986 static int
987  perf_evlist__deliver_sample(struct perf_evlist *evlist,
988 			     struct perf_tool *tool,
989 			     union  perf_event *event,
990 			     struct perf_sample *sample,
991 			     struct perf_evsel *evsel,
992 			     struct machine *machine)
993 {
994 	/* We know evsel != NULL. */
995 	u64 sample_type = evsel->attr.sample_type;
996 	u64 read_format = evsel->attr.read_format;
997 
998 	/* Standard sample delievery. */
999 	if (!(sample_type & PERF_SAMPLE_READ))
1000 		return tool->sample(tool, event, sample, evsel, machine);
1001 
1002 	/* For PERF_SAMPLE_READ we have either single or group mode. */
1003 	if (read_format & PERF_FORMAT_GROUP)
1004 		return deliver_sample_group(evlist, tool, event, sample,
1005 					    machine);
1006 	else
1007 		return deliver_sample_value(evlist, tool, event, sample,
1008 					    &sample->read.one, machine);
1009 }
1010 
1011 static int machines__deliver_event(struct machines *machines,
1012 				   struct perf_evlist *evlist,
1013 				   union perf_event *event,
1014 				   struct perf_sample *sample,
1015 				   struct perf_tool *tool, u64 file_offset)
1016 {
1017 	struct perf_evsel *evsel;
1018 	struct machine *machine;
1019 
1020 	dump_event(evlist, event, file_offset, sample);
1021 
1022 	evsel = perf_evlist__id2evsel(evlist, sample->id);
1023 
1024 	machine = machines__find_for_cpumode(machines, event, sample);
1025 
1026 	switch (event->header.type) {
1027 	case PERF_RECORD_SAMPLE:
1028 		dump_sample(evsel, event, sample);
1029 		if (evsel == NULL) {
1030 			++evlist->stats.nr_unknown_id;
1031 			return 0;
1032 		}
1033 		if (machine == NULL) {
1034 			++evlist->stats.nr_unprocessable_samples;
1035 			return 0;
1036 		}
1037 		return perf_evlist__deliver_sample(evlist, tool, event, sample, evsel, machine);
1038 	case PERF_RECORD_MMAP:
1039 		return tool->mmap(tool, event, sample, machine);
1040 	case PERF_RECORD_MMAP2:
1041 		return tool->mmap2(tool, event, sample, machine);
1042 	case PERF_RECORD_COMM:
1043 		return tool->comm(tool, event, sample, machine);
1044 	case PERF_RECORD_FORK:
1045 		return tool->fork(tool, event, sample, machine);
1046 	case PERF_RECORD_EXIT:
1047 		return tool->exit(tool, event, sample, machine);
1048 	case PERF_RECORD_LOST:
1049 		if (tool->lost == perf_event__process_lost)
1050 			evlist->stats.total_lost += event->lost.lost;
1051 		return tool->lost(tool, event, sample, machine);
1052 	case PERF_RECORD_READ:
1053 		return tool->read(tool, event, sample, evsel, machine);
1054 	case PERF_RECORD_THROTTLE:
1055 		return tool->throttle(tool, event, sample, machine);
1056 	case PERF_RECORD_UNTHROTTLE:
1057 		return tool->unthrottle(tool, event, sample, machine);
1058 	case PERF_RECORD_AUX:
1059 		return tool->aux(tool, event, sample, machine);
1060 	case PERF_RECORD_ITRACE_START:
1061 		return tool->itrace_start(tool, event, sample, machine);
1062 	default:
1063 		++evlist->stats.nr_unknown_events;
1064 		return -1;
1065 	}
1066 }
1067 
1068 static int perf_session__deliver_event(struct perf_session *session,
1069 				       union perf_event *event,
1070 				       struct perf_sample *sample,
1071 				       struct perf_tool *tool,
1072 				       u64 file_offset)
1073 {
1074 	int ret;
1075 
1076 	ret = auxtrace__process_event(session, event, sample, tool);
1077 	if (ret < 0)
1078 		return ret;
1079 	if (ret > 0)
1080 		return 0;
1081 
1082 	return machines__deliver_event(&session->machines, session->evlist,
1083 				       event, sample, tool, file_offset);
1084 }
1085 
1086 static s64 perf_session__process_user_event(struct perf_session *session,
1087 					    union perf_event *event,
1088 					    u64 file_offset)
1089 {
1090 	struct ordered_events *oe = &session->ordered_events;
1091 	struct perf_tool *tool = session->tool;
1092 	int fd = perf_data_file__fd(session->file);
1093 	int err;
1094 
1095 	dump_event(session->evlist, event, file_offset, NULL);
1096 
1097 	/* These events are processed right away */
1098 	switch (event->header.type) {
1099 	case PERF_RECORD_HEADER_ATTR:
1100 		err = tool->attr(tool, event, &session->evlist);
1101 		if (err == 0) {
1102 			perf_session__set_id_hdr_size(session);
1103 			perf_session__set_comm_exec(session);
1104 		}
1105 		return err;
1106 	case PERF_RECORD_HEADER_EVENT_TYPE:
1107 		/*
1108 		 * Depreceated, but we need to handle it for sake
1109 		 * of old data files create in pipe mode.
1110 		 */
1111 		return 0;
1112 	case PERF_RECORD_HEADER_TRACING_DATA:
1113 		/* setup for reading amidst mmap */
1114 		lseek(fd, file_offset, SEEK_SET);
1115 		return tool->tracing_data(tool, event, session);
1116 	case PERF_RECORD_HEADER_BUILD_ID:
1117 		return tool->build_id(tool, event, session);
1118 	case PERF_RECORD_FINISHED_ROUND:
1119 		return tool->finished_round(tool, event, oe);
1120 	case PERF_RECORD_ID_INDEX:
1121 		return tool->id_index(tool, event, session);
1122 	case PERF_RECORD_AUXTRACE_INFO:
1123 		return tool->auxtrace_info(tool, event, session);
1124 	case PERF_RECORD_AUXTRACE:
1125 		/* setup for reading amidst mmap */
1126 		lseek(fd, file_offset + event->header.size, SEEK_SET);
1127 		return tool->auxtrace(tool, event, session);
1128 	case PERF_RECORD_AUXTRACE_ERROR:
1129 		perf_session__auxtrace_error_inc(session, event);
1130 		return tool->auxtrace_error(tool, event, session);
1131 	default:
1132 		return -EINVAL;
1133 	}
1134 }
1135 
1136 int perf_session__deliver_synth_event(struct perf_session *session,
1137 				      union perf_event *event,
1138 				      struct perf_sample *sample)
1139 {
1140 	struct perf_evlist *evlist = session->evlist;
1141 	struct perf_tool *tool = session->tool;
1142 
1143 	events_stats__inc(&evlist->stats, event->header.type);
1144 
1145 	if (event->header.type >= PERF_RECORD_USER_TYPE_START)
1146 		return perf_session__process_user_event(session, event, 0);
1147 
1148 	return machines__deliver_event(&session->machines, evlist, event, sample, tool, 0);
1149 }
1150 
1151 static void event_swap(union perf_event *event, bool sample_id_all)
1152 {
1153 	perf_event__swap_op swap;
1154 
1155 	swap = perf_event__swap_ops[event->header.type];
1156 	if (swap)
1157 		swap(event, sample_id_all);
1158 }
1159 
1160 int perf_session__peek_event(struct perf_session *session, off_t file_offset,
1161 			     void *buf, size_t buf_sz,
1162 			     union perf_event **event_ptr,
1163 			     struct perf_sample *sample)
1164 {
1165 	union perf_event *event;
1166 	size_t hdr_sz, rest;
1167 	int fd;
1168 
1169 	if (session->one_mmap && !session->header.needs_swap) {
1170 		event = file_offset - session->one_mmap_offset +
1171 			session->one_mmap_addr;
1172 		goto out_parse_sample;
1173 	}
1174 
1175 	if (perf_data_file__is_pipe(session->file))
1176 		return -1;
1177 
1178 	fd = perf_data_file__fd(session->file);
1179 	hdr_sz = sizeof(struct perf_event_header);
1180 
1181 	if (buf_sz < hdr_sz)
1182 		return -1;
1183 
1184 	if (lseek(fd, file_offset, SEEK_SET) == (off_t)-1 ||
1185 	    readn(fd, &buf, hdr_sz) != (ssize_t)hdr_sz)
1186 		return -1;
1187 
1188 	event = (union perf_event *)buf;
1189 
1190 	if (session->header.needs_swap)
1191 		perf_event_header__bswap(&event->header);
1192 
1193 	if (event->header.size < hdr_sz)
1194 		return -1;
1195 
1196 	rest = event->header.size - hdr_sz;
1197 
1198 	if (readn(fd, &buf, rest) != (ssize_t)rest)
1199 		return -1;
1200 
1201 	if (session->header.needs_swap)
1202 		event_swap(event, perf_evlist__sample_id_all(session->evlist));
1203 
1204 out_parse_sample:
1205 
1206 	if (sample && event->header.type < PERF_RECORD_USER_TYPE_START &&
1207 	    perf_evlist__parse_sample(session->evlist, event, sample))
1208 		return -1;
1209 
1210 	*event_ptr = event;
1211 
1212 	return 0;
1213 }
1214 
1215 static s64 perf_session__process_event(struct perf_session *session,
1216 				       union perf_event *event, u64 file_offset)
1217 {
1218 	struct perf_evlist *evlist = session->evlist;
1219 	struct perf_tool *tool = session->tool;
1220 	struct perf_sample sample;
1221 	int ret;
1222 
1223 	if (session->header.needs_swap)
1224 		event_swap(event, perf_evlist__sample_id_all(evlist));
1225 
1226 	if (event->header.type >= PERF_RECORD_HEADER_MAX)
1227 		return -EINVAL;
1228 
1229 	events_stats__inc(&evlist->stats, event->header.type);
1230 
1231 	if (event->header.type >= PERF_RECORD_USER_TYPE_START)
1232 		return perf_session__process_user_event(session, event, file_offset);
1233 
1234 	/*
1235 	 * For all kernel events we get the sample data
1236 	 */
1237 	ret = perf_evlist__parse_sample(evlist, event, &sample);
1238 	if (ret)
1239 		return ret;
1240 
1241 	if (tool->ordered_events) {
1242 		ret = perf_session__queue_event(session, event, &sample, file_offset);
1243 		if (ret != -ETIME)
1244 			return ret;
1245 	}
1246 
1247 	return perf_session__deliver_event(session, event, &sample, tool,
1248 					   file_offset);
1249 }
1250 
1251 void perf_event_header__bswap(struct perf_event_header *hdr)
1252 {
1253 	hdr->type = bswap_32(hdr->type);
1254 	hdr->misc = bswap_16(hdr->misc);
1255 	hdr->size = bswap_16(hdr->size);
1256 }
1257 
1258 struct thread *perf_session__findnew(struct perf_session *session, pid_t pid)
1259 {
1260 	return machine__findnew_thread(&session->machines.host, -1, pid);
1261 }
1262 
1263 static struct thread *perf_session__register_idle_thread(struct perf_session *session)
1264 {
1265 	struct thread *thread;
1266 
1267 	thread = machine__findnew_thread(&session->machines.host, 0, 0);
1268 	if (thread == NULL || thread__set_comm(thread, "swapper", 0)) {
1269 		pr_err("problem inserting idle task.\n");
1270 		thread = NULL;
1271 	}
1272 
1273 	return thread;
1274 }
1275 
1276 static void perf_session__warn_about_errors(const struct perf_session *session)
1277 {
1278 	const struct events_stats *stats = &session->evlist->stats;
1279 	const struct ordered_events *oe = &session->ordered_events;
1280 
1281 	if (session->tool->lost == perf_event__process_lost &&
1282 	    stats->nr_events[PERF_RECORD_LOST] != 0) {
1283 		ui__warning("Processed %d events and lost %d chunks!\n\n"
1284 			    "Check IO/CPU overload!\n\n",
1285 			    stats->nr_events[0],
1286 			    stats->nr_events[PERF_RECORD_LOST]);
1287 	}
1288 
1289 	if (stats->nr_unknown_events != 0) {
1290 		ui__warning("Found %u unknown events!\n\n"
1291 			    "Is this an older tool processing a perf.data "
1292 			    "file generated by a more recent tool?\n\n"
1293 			    "If that is not the case, consider "
1294 			    "reporting to linux-kernel@vger.kernel.org.\n\n",
1295 			    stats->nr_unknown_events);
1296 	}
1297 
1298 	if (stats->nr_unknown_id != 0) {
1299 		ui__warning("%u samples with id not present in the header\n",
1300 			    stats->nr_unknown_id);
1301 	}
1302 
1303 	if (stats->nr_invalid_chains != 0) {
1304 		ui__warning("Found invalid callchains!\n\n"
1305 			    "%u out of %u events were discarded for this reason.\n\n"
1306 			    "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
1307 			    stats->nr_invalid_chains,
1308 			    stats->nr_events[PERF_RECORD_SAMPLE]);
1309 	}
1310 
1311 	if (stats->nr_unprocessable_samples != 0) {
1312 		ui__warning("%u unprocessable samples recorded.\n"
1313 			    "Do you have a KVM guest running and not using 'perf kvm'?\n",
1314 			    stats->nr_unprocessable_samples);
1315 	}
1316 
1317 	if (oe->nr_unordered_events != 0)
1318 		ui__warning("%u out of order events recorded.\n", oe->nr_unordered_events);
1319 
1320 	events_stats__auxtrace_error_warn(stats);
1321 }
1322 
1323 volatile int session_done;
1324 
1325 static int __perf_session__process_pipe_events(struct perf_session *session)
1326 {
1327 	struct ordered_events *oe = &session->ordered_events;
1328 	struct perf_tool *tool = session->tool;
1329 	int fd = perf_data_file__fd(session->file);
1330 	union perf_event *event;
1331 	uint32_t size, cur_size = 0;
1332 	void *buf = NULL;
1333 	s64 skip = 0;
1334 	u64 head;
1335 	ssize_t err;
1336 	void *p;
1337 
1338 	perf_tool__fill_defaults(tool);
1339 
1340 	head = 0;
1341 	cur_size = sizeof(union perf_event);
1342 
1343 	buf = malloc(cur_size);
1344 	if (!buf)
1345 		return -errno;
1346 more:
1347 	event = buf;
1348 	err = readn(fd, event, sizeof(struct perf_event_header));
1349 	if (err <= 0) {
1350 		if (err == 0)
1351 			goto done;
1352 
1353 		pr_err("failed to read event header\n");
1354 		goto out_err;
1355 	}
1356 
1357 	if (session->header.needs_swap)
1358 		perf_event_header__bswap(&event->header);
1359 
1360 	size = event->header.size;
1361 	if (size < sizeof(struct perf_event_header)) {
1362 		pr_err("bad event header size\n");
1363 		goto out_err;
1364 	}
1365 
1366 	if (size > cur_size) {
1367 		void *new = realloc(buf, size);
1368 		if (!new) {
1369 			pr_err("failed to allocate memory to read event\n");
1370 			goto out_err;
1371 		}
1372 		buf = new;
1373 		cur_size = size;
1374 		event = buf;
1375 	}
1376 	p = event;
1377 	p += sizeof(struct perf_event_header);
1378 
1379 	if (size - sizeof(struct perf_event_header)) {
1380 		err = readn(fd, p, size - sizeof(struct perf_event_header));
1381 		if (err <= 0) {
1382 			if (err == 0) {
1383 				pr_err("unexpected end of event stream\n");
1384 				goto done;
1385 			}
1386 
1387 			pr_err("failed to read event data\n");
1388 			goto out_err;
1389 		}
1390 	}
1391 
1392 	if ((skip = perf_session__process_event(session, event, head)) < 0) {
1393 		pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
1394 		       head, event->header.size, event->header.type);
1395 		err = -EINVAL;
1396 		goto out_err;
1397 	}
1398 
1399 	head += size;
1400 
1401 	if (skip > 0)
1402 		head += skip;
1403 
1404 	if (!session_done())
1405 		goto more;
1406 done:
1407 	/* do the final flush for ordered samples */
1408 	err = ordered_events__flush(oe, OE_FLUSH__FINAL);
1409 	if (err)
1410 		goto out_err;
1411 	err = auxtrace__flush_events(session, tool);
1412 out_err:
1413 	free(buf);
1414 	perf_session__warn_about_errors(session);
1415 	ordered_events__free(&session->ordered_events);
1416 	auxtrace__free_events(session);
1417 	return err;
1418 }
1419 
1420 static union perf_event *
1421 fetch_mmaped_event(struct perf_session *session,
1422 		   u64 head, size_t mmap_size, char *buf)
1423 {
1424 	union perf_event *event;
1425 
1426 	/*
1427 	 * Ensure we have enough space remaining to read
1428 	 * the size of the event in the headers.
1429 	 */
1430 	if (head + sizeof(event->header) > mmap_size)
1431 		return NULL;
1432 
1433 	event = (union perf_event *)(buf + head);
1434 
1435 	if (session->header.needs_swap)
1436 		perf_event_header__bswap(&event->header);
1437 
1438 	if (head + event->header.size > mmap_size) {
1439 		/* We're not fetching the event so swap back again */
1440 		if (session->header.needs_swap)
1441 			perf_event_header__bswap(&event->header);
1442 		return NULL;
1443 	}
1444 
1445 	return event;
1446 }
1447 
1448 /*
1449  * On 64bit we can mmap the data file in one go. No need for tiny mmap
1450  * slices. On 32bit we use 32MB.
1451  */
1452 #if BITS_PER_LONG == 64
1453 #define MMAP_SIZE ULLONG_MAX
1454 #define NUM_MMAPS 1
1455 #else
1456 #define MMAP_SIZE (32 * 1024 * 1024ULL)
1457 #define NUM_MMAPS 128
1458 #endif
1459 
1460 static int __perf_session__process_events(struct perf_session *session,
1461 					  u64 data_offset, u64 data_size,
1462 					  u64 file_size)
1463 {
1464 	struct ordered_events *oe = &session->ordered_events;
1465 	struct perf_tool *tool = session->tool;
1466 	int fd = perf_data_file__fd(session->file);
1467 	u64 head, page_offset, file_offset, file_pos, size;
1468 	int err, mmap_prot, mmap_flags, map_idx = 0;
1469 	size_t	mmap_size;
1470 	char *buf, *mmaps[NUM_MMAPS];
1471 	union perf_event *event;
1472 	struct ui_progress prog;
1473 	s64 skip;
1474 
1475 	perf_tool__fill_defaults(tool);
1476 
1477 	page_offset = page_size * (data_offset / page_size);
1478 	file_offset = page_offset;
1479 	head = data_offset - page_offset;
1480 
1481 	if (data_size && (data_offset + data_size < file_size))
1482 		file_size = data_offset + data_size;
1483 
1484 	ui_progress__init(&prog, file_size, "Processing events...");
1485 
1486 	mmap_size = MMAP_SIZE;
1487 	if (mmap_size > file_size) {
1488 		mmap_size = file_size;
1489 		session->one_mmap = true;
1490 	}
1491 
1492 	memset(mmaps, 0, sizeof(mmaps));
1493 
1494 	mmap_prot  = PROT_READ;
1495 	mmap_flags = MAP_SHARED;
1496 
1497 	if (session->header.needs_swap) {
1498 		mmap_prot  |= PROT_WRITE;
1499 		mmap_flags = MAP_PRIVATE;
1500 	}
1501 remap:
1502 	buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, fd,
1503 		   file_offset);
1504 	if (buf == MAP_FAILED) {
1505 		pr_err("failed to mmap file\n");
1506 		err = -errno;
1507 		goto out_err;
1508 	}
1509 	mmaps[map_idx] = buf;
1510 	map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
1511 	file_pos = file_offset + head;
1512 	if (session->one_mmap) {
1513 		session->one_mmap_addr = buf;
1514 		session->one_mmap_offset = file_offset;
1515 	}
1516 
1517 more:
1518 	event = fetch_mmaped_event(session, head, mmap_size, buf);
1519 	if (!event) {
1520 		if (mmaps[map_idx]) {
1521 			munmap(mmaps[map_idx], mmap_size);
1522 			mmaps[map_idx] = NULL;
1523 		}
1524 
1525 		page_offset = page_size * (head / page_size);
1526 		file_offset += page_offset;
1527 		head -= page_offset;
1528 		goto remap;
1529 	}
1530 
1531 	size = event->header.size;
1532 
1533 	if (size < sizeof(struct perf_event_header) ||
1534 	    (skip = perf_session__process_event(session, event, file_pos)) < 0) {
1535 		pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
1536 		       file_offset + head, event->header.size,
1537 		       event->header.type);
1538 		err = -EINVAL;
1539 		goto out_err;
1540 	}
1541 
1542 	if (skip)
1543 		size += skip;
1544 
1545 	head += size;
1546 	file_pos += size;
1547 
1548 	ui_progress__update(&prog, size);
1549 
1550 	if (session_done())
1551 		goto out;
1552 
1553 	if (file_pos < file_size)
1554 		goto more;
1555 
1556 out:
1557 	/* do the final flush for ordered samples */
1558 	err = ordered_events__flush(oe, OE_FLUSH__FINAL);
1559 	if (err)
1560 		goto out_err;
1561 	err = auxtrace__flush_events(session, tool);
1562 out_err:
1563 	ui_progress__finish();
1564 	perf_session__warn_about_errors(session);
1565 	ordered_events__free(&session->ordered_events);
1566 	auxtrace__free_events(session);
1567 	session->one_mmap = false;
1568 	return err;
1569 }
1570 
1571 int perf_session__process_events(struct perf_session *session)
1572 {
1573 	u64 size = perf_data_file__size(session->file);
1574 	int err;
1575 
1576 	if (perf_session__register_idle_thread(session) == NULL)
1577 		return -ENOMEM;
1578 
1579 	if (!perf_data_file__is_pipe(session->file))
1580 		err = __perf_session__process_events(session,
1581 						     session->header.data_offset,
1582 						     session->header.data_size, size);
1583 	else
1584 		err = __perf_session__process_pipe_events(session);
1585 
1586 	return err;
1587 }
1588 
1589 bool perf_session__has_traces(struct perf_session *session, const char *msg)
1590 {
1591 	struct perf_evsel *evsel;
1592 
1593 	evlist__for_each(session->evlist, evsel) {
1594 		if (evsel->attr.type == PERF_TYPE_TRACEPOINT)
1595 			return true;
1596 	}
1597 
1598 	pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
1599 	return false;
1600 }
1601 
1602 int maps__set_kallsyms_ref_reloc_sym(struct map **maps,
1603 				     const char *symbol_name, u64 addr)
1604 {
1605 	char *bracket;
1606 	enum map_type i;
1607 	struct ref_reloc_sym *ref;
1608 
1609 	ref = zalloc(sizeof(struct ref_reloc_sym));
1610 	if (ref == NULL)
1611 		return -ENOMEM;
1612 
1613 	ref->name = strdup(symbol_name);
1614 	if (ref->name == NULL) {
1615 		free(ref);
1616 		return -ENOMEM;
1617 	}
1618 
1619 	bracket = strchr(ref->name, ']');
1620 	if (bracket)
1621 		*bracket = '\0';
1622 
1623 	ref->addr = addr;
1624 
1625 	for (i = 0; i < MAP__NR_TYPES; ++i) {
1626 		struct kmap *kmap = map__kmap(maps[i]);
1627 
1628 		if (!kmap)
1629 			continue;
1630 		kmap->ref_reloc_sym = ref;
1631 	}
1632 
1633 	return 0;
1634 }
1635 
1636 size_t perf_session__fprintf_dsos(struct perf_session *session, FILE *fp)
1637 {
1638 	return machines__fprintf_dsos(&session->machines, fp);
1639 }
1640 
1641 size_t perf_session__fprintf_dsos_buildid(struct perf_session *session, FILE *fp,
1642 					  bool (skip)(struct dso *dso, int parm), int parm)
1643 {
1644 	return machines__fprintf_dsos_buildid(&session->machines, fp, skip, parm);
1645 }
1646 
1647 size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp)
1648 {
1649 	size_t ret;
1650 	const char *msg = "";
1651 
1652 	if (perf_header__has_feat(&session->header, HEADER_AUXTRACE))
1653 		msg = " (excludes AUX area (e.g. instruction trace) decoded / synthesized events)";
1654 
1655 	ret = fprintf(fp, "Aggregated stats:%s\n", msg);
1656 
1657 	ret += events_stats__fprintf(&session->evlist->stats, fp);
1658 	return ret;
1659 }
1660 
1661 size_t perf_session__fprintf(struct perf_session *session, FILE *fp)
1662 {
1663 	/*
1664 	 * FIXME: Here we have to actually print all the machines in this
1665 	 * session, not just the host...
1666 	 */
1667 	return machine__fprintf(&session->machines.host, fp);
1668 }
1669 
1670 struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
1671 					      unsigned int type)
1672 {
1673 	struct perf_evsel *pos;
1674 
1675 	evlist__for_each(session->evlist, pos) {
1676 		if (pos->attr.type == type)
1677 			return pos;
1678 	}
1679 	return NULL;
1680 }
1681 
1682 void perf_evsel__print_ip(struct perf_evsel *evsel, struct perf_sample *sample,
1683 			  struct addr_location *al,
1684 			  unsigned int print_opts, unsigned int stack_depth)
1685 {
1686 	struct callchain_cursor_node *node;
1687 	int print_ip = print_opts & PRINT_IP_OPT_IP;
1688 	int print_sym = print_opts & PRINT_IP_OPT_SYM;
1689 	int print_dso = print_opts & PRINT_IP_OPT_DSO;
1690 	int print_symoffset = print_opts & PRINT_IP_OPT_SYMOFFSET;
1691 	int print_oneline = print_opts & PRINT_IP_OPT_ONELINE;
1692 	int print_srcline = print_opts & PRINT_IP_OPT_SRCLINE;
1693 	char s = print_oneline ? ' ' : '\t';
1694 
1695 	if (symbol_conf.use_callchain && sample->callchain) {
1696 		struct addr_location node_al;
1697 
1698 		if (thread__resolve_callchain(al->thread, evsel,
1699 					      sample, NULL, NULL,
1700 					      PERF_MAX_STACK_DEPTH) != 0) {
1701 			if (verbose)
1702 				error("Failed to resolve callchain. Skipping\n");
1703 			return;
1704 		}
1705 		callchain_cursor_commit(&callchain_cursor);
1706 
1707 		if (print_symoffset)
1708 			node_al = *al;
1709 
1710 		while (stack_depth) {
1711 			u64 addr = 0;
1712 
1713 			node = callchain_cursor_current(&callchain_cursor);
1714 			if (!node)
1715 				break;
1716 
1717 			if (node->sym && node->sym->ignore)
1718 				goto next;
1719 
1720 			if (print_ip)
1721 				printf("%c%16" PRIx64, s, node->ip);
1722 
1723 			if (node->map)
1724 				addr = node->map->map_ip(node->map, node->ip);
1725 
1726 			if (print_sym) {
1727 				printf(" ");
1728 				if (print_symoffset) {
1729 					node_al.addr = addr;
1730 					node_al.map  = node->map;
1731 					symbol__fprintf_symname_offs(node->sym, &node_al, stdout);
1732 				} else
1733 					symbol__fprintf_symname(node->sym, stdout);
1734 			}
1735 
1736 			if (print_dso) {
1737 				printf(" (");
1738 				map__fprintf_dsoname(node->map, stdout);
1739 				printf(")");
1740 			}
1741 
1742 			if (print_srcline)
1743 				map__fprintf_srcline(node->map, addr, "\n  ",
1744 						     stdout);
1745 
1746 			if (!print_oneline)
1747 				printf("\n");
1748 
1749 			stack_depth--;
1750 next:
1751 			callchain_cursor_advance(&callchain_cursor);
1752 		}
1753 
1754 	} else {
1755 		if (al->sym && al->sym->ignore)
1756 			return;
1757 
1758 		if (print_ip)
1759 			printf("%16" PRIx64, sample->ip);
1760 
1761 		if (print_sym) {
1762 			printf(" ");
1763 			if (print_symoffset)
1764 				symbol__fprintf_symname_offs(al->sym, al,
1765 							     stdout);
1766 			else
1767 				symbol__fprintf_symname(al->sym, stdout);
1768 		}
1769 
1770 		if (print_dso) {
1771 			printf(" (");
1772 			map__fprintf_dsoname(al->map, stdout);
1773 			printf(")");
1774 		}
1775 
1776 		if (print_srcline)
1777 			map__fprintf_srcline(al->map, al->addr, "\n  ", stdout);
1778 	}
1779 }
1780 
1781 int perf_session__cpu_bitmap(struct perf_session *session,
1782 			     const char *cpu_list, unsigned long *cpu_bitmap)
1783 {
1784 	int i, err = -1;
1785 	struct cpu_map *map;
1786 
1787 	for (i = 0; i < PERF_TYPE_MAX; ++i) {
1788 		struct perf_evsel *evsel;
1789 
1790 		evsel = perf_session__find_first_evtype(session, i);
1791 		if (!evsel)
1792 			continue;
1793 
1794 		if (!(evsel->attr.sample_type & PERF_SAMPLE_CPU)) {
1795 			pr_err("File does not contain CPU events. "
1796 			       "Remove -c option to proceed.\n");
1797 			return -1;
1798 		}
1799 	}
1800 
1801 	map = cpu_map__new(cpu_list);
1802 	if (map == NULL) {
1803 		pr_err("Invalid cpu_list\n");
1804 		return -1;
1805 	}
1806 
1807 	for (i = 0; i < map->nr; i++) {
1808 		int cpu = map->map[i];
1809 
1810 		if (cpu >= MAX_NR_CPUS) {
1811 			pr_err("Requested CPU %d too large. "
1812 			       "Consider raising MAX_NR_CPUS\n", cpu);
1813 			goto out_delete_map;
1814 		}
1815 
1816 		set_bit(cpu, cpu_bitmap);
1817 	}
1818 
1819 	err = 0;
1820 
1821 out_delete_map:
1822 	cpu_map__delete(map);
1823 	return err;
1824 }
1825 
1826 void perf_session__fprintf_info(struct perf_session *session, FILE *fp,
1827 				bool full)
1828 {
1829 	struct stat st;
1830 	int fd, ret;
1831 
1832 	if (session == NULL || fp == NULL)
1833 		return;
1834 
1835 	fd = perf_data_file__fd(session->file);
1836 
1837 	ret = fstat(fd, &st);
1838 	if (ret == -1)
1839 		return;
1840 
1841 	fprintf(fp, "# ========\n");
1842 	fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
1843 	perf_header__fprintf_info(session, fp, full);
1844 	fprintf(fp, "# ========\n#\n");
1845 }
1846 
1847 
1848 int __perf_session__set_tracepoints_handlers(struct perf_session *session,
1849 					     const struct perf_evsel_str_handler *assocs,
1850 					     size_t nr_assocs)
1851 {
1852 	struct perf_evsel *evsel;
1853 	size_t i;
1854 	int err;
1855 
1856 	for (i = 0; i < nr_assocs; i++) {
1857 		/*
1858 		 * Adding a handler for an event not in the session,
1859 		 * just ignore it.
1860 		 */
1861 		evsel = perf_evlist__find_tracepoint_by_name(session->evlist, assocs[i].name);
1862 		if (evsel == NULL)
1863 			continue;
1864 
1865 		err = -EEXIST;
1866 		if (evsel->handler != NULL)
1867 			goto out;
1868 		evsel->handler = assocs[i].handler;
1869 	}
1870 
1871 	err = 0;
1872 out:
1873 	return err;
1874 }
1875 
1876 int perf_event__process_id_index(struct perf_tool *tool __maybe_unused,
1877 				 union perf_event *event,
1878 				 struct perf_session *session)
1879 {
1880 	struct perf_evlist *evlist = session->evlist;
1881 	struct id_index_event *ie = &event->id_index;
1882 	size_t i, nr, max_nr;
1883 
1884 	max_nr = (ie->header.size - sizeof(struct id_index_event)) /
1885 		 sizeof(struct id_index_entry);
1886 	nr = ie->nr;
1887 	if (nr > max_nr)
1888 		return -EINVAL;
1889 
1890 	if (dump_trace)
1891 		fprintf(stdout, " nr: %zu\n", nr);
1892 
1893 	for (i = 0; i < nr; i++) {
1894 		struct id_index_entry *e = &ie->entries[i];
1895 		struct perf_sample_id *sid;
1896 
1897 		if (dump_trace) {
1898 			fprintf(stdout,	" ... id: %"PRIu64, e->id);
1899 			fprintf(stdout,	"  idx: %"PRIu64, e->idx);
1900 			fprintf(stdout,	"  cpu: %"PRId64, e->cpu);
1901 			fprintf(stdout,	"  tid: %"PRId64"\n", e->tid);
1902 		}
1903 
1904 		sid = perf_evlist__id2sid(evlist, e->id);
1905 		if (!sid)
1906 			return -ENOENT;
1907 		sid->idx = e->idx;
1908 		sid->cpu = e->cpu;
1909 		sid->tid = e->tid;
1910 	}
1911 	return 0;
1912 }
1913 
1914 int perf_event__synthesize_id_index(struct perf_tool *tool,
1915 				    perf_event__handler_t process,
1916 				    struct perf_evlist *evlist,
1917 				    struct machine *machine)
1918 {
1919 	union perf_event *ev;
1920 	struct perf_evsel *evsel;
1921 	size_t nr = 0, i = 0, sz, max_nr, n;
1922 	int err;
1923 
1924 	pr_debug2("Synthesizing id index\n");
1925 
1926 	max_nr = (UINT16_MAX - sizeof(struct id_index_event)) /
1927 		 sizeof(struct id_index_entry);
1928 
1929 	evlist__for_each(evlist, evsel)
1930 		nr += evsel->ids;
1931 
1932 	n = nr > max_nr ? max_nr : nr;
1933 	sz = sizeof(struct id_index_event) + n * sizeof(struct id_index_entry);
1934 	ev = zalloc(sz);
1935 	if (!ev)
1936 		return -ENOMEM;
1937 
1938 	ev->id_index.header.type = PERF_RECORD_ID_INDEX;
1939 	ev->id_index.header.size = sz;
1940 	ev->id_index.nr = n;
1941 
1942 	evlist__for_each(evlist, evsel) {
1943 		u32 j;
1944 
1945 		for (j = 0; j < evsel->ids; j++) {
1946 			struct id_index_entry *e;
1947 			struct perf_sample_id *sid;
1948 
1949 			if (i >= n) {
1950 				err = process(tool, ev, NULL, machine);
1951 				if (err)
1952 					goto out_err;
1953 				nr -= n;
1954 				i = 0;
1955 			}
1956 
1957 			e = &ev->id_index.entries[i++];
1958 
1959 			e->id = evsel->id[j];
1960 
1961 			sid = perf_evlist__id2sid(evlist, e->id);
1962 			if (!sid) {
1963 				free(ev);
1964 				return -ENOENT;
1965 			}
1966 
1967 			e->idx = sid->idx;
1968 			e->cpu = sid->cpu;
1969 			e->tid = sid->tid;
1970 		}
1971 	}
1972 
1973 	sz = sizeof(struct id_index_event) + nr * sizeof(struct id_index_entry);
1974 	ev->id_index.header.size = sz;
1975 	ev->id_index.nr = nr;
1976 
1977 	err = process(tool, ev, NULL, machine);
1978 out_err:
1979 	free(ev);
1980 
1981 	return err;
1982 }
1983