builtin-report.c 10.1 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 sample_type_check(struct perf_session *session)
160
{
161
	if (!(session->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 -1;
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 -1;
180
			}
181 182
	}

183 184
	return 0;
}
185

186
static struct perf_event_ops event_ops = {
187
	.process_sample_event	= process_sample_event,
188
	.process_mmap_event	= event__process_mmap,
189
	.process_comm_event	= event__process_mmap,
190 191 192
	.process_exit_event	= event__process_task,
	.process_fork_event	= event__process_task,
	.process_lost_event	= event__process_lost,
193 194 195
	.process_read_event	= process_read_event,
	.sample_type_check	= sample_type_check,
};
196 197


198 199 200
static int __cmd_report(void)
{
	int ret;
201
	struct perf_session *session;
202

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

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

210
	ret = perf_session__process_events(session, &event_ops);
211
	if (ret)
212
		goto out_delete;
213

214 215
	if (dump_trace) {
		event__print_totals();
216
		goto out_delete;
217
	}
218

219
	if (verbose > 3)
220
		perf_session__fprintf(session, stdout);
221

222
	if (verbose > 2)
223 224
		dsos__fprintf(stdout);

225
	perf_session__collapse_resort(session);
226
	perf_session__output_resort(session, session->events_stats.total);
227 228 229 230 231
	perf_session__fprintf_hists(session, stdout);
	if (show_threads) {
		bool raw_printing_style = !strcmp(pretty_printing_style, "raw");
		perf_read_values_display(stdout, &show_threads_values,
					 raw_printing_style);
232
		perf_read_values_destroy(&show_threads_values);
233
	}
234 235
out_delete:
	perf_session__delete(session);
236
	return ret;
237 238
}

239 240 241 242
static int
parse_callchain_opt(const struct option *opt __used, const char *arg,
		    int unset __used)
{
243 244 245
	char *tok;
	char *endptr;

246
	symbol_conf.use_callchain = true;
247 248 249 250

	if (!arg)
		return 0;

251 252 253 254 255 256
	tok = strtok((char *)arg, ",");
	if (!tok)
		return -1;

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

259
	else if (!strncmp(tok, "flat", strlen(arg)))
260 261 262 263 264
		callchain_param.mode = CHAIN_FLAT;

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

265 266
	else if (!strncmp(tok, "none", strlen(arg))) {
		callchain_param.mode = CHAIN_NONE;
267
		symbol_conf.use_callchain = true;
268 269 270 271

		return 0;
	}

272 273 274
	else
		return -1;

275 276 277
	/* get the min percentage */
	tok = strtok(NULL, ",");
	if (!tok)
278
		goto setup;
279

280
	callchain_param.min_percent = strtod(tok, &endptr);
281 282 283
	if (tok == endptr)
		return -1;

284 285 286 287 288
setup:
	if (register_callchain_param(&callchain_param) < 0) {
		fprintf(stderr, "Can't register callchain params\n");
		return -1;
	}
289 290 291
	return 0;
}

292 293
//static const char * const report_usage[] = {
const char * const report_usage[] = {
294 295 296 297 298 299 300
	"perf report [<options>] <command>",
	NULL
};

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

342 343 344
static void sort_entry__setup_elide(struct sort_entry *self,
				    struct strlist *list,
				    const char *list_name, FILE *fp)
345
{
346 347 348
	if (list && strlist__nr_entries(list) == 1) {
		fprintf(fp, "# %s: %s\n", list_name, strlist__entry(list, 0)->s);
		self->elide = true;
349 350 351
	}
}

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

	setup_pager();

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

361
	setup_sorting(report_usage, options);
362

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

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

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

379 380
	return __cmd_report();
}