perf report: Print -D to stdout
[deliverable/linux.git] / Documentation / perf_counter / builtin-report.c
1 /*
2 * builtin-report.c
3 *
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.
7 */
8 #include "builtin.h"
9
10 #include "util/util.h"
11
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"
17
18 #include "perf.h"
19
20 #include "util/parse-options.h"
21 #include "util/parse-events.h"
22
23 #define SHOW_KERNEL 1
24 #define SHOW_USER 2
25 #define SHOW_HV 4
26
27 static char const *input_name = "perf.data";
28 static char *vmlinux = NULL;
29 static char *sort_order = "comm,dso";
30 static int input;
31 static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
32
33 static int dump_trace = 0;
34 #define dprintf(x...) do { if (dump_trace) printf(x); } while (0)
35
36 static int verbose;
37 static int full_paths;
38
39 static unsigned long page_size;
40 static unsigned long mmap_window = 32;
41
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",
46 };
47
48 struct ip_event {
49 struct perf_event_header header;
50 __u64 ip;
51 __u32 pid, tid;
52 };
53 struct mmap_event {
54 struct perf_event_header header;
55 __u32 pid, tid;
56 __u64 start;
57 __u64 len;
58 __u64 pgoff;
59 char filename[PATH_MAX];
60 };
61 struct comm_event {
62 struct perf_event_header header;
63 __u32 pid,tid;
64 char comm[16];
65 };
66
67 typedef union event_union {
68 struct perf_event_header header;
69 struct ip_event ip;
70 struct mmap_event mmap;
71 struct comm_event comm;
72 } event_t;
73
74 static LIST_HEAD(dsos);
75 static struct dso *kernel_dso;
76
77 static void dsos__add(struct dso *dso)
78 {
79 list_add_tail(&dso->node, &dsos);
80 }
81
82 static struct dso *dsos__find(const char *name)
83 {
84 struct dso *pos;
85
86 list_for_each_entry(pos, &dsos, node)
87 if (strcmp(pos->name, name) == 0)
88 return pos;
89 return NULL;
90 }
91
92 static struct dso *dsos__findnew(const char *name)
93 {
94 struct dso *dso = dsos__find(name);
95 int nr;
96
97 if (dso)
98 return dso;
99
100 dso = dso__new(name, 0);
101 if (!dso)
102 goto out_delete_dso;
103
104 nr = dso__load(dso, NULL);
105 if (nr < 0) {
106 fprintf(stderr, "Failed to open: %s\n", name);
107 goto out_delete_dso;
108 }
109 if (!nr && verbose) {
110 fprintf(stderr,
111 "No symbols found in: %s, maybe install a debug package?\n",
112 name);
113 }
114
115 dsos__add(dso);
116
117 return dso;
118
119 out_delete_dso:
120 dso__delete(dso);
121 return NULL;
122 }
123
124 static void dsos__fprintf(FILE *fp)
125 {
126 struct dso *pos;
127
128 list_for_each_entry(pos, &dsos, node)
129 dso__fprintf(pos, fp);
130 }
131
132 static int load_kernel(void)
133 {
134 int err;
135
136 kernel_dso = dso__new("[kernel]", 0);
137 if (!kernel_dso)
138 return -1;
139
140 err = dso__load_kernel(kernel_dso, vmlinux, NULL);
141 if (err) {
142 dso__delete(kernel_dso);
143 kernel_dso = NULL;
144 } else
145 dsos__add(kernel_dso);
146
147 return err;
148 }
149
150 static int strcommon(const char *pathname, const char *cwd, int cwdlen)
151 {
152 int n = 0;
153
154 while (pathname[n] == cwd[n] && n < cwdlen)
155 ++n;
156
157 return n;
158 }
159
160 struct map {
161 struct list_head node;
162 uint64_t start;
163 uint64_t end;
164 uint64_t pgoff;
165 struct dso *dso;
166 };
167
168 static struct map *map__new(struct mmap_event *event, char *cwd, int cwdlen)
169 {
170 struct map *self = malloc(sizeof(*self));
171
172 if (self != NULL) {
173 const char *filename = event->filename;
174 char newfilename[PATH_MAX];
175
176 if (cwd) {
177 int n = strcommon(filename, cwd, cwdlen);
178 if (n == cwdlen) {
179 snprintf(newfilename, sizeof(newfilename),
180 ".%s", filename + n);
181 filename = newfilename;
182 }
183 }
184
185 self->start = event->start;
186 self->end = event->start + event->len;
187 self->pgoff = event->pgoff;
188
189 self->dso = dsos__findnew(filename);
190 if (self->dso == NULL)
191 goto out_delete;
192 }
193 return self;
194 out_delete:
195 free(self);
196 return NULL;
197 }
198
199 struct thread;
200
201 struct thread {
202 struct rb_node rb_node;
203 struct list_head maps;
204 pid_t pid;
205 char *comm;
206 };
207
208 static struct thread *thread__new(pid_t pid)
209 {
210 struct thread *self = malloc(sizeof(*self));
211
212 if (self != NULL) {
213 self->pid = pid;
214 self->comm = malloc(30);
215 if (self->comm)
216 sprintf(self->comm, ":%d", pid);
217 INIT_LIST_HEAD(&self->maps);
218 }
219
220 return self;
221 }
222
223 static int thread__set_comm(struct thread *self, const char *comm)
224 {
225 self->comm = strdup(comm);
226 return self->comm ? 0 : -ENOMEM;
227 }
228
229 static struct rb_root threads;
230
231 static struct thread *threads__findnew(pid_t pid)
232 {
233 struct rb_node **p = &threads.rb_node;
234 struct rb_node *parent = NULL;
235 struct thread *th;
236
237 while (*p != NULL) {
238 parent = *p;
239 th = rb_entry(parent, struct thread, rb_node);
240
241 if (th->pid == pid)
242 return th;
243
244 if (pid < th->pid)
245 p = &(*p)->rb_left;
246 else
247 p = &(*p)->rb_right;
248 }
249
250 th = thread__new(pid);
251 if (th != NULL) {
252 rb_link_node(&th->rb_node, parent, p);
253 rb_insert_color(&th->rb_node, &threads);
254 }
255 return th;
256 }
257
258 static void thread__insert_map(struct thread *self, struct map *map)
259 {
260 list_add_tail(&map->node, &self->maps);
261 }
262
263 static struct map *thread__find_map(struct thread *self, uint64_t ip)
264 {
265 struct map *pos;
266
267 if (self == NULL)
268 return NULL;
269
270 list_for_each_entry(pos, &self->maps, node)
271 if (ip >= pos->start && ip <= pos->end)
272 return pos;
273
274 return NULL;
275 }
276
277 /*
278 * histogram, sorted on item, collects counts
279 */
280
281 static struct rb_root hist;
282
283 struct hist_entry {
284 struct rb_node rb_node;
285
286 struct thread *thread;
287 struct map *map;
288 struct dso *dso;
289 struct symbol *sym;
290 uint64_t ip;
291 char level;
292
293 uint32_t count;
294 };
295
296 /*
297 * configurable sorting bits
298 */
299
300 struct sort_entry {
301 struct list_head list;
302
303 char *header;
304
305 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
306 size_t (*print)(FILE *fp, struct hist_entry *);
307 };
308
309 static int64_t
310 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
311 {
312 return right->thread->pid - left->thread->pid;
313 }
314
315 static size_t
316 sort__thread_print(FILE *fp, struct hist_entry *self)
317 {
318 return fprintf(fp, " %16s:%5d", self->thread->comm ?: "", self->thread->pid);
319 }
320
321 static struct sort_entry sort_thread = {
322 .header = " Command: Pid ",
323 .cmp = sort__thread_cmp,
324 .print = sort__thread_print,
325 };
326
327 static int64_t
328 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
329 {
330 char *comm_l = left->thread->comm;
331 char *comm_r = right->thread->comm;
332
333 if (!comm_l || !comm_r) {
334 if (!comm_l && !comm_r)
335 return 0;
336 else if (!comm_l)
337 return -1;
338 else
339 return 1;
340 }
341
342 return strcmp(comm_l, comm_r);
343 }
344
345 static size_t
346 sort__comm_print(FILE *fp, struct hist_entry *self)
347 {
348 return fprintf(fp, " %16s", self->thread->comm);
349 }
350
351 static struct sort_entry sort_comm = {
352 .header = " Command",
353 .cmp = sort__comm_cmp,
354 .print = sort__comm_print,
355 };
356
357 static int64_t
358 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
359 {
360 struct dso *dso_l = left->dso;
361 struct dso *dso_r = right->dso;
362
363 if (!dso_l || !dso_r) {
364 if (!dso_l && !dso_r)
365 return 0;
366 else if (!dso_l)
367 return -1;
368 else
369 return 1;
370 }
371
372 return strcmp(dso_l->name, dso_r->name);
373 }
374
375 static size_t
376 sort__dso_print(FILE *fp, struct hist_entry *self)
377 {
378 if (self->dso)
379 return fprintf(fp, " %-25s", self->dso->name);
380
381 return fprintf(fp, " %016llx", (__u64)self->ip);
382 }
383
384 static struct sort_entry sort_dso = {
385 .header = " Shared Object ",
386 .cmp = sort__dso_cmp,
387 .print = sort__dso_print,
388 };
389
390 static int64_t
391 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
392 {
393 uint64_t ip_l, ip_r;
394
395 if (left->sym == right->sym)
396 return 0;
397
398 ip_l = left->sym ? left->sym->start : left->ip;
399 ip_r = right->sym ? right->sym->start : right->ip;
400
401 return (int64_t)(ip_r - ip_l);
402 }
403
404 static size_t
405 sort__sym_print(FILE *fp, struct hist_entry *self)
406 {
407 size_t ret = 0;
408
409 if (verbose)
410 ret += fprintf(fp, " %#018llx", (__u64)self->ip);
411
412 if (self->dso)
413 ret += fprintf(fp, " %s: ", self->dso->name);
414 else
415 ret += fprintf(fp, " %#016llx: ", (__u64)self->ip);
416
417 if (self->sym)
418 ret += fprintf(fp, "%s", self->sym->name);
419 else
420 ret += fprintf(fp, "%#016llx", (__u64)self->ip);
421
422 return ret;
423 }
424
425 static struct sort_entry sort_sym = {
426 .header = " Shared Object: Symbol",
427 .cmp = sort__sym_cmp,
428 .print = sort__sym_print,
429 };
430
431 struct sort_dimension {
432 char *name;
433 struct sort_entry *entry;
434 int taken;
435 };
436
437 static struct sort_dimension sort_dimensions[] = {
438 { .name = "pid", .entry = &sort_thread, },
439 { .name = "comm", .entry = &sort_comm, },
440 { .name = "dso", .entry = &sort_dso, },
441 { .name = "symbol", .entry = &sort_sym, },
442 };
443
444 static LIST_HEAD(hist_entry__sort_list);
445
446 static int sort_dimension__add(char *tok)
447 {
448 int i;
449
450 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
451 struct sort_dimension *sd = &sort_dimensions[i];
452
453 if (sd->taken)
454 continue;
455
456 if (strcmp(tok, sd->name))
457 continue;
458
459 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
460 sd->taken = 1;
461 return 0;
462 }
463
464 return -ESRCH;
465 }
466
467 static void setup_sorting(void)
468 {
469 char *tmp, *tok, *str = strdup(sort_order);
470
471 for (tok = strtok_r(str, ", ", &tmp);
472 tok; tok = strtok_r(NULL, ", ", &tmp))
473 sort_dimension__add(tok);
474
475 free(str);
476 }
477
478 static int64_t
479 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
480 {
481 struct sort_entry *se;
482 int64_t cmp = 0;
483
484 list_for_each_entry(se, &hist_entry__sort_list, list) {
485 cmp = se->cmp(left, right);
486 if (cmp)
487 break;
488 }
489
490 return cmp;
491 }
492
493 static size_t
494 hist_entry__fprintf(FILE *fp, struct hist_entry *self, uint64_t total_samples)
495 {
496 struct sort_entry *se;
497 size_t ret;
498
499 if (total_samples) {
500 ret = fprintf(fp, " %5.2f%%",
501 (self->count * 100.0) / total_samples);
502 } else
503 ret = fprintf(fp, "%12d ", self->count);
504
505 list_for_each_entry(se, &hist_entry__sort_list, list)
506 ret += se->print(fp, self);
507
508 ret += fprintf(fp, "\n");
509
510 return ret;
511 }
512
513 /*
514 * collect histogram counts
515 */
516
517 static int
518 hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
519 struct symbol *sym, uint64_t ip, char level)
520 {
521 struct rb_node **p = &hist.rb_node;
522 struct rb_node *parent = NULL;
523 struct hist_entry *he;
524 struct hist_entry entry = {
525 .thread = thread,
526 .map = map,
527 .dso = dso,
528 .sym = sym,
529 .ip = ip,
530 .level = level,
531 .count = 1,
532 };
533 int cmp;
534
535 while (*p != NULL) {
536 parent = *p;
537 he = rb_entry(parent, struct hist_entry, rb_node);
538
539 cmp = hist_entry__cmp(&entry, he);
540
541 if (!cmp) {
542 he->count++;
543 return 0;
544 }
545
546 if (cmp < 0)
547 p = &(*p)->rb_left;
548 else
549 p = &(*p)->rb_right;
550 }
551
552 he = malloc(sizeof(*he));
553 if (!he)
554 return -ENOMEM;
555 *he = entry;
556 rb_link_node(&he->rb_node, parent, p);
557 rb_insert_color(&he->rb_node, &hist);
558
559 return 0;
560 }
561
562 /*
563 * reverse the map, sort on count.
564 */
565
566 static struct rb_root output_hists;
567
568 static void output__insert_entry(struct hist_entry *he)
569 {
570 struct rb_node **p = &output_hists.rb_node;
571 struct rb_node *parent = NULL;
572 struct hist_entry *iter;
573
574 while (*p != NULL) {
575 parent = *p;
576 iter = rb_entry(parent, struct hist_entry, rb_node);
577
578 if (he->count > iter->count)
579 p = &(*p)->rb_left;
580 else
581 p = &(*p)->rb_right;
582 }
583
584 rb_link_node(&he->rb_node, parent, p);
585 rb_insert_color(&he->rb_node, &output_hists);
586 }
587
588 static void output__resort(void)
589 {
590 struct rb_node *next = rb_first(&hist);
591 struct hist_entry *n;
592
593 while (next) {
594 n = rb_entry(next, struct hist_entry, rb_node);
595 next = rb_next(&n->rb_node);
596
597 rb_erase(&n->rb_node, &hist);
598 output__insert_entry(n);
599 }
600 }
601
602 static size_t output__fprintf(FILE *fp, uint64_t total_samples)
603 {
604 struct hist_entry *pos;
605 struct sort_entry *se;
606 struct rb_node *nd;
607 size_t ret = 0;
608
609 fprintf(fp, "#\n");
610
611 fprintf(fp, "# Overhead");
612 list_for_each_entry(se, &hist_entry__sort_list, list)
613 fprintf(fp, " %s", se->header);
614 fprintf(fp, "\n");
615
616 fprintf(fp, "# ........");
617 list_for_each_entry(se, &hist_entry__sort_list, list) {
618 int i;
619
620 fprintf(fp, " ");
621 for (i = 0; i < strlen(se->header)-1; i++)
622 fprintf(fp, ".");
623 }
624 fprintf(fp, "\n");
625
626 fprintf(fp, "#\n");
627
628 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
629 pos = rb_entry(nd, struct hist_entry, rb_node);
630 ret += hist_entry__fprintf(fp, pos, total_samples);
631 }
632
633 return ret;
634 }
635
636 static void register_idle_thread(void)
637 {
638 struct thread *thread = threads__findnew(0);
639
640 if (thread == NULL ||
641 thread__set_comm(thread, "[idle]")) {
642 fprintf(stderr, "problem inserting idle task.\n");
643 exit(-1);
644 }
645 }
646
647
648 static int __cmd_report(void)
649 {
650 unsigned long offset = 0;
651 unsigned long head = 0;
652 struct stat stat;
653 char *buf;
654 event_t *event;
655 int ret, rc = EXIT_FAILURE;
656 uint32_t size;
657 unsigned long total = 0, total_mmap = 0, total_comm = 0, total_unknown = 0;
658 char cwd[PATH_MAX], *cwdp = cwd;
659 int cwdlen;
660
661 register_idle_thread();
662
663 input = open(input_name, O_RDONLY);
664 if (input < 0) {
665 perror("failed to open file");
666 exit(-1);
667 }
668
669 ret = fstat(input, &stat);
670 if (ret < 0) {
671 perror("failed to stat file");
672 exit(-1);
673 }
674
675 if (!stat.st_size) {
676 fprintf(stderr, "zero-sized file, nothing to do!\n");
677 exit(0);
678 }
679
680 if (load_kernel() < 0) {
681 perror("failed to load kernel symbols");
682 return EXIT_FAILURE;
683 }
684
685 if (!full_paths) {
686 if (getcwd(cwd, sizeof(cwd)) == NULL) {
687 perror("failed to get the current directory");
688 return EXIT_FAILURE;
689 }
690 cwdlen = strlen(cwd);
691 } else {
692 cwdp = NULL;
693 cwdlen = 0;
694 }
695 remap:
696 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
697 MAP_SHARED, input, offset);
698 if (buf == MAP_FAILED) {
699 perror("failed to mmap file");
700 exit(-1);
701 }
702
703 more:
704 event = (event_t *)(buf + head);
705
706 size = event->header.size;
707 if (!size)
708 size = 8;
709
710 if (head + event->header.size >= page_size * mmap_window) {
711 unsigned long shift = page_size * (head / page_size);
712 int ret;
713
714 ret = munmap(buf, page_size * mmap_window);
715 assert(ret == 0);
716
717 offset += shift;
718 head -= shift;
719 goto remap;
720 }
721
722 size = event->header.size;
723 if (!size)
724 goto broken_event;
725
726 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) {
727 char level;
728 int show = 0;
729 struct dso *dso = NULL;
730 struct thread *thread = threads__findnew(event->ip.pid);
731 uint64_t ip = event->ip.ip;
732 struct map *map = NULL;
733
734 dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
735 (void *)(offset + head),
736 (void *)(long)(event->header.size),
737 event->header.misc,
738 event->ip.pid,
739 (void *)(long)ip);
740
741 if (thread == NULL) {
742 fprintf(stderr, "problem processing %d event, skipping it.\n",
743 event->header.type);
744 goto broken_event;
745 }
746
747 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
748 show = SHOW_KERNEL;
749 level = 'k';
750
751 dso = kernel_dso;
752
753 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
754
755 show = SHOW_USER;
756 level = '.';
757
758 map = thread__find_map(thread, ip);
759 if (map != NULL) {
760 dso = map->dso;
761 ip -= map->start + map->pgoff;
762 }
763
764 } else {
765 show = SHOW_HV;
766 level = 'H';
767 }
768
769 if (show & show_mask) {
770 struct symbol *sym = dso__find_symbol(dso, ip);
771
772 if (hist_entry__add(thread, map, dso, sym, ip, level)) {
773 fprintf(stderr,
774 "problem incrementing symbol count, skipping event\n");
775 goto broken_event;
776 }
777 }
778 total++;
779 } else switch (event->header.type) {
780 case PERF_EVENT_MMAP: {
781 struct thread *thread = threads__findnew(event->mmap.pid);
782 struct map *map = map__new(&event->mmap, cwdp, cwdlen);
783
784 dprintf("%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n",
785 (void *)(offset + head),
786 (void *)(long)(event->header.size),
787 (void *)(long)event->mmap.start,
788 (void *)(long)event->mmap.len,
789 (void *)(long)event->mmap.pgoff,
790 event->mmap.filename);
791
792 if (thread == NULL || map == NULL) {
793 if (verbose)
794 fprintf(stderr, "problem processing PERF_EVENT_MMAP, skipping event.\n");
795 goto broken_event;
796 }
797 thread__insert_map(thread, map);
798 total_mmap++;
799 break;
800 }
801 case PERF_EVENT_COMM: {
802 struct thread *thread = threads__findnew(event->comm.pid);
803
804 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
805 (void *)(offset + head),
806 (void *)(long)(event->header.size),
807 event->comm.comm, event->comm.pid);
808
809 if (thread == NULL ||
810 thread__set_comm(thread, event->comm.comm)) {
811 fprintf(stderr, "problem processing PERF_EVENT_COMM, skipping event.\n");
812 goto broken_event;
813 }
814 total_comm++;
815 break;
816 }
817 default: {
818 broken_event:
819 dprintf("%p [%p]: skipping unknown header type: %d\n",
820 (void *)(offset + head),
821 (void *)(long)(event->header.size),
822 event->header.type);
823
824 total_unknown++;
825
826 /*
827 * assume we lost track of the stream, check alignment, and
828 * increment a single u64 in the hope to catch on again 'soon'.
829 */
830
831 if (unlikely(head & 7))
832 head &= ~7ULL;
833
834 size = 8;
835 }
836 }
837
838 head += size;
839
840 if (offset + head < stat.st_size)
841 goto more;
842
843 rc = EXIT_SUCCESS;
844 close(input);
845
846 dprintf(" IP events: %10ld\n", total);
847 dprintf(" mmap events: %10ld\n", total_mmap);
848 dprintf(" comm events: %10ld\n", total_comm);
849 dprintf(" unknown events: %10ld\n", total_unknown);
850
851 if (dump_trace)
852 return 0;
853
854 if (verbose >= 2)
855 dsos__fprintf(stdout);
856
857 output__resort();
858 output__fprintf(stdout, total);
859
860 return rc;
861 }
862
863 static const char * const report_usage[] = {
864 "perf report [<options>] <command>",
865 NULL
866 };
867
868 static const struct option options[] = {
869 OPT_STRING('i', "input", &input_name, "file",
870 "input file name"),
871 OPT_BOOLEAN('v', "verbose", &verbose,
872 "be more verbose (show symbol address, etc)"),
873 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
874 "dump raw trace in ASCII"),
875 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
876 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
877 "sort by key(s): pid, comm, dso, symbol. Default: pid,symbol"),
878 OPT_BOOLEAN('P', "full-paths", &full_paths,
879 "Don't shorten the pathnames taking into account the cwd"),
880 OPT_END()
881 };
882
883 int cmd_report(int argc, const char **argv, const char *prefix)
884 {
885 symbol__init();
886
887 page_size = getpagesize();
888
889 parse_options(argc, argv, options, report_usage, 0);
890
891 setup_sorting();
892
893 setup_pager();
894
895 return __cmd_report();
896 }
This page took 0.050486 seconds and 6 git commands to generate.