Linux Audio

Check our new training course

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