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 struct map *map__new(struct mmap_event *event)
214 struct map *self = malloc(sizeof(*self));
217 const char *filename = event->filename;
218 char newfilename[PATH_MAX];
221 int n = strcommon(filename);
224 snprintf(newfilename, sizeof(newfilename),
225 ".%s", filename + n);
226 filename = newfilename;
230 self->start = event->start;
231 self->end = event->start + event->len;
232 self->pgoff = event->pgoff;
234 self->dso = dsos__findnew(filename);
235 if (self->dso == NULL)
238 if (self->dso == vdso)
239 self->map_ip = vdso__map_ip;
241 self->map_ip = map__map_ip;
249 static struct map *map__clone(struct map *self)
251 struct map *map = malloc(sizeof(*self));
256 memcpy(map, self, sizeof(*self));
261 static int map__overlap(struct map *l, struct map *r)
263 if (l->start > r->start) {
269 if (l->end > r->start)
275 static size_t map__fprintf(struct map *self, FILE *fp)
277 return fprintf(fp, " %"PRIx64"-%"PRIx64" %"PRIx64" %s\n",
278 self->start, self->end, self->pgoff, self->dso->name);
283 struct rb_node rb_node;
284 struct list_head maps;
289 static struct thread *thread__new(pid_t pid)
291 struct thread *self = malloc(sizeof(*self));
295 self->comm = malloc(32);
297 snprintf(self->comm, 32, ":%d", self->pid);
298 INIT_LIST_HEAD(&self->maps);
304 static int thread__set_comm(struct thread *self, const char *comm)
308 self->comm = strdup(comm);
309 return self->comm ? 0 : -ENOMEM;
312 static size_t thread__fprintf(struct thread *self, FILE *fp)
315 size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
317 list_for_each_entry(pos, &self->maps, node)
318 ret += map__fprintf(pos, fp);
324 static struct rb_root threads;
325 static struct thread *last_match;
327 static struct thread *threads__findnew(pid_t pid)
329 struct rb_node **p = &threads.rb_node;
330 struct rb_node *parent = NULL;
334 * Font-end cache - PID lookups come in blocks,
335 * so most of the time we dont have to look up
338 if (last_match && last_match->pid == pid)
343 th = rb_entry(parent, struct thread, rb_node);
345 if (th->pid == pid) {
356 th = thread__new(pid);
358 rb_link_node(&th->rb_node, parent, p);
359 rb_insert_color(&th->rb_node, &threads);
366 static void thread__insert_map(struct thread *self, struct map *map)
368 struct map *pos, *tmp;
370 list_for_each_entry_safe(pos, tmp, &self->maps, node) {
371 if (map__overlap(pos, map)) {
372 list_del_init(&pos->node);
378 list_add_tail(&map->node, &self->maps);
381 static int thread__fork(struct thread *self, struct thread *parent)
387 self->comm = strdup(parent->comm);
391 list_for_each_entry(map, &parent->maps, node) {
392 struct map *new = map__clone(map);
395 thread__insert_map(self, new);
401 static struct map *thread__find_map(struct thread *self, uint64_t ip)
408 list_for_each_entry(pos, &self->maps, node)
409 if (ip >= pos->start && ip <= pos->end)
415 static size_t threads__fprintf(FILE *fp)
420 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
421 struct thread *pos = rb_entry(nd, struct thread, rb_node);
423 ret += thread__fprintf(pos, fp);
430 * histogram, sorted on item, collects counts
433 static struct rb_root hist;
436 struct rb_node rb_node;
438 struct thread *thread;
449 * configurable sorting bits
453 struct list_head list;
457 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
458 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
459 size_t (*print)(FILE *fp, struct hist_entry *);
465 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
467 return right->thread->pid - left->thread->pid;
471 sort__thread_print(FILE *fp, struct hist_entry *self)
473 return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
476 static struct sort_entry sort_thread = {
477 .header = " Command: Pid",
478 .cmp = sort__thread_cmp,
479 .print = sort__thread_print,
485 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
487 return right->thread->pid - left->thread->pid;
491 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
493 char *comm_l = left->thread->comm;
494 char *comm_r = right->thread->comm;
496 if (!comm_l || !comm_r) {
497 if (!comm_l && !comm_r)
505 return strcmp(comm_l, comm_r);
509 sort__comm_print(FILE *fp, struct hist_entry *self)
511 return fprintf(fp, "%16s", self->thread->comm);
514 static struct sort_entry sort_comm = {
515 .header = " Command",
516 .cmp = sort__comm_cmp,
517 .collapse = sort__comm_collapse,
518 .print = sort__comm_print,
524 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
526 struct dso *dso_l = left->dso;
527 struct dso *dso_r = right->dso;
529 if (!dso_l || !dso_r) {
530 if (!dso_l && !dso_r)
538 return strcmp(dso_l->name, dso_r->name);
542 sort__dso_print(FILE *fp, struct hist_entry *self)
545 return fprintf(fp, "%-25s", self->dso->name);
547 return fprintf(fp, "%016llx ", (__u64)self->ip);
550 static struct sort_entry sort_dso = {
551 .header = "Shared Object ",
552 .cmp = sort__dso_cmp,
553 .print = sort__dso_print,
559 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
563 if (left->sym == right->sym)
566 ip_l = left->sym ? left->sym->start : left->ip;
567 ip_r = right->sym ? right->sym->start : right->ip;
569 return (int64_t)(ip_r - ip_l);
573 sort__sym_print(FILE *fp, struct hist_entry *self)
578 ret += fprintf(fp, "%#018llx ", (__u64)self->ip);
581 ret += fprintf(fp, "[%c] %s",
582 self->dso == kernel_dso ? 'k' : '.', self->sym->name);
584 ret += fprintf(fp, "%#016llx", (__u64)self->ip);
590 static struct sort_entry sort_sym = {
592 .cmp = sort__sym_cmp,
593 .print = sort__sym_print,
596 static int sort__need_collapse = 0;
598 struct sort_dimension {
600 struct sort_entry *entry;
604 static struct sort_dimension sort_dimensions[] = {
605 { .name = "pid", .entry = &sort_thread, },
606 { .name = "comm", .entry = &sort_comm, },
607 { .name = "dso", .entry = &sort_dso, },
608 { .name = "symbol", .entry = &sort_sym, },
611 static LIST_HEAD(hist_entry__sort_list);
613 static int sort_dimension__add(char *tok)
617 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
618 struct sort_dimension *sd = &sort_dimensions[i];
623 if (strncasecmp(tok, sd->name, strlen(tok)))
626 if (sd->entry->collapse)
627 sort__need_collapse = 1;
629 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
639 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
641 struct sort_entry *se;
644 list_for_each_entry(se, &hist_entry__sort_list, list) {
645 cmp = se->cmp(left, right);
654 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
656 struct sort_entry *se;
659 list_for_each_entry(se, &hist_entry__sort_list, list) {
660 int64_t (*f)(struct hist_entry *, struct hist_entry *);
662 f = se->collapse ?: se->cmp;
664 cmp = f(left, right);
673 hist_entry__fprintf(FILE *fp, struct hist_entry *self, uint64_t total_samples)
675 struct sort_entry *se;
679 double percent = self->count * 100.0 / total_samples;
680 char *color = PERF_COLOR_NORMAL;
683 * We color high-overhead entries in red, low-overhead
684 * entries in green - and keep the middle ground normal:
687 color = PERF_COLOR_RED;
689 color = PERF_COLOR_GREEN;
691 ret = color_fprintf(fp, color, " %6.2f%%",
692 (self->count * 100.0) / total_samples);
694 ret = fprintf(fp, "%12d ", self->count);
696 list_for_each_entry(se, &hist_entry__sort_list, list) {
698 ret += se->print(fp, self);
701 ret += fprintf(fp, "\n");
707 * collect histogram counts
711 hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
712 struct symbol *sym, uint64_t ip, char level)
714 struct rb_node **p = &hist.rb_node;
715 struct rb_node *parent = NULL;
716 struct hist_entry *he;
717 struct hist_entry entry = {
730 he = rb_entry(parent, struct hist_entry, rb_node);
732 cmp = hist_entry__cmp(&entry, he);
745 he = malloc(sizeof(*he));
749 rb_link_node(&he->rb_node, parent, p);
750 rb_insert_color(&he->rb_node, &hist);
755 static void hist_entry__free(struct hist_entry *he)
761 * collapse the histogram
764 static struct rb_root collapse_hists;
766 static void collapse__insert_entry(struct hist_entry *he)
768 struct rb_node **p = &collapse_hists.rb_node;
769 struct rb_node *parent = NULL;
770 struct hist_entry *iter;
775 iter = rb_entry(parent, struct hist_entry, rb_node);
777 cmp = hist_entry__collapse(iter, he);
780 iter->count += he->count;
781 hist_entry__free(he);
791 rb_link_node(&he->rb_node, parent, p);
792 rb_insert_color(&he->rb_node, &collapse_hists);
795 static void collapse__resort(void)
797 struct rb_node *next;
798 struct hist_entry *n;
800 if (!sort__need_collapse)
803 next = rb_first(&hist);
805 n = rb_entry(next, struct hist_entry, rb_node);
806 next = rb_next(&n->rb_node);
808 rb_erase(&n->rb_node, &hist);
809 collapse__insert_entry(n);
814 * reverse the map, sort on count.
817 static struct rb_root output_hists;
819 static void output__insert_entry(struct hist_entry *he)
821 struct rb_node **p = &output_hists.rb_node;
822 struct rb_node *parent = NULL;
823 struct hist_entry *iter;
827 iter = rb_entry(parent, struct hist_entry, rb_node);
829 if (he->count > iter->count)
835 rb_link_node(&he->rb_node, parent, p);
836 rb_insert_color(&he->rb_node, &output_hists);
839 static void output__resort(void)
841 struct rb_node *next;
842 struct hist_entry *n;
843 struct rb_root *tree = &hist;
845 if (sort__need_collapse)
846 tree = &collapse_hists;
848 next = rb_first(tree);
851 n = rb_entry(next, struct hist_entry, rb_node);
852 next = rb_next(&n->rb_node);
854 rb_erase(&n->rb_node, tree);
855 output__insert_entry(n);
859 static size_t output__fprintf(FILE *fp, uint64_t total_samples)
861 struct hist_entry *pos;
862 struct sort_entry *se;
868 fprintf(fp, "# (%Ld samples)\n", (__u64)total_samples);
871 fprintf(fp, "# Overhead");
872 list_for_each_entry(se, &hist_entry__sort_list, list)
873 fprintf(fp, " %s", se->header);
876 fprintf(fp, "# ........");
877 list_for_each_entry(se, &hist_entry__sort_list, list) {
881 for (i = 0; i < strlen(se->header); i++)
888 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
889 pos = rb_entry(nd, struct hist_entry, rb_node);
890 ret += hist_entry__fprintf(fp, pos, total_samples);
893 if (!strcmp(sort_order, default_sort_order)) {
895 fprintf(fp, "# (For more details, try: perf report --sort comm,dso,symbol)\n");
903 static void register_idle_thread(void)
905 struct thread *thread = threads__findnew(0);
907 if (thread == NULL ||
908 thread__set_comm(thread, "[idle]")) {
909 fprintf(stderr, "problem inserting idle task.\n");
914 static unsigned long total = 0,
921 process_overflow_event(event_t *event, unsigned long offset, unsigned long head)
925 struct dso *dso = NULL;
926 struct thread *thread = threads__findnew(event->ip.pid);
927 uint64_t ip = event->ip.ip;
928 struct map *map = NULL;
930 dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
931 (void *)(offset + head),
932 (void *)(long)(event->header.size),
937 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
939 if (thread == NULL) {
940 fprintf(stderr, "problem processing %d event, skipping it.\n",
945 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
951 dprintf(" ...... dso: %s\n", dso->name);
953 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
958 map = thread__find_map(thread, ip);
960 ip = map->map_ip(map, ip);
964 * If this is outside of all known maps,
965 * and is a negative address, try to look it
966 * up in the kernel dso, as it might be a
967 * vsyscall (which executes in user-mode):
969 if ((long long)ip < 0)
972 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
977 dprintf(" ...... dso: [hypervisor]\n");
980 if (show & show_mask) {
981 struct symbol *sym = NULL;
984 sym = dso->find_symbol(dso, ip);
986 if (hist_entry__add(thread, map, dso, sym, ip, level)) {
988 "problem incrementing symbol count, skipping event\n");
998 process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
1000 struct thread *thread = threads__findnew(event->mmap.pid);
1001 struct map *map = map__new(&event->mmap);
1003 dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
1004 (void *)(offset + head),
1005 (void *)(long)(event->header.size),
1007 (void *)(long)event->mmap.start,
1008 (void *)(long)event->mmap.len,
1009 (void *)(long)event->mmap.pgoff,
1010 event->mmap.filename);
1012 if (thread == NULL || map == NULL) {
1013 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
1017 thread__insert_map(thread, map);
1024 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
1026 struct thread *thread = threads__findnew(event->comm.pid);
1028 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
1029 (void *)(offset + head),
1030 (void *)(long)(event->header.size),
1031 event->comm.comm, event->comm.pid);
1033 if (thread == NULL ||
1034 thread__set_comm(thread, event->comm.comm)) {
1035 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
1044 process_fork_event(event_t *event, unsigned long offset, unsigned long head)
1046 struct thread *thread = threads__findnew(event->fork.pid);
1047 struct thread *parent = threads__findnew(event->fork.ppid);
1049 dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
1050 (void *)(offset + head),
1051 (void *)(long)(event->header.size),
1052 event->fork.pid, event->fork.ppid);
1054 if (!thread || !parent || thread__fork(thread, parent)) {
1055 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
1064 process_period_event(event_t *event, unsigned long offset, unsigned long head)
1066 dprintf("%p [%p]: PERF_EVENT_PERIOD: time:%Ld, id:%Ld: period:%Ld\n",
1067 (void *)(offset + head),
1068 (void *)(long)(event->header.size),
1071 event->period.sample_period);
1077 process_event(event_t *event, unsigned long offset, unsigned long head)
1079 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW)
1080 return process_overflow_event(event, offset, head);
1082 switch (event->header.type) {
1083 case PERF_EVENT_MMAP:
1084 return process_mmap_event(event, offset, head);
1086 case PERF_EVENT_COMM:
1087 return process_comm_event(event, offset, head);
1089 case PERF_EVENT_FORK:
1090 return process_fork_event(event, offset, head);
1092 case PERF_EVENT_PERIOD:
1093 return process_period_event(event, offset, head);
1095 * We dont process them right now but they are fine:
1098 case PERF_EVENT_THROTTLE:
1099 case PERF_EVENT_UNTHROTTLE:
1109 static int __cmd_report(void)
1111 int ret, rc = EXIT_FAILURE;
1112 unsigned long offset = 0;
1113 unsigned long head = 0;
1119 register_idle_thread();
1121 input = open(input_name, O_RDONLY);
1123 perror("failed to open file");
1127 ret = fstat(input, &stat);
1129 perror("failed to stat file");
1133 if (!stat.st_size) {
1134 fprintf(stderr, "zero-sized file, nothing to do!\n");
1138 if (load_kernel() < 0) {
1139 perror("failed to load kernel symbols");
1140 return EXIT_FAILURE;
1144 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
1145 perror("failed to get the current directory");
1146 return EXIT_FAILURE;
1148 cwdlen = strlen(cwd);
1154 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1155 MAP_SHARED, input, offset);
1156 if (buf == MAP_FAILED) {
1157 perror("failed to mmap file");
1162 event = (event_t *)(buf + head);
1164 size = event->header.size;
1168 if (head + event->header.size >= page_size * mmap_window) {
1169 unsigned long shift = page_size * (head / page_size);
1172 ret = munmap(buf, page_size * mmap_window);
1180 size = event->header.size;
1182 dprintf("%p [%p]: event: %d\n",
1183 (void *)(offset + head),
1184 (void *)(long)event->header.size,
1185 event->header.type);
1187 if (!size || process_event(event, offset, head) < 0) {
1189 dprintf("%p [%p]: skipping unknown header type: %d\n",
1190 (void *)(offset + head),
1191 (void *)(long)(event->header.size),
1192 event->header.type);
1197 * assume we lost track of the stream, check alignment, and
1198 * increment a single u64 in the hope to catch on again 'soon'.
1201 if (unlikely(head & 7))
1209 if (offset + head < stat.st_size)
1215 dprintf(" IP events: %10ld\n", total);
1216 dprintf(" mmap events: %10ld\n", total_mmap);
1217 dprintf(" comm events: %10ld\n", total_comm);
1218 dprintf(" fork events: %10ld\n", total_fork);
1219 dprintf(" unknown events: %10ld\n", total_unknown);
1225 threads__fprintf(stdout);
1228 dsos__fprintf(stdout);
1232 output__fprintf(stdout, total);
1237 static const char * const report_usage[] = {
1238 "perf report [<options>] <command>",
1242 static const struct option options[] = {
1243 OPT_STRING('i', "input", &input_name, "file",
1245 OPT_BOOLEAN('v', "verbose", &verbose,
1246 "be more verbose (show symbol address, etc)"),
1247 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1248 "dump raw trace in ASCII"),
1249 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
1250 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1251 "sort by key(s): pid, comm, dso, symbol. Default: pid,symbol"),
1252 OPT_BOOLEAN('P', "full-paths", &full_paths,
1253 "Don't shorten the pathnames taking into account the cwd"),
1257 static void setup_sorting(void)
1259 char *tmp, *tok, *str = strdup(sort_order);
1261 for (tok = strtok_r(str, ", ", &tmp);
1262 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1263 if (sort_dimension__add(tok) < 0) {
1264 error("Unknown --sort key: `%s'", tok);
1265 usage_with_options(report_usage, options);
1272 int cmd_report(int argc, const char **argv, const char *prefix)
1276 page_size = getpagesize();
1278 argc = parse_options(argc, argv, options, report_usage, 0);
1283 * Any (unrecognized) arguments left?
1286 usage_with_options(report_usage, options);
1290 return __cmd_report();