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;
87 struct perf_event_header header;
92 typedef union event_union {
93 struct perf_event_header header;
95 struct mmap_event mmap;
96 struct comm_event comm;
97 struct fork_event fork;
98 struct period_event period;
99 struct lost_event lost;
102 static LIST_HEAD(dsos);
103 static struct dso *kernel_dso;
104 static struct dso *vdso;
106 static void dsos__add(struct dso *dso)
108 list_add_tail(&dso->node, &dsos);
111 static struct dso *dsos__find(const char *name)
115 list_for_each_entry(pos, &dsos, node)
116 if (strcmp(pos->name, name) == 0)
121 static struct dso *dsos__findnew(const char *name)
123 struct dso *dso = dsos__find(name);
129 dso = dso__new(name, 0);
133 nr = dso__load(dso, NULL, verbose);
135 eprintf("Failed to open: %s\n", name);
139 eprintf("No symbols found in: %s, maybe install a debug package?\n", name);
150 static void dsos__fprintf(FILE *fp)
154 list_for_each_entry(pos, &dsos, node)
155 dso__fprintf(pos, fp);
158 static struct symbol *vdso__find_symbol(struct dso *dso, __u64 ip)
160 return dso__find_symbol(kernel_dso, ip);
163 static int load_kernel(void)
167 kernel_dso = dso__new("[kernel]", 0);
171 err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose);
173 dso__delete(kernel_dso);
176 dsos__add(kernel_dso);
178 vdso = dso__new("[vdso]", 0);
182 vdso->find_symbol = vdso__find_symbol;
189 static char __cwd[PATH_MAX];
190 static char *cwd = __cwd;
193 static int strcommon(const char *pathname)
197 while (pathname[n] == cwd[n] && n < cwdlen)
204 struct list_head node;
208 __u64 (*map_ip)(struct map *, __u64);
212 static __u64 map__map_ip(struct map *map, __u64 ip)
214 return ip - map->start + map->pgoff;
217 static __u64 vdso__map_ip(struct map *map, __u64 ip)
222 static inline int is_anon_memory(const char *filename)
224 return strcmp(filename, "//anon") == 0;
227 static struct map *map__new(struct mmap_event *event)
229 struct map *self = malloc(sizeof(*self));
232 const char *filename = event->filename;
233 char newfilename[PATH_MAX];
237 int n = strcommon(filename);
240 snprintf(newfilename, sizeof(newfilename),
241 ".%s", filename + n);
242 filename = newfilename;
246 anon = is_anon_memory(filename);
249 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
250 filename = newfilename;
253 self->start = event->start;
254 self->end = event->start + event->len;
255 self->pgoff = event->pgoff;
257 self->dso = dsos__findnew(filename);
258 if (self->dso == NULL)
261 if (self->dso == vdso || anon)
262 self->map_ip = vdso__map_ip;
264 self->map_ip = map__map_ip;
272 static struct map *map__clone(struct map *self)
274 struct map *map = malloc(sizeof(*self));
279 memcpy(map, self, sizeof(*self));
284 static int map__overlap(struct map *l, struct map *r)
286 if (l->start > r->start) {
292 if (l->end > r->start)
298 static size_t map__fprintf(struct map *self, FILE *fp)
300 return fprintf(fp, " %Lx-%Lx %Lx %s\n",
301 self->start, self->end, self->pgoff, self->dso->name);
306 struct rb_node rb_node;
307 struct list_head maps;
312 static struct thread *thread__new(pid_t pid)
314 struct thread *self = malloc(sizeof(*self));
318 self->comm = malloc(32);
320 snprintf(self->comm, 32, ":%d", self->pid);
321 INIT_LIST_HEAD(&self->maps);
327 static int thread__set_comm(struct thread *self, const char *comm)
331 self->comm = strdup(comm);
332 return self->comm ? 0 : -ENOMEM;
335 static size_t thread__fprintf(struct thread *self, FILE *fp)
338 size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
340 list_for_each_entry(pos, &self->maps, node)
341 ret += map__fprintf(pos, fp);
347 static struct rb_root threads;
348 static struct thread *last_match;
350 static struct thread *threads__findnew(pid_t pid)
352 struct rb_node **p = &threads.rb_node;
353 struct rb_node *parent = NULL;
357 * Font-end cache - PID lookups come in blocks,
358 * so most of the time we dont have to look up
361 if (last_match && last_match->pid == pid)
366 th = rb_entry(parent, struct thread, rb_node);
368 if (th->pid == pid) {
379 th = thread__new(pid);
381 rb_link_node(&th->rb_node, parent, p);
382 rb_insert_color(&th->rb_node, &threads);
389 static void thread__insert_map(struct thread *self, struct map *map)
391 struct map *pos, *tmp;
393 list_for_each_entry_safe(pos, tmp, &self->maps, node) {
394 if (map__overlap(pos, map)) {
395 list_del_init(&pos->node);
401 list_add_tail(&map->node, &self->maps);
404 static int thread__fork(struct thread *self, struct thread *parent)
410 self->comm = strdup(parent->comm);
414 list_for_each_entry(map, &parent->maps, node) {
415 struct map *new = map__clone(map);
418 thread__insert_map(self, new);
424 static struct map *thread__find_map(struct thread *self, __u64 ip)
431 list_for_each_entry(pos, &self->maps, node)
432 if (ip >= pos->start && ip <= pos->end)
438 static size_t threads__fprintf(FILE *fp)
443 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
444 struct thread *pos = rb_entry(nd, struct thread, rb_node);
446 ret += thread__fprintf(pos, fp);
453 * histogram, sorted on item, collects counts
456 static struct rb_root hist;
459 struct rb_node rb_node;
461 struct thread *thread;
465 struct symbol *parent;
473 * configurable sorting bits
477 struct list_head list;
481 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
482 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
483 size_t (*print)(FILE *fp, struct hist_entry *);
486 static int64_t cmp_null(void *l, void *r)
499 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
501 return right->thread->pid - left->thread->pid;
505 sort__thread_print(FILE *fp, struct hist_entry *self)
507 return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
510 static struct sort_entry sort_thread = {
511 .header = " Command: Pid",
512 .cmp = sort__thread_cmp,
513 .print = sort__thread_print,
519 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
521 return right->thread->pid - left->thread->pid;
525 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
527 char *comm_l = left->thread->comm;
528 char *comm_r = right->thread->comm;
530 if (!comm_l || !comm_r)
531 return cmp_null(comm_l, comm_r);
533 return strcmp(comm_l, comm_r);
537 sort__comm_print(FILE *fp, struct hist_entry *self)
539 return fprintf(fp, "%16s", self->thread->comm);
542 static struct sort_entry sort_comm = {
543 .header = " Command",
544 .cmp = sort__comm_cmp,
545 .collapse = sort__comm_collapse,
546 .print = sort__comm_print,
552 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
554 struct dso *dso_l = left->dso;
555 struct dso *dso_r = right->dso;
557 if (!dso_l || !dso_r)
558 return cmp_null(dso_l, dso_r);
560 return strcmp(dso_l->name, dso_r->name);
564 sort__dso_print(FILE *fp, struct hist_entry *self)
567 return fprintf(fp, "%-25s", self->dso->name);
569 return fprintf(fp, "%016llx ", (__u64)self->ip);
572 static struct sort_entry sort_dso = {
573 .header = "Shared Object ",
574 .cmp = sort__dso_cmp,
575 .print = sort__dso_print,
581 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
585 if (left->sym == right->sym)
588 ip_l = left->sym ? left->sym->start : left->ip;
589 ip_r = right->sym ? right->sym->start : right->ip;
591 return (int64_t)(ip_r - ip_l);
595 sort__sym_print(FILE *fp, struct hist_entry *self)
600 ret += fprintf(fp, "%#018llx ", (__u64)self->ip);
603 ret += fprintf(fp, "[%c] %s",
604 self->dso == kernel_dso ? 'k' : '.', self->sym->name);
606 ret += fprintf(fp, "%#016llx", (__u64)self->ip);
612 static struct sort_entry sort_sym = {
614 .cmp = sort__sym_cmp,
615 .print = sort__sym_print,
621 sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
623 struct symbol *sym_l = left->parent;
624 struct symbol *sym_r = right->parent;
626 if (!sym_l || !sym_r)
627 return cmp_null(sym_l, sym_r);
629 return strcmp(sym_l->name, sym_r->name);
633 sort__parent_print(FILE *fp, struct hist_entry *self)
637 ret += fprintf(fp, "%-20s", self->parent ? self->parent->name : "[other]");
642 static struct sort_entry sort_parent = {
643 .header = "Parent symbol ",
644 .cmp = sort__parent_cmp,
645 .print = sort__parent_print,
648 static int sort__need_collapse = 0;
649 static int sort__has_parent = 0;
651 struct sort_dimension {
653 struct sort_entry *entry;
657 static struct sort_dimension sort_dimensions[] = {
658 { .name = "pid", .entry = &sort_thread, },
659 { .name = "comm", .entry = &sort_comm, },
660 { .name = "dso", .entry = &sort_dso, },
661 { .name = "symbol", .entry = &sort_sym, },
662 { .name = "parent", .entry = &sort_parent, },
665 static LIST_HEAD(hist_entry__sort_list);
667 static int sort_dimension__add(char *tok)
671 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
672 struct sort_dimension *sd = &sort_dimensions[i];
677 if (strncasecmp(tok, sd->name, strlen(tok)))
680 if (sd->entry->collapse)
681 sort__need_collapse = 1;
683 if (sd->entry == &sort_parent) {
684 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
688 regerror(ret, &parent_regex, err, sizeof(err));
689 fprintf(stderr, "Invalid regex: %s\n%s",
690 parent_pattern, err);
693 sort__has_parent = 1;
696 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
706 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
708 struct sort_entry *se;
711 list_for_each_entry(se, &hist_entry__sort_list, list) {
712 cmp = se->cmp(left, right);
721 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
723 struct sort_entry *se;
726 list_for_each_entry(se, &hist_entry__sort_list, list) {
727 int64_t (*f)(struct hist_entry *, struct hist_entry *);
729 f = se->collapse ?: se->cmp;
731 cmp = f(left, right);
740 hist_entry__fprintf(FILE *fp, struct hist_entry *self, __u64 total_samples)
742 struct sort_entry *se;
746 double percent = self->count * 100.0 / total_samples;
747 char *color = PERF_COLOR_NORMAL;
750 * We color high-overhead entries in red, mid-overhead
751 * entries in green - and keep the low overhead places
754 if (percent >= 5.0) {
755 color = PERF_COLOR_RED;
758 color = PERF_COLOR_GREEN;
761 ret = color_fprintf(fp, color, " %6.2f%%",
762 (self->count * 100.0) / total_samples);
764 ret = fprintf(fp, "%12Ld ", self->count);
766 list_for_each_entry(se, &hist_entry__sort_list, list) {
768 ret += se->print(fp, self);
771 ret += fprintf(fp, "\n");
780 static struct symbol *
781 resolve_symbol(struct thread *thread, struct map **mapp,
782 struct dso **dsop, __u64 *ipp)
784 struct dso *dso = dsop ? *dsop : NULL;
785 struct map *map = mapp ? *mapp : NULL;
797 map = thread__find_map(thread, ip);
802 ip = map->map_ip(map, ip);
808 * If this is outside of all known maps,
809 * and is a negative address, try to look it
810 * up in the kernel dso, as it might be a
811 * vsyscall (which executes in user-mode):
813 if ((long long)ip < 0)
816 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
824 return dso->find_symbol(dso, ip);
827 static struct symbol *call__match(struct symbol *sym)
832 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
839 * collect histogram counts
843 hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
844 struct symbol *sym, __u64 ip, struct perf_callchain_entry *chain,
845 char level, __u64 count)
847 struct rb_node **p = &hist.rb_node;
848 struct rb_node *parent = NULL;
849 struct hist_entry *he;
850 struct hist_entry entry = {
861 if (sort__has_parent && chain) {
862 int i, nr = chain->hv;
867 for (i = 0; i < chain->kernel; i++) {
868 ip = chain->ip[nr + i];
870 sym = resolve_symbol(thread, NULL, &dso, &ip);
871 entry.parent = call__match(sym);
877 for (i = 0; i < chain->user; i++) {
878 ip = chain->ip[nr + i];
879 sym = resolve_symbol(thread, NULL, NULL, &ip);
880 entry.parent = call__match(sym);
890 he = rb_entry(parent, struct hist_entry, rb_node);
892 cmp = hist_entry__cmp(&entry, he);
905 he = malloc(sizeof(*he));
909 rb_link_node(&he->rb_node, parent, p);
910 rb_insert_color(&he->rb_node, &hist);
915 static void hist_entry__free(struct hist_entry *he)
921 * collapse the histogram
924 static struct rb_root collapse_hists;
926 static void collapse__insert_entry(struct hist_entry *he)
928 struct rb_node **p = &collapse_hists.rb_node;
929 struct rb_node *parent = NULL;
930 struct hist_entry *iter;
935 iter = rb_entry(parent, struct hist_entry, rb_node);
937 cmp = hist_entry__collapse(iter, he);
940 iter->count += he->count;
941 hist_entry__free(he);
951 rb_link_node(&he->rb_node, parent, p);
952 rb_insert_color(&he->rb_node, &collapse_hists);
955 static void collapse__resort(void)
957 struct rb_node *next;
958 struct hist_entry *n;
960 if (!sort__need_collapse)
963 next = rb_first(&hist);
965 n = rb_entry(next, struct hist_entry, rb_node);
966 next = rb_next(&n->rb_node);
968 rb_erase(&n->rb_node, &hist);
969 collapse__insert_entry(n);
974 * reverse the map, sort on count.
977 static struct rb_root output_hists;
979 static void output__insert_entry(struct hist_entry *he)
981 struct rb_node **p = &output_hists.rb_node;
982 struct rb_node *parent = NULL;
983 struct hist_entry *iter;
987 iter = rb_entry(parent, struct hist_entry, rb_node);
989 if (he->count > iter->count)
995 rb_link_node(&he->rb_node, parent, p);
996 rb_insert_color(&he->rb_node, &output_hists);
999 static void output__resort(void)
1001 struct rb_node *next;
1002 struct hist_entry *n;
1003 struct rb_root *tree = &hist;
1005 if (sort__need_collapse)
1006 tree = &collapse_hists;
1008 next = rb_first(tree);
1011 n = rb_entry(next, struct hist_entry, rb_node);
1012 next = rb_next(&n->rb_node);
1014 rb_erase(&n->rb_node, tree);
1015 output__insert_entry(n);
1019 static size_t output__fprintf(FILE *fp, __u64 total_samples)
1021 struct hist_entry *pos;
1022 struct sort_entry *se;
1028 fprintf(fp, "# (%Ld samples)\n", (__u64)total_samples);
1031 fprintf(fp, "# Overhead");
1032 list_for_each_entry(se, &hist_entry__sort_list, list)
1033 fprintf(fp, " %s", se->header);
1036 fprintf(fp, "# ........");
1037 list_for_each_entry(se, &hist_entry__sort_list, list) {
1041 for (i = 0; i < strlen(se->header); i++)
1048 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
1049 pos = rb_entry(nd, struct hist_entry, rb_node);
1050 ret += hist_entry__fprintf(fp, pos, total_samples);
1053 if (!strcmp(sort_order, default_sort_order)) {
1055 fprintf(fp, "# (For more details, try: perf report --sort comm,dso,symbol)\n");
1063 static void register_idle_thread(void)
1065 struct thread *thread = threads__findnew(0);
1067 if (thread == NULL ||
1068 thread__set_comm(thread, "[idle]")) {
1069 fprintf(stderr, "problem inserting idle task.\n");
1074 static unsigned long total = 0,
1081 static int validate_chain(struct perf_callchain_entry *chain, event_t *event)
1083 unsigned int chain_size;
1085 if (chain->nr > MAX_STACK_DEPTH)
1087 if (chain->hv > MAX_STACK_DEPTH)
1089 if (chain->kernel > MAX_STACK_DEPTH)
1091 if (chain->user > MAX_STACK_DEPTH)
1093 if (chain->hv + chain->kernel + chain->user != chain->nr)
1096 chain_size = event->header.size;
1097 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
1099 if (chain->nr*sizeof(__u64) > chain_size)
1106 process_overflow_event(event_t *event, unsigned long offset, unsigned long head)
1110 struct dso *dso = NULL;
1111 struct thread *thread = threads__findnew(event->ip.pid);
1112 __u64 ip = event->ip.ip;
1114 struct map *map = NULL;
1115 void *more_data = event->ip.__more_data;
1116 struct perf_callchain_entry *chain = NULL;
1118 if (event->header.type & PERF_SAMPLE_PERIOD) {
1119 period = *(__u64 *)more_data;
1120 more_data += sizeof(__u64);
1123 dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p period: %Ld\n",
1124 (void *)(offset + head),
1125 (void *)(long)(event->header.size),
1131 if (event->header.type & PERF_SAMPLE_CALLCHAIN) {
1134 chain = (void *)more_data;
1136 dprintf("... chain: u:%d, k:%d, nr:%d\n",
1141 if (validate_chain(chain, event) < 0) {
1142 eprintf("call-chain problem with event, skipping it.\n");
1147 for (i = 0; i < chain->nr; i++)
1148 dprintf("..... %2d: %016Lx\n", i, chain->ip[i]);
1152 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1154 if (thread == NULL) {
1155 eprintf("problem processing %d event, skipping it.\n",
1156 event->header.type);
1160 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
1166 dprintf(" ...... dso: %s\n", dso->name);
1168 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
1176 dprintf(" ...... dso: [hypervisor]\n");
1179 if (show & show_mask) {
1180 struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
1182 if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
1183 eprintf("problem incrementing symbol count, skipping event\n");
1193 process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
1195 struct thread *thread = threads__findnew(event->mmap.pid);
1196 struct map *map = map__new(&event->mmap);
1198 dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
1199 (void *)(offset + head),
1200 (void *)(long)(event->header.size),
1202 (void *)(long)event->mmap.start,
1203 (void *)(long)event->mmap.len,
1204 (void *)(long)event->mmap.pgoff,
1205 event->mmap.filename);
1207 if (thread == NULL || map == NULL) {
1208 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
1212 thread__insert_map(thread, map);
1219 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
1221 struct thread *thread = threads__findnew(event->comm.pid);
1223 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
1224 (void *)(offset + head),
1225 (void *)(long)(event->header.size),
1226 event->comm.comm, event->comm.pid);
1228 if (thread == NULL ||
1229 thread__set_comm(thread, event->comm.comm)) {
1230 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
1239 process_fork_event(event_t *event, unsigned long offset, unsigned long head)
1241 struct thread *thread = threads__findnew(event->fork.pid);
1242 struct thread *parent = threads__findnew(event->fork.ppid);
1244 dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
1245 (void *)(offset + head),
1246 (void *)(long)(event->header.size),
1247 event->fork.pid, event->fork.ppid);
1249 if (!thread || !parent || thread__fork(thread, parent)) {
1250 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
1259 process_period_event(event_t *event, unsigned long offset, unsigned long head)
1261 dprintf("%p [%p]: PERF_EVENT_PERIOD: time:%Ld, id:%Ld: period:%Ld\n",
1262 (void *)(offset + head),
1263 (void *)(long)(event->header.size),
1266 event->period.sample_period);
1272 process_lost_event(event_t *event, unsigned long offset, unsigned long head)
1274 dprintf("%p [%p]: PERF_EVENT_LOST: id:%Ld: lost:%Ld\n",
1275 (void *)(offset + head),
1276 (void *)(long)(event->header.size),
1280 total_lost += event->lost.lost;
1285 static void trace_event(event_t *event)
1287 unsigned char *raw_event = (void *)event;
1288 char *color = PERF_COLOR_BLUE;
1295 cdprintf("\n. ... raw event: size %d bytes\n", event->header.size);
1297 for (i = 0; i < event->header.size; i++) {
1298 if ((i & 15) == 0) {
1300 cdprintf(" %04x: ", i);
1303 cdprintf(" %02x", raw_event[i]);
1305 if (((i & 15) == 15) || i == event->header.size-1) {
1307 for (j = 0; j < 15-(i & 15); j++)
1309 for (j = 0; j < (i & 15); j++) {
1310 if (isprint(raw_event[i-15+j]))
1311 cdprintf("%c", raw_event[i-15+j]);
1322 process_event(event_t *event, unsigned long offset, unsigned long head)
1326 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW)
1327 return process_overflow_event(event, offset, head);
1329 switch (event->header.type) {
1330 case PERF_EVENT_MMAP:
1331 return process_mmap_event(event, offset, head);
1333 case PERF_EVENT_COMM:
1334 return process_comm_event(event, offset, head);
1336 case PERF_EVENT_FORK:
1337 return process_fork_event(event, offset, head);
1339 case PERF_EVENT_PERIOD:
1340 return process_period_event(event, offset, head);
1342 case PERF_EVENT_LOST:
1343 return process_lost_event(event, offset, head);
1346 * We dont process them right now but they are fine:
1349 case PERF_EVENT_THROTTLE:
1350 case PERF_EVENT_UNTHROTTLE:
1360 static int __cmd_report(void)
1362 int ret, rc = EXIT_FAILURE;
1363 unsigned long offset = 0;
1364 unsigned long head = 0;
1370 register_idle_thread();
1372 input = open(input_name, O_RDONLY);
1374 fprintf(stderr, " failed to open file: %s", input_name);
1375 if (!strcmp(input_name, "perf.data"))
1376 fprintf(stderr, " (try 'perf record' first)");
1377 fprintf(stderr, "\n");
1381 ret = fstat(input, &stat);
1383 perror("failed to stat file");
1387 if (!stat.st_size) {
1388 fprintf(stderr, "zero-sized file, nothing to do!\n");
1392 if (load_kernel() < 0) {
1393 perror("failed to load kernel symbols");
1394 return EXIT_FAILURE;
1398 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
1399 perror("failed to get the current directory");
1400 return EXIT_FAILURE;
1402 cwdlen = strlen(cwd);
1408 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1409 MAP_SHARED, input, offset);
1410 if (buf == MAP_FAILED) {
1411 perror("failed to mmap file");
1416 event = (event_t *)(buf + head);
1418 size = event->header.size;
1422 if (head + event->header.size >= page_size * mmap_window) {
1423 unsigned long shift = page_size * (head / page_size);
1426 ret = munmap(buf, page_size * mmap_window);
1434 size = event->header.size;
1436 dprintf("\n%p [%p]: event: %d\n",
1437 (void *)(offset + head),
1438 (void *)(long)event->header.size,
1439 event->header.type);
1441 if (!size || process_event(event, offset, head) < 0) {
1443 dprintf("%p [%p]: skipping unknown header type: %d\n",
1444 (void *)(offset + head),
1445 (void *)(long)(event->header.size),
1446 event->header.type);
1451 * assume we lost track of the stream, check alignment, and
1452 * increment a single u64 in the hope to catch on again 'soon'.
1455 if (unlikely(head & 7))
1463 if (offset + head < stat.st_size)
1469 dprintf(" IP events: %10ld\n", total);
1470 dprintf(" mmap events: %10ld\n", total_mmap);
1471 dprintf(" comm events: %10ld\n", total_comm);
1472 dprintf(" fork events: %10ld\n", total_fork);
1473 dprintf(" lost events: %10ld\n", total_lost);
1474 dprintf(" unknown events: %10ld\n", total_unknown);
1480 threads__fprintf(stdout);
1483 dsos__fprintf(stdout);
1487 output__fprintf(stdout, total);
1492 static const char * const report_usage[] = {
1493 "perf report [<options>] <command>",
1497 static const struct option options[] = {
1498 OPT_STRING('i', "input", &input_name, "file",
1500 OPT_BOOLEAN('v', "verbose", &verbose,
1501 "be more verbose (show symbol address, etc)"),
1502 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1503 "dump raw trace in ASCII"),
1504 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
1505 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1506 "sort by key(s): pid, comm, dso, symbol, parent"),
1507 OPT_BOOLEAN('P', "full-paths", &full_paths,
1508 "Don't shorten the pathnames taking into account the cwd"),
1509 OPT_STRING('p', "parent", &parent_pattern, "regex",
1510 "regex filter to identify parent, see: '--sort parent'"),
1514 static void setup_sorting(void)
1516 char *tmp, *tok, *str = strdup(sort_order);
1518 for (tok = strtok_r(str, ", ", &tmp);
1519 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1520 if (sort_dimension__add(tok) < 0) {
1521 error("Unknown --sort key: `%s'", tok);
1522 usage_with_options(report_usage, options);
1529 int cmd_report(int argc, const char **argv, const char *prefix)
1533 page_size = getpagesize();
1535 argc = parse_options(argc, argv, options, report_usage, 0);
1540 * Any (unrecognized) arguments left?
1543 usage_with_options(report_usage, options);
1547 return __cmd_report();