perf top: Fix code typo in prompt_symbol()
[linux-block.git] / tools / perf / builtin-report.c
CommitLineData
bf9e1876
IM
1/*
2 * builtin-report.c
3 *
4 * Builtin report command: Analyze the perf.data input file,
5 * look up and read DSOs and symbol information and display
6 * a histogram of results, along various sorting keys.
7 */
16f762a2 8#include "builtin.h"
53cb8bc2 9
bf9e1876
IM
10#include "util/util.h"
11
8fc0321f 12#include "util/color.h"
5da50258 13#include <linux/list.h>
a930d2c0 14#include "util/cache.h"
43cbcd8a 15#include <linux/rbtree.h>
a2928c42 16#include "util/symbol.h"
a0055ae2 17#include "util/string.h"
f55c5552 18#include "util/callchain.h"
25903407 19#include "util/strlist.h"
8d513270 20#include "util/values.h"
8fa66bdc 21
53cb8bc2 22#include "perf.h"
8f28827a 23#include "util/debug.h"
7c6a1c65 24#include "util/header.h"
94c744b6 25#include "util/session.h"
53cb8bc2
IM
26
27#include "util/parse-options.h"
28#include "util/parse-events.h"
29
6baa0a5a 30#include "util/thread.h"
dd68ada2 31#include "util/sort.h"
3d1d07ec 32#include "util/hist.h"
6baa0a5a 33
23ac9cbe 34static char const *input_name = "perf.data";
bd74137e 35
fa6963b2 36static int force;
71289be7 37static bool hide_unresolved;
b9a63b9b 38static bool dont_use_callchains;
97b07b69 39
8d513270
BG
40static int show_threads;
41static struct perf_read_values show_threads_values;
42
9f866697
BG
43static char default_pretty_printing_style[] = "normal";
44static char *pretty_printing_style = default_pretty_printing_style;
45
805d127d
FW
46static char callchain_default_opt[] = "fractal,0.5";
47
4e4f06e4
ACM
48static int perf_session__add_hist_entry(struct perf_session *self,
49 struct addr_location *al,
50 struct ip_callchain *chain, u64 count)
8fa66bdc 51{
9735abf1
ACM
52 struct symbol **syms = NULL, *parent = NULL;
53 bool hit;
e7fb08b1 54 struct hist_entry *he;
e7fb08b1 55
d599db3f 56 if ((sort__has_parent || symbol_conf.use_callchain) && chain)
a328626b
ACM
57 syms = perf_session__resolve_callchain(self, al->thread,
58 chain, &parent);
4e4f06e4 59 he = __perf_session__add_hist_entry(self, al, parent, count, &hit);
9735abf1
ACM
60 if (he == NULL)
61 return -ENOMEM;
e7fb08b1 62
9735abf1
ACM
63 if (hit)
64 he->count += count;
e7fb08b1 65
d599db3f 66 if (symbol_conf.use_callchain) {
9735abf1
ACM
67 if (!hit)
68 callchain_init(&he->callchain);
4424961a
FW
69 append_chain(&he->callchain, chain, syms);
70 free(syms);
f55c5552 71 }
e7fb08b1
PZ
72
73 return 0;
8fa66bdc
ACM
74}
75
2a0a50fe 76static int validate_chain(struct ip_callchain *chain, event_t *event)
7522060c
IM
77{
78 unsigned int chain_size;
79
7522060c
IM
80 chain_size = event->header.size;
81 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
82
9cffa8d5 83 if (chain->nr*sizeof(u64) > chain_size)
7522060c
IM
84 return -1;
85
86 return 0;
87}
88
b3165f41 89static int process_sample_event(event_t *event, struct perf_session *session)
75051724 90{
c410a338 91 struct sample_data data = { .period = 1, };
1ed091c4 92 struct addr_location al;
180f95e2 93
c019879b 94 event__parse_sample(event, session->sample_type, &data);
ea1900e5 95
62daacb5 96 dump_printf("(IP, %d): %d/%d: %p period: %Ld\n",
75051724 97 event->header.misc,
180f95e2
OH
98 data.pid, data.tid,
99 (void *)(long)data.ip,
100 (long long)data.period);
75051724 101
c019879b 102 if (session->sample_type & PERF_SAMPLE_CALLCHAIN) {
f37a291c 103 unsigned int i;
3efa1cc9 104
180f95e2 105 dump_printf("... chain: nr:%Lu\n", data.callchain->nr);
3efa1cc9 106
180f95e2 107 if (validate_chain(data.callchain, event) < 0) {
6beba7ad
ACM
108 pr_debug("call-chain problem with event, "
109 "skipping it.\n");
7522060c
IM
110 return 0;
111 }
112
113 if (dump_trace) {
180f95e2
OH
114 for (i = 0; i < data.callchain->nr; i++)
115 dump_printf("..... %2d: %016Lx\n",
116 i, data.callchain->ips[i]);
3efa1cc9
IM
117 }
118 }
119
c410a338
ACM
120 if (event__preprocess_sample(event, session, &al, NULL) < 0) {
121 fprintf(stderr, "problem processing %d event, skipping it.\n",
75051724
IM
122 event->header.type);
123 return -1;
124 }
e7fb08b1 125
71289be7 126 if (al.filtered || (hide_unresolved && al.sym == NULL))
ec218fc4 127 return 0;
7bec7a91 128
4e4f06e4 129 if (perf_session__add_hist_entry(session, &al, data.callchain, data.period)) {
6beba7ad 130 pr_debug("problem incrementing symbol count, skipping event\n");
ec218fc4 131 return -1;
8fa66bdc 132 }
ec218fc4 133
f823e441 134 session->events_stats.total += data.period;
75051724
IM
135 return 0;
136}
3502973d 137
d8f66248 138static int process_read_event(event_t *event, struct perf_session *session __used)
e9ea2fde 139{
cdd6c482 140 struct perf_event_attr *attr;
0d3a5c88 141
94c744b6 142 attr = perf_header__find_attr(event->read.id, &session->header);
8f18aec5 143
8d513270 144 if (show_threads) {
83a0944f 145 const char *name = attr ? __event_name(attr->type, attr->config)
8d513270
BG
146 : "unknown";
147 perf_read_values_add_value(&show_threads_values,
148 event->read.pid, event->read.tid,
149 event->read.id,
150 name,
151 event->read.value);
152 }
153
62daacb5
ACM
154 dump_printf(": %d %d %s %Lu\n", event->read.pid, event->read.tid,
155 attr ? __event_name(attr->type, attr->config) : "FAIL",
156 event->read.value);
e9ea2fde
PZ
157
158 return 0;
159}
160
d549c769 161static int perf_session__setup_sample_type(struct perf_session *self)
d80d338d 162{
d549c769 163 if (!(self->sample_type & PERF_SAMPLE_CALLCHAIN)) {
91b4eaea
FW
164 if (sort__has_parent) {
165 fprintf(stderr, "selected --sort parent, but no"
166 " callchain data. Did you call"
167 " perf record without -g?\n");
d549c769 168 return -EINVAL;
91b4eaea 169 }
d599db3f 170 if (symbol_conf.use_callchain) {
6ede59c4 171 fprintf(stderr, "selected -g but no callchain data."
91b4eaea
FW
172 " Did you call perf record without"
173 " -g?\n");
016e92fb 174 return -1;
91b4eaea 175 }
b9a63b9b
ACM
176 } else if (!dont_use_callchains && callchain_param.mode != CHAIN_NONE &&
177 !symbol_conf.use_callchain) {
d599db3f 178 symbol_conf.use_callchain = true;
b1a88349
FW
179 if (register_callchain_param(&callchain_param) < 0) {
180 fprintf(stderr, "Can't register callchain"
181 " params\n");
d549c769 182 return -EINVAL;
b1a88349 183 }
f5970550
PZ
184 }
185
016e92fb
FW
186 return 0;
187}
6142f9ec 188
301a0b02 189static struct perf_event_ops event_ops = {
55aa640f
ACM
190 .sample = process_sample_event,
191 .mmap = event__process_mmap,
192 .comm = event__process_comm,
193 .exit = event__process_task,
194 .fork = event__process_task,
195 .lost = event__process_lost,
196 .read = process_read_event,
016e92fb 197};
6142f9ec 198
016e92fb
FW
199static int __cmd_report(void)
200{
d549c769 201 int ret = -EINVAL;
d8f66248 202 struct perf_session *session;
8fa66bdc 203
75be6cf4 204 session = perf_session__new(input_name, O_RDONLY, force);
94c744b6
ACM
205 if (session == NULL)
206 return -ENOMEM;
207
016e92fb
FW
208 if (show_threads)
209 perf_read_values_init(&show_threads_values);
f5970550 210
d549c769
ACM
211 ret = perf_session__setup_sample_type(session);
212 if (ret)
213 goto out_delete;
214
ec913369 215 ret = perf_session__process_events(session, &event_ops);
016e92fb 216 if (ret)
94c744b6 217 goto out_delete;
97b07b69 218
62daacb5
ACM
219 if (dump_trace) {
220 event__print_totals();
94c744b6 221 goto out_delete;
62daacb5 222 }
97b07b69 223
da21d1b5 224 if (verbose > 3)
b3165f41 225 perf_session__fprintf(session, stdout);
9ac99545 226
da21d1b5 227 if (verbose > 2)
16f762a2 228 dsos__fprintf(stdout);
16f762a2 229
4e4f06e4 230 perf_session__collapse_resort(session);
f823e441 231 perf_session__output_resort(session, session->events_stats.total);
b5b60fda 232 fprintf(stdout, "# Samples: %Ld\n#\n", session->events_stats.total);
c351c281 233 perf_session__fprintf_hists(session, NULL, false, stdout);
3e6055ab
ACM
234 if (sort_order == default_sort_order &&
235 parent_pattern == default_parent_pattern)
236 fprintf(stdout, "#\n# (For a higher level overview, try: perf report --sort comm,dso)\n#\n");
237
d599db3f
ACM
238 if (show_threads) {
239 bool raw_printing_style = !strcmp(pretty_printing_style, "raw");
240 perf_read_values_display(stdout, &show_threads_values,
241 raw_printing_style);
8d513270 242 perf_read_values_destroy(&show_threads_values);
d599db3f 243 }
94c744b6
ACM
244out_delete:
245 perf_session__delete(session);
016e92fb 246 return ret;
8fa66bdc
ACM
247}
248
4eb3e478
FW
249static int
250parse_callchain_opt(const struct option *opt __used, const char *arg,
b9a63b9b 251 int unset)
4eb3e478 252{
c20ab37e
FW
253 char *tok;
254 char *endptr;
255
b9a63b9b
ACM
256 /*
257 * --no-call-graph
258 */
259 if (unset) {
260 dont_use_callchains = true;
261 return 0;
262 }
263
d599db3f 264 symbol_conf.use_callchain = true;
4eb3e478
FW
265
266 if (!arg)
267 return 0;
268
c20ab37e
FW
269 tok = strtok((char *)arg, ",");
270 if (!tok)
271 return -1;
272
273 /* get the output mode */
274 if (!strncmp(tok, "graph", strlen(arg)))
805d127d 275 callchain_param.mode = CHAIN_GRAPH_ABS;
4eb3e478 276
c20ab37e 277 else if (!strncmp(tok, "flat", strlen(arg)))
805d127d
FW
278 callchain_param.mode = CHAIN_FLAT;
279
280 else if (!strncmp(tok, "fractal", strlen(arg)))
281 callchain_param.mode = CHAIN_GRAPH_REL;
282
b1a88349
FW
283 else if (!strncmp(tok, "none", strlen(arg))) {
284 callchain_param.mode = CHAIN_NONE;
d599db3f 285 symbol_conf.use_callchain = true;
b1a88349
FW
286
287 return 0;
288 }
289
4eb3e478
FW
290 else
291 return -1;
292
c20ab37e
FW
293 /* get the min percentage */
294 tok = strtok(NULL, ",");
295 if (!tok)
805d127d 296 goto setup;
c20ab37e 297
805d127d 298 callchain_param.min_percent = strtod(tok, &endptr);
c20ab37e
FW
299 if (tok == endptr)
300 return -1;
301
805d127d
FW
302setup:
303 if (register_callchain_param(&callchain_param) < 0) {
304 fprintf(stderr, "Can't register callchain params\n");
305 return -1;
306 }
4eb3e478
FW
307 return 0;
308}
309
0422a4fc 310static const char * const report_usage[] = {
53cb8bc2
IM
311 "perf report [<options>] <command>",
312 NULL
313};
314
315static const struct option options[] = {
316 OPT_STRING('i', "input", &input_name, "file",
317 "input file name"),
815e777f
ACM
318 OPT_BOOLEAN('v', "verbose", &verbose,
319 "be more verbose (show symbol address, etc)"),
97b07b69
IM
320 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
321 "dump raw trace in ASCII"),
b32d133a
ACM
322 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
323 "file", "vmlinux pathname"),
fa6963b2 324 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
b32d133a 325 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
42976487 326 "load module symbols - WARNING: use only with -k and LIVE kernel"),
d599db3f 327 OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
e3d7e183 328 "Show a column with the number of samples"),
8d513270
BG
329 OPT_BOOLEAN('T', "threads", &show_threads,
330 "Show per-thread event counters"),
9f866697
BG
331 OPT_STRING(0, "pretty", &pretty_printing_style, "key",
332 "pretty printing style key: normal raw"),
63299f05 333 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
b25bcf2f 334 "sort by key(s): pid, comm, dso, symbol, parent"),
f7d87444 335 OPT_BOOLEAN('P', "full-paths", &symbol_conf.full_paths,
b78c07d4 336 "Don't shorten the pathnames taking into account the cwd"),
b25bcf2f
IM
337 OPT_STRING('p', "parent", &parent_pattern, "regex",
338 "regex filter to identify parent, see: '--sort parent'"),
d599db3f 339 OPT_BOOLEAN('x', "exclude-other", &symbol_conf.exclude_other,
b8e6d829 340 "Only display entries with parent-match"),
1483b19f 341 OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
c20ab37e 342 "Display callchains using output_type and min percent threshold. "
1483b19f 343 "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
655000e7 344 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
25903407 345 "only consider symbols in these dsos"),
655000e7 346 OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
cc8b88b1 347 "only consider symbols in these comms"),
655000e7 348 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
7bec7a91 349 "only consider these symbols"),
655000e7 350 OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str,
52d422de
ACM
351 "width[,width...]",
352 "don't try to adjust column width, use these fixed values"),
c410a338 353 OPT_STRING('t', "field-separator", &symbol_conf.field_sep, "separator",
52d422de
ACM
354 "separator for columns, no spaces will be added between "
355 "columns '.' is reserved."),
71289be7
ACM
356 OPT_BOOLEAN('U', "hide-unresolved", &hide_unresolved,
357 "Only display entries resolved to a symbol"),
53cb8bc2
IM
358 OPT_END()
359};
360
f37a291c 361int cmd_report(int argc, const char **argv, const char *prefix __used)
53cb8bc2 362{
655000e7
ACM
363 argc = parse_options(argc, argv, options, report_usage, 0);
364
365 setup_pager();
366
75be6cf4 367 if (symbol__init() < 0)
b32d133a 368 return -1;
53cb8bc2 369
c8829c7a 370 setup_sorting(report_usage, options);
1aa16738 371
021191b3 372 if (parent_pattern != default_parent_pattern) {
b8e6d829 373 sort_dimension__add("parent");
021191b3
ACM
374 sort_parent.elide = 1;
375 } else
d599db3f 376 symbol_conf.exclude_other = false;
b8e6d829 377
edc52dea
IM
378 /*
379 * Any (unrecognized) arguments left?
380 */
381 if (argc)
382 usage_with_options(report_usage, options);
383
655000e7
ACM
384 sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list, "dso", stdout);
385 sort_entry__setup_elide(&sort_comm, symbol_conf.comm_list, "comm", stdout);
386 sort_entry__setup_elide(&sort_sym, symbol_conf.sym_list, "symbol", stdout);
25903407 387
53cb8bc2
IM
388 return __cmd_report();
389}