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";
55 struct symbol_conf symbol_conf;
59 callchain__fprintf_left_margin(FILE *fp, int left_margin)
64 ret = fprintf(fp, " ");
66 for (i = 0; i < left_margin; i++)
67 ret += fprintf(fp, " ");
72 static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
78 ret += callchain__fprintf_left_margin(fp, left_margin);
80 for (i = 0; i < depth; i++)
81 if (depth_mask & (1 << i))
82 ret += fprintf(fp, "| ");
84 ret += fprintf(fp, " ");
86 ret += fprintf(fp, "\n");
91 ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, int depth,
92 int depth_mask, int count, u64 total_samples,
93 int hits, int left_margin)
98 ret += callchain__fprintf_left_margin(fp, left_margin);
99 for (i = 0; i < depth; i++) {
100 if (depth_mask & (1 << i))
101 ret += fprintf(fp, "|");
103 ret += fprintf(fp, " ");
104 if (!count && i == depth - 1) {
107 percent = hits * 100.0 / total_samples;
108 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
110 ret += fprintf(fp, "%s", " ");
113 ret += fprintf(fp, "%s\n", chain->sym->name);
115 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
120 static struct symbol *rem_sq_bracket;
121 static struct callchain_list rem_hits;
123 static void init_rem_hits(void)
125 rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
126 if (!rem_sq_bracket) {
127 fprintf(stderr, "Not enough memory to display remaining hits\n");
131 strcpy(rem_sq_bracket->name, "[...]");
132 rem_hits.sym = rem_sq_bracket;
136 __callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
137 u64 total_samples, int depth, int depth_mask,
140 struct rb_node *node, *next;
141 struct callchain_node *child;
142 struct callchain_list *chain;
143 int new_depth_mask = depth_mask;
149 if (callchain_param.mode == CHAIN_GRAPH_REL)
150 new_total = self->children_hit;
152 new_total = total_samples;
154 remaining = new_total;
156 node = rb_first(&self->rb_root);
160 child = rb_entry(node, struct callchain_node, rb_node);
161 cumul = cumul_hits(child);
165 * The depth mask manages the output of pipes that show
166 * the depth. We don't want to keep the pipes of the current
167 * level for the last child of this depth.
168 * Except if we have remaining filtered hits. They will
169 * supersede the last child
171 next = rb_next(node);
172 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
173 new_depth_mask &= ~(1 << (depth - 1));
176 * But we keep the older depth mask for the line seperator
177 * to keep the level link until we reach the last child
179 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
182 list_for_each_entry(chain, &child->val, list) {
183 if (chain->ip >= PERF_CONTEXT_MAX)
185 ret += ipchain__fprintf_graph(fp, chain, depth,
191 ret += __callchain__fprintf_graph(fp, child, new_total,
193 new_depth_mask | (1 << depth),
198 if (callchain_param.mode == CHAIN_GRAPH_REL &&
199 remaining && remaining != new_total) {
204 new_depth_mask &= ~(1 << (depth - 1));
206 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
207 new_depth_mask, 0, new_total,
208 remaining, left_margin);
216 callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
217 u64 total_samples, int left_margin)
219 struct callchain_list *chain;
220 bool printed = false;
224 list_for_each_entry(chain, &self->val, list) {
225 if (chain->ip >= PERF_CONTEXT_MAX)
228 if (!i++ && sort__first_dimension == SORT_SYM)
232 ret += callchain__fprintf_left_margin(fp, left_margin);
233 ret += fprintf(fp, "|\n");
234 ret += callchain__fprintf_left_margin(fp, left_margin);
235 ret += fprintf(fp, "---");
240 ret += callchain__fprintf_left_margin(fp, left_margin);
243 ret += fprintf(fp, " %s\n", chain->sym->name);
245 ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
248 ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin);
254 callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
257 struct callchain_list *chain;
263 ret += callchain__fprintf_flat(fp, self->parent, total_samples);
266 list_for_each_entry(chain, &self->val, list) {
267 if (chain->ip >= PERF_CONTEXT_MAX)
270 ret += fprintf(fp, " %s\n", chain->sym->name);
272 ret += fprintf(fp, " %p\n",
273 (void *)(long)chain->ip);
280 hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
281 u64 total_samples, int left_margin)
283 struct rb_node *rb_node;
284 struct callchain_node *chain;
287 rb_node = rb_first(&self->sorted_chain);
291 chain = rb_entry(rb_node, struct callchain_node, rb_node);
292 percent = chain->hit * 100.0 / total_samples;
293 switch (callchain_param.mode) {
295 ret += percent_color_fprintf(fp, " %6.2f%%\n",
297 ret += callchain__fprintf_flat(fp, chain, total_samples);
299 case CHAIN_GRAPH_ABS: /* Falldown */
300 case CHAIN_GRAPH_REL:
301 ret += callchain__fprintf_graph(fp, chain, total_samples,
307 ret += fprintf(fp, "\n");
308 rb_node = rb_next(rb_node);
314 static size_t hist_entry__fprintf(FILE *fp, struct hist_entry *self,
315 struct perf_session *session,
318 struct sort_entry *se;
321 if (exclude_other && !self->parent)
325 ret = percent_color_fprintf(fp,
326 field_sep ? "%.2f" : " %6.2f%%",
327 (self->count * 100.0) / total_samples);
329 ret = fprintf(fp, field_sep ? "%lld" : "%12lld ", self->count);
331 if (show_nr_samples) {
333 fprintf(fp, "%c%lld", *field_sep, self->count);
335 fprintf(fp, "%11lld", self->count);
338 list_for_each_entry(se, &hist_entry__sort_list, list) {
342 fprintf(fp, "%s", field_sep ?: " ");
343 ret += se->print(fp, self, se->width ? *se->width : 0);
346 ret += fprintf(fp, "\n");
348 if (session->use_callchain) {
351 if (sort__first_dimension == SORT_COMM) {
352 se = list_first_entry(&hist_entry__sort_list, typeof(*se),
354 left_margin = se->width ? *se->width : 0;
355 left_margin -= thread__comm_len(self->thread);
358 hist_entry_callchain__fprintf(fp, self, total_samples,
369 static void dso__calc_col_width(struct dso *self)
371 if (!col_width_list_str && !field_sep &&
372 (!dso_list || strlist__has_entry(dso_list, self->name))) {
373 unsigned int slen = strlen(self->name);
374 if (slen > dsos__col_width)
375 dsos__col_width = slen;
378 self->slen_calculated = 1;
381 static void thread__comm_adjust(struct thread *self)
383 char *comm = self->comm;
385 if (!col_width_list_str && !field_sep &&
386 (!comm_list || strlist__has_entry(comm_list, comm))) {
387 unsigned int slen = strlen(comm);
389 if (slen > comms__col_width) {
390 comms__col_width = slen;
391 threads__col_width = slen + 6;
396 static int thread__set_comm_adjust(struct thread *self, const char *comm)
398 int ret = thread__set_comm(self, comm);
403 thread__comm_adjust(self);
409 * collect histogram counts
412 static int perf_session__add_hist_entry(struct perf_session *self,
413 struct addr_location *al,
414 struct ip_callchain *chain, u64 count)
416 struct symbol **syms = NULL, *parent = NULL;
418 struct hist_entry *he;
420 if ((sort__has_parent || self->use_callchain) && chain)
421 syms = perf_session__resolve_callchain(self, al->thread,
423 he = __perf_session__add_hist_entry(self, al, parent, count, &hit);
430 if (self->use_callchain) {
432 callchain_init(&he->callchain);
433 append_chain(&he->callchain, chain, syms);
440 static size_t perf_session__fprintf_hist_entries(struct perf_session *self,
441 u64 total_samples, FILE *fp)
443 struct hist_entry *pos;
444 struct sort_entry *se;
448 char *col_width = col_width_list_str;
449 int raw_printing_style;
451 raw_printing_style = !strcmp(pretty_printing_style, "raw");
455 fprintf(fp, "# Samples: %Ld\n", (u64)total_samples);
458 fprintf(fp, "# Overhead");
459 if (show_nr_samples) {
461 fprintf(fp, "%cSamples", *field_sep);
463 fputs(" Samples ", fp);
465 list_for_each_entry(se, &hist_entry__sort_list, list) {
469 fprintf(fp, "%c%s", *field_sep, se->header);
472 width = strlen(se->header);
474 if (col_width_list_str) {
476 *se->width = atoi(col_width);
477 col_width = strchr(col_width, ',');
482 width = *se->width = max(*se->width, width);
484 fprintf(fp, " %*s", width, se->header);
491 fprintf(fp, "# ........");
493 fprintf(fp, " ..........");
494 list_for_each_entry(se, &hist_entry__sort_list, list) {
504 width = strlen(se->header);
505 for (i = 0; i < width; i++)
513 for (nd = rb_first(&self->hists); nd; nd = rb_next(nd)) {
514 pos = rb_entry(nd, struct hist_entry, rb_node);
515 ret += hist_entry__fprintf(fp, pos, self, total_samples);
518 if (sort_order == default_sort_order &&
519 parent_pattern == default_parent_pattern) {
521 fprintf(fp, "# (For a higher level overview, try: perf report --sort comm,dso)\n");
526 free(rem_sq_bracket);
529 perf_read_values_display(fp, &show_threads_values,
535 static int validate_chain(struct ip_callchain *chain, event_t *event)
537 unsigned int chain_size;
539 chain_size = event->header.size;
540 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
542 if (chain->nr*sizeof(u64) > chain_size)
548 static int process_sample_event(event_t *event, struct perf_session *session)
550 struct sample_data data;
552 struct addr_location al;
553 struct thread *thread;
555 memset(&data, 0, sizeof(data));
558 event__parse_sample(event, session->sample_type, &data);
560 dump_printf("(IP, %d): %d/%d: %p period: %Ld\n",
563 (void *)(long)data.ip,
564 (long long)data.period);
566 if (session->sample_type & PERF_SAMPLE_CALLCHAIN) {
569 dump_printf("... chain: nr:%Lu\n", data.callchain->nr);
571 if (validate_chain(data.callchain, event) < 0) {
572 pr_debug("call-chain problem with event, "
578 for (i = 0; i < data.callchain->nr; i++)
579 dump_printf("..... %2d: %016Lx\n",
580 i, data.callchain->ips[i]);
584 thread = perf_session__findnew(session, data.pid);
585 if (thread == NULL) {
586 pr_debug("problem processing %d event, skipping it.\n",
591 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
593 if (comm_list && !strlist__has_entry(comm_list, thread->comm))
596 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
598 thread__find_addr_location(thread, session, cpumode,
599 MAP__FUNCTION, data.ip, &al, NULL);
601 * We have to do this here as we may have a dso with no symbol hit that
602 * has a name longer than the ones with symbols sampled.
604 if (al.map && !sort_dso.elide && !al.map->dso->slen_calculated)
605 dso__calc_col_width(al.map->dso);
608 (!al.map || !al.map->dso ||
609 !(strlist__has_entry(dso_list, al.map->dso->short_name) ||
610 (al.map->dso->short_name != al.map->dso->long_name &&
611 strlist__has_entry(dso_list, al.map->dso->long_name)))))
614 if (sym_list && al.sym && !strlist__has_entry(sym_list, al.sym->name))
617 if (perf_session__add_hist_entry(session, &al, data.callchain, data.period)) {
618 pr_debug("problem incrementing symbol count, skipping event\n");
622 session->events_stats.total += data.period;
626 static int process_comm_event(event_t *event, struct perf_session *session)
628 struct thread *thread = perf_session__findnew(session, event->comm.pid);
630 dump_printf(": %s:%d\n", event->comm.comm, event->comm.pid);
632 if (thread == NULL ||
633 thread__set_comm_adjust(thread, event->comm.comm)) {
634 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
641 static int process_read_event(event_t *event, struct perf_session *session __used)
643 struct perf_event_attr *attr;
645 attr = perf_header__find_attr(event->read.id, &session->header);
648 const char *name = attr ? __event_name(attr->type, attr->config)
650 perf_read_values_add_value(&show_threads_values,
651 event->read.pid, event->read.tid,
657 dump_printf(": %d %d %s %Lu\n", event->read.pid, event->read.tid,
658 attr ? __event_name(attr->type, attr->config) : "FAIL",
664 static int sample_type_check(struct perf_session *session)
666 if (!(session->sample_type & PERF_SAMPLE_CALLCHAIN)) {
667 if (sort__has_parent) {
668 fprintf(stderr, "selected --sort parent, but no"
669 " callchain data. Did you call"
670 " perf record without -g?\n");
673 if (session->use_callchain) {
674 fprintf(stderr, "selected -g but no callchain data."
675 " Did you call perf record without"
679 } else if (callchain_param.mode != CHAIN_NONE && !session->use_callchain) {
680 session->use_callchain = true;
681 if (register_callchain_param(&callchain_param) < 0) {
682 fprintf(stderr, "Can't register callchain"
691 static struct perf_event_ops event_ops = {
692 .process_sample_event = process_sample_event,
693 .process_mmap_event = event__process_mmap,
694 .process_comm_event = process_comm_event,
695 .process_exit_event = event__process_task,
696 .process_fork_event = event__process_task,
697 .process_lost_event = event__process_lost,
698 .process_read_event = process_read_event,
699 .sample_type_check = sample_type_check,
703 static int __cmd_report(void)
706 struct perf_session *session;
708 session = perf_session__new(input_name, O_RDONLY, force, &symbol_conf);
712 session->use_callchain = use_callchain;
715 perf_read_values_init(&show_threads_values);
717 ret = perf_session__process_events(session, &event_ops);
722 event__print_totals();
727 perf_session__fprintf(session, stdout);
730 dsos__fprintf(stdout);
732 perf_session__collapse_resort(session);
733 perf_session__output_resort(session, session->events_stats.total);
734 perf_session__fprintf_hist_entries(session, session->events_stats.total, stdout);
737 perf_read_values_destroy(&show_threads_values);
739 perf_session__delete(session);
744 parse_callchain_opt(const struct option *opt __used, const char *arg,
750 use_callchain = true;
755 tok = strtok((char *)arg, ",");
759 /* get the output mode */
760 if (!strncmp(tok, "graph", strlen(arg)))
761 callchain_param.mode = CHAIN_GRAPH_ABS;
763 else if (!strncmp(tok, "flat", strlen(arg)))
764 callchain_param.mode = CHAIN_FLAT;
766 else if (!strncmp(tok, "fractal", strlen(arg)))
767 callchain_param.mode = CHAIN_GRAPH_REL;
769 else if (!strncmp(tok, "none", strlen(arg))) {
770 callchain_param.mode = CHAIN_NONE;
771 use_callchain = true;
779 /* get the min percentage */
780 tok = strtok(NULL, ",");
784 callchain_param.min_percent = strtod(tok, &endptr);
789 if (register_callchain_param(&callchain_param) < 0) {
790 fprintf(stderr, "Can't register callchain params\n");
796 //static const char * const report_usage[] = {
797 const char * const report_usage[] = {
798 "perf report [<options>] <command>",
802 static const struct option options[] = {
803 OPT_STRING('i', "input", &input_name, "file",
805 OPT_BOOLEAN('v', "verbose", &verbose,
806 "be more verbose (show symbol address, etc)"),
807 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
808 "dump raw trace in ASCII"),
809 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
810 "file", "vmlinux pathname"),
811 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
812 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
813 "load module symbols - WARNING: use only with -k and LIVE kernel"),
814 OPT_BOOLEAN('n', "show-nr-samples", &show_nr_samples,
815 "Show a column with the number of samples"),
816 OPT_BOOLEAN('T', "threads", &show_threads,
817 "Show per-thread event counters"),
818 OPT_STRING(0, "pretty", &pretty_printing_style, "key",
819 "pretty printing style key: normal raw"),
820 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
821 "sort by key(s): pid, comm, dso, symbol, parent"),
822 OPT_BOOLEAN('P', "full-paths", &event_ops.full_paths,
823 "Don't shorten the pathnames taking into account the cwd"),
824 OPT_STRING('p', "parent", &parent_pattern, "regex",
825 "regex filter to identify parent, see: '--sort parent'"),
826 OPT_BOOLEAN('x', "exclude-other", &exclude_other,
827 "Only display entries with parent-match"),
828 OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
829 "Display callchains using output_type and min percent threshold. "
830 "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
831 OPT_STRING('d', "dsos", &dso_list_str, "dso[,dso...]",
832 "only consider symbols in these dsos"),
833 OPT_STRING('C', "comms", &comm_list_str, "comm[,comm...]",
834 "only consider symbols in these comms"),
835 OPT_STRING('S', "symbols", &sym_list_str, "symbol[,symbol...]",
836 "only consider these symbols"),
837 OPT_STRING('w', "column-widths", &col_width_list_str,
839 "don't try to adjust column width, use these fixed values"),
840 OPT_STRING('t', "field-separator", &field_sep, "separator",
841 "separator for columns, no spaces will be added between "
842 "columns '.' is reserved."),
846 static void setup_sorting(void)
848 char *tmp, *tok, *str = strdup(sort_order);
850 for (tok = strtok_r(str, ", ", &tmp);
851 tok; tok = strtok_r(NULL, ", ", &tmp)) {
852 if (sort_dimension__add(tok) < 0) {
853 error("Unknown --sort key: `%s'", tok);
854 usage_with_options(report_usage, options);
861 static void setup_list(struct strlist **list, const char *list_str,
862 struct sort_entry *se, const char *list_name,
866 *list = strlist__new(true, list_str);
868 fprintf(stderr, "problems parsing %s list\n",
872 if (strlist__nr_entries(*list) == 1) {
873 fprintf(fp, "# %s: %s\n", list_name,
874 strlist__entry(*list, 0)->s);
880 int cmd_report(int argc, const char **argv, const char *prefix __used)
882 if (symbol__init(&symbol_conf) < 0)
885 argc = parse_options(argc, argv, options, report_usage, 0);
889 if (parent_pattern != default_parent_pattern) {
890 sort_dimension__add("parent");
891 sort_parent.elide = 1;
896 * Any (unrecognized) arguments left?
899 usage_with_options(report_usage, options);
903 setup_list(&dso_list, dso_list_str, &sort_dso, "dso", stdout);
904 setup_list(&comm_list, comm_list_str, &sort_comm, "comm", stdout);
905 setup_list(&sym_list, sym_list_str, &sort_sym, "symbol", stdout);
907 if (field_sep && *field_sep == '.') {
908 fputs("'.' is the only non valid --field-separator argument\n",
913 return __cmd_report();