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 "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,symbol";
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)
42 static unsigned long page_size;
43 static unsigned long mmap_window = 32;
46 struct perf_event_header header;
52 struct perf_event_header header;
57 char filename[PATH_MAX];
61 struct perf_event_header header;
67 struct perf_event_header header;
72 struct perf_event_header header;
78 typedef union event_union {
79 struct perf_event_header header;
81 struct mmap_event mmap;
82 struct comm_event comm;
83 struct fork_event fork;
84 struct period_event period;
87 static LIST_HEAD(dsos);
88 static struct dso *kernel_dso;
89 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;
180 struct list_head node;
184 uint64_t (*map_ip)(struct map *, uint64_t);
188 static uint64_t map__map_ip(struct map *map, uint64_t ip)
190 return ip - map->start + map->pgoff;
193 static uint64_t vdso__map_ip(struct map *map, uint64_t ip)
198 static struct map *map__new(struct mmap_event *event)
200 struct map *self = malloc(sizeof(*self));
203 const char *filename = event->filename;
205 self->start = event->start;
206 self->end = event->start + event->len;
207 self->pgoff = event->pgoff;
209 self->dso = dsos__findnew(filename);
210 if (self->dso == NULL)
213 if (self->dso == vdso)
214 self->map_ip = vdso__map_ip;
216 self->map_ip = map__map_ip;
224 static struct map *map__clone(struct map *self)
226 struct map *map = malloc(sizeof(*self));
231 memcpy(map, self, sizeof(*self));
236 static int map__overlap(struct map *l, struct map *r)
238 if (l->start > r->start) {
244 if (l->end > r->start)
250 static size_t map__fprintf(struct map *self, FILE *fp)
252 return fprintf(fp, " %"PRIx64"-%"PRIx64" %"PRIx64" %s\n",
253 self->start, self->end, self->pgoff, self->dso->name);
258 struct rb_node rb_node;
259 struct list_head maps;
264 static struct thread *thread__new(pid_t pid)
266 struct thread *self = malloc(sizeof(*self));
270 self->comm = malloc(32);
272 snprintf(self->comm, 32, ":%d", self->pid);
273 INIT_LIST_HEAD(&self->maps);
279 static int thread__set_comm(struct thread *self, const char *comm)
283 self->comm = strdup(comm);
284 return self->comm ? 0 : -ENOMEM;
287 static size_t thread__fprintf(struct thread *self, FILE *fp)
290 size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
292 list_for_each_entry(pos, &self->maps, node)
293 ret += map__fprintf(pos, fp);
299 static struct rb_root threads;
300 static struct thread *last_match;
302 static struct thread *threads__findnew(pid_t pid)
304 struct rb_node **p = &threads.rb_node;
305 struct rb_node *parent = NULL;
309 * Font-end cache - PID lookups come in blocks,
310 * so most of the time we dont have to look up
313 if (last_match && last_match->pid == pid)
318 th = rb_entry(parent, struct thread, rb_node);
320 if (th->pid == pid) {
331 th = thread__new(pid);
333 rb_link_node(&th->rb_node, parent, p);
334 rb_insert_color(&th->rb_node, &threads);
341 static void thread__insert_map(struct thread *self, struct map *map)
343 struct map *pos, *tmp;
345 list_for_each_entry_safe(pos, tmp, &self->maps, node) {
346 if (map__overlap(pos, map)) {
347 list_del_init(&pos->node);
353 list_add_tail(&map->node, &self->maps);
356 static int thread__fork(struct thread *self, struct thread *parent)
362 self->comm = strdup(parent->comm);
366 list_for_each_entry(map, &parent->maps, node) {
367 struct map *new = map__clone(map);
370 thread__insert_map(self, new);
376 static struct map *thread__find_map(struct thread *self, uint64_t ip)
383 list_for_each_entry(pos, &self->maps, node)
384 if (ip >= pos->start && ip <= pos->end)
390 static size_t threads__fprintf(FILE *fp)
395 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
396 struct thread *pos = rb_entry(nd, struct thread, rb_node);
398 ret += thread__fprintf(pos, fp);
405 * histogram, sorted on item, collects counts
408 static struct rb_root hist;
411 struct rb_node rb_node;
413 struct thread *thread;
424 * configurable sorting bits
428 struct list_head list;
432 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
433 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
434 size_t (*print)(FILE *fp, struct hist_entry *);
440 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
442 return right->thread->pid - left->thread->pid;
446 sort__thread_print(FILE *fp, struct hist_entry *self)
448 return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
451 static struct sort_entry sort_thread = {
452 .header = " Command: Pid",
453 .cmp = sort__thread_cmp,
454 .print = sort__thread_print,
460 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
462 return right->thread->pid - left->thread->pid;
466 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
468 char *comm_l = left->thread->comm;
469 char *comm_r = right->thread->comm;
471 if (!comm_l || !comm_r) {
472 if (!comm_l && !comm_r)
480 return strcmp(comm_l, comm_r);
484 sort__comm_print(FILE *fp, struct hist_entry *self)
486 return fprintf(fp, "%16s", self->thread->comm);
489 static struct sort_entry sort_comm = {
490 .header = " Command",
491 .cmp = sort__comm_cmp,
492 .collapse = sort__comm_collapse,
493 .print = sort__comm_print,
499 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
501 struct dso *dso_l = left->dso;
502 struct dso *dso_r = right->dso;
504 if (!dso_l || !dso_r) {
505 if (!dso_l && !dso_r)
513 return strcmp(dso_l->name, dso_r->name);
517 sort__dso_print(FILE *fp, struct hist_entry *self)
520 return fprintf(fp, "%-25s", self->dso->name);
522 return fprintf(fp, "%016llx ", (__u64)self->ip);
525 static struct sort_entry sort_dso = {
526 .header = "Shared Object ",
527 .cmp = sort__dso_cmp,
528 .print = sort__dso_print,
534 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
538 if (left->sym == right->sym)
541 ip_l = left->sym ? left->sym->start : left->ip;
542 ip_r = right->sym ? right->sym->start : right->ip;
544 return (int64_t)(ip_r - ip_l);
548 sort__sym_print(FILE *fp, struct hist_entry *self)
553 ret += fprintf(fp, "%#018llx ", (__u64)self->ip);
556 ret += fprintf(fp, "[%c] %s",
557 self->dso == kernel_dso ? 'k' : '.', self->sym->name);
559 ret += fprintf(fp, "%#016llx", (__u64)self->ip);
565 static struct sort_entry sort_sym = {
567 .cmp = sort__sym_cmp,
568 .print = sort__sym_print,
571 static int sort__need_collapse = 0;
573 struct sort_dimension {
575 struct sort_entry *entry;
579 static struct sort_dimension sort_dimensions[] = {
580 { .name = "pid", .entry = &sort_thread, },
581 { .name = "comm", .entry = &sort_comm, },
582 { .name = "dso", .entry = &sort_dso, },
583 { .name = "symbol", .entry = &sort_sym, },
586 static LIST_HEAD(hist_entry__sort_list);
588 static int sort_dimension__add(char *tok)
592 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
593 struct sort_dimension *sd = &sort_dimensions[i];
598 if (strncasecmp(tok, sd->name, strlen(tok)))
601 if (sd->entry->collapse)
602 sort__need_collapse = 1;
604 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
614 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
616 struct sort_entry *se;
619 list_for_each_entry(se, &hist_entry__sort_list, list) {
620 cmp = se->cmp(left, right);
629 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
631 struct sort_entry *se;
634 list_for_each_entry(se, &hist_entry__sort_list, list) {
635 int64_t (*f)(struct hist_entry *, struct hist_entry *);
637 f = se->collapse ?: se->cmp;
639 cmp = f(left, right);
648 * collect histogram counts
650 static void hist_hit(struct hist_entry *he, uint64_t ip)
652 unsigned int sym_size, offset;
653 struct symbol *sym = he->sym;
657 if (!sym || !sym->hist)
660 sym_size = sym->end - sym->start;
661 offset = ip - sym->start;
663 if (offset >= sym_size)
670 printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n",
671 (void *)he->sym->start,
673 (void *)ip, ip - he->sym->start,
678 hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
679 struct symbol *sym, uint64_t ip, char level)
681 struct rb_node **p = &hist.rb_node;
682 struct rb_node *parent = NULL;
683 struct hist_entry *he;
684 struct hist_entry entry = {
697 he = rb_entry(parent, struct hist_entry, rb_node);
699 cmp = hist_entry__cmp(&entry, he);
713 he = malloc(sizeof(*he));
717 rb_link_node(&he->rb_node, parent, p);
718 rb_insert_color(&he->rb_node, &hist);
723 static void hist_entry__free(struct hist_entry *he)
729 * collapse the histogram
732 static struct rb_root collapse_hists;
734 static void collapse__insert_entry(struct hist_entry *he)
736 struct rb_node **p = &collapse_hists.rb_node;
737 struct rb_node *parent = NULL;
738 struct hist_entry *iter;
743 iter = rb_entry(parent, struct hist_entry, rb_node);
745 cmp = hist_entry__collapse(iter, he);
748 iter->count += he->count;
749 hist_entry__free(he);
759 rb_link_node(&he->rb_node, parent, p);
760 rb_insert_color(&he->rb_node, &collapse_hists);
763 static void collapse__resort(void)
765 struct rb_node *next;
766 struct hist_entry *n;
768 if (!sort__need_collapse)
771 next = rb_first(&hist);
773 n = rb_entry(next, struct hist_entry, rb_node);
774 next = rb_next(&n->rb_node);
776 rb_erase(&n->rb_node, &hist);
777 collapse__insert_entry(n);
782 * reverse the map, sort on count.
785 static struct rb_root output_hists;
787 static void output__insert_entry(struct hist_entry *he)
789 struct rb_node **p = &output_hists.rb_node;
790 struct rb_node *parent = NULL;
791 struct hist_entry *iter;
795 iter = rb_entry(parent, struct hist_entry, rb_node);
797 if (he->count > iter->count)
803 rb_link_node(&he->rb_node, parent, p);
804 rb_insert_color(&he->rb_node, &output_hists);
807 static void output__resort(void)
809 struct rb_node *next;
810 struct hist_entry *n;
811 struct rb_root *tree = &hist;
813 if (sort__need_collapse)
814 tree = &collapse_hists;
816 next = rb_first(tree);
819 n = rb_entry(next, struct hist_entry, rb_node);
820 next = rb_next(&n->rb_node);
822 rb_erase(&n->rb_node, tree);
823 output__insert_entry(n);
827 static void register_idle_thread(void)
829 struct thread *thread = threads__findnew(0);
831 if (thread == NULL ||
832 thread__set_comm(thread, "[idle]")) {
833 fprintf(stderr, "problem inserting idle task.\n");
838 static unsigned long total = 0,
845 process_overflow_event(event_t *event, unsigned long offset, unsigned long head)
849 struct dso *dso = NULL;
850 struct thread *thread = threads__findnew(event->ip.pid);
851 uint64_t ip = event->ip.ip;
852 struct map *map = NULL;
854 dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
855 (void *)(offset + head),
856 (void *)(long)(event->header.size),
861 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
863 if (thread == NULL) {
864 fprintf(stderr, "problem processing %d event, skipping it.\n",
869 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
875 dprintf(" ...... dso: %s\n", dso->name);
877 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
882 map = thread__find_map(thread, ip);
884 ip = map->map_ip(map, ip);
888 * If this is outside of all known maps,
889 * and is a negative address, try to look it
890 * up in the kernel dso, as it might be a
891 * vsyscall (which executes in user-mode):
893 if ((long long)ip < 0)
896 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
901 dprintf(" ...... dso: [hypervisor]\n");
904 if (show & show_mask) {
905 struct symbol *sym = NULL;
908 sym = dso->find_symbol(dso, ip);
910 if (hist_entry__add(thread, map, dso, sym, ip, level)) {
912 "problem incrementing symbol count, skipping event\n");
922 process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
924 struct thread *thread = threads__findnew(event->mmap.pid);
925 struct map *map = map__new(&event->mmap);
927 dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
928 (void *)(offset + head),
929 (void *)(long)(event->header.size),
931 (void *)(long)event->mmap.start,
932 (void *)(long)event->mmap.len,
933 (void *)(long)event->mmap.pgoff,
934 event->mmap.filename);
936 if (thread == NULL || map == NULL) {
937 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
941 thread__insert_map(thread, map);
948 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
950 struct thread *thread = threads__findnew(event->comm.pid);
952 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
953 (void *)(offset + head),
954 (void *)(long)(event->header.size),
955 event->comm.comm, event->comm.pid);
957 if (thread == NULL ||
958 thread__set_comm(thread, event->comm.comm)) {
959 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
968 process_fork_event(event_t *event, unsigned long offset, unsigned long head)
970 struct thread *thread = threads__findnew(event->fork.pid);
971 struct thread *parent = threads__findnew(event->fork.ppid);
973 dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
974 (void *)(offset + head),
975 (void *)(long)(event->header.size),
976 event->fork.pid, event->fork.ppid);
978 if (!thread || !parent || thread__fork(thread, parent)) {
979 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
988 process_period_event(event_t *event, unsigned long offset, unsigned long head)
990 dprintf("%p [%p]: PERF_EVENT_PERIOD: time:%Ld, id:%Ld: period:%Ld\n",
991 (void *)(offset + head),
992 (void *)(long)(event->header.size),
995 event->period.sample_period);
1001 process_event(event_t *event, unsigned long offset, unsigned long head)
1003 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW)
1004 return process_overflow_event(event, offset, head);
1006 switch (event->header.type) {
1007 case PERF_EVENT_MMAP:
1008 return process_mmap_event(event, offset, head);
1010 case PERF_EVENT_COMM:
1011 return process_comm_event(event, offset, head);
1013 case PERF_EVENT_FORK:
1014 return process_fork_event(event, offset, head);
1016 case PERF_EVENT_PERIOD:
1017 return process_period_event(event, offset, head);
1019 * We dont process them right now but they are fine:
1022 case PERF_EVENT_THROTTLE:
1023 case PERF_EVENT_UNTHROTTLE:
1034 parse_line(FILE *file, struct symbol *sym, uint64_t start, uint64_t len)
1036 char *line = NULL, *tmp, *tmp2;
1037 unsigned int offset;
1043 if (getline(&line, &line_len, file) < 0)
1048 c = strchr(line, '\n');
1057 * Strip leading spaces:
1068 * Parse hexa addresses followed by ':'
1070 line_ip = strtoull(tmp, &tmp2, 16);
1075 if (line_ip != -1) {
1076 unsigned int hits = 0;
1077 double percent = 0.0;
1078 char *color = PERF_COLOR_NORMAL;
1080 offset = line_ip - start;
1082 hits = sym->hist[offset];
1085 percent = 100.0 * hits / sym->hist_sum;
1088 * We color high-overhead entries in red, low-overhead
1089 * entries in green - and keep the middle ground normal:
1092 color = PERF_COLOR_RED;
1095 color = PERF_COLOR_GREEN;
1098 color_fprintf(stdout, color, " %7.2f", percent);
1100 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", line);
1105 printf(" : %s\n", line);
1111 static void annotate_sym(struct dso *dso, struct symbol *sym)
1113 char *filename = dso->name;
1114 uint64_t start, end, len;
1115 char command[PATH_MAX*2];
1120 if (dso == kernel_dso)
1123 printf("\n------------------------------------------------\n");
1124 printf(" Percent | Source code & Disassembly of %s\n", filename);
1125 printf("------------------------------------------------\n");
1128 printf("annotating [%p] %30s : [%p] %30s\n", dso, dso->name, sym, sym->name);
1130 start = sym->obj_start;
1134 end = start + sym->end - sym->start + 1;
1135 len = sym->end - sym->start;
1137 sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s", (__u64)start, (__u64)end, filename);
1140 printf("doing: %s\n", command);
1142 file = popen(command, "r");
1146 while (!feof(file)) {
1147 if (parse_line(file, sym, start, len) < 0)
1154 static void find_annotations(void)
1160 list_for_each_entry(dso, &dsos, node) {
1162 for (nd = rb_first(&dso->syms); nd; nd = rb_next(nd)) {
1163 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
1166 annotate_sym(dso, sym);
1173 printf(" Error: symbol '%s' not present amongst the samples.\n", sym_hist_filter);
1176 static int __cmd_annotate(void)
1178 int ret, rc = EXIT_FAILURE;
1179 unsigned long offset = 0;
1180 unsigned long head = 0;
1186 register_idle_thread();
1188 input = open(input_name, O_RDONLY);
1190 perror("failed to open file");
1194 ret = fstat(input, &stat);
1196 perror("failed to stat file");
1200 if (!stat.st_size) {
1201 fprintf(stderr, "zero-sized file, nothing to do!\n");
1205 if (load_kernel() < 0) {
1206 perror("failed to load kernel symbols");
1207 return EXIT_FAILURE;
1211 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1212 MAP_SHARED, input, offset);
1213 if (buf == MAP_FAILED) {
1214 perror("failed to mmap file");
1219 event = (event_t *)(buf + head);
1221 size = event->header.size;
1225 if (head + event->header.size >= page_size * mmap_window) {
1226 unsigned long shift = page_size * (head / page_size);
1229 ret = munmap(buf, page_size * mmap_window);
1237 size = event->header.size;
1239 dprintf("%p [%p]: event: %d\n",
1240 (void *)(offset + head),
1241 (void *)(long)event->header.size,
1242 event->header.type);
1244 if (!size || process_event(event, offset, head) < 0) {
1246 dprintf("%p [%p]: skipping unknown header type: %d\n",
1247 (void *)(offset + head),
1248 (void *)(long)(event->header.size),
1249 event->header.type);
1254 * assume we lost track of the stream, check alignment, and
1255 * increment a single u64 in the hope to catch on again 'soon'.
1258 if (unlikely(head & 7))
1266 if (offset + head < stat.st_size)
1272 dprintf(" IP events: %10ld\n", total);
1273 dprintf(" mmap events: %10ld\n", total_mmap);
1274 dprintf(" comm events: %10ld\n", total_comm);
1275 dprintf(" fork events: %10ld\n", total_fork);
1276 dprintf(" unknown events: %10ld\n", total_unknown);
1282 threads__fprintf(stdout);
1285 dsos__fprintf(stdout);
1295 static const char * const annotate_usage[] = {
1296 "perf annotate [<options>] <command>",
1300 static const struct option options[] = {
1301 OPT_STRING('i', "input", &input_name, "file",
1303 OPT_STRING('s', "symbol", &sym_hist_filter, "file",
1304 "symbol to annotate"),
1305 OPT_BOOLEAN('v', "verbose", &verbose,
1306 "be more verbose (show symbol address, etc)"),
1307 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1308 "dump raw trace in ASCII"),
1309 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
1313 static void setup_sorting(void)
1315 char *tmp, *tok, *str = strdup(sort_order);
1317 for (tok = strtok_r(str, ", ", &tmp);
1318 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1319 if (sort_dimension__add(tok) < 0) {
1320 error("Unknown --sort key: `%s'", tok);
1321 usage_with_options(annotate_usage, options);
1328 int cmd_annotate(int argc, const char **argv, const char *prefix)
1332 page_size = getpagesize();
1334 argc = parse_options(argc, argv, options, annotate_usage, 0);
1340 * Special case: if there's an argument left then assume tha
1341 * it's a symbol filter:
1344 usage_with_options(annotate_usage, options);
1346 sym_hist_filter = argv[0];
1349 if (!sym_hist_filter)
1350 usage_with_options(annotate_usage, options);
1354 return __cmd_annotate();