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