perf callchain: Pass parent_samples to __callchain__fprintf_graph()
[linux-2.6-block.git] / tools / perf / ui / stdio / hist.c
CommitLineData
7ccf4f90 1#include <stdio.h>
7ccf4f90
NK
2
3#include "../../util/util.h"
4#include "../../util/hist.h"
5#include "../../util/sort.h"
5b9e2146 6#include "../../util/evsel.h"
7ccf4f90
NK
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
5ab250ca
NK
37static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_node *node,
38 struct callchain_list *chain,
7ccf4f90 39 int depth, int depth_mask, int period,
5ab250ca 40 u64 total_samples, int left_margin)
7ccf4f90
NK
41{
42 int i;
43 size_t ret = 0;
2989ccaa 44 char bf[1024];
7ccf4f90
NK
45
46 ret += callchain__fprintf_left_margin(fp, left_margin);
47 for (i = 0; i < depth; i++) {
48 if (depth_mask & (1 << i))
49 ret += fprintf(fp, "|");
50 else
51 ret += fprintf(fp, " ");
52 if (!period && i == depth - 1) {
5ab250ca
NK
53 ret += fprintf(fp, "--");
54 ret += callchain_node__fprintf_value(node, fp, total_samples);
55 ret += fprintf(fp, "--");
7ccf4f90
NK
56 } else
57 ret += fprintf(fp, "%s", " ");
58 }
2989ccaa
AK
59 fputs(callchain_list__sym_name(chain, bf, sizeof(bf), false), fp);
60 fputc('\n', fp);
7ccf4f90
NK
61 return ret;
62}
63
64static struct symbol *rem_sq_bracket;
65static struct callchain_list rem_hits;
66
67static void init_rem_hits(void)
68{
69 rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
70 if (!rem_sq_bracket) {
71 fprintf(stderr, "Not enough memory to display remaining hits\n");
72 return;
73 }
74
75 strcpy(rem_sq_bracket->name, "[...]");
76 rem_hits.ms.sym = rem_sq_bracket;
77}
78
79static size_t __callchain__fprintf_graph(FILE *fp, struct rb_root *root,
80 u64 total_samples, int depth,
81 int depth_mask, int left_margin)
82{
83 struct rb_node *node, *next;
f2af0086 84 struct callchain_node *child = NULL;
7ccf4f90
NK
85 struct callchain_list *chain;
86 int new_depth_mask = depth_mask;
87 u64 remaining;
88 size_t ret = 0;
89 int i;
90 uint entries_printed = 0;
f2af0086 91 int cumul_count = 0;
7ccf4f90
NK
92
93 remaining = total_samples;
94
95 node = rb_first(root);
96 while (node) {
97 u64 new_total;
98 u64 cumul;
99
100 child = rb_entry(node, struct callchain_node, rb_node);
101 cumul = callchain_cumul_hits(child);
102 remaining -= cumul;
f2af0086 103 cumul_count += callchain_cumul_counts(child);
7ccf4f90
NK
104
105 /*
106 * The depth mask manages the output of pipes that show
107 * the depth. We don't want to keep the pipes of the current
108 * level for the last child of this depth.
109 * Except if we have remaining filtered hits. They will
110 * supersede the last child
111 */
112 next = rb_next(node);
113 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
114 new_depth_mask &= ~(1 << (depth - 1));
115
116 /*
117 * But we keep the older depth mask for the line separator
118 * to keep the level link until we reach the last child
119 */
120 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
121 left_margin);
122 i = 0;
123 list_for_each_entry(chain, &child->val, list) {
5ab250ca 124 ret += ipchain__fprintf_graph(fp, child, chain, depth,
7ccf4f90
NK
125 new_depth_mask, i++,
126 total_samples,
7ccf4f90
NK
127 left_margin);
128 }
129
130 if (callchain_param.mode == CHAIN_GRAPH_REL)
131 new_total = child->children_hit;
132 else
133 new_total = total_samples;
134
135 ret += __callchain__fprintf_graph(fp, &child->rb_root, new_total,
136 depth + 1,
137 new_depth_mask | (1 << depth),
138 left_margin);
139 node = next;
140 if (++entries_printed == callchain_param.print_limit)
141 break;
142 }
143
144 if (callchain_param.mode == CHAIN_GRAPH_REL &&
145 remaining && remaining != total_samples) {
5ab250ca
NK
146 struct callchain_node rem_node = {
147 .hit = remaining,
148 };
7ccf4f90
NK
149
150 if (!rem_sq_bracket)
151 return ret;
152
f2af0086
NK
153 if (callchain_param.value == CCVAL_COUNT && child && child->parent) {
154 rem_node.count = child->parent->children_count - cumul_count;
155 if (rem_node.count <= 0)
156 return ret;
157 }
158
7ccf4f90 159 new_depth_mask &= ~(1 << (depth - 1));
5ab250ca 160 ret += ipchain__fprintf_graph(fp, &rem_node, &rem_hits, depth,
7ccf4f90 161 new_depth_mask, 0, total_samples,
5ab250ca 162 left_margin);
7ccf4f90
NK
163 }
164
165 return ret;
166}
167
168static size_t callchain__fprintf_graph(FILE *fp, struct rb_root *root,
54d27b31
NK
169 u64 total_samples, u64 parent_samples,
170 int left_margin)
7ccf4f90
NK
171{
172 struct callchain_node *cnode;
173 struct callchain_list *chain;
174 u32 entries_printed = 0;
175 bool printed = false;
176 struct rb_node *node;
177 int i = 0;
178 int ret = 0;
2989ccaa 179 char bf[1024];
7ccf4f90
NK
180
181 /*
182 * If have one single callchain root, don't bother printing
183 * its percentage (100 % in fractal mode and the same percentage
184 * than the hist in graph mode). This also avoid one level of column.
185 */
186 node = rb_first(root);
187 if (node && !rb_next(node)) {
188 cnode = rb_entry(node, struct callchain_node, rb_node);
189 list_for_each_entry(chain, &cnode->val, list) {
190 /*
191 * If we sort by symbol, the first entry is the same than
192 * the symbol. No need to print it otherwise it appears as
193 * displayed twice.
194 */
cfaa154b
NK
195 if (!i++ && field_order == NULL &&
196 sort_order && !prefixcmp(sort_order, "sym"))
7ccf4f90
NK
197 continue;
198 if (!printed) {
199 ret += callchain__fprintf_left_margin(fp, left_margin);
200 ret += fprintf(fp, "|\n");
201 ret += callchain__fprintf_left_margin(fp, left_margin);
202 ret += fprintf(fp, "---");
203 left_margin += 3;
204 printed = true;
205 } else
206 ret += callchain__fprintf_left_margin(fp, left_margin);
207
2989ccaa
AK
208 ret += fprintf(fp, "%s\n", callchain_list__sym_name(chain, bf, sizeof(bf),
209 false));
7ccf4f90
NK
210
211 if (++entries_printed == callchain_param.print_limit)
212 break;
213 }
214 root = &cnode->rb_root;
215 }
216
54d27b31
NK
217 if (callchain_param.mode == CHAIN_GRAPH_REL)
218 total_samples = parent_samples;
219
7ccf4f90
NK
220 ret += __callchain__fprintf_graph(fp, root, total_samples,
221 1, 1, left_margin);
222 ret += fprintf(fp, "\n");
223
224 return ret;
225}
226
316c7136 227static size_t __callchain__fprintf_flat(FILE *fp, struct callchain_node *node,
7ccf4f90
NK
228 u64 total_samples)
229{
230 struct callchain_list *chain;
231 size_t ret = 0;
2989ccaa 232 char bf[1024];
7ccf4f90 233
316c7136 234 if (!node)
7ccf4f90
NK
235 return 0;
236
316c7136 237 ret += __callchain__fprintf_flat(fp, node->parent, total_samples);
7ccf4f90
NK
238
239
316c7136 240 list_for_each_entry(chain, &node->val, list) {
7ccf4f90
NK
241 if (chain->ip >= PERF_CONTEXT_MAX)
242 continue;
2989ccaa
AK
243 ret += fprintf(fp, " %s\n", callchain_list__sym_name(chain,
244 bf, sizeof(bf), false));
7ccf4f90
NK
245 }
246
247 return ret;
248}
249
316c7136 250static size_t callchain__fprintf_flat(FILE *fp, struct rb_root *tree,
7ccf4f90
NK
251 u64 total_samples)
252{
253 size_t ret = 0;
254 u32 entries_printed = 0;
7ccf4f90 255 struct callchain_node *chain;
316c7136 256 struct rb_node *rb_node = rb_first(tree);
7ccf4f90 257
7ccf4f90 258 while (rb_node) {
7ccf4f90 259 chain = rb_entry(rb_node, struct callchain_node, rb_node);
7ccf4f90 260
5ab250ca
NK
261 ret += fprintf(fp, " ");
262 ret += callchain_node__fprintf_value(chain, fp, total_samples);
263 ret += fprintf(fp, "\n");
7ccf4f90
NK
264 ret += __callchain__fprintf_flat(fp, chain, total_samples);
265 ret += fprintf(fp, "\n");
266 if (++entries_printed == callchain_param.print_limit)
267 break;
268
269 rb_node = rb_next(rb_node);
270 }
271
272 return ret;
273}
274
26e77924
NK
275static size_t __callchain__fprintf_folded(FILE *fp, struct callchain_node *node)
276{
277 const char *sep = symbol_conf.field_sep ?: ";";
278 struct callchain_list *chain;
279 size_t ret = 0;
280 char bf[1024];
281 bool first;
282
283 if (!node)
284 return 0;
285
286 ret += __callchain__fprintf_folded(fp, node->parent);
287
288 first = (ret == 0);
289 list_for_each_entry(chain, &node->val, list) {
290 if (chain->ip >= PERF_CONTEXT_MAX)
291 continue;
292 ret += fprintf(fp, "%s%s", first ? "" : sep,
293 callchain_list__sym_name(chain,
294 bf, sizeof(bf), false));
295 first = false;
296 }
297
298 return ret;
299}
300
301static size_t callchain__fprintf_folded(FILE *fp, struct rb_root *tree,
302 u64 total_samples)
303{
304 size_t ret = 0;
305 u32 entries_printed = 0;
306 struct callchain_node *chain;
307 struct rb_node *rb_node = rb_first(tree);
308
309 while (rb_node) {
26e77924
NK
310
311 chain = rb_entry(rb_node, struct callchain_node, rb_node);
26e77924 312
5ab250ca
NK
313 ret += callchain_node__fprintf_value(chain, fp, total_samples);
314 ret += fprintf(fp, " ");
26e77924
NK
315 ret += __callchain__fprintf_folded(fp, chain);
316 ret += fprintf(fp, "\n");
317 if (++entries_printed == callchain_param.print_limit)
318 break;
319
320 rb_node = rb_next(rb_node);
321 }
322
323 return ret;
324}
325
7ccf4f90
NK
326static size_t hist_entry_callchain__fprintf(struct hist_entry *he,
327 u64 total_samples, int left_margin,
328 FILE *fp)
329{
54d27b31
NK
330 u64 parent_samples = he->stat.period;
331
332 if (symbol_conf.cumulate_callchain)
333 parent_samples = he->stat_acc->period;
334
7ccf4f90
NK
335 switch (callchain_param.mode) {
336 case CHAIN_GRAPH_REL:
54d27b31
NK
337 return callchain__fprintf_graph(fp, &he->sorted_chain, total_samples,
338 parent_samples, left_margin);
7ccf4f90
NK
339 break;
340 case CHAIN_GRAPH_ABS:
341 return callchain__fprintf_graph(fp, &he->sorted_chain, total_samples,
54d27b31 342 parent_samples, left_margin);
7ccf4f90
NK
343 break;
344 case CHAIN_FLAT:
345 return callchain__fprintf_flat(fp, &he->sorted_chain, total_samples);
346 break;
26e77924
NK
347 case CHAIN_FOLDED:
348 return callchain__fprintf_folded(fp, &he->sorted_chain, total_samples);
349 break;
7ccf4f90
NK
350 case CHAIN_NONE:
351 break;
352 default:
353 pr_err("Bad callchain mode\n");
354 }
355
356 return 0;
357}
358
26d8b338 359static int hist_entry__snprintf(struct hist_entry *he, struct perf_hpp *hpp)
be0e6d10
JO
360{
361 const char *sep = symbol_conf.field_sep;
362 struct perf_hpp_fmt *fmt;
363 char *start = hpp->buf;
364 int ret;
365 bool first = true;
366
367 if (symbol_conf.exclude_other && !he->parent)
368 return 0;
369
370 perf_hpp__for_each_format(fmt) {
361459f1 371 if (perf_hpp__should_skip(fmt, he->hists))
e67d49a7
NK
372 continue;
373
be0e6d10
JO
374 /*
375 * If there's no field_sep, we still need
376 * to display initial ' '.
377 */
378 if (!sep || !first) {
379 ret = scnprintf(hpp->buf, hpp->size, "%s", sep ?: " ");
380 advance_hpp(hpp, ret);
381 } else
382 first = false;
383
9754c4f9 384 if (perf_hpp__use_color() && fmt->color)
be0e6d10
JO
385 ret = fmt->color(fmt, hpp, he);
386 else
387 ret = fmt->entry(fmt, hpp, he);
388
389 advance_hpp(hpp, ret);
390 }
391
392 return hpp->buf - start;
393}
394
7ccf4f90 395static int hist_entry__fprintf(struct hist_entry *he, size_t size,
99cf666c
ACM
396 struct hists *hists,
397 char *bf, size_t bfsz, FILE *fp)
7ccf4f90 398{
7ccf4f90 399 int ret;
ea251d51
NK
400 struct perf_hpp hpp = {
401 .buf = bf,
402 .size = size,
ea251d51 403 };
7e597d32 404 u64 total_period = hists->stats.total_period;
7ccf4f90 405
99cf666c
ACM
406 if (size == 0 || size > bfsz)
407 size = hpp.size = bfsz;
7ccf4f90 408
26d8b338 409 hist_entry__snprintf(he, &hpp);
7ccf4f90 410
000078bc 411 ret = fprintf(fp, "%s\n", bf);
7ccf4f90 412
000078bc 413 if (symbol_conf.use_callchain)
7e597d32 414 ret += hist_entry_callchain__fprintf(he, total_period, 0, fp);
7ccf4f90 415
000078bc 416 return ret;
7ccf4f90
NK
417}
418
41724e4c 419size_t hists__fprintf(struct hists *hists, bool show_header, int max_rows,
064f1981 420 int max_cols, float min_pcnt, FILE *fp)
7ccf4f90 421{
1240005e 422 struct perf_hpp_fmt *fmt;
7ccf4f90
NK
423 struct rb_node *nd;
424 size_t ret = 0;
7ccf4f90
NK
425 unsigned int width;
426 const char *sep = symbol_conf.field_sep;
1240005e 427 int nr_rows = 0;
ed279da2 428 char bf[96];
ea251d51
NK
429 struct perf_hpp dummy_hpp = {
430 .buf = bf,
431 .size = sizeof(bf),
ea251d51 432 };
5395a048 433 bool first = true;
99cf666c
ACM
434 size_t linesz;
435 char *line = NULL;
7ccf4f90
NK
436
437 init_rem_hits();
438
678a500d
NK
439 perf_hpp__for_each_format(fmt)
440 perf_hpp__reset_width(fmt, hists);
26d8b338 441
5b591669
NK
442 if (symbol_conf.col_width_list_str)
443 perf_hpp__set_user_width(symbol_conf.col_width_list_str);
444
26d8b338
NK
445 if (!show_header)
446 goto print_entries;
447
448 fprintf(fp, "# ");
449
450 perf_hpp__for_each_format(fmt) {
361459f1 451 if (perf_hpp__should_skip(fmt, hists))
e67d49a7
NK
452 continue;
453
26d8b338
NK
454 if (!first)
455 fprintf(fp, "%s", sep ?: " ");
456 else
457 first = false;
458
459 fmt->header(fmt, &dummy_hpp, hists_to_evsel(hists));
460 fprintf(fp, "%s", bf);
7ccf4f90
NK
461 }
462
463 fprintf(fp, "\n");
464 if (max_rows && ++nr_rows >= max_rows)
465 goto out;
466
467 if (sep)
468 goto print_entries;
469
5395a048
JO
470 first = true;
471
ea251d51 472 fprintf(fp, "# ");
ea251d51 473
1240005e
JO
474 perf_hpp__for_each_format(fmt) {
475 unsigned int i;
ea251d51 476
361459f1 477 if (perf_hpp__should_skip(fmt, hists))
e67d49a7
NK
478 continue;
479
5395a048 480 if (!first)
ea251d51 481 fprintf(fp, "%s", sep ?: " ");
5395a048
JO
482 else
483 first = false;
ea251d51 484
94a0793d 485 width = fmt->width(fmt, &dummy_hpp, hists_to_evsel(hists));
ea251d51
NK
486 for (i = 0; i < width; i++)
487 fprintf(fp, ".");
7ccf4f90 488 }
ea251d51 489
7ccf4f90
NK
490 fprintf(fp, "\n");
491 if (max_rows && ++nr_rows >= max_rows)
492 goto out;
493
494 fprintf(fp, "#\n");
495 if (max_rows && ++nr_rows >= max_rows)
496 goto out;
497
498print_entries:
99cf666c 499 linesz = hists__sort_list_width(hists) + 3 + 1;
9754c4f9 500 linesz += perf_hpp__color_overhead();
99cf666c
ACM
501 line = malloc(linesz);
502 if (line == NULL) {
503 ret = -1;
504 goto out;
505 }
506
7ccf4f90
NK
507 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
508 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
14135663 509 float percent;
7ccf4f90
NK
510
511 if (h->filtered)
512 continue;
513
14135663 514 percent = hist_entry__get_percent_limit(h);
064f1981
NK
515 if (percent < min_pcnt)
516 continue;
517
99cf666c 518 ret += hist_entry__fprintf(h, max_cols, hists, line, linesz, fp);
7ccf4f90 519
7ccf4f90 520 if (max_rows && ++nr_rows >= max_rows)
99cf666c 521 break;
7ccf4f90
NK
522
523 if (h->ms.map == NULL && verbose > 1) {
93d5731d 524 __map_groups__fprintf_maps(h->thread->mg,
acebd408 525 MAP__FUNCTION, fp);
7ccf4f90
NK
526 fprintf(fp, "%.10s end\n", graph_dotted_line);
527 }
528 }
99cf666c
ACM
529
530 free(line);
7ccf4f90 531out:
74cf249d 532 zfree(&rem_sq_bracket);
7ccf4f90
NK
533
534 return ret;
535}
536
52168eea 537size_t events_stats__fprintf(struct events_stats *stats, FILE *fp)
7ccf4f90
NK
538{
539 int i;
540 size_t ret = 0;
541
542 for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
543 const char *name;
544
52168eea 545 if (stats->nr_events[i] == 0)
7ccf4f90
NK
546 continue;
547
548 name = perf_event__name(i);
549 if (!strcmp(name, "UNKNOWN"))
550 continue;
551
552 ret += fprintf(fp, "%16s events: %10d\n", name,
52168eea 553 stats->nr_events[i]);
7ccf4f90
NK
554 }
555
556 return ret;
557}