blob: e0c8a722e11f427976ddbed8250ed22db6e9c711 [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
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -030011static void perf_session__add_cpumode_count(struct hist_entry *he,
12 unsigned int cpumode, u64 count)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080013{
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -030014 switch (cpumode) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080015 case PERF_RECORD_MISC_KERNEL:
16 he->count_sys += count;
17 break;
18 case PERF_RECORD_MISC_USER:
19 he->count_us += count;
20 break;
21 case PERF_RECORD_MISC_GUEST_KERNEL:
22 he->count_guest_sys += count;
23 break;
24 case PERF_RECORD_MISC_GUEST_USER:
25 he->count_guest_us += count;
26 break;
27 default:
28 break;
29 }
30}
31
John Kacur3d1d07e2009-09-28 15:32:55 +020032/*
33 * histogram, sorted on item, collects counts
34 */
35
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -030036static struct hist_entry *hist_entry__new(struct hist_entry *template)
37{
38 size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_node) : 0;
39 struct hist_entry *self = malloc(sizeof(*self) + callchain_size);
40
41 if (self != NULL) {
42 *self = *template;
43 if (symbol_conf.use_callchain)
44 callchain_init(self->callchain);
45 }
46
47 return self;
48}
49
Eric B Munsond403d0a2010-03-05 12:51:06 -030050struct hist_entry *__perf_session__add_hist_entry(struct rb_root *hists,
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -020051 struct addr_location *al,
52 struct symbol *sym_parent,
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -030053 u64 count)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -030054{
Eric B Munsond403d0a2010-03-05 12:51:06 -030055 struct rb_node **p = &hists->rb_node;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -030056 struct rb_node *parent = NULL;
57 struct hist_entry *he;
58 struct hist_entry entry = {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -020059 .thread = al->thread,
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -030060 .ms = {
61 .map = al->map,
62 .sym = al->sym,
63 },
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -020064 .ip = al->addr,
65 .level = al->level,
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -030066 .count = count,
67 .parent = sym_parent,
68 };
69 int cmp;
70
71 while (*p != NULL) {
72 parent = *p;
73 he = rb_entry(parent, struct hist_entry, rb_node);
74
75 cmp = hist_entry__cmp(&entry, he);
76
77 if (!cmp) {
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -030078 he->count += count;
79 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -030080 }
81
82 if (cmp < 0)
83 p = &(*p)->rb_left;
84 else
85 p = &(*p)->rb_right;
86 }
87
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -030088 he = hist_entry__new(&entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -030089 if (!he)
90 return NULL;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -030091 rb_link_node(&he->rb_node, parent, p);
Eric B Munsond403d0a2010-03-05 12:51:06 -030092 rb_insert_color(&he->rb_node, hists);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -030093out:
94 perf_session__add_cpumode_count(he, al->cpumode, count);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -030095 return he;
96}
97
John Kacur3d1d07e2009-09-28 15:32:55 +020098int64_t
99hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
100{
101 struct sort_entry *se;
102 int64_t cmp = 0;
103
104 list_for_each_entry(se, &hist_entry__sort_list, list) {
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200105 cmp = se->se_cmp(left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200106 if (cmp)
107 break;
108 }
109
110 return cmp;
111}
112
113int64_t
114hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
115{
116 struct sort_entry *se;
117 int64_t cmp = 0;
118
119 list_for_each_entry(se, &hist_entry__sort_list, list) {
120 int64_t (*f)(struct hist_entry *, struct hist_entry *);
121
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200122 f = se->se_collapse ?: se->se_cmp;
John Kacur3d1d07e2009-09-28 15:32:55 +0200123
124 cmp = f(left, right);
125 if (cmp)
126 break;
127 }
128
129 return cmp;
130}
131
132void hist_entry__free(struct hist_entry *he)
133{
134 free(he);
135}
136
137/*
138 * collapse the histogram
139 */
140
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200141static void collapse__insert_entry(struct rb_root *root, struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +0200142{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200143 struct rb_node **p = &root->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200144 struct rb_node *parent = NULL;
145 struct hist_entry *iter;
146 int64_t cmp;
147
148 while (*p != NULL) {
149 parent = *p;
150 iter = rb_entry(parent, struct hist_entry, rb_node);
151
152 cmp = hist_entry__collapse(iter, he);
153
154 if (!cmp) {
155 iter->count += he->count;
156 hist_entry__free(he);
157 return;
158 }
159
160 if (cmp < 0)
161 p = &(*p)->rb_left;
162 else
163 p = &(*p)->rb_right;
164 }
165
166 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200167 rb_insert_color(&he->rb_node, root);
John Kacur3d1d07e2009-09-28 15:32:55 +0200168}
169
Eric B Munsoneefc4652010-03-05 12:51:08 -0300170void perf_session__collapse_resort(struct rb_root *hists)
John Kacur3d1d07e2009-09-28 15:32:55 +0200171{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200172 struct rb_root tmp;
John Kacur3d1d07e2009-09-28 15:32:55 +0200173 struct rb_node *next;
174 struct hist_entry *n;
175
176 if (!sort__need_collapse)
177 return;
178
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200179 tmp = RB_ROOT;
Eric B Munsoneefc4652010-03-05 12:51:08 -0300180 next = rb_first(hists);
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200181
John Kacur3d1d07e2009-09-28 15:32:55 +0200182 while (next) {
183 n = rb_entry(next, struct hist_entry, rb_node);
184 next = rb_next(&n->rb_node);
185
Eric B Munsoneefc4652010-03-05 12:51:08 -0300186 rb_erase(&n->rb_node, hists);
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200187 collapse__insert_entry(&tmp, n);
John Kacur3d1d07e2009-09-28 15:32:55 +0200188 }
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200189
Eric B Munsoneefc4652010-03-05 12:51:08 -0300190 *hists = tmp;
John Kacur3d1d07e2009-09-28 15:32:55 +0200191}
192
193/*
194 * reverse the map, sort on count.
195 */
196
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200197static void perf_session__insert_output_hist_entry(struct rb_root *root,
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -0200198 struct hist_entry *he,
199 u64 min_callchain_hits)
John Kacur3d1d07e2009-09-28 15:32:55 +0200200{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200201 struct rb_node **p = &root->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200202 struct rb_node *parent = NULL;
203 struct hist_entry *iter;
204
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200205 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melob9fb9302010-04-02 09:50:42 -0300206 callchain_param.sort(&he->sorted_chain, he->callchain,
John Kacur3d1d07e2009-09-28 15:32:55 +0200207 min_callchain_hits, &callchain_param);
208
209 while (*p != NULL) {
210 parent = *p;
211 iter = rb_entry(parent, struct hist_entry, rb_node);
212
213 if (he->count > iter->count)
214 p = &(*p)->rb_left;
215 else
216 p = &(*p)->rb_right;
217 }
218
219 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200220 rb_insert_color(&he->rb_node, root);
John Kacur3d1d07e2009-09-28 15:32:55 +0200221}
222
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300223u64 perf_session__output_resort(struct rb_root *hists, u64 total_samples)
John Kacur3d1d07e2009-09-28 15:32:55 +0200224{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200225 struct rb_root tmp;
John Kacur3d1d07e2009-09-28 15:32:55 +0200226 struct rb_node *next;
227 struct hist_entry *n;
John Kacur3d1d07e2009-09-28 15:32:55 +0200228 u64 min_callchain_hits;
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300229 u64 nr_hists = 0;
John Kacur3d1d07e2009-09-28 15:32:55 +0200230
231 min_callchain_hits =
232 total_samples * (callchain_param.min_percent / 100);
233
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200234 tmp = RB_ROOT;
Eric B Munsoneefc4652010-03-05 12:51:08 -0300235 next = rb_first(hists);
John Kacur3d1d07e2009-09-28 15:32:55 +0200236
237 while (next) {
238 n = rb_entry(next, struct hist_entry, rb_node);
239 next = rb_next(&n->rb_node);
240
Eric B Munsoneefc4652010-03-05 12:51:08 -0300241 rb_erase(&n->rb_node, hists);
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200242 perf_session__insert_output_hist_entry(&tmp, n,
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -0200243 min_callchain_hits);
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300244 ++nr_hists;
John Kacur3d1d07e2009-09-28 15:32:55 +0200245 }
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200246
Eric B Munsoneefc4652010-03-05 12:51:08 -0300247 *hists = tmp;
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300248 return nr_hists;
John Kacur3d1d07e2009-09-28 15:32:55 +0200249}
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200250
251static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
252{
253 int i;
254 int ret = fprintf(fp, " ");
255
256 for (i = 0; i < left_margin; i++)
257 ret += fprintf(fp, " ");
258
259 return ret;
260}
261
262static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
263 int left_margin)
264{
265 int i;
266 size_t ret = callchain__fprintf_left_margin(fp, left_margin);
267
268 for (i = 0; i < depth; i++)
269 if (depth_mask & (1 << i))
270 ret += fprintf(fp, "| ");
271 else
272 ret += fprintf(fp, " ");
273
274 ret += fprintf(fp, "\n");
275
276 return ret;
277}
278
279static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain,
280 int depth, int depth_mask, int count,
281 u64 total_samples, int hits,
282 int left_margin)
283{
284 int i;
285 size_t ret = 0;
286
287 ret += callchain__fprintf_left_margin(fp, left_margin);
288 for (i = 0; i < depth; i++) {
289 if (depth_mask & (1 << i))
290 ret += fprintf(fp, "|");
291 else
292 ret += fprintf(fp, " ");
293 if (!count && i == depth - 1) {
294 double percent;
295
296 percent = hits * 100.0 / total_samples;
297 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
298 } else
299 ret += fprintf(fp, "%s", " ");
300 }
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300301 if (chain->ms.sym)
302 ret += fprintf(fp, "%s\n", chain->ms.sym->name);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200303 else
304 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
305
306 return ret;
307}
308
309static struct symbol *rem_sq_bracket;
310static struct callchain_list rem_hits;
311
312static void init_rem_hits(void)
313{
314 rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
315 if (!rem_sq_bracket) {
316 fprintf(stderr, "Not enough memory to display remaining hits\n");
317 return;
318 }
319
320 strcpy(rem_sq_bracket->name, "[...]");
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300321 rem_hits.ms.sym = rem_sq_bracket;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200322}
323
324static size_t __callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
325 u64 total_samples, int depth,
326 int depth_mask, int left_margin)
327{
328 struct rb_node *node, *next;
329 struct callchain_node *child;
330 struct callchain_list *chain;
331 int new_depth_mask = depth_mask;
332 u64 new_total;
333 u64 remaining;
334 size_t ret = 0;
335 int i;
336
337 if (callchain_param.mode == CHAIN_GRAPH_REL)
338 new_total = self->children_hit;
339 else
340 new_total = total_samples;
341
342 remaining = new_total;
343
344 node = rb_first(&self->rb_root);
345 while (node) {
346 u64 cumul;
347
348 child = rb_entry(node, struct callchain_node, rb_node);
349 cumul = cumul_hits(child);
350 remaining -= cumul;
351
352 /*
353 * The depth mask manages the output of pipes that show
354 * the depth. We don't want to keep the pipes of the current
355 * level for the last child of this depth.
356 * Except if we have remaining filtered hits. They will
357 * supersede the last child
358 */
359 next = rb_next(node);
360 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
361 new_depth_mask &= ~(1 << (depth - 1));
362
363 /*
Daniel Mack3ad2f3fb2010-02-03 08:01:28 +0800364 * But we keep the older depth mask for the line separator
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200365 * to keep the level link until we reach the last child
366 */
367 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
368 left_margin);
369 i = 0;
370 list_for_each_entry(chain, &child->val, list) {
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200371 ret += ipchain__fprintf_graph(fp, chain, depth,
372 new_depth_mask, i++,
373 new_total,
374 cumul,
375 left_margin);
376 }
377 ret += __callchain__fprintf_graph(fp, child, new_total,
378 depth + 1,
379 new_depth_mask | (1 << depth),
380 left_margin);
381 node = next;
382 }
383
384 if (callchain_param.mode == CHAIN_GRAPH_REL &&
385 remaining && remaining != new_total) {
386
387 if (!rem_sq_bracket)
388 return ret;
389
390 new_depth_mask &= ~(1 << (depth - 1));
391
392 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
393 new_depth_mask, 0, new_total,
394 remaining, left_margin);
395 }
396
397 return ret;
398}
399
400static size_t callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
401 u64 total_samples, int left_margin)
402{
403 struct callchain_list *chain;
404 bool printed = false;
405 int i = 0;
406 int ret = 0;
407
408 list_for_each_entry(chain, &self->val, list) {
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200409 if (!i++ && sort__first_dimension == SORT_SYM)
410 continue;
411
412 if (!printed) {
413 ret += callchain__fprintf_left_margin(fp, left_margin);
414 ret += fprintf(fp, "|\n");
415 ret += callchain__fprintf_left_margin(fp, left_margin);
416 ret += fprintf(fp, "---");
417
418 left_margin += 3;
419 printed = true;
420 } else
421 ret += callchain__fprintf_left_margin(fp, left_margin);
422
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300423 if (chain->ms.sym)
424 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200425 else
426 ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
427 }
428
429 ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin);
430
431 return ret;
432}
433
434static size_t callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
435 u64 total_samples)
436{
437 struct callchain_list *chain;
438 size_t ret = 0;
439
440 if (!self)
441 return 0;
442
443 ret += callchain__fprintf_flat(fp, self->parent, total_samples);
444
445
446 list_for_each_entry(chain, &self->val, list) {
447 if (chain->ip >= PERF_CONTEXT_MAX)
448 continue;
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300449 if (chain->ms.sym)
450 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200451 else
452 ret += fprintf(fp, " %p\n",
453 (void *)(long)chain->ip);
454 }
455
456 return ret;
457}
458
459static size_t hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
460 u64 total_samples, int left_margin)
461{
462 struct rb_node *rb_node;
463 struct callchain_node *chain;
464 size_t ret = 0;
465
466 rb_node = rb_first(&self->sorted_chain);
467 while (rb_node) {
468 double percent;
469
470 chain = rb_entry(rb_node, struct callchain_node, rb_node);
471 percent = chain->hit * 100.0 / total_samples;
472 switch (callchain_param.mode) {
473 case CHAIN_FLAT:
474 ret += percent_color_fprintf(fp, " %6.2f%%\n",
475 percent);
476 ret += callchain__fprintf_flat(fp, chain, total_samples);
477 break;
478 case CHAIN_GRAPH_ABS: /* Falldown */
479 case CHAIN_GRAPH_REL:
480 ret += callchain__fprintf_graph(fp, chain, total_samples,
481 left_margin);
482 case CHAIN_NONE:
483 default:
484 break;
485 }
486 ret += fprintf(fp, "\n");
487 rb_node = rb_next(rb_node);
488 }
489
490 return ret;
491}
492
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300493int hist_entry__snprintf(struct hist_entry *self,
494 char *s, size_t size,
Arnaldo Carvalho de Melodd2ee782010-03-11 20:12:43 -0300495 struct perf_session *pair_session,
496 bool show_displacement,
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300497 long displacement, bool color,
Arnaldo Carvalho de Melodd2ee782010-03-11 20:12:43 -0300498 u64 session_total)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200499{
500 struct sort_entry *se;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800501 u64 count, total, count_sys, count_us, count_guest_sys, count_guest_us;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200502 const char *sep = symbol_conf.field_sep;
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300503 int ret;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200504
505 if (symbol_conf.exclude_other && !self->parent)
506 return 0;
507
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200508 if (pair_session) {
509 count = self->pair ? self->pair->count : 0;
510 total = pair_session->events_stats.total;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800511 count_sys = self->pair ? self->pair->count_sys : 0;
512 count_us = self->pair ? self->pair->count_us : 0;
513 count_guest_sys = self->pair ? self->pair->count_guest_sys : 0;
514 count_guest_us = self->pair ? self->pair->count_guest_us : 0;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200515 } else {
516 count = self->count;
Eric B Munsoneefc4652010-03-05 12:51:08 -0300517 total = session_total;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800518 count_sys = self->count_sys;
519 count_us = self->count_us;
520 count_guest_sys = self->count_guest_sys;
521 count_guest_us = self->count_guest_us;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200522 }
523
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300524 if (total) {
525 if (color)
526 ret = percent_color_snprintf(s, size,
527 sep ? "%.2f" : " %6.2f%%",
528 (count * 100.0) / total);
529 else
530 ret = snprintf(s, size, sep ? "%.2f" : " %6.2f%%",
531 (count * 100.0) / total);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800532 if (symbol_conf.show_cpu_utilization) {
533 ret += percent_color_snprintf(s + ret, size - ret,
534 sep ? "%.2f" : " %6.2f%%",
535 (count_sys * 100.0) / total);
536 ret += percent_color_snprintf(s + ret, size - ret,
537 sep ? "%.2f" : " %6.2f%%",
538 (count_us * 100.0) / total);
539 if (perf_guest) {
540 ret += percent_color_snprintf(s + ret,
541 size - ret,
542 sep ? "%.2f" : " %6.2f%%",
543 (count_guest_sys * 100.0) /
544 total);
545 ret += percent_color_snprintf(s + ret,
546 size - ret,
547 sep ? "%.2f" : " %6.2f%%",
548 (count_guest_us * 100.0) /
549 total);
550 }
551 }
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300552 } else
553 ret = snprintf(s, size, sep ? "%lld" : "%12lld ", count);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200554
555 if (symbol_conf.show_nr_samples) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200556 if (sep)
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300557 ret += snprintf(s + ret, size - ret, "%c%lld", *sep, count);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200558 else
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300559 ret += snprintf(s + ret, size - ret, "%11lld", count);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200560 }
561
562 if (pair_session) {
563 char bf[32];
564 double old_percent = 0, new_percent = 0, diff;
565
566 if (total > 0)
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -0200567 old_percent = (count * 100.0) / total;
Eric B Munsoneefc4652010-03-05 12:51:08 -0300568 if (session_total > 0)
569 new_percent = (self->count * 100.0) / session_total;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200570
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -0200571 diff = new_percent - old_percent;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200572
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -0200573 if (fabs(diff) >= 0.01)
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200574 snprintf(bf, sizeof(bf), "%+4.2F%%", diff);
575 else
576 snprintf(bf, sizeof(bf), " ");
577
578 if (sep)
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300579 ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200580 else
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300581 ret += snprintf(s + ret, size - ret, "%11.11s", bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200582
583 if (show_displacement) {
584 if (displacement)
585 snprintf(bf, sizeof(bf), "%+4ld", displacement);
586 else
587 snprintf(bf, sizeof(bf), " ");
588
589 if (sep)
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300590 ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200591 else
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300592 ret += snprintf(s + ret, size - ret, "%6.6s", bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200593 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200594 }
595
596 list_for_each_entry(se, &hist_entry__sort_list, list) {
597 if (se->elide)
598 continue;
599
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300600 ret += snprintf(s + ret, size - ret, "%s", sep ?: " ");
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200601 ret += se->se_snprintf(self, s + ret, size - ret,
602 se->se_width ? *se->se_width : 0);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200603 }
604
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300605 return ret;
606}
607
608int hist_entry__fprintf(struct hist_entry *self,
609 struct perf_session *pair_session,
610 bool show_displacement,
611 long displacement, FILE *fp,
612 u64 session_total)
613{
614 char bf[512];
615 hist_entry__snprintf(self, bf, sizeof(bf), pair_session,
616 show_displacement, displacement,
617 true, session_total);
618 return fprintf(fp, "%s\n", bf);
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300619}
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200620
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300621static size_t hist_entry__fprintf_callchain(struct hist_entry *self, FILE *fp,
622 u64 session_total)
623{
624 int left_margin = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200625
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300626 if (sort__first_dimension == SORT_COMM) {
627 struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
628 typeof(*se), list);
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200629 left_margin = se->se_width ? *se->se_width : 0;
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300630 left_margin -= thread__comm_len(self->thread);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200631 }
632
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300633 return hist_entry_callchain__fprintf(fp, self, session_total,
634 left_margin);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200635}
636
Eric B Munsoneefc4652010-03-05 12:51:08 -0300637size_t perf_session__fprintf_hists(struct rb_root *hists,
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200638 struct perf_session *pair,
Eric B Munsoneefc4652010-03-05 12:51:08 -0300639 bool show_displacement, FILE *fp,
640 u64 session_total)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200641{
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200642 struct sort_entry *se;
643 struct rb_node *nd;
644 size_t ret = 0;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200645 unsigned long position = 1;
646 long displacement = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200647 unsigned int width;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200648 const char *sep = symbol_conf.field_sep;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200649 char *col_width = symbol_conf.col_width_list_str;
650
651 init_rem_hits();
652
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200653 fprintf(fp, "# %s", pair ? "Baseline" : "Overhead");
654
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200655 if (symbol_conf.show_nr_samples) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200656 if (sep)
657 fprintf(fp, "%cSamples", *sep);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200658 else
659 fputs(" Samples ", fp);
660 }
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200661
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800662 if (symbol_conf.show_cpu_utilization) {
663 if (sep) {
664 ret += fprintf(fp, "%csys", *sep);
665 ret += fprintf(fp, "%cus", *sep);
666 if (perf_guest) {
667 ret += fprintf(fp, "%cguest sys", *sep);
668 ret += fprintf(fp, "%cguest us", *sep);
669 }
670 } else {
671 ret += fprintf(fp, " sys ");
672 ret += fprintf(fp, " us ");
673 if (perf_guest) {
674 ret += fprintf(fp, " guest sys ");
675 ret += fprintf(fp, " guest us ");
676 }
677 }
678 }
679
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200680 if (pair) {
681 if (sep)
682 ret += fprintf(fp, "%cDelta", *sep);
683 else
684 ret += fprintf(fp, " Delta ");
685
686 if (show_displacement) {
687 if (sep)
688 ret += fprintf(fp, "%cDisplacement", *sep);
689 else
690 ret += fprintf(fp, " Displ");
691 }
692 }
693
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200694 list_for_each_entry(se, &hist_entry__sort_list, list) {
695 if (se->elide)
696 continue;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200697 if (sep) {
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200698 fprintf(fp, "%c%s", *sep, se->se_header);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200699 continue;
700 }
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200701 width = strlen(se->se_header);
702 if (se->se_width) {
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200703 if (symbol_conf.col_width_list_str) {
704 if (col_width) {
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200705 *se->se_width = atoi(col_width);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200706 col_width = strchr(col_width, ',');
707 if (col_width)
708 ++col_width;
709 }
710 }
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200711 width = *se->se_width = max(*se->se_width, width);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200712 }
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200713 fprintf(fp, " %*s", width, se->se_header);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200714 }
715 fprintf(fp, "\n");
716
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200717 if (sep)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200718 goto print_entries;
719
720 fprintf(fp, "# ........");
721 if (symbol_conf.show_nr_samples)
722 fprintf(fp, " ..........");
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200723 if (pair) {
724 fprintf(fp, " ..........");
725 if (show_displacement)
726 fprintf(fp, " .....");
727 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200728 list_for_each_entry(se, &hist_entry__sort_list, list) {
729 unsigned int i;
730
731 if (se->elide)
732 continue;
733
734 fprintf(fp, " ");
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200735 if (se->se_width)
736 width = *se->se_width;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200737 else
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200738 width = strlen(se->se_header);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200739 for (i = 0; i < width; i++)
740 fprintf(fp, ".");
741 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200742
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200743 fprintf(fp, "\n#\n");
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200744
745print_entries:
Eric B Munsoneefc4652010-03-05 12:51:08 -0300746 for (nd = rb_first(hists); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200747 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
748
749 if (show_displacement) {
750 if (h->pair != NULL)
751 displacement = ((long)h->pair->position -
752 (long)position);
753 else
754 displacement = 0;
755 ++position;
756 }
Eric B Munsoneefc4652010-03-05 12:51:08 -0300757 ret += hist_entry__fprintf(h, pair, show_displacement,
758 displacement, fp, session_total);
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300759
760 if (symbol_conf.use_callchain)
761 ret += hist_entry__fprintf_callchain(h, fp, session_total);
762
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300763 if (h->ms.map == NULL && verbose > 1) {
Arnaldo Carvalho de Melo65f2ed22010-03-09 15:58:17 -0300764 __map_groups__fprintf_maps(&h->thread->mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300765 MAP__FUNCTION, verbose, fp);
Arnaldo Carvalho de Melo65f2ed22010-03-09 15:58:17 -0300766 fprintf(fp, "%.10s end\n", graph_dotted_line);
767 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200768 }
769
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200770 free(rem_sq_bracket);
771
772 return ret;
773}