perf record: Convert to Git option parsing
[linux-2.6/verdex.git] / Documentation / perf_counter / builtin-report.c
blob9e59d6071ef3a117a98650f61dd331d9ff7033fa
1 #include "util/util.h"
3 #include <libelf.h>
4 #include <gelf.h>
5 #include <elf.h>
7 #include "util/list.h"
8 #include "util/rbtree.h"
10 #include "perf.h"
12 #include "util/parse-options.h"
13 #include "util/parse-events.h"
15 #define SHOW_KERNEL 1
16 #define SHOW_USER 2
17 #define SHOW_HV 4
19 static char const *input_name = "output.perf";
20 static int input;
21 static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
23 static unsigned long page_size;
24 static unsigned long mmap_window = 32;
26 const char *perf_event_names[] = {
27 [PERF_EVENT_MMAP] = " PERF_EVENT_MMAP",
28 [PERF_EVENT_MUNMAP] = " PERF_EVENT_MUNMAP",
29 [PERF_EVENT_COMM] = " PERF_EVENT_COMM",
32 struct ip_event {
33 struct perf_event_header header;
34 __u64 ip;
35 __u32 pid, tid;
37 struct mmap_event {
38 struct perf_event_header header;
39 __u32 pid, tid;
40 __u64 start;
41 __u64 len;
42 __u64 pgoff;
43 char filename[PATH_MAX];
45 struct comm_event {
46 struct perf_event_header header;
47 __u32 pid,tid;
48 char comm[16];
51 typedef union event_union {
52 struct perf_event_header header;
53 struct ip_event ip;
54 struct mmap_event mmap;
55 struct comm_event comm;
56 } event_t;
58 struct section {
59 struct list_head node;
60 uint64_t start;
61 uint64_t end;
62 uint64_t offset;
63 char name[0];
66 struct section *section__new(uint64_t start, uint64_t size,
67 uint64_t offset, char *name)
69 struct section *self = malloc(sizeof(*self) + strlen(name) + 1);
71 if (self != NULL) {
72 self->start = start;
73 self->end = start + size;
74 self->offset = offset;
75 strcpy(self->name, name);
78 return self;
81 static void section__delete(struct section *self)
83 free(self);
86 struct symbol {
87 struct rb_node rb_node;
88 uint64_t start;
89 uint64_t end;
90 char name[0];
93 static struct symbol *symbol__new(uint64_t start, uint64_t len, const char *name)
95 struct symbol *self = malloc(sizeof(*self) + strlen(name) + 1);
97 if (self != NULL) {
98 self->start = start;
99 self->end = start + len;
100 strcpy(self->name, name);
103 return self;
106 static void symbol__delete(struct symbol *self)
108 free(self);
111 static size_t symbol__fprintf(struct symbol *self, FILE *fp)
113 return fprintf(fp, " %lx-%lx %s\n",
114 self->start, self->end, self->name);
117 struct dso {
118 struct list_head node;
119 struct list_head sections;
120 struct rb_root syms;
121 char name[0];
124 static struct dso *dso__new(const char *name)
126 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
128 if (self != NULL) {
129 strcpy(self->name, name);
130 INIT_LIST_HEAD(&self->sections);
131 self->syms = RB_ROOT;
134 return self;
137 static void dso__delete_sections(struct dso *self)
139 struct section *pos, *n;
141 list_for_each_entry_safe(pos, n, &self->sections, node)
142 section__delete(pos);
145 static void dso__delete_symbols(struct dso *self)
147 struct symbol *pos;
148 struct rb_node *next = rb_first(&self->syms);
150 while (next) {
151 pos = rb_entry(next, struct symbol, rb_node);
152 next = rb_next(&pos->rb_node);
153 symbol__delete(pos);
157 static void dso__delete(struct dso *self)
159 dso__delete_sections(self);
160 dso__delete_symbols(self);
161 free(self);
164 static void dso__insert_symbol(struct dso *self, struct symbol *sym)
166 struct rb_node **p = &self->syms.rb_node;
167 struct rb_node *parent = NULL;
168 const uint64_t ip = sym->start;
169 struct symbol *s;
171 while (*p != NULL) {
172 parent = *p;
173 s = rb_entry(parent, struct symbol, rb_node);
174 if (ip < s->start)
175 p = &(*p)->rb_left;
176 else
177 p = &(*p)->rb_right;
179 rb_link_node(&sym->rb_node, parent, p);
180 rb_insert_color(&sym->rb_node, &self->syms);
183 static struct symbol *dso__find_symbol(struct dso *self, uint64_t ip)
185 if (self == NULL)
186 return NULL;
188 struct rb_node *n = self->syms.rb_node;
190 while (n) {
191 struct symbol *s = rb_entry(n, struct symbol, rb_node);
193 if (ip < s->start)
194 n = n->rb_left;
195 else if (ip > s->end)
196 n = n->rb_right;
197 else
198 return s;
201 return NULL;
205 * elf_symtab__for_each_symbol - iterate thru all the symbols
207 * @self: struct elf_symtab instance to iterate
208 * @index: uint32_t index
209 * @sym: GElf_Sym iterator
211 #define elf_symtab__for_each_symbol(syms, nr_syms, index, sym) \
212 for (index = 0, gelf_getsym(syms, index, &sym);\
213 index < nr_syms; \
214 index++, gelf_getsym(syms, index, &sym))
216 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
218 return GELF_ST_TYPE(sym->st_info);
221 static inline int elf_sym__is_function(const GElf_Sym *sym)
223 return elf_sym__type(sym) == STT_FUNC &&
224 sym->st_name != 0 &&
225 sym->st_shndx != SHN_UNDEF;
228 static inline const char *elf_sym__name(const GElf_Sym *sym,
229 const Elf_Data *symstrs)
231 return symstrs->d_buf + sym->st_name;
234 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
235 GElf_Shdr *shp, const char *name,
236 size_t *index)
238 Elf_Scn *sec = NULL;
239 size_t cnt = 1;
241 while ((sec = elf_nextscn(elf, sec)) != NULL) {
242 char *str;
244 gelf_getshdr(sec, shp);
245 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
246 if (!strcmp(name, str)) {
247 if (index)
248 *index = cnt;
249 break;
251 ++cnt;
254 return sec;
257 static int dso__load(struct dso *self)
259 int fd = open(self->name, O_RDONLY), err = -1;
261 if (fd == -1)
262 return -1;
264 Elf *elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
265 if (elf == NULL) {
266 fprintf(stderr, "%s: cannot read %s ELF file.\n",
267 __func__, self->name);
268 goto out_close;
271 GElf_Ehdr ehdr;
272 if (gelf_getehdr(elf, &ehdr) == NULL) {
273 fprintf(stderr, "%s: cannot get elf header.\n", __func__);
274 goto out_elf_end;
277 GElf_Shdr shdr;
278 Elf_Scn *sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
279 if (sec == NULL)
280 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
282 if (sec == NULL)
283 goto out_elf_end;
285 if (gelf_getshdr(sec, &shdr) == NULL)
286 goto out_elf_end;
288 Elf_Data *syms = elf_getdata(sec, NULL);
289 if (syms == NULL)
290 goto out_elf_end;
292 sec = elf_getscn(elf, shdr.sh_link);
293 if (sec == NULL)
294 goto out_elf_end;
296 Elf_Data *symstrs = elf_getdata(sec, NULL);
297 if (symstrs == NULL)
298 goto out_elf_end;
300 const uint32_t nr_syms = shdr.sh_size / shdr.sh_entsize;
302 GElf_Sym sym;
303 uint32_t index;
304 elf_symtab__for_each_symbol(syms, nr_syms, index, sym) {
305 if (!elf_sym__is_function(&sym))
306 continue;
307 struct symbol *f = symbol__new(sym.st_value, sym.st_size,
308 elf_sym__name(&sym, symstrs));
309 if (f == NULL)
310 goto out_elf_end;
312 dso__insert_symbol(self, f);
315 err = 0;
316 out_elf_end:
317 elf_end(elf);
318 out_close:
319 close(fd);
320 return err;
323 static size_t dso__fprintf(struct dso *self, FILE *fp)
325 size_t ret = fprintf(fp, "dso: %s\n", self->name);
327 struct rb_node *nd;
328 for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
329 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
330 ret += symbol__fprintf(pos, fp);
333 return ret;
336 static LIST_HEAD(dsos);
337 static struct dso *kernel_dso;
339 static void dsos__add(struct dso *dso)
341 list_add_tail(&dso->node, &dsos);
344 static struct dso *dsos__find(const char *name)
346 struct dso *pos;
348 list_for_each_entry(pos, &dsos, node)
349 if (strcmp(pos->name, name) == 0)
350 return pos;
351 return NULL;
354 static struct dso *dsos__findnew(const char *name)
356 struct dso *dso = dsos__find(name);
358 if (dso == NULL) {
359 dso = dso__new(name);
360 if (dso != NULL && dso__load(dso) < 0)
361 goto out_delete_dso;
363 dsos__add(dso);
366 return dso;
368 out_delete_dso:
369 dso__delete(dso);
370 return NULL;
373 void dsos__fprintf(FILE *fp)
375 struct dso *pos;
377 list_for_each_entry(pos, &dsos, node)
378 dso__fprintf(pos, fp);
381 static int load_kallsyms(void)
383 kernel_dso = dso__new("[kernel]");
384 if (kernel_dso == NULL)
385 return -1;
387 FILE *file = fopen("/proc/kallsyms", "r");
389 if (file == NULL)
390 goto out_delete_dso;
392 char *line = NULL;
393 size_t n;
395 while (!feof(file)) {
396 unsigned long long start;
397 char c, symbf[4096];
399 if (getline(&line, &n, file) < 0)
400 break;
402 if (!line)
403 goto out_delete_dso;
405 if (sscanf(line, "%llx %c %s", &start, &c, symbf) == 3) {
406 struct symbol *sym = symbol__new(start, 0x1000000, symbf);
408 if (sym == NULL)
409 goto out_delete_dso;
411 dso__insert_symbol(kernel_dso, sym);
415 dsos__add(kernel_dso);
416 free(line);
417 fclose(file);
418 return 0;
420 out_delete_dso:
421 dso__delete(kernel_dso);
422 return -1;
425 struct map {
426 struct list_head node;
427 uint64_t start;
428 uint64_t end;
429 uint64_t pgoff;
430 struct dso *dso;
433 static struct map *map__new(struct mmap_event *event)
435 struct map *self = malloc(sizeof(*self));
437 if (self != NULL) {
438 self->start = event->start;
439 self->end = event->start + event->len;
440 self->pgoff = event->pgoff;
442 self->dso = dsos__findnew(event->filename);
443 if (self->dso == NULL)
444 goto out_delete;
446 return self;
447 out_delete:
448 free(self);
449 return NULL;
452 static size_t map__fprintf(struct map *self, FILE *fp)
454 return fprintf(fp, " %lx-%lx %lx %s\n",
455 self->start, self->end, self->pgoff, self->dso->name);
458 struct symhist {
459 struct rb_node rb_node;
460 struct dso *dso;
461 struct symbol *sym;
462 uint64_t ip;
463 uint32_t count;
464 char level;
467 static struct symhist *symhist__new(struct symbol *sym, uint64_t ip,
468 struct dso *dso, char level)
470 struct symhist *self = malloc(sizeof(*self));
472 if (self != NULL) {
473 self->sym = sym;
474 self->ip = ip;
475 self->dso = dso;
476 self->level = level;
477 self->count = 1;
480 return self;
483 void symhist__delete(struct symhist *self)
485 free(self);
488 static void symhist__inc(struct symhist *self)
490 ++self->count;
493 static size_t symhist__fprintf(struct symhist *self, FILE *fp)
495 size_t ret = fprintf(fp, "%#llx [%c] ", (unsigned long long)self->ip, self->level);
497 if (self->level != '.')
498 ret += fprintf(fp, "%s", self->sym ? self->sym->name: "<unknown>");
499 else
500 ret += fprintf(fp, "%s: %s",
501 self->dso ? self->dso->name : "<unknown",
502 self->sym ? self->sym->name : "<unknown>");
503 return ret + fprintf(fp, ": %u\n", self->count);
506 struct thread {
507 struct rb_node rb_node;
508 struct list_head maps;
509 struct rb_root symhists;
510 pid_t pid;
511 char *comm;
514 static struct thread *thread__new(pid_t pid)
516 struct thread *self = malloc(sizeof(*self));
518 if (self != NULL) {
519 self->pid = pid;
520 self->comm = NULL;
521 INIT_LIST_HEAD(&self->maps);
522 self->symhists = RB_ROOT;
525 return self;
528 static int thread__symbol_incnew(struct thread *self, struct symbol *sym,
529 uint64_t ip, struct dso *dso, char level)
531 struct rb_node **p = &self->symhists.rb_node;
532 struct rb_node *parent = NULL;
533 struct symhist *sh;
535 while (*p != NULL) {
536 parent = *p;
537 sh = rb_entry(parent, struct symhist, rb_node);
539 if (sh->sym == sym || ip == sh->ip) {
540 symhist__inc(sh);
541 return 0;
544 /* Handle unresolved symbols too */
545 const uint64_t start = !sh->sym ? sh->ip : sh->sym->start;
547 if (ip < start)
548 p = &(*p)->rb_left;
549 else
550 p = &(*p)->rb_right;
553 sh = symhist__new(sym, ip, dso, level);
554 if (sh == NULL)
555 return -ENOMEM;
556 rb_link_node(&sh->rb_node, parent, p);
557 rb_insert_color(&sh->rb_node, &self->symhists);
558 return 0;
561 static int thread__set_comm(struct thread *self, const char *comm)
563 self->comm = strdup(comm);
564 return self->comm ? 0 : -ENOMEM;
567 size_t thread__maps_fprintf(struct thread *self, FILE *fp)
569 struct map *pos;
570 size_t ret = 0;
572 list_for_each_entry(pos, &self->maps, node)
573 ret += map__fprintf(pos, fp);
575 return ret;
578 static size_t thread__fprintf(struct thread *self, FILE *fp)
580 int ret = fprintf(fp, "thread: %d %s\n", self->pid, self->comm);
581 struct rb_node *nd;
583 for (nd = rb_first(&self->symhists); nd; nd = rb_next(nd)) {
584 struct symhist *pos = rb_entry(nd, struct symhist, rb_node);
585 ret += symhist__fprintf(pos, fp);
588 return ret;
591 static struct rb_root threads = RB_ROOT;
593 static struct thread *threads__findnew(pid_t pid)
595 struct rb_node **p = &threads.rb_node;
596 struct rb_node *parent = NULL;
597 struct thread *th;
599 while (*p != NULL) {
600 parent = *p;
601 th = rb_entry(parent, struct thread, rb_node);
603 if (th->pid == pid)
604 return th;
606 if (pid < th->pid)
607 p = &(*p)->rb_left;
608 else
609 p = &(*p)->rb_right;
612 th = thread__new(pid);
613 if (th != NULL) {
614 rb_link_node(&th->rb_node, parent, p);
615 rb_insert_color(&th->rb_node, &threads);
617 return th;
620 static void thread__insert_map(struct thread *self, struct map *map)
622 list_add_tail(&map->node, &self->maps);
625 static struct map *thread__find_map(struct thread *self, uint64_t ip)
627 if (self == NULL)
628 return NULL;
630 struct map *pos;
632 list_for_each_entry(pos, &self->maps, node)
633 if (ip >= pos->start && ip <= pos->end)
634 return pos;
636 return NULL;
639 static void threads__fprintf(FILE *fp)
641 struct rb_node *nd;
642 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
643 struct thread *pos = rb_entry(nd, struct thread, rb_node);
644 thread__fprintf(pos, fp);
648 static int __cmd_report(void)
650 unsigned long offset = 0;
651 unsigned long head = 0;
652 struct stat stat;
653 char *buf;
654 event_t *event;
655 int ret, rc = EXIT_FAILURE;
656 unsigned long total = 0;
658 input = open(input_name, O_RDONLY);
659 if (input < 0) {
660 perror("failed to open file");
661 exit(-1);
664 ret = fstat(input, &stat);
665 if (ret < 0) {
666 perror("failed to stat file");
667 exit(-1);
670 if (!stat.st_size) {
671 fprintf(stderr, "zero-sized file, nothing to do!\n");
672 exit(0);
675 if (load_kallsyms() < 0) {
676 perror("failed to open kallsyms");
677 return EXIT_FAILURE;
680 remap:
681 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
682 MAP_SHARED, input, offset);
683 if (buf == MAP_FAILED) {
684 perror("failed to mmap file");
685 exit(-1);
688 more:
689 event = (event_t *)(buf + head);
691 if (head + event->header.size >= page_size * mmap_window) {
692 unsigned long shift = page_size * (head / page_size);
693 int ret;
695 ret = munmap(buf, page_size * mmap_window);
696 assert(ret == 0);
698 offset += shift;
699 head -= shift;
700 goto remap;
704 if (!event->header.size) {
705 fprintf(stderr, "zero-sized event at file offset %ld\n", offset + head);
706 fprintf(stderr, "skipping %ld bytes of events.\n", stat.st_size - offset - head);
707 goto done;
710 head += event->header.size;
712 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) {
713 char level;
714 int show = 0;
715 struct dso *dso = NULL;
716 struct thread *thread = threads__findnew(event->ip.pid);
718 if (thread == NULL) {
719 fprintf(stderr, "problem processing %d event, bailing out\n",
720 event->header.type);
721 goto done;
724 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
725 show = SHOW_KERNEL;
726 level = 'k';
727 dso = kernel_dso;
728 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
729 show = SHOW_USER;
730 level = '.';
731 struct map *map = thread__find_map(thread, event->ip.ip);
732 if (map != NULL)
733 dso = map->dso;
734 } else {
735 show = SHOW_HV;
736 level = 'H';
739 if (show & show_mask) {
740 struct symbol *sym = dso__find_symbol(dso, event->ip.ip);
742 if (thread__symbol_incnew(thread, sym, event->ip.ip,
743 dso, level)) {
744 fprintf(stderr, "problem incrementing symbol count, bailing out\n");
745 goto done;
748 total++;
749 } else switch (event->header.type) {
750 case PERF_EVENT_MMAP: {
751 struct thread *thread = threads__findnew(event->mmap.pid);
752 struct map *map = map__new(&event->mmap);
754 if (thread == NULL || map == NULL) {
755 fprintf(stderr, "problem processing PERF_EVENT_MMAP, bailing out\n");
756 goto done;
758 thread__insert_map(thread, map);
759 break;
761 case PERF_EVENT_COMM: {
762 struct thread *thread = threads__findnew(event->comm.pid);
764 if (thread == NULL ||
765 thread__set_comm(thread, event->comm.comm)) {
766 fprintf(stderr, "problem processing PERF_EVENT_COMM, bailing out\n");
767 goto done;
769 break;
773 if (offset + head < stat.st_size)
774 goto more;
776 rc = EXIT_SUCCESS;
777 done:
778 close(input);
779 //dsos__fprintf(stdout);
780 threads__fprintf(stdout);
781 #if 0
782 std::map<std::string, int>::iterator hi = hist.begin();
784 while (hi != hist.end()) {
785 rev_hist.insert(std::pair<int, std::string>(hi->second, hi->first));
786 hist.erase(hi++);
789 std::multimap<int, std::string>::const_iterator ri = rev_hist.begin();
791 while (ri != rev_hist.end()) {
792 printf(" %5.2f %s\n", (100.0 * ri->first)/total, ri->second.c_str());
793 ri++;
795 #endif
796 return rc;
799 static const char * const report_usage[] = {
800 "perf report [<options>] <command>",
801 NULL
804 static const struct option options[] = {
805 OPT_STRING('i', "input", &input_name, "file",
806 "input file name"),
807 OPT_END()
810 int cmd_report(int argc, const char **argv, const char *prefix)
812 elf_version(EV_CURRENT);
814 page_size = getpagesize();
816 parse_options(argc, argv, options, report_usage, 0);
818 return __cmd_report();