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