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"
28 #include "util/thread.h"
34 static char const *input_name = "perf.data";
36 static char default_sort_order[] = "comm,dso,symbol";
37 static char *sort_order = default_sort_order;
38 static char *dso_list_str, *comm_list_str, *sym_list_str,
40 static struct strlist *dso_list, *comm_list, *sym_list;
41 static char *field_sep;
44 static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
46 static int dump_trace = 0;
47 #define dprintf(x...) do { if (dump_trace) printf(x); } while (0)
48 #define cdprintf(x...) do { if (dump_trace) color_fprintf(stdout, color, x); } while (0)
50 static int full_paths;
51 static int show_nr_samples;
53 static int show_threads;
54 static struct perf_read_values show_threads_values;
56 static char default_pretty_printing_style[] = "normal";
57 static char *pretty_printing_style = default_pretty_printing_style;
59 static unsigned long page_size;
60 static unsigned long mmap_window = 32;
62 static char default_parent_pattern[] = "^sys_|^do_page_fault";
63 static char *parent_pattern = default_parent_pattern;
64 static regex_t parent_regex;
66 static int exclude_other = 1;
68 static char callchain_default_opt[] = "fractal,0.5";
72 static char __cwd[PATH_MAX];
73 static char *cwd = __cwd;
76 static struct rb_root threads;
77 static struct thread *last_match;
80 struct callchain_param callchain_param = {
81 .mode = CHAIN_GRAPH_REL,
85 static u64 sample_type;
87 static int repsep_fprintf(FILE *fp, const char *fmt, ...)
94 n = vfprintf(fp, fmt, ap);
97 n = vasprintf(&bf, fmt, ap);
102 sep = strchr(sep, *field_sep);
115 static unsigned int dsos__col_width,
120 * histogram, sorted on item, collects counts
123 static struct rb_root hist;
126 struct rb_node rb_node;
128 struct thread *thread;
132 struct symbol *parent;
135 struct callchain_node callchain;
136 struct rb_root sorted_chain;
142 * configurable sorting bits
146 struct list_head list;
150 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
151 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
152 size_t (*print)(FILE *fp, struct hist_entry *, unsigned int width);
157 static int64_t cmp_null(void *l, void *r)
170 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
172 return right->thread->pid - left->thread->pid;
176 sort__thread_print(FILE *fp, struct hist_entry *self, unsigned int width)
178 return repsep_fprintf(fp, "%*s:%5d", width - 6,
179 self->thread->comm ?: "", self->thread->pid);
182 static struct sort_entry sort_thread = {
183 .header = "Command: Pid",
184 .cmp = sort__thread_cmp,
185 .print = sort__thread_print,
186 .width = &threads__col_width,
192 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
194 return right->thread->pid - left->thread->pid;
198 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
200 char *comm_l = left->thread->comm;
201 char *comm_r = right->thread->comm;
203 if (!comm_l || !comm_r)
204 return cmp_null(comm_l, comm_r);
206 return strcmp(comm_l, comm_r);
210 sort__comm_print(FILE *fp, struct hist_entry *self, unsigned int width)
212 return repsep_fprintf(fp, "%*s", width, self->thread->comm);
215 static struct sort_entry sort_comm = {
217 .cmp = sort__comm_cmp,
218 .collapse = sort__comm_collapse,
219 .print = sort__comm_print,
220 .width = &comms__col_width,
226 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
228 struct dso *dso_l = left->dso;
229 struct dso *dso_r = right->dso;
231 if (!dso_l || !dso_r)
232 return cmp_null(dso_l, dso_r);
234 return strcmp(dso_l->name, dso_r->name);
238 sort__dso_print(FILE *fp, struct hist_entry *self, unsigned int width)
241 return repsep_fprintf(fp, "%-*s", width, self->dso->name);
243 return repsep_fprintf(fp, "%*llx", width, (u64)self->ip);
246 static struct sort_entry sort_dso = {
247 .header = "Shared Object",
248 .cmp = sort__dso_cmp,
249 .print = sort__dso_print,
250 .width = &dsos__col_width,
256 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
260 if (left->sym == right->sym)
263 ip_l = left->sym ? left->sym->start : left->ip;
264 ip_r = right->sym ? right->sym->start : right->ip;
266 return (int64_t)(ip_r - ip_l);
270 sort__sym_print(FILE *fp, struct hist_entry *self, unsigned int width __used)
275 ret += repsep_fprintf(fp, "%#018llx %c ", (u64)self->ip,
276 dso__symtab_origin(self->dso));
278 ret += repsep_fprintf(fp, "[%c] ", self->level);
280 ret += repsep_fprintf(fp, "%s", self->sym->name);
282 if (self->sym->module)
283 ret += repsep_fprintf(fp, "\t[%s]",
284 self->sym->module->name);
286 ret += repsep_fprintf(fp, "%#016llx", (u64)self->ip);
292 static struct sort_entry sort_sym = {
294 .cmp = sort__sym_cmp,
295 .print = sort__sym_print,
301 sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
303 struct symbol *sym_l = left->parent;
304 struct symbol *sym_r = right->parent;
306 if (!sym_l || !sym_r)
307 return cmp_null(sym_l, sym_r);
309 return strcmp(sym_l->name, sym_r->name);
313 sort__parent_print(FILE *fp, struct hist_entry *self, unsigned int width)
315 return repsep_fprintf(fp, "%-*s", width,
316 self->parent ? self->parent->name : "[other]");
319 static unsigned int parent_symbol__col_width;
321 static struct sort_entry sort_parent = {
322 .header = "Parent symbol",
323 .cmp = sort__parent_cmp,
324 .print = sort__parent_print,
325 .width = &parent_symbol__col_width,
328 static int sort__need_collapse = 0;
329 static int sort__has_parent = 0;
331 struct sort_dimension {
333 struct sort_entry *entry;
337 static struct sort_dimension sort_dimensions[] = {
338 { .name = "pid", .entry = &sort_thread, },
339 { .name = "comm", .entry = &sort_comm, },
340 { .name = "dso", .entry = &sort_dso, },
341 { .name = "symbol", .entry = &sort_sym, },
342 { .name = "parent", .entry = &sort_parent, },
345 static LIST_HEAD(hist_entry__sort_list);
347 static int sort_dimension__add(const char *tok)
351 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
352 struct sort_dimension *sd = &sort_dimensions[i];
357 if (strncasecmp(tok, sd->name, strlen(tok)))
360 if (sd->entry->collapse)
361 sort__need_collapse = 1;
363 if (sd->entry == &sort_parent) {
364 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
368 regerror(ret, &parent_regex, err, sizeof(err));
369 fprintf(stderr, "Invalid regex: %s\n%s",
370 parent_pattern, err);
373 sort__has_parent = 1;
376 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
386 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
388 struct sort_entry *se;
391 list_for_each_entry(se, &hist_entry__sort_list, list) {
392 cmp = se->cmp(left, right);
401 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
403 struct sort_entry *se;
406 list_for_each_entry(se, &hist_entry__sort_list, list) {
407 int64_t (*f)(struct hist_entry *, struct hist_entry *);
409 f = se->collapse ?: se->cmp;
411 cmp = f(left, right);
419 static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask)
424 ret += fprintf(fp, "%s", " ");
426 for (i = 0; i < depth; i++)
427 if (depth_mask & (1 << i))
428 ret += fprintf(fp, "| ");
430 ret += fprintf(fp, " ");
432 ret += fprintf(fp, "\n");
437 ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, int depth,
438 int depth_mask, int count, u64 total_samples,
444 ret += fprintf(fp, "%s", " ");
445 for (i = 0; i < depth; i++) {
446 if (depth_mask & (1 << i))
447 ret += fprintf(fp, "|");
449 ret += fprintf(fp, " ");
450 if (!count && i == depth - 1) {
453 percent = hits * 100.0 / total_samples;
454 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
456 ret += fprintf(fp, "%s", " ");
459 ret += fprintf(fp, "%s\n", chain->sym->name);
461 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
466 static struct symbol *rem_sq_bracket;
467 static struct callchain_list rem_hits;
469 static void init_rem_hits(void)
471 rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
472 if (!rem_sq_bracket) {
473 fprintf(stderr, "Not enough memory to display remaining hits\n");
477 strcpy(rem_sq_bracket->name, "[...]");
478 rem_hits.sym = rem_sq_bracket;
482 callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
483 u64 total_samples, int depth, int depth_mask)
485 struct rb_node *node, *next;
486 struct callchain_node *child;
487 struct callchain_list *chain;
488 int new_depth_mask = depth_mask;
494 if (callchain_param.mode == CHAIN_GRAPH_REL)
495 new_total = self->children_hit;
497 new_total = total_samples;
499 remaining = new_total;
501 node = rb_first(&self->rb_root);
505 child = rb_entry(node, struct callchain_node, rb_node);
506 cumul = cumul_hits(child);
510 * The depth mask manages the output of pipes that show
511 * the depth. We don't want to keep the pipes of the current
512 * level for the last child of this depth.
513 * Except if we have remaining filtered hits. They will
514 * supersede the last child
516 next = rb_next(node);
517 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
518 new_depth_mask &= ~(1 << (depth - 1));
521 * But we keep the older depth mask for the line seperator
522 * to keep the level link until we reach the last child
524 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask);
526 list_for_each_entry(chain, &child->val, list) {
527 if (chain->ip >= PERF_CONTEXT_MAX)
529 ret += ipchain__fprintf_graph(fp, chain, depth,
534 ret += callchain__fprintf_graph(fp, child, new_total,
536 new_depth_mask | (1 << depth));
540 if (callchain_param.mode == CHAIN_GRAPH_REL &&
541 remaining && remaining != new_total) {
546 new_depth_mask &= ~(1 << (depth - 1));
548 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
549 new_depth_mask, 0, new_total,
557 callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
560 struct callchain_list *chain;
566 ret += callchain__fprintf_flat(fp, self->parent, total_samples);
569 list_for_each_entry(chain, &self->val, list) {
570 if (chain->ip >= PERF_CONTEXT_MAX)
573 ret += fprintf(fp, " %s\n", chain->sym->name);
575 ret += fprintf(fp, " %p\n",
576 (void *)(long)chain->ip);
583 hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
586 struct rb_node *rb_node;
587 struct callchain_node *chain;
590 rb_node = rb_first(&self->sorted_chain);
594 chain = rb_entry(rb_node, struct callchain_node, rb_node);
595 percent = chain->hit * 100.0 / total_samples;
596 switch (callchain_param.mode) {
598 ret += percent_color_fprintf(fp, " %6.2f%%\n",
600 ret += callchain__fprintf_flat(fp, chain, total_samples);
602 case CHAIN_GRAPH_ABS: /* Falldown */
603 case CHAIN_GRAPH_REL:
604 ret += callchain__fprintf_graph(fp, chain,
605 total_samples, 1, 1);
610 ret += fprintf(fp, "\n");
611 rb_node = rb_next(rb_node);
619 hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
621 struct sort_entry *se;
624 if (exclude_other && !self->parent)
628 ret = percent_color_fprintf(fp,
629 field_sep ? "%.2f" : " %6.2f%%",
630 (self->count * 100.0) / total_samples);
632 ret = fprintf(fp, field_sep ? "%lld" : "%12lld ", self->count);
634 if (show_nr_samples) {
636 fprintf(fp, "%c%lld", *field_sep, self->count);
638 fprintf(fp, "%11lld", self->count);
641 list_for_each_entry(se, &hist_entry__sort_list, list) {
645 fprintf(fp, "%s", field_sep ?: " ");
646 ret += se->print(fp, self, se->width ? *se->width : 0);
649 ret += fprintf(fp, "\n");
652 hist_entry_callchain__fprintf(fp, self, total_samples);
661 static void dso__calc_col_width(struct dso *self)
663 if (!col_width_list_str && !field_sep &&
664 (!dso_list || strlist__has_entry(dso_list, self->name))) {
665 unsigned int slen = strlen(self->name);
666 if (slen > dsos__col_width)
667 dsos__col_width = slen;
670 self->slen_calculated = 1;
673 static struct symbol *
674 resolve_symbol(struct thread *thread, struct map **mapp,
675 struct dso **dsop, u64 *ipp)
677 struct dso *dso = dsop ? *dsop : NULL;
678 struct map *map = mapp ? *mapp : NULL;
690 map = thread__find_map(thread, ip);
693 * We have to do this here as we may have a dso
694 * with no symbol hit that has a name longer than
695 * the ones with symbols sampled.
697 if (!sort_dso.elide && !map->dso->slen_calculated)
698 dso__calc_col_width(map->dso);
703 ip = map->map_ip(map, ip);
708 * If this is outside of all known maps,
709 * and is a negative address, try to look it
710 * up in the kernel dso, as it might be a
711 * vsyscall (which executes in user-mode):
713 if ((long long)ip < 0)
716 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
717 dprintf(" ...... map: %Lx -> %Lx\n", *ipp, ip);
726 return dso->find_symbol(dso, ip);
729 static int call__match(struct symbol *sym)
731 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
737 static struct symbol **
738 resolve_callchain(struct thread *thread, struct map *map __used,
739 struct ip_callchain *chain, struct hist_entry *entry)
741 u64 context = PERF_CONTEXT_MAX;
742 struct symbol **syms = NULL;
746 syms = calloc(chain->nr, sizeof(*syms));
748 fprintf(stderr, "Can't allocate memory for symbols\n");
753 for (i = 0; i < chain->nr; i++) {
754 u64 ip = chain->ips[i];
755 struct dso *dso = NULL;
758 if (ip >= PERF_CONTEXT_MAX) {
764 case PERF_CONTEXT_HV:
765 dso = hypervisor_dso;
767 case PERF_CONTEXT_KERNEL:
774 sym = resolve_symbol(thread, NULL, &dso, &ip);
777 if (sort__has_parent && call__match(sym) &&
790 * collect histogram counts
794 hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
795 struct symbol *sym, u64 ip, struct ip_callchain *chain,
796 char level, u64 count)
798 struct rb_node **p = &hist.rb_node;
799 struct rb_node *parent = NULL;
800 struct hist_entry *he;
801 struct symbol **syms = NULL;
802 struct hist_entry entry = {
811 .sorted_chain = RB_ROOT
815 if ((sort__has_parent || callchain) && chain)
816 syms = resolve_callchain(thread, map, chain, &entry);
820 he = rb_entry(parent, struct hist_entry, rb_node);
822 cmp = hist_entry__cmp(&entry, he);
827 append_chain(&he->callchain, chain, syms);
839 he = malloc(sizeof(*he));
844 callchain_init(&he->callchain);
845 append_chain(&he->callchain, chain, syms);
848 rb_link_node(&he->rb_node, parent, p);
849 rb_insert_color(&he->rb_node, &hist);
854 static void hist_entry__free(struct hist_entry *he)
860 * collapse the histogram
863 static struct rb_root collapse_hists;
865 static void collapse__insert_entry(struct hist_entry *he)
867 struct rb_node **p = &collapse_hists.rb_node;
868 struct rb_node *parent = NULL;
869 struct hist_entry *iter;
874 iter = rb_entry(parent, struct hist_entry, rb_node);
876 cmp = hist_entry__collapse(iter, he);
879 iter->count += he->count;
880 hist_entry__free(he);
890 rb_link_node(&he->rb_node, parent, p);
891 rb_insert_color(&he->rb_node, &collapse_hists);
894 static void collapse__resort(void)
896 struct rb_node *next;
897 struct hist_entry *n;
899 if (!sort__need_collapse)
902 next = rb_first(&hist);
904 n = rb_entry(next, struct hist_entry, rb_node);
905 next = rb_next(&n->rb_node);
907 rb_erase(&n->rb_node, &hist);
908 collapse__insert_entry(n);
913 * reverse the map, sort on count.
916 static struct rb_root output_hists;
918 static void output__insert_entry(struct hist_entry *he, u64 min_callchain_hits)
920 struct rb_node **p = &output_hists.rb_node;
921 struct rb_node *parent = NULL;
922 struct hist_entry *iter;
925 callchain_param.sort(&he->sorted_chain, &he->callchain,
926 min_callchain_hits, &callchain_param);
930 iter = rb_entry(parent, struct hist_entry, rb_node);
932 if (he->count > iter->count)
938 rb_link_node(&he->rb_node, parent, p);
939 rb_insert_color(&he->rb_node, &output_hists);
942 static void output__resort(u64 total_samples)
944 struct rb_node *next;
945 struct hist_entry *n;
946 struct rb_root *tree = &hist;
947 u64 min_callchain_hits;
949 min_callchain_hits = total_samples * (callchain_param.min_percent / 100);
951 if (sort__need_collapse)
952 tree = &collapse_hists;
954 next = rb_first(tree);
957 n = rb_entry(next, struct hist_entry, rb_node);
958 next = rb_next(&n->rb_node);
960 rb_erase(&n->rb_node, tree);
961 output__insert_entry(n, min_callchain_hits);
965 static size_t output__fprintf(FILE *fp, u64 total_samples)
967 struct hist_entry *pos;
968 struct sort_entry *se;
972 char *col_width = col_width_list_str;
973 int raw_printing_style;
975 raw_printing_style = !strcmp(pretty_printing_style, "raw");
979 fprintf(fp, "# Samples: %Ld\n", (u64)total_samples);
982 fprintf(fp, "# Overhead");
983 if (show_nr_samples) {
985 fprintf(fp, "%cSamples", *field_sep);
987 fputs(" Samples ", fp);
989 list_for_each_entry(se, &hist_entry__sort_list, list) {
993 fprintf(fp, "%c%s", *field_sep, se->header);
996 width = strlen(se->header);
998 if (col_width_list_str) {
1000 *se->width = atoi(col_width);
1001 col_width = strchr(col_width, ',');
1006 width = *se->width = max(*se->width, width);
1008 fprintf(fp, " %*s", width, se->header);
1015 fprintf(fp, "# ........");
1016 if (show_nr_samples)
1017 fprintf(fp, " ..........");
1018 list_for_each_entry(se, &hist_entry__sort_list, list) {
1028 width = strlen(se->header);
1029 for (i = 0; i < width; i++)
1037 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
1038 pos = rb_entry(nd, struct hist_entry, rb_node);
1039 ret += hist_entry__fprintf(fp, pos, total_samples);
1042 if (sort_order == default_sort_order &&
1043 parent_pattern == default_parent_pattern) {
1045 fprintf(fp, "# (For a higher level overview, try: perf report --sort comm,dso)\n");
1050 free(rem_sq_bracket);
1053 perf_read_values_display(fp, &show_threads_values,
1054 raw_printing_style);
1059 static void register_idle_thread(void)
1061 struct thread *thread = threads__findnew(0, &threads, &last_match);
1063 if (thread == NULL ||
1064 thread__set_comm(thread, "[idle]")) {
1065 fprintf(stderr, "problem inserting idle task.\n");
1070 static unsigned long total = 0,
1077 static int validate_chain(struct ip_callchain *chain, event_t *event)
1079 unsigned int chain_size;
1081 chain_size = event->header.size;
1082 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
1084 if (chain->nr*sizeof(u64) > chain_size)
1091 process_sample_event(event_t *event, unsigned long offset, unsigned long head)
1095 struct dso *dso = NULL;
1096 struct thread *thread;
1097 u64 ip = event->ip.ip;
1099 struct map *map = NULL;
1100 void *more_data = event->ip.__more_data;
1101 struct ip_callchain *chain = NULL;
1104 thread = threads__findnew(event->ip.pid, &threads, &last_match);
1106 if (sample_type & PERF_SAMPLE_PERIOD) {
1107 period = *(u64 *)more_data;
1108 more_data += sizeof(u64);
1111 dprintf("%p [%p]: PERF_EVENT_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
1112 (void *)(offset + head),
1113 (void *)(long)(event->header.size),
1115 event->ip.pid, event->ip.tid,
1119 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
1122 chain = (void *)more_data;
1124 dprintf("... chain: nr:%Lu\n", chain->nr);
1126 if (validate_chain(chain, event) < 0) {
1127 eprintf("call-chain problem with event, skipping it.\n");
1132 for (i = 0; i < chain->nr; i++)
1133 dprintf("..... %2d: %016Lx\n", i, chain->ips[i]);
1137 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1139 if (thread == NULL) {
1140 eprintf("problem processing %d event, skipping it.\n",
1141 event->header.type);
1145 if (comm_list && !strlist__has_entry(comm_list, thread->comm))
1148 cpumode = event->header.misc & PERF_EVENT_MISC_CPUMODE_MASK;
1150 if (cpumode == PERF_EVENT_MISC_KERNEL) {
1156 dprintf(" ...... dso: %s\n", dso->name);
1158 } else if (cpumode == PERF_EVENT_MISC_USER) {
1167 dso = hypervisor_dso;
1169 dprintf(" ...... dso: [hypervisor]\n");
1172 if (show & show_mask) {
1173 struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
1175 if (dso_list && (!dso || !dso->name ||
1176 !strlist__has_entry(dso_list, dso->name)))
1179 if (sym_list && (!sym || !strlist__has_entry(sym_list, sym->name)))
1182 if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
1183 eprintf("problem incrementing symbol count, skipping event\n");
1193 process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
1195 struct thread *thread;
1196 struct map *map = map__new(&event->mmap, cwd, cwdlen);
1198 thread = threads__findnew(event->mmap.pid, &threads, &last_match);
1200 dprintf("%p [%p]: PERF_EVENT_MMAP %d/%d: [%p(%p) @ %p]: %s\n",
1201 (void *)(offset + head),
1202 (void *)(long)(event->header.size),
1205 (void *)(long)event->mmap.start,
1206 (void *)(long)event->mmap.len,
1207 (void *)(long)event->mmap.pgoff,
1208 event->mmap.filename);
1210 if (thread == NULL || map == NULL) {
1211 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
1215 thread__insert_map(thread, map);
1222 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
1224 struct thread *thread;
1226 thread = threads__findnew(event->comm.pid, &threads, &last_match);
1228 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
1229 (void *)(offset + head),
1230 (void *)(long)(event->header.size),
1231 event->comm.comm, event->comm.pid);
1233 if (thread == NULL ||
1234 thread__set_comm(thread, event->comm.comm)) {
1235 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
1244 process_task_event(event_t *event, unsigned long offset, unsigned long head)
1246 struct thread *thread;
1247 struct thread *parent;
1249 thread = threads__findnew(event->fork.pid, &threads, &last_match);
1250 parent = threads__findnew(event->fork.ppid, &threads, &last_match);
1252 dprintf("%p [%p]: PERF_EVENT_%s: (%d:%d):(%d:%d)\n",
1253 (void *)(offset + head),
1254 (void *)(long)(event->header.size),
1255 event->header.type == PERF_EVENT_FORK ? "FORK" : "EXIT",
1256 event->fork.pid, event->fork.tid,
1257 event->fork.ppid, event->fork.ptid);
1260 * A thread clone will have the same PID for both
1263 if (thread == parent)
1266 if (event->header.type == PERF_EVENT_EXIT)
1269 if (!thread || !parent || thread__fork(thread, parent)) {
1270 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
1279 process_lost_event(event_t *event, unsigned long offset, unsigned long head)
1281 dprintf("%p [%p]: PERF_EVENT_LOST: id:%Ld: lost:%Ld\n",
1282 (void *)(offset + head),
1283 (void *)(long)(event->header.size),
1287 total_lost += event->lost.lost;
1292 static void trace_event(event_t *event)
1294 unsigned char *raw_event = (void *)event;
1295 const char *color = PERF_COLOR_BLUE;
1302 cdprintf("\n. ... raw event: size %d bytes\n", event->header.size);
1304 for (i = 0; i < event->header.size; i++) {
1305 if ((i & 15) == 0) {
1307 cdprintf(" %04x: ", i);
1310 cdprintf(" %02x", raw_event[i]);
1312 if (((i & 15) == 15) || i == event->header.size-1) {
1314 for (j = 0; j < 15-(i & 15); j++)
1316 for (j = 0; j < (i & 15); j++) {
1317 if (isprint(raw_event[i-15+j]))
1318 cdprintf("%c", raw_event[i-15+j]);
1328 static struct perf_header *header;
1330 static struct perf_counter_attr *perf_header__find_attr(u64 id)
1334 for (i = 0; i < header->attrs; i++) {
1335 struct perf_header_attr *attr = header->attr[i];
1338 for (j = 0; j < attr->ids; j++) {
1339 if (attr->id[j] == id)
1348 process_read_event(event_t *event, unsigned long offset, unsigned long head)
1350 struct perf_counter_attr *attr = perf_header__find_attr(event->read.id);
1353 const char *name = attr ? __event_name(attr->type, attr->config)
1355 perf_read_values_add_value(&show_threads_values,
1356 event->read.pid, event->read.tid,
1362 dprintf("%p [%p]: PERF_EVENT_READ: %d %d %s %Lu\n",
1363 (void *)(offset + head),
1364 (void *)(long)(event->header.size),
1367 attr ? __event_name(attr->type, attr->config)
1375 process_event(event_t *event, unsigned long offset, unsigned long head)
1379 switch (event->header.type) {
1380 case PERF_EVENT_SAMPLE:
1381 return process_sample_event(event, offset, head);
1383 case PERF_EVENT_MMAP:
1384 return process_mmap_event(event, offset, head);
1386 case PERF_EVENT_COMM:
1387 return process_comm_event(event, offset, head);
1389 case PERF_EVENT_FORK:
1390 case PERF_EVENT_EXIT:
1391 return process_task_event(event, offset, head);
1393 case PERF_EVENT_LOST:
1394 return process_lost_event(event, offset, head);
1396 case PERF_EVENT_READ:
1397 return process_read_event(event, offset, head);
1400 * We dont process them right now but they are fine:
1403 case PERF_EVENT_THROTTLE:
1404 case PERF_EVENT_UNTHROTTLE:
1414 static u64 perf_header__sample_type(void)
1419 for (i = 0; i < header->attrs; i++) {
1420 struct perf_header_attr *attr = header->attr[i];
1423 type = attr->attr.sample_type;
1424 else if (type != attr->attr.sample_type)
1425 die("non matching sample_type");
1431 static int __cmd_report(void)
1433 int ret, rc = EXIT_FAILURE;
1434 unsigned long offset = 0;
1435 unsigned long head, shift;
1436 struct stat input_stat;
1441 register_idle_thread();
1444 perf_read_values_init(&show_threads_values);
1446 input = open(input_name, O_RDONLY);
1448 fprintf(stderr, " failed to open file: %s", input_name);
1449 if (!strcmp(input_name, "perf.data"))
1450 fprintf(stderr, " (try 'perf record' first)");
1451 fprintf(stderr, "\n");
1455 ret = fstat(input, &input_stat);
1457 perror("failed to stat file");
1461 if (!input_stat.st_size) {
1462 fprintf(stderr, "zero-sized file, nothing to do!\n");
1466 header = perf_header__read(input);
1467 head = header->data_offset;
1469 sample_type = perf_header__sample_type();
1471 if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
1472 if (sort__has_parent) {
1473 fprintf(stderr, "selected --sort parent, but no"
1474 " callchain data. Did you call"
1475 " perf record without -g?\n");
1479 fprintf(stderr, "selected -c but no callchain data."
1480 " Did you call perf record without"
1484 } else if (callchain_param.mode != CHAIN_NONE && !callchain) {
1486 if (register_callchain_param(&callchain_param) < 0) {
1487 fprintf(stderr, "Can't register callchain"
1493 if (load_kernel() < 0) {
1494 perror("failed to load kernel symbols");
1495 return EXIT_FAILURE;
1499 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
1500 perror("failed to get the current directory");
1501 return EXIT_FAILURE;
1503 cwdlen = strlen(cwd);
1509 shift = page_size * (head / page_size);
1514 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1515 MAP_SHARED, input, offset);
1516 if (buf == MAP_FAILED) {
1517 perror("failed to mmap file");
1522 event = (event_t *)(buf + head);
1524 size = event->header.size;
1528 if (head + event->header.size >= page_size * mmap_window) {
1531 shift = page_size * (head / page_size);
1533 munmap_ret = munmap(buf, page_size * mmap_window);
1534 assert(munmap_ret == 0);
1541 size = event->header.size;
1543 dprintf("\n%p [%p]: event: %d\n",
1544 (void *)(offset + head),
1545 (void *)(long)event->header.size,
1546 event->header.type);
1548 if (!size || process_event(event, offset, head) < 0) {
1550 dprintf("%p [%p]: skipping unknown header type: %d\n",
1551 (void *)(offset + head),
1552 (void *)(long)(event->header.size),
1553 event->header.type);
1558 * assume we lost track of the stream, check alignment, and
1559 * increment a single u64 in the hope to catch on again 'soon'.
1562 if (unlikely(head & 7))
1570 if (offset + head >= header->data_offset + header->data_size)
1573 if (offset + head < (unsigned long)input_stat.st_size)
1580 dprintf(" IP events: %10ld\n", total);
1581 dprintf(" mmap events: %10ld\n", total_mmap);
1582 dprintf(" comm events: %10ld\n", total_comm);
1583 dprintf(" fork events: %10ld\n", total_fork);
1584 dprintf(" lost events: %10ld\n", total_lost);
1585 dprintf(" unknown events: %10ld\n", total_unknown);
1591 threads__fprintf(stdout, &threads);
1594 dsos__fprintf(stdout);
1597 output__resort(total);
1598 output__fprintf(stdout, total);
1601 perf_read_values_destroy(&show_threads_values);
1607 parse_callchain_opt(const struct option *opt __used, const char *arg,
1618 tok = strtok((char *)arg, ",");
1622 /* get the output mode */
1623 if (!strncmp(tok, "graph", strlen(arg)))
1624 callchain_param.mode = CHAIN_GRAPH_ABS;
1626 else if (!strncmp(tok, "flat", strlen(arg)))
1627 callchain_param.mode = CHAIN_FLAT;
1629 else if (!strncmp(tok, "fractal", strlen(arg)))
1630 callchain_param.mode = CHAIN_GRAPH_REL;
1632 else if (!strncmp(tok, "none", strlen(arg))) {
1633 callchain_param.mode = CHAIN_NONE;
1642 /* get the min percentage */
1643 tok = strtok(NULL, ",");
1647 callchain_param.min_percent = strtod(tok, &endptr);
1652 if (register_callchain_param(&callchain_param) < 0) {
1653 fprintf(stderr, "Can't register callchain params\n");
1659 static const char * const report_usage[] = {
1660 "perf report [<options>] <command>",
1664 static const struct option options[] = {
1665 OPT_STRING('i', "input", &input_name, "file",
1667 OPT_BOOLEAN('v', "verbose", &verbose,
1668 "be more verbose (show symbol address, etc)"),
1669 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1670 "dump raw trace in ASCII"),
1671 OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
1672 OPT_BOOLEAN('m', "modules", &modules,
1673 "load module symbols - WARNING: use only with -k and LIVE kernel"),
1674 OPT_BOOLEAN('n', "show-nr-samples", &show_nr_samples,
1675 "Show a column with the number of samples"),
1676 OPT_BOOLEAN('T', "threads", &show_threads,
1677 "Show per-thread event counters"),
1678 OPT_STRING(0, "pretty", &pretty_printing_style, "key",
1679 "pretty printing style key: normal raw"),
1680 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1681 "sort by key(s): pid, comm, dso, symbol, parent"),
1682 OPT_BOOLEAN('P', "full-paths", &full_paths,
1683 "Don't shorten the pathnames taking into account the cwd"),
1684 OPT_STRING('p', "parent", &parent_pattern, "regex",
1685 "regex filter to identify parent, see: '--sort parent'"),
1686 OPT_BOOLEAN('x', "exclude-other", &exclude_other,
1687 "Only display entries with parent-match"),
1688 OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
1689 "Display callchains using output_type and min percent threshold. "
1690 "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
1691 OPT_STRING('d', "dsos", &dso_list_str, "dso[,dso...]",
1692 "only consider symbols in these dsos"),
1693 OPT_STRING('C', "comms", &comm_list_str, "comm[,comm...]",
1694 "only consider symbols in these comms"),
1695 OPT_STRING('S', "symbols", &sym_list_str, "symbol[,symbol...]",
1696 "only consider these symbols"),
1697 OPT_STRING('w', "column-widths", &col_width_list_str,
1699 "don't try to adjust column width, use these fixed values"),
1700 OPT_STRING('t', "field-separator", &field_sep, "separator",
1701 "separator for columns, no spaces will be added between "
1702 "columns '.' is reserved."),
1706 static void setup_sorting(void)
1708 char *tmp, *tok, *str = strdup(sort_order);
1710 for (tok = strtok_r(str, ", ", &tmp);
1711 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1712 if (sort_dimension__add(tok) < 0) {
1713 error("Unknown --sort key: `%s'", tok);
1714 usage_with_options(report_usage, options);
1721 static void setup_list(struct strlist **list, const char *list_str,
1722 struct sort_entry *se, const char *list_name,
1726 *list = strlist__new(true, list_str);
1728 fprintf(stderr, "problems parsing %s list\n",
1732 if (strlist__nr_entries(*list) == 1) {
1733 fprintf(fp, "# %s: %s\n", list_name,
1734 strlist__entry(*list, 0)->s);
1740 int cmd_report(int argc, const char **argv, const char *prefix __used)
1744 page_size = getpagesize();
1746 argc = parse_options(argc, argv, options, report_usage, 0);
1750 if (parent_pattern != default_parent_pattern) {
1751 sort_dimension__add("parent");
1752 sort_parent.elide = 1;
1757 * Any (unrecognized) arguments left?
1760 usage_with_options(report_usage, options);
1764 setup_list(&dso_list, dso_list_str, &sort_dso, "dso", stdout);
1765 setup_list(&comm_list, comm_list_str, &sort_comm, "comm", stdout);
1766 setup_list(&sym_list, sym_list_str, &sort_sym, "symbol", stdout);
1768 if (field_sep && *field_sep == '.') {
1769 fputs("'.' is the only non valid --field-separator argument\n",
1774 return __cmd_report();