Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 1 | #include <stdio.h> |
Arnaldo Carvalho de Melo | 8e99b6d | 2017-07-20 15:27:39 -0300 | [diff] [blame] | 2 | #include <linux/string.h> |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 3 | |
| 4 | #include "../../util/util.h" |
| 5 | #include "../../util/hist.h" |
| 6 | #include "../../util/sort.h" |
Namhyung Kim | 5b9e214 | 2013-01-22 18:09:37 +0900 | [diff] [blame] | 7 | #include "../../util/evsel.h" |
Arnaldo Carvalho de Melo | 632a5ca | 2017-04-17 16:30:49 -0300 | [diff] [blame] | 8 | #include "../../util/srcline.h" |
Arnaldo Carvalho de Melo | a067558 | 2017-04-17 16:51:59 -0300 | [diff] [blame] | 9 | #include "../../util/string2.h" |
Arnaldo Carvalho de Melo | e7ff892 | 2017-04-19 21:34:35 -0300 | [diff] [blame] | 10 | #include "../../util/thread.h" |
Arnaldo Carvalho de Melo | 3d689ed | 2017-04-17 16:10:49 -0300 | [diff] [blame] | 11 | #include "../../util/sane_ctype.h" |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 12 | |
| 13 | static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin) |
| 14 | { |
| 15 | int i; |
| 16 | int ret = fprintf(fp, " "); |
| 17 | |
| 18 | for (i = 0; i < left_margin; i++) |
| 19 | ret += fprintf(fp, " "); |
| 20 | |
| 21 | return ret; |
| 22 | } |
| 23 | |
Jin Yao | 0db64dd | 2017-03-26 04:34:28 +0800 | [diff] [blame] | 24 | static size_t inline__fprintf(struct map *map, u64 ip, int left_margin, |
| 25 | int depth, int depth_mask, FILE *fp) |
| 26 | { |
| 27 | struct dso *dso; |
| 28 | struct inline_node *node; |
| 29 | struct inline_list *ilist; |
| 30 | int ret = 0, i; |
| 31 | |
| 32 | if (map == NULL) |
| 33 | return 0; |
| 34 | |
| 35 | dso = map->dso; |
| 36 | if (dso == NULL) |
| 37 | return 0; |
| 38 | |
Jin Yao | 0db64dd | 2017-03-26 04:34:28 +0800 | [diff] [blame] | 39 | node = dso__parse_addr_inlines(dso, |
| 40 | map__rip_2objdump(map, ip)); |
| 41 | if (node == NULL) |
| 42 | return 0; |
| 43 | |
| 44 | list_for_each_entry(ilist, &node->val, list) { |
| 45 | if ((ilist->filename != NULL) || (ilist->funcname != NULL)) { |
| 46 | ret += callchain__fprintf_left_margin(fp, left_margin); |
| 47 | |
| 48 | for (i = 0; i < depth; i++) { |
| 49 | if (depth_mask & (1 << i)) |
| 50 | ret += fprintf(fp, "|"); |
| 51 | else |
| 52 | ret += fprintf(fp, " "); |
| 53 | ret += fprintf(fp, " "); |
| 54 | } |
| 55 | |
Milian Wolff | 5dfa210 | 2017-03-18 22:49:28 +0100 | [diff] [blame] | 56 | if (callchain_param.key == CCKEY_ADDRESS || |
| 57 | callchain_param.key == CCKEY_SRCLINE) { |
Jin Yao | 0db64dd | 2017-03-26 04:34:28 +0800 | [diff] [blame] | 58 | if (ilist->filename != NULL) |
| 59 | ret += fprintf(fp, "%s:%d (inline)", |
| 60 | ilist->filename, |
| 61 | ilist->line_nr); |
| 62 | else |
| 63 | ret += fprintf(fp, "??"); |
| 64 | } else if (ilist->funcname != NULL) |
| 65 | ret += fprintf(fp, "%s (inline)", |
| 66 | ilist->funcname); |
| 67 | else if (ilist->filename != NULL) |
| 68 | ret += fprintf(fp, "%s:%d (inline)", |
| 69 | ilist->filename, |
| 70 | ilist->line_nr); |
| 71 | else |
| 72 | ret += fprintf(fp, "??"); |
| 73 | |
| 74 | ret += fprintf(fp, "\n"); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | inline_node__delete(node); |
| 79 | return ret; |
| 80 | } |
| 81 | |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 82 | static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask, |
| 83 | int left_margin) |
| 84 | { |
| 85 | int i; |
| 86 | size_t ret = callchain__fprintf_left_margin(fp, left_margin); |
| 87 | |
| 88 | for (i = 0; i < depth; i++) |
| 89 | if (depth_mask & (1 << i)) |
| 90 | ret += fprintf(fp, "| "); |
| 91 | else |
| 92 | ret += fprintf(fp, " "); |
| 93 | |
| 94 | ret += fprintf(fp, "\n"); |
| 95 | |
| 96 | return ret; |
| 97 | } |
| 98 | |
Namhyung Kim | 5ab250c | 2015-11-09 14:45:39 +0900 | [diff] [blame] | 99 | static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_node *node, |
| 100 | struct callchain_list *chain, |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 101 | int depth, int depth_mask, int period, |
Namhyung Kim | 5ab250c | 2015-11-09 14:45:39 +0900 | [diff] [blame] | 102 | u64 total_samples, int left_margin) |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 103 | { |
| 104 | int i; |
| 105 | size_t ret = 0; |
Jin Yao | 8577ae6 | 2016-10-31 09:19:52 +0800 | [diff] [blame] | 106 | char bf[1024], *alloc_str = NULL; |
| 107 | char buf[64]; |
| 108 | const char *str; |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 109 | |
| 110 | ret += callchain__fprintf_left_margin(fp, left_margin); |
| 111 | for (i = 0; i < depth; i++) { |
| 112 | if (depth_mask & (1 << i)) |
| 113 | ret += fprintf(fp, "|"); |
| 114 | else |
| 115 | ret += fprintf(fp, " "); |
| 116 | if (!period && i == depth - 1) { |
Namhyung Kim | 5ab250c | 2015-11-09 14:45:39 +0900 | [diff] [blame] | 117 | ret += fprintf(fp, "--"); |
| 118 | ret += callchain_node__fprintf_value(node, fp, total_samples); |
| 119 | ret += fprintf(fp, "--"); |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 120 | } else |
| 121 | ret += fprintf(fp, "%s", " "); |
| 122 | } |
Jin Yao | 8577ae6 | 2016-10-31 09:19:52 +0800 | [diff] [blame] | 123 | |
| 124 | str = callchain_list__sym_name(chain, bf, sizeof(bf), false); |
| 125 | |
| 126 | if (symbol_conf.show_branchflag_count) { |
Jin Yao | c4ee062 | 2017-08-07 21:05:15 +0800 | [diff] [blame^] | 127 | callchain_list_counts__printf_value(chain, NULL, |
| 128 | buf, sizeof(buf)); |
Jin Yao | 8577ae6 | 2016-10-31 09:19:52 +0800 | [diff] [blame] | 129 | |
| 130 | if (asprintf(&alloc_str, "%s%s", str, buf) < 0) |
| 131 | str = "Not enough memory!"; |
| 132 | else |
| 133 | str = alloc_str; |
| 134 | } |
| 135 | |
| 136 | fputs(str, fp); |
Andi Kleen | 2989cca | 2014-11-12 18:05:23 -0800 | [diff] [blame] | 137 | fputc('\n', fp); |
Jin Yao | 8577ae6 | 2016-10-31 09:19:52 +0800 | [diff] [blame] | 138 | free(alloc_str); |
Jin Yao | 0db64dd | 2017-03-26 04:34:28 +0800 | [diff] [blame] | 139 | |
| 140 | if (symbol_conf.inline_name) |
| 141 | ret += inline__fprintf(chain->ms.map, chain->ip, |
| 142 | left_margin, depth, depth_mask, fp); |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 143 | return ret; |
| 144 | } |
| 145 | |
| 146 | static struct symbol *rem_sq_bracket; |
| 147 | static struct callchain_list rem_hits; |
| 148 | |
| 149 | static void init_rem_hits(void) |
| 150 | { |
| 151 | rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6); |
| 152 | if (!rem_sq_bracket) { |
| 153 | fprintf(stderr, "Not enough memory to display remaining hits\n"); |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | strcpy(rem_sq_bracket->name, "[...]"); |
| 158 | rem_hits.ms.sym = rem_sq_bracket; |
| 159 | } |
| 160 | |
| 161 | static size_t __callchain__fprintf_graph(FILE *fp, struct rb_root *root, |
| 162 | u64 total_samples, int depth, |
| 163 | int depth_mask, int left_margin) |
| 164 | { |
| 165 | struct rb_node *node, *next; |
Namhyung Kim | f2af008 | 2015-11-09 14:45:41 +0900 | [diff] [blame] | 166 | struct callchain_node *child = NULL; |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 167 | struct callchain_list *chain; |
| 168 | int new_depth_mask = depth_mask; |
| 169 | u64 remaining; |
| 170 | size_t ret = 0; |
| 171 | int i; |
| 172 | uint entries_printed = 0; |
Namhyung Kim | f2af008 | 2015-11-09 14:45:41 +0900 | [diff] [blame] | 173 | int cumul_count = 0; |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 174 | |
| 175 | remaining = total_samples; |
| 176 | |
| 177 | node = rb_first(root); |
| 178 | while (node) { |
| 179 | u64 new_total; |
| 180 | u64 cumul; |
| 181 | |
| 182 | child = rb_entry(node, struct callchain_node, rb_node); |
| 183 | cumul = callchain_cumul_hits(child); |
| 184 | remaining -= cumul; |
Namhyung Kim | f2af008 | 2015-11-09 14:45:41 +0900 | [diff] [blame] | 185 | cumul_count += callchain_cumul_counts(child); |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 186 | |
| 187 | /* |
| 188 | * The depth mask manages the output of pipes that show |
| 189 | * the depth. We don't want to keep the pipes of the current |
| 190 | * level for the last child of this depth. |
| 191 | * Except if we have remaining filtered hits. They will |
| 192 | * supersede the last child |
| 193 | */ |
| 194 | next = rb_next(node); |
| 195 | if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining)) |
| 196 | new_depth_mask &= ~(1 << (depth - 1)); |
| 197 | |
| 198 | /* |
| 199 | * But we keep the older depth mask for the line separator |
| 200 | * to keep the level link until we reach the last child |
| 201 | */ |
| 202 | ret += ipchain__fprintf_graph_line(fp, depth, depth_mask, |
| 203 | left_margin); |
| 204 | i = 0; |
| 205 | list_for_each_entry(chain, &child->val, list) { |
Namhyung Kim | 5ab250c | 2015-11-09 14:45:39 +0900 | [diff] [blame] | 206 | ret += ipchain__fprintf_graph(fp, child, chain, depth, |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 207 | new_depth_mask, i++, |
| 208 | total_samples, |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 209 | left_margin); |
| 210 | } |
| 211 | |
| 212 | if (callchain_param.mode == CHAIN_GRAPH_REL) |
| 213 | new_total = child->children_hit; |
| 214 | else |
| 215 | new_total = total_samples; |
| 216 | |
| 217 | ret += __callchain__fprintf_graph(fp, &child->rb_root, new_total, |
| 218 | depth + 1, |
| 219 | new_depth_mask | (1 << depth), |
| 220 | left_margin); |
| 221 | node = next; |
| 222 | if (++entries_printed == callchain_param.print_limit) |
| 223 | break; |
| 224 | } |
| 225 | |
| 226 | if (callchain_param.mode == CHAIN_GRAPH_REL && |
| 227 | remaining && remaining != total_samples) { |
Namhyung Kim | 5ab250c | 2015-11-09 14:45:39 +0900 | [diff] [blame] | 228 | struct callchain_node rem_node = { |
| 229 | .hit = remaining, |
| 230 | }; |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 231 | |
| 232 | if (!rem_sq_bracket) |
| 233 | return ret; |
| 234 | |
Namhyung Kim | f2af008 | 2015-11-09 14:45:41 +0900 | [diff] [blame] | 235 | if (callchain_param.value == CCVAL_COUNT && child && child->parent) { |
| 236 | rem_node.count = child->parent->children_count - cumul_count; |
| 237 | if (rem_node.count <= 0) |
| 238 | return ret; |
| 239 | } |
| 240 | |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 241 | new_depth_mask &= ~(1 << (depth - 1)); |
Namhyung Kim | 5ab250c | 2015-11-09 14:45:39 +0900 | [diff] [blame] | 242 | ret += ipchain__fprintf_graph(fp, &rem_node, &rem_hits, depth, |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 243 | new_depth_mask, 0, total_samples, |
Namhyung Kim | 5ab250c | 2015-11-09 14:45:39 +0900 | [diff] [blame] | 244 | left_margin); |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | return ret; |
| 248 | } |
| 249 | |
Namhyung Kim | 7ed5d6e | 2016-01-28 00:40:53 +0900 | [diff] [blame] | 250 | /* |
| 251 | * If have one single callchain root, don't bother printing |
| 252 | * its percentage (100 % in fractal mode and the same percentage |
| 253 | * than the hist in graph mode). This also avoid one level of column. |
| 254 | * |
| 255 | * However when percent-limit applied, it's possible that single callchain |
| 256 | * node have different (non-100% in fractal mode) percentage. |
| 257 | */ |
| 258 | static bool need_percent_display(struct rb_node *node, u64 parent_samples) |
| 259 | { |
| 260 | struct callchain_node *cnode; |
| 261 | |
| 262 | if (rb_next(node)) |
| 263 | return true; |
| 264 | |
| 265 | cnode = rb_entry(node, struct callchain_node, rb_node); |
| 266 | return callchain_cumul_hits(cnode) != parent_samples; |
| 267 | } |
| 268 | |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 269 | static size_t callchain__fprintf_graph(FILE *fp, struct rb_root *root, |
Namhyung Kim | 54d27b3 | 2016-01-28 00:40:52 +0900 | [diff] [blame] | 270 | u64 total_samples, u64 parent_samples, |
| 271 | int left_margin) |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 272 | { |
| 273 | struct callchain_node *cnode; |
| 274 | struct callchain_list *chain; |
| 275 | u32 entries_printed = 0; |
| 276 | bool printed = false; |
| 277 | struct rb_node *node; |
| 278 | int i = 0; |
| 279 | int ret = 0; |
Andi Kleen | 2989cca | 2014-11-12 18:05:23 -0800 | [diff] [blame] | 280 | char bf[1024]; |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 281 | |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 282 | node = rb_first(root); |
Namhyung Kim | 7ed5d6e | 2016-01-28 00:40:53 +0900 | [diff] [blame] | 283 | if (node && !need_percent_display(node, parent_samples)) { |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 284 | cnode = rb_entry(node, struct callchain_node, rb_node); |
| 285 | list_for_each_entry(chain, &cnode->val, list) { |
| 286 | /* |
| 287 | * If we sort by symbol, the first entry is the same than |
| 288 | * the symbol. No need to print it otherwise it appears as |
| 289 | * displayed twice. |
| 290 | */ |
Namhyung Kim | cfaa154 | 2014-05-19 14:19:30 +0900 | [diff] [blame] | 291 | if (!i++ && field_order == NULL && |
Arnaldo Carvalho de Melo | 8e99b6d | 2017-07-20 15:27:39 -0300 | [diff] [blame] | 292 | sort_order && strstarts(sort_order, "sym")) |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 293 | continue; |
Jin Yao | 0db64dd | 2017-03-26 04:34:28 +0800 | [diff] [blame] | 294 | |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 295 | if (!printed) { |
| 296 | ret += callchain__fprintf_left_margin(fp, left_margin); |
| 297 | ret += fprintf(fp, "|\n"); |
| 298 | ret += callchain__fprintf_left_margin(fp, left_margin); |
| 299 | ret += fprintf(fp, "---"); |
| 300 | left_margin += 3; |
| 301 | printed = true; |
| 302 | } else |
| 303 | ret += callchain__fprintf_left_margin(fp, left_margin); |
| 304 | |
Jin Yao | 8577ae6 | 2016-10-31 09:19:52 +0800 | [diff] [blame] | 305 | ret += fprintf(fp, "%s", |
| 306 | callchain_list__sym_name(chain, bf, |
| 307 | sizeof(bf), |
| 308 | false)); |
| 309 | |
| 310 | if (symbol_conf.show_branchflag_count) |
| 311 | ret += callchain_list_counts__printf_value( |
Jin Yao | c4ee062 | 2017-08-07 21:05:15 +0800 | [diff] [blame^] | 312 | chain, fp, NULL, 0); |
Jin Yao | 8577ae6 | 2016-10-31 09:19:52 +0800 | [diff] [blame] | 313 | ret += fprintf(fp, "\n"); |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 314 | |
| 315 | if (++entries_printed == callchain_param.print_limit) |
| 316 | break; |
Jin Yao | 0db64dd | 2017-03-26 04:34:28 +0800 | [diff] [blame] | 317 | |
| 318 | if (symbol_conf.inline_name) |
| 319 | ret += inline__fprintf(chain->ms.map, |
| 320 | chain->ip, |
| 321 | left_margin, |
| 322 | 0, 0, |
| 323 | fp); |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 324 | } |
| 325 | root = &cnode->rb_root; |
| 326 | } |
| 327 | |
Namhyung Kim | 54d27b3 | 2016-01-28 00:40:52 +0900 | [diff] [blame] | 328 | if (callchain_param.mode == CHAIN_GRAPH_REL) |
| 329 | total_samples = parent_samples; |
| 330 | |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 331 | ret += __callchain__fprintf_graph(fp, root, total_samples, |
| 332 | 1, 1, left_margin); |
Namhyung Kim | 3848c23 | 2016-01-28 21:24:54 +0900 | [diff] [blame] | 333 | if (ret) { |
| 334 | /* do not add a blank line if it printed nothing */ |
| 335 | ret += fprintf(fp, "\n"); |
| 336 | } |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 337 | |
| 338 | return ret; |
| 339 | } |
| 340 | |
Arnaldo Carvalho de Melo | 316c713 | 2013-11-05 15:32:36 -0300 | [diff] [blame] | 341 | static size_t __callchain__fprintf_flat(FILE *fp, struct callchain_node *node, |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 342 | u64 total_samples) |
| 343 | { |
| 344 | struct callchain_list *chain; |
| 345 | size_t ret = 0; |
Andi Kleen | 2989cca | 2014-11-12 18:05:23 -0800 | [diff] [blame] | 346 | char bf[1024]; |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 347 | |
Arnaldo Carvalho de Melo | 316c713 | 2013-11-05 15:32:36 -0300 | [diff] [blame] | 348 | if (!node) |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 349 | return 0; |
| 350 | |
Arnaldo Carvalho de Melo | 316c713 | 2013-11-05 15:32:36 -0300 | [diff] [blame] | 351 | ret += __callchain__fprintf_flat(fp, node->parent, total_samples); |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 352 | |
| 353 | |
Arnaldo Carvalho de Melo | 316c713 | 2013-11-05 15:32:36 -0300 | [diff] [blame] | 354 | list_for_each_entry(chain, &node->val, list) { |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 355 | if (chain->ip >= PERF_CONTEXT_MAX) |
| 356 | continue; |
Andi Kleen | 2989cca | 2014-11-12 18:05:23 -0800 | [diff] [blame] | 357 | ret += fprintf(fp, " %s\n", callchain_list__sym_name(chain, |
| 358 | bf, sizeof(bf), false)); |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | return ret; |
| 362 | } |
| 363 | |
Arnaldo Carvalho de Melo | 316c713 | 2013-11-05 15:32:36 -0300 | [diff] [blame] | 364 | static size_t callchain__fprintf_flat(FILE *fp, struct rb_root *tree, |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 365 | u64 total_samples) |
| 366 | { |
| 367 | size_t ret = 0; |
| 368 | u32 entries_printed = 0; |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 369 | struct callchain_node *chain; |
Arnaldo Carvalho de Melo | 316c713 | 2013-11-05 15:32:36 -0300 | [diff] [blame] | 370 | struct rb_node *rb_node = rb_first(tree); |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 371 | |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 372 | while (rb_node) { |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 373 | chain = rb_entry(rb_node, struct callchain_node, rb_node); |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 374 | |
Namhyung Kim | 5ab250c | 2015-11-09 14:45:39 +0900 | [diff] [blame] | 375 | ret += fprintf(fp, " "); |
| 376 | ret += callchain_node__fprintf_value(chain, fp, total_samples); |
| 377 | ret += fprintf(fp, "\n"); |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 378 | ret += __callchain__fprintf_flat(fp, chain, total_samples); |
| 379 | ret += fprintf(fp, "\n"); |
| 380 | if (++entries_printed == callchain_param.print_limit) |
| 381 | break; |
| 382 | |
| 383 | rb_node = rb_next(rb_node); |
| 384 | } |
| 385 | |
| 386 | return ret; |
| 387 | } |
| 388 | |
Namhyung Kim | 26e7792 | 2015-11-09 14:45:37 +0900 | [diff] [blame] | 389 | static size_t __callchain__fprintf_folded(FILE *fp, struct callchain_node *node) |
| 390 | { |
| 391 | const char *sep = symbol_conf.field_sep ?: ";"; |
| 392 | struct callchain_list *chain; |
| 393 | size_t ret = 0; |
| 394 | char bf[1024]; |
| 395 | bool first; |
| 396 | |
| 397 | if (!node) |
| 398 | return 0; |
| 399 | |
| 400 | ret += __callchain__fprintf_folded(fp, node->parent); |
| 401 | |
| 402 | first = (ret == 0); |
| 403 | list_for_each_entry(chain, &node->val, list) { |
| 404 | if (chain->ip >= PERF_CONTEXT_MAX) |
| 405 | continue; |
| 406 | ret += fprintf(fp, "%s%s", first ? "" : sep, |
| 407 | callchain_list__sym_name(chain, |
| 408 | bf, sizeof(bf), false)); |
| 409 | first = false; |
| 410 | } |
| 411 | |
| 412 | return ret; |
| 413 | } |
| 414 | |
| 415 | static size_t callchain__fprintf_folded(FILE *fp, struct rb_root *tree, |
| 416 | u64 total_samples) |
| 417 | { |
| 418 | size_t ret = 0; |
| 419 | u32 entries_printed = 0; |
| 420 | struct callchain_node *chain; |
| 421 | struct rb_node *rb_node = rb_first(tree); |
| 422 | |
| 423 | while (rb_node) { |
Namhyung Kim | 26e7792 | 2015-11-09 14:45:37 +0900 | [diff] [blame] | 424 | |
| 425 | chain = rb_entry(rb_node, struct callchain_node, rb_node); |
Namhyung Kim | 26e7792 | 2015-11-09 14:45:37 +0900 | [diff] [blame] | 426 | |
Namhyung Kim | 5ab250c | 2015-11-09 14:45:39 +0900 | [diff] [blame] | 427 | ret += callchain_node__fprintf_value(chain, fp, total_samples); |
| 428 | ret += fprintf(fp, " "); |
Namhyung Kim | 26e7792 | 2015-11-09 14:45:37 +0900 | [diff] [blame] | 429 | ret += __callchain__fprintf_folded(fp, chain); |
| 430 | ret += fprintf(fp, "\n"); |
| 431 | if (++entries_printed == callchain_param.print_limit) |
| 432 | break; |
| 433 | |
| 434 | rb_node = rb_next(rb_node); |
| 435 | } |
| 436 | |
| 437 | return ret; |
| 438 | } |
| 439 | |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 440 | static size_t hist_entry_callchain__fprintf(struct hist_entry *he, |
| 441 | u64 total_samples, int left_margin, |
| 442 | FILE *fp) |
| 443 | { |
Namhyung Kim | 54d27b3 | 2016-01-28 00:40:52 +0900 | [diff] [blame] | 444 | u64 parent_samples = he->stat.period; |
| 445 | |
| 446 | if (symbol_conf.cumulate_callchain) |
| 447 | parent_samples = he->stat_acc->period; |
| 448 | |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 449 | switch (callchain_param.mode) { |
| 450 | case CHAIN_GRAPH_REL: |
Namhyung Kim | 54d27b3 | 2016-01-28 00:40:52 +0900 | [diff] [blame] | 451 | return callchain__fprintf_graph(fp, &he->sorted_chain, total_samples, |
| 452 | parent_samples, left_margin); |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 453 | break; |
| 454 | case CHAIN_GRAPH_ABS: |
| 455 | return callchain__fprintf_graph(fp, &he->sorted_chain, total_samples, |
Namhyung Kim | 54d27b3 | 2016-01-28 00:40:52 +0900 | [diff] [blame] | 456 | parent_samples, left_margin); |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 457 | break; |
| 458 | case CHAIN_FLAT: |
| 459 | return callchain__fprintf_flat(fp, &he->sorted_chain, total_samples); |
| 460 | break; |
Namhyung Kim | 26e7792 | 2015-11-09 14:45:37 +0900 | [diff] [blame] | 461 | case CHAIN_FOLDED: |
| 462 | return callchain__fprintf_folded(fp, &he->sorted_chain, total_samples); |
| 463 | break; |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 464 | case CHAIN_NONE: |
| 465 | break; |
| 466 | default: |
| 467 | pr_err("Bad callchain mode\n"); |
| 468 | } |
| 469 | |
| 470 | return 0; |
| 471 | } |
| 472 | |
Jiri Olsa | bd28d0c | 2016-09-22 17:36:36 +0200 | [diff] [blame] | 473 | int __hist_entry__snprintf(struct hist_entry *he, struct perf_hpp *hpp, |
| 474 | struct perf_hpp_list *hpp_list) |
Jiri Olsa | be0e6d1 | 2013-02-04 16:33:19 +0100 | [diff] [blame] | 475 | { |
| 476 | const char *sep = symbol_conf.field_sep; |
| 477 | struct perf_hpp_fmt *fmt; |
| 478 | char *start = hpp->buf; |
| 479 | int ret; |
| 480 | bool first = true; |
| 481 | |
| 482 | if (symbol_conf.exclude_other && !he->parent) |
| 483 | return 0; |
| 484 | |
Jiri Olsa | 9da44db | 2016-09-22 17:36:29 +0200 | [diff] [blame] | 485 | perf_hpp_list__for_each_format(hpp_list, fmt) { |
Namhyung Kim | 361459f | 2015-12-23 02:07:08 +0900 | [diff] [blame] | 486 | if (perf_hpp__should_skip(fmt, he->hists)) |
Namhyung Kim | e67d49a | 2014-03-18 13:00:59 +0900 | [diff] [blame] | 487 | continue; |
| 488 | |
Jiri Olsa | be0e6d1 | 2013-02-04 16:33:19 +0100 | [diff] [blame] | 489 | /* |
| 490 | * If there's no field_sep, we still need |
| 491 | * to display initial ' '. |
| 492 | */ |
| 493 | if (!sep || !first) { |
| 494 | ret = scnprintf(hpp->buf, hpp->size, "%s", sep ?: " "); |
| 495 | advance_hpp(hpp, ret); |
| 496 | } else |
| 497 | first = false; |
| 498 | |
Jiri Olsa | 9754c4f | 2013-10-25 13:24:53 +0200 | [diff] [blame] | 499 | if (perf_hpp__use_color() && fmt->color) |
Jiri Olsa | be0e6d1 | 2013-02-04 16:33:19 +0100 | [diff] [blame] | 500 | ret = fmt->color(fmt, hpp, he); |
| 501 | else |
| 502 | ret = fmt->entry(fmt, hpp, he); |
| 503 | |
Arnaldo Carvalho de Melo | 89fee70 | 2016-02-11 17:14:13 -0300 | [diff] [blame] | 504 | ret = hist_entry__snprintf_alignment(he, hpp, fmt, ret); |
Jiri Olsa | be0e6d1 | 2013-02-04 16:33:19 +0100 | [diff] [blame] | 505 | advance_hpp(hpp, ret); |
| 506 | } |
| 507 | |
| 508 | return hpp->buf - start; |
| 509 | } |
| 510 | |
Jiri Olsa | 9da44db | 2016-09-22 17:36:29 +0200 | [diff] [blame] | 511 | static int hist_entry__snprintf(struct hist_entry *he, struct perf_hpp *hpp) |
| 512 | { |
| 513 | return __hist_entry__snprintf(he, hpp, he->hists->hpp_list); |
| 514 | } |
| 515 | |
Namhyung Kim | ef86d68 | 2016-02-25 00:13:41 +0900 | [diff] [blame] | 516 | static int hist_entry__hierarchy_fprintf(struct hist_entry *he, |
| 517 | struct perf_hpp *hpp, |
Namhyung Kim | 2dbbe9f | 2016-03-07 16:44:48 -0300 | [diff] [blame] | 518 | struct hists *hists, |
Namhyung Kim | ef86d68 | 2016-02-25 00:13:41 +0900 | [diff] [blame] | 519 | FILE *fp) |
| 520 | { |
| 521 | const char *sep = symbol_conf.field_sep; |
| 522 | struct perf_hpp_fmt *fmt; |
Namhyung Kim | f58c95e | 2016-03-07 16:44:49 -0300 | [diff] [blame] | 523 | struct perf_hpp_list_node *fmt_node; |
Namhyung Kim | ef86d68 | 2016-02-25 00:13:41 +0900 | [diff] [blame] | 524 | char *buf = hpp->buf; |
Namhyung Kim | cb1fab9 | 2016-02-27 03:52:45 +0900 | [diff] [blame] | 525 | size_t size = hpp->size; |
Namhyung Kim | ef86d68 | 2016-02-25 00:13:41 +0900 | [diff] [blame] | 526 | int ret, printed = 0; |
| 527 | bool first = true; |
| 528 | |
| 529 | if (symbol_conf.exclude_other && !he->parent) |
| 530 | return 0; |
| 531 | |
| 532 | ret = scnprintf(hpp->buf, hpp->size, "%*s", he->depth * HIERARCHY_INDENT, ""); |
| 533 | advance_hpp(hpp, ret); |
| 534 | |
Namhyung Kim | f58c95e | 2016-03-07 16:44:49 -0300 | [diff] [blame] | 535 | /* the first hpp_list_node is for overhead columns */ |
| 536 | fmt_node = list_first_entry(&hists->hpp_formats, |
| 537 | struct perf_hpp_list_node, list); |
| 538 | perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) { |
Namhyung Kim | ef86d68 | 2016-02-25 00:13:41 +0900 | [diff] [blame] | 539 | /* |
| 540 | * If there's no field_sep, we still need |
| 541 | * to display initial ' '. |
| 542 | */ |
| 543 | if (!sep || !first) { |
| 544 | ret = scnprintf(hpp->buf, hpp->size, "%s", sep ?: " "); |
| 545 | advance_hpp(hpp, ret); |
| 546 | } else |
| 547 | first = false; |
| 548 | |
| 549 | if (perf_hpp__use_color() && fmt->color) |
| 550 | ret = fmt->color(fmt, hpp, he); |
| 551 | else |
| 552 | ret = fmt->entry(fmt, hpp, he); |
| 553 | |
| 554 | ret = hist_entry__snprintf_alignment(he, hpp, fmt, ret); |
| 555 | advance_hpp(hpp, ret); |
| 556 | } |
| 557 | |
Namhyung Kim | 1b2dbbf | 2016-03-07 16:44:46 -0300 | [diff] [blame] | 558 | if (!sep) |
Namhyung Kim | ef86d68 | 2016-02-25 00:13:41 +0900 | [diff] [blame] | 559 | ret = scnprintf(hpp->buf, hpp->size, "%*s", |
Namhyung Kim | 2dbbe9f | 2016-03-07 16:44:48 -0300 | [diff] [blame] | 560 | (hists->nr_hpp_node - 2) * HIERARCHY_INDENT, ""); |
Namhyung Kim | ef86d68 | 2016-02-25 00:13:41 +0900 | [diff] [blame] | 561 | advance_hpp(hpp, ret); |
| 562 | |
Namhyung Kim | cb1fab9 | 2016-02-27 03:52:45 +0900 | [diff] [blame] | 563 | printed += fprintf(fp, "%s", buf); |
| 564 | |
Namhyung Kim | 1b2dbbf | 2016-03-07 16:44:46 -0300 | [diff] [blame] | 565 | perf_hpp_list__for_each_format(he->hpp_list, fmt) { |
| 566 | hpp->buf = buf; |
| 567 | hpp->size = size; |
Namhyung Kim | cb1fab9 | 2016-02-27 03:52:45 +0900 | [diff] [blame] | 568 | |
Namhyung Kim | 1b2dbbf | 2016-03-07 16:44:46 -0300 | [diff] [blame] | 569 | /* |
| 570 | * No need to call hist_entry__snprintf_alignment() since this |
| 571 | * fmt is always the last column in the hierarchy mode. |
| 572 | */ |
| 573 | if (perf_hpp__use_color() && fmt->color) |
| 574 | fmt->color(fmt, hpp, he); |
| 575 | else |
| 576 | fmt->entry(fmt, hpp, he); |
Namhyung Kim | ef86d68 | 2016-02-25 00:13:41 +0900 | [diff] [blame] | 577 | |
Namhyung Kim | 1b2dbbf | 2016-03-07 16:44:46 -0300 | [diff] [blame] | 578 | /* |
| 579 | * dynamic entries are right-aligned but we want left-aligned |
| 580 | * in the hierarchy mode |
| 581 | */ |
| 582 | printed += fprintf(fp, "%s%s", sep ?: " ", ltrim(buf)); |
| 583 | } |
| 584 | printed += putc('\n', fp); |
Namhyung Kim | ef86d68 | 2016-02-25 00:13:41 +0900 | [diff] [blame] | 585 | |
| 586 | if (symbol_conf.use_callchain && he->leaf) { |
| 587 | u64 total = hists__total_period(hists); |
| 588 | |
| 589 | printed += hist_entry_callchain__fprintf(he, total, 0, fp); |
| 590 | goto out; |
| 591 | } |
| 592 | |
| 593 | out: |
| 594 | return printed; |
| 595 | } |
| 596 | |
Namhyung Kim | 000078b | 2012-08-20 13:52:06 +0900 | [diff] [blame] | 597 | static int hist_entry__fprintf(struct hist_entry *he, size_t size, |
Jiri Olsa | d05e3aa | 2016-06-14 20:19:18 +0200 | [diff] [blame] | 598 | char *bf, size_t bfsz, FILE *fp, |
| 599 | bool use_callchain) |
Namhyung Kim | 000078b | 2012-08-20 13:52:06 +0900 | [diff] [blame] | 600 | { |
Namhyung Kim | 000078b | 2012-08-20 13:52:06 +0900 | [diff] [blame] | 601 | int ret; |
Jin Yao | 0db64dd | 2017-03-26 04:34:28 +0800 | [diff] [blame] | 602 | int callchain_ret = 0; |
| 603 | int inline_ret = 0; |
Namhyung Kim | ea251d5 | 2012-09-03 11:53:06 +0900 | [diff] [blame] | 604 | struct perf_hpp hpp = { |
| 605 | .buf = bf, |
| 606 | .size = size, |
Namhyung Kim | ea251d5 | 2012-09-03 11:53:06 +0900 | [diff] [blame] | 607 | }; |
Jiri Olsa | 8f1d1b4 | 2016-06-14 20:19:17 +0200 | [diff] [blame] | 608 | struct hists *hists = he->hists; |
Namhyung Kim | 7e597d3 | 2016-01-28 00:40:51 +0900 | [diff] [blame] | 609 | u64 total_period = hists->stats.total_period; |
Namhyung Kim | 000078b | 2012-08-20 13:52:06 +0900 | [diff] [blame] | 610 | |
Arnaldo Carvalho de Melo | 99cf666 | 2013-09-05 15:39:12 -0300 | [diff] [blame] | 611 | if (size == 0 || size > bfsz) |
| 612 | size = hpp.size = bfsz; |
Namhyung Kim | 000078b | 2012-08-20 13:52:06 +0900 | [diff] [blame] | 613 | |
Namhyung Kim | 2dbbe9f | 2016-03-07 16:44:48 -0300 | [diff] [blame] | 614 | if (symbol_conf.report_hierarchy) |
| 615 | return hist_entry__hierarchy_fprintf(he, &hpp, hists, fp); |
Namhyung Kim | ef86d68 | 2016-02-25 00:13:41 +0900 | [diff] [blame] | 616 | |
Namhyung Kim | 26d8b33 | 2014-03-03 16:16:20 +0900 | [diff] [blame] | 617 | hist_entry__snprintf(he, &hpp); |
Namhyung Kim | 000078b | 2012-08-20 13:52:06 +0900 | [diff] [blame] | 618 | |
| 619 | ret = fprintf(fp, "%s\n", bf); |
| 620 | |
Jiri Olsa | d05e3aa | 2016-06-14 20:19:18 +0200 | [diff] [blame] | 621 | if (use_callchain) |
Jin Yao | 0db64dd | 2017-03-26 04:34:28 +0800 | [diff] [blame] | 622 | callchain_ret = hist_entry_callchain__fprintf(he, total_period, |
| 623 | 0, fp); |
| 624 | |
| 625 | if (callchain_ret == 0 && symbol_conf.inline_name) { |
| 626 | inline_ret = inline__fprintf(he->ms.map, he->ip, 0, 0, 0, fp); |
| 627 | ret += inline_ret; |
| 628 | if (inline_ret > 0) |
| 629 | ret += fprintf(fp, "\n"); |
| 630 | } else |
| 631 | ret += callchain_ret; |
Namhyung Kim | 000078b | 2012-08-20 13:52:06 +0900 | [diff] [blame] | 632 | |
| 633 | return ret; |
| 634 | } |
| 635 | |
Namhyung Kim | 2dbbe9f | 2016-03-07 16:44:48 -0300 | [diff] [blame] | 636 | static int print_hierarchy_indent(const char *sep, int indent, |
Namhyung Kim | 8e2fc44 | 2016-02-25 00:13:42 +0900 | [diff] [blame] | 637 | const char *line, FILE *fp) |
| 638 | { |
Namhyung Kim | 2dbbe9f | 2016-03-07 16:44:48 -0300 | [diff] [blame] | 639 | if (sep != NULL || indent < 2) |
Namhyung Kim | 8e2fc44 | 2016-02-25 00:13:42 +0900 | [diff] [blame] | 640 | return 0; |
| 641 | |
Namhyung Kim | 2dbbe9f | 2016-03-07 16:44:48 -0300 | [diff] [blame] | 642 | return fprintf(fp, "%-.*s", (indent - 2) * HIERARCHY_INDENT, line); |
Namhyung Kim | 8e2fc44 | 2016-02-25 00:13:42 +0900 | [diff] [blame] | 643 | } |
| 644 | |
Namhyung Kim | 195bc0f | 2016-09-13 16:45:50 +0900 | [diff] [blame] | 645 | static int hists__fprintf_hierarchy_headers(struct hists *hists, |
| 646 | struct perf_hpp *hpp, FILE *fp) |
Namhyung Kim | 8e2fc44 | 2016-02-25 00:13:42 +0900 | [diff] [blame] | 647 | { |
Namhyung Kim | f58c95e | 2016-03-07 16:44:49 -0300 | [diff] [blame] | 648 | bool first_node, first_col; |
Namhyung Kim | 2dbbe9f | 2016-03-07 16:44:48 -0300 | [diff] [blame] | 649 | int indent; |
Namhyung Kim | cb1fab9 | 2016-02-27 03:52:45 +0900 | [diff] [blame] | 650 | int depth; |
Namhyung Kim | 8e2fc44 | 2016-02-25 00:13:42 +0900 | [diff] [blame] | 651 | unsigned width = 0; |
| 652 | unsigned header_width = 0; |
| 653 | struct perf_hpp_fmt *fmt; |
Namhyung Kim | f58c95e | 2016-03-07 16:44:49 -0300 | [diff] [blame] | 654 | struct perf_hpp_list_node *fmt_node; |
Namhyung Kim | 195bc0f | 2016-09-13 16:45:50 +0900 | [diff] [blame] | 655 | const char *sep = symbol_conf.field_sep; |
Namhyung Kim | 8e2fc44 | 2016-02-25 00:13:42 +0900 | [diff] [blame] | 656 | |
Namhyung Kim | 2dbbe9f | 2016-03-07 16:44:48 -0300 | [diff] [blame] | 657 | indent = hists->nr_hpp_node; |
Namhyung Kim | 8e2fc44 | 2016-02-25 00:13:42 +0900 | [diff] [blame] | 658 | |
| 659 | /* preserve max indent depth for column headers */ |
Namhyung Kim | 2dbbe9f | 2016-03-07 16:44:48 -0300 | [diff] [blame] | 660 | print_hierarchy_indent(sep, indent, spaces, fp); |
Namhyung Kim | 8e2fc44 | 2016-02-25 00:13:42 +0900 | [diff] [blame] | 661 | |
Namhyung Kim | f58c95e | 2016-03-07 16:44:49 -0300 | [diff] [blame] | 662 | /* the first hpp_list_node is for overhead columns */ |
| 663 | fmt_node = list_first_entry(&hists->hpp_formats, |
| 664 | struct perf_hpp_list_node, list); |
Namhyung Kim | 8e2fc44 | 2016-02-25 00:13:42 +0900 | [diff] [blame] | 665 | |
Namhyung Kim | f58c95e | 2016-03-07 16:44:49 -0300 | [diff] [blame] | 666 | perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) { |
Jiri Olsa | 29659ab | 2016-08-07 17:28:30 +0200 | [diff] [blame] | 667 | fmt->header(fmt, hpp, hists, 0, NULL); |
Namhyung Kim | f58c95e | 2016-03-07 16:44:49 -0300 | [diff] [blame] | 668 | fprintf(fp, "%s%s", hpp->buf, sep ?: " "); |
Namhyung Kim | 8e2fc44 | 2016-02-25 00:13:42 +0900 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | /* combine sort headers with ' / ' */ |
Namhyung Kim | f58c95e | 2016-03-07 16:44:49 -0300 | [diff] [blame] | 672 | first_node = true; |
| 673 | list_for_each_entry_continue(fmt_node, &hists->hpp_formats, list) { |
| 674 | if (!first_node) |
Namhyung Kim | 8e2fc44 | 2016-02-25 00:13:42 +0900 | [diff] [blame] | 675 | header_width += fprintf(fp, " / "); |
Namhyung Kim | f58c95e | 2016-03-07 16:44:49 -0300 | [diff] [blame] | 676 | first_node = false; |
| 677 | |
| 678 | first_col = true; |
| 679 | perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) { |
| 680 | if (perf_hpp__should_skip(fmt, hists)) |
| 681 | continue; |
| 682 | |
| 683 | if (!first_col) |
| 684 | header_width += fprintf(fp, "+"); |
| 685 | first_col = false; |
| 686 | |
Jiri Olsa | 29659ab | 2016-08-07 17:28:30 +0200 | [diff] [blame] | 687 | fmt->header(fmt, hpp, hists, 0, NULL); |
Namhyung Kim | f58c95e | 2016-03-07 16:44:49 -0300 | [diff] [blame] | 688 | |
Jiri Olsa | 7d6a7e7 | 2016-04-07 09:11:11 +0200 | [diff] [blame] | 689 | header_width += fprintf(fp, "%s", trim(hpp->buf)); |
Namhyung Kim | 8e2fc44 | 2016-02-25 00:13:42 +0900 | [diff] [blame] | 690 | } |
Namhyung Kim | 8e2fc44 | 2016-02-25 00:13:42 +0900 | [diff] [blame] | 691 | } |
| 692 | |
Namhyung Kim | 8e2fc44 | 2016-02-25 00:13:42 +0900 | [diff] [blame] | 693 | fprintf(fp, "\n# "); |
| 694 | |
| 695 | /* preserve max indent depth for initial dots */ |
Namhyung Kim | 2dbbe9f | 2016-03-07 16:44:48 -0300 | [diff] [blame] | 696 | print_hierarchy_indent(sep, indent, dots, fp); |
Namhyung Kim | 8e2fc44 | 2016-02-25 00:13:42 +0900 | [diff] [blame] | 697 | |
Namhyung Kim | f58c95e | 2016-03-07 16:44:49 -0300 | [diff] [blame] | 698 | /* the first hpp_list_node is for overhead columns */ |
| 699 | fmt_node = list_first_entry(&hists->hpp_formats, |
| 700 | struct perf_hpp_list_node, list); |
Namhyung Kim | 8e2fc44 | 2016-02-25 00:13:42 +0900 | [diff] [blame] | 701 | |
Namhyung Kim | f58c95e | 2016-03-07 16:44:49 -0300 | [diff] [blame] | 702 | first_col = true; |
| 703 | perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) { |
| 704 | if (!first_col) |
| 705 | fprintf(fp, "%s", sep ?: ".."); |
| 706 | first_col = false; |
Namhyung Kim | 8e2fc44 | 2016-02-25 00:13:42 +0900 | [diff] [blame] | 707 | |
Jiri Olsa | da1b040 | 2016-06-14 20:19:20 +0200 | [diff] [blame] | 708 | width = fmt->width(fmt, hpp, hists); |
Namhyung Kim | 8e2fc44 | 2016-02-25 00:13:42 +0900 | [diff] [blame] | 709 | fprintf(fp, "%.*s", width, dots); |
| 710 | } |
| 711 | |
Namhyung Kim | cb1fab9 | 2016-02-27 03:52:45 +0900 | [diff] [blame] | 712 | depth = 0; |
Namhyung Kim | f58c95e | 2016-03-07 16:44:49 -0300 | [diff] [blame] | 713 | list_for_each_entry_continue(fmt_node, &hists->hpp_formats, list) { |
| 714 | first_col = true; |
| 715 | width = depth * HIERARCHY_INDENT; |
Namhyung Kim | 8e2fc44 | 2016-02-25 00:13:42 +0900 | [diff] [blame] | 716 | |
Namhyung Kim | f58c95e | 2016-03-07 16:44:49 -0300 | [diff] [blame] | 717 | perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) { |
| 718 | if (perf_hpp__should_skip(fmt, hists)) |
| 719 | continue; |
| 720 | |
| 721 | if (!first_col) |
| 722 | width++; /* for '+' sign between column header */ |
| 723 | first_col = false; |
| 724 | |
Jiri Olsa | da1b040 | 2016-06-14 20:19:20 +0200 | [diff] [blame] | 725 | width += fmt->width(fmt, hpp, hists); |
Namhyung Kim | f58c95e | 2016-03-07 16:44:49 -0300 | [diff] [blame] | 726 | } |
Namhyung Kim | cb1fab9 | 2016-02-27 03:52:45 +0900 | [diff] [blame] | 727 | |
Namhyung Kim | 8e2fc44 | 2016-02-25 00:13:42 +0900 | [diff] [blame] | 728 | if (width > header_width) |
| 729 | header_width = width; |
Namhyung Kim | cb1fab9 | 2016-02-27 03:52:45 +0900 | [diff] [blame] | 730 | |
| 731 | depth++; |
Namhyung Kim | 8e2fc44 | 2016-02-25 00:13:42 +0900 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | fprintf(fp, "%s%-.*s", sep ?: " ", header_width, dots); |
| 735 | |
Namhyung Kim | 8e2fc44 | 2016-02-25 00:13:42 +0900 | [diff] [blame] | 736 | fprintf(fp, "\n#\n"); |
| 737 | |
| 738 | return 2; |
| 739 | } |
| 740 | |
Jiri Olsa | f3705b0 | 2016-08-07 17:28:29 +0200 | [diff] [blame] | 741 | static void fprintf_line(struct hists *hists, struct perf_hpp *hpp, |
| 742 | int line, FILE *fp) |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 743 | { |
Jiri Olsa | 1240005 | 2012-10-13 00:06:16 +0200 | [diff] [blame] | 744 | struct perf_hpp_fmt *fmt; |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 745 | const char *sep = symbol_conf.field_sep; |
Jiri Olsa | 5395a04 | 2012-10-04 21:49:37 +0900 | [diff] [blame] | 746 | bool first = true; |
Jiri Olsa | 29659ab | 2016-08-07 17:28:30 +0200 | [diff] [blame] | 747 | int span = 0; |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 748 | |
Jiri Olsa | f0786af | 2016-01-18 10:24:23 +0100 | [diff] [blame] | 749 | hists__for_each_format(hists, fmt) { |
Namhyung Kim | 361459f | 2015-12-23 02:07:08 +0900 | [diff] [blame] | 750 | if (perf_hpp__should_skip(fmt, hists)) |
Namhyung Kim | e67d49a | 2014-03-18 13:00:59 +0900 | [diff] [blame] | 751 | continue; |
| 752 | |
Jiri Olsa | 29659ab | 2016-08-07 17:28:30 +0200 | [diff] [blame] | 753 | if (!first && !span) |
Namhyung Kim | ea251d5 | 2012-09-03 11:53:06 +0900 | [diff] [blame] | 754 | fprintf(fp, "%s", sep ?: " "); |
Jiri Olsa | 5395a04 | 2012-10-04 21:49:37 +0900 | [diff] [blame] | 755 | else |
| 756 | first = false; |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 757 | |
Jiri Olsa | 29659ab | 2016-08-07 17:28:30 +0200 | [diff] [blame] | 758 | fmt->header(fmt, hpp, hists, line, &span); |
| 759 | |
| 760 | if (!span) |
| 761 | fprintf(fp, "%s", hpp->buf); |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 762 | } |
Jiri Olsa | f3705b0 | 2016-08-07 17:28:29 +0200 | [diff] [blame] | 763 | } |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 764 | |
Jiri Olsa | f3705b0 | 2016-08-07 17:28:29 +0200 | [diff] [blame] | 765 | static int |
| 766 | hists__fprintf_standard_headers(struct hists *hists, |
| 767 | struct perf_hpp *hpp, |
| 768 | FILE *fp) |
| 769 | { |
| 770 | struct perf_hpp_list *hpp_list = hists->hpp_list; |
| 771 | struct perf_hpp_fmt *fmt; |
| 772 | unsigned int width; |
| 773 | const char *sep = symbol_conf.field_sep; |
| 774 | bool first = true; |
| 775 | int line; |
| 776 | |
| 777 | for (line = 0; line < hpp_list->nr_header_lines; line++) { |
| 778 | /* first # is displayed one level up */ |
| 779 | if (line) |
| 780 | fprintf(fp, "# "); |
| 781 | fprintf_line(hists, hpp, line, fp); |
| 782 | fprintf(fp, "\n"); |
| 783 | } |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 784 | |
| 785 | if (sep) |
Jiri Olsa | f3705b0 | 2016-08-07 17:28:29 +0200 | [diff] [blame] | 786 | return hpp_list->nr_header_lines; |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 787 | |
Jiri Olsa | 5395a04 | 2012-10-04 21:49:37 +0900 | [diff] [blame] | 788 | first = true; |
| 789 | |
Namhyung Kim | ea251d5 | 2012-09-03 11:53:06 +0900 | [diff] [blame] | 790 | fprintf(fp, "# "); |
Namhyung Kim | ea251d5 | 2012-09-03 11:53:06 +0900 | [diff] [blame] | 791 | |
Jiri Olsa | f0786af | 2016-01-18 10:24:23 +0100 | [diff] [blame] | 792 | hists__for_each_format(hists, fmt) { |
Jiri Olsa | 1240005 | 2012-10-13 00:06:16 +0200 | [diff] [blame] | 793 | unsigned int i; |
Namhyung Kim | ea251d5 | 2012-09-03 11:53:06 +0900 | [diff] [blame] | 794 | |
Namhyung Kim | 361459f | 2015-12-23 02:07:08 +0900 | [diff] [blame] | 795 | if (perf_hpp__should_skip(fmt, hists)) |
Namhyung Kim | e67d49a | 2014-03-18 13:00:59 +0900 | [diff] [blame] | 796 | continue; |
| 797 | |
Jiri Olsa | 5395a04 | 2012-10-04 21:49:37 +0900 | [diff] [blame] | 798 | if (!first) |
Namhyung Kim | ea251d5 | 2012-09-03 11:53:06 +0900 | [diff] [blame] | 799 | fprintf(fp, "%s", sep ?: " "); |
Jiri Olsa | 5395a04 | 2012-10-04 21:49:37 +0900 | [diff] [blame] | 800 | else |
| 801 | first = false; |
Namhyung Kim | ea251d5 | 2012-09-03 11:53:06 +0900 | [diff] [blame] | 802 | |
Jiri Olsa | da1b040 | 2016-06-14 20:19:20 +0200 | [diff] [blame] | 803 | width = fmt->width(fmt, hpp, hists); |
Namhyung Kim | ea251d5 | 2012-09-03 11:53:06 +0900 | [diff] [blame] | 804 | for (i = 0; i < width; i++) |
| 805 | fprintf(fp, "."); |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 806 | } |
Namhyung Kim | ea251d5 | 2012-09-03 11:53:06 +0900 | [diff] [blame] | 807 | |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 808 | fprintf(fp, "\n"); |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 809 | fprintf(fp, "#\n"); |
Jiri Olsa | f3705b0 | 2016-08-07 17:28:29 +0200 | [diff] [blame] | 810 | return hpp_list->nr_header_lines + 2; |
Jiri Olsa | 36592eb | 2016-06-14 20:19:14 +0200 | [diff] [blame] | 811 | } |
| 812 | |
Jiri Olsa | 2d83145 | 2016-09-22 17:36:37 +0200 | [diff] [blame] | 813 | int hists__fprintf_headers(struct hists *hists, FILE *fp) |
Jiri Olsa | 7a72a2e | 2016-06-14 20:19:16 +0200 | [diff] [blame] | 814 | { |
Jiri Olsa | d527822 | 2016-09-19 15:09:13 +0200 | [diff] [blame] | 815 | char bf[1024]; |
Jiri Olsa | 7a72a2e | 2016-06-14 20:19:16 +0200 | [diff] [blame] | 816 | struct perf_hpp dummy_hpp = { |
| 817 | .buf = bf, |
| 818 | .size = sizeof(bf), |
| 819 | }; |
| 820 | |
| 821 | fprintf(fp, "# "); |
| 822 | |
| 823 | if (symbol_conf.report_hierarchy) |
| 824 | return hists__fprintf_hierarchy_headers(hists, &dummy_hpp, fp); |
| 825 | else |
| 826 | return hists__fprintf_standard_headers(hists, &dummy_hpp, fp); |
| 827 | |
| 828 | } |
| 829 | |
Jiri Olsa | 36592eb | 2016-06-14 20:19:14 +0200 | [diff] [blame] | 830 | size_t hists__fprintf(struct hists *hists, bool show_header, int max_rows, |
Jiri Olsa | d05e3aa | 2016-06-14 20:19:18 +0200 | [diff] [blame] | 831 | int max_cols, float min_pcnt, FILE *fp, |
| 832 | bool use_callchain) |
Jiri Olsa | 36592eb | 2016-06-14 20:19:14 +0200 | [diff] [blame] | 833 | { |
Jiri Olsa | 36592eb | 2016-06-14 20:19:14 +0200 | [diff] [blame] | 834 | struct rb_node *nd; |
| 835 | size_t ret = 0; |
| 836 | const char *sep = symbol_conf.field_sep; |
| 837 | int nr_rows = 0; |
| 838 | size_t linesz; |
| 839 | char *line = NULL; |
| 840 | unsigned indent; |
| 841 | |
| 842 | init_rem_hits(); |
| 843 | |
Namhyung Kim | e3b60bc | 2016-09-20 14:30:24 +0900 | [diff] [blame] | 844 | hists__reset_column_width(hists); |
Jiri Olsa | 36592eb | 2016-06-14 20:19:14 +0200 | [diff] [blame] | 845 | |
| 846 | if (symbol_conf.col_width_list_str) |
| 847 | perf_hpp__set_user_width(symbol_conf.col_width_list_str); |
| 848 | |
| 849 | if (show_header) |
| 850 | nr_rows += hists__fprintf_headers(hists, fp); |
| 851 | |
| 852 | if (max_rows && nr_rows >= max_rows) |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 853 | goto out; |
| 854 | |
Arnaldo Carvalho de Melo | 99cf666 | 2013-09-05 15:39:12 -0300 | [diff] [blame] | 855 | linesz = hists__sort_list_width(hists) + 3 + 1; |
Jiri Olsa | 9754c4f | 2013-10-25 13:24:53 +0200 | [diff] [blame] | 856 | linesz += perf_hpp__color_overhead(); |
Arnaldo Carvalho de Melo | 99cf666 | 2013-09-05 15:39:12 -0300 | [diff] [blame] | 857 | line = malloc(linesz); |
| 858 | if (line == NULL) { |
| 859 | ret = -1; |
| 860 | goto out; |
| 861 | } |
| 862 | |
Namhyung Kim | bd4abd3 | 2016-02-26 21:13:17 +0900 | [diff] [blame] | 863 | indent = hists__overhead_width(hists) + 4; |
| 864 | |
Namhyung Kim | ef86d68 | 2016-02-25 00:13:41 +0900 | [diff] [blame] | 865 | for (nd = rb_first(&hists->entries); nd; nd = __rb_hierarchy_next(nd, HMD_FORCE_CHILD)) { |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 866 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); |
Namhyung Kim | 1413566 | 2013-10-31 10:17:39 +0900 | [diff] [blame] | 867 | float percent; |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 868 | |
| 869 | if (h->filtered) |
| 870 | continue; |
| 871 | |
Namhyung Kim | 1413566 | 2013-10-31 10:17:39 +0900 | [diff] [blame] | 872 | percent = hist_entry__get_percent_limit(h); |
Namhyung Kim | 064f198 | 2013-05-14 11:09:04 +0900 | [diff] [blame] | 873 | if (percent < min_pcnt) |
| 874 | continue; |
| 875 | |
Jiri Olsa | d05e3aa | 2016-06-14 20:19:18 +0200 | [diff] [blame] | 876 | ret += hist_entry__fprintf(h, max_cols, line, linesz, fp, use_callchain); |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 877 | |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 878 | if (max_rows && ++nr_rows >= max_rows) |
Arnaldo Carvalho de Melo | 99cf666 | 2013-09-05 15:39:12 -0300 | [diff] [blame] | 879 | break; |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 880 | |
Namhyung Kim | bd4abd3 | 2016-02-26 21:13:17 +0900 | [diff] [blame] | 881 | /* |
| 882 | * If all children are filtered out or percent-limited, |
| 883 | * display "no entry >= x.xx%" message. |
| 884 | */ |
| 885 | if (!h->leaf && !hist_entry__has_hierarchy_children(h, min_pcnt)) { |
Namhyung Kim | f58c95e | 2016-03-07 16:44:49 -0300 | [diff] [blame] | 886 | int depth = hists->nr_hpp_node + h->depth + 1; |
Namhyung Kim | bd4abd3 | 2016-02-26 21:13:17 +0900 | [diff] [blame] | 887 | |
Namhyung Kim | f58c95e | 2016-03-07 16:44:49 -0300 | [diff] [blame] | 888 | print_hierarchy_indent(sep, depth, spaces, fp); |
Namhyung Kim | bd4abd3 | 2016-02-26 21:13:17 +0900 | [diff] [blame] | 889 | fprintf(fp, "%*sno entry >= %.2f%%\n", indent, "", min_pcnt); |
| 890 | |
| 891 | if (max_rows && ++nr_rows >= max_rows) |
| 892 | break; |
| 893 | } |
| 894 | |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 895 | if (h->ms.map == NULL && verbose > 1) { |
Arnaldo Carvalho de Melo | 93d5731 | 2014-03-21 17:57:01 -0300 | [diff] [blame] | 896 | __map_groups__fprintf_maps(h->thread->mg, |
Jiri Olsa | acebd40 | 2014-07-14 23:46:47 +0200 | [diff] [blame] | 897 | MAP__FUNCTION, fp); |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 898 | fprintf(fp, "%.10s end\n", graph_dotted_line); |
| 899 | } |
| 900 | } |
Arnaldo Carvalho de Melo | 99cf666 | 2013-09-05 15:39:12 -0300 | [diff] [blame] | 901 | |
| 902 | free(line); |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 903 | out: |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 904 | zfree(&rem_sq_bracket); |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 905 | |
| 906 | return ret; |
| 907 | } |
| 908 | |
Arnaldo Carvalho de Melo | 52168ee | 2012-12-18 16:02:17 -0300 | [diff] [blame] | 909 | size_t events_stats__fprintf(struct events_stats *stats, FILE *fp) |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 910 | { |
| 911 | int i; |
| 912 | size_t ret = 0; |
| 913 | |
| 914 | for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) { |
| 915 | const char *name; |
| 916 | |
Arnaldo Carvalho de Melo | 52168ee | 2012-12-18 16:02:17 -0300 | [diff] [blame] | 917 | if (stats->nr_events[i] == 0) |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 918 | continue; |
| 919 | |
| 920 | name = perf_event__name(i); |
| 921 | if (!strcmp(name, "UNKNOWN")) |
| 922 | continue; |
| 923 | |
| 924 | ret += fprintf(fp, "%16s events: %10d\n", name, |
Arnaldo Carvalho de Melo | 52168ee | 2012-12-18 16:02:17 -0300 | [diff] [blame] | 925 | stats->nr_events[i]); |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | return ret; |
| 929 | } |