John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 1 | #include "hist.h" |
Arnaldo Carvalho de Melo | 4e4f06e | 2009-12-14 13:10:39 -0200 | [diff] [blame] | 2 | #include "session.h" |
| 3 | #include "sort.h" |
Arnaldo Carvalho de Melo | 9b33827 | 2009-12-16 14:31:49 -0200 | [diff] [blame] | 4 | #include <math.h> |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 5 | |
| 6 | struct callchain_param callchain_param = { |
| 7 | .mode = CHAIN_GRAPH_REL, |
| 8 | .min_percent = 0.5 |
| 9 | }; |
| 10 | |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 11 | /* |
| 12 | * histogram, sorted on item, collects counts |
| 13 | */ |
| 14 | |
Eric B Munson | d403d0a | 2010-03-05 12:51:06 -0300 | [diff] [blame] | 15 | struct hist_entry *__perf_session__add_hist_entry(struct rb_root *hists, |
Arnaldo Carvalho de Melo | 4e4f06e | 2009-12-14 13:10:39 -0200 | [diff] [blame] | 16 | struct addr_location *al, |
| 17 | struct symbol *sym_parent, |
| 18 | u64 count, bool *hit) |
Arnaldo Carvalho de Melo | 9735abf | 2009-10-03 10:42:45 -0300 | [diff] [blame] | 19 | { |
Eric B Munson | d403d0a | 2010-03-05 12:51:06 -0300 | [diff] [blame] | 20 | struct rb_node **p = &hists->rb_node; |
Arnaldo Carvalho de Melo | 9735abf | 2009-10-03 10:42:45 -0300 | [diff] [blame] | 21 | struct rb_node *parent = NULL; |
| 22 | struct hist_entry *he; |
| 23 | struct hist_entry entry = { |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 24 | .thread = al->thread, |
Arnaldo Carvalho de Melo | 59fd530 | 2010-03-24 16:40:17 -0300 | [diff] [blame] | 25 | .ms = { |
| 26 | .map = al->map, |
| 27 | .sym = al->sym, |
| 28 | }, |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 29 | .ip = al->addr, |
| 30 | .level = al->level, |
Arnaldo Carvalho de Melo | 9735abf | 2009-10-03 10:42:45 -0300 | [diff] [blame] | 31 | .count = count, |
| 32 | .parent = sym_parent, |
| 33 | }; |
| 34 | int cmp; |
| 35 | |
| 36 | while (*p != NULL) { |
| 37 | parent = *p; |
| 38 | he = rb_entry(parent, struct hist_entry, rb_node); |
| 39 | |
| 40 | cmp = hist_entry__cmp(&entry, he); |
| 41 | |
| 42 | if (!cmp) { |
| 43 | *hit = true; |
| 44 | return he; |
| 45 | } |
| 46 | |
| 47 | if (cmp < 0) |
| 48 | p = &(*p)->rb_left; |
| 49 | else |
| 50 | p = &(*p)->rb_right; |
| 51 | } |
| 52 | |
Arnaldo Carvalho de Melo | b9fb930 | 2010-04-02 09:50:42 -0300 | [diff] [blame^] | 53 | he = malloc(sizeof(*he) + (symbol_conf.use_callchain ? |
| 54 | sizeof(struct callchain_node) : 0)); |
Arnaldo Carvalho de Melo | 9735abf | 2009-10-03 10:42:45 -0300 | [diff] [blame] | 55 | if (!he) |
| 56 | return NULL; |
| 57 | *he = entry; |
| 58 | rb_link_node(&he->rb_node, parent, p); |
Eric B Munson | d403d0a | 2010-03-05 12:51:06 -0300 | [diff] [blame] | 59 | rb_insert_color(&he->rb_node, hists); |
Arnaldo Carvalho de Melo | 9735abf | 2009-10-03 10:42:45 -0300 | [diff] [blame] | 60 | *hit = false; |
| 61 | return he; |
| 62 | } |
| 63 | |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 64 | int64_t |
| 65 | hist_entry__cmp(struct hist_entry *left, struct hist_entry *right) |
| 66 | { |
| 67 | struct sort_entry *se; |
| 68 | int64_t cmp = 0; |
| 69 | |
| 70 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
| 71 | cmp = se->cmp(left, right); |
| 72 | if (cmp) |
| 73 | break; |
| 74 | } |
| 75 | |
| 76 | return cmp; |
| 77 | } |
| 78 | |
| 79 | int64_t |
| 80 | hist_entry__collapse(struct hist_entry *left, struct hist_entry *right) |
| 81 | { |
| 82 | struct sort_entry *se; |
| 83 | int64_t cmp = 0; |
| 84 | |
| 85 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
| 86 | int64_t (*f)(struct hist_entry *, struct hist_entry *); |
| 87 | |
| 88 | f = se->collapse ?: se->cmp; |
| 89 | |
| 90 | cmp = f(left, right); |
| 91 | if (cmp) |
| 92 | break; |
| 93 | } |
| 94 | |
| 95 | return cmp; |
| 96 | } |
| 97 | |
| 98 | void hist_entry__free(struct hist_entry *he) |
| 99 | { |
| 100 | free(he); |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * collapse the histogram |
| 105 | */ |
| 106 | |
Arnaldo Carvalho de Melo | b9bf089 | 2009-12-14 11:37:11 -0200 | [diff] [blame] | 107 | static void collapse__insert_entry(struct rb_root *root, struct hist_entry *he) |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 108 | { |
Arnaldo Carvalho de Melo | b9bf089 | 2009-12-14 11:37:11 -0200 | [diff] [blame] | 109 | struct rb_node **p = &root->rb_node; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 110 | struct rb_node *parent = NULL; |
| 111 | struct hist_entry *iter; |
| 112 | int64_t cmp; |
| 113 | |
| 114 | while (*p != NULL) { |
| 115 | parent = *p; |
| 116 | iter = rb_entry(parent, struct hist_entry, rb_node); |
| 117 | |
| 118 | cmp = hist_entry__collapse(iter, he); |
| 119 | |
| 120 | if (!cmp) { |
| 121 | iter->count += he->count; |
| 122 | hist_entry__free(he); |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | if (cmp < 0) |
| 127 | p = &(*p)->rb_left; |
| 128 | else |
| 129 | p = &(*p)->rb_right; |
| 130 | } |
| 131 | |
| 132 | rb_link_node(&he->rb_node, parent, p); |
Arnaldo Carvalho de Melo | b9bf089 | 2009-12-14 11:37:11 -0200 | [diff] [blame] | 133 | rb_insert_color(&he->rb_node, root); |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 134 | } |
| 135 | |
Eric B Munson | eefc465 | 2010-03-05 12:51:08 -0300 | [diff] [blame] | 136 | void perf_session__collapse_resort(struct rb_root *hists) |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 137 | { |
Arnaldo Carvalho de Melo | b9bf089 | 2009-12-14 11:37:11 -0200 | [diff] [blame] | 138 | struct rb_root tmp; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 139 | struct rb_node *next; |
| 140 | struct hist_entry *n; |
| 141 | |
| 142 | if (!sort__need_collapse) |
| 143 | return; |
| 144 | |
Arnaldo Carvalho de Melo | b9bf089 | 2009-12-14 11:37:11 -0200 | [diff] [blame] | 145 | tmp = RB_ROOT; |
Eric B Munson | eefc465 | 2010-03-05 12:51:08 -0300 | [diff] [blame] | 146 | next = rb_first(hists); |
Arnaldo Carvalho de Melo | b9bf089 | 2009-12-14 11:37:11 -0200 | [diff] [blame] | 147 | |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 148 | while (next) { |
| 149 | n = rb_entry(next, struct hist_entry, rb_node); |
| 150 | next = rb_next(&n->rb_node); |
| 151 | |
Eric B Munson | eefc465 | 2010-03-05 12:51:08 -0300 | [diff] [blame] | 152 | rb_erase(&n->rb_node, hists); |
Arnaldo Carvalho de Melo | b9bf089 | 2009-12-14 11:37:11 -0200 | [diff] [blame] | 153 | collapse__insert_entry(&tmp, n); |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 154 | } |
Arnaldo Carvalho de Melo | b9bf089 | 2009-12-14 11:37:11 -0200 | [diff] [blame] | 155 | |
Eric B Munson | eefc465 | 2010-03-05 12:51:08 -0300 | [diff] [blame] | 156 | *hists = tmp; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /* |
| 160 | * reverse the map, sort on count. |
| 161 | */ |
| 162 | |
Arnaldo Carvalho de Melo | d599db3 | 2009-12-15 20:04:42 -0200 | [diff] [blame] | 163 | static void perf_session__insert_output_hist_entry(struct rb_root *root, |
Arnaldo Carvalho de Melo | 4e4f06e | 2009-12-14 13:10:39 -0200 | [diff] [blame] | 164 | struct hist_entry *he, |
| 165 | u64 min_callchain_hits) |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 166 | { |
Arnaldo Carvalho de Melo | b9bf089 | 2009-12-14 11:37:11 -0200 | [diff] [blame] | 167 | struct rb_node **p = &root->rb_node; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 168 | struct rb_node *parent = NULL; |
| 169 | struct hist_entry *iter; |
| 170 | |
Arnaldo Carvalho de Melo | d599db3 | 2009-12-15 20:04:42 -0200 | [diff] [blame] | 171 | if (symbol_conf.use_callchain) |
Arnaldo Carvalho de Melo | b9fb930 | 2010-04-02 09:50:42 -0300 | [diff] [blame^] | 172 | callchain_param.sort(&he->sorted_chain, he->callchain, |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 173 | min_callchain_hits, &callchain_param); |
| 174 | |
| 175 | while (*p != NULL) { |
| 176 | parent = *p; |
| 177 | iter = rb_entry(parent, struct hist_entry, rb_node); |
| 178 | |
| 179 | if (he->count > iter->count) |
| 180 | p = &(*p)->rb_left; |
| 181 | else |
| 182 | p = &(*p)->rb_right; |
| 183 | } |
| 184 | |
| 185 | rb_link_node(&he->rb_node, parent, p); |
Arnaldo Carvalho de Melo | b9bf089 | 2009-12-14 11:37:11 -0200 | [diff] [blame] | 186 | rb_insert_color(&he->rb_node, root); |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 187 | } |
| 188 | |
Arnaldo Carvalho de Melo | 5f4d3f8 | 2010-03-26 21:16:22 -0300 | [diff] [blame] | 189 | u64 perf_session__output_resort(struct rb_root *hists, u64 total_samples) |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 190 | { |
Arnaldo Carvalho de Melo | b9bf089 | 2009-12-14 11:37:11 -0200 | [diff] [blame] | 191 | struct rb_root tmp; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 192 | struct rb_node *next; |
| 193 | struct hist_entry *n; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 194 | u64 min_callchain_hits; |
Arnaldo Carvalho de Melo | 5f4d3f8 | 2010-03-26 21:16:22 -0300 | [diff] [blame] | 195 | u64 nr_hists = 0; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 196 | |
| 197 | min_callchain_hits = |
| 198 | total_samples * (callchain_param.min_percent / 100); |
| 199 | |
Arnaldo Carvalho de Melo | b9bf089 | 2009-12-14 11:37:11 -0200 | [diff] [blame] | 200 | tmp = RB_ROOT; |
Eric B Munson | eefc465 | 2010-03-05 12:51:08 -0300 | [diff] [blame] | 201 | next = rb_first(hists); |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 202 | |
| 203 | while (next) { |
| 204 | n = rb_entry(next, struct hist_entry, rb_node); |
| 205 | next = rb_next(&n->rb_node); |
| 206 | |
Eric B Munson | eefc465 | 2010-03-05 12:51:08 -0300 | [diff] [blame] | 207 | rb_erase(&n->rb_node, hists); |
Arnaldo Carvalho de Melo | d599db3 | 2009-12-15 20:04:42 -0200 | [diff] [blame] | 208 | perf_session__insert_output_hist_entry(&tmp, n, |
Arnaldo Carvalho de Melo | 4e4f06e | 2009-12-14 13:10:39 -0200 | [diff] [blame] | 209 | min_callchain_hits); |
Arnaldo Carvalho de Melo | 5f4d3f8 | 2010-03-26 21:16:22 -0300 | [diff] [blame] | 210 | ++nr_hists; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 211 | } |
Arnaldo Carvalho de Melo | b9bf089 | 2009-12-14 11:37:11 -0200 | [diff] [blame] | 212 | |
Eric B Munson | eefc465 | 2010-03-05 12:51:08 -0300 | [diff] [blame] | 213 | *hists = tmp; |
Arnaldo Carvalho de Melo | 5f4d3f8 | 2010-03-26 21:16:22 -0300 | [diff] [blame] | 214 | return nr_hists; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 215 | } |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 216 | |
| 217 | static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin) |
| 218 | { |
| 219 | int i; |
| 220 | int ret = fprintf(fp, " "); |
| 221 | |
| 222 | for (i = 0; i < left_margin; i++) |
| 223 | ret += fprintf(fp, " "); |
| 224 | |
| 225 | return ret; |
| 226 | } |
| 227 | |
| 228 | static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask, |
| 229 | int left_margin) |
| 230 | { |
| 231 | int i; |
| 232 | size_t ret = callchain__fprintf_left_margin(fp, left_margin); |
| 233 | |
| 234 | for (i = 0; i < depth; i++) |
| 235 | if (depth_mask & (1 << i)) |
| 236 | ret += fprintf(fp, "| "); |
| 237 | else |
| 238 | ret += fprintf(fp, " "); |
| 239 | |
| 240 | ret += fprintf(fp, "\n"); |
| 241 | |
| 242 | return ret; |
| 243 | } |
| 244 | |
| 245 | static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, |
| 246 | int depth, int depth_mask, int count, |
| 247 | u64 total_samples, int hits, |
| 248 | int left_margin) |
| 249 | { |
| 250 | int i; |
| 251 | size_t ret = 0; |
| 252 | |
| 253 | ret += callchain__fprintf_left_margin(fp, left_margin); |
| 254 | for (i = 0; i < depth; i++) { |
| 255 | if (depth_mask & (1 << i)) |
| 256 | ret += fprintf(fp, "|"); |
| 257 | else |
| 258 | ret += fprintf(fp, " "); |
| 259 | if (!count && i == depth - 1) { |
| 260 | double percent; |
| 261 | |
| 262 | percent = hits * 100.0 / total_samples; |
| 263 | ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent); |
| 264 | } else |
| 265 | ret += fprintf(fp, "%s", " "); |
| 266 | } |
Arnaldo Carvalho de Melo | b3c9ac0 | 2010-03-24 16:40:18 -0300 | [diff] [blame] | 267 | if (chain->ms.sym) |
| 268 | ret += fprintf(fp, "%s\n", chain->ms.sym->name); |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 269 | else |
| 270 | ret += fprintf(fp, "%p\n", (void *)(long)chain->ip); |
| 271 | |
| 272 | return ret; |
| 273 | } |
| 274 | |
| 275 | static struct symbol *rem_sq_bracket; |
| 276 | static struct callchain_list rem_hits; |
| 277 | |
| 278 | static void init_rem_hits(void) |
| 279 | { |
| 280 | rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6); |
| 281 | if (!rem_sq_bracket) { |
| 282 | fprintf(stderr, "Not enough memory to display remaining hits\n"); |
| 283 | return; |
| 284 | } |
| 285 | |
| 286 | strcpy(rem_sq_bracket->name, "[...]"); |
Arnaldo Carvalho de Melo | b3c9ac0 | 2010-03-24 16:40:18 -0300 | [diff] [blame] | 287 | rem_hits.ms.sym = rem_sq_bracket; |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | static size_t __callchain__fprintf_graph(FILE *fp, struct callchain_node *self, |
| 291 | u64 total_samples, int depth, |
| 292 | int depth_mask, int left_margin) |
| 293 | { |
| 294 | struct rb_node *node, *next; |
| 295 | struct callchain_node *child; |
| 296 | struct callchain_list *chain; |
| 297 | int new_depth_mask = depth_mask; |
| 298 | u64 new_total; |
| 299 | u64 remaining; |
| 300 | size_t ret = 0; |
| 301 | int i; |
| 302 | |
| 303 | if (callchain_param.mode == CHAIN_GRAPH_REL) |
| 304 | new_total = self->children_hit; |
| 305 | else |
| 306 | new_total = total_samples; |
| 307 | |
| 308 | remaining = new_total; |
| 309 | |
| 310 | node = rb_first(&self->rb_root); |
| 311 | while (node) { |
| 312 | u64 cumul; |
| 313 | |
| 314 | child = rb_entry(node, struct callchain_node, rb_node); |
| 315 | cumul = cumul_hits(child); |
| 316 | remaining -= cumul; |
| 317 | |
| 318 | /* |
| 319 | * The depth mask manages the output of pipes that show |
| 320 | * the depth. We don't want to keep the pipes of the current |
| 321 | * level for the last child of this depth. |
| 322 | * Except if we have remaining filtered hits. They will |
| 323 | * supersede the last child |
| 324 | */ |
| 325 | next = rb_next(node); |
| 326 | if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining)) |
| 327 | new_depth_mask &= ~(1 << (depth - 1)); |
| 328 | |
| 329 | /* |
Daniel Mack | 3ad2f3fb | 2010-02-03 08:01:28 +0800 | [diff] [blame] | 330 | * But we keep the older depth mask for the line separator |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 331 | * to keep the level link until we reach the last child |
| 332 | */ |
| 333 | ret += ipchain__fprintf_graph_line(fp, depth, depth_mask, |
| 334 | left_margin); |
| 335 | i = 0; |
| 336 | list_for_each_entry(chain, &child->val, list) { |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 337 | ret += ipchain__fprintf_graph(fp, chain, depth, |
| 338 | new_depth_mask, i++, |
| 339 | new_total, |
| 340 | cumul, |
| 341 | left_margin); |
| 342 | } |
| 343 | ret += __callchain__fprintf_graph(fp, child, new_total, |
| 344 | depth + 1, |
| 345 | new_depth_mask | (1 << depth), |
| 346 | left_margin); |
| 347 | node = next; |
| 348 | } |
| 349 | |
| 350 | if (callchain_param.mode == CHAIN_GRAPH_REL && |
| 351 | remaining && remaining != new_total) { |
| 352 | |
| 353 | if (!rem_sq_bracket) |
| 354 | return ret; |
| 355 | |
| 356 | new_depth_mask &= ~(1 << (depth - 1)); |
| 357 | |
| 358 | ret += ipchain__fprintf_graph(fp, &rem_hits, depth, |
| 359 | new_depth_mask, 0, new_total, |
| 360 | remaining, left_margin); |
| 361 | } |
| 362 | |
| 363 | return ret; |
| 364 | } |
| 365 | |
| 366 | static size_t callchain__fprintf_graph(FILE *fp, struct callchain_node *self, |
| 367 | u64 total_samples, int left_margin) |
| 368 | { |
| 369 | struct callchain_list *chain; |
| 370 | bool printed = false; |
| 371 | int i = 0; |
| 372 | int ret = 0; |
| 373 | |
| 374 | list_for_each_entry(chain, &self->val, list) { |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 375 | if (!i++ && sort__first_dimension == SORT_SYM) |
| 376 | continue; |
| 377 | |
| 378 | if (!printed) { |
| 379 | ret += callchain__fprintf_left_margin(fp, left_margin); |
| 380 | ret += fprintf(fp, "|\n"); |
| 381 | ret += callchain__fprintf_left_margin(fp, left_margin); |
| 382 | ret += fprintf(fp, "---"); |
| 383 | |
| 384 | left_margin += 3; |
| 385 | printed = true; |
| 386 | } else |
| 387 | ret += callchain__fprintf_left_margin(fp, left_margin); |
| 388 | |
Arnaldo Carvalho de Melo | b3c9ac0 | 2010-03-24 16:40:18 -0300 | [diff] [blame] | 389 | if (chain->ms.sym) |
| 390 | ret += fprintf(fp, " %s\n", chain->ms.sym->name); |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 391 | else |
| 392 | ret += fprintf(fp, " %p\n", (void *)(long)chain->ip); |
| 393 | } |
| 394 | |
| 395 | ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin); |
| 396 | |
| 397 | return ret; |
| 398 | } |
| 399 | |
| 400 | static size_t callchain__fprintf_flat(FILE *fp, struct callchain_node *self, |
| 401 | u64 total_samples) |
| 402 | { |
| 403 | struct callchain_list *chain; |
| 404 | size_t ret = 0; |
| 405 | |
| 406 | if (!self) |
| 407 | return 0; |
| 408 | |
| 409 | ret += callchain__fprintf_flat(fp, self->parent, total_samples); |
| 410 | |
| 411 | |
| 412 | list_for_each_entry(chain, &self->val, list) { |
| 413 | if (chain->ip >= PERF_CONTEXT_MAX) |
| 414 | continue; |
Arnaldo Carvalho de Melo | b3c9ac0 | 2010-03-24 16:40:18 -0300 | [diff] [blame] | 415 | if (chain->ms.sym) |
| 416 | ret += fprintf(fp, " %s\n", chain->ms.sym->name); |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 417 | else |
| 418 | ret += fprintf(fp, " %p\n", |
| 419 | (void *)(long)chain->ip); |
| 420 | } |
| 421 | |
| 422 | return ret; |
| 423 | } |
| 424 | |
| 425 | static size_t hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self, |
| 426 | u64 total_samples, int left_margin) |
| 427 | { |
| 428 | struct rb_node *rb_node; |
| 429 | struct callchain_node *chain; |
| 430 | size_t ret = 0; |
| 431 | |
| 432 | rb_node = rb_first(&self->sorted_chain); |
| 433 | while (rb_node) { |
| 434 | double percent; |
| 435 | |
| 436 | chain = rb_entry(rb_node, struct callchain_node, rb_node); |
| 437 | percent = chain->hit * 100.0 / total_samples; |
| 438 | switch (callchain_param.mode) { |
| 439 | case CHAIN_FLAT: |
| 440 | ret += percent_color_fprintf(fp, " %6.2f%%\n", |
| 441 | percent); |
| 442 | ret += callchain__fprintf_flat(fp, chain, total_samples); |
| 443 | break; |
| 444 | case CHAIN_GRAPH_ABS: /* Falldown */ |
| 445 | case CHAIN_GRAPH_REL: |
| 446 | ret += callchain__fprintf_graph(fp, chain, total_samples, |
| 447 | left_margin); |
| 448 | case CHAIN_NONE: |
| 449 | default: |
| 450 | break; |
| 451 | } |
| 452 | ret += fprintf(fp, "\n"); |
| 453 | rb_node = rb_next(rb_node); |
| 454 | } |
| 455 | |
| 456 | return ret; |
| 457 | } |
| 458 | |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 459 | int hist_entry__snprintf(struct hist_entry *self, |
| 460 | char *s, size_t size, |
Arnaldo Carvalho de Melo | dd2ee78 | 2010-03-11 20:12:43 -0300 | [diff] [blame] | 461 | struct perf_session *pair_session, |
| 462 | bool show_displacement, |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 463 | long displacement, bool color, |
Arnaldo Carvalho de Melo | dd2ee78 | 2010-03-11 20:12:43 -0300 | [diff] [blame] | 464 | u64 session_total) |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 465 | { |
| 466 | struct sort_entry *se; |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 467 | u64 count, total; |
| 468 | const char *sep = symbol_conf.field_sep; |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 469 | int ret; |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 470 | |
| 471 | if (symbol_conf.exclude_other && !self->parent) |
| 472 | return 0; |
| 473 | |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 474 | if (pair_session) { |
| 475 | count = self->pair ? self->pair->count : 0; |
| 476 | total = pair_session->events_stats.total; |
| 477 | } else { |
| 478 | count = self->count; |
Eric B Munson | eefc465 | 2010-03-05 12:51:08 -0300 | [diff] [blame] | 479 | total = session_total; |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 480 | } |
| 481 | |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 482 | if (total) { |
| 483 | if (color) |
| 484 | ret = percent_color_snprintf(s, size, |
| 485 | sep ? "%.2f" : " %6.2f%%", |
| 486 | (count * 100.0) / total); |
| 487 | else |
| 488 | ret = snprintf(s, size, sep ? "%.2f" : " %6.2f%%", |
| 489 | (count * 100.0) / total); |
| 490 | } else |
| 491 | ret = snprintf(s, size, sep ? "%lld" : "%12lld ", count); |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 492 | |
| 493 | if (symbol_conf.show_nr_samples) { |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 494 | if (sep) |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 495 | ret += snprintf(s + ret, size - ret, "%c%lld", *sep, count); |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 496 | else |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 497 | ret += snprintf(s + ret, size - ret, "%11lld", count); |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | if (pair_session) { |
| 501 | char bf[32]; |
| 502 | double old_percent = 0, new_percent = 0, diff; |
| 503 | |
| 504 | if (total > 0) |
Arnaldo Carvalho de Melo | 9b33827 | 2009-12-16 14:31:49 -0200 | [diff] [blame] | 505 | old_percent = (count * 100.0) / total; |
Eric B Munson | eefc465 | 2010-03-05 12:51:08 -0300 | [diff] [blame] | 506 | if (session_total > 0) |
| 507 | new_percent = (self->count * 100.0) / session_total; |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 508 | |
Arnaldo Carvalho de Melo | 9b33827 | 2009-12-16 14:31:49 -0200 | [diff] [blame] | 509 | diff = new_percent - old_percent; |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 510 | |
Arnaldo Carvalho de Melo | 9b33827 | 2009-12-16 14:31:49 -0200 | [diff] [blame] | 511 | if (fabs(diff) >= 0.01) |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 512 | snprintf(bf, sizeof(bf), "%+4.2F%%", diff); |
| 513 | else |
| 514 | snprintf(bf, sizeof(bf), " "); |
| 515 | |
| 516 | if (sep) |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 517 | ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf); |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 518 | else |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 519 | ret += snprintf(s + ret, size - ret, "%11.11s", bf); |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 520 | |
| 521 | if (show_displacement) { |
| 522 | if (displacement) |
| 523 | snprintf(bf, sizeof(bf), "%+4ld", displacement); |
| 524 | else |
| 525 | snprintf(bf, sizeof(bf), " "); |
| 526 | |
| 527 | if (sep) |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 528 | ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf); |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 529 | else |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 530 | ret += snprintf(s + ret, size - ret, "%6.6s", bf); |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 531 | } |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
| 535 | if (se->elide) |
| 536 | continue; |
| 537 | |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 538 | ret += snprintf(s + ret, size - ret, "%s", sep ?: " "); |
| 539 | ret += se->snprintf(self, s + ret, size - ret, |
| 540 | se->width ? *se->width : 0); |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 541 | } |
| 542 | |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 543 | return ret; |
| 544 | } |
| 545 | |
| 546 | int hist_entry__fprintf(struct hist_entry *self, |
| 547 | struct perf_session *pair_session, |
| 548 | bool show_displacement, |
| 549 | long displacement, FILE *fp, |
| 550 | u64 session_total) |
| 551 | { |
| 552 | char bf[512]; |
| 553 | hist_entry__snprintf(self, bf, sizeof(bf), pair_session, |
| 554 | show_displacement, displacement, |
| 555 | true, session_total); |
| 556 | return fprintf(fp, "%s\n", bf); |
Arnaldo Carvalho de Melo | 3997d37 | 2010-03-12 12:46:48 -0300 | [diff] [blame] | 557 | } |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 558 | |
Arnaldo Carvalho de Melo | 3997d37 | 2010-03-12 12:46:48 -0300 | [diff] [blame] | 559 | static size_t hist_entry__fprintf_callchain(struct hist_entry *self, FILE *fp, |
| 560 | u64 session_total) |
| 561 | { |
| 562 | int left_margin = 0; |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 563 | |
Arnaldo Carvalho de Melo | 3997d37 | 2010-03-12 12:46:48 -0300 | [diff] [blame] | 564 | if (sort__first_dimension == SORT_COMM) { |
| 565 | struct sort_entry *se = list_first_entry(&hist_entry__sort_list, |
| 566 | typeof(*se), list); |
| 567 | left_margin = se->width ? *se->width : 0; |
| 568 | left_margin -= thread__comm_len(self->thread); |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 569 | } |
| 570 | |
Arnaldo Carvalho de Melo | 3997d37 | 2010-03-12 12:46:48 -0300 | [diff] [blame] | 571 | return hist_entry_callchain__fprintf(fp, self, session_total, |
| 572 | left_margin); |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 573 | } |
| 574 | |
Eric B Munson | eefc465 | 2010-03-05 12:51:08 -0300 | [diff] [blame] | 575 | size_t perf_session__fprintf_hists(struct rb_root *hists, |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 576 | struct perf_session *pair, |
Eric B Munson | eefc465 | 2010-03-05 12:51:08 -0300 | [diff] [blame] | 577 | bool show_displacement, FILE *fp, |
| 578 | u64 session_total) |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 579 | { |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 580 | struct sort_entry *se; |
| 581 | struct rb_node *nd; |
| 582 | size_t ret = 0; |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 583 | unsigned long position = 1; |
| 584 | long displacement = 0; |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 585 | unsigned int width; |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 586 | const char *sep = symbol_conf.field_sep; |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 587 | char *col_width = symbol_conf.col_width_list_str; |
| 588 | |
| 589 | init_rem_hits(); |
| 590 | |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 591 | fprintf(fp, "# %s", pair ? "Baseline" : "Overhead"); |
| 592 | |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 593 | if (symbol_conf.show_nr_samples) { |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 594 | if (sep) |
| 595 | fprintf(fp, "%cSamples", *sep); |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 596 | else |
| 597 | fputs(" Samples ", fp); |
| 598 | } |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 599 | |
| 600 | if (pair) { |
| 601 | if (sep) |
| 602 | ret += fprintf(fp, "%cDelta", *sep); |
| 603 | else |
| 604 | ret += fprintf(fp, " Delta "); |
| 605 | |
| 606 | if (show_displacement) { |
| 607 | if (sep) |
| 608 | ret += fprintf(fp, "%cDisplacement", *sep); |
| 609 | else |
| 610 | ret += fprintf(fp, " Displ"); |
| 611 | } |
| 612 | } |
| 613 | |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 614 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
| 615 | if (se->elide) |
| 616 | continue; |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 617 | if (sep) { |
| 618 | fprintf(fp, "%c%s", *sep, se->header); |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 619 | continue; |
| 620 | } |
| 621 | width = strlen(se->header); |
| 622 | if (se->width) { |
| 623 | if (symbol_conf.col_width_list_str) { |
| 624 | if (col_width) { |
| 625 | *se->width = atoi(col_width); |
| 626 | col_width = strchr(col_width, ','); |
| 627 | if (col_width) |
| 628 | ++col_width; |
| 629 | } |
| 630 | } |
| 631 | width = *se->width = max(*se->width, width); |
| 632 | } |
| 633 | fprintf(fp, " %*s", width, se->header); |
| 634 | } |
| 635 | fprintf(fp, "\n"); |
| 636 | |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 637 | if (sep) |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 638 | goto print_entries; |
| 639 | |
| 640 | fprintf(fp, "# ........"); |
| 641 | if (symbol_conf.show_nr_samples) |
| 642 | fprintf(fp, " .........."); |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 643 | if (pair) { |
| 644 | fprintf(fp, " .........."); |
| 645 | if (show_displacement) |
| 646 | fprintf(fp, " ....."); |
| 647 | } |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 648 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
| 649 | unsigned int i; |
| 650 | |
| 651 | if (se->elide) |
| 652 | continue; |
| 653 | |
| 654 | fprintf(fp, " "); |
| 655 | if (se->width) |
| 656 | width = *se->width; |
| 657 | else |
| 658 | width = strlen(se->header); |
| 659 | for (i = 0; i < width; i++) |
| 660 | fprintf(fp, "."); |
| 661 | } |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 662 | |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 663 | fprintf(fp, "\n#\n"); |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 664 | |
| 665 | print_entries: |
Eric B Munson | eefc465 | 2010-03-05 12:51:08 -0300 | [diff] [blame] | 666 | for (nd = rb_first(hists); nd; nd = rb_next(nd)) { |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 667 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); |
| 668 | |
| 669 | if (show_displacement) { |
| 670 | if (h->pair != NULL) |
| 671 | displacement = ((long)h->pair->position - |
| 672 | (long)position); |
| 673 | else |
| 674 | displacement = 0; |
| 675 | ++position; |
| 676 | } |
Eric B Munson | eefc465 | 2010-03-05 12:51:08 -0300 | [diff] [blame] | 677 | ret += hist_entry__fprintf(h, pair, show_displacement, |
| 678 | displacement, fp, session_total); |
Arnaldo Carvalho de Melo | 3997d37 | 2010-03-12 12:46:48 -0300 | [diff] [blame] | 679 | |
| 680 | if (symbol_conf.use_callchain) |
| 681 | ret += hist_entry__fprintf_callchain(h, fp, session_total); |
| 682 | |
Arnaldo Carvalho de Melo | 59fd530 | 2010-03-24 16:40:17 -0300 | [diff] [blame] | 683 | if (h->ms.map == NULL && verbose > 1) { |
Arnaldo Carvalho de Melo | 65f2ed2 | 2010-03-09 15:58:17 -0300 | [diff] [blame] | 684 | __map_groups__fprintf_maps(&h->thread->mg, |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 685 | MAP__FUNCTION, verbose, fp); |
Arnaldo Carvalho de Melo | 65f2ed2 | 2010-03-09 15:58:17 -0300 | [diff] [blame] | 686 | fprintf(fp, "%.10s end\n", graph_dotted_line); |
| 687 | } |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 688 | } |
| 689 | |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 690 | free(rem_sq_bracket); |
| 691 | |
| 692 | return ret; |
| 693 | } |