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