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