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 unsigned long page_size;
24 static unsigned long mmap_window = 32;
26 const char *perf_event_names[] = {
27 [PERF_EVENT_MMAP] = " PERF_EVENT_MMAP",
28 [PERF_EVENT_MUNMAP] = " PERF_EVENT_MUNMAP",
29 [PERF_EVENT_COMM] = " PERF_EVENT_COMM",
33 struct perf_event_header header;
38 struct perf_event_header header;
43 char filename[PATH_MAX];
46 struct perf_event_header header;
51 typedef union event_union {
52 struct perf_event_header header;
54 struct mmap_event mmap;
55 struct comm_event comm;
59 struct rb_node rb_node;
65 static struct symbol *symbol__new(uint64_t start, uint64_t len, const char *name)
67 struct symbol *self = malloc(sizeof(*self) + strlen(name) + 1);
71 self->end = start + len;
72 strcpy(self->name, name);
78 static void symbol__delete(struct symbol *self)
83 static size_t symbol__fprintf(struct symbol *self, FILE *fp)
85 return fprintf(fp, " %lx-%lx %s\n",
86 self->start, self->end, self->name);
90 struct list_head node;
95 static struct dso *dso__new(const char *name)
97 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
100 strcpy(self->name, name);
101 self->syms = RB_ROOT;
107 static void dso__delete_symbols(struct dso *self)
110 struct rb_node *next = rb_first(&self->syms);
113 pos = rb_entry(next, struct symbol, rb_node);
114 next = rb_next(&pos->rb_node);
119 static void dso__delete(struct dso *self)
121 dso__delete_symbols(self);
125 static void dso__insert_symbol(struct dso *self, struct symbol *sym)
127 struct rb_node **p = &self->syms.rb_node;
128 struct rb_node *parent = NULL;
129 const uint64_t ip = sym->start;
134 s = rb_entry(parent, struct symbol, rb_node);
140 rb_link_node(&sym->rb_node, parent, p);
141 rb_insert_color(&sym->rb_node, &self->syms);
144 static struct symbol *dso__find_symbol(struct dso *self, uint64_t ip)
149 struct rb_node *n = self->syms.rb_node;
152 struct symbol *s = rb_entry(n, struct symbol, rb_node);
156 else if (ip > s->end)
166 * elf_symtab__for_each_symbol - iterate thru all the symbols
168 * @self: struct elf_symtab instance to iterate
169 * @index: uint32_t index
170 * @sym: GElf_Sym iterator
172 #define elf_symtab__for_each_symbol(syms, nr_syms, index, sym) \
173 for (index = 0, gelf_getsym(syms, index, &sym);\
175 index++, gelf_getsym(syms, index, &sym))
177 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
179 return GELF_ST_TYPE(sym->st_info);
182 static inline int elf_sym__is_function(const GElf_Sym *sym)
184 return elf_sym__type(sym) == STT_FUNC &&
186 sym->st_shndx != SHN_UNDEF;
189 static inline const char *elf_sym__name(const GElf_Sym *sym,
190 const Elf_Data *symstrs)
192 return symstrs->d_buf + sym->st_name;
195 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
196 GElf_Shdr *shp, const char *name,
202 while ((sec = elf_nextscn(elf, sec)) != NULL) {
205 gelf_getshdr(sec, shp);
206 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
207 if (!strcmp(name, str)) {
218 static int dso__load(struct dso *self)
220 int fd = open(self->name, O_RDONLY), err = -1;
225 Elf *elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
227 fprintf(stderr, "%s: cannot read %s ELF file.\n",
228 __func__, self->name);
233 if (gelf_getehdr(elf, &ehdr) == NULL) {
234 fprintf(stderr, "%s: cannot get elf header.\n", __func__);
239 Elf_Scn *sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
241 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
246 Elf_Data *syms = elf_getdata(sec, NULL);
250 sec = elf_getscn(elf, shdr.sh_link);
254 Elf_Data *symstrs = elf_getdata(sec, NULL);
258 const uint32_t nr_syms = shdr.sh_size / shdr.sh_entsize;
262 elf_symtab__for_each_symbol(syms, nr_syms, index, sym) {
265 if (!elf_sym__is_function(&sym))
268 sec = elf_getscn(elf, sym.st_shndx);
272 gelf_getshdr(sec, &shdr);
273 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
275 f = symbol__new(sym.st_value, sym.st_size,
276 elf_sym__name(&sym, symstrs));
280 dso__insert_symbol(self, f);
291 static size_t dso__fprintf(struct dso *self, FILE *fp)
293 size_t ret = fprintf(fp, "dso: %s\n", self->name);
296 for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
297 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
298 ret += symbol__fprintf(pos, fp);
304 static LIST_HEAD(dsos);
305 static struct dso *kernel_dso;
307 static void dsos__add(struct dso *dso)
309 list_add_tail(&dso->node, &dsos);
312 static struct dso *dsos__find(const char *name)
316 list_for_each_entry(pos, &dsos, node)
317 if (strcmp(pos->name, name) == 0)
322 static struct dso *dsos__findnew(const char *name)
324 struct dso *dso = dsos__find(name);
327 dso = dso__new(name);
328 if (dso != NULL && dso__load(dso) < 0)
341 void dsos__fprintf(FILE *fp)
345 list_for_each_entry(pos, &dsos, node)
346 dso__fprintf(pos, fp);
349 static int load_kallsyms(void)
351 kernel_dso = dso__new("[kernel]");
352 if (kernel_dso == NULL)
355 FILE *file = fopen("/proc/kallsyms", "r");
363 while (!feof(file)) {
364 unsigned long long start;
367 if (getline(&line, &n, file) < 0)
373 if (sscanf(line, "%llx %c %s", &start, &c, symbf) == 3) {
375 * Well fix up the end later, when we have all sorted.
377 struct symbol *sym = symbol__new(start, 0xdead, symbf);
382 dso__insert_symbol(kernel_dso, sym);
387 * Now that we have all sorted out, just set the ->end of all
390 struct rb_node *nd, *prevnd = rb_first(&kernel_dso->syms);
393 goto out_delete_line;
395 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
396 struct symbol *prev = rb_entry(prevnd, struct symbol, rb_node),
397 *curr = rb_entry(nd, struct symbol, rb_node);
399 prev->end = curr->start - 1;
403 dsos__add(kernel_dso);
411 dso__delete(kernel_dso);
416 struct list_head node;
423 static struct map *map__new(struct mmap_event *event)
425 struct map *self = malloc(sizeof(*self));
428 self->start = event->start;
429 self->end = event->start + event->len;
430 self->pgoff = event->pgoff;
432 self->dso = dsos__findnew(event->filename);
433 if (self->dso == NULL)
442 static size_t map__fprintf(struct map *self, FILE *fp)
444 return fprintf(fp, " %lx-%lx %lx %s\n",
445 self->start, self->end, self->pgoff, self->dso->name);
449 struct rb_node rb_node;
457 static struct symhist *symhist__new(struct symbol *sym, uint64_t ip,
458 struct dso *dso, char level)
460 struct symhist *self = malloc(sizeof(*self));
473 void symhist__delete(struct symhist *self)
478 static void symhist__inc(struct symhist *self)
483 static size_t symhist__fprintf(struct symhist *self, FILE *fp)
485 size_t ret = fprintf(fp, "%#llx [%c] ", (unsigned long long)self->ip, self->level);
487 if (self->level != '.')
488 ret += fprintf(fp, "%s", self->sym ? self->sym->name: "<unknown>");
490 ret += fprintf(fp, "%s: %s",
491 self->dso ? self->dso->name : "<unknown>",
492 self->sym ? self->sym->name : "<unknown>");
493 return ret + fprintf(fp, ": %u\n", self->count);
497 struct rb_node rb_node;
498 struct list_head maps;
499 struct rb_root symhists;
504 static struct thread *thread__new(pid_t pid)
506 struct thread *self = malloc(sizeof(*self));
511 INIT_LIST_HEAD(&self->maps);
512 self->symhists = RB_ROOT;
518 static int thread__symbol_incnew(struct thread *self, struct symbol *sym,
519 uint64_t ip, struct dso *dso, char level)
521 struct rb_node **p = &self->symhists.rb_node;
522 struct rb_node *parent = NULL;
527 sh = rb_entry(parent, struct symhist, rb_node);
529 if (sh->sym == sym || ip == sh->ip) {
534 /* Handle unresolved symbols too */
535 const uint64_t start = !sh->sym ? sh->ip : sh->sym->start;
543 sh = symhist__new(sym, ip, dso, level);
546 rb_link_node(&sh->rb_node, parent, p);
547 rb_insert_color(&sh->rb_node, &self->symhists);
551 static int thread__set_comm(struct thread *self, const char *comm)
553 self->comm = strdup(comm);
554 return self->comm ? 0 : -ENOMEM;
557 size_t thread__maps_fprintf(struct thread *self, FILE *fp)
562 list_for_each_entry(pos, &self->maps, node)
563 ret += map__fprintf(pos, fp);
568 static size_t thread__fprintf(struct thread *self, FILE *fp)
570 int ret = fprintf(fp, "thread: %d %s\n", self->pid, self->comm);
573 for (nd = rb_first(&self->symhists); nd; nd = rb_next(nd)) {
574 struct symhist *pos = rb_entry(nd, struct symhist, rb_node);
575 ret += symhist__fprintf(pos, fp);
581 static struct rb_root threads = RB_ROOT;
583 static struct thread *threads__findnew(pid_t pid)
585 struct rb_node **p = &threads.rb_node;
586 struct rb_node *parent = NULL;
591 th = rb_entry(parent, struct thread, rb_node);
602 th = thread__new(pid);
604 rb_link_node(&th->rb_node, parent, p);
605 rb_insert_color(&th->rb_node, &threads);
610 static void thread__insert_map(struct thread *self, struct map *map)
612 list_add_tail(&map->node, &self->maps);
615 static struct map *thread__find_map(struct thread *self, uint64_t ip)
622 list_for_each_entry(pos, &self->maps, node)
623 if (ip >= pos->start && ip <= pos->end)
629 static void threads__fprintf(FILE *fp)
632 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
633 struct thread *pos = rb_entry(nd, struct thread, rb_node);
634 thread__fprintf(pos, fp);
638 static int __cmd_report(void)
640 unsigned long offset = 0;
641 unsigned long head = 0;
645 int ret, rc = EXIT_FAILURE;
646 unsigned long total = 0;
648 input = open(input_name, O_RDONLY);
650 perror("failed to open file");
654 ret = fstat(input, &stat);
656 perror("failed to stat file");
661 fprintf(stderr, "zero-sized file, nothing to do!\n");
665 if (load_kallsyms() < 0) {
666 perror("failed to open kallsyms");
671 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
672 MAP_SHARED, input, offset);
673 if (buf == MAP_FAILED) {
674 perror("failed to mmap file");
679 event = (event_t *)(buf + head);
681 if (head + event->header.size >= page_size * mmap_window) {
682 unsigned long shift = page_size * (head / page_size);
685 ret = munmap(buf, page_size * mmap_window);
694 if (!event->header.size) {
695 fprintf(stderr, "zero-sized event at file offset %ld\n", offset + head);
696 fprintf(stderr, "skipping %ld bytes of events.\n", stat.st_size - offset - head);
700 head += event->header.size;
702 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) {
705 struct dso *dso = NULL;
706 struct thread *thread = threads__findnew(event->ip.pid);
707 uint64_t ip = event->ip.ip;
709 if (thread == NULL) {
710 fprintf(stderr, "problem processing %d event, bailing out\n",
715 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
719 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
722 struct map *map = thread__find_map(thread, ip);
725 ip -= map->start + map->pgoff;
732 if (show & show_mask) {
733 struct symbol *sym = dso__find_symbol(dso, ip);
735 if (thread__symbol_incnew(thread, sym, ip, dso, level)) {
736 fprintf(stderr, "problem incrementing symbol count, bailing out\n");
741 } else switch (event->header.type) {
742 case PERF_EVENT_MMAP: {
743 struct thread *thread = threads__findnew(event->mmap.pid);
744 struct map *map = map__new(&event->mmap);
746 if (thread == NULL || map == NULL) {
747 fprintf(stderr, "problem processing PERF_EVENT_MMAP, bailing out\n");
750 thread__insert_map(thread, map);
753 case PERF_EVENT_COMM: {
754 struct thread *thread = threads__findnew(event->comm.pid);
756 if (thread == NULL ||
757 thread__set_comm(thread, event->comm.comm)) {
758 fprintf(stderr, "problem processing PERF_EVENT_COMM, bailing out\n");
765 if (offset + head < stat.st_size)
771 //dsos__fprintf(stdout);
772 threads__fprintf(stdout);
774 std::map<std::string, int>::iterator hi = hist.begin();
776 while (hi != hist.end()) {
777 rev_hist.insert(std::pair<int, std::string>(hi->second, hi->first));
781 std::multimap<int, std::string>::const_iterator ri = rev_hist.begin();
783 while (ri != rev_hist.end()) {
784 printf(" %5.2f %s\n", (100.0 * ri->first)/total, ri->second.c_str());
791 static const char * const report_usage[] = {
792 "perf report [<options>] <command>",
796 static const struct option options[] = {
797 OPT_STRING('i', "input", &input_name, "file",
802 int cmd_report(int argc, const char **argv, const char *prefix)
804 elf_version(EV_CURRENT);
806 page_size = getpagesize();
808 parse_options(argc, argv, options, report_usage, 0);
810 return __cmd_report();