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