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