Commit | Line | Data |
---|---|---|
53cb8bc2 | 1 | #include "util/util.h" |
16f762a2 | 2 | #include "builtin.h" |
53cb8bc2 | 3 | |
35a50c8a | 4 | #include "util/list.h" |
a930d2c0 | 5 | #include "util/cache.h" |
35a50c8a | 6 | #include "util/rbtree.h" |
a2928c42 | 7 | #include "util/symbol.h" |
a0055ae2 | 8 | #include "util/string.h" |
8fa66bdc | 9 | |
53cb8bc2 IM |
10 | #include "perf.h" |
11 | ||
12 | #include "util/parse-options.h" | |
13 | #include "util/parse-events.h" | |
14 | ||
8fa66bdc ACM |
15 | #define SHOW_KERNEL 1 |
16 | #define SHOW_USER 2 | |
17 | #define SHOW_HV 4 | |
18 | ||
23ac9cbe | 19 | static char const *input_name = "perf.data"; |
450aaa2b | 20 | static char *vmlinux = NULL; |
f70e87d7 | 21 | static char *sort_order = "comm,dso"; |
8fa66bdc ACM |
22 | static int input; |
23 | static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV; | |
24 | ||
97b07b69 | 25 | static int dump_trace = 0; |
16f762a2 | 26 | static int verbose; |
b78c07d4 | 27 | static int full_paths; |
97b07b69 | 28 | |
8fa66bdc ACM |
29 | static unsigned long page_size; |
30 | static unsigned long mmap_window = 32; | |
31 | ||
53cb8bc2 | 32 | const char *perf_event_names[] = { |
8fa66bdc ACM |
33 | [PERF_EVENT_MMAP] = " PERF_EVENT_MMAP", |
34 | [PERF_EVENT_MUNMAP] = " PERF_EVENT_MUNMAP", | |
35 | [PERF_EVENT_COMM] = " PERF_EVENT_COMM", | |
36 | }; | |
37 | ||
38 | struct ip_event { | |
39 | struct perf_event_header header; | |
40 | __u64 ip; | |
41 | __u32 pid, tid; | |
42 | }; | |
43 | struct mmap_event { | |
44 | struct perf_event_header header; | |
45 | __u32 pid, tid; | |
46 | __u64 start; | |
47 | __u64 len; | |
48 | __u64 pgoff; | |
49 | char filename[PATH_MAX]; | |
50 | }; | |
51 | struct comm_event { | |
52 | struct perf_event_header header; | |
53 | __u32 pid,tid; | |
54 | char comm[16]; | |
55 | }; | |
56 | ||
57 | typedef union event_union { | |
58 | struct perf_event_header header; | |
59 | struct ip_event ip; | |
60 | struct mmap_event mmap; | |
61 | struct comm_event comm; | |
62 | } event_t; | |
63 | ||
8fa66bdc ACM |
64 | static LIST_HEAD(dsos); |
65 | static struct dso *kernel_dso; | |
66 | ||
67 | static void dsos__add(struct dso *dso) | |
68 | { | |
69 | list_add_tail(&dso->node, &dsos); | |
70 | } | |
71 | ||
72 | static struct dso *dsos__find(const char *name) | |
73 | { | |
74 | struct dso *pos; | |
75 | ||
76 | list_for_each_entry(pos, &dsos, node) | |
77 | if (strcmp(pos->name, name) == 0) | |
78 | return pos; | |
79 | return NULL; | |
80 | } | |
81 | ||
82 | static struct dso *dsos__findnew(const char *name) | |
83 | { | |
84 | struct dso *dso = dsos__find(name); | |
b7a16eac | 85 | int nr; |
8fa66bdc | 86 | |
4593bba8 IM |
87 | if (dso) |
88 | return dso; | |
89 | ||
90 | dso = dso__new(name, 0); | |
91 | if (!dso) | |
92 | goto out_delete_dso; | |
8fa66bdc | 93 | |
4593bba8 IM |
94 | nr = dso__load(dso, NULL); |
95 | if (nr < 0) { | |
96 | fprintf(stderr, "Failed to open: %s\n", name); | |
97 | goto out_delete_dso; | |
8fa66bdc | 98 | } |
4593bba8 IM |
99 | if (!nr && verbose) { |
100 | fprintf(stderr, | |
101 | "No symbols found in: %s, maybe install a debug package?\n", | |
102 | name); | |
103 | } | |
104 | ||
105 | dsos__add(dso); | |
8fa66bdc ACM |
106 | |
107 | return dso; | |
108 | ||
109 | out_delete_dso: | |
110 | dso__delete(dso); | |
111 | return NULL; | |
112 | } | |
113 | ||
16f762a2 | 114 | static void dsos__fprintf(FILE *fp) |
8fa66bdc ACM |
115 | { |
116 | struct dso *pos; | |
117 | ||
118 | list_for_each_entry(pos, &dsos, node) | |
119 | dso__fprintf(pos, fp); | |
120 | } | |
121 | ||
450aaa2b PZ |
122 | static int load_kernel(void) |
123 | { | |
a827c875 | 124 | int err; |
450aaa2b | 125 | |
0085c954 | 126 | kernel_dso = dso__new("[kernel]", 0); |
450aaa2b | 127 | if (!kernel_dso) |
a2928c42 | 128 | return -1; |
450aaa2b | 129 | |
69ee69f6 | 130 | err = dso__load_kernel(kernel_dso, vmlinux, NULL); |
a2928c42 ACM |
131 | if (err) { |
132 | dso__delete(kernel_dso); | |
133 | kernel_dso = NULL; | |
134 | } else | |
135 | dsos__add(kernel_dso); | |
450aaa2b | 136 | |
a2928c42 | 137 | return err; |
450aaa2b PZ |
138 | } |
139 | ||
b78c07d4 ACM |
140 | static int strcommon(const char *pathname, const char *cwd, int cwdlen) |
141 | { | |
142 | int n = 0; | |
143 | ||
144 | while (pathname[n] == cwd[n] && n < cwdlen) | |
145 | ++n; | |
146 | ||
147 | return n; | |
148 | } | |
149 | ||
8fa66bdc ACM |
150 | struct map { |
151 | struct list_head node; | |
152 | uint64_t start; | |
153 | uint64_t end; | |
154 | uint64_t pgoff; | |
155 | struct dso *dso; | |
156 | }; | |
157 | ||
b78c07d4 | 158 | static struct map *map__new(struct mmap_event *event, char *cwd, int cwdlen) |
8fa66bdc ACM |
159 | { |
160 | struct map *self = malloc(sizeof(*self)); | |
161 | ||
162 | if (self != NULL) { | |
b78c07d4 ACM |
163 | const char *filename = event->filename; |
164 | char newfilename[PATH_MAX]; | |
165 | ||
166 | if (cwd) { | |
167 | int n = strcommon(filename, cwd, cwdlen); | |
168 | if (n == cwdlen) { | |
169 | snprintf(newfilename, sizeof(newfilename), | |
170 | ".%s", filename + n); | |
171 | filename = newfilename; | |
172 | } | |
173 | } | |
174 | ||
8fa66bdc ACM |
175 | self->start = event->start; |
176 | self->end = event->start + event->len; | |
177 | self->pgoff = event->pgoff; | |
178 | ||
b78c07d4 | 179 | self->dso = dsos__findnew(filename); |
8fa66bdc ACM |
180 | if (self->dso == NULL) |
181 | goto out_delete; | |
182 | } | |
183 | return self; | |
184 | out_delete: | |
185 | free(self); | |
186 | return NULL; | |
187 | } | |
188 | ||
3a4b8cc7 ACM |
189 | struct thread; |
190 | ||
8fa66bdc | 191 | struct thread { |
ce7e4365 | 192 | struct rb_node rb_node; |
8fa66bdc | 193 | struct list_head maps; |
8fa66bdc ACM |
194 | pid_t pid; |
195 | char *comm; | |
196 | }; | |
197 | ||
198 | static struct thread *thread__new(pid_t pid) | |
199 | { | |
200 | struct thread *self = malloc(sizeof(*self)); | |
201 | ||
202 | if (self != NULL) { | |
203 | self->pid = pid; | |
204 | self->comm = NULL; | |
205 | INIT_LIST_HEAD(&self->maps); | |
8fa66bdc ACM |
206 | } |
207 | ||
208 | return self; | |
209 | } | |
210 | ||
8fa66bdc ACM |
211 | static int thread__set_comm(struct thread *self, const char *comm) |
212 | { | |
213 | self->comm = strdup(comm); | |
214 | return self->comm ? 0 : -ENOMEM; | |
215 | } | |
216 | ||
16f762a2 | 217 | static struct rb_root threads; |
8fa66bdc | 218 | |
ce7e4365 | 219 | static struct thread *threads__findnew(pid_t pid) |
8fa66bdc | 220 | { |
ce7e4365 ACM |
221 | struct rb_node **p = &threads.rb_node; |
222 | struct rb_node *parent = NULL; | |
223 | struct thread *th; | |
8fa66bdc | 224 | |
ce7e4365 ACM |
225 | while (*p != NULL) { |
226 | parent = *p; | |
227 | th = rb_entry(parent, struct thread, rb_node); | |
8fa66bdc | 228 | |
ce7e4365 ACM |
229 | if (th->pid == pid) |
230 | return th; | |
8fa66bdc | 231 | |
ce7e4365 ACM |
232 | if (pid < th->pid) |
233 | p = &(*p)->rb_left; | |
234 | else | |
235 | p = &(*p)->rb_right; | |
8fa66bdc ACM |
236 | } |
237 | ||
ce7e4365 ACM |
238 | th = thread__new(pid); |
239 | if (th != NULL) { | |
240 | rb_link_node(&th->rb_node, parent, p); | |
241 | rb_insert_color(&th->rb_node, &threads); | |
242 | } | |
243 | return th; | |
8fa66bdc ACM |
244 | } |
245 | ||
246 | static void thread__insert_map(struct thread *self, struct map *map) | |
247 | { | |
248 | list_add_tail(&map->node, &self->maps); | |
249 | } | |
250 | ||
251 | static struct map *thread__find_map(struct thread *self, uint64_t ip) | |
252 | { | |
16f762a2 IM |
253 | struct map *pos; |
254 | ||
8fa66bdc ACM |
255 | if (self == NULL) |
256 | return NULL; | |
257 | ||
8fa66bdc ACM |
258 | list_for_each_entry(pos, &self->maps, node) |
259 | if (ip >= pos->start && ip <= pos->end) | |
260 | return pos; | |
261 | ||
262 | return NULL; | |
263 | } | |
264 | ||
e7fb08b1 PZ |
265 | /* |
266 | * histogram, sorted on item, collects counts | |
267 | */ | |
268 | ||
269 | static struct rb_root hist; | |
270 | ||
271 | struct hist_entry { | |
272 | struct rb_node rb_node; | |
273 | ||
274 | struct thread *thread; | |
275 | struct map *map; | |
276 | struct dso *dso; | |
277 | struct symbol *sym; | |
278 | uint64_t ip; | |
279 | char level; | |
280 | ||
281 | uint32_t count; | |
282 | }; | |
283 | ||
1aa16738 PZ |
284 | /* |
285 | * configurable sorting bits | |
286 | */ | |
287 | ||
288 | struct sort_entry { | |
289 | struct list_head list; | |
290 | ||
ca8cdeef PZ |
291 | char *header; |
292 | ||
1aa16738 PZ |
293 | int64_t (*cmp)(struct hist_entry *, struct hist_entry *); |
294 | size_t (*print)(FILE *fp, struct hist_entry *); | |
295 | }; | |
296 | ||
e7fb08b1 | 297 | static int64_t |
1aa16738 | 298 | sort__thread_cmp(struct hist_entry *left, struct hist_entry *right) |
e7fb08b1 | 299 | { |
1aa16738 PZ |
300 | return right->thread->pid - left->thread->pid; |
301 | } | |
302 | ||
303 | static size_t | |
304 | sort__thread_print(FILE *fp, struct hist_entry *self) | |
305 | { | |
4593bba8 | 306 | return fprintf(fp, " %16s:%5d", self->thread->comm ?: "", self->thread->pid); |
1aa16738 | 307 | } |
e7fb08b1 | 308 | |
1aa16738 | 309 | static struct sort_entry sort_thread = { |
4593bba8 | 310 | .header = " Command: Pid ", |
1aa16738 PZ |
311 | .cmp = sort__thread_cmp, |
312 | .print = sort__thread_print, | |
313 | }; | |
314 | ||
992444b1 PZ |
315 | static int64_t |
316 | sort__comm_cmp(struct hist_entry *left, struct hist_entry *right) | |
317 | { | |
318 | char *comm_l = left->thread->comm; | |
319 | char *comm_r = right->thread->comm; | |
320 | ||
321 | if (!comm_l || !comm_r) { | |
322 | if (!comm_l && !comm_r) | |
323 | return 0; | |
324 | else if (!comm_l) | |
325 | return -1; | |
326 | else | |
327 | return 1; | |
328 | } | |
329 | ||
330 | return strcmp(comm_l, comm_r); | |
331 | } | |
332 | ||
333 | static size_t | |
334 | sort__comm_print(FILE *fp, struct hist_entry *self) | |
335 | { | |
4593bba8 | 336 | return fprintf(fp, " %16s", self->thread->comm ?: "<unknown>"); |
992444b1 PZ |
337 | } |
338 | ||
339 | static struct sort_entry sort_comm = { | |
4593bba8 | 340 | .header = " Command", |
992444b1 PZ |
341 | .cmp = sort__comm_cmp, |
342 | .print = sort__comm_print, | |
343 | }; | |
344 | ||
55e5ec41 PZ |
345 | static int64_t |
346 | sort__dso_cmp(struct hist_entry *left, struct hist_entry *right) | |
347 | { | |
348 | struct dso *dso_l = left->dso; | |
349 | struct dso *dso_r = right->dso; | |
350 | ||
351 | if (!dso_l || !dso_r) { | |
352 | if (!dso_l && !dso_r) | |
353 | return 0; | |
354 | else if (!dso_l) | |
355 | return -1; | |
356 | else | |
357 | return 1; | |
358 | } | |
359 | ||
360 | return strcmp(dso_l->name, dso_r->name); | |
361 | } | |
362 | ||
363 | static size_t | |
364 | sort__dso_print(FILE *fp, struct hist_entry *self) | |
365 | { | |
4593bba8 | 366 | return fprintf(fp, " %s", self->dso ? self->dso->name : "<unknown>"); |
55e5ec41 PZ |
367 | } |
368 | ||
369 | static struct sort_entry sort_dso = { | |
4593bba8 | 370 | .header = " Shared Object", |
55e5ec41 PZ |
371 | .cmp = sort__dso_cmp, |
372 | .print = sort__dso_print, | |
373 | }; | |
374 | ||
1aa16738 PZ |
375 | static int64_t |
376 | sort__sym_cmp(struct hist_entry *left, struct hist_entry *right) | |
377 | { | |
378 | uint64_t ip_l, ip_r; | |
e7fb08b1 PZ |
379 | |
380 | if (left->sym == right->sym) | |
381 | return 0; | |
382 | ||
383 | ip_l = left->sym ? left->sym->start : left->ip; | |
384 | ip_r = right->sym ? right->sym->start : right->ip; | |
385 | ||
386 | return (int64_t)(ip_r - ip_l); | |
387 | } | |
388 | ||
1aa16738 PZ |
389 | static size_t |
390 | sort__sym_print(FILE *fp, struct hist_entry *self) | |
391 | { | |
392 | size_t ret = 0; | |
393 | ||
1aa16738 | 394 | if (verbose) |
4593bba8 | 395 | ret += fprintf(fp, " %#018llx", (unsigned long long)self->ip); |
1aa16738 | 396 | |
4593bba8 | 397 | ret += fprintf(fp, " %s: %s", |
ca8cdeef PZ |
398 | self->dso ? self->dso->name : "<unknown>", |
399 | self->sym ? self->sym->name : "<unknown>"); | |
1aa16738 PZ |
400 | |
401 | return ret; | |
402 | } | |
403 | ||
404 | static struct sort_entry sort_sym = { | |
4593bba8 | 405 | .header = " Shared Object: Symbol", |
ca8cdeef PZ |
406 | .cmp = sort__sym_cmp, |
407 | .print = sort__sym_print, | |
1aa16738 PZ |
408 | }; |
409 | ||
37f440cb PZ |
410 | struct sort_dimension { |
411 | char *name; | |
412 | struct sort_entry *entry; | |
413 | int taken; | |
414 | }; | |
415 | ||
416 | static struct sort_dimension sort_dimensions[] = { | |
417 | { .name = "pid", .entry = &sort_thread, }, | |
992444b1 | 418 | { .name = "comm", .entry = &sort_comm, }, |
55e5ec41 | 419 | { .name = "dso", .entry = &sort_dso, }, |
37f440cb PZ |
420 | { .name = "symbol", .entry = &sort_sym, }, |
421 | }; | |
422 | ||
1aa16738 PZ |
423 | static LIST_HEAD(hist_entry__sort_list); |
424 | ||
37f440cb PZ |
425 | static int sort_dimension__add(char *tok) |
426 | { | |
427 | int i; | |
428 | ||
429 | for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) { | |
430 | struct sort_dimension *sd = &sort_dimensions[i]; | |
431 | ||
432 | if (sd->taken) | |
433 | continue; | |
434 | ||
435 | if (strcmp(tok, sd->name)) | |
436 | continue; | |
437 | ||
438 | list_add_tail(&sd->entry->list, &hist_entry__sort_list); | |
439 | sd->taken = 1; | |
440 | return 0; | |
441 | } | |
442 | ||
443 | return -ESRCH; | |
444 | } | |
445 | ||
1aa16738 PZ |
446 | static void setup_sorting(void) |
447 | { | |
37f440cb PZ |
448 | char *tmp, *tok, *str = strdup(sort_order); |
449 | ||
450 | for (tok = strtok_r(str, ", ", &tmp); | |
451 | tok; tok = strtok_r(NULL, ", ", &tmp)) | |
452 | sort_dimension__add(tok); | |
453 | ||
454 | free(str); | |
1aa16738 PZ |
455 | } |
456 | ||
457 | static int64_t | |
458 | hist_entry__cmp(struct hist_entry *left, struct hist_entry *right) | |
459 | { | |
460 | struct sort_entry *se; | |
461 | int64_t cmp = 0; | |
462 | ||
463 | list_for_each_entry(se, &hist_entry__sort_list, list) { | |
464 | cmp = se->cmp(left, right); | |
465 | if (cmp) | |
466 | break; | |
467 | } | |
468 | ||
469 | return cmp; | |
470 | } | |
471 | ||
472 | static size_t | |
473 | hist_entry__fprintf(FILE *fp, struct hist_entry *self, uint64_t total_samples) | |
474 | { | |
475 | struct sort_entry *se; | |
476 | size_t ret; | |
477 | ||
478 | if (total_samples) { | |
2d65537e | 479 | ret = fprintf(fp, " %5.2f%%", |
1aa16738 PZ |
480 | (self->count * 100.0) / total_samples); |
481 | } else | |
482 | ret = fprintf(fp, "%12d ", self->count); | |
483 | ||
484 | list_for_each_entry(se, &hist_entry__sort_list, list) | |
485 | ret += se->print(fp, self); | |
486 | ||
487 | ret += fprintf(fp, "\n"); | |
488 | ||
489 | return ret; | |
490 | } | |
491 | ||
492 | /* | |
493 | * collect histogram counts | |
494 | */ | |
495 | ||
e7fb08b1 PZ |
496 | static int |
497 | hist_entry__add(struct thread *thread, struct map *map, struct dso *dso, | |
498 | struct symbol *sym, uint64_t ip, char level) | |
8fa66bdc | 499 | { |
e7fb08b1 PZ |
500 | struct rb_node **p = &hist.rb_node; |
501 | struct rb_node *parent = NULL; | |
502 | struct hist_entry *he; | |
503 | struct hist_entry entry = { | |
504 | .thread = thread, | |
505 | .map = map, | |
506 | .dso = dso, | |
507 | .sym = sym, | |
508 | .ip = ip, | |
509 | .level = level, | |
510 | .count = 1, | |
511 | }; | |
512 | int cmp; | |
513 | ||
514 | while (*p != NULL) { | |
515 | parent = *p; | |
516 | he = rb_entry(parent, struct hist_entry, rb_node); | |
517 | ||
518 | cmp = hist_entry__cmp(&entry, he); | |
519 | ||
520 | if (!cmp) { | |
521 | he->count++; | |
522 | return 0; | |
523 | } | |
524 | ||
525 | if (cmp < 0) | |
526 | p = &(*p)->rb_left; | |
527 | else | |
528 | p = &(*p)->rb_right; | |
ce7e4365 | 529 | } |
e7fb08b1 PZ |
530 | |
531 | he = malloc(sizeof(*he)); | |
532 | if (!he) | |
533 | return -ENOMEM; | |
534 | *he = entry; | |
535 | rb_link_node(&he->rb_node, parent, p); | |
536 | rb_insert_color(&he->rb_node, &hist); | |
537 | ||
538 | return 0; | |
8fa66bdc ACM |
539 | } |
540 | ||
e7fb08b1 PZ |
541 | /* |
542 | * reverse the map, sort on count. | |
543 | */ | |
544 | ||
545 | static struct rb_root output_hists; | |
546 | ||
547 | static void output__insert_entry(struct hist_entry *he) | |
3a4b8cc7 | 548 | { |
e7fb08b1 | 549 | struct rb_node **p = &output_hists.rb_node; |
3a4b8cc7 | 550 | struct rb_node *parent = NULL; |
e7fb08b1 | 551 | struct hist_entry *iter; |
3a4b8cc7 ACM |
552 | |
553 | while (*p != NULL) { | |
554 | parent = *p; | |
e7fb08b1 | 555 | iter = rb_entry(parent, struct hist_entry, rb_node); |
3a4b8cc7 | 556 | |
e7fb08b1 | 557 | if (he->count > iter->count) |
3a4b8cc7 ACM |
558 | p = &(*p)->rb_left; |
559 | else | |
560 | p = &(*p)->rb_right; | |
561 | } | |
562 | ||
e7fb08b1 PZ |
563 | rb_link_node(&he->rb_node, parent, p); |
564 | rb_insert_color(&he->rb_node, &output_hists); | |
3a4b8cc7 ACM |
565 | } |
566 | ||
e7fb08b1 | 567 | static void output__resort(void) |
3a4b8cc7 | 568 | { |
e7fb08b1 PZ |
569 | struct rb_node *next = rb_first(&hist); |
570 | struct hist_entry *n; | |
3a4b8cc7 | 571 | |
e7fb08b1 PZ |
572 | while (next) { |
573 | n = rb_entry(next, struct hist_entry, rb_node); | |
574 | next = rb_next(&n->rb_node); | |
3a4b8cc7 | 575 | |
e7fb08b1 PZ |
576 | rb_erase(&n->rb_node, &hist); |
577 | output__insert_entry(n); | |
3a4b8cc7 ACM |
578 | } |
579 | } | |
580 | ||
e7fb08b1 | 581 | static size_t output__fprintf(FILE *fp, uint64_t total_samples) |
3a4b8cc7 | 582 | { |
e7fb08b1 | 583 | struct hist_entry *pos; |
2d65537e | 584 | struct sort_entry *se; |
3a4b8cc7 ACM |
585 | struct rb_node *nd; |
586 | size_t ret = 0; | |
587 | ||
ca8cdeef PZ |
588 | fprintf(fp, "#\n"); |
589 | ||
590 | fprintf(fp, "# Overhead"); | |
591 | list_for_each_entry(se, &hist_entry__sort_list, list) | |
592 | fprintf(fp, " %s", se->header); | |
593 | fprintf(fp, "\n"); | |
594 | ||
595 | fprintf(fp, "# ........"); | |
2d65537e | 596 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
ca8cdeef PZ |
597 | int i; |
598 | ||
4593bba8 IM |
599 | fprintf(fp, " "); |
600 | for (i = 0; i < strlen(se->header)-1; i++) | |
ca8cdeef | 601 | fprintf(fp, "."); |
2d65537e | 602 | } |
ca8cdeef PZ |
603 | fprintf(fp, "\n"); |
604 | ||
605 | fprintf(fp, "#\n"); | |
2d65537e | 606 | |
e7fb08b1 PZ |
607 | for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) { |
608 | pos = rb_entry(nd, struct hist_entry, rb_node); | |
609 | ret += hist_entry__fprintf(fp, pos, total_samples); | |
3a4b8cc7 ACM |
610 | } |
611 | ||
612 | return ret; | |
613 | } | |
614 | ||
e7fb08b1 | 615 | |
53cb8bc2 | 616 | static int __cmd_report(void) |
8fa66bdc ACM |
617 | { |
618 | unsigned long offset = 0; | |
619 | unsigned long head = 0; | |
620 | struct stat stat; | |
621 | char *buf; | |
622 | event_t *event; | |
623 | int ret, rc = EXIT_FAILURE; | |
6142f9ec | 624 | uint32_t size; |
f49515b1 | 625 | unsigned long total = 0, total_mmap = 0, total_comm = 0, total_unknown = 0; |
b78c07d4 ACM |
626 | char cwd[PATH_MAX], *cwdp = cwd; |
627 | int cwdlen; | |
8fa66bdc | 628 | |
8fa66bdc ACM |
629 | input = open(input_name, O_RDONLY); |
630 | if (input < 0) { | |
631 | perror("failed to open file"); | |
632 | exit(-1); | |
633 | } | |
634 | ||
635 | ret = fstat(input, &stat); | |
636 | if (ret < 0) { | |
637 | perror("failed to stat file"); | |
638 | exit(-1); | |
639 | } | |
640 | ||
641 | if (!stat.st_size) { | |
642 | fprintf(stderr, "zero-sized file, nothing to do!\n"); | |
643 | exit(0); | |
644 | } | |
645 | ||
450aaa2b | 646 | if (load_kernel() < 0) { |
a2928c42 | 647 | perror("failed to load kernel symbols"); |
8fa66bdc ACM |
648 | return EXIT_FAILURE; |
649 | } | |
650 | ||
b78c07d4 ACM |
651 | if (!full_paths) { |
652 | if (getcwd(cwd, sizeof(cwd)) == NULL) { | |
653 | perror("failed to get the current directory"); | |
654 | return EXIT_FAILURE; | |
655 | } | |
656 | cwdlen = strlen(cwd); | |
10a28255 | 657 | } else { |
b78c07d4 | 658 | cwdp = NULL; |
10a28255 MG |
659 | cwdlen = 0; |
660 | } | |
8fa66bdc ACM |
661 | remap: |
662 | buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ, | |
663 | MAP_SHARED, input, offset); | |
664 | if (buf == MAP_FAILED) { | |
665 | perror("failed to mmap file"); | |
666 | exit(-1); | |
667 | } | |
668 | ||
669 | more: | |
670 | event = (event_t *)(buf + head); | |
671 | ||
6142f9ec PZ |
672 | size = event->header.size; |
673 | if (!size) | |
674 | size = 8; | |
675 | ||
8fa66bdc ACM |
676 | if (head + event->header.size >= page_size * mmap_window) { |
677 | unsigned long shift = page_size * (head / page_size); | |
678 | int ret; | |
679 | ||
680 | ret = munmap(buf, page_size * mmap_window); | |
681 | assert(ret == 0); | |
682 | ||
683 | offset += shift; | |
684 | head -= shift; | |
685 | goto remap; | |
686 | } | |
687 | ||
6142f9ec PZ |
688 | size = event->header.size; |
689 | if (!size) | |
690 | goto broken_event; | |
8fa66bdc | 691 | |
8fa66bdc ACM |
692 | if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) { |
693 | char level; | |
694 | int show = 0; | |
695 | struct dso *dso = NULL; | |
696 | struct thread *thread = threads__findnew(event->ip.pid); | |
f17e04af | 697 | uint64_t ip = event->ip.ip; |
e7fb08b1 | 698 | struct map *map = NULL; |
8fa66bdc | 699 | |
97b07b69 | 700 | if (dump_trace) { |
f49515b1 IM |
701 | fprintf(stderr, "%p [%p]: PERF_EVENT (IP, %d): %d: %p\n", |
702 | (void *)(offset + head), | |
703 | (void *)(long)(event->header.size), | |
97b07b69 IM |
704 | event->header.misc, |
705 | event->ip.pid, | |
16f762a2 | 706 | (void *)(long)ip); |
97b07b69 IM |
707 | } |
708 | ||
ce7e4365 | 709 | if (thread == NULL) { |
55717314 | 710 | fprintf(stderr, "problem processing %d event, skipping it.\n", |
ce7e4365 | 711 | event->header.type); |
55717314 | 712 | goto broken_event; |
ce7e4365 | 713 | } |
8fa66bdc ACM |
714 | |
715 | if (event->header.misc & PERF_EVENT_MISC_KERNEL) { | |
716 | show = SHOW_KERNEL; | |
717 | level = 'k'; | |
e7fb08b1 | 718 | |
8fa66bdc | 719 | dso = kernel_dso; |
e7fb08b1 | 720 | |
8fa66bdc | 721 | } else if (event->header.misc & PERF_EVENT_MISC_USER) { |
16f762a2 | 722 | |
8fa66bdc ACM |
723 | show = SHOW_USER; |
724 | level = '.'; | |
16f762a2 IM |
725 | |
726 | map = thread__find_map(thread, ip); | |
f17e04af | 727 | if (map != NULL) { |
8fa66bdc | 728 | dso = map->dso; |
f17e04af PZ |
729 | ip -= map->start + map->pgoff; |
730 | } | |
e7fb08b1 | 731 | |
8fa66bdc ACM |
732 | } else { |
733 | show = SHOW_HV; | |
734 | level = 'H'; | |
735 | } | |
736 | ||
737 | if (show & show_mask) { | |
f17e04af | 738 | struct symbol *sym = dso__find_symbol(dso, ip); |
8fa66bdc | 739 | |
e7fb08b1 PZ |
740 | if (hist_entry__add(thread, map, dso, sym, ip, level)) { |
741 | fprintf(stderr, | |
55717314 IM |
742 | "problem incrementing symbol count, skipping event\n"); |
743 | goto broken_event; | |
ce7e4365 | 744 | } |
8fa66bdc ACM |
745 | } |
746 | total++; | |
747 | } else switch (event->header.type) { | |
748 | case PERF_EVENT_MMAP: { | |
749 | struct thread *thread = threads__findnew(event->mmap.pid); | |
b78c07d4 | 750 | struct map *map = map__new(&event->mmap, cwdp, cwdlen); |
8fa66bdc | 751 | |
97b07b69 | 752 | if (dump_trace) { |
f49515b1 IM |
753 | fprintf(stderr, "%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n", |
754 | (void *)(offset + head), | |
755 | (void *)(long)(event->header.size), | |
16f762a2 IM |
756 | (void *)(long)event->mmap.start, |
757 | (void *)(long)event->mmap.len, | |
758 | (void *)(long)event->mmap.pgoff, | |
97b07b69 IM |
759 | event->mmap.filename); |
760 | } | |
ce7e4365 | 761 | if (thread == NULL || map == NULL) { |
55717314 IM |
762 | fprintf(stderr, "problem processing PERF_EVENT_MMAP, skipping event.\n"); |
763 | goto broken_event; | |
ce7e4365 | 764 | } |
8fa66bdc | 765 | thread__insert_map(thread, map); |
97b07b69 | 766 | total_mmap++; |
8fa66bdc ACM |
767 | break; |
768 | } | |
769 | case PERF_EVENT_COMM: { | |
770 | struct thread *thread = threads__findnew(event->comm.pid); | |
771 | ||
97b07b69 | 772 | if (dump_trace) { |
f49515b1 IM |
773 | fprintf(stderr, "%p [%p]: PERF_EVENT_COMM: %s:%d\n", |
774 | (void *)(offset + head), | |
775 | (void *)(long)(event->header.size), | |
97b07b69 IM |
776 | event->comm.comm, event->comm.pid); |
777 | } | |
8fa66bdc | 778 | if (thread == NULL || |
ce7e4365 | 779 | thread__set_comm(thread, event->comm.comm)) { |
55717314 IM |
780 | fprintf(stderr, "problem processing PERF_EVENT_COMM, skipping event.\n"); |
781 | goto broken_event; | |
ce7e4365 | 782 | } |
97b07b69 | 783 | total_comm++; |
8fa66bdc ACM |
784 | break; |
785 | } | |
97b07b69 | 786 | default: { |
6142f9ec | 787 | broken_event: |
b7a16eac PZ |
788 | if (dump_trace) |
789 | fprintf(stderr, "%p [%p]: skipping unknown header type: %d\n", | |
790 | (void *)(offset + head), | |
791 | (void *)(long)(event->header.size), | |
792 | event->header.type); | |
793 | ||
3e706114 | 794 | total_unknown++; |
6142f9ec PZ |
795 | |
796 | /* | |
797 | * assume we lost track of the stream, check alignment, and | |
798 | * increment a single u64 in the hope to catch on again 'soon'. | |
799 | */ | |
800 | ||
801 | if (unlikely(head & 7)) | |
802 | head &= ~7ULL; | |
803 | ||
804 | size = 8; | |
97b07b69 | 805 | } |
8fa66bdc ACM |
806 | } |
807 | ||
6142f9ec | 808 | head += size; |
f49515b1 | 809 | |
8fa66bdc ACM |
810 | if (offset + head < stat.st_size) |
811 | goto more; | |
812 | ||
813 | rc = EXIT_SUCCESS; | |
8fa66bdc | 814 | close(input); |
97b07b69 IM |
815 | |
816 | if (dump_trace) { | |
3e706114 IM |
817 | fprintf(stderr, " IP events: %10ld\n", total); |
818 | fprintf(stderr, " mmap events: %10ld\n", total_mmap); | |
819 | fprintf(stderr, " comm events: %10ld\n", total_comm); | |
820 | fprintf(stderr, " unknown events: %10ld\n", total_unknown); | |
97b07b69 IM |
821 | |
822 | return 0; | |
823 | } | |
824 | ||
e7fb08b1 | 825 | if (verbose >= 2) |
16f762a2 | 826 | dsos__fprintf(stdout); |
16f762a2 | 827 | |
e7fb08b1 PZ |
828 | output__resort(); |
829 | output__fprintf(stdout, total); | |
8fa66bdc | 830 | |
8fa66bdc ACM |
831 | return rc; |
832 | } | |
833 | ||
53cb8bc2 IM |
834 | static const char * const report_usage[] = { |
835 | "perf report [<options>] <command>", | |
836 | NULL | |
837 | }; | |
838 | ||
839 | static const struct option options[] = { | |
840 | OPT_STRING('i', "input", &input_name, "file", | |
841 | "input file name"), | |
815e777f ACM |
842 | OPT_BOOLEAN('v', "verbose", &verbose, |
843 | "be more verbose (show symbol address, etc)"), | |
97b07b69 IM |
844 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, |
845 | "dump raw trace in ASCII"), | |
450aaa2b | 846 | OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"), |
63299f05 IM |
847 | OPT_STRING('s', "sort", &sort_order, "key[,key2...]", |
848 | "sort by key(s): pid, comm, dso, symbol. Default: pid,symbol"), | |
b78c07d4 ACM |
849 | OPT_BOOLEAN('P', "full-paths", &full_paths, |
850 | "Don't shorten the pathnames taking into account the cwd"), | |
53cb8bc2 IM |
851 | OPT_END() |
852 | }; | |
853 | ||
854 | int cmd_report(int argc, const char **argv, const char *prefix) | |
855 | { | |
a2928c42 | 856 | symbol__init(); |
53cb8bc2 IM |
857 | |
858 | page_size = getpagesize(); | |
859 | ||
860 | parse_options(argc, argv, options, report_usage, 0); | |
861 | ||
1aa16738 PZ |
862 | setup_sorting(); |
863 | ||
a930d2c0 IM |
864 | setup_pager(); |
865 | ||
53cb8bc2 IM |
866 | return __cmd_report(); |
867 | } |