builtin-report.c 37.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 30
#include "util/thread.h"

31
static char		const *input_name = "perf.data";
32

33
static char		default_sort_order[] = "comm,dso,symbol";
34
static char		*sort_order = default_sort_order;
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
static char		*field_sep;
39

40
static int		force;
41 42 43
static int		input;
static int		show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;

44
static int		full_paths;
45
static int		show_nr_samples;
46

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

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

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

56 57
static char		default_parent_pattern[] = "^sys_|^do_page_fault";
static char		*parent_pattern = default_parent_pattern;
58
static regex_t		parent_regex;
59

60
static int		exclude_other = 1;
61

62 63
static char		callchain_default_opt[] = "fractal,0.5";

64
static int		callchain;
65

66 67 68 69
static char		__cwd[PATH_MAX];
static char		*cwd = __cwd;
static int		cwdlen;

70 71 72
static struct rb_root	threads;
static struct thread	*last_match;

73 74
static struct perf_header *header;

75 76
static
struct callchain_param	callchain_param = {
77
	.mode	= CHAIN_GRAPH_REL,
78 79
	.min_percent = 0.5
};
80

81 82
static u64		sample_type;

83 84 85 86 87 88 89 90 91 92 93 94 95
static int repsep_fprintf(FILE *fp, const char *fmt, ...)
{
	int n;
	va_list ap;

	va_start(ap, fmt);
	if (!field_sep)
		n = vfprintf(fp, fmt, ap);
	else {
		char *bf = NULL;
		n = vasprintf(&bf, fmt, ap);
		if (n > 0) {
			char *sep = bf;
96

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
			while (1) {
				sep = strchr(sep, *field_sep);
				if (sep == NULL)
					break;
				*sep = '.';
			}
		}
		fputs(bf, fp);
		free(bf);
	}
	va_end(ap);
	return n;
}

static unsigned int dsos__col_width,
		    comms__col_width,
		    threads__col_width;

115 116 117 118 119 120 121
/*
 * histogram, sorted on item, collects counts
 */

static struct rb_root hist;

struct hist_entry {
122 123 124 125 126 127 128 129 130 131 132 133 134
	struct rb_node		rb_node;

	struct thread		*thread;
	struct map		*map;
	struct dso		*dso;
	struct symbol		*sym;
	struct symbol		*parent;
	u64			ip;
	char			level;
	struct callchain_node	callchain;
	struct rb_root		sorted_chain;

	u64			count;
135 136
};

137 138 139 140 141 142 143
/*
 * configurable sorting bits
 */

struct sort_entry {
	struct list_head list;

144
	const char *header;
145

146
	int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
P
Peter Zijlstra 已提交
147
	int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
148 149
	size_t	(*print)(FILE *fp, struct hist_entry *, unsigned int width);
	unsigned int *width;
150
	bool	elide;
151 152
};

153 154 155 156 157 158 159 160 161 162
static int64_t cmp_null(void *l, void *r)
{
	if (!l && !r)
		return 0;
	else if (!l)
		return -1;
	else
		return 1;
}

P
Peter Zijlstra 已提交
163 164
/* --sort pid */

165
static int64_t
166
sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
167
{
168 169 170 171
	return right->thread->pid - left->thread->pid;
}

static size_t
172
sort__thread_print(FILE *fp, struct hist_entry *self, unsigned int width)
173
{
174 175
	return repsep_fprintf(fp, "%*s:%5d", width - 6,
			      self->thread->comm ?: "", self->thread->pid);
176
}
177

178
static struct sort_entry sort_thread = {
179
	.header = "Command:  Pid",
180 181
	.cmp	= sort__thread_cmp,
	.print	= sort__thread_print,
182
	.width	= &threads__col_width,
183 184
};

P
Peter Zijlstra 已提交
185 186
/* --sort comm */

187 188
static int64_t
sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
P
Peter Zijlstra 已提交
189 190 191 192 193 194
{
	return right->thread->pid - left->thread->pid;
}

static int64_t
sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
195 196 197 198
{
	char *comm_l = left->thread->comm;
	char *comm_r = right->thread->comm;

199 200
	if (!comm_l || !comm_r)
		return cmp_null(comm_l, comm_r);
201 202 203 204 205

	return strcmp(comm_l, comm_r);
}

static size_t
206
sort__comm_print(FILE *fp, struct hist_entry *self, unsigned int width)
207
{
208
	return repsep_fprintf(fp, "%*s", width, self->thread->comm);
209 210 211
}

static struct sort_entry sort_comm = {
212
	.header		= "Command",
P
Peter Zijlstra 已提交
213 214 215
	.cmp		= sort__comm_cmp,
	.collapse	= sort__comm_collapse,
	.print		= sort__comm_print,
216
	.width		= &comms__col_width,
217 218
};

P
Peter Zijlstra 已提交
219 220
/* --sort dso */

221 222 223 224 225 226
static int64_t
sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
{
	struct dso *dso_l = left->dso;
	struct dso *dso_r = right->dso;

227 228
	if (!dso_l || !dso_r)
		return cmp_null(dso_l, dso_r);
229 230 231 232 233

	return strcmp(dso_l->name, dso_r->name);
}

static size_t
234
sort__dso_print(FILE *fp, struct hist_entry *self, unsigned int width)
235
{
236
	if (self->dso)
237
		return repsep_fprintf(fp, "%-*s", width, self->dso->name);
238

239
	return repsep_fprintf(fp, "%*llx", width, (u64)self->ip);
240 241 242
}

static struct sort_entry sort_dso = {
243
	.header = "Shared Object",
244 245
	.cmp	= sort__dso_cmp,
	.print	= sort__dso_print,
246
	.width	= &dsos__col_width,
247 248
};

P
Peter Zijlstra 已提交
249 250
/* --sort symbol */

251 252 253
static int64_t
sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
{
254
	u64 ip_l, ip_r;
255 256 257 258 259 260 261 262 263 264

	if (left->sym == right->sym)
		return 0;

	ip_l = left->sym ? left->sym->start : left->ip;
	ip_r = right->sym ? right->sym->start : right->ip;

	return (int64_t)(ip_r - ip_l);
}

265
static size_t
266
sort__sym_print(FILE *fp, struct hist_entry *self, unsigned int width __used)
267 268 269 270
{
	size_t ret = 0;

	if (verbose)
271 272
		ret += repsep_fprintf(fp, "%#018llx %c ", (u64)self->ip,
				      dso__symtab_origin(self->dso));
273

274
	ret += repsep_fprintf(fp, "[%c] ", self->level);
275
	if (self->sym) {
276
		ret += repsep_fprintf(fp, "%s", self->sym->name);
277 278

		if (self->sym->module)
279 280
			ret += repsep_fprintf(fp, "\t[%s]",
					     self->sym->module->name);
281
	} else {
282
		ret += repsep_fprintf(fp, "%#016llx", (u64)self->ip);
283
	}
284 285 286 287 288

	return ret;
}

static struct sort_entry sort_sym = {
289
	.header = "Symbol",
290 291
	.cmp	= sort__sym_cmp,
	.print	= sort__sym_print,
292 293
};

294
/* --sort parent */
295 296

static int64_t
297
sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
298
{
299 300
	struct symbol *sym_l = left->parent;
	struct symbol *sym_r = right->parent;
301 302 303 304 305 306 307 308

	if (!sym_l || !sym_r)
		return cmp_null(sym_l, sym_r);

	return strcmp(sym_l->name, sym_r->name);
}

static size_t
309
sort__parent_print(FILE *fp, struct hist_entry *self, unsigned int width)
310
{
311 312
	return repsep_fprintf(fp, "%-*s", width,
			      self->parent ? self->parent->name : "[other]");
313 314
}

315 316
static unsigned int parent_symbol__col_width;

317
static struct sort_entry sort_parent = {
318
	.header = "Parent symbol",
319 320
	.cmp	= sort__parent_cmp,
	.print	= sort__parent_print,
321
	.width	= &parent_symbol__col_width,
322 323
};

P
Peter Zijlstra 已提交
324
static int sort__need_collapse = 0;
325
static int sort__has_parent = 0;
P
Peter Zijlstra 已提交
326

327
struct sort_dimension {
328
	const char		*name;
329 330
	struct sort_entry	*entry;
	int			taken;
331 332 333 334
};

static struct sort_dimension sort_dimensions[] = {
	{ .name = "pid",	.entry = &sort_thread,	},
335
	{ .name = "comm",	.entry = &sort_comm,	},
336
	{ .name = "dso",	.entry = &sort_dso,	},
337
	{ .name = "symbol",	.entry = &sort_sym,	},
338
	{ .name = "parent",	.entry = &sort_parent,	},
339 340
};

341 342
static LIST_HEAD(hist_entry__sort_list);

343
static int sort_dimension__add(const char *tok)
344
{
345
	unsigned int i;
346 347 348 349 350 351 352

	for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
		struct sort_dimension *sd = &sort_dimensions[i];

		if (sd->taken)
			continue;

353
		if (strncasecmp(tok, sd->name, strlen(tok)))
354 355
			continue;

P
Peter Zijlstra 已提交
356 357 358
		if (sd->entry->collapse)
			sort__need_collapse = 1;

359 360
		if (sd->entry == &sort_parent) {
			int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
361 362 363
			if (ret) {
				char err[BUFSIZ];

364 365 366
				regerror(ret, &parent_regex, err, sizeof(err));
				fprintf(stderr, "Invalid regex: %s\n%s",
					parent_pattern, err);
367 368
				exit(-1);
			}
369
			sort__has_parent = 1;
370 371
		}

372 373
		list_add_tail(&sd->entry->list, &hist_entry__sort_list);
		sd->taken = 1;
374

375 376 377 378 379 380
		return 0;
	}

	return -ESRCH;
}

381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
static int64_t
hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
{
	struct sort_entry *se;
	int64_t cmp = 0;

	list_for_each_entry(se, &hist_entry__sort_list, list) {
		cmp = se->cmp(left, right);
		if (cmp)
			break;
	}

	return cmp;
}

P
Peter Zijlstra 已提交
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
static int64_t
hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
{
	struct sort_entry *se;
	int64_t cmp = 0;

	list_for_each_entry(se, &hist_entry__sort_list, list) {
		int64_t (*f)(struct hist_entry *, struct hist_entry *);

		f = se->collapse ?: se->cmp;

		cmp = f(left, right);
		if (cmp)
			break;
	}

	return cmp;
}

415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
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;
}
432
static size_t
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
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;
450
			ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
451 452 453 454 455 456 457 458 459 460 461
		} 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;
}

462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
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;
}

477 478 479 480 481 482 483 484
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;
485
	u64 new_total;
486
	u64 remaining;
487 488 489
	size_t ret = 0;
	int i;

490
	if (callchain_param.mode == CHAIN_GRAPH_REL)
491
		new_total = self->children_hit;
492 493 494
	else
		new_total = total_samples;

495 496
	remaining = new_total;

497 498
	node = rb_first(&self->rb_root);
	while (node) {
499 500
		u64 cumul;

501
		child = rb_entry(node, struct callchain_node, rb_node);
502 503
		cumul = cumul_hits(child);
		remaining -= cumul;
504 505 506 507

		/*
		 * The depth mask manages the output of pipes that show
		 * the depth. We don't want to keep the pipes of the current
508 509 510
		 * level for the last child of this depth.
		 * Except if we have remaining filtered hits. They will
		 * supersede the last child
511 512
		 */
		next = rb_next(node);
513
		if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
514 515 516 517 518 519 520 521 522 523 524 525 526
			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++,
527
						      new_total,
528
						      cumul);
529
		}
530
		ret += callchain__fprintf_graph(fp, child, new_total,
531 532 533 534 535
						depth + 1,
						new_depth_mask | (1 << depth));
		node = next;
	}

536 537 538 539 540 541 542 543 544 545 546 547 548
	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);
	}

549 550 551 552 553 554
	return ret;
}

static size_t
callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
			u64 total_samples)
555 556 557 558 559 560 561
{
	struct callchain_list *chain;
	size_t ret = 0;

	if (!self)
		return 0;

562
	ret += callchain__fprintf_flat(fp, self->parent, total_samples);
563 564


565 566 567 568 569 570 571
	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",
572
					(void *)(long)chain->ip);
573
	}
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591

	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;
592 593
		switch (callchain_param.mode) {
		case CHAIN_FLAT:
594 595
			ret += percent_color_fprintf(fp, "           %6.2f%%\n",
						     percent);
596
			ret += callchain__fprintf_flat(fp, chain, total_samples);
597 598 599
			break;
		case CHAIN_GRAPH_ABS: /* Falldown */
		case CHAIN_GRAPH_REL:
600 601
			ret += callchain__fprintf_graph(fp, chain,
							total_samples, 1, 1);
602
		case CHAIN_NONE:
603 604
		default:
			break;
605
		}
606 607 608 609 610 611 612 613
		ret += fprintf(fp, "\n");
		rb_node = rb_next(rb_node);
	}

	return ret;
}


614
static size_t
615
hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
616 617 618 619
{
	struct sort_entry *se;
	size_t ret;

620 621 622
	if (exclude_other && !self->parent)
		return 0;

623
	if (total_samples)
624 625 626
		ret = percent_color_fprintf(fp,
					    field_sep ? "%.2f" : "   %6.2f%%",
					(self->count * 100.0) / total_samples);
627
	else
628
		ret = fprintf(fp, field_sep ? "%lld" : "%12lld ", self->count);
629

630 631 632 633 634 635
	if (show_nr_samples) {
		if (field_sep)
			fprintf(fp, "%c%lld", *field_sep, self->count);
		else
			fprintf(fp, "%11lld", self->count);
	}
636

637
	list_for_each_entry(se, &hist_entry__sort_list, list) {
638
		if (se->elide)
639 640
			continue;

641 642
		fprintf(fp, "%s", field_sep ?: "  ");
		ret += se->print(fp, self, se->width ? *se->width : 0);
643
	}
644 645 646

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

647 648 649
	if (callchain)
		hist_entry_callchain__fprintf(fp, self, total_samples);

650 651 652
	return ret;
}

653 654 655 656
/*
 *
 */

657 658 659 660 661 662 663 664 665 666 667 668
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;
}

669
static void thread__comm_adjust(struct thread *self)
670
{
671
	char *comm = self->comm;
672 673 674 675 676 677 678 679 680 681

	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;
		}
	}
682 683 684 685 686 687 688 689 690 691
}

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);
692 693 694 695 696

	return 0;
}


697 698
static struct symbol *
resolve_symbol(struct thread *thread, struct map **mapp,
699
	       struct dso **dsop, u64 *ipp)
700 701 702
{
	struct dso *dso = dsop ? *dsop : NULL;
	struct map *map = mapp ? *mapp : NULL;
703
	u64 ip = *ipp;
704 705 706 707 708 709 710 711 712 713 714 715

	if (!thread)
		return NULL;

	if (dso)
		goto got_dso;

	if (map)
		goto got_map;

	map = thread__find_map(thread, ip);
	if (map != NULL) {
716 717 718 719 720
		/*
		 * 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.
		 */
721
		if (!sort_dso.elide && !map->dso->slen_calculated)
722 723
			dso__calc_col_width(map->dso);

724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
		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;
	}
740 741
	dump_printf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
	dump_printf(" ...... map: %Lx -> %Lx\n", *ipp, ip);
742
	*ipp  = ip;
743 744 745 746 747 748 749 750 751 752

	if (dsop)
		*dsop = dso;

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

753
static int call__match(struct symbol *sym)
754
{
755
	if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
756
		return 1;
757

758
	return 0;
759 760
}

761
static struct symbol **
762
resolve_callchain(struct thread *thread, struct map *map __used,
763 764 765
		    struct ip_callchain *chain, struct hist_entry *entry)
{
	u64 context = PERF_CONTEXT_MAX;
766
	struct symbol **syms = NULL;
767
	unsigned int i;
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787

	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 已提交
788 789 790
		case PERF_CONTEXT_HV:
			dso = hypervisor_dso;
			break;
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
		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;
}

813 814 815 816
/*
 * collect histogram counts
 */

817 818
static int
hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
819 820
		struct symbol *sym, u64 ip, struct ip_callchain *chain,
		char level, u64 count)
821
{
822 823 824
	struct rb_node **p = &hist.rb_node;
	struct rb_node *parent = NULL;
	struct hist_entry *he;
825
	struct symbol **syms = NULL;
826 827 828 829 830 831 832
	struct hist_entry entry = {
		.thread	= thread,
		.map	= map,
		.dso	= dso,
		.sym	= sym,
		.ip	= ip,
		.level	= level,
833
		.count	= count,
834
		.parent = NULL,
835
		.sorted_chain = RB_ROOT
836 837 838
	};
	int cmp;

839 840
	if ((sort__has_parent || callchain) && chain)
		syms = resolve_callchain(thread, map, chain, &entry);
841

842 843 844 845 846 847 848
	while (*p != NULL) {
		parent = *p;
		he = rb_entry(parent, struct hist_entry, rb_node);

		cmp = hist_entry__cmp(&entry, he);

		if (!cmp) {
849
			he->count += count;
850 851 852 853
			if (callchain) {
				append_chain(&he->callchain, chain, syms);
				free(syms);
			}
854 855 856 857 858 859 860
			return 0;
		}

		if (cmp < 0)
			p = &(*p)->rb_left;
		else
			p = &(*p)->rb_right;
861
	}
862 863 864 865 866

	he = malloc(sizeof(*he));
	if (!he)
		return -ENOMEM;
	*he = entry;
867 868
	if (callchain) {
		callchain_init(&he->callchain);
869 870
		append_chain(&he->callchain, chain, syms);
		free(syms);
871
	}
872 873 874 875
	rb_link_node(&he->rb_node, parent, p);
	rb_insert_color(&he->rb_node, &hist);

	return 0;
876 877
}

P
Peter Zijlstra 已提交
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
static void hist_entry__free(struct hist_entry *he)
{
	free(he);
}

/*
 * collapse the histogram
 */

static struct rb_root collapse_hists;

static void collapse__insert_entry(struct hist_entry *he)
{
	struct rb_node **p = &collapse_hists.rb_node;
	struct rb_node *parent = NULL;
	struct hist_entry *iter;
	int64_t cmp;

	while (*p != NULL) {
		parent = *p;
		iter = rb_entry(parent, struct hist_entry, rb_node);

		cmp = hist_entry__collapse(iter, he);

		if (!cmp) {
			iter->count += he->count;
			hist_entry__free(he);
			return;
		}

		if (cmp < 0)
			p = &(*p)->rb_left;
		else
			p = &(*p)->rb_right;
	}

	rb_link_node(&he->rb_node, parent, p);
	rb_insert_color(&he->rb_node, &collapse_hists);
}

static void collapse__resort(void)
{
	struct rb_node *next;
	struct hist_entry *n;

	if (!sort__need_collapse)
		return;

	next = rb_first(&hist);
	while (next) {
		n = rb_entry(next, struct hist_entry, rb_node);
		next = rb_next(&n->rb_node);

		rb_erase(&n->rb_node, &hist);
		collapse__insert_entry(n);
	}
}

936 937 938 939 940 941
/*
 * reverse the map, sort on count.
 */

static struct rb_root output_hists;

942
static void output__insert_entry(struct hist_entry *he, u64 min_callchain_hits)
943
{
944
	struct rb_node **p = &output_hists.rb_node;
945
	struct rb_node *parent = NULL;
946
	struct hist_entry *iter;
947

948 949 950
	if (callchain)
		callchain_param.sort(&he->sorted_chain, &he->callchain,
				      min_callchain_hits, &callchain_param);
951

952 953
	while (*p != NULL) {
		parent = *p;
954
		iter = rb_entry(parent, struct hist_entry, rb_node);
955

956
		if (he->count > iter->count)
957 958 959 960 961
			p = &(*p)->rb_left;
		else
			p = &(*p)->rb_right;
	}

962 963
	rb_link_node(&he->rb_node, parent, p);
	rb_insert_color(&he->rb_node, &output_hists);
964 965
}

966
static void output__resort(u64 total_samples)
967
{
P
Peter Zijlstra 已提交
968
	struct rb_node *next;
969
	struct hist_entry *n;
970
	struct rb_root *tree = &hist;
971 972
	u64 min_callchain_hits;

973
	min_callchain_hits = total_samples * (callchain_param.min_percent / 100);
974

P
Peter Zijlstra 已提交
975
	if (sort__need_collapse)
976 977 978
		tree = &collapse_hists;

	next = rb_first(tree);
P
Peter Zijlstra 已提交
979

980 981 982
	while (next) {
		n = rb_entry(next, struct hist_entry, rb_node);
		next = rb_next(&n->rb_node);
983

984
		rb_erase(&n->rb_node, tree);
985
		output__insert_entry(n, min_callchain_hits);
986 987 988
	}
}

989
static size_t output__fprintf(FILE *fp, u64 total_samples)
990
{
991
	struct hist_entry *pos;
992
	struct sort_entry *se;
993 994
	struct rb_node *nd;
	size_t ret = 0;
995 996
	unsigned int width;
	char *col_width = col_width_list_str;
997 998 999
	int raw_printing_style;

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

1001 1002
	init_rem_hits();

1003
	fprintf(fp, "# Samples: %Ld\n", (u64)total_samples);
1004 1005 1006
	fprintf(fp, "#\n");

	fprintf(fp, "# Overhead");
1007 1008 1009 1010 1011 1012
	if (show_nr_samples) {
		if (field_sep)
			fprintf(fp, "%cSamples", *field_sep);
		else
			fputs("  Samples  ", fp);
	}
1013
	list_for_each_entry(se, &hist_entry__sort_list, list) {
1014
		if (se->elide)
1015
			continue;
1016 1017
		if (field_sep) {
			fprintf(fp, "%c%s", *field_sep, se->header);
1018
			continue;
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
		}
		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);
1033
	}
1034 1035
	fprintf(fp, "\n");

1036 1037 1038
	if (field_sep)
		goto print_entries;

1039
	fprintf(fp, "# ........");
1040 1041
	if (show_nr_samples)
		fprintf(fp, " ..........");
1042
	list_for_each_entry(se, &hist_entry__sort_list, list) {
1043
		unsigned int i;
1044

1045
		if (se->elide)
1046 1047
			continue;

1048
		fprintf(fp, "  ");
1049 1050 1051 1052 1053
		if (se->width)
			width = *se->width;
		else
			width = strlen(se->header);
		for (i = 0; i < width; i++)
1054
			fprintf(fp, ".");
1055
	}
1056 1057 1058
	fprintf(fp, "\n");

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

1060
print_entries:
1061 1062 1063
	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);
1064 1065
	}

1066 1067
	if (sort_order == default_sort_order &&
			parent_pattern == default_parent_pattern) {
1068
		fprintf(fp, "#\n");
1069
		fprintf(fp, "# (For a higher level overview, try: perf report --sort comm,dso)\n");
1070 1071
		fprintf(fp, "#\n");
	}
1072
	fprintf(fp, "\n");
1073

1074 1075
	free(rem_sq_bracket);

1076
	if (show_threads)
1077 1078
		perf_read_values_display(fp, &show_threads_values,
					 raw_printing_style);
1079

1080 1081 1082
	return ret;
}

1083 1084 1085 1086
static unsigned long total = 0,
		     total_mmap = 0,
		     total_comm = 0,
		     total_fork = 0,
1087 1088
		     total_unknown = 0,
		     total_lost = 0;
1089

1090
static int validate_chain(struct ip_callchain *chain, event_t *event)
1091 1092 1093 1094 1095 1096
{
	unsigned int chain_size;

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

1097
	if (chain->nr*sizeof(u64) > chain_size)
1098 1099 1100 1101 1102
		return -1;

	return 0;
}

1103
static int
1104
process_sample_event(event_t *event, unsigned long offset, unsigned long head)
1105 1106 1107 1108
{
	char level;
	int show = 0;
	struct dso *dso = NULL;
1109
	struct thread *thread;
1110 1111
	u64 ip = event->ip.ip;
	u64 period = 1;
1112
	struct map *map = NULL;
1113
	void *more_data = event->ip.__more_data;
1114
	struct ip_callchain *chain = NULL;
1115
	int cpumode;
1116

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

1119
	if (sample_type & PERF_SAMPLE_PERIOD) {
1120 1121
		period = *(u64 *)more_data;
		more_data += sizeof(u64);
1122
	}
1123

1124
	dump_printf("%p [%p]: PERF_RECORD_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
1125 1126 1127
		(void *)(offset + head),
		(void *)(long)(event->header.size),
		event->header.misc,
1128
		event->ip.pid, event->ip.tid,
1129
		(void *)(long)ip,
1130
		(long long)period);
1131

1132
	if (sample_type & PERF_SAMPLE_CALLCHAIN) {
1133
		unsigned int i;
1134 1135 1136

		chain = (void *)more_data;

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

1139 1140 1141 1142 1143 1144
		if (validate_chain(chain, event) < 0) {
			eprintf("call-chain problem with event, skipping it.\n");
			return 0;
		}

		if (dump_trace) {
1145
			for (i = 0; i < chain->nr; i++)
1146
				dump_printf("..... %2d: %016Lx\n", i, chain->ips[i]);
1147 1148 1149
		}
	}

1150
	dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1151 1152

	if (thread == NULL) {
1153
		eprintf("problem processing %d event, skipping it.\n",
1154 1155 1156
			event->header.type);
		return -1;
	}
1157

1158 1159 1160
	if (comm_list && !strlist__has_entry(comm_list, thread->comm))
		return 0;

1161
	cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1162

1163
	if (cpumode == PERF_RECORD_MISC_KERNEL) {
1164 1165
		show = SHOW_KERNEL;
		level = 'k';
1166

1167
		dso = kernel_dso;
1168

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

1171
	} else if (cpumode == PERF_RECORD_MISC_USER) {
1172

1173 1174
		show = SHOW_USER;
		level = '.';
1175

1176 1177 1178
	} else {
		show = SHOW_HV;
		level = 'H';
1179 1180 1181

		dso = hypervisor_dso;

1182
		dump_printf(" ...... dso: [hypervisor]\n");
1183
	}
1184

1185
	if (show & show_mask) {
1186
		struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
1187

1188 1189
		if (dso_list && (!dso || !dso->name ||
				 !strlist__has_entry(dso_list, dso->name)))
1190 1191
			return 0;

1192
		if (sym_list && (!sym || !strlist__has_entry(sym_list, sym->name)))
1193 1194
			return 0;

1195
		if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
1196
			eprintf("problem incrementing symbol count, skipping event\n");
1197
			return -1;
1198
		}
1199
	}
1200
	total += period;
1201

1202 1203
	return 0;
}
I
Ingo Molnar 已提交
1204

1205 1206 1207
static int
process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
{
1208
	struct thread *thread;
1209
	struct map *map = map__new(&event->mmap, cwd, cwdlen);
1210

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

1213
	dump_printf("%p [%p]: PERF_RECORD_MMAP %d/%d: [%p(%p) @ %p]: %s\n",
1214 1215
		(void *)(offset + head),
		(void *)(long)(event->header.size),
1216
		event->mmap.pid,
1217
		event->mmap.tid,
1218 1219 1220 1221 1222 1223
		(void *)(long)event->mmap.start,
		(void *)(long)event->mmap.len,
		(void *)(long)event->mmap.pgoff,
		event->mmap.filename);

	if (thread == NULL || map == NULL) {
1224
		dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
1225
		return 0;
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
	}

	thread__insert_map(thread, map);
	total_mmap++;

	return 0;
}

static int
process_comm_event(event_t *event, unsigned long offset, unsigned long head)
{
1237 1238 1239
	struct thread *thread;

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

1241
	dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
1242 1243 1244 1245 1246
		(void *)(offset + head),
		(void *)(long)(event->header.size),
		event->comm.comm, event->comm.pid);

	if (thread == NULL ||
1247
	    thread__set_comm_adjust(thread, event->comm.comm)) {
1248
		dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
1249
		return -1;
1250
	}
1251 1252 1253 1254 1255
	total_comm++;

	return 0;
}

1256
static int
1257
process_task_event(event_t *event, unsigned long offset, unsigned long head)
1258
{
1259 1260 1261 1262 1263
	struct thread *thread;
	struct thread *parent;

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

1265
	dump_printf("%p [%p]: PERF_RECORD_%s: (%d:%d):(%d:%d)\n",
1266 1267
		(void *)(offset + head),
		(void *)(long)(event->header.size),
1268
		event->header.type == PERF_RECORD_FORK ? "FORK" : "EXIT",
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
		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;

1279
	if (event->header.type == PERF_RECORD_EXIT)
1280
		return 0;
1281 1282

	if (!thread || !parent || thread__fork(thread, parent)) {
1283
		dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
1284 1285 1286 1287 1288 1289 1290
		return -1;
	}
	total_fork++;

	return 0;
}

1291 1292 1293
static int
process_lost_event(event_t *event, unsigned long offset, unsigned long head)
{
1294
	dump_printf("%p [%p]: PERF_RECORD_LOST: id:%Ld: lost:%Ld\n",
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
		(void *)(offset + head),
		(void *)(long)(event->header.size),
		event->lost.id,
		event->lost.lost);

	total_lost += event->lost.lost;

	return 0;
}

1305 1306 1307
static int
process_read_event(event_t *event, unsigned long offset, unsigned long head)
{
1308
	struct perf_event_attr *attr;
1309 1310

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

1312
	if (show_threads) {
1313
		const char *name = attr ? __event_name(attr->type, attr->config)
1314 1315 1316 1317 1318 1319 1320 1321
				   : "unknown";
		perf_read_values_add_value(&show_threads_values,
					   event->read.pid, event->read.tid,
					   event->read.id,
					   name,
					   event->read.value);
	}

1322
	dump_printf("%p [%p]: PERF_RECORD_READ: %d %d %s %Lu\n",
1323 1324 1325 1326
			(void *)(offset + head),
			(void *)(long)(event->header.size),
			event->read.pid,
			event->read.tid,
1327 1328
			attr ? __event_name(attr->type, attr->config)
			     : "FAIL",
1329 1330 1331 1332 1333
			event->read.value);

	return 0;
}

1334 1335 1336
static int
process_event(event_t *event, unsigned long offset, unsigned long head)
{
1337 1338
	trace_event(event);

1339
	switch (event->header.type) {
1340
	case PERF_RECORD_SAMPLE:
1341 1342
		return process_sample_event(event, offset, head);

1343
	case PERF_RECORD_MMAP:
1344 1345
		return process_mmap_event(event, offset, head);

1346
	case PERF_RECORD_COMM:
1347 1348
		return process_comm_event(event, offset, head);

1349 1350
	case PERF_RECORD_FORK:
	case PERF_RECORD_EXIT:
1351
		return process_task_event(event, offset, head);
1352

1353
	case PERF_RECORD_LOST:
1354 1355
		return process_lost_event(event, offset, head);

1356
	case PERF_RECORD_READ:
1357 1358
		return process_read_event(event, offset, head);

1359 1360 1361
	/*
	 * We dont process them right now but they are fine:
	 */
1362

1363 1364
	case PERF_RECORD_THROTTLE:
	case PERF_RECORD_UNTHROTTLE:
1365 1366
		return 0;

1367 1368 1369 1370 1371 1372 1373 1374 1375
	default:
		return -1;
	}

	return 0;
}

static int __cmd_report(void)
{
1376
	int ret, rc = EXIT_FAILURE;
1377
	unsigned long offset = 0;
1378
	unsigned long head, shift;
1379
	struct stat input_stat;
1380
	struct thread *idle;
1381 1382
	event_t *event;
	uint32_t size;
1383
	char *buf;
1384

1385 1386
	idle = register_idle_thread(&threads, &last_match);
	thread__comm_adjust(idle);
1387

1388 1389 1390
	if (show_threads)
		perf_read_values_init(&show_threads_values);

1391 1392
	input = open(input_name, O_RDONLY);
	if (input < 0) {
1393 1394 1395 1396
		fprintf(stderr, " failed to open file: %s", input_name);
		if (!strcmp(input_name, "perf.data"))
			fprintf(stderr, "  (try 'perf record' first)");
		fprintf(stderr, "\n");
1397 1398 1399
		exit(-1);
	}

1400
	ret = fstat(input, &input_stat);
1401 1402 1403 1404 1405
	if (ret < 0) {
		perror("failed to stat file");
		exit(-1);
	}

1406 1407
	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);
1408 1409 1410
		exit(-1);
	}

1411
	if (!input_stat.st_size) {
1412 1413 1414 1415
		fprintf(stderr, "zero-sized file, nothing to do!\n");
		exit(0);
	}

1416 1417
	header = perf_header__read(input);
	head = header->data_offset;
1418

1419
	sample_type = perf_header__sample_type(header);
1420

1421 1422 1423 1424 1425 1426 1427 1428
	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) {
1429
			fprintf(stderr, "selected -g but no callchain data."
1430 1431 1432 1433
					" Did you call perf record without"
					" -g?\n");
			exit(-1);
		}
1434 1435 1436 1437 1438 1439 1440
	} 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);
			}
1441 1442
	}

1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457
	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;
	}
1458 1459 1460 1461 1462

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

1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478
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) {
1479
		int munmap_ret;
1480

1481 1482
		shift = page_size * (head / page_size);

1483 1484
		munmap_ret = munmap(buf, page_size * mmap_window);
		assert(munmap_ret == 0);
1485 1486 1487 1488 1489 1490 1491 1492

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

	size = event->header.size;

1493
	dump_printf("\n%p [%p]: event: %d\n",
1494 1495 1496 1497
			(void *)(offset + head),
			(void *)(long)event->header.size,
			event->header.type);

1498 1499
	if (!size || process_event(event, offset, head) < 0) {

1500
		dump_printf("%p [%p]: skipping unknown header type: %d\n",
I
Ingo Molnar 已提交
1501 1502 1503
			(void *)(offset + head),
			(void *)(long)(event->header.size),
			event->header.type);
1504

1505
		total_unknown++;
1506 1507 1508 1509 1510 1511 1512 1513 1514 1515

		/*
		 * 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;
1516
	}
1517

1518
	head += size;
I
Ingo Molnar 已提交
1519

1520
	if (offset + head >= header->data_offset + header->data_size)
1521 1522
		goto done;

1523
	if (offset + head < (unsigned long)input_stat.st_size)
1524 1525
		goto more;

1526
done:
1527 1528
	rc = EXIT_SUCCESS;
	close(input);
1529

1530 1531 1532 1533 1534 1535
	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);
1536

I
Ingo Molnar 已提交
1537
	if (dump_trace)
1538 1539
		return 0;

1540
	if (verbose >= 3)
1541
		threads__fprintf(stdout, &threads);
1542

1543
	if (verbose >= 2)
1544 1545
		dsos__fprintf(stdout);

P
Peter Zijlstra 已提交
1546
	collapse__resort();
1547
	output__resort(total);
1548
	output__fprintf(stdout, total);
1549

1550 1551 1552
	if (show_threads)
		perf_read_values_destroy(&show_threads_values);

1553 1554 1555
	return rc;
}

1556 1557 1558 1559
static int
parse_callchain_opt(const struct option *opt __used, const char *arg,
		    int unset __used)
{
1560 1561 1562
	char *tok;
	char *endptr;

1563 1564 1565 1566 1567
	callchain = 1;

	if (!arg)
		return 0;

1568 1569 1570 1571 1572 1573
	tok = strtok((char *)arg, ",");
	if (!tok)
		return -1;

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

1576
	else if (!strncmp(tok, "flat", strlen(arg)))
1577 1578 1579 1580 1581
		callchain_param.mode = CHAIN_FLAT;

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

1582 1583 1584 1585 1586 1587 1588
	else if (!strncmp(tok, "none", strlen(arg))) {
		callchain_param.mode = CHAIN_NONE;
		callchain = 0;

		return 0;
	}

1589 1590 1591
	else
		return -1;

1592 1593 1594
	/* get the min percentage */
	tok = strtok(NULL, ",");
	if (!tok)
1595
		goto setup;
1596

1597
	callchain_param.min_percent = strtod(tok, &endptr);
1598 1599 1600
	if (tok == endptr)
		return -1;

1601 1602 1603 1604 1605
setup:
	if (register_callchain_param(&callchain_param) < 0) {
		fprintf(stderr, "Can't register callchain params\n");
		return -1;
	}
1606 1607 1608
	return 0;
}

1609 1610 1611 1612 1613 1614 1615 1616
static const char * const report_usage[] = {
	"perf report [<options>] <command>",
	NULL
};

static const struct option options[] = {
	OPT_STRING('i', "input", &input_name, "file",
		    "input file name"),
1617 1618
	OPT_BOOLEAN('v', "verbose", &verbose,
		    "be more verbose (show symbol address, etc)"),
1619 1620
	OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
		    "dump raw trace in ASCII"),
1621
	OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
1622
	OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
1623 1624
	OPT_BOOLEAN('m', "modules", &modules,
		    "load module symbols - WARNING: use only with -k and LIVE kernel"),
1625 1626
	OPT_BOOLEAN('n', "show-nr-samples", &show_nr_samples,
		    "Show a column with the number of samples"),
1627 1628
	OPT_BOOLEAN('T', "threads", &show_threads,
		    "Show per-thread event counters"),
1629 1630
	OPT_STRING(0, "pretty", &pretty_printing_style, "key",
		   "pretty printing style key: normal raw"),
1631
	OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1632
		   "sort by key(s): pid, comm, dso, symbol, parent"),
1633 1634
	OPT_BOOLEAN('P', "full-paths", &full_paths,
		    "Don't shorten the pathnames taking into account the cwd"),
1635 1636
	OPT_STRING('p', "parent", &parent_pattern, "regex",
		   "regex filter to identify parent, see: '--sort parent'"),
1637 1638
	OPT_BOOLEAN('x', "exclude-other", &exclude_other,
		    "Only display entries with parent-match"),
1639
	OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
1640
		     "Display callchains using output_type and min percent threshold. "
1641
		     "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
1642 1643
	OPT_STRING('d', "dsos", &dso_list_str, "dso[,dso...]",
		   "only consider symbols in these dsos"),
1644 1645
	OPT_STRING('C', "comms", &comm_list_str, "comm[,comm...]",
		   "only consider symbols in these comms"),
1646 1647
	OPT_STRING('S', "symbols", &sym_list_str, "symbol[,symbol...]",
		   "only consider these symbols"),
1648 1649 1650 1651 1652 1653
	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."),
1654 1655 1656
	OPT_END()
};

1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671
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);
}

1672
static void setup_list(struct strlist **list, const char *list_str,
1673 1674
		       struct sort_entry *se, const char *list_name,
		       FILE *fp)
1675 1676 1677 1678 1679 1680 1681 1682
{
	if (list_str) {
		*list = strlist__new(true, list_str);
		if (!*list) {
			fprintf(stderr, "problems parsing %s list\n",
				list_name);
			exit(129);
		}
1683 1684 1685 1686 1687
		if (strlist__nr_entries(*list) == 1) {
			fprintf(fp, "# %s: %s\n", list_name,
				strlist__entry(*list, 0)->s);
			se->elide = true;
		}
1688 1689 1690
	}
}

1691
int cmd_report(int argc, const char **argv, const char *prefix __used)
1692
{
1693
	symbol__init();
1694 1695 1696

	page_size = getpagesize();

1697
	argc = parse_options(argc, argv, options, report_usage, 0);
1698

1699 1700
	setup_sorting();

1701
	if (parent_pattern != default_parent_pattern) {
1702
		sort_dimension__add("parent");
1703 1704
		sort_parent.elide = 1;
	} else
1705 1706
		exclude_other = 0;

1707 1708 1709 1710 1711 1712
	/*
	 * Any (unrecognized) arguments left?
	 */
	if (argc)
		usage_with_options(report_usage, options);

1713 1714
	setup_pager();

1715 1716 1717
	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);
1718

1719 1720 1721 1722 1723 1724
	if (field_sep && *field_sep == '.') {
		fputs("'.' is the only non valid --field-separator argument\n",
		      stderr);
		exit(129);
	}

1725 1726
	return __cmd_report();
}