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