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