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;
46 const char *perf_event_names[] = {
47 [PERF_EVENT_MMAP] = " PERF_EVENT_MMAP",
48 [PERF_EVENT_MUNMAP] = " PERF_EVENT_MUNMAP",
49 [PERF_EVENT_COMM] = " PERF_EVENT_COMM",
53 struct perf_event_header header;
59 struct perf_event_header header;
64 char filename[PATH_MAX];
68 struct perf_event_header header;
73 typedef union event_union {
74 struct perf_event_header header;
76 struct mmap_event mmap;
77 struct comm_event comm;
80 static LIST_HEAD(dsos);
81 static struct dso *kernel_dso;
83 static void dsos__add(struct dso *dso)
85 list_add_tail(&dso->node, &dsos);
88 static struct dso *dsos__find(const char *name)
92 list_for_each_entry(pos, &dsos, node)
93 if (strcmp(pos->name, name) == 0)
98 static struct dso *dsos__findnew(const char *name)
100 struct dso *dso = dsos__find(name);
106 dso = dso__new(name, 0);
110 nr = dso__load(dso, NULL, verbose);
113 fprintf(stderr, "Failed to open: %s\n", name);
116 if (!nr && verbose) {
118 "No symbols found in: %s, maybe install a debug package?\n",
131 static void dsos__fprintf(FILE *fp)
135 list_for_each_entry(pos, &dsos, node)
136 dso__fprintf(pos, fp);
139 static int load_kernel(void)
143 kernel_dso = dso__new("[kernel]", 0);
147 err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose);
149 dso__delete(kernel_dso);
152 dsos__add(kernel_dso);
157 static char __cwd[PATH_MAX];
158 static char *cwd = __cwd;
161 static int strcommon(const char *pathname)
165 while (pathname[n] == cwd[n] && n < cwdlen)
172 struct list_head node;
179 static struct map *map__new(struct mmap_event *event)
181 struct map *self = malloc(sizeof(*self));
184 const char *filename = event->filename;
185 char newfilename[PATH_MAX];
188 int n = strcommon(filename);
191 snprintf(newfilename, sizeof(newfilename),
192 ".%s", filename + n);
193 filename = newfilename;
197 self->start = event->start;
198 self->end = event->start + event->len;
199 self->pgoff = event->pgoff;
201 self->dso = dsos__findnew(filename);
202 if (self->dso == NULL)
214 struct rb_node rb_node;
215 struct list_head maps;
220 static struct thread *thread__new(pid_t pid)
222 struct thread *self = malloc(sizeof(*self));
226 self->comm = malloc(32);
228 snprintf(self->comm, 32, ":%d", self->pid);
229 INIT_LIST_HEAD(&self->maps);
235 static int thread__set_comm(struct thread *self, const char *comm)
239 self->comm = strdup(comm);
240 return self->comm ? 0 : -ENOMEM;
243 static struct rb_root threads;
244 static struct thread *last_match;
246 static struct thread *threads__findnew(pid_t pid)
248 struct rb_node **p = &threads.rb_node;
249 struct rb_node *parent = NULL;
253 * Font-end cache - PID lookups come in blocks,
254 * so most of the time we dont have to look up
257 if (last_match && last_match->pid == pid)
262 th = rb_entry(parent, struct thread, rb_node);
264 if (th->pid == pid) {
275 th = thread__new(pid);
277 rb_link_node(&th->rb_node, parent, p);
278 rb_insert_color(&th->rb_node, &threads);
285 static void thread__insert_map(struct thread *self, struct map *map)
287 list_add_tail(&map->node, &self->maps);
290 static struct map *thread__find_map(struct thread *self, uint64_t ip)
297 list_for_each_entry(pos, &self->maps, node)
298 if (ip >= pos->start && ip <= pos->end)
305 * histogram, sorted on item, collects counts
308 static struct rb_root hist;
311 struct rb_node rb_node;
313 struct thread *thread;
324 * configurable sorting bits
328 struct list_head list;
332 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
333 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
334 size_t (*print)(FILE *fp, struct hist_entry *);
340 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
342 return right->thread->pid - left->thread->pid;
346 sort__thread_print(FILE *fp, struct hist_entry *self)
348 return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
351 static struct sort_entry sort_thread = {
352 .header = " Command: Pid",
353 .cmp = sort__thread_cmp,
354 .print = sort__thread_print,
360 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
362 return right->thread->pid - left->thread->pid;
366 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
368 char *comm_l = left->thread->comm;
369 char *comm_r = right->thread->comm;
371 if (!comm_l || !comm_r) {
372 if (!comm_l && !comm_r)
380 return strcmp(comm_l, comm_r);
384 sort__comm_print(FILE *fp, struct hist_entry *self)
386 return fprintf(fp, "%16s", self->thread->comm);
389 static struct sort_entry sort_comm = {
390 .header = " Command",
391 .cmp = sort__comm_cmp,
392 .collapse = sort__comm_collapse,
393 .print = sort__comm_print,
399 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
401 struct dso *dso_l = left->dso;
402 struct dso *dso_r = right->dso;
404 if (!dso_l || !dso_r) {
405 if (!dso_l && !dso_r)
413 return strcmp(dso_l->name, dso_r->name);
417 sort__dso_print(FILE *fp, struct hist_entry *self)
420 return fprintf(fp, "%-25s", self->dso->name);
422 return fprintf(fp, "%016llx ", (__u64)self->ip);
425 static struct sort_entry sort_dso = {
426 .header = "Shared Object ",
427 .cmp = sort__dso_cmp,
428 .print = sort__dso_print,
434 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
438 if (left->sym == right->sym)
441 ip_l = left->sym ? left->sym->start : left->ip;
442 ip_r = right->sym ? right->sym->start : right->ip;
444 return (int64_t)(ip_r - ip_l);
448 sort__sym_print(FILE *fp, struct hist_entry *self)
453 ret += fprintf(fp, "%#018llx ", (__u64)self->ip);
456 ret += fprintf(fp, "%s", self->sym->name);
458 ret += fprintf(fp, "%#016llx", (__u64)self->ip);
463 static struct sort_entry sort_sym = {
465 .cmp = sort__sym_cmp,
466 .print = sort__sym_print,
469 static int sort__need_collapse = 0;
471 struct sort_dimension {
473 struct sort_entry *entry;
477 static struct sort_dimension sort_dimensions[] = {
478 { .name = "pid", .entry = &sort_thread, },
479 { .name = "comm", .entry = &sort_comm, },
480 { .name = "dso", .entry = &sort_dso, },
481 { .name = "symbol", .entry = &sort_sym, },
484 static LIST_HEAD(hist_entry__sort_list);
486 static int sort_dimension__add(char *tok)
490 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
491 struct sort_dimension *sd = &sort_dimensions[i];
496 if (strncasecmp(tok, sd->name, strlen(tok)))
499 if (sd->entry->collapse)
500 sort__need_collapse = 1;
502 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
512 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
514 struct sort_entry *se;
517 list_for_each_entry(se, &hist_entry__sort_list, list) {
518 cmp = se->cmp(left, right);
527 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
529 struct sort_entry *se;
532 list_for_each_entry(se, &hist_entry__sort_list, list) {
533 int64_t (*f)(struct hist_entry *, struct hist_entry *);
535 f = se->collapse ?: se->cmp;
537 cmp = f(left, right);
546 hist_entry__fprintf(FILE *fp, struct hist_entry *self, uint64_t total_samples)
548 struct sort_entry *se;
552 double percent = self->count * 100.0 / total_samples;
553 char *color = PERF_COLOR_NORMAL;
556 * We color high-overhead entries in red, low-overhead
557 * entries in green - and keep the middle ground normal:
560 color = PERF_COLOR_RED;
562 color = PERF_COLOR_GREEN;
564 ret = color_fprintf(fp, color, " %6.2f%%",
565 (self->count * 100.0) / total_samples);
567 ret = fprintf(fp, "%12d ", self->count);
569 list_for_each_entry(se, &hist_entry__sort_list, list) {
571 ret += se->print(fp, self);
574 ret += fprintf(fp, "\n");
580 * collect histogram counts
584 hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
585 struct symbol *sym, uint64_t ip, char level)
587 struct rb_node **p = &hist.rb_node;
588 struct rb_node *parent = NULL;
589 struct hist_entry *he;
590 struct hist_entry entry = {
603 he = rb_entry(parent, struct hist_entry, rb_node);
605 cmp = hist_entry__cmp(&entry, he);
618 he = malloc(sizeof(*he));
622 rb_link_node(&he->rb_node, parent, p);
623 rb_insert_color(&he->rb_node, &hist);
628 static void hist_entry__free(struct hist_entry *he)
634 * collapse the histogram
637 static struct rb_root collapse_hists;
639 static void collapse__insert_entry(struct hist_entry *he)
641 struct rb_node **p = &collapse_hists.rb_node;
642 struct rb_node *parent = NULL;
643 struct hist_entry *iter;
648 iter = rb_entry(parent, struct hist_entry, rb_node);
650 cmp = hist_entry__collapse(iter, he);
653 iter->count += he->count;
654 hist_entry__free(he);
664 rb_link_node(&he->rb_node, parent, p);
665 rb_insert_color(&he->rb_node, &collapse_hists);
668 static void collapse__resort(void)
670 struct rb_node *next;
671 struct hist_entry *n;
673 if (!sort__need_collapse)
676 next = rb_first(&hist);
678 n = rb_entry(next, struct hist_entry, rb_node);
679 next = rb_next(&n->rb_node);
681 rb_erase(&n->rb_node, &hist);
682 collapse__insert_entry(n);
687 * reverse the map, sort on count.
690 static struct rb_root output_hists;
692 static void output__insert_entry(struct hist_entry *he)
694 struct rb_node **p = &output_hists.rb_node;
695 struct rb_node *parent = NULL;
696 struct hist_entry *iter;
700 iter = rb_entry(parent, struct hist_entry, rb_node);
702 if (he->count > iter->count)
708 rb_link_node(&he->rb_node, parent, p);
709 rb_insert_color(&he->rb_node, &output_hists);
712 static void output__resort(void)
714 struct rb_node *next;
715 struct hist_entry *n;
716 struct rb_root *tree = &hist;
718 if (sort__need_collapse)
719 tree = &collapse_hists;
721 next = rb_first(tree);
724 n = rb_entry(next, struct hist_entry, rb_node);
725 next = rb_next(&n->rb_node);
727 rb_erase(&n->rb_node, tree);
728 output__insert_entry(n);
732 static size_t output__fprintf(FILE *fp, uint64_t total_samples)
734 struct hist_entry *pos;
735 struct sort_entry *se;
741 fprintf(fp, "# (%Ld profiler events)\n", (__u64)total_samples);
744 fprintf(fp, "# Overhead");
745 list_for_each_entry(se, &hist_entry__sort_list, list)
746 fprintf(fp, " %s", se->header);
749 fprintf(fp, "# ........");
750 list_for_each_entry(se, &hist_entry__sort_list, list) {
754 for (i = 0; i < strlen(se->header); i++)
761 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
762 pos = rb_entry(nd, struct hist_entry, rb_node);
763 ret += hist_entry__fprintf(fp, pos, total_samples);
766 if (!strcmp(sort_order, default_sort_order)) {
768 fprintf(fp, "# (For more details, try: perf report --sort comm,dso,symbol)\n");
776 static void register_idle_thread(void)
778 struct thread *thread = threads__findnew(0);
780 if (thread == NULL ||
781 thread__set_comm(thread, "[idle]")) {
782 fprintf(stderr, "problem inserting idle task.\n");
787 static unsigned long total = 0, total_mmap = 0, total_comm = 0, total_unknown = 0;
790 process_overflow_event(event_t *event, unsigned long offset, unsigned long head)
794 struct dso *dso = NULL;
795 struct thread *thread = threads__findnew(event->ip.pid);
796 uint64_t ip = event->ip.ip;
797 struct map *map = NULL;
799 dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
800 (void *)(offset + head),
801 (void *)(long)(event->header.size),
806 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
808 if (thread == NULL) {
809 fprintf(stderr, "problem processing %d event, skipping it.\n",
814 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
820 dprintf(" ...... dso: %s\n", dso->name);
822 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
827 map = thread__find_map(thread, ip);
830 ip -= map->start + map->pgoff;
833 * If this is outside of all known maps,
834 * and is a negative address, try to look it
835 * up in the kernel dso, as it might be a
836 * vsyscall (which executes in user-mode):
838 if ((long long)ip < 0)
841 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
846 dprintf(" ...... dso: [hypervisor]\n");
849 if (show & show_mask) {
850 struct symbol *sym = dso__find_symbol(dso, ip);
852 if (hist_entry__add(thread, map, dso, sym, ip, level)) {
854 "problem incrementing symbol count, skipping event\n");
864 process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
866 struct thread *thread = threads__findnew(event->mmap.pid);
867 struct map *map = map__new(&event->mmap);
869 dprintf("%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n",
870 (void *)(offset + head),
871 (void *)(long)(event->header.size),
872 (void *)(long)event->mmap.start,
873 (void *)(long)event->mmap.len,
874 (void *)(long)event->mmap.pgoff,
875 event->mmap.filename);
877 if (thread == NULL || map == NULL) {
878 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
882 thread__insert_map(thread, map);
889 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
891 struct thread *thread = threads__findnew(event->comm.pid);
893 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
894 (void *)(offset + head),
895 (void *)(long)(event->header.size),
896 event->comm.comm, event->comm.pid);
898 if (thread == NULL ||
899 thread__set_comm(thread, event->comm.comm)) {
900 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
909 process_event(event_t *event, unsigned long offset, unsigned long head)
911 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW)
912 return process_overflow_event(event, offset, head);
914 switch (event->header.type) {
915 case PERF_EVENT_MMAP:
916 return process_mmap_event(event, offset, head);
918 case PERF_EVENT_COMM:
919 return process_comm_event(event, offset, head);
922 * We dont process them right now but they are fine:
924 case PERF_EVENT_MUNMAP:
925 case PERF_EVENT_PERIOD:
926 case PERF_EVENT_THROTTLE:
927 case PERF_EVENT_UNTHROTTLE:
937 static int __cmd_report(void)
939 int ret, rc = EXIT_FAILURE;
940 unsigned long offset = 0;
941 unsigned long head = 0;
947 register_idle_thread();
949 input = open(input_name, O_RDONLY);
951 perror("failed to open file");
955 ret = fstat(input, &stat);
957 perror("failed to stat file");
962 fprintf(stderr, "zero-sized file, nothing to do!\n");
966 if (load_kernel() < 0) {
967 perror("failed to load kernel symbols");
972 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
973 perror("failed to get the current directory");
976 cwdlen = strlen(cwd);
982 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
983 MAP_SHARED, input, offset);
984 if (buf == MAP_FAILED) {
985 perror("failed to mmap file");
990 event = (event_t *)(buf + head);
992 size = event->header.size;
996 if (head + event->header.size >= page_size * mmap_window) {
997 unsigned long shift = page_size * (head / page_size);
1000 ret = munmap(buf, page_size * mmap_window);
1008 size = event->header.size;
1010 if (!size || process_event(event, offset, head) < 0) {
1012 dprintf("%p [%p]: skipping unknown header type: %d\n",
1013 (void *)(offset + head),
1014 (void *)(long)(event->header.size),
1015 event->header.type);
1020 * assume we lost track of the stream, check alignment, and
1021 * increment a single u64 in the hope to catch on again 'soon'.
1024 if (unlikely(head & 7))
1032 if (offset + head < stat.st_size)
1038 dprintf(" IP events: %10ld\n", total);
1039 dprintf(" mmap events: %10ld\n", total_mmap);
1040 dprintf(" comm events: %10ld\n", total_comm);
1041 dprintf(" unknown events: %10ld\n", total_unknown);
1047 dsos__fprintf(stdout);
1051 output__fprintf(stdout, total);
1056 static const char * const report_usage[] = {
1057 "perf report [<options>] <command>",
1061 static const struct option options[] = {
1062 OPT_STRING('i', "input", &input_name, "file",
1064 OPT_BOOLEAN('v', "verbose", &verbose,
1065 "be more verbose (show symbol address, etc)"),
1066 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1067 "dump raw trace in ASCII"),
1068 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
1069 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1070 "sort by key(s): pid, comm, dso, symbol. Default: pid,symbol"),
1071 OPT_BOOLEAN('P', "full-paths", &full_paths,
1072 "Don't shorten the pathnames taking into account the cwd"),
1076 static void setup_sorting(void)
1078 char *tmp, *tok, *str = strdup(sort_order);
1080 for (tok = strtok_r(str, ", ", &tmp);
1081 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1082 if (sort_dimension__add(tok) < 0) {
1083 error("Unknown --sort key: `%s'", tok);
1084 usage_with_options(report_usage, options);
1091 int cmd_report(int argc, const char **argv, const char *prefix)
1095 page_size = getpagesize();
1097 argc = parse_options(argc, argv, options, report_usage, 0);
1102 * Any (unrecognized) arguments left?
1105 usage_with_options(report_usage, options);
1109 return __cmd_report();