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[];
71 struct perf_event_header header;
76 char filename[PATH_MAX];
80 struct perf_event_header header;
86 struct perf_event_header header;
91 struct perf_event_header header;
98 struct perf_event_header header;
104 struct perf_event_header header;
110 typedef union event_union {
111 struct perf_event_header header;
113 struct mmap_event mmap;
114 struct comm_event comm;
115 struct fork_event fork;
116 struct period_event period;
117 struct lost_event lost;
118 struct read_event read;
121 static LIST_HEAD(dsos);
122 static struct dso *kernel_dso;
123 static struct dso *vdso;
125 static void dsos__add(struct dso *dso)
127 list_add_tail(&dso->node, &dsos);
130 static struct dso *dsos__find(const char *name)
134 list_for_each_entry(pos, &dsos, node)
135 if (strcmp(pos->name, name) == 0)
140 static struct dso *dsos__findnew(const char *name)
142 struct dso *dso = dsos__find(name);
148 dso = dso__new(name, 0);
152 nr = dso__load(dso, NULL, verbose);
154 eprintf("Failed to open: %s\n", name);
158 eprintf("No symbols found in: %s, maybe install a debug package?\n", name);
169 static void dsos__fprintf(FILE *fp)
173 list_for_each_entry(pos, &dsos, node)
174 dso__fprintf(pos, fp);
177 static struct symbol *vdso__find_symbol(struct dso *dso, u64 ip)
179 return dso__find_symbol(kernel_dso, ip);
182 static int load_kernel(void)
186 kernel_dso = dso__new("[kernel]", 0);
190 err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose);
192 dso__delete(kernel_dso);
195 dsos__add(kernel_dso);
197 vdso = dso__new("[vdso]", 0);
201 vdso->find_symbol = vdso__find_symbol;
208 static char __cwd[PATH_MAX];
209 static char *cwd = __cwd;
212 static int strcommon(const char *pathname)
216 while (pathname[n] == cwd[n] && n < cwdlen)
223 struct list_head node;
227 u64 (*map_ip)(struct map *, u64);
231 static u64 map__map_ip(struct map *map, u64 ip)
233 return ip - map->start + map->pgoff;
236 static u64 vdso__map_ip(struct map *map, u64 ip)
241 static inline int is_anon_memory(const char *filename)
243 return strcmp(filename, "//anon") == 0;
246 static struct map *map__new(struct mmap_event *event)
248 struct map *self = malloc(sizeof(*self));
251 const char *filename = event->filename;
252 char newfilename[PATH_MAX];
256 int n = strcommon(filename);
259 snprintf(newfilename, sizeof(newfilename),
260 ".%s", filename + n);
261 filename = newfilename;
265 anon = is_anon_memory(filename);
268 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
269 filename = newfilename;
272 self->start = event->start;
273 self->end = event->start + event->len;
274 self->pgoff = event->pgoff;
276 self->dso = dsos__findnew(filename);
277 if (self->dso == NULL)
280 if (self->dso == vdso || anon)
281 self->map_ip = vdso__map_ip;
283 self->map_ip = map__map_ip;
291 static struct map *map__clone(struct map *self)
293 struct map *map = malloc(sizeof(*self));
298 memcpy(map, self, sizeof(*self));
303 static int map__overlap(struct map *l, struct map *r)
305 if (l->start > r->start) {
311 if (l->end > r->start)
317 static size_t map__fprintf(struct map *self, FILE *fp)
319 return fprintf(fp, " %Lx-%Lx %Lx %s\n",
320 self->start, self->end, self->pgoff, self->dso->name);
325 struct rb_node rb_node;
326 struct list_head maps;
331 static struct thread *thread__new(pid_t pid)
333 struct thread *self = malloc(sizeof(*self));
337 self->comm = malloc(32);
339 snprintf(self->comm, 32, ":%d", self->pid);
340 INIT_LIST_HEAD(&self->maps);
346 static int thread__set_comm(struct thread *self, const char *comm)
350 self->comm = strdup(comm);
351 return self->comm ? 0 : -ENOMEM;
354 static size_t thread__fprintf(struct thread *self, FILE *fp)
357 size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
359 list_for_each_entry(pos, &self->maps, node)
360 ret += map__fprintf(pos, fp);
366 static struct rb_root threads;
367 static struct thread *last_match;
369 static struct thread *threads__findnew(pid_t pid)
371 struct rb_node **p = &threads.rb_node;
372 struct rb_node *parent = NULL;
376 * Font-end cache - PID lookups come in blocks,
377 * so most of the time we dont have to look up
380 if (last_match && last_match->pid == pid)
385 th = rb_entry(parent, struct thread, rb_node);
387 if (th->pid == pid) {
398 th = thread__new(pid);
400 rb_link_node(&th->rb_node, parent, p);
401 rb_insert_color(&th->rb_node, &threads);
408 static void thread__insert_map(struct thread *self, struct map *map)
410 struct map *pos, *tmp;
412 list_for_each_entry_safe(pos, tmp, &self->maps, node) {
413 if (map__overlap(pos, map)) {
415 printf("overlapping maps:\n");
416 map__fprintf(map, stdout);
417 map__fprintf(pos, stdout);
420 if (map->start <= pos->start && map->end > pos->start)
421 pos->start = map->end;
423 if (map->end >= pos->end && map->start < pos->end)
424 pos->end = map->start;
427 printf("after collision:\n");
428 map__fprintf(pos, stdout);
431 if (pos->start >= pos->end) {
432 list_del_init(&pos->node);
438 list_add_tail(&map->node, &self->maps);
441 static int thread__fork(struct thread *self, struct thread *parent)
447 self->comm = strdup(parent->comm);
451 list_for_each_entry(map, &parent->maps, node) {
452 struct map *new = map__clone(map);
455 thread__insert_map(self, new);
461 static struct map *thread__find_map(struct thread *self, u64 ip)
468 list_for_each_entry(pos, &self->maps, node)
469 if (ip >= pos->start && ip <= pos->end)
475 static size_t threads__fprintf(FILE *fp)
480 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
481 struct thread *pos = rb_entry(nd, struct thread, rb_node);
483 ret += thread__fprintf(pos, fp);
490 * histogram, sorted on item, collects counts
493 static struct rb_root hist;
496 struct rb_node rb_node;
498 struct thread *thread;
502 struct symbol *parent;
510 * configurable sorting bits
514 struct list_head list;
518 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
519 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
520 size_t (*print)(FILE *fp, struct hist_entry *);
523 static int64_t cmp_null(void *l, void *r)
536 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
538 return right->thread->pid - left->thread->pid;
542 sort__thread_print(FILE *fp, struct hist_entry *self)
544 return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
547 static struct sort_entry sort_thread = {
548 .header = " Command: Pid",
549 .cmp = sort__thread_cmp,
550 .print = sort__thread_print,
556 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
558 return right->thread->pid - left->thread->pid;
562 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
564 char *comm_l = left->thread->comm;
565 char *comm_r = right->thread->comm;
567 if (!comm_l || !comm_r)
568 return cmp_null(comm_l, comm_r);
570 return strcmp(comm_l, comm_r);
574 sort__comm_print(FILE *fp, struct hist_entry *self)
576 return fprintf(fp, "%16s", self->thread->comm);
579 static struct sort_entry sort_comm = {
580 .header = " Command",
581 .cmp = sort__comm_cmp,
582 .collapse = sort__comm_collapse,
583 .print = sort__comm_print,
589 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
591 struct dso *dso_l = left->dso;
592 struct dso *dso_r = right->dso;
594 if (!dso_l || !dso_r)
595 return cmp_null(dso_l, dso_r);
597 return strcmp(dso_l->name, dso_r->name);
601 sort__dso_print(FILE *fp, struct hist_entry *self)
604 return fprintf(fp, "%-25s", self->dso->name);
606 return fprintf(fp, "%016llx ", (u64)self->ip);
609 static struct sort_entry sort_dso = {
610 .header = "Shared Object ",
611 .cmp = sort__dso_cmp,
612 .print = sort__dso_print,
618 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
622 if (left->sym == right->sym)
625 ip_l = left->sym ? left->sym->start : left->ip;
626 ip_r = right->sym ? right->sym->start : right->ip;
628 return (int64_t)(ip_r - ip_l);
632 sort__sym_print(FILE *fp, struct hist_entry *self)
637 ret += fprintf(fp, "%#018llx ", (u64)self->ip);
640 ret += fprintf(fp, "[%c] %s",
641 self->dso == kernel_dso ? 'k' : '.', self->sym->name);
643 ret += fprintf(fp, "%#016llx", (u64)self->ip);
649 static struct sort_entry sort_sym = {
651 .cmp = sort__sym_cmp,
652 .print = sort__sym_print,
658 sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
660 struct symbol *sym_l = left->parent;
661 struct symbol *sym_r = right->parent;
663 if (!sym_l || !sym_r)
664 return cmp_null(sym_l, sym_r);
666 return strcmp(sym_l->name, sym_r->name);
670 sort__parent_print(FILE *fp, struct hist_entry *self)
674 ret += fprintf(fp, "%-20s", self->parent ? self->parent->name : "[other]");
679 static struct sort_entry sort_parent = {
680 .header = "Parent symbol ",
681 .cmp = sort__parent_cmp,
682 .print = sort__parent_print,
685 static int sort__need_collapse = 0;
686 static int sort__has_parent = 0;
688 struct sort_dimension {
690 struct sort_entry *entry;
694 static struct sort_dimension sort_dimensions[] = {
695 { .name = "pid", .entry = &sort_thread, },
696 { .name = "comm", .entry = &sort_comm, },
697 { .name = "dso", .entry = &sort_dso, },
698 { .name = "symbol", .entry = &sort_sym, },
699 { .name = "parent", .entry = &sort_parent, },
702 static LIST_HEAD(hist_entry__sort_list);
704 static int sort_dimension__add(char *tok)
708 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
709 struct sort_dimension *sd = &sort_dimensions[i];
714 if (strncasecmp(tok, sd->name, strlen(tok)))
717 if (sd->entry->collapse)
718 sort__need_collapse = 1;
720 if (sd->entry == &sort_parent) {
721 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
725 regerror(ret, &parent_regex, err, sizeof(err));
726 fprintf(stderr, "Invalid regex: %s\n%s",
727 parent_pattern, err);
730 sort__has_parent = 1;
733 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
743 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
745 struct sort_entry *se;
748 list_for_each_entry(se, &hist_entry__sort_list, list) {
749 cmp = se->cmp(left, right);
758 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
760 struct sort_entry *se;
763 list_for_each_entry(se, &hist_entry__sort_list, list) {
764 int64_t (*f)(struct hist_entry *, struct hist_entry *);
766 f = se->collapse ?: se->cmp;
768 cmp = f(left, right);
777 hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
779 struct sort_entry *se;
782 if (exclude_other && !self->parent)
786 double percent = self->count * 100.0 / total_samples;
787 char *color = PERF_COLOR_NORMAL;
790 * We color high-overhead entries in red, mid-overhead
791 * entries in green - and keep the low overhead places
794 if (percent >= 5.0) {
795 color = PERF_COLOR_RED;
798 color = PERF_COLOR_GREEN;
801 ret = color_fprintf(fp, color, " %6.2f%%",
802 (self->count * 100.0) / total_samples);
804 ret = fprintf(fp, "%12Ld ", self->count);
806 list_for_each_entry(se, &hist_entry__sort_list, list) {
807 if (exclude_other && (se == &sort_parent))
811 ret += se->print(fp, self);
814 ret += fprintf(fp, "\n");
823 static struct symbol *
824 resolve_symbol(struct thread *thread, struct map **mapp,
825 struct dso **dsop, u64 *ipp)
827 struct dso *dso = dsop ? *dsop : NULL;
828 struct map *map = mapp ? *mapp : NULL;
840 map = thread__find_map(thread, ip);
845 ip = map->map_ip(map, ip);
850 * If this is outside of all known maps,
851 * and is a negative address, try to look it
852 * up in the kernel dso, as it might be a
853 * vsyscall (which executes in user-mode):
855 if ((long long)ip < 0)
858 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
859 dprintf(" ...... map: %Lx -> %Lx\n", *ipp, ip);
868 return dso->find_symbol(dso, ip);
871 static int call__match(struct symbol *sym)
873 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
880 * collect histogram counts
884 hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
885 struct symbol *sym, u64 ip, struct ip_callchain *chain,
886 char level, u64 count)
888 struct rb_node **p = &hist.rb_node;
889 struct rb_node *parent = NULL;
890 struct hist_entry *he;
891 struct hist_entry entry = {
903 if (sort__has_parent && chain) {
904 u64 context = PERF_CONTEXT_MAX;
907 for (i = 0; i < chain->nr; i++) {
908 u64 ip = chain->ips[i];
909 struct dso *dso = NULL;
912 if (ip >= PERF_CONTEXT_MAX) {
918 case PERF_CONTEXT_KERNEL:
925 sym = resolve_symbol(thread, NULL, &dso, &ip);
927 if (sym && call__match(sym)) {
936 he = rb_entry(parent, struct hist_entry, rb_node);
938 cmp = hist_entry__cmp(&entry, he);
951 he = malloc(sizeof(*he));
955 rb_link_node(&he->rb_node, parent, p);
956 rb_insert_color(&he->rb_node, &hist);
961 static void hist_entry__free(struct hist_entry *he)
967 * collapse the histogram
970 static struct rb_root collapse_hists;
972 static void collapse__insert_entry(struct hist_entry *he)
974 struct rb_node **p = &collapse_hists.rb_node;
975 struct rb_node *parent = NULL;
976 struct hist_entry *iter;
981 iter = rb_entry(parent, struct hist_entry, rb_node);
983 cmp = hist_entry__collapse(iter, he);
986 iter->count += he->count;
987 hist_entry__free(he);
997 rb_link_node(&he->rb_node, parent, p);
998 rb_insert_color(&he->rb_node, &collapse_hists);
1001 static void collapse__resort(void)
1003 struct rb_node *next;
1004 struct hist_entry *n;
1006 if (!sort__need_collapse)
1009 next = rb_first(&hist);
1011 n = rb_entry(next, struct hist_entry, rb_node);
1012 next = rb_next(&n->rb_node);
1014 rb_erase(&n->rb_node, &hist);
1015 collapse__insert_entry(n);
1020 * reverse the map, sort on count.
1023 static struct rb_root output_hists;
1025 static void output__insert_entry(struct hist_entry *he)
1027 struct rb_node **p = &output_hists.rb_node;
1028 struct rb_node *parent = NULL;
1029 struct hist_entry *iter;
1031 while (*p != NULL) {
1033 iter = rb_entry(parent, struct hist_entry, rb_node);
1035 if (he->count > iter->count)
1038 p = &(*p)->rb_right;
1041 rb_link_node(&he->rb_node, parent, p);
1042 rb_insert_color(&he->rb_node, &output_hists);
1045 static void output__resort(void)
1047 struct rb_node *next;
1048 struct hist_entry *n;
1049 struct rb_root *tree = &hist;
1051 if (sort__need_collapse)
1052 tree = &collapse_hists;
1054 next = rb_first(tree);
1057 n = rb_entry(next, struct hist_entry, rb_node);
1058 next = rb_next(&n->rb_node);
1060 rb_erase(&n->rb_node, tree);
1061 output__insert_entry(n);
1065 static size_t output__fprintf(FILE *fp, u64 total_samples)
1067 struct hist_entry *pos;
1068 struct sort_entry *se;
1074 fprintf(fp, "# (%Ld samples)\n", (u64)total_samples);
1077 fprintf(fp, "# Overhead");
1078 list_for_each_entry(se, &hist_entry__sort_list, list) {
1079 if (exclude_other && (se == &sort_parent))
1081 fprintf(fp, " %s", se->header);
1085 fprintf(fp, "# ........");
1086 list_for_each_entry(se, &hist_entry__sort_list, list) {
1089 if (exclude_other && (se == &sort_parent))
1093 for (i = 0; i < strlen(se->header); i++)
1100 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
1101 pos = rb_entry(nd, struct hist_entry, rb_node);
1102 ret += hist_entry__fprintf(fp, pos, total_samples);
1105 if (sort_order == default_sort_order &&
1106 parent_pattern == default_parent_pattern) {
1108 fprintf(fp, "# (For more details, try: perf report --sort comm,dso,symbol)\n");
1116 static void register_idle_thread(void)
1118 struct thread *thread = threads__findnew(0);
1120 if (thread == NULL ||
1121 thread__set_comm(thread, "[idle]")) {
1122 fprintf(stderr, "problem inserting idle task.\n");
1127 static unsigned long total = 0,
1134 static int validate_chain(struct ip_callchain *chain, event_t *event)
1136 unsigned int chain_size;
1138 chain_size = event->header.size;
1139 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
1141 if (chain->nr*sizeof(u64) > chain_size)
1148 process_sample_event(event_t *event, unsigned long offset, unsigned long head)
1152 struct dso *dso = NULL;
1153 struct thread *thread = threads__findnew(event->ip.pid);
1154 u64 ip = event->ip.ip;
1156 struct map *map = NULL;
1157 void *more_data = event->ip.__more_data;
1158 struct ip_callchain *chain = NULL;
1160 if (sample_type & PERF_SAMPLE_PERIOD) {
1161 period = *(u64 *)more_data;
1162 more_data += sizeof(u64);
1165 dprintf("%p [%p]: PERF_EVENT_SAMPLE (IP, %d): %d: %p period: %Ld\n",
1166 (void *)(offset + head),
1167 (void *)(long)(event->header.size),
1173 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
1176 chain = (void *)more_data;
1178 dprintf("... chain: nr:%Lu\n", chain->nr);
1180 if (validate_chain(chain, event) < 0) {
1181 eprintf("call-chain problem with event, skipping it.\n");
1186 for (i = 0; i < chain->nr; i++)
1187 dprintf("..... %2d: %016Lx\n", i, chain->ips[i]);
1191 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1193 if (thread == NULL) {
1194 eprintf("problem processing %d event, skipping it.\n",
1195 event->header.type);
1199 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
1205 dprintf(" ...... dso: %s\n", dso->name);
1207 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
1215 dprintf(" ...... dso: [hypervisor]\n");
1218 if (show & show_mask) {
1219 struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
1221 if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
1222 eprintf("problem incrementing symbol count, skipping event\n");
1232 process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
1234 struct thread *thread = threads__findnew(event->mmap.pid);
1235 struct map *map = map__new(&event->mmap);
1237 dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
1238 (void *)(offset + head),
1239 (void *)(long)(event->header.size),
1241 (void *)(long)event->mmap.start,
1242 (void *)(long)event->mmap.len,
1243 (void *)(long)event->mmap.pgoff,
1244 event->mmap.filename);
1246 if (thread == NULL || map == NULL) {
1247 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
1251 thread__insert_map(thread, map);
1258 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
1260 struct thread *thread = threads__findnew(event->comm.pid);
1262 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
1263 (void *)(offset + head),
1264 (void *)(long)(event->header.size),
1265 event->comm.comm, event->comm.pid);
1267 if (thread == NULL ||
1268 thread__set_comm(thread, event->comm.comm)) {
1269 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
1278 process_fork_event(event_t *event, unsigned long offset, unsigned long head)
1280 struct thread *thread = threads__findnew(event->fork.pid);
1281 struct thread *parent = threads__findnew(event->fork.ppid);
1283 dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
1284 (void *)(offset + head),
1285 (void *)(long)(event->header.size),
1286 event->fork.pid, event->fork.ppid);
1288 if (!thread || !parent || thread__fork(thread, parent)) {
1289 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
1298 process_period_event(event_t *event, unsigned long offset, unsigned long head)
1300 dprintf("%p [%p]: PERF_EVENT_PERIOD: time:%Ld, id:%Ld: period:%Ld\n",
1301 (void *)(offset + head),
1302 (void *)(long)(event->header.size),
1305 event->period.sample_period);
1311 process_lost_event(event_t *event, unsigned long offset, unsigned long head)
1313 dprintf("%p [%p]: PERF_EVENT_LOST: id:%Ld: lost:%Ld\n",
1314 (void *)(offset + head),
1315 (void *)(long)(event->header.size),
1319 total_lost += event->lost.lost;
1324 static void trace_event(event_t *event)
1326 unsigned char *raw_event = (void *)event;
1327 char *color = PERF_COLOR_BLUE;
1334 cdprintf("\n. ... raw event: size %d bytes\n", event->header.size);
1336 for (i = 0; i < event->header.size; i++) {
1337 if ((i & 15) == 0) {
1339 cdprintf(" %04x: ", i);
1342 cdprintf(" %02x", raw_event[i]);
1344 if (((i & 15) == 15) || i == event->header.size-1) {
1346 for (j = 0; j < 15-(i & 15); j++)
1348 for (j = 0; j < (i & 15); j++) {
1349 if (isprint(raw_event[i-15+j]))
1350 cdprintf("%c", raw_event[i-15+j]);
1361 process_read_event(event_t *event, unsigned long offset, unsigned long head)
1363 dprintf("%p [%p]: PERF_EVENT_READ: %d %d %Lu\n",
1364 (void *)(offset + head),
1365 (void *)(long)(event->header.size),
1374 process_event(event_t *event, unsigned long offset, unsigned long head)
1378 switch (event->header.type) {
1379 case PERF_EVENT_SAMPLE:
1380 return process_sample_event(event, offset, head);
1382 case PERF_EVENT_MMAP:
1383 return process_mmap_event(event, offset, head);
1385 case PERF_EVENT_COMM:
1386 return process_comm_event(event, offset, head);
1388 case PERF_EVENT_FORK:
1389 return process_fork_event(event, offset, head);
1391 case PERF_EVENT_PERIOD:
1392 return process_period_event(event, offset, head);
1394 case PERF_EVENT_LOST:
1395 return process_lost_event(event, offset, head);
1397 case PERF_EVENT_READ:
1398 return process_read_event(event, offset, head);
1401 * We dont process them right now but they are fine:
1404 case PERF_EVENT_THROTTLE:
1405 case PERF_EVENT_UNTHROTTLE:
1415 static struct perf_header *header;
1417 static u64 perf_header__sample_type(void)
1419 u64 sample_type = 0;
1422 for (i = 0; i < header->attrs; i++) {
1423 struct perf_header_attr *attr = header->attr[i];
1426 sample_type = attr->attr.sample_type;
1427 else if (sample_type != attr->attr.sample_type)
1428 die("non matching sample_type");
1434 static int __cmd_report(void)
1436 int ret, rc = EXIT_FAILURE;
1437 unsigned long offset = 0;
1438 unsigned long head, shift;
1444 register_idle_thread();
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, &stat);
1457 perror("failed to stat file");
1461 if (!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 (sort__has_parent && !(sample_type & PERF_SAMPLE_CALLCHAIN)) {
1472 fprintf(stderr, "selected --sort parent, but no callchain data\n");
1476 if (load_kernel() < 0) {
1477 perror("failed to load kernel symbols");
1478 return EXIT_FAILURE;
1482 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
1483 perror("failed to get the current directory");
1484 return EXIT_FAILURE;
1486 cwdlen = strlen(cwd);
1492 shift = page_size * (head / page_size);
1497 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1498 MAP_SHARED, input, offset);
1499 if (buf == MAP_FAILED) {
1500 perror("failed to mmap file");
1505 event = (event_t *)(buf + head);
1507 size = event->header.size;
1511 if (head + event->header.size >= page_size * mmap_window) {
1514 shift = page_size * (head / page_size);
1516 ret = munmap(buf, page_size * mmap_window);
1524 size = event->header.size;
1526 dprintf("\n%p [%p]: event: %d\n",
1527 (void *)(offset + head),
1528 (void *)(long)event->header.size,
1529 event->header.type);
1531 if (!size || process_event(event, offset, head) < 0) {
1533 dprintf("%p [%p]: skipping unknown header type: %d\n",
1534 (void *)(offset + head),
1535 (void *)(long)(event->header.size),
1536 event->header.type);
1541 * assume we lost track of the stream, check alignment, and
1542 * increment a single u64 in the hope to catch on again 'soon'.
1545 if (unlikely(head & 7))
1553 if (offset + head >= header->data_offset + header->data_size)
1556 if (offset + head < stat.st_size)
1563 dprintf(" IP events: %10ld\n", total);
1564 dprintf(" mmap events: %10ld\n", total_mmap);
1565 dprintf(" comm events: %10ld\n", total_comm);
1566 dprintf(" fork events: %10ld\n", total_fork);
1567 dprintf(" lost events: %10ld\n", total_lost);
1568 dprintf(" unknown events: %10ld\n", total_unknown);
1574 threads__fprintf(stdout);
1577 dsos__fprintf(stdout);
1581 output__fprintf(stdout, total);
1586 static const char * const report_usage[] = {
1587 "perf report [<options>] <command>",
1591 static const struct option options[] = {
1592 OPT_STRING('i', "input", &input_name, "file",
1594 OPT_BOOLEAN('v', "verbose", &verbose,
1595 "be more verbose (show symbol address, etc)"),
1596 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1597 "dump raw trace in ASCII"),
1598 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
1599 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1600 "sort by key(s): pid, comm, dso, symbol, parent"),
1601 OPT_BOOLEAN('P', "full-paths", &full_paths,
1602 "Don't shorten the pathnames taking into account the cwd"),
1603 OPT_STRING('p', "parent", &parent_pattern, "regex",
1604 "regex filter to identify parent, see: '--sort parent'"),
1605 OPT_BOOLEAN('x', "exclude-other", &exclude_other,
1606 "Only display entries with parent-match"),
1610 static void setup_sorting(void)
1612 char *tmp, *tok, *str = strdup(sort_order);
1614 for (tok = strtok_r(str, ", ", &tmp);
1615 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1616 if (sort_dimension__add(tok) < 0) {
1617 error("Unknown --sort key: `%s'", tok);
1618 usage_with_options(report_usage, options);
1625 int cmd_report(int argc, const char **argv, const char *prefix)
1629 page_size = getpagesize();
1631 argc = parse_options(argc, argv, options, report_usage, 0);
1635 if (parent_pattern != default_parent_pattern)
1636 sort_dimension__add("parent");
1641 * Any (unrecognized) arguments left?
1644 usage_with_options(report_usage, options);
1648 return __cmd_report();