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"
18 #include "util/callchain.h"
21 #include "util/header.h"
23 #include "util/parse-options.h"
24 #include "util/parse-events.h"
30 static char const *input_name = "perf.data";
31 static char *vmlinux = NULL;
33 static char default_sort_order[] = "comm,dso";
34 static char *sort_order = default_sort_order;
37 static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
39 static int dump_trace = 0;
40 #define dprintf(x...) do { if (dump_trace) printf(x); } while (0)
41 #define cdprintf(x...) do { if (dump_trace) color_fprintf(stdout, color, x); } while (0)
44 #define eprintf(x...) do { if (verbose) fprintf(stderr, x); } while (0)
46 static int full_paths;
48 static unsigned long page_size;
49 static unsigned long mmap_window = 32;
51 static char default_parent_pattern[] = "^sys_|^do_page_fault";
52 static char *parent_pattern = default_parent_pattern;
53 static regex_t parent_regex;
55 static int exclude_other = 1;
58 static u64 sample_type;
61 struct perf_event_header header;
64 unsigned char __more_data[];
68 struct perf_event_header header;
73 char filename[PATH_MAX];
77 struct perf_event_header header;
83 struct perf_event_header header;
88 struct perf_event_header header;
95 struct perf_event_header header;
101 struct perf_event_header header;
107 typedef union event_union {
108 struct perf_event_header header;
110 struct mmap_event mmap;
111 struct comm_event comm;
112 struct fork_event fork;
113 struct period_event period;
114 struct lost_event lost;
115 struct read_event read;
118 static LIST_HEAD(dsos);
119 static struct dso *kernel_dso;
120 static struct dso *vdso;
122 static void dsos__add(struct dso *dso)
124 list_add_tail(&dso->node, &dsos);
127 static struct dso *dsos__find(const char *name)
131 list_for_each_entry(pos, &dsos, node)
132 if (strcmp(pos->name, name) == 0)
137 static struct dso *dsos__findnew(const char *name)
139 struct dso *dso = dsos__find(name);
145 dso = dso__new(name, 0);
149 nr = dso__load(dso, NULL, verbose);
151 eprintf("Failed to open: %s\n", name);
155 eprintf("No symbols found in: %s, maybe install a debug package?\n", name);
166 static void dsos__fprintf(FILE *fp)
170 list_for_each_entry(pos, &dsos, node)
171 dso__fprintf(pos, fp);
174 static struct symbol *vdso__find_symbol(struct dso *dso, u64 ip)
176 return dso__find_symbol(kernel_dso, ip);
179 static int load_kernel(void)
183 kernel_dso = dso__new("[kernel]", 0);
187 err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose);
189 dso__delete(kernel_dso);
192 dsos__add(kernel_dso);
194 vdso = dso__new("[vdso]", 0);
198 vdso->find_symbol = vdso__find_symbol;
205 static char __cwd[PATH_MAX];
206 static char *cwd = __cwd;
209 static int strcommon(const char *pathname)
213 while (pathname[n] == cwd[n] && n < cwdlen)
220 struct list_head node;
224 u64 (*map_ip)(struct map *, u64);
228 static u64 map__map_ip(struct map *map, u64 ip)
230 return ip - map->start + map->pgoff;
233 static u64 vdso__map_ip(struct map *map, u64 ip)
238 static inline int is_anon_memory(const char *filename)
240 return strcmp(filename, "//anon") == 0;
243 static struct map *map__new(struct mmap_event *event)
245 struct map *self = malloc(sizeof(*self));
248 const char *filename = event->filename;
249 char newfilename[PATH_MAX];
253 int n = strcommon(filename);
256 snprintf(newfilename, sizeof(newfilename),
257 ".%s", filename + n);
258 filename = newfilename;
262 anon = is_anon_memory(filename);
265 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
266 filename = newfilename;
269 self->start = event->start;
270 self->end = event->start + event->len;
271 self->pgoff = event->pgoff;
273 self->dso = dsos__findnew(filename);
274 if (self->dso == NULL)
277 if (self->dso == vdso || anon)
278 self->map_ip = vdso__map_ip;
280 self->map_ip = map__map_ip;
288 static struct map *map__clone(struct map *self)
290 struct map *map = malloc(sizeof(*self));
295 memcpy(map, self, sizeof(*self));
300 static int map__overlap(struct map *l, struct map *r)
302 if (l->start > r->start) {
308 if (l->end > r->start)
314 static size_t map__fprintf(struct map *self, FILE *fp)
316 return fprintf(fp, " %Lx-%Lx %Lx %s\n",
317 self->start, self->end, self->pgoff, self->dso->name);
322 struct rb_node rb_node;
323 struct list_head maps;
328 static struct thread *thread__new(pid_t pid)
330 struct thread *self = malloc(sizeof(*self));
334 self->comm = malloc(32);
336 snprintf(self->comm, 32, ":%d", self->pid);
337 INIT_LIST_HEAD(&self->maps);
343 static int thread__set_comm(struct thread *self, const char *comm)
347 self->comm = strdup(comm);
348 return self->comm ? 0 : -ENOMEM;
351 static size_t thread__fprintf(struct thread *self, FILE *fp)
354 size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
356 list_for_each_entry(pos, &self->maps, node)
357 ret += map__fprintf(pos, fp);
363 static struct rb_root threads;
364 static struct thread *last_match;
366 static struct thread *threads__findnew(pid_t pid)
368 struct rb_node **p = &threads.rb_node;
369 struct rb_node *parent = NULL;
373 * Font-end cache - PID lookups come in blocks,
374 * so most of the time we dont have to look up
377 if (last_match && last_match->pid == pid)
382 th = rb_entry(parent, struct thread, rb_node);
384 if (th->pid == pid) {
395 th = thread__new(pid);
397 rb_link_node(&th->rb_node, parent, p);
398 rb_insert_color(&th->rb_node, &threads);
405 static void thread__insert_map(struct thread *self, struct map *map)
407 struct map *pos, *tmp;
409 list_for_each_entry_safe(pos, tmp, &self->maps, node) {
410 if (map__overlap(pos, map)) {
412 printf("overlapping maps:\n");
413 map__fprintf(map, stdout);
414 map__fprintf(pos, stdout);
417 if (map->start <= pos->start && map->end > pos->start)
418 pos->start = map->end;
420 if (map->end >= pos->end && map->start < pos->end)
421 pos->end = map->start;
424 printf("after collision:\n");
425 map__fprintf(pos, stdout);
428 if (pos->start >= pos->end) {
429 list_del_init(&pos->node);
435 list_add_tail(&map->node, &self->maps);
438 static int thread__fork(struct thread *self, struct thread *parent)
444 self->comm = strdup(parent->comm);
448 list_for_each_entry(map, &parent->maps, node) {
449 struct map *new = map__clone(map);
452 thread__insert_map(self, new);
458 static struct map *thread__find_map(struct thread *self, u64 ip)
465 list_for_each_entry(pos, &self->maps, node)
466 if (ip >= pos->start && ip <= pos->end)
472 static size_t threads__fprintf(FILE *fp)
477 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
478 struct thread *pos = rb_entry(nd, struct thread, rb_node);
480 ret += thread__fprintf(pos, fp);
487 * histogram, sorted on item, collects counts
490 static struct rb_root hist;
493 struct rb_node rb_node;
495 struct thread *thread;
499 struct symbol *parent;
502 struct callchain_node callchain;
503 struct rb_root sorted_chain;
509 * configurable sorting bits
513 struct list_head list;
517 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
518 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
519 size_t (*print)(FILE *fp, struct hist_entry *);
522 static int64_t cmp_null(void *l, void *r)
535 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
537 return right->thread->pid - left->thread->pid;
541 sort__thread_print(FILE *fp, struct hist_entry *self)
543 return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
546 static struct sort_entry sort_thread = {
547 .header = " Command: Pid",
548 .cmp = sort__thread_cmp,
549 .print = sort__thread_print,
555 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
557 return right->thread->pid - left->thread->pid;
561 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
563 char *comm_l = left->thread->comm;
564 char *comm_r = right->thread->comm;
566 if (!comm_l || !comm_r)
567 return cmp_null(comm_l, comm_r);
569 return strcmp(comm_l, comm_r);
573 sort__comm_print(FILE *fp, struct hist_entry *self)
575 return fprintf(fp, "%16s", self->thread->comm);
578 static struct sort_entry sort_comm = {
579 .header = " Command",
580 .cmp = sort__comm_cmp,
581 .collapse = sort__comm_collapse,
582 .print = sort__comm_print,
588 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
590 struct dso *dso_l = left->dso;
591 struct dso *dso_r = right->dso;
593 if (!dso_l || !dso_r)
594 return cmp_null(dso_l, dso_r);
596 return strcmp(dso_l->name, dso_r->name);
600 sort__dso_print(FILE *fp, struct hist_entry *self)
603 return fprintf(fp, "%-25s", self->dso->name);
605 return fprintf(fp, "%016llx ", (u64)self->ip);
608 static struct sort_entry sort_dso = {
609 .header = "Shared Object ",
610 .cmp = sort__dso_cmp,
611 .print = sort__dso_print,
617 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
621 if (left->sym == right->sym)
624 ip_l = left->sym ? left->sym->start : left->ip;
625 ip_r = right->sym ? right->sym->start : right->ip;
627 return (int64_t)(ip_r - ip_l);
631 sort__sym_print(FILE *fp, struct hist_entry *self)
636 ret += fprintf(fp, "%#018llx ", (u64)self->ip);
639 ret += fprintf(fp, "[%c] %s",
640 self->dso == kernel_dso ? 'k' : '.', self->sym->name);
642 ret += fprintf(fp, "%#016llx", (u64)self->ip);
648 static struct sort_entry sort_sym = {
650 .cmp = sort__sym_cmp,
651 .print = sort__sym_print,
657 sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
659 struct symbol *sym_l = left->parent;
660 struct symbol *sym_r = right->parent;
662 if (!sym_l || !sym_r)
663 return cmp_null(sym_l, sym_r);
665 return strcmp(sym_l->name, sym_r->name);
669 sort__parent_print(FILE *fp, struct hist_entry *self)
673 ret += fprintf(fp, "%-20s", self->parent ? self->parent->name : "[other]");
678 static struct sort_entry sort_parent = {
679 .header = "Parent symbol ",
680 .cmp = sort__parent_cmp,
681 .print = sort__parent_print,
684 static int sort__need_collapse = 0;
685 static int sort__has_parent = 0;
687 struct sort_dimension {
689 struct sort_entry *entry;
693 static struct sort_dimension sort_dimensions[] = {
694 { .name = "pid", .entry = &sort_thread, },
695 { .name = "comm", .entry = &sort_comm, },
696 { .name = "dso", .entry = &sort_dso, },
697 { .name = "symbol", .entry = &sort_sym, },
698 { .name = "parent", .entry = &sort_parent, },
701 static LIST_HEAD(hist_entry__sort_list);
703 static int sort_dimension__add(char *tok)
707 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
708 struct sort_dimension *sd = &sort_dimensions[i];
713 if (strncasecmp(tok, sd->name, strlen(tok)))
716 if (sd->entry->collapse)
717 sort__need_collapse = 1;
719 if (sd->entry == &sort_parent) {
720 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
724 regerror(ret, &parent_regex, err, sizeof(err));
725 fprintf(stderr, "Invalid regex: %s\n%s",
726 parent_pattern, err);
729 sort__has_parent = 1;
732 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
742 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
744 struct sort_entry *se;
747 list_for_each_entry(se, &hist_entry__sort_list, list) {
748 cmp = se->cmp(left, right);
757 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
759 struct sort_entry *se;
762 list_for_each_entry(se, &hist_entry__sort_list, list) {
763 int64_t (*f)(struct hist_entry *, struct hist_entry *);
765 f = se->collapse ?: se->cmp;
767 cmp = f(left, right);
776 callchain__fprintf(FILE *fp, struct callchain_node *self, u64 total_samples)
778 struct callchain_list *chain;
784 ret += callchain__fprintf(fp, self->parent, total_samples);
787 list_for_each_entry(chain, &self->val, list)
788 ret += fprintf(fp, " %p\n", (void *)chain->ip);
794 hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
797 struct rb_node *rb_node;
798 struct callchain_node *chain;
801 rb_node = rb_first(&self->sorted_chain);
805 chain = rb_entry(rb_node, struct callchain_node, rb_node);
806 percent = chain->hit * 100.0 / total_samples;
807 ret += fprintf(fp, " %6.2f%%\n", percent);
808 ret += callchain__fprintf(fp, chain, total_samples);
809 ret += fprintf(fp, "\n");
810 rb_node = rb_next(rb_node);
818 hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
820 struct sort_entry *se;
823 if (exclude_other && !self->parent)
827 double percent = self->count * 100.0 / total_samples;
828 char *color = PERF_COLOR_NORMAL;
831 * We color high-overhead entries in red, mid-overhead
832 * entries in green - and keep the low overhead places
835 if (percent >= 5.0) {
836 color = PERF_COLOR_RED;
839 color = PERF_COLOR_GREEN;
842 ret = color_fprintf(fp, color, " %6.2f%%",
843 (self->count * 100.0) / total_samples);
845 ret = fprintf(fp, "%12Ld ", self->count);
847 list_for_each_entry(se, &hist_entry__sort_list, list) {
848 if (exclude_other && (se == &sort_parent))
852 ret += se->print(fp, self);
855 ret += fprintf(fp, "\n");
858 hist_entry_callchain__fprintf(fp, self, total_samples);
867 static struct symbol *
868 resolve_symbol(struct thread *thread, struct map **mapp,
869 struct dso **dsop, u64 *ipp)
871 struct dso *dso = dsop ? *dsop : NULL;
872 struct map *map = mapp ? *mapp : NULL;
884 map = thread__find_map(thread, ip);
889 ip = map->map_ip(map, ip);
894 * If this is outside of all known maps,
895 * and is a negative address, try to look it
896 * up in the kernel dso, as it might be a
897 * vsyscall (which executes in user-mode):
899 if ((long long)ip < 0)
902 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
903 dprintf(" ...... map: %Lx -> %Lx\n", *ipp, ip);
912 return dso->find_symbol(dso, ip);
915 static int call__match(struct symbol *sym)
917 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
924 * collect histogram counts
928 hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
929 struct symbol *sym, u64 ip, struct ip_callchain *chain,
930 char level, u64 count)
932 struct rb_node **p = &hist.rb_node;
933 struct rb_node *parent = NULL;
934 struct hist_entry *he;
935 struct hist_entry entry = {
944 .sorted_chain = RB_ROOT
948 if (sort__has_parent && chain) {
949 u64 context = PERF_CONTEXT_MAX;
952 for (i = 0; i < chain->nr; i++) {
953 u64 ip = chain->ips[i];
954 struct dso *dso = NULL;
957 if (ip >= PERF_CONTEXT_MAX) {
963 case PERF_CONTEXT_KERNEL:
970 sym = resolve_symbol(thread, NULL, &dso, &ip);
972 if (sym && call__match(sym)) {
981 he = rb_entry(parent, struct hist_entry, rb_node);
983 cmp = hist_entry__cmp(&entry, he);
988 append_chain(&he->callchain, chain);
998 he = malloc(sizeof(*he));
1003 callchain_init(&he->callchain);
1004 append_chain(&he->callchain, chain);
1006 rb_link_node(&he->rb_node, parent, p);
1007 rb_insert_color(&he->rb_node, &hist);
1012 static void hist_entry__free(struct hist_entry *he)
1018 * collapse the histogram
1021 static struct rb_root collapse_hists;
1023 static void collapse__insert_entry(struct hist_entry *he)
1025 struct rb_node **p = &collapse_hists.rb_node;
1026 struct rb_node *parent = NULL;
1027 struct hist_entry *iter;
1030 while (*p != NULL) {
1032 iter = rb_entry(parent, struct hist_entry, rb_node);
1034 cmp = hist_entry__collapse(iter, he);
1037 iter->count += he->count;
1038 hist_entry__free(he);
1045 p = &(*p)->rb_right;
1048 rb_link_node(&he->rb_node, parent, p);
1049 rb_insert_color(&he->rb_node, &collapse_hists);
1052 static void collapse__resort(void)
1054 struct rb_node *next;
1055 struct hist_entry *n;
1057 if (!sort__need_collapse)
1060 next = rb_first(&hist);
1062 n = rb_entry(next, struct hist_entry, rb_node);
1063 next = rb_next(&n->rb_node);
1065 rb_erase(&n->rb_node, &hist);
1066 collapse__insert_entry(n);
1071 * reverse the map, sort on count.
1074 static struct rb_root output_hists;
1076 static void output__insert_entry(struct hist_entry *he)
1078 struct rb_node **p = &output_hists.rb_node;
1079 struct rb_node *parent = NULL;
1080 struct hist_entry *iter;
1083 sort_chain_to_rbtree(&he->sorted_chain, &he->callchain);
1085 while (*p != NULL) {
1087 iter = rb_entry(parent, struct hist_entry, rb_node);
1089 if (he->count > iter->count)
1092 p = &(*p)->rb_right;
1095 rb_link_node(&he->rb_node, parent, p);
1096 rb_insert_color(&he->rb_node, &output_hists);
1099 static void output__resort(void)
1101 struct rb_node *next;
1102 struct hist_entry *n;
1103 struct rb_root *tree = &hist;
1105 if (sort__need_collapse)
1106 tree = &collapse_hists;
1108 next = rb_first(tree);
1111 n = rb_entry(next, struct hist_entry, rb_node);
1112 next = rb_next(&n->rb_node);
1114 rb_erase(&n->rb_node, tree);
1115 output__insert_entry(n);
1119 static size_t output__fprintf(FILE *fp, u64 total_samples)
1121 struct hist_entry *pos;
1122 struct sort_entry *se;
1128 fprintf(fp, "# (%Ld samples)\n", (u64)total_samples);
1131 fprintf(fp, "# Overhead");
1132 list_for_each_entry(se, &hist_entry__sort_list, list) {
1133 if (exclude_other && (se == &sort_parent))
1135 fprintf(fp, " %s", se->header);
1139 fprintf(fp, "# ........");
1140 list_for_each_entry(se, &hist_entry__sort_list, list) {
1143 if (exclude_other && (se == &sort_parent))
1147 for (i = 0; i < strlen(se->header); i++)
1154 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
1155 pos = rb_entry(nd, struct hist_entry, rb_node);
1156 ret += hist_entry__fprintf(fp, pos, total_samples);
1159 if (sort_order == default_sort_order &&
1160 parent_pattern == default_parent_pattern) {
1162 fprintf(fp, "# (For more details, try: perf report --sort comm,dso,symbol)\n");
1170 static void register_idle_thread(void)
1172 struct thread *thread = threads__findnew(0);
1174 if (thread == NULL ||
1175 thread__set_comm(thread, "[idle]")) {
1176 fprintf(stderr, "problem inserting idle task.\n");
1181 static unsigned long total = 0,
1188 static int validate_chain(struct ip_callchain *chain, event_t *event)
1190 unsigned int chain_size;
1192 chain_size = event->header.size;
1193 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
1195 if (chain->nr*sizeof(u64) > chain_size)
1202 process_sample_event(event_t *event, unsigned long offset, unsigned long head)
1206 struct dso *dso = NULL;
1207 struct thread *thread = threads__findnew(event->ip.pid);
1208 u64 ip = event->ip.ip;
1210 struct map *map = NULL;
1211 void *more_data = event->ip.__more_data;
1212 struct ip_callchain *chain = NULL;
1214 if (sample_type & PERF_SAMPLE_PERIOD) {
1215 period = *(u64 *)more_data;
1216 more_data += sizeof(u64);
1219 dprintf("%p [%p]: PERF_EVENT_SAMPLE (IP, %d): %d: %p period: %Ld\n",
1220 (void *)(offset + head),
1221 (void *)(long)(event->header.size),
1227 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
1230 chain = (void *)more_data;
1232 dprintf("... chain: nr:%Lu\n", chain->nr);
1234 if (validate_chain(chain, event) < 0) {
1235 eprintf("call-chain problem with event, skipping it.\n");
1240 for (i = 0; i < chain->nr; i++)
1241 dprintf("..... %2d: %016Lx\n", i, chain->ips[i]);
1245 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1247 if (thread == NULL) {
1248 eprintf("problem processing %d event, skipping it.\n",
1249 event->header.type);
1253 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
1259 dprintf(" ...... dso: %s\n", dso->name);
1261 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
1269 dprintf(" ...... dso: [hypervisor]\n");
1272 if (show & show_mask) {
1273 struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
1275 if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
1276 eprintf("problem incrementing symbol count, skipping event\n");
1286 process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
1288 struct thread *thread = threads__findnew(event->mmap.pid);
1289 struct map *map = map__new(&event->mmap);
1291 dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
1292 (void *)(offset + head),
1293 (void *)(long)(event->header.size),
1295 (void *)(long)event->mmap.start,
1296 (void *)(long)event->mmap.len,
1297 (void *)(long)event->mmap.pgoff,
1298 event->mmap.filename);
1300 if (thread == NULL || map == NULL) {
1301 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
1305 thread__insert_map(thread, map);
1312 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
1314 struct thread *thread = threads__findnew(event->comm.pid);
1316 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
1317 (void *)(offset + head),
1318 (void *)(long)(event->header.size),
1319 event->comm.comm, event->comm.pid);
1321 if (thread == NULL ||
1322 thread__set_comm(thread, event->comm.comm)) {
1323 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
1332 process_fork_event(event_t *event, unsigned long offset, unsigned long head)
1334 struct thread *thread = threads__findnew(event->fork.pid);
1335 struct thread *parent = threads__findnew(event->fork.ppid);
1337 dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
1338 (void *)(offset + head),
1339 (void *)(long)(event->header.size),
1340 event->fork.pid, event->fork.ppid);
1342 if (!thread || !parent || thread__fork(thread, parent)) {
1343 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
1352 process_period_event(event_t *event, unsigned long offset, unsigned long head)
1354 dprintf("%p [%p]: PERF_EVENT_PERIOD: time:%Ld, id:%Ld: period:%Ld\n",
1355 (void *)(offset + head),
1356 (void *)(long)(event->header.size),
1359 event->period.sample_period);
1365 process_lost_event(event_t *event, unsigned long offset, unsigned long head)
1367 dprintf("%p [%p]: PERF_EVENT_LOST: id:%Ld: lost:%Ld\n",
1368 (void *)(offset + head),
1369 (void *)(long)(event->header.size),
1373 total_lost += event->lost.lost;
1378 static void trace_event(event_t *event)
1380 unsigned char *raw_event = (void *)event;
1381 char *color = PERF_COLOR_BLUE;
1388 cdprintf("\n. ... raw event: size %d bytes\n", event->header.size);
1390 for (i = 0; i < event->header.size; i++) {
1391 if ((i & 15) == 0) {
1393 cdprintf(" %04x: ", i);
1396 cdprintf(" %02x", raw_event[i]);
1398 if (((i & 15) == 15) || i == event->header.size-1) {
1400 for (j = 0; j < 15-(i & 15); j++)
1402 for (j = 0; j < (i & 15); j++) {
1403 if (isprint(raw_event[i-15+j]))
1404 cdprintf("%c", raw_event[i-15+j]);
1415 process_read_event(event_t *event, unsigned long offset, unsigned long head)
1417 dprintf("%p [%p]: PERF_EVENT_READ: %d %d %Lu\n",
1418 (void *)(offset + head),
1419 (void *)(long)(event->header.size),
1428 process_event(event_t *event, unsigned long offset, unsigned long head)
1432 switch (event->header.type) {
1433 case PERF_EVENT_SAMPLE:
1434 return process_sample_event(event, offset, head);
1436 case PERF_EVENT_MMAP:
1437 return process_mmap_event(event, offset, head);
1439 case PERF_EVENT_COMM:
1440 return process_comm_event(event, offset, head);
1442 case PERF_EVENT_FORK:
1443 return process_fork_event(event, offset, head);
1445 case PERF_EVENT_PERIOD:
1446 return process_period_event(event, offset, head);
1448 case PERF_EVENT_LOST:
1449 return process_lost_event(event, offset, head);
1451 case PERF_EVENT_READ:
1452 return process_read_event(event, offset, head);
1455 * We dont process them right now but they are fine:
1458 case PERF_EVENT_THROTTLE:
1459 case PERF_EVENT_UNTHROTTLE:
1469 static struct perf_header *header;
1471 static u64 perf_header__sample_type(void)
1473 u64 sample_type = 0;
1476 for (i = 0; i < header->attrs; i++) {
1477 struct perf_header_attr *attr = header->attr[i];
1480 sample_type = attr->attr.sample_type;
1481 else if (sample_type != attr->attr.sample_type)
1482 die("non matching sample_type");
1488 static int __cmd_report(void)
1490 int ret, rc = EXIT_FAILURE;
1491 unsigned long offset = 0;
1492 unsigned long head, shift;
1498 register_idle_thread();
1500 input = open(input_name, O_RDONLY);
1502 fprintf(stderr, " failed to open file: %s", input_name);
1503 if (!strcmp(input_name, "perf.data"))
1504 fprintf(stderr, " (try 'perf record' first)");
1505 fprintf(stderr, "\n");
1509 ret = fstat(input, &stat);
1511 perror("failed to stat file");
1515 if (!stat.st_size) {
1516 fprintf(stderr, "zero-sized file, nothing to do!\n");
1520 header = perf_header__read(input);
1521 head = header->data_offset;
1523 sample_type = perf_header__sample_type();
1525 if (sort__has_parent && !(sample_type & PERF_SAMPLE_CALLCHAIN)) {
1526 fprintf(stderr, "selected --sort parent, but no callchain data\n");
1530 if (load_kernel() < 0) {
1531 perror("failed to load kernel symbols");
1532 return EXIT_FAILURE;
1536 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
1537 perror("failed to get the current directory");
1538 return EXIT_FAILURE;
1540 cwdlen = strlen(cwd);
1546 shift = page_size * (head / page_size);
1551 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1552 MAP_SHARED, input, offset);
1553 if (buf == MAP_FAILED) {
1554 perror("failed to mmap file");
1559 event = (event_t *)(buf + head);
1561 size = event->header.size;
1565 if (head + event->header.size >= page_size * mmap_window) {
1568 shift = page_size * (head / page_size);
1570 ret = munmap(buf, page_size * mmap_window);
1578 size = event->header.size;
1580 dprintf("\n%p [%p]: event: %d\n",
1581 (void *)(offset + head),
1582 (void *)(long)event->header.size,
1583 event->header.type);
1585 if (!size || process_event(event, offset, head) < 0) {
1587 dprintf("%p [%p]: skipping unknown header type: %d\n",
1588 (void *)(offset + head),
1589 (void *)(long)(event->header.size),
1590 event->header.type);
1595 * assume we lost track of the stream, check alignment, and
1596 * increment a single u64 in the hope to catch on again 'soon'.
1599 if (unlikely(head & 7))
1607 if (offset + head >= header->data_offset + header->data_size)
1610 if (offset + head < stat.st_size)
1617 dprintf(" IP events: %10ld\n", total);
1618 dprintf(" mmap events: %10ld\n", total_mmap);
1619 dprintf(" comm events: %10ld\n", total_comm);
1620 dprintf(" fork events: %10ld\n", total_fork);
1621 dprintf(" lost events: %10ld\n", total_lost);
1622 dprintf(" unknown events: %10ld\n", total_unknown);
1628 threads__fprintf(stdout);
1631 dsos__fprintf(stdout);
1635 output__fprintf(stdout, total);
1640 static const char * const report_usage[] = {
1641 "perf report [<options>] <command>",
1645 static const struct option options[] = {
1646 OPT_STRING('i', "input", &input_name, "file",
1648 OPT_BOOLEAN('v', "verbose", &verbose,
1649 "be more verbose (show symbol address, etc)"),
1650 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1651 "dump raw trace in ASCII"),
1652 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
1653 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1654 "sort by key(s): pid, comm, dso, symbol, parent"),
1655 OPT_BOOLEAN('P', "full-paths", &full_paths,
1656 "Don't shorten the pathnames taking into account the cwd"),
1657 OPT_STRING('p', "parent", &parent_pattern, "regex",
1658 "regex filter to identify parent, see: '--sort parent'"),
1659 OPT_BOOLEAN('x', "exclude-other", &exclude_other,
1660 "Only display entries with parent-match"),
1661 OPT_BOOLEAN('c', "callchain", &callchain, "Display callchains"),
1665 static void setup_sorting(void)
1667 char *tmp, *tok, *str = strdup(sort_order);
1669 for (tok = strtok_r(str, ", ", &tmp);
1670 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1671 if (sort_dimension__add(tok) < 0) {
1672 error("Unknown --sort key: `%s'", tok);
1673 usage_with_options(report_usage, options);
1680 int cmd_report(int argc, const char **argv, const char *prefix)
1684 page_size = getpagesize();
1686 argc = parse_options(argc, argv, options, report_usage, 0);
1690 if (parent_pattern != default_parent_pattern)
1691 sort_dimension__add("parent");
1696 * Any (unrecognized) arguments left?
1699 usage_with_options(report_usage, options);
1703 return __cmd_report();