builtin-report.c 10.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
#include "util/session.h"
26 27 28 29

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

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

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

36
static int		force;
37
static bool		hide_unresolved;
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
static int perf_session__add_hist_entry(struct perf_session *self,
					struct addr_location *al,
					struct ip_callchain *chain, u64 count)
50
{
51 52
	struct symbol **syms = NULL, *parent = NULL;
	bool hit;
53 54
	struct hist_entry *he;

55
	if ((sort__has_parent || symbol_conf.use_callchain) && chain)
56 57
		syms = perf_session__resolve_callchain(self, al->thread,
						       chain, &parent);
58
	he = __perf_session__add_hist_entry(self, al, parent, count, &hit);
59 60
	if (he == NULL)
		return -ENOMEM;
61

62 63
	if (hit)
		he->count += count;
64

65
	if (symbol_conf.use_callchain) {
66 67
		if (!hit)
			callchain_init(&he->callchain);
68 69
		append_chain(&he->callchain, chain, syms);
		free(syms);
70
	}
71 72

	return 0;
73 74
}

75
static int validate_chain(struct ip_callchain *chain, event_t *event)
76 77 78 79 80 81
{
	unsigned int chain_size;

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

82
	if (chain->nr*sizeof(u64) > chain_size)
83 84 85 86 87
		return -1;

	return 0;
}

88
static int process_sample_event(event_t *event, struct perf_session *session)
89
{
90
	struct sample_data data = { .period = 1, };
91
	struct addr_location al;
92

93
	event__parse_sample(event, session->sample_type, &data);
94

95
	dump_printf("(IP, %d): %d/%d: %p period: %Ld\n",
96
		event->header.misc,
97 98 99
		data.pid, data.tid,
		(void *)(long)data.ip,
		(long long)data.period);
100

101
	if (session->sample_type & PERF_SAMPLE_CALLCHAIN) {
102
		unsigned int i;
103

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

106
		if (validate_chain(data.callchain, event) < 0) {
107 108
			pr_debug("call-chain problem with event, "
				 "skipping it.\n");
109 110 111 112
			return 0;
		}

		if (dump_trace) {
113 114 115
			for (i = 0; i < data.callchain->nr; i++)
				dump_printf("..... %2d: %016Lx\n",
					    i, data.callchain->ips[i]);
116 117 118
		}
	}

119 120
	if (event__preprocess_sample(event, session, &al, NULL) < 0) {
		fprintf(stderr, "problem processing %d event, skipping it.\n",
121 122 123
			event->header.type);
		return -1;
	}
124

125
	if (al.filtered || (hide_unresolved && al.sym == NULL))
126
		return 0;
127

128
	if (perf_session__add_hist_entry(session, &al, data.callchain, data.period)) {
129
		pr_debug("problem incrementing symbol count, skipping event\n");
130
		return -1;
131
	}
132

133
	session->events_stats.total += data.period;
134 135
	return 0;
}
I
Ingo Molnar 已提交
136

137
static int process_read_event(event_t *event, struct perf_session *session __used)
138
{
139
	struct perf_event_attr *attr;
140

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

143
	if (show_threads) {
144
		const char *name = attr ? __event_name(attr->type, attr->config)
145 146 147 148 149 150 151 152
				   : "unknown";
		perf_read_values_add_value(&show_threads_values,
					   event->read.pid, event->read.tid,
					   event->read.id,
					   name,
					   event->read.value);
	}

153 154 155
	dump_printf(": %d %d %s %Lu\n", event->read.pid, event->read.tid,
		    attr ? __event_name(attr->type, attr->config) : "FAIL",
		    event->read.value);
156 157 158 159

	return 0;
}

160
static int perf_session__setup_sample_type(struct perf_session *self)
161
{
162
	if (!(self->sample_type & PERF_SAMPLE_CALLCHAIN)) {
163 164 165 166
		if (sort__has_parent) {
			fprintf(stderr, "selected --sort parent, but no"
					" callchain data. Did you call"
					" perf record without -g?\n");
167
			return -EINVAL;
168
		}
169
		if (symbol_conf.use_callchain) {
170
			fprintf(stderr, "selected -g but no callchain data."
171 172
					" Did you call perf record without"
					" -g?\n");
173
			return -1;
174
		}
175 176
	} else if (callchain_param.mode != CHAIN_NONE && !symbol_conf.use_callchain) {
			symbol_conf.use_callchain = true;
177 178 179
			if (register_callchain_param(&callchain_param) < 0) {
				fprintf(stderr, "Can't register callchain"
						" params\n");
180
				return -EINVAL;
181
			}
182 183
	}

184 185
	return 0;
}
186

187
static struct perf_event_ops event_ops = {
188 189 190 191 192 193 194
	.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,
195
};
196

197 198
static int __cmd_report(void)
{
199
	int ret = -EINVAL;
200
	struct perf_session *session;
201

202
	session = perf_session__new(input_name, O_RDONLY, force);
203 204 205
	if (session == NULL)
		return -ENOMEM;

206 207
	if (show_threads)
		perf_read_values_init(&show_threads_values);
208

209 210 211 212
	ret = perf_session__setup_sample_type(session);
	if (ret)
		goto out_delete;

213
	ret = perf_session__process_events(session, &event_ops);
214
	if (ret)
215
		goto out_delete;
216

217 218
	if (dump_trace) {
		event__print_totals();
219
		goto out_delete;
220
	}
221

222
	if (verbose > 3)
223
		perf_session__fprintf(session, stdout);
224

225
	if (verbose > 2)
226 227
		dsos__fprintf(stdout);

228
	perf_session__collapse_resort(session);
229
	perf_session__output_resort(session, session->events_stats.total);
230
	fprintf(stdout, "# Samples: %Ld\n#\n", session->events_stats.total);
231
	perf_session__fprintf_hists(session, NULL, false, stdout);
232 233 234 235
	if (sort_order == default_sort_order &&
	    parent_pattern == default_parent_pattern)
		fprintf(stdout, "#\n# (For a higher level overview, try: perf report --sort comm,dso)\n#\n");

236 237 238 239
	if (show_threads) {
		bool raw_printing_style = !strcmp(pretty_printing_style, "raw");
		perf_read_values_display(stdout, &show_threads_values,
					 raw_printing_style);
240
		perf_read_values_destroy(&show_threads_values);
241
	}
242 243
out_delete:
	perf_session__delete(session);
244
	return ret;
245 246
}

247 248 249 250
static int
parse_callchain_opt(const struct option *opt __used, const char *arg,
		    int unset __used)
{
251 252 253
	char *tok;
	char *endptr;

254
	symbol_conf.use_callchain = true;
255 256 257 258

	if (!arg)
		return 0;

259 260 261 262 263 264
	tok = strtok((char *)arg, ",");
	if (!tok)
		return -1;

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

267
	else if (!strncmp(tok, "flat", strlen(arg)))
268 269 270 271 272
		callchain_param.mode = CHAIN_FLAT;

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

273 274
	else if (!strncmp(tok, "none", strlen(arg))) {
		callchain_param.mode = CHAIN_NONE;
275
		symbol_conf.use_callchain = true;
276 277 278 279

		return 0;
	}

280 281 282
	else
		return -1;

283 284 285
	/* get the min percentage */
	tok = strtok(NULL, ",");
	if (!tok)
286
		goto setup;
287

288
	callchain_param.min_percent = strtod(tok, &endptr);
289 290 291
	if (tok == endptr)
		return -1;

292 293 294 295 296
setup:
	if (register_callchain_param(&callchain_param) < 0) {
		fprintf(stderr, "Can't register callchain params\n");
		return -1;
	}
297 298 299
	return 0;
}

300
static const char * const report_usage[] = {
301 302 303 304 305 306 307
	"perf report [<options>] <command>",
	NULL
};

static const struct option options[] = {
	OPT_STRING('i', "input", &input_name, "file",
		    "input file name"),
308 309
	OPT_BOOLEAN('v', "verbose", &verbose,
		    "be more verbose (show symbol address, etc)"),
310 311
	OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
		    "dump raw trace in ASCII"),
312 313
	OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
		   "file", "vmlinux pathname"),
314
	OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
315
	OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
316
		    "load module symbols - WARNING: use only with -k and LIVE kernel"),
317
	OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
318
		    "Show a column with the number of samples"),
319 320
	OPT_BOOLEAN('T', "threads", &show_threads,
		    "Show per-thread event counters"),
321 322
	OPT_STRING(0, "pretty", &pretty_printing_style, "key",
		   "pretty printing style key: normal raw"),
323
	OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
324
		   "sort by key(s): pid, comm, dso, symbol, parent"),
325
	OPT_BOOLEAN('P', "full-paths", &symbol_conf.full_paths,
326
		    "Don't shorten the pathnames taking into account the cwd"),
327 328
	OPT_STRING('p', "parent", &parent_pattern, "regex",
		   "regex filter to identify parent, see: '--sort parent'"),
329
	OPT_BOOLEAN('x', "exclude-other", &symbol_conf.exclude_other,
330
		    "Only display entries with parent-match"),
331
	OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
332
		     "Display callchains using output_type and min percent threshold. "
333
		     "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
334
	OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
335
		   "only consider symbols in these dsos"),
336
	OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
337
		   "only consider symbols in these comms"),
338
	OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
339
		   "only consider these symbols"),
340
	OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str,
341 342
		   "width[,width...]",
		   "don't try to adjust column width, use these fixed values"),
343
	OPT_STRING('t', "field-separator", &symbol_conf.field_sep, "separator",
344 345
		   "separator for columns, no spaces will be added between "
		   "columns '.' is reserved."),
346 347
	OPT_BOOLEAN('U', "hide-unresolved", &hide_unresolved,
		    "Only display entries resolved to a symbol"),
348 349 350
	OPT_END()
};

351
int cmd_report(int argc, const char **argv, const char *prefix __used)
352
{
353 354 355 356
	argc = parse_options(argc, argv, options, report_usage, 0);

	setup_pager();

357
	if (symbol__init() < 0)
358
		return -1;
359

360
	setup_sorting(report_usage, options);
361

362
	if (parent_pattern != default_parent_pattern) {
363
		sort_dimension__add("parent");
364 365
		sort_parent.elide = 1;
	} else
366
		symbol_conf.exclude_other = false;
367

368 369 370 371 372 373
	/*
	 * Any (unrecognized) arguments left?
	 */
	if (argc)
		usage_with_options(report_usage, options);

374 375 376
	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);
377

378 379
	return __cmd_report();
}