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 enum chain_mode callchain_mode;
63 static double callchain_min_percent = 0.0;
65 static u64 sample_type;
68 struct perf_event_header header;
71 unsigned char __more_data[];
75 struct perf_event_header header;
80 char filename[PATH_MAX];
84 struct perf_event_header header;
90 struct perf_event_header header;
95 struct perf_event_header header;
102 struct perf_event_header header;
108 struct perf_event_header header;
114 typedef union event_union {
115 struct perf_event_header header;
117 struct mmap_event mmap;
118 struct comm_event comm;
119 struct fork_event fork;
120 struct period_event period;
121 struct lost_event lost;
122 struct read_event read;
125 static LIST_HEAD(dsos);
126 static struct dso *kernel_dso;
127 static struct dso *vdso;
128 static struct dso *hypervisor_dso;
130 static void dsos__add(struct dso *dso)
132 list_add_tail(&dso->node, &dsos);
135 static struct dso *dsos__find(const char *name)
139 list_for_each_entry(pos, &dsos, node)
140 if (strcmp(pos->name, name) == 0)
145 static struct dso *dsos__findnew(const char *name)
147 struct dso *dso = dsos__find(name);
153 dso = dso__new(name, 0);
157 nr = dso__load(dso, NULL, verbose);
159 eprintf("Failed to open: %s\n", name);
163 eprintf("No symbols found in: %s, maybe install a debug package?\n", name);
174 static void dsos__fprintf(FILE *fp)
178 list_for_each_entry(pos, &dsos, node)
179 dso__fprintf(pos, fp);
182 static struct symbol *vdso__find_symbol(struct dso *dso, u64 ip)
184 return dso__find_symbol(dso, ip);
187 static int load_kernel(void)
191 kernel_dso = dso__new("[kernel]", 0);
195 err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose, modules);
197 dso__delete(kernel_dso);
200 dsos__add(kernel_dso);
202 vdso = dso__new("[vdso]", 0);
206 vdso->find_symbol = vdso__find_symbol;
210 hypervisor_dso = dso__new("[hypervisor]", 0);
213 dsos__add(hypervisor_dso);
218 static char __cwd[PATH_MAX];
219 static char *cwd = __cwd;
222 static int strcommon(const char *pathname)
226 while (pathname[n] == cwd[n] && n < cwdlen)
233 struct list_head node;
237 u64 (*map_ip)(struct map *, u64);
241 static u64 map__map_ip(struct map *map, u64 ip)
243 return ip - map->start + map->pgoff;
246 static u64 vdso__map_ip(struct map *map __used, u64 ip)
251 static inline int is_anon_memory(const char *filename)
253 return strcmp(filename, "//anon") == 0;
256 static struct map *map__new(struct mmap_event *event)
258 struct map *self = malloc(sizeof(*self));
261 const char *filename = event->filename;
262 char newfilename[PATH_MAX];
266 int n = strcommon(filename);
269 snprintf(newfilename, sizeof(newfilename),
270 ".%s", filename + n);
271 filename = newfilename;
275 anon = is_anon_memory(filename);
278 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
279 filename = newfilename;
282 self->start = event->start;
283 self->end = event->start + event->len;
284 self->pgoff = event->pgoff;
286 self->dso = dsos__findnew(filename);
287 if (self->dso == NULL)
290 if (self->dso == vdso || anon)
291 self->map_ip = vdso__map_ip;
293 self->map_ip = map__map_ip;
301 static struct map *map__clone(struct map *self)
303 struct map *map = malloc(sizeof(*self));
308 memcpy(map, self, sizeof(*self));
313 static int map__overlap(struct map *l, struct map *r)
315 if (l->start > r->start) {
321 if (l->end > r->start)
327 static size_t map__fprintf(struct map *self, FILE *fp)
329 return fprintf(fp, " %Lx-%Lx %Lx %s\n",
330 self->start, self->end, self->pgoff, self->dso->name);
335 struct rb_node rb_node;
336 struct list_head maps;
341 static struct thread *thread__new(pid_t pid)
343 struct thread *self = malloc(sizeof(*self));
347 self->comm = malloc(32);
349 snprintf(self->comm, 32, ":%d", self->pid);
350 INIT_LIST_HEAD(&self->maps);
356 static int thread__set_comm(struct thread *self, const char *comm)
360 self->comm = strdup(comm);
361 return self->comm ? 0 : -ENOMEM;
364 static size_t thread__fprintf(struct thread *self, FILE *fp)
367 size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
369 list_for_each_entry(pos, &self->maps, node)
370 ret += map__fprintf(pos, fp);
376 static struct rb_root threads;
377 static struct thread *last_match;
379 static struct thread *threads__findnew(pid_t pid)
381 struct rb_node **p = &threads.rb_node;
382 struct rb_node *parent = NULL;
386 * Font-end cache - PID lookups come in blocks,
387 * so most of the time we dont have to look up
390 if (last_match && last_match->pid == pid)
395 th = rb_entry(parent, struct thread, rb_node);
397 if (th->pid == pid) {
408 th = thread__new(pid);
410 rb_link_node(&th->rb_node, parent, p);
411 rb_insert_color(&th->rb_node, &threads);
418 static void thread__insert_map(struct thread *self, struct map *map)
420 struct map *pos, *tmp;
422 list_for_each_entry_safe(pos, tmp, &self->maps, node) {
423 if (map__overlap(pos, map)) {
425 printf("overlapping maps:\n");
426 map__fprintf(map, stdout);
427 map__fprintf(pos, stdout);
430 if (map->start <= pos->start && map->end > pos->start)
431 pos->start = map->end;
433 if (map->end >= pos->end && map->start < pos->end)
434 pos->end = map->start;
437 printf("after collision:\n");
438 map__fprintf(pos, stdout);
441 if (pos->start >= pos->end) {
442 list_del_init(&pos->node);
448 list_add_tail(&map->node, &self->maps);
451 static int thread__fork(struct thread *self, struct thread *parent)
457 self->comm = strdup(parent->comm);
461 list_for_each_entry(map, &parent->maps, node) {
462 struct map *new = map__clone(map);
465 thread__insert_map(self, new);
471 static struct map *thread__find_map(struct thread *self, u64 ip)
478 list_for_each_entry(pos, &self->maps, node)
479 if (ip >= pos->start && ip <= pos->end)
485 static size_t threads__fprintf(FILE *fp)
490 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
491 struct thread *pos = rb_entry(nd, struct thread, rb_node);
493 ret += thread__fprintf(pos, fp);
500 * histogram, sorted on item, collects counts
503 static struct rb_root hist;
506 struct rb_node rb_node;
508 struct thread *thread;
512 struct symbol *parent;
515 struct callchain_node callchain;
516 struct rb_root sorted_chain;
522 * configurable sorting bits
526 struct list_head list;
530 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
531 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
532 size_t (*print)(FILE *fp, struct hist_entry *);
535 static int64_t cmp_null(void *l, void *r)
548 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
550 return right->thread->pid - left->thread->pid;
554 sort__thread_print(FILE *fp, struct hist_entry *self)
556 return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
559 static struct sort_entry sort_thread = {
560 .header = " Command: Pid",
561 .cmp = sort__thread_cmp,
562 .print = sort__thread_print,
568 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
570 return right->thread->pid - left->thread->pid;
574 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
576 char *comm_l = left->thread->comm;
577 char *comm_r = right->thread->comm;
579 if (!comm_l || !comm_r)
580 return cmp_null(comm_l, comm_r);
582 return strcmp(comm_l, comm_r);
586 sort__comm_print(FILE *fp, struct hist_entry *self)
588 return fprintf(fp, "%16s", self->thread->comm);
591 static struct sort_entry sort_comm = {
592 .header = " Command",
593 .cmp = sort__comm_cmp,
594 .collapse = sort__comm_collapse,
595 .print = sort__comm_print,
601 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
603 struct dso *dso_l = left->dso;
604 struct dso *dso_r = right->dso;
606 if (!dso_l || !dso_r)
607 return cmp_null(dso_l, dso_r);
609 return strcmp(dso_l->name, dso_r->name);
613 sort__dso_print(FILE *fp, struct hist_entry *self)
616 return fprintf(fp, "%-25s", self->dso->name);
618 return fprintf(fp, "%016llx ", (u64)self->ip);
621 static struct sort_entry sort_dso = {
622 .header = "Shared Object ",
623 .cmp = sort__dso_cmp,
624 .print = sort__dso_print,
630 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
634 if (left->sym == right->sym)
637 ip_l = left->sym ? left->sym->start : left->ip;
638 ip_r = right->sym ? right->sym->start : right->ip;
640 return (int64_t)(ip_r - ip_l);
644 sort__sym_print(FILE *fp, struct hist_entry *self)
649 ret += fprintf(fp, "%#018llx ", (u64)self->ip);
652 ret += fprintf(fp, "[%c] %s",
653 self->dso == kernel_dso ? 'k' :
654 self->dso == hypervisor_dso ? 'h' : '.', self->sym->name);
656 if (self->sym->module)
657 ret += fprintf(fp, "\t[%s]", self->sym->module->name);
659 ret += fprintf(fp, "%#016llx", (u64)self->ip);
665 static struct sort_entry sort_sym = {
667 .cmp = sort__sym_cmp,
668 .print = sort__sym_print,
674 sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
676 struct symbol *sym_l = left->parent;
677 struct symbol *sym_r = right->parent;
679 if (!sym_l || !sym_r)
680 return cmp_null(sym_l, sym_r);
682 return strcmp(sym_l->name, sym_r->name);
686 sort__parent_print(FILE *fp, struct hist_entry *self)
690 ret += fprintf(fp, "%-20s", self->parent ? self->parent->name : "[other]");
695 static struct sort_entry sort_parent = {
696 .header = "Parent symbol ",
697 .cmp = sort__parent_cmp,
698 .print = sort__parent_print,
701 static int sort__need_collapse = 0;
702 static int sort__has_parent = 0;
704 struct sort_dimension {
706 struct sort_entry *entry;
710 static struct sort_dimension sort_dimensions[] = {
711 { .name = "pid", .entry = &sort_thread, },
712 { .name = "comm", .entry = &sort_comm, },
713 { .name = "dso", .entry = &sort_dso, },
714 { .name = "symbol", .entry = &sort_sym, },
715 { .name = "parent", .entry = &sort_parent, },
718 static LIST_HEAD(hist_entry__sort_list);
720 static int sort_dimension__add(char *tok)
724 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
725 struct sort_dimension *sd = &sort_dimensions[i];
730 if (strncasecmp(tok, sd->name, strlen(tok)))
733 if (sd->entry->collapse)
734 sort__need_collapse = 1;
736 if (sd->entry == &sort_parent) {
737 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
741 regerror(ret, &parent_regex, err, sizeof(err));
742 fprintf(stderr, "Invalid regex: %s\n%s",
743 parent_pattern, err);
746 sort__has_parent = 1;
749 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
759 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
761 struct sort_entry *se;
764 list_for_each_entry(se, &hist_entry__sort_list, list) {
765 cmp = se->cmp(left, right);
774 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
776 struct sort_entry *se;
779 list_for_each_entry(se, &hist_entry__sort_list, list) {
780 int64_t (*f)(struct hist_entry *, struct hist_entry *);
782 f = se->collapse ?: se->cmp;
784 cmp = f(left, right);
792 static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask)
797 ret += fprintf(fp, "%s", " ");
799 for (i = 0; i < depth; i++)
800 if (depth_mask & (1 << i))
801 ret += fprintf(fp, "| ");
803 ret += fprintf(fp, " ");
805 ret += fprintf(fp, "\n");
810 ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, int depth,
811 int depth_mask, int count, u64 total_samples,
817 ret += fprintf(fp, "%s", " ");
818 for (i = 0; i < depth; i++) {
819 if (depth_mask & (1 << i))
820 ret += fprintf(fp, "|");
822 ret += fprintf(fp, " ");
823 if (!count && i == depth - 1) {
826 percent = hits * 100.0 / total_samples;
827 ret += fprintf(fp, "--%2.2f%%-- ", percent);
829 ret += fprintf(fp, "%s", " ");
832 ret += fprintf(fp, "%s\n", chain->sym->name);
834 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
840 callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
841 u64 total_samples, int depth, int depth_mask)
843 struct rb_node *node, *next;
844 struct callchain_node *child;
845 struct callchain_list *chain;
846 int new_depth_mask = depth_mask;
850 node = rb_first(&self->rb_root);
852 child = rb_entry(node, struct callchain_node, rb_node);
855 * The depth mask manages the output of pipes that show
856 * the depth. We don't want to keep the pipes of the current
857 * level for the last child of this depth
859 next = rb_next(node);
861 new_depth_mask &= ~(1 << (depth - 1));
864 * But we keep the older depth mask for the line seperator
865 * to keep the level link until we reach the last child
867 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask);
869 list_for_each_entry(chain, &child->val, list) {
870 if (chain->ip >= PERF_CONTEXT_MAX)
872 ret += ipchain__fprintf_graph(fp, chain, depth,
877 ret += callchain__fprintf_graph(fp, child, total_samples,
879 new_depth_mask | (1 << depth));
887 callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
890 struct callchain_list *chain;
896 ret += callchain__fprintf_flat(fp, self->parent, total_samples);
899 list_for_each_entry(chain, &self->val, list) {
900 if (chain->ip >= PERF_CONTEXT_MAX)
903 ret += fprintf(fp, " %s\n", chain->sym->name);
905 ret += fprintf(fp, " %p\n",
906 (void *)(long)chain->ip);
913 hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
916 struct rb_node *rb_node;
917 struct callchain_node *chain;
920 rb_node = rb_first(&self->sorted_chain);
924 chain = rb_entry(rb_node, struct callchain_node, rb_node);
925 percent = chain->hit * 100.0 / total_samples;
926 if (callchain_mode == FLAT) {
927 ret += fprintf(fp, " %6.2f%%\n", percent);
928 ret += callchain__fprintf_flat(fp, chain, total_samples);
929 } else if (callchain_mode == GRAPH) {
930 ret += callchain__fprintf_graph(fp, chain,
931 total_samples, 1, 1);
933 ret += fprintf(fp, "\n");
934 rb_node = rb_next(rb_node);
942 hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
944 struct sort_entry *se;
947 if (exclude_other && !self->parent)
951 ret = percent_color_fprintf(fp, " %6.2f%%",
952 (self->count * 100.0) / total_samples);
954 ret = fprintf(fp, "%12Ld ", self->count);
956 list_for_each_entry(se, &hist_entry__sort_list, list) {
957 if (exclude_other && (se == &sort_parent))
961 ret += se->print(fp, self);
964 ret += fprintf(fp, "\n");
967 hist_entry_callchain__fprintf(fp, self, total_samples);
976 static struct symbol *
977 resolve_symbol(struct thread *thread, struct map **mapp,
978 struct dso **dsop, u64 *ipp)
980 struct dso *dso = dsop ? *dsop : NULL;
981 struct map *map = mapp ? *mapp : NULL;
993 map = thread__find_map(thread, ip);
998 ip = map->map_ip(map, ip);
1003 * If this is outside of all known maps,
1004 * and is a negative address, try to look it
1005 * up in the kernel dso, as it might be a
1006 * vsyscall (which executes in user-mode):
1008 if ((long long)ip < 0)
1011 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
1012 dprintf(" ...... map: %Lx -> %Lx\n", *ipp, ip);
1021 return dso->find_symbol(dso, ip);
1024 static int call__match(struct symbol *sym)
1026 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
1032 static struct symbol **
1033 resolve_callchain(struct thread *thread, struct map *map __used,
1034 struct ip_callchain *chain, struct hist_entry *entry)
1036 u64 context = PERF_CONTEXT_MAX;
1037 struct symbol **syms;
1041 syms = calloc(chain->nr, sizeof(*syms));
1043 fprintf(stderr, "Can't allocate memory for symbols\n");
1048 for (i = 0; i < chain->nr; i++) {
1049 u64 ip = chain->ips[i];
1050 struct dso *dso = NULL;
1053 if (ip >= PERF_CONTEXT_MAX) {
1059 case PERF_CONTEXT_HV:
1060 dso = hypervisor_dso;
1062 case PERF_CONTEXT_KERNEL:
1069 sym = resolve_symbol(thread, NULL, &dso, &ip);
1072 if (sort__has_parent && call__match(sym) &&
1074 entry->parent = sym;
1085 * collect histogram counts
1089 hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
1090 struct symbol *sym, u64 ip, struct ip_callchain *chain,
1091 char level, u64 count)
1093 struct rb_node **p = &hist.rb_node;
1094 struct rb_node *parent = NULL;
1095 struct hist_entry *he;
1096 struct symbol **syms = NULL;
1097 struct hist_entry entry = {
1106 .sorted_chain = RB_ROOT
1110 if ((sort__has_parent || callchain) && chain)
1111 syms = resolve_callchain(thread, map, chain, &entry);
1113 while (*p != NULL) {
1115 he = rb_entry(parent, struct hist_entry, rb_node);
1117 cmp = hist_entry__cmp(&entry, he);
1122 append_chain(&he->callchain, chain, syms);
1131 p = &(*p)->rb_right;
1134 he = malloc(sizeof(*he));
1139 callchain_init(&he->callchain);
1140 append_chain(&he->callchain, chain, syms);
1143 rb_link_node(&he->rb_node, parent, p);
1144 rb_insert_color(&he->rb_node, &hist);
1149 static void hist_entry__free(struct hist_entry *he)
1155 * collapse the histogram
1158 static struct rb_root collapse_hists;
1160 static void collapse__insert_entry(struct hist_entry *he)
1162 struct rb_node **p = &collapse_hists.rb_node;
1163 struct rb_node *parent = NULL;
1164 struct hist_entry *iter;
1167 while (*p != NULL) {
1169 iter = rb_entry(parent, struct hist_entry, rb_node);
1171 cmp = hist_entry__collapse(iter, he);
1174 iter->count += he->count;
1175 hist_entry__free(he);
1182 p = &(*p)->rb_right;
1185 rb_link_node(&he->rb_node, parent, p);
1186 rb_insert_color(&he->rb_node, &collapse_hists);
1189 static void collapse__resort(void)
1191 struct rb_node *next;
1192 struct hist_entry *n;
1194 if (!sort__need_collapse)
1197 next = rb_first(&hist);
1199 n = rb_entry(next, struct hist_entry, rb_node);
1200 next = rb_next(&n->rb_node);
1202 rb_erase(&n->rb_node, &hist);
1203 collapse__insert_entry(n);
1208 * reverse the map, sort on count.
1211 static struct rb_root output_hists;
1213 static void output__insert_entry(struct hist_entry *he, u64 min_callchain_hits)
1215 struct rb_node **p = &output_hists.rb_node;
1216 struct rb_node *parent = NULL;
1217 struct hist_entry *iter;
1220 if (callchain_mode == FLAT)
1221 sort_chain_flat(&he->sorted_chain, &he->callchain,
1222 min_callchain_hits);
1223 else if (callchain_mode == GRAPH)
1224 sort_chain_graph(&he->sorted_chain, &he->callchain,
1225 min_callchain_hits);
1228 while (*p != NULL) {
1230 iter = rb_entry(parent, struct hist_entry, rb_node);
1232 if (he->count > iter->count)
1235 p = &(*p)->rb_right;
1238 rb_link_node(&he->rb_node, parent, p);
1239 rb_insert_color(&he->rb_node, &output_hists);
1242 static void output__resort(u64 total_samples)
1244 struct rb_node *next;
1245 struct hist_entry *n;
1246 struct rb_root *tree = &hist;
1247 u64 min_callchain_hits;
1249 min_callchain_hits = total_samples * (callchain_min_percent / 100);
1251 if (sort__need_collapse)
1252 tree = &collapse_hists;
1254 next = rb_first(tree);
1257 n = rb_entry(next, struct hist_entry, rb_node);
1258 next = rb_next(&n->rb_node);
1260 rb_erase(&n->rb_node, tree);
1261 output__insert_entry(n, min_callchain_hits);
1265 static size_t output__fprintf(FILE *fp, u64 total_samples)
1267 struct hist_entry *pos;
1268 struct sort_entry *se;
1274 fprintf(fp, "# (%Ld samples)\n", (u64)total_samples);
1277 fprintf(fp, "# Overhead");
1278 list_for_each_entry(se, &hist_entry__sort_list, list) {
1279 if (exclude_other && (se == &sort_parent))
1281 fprintf(fp, " %s", se->header);
1285 fprintf(fp, "# ........");
1286 list_for_each_entry(se, &hist_entry__sort_list, list) {
1289 if (exclude_other && (se == &sort_parent))
1293 for (i = 0; i < strlen(se->header); i++)
1300 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
1301 pos = rb_entry(nd, struct hist_entry, rb_node);
1302 ret += hist_entry__fprintf(fp, pos, total_samples);
1305 if (sort_order == default_sort_order &&
1306 parent_pattern == default_parent_pattern) {
1308 fprintf(fp, "# (For more details, try: perf report --sort comm,dso,symbol)\n");
1316 static void register_idle_thread(void)
1318 struct thread *thread = threads__findnew(0);
1320 if (thread == NULL ||
1321 thread__set_comm(thread, "[idle]")) {
1322 fprintf(stderr, "problem inserting idle task.\n");
1327 static unsigned long total = 0,
1334 static int validate_chain(struct ip_callchain *chain, event_t *event)
1336 unsigned int chain_size;
1338 chain_size = event->header.size;
1339 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
1341 if (chain->nr*sizeof(u64) > chain_size)
1348 process_sample_event(event_t *event, unsigned long offset, unsigned long head)
1352 struct dso *dso = NULL;
1353 struct thread *thread = threads__findnew(event->ip.pid);
1354 u64 ip = event->ip.ip;
1356 struct map *map = NULL;
1357 void *more_data = event->ip.__more_data;
1358 struct ip_callchain *chain = NULL;
1361 if (sample_type & PERF_SAMPLE_PERIOD) {
1362 period = *(u64 *)more_data;
1363 more_data += sizeof(u64);
1366 dprintf("%p [%p]: PERF_EVENT_SAMPLE (IP, %d): %d: %p period: %Ld\n",
1367 (void *)(offset + head),
1368 (void *)(long)(event->header.size),
1374 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
1377 chain = (void *)more_data;
1379 dprintf("... chain: nr:%Lu\n", chain->nr);
1381 if (validate_chain(chain, event) < 0) {
1382 eprintf("call-chain problem with event, skipping it.\n");
1387 for (i = 0; i < chain->nr; i++)
1388 dprintf("..... %2d: %016Lx\n", i, chain->ips[i]);
1392 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1394 if (thread == NULL) {
1395 eprintf("problem processing %d event, skipping it.\n",
1396 event->header.type);
1400 if (comm_list && !strlist__has_entry(comm_list, thread->comm))
1403 cpumode = event->header.misc & PERF_EVENT_MISC_CPUMODE_MASK;
1405 if (cpumode == PERF_EVENT_MISC_KERNEL) {
1411 dprintf(" ...... dso: %s\n", dso->name);
1413 } else if (cpumode == PERF_EVENT_MISC_USER) {
1422 dso = hypervisor_dso;
1424 dprintf(" ...... dso: [hypervisor]\n");
1427 if (show & show_mask) {
1428 struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
1430 if (dso_list && dso && dso->name && !strlist__has_entry(dso_list, dso->name))
1433 if (sym_list && sym && !strlist__has_entry(sym_list, sym->name))
1436 if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
1437 eprintf("problem incrementing symbol count, skipping event\n");
1447 process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
1449 struct thread *thread = threads__findnew(event->mmap.pid);
1450 struct map *map = map__new(&event->mmap);
1452 dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
1453 (void *)(offset + head),
1454 (void *)(long)(event->header.size),
1456 (void *)(long)event->mmap.start,
1457 (void *)(long)event->mmap.len,
1458 (void *)(long)event->mmap.pgoff,
1459 event->mmap.filename);
1461 if (thread == NULL || map == NULL) {
1462 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
1466 thread__insert_map(thread, map);
1473 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
1475 struct thread *thread = threads__findnew(event->comm.pid);
1477 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
1478 (void *)(offset + head),
1479 (void *)(long)(event->header.size),
1480 event->comm.comm, event->comm.pid);
1482 if (thread == NULL ||
1483 thread__set_comm(thread, event->comm.comm)) {
1484 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
1493 process_fork_event(event_t *event, unsigned long offset, unsigned long head)
1495 struct thread *thread = threads__findnew(event->fork.pid);
1496 struct thread *parent = threads__findnew(event->fork.ppid);
1498 dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
1499 (void *)(offset + head),
1500 (void *)(long)(event->header.size),
1501 event->fork.pid, event->fork.ppid);
1503 if (!thread || !parent || thread__fork(thread, parent)) {
1504 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
1513 process_period_event(event_t *event, unsigned long offset, unsigned long head)
1515 dprintf("%p [%p]: PERF_EVENT_PERIOD: time:%Ld, id:%Ld: period:%Ld\n",
1516 (void *)(offset + head),
1517 (void *)(long)(event->header.size),
1520 event->period.sample_period);
1526 process_lost_event(event_t *event, unsigned long offset, unsigned long head)
1528 dprintf("%p [%p]: PERF_EVENT_LOST: id:%Ld: lost:%Ld\n",
1529 (void *)(offset + head),
1530 (void *)(long)(event->header.size),
1534 total_lost += event->lost.lost;
1539 static void trace_event(event_t *event)
1541 unsigned char *raw_event = (void *)event;
1542 char *color = PERF_COLOR_BLUE;
1549 cdprintf("\n. ... raw event: size %d bytes\n", event->header.size);
1551 for (i = 0; i < event->header.size; i++) {
1552 if ((i & 15) == 0) {
1554 cdprintf(" %04x: ", i);
1557 cdprintf(" %02x", raw_event[i]);
1559 if (((i & 15) == 15) || i == event->header.size-1) {
1561 for (j = 0; j < 15-(i & 15); j++)
1563 for (j = 0; j < (i & 15); j++) {
1564 if (isprint(raw_event[i-15+j]))
1565 cdprintf("%c", raw_event[i-15+j]);
1576 process_read_event(event_t *event, unsigned long offset, unsigned long head)
1578 dprintf("%p [%p]: PERF_EVENT_READ: %d %d %Lu\n",
1579 (void *)(offset + head),
1580 (void *)(long)(event->header.size),
1589 process_event(event_t *event, unsigned long offset, unsigned long head)
1593 switch (event->header.type) {
1594 case PERF_EVENT_SAMPLE:
1595 return process_sample_event(event, offset, head);
1597 case PERF_EVENT_MMAP:
1598 return process_mmap_event(event, offset, head);
1600 case PERF_EVENT_COMM:
1601 return process_comm_event(event, offset, head);
1603 case PERF_EVENT_FORK:
1604 return process_fork_event(event, offset, head);
1606 case PERF_EVENT_PERIOD:
1607 return process_period_event(event, offset, head);
1609 case PERF_EVENT_LOST:
1610 return process_lost_event(event, offset, head);
1612 case PERF_EVENT_READ:
1613 return process_read_event(event, offset, head);
1616 * We dont process them right now but they are fine:
1619 case PERF_EVENT_THROTTLE:
1620 case PERF_EVENT_UNTHROTTLE:
1630 static struct perf_header *header;
1632 static u64 perf_header__sample_type(void)
1634 u64 sample_type = 0;
1637 for (i = 0; i < header->attrs; i++) {
1638 struct perf_header_attr *attr = header->attr[i];
1641 sample_type = attr->attr.sample_type;
1642 else if (sample_type != attr->attr.sample_type)
1643 die("non matching sample_type");
1649 static int __cmd_report(void)
1651 int ret, rc = EXIT_FAILURE;
1652 unsigned long offset = 0;
1653 unsigned long head, shift;
1659 register_idle_thread();
1661 input = open(input_name, O_RDONLY);
1663 fprintf(stderr, " failed to open file: %s", input_name);
1664 if (!strcmp(input_name, "perf.data"))
1665 fprintf(stderr, " (try 'perf record' first)");
1666 fprintf(stderr, "\n");
1670 ret = fstat(input, &stat);
1672 perror("failed to stat file");
1676 if (!stat.st_size) {
1677 fprintf(stderr, "zero-sized file, nothing to do!\n");
1681 header = perf_header__read(input);
1682 head = header->data_offset;
1684 sample_type = perf_header__sample_type();
1686 if (sort__has_parent && !(sample_type & PERF_SAMPLE_CALLCHAIN)) {
1687 fprintf(stderr, "selected --sort parent, but no callchain data\n");
1691 if (load_kernel() < 0) {
1692 perror("failed to load kernel symbols");
1693 return EXIT_FAILURE;
1697 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
1698 perror("failed to get the current directory");
1699 return EXIT_FAILURE;
1701 cwdlen = strlen(cwd);
1707 shift = page_size * (head / page_size);
1712 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1713 MAP_SHARED, input, offset);
1714 if (buf == MAP_FAILED) {
1715 perror("failed to mmap file");
1720 event = (event_t *)(buf + head);
1722 size = event->header.size;
1726 if (head + event->header.size >= page_size * mmap_window) {
1729 shift = page_size * (head / page_size);
1731 ret = munmap(buf, page_size * mmap_window);
1739 size = event->header.size;
1741 dprintf("\n%p [%p]: event: %d\n",
1742 (void *)(offset + head),
1743 (void *)(long)event->header.size,
1744 event->header.type);
1746 if (!size || process_event(event, offset, head) < 0) {
1748 dprintf("%p [%p]: skipping unknown header type: %d\n",
1749 (void *)(offset + head),
1750 (void *)(long)(event->header.size),
1751 event->header.type);
1756 * assume we lost track of the stream, check alignment, and
1757 * increment a single u64 in the hope to catch on again 'soon'.
1760 if (unlikely(head & 7))
1768 if (offset + head >= header->data_offset + header->data_size)
1771 if (offset + head < (unsigned long)stat.st_size)
1778 dprintf(" IP events: %10ld\n", total);
1779 dprintf(" mmap events: %10ld\n", total_mmap);
1780 dprintf(" comm events: %10ld\n", total_comm);
1781 dprintf(" fork events: %10ld\n", total_fork);
1782 dprintf(" lost events: %10ld\n", total_lost);
1783 dprintf(" unknown events: %10ld\n", total_unknown);
1789 threads__fprintf(stdout);
1792 dsos__fprintf(stdout);
1795 output__resort(total);
1796 output__fprintf(stdout, total);
1802 parse_callchain_opt(const struct option *opt __used, const char *arg,
1813 tok = strtok((char *)arg, ",");
1817 /* get the output mode */
1818 if (!strncmp(tok, "graph", strlen(arg)))
1819 callchain_mode = GRAPH;
1821 else if (!strncmp(tok, "flat", strlen(arg)))
1822 callchain_mode = FLAT;
1826 /* get the min percentage */
1827 tok = strtok(NULL, ",");
1831 callchain_min_percent = strtod(tok, &endptr);
1838 static const char * const report_usage[] = {
1839 "perf report [<options>] <command>",
1843 static const struct option options[] = {
1844 OPT_STRING('i', "input", &input_name, "file",
1846 OPT_BOOLEAN('v', "verbose", &verbose,
1847 "be more verbose (show symbol address, etc)"),
1848 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1849 "dump raw trace in ASCII"),
1850 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
1851 OPT_BOOLEAN('m', "modules", &modules,
1852 "load module symbols - WARNING: use only with -k and LIVE kernel"),
1853 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1854 "sort by key(s): pid, comm, dso, symbol, parent"),
1855 OPT_BOOLEAN('P', "full-paths", &full_paths,
1856 "Don't shorten the pathnames taking into account the cwd"),
1857 OPT_STRING('p', "parent", &parent_pattern, "regex",
1858 "regex filter to identify parent, see: '--sort parent'"),
1859 OPT_BOOLEAN('x', "exclude-other", &exclude_other,
1860 "Only display entries with parent-match"),
1861 OPT_CALLBACK_DEFAULT('c', "callchain", NULL, "output_type,min_percent",
1862 "Display callchains using output_type and min percent threshold. "
1863 "Default: flat,0", &parse_callchain_opt, "flat,100"),
1864 OPT_STRING('d', "dsos", &dso_list_str, "dso[,dso...]",
1865 "only consider symbols in these dsos"),
1866 OPT_STRING('C', "comms", &comm_list_str, "comm[,comm...]",
1867 "only consider symbols in these comms"),
1868 OPT_STRING('S', "symbols", &sym_list_str, "symbol[,symbol...]",
1869 "only consider these symbols"),
1873 static void setup_sorting(void)
1875 char *tmp, *tok, *str = strdup(sort_order);
1877 for (tok = strtok_r(str, ", ", &tmp);
1878 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1879 if (sort_dimension__add(tok) < 0) {
1880 error("Unknown --sort key: `%s'", tok);
1881 usage_with_options(report_usage, options);
1888 static void setup_list(struct strlist **list, const char *list_str,
1889 const char *list_name)
1892 *list = strlist__new(true, list_str);
1894 fprintf(stderr, "problems parsing %s list\n",
1901 int cmd_report(int argc, const char **argv, const char *prefix __used)
1905 page_size = getpagesize();
1907 argc = parse_options(argc, argv, options, report_usage, 0);
1911 if (parent_pattern != default_parent_pattern)
1912 sort_dimension__add("parent");
1917 * Any (unrecognized) arguments left?
1920 usage_with_options(report_usage, options);
1922 setup_list(&dso_list, dso_list_str, "dso");
1923 setup_list(&comm_list, comm_list_str, "comm");
1924 setup_list(&sym_list, sym_list_str, "symbol");
1928 return __cmd_report();