aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf/builtin-report.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/perf/builtin-report.c')
-rw-r--r--tools/perf/builtin-report.c961
1 files changed, 181 insertions, 780 deletions
diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c
index 19669c20088e..b3d814b54555 100644
--- a/tools/perf/builtin-report.c
+++ b/tools/perf/builtin-report.c
@@ -26,20 +26,18 @@
26#include "util/parse-options.h" 26#include "util/parse-options.h"
27#include "util/parse-events.h" 27#include "util/parse-events.h"
28 28
29#include "util/data_map.h"
29#include "util/thread.h" 30#include "util/thread.h"
31#include "util/sort.h"
32#include "util/hist.h"
30 33
31static char const *input_name = "perf.data"; 34static char const *input_name = "perf.data";
32 35
33static char default_sort_order[] = "comm,dso,symbol";
34static char *sort_order = default_sort_order;
35static char *dso_list_str, *comm_list_str, *sym_list_str, 36static char *dso_list_str, *comm_list_str, *sym_list_str,
36 *col_width_list_str; 37 *col_width_list_str;
37static struct strlist *dso_list, *comm_list, *sym_list; 38static struct strlist *dso_list, *comm_list, *sym_list;
38static char *field_sep;
39 39
40static int force; 40static int force;
41static int input;
42static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
43 41
44static int full_paths; 42static int full_paths;
45static int show_nr_samples; 43static int show_nr_samples;
@@ -50,374 +48,39 @@ static struct perf_read_values show_threads_values;
50static char default_pretty_printing_style[] = "normal"; 48static char default_pretty_printing_style[] = "normal";
51static char *pretty_printing_style = default_pretty_printing_style; 49static char *pretty_printing_style = default_pretty_printing_style;
52 50
53static unsigned long page_size;
54static unsigned long mmap_window = 32;
55
56static char default_parent_pattern[] = "^sys_|^do_page_fault";
57static char *parent_pattern = default_parent_pattern;
58static regex_t parent_regex;
59
60static int exclude_other = 1; 51static int exclude_other = 1;
61 52
62static char callchain_default_opt[] = "fractal,0.5"; 53static char callchain_default_opt[] = "fractal,0.5";
63 54
64static int callchain; 55static char *cwd;
65
66static char __cwd[PATH_MAX];
67static char *cwd = __cwd;
68static int cwdlen; 56static int cwdlen;
69 57
70static struct rb_root threads;
71static struct thread *last_match;
72
73static struct perf_header *header; 58static struct perf_header *header;
74 59
75static
76struct callchain_param callchain_param = {
77 .mode = CHAIN_GRAPH_REL,
78 .min_percent = 0.5
79};
80
81static u64 sample_type; 60static u64 sample_type;
82 61
83static int repsep_fprintf(FILE *fp, const char *fmt, ...)
84{
85 int n;
86 va_list ap;
87
88 va_start(ap, fmt);
89 if (!field_sep)
90 n = vfprintf(fp, fmt, ap);
91 else {
92 char *bf = NULL;
93 n = vasprintf(&bf, fmt, ap);
94 if (n > 0) {
95 char *sep = bf;
96
97 while (1) {
98 sep = strchr(sep, *field_sep);
99 if (sep == NULL)
100 break;
101 *sep = '.';
102 }
103 }
104 fputs(bf, fp);
105 free(bf);
106 }
107 va_end(ap);
108 return n;
109}
110
111static unsigned int dsos__col_width,
112 comms__col_width,
113 threads__col_width;
114
115/*
116 * histogram, sorted on item, collects counts
117 */
118
119static struct rb_root hist;
120
121struct hist_entry {
122 struct rb_node rb_node;
123
124 struct thread *thread;
125 struct map *map;
126 struct dso *dso;
127 struct symbol *sym;
128 struct symbol *parent;
129 u64 ip;
130 char level;
131 struct callchain_node callchain;
132 struct rb_root sorted_chain;
133
134 u64 count;
135};
136
137/*
138 * configurable sorting bits
139 */
140
141struct sort_entry {
142 struct list_head list;
143
144 const char *header;
145
146 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
147 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
148 size_t (*print)(FILE *fp, struct hist_entry *, unsigned int width);
149 unsigned int *width;
150 bool elide;
151};
152
153static int64_t cmp_null(void *l, void *r)
154{
155 if (!l && !r)
156 return 0;
157 else if (!l)
158 return -1;
159 else
160 return 1;
161}
162
163/* --sort pid */
164
165static int64_t
166sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
167{
168 return right->thread->pid - left->thread->pid;
169}
170
171static size_t
172sort__thread_print(FILE *fp, struct hist_entry *self, unsigned int width)
173{
174 return repsep_fprintf(fp, "%*s:%5d", width - 6,
175 self->thread->comm ?: "", self->thread->pid);
176}
177
178static struct sort_entry sort_thread = {
179 .header = "Command: Pid",
180 .cmp = sort__thread_cmp,
181 .print = sort__thread_print,
182 .width = &threads__col_width,
183};
184
185/* --sort comm */
186
187static int64_t
188sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
189{
190 return right->thread->pid - left->thread->pid;
191}
192
193static int64_t
194sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
195{
196 char *comm_l = left->thread->comm;
197 char *comm_r = right->thread->comm;
198
199 if (!comm_l || !comm_r)
200 return cmp_null(comm_l, comm_r);
201
202 return strcmp(comm_l, comm_r);
203}
204
205static size_t
206sort__comm_print(FILE *fp, struct hist_entry *self, unsigned int width)
207{
208 return repsep_fprintf(fp, "%*s", width, self->thread->comm);
209}
210
211static struct sort_entry sort_comm = {
212 .header = "Command",
213 .cmp = sort__comm_cmp,
214 .collapse = sort__comm_collapse,
215 .print = sort__comm_print,
216 .width = &comms__col_width,
217};
218
219/* --sort dso */
220
221static int64_t
222sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
223{
224 struct dso *dso_l = left->dso;
225 struct dso *dso_r = right->dso;
226
227 if (!dso_l || !dso_r)
228 return cmp_null(dso_l, dso_r);
229
230 return strcmp(dso_l->name, dso_r->name);
231}
232
233static size_t
234sort__dso_print(FILE *fp, struct hist_entry *self, unsigned int width)
235{
236 if (self->dso)
237 return repsep_fprintf(fp, "%-*s", width, self->dso->name);
238
239 return repsep_fprintf(fp, "%*llx", width, (u64)self->ip);
240}
241
242static struct sort_entry sort_dso = {
243 .header = "Shared Object",
244 .cmp = sort__dso_cmp,
245 .print = sort__dso_print,
246 .width = &dsos__col_width,
247};
248
249/* --sort symbol */
250
251static int64_t
252sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
253{
254 u64 ip_l, ip_r;
255
256 if (left->sym == right->sym)
257 return 0;
258
259 ip_l = left->sym ? left->sym->start : left->ip;
260 ip_r = right->sym ? right->sym->start : right->ip;
261
262 return (int64_t)(ip_r - ip_l);
263}
264 62
265static size_t 63static size_t
266sort__sym_print(FILE *fp, struct hist_entry *self, unsigned int width __used) 64callchain__fprintf_left_margin(FILE *fp, int left_margin)
267{ 65{
268 size_t ret = 0; 66 int i;
67 int ret;
269 68
270 if (verbose) 69 ret = fprintf(fp, " ");
271 ret += repsep_fprintf(fp, "%#018llx %c ", (u64)self->ip,
272 dso__symtab_origin(self->dso));
273 70
274 ret += repsep_fprintf(fp, "[%c] ", self->level); 71 for (i = 0; i < left_margin; i++)
275 if (self->sym) { 72 ret += fprintf(fp, " ");
276 ret += repsep_fprintf(fp, "%s", self->sym->name);
277
278 if (self->sym->module)
279 ret += repsep_fprintf(fp, "\t[%s]",
280 self->sym->module->name);
281 } else {
282 ret += repsep_fprintf(fp, "%#016llx", (u64)self->ip);
283 }
284 73
285 return ret; 74 return ret;
286} 75}
287 76
288static struct sort_entry sort_sym = { 77static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
289 .header = "Symbol", 78 int left_margin)
290 .cmp = sort__sym_cmp,
291 .print = sort__sym_print,
292};
293
294/* --sort parent */
295
296static int64_t
297sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
298{
299 struct symbol *sym_l = left->parent;
300 struct symbol *sym_r = right->parent;
301
302 if (!sym_l || !sym_r)
303 return cmp_null(sym_l, sym_r);
304
305 return strcmp(sym_l->name, sym_r->name);
306}
307
308static size_t
309sort__parent_print(FILE *fp, struct hist_entry *self, unsigned int width)
310{
311 return repsep_fprintf(fp, "%-*s", width,
312 self->parent ? self->parent->name : "[other]");
313}
314
315static unsigned int parent_symbol__col_width;
316
317static struct sort_entry sort_parent = {
318 .header = "Parent symbol",
319 .cmp = sort__parent_cmp,
320 .print = sort__parent_print,
321 .width = &parent_symbol__col_width,
322};
323
324static int sort__need_collapse = 0;
325static int sort__has_parent = 0;
326
327struct sort_dimension {
328 const char *name;
329 struct sort_entry *entry;
330 int taken;
331};
332
333static struct sort_dimension sort_dimensions[] = {
334 { .name = "pid", .entry = &sort_thread, },
335 { .name = "comm", .entry = &sort_comm, },
336 { .name = "dso", .entry = &sort_dso, },
337 { .name = "symbol", .entry = &sort_sym, },
338 { .name = "parent", .entry = &sort_parent, },
339};
340
341static LIST_HEAD(hist_entry__sort_list);
342
343static int sort_dimension__add(const char *tok)
344{
345 unsigned int i;
346
347 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
348 struct sort_dimension *sd = &sort_dimensions[i];
349
350 if (sd->taken)
351 continue;
352
353 if (strncasecmp(tok, sd->name, strlen(tok)))
354 continue;
355
356 if (sd->entry->collapse)
357 sort__need_collapse = 1;
358
359 if (sd->entry == &sort_parent) {
360 int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
361 if (ret) {
362 char err[BUFSIZ];
363
364 regerror(ret, &parent_regex, err, sizeof(err));
365 fprintf(stderr, "Invalid regex: %s\n%s",
366 parent_pattern, err);
367 exit(-1);
368 }
369 sort__has_parent = 1;
370 }
371
372 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
373 sd->taken = 1;
374
375 return 0;
376 }
377
378 return -ESRCH;
379}
380
381static int64_t
382hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
383{
384 struct sort_entry *se;
385 int64_t cmp = 0;
386
387 list_for_each_entry(se, &hist_entry__sort_list, list) {
388 cmp = se->cmp(left, right);
389 if (cmp)
390 break;
391 }
392
393 return cmp;
394}
395
396static int64_t
397hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
398{
399 struct sort_entry *se;
400 int64_t cmp = 0;
401
402 list_for_each_entry(se, &hist_entry__sort_list, list) {
403 int64_t (*f)(struct hist_entry *, struct hist_entry *);
404
405 f = se->collapse ?: se->cmp;
406
407 cmp = f(left, right);
408 if (cmp)
409 break;
410 }
411
412 return cmp;
413}
414
415static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask)
416{ 79{
417 int i; 80 int i;
418 size_t ret = 0; 81 size_t ret = 0;
419 82
420 ret += fprintf(fp, "%s", " "); 83 ret += callchain__fprintf_left_margin(fp, left_margin);
421 84
422 for (i = 0; i < depth; i++) 85 for (i = 0; i < depth; i++)
423 if (depth_mask & (1 << i)) 86 if (depth_mask & (1 << i))
@@ -432,12 +95,12 @@ static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask)
432static size_t 95static size_t
433ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, int depth, 96ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, int depth,
434 int depth_mask, int count, u64 total_samples, 97 int depth_mask, int count, u64 total_samples,
435 int hits) 98 int hits, int left_margin)
436{ 99{
437 int i; 100 int i;
438 size_t ret = 0; 101 size_t ret = 0;
439 102
440 ret += fprintf(fp, "%s", " "); 103 ret += callchain__fprintf_left_margin(fp, left_margin);
441 for (i = 0; i < depth; i++) { 104 for (i = 0; i < depth; i++) {
442 if (depth_mask & (1 << i)) 105 if (depth_mask & (1 << i))
443 ret += fprintf(fp, "|"); 106 ret += fprintf(fp, "|");
@@ -475,8 +138,9 @@ static void init_rem_hits(void)
475} 138}
476 139
477static size_t 140static size_t
478callchain__fprintf_graph(FILE *fp, struct callchain_node *self, 141__callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
479 u64 total_samples, int depth, int depth_mask) 142 u64 total_samples, int depth, int depth_mask,
143 int left_margin)
480{ 144{
481 struct rb_node *node, *next; 145 struct rb_node *node, *next;
482 struct callchain_node *child; 146 struct callchain_node *child;
@@ -517,7 +181,8 @@ callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
517 * But we keep the older depth mask for the line seperator 181 * But we keep the older depth mask for the line seperator
518 * to keep the level link until we reach the last child 182 * to keep the level link until we reach the last child
519 */ 183 */
520 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask); 184 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
185 left_margin);
521 i = 0; 186 i = 0;
522 list_for_each_entry(chain, &child->val, list) { 187 list_for_each_entry(chain, &child->val, list) {
523 if (chain->ip >= PERF_CONTEXT_MAX) 188 if (chain->ip >= PERF_CONTEXT_MAX)
@@ -525,11 +190,13 @@ callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
525 ret += ipchain__fprintf_graph(fp, chain, depth, 190 ret += ipchain__fprintf_graph(fp, chain, depth,
526 new_depth_mask, i++, 191 new_depth_mask, i++,
527 new_total, 192 new_total,
528 cumul); 193 cumul,
194 left_margin);
529 } 195 }
530 ret += callchain__fprintf_graph(fp, child, new_total, 196 ret += __callchain__fprintf_graph(fp, child, new_total,
531 depth + 1, 197 depth + 1,
532 new_depth_mask | (1 << depth)); 198 new_depth_mask | (1 << depth),
199 left_margin);
533 node = next; 200 node = next;
534 } 201 }
535 202
@@ -543,12 +210,51 @@ callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
543 210
544 ret += ipchain__fprintf_graph(fp, &rem_hits, depth, 211 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
545 new_depth_mask, 0, new_total, 212 new_depth_mask, 0, new_total,
546 remaining); 213 remaining, left_margin);
547 } 214 }
548 215
549 return ret; 216 return ret;
550} 217}
551 218
219
220static size_t
221callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
222 u64 total_samples, int left_margin)
223{
224 struct callchain_list *chain;
225 bool printed = false;
226 int i = 0;
227 int ret = 0;
228
229 list_for_each_entry(chain, &self->val, list) {
230 if (chain->ip >= PERF_CONTEXT_MAX)
231 continue;
232
233 if (!i++ && sort__first_dimension == SORT_SYM)
234 continue;
235
236 if (!printed) {
237 ret += callchain__fprintf_left_margin(fp, left_margin);
238 ret += fprintf(fp, "|\n");
239 ret += callchain__fprintf_left_margin(fp, left_margin);
240 ret += fprintf(fp, "---");
241
242 left_margin += 3;
243 printed = true;
244 } else
245 ret += callchain__fprintf_left_margin(fp, left_margin);
246
247 if (chain->sym)
248 ret += fprintf(fp, " %s\n", chain->sym->name);
249 else
250 ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
251 }
252
253 ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin);
254
255 return ret;
256}
257
552static size_t 258static size_t
553callchain__fprintf_flat(FILE *fp, struct callchain_node *self, 259callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
554 u64 total_samples) 260 u64 total_samples)
@@ -577,7 +283,7 @@ callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
577 283
578static size_t 284static size_t
579hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self, 285hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
580 u64 total_samples) 286 u64 total_samples, int left_margin)
581{ 287{
582 struct rb_node *rb_node; 288 struct rb_node *rb_node;
583 struct callchain_node *chain; 289 struct callchain_node *chain;
@@ -597,8 +303,8 @@ hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
597 break; 303 break;
598 case CHAIN_GRAPH_ABS: /* Falldown */ 304 case CHAIN_GRAPH_ABS: /* Falldown */
599 case CHAIN_GRAPH_REL: 305 case CHAIN_GRAPH_REL:
600 ret += callchain__fprintf_graph(fp, chain, 306 ret += callchain__fprintf_graph(fp, chain, total_samples,
601 total_samples, 1, 1); 307 left_margin);
602 case CHAIN_NONE: 308 case CHAIN_NONE:
603 default: 309 default:
604 break; 310 break;
@@ -610,7 +316,6 @@ hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
610 return ret; 316 return ret;
611} 317}
612 318
613
614static size_t 319static size_t
615hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples) 320hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
616{ 321{
@@ -644,8 +349,19 @@ hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
644 349
645 ret += fprintf(fp, "\n"); 350 ret += fprintf(fp, "\n");
646 351
647 if (callchain) 352 if (callchain) {
648 hist_entry_callchain__fprintf(fp, self, total_samples); 353 int left_margin = 0;
354
355 if (sort__first_dimension == SORT_COMM) {
356 se = list_first_entry(&hist_entry__sort_list, typeof(*se),
357 list);
358 left_margin = se->width ? *se->width : 0;
359 left_margin -= thread__comm_len(self->thread);
360 }
361
362 hist_entry_callchain__fprintf(fp, self, total_samples,
363 left_margin);
364 }
649 365
650 return ret; 366 return ret;
651} 367}
@@ -695,22 +411,17 @@ static int thread__set_comm_adjust(struct thread *self, const char *comm)
695 411
696 412
697static struct symbol * 413static struct symbol *
698resolve_symbol(struct thread *thread, struct map **mapp, 414resolve_symbol(struct thread *thread, struct map **mapp, u64 *ipp)
699 struct dso **dsop, u64 *ipp)
700{ 415{
701 struct dso *dso = dsop ? *dsop : NULL;
702 struct map *map = mapp ? *mapp : NULL; 416 struct map *map = mapp ? *mapp : NULL;
703 u64 ip = *ipp; 417 u64 ip = *ipp;
704 418
705 if (!thread)
706 return NULL;
707
708 if (dso)
709 goto got_dso;
710
711 if (map) 419 if (map)
712 goto got_map; 420 goto got_map;
713 421
422 if (!thread)
423 return NULL;
424
714 map = thread__find_map(thread, ip); 425 map = thread__find_map(thread, ip);
715 if (map != NULL) { 426 if (map != NULL) {
716 /* 427 /*
@@ -725,29 +436,26 @@ resolve_symbol(struct thread *thread, struct map **mapp,
725 *mapp = map; 436 *mapp = map;
726got_map: 437got_map:
727 ip = map->map_ip(map, ip); 438 ip = map->map_ip(map, ip);
728
729 dso = map->dso;
730 } else { 439 } else {
731 /* 440 /*
732 * If this is outside of all known maps, 441 * If this is outside of all known maps,
733 * and is a negative address, try to look it 442 * and is a negative address, try to look it
734 * up in the kernel dso, as it might be a 443 * up in the kernel dso, as it might be a
735 * vsyscall (which executes in user-mode): 444 * vsyscall or vdso (which executes in user-mode).
445 *
446 * XXX This is nasty, we should have a symbol list in
447 * the "[vdso]" dso, but for now lets use the old
448 * trick of looking in the whole kernel symbol list.
736 */ 449 */
737 if ((long long)ip < 0) 450 if ((long long)ip < 0)
738 dso = kernel_dso; 451 return kernel_maps__find_symbol(ip, mapp);
739 } 452 }
740 dump_printf(" ...... dso: %s\n", dso ? dso->name : "<not found>"); 453 dump_printf(" ...... dso: %s\n",
454 map ? map->dso->long_name : "<not found>");
741 dump_printf(" ...... map: %Lx -> %Lx\n", *ipp, ip); 455 dump_printf(" ...... map: %Lx -> %Lx\n", *ipp, ip);
742 *ipp = ip; 456 *ipp = ip;
743 457
744 if (dsop) 458 return map ? map->dso->find_symbol(map->dso, ip) : NULL;
745 *dsop = dso;
746
747 if (!dso)
748 return NULL;
749got_dso:
750 return dso->find_symbol(dso, ip);
751} 459}
752 460
753static int call__match(struct symbol *sym) 461static int call__match(struct symbol *sym)
@@ -758,9 +466,9 @@ static int call__match(struct symbol *sym)
758 return 0; 466 return 0;
759} 467}
760 468
761static struct symbol ** 469static struct symbol **resolve_callchain(struct thread *thread, struct map *map,
762resolve_callchain(struct thread *thread, struct map *map __used, 470 struct ip_callchain *chain,
763 struct ip_callchain *chain, struct hist_entry *entry) 471 struct symbol **parent)
764{ 472{
765 u64 context = PERF_CONTEXT_MAX; 473 u64 context = PERF_CONTEXT_MAX;
766 struct symbol **syms = NULL; 474 struct symbol **syms = NULL;
@@ -776,8 +484,7 @@ resolve_callchain(struct thread *thread, struct map *map __used,
776 484
777 for (i = 0; i < chain->nr; i++) { 485 for (i = 0; i < chain->nr; i++) {
778 u64 ip = chain->ips[i]; 486 u64 ip = chain->ips[i];
779 struct dso *dso = NULL; 487 struct symbol *sym = NULL;
780 struct symbol *sym;
781 488
782 if (ip >= PERF_CONTEXT_MAX) { 489 if (ip >= PERF_CONTEXT_MAX) {
783 context = ip; 490 context = ip;
@@ -786,21 +493,18 @@ resolve_callchain(struct thread *thread, struct map *map __used,
786 493
787 switch (context) { 494 switch (context) {
788 case PERF_CONTEXT_HV: 495 case PERF_CONTEXT_HV:
789 dso = hypervisor_dso;
790 break; 496 break;
791 case PERF_CONTEXT_KERNEL: 497 case PERF_CONTEXT_KERNEL:
792 dso = kernel_dso; 498 sym = kernel_maps__find_symbol(ip, &map);
793 break; 499 break;
794 default: 500 default:
501 sym = resolve_symbol(thread, &map, &ip);
795 break; 502 break;
796 } 503 }
797 504
798 sym = resolve_symbol(thread, NULL, &dso, &ip);
799
800 if (sym) { 505 if (sym) {
801 if (sort__has_parent && call__match(sym) && 506 if (sort__has_parent && !*parent && call__match(sym))
802 !entry->parent) 507 *parent = sym;
803 entry->parent = sym;
804 if (!callchain) 508 if (!callchain)
805 break; 509 break;
806 syms[i] = sym; 510 syms[i] = sym;
@@ -815,177 +519,35 @@ resolve_callchain(struct thread *thread, struct map *map __used,
815 */ 519 */
816 520
817static int 521static int
818hist_entry__add(struct thread *thread, struct map *map, struct dso *dso, 522hist_entry__add(struct thread *thread, struct map *map,
819 struct symbol *sym, u64 ip, struct ip_callchain *chain, 523 struct symbol *sym, u64 ip, struct ip_callchain *chain,
820 char level, u64 count) 524 char level, u64 count)
821{ 525{
822 struct rb_node **p = &hist.rb_node; 526 struct symbol **syms = NULL, *parent = NULL;
823 struct rb_node *parent = NULL; 527 bool hit;
824 struct hist_entry *he; 528 struct hist_entry *he;
825 struct symbol **syms = NULL;
826 struct hist_entry entry = {
827 .thread = thread,
828 .map = map,
829 .dso = dso,
830 .sym = sym,
831 .ip = ip,
832 .level = level,
833 .count = count,
834 .parent = NULL,
835 .sorted_chain = RB_ROOT
836 };
837 int cmp;
838 529
839 if ((sort__has_parent || callchain) && chain) 530 if ((sort__has_parent || callchain) && chain)
840 syms = resolve_callchain(thread, map, chain, &entry); 531 syms = resolve_callchain(thread, map, chain, &parent);
841
842 while (*p != NULL) {
843 parent = *p;
844 he = rb_entry(parent, struct hist_entry, rb_node);
845
846 cmp = hist_entry__cmp(&entry, he);
847 532
848 if (!cmp) { 533 he = __hist_entry__add(thread, map, sym, parent,
849 he->count += count; 534 ip, count, level, &hit);
850 if (callchain) { 535 if (he == NULL)
851 append_chain(&he->callchain, chain, syms); 536 return -ENOMEM;
852 free(syms);
853 }
854 return 0;
855 }
856 537
857 if (cmp < 0) 538 if (hit)
858 p = &(*p)->rb_left; 539 he->count += count;
859 else
860 p = &(*p)->rb_right;
861 }
862 540
863 he = malloc(sizeof(*he));
864 if (!he)
865 return -ENOMEM;
866 *he = entry;
867 if (callchain) { 541 if (callchain) {
868 callchain_init(&he->callchain); 542 if (!hit)
543 callchain_init(&he->callchain);
869 append_chain(&he->callchain, chain, syms); 544 append_chain(&he->callchain, chain, syms);
870 free(syms); 545 free(syms);
871 } 546 }
872 rb_link_node(&he->rb_node, parent, p);
873 rb_insert_color(&he->rb_node, &hist);
874 547
875 return 0; 548 return 0;
876} 549}
877 550
878static void hist_entry__free(struct hist_entry *he)
879{
880 free(he);
881}
882
883/*
884 * collapse the histogram
885 */
886
887static struct rb_root collapse_hists;
888
889static void collapse__insert_entry(struct hist_entry *he)
890{
891 struct rb_node **p = &collapse_hists.rb_node;
892 struct rb_node *parent = NULL;
893 struct hist_entry *iter;
894 int64_t cmp;
895
896 while (*p != NULL) {
897 parent = *p;
898 iter = rb_entry(parent, struct hist_entry, rb_node);
899
900 cmp = hist_entry__collapse(iter, he);
901
902 if (!cmp) {
903 iter->count += he->count;
904 hist_entry__free(he);
905 return;
906 }
907
908 if (cmp < 0)
909 p = &(*p)->rb_left;
910 else
911 p = &(*p)->rb_right;
912 }
913
914 rb_link_node(&he->rb_node, parent, p);
915 rb_insert_color(&he->rb_node, &collapse_hists);
916}
917
918static void collapse__resort(void)
919{
920 struct rb_node *next;
921 struct hist_entry *n;
922
923 if (!sort__need_collapse)
924 return;
925
926 next = rb_first(&hist);
927 while (next) {
928 n = rb_entry(next, struct hist_entry, rb_node);
929 next = rb_next(&n->rb_node);
930
931 rb_erase(&n->rb_node, &hist);
932 collapse__insert_entry(n);
933 }
934}
935
936/*
937 * reverse the map, sort on count.
938 */
939
940static struct rb_root output_hists;
941
942static void output__insert_entry(struct hist_entry *he, u64 min_callchain_hits)
943{
944 struct rb_node **p = &output_hists.rb_node;
945 struct rb_node *parent = NULL;
946 struct hist_entry *iter;
947
948 if (callchain)
949 callchain_param.sort(&he->sorted_chain, &he->callchain,
950 min_callchain_hits, &callchain_param);
951
952 while (*p != NULL) {
953 parent = *p;
954 iter = rb_entry(parent, struct hist_entry, rb_node);
955
956 if (he->count > iter->count)
957 p = &(*p)->rb_left;
958 else
959 p = &(*p)->rb_right;
960 }
961
962 rb_link_node(&he->rb_node, parent, p);
963 rb_insert_color(&he->rb_node, &output_hists);
964}
965
966static void output__resort(u64 total_samples)
967{
968 struct rb_node *next;
969 struct hist_entry *n;
970 struct rb_root *tree = &hist;
971 u64 min_callchain_hits;
972
973 min_callchain_hits = total_samples * (callchain_param.min_percent / 100);
974
975 if (sort__need_collapse)
976 tree = &collapse_hists;
977
978 next = rb_first(tree);
979
980 while (next) {
981 n = rb_entry(next, struct hist_entry, rb_node);
982 next = rb_next(&n->rb_node);
983
984 rb_erase(&n->rb_node, tree);
985 output__insert_entry(n, min_callchain_hits);
986 }
987}
988
989static size_t output__fprintf(FILE *fp, u64 total_samples) 551static size_t output__fprintf(FILE *fp, u64 total_samples)
990{ 552{
991 struct hist_entry *pos; 553 struct hist_entry *pos;
@@ -1080,13 +642,6 @@ print_entries:
1080 return ret; 642 return ret;
1081} 643}
1082 644
1083static unsigned long total = 0,
1084 total_mmap = 0,
1085 total_comm = 0,
1086 total_fork = 0,
1087 total_unknown = 0,
1088 total_lost = 0;
1089
1090static int validate_chain(struct ip_callchain *chain, event_t *event) 645static int validate_chain(struct ip_callchain *chain, event_t *event)
1091{ 646{
1092 unsigned int chain_size; 647 unsigned int chain_size;
@@ -1104,17 +659,14 @@ static int
1104process_sample_event(event_t *event, unsigned long offset, unsigned long head) 659process_sample_event(event_t *event, unsigned long offset, unsigned long head)
1105{ 660{
1106 char level; 661 char level;
1107 int show = 0; 662 struct symbol *sym = NULL;
1108 struct dso *dso = NULL;
1109 struct thread *thread;
1110 u64 ip = event->ip.ip; 663 u64 ip = event->ip.ip;
1111 u64 period = 1; 664 u64 period = 1;
1112 struct map *map = NULL; 665 struct map *map = NULL;
1113 void *more_data = event->ip.__more_data; 666 void *more_data = event->ip.__more_data;
1114 struct ip_callchain *chain = NULL; 667 struct ip_callchain *chain = NULL;
1115 int cpumode; 668 int cpumode;
1116 669 struct thread *thread = threads__findnew(event->ip.pid);
1117 thread = threads__findnew(event->ip.pid, &threads, &last_match);
1118 670
1119 if (sample_type & PERF_SAMPLE_PERIOD) { 671 if (sample_type & PERF_SAMPLE_PERIOD) {
1120 period = *(u64 *)more_data; 672 period = *(u64 *)more_data;
@@ -1137,7 +689,8 @@ process_sample_event(event_t *event, unsigned long offset, unsigned long head)
1137 dump_printf("... chain: nr:%Lu\n", chain->nr); 689 dump_printf("... chain: nr:%Lu\n", chain->nr);
1138 690
1139 if (validate_chain(chain, event) < 0) { 691 if (validate_chain(chain, event) < 0) {
1140 eprintf("call-chain problem with event, skipping it.\n"); 692 pr_debug("call-chain problem with event, "
693 "skipping it.\n");
1141 return 0; 694 return 0;
1142 } 695 }
1143 696
@@ -1147,56 +700,49 @@ process_sample_event(event_t *event, unsigned long offset, unsigned long head)
1147 } 700 }
1148 } 701 }
1149 702
1150 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
1151
1152 if (thread == NULL) { 703 if (thread == NULL) {
1153 eprintf("problem processing %d event, skipping it.\n", 704 pr_debug("problem processing %d event, skipping it.\n",
1154 event->header.type); 705 event->header.type);
1155 return -1; 706 return -1;
1156 } 707 }
1157 708
709 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
710
1158 if (comm_list && !strlist__has_entry(comm_list, thread->comm)) 711 if (comm_list && !strlist__has_entry(comm_list, thread->comm))
1159 return 0; 712 return 0;
1160 713
1161 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; 714 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1162 715
1163 if (cpumode == PERF_RECORD_MISC_KERNEL) { 716 if (cpumode == PERF_RECORD_MISC_KERNEL) {
1164 show = SHOW_KERNEL;
1165 level = 'k'; 717 level = 'k';
1166 718 sym = kernel_maps__find_symbol(ip, &map);
1167 dso = kernel_dso; 719 dump_printf(" ...... dso: %s\n",
1168 720 map ? map->dso->long_name : "<not found>");
1169 dump_printf(" ...... dso: %s\n", dso->name);
1170
1171 } else if (cpumode == PERF_RECORD_MISC_USER) { 721 } else if (cpumode == PERF_RECORD_MISC_USER) {
1172
1173 show = SHOW_USER;
1174 level = '.'; 722 level = '.';
723 sym = resolve_symbol(thread, &map, &ip);
1175 724
1176 } else { 725 } else {
1177 show = SHOW_HV;
1178 level = 'H'; 726 level = 'H';
1179
1180 dso = hypervisor_dso;
1181
1182 dump_printf(" ...... dso: [hypervisor]\n"); 727 dump_printf(" ...... dso: [hypervisor]\n");
1183 } 728 }
1184 729
1185 if (show & show_mask) { 730 if (dso_list &&
1186 struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip); 731 (!map || !map->dso ||
1187 732 !(strlist__has_entry(dso_list, map->dso->short_name) ||
1188 if (dso_list && (!dso || !dso->name || 733 (map->dso->short_name != map->dso->long_name &&
1189 !strlist__has_entry(dso_list, dso->name))) 734 strlist__has_entry(dso_list, map->dso->long_name)))))
1190 return 0; 735 return 0;
1191 736
1192 if (sym_list && (!sym || !strlist__has_entry(sym_list, sym->name))) 737 if (sym_list && sym && !strlist__has_entry(sym_list, sym->name))
1193 return 0; 738 return 0;
1194 739
1195 if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) { 740 if (hist_entry__add(thread, map, sym, ip,
1196 eprintf("problem incrementing symbol count, skipping event\n"); 741 chain, level, period)) {
1197 return -1; 742 pr_debug("problem incrementing symbol count, skipping event\n");
1198 } 743 return -1;
1199 } 744 }
745
1200 total += period; 746 total += period;
1201 747
1202 return 0; 748 return 0;
@@ -1205,10 +751,8 @@ process_sample_event(event_t *event, unsigned long offset, unsigned long head)
1205static int 751static int
1206process_mmap_event(event_t *event, unsigned long offset, unsigned long head) 752process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
1207{ 753{
1208 struct thread *thread; 754 struct map *map = map__new(&event->mmap, cwd, cwdlen, 0, NULL);
1209 struct map *map = map__new(&event->mmap, cwd, cwdlen); 755 struct thread *thread = threads__findnew(event->mmap.pid);
1210
1211 thread = threads__findnew(event->mmap.pid, &threads, &last_match);
1212 756
1213 dump_printf("%p [%p]: PERF_RECORD_MMAP %d/%d: [%p(%p) @ %p]: %s\n", 757 dump_printf("%p [%p]: PERF_RECORD_MMAP %d/%d: [%p(%p) @ %p]: %s\n",
1214 (void *)(offset + head), 758 (void *)(offset + head),
@@ -1234,9 +778,7 @@ process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
1234static int 778static int
1235process_comm_event(event_t *event, unsigned long offset, unsigned long head) 779process_comm_event(event_t *event, unsigned long offset, unsigned long head)
1236{ 780{
1237 struct thread *thread; 781 struct thread *thread = threads__findnew(event->comm.pid);
1238
1239 thread = threads__findnew(event->comm.pid, &threads, &last_match);
1240 782
1241 dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n", 783 dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
1242 (void *)(offset + head), 784 (void *)(offset + head),
@@ -1256,11 +798,8 @@ process_comm_event(event_t *event, unsigned long offset, unsigned long head)
1256static int 798static int
1257process_task_event(event_t *event, unsigned long offset, unsigned long head) 799process_task_event(event_t *event, unsigned long offset, unsigned long head)
1258{ 800{
1259 struct thread *thread; 801 struct thread *thread = threads__findnew(event->fork.pid);
1260 struct thread *parent; 802 struct thread *parent = threads__findnew(event->fork.ppid);
1261
1262 thread = threads__findnew(event->fork.pid, &threads, &last_match);
1263 parent = threads__findnew(event->fork.ppid, &threads, &last_match);
1264 803
1265 dump_printf("%p [%p]: PERF_RECORD_%s: (%d:%d):(%d:%d)\n", 804 dump_printf("%p [%p]: PERF_RECORD_%s: (%d:%d):(%d:%d)\n",
1266 (void *)(offset + head), 805 (void *)(offset + head),
@@ -1331,216 +870,79 @@ process_read_event(event_t *event, unsigned long offset, unsigned long head)
1331 return 0; 870 return 0;
1332} 871}
1333 872
1334static int 873static int sample_type_check(u64 type)
1335process_event(event_t *event, unsigned long offset, unsigned long head)
1336{
1337 trace_event(event);
1338
1339 switch (event->header.type) {
1340 case PERF_RECORD_SAMPLE:
1341 return process_sample_event(event, offset, head);
1342
1343 case PERF_RECORD_MMAP:
1344 return process_mmap_event(event, offset, head);
1345
1346 case PERF_RECORD_COMM:
1347 return process_comm_event(event, offset, head);
1348
1349 case PERF_RECORD_FORK:
1350 case PERF_RECORD_EXIT:
1351 return process_task_event(event, offset, head);
1352
1353 case PERF_RECORD_LOST:
1354 return process_lost_event(event, offset, head);
1355
1356 case PERF_RECORD_READ:
1357 return process_read_event(event, offset, head);
1358
1359 /*
1360 * We dont process them right now but they are fine:
1361 */
1362
1363 case PERF_RECORD_THROTTLE:
1364 case PERF_RECORD_UNTHROTTLE:
1365 return 0;
1366
1367 default:
1368 return -1;
1369 }
1370
1371 return 0;
1372}
1373
1374static int __cmd_report(void)
1375{ 874{
1376 int ret, rc = EXIT_FAILURE; 875 sample_type = type;
1377 unsigned long offset = 0;
1378 unsigned long head, shift;
1379 struct stat input_stat;
1380 struct thread *idle;
1381 event_t *event;
1382 uint32_t size;
1383 char *buf;
1384
1385 idle = register_idle_thread(&threads, &last_match);
1386 thread__comm_adjust(idle);
1387
1388 if (show_threads)
1389 perf_read_values_init(&show_threads_values);
1390
1391 input = open(input_name, O_RDONLY);
1392 if (input < 0) {
1393 fprintf(stderr, " failed to open file: %s", input_name);
1394 if (!strcmp(input_name, "perf.data"))
1395 fprintf(stderr, " (try 'perf record' first)");
1396 fprintf(stderr, "\n");
1397 exit(-1);
1398 }
1399
1400 ret = fstat(input, &input_stat);
1401 if (ret < 0) {
1402 perror("failed to stat file");
1403 exit(-1);
1404 }
1405
1406 if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
1407 fprintf(stderr, "file: %s not owned by current user or root\n", input_name);
1408 exit(-1);
1409 }
1410
1411 if (!input_stat.st_size) {
1412 fprintf(stderr, "zero-sized file, nothing to do!\n");
1413 exit(0);
1414 }
1415
1416 header = perf_header__read(input);
1417 head = header->data_offset;
1418
1419 sample_type = perf_header__sample_type(header);
1420 876
1421 if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) { 877 if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
1422 if (sort__has_parent) { 878 if (sort__has_parent) {
1423 fprintf(stderr, "selected --sort parent, but no" 879 fprintf(stderr, "selected --sort parent, but no"
1424 " callchain data. Did you call" 880 " callchain data. Did you call"
1425 " perf record without -g?\n"); 881 " perf record without -g?\n");
1426 exit(-1); 882 return -1;
1427 } 883 }
1428 if (callchain) { 884 if (callchain) {
1429 fprintf(stderr, "selected -g but no callchain data." 885 fprintf(stderr, "selected -g but no callchain data."
1430 " Did you call perf record without" 886 " Did you call perf record without"
1431 " -g?\n"); 887 " -g?\n");
1432 exit(-1); 888 return -1;
1433 } 889 }
1434 } else if (callchain_param.mode != CHAIN_NONE && !callchain) { 890 } else if (callchain_param.mode != CHAIN_NONE && !callchain) {
1435 callchain = 1; 891 callchain = 1;
1436 if (register_callchain_param(&callchain_param) < 0) { 892 if (register_callchain_param(&callchain_param) < 0) {
1437 fprintf(stderr, "Can't register callchain" 893 fprintf(stderr, "Can't register callchain"
1438 " params\n"); 894 " params\n");
1439 exit(-1); 895 return -1;
1440 } 896 }
1441 } 897 }
1442 898
1443 if (load_kernel() < 0) { 899 return 0;
1444 perror("failed to load kernel symbols"); 900}
1445 return EXIT_FAILURE;
1446 }
1447
1448 if (!full_paths) {
1449 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
1450 perror("failed to get the current directory");
1451 return EXIT_FAILURE;
1452 }
1453 cwdlen = strlen(cwd);
1454 } else {
1455 cwd = NULL;
1456 cwdlen = 0;
1457 }
1458
1459 shift = page_size * (head / page_size);
1460 offset += shift;
1461 head -= shift;
1462
1463remap:
1464 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
1465 MAP_SHARED, input, offset);
1466 if (buf == MAP_FAILED) {
1467 perror("failed to mmap file");
1468 exit(-1);
1469 }
1470
1471more:
1472 event = (event_t *)(buf + head);
1473
1474 size = event->header.size;
1475 if (!size)
1476 size = 8;
1477
1478 if (head + event->header.size >= page_size * mmap_window) {
1479 int munmap_ret;
1480
1481 shift = page_size * (head / page_size);
1482
1483 munmap_ret = munmap(buf, page_size * mmap_window);
1484 assert(munmap_ret == 0);
1485
1486 offset += shift;
1487 head -= shift;
1488 goto remap;
1489 }
1490
1491 size = event->header.size;
1492
1493 dump_printf("\n%p [%p]: event: %d\n",
1494 (void *)(offset + head),
1495 (void *)(long)event->header.size,
1496 event->header.type);
1497
1498 if (!size || process_event(event, offset, head) < 0) {
1499
1500 dump_printf("%p [%p]: skipping unknown header type: %d\n",
1501 (void *)(offset + head),
1502 (void *)(long)(event->header.size),
1503 event->header.type);
1504
1505 total_unknown++;
1506 901
1507 /* 902static struct perf_file_handler file_handler = {
1508 * assume we lost track of the stream, check alignment, and 903 .process_sample_event = process_sample_event,
1509 * increment a single u64 in the hope to catch on again 'soon'. 904 .process_mmap_event = process_mmap_event,
1510 */ 905 .process_comm_event = process_comm_event,
906 .process_exit_event = process_task_event,
907 .process_fork_event = process_task_event,
908 .process_lost_event = process_lost_event,
909 .process_read_event = process_read_event,
910 .sample_type_check = sample_type_check,
911};
1511 912
1512 if (unlikely(head & 7))
1513 head &= ~7ULL;
1514 913
1515 size = 8; 914static int __cmd_report(void)
1516 } 915{
916 struct thread *idle;
917 int ret;
1517 918
1518 head += size; 919 idle = register_idle_thread();
920 thread__comm_adjust(idle);
1519 921
1520 if (offset + head >= header->data_offset + header->data_size) 922 if (show_threads)
1521 goto done; 923 perf_read_values_init(&show_threads_values);
1522 924
1523 if (offset + head < (unsigned long)input_stat.st_size) 925 register_perf_file_handler(&file_handler);
1524 goto more;
1525 926
1526done: 927 ret = mmap_dispatch_perf_file(&header, input_name, force, full_paths,
1527 rc = EXIT_SUCCESS; 928 &cwdlen, &cwd);
1528 close(input); 929 if (ret)
930 return ret;
1529 931
1530 dump_printf(" IP events: %10ld\n", total); 932 dump_printf(" IP events: %10ld\n", total);
1531 dump_printf(" mmap events: %10ld\n", total_mmap); 933 dump_printf(" mmap events: %10ld\n", total_mmap);
1532 dump_printf(" comm events: %10ld\n", total_comm); 934 dump_printf(" comm events: %10ld\n", total_comm);
1533 dump_printf(" fork events: %10ld\n", total_fork); 935 dump_printf(" fork events: %10ld\n", total_fork);
1534 dump_printf(" lost events: %10ld\n", total_lost); 936 dump_printf(" lost events: %10ld\n", total_lost);
1535 dump_printf(" unknown events: %10ld\n", total_unknown); 937 dump_printf(" unknown events: %10ld\n", file_handler.total_unknown);
1536 938
1537 if (dump_trace) 939 if (dump_trace)
1538 return 0; 940 return 0;
1539 941
1540 if (verbose >= 3) 942 if (verbose > 3)
1541 threads__fprintf(stdout, &threads); 943 threads__fprintf(stdout);
1542 944
1543 if (verbose >= 2) 945 if (verbose > 2)
1544 dsos__fprintf(stdout); 946 dsos__fprintf(stdout);
1545 947
1546 collapse__resort(); 948 collapse__resort();
@@ -1550,7 +952,7 @@ done:
1550 if (show_threads) 952 if (show_threads)
1551 perf_read_values_destroy(&show_threads_values); 953 perf_read_values_destroy(&show_threads_values);
1552 954
1553 return rc; 955 return ret;
1554} 956}
1555 957
1556static int 958static int
@@ -1606,7 +1008,8 @@ setup:
1606 return 0; 1008 return 0;
1607} 1009}
1608 1010
1609static const char * const report_usage[] = { 1011//static const char * const report_usage[] = {
1012const char * const report_usage[] = {
1610 "perf report [<options>] <command>", 1013 "perf report [<options>] <command>",
1611 NULL 1014 NULL
1612}; 1015};
@@ -1692,8 +1095,6 @@ int cmd_report(int argc, const char **argv, const char *prefix __used)
1692{ 1095{
1693 symbol__init(); 1096 symbol__init();
1694 1097
1695 page_size = getpagesize();
1696
1697 argc = parse_options(argc, argv, options, report_usage, 0); 1098 argc = parse_options(argc, argv, options, report_usage, 0);
1698 1099
1699 setup_sorting(); 1100 setup_sorting();