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";
36 static char *dso_list_str, *comm_list_str, *sym_list_str,
38 static struct strlist *dso_list, *comm_list, *sym_list;
41 static bool use_callchain;
43 static int show_nr_samples;
45 static int show_threads;
46 static struct perf_read_values show_threads_values;
48 static char default_pretty_printing_style[] = "normal";
49 static char *pretty_printing_style = default_pretty_printing_style;
51 static int exclude_other = 1;
53 static char callchain_default_opt[] = "fractal,0.5";
56 callchain__fprintf_left_margin(FILE *fp, int left_margin)
61 ret = fprintf(fp, " ");
63 for (i = 0; i < left_margin; i++)
64 ret += fprintf(fp, " ");
69 static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
75 ret += callchain__fprintf_left_margin(fp, left_margin);
77 for (i = 0; i < depth; i++)
78 if (depth_mask & (1 << i))
79 ret += fprintf(fp, "| ");
81 ret += fprintf(fp, " ");
83 ret += fprintf(fp, "\n");
88 ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, int depth,
89 int depth_mask, int count, u64 total_samples,
90 int hits, int left_margin)
95 ret += callchain__fprintf_left_margin(fp, left_margin);
96 for (i = 0; i < depth; i++) {
97 if (depth_mask & (1 << i))
98 ret += fprintf(fp, "|");
100 ret += fprintf(fp, " ");
101 if (!count && i == depth - 1) {
104 percent = hits * 100.0 / total_samples;
105 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
107 ret += fprintf(fp, "%s", " ");
110 ret += fprintf(fp, "%s\n", chain->sym->name);
112 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
117 static struct symbol *rem_sq_bracket;
118 static struct callchain_list rem_hits;
120 static void init_rem_hits(void)
122 rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
123 if (!rem_sq_bracket) {
124 fprintf(stderr, "Not enough memory to display remaining hits\n");
128 strcpy(rem_sq_bracket->name, "[...]");
129 rem_hits.sym = rem_sq_bracket;
133 __callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
134 u64 total_samples, int depth, int depth_mask,
137 struct rb_node *node, *next;
138 struct callchain_node *child;
139 struct callchain_list *chain;
140 int new_depth_mask = depth_mask;
146 if (callchain_param.mode == CHAIN_GRAPH_REL)
147 new_total = self->children_hit;
149 new_total = total_samples;
151 remaining = new_total;
153 node = rb_first(&self->rb_root);
157 child = rb_entry(node, struct callchain_node, rb_node);
158 cumul = cumul_hits(child);
162 * The depth mask manages the output of pipes that show
163 * the depth. We don't want to keep the pipes of the current
164 * level for the last child of this depth.
165 * Except if we have remaining filtered hits. They will
166 * supersede the last child
168 next = rb_next(node);
169 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
170 new_depth_mask &= ~(1 << (depth - 1));
173 * But we keep the older depth mask for the line seperator
174 * to keep the level link until we reach the last child
176 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
179 list_for_each_entry(chain, &child->val, list) {
180 if (chain->ip >= PERF_CONTEXT_MAX)
182 ret += ipchain__fprintf_graph(fp, chain, depth,
188 ret += __callchain__fprintf_graph(fp, child, new_total,
190 new_depth_mask | (1 << depth),
195 if (callchain_param.mode == CHAIN_GRAPH_REL &&
196 remaining && remaining != new_total) {
201 new_depth_mask &= ~(1 << (depth - 1));
203 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
204 new_depth_mask, 0, new_total,
205 remaining, left_margin);
213 callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
214 u64 total_samples, int left_margin)
216 struct callchain_list *chain;
217 bool printed = false;
221 list_for_each_entry(chain, &self->val, list) {
222 if (chain->ip >= PERF_CONTEXT_MAX)
225 if (!i++ && sort__first_dimension == SORT_SYM)
229 ret += callchain__fprintf_left_margin(fp, left_margin);
230 ret += fprintf(fp, "|\n");
231 ret += callchain__fprintf_left_margin(fp, left_margin);
232 ret += fprintf(fp, "---");
237 ret += callchain__fprintf_left_margin(fp, left_margin);
240 ret += fprintf(fp, " %s\n", chain->sym->name);
242 ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
245 ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin);
251 callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
254 struct callchain_list *chain;
260 ret += callchain__fprintf_flat(fp, self->parent, total_samples);
263 list_for_each_entry(chain, &self->val, list) {
264 if (chain->ip >= PERF_CONTEXT_MAX)
267 ret += fprintf(fp, " %s\n", chain->sym->name);
269 ret += fprintf(fp, " %p\n",
270 (void *)(long)chain->ip);
277 hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
278 u64 total_samples, int left_margin)
280 struct rb_node *rb_node;
281 struct callchain_node *chain;
284 rb_node = rb_first(&self->sorted_chain);
288 chain = rb_entry(rb_node, struct callchain_node, rb_node);
289 percent = chain->hit * 100.0 / total_samples;
290 switch (callchain_param.mode) {
292 ret += percent_color_fprintf(fp, " %6.2f%%\n",
294 ret += callchain__fprintf_flat(fp, chain, total_samples);
296 case CHAIN_GRAPH_ABS: /* Falldown */
297 case CHAIN_GRAPH_REL:
298 ret += callchain__fprintf_graph(fp, chain, total_samples,
304 ret += fprintf(fp, "\n");
305 rb_node = rb_next(rb_node);
311 static size_t hist_entry__fprintf(FILE *fp, struct hist_entry *self,
312 struct perf_session *session,
315 struct sort_entry *se;
318 if (exclude_other && !self->parent)
322 ret = percent_color_fprintf(fp,
323 field_sep ? "%.2f" : " %6.2f%%",
324 (self->count * 100.0) / total_samples);
326 ret = fprintf(fp, field_sep ? "%lld" : "%12lld ", self->count);
328 if (show_nr_samples) {
330 fprintf(fp, "%c%lld", *field_sep, self->count);
332 fprintf(fp, "%11lld", self->count);
335 list_for_each_entry(se, &hist_entry__sort_list, list) {
339 fprintf(fp, "%s", field_sep ?: " ");
340 ret += se->print(fp, self, se->width ? *se->width : 0);
343 ret += fprintf(fp, "\n");
345 if (session->use_callchain) {
348 if (sort__first_dimension == SORT_COMM) {
349 se = list_first_entry(&hist_entry__sort_list, typeof(*se),
351 left_margin = se->width ? *se->width : 0;
352 left_margin -= thread__comm_len(self->thread);
355 hist_entry_callchain__fprintf(fp, self, total_samples,
366 static void dso__calc_col_width(struct dso *self)
368 if (!col_width_list_str && !field_sep &&
369 (!dso_list || strlist__has_entry(dso_list, self->name))) {
370 unsigned int slen = strlen(self->name);
371 if (slen > dsos__col_width)
372 dsos__col_width = slen;
375 self->slen_calculated = 1;
378 static void thread__comm_adjust(struct thread *self)
380 char *comm = self->comm;
382 if (!col_width_list_str && !field_sep &&
383 (!comm_list || strlist__has_entry(comm_list, comm))) {
384 unsigned int slen = strlen(comm);
386 if (slen > comms__col_width) {
387 comms__col_width = slen;
388 threads__col_width = slen + 6;
393 static int thread__set_comm_adjust(struct thread *self, const char *comm)
395 int ret = thread__set_comm(self, comm);
400 thread__comm_adjust(self);
406 * collect histogram counts
409 static int perf_session__add_hist_entry(struct perf_session *self,
410 struct addr_location *al,
411 struct ip_callchain *chain, u64 count)
413 struct symbol **syms = NULL, *parent = NULL;
415 struct hist_entry *he;
417 if ((sort__has_parent || self->use_callchain) && chain)
418 syms = perf_session__resolve_callchain(self, al->thread,
420 he = __perf_session__add_hist_entry(self, al, parent, count, &hit);
427 if (self->use_callchain) {
429 callchain_init(&he->callchain);
430 append_chain(&he->callchain, chain, syms);
437 static size_t perf_session__fprintf_hist_entries(struct perf_session *self,
438 u64 total_samples, FILE *fp)
440 struct hist_entry *pos;
441 struct sort_entry *se;
445 char *col_width = col_width_list_str;
446 int raw_printing_style;
448 raw_printing_style = !strcmp(pretty_printing_style, "raw");
452 fprintf(fp, "# Samples: %Ld\n", (u64)total_samples);
455 fprintf(fp, "# Overhead");
456 if (show_nr_samples) {
458 fprintf(fp, "%cSamples", *field_sep);
460 fputs(" Samples ", fp);
462 list_for_each_entry(se, &hist_entry__sort_list, list) {
466 fprintf(fp, "%c%s", *field_sep, se->header);
469 width = strlen(se->header);
471 if (col_width_list_str) {
473 *se->width = atoi(col_width);
474 col_width = strchr(col_width, ',');
479 width = *se->width = max(*se->width, width);
481 fprintf(fp, " %*s", width, se->header);
488 fprintf(fp, "# ........");
490 fprintf(fp, " ..........");
491 list_for_each_entry(se, &hist_entry__sort_list, list) {
501 width = strlen(se->header);
502 for (i = 0; i < width; i++)
510 for (nd = rb_first(&self->hists); nd; nd = rb_next(nd)) {
511 pos = rb_entry(nd, struct hist_entry, rb_node);
512 ret += hist_entry__fprintf(fp, pos, self, total_samples);
515 if (sort_order == default_sort_order &&
516 parent_pattern == default_parent_pattern) {
518 fprintf(fp, "# (For a higher level overview, try: perf report --sort comm,dso)\n");
523 free(rem_sq_bracket);
526 perf_read_values_display(fp, &show_threads_values,
532 static int validate_chain(struct ip_callchain *chain, event_t *event)
534 unsigned int chain_size;
536 chain_size = event->header.size;
537 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
539 if (chain->nr*sizeof(u64) > chain_size)
545 static int process_sample_event(event_t *event, struct perf_session *session)
547 struct sample_data data;
549 struct addr_location al;
550 struct thread *thread;
552 memset(&data, 0, sizeof(data));
555 event__parse_sample(event, session->sample_type, &data);
557 dump_printf("(IP, %d): %d/%d: %p period: %Ld\n",
560 (void *)(long)data.ip,
561 (long long)data.period);
563 if (session->sample_type & PERF_SAMPLE_CALLCHAIN) {
566 dump_printf("... chain: nr:%Lu\n", data.callchain->nr);
568 if (validate_chain(data.callchain, event) < 0) {
569 pr_debug("call-chain problem with event, "
575 for (i = 0; i < data.callchain->nr; i++)
576 dump_printf("..... %2d: %016Lx\n",
577 i, data.callchain->ips[i]);
581 thread = perf_session__findnew(session, data.pid);
582 if (thread == NULL) {
583 pr_debug("problem processing %d event, skipping it.\n",
588 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
590 if (comm_list && !strlist__has_entry(comm_list, thread->comm))
593 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
595 thread__find_addr_location(thread, session, cpumode,
596 MAP__FUNCTION, data.ip, &al, NULL);
598 * We have to do this here as we may have a dso with no symbol hit that
599 * has a name longer than the ones with symbols sampled.
601 if (al.map && !sort_dso.elide && !al.map->dso->slen_calculated)
602 dso__calc_col_width(al.map->dso);
605 (!al.map || !al.map->dso ||
606 !(strlist__has_entry(dso_list, al.map->dso->short_name) ||
607 (al.map->dso->short_name != al.map->dso->long_name &&
608 strlist__has_entry(dso_list, al.map->dso->long_name)))))
611 if (sym_list && al.sym && !strlist__has_entry(sym_list, al.sym->name))
614 if (perf_session__add_hist_entry(session, &al, data.callchain, data.period)) {
615 pr_debug("problem incrementing symbol count, skipping event\n");
619 session->events_stats.total += data.period;
623 static int process_comm_event(event_t *event, struct perf_session *session)
625 struct thread *thread = perf_session__findnew(session, event->comm.pid);
627 dump_printf(": %s:%d\n", event->comm.comm, event->comm.pid);
629 if (thread == NULL ||
630 thread__set_comm_adjust(thread, event->comm.comm)) {
631 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
638 static int process_read_event(event_t *event, struct perf_session *session __used)
640 struct perf_event_attr *attr;
642 attr = perf_header__find_attr(event->read.id, &session->header);
645 const char *name = attr ? __event_name(attr->type, attr->config)
647 perf_read_values_add_value(&show_threads_values,
648 event->read.pid, event->read.tid,
654 dump_printf(": %d %d %s %Lu\n", event->read.pid, event->read.tid,
655 attr ? __event_name(attr->type, attr->config) : "FAIL",
661 static int sample_type_check(struct perf_session *session)
663 if (!(session->sample_type & PERF_SAMPLE_CALLCHAIN)) {
664 if (sort__has_parent) {
665 fprintf(stderr, "selected --sort parent, but no"
666 " callchain data. Did you call"
667 " perf record without -g?\n");
670 if (session->use_callchain) {
671 fprintf(stderr, "selected -g but no callchain data."
672 " Did you call perf record without"
676 } else if (callchain_param.mode != CHAIN_NONE && !session->use_callchain) {
677 session->use_callchain = true;
678 if (register_callchain_param(&callchain_param) < 0) {
679 fprintf(stderr, "Can't register callchain"
688 static struct perf_event_ops event_ops = {
689 .process_sample_event = process_sample_event,
690 .process_mmap_event = event__process_mmap,
691 .process_comm_event = process_comm_event,
692 .process_exit_event = event__process_task,
693 .process_fork_event = event__process_task,
694 .process_lost_event = event__process_lost,
695 .process_read_event = process_read_event,
696 .sample_type_check = sample_type_check,
700 static int __cmd_report(void)
703 struct perf_session *session;
705 session = perf_session__new(input_name, O_RDONLY, force);
709 session->use_callchain = use_callchain;
712 perf_read_values_init(&show_threads_values);
714 ret = perf_session__process_events(session, &event_ops);
719 event__print_totals();
724 perf_session__fprintf(session, stdout);
727 dsos__fprintf(stdout);
729 perf_session__collapse_resort(session);
730 perf_session__output_resort(session, session->events_stats.total);
731 perf_session__fprintf_hist_entries(session, session->events_stats.total, stdout);
734 perf_read_values_destroy(&show_threads_values);
736 perf_session__delete(session);
741 parse_callchain_opt(const struct option *opt __used, const char *arg,
747 use_callchain = true;
752 tok = strtok((char *)arg, ",");
756 /* get the output mode */
757 if (!strncmp(tok, "graph", strlen(arg)))
758 callchain_param.mode = CHAIN_GRAPH_ABS;
760 else if (!strncmp(tok, "flat", strlen(arg)))
761 callchain_param.mode = CHAIN_FLAT;
763 else if (!strncmp(tok, "fractal", strlen(arg)))
764 callchain_param.mode = CHAIN_GRAPH_REL;
766 else if (!strncmp(tok, "none", strlen(arg))) {
767 callchain_param.mode = CHAIN_NONE;
768 use_callchain = true;
776 /* get the min percentage */
777 tok = strtok(NULL, ",");
781 callchain_param.min_percent = strtod(tok, &endptr);
786 if (register_callchain_param(&callchain_param) < 0) {
787 fprintf(stderr, "Can't register callchain params\n");
793 //static const char * const report_usage[] = {
794 const char * const report_usage[] = {
795 "perf report [<options>] <command>",
799 static const struct option options[] = {
800 OPT_STRING('i', "input", &input_name, "file",
802 OPT_BOOLEAN('v', "verbose", &verbose,
803 "be more verbose (show symbol address, etc)"),
804 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
805 "dump raw trace in ASCII"),
806 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
807 "file", "vmlinux pathname"),
808 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
809 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
810 "load module symbols - WARNING: use only with -k and LIVE kernel"),
811 OPT_BOOLEAN('n', "show-nr-samples", &show_nr_samples,
812 "Show a column with the number of samples"),
813 OPT_BOOLEAN('T', "threads", &show_threads,
814 "Show per-thread event counters"),
815 OPT_STRING(0, "pretty", &pretty_printing_style, "key",
816 "pretty printing style key: normal raw"),
817 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
818 "sort by key(s): pid, comm, dso, symbol, parent"),
819 OPT_BOOLEAN('P', "full-paths", &event_ops.full_paths,
820 "Don't shorten the pathnames taking into account the cwd"),
821 OPT_STRING('p', "parent", &parent_pattern, "regex",
822 "regex filter to identify parent, see: '--sort parent'"),
823 OPT_BOOLEAN('x', "exclude-other", &exclude_other,
824 "Only display entries with parent-match"),
825 OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
826 "Display callchains using output_type and min percent threshold. "
827 "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
828 OPT_STRING('d', "dsos", &dso_list_str, "dso[,dso...]",
829 "only consider symbols in these dsos"),
830 OPT_STRING('C', "comms", &comm_list_str, "comm[,comm...]",
831 "only consider symbols in these comms"),
832 OPT_STRING('S', "symbols", &sym_list_str, "symbol[,symbol...]",
833 "only consider these symbols"),
834 OPT_STRING('w', "column-widths", &col_width_list_str,
836 "don't try to adjust column width, use these fixed values"),
837 OPT_STRING('t', "field-separator", &field_sep, "separator",
838 "separator for columns, no spaces will be added between "
839 "columns '.' is reserved."),
843 static void setup_list(struct strlist **list, const char *list_str,
844 struct sort_entry *se, const char *list_name,
848 *list = strlist__new(true, list_str);
850 fprintf(stderr, "problems parsing %s list\n",
854 if (strlist__nr_entries(*list) == 1) {
855 fprintf(fp, "# %s: %s\n", list_name,
856 strlist__entry(*list, 0)->s);
862 int cmd_report(int argc, const char **argv, const char *prefix __used)
864 if (symbol__init() < 0)
867 argc = parse_options(argc, argv, options, report_usage, 0);
869 setup_sorting(report_usage, options);
871 if (parent_pattern != default_parent_pattern) {
872 sort_dimension__add("parent");
873 sort_parent.elide = 1;
878 * Any (unrecognized) arguments left?
881 usage_with_options(report_usage, options);
885 setup_list(&dso_list, dso_list_str, &sort_dso, "dso", stdout);
886 setup_list(&comm_list, comm_list_str, &sort_comm, "comm", stdout);
887 setup_list(&sym_list, sym_list_str, &sort_sym, "symbol", stdout);
889 if (field_sep && *field_sep == '.') {
890 fputs("'.' is the only non valid --field-separator argument\n",
895 return __cmd_report();