blob: 3f437236f19333212cc6ca01c21fa6127f877c29 [file] [log] [blame]
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001#include "annotate.h"
Frederic Weisbecker8a0ecfb2010-05-13 19:47:16 +02002#include "util.h"
Frederic Weisbecker598357e2010-05-21 12:48:39 +02003#include "build-id.h"
John Kacur3d1d07e2009-09-28 15:32:55 +02004#include "hist.h"
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -02005#include "session.h"
6#include "sort.h"
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -02007#include <math.h>
John Kacur3d1d07e2009-09-28 15:32:55 +02008
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -03009enum hist_filter {
10 HIST_FILTER__DSO,
11 HIST_FILTER__THREAD,
12 HIST_FILTER__PARENT,
13};
14
John Kacur3d1d07e2009-09-28 15:32:55 +020015struct callchain_param callchain_param = {
16 .mode = CHAIN_GRAPH_REL,
17 .min_percent = 0.5
18};
19
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030020u16 hists__col_len(struct hists *self, enum hist_column col)
21{
22 return self->col_len[col];
23}
24
25void hists__set_col_len(struct hists *self, enum hist_column col, u16 len)
26{
27 self->col_len[col] = len;
28}
29
30bool hists__new_col_len(struct hists *self, enum hist_column col, u16 len)
31{
32 if (len > hists__col_len(self, col)) {
33 hists__set_col_len(self, col, len);
34 return true;
35 }
36 return false;
37}
38
39static void hists__reset_col_len(struct hists *self)
40{
41 enum hist_column col;
42
43 for (col = 0; col < HISTC_NR_COLS; ++col)
44 hists__set_col_len(self, col, 0);
45}
46
47static void hists__calc_col_len(struct hists *self, struct hist_entry *h)
48{
49 u16 len;
50
51 if (h->ms.sym)
52 hists__new_col_len(self, HISTC_SYMBOL, h->ms.sym->namelen);
53
54 len = thread__comm_len(h->thread);
55 if (hists__new_col_len(self, HISTC_COMM, len))
56 hists__set_col_len(self, HISTC_THREAD, len + 6);
57
58 if (h->ms.map) {
59 len = dso__name_len(h->ms.map->dso);
60 hists__new_col_len(self, HISTC_DSO, len);
61 }
62}
63
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -030064static void hist_entry__add_cpumode_period(struct hist_entry *self,
65 unsigned int cpumode, u64 period)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080066{
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -030067 switch (cpumode) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080068 case PERF_RECORD_MISC_KERNEL:
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -030069 self->period_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080070 break;
71 case PERF_RECORD_MISC_USER:
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -030072 self->period_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080073 break;
74 case PERF_RECORD_MISC_GUEST_KERNEL:
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -030075 self->period_guest_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080076 break;
77 case PERF_RECORD_MISC_GUEST_USER:
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -030078 self->period_guest_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080079 break;
80 default:
81 break;
82 }
83}
84
John Kacur3d1d07e2009-09-28 15:32:55 +020085/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -030086 * histogram, sorted on item, collects periods
John Kacur3d1d07e2009-09-28 15:32:55 +020087 */
88
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -030089static struct hist_entry *hist_entry__new(struct hist_entry *template)
90{
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +020091 size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -030092 struct hist_entry *self = malloc(sizeof(*self) + callchain_size);
93
94 if (self != NULL) {
95 *self = *template;
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -030096 self->nr_events = 1;
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -030097 if (self->ms.map)
98 self->ms.map->referenced = true;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -030099 if (symbol_conf.use_callchain)
100 callchain_init(self->callchain);
101 }
102
103 return self;
104}
105
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300106static void hists__inc_nr_entries(struct hists *self, struct hist_entry *h)
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300107{
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300108 if (!h->filtered) {
109 hists__calc_col_len(self, h);
110 ++self->nr_entries;
111 }
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300112}
113
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300114static u8 symbol__parent_filter(const struct symbol *parent)
115{
116 if (symbol_conf.exclude_other && parent == NULL)
117 return 1 << HIST_FILTER__PARENT;
118 return 0;
119}
120
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300121struct hist_entry *__hists__add_entry(struct hists *self,
122 struct addr_location *al,
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300123 struct symbol *sym_parent, u64 period)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300124{
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300125 struct rb_node **p = &self->entries.rb_node;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300126 struct rb_node *parent = NULL;
127 struct hist_entry *he;
128 struct hist_entry entry = {
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200129 .thread = al->thread,
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300130 .ms = {
131 .map = al->map,
132 .sym = al->sym,
133 },
Arun Sharmaf60f3592010-06-04 11:27:10 -0300134 .cpu = al->cpu,
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200135 .ip = al->addr,
136 .level = al->level,
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300137 .period = period,
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300138 .parent = sym_parent,
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300139 .filtered = symbol__parent_filter(sym_parent),
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300140 };
141 int cmp;
142
143 while (*p != NULL) {
144 parent = *p;
145 he = rb_entry(parent, struct hist_entry, rb_node);
146
147 cmp = hist_entry__cmp(&entry, he);
148
149 if (!cmp) {
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300150 he->period += period;
151 ++he->nr_events;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300152 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300153 }
154
155 if (cmp < 0)
156 p = &(*p)->rb_left;
157 else
158 p = &(*p)->rb_right;
159 }
160
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300161 he = hist_entry__new(&entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300162 if (!he)
163 return NULL;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300164 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300165 rb_insert_color(&he->rb_node, &self->entries);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300166 hists__inc_nr_entries(self, he);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300167out:
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300168 hist_entry__add_cpumode_period(he, al->cpumode, period);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300169 return he;
170}
171
John Kacur3d1d07e2009-09-28 15:32:55 +0200172int64_t
173hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
174{
175 struct sort_entry *se;
176 int64_t cmp = 0;
177
178 list_for_each_entry(se, &hist_entry__sort_list, list) {
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200179 cmp = se->se_cmp(left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200180 if (cmp)
181 break;
182 }
183
184 return cmp;
185}
186
187int64_t
188hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
189{
190 struct sort_entry *se;
191 int64_t cmp = 0;
192
193 list_for_each_entry(se, &hist_entry__sort_list, list) {
194 int64_t (*f)(struct hist_entry *, struct hist_entry *);
195
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200196 f = se->se_collapse ?: se->se_cmp;
John Kacur3d1d07e2009-09-28 15:32:55 +0200197
198 cmp = f(left, right);
199 if (cmp)
200 break;
201 }
202
203 return cmp;
204}
205
206void hist_entry__free(struct hist_entry *he)
207{
208 free(he);
209}
210
211/*
212 * collapse the histogram
213 */
214
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100215static bool hists__collapse_insert_entry(struct hists *self,
216 struct rb_root *root,
217 struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +0200218{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200219 struct rb_node **p = &root->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200220 struct rb_node *parent = NULL;
221 struct hist_entry *iter;
222 int64_t cmp;
223
224 while (*p != NULL) {
225 parent = *p;
226 iter = rb_entry(parent, struct hist_entry, rb_node);
227
228 cmp = hist_entry__collapse(iter, he);
229
230 if (!cmp) {
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300231 iter->period += he->period;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100232 if (symbol_conf.use_callchain) {
233 callchain_cursor_reset(&self->callchain_cursor);
234 callchain_merge(&self->callchain_cursor, iter->callchain,
235 he->callchain);
236 }
John Kacur3d1d07e2009-09-28 15:32:55 +0200237 hist_entry__free(he);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300238 return false;
John Kacur3d1d07e2009-09-28 15:32:55 +0200239 }
240
241 if (cmp < 0)
242 p = &(*p)->rb_left;
243 else
244 p = &(*p)->rb_right;
245 }
246
247 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200248 rb_insert_color(&he->rb_node, root);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300249 return true;
John Kacur3d1d07e2009-09-28 15:32:55 +0200250}
251
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300252void hists__collapse_resort(struct hists *self)
John Kacur3d1d07e2009-09-28 15:32:55 +0200253{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200254 struct rb_root tmp;
John Kacur3d1d07e2009-09-28 15:32:55 +0200255 struct rb_node *next;
256 struct hist_entry *n;
257
258 if (!sort__need_collapse)
259 return;
260
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200261 tmp = RB_ROOT;
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300262 next = rb_first(&self->entries);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300263 self->nr_entries = 0;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300264 hists__reset_col_len(self);
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200265
John Kacur3d1d07e2009-09-28 15:32:55 +0200266 while (next) {
267 n = rb_entry(next, struct hist_entry, rb_node);
268 next = rb_next(&n->rb_node);
269
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300270 rb_erase(&n->rb_node, &self->entries);
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100271 if (hists__collapse_insert_entry(self, &tmp, n))
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300272 hists__inc_nr_entries(self, n);
John Kacur3d1d07e2009-09-28 15:32:55 +0200273 }
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200274
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300275 self->entries = tmp;
John Kacur3d1d07e2009-09-28 15:32:55 +0200276}
277
278/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300279 * reverse the map, sort on period.
John Kacur3d1d07e2009-09-28 15:32:55 +0200280 */
281
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300282static void __hists__insert_output_entry(struct rb_root *entries,
283 struct hist_entry *he,
284 u64 min_callchain_hits)
John Kacur3d1d07e2009-09-28 15:32:55 +0200285{
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300286 struct rb_node **p = &entries->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200287 struct rb_node *parent = NULL;
288 struct hist_entry *iter;
289
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200290 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melob9fb9302010-04-02 09:50:42 -0300291 callchain_param.sort(&he->sorted_chain, he->callchain,
John Kacur3d1d07e2009-09-28 15:32:55 +0200292 min_callchain_hits, &callchain_param);
293
294 while (*p != NULL) {
295 parent = *p;
296 iter = rb_entry(parent, struct hist_entry, rb_node);
297
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300298 if (he->period > iter->period)
John Kacur3d1d07e2009-09-28 15:32:55 +0200299 p = &(*p)->rb_left;
300 else
301 p = &(*p)->rb_right;
302 }
303
304 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300305 rb_insert_color(&he->rb_node, entries);
John Kacur3d1d07e2009-09-28 15:32:55 +0200306}
307
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300308void hists__output_resort(struct hists *self)
John Kacur3d1d07e2009-09-28 15:32:55 +0200309{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200310 struct rb_root tmp;
John Kacur3d1d07e2009-09-28 15:32:55 +0200311 struct rb_node *next;
312 struct hist_entry *n;
John Kacur3d1d07e2009-09-28 15:32:55 +0200313 u64 min_callchain_hits;
314
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -0300315 min_callchain_hits = self->stats.total_period * (callchain_param.min_percent / 100);
John Kacur3d1d07e2009-09-28 15:32:55 +0200316
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200317 tmp = RB_ROOT;
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300318 next = rb_first(&self->entries);
John Kacur3d1d07e2009-09-28 15:32:55 +0200319
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300320 self->nr_entries = 0;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300321 hists__reset_col_len(self);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300322
John Kacur3d1d07e2009-09-28 15:32:55 +0200323 while (next) {
324 n = rb_entry(next, struct hist_entry, rb_node);
325 next = rb_next(&n->rb_node);
326
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300327 rb_erase(&n->rb_node, &self->entries);
328 __hists__insert_output_entry(&tmp, n, min_callchain_hits);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300329 hists__inc_nr_entries(self, n);
John Kacur3d1d07e2009-09-28 15:32:55 +0200330 }
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200331
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300332 self->entries = tmp;
John Kacur3d1d07e2009-09-28 15:32:55 +0200333}
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200334
335static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
336{
337 int i;
338 int ret = fprintf(fp, " ");
339
340 for (i = 0; i < left_margin; i++)
341 ret += fprintf(fp, " ");
342
343 return ret;
344}
345
346static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
347 int left_margin)
348{
349 int i;
350 size_t ret = callchain__fprintf_left_margin(fp, left_margin);
351
352 for (i = 0; i < depth; i++)
353 if (depth_mask & (1 << i))
354 ret += fprintf(fp, "| ");
355 else
356 ret += fprintf(fp, " ");
357
358 ret += fprintf(fp, "\n");
359
360 return ret;
361}
362
363static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain,
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300364 int depth, int depth_mask, int period,
Frederic Weisbeckerd425de52011-01-03 16:13:11 +0100365 u64 total_samples, u64 hits,
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200366 int left_margin)
367{
368 int i;
369 size_t ret = 0;
370
371 ret += callchain__fprintf_left_margin(fp, left_margin);
372 for (i = 0; i < depth; i++) {
373 if (depth_mask & (1 << i))
374 ret += fprintf(fp, "|");
375 else
376 ret += fprintf(fp, " ");
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300377 if (!period && i == depth - 1) {
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200378 double percent;
379
380 percent = hits * 100.0 / total_samples;
381 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
382 } else
383 ret += fprintf(fp, "%s", " ");
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 return ret;
391}
392
393static struct symbol *rem_sq_bracket;
394static struct callchain_list rem_hits;
395
396static void init_rem_hits(void)
397{
398 rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
399 if (!rem_sq_bracket) {
400 fprintf(stderr, "Not enough memory to display remaining hits\n");
401 return;
402 }
403
404 strcpy(rem_sq_bracket->name, "[...]");
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300405 rem_hits.ms.sym = rem_sq_bracket;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200406}
407
408static size_t __callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
409 u64 total_samples, int depth,
410 int depth_mask, int left_margin)
411{
412 struct rb_node *node, *next;
413 struct callchain_node *child;
414 struct callchain_list *chain;
415 int new_depth_mask = depth_mask;
416 u64 new_total;
417 u64 remaining;
418 size_t ret = 0;
419 int i;
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300420 uint entries_printed = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200421
422 if (callchain_param.mode == CHAIN_GRAPH_REL)
423 new_total = self->children_hit;
424 else
425 new_total = total_samples;
426
427 remaining = new_total;
428
429 node = rb_first(&self->rb_root);
430 while (node) {
431 u64 cumul;
432
433 child = rb_entry(node, struct callchain_node, rb_node);
Frederic Weisbeckerf08c3152011-01-14 04:51:59 +0100434 cumul = callchain_cumul_hits(child);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200435 remaining -= cumul;
436
437 /*
438 * The depth mask manages the output of pipes that show
439 * the depth. We don't want to keep the pipes of the current
440 * level for the last child of this depth.
441 * Except if we have remaining filtered hits. They will
442 * supersede the last child
443 */
444 next = rb_next(node);
445 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
446 new_depth_mask &= ~(1 << (depth - 1));
447
448 /*
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800449 * But we keep the older depth mask for the line separator
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200450 * to keep the level link until we reach the last child
451 */
452 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
453 left_margin);
454 i = 0;
455 list_for_each_entry(chain, &child->val, list) {
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200456 ret += ipchain__fprintf_graph(fp, chain, depth,
457 new_depth_mask, i++,
458 new_total,
459 cumul,
460 left_margin);
461 }
462 ret += __callchain__fprintf_graph(fp, child, new_total,
463 depth + 1,
464 new_depth_mask | (1 << depth),
465 left_margin);
466 node = next;
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300467 if (++entries_printed == callchain_param.print_limit)
468 break;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200469 }
470
471 if (callchain_param.mode == CHAIN_GRAPH_REL &&
472 remaining && remaining != new_total) {
473
474 if (!rem_sq_bracket)
475 return ret;
476
477 new_depth_mask &= ~(1 << (depth - 1));
478
479 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
480 new_depth_mask, 0, new_total,
481 remaining, left_margin);
482 }
483
484 return ret;
485}
486
487static size_t callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
488 u64 total_samples, int left_margin)
489{
490 struct callchain_list *chain;
491 bool printed = false;
492 int i = 0;
493 int ret = 0;
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300494 u32 entries_printed = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200495
496 list_for_each_entry(chain, &self->val, list) {
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200497 if (!i++ && sort__first_dimension == SORT_SYM)
498 continue;
499
500 if (!printed) {
501 ret += callchain__fprintf_left_margin(fp, left_margin);
502 ret += fprintf(fp, "|\n");
503 ret += callchain__fprintf_left_margin(fp, left_margin);
504 ret += fprintf(fp, "---");
505
506 left_margin += 3;
507 printed = true;
508 } else
509 ret += callchain__fprintf_left_margin(fp, left_margin);
510
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300511 if (chain->ms.sym)
512 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200513 else
514 ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300515
516 if (++entries_printed == callchain_param.print_limit)
517 break;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200518 }
519
520 ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin);
521
522 return ret;
523}
524
525static size_t callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
526 u64 total_samples)
527{
528 struct callchain_list *chain;
529 size_t ret = 0;
530
531 if (!self)
532 return 0;
533
534 ret += callchain__fprintf_flat(fp, self->parent, total_samples);
535
536
537 list_for_each_entry(chain, &self->val, list) {
538 if (chain->ip >= PERF_CONTEXT_MAX)
539 continue;
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300540 if (chain->ms.sym)
541 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200542 else
543 ret += fprintf(fp, " %p\n",
544 (void *)(long)chain->ip);
545 }
546
547 return ret;
548}
549
550static size_t hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
551 u64 total_samples, int left_margin)
552{
553 struct rb_node *rb_node;
554 struct callchain_node *chain;
555 size_t ret = 0;
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300556 u32 entries_printed = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200557
558 rb_node = rb_first(&self->sorted_chain);
559 while (rb_node) {
560 double percent;
561
562 chain = rb_entry(rb_node, struct callchain_node, rb_node);
563 percent = chain->hit * 100.0 / total_samples;
564 switch (callchain_param.mode) {
565 case CHAIN_FLAT:
566 ret += percent_color_fprintf(fp, " %6.2f%%\n",
567 percent);
568 ret += callchain__fprintf_flat(fp, chain, total_samples);
569 break;
570 case CHAIN_GRAPH_ABS: /* Falldown */
571 case CHAIN_GRAPH_REL:
572 ret += callchain__fprintf_graph(fp, chain, total_samples,
573 left_margin);
574 case CHAIN_NONE:
575 default:
576 break;
577 }
578 ret += fprintf(fp, "\n");
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300579 if (++entries_printed == callchain_param.print_limit)
580 break;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200581 rb_node = rb_next(rb_node);
582 }
583
584 return ret;
585}
586
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300587int hist_entry__snprintf(struct hist_entry *self, char *s, size_t size,
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300588 struct hists *hists, struct hists *pair_hists,
589 bool show_displacement, long displacement,
590 bool color, u64 session_total)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200591{
592 struct sort_entry *se;
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300593 u64 period, total, period_sys, period_us, period_guest_sys, period_guest_us;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200594 const char *sep = symbol_conf.field_sep;
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300595 int ret;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200596
597 if (symbol_conf.exclude_other && !self->parent)
598 return 0;
599
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300600 if (pair_hists) {
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300601 period = self->pair ? self->pair->period : 0;
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -0300602 total = pair_hists->stats.total_period;
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300603 period_sys = self->pair ? self->pair->period_sys : 0;
604 period_us = self->pair ? self->pair->period_us : 0;
605 period_guest_sys = self->pair ? self->pair->period_guest_sys : 0;
606 period_guest_us = self->pair ? self->pair->period_guest_us : 0;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200607 } else {
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300608 period = self->period;
Eric B Munsoneefc4652010-03-05 12:51:08 -0300609 total = session_total;
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300610 period_sys = self->period_sys;
611 period_us = self->period_us;
612 period_guest_sys = self->period_guest_sys;
613 period_guest_us = self->period_guest_us;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200614 }
615
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300616 if (total) {
617 if (color)
618 ret = percent_color_snprintf(s, size,
619 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300620 (period * 100.0) / total);
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300621 else
622 ret = snprintf(s, size, sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300623 (period * 100.0) / total);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800624 if (symbol_conf.show_cpu_utilization) {
625 ret += percent_color_snprintf(s + ret, size - ret,
626 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300627 (period_sys * 100.0) / total);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800628 ret += percent_color_snprintf(s + ret, size - ret,
629 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300630 (period_us * 100.0) / total);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800631 if (perf_guest) {
632 ret += percent_color_snprintf(s + ret,
633 size - ret,
634 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300635 (period_guest_sys * 100.0) /
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800636 total);
637 ret += percent_color_snprintf(s + ret,
638 size - ret,
639 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300640 (period_guest_us * 100.0) /
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800641 total);
642 }
643 }
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300644 } else
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200645 ret = snprintf(s, size, sep ? "%" PRIu64 : "%12" PRIu64 " ", period);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200646
647 if (symbol_conf.show_nr_samples) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200648 if (sep)
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200649 ret += snprintf(s + ret, size - ret, "%c%" PRIu64, *sep, period);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200650 else
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200651 ret += snprintf(s + ret, size - ret, "%11" PRIu64, period);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200652 }
653
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300654 if (pair_hists) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200655 char bf[32];
656 double old_percent = 0, new_percent = 0, diff;
657
658 if (total > 0)
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300659 old_percent = (period * 100.0) / total;
Eric B Munsoneefc4652010-03-05 12:51:08 -0300660 if (session_total > 0)
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300661 new_percent = (self->period * 100.0) / session_total;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200662
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -0200663 diff = new_percent - old_percent;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200664
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -0200665 if (fabs(diff) >= 0.01)
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200666 snprintf(bf, sizeof(bf), "%+4.2F%%", diff);
667 else
668 snprintf(bf, sizeof(bf), " ");
669
670 if (sep)
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300671 ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200672 else
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300673 ret += snprintf(s + ret, size - ret, "%11.11s", bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200674
675 if (show_displacement) {
676 if (displacement)
677 snprintf(bf, sizeof(bf), "%+4ld", displacement);
678 else
679 snprintf(bf, sizeof(bf), " ");
680
681 if (sep)
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300682 ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200683 else
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300684 ret += snprintf(s + ret, size - ret, "%6.6s", bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200685 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200686 }
687
688 list_for_each_entry(se, &hist_entry__sort_list, list) {
689 if (se->elide)
690 continue;
691
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300692 ret += snprintf(s + ret, size - ret, "%s", sep ?: " ");
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200693 ret += se->se_snprintf(self, s + ret, size - ret,
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300694 hists__col_len(hists, se->se_width_idx));
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200695 }
696
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300697 return ret;
698}
699
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300700int hist_entry__fprintf(struct hist_entry *self, struct hists *hists,
701 struct hists *pair_hists, bool show_displacement,
702 long displacement, FILE *fp, u64 session_total)
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300703{
704 char bf[512];
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300705 hist_entry__snprintf(self, bf, sizeof(bf), hists, pair_hists,
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300706 show_displacement, displacement,
707 true, session_total);
708 return fprintf(fp, "%s\n", bf);
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300709}
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200710
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300711static size_t hist_entry__fprintf_callchain(struct hist_entry *self,
712 struct hists *hists, FILE *fp,
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300713 u64 session_total)
714{
715 int left_margin = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200716
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300717 if (sort__first_dimension == SORT_COMM) {
718 struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
719 typeof(*se), list);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300720 left_margin = hists__col_len(hists, se->se_width_idx);
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300721 left_margin -= thread__comm_len(self->thread);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200722 }
723
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300724 return hist_entry_callchain__fprintf(fp, self, session_total,
725 left_margin);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200726}
727
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300728size_t hists__fprintf(struct hists *self, struct hists *pair,
729 bool show_displacement, FILE *fp)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200730{
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200731 struct sort_entry *se;
732 struct rb_node *nd;
733 size_t ret = 0;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200734 unsigned long position = 1;
735 long displacement = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200736 unsigned int width;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200737 const char *sep = symbol_conf.field_sep;
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -0300738 const char *col_width = symbol_conf.col_width_list_str;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200739
740 init_rem_hits();
741
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200742 fprintf(fp, "# %s", pair ? "Baseline" : "Overhead");
743
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200744 if (symbol_conf.show_nr_samples) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200745 if (sep)
746 fprintf(fp, "%cSamples", *sep);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200747 else
748 fputs(" Samples ", fp);
749 }
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200750
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800751 if (symbol_conf.show_cpu_utilization) {
752 if (sep) {
753 ret += fprintf(fp, "%csys", *sep);
754 ret += fprintf(fp, "%cus", *sep);
755 if (perf_guest) {
756 ret += fprintf(fp, "%cguest sys", *sep);
757 ret += fprintf(fp, "%cguest us", *sep);
758 }
759 } else {
760 ret += fprintf(fp, " sys ");
761 ret += fprintf(fp, " us ");
762 if (perf_guest) {
763 ret += fprintf(fp, " guest sys ");
764 ret += fprintf(fp, " guest us ");
765 }
766 }
767 }
768
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200769 if (pair) {
770 if (sep)
771 ret += fprintf(fp, "%cDelta", *sep);
772 else
773 ret += fprintf(fp, " Delta ");
774
775 if (show_displacement) {
776 if (sep)
777 ret += fprintf(fp, "%cDisplacement", *sep);
778 else
779 ret += fprintf(fp, " Displ");
780 }
781 }
782
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200783 list_for_each_entry(se, &hist_entry__sort_list, list) {
784 if (se->elide)
785 continue;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200786 if (sep) {
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200787 fprintf(fp, "%c%s", *sep, se->se_header);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200788 continue;
789 }
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200790 width = strlen(se->se_header);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300791 if (symbol_conf.col_width_list_str) {
792 if (col_width) {
793 hists__set_col_len(self, se->se_width_idx,
794 atoi(col_width));
795 col_width = strchr(col_width, ',');
796 if (col_width)
797 ++col_width;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200798 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200799 }
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300800 if (!hists__new_col_len(self, se->se_width_idx, width))
801 width = hists__col_len(self, se->se_width_idx);
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200802 fprintf(fp, " %*s", width, se->se_header);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200803 }
804 fprintf(fp, "\n");
805
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200806 if (sep)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200807 goto print_entries;
808
809 fprintf(fp, "# ........");
810 if (symbol_conf.show_nr_samples)
811 fprintf(fp, " ..........");
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200812 if (pair) {
813 fprintf(fp, " ..........");
814 if (show_displacement)
815 fprintf(fp, " .....");
816 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200817 list_for_each_entry(se, &hist_entry__sort_list, list) {
818 unsigned int i;
819
820 if (se->elide)
821 continue;
822
823 fprintf(fp, " ");
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300824 width = hists__col_len(self, se->se_width_idx);
825 if (width == 0)
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200826 width = strlen(se->se_header);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200827 for (i = 0; i < width; i++)
828 fprintf(fp, ".");
829 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200830
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200831 fprintf(fp, "\n#\n");
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200832
833print_entries:
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300834 for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200835 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
836
837 if (show_displacement) {
838 if (h->pair != NULL)
839 displacement = ((long)h->pair->position -
840 (long)position);
841 else
842 displacement = 0;
843 ++position;
844 }
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300845 ret += hist_entry__fprintf(h, self, pair, show_displacement,
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -0300846 displacement, fp, self->stats.total_period);
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300847
848 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300849 ret += hist_entry__fprintf_callchain(h, self, fp,
850 self->stats.total_period);
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -0300851 if (h->ms.map == NULL && verbose > 1) {
Arnaldo Carvalho de Melo65f2ed22010-03-09 15:58:17 -0300852 __map_groups__fprintf_maps(&h->thread->mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300853 MAP__FUNCTION, verbose, fp);
Arnaldo Carvalho de Melo65f2ed22010-03-09 15:58:17 -0300854 fprintf(fp, "%.10s end\n", graph_dotted_line);
855 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200856 }
857
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200858 free(rem_sq_bracket);
859
860 return ret;
861}
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300862
Arnaldo Carvalho de Melo06daaab2010-07-21 17:58:25 -0300863/*
864 * See hists__fprintf to match the column widths
865 */
866unsigned int hists__sort_list_width(struct hists *self)
867{
868 struct sort_entry *se;
869 int ret = 9; /* total % */
870
871 if (symbol_conf.show_cpu_utilization) {
872 ret += 7; /* count_sys % */
873 ret += 6; /* count_us % */
874 if (perf_guest) {
875 ret += 13; /* count_guest_sys % */
876 ret += 12; /* count_guest_us % */
877 }
878 }
879
880 if (symbol_conf.show_nr_samples)
881 ret += 11;
882
883 list_for_each_entry(se, &hist_entry__sort_list, list)
884 if (!se->elide)
885 ret += 2 + hists__col_len(self, se->se_width_idx);
886
Arnaldo Carvalho de Melo903cce62010-08-05 19:15:48 -0300887 if (verbose) /* Addr + origin */
888 ret += 3 + BITS_PER_LONG / 4;
889
Arnaldo Carvalho de Melo06daaab2010-07-21 17:58:25 -0300890 return ret;
891}
892
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300893static void hists__remove_entry_filter(struct hists *self, struct hist_entry *h,
894 enum hist_filter filter)
895{
896 h->filtered &= ~(1 << filter);
897 if (h->filtered)
898 return;
899
900 ++self->nr_entries;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -0300901 if (h->ms.unfolded)
902 self->nr_entries += h->nr_rows;
903 h->row_offset = 0;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300904 self->stats.total_period += h->period;
905 self->stats.nr_events[PERF_RECORD_SAMPLE] += h->nr_events;
906
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300907 hists__calc_col_len(self, h);
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300908}
909
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300910void hists__filter_by_dso(struct hists *self, const struct dso *dso)
911{
912 struct rb_node *nd;
913
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -0300914 self->nr_entries = self->stats.total_period = 0;
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300915 self->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300916 hists__reset_col_len(self);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300917
918 for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
919 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
920
921 if (symbol_conf.exclude_other && !h->parent)
922 continue;
923
924 if (dso != NULL && (h->ms.map == NULL || h->ms.map->dso != dso)) {
925 h->filtered |= (1 << HIST_FILTER__DSO);
926 continue;
927 }
928
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300929 hists__remove_entry_filter(self, h, HIST_FILTER__DSO);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300930 }
931}
932
933void hists__filter_by_thread(struct hists *self, const struct thread *thread)
934{
935 struct rb_node *nd;
936
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -0300937 self->nr_entries = self->stats.total_period = 0;
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300938 self->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300939 hists__reset_col_len(self);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300940
941 for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
942 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
943
944 if (thread != NULL && h->thread != thread) {
945 h->filtered |= (1 << HIST_FILTER__THREAD);
946 continue;
947 }
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300948
949 hists__remove_entry_filter(self, h, HIST_FILTER__THREAD);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300950 }
951}
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300952
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200953int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300954{
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200955 return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300956}
957
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200958int hist_entry__annotate(struct hist_entry *he, size_t privsize)
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300959{
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200960 return symbol__annotate(he->ms.sym, he->ms.map, privsize);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300961}
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -0300962
963void hists__inc_nr_events(struct hists *self, u32 type)
964{
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -0300965 ++self->stats.nr_events[0];
966 ++self->stats.nr_events[type];
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -0300967}
968
969size_t hists__fprintf_nr_events(struct hists *self, FILE *fp)
970{
971 int i;
972 size_t ret = 0;
973
974 for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200975 const char *name = perf_event__name(i);
Thomas Gleixner3835bc02010-12-07 12:48:42 +0000976
977 if (!strcmp(name, "UNKNOWN"))
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -0300978 continue;
Thomas Gleixner3835bc02010-12-07 12:48:42 +0000979
980 ret += fprintf(fp, "%16s events: %10d\n", name,
981 self->stats.nr_events[i]);
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -0300982 }
983
984 return ret;
985}