xref: /openbmc/linux/tools/perf/util/session.c (revision 69996df4)
1 #define _FILE_OFFSET_BITS 64
2 
3 #include <linux/kernel.h>
4 
5 #include <byteswap.h>
6 #include <unistd.h>
7 #include <sys/types.h>
8 #include <sys/mman.h>
9 
10 #include "evlist.h"
11 #include "evsel.h"
12 #include "session.h"
13 #include "tool.h"
14 #include "sort.h"
15 #include "util.h"
16 #include "cpumap.h"
17 
18 static int perf_session__open(struct perf_session *self, bool force)
19 {
20 	struct stat input_stat;
21 
22 	if (!strcmp(self->filename, "-")) {
23 		self->fd_pipe = true;
24 		self->fd = STDIN_FILENO;
25 
26 		if (perf_session__read_header(self, self->fd) < 0)
27 			pr_err("incompatible file format (rerun with -v to learn more)");
28 
29 		return 0;
30 	}
31 
32 	self->fd = open(self->filename, O_RDONLY);
33 	if (self->fd < 0) {
34 		int err = errno;
35 
36 		pr_err("failed to open %s: %s", self->filename, strerror(err));
37 		if (err == ENOENT && !strcmp(self->filename, "perf.data"))
38 			pr_err("  (try 'perf record' first)");
39 		pr_err("\n");
40 		return -errno;
41 	}
42 
43 	if (fstat(self->fd, &input_stat) < 0)
44 		goto out_close;
45 
46 	if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
47 		pr_err("file %s not owned by current user or root\n",
48 		       self->filename);
49 		goto out_close;
50 	}
51 
52 	if (!input_stat.st_size) {
53 		pr_info("zero-sized file (%s), nothing to do!\n",
54 			self->filename);
55 		goto out_close;
56 	}
57 
58 	if (perf_session__read_header(self, self->fd) < 0) {
59 		pr_err("incompatible file format (rerun with -v to learn more)");
60 		goto out_close;
61 	}
62 
63 	if (!perf_evlist__valid_sample_type(self->evlist)) {
64 		pr_err("non matching sample_type");
65 		goto out_close;
66 	}
67 
68 	if (!perf_evlist__valid_sample_id_all(self->evlist)) {
69 		pr_err("non matching sample_id_all");
70 		goto out_close;
71 	}
72 
73 	self->size = input_stat.st_size;
74 	return 0;
75 
76 out_close:
77 	close(self->fd);
78 	self->fd = -1;
79 	return -1;
80 }
81 
82 void perf_session__update_sample_type(struct perf_session *self)
83 {
84 	self->sample_type = perf_evlist__sample_type(self->evlist);
85 	self->sample_size = __perf_evsel__sample_size(self->sample_type);
86 	self->sample_id_all = perf_evlist__sample_id_all(self->evlist);
87 	self->id_hdr_size = perf_evlist__id_hdr_size(self->evlist);
88 	self->host_machine.id_hdr_size = self->id_hdr_size;
89 }
90 
91 int perf_session__create_kernel_maps(struct perf_session *self)
92 {
93 	int ret = machine__create_kernel_maps(&self->host_machine);
94 
95 	if (ret >= 0)
96 		ret = machines__create_guest_kernel_maps(&self->machines);
97 	return ret;
98 }
99 
100 static void perf_session__destroy_kernel_maps(struct perf_session *self)
101 {
102 	machine__destroy_kernel_maps(&self->host_machine);
103 	machines__destroy_guest_kernel_maps(&self->machines);
104 }
105 
106 struct perf_session *perf_session__new(const char *filename, int mode,
107 				       bool force, bool repipe,
108 				       struct perf_tool *tool)
109 {
110 	struct perf_session *self;
111 	struct stat st;
112 	size_t len;
113 
114 	if (!filename || !strlen(filename)) {
115 		if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
116 			filename = "-";
117 		else
118 			filename = "perf.data";
119 	}
120 
121 	len = strlen(filename);
122 	self = zalloc(sizeof(*self) + len);
123 
124 	if (self == NULL)
125 		goto out;
126 
127 	memcpy(self->filename, filename, len);
128 	/*
129 	 * On 64bit we can mmap the data file in one go. No need for tiny mmap
130 	 * slices. On 32bit we use 32MB.
131 	 */
132 #if BITS_PER_LONG == 64
133 	self->mmap_window = ULLONG_MAX;
134 #else
135 	self->mmap_window = 32 * 1024 * 1024ULL;
136 #endif
137 	self->machines = RB_ROOT;
138 	self->repipe = repipe;
139 	INIT_LIST_HEAD(&self->ordered_samples.samples);
140 	INIT_LIST_HEAD(&self->ordered_samples.sample_cache);
141 	INIT_LIST_HEAD(&self->ordered_samples.to_free);
142 	machine__init(&self->host_machine, "", HOST_KERNEL_ID);
143 
144 	if (mode == O_RDONLY) {
145 		if (perf_session__open(self, force) < 0)
146 			goto out_delete;
147 		perf_session__update_sample_type(self);
148 	} else if (mode == O_WRONLY) {
149 		/*
150 		 * In O_RDONLY mode this will be performed when reading the
151 		 * kernel MMAP event, in perf_event__process_mmap().
152 		 */
153 		if (perf_session__create_kernel_maps(self) < 0)
154 			goto out_delete;
155 	}
156 
157 	if (tool && tool->ordering_requires_timestamps &&
158 	    tool->ordered_samples && !self->sample_id_all) {
159 		dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n");
160 		tool->ordered_samples = false;
161 	}
162 
163 out:
164 	return self;
165 out_delete:
166 	perf_session__delete(self);
167 	return NULL;
168 }
169 
170 static void machine__delete_dead_threads(struct machine *machine)
171 {
172 	struct thread *n, *t;
173 
174 	list_for_each_entry_safe(t, n, &machine->dead_threads, node) {
175 		list_del(&t->node);
176 		thread__delete(t);
177 	}
178 }
179 
180 static void perf_session__delete_dead_threads(struct perf_session *session)
181 {
182 	machine__delete_dead_threads(&session->host_machine);
183 }
184 
185 static void machine__delete_threads(struct machine *self)
186 {
187 	struct rb_node *nd = rb_first(&self->threads);
188 
189 	while (nd) {
190 		struct thread *t = rb_entry(nd, struct thread, rb_node);
191 
192 		rb_erase(&t->rb_node, &self->threads);
193 		nd = rb_next(nd);
194 		thread__delete(t);
195 	}
196 }
197 
198 static void perf_session__delete_threads(struct perf_session *session)
199 {
200 	machine__delete_threads(&session->host_machine);
201 }
202 
203 void perf_session__delete(struct perf_session *self)
204 {
205 	perf_session__destroy_kernel_maps(self);
206 	perf_session__delete_dead_threads(self);
207 	perf_session__delete_threads(self);
208 	machine__exit(&self->host_machine);
209 	close(self->fd);
210 	free(self);
211 }
212 
213 void machine__remove_thread(struct machine *self, struct thread *th)
214 {
215 	self->last_match = NULL;
216 	rb_erase(&th->rb_node, &self->threads);
217 	/*
218 	 * We may have references to this thread, for instance in some hist_entry
219 	 * instances, so just move them to a separate list.
220 	 */
221 	list_add_tail(&th->node, &self->dead_threads);
222 }
223 
224 static bool symbol__match_parent_regex(struct symbol *sym)
225 {
226 	if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
227 		return 1;
228 
229 	return 0;
230 }
231 
232 static const u8 cpumodes[] = {
233 	PERF_RECORD_MISC_USER,
234 	PERF_RECORD_MISC_KERNEL,
235 	PERF_RECORD_MISC_GUEST_USER,
236 	PERF_RECORD_MISC_GUEST_KERNEL
237 };
238 #define NCPUMODES (sizeof(cpumodes)/sizeof(u8))
239 
240 static void ip__resolve_ams(struct machine *self, struct thread *thread,
241 			    struct addr_map_symbol *ams,
242 			    u64 ip)
243 {
244 	struct addr_location al;
245 	size_t i;
246 	u8 m;
247 
248 	memset(&al, 0, sizeof(al));
249 
250 	for (i = 0; i < NCPUMODES; i++) {
251 		m = cpumodes[i];
252 		/*
253 		 * We cannot use the header.misc hint to determine whether a
254 		 * branch stack address is user, kernel, guest, hypervisor.
255 		 * Branches may straddle the kernel/user/hypervisor boundaries.
256 		 * Thus, we have to try consecutively until we find a match
257 		 * or else, the symbol is unknown
258 		 */
259 		thread__find_addr_location(thread, self, m, MAP__FUNCTION,
260 				ip, &al, NULL);
261 		if (al.sym)
262 			goto found;
263 	}
264 found:
265 	ams->addr = ip;
266 	ams->sym = al.sym;
267 	ams->map = al.map;
268 }
269 
270 struct branch_info *machine__resolve_bstack(struct machine *self,
271 					    struct thread *thr,
272 					    struct branch_stack *bs)
273 {
274 	struct branch_info *bi;
275 	unsigned int i;
276 
277 	bi = calloc(bs->nr, sizeof(struct branch_info));
278 	if (!bi)
279 		return NULL;
280 
281 	for (i = 0; i < bs->nr; i++) {
282 		ip__resolve_ams(self, thr, &bi[i].to, bs->entries[i].to);
283 		ip__resolve_ams(self, thr, &bi[i].from, bs->entries[i].from);
284 		bi[i].flags = bs->entries[i].flags;
285 	}
286 	return bi;
287 }
288 
289 int machine__resolve_callchain(struct machine *self, struct perf_evsel *evsel,
290 			       struct thread *thread,
291 			       struct ip_callchain *chain,
292 			       struct symbol **parent)
293 {
294 	u8 cpumode = PERF_RECORD_MISC_USER;
295 	unsigned int i;
296 	int err;
297 
298 	callchain_cursor_reset(&evsel->hists.callchain_cursor);
299 
300 	for (i = 0; i < chain->nr; i++) {
301 		u64 ip;
302 		struct addr_location al;
303 
304 		if (callchain_param.order == ORDER_CALLEE)
305 			ip = chain->ips[i];
306 		else
307 			ip = chain->ips[chain->nr - i - 1];
308 
309 		if (ip >= PERF_CONTEXT_MAX) {
310 			switch (ip) {
311 			case PERF_CONTEXT_HV:
312 				cpumode = PERF_RECORD_MISC_HYPERVISOR;	break;
313 			case PERF_CONTEXT_KERNEL:
314 				cpumode = PERF_RECORD_MISC_KERNEL;	break;
315 			case PERF_CONTEXT_USER:
316 				cpumode = PERF_RECORD_MISC_USER;	break;
317 			default:
318 				break;
319 			}
320 			continue;
321 		}
322 
323 		al.filtered = false;
324 		thread__find_addr_location(thread, self, cpumode,
325 					   MAP__FUNCTION, ip, &al, NULL);
326 		if (al.sym != NULL) {
327 			if (sort__has_parent && !*parent &&
328 			    symbol__match_parent_regex(al.sym))
329 				*parent = al.sym;
330 			if (!symbol_conf.use_callchain)
331 				break;
332 		}
333 
334 		err = callchain_cursor_append(&evsel->hists.callchain_cursor,
335 					      ip, al.map, al.sym);
336 		if (err)
337 			return err;
338 	}
339 
340 	return 0;
341 }
342 
343 static int process_event_synth_tracing_data_stub(union perf_event *event __used,
344 						 struct perf_session *session __used)
345 {
346 	dump_printf(": unhandled!\n");
347 	return 0;
348 }
349 
350 static int process_event_synth_attr_stub(union perf_event *event __used,
351 					 struct perf_evlist **pevlist __used)
352 {
353 	dump_printf(": unhandled!\n");
354 	return 0;
355 }
356 
357 static int process_event_sample_stub(struct perf_tool *tool __used,
358 				     union perf_event *event __used,
359 				     struct perf_sample *sample __used,
360 				     struct perf_evsel *evsel __used,
361 				     struct machine *machine __used)
362 {
363 	dump_printf(": unhandled!\n");
364 	return 0;
365 }
366 
367 static int process_event_stub(struct perf_tool *tool __used,
368 			      union perf_event *event __used,
369 			      struct perf_sample *sample __used,
370 			      struct machine *machine __used)
371 {
372 	dump_printf(": unhandled!\n");
373 	return 0;
374 }
375 
376 static int process_finished_round_stub(struct perf_tool *tool __used,
377 				       union perf_event *event __used,
378 				       struct perf_session *perf_session __used)
379 {
380 	dump_printf(": unhandled!\n");
381 	return 0;
382 }
383 
384 static int process_event_type_stub(struct perf_tool *tool __used,
385 				   union perf_event *event __used)
386 {
387 	dump_printf(": unhandled!\n");
388 	return 0;
389 }
390 
391 static int process_finished_round(struct perf_tool *tool,
392 				  union perf_event *event,
393 				  struct perf_session *session);
394 
395 static void perf_tool__fill_defaults(struct perf_tool *tool)
396 {
397 	if (tool->sample == NULL)
398 		tool->sample = process_event_sample_stub;
399 	if (tool->mmap == NULL)
400 		tool->mmap = process_event_stub;
401 	if (tool->comm == NULL)
402 		tool->comm = process_event_stub;
403 	if (tool->fork == NULL)
404 		tool->fork = process_event_stub;
405 	if (tool->exit == NULL)
406 		tool->exit = process_event_stub;
407 	if (tool->lost == NULL)
408 		tool->lost = perf_event__process_lost;
409 	if (tool->read == NULL)
410 		tool->read = process_event_sample_stub;
411 	if (tool->throttle == NULL)
412 		tool->throttle = process_event_stub;
413 	if (tool->unthrottle == NULL)
414 		tool->unthrottle = process_event_stub;
415 	if (tool->attr == NULL)
416 		tool->attr = process_event_synth_attr_stub;
417 	if (tool->event_type == NULL)
418 		tool->event_type = process_event_type_stub;
419 	if (tool->tracing_data == NULL)
420 		tool->tracing_data = process_event_synth_tracing_data_stub;
421 	if (tool->build_id == NULL)
422 		tool->build_id = process_finished_round_stub;
423 	if (tool->finished_round == NULL) {
424 		if (tool->ordered_samples)
425 			tool->finished_round = process_finished_round;
426 		else
427 			tool->finished_round = process_finished_round_stub;
428 	}
429 }
430 
431 void mem_bswap_64(void *src, int byte_size)
432 {
433 	u64 *m = src;
434 
435 	while (byte_size > 0) {
436 		*m = bswap_64(*m);
437 		byte_size -= sizeof(u64);
438 		++m;
439 	}
440 }
441 
442 static void perf_event__all64_swap(union perf_event *event)
443 {
444 	struct perf_event_header *hdr = &event->header;
445 	mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr));
446 }
447 
448 static void perf_event__comm_swap(union perf_event *event)
449 {
450 	event->comm.pid = bswap_32(event->comm.pid);
451 	event->comm.tid = bswap_32(event->comm.tid);
452 }
453 
454 static void perf_event__mmap_swap(union perf_event *event)
455 {
456 	event->mmap.pid	  = bswap_32(event->mmap.pid);
457 	event->mmap.tid	  = bswap_32(event->mmap.tid);
458 	event->mmap.start = bswap_64(event->mmap.start);
459 	event->mmap.len	  = bswap_64(event->mmap.len);
460 	event->mmap.pgoff = bswap_64(event->mmap.pgoff);
461 }
462 
463 static void perf_event__task_swap(union perf_event *event)
464 {
465 	event->fork.pid	 = bswap_32(event->fork.pid);
466 	event->fork.tid	 = bswap_32(event->fork.tid);
467 	event->fork.ppid = bswap_32(event->fork.ppid);
468 	event->fork.ptid = bswap_32(event->fork.ptid);
469 	event->fork.time = bswap_64(event->fork.time);
470 }
471 
472 static void perf_event__read_swap(union perf_event *event)
473 {
474 	event->read.pid		 = bswap_32(event->read.pid);
475 	event->read.tid		 = bswap_32(event->read.tid);
476 	event->read.value	 = bswap_64(event->read.value);
477 	event->read.time_enabled = bswap_64(event->read.time_enabled);
478 	event->read.time_running = bswap_64(event->read.time_running);
479 	event->read.id		 = bswap_64(event->read.id);
480 }
481 
482 /* exported for swapping attributes in file header */
483 void perf_event__attr_swap(struct perf_event_attr *attr)
484 {
485 	attr->type		= bswap_32(attr->type);
486 	attr->size		= bswap_32(attr->size);
487 	attr->config		= bswap_64(attr->config);
488 	attr->sample_period	= bswap_64(attr->sample_period);
489 	attr->sample_type	= bswap_64(attr->sample_type);
490 	attr->read_format	= bswap_64(attr->read_format);
491 	attr->wakeup_events	= bswap_32(attr->wakeup_events);
492 	attr->bp_type		= bswap_32(attr->bp_type);
493 	attr->bp_addr		= bswap_64(attr->bp_addr);
494 	attr->bp_len		= bswap_64(attr->bp_len);
495 }
496 
497 static void perf_event__hdr_attr_swap(union perf_event *event)
498 {
499 	size_t size;
500 
501 	perf_event__attr_swap(&event->attr.attr);
502 
503 	size = event->header.size;
504 	size -= (void *)&event->attr.id - (void *)event;
505 	mem_bswap_64(event->attr.id, size);
506 }
507 
508 static void perf_event__event_type_swap(union perf_event *event)
509 {
510 	event->event_type.event_type.event_id =
511 		bswap_64(event->event_type.event_type.event_id);
512 }
513 
514 static void perf_event__tracing_data_swap(union perf_event *event)
515 {
516 	event->tracing_data.size = bswap_32(event->tracing_data.size);
517 }
518 
519 typedef void (*perf_event__swap_op)(union perf_event *event);
520 
521 static perf_event__swap_op perf_event__swap_ops[] = {
522 	[PERF_RECORD_MMAP]		  = perf_event__mmap_swap,
523 	[PERF_RECORD_COMM]		  = perf_event__comm_swap,
524 	[PERF_RECORD_FORK]		  = perf_event__task_swap,
525 	[PERF_RECORD_EXIT]		  = perf_event__task_swap,
526 	[PERF_RECORD_LOST]		  = perf_event__all64_swap,
527 	[PERF_RECORD_READ]		  = perf_event__read_swap,
528 	[PERF_RECORD_SAMPLE]		  = perf_event__all64_swap,
529 	[PERF_RECORD_HEADER_ATTR]	  = perf_event__hdr_attr_swap,
530 	[PERF_RECORD_HEADER_EVENT_TYPE]	  = perf_event__event_type_swap,
531 	[PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap,
532 	[PERF_RECORD_HEADER_BUILD_ID]	  = NULL,
533 	[PERF_RECORD_HEADER_MAX]	  = NULL,
534 };
535 
536 struct sample_queue {
537 	u64			timestamp;
538 	u64			file_offset;
539 	union perf_event	*event;
540 	struct list_head	list;
541 };
542 
543 static void perf_session_free_sample_buffers(struct perf_session *session)
544 {
545 	struct ordered_samples *os = &session->ordered_samples;
546 
547 	while (!list_empty(&os->to_free)) {
548 		struct sample_queue *sq;
549 
550 		sq = list_entry(os->to_free.next, struct sample_queue, list);
551 		list_del(&sq->list);
552 		free(sq);
553 	}
554 }
555 
556 static int perf_session_deliver_event(struct perf_session *session,
557 				      union perf_event *event,
558 				      struct perf_sample *sample,
559 				      struct perf_tool *tool,
560 				      u64 file_offset);
561 
562 static void flush_sample_queue(struct perf_session *s,
563 			       struct perf_tool *tool)
564 {
565 	struct ordered_samples *os = &s->ordered_samples;
566 	struct list_head *head = &os->samples;
567 	struct sample_queue *tmp, *iter;
568 	struct perf_sample sample;
569 	u64 limit = os->next_flush;
570 	u64 last_ts = os->last_sample ? os->last_sample->timestamp : 0ULL;
571 	unsigned idx = 0, progress_next = os->nr_samples / 16;
572 	int ret;
573 
574 	if (!tool->ordered_samples || !limit)
575 		return;
576 
577 	list_for_each_entry_safe(iter, tmp, head, list) {
578 		if (iter->timestamp > limit)
579 			break;
580 
581 		ret = perf_session__parse_sample(s, iter->event, &sample);
582 		if (ret)
583 			pr_err("Can't parse sample, err = %d\n", ret);
584 		else
585 			perf_session_deliver_event(s, iter->event, &sample, tool,
586 						   iter->file_offset);
587 
588 		os->last_flush = iter->timestamp;
589 		list_del(&iter->list);
590 		list_add(&iter->list, &os->sample_cache);
591 		if (++idx >= progress_next) {
592 			progress_next += os->nr_samples / 16;
593 			ui_progress__update(idx, os->nr_samples,
594 					    "Processing time ordered events...");
595 		}
596 	}
597 
598 	if (list_empty(head)) {
599 		os->last_sample = NULL;
600 	} else if (last_ts <= limit) {
601 		os->last_sample =
602 			list_entry(head->prev, struct sample_queue, list);
603 	}
604 
605 	os->nr_samples = 0;
606 }
607 
608 /*
609  * When perf record finishes a pass on every buffers, it records this pseudo
610  * event.
611  * We record the max timestamp t found in the pass n.
612  * Assuming these timestamps are monotonic across cpus, we know that if
613  * a buffer still has events with timestamps below t, they will be all
614  * available and then read in the pass n + 1.
615  * Hence when we start to read the pass n + 2, we can safely flush every
616  * events with timestamps below t.
617  *
618  *    ============ PASS n =================
619  *       CPU 0         |   CPU 1
620  *                     |
621  *    cnt1 timestamps  |   cnt2 timestamps
622  *          1          |         2
623  *          2          |         3
624  *          -          |         4  <--- max recorded
625  *
626  *    ============ PASS n + 1 ==============
627  *       CPU 0         |   CPU 1
628  *                     |
629  *    cnt1 timestamps  |   cnt2 timestamps
630  *          3          |         5
631  *          4          |         6
632  *          5          |         7 <---- max recorded
633  *
634  *      Flush every events below timestamp 4
635  *
636  *    ============ PASS n + 2 ==============
637  *       CPU 0         |   CPU 1
638  *                     |
639  *    cnt1 timestamps  |   cnt2 timestamps
640  *          6          |         8
641  *          7          |         9
642  *          -          |         10
643  *
644  *      Flush every events below timestamp 7
645  *      etc...
646  */
647 static int process_finished_round(struct perf_tool *tool,
648 				  union perf_event *event __used,
649 				  struct perf_session *session)
650 {
651 	flush_sample_queue(session, tool);
652 	session->ordered_samples.next_flush = session->ordered_samples.max_timestamp;
653 
654 	return 0;
655 }
656 
657 /* The queue is ordered by time */
658 static void __queue_event(struct sample_queue *new, struct perf_session *s)
659 {
660 	struct ordered_samples *os = &s->ordered_samples;
661 	struct sample_queue *sample = os->last_sample;
662 	u64 timestamp = new->timestamp;
663 	struct list_head *p;
664 
665 	++os->nr_samples;
666 	os->last_sample = new;
667 
668 	if (!sample) {
669 		list_add(&new->list, &os->samples);
670 		os->max_timestamp = timestamp;
671 		return;
672 	}
673 
674 	/*
675 	 * last_sample might point to some random place in the list as it's
676 	 * the last queued event. We expect that the new event is close to
677 	 * this.
678 	 */
679 	if (sample->timestamp <= timestamp) {
680 		while (sample->timestamp <= timestamp) {
681 			p = sample->list.next;
682 			if (p == &os->samples) {
683 				list_add_tail(&new->list, &os->samples);
684 				os->max_timestamp = timestamp;
685 				return;
686 			}
687 			sample = list_entry(p, struct sample_queue, list);
688 		}
689 		list_add_tail(&new->list, &sample->list);
690 	} else {
691 		while (sample->timestamp > timestamp) {
692 			p = sample->list.prev;
693 			if (p == &os->samples) {
694 				list_add(&new->list, &os->samples);
695 				return;
696 			}
697 			sample = list_entry(p, struct sample_queue, list);
698 		}
699 		list_add(&new->list, &sample->list);
700 	}
701 }
702 
703 #define MAX_SAMPLE_BUFFER	(64 * 1024 / sizeof(struct sample_queue))
704 
705 static int perf_session_queue_event(struct perf_session *s, union perf_event *event,
706 				    struct perf_sample *sample, u64 file_offset)
707 {
708 	struct ordered_samples *os = &s->ordered_samples;
709 	struct list_head *sc = &os->sample_cache;
710 	u64 timestamp = sample->time;
711 	struct sample_queue *new;
712 
713 	if (!timestamp || timestamp == ~0ULL)
714 		return -ETIME;
715 
716 	if (timestamp < s->ordered_samples.last_flush) {
717 		printf("Warning: Timestamp below last timeslice flush\n");
718 		return -EINVAL;
719 	}
720 
721 	if (!list_empty(sc)) {
722 		new = list_entry(sc->next, struct sample_queue, list);
723 		list_del(&new->list);
724 	} else if (os->sample_buffer) {
725 		new = os->sample_buffer + os->sample_buffer_idx;
726 		if (++os->sample_buffer_idx == MAX_SAMPLE_BUFFER)
727 			os->sample_buffer = NULL;
728 	} else {
729 		os->sample_buffer = malloc(MAX_SAMPLE_BUFFER * sizeof(*new));
730 		if (!os->sample_buffer)
731 			return -ENOMEM;
732 		list_add(&os->sample_buffer->list, &os->to_free);
733 		os->sample_buffer_idx = 2;
734 		new = os->sample_buffer + 1;
735 	}
736 
737 	new->timestamp = timestamp;
738 	new->file_offset = file_offset;
739 	new->event = event;
740 
741 	__queue_event(new, s);
742 
743 	return 0;
744 }
745 
746 static void callchain__printf(struct perf_sample *sample)
747 {
748 	unsigned int i;
749 
750 	printf("... chain: nr:%" PRIu64 "\n", sample->callchain->nr);
751 
752 	for (i = 0; i < sample->callchain->nr; i++)
753 		printf("..... %2d: %016" PRIx64 "\n",
754 		       i, sample->callchain->ips[i]);
755 }
756 
757 static void branch_stack__printf(struct perf_sample *sample)
758 {
759 	uint64_t i;
760 
761 	printf("... branch stack: nr:%" PRIu64 "\n", sample->branch_stack->nr);
762 
763 	for (i = 0; i < sample->branch_stack->nr; i++)
764 		printf("..... %2"PRIu64": %016" PRIx64 " -> %016" PRIx64 "\n",
765 			i, sample->branch_stack->entries[i].from,
766 			sample->branch_stack->entries[i].to);
767 }
768 
769 static void perf_session__print_tstamp(struct perf_session *session,
770 				       union perf_event *event,
771 				       struct perf_sample *sample)
772 {
773 	if (event->header.type != PERF_RECORD_SAMPLE &&
774 	    !session->sample_id_all) {
775 		fputs("-1 -1 ", stdout);
776 		return;
777 	}
778 
779 	if ((session->sample_type & PERF_SAMPLE_CPU))
780 		printf("%u ", sample->cpu);
781 
782 	if (session->sample_type & PERF_SAMPLE_TIME)
783 		printf("%" PRIu64 " ", sample->time);
784 }
785 
786 static void dump_event(struct perf_session *session, union perf_event *event,
787 		       u64 file_offset, struct perf_sample *sample)
788 {
789 	if (!dump_trace)
790 		return;
791 
792 	printf("\n%#" PRIx64 " [%#x]: event: %d\n",
793 	       file_offset, event->header.size, event->header.type);
794 
795 	trace_event(event);
796 
797 	if (sample)
798 		perf_session__print_tstamp(session, event, sample);
799 
800 	printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset,
801 	       event->header.size, perf_event__name(event->header.type));
802 }
803 
804 static void dump_sample(struct perf_session *session, union perf_event *event,
805 			struct perf_sample *sample)
806 {
807 	if (!dump_trace)
808 		return;
809 
810 	printf("(IP, %d): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n",
811 	       event->header.misc, sample->pid, sample->tid, sample->ip,
812 	       sample->period, sample->addr);
813 
814 	if (session->sample_type & PERF_SAMPLE_CALLCHAIN)
815 		callchain__printf(sample);
816 
817 	if (session->sample_type & PERF_SAMPLE_BRANCH_STACK)
818 		branch_stack__printf(sample);
819 }
820 
821 static struct machine *
822 	perf_session__find_machine_for_cpumode(struct perf_session *session,
823 					       union perf_event *event)
824 {
825 	const u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
826 
827 	if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest)
828 		return perf_session__find_machine(session, event->ip.pid);
829 
830 	return perf_session__find_host_machine(session);
831 }
832 
833 static int perf_session_deliver_event(struct perf_session *session,
834 				      union perf_event *event,
835 				      struct perf_sample *sample,
836 				      struct perf_tool *tool,
837 				      u64 file_offset)
838 {
839 	struct perf_evsel *evsel;
840 	struct machine *machine;
841 
842 	dump_event(session, event, file_offset, sample);
843 
844 	evsel = perf_evlist__id2evsel(session->evlist, sample->id);
845 	if (evsel != NULL && event->header.type != PERF_RECORD_SAMPLE) {
846 		/*
847 		 * XXX We're leaving PERF_RECORD_SAMPLE unnacounted here
848 		 * because the tools right now may apply filters, discarding
849 		 * some of the samples. For consistency, in the future we
850 		 * should have something like nr_filtered_samples and remove
851 		 * the sample->period from total_sample_period, etc, KISS for
852 		 * now tho.
853 		 *
854 		 * Also testing against NULL allows us to handle files without
855 		 * attr.sample_id_all and/or without PERF_SAMPLE_ID. In the
856 		 * future probably it'll be a good idea to restrict event
857 		 * processing via perf_session to files with both set.
858 		 */
859 		hists__inc_nr_events(&evsel->hists, event->header.type);
860 	}
861 
862 	machine = perf_session__find_machine_for_cpumode(session, event);
863 
864 	switch (event->header.type) {
865 	case PERF_RECORD_SAMPLE:
866 		dump_sample(session, event, sample);
867 		if (evsel == NULL) {
868 			++session->hists.stats.nr_unknown_id;
869 			return -1;
870 		}
871 		if (machine == NULL) {
872 			++session->hists.stats.nr_unprocessable_samples;
873 			return -1;
874 		}
875 		return tool->sample(tool, event, sample, evsel, machine);
876 	case PERF_RECORD_MMAP:
877 		return tool->mmap(tool, event, sample, machine);
878 	case PERF_RECORD_COMM:
879 		return tool->comm(tool, event, sample, machine);
880 	case PERF_RECORD_FORK:
881 		return tool->fork(tool, event, sample, machine);
882 	case PERF_RECORD_EXIT:
883 		return tool->exit(tool, event, sample, machine);
884 	case PERF_RECORD_LOST:
885 		if (tool->lost == perf_event__process_lost)
886 			session->hists.stats.total_lost += event->lost.lost;
887 		return tool->lost(tool, event, sample, machine);
888 	case PERF_RECORD_READ:
889 		return tool->read(tool, event, sample, evsel, machine);
890 	case PERF_RECORD_THROTTLE:
891 		return tool->throttle(tool, event, sample, machine);
892 	case PERF_RECORD_UNTHROTTLE:
893 		return tool->unthrottle(tool, event, sample, machine);
894 	default:
895 		++session->hists.stats.nr_unknown_events;
896 		return -1;
897 	}
898 }
899 
900 static int perf_session__preprocess_sample(struct perf_session *session,
901 					   union perf_event *event, struct perf_sample *sample)
902 {
903 	if (event->header.type != PERF_RECORD_SAMPLE ||
904 	    !(session->sample_type & PERF_SAMPLE_CALLCHAIN))
905 		return 0;
906 
907 	if (!ip_callchain__valid(sample->callchain, event)) {
908 		pr_debug("call-chain problem with event, skipping it.\n");
909 		++session->hists.stats.nr_invalid_chains;
910 		session->hists.stats.total_invalid_chains += sample->period;
911 		return -EINVAL;
912 	}
913 	return 0;
914 }
915 
916 static int perf_session__process_user_event(struct perf_session *session, union perf_event *event,
917 					    struct perf_tool *tool, u64 file_offset)
918 {
919 	int err;
920 
921 	dump_event(session, event, file_offset, NULL);
922 
923 	/* These events are processed right away */
924 	switch (event->header.type) {
925 	case PERF_RECORD_HEADER_ATTR:
926 		err = tool->attr(event, &session->evlist);
927 		if (err == 0)
928 			perf_session__update_sample_type(session);
929 		return err;
930 	case PERF_RECORD_HEADER_EVENT_TYPE:
931 		return tool->event_type(tool, event);
932 	case PERF_RECORD_HEADER_TRACING_DATA:
933 		/* setup for reading amidst mmap */
934 		lseek(session->fd, file_offset, SEEK_SET);
935 		return tool->tracing_data(event, session);
936 	case PERF_RECORD_HEADER_BUILD_ID:
937 		return tool->build_id(tool, event, session);
938 	case PERF_RECORD_FINISHED_ROUND:
939 		return tool->finished_round(tool, event, session);
940 	default:
941 		return -EINVAL;
942 	}
943 }
944 
945 static int perf_session__process_event(struct perf_session *session,
946 				       union perf_event *event,
947 				       struct perf_tool *tool,
948 				       u64 file_offset)
949 {
950 	struct perf_sample sample;
951 	int ret;
952 
953 	if (session->header.needs_swap &&
954 	    perf_event__swap_ops[event->header.type])
955 		perf_event__swap_ops[event->header.type](event);
956 
957 	if (event->header.type >= PERF_RECORD_HEADER_MAX)
958 		return -EINVAL;
959 
960 	hists__inc_nr_events(&session->hists, event->header.type);
961 
962 	if (event->header.type >= PERF_RECORD_USER_TYPE_START)
963 		return perf_session__process_user_event(session, event, tool, file_offset);
964 
965 	/*
966 	 * For all kernel events we get the sample data
967 	 */
968 	ret = perf_session__parse_sample(session, event, &sample);
969 	if (ret)
970 		return ret;
971 
972 	/* Preprocess sample records - precheck callchains */
973 	if (perf_session__preprocess_sample(session, event, &sample))
974 		return 0;
975 
976 	if (tool->ordered_samples) {
977 		ret = perf_session_queue_event(session, event, &sample,
978 					       file_offset);
979 		if (ret != -ETIME)
980 			return ret;
981 	}
982 
983 	return perf_session_deliver_event(session, event, &sample, tool,
984 					  file_offset);
985 }
986 
987 void perf_event_header__bswap(struct perf_event_header *self)
988 {
989 	self->type = bswap_32(self->type);
990 	self->misc = bswap_16(self->misc);
991 	self->size = bswap_16(self->size);
992 }
993 
994 struct thread *perf_session__findnew(struct perf_session *session, pid_t pid)
995 {
996 	return machine__findnew_thread(&session->host_machine, pid);
997 }
998 
999 static struct thread *perf_session__register_idle_thread(struct perf_session *self)
1000 {
1001 	struct thread *thread = perf_session__findnew(self, 0);
1002 
1003 	if (thread == NULL || thread__set_comm(thread, "swapper")) {
1004 		pr_err("problem inserting idle task.\n");
1005 		thread = NULL;
1006 	}
1007 
1008 	return thread;
1009 }
1010 
1011 static void perf_session__warn_about_errors(const struct perf_session *session,
1012 					    const struct perf_tool *tool)
1013 {
1014 	if (tool->lost == perf_event__process_lost &&
1015 	    session->hists.stats.nr_events[PERF_RECORD_LOST] != 0) {
1016 		ui__warning("Processed %d events and lost %d chunks!\n\n"
1017 			    "Check IO/CPU overload!\n\n",
1018 			    session->hists.stats.nr_events[0],
1019 			    session->hists.stats.nr_events[PERF_RECORD_LOST]);
1020 	}
1021 
1022 	if (session->hists.stats.nr_unknown_events != 0) {
1023 		ui__warning("Found %u unknown events!\n\n"
1024 			    "Is this an older tool processing a perf.data "
1025 			    "file generated by a more recent tool?\n\n"
1026 			    "If that is not the case, consider "
1027 			    "reporting to linux-kernel@vger.kernel.org.\n\n",
1028 			    session->hists.stats.nr_unknown_events);
1029 	}
1030 
1031 	if (session->hists.stats.nr_unknown_id != 0) {
1032 		ui__warning("%u samples with id not present in the header\n",
1033 			    session->hists.stats.nr_unknown_id);
1034 	}
1035 
1036  	if (session->hists.stats.nr_invalid_chains != 0) {
1037  		ui__warning("Found invalid callchains!\n\n"
1038  			    "%u out of %u events were discarded for this reason.\n\n"
1039  			    "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
1040  			    session->hists.stats.nr_invalid_chains,
1041  			    session->hists.stats.nr_events[PERF_RECORD_SAMPLE]);
1042  	}
1043 
1044 	if (session->hists.stats.nr_unprocessable_samples != 0) {
1045 		ui__warning("%u unprocessable samples recorded.\n"
1046 			    "Do you have a KVM guest running and not using 'perf kvm'?\n",
1047 			    session->hists.stats.nr_unprocessable_samples);
1048 	}
1049 }
1050 
1051 #define session_done()	(*(volatile int *)(&session_done))
1052 volatile int session_done;
1053 
1054 static int __perf_session__process_pipe_events(struct perf_session *self,
1055 					       struct perf_tool *tool)
1056 {
1057 	union perf_event event;
1058 	uint32_t size;
1059 	int skip = 0;
1060 	u64 head;
1061 	int err;
1062 	void *p;
1063 
1064 	perf_tool__fill_defaults(tool);
1065 
1066 	head = 0;
1067 more:
1068 	err = readn(self->fd, &event, sizeof(struct perf_event_header));
1069 	if (err <= 0) {
1070 		if (err == 0)
1071 			goto done;
1072 
1073 		pr_err("failed to read event header\n");
1074 		goto out_err;
1075 	}
1076 
1077 	if (self->header.needs_swap)
1078 		perf_event_header__bswap(&event.header);
1079 
1080 	size = event.header.size;
1081 	if (size == 0)
1082 		size = 8;
1083 
1084 	p = &event;
1085 	p += sizeof(struct perf_event_header);
1086 
1087 	if (size - sizeof(struct perf_event_header)) {
1088 		err = readn(self->fd, p, size - sizeof(struct perf_event_header));
1089 		if (err <= 0) {
1090 			if (err == 0) {
1091 				pr_err("unexpected end of event stream\n");
1092 				goto done;
1093 			}
1094 
1095 			pr_err("failed to read event data\n");
1096 			goto out_err;
1097 		}
1098 	}
1099 
1100 	if ((skip = perf_session__process_event(self, &event, tool, head)) < 0) {
1101 		dump_printf("%#" PRIx64 " [%#x]: skipping unknown header type: %d\n",
1102 			    head, event.header.size, event.header.type);
1103 		/*
1104 		 * assume we lost track of the stream, check alignment, and
1105 		 * increment a single u64 in the hope to catch on again 'soon'.
1106 		 */
1107 		if (unlikely(head & 7))
1108 			head &= ~7ULL;
1109 
1110 		size = 8;
1111 	}
1112 
1113 	head += size;
1114 
1115 	if (skip > 0)
1116 		head += skip;
1117 
1118 	if (!session_done())
1119 		goto more;
1120 done:
1121 	err = 0;
1122 out_err:
1123 	perf_session__warn_about_errors(self, tool);
1124 	perf_session_free_sample_buffers(self);
1125 	return err;
1126 }
1127 
1128 static union perf_event *
1129 fetch_mmaped_event(struct perf_session *session,
1130 		   u64 head, size_t mmap_size, char *buf)
1131 {
1132 	union perf_event *event;
1133 
1134 	/*
1135 	 * Ensure we have enough space remaining to read
1136 	 * the size of the event in the headers.
1137 	 */
1138 	if (head + sizeof(event->header) > mmap_size)
1139 		return NULL;
1140 
1141 	event = (union perf_event *)(buf + head);
1142 
1143 	if (session->header.needs_swap)
1144 		perf_event_header__bswap(&event->header);
1145 
1146 	if (head + event->header.size > mmap_size)
1147 		return NULL;
1148 
1149 	return event;
1150 }
1151 
1152 int __perf_session__process_events(struct perf_session *session,
1153 				   u64 data_offset, u64 data_size,
1154 				   u64 file_size, struct perf_tool *tool)
1155 {
1156 	u64 head, page_offset, file_offset, file_pos, progress_next;
1157 	int err, mmap_prot, mmap_flags, map_idx = 0;
1158 	size_t	page_size, mmap_size;
1159 	char *buf, *mmaps[8];
1160 	union perf_event *event;
1161 	uint32_t size;
1162 
1163 	perf_tool__fill_defaults(tool);
1164 
1165 	page_size = sysconf(_SC_PAGESIZE);
1166 
1167 	page_offset = page_size * (data_offset / page_size);
1168 	file_offset = page_offset;
1169 	head = data_offset - page_offset;
1170 
1171 	if (data_offset + data_size < file_size)
1172 		file_size = data_offset + data_size;
1173 
1174 	progress_next = file_size / 16;
1175 
1176 	mmap_size = session->mmap_window;
1177 	if (mmap_size > file_size)
1178 		mmap_size = file_size;
1179 
1180 	memset(mmaps, 0, sizeof(mmaps));
1181 
1182 	mmap_prot  = PROT_READ;
1183 	mmap_flags = MAP_SHARED;
1184 
1185 	if (session->header.needs_swap) {
1186 		mmap_prot  |= PROT_WRITE;
1187 		mmap_flags = MAP_PRIVATE;
1188 	}
1189 remap:
1190 	buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, session->fd,
1191 		   file_offset);
1192 	if (buf == MAP_FAILED) {
1193 		pr_err("failed to mmap file\n");
1194 		err = -errno;
1195 		goto out_err;
1196 	}
1197 	mmaps[map_idx] = buf;
1198 	map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
1199 	file_pos = file_offset + head;
1200 
1201 more:
1202 	event = fetch_mmaped_event(session, head, mmap_size, buf);
1203 	if (!event) {
1204 		if (mmaps[map_idx]) {
1205 			munmap(mmaps[map_idx], mmap_size);
1206 			mmaps[map_idx] = NULL;
1207 		}
1208 
1209 		page_offset = page_size * (head / page_size);
1210 		file_offset += page_offset;
1211 		head -= page_offset;
1212 		goto remap;
1213 	}
1214 
1215 	size = event->header.size;
1216 
1217 	if (size == 0 ||
1218 	    perf_session__process_event(session, event, tool, file_pos) < 0) {
1219 		dump_printf("%#" PRIx64 " [%#x]: skipping unknown header type: %d\n",
1220 			    file_offset + head, event->header.size,
1221 			    event->header.type);
1222 		/*
1223 		 * assume we lost track of the stream, check alignment, and
1224 		 * increment a single u64 in the hope to catch on again 'soon'.
1225 		 */
1226 		if (unlikely(head & 7))
1227 			head &= ~7ULL;
1228 
1229 		size = 8;
1230 	}
1231 
1232 	head += size;
1233 	file_pos += size;
1234 
1235 	if (file_pos >= progress_next) {
1236 		progress_next += file_size / 16;
1237 		ui_progress__update(file_pos, file_size,
1238 				    "Processing events...");
1239 	}
1240 
1241 	if (file_pos < file_size)
1242 		goto more;
1243 
1244 	err = 0;
1245 	/* do the final flush for ordered samples */
1246 	session->ordered_samples.next_flush = ULLONG_MAX;
1247 	flush_sample_queue(session, tool);
1248 out_err:
1249 	perf_session__warn_about_errors(session, tool);
1250 	perf_session_free_sample_buffers(session);
1251 	return err;
1252 }
1253 
1254 int perf_session__process_events(struct perf_session *self,
1255 				 struct perf_tool *tool)
1256 {
1257 	int err;
1258 
1259 	if (perf_session__register_idle_thread(self) == NULL)
1260 		return -ENOMEM;
1261 
1262 	if (!self->fd_pipe)
1263 		err = __perf_session__process_events(self,
1264 						     self->header.data_offset,
1265 						     self->header.data_size,
1266 						     self->size, tool);
1267 	else
1268 		err = __perf_session__process_pipe_events(self, tool);
1269 
1270 	return err;
1271 }
1272 
1273 bool perf_session__has_traces(struct perf_session *self, const char *msg)
1274 {
1275 	if (!(self->sample_type & PERF_SAMPLE_RAW)) {
1276 		pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
1277 		return false;
1278 	}
1279 
1280 	return true;
1281 }
1282 
1283 int maps__set_kallsyms_ref_reloc_sym(struct map **maps,
1284 				     const char *symbol_name, u64 addr)
1285 {
1286 	char *bracket;
1287 	enum map_type i;
1288 	struct ref_reloc_sym *ref;
1289 
1290 	ref = zalloc(sizeof(struct ref_reloc_sym));
1291 	if (ref == NULL)
1292 		return -ENOMEM;
1293 
1294 	ref->name = strdup(symbol_name);
1295 	if (ref->name == NULL) {
1296 		free(ref);
1297 		return -ENOMEM;
1298 	}
1299 
1300 	bracket = strchr(ref->name, ']');
1301 	if (bracket)
1302 		*bracket = '\0';
1303 
1304 	ref->addr = addr;
1305 
1306 	for (i = 0; i < MAP__NR_TYPES; ++i) {
1307 		struct kmap *kmap = map__kmap(maps[i]);
1308 		kmap->ref_reloc_sym = ref;
1309 	}
1310 
1311 	return 0;
1312 }
1313 
1314 size_t perf_session__fprintf_dsos(struct perf_session *self, FILE *fp)
1315 {
1316 	return __dsos__fprintf(&self->host_machine.kernel_dsos, fp) +
1317 	       __dsos__fprintf(&self->host_machine.user_dsos, fp) +
1318 	       machines__fprintf_dsos(&self->machines, fp);
1319 }
1320 
1321 size_t perf_session__fprintf_dsos_buildid(struct perf_session *self, FILE *fp,
1322 					  bool with_hits)
1323 {
1324 	size_t ret = machine__fprintf_dsos_buildid(&self->host_machine, fp, with_hits);
1325 	return ret + machines__fprintf_dsos_buildid(&self->machines, fp, with_hits);
1326 }
1327 
1328 size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp)
1329 {
1330 	struct perf_evsel *pos;
1331 	size_t ret = fprintf(fp, "Aggregated stats:\n");
1332 
1333 	ret += hists__fprintf_nr_events(&session->hists, fp);
1334 
1335 	list_for_each_entry(pos, &session->evlist->entries, node) {
1336 		ret += fprintf(fp, "%s stats:\n", event_name(pos));
1337 		ret += hists__fprintf_nr_events(&pos->hists, fp);
1338 	}
1339 
1340 	return ret;
1341 }
1342 
1343 size_t perf_session__fprintf(struct perf_session *session, FILE *fp)
1344 {
1345 	/*
1346 	 * FIXME: Here we have to actually print all the machines in this
1347 	 * session, not just the host...
1348 	 */
1349 	return machine__fprintf(&session->host_machine, fp);
1350 }
1351 
1352 void perf_session__remove_thread(struct perf_session *session,
1353 				 struct thread *th)
1354 {
1355 	/*
1356 	 * FIXME: This one makes no sense, we need to remove the thread from
1357 	 * the machine it belongs to, perf_session can have many machines, so
1358 	 * doing it always on ->host_machine is wrong.  Fix when auditing all
1359 	 * the 'perf kvm' code.
1360 	 */
1361 	machine__remove_thread(&session->host_machine, th);
1362 }
1363 
1364 struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
1365 					      unsigned int type)
1366 {
1367 	struct perf_evsel *pos;
1368 
1369 	list_for_each_entry(pos, &session->evlist->entries, node) {
1370 		if (pos->attr.type == type)
1371 			return pos;
1372 	}
1373 	return NULL;
1374 }
1375 
1376 void perf_event__print_ip(union perf_event *event, struct perf_sample *sample,
1377 			  struct machine *machine, struct perf_evsel *evsel,
1378 			  int print_sym, int print_dso, int print_symoffset)
1379 {
1380 	struct addr_location al;
1381 	struct callchain_cursor *cursor = &evsel->hists.callchain_cursor;
1382 	struct callchain_cursor_node *node;
1383 
1384 	if (perf_event__preprocess_sample(event, machine, &al, sample,
1385 					  NULL) < 0) {
1386 		error("problem processing %d event, skipping it.\n",
1387 			event->header.type);
1388 		return;
1389 	}
1390 
1391 	if (symbol_conf.use_callchain && sample->callchain) {
1392 
1393 		if (machine__resolve_callchain(machine, evsel, al.thread,
1394 						sample->callchain, NULL) != 0) {
1395 			if (verbose)
1396 				error("Failed to resolve callchain. Skipping\n");
1397 			return;
1398 		}
1399 		callchain_cursor_commit(cursor);
1400 
1401 		while (1) {
1402 			node = callchain_cursor_current(cursor);
1403 			if (!node)
1404 				break;
1405 
1406 			printf("\t%16" PRIx64, node->ip);
1407 			if (print_sym) {
1408 				printf(" ");
1409 				symbol__fprintf_symname(node->sym, stdout);
1410 			}
1411 			if (print_dso) {
1412 				printf(" (");
1413 				map__fprintf_dsoname(al.map, stdout);
1414 				printf(")");
1415 			}
1416 			printf("\n");
1417 
1418 			callchain_cursor_advance(cursor);
1419 		}
1420 
1421 	} else {
1422 		printf("%16" PRIx64, sample->ip);
1423 		if (print_sym) {
1424 			printf(" ");
1425 			if (print_symoffset)
1426 				symbol__fprintf_symname_offs(al.sym, &al,
1427 							     stdout);
1428 			else
1429 				symbol__fprintf_symname(al.sym, stdout);
1430 		}
1431 
1432 		if (print_dso) {
1433 			printf(" (");
1434 			map__fprintf_dsoname(al.map, stdout);
1435 			printf(")");
1436 		}
1437 	}
1438 }
1439 
1440 int perf_session__cpu_bitmap(struct perf_session *session,
1441 			     const char *cpu_list, unsigned long *cpu_bitmap)
1442 {
1443 	int i;
1444 	struct cpu_map *map;
1445 
1446 	for (i = 0; i < PERF_TYPE_MAX; ++i) {
1447 		struct perf_evsel *evsel;
1448 
1449 		evsel = perf_session__find_first_evtype(session, i);
1450 		if (!evsel)
1451 			continue;
1452 
1453 		if (!(evsel->attr.sample_type & PERF_SAMPLE_CPU)) {
1454 			pr_err("File does not contain CPU events. "
1455 			       "Remove -c option to proceed.\n");
1456 			return -1;
1457 		}
1458 	}
1459 
1460 	map = cpu_map__new(cpu_list);
1461 	if (map == NULL) {
1462 		pr_err("Invalid cpu_list\n");
1463 		return -1;
1464 	}
1465 
1466 	for (i = 0; i < map->nr; i++) {
1467 		int cpu = map->map[i];
1468 
1469 		if (cpu >= MAX_NR_CPUS) {
1470 			pr_err("Requested CPU %d too large. "
1471 			       "Consider raising MAX_NR_CPUS\n", cpu);
1472 			return -1;
1473 		}
1474 
1475 		set_bit(cpu, cpu_bitmap);
1476 	}
1477 
1478 	return 0;
1479 }
1480 
1481 void perf_session__fprintf_info(struct perf_session *session, FILE *fp,
1482 				bool full)
1483 {
1484 	struct stat st;
1485 	int ret;
1486 
1487 	if (session == NULL || fp == NULL)
1488 		return;
1489 
1490 	ret = fstat(session->fd, &st);
1491 	if (ret == -1)
1492 		return;
1493 
1494 	fprintf(fp, "# ========\n");
1495 	fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
1496 	perf_header__fprintf_info(session, fp, full);
1497 	fprintf(fp, "# ========\n#\n");
1498 }
1499