builtin-report.c 10.0 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

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

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

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

46 47 48
static int perf_session__add_hist_entry(struct perf_session *self,
					struct addr_location *al,
					struct ip_callchain *chain, u64 count)
49
{
50 51
	struct symbol **syms = NULL, *parent = NULL;
	bool hit;
52 53
	struct hist_entry *he;

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

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

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

	return 0;
72 73
}

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

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

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

	return 0;
}

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

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

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

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

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

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

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

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

124
	if (al.filtered)
125
		return 0;
126

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

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

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

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

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

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

	return 0;
}

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

183 184
	return 0;
}
185

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

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

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

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

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

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

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

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

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

227
	perf_session__collapse_resort(session);
228
	perf_session__output_resort(session, session->events_stats.total);
229
	fprintf(stdout, "# Samples: %Ld\n#\n", session->events_stats.total);
230
	perf_session__fprintf_hists(session, NULL, false, stdout);
231 232 233 234
	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");

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

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

253
	symbol_conf.use_callchain = true;
254 255 256 257

	if (!arg)
		return 0;

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

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

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

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

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

		return 0;
	}

279 280 281
	else
		return -1;

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

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

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

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

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

348
int cmd_report(int argc, const char **argv, const char *prefix __used)
349
{
350 351 352 353
	argc = parse_options(argc, argv, options, report_usage, 0);

	setup_pager();

354
	if (symbol__init() < 0)
355
		return -1;
356

357
	setup_sorting(report_usage, options);
358

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

365 366 367 368 369 370
	/*
	 * Any (unrecognized) arguments left?
	 */
	if (argc)
		usage_with_options(report_usage, options);

371 372 373
	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);
374

375 376
	return __cmd_report();
}