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.
10 #include "util/util.h"
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"
23 #include "util/debug.h"
24 #include "util/header.h"
25 #include "util/session.h"
27 #include "util/parse-options.h"
28 #include "util/parse-events.h"
30 #include "util/thread.h"
31 #include "util/sort.h"
32 #include "util/hist.h"
34 static char const *input_name = "perf.data";
38 static int show_threads;
39 static struct perf_read_values show_threads_values;
41 static char default_pretty_printing_style[] = "normal";
42 static char *pretty_printing_style = default_pretty_printing_style;
44 static char callchain_default_opt[] = "fractal,0.5";
47 callchain__fprintf_left_margin(FILE *fp, int left_margin)
52 ret = fprintf(fp, " ");
54 for (i = 0; i < left_margin; i++)
55 ret += fprintf(fp, " ");
60 static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
66 ret += callchain__fprintf_left_margin(fp, left_margin);
68 for (i = 0; i < depth; i++)
69 if (depth_mask & (1 << i))
70 ret += fprintf(fp, "| ");
72 ret += fprintf(fp, " ");
74 ret += fprintf(fp, "\n");
79 ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, int depth,
80 int depth_mask, int count, u64 total_samples,
81 int hits, int left_margin)
86 ret += callchain__fprintf_left_margin(fp, left_margin);
87 for (i = 0; i < depth; i++) {
88 if (depth_mask & (1 << i))
89 ret += fprintf(fp, "|");
91 ret += fprintf(fp, " ");
92 if (!count && i == depth - 1) {
95 percent = hits * 100.0 / total_samples;
96 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
98 ret += fprintf(fp, "%s", " ");
101 ret += fprintf(fp, "%s\n", chain->sym->name);
103 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
108 static struct symbol *rem_sq_bracket;
109 static struct callchain_list rem_hits;
111 static void init_rem_hits(void)
113 rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
114 if (!rem_sq_bracket) {
115 fprintf(stderr, "Not enough memory to display remaining hits\n");
119 strcpy(rem_sq_bracket->name, "[...]");
120 rem_hits.sym = rem_sq_bracket;
124 __callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
125 u64 total_samples, int depth, int depth_mask,
128 struct rb_node *node, *next;
129 struct callchain_node *child;
130 struct callchain_list *chain;
131 int new_depth_mask = depth_mask;
137 if (callchain_param.mode == CHAIN_GRAPH_REL)
138 new_total = self->children_hit;
140 new_total = total_samples;
142 remaining = new_total;
144 node = rb_first(&self->rb_root);
148 child = rb_entry(node, struct callchain_node, rb_node);
149 cumul = cumul_hits(child);
153 * The depth mask manages the output of pipes that show
154 * the depth. We don't want to keep the pipes of the current
155 * level for the last child of this depth.
156 * Except if we have remaining filtered hits. They will
157 * supersede the last child
159 next = rb_next(node);
160 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
161 new_depth_mask &= ~(1 << (depth - 1));
164 * But we keep the older depth mask for the line seperator
165 * to keep the level link until we reach the last child
167 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
170 list_for_each_entry(chain, &child->val, list) {
171 if (chain->ip >= PERF_CONTEXT_MAX)
173 ret += ipchain__fprintf_graph(fp, chain, depth,
179 ret += __callchain__fprintf_graph(fp, child, new_total,
181 new_depth_mask | (1 << depth),
186 if (callchain_param.mode == CHAIN_GRAPH_REL &&
187 remaining && remaining != new_total) {
192 new_depth_mask &= ~(1 << (depth - 1));
194 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
195 new_depth_mask, 0, new_total,
196 remaining, left_margin);
204 callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
205 u64 total_samples, int left_margin)
207 struct callchain_list *chain;
208 bool printed = false;
212 list_for_each_entry(chain, &self->val, list) {
213 if (chain->ip >= PERF_CONTEXT_MAX)
216 if (!i++ && sort__first_dimension == SORT_SYM)
220 ret += callchain__fprintf_left_margin(fp, left_margin);
221 ret += fprintf(fp, "|\n");
222 ret += callchain__fprintf_left_margin(fp, left_margin);
223 ret += fprintf(fp, "---");
228 ret += callchain__fprintf_left_margin(fp, left_margin);
231 ret += fprintf(fp, " %s\n", chain->sym->name);
233 ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
236 ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin);
242 callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
245 struct callchain_list *chain;
251 ret += callchain__fprintf_flat(fp, self->parent, total_samples);
254 list_for_each_entry(chain, &self->val, list) {
255 if (chain->ip >= PERF_CONTEXT_MAX)
258 ret += fprintf(fp, " %s\n", chain->sym->name);
260 ret += fprintf(fp, " %p\n",
261 (void *)(long)chain->ip);
268 hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
269 u64 total_samples, int left_margin)
271 struct rb_node *rb_node;
272 struct callchain_node *chain;
275 rb_node = rb_first(&self->sorted_chain);
279 chain = rb_entry(rb_node, struct callchain_node, rb_node);
280 percent = chain->hit * 100.0 / total_samples;
281 switch (callchain_param.mode) {
283 ret += percent_color_fprintf(fp, " %6.2f%%\n",
285 ret += callchain__fprintf_flat(fp, chain, total_samples);
287 case CHAIN_GRAPH_ABS: /* Falldown */
288 case CHAIN_GRAPH_REL:
289 ret += callchain__fprintf_graph(fp, chain, total_samples,
295 ret += fprintf(fp, "\n");
296 rb_node = rb_next(rb_node);
302 static size_t hist_entry__fprintf(FILE *fp, struct hist_entry *self,
303 struct perf_session *session)
305 struct sort_entry *se;
308 if (symbol_conf.exclude_other && !self->parent)
311 if (session->events_stats.total)
312 ret = percent_color_fprintf(fp,
313 symbol_conf.field_sep ? "%.2f" : " %6.2f%%",
314 (self->count * 100.0) / session->events_stats.total);
316 ret = fprintf(fp, symbol_conf.field_sep ? "%lld" : "%12lld ", self->count);
318 if (symbol_conf.show_nr_samples) {
319 if (symbol_conf.field_sep)
320 fprintf(fp, "%c%lld", *symbol_conf.field_sep, self->count);
322 fprintf(fp, "%11lld", self->count);
325 list_for_each_entry(se, &hist_entry__sort_list, list) {
329 fprintf(fp, "%s", symbol_conf.field_sep ?: " ");
330 ret += se->print(fp, self, se->width ? *se->width : 0);
333 ret += fprintf(fp, "\n");
335 if (symbol_conf.use_callchain) {
338 if (sort__first_dimension == SORT_COMM) {
339 se = list_first_entry(&hist_entry__sort_list, typeof(*se),
341 left_margin = se->width ? *se->width : 0;
342 left_margin -= thread__comm_len(self->thread);
345 hist_entry_callchain__fprintf(fp, self, session->events_stats.total,
353 * collect histogram counts
356 static int perf_session__add_hist_entry(struct perf_session *self,
357 struct addr_location *al,
358 struct ip_callchain *chain, u64 count)
360 struct symbol **syms = NULL, *parent = NULL;
362 struct hist_entry *he;
364 if ((sort__has_parent || symbol_conf.use_callchain) && chain)
365 syms = perf_session__resolve_callchain(self, al->thread,
367 he = __perf_session__add_hist_entry(self, al, parent, count, &hit);
374 if (symbol_conf.use_callchain) {
376 callchain_init(&he->callchain);
377 append_chain(&he->callchain, chain, syms);
384 static size_t perf_session__fprintf_hists(struct perf_session *self, FILE *fp)
386 struct hist_entry *pos;
387 struct sort_entry *se;
391 char *col_width = symbol_conf.col_width_list_str;
395 fprintf(fp, "# Samples: %ld\n", self->events_stats.total);
398 fprintf(fp, "# Overhead");
399 if (symbol_conf.show_nr_samples) {
400 if (symbol_conf.field_sep)
401 fprintf(fp, "%cSamples", *symbol_conf.field_sep);
403 fputs(" Samples ", fp);
405 list_for_each_entry(se, &hist_entry__sort_list, list) {
408 if (symbol_conf.field_sep) {
409 fprintf(fp, "%c%s", *symbol_conf.field_sep, se->header);
412 width = strlen(se->header);
414 if (symbol_conf.col_width_list_str) {
416 *se->width = atoi(col_width);
417 col_width = strchr(col_width, ',');
422 width = *se->width = max(*se->width, width);
424 fprintf(fp, " %*s", width, se->header);
428 if (symbol_conf.field_sep)
431 fprintf(fp, "# ........");
432 if (symbol_conf.show_nr_samples)
433 fprintf(fp, " ..........");
434 list_for_each_entry(se, &hist_entry__sort_list, list) {
444 width = strlen(se->header);
445 for (i = 0; i < width; i++)
453 for (nd = rb_first(&self->hists); nd; nd = rb_next(nd)) {
454 pos = rb_entry(nd, struct hist_entry, rb_node);
455 ret += hist_entry__fprintf(fp, pos, self);
458 if (sort_order == default_sort_order &&
459 parent_pattern == default_parent_pattern) {
461 fprintf(fp, "# (For a higher level overview, try: perf report --sort comm,dso)\n");
466 free(rem_sq_bracket);
471 static int validate_chain(struct ip_callchain *chain, event_t *event)
473 unsigned int chain_size;
475 chain_size = event->header.size;
476 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
478 if (chain->nr*sizeof(u64) > chain_size)
484 static int process_sample_event(event_t *event, struct perf_session *session)
486 struct sample_data data = { .period = 1, };
487 struct addr_location al;
489 event__parse_sample(event, session->sample_type, &data);
491 dump_printf("(IP, %d): %d/%d: %p period: %Ld\n",
494 (void *)(long)data.ip,
495 (long long)data.period);
497 if (session->sample_type & PERF_SAMPLE_CALLCHAIN) {
500 dump_printf("... chain: nr:%Lu\n", data.callchain->nr);
502 if (validate_chain(data.callchain, event) < 0) {
503 pr_debug("call-chain problem with event, "
509 for (i = 0; i < data.callchain->nr; i++)
510 dump_printf("..... %2d: %016Lx\n",
511 i, data.callchain->ips[i]);
515 if (event__preprocess_sample(event, session, &al, NULL) < 0) {
516 fprintf(stderr, "problem processing %d event, skipping it.\n",
524 if (perf_session__add_hist_entry(session, &al, data.callchain, data.period)) {
525 pr_debug("problem incrementing symbol count, skipping event\n");
529 session->events_stats.total += data.period;
533 static int process_read_event(event_t *event, struct perf_session *session __used)
535 struct perf_event_attr *attr;
537 attr = perf_header__find_attr(event->read.id, &session->header);
540 const char *name = attr ? __event_name(attr->type, attr->config)
542 perf_read_values_add_value(&show_threads_values,
543 event->read.pid, event->read.tid,
549 dump_printf(": %d %d %s %Lu\n", event->read.pid, event->read.tid,
550 attr ? __event_name(attr->type, attr->config) : "FAIL",
556 static int sample_type_check(struct perf_session *session)
558 if (!(session->sample_type & PERF_SAMPLE_CALLCHAIN)) {
559 if (sort__has_parent) {
560 fprintf(stderr, "selected --sort parent, but no"
561 " callchain data. Did you call"
562 " perf record without -g?\n");
565 if (symbol_conf.use_callchain) {
566 fprintf(stderr, "selected -g but no callchain data."
567 " Did you call perf record without"
571 } else if (callchain_param.mode != CHAIN_NONE && !symbol_conf.use_callchain) {
572 symbol_conf.use_callchain = true;
573 if (register_callchain_param(&callchain_param) < 0) {
574 fprintf(stderr, "Can't register callchain"
583 static struct perf_event_ops event_ops = {
584 .process_sample_event = process_sample_event,
585 .process_mmap_event = event__process_mmap,
586 .process_comm_event = event__process_mmap,
587 .process_exit_event = event__process_task,
588 .process_fork_event = event__process_task,
589 .process_lost_event = event__process_lost,
590 .process_read_event = process_read_event,
591 .sample_type_check = sample_type_check,
595 static int __cmd_report(void)
598 struct perf_session *session;
600 session = perf_session__new(input_name, O_RDONLY, force);
605 perf_read_values_init(&show_threads_values);
607 ret = perf_session__process_events(session, &event_ops);
612 event__print_totals();
617 perf_session__fprintf(session, stdout);
620 dsos__fprintf(stdout);
622 perf_session__collapse_resort(session);
623 perf_session__output_resort(session, session->events_stats.total);
624 perf_session__fprintf_hists(session, stdout);
626 bool raw_printing_style = !strcmp(pretty_printing_style, "raw");
627 perf_read_values_display(stdout, &show_threads_values,
629 perf_read_values_destroy(&show_threads_values);
632 perf_session__delete(session);
637 parse_callchain_opt(const struct option *opt __used, const char *arg,
643 symbol_conf.use_callchain = true;
648 tok = strtok((char *)arg, ",");
652 /* get the output mode */
653 if (!strncmp(tok, "graph", strlen(arg)))
654 callchain_param.mode = CHAIN_GRAPH_ABS;
656 else if (!strncmp(tok, "flat", strlen(arg)))
657 callchain_param.mode = CHAIN_FLAT;
659 else if (!strncmp(tok, "fractal", strlen(arg)))
660 callchain_param.mode = CHAIN_GRAPH_REL;
662 else if (!strncmp(tok, "none", strlen(arg))) {
663 callchain_param.mode = CHAIN_NONE;
664 symbol_conf.use_callchain = true;
672 /* get the min percentage */
673 tok = strtok(NULL, ",");
677 callchain_param.min_percent = strtod(tok, &endptr);
682 if (register_callchain_param(&callchain_param) < 0) {
683 fprintf(stderr, "Can't register callchain params\n");
689 //static const char * const report_usage[] = {
690 const char * const report_usage[] = {
691 "perf report [<options>] <command>",
695 static const struct option options[] = {
696 OPT_STRING('i', "input", &input_name, "file",
698 OPT_BOOLEAN('v', "verbose", &verbose,
699 "be more verbose (show symbol address, etc)"),
700 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
701 "dump raw trace in ASCII"),
702 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
703 "file", "vmlinux pathname"),
704 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
705 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
706 "load module symbols - WARNING: use only with -k and LIVE kernel"),
707 OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
708 "Show a column with the number of samples"),
709 OPT_BOOLEAN('T', "threads", &show_threads,
710 "Show per-thread event counters"),
711 OPT_STRING(0, "pretty", &pretty_printing_style, "key",
712 "pretty printing style key: normal raw"),
713 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
714 "sort by key(s): pid, comm, dso, symbol, parent"),
715 OPT_BOOLEAN('P', "full-paths", &event_ops.full_paths,
716 "Don't shorten the pathnames taking into account the cwd"),
717 OPT_STRING('p', "parent", &parent_pattern, "regex",
718 "regex filter to identify parent, see: '--sort parent'"),
719 OPT_BOOLEAN('x', "exclude-other", &symbol_conf.exclude_other,
720 "Only display entries with parent-match"),
721 OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
722 "Display callchains using output_type and min percent threshold. "
723 "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
724 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
725 "only consider symbols in these dsos"),
726 OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
727 "only consider symbols in these comms"),
728 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
729 "only consider these symbols"),
730 OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str,
732 "don't try to adjust column width, use these fixed values"),
733 OPT_STRING('t', "field-separator", &symbol_conf.field_sep, "separator",
734 "separator for columns, no spaces will be added between "
735 "columns '.' is reserved."),
739 static void sort_entry__setup_elide(struct sort_entry *self,
740 struct strlist *list,
741 const char *list_name, FILE *fp)
743 if (list && strlist__nr_entries(list) == 1) {
744 fprintf(fp, "# %s: %s\n", list_name, strlist__entry(list, 0)->s);
749 int cmd_report(int argc, const char **argv, const char *prefix __used)
751 argc = parse_options(argc, argv, options, report_usage, 0);
755 if (symbol__init() < 0)
758 setup_sorting(report_usage, options);
760 if (parent_pattern != default_parent_pattern) {
761 sort_dimension__add("parent");
762 sort_parent.elide = 1;
764 symbol_conf.exclude_other = false;
767 * Any (unrecognized) arguments left?
770 usage_with_options(report_usage, options);
772 sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list, "dso", stdout);
773 sort_entry__setup_elide(&sort_comm, symbol_conf.comm_list, "comm", stdout);
774 sort_entry__setup_elide(&sort_sym, symbol_conf.sym_list, "symbol", stdout);
776 return __cmd_report();