builtin-report.c 27.6 KB
Newer Older
1 2 3 4 5 6 7
/*
 * builtin-report.c
 *
 * Builtin report command: Analyze the perf.data input file,
 * look up and read DSOs and symbol information and display
 * a histogram of results, along various sorting keys.
 */
8
#include "builtin.h"
9

10 11
#include "util/util.h"

12
#include "util/color.h"
13
#include <linux/list.h>
14
#include "util/cache.h"
15
#include <linux/rbtree.h>
16
#include "util/symbol.h"
17
#include "util/string.h"
18
#include "util/callchain.h"
19
#include "util/strlist.h"
20
#include "util/values.h"
21

22
#include "perf.h"
23
#include "util/debug.h"
24
#include "util/header.h"
25 26 27 28

#include "util/parse-options.h"
#include "util/parse-events.h"

29
#include "util/thread.h"
30
#include "util/sort.h"
31
#include "util/hist.h"
32

33
static char		const *input_name = "perf.data";
34

35 36
static char		*dso_list_str, *comm_list_str, *sym_list_str,
			*col_width_list_str;
37
static struct strlist	*dso_list, *comm_list, *sym_list;
38

39
static int		force;
40 41
static int		input;

42
static int		full_paths;
43
static int		show_nr_samples;
44

45 46 47
static int		show_threads;
static struct perf_read_values	show_threads_values;

48 49 50
static char		default_pretty_printing_style[] = "normal";
static char		*pretty_printing_style = default_pretty_printing_style;

51 52 53
static unsigned long	page_size;
static unsigned long	mmap_window = 32;

54
static int		exclude_other = 1;
55

56 57
static char		callchain_default_opt[] = "fractal,0.5";

58 59 60 61
static char		__cwd[PATH_MAX];
static char		*cwd = __cwd;
static int		cwdlen;

62 63 64
static struct rb_root	threads;
static struct thread	*last_match;

65 66
static struct perf_header *header;

67 68
static u64		sample_type;

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask)
{
	int i;
	size_t ret = 0;

	ret += fprintf(fp, "%s", "                ");

	for (i = 0; i < depth; i++)
		if (depth_mask & (1 << i))
			ret += fprintf(fp, "|          ");
		else
			ret += fprintf(fp, "           ");

	ret += fprintf(fp, "\n");

	return ret;
}
86
static size_t
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, int depth,
		       int depth_mask, int count, u64 total_samples,
		       int hits)
{
	int i;
	size_t ret = 0;

	ret += fprintf(fp, "%s", "                ");
	for (i = 0; i < depth; i++) {
		if (depth_mask & (1 << i))
			ret += fprintf(fp, "|");
		else
			ret += fprintf(fp, " ");
		if (!count && i == depth - 1) {
			double percent;

			percent = hits * 100.0 / total_samples;
104
			ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
105 106 107 108 109 110 111 112 113 114 115
		} else
			ret += fprintf(fp, "%s", "          ");
	}
	if (chain->sym)
		ret += fprintf(fp, "%s\n", chain->sym->name);
	else
		ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);

	return ret;
}

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
static struct symbol *rem_sq_bracket;
static struct callchain_list rem_hits;

static void init_rem_hits(void)
{
	rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
	if (!rem_sq_bracket) {
		fprintf(stderr, "Not enough memory to display remaining hits\n");
		return;
	}

	strcpy(rem_sq_bracket->name, "[...]");
	rem_hits.sym = rem_sq_bracket;
}

131 132 133 134 135 136 137 138
static size_t
callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
			u64 total_samples, int depth, int depth_mask)
{
	struct rb_node *node, *next;
	struct callchain_node *child;
	struct callchain_list *chain;
	int new_depth_mask = depth_mask;
139
	u64 new_total;
140
	u64 remaining;
141 142 143
	size_t ret = 0;
	int i;

144
	if (callchain_param.mode == CHAIN_GRAPH_REL)
145
		new_total = self->children_hit;
146 147 148
	else
		new_total = total_samples;

149 150
	remaining = new_total;

151 152
	node = rb_first(&self->rb_root);
	while (node) {
153 154
		u64 cumul;

155
		child = rb_entry(node, struct callchain_node, rb_node);
156 157
		cumul = cumul_hits(child);
		remaining -= cumul;
158 159 160 161

		/*
		 * The depth mask manages the output of pipes that show
		 * the depth. We don't want to keep the pipes of the current
162 163 164
		 * level for the last child of this depth.
		 * Except if we have remaining filtered hits. They will
		 * supersede the last child
165 166
		 */
		next = rb_next(node);
167
		if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
168 169 170 171 172 173 174 175 176 177 178 179 180
			new_depth_mask &= ~(1 << (depth - 1));

		/*
		 * But we keep the older depth mask for the line seperator
		 * to keep the level link until we reach the last child
		 */
		ret += ipchain__fprintf_graph_line(fp, depth, depth_mask);
		i = 0;
		list_for_each_entry(chain, &child->val, list) {
			if (chain->ip >= PERF_CONTEXT_MAX)
				continue;
			ret += ipchain__fprintf_graph(fp, chain, depth,
						      new_depth_mask, i++,
181
						      new_total,
182
						      cumul);
183
		}
184
		ret += callchain__fprintf_graph(fp, child, new_total,
185 186 187 188 189
						depth + 1,
						new_depth_mask | (1 << depth));
		node = next;
	}

190 191 192 193 194 195 196 197 198 199 200 201 202
	if (callchain_param.mode == CHAIN_GRAPH_REL &&
		remaining && remaining != new_total) {

		if (!rem_sq_bracket)
			return ret;

		new_depth_mask &= ~(1 << (depth - 1));

		ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
					      new_depth_mask, 0, new_total,
					      remaining);
	}

203 204 205 206 207 208
	return ret;
}

static size_t
callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
			u64 total_samples)
209 210 211 212 213 214 215
{
	struct callchain_list *chain;
	size_t ret = 0;

	if (!self)
		return 0;

216
	ret += callchain__fprintf_flat(fp, self->parent, total_samples);
217 218


219 220 221 222 223 224 225
	list_for_each_entry(chain, &self->val, list) {
		if (chain->ip >= PERF_CONTEXT_MAX)
			continue;
		if (chain->sym)
			ret += fprintf(fp, "                %s\n", chain->sym->name);
		else
			ret += fprintf(fp, "                %p\n",
226
					(void *)(long)chain->ip);
227
	}
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245

	return ret;
}

static size_t
hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
			      u64 total_samples)
{
	struct rb_node *rb_node;
	struct callchain_node *chain;
	size_t ret = 0;

	rb_node = rb_first(&self->sorted_chain);
	while (rb_node) {
		double percent;

		chain = rb_entry(rb_node, struct callchain_node, rb_node);
		percent = chain->hit * 100.0 / total_samples;
246 247
		switch (callchain_param.mode) {
		case CHAIN_FLAT:
248 249
			ret += percent_color_fprintf(fp, "           %6.2f%%\n",
						     percent);
250
			ret += callchain__fprintf_flat(fp, chain, total_samples);
251 252 253
			break;
		case CHAIN_GRAPH_ABS: /* Falldown */
		case CHAIN_GRAPH_REL:
254 255
			ret += callchain__fprintf_graph(fp, chain,
							total_samples, 1, 1);
256
		case CHAIN_NONE:
257 258
		default:
			break;
259
		}
260 261 262 263 264 265 266
		ret += fprintf(fp, "\n");
		rb_node = rb_next(rb_node);
	}

	return ret;
}

267
static size_t
268
hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
269 270 271 272
{
	struct sort_entry *se;
	size_t ret;

273 274 275
	if (exclude_other && !self->parent)
		return 0;

276
	if (total_samples)
277 278 279
		ret = percent_color_fprintf(fp,
					    field_sep ? "%.2f" : "   %6.2f%%",
					(self->count * 100.0) / total_samples);
280
	else
281
		ret = fprintf(fp, field_sep ? "%lld" : "%12lld ", self->count);
282

283 284 285 286 287 288
	if (show_nr_samples) {
		if (field_sep)
			fprintf(fp, "%c%lld", *field_sep, self->count);
		else
			fprintf(fp, "%11lld", self->count);
	}
289

290
	list_for_each_entry(se, &hist_entry__sort_list, list) {
291
		if (se->elide)
292 293
			continue;

294 295
		fprintf(fp, "%s", field_sep ?: "  ");
		ret += se->print(fp, self, se->width ? *se->width : 0);
296
	}
297 298 299

	ret += fprintf(fp, "\n");

300 301 302
	if (callchain)
		hist_entry_callchain__fprintf(fp, self, total_samples);

303 304 305
	return ret;
}

306 307 308 309
/*
 *
 */

310 311 312 313 314 315 316 317 318 319 320 321
static void dso__calc_col_width(struct dso *self)
{
	if (!col_width_list_str && !field_sep &&
	    (!dso_list || strlist__has_entry(dso_list, self->name))) {
		unsigned int slen = strlen(self->name);
		if (slen > dsos__col_width)
			dsos__col_width = slen;
	}

	self->slen_calculated = 1;
}

322
static void thread__comm_adjust(struct thread *self)
323
{
324
	char *comm = self->comm;
325 326 327 328 329 330 331 332 333 334

	if (!col_width_list_str && !field_sep &&
	    (!comm_list || strlist__has_entry(comm_list, comm))) {
		unsigned int slen = strlen(comm);

		if (slen > comms__col_width) {
			comms__col_width = slen;
			threads__col_width = slen + 6;
		}
	}
335 336 337 338 339 340 341 342 343 344
}

static int thread__set_comm_adjust(struct thread *self, const char *comm)
{
	int ret = thread__set_comm(self, comm);

	if (ret)
		return ret;

	thread__comm_adjust(self);
345 346 347 348 349

	return 0;
}


350
static struct symbol *
351
resolve_symbol(struct thread *thread, struct map **mapp, u64 *ipp)
352 353
{
	struct map *map = mapp ? *mapp : NULL;
354
	u64 ip = *ipp;
355 356 357 358

	if (map)
		goto got_map;

359 360 361
	if (!thread)
		return NULL;

362 363
	map = thread__find_map(thread, ip);
	if (map != NULL) {
364 365 366 367 368
		/*
		 * We have to do this here as we may have a dso
		 * with no symbol hit that has a name longer than
		 * the ones with symbols sampled.
		 */
369
		if (!sort_dso.elide && !map->dso->slen_calculated)
370 371
			dso__calc_col_width(map->dso);

372 373 374 375 376 377 378 379 380
		if (mapp)
			*mapp = map;
got_map:
		ip = map->map_ip(map, ip);
	} else {
		/*
		 * If this is outside of all known maps,
		 * and is a negative address, try to look it
		 * up in the kernel dso, as it might be a
381 382 383 384 385
		 * vsyscall or vdso (which executes in user-mode).
		 *
		 * XXX This is nasty, we should have a symbol list in
		 * the "[vdso]" dso, but for now lets use the old
		 * trick of looking in the whole kernel symbol list.
386
		 */
387 388 389 390 391
		if ((long long)ip < 0) {
			map = kernel_map;
			if (mapp)
				*mapp = map;
		}
392
	}
393 394
	dump_printf(" ...... dso: %s\n",
		    map ? map->dso->long_name : "<not found>");
395
	dump_printf(" ...... map: %Lx -> %Lx\n", *ipp, ip);
396
	*ipp  = ip;
397

398
	return map ? map->dso->find_symbol(map->dso, ip) : NULL;
399 400
}

401
static int call__match(struct symbol *sym)
402
{
403
	if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
404
		return 1;
405

406
	return 0;
407 408
}

409 410 411
static struct symbol **resolve_callchain(struct thread *thread, struct map *map,
					 struct ip_callchain *chain,
					 struct symbol **parent)
412 413
{
	u64 context = PERF_CONTEXT_MAX;
414
	struct symbol **syms = NULL;
415
	unsigned int i;
416 417 418 419 420 421 422 423 424 425 426

	if (callchain) {
		syms = calloc(chain->nr, sizeof(*syms));
		if (!syms) {
			fprintf(stderr, "Can't allocate memory for symbols\n");
			exit(-1);
		}
	}

	for (i = 0; i < chain->nr; i++) {
		u64 ip = chain->ips[i];
427
		struct symbol *sym = NULL;
428 429 430 431 432 433 434

		if (ip >= PERF_CONTEXT_MAX) {
			context = ip;
			continue;
		}

		switch (context) {
I
Ingo Molnar 已提交
435 436
		case PERF_CONTEXT_HV:
			break;
437
		case PERF_CONTEXT_KERNEL:
438
			sym = kernel_maps__find_symbol(ip, &map);
439 440
			break;
		default:
441
			sym = resolve_symbol(thread, &map, &ip);
442 443 444 445
			break;
		}

		if (sym) {
446 447
			if (sort__has_parent && !*parent && call__match(sym))
				*parent = sym;
448 449 450 451 452 453 454 455 456
			if (!callchain)
				break;
			syms[i] = sym;
		}
	}

	return syms;
}

457 458 459 460
/*
 * collect histogram counts
 */

461
static int
462
hist_entry__add(struct thread *thread, struct map *map,
463 464
		struct symbol *sym, u64 ip, struct ip_callchain *chain,
		char level, u64 count)
465
{
466 467
	struct symbol **syms = NULL, *parent = NULL;
	bool hit;
468 469
	struct hist_entry *he;

470
	if ((sort__has_parent || callchain) && chain)
471
		syms = resolve_callchain(thread, map, chain, &parent);
472

473 474 475 476
	he = __hist_entry__add(thread, map, sym, parent,
			       ip, count, level, &hit);
	if (he == NULL)
		return -ENOMEM;
477

478 479
	if (hit)
		he->count += count;
480

481
	if (callchain) {
482 483
		if (!hit)
			callchain_init(&he->callchain);
484 485
		append_chain(&he->callchain, chain, syms);
		free(syms);
486
	}
487 488

	return 0;
489 490
}

491
static size_t output__fprintf(FILE *fp, u64 total_samples)
492
{
493
	struct hist_entry *pos;
494
	struct sort_entry *se;
495 496
	struct rb_node *nd;
	size_t ret = 0;
497 498
	unsigned int width;
	char *col_width = col_width_list_str;
499 500 501
	int raw_printing_style;

	raw_printing_style = !strcmp(pretty_printing_style, "raw");
502

503 504
	init_rem_hits();

505
	fprintf(fp, "# Samples: %Ld\n", (u64)total_samples);
506 507 508
	fprintf(fp, "#\n");

	fprintf(fp, "# Overhead");
509 510 511 512 513 514
	if (show_nr_samples) {
		if (field_sep)
			fprintf(fp, "%cSamples", *field_sep);
		else
			fputs("  Samples  ", fp);
	}
515
	list_for_each_entry(se, &hist_entry__sort_list, list) {
516
		if (se->elide)
517
			continue;
518 519
		if (field_sep) {
			fprintf(fp, "%c%s", *field_sep, se->header);
520
			continue;
521 522 523 524 525 526 527 528 529 530 531 532 533 534
		}
		width = strlen(se->header);
		if (se->width) {
			if (col_width_list_str) {
				if (col_width) {
					*se->width = atoi(col_width);
					col_width = strchr(col_width, ',');
					if (col_width)
						++col_width;
				}
			}
			width = *se->width = max(*se->width, width);
		}
		fprintf(fp, "  %*s", width, se->header);
535
	}
536 537
	fprintf(fp, "\n");

538 539 540
	if (field_sep)
		goto print_entries;

541
	fprintf(fp, "# ........");
542 543
	if (show_nr_samples)
		fprintf(fp, " ..........");
544
	list_for_each_entry(se, &hist_entry__sort_list, list) {
545
		unsigned int i;
546

547
		if (se->elide)
548 549
			continue;

550
		fprintf(fp, "  ");
551 552 553 554 555
		if (se->width)
			width = *se->width;
		else
			width = strlen(se->header);
		for (i = 0; i < width; i++)
556
			fprintf(fp, ".");
557
	}
558 559 560
	fprintf(fp, "\n");

	fprintf(fp, "#\n");
561

562
print_entries:
563 564 565
	for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
		pos = rb_entry(nd, struct hist_entry, rb_node);
		ret += hist_entry__fprintf(fp, pos, total_samples);
566 567
	}

568 569
	if (sort_order == default_sort_order &&
			parent_pattern == default_parent_pattern) {
570
		fprintf(fp, "#\n");
571
		fprintf(fp, "# (For a higher level overview, try: perf report --sort comm,dso)\n");
572 573
		fprintf(fp, "#\n");
	}
574
	fprintf(fp, "\n");
575

576 577
	free(rem_sq_bracket);

578
	if (show_threads)
579 580
		perf_read_values_display(fp, &show_threads_values,
					 raw_printing_style);
581

582 583 584
	return ret;
}

585
static int validate_chain(struct ip_callchain *chain, event_t *event)
586 587 588 589 590 591
{
	unsigned int chain_size;

	chain_size = event->header.size;
	chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;

592
	if (chain->nr*sizeof(u64) > chain_size)
593 594 595 596 597
		return -1;

	return 0;
}

598
static int
599
process_sample_event(event_t *event, unsigned long offset, unsigned long head)
600 601
{
	char level;
602
	struct symbol *sym = NULL;
603
	struct thread *thread;
604 605
	u64 ip = event->ip.ip;
	u64 period = 1;
606
	struct map *map = NULL;
607
	void *more_data = event->ip.__more_data;
608
	struct ip_callchain *chain = NULL;
609
	int cpumode;
610

611 612
	thread = threads__findnew(event->ip.pid, &threads, &last_match);

613
	if (sample_type & PERF_SAMPLE_PERIOD) {
614 615
		period = *(u64 *)more_data;
		more_data += sizeof(u64);
616
	}
617

618
	dump_printf("%p [%p]: PERF_RECORD_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
619 620 621
		(void *)(offset + head),
		(void *)(long)(event->header.size),
		event->header.misc,
622
		event->ip.pid, event->ip.tid,
623
		(void *)(long)ip,
624
		(long long)period);
625

626
	if (sample_type & PERF_SAMPLE_CALLCHAIN) {
627
		unsigned int i;
628 629 630

		chain = (void *)more_data;

631
		dump_printf("... chain: nr:%Lu\n", chain->nr);
632

633 634 635 636 637 638
		if (validate_chain(chain, event) < 0) {
			eprintf("call-chain problem with event, skipping it.\n");
			return 0;
		}

		if (dump_trace) {
639
			for (i = 0; i < chain->nr; i++)
640
				dump_printf("..... %2d: %016Lx\n", i, chain->ips[i]);
641 642 643
		}
	}

644
	dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
645 646

	if (thread == NULL) {
647
		eprintf("problem processing %d event, skipping it.\n",
648 649 650
			event->header.type);
		return -1;
	}
651

652 653 654
	if (comm_list && !strlist__has_entry(comm_list, thread->comm))
		return 0;

655
	cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
656

657
	if (cpumode == PERF_RECORD_MISC_KERNEL) {
658
		level = 'k';
659 660 661
		sym = kernel_maps__find_symbol(ip, &map);
		dump_printf(" ...... dso: %s\n",
			    map ? map->dso->long_name : "<not found>");
662
	} else if (cpumode == PERF_RECORD_MISC_USER) {
663
		level = '.';
664
		sym = resolve_symbol(thread, &map, &ip);
665

666 667
	} else {
		level = 'H';
668
		dump_printf(" ...... dso: [hypervisor]\n");
669
	}
670

671 672 673 674 675 676
	if (dso_list &&
	    (!map || !map->dso ||
	     !(strlist__has_entry(dso_list, map->dso->short_name) ||
	       (map->dso->short_name != map->dso->long_name &&
		strlist__has_entry(dso_list, map->dso->long_name)))))
		return 0;
677

678 679
	if (sym_list && sym && !strlist__has_entry(sym_list, sym->name))
		return 0;
680

681 682 683 684
	if (hist_entry__add(thread, map, sym, ip,
			    chain, level, period)) {
		eprintf("problem incrementing symbol count, skipping event\n");
		return -1;
685
	}
686

687
	total += period;
688

689 690
	return 0;
}
I
Ingo Molnar 已提交
691

692 693 694
static int
process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
{
695
	struct thread *thread;
696
	struct map *map = map__new(&event->mmap, cwd, cwdlen);
697

698 699
	thread = threads__findnew(event->mmap.pid, &threads, &last_match);

700
	dump_printf("%p [%p]: PERF_RECORD_MMAP %d/%d: [%p(%p) @ %p]: %s\n",
701 702
		(void *)(offset + head),
		(void *)(long)(event->header.size),
703
		event->mmap.pid,
704
		event->mmap.tid,
705 706 707 708 709 710
		(void *)(long)event->mmap.start,
		(void *)(long)event->mmap.len,
		(void *)(long)event->mmap.pgoff,
		event->mmap.filename);

	if (thread == NULL || map == NULL) {
711
		dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
712
		return 0;
713 714 715 716 717 718 719 720 721 722 723
	}

	thread__insert_map(thread, map);
	total_mmap++;

	return 0;
}

static int
process_comm_event(event_t *event, unsigned long offset, unsigned long head)
{
724 725 726
	struct thread *thread;

	thread = threads__findnew(event->comm.pid, &threads, &last_match);
727

728
	dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
729 730 731 732 733
		(void *)(offset + head),
		(void *)(long)(event->header.size),
		event->comm.comm, event->comm.pid);

	if (thread == NULL ||
734
	    thread__set_comm_adjust(thread, event->comm.comm)) {
735
		dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
736
		return -1;
737
	}
738 739 740 741 742
	total_comm++;

	return 0;
}

743
static int
744
process_task_event(event_t *event, unsigned long offset, unsigned long head)
745
{
746 747 748 749 750
	struct thread *thread;
	struct thread *parent;

	thread = threads__findnew(event->fork.pid, &threads, &last_match);
	parent = threads__findnew(event->fork.ppid, &threads, &last_match);
751

752
	dump_printf("%p [%p]: PERF_RECORD_%s: (%d:%d):(%d:%d)\n",
753 754
		(void *)(offset + head),
		(void *)(long)(event->header.size),
755
		event->header.type == PERF_RECORD_FORK ? "FORK" : "EXIT",
756 757 758 759 760 761 762 763 764 765
		event->fork.pid, event->fork.tid,
		event->fork.ppid, event->fork.ptid);

	/*
	 * A thread clone will have the same PID for both
	 * parent and child.
	 */
	if (thread == parent)
		return 0;

766
	if (event->header.type == PERF_RECORD_EXIT)
767
		return 0;
768 769

	if (!thread || !parent || thread__fork(thread, parent)) {
770
		dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
771 772 773 774 775 776 777
		return -1;
	}
	total_fork++;

	return 0;
}

778 779 780
static int
process_lost_event(event_t *event, unsigned long offset, unsigned long head)
{
781
	dump_printf("%p [%p]: PERF_RECORD_LOST: id:%Ld: lost:%Ld\n",
782 783 784 785 786 787 788 789 790 791
		(void *)(offset + head),
		(void *)(long)(event->header.size),
		event->lost.id,
		event->lost.lost);

	total_lost += event->lost.lost;

	return 0;
}

792 793 794
static int
process_read_event(event_t *event, unsigned long offset, unsigned long head)
{
795
	struct perf_event_attr *attr;
796 797

	attr = perf_header__find_attr(event->read.id, header);
798

799
	if (show_threads) {
800
		const char *name = attr ? __event_name(attr->type, attr->config)
801 802 803 804 805 806 807 808
				   : "unknown";
		perf_read_values_add_value(&show_threads_values,
					   event->read.pid, event->read.tid,
					   event->read.id,
					   name,
					   event->read.value);
	}

809
	dump_printf("%p [%p]: PERF_RECORD_READ: %d %d %s %Lu\n",
810 811 812 813
			(void *)(offset + head),
			(void *)(long)(event->header.size),
			event->read.pid,
			event->read.tid,
814 815
			attr ? __event_name(attr->type, attr->config)
			     : "FAIL",
816 817 818 819 820
			event->read.value);

	return 0;
}

821 822 823
static int
process_event(event_t *event, unsigned long offset, unsigned long head)
{
824 825
	trace_event(event);

826
	switch (event->header.type) {
827
	case PERF_RECORD_SAMPLE:
828 829
		return process_sample_event(event, offset, head);

830
	case PERF_RECORD_MMAP:
831 832
		return process_mmap_event(event, offset, head);

833
	case PERF_RECORD_COMM:
834 835
		return process_comm_event(event, offset, head);

836 837
	case PERF_RECORD_FORK:
	case PERF_RECORD_EXIT:
838
		return process_task_event(event, offset, head);
839

840
	case PERF_RECORD_LOST:
841 842
		return process_lost_event(event, offset, head);

843
	case PERF_RECORD_READ:
844 845
		return process_read_event(event, offset, head);

846 847 848
	/*
	 * We dont process them right now but they are fine:
	 */
849

850 851
	case PERF_RECORD_THROTTLE:
	case PERF_RECORD_UNTHROTTLE:
852 853
		return 0;

854 855 856 857 858 859 860 861 862
	default:
		return -1;
	}

	return 0;
}

static int __cmd_report(void)
{
863
	int ret, rc = EXIT_FAILURE;
864
	unsigned long offset = 0;
865
	unsigned long head, shift;
866
	struct stat input_stat;
867
	struct thread *idle;
868 869
	event_t *event;
	uint32_t size;
870
	char *buf;
871

872 873
	idle = register_idle_thread(&threads, &last_match);
	thread__comm_adjust(idle);
874

875 876 877
	if (show_threads)
		perf_read_values_init(&show_threads_values);

878 879
	input = open(input_name, O_RDONLY);
	if (input < 0) {
880 881 882 883
		fprintf(stderr, " failed to open file: %s", input_name);
		if (!strcmp(input_name, "perf.data"))
			fprintf(stderr, "  (try 'perf record' first)");
		fprintf(stderr, "\n");
884 885 886
		exit(-1);
	}

887
	ret = fstat(input, &input_stat);
888 889 890 891 892
	if (ret < 0) {
		perror("failed to stat file");
		exit(-1);
	}

893 894
	if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
		fprintf(stderr, "file: %s not owned by current user or root\n", input_name);
895 896 897
		exit(-1);
	}

898
	if (!input_stat.st_size) {
899 900 901 902
		fprintf(stderr, "zero-sized file, nothing to do!\n");
		exit(0);
	}

903 904
	header = perf_header__read(input);
	head = header->data_offset;
905

906
	sample_type = perf_header__sample_type(header);
907

908 909 910 911 912 913 914 915
	if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
		if (sort__has_parent) {
			fprintf(stderr, "selected --sort parent, but no"
					" callchain data. Did you call"
					" perf record without -g?\n");
			exit(-1);
		}
		if (callchain) {
916
			fprintf(stderr, "selected -g but no callchain data."
917 918 919 920
					" Did you call perf record without"
					" -g?\n");
			exit(-1);
		}
921 922 923 924 925 926 927
	} else if (callchain_param.mode != CHAIN_NONE && !callchain) {
			callchain = 1;
			if (register_callchain_param(&callchain_param) < 0) {
				fprintf(stderr, "Can't register callchain"
						" params\n");
				exit(-1);
			}
928 929
	}

930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
	if (load_kernel() < 0) {
		perror("failed to load kernel symbols");
		return EXIT_FAILURE;
	}

	if (!full_paths) {
		if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
			perror("failed to get the current directory");
			return EXIT_FAILURE;
		}
		cwdlen = strlen(cwd);
	} else {
		cwd = NULL;
		cwdlen = 0;
	}
945 946 947 948 949

	shift = page_size * (head / page_size);
	offset += shift;
	head -= shift;

950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965
remap:
	buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
			   MAP_SHARED, input, offset);
	if (buf == MAP_FAILED) {
		perror("failed to mmap file");
		exit(-1);
	}

more:
	event = (event_t *)(buf + head);

	size = event->header.size;
	if (!size)
		size = 8;

	if (head + event->header.size >= page_size * mmap_window) {
966
		int munmap_ret;
967

968 969
		shift = page_size * (head / page_size);

970 971
		munmap_ret = munmap(buf, page_size * mmap_window);
		assert(munmap_ret == 0);
972 973 974 975 976 977 978 979

		offset += shift;
		head -= shift;
		goto remap;
	}

	size = event->header.size;

980
	dump_printf("\n%p [%p]: event: %d\n",
981 982 983 984
			(void *)(offset + head),
			(void *)(long)event->header.size,
			event->header.type);

985 986
	if (!size || process_event(event, offset, head) < 0) {

987
		dump_printf("%p [%p]: skipping unknown header type: %d\n",
I
Ingo Molnar 已提交
988 989 990
			(void *)(offset + head),
			(void *)(long)(event->header.size),
			event->header.type);
991

992
		total_unknown++;
993 994 995 996 997 998 999 1000 1001 1002

		/*
		 * assume we lost track of the stream, check alignment, and
		 * increment a single u64 in the hope to catch on again 'soon'.
		 */

		if (unlikely(head & 7))
			head &= ~7ULL;

		size = 8;
1003
	}
1004

1005
	head += size;
I
Ingo Molnar 已提交
1006

1007
	if (offset + head >= header->data_offset + header->data_size)
1008 1009
		goto done;

1010
	if (offset + head < (unsigned long)input_stat.st_size)
1011 1012
		goto more;

1013
done:
1014 1015
	rc = EXIT_SUCCESS;
	close(input);
1016

1017 1018 1019 1020 1021 1022
	dump_printf("      IP events: %10ld\n", total);
	dump_printf("    mmap events: %10ld\n", total_mmap);
	dump_printf("    comm events: %10ld\n", total_comm);
	dump_printf("    fork events: %10ld\n", total_fork);
	dump_printf("    lost events: %10ld\n", total_lost);
	dump_printf(" unknown events: %10ld\n", total_unknown);
1023

I
Ingo Molnar 已提交
1024
	if (dump_trace)
1025 1026
		return 0;

1027
	if (verbose >= 3)
1028
		threads__fprintf(stdout, &threads);
1029

1030
	if (verbose >= 2)
1031 1032
		dsos__fprintf(stdout);

P
Peter Zijlstra 已提交
1033
	collapse__resort();
1034
	output__resort(total);
1035
	output__fprintf(stdout, total);
1036

1037 1038 1039
	if (show_threads)
		perf_read_values_destroy(&show_threads_values);

1040 1041 1042
	return rc;
}

1043 1044 1045 1046
static int
parse_callchain_opt(const struct option *opt __used, const char *arg,
		    int unset __used)
{
1047 1048 1049
	char *tok;
	char *endptr;

1050 1051 1052 1053 1054
	callchain = 1;

	if (!arg)
		return 0;

1055 1056 1057 1058 1059 1060
	tok = strtok((char *)arg, ",");
	if (!tok)
		return -1;

	/* get the output mode */
	if (!strncmp(tok, "graph", strlen(arg)))
1061
		callchain_param.mode = CHAIN_GRAPH_ABS;
1062

1063
	else if (!strncmp(tok, "flat", strlen(arg)))
1064 1065 1066 1067 1068
		callchain_param.mode = CHAIN_FLAT;

	else if (!strncmp(tok, "fractal", strlen(arg)))
		callchain_param.mode = CHAIN_GRAPH_REL;

1069 1070 1071 1072 1073 1074 1075
	else if (!strncmp(tok, "none", strlen(arg))) {
		callchain_param.mode = CHAIN_NONE;
		callchain = 0;

		return 0;
	}

1076 1077 1078
	else
		return -1;

1079 1080 1081
	/* get the min percentage */
	tok = strtok(NULL, ",");
	if (!tok)
1082
		goto setup;
1083

1084
	callchain_param.min_percent = strtod(tok, &endptr);
1085 1086 1087
	if (tok == endptr)
		return -1;

1088 1089 1090 1091 1092
setup:
	if (register_callchain_param(&callchain_param) < 0) {
		fprintf(stderr, "Can't register callchain params\n");
		return -1;
	}
1093 1094 1095
	return 0;
}

1096 1097
//static const char * const report_usage[] = {
const char * const report_usage[] = {
1098 1099 1100 1101 1102 1103 1104
	"perf report [<options>] <command>",
	NULL
};

static const struct option options[] = {
	OPT_STRING('i', "input", &input_name, "file",
		    "input file name"),
1105 1106
	OPT_BOOLEAN('v', "verbose", &verbose,
		    "be more verbose (show symbol address, etc)"),
1107 1108
	OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
		    "dump raw trace in ASCII"),
1109
	OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
1110
	OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
1111 1112
	OPT_BOOLEAN('m', "modules", &modules,
		    "load module symbols - WARNING: use only with -k and LIVE kernel"),
1113 1114
	OPT_BOOLEAN('n', "show-nr-samples", &show_nr_samples,
		    "Show a column with the number of samples"),
1115 1116
	OPT_BOOLEAN('T', "threads", &show_threads,
		    "Show per-thread event counters"),
1117 1118
	OPT_STRING(0, "pretty", &pretty_printing_style, "key",
		   "pretty printing style key: normal raw"),
1119
	OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1120
		   "sort by key(s): pid, comm, dso, symbol, parent"),
1121 1122
	OPT_BOOLEAN('P', "full-paths", &full_paths,
		    "Don't shorten the pathnames taking into account the cwd"),
1123 1124
	OPT_STRING('p', "parent", &parent_pattern, "regex",
		   "regex filter to identify parent, see: '--sort parent'"),
1125 1126
	OPT_BOOLEAN('x', "exclude-other", &exclude_other,
		    "Only display entries with parent-match"),
1127
	OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
1128
		     "Display callchains using output_type and min percent threshold. "
1129
		     "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
1130 1131
	OPT_STRING('d', "dsos", &dso_list_str, "dso[,dso...]",
		   "only consider symbols in these dsos"),
1132 1133
	OPT_STRING('C', "comms", &comm_list_str, "comm[,comm...]",
		   "only consider symbols in these comms"),
1134 1135
	OPT_STRING('S', "symbols", &sym_list_str, "symbol[,symbol...]",
		   "only consider these symbols"),
1136 1137 1138 1139 1140 1141
	OPT_STRING('w', "column-widths", &col_width_list_str,
		   "width[,width...]",
		   "don't try to adjust column width, use these fixed values"),
	OPT_STRING('t', "field-separator", &field_sep, "separator",
		   "separator for columns, no spaces will be added between "
		   "columns '.' is reserved."),
1142 1143 1144
	OPT_END()
};

1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
static void setup_sorting(void)
{
	char *tmp, *tok, *str = strdup(sort_order);

	for (tok = strtok_r(str, ", ", &tmp);
			tok; tok = strtok_r(NULL, ", ", &tmp)) {
		if (sort_dimension__add(tok) < 0) {
			error("Unknown --sort key: `%s'", tok);
			usage_with_options(report_usage, options);
		}
	}

	free(str);
}

1160
static void setup_list(struct strlist **list, const char *list_str,
1161 1162
		       struct sort_entry *se, const char *list_name,
		       FILE *fp)
1163 1164 1165 1166 1167 1168 1169 1170
{
	if (list_str) {
		*list = strlist__new(true, list_str);
		if (!*list) {
			fprintf(stderr, "problems parsing %s list\n",
				list_name);
			exit(129);
		}
1171 1172 1173 1174 1175
		if (strlist__nr_entries(*list) == 1) {
			fprintf(fp, "# %s: %s\n", list_name,
				strlist__entry(*list, 0)->s);
			se->elide = true;
		}
1176 1177 1178
	}
}

1179
int cmd_report(int argc, const char **argv, const char *prefix __used)
1180
{
1181
	symbol__init();
1182 1183 1184

	page_size = getpagesize();

1185
	argc = parse_options(argc, argv, options, report_usage, 0);
1186

1187 1188
	setup_sorting();

1189
	if (parent_pattern != default_parent_pattern) {
1190
		sort_dimension__add("parent");
1191 1192
		sort_parent.elide = 1;
	} else
1193 1194
		exclude_other = 0;

1195 1196 1197 1198 1199 1200
	/*
	 * Any (unrecognized) arguments left?
	 */
	if (argc)
		usage_with_options(report_usage, options);

1201 1202
	setup_pager();

1203 1204 1205
	setup_list(&dso_list, dso_list_str, &sort_dso, "dso", stdout);
	setup_list(&comm_list, comm_list_str, &sort_comm, "comm", stdout);
	setup_list(&sym_list, sym_list_str, &sort_sym, "symbol", stdout);
1206

1207 1208 1209 1210 1211 1212
	if (field_sep && *field_sep == '.') {
		fputs("'.' is the only non valid --field-separator argument\n",
		      stderr);
		exit(129);
	}

1213 1214
	return __cmd_report();
}