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