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
= "comm,dso";
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
);
90 dso
= dso__new(name
, 0);
94 nr
= dso__load(dso
, NULL
);
96 fprintf(stderr
, "Failed to open: %s\n", name
);
101 "No symbols found in: %s, maybe install a debug package?\n",
114 static void dsos__fprintf(FILE *fp
)
118 list_for_each_entry(pos
, &dsos
, node
)
119 dso__fprintf(pos
, fp
);
122 static int load_kernel(void)
126 kernel_dso
= dso__new("[kernel]", 0);
130 err
= dso__load_kernel(kernel_dso
, vmlinux
, NULL
);
132 dso__delete(kernel_dso
);
135 dsos__add(kernel_dso
);
140 static int strcommon(const char *pathname
, const char *cwd
, int cwdlen
)
144 while (pathname
[n
] == cwd
[n
] && n
< cwdlen
)
151 struct list_head node
;
158 static struct map
*map__new(struct mmap_event
*event
, char *cwd
, int cwdlen
)
160 struct map
*self
= malloc(sizeof(*self
));
163 const char *filename
= event
->filename
;
164 char newfilename
[PATH_MAX
];
167 int n
= strcommon(filename
, cwd
, cwdlen
);
169 snprintf(newfilename
, sizeof(newfilename
),
170 ".%s", filename
+ n
);
171 filename
= newfilename
;
175 self
->start
= event
->start
;
176 self
->end
= event
->start
+ event
->len
;
177 self
->pgoff
= event
->pgoff
;
179 self
->dso
= dsos__findnew(filename
);
180 if (self
->dso
== NULL
)
192 struct rb_node rb_node
;
193 struct list_head maps
;
198 static struct thread
*thread__new(pid_t pid
)
200 struct thread
*self
= malloc(sizeof(*self
));
205 INIT_LIST_HEAD(&self
->maps
);
211 static int thread__set_comm(struct thread
*self
, const char *comm
)
213 self
->comm
= strdup(comm
);
214 return self
->comm
? 0 : -ENOMEM
;
217 static struct rb_root threads
;
219 static struct thread
*threads__findnew(pid_t pid
)
221 struct rb_node
**p
= &threads
.rb_node
;
222 struct rb_node
*parent
= NULL
;
227 th
= rb_entry(parent
, struct thread
, rb_node
);
238 th
= thread__new(pid
);
240 rb_link_node(&th
->rb_node
, parent
, p
);
241 rb_insert_color(&th
->rb_node
, &threads
);
246 static void thread__insert_map(struct thread
*self
, struct map
*map
)
248 list_add_tail(&map
->node
, &self
->maps
);
251 static struct map
*thread__find_map(struct thread
*self
, uint64_t ip
)
258 list_for_each_entry(pos
, &self
->maps
, node
)
259 if (ip
>= pos
->start
&& ip
<= pos
->end
)
266 * histogram, sorted on item, collects counts
269 static struct rb_root hist
;
272 struct rb_node rb_node
;
274 struct thread
*thread
;
285 * configurable sorting bits
289 struct list_head list
;
293 int64_t (*cmp
)(struct hist_entry
*, struct hist_entry
*);
294 size_t (*print
)(FILE *fp
, struct hist_entry
*);
298 sort__thread_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
300 return right
->thread
->pid
- left
->thread
->pid
;
304 sort__thread_print(FILE *fp
, struct hist_entry
*self
)
306 return fprintf(fp
, " %16s:%5d", self
->thread
->comm
?: "", self
->thread
->pid
);
309 static struct sort_entry sort_thread
= {
310 .header
= " Command: Pid ",
311 .cmp
= sort__thread_cmp
,
312 .print
= sort__thread_print
,
316 sort__comm_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
318 char *comm_l
= left
->thread
->comm
;
319 char *comm_r
= right
->thread
->comm
;
321 if (!comm_l
|| !comm_r
) {
322 if (!comm_l
&& !comm_r
)
330 return strcmp(comm_l
, comm_r
);
334 sort__comm_print(FILE *fp
, struct hist_entry
*self
)
336 return fprintf(fp
, " %16s", self
->thread
->comm
?: "<unknown>");
339 static struct sort_entry sort_comm
= {
340 .header
= " Command",
341 .cmp
= sort__comm_cmp
,
342 .print
= sort__comm_print
,
346 sort__dso_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
348 struct dso
*dso_l
= left
->dso
;
349 struct dso
*dso_r
= right
->dso
;
351 if (!dso_l
|| !dso_r
) {
352 if (!dso_l
&& !dso_r
)
360 return strcmp(dso_l
->name
, dso_r
->name
);
364 sort__dso_print(FILE *fp
, struct hist_entry
*self
)
366 return fprintf(fp
, " %-25s", self
->dso
? self
->dso
->name
: "<unknown>");
369 static struct sort_entry sort_dso
= {
370 .header
= " Shared Object ",
371 .cmp
= sort__dso_cmp
,
372 .print
= sort__dso_print
,
376 sort__sym_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
380 if (left
->sym
== right
->sym
)
383 ip_l
= left
->sym
? left
->sym
->start
: left
->ip
;
384 ip_r
= right
->sym
? right
->sym
->start
: right
->ip
;
386 return (int64_t)(ip_r
- ip_l
);
390 sort__sym_print(FILE *fp
, struct hist_entry
*self
)
395 ret
+= fprintf(fp
, " %#018llx", (unsigned long long)self
->ip
);
397 ret
+= fprintf(fp
, " %s: %s",
398 self
->dso
? self
->dso
->name
: "<unknown>",
399 self
->sym
? self
->sym
->name
: "<unknown>");
404 static struct sort_entry sort_sym
= {
405 .header
= " Shared Object: Symbol",
406 .cmp
= sort__sym_cmp
,
407 .print
= sort__sym_print
,
410 struct sort_dimension
{
412 struct sort_entry
*entry
;
416 static struct sort_dimension sort_dimensions
[] = {
417 { .name
= "pid", .entry
= &sort_thread
, },
418 { .name
= "comm", .entry
= &sort_comm
, },
419 { .name
= "dso", .entry
= &sort_dso
, },
420 { .name
= "symbol", .entry
= &sort_sym
, },
423 static LIST_HEAD(hist_entry__sort_list
);
425 static int sort_dimension__add(char *tok
)
429 for (i
= 0; i
< ARRAY_SIZE(sort_dimensions
); i
++) {
430 struct sort_dimension
*sd
= &sort_dimensions
[i
];
435 if (strcmp(tok
, sd
->name
))
438 list_add_tail(&sd
->entry
->list
, &hist_entry__sort_list
);
446 static void setup_sorting(void)
448 char *tmp
, *tok
, *str
= strdup(sort_order
);
450 for (tok
= strtok_r(str
, ", ", &tmp
);
451 tok
; tok
= strtok_r(NULL
, ", ", &tmp
))
452 sort_dimension__add(tok
);
458 hist_entry__cmp(struct hist_entry
*left
, struct hist_entry
*right
)
460 struct sort_entry
*se
;
463 list_for_each_entry(se
, &hist_entry__sort_list
, list
) {
464 cmp
= se
->cmp(left
, right
);
473 hist_entry__fprintf(FILE *fp
, struct hist_entry
*self
, uint64_t total_samples
)
475 struct sort_entry
*se
;
479 ret
= fprintf(fp
, " %5.2f%%",
480 (self
->count
* 100.0) / total_samples
);
482 ret
= fprintf(fp
, "%12d ", self
->count
);
484 list_for_each_entry(se
, &hist_entry__sort_list
, list
)
485 ret
+= se
->print(fp
, self
);
487 ret
+= fprintf(fp
, "\n");
493 * collect histogram counts
497 hist_entry__add(struct thread
*thread
, struct map
*map
, struct dso
*dso
,
498 struct symbol
*sym
, uint64_t ip
, char level
)
500 struct rb_node
**p
= &hist
.rb_node
;
501 struct rb_node
*parent
= NULL
;
502 struct hist_entry
*he
;
503 struct hist_entry entry
= {
516 he
= rb_entry(parent
, struct hist_entry
, rb_node
);
518 cmp
= hist_entry__cmp(&entry
, he
);
531 he
= malloc(sizeof(*he
));
535 rb_link_node(&he
->rb_node
, parent
, p
);
536 rb_insert_color(&he
->rb_node
, &hist
);
542 * reverse the map, sort on count.
545 static struct rb_root output_hists
;
547 static void output__insert_entry(struct hist_entry
*he
)
549 struct rb_node
**p
= &output_hists
.rb_node
;
550 struct rb_node
*parent
= NULL
;
551 struct hist_entry
*iter
;
555 iter
= rb_entry(parent
, struct hist_entry
, rb_node
);
557 if (he
->count
> iter
->count
)
563 rb_link_node(&he
->rb_node
, parent
, p
);
564 rb_insert_color(&he
->rb_node
, &output_hists
);
567 static void output__resort(void)
569 struct rb_node
*next
= rb_first(&hist
);
570 struct hist_entry
*n
;
573 n
= rb_entry(next
, struct hist_entry
, rb_node
);
574 next
= rb_next(&n
->rb_node
);
576 rb_erase(&n
->rb_node
, &hist
);
577 output__insert_entry(n
);
581 static size_t output__fprintf(FILE *fp
, uint64_t total_samples
)
583 struct hist_entry
*pos
;
584 struct sort_entry
*se
;
590 fprintf(fp
, "# Overhead");
591 list_for_each_entry(se
, &hist_entry__sort_list
, list
)
592 fprintf(fp
, " %s", se
->header
);
595 fprintf(fp
, "# ........");
596 list_for_each_entry(se
, &hist_entry__sort_list
, list
) {
600 for (i
= 0; i
< strlen(se
->header
)-1; i
++)
607 for (nd
= rb_first(&output_hists
); nd
; nd
= rb_next(nd
)) {
608 pos
= rb_entry(nd
, struct hist_entry
, rb_node
);
609 ret
+= hist_entry__fprintf(fp
, pos
, total_samples
);
615 static void register_idle_thread(void)
617 struct thread
*thread
= threads__findnew(0);
619 if (thread
== NULL
||
620 thread__set_comm(thread
, "[idle]")) {
621 fprintf(stderr
, "problem inserting idle task.\n");
627 static int __cmd_report(void)
629 unsigned long offset
= 0;
630 unsigned long head
= 0;
634 int ret
, rc
= EXIT_FAILURE
;
636 unsigned long total
= 0, total_mmap
= 0, total_comm
= 0, total_unknown
= 0;
637 char cwd
[PATH_MAX
], *cwdp
= cwd
;
640 register_idle_thread();
642 input
= open(input_name
, O_RDONLY
);
644 perror("failed to open file");
648 ret
= fstat(input
, &stat
);
650 perror("failed to stat file");
655 fprintf(stderr
, "zero-sized file, nothing to do!\n");
659 if (load_kernel() < 0) {
660 perror("failed to load kernel symbols");
665 if (getcwd(cwd
, sizeof(cwd
)) == NULL
) {
666 perror("failed to get the current directory");
669 cwdlen
= strlen(cwd
);
675 buf
= (char *)mmap(NULL
, page_size
* mmap_window
, PROT_READ
,
676 MAP_SHARED
, input
, offset
);
677 if (buf
== MAP_FAILED
) {
678 perror("failed to mmap file");
683 event
= (event_t
*)(buf
+ head
);
685 size
= event
->header
.size
;
689 if (head
+ event
->header
.size
>= page_size
* mmap_window
) {
690 unsigned long shift
= page_size
* (head
/ page_size
);
693 ret
= munmap(buf
, page_size
* mmap_window
);
701 size
= event
->header
.size
;
705 if (event
->header
.misc
& PERF_EVENT_MISC_OVERFLOW
) {
708 struct dso
*dso
= NULL
;
709 struct thread
*thread
= threads__findnew(event
->ip
.pid
);
710 uint64_t ip
= event
->ip
.ip
;
711 struct map
*map
= NULL
;
714 fprintf(stderr
, "%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
715 (void *)(offset
+ head
),
716 (void *)(long)(event
->header
.size
),
722 if (thread
== NULL
) {
723 fprintf(stderr
, "problem processing %d event, skipping it.\n",
728 if (event
->header
.misc
& PERF_EVENT_MISC_KERNEL
) {
734 } else if (event
->header
.misc
& PERF_EVENT_MISC_USER
) {
739 map
= thread__find_map(thread
, ip
);
742 ip
-= map
->start
+ map
->pgoff
;
750 if (show
& show_mask
) {
751 struct symbol
*sym
= dso__find_symbol(dso
, ip
);
753 if (hist_entry__add(thread
, map
, dso
, sym
, ip
, level
)) {
755 "problem incrementing symbol count, skipping event\n");
760 } else switch (event
->header
.type
) {
761 case PERF_EVENT_MMAP
: {
762 struct thread
*thread
= threads__findnew(event
->mmap
.pid
);
763 struct map
*map
= map__new(&event
->mmap
, cwdp
, cwdlen
);
766 fprintf(stderr
, "%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n",
767 (void *)(offset
+ head
),
768 (void *)(long)(event
->header
.size
),
769 (void *)(long)event
->mmap
.start
,
770 (void *)(long)event
->mmap
.len
,
771 (void *)(long)event
->mmap
.pgoff
,
772 event
->mmap
.filename
);
774 if (thread
== NULL
|| map
== NULL
) {
775 fprintf(stderr
, "problem processing PERF_EVENT_MMAP, skipping event.\n");
778 thread__insert_map(thread
, map
);
782 case PERF_EVENT_COMM
: {
783 struct thread
*thread
= threads__findnew(event
->comm
.pid
);
786 fprintf(stderr
, "%p [%p]: PERF_EVENT_COMM: %s:%d\n",
787 (void *)(offset
+ head
),
788 (void *)(long)(event
->header
.size
),
789 event
->comm
.comm
, event
->comm
.pid
);
791 if (thread
== NULL
||
792 thread__set_comm(thread
, event
->comm
.comm
)) {
793 fprintf(stderr
, "problem processing PERF_EVENT_COMM, skipping event.\n");
802 fprintf(stderr
, "%p [%p]: skipping unknown header type: %d\n",
803 (void *)(offset
+ head
),
804 (void *)(long)(event
->header
.size
),
810 * assume we lost track of the stream, check alignment, and
811 * increment a single u64 in the hope to catch on again 'soon'.
814 if (unlikely(head
& 7))
823 if (offset
+ head
< stat
.st_size
)
830 fprintf(stderr
, " IP events: %10ld\n", total
);
831 fprintf(stderr
, " mmap events: %10ld\n", total_mmap
);
832 fprintf(stderr
, " comm events: %10ld\n", total_comm
);
833 fprintf(stderr
, " unknown events: %10ld\n", total_unknown
);
839 dsos__fprintf(stdout
);
842 output__fprintf(stdout
, total
);
847 static const char * const report_usage
[] = {
848 "perf report [<options>] <command>",
852 static const struct option options
[] = {
853 OPT_STRING('i', "input", &input_name
, "file",
855 OPT_BOOLEAN('v', "verbose", &verbose
,
856 "be more verbose (show symbol address, etc)"),
857 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
858 "dump raw trace in ASCII"),
859 OPT_STRING('k', "vmlinux", &vmlinux
, "file", "vmlinux pathname"),
860 OPT_STRING('s', "sort", &sort_order
, "key[,key2...]",
861 "sort by key(s): pid, comm, dso, symbol. Default: pid,symbol"),
862 OPT_BOOLEAN('P', "full-paths", &full_paths
,
863 "Don't shorten the pathnames taking into account the cwd"),
867 int cmd_report(int argc
, const char **argv
, const char *prefix
)
871 page_size
= getpagesize();
873 parse_options(argc
, argv
, options
, report_usage
, 0);
879 return __cmd_report();