blob: ed3da9d61985abf1c4943bb76b19bb5f3590b409 [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 Melo03f63162009-05-26 19:21:55 -03006#include <ctype.h>
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03007
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -03008#include "util/list.h"
9#include "util/rbtree.h"
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030010
Ingo Molnar53cb8bc2009-05-26 09:17:18 +020011#include "perf.h"
12
13#include "util/parse-options.h"
14#include "util/parse-events.h"
15
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030016#define SHOW_KERNEL 1
17#define SHOW_USER 2
18#define SHOW_HV 4
19
Ingo Molnar53cb8bc2009-05-26 09:17:18 +020020static char const *input_name = "output.perf";
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030021static int input;
22static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
23
Ingo Molnar97b07b62009-05-26 18:48:58 +020024static int dump_trace = 0;
25
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030026static unsigned long page_size;
27static unsigned long mmap_window = 32;
28
Ingo Molnar53cb8bc2009-05-26 09:17:18 +020029const char *perf_event_names[] = {
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030030 [PERF_EVENT_MMAP] = " PERF_EVENT_MMAP",
31 [PERF_EVENT_MUNMAP] = " PERF_EVENT_MUNMAP",
32 [PERF_EVENT_COMM] = " PERF_EVENT_COMM",
33};
34
35struct ip_event {
36 struct perf_event_header header;
37 __u64 ip;
38 __u32 pid, tid;
39};
40struct mmap_event {
41 struct perf_event_header header;
42 __u32 pid, tid;
43 __u64 start;
44 __u64 len;
45 __u64 pgoff;
46 char filename[PATH_MAX];
47};
48struct comm_event {
49 struct perf_event_header header;
50 __u32 pid,tid;
51 char comm[16];
52};
53
54typedef union event_union {
55 struct perf_event_header header;
56 struct ip_event ip;
57 struct mmap_event mmap;
58 struct comm_event comm;
59} event_t;
60
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030061struct symbol {
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -030062 struct rb_node rb_node;
63 uint64_t start;
64 uint64_t end;
65 char name[0];
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030066};
67
68static struct symbol *symbol__new(uint64_t start, uint64_t len, const char *name)
69{
70 struct symbol *self = malloc(sizeof(*self) + strlen(name) + 1);
71
72 if (self != NULL) {
73 self->start = start;
74 self->end = start + len;
75 strcpy(self->name, name);
76 }
77
78 return self;
79}
80
81static void symbol__delete(struct symbol *self)
82{
83 free(self);
84}
85
86static size_t symbol__fprintf(struct symbol *self, FILE *fp)
87{
88 return fprintf(fp, " %lx-%lx %s\n",
89 self->start, self->end, self->name);
90}
91
92struct dso {
93 struct list_head node;
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -030094 struct rb_root syms;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030095 char name[0];
96};
97
98static struct dso *dso__new(const char *name)
99{
100 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
101
102 if (self != NULL) {
103 strcpy(self->name, name);
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300104 self->syms = RB_ROOT;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300105 }
106
107 return self;
108}
109
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300110static void dso__delete_symbols(struct dso *self)
111{
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300112 struct symbol *pos;
113 struct rb_node *next = rb_first(&self->syms);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300114
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300115 while (next) {
116 pos = rb_entry(next, struct symbol, rb_node);
117 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300118 symbol__delete(pos);
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300119 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300120}
121
122static void dso__delete(struct dso *self)
123{
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300124 dso__delete_symbols(self);
125 free(self);
126}
127
128static void dso__insert_symbol(struct dso *self, struct symbol *sym)
129{
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300130 struct rb_node **p = &self->syms.rb_node;
131 struct rb_node *parent = NULL;
132 const uint64_t ip = sym->start;
133 struct symbol *s;
134
135 while (*p != NULL) {
136 parent = *p;
137 s = rb_entry(parent, struct symbol, rb_node);
138 if (ip < s->start)
139 p = &(*p)->rb_left;
140 else
141 p = &(*p)->rb_right;
142 }
143 rb_link_node(&sym->rb_node, parent, p);
144 rb_insert_color(&sym->rb_node, &self->syms);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300145}
146
147static struct symbol *dso__find_symbol(struct dso *self, uint64_t ip)
148{
149 if (self == NULL)
150 return NULL;
151
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300152 struct rb_node *n = self->syms.rb_node;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300153
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300154 while (n) {
155 struct symbol *s = rb_entry(n, struct symbol, rb_node);
156
157 if (ip < s->start)
158 n = n->rb_left;
159 else if (ip > s->end)
160 n = n->rb_right;
161 else
162 return s;
163 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300164
165 return NULL;
166}
167
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300168/**
169 * elf_symtab__for_each_symbol - iterate thru all the symbols
170 *
171 * @self: struct elf_symtab instance to iterate
172 * @index: uint32_t index
173 * @sym: GElf_Sym iterator
174 */
175#define elf_symtab__for_each_symbol(syms, nr_syms, index, sym) \
176 for (index = 0, gelf_getsym(syms, index, &sym);\
177 index < nr_syms; \
178 index++, gelf_getsym(syms, index, &sym))
179
180static inline uint8_t elf_sym__type(const GElf_Sym *sym)
181{
182 return GELF_ST_TYPE(sym->st_info);
183}
184
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200185static inline int elf_sym__is_function(const GElf_Sym *sym)
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300186{
187 return elf_sym__type(sym) == STT_FUNC &&
188 sym->st_name != 0 &&
189 sym->st_shndx != SHN_UNDEF;
190}
191
192static inline const char *elf_sym__name(const GElf_Sym *sym,
193 const Elf_Data *symstrs)
194{
195 return symstrs->d_buf + sym->st_name;
196}
197
198static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
199 GElf_Shdr *shp, const char *name,
200 size_t *index)
201{
202 Elf_Scn *sec = NULL;
203 size_t cnt = 1;
204
205 while ((sec = elf_nextscn(elf, sec)) != NULL) {
206 char *str;
207
208 gelf_getshdr(sec, shp);
209 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
210 if (!strcmp(name, str)) {
211 if (index)
212 *index = cnt;
213 break;
214 }
215 ++cnt;
216 }
217
218 return sec;
219}
220
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300221static int dso__load(struct dso *self)
222{
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300223 int fd = open(self->name, O_RDONLY), err = -1;
224
225 if (fd == -1)
226 return -1;
227
228 Elf *elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
229 if (elf == NULL) {
230 fprintf(stderr, "%s: cannot read %s ELF file.\n",
231 __func__, self->name);
232 goto out_close;
233 }
234
235 GElf_Ehdr ehdr;
236 if (gelf_getehdr(elf, &ehdr) == NULL) {
237 fprintf(stderr, "%s: cannot get elf header.\n", __func__);
238 goto out_elf_end;
239 }
240
241 GElf_Shdr shdr;
242 Elf_Scn *sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
243 if (sec == NULL)
244 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
245
246 if (sec == NULL)
247 goto out_elf_end;
248
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300249 Elf_Data *syms = elf_getdata(sec, NULL);
250 if (syms == NULL)
251 goto out_elf_end;
252
253 sec = elf_getscn(elf, shdr.sh_link);
254 if (sec == NULL)
255 goto out_elf_end;
256
257 Elf_Data *symstrs = elf_getdata(sec, NULL);
258 if (symstrs == NULL)
259 goto out_elf_end;
260
261 const uint32_t nr_syms = shdr.sh_size / shdr.sh_entsize;
262
263 GElf_Sym sym;
264 uint32_t index;
265 elf_symtab__for_each_symbol(syms, nr_syms, index, sym) {
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200266 struct symbol *f;
267
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300268 if (!elf_sym__is_function(&sym))
269 continue;
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200270
271 sec = elf_getscn(elf, sym.st_shndx);
272 if (!sec)
273 goto out_elf_end;
274
275 gelf_getshdr(sec, &shdr);
276 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
277
278 f = symbol__new(sym.st_value, sym.st_size,
279 elf_sym__name(&sym, symstrs));
280 if (!f)
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300281 goto out_elf_end;
282
283 dso__insert_symbol(self, f);
284 }
285
286 err = 0;
287out_elf_end:
288 elf_end(elf);
289out_close:
290 close(fd);
291 return err;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300292}
293
294static size_t dso__fprintf(struct dso *self, FILE *fp)
295{
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300296 size_t ret = fprintf(fp, "dso: %s\n", self->name);
297
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300298 struct rb_node *nd;
299 for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
300 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300301 ret += symbol__fprintf(pos, fp);
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300302 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300303
304 return ret;
305}
306
307static LIST_HEAD(dsos);
308static struct dso *kernel_dso;
309
310static void dsos__add(struct dso *dso)
311{
312 list_add_tail(&dso->node, &dsos);
313}
314
315static struct dso *dsos__find(const char *name)
316{
317 struct dso *pos;
318
319 list_for_each_entry(pos, &dsos, node)
320 if (strcmp(pos->name, name) == 0)
321 return pos;
322 return NULL;
323}
324
325static struct dso *dsos__findnew(const char *name)
326{
327 struct dso *dso = dsos__find(name);
328
329 if (dso == NULL) {
330 dso = dso__new(name);
331 if (dso != NULL && dso__load(dso) < 0)
332 goto out_delete_dso;
333
334 dsos__add(dso);
335 }
336
337 return dso;
338
339out_delete_dso:
340 dso__delete(dso);
341 return NULL;
342}
343
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200344void dsos__fprintf(FILE *fp)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300345{
346 struct dso *pos;
347
348 list_for_each_entry(pos, &dsos, node)
349 dso__fprintf(pos, fp);
350}
351
Arnaldo Carvalho de Melod8d16562009-05-26 19:20:57 -0300352static int hex(char ch)
353{
354 if ((ch >= '0') && (ch <= '9'))
355 return ch - '0';
356 if ((ch >= 'a') && (ch <= 'f'))
357 return ch - 'a' + 10;
358 if ((ch >= 'A') && (ch <= 'F'))
359 return ch - 'A' + 10;
360 return -1;
361}
362
363/*
364 * While we find nice hex chars, build a long_val.
365 * Return number of chars processed.
366 */
367int hex2long(char *ptr, unsigned long *long_val)
368{
369 const char *p = ptr;
370 *long_val = 0;
371
372 while (*p) {
373 const int hex_val = hex(*p);
374
375 if (hex_val < 0)
376 break;
377
378 *long_val = (*long_val << 4) | hex_val;
379 p++;
380 }
381
382 return p - ptr;
383}
384
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300385static int load_kallsyms(void)
386{
Ingo Molnaraf836322009-05-27 08:38:48 +0200387 struct rb_node *nd, *prevnd;
388 char *line = NULL;
389 FILE *file;
390 size_t n;
391
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300392 kernel_dso = dso__new("[kernel]");
393 if (kernel_dso == NULL)
394 return -1;
395
Ingo Molnaraf836322009-05-27 08:38:48 +0200396 file = fopen("/proc/kallsyms", "r");
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300397 if (file == NULL)
398 goto out_delete_dso;
399
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300400 while (!feof(file)) {
Arnaldo Carvalho de Melod8d16562009-05-26 19:20:57 -0300401 unsigned long start;
Ingo Molnaraf836322009-05-27 08:38:48 +0200402 struct symbol *sym;
403 int line_len, len;
404 char symbol_type;
405
406 line_len = getline(&line, &n, file);
Arnaldo Carvalho de Melod8d16562009-05-26 19:20:57 -0300407 if (line_len < 0)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300408 break;
409
410 if (!line)
411 goto out_delete_dso;
412
Arnaldo Carvalho de Melod8d16562009-05-26 19:20:57 -0300413 line[--line_len] = '\0'; /* \n */
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300414
Ingo Molnaraf836322009-05-27 08:38:48 +0200415 len = hex2long(line, &start);
416
Arnaldo Carvalho de Melo03f63162009-05-26 19:21:55 -0300417 len++;
418 if (len + 2 >= line_len)
419 continue;
420
Ingo Molnaraf836322009-05-27 08:38:48 +0200421 symbol_type = toupper(line[len]);
Arnaldo Carvalho de Melo03f63162009-05-26 19:21:55 -0300422 /*
423 * We're interested only in code ('T'ext)
424 */
Ingo Molnaraf836322009-05-27 08:38:48 +0200425 if (symbol_type != 'T' && symbol_type != 'W')
Arnaldo Carvalho de Melod8d16562009-05-26 19:20:57 -0300426 continue;
427 /*
428 * Well fix up the end later, when we have all sorted.
429 */
Ingo Molnaraf836322009-05-27 08:38:48 +0200430 sym = symbol__new(start, 0xdead, line + len + 2);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300431
Arnaldo Carvalho de Melod8d16562009-05-26 19:20:57 -0300432 if (sym == NULL)
433 goto out_delete_dso;
434
435 dso__insert_symbol(kernel_dso, sym);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300436 }
437
Arnaldo Carvalho de Meloabd54f62009-05-26 12:21:34 -0300438 /*
439 * Now that we have all sorted out, just set the ->end of all
440 * symbols
441 */
Ingo Molnaraf836322009-05-27 08:38:48 +0200442 prevnd = rb_first(&kernel_dso->syms);
Arnaldo Carvalho de Meloabd54f62009-05-26 12:21:34 -0300443
444 if (prevnd == NULL)
445 goto out_delete_line;
446
447 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
448 struct symbol *prev = rb_entry(prevnd, struct symbol, rb_node),
449 *curr = rb_entry(nd, struct symbol, rb_node);
450
451 prev->end = curr->start - 1;
452 prevnd = nd;
453 }
454
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300455 dsos__add(kernel_dso);
456 free(line);
457 fclose(file);
Ingo Molnaraf836322009-05-27 08:38:48 +0200458
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300459 return 0;
460
Arnaldo Carvalho de Melo59d81022009-05-26 11:14:27 -0300461out_delete_line:
462 free(line);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300463out_delete_dso:
464 dso__delete(kernel_dso);
465 return -1;
466}
467
468struct map {
469 struct list_head node;
470 uint64_t start;
471 uint64_t end;
472 uint64_t pgoff;
473 struct dso *dso;
474};
475
476static struct map *map__new(struct mmap_event *event)
477{
478 struct map *self = malloc(sizeof(*self));
479
480 if (self != NULL) {
481 self->start = event->start;
482 self->end = event->start + event->len;
483 self->pgoff = event->pgoff;
484
485 self->dso = dsos__findnew(event->filename);
486 if (self->dso == NULL)
487 goto out_delete;
488 }
489 return self;
490out_delete:
491 free(self);
492 return NULL;
493}
494
495static size_t map__fprintf(struct map *self, FILE *fp)
496{
497 return fprintf(fp, " %lx-%lx %lx %s\n",
498 self->start, self->end, self->pgoff, self->dso->name);
499}
500
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300501struct thread;
502
503static const char *thread__name(struct thread *self, char *bf, size_t size);
504
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300505struct symhist {
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300506 struct rb_node rb_node;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300507 struct dso *dso;
508 struct symbol *sym;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300509 struct thread *thread;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300510 uint64_t ip;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300511 uint32_t count;
512 char level;
513};
514
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300515static struct symhist *symhist__new(struct symbol *sym, uint64_t ip,
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300516 struct thread *thread, struct dso *dso,
517 char level)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300518{
519 struct symhist *self = malloc(sizeof(*self));
520
521 if (self != NULL) {
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300522 self->sym = sym;
523 self->thread = thread;
524 self->ip = ip;
525 self->dso = dso;
526 self->level = level;
527 self->count = 1;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300528 }
529
530 return self;
531}
532
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200533void symhist__delete(struct symhist *self)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300534{
535 free(self);
536}
537
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300538static void symhist__inc(struct symhist *self)
539{
540 ++self->count;
541}
542
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300543static size_t
544symhist__fprintf(struct symhist *self, uint64_t total_samples, FILE *fp)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300545{
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300546 char bf[32];
547 size_t ret;
548
549 if (total_samples)
550 ret = fprintf(fp, "%5.2f", (self->count * 100.0) / total_samples);
551 else
552 ret = fprintf(fp, "%12d", self->count);
553
554 ret += fprintf(fp, "%14s [%c] %#018llx ",
555 thread__name(self->thread, bf, sizeof(bf)),
556 self->level, (unsigned long long)self->ip);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300557
558 if (self->level != '.')
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300559 ret += fprintf(fp, "%s\n",
560 self->sym ? self->sym->name : "<unknown>");
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300561 else
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300562 ret += fprintf(fp, "%s: %s\n",
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200563 self->dso ? self->dso->name : "<unknown>",
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300564 self->sym ? self->sym->name : "<unknown>");
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300565 return ret;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300566}
567
568struct thread {
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300569 struct rb_node rb_node;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300570 struct list_head maps;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300571 struct rb_root symhists;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300572 pid_t pid;
573 char *comm;
574};
575
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300576static const char *thread__name(struct thread *self, char *bf, size_t size)
577{
578 if (self->comm)
579 return self->comm;
580
581 snprintf(bf, sizeof(bf), ":%u", self->pid);
582 return bf;
583}
584
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300585static struct thread *thread__new(pid_t pid)
586{
587 struct thread *self = malloc(sizeof(*self));
588
589 if (self != NULL) {
590 self->pid = pid;
591 self->comm = NULL;
592 INIT_LIST_HEAD(&self->maps);
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300593 self->symhists = RB_ROOT;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300594 }
595
596 return self;
597}
598
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300599static int thread__symbol_incnew(struct thread *self, struct symbol *sym,
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300600 uint64_t ip, struct dso *dso, char level)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300601{
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300602 struct rb_node **p = &self->symhists.rb_node;
603 struct rb_node *parent = NULL;
604 struct symhist *sh;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300605
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300606 while (*p != NULL) {
607 parent = *p;
608 sh = rb_entry(parent, struct symhist, rb_node);
609
610 if (sh->sym == sym || ip == sh->ip) {
611 symhist__inc(sh);
612 return 0;
613 }
614
615 /* Handle unresolved symbols too */
616 const uint64_t start = !sh->sym ? sh->ip : sh->sym->start;
617
618 if (ip < start)
619 p = &(*p)->rb_left;
620 else
621 p = &(*p)->rb_right;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300622 }
623
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300624 sh = symhist__new(sym, ip, self, dso, level);
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300625 if (sh == NULL)
626 return -ENOMEM;
627 rb_link_node(&sh->rb_node, parent, p);
628 rb_insert_color(&sh->rb_node, &self->symhists);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300629 return 0;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300630}
631
632static int thread__set_comm(struct thread *self, const char *comm)
633{
634 self->comm = strdup(comm);
635 return self->comm ? 0 : -ENOMEM;
636}
637
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200638size_t thread__maps_fprintf(struct thread *self, FILE *fp)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300639{
640 struct map *pos;
641 size_t ret = 0;
642
643 list_for_each_entry(pos, &self->maps, node)
644 ret += map__fprintf(pos, fp);
645
646 return ret;
647}
648
649static size_t thread__fprintf(struct thread *self, FILE *fp)
650{
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300651 int ret = fprintf(fp, "thread: %d %s\n", self->pid, self->comm);
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300652 struct rb_node *nd;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300653
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300654 for (nd = rb_first(&self->symhists); nd; nd = rb_next(nd)) {
655 struct symhist *pos = rb_entry(nd, struct symhist, rb_node);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300656 ret += symhist__fprintf(pos, 0, fp);
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300657 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300658
659 return ret;
660}
661
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300662static struct rb_root threads = RB_ROOT;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300663
664static struct thread *threads__findnew(pid_t pid)
665{
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300666 struct rb_node **p = &threads.rb_node;
667 struct rb_node *parent = NULL;
668 struct thread *th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300669
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300670 while (*p != NULL) {
671 parent = *p;
672 th = rb_entry(parent, struct thread, rb_node);
673
674 if (th->pid == pid)
675 return th;
676
677 if (pid < th->pid)
678 p = &(*p)->rb_left;
679 else
680 p = &(*p)->rb_right;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300681 }
682
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300683 th = thread__new(pid);
684 if (th != NULL) {
685 rb_link_node(&th->rb_node, parent, p);
686 rb_insert_color(&th->rb_node, &threads);
687 }
688 return th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300689}
690
691static void thread__insert_map(struct thread *self, struct map *map)
692{
693 list_add_tail(&map->node, &self->maps);
694}
695
696static struct map *thread__find_map(struct thread *self, uint64_t ip)
697{
698 if (self == NULL)
699 return NULL;
700
701 struct map *pos;
702
703 list_for_each_entry(pos, &self->maps, node)
704 if (ip >= pos->start && ip <= pos->end)
705 return pos;
706
707 return NULL;
708}
709
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300710void threads__fprintf(FILE *fp)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300711{
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300712 struct rb_node *nd;
713 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
714 struct thread *pos = rb_entry(nd, struct thread, rb_node);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300715 thread__fprintf(pos, fp);
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300716 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300717}
718
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300719static struct rb_root global_symhists = RB_ROOT;
720
721static void threads__insert_symhist(struct symhist *sh)
722{
723 struct rb_node **p = &global_symhists.rb_node;
724 struct rb_node *parent = NULL;
725 struct symhist *iter;
726
727 while (*p != NULL) {
728 parent = *p;
729 iter = rb_entry(parent, struct symhist, rb_node);
730
731 /* Reverse order */
732 if (sh->count > iter->count)
733 p = &(*p)->rb_left;
734 else
735 p = &(*p)->rb_right;
736 }
737
738 rb_link_node(&sh->rb_node, parent, p);
739 rb_insert_color(&sh->rb_node, &global_symhists);
740}
741
742static void threads__sort_symhists(void)
743{
744 struct rb_node *nd;
745
746 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
747 struct thread *thread = rb_entry(nd, struct thread, rb_node);
748 struct rb_node *next = rb_first(&thread->symhists);
749
750 while (next) {
751 struct symhist *n = rb_entry(next, struct symhist,
752 rb_node);
753 next = rb_next(&n->rb_node);
754 rb_erase(&n->rb_node, &thread->symhists);
755 threads__insert_symhist(n);
756 }
757
758 }
759}
760
761static size_t threads__symhists_fprintf(uint64_t total_samples, FILE *fp)
762{
763 struct rb_node *nd;
764 size_t ret = 0;
765
766 for (nd = rb_first(&global_symhists); nd; nd = rb_next(nd)) {
767 struct symhist *pos = rb_entry(nd, struct symhist, rb_node);
768 ret += symhist__fprintf(pos, total_samples, fp);
769 }
770
771 return ret;
772}
773
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200774static int __cmd_report(void)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300775{
776 unsigned long offset = 0;
777 unsigned long head = 0;
778 struct stat stat;
779 char *buf;
780 event_t *event;
781 int ret, rc = EXIT_FAILURE;
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200782 uint32_t size;
Ingo Molnarf49515b2009-05-26 19:03:36 +0200783 unsigned long total = 0, total_mmap = 0, total_comm = 0, total_unknown = 0;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300784
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300785 input = open(input_name, O_RDONLY);
786 if (input < 0) {
787 perror("failed to open file");
788 exit(-1);
789 }
790
791 ret = fstat(input, &stat);
792 if (ret < 0) {
793 perror("failed to stat file");
794 exit(-1);
795 }
796
797 if (!stat.st_size) {
798 fprintf(stderr, "zero-sized file, nothing to do!\n");
799 exit(0);
800 }
801
802 if (load_kallsyms() < 0) {
803 perror("failed to open kallsyms");
804 return EXIT_FAILURE;
805 }
806
807remap:
808 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
809 MAP_SHARED, input, offset);
810 if (buf == MAP_FAILED) {
811 perror("failed to mmap file");
812 exit(-1);
813 }
814
815more:
816 event = (event_t *)(buf + head);
817
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200818 size = event->header.size;
819 if (!size)
820 size = 8;
821
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300822 if (head + event->header.size >= page_size * mmap_window) {
823 unsigned long shift = page_size * (head / page_size);
824 int ret;
825
826 ret = munmap(buf, page_size * mmap_window);
827 assert(ret == 0);
828
829 offset += shift;
830 head -= shift;
831 goto remap;
832 }
833
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200834 size = event->header.size;
835 if (!size)
836 goto broken_event;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300837
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300838 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) {
839 char level;
840 int show = 0;
841 struct dso *dso = NULL;
842 struct thread *thread = threads__findnew(event->ip.pid);
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200843 uint64_t ip = event->ip.ip;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300844
Ingo Molnar97b07b62009-05-26 18:48:58 +0200845 if (dump_trace) {
Ingo Molnarf49515b2009-05-26 19:03:36 +0200846 fprintf(stderr, "%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
847 (void *)(offset + head),
848 (void *)(long)(event->header.size),
Ingo Molnar97b07b62009-05-26 18:48:58 +0200849 event->header.misc,
850 event->ip.pid,
851 (void *)event->ip.ip);
852 }
853
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300854 if (thread == NULL) {
855 fprintf(stderr, "problem processing %d event, bailing out\n",
856 event->header.type);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300857 goto done;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300858 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300859
860 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
861 show = SHOW_KERNEL;
862 level = 'k';
863 dso = kernel_dso;
864 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
865 show = SHOW_USER;
866 level = '.';
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200867 struct map *map = thread__find_map(thread, ip);
868 if (map != NULL) {
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300869 dso = map->dso;
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200870 ip -= map->start + map->pgoff;
871 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300872 } else {
873 show = SHOW_HV;
874 level = 'H';
875 }
876
877 if (show & show_mask) {
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200878 struct symbol *sym = dso__find_symbol(dso, ip);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300879
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200880 if (thread__symbol_incnew(thread, sym, ip, dso, level)) {
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300881 fprintf(stderr, "problem incrementing symbol count, bailing out\n");
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300882 goto done;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300883 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300884 }
885 total++;
886 } else switch (event->header.type) {
887 case PERF_EVENT_MMAP: {
888 struct thread *thread = threads__findnew(event->mmap.pid);
889 struct map *map = map__new(&event->mmap);
890
Ingo Molnar97b07b62009-05-26 18:48:58 +0200891 if (dump_trace) {
Ingo Molnarf49515b2009-05-26 19:03:36 +0200892 fprintf(stderr, "%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n",
893 (void *)(offset + head),
894 (void *)(long)(event->header.size),
Ingo Molnar97b07b62009-05-26 18:48:58 +0200895 (void *)event->mmap.start,
896 (void *)event->mmap.len,
897 (void *)event->mmap.pgoff,
898 event->mmap.filename);
899 }
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300900 if (thread == NULL || map == NULL) {
901 fprintf(stderr, "problem processing PERF_EVENT_MMAP, bailing out\n");
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300902 goto done;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300903 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300904 thread__insert_map(thread, map);
Ingo Molnar97b07b62009-05-26 18:48:58 +0200905 total_mmap++;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300906 break;
907 }
908 case PERF_EVENT_COMM: {
909 struct thread *thread = threads__findnew(event->comm.pid);
910
Ingo Molnar97b07b62009-05-26 18:48:58 +0200911 if (dump_trace) {
Ingo Molnarf49515b2009-05-26 19:03:36 +0200912 fprintf(stderr, "%p [%p]: PERF_EVENT_COMM: %s:%d\n",
913 (void *)(offset + head),
914 (void *)(long)(event->header.size),
Ingo Molnar97b07b62009-05-26 18:48:58 +0200915 event->comm.comm, event->comm.pid);
916 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300917 if (thread == NULL ||
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300918 thread__set_comm(thread, event->comm.comm)) {
919 fprintf(stderr, "problem processing PERF_EVENT_COMM, bailing out\n");
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300920 goto done;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300921 }
Ingo Molnar97b07b62009-05-26 18:48:58 +0200922 total_comm++;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300923 break;
924 }
Ingo Molnar97b07b62009-05-26 18:48:58 +0200925 default: {
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200926broken_event:
Ingo Molnarf49515b2009-05-26 19:03:36 +0200927 fprintf(stderr, "%p [%p]: skipping unknown header type: %d\n",
928 (void *)(offset + head),
929 (void *)(long)(event->header.size),
Ingo Molnar97b07b62009-05-26 18:48:58 +0200930 event->header.type);
Ingo Molnar3e706112009-05-26 18:53:17 +0200931 total_unknown++;
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200932
933 /*
934 * assume we lost track of the stream, check alignment, and
935 * increment a single u64 in the hope to catch on again 'soon'.
936 */
937
938 if (unlikely(head & 7))
939 head &= ~7ULL;
940
941 size = 8;
Ingo Molnar97b07b62009-05-26 18:48:58 +0200942 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300943 }
944
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200945 head += size;
Ingo Molnarf49515b2009-05-26 19:03:36 +0200946
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300947 if (offset + head < stat.st_size)
948 goto more;
949
950 rc = EXIT_SUCCESS;
951done:
952 close(input);
Ingo Molnar97b07b62009-05-26 18:48:58 +0200953
954 if (dump_trace) {
Ingo Molnar3e706112009-05-26 18:53:17 +0200955 fprintf(stderr, " IP events: %10ld\n", total);
956 fprintf(stderr, " mmap events: %10ld\n", total_mmap);
957 fprintf(stderr, " comm events: %10ld\n", total_comm);
958 fprintf(stderr, " unknown events: %10ld\n", total_unknown);
Ingo Molnar97b07b62009-05-26 18:48:58 +0200959
960 return 0;
961 }
962
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300963 threads__sort_symhists();
964 threads__symhists_fprintf(total, stdout);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300965
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300966 return rc;
967}
968
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200969static const char * const report_usage[] = {
970 "perf report [<options>] <command>",
971 NULL
972};
973
974static const struct option options[] = {
975 OPT_STRING('i', "input", &input_name, "file",
976 "input file name"),
Ingo Molnar97b07b62009-05-26 18:48:58 +0200977 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
978 "dump raw trace in ASCII"),
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200979 OPT_END()
980};
981
982int cmd_report(int argc, const char **argv, const char *prefix)
983{
984 elf_version(EV_CURRENT);
985
986 page_size = getpagesize();
987
988 parse_options(argc, argv, options, report_usage, 0);
989
990 return __cmd_report();
991}