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