builtin-report.c 12.7 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/callchain.h"
18
#include "util/strlist.h"
19
#include "util/values.h"
20

21
#include "perf.h"
22
#include "util/debug.h"
23
#include "util/header.h"
24
#include "util/session.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
static int		force;
36
static bool		hide_unresolved;
37
static bool		dont_use_callchains;
38

39 40 41
static int		show_threads;
static struct perf_read_values	show_threads_values;

42 43 44
static char		default_pretty_printing_style[] = "normal";
static char		*pretty_printing_style = default_pretty_printing_style;

45 46
static char		callchain_default_opt[] = "fractal,0.5";

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
static struct event_stat_id *get_stats(struct perf_session *self,
				       u64 event_stream, u32 type, u64 config)
{
	struct rb_node **p = &self->stats_by_id.rb_node;
	struct rb_node *parent = NULL;
	struct event_stat_id *iter, *new;

	while (*p != NULL) {
		parent = *p;
		iter = rb_entry(parent, struct event_stat_id, rb_node);
		if (iter->config == config)
			return iter;


		if (config > iter->config)
			p = &(*p)->rb_right;
		else
			p = &(*p)->rb_left;
	}

	new = malloc(sizeof(struct event_stat_id));
	if (new == NULL)
		return NULL;
	memset(new, 0, sizeof(struct event_stat_id));
	new->event_stream = event_stream;
	new->config = config;
	new->type = type;
	rb_link_node(&new->rb_node, parent, p);
	rb_insert_color(&new->rb_node, &self->stats_by_id);
	return new;
}

79 80
static int perf_session__add_hist_entry(struct perf_session *self,
					struct addr_location *al,
81
					struct sample_data *data)
82
{
83 84
	struct map_symbol *syms = NULL;
	struct symbol *parent = NULL;
85
	bool hit;
86
	int err;
87
	struct hist_entry *he;
88 89
	struct event_stat_id *stats;
	struct perf_event_attr *attr;
90

91
	if ((sort__has_parent || symbol_conf.use_callchain) && data->callchain) {
92
		syms = perf_session__resolve_callchain(self, al->thread,
93
						       data->callchain, &parent);
94 95 96
		if (syms == NULL)
			return -ENOMEM;
	}
97 98 99 100 101 102 103 104 105 106

	attr = perf_header__find_attr(data->id, &self->header);
	if (attr)
		stats = get_stats(self, data->id, attr->type, attr->config);
	else
		stats = get_stats(self, data->id, 0, 0);
	if (stats == NULL)
		return -ENOMEM;
	he = __perf_session__add_hist_entry(&stats->hists, al, parent,
					    data->period, &hit);
107 108
	if (he == NULL)
		return -ENOMEM;
109

110
	if (hit)
111
		he->count += data->period;
112

113
	if (symbol_conf.use_callchain) {
114
		if (!hit)
115 116
			callchain_init(he->callchain);
		err = append_chain(he->callchain, data->callchain, syms);
117
		free(syms);
118 119 120

		if (err)
			return err;
121
	}
122 123

	return 0;
124 125
}

126
static int validate_chain(struct ip_callchain *chain, event_t *event)
127 128 129 130 131 132
{
	unsigned int chain_size;

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

133
	if (chain->nr*sizeof(u64) > chain_size)
134 135 136 137 138
		return -1;

	return 0;
}

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
static int add_event_total(struct perf_session *session,
			   struct sample_data *data,
			   struct perf_event_attr *attr)
{
	struct event_stat_id *stats;

	if (attr)
		stats = get_stats(session, data->id, attr->type, attr->config);
	else
		stats = get_stats(session, data->id, 0, 0);

	if (!stats)
		return -ENOMEM;

	stats->stats.total += data->period;
	session->events_stats.total += data->period;
	return 0;
}

158
static int process_sample_event(event_t *event, struct perf_session *session)
159
{
160
	struct sample_data data = { .period = 1, };
161
	struct addr_location al;
162
	struct perf_event_attr *attr;
163

164
	event__parse_sample(event, session->sample_type, &data);
165

166 167
	dump_printf("(IP, %d): %d/%d: %#Lx period: %Ld\n", event->header.misc,
		    data.pid, data.tid, data.ip, data.period);
168

169
	if (session->sample_type & PERF_SAMPLE_CALLCHAIN) {
170
		unsigned int i;
171

172
		dump_printf("... chain: nr:%Lu\n", data.callchain->nr);
173

174
		if (validate_chain(data.callchain, event) < 0) {
175 176
			pr_debug("call-chain problem with event, "
				 "skipping it.\n");
177 178 179 180
			return 0;
		}

		if (dump_trace) {
181 182 183
			for (i = 0; i < data.callchain->nr; i++)
				dump_printf("..... %2d: %016Lx\n",
					    i, data.callchain->ips[i]);
184 185 186
		}
	}

187 188
	if (event__preprocess_sample(event, session, &al, NULL) < 0) {
		fprintf(stderr, "problem processing %d event, skipping it.\n",
189 190 191
			event->header.type);
		return -1;
	}
192

193
	if (al.filtered || (hide_unresolved && al.sym == NULL))
194
		return 0;
195

196
	if (perf_session__add_hist_entry(session, &al, &data)) {
197
		pr_debug("problem incrementing symbol count, skipping event\n");
198
		return -1;
199
	}
200

201 202 203 204 205 206 207
	attr = perf_header__find_attr(data.id, &session->header);

	if (add_event_total(session, &data, attr)) {
		pr_debug("problem adding event count\n");
		return -1;
	}

208 209
	return 0;
}
I
Ingo Molnar 已提交
210

211
static int process_read_event(event_t *event, struct perf_session *session __used)
212
{
213
	struct perf_event_attr *attr;
214

215
	attr = perf_header__find_attr(event->read.id, &session->header);
216

217
	if (show_threads) {
218
		const char *name = attr ? __event_name(attr->type, attr->config)
219 220 221 222 223 224 225 226
				   : "unknown";
		perf_read_values_add_value(&show_threads_values,
					   event->read.pid, event->read.tid,
					   event->read.id,
					   name,
					   event->read.value);
	}

227 228 229
	dump_printf(": %d %d %s %Lu\n", event->read.pid, event->read.tid,
		    attr ? __event_name(attr->type, attr->config) : "FAIL",
		    event->read.value);
230 231 232 233

	return 0;
}

234
static int perf_session__setup_sample_type(struct perf_session *self)
235
{
236
	if (!(self->sample_type & PERF_SAMPLE_CALLCHAIN)) {
237 238 239 240
		if (sort__has_parent) {
			fprintf(stderr, "selected --sort parent, but no"
					" callchain data. Did you call"
					" perf record without -g?\n");
241
			return -EINVAL;
242
		}
243
		if (symbol_conf.use_callchain) {
244
			fprintf(stderr, "selected -g but no callchain data."
245 246
					" Did you call perf record without"
					" -g?\n");
247
			return -1;
248
		}
249 250
	} else if (!dont_use_callchains && callchain_param.mode != CHAIN_NONE &&
		   !symbol_conf.use_callchain) {
251
			symbol_conf.use_callchain = true;
252 253 254
			if (register_callchain_param(&callchain_param) < 0) {
				fprintf(stderr, "Can't register callchain"
						" params\n");
255
				return -EINVAL;
256
			}
257 258
	}

259 260
	return 0;
}
261

262
static struct perf_event_ops event_ops = {
263 264 265 266 267 268 269
	.sample	= process_sample_event,
	.mmap	= event__process_mmap,
	.comm	= event__process_comm,
	.exit	= event__process_task,
	.fork	= event__process_task,
	.lost	= event__process_lost,
	.read	= process_read_event,
270
};
271

272 273
static int __cmd_report(void)
{
274
	int ret = -EINVAL;
275
	struct perf_session *session;
276
	struct rb_node *next;
277
	const char *help = "For a higher level overview, try: perf report --sort comm,dso";
278

279
	session = perf_session__new(input_name, O_RDONLY, force);
280 281 282
	if (session == NULL)
		return -ENOMEM;

283 284
	if (show_threads)
		perf_read_values_init(&show_threads_values);
285

286 287 288 289
	ret = perf_session__setup_sample_type(session);
	if (ret)
		goto out_delete;

290
	ret = perf_session__process_events(session, &event_ops);
291
	if (ret)
292
		goto out_delete;
293

294 295
	if (dump_trace) {
		event__print_totals();
296
		goto out_delete;
297
	}
298

299
	if (verbose > 3)
300
		perf_session__fprintf(session, stdout);
301

302
	if (verbose > 2)
303 304
		dsos__fprintf(stdout);

305 306 307
	next = rb_first(&session->stats_by_id);
	while (next) {
		struct event_stat_id *stats;
308
		u64 nr_hists;
309 310 311

		stats = rb_entry(next, struct event_stat_id, rb_node);
		perf_session__collapse_resort(&stats->hists);
312 313
		nr_hists = perf_session__output_resort(&stats->hists,
						       stats->stats.total);
314
		if (use_browser)
315
			perf_session__browse_hists(&stats->hists, nr_hists,
316 317 318 319 320 321 322 323 324 325 326 327
						   stats->stats.total, help);
		else {
			if (rb_first(&session->stats_by_id) ==
			    rb_last(&session->stats_by_id))
				fprintf(stdout, "# Samples: %Ld\n#\n",
					stats->stats.total);
			else
				fprintf(stdout, "# Samples: %Ld %s\n#\n",
					stats->stats.total,
					__event_name(stats->type, stats->config));

			perf_session__fprintf_hists(&stats->hists, NULL, false, stdout,
328
					    stats->stats.total);
329 330 331
			fprintf(stdout, "\n\n");
		}

332 333 334
		next = rb_next(&stats->rb_node);
	}

335 336 337
	if (!use_browser && sort_order == default_sort_order &&
	    parent_pattern == default_parent_pattern) {
		fprintf(stdout, "#\n# (%s)\n#\n", help);
338

339 340 341 342 343 344
		if (show_threads) {
			bool style = !strcmp(pretty_printing_style, "raw");
			perf_read_values_display(stdout, &show_threads_values,
						 style);
			perf_read_values_destroy(&show_threads_values);
		}
345
	}
346 347
out_delete:
	perf_session__delete(session);
348
	return ret;
349 350
}

351 352
static int
parse_callchain_opt(const struct option *opt __used, const char *arg,
353
		    int unset)
354
{
355 356 357
	char *tok;
	char *endptr;

358 359 360 361 362 363 364 365
	/*
	 * --no-call-graph
	 */
	if (unset) {
		dont_use_callchains = true;
		return 0;
	}

366
	symbol_conf.use_callchain = true;
367 368 369 370

	if (!arg)
		return 0;

371 372 373 374 375 376
	tok = strtok((char *)arg, ",");
	if (!tok)
		return -1;

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

379
	else if (!strncmp(tok, "flat", strlen(arg)))
380 381 382 383 384
		callchain_param.mode = CHAIN_FLAT;

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

385 386
	else if (!strncmp(tok, "none", strlen(arg))) {
		callchain_param.mode = CHAIN_NONE;
387
		symbol_conf.use_callchain = false;
388 389 390 391

		return 0;
	}

392 393 394
	else
		return -1;

395 396 397
	/* get the min percentage */
	tok = strtok(NULL, ",");
	if (!tok)
398
		goto setup;
399

400
	callchain_param.min_percent = strtod(tok, &endptr);
401 402 403
	if (tok == endptr)
		return -1;

404 405 406 407 408
setup:
	if (register_callchain_param(&callchain_param) < 0) {
		fprintf(stderr, "Can't register callchain params\n");
		return -1;
	}
409 410 411
	return 0;
}

412
static const char * const report_usage[] = {
413 414 415 416 417 418 419
	"perf report [<options>] <command>",
	NULL
};

static const struct option options[] = {
	OPT_STRING('i', "input", &input_name, "file",
		    "input file name"),
420 421
	OPT_BOOLEAN('v', "verbose", &verbose,
		    "be more verbose (show symbol address, etc)"),
422 423
	OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
		    "dump raw trace in ASCII"),
424 425
	OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
		   "file", "vmlinux pathname"),
426
	OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
427
	OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
428
		    "load module symbols - WARNING: use only with -k and LIVE kernel"),
429
	OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
430
		    "Show a column with the number of samples"),
431 432
	OPT_BOOLEAN('T', "threads", &show_threads,
		    "Show per-thread event counters"),
433 434
	OPT_STRING(0, "pretty", &pretty_printing_style, "key",
		   "pretty printing style key: normal raw"),
435
	OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
436
		   "sort by key(s): pid, comm, dso, symbol, parent"),
437
	OPT_BOOLEAN('P', "full-paths", &symbol_conf.full_paths,
438
		    "Don't shorten the pathnames taking into account the cwd"),
439 440
	OPT_STRING('p', "parent", &parent_pattern, "regex",
		   "regex filter to identify parent, see: '--sort parent'"),
441
	OPT_BOOLEAN('x', "exclude-other", &symbol_conf.exclude_other,
442
		    "Only display entries with parent-match"),
443
	OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
444
		     "Display callchains using output_type and min percent threshold. "
445
		     "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
446
	OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
447
		   "only consider symbols in these dsos"),
448
	OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
449
		   "only consider symbols in these comms"),
450
	OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
451
		   "only consider these symbols"),
452
	OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str,
453 454
		   "width[,width...]",
		   "don't try to adjust column width, use these fixed values"),
455
	OPT_STRING('t', "field-separator", &symbol_conf.field_sep, "separator",
456 457
		   "separator for columns, no spaces will be added between "
		   "columns '.' is reserved."),
458 459
	OPT_BOOLEAN('U', "hide-unresolved", &hide_unresolved,
		    "Only display entries resolved to a symbol"),
460 461 462
	OPT_END()
};

463
int cmd_report(int argc, const char **argv, const char *prefix __used)
464
{
465 466
	argc = parse_options(argc, argv, options, report_usage, 0);

467
	setup_browser();
468

469
	if (symbol__init() < 0)
470
		return -1;
471

472
	setup_sorting(report_usage, options);
473

474
	if (parent_pattern != default_parent_pattern) {
475 476
		if (sort_dimension__add("parent") < 0)
			return -1;
477 478
		sort_parent.elide = 1;
	} else
479
		symbol_conf.exclude_other = false;
480

481 482 483 484 485 486
	/*
	 * Any (unrecognized) arguments left?
	 */
	if (argc)
		usage_with_options(report_usage, options);

487 488 489
	sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list, "dso", stdout);
	sort_entry__setup_elide(&sort_comm, symbol_conf.comm_list, "comm", stdout);
	sort_entry__setup_elide(&sort_sym, symbol_conf.sym_list, "symbol", stdout);
490

491 492
	return __cmd_report();
}