blob: 30793f98c8bb4e47d98f0cb697a35d999bfb4f22 [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"
Namhyung Kim29d720e2013-01-22 18:09:33 +09007#include "evsel.h"
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -02008#include <math.h>
John Kacur3d1d07e2009-09-28 15:32:55 +02009
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -020010static bool hists__filter_entry_by_dso(struct hists *hists,
11 struct hist_entry *he);
12static bool hists__filter_entry_by_thread(struct hists *hists,
13 struct hist_entry *he);
Namhyung Kime94d53e2012-03-16 17:50:51 +090014static bool hists__filter_entry_by_symbol(struct hists *hists,
15 struct hist_entry *he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -020016
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -030017enum hist_filter {
18 HIST_FILTER__DSO,
19 HIST_FILTER__THREAD,
20 HIST_FILTER__PARENT,
Namhyung Kime94d53e2012-03-16 17:50:51 +090021 HIST_FILTER__SYMBOL,
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -030022};
23
John Kacur3d1d07e2009-09-28 15:32:55 +020024struct callchain_param callchain_param = {
25 .mode = CHAIN_GRAPH_REL,
Sam Liaod797fdc2011-06-07 23:49:46 +080026 .min_percent = 0.5,
Andi Kleen99571ab2013-07-18 15:33:57 -070027 .order = ORDER_CALLEE,
28 .key = CCKEY_FUNCTION
John Kacur3d1d07e2009-09-28 15:32:55 +020029};
30
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030031u16 hists__col_len(struct hists *hists, enum hist_column col)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030032{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030033 return hists->col_len[col];
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030034}
35
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030036void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030037{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030038 hists->col_len[col] = len;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030039}
40
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030041bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030042{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030043 if (len > hists__col_len(hists, col)) {
44 hists__set_col_len(hists, col, len);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030045 return true;
46 }
47 return false;
48}
49
Namhyung Kim7ccf4f92012-08-20 13:52:05 +090050void hists__reset_col_len(struct hists *hists)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030051{
52 enum hist_column col;
53
54 for (col = 0; col < HISTC_NR_COLS; ++col)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030055 hists__set_col_len(hists, col, 0);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030056}
57
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010058static void hists__set_unres_dso_col_len(struct hists *hists, int dso)
59{
60 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
61
62 if (hists__col_len(hists, dso) < unresolved_col_width &&
63 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
64 !symbol_conf.dso_list)
65 hists__set_col_len(hists, dso, unresolved_col_width);
66}
67
Namhyung Kim7ccf4f92012-08-20 13:52:05 +090068void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030069{
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010070 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
Stephane Eranian98a3b322013-01-24 16:10:35 +010071 int symlen;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030072 u16 len;
73
Namhyung Kimded19d52013-04-01 20:35:19 +090074 /*
75 * +4 accounts for '[x] ' priv level info
76 * +2 accounts for 0x prefix on raw addresses
77 * +3 accounts for ' y ' symtab origin info
78 */
79 if (h->ms.sym) {
80 symlen = h->ms.sym->namelen + 4;
81 if (verbose)
82 symlen += BITS_PER_LONG / 4 + 2 + 3;
83 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
84 } else {
Stephane Eranian98a3b322013-01-24 16:10:35 +010085 symlen = unresolved_col_width + 4 + 2;
86 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010087 hists__set_unres_dso_col_len(hists, HISTC_DSO);
Stephane Eranian98a3b322013-01-24 16:10:35 +010088 }
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030089
90 len = thread__comm_len(h->thread);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030091 if (hists__new_col_len(hists, HISTC_COMM, len))
92 hists__set_col_len(hists, HISTC_THREAD, len + 6);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030093
94 if (h->ms.map) {
95 len = dso__name_len(h->ms.map->dso);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030096 hists__new_col_len(hists, HISTC_DSO, len);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030097 }
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010098
Namhyung Kimcb993742012-12-27 18:11:42 +090099 if (h->parent)
100 hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen);
101
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100102 if (h->branch_info) {
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100103 if (h->branch_info->from.sym) {
104 symlen = (int)h->branch_info->from.sym->namelen + 4;
Namhyung Kimded19d52013-04-01 20:35:19 +0900105 if (verbose)
106 symlen += BITS_PER_LONG / 4 + 2 + 3;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100107 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
108
109 symlen = dso__name_len(h->branch_info->from.map->dso);
110 hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
111 } else {
112 symlen = unresolved_col_width + 4 + 2;
113 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
114 hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
115 }
116
117 if (h->branch_info->to.sym) {
118 symlen = (int)h->branch_info->to.sym->namelen + 4;
Namhyung Kimded19d52013-04-01 20:35:19 +0900119 if (verbose)
120 symlen += BITS_PER_LONG / 4 + 2 + 3;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100121 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
122
123 symlen = dso__name_len(h->branch_info->to.map->dso);
124 hists__new_col_len(hists, HISTC_DSO_TO, symlen);
125 } else {
126 symlen = unresolved_col_width + 4 + 2;
127 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
128 hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
129 }
130 }
Stephane Eranian98a3b322013-01-24 16:10:35 +0100131
132 if (h->mem_info) {
Stephane Eranian98a3b322013-01-24 16:10:35 +0100133 if (h->mem_info->daddr.sym) {
134 symlen = (int)h->mem_info->daddr.sym->namelen + 4
135 + unresolved_col_width + 2;
136 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
137 symlen);
138 } else {
139 symlen = unresolved_col_width + 4 + 2;
140 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
141 symlen);
142 }
143 if (h->mem_info->daddr.map) {
144 symlen = dso__name_len(h->mem_info->daddr.map->dso);
145 hists__new_col_len(hists, HISTC_MEM_DADDR_DSO,
146 symlen);
147 } else {
148 symlen = unresolved_col_width + 4 + 2;
149 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
150 }
151 } else {
152 symlen = unresolved_col_width + 4 + 2;
153 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen);
154 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
155 }
156
157 hists__new_col_len(hists, HISTC_MEM_LOCKED, 6);
158 hists__new_col_len(hists, HISTC_MEM_TLB, 22);
159 hists__new_col_len(hists, HISTC_MEM_SNOOP, 12);
160 hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3);
161 hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12);
162 hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12);
Andi Kleen475eeab2013-09-20 07:40:43 -0700163
164 if (h->transaction)
165 hists__new_col_len(hists, HISTC_TRANSACTION,
166 hist_entry__transaction_len());
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300167}
168
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900169void hists__output_recalc_col_len(struct hists *hists, int max_rows)
170{
171 struct rb_node *next = rb_first(&hists->entries);
172 struct hist_entry *n;
173 int row = 0;
174
175 hists__reset_col_len(hists);
176
177 while (next && row++ < max_rows) {
178 n = rb_entry(next, struct hist_entry, rb_node);
179 if (!n->filtered)
180 hists__calc_col_len(hists, n);
181 next = rb_next(&n->rb_node);
182 }
183}
184
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200185static void hist_entry__add_cpumode_period(struct hist_entry *he,
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300186 unsigned int cpumode, u64 period)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800187{
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300188 switch (cpumode) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800189 case PERF_RECORD_MISC_KERNEL:
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900190 he->stat.period_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800191 break;
192 case PERF_RECORD_MISC_USER:
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900193 he->stat.period_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800194 break;
195 case PERF_RECORD_MISC_GUEST_KERNEL:
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900196 he->stat.period_guest_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800197 break;
198 case PERF_RECORD_MISC_GUEST_USER:
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900199 he->stat.period_guest_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800200 break;
201 default:
202 break;
203 }
204}
205
Andi Kleen05484292013-01-24 16:10:29 +0100206static void he_stat__add_period(struct he_stat *he_stat, u64 period,
207 u64 weight)
Namhyung Kim139c0812012-10-04 21:49:43 +0900208{
Stephane Eranian98a3b322013-01-24 16:10:35 +0100209
Namhyung Kim139c0812012-10-04 21:49:43 +0900210 he_stat->period += period;
Andi Kleen05484292013-01-24 16:10:29 +0100211 he_stat->weight += weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900212 he_stat->nr_events += 1;
213}
214
215static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
216{
217 dest->period += src->period;
218 dest->period_sys += src->period_sys;
219 dest->period_us += src->period_us;
220 dest->period_guest_sys += src->period_guest_sys;
221 dest->period_guest_us += src->period_guest_us;
222 dest->nr_events += src->nr_events;
Andi Kleen05484292013-01-24 16:10:29 +0100223 dest->weight += src->weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900224}
225
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300226static void hist_entry__decay(struct hist_entry *he)
227{
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900228 he->stat.period = (he->stat.period * 7) / 8;
229 he->stat.nr_events = (he->stat.nr_events * 7) / 8;
Andi Kleen05484292013-01-24 16:10:29 +0100230 /* XXX need decay for weight too? */
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300231}
232
233static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
234{
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900235 u64 prev_period = he->stat.period;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200236
237 if (prev_period == 0)
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300238 return true;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200239
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300240 hist_entry__decay(he);
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200241
242 if (!he->filtered)
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900243 hists->stats.total_period -= prev_period - he->stat.period;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200244
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900245 return he->stat.period == 0;
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300246}
247
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900248void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300249{
250 struct rb_node *next = rb_first(&hists->entries);
251 struct hist_entry *n;
252
253 while (next) {
254 n = rb_entry(next, struct hist_entry, rb_node);
255 next = rb_next(&n->rb_node);
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300256 /*
257 * We may be annotating this, for instance, so keep it here in
258 * case some it gets new samples, we'll eventually free it when
259 * the user stops browsing and it agains gets fully decayed.
260 */
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200261 if (((zap_user && n->level == '.') ||
262 (zap_kernel && n->level != '.') ||
263 hists__decay_entry(hists, n)) &&
264 !n->used) {
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300265 rb_erase(&n->rb_node, &hists->entries);
266
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900267 if (sort__need_collapse)
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300268 rb_erase(&n->rb_node_in, &hists->entries_collapsed);
269
270 hist_entry__free(n);
271 --hists->nr_entries;
272 }
273 }
274}
275
John Kacur3d1d07e2009-09-28 15:32:55 +0200276/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300277 * histogram, sorted on item, collects periods
John Kacur3d1d07e2009-09-28 15:32:55 +0200278 */
279
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300280static struct hist_entry *hist_entry__new(struct hist_entry *template)
281{
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +0200282 size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0;
Stephane Eranian98a3b322013-01-24 16:10:35 +0100283 struct hist_entry *he = zalloc(sizeof(*he) + callchain_size);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300284
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200285 if (he != NULL) {
286 *he = *template;
Namhyung Kimc4b35352012-10-04 21:49:42 +0900287
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200288 if (he->ms.map)
289 he->ms.map->referenced = true;
Stephane Eranian3cf0cb12013-01-14 15:02:45 +0100290
291 if (he->branch_info) {
Namhyung Kim26353a62013-04-01 20:35:17 +0900292 /*
293 * This branch info is (a part of) allocated from
294 * machine__resolve_bstack() and will be freed after
295 * adding new entries. So we need to save a copy.
296 */
297 he->branch_info = malloc(sizeof(*he->branch_info));
298 if (he->branch_info == NULL) {
299 free(he);
300 return NULL;
301 }
302
303 memcpy(he->branch_info, template->branch_info,
304 sizeof(*he->branch_info));
305
Stephane Eranian3cf0cb12013-01-14 15:02:45 +0100306 if (he->branch_info->from.map)
307 he->branch_info->from.map->referenced = true;
308 if (he->branch_info->to.map)
309 he->branch_info->to.map->referenced = true;
310 }
311
Stephane Eranian98a3b322013-01-24 16:10:35 +0100312 if (he->mem_info) {
313 if (he->mem_info->iaddr.map)
314 he->mem_info->iaddr.map->referenced = true;
315 if (he->mem_info->daddr.map)
316 he->mem_info->daddr.map->referenced = true;
317 }
318
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300319 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200320 callchain_init(he->callchain);
Arnaldo Carvalho de Melob821c732012-10-25 14:42:45 -0200321
322 INIT_LIST_HEAD(&he->pairs.node);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300323 }
324
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200325 return he;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300326}
327
Namhyung Kim66f97ed2012-12-10 17:29:56 +0900328void hists__inc_nr_entries(struct hists *hists, struct hist_entry *h)
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300329{
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300330 if (!h->filtered) {
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300331 hists__calc_col_len(hists, h);
332 ++hists->nr_entries;
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900333 hists->stats.total_period += h->stat.period;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300334 }
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300335}
336
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300337static u8 symbol__parent_filter(const struct symbol *parent)
338{
339 if (symbol_conf.exclude_other && parent == NULL)
340 return 1 << HIST_FILTER__PARENT;
341 return 0;
342}
343
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100344static struct hist_entry *add_hist_entry(struct hists *hists,
345 struct hist_entry *entry,
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300346 struct addr_location *al,
Andi Kleen05484292013-01-24 16:10:29 +0100347 u64 period,
348 u64 weight)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300349{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300350 struct rb_node **p;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300351 struct rb_node *parent = NULL;
352 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -0700353 int64_t cmp;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300354
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300355 p = &hists->entries_in->rb_node;
356
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300357 while (*p != NULL) {
358 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300359 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300360
Namhyung Kim9afcf932012-12-10 17:29:54 +0900361 /*
362 * Make sure that it receives arguments in a same order as
363 * hist_entry__collapse() so that we can use an appropriate
364 * function when searching an entry regardless which sort
365 * keys were used.
366 */
367 cmp = hist_entry__cmp(he, entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300368
369 if (!cmp) {
Andi Kleen05484292013-01-24 16:10:29 +0100370 he_stat__add_period(&he->stat, period, weight);
David Miller63fa4712012-03-27 03:14:18 -0400371
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900372 /*
373 * This mem info was allocated from machine__resolve_mem
374 * and will not be used anymore.
375 */
376 free(entry->mem_info);
377
David Miller63fa4712012-03-27 03:14:18 -0400378 /* If the map of an existing hist_entry has
379 * become out-of-date due to an exec() or
380 * similar, update it. Otherwise we will
381 * mis-adjust symbol addresses when computing
382 * the history counter to increment.
383 */
384 if (he->ms.map != entry->ms.map) {
385 he->ms.map = entry->ms.map;
386 if (he->ms.map)
387 he->ms.map->referenced = true;
388 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300389 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300390 }
391
392 if (cmp < 0)
393 p = &(*p)->rb_left;
394 else
395 p = &(*p)->rb_right;
396 }
397
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100398 he = hist_entry__new(entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300399 if (!he)
Namhyung Kim27a0dcb2013-05-14 11:09:02 +0900400 return NULL;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300401
Namhyung Kimc1fb5652013-10-11 14:15:38 +0900402 hists->nr_entries++;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300403 rb_link_node(&he->rb_node_in, parent, p);
404 rb_insert_color(&he->rb_node_in, hists->entries_in);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300405out:
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300406 hist_entry__add_cpumode_period(he, al->cpumode, period);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300407 return he;
408}
409
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300410struct hist_entry *__hists__add_mem_entry(struct hists *hists,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100411 struct addr_location *al,
412 struct symbol *sym_parent,
413 struct mem_info *mi,
414 u64 period,
415 u64 weight)
416{
417 struct hist_entry entry = {
418 .thread = al->thread,
Namhyung Kim4dfced32013-09-13 16:28:57 +0900419 .comm = thread__comm(al->thread),
Stephane Eranian98a3b322013-01-24 16:10:35 +0100420 .ms = {
421 .map = al->map,
422 .sym = al->sym,
423 },
424 .stat = {
425 .period = period,
426 .weight = weight,
427 .nr_events = 1,
428 },
429 .cpu = al->cpu,
430 .ip = al->addr,
431 .level = al->level,
432 .parent = sym_parent,
433 .filtered = symbol__parent_filter(sym_parent),
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300434 .hists = hists,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100435 .mem_info = mi,
436 .branch_info = NULL,
437 };
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300438 return add_hist_entry(hists, &entry, al, period, weight);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100439}
440
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300441struct hist_entry *__hists__add_branch_entry(struct hists *hists,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100442 struct addr_location *al,
443 struct symbol *sym_parent,
444 struct branch_info *bi,
Andi Kleen05484292013-01-24 16:10:29 +0100445 u64 period,
446 u64 weight)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100447{
448 struct hist_entry entry = {
449 .thread = al->thread,
Namhyung Kim4dfced32013-09-13 16:28:57 +0900450 .comm = thread__comm(al->thread),
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100451 .ms = {
452 .map = bi->to.map,
453 .sym = bi->to.sym,
454 },
455 .cpu = al->cpu,
456 .ip = bi->to.addr,
457 .level = al->level,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900458 .stat = {
459 .period = period,
Namhyung Kimc4b35352012-10-04 21:49:42 +0900460 .nr_events = 1,
Andi Kleen05484292013-01-24 16:10:29 +0100461 .weight = weight,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900462 },
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100463 .parent = sym_parent,
464 .filtered = symbol__parent_filter(sym_parent),
465 .branch_info = bi,
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300466 .hists = hists,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100467 .mem_info = NULL,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100468 };
469
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300470 return add_hist_entry(hists, &entry, al, period, weight);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100471}
472
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300473struct hist_entry *__hists__add_entry(struct hists *hists,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100474 struct addr_location *al,
Andi Kleen05484292013-01-24 16:10:29 +0100475 struct symbol *sym_parent, u64 period,
Andi Kleen475eeab2013-09-20 07:40:43 -0700476 u64 weight, u64 transaction)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100477{
478 struct hist_entry entry = {
479 .thread = al->thread,
Namhyung Kim4dfced32013-09-13 16:28:57 +0900480 .comm = thread__comm(al->thread),
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100481 .ms = {
482 .map = al->map,
483 .sym = al->sym,
484 },
485 .cpu = al->cpu,
486 .ip = al->addr,
487 .level = al->level,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900488 .stat = {
489 .period = period,
Namhyung Kimc4b35352012-10-04 21:49:42 +0900490 .nr_events = 1,
Andi Kleen05484292013-01-24 16:10:29 +0100491 .weight = weight,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900492 },
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100493 .parent = sym_parent,
494 .filtered = symbol__parent_filter(sym_parent),
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300495 .hists = hists,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100496 .branch_info = NULL,
497 .mem_info = NULL,
Andi Kleen475eeab2013-09-20 07:40:43 -0700498 .transaction = transaction,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100499 };
500
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300501 return add_hist_entry(hists, &entry, al, period, weight);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100502}
503
John Kacur3d1d07e2009-09-28 15:32:55 +0200504int64_t
505hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
506{
507 struct sort_entry *se;
508 int64_t cmp = 0;
509
510 list_for_each_entry(se, &hist_entry__sort_list, list) {
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200511 cmp = se->se_cmp(left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200512 if (cmp)
513 break;
514 }
515
516 return cmp;
517}
518
519int64_t
520hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
521{
522 struct sort_entry *se;
523 int64_t cmp = 0;
524
525 list_for_each_entry(se, &hist_entry__sort_list, list) {
526 int64_t (*f)(struct hist_entry *, struct hist_entry *);
527
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200528 f = se->se_collapse ?: se->se_cmp;
John Kacur3d1d07e2009-09-28 15:32:55 +0200529
530 cmp = f(left, right);
531 if (cmp)
532 break;
533 }
534
535 return cmp;
536}
537
538void hist_entry__free(struct hist_entry *he)
539{
Namhyung Kim580e3382012-11-07 16:27:14 +0900540 free(he->branch_info);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100541 free(he->mem_info);
Namhyung Kimf048d542013-09-11 14:09:28 +0900542 free_srcline(he->srcline);
John Kacur3d1d07e2009-09-28 15:32:55 +0200543 free(he);
544}
545
546/*
547 * collapse the histogram
548 */
549
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300550static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100551 struct rb_root *root,
552 struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +0200553{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200554 struct rb_node **p = &root->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200555 struct rb_node *parent = NULL;
556 struct hist_entry *iter;
557 int64_t cmp;
558
559 while (*p != NULL) {
560 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300561 iter = rb_entry(parent, struct hist_entry, rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +0200562
563 cmp = hist_entry__collapse(iter, he);
564
565 if (!cmp) {
Namhyung Kim139c0812012-10-04 21:49:43 +0900566 he_stat__add_stat(&iter->stat, &he->stat);
Namhyung Kim9ec60972012-09-26 16:47:28 +0900567
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100568 if (symbol_conf.use_callchain) {
Namhyung Kim47260642012-05-31 14:43:26 +0900569 callchain_cursor_reset(&callchain_cursor);
570 callchain_merge(&callchain_cursor,
571 iter->callchain,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100572 he->callchain);
573 }
John Kacur3d1d07e2009-09-28 15:32:55 +0200574 hist_entry__free(he);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300575 return false;
John Kacur3d1d07e2009-09-28 15:32:55 +0200576 }
577
578 if (cmp < 0)
579 p = &(*p)->rb_left;
580 else
581 p = &(*p)->rb_right;
582 }
583
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300584 rb_link_node(&he->rb_node_in, parent, p);
585 rb_insert_color(&he->rb_node_in, root);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300586 return true;
John Kacur3d1d07e2009-09-28 15:32:55 +0200587}
588
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300589static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
590{
591 struct rb_root *root;
592
593 pthread_mutex_lock(&hists->lock);
594
595 root = hists->entries_in;
596 if (++hists->entries_in > &hists->entries_in_array[1])
597 hists->entries_in = &hists->entries_in_array[0];
598
599 pthread_mutex_unlock(&hists->lock);
600
601 return root;
602}
603
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200604static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
605{
606 hists__filter_entry_by_dso(hists, he);
607 hists__filter_entry_by_thread(hists, he);
Namhyung Kime94d53e2012-03-16 17:50:51 +0900608 hists__filter_entry_by_symbol(hists, he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200609}
610
Namhyung Kimc1fb5652013-10-11 14:15:38 +0900611void hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300612{
613 struct rb_root *root;
614 struct rb_node *next;
615 struct hist_entry *n;
616
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900617 if (!sort__need_collapse)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300618 return;
619
620 root = hists__get_rotate_entries_in(hists);
621 next = rb_first(root);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300622
623 while (next) {
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -0300624 if (session_done())
625 break;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300626 n = rb_entry(next, struct hist_entry, rb_node_in);
627 next = rb_next(&n->rb_node_in);
628
629 rb_erase(&n->rb_node_in, root);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200630 if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
631 /*
632 * If it wasn't combined with one of the entries already
633 * collapsed, we need to apply the filters that may have
634 * been set by, say, the hist_browser.
635 */
636 hists__apply_filters(hists, n);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200637 }
Namhyung Kimc1fb5652013-10-11 14:15:38 +0900638 if (prog)
639 ui_progress__update(prog, 1);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300640 }
641}
642
John Kacur3d1d07e2009-09-28 15:32:55 +0200643/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300644 * reverse the map, sort on period.
John Kacur3d1d07e2009-09-28 15:32:55 +0200645 */
646
Namhyung Kim29d720e2013-01-22 18:09:33 +0900647static int period_cmp(u64 period_a, u64 period_b)
648{
649 if (period_a > period_b)
650 return 1;
651 if (period_a < period_b)
652 return -1;
653 return 0;
654}
655
656static int hist_entry__sort_on_period(struct hist_entry *a,
657 struct hist_entry *b)
658{
659 int ret;
660 int i, nr_members;
661 struct perf_evsel *evsel;
662 struct hist_entry *pair;
663 u64 *periods_a, *periods_b;
664
665 ret = period_cmp(a->stat.period, b->stat.period);
666 if (ret || !symbol_conf.event_group)
667 return ret;
668
669 evsel = hists_to_evsel(a->hists);
670 nr_members = evsel->nr_members;
671 if (nr_members <= 1)
672 return ret;
673
674 periods_a = zalloc(sizeof(periods_a) * nr_members);
675 periods_b = zalloc(sizeof(periods_b) * nr_members);
676
677 if (!periods_a || !periods_b)
678 goto out;
679
680 list_for_each_entry(pair, &a->pairs.head, pairs.node) {
681 evsel = hists_to_evsel(pair->hists);
682 periods_a[perf_evsel__group_idx(evsel)] = pair->stat.period;
683 }
684
685 list_for_each_entry(pair, &b->pairs.head, pairs.node) {
686 evsel = hists_to_evsel(pair->hists);
687 periods_b[perf_evsel__group_idx(evsel)] = pair->stat.period;
688 }
689
690 for (i = 1; i < nr_members; i++) {
691 ret = period_cmp(periods_a[i], periods_b[i]);
692 if (ret)
693 break;
694 }
695
696out:
697 free(periods_a);
698 free(periods_b);
699
700 return ret;
701}
702
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300703static void __hists__insert_output_entry(struct rb_root *entries,
704 struct hist_entry *he,
705 u64 min_callchain_hits)
John Kacur3d1d07e2009-09-28 15:32:55 +0200706{
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300707 struct rb_node **p = &entries->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200708 struct rb_node *parent = NULL;
709 struct hist_entry *iter;
710
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200711 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melob9fb9302010-04-02 09:50:42 -0300712 callchain_param.sort(&he->sorted_chain, he->callchain,
John Kacur3d1d07e2009-09-28 15:32:55 +0200713 min_callchain_hits, &callchain_param);
714
715 while (*p != NULL) {
716 parent = *p;
717 iter = rb_entry(parent, struct hist_entry, rb_node);
718
Namhyung Kim29d720e2013-01-22 18:09:33 +0900719 if (hist_entry__sort_on_period(he, iter) > 0)
John Kacur3d1d07e2009-09-28 15:32:55 +0200720 p = &(*p)->rb_left;
721 else
722 p = &(*p)->rb_right;
723 }
724
725 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300726 rb_insert_color(&he->rb_node, entries);
John Kacur3d1d07e2009-09-28 15:32:55 +0200727}
728
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900729void hists__output_resort(struct hists *hists)
John Kacur3d1d07e2009-09-28 15:32:55 +0200730{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300731 struct rb_root *root;
John Kacur3d1d07e2009-09-28 15:32:55 +0200732 struct rb_node *next;
733 struct hist_entry *n;
John Kacur3d1d07e2009-09-28 15:32:55 +0200734 u64 min_callchain_hits;
735
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300736 min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
John Kacur3d1d07e2009-09-28 15:32:55 +0200737
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900738 if (sort__need_collapse)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300739 root = &hists->entries_collapsed;
740 else
741 root = hists->entries_in;
742
743 next = rb_first(root);
744 hists->entries = RB_ROOT;
John Kacur3d1d07e2009-09-28 15:32:55 +0200745
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300746 hists->nr_entries = 0;
Arnaldo Carvalho de Melo79286312011-10-27 09:19:48 -0200747 hists->stats.total_period = 0;
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300748 hists__reset_col_len(hists);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300749
John Kacur3d1d07e2009-09-28 15:32:55 +0200750 while (next) {
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300751 n = rb_entry(next, struct hist_entry, rb_node_in);
752 next = rb_next(&n->rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +0200753
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300754 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300755 hists__inc_nr_entries(hists, n);
John Kacur3d1d07e2009-09-28 15:32:55 +0200756 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300757}
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200758
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300759static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300760 enum hist_filter filter)
761{
762 h->filtered &= ~(1 << filter);
763 if (h->filtered)
764 return;
765
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300766 ++hists->nr_entries;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -0300767 if (h->ms.unfolded)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300768 hists->nr_entries += h->nr_rows;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -0300769 h->row_offset = 0;
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900770 hists->stats.total_period += h->stat.period;
771 hists->stats.nr_events[PERF_RECORD_SAMPLE] += h->stat.nr_events;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300772
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300773 hists__calc_col_len(hists, h);
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300774}
775
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200776
777static bool hists__filter_entry_by_dso(struct hists *hists,
778 struct hist_entry *he)
779{
780 if (hists->dso_filter != NULL &&
781 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
782 he->filtered |= (1 << HIST_FILTER__DSO);
783 return true;
784 }
785
786 return false;
787}
788
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200789void hists__filter_by_dso(struct hists *hists)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300790{
791 struct rb_node *nd;
792
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300793 hists->nr_entries = hists->stats.total_period = 0;
794 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
795 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300796
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300797 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300798 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
799
800 if (symbol_conf.exclude_other && !h->parent)
801 continue;
802
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200803 if (hists__filter_entry_by_dso(hists, h))
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300804 continue;
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300805
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300806 hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300807 }
808}
809
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200810static bool hists__filter_entry_by_thread(struct hists *hists,
811 struct hist_entry *he)
812{
813 if (hists->thread_filter != NULL &&
814 he->thread != hists->thread_filter) {
815 he->filtered |= (1 << HIST_FILTER__THREAD);
816 return true;
817 }
818
819 return false;
820}
821
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200822void hists__filter_by_thread(struct hists *hists)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300823{
824 struct rb_node *nd;
825
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300826 hists->nr_entries = hists->stats.total_period = 0;
827 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
828 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300829
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300830 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300831 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
832
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200833 if (hists__filter_entry_by_thread(hists, h))
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300834 continue;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300835
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300836 hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300837 }
838}
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300839
Namhyung Kime94d53e2012-03-16 17:50:51 +0900840static bool hists__filter_entry_by_symbol(struct hists *hists,
841 struct hist_entry *he)
842{
843 if (hists->symbol_filter_str != NULL &&
844 (!he->ms.sym || strstr(he->ms.sym->name,
845 hists->symbol_filter_str) == NULL)) {
846 he->filtered |= (1 << HIST_FILTER__SYMBOL);
847 return true;
848 }
849
850 return false;
851}
852
853void hists__filter_by_symbol(struct hists *hists)
854{
855 struct rb_node *nd;
856
857 hists->nr_entries = hists->stats.total_period = 0;
858 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
859 hists__reset_col_len(hists);
860
861 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
862 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
863
864 if (hists__filter_entry_by_symbol(hists, h))
865 continue;
866
867 hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
868 }
869}
870
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200871int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300872{
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200873 return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300874}
875
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200876int hist_entry__annotate(struct hist_entry *he, size_t privsize)
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300877{
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200878 return symbol__annotate(he->ms.sym, he->ms.map, privsize);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300879}
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -0300880
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -0300881void events_stats__inc(struct events_stats *stats, u32 type)
882{
883 ++stats->nr_events[0];
884 ++stats->nr_events[type];
885}
886
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300887void hists__inc_nr_events(struct hists *hists, u32 type)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -0300888{
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -0300889 events_stats__inc(&hists->stats, type);
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -0300890}
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300891
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300892static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
893 struct hist_entry *pair)
894{
Namhyung Kimce74f602012-12-10 17:29:55 +0900895 struct rb_root *root;
896 struct rb_node **p;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300897 struct rb_node *parent = NULL;
898 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -0700899 int64_t cmp;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300900
Namhyung Kimce74f602012-12-10 17:29:55 +0900901 if (sort__need_collapse)
902 root = &hists->entries_collapsed;
903 else
904 root = hists->entries_in;
905
906 p = &root->rb_node;
907
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300908 while (*p != NULL) {
909 parent = *p;
Namhyung Kimce74f602012-12-10 17:29:55 +0900910 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300911
Namhyung Kimce74f602012-12-10 17:29:55 +0900912 cmp = hist_entry__collapse(he, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300913
914 if (!cmp)
915 goto out;
916
917 if (cmp < 0)
918 p = &(*p)->rb_left;
919 else
920 p = &(*p)->rb_right;
921 }
922
923 he = hist_entry__new(pair);
924 if (he) {
Arnaldo Carvalho de Melo30193d72012-11-12 13:20:03 -0300925 memset(&he->stat, 0, sizeof(he->stat));
926 he->hists = hists;
Namhyung Kimce74f602012-12-10 17:29:55 +0900927 rb_link_node(&he->rb_node_in, parent, p);
928 rb_insert_color(&he->rb_node_in, root);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300929 hists__inc_nr_entries(hists, he);
Jiri Olsae0af43d2012-12-01 21:18:20 +0100930 he->dummy = true;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300931 }
932out:
933 return he;
934}
935
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300936static struct hist_entry *hists__find_entry(struct hists *hists,
937 struct hist_entry *he)
938{
Namhyung Kimce74f602012-12-10 17:29:55 +0900939 struct rb_node *n;
940
941 if (sort__need_collapse)
942 n = hists->entries_collapsed.rb_node;
943 else
944 n = hists->entries_in->rb_node;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300945
946 while (n) {
Namhyung Kimce74f602012-12-10 17:29:55 +0900947 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
948 int64_t cmp = hist_entry__collapse(iter, he);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300949
950 if (cmp < 0)
951 n = n->rb_left;
952 else if (cmp > 0)
953 n = n->rb_right;
954 else
955 return iter;
956 }
957
958 return NULL;
959}
960
961/*
962 * Look for pairs to link to the leader buckets (hist_entries):
963 */
964void hists__match(struct hists *leader, struct hists *other)
965{
Namhyung Kimce74f602012-12-10 17:29:55 +0900966 struct rb_root *root;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300967 struct rb_node *nd;
968 struct hist_entry *pos, *pair;
969
Namhyung Kimce74f602012-12-10 17:29:55 +0900970 if (sort__need_collapse)
971 root = &leader->entries_collapsed;
972 else
973 root = leader->entries_in;
974
975 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
976 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300977 pair = hists__find_entry(other, pos);
978
979 if (pair)
Namhyung Kim5fa90412012-11-29 15:38:34 +0900980 hist_entry__add_pair(pair, pos);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300981 }
982}
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300983
984/*
985 * Look for entries in the other hists that are not present in the leader, if
986 * we find them, just add a dummy entry on the leader hists, with period=0,
987 * nr_events=0, to serve as the list header.
988 */
989int hists__link(struct hists *leader, struct hists *other)
990{
Namhyung Kimce74f602012-12-10 17:29:55 +0900991 struct rb_root *root;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300992 struct rb_node *nd;
993 struct hist_entry *pos, *pair;
994
Namhyung Kimce74f602012-12-10 17:29:55 +0900995 if (sort__need_collapse)
996 root = &other->entries_collapsed;
997 else
998 root = other->entries_in;
999
1000 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
1001 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001002
1003 if (!hist_entry__has_pairs(pos)) {
1004 pair = hists__add_dummy_entry(leader, pos);
1005 if (pair == NULL)
1006 return -1;
Namhyung Kim5fa90412012-11-29 15:38:34 +09001007 hist_entry__add_pair(pos, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001008 }
1009 }
1010
1011 return 0;
1012}