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 char __cwd
[PATH_MAX
];
151 static char *cwd
= __cwd
;
154 static int strcommon(const char *pathname
)
158 while (pathname
[n
] == cwd
[n
] && n
< cwdlen
)
165 struct list_head node
;
172 static struct map
*map__new(struct mmap_event
*event
)
174 struct map
*self
= malloc(sizeof(*self
));
177 const char *filename
= event
->filename
;
178 char newfilename
[PATH_MAX
];
181 int n
= strcommon(filename
);
184 snprintf(newfilename
, sizeof(newfilename
),
185 ".%s", filename
+ n
);
186 filename
= newfilename
;
190 self
->start
= event
->start
;
191 self
->end
= event
->start
+ event
->len
;
192 self
->pgoff
= event
->pgoff
;
194 self
->dso
= dsos__findnew(filename
);
195 if (self
->dso
== NULL
)
207 struct rb_node rb_node
;
208 struct list_head maps
;
213 static struct thread
*thread__new(pid_t pid
)
215 struct thread
*self
= malloc(sizeof(*self
));
219 self
->comm
= malloc(32);
221 snprintf(self
->comm
, 32, ":%d", self
->pid
);
222 INIT_LIST_HEAD(&self
->maps
);
228 static int thread__set_comm(struct thread
*self
, const char *comm
)
232 self
->comm
= strdup(comm
);
233 return self
->comm
? 0 : -ENOMEM
;
236 static struct rb_root threads
;
237 static struct thread
*last_match
;
239 static struct thread
*threads__findnew(pid_t pid
)
241 struct rb_node
**p
= &threads
.rb_node
;
242 struct rb_node
*parent
= NULL
;
246 * Font-end cache - PID lookups come in blocks,
247 * so most of the time we dont have to look up
250 if (last_match
&& last_match
->pid
== pid
)
255 th
= rb_entry(parent
, struct thread
, rb_node
);
257 if (th
->pid
== pid
) {
268 th
= thread__new(pid
);
270 rb_link_node(&th
->rb_node
, parent
, p
);
271 rb_insert_color(&th
->rb_node
, &threads
);
278 static void thread__insert_map(struct thread
*self
, struct map
*map
)
280 list_add_tail(&map
->node
, &self
->maps
);
283 static struct map
*thread__find_map(struct thread
*self
, uint64_t ip
)
290 list_for_each_entry(pos
, &self
->maps
, node
)
291 if (ip
>= pos
->start
&& ip
<= pos
->end
)
298 * histogram, sorted on item, collects counts
301 static struct rb_root hist
;
304 struct rb_node rb_node
;
306 struct thread
*thread
;
317 * configurable sorting bits
321 struct list_head list
;
325 int64_t (*cmp
)(struct hist_entry
*, struct hist_entry
*);
326 int64_t (*collapse
)(struct hist_entry
*, struct hist_entry
*);
327 size_t (*print
)(FILE *fp
, struct hist_entry
*);
333 sort__thread_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
335 return right
->thread
->pid
- left
->thread
->pid
;
339 sort__thread_print(FILE *fp
, struct hist_entry
*self
)
341 return fprintf(fp
, " %16s:%5d", self
->thread
->comm
?: "", self
->thread
->pid
);
344 static struct sort_entry sort_thread
= {
345 .header
= " Command: Pid ",
346 .cmp
= sort__thread_cmp
,
347 .print
= sort__thread_print
,
353 sort__comm_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
355 return right
->thread
->pid
- left
->thread
->pid
;
359 sort__comm_collapse(struct hist_entry
*left
, struct hist_entry
*right
)
361 char *comm_l
= left
->thread
->comm
;
362 char *comm_r
= right
->thread
->comm
;
364 if (!comm_l
|| !comm_r
) {
365 if (!comm_l
&& !comm_r
)
373 return strcmp(comm_l
, comm_r
);
377 sort__comm_print(FILE *fp
, struct hist_entry
*self
)
379 return fprintf(fp
, " %16s", self
->thread
->comm
);
382 static struct sort_entry sort_comm
= {
383 .header
= " Command",
384 .cmp
= sort__comm_cmp
,
385 .collapse
= sort__comm_collapse
,
386 .print
= sort__comm_print
,
392 sort__dso_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
394 struct dso
*dso_l
= left
->dso
;
395 struct dso
*dso_r
= right
->dso
;
397 if (!dso_l
|| !dso_r
) {
398 if (!dso_l
&& !dso_r
)
406 return strcmp(dso_l
->name
, dso_r
->name
);
410 sort__dso_print(FILE *fp
, struct hist_entry
*self
)
413 return fprintf(fp
, " %-25s", self
->dso
->name
);
415 return fprintf(fp
, " %016llx", (__u64
)self
->ip
);
418 static struct sort_entry sort_dso
= {
419 .header
= " Shared Object ",
420 .cmp
= sort__dso_cmp
,
421 .print
= sort__dso_print
,
427 sort__sym_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
431 if (left
->sym
== right
->sym
)
434 ip_l
= left
->sym
? left
->sym
->start
: left
->ip
;
435 ip_r
= right
->sym
? right
->sym
->start
: right
->ip
;
437 return (int64_t)(ip_r
- ip_l
);
441 sort__sym_print(FILE *fp
, struct hist_entry
*self
)
446 ret
+= fprintf(fp
, " %#018llx", (__u64
)self
->ip
);
449 ret
+= fprintf(fp
, " %s: ", self
->dso
->name
);
451 ret
+= fprintf(fp
, " %#016llx: ", (__u64
)self
->ip
);
454 ret
+= fprintf(fp
, "%s", self
->sym
->name
);
456 ret
+= fprintf(fp
, "%#016llx", (__u64
)self
->ip
);
461 static struct sort_entry sort_sym
= {
462 .header
= " Shared Object: Symbol",
463 .cmp
= sort__sym_cmp
,
464 .print
= sort__sym_print
,
467 static int sort__need_collapse
= 0;
469 struct sort_dimension
{
471 struct sort_entry
*entry
;
475 static struct sort_dimension sort_dimensions
[] = {
476 { .name
= "pid", .entry
= &sort_thread
, },
477 { .name
= "comm", .entry
= &sort_comm
, },
478 { .name
= "dso", .entry
= &sort_dso
, },
479 { .name
= "symbol", .entry
= &sort_sym
, },
482 static LIST_HEAD(hist_entry__sort_list
);
484 static int sort_dimension__add(char *tok
)
488 for (i
= 0; i
< ARRAY_SIZE(sort_dimensions
); i
++) {
489 struct sort_dimension
*sd
= &sort_dimensions
[i
];
494 if (strncasecmp(tok
, sd
->name
, strlen(tok
)))
497 if (sd
->entry
->collapse
)
498 sort__need_collapse
= 1;
500 list_add_tail(&sd
->entry
->list
, &hist_entry__sort_list
);
510 hist_entry__cmp(struct hist_entry
*left
, struct hist_entry
*right
)
512 struct sort_entry
*se
;
515 list_for_each_entry(se
, &hist_entry__sort_list
, list
) {
516 cmp
= se
->cmp(left
, right
);
525 hist_entry__collapse(struct hist_entry
*left
, struct hist_entry
*right
)
527 struct sort_entry
*se
;
530 list_for_each_entry(se
, &hist_entry__sort_list
, list
) {
531 int64_t (*f
)(struct hist_entry
*, struct hist_entry
*);
533 f
= se
->collapse
?: se
->cmp
;
535 cmp
= f(left
, right
);
544 hist_entry__fprintf(FILE *fp
, struct hist_entry
*self
, uint64_t total_samples
)
546 struct sort_entry
*se
;
550 ret
= fprintf(fp
, " %6.2f%%",
551 (self
->count
* 100.0) / total_samples
);
553 ret
= fprintf(fp
, "%12d ", self
->count
);
555 list_for_each_entry(se
, &hist_entry__sort_list
, list
)
556 ret
+= se
->print(fp
, self
);
558 ret
+= fprintf(fp
, "\n");
564 * collect histogram counts
568 hist_entry__add(struct thread
*thread
, struct map
*map
, struct dso
*dso
,
569 struct symbol
*sym
, uint64_t ip
, char level
)
571 struct rb_node
**p
= &hist
.rb_node
;
572 struct rb_node
*parent
= NULL
;
573 struct hist_entry
*he
;
574 struct hist_entry entry
= {
587 he
= rb_entry(parent
, struct hist_entry
, rb_node
);
589 cmp
= hist_entry__cmp(&entry
, he
);
602 he
= malloc(sizeof(*he
));
606 rb_link_node(&he
->rb_node
, parent
, p
);
607 rb_insert_color(&he
->rb_node
, &hist
);
612 static void hist_entry__free(struct hist_entry
*he
)
618 * collapse the histogram
621 static struct rb_root collapse_hists
;
623 static void collapse__insert_entry(struct hist_entry
*he
)
625 struct rb_node
**p
= &collapse_hists
.rb_node
;
626 struct rb_node
*parent
= NULL
;
627 struct hist_entry
*iter
;
632 iter
= rb_entry(parent
, struct hist_entry
, rb_node
);
634 cmp
= hist_entry__collapse(iter
, he
);
637 iter
->count
+= he
->count
;
638 hist_entry__free(he
);
648 rb_link_node(&he
->rb_node
, parent
, p
);
649 rb_insert_color(&he
->rb_node
, &collapse_hists
);
652 static void collapse__resort(void)
654 struct rb_node
*next
;
655 struct hist_entry
*n
;
657 if (!sort__need_collapse
)
660 next
= rb_first(&hist
);
662 n
= rb_entry(next
, struct hist_entry
, rb_node
);
663 next
= rb_next(&n
->rb_node
);
665 rb_erase(&n
->rb_node
, &hist
);
666 collapse__insert_entry(n
);
671 * reverse the map, sort on count.
674 static struct rb_root output_hists
;
676 static void output__insert_entry(struct hist_entry
*he
)
678 struct rb_node
**p
= &output_hists
.rb_node
;
679 struct rb_node
*parent
= NULL
;
680 struct hist_entry
*iter
;
684 iter
= rb_entry(parent
, struct hist_entry
, rb_node
);
686 if (he
->count
> iter
->count
)
692 rb_link_node(&he
->rb_node
, parent
, p
);
693 rb_insert_color(&he
->rb_node
, &output_hists
);
696 static void output__resort(void)
698 struct rb_node
*next
;
699 struct hist_entry
*n
;
701 if (sort__need_collapse
)
702 next
= rb_first(&collapse_hists
);
704 next
= rb_first(&hist
);
707 n
= rb_entry(next
, struct hist_entry
, rb_node
);
708 next
= rb_next(&n
->rb_node
);
710 rb_erase(&n
->rb_node
, &hist
);
711 output__insert_entry(n
);
715 static size_t output__fprintf(FILE *fp
, uint64_t total_samples
)
717 struct hist_entry
*pos
;
718 struct sort_entry
*se
;
724 fprintf(fp
, "# Overhead");
725 list_for_each_entry(se
, &hist_entry__sort_list
, list
)
726 fprintf(fp
, " %s", se
->header
);
729 fprintf(fp
, "# ........");
730 list_for_each_entry(se
, &hist_entry__sort_list
, list
) {
734 for (i
= 0; i
< strlen(se
->header
)-1; i
++)
741 for (nd
= rb_first(&output_hists
); nd
; nd
= rb_next(nd
)) {
742 pos
= rb_entry(nd
, struct hist_entry
, rb_node
);
743 ret
+= hist_entry__fprintf(fp
, pos
, total_samples
);
749 static void register_idle_thread(void)
751 struct thread
*thread
= threads__findnew(0);
753 if (thread
== NULL
||
754 thread__set_comm(thread
, "[idle]")) {
755 fprintf(stderr
, "problem inserting idle task.\n");
760 static unsigned long total
= 0, total_mmap
= 0, total_comm
= 0, total_unknown
= 0;
763 process_event(event_t
*event
, unsigned long offset
, unsigned long head
)
765 if (event
->header
.misc
& PERF_EVENT_MISC_OVERFLOW
) {
768 struct dso
*dso
= NULL
;
769 struct thread
*thread
= threads__findnew(event
->ip
.pid
);
770 uint64_t ip
= event
->ip
.ip
;
771 struct map
*map
= NULL
;
773 dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
774 (void *)(offset
+ head
),
775 (void *)(long)(event
->header
.size
),
780 dprintf(" ... thread: %s:%d\n", thread
->comm
, thread
->pid
);
782 if (thread
== NULL
) {
783 fprintf(stderr
, "problem processing %d event, skipping it.\n",
788 if (event
->header
.misc
& PERF_EVENT_MISC_KERNEL
) {
794 dprintf(" ...... dso: %s\n", dso
->name
);
796 } else if (event
->header
.misc
& PERF_EVENT_MISC_USER
) {
801 map
= thread__find_map(thread
, ip
);
804 ip
-= map
->start
+ map
->pgoff
;
807 * If this is outside of all known maps,
808 * and is a negative address, try to look it
809 * up in the kernel dso, as it might be a
810 * vsyscall (which executes in user-mode):
812 if ((long long)ip
< 0)
815 dprintf(" ...... dso: %s\n", dso
? dso
->name
: "<not found>");
820 dprintf(" ...... dso: [hypervisor]\n");
823 if (show
& show_mask
) {
824 struct symbol
*sym
= dso__find_symbol(dso
, ip
);
826 if (hist_entry__add(thread
, map
, dso
, sym
, ip
, level
)) {
828 "problem incrementing symbol count, skipping event\n");
833 } else switch (event
->header
.type
) {
834 case PERF_EVENT_MMAP
: {
835 struct thread
*thread
= threads__findnew(event
->mmap
.pid
);
836 struct map
*map
= map__new(&event
->mmap
);
838 dprintf("%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n",
839 (void *)(offset
+ head
),
840 (void *)(long)(event
->header
.size
),
841 (void *)(long)event
->mmap
.start
,
842 (void *)(long)event
->mmap
.len
,
843 (void *)(long)event
->mmap
.pgoff
,
844 event
->mmap
.filename
);
846 if (thread
== NULL
|| map
== NULL
) {
848 fprintf(stderr
, "problem processing PERF_EVENT_MMAP, skipping event.\n");
851 thread__insert_map(thread
, map
);
855 case PERF_EVENT_COMM
: {
856 struct thread
*thread
= threads__findnew(event
->comm
.pid
);
858 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
859 (void *)(offset
+ head
),
860 (void *)(long)(event
->header
.size
),
861 event
->comm
.comm
, event
->comm
.pid
);
863 if (thread
== NULL
||
864 thread__set_comm(thread
, event
->comm
.comm
)) {
865 fprintf(stderr
, "problem processing PERF_EVENT_COMM, skipping event.\n");
878 static int __cmd_report(void)
880 unsigned long offset
= 0;
881 unsigned long head
= 0;
885 int ret
, rc
= EXIT_FAILURE
;
888 register_idle_thread();
890 input
= open(input_name
, O_RDONLY
);
892 perror("failed to open file");
896 ret
= fstat(input
, &stat
);
898 perror("failed to stat file");
903 fprintf(stderr
, "zero-sized file, nothing to do!\n");
907 if (load_kernel() < 0) {
908 perror("failed to load kernel symbols");
913 if (getcwd(__cwd
, sizeof(__cwd
)) == NULL
) {
914 perror("failed to get the current directory");
917 cwdlen
= strlen(cwd
);
923 buf
= (char *)mmap(NULL
, page_size
* mmap_window
, PROT_READ
,
924 MAP_SHARED
, input
, offset
);
925 if (buf
== MAP_FAILED
) {
926 perror("failed to mmap file");
931 event
= (event_t
*)(buf
+ head
);
933 size
= event
->header
.size
;
937 if (head
+ event
->header
.size
>= page_size
* mmap_window
) {
938 unsigned long shift
= page_size
* (head
/ page_size
);
941 ret
= munmap(buf
, page_size
* mmap_window
);
949 size
= event
->header
.size
;
951 if (!size
|| process_event(event
, offset
, head
) < 0) {
953 dprintf("%p [%p]: skipping unknown header type: %d\n",
954 (void *)(offset
+ head
),
955 (void *)(long)(event
->header
.size
),
961 * assume we lost track of the stream, check alignment, and
962 * increment a single u64 in the hope to catch on again 'soon'.
965 if (unlikely(head
& 7))
973 if (offset
+ head
< stat
.st_size
)
979 dprintf(" IP events: %10ld\n", total
);
980 dprintf(" mmap events: %10ld\n", total_mmap
);
981 dprintf(" comm events: %10ld\n", total_comm
);
982 dprintf(" unknown events: %10ld\n", total_unknown
);
988 dsos__fprintf(stdout
);
992 output__fprintf(stdout
, total
);
997 static const char * const report_usage
[] = {
998 "perf report [<options>] <command>",
1002 static const struct option options
[] = {
1003 OPT_STRING('i', "input", &input_name
, "file",
1005 OPT_BOOLEAN('v', "verbose", &verbose
,
1006 "be more verbose (show symbol address, etc)"),
1007 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
1008 "dump raw trace in ASCII"),
1009 OPT_STRING('k', "vmlinux", &vmlinux
, "file", "vmlinux pathname"),
1010 OPT_STRING('s', "sort", &sort_order
, "key[,key2...]",
1011 "sort by key(s): pid, comm, dso, symbol. Default: pid,symbol"),
1012 OPT_BOOLEAN('P', "full-paths", &full_paths
,
1013 "Don't shorten the pathnames taking into account the cwd"),
1017 static void setup_sorting(void)
1019 char *tmp
, *tok
, *str
= strdup(sort_order
);
1021 for (tok
= strtok_r(str
, ", ", &tmp
);
1022 tok
; tok
= strtok_r(NULL
, ", ", &tmp
)) {
1023 if (sort_dimension__add(tok
) < 0) {
1024 error("Unknown --sort key: `%s'", tok
);
1025 usage_with_options(report_usage
, options
);
1032 int cmd_report(int argc
, const char **argv
, const char *prefix
)
1036 page_size
= getpagesize();
1038 parse_options(argc
, argv
, options
, report_usage
, 0);
1044 return __cmd_report();