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