aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf/builtin-annotate.c
diff options
context:
space:
mode:
authorIngo Molnar <mingo@elte.hu>2009-06-06 14:33:43 -0400
committerIngo Molnar <mingo@elte.hu>2009-06-06 14:33:43 -0400
commit864709302a80f26fa9da3be5b47304f0b8bae192 (patch)
tree8c2bab78f141fe43a38914bd3e3aae0a88f958e5 /tools/perf/builtin-annotate.c
parent75b5032212641f6d38ac041416945e70da833b68 (diff)
perf_counter tools: Move from Documentation/perf_counter/ to tools/perf/
Several people have suggested that 'perf' has become a full-fledged tool that should be moved out of Documentation/. Move it to the (new) tools/ directory. Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: Mike Galbraith <efault@gmx.de> Cc: Paul Mackerras <paulus@samba.org> Cc: Arnaldo Carvalho de Melo <acme@redhat.com> LKML-Reference: <new-submission> Signed-off-by: Ingo Molnar <mingo@elte.hu>
Diffstat (limited to 'tools/perf/builtin-annotate.c')
-rw-r--r--tools/perf/builtin-annotate.c1355
1 files changed, 1355 insertions, 0 deletions
diff --git a/tools/perf/builtin-annotate.c b/tools/perf/builtin-annotate.c
new file mode 100644
index 000000000000..116a3978b44c
--- /dev/null
+++ b/tools/perf/builtin-annotate.c
@@ -0,0 +1,1355 @@
1/*
2 * builtin-annotate.c
3 *
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.
7 */
8#include "builtin.h"
9
10#include "util/util.h"
11
12#include "util/color.h"
13#include "util/list.h"
14#include "util/cache.h"
15#include "util/rbtree.h"
16#include "util/symbol.h"
17#include "util/string.h"
18
19#include "perf.h"
20
21#include "util/parse-options.h"
22#include "util/parse-events.h"
23
24#define SHOW_KERNEL 1
25#define SHOW_USER 2
26#define SHOW_HV 4
27
28static char const *input_name = "perf.data";
29static char *vmlinux = NULL;
30
31static char default_sort_order[] = "comm,symbol";
32static char *sort_order = default_sort_order;
33
34static int input;
35static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
36
37static int dump_trace = 0;
38#define dprintf(x...) do { if (dump_trace) printf(x); } while (0)
39
40static int verbose;
41
42static unsigned long page_size;
43static unsigned long mmap_window = 32;
44
45struct ip_event {
46 struct perf_event_header header;
47 __u64 ip;
48 __u32 pid, tid;
49};
50
51struct mmap_event {
52 struct perf_event_header header;
53 __u32 pid, tid;
54 __u64 start;
55 __u64 len;
56 __u64 pgoff;
57 char filename[PATH_MAX];
58};
59
60struct comm_event {
61 struct perf_event_header header;
62 __u32 pid, tid;
63 char comm[16];
64};
65
66struct fork_event {
67 struct perf_event_header header;
68 __u32 pid, ppid;
69};
70
71struct period_event {
72 struct perf_event_header header;
73 __u64 time;
74 __u64 id;
75 __u64 sample_period;
76};
77
78typedef union event_union {
79 struct perf_event_header header;
80 struct ip_event ip;
81 struct mmap_event mmap;
82 struct comm_event comm;
83 struct fork_event fork;
84 struct period_event period;
85} event_t;
86
87static LIST_HEAD(dsos);
88static struct dso *kernel_dso;
89static struct dso *vdso;
90
91
92static void dsos__add(struct dso *dso)
93{
94 list_add_tail(&dso->node, &dsos);
95}
96
97static struct dso *dsos__find(const char *name)
98{
99 struct dso *pos;
100
101 list_for_each_entry(pos, &dsos, node)
102 if (strcmp(pos->name, name) == 0)
103 return pos;
104 return NULL;
105}
106
107static struct dso *dsos__findnew(const char *name)
108{
109 struct dso *dso = dsos__find(name);
110 int nr;
111
112 if (dso)
113 return dso;
114
115 dso = dso__new(name, 0);
116 if (!dso)
117 goto out_delete_dso;
118
119 nr = dso__load(dso, NULL, verbose);
120 if (nr < 0) {
121 if (verbose)
122 fprintf(stderr, "Failed to open: %s\n", name);
123 goto out_delete_dso;
124 }
125 if (!nr && verbose) {
126 fprintf(stderr,
127 "No symbols found in: %s, maybe install a debug package?\n",
128 name);
129 }
130
131 dsos__add(dso);
132
133 return dso;
134
135out_delete_dso:
136 dso__delete(dso);
137 return NULL;
138}
139
140static void dsos__fprintf(FILE *fp)
141{
142 struct dso *pos;
143
144 list_for_each_entry(pos, &dsos, node)
145 dso__fprintf(pos, fp);
146}
147
148static struct symbol *vdso__find_symbol(struct dso *dso, uint64_t ip)
149{
150 return dso__find_symbol(kernel_dso, ip);
151}
152
153static int load_kernel(void)
154{
155 int err;
156
157 kernel_dso = dso__new("[kernel]", 0);
158 if (!kernel_dso)
159 return -1;
160
161 err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose);
162 if (err) {
163 dso__delete(kernel_dso);
164 kernel_dso = NULL;
165 } else
166 dsos__add(kernel_dso);
167
168 vdso = dso__new("[vdso]", 0);
169 if (!vdso)
170 return -1;
171
172 vdso->find_symbol = vdso__find_symbol;
173
174 dsos__add(vdso);
175
176 return err;
177}
178
179struct map {
180 struct list_head node;
181 uint64_t start;
182 uint64_t end;
183 uint64_t pgoff;
184 uint64_t (*map_ip)(struct map *, uint64_t);
185 struct dso *dso;
186};
187
188static uint64_t map__map_ip(struct map *map, uint64_t ip)
189{
190 return ip - map->start + map->pgoff;
191}
192
193static uint64_t vdso__map_ip(struct map *map, uint64_t ip)
194{
195 return ip;
196}
197
198static struct map *map__new(struct mmap_event *event)
199{
200 struct map *self = malloc(sizeof(*self));
201
202 if (self != NULL) {
203 const char *filename = event->filename;
204
205 self->start = event->start;
206 self->end = event->start + event->len;
207 self->pgoff = event->pgoff;
208
209 self->dso = dsos__findnew(filename);
210 if (self->dso == NULL)
211 goto out_delete;
212
213 if (self->dso == vdso)
214 self->map_ip = vdso__map_ip;
215 else
216 self->map_ip = map__map_ip;
217 }
218 return self;
219out_delete:
220 free(self);
221 return NULL;
222}
223
224static struct map *map__clone(struct map *self)
225{
226 struct map *map = malloc(sizeof(*self));
227
228 if (!map)
229 return NULL;
230
231 memcpy(map, self, sizeof(*self));
232
233 return map;
234}
235
236static int map__overlap(struct map *l, struct map *r)
237{
238 if (l->start > r->start) {
239 struct map *t = l;
240 l = r;
241 r = t;
242 }
243
244 if (l->end > r->start)
245 return 1;
246
247 return 0;
248}
249
250static size_t map__fprintf(struct map *self, FILE *fp)
251{
252 return fprintf(fp, " %"PRIx64"-%"PRIx64" %"PRIx64" %s\n",
253 self->start, self->end, self->pgoff, self->dso->name);
254}
255
256
257struct thread {
258 struct rb_node rb_node;
259 struct list_head maps;
260 pid_t pid;
261 char *comm;
262};
263
264static struct thread *thread__new(pid_t pid)
265{
266 struct thread *self = malloc(sizeof(*self));
267
268 if (self != NULL) {
269 self->pid = pid;
270 self->comm = malloc(32);
271 if (self->comm)
272 snprintf(self->comm, 32, ":%d", self->pid);
273 INIT_LIST_HEAD(&self->maps);
274 }
275
276 return self;
277}
278
279static int thread__set_comm(struct thread *self, const char *comm)
280{
281 if (self->comm)
282 free(self->comm);
283 self->comm = strdup(comm);
284 return self->comm ? 0 : -ENOMEM;
285}
286
287static size_t thread__fprintf(struct thread *self, FILE *fp)
288{
289 struct map *pos;
290 size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
291
292 list_for_each_entry(pos, &self->maps, node)
293 ret += map__fprintf(pos, fp);
294
295 return ret;
296}
297
298
299static struct rb_root threads;
300static struct thread *last_match;
301
302static struct thread *threads__findnew(pid_t pid)
303{
304 struct rb_node **p = &threads.rb_node;
305 struct rb_node *parent = NULL;
306 struct thread *th;
307
308 /*
309 * Font-end cache - PID lookups come in blocks,
310 * so most of the time we dont have to look up
311 * the full rbtree:
312 */
313 if (last_match && last_match->pid == pid)
314 return last_match;
315
316 while (*p != NULL) {
317 parent = *p;
318 th = rb_entry(parent, struct thread, rb_node);
319
320 if (th->pid == pid) {
321 last_match = th;
322 return th;
323 }
324
325 if (pid < th->pid)
326 p = &(*p)->rb_left;
327 else
328 p = &(*p)->rb_right;
329 }
330
331 th = thread__new(pid);
332 if (th != NULL) {
333 rb_link_node(&th->rb_node, parent, p);
334 rb_insert_color(&th->rb_node, &threads);
335 last_match = th;
336 }
337
338 return th;
339}
340
341static void thread__insert_map(struct thread *self, struct map *map)
342{
343 struct map *pos, *tmp;
344
345 list_for_each_entry_safe(pos, tmp, &self->maps, node) {
346 if (map__overlap(pos, map)) {
347 list_del_init(&pos->node);
348 /* XXX leaks dsos */
349 free(pos);
350 }
351 }
352
353 list_add_tail(&map->node, &self->maps);
354}
355
356static int thread__fork(struct thread *self, struct thread *parent)
357{
358 struct map *map;
359
360 if (self->comm)
361 free(self->comm);
362 self->comm = strdup(parent->comm);
363 if (!self->comm)
364 return -ENOMEM;
365
366 list_for_each_entry(map, &parent->maps, node) {
367 struct map *new = map__clone(map);
368 if (!new)
369 return -ENOMEM;
370 thread__insert_map(self, new);
371 }
372
373 return 0;
374}
375
376static struct map *thread__find_map(struct thread *self, uint64_t ip)
377{
378 struct map *pos;
379
380 if (self == NULL)
381 return NULL;
382
383 list_for_each_entry(pos, &self->maps, node)
384 if (ip >= pos->start && ip <= pos->end)
385 return pos;
386
387 return NULL;
388}
389
390static size_t threads__fprintf(FILE *fp)
391{
392 size_t ret = 0;
393 struct rb_node *nd;
394
395 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
396 struct thread *pos = rb_entry(nd, struct thread, rb_node);
397
398 ret += thread__fprintf(pos, fp);
399 }
400
401 return ret;
402}
403
404/*
405 * histogram, sorted on item, collects counts
406 */
407
408static struct rb_root hist;
409
410struct hist_entry {
411 struct rb_node rb_node;
412
413 struct thread *thread;
414 struct map *map;
415 struct dso *dso;
416 struct symbol *sym;
417 uint64_t ip;
418 char level;
419
420 uint32_t count;
421};
422
423/*
424 * configurable sorting bits
425 */
426
427struct sort_entry {
428 struct list_head list;
429
430 char *header;
431
432 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
433 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
434 size_t (*print)(FILE *fp, struct hist_entry *);
435};
436
437/* --sort pid */
438
439static int64_t
440sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
441{
442 return right->thread->pid - left->thread->pid;
443}
444
445static size_t
446sort__thread_print(FILE *fp, struct hist_entry *self)
447{
448 return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
449}
450
451static struct sort_entry sort_thread = {
452 .header = " Command: Pid",
453 .cmp = sort__thread_cmp,
454 .print = sort__thread_print,
455};
456
457/* --sort comm */
458
459static int64_t
460sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
461{
462 return right->thread->pid - left->thread->pid;
463}
464
465static int64_t
466sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
467{
468 char *comm_l = left->thread->comm;
469 char *comm_r = right->thread->comm;
470
471 if (!comm_l || !comm_r) {
472 if (!comm_l && !comm_r)
473 return 0;
474 else if (!comm_l)
475 return -1;
476 else
477 return 1;
478 }
479
480 return strcmp(comm_l, comm_r);
481}
482
483static size_t
484sort__comm_print(FILE *fp, struct hist_entry *self)
485{
486 return fprintf(fp, "%16s", self->thread->comm);
487}
488
489static struct sort_entry sort_comm = {
490 .header = " Command",
491 .cmp = sort__comm_cmp,
492 .collapse = sort__comm_collapse,
493 .print = sort__comm_print,
494};
495
496/* --sort dso */
497
498static int64_t
499sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
500{
501 struct dso *dso_l = left->dso;
502 struct dso *dso_r = right->dso;
503
504 if (!dso_l || !dso_r) {
505 if (!dso_l && !dso_r)
506 return 0;
507 else if (!dso_l)
508 return -1;
509 else
510 return 1;
511 }
512
513 return strcmp(dso_l->name, dso_r->name);
514}
515
516static size_t
517sort__dso_print(FILE *fp, struct hist_entry *self)
518{
519 if (self->dso)
520 return fprintf(fp, "%-25s", self->dso->name);
521
522 return fprintf(fp, "%016llx ", (__u64)self->ip);
523}
524
525static struct sort_entry sort_dso = {
526 .header = "Shared Object ",
527 .cmp = sort__dso_cmp,
528 .print = sort__dso_print,
529};
530
531/* --sort symbol */
532
533static int64_t
534sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
535{
536 uint64_t ip_l, ip_r;
537
538 if (left->sym == right->sym)
539 return 0;
540
541 ip_l = left->sym ? left->sym->start : left->ip;
542 ip_r = right->sym ? right->sym->start : right->ip;
543
544 return (int64_t)(ip_r - ip_l);
545}
546
547static size_t
548sort__sym_print(FILE *fp, struct hist_entry *self)
549{
550 size_t ret = 0;
551
552 if (verbose)
553 ret += fprintf(fp, "%#018llx ", (__u64)self->ip);
554
555 if (self->sym) {
556 ret += fprintf(fp, "[%c] %s",
557 self->dso == kernel_dso ? 'k' : '.', self->sym->name);
558 } else {
559 ret += fprintf(fp, "%#016llx", (__u64)self->ip);
560 }
561
562 return ret;
563}
564
565static struct sort_entry sort_sym = {
566 .header = "Symbol",
567 .cmp = sort__sym_cmp,
568 .print = sort__sym_print,
569};
570
571static int sort__need_collapse = 0;
572
573struct sort_dimension {
574 char *name;
575 struct sort_entry *entry;
576 int taken;
577};
578
579static struct sort_dimension sort_dimensions[] = {
580 { .name = "pid", .entry = &sort_thread, },
581 { .name = "comm", .entry = &sort_comm, },
582 { .name = "dso", .entry = &sort_dso, },
583 { .name = "symbol", .entry = &sort_sym, },
584};
585
586static LIST_HEAD(hist_entry__sort_list);
587
588static int sort_dimension__add(char *tok)
589{
590 int i;
591
592 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
593 struct sort_dimension *sd = &sort_dimensions[i];
594
595 if (sd->taken)
596 continue;
597
598 if (strncasecmp(tok, sd->name, strlen(tok)))
599 continue;
600
601 if (sd->entry->collapse)
602 sort__need_collapse = 1;
603
604 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
605 sd->taken = 1;
606
607 return 0;
608 }
609
610 return -ESRCH;
611}
612
613static int64_t
614hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
615{
616 struct sort_entry *se;
617 int64_t cmp = 0;
618
619 list_for_each_entry(se, &hist_entry__sort_list, list) {
620 cmp = se->cmp(left, right);
621 if (cmp)
622 break;
623 }
624
625 return cmp;
626}
627
628static int64_t
629hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
630{
631 struct sort_entry *se;
632 int64_t cmp = 0;
633
634 list_for_each_entry(se, &hist_entry__sort_list, list) {
635 int64_t (*f)(struct hist_entry *, struct hist_entry *);
636
637 f = se->collapse ?: se->cmp;
638
639 cmp = f(left, right);
640 if (cmp)
641 break;
642 }
643
644 return cmp;
645}
646
647/*
648 * collect histogram counts
649 */
650static void hist_hit(struct hist_entry *he, uint64_t ip)
651{
652 unsigned int sym_size, offset;
653 struct symbol *sym = he->sym;
654
655 he->count++;
656
657 if (!sym || !sym->hist)
658 return;
659
660 sym_size = sym->end - sym->start;
661 offset = ip - sym->start;
662
663 if (offset >= sym_size)
664 return;
665
666 sym->hist_sum++;
667 sym->hist[offset]++;
668
669 if (verbose >= 3)
670 printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n",
671 (void *)he->sym->start,
672 he->sym->name,
673 (void *)ip, ip - he->sym->start,
674 sym->hist[offset]);
675}
676
677static int
678hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
679 struct symbol *sym, uint64_t ip, char level)
680{
681 struct rb_node **p = &hist.rb_node;
682 struct rb_node *parent = NULL;
683 struct hist_entry *he;
684 struct hist_entry entry = {
685 .thread = thread,
686 .map = map,
687 .dso = dso,
688 .sym = sym,
689 .ip = ip,
690 .level = level,
691 .count = 1,
692 };
693 int cmp;
694
695 while (*p != NULL) {
696 parent = *p;
697 he = rb_entry(parent, struct hist_entry, rb_node);
698
699 cmp = hist_entry__cmp(&entry, he);
700
701 if (!cmp) {
702 hist_hit(he, ip);
703
704 return 0;
705 }
706
707 if (cmp < 0)
708 p = &(*p)->rb_left;
709 else
710 p = &(*p)->rb_right;
711 }
712
713 he = malloc(sizeof(*he));
714 if (!he)
715 return -ENOMEM;
716 *he = entry;
717 rb_link_node(&he->rb_node, parent, p);
718 rb_insert_color(&he->rb_node, &hist);
719
720 return 0;
721}
722
723static void hist_entry__free(struct hist_entry *he)
724{
725 free(he);
726}
727
728/*
729 * collapse the histogram
730 */
731
732static struct rb_root collapse_hists;
733
734static void collapse__insert_entry(struct hist_entry *he)
735{
736 struct rb_node **p = &collapse_hists.rb_node;
737 struct rb_node *parent = NULL;
738 struct hist_entry *iter;
739 int64_t cmp;
740
741 while (*p != NULL) {
742 parent = *p;
743 iter = rb_entry(parent, struct hist_entry, rb_node);
744
745 cmp = hist_entry__collapse(iter, he);
746
747 if (!cmp) {
748 iter->count += he->count;
749 hist_entry__free(he);
750 return;
751 }
752
753 if (cmp < 0)
754 p = &(*p)->rb_left;
755 else
756 p = &(*p)->rb_right;
757 }
758
759 rb_link_node(&he->rb_node, parent, p);
760 rb_insert_color(&he->rb_node, &collapse_hists);
761}
762
763static void collapse__resort(void)
764{
765 struct rb_node *next;
766 struct hist_entry *n;
767
768 if (!sort__need_collapse)
769 return;
770
771 next = rb_first(&hist);
772 while (next) {
773 n = rb_entry(next, struct hist_entry, rb_node);
774 next = rb_next(&n->rb_node);
775
776 rb_erase(&n->rb_node, &hist);
777 collapse__insert_entry(n);
778 }
779}
780
781/*
782 * reverse the map, sort on count.
783 */
784
785static struct rb_root output_hists;
786
787static void output__insert_entry(struct hist_entry *he)
788{
789 struct rb_node **p = &output_hists.rb_node;
790 struct rb_node *parent = NULL;
791 struct hist_entry *iter;
792
793 while (*p != NULL) {
794 parent = *p;
795 iter = rb_entry(parent, struct hist_entry, rb_node);
796
797 if (he->count > iter->count)
798 p = &(*p)->rb_left;
799 else
800 p = &(*p)->rb_right;
801 }
802
803 rb_link_node(&he->rb_node, parent, p);
804 rb_insert_color(&he->rb_node, &output_hists);
805}
806
807static void output__resort(void)
808{
809 struct rb_node *next;
810 struct hist_entry *n;
811 struct rb_root *tree = &hist;
812
813 if (sort__need_collapse)
814 tree = &collapse_hists;
815
816 next = rb_first(tree);
817
818 while (next) {
819 n = rb_entry(next, struct hist_entry, rb_node);
820 next = rb_next(&n->rb_node);
821
822 rb_erase(&n->rb_node, tree);
823 output__insert_entry(n);
824 }
825}
826
827static void register_idle_thread(void)
828{
829 struct thread *thread = threads__findnew(0);
830
831 if (thread == NULL ||
832 thread__set_comm(thread, "[idle]")) {
833 fprintf(stderr, "problem inserting idle task.\n");
834 exit(-1);
835 }
836}
837
838static unsigned long total = 0,
839 total_mmap = 0,
840 total_comm = 0,
841 total_fork = 0,
842 total_unknown = 0;
843
844static int
845process_overflow_event(event_t *event, unsigned long offset, unsigned long head)
846{
847 char level;
848 int show = 0;
849 struct dso *dso = NULL;
850 struct thread *thread = threads__findnew(event->ip.pid);
851 uint64_t ip = event->ip.ip;
852 struct map *map = NULL;
853
854 dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
855 (void *)(offset + head),
856 (void *)(long)(event->header.size),
857 event->header.misc,
858 event->ip.pid,
859 (void *)(long)ip);
860
861 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
862
863 if (thread == NULL) {
864 fprintf(stderr, "problem processing %d event, skipping it.\n",
865 event->header.type);
866 return -1;
867 }
868
869 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
870 show = SHOW_KERNEL;
871 level = 'k';
872
873 dso = kernel_dso;
874
875 dprintf(" ...... dso: %s\n", dso->name);
876
877 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
878
879 show = SHOW_USER;
880 level = '.';
881
882 map = thread__find_map(thread, ip);
883 if (map != NULL) {
884 ip = map->map_ip(map, ip);
885 dso = map->dso;
886 } else {
887 /*
888 * If this is outside of all known maps,
889 * and is a negative address, try to look it
890 * up in the kernel dso, as it might be a
891 * vsyscall (which executes in user-mode):
892 */
893 if ((long long)ip < 0)
894 dso = kernel_dso;
895 }
896 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
897
898 } else {
899 show = SHOW_HV;
900 level = 'H';
901 dprintf(" ...... dso: [hypervisor]\n");
902 }
903
904 if (show & show_mask) {
905 struct symbol *sym = NULL;
906
907 if (dso)
908 sym = dso->find_symbol(dso, ip);
909
910 if (hist_entry__add(thread, map, dso, sym, ip, level)) {
911 fprintf(stderr,
912 "problem incrementing symbol count, skipping event\n");
913 return -1;
914 }
915 }
916 total++;
917
918 return 0;
919}
920
921static int
922process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
923{
924 struct thread *thread = threads__findnew(event->mmap.pid);
925 struct map *map = map__new(&event->mmap);
926
927 dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
928 (void *)(offset + head),
929 (void *)(long)(event->header.size),
930 event->mmap.pid,
931 (void *)(long)event->mmap.start,
932 (void *)(long)event->mmap.len,
933 (void *)(long)event->mmap.pgoff,
934 event->mmap.filename);
935
936 if (thread == NULL || map == NULL) {
937 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
938 return 0;
939 }
940
941 thread__insert_map(thread, map);
942 total_mmap++;
943
944 return 0;
945}
946
947static int
948process_comm_event(event_t *event, unsigned long offset, unsigned long head)
949{
950 struct thread *thread = threads__findnew(event->comm.pid);
951
952 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
953 (void *)(offset + head),
954 (void *)(long)(event->header.size),
955 event->comm.comm, event->comm.pid);
956
957 if (thread == NULL ||
958 thread__set_comm(thread, event->comm.comm)) {
959 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
960 return -1;
961 }
962 total_comm++;
963
964 return 0;
965}
966
967static int
968process_fork_event(event_t *event, unsigned long offset, unsigned long head)
969{
970 struct thread *thread = threads__findnew(event->fork.pid);
971 struct thread *parent = threads__findnew(event->fork.ppid);
972
973 dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
974 (void *)(offset + head),
975 (void *)(long)(event->header.size),
976 event->fork.pid, event->fork.ppid);
977
978 if (!thread || !parent || thread__fork(thread, parent)) {
979 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
980 return -1;
981 }
982 total_fork++;
983
984 return 0;
985}
986
987static int
988process_period_event(event_t *event, unsigned long offset, unsigned long head)
989{
990 dprintf("%p [%p]: PERF_EVENT_PERIOD: time:%Ld, id:%Ld: period:%Ld\n",
991 (void *)(offset + head),
992 (void *)(long)(event->header.size),
993 event->period.time,
994 event->period.id,
995 event->period.sample_period);
996
997 return 0;
998}
999
1000static int
1001process_event(event_t *event, unsigned long offset, unsigned long head)
1002{
1003 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW)
1004 return process_overflow_event(event, offset, head);
1005
1006 switch (event->header.type) {
1007 case PERF_EVENT_MMAP:
1008 return process_mmap_event(event, offset, head);
1009
1010 case PERF_EVENT_COMM:
1011 return process_comm_event(event, offset, head);
1012
1013 case PERF_EVENT_FORK:
1014 return process_fork_event(event, offset, head);
1015
1016 case PERF_EVENT_PERIOD:
1017 return process_period_event(event, offset, head);
1018 /*
1019 * We dont process them right now but they are fine:
1020 */
1021
1022 case PERF_EVENT_THROTTLE:
1023 case PERF_EVENT_UNTHROTTLE:
1024 return 0;
1025
1026 default:
1027 return -1;
1028 }
1029
1030 return 0;
1031}
1032
1033static int
1034parse_line(FILE *file, struct symbol *sym, uint64_t start, uint64_t len)
1035{
1036 char *line = NULL, *tmp, *tmp2;
1037 unsigned int offset;
1038 size_t line_len;
1039 __u64 line_ip;
1040 int ret;
1041 char *c;
1042
1043 if (getline(&line, &line_len, file) < 0)
1044 return -1;
1045 if (!line)
1046 return -1;
1047
1048 c = strchr(line, '\n');
1049 if (c)
1050 *c = 0;
1051
1052 line_ip = -1;
1053 offset = 0;
1054 ret = -2;
1055
1056 /*
1057 * Strip leading spaces:
1058 */
1059 tmp = line;
1060 while (*tmp) {
1061 if (*tmp != ' ')
1062 break;
1063 tmp++;
1064 }
1065
1066 if (*tmp) {
1067 /*
1068 * Parse hexa addresses followed by ':'
1069 */
1070 line_ip = strtoull(tmp, &tmp2, 16);
1071 if (*tmp2 != ':')
1072 line_ip = -1;
1073 }
1074
1075 if (line_ip != -1) {
1076 unsigned int hits = 0;
1077 double percent = 0.0;
1078 char *color = PERF_COLOR_NORMAL;
1079
1080 offset = line_ip - start;
1081 if (offset < len)
1082 hits = sym->hist[offset];
1083
1084 if (sym->hist_sum)
1085 percent = 100.0 * hits / sym->hist_sum;
1086
1087 /*
1088 * We color high-overhead entries in red, low-overhead
1089 * entries in green - and keep the middle ground normal:
1090 */
1091 if (percent >= 5.0)
1092 color = PERF_COLOR_RED;
1093 else {
1094 if (percent > 0.5)
1095 color = PERF_COLOR_GREEN;
1096 }
1097
1098 color_fprintf(stdout, color, " %7.2f", percent);
1099 printf(" : ");
1100 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", line);
1101 } else {
1102 if (!*line)
1103 printf(" :\n");
1104 else
1105 printf(" : %s\n", line);
1106 }
1107
1108 return 0;
1109}
1110
1111static void annotate_sym(struct dso *dso, struct symbol *sym)
1112{
1113 char *filename = dso->name;
1114 uint64_t start, end, len;
1115 char command[PATH_MAX*2];
1116 FILE *file;
1117
1118 if (!filename)
1119 return;
1120 if (dso == kernel_dso)
1121 filename = vmlinux;
1122
1123 printf("\n------------------------------------------------\n");
1124 printf(" Percent | Source code & Disassembly of %s\n", filename);
1125 printf("------------------------------------------------\n");
1126
1127 if (verbose >= 2)
1128 printf("annotating [%p] %30s : [%p] %30s\n", dso, dso->name, sym, sym->name);
1129
1130 start = sym->obj_start;
1131 if (!start)
1132 start = sym->start;
1133
1134 end = start + sym->end - sym->start + 1;
1135 len = sym->end - sym->start;
1136
1137 sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s", (__u64)start, (__u64)end, filename);
1138
1139 if (verbose >= 3)
1140 printf("doing: %s\n", command);
1141
1142 file = popen(command, "r");
1143 if (!file)
1144 return;
1145
1146 while (!feof(file)) {
1147 if (parse_line(file, sym, start, len) < 0)
1148 break;
1149 }
1150
1151 pclose(file);
1152}
1153
1154static void find_annotations(void)
1155{
1156 struct rb_node *nd;
1157 struct dso *dso;
1158 int count = 0;
1159
1160 list_for_each_entry(dso, &dsos, node) {
1161
1162 for (nd = rb_first(&dso->syms); nd; nd = rb_next(nd)) {
1163 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
1164
1165 if (sym->hist) {
1166 annotate_sym(dso, sym);
1167 count++;
1168 }
1169 }
1170 }
1171
1172 if (!count)
1173 printf(" Error: symbol '%s' not present amongst the samples.\n", sym_hist_filter);
1174}
1175
1176static int __cmd_annotate(void)
1177{
1178 int ret, rc = EXIT_FAILURE;
1179 unsigned long offset = 0;
1180 unsigned long head = 0;
1181 struct stat stat;
1182 event_t *event;
1183 uint32_t size;
1184 char *buf;
1185
1186 register_idle_thread();
1187
1188 input = open(input_name, O_RDONLY);
1189 if (input < 0) {
1190 perror("failed to open file");
1191 exit(-1);
1192 }
1193
1194 ret = fstat(input, &stat);
1195 if (ret < 0) {
1196 perror("failed to stat file");
1197 exit(-1);
1198 }
1199
1200 if (!stat.st_size) {
1201 fprintf(stderr, "zero-sized file, nothing to do!\n");
1202 exit(0);
1203 }
1204
1205 if (load_kernel() < 0) {
1206 perror("failed to load kernel symbols");
1207 return EXIT_FAILURE;
1208 }
1209
1210remap:
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");
1215 exit(-1);
1216 }
1217
1218more:
1219 event = (event_t *)(buf + head);
1220
1221 size = event->header.size;
1222 if (!size)
1223 size = 8;
1224
1225 if (head + event->header.size >= page_size * mmap_window) {
1226 unsigned long shift = page_size * (head / page_size);
1227 int ret;
1228
1229 ret = munmap(buf, page_size * mmap_window);
1230 assert(ret == 0);
1231
1232 offset += shift;
1233 head -= shift;
1234 goto remap;
1235 }
1236
1237 size = event->header.size;
1238
1239 dprintf("%p [%p]: event: %d\n",
1240 (void *)(offset + head),
1241 (void *)(long)event->header.size,
1242 event->header.type);
1243
1244 if (!size || process_event(event, offset, head) < 0) {
1245
1246 dprintf("%p [%p]: skipping unknown header type: %d\n",
1247 (void *)(offset + head),
1248 (void *)(long)(event->header.size),
1249 event->header.type);
1250
1251 total_unknown++;
1252
1253 /*
1254 * assume we lost track of the stream, check alignment, and
1255 * increment a single u64 in the hope to catch on again 'soon'.
1256 */
1257
1258 if (unlikely(head & 7))
1259 head &= ~7ULL;
1260
1261 size = 8;
1262 }
1263
1264 head += size;
1265
1266 if (offset + head < stat.st_size)
1267 goto more;
1268
1269 rc = EXIT_SUCCESS;
1270 close(input);
1271
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);
1277
1278 if (dump_trace)
1279 return 0;
1280
1281 if (verbose >= 3)
1282 threads__fprintf(stdout);
1283
1284 if (verbose >= 2)
1285 dsos__fprintf(stdout);
1286
1287 collapse__resort();
1288 output__resort();
1289
1290 find_annotations();
1291
1292 return rc;
1293}
1294
1295static const char * const annotate_usage[] = {
1296 "perf annotate [<options>] <command>",
1297 NULL
1298};
1299
1300static const struct option options[] = {
1301 OPT_STRING('i', "input", &input_name, "file",
1302 "input file name"),
1303 OPT_STRING('s', "symbol", &sym_hist_filter, "file",
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_END()
1311};
1312
1313static void setup_sorting(void)
1314{
1315 char *tmp, *tok, *str = strdup(sort_order);
1316
1317 for (tok = strtok_r(str, ", ", &tmp);
1318 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1319 if (sort_dimension__add(tok) < 0) {
1320 error("Unknown --sort key: `%s'", tok);
1321 usage_with_options(annotate_usage, options);
1322 }
1323 }
1324
1325 free(str);
1326}
1327
1328int cmd_annotate(int argc, const char **argv, const char *prefix)
1329{
1330 symbol__init();
1331
1332 page_size = getpagesize();
1333
1334 argc = parse_options(argc, argv, options, annotate_usage, 0);
1335
1336 setup_sorting();
1337
1338 if (argc) {
1339 /*
1340 * Special case: if there's an argument left then assume tha
1341 * it's a symbol filter:
1342 */
1343 if (argc > 1)
1344 usage_with_options(annotate_usage, options);
1345
1346 sym_hist_filter = argv[0];
1347 }
1348
1349 if (!sym_hist_filter)
1350 usage_with_options(annotate_usage, options);
1351
1352 setup_pager();
1353
1354 return __cmd_annotate();
1355}