3 #include "../../util/util.h"
4 #include "../../util/hist.h"
5 #include "../../util/sort.h"
8 static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
11 int ret = fprintf(fp, " ");
13 for (i = 0; i < left_margin; i++)
14 ret += fprintf(fp, " ");
19 static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
23 size_t ret = callchain__fprintf_left_margin(fp, left_margin);
25 for (i = 0; i < depth; i++)
26 if (depth_mask & (1 << i))
27 ret += fprintf(fp, "| ");
29 ret += fprintf(fp, " ");
31 ret += fprintf(fp, "\n");
36 static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain,
37 int depth, int depth_mask, int period,
38 u64 total_samples, u64 hits,
44 ret += callchain__fprintf_left_margin(fp, left_margin);
45 for (i = 0; i < depth; i++) {
46 if (depth_mask & (1 << i))
47 ret += fprintf(fp, "|");
49 ret += fprintf(fp, " ");
50 if (!period && i == depth - 1) {
53 percent = hits * 100.0 / total_samples;
54 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
56 ret += fprintf(fp, "%s", " ");
59 ret += fprintf(fp, "%s\n", chain->ms.sym->name);
61 ret += fprintf(fp, "0x%0" PRIx64 "\n", chain->ip);
66 static struct symbol *rem_sq_bracket;
67 static struct callchain_list rem_hits;
69 static void init_rem_hits(void)
71 rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
72 if (!rem_sq_bracket) {
73 fprintf(stderr, "Not enough memory to display remaining hits\n");
77 strcpy(rem_sq_bracket->name, "[...]");
78 rem_hits.ms.sym = rem_sq_bracket;
81 static size_t __callchain__fprintf_graph(FILE *fp, struct rb_root *root,
82 u64 total_samples, int depth,
83 int depth_mask, int left_margin)
85 struct rb_node *node, *next;
86 struct callchain_node *child;
87 struct callchain_list *chain;
88 int new_depth_mask = depth_mask;
92 uint entries_printed = 0;
94 remaining = total_samples;
96 node = rb_first(root);
101 child = rb_entry(node, struct callchain_node, rb_node);
102 cumul = callchain_cumul_hits(child);
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
112 next = rb_next(node);
113 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
114 new_depth_mask &= ~(1 << (depth - 1));
117 * But we keep the older depth mask for the line separator
118 * to keep the level link until we reach the last child
120 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
123 list_for_each_entry(chain, &child->val, list) {
124 ret += ipchain__fprintf_graph(fp, chain, depth,
131 if (callchain_param.mode == CHAIN_GRAPH_REL)
132 new_total = child->children_hit;
134 new_total = total_samples;
136 ret += __callchain__fprintf_graph(fp, &child->rb_root, new_total,
138 new_depth_mask | (1 << depth),
141 if (++entries_printed == callchain_param.print_limit)
145 if (callchain_param.mode == CHAIN_GRAPH_REL &&
146 remaining && remaining != total_samples) {
151 new_depth_mask &= ~(1 << (depth - 1));
152 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
153 new_depth_mask, 0, total_samples,
154 remaining, left_margin);
160 static size_t callchain__fprintf_graph(FILE *fp, struct rb_root *root,
161 u64 total_samples, int left_margin)
163 struct callchain_node *cnode;
164 struct callchain_list *chain;
165 u32 entries_printed = 0;
166 bool printed = false;
167 struct rb_node *node;
172 * If have one single callchain root, don't bother printing
173 * its percentage (100 % in fractal mode and the same percentage
174 * than the hist in graph mode). This also avoid one level of column.
176 node = rb_first(root);
177 if (node && !rb_next(node)) {
178 cnode = rb_entry(node, struct callchain_node, rb_node);
179 list_for_each_entry(chain, &cnode->val, list) {
181 * If we sort by symbol, the first entry is the same than
182 * the symbol. No need to print it otherwise it appears as
185 if (!i++ && sort__first_dimension == SORT_SYM)
188 ret += callchain__fprintf_left_margin(fp, left_margin);
189 ret += fprintf(fp, "|\n");
190 ret += callchain__fprintf_left_margin(fp, left_margin);
191 ret += fprintf(fp, "---");
195 ret += callchain__fprintf_left_margin(fp, left_margin);
198 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
200 ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
202 if (++entries_printed == callchain_param.print_limit)
205 root = &cnode->rb_root;
208 ret += __callchain__fprintf_graph(fp, root, total_samples,
210 ret += fprintf(fp, "\n");
215 static size_t __callchain__fprintf_flat(FILE *fp,
216 struct callchain_node *self,
219 struct callchain_list *chain;
225 ret += __callchain__fprintf_flat(fp, self->parent, total_samples);
228 list_for_each_entry(chain, &self->val, list) {
229 if (chain->ip >= PERF_CONTEXT_MAX)
232 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
234 ret += fprintf(fp, " %p\n",
235 (void *)(long)chain->ip);
241 static size_t callchain__fprintf_flat(FILE *fp, struct rb_root *self,
245 u32 entries_printed = 0;
246 struct rb_node *rb_node;
247 struct callchain_node *chain;
249 rb_node = rb_first(self);
253 chain = rb_entry(rb_node, struct callchain_node, rb_node);
254 percent = chain->hit * 100.0 / total_samples;
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)
262 rb_node = rb_next(rb_node);
268 static size_t hist_entry_callchain__fprintf(struct hist_entry *he,
269 u64 total_samples, int left_margin,
272 switch (callchain_param.mode) {
273 case CHAIN_GRAPH_REL:
274 return callchain__fprintf_graph(fp, &he->sorted_chain, he->stat.period,
277 case CHAIN_GRAPH_ABS:
278 return callchain__fprintf_graph(fp, &he->sorted_chain, total_samples,
282 return callchain__fprintf_flat(fp, &he->sorted_chain, total_samples);
287 pr_err("Bad callchain mode\n");
293 static size_t hist_entry__callchain_fprintf(struct hist_entry *he,
298 u64 total_period = hists->stats.total_period;
300 if (sort__first_dimension == SORT_COMM) {
301 struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
303 left_margin = hists__col_len(hists, se->se_width_idx);
304 left_margin -= thread__comm_len(he->thread);
307 return hist_entry_callchain__fprintf(he, total_period, left_margin, fp);
310 static int hist_entry__fprintf(struct hist_entry *he, size_t size,
311 struct hists *hists, FILE *fp)
315 struct perf_hpp hpp = {
319 bool color = !symbol_conf.field_sep;
321 if (size == 0 || size > sizeof(bf))
322 size = hpp.size = sizeof(bf);
324 ret = hist_entry__period_snprintf(&hpp, he, color);
325 hist_entry__sort_snprintf(he, bf + ret, size - ret, hists);
327 ret = fprintf(fp, "%s\n", bf);
329 if (symbol_conf.use_callchain)
330 ret += hist_entry__callchain_fprintf(he, hists, fp);
335 size_t hists__fprintf(struct hists *hists, bool show_header, int max_rows,
336 int max_cols, FILE *fp)
338 struct sort_entry *se;
342 const char *sep = symbol_conf.field_sep;
343 const char *col_width = symbol_conf.col_width_list_str;
344 int idx, nr_rows = 0;
346 struct perf_hpp dummy_hpp = {
358 for (idx = 0; idx < PERF_HPP__MAX_INDEX; idx++) {
359 if (!perf_hpp__format[idx].cond)
363 fprintf(fp, "%s", sep ?: " ");
367 perf_hpp__format[idx].header(&dummy_hpp);
368 fprintf(fp, "%s", bf);
371 list_for_each_entry(se, &hist_entry__sort_list, list) {
375 fprintf(fp, "%c%s", *sep, se->se_header);
378 width = strlen(se->se_header);
379 if (symbol_conf.col_width_list_str) {
381 hists__set_col_len(hists, se->se_width_idx,
383 col_width = strchr(col_width, ',');
388 if (!hists__new_col_len(hists, se->se_width_idx, width))
389 width = hists__col_len(hists, se->se_width_idx);
390 fprintf(fp, " %*s", width, se->se_header);
394 if (max_rows && ++nr_rows >= max_rows)
403 for (idx = 0; idx < PERF_HPP__MAX_INDEX; idx++) {
406 if (!perf_hpp__format[idx].cond)
410 fprintf(fp, "%s", sep ?: " ");
414 width = perf_hpp__format[idx].width(&dummy_hpp);
415 for (i = 0; i < width; i++)
419 list_for_each_entry(se, &hist_entry__sort_list, list) {
426 width = hists__col_len(hists, se->se_width_idx);
428 width = strlen(se->se_header);
429 for (i = 0; i < width; i++)
434 if (max_rows && ++nr_rows >= max_rows)
438 if (max_rows && ++nr_rows >= max_rows)
442 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
443 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
448 ret += hist_entry__fprintf(h, max_cols, hists, fp);
450 if (max_rows && ++nr_rows >= max_rows)
453 if (h->ms.map == NULL && verbose > 1) {
454 __map_groups__fprintf_maps(&h->thread->mg,
455 MAP__FUNCTION, verbose, fp);
456 fprintf(fp, "%.10s end\n", graph_dotted_line);
460 free(rem_sq_bracket);
465 size_t hists__fprintf_nr_events(struct hists *hists, FILE *fp)
470 for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
473 if (hists->stats.nr_events[i] == 0)
476 name = perf_event__name(i);
477 if (!strcmp(name, "UNKNOWN"))
480 ret += fprintf(fp, "%16s events: %10d\n", name,
481 hists->stats.nr_events[i]);