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;
103 typedef union event_union {
104 struct perf_event_header header;
106 struct mmap_event mmap;
107 struct comm_event comm;
108 struct fork_event fork;
109 struct period_event period;
110 struct lost_event lost;
113 static LIST_HEAD(dsos);
114 static struct dso *kernel_dso;
115 static struct dso *vdso;
117 static void dsos__add(struct dso *dso)
119 list_add_tail(&dso->node, &dsos);
122 static struct dso *dsos__find(const char *name)
126 list_for_each_entry(pos, &dsos, node)
127 if (strcmp(pos->name, name) == 0)
132 static struct dso *dsos__findnew(const char *name)
134 struct dso *dso = dsos__find(name);
140 dso = dso__new(name, 0);
144 nr = dso__load(dso, NULL, verbose);
146 eprintf("Failed to open: %s\n", name);
150 eprintf("No symbols found in: %s, maybe install a debug package?\n", name);
161 static void dsos__fprintf(FILE *fp)
165 list_for_each_entry(pos, &dsos, node)
166 dso__fprintf(pos, fp);
169 static struct symbol *vdso__find_symbol(struct dso *dso, u64 ip)
171 return dso__find_symbol(kernel_dso, ip);
174 static int load_kernel(void)
178 kernel_dso = dso__new("[kernel]", 0);
182 err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose);
184 dso__delete(kernel_dso);
187 dsos__add(kernel_dso);
189 vdso = dso__new("[vdso]", 0);
193 vdso->find_symbol = vdso__find_symbol;
200 static char __cwd[PATH_MAX];
201 static char *cwd = __cwd;
204 static int strcommon(const char *pathname)
208 while (pathname[n] == cwd[n] && n < cwdlen)
215 struct list_head node;
219 u64 (*map_ip)(struct map *, u64);
223 static u64 map__map_ip(struct map *map, u64 ip)
225 return ip - map->start + map->pgoff;
228 static u64 vdso__map_ip(struct map *map, u64 ip)
233 static inline int is_anon_memory(const char *filename)
235 return strcmp(filename, "//anon") == 0;
238 static struct map *map__new(struct mmap_event *event)
240 struct map *self = malloc(sizeof(*self));
243 const char *filename = event->filename;
244 char newfilename[PATH_MAX];
248 int n = strcommon(filename);
251 snprintf(newfilename, sizeof(newfilename),
252 ".%s", filename + n);
253 filename = newfilename;
257 anon = is_anon_memory(filename);
260 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
261 filename = newfilename;
264 self->start = event->start;
265 self->end = event->start + event->len;
266 self->pgoff = event->pgoff;
268 self->dso = dsos__findnew(filename);
269 if (self->dso == NULL)
272 if (self->dso == vdso || anon)
273 self->map_ip = vdso__map_ip;
275 self->map_ip = map__map_ip;
283 static struct map *map__clone(struct map *self)
285 struct map *map = malloc(sizeof(*self));
290 memcpy(map, self, sizeof(*self));
295 static int map__overlap(struct map *l, struct map *r)
297 if (l->start > r->start) {
303 if (l->end > r->start)
309 static size_t map__fprintf(struct map *self, FILE *fp)
311 return fprintf(fp, " %Lx-%Lx %Lx %s\n",
312 self->start, self->end, self->pgoff, self->dso->name);
317 struct rb_node rb_node;
318 struct list_head maps;
323 static struct thread *thread__new(pid_t pid)
325 struct thread *self = malloc(sizeof(*self));
329 self->comm = malloc(32);
331 snprintf(self->comm, 32, ":%d", self->pid);
332 INIT_LIST_HEAD(&self->maps);
338 static int thread__set_comm(struct thread *self, const char *comm)
342 self->comm = strdup(comm);
343 return self->comm ? 0 : -ENOMEM;
346 static size_t thread__fprintf(struct thread *self, FILE *fp)
349 size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
351 list_for_each_entry(pos, &self->maps, node)
352 ret += map__fprintf(pos, fp);
358 static struct rb_root threads;
359 static struct thread *last_match;
361 static struct thread *threads__findnew(pid_t pid)
363 struct rb_node **p = &threads.rb_node;
364 struct rb_node *parent = NULL;
368 * Font-end cache - PID lookups come in blocks,
369 * so most of the time we dont have to look up
372 if (last_match && last_match->pid == pid)
377 th = rb_entry(parent, struct thread, rb_node);
379 if (th->pid == pid) {
390 th = thread__new(pid);
392 rb_link_node(&th->rb_node, parent, p);
393 rb_insert_color(&th->rb_node, &threads);
400 static void thread__insert_map(struct thread *self, struct map *map)
402 struct map *pos, *tmp;
404 list_for_each_entry_safe(pos, tmp, &self->maps, node) {
405 if (map__overlap(pos, map)) {
407 printf("overlapping maps:\n");
408 map__fprintf(map, stdout);
409 map__fprintf(pos, stdout);
412 if (map->start <= pos->start && map->end > pos->start)
413 pos->start = map->end;
415 if (map->end >= pos->end && map->start < pos->end)
416 pos->end = map->start;
419 printf("after collision:\n");
420 map__fprintf(pos, stdout);
423 if (pos->start >= pos->end) {
424 list_del_init(&pos->node);
430 list_add_tail(&map->node, &self->maps);
433 static int thread__fork(struct thread *self, struct thread *parent)
439 self->comm = strdup(parent->comm);
443 list_for_each_entry(map, &parent->maps, node) {
444 struct map *new = map__clone(map);
447 thread__insert_map(self, new);
453 static struct map *thread__find_map(struct thread *self, u64 ip)
460 list_for_each_entry(pos, &self->maps, node)
461 if (ip >= pos->start && ip <= pos->end)
467 static size_t threads__fprintf(FILE *fp)
472 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
473 struct thread *pos = rb_entry(nd, struct thread, rb_node);
475 ret += thread__fprintf(pos, fp);
482 * histogram, sorted on item, collects counts
485 static struct rb_root hist;
488 struct rb_node rb_node;
490 struct thread *thread;
494 struct symbol *parent;
502 * configurable sorting bits
506 struct list_head list;
510 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
511 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
512 size_t (*print)(FILE *fp, struct hist_entry *);
515 static int64_t cmp_null(void *l, void *r)
528 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
530 return right->thread->pid - left->thread->pid;
534 sort__thread_print(FILE *fp, struct hist_entry *self)
536 return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
539 static struct sort_entry sort_thread = {
540 .header = " Command: Pid",
541 .cmp = sort__thread_cmp,
542 .print = sort__thread_print,
548 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
550 return right->thread->pid - left->thread->pid;
554 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
556 char *comm_l = left->thread->comm;
557 char *comm_r = right->thread->comm;
559 if (!comm_l || !comm_r)
560 return cmp_null(comm_l, comm_r);
562 return strcmp(comm_l, comm_r);
566 sort__comm_print(FILE *fp, struct hist_entry *self)
568 return fprintf(fp, "%16s", self->thread->comm);
571 static struct sort_entry sort_comm = {
572 .header = " Command",
573 .cmp = sort__comm_cmp,
574 .collapse = sort__comm_collapse,
575 .print = sort__comm_print,
581 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
583 struct dso *dso_l = left->dso;
584 struct dso *dso_r = right->dso;
586 if (!dso_l || !dso_r)
587 return cmp_null(dso_l, dso_r);
589 return strcmp(dso_l->name, dso_r->name);
593 sort__dso_print(FILE *fp, struct hist_entry *self)
596 return fprintf(fp, "%-25s", self->dso->name);
598 return fprintf(fp, "%016llx ", (u64)self->ip);
601 static struct sort_entry sort_dso = {
602 .header = "Shared Object ",
603 .cmp = sort__dso_cmp,
604 .print = sort__dso_print,
610 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
614 if (left->sym == right->sym)
617 ip_l = left->sym ? left->sym->start : left->ip;
618 ip_r = right->sym ? right->sym->start : right->ip;
620 return (int64_t)(ip_r - ip_l);
624 sort__sym_print(FILE *fp, struct hist_entry *self)
629 ret += fprintf(fp, "%#018llx ", (u64)self->ip);
632 ret += fprintf(fp, "[%c] %s",
633 self->dso == kernel_dso ? 'k' : '.', self->sym->name);
635 ret += fprintf(fp, "%#016llx", (u64)self->ip);
641 static struct sort_entry sort_sym = {
643 .cmp = sort__sym_cmp,
644 .print = sort__sym_print,
650 sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
652 struct symbol *sym_l = left->parent;
653 struct symbol *sym_r = right->parent;
655 if (!sym_l || !sym_r)
656 return cmp_null(sym_l, sym_r);
658 return strcmp(sym_l->name, sym_r->name);
662 sort__parent_print(FILE *fp, struct hist_entry *self)
666 ret += fprintf(fp, "%-20s", self->parent ? self->parent->name : "[other]");
671 static struct sort_entry sort_parent = {
672 .header = "Parent symbol ",
673 .cmp = sort__parent_cmp,
674 .print = sort__parent_print,
677 static int sort__need_collapse = 0;
678 static int sort__has_parent = 0;
680 struct sort_dimension {
682 struct sort_entry *entry;
686 static struct sort_dimension sort_dimensions[] = {
687 { .name = "pid", .entry = &sort_thread, },
688 { .name = "comm", .entry = &sort_comm, },
689 { .name = "dso", .entry = &sort_dso, },
690 { .name = "symbol", .entry = &sort_sym, },
691 { .name = "parent", .entry = &sort_parent, },
694 static LIST_HEAD(hist_entry__sort_list);
696 static int sort_dimension__add(char *tok)
700 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
701 struct sort_dimension *sd = &sort_dimensions[i];
706 if (strncasecmp(tok, sd->name, strlen(tok)))
709 if (sd->entry->collapse)
710 sort__need_collapse = 1;
712 if (sd->entry == &sort_parent) {
713 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
717 regerror(ret, &parent_regex, err, sizeof(err));
718 fprintf(stderr, "Invalid regex: %s\n%s",
719 parent_pattern, err);
722 sort__has_parent = 1;
725 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
735 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
737 struct sort_entry *se;
740 list_for_each_entry(se, &hist_entry__sort_list, list) {
741 cmp = se->cmp(left, right);
750 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
752 struct sort_entry *se;
755 list_for_each_entry(se, &hist_entry__sort_list, list) {
756 int64_t (*f)(struct hist_entry *, struct hist_entry *);
758 f = se->collapse ?: se->cmp;
760 cmp = f(left, right);
769 hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
771 struct sort_entry *se;
774 if (exclude_other && !self->parent)
778 double percent = self->count * 100.0 / total_samples;
779 char *color = PERF_COLOR_NORMAL;
782 * We color high-overhead entries in red, mid-overhead
783 * entries in green - and keep the low overhead places
786 if (percent >= 5.0) {
787 color = PERF_COLOR_RED;
790 color = PERF_COLOR_GREEN;
793 ret = color_fprintf(fp, color, " %6.2f%%",
794 (self->count * 100.0) / total_samples);
796 ret = fprintf(fp, "%12Ld ", self->count);
798 list_for_each_entry(se, &hist_entry__sort_list, list) {
799 if (exclude_other && (se == &sort_parent))
803 ret += se->print(fp, self);
806 ret += fprintf(fp, "\n");
815 static struct symbol *
816 resolve_symbol(struct thread *thread, struct map **mapp,
817 struct dso **dsop, u64 *ipp)
819 struct dso *dso = dsop ? *dsop : NULL;
820 struct map *map = mapp ? *mapp : NULL;
832 map = thread__find_map(thread, ip);
837 ip = map->map_ip(map, ip);
842 * If this is outside of all known maps,
843 * and is a negative address, try to look it
844 * up in the kernel dso, as it might be a
845 * vsyscall (which executes in user-mode):
847 if ((long long)ip < 0)
850 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
851 dprintf(" ...... map: %Lx -> %Lx\n", *ipp, ip);
860 return dso->find_symbol(dso, ip);
863 static int call__match(struct symbol *sym)
865 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
872 * collect histogram counts
876 hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
877 struct symbol *sym, u64 ip, struct ip_callchain *chain,
878 char level, u64 count)
880 struct rb_node **p = &hist.rb_node;
881 struct rb_node *parent = NULL;
882 struct hist_entry *he;
883 struct hist_entry entry = {
895 if (sort__has_parent && chain) {
896 u64 context = PERF_CONTEXT_MAX;
899 for (i = 0; i < chain->nr; i++) {
900 u64 ip = chain->ips[i];
901 struct dso *dso = NULL;
904 if (ip >= PERF_CONTEXT_MAX) {
910 case PERF_CONTEXT_KERNEL:
917 sym = resolve_symbol(thread, NULL, &dso, &ip);
919 if (sym && call__match(sym)) {
928 he = rb_entry(parent, struct hist_entry, rb_node);
930 cmp = hist_entry__cmp(&entry, he);
943 he = malloc(sizeof(*he));
947 rb_link_node(&he->rb_node, parent, p);
948 rb_insert_color(&he->rb_node, &hist);
953 static void hist_entry__free(struct hist_entry *he)
959 * collapse the histogram
962 static struct rb_root collapse_hists;
964 static void collapse__insert_entry(struct hist_entry *he)
966 struct rb_node **p = &collapse_hists.rb_node;
967 struct rb_node *parent = NULL;
968 struct hist_entry *iter;
973 iter = rb_entry(parent, struct hist_entry, rb_node);
975 cmp = hist_entry__collapse(iter, he);
978 iter->count += he->count;
979 hist_entry__free(he);
989 rb_link_node(&he->rb_node, parent, p);
990 rb_insert_color(&he->rb_node, &collapse_hists);
993 static void collapse__resort(void)
995 struct rb_node *next;
996 struct hist_entry *n;
998 if (!sort__need_collapse)
1001 next = rb_first(&hist);
1003 n = rb_entry(next, struct hist_entry, rb_node);
1004 next = rb_next(&n->rb_node);
1006 rb_erase(&n->rb_node, &hist);
1007 collapse__insert_entry(n);
1012 * reverse the map, sort on count.
1015 static struct rb_root output_hists;
1017 static void output__insert_entry(struct hist_entry *he)
1019 struct rb_node **p = &output_hists.rb_node;
1020 struct rb_node *parent = NULL;
1021 struct hist_entry *iter;
1023 while (*p != NULL) {
1025 iter = rb_entry(parent, struct hist_entry, rb_node);
1027 if (he->count > iter->count)
1030 p = &(*p)->rb_right;
1033 rb_link_node(&he->rb_node, parent, p);
1034 rb_insert_color(&he->rb_node, &output_hists);
1037 static void output__resort(void)
1039 struct rb_node *next;
1040 struct hist_entry *n;
1041 struct rb_root *tree = &hist;
1043 if (sort__need_collapse)
1044 tree = &collapse_hists;
1046 next = rb_first(tree);
1049 n = rb_entry(next, struct hist_entry, rb_node);
1050 next = rb_next(&n->rb_node);
1052 rb_erase(&n->rb_node, tree);
1053 output__insert_entry(n);
1057 static size_t output__fprintf(FILE *fp, u64 total_samples)
1059 struct hist_entry *pos;
1060 struct sort_entry *se;
1066 fprintf(fp, "# (%Ld samples)\n", (u64)total_samples);
1069 fprintf(fp, "# Overhead");
1070 list_for_each_entry(se, &hist_entry__sort_list, list) {
1071 if (exclude_other && (se == &sort_parent))
1073 fprintf(fp, " %s", se->header);
1077 fprintf(fp, "# ........");
1078 list_for_each_entry(se, &hist_entry__sort_list, list) {
1081 if (exclude_other && (se == &sort_parent))
1085 for (i = 0; i < strlen(se->header); i++)
1092 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
1093 pos = rb_entry(nd, struct hist_entry, rb_node);
1094 ret += hist_entry__fprintf(fp, pos, total_samples);
1097 if (sort_order == default_sort_order &&
1098 parent_pattern == default_parent_pattern) {
1100 fprintf(fp, "# (For more details, try: perf report --sort comm,dso,symbol)\n");
1108 static void register_idle_thread(void)
1110 struct thread *thread = threads__findnew(0);
1112 if (thread == NULL ||
1113 thread__set_comm(thread, "[idle]")) {
1114 fprintf(stderr, "problem inserting idle task.\n");
1119 static unsigned long total = 0,
1126 static int validate_chain(struct ip_callchain *chain, event_t *event)
1128 unsigned int chain_size;
1130 chain_size = event->header.size;
1131 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
1133 if (chain->nr*sizeof(u64) > chain_size)
1140 process_sample_event(event_t *event, unsigned long offset, unsigned long head)
1144 struct dso *dso = NULL;
1145 struct thread *thread = threads__findnew(event->ip.pid);
1146 u64 ip = event->ip.ip;
1148 struct map *map = NULL;
1149 void *more_data = event->ip.__more_data;
1150 struct ip_callchain *chain = NULL;
1152 if (sample_type & PERF_SAMPLE_PERIOD) {
1153 period = *(u64 *)more_data;
1154 more_data += sizeof(u64);
1157 dprintf("%p [%p]: PERF_EVENT_SAMPLE (IP, %d): %d: %p period: %Ld\n",
1158 (void *)(offset + head),
1159 (void *)(long)(event->header.size),
1165 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
1168 chain = (void *)more_data;
1170 dprintf("... chain: nr:%Lu\n", chain->nr);
1172 if (validate_chain(chain, event) < 0) {
1173 eprintf("call-chain problem with event, skipping it.\n");
1178 for (i = 0; i < chain->nr; i++)
1179 dprintf("..... %2d: %016Lx\n", i, chain->ips[i]);
1183 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1185 if (thread == NULL) {
1186 eprintf("problem processing %d event, skipping it.\n",
1187 event->header.type);
1191 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
1197 dprintf(" ...... dso: %s\n", dso->name);
1199 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
1207 dprintf(" ...... dso: [hypervisor]\n");
1210 if (show & show_mask) {
1211 struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
1213 if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
1214 eprintf("problem incrementing symbol count, skipping event\n");
1224 process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
1226 struct thread *thread = threads__findnew(event->mmap.pid);
1227 struct map *map = map__new(&event->mmap);
1229 dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
1230 (void *)(offset + head),
1231 (void *)(long)(event->header.size),
1233 (void *)(long)event->mmap.start,
1234 (void *)(long)event->mmap.len,
1235 (void *)(long)event->mmap.pgoff,
1236 event->mmap.filename);
1238 if (thread == NULL || map == NULL) {
1239 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
1243 thread__insert_map(thread, map);
1250 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
1252 struct thread *thread = threads__findnew(event->comm.pid);
1254 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
1255 (void *)(offset + head),
1256 (void *)(long)(event->header.size),
1257 event->comm.comm, event->comm.pid);
1259 if (thread == NULL ||
1260 thread__set_comm(thread, event->comm.comm)) {
1261 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
1270 process_fork_event(event_t *event, unsigned long offset, unsigned long head)
1272 struct thread *thread = threads__findnew(event->fork.pid);
1273 struct thread *parent = threads__findnew(event->fork.ppid);
1275 dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
1276 (void *)(offset + head),
1277 (void *)(long)(event->header.size),
1278 event->fork.pid, event->fork.ppid);
1280 if (!thread || !parent || thread__fork(thread, parent)) {
1281 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
1290 process_period_event(event_t *event, unsigned long offset, unsigned long head)
1292 dprintf("%p [%p]: PERF_EVENT_PERIOD: time:%Ld, id:%Ld: period:%Ld\n",
1293 (void *)(offset + head),
1294 (void *)(long)(event->header.size),
1297 event->period.sample_period);
1303 process_lost_event(event_t *event, unsigned long offset, unsigned long head)
1305 dprintf("%p [%p]: PERF_EVENT_LOST: id:%Ld: lost:%Ld\n",
1306 (void *)(offset + head),
1307 (void *)(long)(event->header.size),
1311 total_lost += event->lost.lost;
1316 static void trace_event(event_t *event)
1318 unsigned char *raw_event = (void *)event;
1319 char *color = PERF_COLOR_BLUE;
1326 cdprintf("\n. ... raw event: size %d bytes\n", event->header.size);
1328 for (i = 0; i < event->header.size; i++) {
1329 if ((i & 15) == 0) {
1331 cdprintf(" %04x: ", i);
1334 cdprintf(" %02x", raw_event[i]);
1336 if (((i & 15) == 15) || i == event->header.size-1) {
1338 for (j = 0; j < 15-(i & 15); j++)
1340 for (j = 0; j < (i & 15); j++) {
1341 if (isprint(raw_event[i-15+j]))
1342 cdprintf("%c", raw_event[i-15+j]);
1353 process_event(event_t *event, unsigned long offset, unsigned long head)
1357 switch (event->header.type) {
1358 case PERF_EVENT_SAMPLE:
1359 return process_sample_event(event, offset, head);
1361 case PERF_EVENT_MMAP:
1362 return process_mmap_event(event, offset, head);
1364 case PERF_EVENT_COMM:
1365 return process_comm_event(event, offset, head);
1367 case PERF_EVENT_FORK:
1368 return process_fork_event(event, offset, head);
1370 case PERF_EVENT_PERIOD:
1371 return process_period_event(event, offset, head);
1373 case PERF_EVENT_LOST:
1374 return process_lost_event(event, offset, head);
1377 * We dont process them right now but they are fine:
1380 case PERF_EVENT_THROTTLE:
1381 case PERF_EVENT_UNTHROTTLE:
1391 static struct perf_header *header;
1393 static u64 perf_header__sample_type(void)
1395 u64 sample_type = 0;
1398 for (i = 0; i < header->attrs; i++) {
1399 struct perf_header_attr *attr = header->attr[i];
1402 sample_type = attr->attr.sample_type;
1403 else if (sample_type != attr->attr.sample_type)
1404 die("non matching sample_type");
1410 static int __cmd_report(void)
1412 int ret, rc = EXIT_FAILURE;
1413 unsigned long offset = 0;
1414 unsigned long head, shift;
1420 register_idle_thread();
1422 input = open(input_name, O_RDONLY);
1424 fprintf(stderr, " failed to open file: %s", input_name);
1425 if (!strcmp(input_name, "perf.data"))
1426 fprintf(stderr, " (try 'perf record' first)");
1427 fprintf(stderr, "\n");
1431 ret = fstat(input, &stat);
1433 perror("failed to stat file");
1437 if (!stat.st_size) {
1438 fprintf(stderr, "zero-sized file, nothing to do!\n");
1442 header = perf_header__read(input);
1443 head = header->data_offset;
1445 sample_type = perf_header__sample_type();
1447 if (sort__has_parent && !(sample_type & PERF_SAMPLE_CALLCHAIN)) {
1448 fprintf(stderr, "selected --sort parent, but no callchain data\n");
1452 if (load_kernel() < 0) {
1453 perror("failed to load kernel symbols");
1454 return EXIT_FAILURE;
1458 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
1459 perror("failed to get the current directory");
1460 return EXIT_FAILURE;
1462 cwdlen = strlen(cwd);
1468 shift = page_size * (head / page_size);
1473 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1474 MAP_SHARED, input, offset);
1475 if (buf == MAP_FAILED) {
1476 perror("failed to mmap file");
1481 event = (event_t *)(buf + head);
1483 size = event->header.size;
1487 if (head + event->header.size >= page_size * mmap_window) {
1490 shift = page_size * (head / page_size);
1492 ret = munmap(buf, page_size * mmap_window);
1500 size = event->header.size;
1502 dprintf("\n%p [%p]: event: %d\n",
1503 (void *)(offset + head),
1504 (void *)(long)event->header.size,
1505 event->header.type);
1507 if (!size || process_event(event, offset, head) < 0) {
1509 dprintf("%p [%p]: skipping unknown header type: %d\n",
1510 (void *)(offset + head),
1511 (void *)(long)(event->header.size),
1512 event->header.type);
1517 * assume we lost track of the stream, check alignment, and
1518 * increment a single u64 in the hope to catch on again 'soon'.
1521 if (unlikely(head & 7))
1529 if (offset + head >= header->data_offset + header->data_size)
1532 if (offset + head < stat.st_size)
1539 dprintf(" IP events: %10ld\n", total);
1540 dprintf(" mmap events: %10ld\n", total_mmap);
1541 dprintf(" comm events: %10ld\n", total_comm);
1542 dprintf(" fork events: %10ld\n", total_fork);
1543 dprintf(" lost events: %10ld\n", total_lost);
1544 dprintf(" unknown events: %10ld\n", total_unknown);
1550 threads__fprintf(stdout);
1553 dsos__fprintf(stdout);
1557 output__fprintf(stdout, total);
1562 static const char * const report_usage[] = {
1563 "perf report [<options>] <command>",
1567 static const struct option options[] = {
1568 OPT_STRING('i', "input", &input_name, "file",
1570 OPT_BOOLEAN('v', "verbose", &verbose,
1571 "be more verbose (show symbol address, etc)"),
1572 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1573 "dump raw trace in ASCII"),
1574 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
1575 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1576 "sort by key(s): pid, comm, dso, symbol, parent"),
1577 OPT_BOOLEAN('P', "full-paths", &full_paths,
1578 "Don't shorten the pathnames taking into account the cwd"),
1579 OPT_STRING('p', "parent", &parent_pattern, "regex",
1580 "regex filter to identify parent, see: '--sort parent'"),
1581 OPT_BOOLEAN('x', "exclude-other", &exclude_other,
1582 "Only display entries with parent-match"),
1586 static void setup_sorting(void)
1588 char *tmp, *tok, *str = strdup(sort_order);
1590 for (tok = strtok_r(str, ", ", &tmp);
1591 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1592 if (sort_dimension__add(tok) < 0) {
1593 error("Unknown --sort key: `%s'", tok);
1594 usage_with_options(report_usage, options);
1601 int cmd_report(int argc, const char **argv, const char *prefix)
1605 page_size = getpagesize();
1607 argc = parse_options(argc, argv, options, report_usage, 0);
1611 if (parent_pattern != default_parent_pattern)
1612 sort_dimension__add("parent");
1617 * Any (unrecognized) arguments left?
1620 usage_with_options(report_usage, options);
1624 return __cmd_report();