blob: c517483fd614efb3a1b89b6c2acf7f9db6b30a77 [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
Ingo Molnar97b07b62009-05-26 18:48:58 +020023static int dump_trace = 0;
24
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030025static unsigned long page_size;
26static unsigned long mmap_window = 32;
27
Ingo Molnar53cb8bc2009-05-26 09:17:18 +020028const char *perf_event_names[] = {
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030029 [PERF_EVENT_MMAP] = " PERF_EVENT_MMAP",
30 [PERF_EVENT_MUNMAP] = " PERF_EVENT_MUNMAP",
31 [PERF_EVENT_COMM] = " PERF_EVENT_COMM",
32};
33
34struct ip_event {
35 struct perf_event_header header;
36 __u64 ip;
37 __u32 pid, tid;
38};
39struct mmap_event {
40 struct perf_event_header header;
41 __u32 pid, tid;
42 __u64 start;
43 __u64 len;
44 __u64 pgoff;
45 char filename[PATH_MAX];
46};
47struct comm_event {
48 struct perf_event_header header;
49 __u32 pid,tid;
50 char comm[16];
51};
52
53typedef union event_union {
54 struct perf_event_header header;
55 struct ip_event ip;
56 struct mmap_event mmap;
57 struct comm_event comm;
58} event_t;
59
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030060struct symbol {
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -030061 struct rb_node rb_node;
62 uint64_t start;
63 uint64_t end;
64 char name[0];
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030065};
66
67static struct symbol *symbol__new(uint64_t start, uint64_t len, const char *name)
68{
69 struct symbol *self = malloc(sizeof(*self) + strlen(name) + 1);
70
71 if (self != NULL) {
72 self->start = start;
73 self->end = start + len;
74 strcpy(self->name, name);
75 }
76
77 return self;
78}
79
80static void symbol__delete(struct symbol *self)
81{
82 free(self);
83}
84
85static size_t symbol__fprintf(struct symbol *self, FILE *fp)
86{
87 return fprintf(fp, " %lx-%lx %s\n",
88 self->start, self->end, self->name);
89}
90
91struct dso {
92 struct list_head node;
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -030093 struct rb_root syms;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030094 char name[0];
95};
96
97static struct dso *dso__new(const char *name)
98{
99 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
100
101 if (self != NULL) {
102 strcpy(self->name, name);
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300103 self->syms = RB_ROOT;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300104 }
105
106 return self;
107}
108
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300109static void dso__delete_symbols(struct dso *self)
110{
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300111 struct symbol *pos;
112 struct rb_node *next = rb_first(&self->syms);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300113
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300114 while (next) {
115 pos = rb_entry(next, struct symbol, rb_node);
116 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300117 symbol__delete(pos);
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300118 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300119}
120
121static void dso__delete(struct dso *self)
122{
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300123 dso__delete_symbols(self);
124 free(self);
125}
126
127static void dso__insert_symbol(struct dso *self, struct symbol *sym)
128{
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300129 struct rb_node **p = &self->syms.rb_node;
130 struct rb_node *parent = NULL;
131 const uint64_t ip = sym->start;
132 struct symbol *s;
133
134 while (*p != NULL) {
135 parent = *p;
136 s = rb_entry(parent, struct symbol, rb_node);
137 if (ip < s->start)
138 p = &(*p)->rb_left;
139 else
140 p = &(*p)->rb_right;
141 }
142 rb_link_node(&sym->rb_node, parent, p);
143 rb_insert_color(&sym->rb_node, &self->syms);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300144}
145
146static struct symbol *dso__find_symbol(struct dso *self, uint64_t ip)
147{
148 if (self == NULL)
149 return NULL;
150
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300151 struct rb_node *n = self->syms.rb_node;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300152
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300153 while (n) {
154 struct symbol *s = rb_entry(n, struct symbol, rb_node);
155
156 if (ip < s->start)
157 n = n->rb_left;
158 else if (ip > s->end)
159 n = n->rb_right;
160 else
161 return s;
162 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300163
164 return NULL;
165}
166
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300167/**
168 * elf_symtab__for_each_symbol - iterate thru all the symbols
169 *
170 * @self: struct elf_symtab instance to iterate
171 * @index: uint32_t index
172 * @sym: GElf_Sym iterator
173 */
174#define elf_symtab__for_each_symbol(syms, nr_syms, index, sym) \
175 for (index = 0, gelf_getsym(syms, index, &sym);\
176 index < nr_syms; \
177 index++, gelf_getsym(syms, index, &sym))
178
179static inline uint8_t elf_sym__type(const GElf_Sym *sym)
180{
181 return GELF_ST_TYPE(sym->st_info);
182}
183
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200184static inline int elf_sym__is_function(const GElf_Sym *sym)
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300185{
186 return elf_sym__type(sym) == STT_FUNC &&
187 sym->st_name != 0 &&
188 sym->st_shndx != SHN_UNDEF;
189}
190
191static inline const char *elf_sym__name(const GElf_Sym *sym,
192 const Elf_Data *symstrs)
193{
194 return symstrs->d_buf + sym->st_name;
195}
196
197static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
198 GElf_Shdr *shp, const char *name,
199 size_t *index)
200{
201 Elf_Scn *sec = NULL;
202 size_t cnt = 1;
203
204 while ((sec = elf_nextscn(elf, sec)) != NULL) {
205 char *str;
206
207 gelf_getshdr(sec, shp);
208 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
209 if (!strcmp(name, str)) {
210 if (index)
211 *index = cnt;
212 break;
213 }
214 ++cnt;
215 }
216
217 return sec;
218}
219
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300220static int dso__load(struct dso *self)
221{
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300222 int fd = open(self->name, O_RDONLY), err = -1;
223
224 if (fd == -1)
225 return -1;
226
227 Elf *elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
228 if (elf == NULL) {
229 fprintf(stderr, "%s: cannot read %s ELF file.\n",
230 __func__, self->name);
231 goto out_close;
232 }
233
234 GElf_Ehdr ehdr;
235 if (gelf_getehdr(elf, &ehdr) == NULL) {
236 fprintf(stderr, "%s: cannot get elf header.\n", __func__);
237 goto out_elf_end;
238 }
239
240 GElf_Shdr shdr;
241 Elf_Scn *sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
242 if (sec == NULL)
243 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
244
245 if (sec == NULL)
246 goto out_elf_end;
247
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300248 Elf_Data *syms = elf_getdata(sec, NULL);
249 if (syms == NULL)
250 goto out_elf_end;
251
252 sec = elf_getscn(elf, shdr.sh_link);
253 if (sec == NULL)
254 goto out_elf_end;
255
256 Elf_Data *symstrs = elf_getdata(sec, NULL);
257 if (symstrs == NULL)
258 goto out_elf_end;
259
260 const uint32_t nr_syms = shdr.sh_size / shdr.sh_entsize;
261
262 GElf_Sym sym;
263 uint32_t index;
264 elf_symtab__for_each_symbol(syms, nr_syms, index, sym) {
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200265 struct symbol *f;
266
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300267 if (!elf_sym__is_function(&sym))
268 continue;
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200269
270 sec = elf_getscn(elf, sym.st_shndx);
271 if (!sec)
272 goto out_elf_end;
273
274 gelf_getshdr(sec, &shdr);
275 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
276
277 f = symbol__new(sym.st_value, sym.st_size,
278 elf_sym__name(&sym, symstrs));
279 if (!f)
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300280 goto out_elf_end;
281
282 dso__insert_symbol(self, f);
283 }
284
285 err = 0;
286out_elf_end:
287 elf_end(elf);
288out_close:
289 close(fd);
290 return err;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300291}
292
293static size_t dso__fprintf(struct dso *self, FILE *fp)
294{
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300295 size_t ret = fprintf(fp, "dso: %s\n", self->name);
296
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300297 struct rb_node *nd;
298 for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
299 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300300 ret += symbol__fprintf(pos, fp);
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300301 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300302
303 return ret;
304}
305
306static LIST_HEAD(dsos);
307static struct dso *kernel_dso;
308
309static void dsos__add(struct dso *dso)
310{
311 list_add_tail(&dso->node, &dsos);
312}
313
314static struct dso *dsos__find(const char *name)
315{
316 struct dso *pos;
317
318 list_for_each_entry(pos, &dsos, node)
319 if (strcmp(pos->name, name) == 0)
320 return pos;
321 return NULL;
322}
323
324static struct dso *dsos__findnew(const char *name)
325{
326 struct dso *dso = dsos__find(name);
327
328 if (dso == NULL) {
329 dso = dso__new(name);
330 if (dso != NULL && dso__load(dso) < 0)
331 goto out_delete_dso;
332
333 dsos__add(dso);
334 }
335
336 return dso;
337
338out_delete_dso:
339 dso__delete(dso);
340 return NULL;
341}
342
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200343void dsos__fprintf(FILE *fp)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300344{
345 struct dso *pos;
346
347 list_for_each_entry(pos, &dsos, node)
348 dso__fprintf(pos, fp);
349}
350
Arnaldo Carvalho de Melod8d16562009-05-26 19:20:57 -0300351static int hex(char ch)
352{
353 if ((ch >= '0') && (ch <= '9'))
354 return ch - '0';
355 if ((ch >= 'a') && (ch <= 'f'))
356 return ch - 'a' + 10;
357 if ((ch >= 'A') && (ch <= 'F'))
358 return ch - 'A' + 10;
359 return -1;
360}
361
362/*
363 * While we find nice hex chars, build a long_val.
364 * Return number of chars processed.
365 */
366int hex2long(char *ptr, unsigned long *long_val)
367{
368 const char *p = ptr;
369 *long_val = 0;
370
371 while (*p) {
372 const int hex_val = hex(*p);
373
374 if (hex_val < 0)
375 break;
376
377 *long_val = (*long_val << 4) | hex_val;
378 p++;
379 }
380
381 return p - ptr;
382}
383
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300384static int load_kallsyms(void)
385{
386 kernel_dso = dso__new("[kernel]");
387 if (kernel_dso == NULL)
388 return -1;
389
390 FILE *file = fopen("/proc/kallsyms", "r");
391
392 if (file == NULL)
393 goto out_delete_dso;
394
395 char *line = NULL;
396 size_t n;
397
398 while (!feof(file)) {
Arnaldo Carvalho de Melod8d16562009-05-26 19:20:57 -0300399 unsigned long start;
400 int line_len = getline(&line, &n, file);
401 if (line_len < 0)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300402 break;
403
404 if (!line)
405 goto out_delete_dso;
406
Arnaldo Carvalho de Melod8d16562009-05-26 19:20:57 -0300407 line[--line_len] = '\0'; /* \n */
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300408
Arnaldo Carvalho de Melod8d16562009-05-26 19:20:57 -0300409 int len = hex2long(line, &start);
410
411 len += 3; /* ' t ' */
412 if (len >= line_len)
413 continue;
414 /*
415 * Well fix up the end later, when we have all sorted.
416 */
417 struct symbol *sym = symbol__new(start, 0xdead, line + len);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300418
Arnaldo Carvalho de Melod8d16562009-05-26 19:20:57 -0300419 if (sym == NULL)
420 goto out_delete_dso;
421
422 dso__insert_symbol(kernel_dso, sym);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300423 }
424
Arnaldo Carvalho de Meloabd54f62009-05-26 12:21:34 -0300425 /*
426 * Now that we have all sorted out, just set the ->end of all
427 * symbols
428 */
429 struct rb_node *nd, *prevnd = rb_first(&kernel_dso->syms);
430
431 if (prevnd == NULL)
432 goto out_delete_line;
433
434 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
435 struct symbol *prev = rb_entry(prevnd, struct symbol, rb_node),
436 *curr = rb_entry(nd, struct symbol, rb_node);
437
438 prev->end = curr->start - 1;
439 prevnd = nd;
440 }
441
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300442 dsos__add(kernel_dso);
443 free(line);
444 fclose(file);
445 return 0;
446
Arnaldo Carvalho de Melo59d81022009-05-26 11:14:27 -0300447out_delete_line:
448 free(line);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300449out_delete_dso:
450 dso__delete(kernel_dso);
451 return -1;
452}
453
454struct map {
455 struct list_head node;
456 uint64_t start;
457 uint64_t end;
458 uint64_t pgoff;
459 struct dso *dso;
460};
461
462static struct map *map__new(struct mmap_event *event)
463{
464 struct map *self = malloc(sizeof(*self));
465
466 if (self != NULL) {
467 self->start = event->start;
468 self->end = event->start + event->len;
469 self->pgoff = event->pgoff;
470
471 self->dso = dsos__findnew(event->filename);
472 if (self->dso == NULL)
473 goto out_delete;
474 }
475 return self;
476out_delete:
477 free(self);
478 return NULL;
479}
480
481static size_t map__fprintf(struct map *self, FILE *fp)
482{
483 return fprintf(fp, " %lx-%lx %lx %s\n",
484 self->start, self->end, self->pgoff, self->dso->name);
485}
486
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300487struct thread;
488
489static const char *thread__name(struct thread *self, char *bf, size_t size);
490
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300491struct symhist {
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300492 struct rb_node rb_node;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300493 struct dso *dso;
494 struct symbol *sym;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300495 struct thread *thread;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300496 uint64_t ip;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300497 uint32_t count;
498 char level;
499};
500
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300501static struct symhist *symhist__new(struct symbol *sym, uint64_t ip,
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300502 struct thread *thread, struct dso *dso,
503 char level)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300504{
505 struct symhist *self = malloc(sizeof(*self));
506
507 if (self != NULL) {
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300508 self->sym = sym;
509 self->thread = thread;
510 self->ip = ip;
511 self->dso = dso;
512 self->level = level;
513 self->count = 1;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300514 }
515
516 return self;
517}
518
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200519void symhist__delete(struct symhist *self)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300520{
521 free(self);
522}
523
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300524static void symhist__inc(struct symhist *self)
525{
526 ++self->count;
527}
528
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300529static size_t
530symhist__fprintf(struct symhist *self, uint64_t total_samples, FILE *fp)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300531{
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300532 char bf[32];
533 size_t ret;
534
535 if (total_samples)
536 ret = fprintf(fp, "%5.2f", (self->count * 100.0) / total_samples);
537 else
538 ret = fprintf(fp, "%12d", self->count);
539
540 ret += fprintf(fp, "%14s [%c] %#018llx ",
541 thread__name(self->thread, bf, sizeof(bf)),
542 self->level, (unsigned long long)self->ip);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300543
544 if (self->level != '.')
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300545 ret += fprintf(fp, "%s\n",
546 self->sym ? self->sym->name : "<unknown>");
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300547 else
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300548 ret += fprintf(fp, "%s: %s\n",
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200549 self->dso ? self->dso->name : "<unknown>",
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300550 self->sym ? self->sym->name : "<unknown>");
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300551 return ret;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300552}
553
554struct thread {
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300555 struct rb_node rb_node;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300556 struct list_head maps;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300557 struct rb_root symhists;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300558 pid_t pid;
559 char *comm;
560};
561
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300562static const char *thread__name(struct thread *self, char *bf, size_t size)
563{
564 if (self->comm)
565 return self->comm;
566
567 snprintf(bf, sizeof(bf), ":%u", self->pid);
568 return bf;
569}
570
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300571static struct thread *thread__new(pid_t pid)
572{
573 struct thread *self = malloc(sizeof(*self));
574
575 if (self != NULL) {
576 self->pid = pid;
577 self->comm = NULL;
578 INIT_LIST_HEAD(&self->maps);
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300579 self->symhists = RB_ROOT;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300580 }
581
582 return self;
583}
584
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300585static int thread__symbol_incnew(struct thread *self, struct symbol *sym,
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300586 uint64_t ip, struct dso *dso, char level)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300587{
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300588 struct rb_node **p = &self->symhists.rb_node;
589 struct rb_node *parent = NULL;
590 struct symhist *sh;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300591
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300592 while (*p != NULL) {
593 parent = *p;
594 sh = rb_entry(parent, struct symhist, rb_node);
595
596 if (sh->sym == sym || ip == sh->ip) {
597 symhist__inc(sh);
598 return 0;
599 }
600
601 /* Handle unresolved symbols too */
602 const uint64_t start = !sh->sym ? sh->ip : sh->sym->start;
603
604 if (ip < start)
605 p = &(*p)->rb_left;
606 else
607 p = &(*p)->rb_right;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300608 }
609
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300610 sh = symhist__new(sym, ip, self, dso, level);
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300611 if (sh == NULL)
612 return -ENOMEM;
613 rb_link_node(&sh->rb_node, parent, p);
614 rb_insert_color(&sh->rb_node, &self->symhists);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300615 return 0;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300616}
617
618static int thread__set_comm(struct thread *self, const char *comm)
619{
620 self->comm = strdup(comm);
621 return self->comm ? 0 : -ENOMEM;
622}
623
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200624size_t thread__maps_fprintf(struct thread *self, FILE *fp)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300625{
626 struct map *pos;
627 size_t ret = 0;
628
629 list_for_each_entry(pos, &self->maps, node)
630 ret += map__fprintf(pos, fp);
631
632 return ret;
633}
634
635static size_t thread__fprintf(struct thread *self, FILE *fp)
636{
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300637 int ret = fprintf(fp, "thread: %d %s\n", self->pid, self->comm);
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300638 struct rb_node *nd;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300639
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300640 for (nd = rb_first(&self->symhists); nd; nd = rb_next(nd)) {
641 struct symhist *pos = rb_entry(nd, struct symhist, rb_node);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300642 ret += symhist__fprintf(pos, 0, fp);
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300643 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300644
645 return ret;
646}
647
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300648static struct rb_root threads = RB_ROOT;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300649
650static struct thread *threads__findnew(pid_t pid)
651{
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300652 struct rb_node **p = &threads.rb_node;
653 struct rb_node *parent = NULL;
654 struct thread *th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300655
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300656 while (*p != NULL) {
657 parent = *p;
658 th = rb_entry(parent, struct thread, rb_node);
659
660 if (th->pid == pid)
661 return th;
662
663 if (pid < th->pid)
664 p = &(*p)->rb_left;
665 else
666 p = &(*p)->rb_right;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300667 }
668
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300669 th = thread__new(pid);
670 if (th != NULL) {
671 rb_link_node(&th->rb_node, parent, p);
672 rb_insert_color(&th->rb_node, &threads);
673 }
674 return th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300675}
676
677static void thread__insert_map(struct thread *self, struct map *map)
678{
679 list_add_tail(&map->node, &self->maps);
680}
681
682static struct map *thread__find_map(struct thread *self, uint64_t ip)
683{
684 if (self == NULL)
685 return NULL;
686
687 struct map *pos;
688
689 list_for_each_entry(pos, &self->maps, node)
690 if (ip >= pos->start && ip <= pos->end)
691 return pos;
692
693 return NULL;
694}
695
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300696void threads__fprintf(FILE *fp)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300697{
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300698 struct rb_node *nd;
699 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
700 struct thread *pos = rb_entry(nd, struct thread, rb_node);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300701 thread__fprintf(pos, fp);
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300702 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300703}
704
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300705static struct rb_root global_symhists = RB_ROOT;
706
707static void threads__insert_symhist(struct symhist *sh)
708{
709 struct rb_node **p = &global_symhists.rb_node;
710 struct rb_node *parent = NULL;
711 struct symhist *iter;
712
713 while (*p != NULL) {
714 parent = *p;
715 iter = rb_entry(parent, struct symhist, rb_node);
716
717 /* Reverse order */
718 if (sh->count > iter->count)
719 p = &(*p)->rb_left;
720 else
721 p = &(*p)->rb_right;
722 }
723
724 rb_link_node(&sh->rb_node, parent, p);
725 rb_insert_color(&sh->rb_node, &global_symhists);
726}
727
728static void threads__sort_symhists(void)
729{
730 struct rb_node *nd;
731
732 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
733 struct thread *thread = rb_entry(nd, struct thread, rb_node);
734 struct rb_node *next = rb_first(&thread->symhists);
735
736 while (next) {
737 struct symhist *n = rb_entry(next, struct symhist,
738 rb_node);
739 next = rb_next(&n->rb_node);
740 rb_erase(&n->rb_node, &thread->symhists);
741 threads__insert_symhist(n);
742 }
743
744 }
745}
746
747static size_t threads__symhists_fprintf(uint64_t total_samples, FILE *fp)
748{
749 struct rb_node *nd;
750 size_t ret = 0;
751
752 for (nd = rb_first(&global_symhists); nd; nd = rb_next(nd)) {
753 struct symhist *pos = rb_entry(nd, struct symhist, rb_node);
754 ret += symhist__fprintf(pos, total_samples, fp);
755 }
756
757 return ret;
758}
759
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200760static int __cmd_report(void)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300761{
762 unsigned long offset = 0;
763 unsigned long head = 0;
764 struct stat stat;
765 char *buf;
766 event_t *event;
767 int ret, rc = EXIT_FAILURE;
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200768 uint32_t size;
Ingo Molnarf49515b2009-05-26 19:03:36 +0200769 unsigned long total = 0, total_mmap = 0, total_comm = 0, total_unknown = 0;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300770
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300771 input = open(input_name, O_RDONLY);
772 if (input < 0) {
773 perror("failed to open file");
774 exit(-1);
775 }
776
777 ret = fstat(input, &stat);
778 if (ret < 0) {
779 perror("failed to stat file");
780 exit(-1);
781 }
782
783 if (!stat.st_size) {
784 fprintf(stderr, "zero-sized file, nothing to do!\n");
785 exit(0);
786 }
787
788 if (load_kallsyms() < 0) {
789 perror("failed to open kallsyms");
790 return EXIT_FAILURE;
791 }
792
793remap:
794 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
795 MAP_SHARED, input, offset);
796 if (buf == MAP_FAILED) {
797 perror("failed to mmap file");
798 exit(-1);
799 }
800
801more:
802 event = (event_t *)(buf + head);
803
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200804 size = event->header.size;
805 if (!size)
806 size = 8;
807
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300808 if (head + event->header.size >= page_size * mmap_window) {
809 unsigned long shift = page_size * (head / page_size);
810 int ret;
811
812 ret = munmap(buf, page_size * mmap_window);
813 assert(ret == 0);
814
815 offset += shift;
816 head -= shift;
817 goto remap;
818 }
819
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200820 size = event->header.size;
821 if (!size)
822 goto broken_event;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300823
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300824 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) {
825 char level;
826 int show = 0;
827 struct dso *dso = NULL;
828 struct thread *thread = threads__findnew(event->ip.pid);
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200829 uint64_t ip = event->ip.ip;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300830
Ingo Molnar97b07b62009-05-26 18:48:58 +0200831 if (dump_trace) {
Ingo Molnarf49515b2009-05-26 19:03:36 +0200832 fprintf(stderr, "%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
833 (void *)(offset + head),
834 (void *)(long)(event->header.size),
Ingo Molnar97b07b62009-05-26 18:48:58 +0200835 event->header.misc,
836 event->ip.pid,
837 (void *)event->ip.ip);
838 }
839
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300840 if (thread == NULL) {
841 fprintf(stderr, "problem processing %d event, bailing out\n",
842 event->header.type);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300843 goto done;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300844 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300845
846 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
847 show = SHOW_KERNEL;
848 level = 'k';
849 dso = kernel_dso;
850 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
851 show = SHOW_USER;
852 level = '.';
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200853 struct map *map = thread__find_map(thread, ip);
854 if (map != NULL) {
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300855 dso = map->dso;
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200856 ip -= map->start + map->pgoff;
857 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300858 } else {
859 show = SHOW_HV;
860 level = 'H';
861 }
862
863 if (show & show_mask) {
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200864 struct symbol *sym = dso__find_symbol(dso, ip);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300865
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200866 if (thread__symbol_incnew(thread, sym, ip, dso, level)) {
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300867 fprintf(stderr, "problem incrementing symbol count, bailing out\n");
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300868 goto done;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300869 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300870 }
871 total++;
872 } else switch (event->header.type) {
873 case PERF_EVENT_MMAP: {
874 struct thread *thread = threads__findnew(event->mmap.pid);
875 struct map *map = map__new(&event->mmap);
876
Ingo Molnar97b07b62009-05-26 18:48:58 +0200877 if (dump_trace) {
Ingo Molnarf49515b2009-05-26 19:03:36 +0200878 fprintf(stderr, "%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n",
879 (void *)(offset + head),
880 (void *)(long)(event->header.size),
Ingo Molnar97b07b62009-05-26 18:48:58 +0200881 (void *)event->mmap.start,
882 (void *)event->mmap.len,
883 (void *)event->mmap.pgoff,
884 event->mmap.filename);
885 }
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300886 if (thread == NULL || map == NULL) {
887 fprintf(stderr, "problem processing PERF_EVENT_MMAP, bailing out\n");
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300888 goto done;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300889 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300890 thread__insert_map(thread, map);
Ingo Molnar97b07b62009-05-26 18:48:58 +0200891 total_mmap++;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300892 break;
893 }
894 case PERF_EVENT_COMM: {
895 struct thread *thread = threads__findnew(event->comm.pid);
896
Ingo Molnar97b07b62009-05-26 18:48:58 +0200897 if (dump_trace) {
Ingo Molnarf49515b2009-05-26 19:03:36 +0200898 fprintf(stderr, "%p [%p]: PERF_EVENT_COMM: %s:%d\n",
899 (void *)(offset + head),
900 (void *)(long)(event->header.size),
Ingo Molnar97b07b62009-05-26 18:48:58 +0200901 event->comm.comm, event->comm.pid);
902 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300903 if (thread == NULL ||
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300904 thread__set_comm(thread, event->comm.comm)) {
905 fprintf(stderr, "problem processing PERF_EVENT_COMM, bailing out\n");
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300906 goto done;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300907 }
Ingo Molnar97b07b62009-05-26 18:48:58 +0200908 total_comm++;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300909 break;
910 }
Ingo Molnar97b07b62009-05-26 18:48:58 +0200911 default: {
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200912broken_event:
Ingo Molnarf49515b2009-05-26 19:03:36 +0200913 fprintf(stderr, "%p [%p]: skipping unknown header type: %d\n",
914 (void *)(offset + head),
915 (void *)(long)(event->header.size),
Ingo Molnar97b07b62009-05-26 18:48:58 +0200916 event->header.type);
Ingo Molnar3e706112009-05-26 18:53:17 +0200917 total_unknown++;
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200918
919 /*
920 * assume we lost track of the stream, check alignment, and
921 * increment a single u64 in the hope to catch on again 'soon'.
922 */
923
924 if (unlikely(head & 7))
925 head &= ~7ULL;
926
927 size = 8;
Ingo Molnar97b07b62009-05-26 18:48:58 +0200928 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300929 }
930
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200931 head += size;
Ingo Molnarf49515b2009-05-26 19:03:36 +0200932
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300933 if (offset + head < stat.st_size)
934 goto more;
935
936 rc = EXIT_SUCCESS;
937done:
938 close(input);
Ingo Molnar97b07b62009-05-26 18:48:58 +0200939
940 if (dump_trace) {
Ingo Molnar3e706112009-05-26 18:53:17 +0200941 fprintf(stderr, " IP events: %10ld\n", total);
942 fprintf(stderr, " mmap events: %10ld\n", total_mmap);
943 fprintf(stderr, " comm events: %10ld\n", total_comm);
944 fprintf(stderr, " unknown events: %10ld\n", total_unknown);
Ingo Molnar97b07b62009-05-26 18:48:58 +0200945
946 return 0;
947 }
948
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300949 threads__sort_symhists();
950 threads__symhists_fprintf(total, stdout);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300951
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300952 return rc;
953}
954
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200955static const char * const report_usage[] = {
956 "perf report [<options>] <command>",
957 NULL
958};
959
960static const struct option options[] = {
961 OPT_STRING('i', "input", &input_name, "file",
962 "input file name"),
Ingo Molnar97b07b62009-05-26 18:48:58 +0200963 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
964 "dump raw trace in ASCII"),
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200965 OPT_END()
966};
967
968int cmd_report(int argc, const char **argv, const char *prefix)
969{
970 elf_version(EV_CURRENT);
971
972 page_size = getpagesize();
973
974 parse_options(argc, argv, options, report_usage, 0);
975
976 return __cmd_report();
977}