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";
37 static bool use_callchain;
39 static int show_nr_samples;
41 static int show_threads;
42 static struct perf_read_values show_threads_values;
44 static char default_pretty_printing_style[] = "normal";
45 static char *pretty_printing_style = default_pretty_printing_style;
47 static int exclude_other = 1;
49 static char callchain_default_opt[] = "fractal,0.5";
52 callchain__fprintf_left_margin(FILE *fp, int left_margin)
57 ret = fprintf(fp, " ");
59 for (i = 0; i < left_margin; i++)
60 ret += fprintf(fp, " ");
65 static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
71 ret += callchain__fprintf_left_margin(fp, left_margin);
73 for (i = 0; i < depth; i++)
74 if (depth_mask & (1 << i))
75 ret += fprintf(fp, "| ");
77 ret += fprintf(fp, " ");
79 ret += fprintf(fp, "\n");
84 ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, int depth,
85 int depth_mask, int count, u64 total_samples,
86 int hits, int left_margin)
91 ret += callchain__fprintf_left_margin(fp, left_margin);
92 for (i = 0; i < depth; i++) {
93 if (depth_mask & (1 << i))
94 ret += fprintf(fp, "|");
96 ret += fprintf(fp, " ");
97 if (!count && i == depth - 1) {
100 percent = hits * 100.0 / total_samples;
101 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
103 ret += fprintf(fp, "%s", " ");
106 ret += fprintf(fp, "%s\n", chain->sym->name);
108 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
113 static struct symbol *rem_sq_bracket;
114 static struct callchain_list rem_hits;
116 static void init_rem_hits(void)
118 rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
119 if (!rem_sq_bracket) {
120 fprintf(stderr, "Not enough memory to display remaining hits\n");
124 strcpy(rem_sq_bracket->name, "[...]");
125 rem_hits.sym = rem_sq_bracket;
129 __callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
130 u64 total_samples, int depth, int depth_mask,
133 struct rb_node *node, *next;
134 struct callchain_node *child;
135 struct callchain_list *chain;
136 int new_depth_mask = depth_mask;
142 if (callchain_param.mode == CHAIN_GRAPH_REL)
143 new_total = self->children_hit;
145 new_total = total_samples;
147 remaining = new_total;
149 node = rb_first(&self->rb_root);
153 child = rb_entry(node, struct callchain_node, rb_node);
154 cumul = cumul_hits(child);
158 * The depth mask manages the output of pipes that show
159 * the depth. We don't want to keep the pipes of the current
160 * level for the last child of this depth.
161 * Except if we have remaining filtered hits. They will
162 * supersede the last child
164 next = rb_next(node);
165 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
166 new_depth_mask &= ~(1 << (depth - 1));
169 * But we keep the older depth mask for the line seperator
170 * to keep the level link until we reach the last child
172 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
175 list_for_each_entry(chain, &child->val, list) {
176 if (chain->ip >= PERF_CONTEXT_MAX)
178 ret += ipchain__fprintf_graph(fp, chain, depth,
184 ret += __callchain__fprintf_graph(fp, child, new_total,
186 new_depth_mask | (1 << depth),
191 if (callchain_param.mode == CHAIN_GRAPH_REL &&
192 remaining && remaining != new_total) {
197 new_depth_mask &= ~(1 << (depth - 1));
199 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
200 new_depth_mask, 0, new_total,
201 remaining, left_margin);
209 callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
210 u64 total_samples, int left_margin)
212 struct callchain_list *chain;
213 bool printed = false;
217 list_for_each_entry(chain, &self->val, list) {
218 if (chain->ip >= PERF_CONTEXT_MAX)
221 if (!i++ && sort__first_dimension == SORT_SYM)
225 ret += callchain__fprintf_left_margin(fp, left_margin);
226 ret += fprintf(fp, "|\n");
227 ret += callchain__fprintf_left_margin(fp, left_margin);
228 ret += fprintf(fp, "---");
233 ret += callchain__fprintf_left_margin(fp, left_margin);
236 ret += fprintf(fp, " %s\n", chain->sym->name);
238 ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
241 ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin);
247 callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
250 struct callchain_list *chain;
256 ret += callchain__fprintf_flat(fp, self->parent, total_samples);
259 list_for_each_entry(chain, &self->val, list) {
260 if (chain->ip >= PERF_CONTEXT_MAX)
263 ret += fprintf(fp, " %s\n", chain->sym->name);
265 ret += fprintf(fp, " %p\n",
266 (void *)(long)chain->ip);
273 hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
274 u64 total_samples, int left_margin)
276 struct rb_node *rb_node;
277 struct callchain_node *chain;
280 rb_node = rb_first(&self->sorted_chain);
284 chain = rb_entry(rb_node, struct callchain_node, rb_node);
285 percent = chain->hit * 100.0 / total_samples;
286 switch (callchain_param.mode) {
288 ret += percent_color_fprintf(fp, " %6.2f%%\n",
290 ret += callchain__fprintf_flat(fp, chain, total_samples);
292 case CHAIN_GRAPH_ABS: /* Falldown */
293 case CHAIN_GRAPH_REL:
294 ret += callchain__fprintf_graph(fp, chain, total_samples,
300 ret += fprintf(fp, "\n");
301 rb_node = rb_next(rb_node);
307 static size_t hist_entry__fprintf(FILE *fp, struct hist_entry *self,
308 struct perf_session *session,
311 struct sort_entry *se;
314 if (exclude_other && !self->parent)
318 ret = percent_color_fprintf(fp,
319 symbol_conf.field_sep ? "%.2f" : " %6.2f%%",
320 (self->count * 100.0) / total_samples);
322 ret = fprintf(fp, symbol_conf.field_sep ? "%lld" : "%12lld ", self->count);
324 if (show_nr_samples) {
325 if (symbol_conf.field_sep)
326 fprintf(fp, "%c%lld", *symbol_conf.field_sep, self->count);
328 fprintf(fp, "%11lld", self->count);
331 list_for_each_entry(se, &hist_entry__sort_list, list) {
335 fprintf(fp, "%s", symbol_conf.field_sep ?: " ");
336 ret += se->print(fp, self, se->width ? *se->width : 0);
339 ret += fprintf(fp, "\n");
341 if (session->use_callchain) {
344 if (sort__first_dimension == SORT_COMM) {
345 se = list_first_entry(&hist_entry__sort_list, typeof(*se),
347 left_margin = se->width ? *se->width : 0;
348 left_margin -= thread__comm_len(self->thread);
351 hist_entry_callchain__fprintf(fp, self, total_samples,
358 static void thread__comm_adjust(struct thread *self)
360 char *comm = self->comm;
362 if (!symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
363 (!symbol_conf.comm_list ||
364 strlist__has_entry(symbol_conf.comm_list, comm))) {
365 unsigned int slen = strlen(comm);
367 if (slen > comms__col_width) {
368 comms__col_width = slen;
369 threads__col_width = slen + 6;
374 static int thread__set_comm_adjust(struct thread *self, const char *comm)
376 int ret = thread__set_comm(self, comm);
381 thread__comm_adjust(self);
387 * collect histogram counts
390 static int perf_session__add_hist_entry(struct perf_session *self,
391 struct addr_location *al,
392 struct ip_callchain *chain, u64 count)
394 struct symbol **syms = NULL, *parent = NULL;
396 struct hist_entry *he;
398 if ((sort__has_parent || self->use_callchain) && chain)
399 syms = perf_session__resolve_callchain(self, al->thread,
401 he = __perf_session__add_hist_entry(self, al, parent, count, &hit);
408 if (self->use_callchain) {
410 callchain_init(&he->callchain);
411 append_chain(&he->callchain, chain, syms);
418 static size_t perf_session__fprintf_hist_entries(struct perf_session *self,
419 u64 total_samples, FILE *fp)
421 struct hist_entry *pos;
422 struct sort_entry *se;
426 char *col_width = symbol_conf.col_width_list_str;
427 int raw_printing_style;
429 raw_printing_style = !strcmp(pretty_printing_style, "raw");
433 fprintf(fp, "# Samples: %Ld\n", (u64)total_samples);
436 fprintf(fp, "# Overhead");
437 if (show_nr_samples) {
438 if (symbol_conf.field_sep)
439 fprintf(fp, "%cSamples", *symbol_conf.field_sep);
441 fputs(" Samples ", fp);
443 list_for_each_entry(se, &hist_entry__sort_list, list) {
446 if (symbol_conf.field_sep) {
447 fprintf(fp, "%c%s", *symbol_conf.field_sep, se->header);
450 width = strlen(se->header);
452 if (symbol_conf.col_width_list_str) {
454 *se->width = atoi(col_width);
455 col_width = strchr(col_width, ',');
460 width = *se->width = max(*se->width, width);
462 fprintf(fp, " %*s", width, se->header);
466 if (symbol_conf.field_sep)
469 fprintf(fp, "# ........");
471 fprintf(fp, " ..........");
472 list_for_each_entry(se, &hist_entry__sort_list, list) {
482 width = strlen(se->header);
483 for (i = 0; i < width; i++)
491 for (nd = rb_first(&self->hists); nd; nd = rb_next(nd)) {
492 pos = rb_entry(nd, struct hist_entry, rb_node);
493 ret += hist_entry__fprintf(fp, pos, self, total_samples);
496 if (sort_order == default_sort_order &&
497 parent_pattern == default_parent_pattern) {
499 fprintf(fp, "# (For a higher level overview, try: perf report --sort comm,dso)\n");
504 free(rem_sq_bracket);
507 perf_read_values_display(fp, &show_threads_values,
513 static int validate_chain(struct ip_callchain *chain, event_t *event)
515 unsigned int chain_size;
517 chain_size = event->header.size;
518 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
520 if (chain->nr*sizeof(u64) > chain_size)
526 static int process_sample_event(event_t *event, struct perf_session *session)
528 struct sample_data data = { .period = 1, };
529 struct addr_location al;
531 event__parse_sample(event, session->sample_type, &data);
533 dump_printf("(IP, %d): %d/%d: %p period: %Ld\n",
536 (void *)(long)data.ip,
537 (long long)data.period);
539 if (session->sample_type & PERF_SAMPLE_CALLCHAIN) {
542 dump_printf("... chain: nr:%Lu\n", data.callchain->nr);
544 if (validate_chain(data.callchain, event) < 0) {
545 pr_debug("call-chain problem with event, "
551 for (i = 0; i < data.callchain->nr; i++)
552 dump_printf("..... %2d: %016Lx\n",
553 i, data.callchain->ips[i]);
557 if (event__preprocess_sample(event, session, &al, NULL) < 0) {
558 fprintf(stderr, "problem processing %d event, skipping it.\n",
566 if (perf_session__add_hist_entry(session, &al, data.callchain, data.period)) {
567 pr_debug("problem incrementing symbol count, skipping event\n");
571 session->events_stats.total += data.period;
575 static int process_comm_event(event_t *event, struct perf_session *session)
577 struct thread *thread = perf_session__findnew(session, event->comm.pid);
579 dump_printf(": %s:%d\n", event->comm.comm, event->comm.pid);
581 if (thread == NULL ||
582 thread__set_comm_adjust(thread, event->comm.comm)) {
583 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
590 static int process_read_event(event_t *event, struct perf_session *session __used)
592 struct perf_event_attr *attr;
594 attr = perf_header__find_attr(event->read.id, &session->header);
597 const char *name = attr ? __event_name(attr->type, attr->config)
599 perf_read_values_add_value(&show_threads_values,
600 event->read.pid, event->read.tid,
606 dump_printf(": %d %d %s %Lu\n", event->read.pid, event->read.tid,
607 attr ? __event_name(attr->type, attr->config) : "FAIL",
613 static int sample_type_check(struct perf_session *session)
615 if (!(session->sample_type & PERF_SAMPLE_CALLCHAIN)) {
616 if (sort__has_parent) {
617 fprintf(stderr, "selected --sort parent, but no"
618 " callchain data. Did you call"
619 " perf record without -g?\n");
622 if (session->use_callchain) {
623 fprintf(stderr, "selected -g but no callchain data."
624 " Did you call perf record without"
628 } else if (callchain_param.mode != CHAIN_NONE && !session->use_callchain) {
629 session->use_callchain = true;
630 if (register_callchain_param(&callchain_param) < 0) {
631 fprintf(stderr, "Can't register callchain"
640 static struct perf_event_ops event_ops = {
641 .process_sample_event = process_sample_event,
642 .process_mmap_event = event__process_mmap,
643 .process_comm_event = process_comm_event,
644 .process_exit_event = event__process_task,
645 .process_fork_event = event__process_task,
646 .process_lost_event = event__process_lost,
647 .process_read_event = process_read_event,
648 .sample_type_check = sample_type_check,
652 static int __cmd_report(void)
655 struct perf_session *session;
657 session = perf_session__new(input_name, O_RDONLY, force);
661 session->use_callchain = use_callchain;
664 perf_read_values_init(&show_threads_values);
666 ret = perf_session__process_events(session, &event_ops);
671 event__print_totals();
676 perf_session__fprintf(session, stdout);
679 dsos__fprintf(stdout);
681 perf_session__collapse_resort(session);
682 perf_session__output_resort(session, session->events_stats.total);
683 perf_session__fprintf_hist_entries(session, session->events_stats.total, stdout);
686 perf_read_values_destroy(&show_threads_values);
688 perf_session__delete(session);
693 parse_callchain_opt(const struct option *opt __used, const char *arg,
699 use_callchain = true;
704 tok = strtok((char *)arg, ",");
708 /* get the output mode */
709 if (!strncmp(tok, "graph", strlen(arg)))
710 callchain_param.mode = CHAIN_GRAPH_ABS;
712 else if (!strncmp(tok, "flat", strlen(arg)))
713 callchain_param.mode = CHAIN_FLAT;
715 else if (!strncmp(tok, "fractal", strlen(arg)))
716 callchain_param.mode = CHAIN_GRAPH_REL;
718 else if (!strncmp(tok, "none", strlen(arg))) {
719 callchain_param.mode = CHAIN_NONE;
720 use_callchain = true;
728 /* get the min percentage */
729 tok = strtok(NULL, ",");
733 callchain_param.min_percent = strtod(tok, &endptr);
738 if (register_callchain_param(&callchain_param) < 0) {
739 fprintf(stderr, "Can't register callchain params\n");
745 //static const char * const report_usage[] = {
746 const char * const report_usage[] = {
747 "perf report [<options>] <command>",
751 static const struct option options[] = {
752 OPT_STRING('i', "input", &input_name, "file",
754 OPT_BOOLEAN('v', "verbose", &verbose,
755 "be more verbose (show symbol address, etc)"),
756 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
757 "dump raw trace in ASCII"),
758 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
759 "file", "vmlinux pathname"),
760 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
761 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
762 "load module symbols - WARNING: use only with -k and LIVE kernel"),
763 OPT_BOOLEAN('n', "show-nr-samples", &show_nr_samples,
764 "Show a column with the number of samples"),
765 OPT_BOOLEAN('T', "threads", &show_threads,
766 "Show per-thread event counters"),
767 OPT_STRING(0, "pretty", &pretty_printing_style, "key",
768 "pretty printing style key: normal raw"),
769 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
770 "sort by key(s): pid, comm, dso, symbol, parent"),
771 OPT_BOOLEAN('P', "full-paths", &event_ops.full_paths,
772 "Don't shorten the pathnames taking into account the cwd"),
773 OPT_STRING('p', "parent", &parent_pattern, "regex",
774 "regex filter to identify parent, see: '--sort parent'"),
775 OPT_BOOLEAN('x', "exclude-other", &exclude_other,
776 "Only display entries with parent-match"),
777 OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
778 "Display callchains using output_type and min percent threshold. "
779 "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
780 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
781 "only consider symbols in these dsos"),
782 OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
783 "only consider symbols in these comms"),
784 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
785 "only consider these symbols"),
786 OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str,
788 "don't try to adjust column width, use these fixed values"),
789 OPT_STRING('t', "field-separator", &symbol_conf.field_sep, "separator",
790 "separator for columns, no spaces will be added between "
791 "columns '.' is reserved."),
795 static void sort_entry__setup_elide(struct sort_entry *self,
796 struct strlist *list,
797 const char *list_name, FILE *fp)
799 if (list && strlist__nr_entries(list) == 1) {
800 fprintf(fp, "# %s: %s\n", list_name, strlist__entry(list, 0)->s);
805 int cmd_report(int argc, const char **argv, const char *prefix __used)
807 argc = parse_options(argc, argv, options, report_usage, 0);
811 if (symbol__init() < 0)
814 setup_sorting(report_usage, options);
816 if (parent_pattern != default_parent_pattern) {
817 sort_dimension__add("parent");
818 sort_parent.elide = 1;
823 * Any (unrecognized) arguments left?
826 usage_with_options(report_usage, options);
828 sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list, "dso", stdout);
829 sort_entry__setup_elide(&sort_comm, symbol_conf.comm_list, "comm", stdout);
830 sort_entry__setup_elide(&sort_sym, symbol_conf.sym_list, "symbol", stdout);
832 return __cmd_report();