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"
25 #include "util/sort.h"
26 #include "util/hist.h"
28 static char const *input_name = "perf.data";
33 static int full_paths;
35 static int print_line;
37 static unsigned long page_size;
38 static unsigned long mmap_window = 32;
48 * collect histogram counts
50 static void hist_hit(struct hist_entry *he, u64 ip)
52 unsigned int sym_size, offset;
53 struct symbol *sym = he->sym;
57 if (!sym || !sym->hist)
60 sym_size = sym->end - sym->start;
61 offset = ip - sym->start;
64 fprintf(stderr, "%s: ip=%Lx\n", __func__,
65 he->map->unmap_ip(he->map, ip));
67 if (offset >= sym_size)
74 printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n",
75 (void *)(unsigned long)he->sym->start,
77 (void *)(unsigned long)ip, ip - he->sym->start,
81 static int hist_entry__add(struct thread *thread, struct map *map,
82 struct symbol *sym, u64 ip, u64 count, char level)
85 struct hist_entry *he = __hist_entry__add(thread, map, sym, NULL, ip,
94 process_sample_event(event_t *event, unsigned long offset, unsigned long head)
97 u64 ip = event->ip.ip;
98 struct map *map = NULL;
99 struct symbol *sym = NULL;
100 struct thread *thread = threads__findnew(event->ip.pid);
102 dump_printf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
103 (void *)(offset + head),
104 (void *)(long)(event->header.size),
109 if (thread == NULL) {
110 fprintf(stderr, "problem processing %d event, skipping it.\n",
115 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
117 if (event->header.misc & PERF_RECORD_MISC_KERNEL) {
119 sym = kernel_maps__find_symbol(ip, &map);
120 dump_printf(" ...... dso: %s\n",
121 map ? map->dso->long_name : "<not found>");
122 } else if (event->header.misc & PERF_RECORD_MISC_USER) {
124 map = thread__find_map(thread, ip);
127 ip = map->map_ip(map, ip);
128 sym = map->dso->find_symbol(map->dso, ip);
131 * If this is outside of all known maps,
132 * and is a negative address, try to look it
133 * up in the kernel dso, as it might be a
134 * vsyscall or vdso (which executes in user-mode).
136 * XXX This is nasty, we should have a symbol list in
137 * the "[vdso]" dso, but for now lets use the old
138 * trick of looking in the whole kernel symbol list.
140 if ((long long)ip < 0) {
145 dump_printf(" ...... dso: %s\n",
146 map ? map->dso->long_name : "<not found>");
149 dump_printf(" ...... dso: [hypervisor]\n");
152 if (hist_entry__add(thread, map, sym, ip, 1, level)) {
153 fprintf(stderr, "problem incrementing symbol count, "
163 process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
165 struct map *map = map__new(&event->mmap, NULL, 0);
166 struct thread *thread = threads__findnew(event->mmap.pid);
168 dump_printf("%p [%p]: PERF_RECORD_MMAP %d: [%p(%p) @ %p]: %s\n",
169 (void *)(offset + head),
170 (void *)(long)(event->header.size),
172 (void *)(long)event->mmap.start,
173 (void *)(long)event->mmap.len,
174 (void *)(long)event->mmap.pgoff,
175 event->mmap.filename);
177 if (thread == NULL || map == NULL) {
178 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
182 thread__insert_map(thread, map);
189 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
191 struct thread *thread = threads__findnew(event->comm.pid);
193 dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
194 (void *)(offset + head),
195 (void *)(long)(event->header.size),
196 event->comm.comm, event->comm.pid);
198 if (thread == NULL ||
199 thread__set_comm(thread, event->comm.comm)) {
200 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
209 process_fork_event(event_t *event, unsigned long offset, unsigned long head)
211 struct thread *thread = threads__findnew(event->fork.pid);
212 struct thread *parent = threads__findnew(event->fork.ppid);
214 dump_printf("%p [%p]: PERF_RECORD_FORK: %d:%d\n",
215 (void *)(offset + head),
216 (void *)(long)(event->header.size),
217 event->fork.pid, event->fork.ppid);
220 * A thread clone will have the same PID for both
223 if (thread == parent)
226 if (!thread || !parent || thread__fork(thread, parent)) {
227 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
236 process_event(event_t *event, unsigned long offset, unsigned long head)
238 switch (event->header.type) {
239 case PERF_RECORD_SAMPLE:
240 return process_sample_event(event, offset, head);
242 case PERF_RECORD_MMAP:
243 return process_mmap_event(event, offset, head);
245 case PERF_RECORD_COMM:
246 return process_comm_event(event, offset, head);
248 case PERF_RECORD_FORK:
249 return process_fork_event(event, offset, head);
251 * We dont process them right now but they are fine:
254 case PERF_RECORD_THROTTLE:
255 case PERF_RECORD_UNTHROTTLE:
265 static int parse_line(FILE *file, struct hist_entry *he, u64 len)
267 struct symbol *sym = he->sym;
268 char *line = NULL, *tmp, *tmp2;
269 static const char *prev_line;
270 static const char *prev_color;
278 if (getline(&line, &line_len, file) < 0)
283 c = strchr(line, '\n');
292 * Strip leading spaces:
303 * Parse hexa addresses followed by ':'
305 line_ip = strtoull(tmp, &tmp2, 16);
310 start = he->map->unmap_ip(he->map, sym->start);
313 const char *path = NULL;
314 unsigned int hits = 0;
315 double percent = 0.0;
317 struct sym_ext *sym_ext = sym->priv;
319 offset = line_ip - start;
321 hits = sym->hist[offset];
323 if (offset < len && sym_ext) {
324 path = sym_ext[offset].path;
325 percent = sym_ext[offset].percent;
326 } else if (sym->hist_sum)
327 percent = 100.0 * hits / sym->hist_sum;
329 color = get_percent_color(percent);
332 * Also color the filename and line if needed, with
333 * the same color than the percentage. Don't print it
334 * twice for close colored ip with the same filename:line
337 if (!prev_line || strcmp(prev_line, path)
338 || color != prev_color) {
339 color_fprintf(stdout, color, " %s", path);
345 color_fprintf(stdout, color, " %7.2f", percent);
347 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", line);
352 printf(" : %s\n", line);
358 static struct rb_root root_sym_ext;
360 static void insert_source_line(struct sym_ext *sym_ext)
362 struct sym_ext *iter;
363 struct rb_node **p = &root_sym_ext.rb_node;
364 struct rb_node *parent = NULL;
368 iter = rb_entry(parent, struct sym_ext, node);
370 if (sym_ext->percent > iter->percent)
376 rb_link_node(&sym_ext->node, parent, p);
377 rb_insert_color(&sym_ext->node, &root_sym_ext);
380 static void free_source_line(struct symbol *sym, int len)
382 struct sym_ext *sym_ext = sym->priv;
388 for (i = 0; i < len; i++)
389 free(sym_ext[i].path);
393 root_sym_ext = RB_ROOT;
396 /* Get the filename:line for the colored entries */
398 get_source_line(struct hist_entry *he, int len, const char *filename)
400 struct symbol *sym = he->sym;
403 char cmd[PATH_MAX * 2];
404 struct sym_ext *sym_ext;
409 sym->priv = calloc(len, sizeof(struct sym_ext));
414 start = he->map->unmap_ip(he->map, sym->start);
416 for (i = 0; i < len; i++) {
422 sym_ext[i].percent = 100.0 * sym->hist[i] / sym->hist_sum;
423 if (sym_ext[i].percent <= 0.5)
427 sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
428 fp = popen(cmd, "r");
432 if (getline(&path, &line_len, fp) < 0 || !line_len)
435 sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
436 if (!sym_ext[i].path)
439 strcpy(sym_ext[i].path, path);
440 insert_source_line(&sym_ext[i]);
447 static void print_summary(const char *filename)
449 struct sym_ext *sym_ext;
450 struct rb_node *node;
452 printf("\nSorted summary for file %s\n", filename);
453 printf("----------------------------------------------\n\n");
455 if (RB_EMPTY_ROOT(&root_sym_ext)) {
456 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
460 node = rb_first(&root_sym_ext);
466 sym_ext = rb_entry(node, struct sym_ext, node);
467 percent = sym_ext->percent;
468 color = get_percent_color(percent);
469 path = sym_ext->path;
471 color_fprintf(stdout, color, " %7.2f %s", percent, path);
472 node = rb_next(node);
476 static void annotate_sym(struct hist_entry *he)
478 struct map *map = he->map;
479 struct dso *dso = map->dso;
480 struct symbol *sym = he->sym;
481 const char *filename = dso->long_name, *d_filename;
483 char command[PATH_MAX*2];
490 fprintf(stderr, "%s: filename=%s, sym=%s, start=%Lx, end=%Lx\n",
491 __func__, filename, sym->name,
492 map->unmap_ip(map, sym->start),
493 map->unmap_ip(map, sym->end));
496 d_filename = filename;
498 d_filename = basename(filename);
500 len = sym->end - sym->start;
503 get_source_line(he, len, filename);
504 print_summary(filename);
507 printf("\n\n------------------------------------------------\n");
508 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
509 printf("------------------------------------------------\n");
512 printf("annotating [%p] %30s : [%p] %30s\n",
513 dso, dso->long_name, sym, sym->name);
515 sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
516 map->unmap_ip(map, sym->start), map->unmap_ip(map, sym->end),
520 printf("doing: %s\n", command);
522 file = popen(command, "r");
526 while (!feof(file)) {
527 if (parse_line(file, he, len) < 0)
533 free_source_line(sym, len);
536 static void find_annotations(void)
541 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
542 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
544 if (he->sym && he->sym->hist) {
548 * Since we have a hist_entry per IP for the same
549 * symbol, free he->sym->hist to signal we already
550 * processed this symbol.
553 he->sym->hist = NULL;
559 printf(" Error: symbol '%s' not present amongst the samples.\n", sym_hist_filter);
562 static int __cmd_annotate(void)
564 int ret, rc = EXIT_FAILURE;
565 unsigned long offset = 0;
566 unsigned long head = 0;
567 struct stat input_stat;
572 register_idle_thread();
574 input = open(input_name, O_RDONLY);
576 perror("failed to open file");
580 ret = fstat(input, &input_stat);
582 perror("failed to stat file");
586 if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
587 fprintf(stderr, "file: %s not owned by current user or root\n", input_name);
591 if (!input_stat.st_size) {
592 fprintf(stderr, "zero-sized file, nothing to do!\n");
596 if (load_kernel() < 0) {
597 perror("failed to load kernel symbols");
602 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
603 MAP_SHARED, input, offset);
604 if (buf == MAP_FAILED) {
605 perror("failed to mmap file");
610 event = (event_t *)(buf + head);
612 size = event->header.size;
616 if (head + event->header.size >= page_size * mmap_window) {
617 unsigned long shift = page_size * (head / page_size);
620 munmap_ret = munmap(buf, page_size * mmap_window);
621 assert(munmap_ret == 0);
628 size = event->header.size;
630 dump_printf("%p [%p]: event: %d\n",
631 (void *)(offset + head),
632 (void *)(long)event->header.size,
635 if (!size || process_event(event, offset, head) < 0) {
637 dump_printf("%p [%p]: skipping unknown header type: %d\n",
638 (void *)(offset + head),
639 (void *)(long)(event->header.size),
645 * assume we lost track of the stream, check alignment, and
646 * increment a single u64 in the hope to catch on again 'soon'.
649 if (unlikely(head & 7))
657 if (offset + head < (unsigned long)input_stat.st_size)
663 dump_printf(" IP events: %10ld\n", total);
664 dump_printf(" mmap events: %10ld\n", total_mmap);
665 dump_printf(" comm events: %10ld\n", total_comm);
666 dump_printf(" fork events: %10ld\n", total_fork);
667 dump_printf(" unknown events: %10ld\n", total_unknown);
673 threads__fprintf(stdout);
676 dsos__fprintf(stdout);
679 output__resort(total);
686 static const char * const annotate_usage[] = {
687 "perf annotate [<options>] <command>",
691 static const struct option options[] = {
692 OPT_STRING('i', "input", &input_name, "file",
694 OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
695 "symbol to annotate"),
696 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
697 OPT_BOOLEAN('v', "verbose", &verbose,
698 "be more verbose (show symbol address, etc)"),
699 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
700 "dump raw trace in ASCII"),
701 OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
702 OPT_BOOLEAN('m', "modules", &modules,
703 "load module symbols - WARNING: use only with -k and LIVE kernel"),
704 OPT_BOOLEAN('l', "print-line", &print_line,
705 "print matching source lines (may be slow)"),
706 OPT_BOOLEAN('P', "full-paths", &full_paths,
707 "Don't shorten the displayed pathnames"),
711 static void setup_sorting(void)
713 char *tmp, *tok, *str = strdup(sort_order);
715 for (tok = strtok_r(str, ", ", &tmp);
716 tok; tok = strtok_r(NULL, ", ", &tmp)) {
717 if (sort_dimension__add(tok) < 0) {
718 error("Unknown --sort key: `%s'", tok);
719 usage_with_options(annotate_usage, options);
726 int cmd_annotate(int argc, const char **argv, const char *prefix __used)
730 page_size = getpagesize();
732 argc = parse_options(argc, argv, options, annotate_usage, 0);
738 * Special case: if there's an argument left then assume tha
739 * it's a symbol filter:
742 usage_with_options(annotate_usage, options);
744 sym_hist_filter = argv[0];
747 if (!sym_hist_filter)
748 usage_with_options(annotate_usage, options);
752 if (field_sep && *field_sep == '.') {
753 fputs("'.' is the only non valid --field-separator argument\n",
758 return __cmd_annotate();