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;
648 unsigned long total = 0, total_mmap = 0, total_comm = 0, total_unknown;
650 input = open(input_name, O_RDONLY);
652 perror("failed to open file");
656 ret = fstat(input, &stat);
658 perror("failed to stat file");
663 fprintf(stderr, "zero-sized file, nothing to do!\n");
667 if (load_kallsyms() < 0) {
668 perror("failed to open kallsyms");
673 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
674 MAP_SHARED, input, offset);
675 if (buf == MAP_FAILED) {
676 perror("failed to mmap file");
681 event = (event_t *)(buf + head);
683 if (head + event->header.size >= page_size * mmap_window) {
684 unsigned long shift = page_size * (head / page_size);
687 ret = munmap(buf, page_size * mmap_window);
696 if (!event->header.size) {
697 fprintf(stderr, "zero-sized event at file offset %ld\n", offset + head);
698 fprintf(stderr, "skipping %ld bytes of events.\n", stat.st_size - offset - head);
702 head += 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, "PERF_EVENT (IP, %d): %d: %p\n",
715 (void *)event->ip.ip);
718 if (thread == NULL) {
719 fprintf(stderr, "problem processing %d event, bailing out\n",
724 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
728 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
731 struct map *map = thread__find_map(thread, ip);
734 ip -= map->start + map->pgoff;
741 if (show & show_mask) {
742 struct symbol *sym = dso__find_symbol(dso, ip);
744 if (thread__symbol_incnew(thread, sym, ip, dso, level)) {
745 fprintf(stderr, "problem incrementing symbol count, bailing out\n");
750 } else switch (event->header.type) {
751 case PERF_EVENT_MMAP: {
752 struct thread *thread = threads__findnew(event->mmap.pid);
753 struct map *map = map__new(&event->mmap);
756 fprintf(stderr, "PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n",
757 (void *)event->mmap.start,
758 (void *)event->mmap.len,
759 (void *)event->mmap.pgoff,
760 event->mmap.filename);
762 if (thread == NULL || map == NULL) {
763 fprintf(stderr, "problem processing PERF_EVENT_MMAP, bailing out\n");
766 thread__insert_map(thread, map);
770 case PERF_EVENT_COMM: {
771 struct thread *thread = threads__findnew(event->comm.pid);
774 fprintf(stderr, "PERF_EVENT_COMM: %s:%d\n",
775 event->comm.comm, event->comm.pid);
777 if (thread == NULL ||
778 thread__set_comm(thread, event->comm.comm)) {
779 fprintf(stderr, "problem processing PERF_EVENT_COMM, bailing out\n");
786 fprintf(stderr, "skipping unknown header type: %d\n",
792 if (offset + head < stat.st_size)
800 fprintf(stderr, " IP events: %10ld\n", total);
801 fprintf(stderr, " mmap events: %10ld\n", total_mmap);
802 fprintf(stderr, " comm events: %10ld\n", total_comm);
803 fprintf(stderr, " unknown events: %10ld\n", total_unknown);
808 //dsos__fprintf(stdout);
809 threads__fprintf(stdout);
811 std::map<std::string, int>::iterator hi = hist.begin();
813 while (hi != hist.end()) {
814 rev_hist.insert(std::pair<int, std::string>(hi->second, hi->first));
818 std::multimap<int, std::string>::const_iterator ri = rev_hist.begin();
820 while (ri != rev_hist.end()) {
821 printf(" %5.2f %s\n", (100.0 * ri->first)/total, ri->second.c_str());
828 static const char * const report_usage[] = {
829 "perf report [<options>] <command>",
833 static const struct option options[] = {
834 OPT_STRING('i', "input", &input_name, "file",
836 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
837 "dump raw trace in ASCII"),
841 int cmd_report(int argc, const char **argv, const char *prefix)
843 elf_version(EV_CURRENT);
845 page_size = getpagesize();
847 parse_options(argc, argv, options, report_usage, 0);
849 return __cmd_report();