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"
26 #include "util/parse-options.h"
27 #include "util/parse-events.h"
29 #include "util/thread.h"
31 static char const *input_name = "perf.data";
33 static char default_sort_order[] = "comm,dso,symbol";
34 static char *sort_order = default_sort_order;
35 static char *dso_list_str, *comm_list_str, *sym_list_str,
37 static struct strlist *dso_list, *comm_list, *sym_list;
38 static char *field_sep;
42 static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
44 static int full_paths;
45 static int show_nr_samples;
47 static int show_threads;
48 static struct perf_read_values show_threads_values;
50 static char default_pretty_printing_style[] = "normal";
51 static char *pretty_printing_style = default_pretty_printing_style;
53 static unsigned long page_size;
54 static unsigned long mmap_window = 32;
56 static char default_parent_pattern[] = "^sys_|^do_page_fault";
57 static char *parent_pattern = default_parent_pattern;
58 static regex_t parent_regex;
60 static int exclude_other = 1;
62 static char callchain_default_opt[] = "fractal,0.5";
66 static char __cwd[PATH_MAX];
67 static char *cwd = __cwd;
70 static struct rb_root threads;
71 static struct thread *last_match;
73 static struct perf_header *header;
76 struct callchain_param callchain_param = {
77 .mode = CHAIN_GRAPH_REL,
81 static u64 sample_type;
83 static int repsep_fprintf(FILE *fp, const char *fmt, ...)
90 n = vfprintf(fp, fmt, ap);
93 n = vasprintf(&bf, fmt, ap);
98 sep = strchr(sep, *field_sep);
111 static unsigned int dsos__col_width,
116 * histogram, sorted on item, collects counts
119 static struct rb_root hist;
122 struct rb_node rb_node;
124 struct thread *thread;
128 struct symbol *parent;
131 struct callchain_node callchain;
132 struct rb_root sorted_chain;
138 * configurable sorting bits
142 struct list_head list;
146 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
147 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
148 size_t (*print)(FILE *fp, struct hist_entry *, unsigned int width);
153 static int64_t cmp_null(void *l, void *r)
166 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
168 return right->thread->pid - left->thread->pid;
172 sort__thread_print(FILE *fp, struct hist_entry *self, unsigned int width)
174 return repsep_fprintf(fp, "%*s:%5d", width - 6,
175 self->thread->comm ?: "", self->thread->pid);
178 static struct sort_entry sort_thread = {
179 .header = "Command: Pid",
180 .cmp = sort__thread_cmp,
181 .print = sort__thread_print,
182 .width = &threads__col_width,
188 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
190 return right->thread->pid - left->thread->pid;
194 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
196 char *comm_l = left->thread->comm;
197 char *comm_r = right->thread->comm;
199 if (!comm_l || !comm_r)
200 return cmp_null(comm_l, comm_r);
202 return strcmp(comm_l, comm_r);
206 sort__comm_print(FILE *fp, struct hist_entry *self, unsigned int width)
208 return repsep_fprintf(fp, "%*s", width, self->thread->comm);
211 static struct sort_entry sort_comm = {
213 .cmp = sort__comm_cmp,
214 .collapse = sort__comm_collapse,
215 .print = sort__comm_print,
216 .width = &comms__col_width,
222 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
224 struct dso *dso_l = left->dso;
225 struct dso *dso_r = right->dso;
227 if (!dso_l || !dso_r)
228 return cmp_null(dso_l, dso_r);
230 return strcmp(dso_l->name, dso_r->name);
234 sort__dso_print(FILE *fp, struct hist_entry *self, unsigned int width)
237 return repsep_fprintf(fp, "%-*s", width, self->dso->name);
239 return repsep_fprintf(fp, "%*llx", width, (u64)self->ip);
242 static struct sort_entry sort_dso = {
243 .header = "Shared Object",
244 .cmp = sort__dso_cmp,
245 .print = sort__dso_print,
246 .width = &dsos__col_width,
252 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
256 if (left->sym == right->sym)
259 ip_l = left->sym ? left->sym->start : left->ip;
260 ip_r = right->sym ? right->sym->start : right->ip;
262 return (int64_t)(ip_r - ip_l);
266 sort__sym_print(FILE *fp, struct hist_entry *self, unsigned int width __used)
271 ret += repsep_fprintf(fp, "%#018llx %c ", (u64)self->ip,
272 dso__symtab_origin(self->dso));
274 ret += repsep_fprintf(fp, "[%c] ", self->level);
276 ret += repsep_fprintf(fp, "%s", self->sym->name);
278 if (self->sym->module)
279 ret += repsep_fprintf(fp, "\t[%s]",
280 self->sym->module->name);
282 ret += repsep_fprintf(fp, "%#016llx", (u64)self->ip);
288 static struct sort_entry sort_sym = {
290 .cmp = sort__sym_cmp,
291 .print = sort__sym_print,
297 sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
299 struct symbol *sym_l = left->parent;
300 struct symbol *sym_r = right->parent;
302 if (!sym_l || !sym_r)
303 return cmp_null(sym_l, sym_r);
305 return strcmp(sym_l->name, sym_r->name);
309 sort__parent_print(FILE *fp, struct hist_entry *self, unsigned int width)
311 return repsep_fprintf(fp, "%-*s", width,
312 self->parent ? self->parent->name : "[other]");
315 static unsigned int parent_symbol__col_width;
317 static struct sort_entry sort_parent = {
318 .header = "Parent symbol",
319 .cmp = sort__parent_cmp,
320 .print = sort__parent_print,
321 .width = &parent_symbol__col_width,
324 static int sort__need_collapse = 0;
325 static int sort__has_parent = 0;
327 struct sort_dimension {
329 struct sort_entry *entry;
333 static struct sort_dimension sort_dimensions[] = {
334 { .name = "pid", .entry = &sort_thread, },
335 { .name = "comm", .entry = &sort_comm, },
336 { .name = "dso", .entry = &sort_dso, },
337 { .name = "symbol", .entry = &sort_sym, },
338 { .name = "parent", .entry = &sort_parent, },
341 static LIST_HEAD(hist_entry__sort_list);
343 static int sort_dimension__add(const char *tok)
347 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
348 struct sort_dimension *sd = &sort_dimensions[i];
353 if (strncasecmp(tok, sd->name, strlen(tok)))
356 if (sd->entry->collapse)
357 sort__need_collapse = 1;
359 if (sd->entry == &sort_parent) {
360 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
364 regerror(ret, &parent_regex, err, sizeof(err));
365 fprintf(stderr, "Invalid regex: %s\n%s",
366 parent_pattern, err);
369 sort__has_parent = 1;
372 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
382 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
384 struct sort_entry *se;
387 list_for_each_entry(se, &hist_entry__sort_list, list) {
388 cmp = se->cmp(left, right);
397 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
399 struct sort_entry *se;
402 list_for_each_entry(se, &hist_entry__sort_list, list) {
403 int64_t (*f)(struct hist_entry *, struct hist_entry *);
405 f = se->collapse ?: se->cmp;
407 cmp = f(left, right);
415 static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask)
420 ret += fprintf(fp, "%s", " ");
422 for (i = 0; i < depth; i++)
423 if (depth_mask & (1 << i))
424 ret += fprintf(fp, "| ");
426 ret += fprintf(fp, " ");
428 ret += fprintf(fp, "\n");
433 ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, int depth,
434 int depth_mask, int count, u64 total_samples,
440 ret += fprintf(fp, "%s", " ");
441 for (i = 0; i < depth; i++) {
442 if (depth_mask & (1 << i))
443 ret += fprintf(fp, "|");
445 ret += fprintf(fp, " ");
446 if (!count && i == depth - 1) {
449 percent = hits * 100.0 / total_samples;
450 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
452 ret += fprintf(fp, "%s", " ");
455 ret += fprintf(fp, "%s\n", chain->sym->name);
457 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
462 static struct symbol *rem_sq_bracket;
463 static struct callchain_list rem_hits;
465 static void init_rem_hits(void)
467 rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
468 if (!rem_sq_bracket) {
469 fprintf(stderr, "Not enough memory to display remaining hits\n");
473 strcpy(rem_sq_bracket->name, "[...]");
474 rem_hits.sym = rem_sq_bracket;
478 callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
479 u64 total_samples, int depth, int depth_mask)
481 struct rb_node *node, *next;
482 struct callchain_node *child;
483 struct callchain_list *chain;
484 int new_depth_mask = depth_mask;
490 if (callchain_param.mode == CHAIN_GRAPH_REL)
491 new_total = self->children_hit;
493 new_total = total_samples;
495 remaining = new_total;
497 node = rb_first(&self->rb_root);
501 child = rb_entry(node, struct callchain_node, rb_node);
502 cumul = cumul_hits(child);
506 * The depth mask manages the output of pipes that show
507 * the depth. We don't want to keep the pipes of the current
508 * level for the last child of this depth.
509 * Except if we have remaining filtered hits. They will
510 * supersede the last child
512 next = rb_next(node);
513 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
514 new_depth_mask &= ~(1 << (depth - 1));
517 * But we keep the older depth mask for the line seperator
518 * to keep the level link until we reach the last child
520 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask);
522 list_for_each_entry(chain, &child->val, list) {
523 if (chain->ip >= PERF_CONTEXT_MAX)
525 ret += ipchain__fprintf_graph(fp, chain, depth,
530 ret += callchain__fprintf_graph(fp, child, new_total,
532 new_depth_mask | (1 << depth));
536 if (callchain_param.mode == CHAIN_GRAPH_REL &&
537 remaining && remaining != new_total) {
542 new_depth_mask &= ~(1 << (depth - 1));
544 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
545 new_depth_mask, 0, new_total,
553 callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
556 struct callchain_list *chain;
562 ret += callchain__fprintf_flat(fp, self->parent, total_samples);
565 list_for_each_entry(chain, &self->val, list) {
566 if (chain->ip >= PERF_CONTEXT_MAX)
569 ret += fprintf(fp, " %s\n", chain->sym->name);
571 ret += fprintf(fp, " %p\n",
572 (void *)(long)chain->ip);
579 hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
582 struct rb_node *rb_node;
583 struct callchain_node *chain;
586 rb_node = rb_first(&self->sorted_chain);
590 chain = rb_entry(rb_node, struct callchain_node, rb_node);
591 percent = chain->hit * 100.0 / total_samples;
592 switch (callchain_param.mode) {
594 ret += percent_color_fprintf(fp, " %6.2f%%\n",
596 ret += callchain__fprintf_flat(fp, chain, total_samples);
598 case CHAIN_GRAPH_ABS: /* Falldown */
599 case CHAIN_GRAPH_REL:
600 ret += callchain__fprintf_graph(fp, chain,
601 total_samples, 1, 1);
606 ret += fprintf(fp, "\n");
607 rb_node = rb_next(rb_node);
615 hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
617 struct sort_entry *se;
620 if (exclude_other && !self->parent)
624 ret = percent_color_fprintf(fp,
625 field_sep ? "%.2f" : " %6.2f%%",
626 (self->count * 100.0) / total_samples);
628 ret = fprintf(fp, field_sep ? "%lld" : "%12lld ", self->count);
630 if (show_nr_samples) {
632 fprintf(fp, "%c%lld", *field_sep, self->count);
634 fprintf(fp, "%11lld", self->count);
637 list_for_each_entry(se, &hist_entry__sort_list, list) {
641 fprintf(fp, "%s", field_sep ?: " ");
642 ret += se->print(fp, self, se->width ? *se->width : 0);
645 ret += fprintf(fp, "\n");
648 hist_entry_callchain__fprintf(fp, self, total_samples);
657 static void dso__calc_col_width(struct dso *self)
659 if (!col_width_list_str && !field_sep &&
660 (!dso_list || strlist__has_entry(dso_list, self->name))) {
661 unsigned int slen = strlen(self->name);
662 if (slen > dsos__col_width)
663 dsos__col_width = slen;
666 self->slen_calculated = 1;
669 static int thread__set_comm_adjust(struct thread *self, const char *comm)
671 int ret = thread__set_comm(self, comm);
676 if (!col_width_list_str && !field_sep &&
677 (!comm_list || strlist__has_entry(comm_list, comm))) {
678 unsigned int slen = strlen(comm);
680 if (slen > comms__col_width) {
681 comms__col_width = slen;
682 threads__col_width = slen + 6;
690 static struct symbol *
691 resolve_symbol(struct thread *thread, struct map **mapp,
692 struct dso **dsop, u64 *ipp)
694 struct dso *dso = dsop ? *dsop : NULL;
695 struct map *map = mapp ? *mapp : NULL;
707 map = thread__find_map(thread, ip);
710 * We have to do this here as we may have a dso
711 * with no symbol hit that has a name longer than
712 * the ones with symbols sampled.
714 if (!sort_dso.elide && !map->dso->slen_calculated)
715 dso__calc_col_width(map->dso);
720 ip = map->map_ip(map, ip);
725 * If this is outside of all known maps,
726 * and is a negative address, try to look it
727 * up in the kernel dso, as it might be a
728 * vsyscall (which executes in user-mode):
730 if ((long long)ip < 0)
733 dump_printf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
734 dump_printf(" ...... map: %Lx -> %Lx\n", *ipp, ip);
743 return dso->find_symbol(dso, ip);
746 static int call__match(struct symbol *sym)
748 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
754 static struct symbol **
755 resolve_callchain(struct thread *thread, struct map *map __used,
756 struct ip_callchain *chain, struct hist_entry *entry)
758 u64 context = PERF_CONTEXT_MAX;
759 struct symbol **syms = NULL;
763 syms = calloc(chain->nr, sizeof(*syms));
765 fprintf(stderr, "Can't allocate memory for symbols\n");
770 for (i = 0; i < chain->nr; i++) {
771 u64 ip = chain->ips[i];
772 struct dso *dso = NULL;
775 if (ip >= PERF_CONTEXT_MAX) {
781 case PERF_CONTEXT_HV:
782 dso = hypervisor_dso;
784 case PERF_CONTEXT_KERNEL:
791 sym = resolve_symbol(thread, NULL, &dso, &ip);
794 if (sort__has_parent && call__match(sym) &&
807 * collect histogram counts
811 hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
812 struct symbol *sym, u64 ip, struct ip_callchain *chain,
813 char level, u64 count)
815 struct rb_node **p = &hist.rb_node;
816 struct rb_node *parent = NULL;
817 struct hist_entry *he;
818 struct symbol **syms = NULL;
819 struct hist_entry entry = {
828 .sorted_chain = RB_ROOT
832 if ((sort__has_parent || callchain) && chain)
833 syms = resolve_callchain(thread, map, chain, &entry);
837 he = rb_entry(parent, struct hist_entry, rb_node);
839 cmp = hist_entry__cmp(&entry, he);
844 append_chain(&he->callchain, chain, syms);
856 he = malloc(sizeof(*he));
861 callchain_init(&he->callchain);
862 append_chain(&he->callchain, chain, syms);
865 rb_link_node(&he->rb_node, parent, p);
866 rb_insert_color(&he->rb_node, &hist);
871 static void hist_entry__free(struct hist_entry *he)
877 * collapse the histogram
880 static struct rb_root collapse_hists;
882 static void collapse__insert_entry(struct hist_entry *he)
884 struct rb_node **p = &collapse_hists.rb_node;
885 struct rb_node *parent = NULL;
886 struct hist_entry *iter;
891 iter = rb_entry(parent, struct hist_entry, rb_node);
893 cmp = hist_entry__collapse(iter, he);
896 iter->count += he->count;
897 hist_entry__free(he);
907 rb_link_node(&he->rb_node, parent, p);
908 rb_insert_color(&he->rb_node, &collapse_hists);
911 static void collapse__resort(void)
913 struct rb_node *next;
914 struct hist_entry *n;
916 if (!sort__need_collapse)
919 next = rb_first(&hist);
921 n = rb_entry(next, struct hist_entry, rb_node);
922 next = rb_next(&n->rb_node);
924 rb_erase(&n->rb_node, &hist);
925 collapse__insert_entry(n);
930 * reverse the map, sort on count.
933 static struct rb_root output_hists;
935 static void output__insert_entry(struct hist_entry *he, u64 min_callchain_hits)
937 struct rb_node **p = &output_hists.rb_node;
938 struct rb_node *parent = NULL;
939 struct hist_entry *iter;
942 callchain_param.sort(&he->sorted_chain, &he->callchain,
943 min_callchain_hits, &callchain_param);
947 iter = rb_entry(parent, struct hist_entry, rb_node);
949 if (he->count > iter->count)
955 rb_link_node(&he->rb_node, parent, p);
956 rb_insert_color(&he->rb_node, &output_hists);
959 static void output__resort(u64 total_samples)
961 struct rb_node *next;
962 struct hist_entry *n;
963 struct rb_root *tree = &hist;
964 u64 min_callchain_hits;
966 min_callchain_hits = total_samples * (callchain_param.min_percent / 100);
968 if (sort__need_collapse)
969 tree = &collapse_hists;
971 next = rb_first(tree);
974 n = rb_entry(next, struct hist_entry, rb_node);
975 next = rb_next(&n->rb_node);
977 rb_erase(&n->rb_node, tree);
978 output__insert_entry(n, min_callchain_hits);
982 static size_t output__fprintf(FILE *fp, u64 total_samples)
984 struct hist_entry *pos;
985 struct sort_entry *se;
989 char *col_width = col_width_list_str;
990 int raw_printing_style;
992 raw_printing_style = !strcmp(pretty_printing_style, "raw");
996 fprintf(fp, "# Samples: %Ld\n", (u64)total_samples);
999 fprintf(fp, "# Overhead");
1000 if (show_nr_samples) {
1002 fprintf(fp, "%cSamples", *field_sep);
1004 fputs(" Samples ", fp);
1006 list_for_each_entry(se, &hist_entry__sort_list, list) {
1010 fprintf(fp, "%c%s", *field_sep, se->header);
1013 width = strlen(se->header);
1015 if (col_width_list_str) {
1017 *se->width = atoi(col_width);
1018 col_width = strchr(col_width, ',');
1023 width = *se->width = max(*se->width, width);
1025 fprintf(fp, " %*s", width, se->header);
1032 fprintf(fp, "# ........");
1033 if (show_nr_samples)
1034 fprintf(fp, " ..........");
1035 list_for_each_entry(se, &hist_entry__sort_list, list) {
1045 width = strlen(se->header);
1046 for (i = 0; i < width; i++)
1054 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
1055 pos = rb_entry(nd, struct hist_entry, rb_node);
1056 ret += hist_entry__fprintf(fp, pos, total_samples);
1059 if (sort_order == default_sort_order &&
1060 parent_pattern == default_parent_pattern) {
1062 fprintf(fp, "# (For a higher level overview, try: perf report --sort comm,dso)\n");
1067 free(rem_sq_bracket);
1070 perf_read_values_display(fp, &show_threads_values,
1071 raw_printing_style);
1076 static void register_idle_thread(void)
1078 struct thread *thread = threads__findnew(0, &threads, &last_match);
1080 if (thread == NULL ||
1081 thread__set_comm_adjust(thread, "[idle]")) {
1082 fprintf(stderr, "problem inserting idle task.\n");
1087 static unsigned long total = 0,
1094 static int validate_chain(struct ip_callchain *chain, event_t *event)
1096 unsigned int chain_size;
1098 chain_size = event->header.size;
1099 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
1101 if (chain->nr*sizeof(u64) > chain_size)
1108 process_sample_event(event_t *event, unsigned long offset, unsigned long head)
1112 struct dso *dso = NULL;
1113 struct thread *thread;
1114 u64 ip = event->ip.ip;
1116 struct map *map = NULL;
1117 void *more_data = event->ip.__more_data;
1118 struct ip_callchain *chain = NULL;
1121 thread = threads__findnew(event->ip.pid, &threads, &last_match);
1123 if (sample_type & PERF_SAMPLE_PERIOD) {
1124 period = *(u64 *)more_data;
1125 more_data += sizeof(u64);
1128 dump_printf("%p [%p]: PERF_EVENT_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
1129 (void *)(offset + head),
1130 (void *)(long)(event->header.size),
1132 event->ip.pid, event->ip.tid,
1136 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
1139 chain = (void *)more_data;
1141 dump_printf("... chain: nr:%Lu\n", chain->nr);
1143 if (validate_chain(chain, event) < 0) {
1144 eprintf("call-chain problem with event, skipping it.\n");
1149 for (i = 0; i < chain->nr; i++)
1150 dump_printf("..... %2d: %016Lx\n", i, chain->ips[i]);
1154 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1156 if (thread == NULL) {
1157 eprintf("problem processing %d event, skipping it.\n",
1158 event->header.type);
1162 if (comm_list && !strlist__has_entry(comm_list, thread->comm))
1165 cpumode = event->header.misc & PERF_EVENT_MISC_CPUMODE_MASK;
1167 if (cpumode == PERF_EVENT_MISC_KERNEL) {
1173 dump_printf(" ...... dso: %s\n", dso->name);
1175 } else if (cpumode == PERF_EVENT_MISC_USER) {
1184 dso = hypervisor_dso;
1186 dump_printf(" ...... dso: [hypervisor]\n");
1189 if (show & show_mask) {
1190 struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
1192 if (dso_list && (!dso || !dso->name ||
1193 !strlist__has_entry(dso_list, dso->name)))
1196 if (sym_list && (!sym || !strlist__has_entry(sym_list, sym->name)))
1199 if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
1200 eprintf("problem incrementing symbol count, skipping event\n");
1210 process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
1212 struct thread *thread;
1213 struct map *map = map__new(&event->mmap, cwd, cwdlen);
1215 thread = threads__findnew(event->mmap.pid, &threads, &last_match);
1217 dump_printf("%p [%p]: PERF_EVENT_MMAP %d/%d: [%p(%p) @ %p]: %s\n",
1218 (void *)(offset + head),
1219 (void *)(long)(event->header.size),
1222 (void *)(long)event->mmap.start,
1223 (void *)(long)event->mmap.len,
1224 (void *)(long)event->mmap.pgoff,
1225 event->mmap.filename);
1227 if (thread == NULL || map == NULL) {
1228 dump_printf("problem processing PERF_EVENT_MMAP, skipping event.\n");
1232 thread__insert_map(thread, map);
1239 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
1241 struct thread *thread;
1243 thread = threads__findnew(event->comm.pid, &threads, &last_match);
1245 dump_printf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
1246 (void *)(offset + head),
1247 (void *)(long)(event->header.size),
1248 event->comm.comm, event->comm.pid);
1250 if (thread == NULL ||
1251 thread__set_comm_adjust(thread, event->comm.comm)) {
1252 dump_printf("problem processing PERF_EVENT_COMM, skipping event.\n");
1261 process_task_event(event_t *event, unsigned long offset, unsigned long head)
1263 struct thread *thread;
1264 struct thread *parent;
1266 thread = threads__findnew(event->fork.pid, &threads, &last_match);
1267 parent = threads__findnew(event->fork.ppid, &threads, &last_match);
1269 dump_printf("%p [%p]: PERF_EVENT_%s: (%d:%d):(%d:%d)\n",
1270 (void *)(offset + head),
1271 (void *)(long)(event->header.size),
1272 event->header.type == PERF_EVENT_FORK ? "FORK" : "EXIT",
1273 event->fork.pid, event->fork.tid,
1274 event->fork.ppid, event->fork.ptid);
1277 * A thread clone will have the same PID for both
1280 if (thread == parent)
1283 if (event->header.type == PERF_EVENT_EXIT)
1286 if (!thread || !parent || thread__fork(thread, parent)) {
1287 dump_printf("problem processing PERF_EVENT_FORK, skipping event.\n");
1296 process_lost_event(event_t *event, unsigned long offset, unsigned long head)
1298 dump_printf("%p [%p]: PERF_EVENT_LOST: id:%Ld: lost:%Ld\n",
1299 (void *)(offset + head),
1300 (void *)(long)(event->header.size),
1304 total_lost += event->lost.lost;
1310 process_read_event(event_t *event, unsigned long offset, unsigned long head)
1312 struct perf_counter_attr *attr;
1314 attr = perf_header__find_attr(event->read.id, header);
1317 const char *name = attr ? __event_name(attr->type, attr->config)
1319 perf_read_values_add_value(&show_threads_values,
1320 event->read.pid, event->read.tid,
1326 dump_printf("%p [%p]: PERF_EVENT_READ: %d %d %s %Lu\n",
1327 (void *)(offset + head),
1328 (void *)(long)(event->header.size),
1331 attr ? __event_name(attr->type, attr->config)
1339 process_event(event_t *event, unsigned long offset, unsigned long head)
1343 switch (event->header.type) {
1344 case PERF_EVENT_SAMPLE:
1345 return process_sample_event(event, offset, head);
1347 case PERF_EVENT_MMAP:
1348 return process_mmap_event(event, offset, head);
1350 case PERF_EVENT_COMM:
1351 return process_comm_event(event, offset, head);
1353 case PERF_EVENT_FORK:
1354 case PERF_EVENT_EXIT:
1355 return process_task_event(event, offset, head);
1357 case PERF_EVENT_LOST:
1358 return process_lost_event(event, offset, head);
1360 case PERF_EVENT_READ:
1361 return process_read_event(event, offset, head);
1364 * We dont process them right now but they are fine:
1367 case PERF_EVENT_THROTTLE:
1368 case PERF_EVENT_UNTHROTTLE:
1378 static int __cmd_report(void)
1380 int ret, rc = EXIT_FAILURE;
1381 unsigned long offset = 0;
1382 unsigned long head, shift;
1383 struct stat input_stat;
1388 register_idle_thread();
1391 perf_read_values_init(&show_threads_values);
1393 input = open(input_name, O_RDONLY);
1395 fprintf(stderr, " failed to open file: %s", input_name);
1396 if (!strcmp(input_name, "perf.data"))
1397 fprintf(stderr, " (try 'perf record' first)");
1398 fprintf(stderr, "\n");
1402 ret = fstat(input, &input_stat);
1404 perror("failed to stat file");
1408 if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
1409 fprintf(stderr, "file: %s not owned by current user or root\n", input_name);
1413 if (!input_stat.st_size) {
1414 fprintf(stderr, "zero-sized file, nothing to do!\n");
1418 header = perf_header__read(input);
1419 head = header->data_offset;
1421 sample_type = perf_header__sample_type(header);
1423 if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
1424 if (sort__has_parent) {
1425 fprintf(stderr, "selected --sort parent, but no"
1426 " callchain data. Did you call"
1427 " perf record without -g?\n");
1431 fprintf(stderr, "selected -c but no callchain data."
1432 " Did you call perf record without"
1436 } else if (callchain_param.mode != CHAIN_NONE && !callchain) {
1438 if (register_callchain_param(&callchain_param) < 0) {
1439 fprintf(stderr, "Can't register callchain"
1445 if (load_kernel() < 0) {
1446 perror("failed to load kernel symbols");
1447 return EXIT_FAILURE;
1451 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
1452 perror("failed to get the current directory");
1453 return EXIT_FAILURE;
1455 cwdlen = strlen(cwd);
1461 shift = page_size * (head / page_size);
1466 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1467 MAP_SHARED, input, offset);
1468 if (buf == MAP_FAILED) {
1469 perror("failed to mmap file");
1474 event = (event_t *)(buf + head);
1476 size = event->header.size;
1480 if (head + event->header.size >= page_size * mmap_window) {
1483 shift = page_size * (head / page_size);
1485 munmap_ret = munmap(buf, page_size * mmap_window);
1486 assert(munmap_ret == 0);
1493 size = event->header.size;
1495 dump_printf("\n%p [%p]: event: %d\n",
1496 (void *)(offset + head),
1497 (void *)(long)event->header.size,
1498 event->header.type);
1500 if (!size || process_event(event, offset, head) < 0) {
1502 dump_printf("%p [%p]: skipping unknown header type: %d\n",
1503 (void *)(offset + head),
1504 (void *)(long)(event->header.size),
1505 event->header.type);
1510 * assume we lost track of the stream, check alignment, and
1511 * increment a single u64 in the hope to catch on again 'soon'.
1514 if (unlikely(head & 7))
1522 if (offset + head >= header->data_offset + header->data_size)
1525 if (offset + head < (unsigned long)input_stat.st_size)
1532 dump_printf(" IP events: %10ld\n", total);
1533 dump_printf(" mmap events: %10ld\n", total_mmap);
1534 dump_printf(" comm events: %10ld\n", total_comm);
1535 dump_printf(" fork events: %10ld\n", total_fork);
1536 dump_printf(" lost events: %10ld\n", total_lost);
1537 dump_printf(" unknown events: %10ld\n", total_unknown);
1543 threads__fprintf(stdout, &threads);
1546 dsos__fprintf(stdout);
1549 output__resort(total);
1550 output__fprintf(stdout, total);
1553 perf_read_values_destroy(&show_threads_values);
1559 parse_callchain_opt(const struct option *opt __used, const char *arg,
1570 tok = strtok((char *)arg, ",");
1574 /* get the output mode */
1575 if (!strncmp(tok, "graph", strlen(arg)))
1576 callchain_param.mode = CHAIN_GRAPH_ABS;
1578 else if (!strncmp(tok, "flat", strlen(arg)))
1579 callchain_param.mode = CHAIN_FLAT;
1581 else if (!strncmp(tok, "fractal", strlen(arg)))
1582 callchain_param.mode = CHAIN_GRAPH_REL;
1584 else if (!strncmp(tok, "none", strlen(arg))) {
1585 callchain_param.mode = CHAIN_NONE;
1594 /* get the min percentage */
1595 tok = strtok(NULL, ",");
1599 callchain_param.min_percent = strtod(tok, &endptr);
1604 if (register_callchain_param(&callchain_param) < 0) {
1605 fprintf(stderr, "Can't register callchain params\n");
1611 static const char * const report_usage[] = {
1612 "perf report [<options>] <command>",
1616 static const struct option options[] = {
1617 OPT_STRING('i', "input", &input_name, "file",
1619 OPT_BOOLEAN('v', "verbose", &verbose,
1620 "be more verbose (show symbol address, etc)"),
1621 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1622 "dump raw trace in ASCII"),
1623 OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
1624 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
1625 OPT_BOOLEAN('m', "modules", &modules,
1626 "load module symbols - WARNING: use only with -k and LIVE kernel"),
1627 OPT_BOOLEAN('n', "show-nr-samples", &show_nr_samples,
1628 "Show a column with the number of samples"),
1629 OPT_BOOLEAN('T', "threads", &show_threads,
1630 "Show per-thread event counters"),
1631 OPT_STRING(0, "pretty", &pretty_printing_style, "key",
1632 "pretty printing style key: normal raw"),
1633 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1634 "sort by key(s): pid, comm, dso, symbol, parent"),
1635 OPT_BOOLEAN('P', "full-paths", &full_paths,
1636 "Don't shorten the pathnames taking into account the cwd"),
1637 OPT_STRING('p', "parent", &parent_pattern, "regex",
1638 "regex filter to identify parent, see: '--sort parent'"),
1639 OPT_BOOLEAN('x', "exclude-other", &exclude_other,
1640 "Only display entries with parent-match"),
1641 OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
1642 "Display callchains using output_type and min percent threshold. "
1643 "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
1644 OPT_STRING('d', "dsos", &dso_list_str, "dso[,dso...]",
1645 "only consider symbols in these dsos"),
1646 OPT_STRING('C', "comms", &comm_list_str, "comm[,comm...]",
1647 "only consider symbols in these comms"),
1648 OPT_STRING('S', "symbols", &sym_list_str, "symbol[,symbol...]",
1649 "only consider these symbols"),
1650 OPT_STRING('w', "column-widths", &col_width_list_str,
1652 "don't try to adjust column width, use these fixed values"),
1653 OPT_STRING('t', "field-separator", &field_sep, "separator",
1654 "separator for columns, no spaces will be added between "
1655 "columns '.' is reserved."),
1659 static void setup_sorting(void)
1661 char *tmp, *tok, *str = strdup(sort_order);
1663 for (tok = strtok_r(str, ", ", &tmp);
1664 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1665 if (sort_dimension__add(tok) < 0) {
1666 error("Unknown --sort key: `%s'", tok);
1667 usage_with_options(report_usage, options);
1674 static void setup_list(struct strlist **list, const char *list_str,
1675 struct sort_entry *se, const char *list_name,
1679 *list = strlist__new(true, list_str);
1681 fprintf(stderr, "problems parsing %s list\n",
1685 if (strlist__nr_entries(*list) == 1) {
1686 fprintf(fp, "# %s: %s\n", list_name,
1687 strlist__entry(*list, 0)->s);
1693 int cmd_report(int argc, const char **argv, const char *prefix __used)
1697 page_size = getpagesize();
1699 argc = parse_options(argc, argv, options, report_usage, 0);
1703 if (parent_pattern != default_parent_pattern) {
1704 sort_dimension__add("parent");
1705 sort_parent.elide = 1;
1710 * Any (unrecognized) arguments left?
1713 usage_with_options(report_usage, options);
1717 setup_list(&dso_list, dso_list_str, &sort_dso, "dso", stdout);
1718 setup_list(&comm_list, comm_list_str, &sort_comm, "comm", stdout);
1719 setup_list(&sym_list, sym_list_str, &sort_sym, "symbol", stdout);
1721 if (field_sep && *field_sep == '.') {
1722 fputs("'.' is the only non valid --field-separator argument\n",
1727 return __cmd_report();