Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1 | #include "util/util.h" |
| 2 | |
| 3 | #include <libelf.h> |
Arnaldo Carvalho de Melo | 62eb939 | 2009-05-18 14:28:47 -0300 | [diff] [blame] | 4 | #include <gelf.h> |
| 5 | #include <elf.h> |
Arnaldo Carvalho de Melo | 03f6316 | 2009-05-26 19:21:55 -0300 | [diff] [blame^] | 6 | #include <ctype.h> |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 7 | |
Arnaldo Carvalho de Melo | 35a50c8 | 2009-05-18 16:24:49 -0300 | [diff] [blame] | 8 | #include "util/list.h" |
| 9 | #include "util/rbtree.h" |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 10 | |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 11 | #include "perf.h" |
| 12 | |
| 13 | #include "util/parse-options.h" |
| 14 | #include "util/parse-events.h" |
| 15 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 16 | #define SHOW_KERNEL 1 |
| 17 | #define SHOW_USER 2 |
| 18 | #define SHOW_HV 4 |
| 19 | |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 20 | static char const *input_name = "output.perf"; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 21 | static int input; |
| 22 | static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV; |
| 23 | |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 24 | static int dump_trace = 0; |
| 25 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 26 | static unsigned long page_size; |
| 27 | static unsigned long mmap_window = 32; |
| 28 | |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 29 | const char *perf_event_names[] = { |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 30 | [PERF_EVENT_MMAP] = " PERF_EVENT_MMAP", |
| 31 | [PERF_EVENT_MUNMAP] = " PERF_EVENT_MUNMAP", |
| 32 | [PERF_EVENT_COMM] = " PERF_EVENT_COMM", |
| 33 | }; |
| 34 | |
| 35 | struct ip_event { |
| 36 | struct perf_event_header header; |
| 37 | __u64 ip; |
| 38 | __u32 pid, tid; |
| 39 | }; |
| 40 | struct 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 | }; |
| 48 | struct comm_event { |
| 49 | struct perf_event_header header; |
| 50 | __u32 pid,tid; |
| 51 | char comm[16]; |
| 52 | }; |
| 53 | |
| 54 | typedef 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 Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 61 | struct symbol { |
Arnaldo Carvalho de Melo | 35a50c8 | 2009-05-18 16:24:49 -0300 | [diff] [blame] | 62 | struct rb_node rb_node; |
| 63 | uint64_t start; |
| 64 | uint64_t end; |
| 65 | char name[0]; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | static 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 | |
| 81 | static void symbol__delete(struct symbol *self) |
| 82 | { |
| 83 | free(self); |
| 84 | } |
| 85 | |
| 86 | static 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 | |
| 92 | struct dso { |
| 93 | struct list_head node; |
Arnaldo Carvalho de Melo | 35a50c8 | 2009-05-18 16:24:49 -0300 | [diff] [blame] | 94 | struct rb_root syms; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 95 | char name[0]; |
| 96 | }; |
| 97 | |
| 98 | static 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 Melo | 35a50c8 | 2009-05-18 16:24:49 -0300 | [diff] [blame] | 104 | self->syms = RB_ROOT; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | return self; |
| 108 | } |
| 109 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 110 | static void dso__delete_symbols(struct dso *self) |
| 111 | { |
Arnaldo Carvalho de Melo | 35a50c8 | 2009-05-18 16:24:49 -0300 | [diff] [blame] | 112 | struct symbol *pos; |
| 113 | struct rb_node *next = rb_first(&self->syms); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 114 | |
Arnaldo Carvalho de Melo | 35a50c8 | 2009-05-18 16:24:49 -0300 | [diff] [blame] | 115 | while (next) { |
| 116 | pos = rb_entry(next, struct symbol, rb_node); |
| 117 | next = rb_next(&pos->rb_node); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 118 | symbol__delete(pos); |
Arnaldo Carvalho de Melo | 35a50c8 | 2009-05-18 16:24:49 -0300 | [diff] [blame] | 119 | } |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | static void dso__delete(struct dso *self) |
| 123 | { |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 124 | dso__delete_symbols(self); |
| 125 | free(self); |
| 126 | } |
| 127 | |
| 128 | static void dso__insert_symbol(struct dso *self, struct symbol *sym) |
| 129 | { |
Arnaldo Carvalho de Melo | 35a50c8 | 2009-05-18 16:24:49 -0300 | [diff] [blame] | 130 | 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 Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | static struct symbol *dso__find_symbol(struct dso *self, uint64_t ip) |
| 148 | { |
| 149 | if (self == NULL) |
| 150 | return NULL; |
| 151 | |
Arnaldo Carvalho de Melo | 35a50c8 | 2009-05-18 16:24:49 -0300 | [diff] [blame] | 152 | struct rb_node *n = self->syms.rb_node; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 153 | |
Arnaldo Carvalho de Melo | 35a50c8 | 2009-05-18 16:24:49 -0300 | [diff] [blame] | 154 | 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 Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 164 | |
| 165 | return NULL; |
| 166 | } |
| 167 | |
Arnaldo Carvalho de Melo | 62eb939 | 2009-05-18 14:28:47 -0300 | [diff] [blame] | 168 | /** |
| 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 | |
| 180 | static inline uint8_t elf_sym__type(const GElf_Sym *sym) |
| 181 | { |
| 182 | return GELF_ST_TYPE(sym->st_info); |
| 183 | } |
| 184 | |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 185 | static inline int elf_sym__is_function(const GElf_Sym *sym) |
Arnaldo Carvalho de Melo | 62eb939 | 2009-05-18 14:28:47 -0300 | [diff] [blame] | 186 | { |
| 187 | return elf_sym__type(sym) == STT_FUNC && |
| 188 | sym->st_name != 0 && |
| 189 | sym->st_shndx != SHN_UNDEF; |
| 190 | } |
| 191 | |
| 192 | static 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 | |
| 198 | static 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 Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 221 | static int dso__load(struct dso *self) |
| 222 | { |
Arnaldo Carvalho de Melo | 62eb939 | 2009-05-18 14:28:47 -0300 | [diff] [blame] | 223 | 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 Melo | 62eb939 | 2009-05-18 14:28:47 -0300 | [diff] [blame] | 249 | 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 Zijlstra | f17e04a | 2009-05-26 15:30:22 +0200 | [diff] [blame] | 266 | struct symbol *f; |
| 267 | |
Arnaldo Carvalho de Melo | 62eb939 | 2009-05-18 14:28:47 -0300 | [diff] [blame] | 268 | if (!elf_sym__is_function(&sym)) |
| 269 | continue; |
Peter Zijlstra | f17e04a | 2009-05-26 15:30:22 +0200 | [diff] [blame] | 270 | |
| 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 Melo | 62eb939 | 2009-05-18 14:28:47 -0300 | [diff] [blame] | 281 | goto out_elf_end; |
| 282 | |
| 283 | dso__insert_symbol(self, f); |
| 284 | } |
| 285 | |
| 286 | err = 0; |
| 287 | out_elf_end: |
| 288 | elf_end(elf); |
| 289 | out_close: |
| 290 | close(fd); |
| 291 | return err; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | static size_t dso__fprintf(struct dso *self, FILE *fp) |
| 295 | { |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 296 | size_t ret = fprintf(fp, "dso: %s\n", self->name); |
| 297 | |
Arnaldo Carvalho de Melo | 35a50c8 | 2009-05-18 16:24:49 -0300 | [diff] [blame] | 298 | 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 Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 301 | ret += symbol__fprintf(pos, fp); |
Arnaldo Carvalho de Melo | 35a50c8 | 2009-05-18 16:24:49 -0300 | [diff] [blame] | 302 | } |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 303 | |
| 304 | return ret; |
| 305 | } |
| 306 | |
| 307 | static LIST_HEAD(dsos); |
| 308 | static struct dso *kernel_dso; |
| 309 | |
| 310 | static void dsos__add(struct dso *dso) |
| 311 | { |
| 312 | list_add_tail(&dso->node, &dsos); |
| 313 | } |
| 314 | |
| 315 | static 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 | |
| 325 | static 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 | |
| 339 | out_delete_dso: |
| 340 | dso__delete(dso); |
| 341 | return NULL; |
| 342 | } |
| 343 | |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 344 | void dsos__fprintf(FILE *fp) |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 345 | { |
| 346 | struct dso *pos; |
| 347 | |
| 348 | list_for_each_entry(pos, &dsos, node) |
| 349 | dso__fprintf(pos, fp); |
| 350 | } |
| 351 | |
Arnaldo Carvalho de Melo | d8d1656 | 2009-05-26 19:20:57 -0300 | [diff] [blame] | 352 | static 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 | */ |
| 367 | int 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 Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 385 | static int load_kallsyms(void) |
| 386 | { |
| 387 | kernel_dso = dso__new("[kernel]"); |
| 388 | if (kernel_dso == NULL) |
| 389 | return -1; |
| 390 | |
| 391 | FILE *file = fopen("/proc/kallsyms", "r"); |
| 392 | |
| 393 | if (file == NULL) |
| 394 | goto out_delete_dso; |
| 395 | |
| 396 | char *line = NULL; |
| 397 | size_t n; |
| 398 | |
| 399 | while (!feof(file)) { |
Arnaldo Carvalho de Melo | d8d1656 | 2009-05-26 19:20:57 -0300 | [diff] [blame] | 400 | unsigned long start; |
| 401 | int line_len = getline(&line, &n, file); |
| 402 | if (line_len < 0) |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 403 | break; |
| 404 | |
| 405 | if (!line) |
| 406 | goto out_delete_dso; |
| 407 | |
Arnaldo Carvalho de Melo | d8d1656 | 2009-05-26 19:20:57 -0300 | [diff] [blame] | 408 | line[--line_len] = '\0'; /* \n */ |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 409 | |
Arnaldo Carvalho de Melo | d8d1656 | 2009-05-26 19:20:57 -0300 | [diff] [blame] | 410 | int len = hex2long(line, &start); |
| 411 | |
Arnaldo Carvalho de Melo | 03f6316 | 2009-05-26 19:21:55 -0300 | [diff] [blame^] | 412 | len++; |
| 413 | if (len + 2 >= line_len) |
| 414 | continue; |
| 415 | |
| 416 | char symbol_type = line[len]; |
| 417 | /* |
| 418 | * We're interested only in code ('T'ext) |
| 419 | */ |
| 420 | if (toupper(symbol_type) != 'T') |
Arnaldo Carvalho de Melo | d8d1656 | 2009-05-26 19:20:57 -0300 | [diff] [blame] | 421 | continue; |
| 422 | /* |
| 423 | * Well fix up the end later, when we have all sorted. |
| 424 | */ |
Arnaldo Carvalho de Melo | 03f6316 | 2009-05-26 19:21:55 -0300 | [diff] [blame^] | 425 | struct symbol *sym = symbol__new(start, 0xdead, line + len + 2); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 426 | |
Arnaldo Carvalho de Melo | d8d1656 | 2009-05-26 19:20:57 -0300 | [diff] [blame] | 427 | if (sym == NULL) |
| 428 | goto out_delete_dso; |
| 429 | |
| 430 | dso__insert_symbol(kernel_dso, sym); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 431 | } |
| 432 | |
Arnaldo Carvalho de Melo | abd54f6 | 2009-05-26 12:21:34 -0300 | [diff] [blame] | 433 | /* |
| 434 | * Now that we have all sorted out, just set the ->end of all |
| 435 | * symbols |
| 436 | */ |
| 437 | struct rb_node *nd, *prevnd = rb_first(&kernel_dso->syms); |
| 438 | |
| 439 | if (prevnd == NULL) |
| 440 | goto out_delete_line; |
| 441 | |
| 442 | for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) { |
| 443 | struct symbol *prev = rb_entry(prevnd, struct symbol, rb_node), |
| 444 | *curr = rb_entry(nd, struct symbol, rb_node); |
| 445 | |
| 446 | prev->end = curr->start - 1; |
| 447 | prevnd = nd; |
| 448 | } |
| 449 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 450 | dsos__add(kernel_dso); |
| 451 | free(line); |
| 452 | fclose(file); |
| 453 | return 0; |
| 454 | |
Arnaldo Carvalho de Melo | 59d8102 | 2009-05-26 11:14:27 -0300 | [diff] [blame] | 455 | out_delete_line: |
| 456 | free(line); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 457 | out_delete_dso: |
| 458 | dso__delete(kernel_dso); |
| 459 | return -1; |
| 460 | } |
| 461 | |
| 462 | struct map { |
| 463 | struct list_head node; |
| 464 | uint64_t start; |
| 465 | uint64_t end; |
| 466 | uint64_t pgoff; |
| 467 | struct dso *dso; |
| 468 | }; |
| 469 | |
| 470 | static struct map *map__new(struct mmap_event *event) |
| 471 | { |
| 472 | struct map *self = malloc(sizeof(*self)); |
| 473 | |
| 474 | if (self != NULL) { |
| 475 | self->start = event->start; |
| 476 | self->end = event->start + event->len; |
| 477 | self->pgoff = event->pgoff; |
| 478 | |
| 479 | self->dso = dsos__findnew(event->filename); |
| 480 | if (self->dso == NULL) |
| 481 | goto out_delete; |
| 482 | } |
| 483 | return self; |
| 484 | out_delete: |
| 485 | free(self); |
| 486 | return NULL; |
| 487 | } |
| 488 | |
| 489 | static size_t map__fprintf(struct map *self, FILE *fp) |
| 490 | { |
| 491 | return fprintf(fp, " %lx-%lx %lx %s\n", |
| 492 | self->start, self->end, self->pgoff, self->dso->name); |
| 493 | } |
| 494 | |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 495 | struct thread; |
| 496 | |
| 497 | static const char *thread__name(struct thread *self, char *bf, size_t size); |
| 498 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 499 | struct symhist { |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 500 | struct rb_node rb_node; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 501 | struct dso *dso; |
| 502 | struct symbol *sym; |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 503 | struct thread *thread; |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 504 | uint64_t ip; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 505 | uint32_t count; |
| 506 | char level; |
| 507 | }; |
| 508 | |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 509 | static struct symhist *symhist__new(struct symbol *sym, uint64_t ip, |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 510 | struct thread *thread, struct dso *dso, |
| 511 | char level) |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 512 | { |
| 513 | struct symhist *self = malloc(sizeof(*self)); |
| 514 | |
| 515 | if (self != NULL) { |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 516 | self->sym = sym; |
| 517 | self->thread = thread; |
| 518 | self->ip = ip; |
| 519 | self->dso = dso; |
| 520 | self->level = level; |
| 521 | self->count = 1; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | return self; |
| 525 | } |
| 526 | |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 527 | void symhist__delete(struct symhist *self) |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 528 | { |
| 529 | free(self); |
| 530 | } |
| 531 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 532 | static void symhist__inc(struct symhist *self) |
| 533 | { |
| 534 | ++self->count; |
| 535 | } |
| 536 | |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 537 | static size_t |
| 538 | symhist__fprintf(struct symhist *self, uint64_t total_samples, FILE *fp) |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 539 | { |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 540 | char bf[32]; |
| 541 | size_t ret; |
| 542 | |
| 543 | if (total_samples) |
| 544 | ret = fprintf(fp, "%5.2f", (self->count * 100.0) / total_samples); |
| 545 | else |
| 546 | ret = fprintf(fp, "%12d", self->count); |
| 547 | |
| 548 | ret += fprintf(fp, "%14s [%c] %#018llx ", |
| 549 | thread__name(self->thread, bf, sizeof(bf)), |
| 550 | self->level, (unsigned long long)self->ip); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 551 | |
| 552 | if (self->level != '.') |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 553 | ret += fprintf(fp, "%s\n", |
| 554 | self->sym ? self->sym->name : "<unknown>"); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 555 | else |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 556 | ret += fprintf(fp, "%s: %s\n", |
Peter Zijlstra | f17e04a | 2009-05-26 15:30:22 +0200 | [diff] [blame] | 557 | self->dso ? self->dso->name : "<unknown>", |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 558 | self->sym ? self->sym->name : "<unknown>"); |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 559 | return ret; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | struct thread { |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 563 | struct rb_node rb_node; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 564 | struct list_head maps; |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 565 | struct rb_root symhists; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 566 | pid_t pid; |
| 567 | char *comm; |
| 568 | }; |
| 569 | |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 570 | static const char *thread__name(struct thread *self, char *bf, size_t size) |
| 571 | { |
| 572 | if (self->comm) |
| 573 | return self->comm; |
| 574 | |
| 575 | snprintf(bf, sizeof(bf), ":%u", self->pid); |
| 576 | return bf; |
| 577 | } |
| 578 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 579 | static struct thread *thread__new(pid_t pid) |
| 580 | { |
| 581 | struct thread *self = malloc(sizeof(*self)); |
| 582 | |
| 583 | if (self != NULL) { |
| 584 | self->pid = pid; |
| 585 | self->comm = NULL; |
| 586 | INIT_LIST_HEAD(&self->maps); |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 587 | self->symhists = RB_ROOT; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | return self; |
| 591 | } |
| 592 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 593 | static int thread__symbol_incnew(struct thread *self, struct symbol *sym, |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 594 | uint64_t ip, struct dso *dso, char level) |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 595 | { |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 596 | struct rb_node **p = &self->symhists.rb_node; |
| 597 | struct rb_node *parent = NULL; |
| 598 | struct symhist *sh; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 599 | |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 600 | while (*p != NULL) { |
| 601 | parent = *p; |
| 602 | sh = rb_entry(parent, struct symhist, rb_node); |
| 603 | |
| 604 | if (sh->sym == sym || ip == sh->ip) { |
| 605 | symhist__inc(sh); |
| 606 | return 0; |
| 607 | } |
| 608 | |
| 609 | /* Handle unresolved symbols too */ |
| 610 | const uint64_t start = !sh->sym ? sh->ip : sh->sym->start; |
| 611 | |
| 612 | if (ip < start) |
| 613 | p = &(*p)->rb_left; |
| 614 | else |
| 615 | p = &(*p)->rb_right; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 616 | } |
| 617 | |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 618 | sh = symhist__new(sym, ip, self, dso, level); |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 619 | if (sh == NULL) |
| 620 | return -ENOMEM; |
| 621 | rb_link_node(&sh->rb_node, parent, p); |
| 622 | rb_insert_color(&sh->rb_node, &self->symhists); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 623 | return 0; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | static int thread__set_comm(struct thread *self, const char *comm) |
| 627 | { |
| 628 | self->comm = strdup(comm); |
| 629 | return self->comm ? 0 : -ENOMEM; |
| 630 | } |
| 631 | |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 632 | size_t thread__maps_fprintf(struct thread *self, FILE *fp) |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 633 | { |
| 634 | struct map *pos; |
| 635 | size_t ret = 0; |
| 636 | |
| 637 | list_for_each_entry(pos, &self->maps, node) |
| 638 | ret += map__fprintf(pos, fp); |
| 639 | |
| 640 | return ret; |
| 641 | } |
| 642 | |
| 643 | static size_t thread__fprintf(struct thread *self, FILE *fp) |
| 644 | { |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 645 | int ret = fprintf(fp, "thread: %d %s\n", self->pid, self->comm); |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 646 | struct rb_node *nd; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 647 | |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 648 | for (nd = rb_first(&self->symhists); nd; nd = rb_next(nd)) { |
| 649 | struct symhist *pos = rb_entry(nd, struct symhist, rb_node); |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 650 | ret += symhist__fprintf(pos, 0, fp); |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 651 | } |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 652 | |
| 653 | return ret; |
| 654 | } |
| 655 | |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 656 | static struct rb_root threads = RB_ROOT; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 657 | |
| 658 | static struct thread *threads__findnew(pid_t pid) |
| 659 | { |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 660 | struct rb_node **p = &threads.rb_node; |
| 661 | struct rb_node *parent = NULL; |
| 662 | struct thread *th; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 663 | |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 664 | while (*p != NULL) { |
| 665 | parent = *p; |
| 666 | th = rb_entry(parent, struct thread, rb_node); |
| 667 | |
| 668 | if (th->pid == pid) |
| 669 | return th; |
| 670 | |
| 671 | if (pid < th->pid) |
| 672 | p = &(*p)->rb_left; |
| 673 | else |
| 674 | p = &(*p)->rb_right; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 675 | } |
| 676 | |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 677 | th = thread__new(pid); |
| 678 | if (th != NULL) { |
| 679 | rb_link_node(&th->rb_node, parent, p); |
| 680 | rb_insert_color(&th->rb_node, &threads); |
| 681 | } |
| 682 | return th; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | static void thread__insert_map(struct thread *self, struct map *map) |
| 686 | { |
| 687 | list_add_tail(&map->node, &self->maps); |
| 688 | } |
| 689 | |
| 690 | static struct map *thread__find_map(struct thread *self, uint64_t ip) |
| 691 | { |
| 692 | if (self == NULL) |
| 693 | return NULL; |
| 694 | |
| 695 | struct map *pos; |
| 696 | |
| 697 | list_for_each_entry(pos, &self->maps, node) |
| 698 | if (ip >= pos->start && ip <= pos->end) |
| 699 | return pos; |
| 700 | |
| 701 | return NULL; |
| 702 | } |
| 703 | |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 704 | void threads__fprintf(FILE *fp) |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 705 | { |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 706 | struct rb_node *nd; |
| 707 | for (nd = rb_first(&threads); nd; nd = rb_next(nd)) { |
| 708 | struct thread *pos = rb_entry(nd, struct thread, rb_node); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 709 | thread__fprintf(pos, fp); |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 710 | } |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 711 | } |
| 712 | |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 713 | static struct rb_root global_symhists = RB_ROOT; |
| 714 | |
| 715 | static void threads__insert_symhist(struct symhist *sh) |
| 716 | { |
| 717 | struct rb_node **p = &global_symhists.rb_node; |
| 718 | struct rb_node *parent = NULL; |
| 719 | struct symhist *iter; |
| 720 | |
| 721 | while (*p != NULL) { |
| 722 | parent = *p; |
| 723 | iter = rb_entry(parent, struct symhist, rb_node); |
| 724 | |
| 725 | /* Reverse order */ |
| 726 | if (sh->count > iter->count) |
| 727 | p = &(*p)->rb_left; |
| 728 | else |
| 729 | p = &(*p)->rb_right; |
| 730 | } |
| 731 | |
| 732 | rb_link_node(&sh->rb_node, parent, p); |
| 733 | rb_insert_color(&sh->rb_node, &global_symhists); |
| 734 | } |
| 735 | |
| 736 | static void threads__sort_symhists(void) |
| 737 | { |
| 738 | struct rb_node *nd; |
| 739 | |
| 740 | for (nd = rb_first(&threads); nd; nd = rb_next(nd)) { |
| 741 | struct thread *thread = rb_entry(nd, struct thread, rb_node); |
| 742 | struct rb_node *next = rb_first(&thread->symhists); |
| 743 | |
| 744 | while (next) { |
| 745 | struct symhist *n = rb_entry(next, struct symhist, |
| 746 | rb_node); |
| 747 | next = rb_next(&n->rb_node); |
| 748 | rb_erase(&n->rb_node, &thread->symhists); |
| 749 | threads__insert_symhist(n); |
| 750 | } |
| 751 | |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | static size_t threads__symhists_fprintf(uint64_t total_samples, FILE *fp) |
| 756 | { |
| 757 | struct rb_node *nd; |
| 758 | size_t ret = 0; |
| 759 | |
| 760 | for (nd = rb_first(&global_symhists); nd; nd = rb_next(nd)) { |
| 761 | struct symhist *pos = rb_entry(nd, struct symhist, rb_node); |
| 762 | ret += symhist__fprintf(pos, total_samples, fp); |
| 763 | } |
| 764 | |
| 765 | return ret; |
| 766 | } |
| 767 | |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 768 | static int __cmd_report(void) |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 769 | { |
| 770 | unsigned long offset = 0; |
| 771 | unsigned long head = 0; |
| 772 | struct stat stat; |
| 773 | char *buf; |
| 774 | event_t *event; |
| 775 | int ret, rc = EXIT_FAILURE; |
Peter Zijlstra | 6142f9e | 2009-05-26 20:51:47 +0200 | [diff] [blame] | 776 | uint32_t size; |
Ingo Molnar | f49515b | 2009-05-26 19:03:36 +0200 | [diff] [blame] | 777 | unsigned long total = 0, total_mmap = 0, total_comm = 0, total_unknown = 0; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 778 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 779 | input = open(input_name, O_RDONLY); |
| 780 | if (input < 0) { |
| 781 | perror("failed to open file"); |
| 782 | exit(-1); |
| 783 | } |
| 784 | |
| 785 | ret = fstat(input, &stat); |
| 786 | if (ret < 0) { |
| 787 | perror("failed to stat file"); |
| 788 | exit(-1); |
| 789 | } |
| 790 | |
| 791 | if (!stat.st_size) { |
| 792 | fprintf(stderr, "zero-sized file, nothing to do!\n"); |
| 793 | exit(0); |
| 794 | } |
| 795 | |
| 796 | if (load_kallsyms() < 0) { |
| 797 | perror("failed to open kallsyms"); |
| 798 | return EXIT_FAILURE; |
| 799 | } |
| 800 | |
| 801 | remap: |
| 802 | buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ, |
| 803 | MAP_SHARED, input, offset); |
| 804 | if (buf == MAP_FAILED) { |
| 805 | perror("failed to mmap file"); |
| 806 | exit(-1); |
| 807 | } |
| 808 | |
| 809 | more: |
| 810 | event = (event_t *)(buf + head); |
| 811 | |
Peter Zijlstra | 6142f9e | 2009-05-26 20:51:47 +0200 | [diff] [blame] | 812 | size = event->header.size; |
| 813 | if (!size) |
| 814 | size = 8; |
| 815 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 816 | if (head + event->header.size >= page_size * mmap_window) { |
| 817 | unsigned long shift = page_size * (head / page_size); |
| 818 | int ret; |
| 819 | |
| 820 | ret = munmap(buf, page_size * mmap_window); |
| 821 | assert(ret == 0); |
| 822 | |
| 823 | offset += shift; |
| 824 | head -= shift; |
| 825 | goto remap; |
| 826 | } |
| 827 | |
Peter Zijlstra | 6142f9e | 2009-05-26 20:51:47 +0200 | [diff] [blame] | 828 | size = event->header.size; |
| 829 | if (!size) |
| 830 | goto broken_event; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 831 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 832 | if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) { |
| 833 | char level; |
| 834 | int show = 0; |
| 835 | struct dso *dso = NULL; |
| 836 | struct thread *thread = threads__findnew(event->ip.pid); |
Peter Zijlstra | f17e04a | 2009-05-26 15:30:22 +0200 | [diff] [blame] | 837 | uint64_t ip = event->ip.ip; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 838 | |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 839 | if (dump_trace) { |
Ingo Molnar | f49515b | 2009-05-26 19:03:36 +0200 | [diff] [blame] | 840 | fprintf(stderr, "%p [%p]: PERF_EVENT (IP, %d): %d: %p\n", |
| 841 | (void *)(offset + head), |
| 842 | (void *)(long)(event->header.size), |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 843 | event->header.misc, |
| 844 | event->ip.pid, |
| 845 | (void *)event->ip.ip); |
| 846 | } |
| 847 | |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 848 | if (thread == NULL) { |
| 849 | fprintf(stderr, "problem processing %d event, bailing out\n", |
| 850 | event->header.type); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 851 | goto done; |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 852 | } |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 853 | |
| 854 | if (event->header.misc & PERF_EVENT_MISC_KERNEL) { |
| 855 | show = SHOW_KERNEL; |
| 856 | level = 'k'; |
| 857 | dso = kernel_dso; |
| 858 | } else if (event->header.misc & PERF_EVENT_MISC_USER) { |
| 859 | show = SHOW_USER; |
| 860 | level = '.'; |
Peter Zijlstra | f17e04a | 2009-05-26 15:30:22 +0200 | [diff] [blame] | 861 | struct map *map = thread__find_map(thread, ip); |
| 862 | if (map != NULL) { |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 863 | dso = map->dso; |
Peter Zijlstra | f17e04a | 2009-05-26 15:30:22 +0200 | [diff] [blame] | 864 | ip -= map->start + map->pgoff; |
| 865 | } |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 866 | } else { |
| 867 | show = SHOW_HV; |
| 868 | level = 'H'; |
| 869 | } |
| 870 | |
| 871 | if (show & show_mask) { |
Peter Zijlstra | f17e04a | 2009-05-26 15:30:22 +0200 | [diff] [blame] | 872 | struct symbol *sym = dso__find_symbol(dso, ip); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 873 | |
Peter Zijlstra | f17e04a | 2009-05-26 15:30:22 +0200 | [diff] [blame] | 874 | if (thread__symbol_incnew(thread, sym, ip, dso, level)) { |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 875 | fprintf(stderr, "problem incrementing symbol count, bailing out\n"); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 876 | goto done; |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 877 | } |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 878 | } |
| 879 | total++; |
| 880 | } else switch (event->header.type) { |
| 881 | case PERF_EVENT_MMAP: { |
| 882 | struct thread *thread = threads__findnew(event->mmap.pid); |
| 883 | struct map *map = map__new(&event->mmap); |
| 884 | |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 885 | if (dump_trace) { |
Ingo Molnar | f49515b | 2009-05-26 19:03:36 +0200 | [diff] [blame] | 886 | fprintf(stderr, "%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n", |
| 887 | (void *)(offset + head), |
| 888 | (void *)(long)(event->header.size), |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 889 | (void *)event->mmap.start, |
| 890 | (void *)event->mmap.len, |
| 891 | (void *)event->mmap.pgoff, |
| 892 | event->mmap.filename); |
| 893 | } |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 894 | if (thread == NULL || map == NULL) { |
| 895 | fprintf(stderr, "problem processing PERF_EVENT_MMAP, bailing out\n"); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 896 | goto done; |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 897 | } |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 898 | thread__insert_map(thread, map); |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 899 | total_mmap++; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 900 | break; |
| 901 | } |
| 902 | case PERF_EVENT_COMM: { |
| 903 | struct thread *thread = threads__findnew(event->comm.pid); |
| 904 | |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 905 | if (dump_trace) { |
Ingo Molnar | f49515b | 2009-05-26 19:03:36 +0200 | [diff] [blame] | 906 | fprintf(stderr, "%p [%p]: PERF_EVENT_COMM: %s:%d\n", |
| 907 | (void *)(offset + head), |
| 908 | (void *)(long)(event->header.size), |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 909 | event->comm.comm, event->comm.pid); |
| 910 | } |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 911 | if (thread == NULL || |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 912 | thread__set_comm(thread, event->comm.comm)) { |
| 913 | fprintf(stderr, "problem processing PERF_EVENT_COMM, bailing out\n"); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 914 | goto done; |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 915 | } |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 916 | total_comm++; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 917 | break; |
| 918 | } |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 919 | default: { |
Peter Zijlstra | 6142f9e | 2009-05-26 20:51:47 +0200 | [diff] [blame] | 920 | broken_event: |
Ingo Molnar | f49515b | 2009-05-26 19:03:36 +0200 | [diff] [blame] | 921 | fprintf(stderr, "%p [%p]: skipping unknown header type: %d\n", |
| 922 | (void *)(offset + head), |
| 923 | (void *)(long)(event->header.size), |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 924 | event->header.type); |
Ingo Molnar | 3e70611 | 2009-05-26 18:53:17 +0200 | [diff] [blame] | 925 | total_unknown++; |
Peter Zijlstra | 6142f9e | 2009-05-26 20:51:47 +0200 | [diff] [blame] | 926 | |
| 927 | /* |
| 928 | * assume we lost track of the stream, check alignment, and |
| 929 | * increment a single u64 in the hope to catch on again 'soon'. |
| 930 | */ |
| 931 | |
| 932 | if (unlikely(head & 7)) |
| 933 | head &= ~7ULL; |
| 934 | |
| 935 | size = 8; |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 936 | } |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 937 | } |
| 938 | |
Peter Zijlstra | 6142f9e | 2009-05-26 20:51:47 +0200 | [diff] [blame] | 939 | head += size; |
Ingo Molnar | f49515b | 2009-05-26 19:03:36 +0200 | [diff] [blame] | 940 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 941 | if (offset + head < stat.st_size) |
| 942 | goto more; |
| 943 | |
| 944 | rc = EXIT_SUCCESS; |
| 945 | done: |
| 946 | close(input); |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 947 | |
| 948 | if (dump_trace) { |
Ingo Molnar | 3e70611 | 2009-05-26 18:53:17 +0200 | [diff] [blame] | 949 | fprintf(stderr, " IP events: %10ld\n", total); |
| 950 | fprintf(stderr, " mmap events: %10ld\n", total_mmap); |
| 951 | fprintf(stderr, " comm events: %10ld\n", total_comm); |
| 952 | fprintf(stderr, " unknown events: %10ld\n", total_unknown); |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 953 | |
| 954 | return 0; |
| 955 | } |
| 956 | |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 957 | threads__sort_symhists(); |
| 958 | threads__symhists_fprintf(total, stdout); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 959 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 960 | return rc; |
| 961 | } |
| 962 | |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 963 | static const char * const report_usage[] = { |
| 964 | "perf report [<options>] <command>", |
| 965 | NULL |
| 966 | }; |
| 967 | |
| 968 | static const struct option options[] = { |
| 969 | OPT_STRING('i', "input", &input_name, "file", |
| 970 | "input file name"), |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 971 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, |
| 972 | "dump raw trace in ASCII"), |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 973 | OPT_END() |
| 974 | }; |
| 975 | |
| 976 | int cmd_report(int argc, const char **argv, const char *prefix) |
| 977 | { |
| 978 | elf_version(EV_CURRENT); |
| 979 | |
| 980 | page_size = getpagesize(); |
| 981 | |
| 982 | parse_options(argc, argv, options, report_usage, 0); |
| 983 | |
| 984 | return __cmd_report(); |
| 985 | } |