perf_counter tools: report: Implement header output for --sort variants
[deliverable/linux.git] / Documentation / perf_counter / builtin-report.c
CommitLineData
53cb8bc2 1#include "util/util.h"
16f762a2 2#include "builtin.h"
53cb8bc2
IM
3
4#include <libelf.h>
62eb9390
ACM
5#include <gelf.h>
6#include <elf.h>
53cb8bc2 7
35a50c8a 8#include "util/list.h"
a930d2c0 9#include "util/cache.h"
35a50c8a 10#include "util/rbtree.h"
8fa66bdc 11
53cb8bc2
IM
12#include "perf.h"
13
14#include "util/parse-options.h"
15#include "util/parse-events.h"
16
8fa66bdc
ACM
17#define SHOW_KERNEL 1
18#define SHOW_USER 2
19#define SHOW_HV 4
20
23ac9cbe 21static char const *input_name = "perf.data";
450aaa2b 22static char *vmlinux = NULL;
37f440cb 23static char *sort_order = "pid,symbol";
8fa66bdc
ACM
24static int input;
25static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
26
97b07b69 27static int dump_trace = 0;
16f762a2 28static int verbose;
97b07b69 29
8fa66bdc
ACM
30static unsigned long page_size;
31static unsigned long mmap_window = 32;
32
53cb8bc2 33const char *perf_event_names[] = {
8fa66bdc
ACM
34 [PERF_EVENT_MMAP] = " PERF_EVENT_MMAP",
35 [PERF_EVENT_MUNMAP] = " PERF_EVENT_MUNMAP",
36 [PERF_EVENT_COMM] = " PERF_EVENT_COMM",
37};
38
39struct ip_event {
40 struct perf_event_header header;
41 __u64 ip;
42 __u32 pid, tid;
43};
44struct mmap_event {
45 struct perf_event_header header;
46 __u32 pid, tid;
47 __u64 start;
48 __u64 len;
49 __u64 pgoff;
50 char filename[PATH_MAX];
51};
52struct comm_event {
53 struct perf_event_header header;
54 __u32 pid,tid;
55 char comm[16];
56};
57
58typedef union event_union {
59 struct perf_event_header header;
60 struct ip_event ip;
61 struct mmap_event mmap;
62 struct comm_event comm;
63} event_t;
64
8fa66bdc 65struct symbol {
16f762a2
IM
66 struct rb_node rb_node;
67 __u64 start;
68 __u64 end;
69 char name[0];
8fa66bdc
ACM
70};
71
72static struct symbol *symbol__new(uint64_t start, uint64_t len, const char *name)
73{
74 struct symbol *self = malloc(sizeof(*self) + strlen(name) + 1);
75
76 if (self != NULL) {
77 self->start = start;
78 self->end = start + len;
79 strcpy(self->name, name);
80 }
81
82 return self;
83}
84
85static void symbol__delete(struct symbol *self)
86{
87 free(self);
88}
89
90static size_t symbol__fprintf(struct symbol *self, FILE *fp)
91{
16f762a2 92 return fprintf(fp, " %llx-%llx %s\n",
8fa66bdc
ACM
93 self->start, self->end, self->name);
94}
95
96struct dso {
97 struct list_head node;
35a50c8a 98 struct rb_root syms;
8fa66bdc
ACM
99 char name[0];
100};
101
102static struct dso *dso__new(const char *name)
103{
104 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
105
106 if (self != NULL) {
107 strcpy(self->name, name);
35a50c8a 108 self->syms = RB_ROOT;
8fa66bdc
ACM
109 }
110
111 return self;
112}
113
8fa66bdc
ACM
114static void dso__delete_symbols(struct dso *self)
115{
35a50c8a
ACM
116 struct symbol *pos;
117 struct rb_node *next = rb_first(&self->syms);
8fa66bdc 118
35a50c8a
ACM
119 while (next) {
120 pos = rb_entry(next, struct symbol, rb_node);
121 next = rb_next(&pos->rb_node);
8fa66bdc 122 symbol__delete(pos);
35a50c8a 123 }
8fa66bdc
ACM
124}
125
126static void dso__delete(struct dso *self)
127{
8fa66bdc
ACM
128 dso__delete_symbols(self);
129 free(self);
130}
131
132static void dso__insert_symbol(struct dso *self, struct symbol *sym)
133{
35a50c8a
ACM
134 struct rb_node **p = &self->syms.rb_node;
135 struct rb_node *parent = NULL;
136 const uint64_t ip = sym->start;
137 struct symbol *s;
138
139 while (*p != NULL) {
140 parent = *p;
141 s = rb_entry(parent, struct symbol, rb_node);
142 if (ip < s->start)
143 p = &(*p)->rb_left;
144 else
145 p = &(*p)->rb_right;
146 }
147 rb_link_node(&sym->rb_node, parent, p);
148 rb_insert_color(&sym->rb_node, &self->syms);
8fa66bdc
ACM
149}
150
151static struct symbol *dso__find_symbol(struct dso *self, uint64_t ip)
152{
16f762a2
IM
153 struct rb_node *n;
154
8fa66bdc
ACM
155 if (self == NULL)
156 return NULL;
157
16f762a2 158 n = self->syms.rb_node;
8fa66bdc 159
35a50c8a
ACM
160 while (n) {
161 struct symbol *s = rb_entry(n, struct symbol, rb_node);
162
163 if (ip < s->start)
164 n = n->rb_left;
165 else if (ip > s->end)
166 n = n->rb_right;
167 else
168 return s;
169 }
8fa66bdc
ACM
170
171 return NULL;
172}
173
62eb9390
ACM
174/**
175 * elf_symtab__for_each_symbol - iterate thru all the symbols
176 *
177 * @self: struct elf_symtab instance to iterate
178 * @index: uint32_t index
179 * @sym: GElf_Sym iterator
180 */
181#define elf_symtab__for_each_symbol(syms, nr_syms, index, sym) \
182 for (index = 0, gelf_getsym(syms, index, &sym);\
183 index < nr_syms; \
184 index++, gelf_getsym(syms, index, &sym))
185
186static inline uint8_t elf_sym__type(const GElf_Sym *sym)
187{
188 return GELF_ST_TYPE(sym->st_info);
189}
190
53cb8bc2 191static inline int elf_sym__is_function(const GElf_Sym *sym)
62eb9390
ACM
192{
193 return elf_sym__type(sym) == STT_FUNC &&
194 sym->st_name != 0 &&
b7a16eac
PZ
195 sym->st_shndx != SHN_UNDEF &&
196 sym->st_size != 0;
62eb9390
ACM
197}
198
199static inline const char *elf_sym__name(const GElf_Sym *sym,
200 const Elf_Data *symstrs)
201{
202 return symstrs->d_buf + sym->st_name;
203}
204
205static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
206 GElf_Shdr *shp, const char *name,
207 size_t *index)
208{
209 Elf_Scn *sec = NULL;
210 size_t cnt = 1;
211
212 while ((sec = elf_nextscn(elf, sec)) != NULL) {
213 char *str;
214
215 gelf_getshdr(sec, shp);
216 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
217 if (!strcmp(name, str)) {
218 if (index)
219 *index = cnt;
220 break;
221 }
222 ++cnt;
223 }
224
225 return sec;
226}
227
b7a16eac 228static int dso__load_sym(struct dso *self, int fd, char *name)
8fa66bdc 229{
16f762a2
IM
230 Elf_Data *symstrs;
231 uint32_t nr_syms;
b7a16eac 232 int err = -1;
16f762a2
IM
233 uint32_t index;
234 GElf_Ehdr ehdr;
235 GElf_Shdr shdr;
236 Elf_Data *syms;
237 GElf_Sym sym;
238 Elf_Scn *sec;
239 Elf *elf;
b7a16eac 240 int nr = 0;
62eb9390 241
16f762a2 242 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
62eb9390
ACM
243 if (elf == NULL) {
244 fprintf(stderr, "%s: cannot read %s ELF file.\n",
b7a16eac 245 __func__, name);
62eb9390
ACM
246 goto out_close;
247 }
248
62eb9390
ACM
249 if (gelf_getehdr(elf, &ehdr) == NULL) {
250 fprintf(stderr, "%s: cannot get elf header.\n", __func__);
251 goto out_elf_end;
252 }
253
16f762a2 254 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
62eb9390
ACM
255 if (sec == NULL)
256 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
257
258 if (sec == NULL)
259 goto out_elf_end;
260
16f762a2 261 syms = elf_getdata(sec, NULL);
62eb9390
ACM
262 if (syms == NULL)
263 goto out_elf_end;
264
265 sec = elf_getscn(elf, shdr.sh_link);
266 if (sec == NULL)
267 goto out_elf_end;
268
16f762a2 269 symstrs = elf_getdata(sec, NULL);
62eb9390
ACM
270 if (symstrs == NULL)
271 goto out_elf_end;
272
16f762a2 273 nr_syms = shdr.sh_size / shdr.sh_entsize;
62eb9390 274
62eb9390 275 elf_symtab__for_each_symbol(syms, nr_syms, index, sym) {
f17e04af
PZ
276 struct symbol *f;
277
62eb9390
ACM
278 if (!elf_sym__is_function(&sym))
279 continue;
f17e04af
PZ
280
281 sec = elf_getscn(elf, sym.st_shndx);
282 if (!sec)
283 goto out_elf_end;
284
285 gelf_getshdr(sec, &shdr);
286 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
287
288 f = symbol__new(sym.st_value, sym.st_size,
289 elf_sym__name(&sym, symstrs));
290 if (!f)
62eb9390
ACM
291 goto out_elf_end;
292
293 dso__insert_symbol(self, f);
b7a16eac
PZ
294
295 nr++;
62eb9390
ACM
296 }
297
b7a16eac 298 err = nr;
62eb9390
ACM
299out_elf_end:
300 elf_end(elf);
301out_close:
62eb9390 302 return err;
8fa66bdc
ACM
303}
304
b7a16eac
PZ
305static int dso__load(struct dso *self)
306{
307 int size = strlen(self->name) + sizeof("/usr/lib/debug%s.debug");
308 char *name = malloc(size);
309 int variant = 0;
310 int ret = -1;
311 int fd;
312
313 if (!name)
314 return -1;
315
316more:
317 do {
318 switch (variant) {
319 case 0: /* Fedora */
320 snprintf(name, size, "/usr/lib/debug%s.debug", self->name);
321 break;
322 case 1: /* Ubuntu */
323 snprintf(name, size, "/usr/lib/debug%s", self->name);
324 break;
325 case 2: /* Sane people */
326 snprintf(name, size, "%s", self->name);
327 break;
328
329 default:
330 goto out;
331 }
332 variant++;
333
334 fd = open(name, O_RDONLY);
335 } while (fd < 0);
336
337 ret = dso__load_sym(self, fd, name);
338 close(fd);
339
340 /*
341 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
342 */
343 if (!ret)
344 goto more;
345
346out:
347 free(name);
348 return ret;
349}
350
8fa66bdc
ACM
351static size_t dso__fprintf(struct dso *self, FILE *fp)
352{
8fa66bdc
ACM
353 size_t ret = fprintf(fp, "dso: %s\n", self->name);
354
35a50c8a
ACM
355 struct rb_node *nd;
356 for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
357 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
8fa66bdc 358 ret += symbol__fprintf(pos, fp);
35a50c8a 359 }
8fa66bdc
ACM
360
361 return ret;
362}
363
364static LIST_HEAD(dsos);
365static struct dso *kernel_dso;
366
367static void dsos__add(struct dso *dso)
368{
369 list_add_tail(&dso->node, &dsos);
370}
371
372static struct dso *dsos__find(const char *name)
373{
374 struct dso *pos;
375
376 list_for_each_entry(pos, &dsos, node)
377 if (strcmp(pos->name, name) == 0)
378 return pos;
379 return NULL;
380}
381
382static struct dso *dsos__findnew(const char *name)
383{
384 struct dso *dso = dsos__find(name);
b7a16eac 385 int nr;
8fa66bdc
ACM
386
387 if (dso == NULL) {
388 dso = dso__new(name);
b7a16eac
PZ
389 if (!dso)
390 goto out_delete_dso;
391
392 nr = dso__load(dso);
393 if (nr < 0) {
394 fprintf(stderr, "Failed to open: %s\n", name);
8fa66bdc 395 goto out_delete_dso;
b7a16eac
PZ
396 }
397 if (!nr) {
398 fprintf(stderr,
399 "Failed to find debug symbols for: %s, maybe install a debug package?\n",
400 name);
401 }
8fa66bdc
ACM
402
403 dsos__add(dso);
404 }
405
406 return dso;
407
408out_delete_dso:
409 dso__delete(dso);
410 return NULL;
411}
412
16f762a2 413static void dsos__fprintf(FILE *fp)
8fa66bdc
ACM
414{
415 struct dso *pos;
416
417 list_for_each_entry(pos, &dsos, node)
418 dso__fprintf(pos, fp);
419}
420
d8d1656e
ACM
421static int hex(char ch)
422{
423 if ((ch >= '0') && (ch <= '9'))
424 return ch - '0';
425 if ((ch >= 'a') && (ch <= 'f'))
426 return ch - 'a' + 10;
427 if ((ch >= 'A') && (ch <= 'F'))
428 return ch - 'A' + 10;
429 return -1;
430}
431
432/*
433 * While we find nice hex chars, build a long_val.
434 * Return number of chars processed.
435 */
16f762a2 436static int hex2long(char *ptr, unsigned long *long_val)
d8d1656e
ACM
437{
438 const char *p = ptr;
439 *long_val = 0;
440
441 while (*p) {
442 const int hex_val = hex(*p);
443
444 if (hex_val < 0)
445 break;
446
447 *long_val = (*long_val << 4) | hex_val;
448 p++;
449 }
450
451 return p - ptr;
452}
453
8fa66bdc
ACM
454static int load_kallsyms(void)
455{
af83632f
IM
456 struct rb_node *nd, *prevnd;
457 char *line = NULL;
458 FILE *file;
459 size_t n;
460
8fa66bdc
ACM
461 kernel_dso = dso__new("[kernel]");
462 if (kernel_dso == NULL)
463 return -1;
464
af83632f 465 file = fopen("/proc/kallsyms", "r");
8fa66bdc
ACM
466 if (file == NULL)
467 goto out_delete_dso;
468
8fa66bdc 469 while (!feof(file)) {
d8d1656e 470 unsigned long start;
af83632f
IM
471 struct symbol *sym;
472 int line_len, len;
473 char symbol_type;
474
475 line_len = getline(&line, &n, file);
d8d1656e 476 if (line_len < 0)
8fa66bdc
ACM
477 break;
478
479 if (!line)
480 goto out_delete_dso;
481
d8d1656e 482 line[--line_len] = '\0'; /* \n */
abd54f68 483
af83632f
IM
484 len = hex2long(line, &start);
485
03f6316d
ACM
486 len++;
487 if (len + 2 >= line_len)
488 continue;
489
af83632f 490 symbol_type = toupper(line[len]);
03f6316d
ACM
491 /*
492 * We're interested only in code ('T'ext)
493 */
af83632f 494 if (symbol_type != 'T' && symbol_type != 'W')
d8d1656e
ACM
495 continue;
496 /*
497 * Well fix up the end later, when we have all sorted.
498 */
af83632f 499 sym = symbol__new(start, 0xdead, line + len + 2);
abd54f68 500
d8d1656e
ACM
501 if (sym == NULL)
502 goto out_delete_dso;
503
504 dso__insert_symbol(kernel_dso, sym);
8fa66bdc
ACM
505 }
506
abd54f68
ACM
507 /*
508 * Now that we have all sorted out, just set the ->end of all
509 * symbols
510 */
af83632f 511 prevnd = rb_first(&kernel_dso->syms);
abd54f68
ACM
512
513 if (prevnd == NULL)
514 goto out_delete_line;
515
516 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
517 struct symbol *prev = rb_entry(prevnd, struct symbol, rb_node),
518 *curr = rb_entry(nd, struct symbol, rb_node);
519
520 prev->end = curr->start - 1;
521 prevnd = nd;
522 }
523
8fa66bdc
ACM
524 dsos__add(kernel_dso);
525 free(line);
526 fclose(file);
af83632f 527
8fa66bdc
ACM
528 return 0;
529
59d81029
ACM
530out_delete_line:
531 free(line);
8fa66bdc
ACM
532out_delete_dso:
533 dso__delete(kernel_dso);
534 return -1;
535}
536
450aaa2b
PZ
537static int load_kernel(void)
538{
539 int fd, nr;
540
541 if (!vmlinux)
542 goto kallsyms;
543
544 fd = open(vmlinux, O_RDONLY);
545 if (fd < 0)
546 goto kallsyms;
547
548 kernel_dso = dso__new("[kernel]");
549 if (!kernel_dso)
550 goto fail_open;
551
552 nr = dso__load_sym(kernel_dso, fd, vmlinux);
553
554 if (nr <= 0)
555 goto fail_load;
556
557 dsos__add(kernel_dso);
558 close(fd);
559
560 return 0;
561
562fail_load:
563 dso__delete(kernel_dso);
564fail_open:
565 close(fd);
566kallsyms:
567 return load_kallsyms();
568}
569
8fa66bdc
ACM
570struct map {
571 struct list_head node;
572 uint64_t start;
573 uint64_t end;
574 uint64_t pgoff;
575 struct dso *dso;
576};
577
578static struct map *map__new(struct mmap_event *event)
579{
580 struct map *self = malloc(sizeof(*self));
581
582 if (self != NULL) {
583 self->start = event->start;
584 self->end = event->start + event->len;
585 self->pgoff = event->pgoff;
586
587 self->dso = dsos__findnew(event->filename);
588 if (self->dso == NULL)
589 goto out_delete;
590 }
591 return self;
592out_delete:
593 free(self);
594 return NULL;
595}
596
3a4b8cc7
ACM
597struct thread;
598
8fa66bdc 599struct thread {
ce7e4365 600 struct rb_node rb_node;
8fa66bdc 601 struct list_head maps;
8fa66bdc
ACM
602 pid_t pid;
603 char *comm;
604};
605
606static struct thread *thread__new(pid_t pid)
607{
608 struct thread *self = malloc(sizeof(*self));
609
610 if (self != NULL) {
611 self->pid = pid;
612 self->comm = NULL;
613 INIT_LIST_HEAD(&self->maps);
8fa66bdc
ACM
614 }
615
616 return self;
617}
618
8fa66bdc
ACM
619static int thread__set_comm(struct thread *self, const char *comm)
620{
621 self->comm = strdup(comm);
622 return self->comm ? 0 : -ENOMEM;
623}
624
16f762a2 625static struct rb_root threads;
8fa66bdc 626
ce7e4365 627static struct thread *threads__findnew(pid_t pid)
8fa66bdc 628{
ce7e4365
ACM
629 struct rb_node **p = &threads.rb_node;
630 struct rb_node *parent = NULL;
631 struct thread *th;
8fa66bdc 632
ce7e4365
ACM
633 while (*p != NULL) {
634 parent = *p;
635 th = rb_entry(parent, struct thread, rb_node);
8fa66bdc 636
ce7e4365
ACM
637 if (th->pid == pid)
638 return th;
8fa66bdc 639
ce7e4365
ACM
640 if (pid < th->pid)
641 p = &(*p)->rb_left;
642 else
643 p = &(*p)->rb_right;
8fa66bdc
ACM
644 }
645
ce7e4365
ACM
646 th = thread__new(pid);
647 if (th != NULL) {
648 rb_link_node(&th->rb_node, parent, p);
649 rb_insert_color(&th->rb_node, &threads);
650 }
651 return th;
8fa66bdc
ACM
652}
653
654static void thread__insert_map(struct thread *self, struct map *map)
655{
656 list_add_tail(&map->node, &self->maps);
657}
658
659static struct map *thread__find_map(struct thread *self, uint64_t ip)
660{
16f762a2
IM
661 struct map *pos;
662
8fa66bdc
ACM
663 if (self == NULL)
664 return NULL;
665
8fa66bdc
ACM
666 list_for_each_entry(pos, &self->maps, node)
667 if (ip >= pos->start && ip <= pos->end)
668 return pos;
669
670 return NULL;
671}
672
e7fb08b1
PZ
673/*
674 * histogram, sorted on item, collects counts
675 */
676
677static struct rb_root hist;
678
679struct hist_entry {
680 struct rb_node rb_node;
681
682 struct thread *thread;
683 struct map *map;
684 struct dso *dso;
685 struct symbol *sym;
686 uint64_t ip;
687 char level;
688
689 uint32_t count;
690};
691
1aa16738
PZ
692/*
693 * configurable sorting bits
694 */
695
696struct sort_entry {
697 struct list_head list;
698
ca8cdeef
PZ
699 char *header;
700
1aa16738
PZ
701 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
702 size_t (*print)(FILE *fp, struct hist_entry *);
703};
704
e7fb08b1 705static int64_t
1aa16738 706sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
e7fb08b1 707{
1aa16738
PZ
708 return right->thread->pid - left->thread->pid;
709}
710
711static size_t
712sort__thread_print(FILE *fp, struct hist_entry *self)
713{
ca8cdeef 714 return fprintf(fp, " %16s:%5d", self->thread->comm ?: "", self->thread->pid);
1aa16738 715}
e7fb08b1 716
1aa16738 717static struct sort_entry sort_thread = {
ca8cdeef 718 .header = " Command: Pid ",
1aa16738
PZ
719 .cmp = sort__thread_cmp,
720 .print = sort__thread_print,
721};
722
992444b1
PZ
723static int64_t
724sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
725{
726 char *comm_l = left->thread->comm;
727 char *comm_r = right->thread->comm;
728
729 if (!comm_l || !comm_r) {
730 if (!comm_l && !comm_r)
731 return 0;
732 else if (!comm_l)
733 return -1;
734 else
735 return 1;
736 }
737
738 return strcmp(comm_l, comm_r);
739}
740
741static size_t
742sort__comm_print(FILE *fp, struct hist_entry *self)
743{
2d65537e 744 return fprintf(fp, " %16s", self->thread->comm ?: "<unknown>");
992444b1
PZ
745}
746
747static struct sort_entry sort_comm = {
ca8cdeef 748 .header = " Command",
992444b1
PZ
749 .cmp = sort__comm_cmp,
750 .print = sort__comm_print,
751};
752
55e5ec41
PZ
753static int64_t
754sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
755{
756 struct dso *dso_l = left->dso;
757 struct dso *dso_r = right->dso;
758
759 if (!dso_l || !dso_r) {
760 if (!dso_l && !dso_r)
761 return 0;
762 else if (!dso_l)
763 return -1;
764 else
765 return 1;
766 }
767
768 return strcmp(dso_l->name, dso_r->name);
769}
770
771static size_t
772sort__dso_print(FILE *fp, struct hist_entry *self)
773{
2d65537e 774 return fprintf(fp, " %64s", self->dso ? self->dso->name : "<unknown>");
55e5ec41
PZ
775}
776
777static struct sort_entry sort_dso = {
ca8cdeef 778 .header = " Shared Object",
55e5ec41
PZ
779 .cmp = sort__dso_cmp,
780 .print = sort__dso_print,
781};
782
1aa16738
PZ
783static int64_t
784sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
785{
786 uint64_t ip_l, ip_r;
e7fb08b1
PZ
787
788 if (left->sym == right->sym)
789 return 0;
790
791 ip_l = left->sym ? left->sym->start : left->ip;
792 ip_r = right->sym ? right->sym->start : right->ip;
793
794 return (int64_t)(ip_r - ip_l);
795}
796
1aa16738
PZ
797static size_t
798sort__sym_print(FILE *fp, struct hist_entry *self)
799{
800 size_t ret = 0;
801
1aa16738 802 if (verbose)
2d65537e 803 ret += fprintf(fp, " %#018llx", (unsigned long long)self->ip);
1aa16738 804
ca8cdeef
PZ
805 ret += fprintf(fp, " %s: %s",
806 self->dso ? self->dso->name : "<unknown>",
807 self->sym ? self->sym->name : "<unknown>");
1aa16738
PZ
808
809 return ret;
810}
811
812static struct sort_entry sort_sym = {
ca8cdeef
PZ
813 .header = "Shared Object: Symbol",
814 .cmp = sort__sym_cmp,
815 .print = sort__sym_print,
1aa16738
PZ
816};
817
37f440cb
PZ
818struct sort_dimension {
819 char *name;
820 struct sort_entry *entry;
821 int taken;
822};
823
824static struct sort_dimension sort_dimensions[] = {
825 { .name = "pid", .entry = &sort_thread, },
992444b1 826 { .name = "comm", .entry = &sort_comm, },
55e5ec41 827 { .name = "dso", .entry = &sort_dso, },
37f440cb
PZ
828 { .name = "symbol", .entry = &sort_sym, },
829};
830
1aa16738
PZ
831static LIST_HEAD(hist_entry__sort_list);
832
37f440cb
PZ
833static int sort_dimension__add(char *tok)
834{
835 int i;
836
837 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
838 struct sort_dimension *sd = &sort_dimensions[i];
839
840 if (sd->taken)
841 continue;
842
843 if (strcmp(tok, sd->name))
844 continue;
845
846 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
847 sd->taken = 1;
848 return 0;
849 }
850
851 return -ESRCH;
852}
853
1aa16738
PZ
854static void setup_sorting(void)
855{
37f440cb
PZ
856 char *tmp, *tok, *str = strdup(sort_order);
857
858 for (tok = strtok_r(str, ", ", &tmp);
859 tok; tok = strtok_r(NULL, ", ", &tmp))
860 sort_dimension__add(tok);
861
862 free(str);
1aa16738
PZ
863}
864
865static int64_t
866hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
867{
868 struct sort_entry *se;
869 int64_t cmp = 0;
870
871 list_for_each_entry(se, &hist_entry__sort_list, list) {
872 cmp = se->cmp(left, right);
873 if (cmp)
874 break;
875 }
876
877 return cmp;
878}
879
880static size_t
881hist_entry__fprintf(FILE *fp, struct hist_entry *self, uint64_t total_samples)
882{
883 struct sort_entry *se;
884 size_t ret;
885
886 if (total_samples) {
2d65537e 887 ret = fprintf(fp, " %5.2f%%",
1aa16738
PZ
888 (self->count * 100.0) / total_samples);
889 } else
890 ret = fprintf(fp, "%12d ", self->count);
891
892 list_for_each_entry(se, &hist_entry__sort_list, list)
893 ret += se->print(fp, self);
894
895 ret += fprintf(fp, "\n");
896
897 return ret;
898}
899
900/*
901 * collect histogram counts
902 */
903
e7fb08b1
PZ
904static int
905hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
906 struct symbol *sym, uint64_t ip, char level)
8fa66bdc 907{
e7fb08b1
PZ
908 struct rb_node **p = &hist.rb_node;
909 struct rb_node *parent = NULL;
910 struct hist_entry *he;
911 struct hist_entry entry = {
912 .thread = thread,
913 .map = map,
914 .dso = dso,
915 .sym = sym,
916 .ip = ip,
917 .level = level,
918 .count = 1,
919 };
920 int cmp;
921
922 while (*p != NULL) {
923 parent = *p;
924 he = rb_entry(parent, struct hist_entry, rb_node);
925
926 cmp = hist_entry__cmp(&entry, he);
927
928 if (!cmp) {
929 he->count++;
930 return 0;
931 }
932
933 if (cmp < 0)
934 p = &(*p)->rb_left;
935 else
936 p = &(*p)->rb_right;
ce7e4365 937 }
e7fb08b1
PZ
938
939 he = malloc(sizeof(*he));
940 if (!he)
941 return -ENOMEM;
942 *he = entry;
943 rb_link_node(&he->rb_node, parent, p);
944 rb_insert_color(&he->rb_node, &hist);
945
946 return 0;
8fa66bdc
ACM
947}
948
e7fb08b1
PZ
949/*
950 * reverse the map, sort on count.
951 */
952
953static struct rb_root output_hists;
954
955static void output__insert_entry(struct hist_entry *he)
3a4b8cc7 956{
e7fb08b1 957 struct rb_node **p = &output_hists.rb_node;
3a4b8cc7 958 struct rb_node *parent = NULL;
e7fb08b1 959 struct hist_entry *iter;
3a4b8cc7
ACM
960
961 while (*p != NULL) {
962 parent = *p;
e7fb08b1 963 iter = rb_entry(parent, struct hist_entry, rb_node);
3a4b8cc7 964
e7fb08b1 965 if (he->count > iter->count)
3a4b8cc7
ACM
966 p = &(*p)->rb_left;
967 else
968 p = &(*p)->rb_right;
969 }
970
e7fb08b1
PZ
971 rb_link_node(&he->rb_node, parent, p);
972 rb_insert_color(&he->rb_node, &output_hists);
3a4b8cc7
ACM
973}
974
e7fb08b1 975static void output__resort(void)
3a4b8cc7 976{
e7fb08b1
PZ
977 struct rb_node *next = rb_first(&hist);
978 struct hist_entry *n;
3a4b8cc7 979
e7fb08b1
PZ
980 while (next) {
981 n = rb_entry(next, struct hist_entry, rb_node);
982 next = rb_next(&n->rb_node);
3a4b8cc7 983
e7fb08b1
PZ
984 rb_erase(&n->rb_node, &hist);
985 output__insert_entry(n);
3a4b8cc7
ACM
986 }
987}
988
e7fb08b1 989static size_t output__fprintf(FILE *fp, uint64_t total_samples)
3a4b8cc7 990{
e7fb08b1 991 struct hist_entry *pos;
2d65537e 992 struct sort_entry *se;
3a4b8cc7
ACM
993 struct rb_node *nd;
994 size_t ret = 0;
995
ca8cdeef
PZ
996 fprintf(fp, "#\n");
997
998 fprintf(fp, "# Overhead");
999 list_for_each_entry(se, &hist_entry__sort_list, list)
1000 fprintf(fp, " %s", se->header);
1001 fprintf(fp, "\n");
1002
1003 fprintf(fp, "# ........");
2d65537e 1004 list_for_each_entry(se, &hist_entry__sort_list, list) {
ca8cdeef
PZ
1005 int i;
1006
1007 fprintf(fp, " ");
1008 for (i = 0; i < strlen(se->header); i++)
1009 fprintf(fp, ".");
2d65537e 1010 }
ca8cdeef
PZ
1011 fprintf(fp, "\n");
1012
1013 fprintf(fp, "#\n");
2d65537e 1014
e7fb08b1
PZ
1015 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
1016 pos = rb_entry(nd, struct hist_entry, rb_node);
1017 ret += hist_entry__fprintf(fp, pos, total_samples);
3a4b8cc7
ACM
1018 }
1019
1020 return ret;
1021}
1022
e7fb08b1 1023
53cb8bc2 1024static int __cmd_report(void)
8fa66bdc
ACM
1025{
1026 unsigned long offset = 0;
1027 unsigned long head = 0;
1028 struct stat stat;
1029 char *buf;
1030 event_t *event;
1031 int ret, rc = EXIT_FAILURE;
6142f9ec 1032 uint32_t size;
f49515b1 1033 unsigned long total = 0, total_mmap = 0, total_comm = 0, total_unknown = 0;
8fa66bdc 1034
8fa66bdc
ACM
1035 input = open(input_name, O_RDONLY);
1036 if (input < 0) {
1037 perror("failed to open file");
1038 exit(-1);
1039 }
1040
1041 ret = fstat(input, &stat);
1042 if (ret < 0) {
1043 perror("failed to stat file");
1044 exit(-1);
1045 }
1046
1047 if (!stat.st_size) {
1048 fprintf(stderr, "zero-sized file, nothing to do!\n");
1049 exit(0);
1050 }
1051
450aaa2b 1052 if (load_kernel() < 0) {
8fa66bdc
ACM
1053 perror("failed to open kallsyms");
1054 return EXIT_FAILURE;
1055 }
1056
1057remap:
1058 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1059 MAP_SHARED, input, offset);
1060 if (buf == MAP_FAILED) {
1061 perror("failed to mmap file");
1062 exit(-1);
1063 }
1064
1065more:
1066 event = (event_t *)(buf + head);
1067
6142f9ec
PZ
1068 size = event->header.size;
1069 if (!size)
1070 size = 8;
1071
8fa66bdc
ACM
1072 if (head + event->header.size >= page_size * mmap_window) {
1073 unsigned long shift = page_size * (head / page_size);
1074 int ret;
1075
1076 ret = munmap(buf, page_size * mmap_window);
1077 assert(ret == 0);
1078
1079 offset += shift;
1080 head -= shift;
1081 goto remap;
1082 }
1083
6142f9ec
PZ
1084 size = event->header.size;
1085 if (!size)
1086 goto broken_event;
8fa66bdc 1087
8fa66bdc
ACM
1088 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) {
1089 char level;
1090 int show = 0;
1091 struct dso *dso = NULL;
1092 struct thread *thread = threads__findnew(event->ip.pid);
f17e04af 1093 uint64_t ip = event->ip.ip;
e7fb08b1 1094 struct map *map = NULL;
8fa66bdc 1095
97b07b69 1096 if (dump_trace) {
f49515b1
IM
1097 fprintf(stderr, "%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
1098 (void *)(offset + head),
1099 (void *)(long)(event->header.size),
97b07b69
IM
1100 event->header.misc,
1101 event->ip.pid,
16f762a2 1102 (void *)(long)ip);
97b07b69
IM
1103 }
1104
ce7e4365 1105 if (thread == NULL) {
55717314 1106 fprintf(stderr, "problem processing %d event, skipping it.\n",
ce7e4365 1107 event->header.type);
55717314 1108 goto broken_event;
ce7e4365 1109 }
8fa66bdc
ACM
1110
1111 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
1112 show = SHOW_KERNEL;
1113 level = 'k';
e7fb08b1 1114
8fa66bdc 1115 dso = kernel_dso;
e7fb08b1 1116
8fa66bdc 1117 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
16f762a2 1118
8fa66bdc
ACM
1119 show = SHOW_USER;
1120 level = '.';
16f762a2
IM
1121
1122 map = thread__find_map(thread, ip);
f17e04af 1123 if (map != NULL) {
8fa66bdc 1124 dso = map->dso;
f17e04af
PZ
1125 ip -= map->start + map->pgoff;
1126 }
e7fb08b1 1127
8fa66bdc
ACM
1128 } else {
1129 show = SHOW_HV;
1130 level = 'H';
1131 }
1132
1133 if (show & show_mask) {
f17e04af 1134 struct symbol *sym = dso__find_symbol(dso, ip);
8fa66bdc 1135
e7fb08b1
PZ
1136 if (hist_entry__add(thread, map, dso, sym, ip, level)) {
1137 fprintf(stderr,
55717314
IM
1138 "problem incrementing symbol count, skipping event\n");
1139 goto broken_event;
ce7e4365 1140 }
8fa66bdc
ACM
1141 }
1142 total++;
1143 } else switch (event->header.type) {
1144 case PERF_EVENT_MMAP: {
1145 struct thread *thread = threads__findnew(event->mmap.pid);
1146 struct map *map = map__new(&event->mmap);
1147
97b07b69 1148 if (dump_trace) {
f49515b1
IM
1149 fprintf(stderr, "%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n",
1150 (void *)(offset + head),
1151 (void *)(long)(event->header.size),
16f762a2
IM
1152 (void *)(long)event->mmap.start,
1153 (void *)(long)event->mmap.len,
1154 (void *)(long)event->mmap.pgoff,
97b07b69
IM
1155 event->mmap.filename);
1156 }
ce7e4365 1157 if (thread == NULL || map == NULL) {
55717314
IM
1158 fprintf(stderr, "problem processing PERF_EVENT_MMAP, skipping event.\n");
1159 goto broken_event;
ce7e4365 1160 }
8fa66bdc 1161 thread__insert_map(thread, map);
97b07b69 1162 total_mmap++;
8fa66bdc
ACM
1163 break;
1164 }
1165 case PERF_EVENT_COMM: {
1166 struct thread *thread = threads__findnew(event->comm.pid);
1167
97b07b69 1168 if (dump_trace) {
f49515b1
IM
1169 fprintf(stderr, "%p [%p]: PERF_EVENT_COMM: %s:%d\n",
1170 (void *)(offset + head),
1171 (void *)(long)(event->header.size),
97b07b69
IM
1172 event->comm.comm, event->comm.pid);
1173 }
8fa66bdc 1174 if (thread == NULL ||
ce7e4365 1175 thread__set_comm(thread, event->comm.comm)) {
55717314
IM
1176 fprintf(stderr, "problem processing PERF_EVENT_COMM, skipping event.\n");
1177 goto broken_event;
ce7e4365 1178 }
97b07b69 1179 total_comm++;
8fa66bdc
ACM
1180 break;
1181 }
97b07b69 1182 default: {
6142f9ec 1183broken_event:
b7a16eac
PZ
1184 if (dump_trace)
1185 fprintf(stderr, "%p [%p]: skipping unknown header type: %d\n",
1186 (void *)(offset + head),
1187 (void *)(long)(event->header.size),
1188 event->header.type);
1189
3e706114 1190 total_unknown++;
6142f9ec
PZ
1191
1192 /*
1193 * assume we lost track of the stream, check alignment, and
1194 * increment a single u64 in the hope to catch on again 'soon'.
1195 */
1196
1197 if (unlikely(head & 7))
1198 head &= ~7ULL;
1199
1200 size = 8;
97b07b69 1201 }
8fa66bdc
ACM
1202 }
1203
6142f9ec 1204 head += size;
f49515b1 1205
8fa66bdc
ACM
1206 if (offset + head < stat.st_size)
1207 goto more;
1208
1209 rc = EXIT_SUCCESS;
8fa66bdc 1210 close(input);
97b07b69
IM
1211
1212 if (dump_trace) {
3e706114
IM
1213 fprintf(stderr, " IP events: %10ld\n", total);
1214 fprintf(stderr, " mmap events: %10ld\n", total_mmap);
1215 fprintf(stderr, " comm events: %10ld\n", total_comm);
1216 fprintf(stderr, " unknown events: %10ld\n", total_unknown);
97b07b69
IM
1217
1218 return 0;
1219 }
1220
e7fb08b1 1221 if (verbose >= 2)
16f762a2 1222 dsos__fprintf(stdout);
16f762a2 1223
e7fb08b1
PZ
1224 output__resort();
1225 output__fprintf(stdout, total);
8fa66bdc 1226
8fa66bdc
ACM
1227 return rc;
1228}
1229
53cb8bc2
IM
1230static const char * const report_usage[] = {
1231 "perf report [<options>] <command>",
1232 NULL
1233};
1234
1235static const struct option options[] = {
1236 OPT_STRING('i', "input", &input_name, "file",
1237 "input file name"),
815e777f
ACM
1238 OPT_BOOLEAN('v', "verbose", &verbose,
1239 "be more verbose (show symbol address, etc)"),
97b07b69
IM
1240 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1241 "dump raw trace in ASCII"),
450aaa2b 1242 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
37f440cb 1243 OPT_STRING('s', "sort", &sort_order, "foo", "bar"),
53cb8bc2
IM
1244 OPT_END()
1245};
1246
1247int cmd_report(int argc, const char **argv, const char *prefix)
1248{
1249 elf_version(EV_CURRENT);
1250
1251 page_size = getpagesize();
1252
1253 parse_options(argc, argv, options, report_usage, 0);
1254
1aa16738
PZ
1255 setup_sorting();
1256
a930d2c0
IM
1257 setup_pager();
1258
53cb8bc2
IM
1259 return __cmd_report();
1260}
This page took 0.084282 seconds and 5 git commands to generate.