642a6d8eb5dca87cceab4f35a3ad32c073df2496
[safe/jmp/linux-2.6] / 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/callchain.h"
18 #include "util/strlist.h"
19 #include "util/values.h"
20
21 #include "perf.h"
22 #include "util/debug.h"
23 #include "util/header.h"
24 #include "util/session.h"
25
26 #include "util/parse-options.h"
27 #include "util/parse-events.h"
28
29 #include "util/thread.h"
30 #include "util/sort.h"
31 #include "util/hist.h"
32
33 static char             const *input_name = "perf.data";
34
35 static bool             force;
36 static bool             hide_unresolved;
37 static bool             dont_use_callchains;
38
39 static bool             show_threads;
40 static struct perf_read_values  show_threads_values;
41
42 static char             default_pretty_printing_style[] = "normal";
43 static char             *pretty_printing_style = default_pretty_printing_style;
44
45 static char             callchain_default_opt[] = "fractal,0.5";
46
47 static struct event_stat_id *get_stats(struct perf_session *self,
48                                        u64 event_stream, u32 type, u64 config)
49 {
50         struct rb_node **p = &self->stats_by_id.rb_node;
51         struct rb_node *parent = NULL;
52         struct event_stat_id *iter, *new;
53
54         while (*p != NULL) {
55                 parent = *p;
56                 iter = rb_entry(parent, struct event_stat_id, rb_node);
57                 if (iter->config == config)
58                         return iter;
59
60
61                 if (config > iter->config)
62                         p = &(*p)->rb_right;
63                 else
64                         p = &(*p)->rb_left;
65         }
66
67         new = malloc(sizeof(struct event_stat_id));
68         if (new == NULL)
69                 return NULL;
70         memset(new, 0, sizeof(struct event_stat_id));
71         new->event_stream = event_stream;
72         new->config = config;
73         new->type = type;
74         rb_link_node(&new->rb_node, parent, p);
75         rb_insert_color(&new->rb_node, &self->stats_by_id);
76         return new;
77 }
78
79 static int perf_session__add_hist_entry(struct perf_session *self,
80                                         struct addr_location *al,
81                                         struct sample_data *data)
82 {
83         struct map_symbol *syms = NULL;
84         struct symbol *parent = NULL;
85         int err = -ENOMEM;
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                 if (syms == NULL)
94                         return -ENOMEM;
95         }
96
97         attr = perf_header__find_attr(data->id, &self->header);
98         if (attr)
99                 stats = get_stats(self, data->id, attr->type, attr->config);
100         else
101                 stats = get_stats(self, data->id, 0, 0);
102         if (stats == NULL)
103                 goto out_free_syms;
104         he = __perf_session__add_hist_entry(&stats->hists, al, parent,
105                                             data->period);
106         if (he == NULL)
107                 goto out_free_syms;
108         err = 0;
109         if (symbol_conf.use_callchain)
110                 err = append_chain(he->callchain, data->callchain, syms);
111 out_free_syms:
112         free(syms);
113         return err;
114 }
115
116 static int add_event_total(struct perf_session *session,
117                            struct sample_data *data,
118                            struct perf_event_attr *attr)
119 {
120         struct event_stat_id *stats;
121
122         if (attr)
123                 stats = get_stats(session, data->id, attr->type, attr->config);
124         else
125                 stats = get_stats(session, data->id, 0, 0);
126
127         if (!stats)
128                 return -ENOMEM;
129
130         stats->stats.total += data->period;
131         session->events_stats.total += data->period;
132         return 0;
133 }
134
135 static int process_sample_event(event_t *event, struct perf_session *session)
136 {
137         struct sample_data data = { .period = 1, };
138         struct addr_location al;
139         struct perf_event_attr *attr;
140
141         event__parse_sample(event, session->sample_type, &data);
142
143         dump_printf("(IP, %d): %d/%d: %#Lx period: %Ld\n", event->header.misc,
144                     data.pid, data.tid, data.ip, data.period);
145
146         if (session->sample_type & PERF_SAMPLE_CALLCHAIN) {
147                 unsigned int i;
148
149                 dump_printf("... chain: nr:%Lu\n", data.callchain->nr);
150
151                 if (!ip_callchain__valid(data.callchain, event)) {
152                         pr_debug("call-chain problem with event, "
153                                  "skipping it.\n");
154                         return 0;
155                 }
156
157                 if (dump_trace) {
158                         for (i = 0; i < data.callchain->nr; i++)
159                                 dump_printf("..... %2d: %016Lx\n",
160                                             i, data.callchain->ips[i]);
161                 }
162         }
163
164         if (event__preprocess_sample(event, session, &al, NULL) < 0) {
165                 fprintf(stderr, "problem processing %d event, skipping it.\n",
166                         event->header.type);
167                 return -1;
168         }
169
170         if (al.filtered || (hide_unresolved && al.sym == NULL))
171                 return 0;
172
173         if (perf_session__add_hist_entry(session, &al, &data)) {
174                 pr_debug("problem incrementing symbol count, skipping event\n");
175                 return -1;
176         }
177
178         attr = perf_header__find_attr(data.id, &session->header);
179
180         if (add_event_total(session, &data, attr)) {
181                 pr_debug("problem adding event count\n");
182                 return -1;
183         }
184
185         return 0;
186 }
187
188 static int process_read_event(event_t *event, struct perf_session *session __used)
189 {
190         struct perf_event_attr *attr;
191
192         attr = perf_header__find_attr(event->read.id, &session->header);
193
194         if (show_threads) {
195                 const char *name = attr ? __event_name(attr->type, attr->config)
196                                    : "unknown";
197                 perf_read_values_add_value(&show_threads_values,
198                                            event->read.pid, event->read.tid,
199                                            event->read.id,
200                                            name,
201                                            event->read.value);
202         }
203
204         dump_printf(": %d %d %s %Lu\n", event->read.pid, event->read.tid,
205                     attr ? __event_name(attr->type, attr->config) : "FAIL",
206                     event->read.value);
207
208         return 0;
209 }
210
211 static int perf_session__setup_sample_type(struct perf_session *self)
212 {
213         if (!(self->sample_type & PERF_SAMPLE_CALLCHAIN)) {
214                 if (sort__has_parent) {
215                         fprintf(stderr, "selected --sort parent, but no"
216                                         " callchain data. Did you call"
217                                         " perf record without -g?\n");
218                         return -EINVAL;
219                 }
220                 if (symbol_conf.use_callchain) {
221                         fprintf(stderr, "selected -g but no callchain data."
222                                         " Did you call perf record without"
223                                         " -g?\n");
224                         return -1;
225                 }
226         } else if (!dont_use_callchains && callchain_param.mode != CHAIN_NONE &&
227                    !symbol_conf.use_callchain) {
228                         symbol_conf.use_callchain = true;
229                         if (register_callchain_param(&callchain_param) < 0) {
230                                 fprintf(stderr, "Can't register callchain"
231                                                 " params\n");
232                                 return -EINVAL;
233                         }
234         }
235
236         return 0;
237 }
238
239 static struct perf_event_ops event_ops = {
240         .sample = process_sample_event,
241         .mmap   = event__process_mmap,
242         .comm   = event__process_comm,
243         .exit   = event__process_task,
244         .fork   = event__process_task,
245         .lost   = event__process_lost,
246         .read   = process_read_event,
247         .attr   = event__process_attr,
248         .event_type = event__process_event_type,
249         .tracing_data = event__process_tracing_data,
250         .build_id = event__process_build_id,
251 };
252
253 extern volatile int session_done;
254
255 static void sig_handler(int sig __attribute__((__unused__)))
256 {
257         session_done = 1;
258 }
259
260 static int __cmd_report(void)
261 {
262         int ret = -EINVAL;
263         struct perf_session *session;
264         struct rb_node *next;
265         const char *help = "For a higher level overview, try: perf report --sort comm,dso";
266
267         signal(SIGINT, sig_handler);
268
269         session = perf_session__new(input_name, O_RDONLY, force, false);
270         if (session == NULL)
271                 return -ENOMEM;
272
273         if (show_threads)
274                 perf_read_values_init(&show_threads_values);
275
276         ret = perf_session__setup_sample_type(session);
277         if (ret)
278                 goto out_delete;
279
280         ret = perf_session__process_events(session, &event_ops);
281         if (ret)
282                 goto out_delete;
283
284         if (dump_trace) {
285                 event__print_totals();
286                 goto out_delete;
287         }
288
289         if (verbose > 3)
290                 perf_session__fprintf(session, stdout);
291
292         if (verbose > 2)
293                 perf_session__fprintf_dsos(session, stdout);
294
295         next = rb_first(&session->stats_by_id);
296         while (next) {
297                 struct event_stat_id *stats;
298                 u64 nr_hists;
299
300                 stats = rb_entry(next, struct event_stat_id, rb_node);
301                 perf_session__collapse_resort(&stats->hists);
302                 nr_hists = perf_session__output_resort(&stats->hists,
303                                                        stats->stats.total);
304                 if (use_browser)
305                         perf_session__browse_hists(&stats->hists, nr_hists,
306                                                    stats->stats.total, help,
307                                                    input_name);
308                 else {
309                         if (rb_first(&session->stats_by_id) ==
310                             rb_last(&session->stats_by_id))
311                                 fprintf(stdout, "# Samples: %Ld\n#\n",
312                                         stats->stats.total);
313                         else
314                                 fprintf(stdout, "# Samples: %Ld %s\n#\n",
315                                         stats->stats.total,
316                                         __event_name(stats->type, stats->config));
317
318                         perf_session__fprintf_hists(&stats->hists, NULL, false, stdout,
319                                             stats->stats.total);
320                         fprintf(stdout, "\n\n");
321                 }
322
323                 next = rb_next(&stats->rb_node);
324         }
325
326         if (!use_browser && sort_order == default_sort_order &&
327             parent_pattern == default_parent_pattern) {
328                 fprintf(stdout, "#\n# (%s)\n#\n", help);
329
330                 if (show_threads) {
331                         bool style = !strcmp(pretty_printing_style, "raw");
332                         perf_read_values_display(stdout, &show_threads_values,
333                                                  style);
334                         perf_read_values_destroy(&show_threads_values);
335                 }
336         }
337 out_delete:
338         perf_session__delete(session);
339         return ret;
340 }
341
342 static int
343 parse_callchain_opt(const struct option *opt __used, const char *arg,
344                     int unset)
345 {
346         char *tok, *tok2;
347         char *endptr;
348
349         /*
350          * --no-call-graph
351          */
352         if (unset) {
353                 dont_use_callchains = true;
354                 return 0;
355         }
356
357         symbol_conf.use_callchain = true;
358
359         if (!arg)
360                 return 0;
361
362         tok = strtok((char *)arg, ",");
363         if (!tok)
364                 return -1;
365
366         /* get the output mode */
367         if (!strncmp(tok, "graph", strlen(arg)))
368                 callchain_param.mode = CHAIN_GRAPH_ABS;
369
370         else if (!strncmp(tok, "flat", strlen(arg)))
371                 callchain_param.mode = CHAIN_FLAT;
372
373         else if (!strncmp(tok, "fractal", strlen(arg)))
374                 callchain_param.mode = CHAIN_GRAPH_REL;
375
376         else if (!strncmp(tok, "none", strlen(arg))) {
377                 callchain_param.mode = CHAIN_NONE;
378                 symbol_conf.use_callchain = false;
379
380                 return 0;
381         }
382
383         else
384                 return -1;
385
386         /* get the min percentage */
387         tok = strtok(NULL, ",");
388         if (!tok)
389                 goto setup;
390
391         tok2 = strtok(NULL, ",");
392         callchain_param.min_percent = strtod(tok, &endptr);
393         if (tok == endptr)
394                 return -1;
395
396         if (tok2)
397                 callchain_param.print_limit = strtod(tok2, &endptr);
398 setup:
399         if (register_callchain_param(&callchain_param) < 0) {
400                 fprintf(stderr, "Can't register callchain params\n");
401                 return -1;
402         }
403         return 0;
404 }
405
406 static const char * const report_usage[] = {
407         "perf report [<options>] <command>",
408         NULL
409 };
410
411 static const struct option options[] = {
412         OPT_STRING('i', "input", &input_name, "file",
413                     "input file name"),
414         OPT_INCR('v', "verbose", &verbose,
415                     "be more verbose (show symbol address, etc)"),
416         OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
417                     "dump raw trace in ASCII"),
418         OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
419                    "file", "vmlinux pathname"),
420         OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
421         OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
422                     "load module symbols - WARNING: use only with -k and LIVE kernel"),
423         OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
424                     "Show a column with the number of samples"),
425         OPT_BOOLEAN('T', "threads", &show_threads,
426                     "Show per-thread event counters"),
427         OPT_STRING(0, "pretty", &pretty_printing_style, "key",
428                    "pretty printing style key: normal raw"),
429         OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
430                    "sort by key(s): pid, comm, dso, symbol, parent"),
431         OPT_BOOLEAN('P', "full-paths", &symbol_conf.full_paths,
432                     "Don't shorten the pathnames taking into account the cwd"),
433         OPT_BOOLEAN(0, "showcpuutilization", &symbol_conf.show_cpu_utilization,
434                     "Show sample percentage for different cpu modes"),
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 (graph, flat, fractal, or none) 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         if (strcmp(input_name, "-") != 0)
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                 if (sort_dimension__add("parent") < 0)
473                         return -1;
474                 sort_parent.elide = 1;
475         } else
476                 symbol_conf.exclude_other = false;
477
478         /*
479          * Any (unrecognized) arguments left?
480          */
481         if (argc)
482                 usage_with_options(report_usage, options);
483
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);
487
488         return __cmd_report();
489 }