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
;
55 struct perf_event_header header
;
60 char filename
[PATH_MAX
];
64 struct perf_event_header header
;
69 typedef union event_union
{
70 struct perf_event_header header
;
72 struct mmap_event mmap
;
73 struct comm_event comm
;
76 static LIST_HEAD(dsos
);
77 static struct dso
*kernel_dso
;
79 static void dsos__add(struct dso
*dso
)
81 list_add_tail(&dso
->node
, &dsos
);
84 static struct dso
*dsos__find(const char *name
)
88 list_for_each_entry(pos
, &dsos
, node
)
89 if (strcmp(pos
->name
, name
) == 0)
94 static struct dso
*dsos__findnew(const char *name
)
96 struct dso
*dso
= dsos__find(name
);
102 dso
= dso__new(name
, 0);
106 nr
= dso__load(dso
, NULL
);
108 fprintf(stderr
, "Failed to open: %s\n", name
);
111 if (!nr
&& verbose
) {
113 "No symbols found in: %s, maybe install a debug package?\n",
126 static void dsos__fprintf(FILE *fp
)
130 list_for_each_entry(pos
, &dsos
, node
)
131 dso__fprintf(pos
, fp
);
134 static int load_kernel(void)
138 kernel_dso
= dso__new("[kernel]", 0);
142 err
= dso__load_kernel(kernel_dso
, vmlinux
, NULL
);
144 dso__delete(kernel_dso
);
147 dsos__add(kernel_dso
);
152 static char __cwd
[PATH_MAX
];
153 static char *cwd
= __cwd
;
156 static int strcommon(const char *pathname
)
160 while (pathname
[n
] == cwd
[n
] && n
< cwdlen
)
167 struct list_head node
;
174 static struct map
*map__new(struct mmap_event
*event
)
176 struct map
*self
= malloc(sizeof(*self
));
179 const char *filename
= event
->filename
;
180 char newfilename
[PATH_MAX
];
183 int n
= strcommon(filename
);
186 snprintf(newfilename
, sizeof(newfilename
),
187 ".%s", filename
+ n
);
188 filename
= newfilename
;
192 self
->start
= event
->start
;
193 self
->end
= event
->start
+ event
->len
;
194 self
->pgoff
= event
->pgoff
;
196 self
->dso
= dsos__findnew(filename
);
197 if (self
->dso
== NULL
)
209 struct rb_node rb_node
;
210 struct list_head maps
;
215 static struct thread
*thread__new(pid_t pid
)
217 struct thread
*self
= malloc(sizeof(*self
));
221 self
->comm
= malloc(32);
223 snprintf(self
->comm
, 32, ":%d", self
->pid
);
224 INIT_LIST_HEAD(&self
->maps
);
230 static int thread__set_comm(struct thread
*self
, const char *comm
)
234 self
->comm
= strdup(comm
);
235 return self
->comm
? 0 : -ENOMEM
;
238 static struct rb_root threads
;
239 static struct thread
*last_match
;
241 static struct thread
*threads__findnew(pid_t pid
)
243 struct rb_node
**p
= &threads
.rb_node
;
244 struct rb_node
*parent
= NULL
;
248 * Font-end cache - PID lookups come in blocks,
249 * so most of the time we dont have to look up
252 if (last_match
&& last_match
->pid
== pid
)
257 th
= rb_entry(parent
, struct thread
, rb_node
);
259 if (th
->pid
== pid
) {
270 th
= thread__new(pid
);
272 rb_link_node(&th
->rb_node
, parent
, p
);
273 rb_insert_color(&th
->rb_node
, &threads
);
280 static void thread__insert_map(struct thread
*self
, struct map
*map
)
282 list_add_tail(&map
->node
, &self
->maps
);
285 static struct map
*thread__find_map(struct thread
*self
, uint64_t ip
)
292 list_for_each_entry(pos
, &self
->maps
, node
)
293 if (ip
>= pos
->start
&& ip
<= pos
->end
)
300 * histogram, sorted on item, collects counts
303 static struct rb_root hist
;
306 struct rb_node rb_node
;
308 struct thread
*thread
;
319 * configurable sorting bits
323 struct list_head list
;
327 int64_t (*cmp
)(struct hist_entry
*, struct hist_entry
*);
328 int64_t (*collapse
)(struct hist_entry
*, struct hist_entry
*);
329 size_t (*print
)(FILE *fp
, struct hist_entry
*);
335 sort__thread_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
337 return right
->thread
->pid
- left
->thread
->pid
;
341 sort__thread_print(FILE *fp
, struct hist_entry
*self
)
343 return fprintf(fp
, " %16s:%5d", self
->thread
->comm
?: "", self
->thread
->pid
);
346 static struct sort_entry sort_thread
= {
347 .header
= " Command: Pid ",
348 .cmp
= sort__thread_cmp
,
349 .print
= sort__thread_print
,
355 sort__comm_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
357 return right
->thread
->pid
- left
->thread
->pid
;
361 sort__comm_collapse(struct hist_entry
*left
, struct hist_entry
*right
)
363 char *comm_l
= left
->thread
->comm
;
364 char *comm_r
= right
->thread
->comm
;
366 if (!comm_l
|| !comm_r
) {
367 if (!comm_l
&& !comm_r
)
375 return strcmp(comm_l
, comm_r
);
379 sort__comm_print(FILE *fp
, struct hist_entry
*self
)
381 return fprintf(fp
, " %16s", self
->thread
->comm
);
384 static struct sort_entry sort_comm
= {
385 .header
= " Command",
386 .cmp
= sort__comm_cmp
,
387 .collapse
= sort__comm_collapse
,
388 .print
= sort__comm_print
,
394 sort__dso_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
396 struct dso
*dso_l
= left
->dso
;
397 struct dso
*dso_r
= right
->dso
;
399 if (!dso_l
|| !dso_r
) {
400 if (!dso_l
&& !dso_r
)
408 return strcmp(dso_l
->name
, dso_r
->name
);
412 sort__dso_print(FILE *fp
, struct hist_entry
*self
)
415 return fprintf(fp
, " %-25s", self
->dso
->name
);
417 return fprintf(fp
, " %016llx", (__u64
)self
->ip
);
420 static struct sort_entry sort_dso
= {
421 .header
= " Shared Object ",
422 .cmp
= sort__dso_cmp
,
423 .print
= sort__dso_print
,
429 sort__sym_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
433 if (left
->sym
== right
->sym
)
436 ip_l
= left
->sym
? left
->sym
->start
: left
->ip
;
437 ip_r
= right
->sym
? right
->sym
->start
: right
->ip
;
439 return (int64_t)(ip_r
- ip_l
);
443 sort__sym_print(FILE *fp
, struct hist_entry
*self
)
448 ret
+= fprintf(fp
, " %#018llx", (__u64
)self
->ip
);
451 ret
+= fprintf(fp
, " %s: ", self
->dso
->name
);
453 ret
+= fprintf(fp
, " %#016llx: ", (__u64
)self
->ip
);
456 ret
+= fprintf(fp
, "%s", self
->sym
->name
);
458 ret
+= fprintf(fp
, "%#016llx", (__u64
)self
->ip
);
463 static struct sort_entry sort_sym
= {
464 .header
= " Shared Object: Symbol",
465 .cmp
= sort__sym_cmp
,
466 .print
= sort__sym_print
,
469 static int sort__need_collapse
= 0;
471 struct sort_dimension
{
473 struct sort_entry
*entry
;
477 static struct sort_dimension sort_dimensions
[] = {
478 { .name
= "pid", .entry
= &sort_thread
, },
479 { .name
= "comm", .entry
= &sort_comm
, },
480 { .name
= "dso", .entry
= &sort_dso
, },
481 { .name
= "symbol", .entry
= &sort_sym
, },
484 static LIST_HEAD(hist_entry__sort_list
);
486 static int sort_dimension__add(char *tok
)
490 for (i
= 0; i
< ARRAY_SIZE(sort_dimensions
); i
++) {
491 struct sort_dimension
*sd
= &sort_dimensions
[i
];
496 if (strncasecmp(tok
, sd
->name
, strlen(tok
)))
499 if (sd
->entry
->collapse
)
500 sort__need_collapse
= 1;
502 list_add_tail(&sd
->entry
->list
, &hist_entry__sort_list
);
512 hist_entry__cmp(struct hist_entry
*left
, struct hist_entry
*right
)
514 struct sort_entry
*se
;
517 list_for_each_entry(se
, &hist_entry__sort_list
, list
) {
518 cmp
= se
->cmp(left
, right
);
527 hist_entry__collapse(struct hist_entry
*left
, struct hist_entry
*right
)
529 struct sort_entry
*se
;
532 list_for_each_entry(se
, &hist_entry__sort_list
, list
) {
533 int64_t (*f
)(struct hist_entry
*, struct hist_entry
*);
535 f
= se
->collapse
?: se
->cmp
;
537 cmp
= f(left
, right
);
546 hist_entry__fprintf(FILE *fp
, struct hist_entry
*self
, uint64_t total_samples
)
548 struct sort_entry
*se
;
552 ret
= fprintf(fp
, " %6.2f%%",
553 (self
->count
* 100.0) / total_samples
);
555 ret
= fprintf(fp
, "%12d ", self
->count
);
557 list_for_each_entry(se
, &hist_entry__sort_list
, list
)
558 ret
+= se
->print(fp
, self
);
560 ret
+= fprintf(fp
, "\n");
566 * collect histogram counts
570 hist_entry__add(struct thread
*thread
, struct map
*map
, struct dso
*dso
,
571 struct symbol
*sym
, uint64_t ip
, char level
)
573 struct rb_node
**p
= &hist
.rb_node
;
574 struct rb_node
*parent
= NULL
;
575 struct hist_entry
*he
;
576 struct hist_entry entry
= {
589 he
= rb_entry(parent
, struct hist_entry
, rb_node
);
591 cmp
= hist_entry__cmp(&entry
, he
);
604 he
= malloc(sizeof(*he
));
608 rb_link_node(&he
->rb_node
, parent
, p
);
609 rb_insert_color(&he
->rb_node
, &hist
);
614 static void hist_entry__free(struct hist_entry
*he
)
620 * collapse the histogram
623 static struct rb_root collapse_hists
;
625 static void collapse__insert_entry(struct hist_entry
*he
)
627 struct rb_node
**p
= &collapse_hists
.rb_node
;
628 struct rb_node
*parent
= NULL
;
629 struct hist_entry
*iter
;
634 iter
= rb_entry(parent
, struct hist_entry
, rb_node
);
636 cmp
= hist_entry__collapse(iter
, he
);
639 iter
->count
+= he
->count
;
640 hist_entry__free(he
);
650 rb_link_node(&he
->rb_node
, parent
, p
);
651 rb_insert_color(&he
->rb_node
, &collapse_hists
);
654 static void collapse__resort(void)
656 struct rb_node
*next
;
657 struct hist_entry
*n
;
659 if (!sort__need_collapse
)
662 next
= rb_first(&hist
);
664 n
= rb_entry(next
, struct hist_entry
, rb_node
);
665 next
= rb_next(&n
->rb_node
);
667 rb_erase(&n
->rb_node
, &hist
);
668 collapse__insert_entry(n
);
673 * reverse the map, sort on count.
676 static struct rb_root output_hists
;
678 static void output__insert_entry(struct hist_entry
*he
)
680 struct rb_node
**p
= &output_hists
.rb_node
;
681 struct rb_node
*parent
= NULL
;
682 struct hist_entry
*iter
;
686 iter
= rb_entry(parent
, struct hist_entry
, rb_node
);
688 if (he
->count
> iter
->count
)
694 rb_link_node(&he
->rb_node
, parent
, p
);
695 rb_insert_color(&he
->rb_node
, &output_hists
);
698 static void output__resort(void)
700 struct rb_node
*next
;
701 struct hist_entry
*n
;
703 if (sort__need_collapse
)
704 next
= rb_first(&collapse_hists
);
706 next
= rb_first(&hist
);
709 n
= rb_entry(next
, struct hist_entry
, rb_node
);
710 next
= rb_next(&n
->rb_node
);
712 rb_erase(&n
->rb_node
, &hist
);
713 output__insert_entry(n
);
717 static size_t output__fprintf(FILE *fp
, uint64_t total_samples
)
719 struct hist_entry
*pos
;
720 struct sort_entry
*se
;
726 fprintf(fp
, "# Overhead");
727 list_for_each_entry(se
, &hist_entry__sort_list
, list
)
728 fprintf(fp
, " %s", se
->header
);
731 fprintf(fp
, "# ........");
732 list_for_each_entry(se
, &hist_entry__sort_list
, list
) {
736 for (i
= 0; i
< strlen(se
->header
)-1; i
++)
743 for (nd
= rb_first(&output_hists
); nd
; nd
= rb_next(nd
)) {
744 pos
= rb_entry(nd
, struct hist_entry
, rb_node
);
745 ret
+= hist_entry__fprintf(fp
, pos
, total_samples
);
751 static void register_idle_thread(void)
753 struct thread
*thread
= threads__findnew(0);
755 if (thread
== NULL
||
756 thread__set_comm(thread
, "[idle]")) {
757 fprintf(stderr
, "problem inserting idle task.\n");
762 static unsigned long total
= 0, total_mmap
= 0, total_comm
= 0, total_unknown
= 0;
765 process_overflow_event(event_t
*event
, unsigned long offset
, unsigned long head
)
769 struct dso
*dso
= NULL
;
770 struct thread
*thread
= threads__findnew(event
->ip
.pid
);
771 uint64_t ip
= event
->ip
.ip
;
772 struct map
*map
= NULL
;
774 dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
775 (void *)(offset
+ head
),
776 (void *)(long)(event
->header
.size
),
781 dprintf(" ... thread: %s:%d\n", thread
->comm
, thread
->pid
);
783 if (thread
== NULL
) {
784 fprintf(stderr
, "problem processing %d event, skipping it.\n",
789 if (event
->header
.misc
& PERF_EVENT_MISC_KERNEL
) {
795 dprintf(" ...... dso: %s\n", dso
->name
);
797 } else if (event
->header
.misc
& PERF_EVENT_MISC_USER
) {
802 map
= thread__find_map(thread
, ip
);
805 ip
-= map
->start
+ map
->pgoff
;
808 * If this is outside of all known maps,
809 * and is a negative address, try to look it
810 * up in the kernel dso, as it might be a
811 * vsyscall (which executes in user-mode):
813 if ((long long)ip
< 0)
816 dprintf(" ...... dso: %s\n", dso
? dso
->name
: "<not found>");
821 dprintf(" ...... dso: [hypervisor]\n");
824 if (show
& show_mask
) {
825 struct symbol
*sym
= dso__find_symbol(dso
, ip
);
827 if (hist_entry__add(thread
, map
, dso
, sym
, ip
, level
)) {
829 "problem incrementing symbol count, skipping event\n");
839 process_mmap_event(event_t
*event
, unsigned long offset
, unsigned long head
)
841 struct thread
*thread
= threads__findnew(event
->mmap
.pid
);
842 struct map
*map
= map__new(&event
->mmap
);
844 dprintf("%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n",
845 (void *)(offset
+ head
),
846 (void *)(long)(event
->header
.size
),
847 (void *)(long)event
->mmap
.start
,
848 (void *)(long)event
->mmap
.len
,
849 (void *)(long)event
->mmap
.pgoff
,
850 event
->mmap
.filename
);
852 if (thread
== NULL
|| map
== NULL
) {
853 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
857 thread__insert_map(thread
, map
);
864 process_comm_event(event_t
*event
, unsigned long offset
, unsigned long head
)
866 struct thread
*thread
= threads__findnew(event
->comm
.pid
);
868 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
869 (void *)(offset
+ head
),
870 (void *)(long)(event
->header
.size
),
871 event
->comm
.comm
, event
->comm
.pid
);
873 if (thread
== NULL
||
874 thread__set_comm(thread
, event
->comm
.comm
)) {
875 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
884 process_event(event_t
*event
, unsigned long offset
, unsigned long head
)
886 if (event
->header
.misc
& PERF_EVENT_MISC_OVERFLOW
)
887 return process_overflow_event(event
, offset
, head
);
889 switch (event
->header
.type
) {
890 case PERF_EVENT_MMAP
:
891 return process_mmap_event(event
, offset
, head
);
893 case PERF_EVENT_COMM
:
894 return process_comm_event(event
, offset
, head
);
903 static int __cmd_report(void)
905 int ret
, rc
= EXIT_FAILURE
;
906 unsigned long offset
= 0;
907 unsigned long head
= 0;
913 register_idle_thread();
915 input
= open(input_name
, O_RDONLY
);
917 perror("failed to open file");
921 ret
= fstat(input
, &stat
);
923 perror("failed to stat file");
928 fprintf(stderr
, "zero-sized file, nothing to do!\n");
932 if (load_kernel() < 0) {
933 perror("failed to load kernel symbols");
938 if (getcwd(__cwd
, sizeof(__cwd
)) == NULL
) {
939 perror("failed to get the current directory");
942 cwdlen
= strlen(cwd
);
948 buf
= (char *)mmap(NULL
, page_size
* mmap_window
, PROT_READ
,
949 MAP_SHARED
, input
, offset
);
950 if (buf
== MAP_FAILED
) {
951 perror("failed to mmap file");
956 event
= (event_t
*)(buf
+ head
);
958 size
= event
->header
.size
;
962 if (head
+ event
->header
.size
>= page_size
* mmap_window
) {
963 unsigned long shift
= page_size
* (head
/ page_size
);
966 ret
= munmap(buf
, page_size
* mmap_window
);
974 size
= event
->header
.size
;
976 if (!size
|| process_event(event
, offset
, head
) < 0) {
978 dprintf("%p [%p]: skipping unknown header type: %d\n",
979 (void *)(offset
+ head
),
980 (void *)(long)(event
->header
.size
),
986 * assume we lost track of the stream, check alignment, and
987 * increment a single u64 in the hope to catch on again 'soon'.
990 if (unlikely(head
& 7))
998 if (offset
+ head
< stat
.st_size
)
1004 dprintf(" IP events: %10ld\n", total
);
1005 dprintf(" mmap events: %10ld\n", total_mmap
);
1006 dprintf(" comm events: %10ld\n", total_comm
);
1007 dprintf(" unknown events: %10ld\n", total_unknown
);
1013 dsos__fprintf(stdout
);
1017 output__fprintf(stdout
, total
);
1022 static const char * const report_usage
[] = {
1023 "perf report [<options>] <command>",
1027 static const struct option options
[] = {
1028 OPT_STRING('i', "input", &input_name
, "file",
1030 OPT_BOOLEAN('v', "verbose", &verbose
,
1031 "be more verbose (show symbol address, etc)"),
1032 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
1033 "dump raw trace in ASCII"),
1034 OPT_STRING('k', "vmlinux", &vmlinux
, "file", "vmlinux pathname"),
1035 OPT_STRING('s', "sort", &sort_order
, "key[,key2...]",
1036 "sort by key(s): pid, comm, dso, symbol. Default: pid,symbol"),
1037 OPT_BOOLEAN('P', "full-paths", &full_paths
,
1038 "Don't shorten the pathnames taking into account the cwd"),
1042 static void setup_sorting(void)
1044 char *tmp
, *tok
, *str
= strdup(sort_order
);
1046 for (tok
= strtok_r(str
, ", ", &tmp
);
1047 tok
; tok
= strtok_r(NULL
, ", ", &tmp
)) {
1048 if (sort_dimension__add(tok
) < 0) {
1049 error("Unknown --sort key: `%s'", tok
);
1050 usage_with_options(report_usage
, options
);
1057 int cmd_report(int argc
, const char **argv
, const char *prefix
)
1061 page_size
= getpagesize();
1063 parse_options(argc
, argv
, options
, report_usage
, 0);
1069 return __cmd_report();