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 load_kallsyms(void)
353 kernel_dso = dso__new("[kernel]");
354 if (kernel_dso == NULL)
357 FILE *file = fopen("/proc/kallsyms", "r");
365 while (!feof(file)) {
366 unsigned long long start;
369 if (getline(&line, &n, file) < 0)
375 if (sscanf(line, "%llx %c %s", &start, &c, symbf) == 3) {
377 * Well fix up the end later, when we have all sorted.
379 struct symbol *sym = symbol__new(start, 0xdead, symbf);
384 dso__insert_symbol(kernel_dso, sym);
389 * Now that we have all sorted out, just set the ->end of all
392 struct rb_node *nd, *prevnd = rb_first(&kernel_dso->syms);
395 goto out_delete_line;
397 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
398 struct symbol *prev = rb_entry(prevnd, struct symbol, rb_node),
399 *curr = rb_entry(nd, struct symbol, rb_node);
401 prev->end = curr->start - 1;
405 dsos__add(kernel_dso);
413 dso__delete(kernel_dso);
418 struct list_head node;
425 static struct map *map__new(struct mmap_event *event)
427 struct map *self = malloc(sizeof(*self));
430 self->start = event->start;
431 self->end = event->start + event->len;
432 self->pgoff = event->pgoff;
434 self->dso = dsos__findnew(event->filename);
435 if (self->dso == NULL)
444 static size_t map__fprintf(struct map *self, FILE *fp)
446 return fprintf(fp, " %lx-%lx %lx %s\n",
447 self->start, self->end, self->pgoff, self->dso->name);
451 struct rb_node rb_node;
459 static struct symhist *symhist__new(struct symbol *sym, uint64_t ip,
460 struct dso *dso, char level)
462 struct symhist *self = malloc(sizeof(*self));
475 void symhist__delete(struct symhist *self)
480 static void symhist__inc(struct symhist *self)
485 static size_t symhist__fprintf(struct symhist *self, FILE *fp)
487 size_t ret = fprintf(fp, "%#llx [%c] ", (unsigned long long)self->ip, self->level);
489 if (self->level != '.')
490 ret += fprintf(fp, "%s", self->sym ? self->sym->name: "<unknown>");
492 ret += fprintf(fp, "%s: %s",
493 self->dso ? self->dso->name : "<unknown>",
494 self->sym ? self->sym->name : "<unknown>");
495 return ret + fprintf(fp, ": %u\n", self->count);
499 struct rb_node rb_node;
500 struct list_head maps;
501 struct rb_root symhists;
506 static struct thread *thread__new(pid_t pid)
508 struct thread *self = malloc(sizeof(*self));
513 INIT_LIST_HEAD(&self->maps);
514 self->symhists = RB_ROOT;
520 static int thread__symbol_incnew(struct thread *self, struct symbol *sym,
521 uint64_t ip, struct dso *dso, char level)
523 struct rb_node **p = &self->symhists.rb_node;
524 struct rb_node *parent = NULL;
529 sh = rb_entry(parent, struct symhist, rb_node);
531 if (sh->sym == sym || ip == sh->ip) {
536 /* Handle unresolved symbols too */
537 const uint64_t start = !sh->sym ? sh->ip : sh->sym->start;
545 sh = symhist__new(sym, ip, dso, level);
548 rb_link_node(&sh->rb_node, parent, p);
549 rb_insert_color(&sh->rb_node, &self->symhists);
553 static int thread__set_comm(struct thread *self, const char *comm)
555 self->comm = strdup(comm);
556 return self->comm ? 0 : -ENOMEM;
559 size_t thread__maps_fprintf(struct thread *self, FILE *fp)
564 list_for_each_entry(pos, &self->maps, node)
565 ret += map__fprintf(pos, fp);
570 static size_t thread__fprintf(struct thread *self, FILE *fp)
572 int ret = fprintf(fp, "thread: %d %s\n", self->pid, self->comm);
575 for (nd = rb_first(&self->symhists); nd; nd = rb_next(nd)) {
576 struct symhist *pos = rb_entry(nd, struct symhist, rb_node);
577 ret += symhist__fprintf(pos, fp);
583 static struct rb_root threads = RB_ROOT;
585 static struct thread *threads__findnew(pid_t pid)
587 struct rb_node **p = &threads.rb_node;
588 struct rb_node *parent = NULL;
593 th = rb_entry(parent, struct thread, rb_node);
604 th = thread__new(pid);
606 rb_link_node(&th->rb_node, parent, p);
607 rb_insert_color(&th->rb_node, &threads);
612 static void thread__insert_map(struct thread *self, struct map *map)
614 list_add_tail(&map->node, &self->maps);
617 static struct map *thread__find_map(struct thread *self, uint64_t ip)
624 list_for_each_entry(pos, &self->maps, node)
625 if (ip >= pos->start && ip <= pos->end)
631 static void threads__fprintf(FILE *fp)
634 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
635 struct thread *pos = rb_entry(nd, struct thread, rb_node);
636 thread__fprintf(pos, fp);
640 static int __cmd_report(void)
642 unsigned long offset = 0;
643 unsigned long head = 0;
647 int ret, rc = EXIT_FAILURE;
649 unsigned long total = 0, total_mmap = 0, total_comm = 0, total_unknown = 0;
651 input = open(input_name, O_RDONLY);
653 perror("failed to open file");
657 ret = fstat(input, &stat);
659 perror("failed to stat file");
664 fprintf(stderr, "zero-sized file, nothing to do!\n");
668 if (load_kallsyms() < 0) {
669 perror("failed to open kallsyms");
674 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
675 MAP_SHARED, input, offset);
676 if (buf == MAP_FAILED) {
677 perror("failed to mmap file");
682 event = (event_t *)(buf + head);
684 size = event->header.size;
688 if (head + event->header.size >= page_size * mmap_window) {
689 unsigned long shift = page_size * (head / page_size);
692 ret = munmap(buf, page_size * mmap_window);
700 size = event->header.size;
704 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) {
707 struct dso *dso = NULL;
708 struct thread *thread = threads__findnew(event->ip.pid);
709 uint64_t ip = event->ip.ip;
712 fprintf(stderr, "%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
713 (void *)(offset + head),
714 (void *)(long)(event->header.size),
717 (void *)event->ip.ip);
720 if (thread == NULL) {
721 fprintf(stderr, "problem processing %d event, bailing out\n",
726 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
730 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
733 struct map *map = thread__find_map(thread, ip);
736 ip -= map->start + map->pgoff;
743 if (show & show_mask) {
744 struct symbol *sym = dso__find_symbol(dso, ip);
746 if (thread__symbol_incnew(thread, sym, ip, dso, level)) {
747 fprintf(stderr, "problem incrementing symbol count, bailing out\n");
752 } else switch (event->header.type) {
753 case PERF_EVENT_MMAP: {
754 struct thread *thread = threads__findnew(event->mmap.pid);
755 struct map *map = map__new(&event->mmap);
758 fprintf(stderr, "%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n",
759 (void *)(offset + head),
760 (void *)(long)(event->header.size),
761 (void *)event->mmap.start,
762 (void *)event->mmap.len,
763 (void *)event->mmap.pgoff,
764 event->mmap.filename);
766 if (thread == NULL || map == NULL) {
767 fprintf(stderr, "problem processing PERF_EVENT_MMAP, bailing out\n");
770 thread__insert_map(thread, map);
774 case PERF_EVENT_COMM: {
775 struct thread *thread = threads__findnew(event->comm.pid);
778 fprintf(stderr, "%p [%p]: PERF_EVENT_COMM: %s:%d\n",
779 (void *)(offset + head),
780 (void *)(long)(event->header.size),
781 event->comm.comm, event->comm.pid);
783 if (thread == NULL ||
784 thread__set_comm(thread, event->comm.comm)) {
785 fprintf(stderr, "problem processing PERF_EVENT_COMM, bailing out\n");
793 fprintf(stderr, "%p [%p]: skipping unknown header type: %d\n",
794 (void *)(offset + head),
795 (void *)(long)(event->header.size),
800 * assume we lost track of the stream, check alignment, and
801 * increment a single u64 in the hope to catch on again 'soon'.
804 if (unlikely(head & 7))
813 if (offset + head < stat.st_size)
821 fprintf(stderr, " IP events: %10ld\n", total);
822 fprintf(stderr, " mmap events: %10ld\n", total_mmap);
823 fprintf(stderr, " comm events: %10ld\n", total_comm);
824 fprintf(stderr, " unknown events: %10ld\n", total_unknown);
829 //dsos__fprintf(stdout);
830 threads__fprintf(stdout);
832 std::map<std::string, int>::iterator hi = hist.begin();
834 while (hi != hist.end()) {
835 rev_hist.insert(std::pair<int, std::string>(hi->second, hi->first));
839 std::multimap<int, std::string>::const_iterator ri = rev_hist.begin();
841 while (ri != rev_hist.end()) {
842 printf(" %5.2f %s\n", (100.0 * ri->first)/total, ri->second.c_str());
849 static const char * const report_usage[] = {
850 "perf report [<options>] <command>",
854 static const struct option options[] = {
855 OPT_STRING('i', "input", &input_name, "file",
857 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
858 "dump raw trace in ASCII"),
862 int cmd_report(int argc, const char **argv, const char *prefix)
864 elf_version(EV_CURRENT);
866 page_size = getpagesize();
868 parse_options(argc, argv, options, report_usage, 0);
870 return __cmd_report();