blob: 9f57991025a90fd2648f6d54832dedd9a4c8ab10 [file] [log] [blame]
Namhyung Kim7ccf4f92012-08-20 13:52:05 +09001#include <stdio.h>
Namhyung Kim7ccf4f92012-08-20 13:52:05 +09002
3#include "../../util/util.h"
4#include "../../util/hist.h"
5#include "../../util/sort.h"
Namhyung Kim5b9e2142013-01-22 18:09:37 +09006#include "../../util/evsel.h"
Namhyung Kim7ccf4f92012-08-20 13:52:05 +09007
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 */
Namhyung Kimcfaa1542014-05-19 14:19:30 +0900186 if (!i++ && field_order == NULL &&
187 sort_order && !prefixcmp(sort_order, "sym"))
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900188 continue;
189 if (!printed) {
190 ret += callchain__fprintf_left_margin(fp, left_margin);
191 ret += fprintf(fp, "|\n");
192 ret += callchain__fprintf_left_margin(fp, left_margin);
193 ret += fprintf(fp, "---");
194 left_margin += 3;
195 printed = true;
196 } else
197 ret += callchain__fprintf_left_margin(fp, left_margin);
198
199 if (chain->ms.sym)
200 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
201 else
202 ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
203
204 if (++entries_printed == callchain_param.print_limit)
205 break;
206 }
207 root = &cnode->rb_root;
208 }
209
210 ret += __callchain__fprintf_graph(fp, root, total_samples,
211 1, 1, left_margin);
212 ret += fprintf(fp, "\n");
213
214 return ret;
215}
216
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300217static size_t __callchain__fprintf_flat(FILE *fp, struct callchain_node *node,
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900218 u64 total_samples)
219{
220 struct callchain_list *chain;
221 size_t ret = 0;
222
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300223 if (!node)
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900224 return 0;
225
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300226 ret += __callchain__fprintf_flat(fp, node->parent, total_samples);
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900227
228
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300229 list_for_each_entry(chain, &node->val, list) {
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900230 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
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300242static size_t callchain__fprintf_flat(FILE *fp, struct rb_root *tree,
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900243 u64 total_samples)
244{
245 size_t ret = 0;
246 u32 entries_printed = 0;
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900247 struct callchain_node *chain;
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -0300248 struct rb_node *rb_node = rb_first(tree);
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900249
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900250 while (rb_node) {
251 double percent;
252
253 chain = rb_entry(rb_node, struct callchain_node, rb_node);
254 percent = chain->hit * 100.0 / total_samples;
255
256 ret = percent_color_fprintf(fp, " %6.2f%%\n", percent);
257 ret += __callchain__fprintf_flat(fp, chain, total_samples);
258 ret += fprintf(fp, "\n");
259 if (++entries_printed == callchain_param.print_limit)
260 break;
261
262 rb_node = rb_next(rb_node);
263 }
264
265 return ret;
266}
267
268static size_t hist_entry_callchain__fprintf(struct hist_entry *he,
269 u64 total_samples, int left_margin,
270 FILE *fp)
271{
272 switch (callchain_param.mode) {
273 case CHAIN_GRAPH_REL:
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900274 return callchain__fprintf_graph(fp, &he->sorted_chain, he->stat.period,
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900275 left_margin);
276 break;
277 case CHAIN_GRAPH_ABS:
278 return callchain__fprintf_graph(fp, &he->sorted_chain, total_samples,
279 left_margin);
280 break;
281 case CHAIN_FLAT:
282 return callchain__fprintf_flat(fp, &he->sorted_chain, total_samples);
283 break;
284 case CHAIN_NONE:
285 break;
286 default:
287 pr_err("Bad callchain mode\n");
288 }
289
290 return 0;
291}
292
Namhyung Kim000078b2012-08-20 13:52:06 +0900293static size_t hist_entry__callchain_fprintf(struct hist_entry *he,
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900294 struct hists *hists,
Jiri Olsab5ff71c2012-10-04 21:49:40 +0900295 FILE *fp)
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900296{
297 int left_margin = 0;
Jiri Olsab5ff71c2012-10-04 21:49:40 +0900298 u64 total_period = hists->stats.total_period;
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900299
Namhyung Kimcfaa1542014-05-19 14:19:30 +0900300 if (field_order == NULL && (sort_order == NULL ||
301 !prefixcmp(sort_order, "comm"))) {
302 struct perf_hpp_fmt *fmt;
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900303
Namhyung Kimcfaa1542014-05-19 14:19:30 +0900304 perf_hpp__for_each_format(fmt) {
305 if (!perf_hpp__is_sort_entry(fmt))
306 continue;
307
308 /* must be 'comm' sort entry */
309 left_margin = fmt->width(fmt, NULL, hists_to_evsel(hists));
310 left_margin -= thread__comm_len(he->thread);
311 break;
312 }
313 }
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900314 return hist_entry_callchain__fprintf(he, total_period, left_margin, fp);
315}
316
Namhyung Kim26d8b332014-03-03 16:16:20 +0900317static int hist_entry__snprintf(struct hist_entry *he, struct perf_hpp *hpp)
Jiri Olsabe0e6d12013-02-04 16:33:19 +0100318{
319 const char *sep = symbol_conf.field_sep;
320 struct perf_hpp_fmt *fmt;
321 char *start = hpp->buf;
322 int ret;
323 bool first = true;
324
325 if (symbol_conf.exclude_other && !he->parent)
326 return 0;
327
328 perf_hpp__for_each_format(fmt) {
Namhyung Kime67d49a2014-03-18 13:00:59 +0900329 if (perf_hpp__should_skip(fmt))
330 continue;
331
Jiri Olsabe0e6d12013-02-04 16:33:19 +0100332 /*
333 * If there's no field_sep, we still need
334 * to display initial ' '.
335 */
336 if (!sep || !first) {
337 ret = scnprintf(hpp->buf, hpp->size, "%s", sep ?: " ");
338 advance_hpp(hpp, ret);
339 } else
340 first = false;
341
Jiri Olsa9754c4f2013-10-25 13:24:53 +0200342 if (perf_hpp__use_color() && fmt->color)
Jiri Olsabe0e6d12013-02-04 16:33:19 +0100343 ret = fmt->color(fmt, hpp, he);
344 else
345 ret = fmt->entry(fmt, hpp, he);
346
347 advance_hpp(hpp, ret);
348 }
349
350 return hpp->buf - start;
351}
352
Namhyung Kim000078b2012-08-20 13:52:06 +0900353static int hist_entry__fprintf(struct hist_entry *he, size_t size,
Arnaldo Carvalho de Melo99cf6662013-09-05 15:39:12 -0300354 struct hists *hists,
355 char *bf, size_t bfsz, FILE *fp)
Namhyung Kim000078b2012-08-20 13:52:06 +0900356{
Namhyung Kim000078b2012-08-20 13:52:06 +0900357 int ret;
Namhyung Kimea251d52012-09-03 11:53:06 +0900358 struct perf_hpp hpp = {
359 .buf = bf,
360 .size = size,
Namhyung Kimea251d52012-09-03 11:53:06 +0900361 };
Namhyung Kim000078b2012-08-20 13:52:06 +0900362
Arnaldo Carvalho de Melo99cf6662013-09-05 15:39:12 -0300363 if (size == 0 || size > bfsz)
364 size = hpp.size = bfsz;
Namhyung Kim000078b2012-08-20 13:52:06 +0900365
Namhyung Kim26d8b332014-03-03 16:16:20 +0900366 hist_entry__snprintf(he, &hpp);
Namhyung Kim000078b2012-08-20 13:52:06 +0900367
368 ret = fprintf(fp, "%s\n", bf);
369
370 if (symbol_conf.use_callchain)
Jiri Olsab5ff71c2012-10-04 21:49:40 +0900371 ret += hist_entry__callchain_fprintf(he, hists, fp);
Namhyung Kim000078b2012-08-20 13:52:06 +0900372
373 return ret;
374}
375
Jiri Olsa41724e42012-10-04 21:49:38 +0900376size_t hists__fprintf(struct hists *hists, bool show_header, int max_rows,
Namhyung Kim064f1982013-05-14 11:09:04 +0900377 int max_cols, float min_pcnt, FILE *fp)
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900378{
Jiri Olsa12400052012-10-13 00:06:16 +0200379 struct perf_hpp_fmt *fmt;
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900380 struct rb_node *nd;
381 size_t ret = 0;
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900382 unsigned int width;
383 const char *sep = symbol_conf.field_sep;
Jiri Olsa12400052012-10-13 00:06:16 +0200384 int nr_rows = 0;
Jiri Olsaed279da2012-10-05 16:44:45 +0200385 char bf[96];
Namhyung Kimea251d52012-09-03 11:53:06 +0900386 struct perf_hpp dummy_hpp = {
387 .buf = bf,
388 .size = sizeof(bf),
Namhyung Kimea251d52012-09-03 11:53:06 +0900389 };
Jiri Olsa5395a042012-10-04 21:49:37 +0900390 bool first = true;
Arnaldo Carvalho de Melo99cf6662013-09-05 15:39:12 -0300391 size_t linesz;
392 char *line = NULL;
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900393
394 init_rem_hits();
395
Namhyung Kim678a5002014-03-20 11:18:54 +0900396
397 perf_hpp__for_each_format(fmt)
398 perf_hpp__reset_width(fmt, hists);
Namhyung Kim26d8b332014-03-03 16:16:20 +0900399
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900400 if (!show_header)
401 goto print_entries;
402
Namhyung Kimea251d52012-09-03 11:53:06 +0900403 fprintf(fp, "# ");
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900404
Jiri Olsa12400052012-10-13 00:06:16 +0200405 perf_hpp__for_each_format(fmt) {
Namhyung Kime67d49a2014-03-18 13:00:59 +0900406 if (perf_hpp__should_skip(fmt))
407 continue;
408
Jiri Olsa5395a042012-10-04 21:49:37 +0900409 if (!first)
Namhyung Kimea251d52012-09-03 11:53:06 +0900410 fprintf(fp, "%s", sep ?: " ");
Jiri Olsa5395a042012-10-04 21:49:37 +0900411 else
412 first = false;
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900413
Namhyung Kim94a07932014-03-10 16:43:52 +0900414 fmt->header(fmt, &dummy_hpp, hists_to_evsel(hists));
Namhyung Kimea251d52012-09-03 11:53:06 +0900415 fprintf(fp, "%s", bf);
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900416 }
417
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900418 fprintf(fp, "\n");
419 if (max_rows && ++nr_rows >= max_rows)
420 goto out;
421
422 if (sep)
423 goto print_entries;
424
Jiri Olsa5395a042012-10-04 21:49:37 +0900425 first = true;
426
Namhyung Kimea251d52012-09-03 11:53:06 +0900427 fprintf(fp, "# ");
Namhyung Kimea251d52012-09-03 11:53:06 +0900428
Jiri Olsa12400052012-10-13 00:06:16 +0200429 perf_hpp__for_each_format(fmt) {
430 unsigned int i;
Namhyung Kimea251d52012-09-03 11:53:06 +0900431
Namhyung Kime67d49a2014-03-18 13:00:59 +0900432 if (perf_hpp__should_skip(fmt))
433 continue;
434
Jiri Olsa5395a042012-10-04 21:49:37 +0900435 if (!first)
Namhyung Kimea251d52012-09-03 11:53:06 +0900436 fprintf(fp, "%s", sep ?: " ");
Jiri Olsa5395a042012-10-04 21:49:37 +0900437 else
438 first = false;
Namhyung Kimea251d52012-09-03 11:53:06 +0900439
Namhyung Kim94a07932014-03-10 16:43:52 +0900440 width = fmt->width(fmt, &dummy_hpp, hists_to_evsel(hists));
Namhyung Kimea251d52012-09-03 11:53:06 +0900441 for (i = 0; i < width; i++)
442 fprintf(fp, ".");
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900443 }
Namhyung Kimea251d52012-09-03 11:53:06 +0900444
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900445 fprintf(fp, "\n");
446 if (max_rows && ++nr_rows >= max_rows)
447 goto out;
448
449 fprintf(fp, "#\n");
450 if (max_rows && ++nr_rows >= max_rows)
451 goto out;
452
453print_entries:
Arnaldo Carvalho de Melo99cf6662013-09-05 15:39:12 -0300454 linesz = hists__sort_list_width(hists) + 3 + 1;
Jiri Olsa9754c4f2013-10-25 13:24:53 +0200455 linesz += perf_hpp__color_overhead();
Arnaldo Carvalho de Melo99cf6662013-09-05 15:39:12 -0300456 line = malloc(linesz);
457 if (line == NULL) {
458 ret = -1;
459 goto out;
460 }
461
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900462 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
463 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
Namhyung Kim064f1982013-05-14 11:09:04 +0900464 float percent = h->stat.period * 100.0 /
465 hists->stats.total_period;
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900466
467 if (h->filtered)
468 continue;
469
Namhyung Kim064f1982013-05-14 11:09:04 +0900470 if (percent < min_pcnt)
471 continue;
472
Arnaldo Carvalho de Melo99cf6662013-09-05 15:39:12 -0300473 ret += hist_entry__fprintf(h, max_cols, hists, line, linesz, fp);
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900474
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900475 if (max_rows && ++nr_rows >= max_rows)
Arnaldo Carvalho de Melo99cf6662013-09-05 15:39:12 -0300476 break;
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900477
478 if (h->ms.map == NULL && verbose > 1) {
Arnaldo Carvalho de Melo93d57312014-03-21 17:57:01 -0300479 __map_groups__fprintf_maps(h->thread->mg,
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900480 MAP__FUNCTION, verbose, fp);
481 fprintf(fp, "%.10s end\n", graph_dotted_line);
482 }
483 }
Arnaldo Carvalho de Melo99cf6662013-09-05 15:39:12 -0300484
485 free(line);
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900486out:
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300487 zfree(&rem_sq_bracket);
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900488
489 return ret;
490}
491
Arnaldo Carvalho de Melo52168ee2012-12-18 16:02:17 -0300492size_t events_stats__fprintf(struct events_stats *stats, FILE *fp)
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900493{
494 int i;
495 size_t ret = 0;
496
497 for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
498 const char *name;
499
Arnaldo Carvalho de Melo52168ee2012-12-18 16:02:17 -0300500 if (stats->nr_events[i] == 0)
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900501 continue;
502
503 name = perf_event__name(i);
504 if (!strcmp(name, "UNKNOWN"))
505 continue;
506
507 ret += fprintf(fp, "%16s events: %10d\n", name,
Arnaldo Carvalho de Melo52168ee2012-12-18 16:02:17 -0300508 stats->nr_events[i]);
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900509 }
510
511 return ret;
512}