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;
105 struct perf_event_header header;
111 struct perf_event_header header;
117 typedef union event_union {
118 struct perf_event_header header;
120 struct mmap_event mmap;
121 struct comm_event comm;
122 struct fork_event fork;
123 struct lost_event lost;
124 struct read_event read;
127 static int repsep_fprintf(FILE *fp, const char *fmt, ...)
134 n = vfprintf(fp, fmt, ap);
137 n = vasprintf(&bf, fmt, ap);
141 sep = strchr(sep, *field_sep);
154 static LIST_HEAD(dsos);
155 static struct dso *kernel_dso;
156 static struct dso *vdso;
157 static struct dso *hypervisor_dso;
159 static void dsos__add(struct dso *dso)
161 list_add_tail(&dso->node, &dsos);
164 static struct dso *dsos__find(const char *name)
168 list_for_each_entry(pos, &dsos, node)
169 if (strcmp(pos->name, name) == 0)
174 static struct dso *dsos__findnew(const char *name)
176 struct dso *dso = dsos__find(name);
182 dso = dso__new(name, 0);
186 nr = dso__load(dso, NULL, verbose);
188 eprintf("Failed to open: %s\n", name);
192 eprintf("No symbols found in: %s, maybe install a debug package?\n", name);
203 static void dsos__fprintf(FILE *fp)
207 list_for_each_entry(pos, &dsos, node)
208 dso__fprintf(pos, fp);
211 static struct symbol *vdso__find_symbol(struct dso *dso, u64 ip)
213 return dso__find_symbol(dso, ip);
216 static int load_kernel(void)
220 kernel_dso = dso__new("[kernel]", 0);
224 err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose, modules);
226 dso__delete(kernel_dso);
229 dsos__add(kernel_dso);
231 vdso = dso__new("[vdso]", 0);
235 vdso->find_symbol = vdso__find_symbol;
239 hypervisor_dso = dso__new("[hypervisor]", 0);
242 dsos__add(hypervisor_dso);
247 static char __cwd[PATH_MAX];
248 static char *cwd = __cwd;
251 static int strcommon(const char *pathname)
255 while (pathname[n] == cwd[n] && n < cwdlen)
262 struct list_head node;
266 u64 (*map_ip)(struct map *, u64);
270 static u64 map__map_ip(struct map *map, u64 ip)
272 return ip - map->start + map->pgoff;
275 static u64 vdso__map_ip(struct map *map __used, u64 ip)
280 static inline int is_anon_memory(const char *filename)
282 return strcmp(filename, "//anon") == 0;
285 static struct map *map__new(struct mmap_event *event)
287 struct map *self = malloc(sizeof(*self));
290 const char *filename = event->filename;
291 char newfilename[PATH_MAX];
295 int n = strcommon(filename);
298 snprintf(newfilename, sizeof(newfilename),
299 ".%s", filename + n);
300 filename = newfilename;
304 anon = is_anon_memory(filename);
307 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
308 filename = newfilename;
311 self->start = event->start;
312 self->end = event->start + event->len;
313 self->pgoff = event->pgoff;
315 self->dso = dsos__findnew(filename);
316 if (self->dso == NULL)
319 if (self->dso == vdso || anon)
320 self->map_ip = vdso__map_ip;
322 self->map_ip = map__map_ip;
330 static struct map *map__clone(struct map *self)
332 struct map *map = malloc(sizeof(*self));
337 memcpy(map, self, sizeof(*self));
342 static int map__overlap(struct map *l, struct map *r)
344 if (l->start > r->start) {
350 if (l->end > r->start)
356 static size_t map__fprintf(struct map *self, FILE *fp)
358 return fprintf(fp, " %Lx-%Lx %Lx %s\n",
359 self->start, self->end, self->pgoff, self->dso->name);
364 struct rb_node rb_node;
365 struct list_head maps;
370 static struct thread *thread__new(pid_t pid)
372 struct thread *self = malloc(sizeof(*self));
376 self->comm = malloc(32);
378 snprintf(self->comm, 32, ":%d", self->pid);
379 INIT_LIST_HEAD(&self->maps);
385 static unsigned int dsos__col_width,
389 static int thread__set_comm(struct thread *self, const char *comm)
393 self->comm = strdup(comm);
397 if (!col_width_list_str && !field_sep &&
398 (!comm_list || strlist__has_entry(comm_list, comm))) {
399 unsigned int slen = strlen(comm);
400 if (slen > comms__col_width) {
401 comms__col_width = slen;
402 threads__col_width = slen + 6;
409 static size_t thread__fprintf(struct thread *self, FILE *fp)
412 size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
414 list_for_each_entry(pos, &self->maps, node)
415 ret += map__fprintf(pos, fp);
421 static struct rb_root threads;
422 static struct thread *last_match;
424 static struct thread *threads__findnew(pid_t pid)
426 struct rb_node **p = &threads.rb_node;
427 struct rb_node *parent = NULL;
431 * Font-end cache - PID lookups come in blocks,
432 * so most of the time we dont have to look up
435 if (last_match && last_match->pid == pid)
440 th = rb_entry(parent, struct thread, rb_node);
442 if (th->pid == pid) {
453 th = thread__new(pid);
455 rb_link_node(&th->rb_node, parent, p);
456 rb_insert_color(&th->rb_node, &threads);
463 static void thread__insert_map(struct thread *self, struct map *map)
465 struct map *pos, *tmp;
467 list_for_each_entry_safe(pos, tmp, &self->maps, node) {
468 if (map__overlap(pos, map)) {
470 printf("overlapping maps:\n");
471 map__fprintf(map, stdout);
472 map__fprintf(pos, stdout);
475 if (map->start <= pos->start && map->end > pos->start)
476 pos->start = map->end;
478 if (map->end >= pos->end && map->start < pos->end)
479 pos->end = map->start;
482 printf("after collision:\n");
483 map__fprintf(pos, stdout);
486 if (pos->start >= pos->end) {
487 list_del_init(&pos->node);
493 list_add_tail(&map->node, &self->maps);
496 static int thread__fork(struct thread *self, struct thread *parent)
502 self->comm = strdup(parent->comm);
506 list_for_each_entry(map, &parent->maps, node) {
507 struct map *new = map__clone(map);
510 thread__insert_map(self, new);
516 static struct map *thread__find_map(struct thread *self, u64 ip)
523 list_for_each_entry(pos, &self->maps, node)
524 if (ip >= pos->start && ip <= pos->end)
530 static size_t threads__fprintf(FILE *fp)
535 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
536 struct thread *pos = rb_entry(nd, struct thread, rb_node);
538 ret += thread__fprintf(pos, fp);
545 * histogram, sorted on item, collects counts
548 static struct rb_root hist;
551 struct rb_node rb_node;
553 struct thread *thread;
557 struct symbol *parent;
560 struct callchain_node callchain;
561 struct rb_root sorted_chain;
567 * configurable sorting bits
571 struct list_head list;
575 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
576 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
577 size_t (*print)(FILE *fp, struct hist_entry *, unsigned int width);
582 static int64_t cmp_null(void *l, void *r)
595 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
597 return right->thread->pid - left->thread->pid;
601 sort__thread_print(FILE *fp, struct hist_entry *self, unsigned int width)
603 return repsep_fprintf(fp, "%*s:%5d", width - 6,
604 self->thread->comm ?: "", self->thread->pid);
607 static struct sort_entry sort_thread = {
608 .header = "Command: Pid",
609 .cmp = sort__thread_cmp,
610 .print = sort__thread_print,
611 .width = &threads__col_width,
617 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
619 return right->thread->pid - left->thread->pid;
623 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
625 char *comm_l = left->thread->comm;
626 char *comm_r = right->thread->comm;
628 if (!comm_l || !comm_r)
629 return cmp_null(comm_l, comm_r);
631 return strcmp(comm_l, comm_r);
635 sort__comm_print(FILE *fp, struct hist_entry *self, unsigned int width)
637 return repsep_fprintf(fp, "%*s", width, self->thread->comm);
640 static struct sort_entry sort_comm = {
642 .cmp = sort__comm_cmp,
643 .collapse = sort__comm_collapse,
644 .print = sort__comm_print,
645 .width = &comms__col_width,
651 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
653 struct dso *dso_l = left->dso;
654 struct dso *dso_r = right->dso;
656 if (!dso_l || !dso_r)
657 return cmp_null(dso_l, dso_r);
659 return strcmp(dso_l->name, dso_r->name);
663 sort__dso_print(FILE *fp, struct hist_entry *self, unsigned int width)
666 return repsep_fprintf(fp, "%-*s", width, self->dso->name);
668 return repsep_fprintf(fp, "%*llx", width, (u64)self->ip);
671 static struct sort_entry sort_dso = {
672 .header = "Shared Object",
673 .cmp = sort__dso_cmp,
674 .print = sort__dso_print,
675 .width = &dsos__col_width,
681 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
685 if (left->sym == right->sym)
688 ip_l = left->sym ? left->sym->start : left->ip;
689 ip_r = right->sym ? right->sym->start : right->ip;
691 return (int64_t)(ip_r - ip_l);
695 sort__sym_print(FILE *fp, struct hist_entry *self, unsigned int width __used)
700 ret += repsep_fprintf(fp, "%#018llx ", (u64)self->ip);
702 ret += repsep_fprintf(fp, "[%c] ", self->level);
704 ret += repsep_fprintf(fp, "%s", self->sym->name);
706 if (self->sym->module)
707 ret += repsep_fprintf(fp, "\t[%s]",
708 self->sym->module->name);
710 ret += repsep_fprintf(fp, "%#016llx", (u64)self->ip);
716 static struct sort_entry sort_sym = {
718 .cmp = sort__sym_cmp,
719 .print = sort__sym_print,
725 sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
727 struct symbol *sym_l = left->parent;
728 struct symbol *sym_r = right->parent;
730 if (!sym_l || !sym_r)
731 return cmp_null(sym_l, sym_r);
733 return strcmp(sym_l->name, sym_r->name);
737 sort__parent_print(FILE *fp, struct hist_entry *self, unsigned int width)
739 return repsep_fprintf(fp, "%-*s", width,
740 self->parent ? self->parent->name : "[other]");
743 static unsigned int parent_symbol__col_width;
745 static struct sort_entry sort_parent = {
746 .header = "Parent symbol",
747 .cmp = sort__parent_cmp,
748 .print = sort__parent_print,
749 .width = &parent_symbol__col_width,
752 static int sort__need_collapse = 0;
753 static int sort__has_parent = 0;
755 struct sort_dimension {
757 struct sort_entry *entry;
761 static struct sort_dimension sort_dimensions[] = {
762 { .name = "pid", .entry = &sort_thread, },
763 { .name = "comm", .entry = &sort_comm, },
764 { .name = "dso", .entry = &sort_dso, },
765 { .name = "symbol", .entry = &sort_sym, },
766 { .name = "parent", .entry = &sort_parent, },
769 static LIST_HEAD(hist_entry__sort_list);
771 static int sort_dimension__add(char *tok)
775 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
776 struct sort_dimension *sd = &sort_dimensions[i];
781 if (strncasecmp(tok, sd->name, strlen(tok)))
784 if (sd->entry->collapse)
785 sort__need_collapse = 1;
787 if (sd->entry == &sort_parent) {
788 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
792 regerror(ret, &parent_regex, err, sizeof(err));
793 fprintf(stderr, "Invalid regex: %s\n%s",
794 parent_pattern, err);
797 sort__has_parent = 1;
800 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
810 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
812 struct sort_entry *se;
815 list_for_each_entry(se, &hist_entry__sort_list, list) {
816 cmp = se->cmp(left, right);
825 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
827 struct sort_entry *se;
830 list_for_each_entry(se, &hist_entry__sort_list, list) {
831 int64_t (*f)(struct hist_entry *, struct hist_entry *);
833 f = se->collapse ?: se->cmp;
835 cmp = f(left, right);
843 static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask)
848 ret += fprintf(fp, "%s", " ");
850 for (i = 0; i < depth; i++)
851 if (depth_mask & (1 << i))
852 ret += fprintf(fp, "| ");
854 ret += fprintf(fp, " ");
856 ret += fprintf(fp, "\n");
861 ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, int depth,
862 int depth_mask, int count, u64 total_samples,
868 ret += fprintf(fp, "%s", " ");
869 for (i = 0; i < depth; i++) {
870 if (depth_mask & (1 << i))
871 ret += fprintf(fp, "|");
873 ret += fprintf(fp, " ");
874 if (!count && i == depth - 1) {
877 percent = hits * 100.0 / total_samples;
878 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
880 ret += fprintf(fp, "%s", " ");
883 ret += fprintf(fp, "%s\n", chain->sym->name);
885 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
891 callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
892 u64 total_samples, int depth, int depth_mask)
894 struct rb_node *node, *next;
895 struct callchain_node *child;
896 struct callchain_list *chain;
897 int new_depth_mask = depth_mask;
902 if (callchain_param.mode == CHAIN_GRAPH_REL)
903 new_total = self->cumul_hit;
905 new_total = total_samples;
907 node = rb_first(&self->rb_root);
909 child = rb_entry(node, struct callchain_node, rb_node);
912 * The depth mask manages the output of pipes that show
913 * the depth. We don't want to keep the pipes of the current
914 * level for the last child of this depth
916 next = rb_next(node);
918 new_depth_mask &= ~(1 << (depth - 1));
921 * But we keep the older depth mask for the line seperator
922 * to keep the level link until we reach the last child
924 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask);
926 list_for_each_entry(chain, &child->val, list) {
927 if (chain->ip >= PERF_CONTEXT_MAX)
929 ret += ipchain__fprintf_graph(fp, chain, depth,
934 ret += callchain__fprintf_graph(fp, child, new_total,
936 new_depth_mask | (1 << depth));
944 callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
947 struct callchain_list *chain;
953 ret += callchain__fprintf_flat(fp, self->parent, total_samples);
956 list_for_each_entry(chain, &self->val, list) {
957 if (chain->ip >= PERF_CONTEXT_MAX)
960 ret += fprintf(fp, " %s\n", chain->sym->name);
962 ret += fprintf(fp, " %p\n",
963 (void *)(long)chain->ip);
970 hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
973 struct rb_node *rb_node;
974 struct callchain_node *chain;
977 rb_node = rb_first(&self->sorted_chain);
981 chain = rb_entry(rb_node, struct callchain_node, rb_node);
982 percent = chain->hit * 100.0 / total_samples;
983 switch (callchain_param.mode) {
985 ret += percent_color_fprintf(fp, " %6.2f%%\n",
987 ret += callchain__fprintf_flat(fp, chain, total_samples);
989 case CHAIN_GRAPH_ABS: /* Falldown */
990 case CHAIN_GRAPH_REL:
991 ret += callchain__fprintf_graph(fp, chain,
992 total_samples, 1, 1);
996 ret += fprintf(fp, "\n");
997 rb_node = rb_next(rb_node);
1005 hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
1007 struct sort_entry *se;
1010 if (exclude_other && !self->parent)
1014 ret = percent_color_fprintf(fp,
1015 field_sep ? "%.2f" : " %6.2f%%",
1016 (self->count * 100.0) / total_samples);
1018 ret = fprintf(fp, field_sep ? "%lld" : "%12lld ", self->count);
1020 if (show_nr_samples) {
1022 fprintf(fp, "%c%lld", *field_sep, self->count);
1024 fprintf(fp, "%11lld", self->count);
1027 list_for_each_entry(se, &hist_entry__sort_list, list) {
1031 fprintf(fp, "%s", field_sep ?: " ");
1032 ret += se->print(fp, self, se->width ? *se->width : 0);
1035 ret += fprintf(fp, "\n");
1038 hist_entry_callchain__fprintf(fp, self, total_samples);
1047 static void dso__calc_col_width(struct dso *self)
1049 if (!col_width_list_str && !field_sep &&
1050 (!dso_list || strlist__has_entry(dso_list, self->name))) {
1051 unsigned int slen = strlen(self->name);
1052 if (slen > dsos__col_width)
1053 dsos__col_width = slen;
1056 self->slen_calculated = 1;
1059 static struct symbol *
1060 resolve_symbol(struct thread *thread, struct map **mapp,
1061 struct dso **dsop, u64 *ipp)
1063 struct dso *dso = dsop ? *dsop : NULL;
1064 struct map *map = mapp ? *mapp : NULL;
1076 map = thread__find_map(thread, ip);
1079 * We have to do this here as we may have a dso
1080 * with no symbol hit that has a name longer than
1081 * the ones with symbols sampled.
1083 if (!sort_dso.elide && !map->dso->slen_calculated)
1084 dso__calc_col_width(map->dso);
1089 ip = map->map_ip(map, ip);
1094 * If this is outside of all known maps,
1095 * and is a negative address, try to look it
1096 * up in the kernel dso, as it might be a
1097 * vsyscall (which executes in user-mode):
1099 if ((long long)ip < 0)
1102 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
1103 dprintf(" ...... map: %Lx -> %Lx\n", *ipp, ip);
1112 return dso->find_symbol(dso, ip);
1115 static int call__match(struct symbol *sym)
1117 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
1123 static struct symbol **
1124 resolve_callchain(struct thread *thread, struct map *map __used,
1125 struct ip_callchain *chain, struct hist_entry *entry)
1127 u64 context = PERF_CONTEXT_MAX;
1128 struct symbol **syms = NULL;
1132 syms = calloc(chain->nr, sizeof(*syms));
1134 fprintf(stderr, "Can't allocate memory for symbols\n");
1139 for (i = 0; i < chain->nr; i++) {
1140 u64 ip = chain->ips[i];
1141 struct dso *dso = NULL;
1144 if (ip >= PERF_CONTEXT_MAX) {
1150 case PERF_CONTEXT_HV:
1151 dso = hypervisor_dso;
1153 case PERF_CONTEXT_KERNEL:
1160 sym = resolve_symbol(thread, NULL, &dso, &ip);
1163 if (sort__has_parent && call__match(sym) &&
1165 entry->parent = sym;
1176 * collect histogram counts
1180 hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
1181 struct symbol *sym, u64 ip, struct ip_callchain *chain,
1182 char level, u64 count)
1184 struct rb_node **p = &hist.rb_node;
1185 struct rb_node *parent = NULL;
1186 struct hist_entry *he;
1187 struct symbol **syms = NULL;
1188 struct hist_entry entry = {
1197 .sorted_chain = RB_ROOT
1201 if ((sort__has_parent || callchain) && chain)
1202 syms = resolve_callchain(thread, map, chain, &entry);
1204 while (*p != NULL) {
1206 he = rb_entry(parent, struct hist_entry, rb_node);
1208 cmp = hist_entry__cmp(&entry, he);
1213 append_chain(&he->callchain, chain, syms);
1222 p = &(*p)->rb_right;
1225 he = malloc(sizeof(*he));
1230 callchain_init(&he->callchain);
1231 append_chain(&he->callchain, chain, syms);
1234 rb_link_node(&he->rb_node, parent, p);
1235 rb_insert_color(&he->rb_node, &hist);
1240 static void hist_entry__free(struct hist_entry *he)
1246 * collapse the histogram
1249 static struct rb_root collapse_hists;
1251 static void collapse__insert_entry(struct hist_entry *he)
1253 struct rb_node **p = &collapse_hists.rb_node;
1254 struct rb_node *parent = NULL;
1255 struct hist_entry *iter;
1258 while (*p != NULL) {
1260 iter = rb_entry(parent, struct hist_entry, rb_node);
1262 cmp = hist_entry__collapse(iter, he);
1265 iter->count += he->count;
1266 hist_entry__free(he);
1273 p = &(*p)->rb_right;
1276 rb_link_node(&he->rb_node, parent, p);
1277 rb_insert_color(&he->rb_node, &collapse_hists);
1280 static void collapse__resort(void)
1282 struct rb_node *next;
1283 struct hist_entry *n;
1285 if (!sort__need_collapse)
1288 next = rb_first(&hist);
1290 n = rb_entry(next, struct hist_entry, rb_node);
1291 next = rb_next(&n->rb_node);
1293 rb_erase(&n->rb_node, &hist);
1294 collapse__insert_entry(n);
1299 * reverse the map, sort on count.
1302 static struct rb_root output_hists;
1304 static void output__insert_entry(struct hist_entry *he, u64 min_callchain_hits)
1306 struct rb_node **p = &output_hists.rb_node;
1307 struct rb_node *parent = NULL;
1308 struct hist_entry *iter;
1311 callchain_param.sort(&he->sorted_chain, &he->callchain,
1312 min_callchain_hits, &callchain_param);
1314 while (*p != NULL) {
1316 iter = rb_entry(parent, struct hist_entry, rb_node);
1318 if (he->count > iter->count)
1321 p = &(*p)->rb_right;
1324 rb_link_node(&he->rb_node, parent, p);
1325 rb_insert_color(&he->rb_node, &output_hists);
1328 static void output__resort(u64 total_samples)
1330 struct rb_node *next;
1331 struct hist_entry *n;
1332 struct rb_root *tree = &hist;
1333 u64 min_callchain_hits;
1335 min_callchain_hits = total_samples * (callchain_param.min_percent / 100);
1337 if (sort__need_collapse)
1338 tree = &collapse_hists;
1340 next = rb_first(tree);
1343 n = rb_entry(next, struct hist_entry, rb_node);
1344 next = rb_next(&n->rb_node);
1346 rb_erase(&n->rb_node, tree);
1347 output__insert_entry(n, min_callchain_hits);
1351 static size_t output__fprintf(FILE *fp, u64 total_samples)
1353 struct hist_entry *pos;
1354 struct sort_entry *se;
1358 char *col_width = col_width_list_str;
1360 fprintf(fp, "# Samples: %Ld\n", (u64)total_samples);
1363 fprintf(fp, "# Overhead");
1364 if (show_nr_samples) {
1366 fprintf(fp, "%cSamples", *field_sep);
1368 fputs(" Samples ", fp);
1370 list_for_each_entry(se, &hist_entry__sort_list, list) {
1374 fprintf(fp, "%c%s", *field_sep, se->header);
1377 width = strlen(se->header);
1379 if (col_width_list_str) {
1381 *se->width = atoi(col_width);
1382 col_width = strchr(col_width, ',');
1387 width = *se->width = max(*se->width, width);
1389 fprintf(fp, " %*s", width, se->header);
1396 fprintf(fp, "# ........");
1397 if (show_nr_samples)
1398 fprintf(fp, " ..........");
1399 list_for_each_entry(se, &hist_entry__sort_list, list) {
1409 width = strlen(se->header);
1410 for (i = 0; i < width; i++)
1418 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
1419 pos = rb_entry(nd, struct hist_entry, rb_node);
1420 ret += hist_entry__fprintf(fp, pos, total_samples);
1423 if (sort_order == default_sort_order &&
1424 parent_pattern == default_parent_pattern) {
1426 fprintf(fp, "# (For more details, try: perf report --sort comm,dso,symbol)\n");
1434 static void register_idle_thread(void)
1436 struct thread *thread = threads__findnew(0);
1438 if (thread == NULL ||
1439 thread__set_comm(thread, "[idle]")) {
1440 fprintf(stderr, "problem inserting idle task.\n");
1445 static unsigned long total = 0,
1452 static int validate_chain(struct ip_callchain *chain, event_t *event)
1454 unsigned int chain_size;
1456 chain_size = event->header.size;
1457 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
1459 if (chain->nr*sizeof(u64) > chain_size)
1466 process_sample_event(event_t *event, unsigned long offset, unsigned long head)
1470 struct dso *dso = NULL;
1471 struct thread *thread = threads__findnew(event->ip.pid);
1472 u64 ip = event->ip.ip;
1474 struct map *map = NULL;
1475 void *more_data = event->ip.__more_data;
1476 struct ip_callchain *chain = NULL;
1479 if (sample_type & PERF_SAMPLE_PERIOD) {
1480 period = *(u64 *)more_data;
1481 more_data += sizeof(u64);
1484 dprintf("%p [%p]: PERF_EVENT_SAMPLE (IP, %d): %d: %p period: %Ld\n",
1485 (void *)(offset + head),
1486 (void *)(long)(event->header.size),
1492 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
1495 chain = (void *)more_data;
1497 dprintf("... chain: nr:%Lu\n", chain->nr);
1499 if (validate_chain(chain, event) < 0) {
1500 eprintf("call-chain problem with event, skipping it.\n");
1505 for (i = 0; i < chain->nr; i++)
1506 dprintf("..... %2d: %016Lx\n", i, chain->ips[i]);
1510 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1512 if (thread == NULL) {
1513 eprintf("problem processing %d event, skipping it.\n",
1514 event->header.type);
1518 if (comm_list && !strlist__has_entry(comm_list, thread->comm))
1521 cpumode = event->header.misc & PERF_EVENT_MISC_CPUMODE_MASK;
1523 if (cpumode == PERF_EVENT_MISC_KERNEL) {
1529 dprintf(" ...... dso: %s\n", dso->name);
1531 } else if (cpumode == PERF_EVENT_MISC_USER) {
1540 dso = hypervisor_dso;
1542 dprintf(" ...... dso: [hypervisor]\n");
1545 if (show & show_mask) {
1546 struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
1548 if (dso_list && dso && dso->name && !strlist__has_entry(dso_list, dso->name))
1551 if (sym_list && sym && !strlist__has_entry(sym_list, sym->name))
1554 if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
1555 eprintf("problem incrementing symbol count, skipping event\n");
1565 process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
1567 struct thread *thread = threads__findnew(event->mmap.pid);
1568 struct map *map = map__new(&event->mmap);
1570 dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
1571 (void *)(offset + head),
1572 (void *)(long)(event->header.size),
1574 (void *)(long)event->mmap.start,
1575 (void *)(long)event->mmap.len,
1576 (void *)(long)event->mmap.pgoff,
1577 event->mmap.filename);
1579 if (thread == NULL || map == NULL) {
1580 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
1584 thread__insert_map(thread, map);
1591 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
1593 struct thread *thread = threads__findnew(event->comm.pid);
1595 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
1596 (void *)(offset + head),
1597 (void *)(long)(event->header.size),
1598 event->comm.comm, event->comm.pid);
1600 if (thread == NULL ||
1601 thread__set_comm(thread, event->comm.comm)) {
1602 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
1611 process_fork_event(event_t *event, unsigned long offset, unsigned long head)
1613 struct thread *thread = threads__findnew(event->fork.pid);
1614 struct thread *parent = threads__findnew(event->fork.ppid);
1616 dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
1617 (void *)(offset + head),
1618 (void *)(long)(event->header.size),
1619 event->fork.pid, event->fork.ppid);
1621 if (!thread || !parent || thread__fork(thread, parent)) {
1622 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
1631 process_lost_event(event_t *event, unsigned long offset, unsigned long head)
1633 dprintf("%p [%p]: PERF_EVENT_LOST: id:%Ld: lost:%Ld\n",
1634 (void *)(offset + head),
1635 (void *)(long)(event->header.size),
1639 total_lost += event->lost.lost;
1644 static void trace_event(event_t *event)
1646 unsigned char *raw_event = (void *)event;
1647 char *color = PERF_COLOR_BLUE;
1654 cdprintf("\n. ... raw event: size %d bytes\n", event->header.size);
1656 for (i = 0; i < event->header.size; i++) {
1657 if ((i & 15) == 0) {
1659 cdprintf(" %04x: ", i);
1662 cdprintf(" %02x", raw_event[i]);
1664 if (((i & 15) == 15) || i == event->header.size-1) {
1666 for (j = 0; j < 15-(i & 15); j++)
1668 for (j = 0; j < (i & 15); j++) {
1669 if (isprint(raw_event[i-15+j]))
1670 cdprintf("%c", raw_event[i-15+j]);
1681 process_read_event(event_t *event, unsigned long offset, unsigned long head)
1683 dprintf("%p [%p]: PERF_EVENT_READ: %d %d %Lu\n",
1684 (void *)(offset + head),
1685 (void *)(long)(event->header.size),
1694 process_event(event_t *event, unsigned long offset, unsigned long head)
1698 switch (event->header.type) {
1699 case PERF_EVENT_SAMPLE:
1700 return process_sample_event(event, offset, head);
1702 case PERF_EVENT_MMAP:
1703 return process_mmap_event(event, offset, head);
1705 case PERF_EVENT_COMM:
1706 return process_comm_event(event, offset, head);
1708 case PERF_EVENT_FORK:
1709 return process_fork_event(event, offset, head);
1711 case PERF_EVENT_LOST:
1712 return process_lost_event(event, offset, head);
1714 case PERF_EVENT_READ:
1715 return process_read_event(event, offset, head);
1718 * We dont process them right now but they are fine:
1721 case PERF_EVENT_THROTTLE:
1722 case PERF_EVENT_UNTHROTTLE:
1732 static struct perf_header *header;
1734 static u64 perf_header__sample_type(void)
1736 u64 sample_type = 0;
1739 for (i = 0; i < header->attrs; i++) {
1740 struct perf_header_attr *attr = header->attr[i];
1743 sample_type = attr->attr.sample_type;
1744 else if (sample_type != attr->attr.sample_type)
1745 die("non matching sample_type");
1751 static int __cmd_report(void)
1753 int ret, rc = EXIT_FAILURE;
1754 unsigned long offset = 0;
1755 unsigned long head, shift;
1761 register_idle_thread();
1763 input = open(input_name, O_RDONLY);
1765 fprintf(stderr, " failed to open file: %s", input_name);
1766 if (!strcmp(input_name, "perf.data"))
1767 fprintf(stderr, " (try 'perf record' first)");
1768 fprintf(stderr, "\n");
1772 ret = fstat(input, &stat);
1774 perror("failed to stat file");
1778 if (!stat.st_size) {
1779 fprintf(stderr, "zero-sized file, nothing to do!\n");
1783 header = perf_header__read(input);
1784 head = header->data_offset;
1786 sample_type = perf_header__sample_type();
1788 if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
1789 if (sort__has_parent) {
1790 fprintf(stderr, "selected --sort parent, but no"
1791 " callchain data. Did you call"
1792 " perf record without -g?\n");
1796 fprintf(stderr, "selected -c but no callchain data."
1797 " Did you call perf record without"
1803 if (load_kernel() < 0) {
1804 perror("failed to load kernel symbols");
1805 return EXIT_FAILURE;
1809 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
1810 perror("failed to get the current directory");
1811 return EXIT_FAILURE;
1813 cwdlen = strlen(cwd);
1819 shift = page_size * (head / page_size);
1824 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1825 MAP_SHARED, input, offset);
1826 if (buf == MAP_FAILED) {
1827 perror("failed to mmap file");
1832 event = (event_t *)(buf + head);
1834 size = event->header.size;
1838 if (head + event->header.size >= page_size * mmap_window) {
1841 shift = page_size * (head / page_size);
1843 ret = munmap(buf, page_size * mmap_window);
1851 size = event->header.size;
1853 dprintf("\n%p [%p]: event: %d\n",
1854 (void *)(offset + head),
1855 (void *)(long)event->header.size,
1856 event->header.type);
1858 if (!size || process_event(event, offset, head) < 0) {
1860 dprintf("%p [%p]: skipping unknown header type: %d\n",
1861 (void *)(offset + head),
1862 (void *)(long)(event->header.size),
1863 event->header.type);
1868 * assume we lost track of the stream, check alignment, and
1869 * increment a single u64 in the hope to catch on again 'soon'.
1872 if (unlikely(head & 7))
1880 if (offset + head >= header->data_offset + header->data_size)
1883 if (offset + head < (unsigned long)stat.st_size)
1890 dprintf(" IP events: %10ld\n", total);
1891 dprintf(" mmap events: %10ld\n", total_mmap);
1892 dprintf(" comm events: %10ld\n", total_comm);
1893 dprintf(" fork events: %10ld\n", total_fork);
1894 dprintf(" lost events: %10ld\n", total_lost);
1895 dprintf(" unknown events: %10ld\n", total_unknown);
1901 threads__fprintf(stdout);
1904 dsos__fprintf(stdout);
1907 output__resort(total);
1908 output__fprintf(stdout, total);
1914 parse_callchain_opt(const struct option *opt __used, const char *arg,
1925 tok = strtok((char *)arg, ",");
1929 /* get the output mode */
1930 if (!strncmp(tok, "graph", strlen(arg)))
1931 callchain_param.mode = CHAIN_GRAPH_ABS;
1933 else if (!strncmp(tok, "flat", strlen(arg)))
1934 callchain_param.mode = CHAIN_FLAT;
1936 else if (!strncmp(tok, "fractal", strlen(arg)))
1937 callchain_param.mode = CHAIN_GRAPH_REL;
1942 /* get the min percentage */
1943 tok = strtok(NULL, ",");
1947 callchain_param.min_percent = strtod(tok, &endptr);
1952 if (register_callchain_param(&callchain_param) < 0) {
1953 fprintf(stderr, "Can't register callchain params\n");
1959 static const char * const report_usage[] = {
1960 "perf report [<options>] <command>",
1964 static const struct option options[] = {
1965 OPT_STRING('i', "input", &input_name, "file",
1967 OPT_BOOLEAN('v', "verbose", &verbose,
1968 "be more verbose (show symbol address, etc)"),
1969 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1970 "dump raw trace in ASCII"),
1971 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
1972 OPT_BOOLEAN('m', "modules", &modules,
1973 "load module symbols - WARNING: use only with -k and LIVE kernel"),
1974 OPT_BOOLEAN('n', "show-nr-samples", &show_nr_samples,
1975 "Show a column with the number of samples"),
1976 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1977 "sort by key(s): pid, comm, dso, symbol, parent"),
1978 OPT_BOOLEAN('P', "full-paths", &full_paths,
1979 "Don't shorten the pathnames taking into account the cwd"),
1980 OPT_STRING('p', "parent", &parent_pattern, "regex",
1981 "regex filter to identify parent, see: '--sort parent'"),
1982 OPT_BOOLEAN('x', "exclude-other", &exclude_other,
1983 "Only display entries with parent-match"),
1984 OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
1985 "Display callchains using output_type and min percent threshold. "
1986 "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
1987 OPT_STRING('d', "dsos", &dso_list_str, "dso[,dso...]",
1988 "only consider symbols in these dsos"),
1989 OPT_STRING('C', "comms", &comm_list_str, "comm[,comm...]",
1990 "only consider symbols in these comms"),
1991 OPT_STRING('S', "symbols", &sym_list_str, "symbol[,symbol...]",
1992 "only consider these symbols"),
1993 OPT_STRING('w', "column-widths", &col_width_list_str,
1995 "don't try to adjust column width, use these fixed values"),
1996 OPT_STRING('t', "field-separator", &field_sep, "separator",
1997 "separator for columns, no spaces will be added between "
1998 "columns '.' is reserved."),
2002 static void setup_sorting(void)
2004 char *tmp, *tok, *str = strdup(sort_order);
2006 for (tok = strtok_r(str, ", ", &tmp);
2007 tok; tok = strtok_r(NULL, ", ", &tmp)) {
2008 if (sort_dimension__add(tok) < 0) {
2009 error("Unknown --sort key: `%s'", tok);
2010 usage_with_options(report_usage, options);
2017 static void setup_list(struct strlist **list, const char *list_str,
2018 struct sort_entry *se, const char *list_name,
2022 *list = strlist__new(true, list_str);
2024 fprintf(stderr, "problems parsing %s list\n",
2028 if (strlist__nr_entries(*list) == 1) {
2029 fprintf(fp, "# %s: %s\n", list_name,
2030 strlist__entry(*list, 0)->s);
2036 int cmd_report(int argc, const char **argv, const char *prefix __used)
2040 page_size = getpagesize();
2042 argc = parse_options(argc, argv, options, report_usage, 0);
2046 if (parent_pattern != default_parent_pattern) {
2047 sort_dimension__add("parent");
2048 sort_parent.elide = 1;
2053 * Any (unrecognized) arguments left?
2056 usage_with_options(report_usage, options);
2060 setup_list(&dso_list, dso_list_str, &sort_dso, "dso", stdout);
2061 setup_list(&comm_list, comm_list_str, &sort_comm, "comm", stdout);
2062 setup_list(&sym_list, sym_list_str, &sort_sym, "symbol", stdout);
2064 if (field_sep && *field_sep == '.') {
2065 fputs("'.' is the only non valid --field-separator argument\n",
2070 return __cmd_report();