perf_counter tools: Adjust only prelinked symbol's addresses
[linux-2.6-block.git] / tools / perf / builtin-report.c
1 /*
2  * builtin-report.c
3  *
4  * Builtin report 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 #include "util/callchain.h"
19
20 #include "perf.h"
21 #include "util/header.h"
22
23 #include "util/parse-options.h"
24 #include "util/parse-events.h"
25
26 #define SHOW_KERNEL     1
27 #define SHOW_USER       2
28 #define SHOW_HV         4
29
30 static char             const *input_name = "perf.data";
31 static char             *vmlinux = NULL;
32
33 static char             default_sort_order[] = "comm,dso";
34 static char             *sort_order = default_sort_order;
35
36 static int              input;
37 static int              show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
38
39 static int              dump_trace = 0;
40 #define dprintf(x...)   do { if (dump_trace) printf(x); } while (0)
41 #define cdprintf(x...)  do { if (dump_trace) color_fprintf(stdout, color, x); } while (0)
42
43 static int              verbose;
44 #define eprintf(x...)   do { if (verbose) fprintf(stderr, x); } while (0)
45
46 static int              full_paths;
47
48 static unsigned long    page_size;
49 static unsigned long    mmap_window = 32;
50
51 static char             default_parent_pattern[] = "^sys_|^do_page_fault";
52 static char             *parent_pattern = default_parent_pattern;
53 static regex_t          parent_regex;
54
55 static int              exclude_other = 1;
56 static int              callchain;
57
58 static u64              sample_type;
59
60 struct ip_event {
61         struct perf_event_header header;
62         u64 ip;
63         u32 pid, tid;
64         unsigned char __more_data[];
65 };
66
67 struct mmap_event {
68         struct perf_event_header header;
69         u32 pid, tid;
70         u64 start;
71         u64 len;
72         u64 pgoff;
73         char filename[PATH_MAX];
74 };
75
76 struct comm_event {
77         struct perf_event_header header;
78         u32 pid, tid;
79         char comm[16];
80 };
81
82 struct fork_event {
83         struct perf_event_header header;
84         u32 pid, ppid;
85 };
86
87 struct period_event {
88         struct perf_event_header header;
89         u64 time;
90         u64 id;
91         u64 sample_period;
92 };
93
94 struct lost_event {
95         struct perf_event_header header;
96         u64 id;
97         u64 lost;
98 };
99
100 struct read_event {
101         struct perf_event_header header;
102         u32 pid,tid;
103         u64 value;
104         u64 format[3];
105 };
106
107 typedef union event_union {
108         struct perf_event_header        header;
109         struct ip_event                 ip;
110         struct mmap_event               mmap;
111         struct comm_event               comm;
112         struct fork_event               fork;
113         struct period_event             period;
114         struct lost_event               lost;
115         struct read_event               read;
116 } event_t;
117
118 static LIST_HEAD(dsos);
119 static struct dso *kernel_dso;
120 static struct dso *vdso;
121
122 static void dsos__add(struct dso *dso)
123 {
124         list_add_tail(&dso->node, &dsos);
125 }
126
127 static struct dso *dsos__find(const char *name)
128 {
129         struct dso *pos;
130
131         list_for_each_entry(pos, &dsos, node)
132                 if (strcmp(pos->name, name) == 0)
133                         return pos;
134         return NULL;
135 }
136
137 static struct dso *dsos__findnew(const char *name)
138 {
139         struct dso *dso = dsos__find(name);
140         int nr;
141
142         if (dso)
143                 return dso;
144
145         dso = dso__new(name, 0);
146         if (!dso)
147                 goto out_delete_dso;
148
149         nr = dso__load(dso, NULL, verbose);
150         if (nr < 0) {
151                 eprintf("Failed to open: %s\n", name);
152                 goto out_delete_dso;
153         }
154         if (!nr)
155                 eprintf("No symbols found in: %s, maybe install a debug package?\n", name);
156
157         dsos__add(dso);
158
159         return dso;
160
161 out_delete_dso:
162         dso__delete(dso);
163         return NULL;
164 }
165
166 static void dsos__fprintf(FILE *fp)
167 {
168         struct dso *pos;
169
170         list_for_each_entry(pos, &dsos, node)
171                 dso__fprintf(pos, fp);
172 }
173
174 static struct symbol *vdso__find_symbol(struct dso *dso, u64 ip)
175 {
176         return dso__find_symbol(kernel_dso, ip);
177 }
178
179 static int load_kernel(void)
180 {
181         int err;
182
183         kernel_dso = dso__new("[kernel]", 0);
184         if (!kernel_dso)
185                 return -1;
186
187         err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose);
188         if (err) {
189                 dso__delete(kernel_dso);
190                 kernel_dso = NULL;
191         } else
192                 dsos__add(kernel_dso);
193
194         vdso = dso__new("[vdso]", 0);
195         if (!vdso)
196                 return -1;
197
198         vdso->find_symbol = vdso__find_symbol;
199
200         dsos__add(vdso);
201
202         return err;
203 }
204
205 static char __cwd[PATH_MAX];
206 static char *cwd = __cwd;
207 static int cwdlen;
208
209 static int strcommon(const char *pathname)
210 {
211         int n = 0;
212
213         while (pathname[n] == cwd[n] && n < cwdlen)
214                 ++n;
215
216         return n;
217 }
218
219 struct map {
220         struct list_head node;
221         u64      start;
222         u64      end;
223         u64      pgoff;
224         u64      (*map_ip)(struct map *, u64);
225         struct dso       *dso;
226 };
227
228 static u64 map__map_ip(struct map *map, u64 ip)
229 {
230         return ip - map->start + map->pgoff;
231 }
232
233 static u64 vdso__map_ip(struct map *map, u64 ip)
234 {
235         return ip;
236 }
237
238 static inline int is_anon_memory(const char *filename)
239 {
240      return strcmp(filename, "//anon") == 0;
241 }
242
243 static struct map *map__new(struct mmap_event *event)
244 {
245         struct map *self = malloc(sizeof(*self));
246
247         if (self != NULL) {
248                 const char *filename = event->filename;
249                 char newfilename[PATH_MAX];
250                 int anon;
251
252                 if (cwd) {
253                         int n = strcommon(filename);
254
255                         if (n == cwdlen) {
256                                 snprintf(newfilename, sizeof(newfilename),
257                                          ".%s", filename + n);
258                                 filename = newfilename;
259                         }
260                 }
261
262                 anon = is_anon_memory(filename);
263
264                 if (anon) {
265                         snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
266                         filename = newfilename;
267                 }
268
269                 self->start = event->start;
270                 self->end   = event->start + event->len;
271                 self->pgoff = event->pgoff;
272
273                 self->dso = dsos__findnew(filename);
274                 if (self->dso == NULL)
275                         goto out_delete;
276
277                 if (self->dso == vdso || anon)
278                         self->map_ip = vdso__map_ip;
279                 else
280                         self->map_ip = map__map_ip;
281         }
282         return self;
283 out_delete:
284         free(self);
285         return NULL;
286 }
287
288 static struct map *map__clone(struct map *self)
289 {
290         struct map *map = malloc(sizeof(*self));
291
292         if (!map)
293                 return NULL;
294
295         memcpy(map, self, sizeof(*self));
296
297         return map;
298 }
299
300 static int map__overlap(struct map *l, struct map *r)
301 {
302         if (l->start > r->start) {
303                 struct map *t = l;
304                 l = r;
305                 r = t;
306         }
307
308         if (l->end > r->start)
309                 return 1;
310
311         return 0;
312 }
313
314 static size_t map__fprintf(struct map *self, FILE *fp)
315 {
316         return fprintf(fp, " %Lx-%Lx %Lx %s\n",
317                        self->start, self->end, self->pgoff, self->dso->name);
318 }
319
320
321 struct thread {
322         struct rb_node   rb_node;
323         struct list_head maps;
324         pid_t            pid;
325         char             *comm;
326 };
327
328 static struct thread *thread__new(pid_t pid)
329 {
330         struct thread *self = malloc(sizeof(*self));
331
332         if (self != NULL) {
333                 self->pid = pid;
334                 self->comm = malloc(32);
335                 if (self->comm)
336                         snprintf(self->comm, 32, ":%d", self->pid);
337                 INIT_LIST_HEAD(&self->maps);
338         }
339
340         return self;
341 }
342
343 static int thread__set_comm(struct thread *self, const char *comm)
344 {
345         if (self->comm)
346                 free(self->comm);
347         self->comm = strdup(comm);
348         return self->comm ? 0 : -ENOMEM;
349 }
350
351 static size_t thread__fprintf(struct thread *self, FILE *fp)
352 {
353         struct map *pos;
354         size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
355
356         list_for_each_entry(pos, &self->maps, node)
357                 ret += map__fprintf(pos, fp);
358
359         return ret;
360 }
361
362
363 static struct rb_root threads;
364 static struct thread *last_match;
365
366 static struct thread *threads__findnew(pid_t pid)
367 {
368         struct rb_node **p = &threads.rb_node;
369         struct rb_node *parent = NULL;
370         struct thread *th;
371
372         /*
373          * Font-end cache - PID lookups come in blocks,
374          * so most of the time we dont have to look up
375          * the full rbtree:
376          */
377         if (last_match && last_match->pid == pid)
378                 return last_match;
379
380         while (*p != NULL) {
381                 parent = *p;
382                 th = rb_entry(parent, struct thread, rb_node);
383
384                 if (th->pid == pid) {
385                         last_match = th;
386                         return th;
387                 }
388
389                 if (pid < th->pid)
390                         p = &(*p)->rb_left;
391                 else
392                         p = &(*p)->rb_right;
393         }
394
395         th = thread__new(pid);
396         if (th != NULL) {
397                 rb_link_node(&th->rb_node, parent, p);
398                 rb_insert_color(&th->rb_node, &threads);
399                 last_match = th;
400         }
401
402         return th;
403 }
404
405 static void thread__insert_map(struct thread *self, struct map *map)
406 {
407         struct map *pos, *tmp;
408
409         list_for_each_entry_safe(pos, tmp, &self->maps, node) {
410                 if (map__overlap(pos, map)) {
411                         if (verbose >= 2) {
412                                 printf("overlapping maps:\n");
413                                 map__fprintf(map, stdout);
414                                 map__fprintf(pos, stdout);
415                         }
416
417                         if (map->start <= pos->start && map->end > pos->start)
418                                 pos->start = map->end;
419
420                         if (map->end >= pos->end && map->start < pos->end)
421                                 pos->end = map->start;
422
423                         if (verbose >= 2) {
424                                 printf("after collision:\n");
425                                 map__fprintf(pos, stdout);
426                         }
427
428                         if (pos->start >= pos->end) {
429                                 list_del_init(&pos->node);
430                                 free(pos);
431                         }
432                 }
433         }
434
435         list_add_tail(&map->node, &self->maps);
436 }
437
438 static int thread__fork(struct thread *self, struct thread *parent)
439 {
440         struct map *map;
441
442         if (self->comm)
443                 free(self->comm);
444         self->comm = strdup(parent->comm);
445         if (!self->comm)
446                 return -ENOMEM;
447
448         list_for_each_entry(map, &parent->maps, node) {
449                 struct map *new = map__clone(map);
450                 if (!new)
451                         return -ENOMEM;
452                 thread__insert_map(self, new);
453         }
454
455         return 0;
456 }
457
458 static struct map *thread__find_map(struct thread *self, u64 ip)
459 {
460         struct map *pos;
461
462         if (self == NULL)
463                 return NULL;
464
465         list_for_each_entry(pos, &self->maps, node)
466                 if (ip >= pos->start && ip <= pos->end)
467                         return pos;
468
469         return NULL;
470 }
471
472 static size_t threads__fprintf(FILE *fp)
473 {
474         size_t ret = 0;
475         struct rb_node *nd;
476
477         for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
478                 struct thread *pos = rb_entry(nd, struct thread, rb_node);
479
480                 ret += thread__fprintf(pos, fp);
481         }
482
483         return ret;
484 }
485
486 /*
487  * histogram, sorted on item, collects counts
488  */
489
490 static struct rb_root hist;
491
492 struct hist_entry {
493         struct rb_node          rb_node;
494
495         struct thread           *thread;
496         struct map              *map;
497         struct dso              *dso;
498         struct symbol           *sym;
499         struct symbol           *parent;
500         u64                     ip;
501         char                    level;
502         struct callchain_node   callchain;
503         struct rb_root          sorted_chain;
504
505         u64                     count;
506 };
507
508 /*
509  * configurable sorting bits
510  */
511
512 struct sort_entry {
513         struct list_head list;
514
515         char *header;
516
517         int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
518         int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
519         size_t  (*print)(FILE *fp, struct hist_entry *);
520 };
521
522 static int64_t cmp_null(void *l, void *r)
523 {
524         if (!l && !r)
525                 return 0;
526         else if (!l)
527                 return -1;
528         else
529                 return 1;
530 }
531
532 /* --sort pid */
533
534 static int64_t
535 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
536 {
537         return right->thread->pid - left->thread->pid;
538 }
539
540 static size_t
541 sort__thread_print(FILE *fp, struct hist_entry *self)
542 {
543         return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
544 }
545
546 static struct sort_entry sort_thread = {
547         .header = "         Command:  Pid",
548         .cmp    = sort__thread_cmp,
549         .print  = sort__thread_print,
550 };
551
552 /* --sort comm */
553
554 static int64_t
555 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
556 {
557         return right->thread->pid - left->thread->pid;
558 }
559
560 static int64_t
561 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
562 {
563         char *comm_l = left->thread->comm;
564         char *comm_r = right->thread->comm;
565
566         if (!comm_l || !comm_r)
567                 return cmp_null(comm_l, comm_r);
568
569         return strcmp(comm_l, comm_r);
570 }
571
572 static size_t
573 sort__comm_print(FILE *fp, struct hist_entry *self)
574 {
575         return fprintf(fp, "%16s", self->thread->comm);
576 }
577
578 static struct sort_entry sort_comm = {
579         .header         = "         Command",
580         .cmp            = sort__comm_cmp,
581         .collapse       = sort__comm_collapse,
582         .print          = sort__comm_print,
583 };
584
585 /* --sort dso */
586
587 static int64_t
588 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
589 {
590         struct dso *dso_l = left->dso;
591         struct dso *dso_r = right->dso;
592
593         if (!dso_l || !dso_r)
594                 return cmp_null(dso_l, dso_r);
595
596         return strcmp(dso_l->name, dso_r->name);
597 }
598
599 static size_t
600 sort__dso_print(FILE *fp, struct hist_entry *self)
601 {
602         if (self->dso)
603                 return fprintf(fp, "%-25s", self->dso->name);
604
605         return fprintf(fp, "%016llx         ", (u64)self->ip);
606 }
607
608 static struct sort_entry sort_dso = {
609         .header = "Shared Object            ",
610         .cmp    = sort__dso_cmp,
611         .print  = sort__dso_print,
612 };
613
614 /* --sort symbol */
615
616 static int64_t
617 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
618 {
619         u64 ip_l, ip_r;
620
621         if (left->sym == right->sym)
622                 return 0;
623
624         ip_l = left->sym ? left->sym->start : left->ip;
625         ip_r = right->sym ? right->sym->start : right->ip;
626
627         return (int64_t)(ip_r - ip_l);
628 }
629
630 static size_t
631 sort__sym_print(FILE *fp, struct hist_entry *self)
632 {
633         size_t ret = 0;
634
635         if (verbose)
636                 ret += fprintf(fp, "%#018llx  ", (u64)self->ip);
637
638         if (self->sym) {
639                 ret += fprintf(fp, "[%c] %s",
640                         self->dso == kernel_dso ? 'k' : '.', self->sym->name);
641         } else {
642                 ret += fprintf(fp, "%#016llx", (u64)self->ip);
643         }
644
645         return ret;
646 }
647
648 static struct sort_entry sort_sym = {
649         .header = "Symbol",
650         .cmp    = sort__sym_cmp,
651         .print  = sort__sym_print,
652 };
653
654 /* --sort parent */
655
656 static int64_t
657 sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
658 {
659         struct symbol *sym_l = left->parent;
660         struct symbol *sym_r = right->parent;
661
662         if (!sym_l || !sym_r)
663                 return cmp_null(sym_l, sym_r);
664
665         return strcmp(sym_l->name, sym_r->name);
666 }
667
668 static size_t
669 sort__parent_print(FILE *fp, struct hist_entry *self)
670 {
671         size_t ret = 0;
672
673         ret += fprintf(fp, "%-20s", self->parent ? self->parent->name : "[other]");
674
675         return ret;
676 }
677
678 static struct sort_entry sort_parent = {
679         .header = "Parent symbol       ",
680         .cmp    = sort__parent_cmp,
681         .print  = sort__parent_print,
682 };
683
684 static int sort__need_collapse = 0;
685 static int sort__has_parent = 0;
686
687 struct sort_dimension {
688         char                    *name;
689         struct sort_entry       *entry;
690         int                     taken;
691 };
692
693 static struct sort_dimension sort_dimensions[] = {
694         { .name = "pid",        .entry = &sort_thread,  },
695         { .name = "comm",       .entry = &sort_comm,    },
696         { .name = "dso",        .entry = &sort_dso,     },
697         { .name = "symbol",     .entry = &sort_sym,     },
698         { .name = "parent",     .entry = &sort_parent,  },
699 };
700
701 static LIST_HEAD(hist_entry__sort_list);
702
703 static int sort_dimension__add(char *tok)
704 {
705         int i;
706
707         for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
708                 struct sort_dimension *sd = &sort_dimensions[i];
709
710                 if (sd->taken)
711                         continue;
712
713                 if (strncasecmp(tok, sd->name, strlen(tok)))
714                         continue;
715
716                 if (sd->entry->collapse)
717                         sort__need_collapse = 1;
718
719                 if (sd->entry == &sort_parent) {
720                         int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
721                         if (ret) {
722                                 char err[BUFSIZ];
723
724                                 regerror(ret, &parent_regex, err, sizeof(err));
725                                 fprintf(stderr, "Invalid regex: %s\n%s",
726                                         parent_pattern, err);
727                                 exit(-1);
728                         }
729                         sort__has_parent = 1;
730                 }
731
732                 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
733                 sd->taken = 1;
734
735                 return 0;
736         }
737
738         return -ESRCH;
739 }
740
741 static int64_t
742 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
743 {
744         struct sort_entry *se;
745         int64_t cmp = 0;
746
747         list_for_each_entry(se, &hist_entry__sort_list, list) {
748                 cmp = se->cmp(left, right);
749                 if (cmp)
750                         break;
751         }
752
753         return cmp;
754 }
755
756 static int64_t
757 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
758 {
759         struct sort_entry *se;
760         int64_t cmp = 0;
761
762         list_for_each_entry(se, &hist_entry__sort_list, list) {
763                 int64_t (*f)(struct hist_entry *, struct hist_entry *);
764
765                 f = se->collapse ?: se->cmp;
766
767                 cmp = f(left, right);
768                 if (cmp)
769                         break;
770         }
771
772         return cmp;
773 }
774
775 static size_t
776 callchain__fprintf(FILE *fp, struct callchain_node *self, u64 total_samples)
777 {
778         struct callchain_list *chain;
779         size_t ret = 0;
780
781         if (!self)
782                 return 0;
783
784         ret += callchain__fprintf(fp, self->parent, total_samples);
785
786
787         list_for_each_entry(chain, &self->val, list)
788                 ret += fprintf(fp, "                %p\n", (void *)chain->ip);
789
790         return ret;
791 }
792
793 static size_t
794 hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
795                               u64 total_samples)
796 {
797         struct rb_node *rb_node;
798         struct callchain_node *chain;
799         size_t ret = 0;
800
801         rb_node = rb_first(&self->sorted_chain);
802         while (rb_node) {
803                 double percent;
804
805                 chain = rb_entry(rb_node, struct callchain_node, rb_node);
806                 percent = chain->hit * 100.0 / total_samples;
807                 ret += fprintf(fp, "           %6.2f%%\n", percent);
808                 ret += callchain__fprintf(fp, chain, total_samples);
809                 ret += fprintf(fp, "\n");
810                 rb_node = rb_next(rb_node);
811         }
812
813         return ret;
814 }
815
816
817 static size_t
818 hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
819 {
820         struct sort_entry *se;
821         size_t ret;
822
823         if (exclude_other && !self->parent)
824                 return 0;
825
826         if (total_samples) {
827                 double percent = self->count * 100.0 / total_samples;
828                 char *color = PERF_COLOR_NORMAL;
829
830                 /*
831                  * We color high-overhead entries in red, mid-overhead
832                  * entries in green - and keep the low overhead places
833                  * normal:
834                  */
835                 if (percent >= 5.0) {
836                         color = PERF_COLOR_RED;
837                 } else {
838                         if (percent >= 0.5)
839                                 color = PERF_COLOR_GREEN;
840                 }
841
842                 ret = color_fprintf(fp, color, "   %6.2f%%",
843                                 (self->count * 100.0) / total_samples);
844         } else
845                 ret = fprintf(fp, "%12Ld ", self->count);
846
847         list_for_each_entry(se, &hist_entry__sort_list, list) {
848                 if (exclude_other && (se == &sort_parent))
849                         continue;
850
851                 fprintf(fp, "  ");
852                 ret += se->print(fp, self);
853         }
854
855         ret += fprintf(fp, "\n");
856
857         if (callchain)
858                 hist_entry_callchain__fprintf(fp, self, total_samples);
859
860         return ret;
861 }
862
863 /*
864  *
865  */
866
867 static struct symbol *
868 resolve_symbol(struct thread *thread, struct map **mapp,
869                struct dso **dsop, u64 *ipp)
870 {
871         struct dso *dso = dsop ? *dsop : NULL;
872         struct map *map = mapp ? *mapp : NULL;
873         u64 ip = *ipp;
874
875         if (!thread)
876                 return NULL;
877
878         if (dso)
879                 goto got_dso;
880
881         if (map)
882                 goto got_map;
883
884         map = thread__find_map(thread, ip);
885         if (map != NULL) {
886                 if (mapp)
887                         *mapp = map;
888 got_map:
889                 ip = map->map_ip(map, ip);
890
891                 dso = map->dso;
892         } else {
893                 /*
894                  * If this is outside of all known maps,
895                  * and is a negative address, try to look it
896                  * up in the kernel dso, as it might be a
897                  * vsyscall (which executes in user-mode):
898                  */
899                 if ((long long)ip < 0)
900                 dso = kernel_dso;
901         }
902         dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
903         dprintf(" ...... map: %Lx -> %Lx\n", *ipp, ip);
904         *ipp  = ip;
905
906         if (dsop)
907                 *dsop = dso;
908
909         if (!dso)
910                 return NULL;
911 got_dso:
912         return dso->find_symbol(dso, ip);
913 }
914
915 static int call__match(struct symbol *sym)
916 {
917         if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
918                 return 1;
919
920         return 0;
921 }
922
923 /*
924  * collect histogram counts
925  */
926
927 static int
928 hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
929                 struct symbol *sym, u64 ip, struct ip_callchain *chain,
930                 char level, u64 count)
931 {
932         struct rb_node **p = &hist.rb_node;
933         struct rb_node *parent = NULL;
934         struct hist_entry *he;
935         struct hist_entry entry = {
936                 .thread = thread,
937                 .map    = map,
938                 .dso    = dso,
939                 .sym    = sym,
940                 .ip     = ip,
941                 .level  = level,
942                 .count  = count,
943                 .parent = NULL,
944                 .sorted_chain = RB_ROOT
945         };
946         int cmp;
947
948         if (sort__has_parent && chain) {
949                 u64 context = PERF_CONTEXT_MAX;
950                 int i;
951
952                 for (i = 0; i < chain->nr; i++) {
953                         u64 ip = chain->ips[i];
954                         struct dso *dso = NULL;
955                         struct symbol *sym;
956
957                         if (ip >= PERF_CONTEXT_MAX) {
958                                 context = ip;
959                                 continue;
960                         }
961
962                         switch (context) {
963                         case PERF_CONTEXT_KERNEL:
964                                 dso = kernel_dso;
965                                 break;
966                         default:
967                                 break;
968                         }
969
970                         sym = resolve_symbol(thread, NULL, &dso, &ip);
971
972                         if (sym && call__match(sym)) {
973                                 entry.parent = sym;
974                                 break;
975                         }
976                 }
977         }
978
979         while (*p != NULL) {
980                 parent = *p;
981                 he = rb_entry(parent, struct hist_entry, rb_node);
982
983                 cmp = hist_entry__cmp(&entry, he);
984
985                 if (!cmp) {
986                         he->count += count;
987                         if (callchain)
988                                 append_chain(&he->callchain, chain);
989                         return 0;
990                 }
991
992                 if (cmp < 0)
993                         p = &(*p)->rb_left;
994                 else
995                         p = &(*p)->rb_right;
996         }
997
998         he = malloc(sizeof(*he));
999         if (!he)
1000                 return -ENOMEM;
1001         *he = entry;
1002         if (callchain) {
1003                 callchain_init(&he->callchain);
1004                 append_chain(&he->callchain, chain);
1005         }
1006         rb_link_node(&he->rb_node, parent, p);
1007         rb_insert_color(&he->rb_node, &hist);
1008
1009         return 0;
1010 }
1011
1012 static void hist_entry__free(struct hist_entry *he)
1013 {
1014         free(he);
1015 }
1016
1017 /*
1018  * collapse the histogram
1019  */
1020
1021 static struct rb_root collapse_hists;
1022
1023 static void collapse__insert_entry(struct hist_entry *he)
1024 {
1025         struct rb_node **p = &collapse_hists.rb_node;
1026         struct rb_node *parent = NULL;
1027         struct hist_entry *iter;
1028         int64_t cmp;
1029
1030         while (*p != NULL) {
1031                 parent = *p;
1032                 iter = rb_entry(parent, struct hist_entry, rb_node);
1033
1034                 cmp = hist_entry__collapse(iter, he);
1035
1036                 if (!cmp) {
1037                         iter->count += he->count;
1038                         hist_entry__free(he);
1039                         return;
1040                 }
1041
1042                 if (cmp < 0)
1043                         p = &(*p)->rb_left;
1044                 else
1045                         p = &(*p)->rb_right;
1046         }
1047
1048         rb_link_node(&he->rb_node, parent, p);
1049         rb_insert_color(&he->rb_node, &collapse_hists);
1050 }
1051
1052 static void collapse__resort(void)
1053 {
1054         struct rb_node *next;
1055         struct hist_entry *n;
1056
1057         if (!sort__need_collapse)
1058                 return;
1059
1060         next = rb_first(&hist);
1061         while (next) {
1062                 n = rb_entry(next, struct hist_entry, rb_node);
1063                 next = rb_next(&n->rb_node);
1064
1065                 rb_erase(&n->rb_node, &hist);
1066                 collapse__insert_entry(n);
1067         }
1068 }
1069
1070 /*
1071  * reverse the map, sort on count.
1072  */
1073
1074 static struct rb_root output_hists;
1075
1076 static void output__insert_entry(struct hist_entry *he)
1077 {
1078         struct rb_node **p = &output_hists.rb_node;
1079         struct rb_node *parent = NULL;
1080         struct hist_entry *iter;
1081
1082         if (callchain)
1083                 sort_chain_to_rbtree(&he->sorted_chain, &he->callchain);
1084
1085         while (*p != NULL) {
1086                 parent = *p;
1087                 iter = rb_entry(parent, struct hist_entry, rb_node);
1088
1089                 if (he->count > iter->count)
1090                         p = &(*p)->rb_left;
1091                 else
1092                         p = &(*p)->rb_right;
1093         }
1094
1095         rb_link_node(&he->rb_node, parent, p);
1096         rb_insert_color(&he->rb_node, &output_hists);
1097 }
1098
1099 static void output__resort(void)
1100 {
1101         struct rb_node *next;
1102         struct hist_entry *n;
1103         struct rb_root *tree = &hist;
1104
1105         if (sort__need_collapse)
1106                 tree = &collapse_hists;
1107
1108         next = rb_first(tree);
1109
1110         while (next) {
1111                 n = rb_entry(next, struct hist_entry, rb_node);
1112                 next = rb_next(&n->rb_node);
1113
1114                 rb_erase(&n->rb_node, tree);
1115                 output__insert_entry(n);
1116         }
1117 }
1118
1119 static size_t output__fprintf(FILE *fp, u64 total_samples)
1120 {
1121         struct hist_entry *pos;
1122         struct sort_entry *se;
1123         struct rb_node *nd;
1124         size_t ret = 0;
1125
1126         fprintf(fp, "\n");
1127         fprintf(fp, "#\n");
1128         fprintf(fp, "# (%Ld samples)\n", (u64)total_samples);
1129         fprintf(fp, "#\n");
1130
1131         fprintf(fp, "# Overhead");
1132         list_for_each_entry(se, &hist_entry__sort_list, list) {
1133                 if (exclude_other && (se == &sort_parent))
1134                         continue;
1135                 fprintf(fp, "  %s", se->header);
1136         }
1137         fprintf(fp, "\n");
1138
1139         fprintf(fp, "# ........");
1140         list_for_each_entry(se, &hist_entry__sort_list, list) {
1141                 int i;
1142
1143                 if (exclude_other && (se == &sort_parent))
1144                         continue;
1145
1146                 fprintf(fp, "  ");
1147                 for (i = 0; i < strlen(se->header); i++)
1148                         fprintf(fp, ".");
1149         }
1150         fprintf(fp, "\n");
1151
1152         fprintf(fp, "#\n");
1153
1154         for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
1155                 pos = rb_entry(nd, struct hist_entry, rb_node);
1156                 ret += hist_entry__fprintf(fp, pos, total_samples);
1157         }
1158
1159         if (sort_order == default_sort_order &&
1160                         parent_pattern == default_parent_pattern) {
1161                 fprintf(fp, "#\n");
1162                 fprintf(fp, "# (For more details, try: perf report --sort comm,dso,symbol)\n");
1163                 fprintf(fp, "#\n");
1164         }
1165         fprintf(fp, "\n");
1166
1167         return ret;
1168 }
1169
1170 static void register_idle_thread(void)
1171 {
1172         struct thread *thread = threads__findnew(0);
1173
1174         if (thread == NULL ||
1175                         thread__set_comm(thread, "[idle]")) {
1176                 fprintf(stderr, "problem inserting idle task.\n");
1177                 exit(-1);
1178         }
1179 }
1180
1181 static unsigned long total = 0,
1182                      total_mmap = 0,
1183                      total_comm = 0,
1184                      total_fork = 0,
1185                      total_unknown = 0,
1186                      total_lost = 0;
1187
1188 static int validate_chain(struct ip_callchain *chain, event_t *event)
1189 {
1190         unsigned int chain_size;
1191
1192         chain_size = event->header.size;
1193         chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
1194
1195         if (chain->nr*sizeof(u64) > chain_size)
1196                 return -1;
1197
1198         return 0;
1199 }
1200
1201 static int
1202 process_sample_event(event_t *event, unsigned long offset, unsigned long head)
1203 {
1204         char level;
1205         int show = 0;
1206         struct dso *dso = NULL;
1207         struct thread *thread = threads__findnew(event->ip.pid);
1208         u64 ip = event->ip.ip;
1209         u64 period = 1;
1210         struct map *map = NULL;
1211         void *more_data = event->ip.__more_data;
1212         struct ip_callchain *chain = NULL;
1213
1214         if (sample_type & PERF_SAMPLE_PERIOD) {
1215                 period = *(u64 *)more_data;
1216                 more_data += sizeof(u64);
1217         }
1218
1219         dprintf("%p [%p]: PERF_EVENT_SAMPLE (IP, %d): %d: %p period: %Ld\n",
1220                 (void *)(offset + head),
1221                 (void *)(long)(event->header.size),
1222                 event->header.misc,
1223                 event->ip.pid,
1224                 (void *)(long)ip,
1225                 (long long)period);
1226
1227         if (sample_type & PERF_SAMPLE_CALLCHAIN) {
1228                 int i;
1229
1230                 chain = (void *)more_data;
1231
1232                 dprintf("... chain: nr:%Lu\n", chain->nr);
1233
1234                 if (validate_chain(chain, event) < 0) {
1235                         eprintf("call-chain problem with event, skipping it.\n");
1236                         return 0;
1237                 }
1238
1239                 if (dump_trace) {
1240                         for (i = 0; i < chain->nr; i++)
1241                                 dprintf("..... %2d: %016Lx\n", i, chain->ips[i]);
1242                 }
1243         }
1244
1245         dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1246
1247         if (thread == NULL) {
1248                 eprintf("problem processing %d event, skipping it.\n",
1249                         event->header.type);
1250                 return -1;
1251         }
1252
1253         if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
1254                 show = SHOW_KERNEL;
1255                 level = 'k';
1256
1257                 dso = kernel_dso;
1258
1259                 dprintf(" ...... dso: %s\n", dso->name);
1260
1261         } else if (event->header.misc & PERF_EVENT_MISC_USER) {
1262
1263                 show = SHOW_USER;
1264                 level = '.';
1265
1266         } else {
1267                 show = SHOW_HV;
1268                 level = 'H';
1269                 dprintf(" ...... dso: [hypervisor]\n");
1270         }
1271
1272         if (show & show_mask) {
1273                 struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
1274
1275                 if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
1276                         eprintf("problem incrementing symbol count, skipping event\n");
1277                         return -1;
1278                 }
1279         }
1280         total += period;
1281
1282         return 0;
1283 }
1284
1285 static int
1286 process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
1287 {
1288         struct thread *thread = threads__findnew(event->mmap.pid);
1289         struct map *map = map__new(&event->mmap);
1290
1291         dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
1292                 (void *)(offset + head),
1293                 (void *)(long)(event->header.size),
1294                 event->mmap.pid,
1295                 (void *)(long)event->mmap.start,
1296                 (void *)(long)event->mmap.len,
1297                 (void *)(long)event->mmap.pgoff,
1298                 event->mmap.filename);
1299
1300         if (thread == NULL || map == NULL) {
1301                 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
1302                 return 0;
1303         }
1304
1305         thread__insert_map(thread, map);
1306         total_mmap++;
1307
1308         return 0;
1309 }
1310
1311 static int
1312 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
1313 {
1314         struct thread *thread = threads__findnew(event->comm.pid);
1315
1316         dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
1317                 (void *)(offset + head),
1318                 (void *)(long)(event->header.size),
1319                 event->comm.comm, event->comm.pid);
1320
1321         if (thread == NULL ||
1322             thread__set_comm(thread, event->comm.comm)) {
1323                 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
1324                 return -1;
1325         }
1326         total_comm++;
1327
1328         return 0;
1329 }
1330
1331 static int
1332 process_fork_event(event_t *event, unsigned long offset, unsigned long head)
1333 {
1334         struct thread *thread = threads__findnew(event->fork.pid);
1335         struct thread *parent = threads__findnew(event->fork.ppid);
1336
1337         dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
1338                 (void *)(offset + head),
1339                 (void *)(long)(event->header.size),
1340                 event->fork.pid, event->fork.ppid);
1341
1342         if (!thread || !parent || thread__fork(thread, parent)) {
1343                 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
1344                 return -1;
1345         }
1346         total_fork++;
1347
1348         return 0;
1349 }
1350
1351 static int
1352 process_period_event(event_t *event, unsigned long offset, unsigned long head)
1353 {
1354         dprintf("%p [%p]: PERF_EVENT_PERIOD: time:%Ld, id:%Ld: period:%Ld\n",
1355                 (void *)(offset + head),
1356                 (void *)(long)(event->header.size),
1357                 event->period.time,
1358                 event->period.id,
1359                 event->period.sample_period);
1360
1361         return 0;
1362 }
1363
1364 static int
1365 process_lost_event(event_t *event, unsigned long offset, unsigned long head)
1366 {
1367         dprintf("%p [%p]: PERF_EVENT_LOST: id:%Ld: lost:%Ld\n",
1368                 (void *)(offset + head),
1369                 (void *)(long)(event->header.size),
1370                 event->lost.id,
1371                 event->lost.lost);
1372
1373         total_lost += event->lost.lost;
1374
1375         return 0;
1376 }
1377
1378 static void trace_event(event_t *event)
1379 {
1380         unsigned char *raw_event = (void *)event;
1381         char *color = PERF_COLOR_BLUE;
1382         int i, j;
1383
1384         if (!dump_trace)
1385                 return;
1386
1387         dprintf(".");
1388         cdprintf("\n. ... raw event: size %d bytes\n", event->header.size);
1389
1390         for (i = 0; i < event->header.size; i++) {
1391                 if ((i & 15) == 0) {
1392                         dprintf(".");
1393                         cdprintf("  %04x: ", i);
1394                 }
1395
1396                 cdprintf(" %02x", raw_event[i]);
1397
1398                 if (((i & 15) == 15) || i == event->header.size-1) {
1399                         cdprintf("  ");
1400                         for (j = 0; j < 15-(i & 15); j++)
1401                                 cdprintf("   ");
1402                         for (j = 0; j < (i & 15); j++) {
1403                                 if (isprint(raw_event[i-15+j]))
1404                                         cdprintf("%c", raw_event[i-15+j]);
1405                                 else
1406                                         cdprintf(".");
1407                         }
1408                         cdprintf("\n");
1409                 }
1410         }
1411         dprintf(".\n");
1412 }
1413
1414 static int
1415 process_read_event(event_t *event, unsigned long offset, unsigned long head)
1416 {
1417         dprintf("%p [%p]: PERF_EVENT_READ: %d %d %Lu\n",
1418                         (void *)(offset + head),
1419                         (void *)(long)(event->header.size),
1420                         event->read.pid,
1421                         event->read.tid,
1422                         event->read.value);
1423
1424         return 0;
1425 }
1426
1427 static int
1428 process_event(event_t *event, unsigned long offset, unsigned long head)
1429 {
1430         trace_event(event);
1431
1432         switch (event->header.type) {
1433         case PERF_EVENT_SAMPLE:
1434                 return process_sample_event(event, offset, head);
1435
1436         case PERF_EVENT_MMAP:
1437                 return process_mmap_event(event, offset, head);
1438
1439         case PERF_EVENT_COMM:
1440                 return process_comm_event(event, offset, head);
1441
1442         case PERF_EVENT_FORK:
1443                 return process_fork_event(event, offset, head);
1444
1445         case PERF_EVENT_PERIOD:
1446                 return process_period_event(event, offset, head);
1447
1448         case PERF_EVENT_LOST:
1449                 return process_lost_event(event, offset, head);
1450
1451         case PERF_EVENT_READ:
1452                 return process_read_event(event, offset, head);
1453
1454         /*
1455          * We dont process them right now but they are fine:
1456          */
1457
1458         case PERF_EVENT_THROTTLE:
1459         case PERF_EVENT_UNTHROTTLE:
1460                 return 0;
1461
1462         default:
1463                 return -1;
1464         }
1465
1466         return 0;
1467 }
1468
1469 static struct perf_header       *header;
1470
1471 static u64 perf_header__sample_type(void)
1472 {
1473         u64 sample_type = 0;
1474         int i;
1475
1476         for (i = 0; i < header->attrs; i++) {
1477                 struct perf_header_attr *attr = header->attr[i];
1478
1479                 if (!sample_type)
1480                         sample_type = attr->attr.sample_type;
1481                 else if (sample_type != attr->attr.sample_type)
1482                         die("non matching sample_type");
1483         }
1484
1485         return sample_type;
1486 }
1487
1488 static int __cmd_report(void)
1489 {
1490         int ret, rc = EXIT_FAILURE;
1491         unsigned long offset = 0;
1492         unsigned long head, shift;
1493         struct stat stat;
1494         event_t *event;
1495         uint32_t size;
1496         char *buf;
1497
1498         register_idle_thread();
1499
1500         input = open(input_name, O_RDONLY);
1501         if (input < 0) {
1502                 fprintf(stderr, " failed to open file: %s", input_name);
1503                 if (!strcmp(input_name, "perf.data"))
1504                         fprintf(stderr, "  (try 'perf record' first)");
1505                 fprintf(stderr, "\n");
1506                 exit(-1);
1507         }
1508
1509         ret = fstat(input, &stat);
1510         if (ret < 0) {
1511                 perror("failed to stat file");
1512                 exit(-1);
1513         }
1514
1515         if (!stat.st_size) {
1516                 fprintf(stderr, "zero-sized file, nothing to do!\n");
1517                 exit(0);
1518         }
1519
1520         header = perf_header__read(input);
1521         head = header->data_offset;
1522
1523         sample_type = perf_header__sample_type();
1524
1525         if (sort__has_parent && !(sample_type & PERF_SAMPLE_CALLCHAIN)) {
1526                 fprintf(stderr, "selected --sort parent, but no callchain data\n");
1527                 exit(-1);
1528         }
1529
1530         if (load_kernel() < 0) {
1531                 perror("failed to load kernel symbols");
1532                 return EXIT_FAILURE;
1533         }
1534
1535         if (!full_paths) {
1536                 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
1537                         perror("failed to get the current directory");
1538                         return EXIT_FAILURE;
1539                 }
1540                 cwdlen = strlen(cwd);
1541         } else {
1542                 cwd = NULL;
1543                 cwdlen = 0;
1544         }
1545
1546         shift = page_size * (head / page_size);
1547         offset += shift;
1548         head -= shift;
1549
1550 remap:
1551         buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1552                            MAP_SHARED, input, offset);
1553         if (buf == MAP_FAILED) {
1554                 perror("failed to mmap file");
1555                 exit(-1);
1556         }
1557
1558 more:
1559         event = (event_t *)(buf + head);
1560
1561         size = event->header.size;
1562         if (!size)
1563                 size = 8;
1564
1565         if (head + event->header.size >= page_size * mmap_window) {
1566                 int ret;
1567
1568                 shift = page_size * (head / page_size);
1569
1570                 ret = munmap(buf, page_size * mmap_window);
1571                 assert(ret == 0);
1572
1573                 offset += shift;
1574                 head -= shift;
1575                 goto remap;
1576         }
1577
1578         size = event->header.size;
1579
1580         dprintf("\n%p [%p]: event: %d\n",
1581                         (void *)(offset + head),
1582                         (void *)(long)event->header.size,
1583                         event->header.type);
1584
1585         if (!size || process_event(event, offset, head) < 0) {
1586
1587                 dprintf("%p [%p]: skipping unknown header type: %d\n",
1588                         (void *)(offset + head),
1589                         (void *)(long)(event->header.size),
1590                         event->header.type);
1591
1592                 total_unknown++;
1593
1594                 /*
1595                  * assume we lost track of the stream, check alignment, and
1596                  * increment a single u64 in the hope to catch on again 'soon'.
1597                  */
1598
1599                 if (unlikely(head & 7))
1600                         head &= ~7ULL;
1601
1602                 size = 8;
1603         }
1604
1605         head += size;
1606
1607         if (offset + head >= header->data_offset + header->data_size)
1608                 goto done;
1609
1610         if (offset + head < stat.st_size)
1611                 goto more;
1612
1613 done:
1614         rc = EXIT_SUCCESS;
1615         close(input);
1616
1617         dprintf("      IP events: %10ld\n", total);
1618         dprintf("    mmap events: %10ld\n", total_mmap);
1619         dprintf("    comm events: %10ld\n", total_comm);
1620         dprintf("    fork events: %10ld\n", total_fork);
1621         dprintf("    lost events: %10ld\n", total_lost);
1622         dprintf(" unknown events: %10ld\n", total_unknown);
1623
1624         if (dump_trace)
1625                 return 0;
1626
1627         if (verbose >= 3)
1628                 threads__fprintf(stdout);
1629
1630         if (verbose >= 2)
1631                 dsos__fprintf(stdout);
1632
1633         collapse__resort();
1634         output__resort();
1635         output__fprintf(stdout, total);
1636
1637         return rc;
1638 }
1639
1640 static const char * const report_usage[] = {
1641         "perf report [<options>] <command>",
1642         NULL
1643 };
1644
1645 static const struct option options[] = {
1646         OPT_STRING('i', "input", &input_name, "file",
1647                     "input file name"),
1648         OPT_BOOLEAN('v', "verbose", &verbose,
1649                     "be more verbose (show symbol address, etc)"),
1650         OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1651                     "dump raw trace in ASCII"),
1652         OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
1653         OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1654                    "sort by key(s): pid, comm, dso, symbol, parent"),
1655         OPT_BOOLEAN('P', "full-paths", &full_paths,
1656                     "Don't shorten the pathnames taking into account the cwd"),
1657         OPT_STRING('p', "parent", &parent_pattern, "regex",
1658                    "regex filter to identify parent, see: '--sort parent'"),
1659         OPT_BOOLEAN('x', "exclude-other", &exclude_other,
1660                     "Only display entries with parent-match"),
1661         OPT_BOOLEAN('c', "callchain", &callchain, "Display callchains"),
1662         OPT_END()
1663 };
1664
1665 static void setup_sorting(void)
1666 {
1667         char *tmp, *tok, *str = strdup(sort_order);
1668
1669         for (tok = strtok_r(str, ", ", &tmp);
1670                         tok; tok = strtok_r(NULL, ", ", &tmp)) {
1671                 if (sort_dimension__add(tok) < 0) {
1672                         error("Unknown --sort key: `%s'", tok);
1673                         usage_with_options(report_usage, options);
1674                 }
1675         }
1676
1677         free(str);
1678 }
1679
1680 int cmd_report(int argc, const char **argv, const char *prefix)
1681 {
1682         symbol__init();
1683
1684         page_size = getpagesize();
1685
1686         argc = parse_options(argc, argv, options, report_usage, 0);
1687
1688         setup_sorting();
1689
1690         if (parent_pattern != default_parent_pattern)
1691                 sort_dimension__add("parent");
1692         else
1693                 exclude_other = 0;
1694
1695         /*
1696          * Any (unrecognized) arguments left?
1697          */
1698         if (argc)
1699                 usage_with_options(report_usage, options);
1700
1701         setup_pager();
1702
1703         return __cmd_report();
1704 }