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"
21 #include "util/parse-options.h"
22 #include "util/parse-events.h"
23 #include "util/thread.h"
29 static char const *input_name
= "perf.data";
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)
41 static int full_paths
;
43 static int print_line
;
45 static unsigned long page_size
;
46 static unsigned long mmap_window
= 32;
48 static struct rb_root threads
;
49 static struct thread
*last_match
;
59 * histogram, sorted on item, collects counts
62 static struct rb_root hist
;
65 struct rb_node rb_node
;
67 struct thread
*thread
;
78 * configurable sorting bits
82 struct list_head list
;
86 int64_t (*cmp
)(struct hist_entry
*, struct hist_entry
*);
87 int64_t (*collapse
)(struct hist_entry
*, struct hist_entry
*);
88 size_t (*print
)(FILE *fp
, struct hist_entry
*);
94 sort__thread_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
96 return right
->thread
->pid
- left
->thread
->pid
;
100 sort__thread_print(FILE *fp
, struct hist_entry
*self
)
102 return fprintf(fp
, "%16s:%5d", self
->thread
->comm
?: "", self
->thread
->pid
);
105 static struct sort_entry sort_thread
= {
106 .header
= " Command: Pid",
107 .cmp
= sort__thread_cmp
,
108 .print
= sort__thread_print
,
114 sort__comm_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
116 return right
->thread
->pid
- left
->thread
->pid
;
120 sort__comm_collapse(struct hist_entry
*left
, struct hist_entry
*right
)
122 char *comm_l
= left
->thread
->comm
;
123 char *comm_r
= right
->thread
->comm
;
125 if (!comm_l
|| !comm_r
) {
126 if (!comm_l
&& !comm_r
)
134 return strcmp(comm_l
, comm_r
);
138 sort__comm_print(FILE *fp
, struct hist_entry
*self
)
140 return fprintf(fp
, "%16s", self
->thread
->comm
);
143 static struct sort_entry sort_comm
= {
144 .header
= " Command",
145 .cmp
= sort__comm_cmp
,
146 .collapse
= sort__comm_collapse
,
147 .print
= sort__comm_print
,
153 sort__dso_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
155 struct dso
*dso_l
= left
->dso
;
156 struct dso
*dso_r
= right
->dso
;
158 if (!dso_l
|| !dso_r
) {
159 if (!dso_l
&& !dso_r
)
167 return strcmp(dso_l
->name
, dso_r
->name
);
171 sort__dso_print(FILE *fp
, struct hist_entry
*self
)
174 return fprintf(fp
, "%-25s", self
->dso
->name
);
176 return fprintf(fp
, "%016llx ", (u64
)self
->ip
);
179 static struct sort_entry sort_dso
= {
180 .header
= "Shared Object ",
181 .cmp
= sort__dso_cmp
,
182 .print
= sort__dso_print
,
188 sort__sym_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
192 if (left
->sym
== right
->sym
)
195 ip_l
= left
->sym
? left
->sym
->start
: left
->ip
;
196 ip_r
= right
->sym
? right
->sym
->start
: right
->ip
;
198 return (int64_t)(ip_r
- ip_l
);
202 sort__sym_print(FILE *fp
, struct hist_entry
*self
)
207 ret
+= fprintf(fp
, "%#018llx ", (u64
)self
->ip
);
210 ret
+= fprintf(fp
, "[%c] %s",
211 self
->dso
== kernel_dso
? 'k' : '.', self
->sym
->name
);
213 ret
+= fprintf(fp
, "%#016llx", (u64
)self
->ip
);
219 static struct sort_entry sort_sym
= {
221 .cmp
= sort__sym_cmp
,
222 .print
= sort__sym_print
,
225 static int sort__need_collapse
= 0;
227 struct sort_dimension
{
229 struct sort_entry
*entry
;
233 static struct sort_dimension sort_dimensions
[] = {
234 { .name
= "pid", .entry
= &sort_thread
, },
235 { .name
= "comm", .entry
= &sort_comm
, },
236 { .name
= "dso", .entry
= &sort_dso
, },
237 { .name
= "symbol", .entry
= &sort_sym
, },
240 static LIST_HEAD(hist_entry__sort_list
);
242 static int sort_dimension__add(char *tok
)
246 for (i
= 0; i
< ARRAY_SIZE(sort_dimensions
); i
++) {
247 struct sort_dimension
*sd
= &sort_dimensions
[i
];
252 if (strncasecmp(tok
, sd
->name
, strlen(tok
)))
255 if (sd
->entry
->collapse
)
256 sort__need_collapse
= 1;
258 list_add_tail(&sd
->entry
->list
, &hist_entry__sort_list
);
268 hist_entry__cmp(struct hist_entry
*left
, struct hist_entry
*right
)
270 struct sort_entry
*se
;
273 list_for_each_entry(se
, &hist_entry__sort_list
, list
) {
274 cmp
= se
->cmp(left
, right
);
283 hist_entry__collapse(struct hist_entry
*left
, struct hist_entry
*right
)
285 struct sort_entry
*se
;
288 list_for_each_entry(se
, &hist_entry__sort_list
, list
) {
289 int64_t (*f
)(struct hist_entry
*, struct hist_entry
*);
291 f
= se
->collapse
?: se
->cmp
;
293 cmp
= f(left
, right
);
302 * collect histogram counts
304 static void hist_hit(struct hist_entry
*he
, u64 ip
)
306 unsigned int sym_size
, offset
;
307 struct symbol
*sym
= he
->sym
;
311 if (!sym
|| !sym
->hist
)
314 sym_size
= sym
->end
- sym
->start
;
315 offset
= ip
- sym
->start
;
317 if (offset
>= sym_size
)
324 printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n",
325 (void *)(unsigned long)he
->sym
->start
,
327 (void *)(unsigned long)ip
, ip
- he
->sym
->start
,
332 hist_entry__add(struct thread
*thread
, struct map
*map
, struct dso
*dso
,
333 struct symbol
*sym
, u64 ip
, char level
)
335 struct rb_node
**p
= &hist
.rb_node
;
336 struct rb_node
*parent
= NULL
;
337 struct hist_entry
*he
;
338 struct hist_entry entry
= {
351 he
= rb_entry(parent
, struct hist_entry
, rb_node
);
353 cmp
= hist_entry__cmp(&entry
, he
);
367 he
= malloc(sizeof(*he
));
371 rb_link_node(&he
->rb_node
, parent
, p
);
372 rb_insert_color(&he
->rb_node
, &hist
);
377 static void hist_entry__free(struct hist_entry
*he
)
383 * collapse the histogram
386 static struct rb_root collapse_hists
;
388 static void collapse__insert_entry(struct hist_entry
*he
)
390 struct rb_node
**p
= &collapse_hists
.rb_node
;
391 struct rb_node
*parent
= NULL
;
392 struct hist_entry
*iter
;
397 iter
= rb_entry(parent
, struct hist_entry
, rb_node
);
399 cmp
= hist_entry__collapse(iter
, he
);
402 iter
->count
+= he
->count
;
403 hist_entry__free(he
);
413 rb_link_node(&he
->rb_node
, parent
, p
);
414 rb_insert_color(&he
->rb_node
, &collapse_hists
);
417 static void collapse__resort(void)
419 struct rb_node
*next
;
420 struct hist_entry
*n
;
422 if (!sort__need_collapse
)
425 next
= rb_first(&hist
);
427 n
= rb_entry(next
, struct hist_entry
, rb_node
);
428 next
= rb_next(&n
->rb_node
);
430 rb_erase(&n
->rb_node
, &hist
);
431 collapse__insert_entry(n
);
436 * reverse the map, sort on count.
439 static struct rb_root output_hists
;
441 static void output__insert_entry(struct hist_entry
*he
)
443 struct rb_node
**p
= &output_hists
.rb_node
;
444 struct rb_node
*parent
= NULL
;
445 struct hist_entry
*iter
;
449 iter
= rb_entry(parent
, struct hist_entry
, rb_node
);
451 if (he
->count
> iter
->count
)
457 rb_link_node(&he
->rb_node
, parent
, p
);
458 rb_insert_color(&he
->rb_node
, &output_hists
);
461 static void output__resort(void)
463 struct rb_node
*next
;
464 struct hist_entry
*n
;
465 struct rb_root
*tree
= &hist
;
467 if (sort__need_collapse
)
468 tree
= &collapse_hists
;
470 next
= rb_first(tree
);
473 n
= rb_entry(next
, struct hist_entry
, rb_node
);
474 next
= rb_next(&n
->rb_node
);
476 rb_erase(&n
->rb_node
, tree
);
477 output__insert_entry(n
);
481 static void register_idle_thread(void)
483 struct thread
*thread
= threads__findnew(0, &threads
, &last_match
);
485 if (thread
== NULL
||
486 thread__set_comm(thread
, "[idle]")) {
487 fprintf(stderr
, "problem inserting idle task.\n");
492 static unsigned long total
= 0,
499 process_sample_event(event_t
*event
, unsigned long offset
, unsigned long head
)
503 struct dso
*dso
= NULL
;
504 struct thread
*thread
;
505 u64 ip
= event
->ip
.ip
;
506 struct map
*map
= NULL
;
508 thread
= threads__findnew(event
->ip
.pid
, &threads
, &last_match
);
510 dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
511 (void *)(offset
+ head
),
512 (void *)(long)(event
->header
.size
),
517 dprintf(" ... thread: %s:%d\n", thread
->comm
, thread
->pid
);
519 if (thread
== NULL
) {
520 fprintf(stderr
, "problem processing %d event, skipping it.\n",
525 if (event
->header
.misc
& PERF_EVENT_MISC_KERNEL
) {
531 dprintf(" ...... dso: %s\n", dso
->name
);
533 } else if (event
->header
.misc
& PERF_EVENT_MISC_USER
) {
538 map
= thread__find_map(thread
, ip
);
540 ip
= map
->map_ip(map
, ip
);
544 * If this is outside of all known maps,
545 * and is a negative address, try to look it
546 * up in the kernel dso, as it might be a
547 * vsyscall (which executes in user-mode):
549 if ((long long)ip
< 0)
552 dprintf(" ...... dso: %s\n", dso
? dso
->name
: "<not found>");
557 dprintf(" ...... dso: [hypervisor]\n");
560 if (show
& show_mask
) {
561 struct symbol
*sym
= NULL
;
564 sym
= dso
->find_symbol(dso
, ip
);
566 if (hist_entry__add(thread
, map
, dso
, sym
, ip
, level
)) {
568 "problem incrementing symbol count, skipping event\n");
578 process_mmap_event(event_t
*event
, unsigned long offset
, unsigned long head
)
580 struct thread
*thread
;
581 struct map
*map
= map__new(&event
->mmap
, NULL
, 0);
583 thread
= threads__findnew(event
->mmap
.pid
, &threads
, &last_match
);
585 dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
586 (void *)(offset
+ head
),
587 (void *)(long)(event
->header
.size
),
589 (void *)(long)event
->mmap
.start
,
590 (void *)(long)event
->mmap
.len
,
591 (void *)(long)event
->mmap
.pgoff
,
592 event
->mmap
.filename
);
594 if (thread
== NULL
|| map
== NULL
) {
595 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
599 thread__insert_map(thread
, map
);
606 process_comm_event(event_t
*event
, unsigned long offset
, unsigned long head
)
608 struct thread
*thread
;
610 thread
= threads__findnew(event
->comm
.pid
, &threads
, &last_match
);
611 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
612 (void *)(offset
+ head
),
613 (void *)(long)(event
->header
.size
),
614 event
->comm
.comm
, event
->comm
.pid
);
616 if (thread
== NULL
||
617 thread__set_comm(thread
, event
->comm
.comm
)) {
618 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
627 process_fork_event(event_t
*event
, unsigned long offset
, unsigned long head
)
629 struct thread
*thread
;
630 struct thread
*parent
;
632 thread
= threads__findnew(event
->fork
.pid
, &threads
, &last_match
);
633 parent
= threads__findnew(event
->fork
.ppid
, &threads
, &last_match
);
634 dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
635 (void *)(offset
+ head
),
636 (void *)(long)(event
->header
.size
),
637 event
->fork
.pid
, event
->fork
.ppid
);
639 if (!thread
|| !parent
|| thread__fork(thread
, parent
)) {
640 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
649 process_event(event_t
*event
, unsigned long offset
, unsigned long head
)
651 switch (event
->header
.type
) {
652 case PERF_EVENT_SAMPLE
:
653 return process_sample_event(event
, offset
, head
);
655 case PERF_EVENT_MMAP
:
656 return process_mmap_event(event
, offset
, head
);
658 case PERF_EVENT_COMM
:
659 return process_comm_event(event
, offset
, head
);
661 case PERF_EVENT_FORK
:
662 return process_fork_event(event
, offset
, head
);
664 * We dont process them right now but they are fine:
667 case PERF_EVENT_THROTTLE
:
668 case PERF_EVENT_UNTHROTTLE
:
679 parse_line(FILE *file
, struct symbol
*sym
, u64 start
, u64 len
)
681 char *line
= NULL
, *tmp
, *tmp2
;
682 static const char *prev_line
;
683 static const char *prev_color
;
690 if (getline(&line
, &line_len
, file
) < 0)
695 c
= strchr(line
, '\n');
704 * Strip leading spaces:
715 * Parse hexa addresses followed by ':'
717 line_ip
= strtoull(tmp
, &tmp2
, 16);
723 const char *path
= NULL
;
724 unsigned int hits
= 0;
725 double percent
= 0.0;
727 struct sym_ext
*sym_ext
= sym
->priv
;
729 offset
= line_ip
- start
;
731 hits
= sym
->hist
[offset
];
733 if (offset
< len
&& sym_ext
) {
734 path
= sym_ext
[offset
].path
;
735 percent
= sym_ext
[offset
].percent
;
736 } else if (sym
->hist_sum
)
737 percent
= 100.0 * hits
/ sym
->hist_sum
;
739 color
= get_percent_color(percent
);
742 * Also color the filename and line if needed, with
743 * the same color than the percentage. Don't print it
744 * twice for close colored ip with the same filename:line
747 if (!prev_line
|| strcmp(prev_line
, path
)
748 || color
!= prev_color
) {
749 color_fprintf(stdout
, color
, " %s", path
);
755 color_fprintf(stdout
, color
, " %7.2f", percent
);
757 color_fprintf(stdout
, PERF_COLOR_BLUE
, "%s\n", line
);
762 printf(" : %s\n", line
);
768 static struct rb_root root_sym_ext
;
770 static void insert_source_line(struct sym_ext
*sym_ext
)
772 struct sym_ext
*iter
;
773 struct rb_node
**p
= &root_sym_ext
.rb_node
;
774 struct rb_node
*parent
= NULL
;
778 iter
= rb_entry(parent
, struct sym_ext
, node
);
780 if (sym_ext
->percent
> iter
->percent
)
786 rb_link_node(&sym_ext
->node
, parent
, p
);
787 rb_insert_color(&sym_ext
->node
, &root_sym_ext
);
790 static void free_source_line(struct symbol
*sym
, int len
)
792 struct sym_ext
*sym_ext
= sym
->priv
;
798 for (i
= 0; i
< len
; i
++)
799 free(sym_ext
[i
].path
);
803 root_sym_ext
= RB_ROOT
;
806 /* Get the filename:line for the colored entries */
808 get_source_line(struct symbol
*sym
, u64 start
, int len
, const char *filename
)
811 char cmd
[PATH_MAX
* 2];
812 struct sym_ext
*sym_ext
;
817 sym
->priv
= calloc(len
, sizeof(struct sym_ext
));
823 for (i
= 0; i
< len
; i
++) {
829 sym_ext
[i
].percent
= 100.0 * sym
->hist
[i
] / sym
->hist_sum
;
830 if (sym_ext
[i
].percent
<= 0.5)
834 sprintf(cmd
, "addr2line -e %s %016llx", filename
, offset
);
835 fp
= popen(cmd
, "r");
839 if (getline(&path
, &line_len
, fp
) < 0 || !line_len
)
842 sym_ext
[i
].path
= malloc(sizeof(char) * line_len
+ 1);
843 if (!sym_ext
[i
].path
)
846 strcpy(sym_ext
[i
].path
, path
);
847 insert_source_line(&sym_ext
[i
]);
854 static void print_summary(const char *filename
)
856 struct sym_ext
*sym_ext
;
857 struct rb_node
*node
;
859 printf("\nSorted summary for file %s\n", filename
);
860 printf("----------------------------------------------\n\n");
862 if (RB_EMPTY_ROOT(&root_sym_ext
)) {
863 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN
);
867 node
= rb_first(&root_sym_ext
);
873 sym_ext
= rb_entry(node
, struct sym_ext
, node
);
874 percent
= sym_ext
->percent
;
875 color
= get_percent_color(percent
);
876 path
= sym_ext
->path
;
878 color_fprintf(stdout
, color
, " %7.2f %s", percent
, path
);
879 node
= rb_next(node
);
883 static void annotate_sym(struct dso
*dso
, struct symbol
*sym
)
885 const char *filename
= dso
->name
, *d_filename
;
887 char command
[PATH_MAX
*2];
893 filename
= sym
->module
->path
;
894 else if (dso
== kernel_dso
)
895 filename
= vmlinux_name
;
897 start
= sym
->obj_start
;
901 d_filename
= filename
;
903 d_filename
= basename(filename
);
905 end
= start
+ sym
->end
- sym
->start
+ 1;
906 len
= sym
->end
- sym
->start
;
909 get_source_line(sym
, start
, len
, filename
);
910 print_summary(filename
);
913 printf("\n\n------------------------------------------------\n");
914 printf(" Percent | Source code & Disassembly of %s\n", d_filename
);
915 printf("------------------------------------------------\n");
918 printf("annotating [%p] %30s : [%p] %30s\n", dso
, dso
->name
, sym
, sym
->name
);
920 sprintf(command
, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
921 (u64
)start
, (u64
)end
, filename
, filename
);
924 printf("doing: %s\n", command
);
926 file
= popen(command
, "r");
930 while (!feof(file
)) {
931 if (parse_line(file
, sym
, start
, len
) < 0)
937 free_source_line(sym
, len
);
940 static void find_annotations(void)
946 list_for_each_entry(dso
, &dsos
, node
) {
948 for (nd
= rb_first(&dso
->syms
); nd
; nd
= rb_next(nd
)) {
949 struct symbol
*sym
= rb_entry(nd
, struct symbol
, rb_node
);
952 annotate_sym(dso
, sym
);
959 printf(" Error: symbol '%s' not present amongst the samples.\n", sym_hist_filter
);
962 static int __cmd_annotate(void)
964 int ret
, rc
= EXIT_FAILURE
;
965 unsigned long offset
= 0;
966 unsigned long head
= 0;
967 struct stat input_stat
;
972 register_idle_thread();
974 input
= open(input_name
, O_RDONLY
);
976 perror("failed to open file");
980 ret
= fstat(input
, &input_stat
);
982 perror("failed to stat file");
986 if (!input_stat
.st_size
) {
987 fprintf(stderr
, "zero-sized file, nothing to do!\n");
991 if (load_kernel() < 0) {
992 perror("failed to load kernel symbols");
997 buf
= (char *)mmap(NULL
, page_size
* mmap_window
, PROT_READ
,
998 MAP_SHARED
, input
, offset
);
999 if (buf
== MAP_FAILED
) {
1000 perror("failed to mmap file");
1005 event
= (event_t
*)(buf
+ head
);
1007 size
= event
->header
.size
;
1011 if (head
+ event
->header
.size
>= page_size
* mmap_window
) {
1012 unsigned long shift
= page_size
* (head
/ page_size
);
1015 munmap_ret
= munmap(buf
, page_size
* mmap_window
);
1016 assert(munmap_ret
== 0);
1023 size
= event
->header
.size
;
1025 dprintf("%p [%p]: event: %d\n",
1026 (void *)(offset
+ head
),
1027 (void *)(long)event
->header
.size
,
1028 event
->header
.type
);
1030 if (!size
|| process_event(event
, offset
, head
) < 0) {
1032 dprintf("%p [%p]: skipping unknown header type: %d\n",
1033 (void *)(offset
+ head
),
1034 (void *)(long)(event
->header
.size
),
1035 event
->header
.type
);
1040 * assume we lost track of the stream, check alignment, and
1041 * increment a single u64 in the hope to catch on again 'soon'.
1044 if (unlikely(head
& 7))
1052 if (offset
+ head
< (unsigned long)input_stat
.st_size
)
1058 dprintf(" IP events: %10ld\n", total
);
1059 dprintf(" mmap events: %10ld\n", total_mmap
);
1060 dprintf(" comm events: %10ld\n", total_comm
);
1061 dprintf(" fork events: %10ld\n", total_fork
);
1062 dprintf(" unknown events: %10ld\n", total_unknown
);
1068 threads__fprintf(stdout
, &threads
);
1071 dsos__fprintf(stdout
);
1081 static const char * const annotate_usage
[] = {
1082 "perf annotate [<options>] <command>",
1086 static const struct option options
[] = {
1087 OPT_STRING('i', "input", &input_name
, "file",
1089 OPT_STRING('s', "symbol", &sym_hist_filter
, "symbol",
1090 "symbol to annotate"),
1091 OPT_BOOLEAN('v', "verbose", &verbose
,
1092 "be more verbose (show symbol address, etc)"),
1093 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
1094 "dump raw trace in ASCII"),
1095 OPT_STRING('k', "vmlinux", &vmlinux_name
, "file", "vmlinux pathname"),
1096 OPT_BOOLEAN('m', "modules", &modules
,
1097 "load module symbols - WARNING: use only with -k and LIVE kernel"),
1098 OPT_BOOLEAN('l', "print-line", &print_line
,
1099 "print matching source lines (may be slow)"),
1100 OPT_BOOLEAN('P', "full-paths", &full_paths
,
1101 "Don't shorten the displayed pathnames"),
1105 static void setup_sorting(void)
1107 char *tmp
, *tok
, *str
= strdup(sort_order
);
1109 for (tok
= strtok_r(str
, ", ", &tmp
);
1110 tok
; tok
= strtok_r(NULL
, ", ", &tmp
)) {
1111 if (sort_dimension__add(tok
) < 0) {
1112 error("Unknown --sort key: `%s'", tok
);
1113 usage_with_options(annotate_usage
, options
);
1120 int cmd_annotate(int argc
, const char **argv
, const char *prefix __used
)
1124 page_size
= getpagesize();
1126 argc
= parse_options(argc
, argv
, options
, annotate_usage
, 0);
1132 * Special case: if there's an argument left then assume tha
1133 * it's a symbol filter:
1136 usage_with_options(annotate_usage
, options
);
1138 sym_hist_filter
= argv
[0];
1141 if (!sym_hist_filter
)
1142 usage_with_options(annotate_usage
, options
);
1146 return __cmd_annotate();