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 *);
85 static int64_t cmp_null(void *l, void *r)
98 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
100 return right->thread->pid - left->thread->pid;
104 sort__thread_print(FILE *fp, struct hist_entry *self)
106 return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
109 static struct sort_entry sort_thread = {
110 .header = " Command: Pid",
111 .cmp = sort__thread_cmp,
112 .print = sort__thread_print,
118 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
120 return right->thread->pid - left->thread->pid;
124 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
126 char *comm_l = left->thread->comm;
127 char *comm_r = right->thread->comm;
129 if (!comm_l || !comm_r)
130 return cmp_null(comm_l, comm_r);
132 return strcmp(comm_l, comm_r);
136 sort__comm_print(FILE *fp, struct hist_entry *self)
138 return fprintf(fp, "%16s", self->thread->comm);
141 static struct sort_entry sort_comm = {
142 .header = " Command",
143 .cmp = sort__comm_cmp,
144 .collapse = sort__comm_collapse,
145 .print = sort__comm_print,
151 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
153 struct dso *dso_l = left->dso;
154 struct dso *dso_r = right->dso;
156 if (!dso_l || !dso_r)
157 return cmp_null(dso_l, dso_r);
159 return strcmp(dso_l->name, dso_r->name);
163 sort__dso_print(FILE *fp, struct hist_entry *self)
166 return fprintf(fp, "%-25s", self->dso->name);
168 return fprintf(fp, "%016llx ", (u64)self->ip);
171 static struct sort_entry sort_dso = {
172 .header = "Shared Object ",
173 .cmp = sort__dso_cmp,
174 .print = sort__dso_print,
180 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
184 if (left->sym == right->sym)
187 ip_l = left->sym ? left->sym->start : left->ip;
188 ip_r = right->sym ? right->sym->start : right->ip;
190 return (int64_t)(ip_r - ip_l);
194 sort__sym_print(FILE *fp, struct hist_entry *self)
199 ret += fprintf(fp, "%#018llx ", (u64)self->ip);
202 ret += fprintf(fp, "[%c] %s",
203 self->dso == kernel_dso ? 'k' : '.', self->sym->name);
205 ret += fprintf(fp, "%#016llx", (u64)self->ip);
211 static struct sort_entry sort_sym = {
213 .cmp = sort__sym_cmp,
214 .print = sort__sym_print,
217 static int sort__need_collapse = 0;
219 struct sort_dimension {
221 struct sort_entry *entry;
225 static struct sort_dimension sort_dimensions[] = {
226 { .name = "pid", .entry = &sort_thread, },
227 { .name = "comm", .entry = &sort_comm, },
228 { .name = "dso", .entry = &sort_dso, },
229 { .name = "symbol", .entry = &sort_sym, },
232 static LIST_HEAD(hist_entry__sort_list);
234 static int sort_dimension__add(char *tok)
238 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
239 struct sort_dimension *sd = &sort_dimensions[i];
244 if (strncasecmp(tok, sd->name, strlen(tok)))
247 if (sd->entry->collapse)
248 sort__need_collapse = 1;
250 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
260 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
262 struct sort_entry *se;
265 list_for_each_entry(se, &hist_entry__sort_list, list) {
266 cmp = se->cmp(left, right);
275 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
277 struct sort_entry *se;
280 list_for_each_entry(se, &hist_entry__sort_list, list) {
281 int64_t (*f)(struct hist_entry *, struct hist_entry *);
283 f = se->collapse ?: se->cmp;
285 cmp = f(left, right);
294 * collect histogram counts
296 static void hist_hit(struct hist_entry *he, u64 ip)
298 unsigned int sym_size, offset;
299 struct symbol *sym = he->sym;
303 if (!sym || !sym->hist)
306 sym_size = sym->end - sym->start;
307 offset = ip - sym->start;
309 if (offset >= sym_size)
316 printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n",
317 (void *)(unsigned long)he->sym->start,
319 (void *)(unsigned long)ip, ip - he->sym->start,
324 hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
325 struct symbol *sym, u64 ip, char level)
327 struct rb_node **p = &hist.rb_node;
328 struct rb_node *parent = NULL;
329 struct hist_entry *he;
330 struct hist_entry entry = {
343 he = rb_entry(parent, struct hist_entry, rb_node);
345 cmp = hist_entry__cmp(&entry, he);
359 he = malloc(sizeof(*he));
363 rb_link_node(&he->rb_node, parent, p);
364 rb_insert_color(&he->rb_node, &hist);
369 static void hist_entry__free(struct hist_entry *he)
375 * collapse the histogram
378 static struct rb_root collapse_hists;
380 static void collapse__insert_entry(struct hist_entry *he)
382 struct rb_node **p = &collapse_hists.rb_node;
383 struct rb_node *parent = NULL;
384 struct hist_entry *iter;
389 iter = rb_entry(parent, struct hist_entry, rb_node);
391 cmp = hist_entry__collapse(iter, he);
394 iter->count += he->count;
395 hist_entry__free(he);
405 rb_link_node(&he->rb_node, parent, p);
406 rb_insert_color(&he->rb_node, &collapse_hists);
409 static void collapse__resort(void)
411 struct rb_node *next;
412 struct hist_entry *n;
414 if (!sort__need_collapse)
417 next = rb_first(&hist);
419 n = rb_entry(next, struct hist_entry, rb_node);
420 next = rb_next(&n->rb_node);
422 rb_erase(&n->rb_node, &hist);
423 collapse__insert_entry(n);
428 * reverse the map, sort on count.
431 static struct rb_root output_hists;
433 static void output__insert_entry(struct hist_entry *he)
435 struct rb_node **p = &output_hists.rb_node;
436 struct rb_node *parent = NULL;
437 struct hist_entry *iter;
441 iter = rb_entry(parent, struct hist_entry, rb_node);
443 if (he->count > iter->count)
449 rb_link_node(&he->rb_node, parent, p);
450 rb_insert_color(&he->rb_node, &output_hists);
453 static void output__resort(void)
455 struct rb_node *next;
456 struct hist_entry *n;
457 struct rb_root *tree = &hist;
459 if (sort__need_collapse)
460 tree = &collapse_hists;
462 next = rb_first(tree);
465 n = rb_entry(next, struct hist_entry, rb_node);
466 next = rb_next(&n->rb_node);
468 rb_erase(&n->rb_node, tree);
469 output__insert_entry(n);
473 static unsigned long total = 0,
480 process_sample_event(event_t *event, unsigned long offset, unsigned long head)
484 struct dso *dso = NULL;
485 struct thread *thread;
486 u64 ip = event->ip.ip;
487 struct map *map = NULL;
489 thread = threads__findnew(event->ip.pid, &threads, &last_match);
491 dump_printf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
492 (void *)(offset + head),
493 (void *)(long)(event->header.size),
498 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
500 if (thread == NULL) {
501 fprintf(stderr, "problem processing %d event, skipping it.\n",
506 if (event->header.misc & PERF_RECORD_MISC_KERNEL) {
512 dump_printf(" ...... dso: %s\n", dso->name);
514 } else if (event->header.misc & PERF_RECORD_MISC_USER) {
519 map = thread__find_map(thread, ip);
521 ip = map->map_ip(map, ip);
525 * If this is outside of all known maps,
526 * and is a negative address, try to look it
527 * up in the kernel dso, as it might be a
528 * vsyscall (which executes in user-mode):
530 if ((long long)ip < 0)
533 dump_printf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
538 dump_printf(" ...... dso: [hypervisor]\n");
541 if (show & show_mask) {
542 struct symbol *sym = NULL;
545 sym = dso->find_symbol(dso, ip);
547 if (hist_entry__add(thread, map, dso, sym, ip, level)) {
549 "problem incrementing symbol count, skipping event\n");
559 process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
561 struct thread *thread;
562 struct map *map = map__new(&event->mmap, NULL, 0);
564 thread = threads__findnew(event->mmap.pid, &threads, &last_match);
566 dump_printf("%p [%p]: PERF_RECORD_MMAP %d: [%p(%p) @ %p]: %s\n",
567 (void *)(offset + head),
568 (void *)(long)(event->header.size),
570 (void *)(long)event->mmap.start,
571 (void *)(long)event->mmap.len,
572 (void *)(long)event->mmap.pgoff,
573 event->mmap.filename);
575 if (thread == NULL || map == NULL) {
576 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
580 thread__insert_map(thread, map);
587 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
589 struct thread *thread;
591 thread = threads__findnew(event->comm.pid, &threads, &last_match);
592 dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
593 (void *)(offset + head),
594 (void *)(long)(event->header.size),
595 event->comm.comm, event->comm.pid);
597 if (thread == NULL ||
598 thread__set_comm(thread, event->comm.comm)) {
599 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
608 process_fork_event(event_t *event, unsigned long offset, unsigned long head)
610 struct thread *thread;
611 struct thread *parent;
613 thread = threads__findnew(event->fork.pid, &threads, &last_match);
614 parent = threads__findnew(event->fork.ppid, &threads, &last_match);
615 dump_printf("%p [%p]: PERF_RECORD_FORK: %d:%d\n",
616 (void *)(offset + head),
617 (void *)(long)(event->header.size),
618 event->fork.pid, event->fork.ppid);
621 * A thread clone will have the same PID for both
624 if (thread == parent)
627 if (!thread || !parent || thread__fork(thread, parent)) {
628 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
637 process_event(event_t *event, unsigned long offset, unsigned long head)
639 switch (event->header.type) {
640 case PERF_RECORD_SAMPLE:
641 return process_sample_event(event, offset, head);
643 case PERF_RECORD_MMAP:
644 return process_mmap_event(event, offset, head);
646 case PERF_RECORD_COMM:
647 return process_comm_event(event, offset, head);
649 case PERF_RECORD_FORK:
650 return process_fork_event(event, offset, head);
652 * We dont process them right now but they are fine:
655 case PERF_RECORD_THROTTLE:
656 case PERF_RECORD_UNTHROTTLE:
667 parse_line(FILE *file, struct symbol *sym, u64 start, u64 len)
669 char *line = NULL, *tmp, *tmp2;
670 static const char *prev_line;
671 static const char *prev_color;
678 if (getline(&line, &line_len, file) < 0)
683 c = strchr(line, '\n');
692 * Strip leading spaces:
703 * Parse hexa addresses followed by ':'
705 line_ip = strtoull(tmp, &tmp2, 16);
711 const char *path = NULL;
712 unsigned int hits = 0;
713 double percent = 0.0;
715 struct sym_ext *sym_ext = sym->priv;
717 offset = line_ip - start;
719 hits = sym->hist[offset];
721 if (offset < len && sym_ext) {
722 path = sym_ext[offset].path;
723 percent = sym_ext[offset].percent;
724 } else if (sym->hist_sum)
725 percent = 100.0 * hits / sym->hist_sum;
727 color = get_percent_color(percent);
730 * Also color the filename and line if needed, with
731 * the same color than the percentage. Don't print it
732 * twice for close colored ip with the same filename:line
735 if (!prev_line || strcmp(prev_line, path)
736 || color != prev_color) {
737 color_fprintf(stdout, color, " %s", path);
743 color_fprintf(stdout, color, " %7.2f", percent);
745 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", line);
750 printf(" : %s\n", line);
756 static struct rb_root root_sym_ext;
758 static void insert_source_line(struct sym_ext *sym_ext)
760 struct sym_ext *iter;
761 struct rb_node **p = &root_sym_ext.rb_node;
762 struct rb_node *parent = NULL;
766 iter = rb_entry(parent, struct sym_ext, node);
768 if (sym_ext->percent > iter->percent)
774 rb_link_node(&sym_ext->node, parent, p);
775 rb_insert_color(&sym_ext->node, &root_sym_ext);
778 static void free_source_line(struct symbol *sym, int len)
780 struct sym_ext *sym_ext = sym->priv;
786 for (i = 0; i < len; i++)
787 free(sym_ext[i].path);
791 root_sym_ext = RB_ROOT;
794 /* Get the filename:line for the colored entries */
796 get_source_line(struct symbol *sym, u64 start, int len, const char *filename)
799 char cmd[PATH_MAX * 2];
800 struct sym_ext *sym_ext;
805 sym->priv = calloc(len, sizeof(struct sym_ext));
811 for (i = 0; i < len; i++) {
817 sym_ext[i].percent = 100.0 * sym->hist[i] / sym->hist_sum;
818 if (sym_ext[i].percent <= 0.5)
822 sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
823 fp = popen(cmd, "r");
827 if (getline(&path, &line_len, fp) < 0 || !line_len)
830 sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
831 if (!sym_ext[i].path)
834 strcpy(sym_ext[i].path, path);
835 insert_source_line(&sym_ext[i]);
842 static void print_summary(const char *filename)
844 struct sym_ext *sym_ext;
845 struct rb_node *node;
847 printf("\nSorted summary for file %s\n", filename);
848 printf("----------------------------------------------\n\n");
850 if (RB_EMPTY_ROOT(&root_sym_ext)) {
851 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
855 node = rb_first(&root_sym_ext);
861 sym_ext = rb_entry(node, struct sym_ext, node);
862 percent = sym_ext->percent;
863 color = get_percent_color(percent);
864 path = sym_ext->path;
866 color_fprintf(stdout, color, " %7.2f %s", percent, path);
867 node = rb_next(node);
871 static void annotate_sym(struct dso *dso, struct symbol *sym)
873 const char *filename = dso->name, *d_filename;
875 char command[PATH_MAX*2];
881 filename = sym->module->path;
882 else if (dso == kernel_dso)
883 filename = vmlinux_name;
885 start = sym->obj_start;
889 d_filename = filename;
891 d_filename = basename(filename);
893 end = start + sym->end - sym->start + 1;
894 len = sym->end - sym->start;
897 get_source_line(sym, start, len, filename);
898 print_summary(filename);
901 printf("\n\n------------------------------------------------\n");
902 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
903 printf("------------------------------------------------\n");
906 printf("annotating [%p] %30s : [%p] %30s\n", dso, dso->name, sym, sym->name);
908 sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
909 (u64)start, (u64)end, filename, filename);
912 printf("doing: %s\n", command);
914 file = popen(command, "r");
918 while (!feof(file)) {
919 if (parse_line(file, sym, start, len) < 0)
925 free_source_line(sym, len);
928 static void find_annotations(void)
934 list_for_each_entry(dso, &dsos, node) {
936 for (nd = rb_first(&dso->syms); nd; nd = rb_next(nd)) {
937 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
940 annotate_sym(dso, sym);
947 printf(" Error: symbol '%s' not present amongst the samples.\n", sym_hist_filter);
950 static int __cmd_annotate(void)
952 int ret, rc = EXIT_FAILURE;
953 unsigned long offset = 0;
954 unsigned long head = 0;
955 struct stat input_stat;
960 register_idle_thread(&threads, &last_match);
962 input = open(input_name, O_RDONLY);
964 perror("failed to open file");
968 ret = fstat(input, &input_stat);
970 perror("failed to stat file");
974 if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
975 fprintf(stderr, "file: %s not owned by current user or root\n", input_name);
979 if (!input_stat.st_size) {
980 fprintf(stderr, "zero-sized file, nothing to do!\n");
984 if (load_kernel() < 0) {
985 perror("failed to load kernel symbols");
990 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
991 MAP_SHARED, input, offset);
992 if (buf == MAP_FAILED) {
993 perror("failed to mmap file");
998 event = (event_t *)(buf + head);
1000 size = event->header.size;
1004 if (head + event->header.size >= page_size * mmap_window) {
1005 unsigned long shift = page_size * (head / page_size);
1008 munmap_ret = munmap(buf, page_size * mmap_window);
1009 assert(munmap_ret == 0);
1016 size = event->header.size;
1018 dump_printf("%p [%p]: event: %d\n",
1019 (void *)(offset + head),
1020 (void *)(long)event->header.size,
1021 event->header.type);
1023 if (!size || process_event(event, offset, head) < 0) {
1025 dump_printf("%p [%p]: skipping unknown header type: %d\n",
1026 (void *)(offset + head),
1027 (void *)(long)(event->header.size),
1028 event->header.type);
1033 * assume we lost track of the stream, check alignment, and
1034 * increment a single u64 in the hope to catch on again 'soon'.
1037 if (unlikely(head & 7))
1045 if (offset + head < (unsigned long)input_stat.st_size)
1051 dump_printf(" IP events: %10ld\n", total);
1052 dump_printf(" mmap events: %10ld\n", total_mmap);
1053 dump_printf(" comm events: %10ld\n", total_comm);
1054 dump_printf(" fork events: %10ld\n", total_fork);
1055 dump_printf(" unknown events: %10ld\n", total_unknown);
1061 threads__fprintf(stdout, &threads);
1064 dsos__fprintf(stdout);
1074 static const char * const annotate_usage[] = {
1075 "perf annotate [<options>] <command>",
1079 static const struct option options[] = {
1080 OPT_STRING('i', "input", &input_name, "file",
1082 OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
1083 "symbol to annotate"),
1084 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
1085 OPT_BOOLEAN('v', "verbose", &verbose,
1086 "be more verbose (show symbol address, etc)"),
1087 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1088 "dump raw trace in ASCII"),
1089 OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
1090 OPT_BOOLEAN('m', "modules", &modules,
1091 "load module symbols - WARNING: use only with -k and LIVE kernel"),
1092 OPT_BOOLEAN('l', "print-line", &print_line,
1093 "print matching source lines (may be slow)"),
1094 OPT_BOOLEAN('P', "full-paths", &full_paths,
1095 "Don't shorten the displayed pathnames"),
1099 static void setup_sorting(void)
1101 char *tmp, *tok, *str = strdup(sort_order);
1103 for (tok = strtok_r(str, ", ", &tmp);
1104 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1105 if (sort_dimension__add(tok) < 0) {
1106 error("Unknown --sort key: `%s'", tok);
1107 usage_with_options(annotate_usage, options);
1114 int cmd_annotate(int argc, const char **argv, const char *prefix __used)
1118 page_size = getpagesize();
1120 argc = parse_options(argc, argv, options, annotate_usage, 0);
1126 * Special case: if there's an argument left then assume tha
1127 * it's a symbol filter:
1130 usage_with_options(annotate_usage, options);
1132 sym_hist_filter = argv[0];
1135 if (!sym_hist_filter)
1136 usage_with_options(annotate_usage, options);
1140 return __cmd_annotate();