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 ip
= he
->map
->map_ip(he
->map
, ip
);
62 offset
= ip
- sym
->start
;
64 if (offset
>= sym_size
)
71 printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n",
72 (void *)(unsigned long)he
->sym
->start
,
74 (void *)(unsigned long)ip
, ip
- he
->sym
->start
,
78 static int hist_entry__add(struct thread
*thread
, struct map
*map
,
79 struct symbol
*sym
, u64 ip
, u64 count
, char level
)
82 struct hist_entry
*he
= __hist_entry__add(thread
, map
, sym
, NULL
, ip
,
92 process_sample_event(event_t
*event
, unsigned long offset
, unsigned long head
)
95 u64 ip
= event
->ip
.ip
;
96 struct map
*map
= NULL
;
97 struct symbol
*sym
= NULL
;
98 struct thread
*thread
= threads__findnew(event
->ip
.pid
);
100 dump_printf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
101 (void *)(offset
+ head
),
102 (void *)(long)(event
->header
.size
),
107 if (thread
== NULL
) {
108 fprintf(stderr
, "problem processing %d event, skipping it.\n",
113 dump_printf(" ... thread: %s:%d\n", thread
->comm
, thread
->pid
);
115 if (event
->header
.misc
& PERF_RECORD_MISC_KERNEL
) {
117 sym
= kernel_maps__find_symbol(ip
, &map
);
118 dump_printf(" ...... dso: %s\n",
119 map
? map
->dso
->long_name
: "<not found>");
120 } else if (event
->header
.misc
& PERF_RECORD_MISC_USER
) {
122 map
= thread__find_map(thread
, ip
);
125 ip
= map
->map_ip(map
, ip
);
126 sym
= map
->dso
->find_symbol(map
->dso
, ip
);
129 * If this is outside of all known maps,
130 * and is a negative address, try to look it
131 * up in the kernel dso, as it might be a
132 * vsyscall or vdso (which executes in user-mode).
134 * XXX This is nasty, we should have a symbol list in
135 * the "[vdso]" dso, but for now lets use the old
136 * trick of looking in the whole kernel symbol list.
138 if ((long long)ip
< 0) {
143 dump_printf(" ...... dso: %s\n",
144 map
? map
->dso
->long_name
: "<not found>");
147 dump_printf(" ...... dso: [hypervisor]\n");
150 if (hist_entry__add(thread
, map
, sym
, ip
, 1, level
)) {
151 fprintf(stderr
, "problem incrementing symbol count, "
161 process_mmap_event(event_t
*event
, unsigned long offset
, unsigned long head
)
163 struct map
*map
= map__new(&event
->mmap
, NULL
, 0);
164 struct thread
*thread
= threads__findnew(event
->mmap
.pid
);
166 dump_printf("%p [%p]: PERF_RECORD_MMAP %d: [%p(%p) @ %p]: %s\n",
167 (void *)(offset
+ head
),
168 (void *)(long)(event
->header
.size
),
170 (void *)(long)event
->mmap
.start
,
171 (void *)(long)event
->mmap
.len
,
172 (void *)(long)event
->mmap
.pgoff
,
173 event
->mmap
.filename
);
175 if (thread
== NULL
|| map
== NULL
) {
176 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
180 thread__insert_map(thread
, map
);
187 process_comm_event(event_t
*event
, unsigned long offset
, unsigned long head
)
189 struct thread
*thread
= threads__findnew(event
->comm
.pid
);
191 dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
192 (void *)(offset
+ head
),
193 (void *)(long)(event
->header
.size
),
194 event
->comm
.comm
, event
->comm
.pid
);
196 if (thread
== NULL
||
197 thread__set_comm(thread
, event
->comm
.comm
)) {
198 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
207 process_fork_event(event_t
*event
, unsigned long offset
, unsigned long head
)
209 struct thread
*thread
= threads__findnew(event
->fork
.pid
);
210 struct thread
*parent
= threads__findnew(event
->fork
.ppid
);
212 dump_printf("%p [%p]: PERF_RECORD_FORK: %d:%d\n",
213 (void *)(offset
+ head
),
214 (void *)(long)(event
->header
.size
),
215 event
->fork
.pid
, event
->fork
.ppid
);
218 * A thread clone will have the same PID for both
221 if (thread
== parent
)
224 if (!thread
|| !parent
|| thread__fork(thread
, parent
)) {
225 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
234 process_event(event_t
*event
, unsigned long offset
, unsigned long head
)
236 switch (event
->header
.type
) {
237 case PERF_RECORD_SAMPLE
:
238 return process_sample_event(event
, offset
, head
);
240 case PERF_RECORD_MMAP
:
241 return process_mmap_event(event
, offset
, head
);
243 case PERF_RECORD_COMM
:
244 return process_comm_event(event
, offset
, head
);
246 case PERF_RECORD_FORK
:
247 return process_fork_event(event
, offset
, head
);
249 * We dont process them right now but they are fine:
252 case PERF_RECORD_THROTTLE
:
253 case PERF_RECORD_UNTHROTTLE
:
264 parse_line(FILE *file
, struct symbol
*sym
, u64 len
)
266 char *line
= NULL
, *tmp
, *tmp2
;
267 static const char *prev_line
;
268 static const char *prev_color
;
275 if (getline(&line
, &line_len
, file
) < 0)
280 c
= strchr(line
, '\n');
289 * Strip leading spaces:
300 * Parse hexa addresses followed by ':'
302 line_ip
= strtoull(tmp
, &tmp2
, 16);
308 const char *path
= NULL
;
309 unsigned int hits
= 0;
310 double percent
= 0.0;
312 struct sym_ext
*sym_ext
= sym
->priv
;
314 offset
= line_ip
- sym
->start
;
316 hits
= sym
->hist
[offset
];
318 if (offset
< len
&& sym_ext
) {
319 path
= sym_ext
[offset
].path
;
320 percent
= sym_ext
[offset
].percent
;
321 } else if (sym
->hist_sum
)
322 percent
= 100.0 * hits
/ sym
->hist_sum
;
324 color
= get_percent_color(percent
);
327 * Also color the filename and line if needed, with
328 * the same color than the percentage. Don't print it
329 * twice for close colored ip with the same filename:line
332 if (!prev_line
|| strcmp(prev_line
, path
)
333 || color
!= prev_color
) {
334 color_fprintf(stdout
, color
, " %s", path
);
340 color_fprintf(stdout
, color
, " %7.2f", percent
);
342 color_fprintf(stdout
, PERF_COLOR_BLUE
, "%s\n", line
);
347 printf(" : %s\n", line
);
353 static struct rb_root root_sym_ext
;
355 static void insert_source_line(struct sym_ext
*sym_ext
)
357 struct sym_ext
*iter
;
358 struct rb_node
**p
= &root_sym_ext
.rb_node
;
359 struct rb_node
*parent
= NULL
;
363 iter
= rb_entry(parent
, struct sym_ext
, node
);
365 if (sym_ext
->percent
> iter
->percent
)
371 rb_link_node(&sym_ext
->node
, parent
, p
);
372 rb_insert_color(&sym_ext
->node
, &root_sym_ext
);
375 static void free_source_line(struct symbol
*sym
, int len
)
377 struct sym_ext
*sym_ext
= sym
->priv
;
383 for (i
= 0; i
< len
; i
++)
384 free(sym_ext
[i
].path
);
388 root_sym_ext
= RB_ROOT
;
391 /* Get the filename:line for the colored entries */
393 get_source_line(struct symbol
*sym
, int len
, const char *filename
)
396 char cmd
[PATH_MAX
* 2];
397 struct sym_ext
*sym_ext
;
402 sym
->priv
= calloc(len
, sizeof(struct sym_ext
));
408 for (i
= 0; i
< len
; i
++) {
414 sym_ext
[i
].percent
= 100.0 * sym
->hist
[i
] / sym
->hist_sum
;
415 if (sym_ext
[i
].percent
<= 0.5)
418 offset
= sym
->start
+ i
;
419 sprintf(cmd
, "addr2line -e %s %016llx", filename
, offset
);
420 fp
= popen(cmd
, "r");
424 if (getline(&path
, &line_len
, fp
) < 0 || !line_len
)
427 sym_ext
[i
].path
= malloc(sizeof(char) * line_len
+ 1);
428 if (!sym_ext
[i
].path
)
431 strcpy(sym_ext
[i
].path
, path
);
432 insert_source_line(&sym_ext
[i
]);
439 static void print_summary(const char *filename
)
441 struct sym_ext
*sym_ext
;
442 struct rb_node
*node
;
444 printf("\nSorted summary for file %s\n", filename
);
445 printf("----------------------------------------------\n\n");
447 if (RB_EMPTY_ROOT(&root_sym_ext
)) {
448 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN
);
452 node
= rb_first(&root_sym_ext
);
458 sym_ext
= rb_entry(node
, struct sym_ext
, node
);
459 percent
= sym_ext
->percent
;
460 color
= get_percent_color(percent
);
461 path
= sym_ext
->path
;
463 color_fprintf(stdout
, color
, " %7.2f %s", percent
, path
);
464 node
= rb_next(node
);
468 static void annotate_sym(struct dso
*dso
, struct symbol
*sym
)
470 const char *filename
= dso
->long_name
, *d_filename
;
472 char command
[PATH_MAX
*2];
479 d_filename
= filename
;
481 d_filename
= basename(filename
);
483 len
= sym
->end
- sym
->start
;
486 get_source_line(sym
, len
, filename
);
487 print_summary(filename
);
490 printf("\n\n------------------------------------------------\n");
491 printf(" Percent | Source code & Disassembly of %s\n", d_filename
);
492 printf("------------------------------------------------\n");
495 printf("annotating [%p] %30s : [%p] %30s\n",
496 dso
, dso
->long_name
, sym
, sym
->name
);
498 sprintf(command
, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
499 sym
->start
, sym
->end
, filename
, filename
);
502 printf("doing: %s\n", command
);
504 file
= popen(command
, "r");
508 while (!feof(file
)) {
509 if (parse_line(file
, sym
, len
) < 0)
515 free_source_line(sym
, len
);
518 static void find_annotations(void)
524 list_for_each_entry(dso
, &dsos
, node
) {
526 for (nd
= rb_first(&dso
->syms
); nd
; nd
= rb_next(nd
)) {
527 struct symbol
*sym
= rb_entry(nd
, struct symbol
, rb_node
);
530 annotate_sym(dso
, sym
);
537 printf(" Error: symbol '%s' not present amongst the samples.\n", sym_hist_filter
);
540 static int __cmd_annotate(void)
542 int ret
, rc
= EXIT_FAILURE
;
543 unsigned long offset
= 0;
544 unsigned long head
= 0;
545 struct stat input_stat
;
550 register_idle_thread();
552 input
= open(input_name
, O_RDONLY
);
554 perror("failed to open file");
558 ret
= fstat(input
, &input_stat
);
560 perror("failed to stat file");
564 if (!force
&& input_stat
.st_uid
&& (input_stat
.st_uid
!= geteuid())) {
565 fprintf(stderr
, "file: %s not owned by current user or root\n", input_name
);
569 if (!input_stat
.st_size
) {
570 fprintf(stderr
, "zero-sized file, nothing to do!\n");
574 if (load_kernel() < 0) {
575 perror("failed to load kernel symbols");
580 buf
= (char *)mmap(NULL
, page_size
* mmap_window
, PROT_READ
,
581 MAP_SHARED
, input
, offset
);
582 if (buf
== MAP_FAILED
) {
583 perror("failed to mmap file");
588 event
= (event_t
*)(buf
+ head
);
590 size
= event
->header
.size
;
594 if (head
+ event
->header
.size
>= page_size
* mmap_window
) {
595 unsigned long shift
= page_size
* (head
/ page_size
);
598 munmap_ret
= munmap(buf
, page_size
* mmap_window
);
599 assert(munmap_ret
== 0);
606 size
= event
->header
.size
;
608 dump_printf("%p [%p]: event: %d\n",
609 (void *)(offset
+ head
),
610 (void *)(long)event
->header
.size
,
613 if (!size
|| process_event(event
, offset
, head
) < 0) {
615 dump_printf("%p [%p]: skipping unknown header type: %d\n",
616 (void *)(offset
+ head
),
617 (void *)(long)(event
->header
.size
),
623 * assume we lost track of the stream, check alignment, and
624 * increment a single u64 in the hope to catch on again 'soon'.
627 if (unlikely(head
& 7))
635 if (offset
+ head
< (unsigned long)input_stat
.st_size
)
641 dump_printf(" IP events: %10ld\n", total
);
642 dump_printf(" mmap events: %10ld\n", total_mmap
);
643 dump_printf(" comm events: %10ld\n", total_comm
);
644 dump_printf(" fork events: %10ld\n", total_fork
);
645 dump_printf(" unknown events: %10ld\n", total_unknown
);
651 threads__fprintf(stdout
);
654 dsos__fprintf(stdout
);
657 output__resort(total
);
664 static const char * const annotate_usage
[] = {
665 "perf annotate [<options>] <command>",
669 static const struct option options
[] = {
670 OPT_STRING('i', "input", &input_name
, "file",
672 OPT_STRING('s', "symbol", &sym_hist_filter
, "symbol",
673 "symbol to annotate"),
674 OPT_BOOLEAN('f', "force", &force
, "don't complain, do it"),
675 OPT_BOOLEAN('v', "verbose", &verbose
,
676 "be more verbose (show symbol address, etc)"),
677 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
678 "dump raw trace in ASCII"),
679 OPT_STRING('k', "vmlinux", &vmlinux_name
, "file", "vmlinux pathname"),
680 OPT_BOOLEAN('m', "modules", &modules
,
681 "load module symbols - WARNING: use only with -k and LIVE kernel"),
682 OPT_BOOLEAN('l', "print-line", &print_line
,
683 "print matching source lines (may be slow)"),
684 OPT_BOOLEAN('P', "full-paths", &full_paths
,
685 "Don't shorten the displayed pathnames"),
689 static void setup_sorting(void)
691 char *tmp
, *tok
, *str
= strdup(sort_order
);
693 for (tok
= strtok_r(str
, ", ", &tmp
);
694 tok
; tok
= strtok_r(NULL
, ", ", &tmp
)) {
695 if (sort_dimension__add(tok
) < 0) {
696 error("Unknown --sort key: `%s'", tok
);
697 usage_with_options(annotate_usage
, options
);
704 int cmd_annotate(int argc
, const char **argv
, const char *prefix __used
)
708 page_size
= getpagesize();
710 argc
= parse_options(argc
, argv
, options
, annotate_usage
, 0);
716 * Special case: if there's an argument left then assume tha
717 * it's a symbol filter:
720 usage_with_options(annotate_usage
, options
);
722 sym_hist_filter
= argv
[0];
725 if (!sym_hist_filter
)
726 usage_with_options(annotate_usage
, options
);
730 if (field_sep
&& *field_sep
== '.') {
731 fputs("'.' is the only non valid --field-separator argument\n",
736 return __cmd_annotate();