Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 1 | /* |
| 2 | * builtin-annotate.c |
| 3 | * |
| 4 | * Builtin annotate 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 | */ |
| 8 | #include "builtin.h" |
| 9 | |
| 10 | #include "util/util.h" |
| 11 | |
| 12 | #include "util/color.h" |
Arnaldo Carvalho de Melo | 5da5025 | 2009-07-01 14:46:08 -0300 | [diff] [blame] | 13 | #include <linux/list.h> |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 14 | #include "util/cache.h" |
Arnaldo Carvalho de Melo | 43cbcd8 | 2009-07-01 12:28:37 -0300 | [diff] [blame] | 15 | #include <linux/rbtree.h> |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 16 | #include "util/symbol.h" |
| 17 | #include "util/string.h" |
| 18 | |
| 19 | #include "perf.h" |
Frederic Weisbecker | 8f28827 | 2009-08-16 22:05:48 +0200 | [diff] [blame] | 20 | #include "util/debug.h" |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 21 | |
| 22 | #include "util/parse-options.h" |
| 23 | #include "util/parse-events.h" |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 24 | #include "util/thread.h" |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame^] | 25 | #include "util/sort.h" |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 26 | |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 27 | static char const *input_name = "perf.data"; |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 28 | |
Peter Zijlstra | fa6963b | 2009-08-19 11:18:26 +0200 | [diff] [blame] | 29 | static int force; |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 30 | static int input; |
| 31 | static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV; |
| 32 | |
Mike Galbraith | 4297648 | 2009-07-02 08:09:46 +0200 | [diff] [blame] | 33 | static int full_paths; |
| 34 | |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 35 | static int print_line; |
| 36 | |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 37 | static unsigned long page_size; |
| 38 | static unsigned long mmap_window = 32; |
| 39 | |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 40 | static struct rb_root threads; |
| 41 | static struct thread *last_match; |
| 42 | |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 43 | |
| 44 | struct sym_ext { |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 45 | struct rb_node node; |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 46 | double percent; |
| 47 | char *path; |
| 48 | }; |
| 49 | |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 50 | /* |
| 51 | * histogram, sorted on item, collects counts |
| 52 | */ |
| 53 | |
| 54 | static struct rb_root hist; |
| 55 | |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 56 | static int64_t |
| 57 | hist_entry__cmp(struct hist_entry *left, struct hist_entry *right) |
| 58 | { |
| 59 | struct sort_entry *se; |
| 60 | int64_t cmp = 0; |
| 61 | |
| 62 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
| 63 | cmp = se->cmp(left, right); |
| 64 | if (cmp) |
| 65 | break; |
| 66 | } |
| 67 | |
| 68 | return cmp; |
| 69 | } |
| 70 | |
| 71 | static int64_t |
| 72 | hist_entry__collapse(struct hist_entry *left, struct hist_entry *right) |
| 73 | { |
| 74 | struct sort_entry *se; |
| 75 | int64_t cmp = 0; |
| 76 | |
| 77 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
| 78 | int64_t (*f)(struct hist_entry *, struct hist_entry *); |
| 79 | |
| 80 | f = se->collapse ?: se->cmp; |
| 81 | |
| 82 | cmp = f(left, right); |
| 83 | if (cmp) |
| 84 | break; |
| 85 | } |
| 86 | |
| 87 | return cmp; |
| 88 | } |
| 89 | |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 90 | /* |
| 91 | * collect histogram counts |
| 92 | */ |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 93 | static void hist_hit(struct hist_entry *he, u64 ip) |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 94 | { |
| 95 | unsigned int sym_size, offset; |
| 96 | struct symbol *sym = he->sym; |
| 97 | |
| 98 | he->count++; |
| 99 | |
| 100 | if (!sym || !sym->hist) |
| 101 | return; |
| 102 | |
| 103 | sym_size = sym->end - sym->start; |
| 104 | offset = ip - sym->start; |
| 105 | |
| 106 | if (offset >= sym_size) |
| 107 | return; |
| 108 | |
| 109 | sym->hist_sum++; |
| 110 | sym->hist[offset]++; |
| 111 | |
| 112 | if (verbose >= 3) |
| 113 | printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n", |
Arjan van de Ven | 7d37a0c | 2009-06-06 20:36:38 +0200 | [diff] [blame] | 114 | (void *)(unsigned long)he->sym->start, |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 115 | he->sym->name, |
Arjan van de Ven | 7d37a0c | 2009-06-06 20:36:38 +0200 | [diff] [blame] | 116 | (void *)(unsigned long)ip, ip - he->sym->start, |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 117 | sym->hist[offset]); |
| 118 | } |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 119 | |
| 120 | static int |
| 121 | hist_entry__add(struct thread *thread, struct map *map, struct dso *dso, |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 122 | struct symbol *sym, u64 ip, char level) |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 123 | { |
| 124 | struct rb_node **p = &hist.rb_node; |
| 125 | struct rb_node *parent = NULL; |
| 126 | struct hist_entry *he; |
| 127 | struct hist_entry entry = { |
| 128 | .thread = thread, |
| 129 | .map = map, |
| 130 | .dso = dso, |
| 131 | .sym = sym, |
| 132 | .ip = ip, |
| 133 | .level = level, |
| 134 | .count = 1, |
| 135 | }; |
| 136 | int cmp; |
| 137 | |
| 138 | while (*p != NULL) { |
| 139 | parent = *p; |
| 140 | he = rb_entry(parent, struct hist_entry, rb_node); |
| 141 | |
| 142 | cmp = hist_entry__cmp(&entry, he); |
| 143 | |
| 144 | if (!cmp) { |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 145 | hist_hit(he, ip); |
| 146 | |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | if (cmp < 0) |
| 151 | p = &(*p)->rb_left; |
| 152 | else |
| 153 | p = &(*p)->rb_right; |
| 154 | } |
| 155 | |
| 156 | he = malloc(sizeof(*he)); |
| 157 | if (!he) |
| 158 | return -ENOMEM; |
| 159 | *he = entry; |
| 160 | rb_link_node(&he->rb_node, parent, p); |
| 161 | rb_insert_color(&he->rb_node, &hist); |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | static void hist_entry__free(struct hist_entry *he) |
| 167 | { |
| 168 | free(he); |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | * collapse the histogram |
| 173 | */ |
| 174 | |
| 175 | static struct rb_root collapse_hists; |
| 176 | |
| 177 | static void collapse__insert_entry(struct hist_entry *he) |
| 178 | { |
| 179 | struct rb_node **p = &collapse_hists.rb_node; |
| 180 | struct rb_node *parent = NULL; |
| 181 | struct hist_entry *iter; |
| 182 | int64_t cmp; |
| 183 | |
| 184 | while (*p != NULL) { |
| 185 | parent = *p; |
| 186 | iter = rb_entry(parent, struct hist_entry, rb_node); |
| 187 | |
| 188 | cmp = hist_entry__collapse(iter, he); |
| 189 | |
| 190 | if (!cmp) { |
| 191 | iter->count += he->count; |
| 192 | hist_entry__free(he); |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | if (cmp < 0) |
| 197 | p = &(*p)->rb_left; |
| 198 | else |
| 199 | p = &(*p)->rb_right; |
| 200 | } |
| 201 | |
| 202 | rb_link_node(&he->rb_node, parent, p); |
| 203 | rb_insert_color(&he->rb_node, &collapse_hists); |
| 204 | } |
| 205 | |
| 206 | static void collapse__resort(void) |
| 207 | { |
| 208 | struct rb_node *next; |
| 209 | struct hist_entry *n; |
| 210 | |
| 211 | if (!sort__need_collapse) |
| 212 | return; |
| 213 | |
| 214 | next = rb_first(&hist); |
| 215 | while (next) { |
| 216 | n = rb_entry(next, struct hist_entry, rb_node); |
| 217 | next = rb_next(&n->rb_node); |
| 218 | |
| 219 | rb_erase(&n->rb_node, &hist); |
| 220 | collapse__insert_entry(n); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | * reverse the map, sort on count. |
| 226 | */ |
| 227 | |
| 228 | static struct rb_root output_hists; |
| 229 | |
| 230 | static void output__insert_entry(struct hist_entry *he) |
| 231 | { |
| 232 | struct rb_node **p = &output_hists.rb_node; |
| 233 | struct rb_node *parent = NULL; |
| 234 | struct hist_entry *iter; |
| 235 | |
| 236 | while (*p != NULL) { |
| 237 | parent = *p; |
| 238 | iter = rb_entry(parent, struct hist_entry, rb_node); |
| 239 | |
| 240 | if (he->count > iter->count) |
| 241 | p = &(*p)->rb_left; |
| 242 | else |
| 243 | p = &(*p)->rb_right; |
| 244 | } |
| 245 | |
| 246 | rb_link_node(&he->rb_node, parent, p); |
| 247 | rb_insert_color(&he->rb_node, &output_hists); |
| 248 | } |
| 249 | |
| 250 | static void output__resort(void) |
| 251 | { |
| 252 | struct rb_node *next; |
| 253 | struct hist_entry *n; |
| 254 | struct rb_root *tree = &hist; |
| 255 | |
| 256 | if (sort__need_collapse) |
| 257 | tree = &collapse_hists; |
| 258 | |
| 259 | next = rb_first(tree); |
| 260 | |
| 261 | while (next) { |
| 262 | n = rb_entry(next, struct hist_entry, rb_node); |
| 263 | next = rb_next(&n->rb_node); |
| 264 | |
| 265 | rb_erase(&n->rb_node, tree); |
| 266 | output__insert_entry(n); |
| 267 | } |
| 268 | } |
| 269 | |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 270 | static unsigned long total = 0, |
| 271 | total_mmap = 0, |
| 272 | total_comm = 0, |
| 273 | total_fork = 0, |
| 274 | total_unknown = 0; |
| 275 | |
| 276 | static int |
Peter Zijlstra | e6e18ec | 2009-06-25 11:27:12 +0200 | [diff] [blame] | 277 | process_sample_event(event_t *event, unsigned long offset, unsigned long head) |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 278 | { |
| 279 | char level; |
| 280 | int show = 0; |
| 281 | struct dso *dso = NULL; |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 282 | struct thread *thread; |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 283 | u64 ip = event->ip.ip; |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 284 | struct map *map = NULL; |
| 285 | |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 286 | thread = threads__findnew(event->ip.pid, &threads, &last_match); |
| 287 | |
Frederic Weisbecker | 2cec19d | 2009-08-16 19:24:21 +0200 | [diff] [blame] | 288 | dump_printf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n", |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 289 | (void *)(offset + head), |
| 290 | (void *)(long)(event->header.size), |
| 291 | event->header.misc, |
| 292 | event->ip.pid, |
| 293 | (void *)(long)ip); |
| 294 | |
Frederic Weisbecker | 2cec19d | 2009-08-16 19:24:21 +0200 | [diff] [blame] | 295 | dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid); |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 296 | |
| 297 | if (thread == NULL) { |
| 298 | fprintf(stderr, "problem processing %d event, skipping it.\n", |
| 299 | event->header.type); |
| 300 | return -1; |
| 301 | } |
| 302 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 303 | if (event->header.misc & PERF_RECORD_MISC_KERNEL) { |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 304 | show = SHOW_KERNEL; |
| 305 | level = 'k'; |
| 306 | |
| 307 | dso = kernel_dso; |
| 308 | |
Frederic Weisbecker | 2cec19d | 2009-08-16 19:24:21 +0200 | [diff] [blame] | 309 | dump_printf(" ...... dso: %s\n", dso->name); |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 310 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 311 | } else if (event->header.misc & PERF_RECORD_MISC_USER) { |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 312 | |
| 313 | show = SHOW_USER; |
| 314 | level = '.'; |
| 315 | |
| 316 | map = thread__find_map(thread, ip); |
| 317 | if (map != NULL) { |
| 318 | ip = map->map_ip(map, ip); |
| 319 | dso = map->dso; |
| 320 | } else { |
| 321 | /* |
| 322 | * If this is outside of all known maps, |
| 323 | * and is a negative address, try to look it |
| 324 | * up in the kernel dso, as it might be a |
| 325 | * vsyscall (which executes in user-mode): |
| 326 | */ |
| 327 | if ((long long)ip < 0) |
| 328 | dso = kernel_dso; |
| 329 | } |
Frederic Weisbecker | 2cec19d | 2009-08-16 19:24:21 +0200 | [diff] [blame] | 330 | dump_printf(" ...... dso: %s\n", dso ? dso->name : "<not found>"); |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 331 | |
| 332 | } else { |
| 333 | show = SHOW_HV; |
| 334 | level = 'H'; |
Frederic Weisbecker | 2cec19d | 2009-08-16 19:24:21 +0200 | [diff] [blame] | 335 | dump_printf(" ...... dso: [hypervisor]\n"); |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | if (show & show_mask) { |
| 339 | struct symbol *sym = NULL; |
| 340 | |
| 341 | if (dso) |
| 342 | sym = dso->find_symbol(dso, ip); |
| 343 | |
| 344 | if (hist_entry__add(thread, map, dso, sym, ip, level)) { |
| 345 | fprintf(stderr, |
| 346 | "problem incrementing symbol count, skipping event\n"); |
| 347 | return -1; |
| 348 | } |
| 349 | } |
| 350 | total++; |
| 351 | |
| 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | static int |
| 356 | process_mmap_event(event_t *event, unsigned long offset, unsigned long head) |
| 357 | { |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 358 | struct thread *thread; |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 359 | struct map *map = map__new(&event->mmap, NULL, 0); |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 360 | |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 361 | thread = threads__findnew(event->mmap.pid, &threads, &last_match); |
| 362 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 363 | dump_printf("%p [%p]: PERF_RECORD_MMAP %d: [%p(%p) @ %p]: %s\n", |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 364 | (void *)(offset + head), |
| 365 | (void *)(long)(event->header.size), |
| 366 | event->mmap.pid, |
| 367 | (void *)(long)event->mmap.start, |
| 368 | (void *)(long)event->mmap.len, |
| 369 | (void *)(long)event->mmap.pgoff, |
| 370 | event->mmap.filename); |
| 371 | |
| 372 | if (thread == NULL || map == NULL) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 373 | dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n"); |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | thread__insert_map(thread, map); |
| 378 | total_mmap++; |
| 379 | |
| 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | static int |
| 384 | process_comm_event(event_t *event, unsigned long offset, unsigned long head) |
| 385 | { |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 386 | struct thread *thread; |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 387 | |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 388 | thread = threads__findnew(event->comm.pid, &threads, &last_match); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 389 | dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n", |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 390 | (void *)(offset + head), |
| 391 | (void *)(long)(event->header.size), |
| 392 | event->comm.comm, event->comm.pid); |
| 393 | |
| 394 | if (thread == NULL || |
| 395 | thread__set_comm(thread, event->comm.comm)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 396 | dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n"); |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 397 | return -1; |
| 398 | } |
| 399 | total_comm++; |
| 400 | |
| 401 | return 0; |
| 402 | } |
| 403 | |
| 404 | static int |
| 405 | process_fork_event(event_t *event, unsigned long offset, unsigned long head) |
| 406 | { |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 407 | struct thread *thread; |
| 408 | struct thread *parent; |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 409 | |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 410 | thread = threads__findnew(event->fork.pid, &threads, &last_match); |
| 411 | parent = threads__findnew(event->fork.ppid, &threads, &last_match); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 412 | dump_printf("%p [%p]: PERF_RECORD_FORK: %d:%d\n", |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 413 | (void *)(offset + head), |
| 414 | (void *)(long)(event->header.size), |
| 415 | event->fork.pid, event->fork.ppid); |
| 416 | |
Ingo Molnar | 15f3fa4 | 2009-08-18 13:52:28 +0200 | [diff] [blame] | 417 | /* |
| 418 | * A thread clone will have the same PID for both |
| 419 | * parent and child. |
| 420 | */ |
| 421 | if (thread == parent) |
| 422 | return 0; |
| 423 | |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 424 | if (!thread || !parent || thread__fork(thread, parent)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 425 | dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n"); |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 426 | return -1; |
| 427 | } |
| 428 | total_fork++; |
| 429 | |
| 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | static int |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 434 | process_event(event_t *event, unsigned long offset, unsigned long head) |
| 435 | { |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 436 | switch (event->header.type) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 437 | case PERF_RECORD_SAMPLE: |
Peter Zijlstra | e6e18ec | 2009-06-25 11:27:12 +0200 | [diff] [blame] | 438 | return process_sample_event(event, offset, head); |
| 439 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 440 | case PERF_RECORD_MMAP: |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 441 | return process_mmap_event(event, offset, head); |
| 442 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 443 | case PERF_RECORD_COMM: |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 444 | return process_comm_event(event, offset, head); |
| 445 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 446 | case PERF_RECORD_FORK: |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 447 | return process_fork_event(event, offset, head); |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 448 | /* |
| 449 | * We dont process them right now but they are fine: |
| 450 | */ |
| 451 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 452 | case PERF_RECORD_THROTTLE: |
| 453 | case PERF_RECORD_UNTHROTTLE: |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 454 | return 0; |
| 455 | |
| 456 | default: |
| 457 | return -1; |
| 458 | } |
| 459 | |
| 460 | return 0; |
| 461 | } |
| 462 | |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 463 | static int |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 464 | parse_line(FILE *file, struct symbol *sym, u64 start, u64 len) |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 465 | { |
| 466 | char *line = NULL, *tmp, *tmp2; |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 467 | static const char *prev_line; |
| 468 | static const char *prev_color; |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 469 | unsigned int offset; |
| 470 | size_t line_len; |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 471 | s64 line_ip; |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 472 | int ret; |
| 473 | char *c; |
| 474 | |
| 475 | if (getline(&line, &line_len, file) < 0) |
| 476 | return -1; |
| 477 | if (!line) |
| 478 | return -1; |
| 479 | |
| 480 | c = strchr(line, '\n'); |
| 481 | if (c) |
| 482 | *c = 0; |
| 483 | |
| 484 | line_ip = -1; |
| 485 | offset = 0; |
| 486 | ret = -2; |
| 487 | |
| 488 | /* |
| 489 | * Strip leading spaces: |
| 490 | */ |
| 491 | tmp = line; |
| 492 | while (*tmp) { |
| 493 | if (*tmp != ' ') |
| 494 | break; |
| 495 | tmp++; |
| 496 | } |
| 497 | |
| 498 | if (*tmp) { |
| 499 | /* |
| 500 | * Parse hexa addresses followed by ':' |
| 501 | */ |
| 502 | line_ip = strtoull(tmp, &tmp2, 16); |
| 503 | if (*tmp2 != ':') |
| 504 | line_ip = -1; |
| 505 | } |
| 506 | |
| 507 | if (line_ip != -1) { |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 508 | const char *path = NULL; |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 509 | unsigned int hits = 0; |
| 510 | double percent = 0.0; |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 511 | const char *color; |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 512 | struct sym_ext *sym_ext = sym->priv; |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 513 | |
| 514 | offset = line_ip - start; |
| 515 | if (offset < len) |
| 516 | hits = sym->hist[offset]; |
| 517 | |
Frederic Weisbecker | c17c2db | 2009-06-13 17:39:23 +0200 | [diff] [blame] | 518 | if (offset < len && sym_ext) { |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 519 | path = sym_ext[offset].path; |
| 520 | percent = sym_ext[offset].percent; |
| 521 | } else if (sym->hist_sum) |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 522 | percent = 100.0 * hits / sym->hist_sum; |
| 523 | |
Frederic Weisbecker | 1e11fd8 | 2009-07-02 20:14:34 +0200 | [diff] [blame] | 524 | color = get_percent_color(percent); |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 525 | |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 526 | /* |
| 527 | * Also color the filename and line if needed, with |
| 528 | * the same color than the percentage. Don't print it |
| 529 | * twice for close colored ip with the same filename:line |
| 530 | */ |
| 531 | if (path) { |
| 532 | if (!prev_line || strcmp(prev_line, path) |
| 533 | || color != prev_color) { |
| 534 | color_fprintf(stdout, color, " %s", path); |
| 535 | prev_line = path; |
| 536 | prev_color = color; |
| 537 | } |
| 538 | } |
| 539 | |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 540 | color_fprintf(stdout, color, " %7.2f", percent); |
| 541 | printf(" : "); |
| 542 | color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", line); |
| 543 | } else { |
| 544 | if (!*line) |
| 545 | printf(" :\n"); |
| 546 | else |
| 547 | printf(" : %s\n", line); |
| 548 | } |
| 549 | |
| 550 | return 0; |
| 551 | } |
| 552 | |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 553 | static struct rb_root root_sym_ext; |
| 554 | |
| 555 | static void insert_source_line(struct sym_ext *sym_ext) |
| 556 | { |
| 557 | struct sym_ext *iter; |
| 558 | struct rb_node **p = &root_sym_ext.rb_node; |
| 559 | struct rb_node *parent = NULL; |
| 560 | |
| 561 | while (*p != NULL) { |
| 562 | parent = *p; |
| 563 | iter = rb_entry(parent, struct sym_ext, node); |
| 564 | |
| 565 | if (sym_ext->percent > iter->percent) |
| 566 | p = &(*p)->rb_left; |
| 567 | else |
| 568 | p = &(*p)->rb_right; |
| 569 | } |
| 570 | |
| 571 | rb_link_node(&sym_ext->node, parent, p); |
| 572 | rb_insert_color(&sym_ext->node, &root_sym_ext); |
| 573 | } |
| 574 | |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 575 | static void free_source_line(struct symbol *sym, int len) |
| 576 | { |
| 577 | struct sym_ext *sym_ext = sym->priv; |
| 578 | int i; |
| 579 | |
| 580 | if (!sym_ext) |
| 581 | return; |
| 582 | |
| 583 | for (i = 0; i < len; i++) |
| 584 | free(sym_ext[i].path); |
| 585 | free(sym_ext); |
| 586 | |
| 587 | sym->priv = NULL; |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 588 | root_sym_ext = RB_ROOT; |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | /* Get the filename:line for the colored entries */ |
Frederic Weisbecker | c17c2db | 2009-06-13 17:39:23 +0200 | [diff] [blame] | 592 | static void |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 593 | get_source_line(struct symbol *sym, u64 start, int len, const char *filename) |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 594 | { |
| 595 | int i; |
| 596 | char cmd[PATH_MAX * 2]; |
| 597 | struct sym_ext *sym_ext; |
| 598 | |
| 599 | if (!sym->hist_sum) |
| 600 | return; |
| 601 | |
| 602 | sym->priv = calloc(len, sizeof(struct sym_ext)); |
| 603 | if (!sym->priv) |
| 604 | return; |
| 605 | |
| 606 | sym_ext = sym->priv; |
| 607 | |
| 608 | for (i = 0; i < len; i++) { |
| 609 | char *path = NULL; |
| 610 | size_t line_len; |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 611 | u64 offset; |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 612 | FILE *fp; |
| 613 | |
| 614 | sym_ext[i].percent = 100.0 * sym->hist[i] / sym->hist_sum; |
| 615 | if (sym_ext[i].percent <= 0.5) |
| 616 | continue; |
| 617 | |
| 618 | offset = start + i; |
Frederic Weisbecker | c17c2db | 2009-06-13 17:39:23 +0200 | [diff] [blame] | 619 | sprintf(cmd, "addr2line -e %s %016llx", filename, offset); |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 620 | fp = popen(cmd, "r"); |
| 621 | if (!fp) |
| 622 | continue; |
| 623 | |
| 624 | if (getline(&path, &line_len, fp) < 0 || !line_len) |
| 625 | goto next; |
| 626 | |
Frederic Weisbecker | c17c2db | 2009-06-13 17:39:23 +0200 | [diff] [blame] | 627 | sym_ext[i].path = malloc(sizeof(char) * line_len + 1); |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 628 | if (!sym_ext[i].path) |
| 629 | goto next; |
| 630 | |
| 631 | strcpy(sym_ext[i].path, path); |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 632 | insert_source_line(&sym_ext[i]); |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 633 | |
| 634 | next: |
| 635 | pclose(fp); |
| 636 | } |
| 637 | } |
| 638 | |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 639 | static void print_summary(const char *filename) |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 640 | { |
| 641 | struct sym_ext *sym_ext; |
| 642 | struct rb_node *node; |
| 643 | |
| 644 | printf("\nSorted summary for file %s\n", filename); |
| 645 | printf("----------------------------------------------\n\n"); |
| 646 | |
| 647 | if (RB_EMPTY_ROOT(&root_sym_ext)) { |
| 648 | printf(" Nothing higher than %1.1f%%\n", MIN_GREEN); |
| 649 | return; |
| 650 | } |
| 651 | |
| 652 | node = rb_first(&root_sym_ext); |
| 653 | while (node) { |
| 654 | double percent; |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 655 | const char *color; |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 656 | char *path; |
| 657 | |
| 658 | sym_ext = rb_entry(node, struct sym_ext, node); |
| 659 | percent = sym_ext->percent; |
Frederic Weisbecker | 1e11fd8 | 2009-07-02 20:14:34 +0200 | [diff] [blame] | 660 | color = get_percent_color(percent); |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 661 | path = sym_ext->path; |
| 662 | |
| 663 | color_fprintf(stdout, color, " %7.2f %s", percent, path); |
| 664 | node = rb_next(node); |
| 665 | } |
| 666 | } |
| 667 | |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 668 | static void annotate_sym(struct dso *dso, struct symbol *sym) |
| 669 | { |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 670 | const char *filename = dso->name, *d_filename; |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 671 | u64 start, end, len; |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 672 | char command[PATH_MAX*2]; |
| 673 | FILE *file; |
| 674 | |
| 675 | if (!filename) |
| 676 | return; |
Mike Galbraith | 4297648 | 2009-07-02 08:09:46 +0200 | [diff] [blame] | 677 | if (sym->module) |
| 678 | filename = sym->module->path; |
| 679 | else if (dso == kernel_dso) |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 680 | filename = vmlinux_name; |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 681 | |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 682 | start = sym->obj_start; |
| 683 | if (!start) |
| 684 | start = sym->start; |
Mike Galbraith | 4297648 | 2009-07-02 08:09:46 +0200 | [diff] [blame] | 685 | if (full_paths) |
| 686 | d_filename = filename; |
| 687 | else |
| 688 | d_filename = basename(filename); |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 689 | |
| 690 | end = start + sym->end - sym->start + 1; |
| 691 | len = sym->end - sym->start; |
| 692 | |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 693 | if (print_line) { |
Frederic Weisbecker | c17c2db | 2009-06-13 17:39:23 +0200 | [diff] [blame] | 694 | get_source_line(sym, start, len, filename); |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 695 | print_summary(filename); |
| 696 | } |
| 697 | |
| 698 | printf("\n\n------------------------------------------------\n"); |
Mike Galbraith | 4297648 | 2009-07-02 08:09:46 +0200 | [diff] [blame] | 699 | printf(" Percent | Source code & Disassembly of %s\n", d_filename); |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 700 | printf("------------------------------------------------\n"); |
| 701 | |
| 702 | if (verbose >= 2) |
| 703 | printf("annotating [%p] %30s : [%p] %30s\n", dso, dso->name, sym, sym->name); |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 704 | |
Mike Galbraith | 4297648 | 2009-07-02 08:09:46 +0200 | [diff] [blame] | 705 | sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s", |
| 706 | (u64)start, (u64)end, filename, filename); |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 707 | |
| 708 | if (verbose >= 3) |
| 709 | printf("doing: %s\n", command); |
| 710 | |
| 711 | file = popen(command, "r"); |
| 712 | if (!file) |
| 713 | return; |
| 714 | |
| 715 | while (!feof(file)) { |
| 716 | if (parse_line(file, sym, start, len) < 0) |
| 717 | break; |
| 718 | } |
| 719 | |
| 720 | pclose(file); |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 721 | if (print_line) |
| 722 | free_source_line(sym, len); |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | static void find_annotations(void) |
| 726 | { |
| 727 | struct rb_node *nd; |
| 728 | struct dso *dso; |
| 729 | int count = 0; |
| 730 | |
| 731 | list_for_each_entry(dso, &dsos, node) { |
| 732 | |
| 733 | for (nd = rb_first(&dso->syms); nd; nd = rb_next(nd)) { |
| 734 | struct symbol *sym = rb_entry(nd, struct symbol, rb_node); |
| 735 | |
| 736 | if (sym->hist) { |
| 737 | annotate_sym(dso, sym); |
| 738 | count++; |
| 739 | } |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | if (!count) |
| 744 | printf(" Error: symbol '%s' not present amongst the samples.\n", sym_hist_filter); |
| 745 | } |
| 746 | |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 747 | static int __cmd_annotate(void) |
| 748 | { |
| 749 | int ret, rc = EXIT_FAILURE; |
| 750 | unsigned long offset = 0; |
| 751 | unsigned long head = 0; |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 752 | struct stat input_stat; |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 753 | event_t *event; |
| 754 | uint32_t size; |
| 755 | char *buf; |
| 756 | |
Frederic Weisbecker | 5b447a6 | 2009-08-31 06:45:18 +0200 | [diff] [blame] | 757 | register_idle_thread(&threads, &last_match); |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 758 | |
| 759 | input = open(input_name, O_RDONLY); |
| 760 | if (input < 0) { |
| 761 | perror("failed to open file"); |
| 762 | exit(-1); |
| 763 | } |
| 764 | |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 765 | ret = fstat(input, &input_stat); |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 766 | if (ret < 0) { |
| 767 | perror("failed to stat file"); |
| 768 | exit(-1); |
| 769 | } |
| 770 | |
Pierre Habouzit | 119e7a2 | 2009-08-27 09:59:02 +0200 | [diff] [blame] | 771 | if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) { |
| 772 | fprintf(stderr, "file: %s not owned by current user or root\n", input_name); |
Peter Zijlstra | fa6963b | 2009-08-19 11:18:26 +0200 | [diff] [blame] | 773 | exit(-1); |
| 774 | } |
| 775 | |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 776 | if (!input_stat.st_size) { |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 777 | fprintf(stderr, "zero-sized file, nothing to do!\n"); |
| 778 | exit(0); |
| 779 | } |
| 780 | |
| 781 | if (load_kernel() < 0) { |
| 782 | perror("failed to load kernel symbols"); |
| 783 | return EXIT_FAILURE; |
| 784 | } |
| 785 | |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 786 | remap: |
| 787 | buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ, |
| 788 | MAP_SHARED, input, offset); |
| 789 | if (buf == MAP_FAILED) { |
| 790 | perror("failed to mmap file"); |
| 791 | exit(-1); |
| 792 | } |
| 793 | |
| 794 | more: |
| 795 | event = (event_t *)(buf + head); |
| 796 | |
| 797 | size = event->header.size; |
| 798 | if (!size) |
| 799 | size = 8; |
| 800 | |
| 801 | if (head + event->header.size >= page_size * mmap_window) { |
| 802 | unsigned long shift = page_size * (head / page_size); |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 803 | int munmap_ret; |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 804 | |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 805 | munmap_ret = munmap(buf, page_size * mmap_window); |
| 806 | assert(munmap_ret == 0); |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 807 | |
| 808 | offset += shift; |
| 809 | head -= shift; |
| 810 | goto remap; |
| 811 | } |
| 812 | |
| 813 | size = event->header.size; |
| 814 | |
Frederic Weisbecker | 2cec19d | 2009-08-16 19:24:21 +0200 | [diff] [blame] | 815 | dump_printf("%p [%p]: event: %d\n", |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 816 | (void *)(offset + head), |
| 817 | (void *)(long)event->header.size, |
| 818 | event->header.type); |
| 819 | |
| 820 | if (!size || process_event(event, offset, head) < 0) { |
| 821 | |
Frederic Weisbecker | 2cec19d | 2009-08-16 19:24:21 +0200 | [diff] [blame] | 822 | dump_printf("%p [%p]: skipping unknown header type: %d\n", |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 823 | (void *)(offset + head), |
| 824 | (void *)(long)(event->header.size), |
| 825 | event->header.type); |
| 826 | |
| 827 | total_unknown++; |
| 828 | |
| 829 | /* |
| 830 | * assume we lost track of the stream, check alignment, and |
| 831 | * increment a single u64 in the hope to catch on again 'soon'. |
| 832 | */ |
| 833 | |
| 834 | if (unlikely(head & 7)) |
| 835 | head &= ~7ULL; |
| 836 | |
| 837 | size = 8; |
| 838 | } |
| 839 | |
| 840 | head += size; |
| 841 | |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 842 | if (offset + head < (unsigned long)input_stat.st_size) |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 843 | goto more; |
| 844 | |
| 845 | rc = EXIT_SUCCESS; |
| 846 | close(input); |
| 847 | |
Frederic Weisbecker | 2cec19d | 2009-08-16 19:24:21 +0200 | [diff] [blame] | 848 | dump_printf(" IP events: %10ld\n", total); |
| 849 | dump_printf(" mmap events: %10ld\n", total_mmap); |
| 850 | dump_printf(" comm events: %10ld\n", total_comm); |
| 851 | dump_printf(" fork events: %10ld\n", total_fork); |
| 852 | dump_printf(" unknown events: %10ld\n", total_unknown); |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 853 | |
| 854 | if (dump_trace) |
| 855 | return 0; |
| 856 | |
| 857 | if (verbose >= 3) |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 858 | threads__fprintf(stdout, &threads); |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 859 | |
| 860 | if (verbose >= 2) |
| 861 | dsos__fprintf(stdout); |
| 862 | |
| 863 | collapse__resort(); |
| 864 | output__resort(); |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 865 | |
| 866 | find_annotations(); |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 867 | |
| 868 | return rc; |
| 869 | } |
| 870 | |
| 871 | static const char * const annotate_usage[] = { |
| 872 | "perf annotate [<options>] <command>", |
| 873 | NULL |
| 874 | }; |
| 875 | |
| 876 | static const struct option options[] = { |
| 877 | OPT_STRING('i', "input", &input_name, "file", |
| 878 | "input file name"), |
Ingo Molnar | 23b8711 | 2009-06-06 21:25:29 +0200 | [diff] [blame] | 879 | OPT_STRING('s', "symbol", &sym_hist_filter, "symbol", |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 880 | "symbol to annotate"), |
Peter Zijlstra | fa6963b | 2009-08-19 11:18:26 +0200 | [diff] [blame] | 881 | OPT_BOOLEAN('f', "force", &force, "don't complain, do it"), |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 882 | OPT_BOOLEAN('v', "verbose", &verbose, |
| 883 | "be more verbose (show symbol address, etc)"), |
| 884 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, |
| 885 | "dump raw trace in ASCII"), |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 886 | OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"), |
Mike Galbraith | 4297648 | 2009-07-02 08:09:46 +0200 | [diff] [blame] | 887 | OPT_BOOLEAN('m', "modules", &modules, |
| 888 | "load module symbols - WARNING: use only with -k and LIVE kernel"), |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 889 | OPT_BOOLEAN('l', "print-line", &print_line, |
| 890 | "print matching source lines (may be slow)"), |
Mike Galbraith | 4297648 | 2009-07-02 08:09:46 +0200 | [diff] [blame] | 891 | OPT_BOOLEAN('P', "full-paths", &full_paths, |
| 892 | "Don't shorten the displayed pathnames"), |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 893 | OPT_END() |
| 894 | }; |
| 895 | |
| 896 | static void setup_sorting(void) |
| 897 | { |
| 898 | char *tmp, *tok, *str = strdup(sort_order); |
| 899 | |
| 900 | for (tok = strtok_r(str, ", ", &tmp); |
| 901 | tok; tok = strtok_r(NULL, ", ", &tmp)) { |
| 902 | if (sort_dimension__add(tok) < 0) { |
| 903 | error("Unknown --sort key: `%s'", tok); |
| 904 | usage_with_options(annotate_usage, options); |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | free(str); |
| 909 | } |
| 910 | |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 911 | int cmd_annotate(int argc, const char **argv, const char *prefix __used) |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 912 | { |
| 913 | symbol__init(); |
| 914 | |
| 915 | page_size = getpagesize(); |
| 916 | |
| 917 | argc = parse_options(argc, argv, options, annotate_usage, 0); |
| 918 | |
| 919 | setup_sorting(); |
| 920 | |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 921 | if (argc) { |
| 922 | /* |
| 923 | * Special case: if there's an argument left then assume tha |
| 924 | * it's a symbol filter: |
| 925 | */ |
| 926 | if (argc > 1) |
| 927 | usage_with_options(annotate_usage, options); |
| 928 | |
| 929 | sym_hist_filter = argv[0]; |
| 930 | } |
| 931 | |
| 932 | if (!sym_hist_filter) |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 933 | usage_with_options(annotate_usage, options); |
| 934 | |
| 935 | setup_pager(); |
| 936 | |
John Kacur | dd68ada | 2009-09-24 18:02:49 +0200 | [diff] [blame^] | 937 | if (field_sep && *field_sep == '.') { |
| 938 | fputs("'.' is the only non valid --field-separator argument\n", |
| 939 | stderr); |
| 940 | exit(129); |
| 941 | } |
| 942 | |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 943 | return __cmd_annotate(); |
| 944 | } |