23 #include <sys/ioctl.h>
25 #include <sys/prctl.h>
28 #include <sys/types.h>
31 #include <linux/unistd.h>
32 #include <linux/types.h>
34 #include "../../include/linux/perf_counter.h"
35 #include "util/list.h"
36 #include "util/rbtree.h"
42 static char const *input_name = "output.perf";
44 static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
46 static unsigned long page_size;
47 static unsigned long mmap_window = 32;
49 static const char *perf_event_names[] = {
50 [PERF_EVENT_MMAP] = " PERF_EVENT_MMAP",
51 [PERF_EVENT_MUNMAP] = " PERF_EVENT_MUNMAP",
52 [PERF_EVENT_COMM] = " PERF_EVENT_COMM",
56 struct perf_event_header header;
61 struct perf_event_header header;
66 char filename[PATH_MAX];
69 struct perf_event_header header;
74 typedef union event_union {
75 struct perf_event_header header;
77 struct mmap_event mmap;
78 struct comm_event comm;
82 struct list_head node;
89 static struct section *section__new(uint64_t start, uint64_t size,
90 uint64_t offset, char *name)
92 struct section *self = malloc(sizeof(*self) + strlen(name) + 1);
96 self->end = start + size;
97 self->offset = offset;
98 strcpy(self->name, name);
104 static void section__delete(struct section *self)
110 struct rb_node rb_node;
116 static struct symbol *symbol__new(uint64_t start, uint64_t len, const char *name)
118 struct symbol *self = malloc(sizeof(*self) + strlen(name) + 1);
122 self->end = start + len;
123 strcpy(self->name, name);
129 static void symbol__delete(struct symbol *self)
134 static size_t symbol__fprintf(struct symbol *self, FILE *fp)
136 return fprintf(fp, " %lx-%lx %s\n",
137 self->start, self->end, self->name);
141 struct list_head node;
142 struct list_head sections;
147 static struct dso *dso__new(const char *name)
149 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
152 strcpy(self->name, name);
153 INIT_LIST_HEAD(&self->sections);
154 self->syms = RB_ROOT;
160 static void dso__delete_sections(struct dso *self)
162 struct section *pos, *n;
164 list_for_each_entry_safe(pos, n, &self->sections, node)
165 section__delete(pos);
168 static void dso__delete_symbols(struct dso *self)
171 struct rb_node *next = rb_first(&self->syms);
174 pos = rb_entry(next, struct symbol, rb_node);
175 next = rb_next(&pos->rb_node);
180 static void dso__delete(struct dso *self)
182 dso__delete_sections(self);
183 dso__delete_symbols(self);
187 static void dso__insert_symbol(struct dso *self, struct symbol *sym)
189 struct rb_node **p = &self->syms.rb_node;
190 struct rb_node *parent = NULL;
191 const uint64_t ip = sym->start;
196 s = rb_entry(parent, struct symbol, rb_node);
202 rb_link_node(&sym->rb_node, parent, p);
203 rb_insert_color(&sym->rb_node, &self->syms);
206 static struct symbol *dso__find_symbol(struct dso *self, uint64_t ip)
211 struct rb_node *n = self->syms.rb_node;
214 struct symbol *s = rb_entry(n, struct symbol, rb_node);
218 else if (ip > s->end)
228 * elf_symtab__for_each_symbol - iterate thru all the symbols
230 * @self: struct elf_symtab instance to iterate
231 * @index: uint32_t index
232 * @sym: GElf_Sym iterator
234 #define elf_symtab__for_each_symbol(syms, nr_syms, index, sym) \
235 for (index = 0, gelf_getsym(syms, index, &sym);\
237 index++, gelf_getsym(syms, index, &sym))
239 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
241 return GELF_ST_TYPE(sym->st_info);
244 static inline bool elf_sym__is_function(const GElf_Sym *sym)
246 return elf_sym__type(sym) == STT_FUNC &&
248 sym->st_shndx != SHN_UNDEF;
251 static inline const char *elf_sym__name(const GElf_Sym *sym,
252 const Elf_Data *symstrs)
254 return symstrs->d_buf + sym->st_name;
257 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
258 GElf_Shdr *shp, const char *name,
264 while ((sec = elf_nextscn(elf, sec)) != NULL) {
267 gelf_getshdr(sec, shp);
268 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
269 if (!strcmp(name, str)) {
280 static int dso__load(struct dso *self)
282 int fd = open(self->name, O_RDONLY), err = -1;
287 Elf *elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
289 fprintf(stderr, "%s: cannot read %s ELF file.\n",
290 __func__, self->name);
295 if (gelf_getehdr(elf, &ehdr) == NULL) {
296 fprintf(stderr, "%s: cannot get elf header.\n", __func__);
301 Elf_Scn *sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
303 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
308 if (gelf_getshdr(sec, &shdr) == NULL)
311 Elf_Data *syms = elf_getdata(sec, NULL);
315 sec = elf_getscn(elf, shdr.sh_link);
319 Elf_Data *symstrs = elf_getdata(sec, NULL);
323 const uint32_t nr_syms = shdr.sh_size / shdr.sh_entsize;
327 elf_symtab__for_each_symbol(syms, nr_syms, index, sym) {
328 if (!elf_sym__is_function(&sym))
330 struct symbol *f = symbol__new(sym.st_value, sym.st_size,
331 elf_sym__name(&sym, symstrs));
335 dso__insert_symbol(self, f);
346 static size_t dso__fprintf(struct dso *self, FILE *fp)
348 size_t ret = fprintf(fp, "dso: %s\n", self->name);
351 for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
352 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
353 ret += symbol__fprintf(pos, fp);
359 static LIST_HEAD(dsos);
360 static struct dso *kernel_dso;
362 static void dsos__add(struct dso *dso)
364 list_add_tail(&dso->node, &dsos);
367 static struct dso *dsos__find(const char *name)
371 list_for_each_entry(pos, &dsos, node)
372 if (strcmp(pos->name, name) == 0)
377 static struct dso *dsos__findnew(const char *name)
379 struct dso *dso = dsos__find(name);
382 dso = dso__new(name);
383 if (dso != NULL && dso__load(dso) < 0)
396 static void dsos__fprintf(FILE *fp)
400 list_for_each_entry(pos, &dsos, node)
401 dso__fprintf(pos, fp);
404 static int load_kallsyms(void)
406 kernel_dso = dso__new("[kernel]");
407 if (kernel_dso == NULL)
410 FILE *file = fopen("/proc/kallsyms", "r");
418 while (!feof(file)) {
419 unsigned long long start;
422 if (getline(&line, &n, file) < 0)
428 if (sscanf(line, "%llx %c %s", &start, &c, symbf) == 3) {
429 struct symbol *sym = symbol__new(start, 0x1000000, symbf);
434 dso__insert_symbol(kernel_dso, sym);
438 dsos__add(kernel_dso);
444 dso__delete(kernel_dso);
449 struct list_head node;
456 static struct map *map__new(struct mmap_event *event)
458 struct map *self = malloc(sizeof(*self));
461 self->start = event->start;
462 self->end = event->start + event->len;
463 self->pgoff = event->pgoff;
465 self->dso = dsos__findnew(event->filename);
466 if (self->dso == NULL)
475 static size_t map__fprintf(struct map *self, FILE *fp)
477 return fprintf(fp, " %lx-%lx %lx %s\n",
478 self->start, self->end, self->pgoff, self->dso->name);
482 struct rb_node rb_node;
490 static struct symhist *symhist__new(struct symbol *sym, uint64_t ip,
491 struct dso *dso, char level)
493 struct symhist *self = malloc(sizeof(*self));
506 static void symhist__delete(struct symhist *self)
511 static void symhist__inc(struct symhist *self)
516 static size_t symhist__fprintf(struct symhist *self, FILE *fp)
518 size_t ret = fprintf(fp, "%#llx [%c] ", (unsigned long long)self->ip, self->level);
520 if (self->level != '.')
521 ret += fprintf(fp, "%s", self->sym->name);
523 ret += fprintf(fp, "%s: %s",
524 self->dso ? self->dso->name : "<unknown",
525 self->sym ? self->sym->name : "<unknown>");
526 return ret + fprintf(fp, ": %u\n", self->count);
530 struct rb_node rb_node;
531 struct list_head maps;
532 struct rb_root symhists;
537 static struct thread *thread__new(pid_t pid)
539 struct thread *self = malloc(sizeof(*self));
544 INIT_LIST_HEAD(&self->maps);
545 self->symhists = RB_ROOT;
551 static int thread__symbol_incnew(struct thread *self, struct symbol *sym,
552 uint64_t ip, struct dso *dso, char level)
554 struct rb_node **p = &self->symhists.rb_node;
555 struct rb_node *parent = NULL;
560 sh = rb_entry(parent, struct symhist, rb_node);
562 if (sh->sym == sym || ip == sh->ip) {
567 /* Handle unresolved symbols too */
568 const uint64_t start = !sh->sym ? sh->ip : sh->sym->start;
576 sh = symhist__new(sym, ip, dso, level);
579 rb_link_node(&sh->rb_node, parent, p);
580 rb_insert_color(&sh->rb_node, &self->symhists);
584 static int thread__set_comm(struct thread *self, const char *comm)
586 self->comm = strdup(comm);
587 return self->comm ? 0 : -ENOMEM;
590 static size_t thread__maps_fprintf(struct thread *self, FILE *fp)
595 list_for_each_entry(pos, &self->maps, node)
596 ret += map__fprintf(pos, fp);
601 static size_t thread__fprintf(struct thread *self, FILE *fp)
603 int ret = fprintf(fp, "thread: %d %s\n", self->pid, self->comm);
606 for (nd = rb_first(&self->symhists); nd; nd = rb_next(nd)) {
607 struct symhist *pos = rb_entry(nd, struct symhist, rb_node);
608 ret += symhist__fprintf(pos, fp);
614 static struct rb_root threads = RB_ROOT;
616 static struct thread *threads__findnew(pid_t pid)
618 struct rb_node **p = &threads.rb_node;
619 struct rb_node *parent = NULL;
624 th = rb_entry(parent, struct thread, rb_node);
635 th = thread__new(pid);
637 rb_link_node(&th->rb_node, parent, p);
638 rb_insert_color(&th->rb_node, &threads);
643 static void thread__insert_map(struct thread *self, struct map *map)
645 list_add_tail(&map->node, &self->maps);
648 static struct map *thread__find_map(struct thread *self, uint64_t ip)
655 list_for_each_entry(pos, &self->maps, node)
656 if (ip >= pos->start && ip <= pos->end)
662 static void threads__fprintf(FILE *fp)
665 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
666 struct thread *pos = rb_entry(nd, struct thread, rb_node);
667 thread__fprintf(pos, fp);
671 static void display_help(void)
674 "Usage: perf-report [<options>]\n"
675 " -i file --input=<file> # input file\n"
681 static void process_options(int argc, char *argv[])
686 int option_index = 0;
687 /** Options for getopt */
688 static struct option long_options[] = {
689 {"input", required_argument, NULL, 'i'},
690 {"no-user", no_argument, NULL, 'u'},
691 {"no-kernel", no_argument, NULL, 'k'},
692 {"no-hv", no_argument, NULL, 'h'},
695 int c = getopt_long(argc, argv, "+:i:kuh",
696 long_options, &option_index);
701 case 'i': input_name = strdup(optarg); break;
702 case 'k': show_mask &= ~SHOW_KERNEL; break;
703 case 'u': show_mask &= ~SHOW_USER; break;
704 case 'h': show_mask &= ~SHOW_HV; break;
705 default: error = 1; break;
713 int cmd_report(int argc, char **argv)
715 unsigned long offset = 0;
716 unsigned long head = 0;
720 int ret, rc = EXIT_FAILURE;
721 unsigned long total = 0;
723 elf_version(EV_CURRENT);
725 page_size = getpagesize();
727 process_options(argc, argv);
729 input = open(input_name, O_RDONLY);
731 perror("failed to open file");
735 ret = fstat(input, &stat);
737 perror("failed to stat file");
742 fprintf(stderr, "zero-sized file, nothing to do!\n");
746 if (load_kallsyms() < 0) {
747 perror("failed to open kallsyms");
752 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
753 MAP_SHARED, input, offset);
754 if (buf == MAP_FAILED) {
755 perror("failed to mmap file");
760 event = (event_t *)(buf + head);
762 if (head + event->header.size >= page_size * mmap_window) {
763 unsigned long shift = page_size * (head / page_size);
766 ret = munmap(buf, page_size * mmap_window);
775 if (!event->header.size) {
776 fprintf(stderr, "zero-sized event at file offset %ld\n", offset + head);
777 fprintf(stderr, "skipping %ld bytes of events.\n", stat.st_size - offset - head);
781 head += event->header.size;
783 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) {
786 struct dso *dso = NULL;
787 struct thread *thread = threads__findnew(event->ip.pid);
789 if (thread == NULL) {
790 fprintf(stderr, "problem processing %d event, bailing out\n",
795 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
799 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
802 struct map *map = thread__find_map(thread, event->ip.ip);
810 if (show & show_mask) {
811 struct symbol *sym = dso__find_symbol(dso, event->ip.ip);
813 if (thread__symbol_incnew(thread, sym, event->ip.ip,
815 fprintf(stderr, "problem incrementing symbol count, bailing out\n");
820 } else switch (event->header.type) {
821 case PERF_EVENT_MMAP: {
822 struct thread *thread = threads__findnew(event->mmap.pid);
823 struct map *map = map__new(&event->mmap);
825 if (thread == NULL || map == NULL) {
826 fprintf(stderr, "problem processing PERF_EVENT_MMAP, bailing out\n");
829 thread__insert_map(thread, map);
832 case PERF_EVENT_COMM: {
833 struct thread *thread = threads__findnew(event->comm.pid);
835 if (thread == NULL ||
836 thread__set_comm(thread, event->comm.comm)) {
837 fprintf(stderr, "problem processing PERF_EVENT_COMM, bailing out\n");
844 if (offset + head < stat.st_size)
850 //dsos__fprintf(stdout);
851 threads__fprintf(stdout);
853 std::map<std::string, int>::iterator hi = hist.begin();
855 while (hi != hist.end()) {
856 rev_hist.insert(std::pair<int, std::string>(hi->second, hi->first));
860 std::multimap<int, std::string>::const_iterator ri = rev_hist.begin();
862 while (ri != rev_hist.end()) {
863 printf(" %5.2f %s\n", (100.0 * ri->first)/total, ri->second.c_str());