blob: c61235f81260a0b63eca35ec6b9cf19ce799b77e [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;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300259 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300260 }
261
262 if (cmp < 0)
263 p = &(*p)->rb_left;
264 else
265 p = &(*p)->rb_right;
266 }
267
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100268 he = hist_entry__new(entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300269 if (!he)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300270 goto out_unlock;
271
272 rb_link_node(&he->rb_node_in, parent, p);
273 rb_insert_color(&he->rb_node_in, hists->entries_in);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300274out:
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300275 hist_entry__add_cpumode_period(he, al->cpumode, period);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300276out_unlock:
277 pthread_mutex_unlock(&hists->lock);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300278 return he;
279}
280
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100281struct hist_entry *__hists__add_branch_entry(struct hists *self,
282 struct addr_location *al,
283 struct symbol *sym_parent,
284 struct branch_info *bi,
285 u64 period)
286{
287 struct hist_entry entry = {
288 .thread = al->thread,
289 .ms = {
290 .map = bi->to.map,
291 .sym = bi->to.sym,
292 },
293 .cpu = al->cpu,
294 .ip = bi->to.addr,
295 .level = al->level,
296 .period = period,
297 .parent = sym_parent,
298 .filtered = symbol__parent_filter(sym_parent),
299 .branch_info = bi,
300 };
301
302 return add_hist_entry(self, &entry, al, period);
303}
304
305struct hist_entry *__hists__add_entry(struct hists *self,
306 struct addr_location *al,
307 struct symbol *sym_parent, u64 period)
308{
309 struct hist_entry entry = {
310 .thread = al->thread,
311 .ms = {
312 .map = al->map,
313 .sym = al->sym,
314 },
315 .cpu = al->cpu,
316 .ip = al->addr,
317 .level = al->level,
318 .period = period,
319 .parent = sym_parent,
320 .filtered = symbol__parent_filter(sym_parent),
321 };
322
323 return add_hist_entry(self, &entry, al, period);
324}
325
John Kacur3d1d07e2009-09-28 15:32:55 +0200326int64_t
327hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
328{
329 struct sort_entry *se;
330 int64_t cmp = 0;
331
332 list_for_each_entry(se, &hist_entry__sort_list, list) {
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200333 cmp = se->se_cmp(left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200334 if (cmp)
335 break;
336 }
337
338 return cmp;
339}
340
341int64_t
342hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
343{
344 struct sort_entry *se;
345 int64_t cmp = 0;
346
347 list_for_each_entry(se, &hist_entry__sort_list, list) {
348 int64_t (*f)(struct hist_entry *, struct hist_entry *);
349
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200350 f = se->se_collapse ?: se->se_cmp;
John Kacur3d1d07e2009-09-28 15:32:55 +0200351
352 cmp = f(left, right);
353 if (cmp)
354 break;
355 }
356
357 return cmp;
358}
359
360void hist_entry__free(struct hist_entry *he)
361{
362 free(he);
363}
364
365/*
366 * collapse the histogram
367 */
368
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300369static bool hists__collapse_insert_entry(struct hists *hists,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100370 struct rb_root *root,
371 struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +0200372{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200373 struct rb_node **p = &root->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200374 struct rb_node *parent = NULL;
375 struct hist_entry *iter;
376 int64_t cmp;
377
378 while (*p != NULL) {
379 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300380 iter = rb_entry(parent, struct hist_entry, rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +0200381
382 cmp = hist_entry__collapse(iter, he);
383
384 if (!cmp) {
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300385 iter->period += he->period;
Stephane Eraniane39622c2011-10-03 11:38:15 +0200386 iter->nr_events += he->nr_events;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100387 if (symbol_conf.use_callchain) {
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300388 callchain_cursor_reset(&hists->callchain_cursor);
389 callchain_merge(&hists->callchain_cursor, iter->callchain,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100390 he->callchain);
391 }
John Kacur3d1d07e2009-09-28 15:32:55 +0200392 hist_entry__free(he);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300393 return false;
John Kacur3d1d07e2009-09-28 15:32:55 +0200394 }
395
396 if (cmp < 0)
397 p = &(*p)->rb_left;
398 else
399 p = &(*p)->rb_right;
400 }
401
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300402 rb_link_node(&he->rb_node_in, parent, p);
403 rb_insert_color(&he->rb_node_in, root);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300404 return true;
John Kacur3d1d07e2009-09-28 15:32:55 +0200405}
406
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300407static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
408{
409 struct rb_root *root;
410
411 pthread_mutex_lock(&hists->lock);
412
413 root = hists->entries_in;
414 if (++hists->entries_in > &hists->entries_in_array[1])
415 hists->entries_in = &hists->entries_in_array[0];
416
417 pthread_mutex_unlock(&hists->lock);
418
419 return root;
420}
421
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200422static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
423{
424 hists__filter_entry_by_dso(hists, he);
425 hists__filter_entry_by_thread(hists, he);
Namhyung Kime94d53e2012-03-16 17:50:51 +0900426 hists__filter_entry_by_symbol(hists, he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200427}
428
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300429static void __hists__collapse_resort(struct hists *hists, bool threaded)
430{
431 struct rb_root *root;
432 struct rb_node *next;
433 struct hist_entry *n;
434
435 if (!sort__need_collapse && !threaded)
436 return;
437
438 root = hists__get_rotate_entries_in(hists);
439 next = rb_first(root);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300440
441 while (next) {
442 n = rb_entry(next, struct hist_entry, rb_node_in);
443 next = rb_next(&n->rb_node_in);
444
445 rb_erase(&n->rb_node_in, root);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200446 if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
447 /*
448 * If it wasn't combined with one of the entries already
449 * collapsed, we need to apply the filters that may have
450 * been set by, say, the hist_browser.
451 */
452 hists__apply_filters(hists, n);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200453 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300454 }
455}
456
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300457void hists__collapse_resort(struct hists *hists)
John Kacur3d1d07e2009-09-28 15:32:55 +0200458{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300459 return __hists__collapse_resort(hists, false);
460}
John Kacur3d1d07e2009-09-28 15:32:55 +0200461
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300462void hists__collapse_resort_threaded(struct hists *hists)
463{
464 return __hists__collapse_resort(hists, true);
John Kacur3d1d07e2009-09-28 15:32:55 +0200465}
466
467/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300468 * reverse the map, sort on period.
John Kacur3d1d07e2009-09-28 15:32:55 +0200469 */
470
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300471static void __hists__insert_output_entry(struct rb_root *entries,
472 struct hist_entry *he,
473 u64 min_callchain_hits)
John Kacur3d1d07e2009-09-28 15:32:55 +0200474{
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300475 struct rb_node **p = &entries->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200476 struct rb_node *parent = NULL;
477 struct hist_entry *iter;
478
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200479 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melob9fb9302010-04-02 09:50:42 -0300480 callchain_param.sort(&he->sorted_chain, he->callchain,
John Kacur3d1d07e2009-09-28 15:32:55 +0200481 min_callchain_hits, &callchain_param);
482
483 while (*p != NULL) {
484 parent = *p;
485 iter = rb_entry(parent, struct hist_entry, rb_node);
486
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300487 if (he->period > iter->period)
John Kacur3d1d07e2009-09-28 15:32:55 +0200488 p = &(*p)->rb_left;
489 else
490 p = &(*p)->rb_right;
491 }
492
493 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300494 rb_insert_color(&he->rb_node, entries);
John Kacur3d1d07e2009-09-28 15:32:55 +0200495}
496
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300497static void __hists__output_resort(struct hists *hists, bool threaded)
John Kacur3d1d07e2009-09-28 15:32:55 +0200498{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300499 struct rb_root *root;
John Kacur3d1d07e2009-09-28 15:32:55 +0200500 struct rb_node *next;
501 struct hist_entry *n;
John Kacur3d1d07e2009-09-28 15:32:55 +0200502 u64 min_callchain_hits;
503
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300504 min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
John Kacur3d1d07e2009-09-28 15:32:55 +0200505
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300506 if (sort__need_collapse || threaded)
507 root = &hists->entries_collapsed;
508 else
509 root = hists->entries_in;
510
511 next = rb_first(root);
512 hists->entries = RB_ROOT;
John Kacur3d1d07e2009-09-28 15:32:55 +0200513
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300514 hists->nr_entries = 0;
Arnaldo Carvalho de Melo79286312011-10-27 09:19:48 -0200515 hists->stats.total_period = 0;
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300516 hists__reset_col_len(hists);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300517
John Kacur3d1d07e2009-09-28 15:32:55 +0200518 while (next) {
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300519 n = rb_entry(next, struct hist_entry, rb_node_in);
520 next = rb_next(&n->rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +0200521
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300522 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300523 hists__inc_nr_entries(hists, n);
John Kacur3d1d07e2009-09-28 15:32:55 +0200524 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300525}
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200526
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300527void hists__output_resort(struct hists *hists)
528{
529 return __hists__output_resort(hists, false);
530}
531
532void hists__output_resort_threaded(struct hists *hists)
533{
534 return __hists__output_resort(hists, true);
John Kacur3d1d07e2009-09-28 15:32:55 +0200535}
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200536
537static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
538{
539 int i;
540 int ret = fprintf(fp, " ");
541
542 for (i = 0; i < left_margin; i++)
543 ret += fprintf(fp, " ");
544
545 return ret;
546}
547
548static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
549 int left_margin)
550{
551 int i;
552 size_t ret = callchain__fprintf_left_margin(fp, left_margin);
553
554 for (i = 0; i < depth; i++)
555 if (depth_mask & (1 << i))
556 ret += fprintf(fp, "| ");
557 else
558 ret += fprintf(fp, " ");
559
560 ret += fprintf(fp, "\n");
561
562 return ret;
563}
564
565static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain,
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300566 int depth, int depth_mask, int period,
Frederic Weisbeckerd425de52011-01-03 16:13:11 +0100567 u64 total_samples, u64 hits,
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200568 int left_margin)
569{
570 int i;
571 size_t ret = 0;
572
573 ret += callchain__fprintf_left_margin(fp, left_margin);
574 for (i = 0; i < depth; i++) {
575 if (depth_mask & (1 << i))
576 ret += fprintf(fp, "|");
577 else
578 ret += fprintf(fp, " ");
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300579 if (!period && i == depth - 1) {
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200580 double percent;
581
582 percent = hits * 100.0 / total_samples;
583 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
584 } else
585 ret += fprintf(fp, "%s", " ");
586 }
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300587 if (chain->ms.sym)
588 ret += fprintf(fp, "%s\n", chain->ms.sym->name);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200589 else
590 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
591
592 return ret;
593}
594
595static struct symbol *rem_sq_bracket;
596static struct callchain_list rem_hits;
597
598static void init_rem_hits(void)
599{
600 rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
601 if (!rem_sq_bracket) {
602 fprintf(stderr, "Not enough memory to display remaining hits\n");
603 return;
604 }
605
606 strcpy(rem_sq_bracket->name, "[...]");
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300607 rem_hits.ms.sym = rem_sq_bracket;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200608}
609
610static size_t __callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
611 u64 total_samples, int depth,
612 int depth_mask, int left_margin)
613{
614 struct rb_node *node, *next;
615 struct callchain_node *child;
616 struct callchain_list *chain;
617 int new_depth_mask = depth_mask;
618 u64 new_total;
619 u64 remaining;
620 size_t ret = 0;
621 int i;
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300622 uint entries_printed = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200623
624 if (callchain_param.mode == CHAIN_GRAPH_REL)
625 new_total = self->children_hit;
626 else
627 new_total = total_samples;
628
629 remaining = new_total;
630
631 node = rb_first(&self->rb_root);
632 while (node) {
633 u64 cumul;
634
635 child = rb_entry(node, struct callchain_node, rb_node);
Frederic Weisbeckerf08c3152011-01-14 04:51:59 +0100636 cumul = callchain_cumul_hits(child);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200637 remaining -= cumul;
638
639 /*
640 * The depth mask manages the output of pipes that show
641 * the depth. We don't want to keep the pipes of the current
642 * level for the last child of this depth.
643 * Except if we have remaining filtered hits. They will
644 * supersede the last child
645 */
646 next = rb_next(node);
647 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
648 new_depth_mask &= ~(1 << (depth - 1));
649
650 /*
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800651 * But we keep the older depth mask for the line separator
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200652 * to keep the level link until we reach the last child
653 */
654 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
655 left_margin);
656 i = 0;
657 list_for_each_entry(chain, &child->val, list) {
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200658 ret += ipchain__fprintf_graph(fp, chain, depth,
659 new_depth_mask, i++,
660 new_total,
661 cumul,
662 left_margin);
663 }
664 ret += __callchain__fprintf_graph(fp, child, new_total,
665 depth + 1,
666 new_depth_mask | (1 << depth),
667 left_margin);
668 node = next;
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300669 if (++entries_printed == callchain_param.print_limit)
670 break;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200671 }
672
673 if (callchain_param.mode == CHAIN_GRAPH_REL &&
674 remaining && remaining != new_total) {
675
676 if (!rem_sq_bracket)
677 return ret;
678
679 new_depth_mask &= ~(1 << (depth - 1));
680
681 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
682 new_depth_mask, 0, new_total,
683 remaining, left_margin);
684 }
685
686 return ret;
687}
688
689static size_t callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
690 u64 total_samples, int left_margin)
691{
692 struct callchain_list *chain;
693 bool printed = false;
694 int i = 0;
695 int ret = 0;
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300696 u32 entries_printed = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200697
698 list_for_each_entry(chain, &self->val, list) {
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200699 if (!i++ && sort__first_dimension == SORT_SYM)
700 continue;
701
702 if (!printed) {
703 ret += callchain__fprintf_left_margin(fp, left_margin);
704 ret += fprintf(fp, "|\n");
705 ret += callchain__fprintf_left_margin(fp, left_margin);
706 ret += fprintf(fp, "---");
707
708 left_margin += 3;
709 printed = true;
710 } else
711 ret += callchain__fprintf_left_margin(fp, left_margin);
712
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300713 if (chain->ms.sym)
714 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200715 else
716 ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300717
718 if (++entries_printed == callchain_param.print_limit)
719 break;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200720 }
721
722 ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin);
723
724 return ret;
725}
726
727static size_t callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
728 u64 total_samples)
729{
730 struct callchain_list *chain;
731 size_t ret = 0;
732
733 if (!self)
734 return 0;
735
736 ret += callchain__fprintf_flat(fp, self->parent, total_samples);
737
738
739 list_for_each_entry(chain, &self->val, list) {
740 if (chain->ip >= PERF_CONTEXT_MAX)
741 continue;
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300742 if (chain->ms.sym)
743 ret += fprintf(fp, " %s\n", chain->ms.sym->name);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200744 else
745 ret += fprintf(fp, " %p\n",
746 (void *)(long)chain->ip);
747 }
748
749 return ret;
750}
751
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200752static size_t hist_entry_callchain__fprintf(struct hist_entry *he,
753 u64 total_samples, int left_margin,
754 FILE *fp)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200755{
756 struct rb_node *rb_node;
757 struct callchain_node *chain;
758 size_t ret = 0;
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300759 u32 entries_printed = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200760
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200761 rb_node = rb_first(&he->sorted_chain);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200762 while (rb_node) {
763 double percent;
764
765 chain = rb_entry(rb_node, struct callchain_node, rb_node);
766 percent = chain->hit * 100.0 / total_samples;
767 switch (callchain_param.mode) {
768 case CHAIN_FLAT:
769 ret += percent_color_fprintf(fp, " %6.2f%%\n",
770 percent);
771 ret += callchain__fprintf_flat(fp, chain, total_samples);
772 break;
773 case CHAIN_GRAPH_ABS: /* Falldown */
774 case CHAIN_GRAPH_REL:
775 ret += callchain__fprintf_graph(fp, chain, total_samples,
776 left_margin);
777 case CHAIN_NONE:
778 default:
779 break;
780 }
781 ret += fprintf(fp, "\n");
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -0300782 if (++entries_printed == callchain_param.print_limit)
783 break;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200784 rb_node = rb_next(rb_node);
785 }
786
787 return ret;
788}
789
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300790void hists__output_recalc_col_len(struct hists *hists, int max_rows)
791{
792 struct rb_node *next = rb_first(&hists->entries);
793 struct hist_entry *n;
794 int row = 0;
795
796 hists__reset_col_len(hists);
797
798 while (next && row++ < max_rows) {
799 n = rb_entry(next, struct hist_entry, rb_node);
Arnaldo Carvalho de Melod197fd52011-10-20 07:35:45 -0200800 if (!n->filtered)
801 hists__calc_col_len(hists, n);
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300802 next = rb_next(&n->rb_node);
803 }
804}
805
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200806static int hist_entry__pcnt_snprintf(struct hist_entry *he, char *s,
Arnaldo Carvalho de Melof1cf6022011-10-18 14:37:34 -0200807 size_t size, struct hists *pair_hists,
808 bool show_displacement, long displacement,
Arnaldo Carvalho de Melo13d3ee52012-01-04 11:37:15 -0200809 bool color, u64 total_period)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200810{
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300811 u64 period, total, period_sys, period_us, period_guest_sys, period_guest_us;
Arnaldo Carvalho de Melofec9cbd2011-02-17 10:37:23 -0200812 u64 nr_events;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200813 const char *sep = symbol_conf.field_sep;
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300814 int ret;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200815
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200816 if (symbol_conf.exclude_other && !he->parent)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200817 return 0;
818
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300819 if (pair_hists) {
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200820 period = he->pair ? he->pair->period : 0;
821 nr_events = he->pair ? he->pair->nr_events : 0;
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -0300822 total = pair_hists->stats.total_period;
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200823 period_sys = he->pair ? he->pair->period_sys : 0;
824 period_us = he->pair ? he->pair->period_us : 0;
825 period_guest_sys = he->pair ? he->pair->period_guest_sys : 0;
826 period_guest_us = he->pair ? he->pair->period_guest_us : 0;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200827 } else {
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200828 period = he->period;
829 nr_events = he->nr_events;
Arnaldo Carvalho de Melo13d3ee52012-01-04 11:37:15 -0200830 total = total_period;
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200831 period_sys = he->period_sys;
832 period_us = he->period_us;
833 period_guest_sys = he->period_guest_sys;
834 period_guest_us = he->period_guest_us;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200835 }
836
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300837 if (total) {
838 if (color)
839 ret = percent_color_snprintf(s, size,
840 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300841 (period * 100.0) / total);
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300842 else
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -0300843 ret = scnprintf(s, size, sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300844 (period * 100.0) / total);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800845 if (symbol_conf.show_cpu_utilization) {
846 ret += percent_color_snprintf(s + ret, size - ret,
847 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300848 (period_sys * 100.0) / total);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800849 ret += percent_color_snprintf(s + ret, size - ret,
850 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300851 (period_us * 100.0) / total);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800852 if (perf_guest) {
853 ret += percent_color_snprintf(s + ret,
854 size - ret,
855 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300856 (period_guest_sys * 100.0) /
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800857 total);
858 ret += percent_color_snprintf(s + ret,
859 size - ret,
860 sep ? "%.2f" : " %6.2f%%",
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300861 (period_guest_us * 100.0) /
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800862 total);
863 }
864 }
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300865 } else
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -0300866 ret = scnprintf(s, size, sep ? "%" PRIu64 : "%12" PRIu64 " ", period);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200867
868 if (symbol_conf.show_nr_samples) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200869 if (sep)
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -0300870 ret += scnprintf(s + ret, size - ret, "%c%" PRIu64, *sep, nr_events);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200871 else
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -0300872 ret += scnprintf(s + ret, size - ret, "%11" PRIu64, nr_events);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200873 }
874
Arnaldo Carvalho de Melo3f2728b2011-10-05 16:10:06 -0300875 if (symbol_conf.show_total_period) {
876 if (sep)
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -0300877 ret += scnprintf(s + ret, size - ret, "%c%" PRIu64, *sep, period);
Arnaldo Carvalho de Melo3f2728b2011-10-05 16:10:06 -0300878 else
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -0300879 ret += scnprintf(s + ret, size - ret, " %12" PRIu64, period);
Arnaldo Carvalho de Melo3f2728b2011-10-05 16:10:06 -0300880 }
881
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300882 if (pair_hists) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200883 char bf[32];
884 double old_percent = 0, new_percent = 0, diff;
885
886 if (total > 0)
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300887 old_percent = (period * 100.0) / total;
Arnaldo Carvalho de Melo13d3ee52012-01-04 11:37:15 -0200888 if (total_period > 0)
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200889 new_percent = (he->period * 100.0) / total_period;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200890
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -0200891 diff = new_percent - old_percent;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200892
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -0200893 if (fabs(diff) >= 0.01)
Jiri Olsa4bf9ce12012-03-22 14:37:26 +0100894 scnprintf(bf, sizeof(bf), "%+4.2F%%", diff);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200895 else
Jiri Olsa4bf9ce12012-03-22 14:37:26 +0100896 scnprintf(bf, sizeof(bf), " ");
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200897
898 if (sep)
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -0300899 ret += scnprintf(s + ret, size - ret, "%c%s", *sep, bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200900 else
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -0300901 ret += scnprintf(s + ret, size - ret, "%11.11s", bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200902
903 if (show_displacement) {
904 if (displacement)
Jiri Olsa4bf9ce12012-03-22 14:37:26 +0100905 scnprintf(bf, sizeof(bf), "%+4ld", displacement);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200906 else
Jiri Olsa4bf9ce12012-03-22 14:37:26 +0100907 scnprintf(bf, sizeof(bf), " ");
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200908
909 if (sep)
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -0300910 ret += scnprintf(s + ret, size - ret, "%c%s", *sep, bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200911 else
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -0300912 ret += scnprintf(s + ret, size - ret, "%6.6s", bf);
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200913 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200914 }
915
Arnaldo Carvalho de Melof1cf6022011-10-18 14:37:34 -0200916 return ret;
917}
918
919int hist_entry__snprintf(struct hist_entry *he, char *s, size_t size,
920 struct hists *hists)
921{
922 const char *sep = symbol_conf.field_sep;
923 struct sort_entry *se;
924 int ret = 0;
925
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200926 list_for_each_entry(se, &hist_entry__sort_list, list) {
927 if (se->elide)
928 continue;
929
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -0300930 ret += scnprintf(s + ret, size - ret, "%s", sep ?: " ");
Arnaldo Carvalho de Melof1cf6022011-10-18 14:37:34 -0200931 ret += se->se_snprintf(he, s + ret, size - ret,
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300932 hists__col_len(hists, se->se_width_idx));
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200933 }
934
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300935 return ret;
936}
937
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200938static int hist_entry__fprintf(struct hist_entry *he, size_t size,
939 struct hists *hists, struct hists *pair_hists,
940 bool show_displacement, long displacement,
941 u64 total_period, FILE *fp)
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300942{
943 char bf[512];
Arnaldo Carvalho de Melof1cf6022011-10-18 14:37:34 -0200944 int ret;
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -0300945
946 if (size == 0 || size > sizeof(bf))
947 size = sizeof(bf);
948
Arnaldo Carvalho de Melof1cf6022011-10-18 14:37:34 -0200949 ret = hist_entry__pcnt_snprintf(he, bf, size, pair_hists,
950 show_displacement, displacement,
Arnaldo Carvalho de Melo13d3ee52012-01-04 11:37:15 -0200951 true, total_period);
Arnaldo Carvalho de Melof1cf6022011-10-18 14:37:34 -0200952 hist_entry__snprintf(he, bf + ret, size - ret, hists);
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300953 return fprintf(fp, "%s\n", bf);
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300954}
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200955
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200956static size_t hist_entry__fprintf_callchain(struct hist_entry *he,
957 struct hists *hists,
958 u64 total_period, FILE *fp)
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300959{
960 int left_margin = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200961
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -0300962 if (sort__first_dimension == SORT_COMM) {
963 struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
964 typeof(*se), list);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300965 left_margin = hists__col_len(hists, se->se_width_idx);
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200966 left_margin -= thread__comm_len(he->thread);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200967 }
968
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200969 return hist_entry_callchain__fprintf(he, total_period, left_margin, fp);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200970}
971
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300972size_t hists__fprintf(struct hists *hists, struct hists *pair,
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -0300973 bool show_displacement, bool show_header, int max_rows,
974 int max_cols, FILE *fp)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200975{
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200976 struct sort_entry *se;
977 struct rb_node *nd;
978 size_t ret = 0;
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200979 u64 total_period;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200980 unsigned long position = 1;
981 long displacement = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200982 unsigned int width;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200983 const char *sep = symbol_conf.field_sep;
Arnaldo Carvalho de Meloedb7c602010-05-17 16:22:41 -0300984 const char *col_width = symbol_conf.col_width_list_str;
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -0300985 int nr_rows = 0;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200986
987 init_rem_hits();
988
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -0300989 if (!show_header)
990 goto print_entries;
991
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -0200992 fprintf(fp, "# %s", pair ? "Baseline" : "Overhead");
993
Namhyung Kim0ed35abc2012-01-08 02:25:32 +0900994 if (symbol_conf.show_cpu_utilization) {
995 if (sep) {
996 ret += fprintf(fp, "%csys", *sep);
997 ret += fprintf(fp, "%cus", *sep);
998 if (perf_guest) {
999 ret += fprintf(fp, "%cguest sys", *sep);
1000 ret += fprintf(fp, "%cguest us", *sep);
1001 }
1002 } else {
1003 ret += fprintf(fp, " sys ");
1004 ret += fprintf(fp, " us ");
1005 if (perf_guest) {
1006 ret += fprintf(fp, " guest sys ");
1007 ret += fprintf(fp, " guest us ");
1008 }
1009 }
1010 }
1011
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001012 if (symbol_conf.show_nr_samples) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02001013 if (sep)
1014 fprintf(fp, "%cSamples", *sep);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001015 else
1016 fputs(" Samples ", fp);
1017 }
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02001018
Arnaldo Carvalho de Melo3f2728b2011-10-05 16:10:06 -03001019 if (symbol_conf.show_total_period) {
1020 if (sep)
1021 ret += fprintf(fp, "%cPeriod", *sep);
1022 else
1023 ret += fprintf(fp, " Period ");
1024 }
1025
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02001026 if (pair) {
1027 if (sep)
1028 ret += fprintf(fp, "%cDelta", *sep);
1029 else
1030 ret += fprintf(fp, " Delta ");
1031
1032 if (show_displacement) {
1033 if (sep)
1034 ret += fprintf(fp, "%cDisplacement", *sep);
1035 else
1036 ret += fprintf(fp, " Displ");
1037 }
1038 }
1039
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001040 list_for_each_entry(se, &hist_entry__sort_list, list) {
1041 if (se->elide)
1042 continue;
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02001043 if (sep) {
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +02001044 fprintf(fp, "%c%s", *sep, se->se_header);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001045 continue;
1046 }
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +02001047 width = strlen(se->se_header);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -03001048 if (symbol_conf.col_width_list_str) {
1049 if (col_width) {
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001050 hists__set_col_len(hists, se->se_width_idx,
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -03001051 atoi(col_width));
1052 col_width = strchr(col_width, ',');
1053 if (col_width)
1054 ++col_width;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001055 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001056 }
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001057 if (!hists__new_col_len(hists, se->se_width_idx, width))
1058 width = hists__col_len(hists, se->se_width_idx);
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +02001059 fprintf(fp, " %*s", width, se->se_header);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001060 }
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -03001061
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001062 fprintf(fp, "\n");
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -03001063 if (max_rows && ++nr_rows >= max_rows)
1064 goto out;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001065
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02001066 if (sep)
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001067 goto print_entries;
1068
1069 fprintf(fp, "# ........");
Namhyung Kim0ed35abc2012-01-08 02:25:32 +09001070 if (symbol_conf.show_cpu_utilization)
1071 fprintf(fp, " ....... .......");
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001072 if (symbol_conf.show_nr_samples)
1073 fprintf(fp, " ..........");
Arnaldo Carvalho de Melo3f2728b2011-10-05 16:10:06 -03001074 if (symbol_conf.show_total_period)
1075 fprintf(fp, " ............");
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02001076 if (pair) {
1077 fprintf(fp, " ..........");
1078 if (show_displacement)
1079 fprintf(fp, " .....");
1080 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001081 list_for_each_entry(se, &hist_entry__sort_list, list) {
1082 unsigned int i;
1083
1084 if (se->elide)
1085 continue;
1086
1087 fprintf(fp, " ");
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001088 width = hists__col_len(hists, se->se_width_idx);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -03001089 if (width == 0)
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +02001090 width = strlen(se->se_header);
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001091 for (i = 0; i < width; i++)
1092 fprintf(fp, ".");
1093 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001094
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -03001095 fprintf(fp, "\n");
1096 if (max_rows && ++nr_rows >= max_rows)
1097 goto out;
1098
1099 fprintf(fp, "#\n");
1100 if (max_rows && ++nr_rows >= max_rows)
1101 goto out;
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001102
1103print_entries:
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -02001104 total_period = hists->stats.total_period;
1105
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001106 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02001107 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1108
Frederic Weisbeckere84d2122011-06-29 22:23:03 +02001109 if (h->filtered)
1110 continue;
1111
Arnaldo Carvalho de Meloc351c282009-12-16 13:49:27 -02001112 if (show_displacement) {
1113 if (h->pair != NULL)
1114 displacement = ((long)h->pair->position -
1115 (long)position);
1116 else
1117 displacement = 0;
1118 ++position;
1119 }
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -03001120 ret += hist_entry__fprintf(h, max_cols, hists, pair, show_displacement,
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -02001121 displacement, total_period, fp);
Arnaldo Carvalho de Melo3997d372010-03-12 12:46:48 -03001122
1123 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -02001124 ret += hist_entry__fprintf_callchain(h, hists, total_period, fp);
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -03001125 if (max_rows && ++nr_rows >= max_rows)
1126 goto out;
1127
Arnaldo Carvalho de Melo59fd5302010-03-24 16:40:17 -03001128 if (h->ms.map == NULL && verbose > 1) {
Arnaldo Carvalho de Melo65f2ed22010-03-09 15:58:17 -03001129 __map_groups__fprintf_maps(&h->thread->mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -03001130 MAP__FUNCTION, verbose, fp);
Arnaldo Carvalho de Melo65f2ed22010-03-09 15:58:17 -03001131 fprintf(fp, "%.10s end\n", graph_dotted_line);
1132 }
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001133 }
Arnaldo Carvalho de Meloef9dfe62011-09-26 12:46:11 -03001134out:
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -02001135 free(rem_sq_bracket);
1136
1137 return ret;
1138}
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001139
Arnaldo Carvalho de Melo06daaab2010-07-21 17:58:25 -03001140/*
1141 * See hists__fprintf to match the column widths
1142 */
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001143unsigned int hists__sort_list_width(struct hists *hists)
Arnaldo Carvalho de Melo06daaab2010-07-21 17:58:25 -03001144{
1145 struct sort_entry *se;
1146 int ret = 9; /* total % */
1147
1148 if (symbol_conf.show_cpu_utilization) {
1149 ret += 7; /* count_sys % */
1150 ret += 6; /* count_us % */
1151 if (perf_guest) {
1152 ret += 13; /* count_guest_sys % */
1153 ret += 12; /* count_guest_us % */
1154 }
1155 }
1156
1157 if (symbol_conf.show_nr_samples)
1158 ret += 11;
1159
Arnaldo Carvalho de Melo3f2728b2011-10-05 16:10:06 -03001160 if (symbol_conf.show_total_period)
1161 ret += 13;
1162
Arnaldo Carvalho de Melo06daaab2010-07-21 17:58:25 -03001163 list_for_each_entry(se, &hist_entry__sort_list, list)
1164 if (!se->elide)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001165 ret += 2 + hists__col_len(hists, se->se_width_idx);
Arnaldo Carvalho de Melo06daaab2010-07-21 17:58:25 -03001166
Arnaldo Carvalho de Melo903cce62010-08-05 19:15:48 -03001167 if (verbose) /* Addr + origin */
1168 ret += 3 + BITS_PER_LONG / 4;
1169
Arnaldo Carvalho de Melo06daaab2010-07-21 17:58:25 -03001170 return ret;
1171}
1172
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001173static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001174 enum hist_filter filter)
1175{
1176 h->filtered &= ~(1 << filter);
1177 if (h->filtered)
1178 return;
1179
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001180 ++hists->nr_entries;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -03001181 if (h->ms.unfolded)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001182 hists->nr_entries += h->nr_rows;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -03001183 h->row_offset = 0;
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001184 hists->stats.total_period += h->period;
1185 hists->stats.nr_events[PERF_RECORD_SAMPLE] += h->nr_events;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001186
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001187 hists__calc_col_len(hists, h);
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001188}
1189
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001190
1191static bool hists__filter_entry_by_dso(struct hists *hists,
1192 struct hist_entry *he)
1193{
1194 if (hists->dso_filter != NULL &&
1195 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
1196 he->filtered |= (1 << HIST_FILTER__DSO);
1197 return true;
1198 }
1199
1200 return false;
1201}
1202
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001203void hists__filter_by_dso(struct hists *hists)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001204{
1205 struct rb_node *nd;
1206
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001207 hists->nr_entries = hists->stats.total_period = 0;
1208 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
1209 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001210
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001211 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001212 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1213
1214 if (symbol_conf.exclude_other && !h->parent)
1215 continue;
1216
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001217 if (hists__filter_entry_by_dso(hists, h))
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001218 continue;
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001219
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001220 hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001221 }
1222}
1223
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001224static bool hists__filter_entry_by_thread(struct hists *hists,
1225 struct hist_entry *he)
1226{
1227 if (hists->thread_filter != NULL &&
1228 he->thread != hists->thread_filter) {
1229 he->filtered |= (1 << HIST_FILTER__THREAD);
1230 return true;
1231 }
1232
1233 return false;
1234}
1235
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001236void hists__filter_by_thread(struct hists *hists)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001237{
1238 struct rb_node *nd;
1239
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001240 hists->nr_entries = hists->stats.total_period = 0;
1241 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
1242 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001243
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001244 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001245 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1246
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001247 if (hists__filter_entry_by_thread(hists, h))
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001248 continue;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001249
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001250 hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001251 }
1252}
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001253
Namhyung Kime94d53e2012-03-16 17:50:51 +09001254static bool hists__filter_entry_by_symbol(struct hists *hists,
1255 struct hist_entry *he)
1256{
1257 if (hists->symbol_filter_str != NULL &&
1258 (!he->ms.sym || strstr(he->ms.sym->name,
1259 hists->symbol_filter_str) == NULL)) {
1260 he->filtered |= (1 << HIST_FILTER__SYMBOL);
1261 return true;
1262 }
1263
1264 return false;
1265}
1266
1267void hists__filter_by_symbol(struct hists *hists)
1268{
1269 struct rb_node *nd;
1270
1271 hists->nr_entries = hists->stats.total_period = 0;
1272 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
1273 hists__reset_col_len(hists);
1274
1275 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1276 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1277
1278 if (hists__filter_entry_by_symbol(hists, h))
1279 continue;
1280
1281 hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
1282 }
1283}
1284
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -02001285int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001286{
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -02001287 return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001288}
1289
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -02001290int hist_entry__annotate(struct hist_entry *he, size_t privsize)
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001291{
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -02001292 return symbol__annotate(he->ms.sym, he->ms.map, privsize);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001293}
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001294
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001295void hists__inc_nr_events(struct hists *hists, u32 type)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001296{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001297 ++hists->stats.nr_events[0];
1298 ++hists->stats.nr_events[type];
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001299}
1300
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001301size_t hists__fprintf_nr_events(struct hists *hists, FILE *fp)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001302{
1303 int i;
1304 size_t ret = 0;
1305
1306 for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001307 const char *name;
Thomas Gleixner3835bc02010-12-07 12:48:42 +00001308
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001309 if (hists->stats.nr_events[i] == 0)
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001310 continue;
1311
1312 name = perf_event__name(i);
Thomas Gleixner3835bc02010-12-07 12:48:42 +00001313 if (!strcmp(name, "UNKNOWN"))
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001314 continue;
Thomas Gleixner3835bc02010-12-07 12:48:42 +00001315
1316 ret += fprintf(fp, "%16s events: %10d\n", name,
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001317 hists->stats.nr_events[i]);
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001318 }
1319
1320 return ret;
1321}