blob: 90dc35ae444adee31fffbb2049382adefc7de7c8 [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 Melo90cf1fb2011-10-19 13:09:10 -02009static bool hists__filter_entry_by_dso(struct hists *hists,
10 struct hist_entry *he);
11static bool hists__filter_entry_by_thread(struct hists *hists,
12 struct hist_entry *he);
Namhyung Kime94d53e2012-03-16 17:50:51 +090013static bool hists__filter_entry_by_symbol(struct hists *hists,
14 struct hist_entry *he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -020015
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -030016enum hist_filter {
17 HIST_FILTER__DSO,
18 HIST_FILTER__THREAD,
19 HIST_FILTER__PARENT,
Namhyung Kime94d53e2012-03-16 17:50:51 +090020 HIST_FILTER__SYMBOL,
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -030021};
22
John Kacur3d1d07e2009-09-28 15:32:55 +020023struct callchain_param callchain_param = {
24 .mode = CHAIN_GRAPH_REL,
Sam Liaod797fdc2011-06-07 23:49:46 +080025 .min_percent = 0.5,
26 .order = ORDER_CALLEE
John Kacur3d1d07e2009-09-28 15:32:55 +020027};
28
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030029u16 hists__col_len(struct hists *hists, enum hist_column col)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030030{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030031 return hists->col_len[col];
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030032}
33
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030034void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030035{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030036 hists->col_len[col] = len;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030037}
38
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030039bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030040{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030041 if (len > hists__col_len(hists, col)) {
42 hists__set_col_len(hists, col, len);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030043 return true;
44 }
45 return false;
46}
47
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030048static void hists__reset_col_len(struct hists *hists)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030049{
50 enum hist_column col;
51
52 for (col = 0; col < HISTC_NR_COLS; ++col)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030053 hists__set_col_len(hists, col, 0);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030054}
55
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010056static void hists__set_unres_dso_col_len(struct hists *hists, int dso)
57{
58 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
59
60 if (hists__col_len(hists, dso) < unresolved_col_width &&
61 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
62 !symbol_conf.dso_list)
63 hists__set_col_len(hists, dso, unresolved_col_width);
64}
65
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030066static void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030067{
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010068 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030069 u16 len;
70
71 if (h->ms.sym)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010072 hists__new_col_len(hists, HISTC_SYMBOL, h->ms.sym->namelen + 4);
73 else
74 hists__set_unres_dso_col_len(hists, HISTC_DSO);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030075
76 len = thread__comm_len(h->thread);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030077 if (hists__new_col_len(hists, HISTC_COMM, len))
78 hists__set_col_len(hists, HISTC_THREAD, len + 6);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030079
80 if (h->ms.map) {
81 len = dso__name_len(h->ms.map->dso);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030082 hists__new_col_len(hists, HISTC_DSO, len);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030083 }
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010084
85 if (h->branch_info) {
86 int symlen;
87 /*
88 * +4 accounts for '[x] ' priv level info
89 * +2 account of 0x prefix on raw addresses
90 */
91 if (h->branch_info->from.sym) {
92 symlen = (int)h->branch_info->from.sym->namelen + 4;
93 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
94
95 symlen = dso__name_len(h->branch_info->from.map->dso);
96 hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
97 } else {
98 symlen = unresolved_col_width + 4 + 2;
99 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
100 hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
101 }
102
103 if (h->branch_info->to.sym) {
104 symlen = (int)h->branch_info->to.sym->namelen + 4;
105 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
106
107 symlen = dso__name_len(h->branch_info->to.map->dso);
108 hists__new_col_len(hists, HISTC_DSO_TO, symlen);
109 } else {
110 symlen = unresolved_col_width + 4 + 2;
111 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
112 hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
113 }
114 }
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300115}
116
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200117static void hist_entry__add_cpumode_period(struct hist_entry *he,
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300118 unsigned int cpumode, u64 period)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800119{
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300120 switch (cpumode) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800121 case PERF_RECORD_MISC_KERNEL:
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200122 he->period_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800123 break;
124 case PERF_RECORD_MISC_USER:
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200125 he->period_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800126 break;
127 case PERF_RECORD_MISC_GUEST_KERNEL:
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200128 he->period_guest_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800129 break;
130 case PERF_RECORD_MISC_GUEST_USER:
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200131 he->period_guest_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800132 break;
133 default:
134 break;
135 }
136}
137
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300138static void hist_entry__decay(struct hist_entry *he)
139{
140 he->period = (he->period * 7) / 8;
141 he->nr_events = (he->nr_events * 7) / 8;
142}
143
144static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
145{
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200146 u64 prev_period = he->period;
147
148 if (prev_period == 0)
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300149 return true;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200150
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300151 hist_entry__decay(he);
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200152
153 if (!he->filtered)
154 hists->stats.total_period -= prev_period - he->period;
155
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300156 return he->period == 0;
157}
158
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200159static void __hists__decay_entries(struct hists *hists, bool zap_user,
160 bool zap_kernel, bool threaded)
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300161{
162 struct rb_node *next = rb_first(&hists->entries);
163 struct hist_entry *n;
164
165 while (next) {
166 n = rb_entry(next, struct hist_entry, rb_node);
167 next = rb_next(&n->rb_node);
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300168 /*
169 * We may be annotating this, for instance, so keep it here in
170 * case some it gets new samples, we'll eventually free it when
171 * the user stops browsing and it agains gets fully decayed.
172 */
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200173 if (((zap_user && n->level == '.') ||
174 (zap_kernel && n->level != '.') ||
175 hists__decay_entry(hists, n)) &&
176 !n->used) {
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300177 rb_erase(&n->rb_node, &hists->entries);
178
Arnaldo Carvalho de Meloe345fa12011-10-13 09:06:54 -0300179 if (sort__need_collapse || threaded)
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300180 rb_erase(&n->rb_node_in, &hists->entries_collapsed);
181
182 hist_entry__free(n);
183 --hists->nr_entries;
184 }
185 }
186}
187
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200188void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
Arnaldo Carvalho de Meloe345fa12011-10-13 09:06:54 -0300189{
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200190 return __hists__decay_entries(hists, zap_user, zap_kernel, false);
Arnaldo Carvalho de Meloe345fa12011-10-13 09:06:54 -0300191}
192
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200193void hists__decay_entries_threaded(struct hists *hists,
194 bool zap_user, bool zap_kernel)
Arnaldo Carvalho de Meloe345fa12011-10-13 09:06:54 -0300195{
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200196 return __hists__decay_entries(hists, zap_user, zap_kernel, true);
Arnaldo Carvalho de Meloe345fa12011-10-13 09:06:54 -0300197}
198
John Kacur3d1d07e2009-09-28 15:32:55 +0200199/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300200 * histogram, sorted on item, collects periods
John Kacur3d1d07e2009-09-28 15:32:55 +0200201 */
202
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300203static struct hist_entry *hist_entry__new(struct hist_entry *template)
204{
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +0200205 size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0;
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200206 struct hist_entry *he = malloc(sizeof(*he) + callchain_size);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300207
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200208 if (he != NULL) {
209 *he = *template;
210 he->nr_events = 1;
211 if (he->ms.map)
212 he->ms.map->referenced = true;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300213 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200214 callchain_init(he->callchain);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300215 }
216
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200217 return he;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300218}
219
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300220static void hists__inc_nr_entries(struct hists *hists, struct hist_entry *h)
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300221{
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300222 if (!h->filtered) {
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300223 hists__calc_col_len(hists, h);
224 ++hists->nr_entries;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300225 hists->stats.total_period += h->period;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300226 }
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300227}
228
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300229static u8 symbol__parent_filter(const struct symbol *parent)
230{
231 if (symbol_conf.exclude_other && parent == NULL)
232 return 1 << HIST_FILTER__PARENT;
233 return 0;
234}
235
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100236static struct hist_entry *add_hist_entry(struct hists *hists,
237 struct hist_entry *entry,
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300238 struct addr_location *al,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100239 u64 period)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300240{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300241 struct rb_node **p;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300242 struct rb_node *parent = NULL;
243 struct hist_entry *he;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300244 int cmp;
245
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300246 pthread_mutex_lock(&hists->lock);
247
248 p = &hists->entries_in->rb_node;
249
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300250 while (*p != NULL) {
251 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300252 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300253
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100254 cmp = hist_entry__cmp(entry, he);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300255
256 if (!cmp) {
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300257 he->period += period;
258 ++he->nr_events;
David Miller63fa4712012-03-27 03:14:18 -0400259
260 /* If the map of an existing hist_entry has
261 * become out-of-date due to an exec() or
262 * similar, update it. Otherwise we will
263 * mis-adjust symbol addresses when computing
264 * the history counter to increment.
265 */
266 if (he->ms.map != entry->ms.map) {
267 he->ms.map = entry->ms.map;
268 if (he->ms.map)
269 he->ms.map->referenced = true;
270 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300271 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300272 }
273
274 if (cmp < 0)
275 p = &(*p)->rb_left;
276 else
277 p = &(*p)->rb_right;
278 }
279
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100280 he = hist_entry__new(entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300281 if (!he)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300282 goto out_unlock;
283
284 rb_link_node(&he->rb_node_in, parent, p);
285 rb_insert_color(&he->rb_node_in, hists->entries_in);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300286out:
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300287 hist_entry__add_cpumode_period(he, al->cpumode, period);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300288out_unlock:
289 pthread_mutex_unlock(&hists->lock);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300290 return he;
291}
292
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100293struct hist_entry *__hists__add_branch_entry(struct hists *self,
294 struct addr_location *al,
295 struct symbol *sym_parent,
296 struct branch_info *bi,
297 u64 period)
298{
299 struct hist_entry entry = {
300 .thread = al->thread,
301 .ms = {
302 .map = bi->to.map,
303 .sym = bi->to.sym,
304 },
305 .cpu = al->cpu,
306 .ip = bi->to.addr,
307 .level = al->level,
308 .period = period,
309 .parent = sym_parent,
310 .filtered = symbol__parent_filter(sym_parent),
311 .branch_info = bi,
312 };
313
314 return add_hist_entry(self, &entry, al, period);
315}
316
317struct hist_entry *__hists__add_entry(struct hists *self,
318 struct addr_location *al,
319 struct symbol *sym_parent, u64 period)
320{
321 struct hist_entry entry = {
322 .thread = al->thread,
323 .ms = {
324 .map = al->map,
325 .sym = al->sym,
326 },
327 .cpu = al->cpu,
328 .ip = al->addr,
329 .level = al->level,
330 .period = period,
331 .parent = sym_parent,
332 .filtered = symbol__parent_filter(sym_parent),
333 };
334
335 return add_hist_entry(self, &entry, al, period);
336}
337
John Kacur3d1d07e2009-09-28 15:32:55 +0200338int64_t
339hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
340{
341 struct sort_entry *se;
342 int64_t cmp = 0;
343
344 list_for_each_entry(se, &hist_entry__sort_list, list) {
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200345 cmp = se->se_cmp(left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200346 if (cmp)
347 break;
348 }
349
350 return cmp;
351}
352
353int64_t
354hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
355{
356 struct sort_entry *se;
357 int64_t cmp = 0;
358
359 list_for_each_entry(se, &hist_entry__sort_list, list) {
360 int64_t (*f)(struct hist_entry *, struct hist_entry *);
361
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200362 f = se->se_collapse ?: se->se_cmp;
John Kacur3d1d07e2009-09-28 15:32:55 +0200363
364 cmp = f(left, right);
365 if (cmp)
366 break;
367 }
368
369 return cmp;
370}
371
372void hist_entry__free(struct hist_entry *he)
373{
374 free(he);
375}
376
377/*
378 * collapse the histogram
379 */
380
Namhyung Kim47260642012-05-31 14:43:26 +0900381static bool hists__collapse_insert_entry(struct hists *hists __used,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100382 struct rb_root *root,
383 struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +0200384{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200385 struct rb_node **p = &root->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200386 struct rb_node *parent = NULL;
387 struct hist_entry *iter;
388 int64_t cmp;
389
390 while (*p != NULL) {
391 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300392 iter = rb_entry(parent, struct hist_entry, rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +0200393
394 cmp = hist_entry__collapse(iter, he);
395
396 if (!cmp) {
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300397 iter->period += he->period;
Stephane Eraniane39622c2011-10-03 11:38:15 +0200398 iter->nr_events += he->nr_events;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100399 if (symbol_conf.use_callchain) {
Namhyung Kim47260642012-05-31 14:43:26 +0900400 callchain_cursor_reset(&callchain_cursor);
401 callchain_merge(&callchain_cursor,
402 iter->callchain,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100403 he->callchain);
404 }
John Kacur3d1d07e2009-09-28 15:32:55 +0200405 hist_entry__free(he);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300406 return false;
John Kacur3d1d07e2009-09-28 15:32:55 +0200407 }
408
409 if (cmp < 0)
410 p = &(*p)->rb_left;
411 else
412 p = &(*p)->rb_right;
413 }
414
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300415 rb_link_node(&he->rb_node_in, parent, p);
416 rb_insert_color(&he->rb_node_in, root);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300417 return true;
John Kacur3d1d07e2009-09-28 15:32:55 +0200418}
419
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300420static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
421{
422 struct rb_root *root;
423
424 pthread_mutex_lock(&hists->lock);
425
426 root = hists->entries_in;
427 if (++hists->entries_in > &hists->entries_in_array[1])
428 hists->entries_in = &hists->entries_in_array[0];
429
430 pthread_mutex_unlock(&hists->lock);
431
432 return root;
433}
434
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200435static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
436{
437 hists__filter_entry_by_dso(hists, he);
438 hists__filter_entry_by_thread(hists, he);
Namhyung Kime94d53e2012-03-16 17:50:51 +0900439 hists__filter_entry_by_symbol(hists, he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200440}
441
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300442static void __hists__collapse_resort(struct hists *hists, bool threaded)
443{
444 struct rb_root *root;
445 struct rb_node *next;
446 struct hist_entry *n;
447
448 if (!sort__need_collapse && !threaded)
449 return;
450
451 root = hists__get_rotate_entries_in(hists);
452 next = rb_first(root);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300453
454 while (next) {
455 n = rb_entry(next, struct hist_entry, rb_node_in);
456 next = rb_next(&n->rb_node_in);
457
458 rb_erase(&n->rb_node_in, root);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200459 if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
460 /*
461 * If it wasn't combined with one of the entries already
462 * collapsed, we need to apply the filters that may have
463 * been set by, say, the hist_browser.
464 */
465 hists__apply_filters(hists, n);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200466 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300467 }
468}
469
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300470void hists__collapse_resort(struct hists *hists)
John Kacur3d1d07e2009-09-28 15:32:55 +0200471{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300472 return __hists__collapse_resort(hists, false);
473}
John Kacur3d1d07e2009-09-28 15:32:55 +0200474
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300475void hists__collapse_resort_threaded(struct hists *hists)
476{
477 return __hists__collapse_resort(hists, true);
John Kacur3d1d07e2009-09-28 15:32:55 +0200478}
479
480/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300481 * reverse the map, sort on period.
John Kacur3d1d07e2009-09-28 15:32:55 +0200482 */
483
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300484static void __hists__insert_output_entry(struct rb_root *entries,
485 struct hist_entry *he,
486 u64 min_callchain_hits)
John Kacur3d1d07e2009-09-28 15:32:55 +0200487{
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300488 struct rb_node **p = &entries->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200489 struct rb_node *parent = NULL;
490 struct hist_entry *iter;
491
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200492 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melob9fb9302010-04-02 09:50:42 -0300493 callchain_param.sort(&he->sorted_chain, he->callchain,
John Kacur3d1d07e2009-09-28 15:32:55 +0200494 min_callchain_hits, &callchain_param);
495
496 while (*p != NULL) {
497 parent = *p;
498 iter = rb_entry(parent, struct hist_entry, rb_node);
499
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300500 if (he->period > iter->period)
John Kacur3d1d07e2009-09-28 15:32:55 +0200501 p = &(*p)->rb_left;
502 else
503 p = &(*p)->rb_right;
504 }
505
506 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300507 rb_insert_color(&he->rb_node, entries);
John Kacur3d1d07e2009-09-28 15:32:55 +0200508}
509
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300510static void __hists__output_resort(struct hists *hists, bool threaded)
John Kacur3d1d07e2009-09-28 15:32:55 +0200511{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300512 struct rb_root *root;
John Kacur3d1d07e2009-09-28 15:32:55 +0200513 struct rb_node *next;
514 struct hist_entry *n;
John Kacur3d1d07e2009-09-28 15:32:55 +0200515 u64 min_callchain_hits;
516
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300517 min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
John Kacur3d1d07e2009-09-28 15:32:55 +0200518
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300519 if (sort__need_collapse || threaded)
520 root = &hists->entries_collapsed;
521 else
522 root = hists->entries_in;
523
524 next = rb_first(root);
525 hists->entries = RB_ROOT;
John Kacur3d1d07e2009-09-28 15:32:55 +0200526
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300527 hists->nr_entries = 0;
Arnaldo Carvalho de Melo79286312011-10-27 09:19:48 -0200528 hists->stats.total_period = 0;
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300529 hists__reset_col_len(hists);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300530
John Kacur3d1d07e2009-09-28 15:32:55 +0200531 while (next) {
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300532 n = rb_entry(next, struct hist_entry, rb_node_in);
533 next = rb_next(&n->rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +0200534
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300535 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300536 hists__inc_nr_entries(hists, n);
John Kacur3d1d07e2009-09-28 15:32:55 +0200537 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300538}
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200539
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300540void hists__output_resort(struct hists *hists)
541{
542 return __hists__output_resort(hists, false);
543}
544
545void hists__output_resort_threaded(struct hists *hists)
546{
547 return __hists__output_resort(hists, true);
John Kacur3d1d07e2009-09-28 15:32:55 +0200548}
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200549
550static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
551{
552 int i;
553 int ret = fprintf(fp, " ");
554
555 for (i = 0; i < left_margin; i++)
556 ret += fprintf(fp, " ");
557
558 return ret;
559}
560
561static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
562 int left_margin)
563{
564 int i;
565 size_t ret = callchain__fprintf_left_margin(fp, left_margin);
566
567 for (i = 0; i < depth; i++)
568 if (depth_mask & (1 << i))
569 ret += fprintf(fp, "| ");
570 else
571 ret += fprintf(fp, " ");
572
573 ret += fprintf(fp, "\n");
574
575 return ret;
576}
577
578static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain,
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300579 int depth, int depth_mask, int period,
Frederic Weisbeckerd425de52011-01-03 16:13:11 +0100580 u64 total_samples, u64 hits,
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200581 int left_margin)
582{
583 int i;
584 size_t ret = 0;
585
586 ret += callchain__fprintf_left_margin(fp, left_margin);
587 for (i = 0; i < depth; i++) {
588 if (depth_mask & (1 << i))
589 ret += fprintf(fp, "|");
590 else
591 ret += fprintf(fp, " ");
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300592 if (!period && i == depth - 1) {
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200593 double percent;
594
595 percent = hits * 100.0 / total_samples;
596 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
597 } else
598 ret += fprintf(fp, "%s", " ");
599 }
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300600 if (chain->ms.sym)
601 ret += fprintf(fp, "%s\n", chain->ms.sym->name);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200602 else
Jiri Olsaa0187062012-05-16 08:59:08 +0200603 ret += fprintf(fp, "0x%0" PRIx64 "\n", chain->ip);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200604
605 return ret;
606}
607
608static struct symbol *rem_sq_bracket;
609static struct callchain_list rem_hits;
610
611static void init_rem_hits(void)
612{
613 rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
614 if (!rem_sq_bracket) {
615 fprintf(stderr, "Not enough memory to display remaining hits\n");
616 return;
617 }
618
619 strcpy(rem_sq_bracket->name, "[...]");
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300620 rem_hits.ms.sym = rem_sq_bracket;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200621}
622
Frederic Weisbecker6d4818c2012-03-23 19:06:50 +0100623static size_t __callchain__fprintf_graph(FILE *fp, struct rb_root *root,
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200624 u64 total_samples, int depth,
625 int depth_mask, int left_margin)
626{
627 struct rb_node *node, *next;
628 struct callchain_node *child;
629 struct callchain_list *chain;
630 int new_depth_mask = depth_mask;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200631 u64 remaining;
632 size_t ret = 0;
633 int i;
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300634 uint entries_printed = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200635
Frederic Weisbecker6d4818c2012-03-23 19:06:50 +0100636 remaining = total_samples;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200637
Frederic Weisbecker6d4818c2012-03-23 19:06:50 +0100638 node = rb_first(root);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200639 while (node) {
Frederic Weisbecker6d4818c2012-03-23 19:06:50 +0100640 u64 new_total;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200641 u64 cumul;
642
643 child = rb_entry(node, struct callchain_node, rb_node);
Frederic Weisbeckerf08c3152011-01-14 04:51:59 +0100644 cumul = callchain_cumul_hits(child);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200645 remaining -= cumul;
646
647 /*
648 * The depth mask manages the output of pipes that show
649 * the depth. We don't want to keep the pipes of the current
650 * level for the last child of this depth.
651 * Except if we have remaining filtered hits. They will
652 * supersede the last child
653 */
654 next = rb_next(node);
655 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
656 new_depth_mask &= ~(1 << (depth - 1));
657
658 /*
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800659 * But we keep the older depth mask for the line separator
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200660 * to keep the level link until we reach the last child
661 */
662 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
663 left_margin);
664 i = 0;
665 list_for_each_entry(chain, &child->val, list) {
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200666 ret += ipchain__fprintf_graph(fp, chain, depth,
667 new_depth_mask, i++,
Frederic Weisbecker6d4818c2012-03-23 19:06:50 +0100668 total_samples,
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200669 cumul,
670 left_margin);
671 }
Frederic Weisbecker6d4818c2012-03-23 19:06:50 +0100672
673 if (callchain_param.mode == CHAIN_GRAPH_REL)
674 new_total = child->children_hit;
675 else
676 new_total = total_samples;
677
678 ret += __callchain__fprintf_graph(fp, &child->rb_root, new_total,
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200679 depth + 1,
680 new_depth_mask | (1 << depth),
681 left_margin);
682 node = next;
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300683 if (++entries_printed == callchain_param.print_limit)
684 break;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200685 }
686
687 if (callchain_param.mode == CHAIN_GRAPH_REL &&
Frederic Weisbecker6d4818c2012-03-23 19:06:50 +0100688 remaining && remaining != total_samples) {
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200689
690 if (!rem_sq_bracket)
691 return ret;
692
693 new_depth_mask &= ~(1 << (depth - 1));
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200694 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
Frederic Weisbecker6d4818c2012-03-23 19:06:50 +0100695 new_depth_mask, 0, total_samples,
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200696 remaining, left_margin);
697 }
698
699 return ret;
700}
701
Frederic Weisbecker6d4818c2012-03-23 19:06:50 +0100702static size_t callchain__fprintf_graph(FILE *fp, struct rb_root *root,
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200703 u64 total_samples, int left_margin)
704{
Frederic Weisbecker6d4818c2012-03-23 19:06:50 +0100705 struct callchain_node *cnode;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200706 struct callchain_list *chain;
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300707 u32 entries_printed = 0;
Frederic Weisbecker6d4818c2012-03-23 19:06:50 +0100708 bool printed = false;
709 struct rb_node *node;
710 int i = 0;
Frederic Weisbecker8760db72012-07-18 19:10:55 +0200711 int ret = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200712
Frederic Weisbecker6d4818c2012-03-23 19:06:50 +0100713 /*
714 * If have one single callchain root, don't bother printing
715 * its percentage (100 % in fractal mode and the same percentage
716 * than the hist in graph mode). This also avoid one level of column.
717 */
718 node = rb_first(root);
719 if (node && !rb_next(node)) {
720 cnode = rb_entry(node, struct callchain_node, rb_node);
721 list_for_each_entry(chain, &cnode->val, list) {
722 /*
723 * If we sort by symbol, the first entry is the same than
724 * the symbol. No need to print it otherwise it appears as
725 * displayed twice.
726 */
727 if (!i++ && sort__first_dimension == SORT_SYM)
728 continue;
729 if (!printed) {
730 ret += callchain__fprintf_left_margin(fp, left_margin);
731 ret += fprintf(fp, "|\n");
732 ret += callchain__fprintf_left_margin(fp, left_margin);
733 ret += fprintf(fp, "---");
734 left_margin += 3;
735 printed = true;
736 } else
737 ret += callchain__fprintf_left_margin(fp, left_margin);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200738
Frederic Weisbecker6d4818c2012-03-23 19:06:50 +0100739 if (chain->ms.sym)
740 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
741 else
742 ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200743
Frederic Weisbecker6d4818c2012-03-23 19:06:50 +0100744 if (++entries_printed == callchain_param.print_limit)
745 break;
746 }
747 root = &cnode->rb_root;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200748 }
749
Frederic Weisbecker8760db72012-07-18 19:10:55 +0200750 ret += __callchain__fprintf_graph(fp, root, total_samples,
Frederic Weisbecker6d4818c2012-03-23 19:06:50 +0100751 1, 1, left_margin);
Frederic Weisbecker8760db72012-07-18 19:10:55 +0200752 return ret;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200753}
754
Frederic Weisbecker6d4818c2012-03-23 19:06:50 +0100755static size_t __callchain__fprintf_flat(FILE *fp,
756 struct callchain_node *self,
757 u64 total_samples)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200758{
759 struct callchain_list *chain;
760 size_t ret = 0;
761
762 if (!self)
763 return 0;
764
Frederic Weisbecker6d4818c2012-03-23 19:06:50 +0100765 ret += __callchain__fprintf_flat(fp, self->parent, total_samples);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200766
767
768 list_for_each_entry(chain, &self->val, list) {
769 if (chain->ip >= PERF_CONTEXT_MAX)
770 continue;
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300771 if (chain->ms.sym)
772 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200773 else
774 ret += fprintf(fp, " %p\n",
775 (void *)(long)chain->ip);
776 }
777
778 return ret;
779}
780
Frederic Weisbecker6d4818c2012-03-23 19:06:50 +0100781static size_t callchain__fprintf_flat(FILE *fp, struct rb_root *self,
782 u64 total_samples)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200783{
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200784 size_t ret = 0;
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300785 u32 entries_printed = 0;
Frederic Weisbecker6d4818c2012-03-23 19:06:50 +0100786 struct rb_node *rb_node;
787 struct callchain_node *chain;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200788
Frederic Weisbecker6d4818c2012-03-23 19:06:50 +0100789 rb_node = rb_first(self);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200790 while (rb_node) {
791 double percent;
792
793 chain = rb_entry(rb_node, struct callchain_node, rb_node);
794 percent = chain->hit * 100.0 / total_samples;
Frederic Weisbecker6d4818c2012-03-23 19:06:50 +0100795
796 ret = percent_color_fprintf(fp, " %6.2f%%\n", percent);
797 ret += __callchain__fprintf_flat(fp, chain, total_samples);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200798 ret += fprintf(fp, "\n");
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300799 if (++entries_printed == callchain_param.print_limit)
800 break;
Frederic Weisbecker6d4818c2012-03-23 19:06:50 +0100801
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200802 rb_node = rb_next(rb_node);
803 }
804
805 return ret;
806}
807
Frederic Weisbecker6d4818c2012-03-23 19:06:50 +0100808static size_t hist_entry_callchain__fprintf(struct hist_entry *he,
809 u64 total_samples, int left_margin,
810 FILE *fp)
811{
812 switch (callchain_param.mode) {
813 case CHAIN_GRAPH_REL:
814 return callchain__fprintf_graph(fp, &he->sorted_chain, he->period,
815 left_margin);
816 break;
817 case CHAIN_GRAPH_ABS:
818 return callchain__fprintf_graph(fp, &he->sorted_chain, total_samples,
819 left_margin);
820 break;
821 case CHAIN_FLAT:
822 return callchain__fprintf_flat(fp, &he->sorted_chain, total_samples);
823 break;
824 case CHAIN_NONE:
825 break;
826 default:
827 pr_err("Bad callchain mode\n");
828 }
829
830 return 0;
831}
832
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300833void hists__output_recalc_col_len(struct hists *hists, int max_rows)
834{
835 struct rb_node *next = rb_first(&hists->entries);
836 struct hist_entry *n;
837 int row = 0;
838
839 hists__reset_col_len(hists);
840
841 while (next && row++ < max_rows) {
842 n = rb_entry(next, struct hist_entry, rb_node);
Arnaldo Carvalho de Melod197fd52011-10-20 07:35:45 -0200843 if (!n->filtered)
844 hists__calc_col_len(hists, n);
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300845 next = rb_next(&n->rb_node);
846 }
847}
848
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200849static int hist_entry__pcnt_snprintf(struct hist_entry *he, char *s,
Arnaldo Carvalho de Melof1cf6022011-10-18 14:37:34 -0200850 size_t size, struct hists *pair_hists,
851 bool show_displacement, long displacement,
Arnaldo Carvalho de Melo13d3ee52012-01-04 11:37:15 -0200852 bool color, u64 total_period)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200853{
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300854 u64 period, total, period_sys, period_us, period_guest_sys, period_guest_us;
Arnaldo Carvalho de Melofec9cbd2011-02-17 10:37:23 -0200855 u64 nr_events;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200856 const char *sep = symbol_conf.field_sep;
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300857 int ret;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200858
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200859 if (symbol_conf.exclude_other && !he->parent)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200860 return 0;
861
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300862 if (pair_hists) {
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200863 period = he->pair ? he->pair->period : 0;
864 nr_events = he->pair ? he->pair->nr_events : 0;
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -0300865 total = pair_hists->stats.total_period;
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200866 period_sys = he->pair ? he->pair->period_sys : 0;
867 period_us = he->pair ? he->pair->period_us : 0;
868 period_guest_sys = he->pair ? he->pair->period_guest_sys : 0;
869 period_guest_us = he->pair ? he->pair->period_guest_us : 0;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200870 } else {
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200871 period = he->period;
872 nr_events = he->nr_events;
Arnaldo Carvalho de Melo13d3ee52012-01-04 11:37:15 -0200873 total = total_period;
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200874 period_sys = he->period_sys;
875 period_us = he->period_us;
876 period_guest_sys = he->period_guest_sys;
877 period_guest_us = he->period_guest_us;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200878 }
879
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300880 if (total) {
881 if (color)
882 ret = percent_color_snprintf(s, size,
883 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300884 (period * 100.0) / total);
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300885 else
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -0300886 ret = scnprintf(s, size, sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300887 (period * 100.0) / total);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800888 if (symbol_conf.show_cpu_utilization) {
889 ret += percent_color_snprintf(s + ret, size - ret,
890 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300891 (period_sys * 100.0) / total);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800892 ret += percent_color_snprintf(s + ret, size - ret,
893 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300894 (period_us * 100.0) / total);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800895 if (perf_guest) {
896 ret += percent_color_snprintf(s + ret,
897 size - ret,
898 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300899 (period_guest_sys * 100.0) /
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800900 total);
901 ret += percent_color_snprintf(s + ret,
902 size - ret,
903 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300904 (period_guest_us * 100.0) /
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800905 total);
906 }
907 }
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300908 } else
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -0300909 ret = scnprintf(s, size, sep ? "%" PRIu64 : "%12" PRIu64 " ", period);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200910
911 if (symbol_conf.show_nr_samples) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200912 if (sep)
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -0300913 ret += scnprintf(s + ret, size - ret, "%c%" PRIu64, *sep, nr_events);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200914 else
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -0300915 ret += scnprintf(s + ret, size - ret, "%11" PRIu64, nr_events);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200916 }
917
Arnaldo Carvalho de Melo3f2728b2011-10-05 16:10:06 -0300918 if (symbol_conf.show_total_period) {
919 if (sep)
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -0300920 ret += scnprintf(s + ret, size - ret, "%c%" PRIu64, *sep, period);
Arnaldo Carvalho de Melo3f2728b2011-10-05 16:10:06 -0300921 else
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -0300922 ret += scnprintf(s + ret, size - ret, " %12" PRIu64, period);
Arnaldo Carvalho de Melo3f2728b2011-10-05 16:10:06 -0300923 }
924
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300925 if (pair_hists) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200926 char bf[32];
927 double old_percent = 0, new_percent = 0, diff;
928
929 if (total > 0)
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300930 old_percent = (period * 100.0) / total;
Arnaldo Carvalho de Melo13d3ee52012-01-04 11:37:15 -0200931 if (total_period > 0)
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200932 new_percent = (he->period * 100.0) / total_period;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200933
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -0200934 diff = new_percent - old_percent;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200935
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -0200936 if (fabs(diff) >= 0.01)
Jiri Olsa4bf9ce12012-03-22 14:37:26 +0100937 scnprintf(bf, sizeof(bf), "%+4.2F%%", diff);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200938 else
Jiri Olsa4bf9ce12012-03-22 14:37:26 +0100939 scnprintf(bf, sizeof(bf), " ");
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200940
941 if (sep)
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -0300942 ret += scnprintf(s + ret, size - ret, "%c%s", *sep, bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200943 else
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -0300944 ret += scnprintf(s + ret, size - ret, "%11.11s", bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200945
946 if (show_displacement) {
947 if (displacement)
Jiri Olsa4bf9ce12012-03-22 14:37:26 +0100948 scnprintf(bf, sizeof(bf), "%+4ld", displacement);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200949 else
Jiri Olsa4bf9ce12012-03-22 14:37:26 +0100950 scnprintf(bf, sizeof(bf), " ");
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200951
952 if (sep)
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -0300953 ret += scnprintf(s + ret, size - ret, "%c%s", *sep, bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200954 else
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -0300955 ret += scnprintf(s + ret, size - ret, "%6.6s", bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200956 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200957 }
958
Arnaldo Carvalho de Melof1cf6022011-10-18 14:37:34 -0200959 return ret;
960}
961
962int hist_entry__snprintf(struct hist_entry *he, char *s, size_t size,
963 struct hists *hists)
964{
965 const char *sep = symbol_conf.field_sep;
966 struct sort_entry *se;
967 int ret = 0;
968
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200969 list_for_each_entry(se, &hist_entry__sort_list, list) {
970 if (se->elide)
971 continue;
972
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -0300973 ret += scnprintf(s + ret, size - ret, "%s", sep ?: " ");
Arnaldo Carvalho de Melof1cf6022011-10-18 14:37:34 -0200974 ret += se->se_snprintf(he, s + ret, size - ret,
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300975 hists__col_len(hists, se->se_width_idx));
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200976 }
977
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300978 return ret;
979}
980
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200981static int hist_entry__fprintf(struct hist_entry *he, size_t size,
982 struct hists *hists, struct hists *pair_hists,
983 bool show_displacement, long displacement,
984 u64 total_period, FILE *fp)
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300985{
986 char bf[512];
Arnaldo Carvalho de Melof1cf6022011-10-18 14:37:34 -0200987 int ret;
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -0300988
989 if (size == 0 || size > sizeof(bf))
990 size = sizeof(bf);
991
Arnaldo Carvalho de Melof1cf6022011-10-18 14:37:34 -0200992 ret = hist_entry__pcnt_snprintf(he, bf, size, pair_hists,
993 show_displacement, displacement,
Arnaldo Carvalho de Melo13d3ee52012-01-04 11:37:15 -0200994 true, total_period);
Arnaldo Carvalho de Melof1cf6022011-10-18 14:37:34 -0200995 hist_entry__snprintf(he, bf + ret, size - ret, hists);
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300996 return fprintf(fp, "%s\n", bf);
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300997}
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200998
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200999static size_t hist_entry__fprintf_callchain(struct hist_entry *he,
1000 struct hists *hists,
1001 u64 total_period, FILE *fp)
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -03001002{
1003 int left_margin = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001004
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -03001005 if (sort__first_dimension == SORT_COMM) {
1006 struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
1007 typeof(*se), list);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -03001008 left_margin = hists__col_len(hists, se->se_width_idx);
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -02001009 left_margin -= thread__comm_len(he->thread);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001010 }
1011
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -02001012 return hist_entry_callchain__fprintf(he, total_period, left_margin, fp);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001013}
1014
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001015size_t hists__fprintf(struct hists *hists, struct hists *pair,
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -03001016 bool show_displacement, bool show_header, int max_rows,
1017 int max_cols, FILE *fp)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001018{
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001019 struct sort_entry *se;
1020 struct rb_node *nd;
1021 size_t ret = 0;
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -02001022 u64 total_period;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02001023 unsigned long position = 1;
1024 long displacement = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001025 unsigned int width;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02001026 const char *sep = symbol_conf.field_sep;
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -03001027 const char *col_width = symbol_conf.col_width_list_str;
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -03001028 int nr_rows = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001029
1030 init_rem_hits();
1031
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -03001032 if (!show_header)
1033 goto print_entries;
1034
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02001035 fprintf(fp, "# %s", pair ? "Baseline" : "Overhead");
1036
Namhyung Kim0ed35abc2012-01-08 02:25:32 +09001037 if (symbol_conf.show_cpu_utilization) {
1038 if (sep) {
1039 ret += fprintf(fp, "%csys", *sep);
1040 ret += fprintf(fp, "%cus", *sep);
1041 if (perf_guest) {
1042 ret += fprintf(fp, "%cguest sys", *sep);
1043 ret += fprintf(fp, "%cguest us", *sep);
1044 }
1045 } else {
1046 ret += fprintf(fp, " sys ");
1047 ret += fprintf(fp, " us ");
1048 if (perf_guest) {
1049 ret += fprintf(fp, " guest sys ");
1050 ret += fprintf(fp, " guest us ");
1051 }
1052 }
1053 }
1054
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001055 if (symbol_conf.show_nr_samples) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02001056 if (sep)
1057 fprintf(fp, "%cSamples", *sep);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001058 else
1059 fputs(" Samples ", fp);
1060 }
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02001061
Arnaldo Carvalho de Melo3f2728b2011-10-05 16:10:06 -03001062 if (symbol_conf.show_total_period) {
1063 if (sep)
1064 ret += fprintf(fp, "%cPeriod", *sep);
1065 else
1066 ret += fprintf(fp, " Period ");
1067 }
1068
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02001069 if (pair) {
1070 if (sep)
1071 ret += fprintf(fp, "%cDelta", *sep);
1072 else
1073 ret += fprintf(fp, " Delta ");
1074
1075 if (show_displacement) {
1076 if (sep)
1077 ret += fprintf(fp, "%cDisplacement", *sep);
1078 else
1079 ret += fprintf(fp, " Displ");
1080 }
1081 }
1082
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001083 list_for_each_entry(se, &hist_entry__sort_list, list) {
1084 if (se->elide)
1085 continue;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02001086 if (sep) {
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +02001087 fprintf(fp, "%c%s", *sep, se->se_header);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001088 continue;
1089 }
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +02001090 width = strlen(se->se_header);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -03001091 if (symbol_conf.col_width_list_str) {
1092 if (col_width) {
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001093 hists__set_col_len(hists, se->se_width_idx,
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -03001094 atoi(col_width));
1095 col_width = strchr(col_width, ',');
1096 if (col_width)
1097 ++col_width;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001098 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001099 }
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001100 if (!hists__new_col_len(hists, se->se_width_idx, width))
1101 width = hists__col_len(hists, se->se_width_idx);
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +02001102 fprintf(fp, " %*s", width, se->se_header);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001103 }
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -03001104
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001105 fprintf(fp, "\n");
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -03001106 if (max_rows && ++nr_rows >= max_rows)
1107 goto out;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001108
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02001109 if (sep)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001110 goto print_entries;
1111
1112 fprintf(fp, "# ........");
Namhyung Kim0ed35abc2012-01-08 02:25:32 +09001113 if (symbol_conf.show_cpu_utilization)
1114 fprintf(fp, " ....... .......");
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001115 if (symbol_conf.show_nr_samples)
1116 fprintf(fp, " ..........");
Arnaldo Carvalho de Melo3f2728b2011-10-05 16:10:06 -03001117 if (symbol_conf.show_total_period)
1118 fprintf(fp, " ............");
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02001119 if (pair) {
1120 fprintf(fp, " ..........");
1121 if (show_displacement)
1122 fprintf(fp, " .....");
1123 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001124 list_for_each_entry(se, &hist_entry__sort_list, list) {
1125 unsigned int i;
1126
1127 if (se->elide)
1128 continue;
1129
1130 fprintf(fp, " ");
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001131 width = hists__col_len(hists, se->se_width_idx);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -03001132 if (width == 0)
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +02001133 width = strlen(se->se_header);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001134 for (i = 0; i < width; i++)
1135 fprintf(fp, ".");
1136 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001137
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -03001138 fprintf(fp, "\n");
1139 if (max_rows && ++nr_rows >= max_rows)
1140 goto out;
1141
1142 fprintf(fp, "#\n");
1143 if (max_rows && ++nr_rows >= max_rows)
1144 goto out;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001145
1146print_entries:
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -02001147 total_period = hists->stats.total_period;
1148
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001149 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02001150 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1151
Frederic Weisbeckere84d2122011-06-29 22:23:03 +02001152 if (h->filtered)
1153 continue;
1154
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02001155 if (show_displacement) {
1156 if (h->pair != NULL)
1157 displacement = ((long)h->pair->position -
1158 (long)position);
1159 else
1160 displacement = 0;
1161 ++position;
1162 }
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -03001163 ret += hist_entry__fprintf(h, max_cols, hists, pair, show_displacement,
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -02001164 displacement, total_period, fp);
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -03001165
1166 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -02001167 ret += hist_entry__fprintf_callchain(h, hists, total_period, fp);
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -03001168 if (max_rows && ++nr_rows >= max_rows)
1169 goto out;
1170
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -03001171 if (h->ms.map == NULL && verbose > 1) {
Arnaldo Carvalho de Melo65f2ed22010-03-09 15:58:17 -03001172 __map_groups__fprintf_maps(&h->thread->mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -03001173 MAP__FUNCTION, verbose, fp);
Arnaldo Carvalho de Melo65f2ed22010-03-09 15:58:17 -03001174 fprintf(fp, "%.10s end\n", graph_dotted_line);
1175 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001176 }
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -03001177out:
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001178 free(rem_sq_bracket);
1179
1180 return ret;
1181}
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001182
Arnaldo Carvalho de Melo06daaab2010-07-21 17:58:25 -03001183/*
1184 * See hists__fprintf to match the column widths
1185 */
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001186unsigned int hists__sort_list_width(struct hists *hists)
Arnaldo Carvalho de Melo06daaab2010-07-21 17:58:25 -03001187{
1188 struct sort_entry *se;
1189 int ret = 9; /* total % */
1190
1191 if (symbol_conf.show_cpu_utilization) {
1192 ret += 7; /* count_sys % */
1193 ret += 6; /* count_us % */
1194 if (perf_guest) {
1195 ret += 13; /* count_guest_sys % */
1196 ret += 12; /* count_guest_us % */
1197 }
1198 }
1199
1200 if (symbol_conf.show_nr_samples)
1201 ret += 11;
1202
Arnaldo Carvalho de Melo3f2728b2011-10-05 16:10:06 -03001203 if (symbol_conf.show_total_period)
1204 ret += 13;
1205
Arnaldo Carvalho de Melo06daaab2010-07-21 17:58:25 -03001206 list_for_each_entry(se, &hist_entry__sort_list, list)
1207 if (!se->elide)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001208 ret += 2 + hists__col_len(hists, se->se_width_idx);
Arnaldo Carvalho de Melo06daaab2010-07-21 17:58:25 -03001209
Arnaldo Carvalho de Melo903cce62010-08-05 19:15:48 -03001210 if (verbose) /* Addr + origin */
1211 ret += 3 + BITS_PER_LONG / 4;
1212
Arnaldo Carvalho de Melo06daaab2010-07-21 17:58:25 -03001213 return ret;
1214}
1215
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001216static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001217 enum hist_filter filter)
1218{
1219 h->filtered &= ~(1 << filter);
1220 if (h->filtered)
1221 return;
1222
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001223 ++hists->nr_entries;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -03001224 if (h->ms.unfolded)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001225 hists->nr_entries += h->nr_rows;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -03001226 h->row_offset = 0;
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001227 hists->stats.total_period += h->period;
1228 hists->stats.nr_events[PERF_RECORD_SAMPLE] += h->nr_events;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001229
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001230 hists__calc_col_len(hists, h);
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001231}
1232
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001233
1234static bool hists__filter_entry_by_dso(struct hists *hists,
1235 struct hist_entry *he)
1236{
1237 if (hists->dso_filter != NULL &&
1238 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
1239 he->filtered |= (1 << HIST_FILTER__DSO);
1240 return true;
1241 }
1242
1243 return false;
1244}
1245
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001246void hists__filter_by_dso(struct hists *hists)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001247{
1248 struct rb_node *nd;
1249
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001250 hists->nr_entries = hists->stats.total_period = 0;
1251 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
1252 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001253
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001254 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001255 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1256
1257 if (symbol_conf.exclude_other && !h->parent)
1258 continue;
1259
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001260 if (hists__filter_entry_by_dso(hists, h))
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001261 continue;
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001262
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001263 hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001264 }
1265}
1266
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001267static bool hists__filter_entry_by_thread(struct hists *hists,
1268 struct hist_entry *he)
1269{
1270 if (hists->thread_filter != NULL &&
1271 he->thread != hists->thread_filter) {
1272 he->filtered |= (1 << HIST_FILTER__THREAD);
1273 return true;
1274 }
1275
1276 return false;
1277}
1278
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001279void hists__filter_by_thread(struct hists *hists)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001280{
1281 struct rb_node *nd;
1282
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001283 hists->nr_entries = hists->stats.total_period = 0;
1284 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
1285 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001286
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001287 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001288 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1289
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001290 if (hists__filter_entry_by_thread(hists, h))
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001291 continue;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001292
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001293 hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001294 }
1295}
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001296
Namhyung Kime94d53e2012-03-16 17:50:51 +09001297static bool hists__filter_entry_by_symbol(struct hists *hists,
1298 struct hist_entry *he)
1299{
1300 if (hists->symbol_filter_str != NULL &&
1301 (!he->ms.sym || strstr(he->ms.sym->name,
1302 hists->symbol_filter_str) == NULL)) {
1303 he->filtered |= (1 << HIST_FILTER__SYMBOL);
1304 return true;
1305 }
1306
1307 return false;
1308}
1309
1310void hists__filter_by_symbol(struct hists *hists)
1311{
1312 struct rb_node *nd;
1313
1314 hists->nr_entries = hists->stats.total_period = 0;
1315 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
1316 hists__reset_col_len(hists);
1317
1318 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1319 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1320
1321 if (hists__filter_entry_by_symbol(hists, h))
1322 continue;
1323
1324 hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
1325 }
1326}
1327
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -02001328int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001329{
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -02001330 return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001331}
1332
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -02001333int hist_entry__annotate(struct hist_entry *he, size_t privsize)
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001334{
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -02001335 return symbol__annotate(he->ms.sym, he->ms.map, privsize);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001336}
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001337
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001338void hists__inc_nr_events(struct hists *hists, u32 type)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001339{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001340 ++hists->stats.nr_events[0];
1341 ++hists->stats.nr_events[type];
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001342}
1343
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001344size_t hists__fprintf_nr_events(struct hists *hists, FILE *fp)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001345{
1346 int i;
1347 size_t ret = 0;
1348
1349 for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001350 const char *name;
Thomas Gleixner3835bc02010-12-07 12:48:42 +00001351
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001352 if (hists->stats.nr_events[i] == 0)
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001353 continue;
1354
1355 name = perf_event__name(i);
Thomas Gleixner3835bc02010-12-07 12:48:42 +00001356 if (!strcmp(name, "UNKNOWN"))
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001357 continue;
Thomas Gleixner3835bc02010-12-07 12:48:42 +00001358
1359 ret += fprintf(fp, "%16s events: %10d\n", name,
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001360 hists->stats.nr_events[i]);
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001361 }
1362
1363 return ret;
1364}