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