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"
23 #include "util/thread.h"
29 static char const *input_name = "perf.data";
31 static char default_sort_order[] = "comm,symbol";
32 static char *sort_order = default_sort_order;
35 static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
37 static int full_paths;
39 static int print_line;
41 static unsigned long page_size;
42 static unsigned long mmap_window = 32;
44 static struct rb_root threads;
45 static struct thread *last_match;
55 * histogram, sorted on item, collects counts
58 static struct rb_root hist;
61 struct rb_node rb_node;
63 struct thread *thread;
74 * configurable sorting bits
78 struct list_head list;
82 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
83 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
84 size_t (*print)(FILE *fp, struct hist_entry *);
90 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
92 return right->thread->pid - left->thread->pid;
96 sort__thread_print(FILE *fp, struct hist_entry *self)
98 return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
101 static struct sort_entry sort_thread = {
102 .header = " Command: Pid",
103 .cmp = sort__thread_cmp,
104 .print = sort__thread_print,
110 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
112 return right->thread->pid - left->thread->pid;
116 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
118 char *comm_l = left->thread->comm;
119 char *comm_r = right->thread->comm;
121 if (!comm_l || !comm_r) {
122 if (!comm_l && !comm_r)
130 return strcmp(comm_l, comm_r);
134 sort__comm_print(FILE *fp, struct hist_entry *self)
136 return fprintf(fp, "%16s", self->thread->comm);
139 static struct sort_entry sort_comm = {
140 .header = " Command",
141 .cmp = sort__comm_cmp,
142 .collapse = sort__comm_collapse,
143 .print = sort__comm_print,
149 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
151 struct dso *dso_l = left->dso;
152 struct dso *dso_r = right->dso;
154 if (!dso_l || !dso_r) {
155 if (!dso_l && !dso_r)
163 return strcmp(dso_l->name, dso_r->name);
167 sort__dso_print(FILE *fp, struct hist_entry *self)
170 return fprintf(fp, "%-25s", self->dso->name);
172 return fprintf(fp, "%016llx ", (u64)self->ip);
175 static struct sort_entry sort_dso = {
176 .header = "Shared Object ",
177 .cmp = sort__dso_cmp,
178 .print = sort__dso_print,
184 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
188 if (left->sym == right->sym)
191 ip_l = left->sym ? left->sym->start : left->ip;
192 ip_r = right->sym ? right->sym->start : right->ip;
194 return (int64_t)(ip_r - ip_l);
198 sort__sym_print(FILE *fp, struct hist_entry *self)
203 ret += fprintf(fp, "%#018llx ", (u64)self->ip);
206 ret += fprintf(fp, "[%c] %s",
207 self->dso == kernel_dso ? 'k' : '.', self->sym->name);
209 ret += fprintf(fp, "%#016llx", (u64)self->ip);
215 static struct sort_entry sort_sym = {
217 .cmp = sort__sym_cmp,
218 .print = sort__sym_print,
221 static int sort__need_collapse = 0;
223 struct sort_dimension {
225 struct sort_entry *entry;
229 static struct sort_dimension sort_dimensions[] = {
230 { .name = "pid", .entry = &sort_thread, },
231 { .name = "comm", .entry = &sort_comm, },
232 { .name = "dso", .entry = &sort_dso, },
233 { .name = "symbol", .entry = &sort_sym, },
236 static LIST_HEAD(hist_entry__sort_list);
238 static int sort_dimension__add(char *tok)
242 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
243 struct sort_dimension *sd = &sort_dimensions[i];
248 if (strncasecmp(tok, sd->name, strlen(tok)))
251 if (sd->entry->collapse)
252 sort__need_collapse = 1;
254 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
264 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
266 struct sort_entry *se;
269 list_for_each_entry(se, &hist_entry__sort_list, list) {
270 cmp = se->cmp(left, right);
279 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
281 struct sort_entry *se;
284 list_for_each_entry(se, &hist_entry__sort_list, list) {
285 int64_t (*f)(struct hist_entry *, struct hist_entry *);
287 f = se->collapse ?: se->cmp;
289 cmp = f(left, right);
298 * collect histogram counts
300 static void hist_hit(struct hist_entry *he, u64 ip)
302 unsigned int sym_size, offset;
303 struct symbol *sym = he->sym;
307 if (!sym || !sym->hist)
310 sym_size = sym->end - sym->start;
311 offset = ip - sym->start;
313 if (offset >= sym_size)
320 printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n",
321 (void *)(unsigned long)he->sym->start,
323 (void *)(unsigned long)ip, ip - he->sym->start,
328 hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
329 struct symbol *sym, u64 ip, char level)
331 struct rb_node **p = &hist.rb_node;
332 struct rb_node *parent = NULL;
333 struct hist_entry *he;
334 struct hist_entry entry = {
347 he = rb_entry(parent, struct hist_entry, rb_node);
349 cmp = hist_entry__cmp(&entry, he);
363 he = malloc(sizeof(*he));
367 rb_link_node(&he->rb_node, parent, p);
368 rb_insert_color(&he->rb_node, &hist);
373 static void hist_entry__free(struct hist_entry *he)
379 * collapse the histogram
382 static struct rb_root collapse_hists;
384 static void collapse__insert_entry(struct hist_entry *he)
386 struct rb_node **p = &collapse_hists.rb_node;
387 struct rb_node *parent = NULL;
388 struct hist_entry *iter;
393 iter = rb_entry(parent, struct hist_entry, rb_node);
395 cmp = hist_entry__collapse(iter, he);
398 iter->count += he->count;
399 hist_entry__free(he);
409 rb_link_node(&he->rb_node, parent, p);
410 rb_insert_color(&he->rb_node, &collapse_hists);
413 static void collapse__resort(void)
415 struct rb_node *next;
416 struct hist_entry *n;
418 if (!sort__need_collapse)
421 next = rb_first(&hist);
423 n = rb_entry(next, struct hist_entry, rb_node);
424 next = rb_next(&n->rb_node);
426 rb_erase(&n->rb_node, &hist);
427 collapse__insert_entry(n);
432 * reverse the map, sort on count.
435 static struct rb_root output_hists;
437 static void output__insert_entry(struct hist_entry *he)
439 struct rb_node **p = &output_hists.rb_node;
440 struct rb_node *parent = NULL;
441 struct hist_entry *iter;
445 iter = rb_entry(parent, struct hist_entry, rb_node);
447 if (he->count > iter->count)
453 rb_link_node(&he->rb_node, parent, p);
454 rb_insert_color(&he->rb_node, &output_hists);
457 static void output__resort(void)
459 struct rb_node *next;
460 struct hist_entry *n;
461 struct rb_root *tree = &hist;
463 if (sort__need_collapse)
464 tree = &collapse_hists;
466 next = rb_first(tree);
469 n = rb_entry(next, struct hist_entry, rb_node);
470 next = rb_next(&n->rb_node);
472 rb_erase(&n->rb_node, tree);
473 output__insert_entry(n);
477 static void register_idle_thread(void)
479 struct thread *thread = threads__findnew(0, &threads, &last_match);
481 if (thread == NULL ||
482 thread__set_comm(thread, "[idle]")) {
483 fprintf(stderr, "problem inserting idle task.\n");
488 static unsigned long total = 0,
495 process_sample_event(event_t *event, unsigned long offset, unsigned long head)
499 struct dso *dso = NULL;
500 struct thread *thread;
501 u64 ip = event->ip.ip;
502 struct map *map = NULL;
504 thread = threads__findnew(event->ip.pid, &threads, &last_match);
506 dump_printf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
507 (void *)(offset + head),
508 (void *)(long)(event->header.size),
513 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
515 if (thread == NULL) {
516 fprintf(stderr, "problem processing %d event, skipping it.\n",
521 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
527 dump_printf(" ...... dso: %s\n", dso->name);
529 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
534 map = thread__find_map(thread, ip);
536 ip = map->map_ip(map, ip);
540 * If this is outside of all known maps,
541 * and is a negative address, try to look it
542 * up in the kernel dso, as it might be a
543 * vsyscall (which executes in user-mode):
545 if ((long long)ip < 0)
548 dump_printf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
553 dump_printf(" ...... dso: [hypervisor]\n");
556 if (show & show_mask) {
557 struct symbol *sym = NULL;
560 sym = dso->find_symbol(dso, ip);
562 if (hist_entry__add(thread, map, dso, sym, ip, level)) {
564 "problem incrementing symbol count, skipping event\n");
574 process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
576 struct thread *thread;
577 struct map *map = map__new(&event->mmap, NULL, 0);
579 thread = threads__findnew(event->mmap.pid, &threads, &last_match);
581 dump_printf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
582 (void *)(offset + head),
583 (void *)(long)(event->header.size),
585 (void *)(long)event->mmap.start,
586 (void *)(long)event->mmap.len,
587 (void *)(long)event->mmap.pgoff,
588 event->mmap.filename);
590 if (thread == NULL || map == NULL) {
591 dump_printf("problem processing PERF_EVENT_MMAP, skipping event.\n");
595 thread__insert_map(thread, map);
602 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
604 struct thread *thread;
606 thread = threads__findnew(event->comm.pid, &threads, &last_match);
607 dump_printf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
608 (void *)(offset + head),
609 (void *)(long)(event->header.size),
610 event->comm.comm, event->comm.pid);
612 if (thread == NULL ||
613 thread__set_comm(thread, event->comm.comm)) {
614 dump_printf("problem processing PERF_EVENT_COMM, skipping event.\n");
623 process_fork_event(event_t *event, unsigned long offset, unsigned long head)
625 struct thread *thread;
626 struct thread *parent;
628 thread = threads__findnew(event->fork.pid, &threads, &last_match);
629 parent = threads__findnew(event->fork.ppid, &threads, &last_match);
630 dump_printf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
631 (void *)(offset + head),
632 (void *)(long)(event->header.size),
633 event->fork.pid, event->fork.ppid);
635 if (!thread || !parent || thread__fork(thread, parent)) {
636 dump_printf("problem processing PERF_EVENT_FORK, skipping event.\n");
645 process_event(event_t *event, unsigned long offset, unsigned long head)
647 switch (event->header.type) {
648 case PERF_EVENT_SAMPLE:
649 return process_sample_event(event, offset, head);
651 case PERF_EVENT_MMAP:
652 return process_mmap_event(event, offset, head);
654 case PERF_EVENT_COMM:
655 return process_comm_event(event, offset, head);
657 case PERF_EVENT_FORK:
658 return process_fork_event(event, offset, head);
660 * We dont process them right now but they are fine:
663 case PERF_EVENT_THROTTLE:
664 case PERF_EVENT_UNTHROTTLE:
675 parse_line(FILE *file, struct symbol *sym, u64 start, u64 len)
677 char *line = NULL, *tmp, *tmp2;
678 static const char *prev_line;
679 static const char *prev_color;
686 if (getline(&line, &line_len, file) < 0)
691 c = strchr(line, '\n');
700 * Strip leading spaces:
711 * Parse hexa addresses followed by ':'
713 line_ip = strtoull(tmp, &tmp2, 16);
719 const char *path = NULL;
720 unsigned int hits = 0;
721 double percent = 0.0;
723 struct sym_ext *sym_ext = sym->priv;
725 offset = line_ip - start;
727 hits = sym->hist[offset];
729 if (offset < len && sym_ext) {
730 path = sym_ext[offset].path;
731 percent = sym_ext[offset].percent;
732 } else if (sym->hist_sum)
733 percent = 100.0 * hits / sym->hist_sum;
735 color = get_percent_color(percent);
738 * Also color the filename and line if needed, with
739 * the same color than the percentage. Don't print it
740 * twice for close colored ip with the same filename:line
743 if (!prev_line || strcmp(prev_line, path)
744 || color != prev_color) {
745 color_fprintf(stdout, color, " %s", path);
751 color_fprintf(stdout, color, " %7.2f", percent);
753 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", line);
758 printf(" : %s\n", line);
764 static struct rb_root root_sym_ext;
766 static void insert_source_line(struct sym_ext *sym_ext)
768 struct sym_ext *iter;
769 struct rb_node **p = &root_sym_ext.rb_node;
770 struct rb_node *parent = NULL;
774 iter = rb_entry(parent, struct sym_ext, node);
776 if (sym_ext->percent > iter->percent)
782 rb_link_node(&sym_ext->node, parent, p);
783 rb_insert_color(&sym_ext->node, &root_sym_ext);
786 static void free_source_line(struct symbol *sym, int len)
788 struct sym_ext *sym_ext = sym->priv;
794 for (i = 0; i < len; i++)
795 free(sym_ext[i].path);
799 root_sym_ext = RB_ROOT;
802 /* Get the filename:line for the colored entries */
804 get_source_line(struct symbol *sym, u64 start, int len, const char *filename)
807 char cmd[PATH_MAX * 2];
808 struct sym_ext *sym_ext;
813 sym->priv = calloc(len, sizeof(struct sym_ext));
819 for (i = 0; i < len; i++) {
825 sym_ext[i].percent = 100.0 * sym->hist[i] / sym->hist_sum;
826 if (sym_ext[i].percent <= 0.5)
830 sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
831 fp = popen(cmd, "r");
835 if (getline(&path, &line_len, fp) < 0 || !line_len)
838 sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
839 if (!sym_ext[i].path)
842 strcpy(sym_ext[i].path, path);
843 insert_source_line(&sym_ext[i]);
850 static void print_summary(const char *filename)
852 struct sym_ext *sym_ext;
853 struct rb_node *node;
855 printf("\nSorted summary for file %s\n", filename);
856 printf("----------------------------------------------\n\n");
858 if (RB_EMPTY_ROOT(&root_sym_ext)) {
859 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
863 node = rb_first(&root_sym_ext);
869 sym_ext = rb_entry(node, struct sym_ext, node);
870 percent = sym_ext->percent;
871 color = get_percent_color(percent);
872 path = sym_ext->path;
874 color_fprintf(stdout, color, " %7.2f %s", percent, path);
875 node = rb_next(node);
879 static void annotate_sym(struct dso *dso, struct symbol *sym)
881 const char *filename = dso->name, *d_filename;
883 char command[PATH_MAX*2];
889 filename = sym->module->path;
890 else if (dso == kernel_dso)
891 filename = vmlinux_name;
893 start = sym->obj_start;
897 d_filename = filename;
899 d_filename = basename(filename);
901 end = start + sym->end - sym->start + 1;
902 len = sym->end - sym->start;
905 get_source_line(sym, start, len, filename);
906 print_summary(filename);
909 printf("\n\n------------------------------------------------\n");
910 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
911 printf("------------------------------------------------\n");
914 printf("annotating [%p] %30s : [%p] %30s\n", dso, dso->name, sym, sym->name);
916 sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
917 (u64)start, (u64)end, filename, filename);
920 printf("doing: %s\n", command);
922 file = popen(command, "r");
926 while (!feof(file)) {
927 if (parse_line(file, sym, start, len) < 0)
933 free_source_line(sym, len);
936 static void find_annotations(void)
942 list_for_each_entry(dso, &dsos, node) {
944 for (nd = rb_first(&dso->syms); nd; nd = rb_next(nd)) {
945 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
948 annotate_sym(dso, sym);
955 printf(" Error: symbol '%s' not present amongst the samples.\n", sym_hist_filter);
958 static int __cmd_annotate(void)
960 int ret, rc = EXIT_FAILURE;
961 unsigned long offset = 0;
962 unsigned long head = 0;
963 struct stat input_stat;
968 register_idle_thread();
970 input = open(input_name, O_RDONLY);
972 perror("failed to open file");
976 ret = fstat(input, &input_stat);
978 perror("failed to stat file");
982 if (!input_stat.st_size) {
983 fprintf(stderr, "zero-sized file, nothing to do!\n");
987 if (load_kernel() < 0) {
988 perror("failed to load kernel symbols");
993 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
994 MAP_SHARED, input, offset);
995 if (buf == MAP_FAILED) {
996 perror("failed to mmap file");
1001 event = (event_t *)(buf + head);
1003 size = event->header.size;
1007 if (head + event->header.size >= page_size * mmap_window) {
1008 unsigned long shift = page_size * (head / page_size);
1011 munmap_ret = munmap(buf, page_size * mmap_window);
1012 assert(munmap_ret == 0);
1019 size = event->header.size;
1021 dump_printf("%p [%p]: event: %d\n",
1022 (void *)(offset + head),
1023 (void *)(long)event->header.size,
1024 event->header.type);
1026 if (!size || process_event(event, offset, head) < 0) {
1028 dump_printf("%p [%p]: skipping unknown header type: %d\n",
1029 (void *)(offset + head),
1030 (void *)(long)(event->header.size),
1031 event->header.type);
1036 * assume we lost track of the stream, check alignment, and
1037 * increment a single u64 in the hope to catch on again 'soon'.
1040 if (unlikely(head & 7))
1048 if (offset + head < (unsigned long)input_stat.st_size)
1054 dump_printf(" IP events: %10ld\n", total);
1055 dump_printf(" mmap events: %10ld\n", total_mmap);
1056 dump_printf(" comm events: %10ld\n", total_comm);
1057 dump_printf(" fork events: %10ld\n", total_fork);
1058 dump_printf(" unknown events: %10ld\n", total_unknown);
1064 threads__fprintf(stdout, &threads);
1067 dsos__fprintf(stdout);
1077 static const char * const annotate_usage[] = {
1078 "perf annotate [<options>] <command>",
1082 static const struct option options[] = {
1083 OPT_STRING('i', "input", &input_name, "file",
1085 OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
1086 "symbol to annotate"),
1087 OPT_BOOLEAN('v', "verbose", &verbose,
1088 "be more verbose (show symbol address, etc)"),
1089 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1090 "dump raw trace in ASCII"),
1091 OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
1092 OPT_BOOLEAN('m', "modules", &modules,
1093 "load module symbols - WARNING: use only with -k and LIVE kernel"),
1094 OPT_BOOLEAN('l', "print-line", &print_line,
1095 "print matching source lines (may be slow)"),
1096 OPT_BOOLEAN('P', "full-paths", &full_paths,
1097 "Don't shorten the displayed pathnames"),
1101 static void setup_sorting(void)
1103 char *tmp, *tok, *str = strdup(sort_order);
1105 for (tok = strtok_r(str, ", ", &tmp);
1106 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1107 if (sort_dimension__add(tok) < 0) {
1108 error("Unknown --sort key: `%s'", tok);
1109 usage_with_options(annotate_usage, options);
1116 int cmd_annotate(int argc, const char **argv, const char *prefix __used)
1120 page_size = getpagesize();
1122 argc = parse_options(argc, argv, options, annotate_usage, 0);
1128 * Special case: if there's an argument left then assume tha
1129 * it's a symbol filter:
1132 usage_with_options(annotate_usage, options);
1134 sym_hist_filter = argv[0];
1137 if (!sym_hist_filter)
1138 usage_with_options(annotate_usage, options);
1142 return __cmd_annotate();