perfcounter: Handle some IO return values
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / tools / perf / builtin-report.c
blob5eb5566f0c95426dd3f5c4c3cf196faf55080fbd
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 ip_callchain {
63 u64 nr;
64 u64 ips[0];
67 struct mmap_event {
68 struct perf_event_header header;
69 u32 pid, tid;
70 u64 start;
71 u64 len;
72 u64 pgoff;
73 char filename[PATH_MAX];
76 struct comm_event {
77 struct perf_event_header header;
78 u32 pid, tid;
79 char comm[16];
82 struct fork_event {
83 struct perf_event_header header;
84 u32 pid, ppid;
87 struct period_event {
88 struct perf_event_header header;
89 u64 time;
90 u64 id;
91 u64 sample_period;
94 struct lost_event {
95 struct perf_event_header header;
96 u64 id;
97 u64 lost;
100 typedef union event_union {
101 struct perf_event_header header;
102 struct ip_event ip;
103 struct mmap_event mmap;
104 struct comm_event comm;
105 struct fork_event fork;
106 struct period_event period;
107 struct lost_event lost;
108 } event_t;
110 static LIST_HEAD(dsos);
111 static struct dso *kernel_dso;
112 static struct dso *vdso;
114 static void dsos__add(struct dso *dso)
116 list_add_tail(&dso->node, &dsos);
119 static struct dso *dsos__find(const char *name)
121 struct dso *pos;
123 list_for_each_entry(pos, &dsos, node)
124 if (strcmp(pos->name, name) == 0)
125 return pos;
126 return NULL;
129 static struct dso *dsos__findnew(const char *name)
131 struct dso *dso = dsos__find(name);
132 int nr;
134 if (dso)
135 return dso;
137 dso = dso__new(name, 0);
138 if (!dso)
139 goto out_delete_dso;
141 nr = dso__load(dso, NULL, verbose);
142 if (nr < 0) {
143 eprintf("Failed to open: %s\n", name);
144 goto out_delete_dso;
146 if (!nr)
147 eprintf("No symbols found in: %s, maybe install a debug package?\n", name);
149 dsos__add(dso);
151 return dso;
153 out_delete_dso:
154 dso__delete(dso);
155 return NULL;
158 static void dsos__fprintf(FILE *fp)
160 struct dso *pos;
162 list_for_each_entry(pos, &dsos, node)
163 dso__fprintf(pos, fp);
166 static struct symbol *vdso__find_symbol(struct dso *dso, u64 ip)
168 return dso__find_symbol(kernel_dso, ip);
171 static int load_kernel(void)
173 int err;
175 kernel_dso = dso__new("[kernel]", 0);
176 if (!kernel_dso)
177 return -1;
179 err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose);
180 if (err) {
181 dso__delete(kernel_dso);
182 kernel_dso = NULL;
183 } else
184 dsos__add(kernel_dso);
186 vdso = dso__new("[vdso]", 0);
187 if (!vdso)
188 return -1;
190 vdso->find_symbol = vdso__find_symbol;
192 dsos__add(vdso);
194 return err;
197 static char __cwd[PATH_MAX];
198 static char *cwd = __cwd;
199 static int cwdlen;
201 static int strcommon(const char *pathname)
203 int n = 0;
205 while (pathname[n] == cwd[n] && n < cwdlen)
206 ++n;
208 return n;
211 struct map {
212 struct list_head node;
213 u64 start;
214 u64 end;
215 u64 pgoff;
216 u64 (*map_ip)(struct map *, u64);
217 struct dso *dso;
220 static u64 map__map_ip(struct map *map, u64 ip)
222 return ip - map->start + map->pgoff;
225 static u64 vdso__map_ip(struct map *map, u64 ip)
227 return ip;
230 static inline int is_anon_memory(const char *filename)
232 return strcmp(filename, "//anon") == 0;
235 static struct map *map__new(struct mmap_event *event)
237 struct map *self = malloc(sizeof(*self));
239 if (self != NULL) {
240 const char *filename = event->filename;
241 char newfilename[PATH_MAX];
242 int anon;
244 if (cwd) {
245 int n = strcommon(filename);
247 if (n == cwdlen) {
248 snprintf(newfilename, sizeof(newfilename),
249 ".%s", filename + n);
250 filename = newfilename;
254 anon = is_anon_memory(filename);
256 if (anon) {
257 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
258 filename = newfilename;
261 self->start = event->start;
262 self->end = event->start + event->len;
263 self->pgoff = event->pgoff;
265 self->dso = dsos__findnew(filename);
266 if (self->dso == NULL)
267 goto out_delete;
269 if (self->dso == vdso || anon)
270 self->map_ip = vdso__map_ip;
271 else
272 self->map_ip = map__map_ip;
274 return self;
275 out_delete:
276 free(self);
277 return NULL;
280 static struct map *map__clone(struct map *self)
282 struct map *map = malloc(sizeof(*self));
284 if (!map)
285 return NULL;
287 memcpy(map, self, sizeof(*self));
289 return map;
292 static int map__overlap(struct map *l, struct map *r)
294 if (l->start > r->start) {
295 struct map *t = l;
296 l = r;
297 r = t;
300 if (l->end > r->start)
301 return 1;
303 return 0;
306 static size_t map__fprintf(struct map *self, FILE *fp)
308 return fprintf(fp, " %Lx-%Lx %Lx %s\n",
309 self->start, self->end, self->pgoff, self->dso->name);
313 struct thread {
314 struct rb_node rb_node;
315 struct list_head maps;
316 pid_t pid;
317 char *comm;
320 static struct thread *thread__new(pid_t pid)
322 struct thread *self = malloc(sizeof(*self));
324 if (self != NULL) {
325 self->pid = pid;
326 self->comm = malloc(32);
327 if (self->comm)
328 snprintf(self->comm, 32, ":%d", self->pid);
329 INIT_LIST_HEAD(&self->maps);
332 return self;
335 static int thread__set_comm(struct thread *self, const char *comm)
337 if (self->comm)
338 free(self->comm);
339 self->comm = strdup(comm);
340 return self->comm ? 0 : -ENOMEM;
343 static size_t thread__fprintf(struct thread *self, FILE *fp)
345 struct map *pos;
346 size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
348 list_for_each_entry(pos, &self->maps, node)
349 ret += map__fprintf(pos, fp);
351 return ret;
355 static struct rb_root threads;
356 static struct thread *last_match;
358 static struct thread *threads__findnew(pid_t pid)
360 struct rb_node **p = &threads.rb_node;
361 struct rb_node *parent = NULL;
362 struct thread *th;
365 * Font-end cache - PID lookups come in blocks,
366 * so most of the time we dont have to look up
367 * the full rbtree:
369 if (last_match && last_match->pid == pid)
370 return last_match;
372 while (*p != NULL) {
373 parent = *p;
374 th = rb_entry(parent, struct thread, rb_node);
376 if (th->pid == pid) {
377 last_match = th;
378 return th;
381 if (pid < th->pid)
382 p = &(*p)->rb_left;
383 else
384 p = &(*p)->rb_right;
387 th = thread__new(pid);
388 if (th != NULL) {
389 rb_link_node(&th->rb_node, parent, p);
390 rb_insert_color(&th->rb_node, &threads);
391 last_match = th;
394 return th;
397 static void thread__insert_map(struct thread *self, struct map *map)
399 struct map *pos, *tmp;
401 list_for_each_entry_safe(pos, tmp, &self->maps, node) {
402 if (map__overlap(pos, map)) {
403 list_del_init(&pos->node);
404 /* XXX leaks dsos */
405 free(pos);
409 list_add_tail(&map->node, &self->maps);
412 static int thread__fork(struct thread *self, struct thread *parent)
414 struct map *map;
416 if (self->comm)
417 free(self->comm);
418 self->comm = strdup(parent->comm);
419 if (!self->comm)
420 return -ENOMEM;
422 list_for_each_entry(map, &parent->maps, node) {
423 struct map *new = map__clone(map);
424 if (!new)
425 return -ENOMEM;
426 thread__insert_map(self, new);
429 return 0;
432 static struct map *thread__find_map(struct thread *self, u64 ip)
434 struct map *pos;
436 if (self == NULL)
437 return NULL;
439 list_for_each_entry(pos, &self->maps, node)
440 if (ip >= pos->start && ip <= pos->end)
441 return pos;
443 return NULL;
446 static size_t threads__fprintf(FILE *fp)
448 size_t ret = 0;
449 struct rb_node *nd;
451 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
452 struct thread *pos = rb_entry(nd, struct thread, rb_node);
454 ret += thread__fprintf(pos, fp);
457 return ret;
461 * histogram, sorted on item, collects counts
464 static struct rb_root hist;
466 struct hist_entry {
467 struct rb_node rb_node;
469 struct thread *thread;
470 struct map *map;
471 struct dso *dso;
472 struct symbol *sym;
473 struct symbol *parent;
474 u64 ip;
475 char level;
477 u64 count;
481 * configurable sorting bits
484 struct sort_entry {
485 struct list_head list;
487 char *header;
489 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
490 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
491 size_t (*print)(FILE *fp, struct hist_entry *);
494 static int64_t cmp_null(void *l, void *r)
496 if (!l && !r)
497 return 0;
498 else if (!l)
499 return -1;
500 else
501 return 1;
504 /* --sort pid */
506 static int64_t
507 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
509 return right->thread->pid - left->thread->pid;
512 static size_t
513 sort__thread_print(FILE *fp, struct hist_entry *self)
515 return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
518 static struct sort_entry sort_thread = {
519 .header = " Command: Pid",
520 .cmp = sort__thread_cmp,
521 .print = sort__thread_print,
524 /* --sort comm */
526 static int64_t
527 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
529 return right->thread->pid - left->thread->pid;
532 static int64_t
533 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
535 char *comm_l = left->thread->comm;
536 char *comm_r = right->thread->comm;
538 if (!comm_l || !comm_r)
539 return cmp_null(comm_l, comm_r);
541 return strcmp(comm_l, comm_r);
544 static size_t
545 sort__comm_print(FILE *fp, struct hist_entry *self)
547 return fprintf(fp, "%16s", self->thread->comm);
550 static struct sort_entry sort_comm = {
551 .header = " Command",
552 .cmp = sort__comm_cmp,
553 .collapse = sort__comm_collapse,
554 .print = sort__comm_print,
557 /* --sort dso */
559 static int64_t
560 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
562 struct dso *dso_l = left->dso;
563 struct dso *dso_r = right->dso;
565 if (!dso_l || !dso_r)
566 return cmp_null(dso_l, dso_r);
568 return strcmp(dso_l->name, dso_r->name);
571 static size_t
572 sort__dso_print(FILE *fp, struct hist_entry *self)
574 if (self->dso)
575 return fprintf(fp, "%-25s", self->dso->name);
577 return fprintf(fp, "%016llx ", (u64)self->ip);
580 static struct sort_entry sort_dso = {
581 .header = "Shared Object ",
582 .cmp = sort__dso_cmp,
583 .print = sort__dso_print,
586 /* --sort symbol */
588 static int64_t
589 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
591 u64 ip_l, ip_r;
593 if (left->sym == right->sym)
594 return 0;
596 ip_l = left->sym ? left->sym->start : left->ip;
597 ip_r = right->sym ? right->sym->start : right->ip;
599 return (int64_t)(ip_r - ip_l);
602 static size_t
603 sort__sym_print(FILE *fp, struct hist_entry *self)
605 size_t ret = 0;
607 if (verbose)
608 ret += fprintf(fp, "%#018llx ", (u64)self->ip);
610 if (self->sym) {
611 ret += fprintf(fp, "[%c] %s",
612 self->dso == kernel_dso ? 'k' : '.', self->sym->name);
613 } else {
614 ret += fprintf(fp, "%#016llx", (u64)self->ip);
617 return ret;
620 static struct sort_entry sort_sym = {
621 .header = "Symbol",
622 .cmp = sort__sym_cmp,
623 .print = sort__sym_print,
626 /* --sort parent */
628 static int64_t
629 sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
631 struct symbol *sym_l = left->parent;
632 struct symbol *sym_r = right->parent;
634 if (!sym_l || !sym_r)
635 return cmp_null(sym_l, sym_r);
637 return strcmp(sym_l->name, sym_r->name);
640 static size_t
641 sort__parent_print(FILE *fp, struct hist_entry *self)
643 size_t ret = 0;
645 ret += fprintf(fp, "%-20s", self->parent ? self->parent->name : "[other]");
647 return ret;
650 static struct sort_entry sort_parent = {
651 .header = "Parent symbol ",
652 .cmp = sort__parent_cmp,
653 .print = sort__parent_print,
656 static int sort__need_collapse = 0;
657 static int sort__has_parent = 0;
659 struct sort_dimension {
660 char *name;
661 struct sort_entry *entry;
662 int taken;
665 static struct sort_dimension sort_dimensions[] = {
666 { .name = "pid", .entry = &sort_thread, },
667 { .name = "comm", .entry = &sort_comm, },
668 { .name = "dso", .entry = &sort_dso, },
669 { .name = "symbol", .entry = &sort_sym, },
670 { .name = "parent", .entry = &sort_parent, },
673 static LIST_HEAD(hist_entry__sort_list);
675 static int sort_dimension__add(char *tok)
677 int i;
679 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
680 struct sort_dimension *sd = &sort_dimensions[i];
682 if (sd->taken)
683 continue;
685 if (strncasecmp(tok, sd->name, strlen(tok)))
686 continue;
688 if (sd->entry->collapse)
689 sort__need_collapse = 1;
691 if (sd->entry == &sort_parent) {
692 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
693 if (ret) {
694 char err[BUFSIZ];
696 regerror(ret, &parent_regex, err, sizeof(err));
697 fprintf(stderr, "Invalid regex: %s\n%s",
698 parent_pattern, err);
699 exit(-1);
701 sort__has_parent = 1;
704 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
705 sd->taken = 1;
707 return 0;
710 return -ESRCH;
713 static int64_t
714 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
716 struct sort_entry *se;
717 int64_t cmp = 0;
719 list_for_each_entry(se, &hist_entry__sort_list, list) {
720 cmp = se->cmp(left, right);
721 if (cmp)
722 break;
725 return cmp;
728 static int64_t
729 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
731 struct sort_entry *se;
732 int64_t cmp = 0;
734 list_for_each_entry(se, &hist_entry__sort_list, list) {
735 int64_t (*f)(struct hist_entry *, struct hist_entry *);
737 f = se->collapse ?: se->cmp;
739 cmp = f(left, right);
740 if (cmp)
741 break;
744 return cmp;
747 static size_t
748 hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
750 struct sort_entry *se;
751 size_t ret;
753 if (exclude_other && !self->parent)
754 return 0;
756 if (total_samples) {
757 double percent = self->count * 100.0 / total_samples;
758 char *color = PERF_COLOR_NORMAL;
761 * We color high-overhead entries in red, mid-overhead
762 * entries in green - and keep the low overhead places
763 * normal:
765 if (percent >= 5.0) {
766 color = PERF_COLOR_RED;
767 } else {
768 if (percent >= 0.5)
769 color = PERF_COLOR_GREEN;
772 ret = color_fprintf(fp, color, " %6.2f%%",
773 (self->count * 100.0) / total_samples);
774 } else
775 ret = fprintf(fp, "%12Ld ", self->count);
777 list_for_each_entry(se, &hist_entry__sort_list, list) {
778 if (exclude_other && (se == &sort_parent))
779 continue;
781 fprintf(fp, " ");
782 ret += se->print(fp, self);
785 ret += fprintf(fp, "\n");
787 return ret;
794 static struct symbol *
795 resolve_symbol(struct thread *thread, struct map **mapp,
796 struct dso **dsop, u64 *ipp)
798 struct dso *dso = dsop ? *dsop : NULL;
799 struct map *map = mapp ? *mapp : NULL;
800 uint64_t ip = *ipp;
802 if (!thread)
803 return NULL;
805 if (dso)
806 goto got_dso;
808 if (map)
809 goto got_map;
811 map = thread__find_map(thread, ip);
812 if (map != NULL) {
813 if (mapp)
814 *mapp = map;
815 got_map:
816 ip = map->map_ip(map, ip);
817 *ipp = ip;
819 dso = map->dso;
820 } else {
822 * If this is outside of all known maps,
823 * and is a negative address, try to look it
824 * up in the kernel dso, as it might be a
825 * vsyscall (which executes in user-mode):
827 if ((long long)ip < 0)
828 dso = kernel_dso;
830 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
832 if (dsop)
833 *dsop = dso;
835 if (!dso)
836 return NULL;
837 got_dso:
838 return dso->find_symbol(dso, ip);
841 static int call__match(struct symbol *sym)
843 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
844 return 1;
846 return 0;
850 * collect histogram counts
853 static int
854 hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
855 struct symbol *sym, u64 ip, struct ip_callchain *chain,
856 char level, u64 count)
858 struct rb_node **p = &hist.rb_node;
859 struct rb_node *parent = NULL;
860 struct hist_entry *he;
861 struct hist_entry entry = {
862 .thread = thread,
863 .map = map,
864 .dso = dso,
865 .sym = sym,
866 .ip = ip,
867 .level = level,
868 .count = count,
869 .parent = NULL,
871 int cmp;
873 if (sort__has_parent && chain) {
874 u64 context = PERF_CONTEXT_MAX;
875 int i;
877 for (i = 0; i < chain->nr; i++) {
878 u64 ip = chain->ips[i];
879 struct dso *dso = NULL;
880 struct symbol *sym;
882 if (ip >= PERF_CONTEXT_MAX) {
883 context = ip;
884 continue;
887 switch (context) {
888 case PERF_CONTEXT_KERNEL:
889 dso = kernel_dso;
890 break;
891 default:
892 break;
895 sym = resolve_symbol(thread, NULL, &dso, &ip);
897 if (sym && call__match(sym)) {
898 entry.parent = sym;
899 break;
904 while (*p != NULL) {
905 parent = *p;
906 he = rb_entry(parent, struct hist_entry, rb_node);
908 cmp = hist_entry__cmp(&entry, he);
910 if (!cmp) {
911 he->count += count;
912 return 0;
915 if (cmp < 0)
916 p = &(*p)->rb_left;
917 else
918 p = &(*p)->rb_right;
921 he = malloc(sizeof(*he));
922 if (!he)
923 return -ENOMEM;
924 *he = entry;
925 rb_link_node(&he->rb_node, parent, p);
926 rb_insert_color(&he->rb_node, &hist);
928 return 0;
931 static void hist_entry__free(struct hist_entry *he)
933 free(he);
937 * collapse the histogram
940 static struct rb_root collapse_hists;
942 static void collapse__insert_entry(struct hist_entry *he)
944 struct rb_node **p = &collapse_hists.rb_node;
945 struct rb_node *parent = NULL;
946 struct hist_entry *iter;
947 int64_t cmp;
949 while (*p != NULL) {
950 parent = *p;
951 iter = rb_entry(parent, struct hist_entry, rb_node);
953 cmp = hist_entry__collapse(iter, he);
955 if (!cmp) {
956 iter->count += he->count;
957 hist_entry__free(he);
958 return;
961 if (cmp < 0)
962 p = &(*p)->rb_left;
963 else
964 p = &(*p)->rb_right;
967 rb_link_node(&he->rb_node, parent, p);
968 rb_insert_color(&he->rb_node, &collapse_hists);
971 static void collapse__resort(void)
973 struct rb_node *next;
974 struct hist_entry *n;
976 if (!sort__need_collapse)
977 return;
979 next = rb_first(&hist);
980 while (next) {
981 n = rb_entry(next, struct hist_entry, rb_node);
982 next = rb_next(&n->rb_node);
984 rb_erase(&n->rb_node, &hist);
985 collapse__insert_entry(n);
990 * reverse the map, sort on count.
993 static struct rb_root output_hists;
995 static void output__insert_entry(struct hist_entry *he)
997 struct rb_node **p = &output_hists.rb_node;
998 struct rb_node *parent = NULL;
999 struct hist_entry *iter;
1001 while (*p != NULL) {
1002 parent = *p;
1003 iter = rb_entry(parent, struct hist_entry, rb_node);
1005 if (he->count > iter->count)
1006 p = &(*p)->rb_left;
1007 else
1008 p = &(*p)->rb_right;
1011 rb_link_node(&he->rb_node, parent, p);
1012 rb_insert_color(&he->rb_node, &output_hists);
1015 static void output__resort(void)
1017 struct rb_node *next;
1018 struct hist_entry *n;
1019 struct rb_root *tree = &hist;
1021 if (sort__need_collapse)
1022 tree = &collapse_hists;
1024 next = rb_first(tree);
1026 while (next) {
1027 n = rb_entry(next, struct hist_entry, rb_node);
1028 next = rb_next(&n->rb_node);
1030 rb_erase(&n->rb_node, tree);
1031 output__insert_entry(n);
1035 static size_t output__fprintf(FILE *fp, u64 total_samples)
1037 struct hist_entry *pos;
1038 struct sort_entry *se;
1039 struct rb_node *nd;
1040 size_t ret = 0;
1042 fprintf(fp, "\n");
1043 fprintf(fp, "#\n");
1044 fprintf(fp, "# (%Ld samples)\n", (u64)total_samples);
1045 fprintf(fp, "#\n");
1047 fprintf(fp, "# Overhead");
1048 list_for_each_entry(se, &hist_entry__sort_list, list) {
1049 if (exclude_other && (se == &sort_parent))
1050 continue;
1051 fprintf(fp, " %s", se->header);
1053 fprintf(fp, "\n");
1055 fprintf(fp, "# ........");
1056 list_for_each_entry(se, &hist_entry__sort_list, list) {
1057 int i;
1059 if (exclude_other && (se == &sort_parent))
1060 continue;
1062 fprintf(fp, " ");
1063 for (i = 0; i < strlen(se->header); i++)
1064 fprintf(fp, ".");
1066 fprintf(fp, "\n");
1068 fprintf(fp, "#\n");
1070 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
1071 pos = rb_entry(nd, struct hist_entry, rb_node);
1072 ret += hist_entry__fprintf(fp, pos, total_samples);
1075 if (sort_order == default_sort_order &&
1076 parent_pattern == default_parent_pattern) {
1077 fprintf(fp, "#\n");
1078 fprintf(fp, "# (For more details, try: perf report --sort comm,dso,symbol)\n");
1079 fprintf(fp, "#\n");
1081 fprintf(fp, "\n");
1083 return ret;
1086 static void register_idle_thread(void)
1088 struct thread *thread = threads__findnew(0);
1090 if (thread == NULL ||
1091 thread__set_comm(thread, "[idle]")) {
1092 fprintf(stderr, "problem inserting idle task.\n");
1093 exit(-1);
1097 static unsigned long total = 0,
1098 total_mmap = 0,
1099 total_comm = 0,
1100 total_fork = 0,
1101 total_unknown = 0,
1102 total_lost = 0;
1104 static int validate_chain(struct ip_callchain *chain, event_t *event)
1106 unsigned int chain_size;
1108 chain_size = event->header.size;
1109 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
1111 if (chain->nr*sizeof(u64) > chain_size)
1112 return -1;
1114 return 0;
1117 static int
1118 process_overflow_event(event_t *event, unsigned long offset, unsigned long head)
1120 char level;
1121 int show = 0;
1122 struct dso *dso = NULL;
1123 struct thread *thread = threads__findnew(event->ip.pid);
1124 u64 ip = event->ip.ip;
1125 u64 period = 1;
1126 struct map *map = NULL;
1127 void *more_data = event->ip.__more_data;
1128 struct ip_callchain *chain = NULL;
1130 if (event->header.type & PERF_SAMPLE_PERIOD) {
1131 period = *(u64 *)more_data;
1132 more_data += sizeof(u64);
1135 dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p period: %Ld\n",
1136 (void *)(offset + head),
1137 (void *)(long)(event->header.size),
1138 event->header.misc,
1139 event->ip.pid,
1140 (void *)(long)ip,
1141 (long long)period);
1143 if (event->header.type & PERF_SAMPLE_CALLCHAIN) {
1144 int i;
1146 chain = (void *)more_data;
1148 dprintf("... chain: nr:%Lu\n", chain->nr);
1150 if (validate_chain(chain, event) < 0) {
1151 eprintf("call-chain problem with event, skipping it.\n");
1152 return 0;
1155 if (dump_trace) {
1156 for (i = 0; i < chain->nr; i++)
1157 dprintf("..... %2d: %016Lx\n", i, chain->ips[i]);
1161 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1163 if (thread == NULL) {
1164 eprintf("problem processing %d event, skipping it.\n",
1165 event->header.type);
1166 return -1;
1169 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
1170 show = SHOW_KERNEL;
1171 level = 'k';
1173 dso = kernel_dso;
1175 dprintf(" ...... dso: %s\n", dso->name);
1177 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
1179 show = SHOW_USER;
1180 level = '.';
1182 } else {
1183 show = SHOW_HV;
1184 level = 'H';
1185 dprintf(" ...... dso: [hypervisor]\n");
1188 if (show & show_mask) {
1189 struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
1191 if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
1192 eprintf("problem incrementing symbol count, skipping event\n");
1193 return -1;
1196 total += period;
1198 return 0;
1201 static int
1202 process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
1204 struct thread *thread = threads__findnew(event->mmap.pid);
1205 struct map *map = map__new(&event->mmap);
1207 dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
1208 (void *)(offset + head),
1209 (void *)(long)(event->header.size),
1210 event->mmap.pid,
1211 (void *)(long)event->mmap.start,
1212 (void *)(long)event->mmap.len,
1213 (void *)(long)event->mmap.pgoff,
1214 event->mmap.filename);
1216 if (thread == NULL || map == NULL) {
1217 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
1218 return 0;
1221 thread__insert_map(thread, map);
1222 total_mmap++;
1224 return 0;
1227 static int
1228 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
1230 struct thread *thread = threads__findnew(event->comm.pid);
1232 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
1233 (void *)(offset + head),
1234 (void *)(long)(event->header.size),
1235 event->comm.comm, event->comm.pid);
1237 if (thread == NULL ||
1238 thread__set_comm(thread, event->comm.comm)) {
1239 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
1240 return -1;
1242 total_comm++;
1244 return 0;
1247 static int
1248 process_fork_event(event_t *event, unsigned long offset, unsigned long head)
1250 struct thread *thread = threads__findnew(event->fork.pid);
1251 struct thread *parent = threads__findnew(event->fork.ppid);
1253 dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
1254 (void *)(offset + head),
1255 (void *)(long)(event->header.size),
1256 event->fork.pid, event->fork.ppid);
1258 if (!thread || !parent || thread__fork(thread, parent)) {
1259 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
1260 return -1;
1262 total_fork++;
1264 return 0;
1267 static int
1268 process_period_event(event_t *event, unsigned long offset, unsigned long head)
1270 dprintf("%p [%p]: PERF_EVENT_PERIOD: time:%Ld, id:%Ld: period:%Ld\n",
1271 (void *)(offset + head),
1272 (void *)(long)(event->header.size),
1273 event->period.time,
1274 event->period.id,
1275 event->period.sample_period);
1277 return 0;
1280 static int
1281 process_lost_event(event_t *event, unsigned long offset, unsigned long head)
1283 dprintf("%p [%p]: PERF_EVENT_LOST: id:%Ld: lost:%Ld\n",
1284 (void *)(offset + head),
1285 (void *)(long)(event->header.size),
1286 event->lost.id,
1287 event->lost.lost);
1289 total_lost += event->lost.lost;
1291 return 0;
1294 static void trace_event(event_t *event)
1296 unsigned char *raw_event = (void *)event;
1297 char *color = PERF_COLOR_BLUE;
1298 int i, j;
1300 if (!dump_trace)
1301 return;
1303 dprintf(".");
1304 cdprintf("\n. ... raw event: size %d bytes\n", event->header.size);
1306 for (i = 0; i < event->header.size; i++) {
1307 if ((i & 15) == 0) {
1308 dprintf(".");
1309 cdprintf(" %04x: ", i);
1312 cdprintf(" %02x", raw_event[i]);
1314 if (((i & 15) == 15) || i == event->header.size-1) {
1315 cdprintf(" ");
1316 for (j = 0; j < 15-(i & 15); j++)
1317 cdprintf(" ");
1318 for (j = 0; j < (i & 15); j++) {
1319 if (isprint(raw_event[i-15+j]))
1320 cdprintf("%c", raw_event[i-15+j]);
1321 else
1322 cdprintf(".");
1324 cdprintf("\n");
1327 dprintf(".\n");
1330 static int
1331 process_event(event_t *event, unsigned long offset, unsigned long head)
1333 trace_event(event);
1335 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW)
1336 return process_overflow_event(event, offset, head);
1338 switch (event->header.type) {
1339 case PERF_EVENT_MMAP:
1340 return process_mmap_event(event, offset, head);
1342 case PERF_EVENT_COMM:
1343 return process_comm_event(event, offset, head);
1345 case PERF_EVENT_FORK:
1346 return process_fork_event(event, offset, head);
1348 case PERF_EVENT_PERIOD:
1349 return process_period_event(event, offset, head);
1351 case PERF_EVENT_LOST:
1352 return process_lost_event(event, offset, head);
1355 * We dont process them right now but they are fine:
1358 case PERF_EVENT_THROTTLE:
1359 case PERF_EVENT_UNTHROTTLE:
1360 return 0;
1362 default:
1363 return -1;
1366 return 0;
1369 static struct perf_file_header file_header;
1371 static int __cmd_report(void)
1373 int ret, rc = EXIT_FAILURE;
1374 unsigned long offset = 0;
1375 unsigned long head = sizeof(file_header);
1376 struct stat stat;
1377 event_t *event;
1378 uint32_t size;
1379 char *buf;
1381 register_idle_thread();
1383 input = open(input_name, O_RDONLY);
1384 if (input < 0) {
1385 fprintf(stderr, " failed to open file: %s", input_name);
1386 if (!strcmp(input_name, "perf.data"))
1387 fprintf(stderr, " (try 'perf record' first)");
1388 fprintf(stderr, "\n");
1389 exit(-1);
1392 ret = fstat(input, &stat);
1393 if (ret < 0) {
1394 perror("failed to stat file");
1395 exit(-1);
1398 if (!stat.st_size) {
1399 fprintf(stderr, "zero-sized file, nothing to do!\n");
1400 exit(0);
1403 if (read(input, &file_header, sizeof(file_header)) == -1) {
1404 perror("failed to read file headers");
1405 exit(-1);
1408 if (sort__has_parent &&
1409 !(file_header.sample_type & PERF_SAMPLE_CALLCHAIN)) {
1410 fprintf(stderr, "selected --sort parent, but no callchain data\n");
1411 exit(-1);
1414 if (load_kernel() < 0) {
1415 perror("failed to load kernel symbols");
1416 return EXIT_FAILURE;
1419 if (!full_paths) {
1420 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
1421 perror("failed to get the current directory");
1422 return EXIT_FAILURE;
1424 cwdlen = strlen(cwd);
1425 } else {
1426 cwd = NULL;
1427 cwdlen = 0;
1429 remap:
1430 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1431 MAP_SHARED, input, offset);
1432 if (buf == MAP_FAILED) {
1433 perror("failed to mmap file");
1434 exit(-1);
1437 more:
1438 event = (event_t *)(buf + head);
1440 size = event->header.size;
1441 if (!size)
1442 size = 8;
1444 if (head + event->header.size >= page_size * mmap_window) {
1445 unsigned long shift = page_size * (head / page_size);
1446 int ret;
1448 ret = munmap(buf, page_size * mmap_window);
1449 assert(ret == 0);
1451 offset += shift;
1452 head -= shift;
1453 goto remap;
1456 size = event->header.size;
1458 dprintf("\n%p [%p]: event: %d\n",
1459 (void *)(offset + head),
1460 (void *)(long)event->header.size,
1461 event->header.type);
1463 if (!size || process_event(event, offset, head) < 0) {
1465 dprintf("%p [%p]: skipping unknown header type: %d\n",
1466 (void *)(offset + head),
1467 (void *)(long)(event->header.size),
1468 event->header.type);
1470 total_unknown++;
1473 * assume we lost track of the stream, check alignment, and
1474 * increment a single u64 in the hope to catch on again 'soon'.
1477 if (unlikely(head & 7))
1478 head &= ~7ULL;
1480 size = 8;
1483 head += size;
1485 if (offset + head >= sizeof(file_header) + file_header.data_size)
1486 goto done;
1488 if (offset + head < stat.st_size)
1489 goto more;
1491 done:
1492 rc = EXIT_SUCCESS;
1493 close(input);
1495 dprintf(" IP events: %10ld\n", total);
1496 dprintf(" mmap events: %10ld\n", total_mmap);
1497 dprintf(" comm events: %10ld\n", total_comm);
1498 dprintf(" fork events: %10ld\n", total_fork);
1499 dprintf(" lost events: %10ld\n", total_lost);
1500 dprintf(" unknown events: %10ld\n", total_unknown);
1502 if (dump_trace)
1503 return 0;
1505 if (verbose >= 3)
1506 threads__fprintf(stdout);
1508 if (verbose >= 2)
1509 dsos__fprintf(stdout);
1511 collapse__resort();
1512 output__resort();
1513 output__fprintf(stdout, total);
1515 return rc;
1518 static const char * const report_usage[] = {
1519 "perf report [<options>] <command>",
1520 NULL
1523 static const struct option options[] = {
1524 OPT_STRING('i', "input", &input_name, "file",
1525 "input file name"),
1526 OPT_BOOLEAN('v', "verbose", &verbose,
1527 "be more verbose (show symbol address, etc)"),
1528 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1529 "dump raw trace in ASCII"),
1530 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
1531 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1532 "sort by key(s): pid, comm, dso, symbol, parent"),
1533 OPT_BOOLEAN('P', "full-paths", &full_paths,
1534 "Don't shorten the pathnames taking into account the cwd"),
1535 OPT_STRING('p', "parent", &parent_pattern, "regex",
1536 "regex filter to identify parent, see: '--sort parent'"),
1537 OPT_BOOLEAN('x', "exclude-other", &exclude_other,
1538 "Only display entries with parent-match"),
1539 OPT_END()
1542 static void setup_sorting(void)
1544 char *tmp, *tok, *str = strdup(sort_order);
1546 for (tok = strtok_r(str, ", ", &tmp);
1547 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1548 if (sort_dimension__add(tok) < 0) {
1549 error("Unknown --sort key: `%s'", tok);
1550 usage_with_options(report_usage, options);
1554 free(str);
1557 int cmd_report(int argc, const char **argv, const char *prefix)
1559 symbol__init();
1561 page_size = getpagesize();
1563 argc = parse_options(argc, argv, options, report_usage, 0);
1565 setup_sorting();
1567 if (parent_pattern != default_parent_pattern)
1568 sort_dimension__add("parent");
1569 else
1570 exclude_other = 0;
1573 * Any (unrecognized) arguments left?
1575 if (argc)
1576 usage_with_options(report_usage, options);
1578 setup_pager();
1580 return __cmd_report();