perf_counter tools: Small frequency related fixes
[linux-2.6/verdex.git] / tools / perf / builtin-report.c
blob9a0e31e79e9df9e2291f565016a2c4c8de548c5a
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)
40 static int verbose;
41 static int full_paths;
43 static unsigned long page_size;
44 static unsigned long mmap_window = 32;
46 struct ip_event {
47 struct perf_event_header header;
48 __u64 ip;
49 __u32 pid, tid;
50 __u64 period;
53 struct mmap_event {
54 struct perf_event_header header;
55 __u32 pid, tid;
56 __u64 start;
57 __u64 len;
58 __u64 pgoff;
59 char filename[PATH_MAX];
62 struct comm_event {
63 struct perf_event_header header;
64 __u32 pid, tid;
65 char comm[16];
68 struct fork_event {
69 struct perf_event_header header;
70 __u32 pid, ppid;
73 struct period_event {
74 struct perf_event_header header;
75 __u64 time;
76 __u64 id;
77 __u64 sample_period;
80 typedef union event_union {
81 struct perf_event_header header;
82 struct ip_event ip;
83 struct mmap_event mmap;
84 struct comm_event comm;
85 struct fork_event fork;
86 struct period_event period;
87 } event_t;
89 static LIST_HEAD(dsos);
90 static struct dso *kernel_dso;
91 static struct dso *vdso;
93 static void dsos__add(struct dso *dso)
95 list_add_tail(&dso->node, &dsos);
98 static struct dso *dsos__find(const char *name)
100 struct dso *pos;
102 list_for_each_entry(pos, &dsos, node)
103 if (strcmp(pos->name, name) == 0)
104 return pos;
105 return NULL;
108 static struct dso *dsos__findnew(const char *name)
110 struct dso *dso = dsos__find(name);
111 int nr;
113 if (dso)
114 return dso;
116 dso = dso__new(name, 0);
117 if (!dso)
118 goto out_delete_dso;
120 nr = dso__load(dso, NULL, verbose);
121 if (nr < 0) {
122 if (verbose)
123 fprintf(stderr, "Failed to open: %s\n", name);
124 goto out_delete_dso;
126 if (!nr && verbose) {
127 fprintf(stderr,
128 "No symbols found in: %s, maybe install a debug package?\n",
129 name);
132 dsos__add(dso);
134 return dso;
136 out_delete_dso:
137 dso__delete(dso);
138 return NULL;
141 static void dsos__fprintf(FILE *fp)
143 struct dso *pos;
145 list_for_each_entry(pos, &dsos, node)
146 dso__fprintf(pos, fp);
149 static struct symbol *vdso__find_symbol(struct dso *dso, uint64_t ip)
151 return dso__find_symbol(kernel_dso, ip);
154 static int load_kernel(void)
156 int err;
158 kernel_dso = dso__new("[kernel]", 0);
159 if (!kernel_dso)
160 return -1;
162 err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose);
163 if (err) {
164 dso__delete(kernel_dso);
165 kernel_dso = NULL;
166 } else
167 dsos__add(kernel_dso);
169 vdso = dso__new("[vdso]", 0);
170 if (!vdso)
171 return -1;
173 vdso->find_symbol = vdso__find_symbol;
175 dsos__add(vdso);
177 return err;
180 static char __cwd[PATH_MAX];
181 static char *cwd = __cwd;
182 static int cwdlen;
184 static int strcommon(const char *pathname)
186 int n = 0;
188 while (pathname[n] == cwd[n] && n < cwdlen)
189 ++n;
191 return n;
194 struct map {
195 struct list_head node;
196 uint64_t start;
197 uint64_t end;
198 uint64_t pgoff;
199 uint64_t (*map_ip)(struct map *, uint64_t);
200 struct dso *dso;
203 static uint64_t map__map_ip(struct map *map, uint64_t ip)
205 return ip - map->start + map->pgoff;
208 static uint64_t vdso__map_ip(struct map *map, uint64_t ip)
210 return ip;
213 static inline int is_anon_memory(const char *filename)
215 return strcmp(filename, "//anon") == 0;
218 static struct map *map__new(struct mmap_event *event)
220 struct map *self = malloc(sizeof(*self));
222 if (self != NULL) {
223 const char *filename = event->filename;
224 char newfilename[PATH_MAX];
225 int anon;
227 if (cwd) {
228 int n = strcommon(filename);
230 if (n == cwdlen) {
231 snprintf(newfilename, sizeof(newfilename),
232 ".%s", filename + n);
233 filename = newfilename;
237 anon = is_anon_memory(filename);
239 if (anon) {
240 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
241 filename = newfilename;
244 self->start = event->start;
245 self->end = event->start + event->len;
246 self->pgoff = event->pgoff;
248 self->dso = dsos__findnew(filename);
249 if (self->dso == NULL)
250 goto out_delete;
252 if (self->dso == vdso || anon)
253 self->map_ip = vdso__map_ip;
254 else
255 self->map_ip = map__map_ip;
257 return self;
258 out_delete:
259 free(self);
260 return NULL;
263 static struct map *map__clone(struct map *self)
265 struct map *map = malloc(sizeof(*self));
267 if (!map)
268 return NULL;
270 memcpy(map, self, sizeof(*self));
272 return map;
275 static int map__overlap(struct map *l, struct map *r)
277 if (l->start > r->start) {
278 struct map *t = l;
279 l = r;
280 r = t;
283 if (l->end > r->start)
284 return 1;
286 return 0;
289 static size_t map__fprintf(struct map *self, FILE *fp)
291 return fprintf(fp, " %"PRIx64"-%"PRIx64" %"PRIx64" %s\n",
292 self->start, self->end, self->pgoff, self->dso->name);
296 struct thread {
297 struct rb_node rb_node;
298 struct list_head maps;
299 pid_t pid;
300 char *comm;
303 static struct thread *thread__new(pid_t pid)
305 struct thread *self = malloc(sizeof(*self));
307 if (self != NULL) {
308 self->pid = pid;
309 self->comm = malloc(32);
310 if (self->comm)
311 snprintf(self->comm, 32, ":%d", self->pid);
312 INIT_LIST_HEAD(&self->maps);
315 return self;
318 static int thread__set_comm(struct thread *self, const char *comm)
320 if (self->comm)
321 free(self->comm);
322 self->comm = strdup(comm);
323 return self->comm ? 0 : -ENOMEM;
326 static size_t thread__fprintf(struct thread *self, FILE *fp)
328 struct map *pos;
329 size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
331 list_for_each_entry(pos, &self->maps, node)
332 ret += map__fprintf(pos, fp);
334 return ret;
338 static struct rb_root threads;
339 static struct thread *last_match;
341 static struct thread *threads__findnew(pid_t pid)
343 struct rb_node **p = &threads.rb_node;
344 struct rb_node *parent = NULL;
345 struct thread *th;
348 * Font-end cache - PID lookups come in blocks,
349 * so most of the time we dont have to look up
350 * the full rbtree:
352 if (last_match && last_match->pid == pid)
353 return last_match;
355 while (*p != NULL) {
356 parent = *p;
357 th = rb_entry(parent, struct thread, rb_node);
359 if (th->pid == pid) {
360 last_match = th;
361 return th;
364 if (pid < th->pid)
365 p = &(*p)->rb_left;
366 else
367 p = &(*p)->rb_right;
370 th = thread__new(pid);
371 if (th != NULL) {
372 rb_link_node(&th->rb_node, parent, p);
373 rb_insert_color(&th->rb_node, &threads);
374 last_match = th;
377 return th;
380 static void thread__insert_map(struct thread *self, struct map *map)
382 struct map *pos, *tmp;
384 list_for_each_entry_safe(pos, tmp, &self->maps, node) {
385 if (map__overlap(pos, map)) {
386 list_del_init(&pos->node);
387 /* XXX leaks dsos */
388 free(pos);
392 list_add_tail(&map->node, &self->maps);
395 static int thread__fork(struct thread *self, struct thread *parent)
397 struct map *map;
399 if (self->comm)
400 free(self->comm);
401 self->comm = strdup(parent->comm);
402 if (!self->comm)
403 return -ENOMEM;
405 list_for_each_entry(map, &parent->maps, node) {
406 struct map *new = map__clone(map);
407 if (!new)
408 return -ENOMEM;
409 thread__insert_map(self, new);
412 return 0;
415 static struct map *thread__find_map(struct thread *self, uint64_t ip)
417 struct map *pos;
419 if (self == NULL)
420 return NULL;
422 list_for_each_entry(pos, &self->maps, node)
423 if (ip >= pos->start && ip <= pos->end)
424 return pos;
426 return NULL;
429 static size_t threads__fprintf(FILE *fp)
431 size_t ret = 0;
432 struct rb_node *nd;
434 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
435 struct thread *pos = rb_entry(nd, struct thread, rb_node);
437 ret += thread__fprintf(pos, fp);
440 return ret;
444 * histogram, sorted on item, collects counts
447 static struct rb_root hist;
449 struct hist_entry {
450 struct rb_node rb_node;
452 struct thread *thread;
453 struct map *map;
454 struct dso *dso;
455 struct symbol *sym;
456 uint64_t ip;
457 char level;
459 uint32_t count;
463 * configurable sorting bits
466 struct sort_entry {
467 struct list_head list;
469 char *header;
471 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
472 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
473 size_t (*print)(FILE *fp, struct hist_entry *);
476 /* --sort pid */
478 static int64_t
479 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
481 return right->thread->pid - left->thread->pid;
484 static size_t
485 sort__thread_print(FILE *fp, struct hist_entry *self)
487 return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
490 static struct sort_entry sort_thread = {
491 .header = " Command: Pid",
492 .cmp = sort__thread_cmp,
493 .print = sort__thread_print,
496 /* --sort comm */
498 static int64_t
499 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
501 return right->thread->pid - left->thread->pid;
504 static int64_t
505 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
507 char *comm_l = left->thread->comm;
508 char *comm_r = right->thread->comm;
510 if (!comm_l || !comm_r) {
511 if (!comm_l && !comm_r)
512 return 0;
513 else if (!comm_l)
514 return -1;
515 else
516 return 1;
519 return strcmp(comm_l, comm_r);
522 static size_t
523 sort__comm_print(FILE *fp, struct hist_entry *self)
525 return fprintf(fp, "%16s", self->thread->comm);
528 static struct sort_entry sort_comm = {
529 .header = " Command",
530 .cmp = sort__comm_cmp,
531 .collapse = sort__comm_collapse,
532 .print = sort__comm_print,
535 /* --sort dso */
537 static int64_t
538 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
540 struct dso *dso_l = left->dso;
541 struct dso *dso_r = right->dso;
543 if (!dso_l || !dso_r) {
544 if (!dso_l && !dso_r)
545 return 0;
546 else if (!dso_l)
547 return -1;
548 else
549 return 1;
552 return strcmp(dso_l->name, dso_r->name);
555 static size_t
556 sort__dso_print(FILE *fp, struct hist_entry *self)
558 if (self->dso)
559 return fprintf(fp, "%-25s", self->dso->name);
561 return fprintf(fp, "%016llx ", (__u64)self->ip);
564 static struct sort_entry sort_dso = {
565 .header = "Shared Object ",
566 .cmp = sort__dso_cmp,
567 .print = sort__dso_print,
570 /* --sort symbol */
572 static int64_t
573 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
575 uint64_t ip_l, ip_r;
577 if (left->sym == right->sym)
578 return 0;
580 ip_l = left->sym ? left->sym->start : left->ip;
581 ip_r = right->sym ? right->sym->start : right->ip;
583 return (int64_t)(ip_r - ip_l);
586 static size_t
587 sort__sym_print(FILE *fp, struct hist_entry *self)
589 size_t ret = 0;
591 if (verbose)
592 ret += fprintf(fp, "%#018llx ", (__u64)self->ip);
594 if (self->sym) {
595 ret += fprintf(fp, "[%c] %s",
596 self->dso == kernel_dso ? 'k' : '.', self->sym->name);
597 } else {
598 ret += fprintf(fp, "%#016llx", (__u64)self->ip);
601 return ret;
604 static struct sort_entry sort_sym = {
605 .header = "Symbol",
606 .cmp = sort__sym_cmp,
607 .print = sort__sym_print,
610 static int sort__need_collapse = 0;
612 struct sort_dimension {
613 char *name;
614 struct sort_entry *entry;
615 int taken;
618 static struct sort_dimension sort_dimensions[] = {
619 { .name = "pid", .entry = &sort_thread, },
620 { .name = "comm", .entry = &sort_comm, },
621 { .name = "dso", .entry = &sort_dso, },
622 { .name = "symbol", .entry = &sort_sym, },
625 static LIST_HEAD(hist_entry__sort_list);
627 static int sort_dimension__add(char *tok)
629 int i;
631 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
632 struct sort_dimension *sd = &sort_dimensions[i];
634 if (sd->taken)
635 continue;
637 if (strncasecmp(tok, sd->name, strlen(tok)))
638 continue;
640 if (sd->entry->collapse)
641 sort__need_collapse = 1;
643 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
644 sd->taken = 1;
646 return 0;
649 return -ESRCH;
652 static int64_t
653 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
655 struct sort_entry *se;
656 int64_t cmp = 0;
658 list_for_each_entry(se, &hist_entry__sort_list, list) {
659 cmp = se->cmp(left, right);
660 if (cmp)
661 break;
664 return cmp;
667 static int64_t
668 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
670 struct sort_entry *se;
671 int64_t cmp = 0;
673 list_for_each_entry(se, &hist_entry__sort_list, list) {
674 int64_t (*f)(struct hist_entry *, struct hist_entry *);
676 f = se->collapse ?: se->cmp;
678 cmp = f(left, right);
679 if (cmp)
680 break;
683 return cmp;
686 static size_t
687 hist_entry__fprintf(FILE *fp, struct hist_entry *self, uint64_t total_samples)
689 struct sort_entry *se;
690 size_t ret;
692 if (total_samples) {
693 double percent = self->count * 100.0 / total_samples;
694 char *color = PERF_COLOR_NORMAL;
697 * We color high-overhead entries in red, mid-overhead
698 * entries in green - and keep the low overhead places
699 * normal:
701 if (percent >= 5.0) {
702 color = PERF_COLOR_RED;
703 } else {
704 if (percent >= 0.5)
705 color = PERF_COLOR_GREEN;
708 ret = color_fprintf(fp, color, " %6.2f%%",
709 (self->count * 100.0) / total_samples);
710 } else
711 ret = fprintf(fp, "%12d ", self->count);
713 list_for_each_entry(se, &hist_entry__sort_list, list) {
714 fprintf(fp, " ");
715 ret += se->print(fp, self);
718 ret += fprintf(fp, "\n");
720 return ret;
724 * collect histogram counts
727 static int
728 hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
729 struct symbol *sym, uint64_t ip, char level)
731 struct rb_node **p = &hist.rb_node;
732 struct rb_node *parent = NULL;
733 struct hist_entry *he;
734 struct hist_entry entry = {
735 .thread = thread,
736 .map = map,
737 .dso = dso,
738 .sym = sym,
739 .ip = ip,
740 .level = level,
741 .count = 1,
743 int cmp;
745 while (*p != NULL) {
746 parent = *p;
747 he = rb_entry(parent, struct hist_entry, rb_node);
749 cmp = hist_entry__cmp(&entry, he);
751 if (!cmp) {
752 he->count++;
753 return 0;
756 if (cmp < 0)
757 p = &(*p)->rb_left;
758 else
759 p = &(*p)->rb_right;
762 he = malloc(sizeof(*he));
763 if (!he)
764 return -ENOMEM;
765 *he = entry;
766 rb_link_node(&he->rb_node, parent, p);
767 rb_insert_color(&he->rb_node, &hist);
769 return 0;
772 static void hist_entry__free(struct hist_entry *he)
774 free(he);
778 * collapse the histogram
781 static struct rb_root collapse_hists;
783 static void collapse__insert_entry(struct hist_entry *he)
785 struct rb_node **p = &collapse_hists.rb_node;
786 struct rb_node *parent = NULL;
787 struct hist_entry *iter;
788 int64_t cmp;
790 while (*p != NULL) {
791 parent = *p;
792 iter = rb_entry(parent, struct hist_entry, rb_node);
794 cmp = hist_entry__collapse(iter, he);
796 if (!cmp) {
797 iter->count += he->count;
798 hist_entry__free(he);
799 return;
802 if (cmp < 0)
803 p = &(*p)->rb_left;
804 else
805 p = &(*p)->rb_right;
808 rb_link_node(&he->rb_node, parent, p);
809 rb_insert_color(&he->rb_node, &collapse_hists);
812 static void collapse__resort(void)
814 struct rb_node *next;
815 struct hist_entry *n;
817 if (!sort__need_collapse)
818 return;
820 next = rb_first(&hist);
821 while (next) {
822 n = rb_entry(next, struct hist_entry, rb_node);
823 next = rb_next(&n->rb_node);
825 rb_erase(&n->rb_node, &hist);
826 collapse__insert_entry(n);
831 * reverse the map, sort on count.
834 static struct rb_root output_hists;
836 static void output__insert_entry(struct hist_entry *he)
838 struct rb_node **p = &output_hists.rb_node;
839 struct rb_node *parent = NULL;
840 struct hist_entry *iter;
842 while (*p != NULL) {
843 parent = *p;
844 iter = rb_entry(parent, struct hist_entry, rb_node);
846 if (he->count > iter->count)
847 p = &(*p)->rb_left;
848 else
849 p = &(*p)->rb_right;
852 rb_link_node(&he->rb_node, parent, p);
853 rb_insert_color(&he->rb_node, &output_hists);
856 static void output__resort(void)
858 struct rb_node *next;
859 struct hist_entry *n;
860 struct rb_root *tree = &hist;
862 if (sort__need_collapse)
863 tree = &collapse_hists;
865 next = rb_first(tree);
867 while (next) {
868 n = rb_entry(next, struct hist_entry, rb_node);
869 next = rb_next(&n->rb_node);
871 rb_erase(&n->rb_node, tree);
872 output__insert_entry(n);
876 static size_t output__fprintf(FILE *fp, uint64_t total_samples)
878 struct hist_entry *pos;
879 struct sort_entry *se;
880 struct rb_node *nd;
881 size_t ret = 0;
883 fprintf(fp, "\n");
884 fprintf(fp, "#\n");
885 fprintf(fp, "# (%Ld samples)\n", (__u64)total_samples);
886 fprintf(fp, "#\n");
888 fprintf(fp, "# Overhead");
889 list_for_each_entry(se, &hist_entry__sort_list, list)
890 fprintf(fp, " %s", se->header);
891 fprintf(fp, "\n");
893 fprintf(fp, "# ........");
894 list_for_each_entry(se, &hist_entry__sort_list, list) {
895 int i;
897 fprintf(fp, " ");
898 for (i = 0; i < strlen(se->header); i++)
899 fprintf(fp, ".");
901 fprintf(fp, "\n");
903 fprintf(fp, "#\n");
905 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
906 pos = rb_entry(nd, struct hist_entry, rb_node);
907 ret += hist_entry__fprintf(fp, pos, total_samples);
910 if (!strcmp(sort_order, default_sort_order)) {
911 fprintf(fp, "#\n");
912 fprintf(fp, "# (For more details, try: perf report --sort comm,dso,symbol)\n");
913 fprintf(fp, "#\n");
915 fprintf(fp, "\n");
917 return ret;
920 static void register_idle_thread(void)
922 struct thread *thread = threads__findnew(0);
924 if (thread == NULL ||
925 thread__set_comm(thread, "[idle]")) {
926 fprintf(stderr, "problem inserting idle task.\n");
927 exit(-1);
931 static unsigned long total = 0,
932 total_mmap = 0,
933 total_comm = 0,
934 total_fork = 0,
935 total_unknown = 0;
937 static int
938 process_overflow_event(event_t *event, unsigned long offset, unsigned long head)
940 char level;
941 int show = 0;
942 struct dso *dso = NULL;
943 struct thread *thread = threads__findnew(event->ip.pid);
944 uint64_t ip = event->ip.ip;
945 struct map *map = NULL;
947 dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p period: %Ld\n",
948 (void *)(offset + head),
949 (void *)(long)(event->header.size),
950 event->header.misc,
951 event->ip.pid,
952 (void *)(long)ip,
953 (long long)event->ip.period);
955 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
957 if (thread == NULL) {
958 fprintf(stderr, "problem processing %d event, skipping it.\n",
959 event->header.type);
960 return -1;
963 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
964 show = SHOW_KERNEL;
965 level = 'k';
967 dso = kernel_dso;
969 dprintf(" ...... dso: %s\n", dso->name);
971 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
973 show = SHOW_USER;
974 level = '.';
976 map = thread__find_map(thread, ip);
977 if (map != NULL) {
978 ip = map->map_ip(map, ip);
979 dso = map->dso;
980 } else {
982 * If this is outside of all known maps,
983 * and is a negative address, try to look it
984 * up in the kernel dso, as it might be a
985 * vsyscall (which executes in user-mode):
987 if ((long long)ip < 0)
988 dso = kernel_dso;
990 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
992 } else {
993 show = SHOW_HV;
994 level = 'H';
995 dprintf(" ...... dso: [hypervisor]\n");
998 if (show & show_mask) {
999 struct symbol *sym = NULL;
1001 if (dso)
1002 sym = dso->find_symbol(dso, ip);
1004 if (hist_entry__add(thread, map, dso, sym, ip, level)) {
1005 fprintf(stderr,
1006 "problem incrementing symbol count, skipping event\n");
1007 return -1;
1010 total++;
1012 return 0;
1015 static int
1016 process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
1018 struct thread *thread = threads__findnew(event->mmap.pid);
1019 struct map *map = map__new(&event->mmap);
1021 dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
1022 (void *)(offset + head),
1023 (void *)(long)(event->header.size),
1024 event->mmap.pid,
1025 (void *)(long)event->mmap.start,
1026 (void *)(long)event->mmap.len,
1027 (void *)(long)event->mmap.pgoff,
1028 event->mmap.filename);
1030 if (thread == NULL || map == NULL) {
1031 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
1032 return 0;
1035 thread__insert_map(thread, map);
1036 total_mmap++;
1038 return 0;
1041 static int
1042 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
1044 struct thread *thread = threads__findnew(event->comm.pid);
1046 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
1047 (void *)(offset + head),
1048 (void *)(long)(event->header.size),
1049 event->comm.comm, event->comm.pid);
1051 if (thread == NULL ||
1052 thread__set_comm(thread, event->comm.comm)) {
1053 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
1054 return -1;
1056 total_comm++;
1058 return 0;
1061 static int
1062 process_fork_event(event_t *event, unsigned long offset, unsigned long head)
1064 struct thread *thread = threads__findnew(event->fork.pid);
1065 struct thread *parent = threads__findnew(event->fork.ppid);
1067 dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
1068 (void *)(offset + head),
1069 (void *)(long)(event->header.size),
1070 event->fork.pid, event->fork.ppid);
1072 if (!thread || !parent || thread__fork(thread, parent)) {
1073 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
1074 return -1;
1076 total_fork++;
1078 return 0;
1081 static int
1082 process_period_event(event_t *event, unsigned long offset, unsigned long head)
1084 dprintf("%p [%p]: PERF_EVENT_PERIOD: time:%Ld, id:%Ld: period:%Ld\n",
1085 (void *)(offset + head),
1086 (void *)(long)(event->header.size),
1087 event->period.time,
1088 event->period.id,
1089 event->period.sample_period);
1091 return 0;
1094 static int
1095 process_event(event_t *event, unsigned long offset, unsigned long head)
1097 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW)
1098 return process_overflow_event(event, offset, head);
1100 switch (event->header.type) {
1101 case PERF_EVENT_MMAP:
1102 return process_mmap_event(event, offset, head);
1104 case PERF_EVENT_COMM:
1105 return process_comm_event(event, offset, head);
1107 case PERF_EVENT_FORK:
1108 return process_fork_event(event, offset, head);
1110 case PERF_EVENT_PERIOD:
1111 return process_period_event(event, offset, head);
1113 * We dont process them right now but they are fine:
1116 case PERF_EVENT_THROTTLE:
1117 case PERF_EVENT_UNTHROTTLE:
1118 return 0;
1120 default:
1121 return -1;
1124 return 0;
1127 static int __cmd_report(void)
1129 int ret, rc = EXIT_FAILURE;
1130 unsigned long offset = 0;
1131 unsigned long head = 0;
1132 struct stat stat;
1133 event_t *event;
1134 uint32_t size;
1135 char *buf;
1137 register_idle_thread();
1139 input = open(input_name, O_RDONLY);
1140 if (input < 0) {
1141 fprintf(stderr, " failed to open file: %s", input_name);
1142 if (!strcmp(input_name, "perf.data"))
1143 fprintf(stderr, " (try 'perf record' first)");
1144 fprintf(stderr, "\n");
1145 exit(-1);
1148 ret = fstat(input, &stat);
1149 if (ret < 0) {
1150 perror("failed to stat file");
1151 exit(-1);
1154 if (!stat.st_size) {
1155 fprintf(stderr, "zero-sized file, nothing to do!\n");
1156 exit(0);
1159 if (load_kernel() < 0) {
1160 perror("failed to load kernel symbols");
1161 return EXIT_FAILURE;
1164 if (!full_paths) {
1165 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
1166 perror("failed to get the current directory");
1167 return EXIT_FAILURE;
1169 cwdlen = strlen(cwd);
1170 } else {
1171 cwd = NULL;
1172 cwdlen = 0;
1174 remap:
1175 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1176 MAP_SHARED, input, offset);
1177 if (buf == MAP_FAILED) {
1178 perror("failed to mmap file");
1179 exit(-1);
1182 more:
1183 event = (event_t *)(buf + head);
1185 size = event->header.size;
1186 if (!size)
1187 size = 8;
1189 if (head + event->header.size >= page_size * mmap_window) {
1190 unsigned long shift = page_size * (head / page_size);
1191 int ret;
1193 ret = munmap(buf, page_size * mmap_window);
1194 assert(ret == 0);
1196 offset += shift;
1197 head -= shift;
1198 goto remap;
1201 size = event->header.size;
1203 dprintf("%p [%p]: event: %d\n",
1204 (void *)(offset + head),
1205 (void *)(long)event->header.size,
1206 event->header.type);
1208 if (!size || process_event(event, offset, head) < 0) {
1210 dprintf("%p [%p]: skipping unknown header type: %d\n",
1211 (void *)(offset + head),
1212 (void *)(long)(event->header.size),
1213 event->header.type);
1215 total_unknown++;
1218 * assume we lost track of the stream, check alignment, and
1219 * increment a single u64 in the hope to catch on again 'soon'.
1222 if (unlikely(head & 7))
1223 head &= ~7ULL;
1225 size = 8;
1228 head += size;
1230 if (offset + head < stat.st_size)
1231 goto more;
1233 rc = EXIT_SUCCESS;
1234 close(input);
1236 dprintf(" IP events: %10ld\n", total);
1237 dprintf(" mmap events: %10ld\n", total_mmap);
1238 dprintf(" comm events: %10ld\n", total_comm);
1239 dprintf(" fork events: %10ld\n", total_fork);
1240 dprintf(" unknown events: %10ld\n", total_unknown);
1242 if (dump_trace)
1243 return 0;
1245 if (verbose >= 3)
1246 threads__fprintf(stdout);
1248 if (verbose >= 2)
1249 dsos__fprintf(stdout);
1251 collapse__resort();
1252 output__resort();
1253 output__fprintf(stdout, total);
1255 return rc;
1258 static const char * const report_usage[] = {
1259 "perf report [<options>] <command>",
1260 NULL
1263 static const struct option options[] = {
1264 OPT_STRING('i', "input", &input_name, "file",
1265 "input file name"),
1266 OPT_BOOLEAN('v', "verbose", &verbose,
1267 "be more verbose (show symbol address, etc)"),
1268 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1269 "dump raw trace in ASCII"),
1270 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
1271 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1272 "sort by key(s): pid, comm, dso, symbol. Default: pid,symbol"),
1273 OPT_BOOLEAN('P', "full-paths", &full_paths,
1274 "Don't shorten the pathnames taking into account the cwd"),
1275 OPT_END()
1278 static void setup_sorting(void)
1280 char *tmp, *tok, *str = strdup(sort_order);
1282 for (tok = strtok_r(str, ", ", &tmp);
1283 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1284 if (sort_dimension__add(tok) < 0) {
1285 error("Unknown --sort key: `%s'", tok);
1286 usage_with_options(report_usage, options);
1290 free(str);
1293 int cmd_report(int argc, const char **argv, const char *prefix)
1295 symbol__init();
1297 page_size = getpagesize();
1299 argc = parse_options(argc, argv, options, report_usage, 0);
1301 setup_sorting();
1304 * Any (unrecognized) arguments left?
1306 if (argc)
1307 usage_with_options(report_usage, options);
1309 setup_pager();
1311 return __cmd_report();