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