7d39bd2b19b83d4b59973f68a04d7531c0249967
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/event.h"
23 #include "util/parse-options.h"
24 #include "util/parse-events.h"
25 #include "util/thread.h"
26 #include "util/sort.h"
27 #include "util/hist.h"
29 static char const *input_name
= "perf.data";
34 static int full_paths
;
36 static int print_line
;
38 static unsigned long page_size
;
39 static unsigned long mmap_window
= 32;
53 struct sym_hist
*hist
;
57 static struct symbol_conf symbol_conf
= {
58 .priv_size
= sizeof(struct sym_priv
),
59 .try_vmlinux_path
= true,
62 static const char *sym_hist_filter
;
64 static int symbol_filter(struct map
*map __used
, struct symbol
*sym
)
66 if (sym_hist_filter
== NULL
||
67 strcmp(sym
->name
, sym_hist_filter
) == 0) {
68 struct sym_priv
*priv
= symbol__priv(sym
);
69 const int size
= (sizeof(*priv
->hist
) +
70 (sym
->end
- sym
->start
) * sizeof(u64
));
72 priv
->hist
= malloc(size
);
74 memset(priv
->hist
, 0, size
);
78 * FIXME: We should really filter it out, as we don't want to go thru symbols
79 * we're not interested, and if a DSO ends up with no symbols, delete it too,
80 * but right now the kernel loading routines in symbol.c bail out if no symbols
81 * are found, fix it later.
87 * collect histogram counts
89 static void hist_hit(struct hist_entry
*he
, u64 ip
)
91 unsigned int sym_size
, offset
;
92 struct symbol
*sym
= he
->sym
;
93 struct sym_priv
*priv
;
101 priv
= symbol__priv(sym
);
105 sym_size
= sym
->end
- sym
->start
;
106 offset
= ip
- sym
->start
;
109 fprintf(stderr
, "%s: ip=%Lx\n", __func__
,
110 he
->map
->unmap_ip(he
->map
, ip
));
112 if (offset
>= sym_size
)
120 printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n",
121 (void *)(unsigned long)he
->sym
->start
,
123 (void *)(unsigned long)ip
, ip
- he
->sym
->start
,
127 static int hist_entry__add(struct thread
*thread
, struct map
*map
,
128 struct symbol
*sym
, u64 ip
, u64 count
, char level
)
131 struct hist_entry
*he
= __hist_entry__add(thread
, map
, sym
, NULL
, ip
,
139 static int process_sample_event(event_t
*event
)
142 u64 ip
= event
->ip
.ip
;
143 struct map
*map
= NULL
;
144 struct symbol
*sym
= NULL
;
145 struct thread
*thread
= threads__findnew(event
->ip
.pid
);
147 dump_printf("(IP, %d): %d: %p\n", event
->header
.misc
,
148 event
->ip
.pid
, (void *)(long)ip
);
150 if (thread
== NULL
) {
151 fprintf(stderr
, "problem processing %d event, skipping it.\n",
156 dump_printf(" ... thread: %s:%d\n", thread
->comm
, thread
->pid
);
158 if (event
->header
.misc
& PERF_RECORD_MISC_KERNEL
) {
160 sym
= kernel_maps__find_function(ip
, &map
, symbol_filter
);
161 dump_printf(" ...... dso: %s\n",
162 map
? map
->dso
->long_name
: "<not found>");
163 } else if (event
->header
.misc
& PERF_RECORD_MISC_USER
) {
165 map
= thread__find_map(thread
, MAP__FUNCTION
, ip
);
167 ip
= map
->map_ip(map
, ip
);
168 sym
= map__find_symbol(map
, ip
, symbol_filter
);
171 * If this is outside of all known maps,
172 * and is a negative address, try to look it
173 * up in the kernel dso, as it might be a
174 * vsyscall or vdso (which executes in user-mode).
176 * XXX This is nasty, we should have a symbol list in
177 * the "[vdso]" dso, but for now lets use the old
178 * trick of looking in the whole kernel symbol list.
180 if ((long long)ip
< 0)
181 sym
= kernel_maps__find_function(ip
, &map
,
184 dump_printf(" ...... dso: %s\n",
185 map
? map
->dso
->long_name
: "<not found>");
188 dump_printf(" ...... dso: [hypervisor]\n");
191 if (hist_entry__add(thread
, map
, sym
, ip
, 1, level
)) {
192 fprintf(stderr
, "problem incrementing symbol count, "
200 static int event__process(event_t
*self
)
202 switch (self
->header
.type
) {
203 case PERF_RECORD_SAMPLE
:
204 return process_sample_event(self
);
206 case PERF_RECORD_MMAP
:
207 return event__process_mmap(self
);
209 case PERF_RECORD_COMM
:
210 return event__process_comm(self
);
212 case PERF_RECORD_FORK
:
213 return event__process_task(self
);
215 * We dont process them right now but they are fine:
218 case PERF_RECORD_THROTTLE
:
219 case PERF_RECORD_UNTHROTTLE
:
229 static int parse_line(FILE *file
, struct hist_entry
*he
, u64 len
)
231 struct symbol
*sym
= he
->sym
;
232 char *line
= NULL
, *tmp
, *tmp2
;
233 static const char *prev_line
;
234 static const char *prev_color
;
242 if (getline(&line
, &line_len
, file
) < 0)
247 c
= strchr(line
, '\n');
256 * Strip leading spaces:
267 * Parse hexa addresses followed by ':'
269 line_ip
= strtoull(tmp
, &tmp2
, 16);
274 start
= he
->map
->unmap_ip(he
->map
, sym
->start
);
277 const char *path
= NULL
;
278 unsigned int hits
= 0;
279 double percent
= 0.0;
281 struct sym_priv
*priv
= symbol__priv(sym
);
282 struct sym_ext
*sym_ext
= priv
->ext
;
283 struct sym_hist
*h
= priv
->hist
;
285 offset
= line_ip
- start
;
287 hits
= h
->ip
[offset
];
289 if (offset
< len
&& sym_ext
) {
290 path
= sym_ext
[offset
].path
;
291 percent
= sym_ext
[offset
].percent
;
293 percent
= 100.0 * hits
/ h
->sum
;
295 color
= get_percent_color(percent
);
298 * Also color the filename and line if needed, with
299 * the same color than the percentage. Don't print it
300 * twice for close colored ip with the same filename:line
303 if (!prev_line
|| strcmp(prev_line
, path
)
304 || color
!= prev_color
) {
305 color_fprintf(stdout
, color
, " %s", path
);
311 color_fprintf(stdout
, color
, " %7.2f", percent
);
313 color_fprintf(stdout
, PERF_COLOR_BLUE
, "%s\n", line
);
318 printf(" : %s\n", line
);
324 static struct rb_root root_sym_ext
;
326 static void insert_source_line(struct sym_ext
*sym_ext
)
328 struct sym_ext
*iter
;
329 struct rb_node
**p
= &root_sym_ext
.rb_node
;
330 struct rb_node
*parent
= NULL
;
334 iter
= rb_entry(parent
, struct sym_ext
, node
);
336 if (sym_ext
->percent
> iter
->percent
)
342 rb_link_node(&sym_ext
->node
, parent
, p
);
343 rb_insert_color(&sym_ext
->node
, &root_sym_ext
);
346 static void free_source_line(struct hist_entry
*he
, int len
)
348 struct sym_priv
*priv
= symbol__priv(he
->sym
);
349 struct sym_ext
*sym_ext
= priv
->ext
;
355 for (i
= 0; i
< len
; i
++)
356 free(sym_ext
[i
].path
);
360 root_sym_ext
= RB_ROOT
;
363 /* Get the filename:line for the colored entries */
365 get_source_line(struct hist_entry
*he
, int len
, const char *filename
)
367 struct symbol
*sym
= he
->sym
;
370 char cmd
[PATH_MAX
* 2];
371 struct sym_ext
*sym_ext
;
372 struct sym_priv
*priv
= symbol__priv(sym
);
373 struct sym_hist
*h
= priv
->hist
;
378 sym_ext
= priv
->ext
= calloc(len
, sizeof(struct sym_ext
));
382 start
= he
->map
->unmap_ip(he
->map
, sym
->start
);
384 for (i
= 0; i
< len
; i
++) {
390 sym_ext
[i
].percent
= 100.0 * h
->ip
[i
] / h
->sum
;
391 if (sym_ext
[i
].percent
<= 0.5)
395 sprintf(cmd
, "addr2line -e %s %016llx", filename
, offset
);
396 fp
= popen(cmd
, "r");
400 if (getline(&path
, &line_len
, fp
) < 0 || !line_len
)
403 sym_ext
[i
].path
= malloc(sizeof(char) * line_len
+ 1);
404 if (!sym_ext
[i
].path
)
407 strcpy(sym_ext
[i
].path
, path
);
408 insert_source_line(&sym_ext
[i
]);
415 static void print_summary(const char *filename
)
417 struct sym_ext
*sym_ext
;
418 struct rb_node
*node
;
420 printf("\nSorted summary for file %s\n", filename
);
421 printf("----------------------------------------------\n\n");
423 if (RB_EMPTY_ROOT(&root_sym_ext
)) {
424 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN
);
428 node
= rb_first(&root_sym_ext
);
434 sym_ext
= rb_entry(node
, struct sym_ext
, node
);
435 percent
= sym_ext
->percent
;
436 color
= get_percent_color(percent
);
437 path
= sym_ext
->path
;
439 color_fprintf(stdout
, color
, " %7.2f %s", percent
, path
);
440 node
= rb_next(node
);
444 static void annotate_sym(struct hist_entry
*he
)
446 struct map
*map
= he
->map
;
447 struct dso
*dso
= map
->dso
;
448 struct symbol
*sym
= he
->sym
;
449 const char *filename
= dso
->long_name
, *d_filename
;
451 char command
[PATH_MAX
*2];
458 fprintf(stderr
, "%s: filename=%s, sym=%s, start=%Lx, end=%Lx\n",
459 __func__
, filename
, sym
->name
,
460 map
->unmap_ip(map
, sym
->start
),
461 map
->unmap_ip(map
, sym
->end
));
464 d_filename
= filename
;
466 d_filename
= basename(filename
);
468 len
= sym
->end
- sym
->start
;
471 get_source_line(he
, len
, filename
);
472 print_summary(filename
);
475 printf("\n\n------------------------------------------------\n");
476 printf(" Percent | Source code & Disassembly of %s\n", d_filename
);
477 printf("------------------------------------------------\n");
480 printf("annotating [%p] %30s : [%p] %30s\n",
481 dso
, dso
->long_name
, sym
, sym
->name
);
483 sprintf(command
, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
484 map
->unmap_ip(map
, sym
->start
), map
->unmap_ip(map
, sym
->end
),
488 printf("doing: %s\n", command
);
490 file
= popen(command
, "r");
494 while (!feof(file
)) {
495 if (parse_line(file
, he
, len
) < 0)
501 free_source_line(he
, len
);
504 static void find_annotations(void)
508 for (nd
= rb_first(&output_hists
); nd
; nd
= rb_next(nd
)) {
509 struct hist_entry
*he
= rb_entry(nd
, struct hist_entry
, rb_node
);
510 struct sym_priv
*priv
;
515 priv
= symbol__priv(he
->sym
);
516 if (priv
->hist
== NULL
)
521 * Since we have a hist_entry per IP for the same symbol, free
522 * he->sym->hist to signal we already processed this symbol.
529 static int __cmd_annotate(void)
531 int ret
, rc
= EXIT_FAILURE
;
532 unsigned long offset
= 0;
533 unsigned long head
= 0;
534 struct stat input_stat
;
539 register_idle_thread();
541 input
= open(input_name
, O_RDONLY
);
543 perror("failed to open file");
547 ret
= fstat(input
, &input_stat
);
549 perror("failed to stat file");
553 if (!force
&& input_stat
.st_uid
&& (input_stat
.st_uid
!= geteuid())) {
554 fprintf(stderr
, "file: %s not owned by current user or root\n", input_name
);
558 if (!input_stat
.st_size
) {
559 fprintf(stderr
, "zero-sized file, nothing to do!\n");
564 buf
= (char *)mmap(NULL
, page_size
* mmap_window
, PROT_READ
,
565 MAP_SHARED
, input
, offset
);
566 if (buf
== MAP_FAILED
) {
567 perror("failed to mmap file");
572 event
= (event_t
*)(buf
+ head
);
574 size
= event
->header
.size
;
578 if (head
+ event
->header
.size
>= page_size
* mmap_window
) {
579 unsigned long shift
= page_size
* (head
/ page_size
);
582 munmap_ret
= munmap(buf
, page_size
* mmap_window
);
583 assert(munmap_ret
== 0);
590 size
= event
->header
.size
;
592 dump_printf("%p [%p]: event: %d\n",
593 (void *)(offset
+ head
),
594 (void *)(long)event
->header
.size
,
597 if (!size
|| event__process(event
) < 0) {
599 dump_printf("%p [%p]: skipping unknown header type: %d\n",
600 (void *)(offset
+ head
),
601 (void *)(long)(event
->header
.size
),
604 * assume we lost track of the stream, check alignment, and
605 * increment a single u64 in the hope to catch on again 'soon'.
608 if (unlikely(head
& 7))
616 if (offset
+ head
< (unsigned long)input_stat
.st_size
)
624 event__print_totals();
629 threads__fprintf(stdout
);
632 dsos__fprintf(stdout
);
635 output__resort(event__total
[0]);
642 static const char * const annotate_usage
[] = {
643 "perf annotate [<options>] <command>",
647 static const struct option options
[] = {
648 OPT_STRING('i', "input", &input_name
, "file",
650 OPT_STRING('s', "symbol", &sym_hist_filter
, "symbol",
651 "symbol to annotate"),
652 OPT_BOOLEAN('f', "force", &force
, "don't complain, do it"),
653 OPT_BOOLEAN('v', "verbose", &verbose
,
654 "be more verbose (show symbol address, etc)"),
655 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
656 "dump raw trace in ASCII"),
657 OPT_STRING('k', "vmlinux", &symbol_conf
.vmlinux_name
,
658 "file", "vmlinux pathname"),
659 OPT_BOOLEAN('m', "modules", &symbol_conf
.use_modules
,
660 "load module symbols - WARNING: use only with -k and LIVE kernel"),
661 OPT_BOOLEAN('l', "print-line", &print_line
,
662 "print matching source lines (may be slow)"),
663 OPT_BOOLEAN('P', "full-paths", &full_paths
,
664 "Don't shorten the displayed pathnames"),
668 static void setup_sorting(void)
670 char *tmp
, *tok
, *str
= strdup(sort_order
);
672 for (tok
= strtok_r(str
, ", ", &tmp
);
673 tok
; tok
= strtok_r(NULL
, ", ", &tmp
)) {
674 if (sort_dimension__add(tok
) < 0) {
675 error("Unknown --sort key: `%s'", tok
);
676 usage_with_options(annotate_usage
, options
);
683 int cmd_annotate(int argc
, const char **argv
, const char *prefix __used
)
685 if (symbol__init(&symbol_conf
) < 0)
688 page_size
= getpagesize();
690 argc
= parse_options(argc
, argv
, options
, annotate_usage
, 0);
696 * Special case: if there's an argument left then assume tha
697 * it's a symbol filter:
700 usage_with_options(annotate_usage
, options
);
702 sym_hist_filter
= argv
[0];
707 if (field_sep
&& *field_sep
== '.') {
708 fputs("'.' is the only non valid --field-separator argument\n",
713 return __cmd_annotate();
This page took 0.055753 seconds and 4 git commands to generate.