9 #include "util/rbtree.h"
13 #include "util/parse-options.h"
14 #include "util/parse-events.h"
20 static char const *input_name
= "output.perf";
22 static int show_mask
= SHOW_KERNEL
| SHOW_USER
| SHOW_HV
;
24 static int dump_trace
= 0;
26 static unsigned long page_size
;
27 static unsigned long mmap_window
= 32;
29 const char *perf_event_names
[] = {
30 [PERF_EVENT_MMAP
] = " PERF_EVENT_MMAP",
31 [PERF_EVENT_MUNMAP
] = " PERF_EVENT_MUNMAP",
32 [PERF_EVENT_COMM
] = " PERF_EVENT_COMM",
36 struct perf_event_header header
;
41 struct perf_event_header header
;
46 char filename
[PATH_MAX
];
49 struct perf_event_header header
;
54 typedef union event_union
{
55 struct perf_event_header header
;
57 struct mmap_event mmap
;
58 struct comm_event comm
;
62 struct rb_node rb_node
;
68 static struct symbol
*symbol__new(uint64_t start
, uint64_t len
, const char *name
)
70 struct symbol
*self
= malloc(sizeof(*self
) + strlen(name
) + 1);
74 self
->end
= start
+ len
;
75 strcpy(self
->name
, name
);
81 static void symbol__delete(struct symbol
*self
)
86 static size_t symbol__fprintf(struct symbol
*self
, FILE *fp
)
88 return fprintf(fp
, " %lx-%lx %s\n",
89 self
->start
, self
->end
, self
->name
);
93 struct list_head node
;
98 static struct dso
*dso__new(const char *name
)
100 struct dso
*self
= malloc(sizeof(*self
) + strlen(name
) + 1);
103 strcpy(self
->name
, name
);
104 self
->syms
= RB_ROOT
;
110 static void dso__delete_symbols(struct dso
*self
)
113 struct rb_node
*next
= rb_first(&self
->syms
);
116 pos
= rb_entry(next
, struct symbol
, rb_node
);
117 next
= rb_next(&pos
->rb_node
);
122 static void dso__delete(struct dso
*self
)
124 dso__delete_symbols(self
);
128 static void dso__insert_symbol(struct dso
*self
, struct symbol
*sym
)
130 struct rb_node
**p
= &self
->syms
.rb_node
;
131 struct rb_node
*parent
= NULL
;
132 const uint64_t ip
= sym
->start
;
137 s
= rb_entry(parent
, struct symbol
, rb_node
);
143 rb_link_node(&sym
->rb_node
, parent
, p
);
144 rb_insert_color(&sym
->rb_node
, &self
->syms
);
147 static struct symbol
*dso__find_symbol(struct dso
*self
, uint64_t ip
)
152 struct rb_node
*n
= self
->syms
.rb_node
;
155 struct symbol
*s
= rb_entry(n
, struct symbol
, rb_node
);
159 else if (ip
> s
->end
)
169 * elf_symtab__for_each_symbol - iterate thru all the symbols
171 * @self: struct elf_symtab instance to iterate
172 * @index: uint32_t index
173 * @sym: GElf_Sym iterator
175 #define elf_symtab__for_each_symbol(syms, nr_syms, index, sym) \
176 for (index = 0, gelf_getsym(syms, index, &sym);\
178 index++, gelf_getsym(syms, index, &sym))
180 static inline uint8_t elf_sym__type(const GElf_Sym
*sym
)
182 return GELF_ST_TYPE(sym
->st_info
);
185 static inline int elf_sym__is_function(const GElf_Sym
*sym
)
187 return elf_sym__type(sym
) == STT_FUNC
&&
189 sym
->st_shndx
!= SHN_UNDEF
;
192 static inline const char *elf_sym__name(const GElf_Sym
*sym
,
193 const Elf_Data
*symstrs
)
195 return symstrs
->d_buf
+ sym
->st_name
;
198 static Elf_Scn
*elf_section_by_name(Elf
*elf
, GElf_Ehdr
*ep
,
199 GElf_Shdr
*shp
, const char *name
,
205 while ((sec
= elf_nextscn(elf
, sec
)) != NULL
) {
208 gelf_getshdr(sec
, shp
);
209 str
= elf_strptr(elf
, ep
->e_shstrndx
, shp
->sh_name
);
210 if (!strcmp(name
, str
)) {
221 static int dso__load(struct dso
*self
)
223 int fd
= open(self
->name
, O_RDONLY
), err
= -1;
228 Elf
*elf
= elf_begin(fd
, ELF_C_READ_MMAP
, NULL
);
230 fprintf(stderr
, "%s: cannot read %s ELF file.\n",
231 __func__
, self
->name
);
236 if (gelf_getehdr(elf
, &ehdr
) == NULL
) {
237 fprintf(stderr
, "%s: cannot get elf header.\n", __func__
);
242 Elf_Scn
*sec
= elf_section_by_name(elf
, &ehdr
, &shdr
, ".symtab", NULL
);
244 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
, ".dynsym", NULL
);
249 Elf_Data
*syms
= elf_getdata(sec
, NULL
);
253 sec
= elf_getscn(elf
, shdr
.sh_link
);
257 Elf_Data
*symstrs
= elf_getdata(sec
, NULL
);
261 const uint32_t nr_syms
= shdr
.sh_size
/ shdr
.sh_entsize
;
265 elf_symtab__for_each_symbol(syms
, nr_syms
, index
, sym
) {
268 if (!elf_sym__is_function(&sym
))
271 sec
= elf_getscn(elf
, sym
.st_shndx
);
275 gelf_getshdr(sec
, &shdr
);
276 sym
.st_value
-= shdr
.sh_addr
- shdr
.sh_offset
;
278 f
= symbol__new(sym
.st_value
, sym
.st_size
,
279 elf_sym__name(&sym
, symstrs
));
283 dso__insert_symbol(self
, f
);
294 static size_t dso__fprintf(struct dso
*self
, FILE *fp
)
296 size_t ret
= fprintf(fp
, "dso: %s\n", self
->name
);
299 for (nd
= rb_first(&self
->syms
); nd
; nd
= rb_next(nd
)) {
300 struct symbol
*pos
= rb_entry(nd
, struct symbol
, rb_node
);
301 ret
+= symbol__fprintf(pos
, fp
);
307 static LIST_HEAD(dsos
);
308 static struct dso
*kernel_dso
;
310 static void dsos__add(struct dso
*dso
)
312 list_add_tail(&dso
->node
, &dsos
);
315 static struct dso
*dsos__find(const char *name
)
319 list_for_each_entry(pos
, &dsos
, node
)
320 if (strcmp(pos
->name
, name
) == 0)
325 static struct dso
*dsos__findnew(const char *name
)
327 struct dso
*dso
= dsos__find(name
);
330 dso
= dso__new(name
);
331 if (dso
!= NULL
&& dso__load(dso
) < 0)
344 void dsos__fprintf(FILE *fp
)
348 list_for_each_entry(pos
, &dsos
, node
)
349 dso__fprintf(pos
, fp
);
352 static int hex(char ch
)
354 if ((ch
>= '0') && (ch
<= '9'))
356 if ((ch
>= 'a') && (ch
<= 'f'))
357 return ch
- 'a' + 10;
358 if ((ch
>= 'A') && (ch
<= 'F'))
359 return ch
- 'A' + 10;
364 * While we find nice hex chars, build a long_val.
365 * Return number of chars processed.
367 int hex2long(char *ptr
, unsigned long *long_val
)
373 const int hex_val
= hex(*p
);
378 *long_val
= (*long_val
<< 4) | hex_val
;
385 static int load_kallsyms(void)
387 struct rb_node
*nd
, *prevnd
;
392 kernel_dso
= dso__new("[kernel]");
393 if (kernel_dso
== NULL
)
396 file
= fopen("/proc/kallsyms", "r");
400 while (!feof(file
)) {
406 line_len
= getline(&line
, &n
, file
);
413 line
[--line_len
] = '\0'; /* \n */
415 len
= hex2long(line
, &start
);
418 if (len
+ 2 >= line_len
)
421 symbol_type
= toupper(line
[len
]);
423 * We're interested only in code ('T'ext)
425 if (symbol_type
!= 'T' && symbol_type
!= 'W')
428 * Well fix up the end later, when we have all sorted.
430 sym
= symbol__new(start
, 0xdead, line
+ len
+ 2);
435 dso__insert_symbol(kernel_dso
, sym
);
439 * Now that we have all sorted out, just set the ->end of all
442 prevnd
= rb_first(&kernel_dso
->syms
);
445 goto out_delete_line
;
447 for (nd
= rb_next(prevnd
); nd
; nd
= rb_next(nd
)) {
448 struct symbol
*prev
= rb_entry(prevnd
, struct symbol
, rb_node
),
449 *curr
= rb_entry(nd
, struct symbol
, rb_node
);
451 prev
->end
= curr
->start
- 1;
455 dsos__add(kernel_dso
);
464 dso__delete(kernel_dso
);
469 struct list_head node
;
476 static struct map
*map__new(struct mmap_event
*event
)
478 struct map
*self
= malloc(sizeof(*self
));
481 self
->start
= event
->start
;
482 self
->end
= event
->start
+ event
->len
;
483 self
->pgoff
= event
->pgoff
;
485 self
->dso
= dsos__findnew(event
->filename
);
486 if (self
->dso
== NULL
)
495 static size_t map__fprintf(struct map
*self
, FILE *fp
)
497 return fprintf(fp
, " %lx-%lx %lx %s\n",
498 self
->start
, self
->end
, self
->pgoff
, self
->dso
->name
);
503 static const char *thread__name(struct thread
*self
, char *bf
, size_t size
);
506 struct rb_node rb_node
;
509 struct thread
*thread
;
515 static struct symhist
*symhist__new(struct symbol
*sym
, uint64_t ip
,
516 struct thread
*thread
, struct dso
*dso
,
519 struct symhist
*self
= malloc(sizeof(*self
));
523 self
->thread
= thread
;
533 void symhist__delete(struct symhist
*self
)
538 static void symhist__inc(struct symhist
*self
)
544 symhist__fprintf(struct symhist
*self
, uint64_t total_samples
, FILE *fp
)
550 ret
= fprintf(fp
, "%5.2f", (self
->count
* 100.0) / total_samples
);
552 ret
= fprintf(fp
, "%12d", self
->count
);
554 ret
+= fprintf(fp
, "%14s [%c] %#018llx ",
555 thread__name(self
->thread
, bf
, sizeof(bf
)),
556 self
->level
, (unsigned long long)self
->ip
);
558 if (self
->level
!= '.')
559 ret
+= fprintf(fp
, "%s\n",
560 self
->sym
? self
->sym
->name
: "<unknown>");
562 ret
+= fprintf(fp
, "%s: %s\n",
563 self
->dso
? self
->dso
->name
: "<unknown>",
564 self
->sym
? self
->sym
->name
: "<unknown>");
569 struct rb_node rb_node
;
570 struct list_head maps
;
571 struct rb_root symhists
;
576 static const char *thread__name(struct thread
*self
, char *bf
, size_t size
)
581 snprintf(bf
, sizeof(bf
), ":%u", self
->pid
);
585 static struct thread
*thread__new(pid_t pid
)
587 struct thread
*self
= malloc(sizeof(*self
));
592 INIT_LIST_HEAD(&self
->maps
);
593 self
->symhists
= RB_ROOT
;
599 static int thread__symbol_incnew(struct thread
*self
, struct symbol
*sym
,
600 uint64_t ip
, struct dso
*dso
, char level
)
602 struct rb_node
**p
= &self
->symhists
.rb_node
;
603 struct rb_node
*parent
= NULL
;
608 sh
= rb_entry(parent
, struct symhist
, rb_node
);
610 if (sh
->sym
== sym
|| ip
== sh
->ip
) {
615 /* Handle unresolved symbols too */
616 const uint64_t start
= !sh
->sym
? sh
->ip
: sh
->sym
->start
;
624 sh
= symhist__new(sym
, ip
, self
, dso
, level
);
627 rb_link_node(&sh
->rb_node
, parent
, p
);
628 rb_insert_color(&sh
->rb_node
, &self
->symhists
);
632 static int thread__set_comm(struct thread
*self
, const char *comm
)
634 self
->comm
= strdup(comm
);
635 return self
->comm
? 0 : -ENOMEM
;
638 size_t thread__maps_fprintf(struct thread
*self
, FILE *fp
)
643 list_for_each_entry(pos
, &self
->maps
, node
)
644 ret
+= map__fprintf(pos
, fp
);
649 static size_t thread__fprintf(struct thread
*self
, FILE *fp
)
651 int ret
= fprintf(fp
, "thread: %d %s\n", self
->pid
, self
->comm
);
654 for (nd
= rb_first(&self
->symhists
); nd
; nd
= rb_next(nd
)) {
655 struct symhist
*pos
= rb_entry(nd
, struct symhist
, rb_node
);
656 ret
+= symhist__fprintf(pos
, 0, fp
);
662 static struct rb_root threads
= RB_ROOT
;
664 static struct thread
*threads__findnew(pid_t pid
)
666 struct rb_node
**p
= &threads
.rb_node
;
667 struct rb_node
*parent
= NULL
;
672 th
= rb_entry(parent
, struct thread
, rb_node
);
683 th
= thread__new(pid
);
685 rb_link_node(&th
->rb_node
, parent
, p
);
686 rb_insert_color(&th
->rb_node
, &threads
);
691 static void thread__insert_map(struct thread
*self
, struct map
*map
)
693 list_add_tail(&map
->node
, &self
->maps
);
696 static struct map
*thread__find_map(struct thread
*self
, uint64_t ip
)
703 list_for_each_entry(pos
, &self
->maps
, node
)
704 if (ip
>= pos
->start
&& ip
<= pos
->end
)
710 void threads__fprintf(FILE *fp
)
713 for (nd
= rb_first(&threads
); nd
; nd
= rb_next(nd
)) {
714 struct thread
*pos
= rb_entry(nd
, struct thread
, rb_node
);
715 thread__fprintf(pos
, fp
);
719 static struct rb_root global_symhists
= RB_ROOT
;
721 static void threads__insert_symhist(struct symhist
*sh
)
723 struct rb_node
**p
= &global_symhists
.rb_node
;
724 struct rb_node
*parent
= NULL
;
725 struct symhist
*iter
;
729 iter
= rb_entry(parent
, struct symhist
, rb_node
);
732 if (sh
->count
> iter
->count
)
738 rb_link_node(&sh
->rb_node
, parent
, p
);
739 rb_insert_color(&sh
->rb_node
, &global_symhists
);
742 static void threads__sort_symhists(void)
746 for (nd
= rb_first(&threads
); nd
; nd
= rb_next(nd
)) {
747 struct thread
*thread
= rb_entry(nd
, struct thread
, rb_node
);
748 struct rb_node
*next
= rb_first(&thread
->symhists
);
751 struct symhist
*n
= rb_entry(next
, struct symhist
,
753 next
= rb_next(&n
->rb_node
);
754 rb_erase(&n
->rb_node
, &thread
->symhists
);
755 threads__insert_symhist(n
);
761 static size_t threads__symhists_fprintf(uint64_t total_samples
, FILE *fp
)
766 for (nd
= rb_first(&global_symhists
); nd
; nd
= rb_next(nd
)) {
767 struct symhist
*pos
= rb_entry(nd
, struct symhist
, rb_node
);
768 ret
+= symhist__fprintf(pos
, total_samples
, fp
);
774 static int __cmd_report(void)
776 unsigned long offset
= 0;
777 unsigned long head
= 0;
781 int ret
, rc
= EXIT_FAILURE
;
783 unsigned long total
= 0, total_mmap
= 0, total_comm
= 0, total_unknown
= 0;
785 input
= open(input_name
, O_RDONLY
);
787 perror("failed to open file");
791 ret
= fstat(input
, &stat
);
793 perror("failed to stat file");
798 fprintf(stderr
, "zero-sized file, nothing to do!\n");
802 if (load_kallsyms() < 0) {
803 perror("failed to open kallsyms");
808 buf
= (char *)mmap(NULL
, page_size
* mmap_window
, PROT_READ
,
809 MAP_SHARED
, input
, offset
);
810 if (buf
== MAP_FAILED
) {
811 perror("failed to mmap file");
816 event
= (event_t
*)(buf
+ head
);
818 size
= event
->header
.size
;
822 if (head
+ event
->header
.size
>= page_size
* mmap_window
) {
823 unsigned long shift
= page_size
* (head
/ page_size
);
826 ret
= munmap(buf
, page_size
* mmap_window
);
834 size
= event
->header
.size
;
838 if (event
->header
.misc
& PERF_EVENT_MISC_OVERFLOW
) {
841 struct dso
*dso
= NULL
;
842 struct thread
*thread
= threads__findnew(event
->ip
.pid
);
843 uint64_t ip
= event
->ip
.ip
;
846 fprintf(stderr
, "%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
847 (void *)(offset
+ head
),
848 (void *)(long)(event
->header
.size
),
851 (void *)event
->ip
.ip
);
854 if (thread
== NULL
) {
855 fprintf(stderr
, "problem processing %d event, bailing out\n",
860 if (event
->header
.misc
& PERF_EVENT_MISC_KERNEL
) {
864 } else if (event
->header
.misc
& PERF_EVENT_MISC_USER
) {
867 struct map
*map
= thread__find_map(thread
, ip
);
870 ip
-= map
->start
+ map
->pgoff
;
877 if (show
& show_mask
) {
878 struct symbol
*sym
= dso__find_symbol(dso
, ip
);
880 if (thread__symbol_incnew(thread
, sym
, ip
, dso
, level
)) {
881 fprintf(stderr
, "problem incrementing symbol count, bailing out\n");
886 } else switch (event
->header
.type
) {
887 case PERF_EVENT_MMAP
: {
888 struct thread
*thread
= threads__findnew(event
->mmap
.pid
);
889 struct map
*map
= map__new(&event
->mmap
);
892 fprintf(stderr
, "%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n",
893 (void *)(offset
+ head
),
894 (void *)(long)(event
->header
.size
),
895 (void *)event
->mmap
.start
,
896 (void *)event
->mmap
.len
,
897 (void *)event
->mmap
.pgoff
,
898 event
->mmap
.filename
);
900 if (thread
== NULL
|| map
== NULL
) {
901 fprintf(stderr
, "problem processing PERF_EVENT_MMAP, bailing out\n");
904 thread__insert_map(thread
, map
);
908 case PERF_EVENT_COMM
: {
909 struct thread
*thread
= threads__findnew(event
->comm
.pid
);
912 fprintf(stderr
, "%p [%p]: PERF_EVENT_COMM: %s:%d\n",
913 (void *)(offset
+ head
),
914 (void *)(long)(event
->header
.size
),
915 event
->comm
.comm
, event
->comm
.pid
);
917 if (thread
== NULL
||
918 thread__set_comm(thread
, event
->comm
.comm
)) {
919 fprintf(stderr
, "problem processing PERF_EVENT_COMM, bailing out\n");
927 fprintf(stderr
, "%p [%p]: skipping unknown header type: %d\n",
928 (void *)(offset
+ head
),
929 (void *)(long)(event
->header
.size
),
934 * assume we lost track of the stream, check alignment, and
935 * increment a single u64 in the hope to catch on again 'soon'.
938 if (unlikely(head
& 7))
947 if (offset
+ head
< stat
.st_size
)
955 fprintf(stderr
, " IP events: %10ld\n", total
);
956 fprintf(stderr
, " mmap events: %10ld\n", total_mmap
);
957 fprintf(stderr
, " comm events: %10ld\n", total_comm
);
958 fprintf(stderr
, " unknown events: %10ld\n", total_unknown
);
963 threads__sort_symhists();
964 threads__symhists_fprintf(total
, stdout
);
969 static const char * const report_usage
[] = {
970 "perf report [<options>] <command>",
974 static const struct option options
[] = {
975 OPT_STRING('i', "input", &input_name
, "file",
977 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
978 "dump raw trace in ASCII"),
982 int cmd_report(int argc
, const char **argv
, const char *prefix
)
984 elf_version(EV_CURRENT
);
986 page_size
= getpagesize();
988 parse_options(argc
, argv
, options
, report_usage
, 0);
990 return __cmd_report();