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