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