4 * Builtin report command: Analyze the perf.data input file,
5 * look up and read DSOs and symbol information and display
6 * a histogram of results, along various sorting keys.
10 #include "util/util.h"
12 #include "util/list.h"
13 #include "util/cache.h"
14 #include "util/rbtree.h"
15 #include "util/symbol.h"
16 #include "util/string.h"
20 #include "util/parse-options.h"
21 #include "util/parse-events.h"
27 static char const *input_name
= "perf.data";
28 static char *vmlinux
= NULL
;
29 static char *sort_order
= "comm,dso";
31 static int show_mask
= SHOW_KERNEL
| SHOW_USER
| SHOW_HV
;
33 static int dump_trace
= 0;
34 #define dprintf(x...) do { if (dump_trace) printf(x); } while (0)
37 static int full_paths
;
39 static unsigned long page_size
;
40 static unsigned long mmap_window
= 32;
42 const char *perf_event_names
[] = {
43 [PERF_EVENT_MMAP
] = " PERF_EVENT_MMAP",
44 [PERF_EVENT_MUNMAP
] = " PERF_EVENT_MUNMAP",
45 [PERF_EVENT_COMM
] = " PERF_EVENT_COMM",
49 struct perf_event_header header
;
54 struct perf_event_header header
;
59 char filename
[PATH_MAX
];
62 struct perf_event_header header
;
67 typedef union event_union
{
68 struct perf_event_header header
;
70 struct mmap_event mmap
;
71 struct comm_event comm
;
74 static LIST_HEAD(dsos
);
75 static struct dso
*kernel_dso
;
77 static void dsos__add(struct dso
*dso
)
79 list_add_tail(&dso
->node
, &dsos
);
82 static struct dso
*dsos__find(const char *name
)
86 list_for_each_entry(pos
, &dsos
, node
)
87 if (strcmp(pos
->name
, name
) == 0)
92 static struct dso
*dsos__findnew(const char *name
)
94 struct dso
*dso
= dsos__find(name
);
100 dso
= dso__new(name
, 0);
104 nr
= dso__load(dso
, NULL
);
106 fprintf(stderr
, "Failed to open: %s\n", name
);
109 if (!nr
&& verbose
) {
111 "No symbols found in: %s, maybe install a debug package?\n",
124 static void dsos__fprintf(FILE *fp
)
128 list_for_each_entry(pos
, &dsos
, node
)
129 dso__fprintf(pos
, fp
);
132 static int load_kernel(void)
136 kernel_dso
= dso__new("[kernel]", 0);
140 err
= dso__load_kernel(kernel_dso
, vmlinux
, NULL
);
142 dso__delete(kernel_dso
);
145 dsos__add(kernel_dso
);
150 static int strcommon(const char *pathname
, const char *cwd
, int cwdlen
)
154 while (pathname
[n
] == cwd
[n
] && n
< cwdlen
)
161 struct list_head node
;
168 static struct map
*map__new(struct mmap_event
*event
, char *cwd
, int cwdlen
)
170 struct map
*self
= malloc(sizeof(*self
));
173 const char *filename
= event
->filename
;
174 char newfilename
[PATH_MAX
];
177 int n
= strcommon(filename
, cwd
, cwdlen
);
179 snprintf(newfilename
, sizeof(newfilename
),
180 ".%s", filename
+ n
);
181 filename
= newfilename
;
185 self
->start
= event
->start
;
186 self
->end
= event
->start
+ event
->len
;
187 self
->pgoff
= event
->pgoff
;
189 self
->dso
= dsos__findnew(filename
);
190 if (self
->dso
== NULL
)
202 struct rb_node rb_node
;
203 struct list_head maps
;
208 static struct thread
*thread__new(pid_t pid
)
210 struct thread
*self
= malloc(sizeof(*self
));
214 self
->comm
= malloc(30);
216 sprintf(self
->comm
, ":%d", pid
);
217 INIT_LIST_HEAD(&self
->maps
);
223 static int thread__set_comm(struct thread
*self
, const char *comm
)
225 self
->comm
= strdup(comm
);
226 return self
->comm
? 0 : -ENOMEM
;
229 static struct rb_root threads
;
231 static struct thread
*threads__findnew(pid_t pid
)
233 struct rb_node
**p
= &threads
.rb_node
;
234 struct rb_node
*parent
= NULL
;
239 th
= rb_entry(parent
, struct thread
, rb_node
);
250 th
= thread__new(pid
);
252 rb_link_node(&th
->rb_node
, parent
, p
);
253 rb_insert_color(&th
->rb_node
, &threads
);
258 static void thread__insert_map(struct thread
*self
, struct map
*map
)
260 list_add_tail(&map
->node
, &self
->maps
);
263 static struct map
*thread__find_map(struct thread
*self
, uint64_t ip
)
270 list_for_each_entry(pos
, &self
->maps
, node
)
271 if (ip
>= pos
->start
&& ip
<= pos
->end
)
278 * histogram, sorted on item, collects counts
281 static struct rb_root hist
;
284 struct rb_node rb_node
;
286 struct thread
*thread
;
297 * configurable sorting bits
301 struct list_head list
;
305 int64_t (*cmp
)(struct hist_entry
*, struct hist_entry
*);
306 size_t (*print
)(FILE *fp
, struct hist_entry
*);
310 sort__thread_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
312 return right
->thread
->pid
- left
->thread
->pid
;
316 sort__thread_print(FILE *fp
, struct hist_entry
*self
)
318 return fprintf(fp
, " %16s:%5d", self
->thread
->comm
?: "", self
->thread
->pid
);
321 static struct sort_entry sort_thread
= {
322 .header
= " Command: Pid ",
323 .cmp
= sort__thread_cmp
,
324 .print
= sort__thread_print
,
328 sort__comm_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
330 char *comm_l
= left
->thread
->comm
;
331 char *comm_r
= right
->thread
->comm
;
333 if (!comm_l
|| !comm_r
) {
334 if (!comm_l
&& !comm_r
)
342 return strcmp(comm_l
, comm_r
);
346 sort__comm_print(FILE *fp
, struct hist_entry
*self
)
348 return fprintf(fp
, " %16s", self
->thread
->comm
);
351 static struct sort_entry sort_comm
= {
352 .header
= " Command",
353 .cmp
= sort__comm_cmp
,
354 .print
= sort__comm_print
,
358 sort__dso_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
360 struct dso
*dso_l
= left
->dso
;
361 struct dso
*dso_r
= right
->dso
;
363 if (!dso_l
|| !dso_r
) {
364 if (!dso_l
&& !dso_r
)
372 return strcmp(dso_l
->name
, dso_r
->name
);
376 sort__dso_print(FILE *fp
, struct hist_entry
*self
)
379 return fprintf(fp
, " %-25s", self
->dso
->name
);
381 return fprintf(fp
, " %016llx", (__u64
)self
->ip
);
384 static struct sort_entry sort_dso
= {
385 .header
= " Shared Object ",
386 .cmp
= sort__dso_cmp
,
387 .print
= sort__dso_print
,
391 sort__sym_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
395 if (left
->sym
== right
->sym
)
398 ip_l
= left
->sym
? left
->sym
->start
: left
->ip
;
399 ip_r
= right
->sym
? right
->sym
->start
: right
->ip
;
401 return (int64_t)(ip_r
- ip_l
);
405 sort__sym_print(FILE *fp
, struct hist_entry
*self
)
410 ret
+= fprintf(fp
, " %#018llx", (__u64
)self
->ip
);
413 ret
+= fprintf(fp
, " %s: ", self
->dso
->name
);
415 ret
+= fprintf(fp
, " %#016llx: ", (__u64
)self
->ip
);
418 ret
+= fprintf(fp
, "%s", self
->sym
->name
);
420 ret
+= fprintf(fp
, "%#016llx", (__u64
)self
->ip
);
425 static struct sort_entry sort_sym
= {
426 .header
= " Shared Object: Symbol",
427 .cmp
= sort__sym_cmp
,
428 .print
= sort__sym_print
,
431 struct sort_dimension
{
433 struct sort_entry
*entry
;
437 static struct sort_dimension sort_dimensions
[] = {
438 { .name
= "pid", .entry
= &sort_thread
, },
439 { .name
= "comm", .entry
= &sort_comm
, },
440 { .name
= "dso", .entry
= &sort_dso
, },
441 { .name
= "symbol", .entry
= &sort_sym
, },
444 static LIST_HEAD(hist_entry__sort_list
);
446 static int sort_dimension__add(char *tok
)
450 for (i
= 0; i
< ARRAY_SIZE(sort_dimensions
); i
++) {
451 struct sort_dimension
*sd
= &sort_dimensions
[i
];
456 if (strcmp(tok
, sd
->name
))
459 list_add_tail(&sd
->entry
->list
, &hist_entry__sort_list
);
467 static void setup_sorting(void)
469 char *tmp
, *tok
, *str
= strdup(sort_order
);
471 for (tok
= strtok_r(str
, ", ", &tmp
);
472 tok
; tok
= strtok_r(NULL
, ", ", &tmp
))
473 sort_dimension__add(tok
);
479 hist_entry__cmp(struct hist_entry
*left
, struct hist_entry
*right
)
481 struct sort_entry
*se
;
484 list_for_each_entry(se
, &hist_entry__sort_list
, list
) {
485 cmp
= se
->cmp(left
, right
);
494 hist_entry__fprintf(FILE *fp
, struct hist_entry
*self
, uint64_t total_samples
)
496 struct sort_entry
*se
;
500 ret
= fprintf(fp
, " %5.2f%%",
501 (self
->count
* 100.0) / total_samples
);
503 ret
= fprintf(fp
, "%12d ", self
->count
);
505 list_for_each_entry(se
, &hist_entry__sort_list
, list
)
506 ret
+= se
->print(fp
, self
);
508 ret
+= fprintf(fp
, "\n");
514 * collect histogram counts
518 hist_entry__add(struct thread
*thread
, struct map
*map
, struct dso
*dso
,
519 struct symbol
*sym
, uint64_t ip
, char level
)
521 struct rb_node
**p
= &hist
.rb_node
;
522 struct rb_node
*parent
= NULL
;
523 struct hist_entry
*he
;
524 struct hist_entry entry
= {
537 he
= rb_entry(parent
, struct hist_entry
, rb_node
);
539 cmp
= hist_entry__cmp(&entry
, he
);
552 he
= malloc(sizeof(*he
));
556 rb_link_node(&he
->rb_node
, parent
, p
);
557 rb_insert_color(&he
->rb_node
, &hist
);
563 * reverse the map, sort on count.
566 static struct rb_root output_hists
;
568 static void output__insert_entry(struct hist_entry
*he
)
570 struct rb_node
**p
= &output_hists
.rb_node
;
571 struct rb_node
*parent
= NULL
;
572 struct hist_entry
*iter
;
576 iter
= rb_entry(parent
, struct hist_entry
, rb_node
);
578 if (he
->count
> iter
->count
)
584 rb_link_node(&he
->rb_node
, parent
, p
);
585 rb_insert_color(&he
->rb_node
, &output_hists
);
588 static void output__resort(void)
590 struct rb_node
*next
= rb_first(&hist
);
591 struct hist_entry
*n
;
594 n
= rb_entry(next
, struct hist_entry
, rb_node
);
595 next
= rb_next(&n
->rb_node
);
597 rb_erase(&n
->rb_node
, &hist
);
598 output__insert_entry(n
);
602 static size_t output__fprintf(FILE *fp
, uint64_t total_samples
)
604 struct hist_entry
*pos
;
605 struct sort_entry
*se
;
611 fprintf(fp
, "# Overhead");
612 list_for_each_entry(se
, &hist_entry__sort_list
, list
)
613 fprintf(fp
, " %s", se
->header
);
616 fprintf(fp
, "# ........");
617 list_for_each_entry(se
, &hist_entry__sort_list
, list
) {
621 for (i
= 0; i
< strlen(se
->header
)-1; i
++)
628 for (nd
= rb_first(&output_hists
); nd
; nd
= rb_next(nd
)) {
629 pos
= rb_entry(nd
, struct hist_entry
, rb_node
);
630 ret
+= hist_entry__fprintf(fp
, pos
, total_samples
);
636 static void register_idle_thread(void)
638 struct thread
*thread
= threads__findnew(0);
640 if (thread
== NULL
||
641 thread__set_comm(thread
, "[idle]")) {
642 fprintf(stderr
, "problem inserting idle task.\n");
648 static int __cmd_report(void)
650 unsigned long offset
= 0;
651 unsigned long head
= 0;
655 int ret
, rc
= EXIT_FAILURE
;
657 unsigned long total
= 0, total_mmap
= 0, total_comm
= 0, total_unknown
= 0;
658 char cwd
[PATH_MAX
], *cwdp
= cwd
;
661 register_idle_thread();
663 input
= open(input_name
, O_RDONLY
);
665 perror("failed to open file");
669 ret
= fstat(input
, &stat
);
671 perror("failed to stat file");
676 fprintf(stderr
, "zero-sized file, nothing to do!\n");
680 if (load_kernel() < 0) {
681 perror("failed to load kernel symbols");
686 if (getcwd(cwd
, sizeof(cwd
)) == NULL
) {
687 perror("failed to get the current directory");
690 cwdlen
= strlen(cwd
);
696 buf
= (char *)mmap(NULL
, page_size
* mmap_window
, PROT_READ
,
697 MAP_SHARED
, input
, offset
);
698 if (buf
== MAP_FAILED
) {
699 perror("failed to mmap file");
704 event
= (event_t
*)(buf
+ head
);
706 size
= event
->header
.size
;
710 if (head
+ event
->header
.size
>= page_size
* mmap_window
) {
711 unsigned long shift
= page_size
* (head
/ page_size
);
714 ret
= munmap(buf
, page_size
* mmap_window
);
722 size
= event
->header
.size
;
726 if (event
->header
.misc
& PERF_EVENT_MISC_OVERFLOW
) {
729 struct dso
*dso
= NULL
;
730 struct thread
*thread
= threads__findnew(event
->ip
.pid
);
731 uint64_t ip
= event
->ip
.ip
;
732 struct map
*map
= NULL
;
734 dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
735 (void *)(offset
+ head
),
736 (void *)(long)(event
->header
.size
),
741 if (thread
== NULL
) {
742 fprintf(stderr
, "problem processing %d event, skipping it.\n",
747 if (event
->header
.misc
& PERF_EVENT_MISC_KERNEL
) {
753 } else if (event
->header
.misc
& PERF_EVENT_MISC_USER
) {
758 map
= thread__find_map(thread
, ip
);
761 ip
-= map
->start
+ map
->pgoff
;
769 if (show
& show_mask
) {
770 struct symbol
*sym
= dso__find_symbol(dso
, ip
);
772 if (hist_entry__add(thread
, map
, dso
, sym
, ip
, level
)) {
774 "problem incrementing symbol count, skipping event\n");
779 } else switch (event
->header
.type
) {
780 case PERF_EVENT_MMAP
: {
781 struct thread
*thread
= threads__findnew(event
->mmap
.pid
);
782 struct map
*map
= map__new(&event
->mmap
, cwdp
, cwdlen
);
784 dprintf("%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n",
785 (void *)(offset
+ head
),
786 (void *)(long)(event
->header
.size
),
787 (void *)(long)event
->mmap
.start
,
788 (void *)(long)event
->mmap
.len
,
789 (void *)(long)event
->mmap
.pgoff
,
790 event
->mmap
.filename
);
792 if (thread
== NULL
|| map
== NULL
) {
794 fprintf(stderr
, "problem processing PERF_EVENT_MMAP, skipping event.\n");
797 thread__insert_map(thread
, map
);
801 case PERF_EVENT_COMM
: {
802 struct thread
*thread
= threads__findnew(event
->comm
.pid
);
804 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
805 (void *)(offset
+ head
),
806 (void *)(long)(event
->header
.size
),
807 event
->comm
.comm
, event
->comm
.pid
);
809 if (thread
== NULL
||
810 thread__set_comm(thread
, event
->comm
.comm
)) {
811 fprintf(stderr
, "problem processing PERF_EVENT_COMM, skipping event.\n");
819 dprintf("%p [%p]: skipping unknown header type: %d\n",
820 (void *)(offset
+ head
),
821 (void *)(long)(event
->header
.size
),
827 * assume we lost track of the stream, check alignment, and
828 * increment a single u64 in the hope to catch on again 'soon'.
831 if (unlikely(head
& 7))
840 if (offset
+ head
< stat
.st_size
)
846 dprintf(" IP events: %10ld\n", total
);
847 dprintf(" mmap events: %10ld\n", total_mmap
);
848 dprintf(" comm events: %10ld\n", total_comm
);
849 dprintf(" unknown events: %10ld\n", total_unknown
);
855 dsos__fprintf(stdout
);
858 output__fprintf(stdout
, total
);
863 static const char * const report_usage
[] = {
864 "perf report [<options>] <command>",
868 static const struct option options
[] = {
869 OPT_STRING('i', "input", &input_name
, "file",
871 OPT_BOOLEAN('v', "verbose", &verbose
,
872 "be more verbose (show symbol address, etc)"),
873 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
874 "dump raw trace in ASCII"),
875 OPT_STRING('k', "vmlinux", &vmlinux
, "file", "vmlinux pathname"),
876 OPT_STRING('s', "sort", &sort_order
, "key[,key2...]",
877 "sort by key(s): pid, comm, dso, symbol. Default: pid,symbol"),
878 OPT_BOOLEAN('P', "full-paths", &full_paths
,
879 "Don't shorten the pathnames taking into account the cwd"),
883 int cmd_report(int argc
, const char **argv
, const char *prefix
)
887 page_size
= getpagesize();
889 parse_options(argc
, argv
, options
, report_usage
, 0);
895 return __cmd_report();