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