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"
21 #include "util/parse-options.h"
22 #include "util/parse-events.h"
28 static char const *input_name = "perf.data";
29 static char *vmlinux = NULL;
31 static char default_sort_order[] = "comm,dso";
32 static char *sort_order = default_sort_order;
35 static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
37 static int dump_trace = 0;
38 #define dprintf(x...) do { if (dump_trace) printf(x); } while (0)
39 #define cdprintf(x...) do { if (dump_trace) color_fprintf(stdout, color, x); } while (0)
42 #define eprintf(x...) do { if (verbose) fprintf(stderr, x); } while (0)
44 static int full_paths;
46 static unsigned long page_size;
47 static unsigned long mmap_window = 32;
49 static char *parent_pattern = "^sys_|^do_page_fault";
50 static regex_t parent_regex;
53 struct perf_event_header header;
56 unsigned char __more_data[];
60 struct perf_event_header header;
65 char filename[PATH_MAX];
69 struct perf_event_header header;
75 struct perf_event_header header;
80 struct perf_event_header header;
86 typedef union event_union {
87 struct perf_event_header header;
89 struct mmap_event mmap;
90 struct comm_event comm;
91 struct fork_event fork;
92 struct period_event period;
95 static LIST_HEAD(dsos);
96 static struct dso *kernel_dso;
97 static struct dso *vdso;
99 static void dsos__add(struct dso *dso)
101 list_add_tail(&dso->node, &dsos);
104 static struct dso *dsos__find(const char *name)
108 list_for_each_entry(pos, &dsos, node)
109 if (strcmp(pos->name, name) == 0)
114 static struct dso *dsos__findnew(const char *name)
116 struct dso *dso = dsos__find(name);
122 dso = dso__new(name, 0);
126 nr = dso__load(dso, NULL, verbose);
128 eprintf("Failed to open: %s\n", name);
132 eprintf("No symbols found in: %s, maybe install a debug package?\n", name);
143 static void dsos__fprintf(FILE *fp)
147 list_for_each_entry(pos, &dsos, node)
148 dso__fprintf(pos, fp);
151 static struct symbol *vdso__find_symbol(struct dso *dso, __u64 ip)
153 return dso__find_symbol(kernel_dso, ip);
156 static int load_kernel(void)
160 kernel_dso = dso__new("[kernel]", 0);
164 err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose);
166 dso__delete(kernel_dso);
169 dsos__add(kernel_dso);
171 vdso = dso__new("[vdso]", 0);
175 vdso->find_symbol = vdso__find_symbol;
182 static char __cwd[PATH_MAX];
183 static char *cwd = __cwd;
186 static int strcommon(const char *pathname)
190 while (pathname[n] == cwd[n] && n < cwdlen)
197 struct list_head node;
201 __u64 (*map_ip)(struct map *, __u64);
205 static __u64 map__map_ip(struct map *map, __u64 ip)
207 return ip - map->start + map->pgoff;
210 static __u64 vdso__map_ip(struct map *map, __u64 ip)
215 static inline int is_anon_memory(const char *filename)
217 return strcmp(filename, "//anon") == 0;
220 static struct map *map__new(struct mmap_event *event)
222 struct map *self = malloc(sizeof(*self));
225 const char *filename = event->filename;
226 char newfilename[PATH_MAX];
230 int n = strcommon(filename);
233 snprintf(newfilename, sizeof(newfilename),
234 ".%s", filename + n);
235 filename = newfilename;
239 anon = is_anon_memory(filename);
242 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
243 filename = newfilename;
246 self->start = event->start;
247 self->end = event->start + event->len;
248 self->pgoff = event->pgoff;
250 self->dso = dsos__findnew(filename);
251 if (self->dso == NULL)
254 if (self->dso == vdso || anon)
255 self->map_ip = vdso__map_ip;
257 self->map_ip = map__map_ip;
265 static struct map *map__clone(struct map *self)
267 struct map *map = malloc(sizeof(*self));
272 memcpy(map, self, sizeof(*self));
277 static int map__overlap(struct map *l, struct map *r)
279 if (l->start > r->start) {
285 if (l->end > r->start)
291 static size_t map__fprintf(struct map *self, FILE *fp)
293 return fprintf(fp, " %Lx-%Lx %Lx %s\n",
294 self->start, self->end, self->pgoff, self->dso->name);
299 struct rb_node rb_node;
300 struct list_head maps;
305 static struct thread *thread__new(pid_t pid)
307 struct thread *self = malloc(sizeof(*self));
311 self->comm = malloc(32);
313 snprintf(self->comm, 32, ":%d", self->pid);
314 INIT_LIST_HEAD(&self->maps);
320 static int thread__set_comm(struct thread *self, const char *comm)
324 self->comm = strdup(comm);
325 return self->comm ? 0 : -ENOMEM;
328 static size_t thread__fprintf(struct thread *self, FILE *fp)
331 size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
333 list_for_each_entry(pos, &self->maps, node)
334 ret += map__fprintf(pos, fp);
340 static struct rb_root threads;
341 static struct thread *last_match;
343 static struct thread *threads__findnew(pid_t pid)
345 struct rb_node **p = &threads.rb_node;
346 struct rb_node *parent = NULL;
350 * Font-end cache - PID lookups come in blocks,
351 * so most of the time we dont have to look up
354 if (last_match && last_match->pid == pid)
359 th = rb_entry(parent, struct thread, rb_node);
361 if (th->pid == pid) {
372 th = thread__new(pid);
374 rb_link_node(&th->rb_node, parent, p);
375 rb_insert_color(&th->rb_node, &threads);
382 static void thread__insert_map(struct thread *self, struct map *map)
384 struct map *pos, *tmp;
386 list_for_each_entry_safe(pos, tmp, &self->maps, node) {
387 if (map__overlap(pos, map)) {
388 list_del_init(&pos->node);
394 list_add_tail(&map->node, &self->maps);
397 static int thread__fork(struct thread *self, struct thread *parent)
403 self->comm = strdup(parent->comm);
407 list_for_each_entry(map, &parent->maps, node) {
408 struct map *new = map__clone(map);
411 thread__insert_map(self, new);
417 static struct map *thread__find_map(struct thread *self, __u64 ip)
424 list_for_each_entry(pos, &self->maps, node)
425 if (ip >= pos->start && ip <= pos->end)
431 static size_t threads__fprintf(FILE *fp)
436 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
437 struct thread *pos = rb_entry(nd, struct thread, rb_node);
439 ret += thread__fprintf(pos, fp);
446 * histogram, sorted on item, collects counts
449 static struct rb_root hist;
452 struct rb_node rb_node;
454 struct thread *thread;
458 struct symbol *parent;
466 * configurable sorting bits
470 struct list_head list;
474 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
475 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
476 size_t (*print)(FILE *fp, struct hist_entry *);
479 static int64_t cmp_null(void *l, void *r)
492 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
494 return right->thread->pid - left->thread->pid;
498 sort__thread_print(FILE *fp, struct hist_entry *self)
500 return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
503 static struct sort_entry sort_thread = {
504 .header = " Command: Pid",
505 .cmp = sort__thread_cmp,
506 .print = sort__thread_print,
512 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
514 return right->thread->pid - left->thread->pid;
518 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
520 char *comm_l = left->thread->comm;
521 char *comm_r = right->thread->comm;
523 if (!comm_l || !comm_r)
524 return cmp_null(comm_l, comm_r);
526 return strcmp(comm_l, comm_r);
530 sort__comm_print(FILE *fp, struct hist_entry *self)
532 return fprintf(fp, "%16s", self->thread->comm);
535 static struct sort_entry sort_comm = {
536 .header = " Command",
537 .cmp = sort__comm_cmp,
538 .collapse = sort__comm_collapse,
539 .print = sort__comm_print,
545 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
547 struct dso *dso_l = left->dso;
548 struct dso *dso_r = right->dso;
550 if (!dso_l || !dso_r)
551 return cmp_null(dso_l, dso_r);
553 return strcmp(dso_l->name, dso_r->name);
557 sort__dso_print(FILE *fp, struct hist_entry *self)
560 return fprintf(fp, "%-25s", self->dso->name);
562 return fprintf(fp, "%016llx ", (__u64)self->ip);
565 static struct sort_entry sort_dso = {
566 .header = "Shared Object ",
567 .cmp = sort__dso_cmp,
568 .print = sort__dso_print,
574 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
578 if (left->sym == right->sym)
581 ip_l = left->sym ? left->sym->start : left->ip;
582 ip_r = right->sym ? right->sym->start : right->ip;
584 return (int64_t)(ip_r - ip_l);
588 sort__sym_print(FILE *fp, struct hist_entry *self)
593 ret += fprintf(fp, "%#018llx ", (__u64)self->ip);
596 ret += fprintf(fp, "[%c] %s",
597 self->dso == kernel_dso ? 'k' : '.', self->sym->name);
599 ret += fprintf(fp, "%#016llx", (__u64)self->ip);
605 static struct sort_entry sort_sym = {
607 .cmp = sort__sym_cmp,
608 .print = sort__sym_print,
614 sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
616 struct symbol *sym_l = left->parent;
617 struct symbol *sym_r = right->parent;
619 if (!sym_l || !sym_r)
620 return cmp_null(sym_l, sym_r);
622 return strcmp(sym_l->name, sym_r->name);
626 sort__parent_print(FILE *fp, struct hist_entry *self)
630 ret += fprintf(fp, "%-20s", self->parent ? self->parent->name : "[other]");
635 static struct sort_entry sort_parent = {
636 .header = "Parent symbol ",
637 .cmp = sort__parent_cmp,
638 .print = sort__parent_print,
641 static int sort__need_collapse = 0;
642 static int sort__has_parent = 0;
644 struct sort_dimension {
646 struct sort_entry *entry;
650 static struct sort_dimension sort_dimensions[] = {
651 { .name = "pid", .entry = &sort_thread, },
652 { .name = "comm", .entry = &sort_comm, },
653 { .name = "dso", .entry = &sort_dso, },
654 { .name = "symbol", .entry = &sort_sym, },
655 { .name = "parent", .entry = &sort_parent, },
658 static LIST_HEAD(hist_entry__sort_list);
660 static int sort_dimension__add(char *tok)
664 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
665 struct sort_dimension *sd = &sort_dimensions[i];
670 if (strncasecmp(tok, sd->name, strlen(tok)))
673 if (sd->entry->collapse)
674 sort__need_collapse = 1;
676 if (sd->entry == &sort_parent) {
677 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
681 regerror(ret, &parent_regex, err, sizeof(err));
682 fprintf(stderr, "Invalid regex: %s\n%s",
683 parent_pattern, err);
686 sort__has_parent = 1;
689 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
699 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
701 struct sort_entry *se;
704 list_for_each_entry(se, &hist_entry__sort_list, list) {
705 cmp = se->cmp(left, right);
714 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
716 struct sort_entry *se;
719 list_for_each_entry(se, &hist_entry__sort_list, list) {
720 int64_t (*f)(struct hist_entry *, struct hist_entry *);
722 f = se->collapse ?: se->cmp;
724 cmp = f(left, right);
733 hist_entry__fprintf(FILE *fp, struct hist_entry *self, __u64 total_samples)
735 struct sort_entry *se;
739 double percent = self->count * 100.0 / total_samples;
740 char *color = PERF_COLOR_NORMAL;
743 * We color high-overhead entries in red, mid-overhead
744 * entries in green - and keep the low overhead places
747 if (percent >= 5.0) {
748 color = PERF_COLOR_RED;
751 color = PERF_COLOR_GREEN;
754 ret = color_fprintf(fp, color, " %6.2f%%",
755 (self->count * 100.0) / total_samples);
757 ret = fprintf(fp, "%12Ld ", self->count);
759 list_for_each_entry(se, &hist_entry__sort_list, list) {
761 ret += se->print(fp, self);
764 ret += fprintf(fp, "\n");
773 static struct symbol *
774 resolve_symbol(struct thread *thread, struct map **mapp,
775 struct dso **dsop, __u64 *ipp)
777 struct dso *dso = dsop ? *dsop : NULL;
778 struct map *map = mapp ? *mapp : NULL;
790 map = thread__find_map(thread, ip);
795 ip = map->map_ip(map, ip);
801 * If this is outside of all known maps,
802 * and is a negative address, try to look it
803 * up in the kernel dso, as it might be a
804 * vsyscall (which executes in user-mode):
806 if ((long long)ip < 0)
809 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
817 return dso->find_symbol(dso, ip);
820 static struct symbol *call__match(struct symbol *sym)
825 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
832 * collect histogram counts
836 hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
837 struct symbol *sym, __u64 ip, struct perf_callchain_entry *chain,
838 char level, __u64 count)
840 struct rb_node **p = &hist.rb_node;
841 struct rb_node *parent = NULL;
842 struct hist_entry *he;
843 struct hist_entry entry = {
854 if (sort__has_parent && chain) {
855 int i, nr = chain->hv;
860 for (i = 0; i < chain->kernel; i++) {
861 ip = chain->ip[nr + i];
863 sym = resolve_symbol(thread, NULL, &dso, &ip);
864 entry.parent = call__match(sym);
870 for (i = 0; i < chain->user; i++) {
871 ip = chain->ip[nr + i];
872 sym = resolve_symbol(thread, NULL, NULL, &ip);
873 entry.parent = call__match(sym);
883 he = rb_entry(parent, struct hist_entry, rb_node);
885 cmp = hist_entry__cmp(&entry, he);
898 he = malloc(sizeof(*he));
902 rb_link_node(&he->rb_node, parent, p);
903 rb_insert_color(&he->rb_node, &hist);
908 static void hist_entry__free(struct hist_entry *he)
914 * collapse the histogram
917 static struct rb_root collapse_hists;
919 static void collapse__insert_entry(struct hist_entry *he)
921 struct rb_node **p = &collapse_hists.rb_node;
922 struct rb_node *parent = NULL;
923 struct hist_entry *iter;
928 iter = rb_entry(parent, struct hist_entry, rb_node);
930 cmp = hist_entry__collapse(iter, he);
933 iter->count += he->count;
934 hist_entry__free(he);
944 rb_link_node(&he->rb_node, parent, p);
945 rb_insert_color(&he->rb_node, &collapse_hists);
948 static void collapse__resort(void)
950 struct rb_node *next;
951 struct hist_entry *n;
953 if (!sort__need_collapse)
956 next = rb_first(&hist);
958 n = rb_entry(next, struct hist_entry, rb_node);
959 next = rb_next(&n->rb_node);
961 rb_erase(&n->rb_node, &hist);
962 collapse__insert_entry(n);
967 * reverse the map, sort on count.
970 static struct rb_root output_hists;
972 static void output__insert_entry(struct hist_entry *he)
974 struct rb_node **p = &output_hists.rb_node;
975 struct rb_node *parent = NULL;
976 struct hist_entry *iter;
980 iter = rb_entry(parent, struct hist_entry, rb_node);
982 if (he->count > iter->count)
988 rb_link_node(&he->rb_node, parent, p);
989 rb_insert_color(&he->rb_node, &output_hists);
992 static void output__resort(void)
994 struct rb_node *next;
995 struct hist_entry *n;
996 struct rb_root *tree = &hist;
998 if (sort__need_collapse)
999 tree = &collapse_hists;
1001 next = rb_first(tree);
1004 n = rb_entry(next, struct hist_entry, rb_node);
1005 next = rb_next(&n->rb_node);
1007 rb_erase(&n->rb_node, tree);
1008 output__insert_entry(n);
1012 static size_t output__fprintf(FILE *fp, __u64 total_samples)
1014 struct hist_entry *pos;
1015 struct sort_entry *se;
1021 fprintf(fp, "# (%Ld samples)\n", (__u64)total_samples);
1024 fprintf(fp, "# Overhead");
1025 list_for_each_entry(se, &hist_entry__sort_list, list)
1026 fprintf(fp, " %s", se->header);
1029 fprintf(fp, "# ........");
1030 list_for_each_entry(se, &hist_entry__sort_list, list) {
1034 for (i = 0; i < strlen(se->header); i++)
1041 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
1042 pos = rb_entry(nd, struct hist_entry, rb_node);
1043 ret += hist_entry__fprintf(fp, pos, total_samples);
1046 if (!strcmp(sort_order, default_sort_order)) {
1048 fprintf(fp, "# (For more details, try: perf report --sort comm,dso,symbol)\n");
1056 static void register_idle_thread(void)
1058 struct thread *thread = threads__findnew(0);
1060 if (thread == NULL ||
1061 thread__set_comm(thread, "[idle]")) {
1062 fprintf(stderr, "problem inserting idle task.\n");
1067 static unsigned long total = 0,
1073 static int validate_chain(struct perf_callchain_entry *chain, event_t *event)
1075 unsigned int chain_size;
1077 if (chain->nr > MAX_STACK_DEPTH)
1079 if (chain->hv > MAX_STACK_DEPTH)
1081 if (chain->kernel > MAX_STACK_DEPTH)
1083 if (chain->user > MAX_STACK_DEPTH)
1085 if (chain->hv + chain->kernel + chain->user != chain->nr)
1088 chain_size = event->header.size;
1089 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
1091 if (chain->nr*sizeof(__u64) > chain_size)
1098 process_overflow_event(event_t *event, unsigned long offset, unsigned long head)
1102 struct dso *dso = NULL;
1103 struct thread *thread = threads__findnew(event->ip.pid);
1104 __u64 ip = event->ip.ip;
1106 struct map *map = NULL;
1107 void *more_data = event->ip.__more_data;
1108 struct perf_callchain_entry *chain = NULL;
1110 if (event->header.type & PERF_SAMPLE_PERIOD) {
1111 period = *(__u64 *)more_data;
1112 more_data += sizeof(__u64);
1115 dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p period: %Ld\n",
1116 (void *)(offset + head),
1117 (void *)(long)(event->header.size),
1123 if (event->header.type & PERF_SAMPLE_CALLCHAIN) {
1126 chain = (void *)more_data;
1128 dprintf("... chain: u:%d, k:%d, nr:%d\n",
1133 if (validate_chain(chain, event) < 0) {
1134 eprintf("call-chain problem with event, skipping it.\n");
1139 for (i = 0; i < chain->nr; i++)
1140 dprintf("..... %2d: %016Lx\n", i, chain->ip[i]);
1144 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1146 if (thread == NULL) {
1147 eprintf("problem processing %d event, skipping it.\n",
1148 event->header.type);
1152 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
1158 dprintf(" ...... dso: %s\n", dso->name);
1160 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
1168 dprintf(" ...... dso: [hypervisor]\n");
1171 if (show & show_mask) {
1172 struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
1174 if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
1175 eprintf("problem incrementing symbol count, skipping event\n");
1185 process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
1187 struct thread *thread = threads__findnew(event->mmap.pid);
1188 struct map *map = map__new(&event->mmap);
1190 dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
1191 (void *)(offset + head),
1192 (void *)(long)(event->header.size),
1194 (void *)(long)event->mmap.start,
1195 (void *)(long)event->mmap.len,
1196 (void *)(long)event->mmap.pgoff,
1197 event->mmap.filename);
1199 if (thread == NULL || map == NULL) {
1200 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
1204 thread__insert_map(thread, map);
1211 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
1213 struct thread *thread = threads__findnew(event->comm.pid);
1215 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
1216 (void *)(offset + head),
1217 (void *)(long)(event->header.size),
1218 event->comm.comm, event->comm.pid);
1220 if (thread == NULL ||
1221 thread__set_comm(thread, event->comm.comm)) {
1222 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
1231 process_fork_event(event_t *event, unsigned long offset, unsigned long head)
1233 struct thread *thread = threads__findnew(event->fork.pid);
1234 struct thread *parent = threads__findnew(event->fork.ppid);
1236 dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
1237 (void *)(offset + head),
1238 (void *)(long)(event->header.size),
1239 event->fork.pid, event->fork.ppid);
1241 if (!thread || !parent || thread__fork(thread, parent)) {
1242 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
1251 process_period_event(event_t *event, unsigned long offset, unsigned long head)
1253 dprintf("%p [%p]: PERF_EVENT_PERIOD: time:%Ld, id:%Ld: period:%Ld\n",
1254 (void *)(offset + head),
1255 (void *)(long)(event->header.size),
1258 event->period.sample_period);
1263 static void trace_event(event_t *event)
1265 unsigned char *raw_event = (void *)event;
1266 char *color = PERF_COLOR_BLUE;
1273 cdprintf("\n. ... raw event: size %d bytes\n", event->header.size);
1275 for (i = 0; i < event->header.size; i++) {
1276 if ((i & 15) == 0) {
1278 cdprintf(" %04x: ", i);
1281 cdprintf(" %02x", raw_event[i]);
1283 if (((i & 15) == 15) || i == event->header.size-1) {
1285 for (j = 0; j < 15-(i & 15); j++)
1287 for (j = 0; j < (i & 15); j++) {
1288 if (isprint(raw_event[i-15+j]))
1289 cdprintf("%c", raw_event[i-15+j]);
1300 process_event(event_t *event, unsigned long offset, unsigned long head)
1304 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW)
1305 return process_overflow_event(event, offset, head);
1307 switch (event->header.type) {
1308 case PERF_EVENT_MMAP:
1309 return process_mmap_event(event, offset, head);
1311 case PERF_EVENT_COMM:
1312 return process_comm_event(event, offset, head);
1314 case PERF_EVENT_FORK:
1315 return process_fork_event(event, offset, head);
1317 case PERF_EVENT_PERIOD:
1318 return process_period_event(event, offset, head);
1320 * We dont process them right now but they are fine:
1323 case PERF_EVENT_THROTTLE:
1324 case PERF_EVENT_UNTHROTTLE:
1334 static int __cmd_report(void)
1336 int ret, rc = EXIT_FAILURE;
1337 unsigned long offset = 0;
1338 unsigned long head = 0;
1344 register_idle_thread();
1346 input = open(input_name, O_RDONLY);
1348 fprintf(stderr, " failed to open file: %s", input_name);
1349 if (!strcmp(input_name, "perf.data"))
1350 fprintf(stderr, " (try 'perf record' first)");
1351 fprintf(stderr, "\n");
1355 ret = fstat(input, &stat);
1357 perror("failed to stat file");
1361 if (!stat.st_size) {
1362 fprintf(stderr, "zero-sized file, nothing to do!\n");
1366 if (load_kernel() < 0) {
1367 perror("failed to load kernel symbols");
1368 return EXIT_FAILURE;
1372 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
1373 perror("failed to get the current directory");
1374 return EXIT_FAILURE;
1376 cwdlen = strlen(cwd);
1382 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1383 MAP_SHARED, input, offset);
1384 if (buf == MAP_FAILED) {
1385 perror("failed to mmap file");
1390 event = (event_t *)(buf + head);
1392 size = event->header.size;
1396 if (head + event->header.size >= page_size * mmap_window) {
1397 unsigned long shift = page_size * (head / page_size);
1400 ret = munmap(buf, page_size * mmap_window);
1408 size = event->header.size;
1410 dprintf("\n%p [%p]: event: %d\n",
1411 (void *)(offset + head),
1412 (void *)(long)event->header.size,
1413 event->header.type);
1415 if (!size || process_event(event, offset, head) < 0) {
1417 dprintf("%p [%p]: skipping unknown header type: %d\n",
1418 (void *)(offset + head),
1419 (void *)(long)(event->header.size),
1420 event->header.type);
1425 * assume we lost track of the stream, check alignment, and
1426 * increment a single u64 in the hope to catch on again 'soon'.
1429 if (unlikely(head & 7))
1437 if (offset + head < stat.st_size)
1443 dprintf(" IP events: %10ld\n", total);
1444 dprintf(" mmap events: %10ld\n", total_mmap);
1445 dprintf(" comm events: %10ld\n", total_comm);
1446 dprintf(" fork events: %10ld\n", total_fork);
1447 dprintf(" unknown events: %10ld\n", total_unknown);
1453 threads__fprintf(stdout);
1456 dsos__fprintf(stdout);
1460 output__fprintf(stdout, total);
1465 static const char * const report_usage[] = {
1466 "perf report [<options>] <command>",
1470 static const struct option options[] = {
1471 OPT_STRING('i', "input", &input_name, "file",
1473 OPT_BOOLEAN('v', "verbose", &verbose,
1474 "be more verbose (show symbol address, etc)"),
1475 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1476 "dump raw trace in ASCII"),
1477 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
1478 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1479 "sort by key(s): pid, comm, dso, symbol, parent"),
1480 OPT_BOOLEAN('P', "full-paths", &full_paths,
1481 "Don't shorten the pathnames taking into account the cwd"),
1482 OPT_STRING('p', "parent", &parent_pattern, "regex",
1483 "regex filter to identify parent, see: '--sort parent'"),
1487 static void setup_sorting(void)
1489 char *tmp, *tok, *str = strdup(sort_order);
1491 for (tok = strtok_r(str, ", ", &tmp);
1492 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1493 if (sort_dimension__add(tok) < 0) {
1494 error("Unknown --sort key: `%s'", tok);
1495 usage_with_options(report_usage, options);
1502 int cmd_report(int argc, const char **argv, const char *prefix)
1506 page_size = getpagesize();
1508 argc = parse_options(argc, argv, options, report_usage, 0);
1513 * Any (unrecognized) arguments left?
1516 usage_with_options(report_usage, options);
1520 return __cmd_report();