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