perf report: Filter to parent set by default
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / tools / perf / builtin-report.c
blob86981bd08f6510f6851bdc0bdbed19c63e7b97ce
1 /*
2 * builtin-report.c
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"
10 #include "util/util.h"
12 #include "util/color.h"
13 #include "util/list.h"
14 #include "util/cache.h"
15 #include "util/rbtree.h"
16 #include "util/symbol.h"
17 #include "util/string.h"
19 #include "perf.h"
21 #include "util/parse-options.h"
22 #include "util/parse-events.h"
24 #define SHOW_KERNEL 1
25 #define SHOW_USER 2
26 #define SHOW_HV 4
28 static char const *input_name = "perf.data";
29 static char *vmlinux = NULL;
31 static char default_sort_order[] = "comm,dso";
32 static char *sort_order = default_sort_order;
34 static int input;
35 static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
37 static int dump_trace = 0;
38 #define dprintf(x...) do { if (dump_trace) printf(x); } while (0)
39 #define cdprintf(x...) do { if (dump_trace) color_fprintf(stdout, color, x); } while (0)
41 static int verbose;
42 #define eprintf(x...) do { if (verbose) fprintf(stderr, x); } while (0)
44 static int full_paths;
46 static unsigned long page_size;
47 static unsigned long mmap_window = 32;
49 static char default_parent_pattern[] = "^sys_|^do_page_fault";
50 static char *parent_pattern = default_parent_pattern;
51 static regex_t parent_regex;
53 static int exclude_other = 1;
55 struct ip_event {
56 struct perf_event_header header;
57 __u64 ip;
58 __u32 pid, tid;
59 unsigned char __more_data[];
62 struct mmap_event {
63 struct perf_event_header header;
64 __u32 pid, tid;
65 __u64 start;
66 __u64 len;
67 __u64 pgoff;
68 char filename[PATH_MAX];
71 struct comm_event {
72 struct perf_event_header header;
73 __u32 pid, tid;
74 char comm[16];
77 struct fork_event {
78 struct perf_event_header header;
79 __u32 pid, ppid;
82 struct period_event {
83 struct perf_event_header header;
84 __u64 time;
85 __u64 id;
86 __u64 sample_period;
89 struct lost_event {
90 struct perf_event_header header;
91 __u64 id;
92 __u64 lost;
95 typedef union event_union {
96 struct perf_event_header header;
97 struct ip_event ip;
98 struct mmap_event mmap;
99 struct comm_event comm;
100 struct fork_event fork;
101 struct period_event period;
102 struct lost_event lost;
103 } event_t;
105 static LIST_HEAD(dsos);
106 static struct dso *kernel_dso;
107 static struct dso *vdso;
109 static void dsos__add(struct dso *dso)
111 list_add_tail(&dso->node, &dsos);
114 static struct dso *dsos__find(const char *name)
116 struct dso *pos;
118 list_for_each_entry(pos, &dsos, node)
119 if (strcmp(pos->name, name) == 0)
120 return pos;
121 return NULL;
124 static struct dso *dsos__findnew(const char *name)
126 struct dso *dso = dsos__find(name);
127 int nr;
129 if (dso)
130 return dso;
132 dso = dso__new(name, 0);
133 if (!dso)
134 goto out_delete_dso;
136 nr = dso__load(dso, NULL, verbose);
137 if (nr < 0) {
138 eprintf("Failed to open: %s\n", name);
139 goto out_delete_dso;
141 if (!nr)
142 eprintf("No symbols found in: %s, maybe install a debug package?\n", name);
144 dsos__add(dso);
146 return dso;
148 out_delete_dso:
149 dso__delete(dso);
150 return NULL;
153 static void dsos__fprintf(FILE *fp)
155 struct dso *pos;
157 list_for_each_entry(pos, &dsos, node)
158 dso__fprintf(pos, fp);
161 static struct symbol *vdso__find_symbol(struct dso *dso, __u64 ip)
163 return dso__find_symbol(kernel_dso, ip);
166 static int load_kernel(void)
168 int err;
170 kernel_dso = dso__new("[kernel]", 0);
171 if (!kernel_dso)
172 return -1;
174 err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose);
175 if (err) {
176 dso__delete(kernel_dso);
177 kernel_dso = NULL;
178 } else
179 dsos__add(kernel_dso);
181 vdso = dso__new("[vdso]", 0);
182 if (!vdso)
183 return -1;
185 vdso->find_symbol = vdso__find_symbol;
187 dsos__add(vdso);
189 return err;
192 static char __cwd[PATH_MAX];
193 static char *cwd = __cwd;
194 static int cwdlen;
196 static int strcommon(const char *pathname)
198 int n = 0;
200 while (pathname[n] == cwd[n] && n < cwdlen)
201 ++n;
203 return n;
206 struct map {
207 struct list_head node;
208 __u64 start;
209 __u64 end;
210 __u64 pgoff;
211 __u64 (*map_ip)(struct map *, __u64);
212 struct dso *dso;
215 static __u64 map__map_ip(struct map *map, __u64 ip)
217 return ip - map->start + map->pgoff;
220 static __u64 vdso__map_ip(struct map *map, __u64 ip)
222 return ip;
225 static inline int is_anon_memory(const char *filename)
227 return strcmp(filename, "//anon") == 0;
230 static struct map *map__new(struct mmap_event *event)
232 struct map *self = malloc(sizeof(*self));
234 if (self != NULL) {
235 const char *filename = event->filename;
236 char newfilename[PATH_MAX];
237 int anon;
239 if (cwd) {
240 int n = strcommon(filename);
242 if (n == cwdlen) {
243 snprintf(newfilename, sizeof(newfilename),
244 ".%s", filename + n);
245 filename = newfilename;
249 anon = is_anon_memory(filename);
251 if (anon) {
252 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
253 filename = newfilename;
256 self->start = event->start;
257 self->end = event->start + event->len;
258 self->pgoff = event->pgoff;
260 self->dso = dsos__findnew(filename);
261 if (self->dso == NULL)
262 goto out_delete;
264 if (self->dso == vdso || anon)
265 self->map_ip = vdso__map_ip;
266 else
267 self->map_ip = map__map_ip;
269 return self;
270 out_delete:
271 free(self);
272 return NULL;
275 static struct map *map__clone(struct map *self)
277 struct map *map = malloc(sizeof(*self));
279 if (!map)
280 return NULL;
282 memcpy(map, self, sizeof(*self));
284 return map;
287 static int map__overlap(struct map *l, struct map *r)
289 if (l->start > r->start) {
290 struct map *t = l;
291 l = r;
292 r = t;
295 if (l->end > r->start)
296 return 1;
298 return 0;
301 static size_t map__fprintf(struct map *self, FILE *fp)
303 return fprintf(fp, " %Lx-%Lx %Lx %s\n",
304 self->start, self->end, self->pgoff, self->dso->name);
308 struct thread {
309 struct rb_node rb_node;
310 struct list_head maps;
311 pid_t pid;
312 char *comm;
315 static struct thread *thread__new(pid_t pid)
317 struct thread *self = malloc(sizeof(*self));
319 if (self != NULL) {
320 self->pid = pid;
321 self->comm = malloc(32);
322 if (self->comm)
323 snprintf(self->comm, 32, ":%d", self->pid);
324 INIT_LIST_HEAD(&self->maps);
327 return self;
330 static int thread__set_comm(struct thread *self, const char *comm)
332 if (self->comm)
333 free(self->comm);
334 self->comm = strdup(comm);
335 return self->comm ? 0 : -ENOMEM;
338 static size_t thread__fprintf(struct thread *self, FILE *fp)
340 struct map *pos;
341 size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
343 list_for_each_entry(pos, &self->maps, node)
344 ret += map__fprintf(pos, fp);
346 return ret;
350 static struct rb_root threads;
351 static struct thread *last_match;
353 static struct thread *threads__findnew(pid_t pid)
355 struct rb_node **p = &threads.rb_node;
356 struct rb_node *parent = NULL;
357 struct thread *th;
360 * Font-end cache - PID lookups come in blocks,
361 * so most of the time we dont have to look up
362 * the full rbtree:
364 if (last_match && last_match->pid == pid)
365 return last_match;
367 while (*p != NULL) {
368 parent = *p;
369 th = rb_entry(parent, struct thread, rb_node);
371 if (th->pid == pid) {
372 last_match = th;
373 return th;
376 if (pid < th->pid)
377 p = &(*p)->rb_left;
378 else
379 p = &(*p)->rb_right;
382 th = thread__new(pid);
383 if (th != NULL) {
384 rb_link_node(&th->rb_node, parent, p);
385 rb_insert_color(&th->rb_node, &threads);
386 last_match = th;
389 return th;
392 static void thread__insert_map(struct thread *self, struct map *map)
394 struct map *pos, *tmp;
396 list_for_each_entry_safe(pos, tmp, &self->maps, node) {
397 if (map__overlap(pos, map)) {
398 list_del_init(&pos->node);
399 /* XXX leaks dsos */
400 free(pos);
404 list_add_tail(&map->node, &self->maps);
407 static int thread__fork(struct thread *self, struct thread *parent)
409 struct map *map;
411 if (self->comm)
412 free(self->comm);
413 self->comm = strdup(parent->comm);
414 if (!self->comm)
415 return -ENOMEM;
417 list_for_each_entry(map, &parent->maps, node) {
418 struct map *new = map__clone(map);
419 if (!new)
420 return -ENOMEM;
421 thread__insert_map(self, new);
424 return 0;
427 static struct map *thread__find_map(struct thread *self, __u64 ip)
429 struct map *pos;
431 if (self == NULL)
432 return NULL;
434 list_for_each_entry(pos, &self->maps, node)
435 if (ip >= pos->start && ip <= pos->end)
436 return pos;
438 return NULL;
441 static size_t threads__fprintf(FILE *fp)
443 size_t ret = 0;
444 struct rb_node *nd;
446 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
447 struct thread *pos = rb_entry(nd, struct thread, rb_node);
449 ret += thread__fprintf(pos, fp);
452 return ret;
456 * histogram, sorted on item, collects counts
459 static struct rb_root hist;
461 struct hist_entry {
462 struct rb_node rb_node;
464 struct thread *thread;
465 struct map *map;
466 struct dso *dso;
467 struct symbol *sym;
468 struct symbol *parent;
469 __u64 ip;
470 char level;
472 __u64 count;
476 * configurable sorting bits
479 struct sort_entry {
480 struct list_head list;
482 char *header;
484 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
485 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
486 size_t (*print)(FILE *fp, struct hist_entry *);
489 static int64_t cmp_null(void *l, void *r)
491 if (!l && !r)
492 return 0;
493 else if (!l)
494 return -1;
495 else
496 return 1;
499 /* --sort pid */
501 static int64_t
502 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
504 return right->thread->pid - left->thread->pid;
507 static size_t
508 sort__thread_print(FILE *fp, struct hist_entry *self)
510 return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
513 static struct sort_entry sort_thread = {
514 .header = " Command: Pid",
515 .cmp = sort__thread_cmp,
516 .print = sort__thread_print,
519 /* --sort comm */
521 static int64_t
522 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
524 return right->thread->pid - left->thread->pid;
527 static int64_t
528 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
530 char *comm_l = left->thread->comm;
531 char *comm_r = right->thread->comm;
533 if (!comm_l || !comm_r)
534 return cmp_null(comm_l, comm_r);
536 return strcmp(comm_l, comm_r);
539 static size_t
540 sort__comm_print(FILE *fp, struct hist_entry *self)
542 return fprintf(fp, "%16s", self->thread->comm);
545 static struct sort_entry sort_comm = {
546 .header = " Command",
547 .cmp = sort__comm_cmp,
548 .collapse = sort__comm_collapse,
549 .print = sort__comm_print,
552 /* --sort dso */
554 static int64_t
555 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
557 struct dso *dso_l = left->dso;
558 struct dso *dso_r = right->dso;
560 if (!dso_l || !dso_r)
561 return cmp_null(dso_l, dso_r);
563 return strcmp(dso_l->name, dso_r->name);
566 static size_t
567 sort__dso_print(FILE *fp, struct hist_entry *self)
569 if (self->dso)
570 return fprintf(fp, "%-25s", self->dso->name);
572 return fprintf(fp, "%016llx ", (__u64)self->ip);
575 static struct sort_entry sort_dso = {
576 .header = "Shared Object ",
577 .cmp = sort__dso_cmp,
578 .print = sort__dso_print,
581 /* --sort symbol */
583 static int64_t
584 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
586 __u64 ip_l, ip_r;
588 if (left->sym == right->sym)
589 return 0;
591 ip_l = left->sym ? left->sym->start : left->ip;
592 ip_r = right->sym ? right->sym->start : right->ip;
594 return (int64_t)(ip_r - ip_l);
597 static size_t
598 sort__sym_print(FILE *fp, struct hist_entry *self)
600 size_t ret = 0;
602 if (verbose)
603 ret += fprintf(fp, "%#018llx ", (__u64)self->ip);
605 if (self->sym) {
606 ret += fprintf(fp, "[%c] %s",
607 self->dso == kernel_dso ? 'k' : '.', self->sym->name);
608 } else {
609 ret += fprintf(fp, "%#016llx", (__u64)self->ip);
612 return ret;
615 static struct sort_entry sort_sym = {
616 .header = "Symbol",
617 .cmp = sort__sym_cmp,
618 .print = sort__sym_print,
621 /* --sort parent */
623 static int64_t
624 sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
626 struct symbol *sym_l = left->parent;
627 struct symbol *sym_r = right->parent;
629 if (!sym_l || !sym_r)
630 return cmp_null(sym_l, sym_r);
632 return strcmp(sym_l->name, sym_r->name);
635 static size_t
636 sort__parent_print(FILE *fp, struct hist_entry *self)
638 size_t ret = 0;
640 ret += fprintf(fp, "%-20s", self->parent ? self->parent->name : "[other]");
642 return ret;
645 static struct sort_entry sort_parent = {
646 .header = "Parent symbol ",
647 .cmp = sort__parent_cmp,
648 .print = sort__parent_print,
651 static int sort__need_collapse = 0;
652 static int sort__has_parent = 0;
654 struct sort_dimension {
655 char *name;
656 struct sort_entry *entry;
657 int taken;
660 static struct sort_dimension sort_dimensions[] = {
661 { .name = "pid", .entry = &sort_thread, },
662 { .name = "comm", .entry = &sort_comm, },
663 { .name = "dso", .entry = &sort_dso, },
664 { .name = "symbol", .entry = &sort_sym, },
665 { .name = "parent", .entry = &sort_parent, },
668 static LIST_HEAD(hist_entry__sort_list);
670 static int sort_dimension__add(char *tok)
672 int i;
674 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
675 struct sort_dimension *sd = &sort_dimensions[i];
677 if (sd->taken)
678 continue;
680 if (strncasecmp(tok, sd->name, strlen(tok)))
681 continue;
683 if (sd->entry->collapse)
684 sort__need_collapse = 1;
686 if (sd->entry == &sort_parent) {
687 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
688 if (ret) {
689 char err[BUFSIZ];
691 regerror(ret, &parent_regex, err, sizeof(err));
692 fprintf(stderr, "Invalid regex: %s\n%s",
693 parent_pattern, err);
694 exit(-1);
696 sort__has_parent = 1;
699 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
700 sd->taken = 1;
702 return 0;
705 return -ESRCH;
708 static int64_t
709 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
711 struct sort_entry *se;
712 int64_t cmp = 0;
714 list_for_each_entry(se, &hist_entry__sort_list, list) {
715 cmp = se->cmp(left, right);
716 if (cmp)
717 break;
720 return cmp;
723 static int64_t
724 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
726 struct sort_entry *se;
727 int64_t cmp = 0;
729 list_for_each_entry(se, &hist_entry__sort_list, list) {
730 int64_t (*f)(struct hist_entry *, struct hist_entry *);
732 f = se->collapse ?: se->cmp;
734 cmp = f(left, right);
735 if (cmp)
736 break;
739 return cmp;
742 static size_t
743 hist_entry__fprintf(FILE *fp, struct hist_entry *self, __u64 total_samples)
745 struct sort_entry *se;
746 size_t ret;
748 if (exclude_other && !self->parent)
749 return 0;
751 if (total_samples) {
752 double percent = self->count * 100.0 / total_samples;
753 char *color = PERF_COLOR_NORMAL;
756 * We color high-overhead entries in red, mid-overhead
757 * entries in green - and keep the low overhead places
758 * normal:
760 if (percent >= 5.0) {
761 color = PERF_COLOR_RED;
762 } else {
763 if (percent >= 0.5)
764 color = PERF_COLOR_GREEN;
767 ret = color_fprintf(fp, color, " %6.2f%%",
768 (self->count * 100.0) / total_samples);
769 } else
770 ret = fprintf(fp, "%12Ld ", self->count);
772 list_for_each_entry(se, &hist_entry__sort_list, list) {
773 if (exclude_other && (se == &sort_parent))
774 continue;
776 fprintf(fp, " ");
777 ret += se->print(fp, self);
780 ret += fprintf(fp, "\n");
782 return ret;
789 static struct symbol *
790 resolve_symbol(struct thread *thread, struct map **mapp,
791 struct dso **dsop, __u64 *ipp)
793 struct dso *dso = dsop ? *dsop : NULL;
794 struct map *map = mapp ? *mapp : NULL;
795 uint64_t ip = *ipp;
797 if (!thread)
798 return NULL;
800 if (dso)
801 goto got_dso;
803 if (map)
804 goto got_map;
806 map = thread__find_map(thread, ip);
807 if (map != NULL) {
808 if (mapp)
809 *mapp = map;
810 got_map:
811 ip = map->map_ip(map, ip);
812 *ipp = ip;
814 dso = map->dso;
815 } else {
817 * If this is outside of all known maps,
818 * and is a negative address, try to look it
819 * up in the kernel dso, as it might be a
820 * vsyscall (which executes in user-mode):
822 if ((long long)ip < 0)
823 dso = kernel_dso;
825 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
827 if (dsop)
828 *dsop = dso;
830 if (!dso)
831 return NULL;
832 got_dso:
833 return dso->find_symbol(dso, ip);
836 static struct symbol *call__match(struct symbol *sym)
838 if (!sym)
839 return NULL;
841 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
842 return sym;
844 return NULL;
848 * collect histogram counts
851 static int
852 hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
853 struct symbol *sym, __u64 ip, struct perf_callchain_entry *chain,
854 char level, __u64 count)
856 struct rb_node **p = &hist.rb_node;
857 struct rb_node *parent = NULL;
858 struct hist_entry *he;
859 struct hist_entry entry = {
860 .thread = thread,
861 .map = map,
862 .dso = dso,
863 .sym = sym,
864 .ip = ip,
865 .level = level,
866 .count = count,
867 .parent = NULL,
869 int cmp;
871 if (sort__has_parent && chain) {
872 int i, nr = chain->hv;
873 struct symbol *sym;
874 struct dso *dso;
875 __u64 ip;
877 for (i = 0; i < chain->kernel; i++) {
878 ip = chain->ip[nr + i];
879 dso = kernel_dso;
880 sym = resolve_symbol(thread, NULL, &dso, &ip);
881 entry.parent = call__match(sym);
882 if (entry.parent)
883 goto got_parent;
885 nr += i;
887 for (i = 0; i < chain->user; i++) {
888 ip = chain->ip[nr + i];
889 sym = resolve_symbol(thread, NULL, NULL, &ip);
890 entry.parent = call__match(sym);
891 if (entry.parent)
892 goto got_parent;
894 nr += i;
896 got_parent:
898 while (*p != NULL) {
899 parent = *p;
900 he = rb_entry(parent, struct hist_entry, rb_node);
902 cmp = hist_entry__cmp(&entry, he);
904 if (!cmp) {
905 he->count += count;
906 return 0;
909 if (cmp < 0)
910 p = &(*p)->rb_left;
911 else
912 p = &(*p)->rb_right;
915 he = malloc(sizeof(*he));
916 if (!he)
917 return -ENOMEM;
918 *he = entry;
919 rb_link_node(&he->rb_node, parent, p);
920 rb_insert_color(&he->rb_node, &hist);
922 return 0;
925 static void hist_entry__free(struct hist_entry *he)
927 free(he);
931 * collapse the histogram
934 static struct rb_root collapse_hists;
936 static void collapse__insert_entry(struct hist_entry *he)
938 struct rb_node **p = &collapse_hists.rb_node;
939 struct rb_node *parent = NULL;
940 struct hist_entry *iter;
941 int64_t cmp;
943 while (*p != NULL) {
944 parent = *p;
945 iter = rb_entry(parent, struct hist_entry, rb_node);
947 cmp = hist_entry__collapse(iter, he);
949 if (!cmp) {
950 iter->count += he->count;
951 hist_entry__free(he);
952 return;
955 if (cmp < 0)
956 p = &(*p)->rb_left;
957 else
958 p = &(*p)->rb_right;
961 rb_link_node(&he->rb_node, parent, p);
962 rb_insert_color(&he->rb_node, &collapse_hists);
965 static void collapse__resort(void)
967 struct rb_node *next;
968 struct hist_entry *n;
970 if (!sort__need_collapse)
971 return;
973 next = rb_first(&hist);
974 while (next) {
975 n = rb_entry(next, struct hist_entry, rb_node);
976 next = rb_next(&n->rb_node);
978 rb_erase(&n->rb_node, &hist);
979 collapse__insert_entry(n);
984 * reverse the map, sort on count.
987 static struct rb_root output_hists;
989 static void output__insert_entry(struct hist_entry *he)
991 struct rb_node **p = &output_hists.rb_node;
992 struct rb_node *parent = NULL;
993 struct hist_entry *iter;
995 while (*p != NULL) {
996 parent = *p;
997 iter = rb_entry(parent, struct hist_entry, rb_node);
999 if (he->count > iter->count)
1000 p = &(*p)->rb_left;
1001 else
1002 p = &(*p)->rb_right;
1005 rb_link_node(&he->rb_node, parent, p);
1006 rb_insert_color(&he->rb_node, &output_hists);
1009 static void output__resort(void)
1011 struct rb_node *next;
1012 struct hist_entry *n;
1013 struct rb_root *tree = &hist;
1015 if (sort__need_collapse)
1016 tree = &collapse_hists;
1018 next = rb_first(tree);
1020 while (next) {
1021 n = rb_entry(next, struct hist_entry, rb_node);
1022 next = rb_next(&n->rb_node);
1024 rb_erase(&n->rb_node, tree);
1025 output__insert_entry(n);
1029 static size_t output__fprintf(FILE *fp, __u64 total_samples)
1031 struct hist_entry *pos;
1032 struct sort_entry *se;
1033 struct rb_node *nd;
1034 size_t ret = 0;
1036 fprintf(fp, "\n");
1037 fprintf(fp, "#\n");
1038 fprintf(fp, "# (%Ld samples)\n", (__u64)total_samples);
1039 fprintf(fp, "#\n");
1041 fprintf(fp, "# Overhead");
1042 list_for_each_entry(se, &hist_entry__sort_list, list) {
1043 if (exclude_other && (se == &sort_parent))
1044 continue;
1045 fprintf(fp, " %s", se->header);
1047 fprintf(fp, "\n");
1049 fprintf(fp, "# ........");
1050 list_for_each_entry(se, &hist_entry__sort_list, list) {
1051 int i;
1053 if (exclude_other && (se == &sort_parent))
1054 continue;
1056 fprintf(fp, " ");
1057 for (i = 0; i < strlen(se->header); i++)
1058 fprintf(fp, ".");
1060 fprintf(fp, "\n");
1062 fprintf(fp, "#\n");
1064 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
1065 pos = rb_entry(nd, struct hist_entry, rb_node);
1066 ret += hist_entry__fprintf(fp, pos, total_samples);
1069 if (sort_order == default_sort_order &&
1070 parent_pattern == default_parent_pattern) {
1071 fprintf(fp, "#\n");
1072 fprintf(fp, "# (For more details, try: perf report --sort comm,dso,symbol)\n");
1073 fprintf(fp, "#\n");
1075 fprintf(fp, "\n");
1077 return ret;
1080 static void register_idle_thread(void)
1082 struct thread *thread = threads__findnew(0);
1084 if (thread == NULL ||
1085 thread__set_comm(thread, "[idle]")) {
1086 fprintf(stderr, "problem inserting idle task.\n");
1087 exit(-1);
1091 static unsigned long total = 0,
1092 total_mmap = 0,
1093 total_comm = 0,
1094 total_fork = 0,
1095 total_unknown = 0,
1096 total_lost = 0;
1098 static int validate_chain(struct perf_callchain_entry *chain, event_t *event)
1100 unsigned int chain_size;
1102 if (chain->nr > MAX_STACK_DEPTH)
1103 return -1;
1104 if (chain->hv > MAX_STACK_DEPTH)
1105 return -1;
1106 if (chain->kernel > MAX_STACK_DEPTH)
1107 return -1;
1108 if (chain->user > MAX_STACK_DEPTH)
1109 return -1;
1110 if (chain->hv + chain->kernel + chain->user != chain->nr)
1111 return -1;
1113 chain_size = event->header.size;
1114 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
1116 if (chain->nr*sizeof(__u64) > chain_size)
1117 return -1;
1119 return 0;
1122 static int
1123 process_overflow_event(event_t *event, unsigned long offset, unsigned long head)
1125 char level;
1126 int show = 0;
1127 struct dso *dso = NULL;
1128 struct thread *thread = threads__findnew(event->ip.pid);
1129 __u64 ip = event->ip.ip;
1130 __u64 period = 1;
1131 struct map *map = NULL;
1132 void *more_data = event->ip.__more_data;
1133 struct perf_callchain_entry *chain = NULL;
1135 if (event->header.type & PERF_SAMPLE_PERIOD) {
1136 period = *(__u64 *)more_data;
1137 more_data += sizeof(__u64);
1140 dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p period: %Ld\n",
1141 (void *)(offset + head),
1142 (void *)(long)(event->header.size),
1143 event->header.misc,
1144 event->ip.pid,
1145 (void *)(long)ip,
1146 (long long)period);
1148 if (event->header.type & PERF_SAMPLE_CALLCHAIN) {
1149 int i;
1151 chain = (void *)more_data;
1153 dprintf("... chain: u:%d, k:%d, nr:%d\n",
1154 chain->user,
1155 chain->kernel,
1156 chain->nr);
1158 if (validate_chain(chain, event) < 0) {
1159 eprintf("call-chain problem with event, skipping it.\n");
1160 return 0;
1163 if (dump_trace) {
1164 for (i = 0; i < chain->nr; i++)
1165 dprintf("..... %2d: %016Lx\n", i, chain->ip[i]);
1169 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1171 if (thread == NULL) {
1172 eprintf("problem processing %d event, skipping it.\n",
1173 event->header.type);
1174 return -1;
1177 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
1178 show = SHOW_KERNEL;
1179 level = 'k';
1181 dso = kernel_dso;
1183 dprintf(" ...... dso: %s\n", dso->name);
1185 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
1187 show = SHOW_USER;
1188 level = '.';
1190 } else {
1191 show = SHOW_HV;
1192 level = 'H';
1193 dprintf(" ...... dso: [hypervisor]\n");
1196 if (show & show_mask) {
1197 struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
1199 if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
1200 eprintf("problem incrementing symbol count, skipping event\n");
1201 return -1;
1204 total += period;
1206 return 0;
1209 static int
1210 process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
1212 struct thread *thread = threads__findnew(event->mmap.pid);
1213 struct map *map = map__new(&event->mmap);
1215 dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
1216 (void *)(offset + head),
1217 (void *)(long)(event->header.size),
1218 event->mmap.pid,
1219 (void *)(long)event->mmap.start,
1220 (void *)(long)event->mmap.len,
1221 (void *)(long)event->mmap.pgoff,
1222 event->mmap.filename);
1224 if (thread == NULL || map == NULL) {
1225 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
1226 return 0;
1229 thread__insert_map(thread, map);
1230 total_mmap++;
1232 return 0;
1235 static int
1236 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
1238 struct thread *thread = threads__findnew(event->comm.pid);
1240 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
1241 (void *)(offset + head),
1242 (void *)(long)(event->header.size),
1243 event->comm.comm, event->comm.pid);
1245 if (thread == NULL ||
1246 thread__set_comm(thread, event->comm.comm)) {
1247 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
1248 return -1;
1250 total_comm++;
1252 return 0;
1255 static int
1256 process_fork_event(event_t *event, unsigned long offset, unsigned long head)
1258 struct thread *thread = threads__findnew(event->fork.pid);
1259 struct thread *parent = threads__findnew(event->fork.ppid);
1261 dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
1262 (void *)(offset + head),
1263 (void *)(long)(event->header.size),
1264 event->fork.pid, event->fork.ppid);
1266 if (!thread || !parent || thread__fork(thread, parent)) {
1267 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
1268 return -1;
1270 total_fork++;
1272 return 0;
1275 static int
1276 process_period_event(event_t *event, unsigned long offset, unsigned long head)
1278 dprintf("%p [%p]: PERF_EVENT_PERIOD: time:%Ld, id:%Ld: period:%Ld\n",
1279 (void *)(offset + head),
1280 (void *)(long)(event->header.size),
1281 event->period.time,
1282 event->period.id,
1283 event->period.sample_period);
1285 return 0;
1288 static int
1289 process_lost_event(event_t *event, unsigned long offset, unsigned long head)
1291 dprintf("%p [%p]: PERF_EVENT_LOST: id:%Ld: lost:%Ld\n",
1292 (void *)(offset + head),
1293 (void *)(long)(event->header.size),
1294 event->lost.id,
1295 event->lost.lost);
1297 total_lost += event->lost.lost;
1299 return 0;
1302 static void trace_event(event_t *event)
1304 unsigned char *raw_event = (void *)event;
1305 char *color = PERF_COLOR_BLUE;
1306 int i, j;
1308 if (!dump_trace)
1309 return;
1311 dprintf(".");
1312 cdprintf("\n. ... raw event: size %d bytes\n", event->header.size);
1314 for (i = 0; i < event->header.size; i++) {
1315 if ((i & 15) == 0) {
1316 dprintf(".");
1317 cdprintf(" %04x: ", i);
1320 cdprintf(" %02x", raw_event[i]);
1322 if (((i & 15) == 15) || i == event->header.size-1) {
1323 cdprintf(" ");
1324 for (j = 0; j < 15-(i & 15); j++)
1325 cdprintf(" ");
1326 for (j = 0; j < (i & 15); j++) {
1327 if (isprint(raw_event[i-15+j]))
1328 cdprintf("%c", raw_event[i-15+j]);
1329 else
1330 cdprintf(".");
1332 cdprintf("\n");
1335 dprintf(".\n");
1338 static int
1339 process_event(event_t *event, unsigned long offset, unsigned long head)
1341 trace_event(event);
1343 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW)
1344 return process_overflow_event(event, offset, head);
1346 switch (event->header.type) {
1347 case PERF_EVENT_MMAP:
1348 return process_mmap_event(event, offset, head);
1350 case PERF_EVENT_COMM:
1351 return process_comm_event(event, offset, head);
1353 case PERF_EVENT_FORK:
1354 return process_fork_event(event, offset, head);
1356 case PERF_EVENT_PERIOD:
1357 return process_period_event(event, offset, head);
1359 case PERF_EVENT_LOST:
1360 return process_lost_event(event, offset, head);
1363 * We dont process them right now but they are fine:
1366 case PERF_EVENT_THROTTLE:
1367 case PERF_EVENT_UNTHROTTLE:
1368 return 0;
1370 default:
1371 return -1;
1374 return 0;
1377 static int __cmd_report(void)
1379 int ret, rc = EXIT_FAILURE;
1380 unsigned long offset = 0;
1381 unsigned long head = 0;
1382 struct stat stat;
1383 event_t *event;
1384 uint32_t size;
1385 char *buf;
1387 register_idle_thread();
1389 input = open(input_name, O_RDONLY);
1390 if (input < 0) {
1391 fprintf(stderr, " failed to open file: %s", input_name);
1392 if (!strcmp(input_name, "perf.data"))
1393 fprintf(stderr, " (try 'perf record' first)");
1394 fprintf(stderr, "\n");
1395 exit(-1);
1398 ret = fstat(input, &stat);
1399 if (ret < 0) {
1400 perror("failed to stat file");
1401 exit(-1);
1404 if (!stat.st_size) {
1405 fprintf(stderr, "zero-sized file, nothing to do!\n");
1406 exit(0);
1409 if (load_kernel() < 0) {
1410 perror("failed to load kernel symbols");
1411 return EXIT_FAILURE;
1414 if (!full_paths) {
1415 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
1416 perror("failed to get the current directory");
1417 return EXIT_FAILURE;
1419 cwdlen = strlen(cwd);
1420 } else {
1421 cwd = NULL;
1422 cwdlen = 0;
1424 remap:
1425 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1426 MAP_SHARED, input, offset);
1427 if (buf == MAP_FAILED) {
1428 perror("failed to mmap file");
1429 exit(-1);
1432 more:
1433 event = (event_t *)(buf + head);
1435 size = event->header.size;
1436 if (!size)
1437 size = 8;
1439 if (head + event->header.size >= page_size * mmap_window) {
1440 unsigned long shift = page_size * (head / page_size);
1441 int ret;
1443 ret = munmap(buf, page_size * mmap_window);
1444 assert(ret == 0);
1446 offset += shift;
1447 head -= shift;
1448 goto remap;
1451 size = event->header.size;
1453 dprintf("\n%p [%p]: event: %d\n",
1454 (void *)(offset + head),
1455 (void *)(long)event->header.size,
1456 event->header.type);
1458 if (!size || process_event(event, offset, head) < 0) {
1460 dprintf("%p [%p]: skipping unknown header type: %d\n",
1461 (void *)(offset + head),
1462 (void *)(long)(event->header.size),
1463 event->header.type);
1465 total_unknown++;
1468 * assume we lost track of the stream, check alignment, and
1469 * increment a single u64 in the hope to catch on again 'soon'.
1472 if (unlikely(head & 7))
1473 head &= ~7ULL;
1475 size = 8;
1478 head += size;
1480 if (offset + head < stat.st_size)
1481 goto more;
1483 rc = EXIT_SUCCESS;
1484 close(input);
1486 dprintf(" IP events: %10ld\n", total);
1487 dprintf(" mmap events: %10ld\n", total_mmap);
1488 dprintf(" comm events: %10ld\n", total_comm);
1489 dprintf(" fork events: %10ld\n", total_fork);
1490 dprintf(" lost events: %10ld\n", total_lost);
1491 dprintf(" unknown events: %10ld\n", total_unknown);
1493 if (dump_trace)
1494 return 0;
1496 if (verbose >= 3)
1497 threads__fprintf(stdout);
1499 if (verbose >= 2)
1500 dsos__fprintf(stdout);
1502 collapse__resort();
1503 output__resort();
1504 output__fprintf(stdout, total);
1506 return rc;
1509 static const char * const report_usage[] = {
1510 "perf report [<options>] <command>",
1511 NULL
1514 static const struct option options[] = {
1515 OPT_STRING('i', "input", &input_name, "file",
1516 "input file name"),
1517 OPT_BOOLEAN('v', "verbose", &verbose,
1518 "be more verbose (show symbol address, etc)"),
1519 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1520 "dump raw trace in ASCII"),
1521 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
1522 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1523 "sort by key(s): pid, comm, dso, symbol, parent"),
1524 OPT_BOOLEAN('P', "full-paths", &full_paths,
1525 "Don't shorten the pathnames taking into account the cwd"),
1526 OPT_STRING('p', "parent", &parent_pattern, "regex",
1527 "regex filter to identify parent, see: '--sort parent'"),
1528 OPT_BOOLEAN('x', "exclude-other", &exclude_other,
1529 "Only display entries with parent-match"),
1530 OPT_END()
1533 static void setup_sorting(void)
1535 char *tmp, *tok, *str = strdup(sort_order);
1537 for (tok = strtok_r(str, ", ", &tmp);
1538 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1539 if (sort_dimension__add(tok) < 0) {
1540 error("Unknown --sort key: `%s'", tok);
1541 usage_with_options(report_usage, options);
1545 free(str);
1548 int cmd_report(int argc, const char **argv, const char *prefix)
1550 symbol__init();
1552 page_size = getpagesize();
1554 argc = parse_options(argc, argv, options, report_usage, 0);
1556 setup_sorting();
1558 if (parent_pattern != default_parent_pattern)
1559 sort_dimension__add("parent");
1560 else
1561 exclude_other = 0;
1564 * Any (unrecognized) arguments left?
1566 if (argc)
1567 usage_with_options(report_usage, options);
1569 setup_pager();
1571 return __cmd_report();