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