perf bench: fix assert when NDEBUG is defined
[linux-2.6-block.git] / tools / perf / ui / stdio / hist.c
CommitLineData
7ccf4f90
NK
1#include <stdio.h>
2#include <math.h>
3
4#include "../../util/util.h"
5#include "../../util/hist.h"
6#include "../../util/sort.h"
7
8
9static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
10{
11 int i;
12 int ret = fprintf(fp, " ");
13
14 for (i = 0; i < left_margin; i++)
15 ret += fprintf(fp, " ");
16
17 return ret;
18}
19
20static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
21 int left_margin)
22{
23 int i;
24 size_t ret = callchain__fprintf_left_margin(fp, left_margin);
25
26 for (i = 0; i < depth; i++)
27 if (depth_mask & (1 << i))
28 ret += fprintf(fp, "| ");
29 else
30 ret += fprintf(fp, " ");
31
32 ret += fprintf(fp, "\n");
33
34 return ret;
35}
36
37static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain,
38 int depth, int depth_mask, int period,
39 u64 total_samples, u64 hits,
40 int left_margin)
41{
42 int i;
43 size_t ret = 0;
44
45 ret += callchain__fprintf_left_margin(fp, left_margin);
46 for (i = 0; i < depth; i++) {
47 if (depth_mask & (1 << i))
48 ret += fprintf(fp, "|");
49 else
50 ret += fprintf(fp, " ");
51 if (!period && i == depth - 1) {
52 double percent;
53
54 percent = hits * 100.0 / total_samples;
55 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
56 } else
57 ret += fprintf(fp, "%s", " ");
58 }
59 if (chain->ms.sym)
60 ret += fprintf(fp, "%s\n", chain->ms.sym->name);
61 else
62 ret += fprintf(fp, "0x%0" PRIx64 "\n", chain->ip);
63
64 return ret;
65}
66
67static struct symbol *rem_sq_bracket;
68static struct callchain_list rem_hits;
69
70static void init_rem_hits(void)
71{
72 rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
73 if (!rem_sq_bracket) {
74 fprintf(stderr, "Not enough memory to display remaining hits\n");
75 return;
76 }
77
78 strcpy(rem_sq_bracket->name, "[...]");
79 rem_hits.ms.sym = rem_sq_bracket;
80}
81
82static size_t __callchain__fprintf_graph(FILE *fp, struct rb_root *root,
83 u64 total_samples, int depth,
84 int depth_mask, int left_margin)
85{
86 struct rb_node *node, *next;
87 struct callchain_node *child;
88 struct callchain_list *chain;
89 int new_depth_mask = depth_mask;
90 u64 remaining;
91 size_t ret = 0;
92 int i;
93 uint entries_printed = 0;
94
95 remaining = total_samples;
96
97 node = rb_first(root);
98 while (node) {
99 u64 new_total;
100 u64 cumul;
101
102 child = rb_entry(node, struct callchain_node, rb_node);
103 cumul = callchain_cumul_hits(child);
104 remaining -= cumul;
105
106 /*
107 * The depth mask manages the output of pipes that show
108 * the depth. We don't want to keep the pipes of the current
109 * level for the last child of this depth.
110 * Except if we have remaining filtered hits. They will
111 * supersede the last child
112 */
113 next = rb_next(node);
114 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
115 new_depth_mask &= ~(1 << (depth - 1));
116
117 /*
118 * But we keep the older depth mask for the line separator
119 * to keep the level link until we reach the last child
120 */
121 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
122 left_margin);
123 i = 0;
124 list_for_each_entry(chain, &child->val, list) {
125 ret += ipchain__fprintf_graph(fp, chain, depth,
126 new_depth_mask, i++,
127 total_samples,
128 cumul,
129 left_margin);
130 }
131
132 if (callchain_param.mode == CHAIN_GRAPH_REL)
133 new_total = child->children_hit;
134 else
135 new_total = total_samples;
136
137 ret += __callchain__fprintf_graph(fp, &child->rb_root, new_total,
138 depth + 1,
139 new_depth_mask | (1 << depth),
140 left_margin);
141 node = next;
142 if (++entries_printed == callchain_param.print_limit)
143 break;
144 }
145
146 if (callchain_param.mode == CHAIN_GRAPH_REL &&
147 remaining && remaining != total_samples) {
148
149 if (!rem_sq_bracket)
150 return ret;
151
152 new_depth_mask &= ~(1 << (depth - 1));
153 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
154 new_depth_mask, 0, total_samples,
155 remaining, left_margin);
156 }
157
158 return ret;
159}
160
161static size_t callchain__fprintf_graph(FILE *fp, struct rb_root *root,
162 u64 total_samples, int left_margin)
163{
164 struct callchain_node *cnode;
165 struct callchain_list *chain;
166 u32 entries_printed = 0;
167 bool printed = false;
168 struct rb_node *node;
169 int i = 0;
170 int ret = 0;
171
172 /*
173 * If have one single callchain root, don't bother printing
174 * its percentage (100 % in fractal mode and the same percentage
175 * than the hist in graph mode). This also avoid one level of column.
176 */
177 node = rb_first(root);
178 if (node && !rb_next(node)) {
179 cnode = rb_entry(node, struct callchain_node, rb_node);
180 list_for_each_entry(chain, &cnode->val, list) {
181 /*
182 * If we sort by symbol, the first entry is the same than
183 * the symbol. No need to print it otherwise it appears as
184 * displayed twice.
185 */
186 if (!i++ && sort__first_dimension == SORT_SYM)
187 continue;
188 if (!printed) {
189 ret += callchain__fprintf_left_margin(fp, left_margin);
190 ret += fprintf(fp, "|\n");
191 ret += callchain__fprintf_left_margin(fp, left_margin);
192 ret += fprintf(fp, "---");
193 left_margin += 3;
194 printed = true;
195 } else
196 ret += callchain__fprintf_left_margin(fp, left_margin);
197
198 if (chain->ms.sym)
199 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
200 else
201 ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
202
203 if (++entries_printed == callchain_param.print_limit)
204 break;
205 }
206 root = &cnode->rb_root;
207 }
208
209 ret += __callchain__fprintf_graph(fp, root, total_samples,
210 1, 1, left_margin);
211 ret += fprintf(fp, "\n");
212
213 return ret;
214}
215
216static size_t __callchain__fprintf_flat(FILE *fp,
217 struct callchain_node *self,
218 u64 total_samples)
219{
220 struct callchain_list *chain;
221 size_t ret = 0;
222
223 if (!self)
224 return 0;
225
226 ret += __callchain__fprintf_flat(fp, self->parent, total_samples);
227
228
229 list_for_each_entry(chain, &self->val, list) {
230 if (chain->ip >= PERF_CONTEXT_MAX)
231 continue;
232 if (chain->ms.sym)
233 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
234 else
235 ret += fprintf(fp, " %p\n",
236 (void *)(long)chain->ip);
237 }
238
239 return ret;
240}
241
242static size_t callchain__fprintf_flat(FILE *fp, struct rb_root *self,
243 u64 total_samples)
244{
245 size_t ret = 0;
246 u32 entries_printed = 0;
247 struct rb_node *rb_node;
248 struct callchain_node *chain;
249
250 rb_node = rb_first(self);
251 while (rb_node) {
252 double percent;
253
254 chain = rb_entry(rb_node, struct callchain_node, rb_node);
255 percent = chain->hit * 100.0 / total_samples;
256
257 ret = percent_color_fprintf(fp, " %6.2f%%\n", percent);
258 ret += __callchain__fprintf_flat(fp, chain, total_samples);
259 ret += fprintf(fp, "\n");
260 if (++entries_printed == callchain_param.print_limit)
261 break;
262
263 rb_node = rb_next(rb_node);
264 }
265
266 return ret;
267}
268
269static size_t hist_entry_callchain__fprintf(struct hist_entry *he,
270 u64 total_samples, int left_margin,
271 FILE *fp)
272{
273 switch (callchain_param.mode) {
274 case CHAIN_GRAPH_REL:
275 return callchain__fprintf_graph(fp, &he->sorted_chain, he->period,
276 left_margin);
277 break;
278 case CHAIN_GRAPH_ABS:
279 return callchain__fprintf_graph(fp, &he->sorted_chain, total_samples,
280 left_margin);
281 break;
282 case CHAIN_FLAT:
283 return callchain__fprintf_flat(fp, &he->sorted_chain, total_samples);
284 break;
285 case CHAIN_NONE:
286 break;
287 default:
288 pr_err("Bad callchain mode\n");
289 }
290
291 return 0;
292}
293
000078bc 294static int hist_entry__period_snprintf(struct hist_entry *he, char *s,
7ccf4f90
NK
295 size_t size, struct hists *pair_hists,
296 bool show_displacement, long displacement,
297 bool color, u64 total_period)
298{
299 u64 period, total, period_sys, period_us, period_guest_sys, period_guest_us;
300 u64 nr_events;
301 const char *sep = symbol_conf.field_sep;
302 int ret;
303
304 if (symbol_conf.exclude_other && !he->parent)
305 return 0;
306
307 if (pair_hists) {
308 period = he->pair ? he->pair->period : 0;
309 nr_events = he->pair ? he->pair->nr_events : 0;
310 total = pair_hists->stats.total_period;
311 period_sys = he->pair ? he->pair->period_sys : 0;
312 period_us = he->pair ? he->pair->period_us : 0;
313 period_guest_sys = he->pair ? he->pair->period_guest_sys : 0;
314 period_guest_us = he->pair ? he->pair->period_guest_us : 0;
315 } else {
316 period = he->period;
317 nr_events = he->nr_events;
318 total = total_period;
319 period_sys = he->period_sys;
320 period_us = he->period_us;
321 period_guest_sys = he->period_guest_sys;
322 period_guest_us = he->period_guest_us;
323 }
324
325 if (total) {
326 if (color)
327 ret = percent_color_snprintf(s, size,
328 sep ? "%.2f" : " %6.2f%%",
329 (period * 100.0) / total);
330 else
331 ret = scnprintf(s, size, sep ? "%.2f" : " %6.2f%%",
332 (period * 100.0) / total);
333 if (symbol_conf.show_cpu_utilization) {
334 ret += percent_color_snprintf(s + ret, size - ret,
335 sep ? "%.2f" : " %6.2f%%",
336 (period_sys * 100.0) / total);
337 ret += percent_color_snprintf(s + ret, size - ret,
338 sep ? "%.2f" : " %6.2f%%",
339 (period_us * 100.0) / total);
340 if (perf_guest) {
341 ret += percent_color_snprintf(s + ret,
342 size - ret,
343 sep ? "%.2f" : " %6.2f%%",
344 (period_guest_sys * 100.0) /
345 total);
346 ret += percent_color_snprintf(s + ret,
347 size - ret,
348 sep ? "%.2f" : " %6.2f%%",
349 (period_guest_us * 100.0) /
350 total);
351 }
352 }
353 } else
354 ret = scnprintf(s, size, sep ? "%" PRIu64 : "%12" PRIu64 " ", period);
355
356 if (symbol_conf.show_nr_samples) {
357 if (sep)
358 ret += scnprintf(s + ret, size - ret, "%c%" PRIu64, *sep, nr_events);
359 else
360 ret += scnprintf(s + ret, size - ret, "%11" PRIu64, nr_events);
361 }
362
363 if (symbol_conf.show_total_period) {
364 if (sep)
365 ret += scnprintf(s + ret, size - ret, "%c%" PRIu64, *sep, period);
366 else
367 ret += scnprintf(s + ret, size - ret, " %12" PRIu64, period);
368 }
369
370 if (pair_hists) {
371 char bf[32];
372 double old_percent = 0, new_percent = 0, diff;
373
374 if (total > 0)
375 old_percent = (period * 100.0) / total;
376 if (total_period > 0)
377 new_percent = (he->period * 100.0) / total_period;
378
379 diff = new_percent - old_percent;
380
381 if (fabs(diff) >= 0.01)
382 scnprintf(bf, sizeof(bf), "%+4.2F%%", diff);
383 else
384 scnprintf(bf, sizeof(bf), " ");
385
386 if (sep)
387 ret += scnprintf(s + ret, size - ret, "%c%s", *sep, bf);
388 else
389 ret += scnprintf(s + ret, size - ret, "%11.11s", bf);
390
391 if (show_displacement) {
392 if (displacement)
393 scnprintf(bf, sizeof(bf), "%+4ld", displacement);
394 else
395 scnprintf(bf, sizeof(bf), " ");
396
397 if (sep)
398 ret += scnprintf(s + ret, size - ret, "%c%s", *sep, bf);
399 else
400 ret += scnprintf(s + ret, size - ret, "%6.6s", bf);
401 }
402 }
403
404 return ret;
405}
406
000078bc
NK
407int hist_entry__sort_snprintf(struct hist_entry *he, char *s, size_t size,
408 struct hists *hists)
7ccf4f90
NK
409{
410 const char *sep = symbol_conf.field_sep;
411 struct sort_entry *se;
412 int ret = 0;
413
414 list_for_each_entry(se, &hist_entry__sort_list, list) {
415 if (se->elide)
416 continue;
417
418 ret += scnprintf(s + ret, size - ret, "%s", sep ?: " ");
419 ret += se->se_snprintf(he, s + ret, size - ret,
420 hists__col_len(hists, se->se_width_idx));
421 }
422
423 return ret;
424}
425
000078bc
NK
426static size_t hist_entry__callchain_fprintf(struct hist_entry *he,
427 struct hists *hists,
428 u64 total_period, FILE *fp)
429{
430 int left_margin = 0;
431
432 if (sort__first_dimension == SORT_COMM) {
433 struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
434 typeof(*se), list);
435 left_margin = hists__col_len(hists, se->se_width_idx);
436 left_margin -= thread__comm_len(he->thread);
437 }
438
439 return hist_entry_callchain__fprintf(he, total_period, left_margin, fp);
440}
441
7ccf4f90
NK
442static int hist_entry__fprintf(struct hist_entry *he, size_t size,
443 struct hists *hists, struct hists *pair_hists,
444 bool show_displacement, long displacement,
445 u64 total_period, FILE *fp)
446{
447 char bf[512];
448 int ret;
449
450 if (size == 0 || size > sizeof(bf))
451 size = sizeof(bf);
452
000078bc
NK
453 ret = hist_entry__period_snprintf(he, bf, size, pair_hists,
454 show_displacement, displacement,
455 true, total_period);
456 hist_entry__sort_snprintf(he, bf + ret, size - ret, hists);
7ccf4f90 457
000078bc 458 ret = fprintf(fp, "%s\n", bf);
7ccf4f90 459
000078bc
NK
460 if (symbol_conf.use_callchain)
461 ret += hist_entry__callchain_fprintf(he, hists,
462 total_period, fp);
7ccf4f90 463
000078bc 464 return ret;
7ccf4f90
NK
465}
466
467size_t hists__fprintf(struct hists *hists, struct hists *pair,
468 bool show_displacement, bool show_header, int max_rows,
469 int max_cols, FILE *fp)
470{
471 struct sort_entry *se;
472 struct rb_node *nd;
473 size_t ret = 0;
474 u64 total_period;
475 unsigned long position = 1;
476 long displacement = 0;
477 unsigned int width;
478 const char *sep = symbol_conf.field_sep;
479 const char *col_width = symbol_conf.col_width_list_str;
480 int nr_rows = 0;
481
482 init_rem_hits();
483
484 if (!show_header)
485 goto print_entries;
486
487 fprintf(fp, "# %s", pair ? "Baseline" : "Overhead");
488
489 if (symbol_conf.show_cpu_utilization) {
490 if (sep) {
491 ret += fprintf(fp, "%csys", *sep);
492 ret += fprintf(fp, "%cus", *sep);
493 if (perf_guest) {
494 ret += fprintf(fp, "%cguest sys", *sep);
495 ret += fprintf(fp, "%cguest us", *sep);
496 }
497 } else {
498 ret += fprintf(fp, " sys ");
499 ret += fprintf(fp, " us ");
500 if (perf_guest) {
501 ret += fprintf(fp, " guest sys ");
502 ret += fprintf(fp, " guest us ");
503 }
504 }
505 }
506
507 if (symbol_conf.show_nr_samples) {
508 if (sep)
509 fprintf(fp, "%cSamples", *sep);
510 else
511 fputs(" Samples ", fp);
512 }
513
514 if (symbol_conf.show_total_period) {
515 if (sep)
516 ret += fprintf(fp, "%cPeriod", *sep);
517 else
518 ret += fprintf(fp, " Period ");
519 }
520
521 if (pair) {
522 if (sep)
523 ret += fprintf(fp, "%cDelta", *sep);
524 else
525 ret += fprintf(fp, " Delta ");
526
527 if (show_displacement) {
528 if (sep)
529 ret += fprintf(fp, "%cDisplacement", *sep);
530 else
531 ret += fprintf(fp, " Displ");
532 }
533 }
534
535 list_for_each_entry(se, &hist_entry__sort_list, list) {
536 if (se->elide)
537 continue;
538 if (sep) {
539 fprintf(fp, "%c%s", *sep, se->se_header);
540 continue;
541 }
542 width = strlen(se->se_header);
543 if (symbol_conf.col_width_list_str) {
544 if (col_width) {
545 hists__set_col_len(hists, se->se_width_idx,
546 atoi(col_width));
547 col_width = strchr(col_width, ',');
548 if (col_width)
549 ++col_width;
550 }
551 }
552 if (!hists__new_col_len(hists, se->se_width_idx, width))
553 width = hists__col_len(hists, se->se_width_idx);
554 fprintf(fp, " %*s", width, se->se_header);
555 }
556
557 fprintf(fp, "\n");
558 if (max_rows && ++nr_rows >= max_rows)
559 goto out;
560
561 if (sep)
562 goto print_entries;
563
564 fprintf(fp, "# ........");
565 if (symbol_conf.show_cpu_utilization)
566 fprintf(fp, " ....... .......");
567 if (symbol_conf.show_nr_samples)
568 fprintf(fp, " ..........");
569 if (symbol_conf.show_total_period)
570 fprintf(fp, " ............");
571 if (pair) {
572 fprintf(fp, " ..........");
573 if (show_displacement)
574 fprintf(fp, " .....");
575 }
576 list_for_each_entry(se, &hist_entry__sort_list, list) {
577 unsigned int i;
578
579 if (se->elide)
580 continue;
581
582 fprintf(fp, " ");
583 width = hists__col_len(hists, se->se_width_idx);
584 if (width == 0)
585 width = strlen(se->se_header);
586 for (i = 0; i < width; i++)
587 fprintf(fp, ".");
588 }
589
590 fprintf(fp, "\n");
591 if (max_rows && ++nr_rows >= max_rows)
592 goto out;
593
594 fprintf(fp, "#\n");
595 if (max_rows && ++nr_rows >= max_rows)
596 goto out;
597
598print_entries:
599 total_period = hists->stats.total_period;
600
601 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
602 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
603
604 if (h->filtered)
605 continue;
606
607 if (show_displacement) {
608 if (h->pair != NULL)
609 displacement = ((long)h->pair->position -
610 (long)position);
611 else
612 displacement = 0;
613 ++position;
614 }
615 ret += hist_entry__fprintf(h, max_cols, hists, pair, show_displacement,
616 displacement, total_period, fp);
617
7ccf4f90
NK
618 if (max_rows && ++nr_rows >= max_rows)
619 goto out;
620
621 if (h->ms.map == NULL && verbose > 1) {
622 __map_groups__fprintf_maps(&h->thread->mg,
623 MAP__FUNCTION, verbose, fp);
624 fprintf(fp, "%.10s end\n", graph_dotted_line);
625 }
626 }
627out:
628 free(rem_sq_bracket);
629
630 return ret;
631}
632
633size_t hists__fprintf_nr_events(struct hists *hists, FILE *fp)
634{
635 int i;
636 size_t ret = 0;
637
638 for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
639 const char *name;
640
641 if (hists->stats.nr_events[i] == 0)
642 continue;
643
644 name = perf_event__name(i);
645 if (!strcmp(name, "UNKNOWN"))
646 continue;
647
648 ret += fprintf(fp, "%16s events: %10d\n", name,
649 hists->stats.nr_events[i]);
650 }
651
652 return ret;
653}