]>
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; | |
0a520c63 IM |
214 | self->comm = malloc(30); |
215 | if (self->comm) | |
216 | sprintf(self->comm, ":%d", 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 | { | |
225 | self->comm = strdup(comm); | |
226 | return self->comm ? 0 : -ENOMEM; | |
227 | } | |
228 | ||
16f762a2 | 229 | static struct rb_root threads; |
8fa66bdc | 230 | |
ce7e4365 | 231 | static struct thread *threads__findnew(pid_t pid) |
8fa66bdc | 232 | { |
ce7e4365 ACM |
233 | struct rb_node **p = &threads.rb_node; |
234 | struct rb_node *parent = NULL; | |
235 | struct thread *th; | |
8fa66bdc | 236 | |
ce7e4365 ACM |
237 | while (*p != NULL) { |
238 | parent = *p; | |
239 | th = rb_entry(parent, struct thread, rb_node); | |
8fa66bdc | 240 | |
ce7e4365 ACM |
241 | if (th->pid == pid) |
242 | return th; | |
8fa66bdc | 243 | |
ce7e4365 ACM |
244 | if (pid < th->pid) |
245 | p = &(*p)->rb_left; | |
246 | else | |
247 | p = &(*p)->rb_right; | |
8fa66bdc ACM |
248 | } |
249 | ||
ce7e4365 ACM |
250 | th = thread__new(pid); |
251 | if (th != NULL) { | |
252 | rb_link_node(&th->rb_node, parent, p); | |
253 | rb_insert_color(&th->rb_node, &threads); | |
254 | } | |
255 | return th; | |
8fa66bdc ACM |
256 | } |
257 | ||
258 | static void thread__insert_map(struct thread *self, struct map *map) | |
259 | { | |
260 | list_add_tail(&map->node, &self->maps); | |
261 | } | |
262 | ||
263 | static struct map *thread__find_map(struct thread *self, uint64_t ip) | |
264 | { | |
16f762a2 IM |
265 | struct map *pos; |
266 | ||
8fa66bdc ACM |
267 | if (self == NULL) |
268 | return NULL; | |
269 | ||
8fa66bdc ACM |
270 | list_for_each_entry(pos, &self->maps, node) |
271 | if (ip >= pos->start && ip <= pos->end) | |
272 | return pos; | |
273 | ||
274 | return NULL; | |
275 | } | |
276 | ||
e7fb08b1 PZ |
277 | /* |
278 | * histogram, sorted on item, collects counts | |
279 | */ | |
280 | ||
281 | static struct rb_root hist; | |
282 | ||
283 | struct hist_entry { | |
284 | struct rb_node rb_node; | |
285 | ||
286 | struct thread *thread; | |
287 | struct map *map; | |
288 | struct dso *dso; | |
289 | struct symbol *sym; | |
290 | uint64_t ip; | |
291 | char level; | |
292 | ||
293 | uint32_t count; | |
294 | }; | |
295 | ||
1aa16738 PZ |
296 | /* |
297 | * configurable sorting bits | |
298 | */ | |
299 | ||
300 | struct sort_entry { | |
301 | struct list_head list; | |
302 | ||
ca8cdeef PZ |
303 | char *header; |
304 | ||
1aa16738 PZ |
305 | int64_t (*cmp)(struct hist_entry *, struct hist_entry *); |
306 | size_t (*print)(FILE *fp, struct hist_entry *); | |
307 | }; | |
308 | ||
e7fb08b1 | 309 | static int64_t |
1aa16738 | 310 | sort__thread_cmp(struct hist_entry *left, struct hist_entry *right) |
e7fb08b1 | 311 | { |
1aa16738 PZ |
312 | return right->thread->pid - left->thread->pid; |
313 | } | |
314 | ||
315 | static size_t | |
316 | sort__thread_print(FILE *fp, struct hist_entry *self) | |
317 | { | |
cf25c63c | 318 | return fprintf(fp, " %16s:%5d", self->thread->comm ?: "", self->thread->pid); |
1aa16738 | 319 | } |
e7fb08b1 | 320 | |
1aa16738 | 321 | static struct sort_entry sort_thread = { |
cf25c63c | 322 | .header = " Command: Pid ", |
1aa16738 PZ |
323 | .cmp = sort__thread_cmp, |
324 | .print = sort__thread_print, | |
325 | }; | |
326 | ||
992444b1 PZ |
327 | static int64_t |
328 | sort__comm_cmp(struct hist_entry *left, struct hist_entry *right) | |
329 | { | |
330 | char *comm_l = left->thread->comm; | |
331 | char *comm_r = right->thread->comm; | |
332 | ||
333 | if (!comm_l || !comm_r) { | |
334 | if (!comm_l && !comm_r) | |
335 | return 0; | |
336 | else if (!comm_l) | |
337 | return -1; | |
338 | else | |
339 | return 1; | |
340 | } | |
341 | ||
342 | return strcmp(comm_l, comm_r); | |
343 | } | |
344 | ||
345 | static size_t | |
346 | sort__comm_print(FILE *fp, struct hist_entry *self) | |
347 | { | |
0a520c63 | 348 | return fprintf(fp, " %16s", self->thread->comm); |
992444b1 PZ |
349 | } |
350 | ||
351 | static struct sort_entry sort_comm = { | |
4593bba8 | 352 | .header = " Command", |
992444b1 PZ |
353 | .cmp = sort__comm_cmp, |
354 | .print = sort__comm_print, | |
355 | }; | |
356 | ||
55e5ec41 PZ |
357 | static int64_t |
358 | sort__dso_cmp(struct hist_entry *left, struct hist_entry *right) | |
359 | { | |
360 | struct dso *dso_l = left->dso; | |
361 | struct dso *dso_r = right->dso; | |
362 | ||
363 | if (!dso_l || !dso_r) { | |
364 | if (!dso_l && !dso_r) | |
365 | return 0; | |
366 | else if (!dso_l) | |
367 | return -1; | |
368 | else | |
369 | return 1; | |
370 | } | |
371 | ||
372 | return strcmp(dso_l->name, dso_r->name); | |
373 | } | |
374 | ||
375 | static size_t | |
376 | sort__dso_print(FILE *fp, struct hist_entry *self) | |
377 | { | |
0a520c63 IM |
378 | if (self->dso) |
379 | return fprintf(fp, " %-25s", self->dso->name); | |
380 | ||
381 | return fprintf(fp, " %016llx", (__u64)self->ip); | |
55e5ec41 PZ |
382 | } |
383 | ||
384 | static struct sort_entry sort_dso = { | |
cf25c63c | 385 | .header = " Shared Object ", |
55e5ec41 PZ |
386 | .cmp = sort__dso_cmp, |
387 | .print = sort__dso_print, | |
388 | }; | |
389 | ||
1aa16738 PZ |
390 | static int64_t |
391 | sort__sym_cmp(struct hist_entry *left, struct hist_entry *right) | |
392 | { | |
393 | uint64_t ip_l, ip_r; | |
e7fb08b1 PZ |
394 | |
395 | if (left->sym == right->sym) | |
396 | return 0; | |
397 | ||
398 | ip_l = left->sym ? left->sym->start : left->ip; | |
399 | ip_r = right->sym ? right->sym->start : right->ip; | |
400 | ||
401 | return (int64_t)(ip_r - ip_l); | |
402 | } | |
403 | ||
1aa16738 PZ |
404 | static size_t |
405 | sort__sym_print(FILE *fp, struct hist_entry *self) | |
406 | { | |
407 | size_t ret = 0; | |
408 | ||
1aa16738 | 409 | if (verbose) |
0a520c63 IM |
410 | ret += fprintf(fp, " %#018llx", (__u64)self->ip); |
411 | ||
412 | if (self->dso) | |
413 | ret += fprintf(fp, " %s: ", self->dso->name); | |
414 | else | |
415 | ret += fprintf(fp, " %#016llx: ", (__u64)self->ip); | |
1aa16738 | 416 | |
0a520c63 IM |
417 | if (self->sym) |
418 | ret += fprintf(fp, "%s", self->sym->name); | |
419 | else | |
420 | ret += fprintf(fp, "%#016llx", (__u64)self->ip); | |
1aa16738 PZ |
421 | |
422 | return ret; | |
423 | } | |
424 | ||
425 | static struct sort_entry sort_sym = { | |
4593bba8 | 426 | .header = " Shared Object: Symbol", |
ca8cdeef PZ |
427 | .cmp = sort__sym_cmp, |
428 | .print = sort__sym_print, | |
1aa16738 PZ |
429 | }; |
430 | ||
37f440cb PZ |
431 | struct sort_dimension { |
432 | char *name; | |
433 | struct sort_entry *entry; | |
434 | int taken; | |
435 | }; | |
436 | ||
437 | static struct sort_dimension sort_dimensions[] = { | |
438 | { .name = "pid", .entry = &sort_thread, }, | |
992444b1 | 439 | { .name = "comm", .entry = &sort_comm, }, |
55e5ec41 | 440 | { .name = "dso", .entry = &sort_dso, }, |
37f440cb PZ |
441 | { .name = "symbol", .entry = &sort_sym, }, |
442 | }; | |
443 | ||
1aa16738 PZ |
444 | static LIST_HEAD(hist_entry__sort_list); |
445 | ||
37f440cb PZ |
446 | static int sort_dimension__add(char *tok) |
447 | { | |
448 | int i; | |
449 | ||
450 | for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) { | |
451 | struct sort_dimension *sd = &sort_dimensions[i]; | |
452 | ||
453 | if (sd->taken) | |
454 | continue; | |
455 | ||
456 | if (strcmp(tok, sd->name)) | |
457 | continue; | |
458 | ||
459 | list_add_tail(&sd->entry->list, &hist_entry__sort_list); | |
460 | sd->taken = 1; | |
461 | return 0; | |
462 | } | |
463 | ||
464 | return -ESRCH; | |
465 | } | |
466 | ||
1aa16738 PZ |
467 | static void setup_sorting(void) |
468 | { | |
37f440cb PZ |
469 | char *tmp, *tok, *str = strdup(sort_order); |
470 | ||
471 | for (tok = strtok_r(str, ", ", &tmp); | |
472 | tok; tok = strtok_r(NULL, ", ", &tmp)) | |
473 | sort_dimension__add(tok); | |
474 | ||
475 | free(str); | |
1aa16738 PZ |
476 | } |
477 | ||
478 | static int64_t | |
479 | hist_entry__cmp(struct hist_entry *left, struct hist_entry *right) | |
480 | { | |
481 | struct sort_entry *se; | |
482 | int64_t cmp = 0; | |
483 | ||
484 | list_for_each_entry(se, &hist_entry__sort_list, list) { | |
485 | cmp = se->cmp(left, right); | |
486 | if (cmp) | |
487 | break; | |
488 | } | |
489 | ||
490 | return cmp; | |
491 | } | |
492 | ||
493 | static size_t | |
494 | hist_entry__fprintf(FILE *fp, struct hist_entry *self, uint64_t total_samples) | |
495 | { | |
496 | struct sort_entry *se; | |
497 | size_t ret; | |
498 | ||
499 | if (total_samples) { | |
2d65537e | 500 | ret = fprintf(fp, " %5.2f%%", |
1aa16738 PZ |
501 | (self->count * 100.0) / total_samples); |
502 | } else | |
503 | ret = fprintf(fp, "%12d ", self->count); | |
504 | ||
505 | list_for_each_entry(se, &hist_entry__sort_list, list) | |
506 | ret += se->print(fp, self); | |
507 | ||
508 | ret += fprintf(fp, "\n"); | |
509 | ||
510 | return ret; | |
511 | } | |
512 | ||
513 | /* | |
514 | * collect histogram counts | |
515 | */ | |
516 | ||
e7fb08b1 PZ |
517 | static int |
518 | hist_entry__add(struct thread *thread, struct map *map, struct dso *dso, | |
519 | struct symbol *sym, uint64_t ip, char level) | |
8fa66bdc | 520 | { |
e7fb08b1 PZ |
521 | struct rb_node **p = &hist.rb_node; |
522 | struct rb_node *parent = NULL; | |
523 | struct hist_entry *he; | |
524 | struct hist_entry entry = { | |
525 | .thread = thread, | |
526 | .map = map, | |
527 | .dso = dso, | |
528 | .sym = sym, | |
529 | .ip = ip, | |
530 | .level = level, | |
531 | .count = 1, | |
532 | }; | |
533 | int cmp; | |
534 | ||
535 | while (*p != NULL) { | |
536 | parent = *p; | |
537 | he = rb_entry(parent, struct hist_entry, rb_node); | |
538 | ||
539 | cmp = hist_entry__cmp(&entry, he); | |
540 | ||
541 | if (!cmp) { | |
542 | he->count++; | |
543 | return 0; | |
544 | } | |
545 | ||
546 | if (cmp < 0) | |
547 | p = &(*p)->rb_left; | |
548 | else | |
549 | p = &(*p)->rb_right; | |
ce7e4365 | 550 | } |
e7fb08b1 PZ |
551 | |
552 | he = malloc(sizeof(*he)); | |
553 | if (!he) | |
554 | return -ENOMEM; | |
555 | *he = entry; | |
556 | rb_link_node(&he->rb_node, parent, p); | |
557 | rb_insert_color(&he->rb_node, &hist); | |
558 | ||
559 | return 0; | |
8fa66bdc ACM |
560 | } |
561 | ||
e7fb08b1 PZ |
562 | /* |
563 | * reverse the map, sort on count. | |
564 | */ | |
565 | ||
566 | static struct rb_root output_hists; | |
567 | ||
568 | static void output__insert_entry(struct hist_entry *he) | |
3a4b8cc7 | 569 | { |
e7fb08b1 | 570 | struct rb_node **p = &output_hists.rb_node; |
3a4b8cc7 | 571 | struct rb_node *parent = NULL; |
e7fb08b1 | 572 | struct hist_entry *iter; |
3a4b8cc7 ACM |
573 | |
574 | while (*p != NULL) { | |
575 | parent = *p; | |
e7fb08b1 | 576 | iter = rb_entry(parent, struct hist_entry, rb_node); |
3a4b8cc7 | 577 | |
e7fb08b1 | 578 | if (he->count > iter->count) |
3a4b8cc7 ACM |
579 | p = &(*p)->rb_left; |
580 | else | |
581 | p = &(*p)->rb_right; | |
582 | } | |
583 | ||
e7fb08b1 PZ |
584 | rb_link_node(&he->rb_node, parent, p); |
585 | rb_insert_color(&he->rb_node, &output_hists); | |
3a4b8cc7 ACM |
586 | } |
587 | ||
e7fb08b1 | 588 | static void output__resort(void) |
3a4b8cc7 | 589 | { |
e7fb08b1 PZ |
590 | struct rb_node *next = rb_first(&hist); |
591 | struct hist_entry *n; | |
3a4b8cc7 | 592 | |
e7fb08b1 PZ |
593 | while (next) { |
594 | n = rb_entry(next, struct hist_entry, rb_node); | |
595 | next = rb_next(&n->rb_node); | |
3a4b8cc7 | 596 | |
e7fb08b1 PZ |
597 | rb_erase(&n->rb_node, &hist); |
598 | output__insert_entry(n); | |
3a4b8cc7 ACM |
599 | } |
600 | } | |
601 | ||
e7fb08b1 | 602 | static size_t output__fprintf(FILE *fp, uint64_t total_samples) |
3a4b8cc7 | 603 | { |
e7fb08b1 | 604 | struct hist_entry *pos; |
2d65537e | 605 | struct sort_entry *se; |
3a4b8cc7 ACM |
606 | struct rb_node *nd; |
607 | size_t ret = 0; | |
608 | ||
ca8cdeef PZ |
609 | fprintf(fp, "#\n"); |
610 | ||
611 | fprintf(fp, "# Overhead"); | |
612 | list_for_each_entry(se, &hist_entry__sort_list, list) | |
613 | fprintf(fp, " %s", se->header); | |
614 | fprintf(fp, "\n"); | |
615 | ||
616 | fprintf(fp, "# ........"); | |
2d65537e | 617 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
ca8cdeef PZ |
618 | int i; |
619 | ||
4593bba8 IM |
620 | fprintf(fp, " "); |
621 | for (i = 0; i < strlen(se->header)-1; i++) | |
ca8cdeef | 622 | fprintf(fp, "."); |
2d65537e | 623 | } |
ca8cdeef PZ |
624 | fprintf(fp, "\n"); |
625 | ||
626 | fprintf(fp, "#\n"); | |
2d65537e | 627 | |
e7fb08b1 PZ |
628 | for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) { |
629 | pos = rb_entry(nd, struct hist_entry, rb_node); | |
630 | ret += hist_entry__fprintf(fp, pos, total_samples); | |
3a4b8cc7 ACM |
631 | } |
632 | ||
633 | return ret; | |
634 | } | |
635 | ||
436224a6 PZ |
636 | static void register_idle_thread(void) |
637 | { | |
638 | struct thread *thread = threads__findnew(0); | |
639 | ||
640 | if (thread == NULL || | |
641 | thread__set_comm(thread, "[idle]")) { | |
642 | fprintf(stderr, "problem inserting idle task.\n"); | |
643 | exit(-1); | |
644 | } | |
645 | } | |
646 | ||
e7fb08b1 | 647 | |
53cb8bc2 | 648 | static int __cmd_report(void) |
8fa66bdc ACM |
649 | { |
650 | unsigned long offset = 0; | |
651 | unsigned long head = 0; | |
652 | struct stat stat; | |
653 | char *buf; | |
654 | event_t *event; | |
655 | int ret, rc = EXIT_FAILURE; | |
6142f9ec | 656 | uint32_t size; |
f49515b1 | 657 | unsigned long total = 0, total_mmap = 0, total_comm = 0, total_unknown = 0; |
b78c07d4 ACM |
658 | char cwd[PATH_MAX], *cwdp = cwd; |
659 | int cwdlen; | |
8fa66bdc | 660 | |
436224a6 PZ |
661 | register_idle_thread(); |
662 | ||
8fa66bdc ACM |
663 | input = open(input_name, O_RDONLY); |
664 | if (input < 0) { | |
665 | perror("failed to open file"); | |
666 | exit(-1); | |
667 | } | |
668 | ||
669 | ret = fstat(input, &stat); | |
670 | if (ret < 0) { | |
671 | perror("failed to stat file"); | |
672 | exit(-1); | |
673 | } | |
674 | ||
675 | if (!stat.st_size) { | |
676 | fprintf(stderr, "zero-sized file, nothing to do!\n"); | |
677 | exit(0); | |
678 | } | |
679 | ||
450aaa2b | 680 | if (load_kernel() < 0) { |
a2928c42 | 681 | perror("failed to load kernel symbols"); |
8fa66bdc ACM |
682 | return EXIT_FAILURE; |
683 | } | |
684 | ||
b78c07d4 ACM |
685 | if (!full_paths) { |
686 | if (getcwd(cwd, sizeof(cwd)) == NULL) { | |
687 | perror("failed to get the current directory"); | |
688 | return EXIT_FAILURE; | |
689 | } | |
690 | cwdlen = strlen(cwd); | |
10a28255 | 691 | } else { |
b78c07d4 | 692 | cwdp = NULL; |
10a28255 MG |
693 | cwdlen = 0; |
694 | } | |
8fa66bdc ACM |
695 | remap: |
696 | buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ, | |
697 | MAP_SHARED, input, offset); | |
698 | if (buf == MAP_FAILED) { | |
699 | perror("failed to mmap file"); | |
700 | exit(-1); | |
701 | } | |
702 | ||
703 | more: | |
704 | event = (event_t *)(buf + head); | |
705 | ||
6142f9ec PZ |
706 | size = event->header.size; |
707 | if (!size) | |
708 | size = 8; | |
709 | ||
8fa66bdc ACM |
710 | if (head + event->header.size >= page_size * mmap_window) { |
711 | unsigned long shift = page_size * (head / page_size); | |
712 | int ret; | |
713 | ||
714 | ret = munmap(buf, page_size * mmap_window); | |
715 | assert(ret == 0); | |
716 | ||
717 | offset += shift; | |
718 | head -= shift; | |
719 | goto remap; | |
720 | } | |
721 | ||
6142f9ec PZ |
722 | size = event->header.size; |
723 | if (!size) | |
724 | goto broken_event; | |
8fa66bdc | 725 | |
8fa66bdc ACM |
726 | if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) { |
727 | char level; | |
728 | int show = 0; | |
729 | struct dso *dso = NULL; | |
730 | struct thread *thread = threads__findnew(event->ip.pid); | |
f17e04af | 731 | uint64_t ip = event->ip.ip; |
e7fb08b1 | 732 | struct map *map = NULL; |
8fa66bdc | 733 | |
3502973d IM |
734 | dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n", |
735 | (void *)(offset + head), | |
736 | (void *)(long)(event->header.size), | |
737 | event->header.misc, | |
738 | event->ip.pid, | |
739 | (void *)(long)ip); | |
97b07b69 | 740 | |
ce7e4365 | 741 | if (thread == NULL) { |
55717314 | 742 | fprintf(stderr, "problem processing %d event, skipping it.\n", |
ce7e4365 | 743 | event->header.type); |
55717314 | 744 | goto broken_event; |
ce7e4365 | 745 | } |
8fa66bdc ACM |
746 | |
747 | if (event->header.misc & PERF_EVENT_MISC_KERNEL) { | |
748 | show = SHOW_KERNEL; | |
749 | level = 'k'; | |
e7fb08b1 | 750 | |
8fa66bdc | 751 | dso = kernel_dso; |
e7fb08b1 | 752 | |
8fa66bdc | 753 | } else if (event->header.misc & PERF_EVENT_MISC_USER) { |
16f762a2 | 754 | |
8fa66bdc ACM |
755 | show = SHOW_USER; |
756 | level = '.'; | |
16f762a2 IM |
757 | |
758 | map = thread__find_map(thread, ip); | |
f17e04af | 759 | if (map != NULL) { |
8fa66bdc | 760 | dso = map->dso; |
f17e04af PZ |
761 | ip -= map->start + map->pgoff; |
762 | } | |
e7fb08b1 | 763 | |
8fa66bdc ACM |
764 | } else { |
765 | show = SHOW_HV; | |
766 | level = 'H'; | |
767 | } | |
768 | ||
769 | if (show & show_mask) { | |
f17e04af | 770 | struct symbol *sym = dso__find_symbol(dso, ip); |
8fa66bdc | 771 | |
e7fb08b1 PZ |
772 | if (hist_entry__add(thread, map, dso, sym, ip, level)) { |
773 | fprintf(stderr, | |
55717314 IM |
774 | "problem incrementing symbol count, skipping event\n"); |
775 | goto broken_event; | |
ce7e4365 | 776 | } |
8fa66bdc ACM |
777 | } |
778 | total++; | |
779 | } else switch (event->header.type) { | |
780 | case PERF_EVENT_MMAP: { | |
781 | struct thread *thread = threads__findnew(event->mmap.pid); | |
b78c07d4 | 782 | struct map *map = map__new(&event->mmap, cwdp, cwdlen); |
8fa66bdc | 783 | |
3502973d IM |
784 | dprintf("%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n", |
785 | (void *)(offset + head), | |
786 | (void *)(long)(event->header.size), | |
787 | (void *)(long)event->mmap.start, | |
788 | (void *)(long)event->mmap.len, | |
789 | (void *)(long)event->mmap.pgoff, | |
790 | event->mmap.filename); | |
791 | ||
ce7e4365 | 792 | if (thread == NULL || map == NULL) { |
0a520c63 IM |
793 | if (verbose) |
794 | fprintf(stderr, "problem processing PERF_EVENT_MMAP, skipping event.\n"); | |
55717314 | 795 | goto broken_event; |
ce7e4365 | 796 | } |
8fa66bdc | 797 | thread__insert_map(thread, map); |
97b07b69 | 798 | total_mmap++; |
8fa66bdc ACM |
799 | break; |
800 | } | |
801 | case PERF_EVENT_COMM: { | |
802 | struct thread *thread = threads__findnew(event->comm.pid); | |
803 | ||
3502973d IM |
804 | dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n", |
805 | (void *)(offset + head), | |
806 | (void *)(long)(event->header.size), | |
807 | event->comm.comm, event->comm.pid); | |
808 | ||
8fa66bdc | 809 | if (thread == NULL || |
ce7e4365 | 810 | thread__set_comm(thread, event->comm.comm)) { |
55717314 IM |
811 | fprintf(stderr, "problem processing PERF_EVENT_COMM, skipping event.\n"); |
812 | goto broken_event; | |
ce7e4365 | 813 | } |
97b07b69 | 814 | total_comm++; |
8fa66bdc ACM |
815 | break; |
816 | } | |
97b07b69 | 817 | default: { |
6142f9ec | 818 | broken_event: |
3502973d IM |
819 | dprintf("%p [%p]: skipping unknown header type: %d\n", |
820 | (void *)(offset + head), | |
821 | (void *)(long)(event->header.size), | |
822 | event->header.type); | |
b7a16eac | 823 | |
3e706114 | 824 | total_unknown++; |
6142f9ec PZ |
825 | |
826 | /* | |
827 | * assume we lost track of the stream, check alignment, and | |
828 | * increment a single u64 in the hope to catch on again 'soon'. | |
829 | */ | |
830 | ||
831 | if (unlikely(head & 7)) | |
832 | head &= ~7ULL; | |
833 | ||
834 | size = 8; | |
97b07b69 | 835 | } |
8fa66bdc ACM |
836 | } |
837 | ||
6142f9ec | 838 | head += size; |
f49515b1 | 839 | |
8fa66bdc ACM |
840 | if (offset + head < stat.st_size) |
841 | goto more; | |
842 | ||
843 | rc = EXIT_SUCCESS; | |
8fa66bdc | 844 | close(input); |
97b07b69 | 845 | |
3502973d IM |
846 | dprintf(" IP events: %10ld\n", total); |
847 | dprintf(" mmap events: %10ld\n", total_mmap); | |
848 | dprintf(" comm events: %10ld\n", total_comm); | |
849 | dprintf(" unknown events: %10ld\n", total_unknown); | |
97b07b69 | 850 | |
3502973d | 851 | if (dump_trace) |
97b07b69 | 852 | return 0; |
97b07b69 | 853 | |
e7fb08b1 | 854 | if (verbose >= 2) |
16f762a2 | 855 | dsos__fprintf(stdout); |
16f762a2 | 856 | |
e7fb08b1 PZ |
857 | output__resort(); |
858 | output__fprintf(stdout, total); | |
8fa66bdc | 859 | |
8fa66bdc ACM |
860 | return rc; |
861 | } | |
862 | ||
53cb8bc2 IM |
863 | static const char * const report_usage[] = { |
864 | "perf report [<options>] <command>", | |
865 | NULL | |
866 | }; | |
867 | ||
868 | static const struct option options[] = { | |
869 | OPT_STRING('i', "input", &input_name, "file", | |
870 | "input file name"), | |
815e777f ACM |
871 | OPT_BOOLEAN('v', "verbose", &verbose, |
872 | "be more verbose (show symbol address, etc)"), | |
97b07b69 IM |
873 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, |
874 | "dump raw trace in ASCII"), | |
450aaa2b | 875 | OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"), |
63299f05 IM |
876 | OPT_STRING('s', "sort", &sort_order, "key[,key2...]", |
877 | "sort by key(s): pid, comm, dso, symbol. Default: pid,symbol"), | |
b78c07d4 ACM |
878 | OPT_BOOLEAN('P', "full-paths", &full_paths, |
879 | "Don't shorten the pathnames taking into account the cwd"), | |
53cb8bc2 IM |
880 | OPT_END() |
881 | }; | |
882 | ||
883 | int cmd_report(int argc, const char **argv, const char *prefix) | |
884 | { | |
a2928c42 | 885 | symbol__init(); |
53cb8bc2 IM |
886 | |
887 | page_size = getpagesize(); | |
888 | ||
889 | parse_options(argc, argv, options, report_usage, 0); | |
890 | ||
1aa16738 PZ |
891 | setup_sorting(); |
892 | ||
a930d2c0 IM |
893 | setup_pager(); |
894 | ||
53cb8bc2 IM |
895 | return __cmd_report(); |
896 | } |