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