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
;
30 static char default_sort_order
[] = "comm,dso";
31 static char *sort_order
= default_sort_order
;
34 static int show_mask
= SHOW_KERNEL
| SHOW_USER
| SHOW_HV
;
36 static int dump_trace
= 0;
37 #define dprintf(x...) do { if (dump_trace) printf(x); } while (0)
40 static int full_paths
;
42 static unsigned long page_size
;
43 static unsigned long mmap_window
= 32;
45 const char *perf_event_names
[] = {
46 [PERF_EVENT_MMAP
] = " PERF_EVENT_MMAP",
47 [PERF_EVENT_MUNMAP
] = " PERF_EVENT_MUNMAP",
48 [PERF_EVENT_COMM
] = " PERF_EVENT_COMM",
52 struct perf_event_header header
;
58 struct perf_event_header header
;
63 char filename
[PATH_MAX
];
67 struct perf_event_header header
;
72 typedef union event_union
{
73 struct perf_event_header header
;
75 struct mmap_event mmap
;
76 struct comm_event comm
;
79 static LIST_HEAD(dsos
);
80 static struct dso
*kernel_dso
;
82 static void dsos__add(struct dso
*dso
)
84 list_add_tail(&dso
->node
, &dsos
);
87 static struct dso
*dsos__find(const char *name
)
91 list_for_each_entry(pos
, &dsos
, node
)
92 if (strcmp(pos
->name
, name
) == 0)
97 static struct dso
*dsos__findnew(const char *name
)
99 struct dso
*dso
= dsos__find(name
);
105 dso
= dso__new(name
, 0);
109 nr
= dso__load(dso
, NULL
, verbose
);
112 fprintf(stderr
, "Failed to open: %s\n", name
);
115 if (!nr
&& verbose
) {
117 "No symbols found in: %s, maybe install a debug package?\n",
130 static void dsos__fprintf(FILE *fp
)
134 list_for_each_entry(pos
, &dsos
, node
)
135 dso__fprintf(pos
, fp
);
138 static int load_kernel(void)
142 kernel_dso
= dso__new("[kernel]", 0);
146 err
= dso__load_kernel(kernel_dso
, vmlinux
, NULL
, verbose
);
148 dso__delete(kernel_dso
);
151 dsos__add(kernel_dso
);
156 static char __cwd
[PATH_MAX
];
157 static char *cwd
= __cwd
;
160 static int strcommon(const char *pathname
)
164 while (pathname
[n
] == cwd
[n
] && n
< cwdlen
)
171 struct list_head node
;
178 static struct map
*map__new(struct mmap_event
*event
)
180 struct map
*self
= malloc(sizeof(*self
));
183 const char *filename
= event
->filename
;
184 char newfilename
[PATH_MAX
];
187 int n
= strcommon(filename
);
190 snprintf(newfilename
, sizeof(newfilename
),
191 ".%s", filename
+ n
);
192 filename
= newfilename
;
196 self
->start
= event
->start
;
197 self
->end
= event
->start
+ event
->len
;
198 self
->pgoff
= event
->pgoff
;
200 self
->dso
= dsos__findnew(filename
);
201 if (self
->dso
== NULL
)
213 struct rb_node rb_node
;
214 struct list_head maps
;
219 static struct thread
*thread__new(pid_t pid
)
221 struct thread
*self
= malloc(sizeof(*self
));
225 self
->comm
= malloc(32);
227 snprintf(self
->comm
, 32, ":%d", self
->pid
);
228 INIT_LIST_HEAD(&self
->maps
);
234 static int thread__set_comm(struct thread
*self
, const char *comm
)
238 self
->comm
= strdup(comm
);
239 return self
->comm
? 0 : -ENOMEM
;
242 static struct rb_root threads
;
243 static struct thread
*last_match
;
245 static struct thread
*threads__findnew(pid_t pid
)
247 struct rb_node
**p
= &threads
.rb_node
;
248 struct rb_node
*parent
= NULL
;
252 * Font-end cache - PID lookups come in blocks,
253 * so most of the time we dont have to look up
256 if (last_match
&& last_match
->pid
== pid
)
261 th
= rb_entry(parent
, struct thread
, rb_node
);
263 if (th
->pid
== pid
) {
274 th
= thread__new(pid
);
276 rb_link_node(&th
->rb_node
, parent
, p
);
277 rb_insert_color(&th
->rb_node
, &threads
);
284 static void thread__insert_map(struct thread
*self
, struct map
*map
)
286 list_add_tail(&map
->node
, &self
->maps
);
289 static struct map
*thread__find_map(struct thread
*self
, uint64_t ip
)
296 list_for_each_entry(pos
, &self
->maps
, node
)
297 if (ip
>= pos
->start
&& ip
<= pos
->end
)
304 * histogram, sorted on item, collects counts
307 static struct rb_root hist
;
310 struct rb_node rb_node
;
312 struct thread
*thread
;
323 * configurable sorting bits
327 struct list_head list
;
331 int64_t (*cmp
)(struct hist_entry
*, struct hist_entry
*);
332 int64_t (*collapse
)(struct hist_entry
*, struct hist_entry
*);
333 size_t (*print
)(FILE *fp
, struct hist_entry
*);
339 sort__thread_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
341 return right
->thread
->pid
- left
->thread
->pid
;
345 sort__thread_print(FILE *fp
, struct hist_entry
*self
)
347 return fprintf(fp
, "%16s:%5d", self
->thread
->comm
?: "", self
->thread
->pid
);
350 static struct sort_entry sort_thread
= {
351 .header
= " Command: Pid",
352 .cmp
= sort__thread_cmp
,
353 .print
= sort__thread_print
,
359 sort__comm_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
361 return right
->thread
->pid
- left
->thread
->pid
;
365 sort__comm_collapse(struct hist_entry
*left
, struct hist_entry
*right
)
367 char *comm_l
= left
->thread
->comm
;
368 char *comm_r
= right
->thread
->comm
;
370 if (!comm_l
|| !comm_r
) {
371 if (!comm_l
&& !comm_r
)
379 return strcmp(comm_l
, comm_r
);
383 sort__comm_print(FILE *fp
, struct hist_entry
*self
)
385 return fprintf(fp
, "%16s", self
->thread
->comm
);
388 static struct sort_entry sort_comm
= {
389 .header
= " Command",
390 .cmp
= sort__comm_cmp
,
391 .collapse
= sort__comm_collapse
,
392 .print
= sort__comm_print
,
398 sort__dso_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
400 struct dso
*dso_l
= left
->dso
;
401 struct dso
*dso_r
= right
->dso
;
403 if (!dso_l
|| !dso_r
) {
404 if (!dso_l
&& !dso_r
)
412 return strcmp(dso_l
->name
, dso_r
->name
);
416 sort__dso_print(FILE *fp
, struct hist_entry
*self
)
419 return fprintf(fp
, "%-25s", self
->dso
->name
);
421 return fprintf(fp
, "%016llx ", (__u64
)self
->ip
);
424 static struct sort_entry sort_dso
= {
425 .header
= "Shared Object ",
426 .cmp
= sort__dso_cmp
,
427 .print
= sort__dso_print
,
433 sort__sym_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
437 if (left
->sym
== right
->sym
)
440 ip_l
= left
->sym
? left
->sym
->start
: left
->ip
;
441 ip_r
= right
->sym
? right
->sym
->start
: right
->ip
;
443 return (int64_t)(ip_r
- ip_l
);
447 sort__sym_print(FILE *fp
, struct hist_entry
*self
)
452 ret
+= fprintf(fp
, "%#018llx ", (__u64
)self
->ip
);
455 ret
+= fprintf(fp
, "%s", self
->sym
->name
);
457 ret
+= fprintf(fp
, "%#016llx", (__u64
)self
->ip
);
462 static struct sort_entry sort_sym
= {
464 .cmp
= sort__sym_cmp
,
465 .print
= sort__sym_print
,
468 static int sort__need_collapse
= 0;
470 struct sort_dimension
{
472 struct sort_entry
*entry
;
476 static struct sort_dimension sort_dimensions
[] = {
477 { .name
= "pid", .entry
= &sort_thread
, },
478 { .name
= "comm", .entry
= &sort_comm
, },
479 { .name
= "dso", .entry
= &sort_dso
, },
480 { .name
= "symbol", .entry
= &sort_sym
, },
483 static LIST_HEAD(hist_entry__sort_list
);
485 static int sort_dimension__add(char *tok
)
489 for (i
= 0; i
< ARRAY_SIZE(sort_dimensions
); i
++) {
490 struct sort_dimension
*sd
= &sort_dimensions
[i
];
495 if (strncasecmp(tok
, sd
->name
, strlen(tok
)))
498 if (sd
->entry
->collapse
)
499 sort__need_collapse
= 1;
501 list_add_tail(&sd
->entry
->list
, &hist_entry__sort_list
);
511 hist_entry__cmp(struct hist_entry
*left
, struct hist_entry
*right
)
513 struct sort_entry
*se
;
516 list_for_each_entry(se
, &hist_entry__sort_list
, list
) {
517 cmp
= se
->cmp(left
, right
);
526 hist_entry__collapse(struct hist_entry
*left
, struct hist_entry
*right
)
528 struct sort_entry
*se
;
531 list_for_each_entry(se
, &hist_entry__sort_list
, list
) {
532 int64_t (*f
)(struct hist_entry
*, struct hist_entry
*);
534 f
= se
->collapse
?: se
->cmp
;
536 cmp
= f(left
, right
);
545 hist_entry__fprintf(FILE *fp
, struct hist_entry
*self
, uint64_t total_samples
)
547 struct sort_entry
*se
;
551 ret
= fprintf(fp
, " %6.2f%%",
552 (self
->count
* 100.0) / total_samples
);
554 ret
= fprintf(fp
, "%12d ", self
->count
);
556 list_for_each_entry(se
, &hist_entry__sort_list
, list
) {
558 ret
+= se
->print(fp
, self
);
561 ret
+= fprintf(fp
, "\n");
567 * collect histogram counts
571 hist_entry__add(struct thread
*thread
, struct map
*map
, struct dso
*dso
,
572 struct symbol
*sym
, uint64_t ip
, char level
)
574 struct rb_node
**p
= &hist
.rb_node
;
575 struct rb_node
*parent
= NULL
;
576 struct hist_entry
*he
;
577 struct hist_entry entry
= {
590 he
= rb_entry(parent
, struct hist_entry
, rb_node
);
592 cmp
= hist_entry__cmp(&entry
, he
);
605 he
= malloc(sizeof(*he
));
609 rb_link_node(&he
->rb_node
, parent
, p
);
610 rb_insert_color(&he
->rb_node
, &hist
);
615 static void hist_entry__free(struct hist_entry
*he
)
621 * collapse the histogram
624 static struct rb_root collapse_hists
;
626 static void collapse__insert_entry(struct hist_entry
*he
)
628 struct rb_node
**p
= &collapse_hists
.rb_node
;
629 struct rb_node
*parent
= NULL
;
630 struct hist_entry
*iter
;
635 iter
= rb_entry(parent
, struct hist_entry
, rb_node
);
637 cmp
= hist_entry__collapse(iter
, he
);
640 iter
->count
+= he
->count
;
641 hist_entry__free(he
);
651 rb_link_node(&he
->rb_node
, parent
, p
);
652 rb_insert_color(&he
->rb_node
, &collapse_hists
);
655 static void collapse__resort(void)
657 struct rb_node
*next
;
658 struct hist_entry
*n
;
660 if (!sort__need_collapse
)
663 next
= rb_first(&hist
);
665 n
= rb_entry(next
, struct hist_entry
, rb_node
);
666 next
= rb_next(&n
->rb_node
);
668 rb_erase(&n
->rb_node
, &hist
);
669 collapse__insert_entry(n
);
674 * reverse the map, sort on count.
677 static struct rb_root output_hists
;
679 static void output__insert_entry(struct hist_entry
*he
)
681 struct rb_node
**p
= &output_hists
.rb_node
;
682 struct rb_node
*parent
= NULL
;
683 struct hist_entry
*iter
;
687 iter
= rb_entry(parent
, struct hist_entry
, rb_node
);
689 if (he
->count
> iter
->count
)
695 rb_link_node(&he
->rb_node
, parent
, p
);
696 rb_insert_color(&he
->rb_node
, &output_hists
);
699 static void output__resort(void)
701 struct rb_node
*next
;
702 struct hist_entry
*n
;
703 struct rb_root
*tree
= &hist
;
705 if (sort__need_collapse
)
706 tree
= &collapse_hists
;
708 next
= rb_first(tree
);
711 n
= rb_entry(next
, struct hist_entry
, rb_node
);
712 next
= rb_next(&n
->rb_node
);
714 rb_erase(&n
->rb_node
, tree
);
715 output__insert_entry(n
);
719 static size_t output__fprintf(FILE *fp
, uint64_t total_samples
)
721 struct hist_entry
*pos
;
722 struct sort_entry
*se
;
728 fprintf(fp
, "# (%Ld profiler events)\n", (__u64
)total_samples
);
731 fprintf(fp
, "# Overhead");
732 list_for_each_entry(se
, &hist_entry__sort_list
, list
)
733 fprintf(fp
, " %s", se
->header
);
736 fprintf(fp
, "# ........");
737 list_for_each_entry(se
, &hist_entry__sort_list
, list
) {
741 for (i
= 0; i
< strlen(se
->header
); i
++)
748 for (nd
= rb_first(&output_hists
); nd
; nd
= rb_next(nd
)) {
749 pos
= rb_entry(nd
, struct hist_entry
, rb_node
);
750 ret
+= hist_entry__fprintf(fp
, pos
, total_samples
);
753 if (!strcmp(sort_order
, default_sort_order
)) {
755 fprintf(fp
, "# (For more details, try: perf report --sort comm,dso,symbol)\n");
763 static void register_idle_thread(void)
765 struct thread
*thread
= threads__findnew(0);
767 if (thread
== NULL
||
768 thread__set_comm(thread
, "[idle]")) {
769 fprintf(stderr
, "problem inserting idle task.\n");
774 static unsigned long total
= 0, total_mmap
= 0, total_comm
= 0, total_unknown
= 0;
777 process_overflow_event(event_t
*event
, unsigned long offset
, unsigned long head
)
781 struct dso
*dso
= NULL
;
782 struct thread
*thread
= threads__findnew(event
->ip
.pid
);
783 uint64_t ip
= event
->ip
.ip
;
784 struct map
*map
= NULL
;
786 dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
787 (void *)(offset
+ head
),
788 (void *)(long)(event
->header
.size
),
793 dprintf(" ... thread: %s:%d\n", thread
->comm
, thread
->pid
);
795 if (thread
== NULL
) {
796 fprintf(stderr
, "problem processing %d event, skipping it.\n",
801 if (event
->header
.misc
& PERF_EVENT_MISC_KERNEL
) {
807 dprintf(" ...... dso: %s\n", dso
->name
);
809 } else if (event
->header
.misc
& PERF_EVENT_MISC_USER
) {
814 map
= thread__find_map(thread
, ip
);
817 ip
-= map
->start
+ map
->pgoff
;
820 * If this is outside of all known maps,
821 * and is a negative address, try to look it
822 * up in the kernel dso, as it might be a
823 * vsyscall (which executes in user-mode):
825 if ((long long)ip
< 0)
828 dprintf(" ...... dso: %s\n", dso
? dso
->name
: "<not found>");
833 dprintf(" ...... dso: [hypervisor]\n");
836 if (show
& show_mask
) {
837 struct symbol
*sym
= dso__find_symbol(dso
, ip
);
839 if (hist_entry__add(thread
, map
, dso
, sym
, ip
, level
)) {
841 "problem incrementing symbol count, skipping event\n");
851 process_mmap_event(event_t
*event
, unsigned long offset
, unsigned long head
)
853 struct thread
*thread
= threads__findnew(event
->mmap
.pid
);
854 struct map
*map
= map__new(&event
->mmap
);
856 dprintf("%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n",
857 (void *)(offset
+ head
),
858 (void *)(long)(event
->header
.size
),
859 (void *)(long)event
->mmap
.start
,
860 (void *)(long)event
->mmap
.len
,
861 (void *)(long)event
->mmap
.pgoff
,
862 event
->mmap
.filename
);
864 if (thread
== NULL
|| map
== NULL
) {
865 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
869 thread__insert_map(thread
, map
);
876 process_comm_event(event_t
*event
, unsigned long offset
, unsigned long head
)
878 struct thread
*thread
= threads__findnew(event
->comm
.pid
);
880 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
881 (void *)(offset
+ head
),
882 (void *)(long)(event
->header
.size
),
883 event
->comm
.comm
, event
->comm
.pid
);
885 if (thread
== NULL
||
886 thread__set_comm(thread
, event
->comm
.comm
)) {
887 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
896 process_event(event_t
*event
, unsigned long offset
, unsigned long head
)
898 if (event
->header
.misc
& PERF_EVENT_MISC_OVERFLOW
)
899 return process_overflow_event(event
, offset
, head
);
901 switch (event
->header
.type
) {
902 case PERF_EVENT_MMAP
:
903 return process_mmap_event(event
, offset
, head
);
905 case PERF_EVENT_COMM
:
906 return process_comm_event(event
, offset
, head
);
909 * We dont process them right now but they are fine:
911 case PERF_EVENT_MUNMAP
:
912 case PERF_EVENT_PERIOD
:
913 case PERF_EVENT_THROTTLE
:
914 case PERF_EVENT_UNTHROTTLE
:
924 static int __cmd_report(void)
926 int ret
, rc
= EXIT_FAILURE
;
927 unsigned long offset
= 0;
928 unsigned long head
= 0;
934 register_idle_thread();
936 input
= open(input_name
, O_RDONLY
);
938 perror("failed to open file");
942 ret
= fstat(input
, &stat
);
944 perror("failed to stat file");
949 fprintf(stderr
, "zero-sized file, nothing to do!\n");
953 if (load_kernel() < 0) {
954 perror("failed to load kernel symbols");
959 if (getcwd(__cwd
, sizeof(__cwd
)) == NULL
) {
960 perror("failed to get the current directory");
963 cwdlen
= strlen(cwd
);
969 buf
= (char *)mmap(NULL
, page_size
* mmap_window
, PROT_READ
,
970 MAP_SHARED
, input
, offset
);
971 if (buf
== MAP_FAILED
) {
972 perror("failed to mmap file");
977 event
= (event_t
*)(buf
+ head
);
979 size
= event
->header
.size
;
983 if (head
+ event
->header
.size
>= page_size
* mmap_window
) {
984 unsigned long shift
= page_size
* (head
/ page_size
);
987 ret
= munmap(buf
, page_size
* mmap_window
);
995 size
= event
->header
.size
;
997 if (!size
|| process_event(event
, offset
, head
) < 0) {
999 dprintf("%p [%p]: skipping unknown header type: %d\n",
1000 (void *)(offset
+ head
),
1001 (void *)(long)(event
->header
.size
),
1002 event
->header
.type
);
1007 * assume we lost track of the stream, check alignment, and
1008 * increment a single u64 in the hope to catch on again 'soon'.
1011 if (unlikely(head
& 7))
1019 if (offset
+ head
< stat
.st_size
)
1025 dprintf(" IP events: %10ld\n", total
);
1026 dprintf(" mmap events: %10ld\n", total_mmap
);
1027 dprintf(" comm events: %10ld\n", total_comm
);
1028 dprintf(" unknown events: %10ld\n", total_unknown
);
1034 dsos__fprintf(stdout
);
1038 output__fprintf(stdout
, total
);
1043 static const char * const report_usage
[] = {
1044 "perf report [<options>] <command>",
1048 static const struct option options
[] = {
1049 OPT_STRING('i', "input", &input_name
, "file",
1051 OPT_BOOLEAN('v', "verbose", &verbose
,
1052 "be more verbose (show symbol address, etc)"),
1053 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
1054 "dump raw trace in ASCII"),
1055 OPT_STRING('k', "vmlinux", &vmlinux
, "file", "vmlinux pathname"),
1056 OPT_STRING('s', "sort", &sort_order
, "key[,key2...]",
1057 "sort by key(s): pid, comm, dso, symbol. Default: pid,symbol"),
1058 OPT_BOOLEAN('P', "full-paths", &full_paths
,
1059 "Don't shorten the pathnames taking into account the cwd"),
1063 static void setup_sorting(void)
1065 char *tmp
, *tok
, *str
= strdup(sort_order
);
1067 for (tok
= strtok_r(str
, ", ", &tmp
);
1068 tok
; tok
= strtok_r(NULL
, ", ", &tmp
)) {
1069 if (sort_dimension__add(tok
) < 0) {
1070 error("Unknown --sort key: `%s'", tok
);
1071 usage_with_options(report_usage
, options
);
1078 int cmd_report(int argc
, const char **argv
, const char *prefix
)
1082 page_size
= getpagesize();
1084 parse_options(argc
, argv
, options
, report_usage
, 0);
1090 return __cmd_report();