blob: 09e09e78cb62eb1476037b0c2bd7dfafe9f0b7ac [file] [log] [blame]
John Kacur3d1d07e2009-09-28 15:32:55 +02001#include "hist.h"
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -02002#include "session.h"
3#include "sort.h"
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -02004#include <math.h>
John Kacur3d1d07e2009-09-28 15:32:55 +02005
6struct callchain_param callchain_param = {
7 .mode = CHAIN_GRAPH_REL,
8 .min_percent = 0.5
9};
10
John Kacur3d1d07e2009-09-28 15:32:55 +020011/*
12 * histogram, sorted on item, collects counts
13 */
14
Eric B Munsond403d0a2010-03-05 12:51:06 -030015struct hist_entry *__perf_session__add_hist_entry(struct rb_root *hists,
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -020016 struct addr_location *al,
17 struct symbol *sym_parent,
18 u64 count, bool *hit)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -030019{
Eric B Munsond403d0a2010-03-05 12:51:06 -030020 struct rb_node **p = &hists->rb_node;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -030021 struct rb_node *parent = NULL;
22 struct hist_entry *he;
23 struct hist_entry entry = {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -020024 .thread = al->thread,
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -030025 .ms = {
26 .map = al->map,
27 .sym = al->sym,
28 },
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -020029 .ip = al->addr,
30 .level = al->level,
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -030031 .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
53 he = malloc(sizeof(*he));
54 if (!he)
55 return NULL;
56 *he = entry;
57 rb_link_node(&he->rb_node, parent, p);
Eric B Munsond403d0a2010-03-05 12:51:06 -030058 rb_insert_color(&he->rb_node, hists);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -030059 *hit = false;
60 return he;
61}
62
John Kacur3d1d07e2009-09-28 15:32:55 +020063int64_t
64hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
65{
66 struct sort_entry *se;
67 int64_t cmp = 0;
68
69 list_for_each_entry(se, &hist_entry__sort_list, list) {
70 cmp = se->cmp(left, right);
71 if (cmp)
72 break;
73 }
74
75 return cmp;
76}
77
78int64_t
79hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
80{
81 struct sort_entry *se;
82 int64_t cmp = 0;
83
84 list_for_each_entry(se, &hist_entry__sort_list, list) {
85 int64_t (*f)(struct hist_entry *, struct hist_entry *);
86
87 f = se->collapse ?: se->cmp;
88
89 cmp = f(left, right);
90 if (cmp)
91 break;
92 }
93
94 return cmp;
95}
96
97void hist_entry__free(struct hist_entry *he)
98{
99 free(he);
100}
101
102/*
103 * collapse the histogram
104 */
105
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200106static void collapse__insert_entry(struct rb_root *root, struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +0200107{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200108 struct rb_node **p = &root->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200109 struct rb_node *parent = NULL;
110 struct hist_entry *iter;
111 int64_t cmp;
112
113 while (*p != NULL) {
114 parent = *p;
115 iter = rb_entry(parent, struct hist_entry, rb_node);
116
117 cmp = hist_entry__collapse(iter, he);
118
119 if (!cmp) {
120 iter->count += he->count;
121 hist_entry__free(he);
122 return;
123 }
124
125 if (cmp < 0)
126 p = &(*p)->rb_left;
127 else
128 p = &(*p)->rb_right;
129 }
130
131 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200132 rb_insert_color(&he->rb_node, root);
John Kacur3d1d07e2009-09-28 15:32:55 +0200133}
134
Eric B Munsoneefc4652010-03-05 12:51:08 -0300135void perf_session__collapse_resort(struct rb_root *hists)
John Kacur3d1d07e2009-09-28 15:32:55 +0200136{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200137 struct rb_root tmp;
John Kacur3d1d07e2009-09-28 15:32:55 +0200138 struct rb_node *next;
139 struct hist_entry *n;
140
141 if (!sort__need_collapse)
142 return;
143
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200144 tmp = RB_ROOT;
Eric B Munsoneefc4652010-03-05 12:51:08 -0300145 next = rb_first(hists);
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200146
John Kacur3d1d07e2009-09-28 15:32:55 +0200147 while (next) {
148 n = rb_entry(next, struct hist_entry, rb_node);
149 next = rb_next(&n->rb_node);
150
Eric B Munsoneefc4652010-03-05 12:51:08 -0300151 rb_erase(&n->rb_node, hists);
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200152 collapse__insert_entry(&tmp, n);
John Kacur3d1d07e2009-09-28 15:32:55 +0200153 }
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200154
Eric B Munsoneefc4652010-03-05 12:51:08 -0300155 *hists = tmp;
John Kacur3d1d07e2009-09-28 15:32:55 +0200156}
157
158/*
159 * reverse the map, sort on count.
160 */
161
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200162static void perf_session__insert_output_hist_entry(struct rb_root *root,
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -0200163 struct hist_entry *he,
164 u64 min_callchain_hits)
John Kacur3d1d07e2009-09-28 15:32:55 +0200165{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200166 struct rb_node **p = &root->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200167 struct rb_node *parent = NULL;
168 struct hist_entry *iter;
169
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200170 if (symbol_conf.use_callchain)
John Kacur3d1d07e2009-09-28 15:32:55 +0200171 callchain_param.sort(&he->sorted_chain, &he->callchain,
172 min_callchain_hits, &callchain_param);
173
174 while (*p != NULL) {
175 parent = *p;
176 iter = rb_entry(parent, struct hist_entry, rb_node);
177
178 if (he->count > iter->count)
179 p = &(*p)->rb_left;
180 else
181 p = &(*p)->rb_right;
182 }
183
184 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200185 rb_insert_color(&he->rb_node, root);
John Kacur3d1d07e2009-09-28 15:32:55 +0200186}
187
Eric B Munsoneefc4652010-03-05 12:51:08 -0300188void perf_session__output_resort(struct rb_root *hists, u64 total_samples)
John Kacur3d1d07e2009-09-28 15:32:55 +0200189{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200190 struct rb_root tmp;
John Kacur3d1d07e2009-09-28 15:32:55 +0200191 struct rb_node *next;
192 struct hist_entry *n;
John Kacur3d1d07e2009-09-28 15:32:55 +0200193 u64 min_callchain_hits;
194
195 min_callchain_hits =
196 total_samples * (callchain_param.min_percent / 100);
197
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200198 tmp = RB_ROOT;
Eric B Munsoneefc4652010-03-05 12:51:08 -0300199 next = rb_first(hists);
John Kacur3d1d07e2009-09-28 15:32:55 +0200200
201 while (next) {
202 n = rb_entry(next, struct hist_entry, rb_node);
203 next = rb_next(&n->rb_node);
204
Eric B Munsoneefc4652010-03-05 12:51:08 -0300205 rb_erase(&n->rb_node, hists);
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200206 perf_session__insert_output_hist_entry(&tmp, n,
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -0200207 min_callchain_hits);
John Kacur3d1d07e2009-09-28 15:32:55 +0200208 }
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200209
Eric B Munsoneefc4652010-03-05 12:51:08 -0300210 *hists = tmp;
John Kacur3d1d07e2009-09-28 15:32:55 +0200211}
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200212
213static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
214{
215 int i;
216 int ret = fprintf(fp, " ");
217
218 for (i = 0; i < left_margin; i++)
219 ret += fprintf(fp, " ");
220
221 return ret;
222}
223
224static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
225 int left_margin)
226{
227 int i;
228 size_t ret = callchain__fprintf_left_margin(fp, left_margin);
229
230 for (i = 0; i < depth; i++)
231 if (depth_mask & (1 << i))
232 ret += fprintf(fp, "| ");
233 else
234 ret += fprintf(fp, " ");
235
236 ret += fprintf(fp, "\n");
237
238 return ret;
239}
240
241static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain,
242 int depth, int depth_mask, int count,
243 u64 total_samples, int hits,
244 int left_margin)
245{
246 int i;
247 size_t ret = 0;
248
249 ret += callchain__fprintf_left_margin(fp, left_margin);
250 for (i = 0; i < depth; i++) {
251 if (depth_mask & (1 << i))
252 ret += fprintf(fp, "|");
253 else
254 ret += fprintf(fp, " ");
255 if (!count && i == depth - 1) {
256 double percent;
257
258 percent = hits * 100.0 / total_samples;
259 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
260 } else
261 ret += fprintf(fp, "%s", " ");
262 }
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300263 if (chain->ms.sym)
264 ret += fprintf(fp, "%s\n", chain->ms.sym->name);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200265 else
266 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
267
268 return ret;
269}
270
271static struct symbol *rem_sq_bracket;
272static struct callchain_list rem_hits;
273
274static void init_rem_hits(void)
275{
276 rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
277 if (!rem_sq_bracket) {
278 fprintf(stderr, "Not enough memory to display remaining hits\n");
279 return;
280 }
281
282 strcpy(rem_sq_bracket->name, "[...]");
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300283 rem_hits.ms.sym = rem_sq_bracket;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200284}
285
286static size_t __callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
287 u64 total_samples, int depth,
288 int depth_mask, int left_margin)
289{
290 struct rb_node *node, *next;
291 struct callchain_node *child;
292 struct callchain_list *chain;
293 int new_depth_mask = depth_mask;
294 u64 new_total;
295 u64 remaining;
296 size_t ret = 0;
297 int i;
298
299 if (callchain_param.mode == CHAIN_GRAPH_REL)
300 new_total = self->children_hit;
301 else
302 new_total = total_samples;
303
304 remaining = new_total;
305
306 node = rb_first(&self->rb_root);
307 while (node) {
308 u64 cumul;
309
310 child = rb_entry(node, struct callchain_node, rb_node);
311 cumul = cumul_hits(child);
312 remaining -= cumul;
313
314 /*
315 * The depth mask manages the output of pipes that show
316 * the depth. We don't want to keep the pipes of the current
317 * level for the last child of this depth.
318 * Except if we have remaining filtered hits. They will
319 * supersede the last child
320 */
321 next = rb_next(node);
322 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
323 new_depth_mask &= ~(1 << (depth - 1));
324
325 /*
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800326 * But we keep the older depth mask for the line separator
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200327 * to keep the level link until we reach the last child
328 */
329 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
330 left_margin);
331 i = 0;
332 list_for_each_entry(chain, &child->val, list) {
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200333 ret += ipchain__fprintf_graph(fp, chain, depth,
334 new_depth_mask, i++,
335 new_total,
336 cumul,
337 left_margin);
338 }
339 ret += __callchain__fprintf_graph(fp, child, new_total,
340 depth + 1,
341 new_depth_mask | (1 << depth),
342 left_margin);
343 node = next;
344 }
345
346 if (callchain_param.mode == CHAIN_GRAPH_REL &&
347 remaining && remaining != new_total) {
348
349 if (!rem_sq_bracket)
350 return ret;
351
352 new_depth_mask &= ~(1 << (depth - 1));
353
354 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
355 new_depth_mask, 0, new_total,
356 remaining, left_margin);
357 }
358
359 return ret;
360}
361
362static size_t callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
363 u64 total_samples, int left_margin)
364{
365 struct callchain_list *chain;
366 bool printed = false;
367 int i = 0;
368 int ret = 0;
369
370 list_for_each_entry(chain, &self->val, list) {
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200371 if (!i++ && sort__first_dimension == SORT_SYM)
372 continue;
373
374 if (!printed) {
375 ret += callchain__fprintf_left_margin(fp, left_margin);
376 ret += fprintf(fp, "|\n");
377 ret += callchain__fprintf_left_margin(fp, left_margin);
378 ret += fprintf(fp, "---");
379
380 left_margin += 3;
381 printed = true;
382 } else
383 ret += callchain__fprintf_left_margin(fp, left_margin);
384
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300385 if (chain->ms.sym)
386 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200387 else
388 ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
389 }
390
391 ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin);
392
393 return ret;
394}
395
396static size_t callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
397 u64 total_samples)
398{
399 struct callchain_list *chain;
400 size_t ret = 0;
401
402 if (!self)
403 return 0;
404
405 ret += callchain__fprintf_flat(fp, self->parent, total_samples);
406
407
408 list_for_each_entry(chain, &self->val, list) {
409 if (chain->ip >= PERF_CONTEXT_MAX)
410 continue;
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300411 if (chain->ms.sym)
412 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200413 else
414 ret += fprintf(fp, " %p\n",
415 (void *)(long)chain->ip);
416 }
417
418 return ret;
419}
420
421static size_t hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
422 u64 total_samples, int left_margin)
423{
424 struct rb_node *rb_node;
425 struct callchain_node *chain;
426 size_t ret = 0;
427
428 rb_node = rb_first(&self->sorted_chain);
429 while (rb_node) {
430 double percent;
431
432 chain = rb_entry(rb_node, struct callchain_node, rb_node);
433 percent = chain->hit * 100.0 / total_samples;
434 switch (callchain_param.mode) {
435 case CHAIN_FLAT:
436 ret += percent_color_fprintf(fp, " %6.2f%%\n",
437 percent);
438 ret += callchain__fprintf_flat(fp, chain, total_samples);
439 break;
440 case CHAIN_GRAPH_ABS: /* Falldown */
441 case CHAIN_GRAPH_REL:
442 ret += callchain__fprintf_graph(fp, chain, total_samples,
443 left_margin);
444 case CHAIN_NONE:
445 default:
446 break;
447 }
448 ret += fprintf(fp, "\n");
449 rb_node = rb_next(rb_node);
450 }
451
452 return ret;
453}
454
Arnaldo Carvalho de Melodd2ee782010-03-11 20:12:43 -0300455size_t hist_entry__fprintf(struct hist_entry *self,
456 struct perf_session *pair_session,
457 bool show_displacement,
458 long displacement, FILE *fp,
459 u64 session_total)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200460{
461 struct sort_entry *se;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200462 u64 count, total;
463 const char *sep = symbol_conf.field_sep;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200464 size_t ret;
465
466 if (symbol_conf.exclude_other && !self->parent)
467 return 0;
468
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200469 if (pair_session) {
470 count = self->pair ? self->pair->count : 0;
471 total = pair_session->events_stats.total;
472 } else {
473 count = self->count;
Eric B Munsoneefc4652010-03-05 12:51:08 -0300474 total = session_total;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200475 }
476
477 if (total)
478 ret = percent_color_fprintf(fp, sep ? "%.2f" : " %6.2f%%",
479 (count * 100.0) / total);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200480 else
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200481 ret = fprintf(fp, sep ? "%lld" : "%12lld ", count);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200482
483 if (symbol_conf.show_nr_samples) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200484 if (sep)
Arnaldo Carvalho de Melodd2ee782010-03-11 20:12:43 -0300485 ret += fprintf(fp, "%c%lld", *sep, count);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200486 else
Arnaldo Carvalho de Melodd2ee782010-03-11 20:12:43 -0300487 ret += fprintf(fp, "%11lld", count);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200488 }
489
490 if (pair_session) {
491 char bf[32];
492 double old_percent = 0, new_percent = 0, diff;
493
494 if (total > 0)
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -0200495 old_percent = (count * 100.0) / total;
Eric B Munsoneefc4652010-03-05 12:51:08 -0300496 if (session_total > 0)
497 new_percent = (self->count * 100.0) / session_total;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200498
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -0200499 diff = new_percent - old_percent;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200500
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -0200501 if (fabs(diff) >= 0.01)
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200502 snprintf(bf, sizeof(bf), "%+4.2F%%", diff);
503 else
504 snprintf(bf, sizeof(bf), " ");
505
506 if (sep)
507 ret += fprintf(fp, "%c%s", *sep, bf);
508 else
509 ret += fprintf(fp, "%11.11s", bf);
510
511 if (show_displacement) {
512 if (displacement)
513 snprintf(bf, sizeof(bf), "%+4ld", displacement);
514 else
515 snprintf(bf, sizeof(bf), " ");
516
517 if (sep)
Arnaldo Carvalho de Melodd2ee782010-03-11 20:12:43 -0300518 ret += fprintf(fp, "%c%s", *sep, bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200519 else
Arnaldo Carvalho de Melodd2ee782010-03-11 20:12:43 -0300520 ret += fprintf(fp, "%6.6s", bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200521 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200522 }
523
524 list_for_each_entry(se, &hist_entry__sort_list, list) {
525 if (se->elide)
526 continue;
527
Arnaldo Carvalho de Melodd2ee782010-03-11 20:12:43 -0300528 ret += fprintf(fp, "%s", sep ?: " ");
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200529 ret += se->print(fp, self, se->width ? *se->width : 0);
530 }
531
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300532 return ret + fprintf(fp, "\n");
533}
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200534
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300535static size_t hist_entry__fprintf_callchain(struct hist_entry *self, FILE *fp,
536 u64 session_total)
537{
538 int left_margin = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200539
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300540 if (sort__first_dimension == SORT_COMM) {
541 struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
542 typeof(*se), list);
543 left_margin = se->width ? *se->width : 0;
544 left_margin -= thread__comm_len(self->thread);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200545 }
546
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300547 return hist_entry_callchain__fprintf(fp, self, session_total,
548 left_margin);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200549}
550
Eric B Munsoneefc4652010-03-05 12:51:08 -0300551size_t perf_session__fprintf_hists(struct rb_root *hists,
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200552 struct perf_session *pair,
Eric B Munsoneefc4652010-03-05 12:51:08 -0300553 bool show_displacement, FILE *fp,
554 u64 session_total)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200555{
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200556 struct sort_entry *se;
557 struct rb_node *nd;
558 size_t ret = 0;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200559 unsigned long position = 1;
560 long displacement = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200561 unsigned int width;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200562 const char *sep = symbol_conf.field_sep;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200563 char *col_width = symbol_conf.col_width_list_str;
564
565 init_rem_hits();
566
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200567 fprintf(fp, "# %s", pair ? "Baseline" : "Overhead");
568
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200569 if (symbol_conf.show_nr_samples) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200570 if (sep)
571 fprintf(fp, "%cSamples", *sep);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200572 else
573 fputs(" Samples ", fp);
574 }
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200575
576 if (pair) {
577 if (sep)
578 ret += fprintf(fp, "%cDelta", *sep);
579 else
580 ret += fprintf(fp, " Delta ");
581
582 if (show_displacement) {
583 if (sep)
584 ret += fprintf(fp, "%cDisplacement", *sep);
585 else
586 ret += fprintf(fp, " Displ");
587 }
588 }
589
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200590 list_for_each_entry(se, &hist_entry__sort_list, list) {
591 if (se->elide)
592 continue;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200593 if (sep) {
594 fprintf(fp, "%c%s", *sep, se->header);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200595 continue;
596 }
597 width = strlen(se->header);
598 if (se->width) {
599 if (symbol_conf.col_width_list_str) {
600 if (col_width) {
601 *se->width = atoi(col_width);
602 col_width = strchr(col_width, ',');
603 if (col_width)
604 ++col_width;
605 }
606 }
607 width = *se->width = max(*se->width, width);
608 }
609 fprintf(fp, " %*s", width, se->header);
610 }
611 fprintf(fp, "\n");
612
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200613 if (sep)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200614 goto print_entries;
615
616 fprintf(fp, "# ........");
617 if (symbol_conf.show_nr_samples)
618 fprintf(fp, " ..........");
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200619 if (pair) {
620 fprintf(fp, " ..........");
621 if (show_displacement)
622 fprintf(fp, " .....");
623 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200624 list_for_each_entry(se, &hist_entry__sort_list, list) {
625 unsigned int i;
626
627 if (se->elide)
628 continue;
629
630 fprintf(fp, " ");
631 if (se->width)
632 width = *se->width;
633 else
634 width = strlen(se->header);
635 for (i = 0; i < width; i++)
636 fprintf(fp, ".");
637 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200638
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200639 fprintf(fp, "\n#\n");
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200640
641print_entries:
Eric B Munsoneefc4652010-03-05 12:51:08 -0300642 for (nd = rb_first(hists); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200643 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
644
645 if (show_displacement) {
646 if (h->pair != NULL)
647 displacement = ((long)h->pair->position -
648 (long)position);
649 else
650 displacement = 0;
651 ++position;
652 }
Eric B Munsoneefc4652010-03-05 12:51:08 -0300653 ret += hist_entry__fprintf(h, pair, show_displacement,
654 displacement, fp, session_total);
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300655
656 if (symbol_conf.use_callchain)
657 ret += hist_entry__fprintf_callchain(h, fp, session_total);
658
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300659 if (h->ms.map == NULL && verbose > 1) {
Arnaldo Carvalho de Melo65f2ed22010-03-09 15:58:17 -0300660 __map_groups__fprintf_maps(&h->thread->mg,
661 MAP__FUNCTION, fp);
662 fprintf(fp, "%.10s end\n", graph_dotted_line);
663 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200664 }
665
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200666 free(rem_sq_bracket);
667
668 return ret;
669}