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"
22 #include "util/header.h"
24 #include "util/parse-options.h"
25 #include "util/parse-events.h"
31 static char const *input_name = "perf.data";
32 static char *vmlinux = NULL;
34 static char default_sort_order[] = "comm,dso";
35 static char *sort_order = default_sort_order;
36 static char *dso_list_str, *comm_list_str, *sym_list_str,
38 static struct strlist *dso_list, *comm_list, *sym_list;
39 static char *field_sep;
42 static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
44 static int dump_trace = 0;
45 #define dprintf(x...) do { if (dump_trace) printf(x); } while (0)
46 #define cdprintf(x...) do { if (dump_trace) color_fprintf(stdout, color, x); } while (0)
49 #define eprintf(x...) do { if (verbose) fprintf(stderr, x); } while (0)
53 static int full_paths;
54 static int show_nr_samples;
56 static unsigned long page_size;
57 static unsigned long mmap_window = 32;
59 static char default_parent_pattern[] = "^sys_|^do_page_fault";
60 static char *parent_pattern = default_parent_pattern;
61 static regex_t parent_regex;
63 static int exclude_other = 1;
65 static char callchain_default_opt[] = "fractal,0.5";
70 struct callchain_param callchain_param = {
71 .mode = CHAIN_GRAPH_ABS,
75 static u64 sample_type;
78 struct perf_event_header header;
81 unsigned char __more_data[];
85 struct perf_event_header header;
90 char filename[PATH_MAX];
94 struct perf_event_header header;
100 struct perf_event_header header;
106 struct perf_event_header header;
112 struct perf_event_header header;
118 typedef union event_union {
119 struct perf_event_header header;
121 struct mmap_event mmap;
122 struct comm_event comm;
123 struct fork_event fork;
124 struct lost_event lost;
125 struct read_event read;
128 static int repsep_fprintf(FILE *fp, const char *fmt, ...)
135 n = vfprintf(fp, fmt, ap);
138 n = vasprintf(&bf, fmt, ap);
142 sep = strchr(sep, *field_sep);
155 static LIST_HEAD(dsos);
156 static struct dso *kernel_dso;
157 static struct dso *vdso;
158 static struct dso *hypervisor_dso;
160 static void dsos__add(struct dso *dso)
162 list_add_tail(&dso->node, &dsos);
165 static struct dso *dsos__find(const char *name)
169 list_for_each_entry(pos, &dsos, node)
170 if (strcmp(pos->name, name) == 0)
175 static struct dso *dsos__findnew(const char *name)
177 struct dso *dso = dsos__find(name);
183 dso = dso__new(name, 0);
187 nr = dso__load(dso, NULL, verbose);
189 eprintf("Failed to open: %s\n", name);
193 eprintf("No symbols found in: %s, maybe install a debug package?\n", name);
204 static void dsos__fprintf(FILE *fp)
208 list_for_each_entry(pos, &dsos, node)
209 dso__fprintf(pos, fp);
212 static struct symbol *vdso__find_symbol(struct dso *dso, u64 ip)
214 return dso__find_symbol(dso, ip);
217 static int load_kernel(void)
221 kernel_dso = dso__new("[kernel]", 0);
225 err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose, modules);
227 dso__delete(kernel_dso);
230 dsos__add(kernel_dso);
232 vdso = dso__new("[vdso]", 0);
236 vdso->find_symbol = vdso__find_symbol;
240 hypervisor_dso = dso__new("[hypervisor]", 0);
243 dsos__add(hypervisor_dso);
248 static char __cwd[PATH_MAX];
249 static char *cwd = __cwd;
252 static int strcommon(const char *pathname)
256 while (n < cwdlen && pathname[n] == cwd[n])
263 struct list_head node;
267 u64 (*map_ip)(struct map *, u64);
271 static u64 map__map_ip(struct map *map, u64 ip)
273 return ip - map->start + map->pgoff;
276 static u64 vdso__map_ip(struct map *map __used, u64 ip)
281 static inline int is_anon_memory(const char *filename)
283 return strcmp(filename, "//anon") == 0;
286 static struct map *map__new(struct mmap_event *event)
288 struct map *self = malloc(sizeof(*self));
291 const char *filename = event->filename;
292 char newfilename[PATH_MAX];
296 int n = strcommon(filename);
299 snprintf(newfilename, sizeof(newfilename),
300 ".%s", filename + n);
301 filename = newfilename;
305 anon = is_anon_memory(filename);
308 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
309 filename = newfilename;
312 self->start = event->start;
313 self->end = event->start + event->len;
314 self->pgoff = event->pgoff;
316 self->dso = dsos__findnew(filename);
317 if (self->dso == NULL)
320 if (self->dso == vdso || anon)
321 self->map_ip = vdso__map_ip;
323 self->map_ip = map__map_ip;
331 static struct map *map__clone(struct map *self)
333 struct map *map = malloc(sizeof(*self));
338 memcpy(map, self, sizeof(*self));
343 static int map__overlap(struct map *l, struct map *r)
345 if (l->start > r->start) {
351 if (l->end > r->start)
357 static size_t map__fprintf(struct map *self, FILE *fp)
359 return fprintf(fp, " %Lx-%Lx %Lx %s\n",
360 self->start, self->end, self->pgoff, self->dso->name);
365 struct rb_node rb_node;
366 struct list_head maps;
371 static struct thread *thread__new(pid_t pid)
373 struct thread *self = malloc(sizeof(*self));
377 self->comm = malloc(32);
379 snprintf(self->comm, 32, ":%d", self->pid);
380 INIT_LIST_HEAD(&self->maps);
386 static unsigned int dsos__col_width,
390 static int thread__set_comm(struct thread *self, const char *comm)
394 self->comm = strdup(comm);
398 if (!col_width_list_str && !field_sep &&
399 (!comm_list || strlist__has_entry(comm_list, comm))) {
400 unsigned int slen = strlen(comm);
401 if (slen > comms__col_width) {
402 comms__col_width = slen;
403 threads__col_width = slen + 6;
410 static size_t thread__fprintf(struct thread *self, FILE *fp)
413 size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
415 list_for_each_entry(pos, &self->maps, node)
416 ret += map__fprintf(pos, fp);
422 static struct rb_root threads;
423 static struct thread *last_match;
425 static struct thread *threads__findnew(pid_t pid)
427 struct rb_node **p = &threads.rb_node;
428 struct rb_node *parent = NULL;
432 * Font-end cache - PID lookups come in blocks,
433 * so most of the time we dont have to look up
436 if (last_match && last_match->pid == pid)
441 th = rb_entry(parent, struct thread, rb_node);
443 if (th->pid == pid) {
454 th = thread__new(pid);
456 rb_link_node(&th->rb_node, parent, p);
457 rb_insert_color(&th->rb_node, &threads);
464 static void thread__insert_map(struct thread *self, struct map *map)
466 struct map *pos, *tmp;
468 list_for_each_entry_safe(pos, tmp, &self->maps, node) {
469 if (map__overlap(pos, map)) {
471 printf("overlapping maps:\n");
472 map__fprintf(map, stdout);
473 map__fprintf(pos, stdout);
476 if (map->start <= pos->start && map->end > pos->start)
477 pos->start = map->end;
479 if (map->end >= pos->end && map->start < pos->end)
480 pos->end = map->start;
483 printf("after collision:\n");
484 map__fprintf(pos, stdout);
487 if (pos->start >= pos->end) {
488 list_del_init(&pos->node);
494 list_add_tail(&map->node, &self->maps);
497 static int thread__fork(struct thread *self, struct thread *parent)
503 self->comm = strdup(parent->comm);
507 list_for_each_entry(map, &parent->maps, node) {
508 struct map *new = map__clone(map);
511 thread__insert_map(self, new);
517 static struct map *thread__find_map(struct thread *self, u64 ip)
524 list_for_each_entry(pos, &self->maps, node)
525 if (ip >= pos->start && ip <= pos->end)
531 static size_t threads__fprintf(FILE *fp)
536 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
537 struct thread *pos = rb_entry(nd, struct thread, rb_node);
539 ret += thread__fprintf(pos, fp);
546 * histogram, sorted on item, collects counts
549 static struct rb_root hist;
552 struct rb_node rb_node;
554 struct thread *thread;
558 struct symbol *parent;
561 struct callchain_node callchain;
562 struct rb_root sorted_chain;
568 * configurable sorting bits
572 struct list_head list;
576 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
577 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
578 size_t (*print)(FILE *fp, struct hist_entry *, unsigned int width);
583 static int64_t cmp_null(void *l, void *r)
596 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
598 return right->thread->pid - left->thread->pid;
602 sort__thread_print(FILE *fp, struct hist_entry *self, unsigned int width)
604 return repsep_fprintf(fp, "%*s:%5d", width - 6,
605 self->thread->comm ?: "", self->thread->pid);
608 static struct sort_entry sort_thread = {
609 .header = "Command: Pid",
610 .cmp = sort__thread_cmp,
611 .print = sort__thread_print,
612 .width = &threads__col_width,
618 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
620 return right->thread->pid - left->thread->pid;
624 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
626 char *comm_l = left->thread->comm;
627 char *comm_r = right->thread->comm;
629 if (!comm_l || !comm_r)
630 return cmp_null(comm_l, comm_r);
632 return strcmp(comm_l, comm_r);
636 sort__comm_print(FILE *fp, struct hist_entry *self, unsigned int width)
638 return repsep_fprintf(fp, "%*s", width, self->thread->comm);
641 static struct sort_entry sort_comm = {
643 .cmp = sort__comm_cmp,
644 .collapse = sort__comm_collapse,
645 .print = sort__comm_print,
646 .width = &comms__col_width,
652 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
654 struct dso *dso_l = left->dso;
655 struct dso *dso_r = right->dso;
657 if (!dso_l || !dso_r)
658 return cmp_null(dso_l, dso_r);
660 return strcmp(dso_l->name, dso_r->name);
664 sort__dso_print(FILE *fp, struct hist_entry *self, unsigned int width)
667 return repsep_fprintf(fp, "%-*s", width, self->dso->name);
669 return repsep_fprintf(fp, "%*llx", width, (u64)self->ip);
672 static struct sort_entry sort_dso = {
673 .header = "Shared Object",
674 .cmp = sort__dso_cmp,
675 .print = sort__dso_print,
676 .width = &dsos__col_width,
682 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
686 if (left->sym == right->sym)
689 ip_l = left->sym ? left->sym->start : left->ip;
690 ip_r = right->sym ? right->sym->start : right->ip;
692 return (int64_t)(ip_r - ip_l);
696 sort__sym_print(FILE *fp, struct hist_entry *self, unsigned int width __used)
701 ret += repsep_fprintf(fp, "%#018llx ", (u64)self->ip);
703 ret += repsep_fprintf(fp, "[%c] ", self->level);
705 ret += repsep_fprintf(fp, "%s", self->sym->name);
707 if (self->sym->module)
708 ret += repsep_fprintf(fp, "\t[%s]",
709 self->sym->module->name);
711 ret += repsep_fprintf(fp, "%#016llx", (u64)self->ip);
717 static struct sort_entry sort_sym = {
719 .cmp = sort__sym_cmp,
720 .print = sort__sym_print,
726 sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
728 struct symbol *sym_l = left->parent;
729 struct symbol *sym_r = right->parent;
731 if (!sym_l || !sym_r)
732 return cmp_null(sym_l, sym_r);
734 return strcmp(sym_l->name, sym_r->name);
738 sort__parent_print(FILE *fp, struct hist_entry *self, unsigned int width)
740 return repsep_fprintf(fp, "%-*s", width,
741 self->parent ? self->parent->name : "[other]");
744 static unsigned int parent_symbol__col_width;
746 static struct sort_entry sort_parent = {
747 .header = "Parent symbol",
748 .cmp = sort__parent_cmp,
749 .print = sort__parent_print,
750 .width = &parent_symbol__col_width,
753 static int sort__need_collapse = 0;
754 static int sort__has_parent = 0;
756 struct sort_dimension {
758 struct sort_entry *entry;
762 static struct sort_dimension sort_dimensions[] = {
763 { .name = "pid", .entry = &sort_thread, },
764 { .name = "comm", .entry = &sort_comm, },
765 { .name = "dso", .entry = &sort_dso, },
766 { .name = "symbol", .entry = &sort_sym, },
767 { .name = "parent", .entry = &sort_parent, },
770 static LIST_HEAD(hist_entry__sort_list);
772 static int sort_dimension__add(char *tok)
776 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
777 struct sort_dimension *sd = &sort_dimensions[i];
782 if (strncasecmp(tok, sd->name, strlen(tok)))
785 if (sd->entry->collapse)
786 sort__need_collapse = 1;
788 if (sd->entry == &sort_parent) {
789 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
793 regerror(ret, &parent_regex, err, sizeof(err));
794 fprintf(stderr, "Invalid regex: %s\n%s",
795 parent_pattern, err);
798 sort__has_parent = 1;
801 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
811 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
813 struct sort_entry *se;
816 list_for_each_entry(se, &hist_entry__sort_list, list) {
817 cmp = se->cmp(left, right);
826 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
828 struct sort_entry *se;
831 list_for_each_entry(se, &hist_entry__sort_list, list) {
832 int64_t (*f)(struct hist_entry *, struct hist_entry *);
834 f = se->collapse ?: se->cmp;
836 cmp = f(left, right);
844 static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask)
849 ret += fprintf(fp, "%s", " ");
851 for (i = 0; i < depth; i++)
852 if (depth_mask & (1 << i))
853 ret += fprintf(fp, "| ");
855 ret += fprintf(fp, " ");
857 ret += fprintf(fp, "\n");
862 ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, int depth,
863 int depth_mask, int count, u64 total_samples,
869 ret += fprintf(fp, "%s", " ");
870 for (i = 0; i < depth; i++) {
871 if (depth_mask & (1 << i))
872 ret += fprintf(fp, "|");
874 ret += fprintf(fp, " ");
875 if (!count && i == depth - 1) {
878 percent = hits * 100.0 / total_samples;
879 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
881 ret += fprintf(fp, "%s", " ");
884 ret += fprintf(fp, "%s\n", chain->sym->name);
886 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
892 callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
893 u64 total_samples, int depth, int depth_mask)
895 struct rb_node *node, *next;
896 struct callchain_node *child;
897 struct callchain_list *chain;
898 int new_depth_mask = depth_mask;
903 if (callchain_param.mode == CHAIN_GRAPH_REL)
904 new_total = self->cumul_hit;
906 new_total = total_samples;
908 node = rb_first(&self->rb_root);
910 child = rb_entry(node, struct callchain_node, rb_node);
913 * The depth mask manages the output of pipes that show
914 * the depth. We don't want to keep the pipes of the current
915 * level for the last child of this depth
917 next = rb_next(node);
919 new_depth_mask &= ~(1 << (depth - 1));
922 * But we keep the older depth mask for the line seperator
923 * to keep the level link until we reach the last child
925 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask);
927 list_for_each_entry(chain, &child->val, list) {
928 if (chain->ip >= PERF_CONTEXT_MAX)
930 ret += ipchain__fprintf_graph(fp, chain, depth,
935 ret += callchain__fprintf_graph(fp, child, new_total,
937 new_depth_mask | (1 << depth));
945 callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
948 struct callchain_list *chain;
954 ret += callchain__fprintf_flat(fp, self->parent, total_samples);
957 list_for_each_entry(chain, &self->val, list) {
958 if (chain->ip >= PERF_CONTEXT_MAX)
961 ret += fprintf(fp, " %s\n", chain->sym->name);
963 ret += fprintf(fp, " %p\n",
964 (void *)(long)chain->ip);
971 hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
974 struct rb_node *rb_node;
975 struct callchain_node *chain;
978 rb_node = rb_first(&self->sorted_chain);
982 chain = rb_entry(rb_node, struct callchain_node, rb_node);
983 percent = chain->hit * 100.0 / total_samples;
984 switch (callchain_param.mode) {
986 ret += percent_color_fprintf(fp, " %6.2f%%\n",
988 ret += callchain__fprintf_flat(fp, chain, total_samples);
990 case CHAIN_GRAPH_ABS: /* Falldown */
991 case CHAIN_GRAPH_REL:
992 ret += callchain__fprintf_graph(fp, chain,
993 total_samples, 1, 1);
997 ret += fprintf(fp, "\n");
998 rb_node = rb_next(rb_node);
1006 hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
1008 struct sort_entry *se;
1011 if (exclude_other && !self->parent)
1015 ret = percent_color_fprintf(fp,
1016 field_sep ? "%.2f" : " %6.2f%%",
1017 (self->count * 100.0) / total_samples);
1019 ret = fprintf(fp, field_sep ? "%lld" : "%12lld ", self->count);
1021 if (show_nr_samples) {
1023 fprintf(fp, "%c%lld", *field_sep, self->count);
1025 fprintf(fp, "%11lld", self->count);
1028 list_for_each_entry(se, &hist_entry__sort_list, list) {
1032 fprintf(fp, "%s", field_sep ?: " ");
1033 ret += se->print(fp, self, se->width ? *se->width : 0);
1036 ret += fprintf(fp, "\n");
1039 hist_entry_callchain__fprintf(fp, self, total_samples);
1048 static void dso__calc_col_width(struct dso *self)
1050 if (!col_width_list_str && !field_sep &&
1051 (!dso_list || strlist__has_entry(dso_list, self->name))) {
1052 unsigned int slen = strlen(self->name);
1053 if (slen > dsos__col_width)
1054 dsos__col_width = slen;
1057 self->slen_calculated = 1;
1060 static struct symbol *
1061 resolve_symbol(struct thread *thread, struct map **mapp,
1062 struct dso **dsop, u64 *ipp)
1064 struct dso *dso = dsop ? *dsop : NULL;
1065 struct map *map = mapp ? *mapp : NULL;
1077 map = thread__find_map(thread, ip);
1080 * We have to do this here as we may have a dso
1081 * with no symbol hit that has a name longer than
1082 * the ones with symbols sampled.
1084 if (!sort_dso.elide && !map->dso->slen_calculated)
1085 dso__calc_col_width(map->dso);
1090 ip = map->map_ip(map, ip);
1095 * If this is outside of all known maps,
1096 * and is a negative address, try to look it
1097 * up in the kernel dso, as it might be a
1098 * vsyscall (which executes in user-mode):
1100 if ((long long)ip < 0)
1103 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
1104 dprintf(" ...... map: %Lx -> %Lx\n", *ipp, ip);
1113 return dso->find_symbol(dso, ip);
1116 static int call__match(struct symbol *sym)
1118 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
1124 static struct symbol **
1125 resolve_callchain(struct thread *thread, struct map *map __used,
1126 struct ip_callchain *chain, struct hist_entry *entry)
1128 u64 context = PERF_CONTEXT_MAX;
1129 struct symbol **syms = NULL;
1133 syms = calloc(chain->nr, sizeof(*syms));
1135 fprintf(stderr, "Can't allocate memory for symbols\n");
1140 for (i = 0; i < chain->nr; i++) {
1141 u64 ip = chain->ips[i];
1142 struct dso *dso = NULL;
1145 if (ip >= PERF_CONTEXT_MAX) {
1151 case PERF_CONTEXT_HV:
1152 dso = hypervisor_dso;
1154 case PERF_CONTEXT_KERNEL:
1161 sym = resolve_symbol(thread, NULL, &dso, &ip);
1164 if (sort__has_parent && call__match(sym) &&
1166 entry->parent = sym;
1177 * collect histogram counts
1181 hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
1182 struct symbol *sym, u64 ip, struct ip_callchain *chain,
1183 char level, u64 count)
1185 struct rb_node **p = &hist.rb_node;
1186 struct rb_node *parent = NULL;
1187 struct hist_entry *he;
1188 struct symbol **syms = NULL;
1189 struct hist_entry entry = {
1198 .sorted_chain = RB_ROOT
1202 if ((sort__has_parent || callchain) && chain)
1203 syms = resolve_callchain(thread, map, chain, &entry);
1205 while (*p != NULL) {
1207 he = rb_entry(parent, struct hist_entry, rb_node);
1209 cmp = hist_entry__cmp(&entry, he);
1214 append_chain(&he->callchain, chain, syms);
1223 p = &(*p)->rb_right;
1226 he = malloc(sizeof(*he));
1231 callchain_init(&he->callchain);
1232 append_chain(&he->callchain, chain, syms);
1235 rb_link_node(&he->rb_node, parent, p);
1236 rb_insert_color(&he->rb_node, &hist);
1241 static void hist_entry__free(struct hist_entry *he)
1247 * collapse the histogram
1250 static struct rb_root collapse_hists;
1252 static void collapse__insert_entry(struct hist_entry *he)
1254 struct rb_node **p = &collapse_hists.rb_node;
1255 struct rb_node *parent = NULL;
1256 struct hist_entry *iter;
1259 while (*p != NULL) {
1261 iter = rb_entry(parent, struct hist_entry, rb_node);
1263 cmp = hist_entry__collapse(iter, he);
1266 iter->count += he->count;
1267 hist_entry__free(he);
1274 p = &(*p)->rb_right;
1277 rb_link_node(&he->rb_node, parent, p);
1278 rb_insert_color(&he->rb_node, &collapse_hists);
1281 static void collapse__resort(void)
1283 struct rb_node *next;
1284 struct hist_entry *n;
1286 if (!sort__need_collapse)
1289 next = rb_first(&hist);
1291 n = rb_entry(next, struct hist_entry, rb_node);
1292 next = rb_next(&n->rb_node);
1294 rb_erase(&n->rb_node, &hist);
1295 collapse__insert_entry(n);
1300 * reverse the map, sort on count.
1303 static struct rb_root output_hists;
1305 static void output__insert_entry(struct hist_entry *he, u64 min_callchain_hits)
1307 struct rb_node **p = &output_hists.rb_node;
1308 struct rb_node *parent = NULL;
1309 struct hist_entry *iter;
1312 callchain_param.sort(&he->sorted_chain, &he->callchain,
1313 min_callchain_hits, &callchain_param);
1315 while (*p != NULL) {
1317 iter = rb_entry(parent, struct hist_entry, rb_node);
1319 if (he->count > iter->count)
1322 p = &(*p)->rb_right;
1325 rb_link_node(&he->rb_node, parent, p);
1326 rb_insert_color(&he->rb_node, &output_hists);
1329 static void output__resort(u64 total_samples)
1331 struct rb_node *next;
1332 struct hist_entry *n;
1333 struct rb_root *tree = &hist;
1334 u64 min_callchain_hits;
1336 min_callchain_hits = total_samples * (callchain_param.min_percent / 100);
1338 if (sort__need_collapse)
1339 tree = &collapse_hists;
1341 next = rb_first(tree);
1344 n = rb_entry(next, struct hist_entry, rb_node);
1345 next = rb_next(&n->rb_node);
1347 rb_erase(&n->rb_node, tree);
1348 output__insert_entry(n, min_callchain_hits);
1352 static size_t output__fprintf(FILE *fp, u64 total_samples)
1354 struct hist_entry *pos;
1355 struct sort_entry *se;
1359 char *col_width = col_width_list_str;
1361 fprintf(fp, "# Samples: %Ld\n", (u64)total_samples);
1364 fprintf(fp, "# Overhead");
1365 if (show_nr_samples) {
1367 fprintf(fp, "%cSamples", *field_sep);
1369 fputs(" Samples ", fp);
1371 list_for_each_entry(se, &hist_entry__sort_list, list) {
1375 fprintf(fp, "%c%s", *field_sep, se->header);
1378 width = strlen(se->header);
1380 if (col_width_list_str) {
1382 *se->width = atoi(col_width);
1383 col_width = strchr(col_width, ',');
1388 width = *se->width = max(*se->width, width);
1390 fprintf(fp, " %*s", width, se->header);
1397 fprintf(fp, "# ........");
1398 if (show_nr_samples)
1399 fprintf(fp, " ..........");
1400 list_for_each_entry(se, &hist_entry__sort_list, list) {
1410 width = strlen(se->header);
1411 for (i = 0; i < width; i++)
1419 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
1420 pos = rb_entry(nd, struct hist_entry, rb_node);
1421 ret += hist_entry__fprintf(fp, pos, total_samples);
1424 if (sort_order == default_sort_order &&
1425 parent_pattern == default_parent_pattern) {
1427 fprintf(fp, "# (For more details, try: perf report --sort comm,dso,symbol)\n");
1435 static void register_idle_thread(void)
1437 struct thread *thread = threads__findnew(0);
1439 if (thread == NULL ||
1440 thread__set_comm(thread, "[idle]")) {
1441 fprintf(stderr, "problem inserting idle task.\n");
1446 static unsigned long total = 0,
1453 static int validate_chain(struct ip_callchain *chain, event_t *event)
1455 unsigned int chain_size;
1457 chain_size = event->header.size;
1458 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
1460 if (chain->nr*sizeof(u64) > chain_size)
1467 process_sample_event(event_t *event, unsigned long offset, unsigned long head)
1471 struct dso *dso = NULL;
1472 struct thread *thread = threads__findnew(event->ip.pid);
1473 u64 ip = event->ip.ip;
1475 struct map *map = NULL;
1476 void *more_data = event->ip.__more_data;
1477 struct ip_callchain *chain = NULL;
1480 if (sample_type & PERF_SAMPLE_PERIOD) {
1481 period = *(u64 *)more_data;
1482 more_data += sizeof(u64);
1485 dprintf("%p [%p]: PERF_EVENT_SAMPLE (IP, %d): %d: %p period: %Ld\n",
1486 (void *)(offset + head),
1487 (void *)(long)(event->header.size),
1493 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
1496 chain = (void *)more_data;
1498 dprintf("... chain: nr:%Lu\n", chain->nr);
1500 if (validate_chain(chain, event) < 0) {
1501 eprintf("call-chain problem with event, skipping it.\n");
1506 for (i = 0; i < chain->nr; i++)
1507 dprintf("..... %2d: %016Lx\n", i, chain->ips[i]);
1511 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1513 if (thread == NULL) {
1514 eprintf("problem processing %d event, skipping it.\n",
1515 event->header.type);
1519 if (comm_list && !strlist__has_entry(comm_list, thread->comm))
1522 cpumode = event->header.misc & PERF_EVENT_MISC_CPUMODE_MASK;
1524 if (cpumode == PERF_EVENT_MISC_KERNEL) {
1530 dprintf(" ...... dso: %s\n", dso->name);
1532 } else if (cpumode == PERF_EVENT_MISC_USER) {
1541 dso = hypervisor_dso;
1543 dprintf(" ...... dso: [hypervisor]\n");
1546 if (show & show_mask) {
1547 struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
1549 if (dso_list && dso && dso->name && !strlist__has_entry(dso_list, dso->name))
1552 if (sym_list && sym && !strlist__has_entry(sym_list, sym->name))
1555 if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
1556 eprintf("problem incrementing symbol count, skipping event\n");
1566 process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
1568 struct thread *thread = threads__findnew(event->mmap.pid);
1569 struct map *map = map__new(&event->mmap);
1571 dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
1572 (void *)(offset + head),
1573 (void *)(long)(event->header.size),
1575 (void *)(long)event->mmap.start,
1576 (void *)(long)event->mmap.len,
1577 (void *)(long)event->mmap.pgoff,
1578 event->mmap.filename);
1580 if (thread == NULL || map == NULL) {
1581 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
1585 thread__insert_map(thread, map);
1592 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
1594 struct thread *thread = threads__findnew(event->comm.pid);
1596 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
1597 (void *)(offset + head),
1598 (void *)(long)(event->header.size),
1599 event->comm.comm, event->comm.pid);
1601 if (thread == NULL ||
1602 thread__set_comm(thread, event->comm.comm)) {
1603 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
1612 process_task_event(event_t *event, unsigned long offset, unsigned long head)
1614 struct thread *thread = threads__findnew(event->fork.pid);
1615 struct thread *parent = threads__findnew(event->fork.ppid);
1617 dprintf("%p [%p]: PERF_EVENT_%s: (%d:%d):(%d:%d)\n",
1618 (void *)(offset + head),
1619 (void *)(long)(event->header.size),
1620 event->header.type == PERF_EVENT_FORK ? "FORK" : "EXIT",
1621 event->fork.pid, event->fork.tid,
1622 event->fork.ppid, event->fork.ptid);
1625 * A thread clone will have the same PID for both
1628 if (thread == parent)
1631 if (event->header.type == PERF_EVENT_EXIT)
1634 if (!thread || !parent || thread__fork(thread, parent)) {
1635 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
1644 process_lost_event(event_t *event, unsigned long offset, unsigned long head)
1646 dprintf("%p [%p]: PERF_EVENT_LOST: id:%Ld: lost:%Ld\n",
1647 (void *)(offset + head),
1648 (void *)(long)(event->header.size),
1652 total_lost += event->lost.lost;
1657 static void trace_event(event_t *event)
1659 unsigned char *raw_event = (void *)event;
1660 char *color = PERF_COLOR_BLUE;
1667 cdprintf("\n. ... raw event: size %d bytes\n", event->header.size);
1669 for (i = 0; i < event->header.size; i++) {
1670 if ((i & 15) == 0) {
1672 cdprintf(" %04x: ", i);
1675 cdprintf(" %02x", raw_event[i]);
1677 if (((i & 15) == 15) || i == event->header.size-1) {
1679 for (j = 0; j < 15-(i & 15); j++)
1681 for (j = 0; j < (i & 15); j++) {
1682 if (isprint(raw_event[i-15+j]))
1683 cdprintf("%c", raw_event[i-15+j]);
1694 process_read_event(event_t *event, unsigned long offset, unsigned long head)
1696 dprintf("%p [%p]: PERF_EVENT_READ: %d %d %Lu\n",
1697 (void *)(offset + head),
1698 (void *)(long)(event->header.size),
1707 process_event(event_t *event, unsigned long offset, unsigned long head)
1711 switch (event->header.type) {
1712 case PERF_EVENT_SAMPLE:
1713 return process_sample_event(event, offset, head);
1715 case PERF_EVENT_MMAP:
1716 return process_mmap_event(event, offset, head);
1718 case PERF_EVENT_COMM:
1719 return process_comm_event(event, offset, head);
1721 case PERF_EVENT_FORK:
1722 case PERF_EVENT_EXIT:
1723 return process_task_event(event, offset, head);
1725 case PERF_EVENT_LOST:
1726 return process_lost_event(event, offset, head);
1728 case PERF_EVENT_READ:
1729 return process_read_event(event, offset, head);
1732 * We dont process them right now but they are fine:
1735 case PERF_EVENT_THROTTLE:
1736 case PERF_EVENT_UNTHROTTLE:
1746 static struct perf_header *header;
1748 static u64 perf_header__sample_type(void)
1750 u64 sample_type = 0;
1753 for (i = 0; i < header->attrs; i++) {
1754 struct perf_header_attr *attr = header->attr[i];
1757 sample_type = attr->attr.sample_type;
1758 else if (sample_type != attr->attr.sample_type)
1759 die("non matching sample_type");
1765 static int __cmd_report(void)
1767 int ret, rc = EXIT_FAILURE;
1768 unsigned long offset = 0;
1769 unsigned long head, shift;
1775 register_idle_thread();
1777 input = open(input_name, O_RDONLY);
1779 fprintf(stderr, " failed to open file: %s", input_name);
1780 if (!strcmp(input_name, "perf.data"))
1781 fprintf(stderr, " (try 'perf record' first)");
1782 fprintf(stderr, "\n");
1786 ret = fstat(input, &stat);
1788 perror("failed to stat file");
1792 if (!stat.st_size) {
1793 fprintf(stderr, "zero-sized file, nothing to do!\n");
1797 header = perf_header__read(input);
1798 head = header->data_offset;
1800 sample_type = perf_header__sample_type();
1802 if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
1803 if (sort__has_parent) {
1804 fprintf(stderr, "selected --sort parent, but no"
1805 " callchain data. Did you call"
1806 " perf record without -g?\n");
1810 fprintf(stderr, "selected -c but no callchain data."
1811 " Did you call perf record without"
1817 if (load_kernel() < 0) {
1818 perror("failed to load kernel symbols");
1819 return EXIT_FAILURE;
1823 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
1824 perror("failed to get the current directory");
1825 return EXIT_FAILURE;
1827 cwdlen = strlen(cwd);
1833 shift = page_size * (head / page_size);
1838 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1839 MAP_SHARED, input, offset);
1840 if (buf == MAP_FAILED) {
1841 perror("failed to mmap file");
1846 event = (event_t *)(buf + head);
1848 size = event->header.size;
1852 if (head + event->header.size >= page_size * mmap_window) {
1855 shift = page_size * (head / page_size);
1857 ret = munmap(buf, page_size * mmap_window);
1865 size = event->header.size;
1867 dprintf("\n%p [%p]: event: %d\n",
1868 (void *)(offset + head),
1869 (void *)(long)event->header.size,
1870 event->header.type);
1872 if (!size || process_event(event, offset, head) < 0) {
1874 dprintf("%p [%p]: skipping unknown header type: %d\n",
1875 (void *)(offset + head),
1876 (void *)(long)(event->header.size),
1877 event->header.type);
1882 * assume we lost track of the stream, check alignment, and
1883 * increment a single u64 in the hope to catch on again 'soon'.
1886 if (unlikely(head & 7))
1894 if (offset + head >= header->data_offset + header->data_size)
1897 if (offset + head < (unsigned long)stat.st_size)
1904 dprintf(" IP events: %10ld\n", total);
1905 dprintf(" mmap events: %10ld\n", total_mmap);
1906 dprintf(" comm events: %10ld\n", total_comm);
1907 dprintf(" fork events: %10ld\n", total_fork);
1908 dprintf(" lost events: %10ld\n", total_lost);
1909 dprintf(" unknown events: %10ld\n", total_unknown);
1915 threads__fprintf(stdout);
1918 dsos__fprintf(stdout);
1921 output__resort(total);
1922 output__fprintf(stdout, total);
1928 parse_callchain_opt(const struct option *opt __used, const char *arg,
1939 tok = strtok((char *)arg, ",");
1943 /* get the output mode */
1944 if (!strncmp(tok, "graph", strlen(arg)))
1945 callchain_param.mode = CHAIN_GRAPH_ABS;
1947 else if (!strncmp(tok, "flat", strlen(arg)))
1948 callchain_param.mode = CHAIN_FLAT;
1950 else if (!strncmp(tok, "fractal", strlen(arg)))
1951 callchain_param.mode = CHAIN_GRAPH_REL;
1956 /* get the min percentage */
1957 tok = strtok(NULL, ",");
1961 callchain_param.min_percent = strtod(tok, &endptr);
1966 if (register_callchain_param(&callchain_param) < 0) {
1967 fprintf(stderr, "Can't register callchain params\n");
1973 static const char * const report_usage[] = {
1974 "perf report [<options>] <command>",
1978 static const struct option options[] = {
1979 OPT_STRING('i', "input", &input_name, "file",
1981 OPT_BOOLEAN('v', "verbose", &verbose,
1982 "be more verbose (show symbol address, etc)"),
1983 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1984 "dump raw trace in ASCII"),
1985 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
1986 OPT_BOOLEAN('m', "modules", &modules,
1987 "load module symbols - WARNING: use only with -k and LIVE kernel"),
1988 OPT_BOOLEAN('n', "show-nr-samples", &show_nr_samples,
1989 "Show a column with the number of samples"),
1990 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1991 "sort by key(s): pid, comm, dso, symbol, parent"),
1992 OPT_BOOLEAN('P', "full-paths", &full_paths,
1993 "Don't shorten the pathnames taking into account the cwd"),
1994 OPT_STRING('p', "parent", &parent_pattern, "regex",
1995 "regex filter to identify parent, see: '--sort parent'"),
1996 OPT_BOOLEAN('x', "exclude-other", &exclude_other,
1997 "Only display entries with parent-match"),
1998 OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
1999 "Display callchains using output_type and min percent threshold. "
2000 "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
2001 OPT_STRING('d', "dsos", &dso_list_str, "dso[,dso...]",
2002 "only consider symbols in these dsos"),
2003 OPT_STRING('C', "comms", &comm_list_str, "comm[,comm...]",
2004 "only consider symbols in these comms"),
2005 OPT_STRING('S', "symbols", &sym_list_str, "symbol[,symbol...]",
2006 "only consider these symbols"),
2007 OPT_STRING('w', "column-widths", &col_width_list_str,
2009 "don't try to adjust column width, use these fixed values"),
2010 OPT_STRING('t', "field-separator", &field_sep, "separator",
2011 "separator for columns, no spaces will be added between "
2012 "columns '.' is reserved."),
2016 static void setup_sorting(void)
2018 char *tmp, *tok, *str = strdup(sort_order);
2020 for (tok = strtok_r(str, ", ", &tmp);
2021 tok; tok = strtok_r(NULL, ", ", &tmp)) {
2022 if (sort_dimension__add(tok) < 0) {
2023 error("Unknown --sort key: `%s'", tok);
2024 usage_with_options(report_usage, options);
2031 static void setup_list(struct strlist **list, const char *list_str,
2032 struct sort_entry *se, const char *list_name,
2036 *list = strlist__new(true, list_str);
2038 fprintf(stderr, "problems parsing %s list\n",
2042 if (strlist__nr_entries(*list) == 1) {
2043 fprintf(fp, "# %s: %s\n", list_name,
2044 strlist__entry(*list, 0)->s);
2050 int cmd_report(int argc, const char **argv, const char *prefix __used)
2054 page_size = getpagesize();
2056 argc = parse_options(argc, argv, options, report_usage, 0);
2060 if (parent_pattern != default_parent_pattern) {
2061 sort_dimension__add("parent");
2062 sort_parent.elide = 1;
2067 * Any (unrecognized) arguments left?
2070 usage_with_options(report_usage, options);
2074 setup_list(&dso_list, dso_list_str, &sort_dso, "dso", stdout);
2075 setup_list(&comm_list, comm_list_str, &sort_comm, "comm", stdout);
2076 setup_list(&sym_list, sym_list_str, &sort_sym, "symbol", stdout);
2078 if (field_sep && *field_sep == '.') {
2079 fputs("'.' is the only non valid --field-separator argument\n",
2084 return __cmd_report();