perf_counter tools: Add color terminal output support
[linux-2.6-block.git] / Documentation / perf_counter / builtin-report.c
CommitLineData
bf9e1876
IM
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 */
16f762a2 8#include "builtin.h"
53cb8bc2 9
bf9e1876
IM
10#include "util/util.h"
11
8fc0321f 12#include "util/color.h"
35a50c8a 13#include "util/list.h"
a930d2c0 14#include "util/cache.h"
35a50c8a 15#include "util/rbtree.h"
a2928c42 16#include "util/symbol.h"
a0055ae2 17#include "util/string.h"
8fa66bdc 18
53cb8bc2
IM
19#include "perf.h"
20
21#include "util/parse-options.h"
22#include "util/parse-events.h"
23
8fa66bdc
ACM
24#define SHOW_KERNEL 1
25#define SHOW_USER 2
26#define SHOW_HV 4
27
23ac9cbe 28static char const *input_name = "perf.data";
450aaa2b 29static char *vmlinux = NULL;
bd74137e
IM
30
31static char default_sort_order[] = "comm,dso";
32static char *sort_order = default_sort_order;
33
8fa66bdc
ACM
34static int input;
35static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
36
97b07b69 37static int dump_trace = 0;
3502973d
IM
38#define dprintf(x...) do { if (dump_trace) printf(x); } while (0)
39
16f762a2 40static int verbose;
b78c07d4 41static int full_paths;
97b07b69 42
8fa66bdc
ACM
43static unsigned long page_size;
44static unsigned long mmap_window = 32;
45
53cb8bc2 46const char *perf_event_names[] = {
8fa66bdc
ACM
47 [PERF_EVENT_MMAP] = " PERF_EVENT_MMAP",
48 [PERF_EVENT_MUNMAP] = " PERF_EVENT_MUNMAP",
49 [PERF_EVENT_COMM] = " PERF_EVENT_COMM",
50};
51
52struct ip_event {
53 struct perf_event_header header;
54 __u64 ip;
55 __u32 pid, tid;
56};
75051724 57
8fa66bdc
ACM
58struct mmap_event {
59 struct perf_event_header header;
60 __u32 pid, tid;
61 __u64 start;
62 __u64 len;
63 __u64 pgoff;
64 char filename[PATH_MAX];
65};
75051724 66
8fa66bdc
ACM
67struct comm_event {
68 struct perf_event_header header;
75051724 69 __u32 pid, tid;
8fa66bdc
ACM
70 char comm[16];
71};
72
73typedef union event_union {
74 struct perf_event_header header;
75 struct ip_event ip;
76 struct mmap_event mmap;
77 struct comm_event comm;
78} event_t;
79
8fa66bdc
ACM
80static LIST_HEAD(dsos);
81static struct dso *kernel_dso;
82
83static void dsos__add(struct dso *dso)
84{
85 list_add_tail(&dso->node, &dsos);
86}
87
88static struct dso *dsos__find(const char *name)
89{
90 struct dso *pos;
91
92 list_for_each_entry(pos, &dsos, node)
93 if (strcmp(pos->name, name) == 0)
94 return pos;
95 return NULL;
96}
97
98static struct dso *dsos__findnew(const char *name)
99{
100 struct dso *dso = dsos__find(name);
b7a16eac 101 int nr;
8fa66bdc 102
4593bba8
IM
103 if (dso)
104 return dso;
105
106 dso = dso__new(name, 0);
107 if (!dso)
108 goto out_delete_dso;
8fa66bdc 109
bd74137e 110 nr = dso__load(dso, NULL, verbose);
4593bba8 111 if (nr < 0) {
bd74137e
IM
112 if (verbose)
113 fprintf(stderr, "Failed to open: %s\n", name);
4593bba8 114 goto out_delete_dso;
8fa66bdc 115 }
4593bba8
IM
116 if (!nr && verbose) {
117 fprintf(stderr,
118 "No symbols found in: %s, maybe install a debug package?\n",
119 name);
120 }
121
122 dsos__add(dso);
8fa66bdc
ACM
123
124 return dso;
125
126out_delete_dso:
127 dso__delete(dso);
128 return NULL;
129}
130
16f762a2 131static void dsos__fprintf(FILE *fp)
8fa66bdc
ACM
132{
133 struct dso *pos;
134
135 list_for_each_entry(pos, &dsos, node)
136 dso__fprintf(pos, fp);
137}
138
450aaa2b
PZ
139static int load_kernel(void)
140{
a827c875 141 int err;
450aaa2b 142
0085c954 143 kernel_dso = dso__new("[kernel]", 0);
450aaa2b 144 if (!kernel_dso)
a2928c42 145 return -1;
450aaa2b 146
bd74137e 147 err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose);
a2928c42
ACM
148 if (err) {
149 dso__delete(kernel_dso);
150 kernel_dso = NULL;
151 } else
152 dsos__add(kernel_dso);
450aaa2b 153
a2928c42 154 return err;
450aaa2b
PZ
155}
156
d80d338d
IM
157static char __cwd[PATH_MAX];
158static char *cwd = __cwd;
159static int cwdlen;
160
161static int strcommon(const char *pathname)
b78c07d4
ACM
162{
163 int n = 0;
164
165 while (pathname[n] == cwd[n] && n < cwdlen)
166 ++n;
167
168 return n;
169}
170
8fa66bdc
ACM
171struct map {
172 struct list_head node;
173 uint64_t start;
174 uint64_t end;
175 uint64_t pgoff;
176 struct dso *dso;
177};
178
d80d338d 179static struct map *map__new(struct mmap_event *event)
8fa66bdc
ACM
180{
181 struct map *self = malloc(sizeof(*self));
182
183 if (self != NULL) {
b78c07d4
ACM
184 const char *filename = event->filename;
185 char newfilename[PATH_MAX];
186
187 if (cwd) {
d80d338d
IM
188 int n = strcommon(filename);
189
b78c07d4
ACM
190 if (n == cwdlen) {
191 snprintf(newfilename, sizeof(newfilename),
192 ".%s", filename + n);
193 filename = newfilename;
194 }
195 }
196
8fa66bdc
ACM
197 self->start = event->start;
198 self->end = event->start + event->len;
199 self->pgoff = event->pgoff;
200
b78c07d4 201 self->dso = dsos__findnew(filename);
8fa66bdc
ACM
202 if (self->dso == NULL)
203 goto out_delete;
204 }
205 return self;
206out_delete:
207 free(self);
208 return NULL;
209}
210
3a4b8cc7
ACM
211struct thread;
212
8fa66bdc 213struct thread {
ce7e4365 214 struct rb_node rb_node;
8fa66bdc 215 struct list_head maps;
8fa66bdc
ACM
216 pid_t pid;
217 char *comm;
218};
219
220static struct thread *thread__new(pid_t pid)
221{
222 struct thread *self = malloc(sizeof(*self));
223
224 if (self != NULL) {
225 self->pid = pid;
8229289b 226 self->comm = malloc(32);
0a520c63 227 if (self->comm)
8229289b 228 snprintf(self->comm, 32, ":%d", self->pid);
8fa66bdc 229 INIT_LIST_HEAD(&self->maps);
8fa66bdc
ACM
230 }
231
232 return self;
233}
234
8fa66bdc
ACM
235static int thread__set_comm(struct thread *self, const char *comm)
236{
8229289b
PZ
237 if (self->comm)
238 free(self->comm);
8fa66bdc
ACM
239 self->comm = strdup(comm);
240 return self->comm ? 0 : -ENOMEM;
241}
242
16f762a2 243static struct rb_root threads;
eed4dcd4 244static struct thread *last_match;
8fa66bdc 245
ce7e4365 246static struct thread *threads__findnew(pid_t pid)
8fa66bdc 247{
ce7e4365
ACM
248 struct rb_node **p = &threads.rb_node;
249 struct rb_node *parent = NULL;
250 struct thread *th;
8fa66bdc 251
eed4dcd4
IM
252 /*
253 * Font-end cache - PID lookups come in blocks,
254 * so most of the time we dont have to look up
255 * the full rbtree:
256 */
257 if (last_match && last_match->pid == pid)
258 return last_match;
259
ce7e4365
ACM
260 while (*p != NULL) {
261 parent = *p;
262 th = rb_entry(parent, struct thread, rb_node);
8fa66bdc 263
eed4dcd4
IM
264 if (th->pid == pid) {
265 last_match = th;
ce7e4365 266 return th;
eed4dcd4 267 }
8fa66bdc 268
ce7e4365
ACM
269 if (pid < th->pid)
270 p = &(*p)->rb_left;
271 else
272 p = &(*p)->rb_right;
8fa66bdc
ACM
273 }
274
ce7e4365
ACM
275 th = thread__new(pid);
276 if (th != NULL) {
277 rb_link_node(&th->rb_node, parent, p);
278 rb_insert_color(&th->rb_node, &threads);
eed4dcd4 279 last_match = th;
ce7e4365 280 }
eed4dcd4 281
ce7e4365 282 return th;
8fa66bdc
ACM
283}
284
285static void thread__insert_map(struct thread *self, struct map *map)
286{
287 list_add_tail(&map->node, &self->maps);
288}
289
290static struct map *thread__find_map(struct thread *self, uint64_t ip)
291{
16f762a2
IM
292 struct map *pos;
293
8fa66bdc
ACM
294 if (self == NULL)
295 return NULL;
296
8fa66bdc
ACM
297 list_for_each_entry(pos, &self->maps, node)
298 if (ip >= pos->start && ip <= pos->end)
299 return pos;
300
301 return NULL;
302}
303
e7fb08b1
PZ
304/*
305 * histogram, sorted on item, collects counts
306 */
307
308static struct rb_root hist;
309
310struct hist_entry {
311 struct rb_node rb_node;
312
313 struct thread *thread;
314 struct map *map;
315 struct dso *dso;
316 struct symbol *sym;
317 uint64_t ip;
318 char level;
319
320 uint32_t count;
321};
322
1aa16738
PZ
323/*
324 * configurable sorting bits
325 */
326
327struct sort_entry {
328 struct list_head list;
329
ca8cdeef
PZ
330 char *header;
331
1aa16738 332 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
8229289b 333 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
1aa16738
PZ
334 size_t (*print)(FILE *fp, struct hist_entry *);
335};
336
8229289b
PZ
337/* --sort pid */
338
e7fb08b1 339static int64_t
1aa16738 340sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
e7fb08b1 341{
1aa16738
PZ
342 return right->thread->pid - left->thread->pid;
343}
344
345static size_t
346sort__thread_print(FILE *fp, struct hist_entry *self)
347{
71dd8945 348 return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
1aa16738 349}
e7fb08b1 350
1aa16738 351static struct sort_entry sort_thread = {
71dd8945 352 .header = " Command: Pid",
1aa16738
PZ
353 .cmp = sort__thread_cmp,
354 .print = sort__thread_print,
355};
356
8229289b
PZ
357/* --sort comm */
358
992444b1
PZ
359static int64_t
360sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
8229289b
PZ
361{
362 return right->thread->pid - left->thread->pid;
363}
364
365static int64_t
366sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
992444b1
PZ
367{
368 char *comm_l = left->thread->comm;
369 char *comm_r = right->thread->comm;
370
371 if (!comm_l || !comm_r) {
372 if (!comm_l && !comm_r)
373 return 0;
374 else if (!comm_l)
375 return -1;
376 else
377 return 1;
378 }
379
380 return strcmp(comm_l, comm_r);
381}
382
383static size_t
384sort__comm_print(FILE *fp, struct hist_entry *self)
385{
71dd8945 386 return fprintf(fp, "%16s", self->thread->comm);
992444b1
PZ
387}
388
389static struct sort_entry sort_comm = {
71dd8945 390 .header = " Command",
8229289b
PZ
391 .cmp = sort__comm_cmp,
392 .collapse = sort__comm_collapse,
393 .print = sort__comm_print,
992444b1
PZ
394};
395
8229289b
PZ
396/* --sort dso */
397
55e5ec41
PZ
398static int64_t
399sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
400{
401 struct dso *dso_l = left->dso;
402 struct dso *dso_r = right->dso;
403
404 if (!dso_l || !dso_r) {
405 if (!dso_l && !dso_r)
406 return 0;
407 else if (!dso_l)
408 return -1;
409 else
410 return 1;
411 }
412
413 return strcmp(dso_l->name, dso_r->name);
414}
415
416static size_t
417sort__dso_print(FILE *fp, struct hist_entry *self)
418{
0a520c63 419 if (self->dso)
71dd8945 420 return fprintf(fp, "%-25s", self->dso->name);
0a520c63 421
71dd8945 422 return fprintf(fp, "%016llx ", (__u64)self->ip);
55e5ec41
PZ
423}
424
425static struct sort_entry sort_dso = {
71dd8945 426 .header = "Shared Object ",
55e5ec41
PZ
427 .cmp = sort__dso_cmp,
428 .print = sort__dso_print,
429};
430
8229289b
PZ
431/* --sort symbol */
432
1aa16738
PZ
433static int64_t
434sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
435{
436 uint64_t ip_l, ip_r;
e7fb08b1
PZ
437
438 if (left->sym == right->sym)
439 return 0;
440
441 ip_l = left->sym ? left->sym->start : left->ip;
442 ip_r = right->sym ? right->sym->start : right->ip;
443
444 return (int64_t)(ip_r - ip_l);
445}
446
1aa16738
PZ
447static size_t
448sort__sym_print(FILE *fp, struct hist_entry *self)
449{
450 size_t ret = 0;
451
1aa16738 452 if (verbose)
71dd8945 453 ret += fprintf(fp, "%#018llx ", (__u64)self->ip);
0a520c63 454
0a520c63 455 if (self->sym)
71dd8945 456 ret += fprintf(fp, "%s", self->sym->name);
0a520c63 457 else
71dd8945 458 ret += fprintf(fp, "%#016llx", (__u64)self->ip);
1aa16738
PZ
459
460 return ret;
461}
462
463static struct sort_entry sort_sym = {
71dd8945 464 .header = "Symbol",
ca8cdeef
PZ
465 .cmp = sort__sym_cmp,
466 .print = sort__sym_print,
1aa16738
PZ
467};
468
8229289b
PZ
469static int sort__need_collapse = 0;
470
37f440cb
PZ
471struct sort_dimension {
472 char *name;
473 struct sort_entry *entry;
474 int taken;
475};
476
477static struct sort_dimension sort_dimensions[] = {
478 { .name = "pid", .entry = &sort_thread, },
992444b1 479 { .name = "comm", .entry = &sort_comm, },
55e5ec41 480 { .name = "dso", .entry = &sort_dso, },
37f440cb
PZ
481 { .name = "symbol", .entry = &sort_sym, },
482};
483
1aa16738
PZ
484static LIST_HEAD(hist_entry__sort_list);
485
37f440cb
PZ
486static int sort_dimension__add(char *tok)
487{
488 int i;
489
490 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
491 struct sort_dimension *sd = &sort_dimensions[i];
492
493 if (sd->taken)
494 continue;
495
5352f35d 496 if (strncasecmp(tok, sd->name, strlen(tok)))
37f440cb
PZ
497 continue;
498
8229289b
PZ
499 if (sd->entry->collapse)
500 sort__need_collapse = 1;
501
37f440cb
PZ
502 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
503 sd->taken = 1;
5352f35d 504
37f440cb
PZ
505 return 0;
506 }
507
508 return -ESRCH;
509}
510
1aa16738
PZ
511static int64_t
512hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
513{
514 struct sort_entry *se;
515 int64_t cmp = 0;
516
517 list_for_each_entry(se, &hist_entry__sort_list, list) {
518 cmp = se->cmp(left, right);
519 if (cmp)
520 break;
521 }
522
523 return cmp;
524}
525
8229289b
PZ
526static int64_t
527hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
528{
529 struct sort_entry *se;
530 int64_t cmp = 0;
531
532 list_for_each_entry(se, &hist_entry__sort_list, list) {
533 int64_t (*f)(struct hist_entry *, struct hist_entry *);
534
535 f = se->collapse ?: se->cmp;
536
537 cmp = f(left, right);
538 if (cmp)
539 break;
540 }
541
542 return cmp;
543}
544
1aa16738
PZ
545static size_t
546hist_entry__fprintf(FILE *fp, struct hist_entry *self, uint64_t total_samples)
547{
548 struct sort_entry *se;
549 size_t ret;
550
551 if (total_samples) {
8fc0321f
IM
552 double percent = self->count * 100.0 / total_samples;
553 char *color = PERF_COLOR_NORMAL;
554
555 /*
556 * We color high-overhead entries in red, low-overhead
557 * entries in green - and keep the middle ground normal:
558 */
559 if (percent >= 5.0)
560 color = PERF_COLOR_RED;
561 if (percent < 0.5)
562 color = PERF_COLOR_GREEN;
563
564 ret = color_fprintf(fp, color, " %6.2f%%",
1aa16738
PZ
565 (self->count * 100.0) / total_samples);
566 } else
567 ret = fprintf(fp, "%12d ", self->count);
568
71dd8945
PZ
569 list_for_each_entry(se, &hist_entry__sort_list, list) {
570 fprintf(fp, " ");
1aa16738 571 ret += se->print(fp, self);
71dd8945 572 }
1aa16738
PZ
573
574 ret += fprintf(fp, "\n");
575
576 return ret;
577}
578
579/*
580 * collect histogram counts
581 */
582
e7fb08b1
PZ
583static int
584hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
585 struct symbol *sym, uint64_t ip, char level)
8fa66bdc 586{
e7fb08b1
PZ
587 struct rb_node **p = &hist.rb_node;
588 struct rb_node *parent = NULL;
589 struct hist_entry *he;
590 struct hist_entry entry = {
591 .thread = thread,
592 .map = map,
593 .dso = dso,
594 .sym = sym,
595 .ip = ip,
596 .level = level,
597 .count = 1,
598 };
599 int cmp;
600
601 while (*p != NULL) {
602 parent = *p;
603 he = rb_entry(parent, struct hist_entry, rb_node);
604
605 cmp = hist_entry__cmp(&entry, he);
606
607 if (!cmp) {
608 he->count++;
609 return 0;
610 }
611
612 if (cmp < 0)
613 p = &(*p)->rb_left;
614 else
615 p = &(*p)->rb_right;
ce7e4365 616 }
e7fb08b1
PZ
617
618 he = malloc(sizeof(*he));
619 if (!he)
620 return -ENOMEM;
621 *he = entry;
622 rb_link_node(&he->rb_node, parent, p);
623 rb_insert_color(&he->rb_node, &hist);
624
625 return 0;
8fa66bdc
ACM
626}
627
8229289b
PZ
628static void hist_entry__free(struct hist_entry *he)
629{
630 free(he);
631}
632
633/*
634 * collapse the histogram
635 */
636
637static struct rb_root collapse_hists;
638
639static void collapse__insert_entry(struct hist_entry *he)
640{
641 struct rb_node **p = &collapse_hists.rb_node;
642 struct rb_node *parent = NULL;
643 struct hist_entry *iter;
644 int64_t cmp;
645
646 while (*p != NULL) {
647 parent = *p;
648 iter = rb_entry(parent, struct hist_entry, rb_node);
649
650 cmp = hist_entry__collapse(iter, he);
651
652 if (!cmp) {
653 iter->count += he->count;
654 hist_entry__free(he);
655 return;
656 }
657
658 if (cmp < 0)
659 p = &(*p)->rb_left;
660 else
661 p = &(*p)->rb_right;
662 }
663
664 rb_link_node(&he->rb_node, parent, p);
665 rb_insert_color(&he->rb_node, &collapse_hists);
666}
667
668static void collapse__resort(void)
669{
670 struct rb_node *next;
671 struct hist_entry *n;
672
673 if (!sort__need_collapse)
674 return;
675
676 next = rb_first(&hist);
677 while (next) {
678 n = rb_entry(next, struct hist_entry, rb_node);
679 next = rb_next(&n->rb_node);
680
681 rb_erase(&n->rb_node, &hist);
682 collapse__insert_entry(n);
683 }
684}
685
e7fb08b1
PZ
686/*
687 * reverse the map, sort on count.
688 */
689
690static struct rb_root output_hists;
691
692static void output__insert_entry(struct hist_entry *he)
3a4b8cc7 693{
e7fb08b1 694 struct rb_node **p = &output_hists.rb_node;
3a4b8cc7 695 struct rb_node *parent = NULL;
e7fb08b1 696 struct hist_entry *iter;
3a4b8cc7
ACM
697
698 while (*p != NULL) {
699 parent = *p;
e7fb08b1 700 iter = rb_entry(parent, struct hist_entry, rb_node);
3a4b8cc7 701
e7fb08b1 702 if (he->count > iter->count)
3a4b8cc7
ACM
703 p = &(*p)->rb_left;
704 else
705 p = &(*p)->rb_right;
706 }
707
e7fb08b1
PZ
708 rb_link_node(&he->rb_node, parent, p);
709 rb_insert_color(&he->rb_node, &output_hists);
3a4b8cc7
ACM
710}
711
e7fb08b1 712static void output__resort(void)
3a4b8cc7 713{
8229289b 714 struct rb_node *next;
e7fb08b1 715 struct hist_entry *n;
a4c43bea 716 struct rb_root *tree = &hist;
3a4b8cc7 717
8229289b 718 if (sort__need_collapse)
a4c43bea
ACM
719 tree = &collapse_hists;
720
721 next = rb_first(tree);
8229289b 722
e7fb08b1
PZ
723 while (next) {
724 n = rb_entry(next, struct hist_entry, rb_node);
725 next = rb_next(&n->rb_node);
3a4b8cc7 726
a4c43bea 727 rb_erase(&n->rb_node, tree);
e7fb08b1 728 output__insert_entry(n);
3a4b8cc7
ACM
729 }
730}
731
e7fb08b1 732static size_t output__fprintf(FILE *fp, uint64_t total_samples)
3a4b8cc7 733{
e7fb08b1 734 struct hist_entry *pos;
2d65537e 735 struct sort_entry *se;
3a4b8cc7
ACM
736 struct rb_node *nd;
737 size_t ret = 0;
738
71dd8945 739 fprintf(fp, "\n");
05ca061e
IM
740 fprintf(fp, "#\n");
741 fprintf(fp, "# (%Ld profiler events)\n", (__u64)total_samples);
ca8cdeef
PZ
742 fprintf(fp, "#\n");
743
744 fprintf(fp, "# Overhead");
745 list_for_each_entry(se, &hist_entry__sort_list, list)
71dd8945 746 fprintf(fp, " %s", se->header);
ca8cdeef
PZ
747 fprintf(fp, "\n");
748
749 fprintf(fp, "# ........");
2d65537e 750 list_for_each_entry(se, &hist_entry__sort_list, list) {
ca8cdeef
PZ
751 int i;
752
4593bba8 753 fprintf(fp, " ");
71dd8945 754 for (i = 0; i < strlen(se->header); i++)
ca8cdeef 755 fprintf(fp, ".");
2d65537e 756 }
ca8cdeef
PZ
757 fprintf(fp, "\n");
758
759 fprintf(fp, "#\n");
2d65537e 760
e7fb08b1
PZ
761 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
762 pos = rb_entry(nd, struct hist_entry, rb_node);
763 ret += hist_entry__fprintf(fp, pos, total_samples);
3a4b8cc7
ACM
764 }
765
bd74137e
IM
766 if (!strcmp(sort_order, default_sort_order)) {
767 fprintf(fp, "#\n");
71dd8945 768 fprintf(fp, "# (For more details, try: perf report --sort comm,dso,symbol)\n");
bd74137e
IM
769 fprintf(fp, "#\n");
770 }
71dd8945 771 fprintf(fp, "\n");
bd74137e 772
3a4b8cc7
ACM
773 return ret;
774}
775
436224a6
PZ
776static void register_idle_thread(void)
777{
778 struct thread *thread = threads__findnew(0);
779
780 if (thread == NULL ||
781 thread__set_comm(thread, "[idle]")) {
782 fprintf(stderr, "problem inserting idle task.\n");
783 exit(-1);
784 }
785}
786
d80d338d 787static unsigned long total = 0, total_mmap = 0, total_comm = 0, total_unknown = 0;
e7fb08b1 788
d80d338d 789static int
75051724
IM
790process_overflow_event(event_t *event, unsigned long offset, unsigned long head)
791{
792 char level;
793 int show = 0;
794 struct dso *dso = NULL;
795 struct thread *thread = threads__findnew(event->ip.pid);
796 uint64_t ip = event->ip.ip;
797 struct map *map = NULL;
798
799 dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
800 (void *)(offset + head),
801 (void *)(long)(event->header.size),
802 event->header.misc,
803 event->ip.pid,
804 (void *)(long)ip);
805
806 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
807
808 if (thread == NULL) {
809 fprintf(stderr, "problem processing %d event, skipping it.\n",
810 event->header.type);
811 return -1;
812 }
e7fb08b1 813
75051724
IM
814 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
815 show = SHOW_KERNEL;
816 level = 'k';
e7fb08b1 817
75051724 818 dso = kernel_dso;
ed966aac 819
75051724 820 dprintf(" ...... dso: %s\n", dso->name);
16f762a2 821
75051724 822 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
16f762a2 823
75051724
IM
824 show = SHOW_USER;
825 level = '.';
e7fb08b1 826
75051724
IM
827 map = thread__find_map(thread, ip);
828 if (map != NULL) {
829 dso = map->dso;
830 ip -= map->start + map->pgoff;
8fa66bdc 831 } else {
75051724
IM
832 /*
833 * If this is outside of all known maps,
834 * and is a negative address, try to look it
835 * up in the kernel dso, as it might be a
836 * vsyscall (which executes in user-mode):
837 */
838 if ((long long)ip < 0)
839 dso = kernel_dso;
8fa66bdc 840 }
75051724
IM
841 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
842
843 } else {
844 show = SHOW_HV;
845 level = 'H';
846 dprintf(" ...... dso: [hypervisor]\n");
847 }
8fa66bdc 848
75051724
IM
849 if (show & show_mask) {
850 struct symbol *sym = dso__find_symbol(dso, ip);
8fa66bdc 851
75051724
IM
852 if (hist_entry__add(thread, map, dso, sym, ip, level)) {
853 fprintf(stderr,
55717314 854 "problem incrementing symbol count, skipping event\n");
d80d338d 855 return -1;
ce7e4365 856 }
8fa66bdc 857 }
75051724 858 total++;
8fa66bdc 859
75051724
IM
860 return 0;
861}
3502973d 862
75051724
IM
863static int
864process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
865{
866 struct thread *thread = threads__findnew(event->mmap.pid);
867 struct map *map = map__new(&event->mmap);
868
869 dprintf("%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n",
870 (void *)(offset + head),
871 (void *)(long)(event->header.size),
872 (void *)(long)event->mmap.start,
873 (void *)(long)event->mmap.len,
874 (void *)(long)event->mmap.pgoff,
875 event->mmap.filename);
876
877 if (thread == NULL || map == NULL) {
878 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
df97992c 879 return 0;
75051724
IM
880 }
881
882 thread__insert_map(thread, map);
883 total_mmap++;
884
885 return 0;
886}
887
888static int
889process_comm_event(event_t *event, unsigned long offset, unsigned long head)
890{
891 struct thread *thread = threads__findnew(event->comm.pid);
892
893 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
894 (void *)(offset + head),
895 (void *)(long)(event->header.size),
896 event->comm.comm, event->comm.pid);
897
898 if (thread == NULL ||
899 thread__set_comm(thread, event->comm.comm)) {
900 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
901 return -1;
8fa66bdc 902 }
75051724
IM
903 total_comm++;
904
905 return 0;
906}
907
908static int
909process_event(event_t *event, unsigned long offset, unsigned long head)
910{
911 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW)
912 return process_overflow_event(event, offset, head);
913
914 switch (event->header.type) {
915 case PERF_EVENT_MMAP:
916 return process_mmap_event(event, offset, head);
917
918 case PERF_EVENT_COMM:
919 return process_comm_event(event, offset, head);
920
d11444df
IM
921 /*
922 * We dont process them right now but they are fine:
923 */
924 case PERF_EVENT_MUNMAP:
925 case PERF_EVENT_PERIOD:
926 case PERF_EVENT_THROTTLE:
927 case PERF_EVENT_UNTHROTTLE:
928 return 0;
929
d80d338d
IM
930 default:
931 return -1;
932 }
933
934 return 0;
935}
936
937static int __cmd_report(void)
938{
75051724 939 int ret, rc = EXIT_FAILURE;
d80d338d
IM
940 unsigned long offset = 0;
941 unsigned long head = 0;
942 struct stat stat;
d80d338d 943 event_t *event;
d80d338d 944 uint32_t size;
75051724 945 char *buf;
d80d338d
IM
946
947 register_idle_thread();
948
949 input = open(input_name, O_RDONLY);
950 if (input < 0) {
951 perror("failed to open file");
952 exit(-1);
953 }
954
955 ret = fstat(input, &stat);
956 if (ret < 0) {
957 perror("failed to stat file");
958 exit(-1);
959 }
960
961 if (!stat.st_size) {
962 fprintf(stderr, "zero-sized file, nothing to do!\n");
963 exit(0);
964 }
965
966 if (load_kernel() < 0) {
967 perror("failed to load kernel symbols");
968 return EXIT_FAILURE;
969 }
970
971 if (!full_paths) {
972 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
973 perror("failed to get the current directory");
974 return EXIT_FAILURE;
975 }
976 cwdlen = strlen(cwd);
977 } else {
978 cwd = NULL;
979 cwdlen = 0;
980 }
981remap:
982 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
983 MAP_SHARED, input, offset);
984 if (buf == MAP_FAILED) {
985 perror("failed to mmap file");
986 exit(-1);
987 }
988
989more:
990 event = (event_t *)(buf + head);
991
992 size = event->header.size;
993 if (!size)
994 size = 8;
995
996 if (head + event->header.size >= page_size * mmap_window) {
997 unsigned long shift = page_size * (head / page_size);
998 int ret;
999
1000 ret = munmap(buf, page_size * mmap_window);
1001 assert(ret == 0);
1002
1003 offset += shift;
1004 head -= shift;
1005 goto remap;
1006 }
1007
1008 size = event->header.size;
1009
1010 if (!size || process_event(event, offset, head) < 0) {
1011
3502973d
IM
1012 dprintf("%p [%p]: skipping unknown header type: %d\n",
1013 (void *)(offset + head),
1014 (void *)(long)(event->header.size),
1015 event->header.type);
b7a16eac 1016
3e706114 1017 total_unknown++;
6142f9ec
PZ
1018
1019 /*
1020 * assume we lost track of the stream, check alignment, and
1021 * increment a single u64 in the hope to catch on again 'soon'.
1022 */
1023
1024 if (unlikely(head & 7))
1025 head &= ~7ULL;
1026
1027 size = 8;
97b07b69 1028 }
8fa66bdc 1029
6142f9ec 1030 head += size;
f49515b1 1031
8fa66bdc
ACM
1032 if (offset + head < stat.st_size)
1033 goto more;
1034
1035 rc = EXIT_SUCCESS;
8fa66bdc 1036 close(input);
97b07b69 1037
3502973d
IM
1038 dprintf(" IP events: %10ld\n", total);
1039 dprintf(" mmap events: %10ld\n", total_mmap);
1040 dprintf(" comm events: %10ld\n", total_comm);
1041 dprintf(" unknown events: %10ld\n", total_unknown);
97b07b69 1042
3502973d 1043 if (dump_trace)
97b07b69 1044 return 0;
97b07b69 1045
e7fb08b1 1046 if (verbose >= 2)
16f762a2 1047 dsos__fprintf(stdout);
16f762a2 1048
8229289b 1049 collapse__resort();
e7fb08b1
PZ
1050 output__resort();
1051 output__fprintf(stdout, total);
8fa66bdc 1052
8fa66bdc
ACM
1053 return rc;
1054}
1055
53cb8bc2
IM
1056static const char * const report_usage[] = {
1057 "perf report [<options>] <command>",
1058 NULL
1059};
1060
1061static const struct option options[] = {
1062 OPT_STRING('i', "input", &input_name, "file",
1063 "input file name"),
815e777f
ACM
1064 OPT_BOOLEAN('v', "verbose", &verbose,
1065 "be more verbose (show symbol address, etc)"),
97b07b69
IM
1066 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1067 "dump raw trace in ASCII"),
450aaa2b 1068 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
63299f05
IM
1069 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1070 "sort by key(s): pid, comm, dso, symbol. Default: pid,symbol"),
b78c07d4
ACM
1071 OPT_BOOLEAN('P', "full-paths", &full_paths,
1072 "Don't shorten the pathnames taking into account the cwd"),
53cb8bc2
IM
1073 OPT_END()
1074};
1075
5352f35d
IM
1076static void setup_sorting(void)
1077{
1078 char *tmp, *tok, *str = strdup(sort_order);
1079
1080 for (tok = strtok_r(str, ", ", &tmp);
1081 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1082 if (sort_dimension__add(tok) < 0) {
1083 error("Unknown --sort key: `%s'", tok);
1084 usage_with_options(report_usage, options);
1085 }
1086 }
1087
1088 free(str);
1089}
1090
53cb8bc2
IM
1091int cmd_report(int argc, const char **argv, const char *prefix)
1092{
a2928c42 1093 symbol__init();
53cb8bc2
IM
1094
1095 page_size = getpagesize();
1096
1097 parse_options(argc, argv, options, report_usage, 0);
1098
1aa16738
PZ
1099 setup_sorting();
1100
a930d2c0
IM
1101 setup_pager();
1102
53cb8bc2
IM
1103 return __cmd_report();
1104}