perf-report: Add bare minimum PERF_EVENT_READ parsing
[deliverable/linux.git] / tools / perf / builtin-report.c
CommitLineData
bf9e1876
IM
1/*
2 * builtin-report.c
3 *
4 * Builtin report command: Analyze the perf.data input file,
5 * look up and read DSOs and symbol information and display
6 * a histogram of results, along various sorting keys.
7 */
16f762a2 8#include "builtin.h"
53cb8bc2 9
bf9e1876
IM
10#include "util/util.h"
11
8fc0321f 12#include "util/color.h"
35a50c8a 13#include "util/list.h"
a930d2c0 14#include "util/cache.h"
35a50c8a 15#include "util/rbtree.h"
a2928c42 16#include "util/symbol.h"
a0055ae2 17#include "util/string.h"
8fa66bdc 18
53cb8bc2 19#include "perf.h"
7c6a1c65 20#include "util/header.h"
53cb8bc2
IM
21
22#include "util/parse-options.h"
23#include "util/parse-events.h"
24
8fa66bdc
ACM
25#define SHOW_KERNEL 1
26#define SHOW_USER 2
27#define SHOW_HV 4
28
23ac9cbe 29static char const *input_name = "perf.data";
450aaa2b 30static char *vmlinux = NULL;
bd74137e
IM
31
32static char default_sort_order[] = "comm,dso";
33static char *sort_order = default_sort_order;
34
8fa66bdc
ACM
35static int input;
36static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
37
97b07b69 38static int dump_trace = 0;
3502973d 39#define dprintf(x...) do { if (dump_trace) printf(x); } while (0)
3efa1cc9 40#define cdprintf(x...) do { if (dump_trace) color_fprintf(stdout, color, x); } while (0)
3502973d 41
16f762a2 42static int verbose;
7522060c
IM
43#define eprintf(x...) do { if (verbose) fprintf(stderr, x); } while (0)
44
b78c07d4 45static int full_paths;
97b07b69 46
8fa66bdc
ACM
47static unsigned long page_size;
48static unsigned long mmap_window = 32;
49
b8e6d829
IM
50static char default_parent_pattern[] = "^sys_|^do_page_fault";
51static char *parent_pattern = default_parent_pattern;
b25bcf2f 52static regex_t parent_regex;
6e7d6fdc 53
b8e6d829
IM
54static int exclude_other = 1;
55
e6e18ec7
PZ
56static u64 sample_type;
57
8fa66bdc
ACM
58struct ip_event {
59 struct perf_event_header header;
9cffa8d5
PM
60 u64 ip;
61 u32 pid, tid;
3efa1cc9 62 unsigned char __more_data[];
8fa66bdc 63};
75051724 64
2a0a50fe 65struct ip_callchain {
9cffa8d5
PM
66 u64 nr;
67 u64 ips[0];
2a0a50fe
PZ
68};
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{
243 return strcmp(filename, "//anon") == 0;
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 {
496 struct rb_node rb_node;
497
498 struct thread *thread;
499 struct map *map;
500 struct dso *dso;
501 struct symbol *sym;
b25bcf2f 502 struct symbol *parent;
9cffa8d5 503 u64 ip;
e7fb08b1
PZ
504 char level;
505
9cffa8d5 506 u64 count;
e7fb08b1
PZ
507};
508
1aa16738
PZ
509/*
510 * configurable sorting bits
511 */
512
513struct sort_entry {
514 struct list_head list;
515
ca8cdeef
PZ
516 char *header;
517
1aa16738 518 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
8229289b 519 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
1aa16738
PZ
520 size_t (*print)(FILE *fp, struct hist_entry *);
521};
522
6e7d6fdc
PZ
523static int64_t cmp_null(void *l, void *r)
524{
525 if (!l && !r)
526 return 0;
527 else if (!l)
528 return -1;
529 else
530 return 1;
531}
532
8229289b
PZ
533/* --sort pid */
534
e7fb08b1 535static int64_t
1aa16738 536sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
e7fb08b1 537{
1aa16738
PZ
538 return right->thread->pid - left->thread->pid;
539}
540
541static size_t
542sort__thread_print(FILE *fp, struct hist_entry *self)
543{
71dd8945 544 return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
1aa16738 545}
e7fb08b1 546
1aa16738 547static struct sort_entry sort_thread = {
71dd8945 548 .header = " Command: Pid",
1aa16738
PZ
549 .cmp = sort__thread_cmp,
550 .print = sort__thread_print,
551};
552
8229289b
PZ
553/* --sort comm */
554
992444b1
PZ
555static int64_t
556sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
8229289b
PZ
557{
558 return right->thread->pid - left->thread->pid;
559}
560
561static int64_t
562sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
992444b1
PZ
563{
564 char *comm_l = left->thread->comm;
565 char *comm_r = right->thread->comm;
566
6e7d6fdc
PZ
567 if (!comm_l || !comm_r)
568 return cmp_null(comm_l, comm_r);
992444b1
PZ
569
570 return strcmp(comm_l, comm_r);
571}
572
573static size_t
574sort__comm_print(FILE *fp, struct hist_entry *self)
575{
71dd8945 576 return fprintf(fp, "%16s", self->thread->comm);
992444b1
PZ
577}
578
579static struct sort_entry sort_comm = {
8edd4286 580 .header = " Command",
8229289b
PZ
581 .cmp = sort__comm_cmp,
582 .collapse = sort__comm_collapse,
583 .print = sort__comm_print,
992444b1
PZ
584};
585
8229289b
PZ
586/* --sort dso */
587
55e5ec41
PZ
588static int64_t
589sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
590{
591 struct dso *dso_l = left->dso;
592 struct dso *dso_r = right->dso;
593
6e7d6fdc
PZ
594 if (!dso_l || !dso_r)
595 return cmp_null(dso_l, dso_r);
55e5ec41
PZ
596
597 return strcmp(dso_l->name, dso_r->name);
598}
599
600static size_t
601sort__dso_print(FILE *fp, struct hist_entry *self)
602{
0a520c63 603 if (self->dso)
71dd8945 604 return fprintf(fp, "%-25s", self->dso->name);
0a520c63 605
9cffa8d5 606 return fprintf(fp, "%016llx ", (u64)self->ip);
55e5ec41
PZ
607}
608
609static struct sort_entry sort_dso = {
71dd8945 610 .header = "Shared Object ",
55e5ec41
PZ
611 .cmp = sort__dso_cmp,
612 .print = sort__dso_print,
613};
614
8229289b
PZ
615/* --sort symbol */
616
1aa16738
PZ
617static int64_t
618sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
619{
9cffa8d5 620 u64 ip_l, ip_r;
e7fb08b1
PZ
621
622 if (left->sym == right->sym)
623 return 0;
624
625 ip_l = left->sym ? left->sym->start : left->ip;
626 ip_r = right->sym ? right->sym->start : right->ip;
627
628 return (int64_t)(ip_r - ip_l);
629}
630
1aa16738
PZ
631static size_t
632sort__sym_print(FILE *fp, struct hist_entry *self)
633{
634 size_t ret = 0;
635
1aa16738 636 if (verbose)
9cffa8d5 637 ret += fprintf(fp, "%#018llx ", (u64)self->ip);
0a520c63 638
8edd4286
IM
639 if (self->sym) {
640 ret += fprintf(fp, "[%c] %s",
641 self->dso == kernel_dso ? 'k' : '.', self->sym->name);
642 } else {
9cffa8d5 643 ret += fprintf(fp, "%#016llx", (u64)self->ip);
8edd4286 644 }
1aa16738
PZ
645
646 return ret;
647}
648
649static struct sort_entry sort_sym = {
71dd8945 650 .header = "Symbol",
ca8cdeef
PZ
651 .cmp = sort__sym_cmp,
652 .print = sort__sym_print,
1aa16738
PZ
653};
654
b25bcf2f 655/* --sort parent */
6e7d6fdc
PZ
656
657static int64_t
b25bcf2f 658sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
6e7d6fdc 659{
b25bcf2f
IM
660 struct symbol *sym_l = left->parent;
661 struct symbol *sym_r = right->parent;
6e7d6fdc
PZ
662
663 if (!sym_l || !sym_r)
664 return cmp_null(sym_l, sym_r);
665
666 return strcmp(sym_l->name, sym_r->name);
667}
668
669static size_t
b25bcf2f 670sort__parent_print(FILE *fp, struct hist_entry *self)
6e7d6fdc
PZ
671{
672 size_t ret = 0;
673
b25bcf2f 674 ret += fprintf(fp, "%-20s", self->parent ? self->parent->name : "[other]");
6e7d6fdc
PZ
675
676 return ret;
677}
678
b25bcf2f
IM
679static struct sort_entry sort_parent = {
680 .header = "Parent symbol ",
681 .cmp = sort__parent_cmp,
682 .print = sort__parent_print,
6e7d6fdc
PZ
683};
684
8229289b 685static int sort__need_collapse = 0;
b25bcf2f 686static int sort__has_parent = 0;
8229289b 687
37f440cb 688struct sort_dimension {
8edd4286
IM
689 char *name;
690 struct sort_entry *entry;
691 int taken;
37f440cb
PZ
692};
693
694static struct sort_dimension sort_dimensions[] = {
695 { .name = "pid", .entry = &sort_thread, },
992444b1 696 { .name = "comm", .entry = &sort_comm, },
55e5ec41 697 { .name = "dso", .entry = &sort_dso, },
37f440cb 698 { .name = "symbol", .entry = &sort_sym, },
b25bcf2f 699 { .name = "parent", .entry = &sort_parent, },
37f440cb
PZ
700};
701
1aa16738
PZ
702static LIST_HEAD(hist_entry__sort_list);
703
37f440cb
PZ
704static int sort_dimension__add(char *tok)
705{
706 int i;
707
708 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
709 struct sort_dimension *sd = &sort_dimensions[i];
710
711 if (sd->taken)
712 continue;
713
5352f35d 714 if (strncasecmp(tok, sd->name, strlen(tok)))
37f440cb
PZ
715 continue;
716
8229289b
PZ
717 if (sd->entry->collapse)
718 sort__need_collapse = 1;
719
b25bcf2f
IM
720 if (sd->entry == &sort_parent) {
721 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
6e7d6fdc
PZ
722 if (ret) {
723 char err[BUFSIZ];
724
b25bcf2f
IM
725 regerror(ret, &parent_regex, err, sizeof(err));
726 fprintf(stderr, "Invalid regex: %s\n%s",
727 parent_pattern, err);
6e7d6fdc
PZ
728 exit(-1);
729 }
b25bcf2f 730 sort__has_parent = 1;
6e7d6fdc
PZ
731 }
732
37f440cb
PZ
733 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
734 sd->taken = 1;
5352f35d 735
37f440cb
PZ
736 return 0;
737 }
738
739 return -ESRCH;
740}
741
1aa16738
PZ
742static int64_t
743hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
744{
745 struct sort_entry *se;
746 int64_t cmp = 0;
747
748 list_for_each_entry(se, &hist_entry__sort_list, list) {
749 cmp = se->cmp(left, right);
750 if (cmp)
751 break;
752 }
753
754 return cmp;
755}
756
8229289b
PZ
757static int64_t
758hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
759{
760 struct sort_entry *se;
761 int64_t cmp = 0;
762
763 list_for_each_entry(se, &hist_entry__sort_list, list) {
764 int64_t (*f)(struct hist_entry *, struct hist_entry *);
765
766 f = se->collapse ?: se->cmp;
767
768 cmp = f(left, right);
769 if (cmp)
770 break;
771 }
772
773 return cmp;
774}
775
1aa16738 776static size_t
9cffa8d5 777hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
1aa16738
PZ
778{
779 struct sort_entry *se;
780 size_t ret;
781
b8e6d829
IM
782 if (exclude_other && !self->parent)
783 return 0;
784
1aa16738 785 if (total_samples) {
8fc0321f
IM
786 double percent = self->count * 100.0 / total_samples;
787 char *color = PERF_COLOR_NORMAL;
788
789 /*
aefcf37b
IM
790 * We color high-overhead entries in red, mid-overhead
791 * entries in green - and keep the low overhead places
792 * normal:
8fc0321f 793 */
aefcf37b 794 if (percent >= 5.0) {
8fc0321f 795 color = PERF_COLOR_RED;
aefcf37b
IM
796 } else {
797 if (percent >= 0.5)
798 color = PERF_COLOR_GREEN;
799 }
8fc0321f
IM
800
801 ret = color_fprintf(fp, color, " %6.2f%%",
1aa16738
PZ
802 (self->count * 100.0) / total_samples);
803 } else
729ff5e2 804 ret = fprintf(fp, "%12Ld ", self->count);
1aa16738 805
71dd8945 806 list_for_each_entry(se, &hist_entry__sort_list, list) {
b8e6d829
IM
807 if (exclude_other && (se == &sort_parent))
808 continue;
809
71dd8945 810 fprintf(fp, " ");
1aa16738 811 ret += se->print(fp, self);
71dd8945 812 }
1aa16738
PZ
813
814 ret += fprintf(fp, "\n");
815
816 return ret;
817}
818
6e7d6fdc
PZ
819/*
820 *
821 */
822
823static struct symbol *
824resolve_symbol(struct thread *thread, struct map **mapp,
9cffa8d5 825 struct dso **dsop, u64 *ipp)
6e7d6fdc
PZ
826{
827 struct dso *dso = dsop ? *dsop : NULL;
828 struct map *map = mapp ? *mapp : NULL;
520f2c34 829 u64 ip = *ipp;
6e7d6fdc
PZ
830
831 if (!thread)
832 return NULL;
833
834 if (dso)
835 goto got_dso;
836
837 if (map)
838 goto got_map;
839
840 map = thread__find_map(thread, ip);
841 if (map != NULL) {
842 if (mapp)
843 *mapp = map;
844got_map:
845 ip = map->map_ip(map, ip);
6e7d6fdc
PZ
846
847 dso = map->dso;
848 } else {
849 /*
850 * If this is outside of all known maps,
851 * and is a negative address, try to look it
852 * up in the kernel dso, as it might be a
853 * vsyscall (which executes in user-mode):
854 */
855 if ((long long)ip < 0)
856 dso = kernel_dso;
857 }
858 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
520f2c34
PZ
859 dprintf(" ...... map: %Lx -> %Lx\n", *ipp, ip);
860 *ipp = ip;
6e7d6fdc
PZ
861
862 if (dsop)
863 *dsop = dso;
864
865 if (!dso)
866 return NULL;
867got_dso:
868 return dso->find_symbol(dso, ip);
869}
870
2a0a50fe 871static int call__match(struct symbol *sym)
6e7d6fdc 872{
b25bcf2f 873 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
2a0a50fe 874 return 1;
6e7d6fdc 875
2a0a50fe 876 return 0;
6e7d6fdc
PZ
877}
878
1aa16738
PZ
879/*
880 * collect histogram counts
881 */
882
e7fb08b1
PZ
883static int
884hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
9cffa8d5
PM
885 struct symbol *sym, u64 ip, struct ip_callchain *chain,
886 char level, u64 count)
8fa66bdc 887{
e7fb08b1
PZ
888 struct rb_node **p = &hist.rb_node;
889 struct rb_node *parent = NULL;
890 struct hist_entry *he;
891 struct hist_entry entry = {
892 .thread = thread,
893 .map = map,
894 .dso = dso,
895 .sym = sym,
896 .ip = ip,
897 .level = level,
ea1900e5 898 .count = count,
b8e6d829 899 .parent = NULL,
e7fb08b1
PZ
900 };
901 int cmp;
902
b25bcf2f 903 if (sort__has_parent && chain) {
9cffa8d5 904 u64 context = PERF_CONTEXT_MAX;
2a0a50fe
PZ
905 int i;
906
907 for (i = 0; i < chain->nr; i++) {
9cffa8d5 908 u64 ip = chain->ips[i];
2a0a50fe
PZ
909 struct dso *dso = NULL;
910 struct symbol *sym;
911
912 if (ip >= PERF_CONTEXT_MAX) {
913 context = ip;
914 continue;
915 }
916
917 switch (context) {
918 case PERF_CONTEXT_KERNEL:
919 dso = kernel_dso;
920 break;
921 default:
922 break;
923 }
924
6e7d6fdc 925 sym = resolve_symbol(thread, NULL, &dso, &ip);
2a0a50fe
PZ
926
927 if (sym && call__match(sym)) {
928 entry.parent = sym;
929 break;
930 }
6e7d6fdc 931 }
6e7d6fdc 932 }
6e7d6fdc 933
e7fb08b1
PZ
934 while (*p != NULL) {
935 parent = *p;
936 he = rb_entry(parent, struct hist_entry, rb_node);
937
938 cmp = hist_entry__cmp(&entry, he);
939
940 if (!cmp) {
ea1900e5 941 he->count += count;
e7fb08b1
PZ
942 return 0;
943 }
944
945 if (cmp < 0)
946 p = &(*p)->rb_left;
947 else
948 p = &(*p)->rb_right;
ce7e4365 949 }
e7fb08b1
PZ
950
951 he = malloc(sizeof(*he));
952 if (!he)
953 return -ENOMEM;
954 *he = entry;
955 rb_link_node(&he->rb_node, parent, p);
956 rb_insert_color(&he->rb_node, &hist);
957
958 return 0;
8fa66bdc
ACM
959}
960
8229289b
PZ
961static void hist_entry__free(struct hist_entry *he)
962{
963 free(he);
964}
965
966/*
967 * collapse the histogram
968 */
969
970static struct rb_root collapse_hists;
971
972static void collapse__insert_entry(struct hist_entry *he)
973{
974 struct rb_node **p = &collapse_hists.rb_node;
975 struct rb_node *parent = NULL;
976 struct hist_entry *iter;
977 int64_t cmp;
978
979 while (*p != NULL) {
980 parent = *p;
981 iter = rb_entry(parent, struct hist_entry, rb_node);
982
983 cmp = hist_entry__collapse(iter, he);
984
985 if (!cmp) {
986 iter->count += he->count;
987 hist_entry__free(he);
988 return;
989 }
990
991 if (cmp < 0)
992 p = &(*p)->rb_left;
993 else
994 p = &(*p)->rb_right;
995 }
996
997 rb_link_node(&he->rb_node, parent, p);
998 rb_insert_color(&he->rb_node, &collapse_hists);
999}
1000
1001static void collapse__resort(void)
1002{
1003 struct rb_node *next;
1004 struct hist_entry *n;
1005
1006 if (!sort__need_collapse)
1007 return;
1008
1009 next = rb_first(&hist);
1010 while (next) {
1011 n = rb_entry(next, struct hist_entry, rb_node);
1012 next = rb_next(&n->rb_node);
1013
1014 rb_erase(&n->rb_node, &hist);
1015 collapse__insert_entry(n);
1016 }
1017}
1018
e7fb08b1
PZ
1019/*
1020 * reverse the map, sort on count.
1021 */
1022
1023static struct rb_root output_hists;
1024
1025static void output__insert_entry(struct hist_entry *he)
3a4b8cc7 1026{
e7fb08b1 1027 struct rb_node **p = &output_hists.rb_node;
3a4b8cc7 1028 struct rb_node *parent = NULL;
e7fb08b1 1029 struct hist_entry *iter;
3a4b8cc7
ACM
1030
1031 while (*p != NULL) {
1032 parent = *p;
e7fb08b1 1033 iter = rb_entry(parent, struct hist_entry, rb_node);
3a4b8cc7 1034
e7fb08b1 1035 if (he->count > iter->count)
3a4b8cc7
ACM
1036 p = &(*p)->rb_left;
1037 else
1038 p = &(*p)->rb_right;
1039 }
1040
e7fb08b1
PZ
1041 rb_link_node(&he->rb_node, parent, p);
1042 rb_insert_color(&he->rb_node, &output_hists);
3a4b8cc7
ACM
1043}
1044
e7fb08b1 1045static void output__resort(void)
3a4b8cc7 1046{
8229289b 1047 struct rb_node *next;
e7fb08b1 1048 struct hist_entry *n;
a4c43bea 1049 struct rb_root *tree = &hist;
3a4b8cc7 1050
8229289b 1051 if (sort__need_collapse)
a4c43bea
ACM
1052 tree = &collapse_hists;
1053
1054 next = rb_first(tree);
8229289b 1055
e7fb08b1
PZ
1056 while (next) {
1057 n = rb_entry(next, struct hist_entry, rb_node);
1058 next = rb_next(&n->rb_node);
3a4b8cc7 1059
a4c43bea 1060 rb_erase(&n->rb_node, tree);
e7fb08b1 1061 output__insert_entry(n);
3a4b8cc7
ACM
1062 }
1063}
1064
9cffa8d5 1065static size_t output__fprintf(FILE *fp, u64 total_samples)
3a4b8cc7 1066{
e7fb08b1 1067 struct hist_entry *pos;
2d65537e 1068 struct sort_entry *se;
3a4b8cc7
ACM
1069 struct rb_node *nd;
1070 size_t ret = 0;
1071
71dd8945 1072 fprintf(fp, "\n");
05ca061e 1073 fprintf(fp, "#\n");
9cffa8d5 1074 fprintf(fp, "# (%Ld samples)\n", (u64)total_samples);
ca8cdeef
PZ
1075 fprintf(fp, "#\n");
1076
1077 fprintf(fp, "# Overhead");
b8e6d829
IM
1078 list_for_each_entry(se, &hist_entry__sort_list, list) {
1079 if (exclude_other && (se == &sort_parent))
1080 continue;
71dd8945 1081 fprintf(fp, " %s", se->header);
b8e6d829 1082 }
ca8cdeef
PZ
1083 fprintf(fp, "\n");
1084
1085 fprintf(fp, "# ........");
2d65537e 1086 list_for_each_entry(se, &hist_entry__sort_list, list) {
ca8cdeef
PZ
1087 int i;
1088
b8e6d829
IM
1089 if (exclude_other && (se == &sort_parent))
1090 continue;
1091
4593bba8 1092 fprintf(fp, " ");
71dd8945 1093 for (i = 0; i < strlen(se->header); i++)
ca8cdeef 1094 fprintf(fp, ".");
2d65537e 1095 }
ca8cdeef
PZ
1096 fprintf(fp, "\n");
1097
1098 fprintf(fp, "#\n");
2d65537e 1099
e7fb08b1
PZ
1100 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
1101 pos = rb_entry(nd, struct hist_entry, rb_node);
1102 ret += hist_entry__fprintf(fp, pos, total_samples);
3a4b8cc7
ACM
1103 }
1104
b8e6d829
IM
1105 if (sort_order == default_sort_order &&
1106 parent_pattern == default_parent_pattern) {
bd74137e 1107 fprintf(fp, "#\n");
71dd8945 1108 fprintf(fp, "# (For more details, try: perf report --sort comm,dso,symbol)\n");
bd74137e
IM
1109 fprintf(fp, "#\n");
1110 }
71dd8945 1111 fprintf(fp, "\n");
bd74137e 1112
3a4b8cc7
ACM
1113 return ret;
1114}
1115
436224a6
PZ
1116static void register_idle_thread(void)
1117{
1118 struct thread *thread = threads__findnew(0);
1119
1120 if (thread == NULL ||
1121 thread__set_comm(thread, "[idle]")) {
1122 fprintf(stderr, "problem inserting idle task.\n");
1123 exit(-1);
1124 }
1125}
1126
62fc4453
PZ
1127static unsigned long total = 0,
1128 total_mmap = 0,
1129 total_comm = 0,
1130 total_fork = 0,
9d91a6f7
PZ
1131 total_unknown = 0,
1132 total_lost = 0;
e7fb08b1 1133
2a0a50fe 1134static int validate_chain(struct ip_callchain *chain, event_t *event)
7522060c
IM
1135{
1136 unsigned int chain_size;
1137
7522060c
IM
1138 chain_size = event->header.size;
1139 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
1140
9cffa8d5 1141 if (chain->nr*sizeof(u64) > chain_size)
7522060c
IM
1142 return -1;
1143
1144 return 0;
1145}
1146
d80d338d 1147static int
e6e18ec7 1148process_sample_event(event_t *event, unsigned long offset, unsigned long head)
75051724
IM
1149{
1150 char level;
1151 int show = 0;
1152 struct dso *dso = NULL;
1153 struct thread *thread = threads__findnew(event->ip.pid);
9cffa8d5
PM
1154 u64 ip = event->ip.ip;
1155 u64 period = 1;
75051724 1156 struct map *map = NULL;
3efa1cc9 1157 void *more_data = event->ip.__more_data;
2a0a50fe 1158 struct ip_callchain *chain = NULL;
75051724 1159
e6e18ec7 1160 if (sample_type & PERF_SAMPLE_PERIOD) {
9cffa8d5
PM
1161 period = *(u64 *)more_data;
1162 more_data += sizeof(u64);
3efa1cc9 1163 }
ea1900e5 1164
e6e18ec7 1165 dprintf("%p [%p]: PERF_EVENT_SAMPLE (IP, %d): %d: %p period: %Ld\n",
75051724
IM
1166 (void *)(offset + head),
1167 (void *)(long)(event->header.size),
1168 event->header.misc,
1169 event->ip.pid,
4502d77c 1170 (void *)(long)ip,
ea1900e5 1171 (long long)period);
75051724 1172
e6e18ec7 1173 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3efa1cc9
IM
1174 int i;
1175
1176 chain = (void *)more_data;
1177
2a0a50fe 1178 dprintf("... chain: nr:%Lu\n", chain->nr);
3efa1cc9 1179
7522060c
IM
1180 if (validate_chain(chain, event) < 0) {
1181 eprintf("call-chain problem with event, skipping it.\n");
1182 return 0;
1183 }
1184
1185 if (dump_trace) {
3efa1cc9 1186 for (i = 0; i < chain->nr; i++)
2a0a50fe 1187 dprintf("..... %2d: %016Lx\n", i, chain->ips[i]);
3efa1cc9
IM
1188 }
1189 }
1190
75051724
IM
1191 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1192
1193 if (thread == NULL) {
7522060c 1194 eprintf("problem processing %d event, skipping it.\n",
75051724
IM
1195 event->header.type);
1196 return -1;
1197 }
e7fb08b1 1198
75051724
IM
1199 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
1200 show = SHOW_KERNEL;
1201 level = 'k';
e7fb08b1 1202
75051724 1203 dso = kernel_dso;
ed966aac 1204
75051724 1205 dprintf(" ...... dso: %s\n", dso->name);
16f762a2 1206
75051724 1207 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
16f762a2 1208
75051724
IM
1209 show = SHOW_USER;
1210 level = '.';
e7fb08b1 1211
75051724
IM
1212 } else {
1213 show = SHOW_HV;
1214 level = 'H';
1215 dprintf(" ...... dso: [hypervisor]\n");
1216 }
8fa66bdc 1217
75051724 1218 if (show & show_mask) {
6e7d6fdc 1219 struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
8fa66bdc 1220
6e7d6fdc 1221 if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
7522060c 1222 eprintf("problem incrementing symbol count, skipping event\n");
d80d338d 1223 return -1;
ce7e4365 1224 }
8fa66bdc 1225 }
ea1900e5 1226 total += period;
8fa66bdc 1227
75051724
IM
1228 return 0;
1229}
3502973d 1230
75051724
IM
1231static int
1232process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
1233{
1234 struct thread *thread = threads__findnew(event->mmap.pid);
1235 struct map *map = map__new(&event->mmap);
1236
62fc4453 1237 dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
75051724
IM
1238 (void *)(offset + head),
1239 (void *)(long)(event->header.size),
62fc4453 1240 event->mmap.pid,
75051724
IM
1241 (void *)(long)event->mmap.start,
1242 (void *)(long)event->mmap.len,
1243 (void *)(long)event->mmap.pgoff,
1244 event->mmap.filename);
1245
1246 if (thread == NULL || map == NULL) {
1247 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
df97992c 1248 return 0;
75051724
IM
1249 }
1250
1251 thread__insert_map(thread, map);
1252 total_mmap++;
1253
1254 return 0;
1255}
1256
1257static int
1258process_comm_event(event_t *event, unsigned long offset, unsigned long head)
1259{
1260 struct thread *thread = threads__findnew(event->comm.pid);
1261
1262 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
1263 (void *)(offset + head),
1264 (void *)(long)(event->header.size),
1265 event->comm.comm, event->comm.pid);
1266
1267 if (thread == NULL ||
1268 thread__set_comm(thread, event->comm.comm)) {
1269 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
1270 return -1;
8fa66bdc 1271 }
75051724
IM
1272 total_comm++;
1273
1274 return 0;
1275}
1276
62fc4453
PZ
1277static int
1278process_fork_event(event_t *event, unsigned long offset, unsigned long head)
1279{
1280 struct thread *thread = threads__findnew(event->fork.pid);
1281 struct thread *parent = threads__findnew(event->fork.ppid);
1282
1283 dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
1284 (void *)(offset + head),
1285 (void *)(long)(event->header.size),
1286 event->fork.pid, event->fork.ppid);
1287
1288 if (!thread || !parent || thread__fork(thread, parent)) {
1289 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
1290 return -1;
1291 }
1292 total_fork++;
1293
1294 return 0;
1295}
1296
b2fef076
IM
1297static int
1298process_period_event(event_t *event, unsigned long offset, unsigned long head)
1299{
1300 dprintf("%p [%p]: PERF_EVENT_PERIOD: time:%Ld, id:%Ld: period:%Ld\n",
1301 (void *)(offset + head),
1302 (void *)(long)(event->header.size),
1303 event->period.time,
1304 event->period.id,
1305 event->period.sample_period);
1306
1307 return 0;
1308}
1309
9d91a6f7
PZ
1310static int
1311process_lost_event(event_t *event, unsigned long offset, unsigned long head)
1312{
1313 dprintf("%p [%p]: PERF_EVENT_LOST: id:%Ld: lost:%Ld\n",
1314 (void *)(offset + head),
1315 (void *)(long)(event->header.size),
1316 event->lost.id,
1317 event->lost.lost);
1318
1319 total_lost += event->lost.lost;
1320
1321 return 0;
1322}
1323
8465b050
IM
1324static void trace_event(event_t *event)
1325{
1326 unsigned char *raw_event = (void *)event;
3efa1cc9 1327 char *color = PERF_COLOR_BLUE;
8465b050
IM
1328 int i, j;
1329
1330 if (!dump_trace)
1331 return;
1332
3efa1cc9
IM
1333 dprintf(".");
1334 cdprintf("\n. ... raw event: size %d bytes\n", event->header.size);
8465b050
IM
1335
1336 for (i = 0; i < event->header.size; i++) {
3efa1cc9
IM
1337 if ((i & 15) == 0) {
1338 dprintf(".");
1339 cdprintf(" %04x: ", i);
1340 }
8465b050 1341
3efa1cc9 1342 cdprintf(" %02x", raw_event[i]);
8465b050
IM
1343
1344 if (((i & 15) == 15) || i == event->header.size-1) {
3efa1cc9 1345 cdprintf(" ");
8465b050 1346 for (j = 0; j < 15-(i & 15); j++)
3efa1cc9 1347 cdprintf(" ");
8465b050 1348 for (j = 0; j < (i & 15); j++) {
a73c7d84 1349 if (isprint(raw_event[i-15+j]))
3efa1cc9 1350 cdprintf("%c", raw_event[i-15+j]);
8465b050 1351 else
3efa1cc9 1352 cdprintf(".");
8465b050 1353 }
3efa1cc9 1354 cdprintf("\n");
8465b050
IM
1355 }
1356 }
1357 dprintf(".\n");
1358}
1359
e9ea2fde
PZ
1360static int
1361process_read_event(event_t *event, unsigned long offset, unsigned long head)
1362{
1363 dprintf("%p [%p]: PERF_EVENT_READ: %d %d %Lu\n",
1364 (void *)(offset + head),
1365 (void *)(long)(event->header.size),
1366 event->read.pid,
1367 event->read.tid,
1368 event->read.value);
1369
1370 return 0;
1371}
1372
75051724
IM
1373static int
1374process_event(event_t *event, unsigned long offset, unsigned long head)
1375{
8465b050
IM
1376 trace_event(event);
1377
75051724 1378 switch (event->header.type) {
e6e18ec7
PZ
1379 case PERF_EVENT_SAMPLE:
1380 return process_sample_event(event, offset, head);
1381
75051724
IM
1382 case PERF_EVENT_MMAP:
1383 return process_mmap_event(event, offset, head);
1384
1385 case PERF_EVENT_COMM:
1386 return process_comm_event(event, offset, head);
1387
62fc4453
PZ
1388 case PERF_EVENT_FORK:
1389 return process_fork_event(event, offset, head);
1390
b2fef076
IM
1391 case PERF_EVENT_PERIOD:
1392 return process_period_event(event, offset, head);
9d91a6f7
PZ
1393
1394 case PERF_EVENT_LOST:
1395 return process_lost_event(event, offset, head);
1396
e9ea2fde
PZ
1397 case PERF_EVENT_READ:
1398 return process_read_event(event, offset, head);
1399
d11444df
IM
1400 /*
1401 * We dont process them right now but they are fine:
1402 */
62fc4453 1403
d11444df
IM
1404 case PERF_EVENT_THROTTLE:
1405 case PERF_EVENT_UNTHROTTLE:
1406 return 0;
1407
d80d338d
IM
1408 default:
1409 return -1;
1410 }
1411
1412 return 0;
1413}
1414
7c6a1c65
PZ
1415static struct perf_header *header;
1416
e6e18ec7 1417static u64 perf_header__sample_type(void)
7c6a1c65 1418{
e6e18ec7 1419 u64 sample_type = 0;
7c6a1c65
PZ
1420 int i;
1421
1422 for (i = 0; i < header->attrs; i++) {
1423 struct perf_header_attr *attr = header->attr[i];
1424
e6e18ec7
PZ
1425 if (!sample_type)
1426 sample_type = attr->attr.sample_type;
1427 else if (sample_type != attr->attr.sample_type)
1428 die("non matching sample_type");
7c6a1c65
PZ
1429 }
1430
e6e18ec7 1431 return sample_type;
7c6a1c65 1432}
f5970550 1433
d80d338d
IM
1434static int __cmd_report(void)
1435{
75051724 1436 int ret, rc = EXIT_FAILURE;
d80d338d 1437 unsigned long offset = 0;
7c6a1c65 1438 unsigned long head, shift;
d80d338d 1439 struct stat stat;
d80d338d 1440 event_t *event;
d80d338d 1441 uint32_t size;
75051724 1442 char *buf;
d80d338d
IM
1443
1444 register_idle_thread();
1445
1446 input = open(input_name, O_RDONLY);
1447 if (input < 0) {
a14832ff
IM
1448 fprintf(stderr, " failed to open file: %s", input_name);
1449 if (!strcmp(input_name, "perf.data"))
1450 fprintf(stderr, " (try 'perf record' first)");
1451 fprintf(stderr, "\n");
d80d338d
IM
1452 exit(-1);
1453 }
1454
1455 ret = fstat(input, &stat);
1456 if (ret < 0) {
1457 perror("failed to stat file");
1458 exit(-1);
1459 }
1460
1461 if (!stat.st_size) {
1462 fprintf(stderr, "zero-sized file, nothing to do!\n");
1463 exit(0);
1464 }
1465
7c6a1c65
PZ
1466 header = perf_header__read(input);
1467 head = header->data_offset;
f5970550 1468
e6e18ec7
PZ
1469 sample_type = perf_header__sample_type();
1470
1471 if (sort__has_parent && !(sample_type & PERF_SAMPLE_CALLCHAIN)) {
f5970550
PZ
1472 fprintf(stderr, "selected --sort parent, but no callchain data\n");
1473 exit(-1);
1474 }
1475
d80d338d
IM
1476 if (load_kernel() < 0) {
1477 perror("failed to load kernel symbols");
1478 return EXIT_FAILURE;
1479 }
1480
1481 if (!full_paths) {
1482 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
1483 perror("failed to get the current directory");
1484 return EXIT_FAILURE;
1485 }
1486 cwdlen = strlen(cwd);
1487 } else {
1488 cwd = NULL;
1489 cwdlen = 0;
1490 }
7c6a1c65
PZ
1491
1492 shift = page_size * (head / page_size);
1493 offset += shift;
1494 head -= shift;
1495
d80d338d
IM
1496remap:
1497 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1498 MAP_SHARED, input, offset);
1499 if (buf == MAP_FAILED) {
1500 perror("failed to mmap file");
1501 exit(-1);
1502 }
1503
1504more:
1505 event = (event_t *)(buf + head);
1506
1507 size = event->header.size;
1508 if (!size)
1509 size = 8;
1510
1511 if (head + event->header.size >= page_size * mmap_window) {
d80d338d
IM
1512 int ret;
1513
7c6a1c65
PZ
1514 shift = page_size * (head / page_size);
1515
d80d338d
IM
1516 ret = munmap(buf, page_size * mmap_window);
1517 assert(ret == 0);
1518
1519 offset += shift;
1520 head -= shift;
1521 goto remap;
1522 }
1523
1524 size = event->header.size;
1525
8465b050 1526 dprintf("\n%p [%p]: event: %d\n",
b2fef076
IM
1527 (void *)(offset + head),
1528 (void *)(long)event->header.size,
1529 event->header.type);
1530
d80d338d
IM
1531 if (!size || process_event(event, offset, head) < 0) {
1532
3502973d
IM
1533 dprintf("%p [%p]: skipping unknown header type: %d\n",
1534 (void *)(offset + head),
1535 (void *)(long)(event->header.size),
1536 event->header.type);
b7a16eac 1537
3e706114 1538 total_unknown++;
6142f9ec
PZ
1539
1540 /*
1541 * assume we lost track of the stream, check alignment, and
1542 * increment a single u64 in the hope to catch on again 'soon'.
1543 */
1544
1545 if (unlikely(head & 7))
1546 head &= ~7ULL;
1547
1548 size = 8;
97b07b69 1549 }
8fa66bdc 1550
6142f9ec 1551 head += size;
f49515b1 1552
7c6a1c65 1553 if (offset + head >= header->data_offset + header->data_size)
f5970550
PZ
1554 goto done;
1555
8fa66bdc
ACM
1556 if (offset + head < stat.st_size)
1557 goto more;
1558
f5970550 1559done:
8fa66bdc 1560 rc = EXIT_SUCCESS;
8fa66bdc 1561 close(input);
97b07b69 1562
3502973d
IM
1563 dprintf(" IP events: %10ld\n", total);
1564 dprintf(" mmap events: %10ld\n", total_mmap);
1565 dprintf(" comm events: %10ld\n", total_comm);
62fc4453 1566 dprintf(" fork events: %10ld\n", total_fork);
9d91a6f7 1567 dprintf(" lost events: %10ld\n", total_lost);
3502973d 1568 dprintf(" unknown events: %10ld\n", total_unknown);
97b07b69 1569
3502973d 1570 if (dump_trace)
97b07b69 1571 return 0;
97b07b69 1572
9ac99545
ACM
1573 if (verbose >= 3)
1574 threads__fprintf(stdout);
1575
e7fb08b1 1576 if (verbose >= 2)
16f762a2 1577 dsos__fprintf(stdout);
16f762a2 1578
8229289b 1579 collapse__resort();
e7fb08b1
PZ
1580 output__resort();
1581 output__fprintf(stdout, total);
8fa66bdc 1582
8fa66bdc
ACM
1583 return rc;
1584}
1585
53cb8bc2
IM
1586static const char * const report_usage[] = {
1587 "perf report [<options>] <command>",
1588 NULL
1589};
1590
1591static const struct option options[] = {
1592 OPT_STRING('i', "input", &input_name, "file",
1593 "input file name"),
815e777f
ACM
1594 OPT_BOOLEAN('v', "verbose", &verbose,
1595 "be more verbose (show symbol address, etc)"),
97b07b69
IM
1596 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1597 "dump raw trace in ASCII"),
450aaa2b 1598 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
63299f05 1599 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
b25bcf2f 1600 "sort by key(s): pid, comm, dso, symbol, parent"),
b78c07d4
ACM
1601 OPT_BOOLEAN('P', "full-paths", &full_paths,
1602 "Don't shorten the pathnames taking into account the cwd"),
b25bcf2f
IM
1603 OPT_STRING('p', "parent", &parent_pattern, "regex",
1604 "regex filter to identify parent, see: '--sort parent'"),
b8e6d829
IM
1605 OPT_BOOLEAN('x', "exclude-other", &exclude_other,
1606 "Only display entries with parent-match"),
53cb8bc2
IM
1607 OPT_END()
1608};
1609
5352f35d
IM
1610static void setup_sorting(void)
1611{
1612 char *tmp, *tok, *str = strdup(sort_order);
1613
1614 for (tok = strtok_r(str, ", ", &tmp);
1615 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1616 if (sort_dimension__add(tok) < 0) {
1617 error("Unknown --sort key: `%s'", tok);
1618 usage_with_options(report_usage, options);
1619 }
1620 }
1621
1622 free(str);
1623}
1624
53cb8bc2
IM
1625int cmd_report(int argc, const char **argv, const char *prefix)
1626{
a2928c42 1627 symbol__init();
53cb8bc2
IM
1628
1629 page_size = getpagesize();
1630
edc52dea 1631 argc = parse_options(argc, argv, options, report_usage, 0);
53cb8bc2 1632
1aa16738
PZ
1633 setup_sorting();
1634
b8e6d829
IM
1635 if (parent_pattern != default_parent_pattern)
1636 sort_dimension__add("parent");
1637 else
1638 exclude_other = 0;
1639
edc52dea
IM
1640 /*
1641 * Any (unrecognized) arguments left?
1642 */
1643 if (argc)
1644 usage_with_options(report_usage, options);
1645
a930d2c0
IM
1646 setup_pager();
1647
53cb8bc2
IM
1648 return __cmd_report();
1649}
This page took 0.153048 seconds and 5 git commands to generate.