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/list.h"
13 #include "util/cache.h"
14 #include "util/rbtree.h"
15 #include "util/symbol.h"
16 #include "util/string.h"
20 #include "util/parse-options.h"
21 #include "util/parse-events.h"
27 static char const *input_name = "perf.data";
28 static char *vmlinux = NULL;
29 static char *sort_order = "comm,dso";
31 static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
33 static int dump_trace = 0;
34 #define dprintf(x...) do { if (dump_trace) printf(x); } while (0)
37 static int full_paths;
39 static unsigned long page_size;
40 static unsigned long mmap_window = 32;
42 const char *perf_event_names[] = {
43 [PERF_EVENT_MMAP] = " PERF_EVENT_MMAP",
44 [PERF_EVENT_MUNMAP] = " PERF_EVENT_MUNMAP",
45 [PERF_EVENT_COMM] = " PERF_EVENT_COMM",
49 struct perf_event_header header;
54 struct perf_event_header header;
59 char filename[PATH_MAX];
62 struct perf_event_header header;
67 typedef union event_union {
68 struct perf_event_header header;
70 struct mmap_event mmap;
71 struct comm_event comm;
74 static LIST_HEAD(dsos);
75 static struct dso *kernel_dso;
77 static void dsos__add(struct dso *dso)
79 list_add_tail(&dso->node, &dsos);
82 static struct dso *dsos__find(const char *name)
86 list_for_each_entry(pos, &dsos, node)
87 if (strcmp(pos->name, name) == 0)
92 static struct dso *dsos__findnew(const char *name)
94 struct dso *dso = dsos__find(name);
100 dso = dso__new(name, 0);
104 nr = dso__load(dso, NULL);
106 fprintf(stderr, "Failed to open: %s\n", name);
109 if (!nr && verbose) {
111 "No symbols found in: %s, maybe install a debug package?\n",
124 static void dsos__fprintf(FILE *fp)
128 list_for_each_entry(pos, &dsos, node)
129 dso__fprintf(pos, fp);
132 static int load_kernel(void)
136 kernel_dso = dso__new("[kernel]", 0);
140 err = dso__load_kernel(kernel_dso, vmlinux, NULL);
142 dso__delete(kernel_dso);
145 dsos__add(kernel_dso);
150 static int strcommon(const char *pathname, const char *cwd, int cwdlen)
154 while (pathname[n] == cwd[n] && n < cwdlen)
161 struct list_head node;
168 static struct map *map__new(struct mmap_event *event, char *cwd, int cwdlen)
170 struct map *self = malloc(sizeof(*self));
173 const char *filename = event->filename;
174 char newfilename[PATH_MAX];
177 int n = strcommon(filename, cwd, cwdlen);
179 snprintf(newfilename, sizeof(newfilename),
180 ".%s", filename + n);
181 filename = newfilename;
185 self->start = event->start;
186 self->end = event->start + event->len;
187 self->pgoff = event->pgoff;
189 self->dso = dsos__findnew(filename);
190 if (self->dso == NULL)
202 struct rb_node rb_node;
203 struct list_head maps;
208 static struct thread *thread__new(pid_t pid)
210 struct thread *self = malloc(sizeof(*self));
214 self->comm = malloc(32);
216 snprintf(self->comm, 32, ":%d", self->pid);
217 INIT_LIST_HEAD(&self->maps);
223 static int thread__set_comm(struct thread *self, const char *comm)
227 self->comm = strdup(comm);
228 return self->comm ? 0 : -ENOMEM;
231 static struct rb_root threads;
232 static struct thread *last_match;
234 static struct thread *threads__findnew(pid_t pid)
236 struct rb_node **p = &threads.rb_node;
237 struct rb_node *parent = NULL;
241 * Font-end cache - PID lookups come in blocks,
242 * so most of the time we dont have to look up
245 if (last_match && last_match->pid == pid)
250 th = rb_entry(parent, struct thread, rb_node);
252 if (th->pid == pid) {
263 th = thread__new(pid);
265 rb_link_node(&th->rb_node, parent, p);
266 rb_insert_color(&th->rb_node, &threads);
273 static void thread__insert_map(struct thread *self, struct map *map)
275 list_add_tail(&map->node, &self->maps);
278 static struct map *thread__find_map(struct thread *self, uint64_t ip)
285 list_for_each_entry(pos, &self->maps, node)
286 if (ip >= pos->start && ip <= pos->end)
293 * histogram, sorted on item, collects counts
296 static struct rb_root hist;
299 struct rb_node rb_node;
301 struct thread *thread;
312 * configurable sorting bits
316 struct list_head list;
320 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
321 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
322 size_t (*print)(FILE *fp, struct hist_entry *);
328 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
330 return right->thread->pid - left->thread->pid;
334 sort__thread_print(FILE *fp, struct hist_entry *self)
336 return fprintf(fp, " %16s:%5d", self->thread->comm ?: "", self->thread->pid);
339 static struct sort_entry sort_thread = {
340 .header = " Command: Pid ",
341 .cmp = sort__thread_cmp,
342 .print = sort__thread_print,
348 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
350 return right->thread->pid - left->thread->pid;
354 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
356 char *comm_l = left->thread->comm;
357 char *comm_r = right->thread->comm;
359 if (!comm_l || !comm_r) {
360 if (!comm_l && !comm_r)
368 return strcmp(comm_l, comm_r);
372 sort__comm_print(FILE *fp, struct hist_entry *self)
374 return fprintf(fp, " %16s", self->thread->comm);
377 static struct sort_entry sort_comm = {
378 .header = " Command",
379 .cmp = sort__comm_cmp,
380 .collapse = sort__comm_collapse,
381 .print = sort__comm_print,
387 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
389 struct dso *dso_l = left->dso;
390 struct dso *dso_r = right->dso;
392 if (!dso_l || !dso_r) {
393 if (!dso_l && !dso_r)
401 return strcmp(dso_l->name, dso_r->name);
405 sort__dso_print(FILE *fp, struct hist_entry *self)
408 return fprintf(fp, " %-25s", self->dso->name);
410 return fprintf(fp, " %016llx", (__u64)self->ip);
413 static struct sort_entry sort_dso = {
414 .header = " Shared Object ",
415 .cmp = sort__dso_cmp,
416 .print = sort__dso_print,
422 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
426 if (left->sym == right->sym)
429 ip_l = left->sym ? left->sym->start : left->ip;
430 ip_r = right->sym ? right->sym->start : right->ip;
432 return (int64_t)(ip_r - ip_l);
436 sort__sym_print(FILE *fp, struct hist_entry *self)
441 ret += fprintf(fp, " %#018llx", (__u64)self->ip);
444 ret += fprintf(fp, " %s: ", self->dso->name);
446 ret += fprintf(fp, " %#016llx: ", (__u64)self->ip);
449 ret += fprintf(fp, "%s", self->sym->name);
451 ret += fprintf(fp, "%#016llx", (__u64)self->ip);
456 static struct sort_entry sort_sym = {
457 .header = " Shared Object: Symbol",
458 .cmp = sort__sym_cmp,
459 .print = sort__sym_print,
462 static int sort__need_collapse = 0;
464 struct sort_dimension {
466 struct sort_entry *entry;
470 static struct sort_dimension sort_dimensions[] = {
471 { .name = "pid", .entry = &sort_thread, },
472 { .name = "comm", .entry = &sort_comm, },
473 { .name = "dso", .entry = &sort_dso, },
474 { .name = "symbol", .entry = &sort_sym, },
477 static LIST_HEAD(hist_entry__sort_list);
479 static int sort_dimension__add(char *tok)
483 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
484 struct sort_dimension *sd = &sort_dimensions[i];
489 if (strncasecmp(tok, sd->name, strlen(tok)))
492 if (sd->entry->collapse)
493 sort__need_collapse = 1;
495 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
505 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
507 struct sort_entry *se;
510 list_for_each_entry(se, &hist_entry__sort_list, list) {
511 cmp = se->cmp(left, right);
520 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
522 struct sort_entry *se;
525 list_for_each_entry(se, &hist_entry__sort_list, list) {
526 int64_t (*f)(struct hist_entry *, struct hist_entry *);
528 f = se->collapse ?: se->cmp;
530 cmp = f(left, right);
539 hist_entry__fprintf(FILE *fp, struct hist_entry *self, uint64_t total_samples)
541 struct sort_entry *se;
545 ret = fprintf(fp, " %6.2f%%",
546 (self->count * 100.0) / total_samples);
548 ret = fprintf(fp, "%12d ", self->count);
550 list_for_each_entry(se, &hist_entry__sort_list, list)
551 ret += se->print(fp, self);
553 ret += fprintf(fp, "\n");
559 * collect histogram counts
563 hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
564 struct symbol *sym, uint64_t ip, char level)
566 struct rb_node **p = &hist.rb_node;
567 struct rb_node *parent = NULL;
568 struct hist_entry *he;
569 struct hist_entry entry = {
582 he = rb_entry(parent, struct hist_entry, rb_node);
584 cmp = hist_entry__cmp(&entry, he);
597 he = malloc(sizeof(*he));
601 rb_link_node(&he->rb_node, parent, p);
602 rb_insert_color(&he->rb_node, &hist);
607 static void hist_entry__free(struct hist_entry *he)
613 * collapse the histogram
616 static struct rb_root collapse_hists;
618 static void collapse__insert_entry(struct hist_entry *he)
620 struct rb_node **p = &collapse_hists.rb_node;
621 struct rb_node *parent = NULL;
622 struct hist_entry *iter;
627 iter = rb_entry(parent, struct hist_entry, rb_node);
629 cmp = hist_entry__collapse(iter, he);
632 iter->count += he->count;
633 hist_entry__free(he);
643 rb_link_node(&he->rb_node, parent, p);
644 rb_insert_color(&he->rb_node, &collapse_hists);
647 static void collapse__resort(void)
649 struct rb_node *next;
650 struct hist_entry *n;
652 if (!sort__need_collapse)
655 next = rb_first(&hist);
657 n = rb_entry(next, struct hist_entry, rb_node);
658 next = rb_next(&n->rb_node);
660 rb_erase(&n->rb_node, &hist);
661 collapse__insert_entry(n);
666 * reverse the map, sort on count.
669 static struct rb_root output_hists;
671 static void output__insert_entry(struct hist_entry *he)
673 struct rb_node **p = &output_hists.rb_node;
674 struct rb_node *parent = NULL;
675 struct hist_entry *iter;
679 iter = rb_entry(parent, struct hist_entry, rb_node);
681 if (he->count > iter->count)
687 rb_link_node(&he->rb_node, parent, p);
688 rb_insert_color(&he->rb_node, &output_hists);
691 static void output__resort(void)
693 struct rb_node *next;
694 struct hist_entry *n;
696 if (sort__need_collapse)
697 next = rb_first(&collapse_hists);
699 next = rb_first(&hist);
702 n = rb_entry(next, struct hist_entry, rb_node);
703 next = rb_next(&n->rb_node);
705 rb_erase(&n->rb_node, &hist);
706 output__insert_entry(n);
710 static size_t output__fprintf(FILE *fp, uint64_t total_samples)
712 struct hist_entry *pos;
713 struct sort_entry *se;
719 fprintf(fp, "# Overhead");
720 list_for_each_entry(se, &hist_entry__sort_list, list)
721 fprintf(fp, " %s", se->header);
724 fprintf(fp, "# ........");
725 list_for_each_entry(se, &hist_entry__sort_list, list) {
729 for (i = 0; i < strlen(se->header)-1; i++)
736 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
737 pos = rb_entry(nd, struct hist_entry, rb_node);
738 ret += hist_entry__fprintf(fp, pos, total_samples);
744 static void register_idle_thread(void)
746 struct thread *thread = threads__findnew(0);
748 if (thread == NULL ||
749 thread__set_comm(thread, "[idle]")) {
750 fprintf(stderr, "problem inserting idle task.\n");
756 static int __cmd_report(void)
758 unsigned long offset = 0;
759 unsigned long head = 0;
763 int ret, rc = EXIT_FAILURE;
765 unsigned long total = 0, total_mmap = 0, total_comm = 0, total_unknown = 0;
766 char cwd[PATH_MAX], *cwdp = cwd;
769 register_idle_thread();
771 input = open(input_name, O_RDONLY);
773 perror("failed to open file");
777 ret = fstat(input, &stat);
779 perror("failed to stat file");
784 fprintf(stderr, "zero-sized file, nothing to do!\n");
788 if (load_kernel() < 0) {
789 perror("failed to load kernel symbols");
794 if (getcwd(cwd, sizeof(cwd)) == NULL) {
795 perror("failed to get the current directory");
798 cwdlen = strlen(cwd);
804 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
805 MAP_SHARED, input, offset);
806 if (buf == MAP_FAILED) {
807 perror("failed to mmap file");
812 event = (event_t *)(buf + head);
814 size = event->header.size;
818 if (head + event->header.size >= page_size * mmap_window) {
819 unsigned long shift = page_size * (head / page_size);
822 ret = munmap(buf, page_size * mmap_window);
830 size = event->header.size;
834 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) {
837 struct dso *dso = NULL;
838 struct thread *thread = threads__findnew(event->ip.pid);
839 uint64_t ip = event->ip.ip;
840 struct map *map = NULL;
842 dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
843 (void *)(offset + head),
844 (void *)(long)(event->header.size),
849 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
851 if (thread == NULL) {
852 fprintf(stderr, "problem processing %d event, skipping it.\n",
857 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
863 dprintf(" ...... dso: %s\n", dso->name);
865 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
870 map = thread__find_map(thread, ip);
873 ip -= map->start + map->pgoff;
876 * If this is outside of all known maps,
877 * and is a negative address, try to look it
878 * up in the kernel dso, as it might be a
879 * vsyscall (which executes in user-mode):
881 if ((long long)ip < 0)
884 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
889 dprintf(" ...... dso: [hypervisor]\n");
892 if (show & show_mask) {
893 struct symbol *sym = dso__find_symbol(dso, ip);
895 if (hist_entry__add(thread, map, dso, sym, ip, level)) {
897 "problem incrementing symbol count, skipping event\n");
902 } else switch (event->header.type) {
903 case PERF_EVENT_MMAP: {
904 struct thread *thread = threads__findnew(event->mmap.pid);
905 struct map *map = map__new(&event->mmap, cwdp, cwdlen);
907 dprintf("%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n",
908 (void *)(offset + head),
909 (void *)(long)(event->header.size),
910 (void *)(long)event->mmap.start,
911 (void *)(long)event->mmap.len,
912 (void *)(long)event->mmap.pgoff,
913 event->mmap.filename);
915 if (thread == NULL || map == NULL) {
917 fprintf(stderr, "problem processing PERF_EVENT_MMAP, skipping event.\n");
920 thread__insert_map(thread, map);
924 case PERF_EVENT_COMM: {
925 struct thread *thread = threads__findnew(event->comm.pid);
927 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
928 (void *)(offset + head),
929 (void *)(long)(event->header.size),
930 event->comm.comm, event->comm.pid);
932 if (thread == NULL ||
933 thread__set_comm(thread, event->comm.comm)) {
934 fprintf(stderr, "problem processing PERF_EVENT_COMM, skipping event.\n");
942 dprintf("%p [%p]: skipping unknown header type: %d\n",
943 (void *)(offset + head),
944 (void *)(long)(event->header.size),
950 * assume we lost track of the stream, check alignment, and
951 * increment a single u64 in the hope to catch on again 'soon'.
954 if (unlikely(head & 7))
963 if (offset + head < stat.st_size)
969 dprintf(" IP events: %10ld\n", total);
970 dprintf(" mmap events: %10ld\n", total_mmap);
971 dprintf(" comm events: %10ld\n", total_comm);
972 dprintf(" unknown events: %10ld\n", total_unknown);
978 dsos__fprintf(stdout);
982 output__fprintf(stdout, total);
987 static const char * const report_usage[] = {
988 "perf report [<options>] <command>",
992 static const struct option options[] = {
993 OPT_STRING('i', "input", &input_name, "file",
995 OPT_BOOLEAN('v', "verbose", &verbose,
996 "be more verbose (show symbol address, etc)"),
997 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
998 "dump raw trace in ASCII"),
999 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
1000 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1001 "sort by key(s): pid, comm, dso, symbol. Default: pid,symbol"),
1002 OPT_BOOLEAN('P', "full-paths", &full_paths,
1003 "Don't shorten the pathnames taking into account the cwd"),
1007 static void setup_sorting(void)
1009 char *tmp, *tok, *str = strdup(sort_order);
1011 for (tok = strtok_r(str, ", ", &tmp);
1012 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1013 if (sort_dimension__add(tok) < 0) {
1014 error("Unknown --sort key: `%s'", tok);
1015 usage_with_options(report_usage, options);
1022 int cmd_report(int argc, const char **argv, const char *prefix)
1026 page_size = getpagesize();
1028 parse_options(argc, argv, options, report_usage, 0);
1034 return __cmd_report();