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 list_head node
;
66 struct section
*section__new(uint64_t start
, uint64_t size
,
67 uint64_t offset
, char *name
)
69 struct section
*self
= malloc(sizeof(*self
) + strlen(name
) + 1);
73 self
->end
= start
+ size
;
74 self
->offset
= offset
;
75 strcpy(self
->name
, name
);
81 static void section__delete(struct section
*self
)
87 struct rb_node rb_node
;
93 static struct symbol
*symbol__new(uint64_t start
, uint64_t len
, const char *name
)
95 struct symbol
*self
= malloc(sizeof(*self
) + strlen(name
) + 1);
99 self
->end
= start
+ len
;
100 strcpy(self
->name
, name
);
106 static void symbol__delete(struct symbol
*self
)
111 static size_t symbol__fprintf(struct symbol
*self
, FILE *fp
)
113 return fprintf(fp
, " %lx-%lx %s\n",
114 self
->start
, self
->end
, self
->name
);
118 struct list_head node
;
119 struct list_head sections
;
124 static struct dso
*dso__new(const char *name
)
126 struct dso
*self
= malloc(sizeof(*self
) + strlen(name
) + 1);
129 strcpy(self
->name
, name
);
130 INIT_LIST_HEAD(&self
->sections
);
131 self
->syms
= RB_ROOT
;
137 static void dso__delete_sections(struct dso
*self
)
139 struct section
*pos
, *n
;
141 list_for_each_entry_safe(pos
, n
, &self
->sections
, node
)
142 section__delete(pos
);
145 static void dso__delete_symbols(struct dso
*self
)
148 struct rb_node
*next
= rb_first(&self
->syms
);
151 pos
= rb_entry(next
, struct symbol
, rb_node
);
152 next
= rb_next(&pos
->rb_node
);
157 static void dso__delete(struct dso
*self
)
159 dso__delete_sections(self
);
160 dso__delete_symbols(self
);
164 static void dso__insert_symbol(struct dso
*self
, struct symbol
*sym
)
166 struct rb_node
**p
= &self
->syms
.rb_node
;
167 struct rb_node
*parent
= NULL
;
168 const uint64_t ip
= sym
->start
;
173 s
= rb_entry(parent
, struct symbol
, rb_node
);
179 rb_link_node(&sym
->rb_node
, parent
, p
);
180 rb_insert_color(&sym
->rb_node
, &self
->syms
);
183 static struct symbol
*dso__find_symbol(struct dso
*self
, uint64_t ip
)
188 struct rb_node
*n
= self
->syms
.rb_node
;
191 struct symbol
*s
= rb_entry(n
, struct symbol
, rb_node
);
195 else if (ip
> s
->end
)
205 * elf_symtab__for_each_symbol - iterate thru all the symbols
207 * @self: struct elf_symtab instance to iterate
208 * @index: uint32_t index
209 * @sym: GElf_Sym iterator
211 #define elf_symtab__for_each_symbol(syms, nr_syms, index, sym) \
212 for (index = 0, gelf_getsym(syms, index, &sym);\
214 index++, gelf_getsym(syms, index, &sym))
216 static inline uint8_t elf_sym__type(const GElf_Sym
*sym
)
218 return GELF_ST_TYPE(sym
->st_info
);
221 static inline int elf_sym__is_function(const GElf_Sym
*sym
)
223 return elf_sym__type(sym
) == STT_FUNC
&&
225 sym
->st_shndx
!= SHN_UNDEF
;
228 static inline const char *elf_sym__name(const GElf_Sym
*sym
,
229 const Elf_Data
*symstrs
)
231 return symstrs
->d_buf
+ sym
->st_name
;
234 static Elf_Scn
*elf_section_by_name(Elf
*elf
, GElf_Ehdr
*ep
,
235 GElf_Shdr
*shp
, const char *name
,
241 while ((sec
= elf_nextscn(elf
, sec
)) != NULL
) {
244 gelf_getshdr(sec
, shp
);
245 str
= elf_strptr(elf
, ep
->e_shstrndx
, shp
->sh_name
);
246 if (!strcmp(name
, str
)) {
257 static int dso__load(struct dso
*self
)
259 int fd
= open(self
->name
, O_RDONLY
), err
= -1;
264 Elf
*elf
= elf_begin(fd
, ELF_C_READ_MMAP
, NULL
);
266 fprintf(stderr
, "%s: cannot read %s ELF file.\n",
267 __func__
, self
->name
);
272 if (gelf_getehdr(elf
, &ehdr
) == NULL
) {
273 fprintf(stderr
, "%s: cannot get elf header.\n", __func__
);
278 Elf_Scn
*sec
= elf_section_by_name(elf
, &ehdr
, &shdr
, ".symtab", NULL
);
280 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
, ".dynsym", NULL
);
285 if (gelf_getshdr(sec
, &shdr
) == NULL
)
288 Elf_Data
*syms
= elf_getdata(sec
, NULL
);
292 sec
= elf_getscn(elf
, shdr
.sh_link
);
296 Elf_Data
*symstrs
= elf_getdata(sec
, NULL
);
300 const uint32_t nr_syms
= shdr
.sh_size
/ shdr
.sh_entsize
;
304 elf_symtab__for_each_symbol(syms
, nr_syms
, index
, sym
) {
305 if (!elf_sym__is_function(&sym
))
307 struct symbol
*f
= symbol__new(sym
.st_value
, sym
.st_size
,
308 elf_sym__name(&sym
, symstrs
));
312 dso__insert_symbol(self
, f
);
323 static size_t dso__fprintf(struct dso
*self
, FILE *fp
)
325 size_t ret
= fprintf(fp
, "dso: %s\n", self
->name
);
328 for (nd
= rb_first(&self
->syms
); nd
; nd
= rb_next(nd
)) {
329 struct symbol
*pos
= rb_entry(nd
, struct symbol
, rb_node
);
330 ret
+= symbol__fprintf(pos
, fp
);
336 static LIST_HEAD(dsos
);
337 static struct dso
*kernel_dso
;
339 static void dsos__add(struct dso
*dso
)
341 list_add_tail(&dso
->node
, &dsos
);
344 static struct dso
*dsos__find(const char *name
)
348 list_for_each_entry(pos
, &dsos
, node
)
349 if (strcmp(pos
->name
, name
) == 0)
354 static struct dso
*dsos__findnew(const char *name
)
356 struct dso
*dso
= dsos__find(name
);
359 dso
= dso__new(name
);
360 if (dso
!= NULL
&& dso__load(dso
) < 0)
373 void dsos__fprintf(FILE *fp
)
377 list_for_each_entry(pos
, &dsos
, node
)
378 dso__fprintf(pos
, fp
);
381 static int load_kallsyms(void)
383 kernel_dso
= dso__new("[kernel]");
384 if (kernel_dso
== NULL
)
387 FILE *file
= fopen("/proc/kallsyms", "r");
395 while (!feof(file
)) {
396 unsigned long long start
;
399 if (getline(&line
, &n
, file
) < 0)
405 if (sscanf(line
, "%llx %c %s", &start
, &c
, symbf
) == 3) {
406 struct symbol
*sym
= symbol__new(start
, 0x1000000, symbf
);
411 dso__insert_symbol(kernel_dso
, sym
);
415 dsos__add(kernel_dso
);
421 dso__delete(kernel_dso
);
426 struct list_head node
;
433 static struct map
*map__new(struct mmap_event
*event
)
435 struct map
*self
= malloc(sizeof(*self
));
438 self
->start
= event
->start
;
439 self
->end
= event
->start
+ event
->len
;
440 self
->pgoff
= event
->pgoff
;
442 self
->dso
= dsos__findnew(event
->filename
);
443 if (self
->dso
== NULL
)
452 static size_t map__fprintf(struct map
*self
, FILE *fp
)
454 return fprintf(fp
, " %lx-%lx %lx %s\n",
455 self
->start
, self
->end
, self
->pgoff
, self
->dso
->name
);
459 struct rb_node rb_node
;
467 static struct symhist
*symhist__new(struct symbol
*sym
, uint64_t ip
,
468 struct dso
*dso
, char level
)
470 struct symhist
*self
= malloc(sizeof(*self
));
483 void symhist__delete(struct symhist
*self
)
488 static void symhist__inc(struct symhist
*self
)
493 static size_t symhist__fprintf(struct symhist
*self
, FILE *fp
)
495 size_t ret
= fprintf(fp
, "%#llx [%c] ", (unsigned long long)self
->ip
, self
->level
);
497 if (self
->level
!= '.')
498 ret
+= fprintf(fp
, "%s", self
->sym
? self
->sym
->name
: "<unknown>");
500 ret
+= fprintf(fp
, "%s: %s",
501 self
->dso
? self
->dso
->name
: "<unknown",
502 self
->sym
? self
->sym
->name
: "<unknown>");
503 return ret
+ fprintf(fp
, ": %u\n", self
->count
);
507 struct rb_node rb_node
;
508 struct list_head maps
;
509 struct rb_root symhists
;
514 static struct thread
*thread__new(pid_t pid
)
516 struct thread
*self
= malloc(sizeof(*self
));
521 INIT_LIST_HEAD(&self
->maps
);
522 self
->symhists
= RB_ROOT
;
528 static int thread__symbol_incnew(struct thread
*self
, struct symbol
*sym
,
529 uint64_t ip
, struct dso
*dso
, char level
)
531 struct rb_node
**p
= &self
->symhists
.rb_node
;
532 struct rb_node
*parent
= NULL
;
537 sh
= rb_entry(parent
, struct symhist
, rb_node
);
539 if (sh
->sym
== sym
|| ip
== sh
->ip
) {
544 /* Handle unresolved symbols too */
545 const uint64_t start
= !sh
->sym
? sh
->ip
: sh
->sym
->start
;
553 sh
= symhist__new(sym
, ip
, dso
, level
);
556 rb_link_node(&sh
->rb_node
, parent
, p
);
557 rb_insert_color(&sh
->rb_node
, &self
->symhists
);
561 static int thread__set_comm(struct thread
*self
, const char *comm
)
563 self
->comm
= strdup(comm
);
564 return self
->comm
? 0 : -ENOMEM
;
567 size_t thread__maps_fprintf(struct thread
*self
, FILE *fp
)
572 list_for_each_entry(pos
, &self
->maps
, node
)
573 ret
+= map__fprintf(pos
, fp
);
578 static size_t thread__fprintf(struct thread
*self
, FILE *fp
)
580 int ret
= fprintf(fp
, "thread: %d %s\n", self
->pid
, self
->comm
);
583 for (nd
= rb_first(&self
->symhists
); nd
; nd
= rb_next(nd
)) {
584 struct symhist
*pos
= rb_entry(nd
, struct symhist
, rb_node
);
585 ret
+= symhist__fprintf(pos
, fp
);
591 static struct rb_root threads
= RB_ROOT
;
593 static struct thread
*threads__findnew(pid_t pid
)
595 struct rb_node
**p
= &threads
.rb_node
;
596 struct rb_node
*parent
= NULL
;
601 th
= rb_entry(parent
, struct thread
, rb_node
);
612 th
= thread__new(pid
);
614 rb_link_node(&th
->rb_node
, parent
, p
);
615 rb_insert_color(&th
->rb_node
, &threads
);
620 static void thread__insert_map(struct thread
*self
, struct map
*map
)
622 list_add_tail(&map
->node
, &self
->maps
);
625 static struct map
*thread__find_map(struct thread
*self
, uint64_t ip
)
632 list_for_each_entry(pos
, &self
->maps
, node
)
633 if (ip
>= pos
->start
&& ip
<= pos
->end
)
639 static void threads__fprintf(FILE *fp
)
642 for (nd
= rb_first(&threads
); nd
; nd
= rb_next(nd
)) {
643 struct thread
*pos
= rb_entry(nd
, struct thread
, rb_node
);
644 thread__fprintf(pos
, fp
);
648 static int __cmd_report(void)
650 unsigned long offset
= 0;
651 unsigned long head
= 0;
655 int ret
, rc
= EXIT_FAILURE
;
656 unsigned long total
= 0;
658 input
= open(input_name
, O_RDONLY
);
660 perror("failed to open file");
664 ret
= fstat(input
, &stat
);
666 perror("failed to stat file");
671 fprintf(stderr
, "zero-sized file, nothing to do!\n");
675 if (load_kallsyms() < 0) {
676 perror("failed to open kallsyms");
681 buf
= (char *)mmap(NULL
, page_size
* mmap_window
, PROT_READ
,
682 MAP_SHARED
, input
, offset
);
683 if (buf
== MAP_FAILED
) {
684 perror("failed to mmap file");
689 event
= (event_t
*)(buf
+ head
);
691 if (head
+ event
->header
.size
>= page_size
* mmap_window
) {
692 unsigned long shift
= page_size
* (head
/ page_size
);
695 ret
= munmap(buf
, page_size
* mmap_window
);
704 if (!event
->header
.size
) {
705 fprintf(stderr
, "zero-sized event at file offset %ld\n", offset
+ head
);
706 fprintf(stderr
, "skipping %ld bytes of events.\n", stat
.st_size
- offset
- head
);
710 head
+= event
->header
.size
;
712 if (event
->header
.misc
& PERF_EVENT_MISC_OVERFLOW
) {
715 struct dso
*dso
= NULL
;
716 struct thread
*thread
= threads__findnew(event
->ip
.pid
);
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
, event
->ip
.ip
);
739 if (show
& show_mask
) {
740 struct symbol
*sym
= dso__find_symbol(dso
, event
->ip
.ip
);
742 if (thread__symbol_incnew(thread
, sym
, event
->ip
.ip
,
744 fprintf(stderr
, "problem incrementing symbol count, bailing out\n");
749 } else switch (event
->header
.type
) {
750 case PERF_EVENT_MMAP
: {
751 struct thread
*thread
= threads__findnew(event
->mmap
.pid
);
752 struct map
*map
= map__new(&event
->mmap
);
754 if (thread
== NULL
|| map
== NULL
) {
755 fprintf(stderr
, "problem processing PERF_EVENT_MMAP, bailing out\n");
758 thread__insert_map(thread
, map
);
761 case PERF_EVENT_COMM
: {
762 struct thread
*thread
= threads__findnew(event
->comm
.pid
);
764 if (thread
== NULL
||
765 thread__set_comm(thread
, event
->comm
.comm
)) {
766 fprintf(stderr
, "problem processing PERF_EVENT_COMM, bailing out\n");
773 if (offset
+ head
< stat
.st_size
)
779 //dsos__fprintf(stdout);
780 threads__fprintf(stdout
);
782 std::map
<std::string
, int>::iterator hi
= hist
.begin();
784 while (hi
!= hist
.end()) {
785 rev_hist
.insert(std::pair
<int, std::string
>(hi
->second
, hi
->first
));
789 std::multimap
<int, std::string
>::const_iterator ri
= rev_hist
.begin();
791 while (ri
!= rev_hist
.end()) {
792 printf(" %5.2f %s\n", (100.0 * ri
->first
)/total
, ri
->second
.c_str());
799 static const char * const report_usage
[] = {
800 "perf report [<options>] <command>",
804 static const struct option options
[] = {
805 OPT_STRING('i', "input", &input_name
, "file",
810 int cmd_report(int argc
, const char **argv
, const char *prefix
)
812 elf_version(EV_CURRENT
);
814 page_size
= getpagesize();
816 parse_options(argc
, argv
, options
, report_usage
, 0);
818 return __cmd_report();