builtin-report.c 28.2 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 42
static int		input;
static int		show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;

43
static int		full_paths;
44
static int		show_nr_samples;
45

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

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

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

55
static int		exclude_other = 1;
56

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

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

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

66 67
static struct perf_header *header;

68 69
static u64		sample_type;

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
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;
}
87
static size_t
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
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;
105
			ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
106 107 108 109 110 111 112 113 114 115 116
		} 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;
}

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
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;
}

132 133 134 135 136 137 138 139
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;
140
	u64 new_total;
141
	u64 remaining;
142 143 144
	size_t ret = 0;
	int i;

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

150 151
	remaining = new_total;

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

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

		/*
		 * The depth mask manages the output of pipes that show
		 * the depth. We don't want to keep the pipes of the current
163 164 165
		 * level for the last child of this depth.
		 * Except if we have remaining filtered hits. They will
		 * supersede the last child
166 167
		 */
		next = rb_next(node);
168
		if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
169 170 171 172 173 174 175 176 177 178 179 180 181
			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++,
182
						      new_total,
183
						      cumul);
184
		}
185
		ret += callchain__fprintf_graph(fp, child, new_total,
186 187 188 189 190
						depth + 1,
						new_depth_mask | (1 << depth));
		node = next;
	}

191 192 193 194 195 196 197 198 199 200 201 202 203
	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);
	}

204 205 206 207 208 209
	return ret;
}

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

	if (!self)
		return 0;

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


220 221 222 223 224 225 226
	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",
227
					(void *)(long)chain->ip);
228
	}
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246

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

	return ret;
}

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

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

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

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

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

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

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

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

304 305 306
	return ret;
}

307 308 309 310
/*
 *
 */

311 312 313 314 315 316 317 318 319 320 321 322
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;
}

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

	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;
		}
	}
336 337 338 339 340 341 342 343 344 345
}

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);
346 347 348 349 350

	return 0;
}


351 352
static struct symbol *
resolve_symbol(struct thread *thread, struct map **mapp,
353
	       struct dso **dsop, u64 *ipp)
354 355 356
{
	struct dso *dso = dsop ? *dsop : NULL;
	struct map *map = mapp ? *mapp : NULL;
357
	u64 ip = *ipp;
358 359 360 361 362 363 364 365 366 367 368 369

	if (!thread)
		return NULL;

	if (dso)
		goto got_dso;

	if (map)
		goto got_map;

	map = thread__find_map(thread, ip);
	if (map != NULL) {
370 371 372 373 374
		/*
		 * 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.
		 */
375
		if (!sort_dso.elide && !map->dso->slen_calculated)
376 377
			dso__calc_col_width(map->dso);

378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
		if (mapp)
			*mapp = map;
got_map:
		ip = map->map_ip(map, ip);

		dso = map->dso;
	} 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
		 * vsyscall (which executes in user-mode):
		 */
		if ((long long)ip < 0)
		dso = kernel_dso;
	}
394 395
	dump_printf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
	dump_printf(" ...... map: %Lx -> %Lx\n", *ipp, ip);
396
	*ipp  = ip;
397 398 399 400 401 402 403 404 405 406

	if (dsop)
		*dsop = dso;

	if (!dso)
		return NULL;
got_dso:
	return dso->find_symbol(dso, ip);
}

407
static int call__match(struct symbol *sym)
408
{
409
	if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
410
		return 1;
411

412
	return 0;
413 414
}

415
static struct symbol **
416
resolve_callchain(struct thread *thread, struct map *map __used,
417 418 419
		    struct ip_callchain *chain, struct hist_entry *entry)
{
	u64 context = PERF_CONTEXT_MAX;
420
	struct symbol **syms = NULL;
421
	unsigned int i;
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441

	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];
		struct dso *dso = NULL;
		struct symbol *sym;

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

		switch (context) {
I
Ingo Molnar 已提交
442 443 444
		case PERF_CONTEXT_HV:
			dso = hypervisor_dso;
			break;
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
		case PERF_CONTEXT_KERNEL:
			dso = kernel_dso;
			break;
		default:
			break;
		}

		sym = resolve_symbol(thread, NULL, &dso, &ip);

		if (sym) {
			if (sort__has_parent && call__match(sym) &&
			    !entry->parent)
				entry->parent = sym;
			if (!callchain)
				break;
			syms[i] = sym;
		}
	}

	return syms;
}

467 468 469 470
/*
 * collect histogram counts
 */

471 472
static int
hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
473 474
		struct symbol *sym, u64 ip, struct ip_callchain *chain,
		char level, u64 count)
475
{
476 477 478
	struct rb_node **p = &hist.rb_node;
	struct rb_node *parent = NULL;
	struct hist_entry *he;
479
	struct symbol **syms = NULL;
480 481 482 483 484 485 486
	struct hist_entry entry = {
		.thread	= thread,
		.map	= map,
		.dso	= dso,
		.sym	= sym,
		.ip	= ip,
		.level	= level,
487
		.count	= count,
488
		.parent = NULL,
489
		.sorted_chain = RB_ROOT
490 491 492
	};
	int cmp;

493 494
	if ((sort__has_parent || callchain) && chain)
		syms = resolve_callchain(thread, map, chain, &entry);
495

496 497 498 499 500 501 502
	while (*p != NULL) {
		parent = *p;
		he = rb_entry(parent, struct hist_entry, rb_node);

		cmp = hist_entry__cmp(&entry, he);

		if (!cmp) {
503
			he->count += count;
504 505 506 507
			if (callchain) {
				append_chain(&he->callchain, chain, syms);
				free(syms);
			}
508 509 510 511 512 513 514
			return 0;
		}

		if (cmp < 0)
			p = &(*p)->rb_left;
		else
			p = &(*p)->rb_right;
515
	}
516 517 518 519 520

	he = malloc(sizeof(*he));
	if (!he)
		return -ENOMEM;
	*he = entry;
521 522
	if (callchain) {
		callchain_init(&he->callchain);
523 524
		append_chain(&he->callchain, chain, syms);
		free(syms);
525
	}
526 527 528 529
	rb_link_node(&he->rb_node, parent, p);
	rb_insert_color(&he->rb_node, &hist);

	return 0;
530 531
}

532
static size_t output__fprintf(FILE *fp, u64 total_samples)
533
{
534
	struct hist_entry *pos;
535
	struct sort_entry *se;
536 537
	struct rb_node *nd;
	size_t ret = 0;
538 539
	unsigned int width;
	char *col_width = col_width_list_str;
540 541 542
	int raw_printing_style;

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

544 545
	init_rem_hits();

546
	fprintf(fp, "# Samples: %Ld\n", (u64)total_samples);
547 548 549
	fprintf(fp, "#\n");

	fprintf(fp, "# Overhead");
550 551 552 553 554 555
	if (show_nr_samples) {
		if (field_sep)
			fprintf(fp, "%cSamples", *field_sep);
		else
			fputs("  Samples  ", fp);
	}
556
	list_for_each_entry(se, &hist_entry__sort_list, list) {
557
		if (se->elide)
558
			continue;
559 560
		if (field_sep) {
			fprintf(fp, "%c%s", *field_sep, se->header);
561
			continue;
562 563 564 565 566 567 568 569 570 571 572 573 574 575
		}
		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);
576
	}
577 578
	fprintf(fp, "\n");

579 580 581
	if (field_sep)
		goto print_entries;

582
	fprintf(fp, "# ........");
583 584
	if (show_nr_samples)
		fprintf(fp, " ..........");
585
	list_for_each_entry(se, &hist_entry__sort_list, list) {
586
		unsigned int i;
587

588
		if (se->elide)
589 590
			continue;

591
		fprintf(fp, "  ");
592 593 594 595 596
		if (se->width)
			width = *se->width;
		else
			width = strlen(se->header);
		for (i = 0; i < width; i++)
597
			fprintf(fp, ".");
598
	}
599 600 601
	fprintf(fp, "\n");

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

603
print_entries:
604 605 606
	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);
607 608
	}

609 610
	if (sort_order == default_sort_order &&
			parent_pattern == default_parent_pattern) {
611
		fprintf(fp, "#\n");
612
		fprintf(fp, "# (For a higher level overview, try: perf report --sort comm,dso)\n");
613 614
		fprintf(fp, "#\n");
	}
615
	fprintf(fp, "\n");
616

617 618
	free(rem_sq_bracket);

619
	if (show_threads)
620 621
		perf_read_values_display(fp, &show_threads_values,
					 raw_printing_style);
622

623 624 625
	return ret;
}

626
static int validate_chain(struct ip_callchain *chain, event_t *event)
627 628 629 630 631 632
{
	unsigned int chain_size;

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

633
	if (chain->nr*sizeof(u64) > chain_size)
634 635 636 637 638
		return -1;

	return 0;
}

639
static int
640
process_sample_event(event_t *event, unsigned long offset, unsigned long head)
641 642 643 644
{
	char level;
	int show = 0;
	struct dso *dso = NULL;
645
	struct thread *thread;
646 647
	u64 ip = event->ip.ip;
	u64 period = 1;
648
	struct map *map = NULL;
649
	void *more_data = event->ip.__more_data;
650
	struct ip_callchain *chain = NULL;
651
	int cpumode;
652

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

655
	if (sample_type & PERF_SAMPLE_PERIOD) {
656 657
		period = *(u64 *)more_data;
		more_data += sizeof(u64);
658
	}
659

660
	dump_printf("%p [%p]: PERF_RECORD_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
661 662 663
		(void *)(offset + head),
		(void *)(long)(event->header.size),
		event->header.misc,
664
		event->ip.pid, event->ip.tid,
665
		(void *)(long)ip,
666
		(long long)period);
667

668
	if (sample_type & PERF_SAMPLE_CALLCHAIN) {
669
		unsigned int i;
670 671 672

		chain = (void *)more_data;

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

675 676 677 678 679 680
		if (validate_chain(chain, event) < 0) {
			eprintf("call-chain problem with event, skipping it.\n");
			return 0;
		}

		if (dump_trace) {
681
			for (i = 0; i < chain->nr; i++)
682
				dump_printf("..... %2d: %016Lx\n", i, chain->ips[i]);
683 684 685
		}
	}

686
	dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
687 688

	if (thread == NULL) {
689
		eprintf("problem processing %d event, skipping it.\n",
690 691 692
			event->header.type);
		return -1;
	}
693

694 695 696
	if (comm_list && !strlist__has_entry(comm_list, thread->comm))
		return 0;

697
	cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
698

699
	if (cpumode == PERF_RECORD_MISC_KERNEL) {
700 701
		show = SHOW_KERNEL;
		level = 'k';
702

703
		dso = kernel_dso;
704

705
		dump_printf(" ...... dso: %s\n", dso->name);
706

707
	} else if (cpumode == PERF_RECORD_MISC_USER) {
708

709 710
		show = SHOW_USER;
		level = '.';
711

712 713 714
	} else {
		show = SHOW_HV;
		level = 'H';
715 716 717

		dso = hypervisor_dso;

718
		dump_printf(" ...... dso: [hypervisor]\n");
719
	}
720

721
	if (show & show_mask) {
722
		struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
723

724 725
		if (dso_list && (!dso || !dso->name ||
				 !strlist__has_entry(dso_list, dso->name)))
726 727
			return 0;

728
		if (sym_list && (!sym || !strlist__has_entry(sym_list, sym->name)))
729 730
			return 0;

731
		if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
732
			eprintf("problem incrementing symbol count, skipping event\n");
733
			return -1;
734
		}
735
	}
736
	total += period;
737

738 739
	return 0;
}
I
Ingo Molnar 已提交
740

741 742 743
static int
process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
{
744
	struct thread *thread;
745
	struct map *map = map__new(&event->mmap, cwd, cwdlen);
746

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

749
	dump_printf("%p [%p]: PERF_RECORD_MMAP %d/%d: [%p(%p) @ %p]: %s\n",
750 751
		(void *)(offset + head),
		(void *)(long)(event->header.size),
752
		event->mmap.pid,
753
		event->mmap.tid,
754 755 756 757 758 759
		(void *)(long)event->mmap.start,
		(void *)(long)event->mmap.len,
		(void *)(long)event->mmap.pgoff,
		event->mmap.filename);

	if (thread == NULL || map == NULL) {
760
		dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
761
		return 0;
762 763 764 765 766 767 768 769 770 771 772
	}

	thread__insert_map(thread, map);
	total_mmap++;

	return 0;
}

static int
process_comm_event(event_t *event, unsigned long offset, unsigned long head)
{
773 774 775
	struct thread *thread;

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

777
	dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
778 779 780 781 782
		(void *)(offset + head),
		(void *)(long)(event->header.size),
		event->comm.comm, event->comm.pid);

	if (thread == NULL ||
783
	    thread__set_comm_adjust(thread, event->comm.comm)) {
784
		dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
785
		return -1;
786
	}
787 788 789 790 791
	total_comm++;

	return 0;
}

792
static int
793
process_task_event(event_t *event, unsigned long offset, unsigned long head)
794
{
795 796 797 798 799
	struct thread *thread;
	struct thread *parent;

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

801
	dump_printf("%p [%p]: PERF_RECORD_%s: (%d:%d):(%d:%d)\n",
802 803
		(void *)(offset + head),
		(void *)(long)(event->header.size),
804
		event->header.type == PERF_RECORD_FORK ? "FORK" : "EXIT",
805 806 807 808 809 810 811 812 813 814
		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;

815
	if (event->header.type == PERF_RECORD_EXIT)
816
		return 0;
817 818

	if (!thread || !parent || thread__fork(thread, parent)) {
819
		dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
820 821 822 823 824 825 826
		return -1;
	}
	total_fork++;

	return 0;
}

827 828 829
static int
process_lost_event(event_t *event, unsigned long offset, unsigned long head)
{
830
	dump_printf("%p [%p]: PERF_RECORD_LOST: id:%Ld: lost:%Ld\n",
831 832 833 834 835 836 837 838 839 840
		(void *)(offset + head),
		(void *)(long)(event->header.size),
		event->lost.id,
		event->lost.lost);

	total_lost += event->lost.lost;

	return 0;
}

841 842 843
static int
process_read_event(event_t *event, unsigned long offset, unsigned long head)
{
844
	struct perf_event_attr *attr;
845 846

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

848
	if (show_threads) {
849
		const char *name = attr ? __event_name(attr->type, attr->config)
850 851 852 853 854 855 856 857
				   : "unknown";
		perf_read_values_add_value(&show_threads_values,
					   event->read.pid, event->read.tid,
					   event->read.id,
					   name,
					   event->read.value);
	}

858
	dump_printf("%p [%p]: PERF_RECORD_READ: %d %d %s %Lu\n",
859 860 861 862
			(void *)(offset + head),
			(void *)(long)(event->header.size),
			event->read.pid,
			event->read.tid,
863 864
			attr ? __event_name(attr->type, attr->config)
			     : "FAIL",
865 866 867 868 869
			event->read.value);

	return 0;
}

870 871 872
static int
process_event(event_t *event, unsigned long offset, unsigned long head)
{
873 874
	trace_event(event);

875
	switch (event->header.type) {
876
	case PERF_RECORD_SAMPLE:
877 878
		return process_sample_event(event, offset, head);

879
	case PERF_RECORD_MMAP:
880 881
		return process_mmap_event(event, offset, head);

882
	case PERF_RECORD_COMM:
883 884
		return process_comm_event(event, offset, head);

885 886
	case PERF_RECORD_FORK:
	case PERF_RECORD_EXIT:
887
		return process_task_event(event, offset, head);
888

889
	case PERF_RECORD_LOST:
890 891
		return process_lost_event(event, offset, head);

892
	case PERF_RECORD_READ:
893 894
		return process_read_event(event, offset, head);

895 896 897
	/*
	 * We dont process them right now but they are fine:
	 */
898

899 900
	case PERF_RECORD_THROTTLE:
	case PERF_RECORD_UNTHROTTLE:
901 902
		return 0;

903 904 905 906 907 908 909 910 911
	default:
		return -1;
	}

	return 0;
}

static int __cmd_report(void)
{
912
	int ret, rc = EXIT_FAILURE;
913
	unsigned long offset = 0;
914
	unsigned long head, shift;
915
	struct stat input_stat;
916
	struct thread *idle;
917 918
	event_t *event;
	uint32_t size;
919
	char *buf;
920

921 922
	idle = register_idle_thread(&threads, &last_match);
	thread__comm_adjust(idle);
923

924 925 926
	if (show_threads)
		perf_read_values_init(&show_threads_values);

927 928
	input = open(input_name, O_RDONLY);
	if (input < 0) {
929 930 931 932
		fprintf(stderr, " failed to open file: %s", input_name);
		if (!strcmp(input_name, "perf.data"))
			fprintf(stderr, "  (try 'perf record' first)");
		fprintf(stderr, "\n");
933 934 935
		exit(-1);
	}

936
	ret = fstat(input, &input_stat);
937 938 939 940 941
	if (ret < 0) {
		perror("failed to stat file");
		exit(-1);
	}

942 943
	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);
944 945 946
		exit(-1);
	}

947
	if (!input_stat.st_size) {
948 949 950 951
		fprintf(stderr, "zero-sized file, nothing to do!\n");
		exit(0);
	}

952 953
	header = perf_header__read(input);
	head = header->data_offset;
954

955
	sample_type = perf_header__sample_type(header);
956

957 958 959 960 961 962 963 964
	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) {
965
			fprintf(stderr, "selected -g but no callchain data."
966 967 968 969
					" Did you call perf record without"
					" -g?\n");
			exit(-1);
		}
970 971 972 973 974 975 976
	} 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);
			}
977 978
	}

979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
	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;
	}
994 995 996 997 998

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

999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
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) {
1015
		int munmap_ret;
1016

1017 1018
		shift = page_size * (head / page_size);

1019 1020
		munmap_ret = munmap(buf, page_size * mmap_window);
		assert(munmap_ret == 0);
1021 1022 1023 1024 1025 1026 1027 1028

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

	size = event->header.size;

1029
	dump_printf("\n%p [%p]: event: %d\n",
1030 1031 1032 1033
			(void *)(offset + head),
			(void *)(long)event->header.size,
			event->header.type);

1034 1035
	if (!size || process_event(event, offset, head) < 0) {

1036
		dump_printf("%p [%p]: skipping unknown header type: %d\n",
I
Ingo Molnar 已提交
1037 1038 1039
			(void *)(offset + head),
			(void *)(long)(event->header.size),
			event->header.type);
1040

1041
		total_unknown++;
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051

		/*
		 * 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;
1052
	}
1053

1054
	head += size;
I
Ingo Molnar 已提交
1055

1056
	if (offset + head >= header->data_offset + header->data_size)
1057 1058
		goto done;

1059
	if (offset + head < (unsigned long)input_stat.st_size)
1060 1061
		goto more;

1062
done:
1063 1064
	rc = EXIT_SUCCESS;
	close(input);
1065

1066 1067 1068 1069 1070 1071
	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);
1072

I
Ingo Molnar 已提交
1073
	if (dump_trace)
1074 1075
		return 0;

1076
	if (verbose >= 3)
1077
		threads__fprintf(stdout, &threads);
1078

1079
	if (verbose >= 2)
1080 1081
		dsos__fprintf(stdout);

P
Peter Zijlstra 已提交
1082
	collapse__resort();
1083
	output__resort(total);
1084
	output__fprintf(stdout, total);
1085

1086 1087 1088
	if (show_threads)
		perf_read_values_destroy(&show_threads_values);

1089 1090 1091
	return rc;
}

1092 1093 1094 1095
static int
parse_callchain_opt(const struct option *opt __used, const char *arg,
		    int unset __used)
{
1096 1097 1098
	char *tok;
	char *endptr;

1099 1100 1101 1102 1103
	callchain = 1;

	if (!arg)
		return 0;

1104 1105 1106 1107 1108 1109
	tok = strtok((char *)arg, ",");
	if (!tok)
		return -1;

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

1112
	else if (!strncmp(tok, "flat", strlen(arg)))
1113 1114 1115 1116 1117
		callchain_param.mode = CHAIN_FLAT;

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

1118 1119 1120 1121 1122 1123 1124
	else if (!strncmp(tok, "none", strlen(arg))) {
		callchain_param.mode = CHAIN_NONE;
		callchain = 0;

		return 0;
	}

1125 1126 1127
	else
		return -1;

1128 1129 1130
	/* get the min percentage */
	tok = strtok(NULL, ",");
	if (!tok)
1131
		goto setup;
1132

1133
	callchain_param.min_percent = strtod(tok, &endptr);
1134 1135 1136
	if (tok == endptr)
		return -1;

1137 1138 1139 1140 1141
setup:
	if (register_callchain_param(&callchain_param) < 0) {
		fprintf(stderr, "Can't register callchain params\n");
		return -1;
	}
1142 1143 1144
	return 0;
}

1145 1146
//static const char * const report_usage[] = {
const char * const report_usage[] = {
1147 1148 1149 1150 1151 1152 1153
	"perf report [<options>] <command>",
	NULL
};

static const struct option options[] = {
	OPT_STRING('i', "input", &input_name, "file",
		    "input file name"),
1154 1155
	OPT_BOOLEAN('v', "verbose", &verbose,
		    "be more verbose (show symbol address, etc)"),
1156 1157
	OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
		    "dump raw trace in ASCII"),
1158
	OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
1159
	OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
1160 1161
	OPT_BOOLEAN('m', "modules", &modules,
		    "load module symbols - WARNING: use only with -k and LIVE kernel"),
1162 1163
	OPT_BOOLEAN('n', "show-nr-samples", &show_nr_samples,
		    "Show a column with the number of samples"),
1164 1165
	OPT_BOOLEAN('T', "threads", &show_threads,
		    "Show per-thread event counters"),
1166 1167
	OPT_STRING(0, "pretty", &pretty_printing_style, "key",
		   "pretty printing style key: normal raw"),
1168
	OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1169
		   "sort by key(s): pid, comm, dso, symbol, parent"),
1170 1171
	OPT_BOOLEAN('P', "full-paths", &full_paths,
		    "Don't shorten the pathnames taking into account the cwd"),
1172 1173
	OPT_STRING('p', "parent", &parent_pattern, "regex",
		   "regex filter to identify parent, see: '--sort parent'"),
1174 1175
	OPT_BOOLEAN('x', "exclude-other", &exclude_other,
		    "Only display entries with parent-match"),
1176
	OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
1177
		     "Display callchains using output_type and min percent threshold. "
1178
		     "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
1179 1180
	OPT_STRING('d', "dsos", &dso_list_str, "dso[,dso...]",
		   "only consider symbols in these dsos"),
1181 1182
	OPT_STRING('C', "comms", &comm_list_str, "comm[,comm...]",
		   "only consider symbols in these comms"),
1183 1184
	OPT_STRING('S', "symbols", &sym_list_str, "symbol[,symbol...]",
		   "only consider these symbols"),
1185 1186 1187 1188 1189 1190
	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."),
1191 1192 1193
	OPT_END()
};

1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
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);
}

1209
static void setup_list(struct strlist **list, const char *list_str,
1210 1211
		       struct sort_entry *se, const char *list_name,
		       FILE *fp)
1212 1213 1214 1215 1216 1217 1218 1219
{
	if (list_str) {
		*list = strlist__new(true, list_str);
		if (!*list) {
			fprintf(stderr, "problems parsing %s list\n",
				list_name);
			exit(129);
		}
1220 1221 1222 1223 1224
		if (strlist__nr_entries(*list) == 1) {
			fprintf(fp, "# %s: %s\n", list_name,
				strlist__entry(*list, 0)->s);
			se->elide = true;
		}
1225 1226 1227
	}
}

1228
int cmd_report(int argc, const char **argv, const char *prefix __used)
1229
{
1230
	symbol__init();
1231 1232 1233

	page_size = getpagesize();

1234
	argc = parse_options(argc, argv, options, report_usage, 0);
1235

1236 1237
	setup_sorting();

1238
	if (parent_pattern != default_parent_pattern) {
1239
		sort_dimension__add("parent");
1240 1241
		sort_parent.elide = 1;
	} else
1242 1243
		exclude_other = 0;

1244 1245 1246 1247 1248 1249
	/*
	 * Any (unrecognized) arguments left?
	 */
	if (argc)
		usage_with_options(report_usage, options);

1250 1251
	setup_pager();

1252 1253 1254
	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);
1255

1256 1257 1258 1259 1260 1261
	if (field_sep && *field_sep == '.') {
		fputs("'.' is the only non valid --field-separator argument\n",
		      stderr);
		exit(129);
	}

1262 1263
	return __cmd_report();
}