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 static int full_paths;
44 static unsigned long page_size;
45 static unsigned long mmap_window = 32;
47 static char *call = "^sys_";
48 static regex_t call_regex;
50 struct ip_chain_event {
59 struct perf_event_header header;
62 unsigned char __more_data[];
66 struct perf_event_header header;
71 char filename[PATH_MAX];
75 struct perf_event_header header;
81 struct perf_event_header header;
86 struct perf_event_header header;
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;
101 static LIST_HEAD(dsos);
102 static struct dso *kernel_dso;
103 static struct dso *vdso;
105 static void dsos__add(struct dso *dso)
107 list_add_tail(&dso->node, &dsos);
110 static struct dso *dsos__find(const char *name)
114 list_for_each_entry(pos, &dsos, node)
115 if (strcmp(pos->name, name) == 0)
120 static struct dso *dsos__findnew(const char *name)
122 struct dso *dso = dsos__find(name);
128 dso = dso__new(name, 0);
132 nr = dso__load(dso, NULL, verbose);
135 fprintf(stderr, "Failed to open: %s\n", name);
138 if (!nr && verbose) {
140 "No symbols found in: %s, maybe install a debug package?\n",
153 static void dsos__fprintf(FILE *fp)
157 list_for_each_entry(pos, &dsos, node)
158 dso__fprintf(pos, fp);
161 static struct symbol *vdso__find_symbol(struct dso *dso, __u64 ip)
163 return dso__find_symbol(kernel_dso, ip);
166 static int load_kernel(void)
170 kernel_dso = dso__new("[kernel]", 0);
174 err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose);
176 dso__delete(kernel_dso);
179 dsos__add(kernel_dso);
181 vdso = dso__new("[vdso]", 0);
185 vdso->find_symbol = vdso__find_symbol;
192 static char __cwd[PATH_MAX];
193 static char *cwd = __cwd;
196 static int strcommon(const char *pathname)
200 while (pathname[n] == cwd[n] && n < cwdlen)
207 struct list_head node;
211 __u64 (*map_ip)(struct map *, __u64);
215 static __u64 map__map_ip(struct map *map, __u64 ip)
217 return ip - map->start + map->pgoff;
220 static __u64 vdso__map_ip(struct map *map, __u64 ip)
225 static inline int is_anon_memory(const char *filename)
227 return strcmp(filename, "//anon") == 0;
230 static struct map *map__new(struct mmap_event *event)
232 struct map *self = malloc(sizeof(*self));
235 const char *filename = event->filename;
236 char newfilename[PATH_MAX];
240 int n = strcommon(filename);
243 snprintf(newfilename, sizeof(newfilename),
244 ".%s", filename + n);
245 filename = newfilename;
249 anon = is_anon_memory(filename);
252 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
253 filename = newfilename;
256 self->start = event->start;
257 self->end = event->start + event->len;
258 self->pgoff = event->pgoff;
260 self->dso = dsos__findnew(filename);
261 if (self->dso == NULL)
264 if (self->dso == vdso || anon)
265 self->map_ip = vdso__map_ip;
267 self->map_ip = map__map_ip;
275 static struct map *map__clone(struct map *self)
277 struct map *map = malloc(sizeof(*self));
282 memcpy(map, self, sizeof(*self));
287 static int map__overlap(struct map *l, struct map *r)
289 if (l->start > r->start) {
295 if (l->end > r->start)
301 static size_t map__fprintf(struct map *self, FILE *fp)
303 return fprintf(fp, " %Lx-%Lx %Lx %s\n",
304 self->start, self->end, self->pgoff, self->dso->name);
309 struct rb_node rb_node;
310 struct list_head maps;
315 static struct thread *thread__new(pid_t pid)
317 struct thread *self = malloc(sizeof(*self));
321 self->comm = malloc(32);
323 snprintf(self->comm, 32, ":%d", self->pid);
324 INIT_LIST_HEAD(&self->maps);
330 static int thread__set_comm(struct thread *self, const char *comm)
334 self->comm = strdup(comm);
335 return self->comm ? 0 : -ENOMEM;
338 static size_t thread__fprintf(struct thread *self, FILE *fp)
341 size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
343 list_for_each_entry(pos, &self->maps, node)
344 ret += map__fprintf(pos, fp);
350 static struct rb_root threads;
351 static struct thread *last_match;
353 static struct thread *threads__findnew(pid_t pid)
355 struct rb_node **p = &threads.rb_node;
356 struct rb_node *parent = NULL;
360 * Font-end cache - PID lookups come in blocks,
361 * so most of the time we dont have to look up
364 if (last_match && last_match->pid == pid)
369 th = rb_entry(parent, struct thread, rb_node);
371 if (th->pid == pid) {
382 th = thread__new(pid);
384 rb_link_node(&th->rb_node, parent, p);
385 rb_insert_color(&th->rb_node, &threads);
392 static void thread__insert_map(struct thread *self, struct map *map)
394 struct map *pos, *tmp;
396 list_for_each_entry_safe(pos, tmp, &self->maps, node) {
397 if (map__overlap(pos, map)) {
398 list_del_init(&pos->node);
404 list_add_tail(&map->node, &self->maps);
407 static int thread__fork(struct thread *self, struct thread *parent)
413 self->comm = strdup(parent->comm);
417 list_for_each_entry(map, &parent->maps, node) {
418 struct map *new = map__clone(map);
421 thread__insert_map(self, new);
427 static struct map *thread__find_map(struct thread *self, __u64 ip)
434 list_for_each_entry(pos, &self->maps, node)
435 if (ip >= pos->start && ip <= pos->end)
441 static size_t threads__fprintf(FILE *fp)
446 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
447 struct thread *pos = rb_entry(nd, struct thread, rb_node);
449 ret += thread__fprintf(pos, fp);
456 * histogram, sorted on item, collects counts
459 static struct rb_root hist;
462 struct rb_node rb_node;
464 struct thread *thread;
476 * configurable sorting bits
480 struct list_head list;
484 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
485 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
486 size_t (*print)(FILE *fp, struct hist_entry *);
489 static int64_t cmp_null(void *l, void *r)
502 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
504 return right->thread->pid - left->thread->pid;
508 sort__thread_print(FILE *fp, struct hist_entry *self)
510 return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
513 static struct sort_entry sort_thread = {
514 .header = " Command: Pid",
515 .cmp = sort__thread_cmp,
516 .print = sort__thread_print,
522 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
524 return right->thread->pid - left->thread->pid;
528 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
530 char *comm_l = left->thread->comm;
531 char *comm_r = right->thread->comm;
533 if (!comm_l || !comm_r)
534 return cmp_null(comm_l, comm_r);
536 return strcmp(comm_l, comm_r);
540 sort__comm_print(FILE *fp, struct hist_entry *self)
542 return fprintf(fp, "%16s", self->thread->comm);
545 static struct sort_entry sort_comm = {
546 .header = " Command",
547 .cmp = sort__comm_cmp,
548 .collapse = sort__comm_collapse,
549 .print = sort__comm_print,
555 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
557 struct dso *dso_l = left->dso;
558 struct dso *dso_r = right->dso;
560 if (!dso_l || !dso_r)
561 return cmp_null(dso_l, dso_r);
563 return strcmp(dso_l->name, dso_r->name);
567 sort__dso_print(FILE *fp, struct hist_entry *self)
570 return fprintf(fp, "%-25s", self->dso->name);
572 return fprintf(fp, "%016llx ", (__u64)self->ip);
575 static struct sort_entry sort_dso = {
576 .header = "Shared Object ",
577 .cmp = sort__dso_cmp,
578 .print = sort__dso_print,
584 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
588 if (left->sym == right->sym)
591 ip_l = left->sym ? left->sym->start : left->ip;
592 ip_r = right->sym ? right->sym->start : right->ip;
594 return (int64_t)(ip_r - ip_l);
598 sort__sym_print(FILE *fp, struct hist_entry *self)
603 ret += fprintf(fp, "%#018llx ", (__u64)self->ip);
606 ret += fprintf(fp, "[%c] %s",
607 self->dso == kernel_dso ? 'k' : '.', self->sym->name);
609 ret += fprintf(fp, "%#016llx", (__u64)self->ip);
615 static struct sort_entry sort_sym = {
617 .cmp = sort__sym_cmp,
618 .print = sort__sym_print,
624 sort__call_cmp(struct hist_entry *left, struct hist_entry *right)
626 struct symbol *sym_l = left->call;
627 struct symbol *sym_r = right->call;
629 if (!sym_l || !sym_r)
630 return cmp_null(sym_l, sym_r);
632 return strcmp(sym_l->name, sym_r->name);
636 sort__call_print(FILE *fp, struct hist_entry *self)
640 ret += fprintf(fp, "%-20s", self->call ? self->call->name : "[unmatched]");
645 static struct sort_entry sort_call = {
646 .header = "Callchain symbol ",
647 .cmp = sort__call_cmp,
648 .print = sort__call_print,
651 static int sort__need_collapse = 0;
652 static int sort__has_call = 0;
654 struct sort_dimension {
656 struct sort_entry *entry;
660 static struct sort_dimension sort_dimensions[] = {
661 { .name = "pid", .entry = &sort_thread, },
662 { .name = "comm", .entry = &sort_comm, },
663 { .name = "dso", .entry = &sort_dso, },
664 { .name = "symbol", .entry = &sort_sym, },
665 { .name = "call", .entry = &sort_call, },
668 static LIST_HEAD(hist_entry__sort_list);
670 static int sort_dimension__add(char *tok)
674 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
675 struct sort_dimension *sd = &sort_dimensions[i];
680 if (strncasecmp(tok, sd->name, strlen(tok)))
683 if (sd->entry->collapse)
684 sort__need_collapse = 1;
686 if (sd->entry == &sort_call) {
687 int ret = regcomp(&call_regex, call, REG_EXTENDED);
691 regerror(ret, &call_regex, err, sizeof(err));
692 fprintf(stderr, "Invalid regex: %s\n%s", call, err);
698 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
708 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
710 struct sort_entry *se;
713 list_for_each_entry(se, &hist_entry__sort_list, list) {
714 cmp = se->cmp(left, right);
723 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
725 struct sort_entry *se;
728 list_for_each_entry(se, &hist_entry__sort_list, list) {
729 int64_t (*f)(struct hist_entry *, struct hist_entry *);
731 f = se->collapse ?: se->cmp;
733 cmp = f(left, right);
742 hist_entry__fprintf(FILE *fp, struct hist_entry *self, __u64 total_samples)
744 struct sort_entry *se;
748 double percent = self->count * 100.0 / total_samples;
749 char *color = PERF_COLOR_NORMAL;
752 * We color high-overhead entries in red, mid-overhead
753 * entries in green - and keep the low overhead places
756 if (percent >= 5.0) {
757 color = PERF_COLOR_RED;
760 color = PERF_COLOR_GREEN;
763 ret = color_fprintf(fp, color, " %6.2f%%",
764 (self->count * 100.0) / total_samples);
766 ret = fprintf(fp, "%12Ld ", self->count);
768 list_for_each_entry(se, &hist_entry__sort_list, list) {
770 ret += se->print(fp, self);
773 ret += fprintf(fp, "\n");
782 static struct symbol *
783 resolve_symbol(struct thread *thread, struct map **mapp,
784 struct dso **dsop, __u64 *ipp)
786 struct dso *dso = dsop ? *dsop : NULL;
787 struct map *map = mapp ? *mapp : NULL;
799 map = thread__find_map(thread, ip);
804 ip = map->map_ip(map, ip);
810 * If this is outside of all known maps,
811 * and is a negative address, try to look it
812 * up in the kernel dso, as it might be a
813 * vsyscall (which executes in user-mode):
815 if ((long long)ip < 0)
818 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
826 return dso->find_symbol(dso, ip);
829 static struct symbol *call__match(struct symbol *sym)
834 if (sym->name && !regexec(&call_regex, sym->name, 0, NULL, 0))
841 * collect histogram counts
845 hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
846 struct symbol *sym, __u64 ip, struct ip_chain_event *chain,
847 char level, __u64 count)
849 struct rb_node **p = &hist.rb_node;
850 struct rb_node *parent = NULL;
851 struct hist_entry *he;
852 struct hist_entry entry = {
863 if (sort__has_call && chain) {
864 int i, nr = chain->hv;
869 for (i = 0; i < chain->kernel; i++) {
870 ip = chain->ips[nr + i];
872 sym = resolve_symbol(thread, NULL, &dso, &ip);
873 entry.call = call__match(sym);
879 for (i = 0; i < chain->user; i++) {
880 ip = chain->ips[nr + i];
881 sym = resolve_symbol(thread, NULL, NULL, &ip);
882 entry.call = call__match(sym);
892 he = rb_entry(parent, struct hist_entry, rb_node);
894 cmp = hist_entry__cmp(&entry, he);
907 he = malloc(sizeof(*he));
911 rb_link_node(&he->rb_node, parent, p);
912 rb_insert_color(&he->rb_node, &hist);
917 static void hist_entry__free(struct hist_entry *he)
923 * collapse the histogram
926 static struct rb_root collapse_hists;
928 static void collapse__insert_entry(struct hist_entry *he)
930 struct rb_node **p = &collapse_hists.rb_node;
931 struct rb_node *parent = NULL;
932 struct hist_entry *iter;
937 iter = rb_entry(parent, struct hist_entry, rb_node);
939 cmp = hist_entry__collapse(iter, he);
942 iter->count += he->count;
943 hist_entry__free(he);
953 rb_link_node(&he->rb_node, parent, p);
954 rb_insert_color(&he->rb_node, &collapse_hists);
957 static void collapse__resort(void)
959 struct rb_node *next;
960 struct hist_entry *n;
962 if (!sort__need_collapse)
965 next = rb_first(&hist);
967 n = rb_entry(next, struct hist_entry, rb_node);
968 next = rb_next(&n->rb_node);
970 rb_erase(&n->rb_node, &hist);
971 collapse__insert_entry(n);
976 * reverse the map, sort on count.
979 static struct rb_root output_hists;
981 static void output__insert_entry(struct hist_entry *he)
983 struct rb_node **p = &output_hists.rb_node;
984 struct rb_node *parent = NULL;
985 struct hist_entry *iter;
989 iter = rb_entry(parent, struct hist_entry, rb_node);
991 if (he->count > iter->count)
997 rb_link_node(&he->rb_node, parent, p);
998 rb_insert_color(&he->rb_node, &output_hists);
1001 static void output__resort(void)
1003 struct rb_node *next;
1004 struct hist_entry *n;
1005 struct rb_root *tree = &hist;
1007 if (sort__need_collapse)
1008 tree = &collapse_hists;
1010 next = rb_first(tree);
1013 n = rb_entry(next, struct hist_entry, rb_node);
1014 next = rb_next(&n->rb_node);
1016 rb_erase(&n->rb_node, tree);
1017 output__insert_entry(n);
1021 static size_t output__fprintf(FILE *fp, __u64 total_samples)
1023 struct hist_entry *pos;
1024 struct sort_entry *se;
1030 fprintf(fp, "# (%Ld samples)\n", (__u64)total_samples);
1033 fprintf(fp, "# Overhead");
1034 list_for_each_entry(se, &hist_entry__sort_list, list)
1035 fprintf(fp, " %s", se->header);
1038 fprintf(fp, "# ........");
1039 list_for_each_entry(se, &hist_entry__sort_list, list) {
1043 for (i = 0; i < strlen(se->header); i++)
1050 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
1051 pos = rb_entry(nd, struct hist_entry, rb_node);
1052 ret += hist_entry__fprintf(fp, pos, total_samples);
1055 if (!strcmp(sort_order, default_sort_order)) {
1057 fprintf(fp, "# (For more details, try: perf report --sort comm,dso,symbol)\n");
1065 static void register_idle_thread(void)
1067 struct thread *thread = threads__findnew(0);
1069 if (thread == NULL ||
1070 thread__set_comm(thread, "[idle]")) {
1071 fprintf(stderr, "problem inserting idle task.\n");
1076 static unsigned long total = 0,
1083 process_overflow_event(event_t *event, unsigned long offset, unsigned long head)
1087 struct dso *dso = NULL;
1088 struct thread *thread = threads__findnew(event->ip.pid);
1089 __u64 ip = event->ip.ip;
1091 struct map *map = NULL;
1092 void *more_data = event->ip.__more_data;
1093 struct ip_chain_event *chain = NULL;
1095 if (event->header.type & PERF_SAMPLE_PERIOD) {
1096 period = *(__u64 *)more_data;
1097 more_data += sizeof(__u64);
1100 dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p period: %Ld\n",
1101 (void *)(offset + head),
1102 (void *)(long)(event->header.size),
1108 if (event->header.type & PERF_SAMPLE_CALLCHAIN) {
1111 chain = (void *)more_data;
1114 dprintf("... chain: u:%d, k:%d, nr:%d\n",
1119 for (i = 0; i < chain->nr; i++)
1120 dprintf("..... %2d: %016Lx\n", i, chain->ips[i]);
1124 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1126 if (thread == NULL) {
1127 fprintf(stderr, "problem processing %d event, skipping it.\n",
1128 event->header.type);
1132 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
1138 dprintf(" ...... dso: %s\n", dso->name);
1140 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
1148 dprintf(" ...... dso: [hypervisor]\n");
1151 if (show & show_mask) {
1152 struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
1154 if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
1156 "problem incrementing symbol count, skipping event\n");
1166 process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
1168 struct thread *thread = threads__findnew(event->mmap.pid);
1169 struct map *map = map__new(&event->mmap);
1171 dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
1172 (void *)(offset + head),
1173 (void *)(long)(event->header.size),
1175 (void *)(long)event->mmap.start,
1176 (void *)(long)event->mmap.len,
1177 (void *)(long)event->mmap.pgoff,
1178 event->mmap.filename);
1180 if (thread == NULL || map == NULL) {
1181 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
1185 thread__insert_map(thread, map);
1192 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
1194 struct thread *thread = threads__findnew(event->comm.pid);
1196 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
1197 (void *)(offset + head),
1198 (void *)(long)(event->header.size),
1199 event->comm.comm, event->comm.pid);
1201 if (thread == NULL ||
1202 thread__set_comm(thread, event->comm.comm)) {
1203 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
1212 process_fork_event(event_t *event, unsigned long offset, unsigned long head)
1214 struct thread *thread = threads__findnew(event->fork.pid);
1215 struct thread *parent = threads__findnew(event->fork.ppid);
1217 dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
1218 (void *)(offset + head),
1219 (void *)(long)(event->header.size),
1220 event->fork.pid, event->fork.ppid);
1222 if (!thread || !parent || thread__fork(thread, parent)) {
1223 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
1232 process_period_event(event_t *event, unsigned long offset, unsigned long head)
1234 dprintf("%p [%p]: PERF_EVENT_PERIOD: time:%Ld, id:%Ld: period:%Ld\n",
1235 (void *)(offset + head),
1236 (void *)(long)(event->header.size),
1239 event->period.sample_period);
1244 static void trace_event(event_t *event)
1246 unsigned char *raw_event = (void *)event;
1247 char *color = PERF_COLOR_BLUE;
1254 cdprintf("\n. ... raw event: size %d bytes\n", event->header.size);
1256 for (i = 0; i < event->header.size; i++) {
1257 if ((i & 15) == 0) {
1259 cdprintf(" %04x: ", i);
1262 cdprintf(" %02x", raw_event[i]);
1264 if (((i & 15) == 15) || i == event->header.size-1) {
1266 for (j = 0; j < 15-(i & 15); j++)
1268 for (j = 0; j < (i & 15); j++) {
1269 if (issane(raw_event[i-15+j]))
1270 cdprintf("%c", raw_event[i-15+j]);
1281 process_event(event_t *event, unsigned long offset, unsigned long head)
1285 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW)
1286 return process_overflow_event(event, offset, head);
1288 switch (event->header.type) {
1289 case PERF_EVENT_MMAP:
1290 return process_mmap_event(event, offset, head);
1292 case PERF_EVENT_COMM:
1293 return process_comm_event(event, offset, head);
1295 case PERF_EVENT_FORK:
1296 return process_fork_event(event, offset, head);
1298 case PERF_EVENT_PERIOD:
1299 return process_period_event(event, offset, head);
1301 * We dont process them right now but they are fine:
1304 case PERF_EVENT_THROTTLE:
1305 case PERF_EVENT_UNTHROTTLE:
1315 static int __cmd_report(void)
1317 int ret, rc = EXIT_FAILURE;
1318 unsigned long offset = 0;
1319 unsigned long head = 0;
1325 register_idle_thread();
1327 input = open(input_name, O_RDONLY);
1329 fprintf(stderr, " failed to open file: %s", input_name);
1330 if (!strcmp(input_name, "perf.data"))
1331 fprintf(stderr, " (try 'perf record' first)");
1332 fprintf(stderr, "\n");
1336 ret = fstat(input, &stat);
1338 perror("failed to stat file");
1342 if (!stat.st_size) {
1343 fprintf(stderr, "zero-sized file, nothing to do!\n");
1347 if (load_kernel() < 0) {
1348 perror("failed to load kernel symbols");
1349 return EXIT_FAILURE;
1353 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
1354 perror("failed to get the current directory");
1355 return EXIT_FAILURE;
1357 cwdlen = strlen(cwd);
1363 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1364 MAP_SHARED, input, offset);
1365 if (buf == MAP_FAILED) {
1366 perror("failed to mmap file");
1371 event = (event_t *)(buf + head);
1373 size = event->header.size;
1377 if (head + event->header.size >= page_size * mmap_window) {
1378 unsigned long shift = page_size * (head / page_size);
1381 ret = munmap(buf, page_size * mmap_window);
1389 size = event->header.size;
1391 dprintf("\n%p [%p]: event: %d\n",
1392 (void *)(offset + head),
1393 (void *)(long)event->header.size,
1394 event->header.type);
1396 if (!size || process_event(event, offset, head) < 0) {
1398 dprintf("%p [%p]: skipping unknown header type: %d\n",
1399 (void *)(offset + head),
1400 (void *)(long)(event->header.size),
1401 event->header.type);
1406 * assume we lost track of the stream, check alignment, and
1407 * increment a single u64 in the hope to catch on again 'soon'.
1410 if (unlikely(head & 7))
1418 if (offset + head < stat.st_size)
1424 dprintf(" IP events: %10ld\n", total);
1425 dprintf(" mmap events: %10ld\n", total_mmap);
1426 dprintf(" comm events: %10ld\n", total_comm);
1427 dprintf(" fork events: %10ld\n", total_fork);
1428 dprintf(" unknown events: %10ld\n", total_unknown);
1434 threads__fprintf(stdout);
1437 dsos__fprintf(stdout);
1441 output__fprintf(stdout, total);
1446 static const char * const report_usage[] = {
1447 "perf report [<options>] <command>",
1451 static const struct option options[] = {
1452 OPT_STRING('i', "input", &input_name, "file",
1454 OPT_BOOLEAN('v', "verbose", &verbose,
1455 "be more verbose (show symbol address, etc)"),
1456 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1457 "dump raw trace in ASCII"),
1458 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
1459 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1460 "sort by key(s): pid, comm, dso, symbol. Default: pid,symbol"),
1461 OPT_BOOLEAN('P', "full-paths", &full_paths,
1462 "Don't shorten the pathnames taking into account the cwd"),
1463 OPT_STRING('c', "call", &call, "regex",
1464 "regex to use for --sort call"),
1468 static void setup_sorting(void)
1470 char *tmp, *tok, *str = strdup(sort_order);
1472 for (tok = strtok_r(str, ", ", &tmp);
1473 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1474 if (sort_dimension__add(tok) < 0) {
1475 error("Unknown --sort key: `%s'", tok);
1476 usage_with_options(report_usage, options);
1483 int cmd_report(int argc, const char **argv, const char *prefix)
1487 page_size = getpagesize();
1489 argc = parse_options(argc, argv, options, report_usage, 0);
1494 * Any (unrecognized) arguments left?
1497 usage_with_options(report_usage, options);
1501 return __cmd_report();