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