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