blob: e17819001dd576dbcf5b2c4f1ddbaa21360b3de5 [file] [log] [blame]
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001#include "util/util.h"
2
3#include <libelf.h>
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -03004#include <gelf.h>
5#include <elf.h>
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03006
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -03007#include "util/list.h"
8#include "util/rbtree.h"
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03009
Ingo Molnar53cb8bc2009-05-26 09:17:18 +020010#include "perf.h"
11
12#include "util/parse-options.h"
13#include "util/parse-events.h"
14
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030015#define SHOW_KERNEL 1
16#define SHOW_USER 2
17#define SHOW_HV 4
18
Ingo Molnar53cb8bc2009-05-26 09:17:18 +020019static char const *input_name = "output.perf";
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030020static int input;
21static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
22
23static unsigned long page_size;
24static unsigned long mmap_window = 32;
25
Ingo Molnar53cb8bc2009-05-26 09:17:18 +020026const char *perf_event_names[] = {
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030027 [PERF_EVENT_MMAP] = " PERF_EVENT_MMAP",
28 [PERF_EVENT_MUNMAP] = " PERF_EVENT_MUNMAP",
29 [PERF_EVENT_COMM] = " PERF_EVENT_COMM",
30};
31
32struct ip_event {
33 struct perf_event_header header;
34 __u64 ip;
35 __u32 pid, tid;
36};
37struct 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];
44};
45struct comm_event {
46 struct perf_event_header header;
47 __u32 pid,tid;
48 char comm[16];
49};
50
51typedef 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;
57
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030058struct symbol {
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -030059 struct rb_node rb_node;
60 uint64_t start;
61 uint64_t end;
62 char name[0];
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030063};
64
65static struct symbol *symbol__new(uint64_t start, uint64_t len, const char *name)
66{
67 struct symbol *self = malloc(sizeof(*self) + strlen(name) + 1);
68
69 if (self != NULL) {
70 self->start = start;
71 self->end = start + len;
72 strcpy(self->name, name);
73 }
74
75 return self;
76}
77
78static void symbol__delete(struct symbol *self)
79{
80 free(self);
81}
82
83static size_t symbol__fprintf(struct symbol *self, FILE *fp)
84{
85 return fprintf(fp, " %lx-%lx %s\n",
86 self->start, self->end, self->name);
87}
88
89struct dso {
90 struct list_head node;
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -030091 struct rb_root syms;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030092 char name[0];
93};
94
95static struct dso *dso__new(const char *name)
96{
97 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
98
99 if (self != NULL) {
100 strcpy(self->name, name);
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300101 self->syms = RB_ROOT;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300102 }
103
104 return self;
105}
106
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300107static void dso__delete_symbols(struct dso *self)
108{
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300109 struct symbol *pos;
110 struct rb_node *next = rb_first(&self->syms);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300111
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300112 while (next) {
113 pos = rb_entry(next, struct symbol, rb_node);
114 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300115 symbol__delete(pos);
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300116 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300117}
118
119static void dso__delete(struct dso *self)
120{
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300121 dso__delete_symbols(self);
122 free(self);
123}
124
125static void dso__insert_symbol(struct dso *self, struct symbol *sym)
126{
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300127 struct rb_node **p = &self->syms.rb_node;
128 struct rb_node *parent = NULL;
129 const uint64_t ip = sym->start;
130 struct symbol *s;
131
132 while (*p != NULL) {
133 parent = *p;
134 s = rb_entry(parent, struct symbol, rb_node);
135 if (ip < s->start)
136 p = &(*p)->rb_left;
137 else
138 p = &(*p)->rb_right;
139 }
140 rb_link_node(&sym->rb_node, parent, p);
141 rb_insert_color(&sym->rb_node, &self->syms);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300142}
143
144static struct symbol *dso__find_symbol(struct dso *self, uint64_t ip)
145{
146 if (self == NULL)
147 return NULL;
148
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300149 struct rb_node *n = self->syms.rb_node;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300150
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300151 while (n) {
152 struct symbol *s = rb_entry(n, struct symbol, rb_node);
153
154 if (ip < s->start)
155 n = n->rb_left;
156 else if (ip > s->end)
157 n = n->rb_right;
158 else
159 return s;
160 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300161
162 return NULL;
163}
164
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300165/**
166 * elf_symtab__for_each_symbol - iterate thru all the symbols
167 *
168 * @self: struct elf_symtab instance to iterate
169 * @index: uint32_t index
170 * @sym: GElf_Sym iterator
171 */
172#define elf_symtab__for_each_symbol(syms, nr_syms, index, sym) \
173 for (index = 0, gelf_getsym(syms, index, &sym);\
174 index < nr_syms; \
175 index++, gelf_getsym(syms, index, &sym))
176
177static inline uint8_t elf_sym__type(const GElf_Sym *sym)
178{
179 return GELF_ST_TYPE(sym->st_info);
180}
181
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200182static inline int elf_sym__is_function(const GElf_Sym *sym)
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300183{
184 return elf_sym__type(sym) == STT_FUNC &&
185 sym->st_name != 0 &&
186 sym->st_shndx != SHN_UNDEF;
187}
188
189static inline const char *elf_sym__name(const GElf_Sym *sym,
190 const Elf_Data *symstrs)
191{
192 return symstrs->d_buf + sym->st_name;
193}
194
195static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
196 GElf_Shdr *shp, const char *name,
197 size_t *index)
198{
199 Elf_Scn *sec = NULL;
200 size_t cnt = 1;
201
202 while ((sec = elf_nextscn(elf, sec)) != NULL) {
203 char *str;
204
205 gelf_getshdr(sec, shp);
206 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
207 if (!strcmp(name, str)) {
208 if (index)
209 *index = cnt;
210 break;
211 }
212 ++cnt;
213 }
214
215 return sec;
216}
217
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300218static int dso__load(struct dso *self)
219{
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300220 int fd = open(self->name, O_RDONLY), err = -1;
221
222 if (fd == -1)
223 return -1;
224
225 Elf *elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
226 if (elf == NULL) {
227 fprintf(stderr, "%s: cannot read %s ELF file.\n",
228 __func__, self->name);
229 goto out_close;
230 }
231
232 GElf_Ehdr ehdr;
233 if (gelf_getehdr(elf, &ehdr) == NULL) {
234 fprintf(stderr, "%s: cannot get elf header.\n", __func__);
235 goto out_elf_end;
236 }
237
238 GElf_Shdr shdr;
239 Elf_Scn *sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
240 if (sec == NULL)
241 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
242
243 if (sec == NULL)
244 goto out_elf_end;
245
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300246 Elf_Data *syms = elf_getdata(sec, NULL);
247 if (syms == NULL)
248 goto out_elf_end;
249
250 sec = elf_getscn(elf, shdr.sh_link);
251 if (sec == NULL)
252 goto out_elf_end;
253
254 Elf_Data *symstrs = elf_getdata(sec, NULL);
255 if (symstrs == NULL)
256 goto out_elf_end;
257
258 const uint32_t nr_syms = shdr.sh_size / shdr.sh_entsize;
259
260 GElf_Sym sym;
261 uint32_t index;
262 elf_symtab__for_each_symbol(syms, nr_syms, index, sym) {
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200263 struct symbol *f;
264
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300265 if (!elf_sym__is_function(&sym))
266 continue;
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200267
268 sec = elf_getscn(elf, sym.st_shndx);
269 if (!sec)
270 goto out_elf_end;
271
272 gelf_getshdr(sec, &shdr);
273 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
274
275 f = symbol__new(sym.st_value, sym.st_size,
276 elf_sym__name(&sym, symstrs));
277 if (!f)
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300278 goto out_elf_end;
279
280 dso__insert_symbol(self, f);
281 }
282
283 err = 0;
284out_elf_end:
285 elf_end(elf);
286out_close:
287 close(fd);
288 return err;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300289}
290
291static size_t dso__fprintf(struct dso *self, FILE *fp)
292{
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300293 size_t ret = fprintf(fp, "dso: %s\n", self->name);
294
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300295 struct rb_node *nd;
296 for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
297 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300298 ret += symbol__fprintf(pos, fp);
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300299 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300300
301 return ret;
302}
303
304static LIST_HEAD(dsos);
305static struct dso *kernel_dso;
306
307static void dsos__add(struct dso *dso)
308{
309 list_add_tail(&dso->node, &dsos);
310}
311
312static struct dso *dsos__find(const char *name)
313{
314 struct dso *pos;
315
316 list_for_each_entry(pos, &dsos, node)
317 if (strcmp(pos->name, name) == 0)
318 return pos;
319 return NULL;
320}
321
322static struct dso *dsos__findnew(const char *name)
323{
324 struct dso *dso = dsos__find(name);
325
326 if (dso == NULL) {
327 dso = dso__new(name);
328 if (dso != NULL && dso__load(dso) < 0)
329 goto out_delete_dso;
330
331 dsos__add(dso);
332 }
333
334 return dso;
335
336out_delete_dso:
337 dso__delete(dso);
338 return NULL;
339}
340
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200341void dsos__fprintf(FILE *fp)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300342{
343 struct dso *pos;
344
345 list_for_each_entry(pos, &dsos, node)
346 dso__fprintf(pos, fp);
347}
348
349static int load_kallsyms(void)
350{
351 kernel_dso = dso__new("[kernel]");
352 if (kernel_dso == NULL)
353 return -1;
354
355 FILE *file = fopen("/proc/kallsyms", "r");
356
357 if (file == NULL)
358 goto out_delete_dso;
359
360 char *line = NULL;
361 size_t n;
362
363 while (!feof(file)) {
364 unsigned long long start;
Arnaldo Carvalho de Meloabd54f62009-05-26 12:21:34 -0300365 char c, symbf[4096];
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300366
367 if (getline(&line, &n, file) < 0)
368 break;
369
370 if (!line)
371 goto out_delete_dso;
372
373 if (sscanf(line, "%llx %c %s", &start, &c, symbf) == 3) {
Arnaldo Carvalho de Meloabd54f62009-05-26 12:21:34 -0300374 /*
375 * Well fix up the end later, when we have all sorted.
376 */
377 struct symbol *sym = symbol__new(start, 0xdead, symbf);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300378
Arnaldo Carvalho de Meloabd54f62009-05-26 12:21:34 -0300379 if (sym == NULL)
380 goto out_delete_dso;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300381
Arnaldo Carvalho de Meloabd54f62009-05-26 12:21:34 -0300382 dso__insert_symbol(kernel_dso, sym);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300383 }
384 }
385
Arnaldo Carvalho de Meloabd54f62009-05-26 12:21:34 -0300386 /*
387 * Now that we have all sorted out, just set the ->end of all
388 * symbols
389 */
390 struct rb_node *nd, *prevnd = rb_first(&kernel_dso->syms);
391
392 if (prevnd == NULL)
393 goto out_delete_line;
394
395 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
396 struct symbol *prev = rb_entry(prevnd, struct symbol, rb_node),
397 *curr = rb_entry(nd, struct symbol, rb_node);
398
399 prev->end = curr->start - 1;
400 prevnd = nd;
401 }
402
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300403 dsos__add(kernel_dso);
404 free(line);
405 fclose(file);
406 return 0;
407
Arnaldo Carvalho de Melo59d81022009-05-26 11:14:27 -0300408out_delete_line:
409 free(line);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300410out_delete_dso:
411 dso__delete(kernel_dso);
412 return -1;
413}
414
415struct map {
416 struct list_head node;
417 uint64_t start;
418 uint64_t end;
419 uint64_t pgoff;
420 struct dso *dso;
421};
422
423static struct map *map__new(struct mmap_event *event)
424{
425 struct map *self = malloc(sizeof(*self));
426
427 if (self != NULL) {
428 self->start = event->start;
429 self->end = event->start + event->len;
430 self->pgoff = event->pgoff;
431
432 self->dso = dsos__findnew(event->filename);
433 if (self->dso == NULL)
434 goto out_delete;
435 }
436 return self;
437out_delete:
438 free(self);
439 return NULL;
440}
441
442static size_t map__fprintf(struct map *self, FILE *fp)
443{
444 return fprintf(fp, " %lx-%lx %lx %s\n",
445 self->start, self->end, self->pgoff, self->dso->name);
446}
447
448struct symhist {
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300449 struct rb_node rb_node;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300450 struct dso *dso;
451 struct symbol *sym;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300452 uint64_t ip;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300453 uint32_t count;
454 char level;
455};
456
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300457static struct symhist *symhist__new(struct symbol *sym, uint64_t ip,
458 struct dso *dso, char level)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300459{
460 struct symhist *self = malloc(sizeof(*self));
461
462 if (self != NULL) {
463 self->sym = sym;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300464 self->ip = ip;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300465 self->dso = dso;
466 self->level = level;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300467 self->count = 1;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300468 }
469
470 return self;
471}
472
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200473void symhist__delete(struct symhist *self)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300474{
475 free(self);
476}
477
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300478static void symhist__inc(struct symhist *self)
479{
480 ++self->count;
481}
482
483static size_t symhist__fprintf(struct symhist *self, FILE *fp)
484{
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300485 size_t ret = fprintf(fp, "%#llx [%c] ", (unsigned long long)self->ip, self->level);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300486
487 if (self->level != '.')
Peter Zijlstraf3e08c52009-05-22 15:34:54 +0200488 ret += fprintf(fp, "%s", self->sym ? self->sym->name: "<unknown>");
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300489 else
490 ret += fprintf(fp, "%s: %s",
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200491 self->dso ? self->dso->name : "<unknown>",
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300492 self->sym ? self->sym->name : "<unknown>");
493 return ret + fprintf(fp, ": %u\n", self->count);
494}
495
496struct thread {
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300497 struct rb_node rb_node;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300498 struct list_head maps;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300499 struct rb_root symhists;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300500 pid_t pid;
501 char *comm;
502};
503
504static struct thread *thread__new(pid_t pid)
505{
506 struct thread *self = malloc(sizeof(*self));
507
508 if (self != NULL) {
509 self->pid = pid;
510 self->comm = NULL;
511 INIT_LIST_HEAD(&self->maps);
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300512 self->symhists = RB_ROOT;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300513 }
514
515 return self;
516}
517
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300518static int thread__symbol_incnew(struct thread *self, struct symbol *sym,
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300519 uint64_t ip, struct dso *dso, char level)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300520{
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300521 struct rb_node **p = &self->symhists.rb_node;
522 struct rb_node *parent = NULL;
523 struct symhist *sh;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300524
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300525 while (*p != NULL) {
526 parent = *p;
527 sh = rb_entry(parent, struct symhist, rb_node);
528
529 if (sh->sym == sym || ip == sh->ip) {
530 symhist__inc(sh);
531 return 0;
532 }
533
534 /* Handle unresolved symbols too */
535 const uint64_t start = !sh->sym ? sh->ip : sh->sym->start;
536
537 if (ip < start)
538 p = &(*p)->rb_left;
539 else
540 p = &(*p)->rb_right;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300541 }
542
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300543 sh = symhist__new(sym, ip, dso, level);
544 if (sh == NULL)
545 return -ENOMEM;
546 rb_link_node(&sh->rb_node, parent, p);
547 rb_insert_color(&sh->rb_node, &self->symhists);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300548 return 0;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300549}
550
551static int thread__set_comm(struct thread *self, const char *comm)
552{
553 self->comm = strdup(comm);
554 return self->comm ? 0 : -ENOMEM;
555}
556
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200557size_t thread__maps_fprintf(struct thread *self, FILE *fp)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300558{
559 struct map *pos;
560 size_t ret = 0;
561
562 list_for_each_entry(pos, &self->maps, node)
563 ret += map__fprintf(pos, fp);
564
565 return ret;
566}
567
568static size_t thread__fprintf(struct thread *self, FILE *fp)
569{
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300570 int ret = fprintf(fp, "thread: %d %s\n", self->pid, self->comm);
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300571 struct rb_node *nd;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300572
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300573 for (nd = rb_first(&self->symhists); nd; nd = rb_next(nd)) {
574 struct symhist *pos = rb_entry(nd, struct symhist, rb_node);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300575 ret += symhist__fprintf(pos, fp);
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300576 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300577
578 return ret;
579}
580
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300581static struct rb_root threads = RB_ROOT;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300582
583static struct thread *threads__findnew(pid_t pid)
584{
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300585 struct rb_node **p = &threads.rb_node;
586 struct rb_node *parent = NULL;
587 struct thread *th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300588
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300589 while (*p != NULL) {
590 parent = *p;
591 th = rb_entry(parent, struct thread, rb_node);
592
593 if (th->pid == pid)
594 return th;
595
596 if (pid < th->pid)
597 p = &(*p)->rb_left;
598 else
599 p = &(*p)->rb_right;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300600 }
601
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300602 th = thread__new(pid);
603 if (th != NULL) {
604 rb_link_node(&th->rb_node, parent, p);
605 rb_insert_color(&th->rb_node, &threads);
606 }
607 return th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300608}
609
610static void thread__insert_map(struct thread *self, struct map *map)
611{
612 list_add_tail(&map->node, &self->maps);
613}
614
615static struct map *thread__find_map(struct thread *self, uint64_t ip)
616{
617 if (self == NULL)
618 return NULL;
619
620 struct map *pos;
621
622 list_for_each_entry(pos, &self->maps, node)
623 if (ip >= pos->start && ip <= pos->end)
624 return pos;
625
626 return NULL;
627}
628
629static void threads__fprintf(FILE *fp)
630{
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300631 struct rb_node *nd;
632 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
633 struct thread *pos = rb_entry(nd, struct thread, rb_node);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300634 thread__fprintf(pos, fp);
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300635 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300636}
637
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200638static int __cmd_report(void)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300639{
640 unsigned long offset = 0;
641 unsigned long head = 0;
642 struct stat stat;
643 char *buf;
644 event_t *event;
645 int ret, rc = EXIT_FAILURE;
646 unsigned long total = 0;
647
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300648 input = open(input_name, O_RDONLY);
649 if (input < 0) {
650 perror("failed to open file");
651 exit(-1);
652 }
653
654 ret = fstat(input, &stat);
655 if (ret < 0) {
656 perror("failed to stat file");
657 exit(-1);
658 }
659
660 if (!stat.st_size) {
661 fprintf(stderr, "zero-sized file, nothing to do!\n");
662 exit(0);
663 }
664
665 if (load_kallsyms() < 0) {
666 perror("failed to open kallsyms");
667 return EXIT_FAILURE;
668 }
669
670remap:
671 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
672 MAP_SHARED, input, offset);
673 if (buf == MAP_FAILED) {
674 perror("failed to mmap file");
675 exit(-1);
676 }
677
678more:
679 event = (event_t *)(buf + head);
680
681 if (head + event->header.size >= page_size * mmap_window) {
682 unsigned long shift = page_size * (head / page_size);
683 int ret;
684
685 ret = munmap(buf, page_size * mmap_window);
686 assert(ret == 0);
687
688 offset += shift;
689 head -= shift;
690 goto remap;
691 }
692
693
694 if (!event->header.size) {
695 fprintf(stderr, "zero-sized event at file offset %ld\n", offset + head);
696 fprintf(stderr, "skipping %ld bytes of events.\n", stat.st_size - offset - head);
697 goto done;
698 }
699
700 head += event->header.size;
701
702 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) {
703 char level;
704 int show = 0;
705 struct dso *dso = NULL;
706 struct thread *thread = threads__findnew(event->ip.pid);
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200707 uint64_t ip = event->ip.ip;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300708
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300709 if (thread == NULL) {
710 fprintf(stderr, "problem processing %d event, bailing out\n",
711 event->header.type);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300712 goto done;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300713 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300714
715 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
716 show = SHOW_KERNEL;
717 level = 'k';
718 dso = kernel_dso;
719 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
720 show = SHOW_USER;
721 level = '.';
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200722 struct map *map = thread__find_map(thread, ip);
723 if (map != NULL) {
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300724 dso = map->dso;
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200725 ip -= map->start + map->pgoff;
726 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300727 } else {
728 show = SHOW_HV;
729 level = 'H';
730 }
731
732 if (show & show_mask) {
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200733 struct symbol *sym = dso__find_symbol(dso, ip);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300734
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200735 if (thread__symbol_incnew(thread, sym, ip, dso, level)) {
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300736 fprintf(stderr, "problem incrementing symbol count, bailing out\n");
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300737 goto done;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300738 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300739 }
740 total++;
741 } else switch (event->header.type) {
742 case PERF_EVENT_MMAP: {
743 struct thread *thread = threads__findnew(event->mmap.pid);
744 struct map *map = map__new(&event->mmap);
745
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300746 if (thread == NULL || map == NULL) {
747 fprintf(stderr, "problem processing PERF_EVENT_MMAP, bailing out\n");
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300748 goto done;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300749 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300750 thread__insert_map(thread, map);
751 break;
752 }
753 case PERF_EVENT_COMM: {
754 struct thread *thread = threads__findnew(event->comm.pid);
755
756 if (thread == NULL ||
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300757 thread__set_comm(thread, event->comm.comm)) {
758 fprintf(stderr, "problem processing PERF_EVENT_COMM, bailing out\n");
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300759 goto done;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300760 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300761 break;
762 }
763 }
764
765 if (offset + head < stat.st_size)
766 goto more;
767
768 rc = EXIT_SUCCESS;
769done:
770 close(input);
771 //dsos__fprintf(stdout);
772 threads__fprintf(stdout);
773#if 0
774 std::map<std::string, int>::iterator hi = hist.begin();
775
776 while (hi != hist.end()) {
777 rev_hist.insert(std::pair<int, std::string>(hi->second, hi->first));
778 hist.erase(hi++);
779 }
780
781 std::multimap<int, std::string>::const_iterator ri = rev_hist.begin();
782
783 while (ri != rev_hist.end()) {
784 printf(" %5.2f %s\n", (100.0 * ri->first)/total, ri->second.c_str());
785 ri++;
786 }
787#endif
788 return rc;
789}
790
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200791static const char * const report_usage[] = {
792 "perf report [<options>] <command>",
793 NULL
794};
795
796static const struct option options[] = {
797 OPT_STRING('i', "input", &input_name, "file",
798 "input file name"),
799 OPT_END()
800};
801
802int cmd_report(int argc, const char **argv, const char *prefix)
803{
804 elf_version(EV_CURRENT);
805
806 page_size = getpagesize();
807
808 parse_options(argc, argv, options, report_usage, 0);
809
810 return __cmd_report();
811}