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 "util/list.h"
14 #include "util/cache.h"
15 #include "util/rbtree.h"
16 #include "util/symbol.h"
17 #include "util/string.h"
20 #include "util/header.h"
22 #include "util/parse-options.h"
23 #include "util/parse-events.h"
29 static char const *input_name = "perf.data";
30 static char *vmlinux = NULL;
32 static char default_sort_order[] = "comm,dso";
33 static char *sort_order = default_sort_order;
36 static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
38 static int dump_trace = 0;
39 #define dprintf(x...) do { if (dump_trace) printf(x); } while (0)
40 #define cdprintf(x...) do { if (dump_trace) color_fprintf(stdout, color, x); } while (0)
43 #define eprintf(x...) do { if (verbose) fprintf(stderr, x); } while (0)
45 static int full_paths;
47 static unsigned long page_size;
48 static unsigned long mmap_window = 32;
50 static char default_parent_pattern[] = "^sys_|^do_page_fault";
51 static char *parent_pattern = default_parent_pattern;
52 static regex_t parent_regex;
54 static int exclude_other = 1;
56 static u64 sample_type;
59 struct perf_event_header header;
62 unsigned char __more_data[];
66 struct perf_event_header header;
71 char filename[PATH_MAX];
75 struct perf_event_header header;
81 struct perf_event_header header;
86 struct perf_event_header header;
93 struct perf_event_header header;
99 struct perf_event_header header;
105 typedef union event_union {
106 struct perf_event_header header;
108 struct mmap_event mmap;
109 struct comm_event comm;
110 struct fork_event fork;
111 struct period_event period;
112 struct lost_event lost;
113 struct read_event read;
116 static LIST_HEAD(dsos);
117 static struct dso *kernel_dso;
118 static struct dso *vdso;
120 static void dsos__add(struct dso *dso)
122 list_add_tail(&dso->node, &dsos);
125 static struct dso *dsos__find(const char *name)
129 list_for_each_entry(pos, &dsos, node)
130 if (strcmp(pos->name, name) == 0)
135 static struct dso *dsos__findnew(const char *name)
137 struct dso *dso = dsos__find(name);
143 dso = dso__new(name, 0);
147 nr = dso__load(dso, NULL, verbose);
149 eprintf("Failed to open: %s\n", name);
153 eprintf("No symbols found in: %s, maybe install a debug package?\n", name);
164 static void dsos__fprintf(FILE *fp)
168 list_for_each_entry(pos, &dsos, node)
169 dso__fprintf(pos, fp);
172 static struct symbol *vdso__find_symbol(struct dso *dso, u64 ip)
174 return dso__find_symbol(kernel_dso, ip);
177 static int load_kernel(void)
181 kernel_dso = dso__new("[kernel]", 0);
185 err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose);
187 dso__delete(kernel_dso);
190 dsos__add(kernel_dso);
192 vdso = dso__new("[vdso]", 0);
196 vdso->find_symbol = vdso__find_symbol;
203 static char __cwd[PATH_MAX];
204 static char *cwd = __cwd;
207 static int strcommon(const char *pathname)
211 while (pathname[n] == cwd[n] && n < cwdlen)
218 struct list_head node;
222 u64 (*map_ip)(struct map *, u64);
226 static u64 map__map_ip(struct map *map, u64 ip)
228 return ip - map->start + map->pgoff;
231 static u64 vdso__map_ip(struct map *map, u64 ip)
236 static inline int is_anon_memory(const char *filename)
238 return strcmp(filename, "//anon") == 0;
241 static struct map *map__new(struct mmap_event *event)
243 struct map *self = malloc(sizeof(*self));
246 const char *filename = event->filename;
247 char newfilename[PATH_MAX];
251 int n = strcommon(filename);
254 snprintf(newfilename, sizeof(newfilename),
255 ".%s", filename + n);
256 filename = newfilename;
260 anon = is_anon_memory(filename);
263 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
264 filename = newfilename;
267 self->start = event->start;
268 self->end = event->start + event->len;
269 self->pgoff = event->pgoff;
271 self->dso = dsos__findnew(filename);
272 if (self->dso == NULL)
275 if (self->dso == vdso || anon)
276 self->map_ip = vdso__map_ip;
278 self->map_ip = map__map_ip;
286 static struct map *map__clone(struct map *self)
288 struct map *map = malloc(sizeof(*self));
293 memcpy(map, self, sizeof(*self));
298 static int map__overlap(struct map *l, struct map *r)
300 if (l->start > r->start) {
306 if (l->end > r->start)
312 static size_t map__fprintf(struct map *self, FILE *fp)
314 return fprintf(fp, " %Lx-%Lx %Lx %s\n",
315 self->start, self->end, self->pgoff, self->dso->name);
320 struct rb_node rb_node;
321 struct list_head maps;
326 static struct thread *thread__new(pid_t pid)
328 struct thread *self = malloc(sizeof(*self));
332 self->comm = malloc(32);
334 snprintf(self->comm, 32, ":%d", self->pid);
335 INIT_LIST_HEAD(&self->maps);
341 static int thread__set_comm(struct thread *self, const char *comm)
345 self->comm = strdup(comm);
346 return self->comm ? 0 : -ENOMEM;
349 static size_t thread__fprintf(struct thread *self, FILE *fp)
352 size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
354 list_for_each_entry(pos, &self->maps, node)
355 ret += map__fprintf(pos, fp);
361 static struct rb_root threads;
362 static struct thread *last_match;
364 static struct thread *threads__findnew(pid_t pid)
366 struct rb_node **p = &threads.rb_node;
367 struct rb_node *parent = NULL;
371 * Font-end cache - PID lookups come in blocks,
372 * so most of the time we dont have to look up
375 if (last_match && last_match->pid == pid)
380 th = rb_entry(parent, struct thread, rb_node);
382 if (th->pid == pid) {
393 th = thread__new(pid);
395 rb_link_node(&th->rb_node, parent, p);
396 rb_insert_color(&th->rb_node, &threads);
403 static void thread__insert_map(struct thread *self, struct map *map)
405 struct map *pos, *tmp;
407 list_for_each_entry_safe(pos, tmp, &self->maps, node) {
408 if (map__overlap(pos, map)) {
410 printf("overlapping maps:\n");
411 map__fprintf(map, stdout);
412 map__fprintf(pos, stdout);
415 if (map->start <= pos->start && map->end > pos->start)
416 pos->start = map->end;
418 if (map->end >= pos->end && map->start < pos->end)
419 pos->end = map->start;
422 printf("after collision:\n");
423 map__fprintf(pos, stdout);
426 if (pos->start >= pos->end) {
427 list_del_init(&pos->node);
433 list_add_tail(&map->node, &self->maps);
436 static int thread__fork(struct thread *self, struct thread *parent)
442 self->comm = strdup(parent->comm);
446 list_for_each_entry(map, &parent->maps, node) {
447 struct map *new = map__clone(map);
450 thread__insert_map(self, new);
456 static struct map *thread__find_map(struct thread *self, u64 ip)
463 list_for_each_entry(pos, &self->maps, node)
464 if (ip >= pos->start && ip <= pos->end)
470 static size_t threads__fprintf(FILE *fp)
475 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
476 struct thread *pos = rb_entry(nd, struct thread, rb_node);
478 ret += thread__fprintf(pos, fp);
485 * histogram, sorted on item, collects counts
488 static struct rb_root hist;
491 struct rb_node rb_node;
493 struct thread *thread;
497 struct symbol *parent;
505 * configurable sorting bits
509 struct list_head list;
513 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
514 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
515 size_t (*print)(FILE *fp, struct hist_entry *);
518 static int64_t cmp_null(void *l, void *r)
531 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
533 return right->thread->pid - left->thread->pid;
537 sort__thread_print(FILE *fp, struct hist_entry *self)
539 return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
542 static struct sort_entry sort_thread = {
543 .header = " Command: Pid",
544 .cmp = sort__thread_cmp,
545 .print = sort__thread_print,
551 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
553 return right->thread->pid - left->thread->pid;
557 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
559 char *comm_l = left->thread->comm;
560 char *comm_r = right->thread->comm;
562 if (!comm_l || !comm_r)
563 return cmp_null(comm_l, comm_r);
565 return strcmp(comm_l, comm_r);
569 sort__comm_print(FILE *fp, struct hist_entry *self)
571 return fprintf(fp, "%16s", self->thread->comm);
574 static struct sort_entry sort_comm = {
575 .header = " Command",
576 .cmp = sort__comm_cmp,
577 .collapse = sort__comm_collapse,
578 .print = sort__comm_print,
584 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
586 struct dso *dso_l = left->dso;
587 struct dso *dso_r = right->dso;
589 if (!dso_l || !dso_r)
590 return cmp_null(dso_l, dso_r);
592 return strcmp(dso_l->name, dso_r->name);
596 sort__dso_print(FILE *fp, struct hist_entry *self)
599 return fprintf(fp, "%-25s", self->dso->name);
601 return fprintf(fp, "%016llx ", (u64)self->ip);
604 static struct sort_entry sort_dso = {
605 .header = "Shared Object ",
606 .cmp = sort__dso_cmp,
607 .print = sort__dso_print,
613 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
617 if (left->sym == right->sym)
620 ip_l = left->sym ? left->sym->start : left->ip;
621 ip_r = right->sym ? right->sym->start : right->ip;
623 return (int64_t)(ip_r - ip_l);
627 sort__sym_print(FILE *fp, struct hist_entry *self)
632 ret += fprintf(fp, "%#018llx ", (u64)self->ip);
635 ret += fprintf(fp, "[%c] %s",
636 self->dso == kernel_dso ? 'k' : '.', self->sym->name);
638 ret += fprintf(fp, "%#016llx", (u64)self->ip);
644 static struct sort_entry sort_sym = {
646 .cmp = sort__sym_cmp,
647 .print = sort__sym_print,
653 sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
655 struct symbol *sym_l = left->parent;
656 struct symbol *sym_r = right->parent;
658 if (!sym_l || !sym_r)
659 return cmp_null(sym_l, sym_r);
661 return strcmp(sym_l->name, sym_r->name);
665 sort__parent_print(FILE *fp, struct hist_entry *self)
669 ret += fprintf(fp, "%-20s", self->parent ? self->parent->name : "[other]");
674 static struct sort_entry sort_parent = {
675 .header = "Parent symbol ",
676 .cmp = sort__parent_cmp,
677 .print = sort__parent_print,
680 static int sort__need_collapse = 0;
681 static int sort__has_parent = 0;
683 struct sort_dimension {
685 struct sort_entry *entry;
689 static struct sort_dimension sort_dimensions[] = {
690 { .name = "pid", .entry = &sort_thread, },
691 { .name = "comm", .entry = &sort_comm, },
692 { .name = "dso", .entry = &sort_dso, },
693 { .name = "symbol", .entry = &sort_sym, },
694 { .name = "parent", .entry = &sort_parent, },
697 static LIST_HEAD(hist_entry__sort_list);
699 static int sort_dimension__add(char *tok)
703 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
704 struct sort_dimension *sd = &sort_dimensions[i];
709 if (strncasecmp(tok, sd->name, strlen(tok)))
712 if (sd->entry->collapse)
713 sort__need_collapse = 1;
715 if (sd->entry == &sort_parent) {
716 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
720 regerror(ret, &parent_regex, err, sizeof(err));
721 fprintf(stderr, "Invalid regex: %s\n%s",
722 parent_pattern, err);
725 sort__has_parent = 1;
728 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
738 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
740 struct sort_entry *se;
743 list_for_each_entry(se, &hist_entry__sort_list, list) {
744 cmp = se->cmp(left, right);
753 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
755 struct sort_entry *se;
758 list_for_each_entry(se, &hist_entry__sort_list, list) {
759 int64_t (*f)(struct hist_entry *, struct hist_entry *);
761 f = se->collapse ?: se->cmp;
763 cmp = f(left, right);
772 hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
774 struct sort_entry *se;
777 if (exclude_other && !self->parent)
781 double percent = self->count * 100.0 / total_samples;
782 char *color = PERF_COLOR_NORMAL;
785 * We color high-overhead entries in red, mid-overhead
786 * entries in green - and keep the low overhead places
789 if (percent >= 5.0) {
790 color = PERF_COLOR_RED;
793 color = PERF_COLOR_GREEN;
796 ret = color_fprintf(fp, color, " %6.2f%%",
797 (self->count * 100.0) / total_samples);
799 ret = fprintf(fp, "%12Ld ", self->count);
801 list_for_each_entry(se, &hist_entry__sort_list, list) {
802 if (exclude_other && (se == &sort_parent))
806 ret += se->print(fp, self);
809 ret += fprintf(fp, "\n");
818 static struct symbol *
819 resolve_symbol(struct thread *thread, struct map **mapp,
820 struct dso **dsop, u64 *ipp)
822 struct dso *dso = dsop ? *dsop : NULL;
823 struct map *map = mapp ? *mapp : NULL;
835 map = thread__find_map(thread, ip);
840 ip = map->map_ip(map, ip);
845 * If this is outside of all known maps,
846 * and is a negative address, try to look it
847 * up in the kernel dso, as it might be a
848 * vsyscall (which executes in user-mode):
850 if ((long long)ip < 0)
853 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
854 dprintf(" ...... map: %Lx -> %Lx\n", *ipp, ip);
863 return dso->find_symbol(dso, ip);
866 static int call__match(struct symbol *sym)
868 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
875 * collect histogram counts
879 hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
880 struct symbol *sym, u64 ip, struct ip_callchain *chain,
881 char level, u64 count)
883 struct rb_node **p = &hist.rb_node;
884 struct rb_node *parent = NULL;
885 struct hist_entry *he;
886 struct hist_entry entry = {
898 if (sort__has_parent && chain) {
899 u64 context = PERF_CONTEXT_MAX;
902 for (i = 0; i < chain->nr; i++) {
903 u64 ip = chain->ips[i];
904 struct dso *dso = NULL;
907 if (ip >= PERF_CONTEXT_MAX) {
913 case PERF_CONTEXT_KERNEL:
920 sym = resolve_symbol(thread, NULL, &dso, &ip);
922 if (sym && call__match(sym)) {
931 he = rb_entry(parent, struct hist_entry, rb_node);
933 cmp = hist_entry__cmp(&entry, he);
946 he = malloc(sizeof(*he));
950 rb_link_node(&he->rb_node, parent, p);
951 rb_insert_color(&he->rb_node, &hist);
956 static void hist_entry__free(struct hist_entry *he)
962 * collapse the histogram
965 static struct rb_root collapse_hists;
967 static void collapse__insert_entry(struct hist_entry *he)
969 struct rb_node **p = &collapse_hists.rb_node;
970 struct rb_node *parent = NULL;
971 struct hist_entry *iter;
976 iter = rb_entry(parent, struct hist_entry, rb_node);
978 cmp = hist_entry__collapse(iter, he);
981 iter->count += he->count;
982 hist_entry__free(he);
992 rb_link_node(&he->rb_node, parent, p);
993 rb_insert_color(&he->rb_node, &collapse_hists);
996 static void collapse__resort(void)
998 struct rb_node *next;
999 struct hist_entry *n;
1001 if (!sort__need_collapse)
1004 next = rb_first(&hist);
1006 n = rb_entry(next, struct hist_entry, rb_node);
1007 next = rb_next(&n->rb_node);
1009 rb_erase(&n->rb_node, &hist);
1010 collapse__insert_entry(n);
1015 * reverse the map, sort on count.
1018 static struct rb_root output_hists;
1020 static void output__insert_entry(struct hist_entry *he)
1022 struct rb_node **p = &output_hists.rb_node;
1023 struct rb_node *parent = NULL;
1024 struct hist_entry *iter;
1026 while (*p != NULL) {
1028 iter = rb_entry(parent, struct hist_entry, rb_node);
1030 if (he->count > iter->count)
1033 p = &(*p)->rb_right;
1036 rb_link_node(&he->rb_node, parent, p);
1037 rb_insert_color(&he->rb_node, &output_hists);
1040 static void output__resort(void)
1042 struct rb_node *next;
1043 struct hist_entry *n;
1044 struct rb_root *tree = &hist;
1046 if (sort__need_collapse)
1047 tree = &collapse_hists;
1049 next = rb_first(tree);
1052 n = rb_entry(next, struct hist_entry, rb_node);
1053 next = rb_next(&n->rb_node);
1055 rb_erase(&n->rb_node, tree);
1056 output__insert_entry(n);
1060 static size_t output__fprintf(FILE *fp, u64 total_samples)
1062 struct hist_entry *pos;
1063 struct sort_entry *se;
1069 fprintf(fp, "# (%Ld samples)\n", (u64)total_samples);
1072 fprintf(fp, "# Overhead");
1073 list_for_each_entry(se, &hist_entry__sort_list, list) {
1074 if (exclude_other && (se == &sort_parent))
1076 fprintf(fp, " %s", se->header);
1080 fprintf(fp, "# ........");
1081 list_for_each_entry(se, &hist_entry__sort_list, list) {
1084 if (exclude_other && (se == &sort_parent))
1088 for (i = 0; i < strlen(se->header); i++)
1095 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
1096 pos = rb_entry(nd, struct hist_entry, rb_node);
1097 ret += hist_entry__fprintf(fp, pos, total_samples);
1100 if (sort_order == default_sort_order &&
1101 parent_pattern == default_parent_pattern) {
1103 fprintf(fp, "# (For more details, try: perf report --sort comm,dso,symbol)\n");
1111 static void register_idle_thread(void)
1113 struct thread *thread = threads__findnew(0);
1115 if (thread == NULL ||
1116 thread__set_comm(thread, "[idle]")) {
1117 fprintf(stderr, "problem inserting idle task.\n");
1122 static unsigned long total = 0,
1129 static int validate_chain(struct ip_callchain *chain, event_t *event)
1131 unsigned int chain_size;
1133 chain_size = event->header.size;
1134 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
1136 if (chain->nr*sizeof(u64) > chain_size)
1143 process_sample_event(event_t *event, unsigned long offset, unsigned long head)
1147 struct dso *dso = NULL;
1148 struct thread *thread = threads__findnew(event->ip.pid);
1149 u64 ip = event->ip.ip;
1151 struct map *map = NULL;
1152 void *more_data = event->ip.__more_data;
1153 struct ip_callchain *chain = NULL;
1155 if (sample_type & PERF_SAMPLE_PERIOD) {
1156 period = *(u64 *)more_data;
1157 more_data += sizeof(u64);
1160 dprintf("%p [%p]: PERF_EVENT_SAMPLE (IP, %d): %d: %p period: %Ld\n",
1161 (void *)(offset + head),
1162 (void *)(long)(event->header.size),
1168 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
1171 chain = (void *)more_data;
1173 dprintf("... chain: nr:%Lu\n", chain->nr);
1175 if (validate_chain(chain, event) < 0) {
1176 eprintf("call-chain problem with event, skipping it.\n");
1181 for (i = 0; i < chain->nr; i++)
1182 dprintf("..... %2d: %016Lx\n", i, chain->ips[i]);
1186 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1188 if (thread == NULL) {
1189 eprintf("problem processing %d event, skipping it.\n",
1190 event->header.type);
1194 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
1200 dprintf(" ...... dso: %s\n", dso->name);
1202 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
1210 dprintf(" ...... dso: [hypervisor]\n");
1213 if (show & show_mask) {
1214 struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
1216 if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
1217 eprintf("problem incrementing symbol count, skipping event\n");
1227 process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
1229 struct thread *thread = threads__findnew(event->mmap.pid);
1230 struct map *map = map__new(&event->mmap);
1232 dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
1233 (void *)(offset + head),
1234 (void *)(long)(event->header.size),
1236 (void *)(long)event->mmap.start,
1237 (void *)(long)event->mmap.len,
1238 (void *)(long)event->mmap.pgoff,
1239 event->mmap.filename);
1241 if (thread == NULL || map == NULL) {
1242 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
1246 thread__insert_map(thread, map);
1253 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
1255 struct thread *thread = threads__findnew(event->comm.pid);
1257 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
1258 (void *)(offset + head),
1259 (void *)(long)(event->header.size),
1260 event->comm.comm, event->comm.pid);
1262 if (thread == NULL ||
1263 thread__set_comm(thread, event->comm.comm)) {
1264 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
1273 process_fork_event(event_t *event, unsigned long offset, unsigned long head)
1275 struct thread *thread = threads__findnew(event->fork.pid);
1276 struct thread *parent = threads__findnew(event->fork.ppid);
1278 dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
1279 (void *)(offset + head),
1280 (void *)(long)(event->header.size),
1281 event->fork.pid, event->fork.ppid);
1283 if (!thread || !parent || thread__fork(thread, parent)) {
1284 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
1293 process_period_event(event_t *event, unsigned long offset, unsigned long head)
1295 dprintf("%p [%p]: PERF_EVENT_PERIOD: time:%Ld, id:%Ld: period:%Ld\n",
1296 (void *)(offset + head),
1297 (void *)(long)(event->header.size),
1300 event->period.sample_period);
1306 process_lost_event(event_t *event, unsigned long offset, unsigned long head)
1308 dprintf("%p [%p]: PERF_EVENT_LOST: id:%Ld: lost:%Ld\n",
1309 (void *)(offset + head),
1310 (void *)(long)(event->header.size),
1314 total_lost += event->lost.lost;
1319 static void trace_event(event_t *event)
1321 unsigned char *raw_event = (void *)event;
1322 char *color = PERF_COLOR_BLUE;
1329 cdprintf("\n. ... raw event: size %d bytes\n", event->header.size);
1331 for (i = 0; i < event->header.size; i++) {
1332 if ((i & 15) == 0) {
1334 cdprintf(" %04x: ", i);
1337 cdprintf(" %02x", raw_event[i]);
1339 if (((i & 15) == 15) || i == event->header.size-1) {
1341 for (j = 0; j < 15-(i & 15); j++)
1343 for (j = 0; j < (i & 15); j++) {
1344 if (isprint(raw_event[i-15+j]))
1345 cdprintf("%c", raw_event[i-15+j]);
1356 process_read_event(event_t *event, unsigned long offset, unsigned long head)
1358 dprintf("%p [%p]: PERF_EVENT_READ: %d %d %Lu\n",
1359 (void *)(offset + head),
1360 (void *)(long)(event->header.size),
1369 process_event(event_t *event, unsigned long offset, unsigned long head)
1373 switch (event->header.type) {
1374 case PERF_EVENT_SAMPLE:
1375 return process_sample_event(event, offset, head);
1377 case PERF_EVENT_MMAP:
1378 return process_mmap_event(event, offset, head);
1380 case PERF_EVENT_COMM:
1381 return process_comm_event(event, offset, head);
1383 case PERF_EVENT_FORK:
1384 return process_fork_event(event, offset, head);
1386 case PERF_EVENT_PERIOD:
1387 return process_period_event(event, offset, head);
1389 case PERF_EVENT_LOST:
1390 return process_lost_event(event, offset, head);
1392 case PERF_EVENT_READ:
1393 return process_read_event(event, offset, head);
1396 * We dont process them right now but they are fine:
1399 case PERF_EVENT_THROTTLE:
1400 case PERF_EVENT_UNTHROTTLE:
1410 static struct perf_header *header;
1412 static u64 perf_header__sample_type(void)
1414 u64 sample_type = 0;
1417 for (i = 0; i < header->attrs; i++) {
1418 struct perf_header_attr *attr = header->attr[i];
1421 sample_type = attr->attr.sample_type;
1422 else if (sample_type != attr->attr.sample_type)
1423 die("non matching sample_type");
1429 static int __cmd_report(void)
1431 int ret, rc = EXIT_FAILURE;
1432 unsigned long offset = 0;
1433 unsigned long head, shift;
1439 register_idle_thread();
1441 input = open(input_name, O_RDONLY);
1443 fprintf(stderr, " failed to open file: %s", input_name);
1444 if (!strcmp(input_name, "perf.data"))
1445 fprintf(stderr, " (try 'perf record' first)");
1446 fprintf(stderr, "\n");
1450 ret = fstat(input, &stat);
1452 perror("failed to stat file");
1456 if (!stat.st_size) {
1457 fprintf(stderr, "zero-sized file, nothing to do!\n");
1461 header = perf_header__read(input);
1462 head = header->data_offset;
1464 sample_type = perf_header__sample_type();
1466 if (sort__has_parent && !(sample_type & PERF_SAMPLE_CALLCHAIN)) {
1467 fprintf(stderr, "selected --sort parent, but no callchain data\n");
1471 if (load_kernel() < 0) {
1472 perror("failed to load kernel symbols");
1473 return EXIT_FAILURE;
1477 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
1478 perror("failed to get the current directory");
1479 return EXIT_FAILURE;
1481 cwdlen = strlen(cwd);
1487 shift = page_size * (head / page_size);
1492 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1493 MAP_SHARED, input, offset);
1494 if (buf == MAP_FAILED) {
1495 perror("failed to mmap file");
1500 event = (event_t *)(buf + head);
1502 size = event->header.size;
1506 if (head + event->header.size >= page_size * mmap_window) {
1509 shift = page_size * (head / page_size);
1511 ret = munmap(buf, page_size * mmap_window);
1519 size = event->header.size;
1521 dprintf("\n%p [%p]: event: %d\n",
1522 (void *)(offset + head),
1523 (void *)(long)event->header.size,
1524 event->header.type);
1526 if (!size || process_event(event, offset, head) < 0) {
1528 dprintf("%p [%p]: skipping unknown header type: %d\n",
1529 (void *)(offset + head),
1530 (void *)(long)(event->header.size),
1531 event->header.type);
1536 * assume we lost track of the stream, check alignment, and
1537 * increment a single u64 in the hope to catch on again 'soon'.
1540 if (unlikely(head & 7))
1548 if (offset + head >= header->data_offset + header->data_size)
1551 if (offset + head < stat.st_size)
1558 dprintf(" IP events: %10ld\n", total);
1559 dprintf(" mmap events: %10ld\n", total_mmap);
1560 dprintf(" comm events: %10ld\n", total_comm);
1561 dprintf(" fork events: %10ld\n", total_fork);
1562 dprintf(" lost events: %10ld\n", total_lost);
1563 dprintf(" unknown events: %10ld\n", total_unknown);
1569 threads__fprintf(stdout);
1572 dsos__fprintf(stdout);
1576 output__fprintf(stdout, total);
1581 static const char * const report_usage[] = {
1582 "perf report [<options>] <command>",
1586 static const struct option options[] = {
1587 OPT_STRING('i', "input", &input_name, "file",
1589 OPT_BOOLEAN('v', "verbose", &verbose,
1590 "be more verbose (show symbol address, etc)"),
1591 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1592 "dump raw trace in ASCII"),
1593 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
1594 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1595 "sort by key(s): pid, comm, dso, symbol, parent"),
1596 OPT_BOOLEAN('P', "full-paths", &full_paths,
1597 "Don't shorten the pathnames taking into account the cwd"),
1598 OPT_STRING('p', "parent", &parent_pattern, "regex",
1599 "regex filter to identify parent, see: '--sort parent'"),
1600 OPT_BOOLEAN('x', "exclude-other", &exclude_other,
1601 "Only display entries with parent-match"),
1605 static void setup_sorting(void)
1607 char *tmp, *tok, *str = strdup(sort_order);
1609 for (tok = strtok_r(str, ", ", &tmp);
1610 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1611 if (sort_dimension__add(tok) < 0) {
1612 error("Unknown --sort key: `%s'", tok);
1613 usage_with_options(report_usage, options);
1620 int cmd_report(int argc, const char **argv, const char *prefix)
1624 page_size = getpagesize();
1626 argc = parse_options(argc, argv, options, report_usage, 0);
1630 if (parent_pattern != default_parent_pattern)
1631 sort_dimension__add("parent");
1636 * Any (unrecognized) arguments left?
1639 usage_with_options(report_usage, options);
1643 return __cmd_report();