perf_counter: Fix throttling lock-up
[deliverable/linux.git] / Documentation / perf_counter / 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
35a50c8a 12#include "util/list.h"
a930d2c0 13#include "util/cache.h"
35a50c8a 14#include "util/rbtree.h"
a2928c42 15#include "util/symbol.h"
a0055ae2 16#include "util/string.h"
8fa66bdc 17
53cb8bc2
IM
18#include "perf.h"
19
20#include "util/parse-options.h"
21#include "util/parse-events.h"
22
8fa66bdc
ACM
23#define SHOW_KERNEL 1
24#define SHOW_USER 2
25#define SHOW_HV 4
26
23ac9cbe 27static char const *input_name = "perf.data";
450aaa2b 28static char *vmlinux = NULL;
f70e87d7 29static char *sort_order = "comm,dso";
8fa66bdc
ACM
30static int input;
31static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
32
97b07b69 33static int dump_trace = 0;
3502973d
IM
34#define dprintf(x...) do { if (dump_trace) printf(x); } while (0)
35
16f762a2 36static int verbose;
b78c07d4 37static int full_paths;
97b07b69 38
8fa66bdc
ACM
39static unsigned long page_size;
40static unsigned long mmap_window = 32;
41
53cb8bc2 42const char *perf_event_names[] = {
8fa66bdc
ACM
43 [PERF_EVENT_MMAP] = " PERF_EVENT_MMAP",
44 [PERF_EVENT_MUNMAP] = " PERF_EVENT_MUNMAP",
45 [PERF_EVENT_COMM] = " PERF_EVENT_COMM",
46};
47
48struct ip_event {
49 struct perf_event_header header;
50 __u64 ip;
51 __u32 pid, tid;
52};
53struct mmap_event {
54 struct perf_event_header header;
55 __u32 pid, tid;
56 __u64 start;
57 __u64 len;
58 __u64 pgoff;
59 char filename[PATH_MAX];
60};
61struct comm_event {
62 struct perf_event_header header;
63 __u32 pid,tid;
64 char comm[16];
65};
66
67typedef union event_union {
68 struct perf_event_header header;
69 struct ip_event ip;
70 struct mmap_event mmap;
71 struct comm_event comm;
72} event_t;
73
8fa66bdc
ACM
74static LIST_HEAD(dsos);
75static struct dso *kernel_dso;
76
77static void dsos__add(struct dso *dso)
78{
79 list_add_tail(&dso->node, &dsos);
80}
81
82static struct dso *dsos__find(const char *name)
83{
84 struct dso *pos;
85
86 list_for_each_entry(pos, &dsos, node)
87 if (strcmp(pos->name, name) == 0)
88 return pos;
89 return NULL;
90}
91
92static struct dso *dsos__findnew(const char *name)
93{
94 struct dso *dso = dsos__find(name);
b7a16eac 95 int nr;
8fa66bdc 96
4593bba8
IM
97 if (dso)
98 return dso;
99
100 dso = dso__new(name, 0);
101 if (!dso)
102 goto out_delete_dso;
8fa66bdc 103
4593bba8
IM
104 nr = dso__load(dso, NULL);
105 if (nr < 0) {
106 fprintf(stderr, "Failed to open: %s\n", name);
107 goto out_delete_dso;
8fa66bdc 108 }
4593bba8
IM
109 if (!nr && verbose) {
110 fprintf(stderr,
111 "No symbols found in: %s, maybe install a debug package?\n",
112 name);
113 }
114
115 dsos__add(dso);
8fa66bdc
ACM
116
117 return dso;
118
119out_delete_dso:
120 dso__delete(dso);
121 return NULL;
122}
123
16f762a2 124static void dsos__fprintf(FILE *fp)
8fa66bdc
ACM
125{
126 struct dso *pos;
127
128 list_for_each_entry(pos, &dsos, node)
129 dso__fprintf(pos, fp);
130}
131
450aaa2b
PZ
132static int load_kernel(void)
133{
a827c875 134 int err;
450aaa2b 135
0085c954 136 kernel_dso = dso__new("[kernel]", 0);
450aaa2b 137 if (!kernel_dso)
a2928c42 138 return -1;
450aaa2b 139
69ee69f6 140 err = dso__load_kernel(kernel_dso, vmlinux, NULL);
a2928c42
ACM
141 if (err) {
142 dso__delete(kernel_dso);
143 kernel_dso = NULL;
144 } else
145 dsos__add(kernel_dso);
450aaa2b 146
a2928c42 147 return err;
450aaa2b
PZ
148}
149
b78c07d4
ACM
150static int strcommon(const char *pathname, const char *cwd, int cwdlen)
151{
152 int n = 0;
153
154 while (pathname[n] == cwd[n] && n < cwdlen)
155 ++n;
156
157 return n;
158}
159
8fa66bdc
ACM
160struct map {
161 struct list_head node;
162 uint64_t start;
163 uint64_t end;
164 uint64_t pgoff;
165 struct dso *dso;
166};
167
b78c07d4 168static struct map *map__new(struct mmap_event *event, char *cwd, int cwdlen)
8fa66bdc
ACM
169{
170 struct map *self = malloc(sizeof(*self));
171
172 if (self != NULL) {
b78c07d4
ACM
173 const char *filename = event->filename;
174 char newfilename[PATH_MAX];
175
176 if (cwd) {
177 int n = strcommon(filename, cwd, cwdlen);
178 if (n == cwdlen) {
179 snprintf(newfilename, sizeof(newfilename),
180 ".%s", filename + n);
181 filename = newfilename;
182 }
183 }
184
8fa66bdc
ACM
185 self->start = event->start;
186 self->end = event->start + event->len;
187 self->pgoff = event->pgoff;
188
b78c07d4 189 self->dso = dsos__findnew(filename);
8fa66bdc
ACM
190 if (self->dso == NULL)
191 goto out_delete;
192 }
193 return self;
194out_delete:
195 free(self);
196 return NULL;
197}
198
3a4b8cc7
ACM
199struct thread;
200
8fa66bdc 201struct thread {
ce7e4365 202 struct rb_node rb_node;
8fa66bdc 203 struct list_head maps;
8fa66bdc
ACM
204 pid_t pid;
205 char *comm;
206};
207
208static struct thread *thread__new(pid_t pid)
209{
210 struct thread *self = malloc(sizeof(*self));
211
212 if (self != NULL) {
213 self->pid = pid;
8229289b 214 self->comm = malloc(32);
0a520c63 215 if (self->comm)
8229289b 216 snprintf(self->comm, 32, ":%d", self->pid);
8fa66bdc 217 INIT_LIST_HEAD(&self->maps);
8fa66bdc
ACM
218 }
219
220 return self;
221}
222
8fa66bdc
ACM
223static int thread__set_comm(struct thread *self, const char *comm)
224{
8229289b
PZ
225 if (self->comm)
226 free(self->comm);
8fa66bdc
ACM
227 self->comm = strdup(comm);
228 return self->comm ? 0 : -ENOMEM;
229}
230
16f762a2 231static struct rb_root threads;
eed4dcd4 232static struct thread *last_match;
8fa66bdc 233
ce7e4365 234static struct thread *threads__findnew(pid_t pid)
8fa66bdc 235{
ce7e4365
ACM
236 struct rb_node **p = &threads.rb_node;
237 struct rb_node *parent = NULL;
238 struct thread *th;
8fa66bdc 239
eed4dcd4
IM
240 /*
241 * Font-end cache - PID lookups come in blocks,
242 * so most of the time we dont have to look up
243 * the full rbtree:
244 */
245 if (last_match && last_match->pid == pid)
246 return last_match;
247
ce7e4365
ACM
248 while (*p != NULL) {
249 parent = *p;
250 th = rb_entry(parent, struct thread, rb_node);
8fa66bdc 251
eed4dcd4
IM
252 if (th->pid == pid) {
253 last_match = th;
ce7e4365 254 return th;
eed4dcd4 255 }
8fa66bdc 256
ce7e4365
ACM
257 if (pid < th->pid)
258 p = &(*p)->rb_left;
259 else
260 p = &(*p)->rb_right;
8fa66bdc
ACM
261 }
262
ce7e4365
ACM
263 th = thread__new(pid);
264 if (th != NULL) {
265 rb_link_node(&th->rb_node, parent, p);
266 rb_insert_color(&th->rb_node, &threads);
eed4dcd4 267 last_match = th;
ce7e4365 268 }
eed4dcd4 269
ce7e4365 270 return th;
8fa66bdc
ACM
271}
272
273static void thread__insert_map(struct thread *self, struct map *map)
274{
275 list_add_tail(&map->node, &self->maps);
276}
277
278static struct map *thread__find_map(struct thread *self, uint64_t ip)
279{
16f762a2
IM
280 struct map *pos;
281
8fa66bdc
ACM
282 if (self == NULL)
283 return NULL;
284
8fa66bdc
ACM
285 list_for_each_entry(pos, &self->maps, node)
286 if (ip >= pos->start && ip <= pos->end)
287 return pos;
288
289 return NULL;
290}
291
e7fb08b1
PZ
292/*
293 * histogram, sorted on item, collects counts
294 */
295
296static struct rb_root hist;
297
298struct hist_entry {
299 struct rb_node rb_node;
300
301 struct thread *thread;
302 struct map *map;
303 struct dso *dso;
304 struct symbol *sym;
305 uint64_t ip;
306 char level;
307
308 uint32_t count;
309};
310
1aa16738
PZ
311/*
312 * configurable sorting bits
313 */
314
315struct sort_entry {
316 struct list_head list;
317
ca8cdeef
PZ
318 char *header;
319
1aa16738 320 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
8229289b 321 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
1aa16738
PZ
322 size_t (*print)(FILE *fp, struct hist_entry *);
323};
324
8229289b
PZ
325/* --sort pid */
326
e7fb08b1 327static int64_t
1aa16738 328sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
e7fb08b1 329{
1aa16738
PZ
330 return right->thread->pid - left->thread->pid;
331}
332
333static size_t
334sort__thread_print(FILE *fp, struct hist_entry *self)
335{
cf25c63c 336 return fprintf(fp, " %16s:%5d", self->thread->comm ?: "", self->thread->pid);
1aa16738 337}
e7fb08b1 338
1aa16738 339static struct sort_entry sort_thread = {
cf25c63c 340 .header = " Command: Pid ",
1aa16738
PZ
341 .cmp = sort__thread_cmp,
342 .print = sort__thread_print,
343};
344
8229289b
PZ
345/* --sort comm */
346
992444b1
PZ
347static int64_t
348sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
8229289b
PZ
349{
350 return right->thread->pid - left->thread->pid;
351}
352
353static int64_t
354sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
992444b1
PZ
355{
356 char *comm_l = left->thread->comm;
357 char *comm_r = right->thread->comm;
358
359 if (!comm_l || !comm_r) {
360 if (!comm_l && !comm_r)
361 return 0;
362 else if (!comm_l)
363 return -1;
364 else
365 return 1;
366 }
367
368 return strcmp(comm_l, comm_r);
369}
370
371static size_t
372sort__comm_print(FILE *fp, struct hist_entry *self)
373{
0a520c63 374 return fprintf(fp, " %16s", self->thread->comm);
992444b1
PZ
375}
376
377static struct sort_entry sort_comm = {
8229289b
PZ
378 .header = " Command",
379 .cmp = sort__comm_cmp,
380 .collapse = sort__comm_collapse,
381 .print = sort__comm_print,
992444b1
PZ
382};
383
8229289b
PZ
384/* --sort dso */
385
55e5ec41
PZ
386static int64_t
387sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
388{
389 struct dso *dso_l = left->dso;
390 struct dso *dso_r = right->dso;
391
392 if (!dso_l || !dso_r) {
393 if (!dso_l && !dso_r)
394 return 0;
395 else if (!dso_l)
396 return -1;
397 else
398 return 1;
399 }
400
401 return strcmp(dso_l->name, dso_r->name);
402}
403
404static size_t
405sort__dso_print(FILE *fp, struct hist_entry *self)
406{
0a520c63
IM
407 if (self->dso)
408 return fprintf(fp, " %-25s", self->dso->name);
409
410 return fprintf(fp, " %016llx", (__u64)self->ip);
55e5ec41
PZ
411}
412
413static struct sort_entry sort_dso = {
cf25c63c 414 .header = " Shared Object ",
55e5ec41
PZ
415 .cmp = sort__dso_cmp,
416 .print = sort__dso_print,
417};
418
8229289b
PZ
419/* --sort symbol */
420
1aa16738
PZ
421static int64_t
422sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
423{
424 uint64_t ip_l, ip_r;
e7fb08b1
PZ
425
426 if (left->sym == right->sym)
427 return 0;
428
429 ip_l = left->sym ? left->sym->start : left->ip;
430 ip_r = right->sym ? right->sym->start : right->ip;
431
432 return (int64_t)(ip_r - ip_l);
433}
434
1aa16738
PZ
435static size_t
436sort__sym_print(FILE *fp, struct hist_entry *self)
437{
438 size_t ret = 0;
439
1aa16738 440 if (verbose)
0a520c63
IM
441 ret += fprintf(fp, " %#018llx", (__u64)self->ip);
442
443 if (self->dso)
444 ret += fprintf(fp, " %s: ", self->dso->name);
445 else
446 ret += fprintf(fp, " %#016llx: ", (__u64)self->ip);
1aa16738 447
0a520c63
IM
448 if (self->sym)
449 ret += fprintf(fp, "%s", self->sym->name);
450 else
451 ret += fprintf(fp, "%#016llx", (__u64)self->ip);
1aa16738
PZ
452
453 return ret;
454}
455
456static struct sort_entry sort_sym = {
4593bba8 457 .header = " Shared Object: Symbol",
ca8cdeef
PZ
458 .cmp = sort__sym_cmp,
459 .print = sort__sym_print,
1aa16738
PZ
460};
461
8229289b
PZ
462static int sort__need_collapse = 0;
463
37f440cb
PZ
464struct sort_dimension {
465 char *name;
466 struct sort_entry *entry;
467 int taken;
468};
469
470static struct sort_dimension sort_dimensions[] = {
471 { .name = "pid", .entry = &sort_thread, },
992444b1 472 { .name = "comm", .entry = &sort_comm, },
55e5ec41 473 { .name = "dso", .entry = &sort_dso, },
37f440cb
PZ
474 { .name = "symbol", .entry = &sort_sym, },
475};
476
1aa16738
PZ
477static LIST_HEAD(hist_entry__sort_list);
478
37f440cb
PZ
479static int sort_dimension__add(char *tok)
480{
481 int i;
482
483 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
484 struct sort_dimension *sd = &sort_dimensions[i];
485
486 if (sd->taken)
487 continue;
488
5352f35d 489 if (strncasecmp(tok, sd->name, strlen(tok)))
37f440cb
PZ
490 continue;
491
8229289b
PZ
492 if (sd->entry->collapse)
493 sort__need_collapse = 1;
494
37f440cb
PZ
495 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
496 sd->taken = 1;
5352f35d 497
37f440cb
PZ
498 return 0;
499 }
500
501 return -ESRCH;
502}
503
1aa16738
PZ
504static int64_t
505hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
506{
507 struct sort_entry *se;
508 int64_t cmp = 0;
509
510 list_for_each_entry(se, &hist_entry__sort_list, list) {
511 cmp = se->cmp(left, right);
512 if (cmp)
513 break;
514 }
515
516 return cmp;
517}
518
8229289b
PZ
519static int64_t
520hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
521{
522 struct sort_entry *se;
523 int64_t cmp = 0;
524
525 list_for_each_entry(se, &hist_entry__sort_list, list) {
526 int64_t (*f)(struct hist_entry *, struct hist_entry *);
527
528 f = se->collapse ?: se->cmp;
529
530 cmp = f(left, right);
531 if (cmp)
532 break;
533 }
534
535 return cmp;
536}
537
1aa16738
PZ
538static size_t
539hist_entry__fprintf(FILE *fp, struct hist_entry *self, uint64_t total_samples)
540{
541 struct sort_entry *se;
542 size_t ret;
543
544 if (total_samples) {
e98e96fe 545 ret = fprintf(fp, " %6.2f%%",
1aa16738
PZ
546 (self->count * 100.0) / total_samples);
547 } else
548 ret = fprintf(fp, "%12d ", self->count);
549
550 list_for_each_entry(se, &hist_entry__sort_list, list)
551 ret += se->print(fp, self);
552
553 ret += fprintf(fp, "\n");
554
555 return ret;
556}
557
558/*
559 * collect histogram counts
560 */
561
e7fb08b1
PZ
562static int
563hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
564 struct symbol *sym, uint64_t ip, char level)
8fa66bdc 565{
e7fb08b1
PZ
566 struct rb_node **p = &hist.rb_node;
567 struct rb_node *parent = NULL;
568 struct hist_entry *he;
569 struct hist_entry entry = {
570 .thread = thread,
571 .map = map,
572 .dso = dso,
573 .sym = sym,
574 .ip = ip,
575 .level = level,
576 .count = 1,
577 };
578 int cmp;
579
580 while (*p != NULL) {
581 parent = *p;
582 he = rb_entry(parent, struct hist_entry, rb_node);
583
584 cmp = hist_entry__cmp(&entry, he);
585
586 if (!cmp) {
587 he->count++;
588 return 0;
589 }
590
591 if (cmp < 0)
592 p = &(*p)->rb_left;
593 else
594 p = &(*p)->rb_right;
ce7e4365 595 }
e7fb08b1
PZ
596
597 he = malloc(sizeof(*he));
598 if (!he)
599 return -ENOMEM;
600 *he = entry;
601 rb_link_node(&he->rb_node, parent, p);
602 rb_insert_color(&he->rb_node, &hist);
603
604 return 0;
8fa66bdc
ACM
605}
606
8229289b
PZ
607static void hist_entry__free(struct hist_entry *he)
608{
609 free(he);
610}
611
612/*
613 * collapse the histogram
614 */
615
616static struct rb_root collapse_hists;
617
618static void collapse__insert_entry(struct hist_entry *he)
619{
620 struct rb_node **p = &collapse_hists.rb_node;
621 struct rb_node *parent = NULL;
622 struct hist_entry *iter;
623 int64_t cmp;
624
625 while (*p != NULL) {
626 parent = *p;
627 iter = rb_entry(parent, struct hist_entry, rb_node);
628
629 cmp = hist_entry__collapse(iter, he);
630
631 if (!cmp) {
632 iter->count += he->count;
633 hist_entry__free(he);
634 return;
635 }
636
637 if (cmp < 0)
638 p = &(*p)->rb_left;
639 else
640 p = &(*p)->rb_right;
641 }
642
643 rb_link_node(&he->rb_node, parent, p);
644 rb_insert_color(&he->rb_node, &collapse_hists);
645}
646
647static void collapse__resort(void)
648{
649 struct rb_node *next;
650 struct hist_entry *n;
651
652 if (!sort__need_collapse)
653 return;
654
655 next = rb_first(&hist);
656 while (next) {
657 n = rb_entry(next, struct hist_entry, rb_node);
658 next = rb_next(&n->rb_node);
659
660 rb_erase(&n->rb_node, &hist);
661 collapse__insert_entry(n);
662 }
663}
664
e7fb08b1
PZ
665/*
666 * reverse the map, sort on count.
667 */
668
669static struct rb_root output_hists;
670
671static void output__insert_entry(struct hist_entry *he)
3a4b8cc7 672{
e7fb08b1 673 struct rb_node **p = &output_hists.rb_node;
3a4b8cc7 674 struct rb_node *parent = NULL;
e7fb08b1 675 struct hist_entry *iter;
3a4b8cc7
ACM
676
677 while (*p != NULL) {
678 parent = *p;
e7fb08b1 679 iter = rb_entry(parent, struct hist_entry, rb_node);
3a4b8cc7 680
e7fb08b1 681 if (he->count > iter->count)
3a4b8cc7
ACM
682 p = &(*p)->rb_left;
683 else
684 p = &(*p)->rb_right;
685 }
686
e7fb08b1
PZ
687 rb_link_node(&he->rb_node, parent, p);
688 rb_insert_color(&he->rb_node, &output_hists);
3a4b8cc7
ACM
689}
690
e7fb08b1 691static void output__resort(void)
3a4b8cc7 692{
8229289b 693 struct rb_node *next;
e7fb08b1 694 struct hist_entry *n;
3a4b8cc7 695
8229289b
PZ
696 if (sort__need_collapse)
697 next = rb_first(&collapse_hists);
698 else
699 next = rb_first(&hist);
700
e7fb08b1
PZ
701 while (next) {
702 n = rb_entry(next, struct hist_entry, rb_node);
703 next = rb_next(&n->rb_node);
3a4b8cc7 704
e7fb08b1
PZ
705 rb_erase(&n->rb_node, &hist);
706 output__insert_entry(n);
3a4b8cc7
ACM
707 }
708}
709
e7fb08b1 710static size_t output__fprintf(FILE *fp, uint64_t total_samples)
3a4b8cc7 711{
e7fb08b1 712 struct hist_entry *pos;
2d65537e 713 struct sort_entry *se;
3a4b8cc7
ACM
714 struct rb_node *nd;
715 size_t ret = 0;
716
ca8cdeef
PZ
717 fprintf(fp, "#\n");
718
719 fprintf(fp, "# Overhead");
720 list_for_each_entry(se, &hist_entry__sort_list, list)
721 fprintf(fp, " %s", se->header);
722 fprintf(fp, "\n");
723
724 fprintf(fp, "# ........");
2d65537e 725 list_for_each_entry(se, &hist_entry__sort_list, list) {
ca8cdeef
PZ
726 int i;
727
4593bba8
IM
728 fprintf(fp, " ");
729 for (i = 0; i < strlen(se->header)-1; i++)
ca8cdeef 730 fprintf(fp, ".");
2d65537e 731 }
ca8cdeef
PZ
732 fprintf(fp, "\n");
733
734 fprintf(fp, "#\n");
2d65537e 735
e7fb08b1
PZ
736 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
737 pos = rb_entry(nd, struct hist_entry, rb_node);
738 ret += hist_entry__fprintf(fp, pos, total_samples);
3a4b8cc7
ACM
739 }
740
741 return ret;
742}
743
436224a6
PZ
744static void register_idle_thread(void)
745{
746 struct thread *thread = threads__findnew(0);
747
748 if (thread == NULL ||
749 thread__set_comm(thread, "[idle]")) {
750 fprintf(stderr, "problem inserting idle task.\n");
751 exit(-1);
752 }
753}
754
e7fb08b1 755
53cb8bc2 756static int __cmd_report(void)
8fa66bdc
ACM
757{
758 unsigned long offset = 0;
759 unsigned long head = 0;
760 struct stat stat;
761 char *buf;
762 event_t *event;
763 int ret, rc = EXIT_FAILURE;
6142f9ec 764 uint32_t size;
f49515b1 765 unsigned long total = 0, total_mmap = 0, total_comm = 0, total_unknown = 0;
b78c07d4
ACM
766 char cwd[PATH_MAX], *cwdp = cwd;
767 int cwdlen;
8fa66bdc 768
436224a6
PZ
769 register_idle_thread();
770
8fa66bdc
ACM
771 input = open(input_name, O_RDONLY);
772 if (input < 0) {
773 perror("failed to open file");
774 exit(-1);
775 }
776
777 ret = fstat(input, &stat);
778 if (ret < 0) {
779 perror("failed to stat file");
780 exit(-1);
781 }
782
783 if (!stat.st_size) {
784 fprintf(stderr, "zero-sized file, nothing to do!\n");
785 exit(0);
786 }
787
450aaa2b 788 if (load_kernel() < 0) {
a2928c42 789 perror("failed to load kernel symbols");
8fa66bdc
ACM
790 return EXIT_FAILURE;
791 }
792
b78c07d4
ACM
793 if (!full_paths) {
794 if (getcwd(cwd, sizeof(cwd)) == NULL) {
795 perror("failed to get the current directory");
796 return EXIT_FAILURE;
797 }
798 cwdlen = strlen(cwd);
10a28255 799 } else {
b78c07d4 800 cwdp = NULL;
10a28255
MG
801 cwdlen = 0;
802 }
8fa66bdc
ACM
803remap:
804 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
805 MAP_SHARED, input, offset);
806 if (buf == MAP_FAILED) {
807 perror("failed to mmap file");
808 exit(-1);
809 }
810
811more:
812 event = (event_t *)(buf + head);
813
6142f9ec
PZ
814 size = event->header.size;
815 if (!size)
816 size = 8;
817
8fa66bdc
ACM
818 if (head + event->header.size >= page_size * mmap_window) {
819 unsigned long shift = page_size * (head / page_size);
820 int ret;
821
822 ret = munmap(buf, page_size * mmap_window);
823 assert(ret == 0);
824
825 offset += shift;
826 head -= shift;
827 goto remap;
828 }
829
6142f9ec
PZ
830 size = event->header.size;
831 if (!size)
832 goto broken_event;
8fa66bdc 833
8fa66bdc
ACM
834 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) {
835 char level;
836 int show = 0;
837 struct dso *dso = NULL;
838 struct thread *thread = threads__findnew(event->ip.pid);
f17e04af 839 uint64_t ip = event->ip.ip;
e7fb08b1 840 struct map *map = NULL;
8fa66bdc 841
3502973d
IM
842 dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
843 (void *)(offset + head),
844 (void *)(long)(event->header.size),
845 event->header.misc,
846 event->ip.pid,
847 (void *)(long)ip);
97b07b69 848
ed966aac
IM
849 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
850
ce7e4365 851 if (thread == NULL) {
55717314 852 fprintf(stderr, "problem processing %d event, skipping it.\n",
ce7e4365 853 event->header.type);
55717314 854 goto broken_event;
ce7e4365 855 }
8fa66bdc
ACM
856
857 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
858 show = SHOW_KERNEL;
859 level = 'k';
e7fb08b1 860
8fa66bdc 861 dso = kernel_dso;
e7fb08b1 862
ed966aac
IM
863 dprintf(" ...... dso: %s\n", dso->name);
864
8fa66bdc 865 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
16f762a2 866
8fa66bdc
ACM
867 show = SHOW_USER;
868 level = '.';
16f762a2
IM
869
870 map = thread__find_map(thread, ip);
f17e04af 871 if (map != NULL) {
8fa66bdc 872 dso = map->dso;
f17e04af 873 ip -= map->start + map->pgoff;
ed966aac
IM
874 } else {
875 /*
876 * If this is outside of all known maps,
877 * and is a negative address, try to look it
878 * up in the kernel dso, as it might be a
879 * vsyscall (which executes in user-mode):
880 */
881 if ((long long)ip < 0)
882 dso = kernel_dso;
f17e04af 883 }
ed966aac 884 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
e7fb08b1 885
8fa66bdc
ACM
886 } else {
887 show = SHOW_HV;
888 level = 'H';
ed966aac 889 dprintf(" ...... dso: [hypervisor]\n");
8fa66bdc
ACM
890 }
891
892 if (show & show_mask) {
f17e04af 893 struct symbol *sym = dso__find_symbol(dso, ip);
8fa66bdc 894
e7fb08b1
PZ
895 if (hist_entry__add(thread, map, dso, sym, ip, level)) {
896 fprintf(stderr,
55717314
IM
897 "problem incrementing symbol count, skipping event\n");
898 goto broken_event;
ce7e4365 899 }
8fa66bdc
ACM
900 }
901 total++;
902 } else switch (event->header.type) {
903 case PERF_EVENT_MMAP: {
904 struct thread *thread = threads__findnew(event->mmap.pid);
b78c07d4 905 struct map *map = map__new(&event->mmap, cwdp, cwdlen);
8fa66bdc 906
3502973d
IM
907 dprintf("%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n",
908 (void *)(offset + head),
909 (void *)(long)(event->header.size),
910 (void *)(long)event->mmap.start,
911 (void *)(long)event->mmap.len,
912 (void *)(long)event->mmap.pgoff,
913 event->mmap.filename);
914
ce7e4365 915 if (thread == NULL || map == NULL) {
0a520c63
IM
916 if (verbose)
917 fprintf(stderr, "problem processing PERF_EVENT_MMAP, skipping event.\n");
55717314 918 goto broken_event;
ce7e4365 919 }
8fa66bdc 920 thread__insert_map(thread, map);
97b07b69 921 total_mmap++;
8fa66bdc
ACM
922 break;
923 }
924 case PERF_EVENT_COMM: {
925 struct thread *thread = threads__findnew(event->comm.pid);
926
3502973d
IM
927 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
928 (void *)(offset + head),
929 (void *)(long)(event->header.size),
930 event->comm.comm, event->comm.pid);
931
8fa66bdc 932 if (thread == NULL ||
ce7e4365 933 thread__set_comm(thread, event->comm.comm)) {
55717314
IM
934 fprintf(stderr, "problem processing PERF_EVENT_COMM, skipping event.\n");
935 goto broken_event;
ce7e4365 936 }
97b07b69 937 total_comm++;
8fa66bdc
ACM
938 break;
939 }
97b07b69 940 default: {
6142f9ec 941broken_event:
3502973d
IM
942 dprintf("%p [%p]: skipping unknown header type: %d\n",
943 (void *)(offset + head),
944 (void *)(long)(event->header.size),
945 event->header.type);
b7a16eac 946
3e706114 947 total_unknown++;
6142f9ec
PZ
948
949 /*
950 * assume we lost track of the stream, check alignment, and
951 * increment a single u64 in the hope to catch on again 'soon'.
952 */
953
954 if (unlikely(head & 7))
955 head &= ~7ULL;
956
957 size = 8;
97b07b69 958 }
8fa66bdc
ACM
959 }
960
6142f9ec 961 head += size;
f49515b1 962
8fa66bdc
ACM
963 if (offset + head < stat.st_size)
964 goto more;
965
966 rc = EXIT_SUCCESS;
8fa66bdc 967 close(input);
97b07b69 968
3502973d
IM
969 dprintf(" IP events: %10ld\n", total);
970 dprintf(" mmap events: %10ld\n", total_mmap);
971 dprintf(" comm events: %10ld\n", total_comm);
972 dprintf(" unknown events: %10ld\n", total_unknown);
97b07b69 973
3502973d 974 if (dump_trace)
97b07b69 975 return 0;
97b07b69 976
e7fb08b1 977 if (verbose >= 2)
16f762a2 978 dsos__fprintf(stdout);
16f762a2 979
8229289b 980 collapse__resort();
e7fb08b1
PZ
981 output__resort();
982 output__fprintf(stdout, total);
8fa66bdc 983
8fa66bdc
ACM
984 return rc;
985}
986
53cb8bc2
IM
987static const char * const report_usage[] = {
988 "perf report [<options>] <command>",
989 NULL
990};
991
992static const struct option options[] = {
993 OPT_STRING('i', "input", &input_name, "file",
994 "input file name"),
815e777f
ACM
995 OPT_BOOLEAN('v', "verbose", &verbose,
996 "be more verbose (show symbol address, etc)"),
97b07b69
IM
997 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
998 "dump raw trace in ASCII"),
450aaa2b 999 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
63299f05
IM
1000 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1001 "sort by key(s): pid, comm, dso, symbol. Default: pid,symbol"),
b78c07d4
ACM
1002 OPT_BOOLEAN('P', "full-paths", &full_paths,
1003 "Don't shorten the pathnames taking into account the cwd"),
53cb8bc2
IM
1004 OPT_END()
1005};
1006
5352f35d
IM
1007static void setup_sorting(void)
1008{
1009 char *tmp, *tok, *str = strdup(sort_order);
1010
1011 for (tok = strtok_r(str, ", ", &tmp);
1012 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1013 if (sort_dimension__add(tok) < 0) {
1014 error("Unknown --sort key: `%s'", tok);
1015 usage_with_options(report_usage, options);
1016 }
1017 }
1018
1019 free(str);
1020}
1021
53cb8bc2
IM
1022int cmd_report(int argc, const char **argv, const char *prefix)
1023{
a2928c42 1024 symbol__init();
53cb8bc2
IM
1025
1026 page_size = getpagesize();
1027
1028 parse_options(argc, argv, options, report_usage, 0);
1029
1aa16738
PZ
1030 setup_sorting();
1031
a930d2c0
IM
1032 setup_pager();
1033
53cb8bc2
IM
1034 return __cmd_report();
1035}
This page took 0.100954 seconds and 5 git commands to generate.