9 #include "util/cache.h"
10 #include "util/rbtree.h"
14 #include "util/parse-options.h"
15 #include "util/parse-events.h"
21 static char const *input_name
= "perf.data";
22 static char *vmlinux
= NULL
;
24 static int show_mask
= SHOW_KERNEL
| SHOW_USER
| SHOW_HV
;
26 static int dump_trace
= 0;
29 static unsigned long page_size
;
30 static unsigned long mmap_window
= 32;
32 const char *perf_event_names
[] = {
33 [PERF_EVENT_MMAP
] = " PERF_EVENT_MMAP",
34 [PERF_EVENT_MUNMAP
] = " PERF_EVENT_MUNMAP",
35 [PERF_EVENT_COMM
] = " PERF_EVENT_COMM",
39 struct perf_event_header header
;
44 struct perf_event_header header
;
49 char filename
[PATH_MAX
];
52 struct perf_event_header header
;
57 typedef union event_union
{
58 struct perf_event_header header
;
60 struct mmap_event mmap
;
61 struct comm_event comm
;
65 struct rb_node rb_node
;
71 static struct symbol
*symbol__new(uint64_t start
, uint64_t len
, const char *name
)
73 struct symbol
*self
= malloc(sizeof(*self
) + strlen(name
) + 1);
77 self
->end
= start
+ len
;
78 strcpy(self
->name
, name
);
84 static void symbol__delete(struct symbol
*self
)
89 static size_t symbol__fprintf(struct symbol
*self
, FILE *fp
)
91 return fprintf(fp
, " %llx-%llx %s\n",
92 self
->start
, self
->end
, self
->name
);
96 struct list_head node
;
101 static struct dso
*dso__new(const char *name
)
103 struct dso
*self
= malloc(sizeof(*self
) + strlen(name
) + 1);
106 strcpy(self
->name
, name
);
107 self
->syms
= RB_ROOT
;
113 static void dso__delete_symbols(struct dso
*self
)
116 struct rb_node
*next
= rb_first(&self
->syms
);
119 pos
= rb_entry(next
, struct symbol
, rb_node
);
120 next
= rb_next(&pos
->rb_node
);
125 static void dso__delete(struct dso
*self
)
127 dso__delete_symbols(self
);
131 static void dso__insert_symbol(struct dso
*self
, struct symbol
*sym
)
133 struct rb_node
**p
= &self
->syms
.rb_node
;
134 struct rb_node
*parent
= NULL
;
135 const uint64_t ip
= sym
->start
;
140 s
= rb_entry(parent
, struct symbol
, rb_node
);
146 rb_link_node(&sym
->rb_node
, parent
, p
);
147 rb_insert_color(&sym
->rb_node
, &self
->syms
);
150 static struct symbol
*dso__find_symbol(struct dso
*self
, uint64_t ip
)
157 n
= self
->syms
.rb_node
;
160 struct symbol
*s
= rb_entry(n
, struct symbol
, rb_node
);
164 else if (ip
> s
->end
)
174 * elf_symtab__for_each_symbol - iterate thru all the symbols
176 * @self: struct elf_symtab instance to iterate
177 * @index: uint32_t index
178 * @sym: GElf_Sym iterator
180 #define elf_symtab__for_each_symbol(syms, nr_syms, index, sym) \
181 for (index = 0, gelf_getsym(syms, index, &sym);\
183 index++, gelf_getsym(syms, index, &sym))
185 static inline uint8_t elf_sym__type(const GElf_Sym
*sym
)
187 return GELF_ST_TYPE(sym
->st_info
);
190 static inline int elf_sym__is_function(const GElf_Sym
*sym
)
192 return elf_sym__type(sym
) == STT_FUNC
&&
194 sym
->st_shndx
!= SHN_UNDEF
&&
198 static inline const char *elf_sym__name(const GElf_Sym
*sym
,
199 const Elf_Data
*symstrs
)
201 return symstrs
->d_buf
+ sym
->st_name
;
204 static Elf_Scn
*elf_section_by_name(Elf
*elf
, GElf_Ehdr
*ep
,
205 GElf_Shdr
*shp
, const char *name
,
211 while ((sec
= elf_nextscn(elf
, sec
)) != NULL
) {
214 gelf_getshdr(sec
, shp
);
215 str
= elf_strptr(elf
, ep
->e_shstrndx
, shp
->sh_name
);
216 if (!strcmp(name
, str
)) {
227 static int dso__load_sym(struct dso
*self
, int fd
, char *name
)
241 elf
= elf_begin(fd
, ELF_C_READ_MMAP
, NULL
);
243 fprintf(stderr
, "%s: cannot read %s ELF file.\n",
248 if (gelf_getehdr(elf
, &ehdr
) == NULL
) {
249 fprintf(stderr
, "%s: cannot get elf header.\n", __func__
);
253 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
, ".symtab", NULL
);
255 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
, ".dynsym", NULL
);
260 syms
= elf_getdata(sec
, NULL
);
264 sec
= elf_getscn(elf
, shdr
.sh_link
);
268 symstrs
= elf_getdata(sec
, NULL
);
272 nr_syms
= shdr
.sh_size
/ shdr
.sh_entsize
;
274 elf_symtab__for_each_symbol(syms
, nr_syms
, index
, sym
) {
277 if (!elf_sym__is_function(&sym
))
280 sec
= elf_getscn(elf
, sym
.st_shndx
);
284 gelf_getshdr(sec
, &shdr
);
285 sym
.st_value
-= shdr
.sh_addr
- shdr
.sh_offset
;
287 f
= symbol__new(sym
.st_value
, sym
.st_size
,
288 elf_sym__name(&sym
, symstrs
));
292 dso__insert_symbol(self
, f
);
304 static int dso__load(struct dso
*self
)
306 int size
= strlen(self
->name
) + sizeof("/usr/lib/debug%s.debug");
307 char *name
= malloc(size
);
319 snprintf(name
, size
, "/usr/lib/debug%s.debug", self
->name
);
322 snprintf(name
, size
, "/usr/lib/debug%s", self
->name
);
324 case 2: /* Sane people */
325 snprintf(name
, size
, "%s", self
->name
);
333 fd
= open(name
, O_RDONLY
);
336 ret
= dso__load_sym(self
, fd
, name
);
340 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
350 static size_t dso__fprintf(struct dso
*self
, FILE *fp
)
352 size_t ret
= fprintf(fp
, "dso: %s\n", self
->name
);
355 for (nd
= rb_first(&self
->syms
); nd
; nd
= rb_next(nd
)) {
356 struct symbol
*pos
= rb_entry(nd
, struct symbol
, rb_node
);
357 ret
+= symbol__fprintf(pos
, fp
);
363 static LIST_HEAD(dsos
);
364 static struct dso
*kernel_dso
;
366 static void dsos__add(struct dso
*dso
)
368 list_add_tail(&dso
->node
, &dsos
);
371 static struct dso
*dsos__find(const char *name
)
375 list_for_each_entry(pos
, &dsos
, node
)
376 if (strcmp(pos
->name
, name
) == 0)
381 static struct dso
*dsos__findnew(const char *name
)
383 struct dso
*dso
= dsos__find(name
);
387 dso
= dso__new(name
);
393 fprintf(stderr
, "Failed to open: %s\n", name
);
398 "Failed to find debug symbols for: %s, maybe install a debug package?\n",
412 static void dsos__fprintf(FILE *fp
)
416 list_for_each_entry(pos
, &dsos
, node
)
417 dso__fprintf(pos
, fp
);
420 static int hex(char ch
)
422 if ((ch
>= '0') && (ch
<= '9'))
424 if ((ch
>= 'a') && (ch
<= 'f'))
425 return ch
- 'a' + 10;
426 if ((ch
>= 'A') && (ch
<= 'F'))
427 return ch
- 'A' + 10;
432 * While we find nice hex chars, build a long_val.
433 * Return number of chars processed.
435 static int hex2long(char *ptr
, unsigned long *long_val
)
441 const int hex_val
= hex(*p
);
446 *long_val
= (*long_val
<< 4) | hex_val
;
453 static int load_kallsyms(void)
455 struct rb_node
*nd
, *prevnd
;
460 kernel_dso
= dso__new("[kernel]");
461 if (kernel_dso
== NULL
)
464 file
= fopen("/proc/kallsyms", "r");
468 while (!feof(file
)) {
474 line_len
= getline(&line
, &n
, file
);
481 line
[--line_len
] = '\0'; /* \n */
483 len
= hex2long(line
, &start
);
486 if (len
+ 2 >= line_len
)
489 symbol_type
= toupper(line
[len
]);
491 * We're interested only in code ('T'ext)
493 if (symbol_type
!= 'T' && symbol_type
!= 'W')
496 * Well fix up the end later, when we have all sorted.
498 sym
= symbol__new(start
, 0xdead, line
+ len
+ 2);
503 dso__insert_symbol(kernel_dso
, sym
);
507 * Now that we have all sorted out, just set the ->end of all
510 prevnd
= rb_first(&kernel_dso
->syms
);
513 goto out_delete_line
;
515 for (nd
= rb_next(prevnd
); nd
; nd
= rb_next(nd
)) {
516 struct symbol
*prev
= rb_entry(prevnd
, struct symbol
, rb_node
),
517 *curr
= rb_entry(nd
, struct symbol
, rb_node
);
519 prev
->end
= curr
->start
- 1;
523 dsos__add(kernel_dso
);
532 dso__delete(kernel_dso
);
536 static int load_kernel(void)
543 fd
= open(vmlinux
, O_RDONLY
);
547 kernel_dso
= dso__new("[kernel]");
551 nr
= dso__load_sym(kernel_dso
, fd
, vmlinux
);
556 dsos__add(kernel_dso
);
562 dso__delete(kernel_dso
);
566 return load_kallsyms();
570 struct list_head node
;
577 static struct map
*map__new(struct mmap_event
*event
)
579 struct map
*self
= malloc(sizeof(*self
));
582 self
->start
= event
->start
;
583 self
->end
= event
->start
+ event
->len
;
584 self
->pgoff
= event
->pgoff
;
586 self
->dso
= dsos__findnew(event
->filename
);
587 if (self
->dso
== NULL
)
598 static const char *thread__name(struct thread
*self
, char *bf
, size_t size
);
601 struct rb_node rb_node
;
602 struct list_head maps
;
607 static const char *thread__name(struct thread
*self
, char *bf
, size_t size
)
612 snprintf(bf
, sizeof(bf
), ":%u", self
->pid
);
616 static struct thread
*thread__new(pid_t pid
)
618 struct thread
*self
= malloc(sizeof(*self
));
623 INIT_LIST_HEAD(&self
->maps
);
629 static int thread__set_comm(struct thread
*self
, const char *comm
)
631 self
->comm
= strdup(comm
);
632 return self
->comm
? 0 : -ENOMEM
;
635 static struct rb_root threads
;
637 static struct thread
*threads__findnew(pid_t pid
)
639 struct rb_node
**p
= &threads
.rb_node
;
640 struct rb_node
*parent
= NULL
;
645 th
= rb_entry(parent
, struct thread
, rb_node
);
656 th
= thread__new(pid
);
658 rb_link_node(&th
->rb_node
, parent
, p
);
659 rb_insert_color(&th
->rb_node
, &threads
);
664 static void thread__insert_map(struct thread
*self
, struct map
*map
)
666 list_add_tail(&map
->node
, &self
->maps
);
669 static struct map
*thread__find_map(struct thread
*self
, uint64_t ip
)
676 list_for_each_entry(pos
, &self
->maps
, node
)
677 if (ip
>= pos
->start
&& ip
<= pos
->end
)
684 * histogram, sorted on item, collects counts
687 static struct rb_root hist
;
690 struct rb_node rb_node
;
692 struct thread
*thread
;
703 hist_entry__cmp(struct hist_entry
*left
, struct hist_entry
*right
)
706 int cmp
= right
->thread
->pid
- left
->thread
->pid
;
711 if (left
->sym
== right
->sym
)
714 ip_l
= left
->sym
? left
->sym
->start
: left
->ip
;
715 ip_r
= right
->sym
? right
->sym
->start
: right
->ip
;
717 return (int64_t)(ip_r
- ip_l
);
721 hist_entry__add(struct thread
*thread
, struct map
*map
, struct dso
*dso
,
722 struct symbol
*sym
, uint64_t ip
, char level
)
724 struct rb_node
**p
= &hist
.rb_node
;
725 struct rb_node
*parent
= NULL
;
726 struct hist_entry
*he
;
727 struct hist_entry entry
= {
740 he
= rb_entry(parent
, struct hist_entry
, rb_node
);
742 cmp
= hist_entry__cmp(&entry
, he
);
755 he
= malloc(sizeof(*he
));
759 rb_link_node(&he
->rb_node
, parent
, p
);
760 rb_insert_color(&he
->rb_node
, &hist
);
766 hist_entry__fprintf(FILE *fp
, struct hist_entry
*self
, uint64_t total_samples
)
772 ret
= fprintf(fp
, "%5.2f%% ",
773 (self
->count
* 100.0) / total_samples
);
775 ret
= fprintf(fp
, "%12d ", self
->count
);
777 ret
+= fprintf(fp
, "%14s [%c] ",
778 thread__name(self
->thread
, bf
, sizeof(bf
)),
782 ret
+= fprintf(fp
, "%#018llx ", (unsigned long long)self
->ip
);
784 if (self
->level
!= '.')
785 ret
+= fprintf(fp
, "%s\n",
786 self
->sym
? self
->sym
->name
: "<unknown>");
788 ret
+= fprintf(fp
, "%s: %s\n",
789 self
->dso
? self
->dso
->name
: "<unknown>",
790 self
->sym
? self
->sym
->name
: "<unknown>");
795 * reverse the map, sort on count.
798 static struct rb_root output_hists
;
800 static void output__insert_entry(struct hist_entry
*he
)
802 struct rb_node
**p
= &output_hists
.rb_node
;
803 struct rb_node
*parent
= NULL
;
804 struct hist_entry
*iter
;
808 iter
= rb_entry(parent
, struct hist_entry
, rb_node
);
810 if (he
->count
> iter
->count
)
816 rb_link_node(&he
->rb_node
, parent
, p
);
817 rb_insert_color(&he
->rb_node
, &output_hists
);
820 static void output__resort(void)
822 struct rb_node
*next
= rb_first(&hist
);
823 struct hist_entry
*n
;
826 n
= rb_entry(next
, struct hist_entry
, rb_node
);
827 next
= rb_next(&n
->rb_node
);
829 rb_erase(&n
->rb_node
, &hist
);
830 output__insert_entry(n
);
834 static size_t output__fprintf(FILE *fp
, uint64_t total_samples
)
836 struct hist_entry
*pos
;
840 for (nd
= rb_first(&output_hists
); nd
; nd
= rb_next(nd
)) {
841 pos
= rb_entry(nd
, struct hist_entry
, rb_node
);
842 ret
+= hist_entry__fprintf(fp
, pos
, total_samples
);
849 static int __cmd_report(void)
851 unsigned long offset
= 0;
852 unsigned long head
= 0;
856 int ret
, rc
= EXIT_FAILURE
;
858 unsigned long total
= 0, total_mmap
= 0, total_comm
= 0, total_unknown
= 0;
860 input
= open(input_name
, O_RDONLY
);
862 perror("failed to open file");
866 ret
= fstat(input
, &stat
);
868 perror("failed to stat file");
873 fprintf(stderr
, "zero-sized file, nothing to do!\n");
877 if (load_kernel() < 0) {
878 perror("failed to open kallsyms");
883 buf
= (char *)mmap(NULL
, page_size
* mmap_window
, PROT_READ
,
884 MAP_SHARED
, input
, offset
);
885 if (buf
== MAP_FAILED
) {
886 perror("failed to mmap file");
891 event
= (event_t
*)(buf
+ head
);
893 size
= event
->header
.size
;
897 if (head
+ event
->header
.size
>= page_size
* mmap_window
) {
898 unsigned long shift
= page_size
* (head
/ page_size
);
901 ret
= munmap(buf
, page_size
* mmap_window
);
909 size
= event
->header
.size
;
913 if (event
->header
.misc
& PERF_EVENT_MISC_OVERFLOW
) {
916 struct dso
*dso
= NULL
;
917 struct thread
*thread
= threads__findnew(event
->ip
.pid
);
918 uint64_t ip
= event
->ip
.ip
;
919 struct map
*map
= NULL
;
922 fprintf(stderr
, "%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
923 (void *)(offset
+ head
),
924 (void *)(long)(event
->header
.size
),
930 if (thread
== NULL
) {
931 fprintf(stderr
, "problem processing %d event, bailing out\n",
936 if (event
->header
.misc
& PERF_EVENT_MISC_KERNEL
) {
942 } else if (event
->header
.misc
& PERF_EVENT_MISC_USER
) {
947 map
= thread__find_map(thread
, ip
);
950 ip
-= map
->start
+ map
->pgoff
;
958 if (show
& show_mask
) {
959 struct symbol
*sym
= dso__find_symbol(dso
, ip
);
961 if (hist_entry__add(thread
, map
, dso
, sym
, ip
, level
)) {
963 "problem incrementing symbol count, bailing out\n");
968 } else switch (event
->header
.type
) {
969 case PERF_EVENT_MMAP
: {
970 struct thread
*thread
= threads__findnew(event
->mmap
.pid
);
971 struct map
*map
= map__new(&event
->mmap
);
974 fprintf(stderr
, "%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n",
975 (void *)(offset
+ head
),
976 (void *)(long)(event
->header
.size
),
977 (void *)(long)event
->mmap
.start
,
978 (void *)(long)event
->mmap
.len
,
979 (void *)(long)event
->mmap
.pgoff
,
980 event
->mmap
.filename
);
982 if (thread
== NULL
|| map
== NULL
) {
983 fprintf(stderr
, "problem processing PERF_EVENT_MMAP, bailing out\n");
986 thread__insert_map(thread
, map
);
990 case PERF_EVENT_COMM
: {
991 struct thread
*thread
= threads__findnew(event
->comm
.pid
);
994 fprintf(stderr
, "%p [%p]: PERF_EVENT_COMM: %s:%d\n",
995 (void *)(offset
+ head
),
996 (void *)(long)(event
->header
.size
),
997 event
->comm
.comm
, event
->comm
.pid
);
999 if (thread
== NULL
||
1000 thread__set_comm(thread
, event
->comm
.comm
)) {
1001 fprintf(stderr
, "problem processing PERF_EVENT_COMM, bailing out\n");
1010 fprintf(stderr
, "%p [%p]: skipping unknown header type: %d\n",
1011 (void *)(offset
+ head
),
1012 (void *)(long)(event
->header
.size
),
1013 event
->header
.type
);
1018 * assume we lost track of the stream, check alignment, and
1019 * increment a single u64 in the hope to catch on again 'soon'.
1022 if (unlikely(head
& 7))
1031 if (offset
+ head
< stat
.st_size
)
1039 fprintf(stderr
, " IP events: %10ld\n", total
);
1040 fprintf(stderr
, " mmap events: %10ld\n", total_mmap
);
1041 fprintf(stderr
, " comm events: %10ld\n", total_comm
);
1042 fprintf(stderr
, " unknown events: %10ld\n", total_unknown
);
1048 dsos__fprintf(stdout
);
1051 output__fprintf(stdout
, total
);
1056 static const char * const report_usage
[] = {
1057 "perf report [<options>] <command>",
1061 static const struct option options
[] = {
1062 OPT_STRING('i', "input", &input_name
, "file",
1064 OPT_BOOLEAN('v', "verbose", &verbose
,
1065 "be more verbose (show symbol address, etc)"),
1066 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
1067 "dump raw trace in ASCII"),
1068 OPT_STRING('k', "vmlinux", &vmlinux
, "file", "vmlinux pathname"),
1072 int cmd_report(int argc
, const char **argv
, const char *prefix
)
1074 elf_version(EV_CURRENT
);
1076 page_size
= getpagesize();
1078 parse_options(argc
, argv
, options
, report_usage
, 0);
1082 return __cmd_report();
This page took 0.07065 seconds and 5 git commands to generate.