perf report: Add option to show total period
[deliverable/linux.git] / tools / perf / util / hist.c
CommitLineData
78f7defe 1#include "annotate.h"
8a0ecfb8 2#include "util.h"
598357eb 3#include "build-id.h"
3d1d07ec 4#include "hist.h"
4e4f06e4
ACM
5#include "session.h"
6#include "sort.h"
9b33827d 7#include <math.h>
3d1d07ec 8
7a007ca9
ACM
9enum hist_filter {
10 HIST_FILTER__DSO,
11 HIST_FILTER__THREAD,
12 HIST_FILTER__PARENT,
13};
14
3d1d07ec
JK
15struct callchain_param callchain_param = {
16 .mode = CHAIN_GRAPH_REL,
d797fdc5
SL
17 .min_percent = 0.5,
18 .order = ORDER_CALLEE
3d1d07ec
JK
19};
20
42b28ac0 21u16 hists__col_len(struct hists *hists, enum hist_column col)
8a6c5b26 22{
42b28ac0 23 return hists->col_len[col];
8a6c5b26
ACM
24}
25
42b28ac0 26void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
8a6c5b26 27{
42b28ac0 28 hists->col_len[col] = len;
8a6c5b26
ACM
29}
30
42b28ac0 31bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
8a6c5b26 32{
42b28ac0
ACM
33 if (len > hists__col_len(hists, col)) {
34 hists__set_col_len(hists, col, len);
8a6c5b26
ACM
35 return true;
36 }
37 return false;
38}
39
42b28ac0 40static void hists__reset_col_len(struct hists *hists)
8a6c5b26
ACM
41{
42 enum hist_column col;
43
44 for (col = 0; col < HISTC_NR_COLS; ++col)
42b28ac0 45 hists__set_col_len(hists, col, 0);
8a6c5b26
ACM
46}
47
42b28ac0 48static void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
8a6c5b26
ACM
49{
50 u16 len;
51
52 if (h->ms.sym)
42b28ac0 53 hists__new_col_len(hists, HISTC_SYMBOL, h->ms.sym->namelen);
d7603d51
ACM
54 else {
55 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
56
42b28ac0 57 if (hists__col_len(hists, HISTC_DSO) < unresolved_col_width &&
d7603d51
ACM
58 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
59 !symbol_conf.dso_list)
42b28ac0 60 hists__set_col_len(hists, HISTC_DSO,
d7603d51
ACM
61 unresolved_col_width);
62 }
8a6c5b26
ACM
63
64 len = thread__comm_len(h->thread);
42b28ac0
ACM
65 if (hists__new_col_len(hists, HISTC_COMM, len))
66 hists__set_col_len(hists, HISTC_THREAD, len + 6);
8a6c5b26
ACM
67
68 if (h->ms.map) {
69 len = dso__name_len(h->ms.map->dso);
42b28ac0 70 hists__new_col_len(hists, HISTC_DSO, len);
8a6c5b26
ACM
71 }
72}
73
c82ee828
ACM
74static void hist_entry__add_cpumode_period(struct hist_entry *self,
75 unsigned int cpumode, u64 period)
a1645ce1 76{
28e2a106 77 switch (cpumode) {
a1645ce1 78 case PERF_RECORD_MISC_KERNEL:
c82ee828 79 self->period_sys += period;
a1645ce1
ZY
80 break;
81 case PERF_RECORD_MISC_USER:
c82ee828 82 self->period_us += period;
a1645ce1
ZY
83 break;
84 case PERF_RECORD_MISC_GUEST_KERNEL:
c82ee828 85 self->period_guest_sys += period;
a1645ce1
ZY
86 break;
87 case PERF_RECORD_MISC_GUEST_USER:
c82ee828 88 self->period_guest_us += period;
a1645ce1
ZY
89 break;
90 default:
91 break;
92 }
93}
94
3d1d07ec 95/*
c82ee828 96 * histogram, sorted on item, collects periods
3d1d07ec
JK
97 */
98
28e2a106
ACM
99static struct hist_entry *hist_entry__new(struct hist_entry *template)
100{
d2009c51 101 size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0;
28e2a106
ACM
102 struct hist_entry *self = malloc(sizeof(*self) + callchain_size);
103
104 if (self != NULL) {
105 *self = *template;
c82ee828 106 self->nr_events = 1;
0a1eae39
ACM
107 if (self->ms.map)
108 self->ms.map->referenced = true;
28e2a106
ACM
109 if (symbol_conf.use_callchain)
110 callchain_init(self->callchain);
111 }
112
113 return self;
114}
115
42b28ac0 116static void hists__inc_nr_entries(struct hists *hists, struct hist_entry *h)
fefb0b94 117{
8a6c5b26 118 if (!h->filtered) {
42b28ac0
ACM
119 hists__calc_col_len(hists, h);
120 ++hists->nr_entries;
8a6c5b26 121 }
fefb0b94
ACM
122}
123
7a007ca9
ACM
124static u8 symbol__parent_filter(const struct symbol *parent)
125{
126 if (symbol_conf.exclude_other && parent == NULL)
127 return 1 << HIST_FILTER__PARENT;
128 return 0;
129}
130
42b28ac0 131struct hist_entry *__hists__add_entry(struct hists *hists,
1c02c4d2 132 struct addr_location *al,
c82ee828 133 struct symbol *sym_parent, u64 period)
9735abf1 134{
42b28ac0 135 struct rb_node **p = &hists->entries.rb_node;
9735abf1
ACM
136 struct rb_node *parent = NULL;
137 struct hist_entry *he;
138 struct hist_entry entry = {
1ed091c4 139 .thread = al->thread,
59fd5306
ACM
140 .ms = {
141 .map = al->map,
142 .sym = al->sym,
143 },
f60f3593 144 .cpu = al->cpu,
1ed091c4
ACM
145 .ip = al->addr,
146 .level = al->level,
c82ee828 147 .period = period,
9735abf1 148 .parent = sym_parent,
7a007ca9 149 .filtered = symbol__parent_filter(sym_parent),
9735abf1
ACM
150 };
151 int cmp;
152
153 while (*p != NULL) {
154 parent = *p;
155 he = rb_entry(parent, struct hist_entry, rb_node);
156
157 cmp = hist_entry__cmp(&entry, he);
158
159 if (!cmp) {
c82ee828
ACM
160 he->period += period;
161 ++he->nr_events;
28e2a106 162 goto out;
9735abf1
ACM
163 }
164
165 if (cmp < 0)
166 p = &(*p)->rb_left;
167 else
168 p = &(*p)->rb_right;
169 }
170
28e2a106 171 he = hist_entry__new(&entry);
9735abf1
ACM
172 if (!he)
173 return NULL;
9735abf1 174 rb_link_node(&he->rb_node, parent, p);
42b28ac0
ACM
175 rb_insert_color(&he->rb_node, &hists->entries);
176 hists__inc_nr_entries(hists, he);
28e2a106 177out:
c82ee828 178 hist_entry__add_cpumode_period(he, al->cpumode, period);
9735abf1
ACM
179 return he;
180}
181
3d1d07ec
JK
182int64_t
183hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
184{
185 struct sort_entry *se;
186 int64_t cmp = 0;
187
188 list_for_each_entry(se, &hist_entry__sort_list, list) {
fcd14984 189 cmp = se->se_cmp(left, right);
3d1d07ec
JK
190 if (cmp)
191 break;
192 }
193
194 return cmp;
195}
196
197int64_t
198hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
199{
200 struct sort_entry *se;
201 int64_t cmp = 0;
202
203 list_for_each_entry(se, &hist_entry__sort_list, list) {
204 int64_t (*f)(struct hist_entry *, struct hist_entry *);
205
fcd14984 206 f = se->se_collapse ?: se->se_cmp;
3d1d07ec
JK
207
208 cmp = f(left, right);
209 if (cmp)
210 break;
211 }
212
213 return cmp;
214}
215
216void hist_entry__free(struct hist_entry *he)
217{
218 free(he);
219}
220
221/*
222 * collapse the histogram
223 */
224
42b28ac0 225static bool hists__collapse_insert_entry(struct hists *hists,
1b3a0e95
FW
226 struct rb_root *root,
227 struct hist_entry *he)
3d1d07ec 228{
b9bf0892 229 struct rb_node **p = &root->rb_node;
3d1d07ec
JK
230 struct rb_node *parent = NULL;
231 struct hist_entry *iter;
232 int64_t cmp;
233
234 while (*p != NULL) {
235 parent = *p;
236 iter = rb_entry(parent, struct hist_entry, rb_node);
237
238 cmp = hist_entry__collapse(iter, he);
239
240 if (!cmp) {
c82ee828 241 iter->period += he->period;
1b3a0e95 242 if (symbol_conf.use_callchain) {
42b28ac0
ACM
243 callchain_cursor_reset(&hists->callchain_cursor);
244 callchain_merge(&hists->callchain_cursor, iter->callchain,
1b3a0e95
FW
245 he->callchain);
246 }
3d1d07ec 247 hist_entry__free(he);
fefb0b94 248 return false;
3d1d07ec
JK
249 }
250
251 if (cmp < 0)
252 p = &(*p)->rb_left;
253 else
254 p = &(*p)->rb_right;
255 }
256
257 rb_link_node(&he->rb_node, parent, p);
b9bf0892 258 rb_insert_color(&he->rb_node, root);
fefb0b94 259 return true;
3d1d07ec
JK
260}
261
42b28ac0 262void hists__collapse_resort(struct hists *hists)
3d1d07ec 263{
b9bf0892 264 struct rb_root tmp;
3d1d07ec
JK
265 struct rb_node *next;
266 struct hist_entry *n;
267
268 if (!sort__need_collapse)
269 return;
270
b9bf0892 271 tmp = RB_ROOT;
42b28ac0
ACM
272 next = rb_first(&hists->entries);
273 hists->nr_entries = 0;
274 hists__reset_col_len(hists);
b9bf0892 275
3d1d07ec
JK
276 while (next) {
277 n = rb_entry(next, struct hist_entry, rb_node);
278 next = rb_next(&n->rb_node);
279
42b28ac0
ACM
280 rb_erase(&n->rb_node, &hists->entries);
281 if (hists__collapse_insert_entry(hists, &tmp, n))
282 hists__inc_nr_entries(hists, n);
3d1d07ec 283 }
b9bf0892 284
42b28ac0 285 hists->entries = tmp;
3d1d07ec
JK
286}
287
288/*
c82ee828 289 * reverse the map, sort on period.
3d1d07ec
JK
290 */
291
1c02c4d2
ACM
292static void __hists__insert_output_entry(struct rb_root *entries,
293 struct hist_entry *he,
294 u64 min_callchain_hits)
3d1d07ec 295{
1c02c4d2 296 struct rb_node **p = &entries->rb_node;
3d1d07ec
JK
297 struct rb_node *parent = NULL;
298 struct hist_entry *iter;
299
d599db3f 300 if (symbol_conf.use_callchain)
b9fb9304 301 callchain_param.sort(&he->sorted_chain, he->callchain,
3d1d07ec
JK
302 min_callchain_hits, &callchain_param);
303
304 while (*p != NULL) {
305 parent = *p;
306 iter = rb_entry(parent, struct hist_entry, rb_node);
307
c82ee828 308 if (he->period > iter->period)
3d1d07ec
JK
309 p = &(*p)->rb_left;
310 else
311 p = &(*p)->rb_right;
312 }
313
314 rb_link_node(&he->rb_node, parent, p);
1c02c4d2 315 rb_insert_color(&he->rb_node, entries);
3d1d07ec
JK
316}
317
42b28ac0 318void hists__output_resort(struct hists *hists)
3d1d07ec 319{
b9bf0892 320 struct rb_root tmp;
3d1d07ec
JK
321 struct rb_node *next;
322 struct hist_entry *n;
3d1d07ec
JK
323 u64 min_callchain_hits;
324
42b28ac0 325 min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
3d1d07ec 326
b9bf0892 327 tmp = RB_ROOT;
42b28ac0 328 next = rb_first(&hists->entries);
3d1d07ec 329
42b28ac0
ACM
330 hists->nr_entries = 0;
331 hists__reset_col_len(hists);
fefb0b94 332
3d1d07ec
JK
333 while (next) {
334 n = rb_entry(next, struct hist_entry, rb_node);
335 next = rb_next(&n->rb_node);
336
42b28ac0 337 rb_erase(&n->rb_node, &hists->entries);
1c02c4d2 338 __hists__insert_output_entry(&tmp, n, min_callchain_hits);
42b28ac0 339 hists__inc_nr_entries(hists, n);
3d1d07ec 340 }
b9bf0892 341
42b28ac0 342 hists->entries = tmp;
3d1d07ec 343}
4ecf84d0
ACM
344
345static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
346{
347 int i;
348 int ret = fprintf(fp, " ");
349
350 for (i = 0; i < left_margin; i++)
351 ret += fprintf(fp, " ");
352
353 return ret;
354}
355
356static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
357 int left_margin)
358{
359 int i;
360 size_t ret = callchain__fprintf_left_margin(fp, left_margin);
361
362 for (i = 0; i < depth; i++)
363 if (depth_mask & (1 << i))
364 ret += fprintf(fp, "| ");
365 else
366 ret += fprintf(fp, " ");
367
368 ret += fprintf(fp, "\n");
369
370 return ret;
371}
372
373static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain,
c82ee828 374 int depth, int depth_mask, int period,
d425de54 375 u64 total_samples, u64 hits,
4ecf84d0
ACM
376 int left_margin)
377{
378 int i;
379 size_t ret = 0;
380
381 ret += callchain__fprintf_left_margin(fp, left_margin);
382 for (i = 0; i < depth; i++) {
383 if (depth_mask & (1 << i))
384 ret += fprintf(fp, "|");
385 else
386 ret += fprintf(fp, " ");
c82ee828 387 if (!period && i == depth - 1) {
4ecf84d0
ACM
388 double percent;
389
390 percent = hits * 100.0 / total_samples;
391 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
392 } else
393 ret += fprintf(fp, "%s", " ");
394 }
b3c9ac08
ACM
395 if (chain->ms.sym)
396 ret += fprintf(fp, "%s\n", chain->ms.sym->name);
4ecf84d0
ACM
397 else
398 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
399
400 return ret;
401}
402
403static struct symbol *rem_sq_bracket;
404static struct callchain_list rem_hits;
405
406static void init_rem_hits(void)
407{
408 rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
409 if (!rem_sq_bracket) {
410 fprintf(stderr, "Not enough memory to display remaining hits\n");
411 return;
412 }
413
414 strcpy(rem_sq_bracket->name, "[...]");
b3c9ac08 415 rem_hits.ms.sym = rem_sq_bracket;
4ecf84d0
ACM
416}
417
418static size_t __callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
419 u64 total_samples, int depth,
420 int depth_mask, int left_margin)
421{
422 struct rb_node *node, *next;
423 struct callchain_node *child;
424 struct callchain_list *chain;
425 int new_depth_mask = depth_mask;
426 u64 new_total;
427 u64 remaining;
428 size_t ret = 0;
429 int i;
232a5c94 430 uint entries_printed = 0;
4ecf84d0
ACM
431
432 if (callchain_param.mode == CHAIN_GRAPH_REL)
433 new_total = self->children_hit;
434 else
435 new_total = total_samples;
436
437 remaining = new_total;
438
439 node = rb_first(&self->rb_root);
440 while (node) {
441 u64 cumul;
442
443 child = rb_entry(node, struct callchain_node, rb_node);
f08c3154 444 cumul = callchain_cumul_hits(child);
4ecf84d0
ACM
445 remaining -= cumul;
446
447 /*
448 * The depth mask manages the output of pipes that show
449 * the depth. We don't want to keep the pipes of the current
450 * level for the last child of this depth.
451 * Except if we have remaining filtered hits. They will
452 * supersede the last child
453 */
454 next = rb_next(node);
455 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
456 new_depth_mask &= ~(1 << (depth - 1));
457
458 /*
3ad2f3fb 459 * But we keep the older depth mask for the line separator
4ecf84d0
ACM
460 * to keep the level link until we reach the last child
461 */
462 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
463 left_margin);
464 i = 0;
465 list_for_each_entry(chain, &child->val, list) {
4ecf84d0
ACM
466 ret += ipchain__fprintf_graph(fp, chain, depth,
467 new_depth_mask, i++,
468 new_total,
469 cumul,
470 left_margin);
471 }
472 ret += __callchain__fprintf_graph(fp, child, new_total,
473 depth + 1,
474 new_depth_mask | (1 << depth),
475 left_margin);
476 node = next;
232a5c94
ACM
477 if (++entries_printed == callchain_param.print_limit)
478 break;
4ecf84d0
ACM
479 }
480
481 if (callchain_param.mode == CHAIN_GRAPH_REL &&
482 remaining && remaining != new_total) {
483
484 if (!rem_sq_bracket)
485 return ret;
486
487 new_depth_mask &= ~(1 << (depth - 1));
488
489 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
490 new_depth_mask, 0, new_total,
491 remaining, left_margin);
492 }
493
494 return ret;
495}
496
497static size_t callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
498 u64 total_samples, int left_margin)
499{
500 struct callchain_list *chain;
501 bool printed = false;
502 int i = 0;
503 int ret = 0;
232a5c94 504 u32 entries_printed = 0;
4ecf84d0
ACM
505
506 list_for_each_entry(chain, &self->val, list) {
4ecf84d0
ACM
507 if (!i++ && sort__first_dimension == SORT_SYM)
508 continue;
509
510 if (!printed) {
511 ret += callchain__fprintf_left_margin(fp, left_margin);
512 ret += fprintf(fp, "|\n");
513 ret += callchain__fprintf_left_margin(fp, left_margin);
514 ret += fprintf(fp, "---");
515
516 left_margin += 3;
517 printed = true;
518 } else
519 ret += callchain__fprintf_left_margin(fp, left_margin);
520
b3c9ac08
ACM
521 if (chain->ms.sym)
522 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
4ecf84d0
ACM
523 else
524 ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
232a5c94
ACM
525
526 if (++entries_printed == callchain_param.print_limit)
527 break;
4ecf84d0
ACM
528 }
529
530 ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin);
531
532 return ret;
533}
534
535static size_t callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
536 u64 total_samples)
537{
538 struct callchain_list *chain;
539 size_t ret = 0;
540
541 if (!self)
542 return 0;
543
544 ret += callchain__fprintf_flat(fp, self->parent, total_samples);
545
546
547 list_for_each_entry(chain, &self->val, list) {
548 if (chain->ip >= PERF_CONTEXT_MAX)
549 continue;
b3c9ac08
ACM
550 if (chain->ms.sym)
551 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
4ecf84d0
ACM
552 else
553 ret += fprintf(fp, " %p\n",
554 (void *)(long)chain->ip);
555 }
556
557 return ret;
558}
559
560static size_t hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
561 u64 total_samples, int left_margin)
562{
563 struct rb_node *rb_node;
564 struct callchain_node *chain;
565 size_t ret = 0;
232a5c94 566 u32 entries_printed = 0;
4ecf84d0
ACM
567
568 rb_node = rb_first(&self->sorted_chain);
569 while (rb_node) {
570 double percent;
571
572 chain = rb_entry(rb_node, struct callchain_node, rb_node);
573 percent = chain->hit * 100.0 / total_samples;
574 switch (callchain_param.mode) {
575 case CHAIN_FLAT:
576 ret += percent_color_fprintf(fp, " %6.2f%%\n",
577 percent);
578 ret += callchain__fprintf_flat(fp, chain, total_samples);
579 break;
580 case CHAIN_GRAPH_ABS: /* Falldown */
581 case CHAIN_GRAPH_REL:
582 ret += callchain__fprintf_graph(fp, chain, total_samples,
583 left_margin);
584 case CHAIN_NONE:
585 default:
586 break;
587 }
588 ret += fprintf(fp, "\n");
232a5c94
ACM
589 if (++entries_printed == callchain_param.print_limit)
590 break;
4ecf84d0
ACM
591 rb_node = rb_next(rb_node);
592 }
593
594 return ret;
595}
596
1c02c4d2 597int hist_entry__snprintf(struct hist_entry *self, char *s, size_t size,
8a6c5b26
ACM
598 struct hists *hists, struct hists *pair_hists,
599 bool show_displacement, long displacement,
600 bool color, u64 session_total)
4ecf84d0
ACM
601{
602 struct sort_entry *se;
c82ee828 603 u64 period, total, period_sys, period_us, period_guest_sys, period_guest_us;
fec9cbd1 604 u64 nr_events;
c351c281 605 const char *sep = symbol_conf.field_sep;
a4e3b956 606 int ret;
4ecf84d0
ACM
607
608 if (symbol_conf.exclude_other && !self->parent)
609 return 0;
610
1c02c4d2 611 if (pair_hists) {
c82ee828 612 period = self->pair ? self->pair->period : 0;
fec9cbd1 613 nr_events = self->pair ? self->pair->nr_events : 0;
cee75ac7 614 total = pair_hists->stats.total_period;
c82ee828
ACM
615 period_sys = self->pair ? self->pair->period_sys : 0;
616 period_us = self->pair ? self->pair->period_us : 0;
617 period_guest_sys = self->pair ? self->pair->period_guest_sys : 0;
618 period_guest_us = self->pair ? self->pair->period_guest_us : 0;
c351c281 619 } else {
c82ee828 620 period = self->period;
fec9cbd1 621 nr_events = self->nr_events;
eefc465c 622 total = session_total;
c82ee828
ACM
623 period_sys = self->period_sys;
624 period_us = self->period_us;
625 period_guest_sys = self->period_guest_sys;
626 period_guest_us = self->period_guest_us;
c351c281
ACM
627 }
628
a4e3b956
ACM
629 if (total) {
630 if (color)
631 ret = percent_color_snprintf(s, size,
632 sep ? "%.2f" : " %6.2f%%",
c82ee828 633 (period * 100.0) / total);
a4e3b956
ACM
634 else
635 ret = snprintf(s, size, sep ? "%.2f" : " %6.2f%%",
c82ee828 636 (period * 100.0) / total);
a1645ce1
ZY
637 if (symbol_conf.show_cpu_utilization) {
638 ret += percent_color_snprintf(s + ret, size - ret,
639 sep ? "%.2f" : " %6.2f%%",
c82ee828 640 (period_sys * 100.0) / total);
a1645ce1
ZY
641 ret += percent_color_snprintf(s + ret, size - ret,
642 sep ? "%.2f" : " %6.2f%%",
c82ee828 643 (period_us * 100.0) / total);
a1645ce1
ZY
644 if (perf_guest) {
645 ret += percent_color_snprintf(s + ret,
646 size - ret,
647 sep ? "%.2f" : " %6.2f%%",
c82ee828 648 (period_guest_sys * 100.0) /
a1645ce1
ZY
649 total);
650 ret += percent_color_snprintf(s + ret,
651 size - ret,
652 sep ? "%.2f" : " %6.2f%%",
c82ee828 653 (period_guest_us * 100.0) /
a1645ce1
ZY
654 total);
655 }
656 }
a4e3b956 657 } else
9486aa38 658 ret = snprintf(s, size, sep ? "%" PRIu64 : "%12" PRIu64 " ", period);
4ecf84d0
ACM
659
660 if (symbol_conf.show_nr_samples) {
c351c281 661 if (sep)
fec9cbd1 662 ret += snprintf(s + ret, size - ret, "%c%" PRIu64, *sep, nr_events);
4ecf84d0 663 else
fec9cbd1 664 ret += snprintf(s + ret, size - ret, "%11" PRIu64, nr_events);
c351c281
ACM
665 }
666
3f2728bd
ACM
667 if (symbol_conf.show_total_period) {
668 if (sep)
669 ret += snprintf(s + ret, size - ret, "%c%" PRIu64, *sep, period);
670 else
671 ret += snprintf(s + ret, size - ret, " %12" PRIu64, period);
672 }
673
1c02c4d2 674 if (pair_hists) {
c351c281
ACM
675 char bf[32];
676 double old_percent = 0, new_percent = 0, diff;
677
678 if (total > 0)
c82ee828 679 old_percent = (period * 100.0) / total;
eefc465c 680 if (session_total > 0)
c82ee828 681 new_percent = (self->period * 100.0) / session_total;
c351c281 682
9b33827d 683 diff = new_percent - old_percent;
c351c281 684
9b33827d 685 if (fabs(diff) >= 0.01)
c351c281
ACM
686 snprintf(bf, sizeof(bf), "%+4.2F%%", diff);
687 else
688 snprintf(bf, sizeof(bf), " ");
689
690 if (sep)
a4e3b956 691 ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
c351c281 692 else
a4e3b956 693 ret += snprintf(s + ret, size - ret, "%11.11s", bf);
c351c281
ACM
694
695 if (show_displacement) {
696 if (displacement)
697 snprintf(bf, sizeof(bf), "%+4ld", displacement);
698 else
699 snprintf(bf, sizeof(bf), " ");
700
701 if (sep)
a4e3b956 702 ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
c351c281 703 else
a4e3b956 704 ret += snprintf(s + ret, size - ret, "%6.6s", bf);
c351c281 705 }
4ecf84d0
ACM
706 }
707
708 list_for_each_entry(se, &hist_entry__sort_list, list) {
709 if (se->elide)
710 continue;
711
a4e3b956 712 ret += snprintf(s + ret, size - ret, "%s", sep ?: " ");
fcd14984 713 ret += se->se_snprintf(self, s + ret, size - ret,
8a6c5b26 714 hists__col_len(hists, se->se_width_idx));
4ecf84d0
ACM
715 }
716
a4e3b956
ACM
717 return ret;
718}
719
ef9dfe6e 720int hist_entry__fprintf(struct hist_entry *he, size_t size, struct hists *hists,
8a6c5b26
ACM
721 struct hists *pair_hists, bool show_displacement,
722 long displacement, FILE *fp, u64 session_total)
a4e3b956
ACM
723{
724 char bf[512];
ef9dfe6e
ACM
725
726 if (size == 0 || size > sizeof(bf))
727 size = sizeof(bf);
728
729 hist_entry__snprintf(he, bf, size, hists, pair_hists,
a4e3b956
ACM
730 show_displacement, displacement,
731 true, session_total);
732 return fprintf(fp, "%s\n", bf);
3997d377 733}
4ecf84d0 734
8a6c5b26
ACM
735static size_t hist_entry__fprintf_callchain(struct hist_entry *self,
736 struct hists *hists, FILE *fp,
3997d377
ACM
737 u64 session_total)
738{
739 int left_margin = 0;
4ecf84d0 740
3997d377
ACM
741 if (sort__first_dimension == SORT_COMM) {
742 struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
743 typeof(*se), list);
8a6c5b26 744 left_margin = hists__col_len(hists, se->se_width_idx);
3997d377 745 left_margin -= thread__comm_len(self->thread);
4ecf84d0
ACM
746 }
747
3997d377
ACM
748 return hist_entry_callchain__fprintf(fp, self, session_total,
749 left_margin);
4ecf84d0
ACM
750}
751
42b28ac0 752size_t hists__fprintf(struct hists *hists, struct hists *pair,
ef9dfe6e
ACM
753 bool show_displacement, bool show_header, int max_rows,
754 int max_cols, FILE *fp)
4ecf84d0 755{
4ecf84d0
ACM
756 struct sort_entry *se;
757 struct rb_node *nd;
758 size_t ret = 0;
c351c281
ACM
759 unsigned long position = 1;
760 long displacement = 0;
4ecf84d0 761 unsigned int width;
c351c281 762 const char *sep = symbol_conf.field_sep;
edb7c60e 763 const char *col_width = symbol_conf.col_width_list_str;
ef9dfe6e 764 int nr_rows = 0;
4ecf84d0
ACM
765
766 init_rem_hits();
767
ef9dfe6e
ACM
768 if (!show_header)
769 goto print_entries;
770
c351c281
ACM
771 fprintf(fp, "# %s", pair ? "Baseline" : "Overhead");
772
4ecf84d0 773 if (symbol_conf.show_nr_samples) {
c351c281
ACM
774 if (sep)
775 fprintf(fp, "%cSamples", *sep);
4ecf84d0
ACM
776 else
777 fputs(" Samples ", fp);
778 }
c351c281 779
3f2728bd
ACM
780 if (symbol_conf.show_total_period) {
781 if (sep)
782 ret += fprintf(fp, "%cPeriod", *sep);
783 else
784 ret += fprintf(fp, " Period ");
785 }
786
a1645ce1
ZY
787 if (symbol_conf.show_cpu_utilization) {
788 if (sep) {
789 ret += fprintf(fp, "%csys", *sep);
790 ret += fprintf(fp, "%cus", *sep);
791 if (perf_guest) {
792 ret += fprintf(fp, "%cguest sys", *sep);
793 ret += fprintf(fp, "%cguest us", *sep);
794 }
795 } else {
796 ret += fprintf(fp, " sys ");
797 ret += fprintf(fp, " us ");
798 if (perf_guest) {
799 ret += fprintf(fp, " guest sys ");
800 ret += fprintf(fp, " guest us ");
801 }
802 }
803 }
804
c351c281
ACM
805 if (pair) {
806 if (sep)
807 ret += fprintf(fp, "%cDelta", *sep);
808 else
809 ret += fprintf(fp, " Delta ");
810
811 if (show_displacement) {
812 if (sep)
813 ret += fprintf(fp, "%cDisplacement", *sep);
814 else
815 ret += fprintf(fp, " Displ");
816 }
817 }
818
4ecf84d0
ACM
819 list_for_each_entry(se, &hist_entry__sort_list, list) {
820 if (se->elide)
821 continue;
c351c281 822 if (sep) {
fcd14984 823 fprintf(fp, "%c%s", *sep, se->se_header);
4ecf84d0
ACM
824 continue;
825 }
fcd14984 826 width = strlen(se->se_header);
8a6c5b26
ACM
827 if (symbol_conf.col_width_list_str) {
828 if (col_width) {
42b28ac0 829 hists__set_col_len(hists, se->se_width_idx,
8a6c5b26
ACM
830 atoi(col_width));
831 col_width = strchr(col_width, ',');
832 if (col_width)
833 ++col_width;
4ecf84d0 834 }
4ecf84d0 835 }
42b28ac0
ACM
836 if (!hists__new_col_len(hists, se->se_width_idx, width))
837 width = hists__col_len(hists, se->se_width_idx);
fcd14984 838 fprintf(fp, " %*s", width, se->se_header);
4ecf84d0 839 }
ef9dfe6e 840
4ecf84d0 841 fprintf(fp, "\n");
ef9dfe6e
ACM
842 if (max_rows && ++nr_rows >= max_rows)
843 goto out;
4ecf84d0 844
c351c281 845 if (sep)
4ecf84d0
ACM
846 goto print_entries;
847
848 fprintf(fp, "# ........");
849 if (symbol_conf.show_nr_samples)
850 fprintf(fp, " ..........");
3f2728bd
ACM
851 if (symbol_conf.show_total_period)
852 fprintf(fp, " ............");
c351c281
ACM
853 if (pair) {
854 fprintf(fp, " ..........");
855 if (show_displacement)
856 fprintf(fp, " .....");
857 }
4ecf84d0
ACM
858 list_for_each_entry(se, &hist_entry__sort_list, list) {
859 unsigned int i;
860
861 if (se->elide)
862 continue;
863
864 fprintf(fp, " ");
42b28ac0 865 width = hists__col_len(hists, se->se_width_idx);
8a6c5b26 866 if (width == 0)
fcd14984 867 width = strlen(se->se_header);
4ecf84d0
ACM
868 for (i = 0; i < width; i++)
869 fprintf(fp, ".");
870 }
4ecf84d0 871
ef9dfe6e
ACM
872 fprintf(fp, "\n");
873 if (max_rows && ++nr_rows >= max_rows)
874 goto out;
875
876 fprintf(fp, "#\n");
877 if (max_rows && ++nr_rows >= max_rows)
878 goto out;
4ecf84d0
ACM
879
880print_entries:
42b28ac0 881 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
c351c281
ACM
882 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
883
e84d2122
FW
884 if (h->filtered)
885 continue;
886
c351c281
ACM
887 if (show_displacement) {
888 if (h->pair != NULL)
889 displacement = ((long)h->pair->position -
890 (long)position);
891 else
892 displacement = 0;
893 ++position;
894 }
ef9dfe6e 895 ret += hist_entry__fprintf(h, max_cols, hists, pair, show_displacement,
42b28ac0 896 displacement, fp, hists->stats.total_period);
3997d377
ACM
897
898 if (symbol_conf.use_callchain)
42b28ac0
ACM
899 ret += hist_entry__fprintf_callchain(h, hists, fp,
900 hists->stats.total_period);
ef9dfe6e
ACM
901 if (max_rows && ++nr_rows >= max_rows)
902 goto out;
903
59fd5306 904 if (h->ms.map == NULL && verbose > 1) {
65f2ed2b 905 __map_groups__fprintf_maps(&h->thread->mg,
c6e718ff 906 MAP__FUNCTION, verbose, fp);
65f2ed2b
ACM
907 fprintf(fp, "%.10s end\n", graph_dotted_line);
908 }
4ecf84d0 909 }
ef9dfe6e 910out:
4ecf84d0
ACM
911 free(rem_sq_bracket);
912
913 return ret;
914}
b09e0190 915
06daaaba
ACM
916/*
917 * See hists__fprintf to match the column widths
918 */
42b28ac0 919unsigned int hists__sort_list_width(struct hists *hists)
06daaaba
ACM
920{
921 struct sort_entry *se;
922 int ret = 9; /* total % */
923
924 if (symbol_conf.show_cpu_utilization) {
925 ret += 7; /* count_sys % */
926 ret += 6; /* count_us % */
927 if (perf_guest) {
928 ret += 13; /* count_guest_sys % */
929 ret += 12; /* count_guest_us % */
930 }
931 }
932
933 if (symbol_conf.show_nr_samples)
934 ret += 11;
935
3f2728bd
ACM
936 if (symbol_conf.show_total_period)
937 ret += 13;
938
06daaaba
ACM
939 list_for_each_entry(se, &hist_entry__sort_list, list)
940 if (!se->elide)
42b28ac0 941 ret += 2 + hists__col_len(hists, se->se_width_idx);
06daaaba 942
903cce6e
ACM
943 if (verbose) /* Addr + origin */
944 ret += 3 + BITS_PER_LONG / 4;
945
06daaaba
ACM
946 return ret;
947}
948
42b28ac0 949static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
cc5edb0e
ACM
950 enum hist_filter filter)
951{
952 h->filtered &= ~(1 << filter);
953 if (h->filtered)
954 return;
955
42b28ac0 956 ++hists->nr_entries;
0f0cbf7a 957 if (h->ms.unfolded)
42b28ac0 958 hists->nr_entries += h->nr_rows;
0f0cbf7a 959 h->row_offset = 0;
42b28ac0
ACM
960 hists->stats.total_period += h->period;
961 hists->stats.nr_events[PERF_RECORD_SAMPLE] += h->nr_events;
cc5edb0e 962
42b28ac0 963 hists__calc_col_len(hists, h);
cc5edb0e
ACM
964}
965
42b28ac0 966void hists__filter_by_dso(struct hists *hists, const struct dso *dso)
b09e0190
ACM
967{
968 struct rb_node *nd;
969
42b28ac0
ACM
970 hists->nr_entries = hists->stats.total_period = 0;
971 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
972 hists__reset_col_len(hists);
b09e0190 973
42b28ac0 974 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
b09e0190
ACM
975 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
976
977 if (symbol_conf.exclude_other && !h->parent)
978 continue;
979
980 if (dso != NULL && (h->ms.map == NULL || h->ms.map->dso != dso)) {
981 h->filtered |= (1 << HIST_FILTER__DSO);
982 continue;
983 }
984
42b28ac0 985 hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
b09e0190
ACM
986 }
987}
988
42b28ac0 989void hists__filter_by_thread(struct hists *hists, const struct thread *thread)
b09e0190
ACM
990{
991 struct rb_node *nd;
992
42b28ac0
ACM
993 hists->nr_entries = hists->stats.total_period = 0;
994 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
995 hists__reset_col_len(hists);
b09e0190 996
42b28ac0 997 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
b09e0190
ACM
998 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
999
1000 if (thread != NULL && h->thread != thread) {
1001 h->filtered |= (1 << HIST_FILTER__THREAD);
1002 continue;
1003 }
cc5edb0e 1004
42b28ac0 1005 hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
b09e0190
ACM
1006 }
1007}
ef7b93a1 1008
2f525d01 1009int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
ef7b93a1 1010{
2f525d01 1011 return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
ef7b93a1
ACM
1012}
1013
ce6f4fab 1014int hist_entry__annotate(struct hist_entry *he, size_t privsize)
ef7b93a1 1015{
ce6f4fab 1016 return symbol__annotate(he->ms.sym, he->ms.map, privsize);
ef7b93a1 1017}
c8446b9b 1018
42b28ac0 1019void hists__inc_nr_events(struct hists *hists, u32 type)
c8446b9b 1020{
42b28ac0
ACM
1021 ++hists->stats.nr_events[0];
1022 ++hists->stats.nr_events[type];
c8446b9b
ACM
1023}
1024
42b28ac0 1025size_t hists__fprintf_nr_events(struct hists *hists, FILE *fp)
c8446b9b
ACM
1026{
1027 int i;
1028 size_t ret = 0;
1029
1030 for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
e248de33 1031 const char *name;
3835bc00 1032
42b28ac0 1033 if (hists->stats.nr_events[i] == 0)
e248de33
ACM
1034 continue;
1035
1036 name = perf_event__name(i);
3835bc00 1037 if (!strcmp(name, "UNKNOWN"))
c8446b9b 1038 continue;
3835bc00
TG
1039
1040 ret += fprintf(fp, "%16s events: %10d\n", name,
42b28ac0 1041 hists->stats.nr_events[i]);
c8446b9b
ACM
1042 }
1043
1044 return ret;
1045}
This page took 0.200726 seconds and 5 git commands to generate.