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" |
| 23 | |
| 24 | #define SHOW_KERNEL 1 |
| 25 | #define SHOW_USER 2 |
| 26 | #define SHOW_HV 4 |
| 27 | |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 28 | #define MIN_GREEN 0.5 |
| 29 | #define MIN_RED 5.0 |
| 30 | |
| 31 | |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 32 | static char const *input_name = "perf.data"; |
Ingo Molnar | 39273ee | 2009-06-06 21:17:03 +0200 | [diff] [blame] | 33 | static char *vmlinux = "vmlinux"; |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 34 | |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 35 | static char default_sort_order[] = "comm,symbol"; |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 36 | static char *sort_order = default_sort_order; |
| 37 | |
| 38 | static int input; |
| 39 | static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV; |
| 40 | |
| 41 | static int dump_trace = 0; |
| 42 | #define dprintf(x...) do { if (dump_trace) printf(x); } while (0) |
| 43 | |
| 44 | static int verbose; |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 45 | |
Mike Galbraith | 4297648 | 2009-07-02 08:09:46 +0200 | [diff] [blame^] | 46 | static int modules; |
| 47 | |
| 48 | static int full_paths; |
| 49 | |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 50 | static int print_line; |
| 51 | |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 52 | static unsigned long page_size; |
| 53 | static unsigned long mmap_window = 32; |
| 54 | |
| 55 | struct ip_event { |
| 56 | struct perf_event_header header; |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 57 | u64 ip; |
| 58 | u32 pid, tid; |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | struct mmap_event { |
| 62 | struct perf_event_header header; |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 63 | u32 pid, tid; |
| 64 | u64 start; |
| 65 | u64 len; |
| 66 | u64 pgoff; |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 67 | char filename[PATH_MAX]; |
| 68 | }; |
| 69 | |
| 70 | struct comm_event { |
| 71 | struct perf_event_header header; |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 72 | u32 pid, tid; |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 73 | char comm[16]; |
| 74 | }; |
| 75 | |
| 76 | struct fork_event { |
| 77 | struct perf_event_header header; |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 78 | u32 pid, ppid; |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | struct period_event { |
| 82 | struct perf_event_header header; |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 83 | u64 time; |
| 84 | u64 id; |
| 85 | u64 sample_period; |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | typedef union event_union { |
| 89 | struct perf_event_header header; |
| 90 | struct ip_event ip; |
| 91 | struct mmap_event mmap; |
| 92 | struct comm_event comm; |
| 93 | struct fork_event fork; |
| 94 | struct period_event period; |
| 95 | } event_t; |
| 96 | |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 97 | |
| 98 | struct sym_ext { |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 99 | struct rb_node node; |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 100 | double percent; |
| 101 | char *path; |
| 102 | }; |
| 103 | |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 104 | static LIST_HEAD(dsos); |
| 105 | static struct dso *kernel_dso; |
| 106 | static struct dso *vdso; |
| 107 | |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 108 | |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 109 | static void dsos__add(struct dso *dso) |
| 110 | { |
| 111 | list_add_tail(&dso->node, &dsos); |
| 112 | } |
| 113 | |
| 114 | static struct dso *dsos__find(const char *name) |
| 115 | { |
| 116 | struct dso *pos; |
| 117 | |
| 118 | list_for_each_entry(pos, &dsos, node) |
| 119 | if (strcmp(pos->name, name) == 0) |
| 120 | return pos; |
| 121 | return NULL; |
| 122 | } |
| 123 | |
| 124 | static struct dso *dsos__findnew(const char *name) |
| 125 | { |
| 126 | struct dso *dso = dsos__find(name); |
| 127 | int nr; |
| 128 | |
| 129 | if (dso) |
| 130 | return dso; |
| 131 | |
| 132 | dso = dso__new(name, 0); |
| 133 | if (!dso) |
| 134 | goto out_delete_dso; |
| 135 | |
| 136 | nr = dso__load(dso, NULL, verbose); |
| 137 | if (nr < 0) { |
| 138 | if (verbose) |
| 139 | fprintf(stderr, "Failed to open: %s\n", name); |
| 140 | goto out_delete_dso; |
| 141 | } |
| 142 | if (!nr && verbose) { |
| 143 | fprintf(stderr, |
| 144 | "No symbols found in: %s, maybe install a debug package?\n", |
| 145 | name); |
| 146 | } |
| 147 | |
| 148 | dsos__add(dso); |
| 149 | |
| 150 | return dso; |
| 151 | |
| 152 | out_delete_dso: |
| 153 | dso__delete(dso); |
| 154 | return NULL; |
| 155 | } |
| 156 | |
| 157 | static void dsos__fprintf(FILE *fp) |
| 158 | { |
| 159 | struct dso *pos; |
| 160 | |
| 161 | list_for_each_entry(pos, &dsos, node) |
| 162 | dso__fprintf(pos, fp); |
| 163 | } |
| 164 | |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 165 | static struct symbol *vdso__find_symbol(struct dso *dso, u64 ip) |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 166 | { |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 167 | return dso__find_symbol(dso, ip); |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | static int load_kernel(void) |
| 171 | { |
| 172 | int err; |
| 173 | |
| 174 | kernel_dso = dso__new("[kernel]", 0); |
| 175 | if (!kernel_dso) |
| 176 | return -1; |
| 177 | |
Mike Galbraith | 4297648 | 2009-07-02 08:09:46 +0200 | [diff] [blame^] | 178 | err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose, modules); |
Mike Galbraith | 9974f49 | 2009-07-02 08:05:58 +0200 | [diff] [blame] | 179 | if (err <= 0) { |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 180 | dso__delete(kernel_dso); |
| 181 | kernel_dso = NULL; |
| 182 | } else |
| 183 | dsos__add(kernel_dso); |
| 184 | |
| 185 | vdso = dso__new("[vdso]", 0); |
| 186 | if (!vdso) |
| 187 | return -1; |
| 188 | |
| 189 | vdso->find_symbol = vdso__find_symbol; |
| 190 | |
| 191 | dsos__add(vdso); |
| 192 | |
| 193 | return err; |
| 194 | } |
| 195 | |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 196 | struct map { |
| 197 | struct list_head node; |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 198 | u64 start; |
| 199 | u64 end; |
| 200 | u64 pgoff; |
| 201 | u64 (*map_ip)(struct map *, u64); |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 202 | struct dso *dso; |
| 203 | }; |
| 204 | |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 205 | static u64 map__map_ip(struct map *map, u64 ip) |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 206 | { |
| 207 | return ip - map->start + map->pgoff; |
| 208 | } |
| 209 | |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 210 | static u64 vdso__map_ip(struct map *map __used, u64 ip) |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 211 | { |
| 212 | return ip; |
| 213 | } |
| 214 | |
| 215 | static struct map *map__new(struct mmap_event *event) |
| 216 | { |
| 217 | struct map *self = malloc(sizeof(*self)); |
| 218 | |
| 219 | if (self != NULL) { |
| 220 | const char *filename = event->filename; |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 221 | |
| 222 | self->start = event->start; |
| 223 | self->end = event->start + event->len; |
| 224 | self->pgoff = event->pgoff; |
| 225 | |
| 226 | self->dso = dsos__findnew(filename); |
| 227 | if (self->dso == NULL) |
| 228 | goto out_delete; |
| 229 | |
| 230 | if (self->dso == vdso) |
| 231 | self->map_ip = vdso__map_ip; |
| 232 | else |
| 233 | self->map_ip = map__map_ip; |
| 234 | } |
| 235 | return self; |
| 236 | out_delete: |
| 237 | free(self); |
| 238 | return NULL; |
| 239 | } |
| 240 | |
| 241 | static struct map *map__clone(struct map *self) |
| 242 | { |
| 243 | struct map *map = malloc(sizeof(*self)); |
| 244 | |
| 245 | if (!map) |
| 246 | return NULL; |
| 247 | |
| 248 | memcpy(map, self, sizeof(*self)); |
| 249 | |
| 250 | return map; |
| 251 | } |
| 252 | |
| 253 | static int map__overlap(struct map *l, struct map *r) |
| 254 | { |
| 255 | if (l->start > r->start) { |
| 256 | struct map *t = l; |
| 257 | l = r; |
| 258 | r = t; |
| 259 | } |
| 260 | |
| 261 | if (l->end > r->start) |
| 262 | return 1; |
| 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | static size_t map__fprintf(struct map *self, FILE *fp) |
| 268 | { |
Ingo Molnar | 729ff5e2 | 2009-06-11 14:16:15 +0200 | [diff] [blame] | 269 | return fprintf(fp, " %Lx-%Lx %Lx %s\n", |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 270 | self->start, self->end, self->pgoff, self->dso->name); |
| 271 | } |
| 272 | |
| 273 | |
| 274 | struct thread { |
| 275 | struct rb_node rb_node; |
| 276 | struct list_head maps; |
| 277 | pid_t pid; |
| 278 | char *comm; |
| 279 | }; |
| 280 | |
| 281 | static struct thread *thread__new(pid_t pid) |
| 282 | { |
| 283 | struct thread *self = malloc(sizeof(*self)); |
| 284 | |
| 285 | if (self != NULL) { |
| 286 | self->pid = pid; |
| 287 | self->comm = malloc(32); |
| 288 | if (self->comm) |
| 289 | snprintf(self->comm, 32, ":%d", self->pid); |
| 290 | INIT_LIST_HEAD(&self->maps); |
| 291 | } |
| 292 | |
| 293 | return self; |
| 294 | } |
| 295 | |
| 296 | static int thread__set_comm(struct thread *self, const char *comm) |
| 297 | { |
| 298 | if (self->comm) |
| 299 | free(self->comm); |
| 300 | self->comm = strdup(comm); |
| 301 | return self->comm ? 0 : -ENOMEM; |
| 302 | } |
| 303 | |
| 304 | static size_t thread__fprintf(struct thread *self, FILE *fp) |
| 305 | { |
| 306 | struct map *pos; |
| 307 | size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm); |
| 308 | |
| 309 | list_for_each_entry(pos, &self->maps, node) |
| 310 | ret += map__fprintf(pos, fp); |
| 311 | |
| 312 | return ret; |
| 313 | } |
| 314 | |
| 315 | |
| 316 | static struct rb_root threads; |
| 317 | static struct thread *last_match; |
| 318 | |
| 319 | static struct thread *threads__findnew(pid_t pid) |
| 320 | { |
| 321 | struct rb_node **p = &threads.rb_node; |
| 322 | struct rb_node *parent = NULL; |
| 323 | struct thread *th; |
| 324 | |
| 325 | /* |
| 326 | * Font-end cache - PID lookups come in blocks, |
| 327 | * so most of the time we dont have to look up |
| 328 | * the full rbtree: |
| 329 | */ |
| 330 | if (last_match && last_match->pid == pid) |
| 331 | return last_match; |
| 332 | |
| 333 | while (*p != NULL) { |
| 334 | parent = *p; |
| 335 | th = rb_entry(parent, struct thread, rb_node); |
| 336 | |
| 337 | if (th->pid == pid) { |
| 338 | last_match = th; |
| 339 | return th; |
| 340 | } |
| 341 | |
| 342 | if (pid < th->pid) |
| 343 | p = &(*p)->rb_left; |
| 344 | else |
| 345 | p = &(*p)->rb_right; |
| 346 | } |
| 347 | |
| 348 | th = thread__new(pid); |
| 349 | if (th != NULL) { |
| 350 | rb_link_node(&th->rb_node, parent, p); |
| 351 | rb_insert_color(&th->rb_node, &threads); |
| 352 | last_match = th; |
| 353 | } |
| 354 | |
| 355 | return th; |
| 356 | } |
| 357 | |
| 358 | static void thread__insert_map(struct thread *self, struct map *map) |
| 359 | { |
| 360 | struct map *pos, *tmp; |
| 361 | |
| 362 | list_for_each_entry_safe(pos, tmp, &self->maps, node) { |
| 363 | if (map__overlap(pos, map)) { |
| 364 | list_del_init(&pos->node); |
| 365 | /* XXX leaks dsos */ |
| 366 | free(pos); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | list_add_tail(&map->node, &self->maps); |
| 371 | } |
| 372 | |
| 373 | static int thread__fork(struct thread *self, struct thread *parent) |
| 374 | { |
| 375 | struct map *map; |
| 376 | |
| 377 | if (self->comm) |
| 378 | free(self->comm); |
| 379 | self->comm = strdup(parent->comm); |
| 380 | if (!self->comm) |
| 381 | return -ENOMEM; |
| 382 | |
| 383 | list_for_each_entry(map, &parent->maps, node) { |
| 384 | struct map *new = map__clone(map); |
| 385 | if (!new) |
| 386 | return -ENOMEM; |
| 387 | thread__insert_map(self, new); |
| 388 | } |
| 389 | |
| 390 | return 0; |
| 391 | } |
| 392 | |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 393 | static struct map *thread__find_map(struct thread *self, u64 ip) |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 394 | { |
| 395 | struct map *pos; |
| 396 | |
| 397 | if (self == NULL) |
| 398 | return NULL; |
| 399 | |
| 400 | list_for_each_entry(pos, &self->maps, node) |
| 401 | if (ip >= pos->start && ip <= pos->end) |
| 402 | return pos; |
| 403 | |
| 404 | return NULL; |
| 405 | } |
| 406 | |
| 407 | static size_t threads__fprintf(FILE *fp) |
| 408 | { |
| 409 | size_t ret = 0; |
| 410 | struct rb_node *nd; |
| 411 | |
| 412 | for (nd = rb_first(&threads); nd; nd = rb_next(nd)) { |
| 413 | struct thread *pos = rb_entry(nd, struct thread, rb_node); |
| 414 | |
| 415 | ret += thread__fprintf(pos, fp); |
| 416 | } |
| 417 | |
| 418 | return ret; |
| 419 | } |
| 420 | |
| 421 | /* |
| 422 | * histogram, sorted on item, collects counts |
| 423 | */ |
| 424 | |
| 425 | static struct rb_root hist; |
| 426 | |
| 427 | struct hist_entry { |
| 428 | struct rb_node rb_node; |
| 429 | |
| 430 | struct thread *thread; |
| 431 | struct map *map; |
| 432 | struct dso *dso; |
| 433 | struct symbol *sym; |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 434 | u64 ip; |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 435 | char level; |
| 436 | |
| 437 | uint32_t count; |
| 438 | }; |
| 439 | |
| 440 | /* |
| 441 | * configurable sorting bits |
| 442 | */ |
| 443 | |
| 444 | struct sort_entry { |
| 445 | struct list_head list; |
| 446 | |
| 447 | char *header; |
| 448 | |
| 449 | int64_t (*cmp)(struct hist_entry *, struct hist_entry *); |
| 450 | int64_t (*collapse)(struct hist_entry *, struct hist_entry *); |
| 451 | size_t (*print)(FILE *fp, struct hist_entry *); |
| 452 | }; |
| 453 | |
| 454 | /* --sort pid */ |
| 455 | |
| 456 | static int64_t |
| 457 | sort__thread_cmp(struct hist_entry *left, struct hist_entry *right) |
| 458 | { |
| 459 | return right->thread->pid - left->thread->pid; |
| 460 | } |
| 461 | |
| 462 | static size_t |
| 463 | sort__thread_print(FILE *fp, struct hist_entry *self) |
| 464 | { |
| 465 | return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid); |
| 466 | } |
| 467 | |
| 468 | static struct sort_entry sort_thread = { |
| 469 | .header = " Command: Pid", |
| 470 | .cmp = sort__thread_cmp, |
| 471 | .print = sort__thread_print, |
| 472 | }; |
| 473 | |
| 474 | /* --sort comm */ |
| 475 | |
| 476 | static int64_t |
| 477 | sort__comm_cmp(struct hist_entry *left, struct hist_entry *right) |
| 478 | { |
| 479 | return right->thread->pid - left->thread->pid; |
| 480 | } |
| 481 | |
| 482 | static int64_t |
| 483 | sort__comm_collapse(struct hist_entry *left, struct hist_entry *right) |
| 484 | { |
| 485 | char *comm_l = left->thread->comm; |
| 486 | char *comm_r = right->thread->comm; |
| 487 | |
| 488 | if (!comm_l || !comm_r) { |
| 489 | if (!comm_l && !comm_r) |
| 490 | return 0; |
| 491 | else if (!comm_l) |
| 492 | return -1; |
| 493 | else |
| 494 | return 1; |
| 495 | } |
| 496 | |
| 497 | return strcmp(comm_l, comm_r); |
| 498 | } |
| 499 | |
| 500 | static size_t |
| 501 | sort__comm_print(FILE *fp, struct hist_entry *self) |
| 502 | { |
| 503 | return fprintf(fp, "%16s", self->thread->comm); |
| 504 | } |
| 505 | |
| 506 | static struct sort_entry sort_comm = { |
| 507 | .header = " Command", |
| 508 | .cmp = sort__comm_cmp, |
| 509 | .collapse = sort__comm_collapse, |
| 510 | .print = sort__comm_print, |
| 511 | }; |
| 512 | |
| 513 | /* --sort dso */ |
| 514 | |
| 515 | static int64_t |
| 516 | sort__dso_cmp(struct hist_entry *left, struct hist_entry *right) |
| 517 | { |
| 518 | struct dso *dso_l = left->dso; |
| 519 | struct dso *dso_r = right->dso; |
| 520 | |
| 521 | if (!dso_l || !dso_r) { |
| 522 | if (!dso_l && !dso_r) |
| 523 | return 0; |
| 524 | else if (!dso_l) |
| 525 | return -1; |
| 526 | else |
| 527 | return 1; |
| 528 | } |
| 529 | |
| 530 | return strcmp(dso_l->name, dso_r->name); |
| 531 | } |
| 532 | |
| 533 | static size_t |
| 534 | sort__dso_print(FILE *fp, struct hist_entry *self) |
| 535 | { |
| 536 | if (self->dso) |
| 537 | return fprintf(fp, "%-25s", self->dso->name); |
| 538 | |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 539 | return fprintf(fp, "%016llx ", (u64)self->ip); |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | static struct sort_entry sort_dso = { |
| 543 | .header = "Shared Object ", |
| 544 | .cmp = sort__dso_cmp, |
| 545 | .print = sort__dso_print, |
| 546 | }; |
| 547 | |
| 548 | /* --sort symbol */ |
| 549 | |
| 550 | static int64_t |
| 551 | sort__sym_cmp(struct hist_entry *left, struct hist_entry *right) |
| 552 | { |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 553 | u64 ip_l, ip_r; |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 554 | |
| 555 | if (left->sym == right->sym) |
| 556 | return 0; |
| 557 | |
| 558 | ip_l = left->sym ? left->sym->start : left->ip; |
| 559 | ip_r = right->sym ? right->sym->start : right->ip; |
| 560 | |
| 561 | return (int64_t)(ip_r - ip_l); |
| 562 | } |
| 563 | |
| 564 | static size_t |
| 565 | sort__sym_print(FILE *fp, struct hist_entry *self) |
| 566 | { |
| 567 | size_t ret = 0; |
| 568 | |
| 569 | if (verbose) |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 570 | ret += fprintf(fp, "%#018llx ", (u64)self->ip); |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 571 | |
| 572 | if (self->sym) { |
| 573 | ret += fprintf(fp, "[%c] %s", |
| 574 | self->dso == kernel_dso ? 'k' : '.', self->sym->name); |
| 575 | } else { |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 576 | ret += fprintf(fp, "%#016llx", (u64)self->ip); |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | return ret; |
| 580 | } |
| 581 | |
| 582 | static struct sort_entry sort_sym = { |
| 583 | .header = "Symbol", |
| 584 | .cmp = sort__sym_cmp, |
| 585 | .print = sort__sym_print, |
| 586 | }; |
| 587 | |
| 588 | static int sort__need_collapse = 0; |
| 589 | |
| 590 | struct sort_dimension { |
| 591 | char *name; |
| 592 | struct sort_entry *entry; |
| 593 | int taken; |
| 594 | }; |
| 595 | |
| 596 | static struct sort_dimension sort_dimensions[] = { |
| 597 | { .name = "pid", .entry = &sort_thread, }, |
| 598 | { .name = "comm", .entry = &sort_comm, }, |
| 599 | { .name = "dso", .entry = &sort_dso, }, |
| 600 | { .name = "symbol", .entry = &sort_sym, }, |
| 601 | }; |
| 602 | |
| 603 | static LIST_HEAD(hist_entry__sort_list); |
| 604 | |
| 605 | static int sort_dimension__add(char *tok) |
| 606 | { |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 607 | unsigned int i; |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 608 | |
| 609 | for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) { |
| 610 | struct sort_dimension *sd = &sort_dimensions[i]; |
| 611 | |
| 612 | if (sd->taken) |
| 613 | continue; |
| 614 | |
| 615 | if (strncasecmp(tok, sd->name, strlen(tok))) |
| 616 | continue; |
| 617 | |
| 618 | if (sd->entry->collapse) |
| 619 | sort__need_collapse = 1; |
| 620 | |
| 621 | list_add_tail(&sd->entry->list, &hist_entry__sort_list); |
| 622 | sd->taken = 1; |
| 623 | |
| 624 | return 0; |
| 625 | } |
| 626 | |
| 627 | return -ESRCH; |
| 628 | } |
| 629 | |
| 630 | static int64_t |
| 631 | hist_entry__cmp(struct hist_entry *left, struct hist_entry *right) |
| 632 | { |
| 633 | struct sort_entry *se; |
| 634 | int64_t cmp = 0; |
| 635 | |
| 636 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
| 637 | cmp = se->cmp(left, right); |
| 638 | if (cmp) |
| 639 | break; |
| 640 | } |
| 641 | |
| 642 | return cmp; |
| 643 | } |
| 644 | |
| 645 | static int64_t |
| 646 | hist_entry__collapse(struct hist_entry *left, struct hist_entry *right) |
| 647 | { |
| 648 | struct sort_entry *se; |
| 649 | int64_t cmp = 0; |
| 650 | |
| 651 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
| 652 | int64_t (*f)(struct hist_entry *, struct hist_entry *); |
| 653 | |
| 654 | f = se->collapse ?: se->cmp; |
| 655 | |
| 656 | cmp = f(left, right); |
| 657 | if (cmp) |
| 658 | break; |
| 659 | } |
| 660 | |
| 661 | return cmp; |
| 662 | } |
| 663 | |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 664 | /* |
| 665 | * collect histogram counts |
| 666 | */ |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 667 | static void hist_hit(struct hist_entry *he, u64 ip) |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 668 | { |
| 669 | unsigned int sym_size, offset; |
| 670 | struct symbol *sym = he->sym; |
| 671 | |
| 672 | he->count++; |
| 673 | |
| 674 | if (!sym || !sym->hist) |
| 675 | return; |
| 676 | |
| 677 | sym_size = sym->end - sym->start; |
| 678 | offset = ip - sym->start; |
| 679 | |
| 680 | if (offset >= sym_size) |
| 681 | return; |
| 682 | |
| 683 | sym->hist_sum++; |
| 684 | sym->hist[offset]++; |
| 685 | |
| 686 | if (verbose >= 3) |
| 687 | printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n", |
Arjan van de Ven | 7d37a0c | 2009-06-06 20:36:38 +0200 | [diff] [blame] | 688 | (void *)(unsigned long)he->sym->start, |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 689 | he->sym->name, |
Arjan van de Ven | 7d37a0c | 2009-06-06 20:36:38 +0200 | [diff] [blame] | 690 | (void *)(unsigned long)ip, ip - he->sym->start, |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 691 | sym->hist[offset]); |
| 692 | } |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 693 | |
| 694 | static int |
| 695 | hist_entry__add(struct thread *thread, struct map *map, struct dso *dso, |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 696 | struct symbol *sym, u64 ip, char level) |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 697 | { |
| 698 | struct rb_node **p = &hist.rb_node; |
| 699 | struct rb_node *parent = NULL; |
| 700 | struct hist_entry *he; |
| 701 | struct hist_entry entry = { |
| 702 | .thread = thread, |
| 703 | .map = map, |
| 704 | .dso = dso, |
| 705 | .sym = sym, |
| 706 | .ip = ip, |
| 707 | .level = level, |
| 708 | .count = 1, |
| 709 | }; |
| 710 | int cmp; |
| 711 | |
| 712 | while (*p != NULL) { |
| 713 | parent = *p; |
| 714 | he = rb_entry(parent, struct hist_entry, rb_node); |
| 715 | |
| 716 | cmp = hist_entry__cmp(&entry, he); |
| 717 | |
| 718 | if (!cmp) { |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 719 | hist_hit(he, ip); |
| 720 | |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 721 | return 0; |
| 722 | } |
| 723 | |
| 724 | if (cmp < 0) |
| 725 | p = &(*p)->rb_left; |
| 726 | else |
| 727 | p = &(*p)->rb_right; |
| 728 | } |
| 729 | |
| 730 | he = malloc(sizeof(*he)); |
| 731 | if (!he) |
| 732 | return -ENOMEM; |
| 733 | *he = entry; |
| 734 | rb_link_node(&he->rb_node, parent, p); |
| 735 | rb_insert_color(&he->rb_node, &hist); |
| 736 | |
| 737 | return 0; |
| 738 | } |
| 739 | |
| 740 | static void hist_entry__free(struct hist_entry *he) |
| 741 | { |
| 742 | free(he); |
| 743 | } |
| 744 | |
| 745 | /* |
| 746 | * collapse the histogram |
| 747 | */ |
| 748 | |
| 749 | static struct rb_root collapse_hists; |
| 750 | |
| 751 | static void collapse__insert_entry(struct hist_entry *he) |
| 752 | { |
| 753 | struct rb_node **p = &collapse_hists.rb_node; |
| 754 | struct rb_node *parent = NULL; |
| 755 | struct hist_entry *iter; |
| 756 | int64_t cmp; |
| 757 | |
| 758 | while (*p != NULL) { |
| 759 | parent = *p; |
| 760 | iter = rb_entry(parent, struct hist_entry, rb_node); |
| 761 | |
| 762 | cmp = hist_entry__collapse(iter, he); |
| 763 | |
| 764 | if (!cmp) { |
| 765 | iter->count += he->count; |
| 766 | hist_entry__free(he); |
| 767 | return; |
| 768 | } |
| 769 | |
| 770 | if (cmp < 0) |
| 771 | p = &(*p)->rb_left; |
| 772 | else |
| 773 | p = &(*p)->rb_right; |
| 774 | } |
| 775 | |
| 776 | rb_link_node(&he->rb_node, parent, p); |
| 777 | rb_insert_color(&he->rb_node, &collapse_hists); |
| 778 | } |
| 779 | |
| 780 | static void collapse__resort(void) |
| 781 | { |
| 782 | struct rb_node *next; |
| 783 | struct hist_entry *n; |
| 784 | |
| 785 | if (!sort__need_collapse) |
| 786 | return; |
| 787 | |
| 788 | next = rb_first(&hist); |
| 789 | while (next) { |
| 790 | n = rb_entry(next, struct hist_entry, rb_node); |
| 791 | next = rb_next(&n->rb_node); |
| 792 | |
| 793 | rb_erase(&n->rb_node, &hist); |
| 794 | collapse__insert_entry(n); |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | /* |
| 799 | * reverse the map, sort on count. |
| 800 | */ |
| 801 | |
| 802 | static struct rb_root output_hists; |
| 803 | |
| 804 | static void output__insert_entry(struct hist_entry *he) |
| 805 | { |
| 806 | struct rb_node **p = &output_hists.rb_node; |
| 807 | struct rb_node *parent = NULL; |
| 808 | struct hist_entry *iter; |
| 809 | |
| 810 | while (*p != NULL) { |
| 811 | parent = *p; |
| 812 | iter = rb_entry(parent, struct hist_entry, rb_node); |
| 813 | |
| 814 | if (he->count > iter->count) |
| 815 | p = &(*p)->rb_left; |
| 816 | else |
| 817 | p = &(*p)->rb_right; |
| 818 | } |
| 819 | |
| 820 | rb_link_node(&he->rb_node, parent, p); |
| 821 | rb_insert_color(&he->rb_node, &output_hists); |
| 822 | } |
| 823 | |
| 824 | static void output__resort(void) |
| 825 | { |
| 826 | struct rb_node *next; |
| 827 | struct hist_entry *n; |
| 828 | struct rb_root *tree = &hist; |
| 829 | |
| 830 | if (sort__need_collapse) |
| 831 | tree = &collapse_hists; |
| 832 | |
| 833 | next = rb_first(tree); |
| 834 | |
| 835 | while (next) { |
| 836 | n = rb_entry(next, struct hist_entry, rb_node); |
| 837 | next = rb_next(&n->rb_node); |
| 838 | |
| 839 | rb_erase(&n->rb_node, tree); |
| 840 | output__insert_entry(n); |
| 841 | } |
| 842 | } |
| 843 | |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 844 | static void register_idle_thread(void) |
| 845 | { |
| 846 | struct thread *thread = threads__findnew(0); |
| 847 | |
| 848 | if (thread == NULL || |
| 849 | thread__set_comm(thread, "[idle]")) { |
| 850 | fprintf(stderr, "problem inserting idle task.\n"); |
| 851 | exit(-1); |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | static unsigned long total = 0, |
| 856 | total_mmap = 0, |
| 857 | total_comm = 0, |
| 858 | total_fork = 0, |
| 859 | total_unknown = 0; |
| 860 | |
| 861 | static int |
Peter Zijlstra | e6e18ec | 2009-06-25 11:27:12 +0200 | [diff] [blame] | 862 | process_sample_event(event_t *event, unsigned long offset, unsigned long head) |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 863 | { |
| 864 | char level; |
| 865 | int show = 0; |
| 866 | struct dso *dso = NULL; |
| 867 | struct thread *thread = threads__findnew(event->ip.pid); |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 868 | u64 ip = event->ip.ip; |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 869 | struct map *map = NULL; |
| 870 | |
| 871 | dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n", |
| 872 | (void *)(offset + head), |
| 873 | (void *)(long)(event->header.size), |
| 874 | event->header.misc, |
| 875 | event->ip.pid, |
| 876 | (void *)(long)ip); |
| 877 | |
| 878 | dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid); |
| 879 | |
| 880 | if (thread == NULL) { |
| 881 | fprintf(stderr, "problem processing %d event, skipping it.\n", |
| 882 | event->header.type); |
| 883 | return -1; |
| 884 | } |
| 885 | |
| 886 | if (event->header.misc & PERF_EVENT_MISC_KERNEL) { |
| 887 | show = SHOW_KERNEL; |
| 888 | level = 'k'; |
| 889 | |
| 890 | dso = kernel_dso; |
| 891 | |
| 892 | dprintf(" ...... dso: %s\n", dso->name); |
| 893 | |
| 894 | } else if (event->header.misc & PERF_EVENT_MISC_USER) { |
| 895 | |
| 896 | show = SHOW_USER; |
| 897 | level = '.'; |
| 898 | |
| 899 | map = thread__find_map(thread, ip); |
| 900 | if (map != NULL) { |
| 901 | ip = map->map_ip(map, ip); |
| 902 | dso = map->dso; |
| 903 | } else { |
| 904 | /* |
| 905 | * If this is outside of all known maps, |
| 906 | * and is a negative address, try to look it |
| 907 | * up in the kernel dso, as it might be a |
| 908 | * vsyscall (which executes in user-mode): |
| 909 | */ |
| 910 | if ((long long)ip < 0) |
| 911 | dso = kernel_dso; |
| 912 | } |
| 913 | dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>"); |
| 914 | |
| 915 | } else { |
| 916 | show = SHOW_HV; |
| 917 | level = 'H'; |
| 918 | dprintf(" ...... dso: [hypervisor]\n"); |
| 919 | } |
| 920 | |
| 921 | if (show & show_mask) { |
| 922 | struct symbol *sym = NULL; |
| 923 | |
| 924 | if (dso) |
| 925 | sym = dso->find_symbol(dso, ip); |
| 926 | |
| 927 | if (hist_entry__add(thread, map, dso, sym, ip, level)) { |
| 928 | fprintf(stderr, |
| 929 | "problem incrementing symbol count, skipping event\n"); |
| 930 | return -1; |
| 931 | } |
| 932 | } |
| 933 | total++; |
| 934 | |
| 935 | return 0; |
| 936 | } |
| 937 | |
| 938 | static int |
| 939 | process_mmap_event(event_t *event, unsigned long offset, unsigned long head) |
| 940 | { |
| 941 | struct thread *thread = threads__findnew(event->mmap.pid); |
| 942 | struct map *map = map__new(&event->mmap); |
| 943 | |
| 944 | dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n", |
| 945 | (void *)(offset + head), |
| 946 | (void *)(long)(event->header.size), |
| 947 | event->mmap.pid, |
| 948 | (void *)(long)event->mmap.start, |
| 949 | (void *)(long)event->mmap.len, |
| 950 | (void *)(long)event->mmap.pgoff, |
| 951 | event->mmap.filename); |
| 952 | |
| 953 | if (thread == NULL || map == NULL) { |
| 954 | dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n"); |
| 955 | return 0; |
| 956 | } |
| 957 | |
| 958 | thread__insert_map(thread, map); |
| 959 | total_mmap++; |
| 960 | |
| 961 | return 0; |
| 962 | } |
| 963 | |
| 964 | static int |
| 965 | process_comm_event(event_t *event, unsigned long offset, unsigned long head) |
| 966 | { |
| 967 | struct thread *thread = threads__findnew(event->comm.pid); |
| 968 | |
| 969 | dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n", |
| 970 | (void *)(offset + head), |
| 971 | (void *)(long)(event->header.size), |
| 972 | event->comm.comm, event->comm.pid); |
| 973 | |
| 974 | if (thread == NULL || |
| 975 | thread__set_comm(thread, event->comm.comm)) { |
| 976 | dprintf("problem processing PERF_EVENT_COMM, skipping event.\n"); |
| 977 | return -1; |
| 978 | } |
| 979 | total_comm++; |
| 980 | |
| 981 | return 0; |
| 982 | } |
| 983 | |
| 984 | static int |
| 985 | process_fork_event(event_t *event, unsigned long offset, unsigned long head) |
| 986 | { |
| 987 | struct thread *thread = threads__findnew(event->fork.pid); |
| 988 | struct thread *parent = threads__findnew(event->fork.ppid); |
| 989 | |
| 990 | dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n", |
| 991 | (void *)(offset + head), |
| 992 | (void *)(long)(event->header.size), |
| 993 | event->fork.pid, event->fork.ppid); |
| 994 | |
| 995 | if (!thread || !parent || thread__fork(thread, parent)) { |
| 996 | dprintf("problem processing PERF_EVENT_FORK, skipping event.\n"); |
| 997 | return -1; |
| 998 | } |
| 999 | total_fork++; |
| 1000 | |
| 1001 | return 0; |
| 1002 | } |
| 1003 | |
| 1004 | static int |
| 1005 | process_period_event(event_t *event, unsigned long offset, unsigned long head) |
| 1006 | { |
| 1007 | dprintf("%p [%p]: PERF_EVENT_PERIOD: time:%Ld, id:%Ld: period:%Ld\n", |
| 1008 | (void *)(offset + head), |
| 1009 | (void *)(long)(event->header.size), |
| 1010 | event->period.time, |
| 1011 | event->period.id, |
| 1012 | event->period.sample_period); |
| 1013 | |
| 1014 | return 0; |
| 1015 | } |
| 1016 | |
| 1017 | static int |
| 1018 | process_event(event_t *event, unsigned long offset, unsigned long head) |
| 1019 | { |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 1020 | switch (event->header.type) { |
Peter Zijlstra | e6e18ec | 2009-06-25 11:27:12 +0200 | [diff] [blame] | 1021 | case PERF_EVENT_SAMPLE: |
| 1022 | return process_sample_event(event, offset, head); |
| 1023 | |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 1024 | case PERF_EVENT_MMAP: |
| 1025 | return process_mmap_event(event, offset, head); |
| 1026 | |
| 1027 | case PERF_EVENT_COMM: |
| 1028 | return process_comm_event(event, offset, head); |
| 1029 | |
| 1030 | case PERF_EVENT_FORK: |
| 1031 | return process_fork_event(event, offset, head); |
| 1032 | |
| 1033 | case PERF_EVENT_PERIOD: |
| 1034 | return process_period_event(event, offset, head); |
| 1035 | /* |
| 1036 | * We dont process them right now but they are fine: |
| 1037 | */ |
| 1038 | |
| 1039 | case PERF_EVENT_THROTTLE: |
| 1040 | case PERF_EVENT_UNTHROTTLE: |
| 1041 | return 0; |
| 1042 | |
| 1043 | default: |
| 1044 | return -1; |
| 1045 | } |
| 1046 | |
| 1047 | return 0; |
| 1048 | } |
| 1049 | |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 1050 | static char *get_color(double percent) |
| 1051 | { |
| 1052 | char *color = PERF_COLOR_NORMAL; |
| 1053 | |
| 1054 | /* |
| 1055 | * We color high-overhead entries in red, mid-overhead |
| 1056 | * entries in green - and keep the low overhead places |
| 1057 | * normal: |
| 1058 | */ |
| 1059 | if (percent >= MIN_RED) |
| 1060 | color = PERF_COLOR_RED; |
| 1061 | else { |
| 1062 | if (percent > MIN_GREEN) |
| 1063 | color = PERF_COLOR_GREEN; |
| 1064 | } |
| 1065 | return color; |
| 1066 | } |
| 1067 | |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 1068 | static int |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 1069 | parse_line(FILE *file, struct symbol *sym, u64 start, u64 len) |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 1070 | { |
| 1071 | char *line = NULL, *tmp, *tmp2; |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 1072 | static const char *prev_line; |
| 1073 | static const char *prev_color; |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 1074 | unsigned int offset; |
| 1075 | size_t line_len; |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 1076 | s64 line_ip; |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 1077 | int ret; |
| 1078 | char *c; |
| 1079 | |
| 1080 | if (getline(&line, &line_len, file) < 0) |
| 1081 | return -1; |
| 1082 | if (!line) |
| 1083 | return -1; |
| 1084 | |
| 1085 | c = strchr(line, '\n'); |
| 1086 | if (c) |
| 1087 | *c = 0; |
| 1088 | |
| 1089 | line_ip = -1; |
| 1090 | offset = 0; |
| 1091 | ret = -2; |
| 1092 | |
| 1093 | /* |
| 1094 | * Strip leading spaces: |
| 1095 | */ |
| 1096 | tmp = line; |
| 1097 | while (*tmp) { |
| 1098 | if (*tmp != ' ') |
| 1099 | break; |
| 1100 | tmp++; |
| 1101 | } |
| 1102 | |
| 1103 | if (*tmp) { |
| 1104 | /* |
| 1105 | * Parse hexa addresses followed by ':' |
| 1106 | */ |
| 1107 | line_ip = strtoull(tmp, &tmp2, 16); |
| 1108 | if (*tmp2 != ':') |
| 1109 | line_ip = -1; |
| 1110 | } |
| 1111 | |
| 1112 | if (line_ip != -1) { |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 1113 | const char *path = NULL; |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 1114 | unsigned int hits = 0; |
| 1115 | double percent = 0.0; |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 1116 | char *color; |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 1117 | struct sym_ext *sym_ext = sym->priv; |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 1118 | |
| 1119 | offset = line_ip - start; |
| 1120 | if (offset < len) |
| 1121 | hits = sym->hist[offset]; |
| 1122 | |
Frederic Weisbecker | c17c2db1 | 2009-06-13 17:39:23 +0200 | [diff] [blame] | 1123 | if (offset < len && sym_ext) { |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 1124 | path = sym_ext[offset].path; |
| 1125 | percent = sym_ext[offset].percent; |
| 1126 | } else if (sym->hist_sum) |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 1127 | percent = 100.0 * hits / sym->hist_sum; |
| 1128 | |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 1129 | color = get_color(percent); |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 1130 | |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 1131 | /* |
| 1132 | * Also color the filename and line if needed, with |
| 1133 | * the same color than the percentage. Don't print it |
| 1134 | * twice for close colored ip with the same filename:line |
| 1135 | */ |
| 1136 | if (path) { |
| 1137 | if (!prev_line || strcmp(prev_line, path) |
| 1138 | || color != prev_color) { |
| 1139 | color_fprintf(stdout, color, " %s", path); |
| 1140 | prev_line = path; |
| 1141 | prev_color = color; |
| 1142 | } |
| 1143 | } |
| 1144 | |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 1145 | color_fprintf(stdout, color, " %7.2f", percent); |
| 1146 | printf(" : "); |
| 1147 | color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", line); |
| 1148 | } else { |
| 1149 | if (!*line) |
| 1150 | printf(" :\n"); |
| 1151 | else |
| 1152 | printf(" : %s\n", line); |
| 1153 | } |
| 1154 | |
| 1155 | return 0; |
| 1156 | } |
| 1157 | |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 1158 | static struct rb_root root_sym_ext; |
| 1159 | |
| 1160 | static void insert_source_line(struct sym_ext *sym_ext) |
| 1161 | { |
| 1162 | struct sym_ext *iter; |
| 1163 | struct rb_node **p = &root_sym_ext.rb_node; |
| 1164 | struct rb_node *parent = NULL; |
| 1165 | |
| 1166 | while (*p != NULL) { |
| 1167 | parent = *p; |
| 1168 | iter = rb_entry(parent, struct sym_ext, node); |
| 1169 | |
| 1170 | if (sym_ext->percent > iter->percent) |
| 1171 | p = &(*p)->rb_left; |
| 1172 | else |
| 1173 | p = &(*p)->rb_right; |
| 1174 | } |
| 1175 | |
| 1176 | rb_link_node(&sym_ext->node, parent, p); |
| 1177 | rb_insert_color(&sym_ext->node, &root_sym_ext); |
| 1178 | } |
| 1179 | |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 1180 | static void free_source_line(struct symbol *sym, int len) |
| 1181 | { |
| 1182 | struct sym_ext *sym_ext = sym->priv; |
| 1183 | int i; |
| 1184 | |
| 1185 | if (!sym_ext) |
| 1186 | return; |
| 1187 | |
| 1188 | for (i = 0; i < len; i++) |
| 1189 | free(sym_ext[i].path); |
| 1190 | free(sym_ext); |
| 1191 | |
| 1192 | sym->priv = NULL; |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 1193 | root_sym_ext = RB_ROOT; |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | /* Get the filename:line for the colored entries */ |
Frederic Weisbecker | c17c2db1 | 2009-06-13 17:39:23 +0200 | [diff] [blame] | 1197 | static void |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 1198 | get_source_line(struct symbol *sym, u64 start, int len, char *filename) |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 1199 | { |
| 1200 | int i; |
| 1201 | char cmd[PATH_MAX * 2]; |
| 1202 | struct sym_ext *sym_ext; |
| 1203 | |
| 1204 | if (!sym->hist_sum) |
| 1205 | return; |
| 1206 | |
| 1207 | sym->priv = calloc(len, sizeof(struct sym_ext)); |
| 1208 | if (!sym->priv) |
| 1209 | return; |
| 1210 | |
| 1211 | sym_ext = sym->priv; |
| 1212 | |
| 1213 | for (i = 0; i < len; i++) { |
| 1214 | char *path = NULL; |
| 1215 | size_t line_len; |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 1216 | u64 offset; |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 1217 | FILE *fp; |
| 1218 | |
| 1219 | sym_ext[i].percent = 100.0 * sym->hist[i] / sym->hist_sum; |
| 1220 | if (sym_ext[i].percent <= 0.5) |
| 1221 | continue; |
| 1222 | |
| 1223 | offset = start + i; |
Frederic Weisbecker | c17c2db1 | 2009-06-13 17:39:23 +0200 | [diff] [blame] | 1224 | sprintf(cmd, "addr2line -e %s %016llx", filename, offset); |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 1225 | fp = popen(cmd, "r"); |
| 1226 | if (!fp) |
| 1227 | continue; |
| 1228 | |
| 1229 | if (getline(&path, &line_len, fp) < 0 || !line_len) |
| 1230 | goto next; |
| 1231 | |
Frederic Weisbecker | c17c2db1 | 2009-06-13 17:39:23 +0200 | [diff] [blame] | 1232 | sym_ext[i].path = malloc(sizeof(char) * line_len + 1); |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 1233 | if (!sym_ext[i].path) |
| 1234 | goto next; |
| 1235 | |
| 1236 | strcpy(sym_ext[i].path, path); |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 1237 | insert_source_line(&sym_ext[i]); |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 1238 | |
| 1239 | next: |
| 1240 | pclose(fp); |
| 1241 | } |
| 1242 | } |
| 1243 | |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 1244 | static void print_summary(char *filename) |
| 1245 | { |
| 1246 | struct sym_ext *sym_ext; |
| 1247 | struct rb_node *node; |
| 1248 | |
| 1249 | printf("\nSorted summary for file %s\n", filename); |
| 1250 | printf("----------------------------------------------\n\n"); |
| 1251 | |
| 1252 | if (RB_EMPTY_ROOT(&root_sym_ext)) { |
| 1253 | printf(" Nothing higher than %1.1f%%\n", MIN_GREEN); |
| 1254 | return; |
| 1255 | } |
| 1256 | |
| 1257 | node = rb_first(&root_sym_ext); |
| 1258 | while (node) { |
| 1259 | double percent; |
| 1260 | char *color; |
| 1261 | char *path; |
| 1262 | |
| 1263 | sym_ext = rb_entry(node, struct sym_ext, node); |
| 1264 | percent = sym_ext->percent; |
| 1265 | color = get_color(percent); |
| 1266 | path = sym_ext->path; |
| 1267 | |
| 1268 | color_fprintf(stdout, color, " %7.2f %s", percent, path); |
| 1269 | node = rb_next(node); |
| 1270 | } |
| 1271 | } |
| 1272 | |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 1273 | static void annotate_sym(struct dso *dso, struct symbol *sym) |
| 1274 | { |
Mike Galbraith | 4297648 | 2009-07-02 08:09:46 +0200 | [diff] [blame^] | 1275 | char *filename = dso->name, *d_filename; |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 1276 | u64 start, end, len; |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 1277 | char command[PATH_MAX*2]; |
| 1278 | FILE *file; |
| 1279 | |
| 1280 | if (!filename) |
| 1281 | return; |
Mike Galbraith | 4297648 | 2009-07-02 08:09:46 +0200 | [diff] [blame^] | 1282 | if (sym->module) |
| 1283 | filename = sym->module->path; |
| 1284 | else if (dso == kernel_dso) |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 1285 | filename = vmlinux; |
| 1286 | |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 1287 | start = sym->obj_start; |
| 1288 | if (!start) |
| 1289 | start = sym->start; |
Mike Galbraith | 4297648 | 2009-07-02 08:09:46 +0200 | [diff] [blame^] | 1290 | if (full_paths) |
| 1291 | d_filename = filename; |
| 1292 | else |
| 1293 | d_filename = basename(filename); |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 1294 | |
| 1295 | end = start + sym->end - sym->start + 1; |
| 1296 | len = sym->end - sym->start; |
| 1297 | |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 1298 | if (print_line) { |
Frederic Weisbecker | c17c2db1 | 2009-06-13 17:39:23 +0200 | [diff] [blame] | 1299 | get_source_line(sym, start, len, filename); |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 1300 | print_summary(filename); |
| 1301 | } |
| 1302 | |
| 1303 | printf("\n\n------------------------------------------------\n"); |
Mike Galbraith | 4297648 | 2009-07-02 08:09:46 +0200 | [diff] [blame^] | 1304 | printf(" Percent | Source code & Disassembly of %s\n", d_filename); |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 1305 | printf("------------------------------------------------\n"); |
| 1306 | |
| 1307 | if (verbose >= 2) |
| 1308 | 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] | 1309 | |
Mike Galbraith | 4297648 | 2009-07-02 08:09:46 +0200 | [diff] [blame^] | 1310 | sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s", |
| 1311 | (u64)start, (u64)end, filename, filename); |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 1312 | |
| 1313 | if (verbose >= 3) |
| 1314 | printf("doing: %s\n", command); |
| 1315 | |
| 1316 | file = popen(command, "r"); |
| 1317 | if (!file) |
| 1318 | return; |
| 1319 | |
| 1320 | while (!feof(file)) { |
| 1321 | if (parse_line(file, sym, start, len) < 0) |
| 1322 | break; |
| 1323 | } |
| 1324 | |
| 1325 | pclose(file); |
Frederic Weisbecker | 971738f | 2009-06-13 00:11:22 +0200 | [diff] [blame] | 1326 | if (print_line) |
| 1327 | free_source_line(sym, len); |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 1328 | } |
| 1329 | |
| 1330 | static void find_annotations(void) |
| 1331 | { |
| 1332 | struct rb_node *nd; |
| 1333 | struct dso *dso; |
| 1334 | int count = 0; |
| 1335 | |
| 1336 | list_for_each_entry(dso, &dsos, node) { |
| 1337 | |
| 1338 | for (nd = rb_first(&dso->syms); nd; nd = rb_next(nd)) { |
| 1339 | struct symbol *sym = rb_entry(nd, struct symbol, rb_node); |
| 1340 | |
| 1341 | if (sym->hist) { |
| 1342 | annotate_sym(dso, sym); |
| 1343 | count++; |
| 1344 | } |
| 1345 | } |
| 1346 | } |
| 1347 | |
| 1348 | if (!count) |
| 1349 | printf(" Error: symbol '%s' not present amongst the samples.\n", sym_hist_filter); |
| 1350 | } |
| 1351 | |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 1352 | static int __cmd_annotate(void) |
| 1353 | { |
| 1354 | int ret, rc = EXIT_FAILURE; |
| 1355 | unsigned long offset = 0; |
| 1356 | unsigned long head = 0; |
| 1357 | struct stat stat; |
| 1358 | event_t *event; |
| 1359 | uint32_t size; |
| 1360 | char *buf; |
| 1361 | |
| 1362 | register_idle_thread(); |
| 1363 | |
| 1364 | input = open(input_name, O_RDONLY); |
| 1365 | if (input < 0) { |
| 1366 | perror("failed to open file"); |
| 1367 | exit(-1); |
| 1368 | } |
| 1369 | |
| 1370 | ret = fstat(input, &stat); |
| 1371 | if (ret < 0) { |
| 1372 | perror("failed to stat file"); |
| 1373 | exit(-1); |
| 1374 | } |
| 1375 | |
| 1376 | if (!stat.st_size) { |
| 1377 | fprintf(stderr, "zero-sized file, nothing to do!\n"); |
| 1378 | exit(0); |
| 1379 | } |
| 1380 | |
| 1381 | if (load_kernel() < 0) { |
| 1382 | perror("failed to load kernel symbols"); |
| 1383 | return EXIT_FAILURE; |
| 1384 | } |
| 1385 | |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 1386 | remap: |
| 1387 | buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ, |
| 1388 | MAP_SHARED, input, offset); |
| 1389 | if (buf == MAP_FAILED) { |
| 1390 | perror("failed to mmap file"); |
| 1391 | exit(-1); |
| 1392 | } |
| 1393 | |
| 1394 | more: |
| 1395 | event = (event_t *)(buf + head); |
| 1396 | |
| 1397 | size = event->header.size; |
| 1398 | if (!size) |
| 1399 | size = 8; |
| 1400 | |
| 1401 | if (head + event->header.size >= page_size * mmap_window) { |
| 1402 | unsigned long shift = page_size * (head / page_size); |
| 1403 | int ret; |
| 1404 | |
| 1405 | ret = munmap(buf, page_size * mmap_window); |
| 1406 | assert(ret == 0); |
| 1407 | |
| 1408 | offset += shift; |
| 1409 | head -= shift; |
| 1410 | goto remap; |
| 1411 | } |
| 1412 | |
| 1413 | size = event->header.size; |
| 1414 | |
| 1415 | dprintf("%p [%p]: event: %d\n", |
| 1416 | (void *)(offset + head), |
| 1417 | (void *)(long)event->header.size, |
| 1418 | event->header.type); |
| 1419 | |
| 1420 | if (!size || process_event(event, offset, head) < 0) { |
| 1421 | |
| 1422 | dprintf("%p [%p]: skipping unknown header type: %d\n", |
| 1423 | (void *)(offset + head), |
| 1424 | (void *)(long)(event->header.size), |
| 1425 | event->header.type); |
| 1426 | |
| 1427 | total_unknown++; |
| 1428 | |
| 1429 | /* |
| 1430 | * assume we lost track of the stream, check alignment, and |
| 1431 | * increment a single u64 in the hope to catch on again 'soon'. |
| 1432 | */ |
| 1433 | |
| 1434 | if (unlikely(head & 7)) |
| 1435 | head &= ~7ULL; |
| 1436 | |
| 1437 | size = 8; |
| 1438 | } |
| 1439 | |
| 1440 | head += size; |
| 1441 | |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 1442 | if (offset + head < (unsigned long)stat.st_size) |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 1443 | goto more; |
| 1444 | |
| 1445 | rc = EXIT_SUCCESS; |
| 1446 | close(input); |
| 1447 | |
| 1448 | dprintf(" IP events: %10ld\n", total); |
| 1449 | dprintf(" mmap events: %10ld\n", total_mmap); |
| 1450 | dprintf(" comm events: %10ld\n", total_comm); |
| 1451 | dprintf(" fork events: %10ld\n", total_fork); |
| 1452 | dprintf(" unknown events: %10ld\n", total_unknown); |
| 1453 | |
| 1454 | if (dump_trace) |
| 1455 | return 0; |
| 1456 | |
| 1457 | if (verbose >= 3) |
| 1458 | threads__fprintf(stdout); |
| 1459 | |
| 1460 | if (verbose >= 2) |
| 1461 | dsos__fprintf(stdout); |
| 1462 | |
| 1463 | collapse__resort(); |
| 1464 | output__resort(); |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 1465 | |
| 1466 | find_annotations(); |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 1467 | |
| 1468 | return rc; |
| 1469 | } |
| 1470 | |
| 1471 | static const char * const annotate_usage[] = { |
| 1472 | "perf annotate [<options>] <command>", |
| 1473 | NULL |
| 1474 | }; |
| 1475 | |
| 1476 | static const struct option options[] = { |
| 1477 | OPT_STRING('i', "input", &input_name, "file", |
| 1478 | "input file name"), |
Ingo Molnar | 23b8711 | 2009-06-06 21:25:29 +0200 | [diff] [blame] | 1479 | OPT_STRING('s', "symbol", &sym_hist_filter, "symbol", |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 1480 | "symbol to annotate"), |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 1481 | OPT_BOOLEAN('v', "verbose", &verbose, |
| 1482 | "be more verbose (show symbol address, etc)"), |
| 1483 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, |
| 1484 | "dump raw trace in ASCII"), |
| 1485 | OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"), |
Mike Galbraith | 4297648 | 2009-07-02 08:09:46 +0200 | [diff] [blame^] | 1486 | OPT_BOOLEAN('m', "modules", &modules, |
| 1487 | "load module symbols - WARNING: use only with -k and LIVE kernel"), |
Frederic Weisbecker | 301406b | 2009-06-13 00:11:21 +0200 | [diff] [blame] | 1488 | OPT_BOOLEAN('l', "print-line", &print_line, |
| 1489 | "print matching source lines (may be slow)"), |
Mike Galbraith | 4297648 | 2009-07-02 08:09:46 +0200 | [diff] [blame^] | 1490 | OPT_BOOLEAN('P', "full-paths", &full_paths, |
| 1491 | "Don't shorten the displayed pathnames"), |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 1492 | OPT_END() |
| 1493 | }; |
| 1494 | |
| 1495 | static void setup_sorting(void) |
| 1496 | { |
| 1497 | char *tmp, *tok, *str = strdup(sort_order); |
| 1498 | |
| 1499 | for (tok = strtok_r(str, ", ", &tmp); |
| 1500 | tok; tok = strtok_r(NULL, ", ", &tmp)) { |
| 1501 | if (sort_dimension__add(tok) < 0) { |
| 1502 | error("Unknown --sort key: `%s'", tok); |
| 1503 | usage_with_options(annotate_usage, options); |
| 1504 | } |
| 1505 | } |
| 1506 | |
| 1507 | free(str); |
| 1508 | } |
| 1509 | |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 1510 | int cmd_annotate(int argc, const char **argv, const char *prefix __used) |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 1511 | { |
| 1512 | symbol__init(); |
| 1513 | |
| 1514 | page_size = getpagesize(); |
| 1515 | |
| 1516 | argc = parse_options(argc, argv, options, annotate_usage, 0); |
| 1517 | |
| 1518 | setup_sorting(); |
| 1519 | |
Ingo Molnar | 0b73da3 | 2009-06-06 15:48:52 +0200 | [diff] [blame] | 1520 | if (argc) { |
| 1521 | /* |
| 1522 | * Special case: if there's an argument left then assume tha |
| 1523 | * it's a symbol filter: |
| 1524 | */ |
| 1525 | if (argc > 1) |
| 1526 | usage_with_options(annotate_usage, options); |
| 1527 | |
| 1528 | sym_hist_filter = argv[0]; |
| 1529 | } |
| 1530 | |
| 1531 | if (!sym_hist_filter) |
Ingo Molnar | 8035e42 | 2009-06-06 15:19:13 +0200 | [diff] [blame] | 1532 | usage_with_options(annotate_usage, options); |
| 1533 | |
| 1534 | setup_pager(); |
| 1535 | |
| 1536 | return __cmd_annotate(); |
| 1537 | } |