perf report: Show the tid too in -D
[linux-2.6/libata-dev.git] / tools / perf / builtin-report.c
blob23e1457f2409dec51d70017ab7729df252104f82
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 <linux/list.h>
14 #include "util/cache.h"
15 #include <linux/rbtree.h>
16 #include "util/symbol.h"
17 #include "util/string.h"
18 #include "util/callchain.h"
19 #include "util/strlist.h"
21 #include "perf.h"
22 #include "util/header.h"
24 #include "util/parse-options.h"
25 #include "util/parse-events.h"
27 #define SHOW_KERNEL 1
28 #define SHOW_USER 2
29 #define SHOW_HV 4
31 static char const *input_name = "perf.data";
32 static char *vmlinux = NULL;
34 static char default_sort_order[] = "comm,dso,symbol";
35 static char *sort_order = default_sort_order;
36 static char *dso_list_str, *comm_list_str, *sym_list_str,
37 *col_width_list_str;
38 static struct strlist *dso_list, *comm_list, *sym_list;
39 static char *field_sep;
41 static int input;
42 static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
44 static int dump_trace = 0;
45 #define dprintf(x...) do { if (dump_trace) printf(x); } while (0)
46 #define cdprintf(x...) do { if (dump_trace) color_fprintf(stdout, color, x); } while (0)
48 static int verbose;
49 #define eprintf(x...) do { if (verbose) fprintf(stderr, x); } while (0)
51 static int modules;
53 static int full_paths;
54 static int show_nr_samples;
56 static unsigned long page_size;
57 static unsigned long mmap_window = 32;
59 static char default_parent_pattern[] = "^sys_|^do_page_fault";
60 static char *parent_pattern = default_parent_pattern;
61 static regex_t parent_regex;
63 static int exclude_other = 1;
65 static char callchain_default_opt[] = "fractal,0.5";
67 static int callchain;
69 static
70 struct callchain_param callchain_param = {
71 .mode = CHAIN_GRAPH_REL,
72 .min_percent = 0.5
75 static u64 sample_type;
77 struct ip_event {
78 struct perf_event_header header;
79 u64 ip;
80 u32 pid, tid;
81 unsigned char __more_data[];
84 struct mmap_event {
85 struct perf_event_header header;
86 u32 pid, tid;
87 u64 start;
88 u64 len;
89 u64 pgoff;
90 char filename[PATH_MAX];
93 struct comm_event {
94 struct perf_event_header header;
95 u32 pid, tid;
96 char comm[16];
99 struct fork_event {
100 struct perf_event_header header;
101 u32 pid, ppid;
102 u32 tid, ptid;
105 struct lost_event {
106 struct perf_event_header header;
107 u64 id;
108 u64 lost;
111 struct read_event {
112 struct perf_event_header header;
113 u32 pid,tid;
114 u64 value;
115 u64 time_enabled;
116 u64 time_running;
117 u64 id;
120 typedef union event_union {
121 struct perf_event_header header;
122 struct ip_event ip;
123 struct mmap_event mmap;
124 struct comm_event comm;
125 struct fork_event fork;
126 struct lost_event lost;
127 struct read_event read;
128 } event_t;
130 static int repsep_fprintf(FILE *fp, const char *fmt, ...)
132 int n;
133 va_list ap;
135 va_start(ap, fmt);
136 if (!field_sep)
137 n = vfprintf(fp, fmt, ap);
138 else {
139 char *bf = NULL;
140 n = vasprintf(&bf, fmt, ap);
141 if (n > 0) {
142 char *sep = bf;
143 while (1) {
144 sep = strchr(sep, *field_sep);
145 if (sep == NULL)
146 break;
147 *sep = '.';
150 fputs(bf, fp);
151 free(bf);
153 va_end(ap);
154 return n;
157 static LIST_HEAD(dsos);
158 static struct dso *kernel_dso;
159 static struct dso *vdso;
160 static struct dso *hypervisor_dso;
162 static void dsos__add(struct dso *dso)
164 list_add_tail(&dso->node, &dsos);
167 static struct dso *dsos__find(const char *name)
169 struct dso *pos;
171 list_for_each_entry(pos, &dsos, node)
172 if (strcmp(pos->name, name) == 0)
173 return pos;
174 return NULL;
177 static struct dso *dsos__findnew(const char *name)
179 struct dso *dso = dsos__find(name);
180 int nr;
182 if (dso)
183 return dso;
185 dso = dso__new(name, 0);
186 if (!dso)
187 goto out_delete_dso;
189 nr = dso__load(dso, NULL, verbose);
190 if (nr < 0) {
191 eprintf("Failed to open: %s\n", name);
192 goto out_delete_dso;
194 if (!nr)
195 eprintf("No symbols found in: %s, maybe install a debug package?\n", name);
197 dsos__add(dso);
199 return dso;
201 out_delete_dso:
202 dso__delete(dso);
203 return NULL;
206 static void dsos__fprintf(FILE *fp)
208 struct dso *pos;
210 list_for_each_entry(pos, &dsos, node)
211 dso__fprintf(pos, fp);
214 static struct symbol *vdso__find_symbol(struct dso *dso, u64 ip)
216 return dso__find_symbol(dso, ip);
219 static int load_kernel(void)
221 int err;
223 kernel_dso = dso__new("[kernel]", 0);
224 if (!kernel_dso)
225 return -1;
227 err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose, modules);
228 if (err <= 0) {
229 dso__delete(kernel_dso);
230 kernel_dso = NULL;
231 } else
232 dsos__add(kernel_dso);
234 vdso = dso__new("[vdso]", 0);
235 if (!vdso)
236 return -1;
238 vdso->find_symbol = vdso__find_symbol;
240 dsos__add(vdso);
242 hypervisor_dso = dso__new("[hypervisor]", 0);
243 if (!hypervisor_dso)
244 return -1;
245 dsos__add(hypervisor_dso);
247 return err;
250 static char __cwd[PATH_MAX];
251 static char *cwd = __cwd;
252 static int cwdlen;
254 static int strcommon(const char *pathname)
256 int n = 0;
258 while (n < cwdlen && pathname[n] == cwd[n])
259 ++n;
261 return n;
264 struct map {
265 struct list_head node;
266 u64 start;
267 u64 end;
268 u64 pgoff;
269 u64 (*map_ip)(struct map *, u64);
270 struct dso *dso;
273 static u64 map__map_ip(struct map *map, u64 ip)
275 return ip - map->start + map->pgoff;
278 static u64 vdso__map_ip(struct map *map __used, u64 ip)
280 return ip;
283 static inline int is_anon_memory(const char *filename)
285 return strcmp(filename, "//anon") == 0;
288 static struct map *map__new(struct mmap_event *event)
290 struct map *self = malloc(sizeof(*self));
292 if (self != NULL) {
293 const char *filename = event->filename;
294 char newfilename[PATH_MAX];
295 int anon;
297 if (cwd) {
298 int n = strcommon(filename);
300 if (n == cwdlen) {
301 snprintf(newfilename, sizeof(newfilename),
302 ".%s", filename + n);
303 filename = newfilename;
307 anon = is_anon_memory(filename);
309 if (anon) {
310 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
311 filename = newfilename;
314 self->start = event->start;
315 self->end = event->start + event->len;
316 self->pgoff = event->pgoff;
318 self->dso = dsos__findnew(filename);
319 if (self->dso == NULL)
320 goto out_delete;
322 if (self->dso == vdso || anon)
323 self->map_ip = vdso__map_ip;
324 else
325 self->map_ip = map__map_ip;
327 return self;
328 out_delete:
329 free(self);
330 return NULL;
333 static struct map *map__clone(struct map *self)
335 struct map *map = malloc(sizeof(*self));
337 if (!map)
338 return NULL;
340 memcpy(map, self, sizeof(*self));
342 return map;
345 static int map__overlap(struct map *l, struct map *r)
347 if (l->start > r->start) {
348 struct map *t = l;
349 l = r;
350 r = t;
353 if (l->end > r->start)
354 return 1;
356 return 0;
359 static size_t map__fprintf(struct map *self, FILE *fp)
361 return fprintf(fp, " %Lx-%Lx %Lx %s\n",
362 self->start, self->end, self->pgoff, self->dso->name);
366 struct thread {
367 struct rb_node rb_node;
368 struct list_head maps;
369 pid_t pid;
370 char *comm;
373 static struct thread *thread__new(pid_t pid)
375 struct thread *self = malloc(sizeof(*self));
377 if (self != NULL) {
378 self->pid = pid;
379 self->comm = malloc(32);
380 if (self->comm)
381 snprintf(self->comm, 32, ":%d", self->pid);
382 INIT_LIST_HEAD(&self->maps);
385 return self;
388 static unsigned int dsos__col_width,
389 comms__col_width,
390 threads__col_width;
392 static int thread__set_comm(struct thread *self, const char *comm)
394 if (self->comm)
395 free(self->comm);
396 self->comm = strdup(comm);
397 if (!self->comm)
398 return -ENOMEM;
400 if (!col_width_list_str && !field_sep &&
401 (!comm_list || strlist__has_entry(comm_list, comm))) {
402 unsigned int slen = strlen(comm);
403 if (slen > comms__col_width) {
404 comms__col_width = slen;
405 threads__col_width = slen + 6;
409 return 0;
412 static size_t thread__fprintf(struct thread *self, FILE *fp)
414 struct map *pos;
415 size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
417 list_for_each_entry(pos, &self->maps, node)
418 ret += map__fprintf(pos, fp);
420 return ret;
424 static struct rb_root threads;
425 static struct thread *last_match;
427 static struct thread *threads__findnew(pid_t pid)
429 struct rb_node **p = &threads.rb_node;
430 struct rb_node *parent = NULL;
431 struct thread *th;
434 * Font-end cache - PID lookups come in blocks,
435 * so most of the time we dont have to look up
436 * the full rbtree:
438 if (last_match && last_match->pid == pid)
439 return last_match;
441 while (*p != NULL) {
442 parent = *p;
443 th = rb_entry(parent, struct thread, rb_node);
445 if (th->pid == pid) {
446 last_match = th;
447 return th;
450 if (pid < th->pid)
451 p = &(*p)->rb_left;
452 else
453 p = &(*p)->rb_right;
456 th = thread__new(pid);
457 if (th != NULL) {
458 rb_link_node(&th->rb_node, parent, p);
459 rb_insert_color(&th->rb_node, &threads);
460 last_match = th;
463 return th;
466 static void thread__insert_map(struct thread *self, struct map *map)
468 struct map *pos, *tmp;
470 list_for_each_entry_safe(pos, tmp, &self->maps, node) {
471 if (map__overlap(pos, map)) {
472 if (verbose >= 2) {
473 printf("overlapping maps:\n");
474 map__fprintf(map, stdout);
475 map__fprintf(pos, stdout);
478 if (map->start <= pos->start && map->end > pos->start)
479 pos->start = map->end;
481 if (map->end >= pos->end && map->start < pos->end)
482 pos->end = map->start;
484 if (verbose >= 2) {
485 printf("after collision:\n");
486 map__fprintf(pos, stdout);
489 if (pos->start >= pos->end) {
490 list_del_init(&pos->node);
491 free(pos);
496 list_add_tail(&map->node, &self->maps);
499 static int thread__fork(struct thread *self, struct thread *parent)
501 struct map *map;
503 if (self->comm)
504 free(self->comm);
505 self->comm = strdup(parent->comm);
506 if (!self->comm)
507 return -ENOMEM;
509 list_for_each_entry(map, &parent->maps, node) {
510 struct map *new = map__clone(map);
511 if (!new)
512 return -ENOMEM;
513 thread__insert_map(self, new);
516 return 0;
519 static struct map *thread__find_map(struct thread *self, u64 ip)
521 struct map *pos;
523 if (self == NULL)
524 return NULL;
526 list_for_each_entry(pos, &self->maps, node)
527 if (ip >= pos->start && ip <= pos->end)
528 return pos;
530 return NULL;
533 static size_t threads__fprintf(FILE *fp)
535 size_t ret = 0;
536 struct rb_node *nd;
538 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
539 struct thread *pos = rb_entry(nd, struct thread, rb_node);
541 ret += thread__fprintf(pos, fp);
544 return ret;
548 * histogram, sorted on item, collects counts
551 static struct rb_root hist;
553 struct hist_entry {
554 struct rb_node rb_node;
556 struct thread *thread;
557 struct map *map;
558 struct dso *dso;
559 struct symbol *sym;
560 struct symbol *parent;
561 u64 ip;
562 char level;
563 struct callchain_node callchain;
564 struct rb_root sorted_chain;
566 u64 count;
570 * configurable sorting bits
573 struct sort_entry {
574 struct list_head list;
576 char *header;
578 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
579 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
580 size_t (*print)(FILE *fp, struct hist_entry *, unsigned int width);
581 unsigned int *width;
582 bool elide;
585 static int64_t cmp_null(void *l, void *r)
587 if (!l && !r)
588 return 0;
589 else if (!l)
590 return -1;
591 else
592 return 1;
595 /* --sort pid */
597 static int64_t
598 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
600 return right->thread->pid - left->thread->pid;
603 static size_t
604 sort__thread_print(FILE *fp, struct hist_entry *self, unsigned int width)
606 return repsep_fprintf(fp, "%*s:%5d", width - 6,
607 self->thread->comm ?: "", self->thread->pid);
610 static struct sort_entry sort_thread = {
611 .header = "Command: Pid",
612 .cmp = sort__thread_cmp,
613 .print = sort__thread_print,
614 .width = &threads__col_width,
617 /* --sort comm */
619 static int64_t
620 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
622 return right->thread->pid - left->thread->pid;
625 static int64_t
626 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
628 char *comm_l = left->thread->comm;
629 char *comm_r = right->thread->comm;
631 if (!comm_l || !comm_r)
632 return cmp_null(comm_l, comm_r);
634 return strcmp(comm_l, comm_r);
637 static size_t
638 sort__comm_print(FILE *fp, struct hist_entry *self, unsigned int width)
640 return repsep_fprintf(fp, "%*s", width, self->thread->comm);
643 static struct sort_entry sort_comm = {
644 .header = "Command",
645 .cmp = sort__comm_cmp,
646 .collapse = sort__comm_collapse,
647 .print = sort__comm_print,
648 .width = &comms__col_width,
651 /* --sort dso */
653 static int64_t
654 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
656 struct dso *dso_l = left->dso;
657 struct dso *dso_r = right->dso;
659 if (!dso_l || !dso_r)
660 return cmp_null(dso_l, dso_r);
662 return strcmp(dso_l->name, dso_r->name);
665 static size_t
666 sort__dso_print(FILE *fp, struct hist_entry *self, unsigned int width)
668 if (self->dso)
669 return repsep_fprintf(fp, "%-*s", width, self->dso->name);
671 return repsep_fprintf(fp, "%*llx", width, (u64)self->ip);
674 static struct sort_entry sort_dso = {
675 .header = "Shared Object",
676 .cmp = sort__dso_cmp,
677 .print = sort__dso_print,
678 .width = &dsos__col_width,
681 /* --sort symbol */
683 static int64_t
684 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
686 u64 ip_l, ip_r;
688 if (left->sym == right->sym)
689 return 0;
691 ip_l = left->sym ? left->sym->start : left->ip;
692 ip_r = right->sym ? right->sym->start : right->ip;
694 return (int64_t)(ip_r - ip_l);
697 static size_t
698 sort__sym_print(FILE *fp, struct hist_entry *self, unsigned int width __used)
700 size_t ret = 0;
702 if (verbose)
703 ret += repsep_fprintf(fp, "%#018llx %c ", (u64)self->ip,
704 dso__symtab_origin(self->dso));
706 ret += repsep_fprintf(fp, "[%c] ", self->level);
707 if (self->sym) {
708 ret += repsep_fprintf(fp, "%s", self->sym->name);
710 if (self->sym->module)
711 ret += repsep_fprintf(fp, "\t[%s]",
712 self->sym->module->name);
713 } else {
714 ret += repsep_fprintf(fp, "%#016llx", (u64)self->ip);
717 return ret;
720 static struct sort_entry sort_sym = {
721 .header = "Symbol",
722 .cmp = sort__sym_cmp,
723 .print = sort__sym_print,
726 /* --sort parent */
728 static int64_t
729 sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
731 struct symbol *sym_l = left->parent;
732 struct symbol *sym_r = right->parent;
734 if (!sym_l || !sym_r)
735 return cmp_null(sym_l, sym_r);
737 return strcmp(sym_l->name, sym_r->name);
740 static size_t
741 sort__parent_print(FILE *fp, struct hist_entry *self, unsigned int width)
743 return repsep_fprintf(fp, "%-*s", width,
744 self->parent ? self->parent->name : "[other]");
747 static unsigned int parent_symbol__col_width;
749 static struct sort_entry sort_parent = {
750 .header = "Parent symbol",
751 .cmp = sort__parent_cmp,
752 .print = sort__parent_print,
753 .width = &parent_symbol__col_width,
756 static int sort__need_collapse = 0;
757 static int sort__has_parent = 0;
759 struct sort_dimension {
760 char *name;
761 struct sort_entry *entry;
762 int taken;
765 static struct sort_dimension sort_dimensions[] = {
766 { .name = "pid", .entry = &sort_thread, },
767 { .name = "comm", .entry = &sort_comm, },
768 { .name = "dso", .entry = &sort_dso, },
769 { .name = "symbol", .entry = &sort_sym, },
770 { .name = "parent", .entry = &sort_parent, },
773 static LIST_HEAD(hist_entry__sort_list);
775 static int sort_dimension__add(char *tok)
777 unsigned int i;
779 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
780 struct sort_dimension *sd = &sort_dimensions[i];
782 if (sd->taken)
783 continue;
785 if (strncasecmp(tok, sd->name, strlen(tok)))
786 continue;
788 if (sd->entry->collapse)
789 sort__need_collapse = 1;
791 if (sd->entry == &sort_parent) {
792 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
793 if (ret) {
794 char err[BUFSIZ];
796 regerror(ret, &parent_regex, err, sizeof(err));
797 fprintf(stderr, "Invalid regex: %s\n%s",
798 parent_pattern, err);
799 exit(-1);
801 sort__has_parent = 1;
804 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
805 sd->taken = 1;
807 return 0;
810 return -ESRCH;
813 static int64_t
814 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
816 struct sort_entry *se;
817 int64_t cmp = 0;
819 list_for_each_entry(se, &hist_entry__sort_list, list) {
820 cmp = se->cmp(left, right);
821 if (cmp)
822 break;
825 return cmp;
828 static int64_t
829 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
831 struct sort_entry *se;
832 int64_t cmp = 0;
834 list_for_each_entry(se, &hist_entry__sort_list, list) {
835 int64_t (*f)(struct hist_entry *, struct hist_entry *);
837 f = se->collapse ?: se->cmp;
839 cmp = f(left, right);
840 if (cmp)
841 break;
844 return cmp;
847 static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask)
849 int i;
850 size_t ret = 0;
852 ret += fprintf(fp, "%s", " ");
854 for (i = 0; i < depth; i++)
855 if (depth_mask & (1 << i))
856 ret += fprintf(fp, "| ");
857 else
858 ret += fprintf(fp, " ");
860 ret += fprintf(fp, "\n");
862 return ret;
864 static size_t
865 ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, int depth,
866 int depth_mask, int count, u64 total_samples,
867 int hits)
869 int i;
870 size_t ret = 0;
872 ret += fprintf(fp, "%s", " ");
873 for (i = 0; i < depth; i++) {
874 if (depth_mask & (1 << i))
875 ret += fprintf(fp, "|");
876 else
877 ret += fprintf(fp, " ");
878 if (!count && i == depth - 1) {
879 double percent;
881 percent = hits * 100.0 / total_samples;
882 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
883 } else
884 ret += fprintf(fp, "%s", " ");
886 if (chain->sym)
887 ret += fprintf(fp, "%s\n", chain->sym->name);
888 else
889 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
891 return ret;
894 static struct symbol *rem_sq_bracket;
895 static struct callchain_list rem_hits;
897 static void init_rem_hits(void)
899 rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
900 if (!rem_sq_bracket) {
901 fprintf(stderr, "Not enough memory to display remaining hits\n");
902 return;
905 strcpy(rem_sq_bracket->name, "[...]");
906 rem_hits.sym = rem_sq_bracket;
909 static size_t
910 callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
911 u64 total_samples, int depth, int depth_mask)
913 struct rb_node *node, *next;
914 struct callchain_node *child;
915 struct callchain_list *chain;
916 int new_depth_mask = depth_mask;
917 u64 new_total;
918 u64 remaining;
919 size_t ret = 0;
920 int i;
922 if (callchain_param.mode == CHAIN_GRAPH_REL)
923 new_total = self->children_hit;
924 else
925 new_total = total_samples;
927 remaining = new_total;
929 node = rb_first(&self->rb_root);
930 while (node) {
931 u64 cumul;
933 child = rb_entry(node, struct callchain_node, rb_node);
934 cumul = cumul_hits(child);
935 remaining -= cumul;
938 * The depth mask manages the output of pipes that show
939 * the depth. We don't want to keep the pipes of the current
940 * level for the last child of this depth.
941 * Except if we have remaining filtered hits. They will
942 * supersede the last child
944 next = rb_next(node);
945 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
946 new_depth_mask &= ~(1 << (depth - 1));
949 * But we keep the older depth mask for the line seperator
950 * to keep the level link until we reach the last child
952 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask);
953 i = 0;
954 list_for_each_entry(chain, &child->val, list) {
955 if (chain->ip >= PERF_CONTEXT_MAX)
956 continue;
957 ret += ipchain__fprintf_graph(fp, chain, depth,
958 new_depth_mask, i++,
959 new_total,
960 cumul);
962 ret += callchain__fprintf_graph(fp, child, new_total,
963 depth + 1,
964 new_depth_mask | (1 << depth));
965 node = next;
968 if (callchain_param.mode == CHAIN_GRAPH_REL &&
969 remaining && remaining != new_total) {
971 if (!rem_sq_bracket)
972 return ret;
974 new_depth_mask &= ~(1 << (depth - 1));
976 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
977 new_depth_mask, 0, new_total,
978 remaining);
981 return ret;
984 static size_t
985 callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
986 u64 total_samples)
988 struct callchain_list *chain;
989 size_t ret = 0;
991 if (!self)
992 return 0;
994 ret += callchain__fprintf_flat(fp, self->parent, total_samples);
997 list_for_each_entry(chain, &self->val, list) {
998 if (chain->ip >= PERF_CONTEXT_MAX)
999 continue;
1000 if (chain->sym)
1001 ret += fprintf(fp, " %s\n", chain->sym->name);
1002 else
1003 ret += fprintf(fp, " %p\n",
1004 (void *)(long)chain->ip);
1007 return ret;
1010 static size_t
1011 hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
1012 u64 total_samples)
1014 struct rb_node *rb_node;
1015 struct callchain_node *chain;
1016 size_t ret = 0;
1018 rb_node = rb_first(&self->sorted_chain);
1019 while (rb_node) {
1020 double percent;
1022 chain = rb_entry(rb_node, struct callchain_node, rb_node);
1023 percent = chain->hit * 100.0 / total_samples;
1024 switch (callchain_param.mode) {
1025 case CHAIN_FLAT:
1026 ret += percent_color_fprintf(fp, " %6.2f%%\n",
1027 percent);
1028 ret += callchain__fprintf_flat(fp, chain, total_samples);
1029 break;
1030 case CHAIN_GRAPH_ABS: /* Falldown */
1031 case CHAIN_GRAPH_REL:
1032 ret += callchain__fprintf_graph(fp, chain,
1033 total_samples, 1, 1);
1034 default:
1035 break;
1037 ret += fprintf(fp, "\n");
1038 rb_node = rb_next(rb_node);
1041 return ret;
1045 static size_t
1046 hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
1048 struct sort_entry *se;
1049 size_t ret;
1051 if (exclude_other && !self->parent)
1052 return 0;
1054 if (total_samples)
1055 ret = percent_color_fprintf(fp,
1056 field_sep ? "%.2f" : " %6.2f%%",
1057 (self->count * 100.0) / total_samples);
1058 else
1059 ret = fprintf(fp, field_sep ? "%lld" : "%12lld ", self->count);
1061 if (show_nr_samples) {
1062 if (field_sep)
1063 fprintf(fp, "%c%lld", *field_sep, self->count);
1064 else
1065 fprintf(fp, "%11lld", self->count);
1068 list_for_each_entry(se, &hist_entry__sort_list, list) {
1069 if (se->elide)
1070 continue;
1072 fprintf(fp, "%s", field_sep ?: " ");
1073 ret += se->print(fp, self, se->width ? *se->width : 0);
1076 ret += fprintf(fp, "\n");
1078 if (callchain)
1079 hist_entry_callchain__fprintf(fp, self, total_samples);
1081 return ret;
1088 static void dso__calc_col_width(struct dso *self)
1090 if (!col_width_list_str && !field_sep &&
1091 (!dso_list || strlist__has_entry(dso_list, self->name))) {
1092 unsigned int slen = strlen(self->name);
1093 if (slen > dsos__col_width)
1094 dsos__col_width = slen;
1097 self->slen_calculated = 1;
1100 static struct symbol *
1101 resolve_symbol(struct thread *thread, struct map **mapp,
1102 struct dso **dsop, u64 *ipp)
1104 struct dso *dso = dsop ? *dsop : NULL;
1105 struct map *map = mapp ? *mapp : NULL;
1106 u64 ip = *ipp;
1108 if (!thread)
1109 return NULL;
1111 if (dso)
1112 goto got_dso;
1114 if (map)
1115 goto got_map;
1117 map = thread__find_map(thread, ip);
1118 if (map != NULL) {
1120 * We have to do this here as we may have a dso
1121 * with no symbol hit that has a name longer than
1122 * the ones with symbols sampled.
1124 if (!sort_dso.elide && !map->dso->slen_calculated)
1125 dso__calc_col_width(map->dso);
1127 if (mapp)
1128 *mapp = map;
1129 got_map:
1130 ip = map->map_ip(map, ip);
1132 dso = map->dso;
1133 } else {
1135 * If this is outside of all known maps,
1136 * and is a negative address, try to look it
1137 * up in the kernel dso, as it might be a
1138 * vsyscall (which executes in user-mode):
1140 if ((long long)ip < 0)
1141 dso = kernel_dso;
1143 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
1144 dprintf(" ...... map: %Lx -> %Lx\n", *ipp, ip);
1145 *ipp = ip;
1147 if (dsop)
1148 *dsop = dso;
1150 if (!dso)
1151 return NULL;
1152 got_dso:
1153 return dso->find_symbol(dso, ip);
1156 static int call__match(struct symbol *sym)
1158 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
1159 return 1;
1161 return 0;
1164 static struct symbol **
1165 resolve_callchain(struct thread *thread, struct map *map __used,
1166 struct ip_callchain *chain, struct hist_entry *entry)
1168 u64 context = PERF_CONTEXT_MAX;
1169 struct symbol **syms = NULL;
1170 unsigned int i;
1172 if (callchain) {
1173 syms = calloc(chain->nr, sizeof(*syms));
1174 if (!syms) {
1175 fprintf(stderr, "Can't allocate memory for symbols\n");
1176 exit(-1);
1180 for (i = 0; i < chain->nr; i++) {
1181 u64 ip = chain->ips[i];
1182 struct dso *dso = NULL;
1183 struct symbol *sym;
1185 if (ip >= PERF_CONTEXT_MAX) {
1186 context = ip;
1187 continue;
1190 switch (context) {
1191 case PERF_CONTEXT_HV:
1192 dso = hypervisor_dso;
1193 break;
1194 case PERF_CONTEXT_KERNEL:
1195 dso = kernel_dso;
1196 break;
1197 default:
1198 break;
1201 sym = resolve_symbol(thread, NULL, &dso, &ip);
1203 if (sym) {
1204 if (sort__has_parent && call__match(sym) &&
1205 !entry->parent)
1206 entry->parent = sym;
1207 if (!callchain)
1208 break;
1209 syms[i] = sym;
1213 return syms;
1217 * collect histogram counts
1220 static int
1221 hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
1222 struct symbol *sym, u64 ip, struct ip_callchain *chain,
1223 char level, u64 count)
1225 struct rb_node **p = &hist.rb_node;
1226 struct rb_node *parent = NULL;
1227 struct hist_entry *he;
1228 struct symbol **syms = NULL;
1229 struct hist_entry entry = {
1230 .thread = thread,
1231 .map = map,
1232 .dso = dso,
1233 .sym = sym,
1234 .ip = ip,
1235 .level = level,
1236 .count = count,
1237 .parent = NULL,
1238 .sorted_chain = RB_ROOT
1240 int cmp;
1242 if ((sort__has_parent || callchain) && chain)
1243 syms = resolve_callchain(thread, map, chain, &entry);
1245 while (*p != NULL) {
1246 parent = *p;
1247 he = rb_entry(parent, struct hist_entry, rb_node);
1249 cmp = hist_entry__cmp(&entry, he);
1251 if (!cmp) {
1252 he->count += count;
1253 if (callchain) {
1254 append_chain(&he->callchain, chain, syms);
1255 free(syms);
1257 return 0;
1260 if (cmp < 0)
1261 p = &(*p)->rb_left;
1262 else
1263 p = &(*p)->rb_right;
1266 he = malloc(sizeof(*he));
1267 if (!he)
1268 return -ENOMEM;
1269 *he = entry;
1270 if (callchain) {
1271 callchain_init(&he->callchain);
1272 append_chain(&he->callchain, chain, syms);
1273 free(syms);
1275 rb_link_node(&he->rb_node, parent, p);
1276 rb_insert_color(&he->rb_node, &hist);
1278 return 0;
1281 static void hist_entry__free(struct hist_entry *he)
1283 free(he);
1287 * collapse the histogram
1290 static struct rb_root collapse_hists;
1292 static void collapse__insert_entry(struct hist_entry *he)
1294 struct rb_node **p = &collapse_hists.rb_node;
1295 struct rb_node *parent = NULL;
1296 struct hist_entry *iter;
1297 int64_t cmp;
1299 while (*p != NULL) {
1300 parent = *p;
1301 iter = rb_entry(parent, struct hist_entry, rb_node);
1303 cmp = hist_entry__collapse(iter, he);
1305 if (!cmp) {
1306 iter->count += he->count;
1307 hist_entry__free(he);
1308 return;
1311 if (cmp < 0)
1312 p = &(*p)->rb_left;
1313 else
1314 p = &(*p)->rb_right;
1317 rb_link_node(&he->rb_node, parent, p);
1318 rb_insert_color(&he->rb_node, &collapse_hists);
1321 static void collapse__resort(void)
1323 struct rb_node *next;
1324 struct hist_entry *n;
1326 if (!sort__need_collapse)
1327 return;
1329 next = rb_first(&hist);
1330 while (next) {
1331 n = rb_entry(next, struct hist_entry, rb_node);
1332 next = rb_next(&n->rb_node);
1334 rb_erase(&n->rb_node, &hist);
1335 collapse__insert_entry(n);
1340 * reverse the map, sort on count.
1343 static struct rb_root output_hists;
1345 static void output__insert_entry(struct hist_entry *he, u64 min_callchain_hits)
1347 struct rb_node **p = &output_hists.rb_node;
1348 struct rb_node *parent = NULL;
1349 struct hist_entry *iter;
1351 if (callchain)
1352 callchain_param.sort(&he->sorted_chain, &he->callchain,
1353 min_callchain_hits, &callchain_param);
1355 while (*p != NULL) {
1356 parent = *p;
1357 iter = rb_entry(parent, struct hist_entry, rb_node);
1359 if (he->count > iter->count)
1360 p = &(*p)->rb_left;
1361 else
1362 p = &(*p)->rb_right;
1365 rb_link_node(&he->rb_node, parent, p);
1366 rb_insert_color(&he->rb_node, &output_hists);
1369 static void output__resort(u64 total_samples)
1371 struct rb_node *next;
1372 struct hist_entry *n;
1373 struct rb_root *tree = &hist;
1374 u64 min_callchain_hits;
1376 min_callchain_hits = total_samples * (callchain_param.min_percent / 100);
1378 if (sort__need_collapse)
1379 tree = &collapse_hists;
1381 next = rb_first(tree);
1383 while (next) {
1384 n = rb_entry(next, struct hist_entry, rb_node);
1385 next = rb_next(&n->rb_node);
1387 rb_erase(&n->rb_node, tree);
1388 output__insert_entry(n, min_callchain_hits);
1392 static size_t output__fprintf(FILE *fp, u64 total_samples)
1394 struct hist_entry *pos;
1395 struct sort_entry *se;
1396 struct rb_node *nd;
1397 size_t ret = 0;
1398 unsigned int width;
1399 char *col_width = col_width_list_str;
1401 init_rem_hits();
1403 fprintf(fp, "# Samples: %Ld\n", (u64)total_samples);
1404 fprintf(fp, "#\n");
1406 fprintf(fp, "# Overhead");
1407 if (show_nr_samples) {
1408 if (field_sep)
1409 fprintf(fp, "%cSamples", *field_sep);
1410 else
1411 fputs(" Samples ", fp);
1413 list_for_each_entry(se, &hist_entry__sort_list, list) {
1414 if (se->elide)
1415 continue;
1416 if (field_sep) {
1417 fprintf(fp, "%c%s", *field_sep, se->header);
1418 continue;
1420 width = strlen(se->header);
1421 if (se->width) {
1422 if (col_width_list_str) {
1423 if (col_width) {
1424 *se->width = atoi(col_width);
1425 col_width = strchr(col_width, ',');
1426 if (col_width)
1427 ++col_width;
1430 width = *se->width = max(*se->width, width);
1432 fprintf(fp, " %*s", width, se->header);
1434 fprintf(fp, "\n");
1436 if (field_sep)
1437 goto print_entries;
1439 fprintf(fp, "# ........");
1440 if (show_nr_samples)
1441 fprintf(fp, " ..........");
1442 list_for_each_entry(se, &hist_entry__sort_list, list) {
1443 unsigned int i;
1445 if (se->elide)
1446 continue;
1448 fprintf(fp, " ");
1449 if (se->width)
1450 width = *se->width;
1451 else
1452 width = strlen(se->header);
1453 for (i = 0; i < width; i++)
1454 fprintf(fp, ".");
1456 fprintf(fp, "\n");
1458 fprintf(fp, "#\n");
1460 print_entries:
1461 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
1462 pos = rb_entry(nd, struct hist_entry, rb_node);
1463 ret += hist_entry__fprintf(fp, pos, total_samples);
1466 if (sort_order == default_sort_order &&
1467 parent_pattern == default_parent_pattern) {
1468 fprintf(fp, "#\n");
1469 fprintf(fp, "# (For a higher level overview, try: perf report --sort comm,dso)\n");
1470 fprintf(fp, "#\n");
1472 fprintf(fp, "\n");
1474 free(rem_sq_bracket);
1476 return ret;
1479 static void register_idle_thread(void)
1481 struct thread *thread = threads__findnew(0);
1483 if (thread == NULL ||
1484 thread__set_comm(thread, "[idle]")) {
1485 fprintf(stderr, "problem inserting idle task.\n");
1486 exit(-1);
1490 static unsigned long total = 0,
1491 total_mmap = 0,
1492 total_comm = 0,
1493 total_fork = 0,
1494 total_unknown = 0,
1495 total_lost = 0;
1497 static int validate_chain(struct ip_callchain *chain, event_t *event)
1499 unsigned int chain_size;
1501 chain_size = event->header.size;
1502 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
1504 if (chain->nr*sizeof(u64) > chain_size)
1505 return -1;
1507 return 0;
1510 static int
1511 process_sample_event(event_t *event, unsigned long offset, unsigned long head)
1513 char level;
1514 int show = 0;
1515 struct dso *dso = NULL;
1516 struct thread *thread = threads__findnew(event->ip.pid);
1517 u64 ip = event->ip.ip;
1518 u64 period = 1;
1519 struct map *map = NULL;
1520 void *more_data = event->ip.__more_data;
1521 struct ip_callchain *chain = NULL;
1522 int cpumode;
1524 if (sample_type & PERF_SAMPLE_PERIOD) {
1525 period = *(u64 *)more_data;
1526 more_data += sizeof(u64);
1529 dprintf("%p [%p]: PERF_EVENT_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
1530 (void *)(offset + head),
1531 (void *)(long)(event->header.size),
1532 event->header.misc,
1533 event->ip.pid, event->ip.tid,
1534 (void *)(long)ip,
1535 (long long)period);
1537 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
1538 unsigned int i;
1540 chain = (void *)more_data;
1542 dprintf("... chain: nr:%Lu\n", chain->nr);
1544 if (validate_chain(chain, event) < 0) {
1545 eprintf("call-chain problem with event, skipping it.\n");
1546 return 0;
1549 if (dump_trace) {
1550 for (i = 0; i < chain->nr; i++)
1551 dprintf("..... %2d: %016Lx\n", i, chain->ips[i]);
1555 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1557 if (thread == NULL) {
1558 eprintf("problem processing %d event, skipping it.\n",
1559 event->header.type);
1560 return -1;
1563 if (comm_list && !strlist__has_entry(comm_list, thread->comm))
1564 return 0;
1566 cpumode = event->header.misc & PERF_EVENT_MISC_CPUMODE_MASK;
1568 if (cpumode == PERF_EVENT_MISC_KERNEL) {
1569 show = SHOW_KERNEL;
1570 level = 'k';
1572 dso = kernel_dso;
1574 dprintf(" ...... dso: %s\n", dso->name);
1576 } else if (cpumode == PERF_EVENT_MISC_USER) {
1578 show = SHOW_USER;
1579 level = '.';
1581 } else {
1582 show = SHOW_HV;
1583 level = 'H';
1585 dso = hypervisor_dso;
1587 dprintf(" ...... dso: [hypervisor]\n");
1590 if (show & show_mask) {
1591 struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
1593 if (dso_list && dso && dso->name && !strlist__has_entry(dso_list, dso->name))
1594 return 0;
1596 if (sym_list && sym && !strlist__has_entry(sym_list, sym->name))
1597 return 0;
1599 if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
1600 eprintf("problem incrementing symbol count, skipping event\n");
1601 return -1;
1604 total += period;
1606 return 0;
1609 static int
1610 process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
1612 struct thread *thread = threads__findnew(event->mmap.pid);
1613 struct map *map = map__new(&event->mmap);
1615 dprintf("%p [%p]: PERF_EVENT_MMAP %d/%d: [%p(%p) @ %p]: %s\n",
1616 (void *)(offset + head),
1617 (void *)(long)(event->header.size),
1618 event->mmap.pid,
1619 event->mmap.tid,
1620 (void *)(long)event->mmap.start,
1621 (void *)(long)event->mmap.len,
1622 (void *)(long)event->mmap.pgoff,
1623 event->mmap.filename);
1625 if (thread == NULL || map == NULL) {
1626 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
1627 return 0;
1630 thread__insert_map(thread, map);
1631 total_mmap++;
1633 return 0;
1636 static int
1637 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
1639 struct thread *thread = threads__findnew(event->comm.pid);
1641 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
1642 (void *)(offset + head),
1643 (void *)(long)(event->header.size),
1644 event->comm.comm, event->comm.pid);
1646 if (thread == NULL ||
1647 thread__set_comm(thread, event->comm.comm)) {
1648 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
1649 return -1;
1651 total_comm++;
1653 return 0;
1656 static int
1657 process_task_event(event_t *event, unsigned long offset, unsigned long head)
1659 struct thread *thread = threads__findnew(event->fork.pid);
1660 struct thread *parent = threads__findnew(event->fork.ppid);
1662 dprintf("%p [%p]: PERF_EVENT_%s: (%d:%d):(%d:%d)\n",
1663 (void *)(offset + head),
1664 (void *)(long)(event->header.size),
1665 event->header.type == PERF_EVENT_FORK ? "FORK" : "EXIT",
1666 event->fork.pid, event->fork.tid,
1667 event->fork.ppid, event->fork.ptid);
1670 * A thread clone will have the same PID for both
1671 * parent and child.
1673 if (thread == parent)
1674 return 0;
1676 if (event->header.type == PERF_EVENT_EXIT)
1677 return 0;
1679 if (!thread || !parent || thread__fork(thread, parent)) {
1680 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
1681 return -1;
1683 total_fork++;
1685 return 0;
1688 static int
1689 process_lost_event(event_t *event, unsigned long offset, unsigned long head)
1691 dprintf("%p [%p]: PERF_EVENT_LOST: id:%Ld: lost:%Ld\n",
1692 (void *)(offset + head),
1693 (void *)(long)(event->header.size),
1694 event->lost.id,
1695 event->lost.lost);
1697 total_lost += event->lost.lost;
1699 return 0;
1702 static void trace_event(event_t *event)
1704 unsigned char *raw_event = (void *)event;
1705 char *color = PERF_COLOR_BLUE;
1706 int i, j;
1708 if (!dump_trace)
1709 return;
1711 dprintf(".");
1712 cdprintf("\n. ... raw event: size %d bytes\n", event->header.size);
1714 for (i = 0; i < event->header.size; i++) {
1715 if ((i & 15) == 0) {
1716 dprintf(".");
1717 cdprintf(" %04x: ", i);
1720 cdprintf(" %02x", raw_event[i]);
1722 if (((i & 15) == 15) || i == event->header.size-1) {
1723 cdprintf(" ");
1724 for (j = 0; j < 15-(i & 15); j++)
1725 cdprintf(" ");
1726 for (j = 0; j < (i & 15); j++) {
1727 if (isprint(raw_event[i-15+j]))
1728 cdprintf("%c", raw_event[i-15+j]);
1729 else
1730 cdprintf(".");
1732 cdprintf("\n");
1735 dprintf(".\n");
1738 static struct perf_header *header;
1740 static struct perf_counter_attr *perf_header__find_attr(u64 id)
1742 int i;
1744 for (i = 0; i < header->attrs; i++) {
1745 struct perf_header_attr *attr = header->attr[i];
1746 int j;
1748 for (j = 0; j < attr->ids; j++) {
1749 if (attr->id[j] == id)
1750 return &attr->attr;
1754 return NULL;
1757 static int
1758 process_read_event(event_t *event, unsigned long offset, unsigned long head)
1760 struct perf_counter_attr *attr = perf_header__find_attr(event->read.id);
1762 dprintf("%p [%p]: PERF_EVENT_READ: %d %d %s %Lu\n",
1763 (void *)(offset + head),
1764 (void *)(long)(event->header.size),
1765 event->read.pid,
1766 event->read.tid,
1767 attr ? __event_name(attr->type, attr->config)
1768 : "FAIL",
1769 event->read.value);
1771 return 0;
1774 static int
1775 process_event(event_t *event, unsigned long offset, unsigned long head)
1777 trace_event(event);
1779 switch (event->header.type) {
1780 case PERF_EVENT_SAMPLE:
1781 return process_sample_event(event, offset, head);
1783 case PERF_EVENT_MMAP:
1784 return process_mmap_event(event, offset, head);
1786 case PERF_EVENT_COMM:
1787 return process_comm_event(event, offset, head);
1789 case PERF_EVENT_FORK:
1790 case PERF_EVENT_EXIT:
1791 return process_task_event(event, offset, head);
1793 case PERF_EVENT_LOST:
1794 return process_lost_event(event, offset, head);
1796 case PERF_EVENT_READ:
1797 return process_read_event(event, offset, head);
1800 * We dont process them right now but they are fine:
1803 case PERF_EVENT_THROTTLE:
1804 case PERF_EVENT_UNTHROTTLE:
1805 return 0;
1807 default:
1808 return -1;
1811 return 0;
1814 static u64 perf_header__sample_type(void)
1816 u64 sample_type = 0;
1817 int i;
1819 for (i = 0; i < header->attrs; i++) {
1820 struct perf_header_attr *attr = header->attr[i];
1822 if (!sample_type)
1823 sample_type = attr->attr.sample_type;
1824 else if (sample_type != attr->attr.sample_type)
1825 die("non matching sample_type");
1828 return sample_type;
1831 static int __cmd_report(void)
1833 int ret, rc = EXIT_FAILURE;
1834 unsigned long offset = 0;
1835 unsigned long head, shift;
1836 struct stat stat;
1837 event_t *event;
1838 uint32_t size;
1839 char *buf;
1841 register_idle_thread();
1843 input = open(input_name, O_RDONLY);
1844 if (input < 0) {
1845 fprintf(stderr, " failed to open file: %s", input_name);
1846 if (!strcmp(input_name, "perf.data"))
1847 fprintf(stderr, " (try 'perf record' first)");
1848 fprintf(stderr, "\n");
1849 exit(-1);
1852 ret = fstat(input, &stat);
1853 if (ret < 0) {
1854 perror("failed to stat file");
1855 exit(-1);
1858 if (!stat.st_size) {
1859 fprintf(stderr, "zero-sized file, nothing to do!\n");
1860 exit(0);
1863 header = perf_header__read(input);
1864 head = header->data_offset;
1866 sample_type = perf_header__sample_type();
1868 if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
1869 if (sort__has_parent) {
1870 fprintf(stderr, "selected --sort parent, but no"
1871 " callchain data. Did you call"
1872 " perf record without -g?\n");
1873 exit(-1);
1875 if (callchain) {
1876 fprintf(stderr, "selected -c but no callchain data."
1877 " Did you call perf record without"
1878 " -g?\n");
1879 exit(-1);
1881 } else if (callchain_param.mode != CHAIN_NONE && !callchain) {
1882 callchain = 1;
1883 if (register_callchain_param(&callchain_param) < 0) {
1884 fprintf(stderr, "Can't register callchain"
1885 " params\n");
1886 exit(-1);
1890 if (load_kernel() < 0) {
1891 perror("failed to load kernel symbols");
1892 return EXIT_FAILURE;
1895 if (!full_paths) {
1896 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
1897 perror("failed to get the current directory");
1898 return EXIT_FAILURE;
1900 cwdlen = strlen(cwd);
1901 } else {
1902 cwd = NULL;
1903 cwdlen = 0;
1906 shift = page_size * (head / page_size);
1907 offset += shift;
1908 head -= shift;
1910 remap:
1911 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1912 MAP_SHARED, input, offset);
1913 if (buf == MAP_FAILED) {
1914 perror("failed to mmap file");
1915 exit(-1);
1918 more:
1919 event = (event_t *)(buf + head);
1921 size = event->header.size;
1922 if (!size)
1923 size = 8;
1925 if (head + event->header.size >= page_size * mmap_window) {
1926 int ret;
1928 shift = page_size * (head / page_size);
1930 ret = munmap(buf, page_size * mmap_window);
1931 assert(ret == 0);
1933 offset += shift;
1934 head -= shift;
1935 goto remap;
1938 size = event->header.size;
1940 dprintf("\n%p [%p]: event: %d\n",
1941 (void *)(offset + head),
1942 (void *)(long)event->header.size,
1943 event->header.type);
1945 if (!size || process_event(event, offset, head) < 0) {
1947 dprintf("%p [%p]: skipping unknown header type: %d\n",
1948 (void *)(offset + head),
1949 (void *)(long)(event->header.size),
1950 event->header.type);
1952 total_unknown++;
1955 * assume we lost track of the stream, check alignment, and
1956 * increment a single u64 in the hope to catch on again 'soon'.
1959 if (unlikely(head & 7))
1960 head &= ~7ULL;
1962 size = 8;
1965 head += size;
1967 if (offset + head >= header->data_offset + header->data_size)
1968 goto done;
1970 if (offset + head < (unsigned long)stat.st_size)
1971 goto more;
1973 done:
1974 rc = EXIT_SUCCESS;
1975 close(input);
1977 dprintf(" IP events: %10ld\n", total);
1978 dprintf(" mmap events: %10ld\n", total_mmap);
1979 dprintf(" comm events: %10ld\n", total_comm);
1980 dprintf(" fork events: %10ld\n", total_fork);
1981 dprintf(" lost events: %10ld\n", total_lost);
1982 dprintf(" unknown events: %10ld\n", total_unknown);
1984 if (dump_trace)
1985 return 0;
1987 if (verbose >= 3)
1988 threads__fprintf(stdout);
1990 if (verbose >= 2)
1991 dsos__fprintf(stdout);
1993 collapse__resort();
1994 output__resort(total);
1995 output__fprintf(stdout, total);
1997 return rc;
2000 static int
2001 parse_callchain_opt(const struct option *opt __used, const char *arg,
2002 int unset __used)
2004 char *tok;
2005 char *endptr;
2007 callchain = 1;
2009 if (!arg)
2010 return 0;
2012 tok = strtok((char *)arg, ",");
2013 if (!tok)
2014 return -1;
2016 /* get the output mode */
2017 if (!strncmp(tok, "graph", strlen(arg)))
2018 callchain_param.mode = CHAIN_GRAPH_ABS;
2020 else if (!strncmp(tok, "flat", strlen(arg)))
2021 callchain_param.mode = CHAIN_FLAT;
2023 else if (!strncmp(tok, "fractal", strlen(arg)))
2024 callchain_param.mode = CHAIN_GRAPH_REL;
2026 else if (!strncmp(tok, "none", strlen(arg))) {
2027 callchain_param.mode = CHAIN_NONE;
2028 callchain = 0;
2030 return 0;
2033 else
2034 return -1;
2036 /* get the min percentage */
2037 tok = strtok(NULL, ",");
2038 if (!tok)
2039 goto setup;
2041 callchain_param.min_percent = strtod(tok, &endptr);
2042 if (tok == endptr)
2043 return -1;
2045 setup:
2046 if (register_callchain_param(&callchain_param) < 0) {
2047 fprintf(stderr, "Can't register callchain params\n");
2048 return -1;
2050 return 0;
2053 static const char * const report_usage[] = {
2054 "perf report [<options>] <command>",
2055 NULL
2058 static const struct option options[] = {
2059 OPT_STRING('i', "input", &input_name, "file",
2060 "input file name"),
2061 OPT_BOOLEAN('v', "verbose", &verbose,
2062 "be more verbose (show symbol address, etc)"),
2063 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
2064 "dump raw trace in ASCII"),
2065 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
2066 OPT_BOOLEAN('m', "modules", &modules,
2067 "load module symbols - WARNING: use only with -k and LIVE kernel"),
2068 OPT_BOOLEAN('n', "show-nr-samples", &show_nr_samples,
2069 "Show a column with the number of samples"),
2070 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
2071 "sort by key(s): pid, comm, dso, symbol, parent"),
2072 OPT_BOOLEAN('P', "full-paths", &full_paths,
2073 "Don't shorten the pathnames taking into account the cwd"),
2074 OPT_STRING('p', "parent", &parent_pattern, "regex",
2075 "regex filter to identify parent, see: '--sort parent'"),
2076 OPT_BOOLEAN('x', "exclude-other", &exclude_other,
2077 "Only display entries with parent-match"),
2078 OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
2079 "Display callchains using output_type and min percent threshold. "
2080 "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
2081 OPT_STRING('d', "dsos", &dso_list_str, "dso[,dso...]",
2082 "only consider symbols in these dsos"),
2083 OPT_STRING('C', "comms", &comm_list_str, "comm[,comm...]",
2084 "only consider symbols in these comms"),
2085 OPT_STRING('S', "symbols", &sym_list_str, "symbol[,symbol...]",
2086 "only consider these symbols"),
2087 OPT_STRING('w', "column-widths", &col_width_list_str,
2088 "width[,width...]",
2089 "don't try to adjust column width, use these fixed values"),
2090 OPT_STRING('t', "field-separator", &field_sep, "separator",
2091 "separator for columns, no spaces will be added between "
2092 "columns '.' is reserved."),
2093 OPT_END()
2096 static void setup_sorting(void)
2098 char *tmp, *tok, *str = strdup(sort_order);
2100 for (tok = strtok_r(str, ", ", &tmp);
2101 tok; tok = strtok_r(NULL, ", ", &tmp)) {
2102 if (sort_dimension__add(tok) < 0) {
2103 error("Unknown --sort key: `%s'", tok);
2104 usage_with_options(report_usage, options);
2108 free(str);
2111 static void setup_list(struct strlist **list, const char *list_str,
2112 struct sort_entry *se, const char *list_name,
2113 FILE *fp)
2115 if (list_str) {
2116 *list = strlist__new(true, list_str);
2117 if (!*list) {
2118 fprintf(stderr, "problems parsing %s list\n",
2119 list_name);
2120 exit(129);
2122 if (strlist__nr_entries(*list) == 1) {
2123 fprintf(fp, "# %s: %s\n", list_name,
2124 strlist__entry(*list, 0)->s);
2125 se->elide = true;
2130 int cmd_report(int argc, const char **argv, const char *prefix __used)
2132 symbol__init();
2134 page_size = getpagesize();
2136 argc = parse_options(argc, argv, options, report_usage, 0);
2138 setup_sorting();
2140 if (parent_pattern != default_parent_pattern) {
2141 sort_dimension__add("parent");
2142 sort_parent.elide = 1;
2143 } else
2144 exclude_other = 0;
2147 * Any (unrecognized) arguments left?
2149 if (argc)
2150 usage_with_options(report_usage, options);
2152 setup_pager();
2154 setup_list(&dso_list, dso_list_str, &sort_dso, "dso", stdout);
2155 setup_list(&comm_list, comm_list_str, &sort_comm, "comm", stdout);
2156 setup_list(&sym_list, sym_list_str, &sort_sym, "symbol", stdout);
2158 if (field_sep && *field_sep == '.') {
2159 fputs("'.' is the only non valid --field-separator argument\n",
2160 stderr);
2161 exit(129);
2164 return __cmd_report();