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