5 #include "util/cache.h"
6 #include "util/rbtree.h"
7 #include "util/symbol.h"
8 #include "util/string.h"
12 #include "util/parse-options.h"
13 #include "util/parse-events.h"
19 static char const *input_name
= "perf.data";
20 static char *vmlinux
= NULL
;
21 static char *sort_order
= "pid,symbol";
23 static int show_mask
= SHOW_KERNEL
| SHOW_USER
| SHOW_HV
;
25 static int dump_trace
= 0;
27 static int full_paths
;
29 static unsigned long page_size
;
30 static unsigned long mmap_window
= 32;
32 const char *perf_event_names
[] = {
33 [PERF_EVENT_MMAP
] = " PERF_EVENT_MMAP",
34 [PERF_EVENT_MUNMAP
] = " PERF_EVENT_MUNMAP",
35 [PERF_EVENT_COMM
] = " PERF_EVENT_COMM",
39 struct perf_event_header header
;
44 struct perf_event_header header
;
49 char filename
[PATH_MAX
];
52 struct perf_event_header header
;
57 typedef union event_union
{
58 struct perf_event_header header
;
60 struct mmap_event mmap
;
61 struct comm_event comm
;
64 static LIST_HEAD(dsos
);
65 static struct dso
*kernel_dso
;
67 static void dsos__add(struct dso
*dso
)
69 list_add_tail(&dso
->node
, &dsos
);
72 static struct dso
*dsos__find(const char *name
)
76 list_for_each_entry(pos
, &dsos
, node
)
77 if (strcmp(pos
->name
, name
) == 0)
82 static struct dso
*dsos__findnew(const char *name
)
84 struct dso
*dso
= dsos__find(name
);
88 dso
= dso__new(name
, 0);
92 nr
= dso__load(dso
, NULL
);
94 fprintf(stderr
, "Failed to open: %s\n", name
);
99 "Failed to find debug symbols for: %s, maybe install a debug package?\n",
113 static void dsos__fprintf(FILE *fp
)
117 list_for_each_entry(pos
, &dsos
, node
)
118 dso__fprintf(pos
, fp
);
121 static int load_kernel(void)
125 kernel_dso
= dso__new("[kernel]", 0);
129 err
= dso__load_kernel(kernel_dso
, vmlinux
, NULL
);
131 dso__delete(kernel_dso
);
134 dsos__add(kernel_dso
);
139 static int strcommon(const char *pathname
, const char *cwd
, int cwdlen
)
143 while (pathname
[n
] == cwd
[n
] && n
< cwdlen
)
150 struct list_head node
;
157 static struct map
*map__new(struct mmap_event
*event
, char *cwd
, int cwdlen
)
159 struct map
*self
= malloc(sizeof(*self
));
162 const char *filename
= event
->filename
;
163 char newfilename
[PATH_MAX
];
166 int n
= strcommon(filename
, cwd
, cwdlen
);
168 snprintf(newfilename
, sizeof(newfilename
),
169 ".%s", filename
+ n
);
170 filename
= newfilename
;
174 self
->start
= event
->start
;
175 self
->end
= event
->start
+ event
->len
;
176 self
->pgoff
= event
->pgoff
;
178 self
->dso
= dsos__findnew(filename
);
179 if (self
->dso
== NULL
)
191 struct rb_node rb_node
;
192 struct list_head maps
;
197 static struct thread
*thread__new(pid_t pid
)
199 struct thread
*self
= malloc(sizeof(*self
));
204 INIT_LIST_HEAD(&self
->maps
);
210 static int thread__set_comm(struct thread
*self
, const char *comm
)
212 self
->comm
= strdup(comm
);
213 return self
->comm
? 0 : -ENOMEM
;
216 static struct rb_root threads
;
218 static struct thread
*threads__findnew(pid_t pid
)
220 struct rb_node
**p
= &threads
.rb_node
;
221 struct rb_node
*parent
= NULL
;
226 th
= rb_entry(parent
, struct thread
, rb_node
);
237 th
= thread__new(pid
);
239 rb_link_node(&th
->rb_node
, parent
, p
);
240 rb_insert_color(&th
->rb_node
, &threads
);
245 static void thread__insert_map(struct thread
*self
, struct map
*map
)
247 list_add_tail(&map
->node
, &self
->maps
);
250 static struct map
*thread__find_map(struct thread
*self
, uint64_t ip
)
257 list_for_each_entry(pos
, &self
->maps
, node
)
258 if (ip
>= pos
->start
&& ip
<= pos
->end
)
265 * histogram, sorted on item, collects counts
268 static struct rb_root hist
;
271 struct rb_node rb_node
;
273 struct thread
*thread
;
284 * configurable sorting bits
288 struct list_head list
;
292 int64_t (*cmp
)(struct hist_entry
*, struct hist_entry
*);
293 size_t (*print
)(FILE *fp
, struct hist_entry
*);
297 sort__thread_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
299 return right
->thread
->pid
- left
->thread
->pid
;
303 sort__thread_print(FILE *fp
, struct hist_entry
*self
)
305 return fprintf(fp
, " %16s:%5d", self
->thread
->comm
?: "", self
->thread
->pid
);
308 static struct sort_entry sort_thread
= {
309 .header
= " Command: Pid ",
310 .cmp
= sort__thread_cmp
,
311 .print
= sort__thread_print
,
315 sort__comm_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
317 char *comm_l
= left
->thread
->comm
;
318 char *comm_r
= right
->thread
->comm
;
320 if (!comm_l
|| !comm_r
) {
321 if (!comm_l
&& !comm_r
)
329 return strcmp(comm_l
, comm_r
);
333 sort__comm_print(FILE *fp
, struct hist_entry
*self
)
335 return fprintf(fp
, " %16s", self
->thread
->comm
?: "<unknown>");
338 static struct sort_entry sort_comm
= {
339 .header
= " Command",
340 .cmp
= sort__comm_cmp
,
341 .print
= sort__comm_print
,
345 sort__dso_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
347 struct dso
*dso_l
= left
->dso
;
348 struct dso
*dso_r
= right
->dso
;
350 if (!dso_l
|| !dso_r
) {
351 if (!dso_l
&& !dso_r
)
359 return strcmp(dso_l
->name
, dso_r
->name
);
363 sort__dso_print(FILE *fp
, struct hist_entry
*self
)
365 return fprintf(fp
, " %64s", self
->dso
? self
->dso
->name
: "<unknown>");
368 static struct sort_entry sort_dso
= {
369 .header
= " Shared Object",
370 .cmp
= sort__dso_cmp
,
371 .print
= sort__dso_print
,
375 sort__sym_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
379 if (left
->sym
== right
->sym
)
382 ip_l
= left
->sym
? left
->sym
->start
: left
->ip
;
383 ip_r
= right
->sym
? right
->sym
->start
: right
->ip
;
385 return (int64_t)(ip_r
- ip_l
);
389 sort__sym_print(FILE *fp
, struct hist_entry
*self
)
394 ret
+= fprintf(fp
, " %#018llx", (unsigned long long)self
->ip
);
396 ret
+= fprintf(fp
, " %s: %s",
397 self
->dso
? self
->dso
->name
: "<unknown>",
398 self
->sym
? self
->sym
->name
: "<unknown>");
403 static struct sort_entry sort_sym
= {
404 .header
= "Shared Object: Symbol",
405 .cmp
= sort__sym_cmp
,
406 .print
= sort__sym_print
,
409 struct sort_dimension
{
411 struct sort_entry
*entry
;
415 static struct sort_dimension sort_dimensions
[] = {
416 { .name
= "pid", .entry
= &sort_thread
, },
417 { .name
= "comm", .entry
= &sort_comm
, },
418 { .name
= "dso", .entry
= &sort_dso
, },
419 { .name
= "symbol", .entry
= &sort_sym
, },
422 static LIST_HEAD(hist_entry__sort_list
);
424 static int sort_dimension__add(char *tok
)
428 for (i
= 0; i
< ARRAY_SIZE(sort_dimensions
); i
++) {
429 struct sort_dimension
*sd
= &sort_dimensions
[i
];
434 if (strcmp(tok
, sd
->name
))
437 list_add_tail(&sd
->entry
->list
, &hist_entry__sort_list
);
445 static void setup_sorting(void)
447 char *tmp
, *tok
, *str
= strdup(sort_order
);
449 for (tok
= strtok_r(str
, ", ", &tmp
);
450 tok
; tok
= strtok_r(NULL
, ", ", &tmp
))
451 sort_dimension__add(tok
);
457 hist_entry__cmp(struct hist_entry
*left
, struct hist_entry
*right
)
459 struct sort_entry
*se
;
462 list_for_each_entry(se
, &hist_entry__sort_list
, list
) {
463 cmp
= se
->cmp(left
, right
);
472 hist_entry__fprintf(FILE *fp
, struct hist_entry
*self
, uint64_t total_samples
)
474 struct sort_entry
*se
;
478 ret
= fprintf(fp
, " %5.2f%%",
479 (self
->count
* 100.0) / total_samples
);
481 ret
= fprintf(fp
, "%12d ", self
->count
);
483 list_for_each_entry(se
, &hist_entry__sort_list
, list
)
484 ret
+= se
->print(fp
, self
);
486 ret
+= fprintf(fp
, "\n");
492 * collect histogram counts
496 hist_entry__add(struct thread
*thread
, struct map
*map
, struct dso
*dso
,
497 struct symbol
*sym
, uint64_t ip
, char level
)
499 struct rb_node
**p
= &hist
.rb_node
;
500 struct rb_node
*parent
= NULL
;
501 struct hist_entry
*he
;
502 struct hist_entry entry
= {
515 he
= rb_entry(parent
, struct hist_entry
, rb_node
);
517 cmp
= hist_entry__cmp(&entry
, he
);
530 he
= malloc(sizeof(*he
));
534 rb_link_node(&he
->rb_node
, parent
, p
);
535 rb_insert_color(&he
->rb_node
, &hist
);
541 * reverse the map, sort on count.
544 static struct rb_root output_hists
;
546 static void output__insert_entry(struct hist_entry
*he
)
548 struct rb_node
**p
= &output_hists
.rb_node
;
549 struct rb_node
*parent
= NULL
;
550 struct hist_entry
*iter
;
554 iter
= rb_entry(parent
, struct hist_entry
, rb_node
);
556 if (he
->count
> iter
->count
)
562 rb_link_node(&he
->rb_node
, parent
, p
);
563 rb_insert_color(&he
->rb_node
, &output_hists
);
566 static void output__resort(void)
568 struct rb_node
*next
= rb_first(&hist
);
569 struct hist_entry
*n
;
572 n
= rb_entry(next
, struct hist_entry
, rb_node
);
573 next
= rb_next(&n
->rb_node
);
575 rb_erase(&n
->rb_node
, &hist
);
576 output__insert_entry(n
);
580 static size_t output__fprintf(FILE *fp
, uint64_t total_samples
)
582 struct hist_entry
*pos
;
583 struct sort_entry
*se
;
589 fprintf(fp
, "# Overhead");
590 list_for_each_entry(se
, &hist_entry__sort_list
, list
)
591 fprintf(fp
, " %s", se
->header
);
594 fprintf(fp
, "# ........");
595 list_for_each_entry(se
, &hist_entry__sort_list
, list
) {
599 for (i
= 0; i
< strlen(se
->header
); i
++)
606 for (nd
= rb_first(&output_hists
); nd
; nd
= rb_next(nd
)) {
607 pos
= rb_entry(nd
, struct hist_entry
, rb_node
);
608 ret
+= hist_entry__fprintf(fp
, pos
, total_samples
);
615 static int __cmd_report(void)
617 unsigned long offset
= 0;
618 unsigned long head
= 0;
622 int ret
, rc
= EXIT_FAILURE
;
624 unsigned long total
= 0, total_mmap
= 0, total_comm
= 0, total_unknown
= 0;
625 char cwd
[PATH_MAX
], *cwdp
= cwd
;
628 input
= open(input_name
, O_RDONLY
);
630 perror("failed to open file");
634 ret
= fstat(input
, &stat
);
636 perror("failed to stat file");
641 fprintf(stderr
, "zero-sized file, nothing to do!\n");
645 if (load_kernel() < 0) {
646 perror("failed to load kernel symbols");
651 if (getcwd(cwd
, sizeof(cwd
)) == NULL
) {
652 perror("failed to get the current directory");
655 cwdlen
= strlen(cwd
);
661 buf
= (char *)mmap(NULL
, page_size
* mmap_window
, PROT_READ
,
662 MAP_SHARED
, input
, offset
);
663 if (buf
== MAP_FAILED
) {
664 perror("failed to mmap file");
669 event
= (event_t
*)(buf
+ head
);
671 size
= event
->header
.size
;
675 if (head
+ event
->header
.size
>= page_size
* mmap_window
) {
676 unsigned long shift
= page_size
* (head
/ page_size
);
679 ret
= munmap(buf
, page_size
* mmap_window
);
687 size
= event
->header
.size
;
691 if (event
->header
.misc
& PERF_EVENT_MISC_OVERFLOW
) {
694 struct dso
*dso
= NULL
;
695 struct thread
*thread
= threads__findnew(event
->ip
.pid
);
696 uint64_t ip
= event
->ip
.ip
;
697 struct map
*map
= NULL
;
700 fprintf(stderr
, "%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
701 (void *)(offset
+ head
),
702 (void *)(long)(event
->header
.size
),
708 if (thread
== NULL
) {
709 fprintf(stderr
, "problem processing %d event, skipping it.\n",
714 if (event
->header
.misc
& PERF_EVENT_MISC_KERNEL
) {
720 } else if (event
->header
.misc
& PERF_EVENT_MISC_USER
) {
725 map
= thread__find_map(thread
, ip
);
728 ip
-= map
->start
+ map
->pgoff
;
736 if (show
& show_mask
) {
737 struct symbol
*sym
= dso__find_symbol(dso
, ip
);
739 if (hist_entry__add(thread
, map
, dso
, sym
, ip
, level
)) {
741 "problem incrementing symbol count, skipping event\n");
746 } else switch (event
->header
.type
) {
747 case PERF_EVENT_MMAP
: {
748 struct thread
*thread
= threads__findnew(event
->mmap
.pid
);
749 struct map
*map
= map__new(&event
->mmap
, cwdp
, cwdlen
);
752 fprintf(stderr
, "%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n",
753 (void *)(offset
+ head
),
754 (void *)(long)(event
->header
.size
),
755 (void *)(long)event
->mmap
.start
,
756 (void *)(long)event
->mmap
.len
,
757 (void *)(long)event
->mmap
.pgoff
,
758 event
->mmap
.filename
);
760 if (thread
== NULL
|| map
== NULL
) {
761 fprintf(stderr
, "problem processing PERF_EVENT_MMAP, skipping event.\n");
764 thread__insert_map(thread
, map
);
768 case PERF_EVENT_COMM
: {
769 struct thread
*thread
= threads__findnew(event
->comm
.pid
);
772 fprintf(stderr
, "%p [%p]: PERF_EVENT_COMM: %s:%d\n",
773 (void *)(offset
+ head
),
774 (void *)(long)(event
->header
.size
),
775 event
->comm
.comm
, event
->comm
.pid
);
777 if (thread
== NULL
||
778 thread__set_comm(thread
, event
->comm
.comm
)) {
779 fprintf(stderr
, "problem processing PERF_EVENT_COMM, skipping event.\n");
788 fprintf(stderr
, "%p [%p]: skipping unknown header type: %d\n",
789 (void *)(offset
+ head
),
790 (void *)(long)(event
->header
.size
),
796 * assume we lost track of the stream, check alignment, and
797 * increment a single u64 in the hope to catch on again 'soon'.
800 if (unlikely(head
& 7))
809 if (offset
+ head
< stat
.st_size
)
816 fprintf(stderr
, " IP events: %10ld\n", total
);
817 fprintf(stderr
, " mmap events: %10ld\n", total_mmap
);
818 fprintf(stderr
, " comm events: %10ld\n", total_comm
);
819 fprintf(stderr
, " unknown events: %10ld\n", total_unknown
);
825 dsos__fprintf(stdout
);
828 output__fprintf(stdout
, total
);
833 static const char * const report_usage
[] = {
834 "perf report [<options>] <command>",
838 static const struct option options
[] = {
839 OPT_STRING('i', "input", &input_name
, "file",
841 OPT_BOOLEAN('v', "verbose", &verbose
,
842 "be more verbose (show symbol address, etc)"),
843 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
844 "dump raw trace in ASCII"),
845 OPT_STRING('k', "vmlinux", &vmlinux
, "file", "vmlinux pathname"),
846 OPT_STRING('s', "sort", &sort_order
, "key[,key2...]",
847 "sort by key(s): pid, comm, dso, symbol. Default: pid,symbol"),
848 OPT_BOOLEAN('P', "full-paths", &full_paths
,
849 "Don't shorten the pathnames taking into account the cwd"),
853 int cmd_report(int argc
, const char **argv
, const char *prefix
)
857 page_size
= getpagesize();
859 parse_options(argc
, argv
, options
, report_usage
, 0);
865 return __cmd_report();