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";
32 static int show_mask
= SHOW_KERNEL
| SHOW_USER
| SHOW_HV
;
34 static int full_paths
;
36 static int print_line
;
38 static unsigned long page_size
;
39 static unsigned long mmap_window
= 32;
41 static struct rb_root threads
;
42 static struct thread
*last_match
;
53 * collect histogram counts
55 static void hist_hit(struct hist_entry
*he
, u64 ip
)
57 unsigned int sym_size
, offset
;
58 struct symbol
*sym
= he
->sym
;
62 if (!sym
|| !sym
->hist
)
65 sym_size
= sym
->end
- sym
->start
;
66 offset
= ip
- sym
->start
;
68 if (offset
>= sym_size
)
75 printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n",
76 (void *)(unsigned long)he
->sym
->start
,
78 (void *)(unsigned long)ip
, ip
- he
->sym
->start
,
83 hist_entry__add(struct thread
*thread
, struct map
*map
, struct dso
*dso
,
84 struct symbol
*sym
, u64 ip
, char level
)
86 struct rb_node
**p
= &hist
.rb_node
;
87 struct rb_node
*parent
= NULL
;
88 struct hist_entry
*he
;
89 struct hist_entry entry
= {
102 he
= rb_entry(parent
, struct hist_entry
, rb_node
);
104 cmp
= hist_entry__cmp(&entry
, he
);
118 he
= malloc(sizeof(*he
));
122 rb_link_node(&he
->rb_node
, parent
, p
);
123 rb_insert_color(&he
->rb_node
, &hist
);
129 process_sample_event(event_t
*event
, unsigned long offset
, unsigned long head
)
133 struct dso
*dso
= NULL
;
134 struct thread
*thread
;
135 u64 ip
= event
->ip
.ip
;
136 struct map
*map
= NULL
;
138 thread
= threads__findnew(event
->ip
.pid
, &threads
, &last_match
);
140 dump_printf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
141 (void *)(offset
+ head
),
142 (void *)(long)(event
->header
.size
),
147 dump_printf(" ... thread: %s:%d\n", thread
->comm
, thread
->pid
);
149 if (thread
== NULL
) {
150 fprintf(stderr
, "problem processing %d event, skipping it.\n",
155 if (event
->header
.misc
& PERF_RECORD_MISC_KERNEL
) {
161 dump_printf(" ...... dso: %s\n", dso
->name
);
163 } else if (event
->header
.misc
& PERF_RECORD_MISC_USER
) {
168 map
= thread__find_map(thread
, ip
);
170 ip
= map
->map_ip(map
, ip
);
174 * If this is outside of all known maps,
175 * and is a negative address, try to look it
176 * up in the kernel dso, as it might be a
177 * vsyscall (which executes in user-mode):
179 if ((long long)ip
< 0)
182 dump_printf(" ...... dso: %s\n", dso
? dso
->name
: "<not found>");
187 dump_printf(" ...... dso: [hypervisor]\n");
190 if (show
& show_mask
) {
191 struct symbol
*sym
= NULL
;
194 sym
= dso
->find_symbol(dso
, ip
);
196 if (hist_entry__add(thread
, map
, dso
, sym
, ip
, level
)) {
198 "problem incrementing symbol count, skipping event\n");
208 process_mmap_event(event_t
*event
, unsigned long offset
, unsigned long head
)
210 struct thread
*thread
;
211 struct map
*map
= map__new(&event
->mmap
, NULL
, 0);
213 thread
= threads__findnew(event
->mmap
.pid
, &threads
, &last_match
);
215 dump_printf("%p [%p]: PERF_RECORD_MMAP %d: [%p(%p) @ %p]: %s\n",
216 (void *)(offset
+ head
),
217 (void *)(long)(event
->header
.size
),
219 (void *)(long)event
->mmap
.start
,
220 (void *)(long)event
->mmap
.len
,
221 (void *)(long)event
->mmap
.pgoff
,
222 event
->mmap
.filename
);
224 if (thread
== NULL
|| map
== NULL
) {
225 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
229 thread__insert_map(thread
, map
);
236 process_comm_event(event_t
*event
, unsigned long offset
, unsigned long head
)
238 struct thread
*thread
;
240 thread
= threads__findnew(event
->comm
.pid
, &threads
, &last_match
);
241 dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
242 (void *)(offset
+ head
),
243 (void *)(long)(event
->header
.size
),
244 event
->comm
.comm
, event
->comm
.pid
);
246 if (thread
== NULL
||
247 thread__set_comm(thread
, event
->comm
.comm
)) {
248 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
257 process_fork_event(event_t
*event
, unsigned long offset
, unsigned long head
)
259 struct thread
*thread
;
260 struct thread
*parent
;
262 thread
= threads__findnew(event
->fork
.pid
, &threads
, &last_match
);
263 parent
= threads__findnew(event
->fork
.ppid
, &threads
, &last_match
);
264 dump_printf("%p [%p]: PERF_RECORD_FORK: %d:%d\n",
265 (void *)(offset
+ head
),
266 (void *)(long)(event
->header
.size
),
267 event
->fork
.pid
, event
->fork
.ppid
);
270 * A thread clone will have the same PID for both
273 if (thread
== parent
)
276 if (!thread
|| !parent
|| thread__fork(thread
, parent
)) {
277 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
286 process_event(event_t
*event
, unsigned long offset
, unsigned long head
)
288 switch (event
->header
.type
) {
289 case PERF_RECORD_SAMPLE
:
290 return process_sample_event(event
, offset
, head
);
292 case PERF_RECORD_MMAP
:
293 return process_mmap_event(event
, offset
, head
);
295 case PERF_RECORD_COMM
:
296 return process_comm_event(event
, offset
, head
);
298 case PERF_RECORD_FORK
:
299 return process_fork_event(event
, offset
, head
);
301 * We dont process them right now but they are fine:
304 case PERF_RECORD_THROTTLE
:
305 case PERF_RECORD_UNTHROTTLE
:
316 parse_line(FILE *file
, struct symbol
*sym
, u64 start
, u64 len
)
318 char *line
= NULL
, *tmp
, *tmp2
;
319 static const char *prev_line
;
320 static const char *prev_color
;
327 if (getline(&line
, &line_len
, file
) < 0)
332 c
= strchr(line
, '\n');
341 * Strip leading spaces:
352 * Parse hexa addresses followed by ':'
354 line_ip
= strtoull(tmp
, &tmp2
, 16);
360 const char *path
= NULL
;
361 unsigned int hits
= 0;
362 double percent
= 0.0;
364 struct sym_ext
*sym_ext
= sym
->priv
;
366 offset
= line_ip
- start
;
368 hits
= sym
->hist
[offset
];
370 if (offset
< len
&& sym_ext
) {
371 path
= sym_ext
[offset
].path
;
372 percent
= sym_ext
[offset
].percent
;
373 } else if (sym
->hist_sum
)
374 percent
= 100.0 * hits
/ sym
->hist_sum
;
376 color
= get_percent_color(percent
);
379 * Also color the filename and line if needed, with
380 * the same color than the percentage. Don't print it
381 * twice for close colored ip with the same filename:line
384 if (!prev_line
|| strcmp(prev_line
, path
)
385 || color
!= prev_color
) {
386 color_fprintf(stdout
, color
, " %s", path
);
392 color_fprintf(stdout
, color
, " %7.2f", percent
);
394 color_fprintf(stdout
, PERF_COLOR_BLUE
, "%s\n", line
);
399 printf(" : %s\n", line
);
405 static struct rb_root root_sym_ext
;
407 static void insert_source_line(struct sym_ext
*sym_ext
)
409 struct sym_ext
*iter
;
410 struct rb_node
**p
= &root_sym_ext
.rb_node
;
411 struct rb_node
*parent
= NULL
;
415 iter
= rb_entry(parent
, struct sym_ext
, node
);
417 if (sym_ext
->percent
> iter
->percent
)
423 rb_link_node(&sym_ext
->node
, parent
, p
);
424 rb_insert_color(&sym_ext
->node
, &root_sym_ext
);
427 static void free_source_line(struct symbol
*sym
, int len
)
429 struct sym_ext
*sym_ext
= sym
->priv
;
435 for (i
= 0; i
< len
; i
++)
436 free(sym_ext
[i
].path
);
440 root_sym_ext
= RB_ROOT
;
443 /* Get the filename:line for the colored entries */
445 get_source_line(struct symbol
*sym
, u64 start
, int len
, const char *filename
)
448 char cmd
[PATH_MAX
* 2];
449 struct sym_ext
*sym_ext
;
454 sym
->priv
= calloc(len
, sizeof(struct sym_ext
));
460 for (i
= 0; i
< len
; i
++) {
466 sym_ext
[i
].percent
= 100.0 * sym
->hist
[i
] / sym
->hist_sum
;
467 if (sym_ext
[i
].percent
<= 0.5)
471 sprintf(cmd
, "addr2line -e %s %016llx", filename
, offset
);
472 fp
= popen(cmd
, "r");
476 if (getline(&path
, &line_len
, fp
) < 0 || !line_len
)
479 sym_ext
[i
].path
= malloc(sizeof(char) * line_len
+ 1);
480 if (!sym_ext
[i
].path
)
483 strcpy(sym_ext
[i
].path
, path
);
484 insert_source_line(&sym_ext
[i
]);
491 static void print_summary(const char *filename
)
493 struct sym_ext
*sym_ext
;
494 struct rb_node
*node
;
496 printf("\nSorted summary for file %s\n", filename
);
497 printf("----------------------------------------------\n\n");
499 if (RB_EMPTY_ROOT(&root_sym_ext
)) {
500 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN
);
504 node
= rb_first(&root_sym_ext
);
510 sym_ext
= rb_entry(node
, struct sym_ext
, node
);
511 percent
= sym_ext
->percent
;
512 color
= get_percent_color(percent
);
513 path
= sym_ext
->path
;
515 color_fprintf(stdout
, color
, " %7.2f %s", percent
, path
);
516 node
= rb_next(node
);
520 static void annotate_sym(struct dso
*dso
, struct symbol
*sym
)
522 const char *filename
= dso
->name
, *d_filename
;
524 char command
[PATH_MAX
*2];
530 filename
= sym
->module
->path
;
531 else if (dso
== kernel_dso
)
532 filename
= vmlinux_name
;
534 start
= sym
->obj_start
;
538 d_filename
= filename
;
540 d_filename
= basename(filename
);
542 end
= start
+ sym
->end
- sym
->start
+ 1;
543 len
= sym
->end
- sym
->start
;
546 get_source_line(sym
, start
, len
, filename
);
547 print_summary(filename
);
550 printf("\n\n------------------------------------------------\n");
551 printf(" Percent | Source code & Disassembly of %s\n", d_filename
);
552 printf("------------------------------------------------\n");
555 printf("annotating [%p] %30s : [%p] %30s\n", dso
, dso
->name
, sym
, sym
->name
);
557 sprintf(command
, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
558 (u64
)start
, (u64
)end
, filename
, filename
);
561 printf("doing: %s\n", command
);
563 file
= popen(command
, "r");
567 while (!feof(file
)) {
568 if (parse_line(file
, sym
, start
, len
) < 0)
574 free_source_line(sym
, len
);
577 static void find_annotations(void)
583 list_for_each_entry(dso
, &dsos
, node
) {
585 for (nd
= rb_first(&dso
->syms
); nd
; nd
= rb_next(nd
)) {
586 struct symbol
*sym
= rb_entry(nd
, struct symbol
, rb_node
);
589 annotate_sym(dso
, sym
);
596 printf(" Error: symbol '%s' not present amongst the samples.\n", sym_hist_filter
);
599 static int __cmd_annotate(void)
601 int ret
, rc
= EXIT_FAILURE
;
602 unsigned long offset
= 0;
603 unsigned long head
= 0;
604 struct stat input_stat
;
609 register_idle_thread(&threads
, &last_match
);
611 input
= open(input_name
, O_RDONLY
);
613 perror("failed to open file");
617 ret
= fstat(input
, &input_stat
);
619 perror("failed to stat file");
623 if (!force
&& input_stat
.st_uid
&& (input_stat
.st_uid
!= geteuid())) {
624 fprintf(stderr
, "file: %s not owned by current user or root\n", input_name
);
628 if (!input_stat
.st_size
) {
629 fprintf(stderr
, "zero-sized file, nothing to do!\n");
633 if (load_kernel() < 0) {
634 perror("failed to load kernel symbols");
639 buf
= (char *)mmap(NULL
, page_size
* mmap_window
, PROT_READ
,
640 MAP_SHARED
, input
, offset
);
641 if (buf
== MAP_FAILED
) {
642 perror("failed to mmap file");
647 event
= (event_t
*)(buf
+ head
);
649 size
= event
->header
.size
;
653 if (head
+ event
->header
.size
>= page_size
* mmap_window
) {
654 unsigned long shift
= page_size
* (head
/ page_size
);
657 munmap_ret
= munmap(buf
, page_size
* mmap_window
);
658 assert(munmap_ret
== 0);
665 size
= event
->header
.size
;
667 dump_printf("%p [%p]: event: %d\n",
668 (void *)(offset
+ head
),
669 (void *)(long)event
->header
.size
,
672 if (!size
|| process_event(event
, offset
, head
) < 0) {
674 dump_printf("%p [%p]: skipping unknown header type: %d\n",
675 (void *)(offset
+ head
),
676 (void *)(long)(event
->header
.size
),
682 * assume we lost track of the stream, check alignment, and
683 * increment a single u64 in the hope to catch on again 'soon'.
686 if (unlikely(head
& 7))
694 if (offset
+ head
< (unsigned long)input_stat
.st_size
)
700 dump_printf(" IP events: %10ld\n", total
);
701 dump_printf(" mmap events: %10ld\n", total_mmap
);
702 dump_printf(" comm events: %10ld\n", total_comm
);
703 dump_printf(" fork events: %10ld\n", total_fork
);
704 dump_printf(" unknown events: %10ld\n", total_unknown
);
710 threads__fprintf(stdout
, &threads
);
713 dsos__fprintf(stdout
);
716 output__resort(total
);
723 static const char * const annotate_usage
[] = {
724 "perf annotate [<options>] <command>",
728 static const struct option options
[] = {
729 OPT_STRING('i', "input", &input_name
, "file",
731 OPT_STRING('s', "symbol", &sym_hist_filter
, "symbol",
732 "symbol to annotate"),
733 OPT_BOOLEAN('f', "force", &force
, "don't complain, do it"),
734 OPT_BOOLEAN('v', "verbose", &verbose
,
735 "be more verbose (show symbol address, etc)"),
736 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
737 "dump raw trace in ASCII"),
738 OPT_STRING('k', "vmlinux", &vmlinux_name
, "file", "vmlinux pathname"),
739 OPT_BOOLEAN('m', "modules", &modules
,
740 "load module symbols - WARNING: use only with -k and LIVE kernel"),
741 OPT_BOOLEAN('l', "print-line", &print_line
,
742 "print matching source lines (may be slow)"),
743 OPT_BOOLEAN('P', "full-paths", &full_paths
,
744 "Don't shorten the displayed pathnames"),
748 static void setup_sorting(void)
750 char *tmp
, *tok
, *str
= strdup(sort_order
);
752 for (tok
= strtok_r(str
, ", ", &tmp
);
753 tok
; tok
= strtok_r(NULL
, ", ", &tmp
)) {
754 if (sort_dimension__add(tok
) < 0) {
755 error("Unknown --sort key: `%s'", tok
);
756 usage_with_options(annotate_usage
, options
);
763 int cmd_annotate(int argc
, const char **argv
, const char *prefix __used
)
767 page_size
= getpagesize();
769 argc
= parse_options(argc
, argv
, options
, annotate_usage
, 0);
775 * Special case: if there's an argument left then assume tha
776 * it's a symbol filter:
779 usage_with_options(annotate_usage
, options
);
781 sym_hist_filter
= argv
[0];
784 if (!sym_hist_filter
)
785 usage_with_options(annotate_usage
, options
);
789 if (field_sep
&& *field_sep
== '.') {
790 fputs("'.' is the only non valid --field-separator argument\n",
795 return __cmd_annotate();
This page took 0.139513 seconds and 5 git commands to generate.