Commit | Line | Data |
---|---|---|
bf9e1876 IM |
1 | /* |
2 | * builtin-report.c | |
3 | * | |
4 | * Builtin report command: Analyze the perf.data input file, | |
5 | * look up and read DSOs and symbol information and display | |
6 | * a histogram of results, along various sorting keys. | |
7 | */ | |
16f762a2 | 8 | #include "builtin.h" |
53cb8bc2 | 9 | |
bf9e1876 IM |
10 | #include "util/util.h" |
11 | ||
8fc0321f | 12 | #include "util/color.h" |
35a50c8a | 13 | #include "util/list.h" |
a930d2c0 | 14 | #include "util/cache.h" |
35a50c8a | 15 | #include "util/rbtree.h" |
a2928c42 | 16 | #include "util/symbol.h" |
a0055ae2 | 17 | #include "util/string.h" |
8fa66bdc | 18 | |
53cb8bc2 IM |
19 | #include "perf.h" |
20 | ||
21 | #include "util/parse-options.h" | |
22 | #include "util/parse-events.h" | |
23 | ||
8fa66bdc ACM |
24 | #define SHOW_KERNEL 1 |
25 | #define SHOW_USER 2 | |
26 | #define SHOW_HV 4 | |
27 | ||
23ac9cbe | 28 | static char const *input_name = "perf.data"; |
450aaa2b | 29 | static char *vmlinux = NULL; |
bd74137e IM |
30 | |
31 | static char default_sort_order[] = "comm,dso"; | |
32 | static char *sort_order = default_sort_order; | |
33 | ||
8fa66bdc ACM |
34 | static int input; |
35 | static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV; | |
36 | ||
97b07b69 | 37 | static int dump_trace = 0; |
3502973d IM |
38 | #define dprintf(x...) do { if (dump_trace) printf(x); } while (0) |
39 | ||
16f762a2 | 40 | static int verbose; |
b78c07d4 | 41 | static int full_paths; |
97b07b69 | 42 | |
8fa66bdc ACM |
43 | static unsigned long page_size; |
44 | static unsigned long mmap_window = 32; | |
45 | ||
8fa66bdc ACM |
46 | struct ip_event { |
47 | struct perf_event_header header; | |
48 | __u64 ip; | |
49 | __u32 pid, tid; | |
50 | }; | |
75051724 | 51 | |
8fa66bdc ACM |
52 | struct mmap_event { |
53 | struct perf_event_header header; | |
54 | __u32 pid, tid; | |
55 | __u64 start; | |
56 | __u64 len; | |
57 | __u64 pgoff; | |
58 | char filename[PATH_MAX]; | |
59 | }; | |
75051724 | 60 | |
8fa66bdc ACM |
61 | struct comm_event { |
62 | struct perf_event_header header; | |
75051724 | 63 | __u32 pid, tid; |
8fa66bdc ACM |
64 | char comm[16]; |
65 | }; | |
66 | ||
62fc4453 PZ |
67 | struct fork_event { |
68 | struct perf_event_header header; | |
69 | __u32 pid, ppid; | |
70 | }; | |
71 | ||
8fa66bdc ACM |
72 | typedef union event_union { |
73 | struct perf_event_header header; | |
74 | struct ip_event ip; | |
75 | struct mmap_event mmap; | |
76 | struct comm_event comm; | |
62fc4453 | 77 | struct fork_event fork; |
8fa66bdc ACM |
78 | } event_t; |
79 | ||
8fa66bdc ACM |
80 | static LIST_HEAD(dsos); |
81 | static struct dso *kernel_dso; | |
82 | ||
83 | static void dsos__add(struct dso *dso) | |
84 | { | |
85 | list_add_tail(&dso->node, &dsos); | |
86 | } | |
87 | ||
88 | static struct dso *dsos__find(const char *name) | |
89 | { | |
90 | struct dso *pos; | |
91 | ||
92 | list_for_each_entry(pos, &dsos, node) | |
93 | if (strcmp(pos->name, name) == 0) | |
94 | return pos; | |
95 | return NULL; | |
96 | } | |
97 | ||
98 | static struct dso *dsos__findnew(const char *name) | |
99 | { | |
100 | struct dso *dso = dsos__find(name); | |
b7a16eac | 101 | int nr; |
8fa66bdc | 102 | |
4593bba8 IM |
103 | if (dso) |
104 | return dso; | |
105 | ||
106 | dso = dso__new(name, 0); | |
107 | if (!dso) | |
108 | goto out_delete_dso; | |
8fa66bdc | 109 | |
bd74137e | 110 | nr = dso__load(dso, NULL, verbose); |
4593bba8 | 111 | if (nr < 0) { |
bd74137e IM |
112 | if (verbose) |
113 | fprintf(stderr, "Failed to open: %s\n", name); | |
4593bba8 | 114 | goto out_delete_dso; |
8fa66bdc | 115 | } |
4593bba8 IM |
116 | if (!nr && verbose) { |
117 | fprintf(stderr, | |
118 | "No symbols found in: %s, maybe install a debug package?\n", | |
119 | name); | |
120 | } | |
121 | ||
122 | dsos__add(dso); | |
8fa66bdc ACM |
123 | |
124 | return dso; | |
125 | ||
126 | out_delete_dso: | |
127 | dso__delete(dso); | |
128 | return NULL; | |
129 | } | |
130 | ||
16f762a2 | 131 | static void dsos__fprintf(FILE *fp) |
8fa66bdc ACM |
132 | { |
133 | struct dso *pos; | |
134 | ||
135 | list_for_each_entry(pos, &dsos, node) | |
136 | dso__fprintf(pos, fp); | |
137 | } | |
138 | ||
450aaa2b PZ |
139 | static int load_kernel(void) |
140 | { | |
a827c875 | 141 | int err; |
450aaa2b | 142 | |
0085c954 | 143 | kernel_dso = dso__new("[kernel]", 0); |
450aaa2b | 144 | if (!kernel_dso) |
a2928c42 | 145 | return -1; |
450aaa2b | 146 | |
bd74137e | 147 | err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose); |
a2928c42 ACM |
148 | if (err) { |
149 | dso__delete(kernel_dso); | |
150 | kernel_dso = NULL; | |
151 | } else | |
152 | dsos__add(kernel_dso); | |
450aaa2b | 153 | |
a2928c42 | 154 | return err; |
450aaa2b PZ |
155 | } |
156 | ||
d80d338d IM |
157 | static char __cwd[PATH_MAX]; |
158 | static char *cwd = __cwd; | |
159 | static int cwdlen; | |
160 | ||
161 | static int strcommon(const char *pathname) | |
b78c07d4 ACM |
162 | { |
163 | int n = 0; | |
164 | ||
165 | while (pathname[n] == cwd[n] && n < cwdlen) | |
166 | ++n; | |
167 | ||
168 | return n; | |
169 | } | |
170 | ||
8fa66bdc ACM |
171 | struct map { |
172 | struct list_head node; | |
173 | uint64_t start; | |
174 | uint64_t end; | |
175 | uint64_t pgoff; | |
176 | struct dso *dso; | |
177 | }; | |
178 | ||
d80d338d | 179 | static struct map *map__new(struct mmap_event *event) |
8fa66bdc ACM |
180 | { |
181 | struct map *self = malloc(sizeof(*self)); | |
182 | ||
183 | if (self != NULL) { | |
b78c07d4 ACM |
184 | const char *filename = event->filename; |
185 | char newfilename[PATH_MAX]; | |
186 | ||
187 | if (cwd) { | |
d80d338d IM |
188 | int n = strcommon(filename); |
189 | ||
b78c07d4 ACM |
190 | if (n == cwdlen) { |
191 | snprintf(newfilename, sizeof(newfilename), | |
192 | ".%s", filename + n); | |
193 | filename = newfilename; | |
194 | } | |
195 | } | |
196 | ||
8fa66bdc ACM |
197 | self->start = event->start; |
198 | self->end = event->start + event->len; | |
199 | self->pgoff = event->pgoff; | |
200 | ||
b78c07d4 | 201 | self->dso = dsos__findnew(filename); |
8fa66bdc ACM |
202 | if (self->dso == NULL) |
203 | goto out_delete; | |
204 | } | |
205 | return self; | |
206 | out_delete: | |
207 | free(self); | |
208 | return NULL; | |
209 | } | |
210 | ||
62fc4453 PZ |
211 | static struct map *map__clone(struct map *self) |
212 | { | |
213 | struct map *map = malloc(sizeof(*self)); | |
214 | ||
215 | if (!map) | |
216 | return NULL; | |
217 | ||
218 | memcpy(map, self, sizeof(*self)); | |
219 | ||
220 | return map; | |
221 | } | |
222 | ||
223 | static int map__overlap(struct map *l, struct map *r) | |
224 | { | |
225 | if (l->start > r->start) { | |
226 | struct map *t = l; | |
227 | l = r; | |
228 | r = t; | |
229 | } | |
230 | ||
231 | if (l->end > r->start) | |
232 | return 1; | |
233 | ||
234 | return 0; | |
235 | } | |
3a4b8cc7 | 236 | |
8fa66bdc | 237 | struct thread { |
ce7e4365 | 238 | struct rb_node rb_node; |
8fa66bdc | 239 | struct list_head maps; |
8fa66bdc ACM |
240 | pid_t pid; |
241 | char *comm; | |
242 | }; | |
243 | ||
244 | static struct thread *thread__new(pid_t pid) | |
245 | { | |
246 | struct thread *self = malloc(sizeof(*self)); | |
247 | ||
248 | if (self != NULL) { | |
249 | self->pid = pid; | |
8229289b | 250 | self->comm = malloc(32); |
0a520c63 | 251 | if (self->comm) |
8229289b | 252 | snprintf(self->comm, 32, ":%d", self->pid); |
8fa66bdc | 253 | INIT_LIST_HEAD(&self->maps); |
8fa66bdc ACM |
254 | } |
255 | ||
256 | return self; | |
257 | } | |
258 | ||
8fa66bdc ACM |
259 | static int thread__set_comm(struct thread *self, const char *comm) |
260 | { | |
8229289b PZ |
261 | if (self->comm) |
262 | free(self->comm); | |
8fa66bdc ACM |
263 | self->comm = strdup(comm); |
264 | return self->comm ? 0 : -ENOMEM; | |
265 | } | |
266 | ||
16f762a2 | 267 | static struct rb_root threads; |
eed4dcd4 | 268 | static struct thread *last_match; |
8fa66bdc | 269 | |
ce7e4365 | 270 | static struct thread *threads__findnew(pid_t pid) |
8fa66bdc | 271 | { |
ce7e4365 ACM |
272 | struct rb_node **p = &threads.rb_node; |
273 | struct rb_node *parent = NULL; | |
274 | struct thread *th; | |
8fa66bdc | 275 | |
eed4dcd4 IM |
276 | /* |
277 | * Font-end cache - PID lookups come in blocks, | |
278 | * so most of the time we dont have to look up | |
279 | * the full rbtree: | |
280 | */ | |
281 | if (last_match && last_match->pid == pid) | |
282 | return last_match; | |
283 | ||
ce7e4365 ACM |
284 | while (*p != NULL) { |
285 | parent = *p; | |
286 | th = rb_entry(parent, struct thread, rb_node); | |
8fa66bdc | 287 | |
eed4dcd4 IM |
288 | if (th->pid == pid) { |
289 | last_match = th; | |
ce7e4365 | 290 | return th; |
eed4dcd4 | 291 | } |
8fa66bdc | 292 | |
ce7e4365 ACM |
293 | if (pid < th->pid) |
294 | p = &(*p)->rb_left; | |
295 | else | |
296 | p = &(*p)->rb_right; | |
8fa66bdc ACM |
297 | } |
298 | ||
ce7e4365 ACM |
299 | th = thread__new(pid); |
300 | if (th != NULL) { | |
301 | rb_link_node(&th->rb_node, parent, p); | |
302 | rb_insert_color(&th->rb_node, &threads); | |
eed4dcd4 | 303 | last_match = th; |
ce7e4365 | 304 | } |
eed4dcd4 | 305 | |
ce7e4365 | 306 | return th; |
8fa66bdc ACM |
307 | } |
308 | ||
309 | static void thread__insert_map(struct thread *self, struct map *map) | |
310 | { | |
62fc4453 PZ |
311 | struct map *pos, *tmp; |
312 | ||
313 | list_for_each_entry_safe(pos, tmp, &self->maps, node) { | |
314 | if (map__overlap(pos, map)) { | |
315 | list_del_init(&pos->node); | |
316 | /* XXX leaks dsos */ | |
317 | free(pos); | |
318 | } | |
319 | } | |
320 | ||
8fa66bdc ACM |
321 | list_add_tail(&map->node, &self->maps); |
322 | } | |
323 | ||
62fc4453 PZ |
324 | static int thread__fork(struct thread *self, struct thread *parent) |
325 | { | |
326 | struct map *map; | |
327 | ||
328 | if (self->comm) | |
329 | free(self->comm); | |
330 | self->comm = strdup(parent->comm); | |
331 | if (!self->comm) | |
332 | return -ENOMEM; | |
333 | ||
334 | list_for_each_entry(map, &parent->maps, node) { | |
335 | struct map *new = map__clone(map); | |
336 | if (!new) | |
337 | return -ENOMEM; | |
338 | thread__insert_map(self, new); | |
339 | } | |
340 | ||
341 | return 0; | |
342 | } | |
343 | ||
8fa66bdc ACM |
344 | static struct map *thread__find_map(struct thread *self, uint64_t ip) |
345 | { | |
16f762a2 IM |
346 | struct map *pos; |
347 | ||
8fa66bdc ACM |
348 | if (self == NULL) |
349 | return NULL; | |
350 | ||
8fa66bdc ACM |
351 | list_for_each_entry(pos, &self->maps, node) |
352 | if (ip >= pos->start && ip <= pos->end) | |
353 | return pos; | |
354 | ||
355 | return NULL; | |
356 | } | |
357 | ||
e7fb08b1 PZ |
358 | /* |
359 | * histogram, sorted on item, collects counts | |
360 | */ | |
361 | ||
362 | static struct rb_root hist; | |
363 | ||
364 | struct hist_entry { | |
365 | struct rb_node rb_node; | |
366 | ||
367 | struct thread *thread; | |
368 | struct map *map; | |
369 | struct dso *dso; | |
370 | struct symbol *sym; | |
371 | uint64_t ip; | |
372 | char level; | |
373 | ||
374 | uint32_t count; | |
375 | }; | |
376 | ||
1aa16738 PZ |
377 | /* |
378 | * configurable sorting bits | |
379 | */ | |
380 | ||
381 | struct sort_entry { | |
382 | struct list_head list; | |
383 | ||
ca8cdeef PZ |
384 | char *header; |
385 | ||
1aa16738 | 386 | int64_t (*cmp)(struct hist_entry *, struct hist_entry *); |
8229289b | 387 | int64_t (*collapse)(struct hist_entry *, struct hist_entry *); |
1aa16738 PZ |
388 | size_t (*print)(FILE *fp, struct hist_entry *); |
389 | }; | |
390 | ||
8229289b PZ |
391 | /* --sort pid */ |
392 | ||
e7fb08b1 | 393 | static int64_t |
1aa16738 | 394 | sort__thread_cmp(struct hist_entry *left, struct hist_entry *right) |
e7fb08b1 | 395 | { |
1aa16738 PZ |
396 | return right->thread->pid - left->thread->pid; |
397 | } | |
398 | ||
399 | static size_t | |
400 | sort__thread_print(FILE *fp, struct hist_entry *self) | |
401 | { | |
71dd8945 | 402 | return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid); |
1aa16738 | 403 | } |
e7fb08b1 | 404 | |
1aa16738 | 405 | static struct sort_entry sort_thread = { |
71dd8945 | 406 | .header = " Command: Pid", |
1aa16738 PZ |
407 | .cmp = sort__thread_cmp, |
408 | .print = sort__thread_print, | |
409 | }; | |
410 | ||
8229289b PZ |
411 | /* --sort comm */ |
412 | ||
992444b1 PZ |
413 | static int64_t |
414 | sort__comm_cmp(struct hist_entry *left, struct hist_entry *right) | |
8229289b PZ |
415 | { |
416 | return right->thread->pid - left->thread->pid; | |
417 | } | |
418 | ||
419 | static int64_t | |
420 | sort__comm_collapse(struct hist_entry *left, struct hist_entry *right) | |
992444b1 PZ |
421 | { |
422 | char *comm_l = left->thread->comm; | |
423 | char *comm_r = right->thread->comm; | |
424 | ||
425 | if (!comm_l || !comm_r) { | |
426 | if (!comm_l && !comm_r) | |
427 | return 0; | |
428 | else if (!comm_l) | |
429 | return -1; | |
430 | else | |
431 | return 1; | |
432 | } | |
433 | ||
434 | return strcmp(comm_l, comm_r); | |
435 | } | |
436 | ||
437 | static size_t | |
438 | sort__comm_print(FILE *fp, struct hist_entry *self) | |
439 | { | |
71dd8945 | 440 | return fprintf(fp, "%16s", self->thread->comm); |
992444b1 PZ |
441 | } |
442 | ||
443 | static struct sort_entry sort_comm = { | |
71dd8945 | 444 | .header = " Command", |
8229289b PZ |
445 | .cmp = sort__comm_cmp, |
446 | .collapse = sort__comm_collapse, | |
447 | .print = sort__comm_print, | |
992444b1 PZ |
448 | }; |
449 | ||
8229289b PZ |
450 | /* --sort dso */ |
451 | ||
55e5ec41 PZ |
452 | static int64_t |
453 | sort__dso_cmp(struct hist_entry *left, struct hist_entry *right) | |
454 | { | |
455 | struct dso *dso_l = left->dso; | |
456 | struct dso *dso_r = right->dso; | |
457 | ||
458 | if (!dso_l || !dso_r) { | |
459 | if (!dso_l && !dso_r) | |
460 | return 0; | |
461 | else if (!dso_l) | |
462 | return -1; | |
463 | else | |
464 | return 1; | |
465 | } | |
466 | ||
467 | return strcmp(dso_l->name, dso_r->name); | |
468 | } | |
469 | ||
470 | static size_t | |
471 | sort__dso_print(FILE *fp, struct hist_entry *self) | |
472 | { | |
0a520c63 | 473 | if (self->dso) |
71dd8945 | 474 | return fprintf(fp, "%-25s", self->dso->name); |
0a520c63 | 475 | |
71dd8945 | 476 | return fprintf(fp, "%016llx ", (__u64)self->ip); |
55e5ec41 PZ |
477 | } |
478 | ||
479 | static struct sort_entry sort_dso = { | |
71dd8945 | 480 | .header = "Shared Object ", |
55e5ec41 PZ |
481 | .cmp = sort__dso_cmp, |
482 | .print = sort__dso_print, | |
483 | }; | |
484 | ||
8229289b PZ |
485 | /* --sort symbol */ |
486 | ||
1aa16738 PZ |
487 | static int64_t |
488 | sort__sym_cmp(struct hist_entry *left, struct hist_entry *right) | |
489 | { | |
490 | uint64_t ip_l, ip_r; | |
e7fb08b1 PZ |
491 | |
492 | if (left->sym == right->sym) | |
493 | return 0; | |
494 | ||
495 | ip_l = left->sym ? left->sym->start : left->ip; | |
496 | ip_r = right->sym ? right->sym->start : right->ip; | |
497 | ||
498 | return (int64_t)(ip_r - ip_l); | |
499 | } | |
500 | ||
1aa16738 PZ |
501 | static size_t |
502 | sort__sym_print(FILE *fp, struct hist_entry *self) | |
503 | { | |
504 | size_t ret = 0; | |
505 | ||
1aa16738 | 506 | if (verbose) |
71dd8945 | 507 | ret += fprintf(fp, "%#018llx ", (__u64)self->ip); |
0a520c63 | 508 | |
0a520c63 | 509 | if (self->sym) |
71dd8945 | 510 | ret += fprintf(fp, "%s", self->sym->name); |
0a520c63 | 511 | else |
71dd8945 | 512 | ret += fprintf(fp, "%#016llx", (__u64)self->ip); |
1aa16738 PZ |
513 | |
514 | return ret; | |
515 | } | |
516 | ||
517 | static struct sort_entry sort_sym = { | |
71dd8945 | 518 | .header = "Symbol", |
ca8cdeef PZ |
519 | .cmp = sort__sym_cmp, |
520 | .print = sort__sym_print, | |
1aa16738 PZ |
521 | }; |
522 | ||
8229289b PZ |
523 | static int sort__need_collapse = 0; |
524 | ||
37f440cb PZ |
525 | struct sort_dimension { |
526 | char *name; | |
527 | struct sort_entry *entry; | |
528 | int taken; | |
529 | }; | |
530 | ||
531 | static struct sort_dimension sort_dimensions[] = { | |
532 | { .name = "pid", .entry = &sort_thread, }, | |
992444b1 | 533 | { .name = "comm", .entry = &sort_comm, }, |
55e5ec41 | 534 | { .name = "dso", .entry = &sort_dso, }, |
37f440cb PZ |
535 | { .name = "symbol", .entry = &sort_sym, }, |
536 | }; | |
537 | ||
1aa16738 PZ |
538 | static LIST_HEAD(hist_entry__sort_list); |
539 | ||
37f440cb PZ |
540 | static int sort_dimension__add(char *tok) |
541 | { | |
542 | int i; | |
543 | ||
544 | for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) { | |
545 | struct sort_dimension *sd = &sort_dimensions[i]; | |
546 | ||
547 | if (sd->taken) | |
548 | continue; | |
549 | ||
5352f35d | 550 | if (strncasecmp(tok, sd->name, strlen(tok))) |
37f440cb PZ |
551 | continue; |
552 | ||
8229289b PZ |
553 | if (sd->entry->collapse) |
554 | sort__need_collapse = 1; | |
555 | ||
37f440cb PZ |
556 | list_add_tail(&sd->entry->list, &hist_entry__sort_list); |
557 | sd->taken = 1; | |
5352f35d | 558 | |
37f440cb PZ |
559 | return 0; |
560 | } | |
561 | ||
562 | return -ESRCH; | |
563 | } | |
564 | ||
1aa16738 PZ |
565 | static int64_t |
566 | hist_entry__cmp(struct hist_entry *left, struct hist_entry *right) | |
567 | { | |
568 | struct sort_entry *se; | |
569 | int64_t cmp = 0; | |
570 | ||
571 | list_for_each_entry(se, &hist_entry__sort_list, list) { | |
572 | cmp = se->cmp(left, right); | |
573 | if (cmp) | |
574 | break; | |
575 | } | |
576 | ||
577 | return cmp; | |
578 | } | |
579 | ||
8229289b PZ |
580 | static int64_t |
581 | hist_entry__collapse(struct hist_entry *left, struct hist_entry *right) | |
582 | { | |
583 | struct sort_entry *se; | |
584 | int64_t cmp = 0; | |
585 | ||
586 | list_for_each_entry(se, &hist_entry__sort_list, list) { | |
587 | int64_t (*f)(struct hist_entry *, struct hist_entry *); | |
588 | ||
589 | f = se->collapse ?: se->cmp; | |
590 | ||
591 | cmp = f(left, right); | |
592 | if (cmp) | |
593 | break; | |
594 | } | |
595 | ||
596 | return cmp; | |
597 | } | |
598 | ||
1aa16738 PZ |
599 | static size_t |
600 | hist_entry__fprintf(FILE *fp, struct hist_entry *self, uint64_t total_samples) | |
601 | { | |
602 | struct sort_entry *se; | |
603 | size_t ret; | |
604 | ||
605 | if (total_samples) { | |
8fc0321f IM |
606 | double percent = self->count * 100.0 / total_samples; |
607 | char *color = PERF_COLOR_NORMAL; | |
608 | ||
609 | /* | |
610 | * We color high-overhead entries in red, low-overhead | |
611 | * entries in green - and keep the middle ground normal: | |
612 | */ | |
613 | if (percent >= 5.0) | |
614 | color = PERF_COLOR_RED; | |
615 | if (percent < 0.5) | |
616 | color = PERF_COLOR_GREEN; | |
617 | ||
618 | ret = color_fprintf(fp, color, " %6.2f%%", | |
1aa16738 PZ |
619 | (self->count * 100.0) / total_samples); |
620 | } else | |
621 | ret = fprintf(fp, "%12d ", self->count); | |
622 | ||
71dd8945 PZ |
623 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
624 | fprintf(fp, " "); | |
1aa16738 | 625 | ret += se->print(fp, self); |
71dd8945 | 626 | } |
1aa16738 PZ |
627 | |
628 | ret += fprintf(fp, "\n"); | |
629 | ||
630 | return ret; | |
631 | } | |
632 | ||
633 | /* | |
634 | * collect histogram counts | |
635 | */ | |
636 | ||
e7fb08b1 PZ |
637 | static int |
638 | hist_entry__add(struct thread *thread, struct map *map, struct dso *dso, | |
639 | struct symbol *sym, uint64_t ip, char level) | |
8fa66bdc | 640 | { |
e7fb08b1 PZ |
641 | struct rb_node **p = &hist.rb_node; |
642 | struct rb_node *parent = NULL; | |
643 | struct hist_entry *he; | |
644 | struct hist_entry entry = { | |
645 | .thread = thread, | |
646 | .map = map, | |
647 | .dso = dso, | |
648 | .sym = sym, | |
649 | .ip = ip, | |
650 | .level = level, | |
651 | .count = 1, | |
652 | }; | |
653 | int cmp; | |
654 | ||
655 | while (*p != NULL) { | |
656 | parent = *p; | |
657 | he = rb_entry(parent, struct hist_entry, rb_node); | |
658 | ||
659 | cmp = hist_entry__cmp(&entry, he); | |
660 | ||
661 | if (!cmp) { | |
662 | he->count++; | |
663 | return 0; | |
664 | } | |
665 | ||
666 | if (cmp < 0) | |
667 | p = &(*p)->rb_left; | |
668 | else | |
669 | p = &(*p)->rb_right; | |
ce7e4365 | 670 | } |
e7fb08b1 PZ |
671 | |
672 | he = malloc(sizeof(*he)); | |
673 | if (!he) | |
674 | return -ENOMEM; | |
675 | *he = entry; | |
676 | rb_link_node(&he->rb_node, parent, p); | |
677 | rb_insert_color(&he->rb_node, &hist); | |
678 | ||
679 | return 0; | |
8fa66bdc ACM |
680 | } |
681 | ||
8229289b PZ |
682 | static void hist_entry__free(struct hist_entry *he) |
683 | { | |
684 | free(he); | |
685 | } | |
686 | ||
687 | /* | |
688 | * collapse the histogram | |
689 | */ | |
690 | ||
691 | static struct rb_root collapse_hists; | |
692 | ||
693 | static void collapse__insert_entry(struct hist_entry *he) | |
694 | { | |
695 | struct rb_node **p = &collapse_hists.rb_node; | |
696 | struct rb_node *parent = NULL; | |
697 | struct hist_entry *iter; | |
698 | int64_t cmp; | |
699 | ||
700 | while (*p != NULL) { | |
701 | parent = *p; | |
702 | iter = rb_entry(parent, struct hist_entry, rb_node); | |
703 | ||
704 | cmp = hist_entry__collapse(iter, he); | |
705 | ||
706 | if (!cmp) { | |
707 | iter->count += he->count; | |
708 | hist_entry__free(he); | |
709 | return; | |
710 | } | |
711 | ||
712 | if (cmp < 0) | |
713 | p = &(*p)->rb_left; | |
714 | else | |
715 | p = &(*p)->rb_right; | |
716 | } | |
717 | ||
718 | rb_link_node(&he->rb_node, parent, p); | |
719 | rb_insert_color(&he->rb_node, &collapse_hists); | |
720 | } | |
721 | ||
722 | static void collapse__resort(void) | |
723 | { | |
724 | struct rb_node *next; | |
725 | struct hist_entry *n; | |
726 | ||
727 | if (!sort__need_collapse) | |
728 | return; | |
729 | ||
730 | next = rb_first(&hist); | |
731 | while (next) { | |
732 | n = rb_entry(next, struct hist_entry, rb_node); | |
733 | next = rb_next(&n->rb_node); | |
734 | ||
735 | rb_erase(&n->rb_node, &hist); | |
736 | collapse__insert_entry(n); | |
737 | } | |
738 | } | |
739 | ||
e7fb08b1 PZ |
740 | /* |
741 | * reverse the map, sort on count. | |
742 | */ | |
743 | ||
744 | static struct rb_root output_hists; | |
745 | ||
746 | static void output__insert_entry(struct hist_entry *he) | |
3a4b8cc7 | 747 | { |
e7fb08b1 | 748 | struct rb_node **p = &output_hists.rb_node; |
3a4b8cc7 | 749 | struct rb_node *parent = NULL; |
e7fb08b1 | 750 | struct hist_entry *iter; |
3a4b8cc7 ACM |
751 | |
752 | while (*p != NULL) { | |
753 | parent = *p; | |
e7fb08b1 | 754 | iter = rb_entry(parent, struct hist_entry, rb_node); |
3a4b8cc7 | 755 | |
e7fb08b1 | 756 | if (he->count > iter->count) |
3a4b8cc7 ACM |
757 | p = &(*p)->rb_left; |
758 | else | |
759 | p = &(*p)->rb_right; | |
760 | } | |
761 | ||
e7fb08b1 PZ |
762 | rb_link_node(&he->rb_node, parent, p); |
763 | rb_insert_color(&he->rb_node, &output_hists); | |
3a4b8cc7 ACM |
764 | } |
765 | ||
e7fb08b1 | 766 | static void output__resort(void) |
3a4b8cc7 | 767 | { |
8229289b | 768 | struct rb_node *next; |
e7fb08b1 | 769 | struct hist_entry *n; |
a4c43bea | 770 | struct rb_root *tree = &hist; |
3a4b8cc7 | 771 | |
8229289b | 772 | if (sort__need_collapse) |
a4c43bea ACM |
773 | tree = &collapse_hists; |
774 | ||
775 | next = rb_first(tree); | |
8229289b | 776 | |
e7fb08b1 PZ |
777 | while (next) { |
778 | n = rb_entry(next, struct hist_entry, rb_node); | |
779 | next = rb_next(&n->rb_node); | |
3a4b8cc7 | 780 | |
a4c43bea | 781 | rb_erase(&n->rb_node, tree); |
e7fb08b1 | 782 | output__insert_entry(n); |
3a4b8cc7 ACM |
783 | } |
784 | } | |
785 | ||
e7fb08b1 | 786 | static size_t output__fprintf(FILE *fp, uint64_t total_samples) |
3a4b8cc7 | 787 | { |
e7fb08b1 | 788 | struct hist_entry *pos; |
2d65537e | 789 | struct sort_entry *se; |
3a4b8cc7 ACM |
790 | struct rb_node *nd; |
791 | size_t ret = 0; | |
792 | ||
71dd8945 | 793 | fprintf(fp, "\n"); |
05ca061e IM |
794 | fprintf(fp, "#\n"); |
795 | fprintf(fp, "# (%Ld profiler events)\n", (__u64)total_samples); | |
ca8cdeef PZ |
796 | fprintf(fp, "#\n"); |
797 | ||
798 | fprintf(fp, "# Overhead"); | |
799 | list_for_each_entry(se, &hist_entry__sort_list, list) | |
71dd8945 | 800 | fprintf(fp, " %s", se->header); |
ca8cdeef PZ |
801 | fprintf(fp, "\n"); |
802 | ||
803 | fprintf(fp, "# ........"); | |
2d65537e | 804 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
ca8cdeef PZ |
805 | int i; |
806 | ||
4593bba8 | 807 | fprintf(fp, " "); |
71dd8945 | 808 | for (i = 0; i < strlen(se->header); i++) |
ca8cdeef | 809 | fprintf(fp, "."); |
2d65537e | 810 | } |
ca8cdeef PZ |
811 | fprintf(fp, "\n"); |
812 | ||
813 | fprintf(fp, "#\n"); | |
2d65537e | 814 | |
e7fb08b1 PZ |
815 | for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) { |
816 | pos = rb_entry(nd, struct hist_entry, rb_node); | |
817 | ret += hist_entry__fprintf(fp, pos, total_samples); | |
3a4b8cc7 ACM |
818 | } |
819 | ||
bd74137e IM |
820 | if (!strcmp(sort_order, default_sort_order)) { |
821 | fprintf(fp, "#\n"); | |
71dd8945 | 822 | fprintf(fp, "# (For more details, try: perf report --sort comm,dso,symbol)\n"); |
bd74137e IM |
823 | fprintf(fp, "#\n"); |
824 | } | |
71dd8945 | 825 | fprintf(fp, "\n"); |
bd74137e | 826 | |
3a4b8cc7 ACM |
827 | return ret; |
828 | } | |
829 | ||
436224a6 PZ |
830 | static void register_idle_thread(void) |
831 | { | |
832 | struct thread *thread = threads__findnew(0); | |
833 | ||
834 | if (thread == NULL || | |
835 | thread__set_comm(thread, "[idle]")) { | |
836 | fprintf(stderr, "problem inserting idle task.\n"); | |
837 | exit(-1); | |
838 | } | |
839 | } | |
840 | ||
62fc4453 PZ |
841 | static unsigned long total = 0, |
842 | total_mmap = 0, | |
843 | total_comm = 0, | |
844 | total_fork = 0, | |
845 | total_unknown = 0; | |
e7fb08b1 | 846 | |
d80d338d | 847 | static int |
75051724 IM |
848 | process_overflow_event(event_t *event, unsigned long offset, unsigned long head) |
849 | { | |
850 | char level; | |
851 | int show = 0; | |
852 | struct dso *dso = NULL; | |
853 | struct thread *thread = threads__findnew(event->ip.pid); | |
854 | uint64_t ip = event->ip.ip; | |
855 | struct map *map = NULL; | |
856 | ||
857 | dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n", | |
858 | (void *)(offset + head), | |
859 | (void *)(long)(event->header.size), | |
860 | event->header.misc, | |
861 | event->ip.pid, | |
862 | (void *)(long)ip); | |
863 | ||
864 | dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid); | |
865 | ||
866 | if (thread == NULL) { | |
867 | fprintf(stderr, "problem processing %d event, skipping it.\n", | |
868 | event->header.type); | |
869 | return -1; | |
870 | } | |
e7fb08b1 | 871 | |
75051724 IM |
872 | if (event->header.misc & PERF_EVENT_MISC_KERNEL) { |
873 | show = SHOW_KERNEL; | |
874 | level = 'k'; | |
e7fb08b1 | 875 | |
75051724 | 876 | dso = kernel_dso; |
ed966aac | 877 | |
75051724 | 878 | dprintf(" ...... dso: %s\n", dso->name); |
16f762a2 | 879 | |
75051724 | 880 | } else if (event->header.misc & PERF_EVENT_MISC_USER) { |
16f762a2 | 881 | |
75051724 IM |
882 | show = SHOW_USER; |
883 | level = '.'; | |
e7fb08b1 | 884 | |
75051724 IM |
885 | map = thread__find_map(thread, ip); |
886 | if (map != NULL) { | |
887 | dso = map->dso; | |
888 | ip -= map->start + map->pgoff; | |
8fa66bdc | 889 | } else { |
75051724 IM |
890 | /* |
891 | * If this is outside of all known maps, | |
892 | * and is a negative address, try to look it | |
893 | * up in the kernel dso, as it might be a | |
894 | * vsyscall (which executes in user-mode): | |
895 | */ | |
896 | if ((long long)ip < 0) | |
897 | dso = kernel_dso; | |
8fa66bdc | 898 | } |
75051724 IM |
899 | dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>"); |
900 | ||
901 | } else { | |
902 | show = SHOW_HV; | |
903 | level = 'H'; | |
904 | dprintf(" ...... dso: [hypervisor]\n"); | |
905 | } | |
8fa66bdc | 906 | |
75051724 IM |
907 | if (show & show_mask) { |
908 | struct symbol *sym = dso__find_symbol(dso, ip); | |
8fa66bdc | 909 | |
75051724 IM |
910 | if (hist_entry__add(thread, map, dso, sym, ip, level)) { |
911 | fprintf(stderr, | |
55717314 | 912 | "problem incrementing symbol count, skipping event\n"); |
d80d338d | 913 | return -1; |
ce7e4365 | 914 | } |
8fa66bdc | 915 | } |
75051724 | 916 | total++; |
8fa66bdc | 917 | |
75051724 IM |
918 | return 0; |
919 | } | |
3502973d | 920 | |
75051724 IM |
921 | static int |
922 | process_mmap_event(event_t *event, unsigned long offset, unsigned long head) | |
923 | { | |
924 | struct thread *thread = threads__findnew(event->mmap.pid); | |
925 | struct map *map = map__new(&event->mmap); | |
926 | ||
62fc4453 | 927 | dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n", |
75051724 IM |
928 | (void *)(offset + head), |
929 | (void *)(long)(event->header.size), | |
62fc4453 | 930 | event->mmap.pid, |
75051724 IM |
931 | (void *)(long)event->mmap.start, |
932 | (void *)(long)event->mmap.len, | |
933 | (void *)(long)event->mmap.pgoff, | |
934 | event->mmap.filename); | |
935 | ||
936 | if (thread == NULL || map == NULL) { | |
937 | dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n"); | |
df97992c | 938 | return 0; |
75051724 IM |
939 | } |
940 | ||
941 | thread__insert_map(thread, map); | |
942 | total_mmap++; | |
943 | ||
944 | return 0; | |
945 | } | |
946 | ||
947 | static int | |
948 | process_comm_event(event_t *event, unsigned long offset, unsigned long head) | |
949 | { | |
950 | struct thread *thread = threads__findnew(event->comm.pid); | |
951 | ||
952 | dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n", | |
953 | (void *)(offset + head), | |
954 | (void *)(long)(event->header.size), | |
955 | event->comm.comm, event->comm.pid); | |
956 | ||
957 | if (thread == NULL || | |
958 | thread__set_comm(thread, event->comm.comm)) { | |
959 | dprintf("problem processing PERF_EVENT_COMM, skipping event.\n"); | |
960 | return -1; | |
8fa66bdc | 961 | } |
75051724 IM |
962 | total_comm++; |
963 | ||
964 | return 0; | |
965 | } | |
966 | ||
62fc4453 PZ |
967 | static int |
968 | process_fork_event(event_t *event, unsigned long offset, unsigned long head) | |
969 | { | |
970 | struct thread *thread = threads__findnew(event->fork.pid); | |
971 | struct thread *parent = threads__findnew(event->fork.ppid); | |
972 | ||
973 | dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n", | |
974 | (void *)(offset + head), | |
975 | (void *)(long)(event->header.size), | |
976 | event->fork.pid, event->fork.ppid); | |
977 | ||
978 | if (!thread || !parent || thread__fork(thread, parent)) { | |
979 | dprintf("problem processing PERF_EVENT_FORK, skipping event.\n"); | |
980 | return -1; | |
981 | } | |
982 | total_fork++; | |
983 | ||
984 | return 0; | |
985 | } | |
986 | ||
75051724 IM |
987 | static int |
988 | process_event(event_t *event, unsigned long offset, unsigned long head) | |
989 | { | |
990 | if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) | |
991 | return process_overflow_event(event, offset, head); | |
992 | ||
993 | switch (event->header.type) { | |
994 | case PERF_EVENT_MMAP: | |
995 | return process_mmap_event(event, offset, head); | |
996 | ||
997 | case PERF_EVENT_COMM: | |
998 | return process_comm_event(event, offset, head); | |
999 | ||
62fc4453 PZ |
1000 | case PERF_EVENT_FORK: |
1001 | return process_fork_event(event, offset, head); | |
1002 | ||
d11444df IM |
1003 | /* |
1004 | * We dont process them right now but they are fine: | |
1005 | */ | |
62fc4453 | 1006 | |
d11444df IM |
1007 | case PERF_EVENT_PERIOD: |
1008 | case PERF_EVENT_THROTTLE: | |
1009 | case PERF_EVENT_UNTHROTTLE: | |
1010 | return 0; | |
1011 | ||
d80d338d IM |
1012 | default: |
1013 | return -1; | |
1014 | } | |
1015 | ||
1016 | return 0; | |
1017 | } | |
1018 | ||
1019 | static int __cmd_report(void) | |
1020 | { | |
75051724 | 1021 | int ret, rc = EXIT_FAILURE; |
d80d338d IM |
1022 | unsigned long offset = 0; |
1023 | unsigned long head = 0; | |
1024 | struct stat stat; | |
d80d338d | 1025 | event_t *event; |
d80d338d | 1026 | uint32_t size; |
75051724 | 1027 | char *buf; |
d80d338d IM |
1028 | |
1029 | register_idle_thread(); | |
1030 | ||
1031 | input = open(input_name, O_RDONLY); | |
1032 | if (input < 0) { | |
1033 | perror("failed to open file"); | |
1034 | exit(-1); | |
1035 | } | |
1036 | ||
1037 | ret = fstat(input, &stat); | |
1038 | if (ret < 0) { | |
1039 | perror("failed to stat file"); | |
1040 | exit(-1); | |
1041 | } | |
1042 | ||
1043 | if (!stat.st_size) { | |
1044 | fprintf(stderr, "zero-sized file, nothing to do!\n"); | |
1045 | exit(0); | |
1046 | } | |
1047 | ||
1048 | if (load_kernel() < 0) { | |
1049 | perror("failed to load kernel symbols"); | |
1050 | return EXIT_FAILURE; | |
1051 | } | |
1052 | ||
1053 | if (!full_paths) { | |
1054 | if (getcwd(__cwd, sizeof(__cwd)) == NULL) { | |
1055 | perror("failed to get the current directory"); | |
1056 | return EXIT_FAILURE; | |
1057 | } | |
1058 | cwdlen = strlen(cwd); | |
1059 | } else { | |
1060 | cwd = NULL; | |
1061 | cwdlen = 0; | |
1062 | } | |
1063 | remap: | |
1064 | buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ, | |
1065 | MAP_SHARED, input, offset); | |
1066 | if (buf == MAP_FAILED) { | |
1067 | perror("failed to mmap file"); | |
1068 | exit(-1); | |
1069 | } | |
1070 | ||
1071 | more: | |
1072 | event = (event_t *)(buf + head); | |
1073 | ||
1074 | size = event->header.size; | |
1075 | if (!size) | |
1076 | size = 8; | |
1077 | ||
1078 | if (head + event->header.size >= page_size * mmap_window) { | |
1079 | unsigned long shift = page_size * (head / page_size); | |
1080 | int ret; | |
1081 | ||
1082 | ret = munmap(buf, page_size * mmap_window); | |
1083 | assert(ret == 0); | |
1084 | ||
1085 | offset += shift; | |
1086 | head -= shift; | |
1087 | goto remap; | |
1088 | } | |
1089 | ||
1090 | size = event->header.size; | |
1091 | ||
1092 | if (!size || process_event(event, offset, head) < 0) { | |
1093 | ||
3502973d IM |
1094 | dprintf("%p [%p]: skipping unknown header type: %d\n", |
1095 | (void *)(offset + head), | |
1096 | (void *)(long)(event->header.size), | |
1097 | event->header.type); | |
b7a16eac | 1098 | |
3e706114 | 1099 | total_unknown++; |
6142f9ec PZ |
1100 | |
1101 | /* | |
1102 | * assume we lost track of the stream, check alignment, and | |
1103 | * increment a single u64 in the hope to catch on again 'soon'. | |
1104 | */ | |
1105 | ||
1106 | if (unlikely(head & 7)) | |
1107 | head &= ~7ULL; | |
1108 | ||
1109 | size = 8; | |
97b07b69 | 1110 | } |
8fa66bdc | 1111 | |
6142f9ec | 1112 | head += size; |
f49515b1 | 1113 | |
8fa66bdc ACM |
1114 | if (offset + head < stat.st_size) |
1115 | goto more; | |
1116 | ||
1117 | rc = EXIT_SUCCESS; | |
8fa66bdc | 1118 | close(input); |
97b07b69 | 1119 | |
3502973d IM |
1120 | dprintf(" IP events: %10ld\n", total); |
1121 | dprintf(" mmap events: %10ld\n", total_mmap); | |
1122 | dprintf(" comm events: %10ld\n", total_comm); | |
62fc4453 | 1123 | dprintf(" fork events: %10ld\n", total_fork); |
3502973d | 1124 | dprintf(" unknown events: %10ld\n", total_unknown); |
97b07b69 | 1125 | |
3502973d | 1126 | if (dump_trace) |
97b07b69 | 1127 | return 0; |
97b07b69 | 1128 | |
e7fb08b1 | 1129 | if (verbose >= 2) |
16f762a2 | 1130 | dsos__fprintf(stdout); |
16f762a2 | 1131 | |
8229289b | 1132 | collapse__resort(); |
e7fb08b1 PZ |
1133 | output__resort(); |
1134 | output__fprintf(stdout, total); | |
8fa66bdc | 1135 | |
8fa66bdc ACM |
1136 | return rc; |
1137 | } | |
1138 | ||
53cb8bc2 IM |
1139 | static const char * const report_usage[] = { |
1140 | "perf report [<options>] <command>", | |
1141 | NULL | |
1142 | }; | |
1143 | ||
1144 | static const struct option options[] = { | |
1145 | OPT_STRING('i', "input", &input_name, "file", | |
1146 | "input file name"), | |
815e777f ACM |
1147 | OPT_BOOLEAN('v', "verbose", &verbose, |
1148 | "be more verbose (show symbol address, etc)"), | |
97b07b69 IM |
1149 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, |
1150 | "dump raw trace in ASCII"), | |
450aaa2b | 1151 | OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"), |
63299f05 IM |
1152 | OPT_STRING('s', "sort", &sort_order, "key[,key2...]", |
1153 | "sort by key(s): pid, comm, dso, symbol. Default: pid,symbol"), | |
b78c07d4 ACM |
1154 | OPT_BOOLEAN('P', "full-paths", &full_paths, |
1155 | "Don't shorten the pathnames taking into account the cwd"), | |
53cb8bc2 IM |
1156 | OPT_END() |
1157 | }; | |
1158 | ||
5352f35d IM |
1159 | static void setup_sorting(void) |
1160 | { | |
1161 | char *tmp, *tok, *str = strdup(sort_order); | |
1162 | ||
1163 | for (tok = strtok_r(str, ", ", &tmp); | |
1164 | tok; tok = strtok_r(NULL, ", ", &tmp)) { | |
1165 | if (sort_dimension__add(tok) < 0) { | |
1166 | error("Unknown --sort key: `%s'", tok); | |
1167 | usage_with_options(report_usage, options); | |
1168 | } | |
1169 | } | |
1170 | ||
1171 | free(str); | |
1172 | } | |
1173 | ||
53cb8bc2 IM |
1174 | int cmd_report(int argc, const char **argv, const char *prefix) |
1175 | { | |
a2928c42 | 1176 | symbol__init(); |
53cb8bc2 IM |
1177 | |
1178 | page_size = getpagesize(); | |
1179 | ||
edc52dea | 1180 | argc = parse_options(argc, argv, options, report_usage, 0); |
53cb8bc2 | 1181 | |
1aa16738 PZ |
1182 | setup_sorting(); |
1183 | ||
edc52dea IM |
1184 | /* |
1185 | * Any (unrecognized) arguments left? | |
1186 | */ | |
1187 | if (argc) | |
1188 | usage_with_options(report_usage, options); | |
1189 | ||
a930d2c0 IM |
1190 | setup_pager(); |
1191 | ||
53cb8bc2 IM |
1192 | return __cmd_report(); |
1193 | } |