blob: 7f1255dcd2223910d379ae70dfdac4cef888fe59 [file] [log] [blame]
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001#include "util/util.h"
Ingo Molnar16f762a2009-05-27 09:10:38 +02002#include "builtin.h"
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02003
4#include <libelf.h>
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -03005#include <gelf.h>
6#include <elf.h>
Arnaldo Carvalho de Melo03f63162009-05-26 19:21:55 -03007#include <ctype.h>
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03008
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -03009#include "util/list.h"
10#include "util/rbtree.h"
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030011
Ingo Molnar53cb8bc2009-05-26 09:17:18 +020012#include "perf.h"
13
14#include "util/parse-options.h"
15#include "util/parse-events.h"
16
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030017#define SHOW_KERNEL 1
18#define SHOW_USER 2
19#define SHOW_HV 4
20
Ingo Molnar53cb8bc2009-05-26 09:17:18 +020021static char const *input_name = "output.perf";
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030022static int input;
23static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
24
Ingo Molnar97b07b62009-05-26 18:48:58 +020025static int dump_trace = 0;
Ingo Molnar16f762a2009-05-27 09:10:38 +020026static int verbose;
Ingo Molnar97b07b62009-05-26 18:48:58 +020027
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030028static unsigned long page_size;
29static unsigned long mmap_window = 32;
30
Ingo Molnar53cb8bc2009-05-26 09:17:18 +020031const char *perf_event_names[] = {
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030032 [PERF_EVENT_MMAP] = " PERF_EVENT_MMAP",
33 [PERF_EVENT_MUNMAP] = " PERF_EVENT_MUNMAP",
34 [PERF_EVENT_COMM] = " PERF_EVENT_COMM",
35};
36
37struct ip_event {
38 struct perf_event_header header;
39 __u64 ip;
40 __u32 pid, tid;
41};
42struct mmap_event {
43 struct perf_event_header header;
44 __u32 pid, tid;
45 __u64 start;
46 __u64 len;
47 __u64 pgoff;
48 char filename[PATH_MAX];
49};
50struct comm_event {
51 struct perf_event_header header;
52 __u32 pid,tid;
53 char comm[16];
54};
55
56typedef union event_union {
57 struct perf_event_header header;
58 struct ip_event ip;
59 struct mmap_event mmap;
60 struct comm_event comm;
61} event_t;
62
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030063struct symbol {
Ingo Molnar16f762a2009-05-27 09:10:38 +020064 struct rb_node rb_node;
65 __u64 start;
66 __u64 end;
67 char name[0];
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030068};
69
70static struct symbol *symbol__new(uint64_t start, uint64_t len, const char *name)
71{
72 struct symbol *self = malloc(sizeof(*self) + strlen(name) + 1);
73
74 if (self != NULL) {
75 self->start = start;
76 self->end = start + len;
77 strcpy(self->name, name);
78 }
79
80 return self;
81}
82
83static void symbol__delete(struct symbol *self)
84{
85 free(self);
86}
87
88static size_t symbol__fprintf(struct symbol *self, FILE *fp)
89{
Ingo Molnar16f762a2009-05-27 09:10:38 +020090 return fprintf(fp, " %llx-%llx %s\n",
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030091 self->start, self->end, self->name);
92}
93
94struct dso {
95 struct list_head node;
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -030096 struct rb_root syms;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030097 char name[0];
98};
99
100static struct dso *dso__new(const char *name)
101{
102 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
103
104 if (self != NULL) {
105 strcpy(self->name, name);
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300106 self->syms = RB_ROOT;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300107 }
108
109 return self;
110}
111
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300112static void dso__delete_symbols(struct dso *self)
113{
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300114 struct symbol *pos;
115 struct rb_node *next = rb_first(&self->syms);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300116
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300117 while (next) {
118 pos = rb_entry(next, struct symbol, rb_node);
119 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300120 symbol__delete(pos);
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300121 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300122}
123
124static void dso__delete(struct dso *self)
125{
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300126 dso__delete_symbols(self);
127 free(self);
128}
129
130static void dso__insert_symbol(struct dso *self, struct symbol *sym)
131{
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300132 struct rb_node **p = &self->syms.rb_node;
133 struct rb_node *parent = NULL;
134 const uint64_t ip = sym->start;
135 struct symbol *s;
136
137 while (*p != NULL) {
138 parent = *p;
139 s = rb_entry(parent, struct symbol, rb_node);
140 if (ip < s->start)
141 p = &(*p)->rb_left;
142 else
143 p = &(*p)->rb_right;
144 }
145 rb_link_node(&sym->rb_node, parent, p);
146 rb_insert_color(&sym->rb_node, &self->syms);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300147}
148
149static struct symbol *dso__find_symbol(struct dso *self, uint64_t ip)
150{
Ingo Molnar16f762a2009-05-27 09:10:38 +0200151 struct rb_node *n;
152
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300153 if (self == NULL)
154 return NULL;
155
Ingo Molnar16f762a2009-05-27 09:10:38 +0200156 n = self->syms.rb_node;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300157
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300158 while (n) {
159 struct symbol *s = rb_entry(n, struct symbol, rb_node);
160
161 if (ip < s->start)
162 n = n->rb_left;
163 else if (ip > s->end)
164 n = n->rb_right;
165 else
166 return s;
167 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300168
169 return NULL;
170}
171
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300172/**
173 * elf_symtab__for_each_symbol - iterate thru all the symbols
174 *
175 * @self: struct elf_symtab instance to iterate
176 * @index: uint32_t index
177 * @sym: GElf_Sym iterator
178 */
179#define elf_symtab__for_each_symbol(syms, nr_syms, index, sym) \
180 for (index = 0, gelf_getsym(syms, index, &sym);\
181 index < nr_syms; \
182 index++, gelf_getsym(syms, index, &sym))
183
184static inline uint8_t elf_sym__type(const GElf_Sym *sym)
185{
186 return GELF_ST_TYPE(sym->st_info);
187}
188
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200189static inline int elf_sym__is_function(const GElf_Sym *sym)
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300190{
191 return elf_sym__type(sym) == STT_FUNC &&
192 sym->st_name != 0 &&
193 sym->st_shndx != SHN_UNDEF;
194}
195
196static inline const char *elf_sym__name(const GElf_Sym *sym,
197 const Elf_Data *symstrs)
198{
199 return symstrs->d_buf + sym->st_name;
200}
201
202static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
203 GElf_Shdr *shp, const char *name,
204 size_t *index)
205{
206 Elf_Scn *sec = NULL;
207 size_t cnt = 1;
208
209 while ((sec = elf_nextscn(elf, sec)) != NULL) {
210 char *str;
211
212 gelf_getshdr(sec, shp);
213 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
214 if (!strcmp(name, str)) {
215 if (index)
216 *index = cnt;
217 break;
218 }
219 ++cnt;
220 }
221
222 return sec;
223}
224
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300225static int dso__load(struct dso *self)
226{
Ingo Molnar16f762a2009-05-27 09:10:38 +0200227 Elf_Data *symstrs;
228 uint32_t nr_syms;
229 int fd, err = -1;
230 uint32_t index;
231 GElf_Ehdr ehdr;
232 GElf_Shdr shdr;
233 Elf_Data *syms;
234 GElf_Sym sym;
235 Elf_Scn *sec;
236 Elf *elf;
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300237
Ingo Molnar16f762a2009-05-27 09:10:38 +0200238
239 fd = open(self->name, O_RDONLY);
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300240 if (fd == -1)
241 return -1;
242
Ingo Molnar16f762a2009-05-27 09:10:38 +0200243 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300244 if (elf == NULL) {
245 fprintf(stderr, "%s: cannot read %s ELF file.\n",
246 __func__, self->name);
247 goto out_close;
248 }
249
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300250 if (gelf_getehdr(elf, &ehdr) == NULL) {
251 fprintf(stderr, "%s: cannot get elf header.\n", __func__);
252 goto out_elf_end;
253 }
254
Ingo Molnar16f762a2009-05-27 09:10:38 +0200255 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300256 if (sec == NULL)
257 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
258
259 if (sec == NULL)
260 goto out_elf_end;
261
Ingo Molnar16f762a2009-05-27 09:10:38 +0200262 syms = elf_getdata(sec, NULL);
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300263 if (syms == NULL)
264 goto out_elf_end;
265
266 sec = elf_getscn(elf, shdr.sh_link);
267 if (sec == NULL)
268 goto out_elf_end;
269
Ingo Molnar16f762a2009-05-27 09:10:38 +0200270 symstrs = elf_getdata(sec, NULL);
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300271 if (symstrs == NULL)
272 goto out_elf_end;
273
Ingo Molnar16f762a2009-05-27 09:10:38 +0200274 nr_syms = shdr.sh_size / shdr.sh_entsize;
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300275
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300276 elf_symtab__for_each_symbol(syms, nr_syms, index, sym) {
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200277 struct symbol *f;
278
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300279 if (!elf_sym__is_function(&sym))
280 continue;
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200281
282 sec = elf_getscn(elf, sym.st_shndx);
283 if (!sec)
284 goto out_elf_end;
285
286 gelf_getshdr(sec, &shdr);
287 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
288
289 f = symbol__new(sym.st_value, sym.st_size,
290 elf_sym__name(&sym, symstrs));
291 if (!f)
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300292 goto out_elf_end;
293
294 dso__insert_symbol(self, f);
295 }
296
297 err = 0;
298out_elf_end:
299 elf_end(elf);
300out_close:
301 close(fd);
302 return err;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300303}
304
305static size_t dso__fprintf(struct dso *self, FILE *fp)
306{
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300307 size_t ret = fprintf(fp, "dso: %s\n", self->name);
308
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300309 struct rb_node *nd;
310 for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
311 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300312 ret += symbol__fprintf(pos, fp);
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300313 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300314
315 return ret;
316}
317
318static LIST_HEAD(dsos);
319static struct dso *kernel_dso;
320
321static void dsos__add(struct dso *dso)
322{
323 list_add_tail(&dso->node, &dsos);
324}
325
326static struct dso *dsos__find(const char *name)
327{
328 struct dso *pos;
329
330 list_for_each_entry(pos, &dsos, node)
331 if (strcmp(pos->name, name) == 0)
332 return pos;
333 return NULL;
334}
335
336static struct dso *dsos__findnew(const char *name)
337{
338 struct dso *dso = dsos__find(name);
339
340 if (dso == NULL) {
341 dso = dso__new(name);
342 if (dso != NULL && dso__load(dso) < 0)
343 goto out_delete_dso;
344
345 dsos__add(dso);
346 }
347
348 return dso;
349
350out_delete_dso:
351 dso__delete(dso);
352 return NULL;
353}
354
Ingo Molnar16f762a2009-05-27 09:10:38 +0200355static void dsos__fprintf(FILE *fp)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300356{
357 struct dso *pos;
358
359 list_for_each_entry(pos, &dsos, node)
360 dso__fprintf(pos, fp);
361}
362
Arnaldo Carvalho de Melod8d16562009-05-26 19:20:57 -0300363static int hex(char ch)
364{
365 if ((ch >= '0') && (ch <= '9'))
366 return ch - '0';
367 if ((ch >= 'a') && (ch <= 'f'))
368 return ch - 'a' + 10;
369 if ((ch >= 'A') && (ch <= 'F'))
370 return ch - 'A' + 10;
371 return -1;
372}
373
374/*
375 * While we find nice hex chars, build a long_val.
376 * Return number of chars processed.
377 */
Ingo Molnar16f762a2009-05-27 09:10:38 +0200378static int hex2long(char *ptr, unsigned long *long_val)
Arnaldo Carvalho de Melod8d16562009-05-26 19:20:57 -0300379{
380 const char *p = ptr;
381 *long_val = 0;
382
383 while (*p) {
384 const int hex_val = hex(*p);
385
386 if (hex_val < 0)
387 break;
388
389 *long_val = (*long_val << 4) | hex_val;
390 p++;
391 }
392
393 return p - ptr;
394}
395
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300396static int load_kallsyms(void)
397{
Ingo Molnaraf836322009-05-27 08:38:48 +0200398 struct rb_node *nd, *prevnd;
399 char *line = NULL;
400 FILE *file;
401 size_t n;
402
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300403 kernel_dso = dso__new("[kernel]");
404 if (kernel_dso == NULL)
405 return -1;
406
Ingo Molnaraf836322009-05-27 08:38:48 +0200407 file = fopen("/proc/kallsyms", "r");
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300408 if (file == NULL)
409 goto out_delete_dso;
410
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300411 while (!feof(file)) {
Arnaldo Carvalho de Melod8d16562009-05-26 19:20:57 -0300412 unsigned long start;
Ingo Molnaraf836322009-05-27 08:38:48 +0200413 struct symbol *sym;
414 int line_len, len;
415 char symbol_type;
416
417 line_len = getline(&line, &n, file);
Arnaldo Carvalho de Melod8d16562009-05-26 19:20:57 -0300418 if (line_len < 0)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300419 break;
420
421 if (!line)
422 goto out_delete_dso;
423
Arnaldo Carvalho de Melod8d16562009-05-26 19:20:57 -0300424 line[--line_len] = '\0'; /* \n */
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300425
Ingo Molnaraf836322009-05-27 08:38:48 +0200426 len = hex2long(line, &start);
427
Arnaldo Carvalho de Melo03f63162009-05-26 19:21:55 -0300428 len++;
429 if (len + 2 >= line_len)
430 continue;
431
Ingo Molnaraf836322009-05-27 08:38:48 +0200432 symbol_type = toupper(line[len]);
Arnaldo Carvalho de Melo03f63162009-05-26 19:21:55 -0300433 /*
434 * We're interested only in code ('T'ext)
435 */
Ingo Molnaraf836322009-05-27 08:38:48 +0200436 if (symbol_type != 'T' && symbol_type != 'W')
Arnaldo Carvalho de Melod8d16562009-05-26 19:20:57 -0300437 continue;
438 /*
439 * Well fix up the end later, when we have all sorted.
440 */
Ingo Molnaraf836322009-05-27 08:38:48 +0200441 sym = symbol__new(start, 0xdead, line + len + 2);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300442
Arnaldo Carvalho de Melod8d16562009-05-26 19:20:57 -0300443 if (sym == NULL)
444 goto out_delete_dso;
445
446 dso__insert_symbol(kernel_dso, sym);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300447 }
448
Arnaldo Carvalho de Meloabd54f62009-05-26 12:21:34 -0300449 /*
450 * Now that we have all sorted out, just set the ->end of all
451 * symbols
452 */
Ingo Molnaraf836322009-05-27 08:38:48 +0200453 prevnd = rb_first(&kernel_dso->syms);
Arnaldo Carvalho de Meloabd54f62009-05-26 12:21:34 -0300454
455 if (prevnd == NULL)
456 goto out_delete_line;
457
458 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
459 struct symbol *prev = rb_entry(prevnd, struct symbol, rb_node),
460 *curr = rb_entry(nd, struct symbol, rb_node);
461
462 prev->end = curr->start - 1;
463 prevnd = nd;
464 }
465
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300466 dsos__add(kernel_dso);
467 free(line);
468 fclose(file);
Ingo Molnaraf836322009-05-27 08:38:48 +0200469
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300470 return 0;
471
Arnaldo Carvalho de Melo59d81022009-05-26 11:14:27 -0300472out_delete_line:
473 free(line);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300474out_delete_dso:
475 dso__delete(kernel_dso);
476 return -1;
477}
478
479struct map {
480 struct list_head node;
481 uint64_t start;
482 uint64_t end;
483 uint64_t pgoff;
484 struct dso *dso;
485};
486
487static struct map *map__new(struct mmap_event *event)
488{
489 struct map *self = malloc(sizeof(*self));
490
491 if (self != NULL) {
492 self->start = event->start;
493 self->end = event->start + event->len;
494 self->pgoff = event->pgoff;
495
496 self->dso = dsos__findnew(event->filename);
497 if (self->dso == NULL)
498 goto out_delete;
499 }
500 return self;
501out_delete:
502 free(self);
503 return NULL;
504}
505
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300506struct thread;
507
508static const char *thread__name(struct thread *self, char *bf, size_t size);
509
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300510struct symhist {
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300511 struct rb_node rb_node;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300512 struct dso *dso;
513 struct symbol *sym;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300514 struct thread *thread;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300515 uint64_t ip;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300516 uint32_t count;
517 char level;
518};
519
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300520static struct symhist *symhist__new(struct symbol *sym, uint64_t ip,
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300521 struct thread *thread, struct dso *dso,
522 char level)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300523{
524 struct symhist *self = malloc(sizeof(*self));
525
526 if (self != NULL) {
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300527 self->sym = sym;
528 self->thread = thread;
529 self->ip = ip;
530 self->dso = dso;
531 self->level = level;
532 self->count = 1;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300533 }
534
535 return 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
Arnaldo Carvalho de Melo815e7772009-05-26 19:46:14 -0300554 ret += fprintf(fp, "%14s [%c] ",
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300555 thread__name(self->thread, bf, sizeof(bf)),
Arnaldo Carvalho de Melo815e7772009-05-26 19:46:14 -0300556 self->level);
557
558 if (verbose)
559 ret += fprintf(fp, "%#018llx ", (unsigned long long)self->ip);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300560
561 if (self->level != '.')
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300562 ret += fprintf(fp, "%s\n",
563 self->sym ? self->sym->name : "<unknown>");
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300564 else
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300565 ret += fprintf(fp, "%s: %s\n",
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200566 self->dso ? self->dso->name : "<unknown>",
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300567 self->sym ? self->sym->name : "<unknown>");
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300568 return ret;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300569}
570
571struct thread {
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300572 struct rb_node rb_node;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300573 struct list_head maps;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300574 struct rb_root symhists;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300575 pid_t pid;
576 char *comm;
577};
578
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300579static const char *thread__name(struct thread *self, char *bf, size_t size)
580{
581 if (self->comm)
582 return self->comm;
583
584 snprintf(bf, sizeof(bf), ":%u", self->pid);
585 return bf;
586}
587
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300588static struct thread *thread__new(pid_t pid)
589{
590 struct thread *self = malloc(sizeof(*self));
591
592 if (self != NULL) {
593 self->pid = pid;
594 self->comm = NULL;
595 INIT_LIST_HEAD(&self->maps);
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300596 self->symhists = RB_ROOT;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300597 }
598
599 return self;
600}
601
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300602static int thread__symbol_incnew(struct thread *self, struct symbol *sym,
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300603 uint64_t ip, struct dso *dso, char level)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300604{
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300605 struct rb_node **p = &self->symhists.rb_node;
606 struct rb_node *parent = NULL;
607 struct symhist *sh;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300608
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300609 while (*p != NULL) {
Ingo Molnar16f762a2009-05-27 09:10:38 +0200610 uint64_t start;
611
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300612 parent = *p;
613 sh = rb_entry(parent, struct symhist, rb_node);
614
615 if (sh->sym == sym || ip == sh->ip) {
616 symhist__inc(sh);
617 return 0;
618 }
619
620 /* Handle unresolved symbols too */
Ingo Molnar16f762a2009-05-27 09:10:38 +0200621 start = !sh->sym ? sh->ip : sh->sym->start;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300622
623 if (ip < start)
624 p = &(*p)->rb_left;
625 else
626 p = &(*p)->rb_right;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300627 }
628
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300629 sh = symhist__new(sym, ip, self, dso, level);
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300630 if (sh == NULL)
631 return -ENOMEM;
632 rb_link_node(&sh->rb_node, parent, p);
633 rb_insert_color(&sh->rb_node, &self->symhists);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300634 return 0;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300635}
636
637static int thread__set_comm(struct thread *self, const char *comm)
638{
639 self->comm = strdup(comm);
640 return self->comm ? 0 : -ENOMEM;
641}
642
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300643static size_t thread__fprintf(struct thread *self, FILE *fp)
644{
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300645 int ret = fprintf(fp, "thread: %d %s\n", self->pid, self->comm);
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300646 struct rb_node *nd;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300647
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300648 for (nd = rb_first(&self->symhists); nd; nd = rb_next(nd)) {
649 struct symhist *pos = rb_entry(nd, struct symhist, rb_node);
Ingo Molnar16f762a2009-05-27 09:10:38 +0200650
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300651 ret += symhist__fprintf(pos, 0, fp);
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300652 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300653
654 return ret;
655}
656
Ingo Molnar16f762a2009-05-27 09:10:38 +0200657static struct rb_root threads;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300658
659static struct thread *threads__findnew(pid_t pid)
660{
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300661 struct rb_node **p = &threads.rb_node;
662 struct rb_node *parent = NULL;
663 struct thread *th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300664
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300665 while (*p != NULL) {
666 parent = *p;
667 th = rb_entry(parent, struct thread, rb_node);
668
669 if (th->pid == pid)
670 return th;
671
672 if (pid < th->pid)
673 p = &(*p)->rb_left;
674 else
675 p = &(*p)->rb_right;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300676 }
677
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300678 th = thread__new(pid);
679 if (th != NULL) {
680 rb_link_node(&th->rb_node, parent, p);
681 rb_insert_color(&th->rb_node, &threads);
682 }
683 return th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300684}
685
686static void thread__insert_map(struct thread *self, struct map *map)
687{
688 list_add_tail(&map->node, &self->maps);
689}
690
691static struct map *thread__find_map(struct thread *self, uint64_t ip)
692{
Ingo Molnar16f762a2009-05-27 09:10:38 +0200693 struct map *pos;
694
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300695 if (self == NULL)
696 return NULL;
697
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300698 list_for_each_entry(pos, &self->maps, node)
699 if (ip >= pos->start && ip <= pos->end)
700 return pos;
701
702 return NULL;
703}
704
Ingo Molnar16f762a2009-05-27 09:10:38 +0200705static void threads__fprintf(FILE *fp)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300706{
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300707 struct rb_node *nd;
708 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
709 struct thread *pos = rb_entry(nd, struct thread, rb_node);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300710 thread__fprintf(pos, fp);
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300711 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300712}
713
Ingo Molnar16f762a2009-05-27 09:10:38 +0200714static struct rb_root global_symhists;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300715
716static void threads__insert_symhist(struct symhist *sh)
717{
718 struct rb_node **p = &global_symhists.rb_node;
719 struct rb_node *parent = NULL;
720 struct symhist *iter;
721
722 while (*p != NULL) {
723 parent = *p;
724 iter = rb_entry(parent, struct symhist, rb_node);
725
726 /* Reverse order */
727 if (sh->count > iter->count)
728 p = &(*p)->rb_left;
729 else
730 p = &(*p)->rb_right;
731 }
732
733 rb_link_node(&sh->rb_node, parent, p);
734 rb_insert_color(&sh->rb_node, &global_symhists);
735}
736
737static void threads__sort_symhists(void)
738{
739 struct rb_node *nd;
740
741 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
742 struct thread *thread = rb_entry(nd, struct thread, rb_node);
743 struct rb_node *next = rb_first(&thread->symhists);
744
745 while (next) {
746 struct symhist *n = rb_entry(next, struct symhist,
747 rb_node);
748 next = rb_next(&n->rb_node);
749 rb_erase(&n->rb_node, &thread->symhists);
750 threads__insert_symhist(n);
751 }
752
753 }
754}
755
756static size_t threads__symhists_fprintf(uint64_t total_samples, FILE *fp)
757{
758 struct rb_node *nd;
759 size_t ret = 0;
760
761 for (nd = rb_first(&global_symhists); nd; nd = rb_next(nd)) {
762 struct symhist *pos = rb_entry(nd, struct symhist, rb_node);
763 ret += symhist__fprintf(pos, total_samples, fp);
764 }
765
766 return ret;
767}
768
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200769static int __cmd_report(void)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300770{
771 unsigned long offset = 0;
772 unsigned long head = 0;
773 struct stat stat;
774 char *buf;
775 event_t *event;
776 int ret, rc = EXIT_FAILURE;
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200777 uint32_t size;
Ingo Molnarf49515b2009-05-26 19:03:36 +0200778 unsigned long total = 0, total_mmap = 0, total_comm = 0, total_unknown = 0;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300779
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300780 input = open(input_name, O_RDONLY);
781 if (input < 0) {
782 perror("failed to open file");
783 exit(-1);
784 }
785
786 ret = fstat(input, &stat);
787 if (ret < 0) {
788 perror("failed to stat file");
789 exit(-1);
790 }
791
792 if (!stat.st_size) {
793 fprintf(stderr, "zero-sized file, nothing to do!\n");
794 exit(0);
795 }
796
797 if (load_kallsyms() < 0) {
798 perror("failed to open kallsyms");
799 return EXIT_FAILURE;
800 }
801
802remap:
803 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
804 MAP_SHARED, input, offset);
805 if (buf == MAP_FAILED) {
806 perror("failed to mmap file");
807 exit(-1);
808 }
809
810more:
811 event = (event_t *)(buf + head);
812
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200813 size = event->header.size;
814 if (!size)
815 size = 8;
816
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300817 if (head + event->header.size >= page_size * mmap_window) {
818 unsigned long shift = page_size * (head / page_size);
819 int ret;
820
821 ret = munmap(buf, page_size * mmap_window);
822 assert(ret == 0);
823
824 offset += shift;
825 head -= shift;
826 goto remap;
827 }
828
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200829 size = event->header.size;
830 if (!size)
831 goto broken_event;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300832
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300833 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) {
834 char level;
835 int show = 0;
836 struct dso *dso = NULL;
837 struct thread *thread = threads__findnew(event->ip.pid);
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200838 uint64_t ip = event->ip.ip;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300839
Ingo Molnar97b07b62009-05-26 18:48:58 +0200840 if (dump_trace) {
Ingo Molnarf49515b2009-05-26 19:03:36 +0200841 fprintf(stderr, "%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
842 (void *)(offset + head),
843 (void *)(long)(event->header.size),
Ingo Molnar97b07b62009-05-26 18:48:58 +0200844 event->header.misc,
845 event->ip.pid,
Ingo Molnar16f762a2009-05-27 09:10:38 +0200846 (void *)(long)ip);
Ingo Molnar97b07b62009-05-26 18:48:58 +0200847 }
848
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300849 if (thread == NULL) {
850 fprintf(stderr, "problem processing %d event, bailing out\n",
851 event->header.type);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300852 goto done;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300853 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300854
855 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
856 show = SHOW_KERNEL;
857 level = 'k';
858 dso = kernel_dso;
859 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
Ingo Molnar16f762a2009-05-27 09:10:38 +0200860 struct map *map;
861
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300862 show = SHOW_USER;
863 level = '.';
Ingo Molnar16f762a2009-05-27 09:10:38 +0200864
865 map = thread__find_map(thread, ip);
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200866 if (map != NULL) {
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300867 dso = map->dso;
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200868 ip -= map->start + map->pgoff;
869 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300870 } else {
871 show = SHOW_HV;
872 level = 'H';
873 }
874
875 if (show & show_mask) {
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200876 struct symbol *sym = dso__find_symbol(dso, ip);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300877
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200878 if (thread__symbol_incnew(thread, sym, ip, dso, level)) {
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300879 fprintf(stderr, "problem incrementing symbol count, bailing out\n");
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300880 goto done;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300881 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300882 }
883 total++;
884 } else switch (event->header.type) {
885 case PERF_EVENT_MMAP: {
886 struct thread *thread = threads__findnew(event->mmap.pid);
887 struct map *map = map__new(&event->mmap);
888
Ingo Molnar97b07b62009-05-26 18:48:58 +0200889 if (dump_trace) {
Ingo Molnarf49515b2009-05-26 19:03:36 +0200890 fprintf(stderr, "%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n",
891 (void *)(offset + head),
892 (void *)(long)(event->header.size),
Ingo Molnar16f762a2009-05-27 09:10:38 +0200893 (void *)(long)event->mmap.start,
894 (void *)(long)event->mmap.len,
895 (void *)(long)event->mmap.pgoff,
Ingo Molnar97b07b62009-05-26 18:48:58 +0200896 event->mmap.filename);
897 }
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300898 if (thread == NULL || map == NULL) {
899 fprintf(stderr, "problem processing PERF_EVENT_MMAP, bailing out\n");
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300900 goto done;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300901 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300902 thread__insert_map(thread, map);
Ingo Molnar97b07b62009-05-26 18:48:58 +0200903 total_mmap++;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300904 break;
905 }
906 case PERF_EVENT_COMM: {
907 struct thread *thread = threads__findnew(event->comm.pid);
908
Ingo Molnar97b07b62009-05-26 18:48:58 +0200909 if (dump_trace) {
Ingo Molnarf49515b2009-05-26 19:03:36 +0200910 fprintf(stderr, "%p [%p]: PERF_EVENT_COMM: %s:%d\n",
911 (void *)(offset + head),
912 (void *)(long)(event->header.size),
Ingo Molnar97b07b62009-05-26 18:48:58 +0200913 event->comm.comm, event->comm.pid);
914 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300915 if (thread == NULL ||
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300916 thread__set_comm(thread, event->comm.comm)) {
917 fprintf(stderr, "problem processing PERF_EVENT_COMM, bailing out\n");
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300918 goto done;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300919 }
Ingo Molnar97b07b62009-05-26 18:48:58 +0200920 total_comm++;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300921 break;
922 }
Ingo Molnar97b07b62009-05-26 18:48:58 +0200923 default: {
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200924broken_event:
Ingo Molnarf49515b2009-05-26 19:03:36 +0200925 fprintf(stderr, "%p [%p]: skipping unknown header type: %d\n",
926 (void *)(offset + head),
927 (void *)(long)(event->header.size),
Ingo Molnar97b07b62009-05-26 18:48:58 +0200928 event->header.type);
Ingo Molnar3e706112009-05-26 18:53:17 +0200929 total_unknown++;
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200930
931 /*
932 * assume we lost track of the stream, check alignment, and
933 * increment a single u64 in the hope to catch on again 'soon'.
934 */
935
936 if (unlikely(head & 7))
937 head &= ~7ULL;
938
939 size = 8;
Ingo Molnar97b07b62009-05-26 18:48:58 +0200940 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300941 }
942
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200943 head += size;
Ingo Molnarf49515b2009-05-26 19:03:36 +0200944
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300945 if (offset + head < stat.st_size)
946 goto more;
947
948 rc = EXIT_SUCCESS;
949done:
950 close(input);
Ingo Molnar97b07b62009-05-26 18:48:58 +0200951
952 if (dump_trace) {
Ingo Molnar3e706112009-05-26 18:53:17 +0200953 fprintf(stderr, " IP events: %10ld\n", total);
954 fprintf(stderr, " mmap events: %10ld\n", total_mmap);
955 fprintf(stderr, " comm events: %10ld\n", total_comm);
956 fprintf(stderr, " unknown events: %10ld\n", total_unknown);
Ingo Molnar97b07b62009-05-26 18:48:58 +0200957
958 return 0;
959 }
960
Ingo Molnar16f762a2009-05-27 09:10:38 +0200961 if (verbose >= 2) {
962 dsos__fprintf(stdout);
963 threads__fprintf(stdout);
964 }
965
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300966 threads__sort_symhists();
967 threads__symhists_fprintf(total, stdout);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300968
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300969 return rc;
970}
971
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200972static const char * const report_usage[] = {
973 "perf report [<options>] <command>",
974 NULL
975};
976
977static const struct option options[] = {
978 OPT_STRING('i', "input", &input_name, "file",
979 "input file name"),
Arnaldo Carvalho de Melo815e7772009-05-26 19:46:14 -0300980 OPT_BOOLEAN('v', "verbose", &verbose,
981 "be more verbose (show symbol address, etc)"),
Ingo Molnar97b07b62009-05-26 18:48:58 +0200982 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
983 "dump raw trace in ASCII"),
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200984 OPT_END()
985};
986
987int cmd_report(int argc, const char **argv, const char *prefix)
988{
989 elf_version(EV_CURRENT);
990
991 page_size = getpagesize();
992
993 parse_options(argc, argv, options, report_usage, 0);
994
995 return __cmd_report();
996}