perf kmem: Fixup the symbol address before using it
[deliverable/linux.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
cbbc79a5
EM
48static struct event_stat_id *get_stats(struct perf_session *self,
49 u64 event_stream, u32 type, u64 config)
50{
51 struct rb_node **p = &self->stats_by_id.rb_node;
52 struct rb_node *parent = NULL;
53 struct event_stat_id *iter, *new;
54
55 while (*p != NULL) {
56 parent = *p;
57 iter = rb_entry(parent, struct event_stat_id, rb_node);
58 if (iter->config == config)
59 return iter;
60
61
62 if (config > iter->config)
63 p = &(*p)->rb_right;
64 else
65 p = &(*p)->rb_left;
66 }
67
68 new = malloc(sizeof(struct event_stat_id));
69 if (new == NULL)
70 return NULL;
71 memset(new, 0, sizeof(struct event_stat_id));
72 new->event_stream = event_stream;
73 new->config = config;
74 new->type = type;
75 rb_link_node(&new->rb_node, parent, p);
76 rb_insert_color(&new->rb_node, &self->stats_by_id);
77 return new;
78}
79
4e4f06e4
ACM
80static int perf_session__add_hist_entry(struct perf_session *self,
81 struct addr_location *al,
cbbc79a5 82 struct sample_data *data)
8fa66bdc 83{
b3c9ac08
ACM
84 struct map_symbol *syms = NULL;
85 struct symbol *parent = NULL;
9735abf1 86 bool hit;
301fde27 87 int err;
e7fb08b1 88 struct hist_entry *he;
cbbc79a5
EM
89 struct event_stat_id *stats;
90 struct perf_event_attr *attr;
e7fb08b1 91
cbbc79a5 92 if ((sort__has_parent || symbol_conf.use_callchain) && data->callchain)
a328626b 93 syms = perf_session__resolve_callchain(self, al->thread,
cbbc79a5
EM
94 data->callchain, &parent);
95
96 attr = perf_header__find_attr(data->id, &self->header);
97 if (attr)
98 stats = get_stats(self, data->id, attr->type, attr->config);
99 else
100 stats = get_stats(self, data->id, 0, 0);
101 if (stats == NULL)
102 return -ENOMEM;
103 he = __perf_session__add_hist_entry(&stats->hists, al, parent,
104 data->period, &hit);
9735abf1
ACM
105 if (he == NULL)
106 return -ENOMEM;
e7fb08b1 107
9735abf1 108 if (hit)
cbbc79a5 109 he->count += data->period;
e7fb08b1 110
d599db3f 111 if (symbol_conf.use_callchain) {
9735abf1
ACM
112 if (!hit)
113 callchain_init(&he->callchain);
301fde27 114 err = append_chain(&he->callchain, data->callchain, syms);
4424961a 115 free(syms);
301fde27
FW
116
117 if (err)
118 return err;
f55c5552 119 }
e7fb08b1
PZ
120
121 return 0;
8fa66bdc
ACM
122}
123
2a0a50fe 124static int validate_chain(struct ip_callchain *chain, event_t *event)
7522060c
IM
125{
126 unsigned int chain_size;
127
7522060c
IM
128 chain_size = event->header.size;
129 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
130
9cffa8d5 131 if (chain->nr*sizeof(u64) > chain_size)
7522060c
IM
132 return -1;
133
134 return 0;
135}
136
cbbc79a5
EM
137static int add_event_total(struct perf_session *session,
138 struct sample_data *data,
139 struct perf_event_attr *attr)
140{
141 struct event_stat_id *stats;
142
143 if (attr)
144 stats = get_stats(session, data->id, attr->type, attr->config);
145 else
146 stats = get_stats(session, data->id, 0, 0);
147
148 if (!stats)
149 return -ENOMEM;
150
151 stats->stats.total += data->period;
152 session->events_stats.total += data->period;
153 return 0;
154}
155
b3165f41 156static int process_sample_event(event_t *event, struct perf_session *session)
75051724 157{
c410a338 158 struct sample_data data = { .period = 1, };
1ed091c4 159 struct addr_location al;
cbbc79a5 160 struct perf_event_attr *attr;
180f95e2 161
c019879b 162 event__parse_sample(event, session->sample_type, &data);
ea1900e5 163
0d755034
ACM
164 dump_printf("(IP, %d): %d/%d: %#Lx period: %Ld\n", event->header.misc,
165 data.pid, data.tid, data.ip, data.period);
75051724 166
c019879b 167 if (session->sample_type & PERF_SAMPLE_CALLCHAIN) {
f37a291c 168 unsigned int i;
3efa1cc9 169
180f95e2 170 dump_printf("... chain: nr:%Lu\n", data.callchain->nr);
3efa1cc9 171
180f95e2 172 if (validate_chain(data.callchain, event) < 0) {
6beba7ad
ACM
173 pr_debug("call-chain problem with event, "
174 "skipping it.\n");
7522060c
IM
175 return 0;
176 }
177
178 if (dump_trace) {
180f95e2
OH
179 for (i = 0; i < data.callchain->nr; i++)
180 dump_printf("..... %2d: %016Lx\n",
181 i, data.callchain->ips[i]);
3efa1cc9
IM
182 }
183 }
184
c410a338
ACM
185 if (event__preprocess_sample(event, session, &al, NULL) < 0) {
186 fprintf(stderr, "problem processing %d event, skipping it.\n",
75051724
IM
187 event->header.type);
188 return -1;
189 }
e7fb08b1 190
71289be7 191 if (al.filtered || (hide_unresolved && al.sym == NULL))
ec218fc4 192 return 0;
7bec7a91 193
cbbc79a5 194 if (perf_session__add_hist_entry(session, &al, &data)) {
6beba7ad 195 pr_debug("problem incrementing symbol count, skipping event\n");
ec218fc4 196 return -1;
8fa66bdc 197 }
ec218fc4 198
cbbc79a5
EM
199 attr = perf_header__find_attr(data.id, &session->header);
200
201 if (add_event_total(session, &data, attr)) {
202 pr_debug("problem adding event count\n");
203 return -1;
204 }
205
75051724
IM
206 return 0;
207}
3502973d 208
d8f66248 209static int process_read_event(event_t *event, struct perf_session *session __used)
e9ea2fde 210{
cdd6c482 211 struct perf_event_attr *attr;
0d3a5c88 212
94c744b6 213 attr = perf_header__find_attr(event->read.id, &session->header);
8f18aec5 214
8d513270 215 if (show_threads) {
83a0944f 216 const char *name = attr ? __event_name(attr->type, attr->config)
8d513270
BG
217 : "unknown";
218 perf_read_values_add_value(&show_threads_values,
219 event->read.pid, event->read.tid,
220 event->read.id,
221 name,
222 event->read.value);
223 }
224
62daacb5
ACM
225 dump_printf(": %d %d %s %Lu\n", event->read.pid, event->read.tid,
226 attr ? __event_name(attr->type, attr->config) : "FAIL",
227 event->read.value);
e9ea2fde
PZ
228
229 return 0;
230}
231
d549c769 232static int perf_session__setup_sample_type(struct perf_session *self)
d80d338d 233{
d549c769 234 if (!(self->sample_type & PERF_SAMPLE_CALLCHAIN)) {
91b4eaea
FW
235 if (sort__has_parent) {
236 fprintf(stderr, "selected --sort parent, but no"
237 " callchain data. Did you call"
238 " perf record without -g?\n");
d549c769 239 return -EINVAL;
91b4eaea 240 }
d599db3f 241 if (symbol_conf.use_callchain) {
6ede59c4 242 fprintf(stderr, "selected -g but no callchain data."
91b4eaea
FW
243 " Did you call perf record without"
244 " -g?\n");
016e92fb 245 return -1;
91b4eaea 246 }
b9a63b9b
ACM
247 } else if (!dont_use_callchains && callchain_param.mode != CHAIN_NONE &&
248 !symbol_conf.use_callchain) {
d599db3f 249 symbol_conf.use_callchain = true;
b1a88349
FW
250 if (register_callchain_param(&callchain_param) < 0) {
251 fprintf(stderr, "Can't register callchain"
252 " params\n");
d549c769 253 return -EINVAL;
b1a88349 254 }
f5970550
PZ
255 }
256
016e92fb
FW
257 return 0;
258}
6142f9ec 259
301a0b02 260static struct perf_event_ops event_ops = {
55aa640f
ACM
261 .sample = process_sample_event,
262 .mmap = event__process_mmap,
263 .comm = event__process_comm,
264 .exit = event__process_task,
265 .fork = event__process_task,
266 .lost = event__process_lost,
267 .read = process_read_event,
016e92fb 268};
6142f9ec 269
016e92fb
FW
270static int __cmd_report(void)
271{
d549c769 272 int ret = -EINVAL;
d8f66248 273 struct perf_session *session;
cbbc79a5 274 struct rb_node *next;
f9224c5c 275 const char *help = "For a higher level overview, try: perf report --sort comm,dso";
8fa66bdc 276
75be6cf4 277 session = perf_session__new(input_name, O_RDONLY, force);
94c744b6
ACM
278 if (session == NULL)
279 return -ENOMEM;
280
016e92fb
FW
281 if (show_threads)
282 perf_read_values_init(&show_threads_values);
f5970550 283
d549c769
ACM
284 ret = perf_session__setup_sample_type(session);
285 if (ret)
286 goto out_delete;
287
ec913369 288 ret = perf_session__process_events(session, &event_ops);
016e92fb 289 if (ret)
94c744b6 290 goto out_delete;
97b07b69 291
62daacb5
ACM
292 if (dump_trace) {
293 event__print_totals();
94c744b6 294 goto out_delete;
62daacb5 295 }
97b07b69 296
da21d1b5 297 if (verbose > 3)
b3165f41 298 perf_session__fprintf(session, stdout);
9ac99545 299
da21d1b5 300 if (verbose > 2)
16f762a2 301 dsos__fprintf(stdout);
16f762a2 302
cbbc79a5
EM
303 next = rb_first(&session->stats_by_id);
304 while (next) {
305 struct event_stat_id *stats;
5f4d3f88 306 u64 nr_hists;
cbbc79a5
EM
307
308 stats = rb_entry(next, struct event_stat_id, rb_node);
309 perf_session__collapse_resort(&stats->hists);
5f4d3f88
ACM
310 nr_hists = perf_session__output_resort(&stats->hists,
311 stats->stats.total);
f9224c5c 312 if (use_browser)
5f4d3f88 313 perf_session__browse_hists(&stats->hists, nr_hists,
f9224c5c
ACM
314 stats->stats.total, help);
315 else {
316 if (rb_first(&session->stats_by_id) ==
317 rb_last(&session->stats_by_id))
318 fprintf(stdout, "# Samples: %Ld\n#\n",
319 stats->stats.total);
320 else
321 fprintf(stdout, "# Samples: %Ld %s\n#\n",
322 stats->stats.total,
323 __event_name(stats->type, stats->config));
324
325 perf_session__fprintf_hists(&stats->hists, NULL, false, stdout,
cbbc79a5 326 stats->stats.total);
f9224c5c
ACM
327 fprintf(stdout, "\n\n");
328 }
329
cbbc79a5
EM
330 next = rb_next(&stats->rb_node);
331 }
332
f9224c5c
ACM
333 if (!use_browser && sort_order == default_sort_order &&
334 parent_pattern == default_parent_pattern) {
335 fprintf(stdout, "#\n# (%s)\n#\n", help);
3e6055ab 336
f9224c5c
ACM
337 if (show_threads) {
338 bool style = !strcmp(pretty_printing_style, "raw");
339 perf_read_values_display(stdout, &show_threads_values,
340 style);
341 perf_read_values_destroy(&show_threads_values);
342 }
d599db3f 343 }
94c744b6
ACM
344out_delete:
345 perf_session__delete(session);
016e92fb 346 return ret;
8fa66bdc
ACM
347}
348
4eb3e478
FW
349static int
350parse_callchain_opt(const struct option *opt __used, const char *arg,
b9a63b9b 351 int unset)
4eb3e478 352{
c20ab37e
FW
353 char *tok;
354 char *endptr;
355
b9a63b9b
ACM
356 /*
357 * --no-call-graph
358 */
359 if (unset) {
360 dont_use_callchains = true;
361 return 0;
362 }
363
d599db3f 364 symbol_conf.use_callchain = true;
4eb3e478
FW
365
366 if (!arg)
367 return 0;
368
c20ab37e
FW
369 tok = strtok((char *)arg, ",");
370 if (!tok)
371 return -1;
372
373 /* get the output mode */
374 if (!strncmp(tok, "graph", strlen(arg)))
805d127d 375 callchain_param.mode = CHAIN_GRAPH_ABS;
4eb3e478 376
c20ab37e 377 else if (!strncmp(tok, "flat", strlen(arg)))
805d127d
FW
378 callchain_param.mode = CHAIN_FLAT;
379
380 else if (!strncmp(tok, "fractal", strlen(arg)))
381 callchain_param.mode = CHAIN_GRAPH_REL;
382
b1a88349
FW
383 else if (!strncmp(tok, "none", strlen(arg))) {
384 callchain_param.mode = CHAIN_NONE;
b7ae11b3 385 symbol_conf.use_callchain = false;
b1a88349
FW
386
387 return 0;
388 }
389
4eb3e478
FW
390 else
391 return -1;
392
c20ab37e
FW
393 /* get the min percentage */
394 tok = strtok(NULL, ",");
395 if (!tok)
805d127d 396 goto setup;
c20ab37e 397
805d127d 398 callchain_param.min_percent = strtod(tok, &endptr);
c20ab37e
FW
399 if (tok == endptr)
400 return -1;
401
805d127d
FW
402setup:
403 if (register_callchain_param(&callchain_param) < 0) {
404 fprintf(stderr, "Can't register callchain params\n");
405 return -1;
406 }
4eb3e478
FW
407 return 0;
408}
409
0422a4fc 410static const char * const report_usage[] = {
53cb8bc2
IM
411 "perf report [<options>] <command>",
412 NULL
413};
414
415static const struct option options[] = {
416 OPT_STRING('i', "input", &input_name, "file",
417 "input file name"),
815e777f
ACM
418 OPT_BOOLEAN('v', "verbose", &verbose,
419 "be more verbose (show symbol address, etc)"),
97b07b69
IM
420 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
421 "dump raw trace in ASCII"),
b32d133a
ACM
422 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
423 "file", "vmlinux pathname"),
fa6963b2 424 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
b32d133a 425 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
42976487 426 "load module symbols - WARNING: use only with -k and LIVE kernel"),
d599db3f 427 OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
e3d7e183 428 "Show a column with the number of samples"),
8d513270
BG
429 OPT_BOOLEAN('T', "threads", &show_threads,
430 "Show per-thread event counters"),
9f866697
BG
431 OPT_STRING(0, "pretty", &pretty_printing_style, "key",
432 "pretty printing style key: normal raw"),
63299f05 433 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
b25bcf2f 434 "sort by key(s): pid, comm, dso, symbol, parent"),
f7d87444 435 OPT_BOOLEAN('P', "full-paths", &symbol_conf.full_paths,
b78c07d4 436 "Don't shorten the pathnames taking into account the cwd"),
b25bcf2f
IM
437 OPT_STRING('p', "parent", &parent_pattern, "regex",
438 "regex filter to identify parent, see: '--sort parent'"),
d599db3f 439 OPT_BOOLEAN('x', "exclude-other", &symbol_conf.exclude_other,
b8e6d829 440 "Only display entries with parent-match"),
1483b19f 441 OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
c20ab37e 442 "Display callchains using output_type and min percent threshold. "
1483b19f 443 "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
655000e7 444 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
25903407 445 "only consider symbols in these dsos"),
655000e7 446 OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
cc8b88b1 447 "only consider symbols in these comms"),
655000e7 448 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
7bec7a91 449 "only consider these symbols"),
655000e7 450 OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str,
52d422de
ACM
451 "width[,width...]",
452 "don't try to adjust column width, use these fixed values"),
c410a338 453 OPT_STRING('t', "field-separator", &symbol_conf.field_sep, "separator",
52d422de
ACM
454 "separator for columns, no spaces will be added between "
455 "columns '.' is reserved."),
71289be7
ACM
456 OPT_BOOLEAN('U', "hide-unresolved", &hide_unresolved,
457 "Only display entries resolved to a symbol"),
53cb8bc2
IM
458 OPT_END()
459};
460
f37a291c 461int cmd_report(int argc, const char **argv, const char *prefix __used)
53cb8bc2 462{
655000e7
ACM
463 argc = parse_options(argc, argv, options, report_usage, 0);
464
f9224c5c 465 setup_browser();
655000e7 466
75be6cf4 467 if (symbol__init() < 0)
b32d133a 468 return -1;
53cb8bc2 469
c8829c7a 470 setup_sorting(report_usage, options);
1aa16738 471
021191b3 472 if (parent_pattern != default_parent_pattern) {
b8e6d829 473 sort_dimension__add("parent");
021191b3
ACM
474 sort_parent.elide = 1;
475 } else
d599db3f 476 symbol_conf.exclude_other = false;
b8e6d829 477
edc52dea
IM
478 /*
479 * Any (unrecognized) arguments left?
480 */
481 if (argc)
482 usage_with_options(report_usage, options);
483
655000e7
ACM
484 sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list, "dso", stdout);
485 sort_entry__setup_elide(&sort_comm, symbol_conf.comm_list, "comm", stdout);
486 sort_entry__setup_elide(&sort_sym, symbol_conf.sym_list, "symbol", stdout);
25903407 487
53cb8bc2
IM
488 return __cmd_report();
489}
This page took 0.122329 seconds and 5 git commands to generate.