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