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