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