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.
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"
20 #include "util/values.h"
23 #include "util/header.h"
25 #include "util/parse-options.h"
26 #include "util/parse-events.h"
32 static char const *input_name = "perf.data";
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,
38 static struct strlist *dso_list, *comm_list, *sym_list;
39 static char *field_sep;
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 full_paths;
49 static int show_nr_samples;
51 static int show_threads;
52 static struct perf_read_values show_threads_values;
54 static char default_pretty_printing_style[] = "normal";
55 static char *pretty_printing_style = default_pretty_printing_style;
57 static unsigned long page_size;
58 static unsigned long mmap_window = 32;
60 static char default_parent_pattern[] = "^sys_|^do_page_fault";
61 static char *parent_pattern = default_parent_pattern;
62 static regex_t parent_regex;
64 static int exclude_other = 1;
66 static char callchain_default_opt[] = "fractal,0.5";
71 struct callchain_param callchain_param = {
72 .mode = CHAIN_GRAPH_REL,
76 static u64 sample_type;
79 struct perf_event_header header;
82 unsigned char __more_data[];
86 struct perf_event_header header;
91 char filename[PATH_MAX];
95 struct perf_event_header header;
101 struct perf_event_header header;
107 struct perf_event_header header;
113 struct perf_event_header header;
121 typedef union event_union {
122 struct perf_event_header header;
124 struct mmap_event mmap;
125 struct comm_event comm;
126 struct fork_event fork;
127 struct lost_event lost;
128 struct read_event read;
131 static int repsep_fprintf(FILE *fp, const char *fmt, ...)
138 n = vfprintf(fp, fmt, ap);
141 n = vasprintf(&bf, fmt, ap);
145 sep = strchr(sep, *field_sep);
160 static char __cwd[PATH_MAX];
161 static char *cwd = __cwd;
164 static int strcommon(const char *pathname)
168 while (n < cwdlen && pathname[n] == cwd[n])
175 struct list_head node;
179 u64 (*map_ip)(struct map *, u64);
183 static u64 map__map_ip(struct map *map, u64 ip)
185 return ip - map->start + map->pgoff;
188 static u64 vdso__map_ip(struct map *map __used, u64 ip)
193 static inline int is_anon_memory(const char *filename)
195 return strcmp(filename, "//anon") == 0;
198 static struct map *map__new(struct mmap_event *event)
200 struct map *self = malloc(sizeof(*self));
203 const char *filename = event->filename;
204 char newfilename[PATH_MAX];
208 int n = strcommon(filename);
211 snprintf(newfilename, sizeof(newfilename),
212 ".%s", filename + n);
213 filename = newfilename;
217 anon = is_anon_memory(filename);
220 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
221 filename = newfilename;
224 self->start = event->start;
225 self->end = event->start + event->len;
226 self->pgoff = event->pgoff;
228 self->dso = dsos__findnew(filename);
229 if (self->dso == NULL)
232 if (self->dso == vdso || anon)
233 self->map_ip = vdso__map_ip;
235 self->map_ip = map__map_ip;
243 static struct map *map__clone(struct map *self)
245 struct map *map = malloc(sizeof(*self));
250 memcpy(map, self, sizeof(*self));
255 static int map__overlap(struct map *l, struct map *r)
257 if (l->start > r->start) {
263 if (l->end > r->start)
269 static size_t map__fprintf(struct map *self, FILE *fp)
271 return fprintf(fp, " %Lx-%Lx %Lx %s\n",
272 self->start, self->end, self->pgoff, self->dso->name);
277 struct rb_node rb_node;
278 struct list_head maps;
283 static struct thread *thread__new(pid_t pid)
285 struct thread *self = malloc(sizeof(*self));
289 self->comm = malloc(32);
291 snprintf(self->comm, 32, ":%d", self->pid);
292 INIT_LIST_HEAD(&self->maps);
298 static unsigned int dsos__col_width,
302 static int thread__set_comm(struct thread *self, const char *comm)
306 self->comm = strdup(comm);
310 if (!col_width_list_str && !field_sep &&
311 (!comm_list || strlist__has_entry(comm_list, comm))) {
312 unsigned int slen = strlen(comm);
313 if (slen > comms__col_width) {
314 comms__col_width = slen;
315 threads__col_width = slen + 6;
322 static size_t thread__fprintf(struct thread *self, FILE *fp)
325 size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
327 list_for_each_entry(pos, &self->maps, node)
328 ret += map__fprintf(pos, fp);
334 static struct rb_root threads;
335 static struct thread *last_match;
337 static struct thread *threads__findnew(pid_t pid)
339 struct rb_node **p = &threads.rb_node;
340 struct rb_node *parent = NULL;
344 * Font-end cache - PID lookups come in blocks,
345 * so most of the time we dont have to look up
348 if (last_match && last_match->pid == pid)
353 th = rb_entry(parent, struct thread, rb_node);
355 if (th->pid == pid) {
366 th = thread__new(pid);
368 rb_link_node(&th->rb_node, parent, p);
369 rb_insert_color(&th->rb_node, &threads);
376 static void thread__insert_map(struct thread *self, struct map *map)
378 struct map *pos, *tmp;
380 list_for_each_entry_safe(pos, tmp, &self->maps, node) {
381 if (map__overlap(pos, map)) {
383 printf("overlapping maps:\n");
384 map__fprintf(map, stdout);
385 map__fprintf(pos, stdout);
388 if (map->start <= pos->start && map->end > pos->start)
389 pos->start = map->end;
391 if (map->end >= pos->end && map->start < pos->end)
392 pos->end = map->start;
395 printf("after collision:\n");
396 map__fprintf(pos, stdout);
399 if (pos->start >= pos->end) {
400 list_del_init(&pos->node);
406 list_add_tail(&map->node, &self->maps);
409 static int thread__fork(struct thread *self, struct thread *parent)
415 self->comm = strdup(parent->comm);
419 list_for_each_entry(map, &parent->maps, node) {
420 struct map *new = map__clone(map);
423 thread__insert_map(self, new);
429 static struct map *thread__find_map(struct thread *self, u64 ip)
436 list_for_each_entry(pos, &self->maps, node)
437 if (ip >= pos->start && ip <= pos->end)
443 static size_t threads__fprintf(FILE *fp)
448 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
449 struct thread *pos = rb_entry(nd, struct thread, rb_node);
451 ret += thread__fprintf(pos, fp);
458 * histogram, sorted on item, collects counts
461 static struct rb_root hist;
464 struct rb_node rb_node;
466 struct thread *thread;
470 struct symbol *parent;
473 struct callchain_node callchain;
474 struct rb_root sorted_chain;
480 * configurable sorting bits
484 struct list_head list;
488 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
489 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
490 size_t (*print)(FILE *fp, struct hist_entry *, unsigned int width);
495 static int64_t cmp_null(void *l, void *r)
508 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
510 return right->thread->pid - left->thread->pid;
514 sort__thread_print(FILE *fp, struct hist_entry *self, unsigned int width)
516 return repsep_fprintf(fp, "%*s:%5d", width - 6,
517 self->thread->comm ?: "", self->thread->pid);
520 static struct sort_entry sort_thread = {
521 .header = "Command: Pid",
522 .cmp = sort__thread_cmp,
523 .print = sort__thread_print,
524 .width = &threads__col_width,
530 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
532 return right->thread->pid - left->thread->pid;
536 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
538 char *comm_l = left->thread->comm;
539 char *comm_r = right->thread->comm;
541 if (!comm_l || !comm_r)
542 return cmp_null(comm_l, comm_r);
544 return strcmp(comm_l, comm_r);
548 sort__comm_print(FILE *fp, struct hist_entry *self, unsigned int width)
550 return repsep_fprintf(fp, "%*s", width, self->thread->comm);
553 static struct sort_entry sort_comm = {
555 .cmp = sort__comm_cmp,
556 .collapse = sort__comm_collapse,
557 .print = sort__comm_print,
558 .width = &comms__col_width,
564 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
566 struct dso *dso_l = left->dso;
567 struct dso *dso_r = right->dso;
569 if (!dso_l || !dso_r)
570 return cmp_null(dso_l, dso_r);
572 return strcmp(dso_l->name, dso_r->name);
576 sort__dso_print(FILE *fp, struct hist_entry *self, unsigned int width)
579 return repsep_fprintf(fp, "%-*s", width, self->dso->name);
581 return repsep_fprintf(fp, "%*llx", width, (u64)self->ip);
584 static struct sort_entry sort_dso = {
585 .header = "Shared Object",
586 .cmp = sort__dso_cmp,
587 .print = sort__dso_print,
588 .width = &dsos__col_width,
594 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
598 if (left->sym == right->sym)
601 ip_l = left->sym ? left->sym->start : left->ip;
602 ip_r = right->sym ? right->sym->start : right->ip;
604 return (int64_t)(ip_r - ip_l);
608 sort__sym_print(FILE *fp, struct hist_entry *self, unsigned int width __used)
613 ret += repsep_fprintf(fp, "%#018llx %c ", (u64)self->ip,
614 dso__symtab_origin(self->dso));
616 ret += repsep_fprintf(fp, "[%c] ", self->level);
618 ret += repsep_fprintf(fp, "%s", self->sym->name);
620 if (self->sym->module)
621 ret += repsep_fprintf(fp, "\t[%s]",
622 self->sym->module->name);
624 ret += repsep_fprintf(fp, "%#016llx", (u64)self->ip);
630 static struct sort_entry sort_sym = {
632 .cmp = sort__sym_cmp,
633 .print = sort__sym_print,
639 sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
641 struct symbol *sym_l = left->parent;
642 struct symbol *sym_r = right->parent;
644 if (!sym_l || !sym_r)
645 return cmp_null(sym_l, sym_r);
647 return strcmp(sym_l->name, sym_r->name);
651 sort__parent_print(FILE *fp, struct hist_entry *self, unsigned int width)
653 return repsep_fprintf(fp, "%-*s", width,
654 self->parent ? self->parent->name : "[other]");
657 static unsigned int parent_symbol__col_width;
659 static struct sort_entry sort_parent = {
660 .header = "Parent symbol",
661 .cmp = sort__parent_cmp,
662 .print = sort__parent_print,
663 .width = &parent_symbol__col_width,
666 static int sort__need_collapse = 0;
667 static int sort__has_parent = 0;
669 struct sort_dimension {
671 struct sort_entry *entry;
675 static struct sort_dimension sort_dimensions[] = {
676 { .name = "pid", .entry = &sort_thread, },
677 { .name = "comm", .entry = &sort_comm, },
678 { .name = "dso", .entry = &sort_dso, },
679 { .name = "symbol", .entry = &sort_sym, },
680 { .name = "parent", .entry = &sort_parent, },
683 static LIST_HEAD(hist_entry__sort_list);
685 static int sort_dimension__add(char *tok)
689 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
690 struct sort_dimension *sd = &sort_dimensions[i];
695 if (strncasecmp(tok, sd->name, strlen(tok)))
698 if (sd->entry->collapse)
699 sort__need_collapse = 1;
701 if (sd->entry == &sort_parent) {
702 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
706 regerror(ret, &parent_regex, err, sizeof(err));
707 fprintf(stderr, "Invalid regex: %s\n%s",
708 parent_pattern, err);
711 sort__has_parent = 1;
714 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
724 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
726 struct sort_entry *se;
729 list_for_each_entry(se, &hist_entry__sort_list, list) {
730 cmp = se->cmp(left, right);
739 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
741 struct sort_entry *se;
744 list_for_each_entry(se, &hist_entry__sort_list, list) {
745 int64_t (*f)(struct hist_entry *, struct hist_entry *);
747 f = se->collapse ?: se->cmp;
749 cmp = f(left, right);
757 static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask)
762 ret += fprintf(fp, "%s", " ");
764 for (i = 0; i < depth; i++)
765 if (depth_mask & (1 << i))
766 ret += fprintf(fp, "| ");
768 ret += fprintf(fp, " ");
770 ret += fprintf(fp, "\n");
775 ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, int depth,
776 int depth_mask, int count, u64 total_samples,
782 ret += fprintf(fp, "%s", " ");
783 for (i = 0; i < depth; i++) {
784 if (depth_mask & (1 << i))
785 ret += fprintf(fp, "|");
787 ret += fprintf(fp, " ");
788 if (!count && i == depth - 1) {
791 percent = hits * 100.0 / total_samples;
792 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
794 ret += fprintf(fp, "%s", " ");
797 ret += fprintf(fp, "%s\n", chain->sym->name);
799 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
804 static struct symbol *rem_sq_bracket;
805 static struct callchain_list rem_hits;
807 static void init_rem_hits(void)
809 rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
810 if (!rem_sq_bracket) {
811 fprintf(stderr, "Not enough memory to display remaining hits\n");
815 strcpy(rem_sq_bracket->name, "[...]");
816 rem_hits.sym = rem_sq_bracket;
820 callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
821 u64 total_samples, int depth, int depth_mask)
823 struct rb_node *node, *next;
824 struct callchain_node *child;
825 struct callchain_list *chain;
826 int new_depth_mask = depth_mask;
832 if (callchain_param.mode == CHAIN_GRAPH_REL)
833 new_total = self->children_hit;
835 new_total = total_samples;
837 remaining = new_total;
839 node = rb_first(&self->rb_root);
843 child = rb_entry(node, struct callchain_node, rb_node);
844 cumul = cumul_hits(child);
848 * The depth mask manages the output of pipes that show
849 * the depth. We don't want to keep the pipes of the current
850 * level for the last child of this depth.
851 * Except if we have remaining filtered hits. They will
852 * supersede the last child
854 next = rb_next(node);
855 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
856 new_depth_mask &= ~(1 << (depth - 1));
859 * But we keep the older depth mask for the line seperator
860 * to keep the level link until we reach the last child
862 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask);
864 list_for_each_entry(chain, &child->val, list) {
865 if (chain->ip >= PERF_CONTEXT_MAX)
867 ret += ipchain__fprintf_graph(fp, chain, depth,
872 ret += callchain__fprintf_graph(fp, child, new_total,
874 new_depth_mask | (1 << depth));
878 if (callchain_param.mode == CHAIN_GRAPH_REL &&
879 remaining && remaining != new_total) {
884 new_depth_mask &= ~(1 << (depth - 1));
886 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
887 new_depth_mask, 0, new_total,
895 callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
898 struct callchain_list *chain;
904 ret += callchain__fprintf_flat(fp, self->parent, total_samples);
907 list_for_each_entry(chain, &self->val, list) {
908 if (chain->ip >= PERF_CONTEXT_MAX)
911 ret += fprintf(fp, " %s\n", chain->sym->name);
913 ret += fprintf(fp, " %p\n",
914 (void *)(long)chain->ip);
921 hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
924 struct rb_node *rb_node;
925 struct callchain_node *chain;
928 rb_node = rb_first(&self->sorted_chain);
932 chain = rb_entry(rb_node, struct callchain_node, rb_node);
933 percent = chain->hit * 100.0 / total_samples;
934 switch (callchain_param.mode) {
936 ret += percent_color_fprintf(fp, " %6.2f%%\n",
938 ret += callchain__fprintf_flat(fp, chain, total_samples);
940 case CHAIN_GRAPH_ABS: /* Falldown */
941 case CHAIN_GRAPH_REL:
942 ret += callchain__fprintf_graph(fp, chain,
943 total_samples, 1, 1);
947 ret += fprintf(fp, "\n");
948 rb_node = rb_next(rb_node);
956 hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
958 struct sort_entry *se;
961 if (exclude_other && !self->parent)
965 ret = percent_color_fprintf(fp,
966 field_sep ? "%.2f" : " %6.2f%%",
967 (self->count * 100.0) / total_samples);
969 ret = fprintf(fp, field_sep ? "%lld" : "%12lld ", self->count);
971 if (show_nr_samples) {
973 fprintf(fp, "%c%lld", *field_sep, self->count);
975 fprintf(fp, "%11lld", self->count);
978 list_for_each_entry(se, &hist_entry__sort_list, list) {
982 fprintf(fp, "%s", field_sep ?: " ");
983 ret += se->print(fp, self, se->width ? *se->width : 0);
986 ret += fprintf(fp, "\n");
989 hist_entry_callchain__fprintf(fp, self, total_samples);
998 static void dso__calc_col_width(struct dso *self)
1000 if (!col_width_list_str && !field_sep &&
1001 (!dso_list || strlist__has_entry(dso_list, self->name))) {
1002 unsigned int slen = strlen(self->name);
1003 if (slen > dsos__col_width)
1004 dsos__col_width = slen;
1007 self->slen_calculated = 1;
1010 static struct symbol *
1011 resolve_symbol(struct thread *thread, struct map **mapp,
1012 struct dso **dsop, u64 *ipp)
1014 struct dso *dso = dsop ? *dsop : NULL;
1015 struct map *map = mapp ? *mapp : NULL;
1027 map = thread__find_map(thread, ip);
1030 * We have to do this here as we may have a dso
1031 * with no symbol hit that has a name longer than
1032 * the ones with symbols sampled.
1034 if (!sort_dso.elide && !map->dso->slen_calculated)
1035 dso__calc_col_width(map->dso);
1040 ip = map->map_ip(map, ip);
1045 * If this is outside of all known maps,
1046 * and is a negative address, try to look it
1047 * up in the kernel dso, as it might be a
1048 * vsyscall (which executes in user-mode):
1050 if ((long long)ip < 0)
1053 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
1054 dprintf(" ...... map: %Lx -> %Lx\n", *ipp, ip);
1063 return dso->find_symbol(dso, ip);
1066 static int call__match(struct symbol *sym)
1068 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
1074 static struct symbol **
1075 resolve_callchain(struct thread *thread, struct map *map __used,
1076 struct ip_callchain *chain, struct hist_entry *entry)
1078 u64 context = PERF_CONTEXT_MAX;
1079 struct symbol **syms = NULL;
1083 syms = calloc(chain->nr, sizeof(*syms));
1085 fprintf(stderr, "Can't allocate memory for symbols\n");
1090 for (i = 0; i < chain->nr; i++) {
1091 u64 ip = chain->ips[i];
1092 struct dso *dso = NULL;
1095 if (ip >= PERF_CONTEXT_MAX) {
1101 case PERF_CONTEXT_HV:
1102 dso = hypervisor_dso;
1104 case PERF_CONTEXT_KERNEL:
1111 sym = resolve_symbol(thread, NULL, &dso, &ip);
1114 if (sort__has_parent && call__match(sym) &&
1116 entry->parent = sym;
1127 * collect histogram counts
1131 hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
1132 struct symbol *sym, u64 ip, struct ip_callchain *chain,
1133 char level, u64 count)
1135 struct rb_node **p = &hist.rb_node;
1136 struct rb_node *parent = NULL;
1137 struct hist_entry *he;
1138 struct symbol **syms = NULL;
1139 struct hist_entry entry = {
1148 .sorted_chain = RB_ROOT
1152 if ((sort__has_parent || callchain) && chain)
1153 syms = resolve_callchain(thread, map, chain, &entry);
1155 while (*p != NULL) {
1157 he = rb_entry(parent, struct hist_entry, rb_node);
1159 cmp = hist_entry__cmp(&entry, he);
1164 append_chain(&he->callchain, chain, syms);
1173 p = &(*p)->rb_right;
1176 he = malloc(sizeof(*he));
1181 callchain_init(&he->callchain);
1182 append_chain(&he->callchain, chain, syms);
1185 rb_link_node(&he->rb_node, parent, p);
1186 rb_insert_color(&he->rb_node, &hist);
1191 static void hist_entry__free(struct hist_entry *he)
1197 * collapse the histogram
1200 static struct rb_root collapse_hists;
1202 static void collapse__insert_entry(struct hist_entry *he)
1204 struct rb_node **p = &collapse_hists.rb_node;
1205 struct rb_node *parent = NULL;
1206 struct hist_entry *iter;
1209 while (*p != NULL) {
1211 iter = rb_entry(parent, struct hist_entry, rb_node);
1213 cmp = hist_entry__collapse(iter, he);
1216 iter->count += he->count;
1217 hist_entry__free(he);
1224 p = &(*p)->rb_right;
1227 rb_link_node(&he->rb_node, parent, p);
1228 rb_insert_color(&he->rb_node, &collapse_hists);
1231 static void collapse__resort(void)
1233 struct rb_node *next;
1234 struct hist_entry *n;
1236 if (!sort__need_collapse)
1239 next = rb_first(&hist);
1241 n = rb_entry(next, struct hist_entry, rb_node);
1242 next = rb_next(&n->rb_node);
1244 rb_erase(&n->rb_node, &hist);
1245 collapse__insert_entry(n);
1250 * reverse the map, sort on count.
1253 static struct rb_root output_hists;
1255 static void output__insert_entry(struct hist_entry *he, u64 min_callchain_hits)
1257 struct rb_node **p = &output_hists.rb_node;
1258 struct rb_node *parent = NULL;
1259 struct hist_entry *iter;
1262 callchain_param.sort(&he->sorted_chain, &he->callchain,
1263 min_callchain_hits, &callchain_param);
1265 while (*p != NULL) {
1267 iter = rb_entry(parent, struct hist_entry, rb_node);
1269 if (he->count > iter->count)
1272 p = &(*p)->rb_right;
1275 rb_link_node(&he->rb_node, parent, p);
1276 rb_insert_color(&he->rb_node, &output_hists);
1279 static void output__resort(u64 total_samples)
1281 struct rb_node *next;
1282 struct hist_entry *n;
1283 struct rb_root *tree = &hist;
1284 u64 min_callchain_hits;
1286 min_callchain_hits = total_samples * (callchain_param.min_percent / 100);
1288 if (sort__need_collapse)
1289 tree = &collapse_hists;
1291 next = rb_first(tree);
1294 n = rb_entry(next, struct hist_entry, rb_node);
1295 next = rb_next(&n->rb_node);
1297 rb_erase(&n->rb_node, tree);
1298 output__insert_entry(n, min_callchain_hits);
1302 static size_t output__fprintf(FILE *fp, u64 total_samples)
1304 struct hist_entry *pos;
1305 struct sort_entry *se;
1309 char *col_width = col_width_list_str;
1310 int raw_printing_style;
1312 raw_printing_style = !strcmp(pretty_printing_style, "raw");
1316 fprintf(fp, "# Samples: %Ld\n", (u64)total_samples);
1319 fprintf(fp, "# Overhead");
1320 if (show_nr_samples) {
1322 fprintf(fp, "%cSamples", *field_sep);
1324 fputs(" Samples ", fp);
1326 list_for_each_entry(se, &hist_entry__sort_list, list) {
1330 fprintf(fp, "%c%s", *field_sep, se->header);
1333 width = strlen(se->header);
1335 if (col_width_list_str) {
1337 *se->width = atoi(col_width);
1338 col_width = strchr(col_width, ',');
1343 width = *se->width = max(*se->width, width);
1345 fprintf(fp, " %*s", width, se->header);
1352 fprintf(fp, "# ........");
1353 if (show_nr_samples)
1354 fprintf(fp, " ..........");
1355 list_for_each_entry(se, &hist_entry__sort_list, list) {
1365 width = strlen(se->header);
1366 for (i = 0; i < width; i++)
1374 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
1375 pos = rb_entry(nd, struct hist_entry, rb_node);
1376 ret += hist_entry__fprintf(fp, pos, total_samples);
1379 if (sort_order == default_sort_order &&
1380 parent_pattern == default_parent_pattern) {
1382 fprintf(fp, "# (For a higher level overview, try: perf report --sort comm,dso)\n");
1387 free(rem_sq_bracket);
1390 perf_read_values_display(fp, &show_threads_values,
1391 raw_printing_style);
1396 static void register_idle_thread(void)
1398 struct thread *thread = threads__findnew(0);
1400 if (thread == NULL ||
1401 thread__set_comm(thread, "[idle]")) {
1402 fprintf(stderr, "problem inserting idle task.\n");
1407 static unsigned long total = 0,
1414 static int validate_chain(struct ip_callchain *chain, event_t *event)
1416 unsigned int chain_size;
1418 chain_size = event->header.size;
1419 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
1421 if (chain->nr*sizeof(u64) > chain_size)
1428 process_sample_event(event_t *event, unsigned long offset, unsigned long head)
1432 struct dso *dso = NULL;
1433 struct thread *thread = threads__findnew(event->ip.pid);
1434 u64 ip = event->ip.ip;
1436 struct map *map = NULL;
1437 void *more_data = event->ip.__more_data;
1438 struct ip_callchain *chain = NULL;
1441 if (sample_type & PERF_SAMPLE_PERIOD) {
1442 period = *(u64 *)more_data;
1443 more_data += sizeof(u64);
1446 dprintf("%p [%p]: PERF_EVENT_SAMPLE (IP, %d): %d: %p period: %Ld\n",
1447 (void *)(offset + head),
1448 (void *)(long)(event->header.size),
1454 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
1457 chain = (void *)more_data;
1459 dprintf("... chain: nr:%Lu\n", chain->nr);
1461 if (validate_chain(chain, event) < 0) {
1462 eprintf("call-chain problem with event, skipping it.\n");
1467 for (i = 0; i < chain->nr; i++)
1468 dprintf("..... %2d: %016Lx\n", i, chain->ips[i]);
1472 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1474 if (thread == NULL) {
1475 eprintf("problem processing %d event, skipping it.\n",
1476 event->header.type);
1480 if (comm_list && !strlist__has_entry(comm_list, thread->comm))
1483 cpumode = event->header.misc & PERF_EVENT_MISC_CPUMODE_MASK;
1485 if (cpumode == PERF_EVENT_MISC_KERNEL) {
1491 dprintf(" ...... dso: %s\n", dso->name);
1493 } else if (cpumode == PERF_EVENT_MISC_USER) {
1502 dso = hypervisor_dso;
1504 dprintf(" ...... dso: [hypervisor]\n");
1507 if (show & show_mask) {
1508 struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
1510 if (dso_list && dso && dso->name && !strlist__has_entry(dso_list, dso->name))
1513 if (sym_list && sym && !strlist__has_entry(sym_list, sym->name))
1516 if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
1517 eprintf("problem incrementing symbol count, skipping event\n");
1527 process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
1529 struct thread *thread = threads__findnew(event->mmap.pid);
1530 struct map *map = map__new(&event->mmap);
1532 dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
1533 (void *)(offset + head),
1534 (void *)(long)(event->header.size),
1536 (void *)(long)event->mmap.start,
1537 (void *)(long)event->mmap.len,
1538 (void *)(long)event->mmap.pgoff,
1539 event->mmap.filename);
1541 if (thread == NULL || map == NULL) {
1542 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
1546 thread__insert_map(thread, map);
1553 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
1555 struct thread *thread = threads__findnew(event->comm.pid);
1557 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
1558 (void *)(offset + head),
1559 (void *)(long)(event->header.size),
1560 event->comm.comm, event->comm.pid);
1562 if (thread == NULL ||
1563 thread__set_comm(thread, event->comm.comm)) {
1564 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
1573 process_task_event(event_t *event, unsigned long offset, unsigned long head)
1575 struct thread *thread = threads__findnew(event->fork.pid);
1576 struct thread *parent = threads__findnew(event->fork.ppid);
1578 dprintf("%p [%p]: PERF_EVENT_%s: (%d:%d):(%d:%d)\n",
1579 (void *)(offset + head),
1580 (void *)(long)(event->header.size),
1581 event->header.type == PERF_EVENT_FORK ? "FORK" : "EXIT",
1582 event->fork.pid, event->fork.tid,
1583 event->fork.ppid, event->fork.ptid);
1586 * A thread clone will have the same PID for both
1589 if (thread == parent)
1592 if (event->header.type == PERF_EVENT_EXIT)
1595 if (!thread || !parent || thread__fork(thread, parent)) {
1596 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
1605 process_lost_event(event_t *event, unsigned long offset, unsigned long head)
1607 dprintf("%p [%p]: PERF_EVENT_LOST: id:%Ld: lost:%Ld\n",
1608 (void *)(offset + head),
1609 (void *)(long)(event->header.size),
1613 total_lost += event->lost.lost;
1618 static void trace_event(event_t *event)
1620 unsigned char *raw_event = (void *)event;
1621 char *color = PERF_COLOR_BLUE;
1628 cdprintf("\n. ... raw event: size %d bytes\n", event->header.size);
1630 for (i = 0; i < event->header.size; i++) {
1631 if ((i & 15) == 0) {
1633 cdprintf(" %04x: ", i);
1636 cdprintf(" %02x", raw_event[i]);
1638 if (((i & 15) == 15) || i == event->header.size-1) {
1640 for (j = 0; j < 15-(i & 15); j++)
1642 for (j = 0; j < (i & 15); j++) {
1643 if (isprint(raw_event[i-15+j]))
1644 cdprintf("%c", raw_event[i-15+j]);
1654 static struct perf_header *header;
1656 static struct perf_counter_attr *perf_header__find_attr(u64 id)
1660 for (i = 0; i < header->attrs; i++) {
1661 struct perf_header_attr *attr = header->attr[i];
1664 for (j = 0; j < attr->ids; j++) {
1665 if (attr->id[j] == id)
1674 process_read_event(event_t *event, unsigned long offset, unsigned long head)
1676 struct perf_counter_attr *attr = perf_header__find_attr(event->read.id);
1679 char *name = attr ? __event_name(attr->type, attr->config)
1681 perf_read_values_add_value(&show_threads_values,
1682 event->read.pid, event->read.tid,
1688 dprintf("%p [%p]: PERF_EVENT_READ: %d %d %s %Lu\n",
1689 (void *)(offset + head),
1690 (void *)(long)(event->header.size),
1693 attr ? __event_name(attr->type, attr->config)
1701 process_event(event_t *event, unsigned long offset, unsigned long head)
1705 switch (event->header.type) {
1706 case PERF_EVENT_SAMPLE:
1707 return process_sample_event(event, offset, head);
1709 case PERF_EVENT_MMAP:
1710 return process_mmap_event(event, offset, head);
1712 case PERF_EVENT_COMM:
1713 return process_comm_event(event, offset, head);
1715 case PERF_EVENT_FORK:
1716 case PERF_EVENT_EXIT:
1717 return process_task_event(event, offset, head);
1719 case PERF_EVENT_LOST:
1720 return process_lost_event(event, offset, head);
1722 case PERF_EVENT_READ:
1723 return process_read_event(event, offset, head);
1726 * We dont process them right now but they are fine:
1729 case PERF_EVENT_THROTTLE:
1730 case PERF_EVENT_UNTHROTTLE:
1740 static u64 perf_header__sample_type(void)
1742 u64 sample_type = 0;
1745 for (i = 0; i < header->attrs; i++) {
1746 struct perf_header_attr *attr = header->attr[i];
1749 sample_type = attr->attr.sample_type;
1750 else if (sample_type != attr->attr.sample_type)
1751 die("non matching sample_type");
1757 static int __cmd_report(void)
1759 int ret, rc = EXIT_FAILURE;
1760 unsigned long offset = 0;
1761 unsigned long head, shift;
1767 register_idle_thread();
1770 perf_read_values_init(&show_threads_values);
1772 input = open(input_name, O_RDONLY);
1774 fprintf(stderr, " failed to open file: %s", input_name);
1775 if (!strcmp(input_name, "perf.data"))
1776 fprintf(stderr, " (try 'perf record' first)");
1777 fprintf(stderr, "\n");
1781 ret = fstat(input, &stat);
1783 perror("failed to stat file");
1787 if (!stat.st_size) {
1788 fprintf(stderr, "zero-sized file, nothing to do!\n");
1792 header = perf_header__read(input);
1793 head = header->data_offset;
1795 sample_type = perf_header__sample_type();
1797 if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
1798 if (sort__has_parent) {
1799 fprintf(stderr, "selected --sort parent, but no"
1800 " callchain data. Did you call"
1801 " perf record without -g?\n");
1805 fprintf(stderr, "selected -c but no callchain data."
1806 " Did you call perf record without"
1810 } else if (callchain_param.mode != CHAIN_NONE && !callchain) {
1812 if (register_callchain_param(&callchain_param) < 0) {
1813 fprintf(stderr, "Can't register callchain"
1819 if (load_kernel() < 0) {
1820 perror("failed to load kernel symbols");
1821 return EXIT_FAILURE;
1825 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
1826 perror("failed to get the current directory");
1827 return EXIT_FAILURE;
1829 cwdlen = strlen(cwd);
1835 shift = page_size * (head / page_size);
1840 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1841 MAP_SHARED, input, offset);
1842 if (buf == MAP_FAILED) {
1843 perror("failed to mmap file");
1848 event = (event_t *)(buf + head);
1850 size = event->header.size;
1854 if (head + event->header.size >= page_size * mmap_window) {
1857 shift = page_size * (head / page_size);
1859 ret = munmap(buf, page_size * mmap_window);
1867 size = event->header.size;
1869 dprintf("\n%p [%p]: event: %d\n",
1870 (void *)(offset + head),
1871 (void *)(long)event->header.size,
1872 event->header.type);
1874 if (!size || process_event(event, offset, head) < 0) {
1876 dprintf("%p [%p]: skipping unknown header type: %d\n",
1877 (void *)(offset + head),
1878 (void *)(long)(event->header.size),
1879 event->header.type);
1884 * assume we lost track of the stream, check alignment, and
1885 * increment a single u64 in the hope to catch on again 'soon'.
1888 if (unlikely(head & 7))
1896 if (offset + head >= header->data_offset + header->data_size)
1899 if (offset + head < (unsigned long)stat.st_size)
1906 dprintf(" IP events: %10ld\n", total);
1907 dprintf(" mmap events: %10ld\n", total_mmap);
1908 dprintf(" comm events: %10ld\n", total_comm);
1909 dprintf(" fork events: %10ld\n", total_fork);
1910 dprintf(" lost events: %10ld\n", total_lost);
1911 dprintf(" unknown events: %10ld\n", total_unknown);
1917 threads__fprintf(stdout);
1920 dsos__fprintf(stdout);
1923 output__resort(total);
1924 output__fprintf(stdout, total);
1927 perf_read_values_destroy(&show_threads_values);
1933 parse_callchain_opt(const struct option *opt __used, const char *arg,
1944 tok = strtok((char *)arg, ",");
1948 /* get the output mode */
1949 if (!strncmp(tok, "graph", strlen(arg)))
1950 callchain_param.mode = CHAIN_GRAPH_ABS;
1952 else if (!strncmp(tok, "flat", strlen(arg)))
1953 callchain_param.mode = CHAIN_FLAT;
1955 else if (!strncmp(tok, "fractal", strlen(arg)))
1956 callchain_param.mode = CHAIN_GRAPH_REL;
1958 else if (!strncmp(tok, "none", strlen(arg))) {
1959 callchain_param.mode = CHAIN_NONE;
1968 /* get the min percentage */
1969 tok = strtok(NULL, ",");
1973 callchain_param.min_percent = strtod(tok, &endptr);
1978 if (register_callchain_param(&callchain_param) < 0) {
1979 fprintf(stderr, "Can't register callchain params\n");
1985 static const char * const report_usage[] = {
1986 "perf report [<options>] <command>",
1990 static const struct option options[] = {
1991 OPT_STRING('i', "input", &input_name, "file",
1993 OPT_BOOLEAN('v', "verbose", &verbose,
1994 "be more verbose (show symbol address, etc)"),
1995 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1996 "dump raw trace in ASCII"),
1997 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
1998 OPT_BOOLEAN('m', "modules", &modules,
1999 "load module symbols - WARNING: use only with -k and LIVE kernel"),
2000 OPT_BOOLEAN('n', "show-nr-samples", &show_nr_samples,
2001 "Show a column with the number of samples"),
2002 OPT_BOOLEAN('T', "threads", &show_threads,
2003 "Show per-thread event counters"),
2004 OPT_STRING(0, "pretty", &pretty_printing_style, "key",
2005 "pretty printing style key: normal raw"),
2006 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
2007 "sort by key(s): pid, comm, dso, symbol, parent"),
2008 OPT_BOOLEAN('P', "full-paths", &full_paths,
2009 "Don't shorten the pathnames taking into account the cwd"),
2010 OPT_STRING('p', "parent", &parent_pattern, "regex",
2011 "regex filter to identify parent, see: '--sort parent'"),
2012 OPT_BOOLEAN('x', "exclude-other", &exclude_other,
2013 "Only display entries with parent-match"),
2014 OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
2015 "Display callchains using output_type and min percent threshold. "
2016 "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
2017 OPT_STRING('d', "dsos", &dso_list_str, "dso[,dso...]",
2018 "only consider symbols in these dsos"),
2019 OPT_STRING('C', "comms", &comm_list_str, "comm[,comm...]",
2020 "only consider symbols in these comms"),
2021 OPT_STRING('S', "symbols", &sym_list_str, "symbol[,symbol...]",
2022 "only consider these symbols"),
2023 OPT_STRING('w', "column-widths", &col_width_list_str,
2025 "don't try to adjust column width, use these fixed values"),
2026 OPT_STRING('t', "field-separator", &field_sep, "separator",
2027 "separator for columns, no spaces will be added between "
2028 "columns '.' is reserved."),
2032 static void setup_sorting(void)
2034 char *tmp, *tok, *str = strdup(sort_order);
2036 for (tok = strtok_r(str, ", ", &tmp);
2037 tok; tok = strtok_r(NULL, ", ", &tmp)) {
2038 if (sort_dimension__add(tok) < 0) {
2039 error("Unknown --sort key: `%s'", tok);
2040 usage_with_options(report_usage, options);
2047 static void setup_list(struct strlist **list, const char *list_str,
2048 struct sort_entry *se, const char *list_name,
2052 *list = strlist__new(true, list_str);
2054 fprintf(stderr, "problems parsing %s list\n",
2058 if (strlist__nr_entries(*list) == 1) {
2059 fprintf(fp, "# %s: %s\n", list_name,
2060 strlist__entry(*list, 0)->s);
2066 int cmd_report(int argc, const char **argv, const char *prefix __used)
2070 page_size = getpagesize();
2072 argc = parse_options(argc, argv, options, report_usage, 0);
2076 if (parent_pattern != default_parent_pattern) {
2077 sort_dimension__add("parent");
2078 sort_parent.elide = 1;
2083 * Any (unrecognized) arguments left?
2086 usage_with_options(report_usage, options);
2090 setup_list(&dso_list, dso_list_str, &sort_dso, "dso", stdout);
2091 setup_list(&comm_list, comm_list_str, &sort_comm, "comm", stdout);
2092 setup_list(&sym_list, sym_list_str, &sort_sym, "symbol", stdout);
2094 if (field_sep && *field_sep == '.') {
2095 fputs("'.' is the only non valid --field-separator argument\n",
2100 return __cmd_report();