333d31269e3fc6daa3c5c3edaf93718c89a51b0d
[GitHub/mt8127/android_kernel_alcatel_ttab.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 char __cwd[PATH_MAX];
151 static char *cwd = __cwd;
152 static int cwdlen;
153
154 static int strcommon(const char *pathname)
155 {
156 int n = 0;
157
158 while (pathname[n] == cwd[n] && n < cwdlen)
159 ++n;
160
161 return n;
162 }
163
164 struct map {
165 struct list_head node;
166 uint64_t start;
167 uint64_t end;
168 uint64_t pgoff;
169 struct dso *dso;
170 };
171
172 static struct map *map__new(struct mmap_event *event)
173 {
174 struct map *self = malloc(sizeof(*self));
175
176 if (self != NULL) {
177 const char *filename = event->filename;
178 char newfilename[PATH_MAX];
179
180 if (cwd) {
181 int n = strcommon(filename);
182
183 if (n == cwdlen) {
184 snprintf(newfilename, sizeof(newfilename),
185 ".%s", filename + n);
186 filename = newfilename;
187 }
188 }
189
190 self->start = event->start;
191 self->end = event->start + event->len;
192 self->pgoff = event->pgoff;
193
194 self->dso = dsos__findnew(filename);
195 if (self->dso == NULL)
196 goto out_delete;
197 }
198 return self;
199 out_delete:
200 free(self);
201 return NULL;
202 }
203
204 struct thread;
205
206 struct thread {
207 struct rb_node rb_node;
208 struct list_head maps;
209 pid_t pid;
210 char *comm;
211 };
212
213 static struct thread *thread__new(pid_t pid)
214 {
215 struct thread *self = malloc(sizeof(*self));
216
217 if (self != NULL) {
218 self->pid = pid;
219 self->comm = malloc(32);
220 if (self->comm)
221 snprintf(self->comm, 32, ":%d", self->pid);
222 INIT_LIST_HEAD(&self->maps);
223 }
224
225 return self;
226 }
227
228 static int thread__set_comm(struct thread *self, const char *comm)
229 {
230 if (self->comm)
231 free(self->comm);
232 self->comm = strdup(comm);
233 return self->comm ? 0 : -ENOMEM;
234 }
235
236 static struct rb_root threads;
237 static struct thread *last_match;
238
239 static struct thread *threads__findnew(pid_t pid)
240 {
241 struct rb_node **p = &threads.rb_node;
242 struct rb_node *parent = NULL;
243 struct thread *th;
244
245 /*
246 * Font-end cache - PID lookups come in blocks,
247 * so most of the time we dont have to look up
248 * the full rbtree:
249 */
250 if (last_match && last_match->pid == pid)
251 return last_match;
252
253 while (*p != NULL) {
254 parent = *p;
255 th = rb_entry(parent, struct thread, rb_node);
256
257 if (th->pid == pid) {
258 last_match = th;
259 return th;
260 }
261
262 if (pid < th->pid)
263 p = &(*p)->rb_left;
264 else
265 p = &(*p)->rb_right;
266 }
267
268 th = thread__new(pid);
269 if (th != NULL) {
270 rb_link_node(&th->rb_node, parent, p);
271 rb_insert_color(&th->rb_node, &threads);
272 last_match = th;
273 }
274
275 return th;
276 }
277
278 static void thread__insert_map(struct thread *self, struct map *map)
279 {
280 list_add_tail(&map->node, &self->maps);
281 }
282
283 static struct map *thread__find_map(struct thread *self, uint64_t ip)
284 {
285 struct map *pos;
286
287 if (self == NULL)
288 return NULL;
289
290 list_for_each_entry(pos, &self->maps, node)
291 if (ip >= pos->start && ip <= pos->end)
292 return pos;
293
294 return NULL;
295 }
296
297 /*
298 * histogram, sorted on item, collects counts
299 */
300
301 static struct rb_root hist;
302
303 struct hist_entry {
304 struct rb_node rb_node;
305
306 struct thread *thread;
307 struct map *map;
308 struct dso *dso;
309 struct symbol *sym;
310 uint64_t ip;
311 char level;
312
313 uint32_t count;
314 };
315
316 /*
317 * configurable sorting bits
318 */
319
320 struct sort_entry {
321 struct list_head list;
322
323 char *header;
324
325 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
326 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
327 size_t (*print)(FILE *fp, struct hist_entry *);
328 };
329
330 /* --sort pid */
331
332 static int64_t
333 sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
334 {
335 return right->thread->pid - left->thread->pid;
336 }
337
338 static size_t
339 sort__thread_print(FILE *fp, struct hist_entry *self)
340 {
341 return fprintf(fp, " %16s:%5d", self->thread->comm ?: "", self->thread->pid);
342 }
343
344 static struct sort_entry sort_thread = {
345 .header = " Command: Pid ",
346 .cmp = sort__thread_cmp,
347 .print = sort__thread_print,
348 };
349
350 /* --sort comm */
351
352 static int64_t
353 sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
354 {
355 return right->thread->pid - left->thread->pid;
356 }
357
358 static int64_t
359 sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
360 {
361 char *comm_l = left->thread->comm;
362 char *comm_r = right->thread->comm;
363
364 if (!comm_l || !comm_r) {
365 if (!comm_l && !comm_r)
366 return 0;
367 else if (!comm_l)
368 return -1;
369 else
370 return 1;
371 }
372
373 return strcmp(comm_l, comm_r);
374 }
375
376 static size_t
377 sort__comm_print(FILE *fp, struct hist_entry *self)
378 {
379 return fprintf(fp, " %16s", self->thread->comm);
380 }
381
382 static struct sort_entry sort_comm = {
383 .header = " Command",
384 .cmp = sort__comm_cmp,
385 .collapse = sort__comm_collapse,
386 .print = sort__comm_print,
387 };
388
389 /* --sort dso */
390
391 static int64_t
392 sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
393 {
394 struct dso *dso_l = left->dso;
395 struct dso *dso_r = right->dso;
396
397 if (!dso_l || !dso_r) {
398 if (!dso_l && !dso_r)
399 return 0;
400 else if (!dso_l)
401 return -1;
402 else
403 return 1;
404 }
405
406 return strcmp(dso_l->name, dso_r->name);
407 }
408
409 static size_t
410 sort__dso_print(FILE *fp, struct hist_entry *self)
411 {
412 if (self->dso)
413 return fprintf(fp, " %-25s", self->dso->name);
414
415 return fprintf(fp, " %016llx", (__u64)self->ip);
416 }
417
418 static struct sort_entry sort_dso = {
419 .header = " Shared Object ",
420 .cmp = sort__dso_cmp,
421 .print = sort__dso_print,
422 };
423
424 /* --sort symbol */
425
426 static int64_t
427 sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
428 {
429 uint64_t ip_l, ip_r;
430
431 if (left->sym == right->sym)
432 return 0;
433
434 ip_l = left->sym ? left->sym->start : left->ip;
435 ip_r = right->sym ? right->sym->start : right->ip;
436
437 return (int64_t)(ip_r - ip_l);
438 }
439
440 static size_t
441 sort__sym_print(FILE *fp, struct hist_entry *self)
442 {
443 size_t ret = 0;
444
445 if (verbose)
446 ret += fprintf(fp, " %#018llx", (__u64)self->ip);
447
448 if (self->dso)
449 ret += fprintf(fp, " %s: ", self->dso->name);
450 else
451 ret += fprintf(fp, " %#016llx: ", (__u64)self->ip);
452
453 if (self->sym)
454 ret += fprintf(fp, "%s", self->sym->name);
455 else
456 ret += fprintf(fp, "%#016llx", (__u64)self->ip);
457
458 return ret;
459 }
460
461 static struct sort_entry sort_sym = {
462 .header = " Shared Object: Symbol",
463 .cmp = sort__sym_cmp,
464 .print = sort__sym_print,
465 };
466
467 static int sort__need_collapse = 0;
468
469 struct sort_dimension {
470 char *name;
471 struct sort_entry *entry;
472 int taken;
473 };
474
475 static struct sort_dimension sort_dimensions[] = {
476 { .name = "pid", .entry = &sort_thread, },
477 { .name = "comm", .entry = &sort_comm, },
478 { .name = "dso", .entry = &sort_dso, },
479 { .name = "symbol", .entry = &sort_sym, },
480 };
481
482 static LIST_HEAD(hist_entry__sort_list);
483
484 static int sort_dimension__add(char *tok)
485 {
486 int i;
487
488 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
489 struct sort_dimension *sd = &sort_dimensions[i];
490
491 if (sd->taken)
492 continue;
493
494 if (strncasecmp(tok, sd->name, strlen(tok)))
495 continue;
496
497 if (sd->entry->collapse)
498 sort__need_collapse = 1;
499
500 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
501 sd->taken = 1;
502
503 return 0;
504 }
505
506 return -ESRCH;
507 }
508
509 static int64_t
510 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
511 {
512 struct sort_entry *se;
513 int64_t cmp = 0;
514
515 list_for_each_entry(se, &hist_entry__sort_list, list) {
516 cmp = se->cmp(left, right);
517 if (cmp)
518 break;
519 }
520
521 return cmp;
522 }
523
524 static int64_t
525 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
526 {
527 struct sort_entry *se;
528 int64_t cmp = 0;
529
530 list_for_each_entry(se, &hist_entry__sort_list, list) {
531 int64_t (*f)(struct hist_entry *, struct hist_entry *);
532
533 f = se->collapse ?: se->cmp;
534
535 cmp = f(left, right);
536 if (cmp)
537 break;
538 }
539
540 return cmp;
541 }
542
543 static size_t
544 hist_entry__fprintf(FILE *fp, struct hist_entry *self, uint64_t total_samples)
545 {
546 struct sort_entry *se;
547 size_t ret;
548
549 if (total_samples) {
550 ret = fprintf(fp, " %6.2f%%",
551 (self->count * 100.0) / total_samples);
552 } else
553 ret = fprintf(fp, "%12d ", self->count);
554
555 list_for_each_entry(se, &hist_entry__sort_list, list)
556 ret += se->print(fp, self);
557
558 ret += fprintf(fp, "\n");
559
560 return ret;
561 }
562
563 /*
564 * collect histogram counts
565 */
566
567 static int
568 hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
569 struct symbol *sym, uint64_t ip, char level)
570 {
571 struct rb_node **p = &hist.rb_node;
572 struct rb_node *parent = NULL;
573 struct hist_entry *he;
574 struct hist_entry entry = {
575 .thread = thread,
576 .map = map,
577 .dso = dso,
578 .sym = sym,
579 .ip = ip,
580 .level = level,
581 .count = 1,
582 };
583 int cmp;
584
585 while (*p != NULL) {
586 parent = *p;
587 he = rb_entry(parent, struct hist_entry, rb_node);
588
589 cmp = hist_entry__cmp(&entry, he);
590
591 if (!cmp) {
592 he->count++;
593 return 0;
594 }
595
596 if (cmp < 0)
597 p = &(*p)->rb_left;
598 else
599 p = &(*p)->rb_right;
600 }
601
602 he = malloc(sizeof(*he));
603 if (!he)
604 return -ENOMEM;
605 *he = entry;
606 rb_link_node(&he->rb_node, parent, p);
607 rb_insert_color(&he->rb_node, &hist);
608
609 return 0;
610 }
611
612 static void hist_entry__free(struct hist_entry *he)
613 {
614 free(he);
615 }
616
617 /*
618 * collapse the histogram
619 */
620
621 static struct rb_root collapse_hists;
622
623 static void collapse__insert_entry(struct hist_entry *he)
624 {
625 struct rb_node **p = &collapse_hists.rb_node;
626 struct rb_node *parent = NULL;
627 struct hist_entry *iter;
628 int64_t cmp;
629
630 while (*p != NULL) {
631 parent = *p;
632 iter = rb_entry(parent, struct hist_entry, rb_node);
633
634 cmp = hist_entry__collapse(iter, he);
635
636 if (!cmp) {
637 iter->count += he->count;
638 hist_entry__free(he);
639 return;
640 }
641
642 if (cmp < 0)
643 p = &(*p)->rb_left;
644 else
645 p = &(*p)->rb_right;
646 }
647
648 rb_link_node(&he->rb_node, parent, p);
649 rb_insert_color(&he->rb_node, &collapse_hists);
650 }
651
652 static void collapse__resort(void)
653 {
654 struct rb_node *next;
655 struct hist_entry *n;
656
657 if (!sort__need_collapse)
658 return;
659
660 next = rb_first(&hist);
661 while (next) {
662 n = rb_entry(next, struct hist_entry, rb_node);
663 next = rb_next(&n->rb_node);
664
665 rb_erase(&n->rb_node, &hist);
666 collapse__insert_entry(n);
667 }
668 }
669
670 /*
671 * reverse the map, sort on count.
672 */
673
674 static struct rb_root output_hists;
675
676 static void output__insert_entry(struct hist_entry *he)
677 {
678 struct rb_node **p = &output_hists.rb_node;
679 struct rb_node *parent = NULL;
680 struct hist_entry *iter;
681
682 while (*p != NULL) {
683 parent = *p;
684 iter = rb_entry(parent, struct hist_entry, rb_node);
685
686 if (he->count > iter->count)
687 p = &(*p)->rb_left;
688 else
689 p = &(*p)->rb_right;
690 }
691
692 rb_link_node(&he->rb_node, parent, p);
693 rb_insert_color(&he->rb_node, &output_hists);
694 }
695
696 static void output__resort(void)
697 {
698 struct rb_node *next;
699 struct hist_entry *n;
700
701 if (sort__need_collapse)
702 next = rb_first(&collapse_hists);
703 else
704 next = rb_first(&hist);
705
706 while (next) {
707 n = rb_entry(next, struct hist_entry, rb_node);
708 next = rb_next(&n->rb_node);
709
710 rb_erase(&n->rb_node, &hist);
711 output__insert_entry(n);
712 }
713 }
714
715 static size_t output__fprintf(FILE *fp, uint64_t total_samples)
716 {
717 struct hist_entry *pos;
718 struct sort_entry *se;
719 struct rb_node *nd;
720 size_t ret = 0;
721
722 fprintf(fp, "#\n");
723
724 fprintf(fp, "# Overhead");
725 list_for_each_entry(se, &hist_entry__sort_list, list)
726 fprintf(fp, " %s", se->header);
727 fprintf(fp, "\n");
728
729 fprintf(fp, "# ........");
730 list_for_each_entry(se, &hist_entry__sort_list, list) {
731 int i;
732
733 fprintf(fp, " ");
734 for (i = 0; i < strlen(se->header)-1; i++)
735 fprintf(fp, ".");
736 }
737 fprintf(fp, "\n");
738
739 fprintf(fp, "#\n");
740
741 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
742 pos = rb_entry(nd, struct hist_entry, rb_node);
743 ret += hist_entry__fprintf(fp, pos, total_samples);
744 }
745
746 return ret;
747 }
748
749 static void register_idle_thread(void)
750 {
751 struct thread *thread = threads__findnew(0);
752
753 if (thread == NULL ||
754 thread__set_comm(thread, "[idle]")) {
755 fprintf(stderr, "problem inserting idle task.\n");
756 exit(-1);
757 }
758 }
759
760 static unsigned long total = 0, total_mmap = 0, total_comm = 0, total_unknown = 0;
761
762 static int
763 process_event(event_t *event, unsigned long offset, unsigned long head)
764 {
765 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) {
766 char level;
767 int show = 0;
768 struct dso *dso = NULL;
769 struct thread *thread = threads__findnew(event->ip.pid);
770 uint64_t ip = event->ip.ip;
771 struct map *map = NULL;
772
773 dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
774 (void *)(offset + head),
775 (void *)(long)(event->header.size),
776 event->header.misc,
777 event->ip.pid,
778 (void *)(long)ip);
779
780 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
781
782 if (thread == NULL) {
783 fprintf(stderr, "problem processing %d event, skipping it.\n",
784 event->header.type);
785 return -1;
786 }
787
788 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
789 show = SHOW_KERNEL;
790 level = 'k';
791
792 dso = kernel_dso;
793
794 dprintf(" ...... dso: %s\n", dso->name);
795
796 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
797
798 show = SHOW_USER;
799 level = '.';
800
801 map = thread__find_map(thread, ip);
802 if (map != NULL) {
803 dso = map->dso;
804 ip -= map->start + map->pgoff;
805 } else {
806 /*
807 * If this is outside of all known maps,
808 * and is a negative address, try to look it
809 * up in the kernel dso, as it might be a
810 * vsyscall (which executes in user-mode):
811 */
812 if ((long long)ip < 0)
813 dso = kernel_dso;
814 }
815 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
816
817 } else {
818 show = SHOW_HV;
819 level = 'H';
820 dprintf(" ...... dso: [hypervisor]\n");
821 }
822
823 if (show & show_mask) {
824 struct symbol *sym = dso__find_symbol(dso, ip);
825
826 if (hist_entry__add(thread, map, dso, sym, ip, level)) {
827 fprintf(stderr,
828 "problem incrementing symbol count, skipping event\n");
829 return -1;
830 }
831 }
832 total++;
833 } else switch (event->header.type) {
834 case PERF_EVENT_MMAP: {
835 struct thread *thread = threads__findnew(event->mmap.pid);
836 struct map *map = map__new(&event->mmap);
837
838 dprintf("%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n",
839 (void *)(offset + head),
840 (void *)(long)(event->header.size),
841 (void *)(long)event->mmap.start,
842 (void *)(long)event->mmap.len,
843 (void *)(long)event->mmap.pgoff,
844 event->mmap.filename);
845
846 if (thread == NULL || map == NULL) {
847 if (verbose)
848 fprintf(stderr, "problem processing PERF_EVENT_MMAP, skipping event.\n");
849 return -1;
850 }
851 thread__insert_map(thread, map);
852 total_mmap++;
853 break;
854 }
855 case PERF_EVENT_COMM: {
856 struct thread *thread = threads__findnew(event->comm.pid);
857
858 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
859 (void *)(offset + head),
860 (void *)(long)(event->header.size),
861 event->comm.comm, event->comm.pid);
862
863 if (thread == NULL ||
864 thread__set_comm(thread, event->comm.comm)) {
865 fprintf(stderr, "problem processing PERF_EVENT_COMM, skipping event.\n");
866 return -1;
867 }
868 total_comm++;
869 break;
870 }
871 default:
872 return -1;
873 }
874
875 return 0;
876 }
877
878 static int __cmd_report(void)
879 {
880 unsigned long offset = 0;
881 unsigned long head = 0;
882 struct stat stat;
883 char *buf;
884 event_t *event;
885 int ret, rc = EXIT_FAILURE;
886 uint32_t size;
887
888 register_idle_thread();
889
890 input = open(input_name, O_RDONLY);
891 if (input < 0) {
892 perror("failed to open file");
893 exit(-1);
894 }
895
896 ret = fstat(input, &stat);
897 if (ret < 0) {
898 perror("failed to stat file");
899 exit(-1);
900 }
901
902 if (!stat.st_size) {
903 fprintf(stderr, "zero-sized file, nothing to do!\n");
904 exit(0);
905 }
906
907 if (load_kernel() < 0) {
908 perror("failed to load kernel symbols");
909 return EXIT_FAILURE;
910 }
911
912 if (!full_paths) {
913 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
914 perror("failed to get the current directory");
915 return EXIT_FAILURE;
916 }
917 cwdlen = strlen(cwd);
918 } else {
919 cwd = NULL;
920 cwdlen = 0;
921 }
922 remap:
923 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
924 MAP_SHARED, input, offset);
925 if (buf == MAP_FAILED) {
926 perror("failed to mmap file");
927 exit(-1);
928 }
929
930 more:
931 event = (event_t *)(buf + head);
932
933 size = event->header.size;
934 if (!size)
935 size = 8;
936
937 if (head + event->header.size >= page_size * mmap_window) {
938 unsigned long shift = page_size * (head / page_size);
939 int ret;
940
941 ret = munmap(buf, page_size * mmap_window);
942 assert(ret == 0);
943
944 offset += shift;
945 head -= shift;
946 goto remap;
947 }
948
949 size = event->header.size;
950
951 if (!size || process_event(event, offset, head) < 0) {
952
953 dprintf("%p [%p]: skipping unknown header type: %d\n",
954 (void *)(offset + head),
955 (void *)(long)(event->header.size),
956 event->header.type);
957
958 total_unknown++;
959
960 /*
961 * assume we lost track of the stream, check alignment, and
962 * increment a single u64 in the hope to catch on again 'soon'.
963 */
964
965 if (unlikely(head & 7))
966 head &= ~7ULL;
967
968 size = 8;
969 }
970
971 head += size;
972
973 if (offset + head < stat.st_size)
974 goto more;
975
976 rc = EXIT_SUCCESS;
977 close(input);
978
979 dprintf(" IP events: %10ld\n", total);
980 dprintf(" mmap events: %10ld\n", total_mmap);
981 dprintf(" comm events: %10ld\n", total_comm);
982 dprintf(" unknown events: %10ld\n", total_unknown);
983
984 if (dump_trace)
985 return 0;
986
987 if (verbose >= 2)
988 dsos__fprintf(stdout);
989
990 collapse__resort();
991 output__resort();
992 output__fprintf(stdout, total);
993
994 return rc;
995 }
996
997 static const char * const report_usage[] = {
998 "perf report [<options>] <command>",
999 NULL
1000 };
1001
1002 static const struct option options[] = {
1003 OPT_STRING('i', "input", &input_name, "file",
1004 "input file name"),
1005 OPT_BOOLEAN('v', "verbose", &verbose,
1006 "be more verbose (show symbol address, etc)"),
1007 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1008 "dump raw trace in ASCII"),
1009 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
1010 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1011 "sort by key(s): pid, comm, dso, symbol. Default: pid,symbol"),
1012 OPT_BOOLEAN('P', "full-paths", &full_paths,
1013 "Don't shorten the pathnames taking into account the cwd"),
1014 OPT_END()
1015 };
1016
1017 static void setup_sorting(void)
1018 {
1019 char *tmp, *tok, *str = strdup(sort_order);
1020
1021 for (tok = strtok_r(str, ", ", &tmp);
1022 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1023 if (sort_dimension__add(tok) < 0) {
1024 error("Unknown --sort key: `%s'", tok);
1025 usage_with_options(report_usage, options);
1026 }
1027 }
1028
1029 free(str);
1030 }
1031
1032 int cmd_report(int argc, const char **argv, const char *prefix)
1033 {
1034 symbol__init();
1035
1036 page_size = getpagesize();
1037
1038 parse_options(argc, argv, options, report_usage, 0);
1039
1040 setup_sorting();
1041
1042 setup_pager();
1043
1044 return __cmd_report();
1045 }