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"
20 #include "util/debug.h"
22 #include "util/parse-options.h"
23 #include "util/parse-events.h"
24 #include "util/thread.h"
26 static char const *input_name = "perf.data";
28 static char default_sort_order[] = "comm,symbol";
29 static char *sort_order = default_sort_order;
33 static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
35 static int full_paths;
37 static int print_line;
39 static unsigned long page_size;
40 static unsigned long mmap_window = 32;
42 static struct rb_root threads;
43 static struct thread *last_match;
53 * histogram, sorted on item, collects counts
56 static struct rb_root hist;
59 struct rb_node rb_node;
61 struct thread *thread;
72 * configurable sorting bits
76 struct list_head list;
80 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
81 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
82 size_t (*print)(FILE *fp, struct hist_entry *);
88 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
90 return right->thread->pid - left->thread->pid;
94 sort__thread_print(FILE *fp, struct hist_entry *self)
96 return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
99 static struct sort_entry sort_thread = {
100 .header = " Command: Pid",
101 .cmp = sort__thread_cmp,
102 .print = sort__thread_print,
108 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
110 return right->thread->pid - left->thread->pid;
114 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
116 char *comm_l = left->thread->comm;
117 char *comm_r = right->thread->comm;
119 if (!comm_l || !comm_r) {
120 if (!comm_l && !comm_r)
128 return strcmp(comm_l, comm_r);
132 sort__comm_print(FILE *fp, struct hist_entry *self)
134 return fprintf(fp, "%16s", self->thread->comm);
137 static struct sort_entry sort_comm = {
138 .header = " Command",
139 .cmp = sort__comm_cmp,
140 .collapse = sort__comm_collapse,
141 .print = sort__comm_print,
147 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
149 struct dso *dso_l = left->dso;
150 struct dso *dso_r = right->dso;
152 if (!dso_l || !dso_r) {
153 if (!dso_l && !dso_r)
161 return strcmp(dso_l->name, dso_r->name);
165 sort__dso_print(FILE *fp, struct hist_entry *self)
168 return fprintf(fp, "%-25s", self->dso->name);
170 return fprintf(fp, "%016llx ", (u64)self->ip);
173 static struct sort_entry sort_dso = {
174 .header = "Shared Object ",
175 .cmp = sort__dso_cmp,
176 .print = sort__dso_print,
182 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
186 if (left->sym == right->sym)
189 ip_l = left->sym ? left->sym->start : left->ip;
190 ip_r = right->sym ? right->sym->start : right->ip;
192 return (int64_t)(ip_r - ip_l);
196 sort__sym_print(FILE *fp, struct hist_entry *self)
201 ret += fprintf(fp, "%#018llx ", (u64)self->ip);
204 ret += fprintf(fp, "[%c] %s",
205 self->dso == kernel_dso ? 'k' : '.', self->sym->name);
207 ret += fprintf(fp, "%#016llx", (u64)self->ip);
213 static struct sort_entry sort_sym = {
215 .cmp = sort__sym_cmp,
216 .print = sort__sym_print,
219 static int sort__need_collapse = 0;
221 struct sort_dimension {
223 struct sort_entry *entry;
227 static struct sort_dimension sort_dimensions[] = {
228 { .name = "pid", .entry = &sort_thread, },
229 { .name = "comm", .entry = &sort_comm, },
230 { .name = "dso", .entry = &sort_dso, },
231 { .name = "symbol", .entry = &sort_sym, },
234 static LIST_HEAD(hist_entry__sort_list);
236 static int sort_dimension__add(char *tok)
240 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
241 struct sort_dimension *sd = &sort_dimensions[i];
246 if (strncasecmp(tok, sd->name, strlen(tok)))
249 if (sd->entry->collapse)
250 sort__need_collapse = 1;
252 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
262 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
264 struct sort_entry *se;
267 list_for_each_entry(se, &hist_entry__sort_list, list) {
268 cmp = se->cmp(left, right);
277 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
279 struct sort_entry *se;
282 list_for_each_entry(se, &hist_entry__sort_list, list) {
283 int64_t (*f)(struct hist_entry *, struct hist_entry *);
285 f = se->collapse ?: se->cmp;
287 cmp = f(left, right);
296 * collect histogram counts
298 static void hist_hit(struct hist_entry *he, u64 ip)
300 unsigned int sym_size, offset;
301 struct symbol *sym = he->sym;
305 if (!sym || !sym->hist)
308 sym_size = sym->end - sym->start;
309 offset = ip - sym->start;
311 if (offset >= sym_size)
318 printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n",
319 (void *)(unsigned long)he->sym->start,
321 (void *)(unsigned long)ip, ip - he->sym->start,
326 hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
327 struct symbol *sym, u64 ip, char level)
329 struct rb_node **p = &hist.rb_node;
330 struct rb_node *parent = NULL;
331 struct hist_entry *he;
332 struct hist_entry entry = {
345 he = rb_entry(parent, struct hist_entry, rb_node);
347 cmp = hist_entry__cmp(&entry, he);
361 he = malloc(sizeof(*he));
365 rb_link_node(&he->rb_node, parent, p);
366 rb_insert_color(&he->rb_node, &hist);
371 static void hist_entry__free(struct hist_entry *he)
377 * collapse the histogram
380 static struct rb_root collapse_hists;
382 static void collapse__insert_entry(struct hist_entry *he)
384 struct rb_node **p = &collapse_hists.rb_node;
385 struct rb_node *parent = NULL;
386 struct hist_entry *iter;
391 iter = rb_entry(parent, struct hist_entry, rb_node);
393 cmp = hist_entry__collapse(iter, he);
396 iter->count += he->count;
397 hist_entry__free(he);
407 rb_link_node(&he->rb_node, parent, p);
408 rb_insert_color(&he->rb_node, &collapse_hists);
411 static void collapse__resort(void)
413 struct rb_node *next;
414 struct hist_entry *n;
416 if (!sort__need_collapse)
419 next = rb_first(&hist);
421 n = rb_entry(next, struct hist_entry, rb_node);
422 next = rb_next(&n->rb_node);
424 rb_erase(&n->rb_node, &hist);
425 collapse__insert_entry(n);
430 * reverse the map, sort on count.
433 static struct rb_root output_hists;
435 static void output__insert_entry(struct hist_entry *he)
437 struct rb_node **p = &output_hists.rb_node;
438 struct rb_node *parent = NULL;
439 struct hist_entry *iter;
443 iter = rb_entry(parent, struct hist_entry, rb_node);
445 if (he->count > iter->count)
451 rb_link_node(&he->rb_node, parent, p);
452 rb_insert_color(&he->rb_node, &output_hists);
455 static void output__resort(void)
457 struct rb_node *next;
458 struct hist_entry *n;
459 struct rb_root *tree = &hist;
461 if (sort__need_collapse)
462 tree = &collapse_hists;
464 next = rb_first(tree);
467 n = rb_entry(next, struct hist_entry, rb_node);
468 next = rb_next(&n->rb_node);
470 rb_erase(&n->rb_node, tree);
471 output__insert_entry(n);
475 static void register_idle_thread(void)
477 struct thread *thread = threads__findnew(0, &threads, &last_match);
479 if (thread == NULL ||
480 thread__set_comm(thread, "[idle]")) {
481 fprintf(stderr, "problem inserting idle task.\n");
486 static unsigned long total = 0,
493 process_sample_event(event_t *event, unsigned long offset, unsigned long head)
497 struct dso *dso = NULL;
498 struct thread *thread;
499 u64 ip = event->ip.ip;
500 struct map *map = NULL;
502 thread = threads__findnew(event->ip.pid, &threads, &last_match);
504 dump_printf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
505 (void *)(offset + head),
506 (void *)(long)(event->header.size),
511 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
513 if (thread == NULL) {
514 fprintf(stderr, "problem processing %d event, skipping it.\n",
519 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
525 dump_printf(" ...... dso: %s\n", dso->name);
527 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
532 map = thread__find_map(thread, ip);
534 ip = map->map_ip(map, ip);
538 * If this is outside of all known maps,
539 * and is a negative address, try to look it
540 * up in the kernel dso, as it might be a
541 * vsyscall (which executes in user-mode):
543 if ((long long)ip < 0)
546 dump_printf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
551 dump_printf(" ...... dso: [hypervisor]\n");
554 if (show & show_mask) {
555 struct symbol *sym = NULL;
558 sym = dso->find_symbol(dso, ip);
560 if (hist_entry__add(thread, map, dso, sym, ip, level)) {
562 "problem incrementing symbol count, skipping event\n");
572 process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
574 struct thread *thread;
575 struct map *map = map__new(&event->mmap, NULL, 0);
577 thread = threads__findnew(event->mmap.pid, &threads, &last_match);
579 dump_printf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
580 (void *)(offset + head),
581 (void *)(long)(event->header.size),
583 (void *)(long)event->mmap.start,
584 (void *)(long)event->mmap.len,
585 (void *)(long)event->mmap.pgoff,
586 event->mmap.filename);
588 if (thread == NULL || map == NULL) {
589 dump_printf("problem processing PERF_EVENT_MMAP, skipping event.\n");
593 thread__insert_map(thread, map);
600 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
602 struct thread *thread;
604 thread = threads__findnew(event->comm.pid, &threads, &last_match);
605 dump_printf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
606 (void *)(offset + head),
607 (void *)(long)(event->header.size),
608 event->comm.comm, event->comm.pid);
610 if (thread == NULL ||
611 thread__set_comm(thread, event->comm.comm)) {
612 dump_printf("problem processing PERF_EVENT_COMM, skipping event.\n");
621 process_fork_event(event_t *event, unsigned long offset, unsigned long head)
623 struct thread *thread;
624 struct thread *parent;
626 thread = threads__findnew(event->fork.pid, &threads, &last_match);
627 parent = threads__findnew(event->fork.ppid, &threads, &last_match);
628 dump_printf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
629 (void *)(offset + head),
630 (void *)(long)(event->header.size),
631 event->fork.pid, event->fork.ppid);
634 * A thread clone will have the same PID for both
637 if (thread == parent)
640 if (!thread || !parent || thread__fork(thread, parent)) {
641 dump_printf("problem processing PERF_EVENT_FORK, skipping event.\n");
650 process_event(event_t *event, unsigned long offset, unsigned long head)
652 switch (event->header.type) {
653 case PERF_EVENT_SAMPLE:
654 return process_sample_event(event, offset, head);
656 case PERF_EVENT_MMAP:
657 return process_mmap_event(event, offset, head);
659 case PERF_EVENT_COMM:
660 return process_comm_event(event, offset, head);
662 case PERF_EVENT_FORK:
663 return process_fork_event(event, offset, head);
665 * We dont process them right now but they are fine:
668 case PERF_EVENT_THROTTLE:
669 case PERF_EVENT_UNTHROTTLE:
680 parse_line(FILE *file, struct symbol *sym, u64 start, u64 len)
682 char *line = NULL, *tmp, *tmp2;
683 static const char *prev_line;
684 static const char *prev_color;
691 if (getline(&line, &line_len, file) < 0)
696 c = strchr(line, '\n');
705 * Strip leading spaces:
716 * Parse hexa addresses followed by ':'
718 line_ip = strtoull(tmp, &tmp2, 16);
724 const char *path = NULL;
725 unsigned int hits = 0;
726 double percent = 0.0;
728 struct sym_ext *sym_ext = sym->priv;
730 offset = line_ip - start;
732 hits = sym->hist[offset];
734 if (offset < len && sym_ext) {
735 path = sym_ext[offset].path;
736 percent = sym_ext[offset].percent;
737 } else if (sym->hist_sum)
738 percent = 100.0 * hits / sym->hist_sum;
740 color = get_percent_color(percent);
743 * Also color the filename and line if needed, with
744 * the same color than the percentage. Don't print it
745 * twice for close colored ip with the same filename:line
748 if (!prev_line || strcmp(prev_line, path)
749 || color != prev_color) {
750 color_fprintf(stdout, color, " %s", path);
756 color_fprintf(stdout, color, " %7.2f", percent);
758 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", line);
763 printf(" : %s\n", line);
769 static struct rb_root root_sym_ext;
771 static void insert_source_line(struct sym_ext *sym_ext)
773 struct sym_ext *iter;
774 struct rb_node **p = &root_sym_ext.rb_node;
775 struct rb_node *parent = NULL;
779 iter = rb_entry(parent, struct sym_ext, node);
781 if (sym_ext->percent > iter->percent)
787 rb_link_node(&sym_ext->node, parent, p);
788 rb_insert_color(&sym_ext->node, &root_sym_ext);
791 static void free_source_line(struct symbol *sym, int len)
793 struct sym_ext *sym_ext = sym->priv;
799 for (i = 0; i < len; i++)
800 free(sym_ext[i].path);
804 root_sym_ext = RB_ROOT;
807 /* Get the filename:line for the colored entries */
809 get_source_line(struct symbol *sym, u64 start, int len, const char *filename)
812 char cmd[PATH_MAX * 2];
813 struct sym_ext *sym_ext;
818 sym->priv = calloc(len, sizeof(struct sym_ext));
824 for (i = 0; i < len; i++) {
830 sym_ext[i].percent = 100.0 * sym->hist[i] / sym->hist_sum;
831 if (sym_ext[i].percent <= 0.5)
835 sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
836 fp = popen(cmd, "r");
840 if (getline(&path, &line_len, fp) < 0 || !line_len)
843 sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
844 if (!sym_ext[i].path)
847 strcpy(sym_ext[i].path, path);
848 insert_source_line(&sym_ext[i]);
855 static void print_summary(const char *filename)
857 struct sym_ext *sym_ext;
858 struct rb_node *node;
860 printf("\nSorted summary for file %s\n", filename);
861 printf("----------------------------------------------\n\n");
863 if (RB_EMPTY_ROOT(&root_sym_ext)) {
864 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
868 node = rb_first(&root_sym_ext);
874 sym_ext = rb_entry(node, struct sym_ext, node);
875 percent = sym_ext->percent;
876 color = get_percent_color(percent);
877 path = sym_ext->path;
879 color_fprintf(stdout, color, " %7.2f %s", percent, path);
880 node = rb_next(node);
884 static void annotate_sym(struct dso *dso, struct symbol *sym)
886 const char *filename = dso->name, *d_filename;
888 char command[PATH_MAX*2];
894 filename = sym->module->path;
895 else if (dso == kernel_dso)
896 filename = vmlinux_name;
898 start = sym->obj_start;
902 d_filename = filename;
904 d_filename = basename(filename);
906 end = start + sym->end - sym->start + 1;
907 len = sym->end - sym->start;
910 get_source_line(sym, start, len, filename);
911 print_summary(filename);
914 printf("\n\n------------------------------------------------\n");
915 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
916 printf("------------------------------------------------\n");
919 printf("annotating [%p] %30s : [%p] %30s\n", dso, dso->name, sym, sym->name);
921 sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
922 (u64)start, (u64)end, filename, filename);
925 printf("doing: %s\n", command);
927 file = popen(command, "r");
931 while (!feof(file)) {
932 if (parse_line(file, sym, start, len) < 0)
938 free_source_line(sym, len);
941 static void find_annotations(void)
947 list_for_each_entry(dso, &dsos, node) {
949 for (nd = rb_first(&dso->syms); nd; nd = rb_next(nd)) {
950 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
953 annotate_sym(dso, sym);
960 printf(" Error: symbol '%s' not present amongst the samples.\n", sym_hist_filter);
963 static int __cmd_annotate(void)
965 int ret, rc = EXIT_FAILURE;
966 unsigned long offset = 0;
967 unsigned long head = 0;
968 struct stat input_stat;
973 register_idle_thread();
975 input = open(input_name, O_RDONLY);
977 perror("failed to open file");
981 ret = fstat(input, &input_stat);
983 perror("failed to stat file");
987 if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
988 fprintf(stderr, "file: %s not owned by current user or root\n", input_name);
992 if (!input_stat.st_size) {
993 fprintf(stderr, "zero-sized file, nothing to do!\n");
997 if (load_kernel() < 0) {
998 perror("failed to load kernel symbols");
1003 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1004 MAP_SHARED, input, offset);
1005 if (buf == MAP_FAILED) {
1006 perror("failed to mmap file");
1011 event = (event_t *)(buf + head);
1013 size = event->header.size;
1017 if (head + event->header.size >= page_size * mmap_window) {
1018 unsigned long shift = page_size * (head / page_size);
1021 munmap_ret = munmap(buf, page_size * mmap_window);
1022 assert(munmap_ret == 0);
1029 size = event->header.size;
1031 dump_printf("%p [%p]: event: %d\n",
1032 (void *)(offset + head),
1033 (void *)(long)event->header.size,
1034 event->header.type);
1036 if (!size || process_event(event, offset, head) < 0) {
1038 dump_printf("%p [%p]: skipping unknown header type: %d\n",
1039 (void *)(offset + head),
1040 (void *)(long)(event->header.size),
1041 event->header.type);
1046 * assume we lost track of the stream, check alignment, and
1047 * increment a single u64 in the hope to catch on again 'soon'.
1050 if (unlikely(head & 7))
1058 if (offset + head < (unsigned long)input_stat.st_size)
1064 dump_printf(" IP events: %10ld\n", total);
1065 dump_printf(" mmap events: %10ld\n", total_mmap);
1066 dump_printf(" comm events: %10ld\n", total_comm);
1067 dump_printf(" fork events: %10ld\n", total_fork);
1068 dump_printf(" unknown events: %10ld\n", total_unknown);
1074 threads__fprintf(stdout, &threads);
1077 dsos__fprintf(stdout);
1087 static const char * const annotate_usage[] = {
1088 "perf annotate [<options>] <command>",
1092 static const struct option options[] = {
1093 OPT_STRING('i', "input", &input_name, "file",
1095 OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
1096 "symbol to annotate"),
1097 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
1098 OPT_BOOLEAN('v', "verbose", &verbose,
1099 "be more verbose (show symbol address, etc)"),
1100 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1101 "dump raw trace in ASCII"),
1102 OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
1103 OPT_BOOLEAN('m', "modules", &modules,
1104 "load module symbols - WARNING: use only with -k and LIVE kernel"),
1105 OPT_BOOLEAN('l', "print-line", &print_line,
1106 "print matching source lines (may be slow)"),
1107 OPT_BOOLEAN('P', "full-paths", &full_paths,
1108 "Don't shorten the displayed pathnames"),
1112 static void setup_sorting(void)
1114 char *tmp, *tok, *str = strdup(sort_order);
1116 for (tok = strtok_r(str, ", ", &tmp);
1117 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1118 if (sort_dimension__add(tok) < 0) {
1119 error("Unknown --sort key: `%s'", tok);
1120 usage_with_options(annotate_usage, options);
1127 int cmd_annotate(int argc, const char **argv, const char *prefix __used)
1131 page_size = getpagesize();
1133 argc = parse_options(argc, argv, options, annotate_usage, 0);
1139 * Special case: if there's an argument left then assume tha
1140 * it's a symbol filter:
1143 usage_with_options(annotate_usage, options);
1145 sym_hist_filter = argv[0];
1148 if (!sym_hist_filter)
1149 usage_with_options(annotate_usage, options);
1153 return __cmd_annotate();