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"
32 static char const *input_name = "perf.data";
33 static char *vmlinux = "vmlinux";
35 static char default_sort_order[] = "comm,symbol";
36 static char *sort_order = default_sort_order;
39 static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
41 static int dump_trace = 0;
42 #define dprintf(x...) do { if (dump_trace) printf(x); } while (0)
48 static int full_paths;
50 static int print_line;
52 static unsigned long page_size;
53 static unsigned long mmap_window = 32;
56 struct perf_event_header header;
62 struct perf_event_header header;
67 char filename[PATH_MAX];
71 struct perf_event_header header;
77 struct perf_event_header header;
82 struct perf_event_header header;
88 typedef union event_union {
89 struct perf_event_header header;
91 struct mmap_event mmap;
92 struct comm_event comm;
93 struct fork_event fork;
94 struct period_event period;
104 static LIST_HEAD(dsos);
105 static struct dso *kernel_dso;
106 static struct dso *vdso;
109 static void dsos__add(struct dso *dso)
111 list_add_tail(&dso->node, &dsos);
114 static struct dso *dsos__find(const char *name)
118 list_for_each_entry(pos, &dsos, node)
119 if (strcmp(pos->name, name) == 0)
124 static struct dso *dsos__findnew(const char *name)
126 struct dso *dso = dsos__find(name);
132 dso = dso__new(name, 0);
136 nr = dso__load(dso, NULL, verbose);
139 fprintf(stderr, "Failed to open: %s\n", name);
142 if (!nr && verbose) {
144 "No symbols found in: %s, maybe install a debug package?\n",
157 static void dsos__fprintf(FILE *fp)
161 list_for_each_entry(pos, &dsos, node)
162 dso__fprintf(pos, fp);
165 static struct symbol *vdso__find_symbol(struct dso *dso, u64 ip)
167 return dso__find_symbol(dso, ip);
170 static int load_kernel(void)
174 kernel_dso = dso__new("[kernel]", 0);
178 err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose, modules);
180 dso__delete(kernel_dso);
183 dsos__add(kernel_dso);
185 vdso = dso__new("[vdso]", 0);
189 vdso->find_symbol = vdso__find_symbol;
197 struct list_head node;
201 u64 (*map_ip)(struct map *, u64);
205 static u64 map__map_ip(struct map *map, u64 ip)
207 return ip - map->start + map->pgoff;
210 static u64 vdso__map_ip(struct map *map __used, u64 ip)
215 static struct map *map__new(struct mmap_event *event)
217 struct map *self = malloc(sizeof(*self));
220 const char *filename = event->filename;
222 self->start = event->start;
223 self->end = event->start + event->len;
224 self->pgoff = event->pgoff;
226 self->dso = dsos__findnew(filename);
227 if (self->dso == NULL)
230 if (self->dso == vdso)
231 self->map_ip = vdso__map_ip;
233 self->map_ip = map__map_ip;
241 static struct map *map__clone(struct map *self)
243 struct map *map = malloc(sizeof(*self));
248 memcpy(map, self, sizeof(*self));
253 static int map__overlap(struct map *l, struct map *r)
255 if (l->start > r->start) {
261 if (l->end > r->start)
267 static size_t map__fprintf(struct map *self, FILE *fp)
269 return fprintf(fp, " %Lx-%Lx %Lx %s\n",
270 self->start, self->end, self->pgoff, self->dso->name);
275 struct rb_node rb_node;
276 struct list_head maps;
281 static struct thread *thread__new(pid_t pid)
283 struct thread *self = malloc(sizeof(*self));
287 self->comm = malloc(32);
289 snprintf(self->comm, 32, ":%d", self->pid);
290 INIT_LIST_HEAD(&self->maps);
296 static int thread__set_comm(struct thread *self, const char *comm)
300 self->comm = strdup(comm);
301 return self->comm ? 0 : -ENOMEM;
304 static size_t thread__fprintf(struct thread *self, FILE *fp)
307 size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
309 list_for_each_entry(pos, &self->maps, node)
310 ret += map__fprintf(pos, fp);
316 static struct rb_root threads;
317 static struct thread *last_match;
319 static struct thread *threads__findnew(pid_t pid)
321 struct rb_node **p = &threads.rb_node;
322 struct rb_node *parent = NULL;
326 * Font-end cache - PID lookups come in blocks,
327 * so most of the time we dont have to look up
330 if (last_match && last_match->pid == pid)
335 th = rb_entry(parent, struct thread, rb_node);
337 if (th->pid == pid) {
348 th = thread__new(pid);
350 rb_link_node(&th->rb_node, parent, p);
351 rb_insert_color(&th->rb_node, &threads);
358 static void thread__insert_map(struct thread *self, struct map *map)
360 struct map *pos, *tmp;
362 list_for_each_entry_safe(pos, tmp, &self->maps, node) {
363 if (map__overlap(pos, map)) {
364 list_del_init(&pos->node);
370 list_add_tail(&map->node, &self->maps);
373 static int thread__fork(struct thread *self, struct thread *parent)
379 self->comm = strdup(parent->comm);
383 list_for_each_entry(map, &parent->maps, node) {
384 struct map *new = map__clone(map);
387 thread__insert_map(self, new);
393 static struct map *thread__find_map(struct thread *self, u64 ip)
400 list_for_each_entry(pos, &self->maps, node)
401 if (ip >= pos->start && ip <= pos->end)
407 static size_t threads__fprintf(FILE *fp)
412 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
413 struct thread *pos = rb_entry(nd, struct thread, rb_node);
415 ret += thread__fprintf(pos, fp);
422 * histogram, sorted on item, collects counts
425 static struct rb_root hist;
428 struct rb_node rb_node;
430 struct thread *thread;
441 * configurable sorting bits
445 struct list_head list;
449 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
450 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
451 size_t (*print)(FILE *fp, struct hist_entry *);
457 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
459 return right->thread->pid - left->thread->pid;
463 sort__thread_print(FILE *fp, struct hist_entry *self)
465 return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
468 static struct sort_entry sort_thread = {
469 .header = " Command: Pid",
470 .cmp = sort__thread_cmp,
471 .print = sort__thread_print,
477 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
479 return right->thread->pid - left->thread->pid;
483 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
485 char *comm_l = left->thread->comm;
486 char *comm_r = right->thread->comm;
488 if (!comm_l || !comm_r) {
489 if (!comm_l && !comm_r)
497 return strcmp(comm_l, comm_r);
501 sort__comm_print(FILE *fp, struct hist_entry *self)
503 return fprintf(fp, "%16s", self->thread->comm);
506 static struct sort_entry sort_comm = {
507 .header = " Command",
508 .cmp = sort__comm_cmp,
509 .collapse = sort__comm_collapse,
510 .print = sort__comm_print,
516 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
518 struct dso *dso_l = left->dso;
519 struct dso *dso_r = right->dso;
521 if (!dso_l || !dso_r) {
522 if (!dso_l && !dso_r)
530 return strcmp(dso_l->name, dso_r->name);
534 sort__dso_print(FILE *fp, struct hist_entry *self)
537 return fprintf(fp, "%-25s", self->dso->name);
539 return fprintf(fp, "%016llx ", (u64)self->ip);
542 static struct sort_entry sort_dso = {
543 .header = "Shared Object ",
544 .cmp = sort__dso_cmp,
545 .print = sort__dso_print,
551 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
555 if (left->sym == right->sym)
558 ip_l = left->sym ? left->sym->start : left->ip;
559 ip_r = right->sym ? right->sym->start : right->ip;
561 return (int64_t)(ip_r - ip_l);
565 sort__sym_print(FILE *fp, struct hist_entry *self)
570 ret += fprintf(fp, "%#018llx ", (u64)self->ip);
573 ret += fprintf(fp, "[%c] %s",
574 self->dso == kernel_dso ? 'k' : '.', self->sym->name);
576 ret += fprintf(fp, "%#016llx", (u64)self->ip);
582 static struct sort_entry sort_sym = {
584 .cmp = sort__sym_cmp,
585 .print = sort__sym_print,
588 static int sort__need_collapse = 0;
590 struct sort_dimension {
592 struct sort_entry *entry;
596 static struct sort_dimension sort_dimensions[] = {
597 { .name = "pid", .entry = &sort_thread, },
598 { .name = "comm", .entry = &sort_comm, },
599 { .name = "dso", .entry = &sort_dso, },
600 { .name = "symbol", .entry = &sort_sym, },
603 static LIST_HEAD(hist_entry__sort_list);
605 static int sort_dimension__add(char *tok)
609 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
610 struct sort_dimension *sd = &sort_dimensions[i];
615 if (strncasecmp(tok, sd->name, strlen(tok)))
618 if (sd->entry->collapse)
619 sort__need_collapse = 1;
621 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
631 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
633 struct sort_entry *se;
636 list_for_each_entry(se, &hist_entry__sort_list, list) {
637 cmp = se->cmp(left, right);
646 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
648 struct sort_entry *se;
651 list_for_each_entry(se, &hist_entry__sort_list, list) {
652 int64_t (*f)(struct hist_entry *, struct hist_entry *);
654 f = se->collapse ?: se->cmp;
656 cmp = f(left, right);
665 * collect histogram counts
667 static void hist_hit(struct hist_entry *he, u64 ip)
669 unsigned int sym_size, offset;
670 struct symbol *sym = he->sym;
674 if (!sym || !sym->hist)
677 sym_size = sym->end - sym->start;
678 offset = ip - sym->start;
680 if (offset >= sym_size)
687 printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n",
688 (void *)(unsigned long)he->sym->start,
690 (void *)(unsigned long)ip, ip - he->sym->start,
695 hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
696 struct symbol *sym, u64 ip, char level)
698 struct rb_node **p = &hist.rb_node;
699 struct rb_node *parent = NULL;
700 struct hist_entry *he;
701 struct hist_entry entry = {
714 he = rb_entry(parent, struct hist_entry, rb_node);
716 cmp = hist_entry__cmp(&entry, he);
730 he = malloc(sizeof(*he));
734 rb_link_node(&he->rb_node, parent, p);
735 rb_insert_color(&he->rb_node, &hist);
740 static void hist_entry__free(struct hist_entry *he)
746 * collapse the histogram
749 static struct rb_root collapse_hists;
751 static void collapse__insert_entry(struct hist_entry *he)
753 struct rb_node **p = &collapse_hists.rb_node;
754 struct rb_node *parent = NULL;
755 struct hist_entry *iter;
760 iter = rb_entry(parent, struct hist_entry, rb_node);
762 cmp = hist_entry__collapse(iter, he);
765 iter->count += he->count;
766 hist_entry__free(he);
776 rb_link_node(&he->rb_node, parent, p);
777 rb_insert_color(&he->rb_node, &collapse_hists);
780 static void collapse__resort(void)
782 struct rb_node *next;
783 struct hist_entry *n;
785 if (!sort__need_collapse)
788 next = rb_first(&hist);
790 n = rb_entry(next, struct hist_entry, rb_node);
791 next = rb_next(&n->rb_node);
793 rb_erase(&n->rb_node, &hist);
794 collapse__insert_entry(n);
799 * reverse the map, sort on count.
802 static struct rb_root output_hists;
804 static void output__insert_entry(struct hist_entry *he)
806 struct rb_node **p = &output_hists.rb_node;
807 struct rb_node *parent = NULL;
808 struct hist_entry *iter;
812 iter = rb_entry(parent, struct hist_entry, rb_node);
814 if (he->count > iter->count)
820 rb_link_node(&he->rb_node, parent, p);
821 rb_insert_color(&he->rb_node, &output_hists);
824 static void output__resort(void)
826 struct rb_node *next;
827 struct hist_entry *n;
828 struct rb_root *tree = &hist;
830 if (sort__need_collapse)
831 tree = &collapse_hists;
833 next = rb_first(tree);
836 n = rb_entry(next, struct hist_entry, rb_node);
837 next = rb_next(&n->rb_node);
839 rb_erase(&n->rb_node, tree);
840 output__insert_entry(n);
844 static void register_idle_thread(void)
846 struct thread *thread = threads__findnew(0);
848 if (thread == NULL ||
849 thread__set_comm(thread, "[idle]")) {
850 fprintf(stderr, "problem inserting idle task.\n");
855 static unsigned long total = 0,
862 process_sample_event(event_t *event, unsigned long offset, unsigned long head)
866 struct dso *dso = NULL;
867 struct thread *thread = threads__findnew(event->ip.pid);
868 u64 ip = event->ip.ip;
869 struct map *map = NULL;
871 dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
872 (void *)(offset + head),
873 (void *)(long)(event->header.size),
878 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
880 if (thread == NULL) {
881 fprintf(stderr, "problem processing %d event, skipping it.\n",
886 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
892 dprintf(" ...... dso: %s\n", dso->name);
894 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
899 map = thread__find_map(thread, ip);
901 ip = map->map_ip(map, ip);
905 * If this is outside of all known maps,
906 * and is a negative address, try to look it
907 * up in the kernel dso, as it might be a
908 * vsyscall (which executes in user-mode):
910 if ((long long)ip < 0)
913 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
918 dprintf(" ...... dso: [hypervisor]\n");
921 if (show & show_mask) {
922 struct symbol *sym = NULL;
925 sym = dso->find_symbol(dso, ip);
927 if (hist_entry__add(thread, map, dso, sym, ip, level)) {
929 "problem incrementing symbol count, skipping event\n");
939 process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
941 struct thread *thread = threads__findnew(event->mmap.pid);
942 struct map *map = map__new(&event->mmap);
944 dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
945 (void *)(offset + head),
946 (void *)(long)(event->header.size),
948 (void *)(long)event->mmap.start,
949 (void *)(long)event->mmap.len,
950 (void *)(long)event->mmap.pgoff,
951 event->mmap.filename);
953 if (thread == NULL || map == NULL) {
954 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
958 thread__insert_map(thread, map);
965 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
967 struct thread *thread = threads__findnew(event->comm.pid);
969 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
970 (void *)(offset + head),
971 (void *)(long)(event->header.size),
972 event->comm.comm, event->comm.pid);
974 if (thread == NULL ||
975 thread__set_comm(thread, event->comm.comm)) {
976 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
985 process_fork_event(event_t *event, unsigned long offset, unsigned long head)
987 struct thread *thread = threads__findnew(event->fork.pid);
988 struct thread *parent = threads__findnew(event->fork.ppid);
990 dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
991 (void *)(offset + head),
992 (void *)(long)(event->header.size),
993 event->fork.pid, event->fork.ppid);
995 if (!thread || !parent || thread__fork(thread, parent)) {
996 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
1005 process_period_event(event_t *event, unsigned long offset, unsigned long head)
1007 dprintf("%p [%p]: PERF_EVENT_PERIOD: time:%Ld, id:%Ld: period:%Ld\n",
1008 (void *)(offset + head),
1009 (void *)(long)(event->header.size),
1012 event->period.sample_period);
1018 process_event(event_t *event, unsigned long offset, unsigned long head)
1020 switch (event->header.type) {
1021 case PERF_EVENT_SAMPLE:
1022 return process_sample_event(event, offset, head);
1024 case PERF_EVENT_MMAP:
1025 return process_mmap_event(event, offset, head);
1027 case PERF_EVENT_COMM:
1028 return process_comm_event(event, offset, head);
1030 case PERF_EVENT_FORK:
1031 return process_fork_event(event, offset, head);
1033 case PERF_EVENT_PERIOD:
1034 return process_period_event(event, offset, head);
1036 * We dont process them right now but they are fine:
1039 case PERF_EVENT_THROTTLE:
1040 case PERF_EVENT_UNTHROTTLE:
1050 static char *get_color(double percent)
1052 char *color = PERF_COLOR_NORMAL;
1055 * We color high-overhead entries in red, mid-overhead
1056 * entries in green - and keep the low overhead places
1059 if (percent >= MIN_RED)
1060 color = PERF_COLOR_RED;
1062 if (percent > MIN_GREEN)
1063 color = PERF_COLOR_GREEN;
1069 parse_line(FILE *file, struct symbol *sym, u64 start, u64 len)
1071 char *line = NULL, *tmp, *tmp2;
1072 static const char *prev_line;
1073 static const char *prev_color;
1074 unsigned int offset;
1080 if (getline(&line, &line_len, file) < 0)
1085 c = strchr(line, '\n');
1094 * Strip leading spaces:
1105 * Parse hexa addresses followed by ':'
1107 line_ip = strtoull(tmp, &tmp2, 16);
1112 if (line_ip != -1) {
1113 const char *path = NULL;
1114 unsigned int hits = 0;
1115 double percent = 0.0;
1117 struct sym_ext *sym_ext = sym->priv;
1119 offset = line_ip - start;
1121 hits = sym->hist[offset];
1123 if (offset < len && sym_ext) {
1124 path = sym_ext[offset].path;
1125 percent = sym_ext[offset].percent;
1126 } else if (sym->hist_sum)
1127 percent = 100.0 * hits / sym->hist_sum;
1129 color = get_color(percent);
1132 * Also color the filename and line if needed, with
1133 * the same color than the percentage. Don't print it
1134 * twice for close colored ip with the same filename:line
1137 if (!prev_line || strcmp(prev_line, path)
1138 || color != prev_color) {
1139 color_fprintf(stdout, color, " %s", path);
1145 color_fprintf(stdout, color, " %7.2f", percent);
1147 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", line);
1152 printf(" : %s\n", line);
1158 static struct rb_root root_sym_ext;
1160 static void insert_source_line(struct sym_ext *sym_ext)
1162 struct sym_ext *iter;
1163 struct rb_node **p = &root_sym_ext.rb_node;
1164 struct rb_node *parent = NULL;
1166 while (*p != NULL) {
1168 iter = rb_entry(parent, struct sym_ext, node);
1170 if (sym_ext->percent > iter->percent)
1173 p = &(*p)->rb_right;
1176 rb_link_node(&sym_ext->node, parent, p);
1177 rb_insert_color(&sym_ext->node, &root_sym_ext);
1180 static void free_source_line(struct symbol *sym, int len)
1182 struct sym_ext *sym_ext = sym->priv;
1188 for (i = 0; i < len; i++)
1189 free(sym_ext[i].path);
1193 root_sym_ext = RB_ROOT;
1196 /* Get the filename:line for the colored entries */
1198 get_source_line(struct symbol *sym, u64 start, int len, char *filename)
1201 char cmd[PATH_MAX * 2];
1202 struct sym_ext *sym_ext;
1207 sym->priv = calloc(len, sizeof(struct sym_ext));
1211 sym_ext = sym->priv;
1213 for (i = 0; i < len; i++) {
1219 sym_ext[i].percent = 100.0 * sym->hist[i] / sym->hist_sum;
1220 if (sym_ext[i].percent <= 0.5)
1224 sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
1225 fp = popen(cmd, "r");
1229 if (getline(&path, &line_len, fp) < 0 || !line_len)
1232 sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
1233 if (!sym_ext[i].path)
1236 strcpy(sym_ext[i].path, path);
1237 insert_source_line(&sym_ext[i]);
1244 static void print_summary(char *filename)
1246 struct sym_ext *sym_ext;
1247 struct rb_node *node;
1249 printf("\nSorted summary for file %s\n", filename);
1250 printf("----------------------------------------------\n\n");
1252 if (RB_EMPTY_ROOT(&root_sym_ext)) {
1253 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
1257 node = rb_first(&root_sym_ext);
1263 sym_ext = rb_entry(node, struct sym_ext, node);
1264 percent = sym_ext->percent;
1265 color = get_color(percent);
1266 path = sym_ext->path;
1268 color_fprintf(stdout, color, " %7.2f %s", percent, path);
1269 node = rb_next(node);
1273 static void annotate_sym(struct dso *dso, struct symbol *sym)
1275 char *filename = dso->name, *d_filename;
1276 u64 start, end, len;
1277 char command[PATH_MAX*2];
1283 filename = sym->module->path;
1284 else if (dso == kernel_dso)
1287 start = sym->obj_start;
1291 d_filename = filename;
1293 d_filename = basename(filename);
1295 end = start + sym->end - sym->start + 1;
1296 len = sym->end - sym->start;
1299 get_source_line(sym, start, len, filename);
1300 print_summary(filename);
1303 printf("\n\n------------------------------------------------\n");
1304 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
1305 printf("------------------------------------------------\n");
1308 printf("annotating [%p] %30s : [%p] %30s\n", dso, dso->name, sym, sym->name);
1310 sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
1311 (u64)start, (u64)end, filename, filename);
1314 printf("doing: %s\n", command);
1316 file = popen(command, "r");
1320 while (!feof(file)) {
1321 if (parse_line(file, sym, start, len) < 0)
1327 free_source_line(sym, len);
1330 static void find_annotations(void)
1336 list_for_each_entry(dso, &dsos, node) {
1338 for (nd = rb_first(&dso->syms); nd; nd = rb_next(nd)) {
1339 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
1342 annotate_sym(dso, sym);
1349 printf(" Error: symbol '%s' not present amongst the samples.\n", sym_hist_filter);
1352 static int __cmd_annotate(void)
1354 int ret, rc = EXIT_FAILURE;
1355 unsigned long offset = 0;
1356 unsigned long head = 0;
1362 register_idle_thread();
1364 input = open(input_name, O_RDONLY);
1366 perror("failed to open file");
1370 ret = fstat(input, &stat);
1372 perror("failed to stat file");
1376 if (!stat.st_size) {
1377 fprintf(stderr, "zero-sized file, nothing to do!\n");
1381 if (load_kernel() < 0) {
1382 perror("failed to load kernel symbols");
1383 return EXIT_FAILURE;
1387 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1388 MAP_SHARED, input, offset);
1389 if (buf == MAP_FAILED) {
1390 perror("failed to mmap file");
1395 event = (event_t *)(buf + head);
1397 size = event->header.size;
1401 if (head + event->header.size >= page_size * mmap_window) {
1402 unsigned long shift = page_size * (head / page_size);
1405 ret = munmap(buf, page_size * mmap_window);
1413 size = event->header.size;
1415 dprintf("%p [%p]: event: %d\n",
1416 (void *)(offset + head),
1417 (void *)(long)event->header.size,
1418 event->header.type);
1420 if (!size || process_event(event, offset, head) < 0) {
1422 dprintf("%p [%p]: skipping unknown header type: %d\n",
1423 (void *)(offset + head),
1424 (void *)(long)(event->header.size),
1425 event->header.type);
1430 * assume we lost track of the stream, check alignment, and
1431 * increment a single u64 in the hope to catch on again 'soon'.
1434 if (unlikely(head & 7))
1442 if (offset + head < (unsigned long)stat.st_size)
1448 dprintf(" IP events: %10ld\n", total);
1449 dprintf(" mmap events: %10ld\n", total_mmap);
1450 dprintf(" comm events: %10ld\n", total_comm);
1451 dprintf(" fork events: %10ld\n", total_fork);
1452 dprintf(" unknown events: %10ld\n", total_unknown);
1458 threads__fprintf(stdout);
1461 dsos__fprintf(stdout);
1471 static const char * const annotate_usage[] = {
1472 "perf annotate [<options>] <command>",
1476 static const struct option options[] = {
1477 OPT_STRING('i', "input", &input_name, "file",
1479 OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
1480 "symbol to annotate"),
1481 OPT_BOOLEAN('v', "verbose", &verbose,
1482 "be more verbose (show symbol address, etc)"),
1483 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1484 "dump raw trace in ASCII"),
1485 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
1486 OPT_BOOLEAN('m', "modules", &modules,
1487 "load module symbols - WARNING: use only with -k and LIVE kernel"),
1488 OPT_BOOLEAN('l', "print-line", &print_line,
1489 "print matching source lines (may be slow)"),
1490 OPT_BOOLEAN('P', "full-paths", &full_paths,
1491 "Don't shorten the displayed pathnames"),
1495 static void setup_sorting(void)
1497 char *tmp, *tok, *str = strdup(sort_order);
1499 for (tok = strtok_r(str, ", ", &tmp);
1500 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1501 if (sort_dimension__add(tok) < 0) {
1502 error("Unknown --sort key: `%s'", tok);
1503 usage_with_options(annotate_usage, options);
1510 int cmd_annotate(int argc, const char **argv, const char *prefix __used)
1514 page_size = getpagesize();
1516 argc = parse_options(argc, argv, options, annotate_usage, 0);
1522 * Special case: if there's an argument left then assume tha
1523 * it's a symbol filter:
1526 usage_with_options(annotate_usage, options);
1528 sym_hist_filter = argv[0];
1531 if (!sym_hist_filter)
1532 usage_with_options(annotate_usage, options);
1536 return __cmd_annotate();