perf report: Fix and improve the displaying of per-thread event counters
[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 <linux/list.h>
14 #include "util/cache.h"
15 #include <linux/rbtree.h>
16 #include "util/symbol.h"
17 #include "util/string.h"
18 #include "util/callchain.h"
19 #include "util/strlist.h"
20 #include "util/values.h"
21
22 #include "perf.h"
23 #include "util/header.h"
24
25 #include "util/parse-options.h"
26 #include "util/parse-events.h"
27
28 #define SHOW_KERNEL     1
29 #define SHOW_USER       2
30 #define SHOW_HV         4
31
32 static char             const *input_name = "perf.data";
33 static char             *vmlinux = NULL;
34
35 static char             default_sort_order[] = "comm,dso,symbol";
36 static char             *sort_order = default_sort_order;
37 static char             *dso_list_str, *comm_list_str, *sym_list_str,
38                         *col_width_list_str;
39 static struct strlist   *dso_list, *comm_list, *sym_list;
40 static char             *field_sep;
41
42 static int              input;
43 static int              show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
44
45 static int              dump_trace = 0;
46 #define dprintf(x...)   do { if (dump_trace) printf(x); } while (0)
47 #define cdprintf(x...)  do { if (dump_trace) color_fprintf(stdout, color, x); } while (0)
48
49 static int              verbose;
50 #define eprintf(x...)   do { if (verbose) fprintf(stderr, x); } while (0)
51
52 static int              modules;
53
54 static int              full_paths;
55 static int              show_nr_samples;
56
57 static int              show_threads;
58 static struct perf_read_values  show_threads_values;
59
60 static unsigned long    page_size;
61 static unsigned long    mmap_window = 32;
62
63 static char             default_parent_pattern[] = "^sys_|^do_page_fault";
64 static char             *parent_pattern = default_parent_pattern;
65 static regex_t          parent_regex;
66
67 static int              exclude_other = 1;
68
69 static char             callchain_default_opt[] = "fractal,0.5";
70
71 static int              callchain;
72
73 static
74 struct callchain_param  callchain_param = {
75         .mode   = CHAIN_GRAPH_REL,
76         .min_percent = 0.5
77 };
78
79 static u64              sample_type;
80
81 struct ip_event {
82         struct perf_event_header header;
83         u64 ip;
84         u32 pid, tid;
85         unsigned char __more_data[];
86 };
87
88 struct mmap_event {
89         struct perf_event_header header;
90         u32 pid, tid;
91         u64 start;
92         u64 len;
93         u64 pgoff;
94         char filename[PATH_MAX];
95 };
96
97 struct comm_event {
98         struct perf_event_header header;
99         u32 pid, tid;
100         char comm[16];
101 };
102
103 struct fork_event {
104         struct perf_event_header header;
105         u32 pid, ppid;
106         u32 tid, ptid;
107 };
108
109 struct lost_event {
110         struct perf_event_header header;
111         u64 id;
112         u64 lost;
113 };
114
115 struct read_event {
116         struct perf_event_header header;
117         u32 pid,tid;
118         u64 value;
119         u64 time_enabled;
120         u64 time_running;
121         u64 id;
122 };
123
124 typedef union event_union {
125         struct perf_event_header        header;
126         struct ip_event                 ip;
127         struct mmap_event               mmap;
128         struct comm_event               comm;
129         struct fork_event               fork;
130         struct lost_event               lost;
131         struct read_event               read;
132 } event_t;
133
134 static int repsep_fprintf(FILE *fp, const char *fmt, ...)
135 {
136         int n;
137         va_list ap;
138
139         va_start(ap, fmt);
140         if (!field_sep)
141                 n = vfprintf(fp, fmt, ap);
142         else {
143                 char *bf = NULL;
144                 n = vasprintf(&bf, fmt, ap);
145                 if (n > 0) {
146                         char *sep = bf;
147                         while (1) {
148                                 sep = strchr(sep, *field_sep);
149                                 if (sep == NULL)
150                                         break;
151                                 *sep = '.';
152                         }
153                 }
154                 fputs(bf, fp);
155                 free(bf);
156         }
157         va_end(ap);
158         return n;
159 }
160
161 static LIST_HEAD(dsos);
162 static struct dso *kernel_dso;
163 static struct dso *vdso;
164 static struct dso *hypervisor_dso;
165
166 static void dsos__add(struct dso *dso)
167 {
168         list_add_tail(&dso->node, &dsos);
169 }
170
171 static struct dso *dsos__find(const char *name)
172 {
173         struct dso *pos;
174
175         list_for_each_entry(pos, &dsos, node)
176                 if (strcmp(pos->name, name) == 0)
177                         return pos;
178         return NULL;
179 }
180
181 static struct dso *dsos__findnew(const char *name)
182 {
183         struct dso *dso = dsos__find(name);
184         int nr;
185
186         if (dso)
187                 return dso;
188
189         dso = dso__new(name, 0);
190         if (!dso)
191                 goto out_delete_dso;
192
193         nr = dso__load(dso, NULL, verbose);
194         if (nr < 0) {
195                 eprintf("Failed to open: %s\n", name);
196                 goto out_delete_dso;
197         }
198         if (!nr)
199                 eprintf("No symbols found in: %s, maybe install a debug package?\n", name);
200
201         dsos__add(dso);
202
203         return dso;
204
205 out_delete_dso:
206         dso__delete(dso);
207         return NULL;
208 }
209
210 static void dsos__fprintf(FILE *fp)
211 {
212         struct dso *pos;
213
214         list_for_each_entry(pos, &dsos, node)
215                 dso__fprintf(pos, fp);
216 }
217
218 static struct symbol *vdso__find_symbol(struct dso *dso, u64 ip)
219 {
220         return dso__find_symbol(dso, ip);
221 }
222
223 static int load_kernel(void)
224 {
225         int err;
226
227         kernel_dso = dso__new("[kernel]", 0);
228         if (!kernel_dso)
229                 return -1;
230
231         err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose, modules);
232         if (err <= 0) {
233                 dso__delete(kernel_dso);
234                 kernel_dso = NULL;
235         } else
236                 dsos__add(kernel_dso);
237
238         vdso = dso__new("[vdso]", 0);
239         if (!vdso)
240                 return -1;
241
242         vdso->find_symbol = vdso__find_symbol;
243
244         dsos__add(vdso);
245
246         hypervisor_dso = dso__new("[hypervisor]", 0);
247         if (!hypervisor_dso)
248                 return -1;
249         dsos__add(hypervisor_dso);
250
251         return err;
252 }
253
254 static char __cwd[PATH_MAX];
255 static char *cwd = __cwd;
256 static int cwdlen;
257
258 static int strcommon(const char *pathname)
259 {
260         int n = 0;
261
262         while (n < cwdlen && pathname[n] == cwd[n])
263                 ++n;
264
265         return n;
266 }
267
268 struct map {
269         struct list_head node;
270         u64      start;
271         u64      end;
272         u64      pgoff;
273         u64      (*map_ip)(struct map *, u64);
274         struct dso       *dso;
275 };
276
277 static u64 map__map_ip(struct map *map, u64 ip)
278 {
279         return ip - map->start + map->pgoff;
280 }
281
282 static u64 vdso__map_ip(struct map *map __used, u64 ip)
283 {
284         return ip;
285 }
286
287 static inline int is_anon_memory(const char *filename)
288 {
289         return strcmp(filename, "//anon") == 0;
290 }
291
292 static struct map *map__new(struct mmap_event *event)
293 {
294         struct map *self = malloc(sizeof(*self));
295
296         if (self != NULL) {
297                 const char *filename = event->filename;
298                 char newfilename[PATH_MAX];
299                 int anon;
300
301                 if (cwd) {
302                         int n = strcommon(filename);
303
304                         if (n == cwdlen) {
305                                 snprintf(newfilename, sizeof(newfilename),
306                                          ".%s", filename + n);
307                                 filename = newfilename;
308                         }
309                 }
310
311                 anon = is_anon_memory(filename);
312
313                 if (anon) {
314                         snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
315                         filename = newfilename;
316                 }
317
318                 self->start = event->start;
319                 self->end   = event->start + event->len;
320                 self->pgoff = event->pgoff;
321
322                 self->dso = dsos__findnew(filename);
323                 if (self->dso == NULL)
324                         goto out_delete;
325
326                 if (self->dso == vdso || anon)
327                         self->map_ip = vdso__map_ip;
328                 else
329                         self->map_ip = map__map_ip;
330         }
331         return self;
332 out_delete:
333         free(self);
334         return NULL;
335 }
336
337 static struct map *map__clone(struct map *self)
338 {
339         struct map *map = malloc(sizeof(*self));
340
341         if (!map)
342                 return NULL;
343
344         memcpy(map, self, sizeof(*self));
345
346         return map;
347 }
348
349 static int map__overlap(struct map *l, struct map *r)
350 {
351         if (l->start > r->start) {
352                 struct map *t = l;
353                 l = r;
354                 r = t;
355         }
356
357         if (l->end > r->start)
358                 return 1;
359
360         return 0;
361 }
362
363 static size_t map__fprintf(struct map *self, FILE *fp)
364 {
365         return fprintf(fp, " %Lx-%Lx %Lx %s\n",
366                        self->start, self->end, self->pgoff, self->dso->name);
367 }
368
369
370 struct thread {
371         struct rb_node   rb_node;
372         struct list_head maps;
373         pid_t            pid;
374         char             *comm;
375 };
376
377 static struct thread *thread__new(pid_t pid)
378 {
379         struct thread *self = malloc(sizeof(*self));
380
381         if (self != NULL) {
382                 self->pid = pid;
383                 self->comm = malloc(32);
384                 if (self->comm)
385                         snprintf(self->comm, 32, ":%d", self->pid);
386                 INIT_LIST_HEAD(&self->maps);
387         }
388
389         return self;
390 }
391
392 static unsigned int dsos__col_width,
393                     comms__col_width,
394                     threads__col_width;
395
396 static int thread__set_comm(struct thread *self, const char *comm)
397 {
398         if (self->comm)
399                 free(self->comm);
400         self->comm = strdup(comm);
401         if (!self->comm)
402                 return -ENOMEM;
403
404         if (!col_width_list_str && !field_sep &&
405             (!comm_list || strlist__has_entry(comm_list, comm))) {
406                 unsigned int slen = strlen(comm);
407                 if (slen > comms__col_width) {
408                         comms__col_width = slen;
409                         threads__col_width = slen + 6;
410                 }
411         }
412
413         return 0;
414 }
415
416 static size_t thread__fprintf(struct thread *self, FILE *fp)
417 {
418         struct map *pos;
419         size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
420
421         list_for_each_entry(pos, &self->maps, node)
422                 ret += map__fprintf(pos, fp);
423
424         return ret;
425 }
426
427
428 static struct rb_root threads;
429 static struct thread *last_match;
430
431 static struct thread *threads__findnew(pid_t pid)
432 {
433         struct rb_node **p = &threads.rb_node;
434         struct rb_node *parent = NULL;
435         struct thread *th;
436
437         /*
438          * Font-end cache - PID lookups come in blocks,
439          * so most of the time we dont have to look up
440          * the full rbtree:
441          */
442         if (last_match && last_match->pid == pid)
443                 return last_match;
444
445         while (*p != NULL) {
446                 parent = *p;
447                 th = rb_entry(parent, struct thread, rb_node);
448
449                 if (th->pid == pid) {
450                         last_match = th;
451                         return th;
452                 }
453
454                 if (pid < th->pid)
455                         p = &(*p)->rb_left;
456                 else
457                         p = &(*p)->rb_right;
458         }
459
460         th = thread__new(pid);
461         if (th != NULL) {
462                 rb_link_node(&th->rb_node, parent, p);
463                 rb_insert_color(&th->rb_node, &threads);
464                 last_match = th;
465         }
466
467         return th;
468 }
469
470 static void thread__insert_map(struct thread *self, struct map *map)
471 {
472         struct map *pos, *tmp;
473
474         list_for_each_entry_safe(pos, tmp, &self->maps, node) {
475                 if (map__overlap(pos, map)) {
476                         if (verbose >= 2) {
477                                 printf("overlapping maps:\n");
478                                 map__fprintf(map, stdout);
479                                 map__fprintf(pos, stdout);
480                         }
481
482                         if (map->start <= pos->start && map->end > pos->start)
483                                 pos->start = map->end;
484
485                         if (map->end >= pos->end && map->start < pos->end)
486                                 pos->end = map->start;
487
488                         if (verbose >= 2) {
489                                 printf("after collision:\n");
490                                 map__fprintf(pos, stdout);
491                         }
492
493                         if (pos->start >= pos->end) {
494                                 list_del_init(&pos->node);
495                                 free(pos);
496                         }
497                 }
498         }
499
500         list_add_tail(&map->node, &self->maps);
501 }
502
503 static int thread__fork(struct thread *self, struct thread *parent)
504 {
505         struct map *map;
506
507         if (self->comm)
508                 free(self->comm);
509         self->comm = strdup(parent->comm);
510         if (!self->comm)
511                 return -ENOMEM;
512
513         list_for_each_entry(map, &parent->maps, node) {
514                 struct map *new = map__clone(map);
515                 if (!new)
516                         return -ENOMEM;
517                 thread__insert_map(self, new);
518         }
519
520         return 0;
521 }
522
523 static struct map *thread__find_map(struct thread *self, u64 ip)
524 {
525         struct map *pos;
526
527         if (self == NULL)
528                 return NULL;
529
530         list_for_each_entry(pos, &self->maps, node)
531                 if (ip >= pos->start && ip <= pos->end)
532                         return pos;
533
534         return NULL;
535 }
536
537 static size_t threads__fprintf(FILE *fp)
538 {
539         size_t ret = 0;
540         struct rb_node *nd;
541
542         for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
543                 struct thread *pos = rb_entry(nd, struct thread, rb_node);
544
545                 ret += thread__fprintf(pos, fp);
546         }
547
548         return ret;
549 }
550
551 /*
552  * histogram, sorted on item, collects counts
553  */
554
555 static struct rb_root hist;
556
557 struct hist_entry {
558         struct rb_node          rb_node;
559
560         struct thread           *thread;
561         struct map              *map;
562         struct dso              *dso;
563         struct symbol           *sym;
564         struct symbol           *parent;
565         u64                     ip;
566         char                    level;
567         struct callchain_node   callchain;
568         struct rb_root          sorted_chain;
569
570         u64                     count;
571 };
572
573 /*
574  * configurable sorting bits
575  */
576
577 struct sort_entry {
578         struct list_head list;
579
580         char *header;
581
582         int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
583         int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
584         size_t  (*print)(FILE *fp, struct hist_entry *, unsigned int width);
585         unsigned int *width;
586         bool    elide;
587 };
588
589 static int64_t cmp_null(void *l, void *r)
590 {
591         if (!l && !r)
592                 return 0;
593         else if (!l)
594                 return -1;
595         else
596                 return 1;
597 }
598
599 /* --sort pid */
600
601 static int64_t
602 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
603 {
604         return right->thread->pid - left->thread->pid;
605 }
606
607 static size_t
608 sort__thread_print(FILE *fp, struct hist_entry *self, unsigned int width)
609 {
610         return repsep_fprintf(fp, "%*s:%5d", width - 6,
611                               self->thread->comm ?: "", self->thread->pid);
612 }
613
614 static struct sort_entry sort_thread = {
615         .header = "Command:  Pid",
616         .cmp    = sort__thread_cmp,
617         .print  = sort__thread_print,
618         .width  = &threads__col_width,
619 };
620
621 /* --sort comm */
622
623 static int64_t
624 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
625 {
626         return right->thread->pid - left->thread->pid;
627 }
628
629 static int64_t
630 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
631 {
632         char *comm_l = left->thread->comm;
633         char *comm_r = right->thread->comm;
634
635         if (!comm_l || !comm_r)
636                 return cmp_null(comm_l, comm_r);
637
638         return strcmp(comm_l, comm_r);
639 }
640
641 static size_t
642 sort__comm_print(FILE *fp, struct hist_entry *self, unsigned int width)
643 {
644         return repsep_fprintf(fp, "%*s", width, self->thread->comm);
645 }
646
647 static struct sort_entry sort_comm = {
648         .header         = "Command",
649         .cmp            = sort__comm_cmp,
650         .collapse       = sort__comm_collapse,
651         .print          = sort__comm_print,
652         .width          = &comms__col_width,
653 };
654
655 /* --sort dso */
656
657 static int64_t
658 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
659 {
660         struct dso *dso_l = left->dso;
661         struct dso *dso_r = right->dso;
662
663         if (!dso_l || !dso_r)
664                 return cmp_null(dso_l, dso_r);
665
666         return strcmp(dso_l->name, dso_r->name);
667 }
668
669 static size_t
670 sort__dso_print(FILE *fp, struct hist_entry *self, unsigned int width)
671 {
672         if (self->dso)
673                 return repsep_fprintf(fp, "%-*s", width, self->dso->name);
674
675         return repsep_fprintf(fp, "%*llx", width, (u64)self->ip);
676 }
677
678 static struct sort_entry sort_dso = {
679         .header = "Shared Object",
680         .cmp    = sort__dso_cmp,
681         .print  = sort__dso_print,
682         .width  = &dsos__col_width,
683 };
684
685 /* --sort symbol */
686
687 static int64_t
688 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
689 {
690         u64 ip_l, ip_r;
691
692         if (left->sym == right->sym)
693                 return 0;
694
695         ip_l = left->sym ? left->sym->start : left->ip;
696         ip_r = right->sym ? right->sym->start : right->ip;
697
698         return (int64_t)(ip_r - ip_l);
699 }
700
701 static size_t
702 sort__sym_print(FILE *fp, struct hist_entry *self, unsigned int width __used)
703 {
704         size_t ret = 0;
705
706         if (verbose)
707                 ret += repsep_fprintf(fp, "%#018llx %c ", (u64)self->ip,
708                                       dso__symtab_origin(self->dso));
709
710         ret += repsep_fprintf(fp, "[%c] ", self->level);
711         if (self->sym) {
712                 ret += repsep_fprintf(fp, "%s", self->sym->name);
713
714                 if (self->sym->module)
715                         ret += repsep_fprintf(fp, "\t[%s]",
716                                              self->sym->module->name);
717         } else {
718                 ret += repsep_fprintf(fp, "%#016llx", (u64)self->ip);
719         }
720
721         return ret;
722 }
723
724 static struct sort_entry sort_sym = {
725         .header = "Symbol",
726         .cmp    = sort__sym_cmp,
727         .print  = sort__sym_print,
728 };
729
730 /* --sort parent */
731
732 static int64_t
733 sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
734 {
735         struct symbol *sym_l = left->parent;
736         struct symbol *sym_r = right->parent;
737
738         if (!sym_l || !sym_r)
739                 return cmp_null(sym_l, sym_r);
740
741         return strcmp(sym_l->name, sym_r->name);
742 }
743
744 static size_t
745 sort__parent_print(FILE *fp, struct hist_entry *self, unsigned int width)
746 {
747         return repsep_fprintf(fp, "%-*s", width,
748                               self->parent ? self->parent->name : "[other]");
749 }
750
751 static unsigned int parent_symbol__col_width;
752
753 static struct sort_entry sort_parent = {
754         .header = "Parent symbol",
755         .cmp    = sort__parent_cmp,
756         .print  = sort__parent_print,
757         .width  = &parent_symbol__col_width,
758 };
759
760 static int sort__need_collapse = 0;
761 static int sort__has_parent = 0;
762
763 struct sort_dimension {
764         char                    *name;
765         struct sort_entry       *entry;
766         int                     taken;
767 };
768
769 static struct sort_dimension sort_dimensions[] = {
770         { .name = "pid",        .entry = &sort_thread,  },
771         { .name = "comm",       .entry = &sort_comm,    },
772         { .name = "dso",        .entry = &sort_dso,     },
773         { .name = "symbol",     .entry = &sort_sym,     },
774         { .name = "parent",     .entry = &sort_parent,  },
775 };
776
777 static LIST_HEAD(hist_entry__sort_list);
778
779 static int sort_dimension__add(char *tok)
780 {
781         unsigned int i;
782
783         for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
784                 struct sort_dimension *sd = &sort_dimensions[i];
785
786                 if (sd->taken)
787                         continue;
788
789                 if (strncasecmp(tok, sd->name, strlen(tok)))
790                         continue;
791
792                 if (sd->entry->collapse)
793                         sort__need_collapse = 1;
794
795                 if (sd->entry == &sort_parent) {
796                         int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
797                         if (ret) {
798                                 char err[BUFSIZ];
799
800                                 regerror(ret, &parent_regex, err, sizeof(err));
801                                 fprintf(stderr, "Invalid regex: %s\n%s",
802                                         parent_pattern, err);
803                                 exit(-1);
804                         }
805                         sort__has_parent = 1;
806                 }
807
808                 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
809                 sd->taken = 1;
810
811                 return 0;
812         }
813
814         return -ESRCH;
815 }
816
817 static int64_t
818 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
819 {
820         struct sort_entry *se;
821         int64_t cmp = 0;
822
823         list_for_each_entry(se, &hist_entry__sort_list, list) {
824                 cmp = se->cmp(left, right);
825                 if (cmp)
826                         break;
827         }
828
829         return cmp;
830 }
831
832 static int64_t
833 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
834 {
835         struct sort_entry *se;
836         int64_t cmp = 0;
837
838         list_for_each_entry(se, &hist_entry__sort_list, list) {
839                 int64_t (*f)(struct hist_entry *, struct hist_entry *);
840
841                 f = se->collapse ?: se->cmp;
842
843                 cmp = f(left, right);
844                 if (cmp)
845                         break;
846         }
847
848         return cmp;
849 }
850
851 static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask)
852 {
853         int i;
854         size_t ret = 0;
855
856         ret += fprintf(fp, "%s", "                ");
857
858         for (i = 0; i < depth; i++)
859                 if (depth_mask & (1 << i))
860                         ret += fprintf(fp, "|          ");
861                 else
862                         ret += fprintf(fp, "           ");
863
864         ret += fprintf(fp, "\n");
865
866         return ret;
867 }
868 static size_t
869 ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, int depth,
870                        int depth_mask, int count, u64 total_samples,
871                        int hits)
872 {
873         int i;
874         size_t ret = 0;
875
876         ret += fprintf(fp, "%s", "                ");
877         for (i = 0; i < depth; i++) {
878                 if (depth_mask & (1 << i))
879                         ret += fprintf(fp, "|");
880                 else
881                         ret += fprintf(fp, " ");
882                 if (!count && i == depth - 1) {
883                         double percent;
884
885                         percent = hits * 100.0 / total_samples;
886                         ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
887                 } else
888                         ret += fprintf(fp, "%s", "          ");
889         }
890         if (chain->sym)
891                 ret += fprintf(fp, "%s\n", chain->sym->name);
892         else
893                 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
894
895         return ret;
896 }
897
898 static struct symbol *rem_sq_bracket;
899 static struct callchain_list rem_hits;
900
901 static void init_rem_hits(void)
902 {
903         rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
904         if (!rem_sq_bracket) {
905                 fprintf(stderr, "Not enough memory to display remaining hits\n");
906                 return;
907         }
908
909         strcpy(rem_sq_bracket->name, "[...]");
910         rem_hits.sym = rem_sq_bracket;
911 }
912
913 static size_t
914 callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
915                         u64 total_samples, int depth, int depth_mask)
916 {
917         struct rb_node *node, *next;
918         struct callchain_node *child;
919         struct callchain_list *chain;
920         int new_depth_mask = depth_mask;
921         u64 new_total;
922         u64 remaining;
923         size_t ret = 0;
924         int i;
925
926         if (callchain_param.mode == CHAIN_GRAPH_REL)
927                 new_total = self->children_hit;
928         else
929                 new_total = total_samples;
930
931         remaining = new_total;
932
933         node = rb_first(&self->rb_root);
934         while (node) {
935                 u64 cumul;
936
937                 child = rb_entry(node, struct callchain_node, rb_node);
938                 cumul = cumul_hits(child);
939                 remaining -= cumul;
940
941                 /*
942                  * The depth mask manages the output of pipes that show
943                  * the depth. We don't want to keep the pipes of the current
944                  * level for the last child of this depth.
945                  * Except if we have remaining filtered hits. They will
946                  * supersede the last child
947                  */
948                 next = rb_next(node);
949                 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
950                         new_depth_mask &= ~(1 << (depth - 1));
951
952                 /*
953                  * But we keep the older depth mask for the line seperator
954                  * to keep the level link until we reach the last child
955                  */
956                 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask);
957                 i = 0;
958                 list_for_each_entry(chain, &child->val, list) {
959                         if (chain->ip >= PERF_CONTEXT_MAX)
960                                 continue;
961                         ret += ipchain__fprintf_graph(fp, chain, depth,
962                                                       new_depth_mask, i++,
963                                                       new_total,
964                                                       cumul);
965                 }
966                 ret += callchain__fprintf_graph(fp, child, new_total,
967                                                 depth + 1,
968                                                 new_depth_mask | (1 << depth));
969                 node = next;
970         }
971
972         if (callchain_param.mode == CHAIN_GRAPH_REL &&
973                 remaining && remaining != new_total) {
974
975                 if (!rem_sq_bracket)
976                         return ret;
977
978                 new_depth_mask &= ~(1 << (depth - 1));
979
980                 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
981                                               new_depth_mask, 0, new_total,
982                                               remaining);
983         }
984
985         return ret;
986 }
987
988 static size_t
989 callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
990                         u64 total_samples)
991 {
992         struct callchain_list *chain;
993         size_t ret = 0;
994
995         if (!self)
996                 return 0;
997
998         ret += callchain__fprintf_flat(fp, self->parent, total_samples);
999
1000
1001         list_for_each_entry(chain, &self->val, list) {
1002                 if (chain->ip >= PERF_CONTEXT_MAX)
1003                         continue;
1004                 if (chain->sym)
1005                         ret += fprintf(fp, "                %s\n", chain->sym->name);
1006                 else
1007                         ret += fprintf(fp, "                %p\n",
1008                                         (void *)(long)chain->ip);
1009         }
1010
1011         return ret;
1012 }
1013
1014 static size_t
1015 hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
1016                               u64 total_samples)
1017 {
1018         struct rb_node *rb_node;
1019         struct callchain_node *chain;
1020         size_t ret = 0;
1021
1022         rb_node = rb_first(&self->sorted_chain);
1023         while (rb_node) {
1024                 double percent;
1025
1026                 chain = rb_entry(rb_node, struct callchain_node, rb_node);
1027                 percent = chain->hit * 100.0 / total_samples;
1028                 switch (callchain_param.mode) {
1029                 case CHAIN_FLAT:
1030                         ret += percent_color_fprintf(fp, "           %6.2f%%\n",
1031                                                      percent);
1032                         ret += callchain__fprintf_flat(fp, chain, total_samples);
1033                         break;
1034                 case CHAIN_GRAPH_ABS: /* Falldown */
1035                 case CHAIN_GRAPH_REL:
1036                         ret += callchain__fprintf_graph(fp, chain,
1037                                                         total_samples, 1, 1);
1038                 default:
1039                         break;
1040                 }
1041                 ret += fprintf(fp, "\n");
1042                 rb_node = rb_next(rb_node);
1043         }
1044
1045         return ret;
1046 }
1047
1048
1049 static size_t
1050 hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
1051 {
1052         struct sort_entry *se;
1053         size_t ret;
1054
1055         if (exclude_other && !self->parent)
1056                 return 0;
1057
1058         if (total_samples)
1059                 ret = percent_color_fprintf(fp,
1060                                             field_sep ? "%.2f" : "   %6.2f%%",
1061                                         (self->count * 100.0) / total_samples);
1062         else
1063                 ret = fprintf(fp, field_sep ? "%lld" : "%12lld ", self->count);
1064
1065         if (show_nr_samples) {
1066                 if (field_sep)
1067                         fprintf(fp, "%c%lld", *field_sep, self->count);
1068                 else
1069                         fprintf(fp, "%11lld", self->count);
1070         }
1071
1072         list_for_each_entry(se, &hist_entry__sort_list, list) {
1073                 if (se->elide)
1074                         continue;
1075
1076                 fprintf(fp, "%s", field_sep ?: "  ");
1077                 ret += se->print(fp, self, se->width ? *se->width : 0);
1078         }
1079
1080         ret += fprintf(fp, "\n");
1081
1082         if (callchain)
1083                 hist_entry_callchain__fprintf(fp, self, total_samples);
1084
1085         return ret;
1086 }
1087
1088 /*
1089  *
1090  */
1091
1092 static void dso__calc_col_width(struct dso *self)
1093 {
1094         if (!col_width_list_str && !field_sep &&
1095             (!dso_list || strlist__has_entry(dso_list, self->name))) {
1096                 unsigned int slen = strlen(self->name);
1097                 if (slen > dsos__col_width)
1098                         dsos__col_width = slen;
1099         }
1100
1101         self->slen_calculated = 1;
1102 }
1103
1104 static struct symbol *
1105 resolve_symbol(struct thread *thread, struct map **mapp,
1106                struct dso **dsop, u64 *ipp)
1107 {
1108         struct dso *dso = dsop ? *dsop : NULL;
1109         struct map *map = mapp ? *mapp : NULL;
1110         u64 ip = *ipp;
1111
1112         if (!thread)
1113                 return NULL;
1114
1115         if (dso)
1116                 goto got_dso;
1117
1118         if (map)
1119                 goto got_map;
1120
1121         map = thread__find_map(thread, ip);
1122         if (map != NULL) {
1123                 /*
1124                  * We have to do this here as we may have a dso
1125                  * with no symbol hit that has a name longer than
1126                  * the ones with symbols sampled.
1127                  */
1128                 if (!sort_dso.elide && !map->dso->slen_calculated)
1129                         dso__calc_col_width(map->dso);
1130
1131                 if (mapp)
1132                         *mapp = map;
1133 got_map:
1134                 ip = map->map_ip(map, ip);
1135
1136                 dso = map->dso;
1137         } else {
1138                 /*
1139                  * If this is outside of all known maps,
1140                  * and is a negative address, try to look it
1141                  * up in the kernel dso, as it might be a
1142                  * vsyscall (which executes in user-mode):
1143                  */
1144                 if ((long long)ip < 0)
1145                 dso = kernel_dso;
1146         }
1147         dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
1148         dprintf(" ...... map: %Lx -> %Lx\n", *ipp, ip);
1149         *ipp  = ip;
1150
1151         if (dsop)
1152                 *dsop = dso;
1153
1154         if (!dso)
1155                 return NULL;
1156 got_dso:
1157         return dso->find_symbol(dso, ip);
1158 }
1159
1160 static int call__match(struct symbol *sym)
1161 {
1162         if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
1163                 return 1;
1164
1165         return 0;
1166 }
1167
1168 static struct symbol **
1169 resolve_callchain(struct thread *thread, struct map *map __used,
1170                     struct ip_callchain *chain, struct hist_entry *entry)
1171 {
1172         u64 context = PERF_CONTEXT_MAX;
1173         struct symbol **syms = NULL;
1174         unsigned int i;
1175
1176         if (callchain) {
1177                 syms = calloc(chain->nr, sizeof(*syms));
1178                 if (!syms) {
1179                         fprintf(stderr, "Can't allocate memory for symbols\n");
1180                         exit(-1);
1181                 }
1182         }
1183
1184         for (i = 0; i < chain->nr; i++) {
1185                 u64 ip = chain->ips[i];
1186                 struct dso *dso = NULL;
1187                 struct symbol *sym;
1188
1189                 if (ip >= PERF_CONTEXT_MAX) {
1190                         context = ip;
1191                         continue;
1192                 }
1193
1194                 switch (context) {
1195                 case PERF_CONTEXT_HV:
1196                         dso = hypervisor_dso;
1197                         break;
1198                 case PERF_CONTEXT_KERNEL:
1199                         dso = kernel_dso;
1200                         break;
1201                 default:
1202                         break;
1203                 }
1204
1205                 sym = resolve_symbol(thread, NULL, &dso, &ip);
1206
1207                 if (sym) {
1208                         if (sort__has_parent && call__match(sym) &&
1209                             !entry->parent)
1210                                 entry->parent = sym;
1211                         if (!callchain)
1212                                 break;
1213                         syms[i] = sym;
1214                 }
1215         }
1216
1217         return syms;
1218 }
1219
1220 /*
1221  * collect histogram counts
1222  */
1223
1224 static int
1225 hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
1226                 struct symbol *sym, u64 ip, struct ip_callchain *chain,
1227                 char level, u64 count)
1228 {
1229         struct rb_node **p = &hist.rb_node;
1230         struct rb_node *parent = NULL;
1231         struct hist_entry *he;
1232         struct symbol **syms = NULL;
1233         struct hist_entry entry = {
1234                 .thread = thread,
1235                 .map    = map,
1236                 .dso    = dso,
1237                 .sym    = sym,
1238                 .ip     = ip,
1239                 .level  = level,
1240                 .count  = count,
1241                 .parent = NULL,
1242                 .sorted_chain = RB_ROOT
1243         };
1244         int cmp;
1245
1246         if ((sort__has_parent || callchain) && chain)
1247                 syms = resolve_callchain(thread, map, chain, &entry);
1248
1249         while (*p != NULL) {
1250                 parent = *p;
1251                 he = rb_entry(parent, struct hist_entry, rb_node);
1252
1253                 cmp = hist_entry__cmp(&entry, he);
1254
1255                 if (!cmp) {
1256                         he->count += count;
1257                         if (callchain) {
1258                                 append_chain(&he->callchain, chain, syms);
1259                                 free(syms);
1260                         }
1261                         return 0;
1262                 }
1263
1264                 if (cmp < 0)
1265                         p = &(*p)->rb_left;
1266                 else
1267                         p = &(*p)->rb_right;
1268         }
1269
1270         he = malloc(sizeof(*he));
1271         if (!he)
1272                 return -ENOMEM;
1273         *he = entry;
1274         if (callchain) {
1275                 callchain_init(&he->callchain);
1276                 append_chain(&he->callchain, chain, syms);
1277                 free(syms);
1278         }
1279         rb_link_node(&he->rb_node, parent, p);
1280         rb_insert_color(&he->rb_node, &hist);
1281
1282         return 0;
1283 }
1284
1285 static void hist_entry__free(struct hist_entry *he)
1286 {
1287         free(he);
1288 }
1289
1290 /*
1291  * collapse the histogram
1292  */
1293
1294 static struct rb_root collapse_hists;
1295
1296 static void collapse__insert_entry(struct hist_entry *he)
1297 {
1298         struct rb_node **p = &collapse_hists.rb_node;
1299         struct rb_node *parent = NULL;
1300         struct hist_entry *iter;
1301         int64_t cmp;
1302
1303         while (*p != NULL) {
1304                 parent = *p;
1305                 iter = rb_entry(parent, struct hist_entry, rb_node);
1306
1307                 cmp = hist_entry__collapse(iter, he);
1308
1309                 if (!cmp) {
1310                         iter->count += he->count;
1311                         hist_entry__free(he);
1312                         return;
1313                 }
1314
1315                 if (cmp < 0)
1316                         p = &(*p)->rb_left;
1317                 else
1318                         p = &(*p)->rb_right;
1319         }
1320
1321         rb_link_node(&he->rb_node, parent, p);
1322         rb_insert_color(&he->rb_node, &collapse_hists);
1323 }
1324
1325 static void collapse__resort(void)
1326 {
1327         struct rb_node *next;
1328         struct hist_entry *n;
1329
1330         if (!sort__need_collapse)
1331                 return;
1332
1333         next = rb_first(&hist);
1334         while (next) {
1335                 n = rb_entry(next, struct hist_entry, rb_node);
1336                 next = rb_next(&n->rb_node);
1337
1338                 rb_erase(&n->rb_node, &hist);
1339                 collapse__insert_entry(n);
1340         }
1341 }
1342
1343 /*
1344  * reverse the map, sort on count.
1345  */
1346
1347 static struct rb_root output_hists;
1348
1349 static void output__insert_entry(struct hist_entry *he, u64 min_callchain_hits)
1350 {
1351         struct rb_node **p = &output_hists.rb_node;
1352         struct rb_node *parent = NULL;
1353         struct hist_entry *iter;
1354
1355         if (callchain)
1356                 callchain_param.sort(&he->sorted_chain, &he->callchain,
1357                                       min_callchain_hits, &callchain_param);
1358
1359         while (*p != NULL) {
1360                 parent = *p;
1361                 iter = rb_entry(parent, struct hist_entry, rb_node);
1362
1363                 if (he->count > iter->count)
1364                         p = &(*p)->rb_left;
1365                 else
1366                         p = &(*p)->rb_right;
1367         }
1368
1369         rb_link_node(&he->rb_node, parent, p);
1370         rb_insert_color(&he->rb_node, &output_hists);
1371 }
1372
1373 static void output__resort(u64 total_samples)
1374 {
1375         struct rb_node *next;
1376         struct hist_entry *n;
1377         struct rb_root *tree = &hist;
1378         u64 min_callchain_hits;
1379
1380         min_callchain_hits = total_samples * (callchain_param.min_percent / 100);
1381
1382         if (sort__need_collapse)
1383                 tree = &collapse_hists;
1384
1385         next = rb_first(tree);
1386
1387         while (next) {
1388                 n = rb_entry(next, struct hist_entry, rb_node);
1389                 next = rb_next(&n->rb_node);
1390
1391                 rb_erase(&n->rb_node, tree);
1392                 output__insert_entry(n, min_callchain_hits);
1393         }
1394 }
1395
1396 static size_t output__fprintf(FILE *fp, u64 total_samples)
1397 {
1398         struct hist_entry *pos;
1399         struct sort_entry *se;
1400         struct rb_node *nd;
1401         size_t ret = 0;
1402         unsigned int width;
1403         char *col_width = col_width_list_str;
1404
1405         init_rem_hits();
1406
1407         fprintf(fp, "# Samples: %Ld\n", (u64)total_samples);
1408         fprintf(fp, "#\n");
1409
1410         fprintf(fp, "# Overhead");
1411         if (show_nr_samples) {
1412                 if (field_sep)
1413                         fprintf(fp, "%cSamples", *field_sep);
1414                 else
1415                         fputs("  Samples  ", fp);
1416         }
1417         list_for_each_entry(se, &hist_entry__sort_list, list) {
1418                 if (se->elide)
1419                         continue;
1420                 if (field_sep) {
1421                         fprintf(fp, "%c%s", *field_sep, se->header);
1422                         continue;
1423                 }
1424                 width = strlen(se->header);
1425                 if (se->width) {
1426                         if (col_width_list_str) {
1427                                 if (col_width) {
1428                                         *se->width = atoi(col_width);
1429                                         col_width = strchr(col_width, ',');
1430                                         if (col_width)
1431                                                 ++col_width;
1432                                 }
1433                         }
1434                         width = *se->width = max(*se->width, width);
1435                 }
1436                 fprintf(fp, "  %*s", width, se->header);
1437         }
1438         fprintf(fp, "\n");
1439
1440         if (field_sep)
1441                 goto print_entries;
1442
1443         fprintf(fp, "# ........");
1444         if (show_nr_samples)
1445                 fprintf(fp, " ..........");
1446         list_for_each_entry(se, &hist_entry__sort_list, list) {
1447                 unsigned int i;
1448
1449                 if (se->elide)
1450                         continue;
1451
1452                 fprintf(fp, "  ");
1453                 if (se->width)
1454                         width = *se->width;
1455                 else
1456                         width = strlen(se->header);
1457                 for (i = 0; i < width; i++)
1458                         fprintf(fp, ".");
1459         }
1460         fprintf(fp, "\n");
1461
1462         fprintf(fp, "#\n");
1463
1464 print_entries:
1465         for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
1466                 pos = rb_entry(nd, struct hist_entry, rb_node);
1467                 ret += hist_entry__fprintf(fp, pos, total_samples);
1468         }
1469
1470         if (sort_order == default_sort_order &&
1471                         parent_pattern == default_parent_pattern) {
1472                 fprintf(fp, "#\n");
1473                 fprintf(fp, "# (For a higher level overview, try: perf report --sort comm,dso)\n");
1474                 fprintf(fp, "#\n");
1475         }
1476         fprintf(fp, "\n");
1477
1478         free(rem_sq_bracket);
1479
1480         if (show_threads)
1481                 perf_read_values_display(fp, &show_threads_values);
1482
1483         return ret;
1484 }
1485
1486 static void register_idle_thread(void)
1487 {
1488         struct thread *thread = threads__findnew(0);
1489
1490         if (thread == NULL ||
1491                         thread__set_comm(thread, "[idle]")) {
1492                 fprintf(stderr, "problem inserting idle task.\n");
1493                 exit(-1);
1494         }
1495 }
1496
1497 static unsigned long total = 0,
1498                      total_mmap = 0,
1499                      total_comm = 0,
1500                      total_fork = 0,
1501                      total_unknown = 0,
1502                      total_lost = 0;
1503
1504 static int validate_chain(struct ip_callchain *chain, event_t *event)
1505 {
1506         unsigned int chain_size;
1507
1508         chain_size = event->header.size;
1509         chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
1510
1511         if (chain->nr*sizeof(u64) > chain_size)
1512                 return -1;
1513
1514         return 0;
1515 }
1516
1517 static int
1518 process_sample_event(event_t *event, unsigned long offset, unsigned long head)
1519 {
1520         char level;
1521         int show = 0;
1522         struct dso *dso = NULL;
1523         struct thread *thread = threads__findnew(event->ip.pid);
1524         u64 ip = event->ip.ip;
1525         u64 period = 1;
1526         struct map *map = NULL;
1527         void *more_data = event->ip.__more_data;
1528         struct ip_callchain *chain = NULL;
1529         int cpumode;
1530
1531         if (sample_type & PERF_SAMPLE_PERIOD) {
1532                 period = *(u64 *)more_data;
1533                 more_data += sizeof(u64);
1534         }
1535
1536         dprintf("%p [%p]: PERF_EVENT_SAMPLE (IP, %d): %d: %p period: %Ld\n",
1537                 (void *)(offset + head),
1538                 (void *)(long)(event->header.size),
1539                 event->header.misc,
1540                 event->ip.pid,
1541                 (void *)(long)ip,
1542                 (long long)period);
1543
1544         if (sample_type & PERF_SAMPLE_CALLCHAIN) {
1545                 unsigned int i;
1546
1547                 chain = (void *)more_data;
1548
1549                 dprintf("... chain: nr:%Lu\n", chain->nr);
1550
1551                 if (validate_chain(chain, event) < 0) {
1552                         eprintf("call-chain problem with event, skipping it.\n");
1553                         return 0;
1554                 }
1555
1556                 if (dump_trace) {
1557                         for (i = 0; i < chain->nr; i++)
1558                                 dprintf("..... %2d: %016Lx\n", i, chain->ips[i]);
1559                 }
1560         }
1561
1562         dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1563
1564         if (thread == NULL) {
1565                 eprintf("problem processing %d event, skipping it.\n",
1566                         event->header.type);
1567                 return -1;
1568         }
1569
1570         if (comm_list && !strlist__has_entry(comm_list, thread->comm))
1571                 return 0;
1572
1573         cpumode = event->header.misc & PERF_EVENT_MISC_CPUMODE_MASK;
1574
1575         if (cpumode == PERF_EVENT_MISC_KERNEL) {
1576                 show = SHOW_KERNEL;
1577                 level = 'k';
1578
1579                 dso = kernel_dso;
1580
1581                 dprintf(" ...... dso: %s\n", dso->name);
1582
1583         } else if (cpumode == PERF_EVENT_MISC_USER) {
1584
1585                 show = SHOW_USER;
1586                 level = '.';
1587
1588         } else {
1589                 show = SHOW_HV;
1590                 level = 'H';
1591
1592                 dso = hypervisor_dso;
1593
1594                 dprintf(" ...... dso: [hypervisor]\n");
1595         }
1596
1597         if (show & show_mask) {
1598                 struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
1599
1600                 if (dso_list && dso && dso->name && !strlist__has_entry(dso_list, dso->name))
1601                         return 0;
1602
1603                 if (sym_list && sym && !strlist__has_entry(sym_list, sym->name))
1604                         return 0;
1605
1606                 if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
1607                         eprintf("problem incrementing symbol count, skipping event\n");
1608                         return -1;
1609                 }
1610         }
1611         total += period;
1612
1613         return 0;
1614 }
1615
1616 static int
1617 process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
1618 {
1619         struct thread *thread = threads__findnew(event->mmap.pid);
1620         struct map *map = map__new(&event->mmap);
1621
1622         dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
1623                 (void *)(offset + head),
1624                 (void *)(long)(event->header.size),
1625                 event->mmap.pid,
1626                 (void *)(long)event->mmap.start,
1627                 (void *)(long)event->mmap.len,
1628                 (void *)(long)event->mmap.pgoff,
1629                 event->mmap.filename);
1630
1631         if (thread == NULL || map == NULL) {
1632                 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
1633                 return 0;
1634         }
1635
1636         thread__insert_map(thread, map);
1637         total_mmap++;
1638
1639         return 0;
1640 }
1641
1642 static int
1643 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
1644 {
1645         struct thread *thread = threads__findnew(event->comm.pid);
1646
1647         dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
1648                 (void *)(offset + head),
1649                 (void *)(long)(event->header.size),
1650                 event->comm.comm, event->comm.pid);
1651
1652         if (thread == NULL ||
1653             thread__set_comm(thread, event->comm.comm)) {
1654                 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
1655                 return -1;
1656         }
1657         total_comm++;
1658
1659         return 0;
1660 }
1661
1662 static int
1663 process_task_event(event_t *event, unsigned long offset, unsigned long head)
1664 {
1665         struct thread *thread = threads__findnew(event->fork.pid);
1666         struct thread *parent = threads__findnew(event->fork.ppid);
1667
1668         dprintf("%p [%p]: PERF_EVENT_%s: (%d:%d):(%d:%d)\n",
1669                 (void *)(offset + head),
1670                 (void *)(long)(event->header.size),
1671                 event->header.type == PERF_EVENT_FORK ? "FORK" : "EXIT",
1672                 event->fork.pid, event->fork.tid,
1673                 event->fork.ppid, event->fork.ptid);
1674
1675         /*
1676          * A thread clone will have the same PID for both
1677          * parent and child.
1678          */
1679         if (thread == parent)
1680                 return 0;
1681
1682         if (event->header.type == PERF_EVENT_EXIT)
1683                 return 0;
1684
1685         if (!thread || !parent || thread__fork(thread, parent)) {
1686                 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
1687                 return -1;
1688         }
1689         total_fork++;
1690
1691         return 0;
1692 }
1693
1694 static int
1695 process_lost_event(event_t *event, unsigned long offset, unsigned long head)
1696 {
1697         dprintf("%p [%p]: PERF_EVENT_LOST: id:%Ld: lost:%Ld\n",
1698                 (void *)(offset + head),
1699                 (void *)(long)(event->header.size),
1700                 event->lost.id,
1701                 event->lost.lost);
1702
1703         total_lost += event->lost.lost;
1704
1705         return 0;
1706 }
1707
1708 static void trace_event(event_t *event)
1709 {
1710         unsigned char *raw_event = (void *)event;
1711         char *color = PERF_COLOR_BLUE;
1712         int i, j;
1713
1714         if (!dump_trace)
1715                 return;
1716
1717         dprintf(".");
1718         cdprintf("\n. ... raw event: size %d bytes\n", event->header.size);
1719
1720         for (i = 0; i < event->header.size; i++) {
1721                 if ((i & 15) == 0) {
1722                         dprintf(".");
1723                         cdprintf("  %04x: ", i);
1724                 }
1725
1726                 cdprintf(" %02x", raw_event[i]);
1727
1728                 if (((i & 15) == 15) || i == event->header.size-1) {
1729                         cdprintf("  ");
1730                         for (j = 0; j < 15-(i & 15); j++)
1731                                 cdprintf("   ");
1732                         for (j = 0; j < (i & 15); j++) {
1733                                 if (isprint(raw_event[i-15+j]))
1734                                         cdprintf("%c", raw_event[i-15+j]);
1735                                 else
1736                                         cdprintf(".");
1737                         }
1738                         cdprintf("\n");
1739                 }
1740         }
1741         dprintf(".\n");
1742 }
1743
1744 static struct perf_header       *header;
1745
1746 static struct perf_counter_attr *perf_header__find_attr(u64 id)
1747 {
1748         int i;
1749
1750         for (i = 0; i < header->attrs; i++) {
1751                 struct perf_header_attr *attr = header->attr[i];
1752                 int j;
1753
1754                 for (j = 0; j < attr->ids; j++) {
1755                         if (attr->id[j] == id)
1756                                 return &attr->attr;
1757                 }
1758         }
1759
1760         return NULL;
1761 }
1762
1763 static int
1764 process_read_event(event_t *event, unsigned long offset, unsigned long head)
1765 {
1766         struct perf_counter_attr *attr = perf_header__find_attr(event->read.id);
1767
1768         if (show_threads) {
1769                 char *name = attr ? __event_name(attr->type, attr->config)
1770                                    : "unknown";
1771                 perf_read_values_add_value(&show_threads_values,
1772                                            event->read.pid, event->read.tid,
1773                                            event->read.id,
1774                                            name,
1775                                            event->read.value);
1776         }
1777
1778         dprintf("%p [%p]: PERF_EVENT_READ: %d %d %s %Lu\n",
1779                         (void *)(offset + head),
1780                         (void *)(long)(event->header.size),
1781                         event->read.pid,
1782                         event->read.tid,
1783                         attr ? __event_name(attr->type, attr->config)
1784                              : "FAIL",
1785                         event->read.value);
1786
1787         return 0;
1788 }
1789
1790 static int
1791 process_event(event_t *event, unsigned long offset, unsigned long head)
1792 {
1793         trace_event(event);
1794
1795         switch (event->header.type) {
1796         case PERF_EVENT_SAMPLE:
1797                 return process_sample_event(event, offset, head);
1798
1799         case PERF_EVENT_MMAP:
1800                 return process_mmap_event(event, offset, head);
1801
1802         case PERF_EVENT_COMM:
1803                 return process_comm_event(event, offset, head);
1804
1805         case PERF_EVENT_FORK:
1806         case PERF_EVENT_EXIT:
1807                 return process_task_event(event, offset, head);
1808
1809         case PERF_EVENT_LOST:
1810                 return process_lost_event(event, offset, head);
1811
1812         case PERF_EVENT_READ:
1813                 return process_read_event(event, offset, head);
1814
1815         /*
1816          * We dont process them right now but they are fine:
1817          */
1818
1819         case PERF_EVENT_THROTTLE:
1820         case PERF_EVENT_UNTHROTTLE:
1821                 return 0;
1822
1823         default:
1824                 return -1;
1825         }
1826
1827         return 0;
1828 }
1829
1830 static u64 perf_header__sample_type(void)
1831 {
1832         u64 sample_type = 0;
1833         int i;
1834
1835         for (i = 0; i < header->attrs; i++) {
1836                 struct perf_header_attr *attr = header->attr[i];
1837
1838                 if (!sample_type)
1839                         sample_type = attr->attr.sample_type;
1840                 else if (sample_type != attr->attr.sample_type)
1841                         die("non matching sample_type");
1842         }
1843
1844         return sample_type;
1845 }
1846
1847 static int __cmd_report(void)
1848 {
1849         int ret, rc = EXIT_FAILURE;
1850         unsigned long offset = 0;
1851         unsigned long head, shift;
1852         struct stat stat;
1853         event_t *event;
1854         uint32_t size;
1855         char *buf;
1856
1857         register_idle_thread();
1858
1859         if (show_threads)
1860                 perf_read_values_init(&show_threads_values);
1861
1862         input = open(input_name, O_RDONLY);
1863         if (input < 0) {
1864                 fprintf(stderr, " failed to open file: %s", input_name);
1865                 if (!strcmp(input_name, "perf.data"))
1866                         fprintf(stderr, "  (try 'perf record' first)");
1867                 fprintf(stderr, "\n");
1868                 exit(-1);
1869         }
1870
1871         ret = fstat(input, &stat);
1872         if (ret < 0) {
1873                 perror("failed to stat file");
1874                 exit(-1);
1875         }
1876
1877         if (!stat.st_size) {
1878                 fprintf(stderr, "zero-sized file, nothing to do!\n");
1879                 exit(0);
1880         }
1881
1882         header = perf_header__read(input);
1883         head = header->data_offset;
1884
1885         sample_type = perf_header__sample_type();
1886
1887         if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
1888                 if (sort__has_parent) {
1889                         fprintf(stderr, "selected --sort parent, but no"
1890                                         " callchain data. Did you call"
1891                                         " perf record without -g?\n");
1892                         exit(-1);
1893                 }
1894                 if (callchain) {
1895                         fprintf(stderr, "selected -c but no callchain data."
1896                                         " Did you call perf record without"
1897                                         " -g?\n");
1898                         exit(-1);
1899                 }
1900         } else if (callchain_param.mode != CHAIN_NONE && !callchain) {
1901                         callchain = 1;
1902                         if (register_callchain_param(&callchain_param) < 0) {
1903                                 fprintf(stderr, "Can't register callchain"
1904                                                 " params\n");
1905                                 exit(-1);
1906                         }
1907         }
1908
1909         if (load_kernel() < 0) {
1910                 perror("failed to load kernel symbols");
1911                 return EXIT_FAILURE;
1912         }
1913
1914         if (!full_paths) {
1915                 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
1916                         perror("failed to get the current directory");
1917                         return EXIT_FAILURE;
1918                 }
1919                 cwdlen = strlen(cwd);
1920         } else {
1921                 cwd = NULL;
1922                 cwdlen = 0;
1923         }
1924
1925         shift = page_size * (head / page_size);
1926         offset += shift;
1927         head -= shift;
1928
1929 remap:
1930         buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1931                            MAP_SHARED, input, offset);
1932         if (buf == MAP_FAILED) {
1933                 perror("failed to mmap file");
1934                 exit(-1);
1935         }
1936
1937 more:
1938         event = (event_t *)(buf + head);
1939
1940         size = event->header.size;
1941         if (!size)
1942                 size = 8;
1943
1944         if (head + event->header.size >= page_size * mmap_window) {
1945                 int ret;
1946
1947                 shift = page_size * (head / page_size);
1948
1949                 ret = munmap(buf, page_size * mmap_window);
1950                 assert(ret == 0);
1951
1952                 offset += shift;
1953                 head -= shift;
1954                 goto remap;
1955         }
1956
1957         size = event->header.size;
1958
1959         dprintf("\n%p [%p]: event: %d\n",
1960                         (void *)(offset + head),
1961                         (void *)(long)event->header.size,
1962                         event->header.type);
1963
1964         if (!size || process_event(event, offset, head) < 0) {
1965
1966                 dprintf("%p [%p]: skipping unknown header type: %d\n",
1967                         (void *)(offset + head),
1968                         (void *)(long)(event->header.size),
1969                         event->header.type);
1970
1971                 total_unknown++;
1972
1973                 /*
1974                  * assume we lost track of the stream, check alignment, and
1975                  * increment a single u64 in the hope to catch on again 'soon'.
1976                  */
1977
1978                 if (unlikely(head & 7))
1979                         head &= ~7ULL;
1980
1981                 size = 8;
1982         }
1983
1984         head += size;
1985
1986         if (offset + head >= header->data_offset + header->data_size)
1987                 goto done;
1988
1989         if (offset + head < (unsigned long)stat.st_size)
1990                 goto more;
1991
1992 done:
1993         rc = EXIT_SUCCESS;
1994         close(input);
1995
1996         dprintf("      IP events: %10ld\n", total);
1997         dprintf("    mmap events: %10ld\n", total_mmap);
1998         dprintf("    comm events: %10ld\n", total_comm);
1999         dprintf("    fork events: %10ld\n", total_fork);
2000         dprintf("    lost events: %10ld\n", total_lost);
2001         dprintf(" unknown events: %10ld\n", total_unknown);
2002
2003         if (dump_trace)
2004                 return 0;
2005
2006         if (verbose >= 3)
2007                 threads__fprintf(stdout);
2008
2009         if (verbose >= 2)
2010                 dsos__fprintf(stdout);
2011
2012         collapse__resort();
2013         output__resort(total);
2014         output__fprintf(stdout, total);
2015
2016         if (show_threads)
2017                 perf_read_values_destroy(&show_threads_values);
2018
2019         return rc;
2020 }
2021
2022 static int
2023 parse_callchain_opt(const struct option *opt __used, const char *arg,
2024                     int unset __used)
2025 {
2026         char *tok;
2027         char *endptr;
2028
2029         callchain = 1;
2030
2031         if (!arg)
2032                 return 0;
2033
2034         tok = strtok((char *)arg, ",");
2035         if (!tok)
2036                 return -1;
2037
2038         /* get the output mode */
2039         if (!strncmp(tok, "graph", strlen(arg)))
2040                 callchain_param.mode = CHAIN_GRAPH_ABS;
2041
2042         else if (!strncmp(tok, "flat", strlen(arg)))
2043                 callchain_param.mode = CHAIN_FLAT;
2044
2045         else if (!strncmp(tok, "fractal", strlen(arg)))
2046                 callchain_param.mode = CHAIN_GRAPH_REL;
2047
2048         else if (!strncmp(tok, "none", strlen(arg))) {
2049                 callchain_param.mode = CHAIN_NONE;
2050                 callchain = 0;
2051
2052                 return 0;
2053         }
2054
2055         else
2056                 return -1;
2057
2058         /* get the min percentage */
2059         tok = strtok(NULL, ",");
2060         if (!tok)
2061                 goto setup;
2062
2063         callchain_param.min_percent = strtod(tok, &endptr);
2064         if (tok == endptr)
2065                 return -1;
2066
2067 setup:
2068         if (register_callchain_param(&callchain_param) < 0) {
2069                 fprintf(stderr, "Can't register callchain params\n");
2070                 return -1;
2071         }
2072         return 0;
2073 }
2074
2075 static const char * const report_usage[] = {
2076         "perf report [<options>] <command>",
2077         NULL
2078 };
2079
2080 static const struct option options[] = {
2081         OPT_STRING('i', "input", &input_name, "file",
2082                     "input file name"),
2083         OPT_BOOLEAN('v', "verbose", &verbose,
2084                     "be more verbose (show symbol address, etc)"),
2085         OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
2086                     "dump raw trace in ASCII"),
2087         OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
2088         OPT_BOOLEAN('m', "modules", &modules,
2089                     "load module symbols - WARNING: use only with -k and LIVE kernel"),
2090         OPT_BOOLEAN('n', "show-nr-samples", &show_nr_samples,
2091                     "Show a column with the number of samples"),
2092         OPT_BOOLEAN('T', "threads", &show_threads,
2093                     "Show per-thread event counters"),
2094         OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
2095                    "sort by key(s): pid, comm, dso, symbol, parent"),
2096         OPT_BOOLEAN('P', "full-paths", &full_paths,
2097                     "Don't shorten the pathnames taking into account the cwd"),
2098         OPT_STRING('p', "parent", &parent_pattern, "regex",
2099                    "regex filter to identify parent, see: '--sort parent'"),
2100         OPT_BOOLEAN('x', "exclude-other", &exclude_other,
2101                     "Only display entries with parent-match"),
2102         OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
2103                      "Display callchains using output_type and min percent threshold. "
2104                      "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
2105         OPT_STRING('d', "dsos", &dso_list_str, "dso[,dso...]",
2106                    "only consider symbols in these dsos"),
2107         OPT_STRING('C', "comms", &comm_list_str, "comm[,comm...]",
2108                    "only consider symbols in these comms"),
2109         OPT_STRING('S', "symbols", &sym_list_str, "symbol[,symbol...]",
2110                    "only consider these symbols"),
2111         OPT_STRING('w', "column-widths", &col_width_list_str,
2112                    "width[,width...]",
2113                    "don't try to adjust column width, use these fixed values"),
2114         OPT_STRING('t', "field-separator", &field_sep, "separator",
2115                    "separator for columns, no spaces will be added between "
2116                    "columns '.' is reserved."),
2117         OPT_END()
2118 };
2119
2120 static void setup_sorting(void)
2121 {
2122         char *tmp, *tok, *str = strdup(sort_order);
2123
2124         for (tok = strtok_r(str, ", ", &tmp);
2125                         tok; tok = strtok_r(NULL, ", ", &tmp)) {
2126                 if (sort_dimension__add(tok) < 0) {
2127                         error("Unknown --sort key: `%s'", tok);
2128                         usage_with_options(report_usage, options);
2129                 }
2130         }
2131
2132         free(str);
2133 }
2134
2135 static void setup_list(struct strlist **list, const char *list_str,
2136                        struct sort_entry *se, const char *list_name,
2137                        FILE *fp)
2138 {
2139         if (list_str) {
2140                 *list = strlist__new(true, list_str);
2141                 if (!*list) {
2142                         fprintf(stderr, "problems parsing %s list\n",
2143                                 list_name);
2144                         exit(129);
2145                 }
2146                 if (strlist__nr_entries(*list) == 1) {
2147                         fprintf(fp, "# %s: %s\n", list_name,
2148                                 strlist__entry(*list, 0)->s);
2149                         se->elide = true;
2150                 }
2151         }
2152 }
2153
2154 int cmd_report(int argc, const char **argv, const char *prefix __used)
2155 {
2156         symbol__init();
2157
2158         page_size = getpagesize();
2159
2160         argc = parse_options(argc, argv, options, report_usage, 0);
2161
2162         setup_sorting();
2163
2164         if (parent_pattern != default_parent_pattern) {
2165                 sort_dimension__add("parent");
2166                 sort_parent.elide = 1;
2167         } else
2168                 exclude_other = 0;
2169
2170         /*
2171          * Any (unrecognized) arguments left?
2172          */
2173         if (argc)
2174                 usage_with_options(report_usage, options);
2175
2176         setup_pager();
2177
2178         setup_list(&dso_list, dso_list_str, &sort_dso, "dso", stdout);
2179         setup_list(&comm_list, comm_list_str, &sort_comm, "comm", stdout);
2180         setup_list(&sym_list, sym_list_str, &sort_sym, "symbol", stdout);
2181
2182         if (field_sep && *field_sep == '.') {
2183                 fputs("'.' is the only non valid --field-separator argument\n",
2184                       stderr);
2185                 exit(129);
2186         }
2187
2188         return __cmd_report();
2189 }