5 #include "util/cache.h"
6 #include "util/rbtree.h"
7 #include "util/symbol.h"
11 #include "util/parse-options.h"
12 #include "util/parse-events.h"
18 static char const *input_name
= "perf.data";
19 static char *vmlinux
= NULL
;
20 static char *sort_order
= "pid,symbol";
22 static int show_mask
= SHOW_KERNEL
| SHOW_USER
| SHOW_HV
;
24 static int dump_trace
= 0;
27 static unsigned long page_size
;
28 static unsigned long mmap_window
= 32;
30 const char *perf_event_names
[] = {
31 [PERF_EVENT_MMAP
] = " PERF_EVENT_MMAP",
32 [PERF_EVENT_MUNMAP
] = " PERF_EVENT_MUNMAP",
33 [PERF_EVENT_COMM
] = " PERF_EVENT_COMM",
37 struct perf_event_header header
;
42 struct perf_event_header header
;
47 char filename
[PATH_MAX
];
50 struct perf_event_header header
;
55 typedef union event_union
{
56 struct perf_event_header header
;
58 struct mmap_event mmap
;
59 struct comm_event comm
;
62 static LIST_HEAD(dsos
);
63 static struct dso
*kernel_dso
;
65 static void dsos__add(struct dso
*dso
)
67 list_add_tail(&dso
->node
, &dsos
);
70 static struct dso
*dsos__find(const char *name
)
74 list_for_each_entry(pos
, &dsos
, node
)
75 if (strcmp(pos
->name
, name
) == 0)
80 static struct dso
*dsos__findnew(const char *name
)
82 struct dso
*dso
= dsos__find(name
);
86 dso
= dso__new(name
, 0);
90 nr
= dso__load(dso
, NULL
);
92 fprintf(stderr
, "Failed to open: %s\n", name
);
97 "Failed to find debug symbols for: %s, maybe install a debug package?\n",
111 static void dsos__fprintf(FILE *fp
)
115 list_for_each_entry(pos
, &dsos
, node
)
116 dso__fprintf(pos
, fp
);
119 static int load_kernel(void)
123 kernel_dso
= dso__new("[kernel]", 0);
127 err
= dso__load_kernel(kernel_dso
, vmlinux
, NULL
);
129 dso__delete(kernel_dso
);
132 dsos__add(kernel_dso
);
138 struct list_head node
;
145 static struct map
*map__new(struct mmap_event
*event
)
147 struct map
*self
= malloc(sizeof(*self
));
150 self
->start
= event
->start
;
151 self
->end
= event
->start
+ event
->len
;
152 self
->pgoff
= event
->pgoff
;
154 self
->dso
= dsos__findnew(event
->filename
);
155 if (self
->dso
== NULL
)
167 struct rb_node rb_node
;
168 struct list_head maps
;
173 static struct thread
*thread__new(pid_t pid
)
175 struct thread
*self
= malloc(sizeof(*self
));
180 INIT_LIST_HEAD(&self
->maps
);
186 static int thread__set_comm(struct thread
*self
, const char *comm
)
188 self
->comm
= strdup(comm
);
189 return self
->comm
? 0 : -ENOMEM
;
192 static struct rb_root threads
;
194 static struct thread
*threads__findnew(pid_t pid
)
196 struct rb_node
**p
= &threads
.rb_node
;
197 struct rb_node
*parent
= NULL
;
202 th
= rb_entry(parent
, struct thread
, rb_node
);
213 th
= thread__new(pid
);
215 rb_link_node(&th
->rb_node
, parent
, p
);
216 rb_insert_color(&th
->rb_node
, &threads
);
221 static void thread__insert_map(struct thread
*self
, struct map
*map
)
223 list_add_tail(&map
->node
, &self
->maps
);
226 static struct map
*thread__find_map(struct thread
*self
, uint64_t ip
)
233 list_for_each_entry(pos
, &self
->maps
, node
)
234 if (ip
>= pos
->start
&& ip
<= pos
->end
)
241 * histogram, sorted on item, collects counts
244 static struct rb_root hist
;
247 struct rb_node rb_node
;
249 struct thread
*thread
;
260 * configurable sorting bits
264 struct list_head list
;
268 int64_t (*cmp
)(struct hist_entry
*, struct hist_entry
*);
269 size_t (*print
)(FILE *fp
, struct hist_entry
*);
273 sort__thread_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
275 return right
->thread
->pid
- left
->thread
->pid
;
279 sort__thread_print(FILE *fp
, struct hist_entry
*self
)
281 return fprintf(fp
, " %16s:%5d", self
->thread
->comm
?: "", self
->thread
->pid
);
284 static struct sort_entry sort_thread
= {
285 .header
= " Command: Pid ",
286 .cmp
= sort__thread_cmp
,
287 .print
= sort__thread_print
,
291 sort__comm_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
293 char *comm_l
= left
->thread
->comm
;
294 char *comm_r
= right
->thread
->comm
;
296 if (!comm_l
|| !comm_r
) {
297 if (!comm_l
&& !comm_r
)
305 return strcmp(comm_l
, comm_r
);
309 sort__comm_print(FILE *fp
, struct hist_entry
*self
)
311 return fprintf(fp
, " %16s", self
->thread
->comm
?: "<unknown>");
314 static struct sort_entry sort_comm
= {
315 .header
= " Command",
316 .cmp
= sort__comm_cmp
,
317 .print
= sort__comm_print
,
321 sort__dso_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
323 struct dso
*dso_l
= left
->dso
;
324 struct dso
*dso_r
= right
->dso
;
326 if (!dso_l
|| !dso_r
) {
327 if (!dso_l
&& !dso_r
)
335 return strcmp(dso_l
->name
, dso_r
->name
);
339 sort__dso_print(FILE *fp
, struct hist_entry
*self
)
341 return fprintf(fp
, " %64s", self
->dso
? self
->dso
->name
: "<unknown>");
344 static struct sort_entry sort_dso
= {
345 .header
= " Shared Object",
346 .cmp
= sort__dso_cmp
,
347 .print
= sort__dso_print
,
351 sort__sym_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
355 if (left
->sym
== right
->sym
)
358 ip_l
= left
->sym
? left
->sym
->start
: left
->ip
;
359 ip_r
= right
->sym
? right
->sym
->start
: right
->ip
;
361 return (int64_t)(ip_r
- ip_l
);
365 sort__sym_print(FILE *fp
, struct hist_entry
*self
)
370 ret
+= fprintf(fp
, " %#018llx", (unsigned long long)self
->ip
);
372 ret
+= fprintf(fp
, " %s: %s",
373 self
->dso
? self
->dso
->name
: "<unknown>",
374 self
->sym
? self
->sym
->name
: "<unknown>");
379 static struct sort_entry sort_sym
= {
380 .header
= "Shared Object: Symbol",
381 .cmp
= sort__sym_cmp
,
382 .print
= sort__sym_print
,
385 struct sort_dimension
{
387 struct sort_entry
*entry
;
391 static struct sort_dimension sort_dimensions
[] = {
392 { .name
= "pid", .entry
= &sort_thread
, },
393 { .name
= "comm", .entry
= &sort_comm
, },
394 { .name
= "dso", .entry
= &sort_dso
, },
395 { .name
= "symbol", .entry
= &sort_sym
, },
398 static LIST_HEAD(hist_entry__sort_list
);
400 static int sort_dimension__add(char *tok
)
404 for (i
= 0; i
< ARRAY_SIZE(sort_dimensions
); i
++) {
405 struct sort_dimension
*sd
= &sort_dimensions
[i
];
410 if (strcmp(tok
, sd
->name
))
413 list_add_tail(&sd
->entry
->list
, &hist_entry__sort_list
);
421 static void setup_sorting(void)
423 char *tmp
, *tok
, *str
= strdup(sort_order
);
425 for (tok
= strtok_r(str
, ", ", &tmp
);
426 tok
; tok
= strtok_r(NULL
, ", ", &tmp
))
427 sort_dimension__add(tok
);
433 hist_entry__cmp(struct hist_entry
*left
, struct hist_entry
*right
)
435 struct sort_entry
*se
;
438 list_for_each_entry(se
, &hist_entry__sort_list
, list
) {
439 cmp
= se
->cmp(left
, right
);
448 hist_entry__fprintf(FILE *fp
, struct hist_entry
*self
, uint64_t total_samples
)
450 struct sort_entry
*se
;
454 ret
= fprintf(fp
, " %5.2f%%",
455 (self
->count
* 100.0) / total_samples
);
457 ret
= fprintf(fp
, "%12d ", self
->count
);
459 list_for_each_entry(se
, &hist_entry__sort_list
, list
)
460 ret
+= se
->print(fp
, self
);
462 ret
+= fprintf(fp
, "\n");
468 * collect histogram counts
472 hist_entry__add(struct thread
*thread
, struct map
*map
, struct dso
*dso
,
473 struct symbol
*sym
, uint64_t ip
, char level
)
475 struct rb_node
**p
= &hist
.rb_node
;
476 struct rb_node
*parent
= NULL
;
477 struct hist_entry
*he
;
478 struct hist_entry entry
= {
491 he
= rb_entry(parent
, struct hist_entry
, rb_node
);
493 cmp
= hist_entry__cmp(&entry
, he
);
506 he
= malloc(sizeof(*he
));
510 rb_link_node(&he
->rb_node
, parent
, p
);
511 rb_insert_color(&he
->rb_node
, &hist
);
517 * reverse the map, sort on count.
520 static struct rb_root output_hists
;
522 static void output__insert_entry(struct hist_entry
*he
)
524 struct rb_node
**p
= &output_hists
.rb_node
;
525 struct rb_node
*parent
= NULL
;
526 struct hist_entry
*iter
;
530 iter
= rb_entry(parent
, struct hist_entry
, rb_node
);
532 if (he
->count
> iter
->count
)
538 rb_link_node(&he
->rb_node
, parent
, p
);
539 rb_insert_color(&he
->rb_node
, &output_hists
);
542 static void output__resort(void)
544 struct rb_node
*next
= rb_first(&hist
);
545 struct hist_entry
*n
;
548 n
= rb_entry(next
, struct hist_entry
, rb_node
);
549 next
= rb_next(&n
->rb_node
);
551 rb_erase(&n
->rb_node
, &hist
);
552 output__insert_entry(n
);
556 static size_t output__fprintf(FILE *fp
, uint64_t total_samples
)
558 struct hist_entry
*pos
;
559 struct sort_entry
*se
;
565 fprintf(fp
, "# Overhead");
566 list_for_each_entry(se
, &hist_entry__sort_list
, list
)
567 fprintf(fp
, " %s", se
->header
);
570 fprintf(fp
, "# ........");
571 list_for_each_entry(se
, &hist_entry__sort_list
, list
) {
575 for (i
= 0; i
< strlen(se
->header
); i
++)
582 for (nd
= rb_first(&output_hists
); nd
; nd
= rb_next(nd
)) {
583 pos
= rb_entry(nd
, struct hist_entry
, rb_node
);
584 ret
+= hist_entry__fprintf(fp
, pos
, total_samples
);
591 static int __cmd_report(void)
593 unsigned long offset
= 0;
594 unsigned long head
= 0;
598 int ret
, rc
= EXIT_FAILURE
;
600 unsigned long total
= 0, total_mmap
= 0, total_comm
= 0, total_unknown
= 0;
602 input
= open(input_name
, O_RDONLY
);
604 perror("failed to open file");
608 ret
= fstat(input
, &stat
);
610 perror("failed to stat file");
615 fprintf(stderr
, "zero-sized file, nothing to do!\n");
619 if (load_kernel() < 0) {
620 perror("failed to load kernel symbols");
625 buf
= (char *)mmap(NULL
, page_size
* mmap_window
, PROT_READ
,
626 MAP_SHARED
, input
, offset
);
627 if (buf
== MAP_FAILED
) {
628 perror("failed to mmap file");
633 event
= (event_t
*)(buf
+ head
);
635 size
= event
->header
.size
;
639 if (head
+ event
->header
.size
>= page_size
* mmap_window
) {
640 unsigned long shift
= page_size
* (head
/ page_size
);
643 ret
= munmap(buf
, page_size
* mmap_window
);
651 size
= event
->header
.size
;
655 if (event
->header
.misc
& PERF_EVENT_MISC_OVERFLOW
) {
658 struct dso
*dso
= NULL
;
659 struct thread
*thread
= threads__findnew(event
->ip
.pid
);
660 uint64_t ip
= event
->ip
.ip
;
661 struct map
*map
= NULL
;
664 fprintf(stderr
, "%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
665 (void *)(offset
+ head
),
666 (void *)(long)(event
->header
.size
),
672 if (thread
== NULL
) {
673 fprintf(stderr
, "problem processing %d event, skipping it.\n",
678 if (event
->header
.misc
& PERF_EVENT_MISC_KERNEL
) {
684 } else if (event
->header
.misc
& PERF_EVENT_MISC_USER
) {
689 map
= thread__find_map(thread
, ip
);
692 ip
-= map
->start
+ map
->pgoff
;
700 if (show
& show_mask
) {
701 struct symbol
*sym
= dso__find_symbol(dso
, ip
);
703 if (hist_entry__add(thread
, map
, dso
, sym
, ip
, level
)) {
705 "problem incrementing symbol count, skipping event\n");
710 } else switch (event
->header
.type
) {
711 case PERF_EVENT_MMAP
: {
712 struct thread
*thread
= threads__findnew(event
->mmap
.pid
);
713 struct map
*map
= map__new(&event
->mmap
);
716 fprintf(stderr
, "%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n",
717 (void *)(offset
+ head
),
718 (void *)(long)(event
->header
.size
),
719 (void *)(long)event
->mmap
.start
,
720 (void *)(long)event
->mmap
.len
,
721 (void *)(long)event
->mmap
.pgoff
,
722 event
->mmap
.filename
);
724 if (thread
== NULL
|| map
== NULL
) {
725 fprintf(stderr
, "problem processing PERF_EVENT_MMAP, skipping event.\n");
728 thread__insert_map(thread
, map
);
732 case PERF_EVENT_COMM
: {
733 struct thread
*thread
= threads__findnew(event
->comm
.pid
);
736 fprintf(stderr
, "%p [%p]: PERF_EVENT_COMM: %s:%d\n",
737 (void *)(offset
+ head
),
738 (void *)(long)(event
->header
.size
),
739 event
->comm
.comm
, event
->comm
.pid
);
741 if (thread
== NULL
||
742 thread__set_comm(thread
, event
->comm
.comm
)) {
743 fprintf(stderr
, "problem processing PERF_EVENT_COMM, skipping event.\n");
752 fprintf(stderr
, "%p [%p]: skipping unknown header type: %d\n",
753 (void *)(offset
+ head
),
754 (void *)(long)(event
->header
.size
),
760 * assume we lost track of the stream, check alignment, and
761 * increment a single u64 in the hope to catch on again 'soon'.
764 if (unlikely(head
& 7))
773 if (offset
+ head
< stat
.st_size
)
780 fprintf(stderr
, " IP events: %10ld\n", total
);
781 fprintf(stderr
, " mmap events: %10ld\n", total_mmap
);
782 fprintf(stderr
, " comm events: %10ld\n", total_comm
);
783 fprintf(stderr
, " unknown events: %10ld\n", total_unknown
);
789 dsos__fprintf(stdout
);
792 output__fprintf(stdout
, total
);
797 static const char * const report_usage
[] = {
798 "perf report [<options>] <command>",
802 static const struct option options
[] = {
803 OPT_STRING('i', "input", &input_name
, "file",
805 OPT_BOOLEAN('v', "verbose", &verbose
,
806 "be more verbose (show symbol address, etc)"),
807 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
808 "dump raw trace in ASCII"),
809 OPT_STRING('k', "vmlinux", &vmlinux
, "file", "vmlinux pathname"),
810 OPT_STRING('s', "sort", &sort_order
, "key[,key2...]",
811 "sort by key(s): pid, comm, dso, symbol. Default: pid,symbol"),
815 int cmd_report(int argc
, const char **argv
, const char *prefix
)
819 page_size
= getpagesize();
821 parse_options(argc
, argv
, options
, report_usage
, 0);
827 return __cmd_report();