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 "util/list.h"
14 #include "util/cache.h"
15 #include "util/rbtree.h"
16 #include "util/symbol.h"
17 #include "util/string.h"
21 #include "util/parse-options.h"
22 #include "util/parse-events.h"
28 static char const *input_name = "perf.data";
29 static char *vmlinux = NULL;
31 static char default_sort_order[] = "comm,dso";
32 static char *sort_order = default_sort_order;
35 static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
37 static int dump_trace = 0;
38 #define dprintf(x...) do { if (dump_trace) printf(x); } while (0)
41 static int full_paths;
43 static unsigned long page_size;
44 static unsigned long mmap_window = 32;
47 struct perf_event_header header;
53 struct perf_event_header header;
58 char filename[PATH_MAX];
62 struct perf_event_header header;
68 struct perf_event_header header;
73 struct perf_event_header header;
79 typedef union event_union {
80 struct perf_event_header header;
82 struct mmap_event mmap;
83 struct comm_event comm;
84 struct fork_event fork;
85 struct period_event period;
88 static LIST_HEAD(dsos);
89 static struct dso *kernel_dso;
90 static struct dso *vdso;
92 static void dsos__add(struct dso *dso)
94 list_add_tail(&dso->node, &dsos);
97 static struct dso *dsos__find(const char *name)
101 list_for_each_entry(pos, &dsos, node)
102 if (strcmp(pos->name, name) == 0)
107 static struct dso *dsos__findnew(const char *name)
109 struct dso *dso = dsos__find(name);
115 dso = dso__new(name, 0);
119 nr = dso__load(dso, NULL, verbose);
122 fprintf(stderr, "Failed to open: %s\n", name);
125 if (!nr && verbose) {
127 "No symbols found in: %s, maybe install a debug package?\n",
140 static void dsos__fprintf(FILE *fp)
144 list_for_each_entry(pos, &dsos, node)
145 dso__fprintf(pos, fp);
148 static struct symbol *vdso__find_symbol(struct dso *dso, uint64_t ip)
150 return dso__find_symbol(kernel_dso, ip);
153 static int load_kernel(void)
157 kernel_dso = dso__new("[kernel]", 0);
161 err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose);
163 dso__delete(kernel_dso);
166 dsos__add(kernel_dso);
168 vdso = dso__new("[vdso]", 0);
172 vdso->find_symbol = vdso__find_symbol;
179 static char __cwd[PATH_MAX];
180 static char *cwd = __cwd;
183 static int strcommon(const char *pathname)
187 while (pathname[n] == cwd[n] && n < cwdlen)
194 struct list_head node;
198 uint64_t (*map_ip)(struct map *, uint64_t);
202 static uint64_t map__map_ip(struct map *map, uint64_t ip)
204 return ip - map->start + map->pgoff;
207 static uint64_t vdso__map_ip(struct map *map, uint64_t ip)
212 static inline int is_anon_memory(const char *filename)
214 return strcmp(filename, "//anon") == 0;
217 static struct map *map__new(struct mmap_event *event)
219 struct map *self = malloc(sizeof(*self));
222 const char *filename = event->filename;
223 char newfilename[PATH_MAX];
227 int n = strcommon(filename);
230 snprintf(newfilename, sizeof(newfilename),
231 ".%s", filename + n);
232 filename = newfilename;
236 anon = is_anon_memory(filename);
239 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
240 filename = newfilename;
243 self->start = event->start;
244 self->end = event->start + event->len;
245 self->pgoff = event->pgoff;
247 self->dso = dsos__findnew(filename);
248 if (self->dso == NULL)
251 if (self->dso == vdso || anon)
252 self->map_ip = vdso__map_ip;
254 self->map_ip = map__map_ip;
262 static struct map *map__clone(struct map *self)
264 struct map *map = malloc(sizeof(*self));
269 memcpy(map, self, sizeof(*self));
274 static int map__overlap(struct map *l, struct map *r)
276 if (l->start > r->start) {
282 if (l->end > r->start)
288 static size_t map__fprintf(struct map *self, FILE *fp)
290 return fprintf(fp, " %"PRIx64"-%"PRIx64" %"PRIx64" %s\n",
291 self->start, self->end, self->pgoff, self->dso->name);
296 struct rb_node rb_node;
297 struct list_head maps;
302 static struct thread *thread__new(pid_t pid)
304 struct thread *self = malloc(sizeof(*self));
308 self->comm = malloc(32);
310 snprintf(self->comm, 32, ":%d", self->pid);
311 INIT_LIST_HEAD(&self->maps);
317 static int thread__set_comm(struct thread *self, const char *comm)
321 self->comm = strdup(comm);
322 return self->comm ? 0 : -ENOMEM;
325 static size_t thread__fprintf(struct thread *self, FILE *fp)
328 size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
330 list_for_each_entry(pos, &self->maps, node)
331 ret += map__fprintf(pos, fp);
337 static struct rb_root threads;
338 static struct thread *last_match;
340 static struct thread *threads__findnew(pid_t pid)
342 struct rb_node **p = &threads.rb_node;
343 struct rb_node *parent = NULL;
347 * Font-end cache - PID lookups come in blocks,
348 * so most of the time we dont have to look up
351 if (last_match && last_match->pid == pid)
356 th = rb_entry(parent, struct thread, rb_node);
358 if (th->pid == pid) {
369 th = thread__new(pid);
371 rb_link_node(&th->rb_node, parent, p);
372 rb_insert_color(&th->rb_node, &threads);
379 static void thread__insert_map(struct thread *self, struct map *map)
381 struct map *pos, *tmp;
383 list_for_each_entry_safe(pos, tmp, &self->maps, node) {
384 if (map__overlap(pos, map)) {
385 list_del_init(&pos->node);
391 list_add_tail(&map->node, &self->maps);
394 static int thread__fork(struct thread *self, struct thread *parent)
400 self->comm = strdup(parent->comm);
404 list_for_each_entry(map, &parent->maps, node) {
405 struct map *new = map__clone(map);
408 thread__insert_map(self, new);
414 static struct map *thread__find_map(struct thread *self, uint64_t ip)
421 list_for_each_entry(pos, &self->maps, node)
422 if (ip >= pos->start && ip <= pos->end)
428 static size_t threads__fprintf(FILE *fp)
433 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
434 struct thread *pos = rb_entry(nd, struct thread, rb_node);
436 ret += thread__fprintf(pos, fp);
443 * histogram, sorted on item, collects counts
446 static struct rb_root hist;
449 struct rb_node rb_node;
451 struct thread *thread;
462 * configurable sorting bits
466 struct list_head list;
470 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
471 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
472 size_t (*print)(FILE *fp, struct hist_entry *);
478 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
480 return right->thread->pid - left->thread->pid;
484 sort__thread_print(FILE *fp, struct hist_entry *self)
486 return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
489 static struct sort_entry sort_thread = {
490 .header = " Command: Pid",
491 .cmp = sort__thread_cmp,
492 .print = sort__thread_print,
498 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
500 return right->thread->pid - left->thread->pid;
504 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
506 char *comm_l = left->thread->comm;
507 char *comm_r = right->thread->comm;
509 if (!comm_l || !comm_r) {
510 if (!comm_l && !comm_r)
518 return strcmp(comm_l, comm_r);
522 sort__comm_print(FILE *fp, struct hist_entry *self)
524 return fprintf(fp, "%16s", self->thread->comm);
527 static struct sort_entry sort_comm = {
528 .header = " Command",
529 .cmp = sort__comm_cmp,
530 .collapse = sort__comm_collapse,
531 .print = sort__comm_print,
537 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
539 struct dso *dso_l = left->dso;
540 struct dso *dso_r = right->dso;
542 if (!dso_l || !dso_r) {
543 if (!dso_l && !dso_r)
551 return strcmp(dso_l->name, dso_r->name);
555 sort__dso_print(FILE *fp, struct hist_entry *self)
558 return fprintf(fp, "%-25s", self->dso->name);
560 return fprintf(fp, "%016llx ", (__u64)self->ip);
563 static struct sort_entry sort_dso = {
564 .header = "Shared Object ",
565 .cmp = sort__dso_cmp,
566 .print = sort__dso_print,
572 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
576 if (left->sym == right->sym)
579 ip_l = left->sym ? left->sym->start : left->ip;
580 ip_r = right->sym ? right->sym->start : right->ip;
582 return (int64_t)(ip_r - ip_l);
586 sort__sym_print(FILE *fp, struct hist_entry *self)
591 ret += fprintf(fp, "%#018llx ", (__u64)self->ip);
594 ret += fprintf(fp, "[%c] %s",
595 self->dso == kernel_dso ? 'k' : '.', self->sym->name);
597 ret += fprintf(fp, "%#016llx", (__u64)self->ip);
603 static struct sort_entry sort_sym = {
605 .cmp = sort__sym_cmp,
606 .print = sort__sym_print,
609 static int sort__need_collapse = 0;
611 struct sort_dimension {
613 struct sort_entry *entry;
617 static struct sort_dimension sort_dimensions[] = {
618 { .name = "pid", .entry = &sort_thread, },
619 { .name = "comm", .entry = &sort_comm, },
620 { .name = "dso", .entry = &sort_dso, },
621 { .name = "symbol", .entry = &sort_sym, },
624 static LIST_HEAD(hist_entry__sort_list);
626 static int sort_dimension__add(char *tok)
630 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
631 struct sort_dimension *sd = &sort_dimensions[i];
636 if (strncasecmp(tok, sd->name, strlen(tok)))
639 if (sd->entry->collapse)
640 sort__need_collapse = 1;
642 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
652 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
654 struct sort_entry *se;
657 list_for_each_entry(se, &hist_entry__sort_list, list) {
658 cmp = se->cmp(left, right);
667 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
669 struct sort_entry *se;
672 list_for_each_entry(se, &hist_entry__sort_list, list) {
673 int64_t (*f)(struct hist_entry *, struct hist_entry *);
675 f = se->collapse ?: se->cmp;
677 cmp = f(left, right);
686 hist_entry__fprintf(FILE *fp, struct hist_entry *self, uint64_t total_samples)
688 struct sort_entry *se;
692 double percent = self->count * 100.0 / total_samples;
693 char *color = PERF_COLOR_NORMAL;
696 * We color high-overhead entries in red, mid-overhead
697 * entries in green - and keep the low overhead places
700 if (percent >= 5.0) {
701 color = PERF_COLOR_RED;
704 color = PERF_COLOR_GREEN;
707 ret = color_fprintf(fp, color, " %6.2f%%",
708 (self->count * 100.0) / total_samples);
710 ret = fprintf(fp, "%12d ", self->count);
712 list_for_each_entry(se, &hist_entry__sort_list, list) {
714 ret += se->print(fp, self);
717 ret += fprintf(fp, "\n");
723 * collect histogram counts
727 hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
728 struct symbol *sym, uint64_t ip, char level)
730 struct rb_node **p = &hist.rb_node;
731 struct rb_node *parent = NULL;
732 struct hist_entry *he;
733 struct hist_entry entry = {
746 he = rb_entry(parent, struct hist_entry, rb_node);
748 cmp = hist_entry__cmp(&entry, he);
761 he = malloc(sizeof(*he));
765 rb_link_node(&he->rb_node, parent, p);
766 rb_insert_color(&he->rb_node, &hist);
771 static void hist_entry__free(struct hist_entry *he)
777 * collapse the histogram
780 static struct rb_root collapse_hists;
782 static void collapse__insert_entry(struct hist_entry *he)
784 struct rb_node **p = &collapse_hists.rb_node;
785 struct rb_node *parent = NULL;
786 struct hist_entry *iter;
791 iter = rb_entry(parent, struct hist_entry, rb_node);
793 cmp = hist_entry__collapse(iter, he);
796 iter->count += he->count;
797 hist_entry__free(he);
807 rb_link_node(&he->rb_node, parent, p);
808 rb_insert_color(&he->rb_node, &collapse_hists);
811 static void collapse__resort(void)
813 struct rb_node *next;
814 struct hist_entry *n;
816 if (!sort__need_collapse)
819 next = rb_first(&hist);
821 n = rb_entry(next, struct hist_entry, rb_node);
822 next = rb_next(&n->rb_node);
824 rb_erase(&n->rb_node, &hist);
825 collapse__insert_entry(n);
830 * reverse the map, sort on count.
833 static struct rb_root output_hists;
835 static void output__insert_entry(struct hist_entry *he)
837 struct rb_node **p = &output_hists.rb_node;
838 struct rb_node *parent = NULL;
839 struct hist_entry *iter;
843 iter = rb_entry(parent, struct hist_entry, rb_node);
845 if (he->count > iter->count)
851 rb_link_node(&he->rb_node, parent, p);
852 rb_insert_color(&he->rb_node, &output_hists);
855 static void output__resort(void)
857 struct rb_node *next;
858 struct hist_entry *n;
859 struct rb_root *tree = &hist;
861 if (sort__need_collapse)
862 tree = &collapse_hists;
864 next = rb_first(tree);
867 n = rb_entry(next, struct hist_entry, rb_node);
868 next = rb_next(&n->rb_node);
870 rb_erase(&n->rb_node, tree);
871 output__insert_entry(n);
875 static size_t output__fprintf(FILE *fp, uint64_t total_samples)
877 struct hist_entry *pos;
878 struct sort_entry *se;
884 fprintf(fp, "# (%Ld samples)\n", (__u64)total_samples);
887 fprintf(fp, "# Overhead");
888 list_for_each_entry(se, &hist_entry__sort_list, list)
889 fprintf(fp, " %s", se->header);
892 fprintf(fp, "# ........");
893 list_for_each_entry(se, &hist_entry__sort_list, list) {
897 for (i = 0; i < strlen(se->header); i++)
904 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
905 pos = rb_entry(nd, struct hist_entry, rb_node);
906 ret += hist_entry__fprintf(fp, pos, total_samples);
909 if (!strcmp(sort_order, default_sort_order)) {
911 fprintf(fp, "# (For more details, try: perf report --sort comm,dso,symbol)\n");
919 static void register_idle_thread(void)
921 struct thread *thread = threads__findnew(0);
923 if (thread == NULL ||
924 thread__set_comm(thread, "[idle]")) {
925 fprintf(stderr, "problem inserting idle task.\n");
930 static unsigned long total = 0,
937 process_overflow_event(event_t *event, unsigned long offset, unsigned long head)
941 struct dso *dso = NULL;
942 struct thread *thread = threads__findnew(event->ip.pid);
943 uint64_t ip = event->ip.ip;
944 struct map *map = NULL;
946 dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
947 (void *)(offset + head),
948 (void *)(long)(event->header.size),
953 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
955 if (thread == NULL) {
956 fprintf(stderr, "problem processing %d event, skipping it.\n",
961 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
967 dprintf(" ...... dso: %s\n", dso->name);
969 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
974 map = thread__find_map(thread, ip);
976 ip = map->map_ip(map, ip);
980 * If this is outside of all known maps,
981 * and is a negative address, try to look it
982 * up in the kernel dso, as it might be a
983 * vsyscall (which executes in user-mode):
985 if ((long long)ip < 0)
988 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
993 dprintf(" ...... dso: [hypervisor]\n");
996 if (show & show_mask) {
997 struct symbol *sym = NULL;
1000 sym = dso->find_symbol(dso, ip);
1002 if (hist_entry__add(thread, map, dso, sym, ip, level)) {
1004 "problem incrementing symbol count, skipping event\n");
1014 process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
1016 struct thread *thread = threads__findnew(event->mmap.pid);
1017 struct map *map = map__new(&event->mmap);
1019 dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
1020 (void *)(offset + head),
1021 (void *)(long)(event->header.size),
1023 (void *)(long)event->mmap.start,
1024 (void *)(long)event->mmap.len,
1025 (void *)(long)event->mmap.pgoff,
1026 event->mmap.filename);
1028 if (thread == NULL || map == NULL) {
1029 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
1033 thread__insert_map(thread, map);
1040 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
1042 struct thread *thread = threads__findnew(event->comm.pid);
1044 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
1045 (void *)(offset + head),
1046 (void *)(long)(event->header.size),
1047 event->comm.comm, event->comm.pid);
1049 if (thread == NULL ||
1050 thread__set_comm(thread, event->comm.comm)) {
1051 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
1060 process_fork_event(event_t *event, unsigned long offset, unsigned long head)
1062 struct thread *thread = threads__findnew(event->fork.pid);
1063 struct thread *parent = threads__findnew(event->fork.ppid);
1065 dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
1066 (void *)(offset + head),
1067 (void *)(long)(event->header.size),
1068 event->fork.pid, event->fork.ppid);
1070 if (!thread || !parent || thread__fork(thread, parent)) {
1071 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
1080 process_period_event(event_t *event, unsigned long offset, unsigned long head)
1082 dprintf("%p [%p]: PERF_EVENT_PERIOD: time:%Ld, id:%Ld: period:%Ld\n",
1083 (void *)(offset + head),
1084 (void *)(long)(event->header.size),
1087 event->period.sample_period);
1093 process_event(event_t *event, unsigned long offset, unsigned long head)
1095 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW)
1096 return process_overflow_event(event, offset, head);
1098 switch (event->header.type) {
1099 case PERF_EVENT_MMAP:
1100 return process_mmap_event(event, offset, head);
1102 case PERF_EVENT_COMM:
1103 return process_comm_event(event, offset, head);
1105 case PERF_EVENT_FORK:
1106 return process_fork_event(event, offset, head);
1108 case PERF_EVENT_PERIOD:
1109 return process_period_event(event, offset, head);
1111 * We dont process them right now but they are fine:
1114 case PERF_EVENT_THROTTLE:
1115 case PERF_EVENT_UNTHROTTLE:
1125 static int __cmd_report(void)
1127 int ret, rc = EXIT_FAILURE;
1128 unsigned long offset = 0;
1129 unsigned long head = 0;
1135 register_idle_thread();
1137 input = open(input_name, O_RDONLY);
1139 fprintf(stderr, " failed to open file: %s", input_name);
1140 if (!strcmp(input_name, "perf.data"))
1141 fprintf(stderr, " (try 'perf record' first)");
1142 fprintf(stderr, "\n");
1146 ret = fstat(input, &stat);
1148 perror("failed to stat file");
1152 if (!stat.st_size) {
1153 fprintf(stderr, "zero-sized file, nothing to do!\n");
1157 if (load_kernel() < 0) {
1158 perror("failed to load kernel symbols");
1159 return EXIT_FAILURE;
1163 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
1164 perror("failed to get the current directory");
1165 return EXIT_FAILURE;
1167 cwdlen = strlen(cwd);
1173 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1174 MAP_SHARED, input, offset);
1175 if (buf == MAP_FAILED) {
1176 perror("failed to mmap file");
1181 event = (event_t *)(buf + head);
1183 size = event->header.size;
1187 if (head + event->header.size >= page_size * mmap_window) {
1188 unsigned long shift = page_size * (head / page_size);
1191 ret = munmap(buf, page_size * mmap_window);
1199 size = event->header.size;
1201 dprintf("%p [%p]: event: %d\n",
1202 (void *)(offset + head),
1203 (void *)(long)event->header.size,
1204 event->header.type);
1206 if (!size || process_event(event, offset, head) < 0) {
1208 dprintf("%p [%p]: skipping unknown header type: %d\n",
1209 (void *)(offset + head),
1210 (void *)(long)(event->header.size),
1211 event->header.type);
1216 * assume we lost track of the stream, check alignment, and
1217 * increment a single u64 in the hope to catch on again 'soon'.
1220 if (unlikely(head & 7))
1228 if (offset + head < stat.st_size)
1234 dprintf(" IP events: %10ld\n", total);
1235 dprintf(" mmap events: %10ld\n", total_mmap);
1236 dprintf(" comm events: %10ld\n", total_comm);
1237 dprintf(" fork events: %10ld\n", total_fork);
1238 dprintf(" unknown events: %10ld\n", total_unknown);
1244 threads__fprintf(stdout);
1247 dsos__fprintf(stdout);
1251 output__fprintf(stdout, total);
1256 static const char * const report_usage[] = {
1257 "perf report [<options>] <command>",
1261 static const struct option options[] = {
1262 OPT_STRING('i', "input", &input_name, "file",
1264 OPT_BOOLEAN('v', "verbose", &verbose,
1265 "be more verbose (show symbol address, etc)"),
1266 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1267 "dump raw trace in ASCII"),
1268 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
1269 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1270 "sort by key(s): pid, comm, dso, symbol. Default: pid,symbol"),
1271 OPT_BOOLEAN('P', "full-paths", &full_paths,
1272 "Don't shorten the pathnames taking into account the cwd"),
1276 static void setup_sorting(void)
1278 char *tmp, *tok, *str = strdup(sort_order);
1280 for (tok = strtok_r(str, ", ", &tmp);
1281 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1282 if (sort_dimension__add(tok) < 0) {
1283 error("Unknown --sort key: `%s'", tok);
1284 usage_with_options(report_usage, options);
1291 int cmd_report(int argc, const char **argv, const char *prefix)
1295 page_size = getpagesize();
1297 argc = parse_options(argc, argv, options, report_usage, 0);
1302 * Any (unrecognized) arguments left?
1305 usage_with_options(report_usage, options);
1309 return __cmd_report();