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