4 * Builtin annotate 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 <linux/list.h>
14 #include "util/cache.h"
15 #include <linux/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";
30 static char default_sort_order[] = "comm,symbol";
31 static char *sort_order = default_sort_order;
34 static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
36 static int dump_trace = 0;
37 #define dprintf(x...) do { if (dump_trace) printf(x); } while (0)
40 static int full_paths;
42 static int print_line;
44 static unsigned long page_size;
45 static unsigned long mmap_window = 32;
56 struct rb_node rb_node;
57 struct list_head maps;
62 static struct thread *thread__new(pid_t pid)
64 struct thread *self = malloc(sizeof(*self));
68 self->comm = malloc(32);
70 snprintf(self->comm, 32, ":%d", self->pid);
71 INIT_LIST_HEAD(&self->maps);
77 static int thread__set_comm(struct thread *self, const char *comm)
81 self->comm = strdup(comm);
82 return self->comm ? 0 : -ENOMEM;
85 static size_t thread__fprintf(struct thread *self, FILE *fp)
88 size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
90 list_for_each_entry(pos, &self->maps, node)
91 ret += map__fprintf(pos, fp);
97 static struct rb_root threads;
98 static struct thread *last_match;
100 static struct thread *threads__findnew(pid_t pid)
102 struct rb_node **p = &threads.rb_node;
103 struct rb_node *parent = NULL;
107 * Font-end cache - PID lookups come in blocks,
108 * so most of the time we dont have to look up
111 if (last_match && last_match->pid == pid)
116 th = rb_entry(parent, struct thread, rb_node);
118 if (th->pid == pid) {
129 th = thread__new(pid);
131 rb_link_node(&th->rb_node, parent, p);
132 rb_insert_color(&th->rb_node, &threads);
139 static void thread__insert_map(struct thread *self, struct map *map)
141 struct map *pos, *tmp;
143 list_for_each_entry_safe(pos, tmp, &self->maps, node) {
144 if (map__overlap(pos, map)) {
145 list_del_init(&pos->node);
151 list_add_tail(&map->node, &self->maps);
154 static int thread__fork(struct thread *self, struct thread *parent)
160 self->comm = strdup(parent->comm);
164 list_for_each_entry(map, &parent->maps, node) {
165 struct map *new = map__clone(map);
168 thread__insert_map(self, new);
174 static struct map *thread__find_map(struct thread *self, u64 ip)
181 list_for_each_entry(pos, &self->maps, node)
182 if (ip >= pos->start && ip <= pos->end)
188 static size_t threads__fprintf(FILE *fp)
193 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
194 struct thread *pos = rb_entry(nd, struct thread, rb_node);
196 ret += thread__fprintf(pos, fp);
203 * histogram, sorted on item, collects counts
206 static struct rb_root hist;
209 struct rb_node rb_node;
211 struct thread *thread;
222 * configurable sorting bits
226 struct list_head list;
230 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
231 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
232 size_t (*print)(FILE *fp, struct hist_entry *);
238 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
240 return right->thread->pid - left->thread->pid;
244 sort__thread_print(FILE *fp, struct hist_entry *self)
246 return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
249 static struct sort_entry sort_thread = {
250 .header = " Command: Pid",
251 .cmp = sort__thread_cmp,
252 .print = sort__thread_print,
258 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
260 return right->thread->pid - left->thread->pid;
264 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
266 char *comm_l = left->thread->comm;
267 char *comm_r = right->thread->comm;
269 if (!comm_l || !comm_r) {
270 if (!comm_l && !comm_r)
278 return strcmp(comm_l, comm_r);
282 sort__comm_print(FILE *fp, struct hist_entry *self)
284 return fprintf(fp, "%16s", self->thread->comm);
287 static struct sort_entry sort_comm = {
288 .header = " Command",
289 .cmp = sort__comm_cmp,
290 .collapse = sort__comm_collapse,
291 .print = sort__comm_print,
297 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
299 struct dso *dso_l = left->dso;
300 struct dso *dso_r = right->dso;
302 if (!dso_l || !dso_r) {
303 if (!dso_l && !dso_r)
311 return strcmp(dso_l->name, dso_r->name);
315 sort__dso_print(FILE *fp, struct hist_entry *self)
318 return fprintf(fp, "%-25s", self->dso->name);
320 return fprintf(fp, "%016llx ", (u64)self->ip);
323 static struct sort_entry sort_dso = {
324 .header = "Shared Object ",
325 .cmp = sort__dso_cmp,
326 .print = sort__dso_print,
332 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
336 if (left->sym == right->sym)
339 ip_l = left->sym ? left->sym->start : left->ip;
340 ip_r = right->sym ? right->sym->start : right->ip;
342 return (int64_t)(ip_r - ip_l);
346 sort__sym_print(FILE *fp, struct hist_entry *self)
351 ret += fprintf(fp, "%#018llx ", (u64)self->ip);
354 ret += fprintf(fp, "[%c] %s",
355 self->dso == kernel_dso ? 'k' : '.', self->sym->name);
357 ret += fprintf(fp, "%#016llx", (u64)self->ip);
363 static struct sort_entry sort_sym = {
365 .cmp = sort__sym_cmp,
366 .print = sort__sym_print,
369 static int sort__need_collapse = 0;
371 struct sort_dimension {
373 struct sort_entry *entry;
377 static struct sort_dimension sort_dimensions[] = {
378 { .name = "pid", .entry = &sort_thread, },
379 { .name = "comm", .entry = &sort_comm, },
380 { .name = "dso", .entry = &sort_dso, },
381 { .name = "symbol", .entry = &sort_sym, },
384 static LIST_HEAD(hist_entry__sort_list);
386 static int sort_dimension__add(char *tok)
390 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
391 struct sort_dimension *sd = &sort_dimensions[i];
396 if (strncasecmp(tok, sd->name, strlen(tok)))
399 if (sd->entry->collapse)
400 sort__need_collapse = 1;
402 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
412 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
414 struct sort_entry *se;
417 list_for_each_entry(se, &hist_entry__sort_list, list) {
418 cmp = se->cmp(left, right);
427 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
429 struct sort_entry *se;
432 list_for_each_entry(se, &hist_entry__sort_list, list) {
433 int64_t (*f)(struct hist_entry *, struct hist_entry *);
435 f = se->collapse ?: se->cmp;
437 cmp = f(left, right);
446 * collect histogram counts
448 static void hist_hit(struct hist_entry *he, u64 ip)
450 unsigned int sym_size, offset;
451 struct symbol *sym = he->sym;
455 if (!sym || !sym->hist)
458 sym_size = sym->end - sym->start;
459 offset = ip - sym->start;
461 if (offset >= sym_size)
468 printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n",
469 (void *)(unsigned long)he->sym->start,
471 (void *)(unsigned long)ip, ip - he->sym->start,
476 hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
477 struct symbol *sym, u64 ip, char level)
479 struct rb_node **p = &hist.rb_node;
480 struct rb_node *parent = NULL;
481 struct hist_entry *he;
482 struct hist_entry entry = {
495 he = rb_entry(parent, struct hist_entry, rb_node);
497 cmp = hist_entry__cmp(&entry, he);
511 he = malloc(sizeof(*he));
515 rb_link_node(&he->rb_node, parent, p);
516 rb_insert_color(&he->rb_node, &hist);
521 static void hist_entry__free(struct hist_entry *he)
527 * collapse the histogram
530 static struct rb_root collapse_hists;
532 static void collapse__insert_entry(struct hist_entry *he)
534 struct rb_node **p = &collapse_hists.rb_node;
535 struct rb_node *parent = NULL;
536 struct hist_entry *iter;
541 iter = rb_entry(parent, struct hist_entry, rb_node);
543 cmp = hist_entry__collapse(iter, he);
546 iter->count += he->count;
547 hist_entry__free(he);
557 rb_link_node(&he->rb_node, parent, p);
558 rb_insert_color(&he->rb_node, &collapse_hists);
561 static void collapse__resort(void)
563 struct rb_node *next;
564 struct hist_entry *n;
566 if (!sort__need_collapse)
569 next = rb_first(&hist);
571 n = rb_entry(next, struct hist_entry, rb_node);
572 next = rb_next(&n->rb_node);
574 rb_erase(&n->rb_node, &hist);
575 collapse__insert_entry(n);
580 * reverse the map, sort on count.
583 static struct rb_root output_hists;
585 static void output__insert_entry(struct hist_entry *he)
587 struct rb_node **p = &output_hists.rb_node;
588 struct rb_node *parent = NULL;
589 struct hist_entry *iter;
593 iter = rb_entry(parent, struct hist_entry, rb_node);
595 if (he->count > iter->count)
601 rb_link_node(&he->rb_node, parent, p);
602 rb_insert_color(&he->rb_node, &output_hists);
605 static void output__resort(void)
607 struct rb_node *next;
608 struct hist_entry *n;
609 struct rb_root *tree = &hist;
611 if (sort__need_collapse)
612 tree = &collapse_hists;
614 next = rb_first(tree);
617 n = rb_entry(next, struct hist_entry, rb_node);
618 next = rb_next(&n->rb_node);
620 rb_erase(&n->rb_node, tree);
621 output__insert_entry(n);
625 static void register_idle_thread(void)
627 struct thread *thread = threads__findnew(0);
629 if (thread == NULL ||
630 thread__set_comm(thread, "[idle]")) {
631 fprintf(stderr, "problem inserting idle task.\n");
636 static unsigned long total = 0,
643 process_sample_event(event_t *event, unsigned long offset, unsigned long head)
647 struct dso *dso = NULL;
648 struct thread *thread = threads__findnew(event->ip.pid);
649 u64 ip = event->ip.ip;
650 struct map *map = NULL;
652 dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
653 (void *)(offset + head),
654 (void *)(long)(event->header.size),
659 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
661 if (thread == NULL) {
662 fprintf(stderr, "problem processing %d event, skipping it.\n",
667 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
673 dprintf(" ...... dso: %s\n", dso->name);
675 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
680 map = thread__find_map(thread, ip);
682 ip = map->map_ip(map, ip);
686 * If this is outside of all known maps,
687 * and is a negative address, try to look it
688 * up in the kernel dso, as it might be a
689 * vsyscall (which executes in user-mode):
691 if ((long long)ip < 0)
694 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
699 dprintf(" ...... dso: [hypervisor]\n");
702 if (show & show_mask) {
703 struct symbol *sym = NULL;
706 sym = dso->find_symbol(dso, ip);
708 if (hist_entry__add(thread, map, dso, sym, ip, level)) {
710 "problem incrementing symbol count, skipping event\n");
720 process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
722 struct thread *thread = threads__findnew(event->mmap.pid);
723 struct map *map = map__new(&event->mmap, NULL, 0);
725 dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
726 (void *)(offset + head),
727 (void *)(long)(event->header.size),
729 (void *)(long)event->mmap.start,
730 (void *)(long)event->mmap.len,
731 (void *)(long)event->mmap.pgoff,
732 event->mmap.filename);
734 if (thread == NULL || map == NULL) {
735 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
739 thread__insert_map(thread, map);
746 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
748 struct thread *thread = threads__findnew(event->comm.pid);
750 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
751 (void *)(offset + head),
752 (void *)(long)(event->header.size),
753 event->comm.comm, event->comm.pid);
755 if (thread == NULL ||
756 thread__set_comm(thread, event->comm.comm)) {
757 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
766 process_fork_event(event_t *event, unsigned long offset, unsigned long head)
768 struct thread *thread = threads__findnew(event->fork.pid);
769 struct thread *parent = threads__findnew(event->fork.ppid);
771 dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
772 (void *)(offset + head),
773 (void *)(long)(event->header.size),
774 event->fork.pid, event->fork.ppid);
776 if (!thread || !parent || thread__fork(thread, parent)) {
777 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
786 process_event(event_t *event, unsigned long offset, unsigned long head)
788 switch (event->header.type) {
789 case PERF_EVENT_SAMPLE:
790 return process_sample_event(event, offset, head);
792 case PERF_EVENT_MMAP:
793 return process_mmap_event(event, offset, head);
795 case PERF_EVENT_COMM:
796 return process_comm_event(event, offset, head);
798 case PERF_EVENT_FORK:
799 return process_fork_event(event, offset, head);
801 * We dont process them right now but they are fine:
804 case PERF_EVENT_THROTTLE:
805 case PERF_EVENT_UNTHROTTLE:
816 parse_line(FILE *file, struct symbol *sym, u64 start, u64 len)
818 char *line = NULL, *tmp, *tmp2;
819 static const char *prev_line;
820 static const char *prev_color;
827 if (getline(&line, &line_len, file) < 0)
832 c = strchr(line, '\n');
841 * Strip leading spaces:
852 * Parse hexa addresses followed by ':'
854 line_ip = strtoull(tmp, &tmp2, 16);
860 const char *path = NULL;
861 unsigned int hits = 0;
862 double percent = 0.0;
864 struct sym_ext *sym_ext = sym->priv;
866 offset = line_ip - start;
868 hits = sym->hist[offset];
870 if (offset < len && sym_ext) {
871 path = sym_ext[offset].path;
872 percent = sym_ext[offset].percent;
873 } else if (sym->hist_sum)
874 percent = 100.0 * hits / sym->hist_sum;
876 color = get_percent_color(percent);
879 * Also color the filename and line if needed, with
880 * the same color than the percentage. Don't print it
881 * twice for close colored ip with the same filename:line
884 if (!prev_line || strcmp(prev_line, path)
885 || color != prev_color) {
886 color_fprintf(stdout, color, " %s", path);
892 color_fprintf(stdout, color, " %7.2f", percent);
894 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", line);
899 printf(" : %s\n", line);
905 static struct rb_root root_sym_ext;
907 static void insert_source_line(struct sym_ext *sym_ext)
909 struct sym_ext *iter;
910 struct rb_node **p = &root_sym_ext.rb_node;
911 struct rb_node *parent = NULL;
915 iter = rb_entry(parent, struct sym_ext, node);
917 if (sym_ext->percent > iter->percent)
923 rb_link_node(&sym_ext->node, parent, p);
924 rb_insert_color(&sym_ext->node, &root_sym_ext);
927 static void free_source_line(struct symbol *sym, int len)
929 struct sym_ext *sym_ext = sym->priv;
935 for (i = 0; i < len; i++)
936 free(sym_ext[i].path);
940 root_sym_ext = RB_ROOT;
943 /* Get the filename:line for the colored entries */
945 get_source_line(struct symbol *sym, u64 start, int len, char *filename)
948 char cmd[PATH_MAX * 2];
949 struct sym_ext *sym_ext;
954 sym->priv = calloc(len, sizeof(struct sym_ext));
960 for (i = 0; i < len; i++) {
966 sym_ext[i].percent = 100.0 * sym->hist[i] / sym->hist_sum;
967 if (sym_ext[i].percent <= 0.5)
971 sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
972 fp = popen(cmd, "r");
976 if (getline(&path, &line_len, fp) < 0 || !line_len)
979 sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
980 if (!sym_ext[i].path)
983 strcpy(sym_ext[i].path, path);
984 insert_source_line(&sym_ext[i]);
991 static void print_summary(char *filename)
993 struct sym_ext *sym_ext;
994 struct rb_node *node;
996 printf("\nSorted summary for file %s\n", filename);
997 printf("----------------------------------------------\n\n");
999 if (RB_EMPTY_ROOT(&root_sym_ext)) {
1000 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
1004 node = rb_first(&root_sym_ext);
1010 sym_ext = rb_entry(node, struct sym_ext, node);
1011 percent = sym_ext->percent;
1012 color = get_percent_color(percent);
1013 path = sym_ext->path;
1015 color_fprintf(stdout, color, " %7.2f %s", percent, path);
1016 node = rb_next(node);
1020 static void annotate_sym(struct dso *dso, struct symbol *sym)
1022 char *filename = dso->name, *d_filename;
1023 u64 start, end, len;
1024 char command[PATH_MAX*2];
1030 filename = sym->module->path;
1031 else if (dso == kernel_dso)
1034 start = sym->obj_start;
1038 d_filename = filename;
1040 d_filename = basename(filename);
1042 end = start + sym->end - sym->start + 1;
1043 len = sym->end - sym->start;
1046 get_source_line(sym, start, len, filename);
1047 print_summary(filename);
1050 printf("\n\n------------------------------------------------\n");
1051 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
1052 printf("------------------------------------------------\n");
1055 printf("annotating [%p] %30s : [%p] %30s\n", dso, dso->name, sym, sym->name);
1057 sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
1058 (u64)start, (u64)end, filename, filename);
1061 printf("doing: %s\n", command);
1063 file = popen(command, "r");
1067 while (!feof(file)) {
1068 if (parse_line(file, sym, start, len) < 0)
1074 free_source_line(sym, len);
1077 static void find_annotations(void)
1083 list_for_each_entry(dso, &dsos, node) {
1085 for (nd = rb_first(&dso->syms); nd; nd = rb_next(nd)) {
1086 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
1089 annotate_sym(dso, sym);
1096 printf(" Error: symbol '%s' not present amongst the samples.\n", sym_hist_filter);
1099 static int __cmd_annotate(void)
1101 int ret, rc = EXIT_FAILURE;
1102 unsigned long offset = 0;
1103 unsigned long head = 0;
1109 register_idle_thread();
1111 input = open(input_name, O_RDONLY);
1113 perror("failed to open file");
1117 ret = fstat(input, &stat);
1119 perror("failed to stat file");
1123 if (!stat.st_size) {
1124 fprintf(stderr, "zero-sized file, nothing to do!\n");
1128 if (load_kernel() < 0) {
1129 perror("failed to load kernel symbols");
1130 return EXIT_FAILURE;
1134 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1135 MAP_SHARED, input, offset);
1136 if (buf == MAP_FAILED) {
1137 perror("failed to mmap file");
1142 event = (event_t *)(buf + head);
1144 size = event->header.size;
1148 if (head + event->header.size >= page_size * mmap_window) {
1149 unsigned long shift = page_size * (head / page_size);
1152 ret = munmap(buf, page_size * mmap_window);
1160 size = event->header.size;
1162 dprintf("%p [%p]: event: %d\n",
1163 (void *)(offset + head),
1164 (void *)(long)event->header.size,
1165 event->header.type);
1167 if (!size || process_event(event, offset, head) < 0) {
1169 dprintf("%p [%p]: skipping unknown header type: %d\n",
1170 (void *)(offset + head),
1171 (void *)(long)(event->header.size),
1172 event->header.type);
1177 * assume we lost track of the stream, check alignment, and
1178 * increment a single u64 in the hope to catch on again 'soon'.
1181 if (unlikely(head & 7))
1189 if (offset + head < (unsigned long)stat.st_size)
1195 dprintf(" IP events: %10ld\n", total);
1196 dprintf(" mmap events: %10ld\n", total_mmap);
1197 dprintf(" comm events: %10ld\n", total_comm);
1198 dprintf(" fork events: %10ld\n", total_fork);
1199 dprintf(" unknown events: %10ld\n", total_unknown);
1205 threads__fprintf(stdout);
1208 dsos__fprintf(stdout);
1218 static const char * const annotate_usage[] = {
1219 "perf annotate [<options>] <command>",
1223 static const struct option options[] = {
1224 OPT_STRING('i', "input", &input_name, "file",
1226 OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
1227 "symbol to annotate"),
1228 OPT_BOOLEAN('v', "verbose", &verbose,
1229 "be more verbose (show symbol address, etc)"),
1230 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1231 "dump raw trace in ASCII"),
1232 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
1233 OPT_BOOLEAN('m', "modules", &modules,
1234 "load module symbols - WARNING: use only with -k and LIVE kernel"),
1235 OPT_BOOLEAN('l', "print-line", &print_line,
1236 "print matching source lines (may be slow)"),
1237 OPT_BOOLEAN('P', "full-paths", &full_paths,
1238 "Don't shorten the displayed pathnames"),
1242 static void setup_sorting(void)
1244 char *tmp, *tok, *str = strdup(sort_order);
1246 for (tok = strtok_r(str, ", ", &tmp);
1247 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1248 if (sort_dimension__add(tok) < 0) {
1249 error("Unknown --sort key: `%s'", tok);
1250 usage_with_options(annotate_usage, options);
1257 int cmd_annotate(int argc, const char **argv, const char *prefix __used)
1261 page_size = getpagesize();
1263 argc = parse_options(argc, argv, options, annotate_usage, 0);
1269 * Special case: if there's an argument left then assume tha
1270 * it's a symbol filter:
1273 usage_with_options(annotate_usage, options);
1275 sym_hist_filter = argv[0];
1278 if (!sym_hist_filter)
1279 usage_with_options(annotate_usage, options);
1283 return __cmd_annotate();