blob: f63057fe2cd276faa4aaca7bbbbc8077bb618d0c [file] [log] [blame]
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001#define _GNU_SOURCE
2#include <sys/types.h>
3#include <sys/stat.h>
4#include <sys/time.h>
5#include <unistd.h>
6#include <stdint.h>
7#include <stdbool.h>
8#include <stdlib.h>
9#include <string.h>
10#include <limits.h>
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -030011#include <gelf.h>
12#include <elf.h>
13#include <libelf.h>
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030014#include <fcntl.h>
15#include <stdio.h>
16#include <errno.h>
17#include <ctype.h>
18#include <time.h>
19#include <getopt.h>
20#include <assert.h>
21#include <search.h>
22
23#include <sys/ioctl.h>
24#include <sys/poll.h>
25#include <sys/prctl.h>
26#include <sys/wait.h>
27#include <sys/mman.h>
28#include <sys/types.h>
29#include <sys/stat.h>
30
31#include <linux/unistd.h>
32#include <linux/types.h>
33
34#include "../../include/linux/perf_counter.h"
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -030035#include "util/list.h"
36#include "util/rbtree.h"
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030037
38#define SHOW_KERNEL 1
39#define SHOW_USER 2
40#define SHOW_HV 4
41
42static char const *input_name = "output.perf";
43static int input;
44static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
45
46static unsigned long page_size;
47static unsigned long mmap_window = 32;
48
49static const char *perf_event_names[] = {
50 [PERF_EVENT_MMAP] = " PERF_EVENT_MMAP",
51 [PERF_EVENT_MUNMAP] = " PERF_EVENT_MUNMAP",
52 [PERF_EVENT_COMM] = " PERF_EVENT_COMM",
53};
54
55struct ip_event {
56 struct perf_event_header header;
57 __u64 ip;
58 __u32 pid, tid;
59};
60struct mmap_event {
61 struct perf_event_header header;
62 __u32 pid, tid;
63 __u64 start;
64 __u64 len;
65 __u64 pgoff;
66 char filename[PATH_MAX];
67};
68struct comm_event {
69 struct perf_event_header header;
70 __u32 pid,tid;
71 char comm[16];
72};
73
74typedef union event_union {
75 struct perf_event_header header;
76 struct ip_event ip;
77 struct mmap_event mmap;
78 struct comm_event comm;
79} event_t;
80
81struct section {
82 struct list_head node;
83 uint64_t start;
84 uint64_t end;
85 uint64_t offset;
86 char name[0];
87};
88
89static struct section *section__new(uint64_t start, uint64_t size,
90 uint64_t offset, char *name)
91{
92 struct section *self = malloc(sizeof(*self) + strlen(name) + 1);
93
94 if (self != NULL) {
95 self->start = start;
96 self->end = start + size;
97 self->offset = offset;
98 strcpy(self->name, name);
99 }
100
101 return self;
102}
103
104static void section__delete(struct section *self)
105{
106 free(self);
107}
108
109struct symbol {
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300110 struct rb_node rb_node;
111 uint64_t start;
112 uint64_t end;
113 char name[0];
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300114};
115
116static struct symbol *symbol__new(uint64_t start, uint64_t len, const char *name)
117{
118 struct symbol *self = malloc(sizeof(*self) + strlen(name) + 1);
119
120 if (self != NULL) {
121 self->start = start;
122 self->end = start + len;
123 strcpy(self->name, name);
124 }
125
126 return self;
127}
128
129static void symbol__delete(struct symbol *self)
130{
131 free(self);
132}
133
134static size_t symbol__fprintf(struct symbol *self, FILE *fp)
135{
136 return fprintf(fp, " %lx-%lx %s\n",
137 self->start, self->end, self->name);
138}
139
140struct dso {
141 struct list_head node;
142 struct list_head sections;
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300143 struct rb_root syms;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300144 char name[0];
145};
146
147static struct dso *dso__new(const char *name)
148{
149 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
150
151 if (self != NULL) {
152 strcpy(self->name, name);
153 INIT_LIST_HEAD(&self->sections);
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300154 self->syms = RB_ROOT;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300155 }
156
157 return self;
158}
159
160static void dso__delete_sections(struct dso *self)
161{
162 struct section *pos, *n;
163
164 list_for_each_entry_safe(pos, n, &self->sections, node)
165 section__delete(pos);
166}
167
168static void dso__delete_symbols(struct dso *self)
169{
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300170 struct symbol *pos;
171 struct rb_node *next = rb_first(&self->syms);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300172
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300173 while (next) {
174 pos = rb_entry(next, struct symbol, rb_node);
175 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300176 symbol__delete(pos);
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300177 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300178}
179
180static void dso__delete(struct dso *self)
181{
182 dso__delete_sections(self);
183 dso__delete_symbols(self);
184 free(self);
185}
186
187static void dso__insert_symbol(struct dso *self, struct symbol *sym)
188{
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300189 struct rb_node **p = &self->syms.rb_node;
190 struct rb_node *parent = NULL;
191 const uint64_t ip = sym->start;
192 struct symbol *s;
193
194 while (*p != NULL) {
195 parent = *p;
196 s = rb_entry(parent, struct symbol, rb_node);
197 if (ip < s->start)
198 p = &(*p)->rb_left;
199 else
200 p = &(*p)->rb_right;
201 }
202 rb_link_node(&sym->rb_node, parent, p);
203 rb_insert_color(&sym->rb_node, &self->syms);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300204}
205
206static struct symbol *dso__find_symbol(struct dso *self, uint64_t ip)
207{
208 if (self == NULL)
209 return NULL;
210
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300211 struct rb_node *n = self->syms.rb_node;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300212
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300213 while (n) {
214 struct symbol *s = rb_entry(n, struct symbol, rb_node);
215
216 if (ip < s->start)
217 n = n->rb_left;
218 else if (ip > s->end)
219 n = n->rb_right;
220 else
221 return s;
222 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300223
224 return NULL;
225}
226
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300227/**
228 * elf_symtab__for_each_symbol - iterate thru all the symbols
229 *
230 * @self: struct elf_symtab instance to iterate
231 * @index: uint32_t index
232 * @sym: GElf_Sym iterator
233 */
234#define elf_symtab__for_each_symbol(syms, nr_syms, index, sym) \
235 for (index = 0, gelf_getsym(syms, index, &sym);\
236 index < nr_syms; \
237 index++, gelf_getsym(syms, index, &sym))
238
239static inline uint8_t elf_sym__type(const GElf_Sym *sym)
240{
241 return GELF_ST_TYPE(sym->st_info);
242}
243
244static inline bool elf_sym__is_function(const GElf_Sym *sym)
245{
246 return elf_sym__type(sym) == STT_FUNC &&
247 sym->st_name != 0 &&
248 sym->st_shndx != SHN_UNDEF;
249}
250
251static inline const char *elf_sym__name(const GElf_Sym *sym,
252 const Elf_Data *symstrs)
253{
254 return symstrs->d_buf + sym->st_name;
255}
256
257static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
258 GElf_Shdr *shp, const char *name,
259 size_t *index)
260{
261 Elf_Scn *sec = NULL;
262 size_t cnt = 1;
263
264 while ((sec = elf_nextscn(elf, sec)) != NULL) {
265 char *str;
266
267 gelf_getshdr(sec, shp);
268 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
269 if (!strcmp(name, str)) {
270 if (index)
271 *index = cnt;
272 break;
273 }
274 ++cnt;
275 }
276
277 return sec;
278}
279
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300280static int dso__load(struct dso *self)
281{
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300282 int fd = open(self->name, O_RDONLY), err = -1;
283
284 if (fd == -1)
285 return -1;
286
287 Elf *elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
288 if (elf == NULL) {
289 fprintf(stderr, "%s: cannot read %s ELF file.\n",
290 __func__, self->name);
291 goto out_close;
292 }
293
294 GElf_Ehdr ehdr;
295 if (gelf_getehdr(elf, &ehdr) == NULL) {
296 fprintf(stderr, "%s: cannot get elf header.\n", __func__);
297 goto out_elf_end;
298 }
299
300 GElf_Shdr shdr;
301 Elf_Scn *sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
302 if (sec == NULL)
303 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
304
305 if (sec == NULL)
306 goto out_elf_end;
307
308 if (gelf_getshdr(sec, &shdr) == NULL)
309 goto out_elf_end;
310
311 Elf_Data *syms = elf_getdata(sec, NULL);
312 if (syms == NULL)
313 goto out_elf_end;
314
315 sec = elf_getscn(elf, shdr.sh_link);
316 if (sec == NULL)
317 goto out_elf_end;
318
319 Elf_Data *symstrs = elf_getdata(sec, NULL);
320 if (symstrs == NULL)
321 goto out_elf_end;
322
323 const uint32_t nr_syms = shdr.sh_size / shdr.sh_entsize;
324
325 GElf_Sym sym;
326 uint32_t index;
327 elf_symtab__for_each_symbol(syms, nr_syms, index, sym) {
328 if (!elf_sym__is_function(&sym))
329 continue;
330 struct symbol *f = symbol__new(sym.st_value, sym.st_size,
331 elf_sym__name(&sym, symstrs));
332 if (f == NULL)
333 goto out_elf_end;
334
335 dso__insert_symbol(self, f);
336 }
337
338 err = 0;
339out_elf_end:
340 elf_end(elf);
341out_close:
342 close(fd);
343 return err;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300344}
345
346static size_t dso__fprintf(struct dso *self, FILE *fp)
347{
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300348 size_t ret = fprintf(fp, "dso: %s\n", self->name);
349
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300350 struct rb_node *nd;
351 for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
352 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300353 ret += symbol__fprintf(pos, fp);
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300354 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300355
356 return ret;
357}
358
359static LIST_HEAD(dsos);
360static struct dso *kernel_dso;
361
362static void dsos__add(struct dso *dso)
363{
364 list_add_tail(&dso->node, &dsos);
365}
366
367static struct dso *dsos__find(const char *name)
368{
369 struct dso *pos;
370
371 list_for_each_entry(pos, &dsos, node)
372 if (strcmp(pos->name, name) == 0)
373 return pos;
374 return NULL;
375}
376
377static struct dso *dsos__findnew(const char *name)
378{
379 struct dso *dso = dsos__find(name);
380
381 if (dso == NULL) {
382 dso = dso__new(name);
383 if (dso != NULL && dso__load(dso) < 0)
384 goto out_delete_dso;
385
386 dsos__add(dso);
387 }
388
389 return dso;
390
391out_delete_dso:
392 dso__delete(dso);
393 return NULL;
394}
395
396static void dsos__fprintf(FILE *fp)
397{
398 struct dso *pos;
399
400 list_for_each_entry(pos, &dsos, node)
401 dso__fprintf(pos, fp);
402}
403
404static int load_kallsyms(void)
405{
406 kernel_dso = dso__new("[kernel]");
407 if (kernel_dso == NULL)
408 return -1;
409
410 FILE *file = fopen("/proc/kallsyms", "r");
411
412 if (file == NULL)
413 goto out_delete_dso;
414
415 char *line = NULL;
416 size_t n;
417
418 while (!feof(file)) {
419 unsigned long long start;
420 char c, symbf[4096];
421
422 if (getline(&line, &n, file) < 0)
423 break;
424
425 if (!line)
426 goto out_delete_dso;
427
428 if (sscanf(line, "%llx %c %s", &start, &c, symbf) == 3) {
429 struct symbol *sym = symbol__new(start, 0x1000000, symbf);
430
431 if (sym == NULL)
432 goto out_delete_dso;
433
434 dso__insert_symbol(kernel_dso, sym);
435 }
436 }
437
438 dsos__add(kernel_dso);
439 free(line);
440 fclose(file);
441 return 0;
442
443out_delete_dso:
444 dso__delete(kernel_dso);
445 return -1;
446}
447
448struct map {
449 struct list_head node;
450 uint64_t start;
451 uint64_t end;
452 uint64_t pgoff;
453 struct dso *dso;
454};
455
456static struct map *map__new(struct mmap_event *event)
457{
458 struct map *self = malloc(sizeof(*self));
459
460 if (self != NULL) {
461 self->start = event->start;
462 self->end = event->start + event->len;
463 self->pgoff = event->pgoff;
464
465 self->dso = dsos__findnew(event->filename);
466 if (self->dso == NULL)
467 goto out_delete;
468 }
469 return self;
470out_delete:
471 free(self);
472 return NULL;
473}
474
475static size_t map__fprintf(struct map *self, FILE *fp)
476{
477 return fprintf(fp, " %lx-%lx %lx %s\n",
478 self->start, self->end, self->pgoff, self->dso->name);
479}
480
481struct symhist {
482 struct list_head node;
483 struct dso *dso;
484 struct symbol *sym;
485 uint32_t count;
486 char level;
487};
488
489static struct symhist *symhist__new(struct symbol *sym, struct dso *dso,
490 char level)
491{
492 struct symhist *self = malloc(sizeof(*self));
493
494 if (self != NULL) {
495 self->sym = sym;
496 self->dso = dso;
497 self->level = level;
498 self->count = 0;
499 }
500
501 return self;
502}
503
504static void symhist__delete(struct symhist *self)
505{
506 free(self);
507}
508
509static bool symhist__equal(struct symhist *self, struct symbol *sym,
510 struct dso *dso, char level)
511{
512 return self->level == level && self->sym == sym && self->dso == dso;
513}
514
515static void symhist__inc(struct symhist *self)
516{
517 ++self->count;
518}
519
520static size_t symhist__fprintf(struct symhist *self, FILE *fp)
521{
522 size_t ret = fprintf(fp, "[%c] ", self->level);
523
524 if (self->level != '.')
525 ret += fprintf(fp, "%s", self->sym->name);
526 else
527 ret += fprintf(fp, "%s: %s",
528 self->dso ? self->dso->name : "<unknown",
529 self->sym ? self->sym->name : "<unknown>");
530 return ret + fprintf(fp, ": %u\n", self->count);
531}
532
533struct thread {
534 struct list_head node;
535 struct list_head maps;
536 struct list_head symhists;
537 pid_t pid;
538 char *comm;
539};
540
541static struct thread *thread__new(pid_t pid)
542{
543 struct thread *self = malloc(sizeof(*self));
544
545 if (self != NULL) {
546 self->pid = pid;
547 self->comm = NULL;
548 INIT_LIST_HEAD(&self->maps);
549 INIT_LIST_HEAD(&self->symhists);
550 }
551
552 return self;
553}
554
555static void thread__insert_symhist(struct thread *self,
556 struct symhist *symhist)
557{
558 list_add_tail(&symhist->node, &self->symhists);
559}
560
561static struct symhist *thread__symhists_find(struct thread *self,
562 struct symbol *sym,
563 struct dso *dso, char level)
564{
565 struct symhist *pos;
566
567 list_for_each_entry(pos, &self->symhists, node)
568 if (symhist__equal(pos, sym, dso, level))
569 return pos;
570
571 return NULL;
572}
573
574static int thread__symbol_incnew(struct thread *self, struct symbol *sym,
575 struct dso *dso, char level)
576{
577 struct symhist *symhist = thread__symhists_find(self, sym, dso, level);
578
579 if (symhist == NULL) {
580 symhist = symhist__new(sym, dso, level);
581 if (symhist == NULL)
582 goto out_error;
583 thread__insert_symhist(self, symhist);
584 }
585
586 symhist__inc(symhist);
587 return 0;
588out_error:
589 return -ENOMEM;
590}
591
592static int thread__set_comm(struct thread *self, const char *comm)
593{
594 self->comm = strdup(comm);
595 return self->comm ? 0 : -ENOMEM;
596}
597
598static size_t thread__maps_fprintf(struct thread *self, FILE *fp)
599{
600 struct map *pos;
601 size_t ret = 0;
602
603 list_for_each_entry(pos, &self->maps, node)
604 ret += map__fprintf(pos, fp);
605
606 return ret;
607}
608
609static size_t thread__fprintf(struct thread *self, FILE *fp)
610{
611 struct symhist *pos;
612 int ret = fprintf(fp, "thread: %d %s\n", self->pid, self->comm);
613
614 list_for_each_entry(pos, &self->symhists, node)
615 ret += symhist__fprintf(pos, fp);
616
617 return ret;
618}
619
620static LIST_HEAD(threads);
621
622static void threads__add(struct thread *thread)
623{
624 list_add_tail(&thread->node, &threads);
625}
626
627static struct thread *threads__find(pid_t pid)
628{
629 struct thread *pos;
630
631 list_for_each_entry(pos, &threads, node)
632 if (pos->pid == pid)
633 return pos;
634 return NULL;
635}
636
637static struct thread *threads__findnew(pid_t pid)
638{
639 struct thread *thread = threads__find(pid);
640
641 if (thread == NULL) {
642 thread = thread__new(pid);
643 if (thread != NULL)
644 threads__add(thread);
645 }
646
647 return thread;
648}
649
650static void thread__insert_map(struct thread *self, struct map *map)
651{
652 list_add_tail(&map->node, &self->maps);
653}
654
655static struct map *thread__find_map(struct thread *self, uint64_t ip)
656{
657 if (self == NULL)
658 return NULL;
659
660 struct map *pos;
661
662 list_for_each_entry(pos, &self->maps, node)
663 if (ip >= pos->start && ip <= pos->end)
664 return pos;
665
666 return NULL;
667}
668
669static void threads__fprintf(FILE *fp)
670{
671 struct thread *pos;
672
673 list_for_each_entry(pos, &threads, node)
674 thread__fprintf(pos, fp);
675}
676
677#if 0
678static std::string resolve_user_symbol(int pid, uint64_t ip)
679{
680 std::string sym = "<unknown>";
681
682 maps_t &m = maps[pid];
683 maps_t::const_iterator mi = m.upper_bound(map(ip));
684 if (mi == m.end())
685 return sym;
686
687 ip -= mi->start + mi->pgoff;
688
689 symbols_t &s = dsos[mi->dso].syms;
690 symbols_t::const_iterator si = s.upper_bound(symbol(ip));
691
692 sym = mi->dso + ": <unknown>";
693
694 if (si == s.begin())
695 return sym;
696 si--;
697
698 if (si->start <= ip && ip < si->end)
699 sym = mi->dso + ": " + si->name;
700#if 0
701 else if (si->start <= ip)
702 sym = mi->dso + ": ?" + si->name;
703#endif
704
705 return sym;
706}
707#endif
708
709static void display_help(void)
710{
711 printf(
712 "Usage: perf-report [<options>]\n"
713 " -i file --input=<file> # input file\n"
714 );
715
716 exit(0);
717}
718
719static void process_options(int argc, char *argv[])
720{
721 int error = 0;
722
723 for (;;) {
724 int option_index = 0;
725 /** Options for getopt */
726 static struct option long_options[] = {
727 {"input", required_argument, NULL, 'i'},
728 {"no-user", no_argument, NULL, 'u'},
729 {"no-kernel", no_argument, NULL, 'k'},
730 {"no-hv", no_argument, NULL, 'h'},
731 {NULL, 0, NULL, 0 }
732 };
733 int c = getopt_long(argc, argv, "+:i:kuh",
734 long_options, &option_index);
735 if (c == -1)
736 break;
737
738 switch (c) {
739 case 'i': input_name = strdup(optarg); break;
740 case 'k': show_mask &= ~SHOW_KERNEL; break;
741 case 'u': show_mask &= ~SHOW_USER; break;
742 case 'h': show_mask &= ~SHOW_HV; break;
743 default: error = 1; break;
744 }
745 }
746
747 if (error)
748 display_help();
749}
750
751int cmd_report(int argc, char **argv)
752{
753 unsigned long offset = 0;
754 unsigned long head = 0;
755 struct stat stat;
756 char *buf;
757 event_t *event;
758 int ret, rc = EXIT_FAILURE;
759 unsigned long total = 0;
760
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300761 elf_version(EV_CURRENT);
762
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300763 page_size = getpagesize();
764
765 process_options(argc, argv);
766
767 input = open(input_name, O_RDONLY);
768 if (input < 0) {
769 perror("failed to open file");
770 exit(-1);
771 }
772
773 ret = fstat(input, &stat);
774 if (ret < 0) {
775 perror("failed to stat file");
776 exit(-1);
777 }
778
779 if (!stat.st_size) {
780 fprintf(stderr, "zero-sized file, nothing to do!\n");
781 exit(0);
782 }
783
784 if (load_kallsyms() < 0) {
785 perror("failed to open kallsyms");
786 return EXIT_FAILURE;
787 }
788
789remap:
790 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
791 MAP_SHARED, input, offset);
792 if (buf == MAP_FAILED) {
793 perror("failed to mmap file");
794 exit(-1);
795 }
796
797more:
798 event = (event_t *)(buf + head);
799
800 if (head + event->header.size >= page_size * mmap_window) {
801 unsigned long shift = page_size * (head / page_size);
802 int ret;
803
804 ret = munmap(buf, page_size * mmap_window);
805 assert(ret == 0);
806
807 offset += shift;
808 head -= shift;
809 goto remap;
810 }
811
812
813 if (!event->header.size) {
814 fprintf(stderr, "zero-sized event at file offset %ld\n", offset + head);
815 fprintf(stderr, "skipping %ld bytes of events.\n", stat.st_size - offset - head);
816 goto done;
817 }
818
819 head += event->header.size;
820
821 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) {
822 char level;
823 int show = 0;
824 struct dso *dso = NULL;
825 struct thread *thread = threads__findnew(event->ip.pid);
826
827 if (thread == NULL)
828 goto done;
829
830 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
831 show = SHOW_KERNEL;
832 level = 'k';
833 dso = kernel_dso;
834 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
835 show = SHOW_USER;
836 level = '.';
837 struct map *map = thread__find_map(thread, event->ip.ip);
838 if (map != NULL)
839 dso = map->dso;
840 } else {
841 show = SHOW_HV;
842 level = 'H';
843 }
844
845 if (show & show_mask) {
846 struct symbol *sym = dso__find_symbol(dso, event->ip.ip);
847
848 if (thread__symbol_incnew(thread, sym, dso, level))
849 goto done;
850 }
851 total++;
852 } else switch (event->header.type) {
853 case PERF_EVENT_MMAP: {
854 struct thread *thread = threads__findnew(event->mmap.pid);
855 struct map *map = map__new(&event->mmap);
856
857 if (thread == NULL || map == NULL )
858 goto done;
859 thread__insert_map(thread, map);
860 break;
861 }
862 case PERF_EVENT_COMM: {
863 struct thread *thread = threads__findnew(event->comm.pid);
864
865 if (thread == NULL ||
866 thread__set_comm(thread, event->comm.comm))
867 goto done;
868 break;
869 }
870 }
871
872 if (offset + head < stat.st_size)
873 goto more;
874
875 rc = EXIT_SUCCESS;
876done:
877 close(input);
878 //dsos__fprintf(stdout);
879 threads__fprintf(stdout);
880#if 0
881 std::map<std::string, int>::iterator hi = hist.begin();
882
883 while (hi != hist.end()) {
884 rev_hist.insert(std::pair<int, std::string>(hi->second, hi->first));
885 hist.erase(hi++);
886 }
887
888 std::multimap<int, std::string>::const_iterator ri = rev_hist.begin();
889
890 while (ri != rev_hist.end()) {
891 printf(" %5.2f %s\n", (100.0 * ri->first)/total, ri->second.c_str());
892 ri++;
893 }
894#endif
895 return rc;
896}
897