1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 27ccf4f90SNamhyung Kim #include <stdio.h> 38e99b6d4SArnaldo Carvalho de Melo #include <linux/string.h> 47ccf4f90SNamhyung Kim 5b10ba7f1SArnaldo Carvalho de Melo #include "../../util/callchain.h" 67ccf4f90SNamhyung Kim #include "../../util/hist.h" 77b644f9aSArnaldo Carvalho de Melo #include "../../util/map.h" 841f30914SArnaldo Carvalho de Melo #include "../../util/map_groups.h" 9daecf9e0SArnaldo Carvalho de Melo #include "../../util/symbol.h" 107ccf4f90SNamhyung Kim #include "../../util/sort.h" 115b9e2146SNamhyung Kim #include "../../util/evsel.h" 12632a5cabSArnaldo Carvalho de Melo #include "../../util/srcline.h" 13a067558eSArnaldo Carvalho de Melo #include "../../util/string2.h" 14e7ff8920SArnaldo Carvalho de Melo #include "../../util/thread.h" 153052ba56SArnaldo Carvalho de Melo #include <linux/ctype.h> 16*7f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h> 177ccf4f90SNamhyung Kim 187ccf4f90SNamhyung Kim static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin) 197ccf4f90SNamhyung Kim { 207ccf4f90SNamhyung Kim int i; 217ccf4f90SNamhyung Kim int ret = fprintf(fp, " "); 227ccf4f90SNamhyung Kim 237ccf4f90SNamhyung Kim for (i = 0; i < left_margin; i++) 247ccf4f90SNamhyung Kim ret += fprintf(fp, " "); 257ccf4f90SNamhyung Kim 267ccf4f90SNamhyung Kim return ret; 277ccf4f90SNamhyung Kim } 287ccf4f90SNamhyung Kim 297ccf4f90SNamhyung Kim static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask, 307ccf4f90SNamhyung Kim int left_margin) 317ccf4f90SNamhyung Kim { 327ccf4f90SNamhyung Kim int i; 337ccf4f90SNamhyung Kim size_t ret = callchain__fprintf_left_margin(fp, left_margin); 347ccf4f90SNamhyung Kim 357ccf4f90SNamhyung Kim for (i = 0; i < depth; i++) 367ccf4f90SNamhyung Kim if (depth_mask & (1 << i)) 377ccf4f90SNamhyung Kim ret += fprintf(fp, "| "); 387ccf4f90SNamhyung Kim else 397ccf4f90SNamhyung Kim ret += fprintf(fp, " "); 407ccf4f90SNamhyung Kim 417ccf4f90SNamhyung Kim ret += fprintf(fp, "\n"); 427ccf4f90SNamhyung Kim 437ccf4f90SNamhyung Kim return ret; 447ccf4f90SNamhyung Kim } 457ccf4f90SNamhyung Kim 465ab250caSNamhyung Kim static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_node *node, 475ab250caSNamhyung Kim struct callchain_list *chain, 487ccf4f90SNamhyung Kim int depth, int depth_mask, int period, 495ab250caSNamhyung Kim u64 total_samples, int left_margin) 507ccf4f90SNamhyung Kim { 517ccf4f90SNamhyung Kim int i; 527ccf4f90SNamhyung Kim size_t ret = 0; 538577ae6bSJin Yao char bf[1024], *alloc_str = NULL; 548577ae6bSJin Yao char buf[64]; 558577ae6bSJin Yao const char *str; 567ccf4f90SNamhyung Kim 577ccf4f90SNamhyung Kim ret += callchain__fprintf_left_margin(fp, left_margin); 587ccf4f90SNamhyung Kim for (i = 0; i < depth; i++) { 597ccf4f90SNamhyung Kim if (depth_mask & (1 << i)) 607ccf4f90SNamhyung Kim ret += fprintf(fp, "|"); 617ccf4f90SNamhyung Kim else 627ccf4f90SNamhyung Kim ret += fprintf(fp, " "); 637ccf4f90SNamhyung Kim if (!period && i == depth - 1) { 645ab250caSNamhyung Kim ret += fprintf(fp, "--"); 655ab250caSNamhyung Kim ret += callchain_node__fprintf_value(node, fp, total_samples); 665ab250caSNamhyung Kim ret += fprintf(fp, "--"); 677ccf4f90SNamhyung Kim } else 687ccf4f90SNamhyung Kim ret += fprintf(fp, "%s", " "); 697ccf4f90SNamhyung Kim } 708577ae6bSJin Yao 718577ae6bSJin Yao str = callchain_list__sym_name(chain, bf, sizeof(bf), false); 728577ae6bSJin Yao 738577ae6bSJin Yao if (symbol_conf.show_branchflag_count) { 74c4ee0625SJin Yao callchain_list_counts__printf_value(chain, NULL, 758577ae6bSJin Yao buf, sizeof(buf)); 768577ae6bSJin Yao 778577ae6bSJin Yao if (asprintf(&alloc_str, "%s%s", str, buf) < 0) 788577ae6bSJin Yao str = "Not enough memory!"; 798577ae6bSJin Yao else 808577ae6bSJin Yao str = alloc_str; 818577ae6bSJin Yao } 828577ae6bSJin Yao 838577ae6bSJin Yao fputs(str, fp); 842989ccaaSAndi Kleen fputc('\n', fp); 858577ae6bSJin Yao free(alloc_str); 860db64dd0SJin Yao 877ccf4f90SNamhyung Kim return ret; 887ccf4f90SNamhyung Kim } 897ccf4f90SNamhyung Kim 907ccf4f90SNamhyung Kim static struct symbol *rem_sq_bracket; 917ccf4f90SNamhyung Kim static struct callchain_list rem_hits; 927ccf4f90SNamhyung Kim 937ccf4f90SNamhyung Kim static void init_rem_hits(void) 947ccf4f90SNamhyung Kim { 957ccf4f90SNamhyung Kim rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6); 967ccf4f90SNamhyung Kim if (!rem_sq_bracket) { 977ccf4f90SNamhyung Kim fprintf(stderr, "Not enough memory to display remaining hits\n"); 987ccf4f90SNamhyung Kim return; 997ccf4f90SNamhyung Kim } 1007ccf4f90SNamhyung Kim 1017ccf4f90SNamhyung Kim strcpy(rem_sq_bracket->name, "[...]"); 1027ccf4f90SNamhyung Kim rem_hits.ms.sym = rem_sq_bracket; 1037ccf4f90SNamhyung Kim } 1047ccf4f90SNamhyung Kim 1057ccf4f90SNamhyung Kim static size_t __callchain__fprintf_graph(FILE *fp, struct rb_root *root, 1067ccf4f90SNamhyung Kim u64 total_samples, int depth, 1077ccf4f90SNamhyung Kim int depth_mask, int left_margin) 1087ccf4f90SNamhyung Kim { 1097ccf4f90SNamhyung Kim struct rb_node *node, *next; 110f2af0086SNamhyung Kim struct callchain_node *child = NULL; 1117ccf4f90SNamhyung Kim struct callchain_list *chain; 1127ccf4f90SNamhyung Kim int new_depth_mask = depth_mask; 1137ccf4f90SNamhyung Kim u64 remaining; 1147ccf4f90SNamhyung Kim size_t ret = 0; 1157ccf4f90SNamhyung Kim int i; 1167ccf4f90SNamhyung Kim uint entries_printed = 0; 117f2af0086SNamhyung Kim int cumul_count = 0; 1187ccf4f90SNamhyung Kim 1197ccf4f90SNamhyung Kim remaining = total_samples; 1207ccf4f90SNamhyung Kim 1217ccf4f90SNamhyung Kim node = rb_first(root); 1227ccf4f90SNamhyung Kim while (node) { 1237ccf4f90SNamhyung Kim u64 new_total; 1247ccf4f90SNamhyung Kim u64 cumul; 1257ccf4f90SNamhyung Kim 1267ccf4f90SNamhyung Kim child = rb_entry(node, struct callchain_node, rb_node); 1277ccf4f90SNamhyung Kim cumul = callchain_cumul_hits(child); 1287ccf4f90SNamhyung Kim remaining -= cumul; 129f2af0086SNamhyung Kim cumul_count += callchain_cumul_counts(child); 1307ccf4f90SNamhyung Kim 1317ccf4f90SNamhyung Kim /* 1327ccf4f90SNamhyung Kim * The depth mask manages the output of pipes that show 1337ccf4f90SNamhyung Kim * the depth. We don't want to keep the pipes of the current 1347ccf4f90SNamhyung Kim * level for the last child of this depth. 1357ccf4f90SNamhyung Kim * Except if we have remaining filtered hits. They will 1367ccf4f90SNamhyung Kim * supersede the last child 1377ccf4f90SNamhyung Kim */ 1387ccf4f90SNamhyung Kim next = rb_next(node); 1397ccf4f90SNamhyung Kim if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining)) 1407ccf4f90SNamhyung Kim new_depth_mask &= ~(1 << (depth - 1)); 1417ccf4f90SNamhyung Kim 1427ccf4f90SNamhyung Kim /* 1437ccf4f90SNamhyung Kim * But we keep the older depth mask for the line separator 1447ccf4f90SNamhyung Kim * to keep the level link until we reach the last child 1457ccf4f90SNamhyung Kim */ 1467ccf4f90SNamhyung Kim ret += ipchain__fprintf_graph_line(fp, depth, depth_mask, 1477ccf4f90SNamhyung Kim left_margin); 1487ccf4f90SNamhyung Kim i = 0; 1497ccf4f90SNamhyung Kim list_for_each_entry(chain, &child->val, list) { 1505ab250caSNamhyung Kim ret += ipchain__fprintf_graph(fp, child, chain, depth, 1517ccf4f90SNamhyung Kim new_depth_mask, i++, 1527ccf4f90SNamhyung Kim total_samples, 1537ccf4f90SNamhyung Kim left_margin); 1547ccf4f90SNamhyung Kim } 1557ccf4f90SNamhyung Kim 1567ccf4f90SNamhyung Kim if (callchain_param.mode == CHAIN_GRAPH_REL) 1577ccf4f90SNamhyung Kim new_total = child->children_hit; 1587ccf4f90SNamhyung Kim else 1597ccf4f90SNamhyung Kim new_total = total_samples; 1607ccf4f90SNamhyung Kim 1617ccf4f90SNamhyung Kim ret += __callchain__fprintf_graph(fp, &child->rb_root, new_total, 1627ccf4f90SNamhyung Kim depth + 1, 1637ccf4f90SNamhyung Kim new_depth_mask | (1 << depth), 1647ccf4f90SNamhyung Kim left_margin); 1657ccf4f90SNamhyung Kim node = next; 1667ccf4f90SNamhyung Kim if (++entries_printed == callchain_param.print_limit) 1677ccf4f90SNamhyung Kim break; 1687ccf4f90SNamhyung Kim } 1697ccf4f90SNamhyung Kim 1707ccf4f90SNamhyung Kim if (callchain_param.mode == CHAIN_GRAPH_REL && 1717ccf4f90SNamhyung Kim remaining && remaining != total_samples) { 1725ab250caSNamhyung Kim struct callchain_node rem_node = { 1735ab250caSNamhyung Kim .hit = remaining, 1745ab250caSNamhyung Kim }; 1757ccf4f90SNamhyung Kim 1767ccf4f90SNamhyung Kim if (!rem_sq_bracket) 1777ccf4f90SNamhyung Kim return ret; 1787ccf4f90SNamhyung Kim 179f2af0086SNamhyung Kim if (callchain_param.value == CCVAL_COUNT && child && child->parent) { 180f2af0086SNamhyung Kim rem_node.count = child->parent->children_count - cumul_count; 181f2af0086SNamhyung Kim if (rem_node.count <= 0) 182f2af0086SNamhyung Kim return ret; 183f2af0086SNamhyung Kim } 184f2af0086SNamhyung Kim 1857ccf4f90SNamhyung Kim new_depth_mask &= ~(1 << (depth - 1)); 1865ab250caSNamhyung Kim ret += ipchain__fprintf_graph(fp, &rem_node, &rem_hits, depth, 1877ccf4f90SNamhyung Kim new_depth_mask, 0, total_samples, 1885ab250caSNamhyung Kim left_margin); 1897ccf4f90SNamhyung Kim } 1907ccf4f90SNamhyung Kim 1917ccf4f90SNamhyung Kim return ret; 1927ccf4f90SNamhyung Kim } 1937ccf4f90SNamhyung Kim 1947ed5d6e2SNamhyung Kim /* 1957ed5d6e2SNamhyung Kim * If have one single callchain root, don't bother printing 1967ed5d6e2SNamhyung Kim * its percentage (100 % in fractal mode and the same percentage 1977ed5d6e2SNamhyung Kim * than the hist in graph mode). This also avoid one level of column. 1987ed5d6e2SNamhyung Kim * 1997ed5d6e2SNamhyung Kim * However when percent-limit applied, it's possible that single callchain 2007ed5d6e2SNamhyung Kim * node have different (non-100% in fractal mode) percentage. 2017ed5d6e2SNamhyung Kim */ 2027ed5d6e2SNamhyung Kim static bool need_percent_display(struct rb_node *node, u64 parent_samples) 2037ed5d6e2SNamhyung Kim { 2047ed5d6e2SNamhyung Kim struct callchain_node *cnode; 2057ed5d6e2SNamhyung Kim 2067ed5d6e2SNamhyung Kim if (rb_next(node)) 2077ed5d6e2SNamhyung Kim return true; 2087ed5d6e2SNamhyung Kim 2097ed5d6e2SNamhyung Kim cnode = rb_entry(node, struct callchain_node, rb_node); 2107ed5d6e2SNamhyung Kim return callchain_cumul_hits(cnode) != parent_samples; 2117ed5d6e2SNamhyung Kim } 2127ed5d6e2SNamhyung Kim 2137ccf4f90SNamhyung Kim static size_t callchain__fprintf_graph(FILE *fp, struct rb_root *root, 21454d27b31SNamhyung Kim u64 total_samples, u64 parent_samples, 21554d27b31SNamhyung Kim int left_margin) 2167ccf4f90SNamhyung Kim { 2177ccf4f90SNamhyung Kim struct callchain_node *cnode; 2187ccf4f90SNamhyung Kim struct callchain_list *chain; 2197ccf4f90SNamhyung Kim u32 entries_printed = 0; 2207ccf4f90SNamhyung Kim bool printed = false; 2217ccf4f90SNamhyung Kim struct rb_node *node; 2227ccf4f90SNamhyung Kim int i = 0; 2237ccf4f90SNamhyung Kim int ret = 0; 2242989ccaaSAndi Kleen char bf[1024]; 2257ccf4f90SNamhyung Kim 2267ccf4f90SNamhyung Kim node = rb_first(root); 2277ed5d6e2SNamhyung Kim if (node && !need_percent_display(node, parent_samples)) { 2287ccf4f90SNamhyung Kim cnode = rb_entry(node, struct callchain_node, rb_node); 2297ccf4f90SNamhyung Kim list_for_each_entry(chain, &cnode->val, list) { 2307ccf4f90SNamhyung Kim /* 2317ccf4f90SNamhyung Kim * If we sort by symbol, the first entry is the same than 2327ccf4f90SNamhyung Kim * the symbol. No need to print it otherwise it appears as 2337ccf4f90SNamhyung Kim * displayed twice. 2347ccf4f90SNamhyung Kim */ 235cfaa154bSNamhyung Kim if (!i++ && field_order == NULL && 2368e99b6d4SArnaldo Carvalho de Melo sort_order && strstarts(sort_order, "sym")) 2377ccf4f90SNamhyung Kim continue; 2380db64dd0SJin Yao 2397ccf4f90SNamhyung Kim if (!printed) { 2407ccf4f90SNamhyung Kim ret += callchain__fprintf_left_margin(fp, left_margin); 2417ccf4f90SNamhyung Kim ret += fprintf(fp, "|\n"); 2427ccf4f90SNamhyung Kim ret += callchain__fprintf_left_margin(fp, left_margin); 2437ccf4f90SNamhyung Kim ret += fprintf(fp, "---"); 2447ccf4f90SNamhyung Kim left_margin += 3; 2457ccf4f90SNamhyung Kim printed = true; 2467ccf4f90SNamhyung Kim } else 2477ccf4f90SNamhyung Kim ret += callchain__fprintf_left_margin(fp, left_margin); 2487ccf4f90SNamhyung Kim 2498577ae6bSJin Yao ret += fprintf(fp, "%s", 2508577ae6bSJin Yao callchain_list__sym_name(chain, bf, 2518577ae6bSJin Yao sizeof(bf), 2522989ccaaSAndi Kleen false)); 2537ccf4f90SNamhyung Kim 2548577ae6bSJin Yao if (symbol_conf.show_branchflag_count) 2558577ae6bSJin Yao ret += callchain_list_counts__printf_value( 256c4ee0625SJin Yao chain, fp, NULL, 0); 2578577ae6bSJin Yao ret += fprintf(fp, "\n"); 2588577ae6bSJin Yao 2597ccf4f90SNamhyung Kim if (++entries_printed == callchain_param.print_limit) 2607ccf4f90SNamhyung Kim break; 2617ccf4f90SNamhyung Kim } 2627ccf4f90SNamhyung Kim root = &cnode->rb_root; 2637ccf4f90SNamhyung Kim } 2647ccf4f90SNamhyung Kim 26554d27b31SNamhyung Kim if (callchain_param.mode == CHAIN_GRAPH_REL) 26654d27b31SNamhyung Kim total_samples = parent_samples; 26754d27b31SNamhyung Kim 2687ccf4f90SNamhyung Kim ret += __callchain__fprintf_graph(fp, root, total_samples, 2697ccf4f90SNamhyung Kim 1, 1, left_margin); 2703848c23bSNamhyung Kim if (ret) { 2713848c23bSNamhyung Kim /* do not add a blank line if it printed nothing */ 2727ccf4f90SNamhyung Kim ret += fprintf(fp, "\n"); 2733848c23bSNamhyung Kim } 2747ccf4f90SNamhyung Kim 2757ccf4f90SNamhyung Kim return ret; 2767ccf4f90SNamhyung Kim } 2777ccf4f90SNamhyung Kim 278316c7136SArnaldo Carvalho de Melo static size_t __callchain__fprintf_flat(FILE *fp, struct callchain_node *node, 2797ccf4f90SNamhyung Kim u64 total_samples) 2807ccf4f90SNamhyung Kim { 2817ccf4f90SNamhyung Kim struct callchain_list *chain; 2827ccf4f90SNamhyung Kim size_t ret = 0; 2832989ccaaSAndi Kleen char bf[1024]; 2847ccf4f90SNamhyung Kim 285316c7136SArnaldo Carvalho de Melo if (!node) 2867ccf4f90SNamhyung Kim return 0; 2877ccf4f90SNamhyung Kim 288316c7136SArnaldo Carvalho de Melo ret += __callchain__fprintf_flat(fp, node->parent, total_samples); 2897ccf4f90SNamhyung Kim 2907ccf4f90SNamhyung Kim 291316c7136SArnaldo Carvalho de Melo list_for_each_entry(chain, &node->val, list) { 2927ccf4f90SNamhyung Kim if (chain->ip >= PERF_CONTEXT_MAX) 2937ccf4f90SNamhyung Kim continue; 2942989ccaaSAndi Kleen ret += fprintf(fp, " %s\n", callchain_list__sym_name(chain, 2952989ccaaSAndi Kleen bf, sizeof(bf), false)); 2967ccf4f90SNamhyung Kim } 2977ccf4f90SNamhyung Kim 2987ccf4f90SNamhyung Kim return ret; 2997ccf4f90SNamhyung Kim } 3007ccf4f90SNamhyung Kim 301316c7136SArnaldo Carvalho de Melo static size_t callchain__fprintf_flat(FILE *fp, struct rb_root *tree, 3027ccf4f90SNamhyung Kim u64 total_samples) 3037ccf4f90SNamhyung Kim { 3047ccf4f90SNamhyung Kim size_t ret = 0; 3057ccf4f90SNamhyung Kim u32 entries_printed = 0; 3067ccf4f90SNamhyung Kim struct callchain_node *chain; 307316c7136SArnaldo Carvalho de Melo struct rb_node *rb_node = rb_first(tree); 3087ccf4f90SNamhyung Kim 3097ccf4f90SNamhyung Kim while (rb_node) { 3107ccf4f90SNamhyung Kim chain = rb_entry(rb_node, struct callchain_node, rb_node); 3117ccf4f90SNamhyung Kim 3125ab250caSNamhyung Kim ret += fprintf(fp, " "); 3135ab250caSNamhyung Kim ret += callchain_node__fprintf_value(chain, fp, total_samples); 3145ab250caSNamhyung Kim ret += fprintf(fp, "\n"); 3157ccf4f90SNamhyung Kim ret += __callchain__fprintf_flat(fp, chain, total_samples); 3167ccf4f90SNamhyung Kim ret += fprintf(fp, "\n"); 3177ccf4f90SNamhyung Kim if (++entries_printed == callchain_param.print_limit) 3187ccf4f90SNamhyung Kim break; 3197ccf4f90SNamhyung Kim 3207ccf4f90SNamhyung Kim rb_node = rb_next(rb_node); 3217ccf4f90SNamhyung Kim } 3227ccf4f90SNamhyung Kim 3237ccf4f90SNamhyung Kim return ret; 3247ccf4f90SNamhyung Kim } 3257ccf4f90SNamhyung Kim 32626e77924SNamhyung Kim static size_t __callchain__fprintf_folded(FILE *fp, struct callchain_node *node) 32726e77924SNamhyung Kim { 32826e77924SNamhyung Kim const char *sep = symbol_conf.field_sep ?: ";"; 32926e77924SNamhyung Kim struct callchain_list *chain; 33026e77924SNamhyung Kim size_t ret = 0; 33126e77924SNamhyung Kim char bf[1024]; 33226e77924SNamhyung Kim bool first; 33326e77924SNamhyung Kim 33426e77924SNamhyung Kim if (!node) 33526e77924SNamhyung Kim return 0; 33626e77924SNamhyung Kim 33726e77924SNamhyung Kim ret += __callchain__fprintf_folded(fp, node->parent); 33826e77924SNamhyung Kim 33926e77924SNamhyung Kim first = (ret == 0); 34026e77924SNamhyung Kim list_for_each_entry(chain, &node->val, list) { 34126e77924SNamhyung Kim if (chain->ip >= PERF_CONTEXT_MAX) 34226e77924SNamhyung Kim continue; 34326e77924SNamhyung Kim ret += fprintf(fp, "%s%s", first ? "" : sep, 34426e77924SNamhyung Kim callchain_list__sym_name(chain, 34526e77924SNamhyung Kim bf, sizeof(bf), false)); 34626e77924SNamhyung Kim first = false; 34726e77924SNamhyung Kim } 34826e77924SNamhyung Kim 34926e77924SNamhyung Kim return ret; 35026e77924SNamhyung Kim } 35126e77924SNamhyung Kim 35226e77924SNamhyung Kim static size_t callchain__fprintf_folded(FILE *fp, struct rb_root *tree, 35326e77924SNamhyung Kim u64 total_samples) 35426e77924SNamhyung Kim { 35526e77924SNamhyung Kim size_t ret = 0; 35626e77924SNamhyung Kim u32 entries_printed = 0; 35726e77924SNamhyung Kim struct callchain_node *chain; 35826e77924SNamhyung Kim struct rb_node *rb_node = rb_first(tree); 35926e77924SNamhyung Kim 36026e77924SNamhyung Kim while (rb_node) { 36126e77924SNamhyung Kim 36226e77924SNamhyung Kim chain = rb_entry(rb_node, struct callchain_node, rb_node); 36326e77924SNamhyung Kim 3645ab250caSNamhyung Kim ret += callchain_node__fprintf_value(chain, fp, total_samples); 3655ab250caSNamhyung Kim ret += fprintf(fp, " "); 36626e77924SNamhyung Kim ret += __callchain__fprintf_folded(fp, chain); 36726e77924SNamhyung Kim ret += fprintf(fp, "\n"); 36826e77924SNamhyung Kim if (++entries_printed == callchain_param.print_limit) 36926e77924SNamhyung Kim break; 37026e77924SNamhyung Kim 37126e77924SNamhyung Kim rb_node = rb_next(rb_node); 37226e77924SNamhyung Kim } 37326e77924SNamhyung Kim 37426e77924SNamhyung Kim return ret; 37526e77924SNamhyung Kim } 37626e77924SNamhyung Kim 3777ccf4f90SNamhyung Kim static size_t hist_entry_callchain__fprintf(struct hist_entry *he, 3787ccf4f90SNamhyung Kim u64 total_samples, int left_margin, 3797ccf4f90SNamhyung Kim FILE *fp) 3807ccf4f90SNamhyung Kim { 38154d27b31SNamhyung Kim u64 parent_samples = he->stat.period; 38254d27b31SNamhyung Kim 38354d27b31SNamhyung Kim if (symbol_conf.cumulate_callchain) 38454d27b31SNamhyung Kim parent_samples = he->stat_acc->period; 38554d27b31SNamhyung Kim 3867ccf4f90SNamhyung Kim switch (callchain_param.mode) { 3877ccf4f90SNamhyung Kim case CHAIN_GRAPH_REL: 38854d27b31SNamhyung Kim return callchain__fprintf_graph(fp, &he->sorted_chain, total_samples, 38954d27b31SNamhyung Kim parent_samples, left_margin); 3907ccf4f90SNamhyung Kim break; 3917ccf4f90SNamhyung Kim case CHAIN_GRAPH_ABS: 3927ccf4f90SNamhyung Kim return callchain__fprintf_graph(fp, &he->sorted_chain, total_samples, 39354d27b31SNamhyung Kim parent_samples, left_margin); 3947ccf4f90SNamhyung Kim break; 3957ccf4f90SNamhyung Kim case CHAIN_FLAT: 3967ccf4f90SNamhyung Kim return callchain__fprintf_flat(fp, &he->sorted_chain, total_samples); 3977ccf4f90SNamhyung Kim break; 39826e77924SNamhyung Kim case CHAIN_FOLDED: 39926e77924SNamhyung Kim return callchain__fprintf_folded(fp, &he->sorted_chain, total_samples); 40026e77924SNamhyung Kim break; 4017ccf4f90SNamhyung Kim case CHAIN_NONE: 4027ccf4f90SNamhyung Kim break; 4037ccf4f90SNamhyung Kim default: 4047ccf4f90SNamhyung Kim pr_err("Bad callchain mode\n"); 4057ccf4f90SNamhyung Kim } 4067ccf4f90SNamhyung Kim 4077ccf4f90SNamhyung Kim return 0; 4087ccf4f90SNamhyung Kim } 4097ccf4f90SNamhyung Kim 410bd28d0c5SJiri Olsa int __hist_entry__snprintf(struct hist_entry *he, struct perf_hpp *hpp, 4119da44db1SJiri Olsa struct perf_hpp_list *hpp_list) 412be0e6d10SJiri Olsa { 413be0e6d10SJiri Olsa const char *sep = symbol_conf.field_sep; 414be0e6d10SJiri Olsa struct perf_hpp_fmt *fmt; 415be0e6d10SJiri Olsa char *start = hpp->buf; 416be0e6d10SJiri Olsa int ret; 417be0e6d10SJiri Olsa bool first = true; 418be0e6d10SJiri Olsa 419be0e6d10SJiri Olsa if (symbol_conf.exclude_other && !he->parent) 420be0e6d10SJiri Olsa return 0; 421be0e6d10SJiri Olsa 4229da44db1SJiri Olsa perf_hpp_list__for_each_format(hpp_list, fmt) { 423361459f1SNamhyung Kim if (perf_hpp__should_skip(fmt, he->hists)) 424e67d49a7SNamhyung Kim continue; 425e67d49a7SNamhyung Kim 426be0e6d10SJiri Olsa /* 427be0e6d10SJiri Olsa * If there's no field_sep, we still need 428be0e6d10SJiri Olsa * to display initial ' '. 429be0e6d10SJiri Olsa */ 430be0e6d10SJiri Olsa if (!sep || !first) { 431be0e6d10SJiri Olsa ret = scnprintf(hpp->buf, hpp->size, "%s", sep ?: " "); 432be0e6d10SJiri Olsa advance_hpp(hpp, ret); 433be0e6d10SJiri Olsa } else 434be0e6d10SJiri Olsa first = false; 435be0e6d10SJiri Olsa 4369754c4f9SJiri Olsa if (perf_hpp__use_color() && fmt->color) 437be0e6d10SJiri Olsa ret = fmt->color(fmt, hpp, he); 438be0e6d10SJiri Olsa else 439be0e6d10SJiri Olsa ret = fmt->entry(fmt, hpp, he); 440be0e6d10SJiri Olsa 44189fee709SArnaldo Carvalho de Melo ret = hist_entry__snprintf_alignment(he, hpp, fmt, ret); 442be0e6d10SJiri Olsa advance_hpp(hpp, ret); 443be0e6d10SJiri Olsa } 444be0e6d10SJiri Olsa 445be0e6d10SJiri Olsa return hpp->buf - start; 446be0e6d10SJiri Olsa } 447be0e6d10SJiri Olsa 4489da44db1SJiri Olsa static int hist_entry__snprintf(struct hist_entry *he, struct perf_hpp *hpp) 4499da44db1SJiri Olsa { 4509da44db1SJiri Olsa return __hist_entry__snprintf(he, hpp, he->hists->hpp_list); 4519da44db1SJiri Olsa } 4529da44db1SJiri Olsa 453ef86d68aSNamhyung Kim static int hist_entry__hierarchy_fprintf(struct hist_entry *he, 454ef86d68aSNamhyung Kim struct perf_hpp *hpp, 4552dbbe9f2SNamhyung Kim struct hists *hists, 456ef86d68aSNamhyung Kim FILE *fp) 457ef86d68aSNamhyung Kim { 458ef86d68aSNamhyung Kim const char *sep = symbol_conf.field_sep; 459ef86d68aSNamhyung Kim struct perf_hpp_fmt *fmt; 460f58c95e3SNamhyung Kim struct perf_hpp_list_node *fmt_node; 461ef86d68aSNamhyung Kim char *buf = hpp->buf; 462cb1fab91SNamhyung Kim size_t size = hpp->size; 463ef86d68aSNamhyung Kim int ret, printed = 0; 464ef86d68aSNamhyung Kim bool first = true; 465ef86d68aSNamhyung Kim 466ef86d68aSNamhyung Kim if (symbol_conf.exclude_other && !he->parent) 467ef86d68aSNamhyung Kim return 0; 468ef86d68aSNamhyung Kim 469ef86d68aSNamhyung Kim ret = scnprintf(hpp->buf, hpp->size, "%*s", he->depth * HIERARCHY_INDENT, ""); 470ef86d68aSNamhyung Kim advance_hpp(hpp, ret); 471ef86d68aSNamhyung Kim 472f58c95e3SNamhyung Kim /* the first hpp_list_node is for overhead columns */ 473f58c95e3SNamhyung Kim fmt_node = list_first_entry(&hists->hpp_formats, 474f58c95e3SNamhyung Kim struct perf_hpp_list_node, list); 475f58c95e3SNamhyung Kim perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) { 476ef86d68aSNamhyung Kim /* 477ef86d68aSNamhyung Kim * If there's no field_sep, we still need 478ef86d68aSNamhyung Kim * to display initial ' '. 479ef86d68aSNamhyung Kim */ 480ef86d68aSNamhyung Kim if (!sep || !first) { 481ef86d68aSNamhyung Kim ret = scnprintf(hpp->buf, hpp->size, "%s", sep ?: " "); 482ef86d68aSNamhyung Kim advance_hpp(hpp, ret); 483ef86d68aSNamhyung Kim } else 484ef86d68aSNamhyung Kim first = false; 485ef86d68aSNamhyung Kim 486ef86d68aSNamhyung Kim if (perf_hpp__use_color() && fmt->color) 487ef86d68aSNamhyung Kim ret = fmt->color(fmt, hpp, he); 488ef86d68aSNamhyung Kim else 489ef86d68aSNamhyung Kim ret = fmt->entry(fmt, hpp, he); 490ef86d68aSNamhyung Kim 491ef86d68aSNamhyung Kim ret = hist_entry__snprintf_alignment(he, hpp, fmt, ret); 492ef86d68aSNamhyung Kim advance_hpp(hpp, ret); 493ef86d68aSNamhyung Kim } 494ef86d68aSNamhyung Kim 4951b2dbbf4SNamhyung Kim if (!sep) 496ef86d68aSNamhyung Kim ret = scnprintf(hpp->buf, hpp->size, "%*s", 4972dbbe9f2SNamhyung Kim (hists->nr_hpp_node - 2) * HIERARCHY_INDENT, ""); 498ef86d68aSNamhyung Kim advance_hpp(hpp, ret); 499ef86d68aSNamhyung Kim 500cb1fab91SNamhyung Kim printed += fprintf(fp, "%s", buf); 501cb1fab91SNamhyung Kim 5021b2dbbf4SNamhyung Kim perf_hpp_list__for_each_format(he->hpp_list, fmt) { 503cb1fab91SNamhyung Kim hpp->buf = buf; 504cb1fab91SNamhyung Kim hpp->size = size; 505cb1fab91SNamhyung Kim 506ef86d68aSNamhyung Kim /* 507ef86d68aSNamhyung Kim * No need to call hist_entry__snprintf_alignment() since this 508ef86d68aSNamhyung Kim * fmt is always the last column in the hierarchy mode. 509ef86d68aSNamhyung Kim */ 510ef86d68aSNamhyung Kim if (perf_hpp__use_color() && fmt->color) 511ef86d68aSNamhyung Kim fmt->color(fmt, hpp, he); 512ef86d68aSNamhyung Kim else 513ef86d68aSNamhyung Kim fmt->entry(fmt, hpp, he); 514ef86d68aSNamhyung Kim 515cb1fab91SNamhyung Kim /* 516cb1fab91SNamhyung Kim * dynamic entries are right-aligned but we want left-aligned 517cb1fab91SNamhyung Kim * in the hierarchy mode 518cb1fab91SNamhyung Kim */ 51932858480SArnaldo Carvalho de Melo printed += fprintf(fp, "%s%s", sep ?: " ", skip_spaces(buf)); 5201b2dbbf4SNamhyung Kim } 5211b2dbbf4SNamhyung Kim printed += putc('\n', fp); 522ef86d68aSNamhyung Kim 523fabd37b8SArnaldo Carvalho de Melo if (he->leaf && hist_entry__has_callchains(he) && symbol_conf.use_callchain) { 524ef86d68aSNamhyung Kim u64 total = hists__total_period(hists); 525ef86d68aSNamhyung Kim 526ef86d68aSNamhyung Kim printed += hist_entry_callchain__fprintf(he, total, 0, fp); 527ef86d68aSNamhyung Kim goto out; 528ef86d68aSNamhyung Kim } 529ef86d68aSNamhyung Kim 530ef86d68aSNamhyung Kim out: 531ef86d68aSNamhyung Kim return printed; 532ef86d68aSNamhyung Kim } 533ef86d68aSNamhyung Kim 534b10c78c5SJin Yao static int hist_entry__block_fprintf(struct hist_entry *he, 535b10c78c5SJin Yao char *bf, size_t size, 536b10c78c5SJin Yao FILE *fp) 537b10c78c5SJin Yao { 538b10c78c5SJin Yao struct block_hist *bh = container_of(he, struct block_hist, he); 539b10c78c5SJin Yao int ret = 0; 540b10c78c5SJin Yao 541b10c78c5SJin Yao for (unsigned int i = 0; i < bh->block_hists.nr_entries; i++) { 542b10c78c5SJin Yao struct perf_hpp hpp = { 543b10c78c5SJin Yao .buf = bf, 544b10c78c5SJin Yao .size = size, 545b10c78c5SJin Yao .skip = false, 546b10c78c5SJin Yao }; 547b10c78c5SJin Yao 548b10c78c5SJin Yao bh->block_idx = i; 549b10c78c5SJin Yao hist_entry__snprintf(he, &hpp); 550b10c78c5SJin Yao 551b10c78c5SJin Yao if (!hpp.skip) 552b10c78c5SJin Yao ret += fprintf(fp, "%s\n", bf); 553b10c78c5SJin Yao } 554b10c78c5SJin Yao 555b10c78c5SJin Yao return ret; 556b10c78c5SJin Yao } 557b10c78c5SJin Yao 558000078bcSNamhyung Kim static int hist_entry__fprintf(struct hist_entry *he, size_t size, 559d05e3aaeSJiri Olsa char *bf, size_t bfsz, FILE *fp, 560e9de7e2fSArnaldo Carvalho de Melo bool ignore_callchains) 561000078bcSNamhyung Kim { 562000078bcSNamhyung Kim int ret; 5630db64dd0SJin Yao int callchain_ret = 0; 564ea251d51SNamhyung Kim struct perf_hpp hpp = { 565ea251d51SNamhyung Kim .buf = bf, 566ea251d51SNamhyung Kim .size = size, 567ea251d51SNamhyung Kim }; 5688f1d1b44SJiri Olsa struct hists *hists = he->hists; 5697e597d32SNamhyung Kim u64 total_period = hists->stats.total_period; 570000078bcSNamhyung Kim 57199cf666cSArnaldo Carvalho de Melo if (size == 0 || size > bfsz) 57299cf666cSArnaldo Carvalho de Melo size = hpp.size = bfsz; 573000078bcSNamhyung Kim 5742dbbe9f2SNamhyung Kim if (symbol_conf.report_hierarchy) 5752dbbe9f2SNamhyung Kim return hist_entry__hierarchy_fprintf(he, &hpp, hists, fp); 576ef86d68aSNamhyung Kim 577b10c78c5SJin Yao if (symbol_conf.report_block) 578b10c78c5SJin Yao return hist_entry__block_fprintf(he, bf, size, fp); 579b10c78c5SJin Yao 58026d8b338SNamhyung Kim hist_entry__snprintf(he, &hpp); 581000078bcSNamhyung Kim 582000078bcSNamhyung Kim ret = fprintf(fp, "%s\n", bf); 583000078bcSNamhyung Kim 584e9de7e2fSArnaldo Carvalho de Melo if (hist_entry__has_callchains(he) && !ignore_callchains) 5850db64dd0SJin Yao callchain_ret = hist_entry_callchain__fprintf(he, total_period, 5860db64dd0SJin Yao 0, fp); 5870db64dd0SJin Yao 5880db64dd0SJin Yao ret += callchain_ret; 589000078bcSNamhyung Kim 590000078bcSNamhyung Kim return ret; 591000078bcSNamhyung Kim } 592000078bcSNamhyung Kim 5932dbbe9f2SNamhyung Kim static int print_hierarchy_indent(const char *sep, int indent, 5948e2fc44fSNamhyung Kim const char *line, FILE *fp) 5958e2fc44fSNamhyung Kim { 596b598c34fSArnaldo Carvalho de Melo int width; 597b598c34fSArnaldo Carvalho de Melo 5982dbbe9f2SNamhyung Kim if (sep != NULL || indent < 2) 5998e2fc44fSNamhyung Kim return 0; 6008e2fc44fSNamhyung Kim 601b598c34fSArnaldo Carvalho de Melo width = (indent - 2) * HIERARCHY_INDENT; 602b598c34fSArnaldo Carvalho de Melo 603b598c34fSArnaldo Carvalho de Melo return fprintf(fp, "%-*.*s", width, width, line); 6048e2fc44fSNamhyung Kim } 6058e2fc44fSNamhyung Kim 606195bc0f8SNamhyung Kim static int hists__fprintf_hierarchy_headers(struct hists *hists, 607195bc0f8SNamhyung Kim struct perf_hpp *hpp, FILE *fp) 6088e2fc44fSNamhyung Kim { 609f58c95e3SNamhyung Kim bool first_node, first_col; 6102dbbe9f2SNamhyung Kim int indent; 611cb1fab91SNamhyung Kim int depth; 6128e2fc44fSNamhyung Kim unsigned width = 0; 6138e2fc44fSNamhyung Kim unsigned header_width = 0; 6148e2fc44fSNamhyung Kim struct perf_hpp_fmt *fmt; 615f58c95e3SNamhyung Kim struct perf_hpp_list_node *fmt_node; 616195bc0f8SNamhyung Kim const char *sep = symbol_conf.field_sep; 6178e2fc44fSNamhyung Kim 6182dbbe9f2SNamhyung Kim indent = hists->nr_hpp_node; 6198e2fc44fSNamhyung Kim 6208e2fc44fSNamhyung Kim /* preserve max indent depth for column headers */ 621b598c34fSArnaldo Carvalho de Melo print_hierarchy_indent(sep, indent, " ", fp); 6228e2fc44fSNamhyung Kim 623f58c95e3SNamhyung Kim /* the first hpp_list_node is for overhead columns */ 624f58c95e3SNamhyung Kim fmt_node = list_first_entry(&hists->hpp_formats, 625f58c95e3SNamhyung Kim struct perf_hpp_list_node, list); 6268e2fc44fSNamhyung Kim 627f58c95e3SNamhyung Kim perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) { 62829659ab4SJiri Olsa fmt->header(fmt, hpp, hists, 0, NULL); 629f58c95e3SNamhyung Kim fprintf(fp, "%s%s", hpp->buf, sep ?: " "); 6308e2fc44fSNamhyung Kim } 6318e2fc44fSNamhyung Kim 6328e2fc44fSNamhyung Kim /* combine sort headers with ' / ' */ 633f58c95e3SNamhyung Kim first_node = true; 634f58c95e3SNamhyung Kim list_for_each_entry_continue(fmt_node, &hists->hpp_formats, list) { 635f58c95e3SNamhyung Kim if (!first_node) 636f58c95e3SNamhyung Kim header_width += fprintf(fp, " / "); 637f58c95e3SNamhyung Kim first_node = false; 638f58c95e3SNamhyung Kim 639f58c95e3SNamhyung Kim first_col = true; 640f58c95e3SNamhyung Kim perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) { 6418e2fc44fSNamhyung Kim if (perf_hpp__should_skip(fmt, hists)) 6428e2fc44fSNamhyung Kim continue; 6438e2fc44fSNamhyung Kim 644f58c95e3SNamhyung Kim if (!first_col) 645f58c95e3SNamhyung Kim header_width += fprintf(fp, "+"); 646f58c95e3SNamhyung Kim first_col = false; 6478e2fc44fSNamhyung Kim 64829659ab4SJiri Olsa fmt->header(fmt, hpp, hists, 0, NULL); 6498e2fc44fSNamhyung Kim 6503ca43b60SArnaldo Carvalho de Melo header_width += fprintf(fp, "%s", strim(hpp->buf)); 6518e2fc44fSNamhyung Kim } 652f58c95e3SNamhyung Kim } 6538e2fc44fSNamhyung Kim 6548e2fc44fSNamhyung Kim fprintf(fp, "\n# "); 6558e2fc44fSNamhyung Kim 6568e2fc44fSNamhyung Kim /* preserve max indent depth for initial dots */ 6572dbbe9f2SNamhyung Kim print_hierarchy_indent(sep, indent, dots, fp); 6588e2fc44fSNamhyung Kim 659f58c95e3SNamhyung Kim /* the first hpp_list_node is for overhead columns */ 660f58c95e3SNamhyung Kim fmt_node = list_first_entry(&hists->hpp_formats, 661f58c95e3SNamhyung Kim struct perf_hpp_list_node, list); 6628e2fc44fSNamhyung Kim 663f58c95e3SNamhyung Kim first_col = true; 664f58c95e3SNamhyung Kim perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) { 665f58c95e3SNamhyung Kim if (!first_col) 666f58c95e3SNamhyung Kim fprintf(fp, "%s", sep ?: ".."); 667f58c95e3SNamhyung Kim first_col = false; 6688e2fc44fSNamhyung Kim 669da1b0407SJiri Olsa width = fmt->width(fmt, hpp, hists); 6708e2fc44fSNamhyung Kim fprintf(fp, "%.*s", width, dots); 6718e2fc44fSNamhyung Kim } 6728e2fc44fSNamhyung Kim 673cb1fab91SNamhyung Kim depth = 0; 674f58c95e3SNamhyung Kim list_for_each_entry_continue(fmt_node, &hists->hpp_formats, list) { 675f58c95e3SNamhyung Kim first_col = true; 676f58c95e3SNamhyung Kim width = depth * HIERARCHY_INDENT; 677f58c95e3SNamhyung Kim 678f58c95e3SNamhyung Kim perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) { 6798e2fc44fSNamhyung Kim if (perf_hpp__should_skip(fmt, hists)) 6808e2fc44fSNamhyung Kim continue; 6818e2fc44fSNamhyung Kim 682f58c95e3SNamhyung Kim if (!first_col) 683f58c95e3SNamhyung Kim width++; /* for '+' sign between column header */ 684f58c95e3SNamhyung Kim first_col = false; 685f58c95e3SNamhyung Kim 686da1b0407SJiri Olsa width += fmt->width(fmt, hpp, hists); 687f58c95e3SNamhyung Kim } 688cb1fab91SNamhyung Kim 6898e2fc44fSNamhyung Kim if (width > header_width) 6908e2fc44fSNamhyung Kim header_width = width; 691cb1fab91SNamhyung Kim 692cb1fab91SNamhyung Kim depth++; 6938e2fc44fSNamhyung Kim } 6948e2fc44fSNamhyung Kim 6958e2fc44fSNamhyung Kim fprintf(fp, "%s%-.*s", sep ?: " ", header_width, dots); 6968e2fc44fSNamhyung Kim 6978e2fc44fSNamhyung Kim fprintf(fp, "\n#\n"); 6988e2fc44fSNamhyung Kim 6998e2fc44fSNamhyung Kim return 2; 7008e2fc44fSNamhyung Kim } 7018e2fc44fSNamhyung Kim 702f3705b06SJiri Olsa static void fprintf_line(struct hists *hists, struct perf_hpp *hpp, 703f3705b06SJiri Olsa int line, FILE *fp) 7047ccf4f90SNamhyung Kim { 7051240005eSJiri Olsa struct perf_hpp_fmt *fmt; 7067ccf4f90SNamhyung Kim const char *sep = symbol_conf.field_sep; 7075395a048SJiri Olsa bool first = true; 70829659ab4SJiri Olsa int span = 0; 7097ccf4f90SNamhyung Kim 710f0786af5SJiri Olsa hists__for_each_format(hists, fmt) { 711361459f1SNamhyung Kim if (perf_hpp__should_skip(fmt, hists)) 712e67d49a7SNamhyung Kim continue; 713e67d49a7SNamhyung Kim 71429659ab4SJiri Olsa if (!first && !span) 715ea251d51SNamhyung Kim fprintf(fp, "%s", sep ?: " "); 7165395a048SJiri Olsa else 7175395a048SJiri Olsa first = false; 7187ccf4f90SNamhyung Kim 71929659ab4SJiri Olsa fmt->header(fmt, hpp, hists, line, &span); 72029659ab4SJiri Olsa 72129659ab4SJiri Olsa if (!span) 7227a72a2e5SJiri Olsa fprintf(fp, "%s", hpp->buf); 7237ccf4f90SNamhyung Kim } 724f3705b06SJiri Olsa } 7257ccf4f90SNamhyung Kim 726f3705b06SJiri Olsa static int 727f3705b06SJiri Olsa hists__fprintf_standard_headers(struct hists *hists, 728f3705b06SJiri Olsa struct perf_hpp *hpp, 729f3705b06SJiri Olsa FILE *fp) 730f3705b06SJiri Olsa { 731f3705b06SJiri Olsa struct perf_hpp_list *hpp_list = hists->hpp_list; 732f3705b06SJiri Olsa struct perf_hpp_fmt *fmt; 733f3705b06SJiri Olsa unsigned int width; 734f3705b06SJiri Olsa const char *sep = symbol_conf.field_sep; 735f3705b06SJiri Olsa bool first = true; 736f3705b06SJiri Olsa int line; 737f3705b06SJiri Olsa 738f3705b06SJiri Olsa for (line = 0; line < hpp_list->nr_header_lines; line++) { 739f3705b06SJiri Olsa /* first # is displayed one level up */ 740f3705b06SJiri Olsa if (line) 741f3705b06SJiri Olsa fprintf(fp, "# "); 742f3705b06SJiri Olsa fprintf_line(hists, hpp, line, fp); 7437ccf4f90SNamhyung Kim fprintf(fp, "\n"); 744f3705b06SJiri Olsa } 7457ccf4f90SNamhyung Kim 7467ccf4f90SNamhyung Kim if (sep) 747f3705b06SJiri Olsa return hpp_list->nr_header_lines; 7487ccf4f90SNamhyung Kim 7495395a048SJiri Olsa first = true; 7505395a048SJiri Olsa 751ea251d51SNamhyung Kim fprintf(fp, "# "); 752ea251d51SNamhyung Kim 753f0786af5SJiri Olsa hists__for_each_format(hists, fmt) { 7541240005eSJiri Olsa unsigned int i; 755ea251d51SNamhyung Kim 756361459f1SNamhyung Kim if (perf_hpp__should_skip(fmt, hists)) 757e67d49a7SNamhyung Kim continue; 758e67d49a7SNamhyung Kim 7595395a048SJiri Olsa if (!first) 760ea251d51SNamhyung Kim fprintf(fp, "%s", sep ?: " "); 7615395a048SJiri Olsa else 7625395a048SJiri Olsa first = false; 763ea251d51SNamhyung Kim 764da1b0407SJiri Olsa width = fmt->width(fmt, hpp, hists); 765ea251d51SNamhyung Kim for (i = 0; i < width; i++) 766ea251d51SNamhyung Kim fprintf(fp, "."); 7677ccf4f90SNamhyung Kim } 768ea251d51SNamhyung Kim 7697ccf4f90SNamhyung Kim fprintf(fp, "\n"); 7707ccf4f90SNamhyung Kim fprintf(fp, "#\n"); 771f3705b06SJiri Olsa return hpp_list->nr_header_lines + 2; 77236592ebbSJiri Olsa } 77336592ebbSJiri Olsa 7742d831454SJiri Olsa int hists__fprintf_headers(struct hists *hists, FILE *fp) 7757a72a2e5SJiri Olsa { 776d5278220SJiri Olsa char bf[1024]; 7777a72a2e5SJiri Olsa struct perf_hpp dummy_hpp = { 7787a72a2e5SJiri Olsa .buf = bf, 7797a72a2e5SJiri Olsa .size = sizeof(bf), 7807a72a2e5SJiri Olsa }; 7817a72a2e5SJiri Olsa 7827a72a2e5SJiri Olsa fprintf(fp, "# "); 7837a72a2e5SJiri Olsa 7847a72a2e5SJiri Olsa if (symbol_conf.report_hierarchy) 7857a72a2e5SJiri Olsa return hists__fprintf_hierarchy_headers(hists, &dummy_hpp, fp); 7867a72a2e5SJiri Olsa else 7877a72a2e5SJiri Olsa return hists__fprintf_standard_headers(hists, &dummy_hpp, fp); 7887a72a2e5SJiri Olsa 7897a72a2e5SJiri Olsa } 7907a72a2e5SJiri Olsa 79136592ebbSJiri Olsa size_t hists__fprintf(struct hists *hists, bool show_header, int max_rows, 792d05e3aaeSJiri Olsa int max_cols, float min_pcnt, FILE *fp, 793e9de7e2fSArnaldo Carvalho de Melo bool ignore_callchains) 79436592ebbSJiri Olsa { 79536592ebbSJiri Olsa struct rb_node *nd; 79636592ebbSJiri Olsa size_t ret = 0; 79736592ebbSJiri Olsa const char *sep = symbol_conf.field_sep; 79836592ebbSJiri Olsa int nr_rows = 0; 79936592ebbSJiri Olsa size_t linesz; 80036592ebbSJiri Olsa char *line = NULL; 80136592ebbSJiri Olsa unsigned indent; 80236592ebbSJiri Olsa 80336592ebbSJiri Olsa init_rem_hits(); 80436592ebbSJiri Olsa 805e3b60bc9SNamhyung Kim hists__reset_column_width(hists); 80636592ebbSJiri Olsa 80736592ebbSJiri Olsa if (symbol_conf.col_width_list_str) 80836592ebbSJiri Olsa perf_hpp__set_user_width(symbol_conf.col_width_list_str); 80936592ebbSJiri Olsa 81036592ebbSJiri Olsa if (show_header) 81136592ebbSJiri Olsa nr_rows += hists__fprintf_headers(hists, fp); 81236592ebbSJiri Olsa 81336592ebbSJiri Olsa if (max_rows && nr_rows >= max_rows) 8147ccf4f90SNamhyung Kim goto out; 8157ccf4f90SNamhyung Kim 81699cf666cSArnaldo Carvalho de Melo linesz = hists__sort_list_width(hists) + 3 + 1; 8179754c4f9SJiri Olsa linesz += perf_hpp__color_overhead(); 81899cf666cSArnaldo Carvalho de Melo line = malloc(linesz); 81999cf666cSArnaldo Carvalho de Melo if (line == NULL) { 82099cf666cSArnaldo Carvalho de Melo ret = -1; 82199cf666cSArnaldo Carvalho de Melo goto out; 82299cf666cSArnaldo Carvalho de Melo } 82399cf666cSArnaldo Carvalho de Melo 824bd4abd39SNamhyung Kim indent = hists__overhead_width(hists) + 4; 825bd4abd39SNamhyung Kim 8262eb3d689SDavidlohr Bueso for (nd = rb_first_cached(&hists->entries); nd; 8272eb3d689SDavidlohr Bueso nd = __rb_hierarchy_next(nd, HMD_FORCE_CHILD)) { 8287ccf4f90SNamhyung Kim struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); 82914135663SNamhyung Kim float percent; 8307ccf4f90SNamhyung Kim 8317ccf4f90SNamhyung Kim if (h->filtered) 8327ccf4f90SNamhyung Kim continue; 8337ccf4f90SNamhyung Kim 83414135663SNamhyung Kim percent = hist_entry__get_percent_limit(h); 835064f1981SNamhyung Kim if (percent < min_pcnt) 836064f1981SNamhyung Kim continue; 837064f1981SNamhyung Kim 838e9de7e2fSArnaldo Carvalho de Melo ret += hist_entry__fprintf(h, max_cols, line, linesz, fp, ignore_callchains); 8397ccf4f90SNamhyung Kim 8407ccf4f90SNamhyung Kim if (max_rows && ++nr_rows >= max_rows) 84199cf666cSArnaldo Carvalho de Melo break; 8427ccf4f90SNamhyung Kim 843bd4abd39SNamhyung Kim /* 844bd4abd39SNamhyung Kim * If all children are filtered out or percent-limited, 845bd4abd39SNamhyung Kim * display "no entry >= x.xx%" message. 846bd4abd39SNamhyung Kim */ 847bd4abd39SNamhyung Kim if (!h->leaf && !hist_entry__has_hierarchy_children(h, min_pcnt)) { 848f58c95e3SNamhyung Kim int depth = hists->nr_hpp_node + h->depth + 1; 849bd4abd39SNamhyung Kim 850b598c34fSArnaldo Carvalho de Melo print_hierarchy_indent(sep, depth, " ", fp); 851bd4abd39SNamhyung Kim fprintf(fp, "%*sno entry >= %.2f%%\n", indent, "", min_pcnt); 852bd4abd39SNamhyung Kim 853bd4abd39SNamhyung Kim if (max_rows && ++nr_rows >= max_rows) 854bd4abd39SNamhyung Kim break; 855bd4abd39SNamhyung Kim } 856bd4abd39SNamhyung Kim 8577ccf4f90SNamhyung Kim if (h->ms.map == NULL && verbose > 1) { 858b0867f0cSArnaldo Carvalho de Melo map_groups__fprintf(h->thread->mg, fp); 8597ccf4f90SNamhyung Kim fprintf(fp, "%.10s end\n", graph_dotted_line); 8607ccf4f90SNamhyung Kim } 8617ccf4f90SNamhyung Kim } 86299cf666cSArnaldo Carvalho de Melo 86399cf666cSArnaldo Carvalho de Melo free(line); 8647ccf4f90SNamhyung Kim out: 86574cf249dSArnaldo Carvalho de Melo zfree(&rem_sq_bracket); 8667ccf4f90SNamhyung Kim 8677ccf4f90SNamhyung Kim return ret; 8687ccf4f90SNamhyung Kim } 8697ccf4f90SNamhyung Kim 87052168eeaSArnaldo Carvalho de Melo size_t events_stats__fprintf(struct events_stats *stats, FILE *fp) 8717ccf4f90SNamhyung Kim { 8727ccf4f90SNamhyung Kim int i; 8737ccf4f90SNamhyung Kim size_t ret = 0; 8747ccf4f90SNamhyung Kim 8757ccf4f90SNamhyung Kim for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) { 8767ccf4f90SNamhyung Kim const char *name; 8777ccf4f90SNamhyung Kim 8787ccf4f90SNamhyung Kim name = perf_event__name(i); 8797ccf4f90SNamhyung Kim if (!strcmp(name, "UNKNOWN")) 8807ccf4f90SNamhyung Kim continue; 8817ccf4f90SNamhyung Kim 88239ce7fb3SIngo Molnar ret += fprintf(fp, "%16s events: %10d\n", name, stats->nr_events[i]); 8837ccf4f90SNamhyung Kim } 8847ccf4f90SNamhyung Kim 8857ccf4f90SNamhyung Kim return ret; 8867ccf4f90SNamhyung Kim } 887