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