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;
72 typedef union event_union {
73 struct perf_event_header header;
75 struct mmap_event mmap;
76 struct comm_event comm;
77 struct fork_event fork;
80 static LIST_HEAD(dsos);
81 static struct dso *kernel_dso;
82 static struct dso *vdso;
84 static void dsos__add(struct dso *dso)
86 list_add_tail(&dso->node, &dsos);
89 static struct dso *dsos__find(const char *name)
93 list_for_each_entry(pos, &dsos, node)
94 if (strcmp(pos->name, name) == 0)
99 static struct dso *dsos__findnew(const char *name)
101 struct dso *dso = dsos__find(name);
107 dso = dso__new(name, 0);
111 nr = dso__load(dso, NULL, verbose);
114 fprintf(stderr, "Failed to open: %s\n", name);
117 if (!nr && verbose) {
119 "No symbols found in: %s, maybe install a debug package?\n",
132 static void dsos__fprintf(FILE *fp)
136 list_for_each_entry(pos, &dsos, node)
137 dso__fprintf(pos, fp);
140 static struct symbol *vdso__find_symbol(struct dso *dso, uint64_t ip)
142 return dso__find_symbol(kernel_dso, ip);
145 static int load_kernel(void)
149 kernel_dso = dso__new("[kernel]", 0);
153 err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose);
155 dso__delete(kernel_dso);
158 dsos__add(kernel_dso);
160 vdso = dso__new("[vdso]", 0);
164 vdso->find_symbol = vdso__find_symbol;
171 static char __cwd[PATH_MAX];
172 static char *cwd = __cwd;
175 static int strcommon(const char *pathname)
179 while (pathname[n] == cwd[n] && n < cwdlen)
186 struct list_head node;
190 uint64_t (*map_ip)(struct map *, uint64_t);
194 static uint64_t map__map_ip(struct map *map, uint64_t ip)
196 return ip - map->start + map->pgoff;
199 static uint64_t vdso__map_ip(struct map *map, uint64_t ip)
204 static struct map *map__new(struct mmap_event *event)
206 struct map *self = malloc(sizeof(*self));
209 const char *filename = event->filename;
210 char newfilename[PATH_MAX];
213 int n = strcommon(filename);
216 snprintf(newfilename, sizeof(newfilename),
217 ".%s", filename + n);
218 filename = newfilename;
222 self->start = event->start;
223 self->end = event->start + event->len;
224 self->pgoff = event->pgoff;
226 self->dso = dsos__findnew(filename);
227 if (self->dso == NULL)
230 if (self->dso == vdso)
231 self->map_ip = vdso__map_ip;
233 self->map_ip = map__map_ip;
241 static struct map *map__clone(struct map *self)
243 struct map *map = malloc(sizeof(*self));
248 memcpy(map, self, sizeof(*self));
253 static int map__overlap(struct map *l, struct map *r)
255 if (l->start > r->start) {
261 if (l->end > r->start)
267 static size_t map__fprintf(struct map *self, FILE *fp)
269 return fprintf(fp, " %"PRIx64"-%"PRIx64" %"PRIx64" %s\n",
270 self->start, self->end, self->pgoff, self->dso->name);
275 struct rb_node rb_node;
276 struct list_head maps;
281 static struct thread *thread__new(pid_t pid)
283 struct thread *self = malloc(sizeof(*self));
287 self->comm = malloc(32);
289 snprintf(self->comm, 32, ":%d", self->pid);
290 INIT_LIST_HEAD(&self->maps);
296 static int thread__set_comm(struct thread *self, const char *comm)
300 self->comm = strdup(comm);
301 return self->comm ? 0 : -ENOMEM;
304 static size_t thread__fprintf(struct thread *self, FILE *fp)
307 size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
309 list_for_each_entry(pos, &self->maps, node)
310 ret += map__fprintf(pos, fp);
316 static struct rb_root threads;
317 static struct thread *last_match;
319 static struct thread *threads__findnew(pid_t pid)
321 struct rb_node **p = &threads.rb_node;
322 struct rb_node *parent = NULL;
326 * Font-end cache - PID lookups come in blocks,
327 * so most of the time we dont have to look up
330 if (last_match && last_match->pid == pid)
335 th = rb_entry(parent, struct thread, rb_node);
337 if (th->pid == pid) {
348 th = thread__new(pid);
350 rb_link_node(&th->rb_node, parent, p);
351 rb_insert_color(&th->rb_node, &threads);
358 static void thread__insert_map(struct thread *self, struct map *map)
360 struct map *pos, *tmp;
362 list_for_each_entry_safe(pos, tmp, &self->maps, node) {
363 if (map__overlap(pos, map)) {
364 list_del_init(&pos->node);
370 list_add_tail(&map->node, &self->maps);
373 static int thread__fork(struct thread *self, struct thread *parent)
379 self->comm = strdup(parent->comm);
383 list_for_each_entry(map, &parent->maps, node) {
384 struct map *new = map__clone(map);
387 thread__insert_map(self, new);
393 static struct map *thread__find_map(struct thread *self, uint64_t ip)
400 list_for_each_entry(pos, &self->maps, node)
401 if (ip >= pos->start && ip <= pos->end)
407 static size_t threads__fprintf(FILE *fp)
412 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
413 struct thread *pos = rb_entry(nd, struct thread, rb_node);
415 ret += thread__fprintf(pos, fp);
422 * histogram, sorted on item, collects counts
425 static struct rb_root hist;
428 struct rb_node rb_node;
430 struct thread *thread;
441 * configurable sorting bits
445 struct list_head list;
449 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
450 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
451 size_t (*print)(FILE *fp, struct hist_entry *);
457 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
459 return right->thread->pid - left->thread->pid;
463 sort__thread_print(FILE *fp, struct hist_entry *self)
465 return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
468 static struct sort_entry sort_thread = {
469 .header = " Command: Pid",
470 .cmp = sort__thread_cmp,
471 .print = sort__thread_print,
477 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
479 return right->thread->pid - left->thread->pid;
483 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
485 char *comm_l = left->thread->comm;
486 char *comm_r = right->thread->comm;
488 if (!comm_l || !comm_r) {
489 if (!comm_l && !comm_r)
497 return strcmp(comm_l, comm_r);
501 sort__comm_print(FILE *fp, struct hist_entry *self)
503 return fprintf(fp, "%16s", self->thread->comm);
506 static struct sort_entry sort_comm = {
507 .header = " Command",
508 .cmp = sort__comm_cmp,
509 .collapse = sort__comm_collapse,
510 .print = sort__comm_print,
516 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
518 struct dso *dso_l = left->dso;
519 struct dso *dso_r = right->dso;
521 if (!dso_l || !dso_r) {
522 if (!dso_l && !dso_r)
530 return strcmp(dso_l->name, dso_r->name);
534 sort__dso_print(FILE *fp, struct hist_entry *self)
537 return fprintf(fp, "%-25s", self->dso->name);
539 return fprintf(fp, "%016llx ", (__u64)self->ip);
542 static struct sort_entry sort_dso = {
543 .header = "Shared Object ",
544 .cmp = sort__dso_cmp,
545 .print = sort__dso_print,
551 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
555 if (left->sym == right->sym)
558 ip_l = left->sym ? left->sym->start : left->ip;
559 ip_r = right->sym ? right->sym->start : right->ip;
561 return (int64_t)(ip_r - ip_l);
565 sort__sym_print(FILE *fp, struct hist_entry *self)
570 ret += fprintf(fp, "%#018llx ", (__u64)self->ip);
573 ret += fprintf(fp, "[%c] %s",
574 self->dso == kernel_dso ? 'k' : '.', self->sym->name);
576 ret += fprintf(fp, "%#016llx", (__u64)self->ip);
582 static struct sort_entry sort_sym = {
584 .cmp = sort__sym_cmp,
585 .print = sort__sym_print,
588 static int sort__need_collapse = 0;
590 struct sort_dimension {
592 struct sort_entry *entry;
596 static struct sort_dimension sort_dimensions[] = {
597 { .name = "pid", .entry = &sort_thread, },
598 { .name = "comm", .entry = &sort_comm, },
599 { .name = "dso", .entry = &sort_dso, },
600 { .name = "symbol", .entry = &sort_sym, },
603 static LIST_HEAD(hist_entry__sort_list);
605 static int sort_dimension__add(char *tok)
609 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
610 struct sort_dimension *sd = &sort_dimensions[i];
615 if (strncasecmp(tok, sd->name, strlen(tok)))
618 if (sd->entry->collapse)
619 sort__need_collapse = 1;
621 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
631 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
633 struct sort_entry *se;
636 list_for_each_entry(se, &hist_entry__sort_list, list) {
637 cmp = se->cmp(left, right);
646 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
648 struct sort_entry *se;
651 list_for_each_entry(se, &hist_entry__sort_list, list) {
652 int64_t (*f)(struct hist_entry *, struct hist_entry *);
654 f = se->collapse ?: se->cmp;
656 cmp = f(left, right);
665 hist_entry__fprintf(FILE *fp, struct hist_entry *self, uint64_t total_samples)
667 struct sort_entry *se;
671 double percent = self->count * 100.0 / total_samples;
672 char *color = PERF_COLOR_NORMAL;
675 * We color high-overhead entries in red, low-overhead
676 * entries in green - and keep the middle ground normal:
679 color = PERF_COLOR_RED;
681 color = PERF_COLOR_GREEN;
683 ret = color_fprintf(fp, color, " %6.2f%%",
684 (self->count * 100.0) / total_samples);
686 ret = fprintf(fp, "%12d ", self->count);
688 list_for_each_entry(se, &hist_entry__sort_list, list) {
690 ret += se->print(fp, self);
693 ret += fprintf(fp, "\n");
699 * collect histogram counts
703 hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
704 struct symbol *sym, uint64_t ip, char level)
706 struct rb_node **p = &hist.rb_node;
707 struct rb_node *parent = NULL;
708 struct hist_entry *he;
709 struct hist_entry entry = {
722 he = rb_entry(parent, struct hist_entry, rb_node);
724 cmp = hist_entry__cmp(&entry, he);
737 he = malloc(sizeof(*he));
741 rb_link_node(&he->rb_node, parent, p);
742 rb_insert_color(&he->rb_node, &hist);
747 static void hist_entry__free(struct hist_entry *he)
753 * collapse the histogram
756 static struct rb_root collapse_hists;
758 static void collapse__insert_entry(struct hist_entry *he)
760 struct rb_node **p = &collapse_hists.rb_node;
761 struct rb_node *parent = NULL;
762 struct hist_entry *iter;
767 iter = rb_entry(parent, struct hist_entry, rb_node);
769 cmp = hist_entry__collapse(iter, he);
772 iter->count += he->count;
773 hist_entry__free(he);
783 rb_link_node(&he->rb_node, parent, p);
784 rb_insert_color(&he->rb_node, &collapse_hists);
787 static void collapse__resort(void)
789 struct rb_node *next;
790 struct hist_entry *n;
792 if (!sort__need_collapse)
795 next = rb_first(&hist);
797 n = rb_entry(next, struct hist_entry, rb_node);
798 next = rb_next(&n->rb_node);
800 rb_erase(&n->rb_node, &hist);
801 collapse__insert_entry(n);
806 * reverse the map, sort on count.
809 static struct rb_root output_hists;
811 static void output__insert_entry(struct hist_entry *he)
813 struct rb_node **p = &output_hists.rb_node;
814 struct rb_node *parent = NULL;
815 struct hist_entry *iter;
819 iter = rb_entry(parent, struct hist_entry, rb_node);
821 if (he->count > iter->count)
827 rb_link_node(&he->rb_node, parent, p);
828 rb_insert_color(&he->rb_node, &output_hists);
831 static void output__resort(void)
833 struct rb_node *next;
834 struct hist_entry *n;
835 struct rb_root *tree = &hist;
837 if (sort__need_collapse)
838 tree = &collapse_hists;
840 next = rb_first(tree);
843 n = rb_entry(next, struct hist_entry, rb_node);
844 next = rb_next(&n->rb_node);
846 rb_erase(&n->rb_node, tree);
847 output__insert_entry(n);
851 static size_t output__fprintf(FILE *fp, uint64_t total_samples)
853 struct hist_entry *pos;
854 struct sort_entry *se;
860 fprintf(fp, "# (%Ld samples)\n", (__u64)total_samples);
863 fprintf(fp, "# Overhead");
864 list_for_each_entry(se, &hist_entry__sort_list, list)
865 fprintf(fp, " %s", se->header);
868 fprintf(fp, "# ........");
869 list_for_each_entry(se, &hist_entry__sort_list, list) {
873 for (i = 0; i < strlen(se->header); i++)
880 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
881 pos = rb_entry(nd, struct hist_entry, rb_node);
882 ret += hist_entry__fprintf(fp, pos, total_samples);
885 if (!strcmp(sort_order, default_sort_order)) {
887 fprintf(fp, "# (For more details, try: perf report --sort comm,dso,symbol)\n");
895 static void register_idle_thread(void)
897 struct thread *thread = threads__findnew(0);
899 if (thread == NULL ||
900 thread__set_comm(thread, "[idle]")) {
901 fprintf(stderr, "problem inserting idle task.\n");
906 static unsigned long total = 0,
913 process_overflow_event(event_t *event, unsigned long offset, unsigned long head)
917 struct dso *dso = NULL;
918 struct thread *thread = threads__findnew(event->ip.pid);
919 uint64_t ip = event->ip.ip;
920 struct map *map = NULL;
922 dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
923 (void *)(offset + head),
924 (void *)(long)(event->header.size),
929 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
931 if (thread == NULL) {
932 fprintf(stderr, "problem processing %d event, skipping it.\n",
937 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
943 dprintf(" ...... dso: %s\n", dso->name);
945 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
950 map = thread__find_map(thread, ip);
952 ip = map->map_ip(map, ip);
956 * If this is outside of all known maps,
957 * and is a negative address, try to look it
958 * up in the kernel dso, as it might be a
959 * vsyscall (which executes in user-mode):
961 if ((long long)ip < 0)
964 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
969 dprintf(" ...... dso: [hypervisor]\n");
972 if (show & show_mask) {
973 struct symbol *sym = NULL;
976 sym = dso->find_symbol(dso, ip);
978 if (hist_entry__add(thread, map, dso, sym, ip, level)) {
980 "problem incrementing symbol count, skipping event\n");
990 process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
992 struct thread *thread = threads__findnew(event->mmap.pid);
993 struct map *map = map__new(&event->mmap);
995 dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
996 (void *)(offset + head),
997 (void *)(long)(event->header.size),
999 (void *)(long)event->mmap.start,
1000 (void *)(long)event->mmap.len,
1001 (void *)(long)event->mmap.pgoff,
1002 event->mmap.filename);
1004 if (thread == NULL || map == NULL) {
1005 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
1009 thread__insert_map(thread, map);
1016 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
1018 struct thread *thread = threads__findnew(event->comm.pid);
1020 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
1021 (void *)(offset + head),
1022 (void *)(long)(event->header.size),
1023 event->comm.comm, event->comm.pid);
1025 if (thread == NULL ||
1026 thread__set_comm(thread, event->comm.comm)) {
1027 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
1036 process_fork_event(event_t *event, unsigned long offset, unsigned long head)
1038 struct thread *thread = threads__findnew(event->fork.pid);
1039 struct thread *parent = threads__findnew(event->fork.ppid);
1041 dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
1042 (void *)(offset + head),
1043 (void *)(long)(event->header.size),
1044 event->fork.pid, event->fork.ppid);
1046 if (!thread || !parent || thread__fork(thread, parent)) {
1047 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
1056 process_event(event_t *event, unsigned long offset, unsigned long head)
1058 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW)
1059 return process_overflow_event(event, offset, head);
1061 switch (event->header.type) {
1062 case PERF_EVENT_MMAP:
1063 return process_mmap_event(event, offset, head);
1065 case PERF_EVENT_COMM:
1066 return process_comm_event(event, offset, head);
1068 case PERF_EVENT_FORK:
1069 return process_fork_event(event, offset, head);
1072 * We dont process them right now but they are fine:
1075 case PERF_EVENT_PERIOD:
1076 case PERF_EVENT_THROTTLE:
1077 case PERF_EVENT_UNTHROTTLE:
1087 static int __cmd_report(void)
1089 int ret, rc = EXIT_FAILURE;
1090 unsigned long offset = 0;
1091 unsigned long head = 0;
1097 register_idle_thread();
1099 input = open(input_name, O_RDONLY);
1101 perror("failed to open file");
1105 ret = fstat(input, &stat);
1107 perror("failed to stat file");
1111 if (!stat.st_size) {
1112 fprintf(stderr, "zero-sized file, nothing to do!\n");
1116 if (load_kernel() < 0) {
1117 perror("failed to load kernel symbols");
1118 return EXIT_FAILURE;
1122 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
1123 perror("failed to get the current directory");
1124 return EXIT_FAILURE;
1126 cwdlen = strlen(cwd);
1132 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1133 MAP_SHARED, input, offset);
1134 if (buf == MAP_FAILED) {
1135 perror("failed to mmap file");
1140 event = (event_t *)(buf + head);
1142 size = event->header.size;
1146 if (head + event->header.size >= page_size * mmap_window) {
1147 unsigned long shift = page_size * (head / page_size);
1150 ret = munmap(buf, page_size * mmap_window);
1158 size = event->header.size;
1160 if (!size || process_event(event, offset, head) < 0) {
1162 dprintf("%p [%p]: skipping unknown header type: %d\n",
1163 (void *)(offset + head),
1164 (void *)(long)(event->header.size),
1165 event->header.type);
1170 * assume we lost track of the stream, check alignment, and
1171 * increment a single u64 in the hope to catch on again 'soon'.
1174 if (unlikely(head & 7))
1182 if (offset + head < stat.st_size)
1188 dprintf(" IP events: %10ld\n", total);
1189 dprintf(" mmap events: %10ld\n", total_mmap);
1190 dprintf(" comm events: %10ld\n", total_comm);
1191 dprintf(" fork events: %10ld\n", total_fork);
1192 dprintf(" unknown events: %10ld\n", total_unknown);
1198 threads__fprintf(stdout);
1201 dsos__fprintf(stdout);
1205 output__fprintf(stdout, total);
1210 static const char * const report_usage[] = {
1211 "perf report [<options>] <command>",
1215 static const struct option options[] = {
1216 OPT_STRING('i', "input", &input_name, "file",
1218 OPT_BOOLEAN('v', "verbose", &verbose,
1219 "be more verbose (show symbol address, etc)"),
1220 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1221 "dump raw trace in ASCII"),
1222 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
1223 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1224 "sort by key(s): pid, comm, dso, symbol. Default: pid,symbol"),
1225 OPT_BOOLEAN('P', "full-paths", &full_paths,
1226 "Don't shorten the pathnames taking into account the cwd"),
1230 static void setup_sorting(void)
1232 char *tmp, *tok, *str = strdup(sort_order);
1234 for (tok = strtok_r(str, ", ", &tmp);
1235 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1236 if (sort_dimension__add(tok) < 0) {
1237 error("Unknown --sort key: `%s'", tok);
1238 usage_with_options(report_usage, options);
1245 int cmd_report(int argc, const char **argv, const char *prefix)
1249 page_size = getpagesize();
1251 argc = parse_options(argc, argv, options, report_usage, 0);
1256 * Any (unrecognized) arguments left?
1259 usage_with_options(report_usage, options);
1263 return __cmd_report();