blob: f0794913d575347ceb39b1fd80ccf1b8cd2e4476 [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
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300188u64 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;
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300194 u64 nr_hists = 0;
John Kacur3d1d07e2009-09-28 15:32:55 +0200195
196 min_callchain_hits =
197 total_samples * (callchain_param.min_percent / 100);
198
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200199 tmp = RB_ROOT;
Eric B Munsoneefc4652010-03-05 12:51:08 -0300200 next = rb_first(hists);
John Kacur3d1d07e2009-09-28 15:32:55 +0200201
202 while (next) {
203 n = rb_entry(next, struct hist_entry, rb_node);
204 next = rb_next(&n->rb_node);
205
Eric B Munsoneefc4652010-03-05 12:51:08 -0300206 rb_erase(&n->rb_node, hists);
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200207 perf_session__insert_output_hist_entry(&tmp, n,
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -0200208 min_callchain_hits);
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300209 ++nr_hists;
John Kacur3d1d07e2009-09-28 15:32:55 +0200210 }
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200211
Eric B Munsoneefc4652010-03-05 12:51:08 -0300212 *hists = tmp;
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300213 return nr_hists;
John Kacur3d1d07e2009-09-28 15:32:55 +0200214}
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200215
216static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
217{
218 int i;
219 int ret = fprintf(fp, " ");
220
221 for (i = 0; i < left_margin; i++)
222 ret += fprintf(fp, " ");
223
224 return ret;
225}
226
227static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
228 int left_margin)
229{
230 int i;
231 size_t ret = callchain__fprintf_left_margin(fp, left_margin);
232
233 for (i = 0; i < depth; i++)
234 if (depth_mask & (1 << i))
235 ret += fprintf(fp, "| ");
236 else
237 ret += fprintf(fp, " ");
238
239 ret += fprintf(fp, "\n");
240
241 return ret;
242}
243
244static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain,
245 int depth, int depth_mask, int count,
246 u64 total_samples, int hits,
247 int left_margin)
248{
249 int i;
250 size_t ret = 0;
251
252 ret += callchain__fprintf_left_margin(fp, left_margin);
253 for (i = 0; i < depth; i++) {
254 if (depth_mask & (1 << i))
255 ret += fprintf(fp, "|");
256 else
257 ret += fprintf(fp, " ");
258 if (!count && i == depth - 1) {
259 double percent;
260
261 percent = hits * 100.0 / total_samples;
262 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
263 } else
264 ret += fprintf(fp, "%s", " ");
265 }
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300266 if (chain->ms.sym)
267 ret += fprintf(fp, "%s\n", chain->ms.sym->name);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200268 else
269 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
270
271 return ret;
272}
273
274static struct symbol *rem_sq_bracket;
275static struct callchain_list rem_hits;
276
277static void init_rem_hits(void)
278{
279 rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
280 if (!rem_sq_bracket) {
281 fprintf(stderr, "Not enough memory to display remaining hits\n");
282 return;
283 }
284
285 strcpy(rem_sq_bracket->name, "[...]");
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300286 rem_hits.ms.sym = rem_sq_bracket;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200287}
288
289static size_t __callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
290 u64 total_samples, int depth,
291 int depth_mask, int left_margin)
292{
293 struct rb_node *node, *next;
294 struct callchain_node *child;
295 struct callchain_list *chain;
296 int new_depth_mask = depth_mask;
297 u64 new_total;
298 u64 remaining;
299 size_t ret = 0;
300 int i;
301
302 if (callchain_param.mode == CHAIN_GRAPH_REL)
303 new_total = self->children_hit;
304 else
305 new_total = total_samples;
306
307 remaining = new_total;
308
309 node = rb_first(&self->rb_root);
310 while (node) {
311 u64 cumul;
312
313 child = rb_entry(node, struct callchain_node, rb_node);
314 cumul = cumul_hits(child);
315 remaining -= cumul;
316
317 /*
318 * The depth mask manages the output of pipes that show
319 * the depth. We don't want to keep the pipes of the current
320 * level for the last child of this depth.
321 * Except if we have remaining filtered hits. They will
322 * supersede the last child
323 */
324 next = rb_next(node);
325 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
326 new_depth_mask &= ~(1 << (depth - 1));
327
328 /*
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800329 * But we keep the older depth mask for the line separator
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200330 * to keep the level link until we reach the last child
331 */
332 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
333 left_margin);
334 i = 0;
335 list_for_each_entry(chain, &child->val, list) {
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200336 ret += ipchain__fprintf_graph(fp, chain, depth,
337 new_depth_mask, i++,
338 new_total,
339 cumul,
340 left_margin);
341 }
342 ret += __callchain__fprintf_graph(fp, child, new_total,
343 depth + 1,
344 new_depth_mask | (1 << depth),
345 left_margin);
346 node = next;
347 }
348
349 if (callchain_param.mode == CHAIN_GRAPH_REL &&
350 remaining && remaining != new_total) {
351
352 if (!rem_sq_bracket)
353 return ret;
354
355 new_depth_mask &= ~(1 << (depth - 1));
356
357 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
358 new_depth_mask, 0, new_total,
359 remaining, left_margin);
360 }
361
362 return ret;
363}
364
365static size_t callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
366 u64 total_samples, int left_margin)
367{
368 struct callchain_list *chain;
369 bool printed = false;
370 int i = 0;
371 int ret = 0;
372
373 list_for_each_entry(chain, &self->val, list) {
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200374 if (!i++ && sort__first_dimension == SORT_SYM)
375 continue;
376
377 if (!printed) {
378 ret += callchain__fprintf_left_margin(fp, left_margin);
379 ret += fprintf(fp, "|\n");
380 ret += callchain__fprintf_left_margin(fp, left_margin);
381 ret += fprintf(fp, "---");
382
383 left_margin += 3;
384 printed = true;
385 } else
386 ret += callchain__fprintf_left_margin(fp, left_margin);
387
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300388 if (chain->ms.sym)
389 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200390 else
391 ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
392 }
393
394 ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin);
395
396 return ret;
397}
398
399static size_t callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
400 u64 total_samples)
401{
402 struct callchain_list *chain;
403 size_t ret = 0;
404
405 if (!self)
406 return 0;
407
408 ret += callchain__fprintf_flat(fp, self->parent, total_samples);
409
410
411 list_for_each_entry(chain, &self->val, list) {
412 if (chain->ip >= PERF_CONTEXT_MAX)
413 continue;
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300414 if (chain->ms.sym)
415 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200416 else
417 ret += fprintf(fp, " %p\n",
418 (void *)(long)chain->ip);
419 }
420
421 return ret;
422}
423
424static size_t hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
425 u64 total_samples, int left_margin)
426{
427 struct rb_node *rb_node;
428 struct callchain_node *chain;
429 size_t ret = 0;
430
431 rb_node = rb_first(&self->sorted_chain);
432 while (rb_node) {
433 double percent;
434
435 chain = rb_entry(rb_node, struct callchain_node, rb_node);
436 percent = chain->hit * 100.0 / total_samples;
437 switch (callchain_param.mode) {
438 case CHAIN_FLAT:
439 ret += percent_color_fprintf(fp, " %6.2f%%\n",
440 percent);
441 ret += callchain__fprintf_flat(fp, chain, total_samples);
442 break;
443 case CHAIN_GRAPH_ABS: /* Falldown */
444 case CHAIN_GRAPH_REL:
445 ret += callchain__fprintf_graph(fp, chain, total_samples,
446 left_margin);
447 case CHAIN_NONE:
448 default:
449 break;
450 }
451 ret += fprintf(fp, "\n");
452 rb_node = rb_next(rb_node);
453 }
454
455 return ret;
456}
457
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300458int hist_entry__snprintf(struct hist_entry *self,
459 char *s, size_t size,
Arnaldo Carvalho de Melodd2ee782010-03-11 20:12:43 -0300460 struct perf_session *pair_session,
461 bool show_displacement,
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300462 long displacement, bool color,
Arnaldo Carvalho de Melodd2ee782010-03-11 20:12:43 -0300463 u64 session_total)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200464{
465 struct sort_entry *se;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200466 u64 count, total;
467 const char *sep = symbol_conf.field_sep;
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300468 int ret;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200469
470 if (symbol_conf.exclude_other && !self->parent)
471 return 0;
472
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200473 if (pair_session) {
474 count = self->pair ? self->pair->count : 0;
475 total = pair_session->events_stats.total;
476 } else {
477 count = self->count;
Eric B Munsoneefc4652010-03-05 12:51:08 -0300478 total = session_total;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200479 }
480
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300481 if (total) {
482 if (color)
483 ret = percent_color_snprintf(s, size,
484 sep ? "%.2f" : " %6.2f%%",
485 (count * 100.0) / total);
486 else
487 ret = snprintf(s, size, sep ? "%.2f" : " %6.2f%%",
488 (count * 100.0) / total);
489 } else
490 ret = snprintf(s, size, sep ? "%lld" : "%12lld ", count);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200491
492 if (symbol_conf.show_nr_samples) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200493 if (sep)
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300494 ret += snprintf(s + ret, size - ret, "%c%lld", *sep, count);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200495 else
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300496 ret += snprintf(s + ret, size - ret, "%11lld", count);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200497 }
498
499 if (pair_session) {
500 char bf[32];
501 double old_percent = 0, new_percent = 0, diff;
502
503 if (total > 0)
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -0200504 old_percent = (count * 100.0) / total;
Eric B Munsoneefc4652010-03-05 12:51:08 -0300505 if (session_total > 0)
506 new_percent = (self->count * 100.0) / session_total;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200507
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -0200508 diff = new_percent - old_percent;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200509
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -0200510 if (fabs(diff) >= 0.01)
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200511 snprintf(bf, sizeof(bf), "%+4.2F%%", diff);
512 else
513 snprintf(bf, sizeof(bf), " ");
514
515 if (sep)
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300516 ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200517 else
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300518 ret += snprintf(s + ret, size - ret, "%11.11s", bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200519
520 if (show_displacement) {
521 if (displacement)
522 snprintf(bf, sizeof(bf), "%+4ld", displacement);
523 else
524 snprintf(bf, sizeof(bf), " ");
525
526 if (sep)
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300527 ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200528 else
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300529 ret += snprintf(s + ret, size - ret, "%6.6s", bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200530 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200531 }
532
533 list_for_each_entry(se, &hist_entry__sort_list, list) {
534 if (se->elide)
535 continue;
536
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300537 ret += snprintf(s + ret, size - ret, "%s", sep ?: " ");
538 ret += se->snprintf(self, s + ret, size - ret,
539 se->width ? *se->width : 0);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200540 }
541
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300542 return ret;
543}
544
545int hist_entry__fprintf(struct hist_entry *self,
546 struct perf_session *pair_session,
547 bool show_displacement,
548 long displacement, FILE *fp,
549 u64 session_total)
550{
551 char bf[512];
552 hist_entry__snprintf(self, bf, sizeof(bf), pair_session,
553 show_displacement, displacement,
554 true, session_total);
555 return fprintf(fp, "%s\n", bf);
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300556}
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200557
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300558static size_t hist_entry__fprintf_callchain(struct hist_entry *self, FILE *fp,
559 u64 session_total)
560{
561 int left_margin = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200562
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300563 if (sort__first_dimension == SORT_COMM) {
564 struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
565 typeof(*se), list);
566 left_margin = se->width ? *se->width : 0;
567 left_margin -= thread__comm_len(self->thread);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200568 }
569
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300570 return hist_entry_callchain__fprintf(fp, self, session_total,
571 left_margin);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200572}
573
Eric B Munsoneefc4652010-03-05 12:51:08 -0300574size_t perf_session__fprintf_hists(struct rb_root *hists,
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200575 struct perf_session *pair,
Eric B Munsoneefc4652010-03-05 12:51:08 -0300576 bool show_displacement, FILE *fp,
577 u64 session_total)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200578{
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200579 struct sort_entry *se;
580 struct rb_node *nd;
581 size_t ret = 0;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200582 unsigned long position = 1;
583 long displacement = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200584 unsigned int width;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200585 const char *sep = symbol_conf.field_sep;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200586 char *col_width = symbol_conf.col_width_list_str;
587
588 init_rem_hits();
589
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200590 fprintf(fp, "# %s", pair ? "Baseline" : "Overhead");
591
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200592 if (symbol_conf.show_nr_samples) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200593 if (sep)
594 fprintf(fp, "%cSamples", *sep);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200595 else
596 fputs(" Samples ", fp);
597 }
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200598
599 if (pair) {
600 if (sep)
601 ret += fprintf(fp, "%cDelta", *sep);
602 else
603 ret += fprintf(fp, " Delta ");
604
605 if (show_displacement) {
606 if (sep)
607 ret += fprintf(fp, "%cDisplacement", *sep);
608 else
609 ret += fprintf(fp, " Displ");
610 }
611 }
612
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200613 list_for_each_entry(se, &hist_entry__sort_list, list) {
614 if (se->elide)
615 continue;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200616 if (sep) {
617 fprintf(fp, "%c%s", *sep, se->header);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200618 continue;
619 }
620 width = strlen(se->header);
621 if (se->width) {
622 if (symbol_conf.col_width_list_str) {
623 if (col_width) {
624 *se->width = atoi(col_width);
625 col_width = strchr(col_width, ',');
626 if (col_width)
627 ++col_width;
628 }
629 }
630 width = *se->width = max(*se->width, width);
631 }
632 fprintf(fp, " %*s", width, se->header);
633 }
634 fprintf(fp, "\n");
635
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200636 if (sep)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200637 goto print_entries;
638
639 fprintf(fp, "# ........");
640 if (symbol_conf.show_nr_samples)
641 fprintf(fp, " ..........");
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200642 if (pair) {
643 fprintf(fp, " ..........");
644 if (show_displacement)
645 fprintf(fp, " .....");
646 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200647 list_for_each_entry(se, &hist_entry__sort_list, list) {
648 unsigned int i;
649
650 if (se->elide)
651 continue;
652
653 fprintf(fp, " ");
654 if (se->width)
655 width = *se->width;
656 else
657 width = strlen(se->header);
658 for (i = 0; i < width; i++)
659 fprintf(fp, ".");
660 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200661
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200662 fprintf(fp, "\n#\n");
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200663
664print_entries:
Eric B Munsoneefc4652010-03-05 12:51:08 -0300665 for (nd = rb_first(hists); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200666 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
667
668 if (show_displacement) {
669 if (h->pair != NULL)
670 displacement = ((long)h->pair->position -
671 (long)position);
672 else
673 displacement = 0;
674 ++position;
675 }
Eric B Munsoneefc4652010-03-05 12:51:08 -0300676 ret += hist_entry__fprintf(h, pair, show_displacement,
677 displacement, fp, session_total);
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300678
679 if (symbol_conf.use_callchain)
680 ret += hist_entry__fprintf_callchain(h, fp, session_total);
681
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300682 if (h->ms.map == NULL && verbose > 1) {
Arnaldo Carvalho de Melo65f2ed22010-03-09 15:58:17 -0300683 __map_groups__fprintf_maps(&h->thread->mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300684 MAP__FUNCTION, verbose, fp);
Arnaldo Carvalho de Melo65f2ed22010-03-09 15:58:17 -0300685 fprintf(fp, "%.10s end\n", graph_dotted_line);
686 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200687 }
688
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200689 free(rem_sq_bracket);
690
691 return ret;
692}