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"
41 static char const *input_name
= "output.perf";
43 static int show_mask
= SHOW_KERNEL
| SHOW_USER
| SHOW_HV
;
45 static unsigned long page_size
;
46 static unsigned long mmap_window
= 32;
48 static const char *perf_event_names
[] = {
49 [PERF_EVENT_MMAP
] = " PERF_EVENT_MMAP",
50 [PERF_EVENT_MUNMAP
] = " PERF_EVENT_MUNMAP",
51 [PERF_EVENT_COMM
] = " PERF_EVENT_COMM",
55 struct perf_event_header header
;
60 struct perf_event_header header
;
65 char filename
[PATH_MAX
];
68 struct perf_event_header header
;
73 typedef union event_union
{
74 struct perf_event_header header
;
76 struct mmap_event mmap
;
77 struct comm_event comm
;
81 struct list_head node
;
88 static struct section
*section__new(uint64_t start
, uint64_t size
,
89 uint64_t offset
, char *name
)
91 struct section
*self
= malloc(sizeof(*self
) + strlen(name
) + 1);
95 self
->end
= start
+ size
;
96 self
->offset
= offset
;
97 strcpy(self
->name
, name
);
103 static void section__delete(struct section
*self
)
109 struct list_head node
;
115 static struct symbol
*symbol__new(uint64_t start
, uint64_t len
, const char *name
)
117 struct symbol
*self
= malloc(sizeof(*self
) + strlen(name
) + 1);
121 self
->end
= start
+ len
;
122 strcpy(self
->name
, name
);
128 static void symbol__delete(struct symbol
*self
)
133 static size_t symbol__fprintf(struct symbol
*self
, FILE *fp
)
135 return fprintf(fp
, " %lx-%lx %s\n",
136 self
->start
, self
->end
, self
->name
);
140 struct list_head node
;
141 struct list_head sections
;
142 struct list_head syms
;
146 static struct dso
*dso__new(const char *name
)
148 struct dso
*self
= malloc(sizeof(*self
) + strlen(name
) + 1);
151 strcpy(self
->name
, name
);
152 INIT_LIST_HEAD(&self
->sections
);
153 INIT_LIST_HEAD(&self
->syms
);
159 static void dso__delete_sections(struct dso
*self
)
161 struct section
*pos
, *n
;
163 list_for_each_entry_safe(pos
, n
, &self
->sections
, node
)
164 section__delete(pos
);
167 static void dso__delete_symbols(struct dso
*self
)
169 struct symbol
*pos
, *n
;
171 list_for_each_entry_safe(pos
, n
, &self
->syms
, node
)
175 static void dso__delete(struct dso
*self
)
177 dso__delete_sections(self
);
178 dso__delete_symbols(self
);
182 static void dso__insert_symbol(struct dso
*self
, struct symbol
*sym
)
184 list_add_tail(&sym
->node
, &self
->syms
);
187 static struct symbol
*dso__find_symbol(struct dso
*self
, uint64_t ip
)
194 list_for_each_entry(pos
, &self
->syms
, node
)
195 if (ip
>= pos
->start
&& ip
<= pos
->end
)
202 * elf_symtab__for_each_symbol - iterate thru all the symbols
204 * @self: struct elf_symtab instance to iterate
205 * @index: uint32_t index
206 * @sym: GElf_Sym iterator
208 #define elf_symtab__for_each_symbol(syms, nr_syms, index, sym) \
209 for (index = 0, gelf_getsym(syms, index, &sym);\
211 index++, gelf_getsym(syms, index, &sym))
213 static inline uint8_t elf_sym__type(const GElf_Sym
*sym
)
215 return GELF_ST_TYPE(sym
->st_info
);
218 static inline bool elf_sym__is_function(const GElf_Sym
*sym
)
220 return elf_sym__type(sym
) == STT_FUNC
&&
222 sym
->st_shndx
!= SHN_UNDEF
;
225 static inline const char *elf_sym__name(const GElf_Sym
*sym
,
226 const Elf_Data
*symstrs
)
228 return symstrs
->d_buf
+ sym
->st_name
;
231 static Elf_Scn
*elf_section_by_name(Elf
*elf
, GElf_Ehdr
*ep
,
232 GElf_Shdr
*shp
, const char *name
,
238 while ((sec
= elf_nextscn(elf
, sec
)) != NULL
) {
241 gelf_getshdr(sec
, shp
);
242 str
= elf_strptr(elf
, ep
->e_shstrndx
, shp
->sh_name
);
243 if (!strcmp(name
, str
)) {
254 static int dso__load(struct dso
*self
)
256 int fd
= open(self
->name
, O_RDONLY
), err
= -1;
261 Elf
*elf
= elf_begin(fd
, ELF_C_READ_MMAP
, NULL
);
263 fprintf(stderr
, "%s: cannot read %s ELF file.\n",
264 __func__
, self
->name
);
269 if (gelf_getehdr(elf
, &ehdr
) == NULL
) {
270 fprintf(stderr
, "%s: cannot get elf header.\n", __func__
);
275 Elf_Scn
*sec
= elf_section_by_name(elf
, &ehdr
, &shdr
, ".symtab", NULL
);
277 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
, ".dynsym", NULL
);
282 if (gelf_getshdr(sec
, &shdr
) == NULL
)
285 Elf_Data
*syms
= elf_getdata(sec
, NULL
);
289 sec
= elf_getscn(elf
, shdr
.sh_link
);
293 Elf_Data
*symstrs
= elf_getdata(sec
, NULL
);
297 const uint32_t nr_syms
= shdr
.sh_size
/ shdr
.sh_entsize
;
301 elf_symtab__for_each_symbol(syms
, nr_syms
, index
, sym
) {
302 if (!elf_sym__is_function(&sym
))
304 struct symbol
*f
= symbol__new(sym
.st_value
, sym
.st_size
,
305 elf_sym__name(&sym
, symstrs
));
309 dso__insert_symbol(self
, f
);
320 static size_t dso__fprintf(struct dso
*self
, FILE *fp
)
323 size_t ret
= fprintf(fp
, "dso: %s\n", self
->name
);
325 list_for_each_entry(pos
, &self
->syms
, node
)
326 ret
+= symbol__fprintf(pos
, fp
);
331 static LIST_HEAD(dsos
);
332 static struct dso
*kernel_dso
;
334 static void dsos__add(struct dso
*dso
)
336 list_add_tail(&dso
->node
, &dsos
);
339 static struct dso
*dsos__find(const char *name
)
343 list_for_each_entry(pos
, &dsos
, node
)
344 if (strcmp(pos
->name
, name
) == 0)
349 static struct dso
*dsos__findnew(const char *name
)
351 struct dso
*dso
= dsos__find(name
);
354 dso
= dso__new(name
);
355 if (dso
!= NULL
&& dso__load(dso
) < 0)
368 static void dsos__fprintf(FILE *fp
)
372 list_for_each_entry(pos
, &dsos
, node
)
373 dso__fprintf(pos
, fp
);
376 static int load_kallsyms(void)
378 kernel_dso
= dso__new("[kernel]");
379 if (kernel_dso
== NULL
)
382 FILE *file
= fopen("/proc/kallsyms", "r");
390 while (!feof(file
)) {
391 unsigned long long start
;
394 if (getline(&line
, &n
, file
) < 0)
400 if (sscanf(line
, "%llx %c %s", &start
, &c
, symbf
) == 3) {
401 struct symbol
*sym
= symbol__new(start
, 0x1000000, symbf
);
406 dso__insert_symbol(kernel_dso
, sym
);
410 dsos__add(kernel_dso
);
416 dso__delete(kernel_dso
);
421 struct list_head node
;
428 static struct map
*map__new(struct mmap_event
*event
)
430 struct map
*self
= malloc(sizeof(*self
));
433 self
->start
= event
->start
;
434 self
->end
= event
->start
+ event
->len
;
435 self
->pgoff
= event
->pgoff
;
437 self
->dso
= dsos__findnew(event
->filename
);
438 if (self
->dso
== NULL
)
447 static size_t map__fprintf(struct map
*self
, FILE *fp
)
449 return fprintf(fp
, " %lx-%lx %lx %s\n",
450 self
->start
, self
->end
, self
->pgoff
, self
->dso
->name
);
454 struct list_head node
;
461 static struct symhist
*symhist__new(struct symbol
*sym
, struct dso
*dso
,
464 struct symhist
*self
= malloc(sizeof(*self
));
476 static void symhist__delete(struct symhist
*self
)
481 static bool symhist__equal(struct symhist
*self
, struct symbol
*sym
,
482 struct dso
*dso
, char level
)
484 return self
->level
== level
&& self
->sym
== sym
&& self
->dso
== dso
;
487 static void symhist__inc(struct symhist
*self
)
492 static size_t symhist__fprintf(struct symhist
*self
, FILE *fp
)
494 size_t ret
= fprintf(fp
, "[%c] ", self
->level
);
496 if (self
->level
!= '.')
497 ret
+= fprintf(fp
, "%s", self
->sym
->name
);
499 ret
+= fprintf(fp
, "%s: %s",
500 self
->dso
? self
->dso
->name
: "<unknown",
501 self
->sym
? self
->sym
->name
: "<unknown>");
502 return ret
+ fprintf(fp
, ": %u\n", self
->count
);
506 struct list_head node
;
507 struct list_head maps
;
508 struct list_head symhists
;
513 static struct thread
*thread__new(pid_t pid
)
515 struct thread
*self
= malloc(sizeof(*self
));
520 INIT_LIST_HEAD(&self
->maps
);
521 INIT_LIST_HEAD(&self
->symhists
);
527 static void thread__insert_symhist(struct thread
*self
,
528 struct symhist
*symhist
)
530 list_add_tail(&symhist
->node
, &self
->symhists
);
533 static struct symhist
*thread__symhists_find(struct thread
*self
,
535 struct dso
*dso
, char level
)
539 list_for_each_entry(pos
, &self
->symhists
, node
)
540 if (symhist__equal(pos
, sym
, dso
, level
))
546 static int thread__symbol_incnew(struct thread
*self
, struct symbol
*sym
,
547 struct dso
*dso
, char level
)
549 struct symhist
*symhist
= thread__symhists_find(self
, sym
, dso
, level
);
551 if (symhist
== NULL
) {
552 symhist
= symhist__new(sym
, dso
, level
);
555 thread__insert_symhist(self
, symhist
);
558 symhist__inc(symhist
);
564 static int thread__set_comm(struct thread
*self
, const char *comm
)
566 self
->comm
= strdup(comm
);
567 return self
->comm
? 0 : -ENOMEM
;
570 static size_t thread__maps_fprintf(struct thread
*self
, FILE *fp
)
575 list_for_each_entry(pos
, &self
->maps
, node
)
576 ret
+= map__fprintf(pos
, fp
);
581 static size_t thread__fprintf(struct thread
*self
, FILE *fp
)
584 int ret
= fprintf(fp
, "thread: %d %s\n", self
->pid
, self
->comm
);
586 list_for_each_entry(pos
, &self
->symhists
, node
)
587 ret
+= symhist__fprintf(pos
, fp
);
592 static LIST_HEAD(threads
);
594 static void threads__add(struct thread
*thread
)
596 list_add_tail(&thread
->node
, &threads
);
599 static struct thread
*threads__find(pid_t pid
)
603 list_for_each_entry(pos
, &threads
, node
)
609 static struct thread
*threads__findnew(pid_t pid
)
611 struct thread
*thread
= threads__find(pid
);
613 if (thread
== NULL
) {
614 thread
= thread__new(pid
);
616 threads__add(thread
);
622 static void thread__insert_map(struct thread
*self
, struct map
*map
)
624 list_add_tail(&map
->node
, &self
->maps
);
627 static struct map
*thread__find_map(struct thread
*self
, uint64_t ip
)
634 list_for_each_entry(pos
, &self
->maps
, node
)
635 if (ip
>= pos
->start
&& ip
<= pos
->end
)
641 static void threads__fprintf(FILE *fp
)
645 list_for_each_entry(pos
, &threads
, node
)
646 thread__fprintf(pos
, fp
);
650 static std::string
resolve_user_symbol(int pid
, uint64_t ip
)
652 std::string sym
= "<unknown>";
654 maps_t
&m
= maps
[pid
];
655 maps_t::const_iterator mi
= m
.upper_bound(map(ip
));
659 ip
-= mi
->start
+ mi
->pgoff
;
661 symbols_t
&s
= dsos
[mi
->dso
].syms
;
662 symbols_t::const_iterator si
= s
.upper_bound(symbol(ip
));
664 sym
= mi
->dso
+ ": <unknown>";
670 if (si
->start
<= ip
&& ip
< si
->end
)
671 sym
= mi
->dso
+ ": " + si
->name
;
673 else if (si
->start
<= ip
)
674 sym
= mi
->dso
+ ": ?" + si
->name
;
681 static void display_help(void)
684 "Usage: perf-report [<options>]\n"
685 " -i file --input=<file> # input file\n"
691 static void process_options(int argc
, char *argv
[])
696 int option_index
= 0;
697 /** Options for getopt */
698 static struct option long_options
[] = {
699 {"input", required_argument
, NULL
, 'i'},
700 {"no-user", no_argument
, NULL
, 'u'},
701 {"no-kernel", no_argument
, NULL
, 'k'},
702 {"no-hv", no_argument
, NULL
, 'h'},
705 int c
= getopt_long(argc
, argv
, "+:i:kuh",
706 long_options
, &option_index
);
711 case 'i': input_name
= strdup(optarg
); break;
712 case 'k': show_mask
&= ~SHOW_KERNEL
; break;
713 case 'u': show_mask
&= ~SHOW_USER
; break;
714 case 'h': show_mask
&= ~SHOW_HV
; break;
715 default: error
= 1; break;
723 int cmd_report(int argc
, char **argv
)
725 unsigned long offset
= 0;
726 unsigned long head
= 0;
730 int ret
, rc
= EXIT_FAILURE
;
731 unsigned long total
= 0;
733 elf_version(EV_CURRENT
);
735 page_size
= getpagesize();
737 process_options(argc
, argv
);
739 input
= open(input_name
, O_RDONLY
);
741 perror("failed to open file");
745 ret
= fstat(input
, &stat
);
747 perror("failed to stat file");
752 fprintf(stderr
, "zero-sized file, nothing to do!\n");
756 if (load_kallsyms() < 0) {
757 perror("failed to open kallsyms");
762 buf
= (char *)mmap(NULL
, page_size
* mmap_window
, PROT_READ
,
763 MAP_SHARED
, input
, offset
);
764 if (buf
== MAP_FAILED
) {
765 perror("failed to mmap file");
770 event
= (event_t
*)(buf
+ head
);
772 if (head
+ event
->header
.size
>= page_size
* mmap_window
) {
773 unsigned long shift
= page_size
* (head
/ page_size
);
776 ret
= munmap(buf
, page_size
* mmap_window
);
785 if (!event
->header
.size
) {
786 fprintf(stderr
, "zero-sized event at file offset %ld\n", offset
+ head
);
787 fprintf(stderr
, "skipping %ld bytes of events.\n", stat
.st_size
- offset
- head
);
791 head
+= event
->header
.size
;
793 if (event
->header
.misc
& PERF_EVENT_MISC_OVERFLOW
) {
796 struct dso
*dso
= NULL
;
797 struct thread
*thread
= threads__findnew(event
->ip
.pid
);
802 if (event
->header
.misc
& PERF_EVENT_MISC_KERNEL
) {
806 } else if (event
->header
.misc
& PERF_EVENT_MISC_USER
) {
809 struct map
*map
= thread__find_map(thread
, event
->ip
.ip
);
817 if (show
& show_mask
) {
818 struct symbol
*sym
= dso__find_symbol(dso
, event
->ip
.ip
);
820 if (thread__symbol_incnew(thread
, sym
, dso
, level
))
824 } else switch (event
->header
.type
) {
825 case PERF_EVENT_MMAP
: {
826 struct thread
*thread
= threads__findnew(event
->mmap
.pid
);
827 struct map
*map
= map__new(&event
->mmap
);
829 if (thread
== NULL
|| map
== NULL
)
831 thread__insert_map(thread
, map
);
834 case PERF_EVENT_COMM
: {
835 struct thread
*thread
= threads__findnew(event
->comm
.pid
);
837 if (thread
== NULL
||
838 thread__set_comm(thread
, event
->comm
.comm
))
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());
This page took 0.062374 seconds and 6 git commands to generate.