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