Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 1 | #define _GNU_SOURCE |
| 2 | #include <sys/types.h> |
| 3 | #include <sys/stat.h> |
| 4 | #include <sys/time.h> |
| 5 | #include <unistd.h> |
| 6 | #include <stdint.h> |
| 7 | #include <stdbool.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <string.h> |
| 10 | #include <limits.h> |
Arnaldo Carvalho de Melo | 62eb939 | 2009-05-18 14:28:47 -0300 | [diff] [blame] | 11 | #include <gelf.h> |
| 12 | #include <elf.h> |
| 13 | #include <libelf.h> |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 14 | #include <fcntl.h> |
| 15 | #include <stdio.h> |
| 16 | #include <errno.h> |
| 17 | #include <ctype.h> |
| 18 | #include <time.h> |
| 19 | #include <getopt.h> |
| 20 | #include <assert.h> |
| 21 | #include <search.h> |
| 22 | |
| 23 | #include <sys/ioctl.h> |
| 24 | #include <sys/poll.h> |
| 25 | #include <sys/prctl.h> |
| 26 | #include <sys/wait.h> |
| 27 | #include <sys/mman.h> |
| 28 | #include <sys/types.h> |
| 29 | #include <sys/stat.h> |
| 30 | |
| 31 | #include <linux/unistd.h> |
| 32 | #include <linux/types.h> |
| 33 | |
| 34 | #include "../../include/linux/perf_counter.h" |
Arnaldo Carvalho de Melo | 35a50c8 | 2009-05-18 16:24:49 -0300 | [diff] [blame] | 35 | #include "util/list.h" |
| 36 | #include "util/rbtree.h" |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 37 | |
| 38 | #define SHOW_KERNEL 1 |
| 39 | #define SHOW_USER 2 |
| 40 | #define SHOW_HV 4 |
| 41 | |
| 42 | static char const *input_name = "output.perf"; |
| 43 | static int input; |
| 44 | static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV; |
| 45 | |
| 46 | static unsigned long page_size; |
| 47 | static unsigned long mmap_window = 32; |
| 48 | |
| 49 | static const char *perf_event_names[] = { |
| 50 | [PERF_EVENT_MMAP] = " PERF_EVENT_MMAP", |
| 51 | [PERF_EVENT_MUNMAP] = " PERF_EVENT_MUNMAP", |
| 52 | [PERF_EVENT_COMM] = " PERF_EVENT_COMM", |
| 53 | }; |
| 54 | |
| 55 | struct ip_event { |
| 56 | struct perf_event_header header; |
| 57 | __u64 ip; |
| 58 | __u32 pid, tid; |
| 59 | }; |
| 60 | struct mmap_event { |
| 61 | struct perf_event_header header; |
| 62 | __u32 pid, tid; |
| 63 | __u64 start; |
| 64 | __u64 len; |
| 65 | __u64 pgoff; |
| 66 | char filename[PATH_MAX]; |
| 67 | }; |
| 68 | struct comm_event { |
| 69 | struct perf_event_header header; |
| 70 | __u32 pid,tid; |
| 71 | char comm[16]; |
| 72 | }; |
| 73 | |
| 74 | typedef union event_union { |
| 75 | struct perf_event_header header; |
| 76 | struct ip_event ip; |
| 77 | struct mmap_event mmap; |
| 78 | struct comm_event comm; |
| 79 | } event_t; |
| 80 | |
| 81 | struct section { |
| 82 | struct list_head node; |
| 83 | uint64_t start; |
| 84 | uint64_t end; |
| 85 | uint64_t offset; |
| 86 | char name[0]; |
| 87 | }; |
| 88 | |
| 89 | static struct section *section__new(uint64_t start, uint64_t size, |
| 90 | uint64_t offset, char *name) |
| 91 | { |
| 92 | struct section *self = malloc(sizeof(*self) + strlen(name) + 1); |
| 93 | |
| 94 | if (self != NULL) { |
| 95 | self->start = start; |
| 96 | self->end = start + size; |
| 97 | self->offset = offset; |
| 98 | strcpy(self->name, name); |
| 99 | } |
| 100 | |
| 101 | return self; |
| 102 | } |
| 103 | |
| 104 | static void section__delete(struct section *self) |
| 105 | { |
| 106 | free(self); |
| 107 | } |
| 108 | |
| 109 | struct symbol { |
Arnaldo Carvalho de Melo | 35a50c8 | 2009-05-18 16:24:49 -0300 | [diff] [blame] | 110 | struct rb_node rb_node; |
| 111 | uint64_t start; |
| 112 | uint64_t end; |
| 113 | char name[0]; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | static struct symbol *symbol__new(uint64_t start, uint64_t len, const char *name) |
| 117 | { |
| 118 | struct symbol *self = malloc(sizeof(*self) + strlen(name) + 1); |
| 119 | |
| 120 | if (self != NULL) { |
| 121 | self->start = start; |
| 122 | self->end = start + len; |
| 123 | strcpy(self->name, name); |
| 124 | } |
| 125 | |
| 126 | return self; |
| 127 | } |
| 128 | |
| 129 | static void symbol__delete(struct symbol *self) |
| 130 | { |
| 131 | free(self); |
| 132 | } |
| 133 | |
| 134 | static size_t symbol__fprintf(struct symbol *self, FILE *fp) |
| 135 | { |
| 136 | return fprintf(fp, " %lx-%lx %s\n", |
| 137 | self->start, self->end, self->name); |
| 138 | } |
| 139 | |
| 140 | struct dso { |
| 141 | struct list_head node; |
| 142 | struct list_head sections; |
Arnaldo Carvalho de Melo | 35a50c8 | 2009-05-18 16:24:49 -0300 | [diff] [blame] | 143 | struct rb_root syms; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 144 | char name[0]; |
| 145 | }; |
| 146 | |
| 147 | static struct dso *dso__new(const char *name) |
| 148 | { |
| 149 | struct dso *self = malloc(sizeof(*self) + strlen(name) + 1); |
| 150 | |
| 151 | if (self != NULL) { |
| 152 | strcpy(self->name, name); |
| 153 | INIT_LIST_HEAD(&self->sections); |
Arnaldo Carvalho de Melo | 35a50c8 | 2009-05-18 16:24:49 -0300 | [diff] [blame] | 154 | self->syms = RB_ROOT; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | return self; |
| 158 | } |
| 159 | |
| 160 | static void dso__delete_sections(struct dso *self) |
| 161 | { |
| 162 | struct section *pos, *n; |
| 163 | |
| 164 | list_for_each_entry_safe(pos, n, &self->sections, node) |
| 165 | section__delete(pos); |
| 166 | } |
| 167 | |
| 168 | static void dso__delete_symbols(struct dso *self) |
| 169 | { |
Arnaldo Carvalho de Melo | 35a50c8 | 2009-05-18 16:24:49 -0300 | [diff] [blame] | 170 | struct symbol *pos; |
| 171 | struct rb_node *next = rb_first(&self->syms); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 172 | |
Arnaldo Carvalho de Melo | 35a50c8 | 2009-05-18 16:24:49 -0300 | [diff] [blame] | 173 | while (next) { |
| 174 | pos = rb_entry(next, struct symbol, rb_node); |
| 175 | next = rb_next(&pos->rb_node); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 176 | symbol__delete(pos); |
Arnaldo Carvalho de Melo | 35a50c8 | 2009-05-18 16:24:49 -0300 | [diff] [blame] | 177 | } |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | static void dso__delete(struct dso *self) |
| 181 | { |
| 182 | dso__delete_sections(self); |
| 183 | dso__delete_symbols(self); |
| 184 | free(self); |
| 185 | } |
| 186 | |
| 187 | static void dso__insert_symbol(struct dso *self, struct symbol *sym) |
| 188 | { |
Arnaldo Carvalho de Melo | 35a50c8 | 2009-05-18 16:24:49 -0300 | [diff] [blame] | 189 | struct rb_node **p = &self->syms.rb_node; |
| 190 | struct rb_node *parent = NULL; |
| 191 | const uint64_t ip = sym->start; |
| 192 | struct symbol *s; |
| 193 | |
| 194 | while (*p != NULL) { |
| 195 | parent = *p; |
| 196 | s = rb_entry(parent, struct symbol, rb_node); |
| 197 | if (ip < s->start) |
| 198 | p = &(*p)->rb_left; |
| 199 | else |
| 200 | p = &(*p)->rb_right; |
| 201 | } |
| 202 | rb_link_node(&sym->rb_node, parent, p); |
| 203 | rb_insert_color(&sym->rb_node, &self->syms); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | static struct symbol *dso__find_symbol(struct dso *self, uint64_t ip) |
| 207 | { |
| 208 | if (self == NULL) |
| 209 | return NULL; |
| 210 | |
Arnaldo Carvalho de Melo | 35a50c8 | 2009-05-18 16:24:49 -0300 | [diff] [blame] | 211 | struct rb_node *n = self->syms.rb_node; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 212 | |
Arnaldo Carvalho de Melo | 35a50c8 | 2009-05-18 16:24:49 -0300 | [diff] [blame] | 213 | while (n) { |
| 214 | struct symbol *s = rb_entry(n, struct symbol, rb_node); |
| 215 | |
| 216 | if (ip < s->start) |
| 217 | n = n->rb_left; |
| 218 | else if (ip > s->end) |
| 219 | n = n->rb_right; |
| 220 | else |
| 221 | return s; |
| 222 | } |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 223 | |
| 224 | return NULL; |
| 225 | } |
| 226 | |
Arnaldo Carvalho de Melo | 62eb939 | 2009-05-18 14:28:47 -0300 | [diff] [blame] | 227 | /** |
| 228 | * elf_symtab__for_each_symbol - iterate thru all the symbols |
| 229 | * |
| 230 | * @self: struct elf_symtab instance to iterate |
| 231 | * @index: uint32_t index |
| 232 | * @sym: GElf_Sym iterator |
| 233 | */ |
| 234 | #define elf_symtab__for_each_symbol(syms, nr_syms, index, sym) \ |
| 235 | for (index = 0, gelf_getsym(syms, index, &sym);\ |
| 236 | index < nr_syms; \ |
| 237 | index++, gelf_getsym(syms, index, &sym)) |
| 238 | |
| 239 | static inline uint8_t elf_sym__type(const GElf_Sym *sym) |
| 240 | { |
| 241 | return GELF_ST_TYPE(sym->st_info); |
| 242 | } |
| 243 | |
| 244 | static inline bool elf_sym__is_function(const GElf_Sym *sym) |
| 245 | { |
| 246 | return elf_sym__type(sym) == STT_FUNC && |
| 247 | sym->st_name != 0 && |
| 248 | sym->st_shndx != SHN_UNDEF; |
| 249 | } |
| 250 | |
| 251 | static inline const char *elf_sym__name(const GElf_Sym *sym, |
| 252 | const Elf_Data *symstrs) |
| 253 | { |
| 254 | return symstrs->d_buf + sym->st_name; |
| 255 | } |
| 256 | |
| 257 | static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep, |
| 258 | GElf_Shdr *shp, const char *name, |
| 259 | size_t *index) |
| 260 | { |
| 261 | Elf_Scn *sec = NULL; |
| 262 | size_t cnt = 1; |
| 263 | |
| 264 | while ((sec = elf_nextscn(elf, sec)) != NULL) { |
| 265 | char *str; |
| 266 | |
| 267 | gelf_getshdr(sec, shp); |
| 268 | str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name); |
| 269 | if (!strcmp(name, str)) { |
| 270 | if (index) |
| 271 | *index = cnt; |
| 272 | break; |
| 273 | } |
| 274 | ++cnt; |
| 275 | } |
| 276 | |
| 277 | return sec; |
| 278 | } |
| 279 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 280 | static int dso__load(struct dso *self) |
| 281 | { |
Arnaldo Carvalho de Melo | 62eb939 | 2009-05-18 14:28:47 -0300 | [diff] [blame] | 282 | int fd = open(self->name, O_RDONLY), err = -1; |
| 283 | |
| 284 | if (fd == -1) |
| 285 | return -1; |
| 286 | |
| 287 | Elf *elf = elf_begin(fd, ELF_C_READ_MMAP, NULL); |
| 288 | if (elf == NULL) { |
| 289 | fprintf(stderr, "%s: cannot read %s ELF file.\n", |
| 290 | __func__, self->name); |
| 291 | goto out_close; |
| 292 | } |
| 293 | |
| 294 | GElf_Ehdr ehdr; |
| 295 | if (gelf_getehdr(elf, &ehdr) == NULL) { |
| 296 | fprintf(stderr, "%s: cannot get elf header.\n", __func__); |
| 297 | goto out_elf_end; |
| 298 | } |
| 299 | |
| 300 | GElf_Shdr shdr; |
| 301 | Elf_Scn *sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL); |
| 302 | if (sec == NULL) |
| 303 | sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL); |
| 304 | |
| 305 | if (sec == NULL) |
| 306 | goto out_elf_end; |
| 307 | |
| 308 | if (gelf_getshdr(sec, &shdr) == NULL) |
| 309 | goto out_elf_end; |
| 310 | |
| 311 | Elf_Data *syms = elf_getdata(sec, NULL); |
| 312 | if (syms == NULL) |
| 313 | goto out_elf_end; |
| 314 | |
| 315 | sec = elf_getscn(elf, shdr.sh_link); |
| 316 | if (sec == NULL) |
| 317 | goto out_elf_end; |
| 318 | |
| 319 | Elf_Data *symstrs = elf_getdata(sec, NULL); |
| 320 | if (symstrs == NULL) |
| 321 | goto out_elf_end; |
| 322 | |
| 323 | const uint32_t nr_syms = shdr.sh_size / shdr.sh_entsize; |
| 324 | |
| 325 | GElf_Sym sym; |
| 326 | uint32_t index; |
| 327 | elf_symtab__for_each_symbol(syms, nr_syms, index, sym) { |
| 328 | if (!elf_sym__is_function(&sym)) |
| 329 | continue; |
| 330 | struct symbol *f = symbol__new(sym.st_value, sym.st_size, |
| 331 | elf_sym__name(&sym, symstrs)); |
| 332 | if (f == NULL) |
| 333 | goto out_elf_end; |
| 334 | |
| 335 | dso__insert_symbol(self, f); |
| 336 | } |
| 337 | |
| 338 | err = 0; |
| 339 | out_elf_end: |
| 340 | elf_end(elf); |
| 341 | out_close: |
| 342 | close(fd); |
| 343 | return err; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | static size_t dso__fprintf(struct dso *self, FILE *fp) |
| 347 | { |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 348 | size_t ret = fprintf(fp, "dso: %s\n", self->name); |
| 349 | |
Arnaldo Carvalho de Melo | 35a50c8 | 2009-05-18 16:24:49 -0300 | [diff] [blame] | 350 | struct rb_node *nd; |
| 351 | for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) { |
| 352 | struct symbol *pos = rb_entry(nd, struct symbol, rb_node); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 353 | ret += symbol__fprintf(pos, fp); |
Arnaldo Carvalho de Melo | 35a50c8 | 2009-05-18 16:24:49 -0300 | [diff] [blame] | 354 | } |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 355 | |
| 356 | return ret; |
| 357 | } |
| 358 | |
| 359 | static LIST_HEAD(dsos); |
| 360 | static struct dso *kernel_dso; |
| 361 | |
| 362 | static void dsos__add(struct dso *dso) |
| 363 | { |
| 364 | list_add_tail(&dso->node, &dsos); |
| 365 | } |
| 366 | |
| 367 | static struct dso *dsos__find(const char *name) |
| 368 | { |
| 369 | struct dso *pos; |
| 370 | |
| 371 | list_for_each_entry(pos, &dsos, node) |
| 372 | if (strcmp(pos->name, name) == 0) |
| 373 | return pos; |
| 374 | return NULL; |
| 375 | } |
| 376 | |
| 377 | static struct dso *dsos__findnew(const char *name) |
| 378 | { |
| 379 | struct dso *dso = dsos__find(name); |
| 380 | |
| 381 | if (dso == NULL) { |
| 382 | dso = dso__new(name); |
| 383 | if (dso != NULL && dso__load(dso) < 0) |
| 384 | goto out_delete_dso; |
| 385 | |
| 386 | dsos__add(dso); |
| 387 | } |
| 388 | |
| 389 | return dso; |
| 390 | |
| 391 | out_delete_dso: |
| 392 | dso__delete(dso); |
| 393 | return NULL; |
| 394 | } |
| 395 | |
| 396 | static void dsos__fprintf(FILE *fp) |
| 397 | { |
| 398 | struct dso *pos; |
| 399 | |
| 400 | list_for_each_entry(pos, &dsos, node) |
| 401 | dso__fprintf(pos, fp); |
| 402 | } |
| 403 | |
| 404 | static int load_kallsyms(void) |
| 405 | { |
| 406 | kernel_dso = dso__new("[kernel]"); |
| 407 | if (kernel_dso == NULL) |
| 408 | return -1; |
| 409 | |
| 410 | FILE *file = fopen("/proc/kallsyms", "r"); |
| 411 | |
| 412 | if (file == NULL) |
| 413 | goto out_delete_dso; |
| 414 | |
| 415 | char *line = NULL; |
| 416 | size_t n; |
| 417 | |
| 418 | while (!feof(file)) { |
| 419 | unsigned long long start; |
| 420 | char c, symbf[4096]; |
| 421 | |
| 422 | if (getline(&line, &n, file) < 0) |
| 423 | break; |
| 424 | |
| 425 | if (!line) |
| 426 | goto out_delete_dso; |
| 427 | |
| 428 | if (sscanf(line, "%llx %c %s", &start, &c, symbf) == 3) { |
| 429 | struct symbol *sym = symbol__new(start, 0x1000000, symbf); |
| 430 | |
| 431 | if (sym == NULL) |
| 432 | goto out_delete_dso; |
| 433 | |
| 434 | dso__insert_symbol(kernel_dso, sym); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | dsos__add(kernel_dso); |
| 439 | free(line); |
| 440 | fclose(file); |
| 441 | return 0; |
| 442 | |
| 443 | out_delete_dso: |
| 444 | dso__delete(kernel_dso); |
| 445 | return -1; |
| 446 | } |
| 447 | |
| 448 | struct map { |
| 449 | struct list_head node; |
| 450 | uint64_t start; |
| 451 | uint64_t end; |
| 452 | uint64_t pgoff; |
| 453 | struct dso *dso; |
| 454 | }; |
| 455 | |
| 456 | static struct map *map__new(struct mmap_event *event) |
| 457 | { |
| 458 | struct map *self = malloc(sizeof(*self)); |
| 459 | |
| 460 | if (self != NULL) { |
| 461 | self->start = event->start; |
| 462 | self->end = event->start + event->len; |
| 463 | self->pgoff = event->pgoff; |
| 464 | |
| 465 | self->dso = dsos__findnew(event->filename); |
| 466 | if (self->dso == NULL) |
| 467 | goto out_delete; |
| 468 | } |
| 469 | return self; |
| 470 | out_delete: |
| 471 | free(self); |
| 472 | return NULL; |
| 473 | } |
| 474 | |
| 475 | static size_t map__fprintf(struct map *self, FILE *fp) |
| 476 | { |
| 477 | return fprintf(fp, " %lx-%lx %lx %s\n", |
| 478 | self->start, self->end, self->pgoff, self->dso->name); |
| 479 | } |
| 480 | |
| 481 | struct symhist { |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame^] | 482 | struct rb_node rb_node; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 483 | struct dso *dso; |
| 484 | struct symbol *sym; |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame^] | 485 | uint64_t ip; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 486 | uint32_t count; |
| 487 | char level; |
| 488 | }; |
| 489 | |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame^] | 490 | static struct symhist *symhist__new(struct symbol *sym, uint64_t ip, |
| 491 | struct dso *dso, char level) |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 492 | { |
| 493 | struct symhist *self = malloc(sizeof(*self)); |
| 494 | |
| 495 | if (self != NULL) { |
| 496 | self->sym = sym; |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame^] | 497 | self->ip = ip; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 498 | self->dso = dso; |
| 499 | self->level = level; |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame^] | 500 | self->count = 1; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | return self; |
| 504 | } |
| 505 | |
| 506 | static void symhist__delete(struct symhist *self) |
| 507 | { |
| 508 | free(self); |
| 509 | } |
| 510 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 511 | static void symhist__inc(struct symhist *self) |
| 512 | { |
| 513 | ++self->count; |
| 514 | } |
| 515 | |
| 516 | static size_t symhist__fprintf(struct symhist *self, FILE *fp) |
| 517 | { |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame^] | 518 | size_t ret = fprintf(fp, "%#llx [%c] ", (unsigned long long)self->ip, self->level); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 519 | |
| 520 | if (self->level != '.') |
| 521 | ret += fprintf(fp, "%s", self->sym->name); |
| 522 | else |
| 523 | ret += fprintf(fp, "%s: %s", |
| 524 | self->dso ? self->dso->name : "<unknown", |
| 525 | self->sym ? self->sym->name : "<unknown>"); |
| 526 | return ret + fprintf(fp, ": %u\n", self->count); |
| 527 | } |
| 528 | |
| 529 | struct thread { |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame^] | 530 | struct rb_node rb_node; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 531 | struct list_head maps; |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame^] | 532 | struct rb_root symhists; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 533 | pid_t pid; |
| 534 | char *comm; |
| 535 | }; |
| 536 | |
| 537 | static struct thread *thread__new(pid_t pid) |
| 538 | { |
| 539 | struct thread *self = malloc(sizeof(*self)); |
| 540 | |
| 541 | if (self != NULL) { |
| 542 | self->pid = pid; |
| 543 | self->comm = NULL; |
| 544 | INIT_LIST_HEAD(&self->maps); |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame^] | 545 | self->symhists = RB_ROOT; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | return self; |
| 549 | } |
| 550 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 551 | 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^] | 552 | uint64_t ip, struct dso *dso, char level) |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 553 | { |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame^] | 554 | struct rb_node **p = &self->symhists.rb_node; |
| 555 | struct rb_node *parent = NULL; |
| 556 | struct symhist *sh; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 557 | |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame^] | 558 | while (*p != NULL) { |
| 559 | parent = *p; |
| 560 | sh = rb_entry(parent, struct symhist, rb_node); |
| 561 | |
| 562 | if (sh->sym == sym || ip == sh->ip) { |
| 563 | symhist__inc(sh); |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | /* Handle unresolved symbols too */ |
| 568 | const uint64_t start = !sh->sym ? sh->ip : sh->sym->start; |
| 569 | |
| 570 | if (ip < start) |
| 571 | p = &(*p)->rb_left; |
| 572 | else |
| 573 | p = &(*p)->rb_right; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 574 | } |
| 575 | |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame^] | 576 | sh = symhist__new(sym, ip, dso, level); |
| 577 | if (sh == NULL) |
| 578 | return -ENOMEM; |
| 579 | rb_link_node(&sh->rb_node, parent, p); |
| 580 | rb_insert_color(&sh->rb_node, &self->symhists); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 581 | return 0; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | static int thread__set_comm(struct thread *self, const char *comm) |
| 585 | { |
| 586 | self->comm = strdup(comm); |
| 587 | return self->comm ? 0 : -ENOMEM; |
| 588 | } |
| 589 | |
| 590 | static size_t thread__maps_fprintf(struct thread *self, FILE *fp) |
| 591 | { |
| 592 | struct map *pos; |
| 593 | size_t ret = 0; |
| 594 | |
| 595 | list_for_each_entry(pos, &self->maps, node) |
| 596 | ret += map__fprintf(pos, fp); |
| 597 | |
| 598 | return ret; |
| 599 | } |
| 600 | |
| 601 | static size_t thread__fprintf(struct thread *self, FILE *fp) |
| 602 | { |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 603 | 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^] | 604 | struct rb_node *nd; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 605 | |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame^] | 606 | for (nd = rb_first(&self->symhists); nd; nd = rb_next(nd)) { |
| 607 | struct symhist *pos = rb_entry(nd, struct symhist, rb_node); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 608 | ret += symhist__fprintf(pos, fp); |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame^] | 609 | } |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 610 | |
| 611 | return ret; |
| 612 | } |
| 613 | |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame^] | 614 | static struct rb_root threads = RB_ROOT; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 615 | |
| 616 | static struct thread *threads__findnew(pid_t pid) |
| 617 | { |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame^] | 618 | struct rb_node **p = &threads.rb_node; |
| 619 | struct rb_node *parent = NULL; |
| 620 | struct thread *th; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 621 | |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame^] | 622 | while (*p != NULL) { |
| 623 | parent = *p; |
| 624 | th = rb_entry(parent, struct thread, rb_node); |
| 625 | |
| 626 | if (th->pid == pid) |
| 627 | return th; |
| 628 | |
| 629 | if (pid < th->pid) |
| 630 | p = &(*p)->rb_left; |
| 631 | else |
| 632 | p = &(*p)->rb_right; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 633 | } |
| 634 | |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame^] | 635 | th = thread__new(pid); |
| 636 | if (th != NULL) { |
| 637 | rb_link_node(&th->rb_node, parent, p); |
| 638 | rb_insert_color(&th->rb_node, &threads); |
| 639 | } |
| 640 | return th; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | static void thread__insert_map(struct thread *self, struct map *map) |
| 644 | { |
| 645 | list_add_tail(&map->node, &self->maps); |
| 646 | } |
| 647 | |
| 648 | static struct map *thread__find_map(struct thread *self, uint64_t ip) |
| 649 | { |
| 650 | if (self == NULL) |
| 651 | return NULL; |
| 652 | |
| 653 | struct map *pos; |
| 654 | |
| 655 | list_for_each_entry(pos, &self->maps, node) |
| 656 | if (ip >= pos->start && ip <= pos->end) |
| 657 | return pos; |
| 658 | |
| 659 | return NULL; |
| 660 | } |
| 661 | |
| 662 | static void threads__fprintf(FILE *fp) |
| 663 | { |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame^] | 664 | struct rb_node *nd; |
| 665 | for (nd = rb_first(&threads); nd; nd = rb_next(nd)) { |
| 666 | struct thread *pos = rb_entry(nd, struct thread, rb_node); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 667 | thread__fprintf(pos, fp); |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame^] | 668 | } |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 669 | } |
| 670 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 671 | static void display_help(void) |
| 672 | { |
| 673 | printf( |
| 674 | "Usage: perf-report [<options>]\n" |
| 675 | " -i file --input=<file> # input file\n" |
| 676 | ); |
| 677 | |
| 678 | exit(0); |
| 679 | } |
| 680 | |
| 681 | static void process_options(int argc, char *argv[]) |
| 682 | { |
| 683 | int error = 0; |
| 684 | |
| 685 | for (;;) { |
| 686 | int option_index = 0; |
| 687 | /** Options for getopt */ |
| 688 | static struct option long_options[] = { |
| 689 | {"input", required_argument, NULL, 'i'}, |
| 690 | {"no-user", no_argument, NULL, 'u'}, |
| 691 | {"no-kernel", no_argument, NULL, 'k'}, |
| 692 | {"no-hv", no_argument, NULL, 'h'}, |
| 693 | {NULL, 0, NULL, 0 } |
| 694 | }; |
| 695 | int c = getopt_long(argc, argv, "+:i:kuh", |
| 696 | long_options, &option_index); |
| 697 | if (c == -1) |
| 698 | break; |
| 699 | |
| 700 | switch (c) { |
| 701 | case 'i': input_name = strdup(optarg); break; |
| 702 | case 'k': show_mask &= ~SHOW_KERNEL; break; |
| 703 | case 'u': show_mask &= ~SHOW_USER; break; |
| 704 | case 'h': show_mask &= ~SHOW_HV; break; |
| 705 | default: error = 1; break; |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | if (error) |
| 710 | display_help(); |
| 711 | } |
| 712 | |
| 713 | int cmd_report(int argc, char **argv) |
| 714 | { |
| 715 | unsigned long offset = 0; |
| 716 | unsigned long head = 0; |
| 717 | struct stat stat; |
| 718 | char *buf; |
| 719 | event_t *event; |
| 720 | int ret, rc = EXIT_FAILURE; |
| 721 | unsigned long total = 0; |
| 722 | |
Arnaldo Carvalho de Melo | 62eb939 | 2009-05-18 14:28:47 -0300 | [diff] [blame] | 723 | elf_version(EV_CURRENT); |
| 724 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 725 | page_size = getpagesize(); |
| 726 | |
| 727 | process_options(argc, argv); |
| 728 | |
| 729 | input = open(input_name, O_RDONLY); |
| 730 | if (input < 0) { |
| 731 | perror("failed to open file"); |
| 732 | exit(-1); |
| 733 | } |
| 734 | |
| 735 | ret = fstat(input, &stat); |
| 736 | if (ret < 0) { |
| 737 | perror("failed to stat file"); |
| 738 | exit(-1); |
| 739 | } |
| 740 | |
| 741 | if (!stat.st_size) { |
| 742 | fprintf(stderr, "zero-sized file, nothing to do!\n"); |
| 743 | exit(0); |
| 744 | } |
| 745 | |
| 746 | if (load_kallsyms() < 0) { |
| 747 | perror("failed to open kallsyms"); |
| 748 | return EXIT_FAILURE; |
| 749 | } |
| 750 | |
| 751 | remap: |
| 752 | buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ, |
| 753 | MAP_SHARED, input, offset); |
| 754 | if (buf == MAP_FAILED) { |
| 755 | perror("failed to mmap file"); |
| 756 | exit(-1); |
| 757 | } |
| 758 | |
| 759 | more: |
| 760 | event = (event_t *)(buf + head); |
| 761 | |
| 762 | if (head + event->header.size >= page_size * mmap_window) { |
| 763 | unsigned long shift = page_size * (head / page_size); |
| 764 | int ret; |
| 765 | |
| 766 | ret = munmap(buf, page_size * mmap_window); |
| 767 | assert(ret == 0); |
| 768 | |
| 769 | offset += shift; |
| 770 | head -= shift; |
| 771 | goto remap; |
| 772 | } |
| 773 | |
| 774 | |
| 775 | if (!event->header.size) { |
| 776 | fprintf(stderr, "zero-sized event at file offset %ld\n", offset + head); |
| 777 | fprintf(stderr, "skipping %ld bytes of events.\n", stat.st_size - offset - head); |
| 778 | goto done; |
| 779 | } |
| 780 | |
| 781 | head += event->header.size; |
| 782 | |
| 783 | if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) { |
| 784 | char level; |
| 785 | int show = 0; |
| 786 | struct dso *dso = NULL; |
| 787 | struct thread *thread = threads__findnew(event->ip.pid); |
| 788 | |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame^] | 789 | if (thread == NULL) { |
| 790 | fprintf(stderr, "problem processing %d event, bailing out\n", |
| 791 | event->header.type); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 792 | goto done; |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame^] | 793 | } |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 794 | |
| 795 | if (event->header.misc & PERF_EVENT_MISC_KERNEL) { |
| 796 | show = SHOW_KERNEL; |
| 797 | level = 'k'; |
| 798 | dso = kernel_dso; |
| 799 | } else if (event->header.misc & PERF_EVENT_MISC_USER) { |
| 800 | show = SHOW_USER; |
| 801 | level = '.'; |
| 802 | struct map *map = thread__find_map(thread, event->ip.ip); |
| 803 | if (map != NULL) |
| 804 | dso = map->dso; |
| 805 | } else { |
| 806 | show = SHOW_HV; |
| 807 | level = 'H'; |
| 808 | } |
| 809 | |
| 810 | if (show & show_mask) { |
| 811 | struct symbol *sym = dso__find_symbol(dso, event->ip.ip); |
| 812 | |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame^] | 813 | if (thread__symbol_incnew(thread, sym, event->ip.ip, |
| 814 | dso, level)) { |
| 815 | fprintf(stderr, "problem incrementing symbol count, bailing out\n"); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 816 | goto done; |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame^] | 817 | } |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 818 | } |
| 819 | total++; |
| 820 | } else switch (event->header.type) { |
| 821 | case PERF_EVENT_MMAP: { |
| 822 | struct thread *thread = threads__findnew(event->mmap.pid); |
| 823 | struct map *map = map__new(&event->mmap); |
| 824 | |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame^] | 825 | if (thread == NULL || map == NULL) { |
| 826 | fprintf(stderr, "problem processing PERF_EVENT_MMAP, bailing out\n"); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 827 | goto done; |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame^] | 828 | } |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 829 | thread__insert_map(thread, map); |
| 830 | break; |
| 831 | } |
| 832 | case PERF_EVENT_COMM: { |
| 833 | struct thread *thread = threads__findnew(event->comm.pid); |
| 834 | |
| 835 | if (thread == NULL || |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame^] | 836 | thread__set_comm(thread, event->comm.comm)) { |
| 837 | fprintf(stderr, "problem processing PERF_EVENT_COMM, bailing out\n"); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 838 | goto done; |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame^] | 839 | } |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 840 | break; |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | if (offset + head < stat.st_size) |
| 845 | goto more; |
| 846 | |
| 847 | rc = EXIT_SUCCESS; |
| 848 | done: |
| 849 | close(input); |
| 850 | //dsos__fprintf(stdout); |
| 851 | threads__fprintf(stdout); |
| 852 | #if 0 |
| 853 | std::map<std::string, int>::iterator hi = hist.begin(); |
| 854 | |
| 855 | while (hi != hist.end()) { |
| 856 | rev_hist.insert(std::pair<int, std::string>(hi->second, hi->first)); |
| 857 | hist.erase(hi++); |
| 858 | } |
| 859 | |
| 860 | std::multimap<int, std::string>::const_iterator ri = rev_hist.begin(); |
| 861 | |
| 862 | while (ri != rev_hist.end()) { |
| 863 | printf(" %5.2f %s\n", (100.0 * ri->first)/total, ri->second.c_str()); |
| 864 | ri++; |
| 865 | } |
| 866 | #endif |
| 867 | return rc; |
| 868 | } |
| 869 | |