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;
37 static struct strlist *dso_list, *comm_list, *sym_list;
40 static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
42 static int dump_trace = 0;
43 #define dprintf(x...) do { if (dump_trace) printf(x); } while (0)
44 #define cdprintf(x...) do { if (dump_trace) color_fprintf(stdout, color, x); } while (0)
47 #define eprintf(x...) do { if (verbose) fprintf(stderr, x); } while (0)
51 static int full_paths;
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[] = "graph,0.5";
64 static enum chain_mode callchain_mode;
65 static double callchain_min_percent = 0.5;
67 static u64 sample_type;
70 struct perf_event_header header;
73 unsigned char __more_data[];
77 struct perf_event_header header;
82 char filename[PATH_MAX];
86 struct perf_event_header header;
92 struct perf_event_header header;
97 struct perf_event_header header;
104 struct perf_event_header header;
110 struct perf_event_header header;
116 typedef union event_union {
117 struct perf_event_header header;
119 struct mmap_event mmap;
120 struct comm_event comm;
121 struct fork_event fork;
122 struct period_event period;
123 struct lost_event lost;
124 struct read_event read;
127 static LIST_HEAD(dsos);
128 static struct dso *kernel_dso;
129 static struct dso *vdso;
130 static struct dso *hypervisor_dso;
132 static void dsos__add(struct dso *dso)
134 list_add_tail(&dso->node, &dsos);
137 static struct dso *dsos__find(const char *name)
141 list_for_each_entry(pos, &dsos, node)
142 if (strcmp(pos->name, name) == 0)
147 static struct dso *dsos__findnew(const char *name)
149 struct dso *dso = dsos__find(name);
155 dso = dso__new(name, 0);
159 nr = dso__load(dso, NULL, verbose);
161 eprintf("Failed to open: %s\n", name);
165 eprintf("No symbols found in: %s, maybe install a debug package?\n", name);
176 static void dsos__fprintf(FILE *fp)
180 list_for_each_entry(pos, &dsos, node)
181 dso__fprintf(pos, fp);
184 static struct symbol *vdso__find_symbol(struct dso *dso, u64 ip)
186 return dso__find_symbol(dso, ip);
189 static int load_kernel(void)
193 kernel_dso = dso__new("[kernel]", 0);
197 err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose, modules);
199 dso__delete(kernel_dso);
202 dsos__add(kernel_dso);
204 vdso = dso__new("[vdso]", 0);
208 vdso->find_symbol = vdso__find_symbol;
212 hypervisor_dso = dso__new("[hypervisor]", 0);
215 dsos__add(hypervisor_dso);
220 static char __cwd[PATH_MAX];
221 static char *cwd = __cwd;
224 static int strcommon(const char *pathname)
228 while (pathname[n] == cwd[n] && n < cwdlen)
235 struct list_head node;
239 u64 (*map_ip)(struct map *, u64);
243 static u64 map__map_ip(struct map *map, u64 ip)
245 return ip - map->start + map->pgoff;
248 static u64 vdso__map_ip(struct map *map __used, u64 ip)
253 static inline int is_anon_memory(const char *filename)
255 return strcmp(filename, "//anon") == 0;
258 static struct map *map__new(struct mmap_event *event)
260 struct map *self = malloc(sizeof(*self));
263 const char *filename = event->filename;
264 char newfilename[PATH_MAX];
268 int n = strcommon(filename);
271 snprintf(newfilename, sizeof(newfilename),
272 ".%s", filename + n);
273 filename = newfilename;
277 anon = is_anon_memory(filename);
280 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
281 filename = newfilename;
284 self->start = event->start;
285 self->end = event->start + event->len;
286 self->pgoff = event->pgoff;
288 self->dso = dsos__findnew(filename);
289 if (self->dso == NULL)
292 if (self->dso == vdso || anon)
293 self->map_ip = vdso__map_ip;
295 self->map_ip = map__map_ip;
303 static struct map *map__clone(struct map *self)
305 struct map *map = malloc(sizeof(*self));
310 memcpy(map, self, sizeof(*self));
315 static int map__overlap(struct map *l, struct map *r)
317 if (l->start > r->start) {
323 if (l->end > r->start)
329 static size_t map__fprintf(struct map *self, FILE *fp)
331 return fprintf(fp, " %Lx-%Lx %Lx %s\n",
332 self->start, self->end, self->pgoff, self->dso->name);
337 struct rb_node rb_node;
338 struct list_head maps;
343 static struct thread *thread__new(pid_t pid)
345 struct thread *self = malloc(sizeof(*self));
349 self->comm = malloc(32);
351 snprintf(self->comm, 32, ":%d", self->pid);
352 INIT_LIST_HEAD(&self->maps);
358 static int thread__set_comm(struct thread *self, const char *comm)
362 self->comm = strdup(comm);
363 return self->comm ? 0 : -ENOMEM;
366 static size_t thread__fprintf(struct thread *self, FILE *fp)
369 size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
371 list_for_each_entry(pos, &self->maps, node)
372 ret += map__fprintf(pos, fp);
378 static struct rb_root threads;
379 static struct thread *last_match;
381 static struct thread *threads__findnew(pid_t pid)
383 struct rb_node **p = &threads.rb_node;
384 struct rb_node *parent = NULL;
388 * Font-end cache - PID lookups come in blocks,
389 * so most of the time we dont have to look up
392 if (last_match && last_match->pid == pid)
397 th = rb_entry(parent, struct thread, rb_node);
399 if (th->pid == pid) {
410 th = thread__new(pid);
412 rb_link_node(&th->rb_node, parent, p);
413 rb_insert_color(&th->rb_node, &threads);
420 static void thread__insert_map(struct thread *self, struct map *map)
422 struct map *pos, *tmp;
424 list_for_each_entry_safe(pos, tmp, &self->maps, node) {
425 if (map__overlap(pos, map)) {
427 printf("overlapping maps:\n");
428 map__fprintf(map, stdout);
429 map__fprintf(pos, stdout);
432 if (map->start <= pos->start && map->end > pos->start)
433 pos->start = map->end;
435 if (map->end >= pos->end && map->start < pos->end)
436 pos->end = map->start;
439 printf("after collision:\n");
440 map__fprintf(pos, stdout);
443 if (pos->start >= pos->end) {
444 list_del_init(&pos->node);
450 list_add_tail(&map->node, &self->maps);
453 static int thread__fork(struct thread *self, struct thread *parent)
459 self->comm = strdup(parent->comm);
463 list_for_each_entry(map, &parent->maps, node) {
464 struct map *new = map__clone(map);
467 thread__insert_map(self, new);
473 static struct map *thread__find_map(struct thread *self, u64 ip)
480 list_for_each_entry(pos, &self->maps, node)
481 if (ip >= pos->start && ip <= pos->end)
487 static size_t threads__fprintf(FILE *fp)
492 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
493 struct thread *pos = rb_entry(nd, struct thread, rb_node);
495 ret += thread__fprintf(pos, fp);
502 * histogram, sorted on item, collects counts
505 static struct rb_root hist;
508 struct rb_node rb_node;
510 struct thread *thread;
514 struct symbol *parent;
517 struct callchain_node callchain;
518 struct rb_root sorted_chain;
524 * configurable sorting bits
528 struct list_head list;
532 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
533 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
534 size_t (*print)(FILE *fp, struct hist_entry *);
537 static int64_t cmp_null(void *l, void *r)
550 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
552 return right->thread->pid - left->thread->pid;
556 sort__thread_print(FILE *fp, struct hist_entry *self)
558 return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
561 static struct sort_entry sort_thread = {
562 .header = " Command: Pid",
563 .cmp = sort__thread_cmp,
564 .print = sort__thread_print,
570 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
572 return right->thread->pid - left->thread->pid;
576 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
578 char *comm_l = left->thread->comm;
579 char *comm_r = right->thread->comm;
581 if (!comm_l || !comm_r)
582 return cmp_null(comm_l, comm_r);
584 return strcmp(comm_l, comm_r);
588 sort__comm_print(FILE *fp, struct hist_entry *self)
590 return fprintf(fp, "%16s", self->thread->comm);
593 static struct sort_entry sort_comm = {
594 .header = " Command",
595 .cmp = sort__comm_cmp,
596 .collapse = sort__comm_collapse,
597 .print = sort__comm_print,
603 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
605 struct dso *dso_l = left->dso;
606 struct dso *dso_r = right->dso;
608 if (!dso_l || !dso_r)
609 return cmp_null(dso_l, dso_r);
611 return strcmp(dso_l->name, dso_r->name);
615 sort__dso_print(FILE *fp, struct hist_entry *self)
618 return fprintf(fp, "%-25s", self->dso->name);
620 return fprintf(fp, "%016llx ", (u64)self->ip);
623 static struct sort_entry sort_dso = {
624 .header = "Shared Object ",
625 .cmp = sort__dso_cmp,
626 .print = sort__dso_print,
632 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
636 if (left->sym == right->sym)
639 ip_l = left->sym ? left->sym->start : left->ip;
640 ip_r = right->sym ? right->sym->start : right->ip;
642 return (int64_t)(ip_r - ip_l);
646 sort__sym_print(FILE *fp, struct hist_entry *self)
651 ret += fprintf(fp, "%#018llx ", (u64)self->ip);
654 ret += fprintf(fp, "[%c] %s",
655 self->dso == kernel_dso ? 'k' :
656 self->dso == hypervisor_dso ? 'h' : '.', self->sym->name);
658 if (self->sym->module)
659 ret += fprintf(fp, "\t[%s]", self->sym->module->name);
661 ret += fprintf(fp, "%#016llx", (u64)self->ip);
667 static struct sort_entry sort_sym = {
669 .cmp = sort__sym_cmp,
670 .print = sort__sym_print,
676 sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
678 struct symbol *sym_l = left->parent;
679 struct symbol *sym_r = right->parent;
681 if (!sym_l || !sym_r)
682 return cmp_null(sym_l, sym_r);
684 return strcmp(sym_l->name, sym_r->name);
688 sort__parent_print(FILE *fp, struct hist_entry *self)
692 ret += fprintf(fp, "%-20s", self->parent ? self->parent->name : "[other]");
697 static struct sort_entry sort_parent = {
698 .header = "Parent symbol ",
699 .cmp = sort__parent_cmp,
700 .print = sort__parent_print,
703 static int sort__need_collapse = 0;
704 static int sort__has_parent = 0;
706 struct sort_dimension {
708 struct sort_entry *entry;
712 static struct sort_dimension sort_dimensions[] = {
713 { .name = "pid", .entry = &sort_thread, },
714 { .name = "comm", .entry = &sort_comm, },
715 { .name = "dso", .entry = &sort_dso, },
716 { .name = "symbol", .entry = &sort_sym, },
717 { .name = "parent", .entry = &sort_parent, },
720 static LIST_HEAD(hist_entry__sort_list);
722 static int sort_dimension__add(char *tok)
726 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
727 struct sort_dimension *sd = &sort_dimensions[i];
732 if (strncasecmp(tok, sd->name, strlen(tok)))
735 if (sd->entry->collapse)
736 sort__need_collapse = 1;
738 if (sd->entry == &sort_parent) {
739 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
743 regerror(ret, &parent_regex, err, sizeof(err));
744 fprintf(stderr, "Invalid regex: %s\n%s",
745 parent_pattern, err);
748 sort__has_parent = 1;
751 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
761 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
763 struct sort_entry *se;
766 list_for_each_entry(se, &hist_entry__sort_list, list) {
767 cmp = se->cmp(left, right);
776 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
778 struct sort_entry *se;
781 list_for_each_entry(se, &hist_entry__sort_list, list) {
782 int64_t (*f)(struct hist_entry *, struct hist_entry *);
784 f = se->collapse ?: se->cmp;
786 cmp = f(left, right);
794 static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask)
799 ret += fprintf(fp, "%s", " ");
801 for (i = 0; i < depth; i++)
802 if (depth_mask & (1 << i))
803 ret += fprintf(fp, "| ");
805 ret += fprintf(fp, " ");
807 ret += fprintf(fp, "\n");
812 ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, int depth,
813 int depth_mask, int count, u64 total_samples,
819 ret += fprintf(fp, "%s", " ");
820 for (i = 0; i < depth; i++) {
821 if (depth_mask & (1 << i))
822 ret += fprintf(fp, "|");
824 ret += fprintf(fp, " ");
825 if (!count && i == depth - 1) {
828 percent = hits * 100.0 / total_samples;
829 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
831 ret += fprintf(fp, "%s", " ");
834 ret += fprintf(fp, "%s\n", chain->sym->name);
836 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
842 callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
843 u64 total_samples, int depth, int depth_mask)
845 struct rb_node *node, *next;
846 struct callchain_node *child;
847 struct callchain_list *chain;
848 int new_depth_mask = depth_mask;
852 node = rb_first(&self->rb_root);
854 child = rb_entry(node, struct callchain_node, rb_node);
857 * The depth mask manages the output of pipes that show
858 * the depth. We don't want to keep the pipes of the current
859 * level for the last child of this depth
861 next = rb_next(node);
863 new_depth_mask &= ~(1 << (depth - 1));
866 * But we keep the older depth mask for the line seperator
867 * to keep the level link until we reach the last child
869 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask);
871 list_for_each_entry(chain, &child->val, list) {
872 if (chain->ip >= PERF_CONTEXT_MAX)
874 ret += ipchain__fprintf_graph(fp, chain, depth,
879 ret += callchain__fprintf_graph(fp, child, total_samples,
881 new_depth_mask | (1 << depth));
889 callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
892 struct callchain_list *chain;
898 ret += callchain__fprintf_flat(fp, self->parent, total_samples);
901 list_for_each_entry(chain, &self->val, list) {
902 if (chain->ip >= PERF_CONTEXT_MAX)
905 ret += fprintf(fp, " %s\n", chain->sym->name);
907 ret += fprintf(fp, " %p\n",
908 (void *)(long)chain->ip);
915 hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
918 struct rb_node *rb_node;
919 struct callchain_node *chain;
922 rb_node = rb_first(&self->sorted_chain);
926 chain = rb_entry(rb_node, struct callchain_node, rb_node);
927 percent = chain->hit * 100.0 / total_samples;
928 if (callchain_mode == FLAT) {
929 ret += percent_color_fprintf(fp, " %6.2f%%\n",
931 ret += callchain__fprintf_flat(fp, chain, total_samples);
932 } else if (callchain_mode == GRAPH) {
933 ret += callchain__fprintf_graph(fp, chain,
934 total_samples, 1, 1);
936 ret += fprintf(fp, "\n");
937 rb_node = rb_next(rb_node);
945 hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
947 struct sort_entry *se;
950 if (exclude_other && !self->parent)
954 ret = percent_color_fprintf(fp, " %6.2f%%",
955 (self->count * 100.0) / total_samples);
957 ret = fprintf(fp, "%12Ld ", self->count);
959 list_for_each_entry(se, &hist_entry__sort_list, list) {
960 if (exclude_other && (se == &sort_parent))
964 ret += se->print(fp, self);
967 ret += fprintf(fp, "\n");
970 hist_entry_callchain__fprintf(fp, self, total_samples);
979 static struct symbol *
980 resolve_symbol(struct thread *thread, struct map **mapp,
981 struct dso **dsop, u64 *ipp)
983 struct dso *dso = dsop ? *dsop : NULL;
984 struct map *map = mapp ? *mapp : NULL;
996 map = thread__find_map(thread, ip);
1001 ip = map->map_ip(map, ip);
1006 * If this is outside of all known maps,
1007 * and is a negative address, try to look it
1008 * up in the kernel dso, as it might be a
1009 * vsyscall (which executes in user-mode):
1011 if ((long long)ip < 0)
1014 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
1015 dprintf(" ...... map: %Lx -> %Lx\n", *ipp, ip);
1024 return dso->find_symbol(dso, ip);
1027 static int call__match(struct symbol *sym)
1029 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
1035 static struct symbol **
1036 resolve_callchain(struct thread *thread, struct map *map __used,
1037 struct ip_callchain *chain, struct hist_entry *entry)
1039 u64 context = PERF_CONTEXT_MAX;
1040 struct symbol **syms = NULL;
1044 syms = calloc(chain->nr, sizeof(*syms));
1046 fprintf(stderr, "Can't allocate memory for symbols\n");
1051 for (i = 0; i < chain->nr; i++) {
1052 u64 ip = chain->ips[i];
1053 struct dso *dso = NULL;
1056 if (ip >= PERF_CONTEXT_MAX) {
1062 case PERF_CONTEXT_HV:
1063 dso = hypervisor_dso;
1065 case PERF_CONTEXT_KERNEL:
1072 sym = resolve_symbol(thread, NULL, &dso, &ip);
1075 if (sort__has_parent && call__match(sym) &&
1077 entry->parent = sym;
1088 * collect histogram counts
1092 hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
1093 struct symbol *sym, u64 ip, struct ip_callchain *chain,
1094 char level, u64 count)
1096 struct rb_node **p = &hist.rb_node;
1097 struct rb_node *parent = NULL;
1098 struct hist_entry *he;
1099 struct symbol **syms = NULL;
1100 struct hist_entry entry = {
1109 .sorted_chain = RB_ROOT
1113 if ((sort__has_parent || callchain) && chain)
1114 syms = resolve_callchain(thread, map, chain, &entry);
1116 while (*p != NULL) {
1118 he = rb_entry(parent, struct hist_entry, rb_node);
1120 cmp = hist_entry__cmp(&entry, he);
1125 append_chain(&he->callchain, chain, syms);
1134 p = &(*p)->rb_right;
1137 he = malloc(sizeof(*he));
1142 callchain_init(&he->callchain);
1143 append_chain(&he->callchain, chain, syms);
1146 rb_link_node(&he->rb_node, parent, p);
1147 rb_insert_color(&he->rb_node, &hist);
1152 static void hist_entry__free(struct hist_entry *he)
1158 * collapse the histogram
1161 static struct rb_root collapse_hists;
1163 static void collapse__insert_entry(struct hist_entry *he)
1165 struct rb_node **p = &collapse_hists.rb_node;
1166 struct rb_node *parent = NULL;
1167 struct hist_entry *iter;
1170 while (*p != NULL) {
1172 iter = rb_entry(parent, struct hist_entry, rb_node);
1174 cmp = hist_entry__collapse(iter, he);
1177 iter->count += he->count;
1178 hist_entry__free(he);
1185 p = &(*p)->rb_right;
1188 rb_link_node(&he->rb_node, parent, p);
1189 rb_insert_color(&he->rb_node, &collapse_hists);
1192 static void collapse__resort(void)
1194 struct rb_node *next;
1195 struct hist_entry *n;
1197 if (!sort__need_collapse)
1200 next = rb_first(&hist);
1202 n = rb_entry(next, struct hist_entry, rb_node);
1203 next = rb_next(&n->rb_node);
1205 rb_erase(&n->rb_node, &hist);
1206 collapse__insert_entry(n);
1211 * reverse the map, sort on count.
1214 static struct rb_root output_hists;
1216 static void output__insert_entry(struct hist_entry *he, u64 min_callchain_hits)
1218 struct rb_node **p = &output_hists.rb_node;
1219 struct rb_node *parent = NULL;
1220 struct hist_entry *iter;
1223 if (callchain_mode == FLAT)
1224 sort_chain_flat(&he->sorted_chain, &he->callchain,
1225 min_callchain_hits);
1226 else if (callchain_mode == GRAPH)
1227 sort_chain_graph(&he->sorted_chain, &he->callchain,
1228 min_callchain_hits);
1231 while (*p != NULL) {
1233 iter = rb_entry(parent, struct hist_entry, rb_node);
1235 if (he->count > iter->count)
1238 p = &(*p)->rb_right;
1241 rb_link_node(&he->rb_node, parent, p);
1242 rb_insert_color(&he->rb_node, &output_hists);
1245 static void output__resort(u64 total_samples)
1247 struct rb_node *next;
1248 struct hist_entry *n;
1249 struct rb_root *tree = &hist;
1250 u64 min_callchain_hits;
1252 min_callchain_hits = total_samples * (callchain_min_percent / 100);
1254 if (sort__need_collapse)
1255 tree = &collapse_hists;
1257 next = rb_first(tree);
1260 n = rb_entry(next, struct hist_entry, rb_node);
1261 next = rb_next(&n->rb_node);
1263 rb_erase(&n->rb_node, tree);
1264 output__insert_entry(n, min_callchain_hits);
1268 static size_t output__fprintf(FILE *fp, u64 total_samples)
1270 struct hist_entry *pos;
1271 struct sort_entry *se;
1277 fprintf(fp, "# (%Ld samples)\n", (u64)total_samples);
1280 fprintf(fp, "# Overhead");
1281 list_for_each_entry(se, &hist_entry__sort_list, list) {
1282 if (exclude_other && (se == &sort_parent))
1284 fprintf(fp, " %s", se->header);
1288 fprintf(fp, "# ........");
1289 list_for_each_entry(se, &hist_entry__sort_list, list) {
1292 if (exclude_other && (se == &sort_parent))
1296 for (i = 0; i < strlen(se->header); i++)
1303 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
1304 pos = rb_entry(nd, struct hist_entry, rb_node);
1305 ret += hist_entry__fprintf(fp, pos, total_samples);
1308 if (sort_order == default_sort_order &&
1309 parent_pattern == default_parent_pattern) {
1311 fprintf(fp, "# (For more details, try: perf report --sort comm,dso,symbol)\n");
1319 static void register_idle_thread(void)
1321 struct thread *thread = threads__findnew(0);
1323 if (thread == NULL ||
1324 thread__set_comm(thread, "[idle]")) {
1325 fprintf(stderr, "problem inserting idle task.\n");
1330 static unsigned long total = 0,
1337 static int validate_chain(struct ip_callchain *chain, event_t *event)
1339 unsigned int chain_size;
1341 chain_size = event->header.size;
1342 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
1344 if (chain->nr*sizeof(u64) > chain_size)
1351 process_sample_event(event_t *event, unsigned long offset, unsigned long head)
1355 struct dso *dso = NULL;
1356 struct thread *thread = threads__findnew(event->ip.pid);
1357 u64 ip = event->ip.ip;
1359 struct map *map = NULL;
1360 void *more_data = event->ip.__more_data;
1361 struct ip_callchain *chain = NULL;
1364 if (sample_type & PERF_SAMPLE_PERIOD) {
1365 period = *(u64 *)more_data;
1366 more_data += sizeof(u64);
1369 dprintf("%p [%p]: PERF_EVENT_SAMPLE (IP, %d): %d: %p period: %Ld\n",
1370 (void *)(offset + head),
1371 (void *)(long)(event->header.size),
1377 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
1380 chain = (void *)more_data;
1382 dprintf("... chain: nr:%Lu\n", chain->nr);
1384 if (validate_chain(chain, event) < 0) {
1385 eprintf("call-chain problem with event, skipping it.\n");
1390 for (i = 0; i < chain->nr; i++)
1391 dprintf("..... %2d: %016Lx\n", i, chain->ips[i]);
1395 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1397 if (thread == NULL) {
1398 eprintf("problem processing %d event, skipping it.\n",
1399 event->header.type);
1403 if (comm_list && !strlist__has_entry(comm_list, thread->comm))
1406 cpumode = event->header.misc & PERF_EVENT_MISC_CPUMODE_MASK;
1408 if (cpumode == PERF_EVENT_MISC_KERNEL) {
1414 dprintf(" ...... dso: %s\n", dso->name);
1416 } else if (cpumode == PERF_EVENT_MISC_USER) {
1425 dso = hypervisor_dso;
1427 dprintf(" ...... dso: [hypervisor]\n");
1430 if (show & show_mask) {
1431 struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
1433 if (dso_list && dso && dso->name && !strlist__has_entry(dso_list, dso->name))
1436 if (sym_list && sym && !strlist__has_entry(sym_list, sym->name))
1439 if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
1440 eprintf("problem incrementing symbol count, skipping event\n");
1450 process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
1452 struct thread *thread = threads__findnew(event->mmap.pid);
1453 struct map *map = map__new(&event->mmap);
1455 dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
1456 (void *)(offset + head),
1457 (void *)(long)(event->header.size),
1459 (void *)(long)event->mmap.start,
1460 (void *)(long)event->mmap.len,
1461 (void *)(long)event->mmap.pgoff,
1462 event->mmap.filename);
1464 if (thread == NULL || map == NULL) {
1465 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
1469 thread__insert_map(thread, map);
1476 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
1478 struct thread *thread = threads__findnew(event->comm.pid);
1480 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
1481 (void *)(offset + head),
1482 (void *)(long)(event->header.size),
1483 event->comm.comm, event->comm.pid);
1485 if (thread == NULL ||
1486 thread__set_comm(thread, event->comm.comm)) {
1487 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
1496 process_fork_event(event_t *event, unsigned long offset, unsigned long head)
1498 struct thread *thread = threads__findnew(event->fork.pid);
1499 struct thread *parent = threads__findnew(event->fork.ppid);
1501 dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
1502 (void *)(offset + head),
1503 (void *)(long)(event->header.size),
1504 event->fork.pid, event->fork.ppid);
1506 if (!thread || !parent || thread__fork(thread, parent)) {
1507 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
1516 process_period_event(event_t *event, unsigned long offset, unsigned long head)
1518 dprintf("%p [%p]: PERF_EVENT_PERIOD: time:%Ld, id:%Ld: period:%Ld\n",
1519 (void *)(offset + head),
1520 (void *)(long)(event->header.size),
1523 event->period.sample_period);
1529 process_lost_event(event_t *event, unsigned long offset, unsigned long head)
1531 dprintf("%p [%p]: PERF_EVENT_LOST: id:%Ld: lost:%Ld\n",
1532 (void *)(offset + head),
1533 (void *)(long)(event->header.size),
1537 total_lost += event->lost.lost;
1542 static void trace_event(event_t *event)
1544 unsigned char *raw_event = (void *)event;
1545 char *color = PERF_COLOR_BLUE;
1552 cdprintf("\n. ... raw event: size %d bytes\n", event->header.size);
1554 for (i = 0; i < event->header.size; i++) {
1555 if ((i & 15) == 0) {
1557 cdprintf(" %04x: ", i);
1560 cdprintf(" %02x", raw_event[i]);
1562 if (((i & 15) == 15) || i == event->header.size-1) {
1564 for (j = 0; j < 15-(i & 15); j++)
1566 for (j = 0; j < (i & 15); j++) {
1567 if (isprint(raw_event[i-15+j]))
1568 cdprintf("%c", raw_event[i-15+j]);
1579 process_read_event(event_t *event, unsigned long offset, unsigned long head)
1581 dprintf("%p [%p]: PERF_EVENT_READ: %d %d %Lu\n",
1582 (void *)(offset + head),
1583 (void *)(long)(event->header.size),
1592 process_event(event_t *event, unsigned long offset, unsigned long head)
1596 switch (event->header.type) {
1597 case PERF_EVENT_SAMPLE:
1598 return process_sample_event(event, offset, head);
1600 case PERF_EVENT_MMAP:
1601 return process_mmap_event(event, offset, head);
1603 case PERF_EVENT_COMM:
1604 return process_comm_event(event, offset, head);
1606 case PERF_EVENT_FORK:
1607 return process_fork_event(event, offset, head);
1609 case PERF_EVENT_PERIOD:
1610 return process_period_event(event, offset, head);
1612 case PERF_EVENT_LOST:
1613 return process_lost_event(event, offset, head);
1615 case PERF_EVENT_READ:
1616 return process_read_event(event, offset, head);
1619 * We dont process them right now but they are fine:
1622 case PERF_EVENT_THROTTLE:
1623 case PERF_EVENT_UNTHROTTLE:
1633 static struct perf_header *header;
1635 static u64 perf_header__sample_type(void)
1637 u64 sample_type = 0;
1640 for (i = 0; i < header->attrs; i++) {
1641 struct perf_header_attr *attr = header->attr[i];
1644 sample_type = attr->attr.sample_type;
1645 else if (sample_type != attr->attr.sample_type)
1646 die("non matching sample_type");
1652 static int __cmd_report(void)
1654 int ret, rc = EXIT_FAILURE;
1655 unsigned long offset = 0;
1656 unsigned long head, shift;
1662 register_idle_thread();
1664 input = open(input_name, O_RDONLY);
1666 fprintf(stderr, " failed to open file: %s", input_name);
1667 if (!strcmp(input_name, "perf.data"))
1668 fprintf(stderr, " (try 'perf record' first)");
1669 fprintf(stderr, "\n");
1673 ret = fstat(input, &stat);
1675 perror("failed to stat file");
1679 if (!stat.st_size) {
1680 fprintf(stderr, "zero-sized file, nothing to do!\n");
1684 header = perf_header__read(input);
1685 head = header->data_offset;
1687 sample_type = perf_header__sample_type();
1689 if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
1690 if (sort__has_parent) {
1691 fprintf(stderr, "selected --sort parent, but no"
1692 " callchain data. Did you call"
1693 " perf record without -g?\n");
1697 fprintf(stderr, "selected -c but no callchain data."
1698 " Did you call perf record without"
1704 if (load_kernel() < 0) {
1705 perror("failed to load kernel symbols");
1706 return EXIT_FAILURE;
1710 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
1711 perror("failed to get the current directory");
1712 return EXIT_FAILURE;
1714 cwdlen = strlen(cwd);
1720 shift = page_size * (head / page_size);
1725 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1726 MAP_SHARED, input, offset);
1727 if (buf == MAP_FAILED) {
1728 perror("failed to mmap file");
1733 event = (event_t *)(buf + head);
1735 size = event->header.size;
1739 if (head + event->header.size >= page_size * mmap_window) {
1742 shift = page_size * (head / page_size);
1744 ret = munmap(buf, page_size * mmap_window);
1752 size = event->header.size;
1754 dprintf("\n%p [%p]: event: %d\n",
1755 (void *)(offset + head),
1756 (void *)(long)event->header.size,
1757 event->header.type);
1759 if (!size || process_event(event, offset, head) < 0) {
1761 dprintf("%p [%p]: skipping unknown header type: %d\n",
1762 (void *)(offset + head),
1763 (void *)(long)(event->header.size),
1764 event->header.type);
1769 * assume we lost track of the stream, check alignment, and
1770 * increment a single u64 in the hope to catch on again 'soon'.
1773 if (unlikely(head & 7))
1781 if (offset + head >= header->data_offset + header->data_size)
1784 if (offset + head < (unsigned long)stat.st_size)
1791 dprintf(" IP events: %10ld\n", total);
1792 dprintf(" mmap events: %10ld\n", total_mmap);
1793 dprintf(" comm events: %10ld\n", total_comm);
1794 dprintf(" fork events: %10ld\n", total_fork);
1795 dprintf(" lost events: %10ld\n", total_lost);
1796 dprintf(" unknown events: %10ld\n", total_unknown);
1802 threads__fprintf(stdout);
1805 dsos__fprintf(stdout);
1808 output__resort(total);
1809 output__fprintf(stdout, total);
1815 parse_callchain_opt(const struct option *opt __used, const char *arg,
1826 tok = strtok((char *)arg, ",");
1830 /* get the output mode */
1831 if (!strncmp(tok, "graph", strlen(arg)))
1832 callchain_mode = GRAPH;
1834 else if (!strncmp(tok, "flat", strlen(arg)))
1835 callchain_mode = FLAT;
1839 /* get the min percentage */
1840 tok = strtok(NULL, ",");
1844 callchain_min_percent = strtod(tok, &endptr);
1851 static const char * const report_usage[] = {
1852 "perf report [<options>] <command>",
1856 static const struct option options[] = {
1857 OPT_STRING('i', "input", &input_name, "file",
1859 OPT_BOOLEAN('v', "verbose", &verbose,
1860 "be more verbose (show symbol address, etc)"),
1861 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1862 "dump raw trace in ASCII"),
1863 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
1864 OPT_BOOLEAN('m', "modules", &modules,
1865 "load module symbols - WARNING: use only with -k and LIVE kernel"),
1866 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1867 "sort by key(s): pid, comm, dso, symbol, parent"),
1868 OPT_BOOLEAN('P', "full-paths", &full_paths,
1869 "Don't shorten the pathnames taking into account the cwd"),
1870 OPT_STRING('p', "parent", &parent_pattern, "regex",
1871 "regex filter to identify parent, see: '--sort parent'"),
1872 OPT_BOOLEAN('x', "exclude-other", &exclude_other,
1873 "Only display entries with parent-match"),
1874 OPT_CALLBACK_DEFAULT('c', "callchain", NULL, "output_type,min_percent",
1875 "Display callchains using output_type and min percent threshold. "
1876 "Default: flat,0", &parse_callchain_opt, callchain_default_opt),
1877 OPT_STRING('d', "dsos", &dso_list_str, "dso[,dso...]",
1878 "only consider symbols in these dsos"),
1879 OPT_STRING('C', "comms", &comm_list_str, "comm[,comm...]",
1880 "only consider symbols in these comms"),
1881 OPT_STRING('S', "symbols", &sym_list_str, "symbol[,symbol...]",
1882 "only consider these symbols"),
1886 static void setup_sorting(void)
1888 char *tmp, *tok, *str = strdup(sort_order);
1890 for (tok = strtok_r(str, ", ", &tmp);
1891 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1892 if (sort_dimension__add(tok) < 0) {
1893 error("Unknown --sort key: `%s'", tok);
1894 usage_with_options(report_usage, options);
1901 static void setup_list(struct strlist **list, const char *list_str,
1902 const char *list_name)
1905 *list = strlist__new(true, list_str);
1907 fprintf(stderr, "problems parsing %s list\n",
1914 int cmd_report(int argc, const char **argv, const char *prefix __used)
1918 page_size = getpagesize();
1920 argc = parse_options(argc, argv, options, report_usage, 0);
1924 if (parent_pattern != default_parent_pattern)
1925 sort_dimension__add("parent");
1930 * Any (unrecognized) arguments left?
1933 usage_with_options(report_usage, options);
1935 setup_list(&dso_list, dso_list_str, "dso");
1936 setup_list(&comm_list, comm_list_str, "comm");
1937 setup_list(&sym_list, sym_list_str, "symbol");
1941 return __cmd_report();