8 #include "util/rbtree.h"
12 #include "util/parse-options.h"
13 #include "util/parse-events.h"
19 static char const *input_name = "output.perf";
21 static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
23 static int dump_trace = 0;
25 static unsigned long page_size;
26 static unsigned long mmap_window = 32;
28 const char *perf_event_names[] = {
29 [PERF_EVENT_MMAP] = " PERF_EVENT_MMAP",
30 [PERF_EVENT_MUNMAP] = " PERF_EVENT_MUNMAP",
31 [PERF_EVENT_COMM] = " PERF_EVENT_COMM",
35 struct perf_event_header header;
40 struct perf_event_header header;
45 char filename[PATH_MAX];
48 struct perf_event_header header;
53 typedef union event_union {
54 struct perf_event_header header;
56 struct mmap_event mmap;
57 struct comm_event comm;
61 struct rb_node rb_node;
67 static struct symbol *symbol__new(uint64_t start, uint64_t len, const char *name)
69 struct symbol *self = malloc(sizeof(*self) + strlen(name) + 1);
73 self->end = start + len;
74 strcpy(self->name, name);
80 static void symbol__delete(struct symbol *self)
85 static size_t symbol__fprintf(struct symbol *self, FILE *fp)
87 return fprintf(fp, " %lx-%lx %s\n",
88 self->start, self->end, self->name);
92 struct list_head node;
97 static struct dso *dso__new(const char *name)
99 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
102 strcpy(self->name, name);
103 self->syms = RB_ROOT;
109 static void dso__delete_symbols(struct dso *self)
112 struct rb_node *next = rb_first(&self->syms);
115 pos = rb_entry(next, struct symbol, rb_node);
116 next = rb_next(&pos->rb_node);
121 static void dso__delete(struct dso *self)
123 dso__delete_symbols(self);
127 static void dso__insert_symbol(struct dso *self, struct symbol *sym)
129 struct rb_node **p = &self->syms.rb_node;
130 struct rb_node *parent = NULL;
131 const uint64_t ip = sym->start;
136 s = rb_entry(parent, struct symbol, rb_node);
142 rb_link_node(&sym->rb_node, parent, p);
143 rb_insert_color(&sym->rb_node, &self->syms);
146 static struct symbol *dso__find_symbol(struct dso *self, uint64_t ip)
151 struct rb_node *n = self->syms.rb_node;
154 struct symbol *s = rb_entry(n, struct symbol, rb_node);
158 else if (ip > s->end)
168 * elf_symtab__for_each_symbol - iterate thru all the symbols
170 * @self: struct elf_symtab instance to iterate
171 * @index: uint32_t index
172 * @sym: GElf_Sym iterator
174 #define elf_symtab__for_each_symbol(syms, nr_syms, index, sym) \
175 for (index = 0, gelf_getsym(syms, index, &sym);\
177 index++, gelf_getsym(syms, index, &sym))
179 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
181 return GELF_ST_TYPE(sym->st_info);
184 static inline int elf_sym__is_function(const GElf_Sym *sym)
186 return elf_sym__type(sym) == STT_FUNC &&
188 sym->st_shndx != SHN_UNDEF;
191 static inline const char *elf_sym__name(const GElf_Sym *sym,
192 const Elf_Data *symstrs)
194 return symstrs->d_buf + sym->st_name;
197 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
198 GElf_Shdr *shp, const char *name,
204 while ((sec = elf_nextscn(elf, sec)) != NULL) {
207 gelf_getshdr(sec, shp);
208 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
209 if (!strcmp(name, str)) {
220 static int dso__load(struct dso *self)
222 int fd = open(self->name, O_RDONLY), err = -1;
227 Elf *elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
229 fprintf(stderr, "%s: cannot read %s ELF file.\n",
230 __func__, self->name);
235 if (gelf_getehdr(elf, &ehdr) == NULL) {
236 fprintf(stderr, "%s: cannot get elf header.\n", __func__);
241 Elf_Scn *sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
243 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
248 Elf_Data *syms = elf_getdata(sec, NULL);
252 sec = elf_getscn(elf, shdr.sh_link);
256 Elf_Data *symstrs = elf_getdata(sec, NULL);
260 const uint32_t nr_syms = shdr.sh_size / shdr.sh_entsize;
264 elf_symtab__for_each_symbol(syms, nr_syms, index, sym) {
267 if (!elf_sym__is_function(&sym))
270 sec = elf_getscn(elf, sym.st_shndx);
274 gelf_getshdr(sec, &shdr);
275 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
277 f = symbol__new(sym.st_value, sym.st_size,
278 elf_sym__name(&sym, symstrs));
282 dso__insert_symbol(self, f);
293 static size_t dso__fprintf(struct dso *self, FILE *fp)
295 size_t ret = fprintf(fp, "dso: %s\n", self->name);
298 for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
299 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
300 ret += symbol__fprintf(pos, fp);
306 static LIST_HEAD(dsos);
307 static struct dso *kernel_dso;
309 static void dsos__add(struct dso *dso)
311 list_add_tail(&dso->node, &dsos);
314 static struct dso *dsos__find(const char *name)
318 list_for_each_entry(pos, &dsos, node)
319 if (strcmp(pos->name, name) == 0)
324 static struct dso *dsos__findnew(const char *name)
326 struct dso *dso = dsos__find(name);
329 dso = dso__new(name);
330 if (dso != NULL && dso__load(dso) < 0)
343 void dsos__fprintf(FILE *fp)
347 list_for_each_entry(pos, &dsos, node)
348 dso__fprintf(pos, fp);
351 static int hex(char ch)
353 if ((ch >= '0') && (ch <= '9'))
355 if ((ch >= 'a') && (ch <= 'f'))
356 return ch - 'a' + 10;
357 if ((ch >= 'A') && (ch <= 'F'))
358 return ch - 'A' + 10;
363 * While we find nice hex chars, build a long_val.
364 * Return number of chars processed.
366 int hex2long(char *ptr, unsigned long *long_val)
372 const int hex_val = hex(*p);
377 *long_val = (*long_val << 4) | hex_val;
384 static int load_kallsyms(void)
386 kernel_dso = dso__new("[kernel]");
387 if (kernel_dso == NULL)
390 FILE *file = fopen("/proc/kallsyms", "r");
398 while (!feof(file)) {
400 int line_len = getline(&line, &n, file);
407 line[--line_len] = '\0'; /* \n */
409 int len = hex2long(line, &start);
411 len += 3; /* ' t ' */
415 * Well fix up the end later, when we have all sorted.
417 struct symbol *sym = symbol__new(start, 0xdead, line + len);
422 dso__insert_symbol(kernel_dso, sym);
426 * Now that we have all sorted out, just set the ->end of all
429 struct rb_node *nd, *prevnd = rb_first(&kernel_dso->syms);
432 goto out_delete_line;
434 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
435 struct symbol *prev = rb_entry(prevnd, struct symbol, rb_node),
436 *curr = rb_entry(nd, struct symbol, rb_node);
438 prev->end = curr->start - 1;
442 dsos__add(kernel_dso);
450 dso__delete(kernel_dso);
455 struct list_head node;
462 static struct map *map__new(struct mmap_event *event)
464 struct map *self = malloc(sizeof(*self));
467 self->start = event->start;
468 self->end = event->start + event->len;
469 self->pgoff = event->pgoff;
471 self->dso = dsos__findnew(event->filename);
472 if (self->dso == NULL)
481 static size_t map__fprintf(struct map *self, FILE *fp)
483 return fprintf(fp, " %lx-%lx %lx %s\n",
484 self->start, self->end, self->pgoff, self->dso->name);
489 static const char *thread__name(struct thread *self, char *bf, size_t size);
492 struct rb_node rb_node;
495 struct thread *thread;
501 static struct symhist *symhist__new(struct symbol *sym, uint64_t ip,
502 struct thread *thread, struct dso *dso,
505 struct symhist *self = malloc(sizeof(*self));
509 self->thread = thread;
519 void symhist__delete(struct symhist *self)
524 static void symhist__inc(struct symhist *self)
530 symhist__fprintf(struct symhist *self, uint64_t total_samples, FILE *fp)
536 ret = fprintf(fp, "%5.2f", (self->count * 100.0) / total_samples);
538 ret = fprintf(fp, "%12d", self->count);
540 ret += fprintf(fp, "%14s [%c] %#018llx ",
541 thread__name(self->thread, bf, sizeof(bf)),
542 self->level, (unsigned long long)self->ip);
544 if (self->level != '.')
545 ret += fprintf(fp, "%s\n",
546 self->sym ? self->sym->name : "<unknown>");
548 ret += fprintf(fp, "%s: %s\n",
549 self->dso ? self->dso->name : "<unknown>",
550 self->sym ? self->sym->name : "<unknown>");
555 struct rb_node rb_node;
556 struct list_head maps;
557 struct rb_root symhists;
562 static const char *thread__name(struct thread *self, char *bf, size_t size)
567 snprintf(bf, sizeof(bf), ":%u", self->pid);
571 static struct thread *thread__new(pid_t pid)
573 struct thread *self = malloc(sizeof(*self));
578 INIT_LIST_HEAD(&self->maps);
579 self->symhists = RB_ROOT;
585 static int thread__symbol_incnew(struct thread *self, struct symbol *sym,
586 uint64_t ip, struct dso *dso, char level)
588 struct rb_node **p = &self->symhists.rb_node;
589 struct rb_node *parent = NULL;
594 sh = rb_entry(parent, struct symhist, rb_node);
596 if (sh->sym == sym || ip == sh->ip) {
601 /* Handle unresolved symbols too */
602 const uint64_t start = !sh->sym ? sh->ip : sh->sym->start;
610 sh = symhist__new(sym, ip, self, dso, level);
613 rb_link_node(&sh->rb_node, parent, p);
614 rb_insert_color(&sh->rb_node, &self->symhists);
618 static int thread__set_comm(struct thread *self, const char *comm)
620 self->comm = strdup(comm);
621 return self->comm ? 0 : -ENOMEM;
624 size_t thread__maps_fprintf(struct thread *self, FILE *fp)
629 list_for_each_entry(pos, &self->maps, node)
630 ret += map__fprintf(pos, fp);
635 static size_t thread__fprintf(struct thread *self, FILE *fp)
637 int ret = fprintf(fp, "thread: %d %s\n", self->pid, self->comm);
640 for (nd = rb_first(&self->symhists); nd; nd = rb_next(nd)) {
641 struct symhist *pos = rb_entry(nd, struct symhist, rb_node);
642 ret += symhist__fprintf(pos, 0, fp);
648 static struct rb_root threads = RB_ROOT;
650 static struct thread *threads__findnew(pid_t pid)
652 struct rb_node **p = &threads.rb_node;
653 struct rb_node *parent = NULL;
658 th = rb_entry(parent, struct thread, rb_node);
669 th = thread__new(pid);
671 rb_link_node(&th->rb_node, parent, p);
672 rb_insert_color(&th->rb_node, &threads);
677 static void thread__insert_map(struct thread *self, struct map *map)
679 list_add_tail(&map->node, &self->maps);
682 static struct map *thread__find_map(struct thread *self, uint64_t ip)
689 list_for_each_entry(pos, &self->maps, node)
690 if (ip >= pos->start && ip <= pos->end)
696 void threads__fprintf(FILE *fp)
699 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
700 struct thread *pos = rb_entry(nd, struct thread, rb_node);
701 thread__fprintf(pos, fp);
705 static struct rb_root global_symhists = RB_ROOT;
707 static void threads__insert_symhist(struct symhist *sh)
709 struct rb_node **p = &global_symhists.rb_node;
710 struct rb_node *parent = NULL;
711 struct symhist *iter;
715 iter = rb_entry(parent, struct symhist, rb_node);
718 if (sh->count > iter->count)
724 rb_link_node(&sh->rb_node, parent, p);
725 rb_insert_color(&sh->rb_node, &global_symhists);
728 static void threads__sort_symhists(void)
732 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
733 struct thread *thread = rb_entry(nd, struct thread, rb_node);
734 struct rb_node *next = rb_first(&thread->symhists);
737 struct symhist *n = rb_entry(next, struct symhist,
739 next = rb_next(&n->rb_node);
740 rb_erase(&n->rb_node, &thread->symhists);
741 threads__insert_symhist(n);
747 static size_t threads__symhists_fprintf(uint64_t total_samples, FILE *fp)
752 for (nd = rb_first(&global_symhists); nd; nd = rb_next(nd)) {
753 struct symhist *pos = rb_entry(nd, struct symhist, rb_node);
754 ret += symhist__fprintf(pos, total_samples, fp);
760 static int __cmd_report(void)
762 unsigned long offset = 0;
763 unsigned long head = 0;
767 int ret, rc = EXIT_FAILURE;
769 unsigned long total = 0, total_mmap = 0, total_comm = 0, total_unknown = 0;
771 input = open(input_name, O_RDONLY);
773 perror("failed to open file");
777 ret = fstat(input, &stat);
779 perror("failed to stat file");
784 fprintf(stderr, "zero-sized file, nothing to do!\n");
788 if (load_kallsyms() < 0) {
789 perror("failed to open kallsyms");
794 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
795 MAP_SHARED, input, offset);
796 if (buf == MAP_FAILED) {
797 perror("failed to mmap file");
802 event = (event_t *)(buf + head);
804 size = event->header.size;
808 if (head + event->header.size >= page_size * mmap_window) {
809 unsigned long shift = page_size * (head / page_size);
812 ret = munmap(buf, page_size * mmap_window);
820 size = event->header.size;
824 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) {
827 struct dso *dso = NULL;
828 struct thread *thread = threads__findnew(event->ip.pid);
829 uint64_t ip = event->ip.ip;
832 fprintf(stderr, "%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
833 (void *)(offset + head),
834 (void *)(long)(event->header.size),
837 (void *)event->ip.ip);
840 if (thread == NULL) {
841 fprintf(stderr, "problem processing %d event, bailing out\n",
846 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
850 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
853 struct map *map = thread__find_map(thread, ip);
856 ip -= map->start + map->pgoff;
863 if (show & show_mask) {
864 struct symbol *sym = dso__find_symbol(dso, ip);
866 if (thread__symbol_incnew(thread, sym, ip, dso, level)) {
867 fprintf(stderr, "problem incrementing symbol count, bailing out\n");
872 } else switch (event->header.type) {
873 case PERF_EVENT_MMAP: {
874 struct thread *thread = threads__findnew(event->mmap.pid);
875 struct map *map = map__new(&event->mmap);
878 fprintf(stderr, "%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n",
879 (void *)(offset + head),
880 (void *)(long)(event->header.size),
881 (void *)event->mmap.start,
882 (void *)event->mmap.len,
883 (void *)event->mmap.pgoff,
884 event->mmap.filename);
886 if (thread == NULL || map == NULL) {
887 fprintf(stderr, "problem processing PERF_EVENT_MMAP, bailing out\n");
890 thread__insert_map(thread, map);
894 case PERF_EVENT_COMM: {
895 struct thread *thread = threads__findnew(event->comm.pid);
898 fprintf(stderr, "%p [%p]: PERF_EVENT_COMM: %s:%d\n",
899 (void *)(offset + head),
900 (void *)(long)(event->header.size),
901 event->comm.comm, event->comm.pid);
903 if (thread == NULL ||
904 thread__set_comm(thread, event->comm.comm)) {
905 fprintf(stderr, "problem processing PERF_EVENT_COMM, bailing out\n");
913 fprintf(stderr, "%p [%p]: skipping unknown header type: %d\n",
914 (void *)(offset + head),
915 (void *)(long)(event->header.size),
920 * assume we lost track of the stream, check alignment, and
921 * increment a single u64 in the hope to catch on again 'soon'.
924 if (unlikely(head & 7))
933 if (offset + head < stat.st_size)
941 fprintf(stderr, " IP events: %10ld\n", total);
942 fprintf(stderr, " mmap events: %10ld\n", total_mmap);
943 fprintf(stderr, " comm events: %10ld\n", total_comm);
944 fprintf(stderr, " unknown events: %10ld\n", total_unknown);
949 threads__sort_symhists();
950 threads__symhists_fprintf(total, stdout);
955 static const char * const report_usage[] = {
956 "perf report [<options>] <command>",
960 static const struct option options[] = {
961 OPT_STRING('i', "input", &input_name, "file",
963 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
964 "dump raw trace in ASCII"),
968 int cmd_report(int argc, const char **argv, const char *prefix)
970 elf_version(EV_CURRENT);
972 page_size = getpagesize();
974 parse_options(argc, argv, options, report_usage, 0);
976 return __cmd_report();