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 <linux/rbtree.h>
16 #include "util/symbol.h"
17 #include "util/string.h"
18 #include "util/callchain.h"
19 #include "util/strlist.h"
22 #include "util/header.h"
24 #include "util/parse-options.h"
25 #include "util/parse-events.h"
31 static char const *input_name = "perf.data";
32 static char *vmlinux = NULL;
34 static char default_sort_order[] = "comm,dso";
35 static char *sort_order = default_sort_order;
36 static char *dso_list_str, *comm_list_str, *sym_list_str;
37 static struct strlist *dso_list, *comm_list, *sym_list;
40 static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
42 static int dump_trace = 0;
43 #define dprintf(x...) do { if (dump_trace) printf(x); } while (0)
44 #define cdprintf(x...) do { if (dump_trace) color_fprintf(stdout, color, x); } while (0)
47 #define eprintf(x...) do { if (verbose) fprintf(stderr, x); } while (0)
49 static int full_paths;
51 static unsigned long page_size;
52 static unsigned long mmap_window = 32;
54 static char default_parent_pattern[] = "^sys_|^do_page_fault";
55 static char *parent_pattern = default_parent_pattern;
56 static regex_t parent_regex;
58 static int exclude_other = 1;
61 static u64 sample_type;
64 struct perf_event_header header;
67 unsigned char __more_data[];
71 struct perf_event_header header;
76 char filename[PATH_MAX];
80 struct perf_event_header header;
86 struct perf_event_header header;
91 struct perf_event_header header;
98 struct perf_event_header header;
104 struct perf_event_header header;
110 typedef union event_union {
111 struct perf_event_header header;
113 struct mmap_event mmap;
114 struct comm_event comm;
115 struct fork_event fork;
116 struct period_event period;
117 struct lost_event lost;
118 struct read_event read;
121 static LIST_HEAD(dsos);
122 static struct dso *kernel_dso;
123 static struct dso *vdso;
124 static struct dso *hypervisor_dso;
126 static void dsos__add(struct dso *dso)
128 list_add_tail(&dso->node, &dsos);
131 static struct dso *dsos__find(const char *name)
135 list_for_each_entry(pos, &dsos, node)
136 if (strcmp(pos->name, name) == 0)
141 static struct dso *dsos__findnew(const char *name)
143 struct dso *dso = dsos__find(name);
149 dso = dso__new(name, 0);
153 nr = dso__load(dso, NULL, verbose);
155 eprintf("Failed to open: %s\n", name);
159 eprintf("No symbols found in: %s, maybe install a debug package?\n", name);
170 static void dsos__fprintf(FILE *fp)
174 list_for_each_entry(pos, &dsos, node)
175 dso__fprintf(pos, fp);
178 static struct symbol *vdso__find_symbol(struct dso *dso, u64 ip)
180 return dso__find_symbol(dso, ip);
183 static int load_kernel(void)
187 kernel_dso = dso__new("[kernel]", 0);
191 err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose);
193 dso__delete(kernel_dso);
196 dsos__add(kernel_dso);
198 vdso = dso__new("[vdso]", 0);
202 vdso->find_symbol = vdso__find_symbol;
206 hypervisor_dso = dso__new("[hypervisor]", 0);
209 dsos__add(hypervisor_dso);
214 static char __cwd[PATH_MAX];
215 static char *cwd = __cwd;
218 static int strcommon(const char *pathname)
222 while (pathname[n] == cwd[n] && n < cwdlen)
229 struct list_head node;
233 u64 (*map_ip)(struct map *, u64);
237 static u64 map__map_ip(struct map *map, u64 ip)
239 return ip - map->start + map->pgoff;
242 static u64 vdso__map_ip(struct map *map __used, u64 ip)
247 static inline int is_anon_memory(const char *filename)
249 return strcmp(filename, "//anon") == 0;
252 static struct map *map__new(struct mmap_event *event)
254 struct map *self = malloc(sizeof(*self));
257 const char *filename = event->filename;
258 char newfilename[PATH_MAX];
262 int n = strcommon(filename);
265 snprintf(newfilename, sizeof(newfilename),
266 ".%s", filename + n);
267 filename = newfilename;
271 anon = is_anon_memory(filename);
274 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
275 filename = newfilename;
278 self->start = event->start;
279 self->end = event->start + event->len;
280 self->pgoff = event->pgoff;
282 self->dso = dsos__findnew(filename);
283 if (self->dso == NULL)
286 if (self->dso == vdso || anon)
287 self->map_ip = vdso__map_ip;
289 self->map_ip = map__map_ip;
297 static struct map *map__clone(struct map *self)
299 struct map *map = malloc(sizeof(*self));
304 memcpy(map, self, sizeof(*self));
309 static int map__overlap(struct map *l, struct map *r)
311 if (l->start > r->start) {
317 if (l->end > r->start)
323 static size_t map__fprintf(struct map *self, FILE *fp)
325 return fprintf(fp, " %Lx-%Lx %Lx %s\n",
326 self->start, self->end, self->pgoff, self->dso->name);
331 struct rb_node rb_node;
332 struct list_head maps;
337 static struct thread *thread__new(pid_t pid)
339 struct thread *self = malloc(sizeof(*self));
343 self->comm = malloc(32);
345 snprintf(self->comm, 32, ":%d", self->pid);
346 INIT_LIST_HEAD(&self->maps);
352 static int thread__set_comm(struct thread *self, const char *comm)
356 self->comm = strdup(comm);
357 return self->comm ? 0 : -ENOMEM;
360 static size_t thread__fprintf(struct thread *self, FILE *fp)
363 size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
365 list_for_each_entry(pos, &self->maps, node)
366 ret += map__fprintf(pos, fp);
372 static struct rb_root threads;
373 static struct thread *last_match;
375 static struct thread *threads__findnew(pid_t pid)
377 struct rb_node **p = &threads.rb_node;
378 struct rb_node *parent = NULL;
382 * Font-end cache - PID lookups come in blocks,
383 * so most of the time we dont have to look up
386 if (last_match && last_match->pid == pid)
391 th = rb_entry(parent, struct thread, rb_node);
393 if (th->pid == pid) {
404 th = thread__new(pid);
406 rb_link_node(&th->rb_node, parent, p);
407 rb_insert_color(&th->rb_node, &threads);
414 static void thread__insert_map(struct thread *self, struct map *map)
416 struct map *pos, *tmp;
418 list_for_each_entry_safe(pos, tmp, &self->maps, node) {
419 if (map__overlap(pos, map)) {
421 printf("overlapping maps:\n");
422 map__fprintf(map, stdout);
423 map__fprintf(pos, stdout);
426 if (map->start <= pos->start && map->end > pos->start)
427 pos->start = map->end;
429 if (map->end >= pos->end && map->start < pos->end)
430 pos->end = map->start;
433 printf("after collision:\n");
434 map__fprintf(pos, stdout);
437 if (pos->start >= pos->end) {
438 list_del_init(&pos->node);
444 list_add_tail(&map->node, &self->maps);
447 static int thread__fork(struct thread *self, struct thread *parent)
453 self->comm = strdup(parent->comm);
457 list_for_each_entry(map, &parent->maps, node) {
458 struct map *new = map__clone(map);
461 thread__insert_map(self, new);
467 static struct map *thread__find_map(struct thread *self, u64 ip)
474 list_for_each_entry(pos, &self->maps, node)
475 if (ip >= pos->start && ip <= pos->end)
481 static size_t threads__fprintf(FILE *fp)
486 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
487 struct thread *pos = rb_entry(nd, struct thread, rb_node);
489 ret += thread__fprintf(pos, fp);
496 * histogram, sorted on item, collects counts
499 static struct rb_root hist;
502 struct rb_node rb_node;
504 struct thread *thread;
508 struct symbol *parent;
511 struct callchain_node callchain;
512 struct rb_root sorted_chain;
518 * configurable sorting bits
522 struct list_head list;
526 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
527 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
528 size_t (*print)(FILE *fp, struct hist_entry *);
531 static int64_t cmp_null(void *l, void *r)
544 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
546 return right->thread->pid - left->thread->pid;
550 sort__thread_print(FILE *fp, struct hist_entry *self)
552 return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
555 static struct sort_entry sort_thread = {
556 .header = " Command: Pid",
557 .cmp = sort__thread_cmp,
558 .print = sort__thread_print,
564 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
566 return right->thread->pid - left->thread->pid;
570 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
572 char *comm_l = left->thread->comm;
573 char *comm_r = right->thread->comm;
575 if (!comm_l || !comm_r)
576 return cmp_null(comm_l, comm_r);
578 return strcmp(comm_l, comm_r);
582 sort__comm_print(FILE *fp, struct hist_entry *self)
584 return fprintf(fp, "%16s", self->thread->comm);
587 static struct sort_entry sort_comm = {
588 .header = " Command",
589 .cmp = sort__comm_cmp,
590 .collapse = sort__comm_collapse,
591 .print = sort__comm_print,
597 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
599 struct dso *dso_l = left->dso;
600 struct dso *dso_r = right->dso;
602 if (!dso_l || !dso_r)
603 return cmp_null(dso_l, dso_r);
605 return strcmp(dso_l->name, dso_r->name);
609 sort__dso_print(FILE *fp, struct hist_entry *self)
612 return fprintf(fp, "%-25s", self->dso->name);
614 return fprintf(fp, "%016llx ", (u64)self->ip);
617 static struct sort_entry sort_dso = {
618 .header = "Shared Object ",
619 .cmp = sort__dso_cmp,
620 .print = sort__dso_print,
626 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
630 if (left->sym == right->sym)
633 ip_l = left->sym ? left->sym->start : left->ip;
634 ip_r = right->sym ? right->sym->start : right->ip;
636 return (int64_t)(ip_r - ip_l);
640 sort__sym_print(FILE *fp, struct hist_entry *self)
645 ret += fprintf(fp, "%#018llx ", (u64)self->ip);
648 ret += fprintf(fp, "[%c] %s",
649 self->dso == kernel_dso ? 'k' :
650 self->dso == hypervisor_dso ? 'h' : '.', self->sym->name);
652 ret += fprintf(fp, "%#016llx", (u64)self->ip);
658 static struct sort_entry sort_sym = {
660 .cmp = sort__sym_cmp,
661 .print = sort__sym_print,
667 sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
669 struct symbol *sym_l = left->parent;
670 struct symbol *sym_r = right->parent;
672 if (!sym_l || !sym_r)
673 return cmp_null(sym_l, sym_r);
675 return strcmp(sym_l->name, sym_r->name);
679 sort__parent_print(FILE *fp, struct hist_entry *self)
683 ret += fprintf(fp, "%-20s", self->parent ? self->parent->name : "[other]");
688 static struct sort_entry sort_parent = {
689 .header = "Parent symbol ",
690 .cmp = sort__parent_cmp,
691 .print = sort__parent_print,
694 static int sort__need_collapse = 0;
695 static int sort__has_parent = 0;
697 struct sort_dimension {
699 struct sort_entry *entry;
703 static struct sort_dimension sort_dimensions[] = {
704 { .name = "pid", .entry = &sort_thread, },
705 { .name = "comm", .entry = &sort_comm, },
706 { .name = "dso", .entry = &sort_dso, },
707 { .name = "symbol", .entry = &sort_sym, },
708 { .name = "parent", .entry = &sort_parent, },
711 static LIST_HEAD(hist_entry__sort_list);
713 static int sort_dimension__add(char *tok)
717 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
718 struct sort_dimension *sd = &sort_dimensions[i];
723 if (strncasecmp(tok, sd->name, strlen(tok)))
726 if (sd->entry->collapse)
727 sort__need_collapse = 1;
729 if (sd->entry == &sort_parent) {
730 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
734 regerror(ret, &parent_regex, err, sizeof(err));
735 fprintf(stderr, "Invalid regex: %s\n%s",
736 parent_pattern, err);
739 sort__has_parent = 1;
742 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
752 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
754 struct sort_entry *se;
757 list_for_each_entry(se, &hist_entry__sort_list, list) {
758 cmp = se->cmp(left, right);
767 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
769 struct sort_entry *se;
772 list_for_each_entry(se, &hist_entry__sort_list, list) {
773 int64_t (*f)(struct hist_entry *, struct hist_entry *);
775 f = se->collapse ?: se->cmp;
777 cmp = f(left, right);
786 callchain__fprintf(FILE *fp, struct callchain_node *self, u64 total_samples)
788 struct callchain_list *chain;
794 ret += callchain__fprintf(fp, self->parent, total_samples);
797 list_for_each_entry(chain, &self->val, list) {
798 if (chain->ip >= PERF_CONTEXT_MAX)
801 ret += fprintf(fp, " %s\n", chain->sym->name);
803 ret += fprintf(fp, " %p\n",
804 (void *)(long)chain->ip);
811 hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
814 struct rb_node *rb_node;
815 struct callchain_node *chain;
818 rb_node = rb_first(&self->sorted_chain);
822 chain = rb_entry(rb_node, struct callchain_node, rb_node);
823 percent = chain->hit * 100.0 / total_samples;
824 ret += fprintf(fp, " %6.2f%%\n", percent);
825 ret += callchain__fprintf(fp, chain, total_samples);
826 ret += fprintf(fp, "\n");
827 rb_node = rb_next(rb_node);
835 hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
837 struct sort_entry *se;
840 if (exclude_other && !self->parent)
844 double percent = self->count * 100.0 / total_samples;
845 char *color = PERF_COLOR_NORMAL;
848 * We color high-overhead entries in red, mid-overhead
849 * entries in green - and keep the low overhead places
852 if (percent >= 5.0) {
853 color = PERF_COLOR_RED;
856 color = PERF_COLOR_GREEN;
859 ret = color_fprintf(fp, color, " %6.2f%%",
860 (self->count * 100.0) / total_samples);
862 ret = fprintf(fp, "%12Ld ", self->count);
864 list_for_each_entry(se, &hist_entry__sort_list, list) {
865 if (exclude_other && (se == &sort_parent))
869 ret += se->print(fp, self);
872 ret += fprintf(fp, "\n");
875 hist_entry_callchain__fprintf(fp, self, total_samples);
884 static struct symbol *
885 resolve_symbol(struct thread *thread, struct map **mapp,
886 struct dso **dsop, u64 *ipp)
888 struct dso *dso = dsop ? *dsop : NULL;
889 struct map *map = mapp ? *mapp : NULL;
901 map = thread__find_map(thread, ip);
906 ip = map->map_ip(map, ip);
911 * If this is outside of all known maps,
912 * and is a negative address, try to look it
913 * up in the kernel dso, as it might be a
914 * vsyscall (which executes in user-mode):
916 if ((long long)ip < 0)
919 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
920 dprintf(" ...... map: %Lx -> %Lx\n", *ipp, ip);
929 return dso->find_symbol(dso, ip);
932 static int call__match(struct symbol *sym)
934 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
940 static struct symbol **
941 resolve_callchain(struct thread *thread, struct map *map __used,
942 struct ip_callchain *chain, struct hist_entry *entry)
944 u64 context = PERF_CONTEXT_MAX;
945 struct symbol **syms;
949 syms = calloc(chain->nr, sizeof(*syms));
951 fprintf(stderr, "Can't allocate memory for symbols\n");
956 for (i = 0; i < chain->nr; i++) {
957 u64 ip = chain->ips[i];
958 struct dso *dso = NULL;
961 if (ip >= PERF_CONTEXT_MAX) {
967 case PERF_CONTEXT_HV:
968 dso = hypervisor_dso;
970 case PERF_CONTEXT_KERNEL:
977 sym = resolve_symbol(thread, NULL, &dso, &ip);
980 if (sort__has_parent && call__match(sym) &&
993 * collect histogram counts
997 hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
998 struct symbol *sym, u64 ip, struct ip_callchain *chain,
999 char level, u64 count)
1001 struct rb_node **p = &hist.rb_node;
1002 struct rb_node *parent = NULL;
1003 struct hist_entry *he;
1004 struct symbol **syms = NULL;
1005 struct hist_entry entry = {
1014 .sorted_chain = RB_ROOT
1018 if ((sort__has_parent || callchain) && chain)
1019 syms = resolve_callchain(thread, map, chain, &entry);
1021 while (*p != NULL) {
1023 he = rb_entry(parent, struct hist_entry, rb_node);
1025 cmp = hist_entry__cmp(&entry, he);
1030 append_chain(&he->callchain, chain, syms);
1039 p = &(*p)->rb_right;
1042 he = malloc(sizeof(*he));
1047 callchain_init(&he->callchain);
1048 append_chain(&he->callchain, chain, syms);
1051 rb_link_node(&he->rb_node, parent, p);
1052 rb_insert_color(&he->rb_node, &hist);
1057 static void hist_entry__free(struct hist_entry *he)
1063 * collapse the histogram
1066 static struct rb_root collapse_hists;
1068 static void collapse__insert_entry(struct hist_entry *he)
1070 struct rb_node **p = &collapse_hists.rb_node;
1071 struct rb_node *parent = NULL;
1072 struct hist_entry *iter;
1075 while (*p != NULL) {
1077 iter = rb_entry(parent, struct hist_entry, rb_node);
1079 cmp = hist_entry__collapse(iter, he);
1082 iter->count += he->count;
1083 hist_entry__free(he);
1090 p = &(*p)->rb_right;
1093 rb_link_node(&he->rb_node, parent, p);
1094 rb_insert_color(&he->rb_node, &collapse_hists);
1097 static void collapse__resort(void)
1099 struct rb_node *next;
1100 struct hist_entry *n;
1102 if (!sort__need_collapse)
1105 next = rb_first(&hist);
1107 n = rb_entry(next, struct hist_entry, rb_node);
1108 next = rb_next(&n->rb_node);
1110 rb_erase(&n->rb_node, &hist);
1111 collapse__insert_entry(n);
1116 * reverse the map, sort on count.
1119 static struct rb_root output_hists;
1121 static void output__insert_entry(struct hist_entry *he)
1123 struct rb_node **p = &output_hists.rb_node;
1124 struct rb_node *parent = NULL;
1125 struct hist_entry *iter;
1128 sort_chain_to_rbtree(&he->sorted_chain, &he->callchain);
1130 while (*p != NULL) {
1132 iter = rb_entry(parent, struct hist_entry, rb_node);
1134 if (he->count > iter->count)
1137 p = &(*p)->rb_right;
1140 rb_link_node(&he->rb_node, parent, p);
1141 rb_insert_color(&he->rb_node, &output_hists);
1144 static void output__resort(void)
1146 struct rb_node *next;
1147 struct hist_entry *n;
1148 struct rb_root *tree = &hist;
1150 if (sort__need_collapse)
1151 tree = &collapse_hists;
1153 next = rb_first(tree);
1156 n = rb_entry(next, struct hist_entry, rb_node);
1157 next = rb_next(&n->rb_node);
1159 rb_erase(&n->rb_node, tree);
1160 output__insert_entry(n);
1164 static size_t output__fprintf(FILE *fp, u64 total_samples)
1166 struct hist_entry *pos;
1167 struct sort_entry *se;
1173 fprintf(fp, "# (%Ld samples)\n", (u64)total_samples);
1176 fprintf(fp, "# Overhead");
1177 list_for_each_entry(se, &hist_entry__sort_list, list) {
1178 if (exclude_other && (se == &sort_parent))
1180 fprintf(fp, " %s", se->header);
1184 fprintf(fp, "# ........");
1185 list_for_each_entry(se, &hist_entry__sort_list, list) {
1188 if (exclude_other && (se == &sort_parent))
1192 for (i = 0; i < strlen(se->header); i++)
1199 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
1200 pos = rb_entry(nd, struct hist_entry, rb_node);
1201 ret += hist_entry__fprintf(fp, pos, total_samples);
1204 if (sort_order == default_sort_order &&
1205 parent_pattern == default_parent_pattern) {
1207 fprintf(fp, "# (For more details, try: perf report --sort comm,dso,symbol)\n");
1215 static void register_idle_thread(void)
1217 struct thread *thread = threads__findnew(0);
1219 if (thread == NULL ||
1220 thread__set_comm(thread, "[idle]")) {
1221 fprintf(stderr, "problem inserting idle task.\n");
1226 static unsigned long total = 0,
1233 static int validate_chain(struct ip_callchain *chain, event_t *event)
1235 unsigned int chain_size;
1237 chain_size = event->header.size;
1238 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
1240 if (chain->nr*sizeof(u64) > chain_size)
1247 process_sample_event(event_t *event, unsigned long offset, unsigned long head)
1251 struct dso *dso = NULL;
1252 struct thread *thread = threads__findnew(event->ip.pid);
1253 u64 ip = event->ip.ip;
1255 struct map *map = NULL;
1256 void *more_data = event->ip.__more_data;
1257 struct ip_callchain *chain = NULL;
1260 if (sample_type & PERF_SAMPLE_PERIOD) {
1261 period = *(u64 *)more_data;
1262 more_data += sizeof(u64);
1265 dprintf("%p [%p]: PERF_EVENT_SAMPLE (IP, %d): %d: %p period: %Ld\n",
1266 (void *)(offset + head),
1267 (void *)(long)(event->header.size),
1273 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
1276 chain = (void *)more_data;
1278 dprintf("... chain: nr:%Lu\n", chain->nr);
1280 if (validate_chain(chain, event) < 0) {
1281 eprintf("call-chain problem with event, skipping it.\n");
1286 for (i = 0; i < chain->nr; i++)
1287 dprintf("..... %2d: %016Lx\n", i, chain->ips[i]);
1291 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1293 if (thread == NULL) {
1294 eprintf("problem processing %d event, skipping it.\n",
1295 event->header.type);
1299 if (comm_list && !strlist__has_entry(comm_list, thread->comm))
1302 cpumode = event->header.misc & PERF_EVENT_MISC_CPUMODE_MASK;
1304 if (cpumode == PERF_EVENT_MISC_KERNEL) {
1310 dprintf(" ...... dso: %s\n", dso->name);
1312 } else if (cpumode == PERF_EVENT_MISC_USER) {
1321 dso = hypervisor_dso;
1323 dprintf(" ...... dso: [hypervisor]\n");
1326 if (show & show_mask) {
1327 struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
1329 if (dso_list && dso && dso->name && !strlist__has_entry(dso_list, dso->name))
1332 if (sym_list && sym && !strlist__has_entry(sym_list, sym->name))
1335 if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
1336 eprintf("problem incrementing symbol count, skipping event\n");
1346 process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
1348 struct thread *thread = threads__findnew(event->mmap.pid);
1349 struct map *map = map__new(&event->mmap);
1351 dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
1352 (void *)(offset + head),
1353 (void *)(long)(event->header.size),
1355 (void *)(long)event->mmap.start,
1356 (void *)(long)event->mmap.len,
1357 (void *)(long)event->mmap.pgoff,
1358 event->mmap.filename);
1360 if (thread == NULL || map == NULL) {
1361 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
1365 thread__insert_map(thread, map);
1372 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
1374 struct thread *thread = threads__findnew(event->comm.pid);
1376 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
1377 (void *)(offset + head),
1378 (void *)(long)(event->header.size),
1379 event->comm.comm, event->comm.pid);
1381 if (thread == NULL ||
1382 thread__set_comm(thread, event->comm.comm)) {
1383 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
1392 process_fork_event(event_t *event, unsigned long offset, unsigned long head)
1394 struct thread *thread = threads__findnew(event->fork.pid);
1395 struct thread *parent = threads__findnew(event->fork.ppid);
1397 dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
1398 (void *)(offset + head),
1399 (void *)(long)(event->header.size),
1400 event->fork.pid, event->fork.ppid);
1402 if (!thread || !parent || thread__fork(thread, parent)) {
1403 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
1412 process_period_event(event_t *event, unsigned long offset, unsigned long head)
1414 dprintf("%p [%p]: PERF_EVENT_PERIOD: time:%Ld, id:%Ld: period:%Ld\n",
1415 (void *)(offset + head),
1416 (void *)(long)(event->header.size),
1419 event->period.sample_period);
1425 process_lost_event(event_t *event, unsigned long offset, unsigned long head)
1427 dprintf("%p [%p]: PERF_EVENT_LOST: id:%Ld: lost:%Ld\n",
1428 (void *)(offset + head),
1429 (void *)(long)(event->header.size),
1433 total_lost += event->lost.lost;
1438 static void trace_event(event_t *event)
1440 unsigned char *raw_event = (void *)event;
1441 char *color = PERF_COLOR_BLUE;
1448 cdprintf("\n. ... raw event: size %d bytes\n", event->header.size);
1450 for (i = 0; i < event->header.size; i++) {
1451 if ((i & 15) == 0) {
1453 cdprintf(" %04x: ", i);
1456 cdprintf(" %02x", raw_event[i]);
1458 if (((i & 15) == 15) || i == event->header.size-1) {
1460 for (j = 0; j < 15-(i & 15); j++)
1462 for (j = 0; j < (i & 15); j++) {
1463 if (isprint(raw_event[i-15+j]))
1464 cdprintf("%c", raw_event[i-15+j]);
1475 process_read_event(event_t *event, unsigned long offset, unsigned long head)
1477 dprintf("%p [%p]: PERF_EVENT_READ: %d %d %Lu\n",
1478 (void *)(offset + head),
1479 (void *)(long)(event->header.size),
1488 process_event(event_t *event, unsigned long offset, unsigned long head)
1492 switch (event->header.type) {
1493 case PERF_EVENT_SAMPLE:
1494 return process_sample_event(event, offset, head);
1496 case PERF_EVENT_MMAP:
1497 return process_mmap_event(event, offset, head);
1499 case PERF_EVENT_COMM:
1500 return process_comm_event(event, offset, head);
1502 case PERF_EVENT_FORK:
1503 return process_fork_event(event, offset, head);
1505 case PERF_EVENT_PERIOD:
1506 return process_period_event(event, offset, head);
1508 case PERF_EVENT_LOST:
1509 return process_lost_event(event, offset, head);
1511 case PERF_EVENT_READ:
1512 return process_read_event(event, offset, head);
1515 * We dont process them right now but they are fine:
1518 case PERF_EVENT_THROTTLE:
1519 case PERF_EVENT_UNTHROTTLE:
1529 static struct perf_header *header;
1531 static u64 perf_header__sample_type(void)
1533 u64 sample_type = 0;
1536 for (i = 0; i < header->attrs; i++) {
1537 struct perf_header_attr *attr = header->attr[i];
1540 sample_type = attr->attr.sample_type;
1541 else if (sample_type != attr->attr.sample_type)
1542 die("non matching sample_type");
1548 static int __cmd_report(void)
1550 int ret, rc = EXIT_FAILURE;
1551 unsigned long offset = 0;
1552 unsigned long head, shift;
1558 register_idle_thread();
1560 input = open(input_name, O_RDONLY);
1562 fprintf(stderr, " failed to open file: %s", input_name);
1563 if (!strcmp(input_name, "perf.data"))
1564 fprintf(stderr, " (try 'perf record' first)");
1565 fprintf(stderr, "\n");
1569 ret = fstat(input, &stat);
1571 perror("failed to stat file");
1575 if (!stat.st_size) {
1576 fprintf(stderr, "zero-sized file, nothing to do!\n");
1580 header = perf_header__read(input);
1581 head = header->data_offset;
1583 sample_type = perf_header__sample_type();
1585 if (sort__has_parent && !(sample_type & PERF_SAMPLE_CALLCHAIN)) {
1586 fprintf(stderr, "selected --sort parent, but no callchain data\n");
1590 if (load_kernel() < 0) {
1591 perror("failed to load kernel symbols");
1592 return EXIT_FAILURE;
1596 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
1597 perror("failed to get the current directory");
1598 return EXIT_FAILURE;
1600 cwdlen = strlen(cwd);
1606 shift = page_size * (head / page_size);
1611 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1612 MAP_SHARED, input, offset);
1613 if (buf == MAP_FAILED) {
1614 perror("failed to mmap file");
1619 event = (event_t *)(buf + head);
1621 size = event->header.size;
1625 if (head + event->header.size >= page_size * mmap_window) {
1628 shift = page_size * (head / page_size);
1630 ret = munmap(buf, page_size * mmap_window);
1638 size = event->header.size;
1640 dprintf("\n%p [%p]: event: %d\n",
1641 (void *)(offset + head),
1642 (void *)(long)event->header.size,
1643 event->header.type);
1645 if (!size || process_event(event, offset, head) < 0) {
1647 dprintf("%p [%p]: skipping unknown header type: %d\n",
1648 (void *)(offset + head),
1649 (void *)(long)(event->header.size),
1650 event->header.type);
1655 * assume we lost track of the stream, check alignment, and
1656 * increment a single u64 in the hope to catch on again 'soon'.
1659 if (unlikely(head & 7))
1667 if (offset + head >= header->data_offset + header->data_size)
1670 if (offset + head < (unsigned long)stat.st_size)
1677 dprintf(" IP events: %10ld\n", total);
1678 dprintf(" mmap events: %10ld\n", total_mmap);
1679 dprintf(" comm events: %10ld\n", total_comm);
1680 dprintf(" fork events: %10ld\n", total_fork);
1681 dprintf(" lost events: %10ld\n", total_lost);
1682 dprintf(" unknown events: %10ld\n", total_unknown);
1688 threads__fprintf(stdout);
1691 dsos__fprintf(stdout);
1695 output__fprintf(stdout, total);
1700 static const char * const report_usage[] = {
1701 "perf report [<options>] <command>",
1705 static const struct option options[] = {
1706 OPT_STRING('i', "input", &input_name, "file",
1708 OPT_BOOLEAN('v', "verbose", &verbose,
1709 "be more verbose (show symbol address, etc)"),
1710 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1711 "dump raw trace in ASCII"),
1712 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
1713 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1714 "sort by key(s): pid, comm, dso, symbol, parent"),
1715 OPT_BOOLEAN('P', "full-paths", &full_paths,
1716 "Don't shorten the pathnames taking into account the cwd"),
1717 OPT_STRING('p', "parent", &parent_pattern, "regex",
1718 "regex filter to identify parent, see: '--sort parent'"),
1719 OPT_BOOLEAN('x', "exclude-other", &exclude_other,
1720 "Only display entries with parent-match"),
1721 OPT_BOOLEAN('c', "callchain", &callchain, "Display callchains"),
1722 OPT_STRING('d', "dsos", &dso_list_str, "dso[,dso...]",
1723 "only consider symbols in these dsos"),
1724 OPT_STRING('C', "comms", &comm_list_str, "comm[,comm...]",
1725 "only consider symbols in these comms"),
1726 OPT_STRING('S', "symbols", &sym_list_str, "symbol[,symbol...]",
1727 "only consider these symbols"),
1731 static void setup_sorting(void)
1733 char *tmp, *tok, *str = strdup(sort_order);
1735 for (tok = strtok_r(str, ", ", &tmp);
1736 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1737 if (sort_dimension__add(tok) < 0) {
1738 error("Unknown --sort key: `%s'", tok);
1739 usage_with_options(report_usage, options);
1746 static void setup_list(struct strlist **list, const char *list_str,
1747 const char *list_name)
1750 *list = strlist__new(true, list_str);
1752 fprintf(stderr, "problems parsing %s list\n",
1759 int cmd_report(int argc, const char **argv, const char *prefix __used)
1763 page_size = getpagesize();
1765 argc = parse_options(argc, argv, options, report_usage, 0);
1769 if (parent_pattern != default_parent_pattern)
1770 sort_dimension__add("parent");
1775 * Any (unrecognized) arguments left?
1778 usage_with_options(report_usage, options);
1780 setup_list(&dso_list, dso_list_str, "dso");
1781 setup_list(&comm_list, comm_list_str, "comm");
1782 setup_list(&sym_list, sym_list_str, "symbol");
1786 return __cmd_report();