Ingo Molnar | bf9e187 | 2009-06-02 23:37:05 +0200 | [diff] [blame] | 1 | /* |
| 2 | * builtin-report.c |
| 3 | * |
| 4 | * Builtin report command: Analyze the perf.data input file, |
| 5 | * look up and read DSOs and symbol information and display |
| 6 | * a histogram of results, along various sorting keys. |
| 7 | */ |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 8 | #include "builtin.h" |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 9 | |
Ingo Molnar | bf9e187 | 2009-06-02 23:37:05 +0200 | [diff] [blame] | 10 | #include "util/util.h" |
| 11 | |
Arnaldo Carvalho de Melo | 35a50c8 | 2009-05-18 16:24:49 -0300 | [diff] [blame] | 12 | #include "util/list.h" |
Ingo Molnar | a930d2c | 2009-05-27 09:50:13 +0200 | [diff] [blame] | 13 | #include "util/cache.h" |
Arnaldo Carvalho de Melo | 35a50c8 | 2009-05-18 16:24:49 -0300 | [diff] [blame] | 14 | #include "util/rbtree.h" |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 15 | #include "util/symbol.h" |
Arnaldo Carvalho de Melo | a0055ae | 2009-06-01 17:50:19 -0300 | [diff] [blame] | 16 | #include "util/string.h" |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 17 | |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 18 | #include "perf.h" |
| 19 | |
| 20 | #include "util/parse-options.h" |
| 21 | #include "util/parse-events.h" |
| 22 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 23 | #define SHOW_KERNEL 1 |
| 24 | #define SHOW_USER 2 |
| 25 | #define SHOW_HV 4 |
| 26 | |
Ingo Molnar | 23ac9cb | 2009-05-27 09:33:18 +0200 | [diff] [blame] | 27 | static char const *input_name = "perf.data"; |
Peter Zijlstra | 450aaa2 | 2009-05-27 20:20:23 +0200 | [diff] [blame] | 28 | static char *vmlinux = NULL; |
Peter Zijlstra | f70e87d | 2009-06-02 14:13:24 +0200 | [diff] [blame] | 29 | static char *sort_order = "comm,dso"; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 30 | static int input; |
| 31 | static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV; |
| 32 | |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 33 | static int dump_trace = 0; |
Ingo Molnar | 3502973 | 2009-06-03 09:38:58 +0200 | [diff] [blame] | 34 | #define dprintf(x...) do { if (dump_trace) printf(x); } while (0) |
| 35 | |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 36 | static int verbose; |
Arnaldo Carvalho de Melo | b78c07d | 2009-05-29 13:48:59 -0300 | [diff] [blame] | 37 | static int full_paths; |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 38 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 39 | static unsigned long page_size; |
| 40 | static unsigned long mmap_window = 32; |
| 41 | |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 42 | const char *perf_event_names[] = { |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 43 | [PERF_EVENT_MMAP] = " PERF_EVENT_MMAP", |
| 44 | [PERF_EVENT_MUNMAP] = " PERF_EVENT_MUNMAP", |
| 45 | [PERF_EVENT_COMM] = " PERF_EVENT_COMM", |
| 46 | }; |
| 47 | |
| 48 | struct ip_event { |
| 49 | struct perf_event_header header; |
| 50 | __u64 ip; |
| 51 | __u32 pid, tid; |
| 52 | }; |
| 53 | struct mmap_event { |
| 54 | struct perf_event_header header; |
| 55 | __u32 pid, tid; |
| 56 | __u64 start; |
| 57 | __u64 len; |
| 58 | __u64 pgoff; |
| 59 | char filename[PATH_MAX]; |
| 60 | }; |
| 61 | struct comm_event { |
| 62 | struct perf_event_header header; |
| 63 | __u32 pid,tid; |
| 64 | char comm[16]; |
| 65 | }; |
| 66 | |
| 67 | typedef union event_union { |
| 68 | struct perf_event_header header; |
| 69 | struct ip_event ip; |
| 70 | struct mmap_event mmap; |
| 71 | struct comm_event comm; |
| 72 | } event_t; |
| 73 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 74 | static LIST_HEAD(dsos); |
| 75 | static struct dso *kernel_dso; |
| 76 | |
| 77 | static void dsos__add(struct dso *dso) |
| 78 | { |
| 79 | list_add_tail(&dso->node, &dsos); |
| 80 | } |
| 81 | |
| 82 | static struct dso *dsos__find(const char *name) |
| 83 | { |
| 84 | struct dso *pos; |
| 85 | |
| 86 | list_for_each_entry(pos, &dsos, node) |
| 87 | if (strcmp(pos->name, name) == 0) |
| 88 | return pos; |
| 89 | return NULL; |
| 90 | } |
| 91 | |
| 92 | static struct dso *dsos__findnew(const char *name) |
| 93 | { |
| 94 | struct dso *dso = dsos__find(name); |
Peter Zijlstra | b7a16ea | 2009-05-27 13:35:35 +0200 | [diff] [blame] | 95 | int nr; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 96 | |
Ingo Molnar | 4593bba | 2009-06-02 15:34:25 +0200 | [diff] [blame] | 97 | if (dso) |
| 98 | return dso; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 99 | |
Ingo Molnar | 4593bba | 2009-06-02 15:34:25 +0200 | [diff] [blame] | 100 | dso = dso__new(name, 0); |
| 101 | if (!dso) |
| 102 | goto out_delete_dso; |
Peter Zijlstra | b7a16ea | 2009-05-27 13:35:35 +0200 | [diff] [blame] | 103 | |
Ingo Molnar | 4593bba | 2009-06-02 15:34:25 +0200 | [diff] [blame] | 104 | nr = dso__load(dso, NULL); |
| 105 | if (nr < 0) { |
| 106 | fprintf(stderr, "Failed to open: %s\n", name); |
| 107 | goto out_delete_dso; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 108 | } |
Ingo Molnar | 4593bba | 2009-06-02 15:34:25 +0200 | [diff] [blame] | 109 | if (!nr && verbose) { |
| 110 | fprintf(stderr, |
| 111 | "No symbols found in: %s, maybe install a debug package?\n", |
| 112 | name); |
| 113 | } |
| 114 | |
| 115 | dsos__add(dso); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 116 | |
| 117 | return dso; |
| 118 | |
| 119 | out_delete_dso: |
| 120 | dso__delete(dso); |
| 121 | return NULL; |
| 122 | } |
| 123 | |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 124 | static void dsos__fprintf(FILE *fp) |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 125 | { |
| 126 | struct dso *pos; |
| 127 | |
| 128 | list_for_each_entry(pos, &dsos, node) |
| 129 | dso__fprintf(pos, fp); |
| 130 | } |
| 131 | |
Peter Zijlstra | 450aaa2 | 2009-05-27 20:20:23 +0200 | [diff] [blame] | 132 | static int load_kernel(void) |
| 133 | { |
Arnaldo Carvalho de Melo | a827c87 | 2009-05-28 14:55:19 -0300 | [diff] [blame] | 134 | int err; |
Peter Zijlstra | 450aaa2 | 2009-05-27 20:20:23 +0200 | [diff] [blame] | 135 | |
Arnaldo Carvalho de Melo | 0085c954 | 2009-05-28 14:55:13 -0300 | [diff] [blame] | 136 | kernel_dso = dso__new("[kernel]", 0); |
Peter Zijlstra | 450aaa2 | 2009-05-27 20:20:23 +0200 | [diff] [blame] | 137 | if (!kernel_dso) |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 138 | return -1; |
Peter Zijlstra | 450aaa2 | 2009-05-27 20:20:23 +0200 | [diff] [blame] | 139 | |
Arnaldo Carvalho de Melo | 69ee69f | 2009-05-28 14:55:26 -0300 | [diff] [blame] | 140 | err = dso__load_kernel(kernel_dso, vmlinux, NULL); |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 141 | if (err) { |
| 142 | dso__delete(kernel_dso); |
| 143 | kernel_dso = NULL; |
| 144 | } else |
| 145 | dsos__add(kernel_dso); |
Peter Zijlstra | 450aaa2 | 2009-05-27 20:20:23 +0200 | [diff] [blame] | 146 | |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 147 | return err; |
Peter Zijlstra | 450aaa2 | 2009-05-27 20:20:23 +0200 | [diff] [blame] | 148 | } |
| 149 | |
Arnaldo Carvalho de Melo | b78c07d | 2009-05-29 13:48:59 -0300 | [diff] [blame] | 150 | static int strcommon(const char *pathname, const char *cwd, int cwdlen) |
| 151 | { |
| 152 | int n = 0; |
| 153 | |
| 154 | while (pathname[n] == cwd[n] && n < cwdlen) |
| 155 | ++n; |
| 156 | |
| 157 | return n; |
| 158 | } |
| 159 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 160 | struct map { |
| 161 | struct list_head node; |
| 162 | uint64_t start; |
| 163 | uint64_t end; |
| 164 | uint64_t pgoff; |
| 165 | struct dso *dso; |
| 166 | }; |
| 167 | |
Arnaldo Carvalho de Melo | b78c07d | 2009-05-29 13:48:59 -0300 | [diff] [blame] | 168 | static struct map *map__new(struct mmap_event *event, char *cwd, int cwdlen) |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 169 | { |
| 170 | struct map *self = malloc(sizeof(*self)); |
| 171 | |
| 172 | if (self != NULL) { |
Arnaldo Carvalho de Melo | b78c07d | 2009-05-29 13:48:59 -0300 | [diff] [blame] | 173 | const char *filename = event->filename; |
| 174 | char newfilename[PATH_MAX]; |
| 175 | |
| 176 | if (cwd) { |
| 177 | int n = strcommon(filename, cwd, cwdlen); |
| 178 | if (n == cwdlen) { |
| 179 | snprintf(newfilename, sizeof(newfilename), |
| 180 | ".%s", filename + n); |
| 181 | filename = newfilename; |
| 182 | } |
| 183 | } |
| 184 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 185 | self->start = event->start; |
| 186 | self->end = event->start + event->len; |
| 187 | self->pgoff = event->pgoff; |
| 188 | |
Arnaldo Carvalho de Melo | b78c07d | 2009-05-29 13:48:59 -0300 | [diff] [blame] | 189 | self->dso = dsos__findnew(filename); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 190 | if (self->dso == NULL) |
| 191 | goto out_delete; |
| 192 | } |
| 193 | return self; |
| 194 | out_delete: |
| 195 | free(self); |
| 196 | return NULL; |
| 197 | } |
| 198 | |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 199 | struct thread; |
| 200 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 201 | struct thread { |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 202 | struct rb_node rb_node; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 203 | struct list_head maps; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 204 | pid_t pid; |
| 205 | char *comm; |
| 206 | }; |
| 207 | |
| 208 | static struct thread *thread__new(pid_t pid) |
| 209 | { |
| 210 | struct thread *self = malloc(sizeof(*self)); |
| 211 | |
| 212 | if (self != NULL) { |
| 213 | self->pid = pid; |
Ingo Molnar | 0a520c6 | 2009-06-02 23:24:45 +0200 | [diff] [blame] | 214 | self->comm = malloc(30); |
| 215 | if (self->comm) |
| 216 | sprintf(self->comm, ":%d", pid); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 217 | INIT_LIST_HEAD(&self->maps); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | return self; |
| 221 | } |
| 222 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 223 | static int thread__set_comm(struct thread *self, const char *comm) |
| 224 | { |
| 225 | self->comm = strdup(comm); |
| 226 | return self->comm ? 0 : -ENOMEM; |
| 227 | } |
| 228 | |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 229 | static struct rb_root threads; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 230 | |
| 231 | static struct thread *threads__findnew(pid_t pid) |
| 232 | { |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 233 | struct rb_node **p = &threads.rb_node; |
| 234 | struct rb_node *parent = NULL; |
| 235 | struct thread *th; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 236 | |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 237 | while (*p != NULL) { |
| 238 | parent = *p; |
| 239 | th = rb_entry(parent, struct thread, rb_node); |
| 240 | |
| 241 | if (th->pid == pid) |
| 242 | return th; |
| 243 | |
| 244 | if (pid < th->pid) |
| 245 | p = &(*p)->rb_left; |
| 246 | else |
| 247 | p = &(*p)->rb_right; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 248 | } |
| 249 | |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 250 | th = thread__new(pid); |
| 251 | if (th != NULL) { |
| 252 | rb_link_node(&th->rb_node, parent, p); |
| 253 | rb_insert_color(&th->rb_node, &threads); |
| 254 | } |
| 255 | return th; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | static void thread__insert_map(struct thread *self, struct map *map) |
| 259 | { |
| 260 | list_add_tail(&map->node, &self->maps); |
| 261 | } |
| 262 | |
| 263 | static struct map *thread__find_map(struct thread *self, uint64_t ip) |
| 264 | { |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 265 | struct map *pos; |
| 266 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 267 | if (self == NULL) |
| 268 | return NULL; |
| 269 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 270 | list_for_each_entry(pos, &self->maps, node) |
| 271 | if (ip >= pos->start && ip <= pos->end) |
| 272 | return pos; |
| 273 | |
| 274 | return NULL; |
| 275 | } |
| 276 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 277 | /* |
| 278 | * histogram, sorted on item, collects counts |
| 279 | */ |
| 280 | |
| 281 | static struct rb_root hist; |
| 282 | |
| 283 | struct hist_entry { |
| 284 | struct rb_node rb_node; |
| 285 | |
| 286 | struct thread *thread; |
| 287 | struct map *map; |
| 288 | struct dso *dso; |
| 289 | struct symbol *sym; |
| 290 | uint64_t ip; |
| 291 | char level; |
| 292 | |
| 293 | uint32_t count; |
| 294 | }; |
| 295 | |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 296 | /* |
| 297 | * configurable sorting bits |
| 298 | */ |
| 299 | |
| 300 | struct sort_entry { |
| 301 | struct list_head list; |
| 302 | |
Peter Zijlstra | ca8cdee | 2009-05-28 11:08:33 +0200 | [diff] [blame] | 303 | char *header; |
| 304 | |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 305 | int64_t (*cmp)(struct hist_entry *, struct hist_entry *); |
| 306 | size_t (*print)(FILE *fp, struct hist_entry *); |
| 307 | }; |
| 308 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 309 | static int64_t |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 310 | sort__thread_cmp(struct hist_entry *left, struct hist_entry *right) |
| 311 | { |
| 312 | return right->thread->pid - left->thread->pid; |
| 313 | } |
| 314 | |
| 315 | static size_t |
| 316 | sort__thread_print(FILE *fp, struct hist_entry *self) |
| 317 | { |
Ingo Molnar | cf25c63 | 2009-06-02 22:12:14 +0200 | [diff] [blame] | 318 | return fprintf(fp, " %16s:%5d", self->thread->comm ?: "", self->thread->pid); |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | static struct sort_entry sort_thread = { |
Ingo Molnar | cf25c63 | 2009-06-02 22:12:14 +0200 | [diff] [blame] | 322 | .header = " Command: Pid ", |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 323 | .cmp = sort__thread_cmp, |
| 324 | .print = sort__thread_print, |
| 325 | }; |
| 326 | |
| 327 | static int64_t |
Peter Zijlstra | 992444b | 2009-05-27 20:20:27 +0200 | [diff] [blame] | 328 | sort__comm_cmp(struct hist_entry *left, struct hist_entry *right) |
| 329 | { |
| 330 | char *comm_l = left->thread->comm; |
| 331 | char *comm_r = right->thread->comm; |
| 332 | |
| 333 | if (!comm_l || !comm_r) { |
| 334 | if (!comm_l && !comm_r) |
| 335 | return 0; |
| 336 | else if (!comm_l) |
| 337 | return -1; |
| 338 | else |
| 339 | return 1; |
| 340 | } |
| 341 | |
| 342 | return strcmp(comm_l, comm_r); |
| 343 | } |
| 344 | |
| 345 | static size_t |
| 346 | sort__comm_print(FILE *fp, struct hist_entry *self) |
| 347 | { |
Ingo Molnar | 0a520c6 | 2009-06-02 23:24:45 +0200 | [diff] [blame] | 348 | return fprintf(fp, " %16s", self->thread->comm); |
Peter Zijlstra | 992444b | 2009-05-27 20:20:27 +0200 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | static struct sort_entry sort_comm = { |
Ingo Molnar | 4593bba | 2009-06-02 15:34:25 +0200 | [diff] [blame] | 352 | .header = " Command", |
Peter Zijlstra | 992444b | 2009-05-27 20:20:27 +0200 | [diff] [blame] | 353 | .cmp = sort__comm_cmp, |
| 354 | .print = sort__comm_print, |
| 355 | }; |
| 356 | |
| 357 | static int64_t |
Peter Zijlstra | 55e5ec4 | 2009-05-27 20:20:28 +0200 | [diff] [blame] | 358 | sort__dso_cmp(struct hist_entry *left, struct hist_entry *right) |
| 359 | { |
| 360 | struct dso *dso_l = left->dso; |
| 361 | struct dso *dso_r = right->dso; |
| 362 | |
| 363 | if (!dso_l || !dso_r) { |
| 364 | if (!dso_l && !dso_r) |
| 365 | return 0; |
| 366 | else if (!dso_l) |
| 367 | return -1; |
| 368 | else |
| 369 | return 1; |
| 370 | } |
| 371 | |
| 372 | return strcmp(dso_l->name, dso_r->name); |
| 373 | } |
| 374 | |
| 375 | static size_t |
| 376 | sort__dso_print(FILE *fp, struct hist_entry *self) |
| 377 | { |
Ingo Molnar | 0a520c6 | 2009-06-02 23:24:45 +0200 | [diff] [blame] | 378 | if (self->dso) |
| 379 | return fprintf(fp, " %-25s", self->dso->name); |
| 380 | |
| 381 | return fprintf(fp, " %016llx", (__u64)self->ip); |
Peter Zijlstra | 55e5ec4 | 2009-05-27 20:20:28 +0200 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | static struct sort_entry sort_dso = { |
Ingo Molnar | cf25c63 | 2009-06-02 22:12:14 +0200 | [diff] [blame] | 385 | .header = " Shared Object ", |
Peter Zijlstra | 55e5ec4 | 2009-05-27 20:20:28 +0200 | [diff] [blame] | 386 | .cmp = sort__dso_cmp, |
| 387 | .print = sort__dso_print, |
| 388 | }; |
| 389 | |
| 390 | static int64_t |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 391 | 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] | 392 | { |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 393 | uint64_t ip_l, ip_r; |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 394 | |
| 395 | if (left->sym == right->sym) |
| 396 | return 0; |
| 397 | |
| 398 | ip_l = left->sym ? left->sym->start : left->ip; |
| 399 | ip_r = right->sym ? right->sym->start : right->ip; |
| 400 | |
| 401 | return (int64_t)(ip_r - ip_l); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 402 | } |
| 403 | |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 404 | static size_t |
| 405 | sort__sym_print(FILE *fp, struct hist_entry *self) |
| 406 | { |
| 407 | size_t ret = 0; |
| 408 | |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 409 | if (verbose) |
Ingo Molnar | 0a520c6 | 2009-06-02 23:24:45 +0200 | [diff] [blame] | 410 | ret += fprintf(fp, " %#018llx", (__u64)self->ip); |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 411 | |
Ingo Molnar | 0a520c6 | 2009-06-02 23:24:45 +0200 | [diff] [blame] | 412 | if (self->dso) |
| 413 | ret += fprintf(fp, " %s: ", self->dso->name); |
| 414 | else |
| 415 | ret += fprintf(fp, " %#016llx: ", (__u64)self->ip); |
| 416 | |
| 417 | if (self->sym) |
| 418 | ret += fprintf(fp, "%s", self->sym->name); |
| 419 | else |
| 420 | ret += fprintf(fp, "%#016llx", (__u64)self->ip); |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 421 | |
| 422 | return ret; |
| 423 | } |
| 424 | |
| 425 | static struct sort_entry sort_sym = { |
Ingo Molnar | 4593bba | 2009-06-02 15:34:25 +0200 | [diff] [blame] | 426 | .header = " Shared Object: Symbol", |
Peter Zijlstra | ca8cdee | 2009-05-28 11:08:33 +0200 | [diff] [blame] | 427 | .cmp = sort__sym_cmp, |
| 428 | .print = sort__sym_print, |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 429 | }; |
| 430 | |
Peter Zijlstra | 37f440c | 2009-05-27 20:20:26 +0200 | [diff] [blame] | 431 | struct sort_dimension { |
| 432 | char *name; |
| 433 | struct sort_entry *entry; |
| 434 | int taken; |
| 435 | }; |
| 436 | |
| 437 | static struct sort_dimension sort_dimensions[] = { |
| 438 | { .name = "pid", .entry = &sort_thread, }, |
Peter Zijlstra | 992444b | 2009-05-27 20:20:27 +0200 | [diff] [blame] | 439 | { .name = "comm", .entry = &sort_comm, }, |
Peter Zijlstra | 55e5ec4 | 2009-05-27 20:20:28 +0200 | [diff] [blame] | 440 | { .name = "dso", .entry = &sort_dso, }, |
Peter Zijlstra | 37f440c | 2009-05-27 20:20:26 +0200 | [diff] [blame] | 441 | { .name = "symbol", .entry = &sort_sym, }, |
| 442 | }; |
| 443 | |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 444 | static LIST_HEAD(hist_entry__sort_list); |
| 445 | |
Peter Zijlstra | 37f440c | 2009-05-27 20:20:26 +0200 | [diff] [blame] | 446 | static int sort_dimension__add(char *tok) |
| 447 | { |
| 448 | int i; |
| 449 | |
| 450 | for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) { |
| 451 | struct sort_dimension *sd = &sort_dimensions[i]; |
| 452 | |
| 453 | if (sd->taken) |
| 454 | continue; |
| 455 | |
Ingo Molnar | 5352f35 | 2009-06-03 10:07:39 +0200 | [diff] [blame^] | 456 | if (strncasecmp(tok, sd->name, strlen(tok))) |
Peter Zijlstra | 37f440c | 2009-05-27 20:20:26 +0200 | [diff] [blame] | 457 | continue; |
| 458 | |
| 459 | list_add_tail(&sd->entry->list, &hist_entry__sort_list); |
| 460 | sd->taken = 1; |
Ingo Molnar | 5352f35 | 2009-06-03 10:07:39 +0200 | [diff] [blame^] | 461 | |
Peter Zijlstra | 37f440c | 2009-05-27 20:20:26 +0200 | [diff] [blame] | 462 | return 0; |
| 463 | } |
| 464 | |
| 465 | return -ESRCH; |
| 466 | } |
| 467 | |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 468 | static int64_t |
| 469 | hist_entry__cmp(struct hist_entry *left, struct hist_entry *right) |
| 470 | { |
| 471 | struct sort_entry *se; |
| 472 | int64_t cmp = 0; |
| 473 | |
| 474 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
| 475 | cmp = se->cmp(left, right); |
| 476 | if (cmp) |
| 477 | break; |
| 478 | } |
| 479 | |
| 480 | return cmp; |
| 481 | } |
| 482 | |
| 483 | static size_t |
| 484 | hist_entry__fprintf(FILE *fp, struct hist_entry *self, uint64_t total_samples) |
| 485 | { |
| 486 | struct sort_entry *se; |
| 487 | size_t ret; |
| 488 | |
| 489 | if (total_samples) { |
Ingo Molnar | 2d65537 | 2009-05-27 21:36:22 +0200 | [diff] [blame] | 490 | ret = fprintf(fp, " %5.2f%%", |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 491 | (self->count * 100.0) / total_samples); |
| 492 | } else |
| 493 | ret = fprintf(fp, "%12d ", self->count); |
| 494 | |
| 495 | list_for_each_entry(se, &hist_entry__sort_list, list) |
| 496 | ret += se->print(fp, self); |
| 497 | |
| 498 | ret += fprintf(fp, "\n"); |
| 499 | |
| 500 | return ret; |
| 501 | } |
| 502 | |
| 503 | /* |
| 504 | * collect histogram counts |
| 505 | */ |
| 506 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 507 | static int |
| 508 | hist_entry__add(struct thread *thread, struct map *map, struct dso *dso, |
| 509 | struct symbol *sym, uint64_t ip, char level) |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 510 | { |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 511 | struct rb_node **p = &hist.rb_node; |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 512 | struct rb_node *parent = NULL; |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 513 | struct hist_entry *he; |
| 514 | struct hist_entry entry = { |
| 515 | .thread = thread, |
| 516 | .map = map, |
| 517 | .dso = dso, |
| 518 | .sym = sym, |
| 519 | .ip = ip, |
| 520 | .level = level, |
| 521 | .count = 1, |
| 522 | }; |
| 523 | int cmp; |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 524 | |
| 525 | while (*p != NULL) { |
| 526 | parent = *p; |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 527 | he = rb_entry(parent, struct hist_entry, rb_node); |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 528 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 529 | cmp = hist_entry__cmp(&entry, he); |
| 530 | |
| 531 | if (!cmp) { |
| 532 | he->count++; |
| 533 | return 0; |
| 534 | } |
| 535 | |
| 536 | if (cmp < 0) |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 537 | p = &(*p)->rb_left; |
| 538 | else |
| 539 | p = &(*p)->rb_right; |
| 540 | } |
| 541 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 542 | he = malloc(sizeof(*he)); |
| 543 | if (!he) |
| 544 | return -ENOMEM; |
| 545 | *he = entry; |
| 546 | rb_link_node(&he->rb_node, parent, p); |
| 547 | rb_insert_color(&he->rb_node, &hist); |
| 548 | |
| 549 | return 0; |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 550 | } |
| 551 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 552 | /* |
| 553 | * reverse the map, sort on count. |
| 554 | */ |
| 555 | |
| 556 | static struct rb_root output_hists; |
| 557 | |
| 558 | static void output__insert_entry(struct hist_entry *he) |
| 559 | { |
| 560 | struct rb_node **p = &output_hists.rb_node; |
| 561 | struct rb_node *parent = NULL; |
| 562 | struct hist_entry *iter; |
| 563 | |
| 564 | while (*p != NULL) { |
| 565 | parent = *p; |
| 566 | iter = rb_entry(parent, struct hist_entry, rb_node); |
| 567 | |
| 568 | if (he->count > iter->count) |
| 569 | p = &(*p)->rb_left; |
| 570 | else |
| 571 | p = &(*p)->rb_right; |
| 572 | } |
| 573 | |
| 574 | rb_link_node(&he->rb_node, parent, p); |
| 575 | rb_insert_color(&he->rb_node, &output_hists); |
| 576 | } |
| 577 | |
| 578 | static void output__resort(void) |
| 579 | { |
| 580 | struct rb_node *next = rb_first(&hist); |
| 581 | struct hist_entry *n; |
| 582 | |
| 583 | while (next) { |
| 584 | n = rb_entry(next, struct hist_entry, rb_node); |
| 585 | next = rb_next(&n->rb_node); |
| 586 | |
| 587 | rb_erase(&n->rb_node, &hist); |
| 588 | output__insert_entry(n); |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 589 | } |
| 590 | } |
| 591 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 592 | 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] | 593 | { |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 594 | struct hist_entry *pos; |
Ingo Molnar | 2d65537 | 2009-05-27 21:36:22 +0200 | [diff] [blame] | 595 | struct sort_entry *se; |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 596 | struct rb_node *nd; |
| 597 | size_t ret = 0; |
| 598 | |
Peter Zijlstra | ca8cdee | 2009-05-28 11:08:33 +0200 | [diff] [blame] | 599 | fprintf(fp, "#\n"); |
| 600 | |
| 601 | fprintf(fp, "# Overhead"); |
| 602 | list_for_each_entry(se, &hist_entry__sort_list, list) |
| 603 | fprintf(fp, " %s", se->header); |
| 604 | fprintf(fp, "\n"); |
| 605 | |
| 606 | fprintf(fp, "# ........"); |
Ingo Molnar | 2d65537 | 2009-05-27 21:36:22 +0200 | [diff] [blame] | 607 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
Peter Zijlstra | ca8cdee | 2009-05-28 11:08:33 +0200 | [diff] [blame] | 608 | int i; |
| 609 | |
Ingo Molnar | 4593bba | 2009-06-02 15:34:25 +0200 | [diff] [blame] | 610 | fprintf(fp, " "); |
| 611 | for (i = 0; i < strlen(se->header)-1; i++) |
Peter Zijlstra | ca8cdee | 2009-05-28 11:08:33 +0200 | [diff] [blame] | 612 | fprintf(fp, "."); |
Ingo Molnar | 2d65537 | 2009-05-27 21:36:22 +0200 | [diff] [blame] | 613 | } |
Peter Zijlstra | ca8cdee | 2009-05-28 11:08:33 +0200 | [diff] [blame] | 614 | fprintf(fp, "\n"); |
| 615 | |
| 616 | fprintf(fp, "#\n"); |
Ingo Molnar | 2d65537 | 2009-05-27 21:36:22 +0200 | [diff] [blame] | 617 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 618 | for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) { |
| 619 | pos = rb_entry(nd, struct hist_entry, rb_node); |
| 620 | ret += hist_entry__fprintf(fp, pos, total_samples); |
Arnaldo Carvalho de Melo | 3a4b8cc | 2009-05-26 16:19:04 -0300 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | return ret; |
| 624 | } |
| 625 | |
Peter Zijlstra | 436224a | 2009-06-02 21:02:36 +0200 | [diff] [blame] | 626 | static void register_idle_thread(void) |
| 627 | { |
| 628 | struct thread *thread = threads__findnew(0); |
| 629 | |
| 630 | if (thread == NULL || |
| 631 | thread__set_comm(thread, "[idle]")) { |
| 632 | fprintf(stderr, "problem inserting idle task.\n"); |
| 633 | exit(-1); |
| 634 | } |
| 635 | } |
| 636 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 637 | |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 638 | static int __cmd_report(void) |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 639 | { |
| 640 | unsigned long offset = 0; |
| 641 | unsigned long head = 0; |
| 642 | struct stat stat; |
| 643 | char *buf; |
| 644 | event_t *event; |
| 645 | int ret, rc = EXIT_FAILURE; |
Peter Zijlstra | 6142f9e | 2009-05-26 20:51:47 +0200 | [diff] [blame] | 646 | uint32_t size; |
Ingo Molnar | f49515b | 2009-05-26 19:03:36 +0200 | [diff] [blame] | 647 | unsigned long total = 0, total_mmap = 0, total_comm = 0, total_unknown = 0; |
Arnaldo Carvalho de Melo | b78c07d | 2009-05-29 13:48:59 -0300 | [diff] [blame] | 648 | char cwd[PATH_MAX], *cwdp = cwd; |
| 649 | int cwdlen; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 650 | |
Peter Zijlstra | 436224a | 2009-06-02 21:02:36 +0200 | [diff] [blame] | 651 | register_idle_thread(); |
| 652 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 653 | input = open(input_name, O_RDONLY); |
| 654 | if (input < 0) { |
| 655 | perror("failed to open file"); |
| 656 | exit(-1); |
| 657 | } |
| 658 | |
| 659 | ret = fstat(input, &stat); |
| 660 | if (ret < 0) { |
| 661 | perror("failed to stat file"); |
| 662 | exit(-1); |
| 663 | } |
| 664 | |
| 665 | if (!stat.st_size) { |
| 666 | fprintf(stderr, "zero-sized file, nothing to do!\n"); |
| 667 | exit(0); |
| 668 | } |
| 669 | |
Peter Zijlstra | 450aaa2 | 2009-05-27 20:20:23 +0200 | [diff] [blame] | 670 | if (load_kernel() < 0) { |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 671 | perror("failed to load kernel symbols"); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 672 | return EXIT_FAILURE; |
| 673 | } |
| 674 | |
Arnaldo Carvalho de Melo | b78c07d | 2009-05-29 13:48:59 -0300 | [diff] [blame] | 675 | if (!full_paths) { |
| 676 | if (getcwd(cwd, sizeof(cwd)) == NULL) { |
| 677 | perror("failed to get the current directory"); |
| 678 | return EXIT_FAILURE; |
| 679 | } |
| 680 | cwdlen = strlen(cwd); |
Mike Galbraith | 10a2825 | 2009-06-02 11:04:44 +0200 | [diff] [blame] | 681 | } else { |
Arnaldo Carvalho de Melo | b78c07d | 2009-05-29 13:48:59 -0300 | [diff] [blame] | 682 | cwdp = NULL; |
Mike Galbraith | 10a2825 | 2009-06-02 11:04:44 +0200 | [diff] [blame] | 683 | cwdlen = 0; |
| 684 | } |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 685 | remap: |
| 686 | buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ, |
| 687 | MAP_SHARED, input, offset); |
| 688 | if (buf == MAP_FAILED) { |
| 689 | perror("failed to mmap file"); |
| 690 | exit(-1); |
| 691 | } |
| 692 | |
| 693 | more: |
| 694 | event = (event_t *)(buf + head); |
| 695 | |
Peter Zijlstra | 6142f9e | 2009-05-26 20:51:47 +0200 | [diff] [blame] | 696 | size = event->header.size; |
| 697 | if (!size) |
| 698 | size = 8; |
| 699 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 700 | if (head + event->header.size >= page_size * mmap_window) { |
| 701 | unsigned long shift = page_size * (head / page_size); |
| 702 | int ret; |
| 703 | |
| 704 | ret = munmap(buf, page_size * mmap_window); |
| 705 | assert(ret == 0); |
| 706 | |
| 707 | offset += shift; |
| 708 | head -= shift; |
| 709 | goto remap; |
| 710 | } |
| 711 | |
Peter Zijlstra | 6142f9e | 2009-05-26 20:51:47 +0200 | [diff] [blame] | 712 | size = event->header.size; |
| 713 | if (!size) |
| 714 | goto broken_event; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 715 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 716 | if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) { |
| 717 | char level; |
| 718 | int show = 0; |
| 719 | struct dso *dso = NULL; |
| 720 | struct thread *thread = threads__findnew(event->ip.pid); |
Peter Zijlstra | f17e04a | 2009-05-26 15:30:22 +0200 | [diff] [blame] | 721 | uint64_t ip = event->ip.ip; |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 722 | struct map *map = NULL; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 723 | |
Ingo Molnar | 3502973 | 2009-06-03 09:38:58 +0200 | [diff] [blame] | 724 | dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n", |
| 725 | (void *)(offset + head), |
| 726 | (void *)(long)(event->header.size), |
| 727 | event->header.misc, |
| 728 | event->ip.pid, |
| 729 | (void *)(long)ip); |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 730 | |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 731 | if (thread == NULL) { |
Ingo Molnar | 5571731 | 2009-05-27 22:13:17 +0200 | [diff] [blame] | 732 | fprintf(stderr, "problem processing %d event, skipping it.\n", |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 733 | event->header.type); |
Ingo Molnar | 5571731 | 2009-05-27 22:13:17 +0200 | [diff] [blame] | 734 | goto broken_event; |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 735 | } |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 736 | |
| 737 | if (event->header.misc & PERF_EVENT_MISC_KERNEL) { |
| 738 | show = SHOW_KERNEL; |
| 739 | level = 'k'; |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 740 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 741 | dso = kernel_dso; |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 742 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 743 | } else if (event->header.misc & PERF_EVENT_MISC_USER) { |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 744 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 745 | show = SHOW_USER; |
| 746 | level = '.'; |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 747 | |
| 748 | map = thread__find_map(thread, ip); |
Peter Zijlstra | f17e04a | 2009-05-26 15:30:22 +0200 | [diff] [blame] | 749 | if (map != NULL) { |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 750 | dso = map->dso; |
Peter Zijlstra | f17e04a | 2009-05-26 15:30:22 +0200 | [diff] [blame] | 751 | ip -= map->start + map->pgoff; |
| 752 | } |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 753 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 754 | } else { |
| 755 | show = SHOW_HV; |
| 756 | level = 'H'; |
| 757 | } |
| 758 | |
| 759 | if (show & show_mask) { |
Peter Zijlstra | f17e04a | 2009-05-26 15:30:22 +0200 | [diff] [blame] | 760 | struct symbol *sym = dso__find_symbol(dso, ip); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 761 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 762 | if (hist_entry__add(thread, map, dso, sym, ip, level)) { |
| 763 | fprintf(stderr, |
Ingo Molnar | 5571731 | 2009-05-27 22:13:17 +0200 | [diff] [blame] | 764 | "problem incrementing symbol count, skipping event\n"); |
| 765 | goto broken_event; |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 766 | } |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 767 | } |
| 768 | total++; |
| 769 | } else switch (event->header.type) { |
| 770 | case PERF_EVENT_MMAP: { |
| 771 | struct thread *thread = threads__findnew(event->mmap.pid); |
Arnaldo Carvalho de Melo | b78c07d | 2009-05-29 13:48:59 -0300 | [diff] [blame] | 772 | struct map *map = map__new(&event->mmap, cwdp, cwdlen); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 773 | |
Ingo Molnar | 3502973 | 2009-06-03 09:38:58 +0200 | [diff] [blame] | 774 | dprintf("%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n", |
| 775 | (void *)(offset + head), |
| 776 | (void *)(long)(event->header.size), |
| 777 | (void *)(long)event->mmap.start, |
| 778 | (void *)(long)event->mmap.len, |
| 779 | (void *)(long)event->mmap.pgoff, |
| 780 | event->mmap.filename); |
| 781 | |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 782 | if (thread == NULL || map == NULL) { |
Ingo Molnar | 0a520c6 | 2009-06-02 23:24:45 +0200 | [diff] [blame] | 783 | if (verbose) |
| 784 | fprintf(stderr, "problem processing PERF_EVENT_MMAP, skipping event.\n"); |
Ingo Molnar | 5571731 | 2009-05-27 22:13:17 +0200 | [diff] [blame] | 785 | goto broken_event; |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 786 | } |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 787 | thread__insert_map(thread, map); |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 788 | total_mmap++; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 789 | break; |
| 790 | } |
| 791 | case PERF_EVENT_COMM: { |
| 792 | struct thread *thread = threads__findnew(event->comm.pid); |
| 793 | |
Ingo Molnar | 3502973 | 2009-06-03 09:38:58 +0200 | [diff] [blame] | 794 | dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n", |
| 795 | (void *)(offset + head), |
| 796 | (void *)(long)(event->header.size), |
| 797 | event->comm.comm, event->comm.pid); |
| 798 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 799 | if (thread == NULL || |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 800 | thread__set_comm(thread, event->comm.comm)) { |
Ingo Molnar | 5571731 | 2009-05-27 22:13:17 +0200 | [diff] [blame] | 801 | fprintf(stderr, "problem processing PERF_EVENT_COMM, skipping event.\n"); |
| 802 | goto broken_event; |
Arnaldo Carvalho de Melo | ce7e436 | 2009-05-19 09:30:23 -0300 | [diff] [blame] | 803 | } |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 804 | total_comm++; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 805 | break; |
| 806 | } |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 807 | default: { |
Peter Zijlstra | 6142f9e | 2009-05-26 20:51:47 +0200 | [diff] [blame] | 808 | broken_event: |
Ingo Molnar | 3502973 | 2009-06-03 09:38:58 +0200 | [diff] [blame] | 809 | dprintf("%p [%p]: skipping unknown header type: %d\n", |
| 810 | (void *)(offset + head), |
| 811 | (void *)(long)(event->header.size), |
| 812 | event->header.type); |
Peter Zijlstra | b7a16ea | 2009-05-27 13:35:35 +0200 | [diff] [blame] | 813 | |
Ingo Molnar | 3e70611 | 2009-05-26 18:53:17 +0200 | [diff] [blame] | 814 | total_unknown++; |
Peter Zijlstra | 6142f9e | 2009-05-26 20:51:47 +0200 | [diff] [blame] | 815 | |
| 816 | /* |
| 817 | * assume we lost track of the stream, check alignment, and |
| 818 | * increment a single u64 in the hope to catch on again 'soon'. |
| 819 | */ |
| 820 | |
| 821 | if (unlikely(head & 7)) |
| 822 | head &= ~7ULL; |
| 823 | |
| 824 | size = 8; |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 825 | } |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 826 | } |
| 827 | |
Peter Zijlstra | 6142f9e | 2009-05-26 20:51:47 +0200 | [diff] [blame] | 828 | head += size; |
Ingo Molnar | f49515b | 2009-05-26 19:03:36 +0200 | [diff] [blame] | 829 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 830 | if (offset + head < stat.st_size) |
| 831 | goto more; |
| 832 | |
| 833 | rc = EXIT_SUCCESS; |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 834 | close(input); |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 835 | |
Ingo Molnar | 3502973 | 2009-06-03 09:38:58 +0200 | [diff] [blame] | 836 | dprintf(" IP events: %10ld\n", total); |
| 837 | dprintf(" mmap events: %10ld\n", total_mmap); |
| 838 | dprintf(" comm events: %10ld\n", total_comm); |
| 839 | dprintf(" unknown events: %10ld\n", total_unknown); |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 840 | |
Ingo Molnar | 3502973 | 2009-06-03 09:38:58 +0200 | [diff] [blame] | 841 | if (dump_trace) |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 842 | return 0; |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 843 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 844 | if (verbose >= 2) |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 845 | dsos__fprintf(stdout); |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 846 | |
Peter Zijlstra | e7fb08b | 2009-05-27 20:20:24 +0200 | [diff] [blame] | 847 | output__resort(); |
| 848 | output__fprintf(stdout, total); |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 849 | |
Arnaldo Carvalho de Melo | 8fa66bd | 2009-05-18 12:45:42 -0300 | [diff] [blame] | 850 | return rc; |
| 851 | } |
| 852 | |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 853 | static const char * const report_usage[] = { |
| 854 | "perf report [<options>] <command>", |
| 855 | NULL |
| 856 | }; |
| 857 | |
| 858 | static const struct option options[] = { |
| 859 | OPT_STRING('i', "input", &input_name, "file", |
| 860 | "input file name"), |
Arnaldo Carvalho de Melo | 815e777 | 2009-05-26 19:46:14 -0300 | [diff] [blame] | 861 | OPT_BOOLEAN('v', "verbose", &verbose, |
| 862 | "be more verbose (show symbol address, etc)"), |
Ingo Molnar | 97b07b6 | 2009-05-26 18:48:58 +0200 | [diff] [blame] | 863 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, |
| 864 | "dump raw trace in ASCII"), |
Peter Zijlstra | 450aaa2 | 2009-05-27 20:20:23 +0200 | [diff] [blame] | 865 | OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"), |
Ingo Molnar | 63299f0 | 2009-05-28 10:52:00 +0200 | [diff] [blame] | 866 | OPT_STRING('s', "sort", &sort_order, "key[,key2...]", |
| 867 | "sort by key(s): pid, comm, dso, symbol. Default: pid,symbol"), |
Arnaldo Carvalho de Melo | b78c07d | 2009-05-29 13:48:59 -0300 | [diff] [blame] | 868 | OPT_BOOLEAN('P', "full-paths", &full_paths, |
| 869 | "Don't shorten the pathnames taking into account the cwd"), |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 870 | OPT_END() |
| 871 | }; |
| 872 | |
Ingo Molnar | 5352f35 | 2009-06-03 10:07:39 +0200 | [diff] [blame^] | 873 | static void setup_sorting(void) |
| 874 | { |
| 875 | char *tmp, *tok, *str = strdup(sort_order); |
| 876 | |
| 877 | for (tok = strtok_r(str, ", ", &tmp); |
| 878 | tok; tok = strtok_r(NULL, ", ", &tmp)) { |
| 879 | if (sort_dimension__add(tok) < 0) { |
| 880 | error("Unknown --sort key: `%s'", tok); |
| 881 | usage_with_options(report_usage, options); |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | free(str); |
| 886 | } |
| 887 | |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 888 | int cmd_report(int argc, const char **argv, const char *prefix) |
| 889 | { |
Arnaldo Carvalho de Melo | a2928c4 | 2009-05-28 14:55:04 -0300 | [diff] [blame] | 890 | symbol__init(); |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 891 | |
| 892 | page_size = getpagesize(); |
| 893 | |
| 894 | parse_options(argc, argv, options, report_usage, 0); |
| 895 | |
Peter Zijlstra | 1aa1673 | 2009-05-27 20:20:25 +0200 | [diff] [blame] | 896 | setup_sorting(); |
| 897 | |
Ingo Molnar | a930d2c | 2009-05-27 09:50:13 +0200 | [diff] [blame] | 898 | setup_pager(); |
| 899 | |
Ingo Molnar | 53cb8bc | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 900 | return __cmd_report(); |
| 901 | } |