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"
28 static char const *input_name
= "perf.data";
30 static char default_sort_order
[] = "comm,symbol";
31 static char *sort_order
= default_sort_order
;
34 static int show_mask
= SHOW_KERNEL
| SHOW_USER
| SHOW_HV
;
36 static int dump_trace
= 0;
37 #define dprintf(x...) do { if (dump_trace) printf(x); } while (0)
40 static int full_paths
;
42 static int print_line
;
44 static unsigned long page_size
;
45 static unsigned long mmap_window
= 32;
55 struct list_head node
;
59 u64 (*map_ip
)(struct map
*, u64
);
63 static u64
map__map_ip(struct map
*map
, u64 ip
)
65 return ip
- map
->start
+ map
->pgoff
;
68 static u64
vdso__map_ip(struct map
*map __used
, u64 ip
)
73 static struct map
*map__new(struct mmap_event
*event
)
75 struct map
*self
= malloc(sizeof(*self
));
78 const char *filename
= event
->filename
;
80 self
->start
= event
->start
;
81 self
->end
= event
->start
+ event
->len
;
82 self
->pgoff
= event
->pgoff
;
84 self
->dso
= dsos__findnew(filename
);
85 if (self
->dso
== NULL
)
88 if (self
->dso
== vdso
)
89 self
->map_ip
= vdso__map_ip
;
91 self
->map_ip
= map__map_ip
;
99 static struct map
*map__clone(struct map
*self
)
101 struct map
*map
= malloc(sizeof(*self
));
106 memcpy(map
, self
, sizeof(*self
));
111 static int map__overlap(struct map
*l
, struct map
*r
)
113 if (l
->start
> r
->start
) {
119 if (l
->end
> r
->start
)
125 static size_t map__fprintf(struct map
*self
, FILE *fp
)
127 return fprintf(fp
, " %Lx-%Lx %Lx %s\n",
128 self
->start
, self
->end
, self
->pgoff
, self
->dso
->name
);
133 struct rb_node rb_node
;
134 struct list_head maps
;
139 static struct thread
*thread__new(pid_t pid
)
141 struct thread
*self
= malloc(sizeof(*self
));
145 self
->comm
= malloc(32);
147 snprintf(self
->comm
, 32, ":%d", self
->pid
);
148 INIT_LIST_HEAD(&self
->maps
);
154 static int thread__set_comm(struct thread
*self
, const char *comm
)
158 self
->comm
= strdup(comm
);
159 return self
->comm
? 0 : -ENOMEM
;
162 static size_t thread__fprintf(struct thread
*self
, FILE *fp
)
165 size_t ret
= fprintf(fp
, "Thread %d %s\n", self
->pid
, self
->comm
);
167 list_for_each_entry(pos
, &self
->maps
, node
)
168 ret
+= map__fprintf(pos
, fp
);
174 static struct rb_root threads
;
175 static struct thread
*last_match
;
177 static struct thread
*threads__findnew(pid_t pid
)
179 struct rb_node
**p
= &threads
.rb_node
;
180 struct rb_node
*parent
= NULL
;
184 * Font-end cache - PID lookups come in blocks,
185 * so most of the time we dont have to look up
188 if (last_match
&& last_match
->pid
== pid
)
193 th
= rb_entry(parent
, struct thread
, rb_node
);
195 if (th
->pid
== pid
) {
206 th
= thread__new(pid
);
208 rb_link_node(&th
->rb_node
, parent
, p
);
209 rb_insert_color(&th
->rb_node
, &threads
);
216 static void thread__insert_map(struct thread
*self
, struct map
*map
)
218 struct map
*pos
, *tmp
;
220 list_for_each_entry_safe(pos
, tmp
, &self
->maps
, node
) {
221 if (map__overlap(pos
, map
)) {
222 list_del_init(&pos
->node
);
228 list_add_tail(&map
->node
, &self
->maps
);
231 static int thread__fork(struct thread
*self
, struct thread
*parent
)
237 self
->comm
= strdup(parent
->comm
);
241 list_for_each_entry(map
, &parent
->maps
, node
) {
242 struct map
*new = map__clone(map
);
245 thread__insert_map(self
, new);
251 static struct map
*thread__find_map(struct thread
*self
, u64 ip
)
258 list_for_each_entry(pos
, &self
->maps
, node
)
259 if (ip
>= pos
->start
&& ip
<= pos
->end
)
265 static size_t threads__fprintf(FILE *fp
)
270 for (nd
= rb_first(&threads
); nd
; nd
= rb_next(nd
)) {
271 struct thread
*pos
= rb_entry(nd
, struct thread
, rb_node
);
273 ret
+= thread__fprintf(pos
, fp
);
280 * histogram, sorted on item, collects counts
283 static struct rb_root hist
;
286 struct rb_node rb_node
;
288 struct thread
*thread
;
299 * configurable sorting bits
303 struct list_head list
;
307 int64_t (*cmp
)(struct hist_entry
*, struct hist_entry
*);
308 int64_t (*collapse
)(struct hist_entry
*, struct hist_entry
*);
309 size_t (*print
)(FILE *fp
, struct hist_entry
*);
315 sort__thread_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
317 return right
->thread
->pid
- left
->thread
->pid
;
321 sort__thread_print(FILE *fp
, struct hist_entry
*self
)
323 return fprintf(fp
, "%16s:%5d", self
->thread
->comm
?: "", self
->thread
->pid
);
326 static struct sort_entry sort_thread
= {
327 .header
= " Command: Pid",
328 .cmp
= sort__thread_cmp
,
329 .print
= sort__thread_print
,
335 sort__comm_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
337 return right
->thread
->pid
- left
->thread
->pid
;
341 sort__comm_collapse(struct hist_entry
*left
, struct hist_entry
*right
)
343 char *comm_l
= left
->thread
->comm
;
344 char *comm_r
= right
->thread
->comm
;
346 if (!comm_l
|| !comm_r
) {
347 if (!comm_l
&& !comm_r
)
355 return strcmp(comm_l
, comm_r
);
359 sort__comm_print(FILE *fp
, struct hist_entry
*self
)
361 return fprintf(fp
, "%16s", self
->thread
->comm
);
364 static struct sort_entry sort_comm
= {
365 .header
= " Command",
366 .cmp
= sort__comm_cmp
,
367 .collapse
= sort__comm_collapse
,
368 .print
= sort__comm_print
,
374 sort__dso_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
376 struct dso
*dso_l
= left
->dso
;
377 struct dso
*dso_r
= right
->dso
;
379 if (!dso_l
|| !dso_r
) {
380 if (!dso_l
&& !dso_r
)
388 return strcmp(dso_l
->name
, dso_r
->name
);
392 sort__dso_print(FILE *fp
, struct hist_entry
*self
)
395 return fprintf(fp
, "%-25s", self
->dso
->name
);
397 return fprintf(fp
, "%016llx ", (u64
)self
->ip
);
400 static struct sort_entry sort_dso
= {
401 .header
= "Shared Object ",
402 .cmp
= sort__dso_cmp
,
403 .print
= sort__dso_print
,
409 sort__sym_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
413 if (left
->sym
== right
->sym
)
416 ip_l
= left
->sym
? left
->sym
->start
: left
->ip
;
417 ip_r
= right
->sym
? right
->sym
->start
: right
->ip
;
419 return (int64_t)(ip_r
- ip_l
);
423 sort__sym_print(FILE *fp
, struct hist_entry
*self
)
428 ret
+= fprintf(fp
, "%#018llx ", (u64
)self
->ip
);
431 ret
+= fprintf(fp
, "[%c] %s",
432 self
->dso
== kernel_dso
? 'k' : '.', self
->sym
->name
);
434 ret
+= fprintf(fp
, "%#016llx", (u64
)self
->ip
);
440 static struct sort_entry sort_sym
= {
442 .cmp
= sort__sym_cmp
,
443 .print
= sort__sym_print
,
446 static int sort__need_collapse
= 0;
448 struct sort_dimension
{
450 struct sort_entry
*entry
;
454 static struct sort_dimension sort_dimensions
[] = {
455 { .name
= "pid", .entry
= &sort_thread
, },
456 { .name
= "comm", .entry
= &sort_comm
, },
457 { .name
= "dso", .entry
= &sort_dso
, },
458 { .name
= "symbol", .entry
= &sort_sym
, },
461 static LIST_HEAD(hist_entry__sort_list
);
463 static int sort_dimension__add(char *tok
)
467 for (i
= 0; i
< ARRAY_SIZE(sort_dimensions
); i
++) {
468 struct sort_dimension
*sd
= &sort_dimensions
[i
];
473 if (strncasecmp(tok
, sd
->name
, strlen(tok
)))
476 if (sd
->entry
->collapse
)
477 sort__need_collapse
= 1;
479 list_add_tail(&sd
->entry
->list
, &hist_entry__sort_list
);
489 hist_entry__cmp(struct hist_entry
*left
, struct hist_entry
*right
)
491 struct sort_entry
*se
;
494 list_for_each_entry(se
, &hist_entry__sort_list
, list
) {
495 cmp
= se
->cmp(left
, right
);
504 hist_entry__collapse(struct hist_entry
*left
, struct hist_entry
*right
)
506 struct sort_entry
*se
;
509 list_for_each_entry(se
, &hist_entry__sort_list
, list
) {
510 int64_t (*f
)(struct hist_entry
*, struct hist_entry
*);
512 f
= se
->collapse
?: se
->cmp
;
514 cmp
= f(left
, right
);
523 * collect histogram counts
525 static void hist_hit(struct hist_entry
*he
, u64 ip
)
527 unsigned int sym_size
, offset
;
528 struct symbol
*sym
= he
->sym
;
532 if (!sym
|| !sym
->hist
)
535 sym_size
= sym
->end
- sym
->start
;
536 offset
= ip
- sym
->start
;
538 if (offset
>= sym_size
)
545 printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n",
546 (void *)(unsigned long)he
->sym
->start
,
548 (void *)(unsigned long)ip
, ip
- he
->sym
->start
,
553 hist_entry__add(struct thread
*thread
, struct map
*map
, struct dso
*dso
,
554 struct symbol
*sym
, u64 ip
, char level
)
556 struct rb_node
**p
= &hist
.rb_node
;
557 struct rb_node
*parent
= NULL
;
558 struct hist_entry
*he
;
559 struct hist_entry entry
= {
572 he
= rb_entry(parent
, struct hist_entry
, rb_node
);
574 cmp
= hist_entry__cmp(&entry
, he
);
588 he
= malloc(sizeof(*he
));
592 rb_link_node(&he
->rb_node
, parent
, p
);
593 rb_insert_color(&he
->rb_node
, &hist
);
598 static void hist_entry__free(struct hist_entry
*he
)
604 * collapse the histogram
607 static struct rb_root collapse_hists
;
609 static void collapse__insert_entry(struct hist_entry
*he
)
611 struct rb_node
**p
= &collapse_hists
.rb_node
;
612 struct rb_node
*parent
= NULL
;
613 struct hist_entry
*iter
;
618 iter
= rb_entry(parent
, struct hist_entry
, rb_node
);
620 cmp
= hist_entry__collapse(iter
, he
);
623 iter
->count
+= he
->count
;
624 hist_entry__free(he
);
634 rb_link_node(&he
->rb_node
, parent
, p
);
635 rb_insert_color(&he
->rb_node
, &collapse_hists
);
638 static void collapse__resort(void)
640 struct rb_node
*next
;
641 struct hist_entry
*n
;
643 if (!sort__need_collapse
)
646 next
= rb_first(&hist
);
648 n
= rb_entry(next
, struct hist_entry
, rb_node
);
649 next
= rb_next(&n
->rb_node
);
651 rb_erase(&n
->rb_node
, &hist
);
652 collapse__insert_entry(n
);
657 * reverse the map, sort on count.
660 static struct rb_root output_hists
;
662 static void output__insert_entry(struct hist_entry
*he
)
664 struct rb_node
**p
= &output_hists
.rb_node
;
665 struct rb_node
*parent
= NULL
;
666 struct hist_entry
*iter
;
670 iter
= rb_entry(parent
, struct hist_entry
, rb_node
);
672 if (he
->count
> iter
->count
)
678 rb_link_node(&he
->rb_node
, parent
, p
);
679 rb_insert_color(&he
->rb_node
, &output_hists
);
682 static void output__resort(void)
684 struct rb_node
*next
;
685 struct hist_entry
*n
;
686 struct rb_root
*tree
= &hist
;
688 if (sort__need_collapse
)
689 tree
= &collapse_hists
;
691 next
= rb_first(tree
);
694 n
= rb_entry(next
, struct hist_entry
, rb_node
);
695 next
= rb_next(&n
->rb_node
);
697 rb_erase(&n
->rb_node
, tree
);
698 output__insert_entry(n
);
702 static void register_idle_thread(void)
704 struct thread
*thread
= threads__findnew(0);
706 if (thread
== NULL
||
707 thread__set_comm(thread
, "[idle]")) {
708 fprintf(stderr
, "problem inserting idle task.\n");
713 static unsigned long total
= 0,
720 process_sample_event(event_t
*event
, unsigned long offset
, unsigned long head
)
724 struct dso
*dso
= NULL
;
725 struct thread
*thread
= threads__findnew(event
->ip
.pid
);
726 u64 ip
= event
->ip
.ip
;
727 struct map
*map
= NULL
;
729 dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
730 (void *)(offset
+ head
),
731 (void *)(long)(event
->header
.size
),
736 dprintf(" ... thread: %s:%d\n", thread
->comm
, thread
->pid
);
738 if (thread
== NULL
) {
739 fprintf(stderr
, "problem processing %d event, skipping it.\n",
744 if (event
->header
.misc
& PERF_EVENT_MISC_KERNEL
) {
750 dprintf(" ...... dso: %s\n", dso
->name
);
752 } else if (event
->header
.misc
& PERF_EVENT_MISC_USER
) {
757 map
= thread__find_map(thread
, ip
);
759 ip
= map
->map_ip(map
, ip
);
763 * If this is outside of all known maps,
764 * and is a negative address, try to look it
765 * up in the kernel dso, as it might be a
766 * vsyscall (which executes in user-mode):
768 if ((long long)ip
< 0)
771 dprintf(" ...... dso: %s\n", dso
? dso
->name
: "<not found>");
776 dprintf(" ...... dso: [hypervisor]\n");
779 if (show
& show_mask
) {
780 struct symbol
*sym
= NULL
;
783 sym
= dso
->find_symbol(dso
, ip
);
785 if (hist_entry__add(thread
, map
, dso
, sym
, ip
, level
)) {
787 "problem incrementing symbol count, skipping event\n");
797 process_mmap_event(event_t
*event
, unsigned long offset
, unsigned long head
)
799 struct thread
*thread
= threads__findnew(event
->mmap
.pid
);
800 struct map
*map
= map__new(&event
->mmap
);
802 dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
803 (void *)(offset
+ head
),
804 (void *)(long)(event
->header
.size
),
806 (void *)(long)event
->mmap
.start
,
807 (void *)(long)event
->mmap
.len
,
808 (void *)(long)event
->mmap
.pgoff
,
809 event
->mmap
.filename
);
811 if (thread
== NULL
|| map
== NULL
) {
812 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
816 thread__insert_map(thread
, map
);
823 process_comm_event(event_t
*event
, unsigned long offset
, unsigned long head
)
825 struct thread
*thread
= threads__findnew(event
->comm
.pid
);
827 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
828 (void *)(offset
+ head
),
829 (void *)(long)(event
->header
.size
),
830 event
->comm
.comm
, event
->comm
.pid
);
832 if (thread
== NULL
||
833 thread__set_comm(thread
, event
->comm
.comm
)) {
834 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
843 process_fork_event(event_t
*event
, unsigned long offset
, unsigned long head
)
845 struct thread
*thread
= threads__findnew(event
->fork
.pid
);
846 struct thread
*parent
= threads__findnew(event
->fork
.ppid
);
848 dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
849 (void *)(offset
+ head
),
850 (void *)(long)(event
->header
.size
),
851 event
->fork
.pid
, event
->fork
.ppid
);
853 if (!thread
|| !parent
|| thread__fork(thread
, parent
)) {
854 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
863 process_event(event_t
*event
, unsigned long offset
, unsigned long head
)
865 switch (event
->header
.type
) {
866 case PERF_EVENT_SAMPLE
:
867 return process_sample_event(event
, offset
, head
);
869 case PERF_EVENT_MMAP
:
870 return process_mmap_event(event
, offset
, head
);
872 case PERF_EVENT_COMM
:
873 return process_comm_event(event
, offset
, head
);
875 case PERF_EVENT_FORK
:
876 return process_fork_event(event
, offset
, head
);
878 * We dont process them right now but they are fine:
881 case PERF_EVENT_THROTTLE
:
882 case PERF_EVENT_UNTHROTTLE
:
893 parse_line(FILE *file
, struct symbol
*sym
, u64 start
, u64 len
)
895 char *line
= NULL
, *tmp
, *tmp2
;
896 static const char *prev_line
;
897 static const char *prev_color
;
904 if (getline(&line
, &line_len
, file
) < 0)
909 c
= strchr(line
, '\n');
918 * Strip leading spaces:
929 * Parse hexa addresses followed by ':'
931 line_ip
= strtoull(tmp
, &tmp2
, 16);
937 const char *path
= NULL
;
938 unsigned int hits
= 0;
939 double percent
= 0.0;
941 struct sym_ext
*sym_ext
= sym
->priv
;
943 offset
= line_ip
- start
;
945 hits
= sym
->hist
[offset
];
947 if (offset
< len
&& sym_ext
) {
948 path
= sym_ext
[offset
].path
;
949 percent
= sym_ext
[offset
].percent
;
950 } else if (sym
->hist_sum
)
951 percent
= 100.0 * hits
/ sym
->hist_sum
;
953 color
= get_percent_color(percent
);
956 * Also color the filename and line if needed, with
957 * the same color than the percentage. Don't print it
958 * twice for close colored ip with the same filename:line
961 if (!prev_line
|| strcmp(prev_line
, path
)
962 || color
!= prev_color
) {
963 color_fprintf(stdout
, color
, " %s", path
);
969 color_fprintf(stdout
, color
, " %7.2f", percent
);
971 color_fprintf(stdout
, PERF_COLOR_BLUE
, "%s\n", line
);
976 printf(" : %s\n", line
);
982 static struct rb_root root_sym_ext
;
984 static void insert_source_line(struct sym_ext
*sym_ext
)
986 struct sym_ext
*iter
;
987 struct rb_node
**p
= &root_sym_ext
.rb_node
;
988 struct rb_node
*parent
= NULL
;
992 iter
= rb_entry(parent
, struct sym_ext
, node
);
994 if (sym_ext
->percent
> iter
->percent
)
1000 rb_link_node(&sym_ext
->node
, parent
, p
);
1001 rb_insert_color(&sym_ext
->node
, &root_sym_ext
);
1004 static void free_source_line(struct symbol
*sym
, int len
)
1006 struct sym_ext
*sym_ext
= sym
->priv
;
1012 for (i
= 0; i
< len
; i
++)
1013 free(sym_ext
[i
].path
);
1017 root_sym_ext
= RB_ROOT
;
1020 /* Get the filename:line for the colored entries */
1022 get_source_line(struct symbol
*sym
, u64 start
, int len
, char *filename
)
1025 char cmd
[PATH_MAX
* 2];
1026 struct sym_ext
*sym_ext
;
1031 sym
->priv
= calloc(len
, sizeof(struct sym_ext
));
1035 sym_ext
= sym
->priv
;
1037 for (i
= 0; i
< len
; i
++) {
1043 sym_ext
[i
].percent
= 100.0 * sym
->hist
[i
] / sym
->hist_sum
;
1044 if (sym_ext
[i
].percent
<= 0.5)
1048 sprintf(cmd
, "addr2line -e %s %016llx", filename
, offset
);
1049 fp
= popen(cmd
, "r");
1053 if (getline(&path
, &line_len
, fp
) < 0 || !line_len
)
1056 sym_ext
[i
].path
= malloc(sizeof(char) * line_len
+ 1);
1057 if (!sym_ext
[i
].path
)
1060 strcpy(sym_ext
[i
].path
, path
);
1061 insert_source_line(&sym_ext
[i
]);
1068 static void print_summary(char *filename
)
1070 struct sym_ext
*sym_ext
;
1071 struct rb_node
*node
;
1073 printf("\nSorted summary for file %s\n", filename
);
1074 printf("----------------------------------------------\n\n");
1076 if (RB_EMPTY_ROOT(&root_sym_ext
)) {
1077 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN
);
1081 node
= rb_first(&root_sym_ext
);
1087 sym_ext
= rb_entry(node
, struct sym_ext
, node
);
1088 percent
= sym_ext
->percent
;
1089 color
= get_percent_color(percent
);
1090 path
= sym_ext
->path
;
1092 color_fprintf(stdout
, color
, " %7.2f %s", percent
, path
);
1093 node
= rb_next(node
);
1097 static void annotate_sym(struct dso
*dso
, struct symbol
*sym
)
1099 char *filename
= dso
->name
, *d_filename
;
1100 u64 start
, end
, len
;
1101 char command
[PATH_MAX
*2];
1107 filename
= sym
->module
->path
;
1108 else if (dso
== kernel_dso
)
1111 start
= sym
->obj_start
;
1115 d_filename
= filename
;
1117 d_filename
= basename(filename
);
1119 end
= start
+ sym
->end
- sym
->start
+ 1;
1120 len
= sym
->end
- sym
->start
;
1123 get_source_line(sym
, start
, len
, filename
);
1124 print_summary(filename
);
1127 printf("\n\n------------------------------------------------\n");
1128 printf(" Percent | Source code & Disassembly of %s\n", d_filename
);
1129 printf("------------------------------------------------\n");
1132 printf("annotating [%p] %30s : [%p] %30s\n", dso
, dso
->name
, sym
, sym
->name
);
1134 sprintf(command
, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
1135 (u64
)start
, (u64
)end
, filename
, filename
);
1138 printf("doing: %s\n", command
);
1140 file
= popen(command
, "r");
1144 while (!feof(file
)) {
1145 if (parse_line(file
, sym
, start
, len
) < 0)
1151 free_source_line(sym
, len
);
1154 static void find_annotations(void)
1160 list_for_each_entry(dso
, &dsos
, node
) {
1162 for (nd
= rb_first(&dso
->syms
); nd
; nd
= rb_next(nd
)) {
1163 struct symbol
*sym
= rb_entry(nd
, struct symbol
, rb_node
);
1166 annotate_sym(dso
, sym
);
1173 printf(" Error: symbol '%s' not present amongst the samples.\n", sym_hist_filter
);
1176 static int __cmd_annotate(void)
1178 int ret
, rc
= EXIT_FAILURE
;
1179 unsigned long offset
= 0;
1180 unsigned long head
= 0;
1186 register_idle_thread();
1188 input
= open(input_name
, O_RDONLY
);
1190 perror("failed to open file");
1194 ret
= fstat(input
, &stat
);
1196 perror("failed to stat file");
1200 if (!stat
.st_size
) {
1201 fprintf(stderr
, "zero-sized file, nothing to do!\n");
1205 if (load_kernel() < 0) {
1206 perror("failed to load kernel symbols");
1207 return EXIT_FAILURE
;
1211 buf
= (char *)mmap(NULL
, page_size
* mmap_window
, PROT_READ
,
1212 MAP_SHARED
, input
, offset
);
1213 if (buf
== MAP_FAILED
) {
1214 perror("failed to mmap file");
1219 event
= (event_t
*)(buf
+ head
);
1221 size
= event
->header
.size
;
1225 if (head
+ event
->header
.size
>= page_size
* mmap_window
) {
1226 unsigned long shift
= page_size
* (head
/ page_size
);
1229 ret
= munmap(buf
, page_size
* mmap_window
);
1237 size
= event
->header
.size
;
1239 dprintf("%p [%p]: event: %d\n",
1240 (void *)(offset
+ head
),
1241 (void *)(long)event
->header
.size
,
1242 event
->header
.type
);
1244 if (!size
|| process_event(event
, offset
, head
) < 0) {
1246 dprintf("%p [%p]: skipping unknown header type: %d\n",
1247 (void *)(offset
+ head
),
1248 (void *)(long)(event
->header
.size
),
1249 event
->header
.type
);
1254 * assume we lost track of the stream, check alignment, and
1255 * increment a single u64 in the hope to catch on again 'soon'.
1258 if (unlikely(head
& 7))
1266 if (offset
+ head
< (unsigned long)stat
.st_size
)
1272 dprintf(" IP events: %10ld\n", total
);
1273 dprintf(" mmap events: %10ld\n", total_mmap
);
1274 dprintf(" comm events: %10ld\n", total_comm
);
1275 dprintf(" fork events: %10ld\n", total_fork
);
1276 dprintf(" unknown events: %10ld\n", total_unknown
);
1282 threads__fprintf(stdout
);
1285 dsos__fprintf(stdout
);
1295 static const char * const annotate_usage
[] = {
1296 "perf annotate [<options>] <command>",
1300 static const struct option options
[] = {
1301 OPT_STRING('i', "input", &input_name
, "file",
1303 OPT_STRING('s', "symbol", &sym_hist_filter
, "symbol",
1304 "symbol to annotate"),
1305 OPT_BOOLEAN('v', "verbose", &verbose
,
1306 "be more verbose (show symbol address, etc)"),
1307 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
1308 "dump raw trace in ASCII"),
1309 OPT_STRING('k', "vmlinux", &vmlinux
, "file", "vmlinux pathname"),
1310 OPT_BOOLEAN('m', "modules", &modules
,
1311 "load module symbols - WARNING: use only with -k and LIVE kernel"),
1312 OPT_BOOLEAN('l', "print-line", &print_line
,
1313 "print matching source lines (may be slow)"),
1314 OPT_BOOLEAN('P', "full-paths", &full_paths
,
1315 "Don't shorten the displayed pathnames"),
1319 static void setup_sorting(void)
1321 char *tmp
, *tok
, *str
= strdup(sort_order
);
1323 for (tok
= strtok_r(str
, ", ", &tmp
);
1324 tok
; tok
= strtok_r(NULL
, ", ", &tmp
)) {
1325 if (sort_dimension__add(tok
) < 0) {
1326 error("Unknown --sort key: `%s'", tok
);
1327 usage_with_options(annotate_usage
, options
);
1334 int cmd_annotate(int argc
, const char **argv
, const char *prefix __used
)
1338 page_size
= getpagesize();
1340 argc
= parse_options(argc
, argv
, options
, annotate_usage
, 0);
1346 * Special case: if there's an argument left then assume tha
1347 * it's a symbol filter:
1350 usage_with_options(annotate_usage
, options
);
1352 sym_hist_filter
= argv
[0];
1355 if (!sym_hist_filter
)
1356 usage_with_options(annotate_usage
, options
);
1360 return __cmd_annotate();