Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1 | #include "util/util.h" |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 2 | #include "builtin.h" |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 3 | |
| 4 | #include <libelf.h> |
Arnaldo Carvalho de Melo | 62eb939 | 2009-05-18 14:28:47 -0300 | [diff] [blame] | 5 | #include <gelf.h> |
| 6 | #include <elf.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" |
Ingo Molnar | a930d2c | 2009-05-27 09:50:13 +0200 | [diff] [blame] | 9 | #include "util/cache.h" |
Arnaldo Carvalho de Melo | 35a50c8 | 2009-05-18 16:24:49 -0300 | [diff] [blame] | 10 | #include "util/rbtree.h" |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 11 | |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 12 | #include "perf.h" |
| 13 | |
| 14 | #include "util/parse-options.h" |
| 15 | #include "util/parse-events.h" |
| 16 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 17 | #define SHOW_KERNEL 1 |
| 18 | #define SHOW_USER 2 |
| 19 | #define SHOW_HV 4 |
| 20 | |
Ingo Molnar | 23ac9cb | 2009-05-27 09:33:18 +0200 | [diff] [blame] | 21 | static char const *input_name = "perf.data"; |
Peter Zijlstra | 450aaa2 | 2009-05-27 20:20:23 +0200 | [diff] [blame] | 22 | static char *vmlinux = NULL; |
Peter Zijlstra | 37f440c | 2009-05-27 20:20:26 +0200 | [diff] [blame] | 23 | static char *sort_order = "pid,symbol"; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 24 | static int input; |
| 25 | static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV; |
| 26 | |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 27 | static int dump_trace = 0; |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 28 | static int verbose; |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 29 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 30 | static unsigned long page_size; |
| 31 | static unsigned long mmap_window = 32; |
| 32 | |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 33 | const char *perf_event_names[] = { |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 34 | [PERF_EVENT_MMAP] = " PERF_EVENT_MMAP", |
| 35 | [PERF_EVENT_MUNMAP] = " PERF_EVENT_MUNMAP", |
| 36 | [PERF_EVENT_COMM] = " PERF_EVENT_COMM", |
| 37 | }; |
| 38 | |
| 39 | struct ip_event { |
| 40 | struct perf_event_header header; |
| 41 | __u64 ip; |
| 42 | __u32 pid, tid; |
| 43 | }; |
| 44 | struct mmap_event { |
| 45 | struct perf_event_header header; |
| 46 | __u32 pid, tid; |
| 47 | __u64 start; |
| 48 | __u64 len; |
| 49 | __u64 pgoff; |
| 50 | char filename[PATH_MAX]; |
| 51 | }; |
| 52 | struct comm_event { |
| 53 | struct perf_event_header header; |
| 54 | __u32 pid,tid; |
| 55 | char comm[16]; |
| 56 | }; |
| 57 | |
| 58 | typedef union event_union { |
| 59 | struct perf_event_header header; |
| 60 | struct ip_event ip; |
| 61 | struct mmap_event mmap; |
| 62 | struct comm_event comm; |
| 63 | } event_t; |
| 64 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 65 | struct symbol { |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 66 | struct rb_node rb_node; |
| 67 | __u64 start; |
| 68 | __u64 end; |
| 69 | char name[0]; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | static struct symbol *symbol__new(uint64_t start, uint64_t len, const char *name) |
| 73 | { |
| 74 | struct symbol *self = malloc(sizeof(*self) + strlen(name) + 1); |
| 75 | |
| 76 | if (self != NULL) { |
| 77 | self->start = start; |
| 78 | self->end = start + len; |
| 79 | strcpy(self->name, name); |
| 80 | } |
| 81 | |
| 82 | return self; |
| 83 | } |
| 84 | |
| 85 | static void symbol__delete(struct symbol *self) |
| 86 | { |
| 87 | free(self); |
| 88 | } |
| 89 | |
| 90 | static size_t symbol__fprintf(struct symbol *self, FILE *fp) |
| 91 | { |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 92 | return fprintf(fp, " %llx-%llx %s\n", |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 93 | self->start, self->end, self->name); |
| 94 | } |
| 95 | |
| 96 | struct dso { |
| 97 | struct list_head node; |
Arnaldo Carvalho de Melo | 35a50c8 | 2009-05-18 16:24:49 -0300 | [diff] [blame] | 98 | struct rb_root syms; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 99 | char name[0]; |
| 100 | }; |
| 101 | |
| 102 | static struct dso *dso__new(const char *name) |
| 103 | { |
| 104 | struct dso *self = malloc(sizeof(*self) + strlen(name) + 1); |
| 105 | |
| 106 | if (self != NULL) { |
| 107 | strcpy(self->name, name); |
Arnaldo Carvalho de Melo | 35a50c8 | 2009-05-18 16:24:49 -0300 | [diff] [blame] | 108 | self->syms = RB_ROOT; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | return self; |
| 112 | } |
| 113 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 114 | static void dso__delete_symbols(struct dso *self) |
| 115 | { |
Arnaldo Carvalho de Melo | 35a50c8 | 2009-05-18 16:24:49 -0300 | [diff] [blame] | 116 | struct symbol *pos; |
| 117 | struct rb_node *next = rb_first(&self->syms); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 118 | |
Arnaldo Carvalho de Melo | 35a50c8 | 2009-05-18 16:24:49 -0300 | [diff] [blame] | 119 | while (next) { |
| 120 | pos = rb_entry(next, struct symbol, rb_node); |
| 121 | next = rb_next(&pos->rb_node); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 122 | symbol__delete(pos); |
Arnaldo Carvalho de Melo | 35a50c8 | 2009-05-18 16:24:49 -0300 | [diff] [blame] | 123 | } |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static void dso__delete(struct dso *self) |
| 127 | { |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 128 | dso__delete_symbols(self); |
| 129 | free(self); |
| 130 | } |
| 131 | |
| 132 | static void dso__insert_symbol(struct dso *self, struct symbol *sym) |
| 133 | { |
Arnaldo Carvalho de Melo | 35a50c8 | 2009-05-18 16:24:49 -0300 | [diff] [blame] | 134 | struct rb_node **p = &self->syms.rb_node; |
| 135 | struct rb_node *parent = NULL; |
| 136 | const uint64_t ip = sym->start; |
| 137 | struct symbol *s; |
| 138 | |
| 139 | while (*p != NULL) { |
| 140 | parent = *p; |
| 141 | s = rb_entry(parent, struct symbol, rb_node); |
| 142 | if (ip < s->start) |
| 143 | p = &(*p)->rb_left; |
| 144 | else |
| 145 | p = &(*p)->rb_right; |
| 146 | } |
| 147 | rb_link_node(&sym->rb_node, parent, p); |
| 148 | rb_insert_color(&sym->rb_node, &self->syms); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | static struct symbol *dso__find_symbol(struct dso *self, uint64_t ip) |
| 152 | { |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 153 | struct rb_node *n; |
| 154 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 155 | if (self == NULL) |
| 156 | return NULL; |
| 157 | |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 158 | n = self->syms.rb_node; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 159 | |
Arnaldo Carvalho de Melo | 35a50c8 | 2009-05-18 16:24:49 -0300 | [diff] [blame] | 160 | while (n) { |
| 161 | struct symbol *s = rb_entry(n, struct symbol, rb_node); |
| 162 | |
| 163 | if (ip < s->start) |
| 164 | n = n->rb_left; |
| 165 | else if (ip > s->end) |
| 166 | n = n->rb_right; |
| 167 | else |
| 168 | return s; |
| 169 | } |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 170 | |
| 171 | return NULL; |
| 172 | } |
| 173 | |
Arnaldo Carvalho de Melo | 62eb939 | 2009-05-18 14:28:47 -0300 | [diff] [blame] | 174 | /** |
| 175 | * elf_symtab__for_each_symbol - iterate thru all the symbols |
| 176 | * |
| 177 | * @self: struct elf_symtab instance to iterate |
| 178 | * @index: uint32_t index |
| 179 | * @sym: GElf_Sym iterator |
| 180 | */ |
| 181 | #define elf_symtab__for_each_symbol(syms, nr_syms, index, sym) \ |
| 182 | for (index = 0, gelf_getsym(syms, index, &sym);\ |
| 183 | index < nr_syms; \ |
| 184 | index++, gelf_getsym(syms, index, &sym)) |
| 185 | |
| 186 | static inline uint8_t elf_sym__type(const GElf_Sym *sym) |
| 187 | { |
| 188 | return GELF_ST_TYPE(sym->st_info); |
| 189 | } |
| 190 | |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 191 | 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] | 192 | { |
| 193 | return elf_sym__type(sym) == STT_FUNC && |
| 194 | sym->st_name != 0 && |
Peter Zijlstra | b7a16ea | 2009-05-27 13:35:35 +0200 | [diff] [blame] | 195 | sym->st_shndx != SHN_UNDEF && |
| 196 | sym->st_size != 0; |
Arnaldo Carvalho de Melo | 62eb939 | 2009-05-18 14:28:47 -0300 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | static inline const char *elf_sym__name(const GElf_Sym *sym, |
| 200 | const Elf_Data *symstrs) |
| 201 | { |
| 202 | return symstrs->d_buf + sym->st_name; |
| 203 | } |
| 204 | |
| 205 | static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep, |
| 206 | GElf_Shdr *shp, const char *name, |
| 207 | size_t *index) |
| 208 | { |
| 209 | Elf_Scn *sec = NULL; |
| 210 | size_t cnt = 1; |
| 211 | |
| 212 | while ((sec = elf_nextscn(elf, sec)) != NULL) { |
| 213 | char *str; |
| 214 | |
| 215 | gelf_getshdr(sec, shp); |
| 216 | str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name); |
| 217 | if (!strcmp(name, str)) { |
| 218 | if (index) |
| 219 | *index = cnt; |
| 220 | break; |
| 221 | } |
| 222 | ++cnt; |
| 223 | } |
| 224 | |
| 225 | return sec; |
| 226 | } |
| 227 | |
Peter Zijlstra | b7a16ea | 2009-05-27 13:35:35 +0200 | [diff] [blame] | 228 | static int dso__load_sym(struct dso *self, int fd, char *name) |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 229 | { |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 230 | Elf_Data *symstrs; |
| 231 | uint32_t nr_syms; |
Peter Zijlstra | b7a16ea | 2009-05-27 13:35:35 +0200 | [diff] [blame] | 232 | int err = -1; |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 233 | uint32_t index; |
| 234 | GElf_Ehdr ehdr; |
| 235 | GElf_Shdr shdr; |
| 236 | Elf_Data *syms; |
| 237 | GElf_Sym sym; |
| 238 | Elf_Scn *sec; |
| 239 | Elf *elf; |
Peter Zijlstra | b7a16ea | 2009-05-27 13:35:35 +0200 | [diff] [blame] | 240 | int nr = 0; |
Arnaldo Carvalho de Melo | 62eb939 | 2009-05-18 14:28:47 -0300 | [diff] [blame] | 241 | |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 242 | elf = elf_begin(fd, ELF_C_READ_MMAP, NULL); |
Arnaldo Carvalho de Melo | 62eb939 | 2009-05-18 14:28:47 -0300 | [diff] [blame] | 243 | if (elf == NULL) { |
| 244 | fprintf(stderr, "%s: cannot read %s ELF file.\n", |
Peter Zijlstra | b7a16ea | 2009-05-27 13:35:35 +0200 | [diff] [blame] | 245 | __func__, name); |
Arnaldo Carvalho de Melo | 62eb939 | 2009-05-18 14:28:47 -0300 | [diff] [blame] | 246 | goto out_close; |
| 247 | } |
| 248 | |
Arnaldo Carvalho de Melo | 62eb939 | 2009-05-18 14:28:47 -0300 | [diff] [blame] | 249 | if (gelf_getehdr(elf, &ehdr) == NULL) { |
| 250 | fprintf(stderr, "%s: cannot get elf header.\n", __func__); |
| 251 | goto out_elf_end; |
| 252 | } |
| 253 | |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 254 | sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL); |
Arnaldo Carvalho de Melo | 62eb939 | 2009-05-18 14:28:47 -0300 | [diff] [blame] | 255 | if (sec == NULL) |
| 256 | sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL); |
| 257 | |
| 258 | if (sec == NULL) |
| 259 | goto out_elf_end; |
| 260 | |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 261 | syms = elf_getdata(sec, NULL); |
Arnaldo Carvalho de Melo | 62eb939 | 2009-05-18 14:28:47 -0300 | [diff] [blame] | 262 | if (syms == NULL) |
| 263 | goto out_elf_end; |
| 264 | |
| 265 | sec = elf_getscn(elf, shdr.sh_link); |
| 266 | if (sec == NULL) |
| 267 | goto out_elf_end; |
| 268 | |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 269 | symstrs = elf_getdata(sec, NULL); |
Arnaldo Carvalho de Melo | 62eb939 | 2009-05-18 14:28:47 -0300 | [diff] [blame] | 270 | if (symstrs == NULL) |
| 271 | goto out_elf_end; |
| 272 | |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 273 | nr_syms = shdr.sh_size / shdr.sh_entsize; |
Arnaldo Carvalho de Melo | 62eb939 | 2009-05-18 14:28:47 -0300 | [diff] [blame] | 274 | |
Arnaldo Carvalho de Melo | 62eb939 | 2009-05-18 14:28:47 -0300 | [diff] [blame] | 275 | elf_symtab__for_each_symbol(syms, nr_syms, index, sym) { |
Peter Zijlstra | f17e04a | 2009-05-26 15:30:22 +0200 | [diff] [blame] | 276 | struct symbol *f; |
| 277 | |
Arnaldo Carvalho de Melo | 62eb939 | 2009-05-18 14:28:47 -0300 | [diff] [blame] | 278 | if (!elf_sym__is_function(&sym)) |
| 279 | continue; |
Peter Zijlstra | f17e04a | 2009-05-26 15:30:22 +0200 | [diff] [blame] | 280 | |
| 281 | sec = elf_getscn(elf, sym.st_shndx); |
| 282 | if (!sec) |
| 283 | goto out_elf_end; |
| 284 | |
| 285 | gelf_getshdr(sec, &shdr); |
| 286 | sym.st_value -= shdr.sh_addr - shdr.sh_offset; |
| 287 | |
| 288 | f = symbol__new(sym.st_value, sym.st_size, |
| 289 | elf_sym__name(&sym, symstrs)); |
| 290 | if (!f) |
Arnaldo Carvalho de Melo | 62eb939 | 2009-05-18 14:28:47 -0300 | [diff] [blame] | 291 | goto out_elf_end; |
| 292 | |
| 293 | dso__insert_symbol(self, f); |
Peter Zijlstra | b7a16ea | 2009-05-27 13:35:35 +0200 | [diff] [blame] | 294 | |
| 295 | nr++; |
Arnaldo Carvalho de Melo | 62eb939 | 2009-05-18 14:28:47 -0300 | [diff] [blame] | 296 | } |
| 297 | |
Peter Zijlstra | b7a16ea | 2009-05-27 13:35:35 +0200 | [diff] [blame] | 298 | err = nr; |
Arnaldo Carvalho de Melo | 62eb939 | 2009-05-18 14:28:47 -0300 | [diff] [blame] | 299 | out_elf_end: |
| 300 | elf_end(elf); |
| 301 | out_close: |
Arnaldo Carvalho de Melo | 62eb939 | 2009-05-18 14:28:47 -0300 | [diff] [blame] | 302 | return err; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 303 | } |
| 304 | |
Peter Zijlstra | b7a16ea | 2009-05-27 13:35:35 +0200 | [diff] [blame] | 305 | static int dso__load(struct dso *self) |
| 306 | { |
| 307 | int size = strlen(self->name) + sizeof("/usr/lib/debug%s.debug"); |
| 308 | char *name = malloc(size); |
| 309 | int variant = 0; |
| 310 | int ret = -1; |
| 311 | int fd; |
| 312 | |
| 313 | if (!name) |
| 314 | return -1; |
| 315 | |
| 316 | more: |
| 317 | do { |
| 318 | switch (variant) { |
| 319 | case 0: /* Fedora */ |
| 320 | snprintf(name, size, "/usr/lib/debug%s.debug", self->name); |
| 321 | break; |
| 322 | case 1: /* Ubuntu */ |
| 323 | snprintf(name, size, "/usr/lib/debug%s", self->name); |
| 324 | break; |
| 325 | case 2: /* Sane people */ |
| 326 | snprintf(name, size, "%s", self->name); |
| 327 | break; |
| 328 | |
| 329 | default: |
| 330 | goto out; |
| 331 | } |
| 332 | variant++; |
| 333 | |
| 334 | fd = open(name, O_RDONLY); |
| 335 | } while (fd < 0); |
| 336 | |
| 337 | ret = dso__load_sym(self, fd, name); |
| 338 | close(fd); |
| 339 | |
| 340 | /* |
| 341 | * Some people seem to have debuginfo files _WITHOUT_ debug info!?!? |
| 342 | */ |
| 343 | if (!ret) |
| 344 | goto more; |
| 345 | |
| 346 | out: |
| 347 | free(name); |
| 348 | return ret; |
| 349 | } |
| 350 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 351 | static size_t dso__fprintf(struct dso *self, FILE *fp) |
| 352 | { |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 353 | size_t ret = fprintf(fp, "dso: %s\n", self->name); |
| 354 | |
Arnaldo Carvalho de Melo | 35a50c8 | 2009-05-18 16:24:49 -0300 | [diff] [blame] | 355 | struct rb_node *nd; |
| 356 | for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) { |
| 357 | struct symbol *pos = rb_entry(nd, struct symbol, rb_node); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 358 | ret += symbol__fprintf(pos, fp); |
Arnaldo Carvalho de Melo | 35a50c8 | 2009-05-18 16:24:49 -0300 | [diff] [blame] | 359 | } |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 360 | |
| 361 | return ret; |
| 362 | } |
| 363 | |
| 364 | static LIST_HEAD(dsos); |
| 365 | static struct dso *kernel_dso; |
| 366 | |
| 367 | static void dsos__add(struct dso *dso) |
| 368 | { |
| 369 | list_add_tail(&dso->node, &dsos); |
| 370 | } |
| 371 | |
| 372 | static struct dso *dsos__find(const char *name) |
| 373 | { |
| 374 | struct dso *pos; |
| 375 | |
| 376 | list_for_each_entry(pos, &dsos, node) |
| 377 | if (strcmp(pos->name, name) == 0) |
| 378 | return pos; |
| 379 | return NULL; |
| 380 | } |
| 381 | |
| 382 | static struct dso *dsos__findnew(const char *name) |
| 383 | { |
| 384 | struct dso *dso = dsos__find(name); |
Peter Zijlstra | b7a16ea | 2009-05-27 13:35:35 +0200 | [diff] [blame] | 385 | int nr; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 386 | |
| 387 | if (dso == NULL) { |
| 388 | dso = dso__new(name); |
Peter Zijlstra | b7a16ea | 2009-05-27 13:35:35 +0200 | [diff] [blame] | 389 | if (!dso) |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 390 | goto out_delete_dso; |
| 391 | |
Peter Zijlstra | b7a16ea | 2009-05-27 13:35:35 +0200 | [diff] [blame] | 392 | nr = dso__load(dso); |
| 393 | if (nr < 0) { |
| 394 | fprintf(stderr, "Failed to open: %s\n", name); |
| 395 | goto out_delete_dso; |
| 396 | } |
| 397 | if (!nr) { |
| 398 | fprintf(stderr, |
| 399 | "Failed to find debug symbols for: %s, maybe install a debug package?\n", |
| 400 | name); |
| 401 | } |
| 402 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 403 | dsos__add(dso); |
| 404 | } |
| 405 | |
| 406 | return dso; |
| 407 | |
| 408 | out_delete_dso: |
| 409 | dso__delete(dso); |
| 410 | return NULL; |
| 411 | } |
| 412 | |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 413 | static void dsos__fprintf(FILE *fp) |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 414 | { |
| 415 | struct dso *pos; |
| 416 | |
| 417 | list_for_each_entry(pos, &dsos, node) |
| 418 | dso__fprintf(pos, fp); |
| 419 | } |
| 420 | |
Arnaldo Carvalho de Melo | d8d1656 | 2009-05-26 19:20:57 -0300 | [diff] [blame] | 421 | static int hex(char ch) |
| 422 | { |
| 423 | if ((ch >= '0') && (ch <= '9')) |
| 424 | return ch - '0'; |
| 425 | if ((ch >= 'a') && (ch <= 'f')) |
| 426 | return ch - 'a' + 10; |
| 427 | if ((ch >= 'A') && (ch <= 'F')) |
| 428 | return ch - 'A' + 10; |
| 429 | return -1; |
| 430 | } |
| 431 | |
| 432 | /* |
| 433 | * While we find nice hex chars, build a long_val. |
| 434 | * Return number of chars processed. |
| 435 | */ |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 436 | static int hex2long(char *ptr, unsigned long *long_val) |
Arnaldo Carvalho de Melo | d8d1656 | 2009-05-26 19:20:57 -0300 | [diff] [blame] | 437 | { |
| 438 | const char *p = ptr; |
| 439 | *long_val = 0; |
| 440 | |
| 441 | while (*p) { |
| 442 | const int hex_val = hex(*p); |
| 443 | |
| 444 | if (hex_val < 0) |
| 445 | break; |
| 446 | |
| 447 | *long_val = (*long_val << 4) | hex_val; |
| 448 | p++; |
| 449 | } |
| 450 | |
| 451 | return p - ptr; |
| 452 | } |
| 453 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 454 | static int load_kallsyms(void) |
| 455 | { |
Ingo Molnar | af83632 | 2009-05-27 08:38:48 +0200 | [diff] [blame] | 456 | struct rb_node *nd, *prevnd; |
| 457 | char *line = NULL; |
| 458 | FILE *file; |
| 459 | size_t n; |
| 460 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 461 | kernel_dso = dso__new("[kernel]"); |
| 462 | if (kernel_dso == NULL) |
| 463 | return -1; |
| 464 | |
Ingo Molnar | af83632 | 2009-05-27 08:38:48 +0200 | [diff] [blame] | 465 | file = fopen("/proc/kallsyms", "r"); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 466 | if (file == NULL) |
| 467 | goto out_delete_dso; |
| 468 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 469 | while (!feof(file)) { |
Arnaldo Carvalho de Melo | d8d1656 | 2009-05-26 19:20:57 -0300 | [diff] [blame] | 470 | unsigned long start; |
Ingo Molnar | af83632 | 2009-05-27 08:38:48 +0200 | [diff] [blame] | 471 | struct symbol *sym; |
| 472 | int line_len, len; |
| 473 | char symbol_type; |
| 474 | |
| 475 | line_len = getline(&line, &n, file); |
Arnaldo Carvalho de Melo | d8d1656 | 2009-05-26 19:20:57 -0300 | [diff] [blame] | 476 | if (line_len < 0) |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 477 | break; |
| 478 | |
| 479 | if (!line) |
| 480 | goto out_delete_dso; |
| 481 | |
Arnaldo Carvalho de Melo | d8d1656 | 2009-05-26 19:20:57 -0300 | [diff] [blame] | 482 | line[--line_len] = '\0'; /* \n */ |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 483 | |
Ingo Molnar | af83632 | 2009-05-27 08:38:48 +0200 | [diff] [blame] | 484 | len = hex2long(line, &start); |
| 485 | |
Arnaldo Carvalho de Melo | 03f6316 | 2009-05-26 19:21:55 -0300 | [diff] [blame] | 486 | len++; |
| 487 | if (len + 2 >= line_len) |
| 488 | continue; |
| 489 | |
Ingo Molnar | af83632 | 2009-05-27 08:38:48 +0200 | [diff] [blame] | 490 | symbol_type = toupper(line[len]); |
Arnaldo Carvalho de Melo | 03f6316 | 2009-05-26 19:21:55 -0300 | [diff] [blame] | 491 | /* |
| 492 | * We're interested only in code ('T'ext) |
| 493 | */ |
Ingo Molnar | af83632 | 2009-05-27 08:38:48 +0200 | [diff] [blame] | 494 | if (symbol_type != 'T' && symbol_type != 'W') |
Arnaldo Carvalho de Melo | d8d1656 | 2009-05-26 19:20:57 -0300 | [diff] [blame] | 495 | continue; |
| 496 | /* |
| 497 | * Well fix up the end later, when we have all sorted. |
| 498 | */ |
Ingo Molnar | af83632 | 2009-05-27 08:38:48 +0200 | [diff] [blame] | 499 | sym = symbol__new(start, 0xdead, line + len + 2); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 500 | |
Arnaldo Carvalho de Melo | d8d1656 | 2009-05-26 19:20:57 -0300 | [diff] [blame] | 501 | if (sym == NULL) |
| 502 | goto out_delete_dso; |
| 503 | |
| 504 | dso__insert_symbol(kernel_dso, sym); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 505 | } |
| 506 | |
Arnaldo Carvalho de Melo | abd54f6 | 2009-05-26 12:21:34 -0300 | [diff] [blame] | 507 | /* |
| 508 | * Now that we have all sorted out, just set the ->end of all |
| 509 | * symbols |
| 510 | */ |
Ingo Molnar | af83632 | 2009-05-27 08:38:48 +0200 | [diff] [blame] | 511 | prevnd = rb_first(&kernel_dso->syms); |
Arnaldo Carvalho de Melo | abd54f6 | 2009-05-26 12:21:34 -0300 | [diff] [blame] | 512 | |
| 513 | if (prevnd == NULL) |
| 514 | goto out_delete_line; |
| 515 | |
| 516 | for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) { |
| 517 | struct symbol *prev = rb_entry(prevnd, struct symbol, rb_node), |
| 518 | *curr = rb_entry(nd, struct symbol, rb_node); |
| 519 | |
| 520 | prev->end = curr->start - 1; |
| 521 | prevnd = nd; |
| 522 | } |
| 523 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 524 | dsos__add(kernel_dso); |
| 525 | free(line); |
| 526 | fclose(file); |
Ingo Molnar | af83632 | 2009-05-27 08:38:48 +0200 | [diff] [blame] | 527 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 528 | return 0; |
| 529 | |
Arnaldo Carvalho de Melo | 59d8102 | 2009-05-26 11:14:27 -0300 | [diff] [blame] | 530 | out_delete_line: |
| 531 | free(line); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 532 | out_delete_dso: |
| 533 | dso__delete(kernel_dso); |
| 534 | return -1; |
| 535 | } |
| 536 | |
Peter Zijlstra | 450aaa2 | 2009-05-27 20:20:23 +0200 | [diff] [blame] | 537 | static int load_kernel(void) |
| 538 | { |
| 539 | int fd, nr; |
| 540 | |
| 541 | if (!vmlinux) |
| 542 | goto kallsyms; |
| 543 | |
| 544 | fd = open(vmlinux, O_RDONLY); |
| 545 | if (fd < 0) |
| 546 | goto kallsyms; |
| 547 | |
| 548 | kernel_dso = dso__new("[kernel]"); |
| 549 | if (!kernel_dso) |
| 550 | goto fail_open; |
| 551 | |
| 552 | nr = dso__load_sym(kernel_dso, fd, vmlinux); |
| 553 | |
| 554 | if (nr <= 0) |
| 555 | goto fail_load; |
| 556 | |
| 557 | dsos__add(kernel_dso); |
| 558 | close(fd); |
| 559 | |
| 560 | return 0; |
| 561 | |
| 562 | fail_load: |
| 563 | dso__delete(kernel_dso); |
| 564 | fail_open: |
| 565 | close(fd); |
| 566 | kallsyms: |
| 567 | return load_kallsyms(); |
| 568 | } |
| 569 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 570 | struct map { |
| 571 | struct list_head node; |
| 572 | uint64_t start; |
| 573 | uint64_t end; |
| 574 | uint64_t pgoff; |
| 575 | struct dso *dso; |
| 576 | }; |
| 577 | |
| 578 | static struct map *map__new(struct mmap_event *event) |
| 579 | { |
| 580 | struct map *self = malloc(sizeof(*self)); |
| 581 | |
| 582 | if (self != NULL) { |
| 583 | self->start = event->start; |
| 584 | self->end = event->start + event->len; |
| 585 | self->pgoff = event->pgoff; |
| 586 | |
| 587 | self->dso = dsos__findnew(event->filename); |
| 588 | if (self->dso == NULL) |
| 589 | goto out_delete; |
| 590 | } |
| 591 | return self; |
| 592 | out_delete: |
| 593 | free(self); |
| 594 | return NULL; |
| 595 | } |
| 596 | |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 597 | struct thread; |
| 598 | |
| 599 | static const char *thread__name(struct thread *self, char *bf, size_t size); |
| 600 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 601 | struct thread { |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 602 | struct rb_node rb_node; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 603 | struct list_head maps; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 604 | pid_t pid; |
| 605 | char *comm; |
| 606 | }; |
| 607 | |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 608 | static const char *thread__name(struct thread *self, char *bf, size_t size) |
| 609 | { |
| 610 | if (self->comm) |
| 611 | return self->comm; |
| 612 | |
| 613 | snprintf(bf, sizeof(bf), ":%u", self->pid); |
| 614 | return bf; |
| 615 | } |
| 616 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 617 | static struct thread *thread__new(pid_t pid) |
| 618 | { |
| 619 | struct thread *self = malloc(sizeof(*self)); |
| 620 | |
| 621 | if (self != NULL) { |
| 622 | self->pid = pid; |
| 623 | self->comm = NULL; |
| 624 | INIT_LIST_HEAD(&self->maps); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | return self; |
| 628 | } |
| 629 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 630 | static int thread__set_comm(struct thread *self, const char *comm) |
| 631 | { |
| 632 | self->comm = strdup(comm); |
| 633 | return self->comm ? 0 : -ENOMEM; |
| 634 | } |
| 635 | |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 636 | static struct rb_root threads; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 637 | |
| 638 | static struct thread *threads__findnew(pid_t pid) |
| 639 | { |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 640 | struct rb_node **p = &threads.rb_node; |
| 641 | struct rb_node *parent = NULL; |
| 642 | struct thread *th; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 643 | |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 644 | while (*p != NULL) { |
| 645 | parent = *p; |
| 646 | th = rb_entry(parent, struct thread, rb_node); |
| 647 | |
| 648 | if (th->pid == pid) |
| 649 | return th; |
| 650 | |
| 651 | if (pid < th->pid) |
| 652 | p = &(*p)->rb_left; |
| 653 | else |
| 654 | p = &(*p)->rb_right; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 655 | } |
| 656 | |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 657 | th = thread__new(pid); |
| 658 | if (th != NULL) { |
| 659 | rb_link_node(&th->rb_node, parent, p); |
| 660 | rb_insert_color(&th->rb_node, &threads); |
| 661 | } |
| 662 | return th; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | static void thread__insert_map(struct thread *self, struct map *map) |
| 666 | { |
| 667 | list_add_tail(&map->node, &self->maps); |
| 668 | } |
| 669 | |
| 670 | static struct map *thread__find_map(struct thread *self, uint64_t ip) |
| 671 | { |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 672 | struct map *pos; |
| 673 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 674 | if (self == NULL) |
| 675 | return NULL; |
| 676 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 677 | list_for_each_entry(pos, &self->maps, node) |
| 678 | if (ip >= pos->start && ip <= pos->end) |
| 679 | return pos; |
| 680 | |
| 681 | return NULL; |
| 682 | } |
| 683 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 684 | /* |
| 685 | * histogram, sorted on item, collects counts |
| 686 | */ |
| 687 | |
| 688 | static struct rb_root hist; |
| 689 | |
| 690 | struct hist_entry { |
| 691 | struct rb_node rb_node; |
| 692 | |
| 693 | struct thread *thread; |
| 694 | struct map *map; |
| 695 | struct dso *dso; |
| 696 | struct symbol *sym; |
| 697 | uint64_t ip; |
| 698 | char level; |
| 699 | |
| 700 | uint32_t count; |
| 701 | }; |
| 702 | |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 703 | /* |
| 704 | * configurable sorting bits |
| 705 | */ |
| 706 | |
| 707 | struct sort_entry { |
| 708 | struct list_head list; |
| 709 | |
| 710 | int64_t (*cmp)(struct hist_entry *, struct hist_entry *); |
| 711 | size_t (*print)(FILE *fp, struct hist_entry *); |
| 712 | }; |
| 713 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 714 | static int64_t |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 715 | sort__thread_cmp(struct hist_entry *left, struct hist_entry *right) |
| 716 | { |
| 717 | return right->thread->pid - left->thread->pid; |
| 718 | } |
| 719 | |
| 720 | static size_t |
| 721 | sort__thread_print(FILE *fp, struct hist_entry *self) |
| 722 | { |
| 723 | char bf[32]; |
| 724 | |
| 725 | return fprintf(fp, "%14s ", |
| 726 | thread__name(self->thread, bf, sizeof(bf))); |
| 727 | } |
| 728 | |
| 729 | static struct sort_entry sort_thread = { |
| 730 | .cmp = sort__thread_cmp, |
| 731 | .print = sort__thread_print, |
| 732 | }; |
| 733 | |
| 734 | static int64_t |
Peter Zijlstra | 992444b | 2009-05-27 20:20:27 +0200 | [diff] [blame^] | 735 | sort__comm_cmp(struct hist_entry *left, struct hist_entry *right) |
| 736 | { |
| 737 | char *comm_l = left->thread->comm; |
| 738 | char *comm_r = right->thread->comm; |
| 739 | |
| 740 | if (!comm_l || !comm_r) { |
| 741 | if (!comm_l && !comm_r) |
| 742 | return 0; |
| 743 | else if (!comm_l) |
| 744 | return -1; |
| 745 | else |
| 746 | return 1; |
| 747 | } |
| 748 | |
| 749 | return strcmp(comm_l, comm_r); |
| 750 | } |
| 751 | |
| 752 | static size_t |
| 753 | sort__comm_print(FILE *fp, struct hist_entry *self) |
| 754 | { |
| 755 | return fprintf(fp, "%20s ", self->thread->comm ?: "<unknown>"); |
| 756 | } |
| 757 | |
| 758 | static struct sort_entry sort_comm = { |
| 759 | .cmp = sort__comm_cmp, |
| 760 | .print = sort__comm_print, |
| 761 | }; |
| 762 | |
| 763 | static int64_t |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 764 | sort__sym_cmp(struct hist_entry *left, struct hist_entry *right) |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 765 | { |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 766 | uint64_t ip_l, ip_r; |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 767 | |
| 768 | if (left->sym == right->sym) |
| 769 | return 0; |
| 770 | |
| 771 | ip_l = left->sym ? left->sym->start : left->ip; |
| 772 | ip_r = right->sym ? right->sym->start : right->ip; |
| 773 | |
| 774 | return (int64_t)(ip_r - ip_l); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 775 | } |
| 776 | |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 777 | static size_t |
| 778 | sort__sym_print(FILE *fp, struct hist_entry *self) |
| 779 | { |
| 780 | size_t ret = 0; |
| 781 | |
| 782 | ret += fprintf(fp, "[%c] ", self->level); |
| 783 | |
| 784 | if (verbose) |
| 785 | ret += fprintf(fp, "%#018llx ", (unsigned long long)self->ip); |
| 786 | |
| 787 | if (self->level != '.') |
| 788 | ret += fprintf(fp, "%s ", |
| 789 | self->sym ? self->sym->name : "<unknown>"); |
| 790 | else |
| 791 | ret += fprintf(fp, "%s: %s ", |
| 792 | self->dso ? self->dso->name : "<unknown>", |
| 793 | self->sym ? self->sym->name : "<unknown>"); |
| 794 | |
| 795 | return ret; |
| 796 | } |
| 797 | |
| 798 | static struct sort_entry sort_sym = { |
| 799 | .cmp = sort__sym_cmp, |
| 800 | .print = sort__sym_print, |
| 801 | }; |
| 802 | |
Peter Zijlstra | 37f440c | 2009-05-27 20:20:26 +0200 | [diff] [blame] | 803 | struct sort_dimension { |
| 804 | char *name; |
| 805 | struct sort_entry *entry; |
| 806 | int taken; |
| 807 | }; |
| 808 | |
| 809 | static struct sort_dimension sort_dimensions[] = { |
| 810 | { .name = "pid", .entry = &sort_thread, }, |
Peter Zijlstra | 992444b | 2009-05-27 20:20:27 +0200 | [diff] [blame^] | 811 | { .name = "comm", .entry = &sort_comm, }, |
Peter Zijlstra | 37f440c | 2009-05-27 20:20:26 +0200 | [diff] [blame] | 812 | { .name = "symbol", .entry = &sort_sym, }, |
| 813 | }; |
| 814 | |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 815 | static LIST_HEAD(hist_entry__sort_list); |
| 816 | |
Peter Zijlstra | 37f440c | 2009-05-27 20:20:26 +0200 | [diff] [blame] | 817 | static int sort_dimension__add(char *tok) |
| 818 | { |
| 819 | int i; |
| 820 | |
| 821 | for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) { |
| 822 | struct sort_dimension *sd = &sort_dimensions[i]; |
| 823 | |
| 824 | if (sd->taken) |
| 825 | continue; |
| 826 | |
| 827 | if (strcmp(tok, sd->name)) |
| 828 | continue; |
| 829 | |
| 830 | list_add_tail(&sd->entry->list, &hist_entry__sort_list); |
| 831 | sd->taken = 1; |
| 832 | return 0; |
| 833 | } |
| 834 | |
| 835 | return -ESRCH; |
| 836 | } |
| 837 | |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 838 | static void setup_sorting(void) |
| 839 | { |
Peter Zijlstra | 37f440c | 2009-05-27 20:20:26 +0200 | [diff] [blame] | 840 | char *tmp, *tok, *str = strdup(sort_order); |
| 841 | |
| 842 | for (tok = strtok_r(str, ", ", &tmp); |
| 843 | tok; tok = strtok_r(NULL, ", ", &tmp)) |
| 844 | sort_dimension__add(tok); |
| 845 | |
| 846 | free(str); |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | static int64_t |
| 850 | hist_entry__cmp(struct hist_entry *left, struct hist_entry *right) |
| 851 | { |
| 852 | struct sort_entry *se; |
| 853 | int64_t cmp = 0; |
| 854 | |
| 855 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
| 856 | cmp = se->cmp(left, right); |
| 857 | if (cmp) |
| 858 | break; |
| 859 | } |
| 860 | |
| 861 | return cmp; |
| 862 | } |
| 863 | |
| 864 | static size_t |
| 865 | hist_entry__fprintf(FILE *fp, struct hist_entry *self, uint64_t total_samples) |
| 866 | { |
| 867 | struct sort_entry *se; |
| 868 | size_t ret; |
| 869 | |
| 870 | if (total_samples) { |
| 871 | ret = fprintf(fp, "%5.2f%% ", |
| 872 | (self->count * 100.0) / total_samples); |
| 873 | } else |
| 874 | ret = fprintf(fp, "%12d ", self->count); |
| 875 | |
| 876 | list_for_each_entry(se, &hist_entry__sort_list, list) |
| 877 | ret += se->print(fp, self); |
| 878 | |
| 879 | ret += fprintf(fp, "\n"); |
| 880 | |
| 881 | return ret; |
| 882 | } |
| 883 | |
| 884 | /* |
| 885 | * collect histogram counts |
| 886 | */ |
| 887 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 888 | static int |
| 889 | hist_entry__add(struct thread *thread, struct map *map, struct dso *dso, |
| 890 | struct symbol *sym, uint64_t ip, char level) |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 891 | { |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 892 | struct rb_node **p = &hist.rb_node; |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 893 | struct rb_node *parent = NULL; |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 894 | struct hist_entry *he; |
| 895 | struct hist_entry entry = { |
| 896 | .thread = thread, |
| 897 | .map = map, |
| 898 | .dso = dso, |
| 899 | .sym = sym, |
| 900 | .ip = ip, |
| 901 | .level = level, |
| 902 | .count = 1, |
| 903 | }; |
| 904 | int cmp; |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 905 | |
| 906 | while (*p != NULL) { |
| 907 | parent = *p; |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 908 | he = rb_entry(parent, struct hist_entry, rb_node); |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 909 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 910 | cmp = hist_entry__cmp(&entry, he); |
| 911 | |
| 912 | if (!cmp) { |
| 913 | he->count++; |
| 914 | return 0; |
| 915 | } |
| 916 | |
| 917 | if (cmp < 0) |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 918 | p = &(*p)->rb_left; |
| 919 | else |
| 920 | p = &(*p)->rb_right; |
| 921 | } |
| 922 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 923 | he = malloc(sizeof(*he)); |
| 924 | if (!he) |
| 925 | return -ENOMEM; |
| 926 | *he = entry; |
| 927 | rb_link_node(&he->rb_node, parent, p); |
| 928 | rb_insert_color(&he->rb_node, &hist); |
| 929 | |
| 930 | return 0; |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 931 | } |
| 932 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 933 | /* |
| 934 | * reverse the map, sort on count. |
| 935 | */ |
| 936 | |
| 937 | static struct rb_root output_hists; |
| 938 | |
| 939 | static void output__insert_entry(struct hist_entry *he) |
| 940 | { |
| 941 | struct rb_node **p = &output_hists.rb_node; |
| 942 | struct rb_node *parent = NULL; |
| 943 | struct hist_entry *iter; |
| 944 | |
| 945 | while (*p != NULL) { |
| 946 | parent = *p; |
| 947 | iter = rb_entry(parent, struct hist_entry, rb_node); |
| 948 | |
| 949 | if (he->count > iter->count) |
| 950 | p = &(*p)->rb_left; |
| 951 | else |
| 952 | p = &(*p)->rb_right; |
| 953 | } |
| 954 | |
| 955 | rb_link_node(&he->rb_node, parent, p); |
| 956 | rb_insert_color(&he->rb_node, &output_hists); |
| 957 | } |
| 958 | |
| 959 | static void output__resort(void) |
| 960 | { |
| 961 | struct rb_node *next = rb_first(&hist); |
| 962 | struct hist_entry *n; |
| 963 | |
| 964 | while (next) { |
| 965 | n = rb_entry(next, struct hist_entry, rb_node); |
| 966 | next = rb_next(&n->rb_node); |
| 967 | |
| 968 | rb_erase(&n->rb_node, &hist); |
| 969 | output__insert_entry(n); |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 970 | } |
| 971 | } |
| 972 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 973 | static size_t output__fprintf(FILE *fp, uint64_t total_samples) |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 974 | { |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 975 | struct hist_entry *pos; |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 976 | struct rb_node *nd; |
| 977 | size_t ret = 0; |
| 978 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 979 | for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) { |
| 980 | pos = rb_entry(nd, struct hist_entry, rb_node); |
| 981 | ret += hist_entry__fprintf(fp, pos, total_samples); |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | return ret; |
| 985 | } |
| 986 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 987 | |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 988 | static int __cmd_report(void) |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 989 | { |
| 990 | unsigned long offset = 0; |
| 991 | unsigned long head = 0; |
| 992 | struct stat stat; |
| 993 | char *buf; |
| 994 | event_t *event; |
| 995 | int ret, rc = EXIT_FAILURE; |
Peter Zijlstra | 6142f9e | 2009-05-26 20:51:47 +0200 | [diff] [blame] | 996 | uint32_t size; |
Ingo Molnar | f49515b | 2009-05-26 19:03:36 +0200 | [diff] [blame] | 997 | 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] | 998 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 999 | input = open(input_name, O_RDONLY); |
| 1000 | if (input < 0) { |
| 1001 | perror("failed to open file"); |
| 1002 | exit(-1); |
| 1003 | } |
| 1004 | |
| 1005 | ret = fstat(input, &stat); |
| 1006 | if (ret < 0) { |
| 1007 | perror("failed to stat file"); |
| 1008 | exit(-1); |
| 1009 | } |
| 1010 | |
| 1011 | if (!stat.st_size) { |
| 1012 | fprintf(stderr, "zero-sized file, nothing to do!\n"); |
| 1013 | exit(0); |
| 1014 | } |
| 1015 | |
Peter Zijlstra | 450aaa2 | 2009-05-27 20:20:23 +0200 | [diff] [blame] | 1016 | if (load_kernel() < 0) { |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 1017 | perror("failed to open kallsyms"); |
| 1018 | return EXIT_FAILURE; |
| 1019 | } |
| 1020 | |
| 1021 | remap: |
| 1022 | buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ, |
| 1023 | MAP_SHARED, input, offset); |
| 1024 | if (buf == MAP_FAILED) { |
| 1025 | perror("failed to mmap file"); |
| 1026 | exit(-1); |
| 1027 | } |
| 1028 | |
| 1029 | more: |
| 1030 | event = (event_t *)(buf + head); |
| 1031 | |
Peter Zijlstra | 6142f9e | 2009-05-26 20:51:47 +0200 | [diff] [blame] | 1032 | size = event->header.size; |
| 1033 | if (!size) |
| 1034 | size = 8; |
| 1035 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 1036 | if (head + event->header.size >= page_size * mmap_window) { |
| 1037 | unsigned long shift = page_size * (head / page_size); |
| 1038 | int ret; |
| 1039 | |
| 1040 | ret = munmap(buf, page_size * mmap_window); |
| 1041 | assert(ret == 0); |
| 1042 | |
| 1043 | offset += shift; |
| 1044 | head -= shift; |
| 1045 | goto remap; |
| 1046 | } |
| 1047 | |
Peter Zijlstra | 6142f9e | 2009-05-26 20:51:47 +0200 | [diff] [blame] | 1048 | size = event->header.size; |
| 1049 | if (!size) |
| 1050 | goto broken_event; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 1051 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 1052 | if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) { |
| 1053 | char level; |
| 1054 | int show = 0; |
| 1055 | struct dso *dso = NULL; |
| 1056 | struct thread *thread = threads__findnew(event->ip.pid); |
Peter Zijlstra | f17e04a | 2009-05-26 15:30:22 +0200 | [diff] [blame] | 1057 | uint64_t ip = event->ip.ip; |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 1058 | struct map *map = NULL; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 1059 | |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 1060 | if (dump_trace) { |
Ingo Molnar | f49515b | 2009-05-26 19:03:36 +0200 | [diff] [blame] | 1061 | fprintf(stderr, "%p [%p]: PERF_EVENT (IP, %d): %d: %p\n", |
| 1062 | (void *)(offset + head), |
| 1063 | (void *)(long)(event->header.size), |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 1064 | event->header.misc, |
| 1065 | event->ip.pid, |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 1066 | (void *)(long)ip); |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 1067 | } |
| 1068 | |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 1069 | if (thread == NULL) { |
| 1070 | fprintf(stderr, "problem processing %d event, bailing out\n", |
| 1071 | event->header.type); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 1072 | goto done; |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 1073 | } |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 1074 | |
| 1075 | if (event->header.misc & PERF_EVENT_MISC_KERNEL) { |
| 1076 | show = SHOW_KERNEL; |
| 1077 | level = 'k'; |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 1078 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 1079 | dso = kernel_dso; |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 1080 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 1081 | } else if (event->header.misc & PERF_EVENT_MISC_USER) { |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 1082 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 1083 | show = SHOW_USER; |
| 1084 | level = '.'; |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 1085 | |
| 1086 | map = thread__find_map(thread, ip); |
Peter Zijlstra | f17e04a | 2009-05-26 15:30:22 +0200 | [diff] [blame] | 1087 | if (map != NULL) { |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 1088 | dso = map->dso; |
Peter Zijlstra | f17e04a | 2009-05-26 15:30:22 +0200 | [diff] [blame] | 1089 | ip -= map->start + map->pgoff; |
| 1090 | } |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 1091 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 1092 | } else { |
| 1093 | show = SHOW_HV; |
| 1094 | level = 'H'; |
| 1095 | } |
| 1096 | |
| 1097 | if (show & show_mask) { |
Peter Zijlstra | f17e04a | 2009-05-26 15:30:22 +0200 | [diff] [blame] | 1098 | struct symbol *sym = dso__find_symbol(dso, ip); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 1099 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 1100 | if (hist_entry__add(thread, map, dso, sym, ip, level)) { |
| 1101 | fprintf(stderr, |
| 1102 | "problem incrementing symbol count, bailing out\n"); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 1103 | goto done; |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 1104 | } |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 1105 | } |
| 1106 | total++; |
| 1107 | } else switch (event->header.type) { |
| 1108 | case PERF_EVENT_MMAP: { |
| 1109 | struct thread *thread = threads__findnew(event->mmap.pid); |
| 1110 | struct map *map = map__new(&event->mmap); |
| 1111 | |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 1112 | if (dump_trace) { |
Ingo Molnar | f49515b | 2009-05-26 19:03:36 +0200 | [diff] [blame] | 1113 | fprintf(stderr, "%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n", |
| 1114 | (void *)(offset + head), |
| 1115 | (void *)(long)(event->header.size), |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 1116 | (void *)(long)event->mmap.start, |
| 1117 | (void *)(long)event->mmap.len, |
| 1118 | (void *)(long)event->mmap.pgoff, |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 1119 | event->mmap.filename); |
| 1120 | } |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 1121 | if (thread == NULL || map == NULL) { |
| 1122 | fprintf(stderr, "problem processing PERF_EVENT_MMAP, bailing out\n"); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 1123 | goto done; |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 1124 | } |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 1125 | thread__insert_map(thread, map); |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 1126 | total_mmap++; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 1127 | break; |
| 1128 | } |
| 1129 | case PERF_EVENT_COMM: { |
| 1130 | struct thread *thread = threads__findnew(event->comm.pid); |
| 1131 | |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 1132 | if (dump_trace) { |
Ingo Molnar | f49515b | 2009-05-26 19:03:36 +0200 | [diff] [blame] | 1133 | fprintf(stderr, "%p [%p]: PERF_EVENT_COMM: %s:%d\n", |
| 1134 | (void *)(offset + head), |
| 1135 | (void *)(long)(event->header.size), |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 1136 | event->comm.comm, event->comm.pid); |
| 1137 | } |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 1138 | if (thread == NULL || |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 1139 | thread__set_comm(thread, event->comm.comm)) { |
| 1140 | fprintf(stderr, "problem processing PERF_EVENT_COMM, bailing out\n"); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 1141 | goto done; |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 1142 | } |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 1143 | total_comm++; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 1144 | break; |
| 1145 | } |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 1146 | default: { |
Peter Zijlstra | 6142f9e | 2009-05-26 20:51:47 +0200 | [diff] [blame] | 1147 | broken_event: |
Peter Zijlstra | b7a16ea | 2009-05-27 13:35:35 +0200 | [diff] [blame] | 1148 | if (dump_trace) |
| 1149 | fprintf(stderr, "%p [%p]: skipping unknown header type: %d\n", |
| 1150 | (void *)(offset + head), |
| 1151 | (void *)(long)(event->header.size), |
| 1152 | event->header.type); |
| 1153 | |
Ingo Molnar | 3e70611 | 2009-05-26 18:53:17 +0200 | [diff] [blame] | 1154 | total_unknown++; |
Peter Zijlstra | 6142f9e | 2009-05-26 20:51:47 +0200 | [diff] [blame] | 1155 | |
| 1156 | /* |
| 1157 | * assume we lost track of the stream, check alignment, and |
| 1158 | * increment a single u64 in the hope to catch on again 'soon'. |
| 1159 | */ |
| 1160 | |
| 1161 | if (unlikely(head & 7)) |
| 1162 | head &= ~7ULL; |
| 1163 | |
| 1164 | size = 8; |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 1165 | } |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 1166 | } |
| 1167 | |
Peter Zijlstra | 6142f9e | 2009-05-26 20:51:47 +0200 | [diff] [blame] | 1168 | head += size; |
Ingo Molnar | f49515b | 2009-05-26 19:03:36 +0200 | [diff] [blame] | 1169 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 1170 | if (offset + head < stat.st_size) |
| 1171 | goto more; |
| 1172 | |
| 1173 | rc = EXIT_SUCCESS; |
| 1174 | done: |
| 1175 | close(input); |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 1176 | |
| 1177 | if (dump_trace) { |
Ingo Molnar | 3e70611 | 2009-05-26 18:53:17 +0200 | [diff] [blame] | 1178 | fprintf(stderr, " IP events: %10ld\n", total); |
| 1179 | fprintf(stderr, " mmap events: %10ld\n", total_mmap); |
| 1180 | fprintf(stderr, " comm events: %10ld\n", total_comm); |
| 1181 | fprintf(stderr, " unknown events: %10ld\n", total_unknown); |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 1182 | |
| 1183 | return 0; |
| 1184 | } |
| 1185 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 1186 | if (verbose >= 2) |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 1187 | dsos__fprintf(stdout); |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 1188 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 1189 | output__resort(); |
| 1190 | output__fprintf(stdout, total); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 1191 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 1192 | return rc; |
| 1193 | } |
| 1194 | |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1195 | static const char * const report_usage[] = { |
| 1196 | "perf report [<options>] <command>", |
| 1197 | NULL |
| 1198 | }; |
| 1199 | |
| 1200 | static const struct option options[] = { |
| 1201 | OPT_STRING('i', "input", &input_name, "file", |
| 1202 | "input file name"), |
Arnaldo Carvalho de Melo | 815e777 | 2009-05-26 19:46:14 -0300 | [diff] [blame] | 1203 | OPT_BOOLEAN('v', "verbose", &verbose, |
| 1204 | "be more verbose (show symbol address, etc)"), |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 1205 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, |
| 1206 | "dump raw trace in ASCII"), |
Peter Zijlstra | 450aaa2 | 2009-05-27 20:20:23 +0200 | [diff] [blame] | 1207 | OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"), |
Peter Zijlstra | 37f440c | 2009-05-27 20:20:26 +0200 | [diff] [blame] | 1208 | OPT_STRING('s', "sort", &sort_order, "foo", "bar"), |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1209 | OPT_END() |
| 1210 | }; |
| 1211 | |
| 1212 | int cmd_report(int argc, const char **argv, const char *prefix) |
| 1213 | { |
| 1214 | elf_version(EV_CURRENT); |
| 1215 | |
| 1216 | page_size = getpagesize(); |
| 1217 | |
| 1218 | parse_options(argc, argv, options, report_usage, 0); |
| 1219 | |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 1220 | setup_sorting(); |
| 1221 | |
Ingo Molnar | a930d2c | 2009-05-27 09:50:13 +0200 | [diff] [blame] | 1222 | setup_pager(); |
| 1223 | |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1224 | return __cmd_report(); |
| 1225 | } |