perf annotate: Move annotate functions to util/
[deliverable/linux.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/annotate.h"
13 #include "util/color.h"
14 #include <linux/list.h>
15 #include "util/cache.h"
16 #include <linux/rbtree.h>
17 #include "util/symbol.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 bool force, use_tui, use_stdio;
37 static bool hide_unresolved;
38 static bool dont_use_callchains;
39
40 static bool show_threads;
41 static struct perf_read_values show_threads_values;
42
43 static const char default_pretty_printing_style[] = "normal";
44 static const char *pretty_printing_style = default_pretty_printing_style;
45
46 static char callchain_default_opt[] = "fractal,0.5";
47
48 static struct hists *perf_session__hists_findnew(struct perf_session *self,
49 u64 event_stream, u32 type,
50 u64 config)
51 {
52 struct rb_node **p = &self->hists_tree.rb_node;
53 struct rb_node *parent = NULL;
54 struct hists *iter, *new;
55
56 while (*p != NULL) {
57 parent = *p;
58 iter = rb_entry(parent, struct hists, rb_node);
59 if (iter->config == config)
60 return iter;
61
62
63 if (config > iter->config)
64 p = &(*p)->rb_right;
65 else
66 p = &(*p)->rb_left;
67 }
68
69 new = malloc(sizeof(struct hists));
70 if (new == NULL)
71 return NULL;
72 memset(new, 0, sizeof(struct hists));
73 new->event_stream = event_stream;
74 new->config = config;
75 new->type = type;
76 rb_link_node(&new->rb_node, parent, p);
77 rb_insert_color(&new->rb_node, &self->hists_tree);
78 return new;
79 }
80
81 static int perf_session__add_hist_entry(struct perf_session *session,
82 struct addr_location *al,
83 struct perf_sample *sample)
84 {
85 struct symbol *parent = NULL;
86 int err = 0;
87 struct hist_entry *he;
88 struct hists *hists;
89 struct perf_event_attr *attr;
90
91 if ((sort__has_parent || symbol_conf.use_callchain) && sample->callchain) {
92 err = perf_session__resolve_callchain(session, al->thread,
93 sample->callchain, &parent);
94 if (err)
95 return err;
96 }
97
98 attr = perf_header__find_attr(sample->id, &session->header);
99 if (attr)
100 hists = perf_session__hists_findnew(session, sample->id, attr->type, attr->config);
101 else
102 hists = perf_session__hists_findnew(session, sample->id, 0, 0);
103 if (hists == NULL)
104 return -ENOMEM;
105
106 he = __hists__add_entry(hists, al, parent, sample->period);
107 if (he == NULL)
108 return -ENOMEM;
109
110 if (symbol_conf.use_callchain) {
111 err = callchain_append(he->callchain, &session->callchain_cursor,
112 sample->period);
113 if (err)
114 return err;
115 }
116 /*
117 * Only in the newt browser we are doing integrated annotation,
118 * so we don't allocated the extra space needed because the stdio
119 * code will not use it.
120 */
121 if (use_browser > 0)
122 err = hist_entry__inc_addr_samples(he, al->addr);
123
124 return err;
125 }
126
127 static int add_event_total(struct perf_session *session,
128 struct perf_sample *sample,
129 struct perf_event_attr *attr)
130 {
131 struct hists *hists;
132
133 if (attr)
134 hists = perf_session__hists_findnew(session, sample->id,
135 attr->type, attr->config);
136 else
137 hists = perf_session__hists_findnew(session, sample->id, 0, 0);
138
139 if (!hists)
140 return -ENOMEM;
141
142 hists->stats.total_period += sample->period;
143 /*
144 * FIXME: add_event_total should be moved from here to
145 * perf_session__process_event so that the proper hist is passed to
146 * the event_op methods.
147 */
148 hists__inc_nr_events(hists, PERF_RECORD_SAMPLE);
149 session->hists.stats.total_period += sample->period;
150 return 0;
151 }
152
153 static int process_sample_event(union perf_event *event,
154 struct perf_sample *sample,
155 struct perf_session *session)
156 {
157 struct addr_location al;
158 struct perf_event_attr *attr;
159
160 if (perf_event__preprocess_sample(event, session, &al, sample, NULL) < 0) {
161 fprintf(stderr, "problem processing %d event, skipping it.\n",
162 event->header.type);
163 return -1;
164 }
165
166 if (al.filtered || (hide_unresolved && al.sym == NULL))
167 return 0;
168
169 if (perf_session__add_hist_entry(session, &al, sample)) {
170 pr_debug("problem incrementing symbol period, skipping event\n");
171 return -1;
172 }
173
174 attr = perf_header__find_attr(sample->id, &session->header);
175
176 if (add_event_total(session, sample, attr)) {
177 pr_debug("problem adding event period\n");
178 return -1;
179 }
180
181 return 0;
182 }
183
184 static int process_read_event(union perf_event *event,
185 struct perf_sample *sample __used,
186 struct perf_session *session __used)
187 {
188 struct perf_event_attr *attr;
189
190 attr = perf_header__find_attr(event->read.id, &session->header);
191
192 if (show_threads) {
193 const char *name = attr ? __event_name(attr->type, attr->config)
194 : "unknown";
195 perf_read_values_add_value(&show_threads_values,
196 event->read.pid, event->read.tid,
197 event->read.id,
198 name,
199 event->read.value);
200 }
201
202 dump_printf(": %d %d %s %" PRIu64 "\n", event->read.pid, event->read.tid,
203 attr ? __event_name(attr->type, attr->config) : "FAIL",
204 event->read.value);
205
206 return 0;
207 }
208
209 static int perf_session__setup_sample_type(struct perf_session *self)
210 {
211 if (!(self->sample_type & PERF_SAMPLE_CALLCHAIN)) {
212 if (sort__has_parent) {
213 fprintf(stderr, "selected --sort parent, but no"
214 " callchain data. Did you call"
215 " perf record without -g?\n");
216 return -EINVAL;
217 }
218 if (symbol_conf.use_callchain) {
219 fprintf(stderr, "selected -g but no callchain data."
220 " Did you call perf record without"
221 " -g?\n");
222 return -1;
223 }
224 } else if (!dont_use_callchains && callchain_param.mode != CHAIN_NONE &&
225 !symbol_conf.use_callchain) {
226 symbol_conf.use_callchain = true;
227 if (callchain_register_param(&callchain_param) < 0) {
228 fprintf(stderr, "Can't register callchain"
229 " params\n");
230 return -EINVAL;
231 }
232 }
233
234 return 0;
235 }
236
237 static struct perf_event_ops event_ops = {
238 .sample = process_sample_event,
239 .mmap = perf_event__process_mmap,
240 .comm = perf_event__process_comm,
241 .exit = perf_event__process_task,
242 .fork = perf_event__process_task,
243 .lost = perf_event__process_lost,
244 .read = process_read_event,
245 .attr = perf_event__process_attr,
246 .event_type = perf_event__process_event_type,
247 .tracing_data = perf_event__process_tracing_data,
248 .build_id = perf_event__process_build_id,
249 .ordered_samples = true,
250 .ordering_requires_timestamps = true,
251 };
252
253 extern volatile int session_done;
254
255 static void sig_handler(int sig __used)
256 {
257 session_done = 1;
258 }
259
260 static size_t hists__fprintf_nr_sample_events(struct hists *self,
261 const char *evname, FILE *fp)
262 {
263 size_t ret;
264 char unit;
265 unsigned long nr_events = self->stats.nr_events[PERF_RECORD_SAMPLE];
266
267 nr_events = convert_unit(nr_events, &unit);
268 ret = fprintf(fp, "# Events: %lu%c", nr_events, unit);
269 if (evname != NULL)
270 ret += fprintf(fp, " %s", evname);
271 return ret + fprintf(fp, "\n#\n");
272 }
273
274 static int hists__tty_browse_tree(struct rb_root *tree, const char *help)
275 {
276 struct rb_node *next = rb_first(tree);
277
278 while (next) {
279 struct hists *hists = rb_entry(next, struct hists, rb_node);
280 const char *evname = NULL;
281
282 if (rb_first(&hists->entries) != rb_last(&hists->entries))
283 evname = __event_name(hists->type, hists->config);
284
285 hists__fprintf_nr_sample_events(hists, evname, stdout);
286 hists__fprintf(hists, NULL, false, stdout);
287 fprintf(stdout, "\n\n");
288 next = rb_next(&hists->rb_node);
289 }
290
291 if (sort_order == default_sort_order &&
292 parent_pattern == default_parent_pattern) {
293 fprintf(stdout, "#\n# (%s)\n#\n", help);
294
295 if (show_threads) {
296 bool style = !strcmp(pretty_printing_style, "raw");
297 perf_read_values_display(stdout, &show_threads_values,
298 style);
299 perf_read_values_destroy(&show_threads_values);
300 }
301 }
302
303 return 0;
304 }
305
306 static int __cmd_report(void)
307 {
308 int ret = -EINVAL;
309 struct perf_session *session;
310 struct rb_node *next;
311 const char *help = "For a higher level overview, try: perf report --sort comm,dso";
312
313 signal(SIGINT, sig_handler);
314
315 session = perf_session__new(input_name, O_RDONLY, force, false, &event_ops);
316 if (session == NULL)
317 return -ENOMEM;
318
319 if (show_threads)
320 perf_read_values_init(&show_threads_values);
321
322 ret = perf_session__setup_sample_type(session);
323 if (ret)
324 goto out_delete;
325
326 ret = perf_session__process_events(session, &event_ops);
327 if (ret)
328 goto out_delete;
329
330 if (dump_trace) {
331 perf_session__fprintf_nr_events(session, stdout);
332 goto out_delete;
333 }
334
335 if (verbose > 3)
336 perf_session__fprintf(session, stdout);
337
338 if (verbose > 2)
339 perf_session__fprintf_dsos(session, stdout);
340
341 next = rb_first(&session->hists_tree);
342 while (next) {
343 struct hists *hists;
344
345 hists = rb_entry(next, struct hists, rb_node);
346 hists__collapse_resort(hists);
347 hists__output_resort(hists);
348 next = rb_next(&hists->rb_node);
349 }
350
351 if (use_browser > 0)
352 hists__tui_browse_tree(&session->hists_tree, help);
353 else
354 hists__tty_browse_tree(&session->hists_tree, help);
355
356 out_delete:
357 /*
358 * Speed up the exit process, for large files this can
359 * take quite a while.
360 *
361 * XXX Enable this when using valgrind or if we ever
362 * librarize this command.
363 *
364 * Also experiment with obstacks to see how much speed
365 * up we'll get here.
366 *
367 * perf_session__delete(session);
368 */
369 return ret;
370 }
371
372 static int
373 parse_callchain_opt(const struct option *opt __used, const char *arg,
374 int unset)
375 {
376 char *tok, *tok2;
377 char *endptr;
378
379 /*
380 * --no-call-graph
381 */
382 if (unset) {
383 dont_use_callchains = true;
384 return 0;
385 }
386
387 symbol_conf.use_callchain = true;
388
389 if (!arg)
390 return 0;
391
392 tok = strtok((char *)arg, ",");
393 if (!tok)
394 return -1;
395
396 /* get the output mode */
397 if (!strncmp(tok, "graph", strlen(arg)))
398 callchain_param.mode = CHAIN_GRAPH_ABS;
399
400 else if (!strncmp(tok, "flat", strlen(arg)))
401 callchain_param.mode = CHAIN_FLAT;
402
403 else if (!strncmp(tok, "fractal", strlen(arg)))
404 callchain_param.mode = CHAIN_GRAPH_REL;
405
406 else if (!strncmp(tok, "none", strlen(arg))) {
407 callchain_param.mode = CHAIN_NONE;
408 symbol_conf.use_callchain = false;
409
410 return 0;
411 }
412
413 else
414 return -1;
415
416 /* get the min percentage */
417 tok = strtok(NULL, ",");
418 if (!tok)
419 goto setup;
420
421 tok2 = strtok(NULL, ",");
422 callchain_param.min_percent = strtod(tok, &endptr);
423 if (tok == endptr)
424 return -1;
425
426 if (tok2)
427 callchain_param.print_limit = strtod(tok2, &endptr);
428 setup:
429 if (callchain_register_param(&callchain_param) < 0) {
430 fprintf(stderr, "Can't register callchain params\n");
431 return -1;
432 }
433 return 0;
434 }
435
436 static const char * const report_usage[] = {
437 "perf report [<options>] <command>",
438 NULL
439 };
440
441 static const struct option options[] = {
442 OPT_STRING('i', "input", &input_name, "file",
443 "input file name"),
444 OPT_INCR('v', "verbose", &verbose,
445 "be more verbose (show symbol address, etc)"),
446 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
447 "dump raw trace in ASCII"),
448 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
449 "file", "vmlinux pathname"),
450 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
451 "file", "kallsyms pathname"),
452 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
453 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
454 "load module symbols - WARNING: use only with -k and LIVE kernel"),
455 OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
456 "Show a column with the number of samples"),
457 OPT_BOOLEAN('T', "threads", &show_threads,
458 "Show per-thread event counters"),
459 OPT_STRING(0, "pretty", &pretty_printing_style, "key",
460 "pretty printing style key: normal raw"),
461 OPT_BOOLEAN(0, "tui", &use_tui, "Use the TUI interface"),
462 OPT_BOOLEAN(0, "stdio", &use_stdio, "Use the stdio interface"),
463 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
464 "sort by key(s): pid, comm, dso, symbol, parent"),
465 OPT_BOOLEAN(0, "showcpuutilization", &symbol_conf.show_cpu_utilization,
466 "Show sample percentage for different cpu modes"),
467 OPT_STRING('p', "parent", &parent_pattern, "regex",
468 "regex filter to identify parent, see: '--sort parent'"),
469 OPT_BOOLEAN('x', "exclude-other", &symbol_conf.exclude_other,
470 "Only display entries with parent-match"),
471 OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
472 "Display callchains using output_type (graph, flat, fractal, or none) and min percent threshold. "
473 "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
474 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
475 "only consider symbols in these dsos"),
476 OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
477 "only consider symbols in these comms"),
478 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
479 "only consider these symbols"),
480 OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str,
481 "width[,width...]",
482 "don't try to adjust column width, use these fixed values"),
483 OPT_STRING('t', "field-separator", &symbol_conf.field_sep, "separator",
484 "separator for columns, no spaces will be added between "
485 "columns '.' is reserved."),
486 OPT_BOOLEAN('U', "hide-unresolved", &hide_unresolved,
487 "Only display entries resolved to a symbol"),
488 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
489 "Look for files with symbols relative to this directory"),
490 OPT_END()
491 };
492
493 int cmd_report(int argc, const char **argv, const char *prefix __used)
494 {
495 argc = parse_options(argc, argv, options, report_usage, 0);
496
497 if (use_stdio)
498 use_browser = 0;
499 else if (use_tui)
500 use_browser = 1;
501
502 if (strcmp(input_name, "-") != 0)
503 setup_browser(true);
504 else
505 use_browser = 0;
506 /*
507 * Only in the newt browser we are doing integrated annotation,
508 * so don't allocate extra space that won't be used in the stdio
509 * implementation.
510 */
511 if (use_browser > 0) {
512 symbol_conf.priv_size = sizeof(struct annotation);
513 /*
514 * For searching by name on the "Browse map details".
515 * providing it only in verbose mode not to bloat too
516 * much struct symbol.
517 */
518 if (verbose) {
519 /*
520 * XXX: Need to provide a less kludgy way to ask for
521 * more space per symbol, the u32 is for the index on
522 * the ui browser.
523 * See symbol__browser_index.
524 */
525 symbol_conf.priv_size += sizeof(u32);
526 symbol_conf.sort_by_name = true;
527 }
528 }
529
530 if (symbol__init() < 0)
531 return -1;
532
533 setup_sorting(report_usage, options);
534
535 if (parent_pattern != default_parent_pattern) {
536 if (sort_dimension__add("parent") < 0)
537 return -1;
538 sort_parent.elide = 1;
539 } else
540 symbol_conf.exclude_other = false;
541
542 /*
543 * Any (unrecognized) arguments left?
544 */
545 if (argc)
546 usage_with_options(report_usage, options);
547
548 sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list, "dso", stdout);
549 sort_entry__setup_elide(&sort_comm, symbol_conf.comm_list, "comm", stdout);
550 sort_entry__setup_elide(&sort_sym, symbol_conf.sym_list, "symbol", stdout);
551
552 return __cmd_report();
553 }
This page took 0.050601 seconds and 5 git commands to generate.