Merge branch 'linus' into perfcounters/core
[linux-block.git] / tools / perf / 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 38#define dprintf(x...) do { if (dump_trace) printf(x); } while (0)
3efa1cc9 39#define cdprintf(x...) do { if (dump_trace) color_fprintf(stdout, color, x); } while (0)
3502973d 40
16f762a2 41static int verbose;
b78c07d4 42static int full_paths;
3dfabc74 43static int collapse_syscalls;
97b07b69 44
8fa66bdc
ACM
45static unsigned long page_size;
46static unsigned long mmap_window = 32;
47
3efa1cc9
IM
48struct ip_chain_event {
49 __u16 nr;
50 __u16 hv;
51 __u16 kernel;
52 __u16 user;
53 __u64 ips[];
54};
55
8fa66bdc
ACM
56struct ip_event {
57 struct perf_event_header header;
58 __u64 ip;
59 __u32 pid, tid;
3efa1cc9 60 unsigned char __more_data[];
8fa66bdc 61};
75051724 62
8fa66bdc
ACM
63struct mmap_event {
64 struct perf_event_header header;
65 __u32 pid, tid;
66 __u64 start;
67 __u64 len;
68 __u64 pgoff;
69 char filename[PATH_MAX];
70};
75051724 71
8fa66bdc
ACM
72struct comm_event {
73 struct perf_event_header header;
75051724 74 __u32 pid, tid;
8fa66bdc
ACM
75 char comm[16];
76};
77
62fc4453
PZ
78struct fork_event {
79 struct perf_event_header header;
80 __u32 pid, ppid;
81};
82
b2fef076 83struct period_event {
8fa66bdc 84 struct perf_event_header header;
b2fef076
IM
85 __u64 time;
86 __u64 id;
87 __u64 sample_period;
88};
89
90typedef union event_union {
91 struct perf_event_header header;
92 struct ip_event ip;
93 struct mmap_event mmap;
94 struct comm_event comm;
95 struct fork_event fork;
96 struct period_event period;
8fa66bdc
ACM
97} event_t;
98
8fa66bdc
ACM
99static LIST_HEAD(dsos);
100static struct dso *kernel_dso;
fc54db51 101static struct dso *vdso;
8fa66bdc
ACM
102
103static void dsos__add(struct dso *dso)
104{
105 list_add_tail(&dso->node, &dsos);
106}
107
108static struct dso *dsos__find(const char *name)
109{
110 struct dso *pos;
111
112 list_for_each_entry(pos, &dsos, node)
113 if (strcmp(pos->name, name) == 0)
114 return pos;
115 return NULL;
116}
117
118static struct dso *dsos__findnew(const char *name)
119{
120 struct dso *dso = dsos__find(name);
b7a16eac 121 int nr;
8fa66bdc 122
4593bba8
IM
123 if (dso)
124 return dso;
125
126 dso = dso__new(name, 0);
127 if (!dso)
128 goto out_delete_dso;
8fa66bdc 129
bd74137e 130 nr = dso__load(dso, NULL, verbose);
4593bba8 131 if (nr < 0) {
bd74137e
IM
132 if (verbose)
133 fprintf(stderr, "Failed to open: %s\n", name);
4593bba8 134 goto out_delete_dso;
8fa66bdc 135 }
4593bba8
IM
136 if (!nr && verbose) {
137 fprintf(stderr,
138 "No symbols found in: %s, maybe install a debug package?\n",
139 name);
140 }
141
142 dsos__add(dso);
8fa66bdc
ACM
143
144 return dso;
145
146out_delete_dso:
147 dso__delete(dso);
148 return NULL;
149}
150
16f762a2 151static void dsos__fprintf(FILE *fp)
8fa66bdc
ACM
152{
153 struct dso *pos;
154
155 list_for_each_entry(pos, &dsos, node)
156 dso__fprintf(pos, fp);
157}
158
729ff5e2 159static struct symbol *vdso__find_symbol(struct dso *dso, __u64 ip)
fc54db51
PZ
160{
161 return dso__find_symbol(kernel_dso, ip);
162}
163
450aaa2b
PZ
164static int load_kernel(void)
165{
a827c875 166 int err;
450aaa2b 167
0085c954 168 kernel_dso = dso__new("[kernel]", 0);
450aaa2b 169 if (!kernel_dso)
a2928c42 170 return -1;
450aaa2b 171
bd74137e 172 err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose);
a2928c42
ACM
173 if (err) {
174 dso__delete(kernel_dso);
175 kernel_dso = NULL;
176 } else
177 dsos__add(kernel_dso);
450aaa2b 178
fc54db51
PZ
179 vdso = dso__new("[vdso]", 0);
180 if (!vdso)
181 return -1;
182
183 vdso->find_symbol = vdso__find_symbol;
184
185 dsos__add(vdso);
186
a2928c42 187 return err;
450aaa2b
PZ
188}
189
d80d338d
IM
190static char __cwd[PATH_MAX];
191static char *cwd = __cwd;
192static int cwdlen;
193
194static int strcommon(const char *pathname)
b78c07d4
ACM
195{
196 int n = 0;
197
198 while (pathname[n] == cwd[n] && n < cwdlen)
199 ++n;
200
201 return n;
202}
203
8fa66bdc
ACM
204struct map {
205 struct list_head node;
729ff5e2
IM
206 __u64 start;
207 __u64 end;
208 __u64 pgoff;
209 __u64 (*map_ip)(struct map *, __u64);
8fa66bdc
ACM
210 struct dso *dso;
211};
212
729ff5e2 213static __u64 map__map_ip(struct map *map, __u64 ip)
fc54db51
PZ
214{
215 return ip - map->start + map->pgoff;
216}
217
729ff5e2 218static __u64 vdso__map_ip(struct map *map, __u64 ip)
fc54db51
PZ
219{
220 return ip;
221}
222
80d496be
PE
223static inline int is_anon_memory(const char *filename)
224{
225 return strcmp(filename, "//anon") == 0;
226}
227
d80d338d 228static struct map *map__new(struct mmap_event *event)
8fa66bdc
ACM
229{
230 struct map *self = malloc(sizeof(*self));
231
232 if (self != NULL) {
b78c07d4
ACM
233 const char *filename = event->filename;
234 char newfilename[PATH_MAX];
80d496be 235 int anon;
b78c07d4
ACM
236
237 if (cwd) {
d80d338d
IM
238 int n = strcommon(filename);
239
b78c07d4
ACM
240 if (n == cwdlen) {
241 snprintf(newfilename, sizeof(newfilename),
242 ".%s", filename + n);
243 filename = newfilename;
244 }
245 }
246
80d496be
PE
247 anon = is_anon_memory(filename);
248
249 if (anon) {
250 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
251 filename = newfilename;
252 }
253
8fa66bdc
ACM
254 self->start = event->start;
255 self->end = event->start + event->len;
256 self->pgoff = event->pgoff;
257
b78c07d4 258 self->dso = dsos__findnew(filename);
8fa66bdc
ACM
259 if (self->dso == NULL)
260 goto out_delete;
fc54db51 261
80d496be 262 if (self->dso == vdso || anon)
fc54db51
PZ
263 self->map_ip = vdso__map_ip;
264 else
265 self->map_ip = map__map_ip;
8fa66bdc
ACM
266 }
267 return self;
268out_delete:
269 free(self);
270 return NULL;
271}
272
62fc4453
PZ
273static struct map *map__clone(struct map *self)
274{
275 struct map *map = malloc(sizeof(*self));
276
277 if (!map)
278 return NULL;
279
280 memcpy(map, self, sizeof(*self));
281
282 return map;
283}
284
285static int map__overlap(struct map *l, struct map *r)
286{
287 if (l->start > r->start) {
288 struct map *t = l;
289 l = r;
290 r = t;
291 }
292
293 if (l->end > r->start)
294 return 1;
295
296 return 0;
297}
3a4b8cc7 298
9ac99545
ACM
299static size_t map__fprintf(struct map *self, FILE *fp)
300{
729ff5e2 301 return fprintf(fp, " %Lx-%Lx %Lx %s\n",
9ac99545
ACM
302 self->start, self->end, self->pgoff, self->dso->name);
303}
304
305
8fa66bdc 306struct thread {
ce7e4365 307 struct rb_node rb_node;
8fa66bdc 308 struct list_head maps;
8fa66bdc
ACM
309 pid_t pid;
310 char *comm;
311};
312
313static struct thread *thread__new(pid_t pid)
314{
315 struct thread *self = malloc(sizeof(*self));
316
317 if (self != NULL) {
318 self->pid = pid;
8229289b 319 self->comm = malloc(32);
0a520c63 320 if (self->comm)
8229289b 321 snprintf(self->comm, 32, ":%d", self->pid);
8fa66bdc 322 INIT_LIST_HEAD(&self->maps);
8fa66bdc
ACM
323 }
324
325 return self;
326}
327
8fa66bdc
ACM
328static int thread__set_comm(struct thread *self, const char *comm)
329{
8229289b
PZ
330 if (self->comm)
331 free(self->comm);
8fa66bdc
ACM
332 self->comm = strdup(comm);
333 return self->comm ? 0 : -ENOMEM;
334}
335
9ac99545
ACM
336static size_t thread__fprintf(struct thread *self, FILE *fp)
337{
338 struct map *pos;
339 size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
340
341 list_for_each_entry(pos, &self->maps, node)
342 ret += map__fprintf(pos, fp);
343
344 return ret;
345}
346
347
16f762a2 348static struct rb_root threads;
eed4dcd4 349static struct thread *last_match;
8fa66bdc 350
ce7e4365 351static struct thread *threads__findnew(pid_t pid)
8fa66bdc 352{
ce7e4365
ACM
353 struct rb_node **p = &threads.rb_node;
354 struct rb_node *parent = NULL;
355 struct thread *th;
8fa66bdc 356
eed4dcd4
IM
357 /*
358 * Font-end cache - PID lookups come in blocks,
359 * so most of the time we dont have to look up
360 * the full rbtree:
361 */
362 if (last_match && last_match->pid == pid)
363 return last_match;
364
ce7e4365
ACM
365 while (*p != NULL) {
366 parent = *p;
367 th = rb_entry(parent, struct thread, rb_node);
8fa66bdc 368
eed4dcd4
IM
369 if (th->pid == pid) {
370 last_match = th;
ce7e4365 371 return th;
eed4dcd4 372 }
8fa66bdc 373
ce7e4365
ACM
374 if (pid < th->pid)
375 p = &(*p)->rb_left;
376 else
377 p = &(*p)->rb_right;
8fa66bdc
ACM
378 }
379
ce7e4365
ACM
380 th = thread__new(pid);
381 if (th != NULL) {
382 rb_link_node(&th->rb_node, parent, p);
383 rb_insert_color(&th->rb_node, &threads);
eed4dcd4 384 last_match = th;
ce7e4365 385 }
eed4dcd4 386
ce7e4365 387 return th;
8fa66bdc
ACM
388}
389
390static void thread__insert_map(struct thread *self, struct map *map)
391{
62fc4453
PZ
392 struct map *pos, *tmp;
393
394 list_for_each_entry_safe(pos, tmp, &self->maps, node) {
395 if (map__overlap(pos, map)) {
396 list_del_init(&pos->node);
397 /* XXX leaks dsos */
398 free(pos);
399 }
400 }
401
8fa66bdc
ACM
402 list_add_tail(&map->node, &self->maps);
403}
404
62fc4453
PZ
405static int thread__fork(struct thread *self, struct thread *parent)
406{
407 struct map *map;
408
409 if (self->comm)
410 free(self->comm);
411 self->comm = strdup(parent->comm);
412 if (!self->comm)
413 return -ENOMEM;
414
415 list_for_each_entry(map, &parent->maps, node) {
416 struct map *new = map__clone(map);
417 if (!new)
418 return -ENOMEM;
419 thread__insert_map(self, new);
420 }
421
422 return 0;
423}
424
729ff5e2 425static struct map *thread__find_map(struct thread *self, __u64 ip)
8fa66bdc 426{
16f762a2
IM
427 struct map *pos;
428
8fa66bdc
ACM
429 if (self == NULL)
430 return NULL;
431
8fa66bdc
ACM
432 list_for_each_entry(pos, &self->maps, node)
433 if (ip >= pos->start && ip <= pos->end)
434 return pos;
435
436 return NULL;
437}
438
9ac99545
ACM
439static size_t threads__fprintf(FILE *fp)
440{
441 size_t ret = 0;
442 struct rb_node *nd;
443
444 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
445 struct thread *pos = rb_entry(nd, struct thread, rb_node);
446
447 ret += thread__fprintf(pos, fp);
448 }
449
450 return ret;
451}
452
e7fb08b1
PZ
453/*
454 * histogram, sorted on item, collects counts
455 */
456
457static struct rb_root hist;
458
459struct hist_entry {
460 struct rb_node rb_node;
461
462 struct thread *thread;
463 struct map *map;
464 struct dso *dso;
465 struct symbol *sym;
729ff5e2 466 __u64 ip;
e7fb08b1
PZ
467 char level;
468
729ff5e2 469 __u64 count;
e7fb08b1
PZ
470};
471
1aa16738
PZ
472/*
473 * configurable sorting bits
474 */
475
476struct sort_entry {
477 struct list_head list;
478
ca8cdeef
PZ
479 char *header;
480
1aa16738 481 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
8229289b 482 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
1aa16738
PZ
483 size_t (*print)(FILE *fp, struct hist_entry *);
484};
485
8229289b
PZ
486/* --sort pid */
487
e7fb08b1 488static int64_t
1aa16738 489sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
e7fb08b1 490{
1aa16738
PZ
491 return right->thread->pid - left->thread->pid;
492}
493
494static size_t
495sort__thread_print(FILE *fp, struct hist_entry *self)
496{
71dd8945 497 return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
1aa16738 498}
e7fb08b1 499
1aa16738 500static struct sort_entry sort_thread = {
71dd8945 501 .header = " Command: Pid",
1aa16738
PZ
502 .cmp = sort__thread_cmp,
503 .print = sort__thread_print,
504};
505
8229289b
PZ
506/* --sort comm */
507
992444b1
PZ
508static int64_t
509sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
8229289b
PZ
510{
511 return right->thread->pid - left->thread->pid;
512}
513
514static int64_t
515sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
992444b1
PZ
516{
517 char *comm_l = left->thread->comm;
518 char *comm_r = right->thread->comm;
519
520 if (!comm_l || !comm_r) {
521 if (!comm_l && !comm_r)
522 return 0;
523 else if (!comm_l)
524 return -1;
525 else
526 return 1;
527 }
528
529 return strcmp(comm_l, comm_r);
530}
531
532static size_t
533sort__comm_print(FILE *fp, struct hist_entry *self)
534{
71dd8945 535 return fprintf(fp, "%16s", self->thread->comm);
992444b1
PZ
536}
537
538static struct sort_entry sort_comm = {
8edd4286 539 .header = " Command",
8229289b
PZ
540 .cmp = sort__comm_cmp,
541 .collapse = sort__comm_collapse,
542 .print = sort__comm_print,
992444b1
PZ
543};
544
8229289b
PZ
545/* --sort dso */
546
55e5ec41
PZ
547static int64_t
548sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
549{
550 struct dso *dso_l = left->dso;
551 struct dso *dso_r = right->dso;
552
553 if (!dso_l || !dso_r) {
554 if (!dso_l && !dso_r)
555 return 0;
556 else if (!dso_l)
557 return -1;
558 else
559 return 1;
560 }
561
562 return strcmp(dso_l->name, dso_r->name);
563}
564
565static size_t
566sort__dso_print(FILE *fp, struct hist_entry *self)
567{
0a520c63 568 if (self->dso)
71dd8945 569 return fprintf(fp, "%-25s", self->dso->name);
0a520c63 570
71dd8945 571 return fprintf(fp, "%016llx ", (__u64)self->ip);
55e5ec41
PZ
572}
573
574static struct sort_entry sort_dso = {
71dd8945 575 .header = "Shared Object ",
55e5ec41
PZ
576 .cmp = sort__dso_cmp,
577 .print = sort__dso_print,
578};
579
8229289b
PZ
580/* --sort symbol */
581
1aa16738
PZ
582static int64_t
583sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
584{
729ff5e2 585 __u64 ip_l, ip_r;
e7fb08b1
PZ
586
587 if (left->sym == right->sym)
588 return 0;
589
590 ip_l = left->sym ? left->sym->start : left->ip;
591 ip_r = right->sym ? right->sym->start : right->ip;
592
593 return (int64_t)(ip_r - ip_l);
594}
595
1aa16738
PZ
596static size_t
597sort__sym_print(FILE *fp, struct hist_entry *self)
598{
599 size_t ret = 0;
600
1aa16738 601 if (verbose)
71dd8945 602 ret += fprintf(fp, "%#018llx ", (__u64)self->ip);
0a520c63 603
8edd4286
IM
604 if (self->sym) {
605 ret += fprintf(fp, "[%c] %s",
606 self->dso == kernel_dso ? 'k' : '.', self->sym->name);
607 } else {
71dd8945 608 ret += fprintf(fp, "%#016llx", (__u64)self->ip);
8edd4286 609 }
1aa16738
PZ
610
611 return ret;
612}
613
614static struct sort_entry sort_sym = {
71dd8945 615 .header = "Symbol",
ca8cdeef
PZ
616 .cmp = sort__sym_cmp,
617 .print = sort__sym_print,
1aa16738
PZ
618};
619
8229289b
PZ
620static int sort__need_collapse = 0;
621
37f440cb 622struct sort_dimension {
8edd4286
IM
623 char *name;
624 struct sort_entry *entry;
625 int taken;
37f440cb
PZ
626};
627
628static struct sort_dimension sort_dimensions[] = {
629 { .name = "pid", .entry = &sort_thread, },
992444b1 630 { .name = "comm", .entry = &sort_comm, },
55e5ec41 631 { .name = "dso", .entry = &sort_dso, },
37f440cb
PZ
632 { .name = "symbol", .entry = &sort_sym, },
633};
634
1aa16738
PZ
635static LIST_HEAD(hist_entry__sort_list);
636
37f440cb
PZ
637static int sort_dimension__add(char *tok)
638{
639 int i;
640
641 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
642 struct sort_dimension *sd = &sort_dimensions[i];
643
644 if (sd->taken)
645 continue;
646
5352f35d 647 if (strncasecmp(tok, sd->name, strlen(tok)))
37f440cb
PZ
648 continue;
649
8229289b
PZ
650 if (sd->entry->collapse)
651 sort__need_collapse = 1;
652
37f440cb
PZ
653 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
654 sd->taken = 1;
5352f35d 655
37f440cb
PZ
656 return 0;
657 }
658
659 return -ESRCH;
660}
661
1aa16738
PZ
662static int64_t
663hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
664{
665 struct sort_entry *se;
666 int64_t cmp = 0;
667
668 list_for_each_entry(se, &hist_entry__sort_list, list) {
669 cmp = se->cmp(left, right);
670 if (cmp)
671 break;
672 }
673
674 return cmp;
675}
676
8229289b
PZ
677static int64_t
678hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
679{
680 struct sort_entry *se;
681 int64_t cmp = 0;
682
683 list_for_each_entry(se, &hist_entry__sort_list, list) {
684 int64_t (*f)(struct hist_entry *, struct hist_entry *);
685
686 f = se->collapse ?: se->cmp;
687
688 cmp = f(left, right);
689 if (cmp)
690 break;
691 }
692
693 return cmp;
694}
695
1aa16738 696static size_t
729ff5e2 697hist_entry__fprintf(FILE *fp, struct hist_entry *self, __u64 total_samples)
1aa16738
PZ
698{
699 struct sort_entry *se;
700 size_t ret;
701
702 if (total_samples) {
8fc0321f
IM
703 double percent = self->count * 100.0 / total_samples;
704 char *color = PERF_COLOR_NORMAL;
705
706 /*
aefcf37b
IM
707 * We color high-overhead entries in red, mid-overhead
708 * entries in green - and keep the low overhead places
709 * normal:
8fc0321f 710 */
aefcf37b 711 if (percent >= 5.0) {
8fc0321f 712 color = PERF_COLOR_RED;
aefcf37b
IM
713 } else {
714 if (percent >= 0.5)
715 color = PERF_COLOR_GREEN;
716 }
8fc0321f
IM
717
718 ret = color_fprintf(fp, color, " %6.2f%%",
1aa16738
PZ
719 (self->count * 100.0) / total_samples);
720 } else
729ff5e2 721 ret = fprintf(fp, "%12Ld ", self->count);
1aa16738 722
71dd8945
PZ
723 list_for_each_entry(se, &hist_entry__sort_list, list) {
724 fprintf(fp, " ");
1aa16738 725 ret += se->print(fp, self);
71dd8945 726 }
1aa16738
PZ
727
728 ret += fprintf(fp, "\n");
729
730 return ret;
731}
732
733/*
734 * collect histogram counts
735 */
736
e7fb08b1
PZ
737static int
738hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
729ff5e2 739 struct symbol *sym, __u64 ip, char level, __u64 count)
8fa66bdc 740{
e7fb08b1
PZ
741 struct rb_node **p = &hist.rb_node;
742 struct rb_node *parent = NULL;
743 struct hist_entry *he;
744 struct hist_entry entry = {
745 .thread = thread,
746 .map = map,
747 .dso = dso,
748 .sym = sym,
749 .ip = ip,
750 .level = level,
ea1900e5 751 .count = count,
e7fb08b1
PZ
752 };
753 int cmp;
754
755 while (*p != NULL) {
756 parent = *p;
757 he = rb_entry(parent, struct hist_entry, rb_node);
758
759 cmp = hist_entry__cmp(&entry, he);
760
761 if (!cmp) {
ea1900e5 762 he->count += count;
e7fb08b1
PZ
763 return 0;
764 }
765
766 if (cmp < 0)
767 p = &(*p)->rb_left;
768 else
769 p = &(*p)->rb_right;
ce7e4365 770 }
e7fb08b1
PZ
771
772 he = malloc(sizeof(*he));
773 if (!he)
774 return -ENOMEM;
775 *he = entry;
776 rb_link_node(&he->rb_node, parent, p);
777 rb_insert_color(&he->rb_node, &hist);
778
779 return 0;
8fa66bdc
ACM
780}
781
8229289b
PZ
782static void hist_entry__free(struct hist_entry *he)
783{
784 free(he);
785}
786
787/*
788 * collapse the histogram
789 */
790
791static struct rb_root collapse_hists;
792
793static void collapse__insert_entry(struct hist_entry *he)
794{
795 struct rb_node **p = &collapse_hists.rb_node;
796 struct rb_node *parent = NULL;
797 struct hist_entry *iter;
798 int64_t cmp;
799
800 while (*p != NULL) {
801 parent = *p;
802 iter = rb_entry(parent, struct hist_entry, rb_node);
803
804 cmp = hist_entry__collapse(iter, he);
805
806 if (!cmp) {
807 iter->count += he->count;
808 hist_entry__free(he);
809 return;
810 }
811
812 if (cmp < 0)
813 p = &(*p)->rb_left;
814 else
815 p = &(*p)->rb_right;
816 }
817
818 rb_link_node(&he->rb_node, parent, p);
819 rb_insert_color(&he->rb_node, &collapse_hists);
820}
821
822static void collapse__resort(void)
823{
824 struct rb_node *next;
825 struct hist_entry *n;
826
827 if (!sort__need_collapse)
828 return;
829
830 next = rb_first(&hist);
831 while (next) {
832 n = rb_entry(next, struct hist_entry, rb_node);
833 next = rb_next(&n->rb_node);
834
835 rb_erase(&n->rb_node, &hist);
836 collapse__insert_entry(n);
837 }
838}
839
e7fb08b1
PZ
840/*
841 * reverse the map, sort on count.
842 */
843
844static struct rb_root output_hists;
845
846static void output__insert_entry(struct hist_entry *he)
3a4b8cc7 847{
e7fb08b1 848 struct rb_node **p = &output_hists.rb_node;
3a4b8cc7 849 struct rb_node *parent = NULL;
e7fb08b1 850 struct hist_entry *iter;
3a4b8cc7
ACM
851
852 while (*p != NULL) {
853 parent = *p;
e7fb08b1 854 iter = rb_entry(parent, struct hist_entry, rb_node);
3a4b8cc7 855
e7fb08b1 856 if (he->count > iter->count)
3a4b8cc7
ACM
857 p = &(*p)->rb_left;
858 else
859 p = &(*p)->rb_right;
860 }
861
e7fb08b1
PZ
862 rb_link_node(&he->rb_node, parent, p);
863 rb_insert_color(&he->rb_node, &output_hists);
3a4b8cc7
ACM
864}
865
e7fb08b1 866static void output__resort(void)
3a4b8cc7 867{
8229289b 868 struct rb_node *next;
e7fb08b1 869 struct hist_entry *n;
a4c43bea 870 struct rb_root *tree = &hist;
3a4b8cc7 871
8229289b 872 if (sort__need_collapse)
a4c43bea
ACM
873 tree = &collapse_hists;
874
875 next = rb_first(tree);
8229289b 876
e7fb08b1
PZ
877 while (next) {
878 n = rb_entry(next, struct hist_entry, rb_node);
879 next = rb_next(&n->rb_node);
3a4b8cc7 880
a4c43bea 881 rb_erase(&n->rb_node, tree);
e7fb08b1 882 output__insert_entry(n);
3a4b8cc7
ACM
883 }
884}
885
729ff5e2 886static size_t output__fprintf(FILE *fp, __u64 total_samples)
3a4b8cc7 887{
e7fb08b1 888 struct hist_entry *pos;
2d65537e 889 struct sort_entry *se;
3a4b8cc7
ACM
890 struct rb_node *nd;
891 size_t ret = 0;
892
71dd8945 893 fprintf(fp, "\n");
05ca061e 894 fprintf(fp, "#\n");
2debbc83 895 fprintf(fp, "# (%Ld samples)\n", (__u64)total_samples);
ca8cdeef
PZ
896 fprintf(fp, "#\n");
897
898 fprintf(fp, "# Overhead");
899 list_for_each_entry(se, &hist_entry__sort_list, list)
71dd8945 900 fprintf(fp, " %s", se->header);
ca8cdeef
PZ
901 fprintf(fp, "\n");
902
903 fprintf(fp, "# ........");
2d65537e 904 list_for_each_entry(se, &hist_entry__sort_list, list) {
ca8cdeef
PZ
905 int i;
906
4593bba8 907 fprintf(fp, " ");
71dd8945 908 for (i = 0; i < strlen(se->header); i++)
ca8cdeef 909 fprintf(fp, ".");
2d65537e 910 }
ca8cdeef
PZ
911 fprintf(fp, "\n");
912
913 fprintf(fp, "#\n");
2d65537e 914
e7fb08b1
PZ
915 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
916 pos = rb_entry(nd, struct hist_entry, rb_node);
917 ret += hist_entry__fprintf(fp, pos, total_samples);
3a4b8cc7
ACM
918 }
919
bd74137e
IM
920 if (!strcmp(sort_order, default_sort_order)) {
921 fprintf(fp, "#\n");
71dd8945 922 fprintf(fp, "# (For more details, try: perf report --sort comm,dso,symbol)\n");
bd74137e
IM
923 fprintf(fp, "#\n");
924 }
71dd8945 925 fprintf(fp, "\n");
bd74137e 926
3a4b8cc7
ACM
927 return ret;
928}
929
436224a6
PZ
930static void register_idle_thread(void)
931{
932 struct thread *thread = threads__findnew(0);
933
934 if (thread == NULL ||
935 thread__set_comm(thread, "[idle]")) {
936 fprintf(stderr, "problem inserting idle task.\n");
937 exit(-1);
938 }
939}
940
62fc4453
PZ
941static unsigned long total = 0,
942 total_mmap = 0,
943 total_comm = 0,
944 total_fork = 0,
945 total_unknown = 0;
e7fb08b1 946
d80d338d 947static int
75051724
IM
948process_overflow_event(event_t *event, unsigned long offset, unsigned long head)
949{
950 char level;
951 int show = 0;
952 struct dso *dso = NULL;
953 struct thread *thread = threads__findnew(event->ip.pid);
729ff5e2
IM
954 __u64 ip = event->ip.ip;
955 __u64 period = 1;
75051724 956 struct map *map = NULL;
3efa1cc9
IM
957 void *more_data = event->ip.__more_data;
958 struct ip_chain_event *chain;
75051724 959
3efa1cc9
IM
960 if (event->header.type & PERF_SAMPLE_PERIOD) {
961 period = *(__u64 *)more_data;
962 more_data += sizeof(__u64);
963 }
ea1900e5 964
4502d77c 965 dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p period: %Ld\n",
75051724
IM
966 (void *)(offset + head),
967 (void *)(long)(event->header.size),
968 event->header.misc,
969 event->ip.pid,
4502d77c 970 (void *)(long)ip,
ea1900e5 971 (long long)period);
75051724 972
3efa1cc9
IM
973 if (event->header.type & PERF_SAMPLE_CALLCHAIN) {
974 int i;
975
976 chain = (void *)more_data;
977
978 if (dump_trace) {
979 dprintf("... chain: u:%d, k:%d, nr:%d\n",
980 chain->user,
981 chain->kernel,
982 chain->nr);
983
984 for (i = 0; i < chain->nr; i++)
e2eae0f5 985 dprintf("..... %2d: %016Lx\n", i, chain->ips[i]);
3efa1cc9 986 }
3dfabc74
IM
987 if (collapse_syscalls) {
988 /*
989 * Find the all-but-last kernel entry
990 * amongst the call-chains - to get
991 * to the level of system calls:
992 */
993 if (chain->kernel >= 2)
994 ip = chain->ips[chain->kernel-2];
995 }
3efa1cc9
IM
996 }
997
75051724
IM
998 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
999
1000 if (thread == NULL) {
1001 fprintf(stderr, "problem processing %d event, skipping it.\n",
1002 event->header.type);
1003 return -1;
1004 }
e7fb08b1 1005
75051724
IM
1006 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
1007 show = SHOW_KERNEL;
1008 level = 'k';
e7fb08b1 1009
75051724 1010 dso = kernel_dso;
ed966aac 1011
75051724 1012 dprintf(" ...... dso: %s\n", dso->name);
16f762a2 1013
75051724 1014 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
16f762a2 1015
75051724
IM
1016 show = SHOW_USER;
1017 level = '.';
e7fb08b1 1018
75051724
IM
1019 map = thread__find_map(thread, ip);
1020 if (map != NULL) {
fc54db51 1021 ip = map->map_ip(map, ip);
75051724 1022 dso = map->dso;
8fa66bdc 1023 } else {
75051724
IM
1024 /*
1025 * If this is outside of all known maps,
1026 * and is a negative address, try to look it
1027 * up in the kernel dso, as it might be a
1028 * vsyscall (which executes in user-mode):
1029 */
1030 if ((long long)ip < 0)
1031 dso = kernel_dso;
8fa66bdc 1032 }
75051724
IM
1033 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
1034
1035 } else {
1036 show = SHOW_HV;
1037 level = 'H';
1038 dprintf(" ...... dso: [hypervisor]\n");
1039 }
8fa66bdc 1040
75051724 1041 if (show & show_mask) {
fc54db51
PZ
1042 struct symbol *sym = NULL;
1043
1044 if (dso)
1045 sym = dso->find_symbol(dso, ip);
8fa66bdc 1046
ea1900e5 1047 if (hist_entry__add(thread, map, dso, sym, ip, level, period)) {
75051724 1048 fprintf(stderr,
55717314 1049 "problem incrementing symbol count, skipping event\n");
d80d338d 1050 return -1;
ce7e4365 1051 }
8fa66bdc 1052 }
ea1900e5 1053 total += period;
8fa66bdc 1054
75051724
IM
1055 return 0;
1056}
3502973d 1057
75051724
IM
1058static int
1059process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
1060{
1061 struct thread *thread = threads__findnew(event->mmap.pid);
1062 struct map *map = map__new(&event->mmap);
1063
62fc4453 1064 dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
75051724
IM
1065 (void *)(offset + head),
1066 (void *)(long)(event->header.size),
62fc4453 1067 event->mmap.pid,
75051724
IM
1068 (void *)(long)event->mmap.start,
1069 (void *)(long)event->mmap.len,
1070 (void *)(long)event->mmap.pgoff,
1071 event->mmap.filename);
1072
1073 if (thread == NULL || map == NULL) {
1074 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
df97992c 1075 return 0;
75051724
IM
1076 }
1077
1078 thread__insert_map(thread, map);
1079 total_mmap++;
1080
1081 return 0;
1082}
1083
1084static int
1085process_comm_event(event_t *event, unsigned long offset, unsigned long head)
1086{
1087 struct thread *thread = threads__findnew(event->comm.pid);
1088
1089 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
1090 (void *)(offset + head),
1091 (void *)(long)(event->header.size),
1092 event->comm.comm, event->comm.pid);
1093
1094 if (thread == NULL ||
1095 thread__set_comm(thread, event->comm.comm)) {
1096 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
1097 return -1;
8fa66bdc 1098 }
75051724
IM
1099 total_comm++;
1100
1101 return 0;
1102}
1103
62fc4453
PZ
1104static int
1105process_fork_event(event_t *event, unsigned long offset, unsigned long head)
1106{
1107 struct thread *thread = threads__findnew(event->fork.pid);
1108 struct thread *parent = threads__findnew(event->fork.ppid);
1109
1110 dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
1111 (void *)(offset + head),
1112 (void *)(long)(event->header.size),
1113 event->fork.pid, event->fork.ppid);
1114
1115 if (!thread || !parent || thread__fork(thread, parent)) {
1116 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
1117 return -1;
1118 }
1119 total_fork++;
1120
1121 return 0;
1122}
1123
b2fef076
IM
1124static int
1125process_period_event(event_t *event, unsigned long offset, unsigned long head)
1126{
1127 dprintf("%p [%p]: PERF_EVENT_PERIOD: time:%Ld, id:%Ld: period:%Ld\n",
1128 (void *)(offset + head),
1129 (void *)(long)(event->header.size),
1130 event->period.time,
1131 event->period.id,
1132 event->period.sample_period);
1133
1134 return 0;
1135}
1136
8465b050
IM
1137static void trace_event(event_t *event)
1138{
1139 unsigned char *raw_event = (void *)event;
3efa1cc9 1140 char *color = PERF_COLOR_BLUE;
8465b050
IM
1141 int i, j;
1142
1143 if (!dump_trace)
1144 return;
1145
3efa1cc9
IM
1146 dprintf(".");
1147 cdprintf("\n. ... raw event: size %d bytes\n", event->header.size);
8465b050
IM
1148
1149 for (i = 0; i < event->header.size; i++) {
3efa1cc9
IM
1150 if ((i & 15) == 0) {
1151 dprintf(".");
1152 cdprintf(" %04x: ", i);
1153 }
8465b050 1154
3efa1cc9 1155 cdprintf(" %02x", raw_event[i]);
8465b050
IM
1156
1157 if (((i & 15) == 15) || i == event->header.size-1) {
3efa1cc9 1158 cdprintf(" ");
8465b050 1159 for (j = 0; j < 15-(i & 15); j++)
3efa1cc9 1160 cdprintf(" ");
8465b050
IM
1161 for (j = 0; j < (i & 15); j++) {
1162 if (isprint(raw_event[i-15+j]))
3efa1cc9 1163 cdprintf("%c", raw_event[i-15+j]);
8465b050 1164 else
3efa1cc9 1165 cdprintf(".");
8465b050 1166 }
3efa1cc9 1167 cdprintf("\n");
8465b050
IM
1168 }
1169 }
1170 dprintf(".\n");
1171}
1172
75051724
IM
1173static int
1174process_event(event_t *event, unsigned long offset, unsigned long head)
1175{
8465b050
IM
1176 trace_event(event);
1177
75051724
IM
1178 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW)
1179 return process_overflow_event(event, offset, head);
1180
1181 switch (event->header.type) {
1182 case PERF_EVENT_MMAP:
1183 return process_mmap_event(event, offset, head);
1184
1185 case PERF_EVENT_COMM:
1186 return process_comm_event(event, offset, head);
1187
62fc4453
PZ
1188 case PERF_EVENT_FORK:
1189 return process_fork_event(event, offset, head);
1190
b2fef076
IM
1191 case PERF_EVENT_PERIOD:
1192 return process_period_event(event, offset, head);
d11444df
IM
1193 /*
1194 * We dont process them right now but they are fine:
1195 */
62fc4453 1196
d11444df
IM
1197 case PERF_EVENT_THROTTLE:
1198 case PERF_EVENT_UNTHROTTLE:
1199 return 0;
1200
d80d338d
IM
1201 default:
1202 return -1;
1203 }
1204
1205 return 0;
1206}
1207
1208static int __cmd_report(void)
1209{
75051724 1210 int ret, rc = EXIT_FAILURE;
d80d338d
IM
1211 unsigned long offset = 0;
1212 unsigned long head = 0;
1213 struct stat stat;
d80d338d 1214 event_t *event;
d80d338d 1215 uint32_t size;
75051724 1216 char *buf;
d80d338d
IM
1217
1218 register_idle_thread();
1219
1220 input = open(input_name, O_RDONLY);
1221 if (input < 0) {
a14832ff
IM
1222 fprintf(stderr, " failed to open file: %s", input_name);
1223 if (!strcmp(input_name, "perf.data"))
1224 fprintf(stderr, " (try 'perf record' first)");
1225 fprintf(stderr, "\n");
d80d338d
IM
1226 exit(-1);
1227 }
1228
1229 ret = fstat(input, &stat);
1230 if (ret < 0) {
1231 perror("failed to stat file");
1232 exit(-1);
1233 }
1234
1235 if (!stat.st_size) {
1236 fprintf(stderr, "zero-sized file, nothing to do!\n");
1237 exit(0);
1238 }
1239
1240 if (load_kernel() < 0) {
1241 perror("failed to load kernel symbols");
1242 return EXIT_FAILURE;
1243 }
1244
1245 if (!full_paths) {
1246 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
1247 perror("failed to get the current directory");
1248 return EXIT_FAILURE;
1249 }
1250 cwdlen = strlen(cwd);
1251 } else {
1252 cwd = NULL;
1253 cwdlen = 0;
1254 }
1255remap:
1256 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1257 MAP_SHARED, input, offset);
1258 if (buf == MAP_FAILED) {
1259 perror("failed to mmap file");
1260 exit(-1);
1261 }
1262
1263more:
1264 event = (event_t *)(buf + head);
1265
1266 size = event->header.size;
1267 if (!size)
1268 size = 8;
1269
1270 if (head + event->header.size >= page_size * mmap_window) {
1271 unsigned long shift = page_size * (head / page_size);
1272 int ret;
1273
1274 ret = munmap(buf, page_size * mmap_window);
1275 assert(ret == 0);
1276
1277 offset += shift;
1278 head -= shift;
1279 goto remap;
1280 }
1281
1282 size = event->header.size;
1283
8465b050 1284 dprintf("\n%p [%p]: event: %d\n",
b2fef076
IM
1285 (void *)(offset + head),
1286 (void *)(long)event->header.size,
1287 event->header.type);
1288
d80d338d
IM
1289 if (!size || process_event(event, offset, head) < 0) {
1290
3502973d
IM
1291 dprintf("%p [%p]: skipping unknown header type: %d\n",
1292 (void *)(offset + head),
1293 (void *)(long)(event->header.size),
1294 event->header.type);
b7a16eac 1295
3e706114 1296 total_unknown++;
6142f9ec
PZ
1297
1298 /*
1299 * assume we lost track of the stream, check alignment, and
1300 * increment a single u64 in the hope to catch on again 'soon'.
1301 */
1302
1303 if (unlikely(head & 7))
1304 head &= ~7ULL;
1305
1306 size = 8;
97b07b69 1307 }
8fa66bdc 1308
6142f9ec 1309 head += size;
f49515b1 1310
8fa66bdc
ACM
1311 if (offset + head < stat.st_size)
1312 goto more;
1313
1314 rc = EXIT_SUCCESS;
8fa66bdc 1315 close(input);
97b07b69 1316
3502973d
IM
1317 dprintf(" IP events: %10ld\n", total);
1318 dprintf(" mmap events: %10ld\n", total_mmap);
1319 dprintf(" comm events: %10ld\n", total_comm);
62fc4453 1320 dprintf(" fork events: %10ld\n", total_fork);
3502973d 1321 dprintf(" unknown events: %10ld\n", total_unknown);
97b07b69 1322
3502973d 1323 if (dump_trace)
97b07b69 1324 return 0;
97b07b69 1325
9ac99545
ACM
1326 if (verbose >= 3)
1327 threads__fprintf(stdout);
1328
e7fb08b1 1329 if (verbose >= 2)
16f762a2 1330 dsos__fprintf(stdout);
16f762a2 1331
8229289b 1332 collapse__resort();
e7fb08b1
PZ
1333 output__resort();
1334 output__fprintf(stdout, total);
8fa66bdc 1335
8fa66bdc
ACM
1336 return rc;
1337}
1338
53cb8bc2
IM
1339static const char * const report_usage[] = {
1340 "perf report [<options>] <command>",
1341 NULL
1342};
1343
1344static const struct option options[] = {
1345 OPT_STRING('i', "input", &input_name, "file",
1346 "input file name"),
815e777f
ACM
1347 OPT_BOOLEAN('v', "verbose", &verbose,
1348 "be more verbose (show symbol address, etc)"),
97b07b69
IM
1349 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1350 "dump raw trace in ASCII"),
450aaa2b 1351 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
63299f05
IM
1352 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1353 "sort by key(s): pid, comm, dso, symbol. Default: pid,symbol"),
b78c07d4
ACM
1354 OPT_BOOLEAN('P', "full-paths", &full_paths,
1355 "Don't shorten the pathnames taking into account the cwd"),
3dfabc74
IM
1356 OPT_BOOLEAN('S', "syscalls", &collapse_syscalls,
1357 "show per syscall summary overhead, using call graph"),
53cb8bc2
IM
1358 OPT_END()
1359};
1360
5352f35d
IM
1361static void setup_sorting(void)
1362{
1363 char *tmp, *tok, *str = strdup(sort_order);
1364
1365 for (tok = strtok_r(str, ", ", &tmp);
1366 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1367 if (sort_dimension__add(tok) < 0) {
1368 error("Unknown --sort key: `%s'", tok);
1369 usage_with_options(report_usage, options);
1370 }
1371 }
1372
1373 free(str);
1374}
1375
53cb8bc2
IM
1376int cmd_report(int argc, const char **argv, const char *prefix)
1377{
a2928c42 1378 symbol__init();
53cb8bc2
IM
1379
1380 page_size = getpagesize();
1381
edc52dea 1382 argc = parse_options(argc, argv, options, report_usage, 0);
53cb8bc2 1383
1aa16738
PZ
1384 setup_sorting();
1385
edc52dea
IM
1386 /*
1387 * Any (unrecognized) arguments left?
1388 */
1389 if (argc)
1390 usage_with_options(report_usage, options);
1391
a930d2c0
IM
1392 setup_pager();
1393
53cb8bc2
IM
1394 return __cmd_report();
1395}