blob: f855941bebea0afa369137d72306c86e5904a7bd [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,
27 .order = ORDER_CALLEE
John Kacur3d1d07e2009-09-28 15:32:55 +020028};
29
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030030u16 hists__col_len(struct hists *hists, enum hist_column col)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030031{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030032 return hists->col_len[col];
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030033}
34
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030035void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030036{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030037 hists->col_len[col] = len;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030038}
39
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030040bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030041{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030042 if (len > hists__col_len(hists, col)) {
43 hists__set_col_len(hists, col, len);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030044 return true;
45 }
46 return false;
47}
48
Namhyung Kim7ccf4f92012-08-20 13:52:05 +090049void hists__reset_col_len(struct hists *hists)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030050{
51 enum hist_column col;
52
53 for (col = 0; col < HISTC_NR_COLS; ++col)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030054 hists__set_col_len(hists, col, 0);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030055}
56
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010057static void hists__set_unres_dso_col_len(struct hists *hists, int dso)
58{
59 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
60
61 if (hists__col_len(hists, dso) < unresolved_col_width &&
62 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
63 !symbol_conf.dso_list)
64 hists__set_col_len(hists, dso, unresolved_col_width);
65}
66
Namhyung Kim7ccf4f92012-08-20 13:52:05 +090067void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030068{
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010069 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030070 u16 len;
71
72 if (h->ms.sym)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010073 hists__new_col_len(hists, HISTC_SYMBOL, h->ms.sym->namelen + 4);
74 else
75 hists__set_unres_dso_col_len(hists, HISTC_DSO);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030076
77 len = thread__comm_len(h->thread);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030078 if (hists__new_col_len(hists, HISTC_COMM, len))
79 hists__set_col_len(hists, HISTC_THREAD, len + 6);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030080
81 if (h->ms.map) {
82 len = dso__name_len(h->ms.map->dso);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030083 hists__new_col_len(hists, HISTC_DSO, len);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030084 }
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010085
Namhyung Kimcb993742012-12-27 18:11:42 +090086 if (h->parent)
87 hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen);
88
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010089 if (h->branch_info) {
90 int symlen;
91 /*
92 * +4 accounts for '[x] ' priv level info
93 * +2 account of 0x prefix on raw addresses
94 */
95 if (h->branch_info->from.sym) {
96 symlen = (int)h->branch_info->from.sym->namelen + 4;
97 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
98
99 symlen = dso__name_len(h->branch_info->from.map->dso);
100 hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
101 } else {
102 symlen = unresolved_col_width + 4 + 2;
103 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
104 hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
105 }
106
107 if (h->branch_info->to.sym) {
108 symlen = (int)h->branch_info->to.sym->namelen + 4;
109 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
110
111 symlen = dso__name_len(h->branch_info->to.map->dso);
112 hists__new_col_len(hists, HISTC_DSO_TO, symlen);
113 } else {
114 symlen = unresolved_col_width + 4 + 2;
115 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
116 hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
117 }
118 }
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300119}
120
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900121void hists__output_recalc_col_len(struct hists *hists, int max_rows)
122{
123 struct rb_node *next = rb_first(&hists->entries);
124 struct hist_entry *n;
125 int row = 0;
126
127 hists__reset_col_len(hists);
128
129 while (next && row++ < max_rows) {
130 n = rb_entry(next, struct hist_entry, rb_node);
131 if (!n->filtered)
132 hists__calc_col_len(hists, n);
133 next = rb_next(&n->rb_node);
134 }
135}
136
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200137static void hist_entry__add_cpumode_period(struct hist_entry *he,
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300138 unsigned int cpumode, u64 period)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800139{
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300140 switch (cpumode) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800141 case PERF_RECORD_MISC_KERNEL:
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900142 he->stat.period_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800143 break;
144 case PERF_RECORD_MISC_USER:
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900145 he->stat.period_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800146 break;
147 case PERF_RECORD_MISC_GUEST_KERNEL:
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900148 he->stat.period_guest_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800149 break;
150 case PERF_RECORD_MISC_GUEST_USER:
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900151 he->stat.period_guest_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800152 break;
153 default:
154 break;
155 }
156}
157
Namhyung Kim139c0812012-10-04 21:49:43 +0900158static void he_stat__add_period(struct he_stat *he_stat, u64 period)
159{
160 he_stat->period += period;
161 he_stat->nr_events += 1;
162}
163
164static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
165{
166 dest->period += src->period;
167 dest->period_sys += src->period_sys;
168 dest->period_us += src->period_us;
169 dest->period_guest_sys += src->period_guest_sys;
170 dest->period_guest_us += src->period_guest_us;
171 dest->nr_events += src->nr_events;
172}
173
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300174static void hist_entry__decay(struct hist_entry *he)
175{
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900176 he->stat.period = (he->stat.period * 7) / 8;
177 he->stat.nr_events = (he->stat.nr_events * 7) / 8;
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300178}
179
180static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
181{
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900182 u64 prev_period = he->stat.period;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200183
184 if (prev_period == 0)
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300185 return true;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200186
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300187 hist_entry__decay(he);
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200188
189 if (!he->filtered)
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900190 hists->stats.total_period -= prev_period - he->stat.period;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200191
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900192 return he->stat.period == 0;
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300193}
194
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200195static void __hists__decay_entries(struct hists *hists, bool zap_user,
196 bool zap_kernel, bool threaded)
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300197{
198 struct rb_node *next = rb_first(&hists->entries);
199 struct hist_entry *n;
200
201 while (next) {
202 n = rb_entry(next, struct hist_entry, rb_node);
203 next = rb_next(&n->rb_node);
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300204 /*
205 * We may be annotating this, for instance, so keep it here in
206 * case some it gets new samples, we'll eventually free it when
207 * the user stops browsing and it agains gets fully decayed.
208 */
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200209 if (((zap_user && n->level == '.') ||
210 (zap_kernel && n->level != '.') ||
211 hists__decay_entry(hists, n)) &&
212 !n->used) {
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300213 rb_erase(&n->rb_node, &hists->entries);
214
Arnaldo Carvalho de Meloe345fa12011-10-13 09:06:54 -0300215 if (sort__need_collapse || threaded)
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300216 rb_erase(&n->rb_node_in, &hists->entries_collapsed);
217
218 hist_entry__free(n);
219 --hists->nr_entries;
220 }
221 }
222}
223
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200224void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
Arnaldo Carvalho de Meloe345fa12011-10-13 09:06:54 -0300225{
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200226 return __hists__decay_entries(hists, zap_user, zap_kernel, false);
Arnaldo Carvalho de Meloe345fa12011-10-13 09:06:54 -0300227}
228
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200229void hists__decay_entries_threaded(struct hists *hists,
230 bool zap_user, bool zap_kernel)
Arnaldo Carvalho de Meloe345fa12011-10-13 09:06:54 -0300231{
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200232 return __hists__decay_entries(hists, zap_user, zap_kernel, true);
Arnaldo Carvalho de Meloe345fa12011-10-13 09:06:54 -0300233}
234
John Kacur3d1d07e2009-09-28 15:32:55 +0200235/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300236 * histogram, sorted on item, collects periods
John Kacur3d1d07e2009-09-28 15:32:55 +0200237 */
238
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300239static struct hist_entry *hist_entry__new(struct hist_entry *template)
240{
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +0200241 size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0;
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200242 struct hist_entry *he = malloc(sizeof(*he) + callchain_size);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300243
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200244 if (he != NULL) {
245 *he = *template;
Namhyung Kimc4b35352012-10-04 21:49:42 +0900246
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200247 if (he->ms.map)
248 he->ms.map->referenced = true;
Stephane Eranian3cf0cb12013-01-14 15:02:45 +0100249
250 if (he->branch_info) {
251 if (he->branch_info->from.map)
252 he->branch_info->from.map->referenced = true;
253 if (he->branch_info->to.map)
254 he->branch_info->to.map->referenced = true;
255 }
256
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300257 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200258 callchain_init(he->callchain);
Arnaldo Carvalho de Melob821c732012-10-25 14:42:45 -0200259
260 INIT_LIST_HEAD(&he->pairs.node);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300261 }
262
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200263 return he;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300264}
265
Namhyung Kim66f97ed2012-12-10 17:29:56 +0900266void hists__inc_nr_entries(struct hists *hists, struct hist_entry *h)
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300267{
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300268 if (!h->filtered) {
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300269 hists__calc_col_len(hists, h);
270 ++hists->nr_entries;
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900271 hists->stats.total_period += h->stat.period;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300272 }
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300273}
274
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300275static u8 symbol__parent_filter(const struct symbol *parent)
276{
277 if (symbol_conf.exclude_other && parent == NULL)
278 return 1 << HIST_FILTER__PARENT;
279 return 0;
280}
281
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100282static struct hist_entry *add_hist_entry(struct hists *hists,
283 struct hist_entry *entry,
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300284 struct addr_location *al,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100285 u64 period)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300286{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300287 struct rb_node **p;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300288 struct rb_node *parent = NULL;
289 struct hist_entry *he;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300290 int cmp;
291
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300292 pthread_mutex_lock(&hists->lock);
293
294 p = &hists->entries_in->rb_node;
295
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300296 while (*p != NULL) {
297 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300298 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300299
Namhyung Kim9afcf932012-12-10 17:29:54 +0900300 /*
301 * Make sure that it receives arguments in a same order as
302 * hist_entry__collapse() so that we can use an appropriate
303 * function when searching an entry regardless which sort
304 * keys were used.
305 */
306 cmp = hist_entry__cmp(he, entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300307
308 if (!cmp) {
Namhyung Kim139c0812012-10-04 21:49:43 +0900309 he_stat__add_period(&he->stat, period);
David Miller63fa4712012-03-27 03:14:18 -0400310
311 /* If the map of an existing hist_entry has
312 * become out-of-date due to an exec() or
313 * similar, update it. Otherwise we will
314 * mis-adjust symbol addresses when computing
315 * the history counter to increment.
316 */
317 if (he->ms.map != entry->ms.map) {
318 he->ms.map = entry->ms.map;
319 if (he->ms.map)
320 he->ms.map->referenced = true;
321 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300322 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300323 }
324
325 if (cmp < 0)
326 p = &(*p)->rb_left;
327 else
328 p = &(*p)->rb_right;
329 }
330
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100331 he = hist_entry__new(entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300332 if (!he)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300333 goto out_unlock;
334
335 rb_link_node(&he->rb_node_in, parent, p);
336 rb_insert_color(&he->rb_node_in, hists->entries_in);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300337out:
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300338 hist_entry__add_cpumode_period(he, al->cpumode, period);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300339out_unlock:
340 pthread_mutex_unlock(&hists->lock);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300341 return he;
342}
343
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100344struct hist_entry *__hists__add_branch_entry(struct hists *self,
345 struct addr_location *al,
346 struct symbol *sym_parent,
347 struct branch_info *bi,
348 u64 period)
349{
350 struct hist_entry entry = {
351 .thread = al->thread,
352 .ms = {
353 .map = bi->to.map,
354 .sym = bi->to.sym,
355 },
356 .cpu = al->cpu,
357 .ip = bi->to.addr,
358 .level = al->level,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900359 .stat = {
360 .period = period,
Namhyung Kimc4b35352012-10-04 21:49:42 +0900361 .nr_events = 1,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900362 },
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100363 .parent = sym_parent,
364 .filtered = symbol__parent_filter(sym_parent),
365 .branch_info = bi,
Jiri Olsaae359f12012-10-04 21:49:35 +0900366 .hists = self,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100367 };
368
369 return add_hist_entry(self, &entry, al, period);
370}
371
372struct hist_entry *__hists__add_entry(struct hists *self,
373 struct addr_location *al,
374 struct symbol *sym_parent, u64 period)
375{
376 struct hist_entry entry = {
377 .thread = al->thread,
378 .ms = {
379 .map = al->map,
380 .sym = al->sym,
381 },
382 .cpu = al->cpu,
383 .ip = al->addr,
384 .level = al->level,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900385 .stat = {
386 .period = period,
Namhyung Kimc4b35352012-10-04 21:49:42 +0900387 .nr_events = 1,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900388 },
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100389 .parent = sym_parent,
390 .filtered = symbol__parent_filter(sym_parent),
Jiri Olsaae359f12012-10-04 21:49:35 +0900391 .hists = self,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100392 };
393
394 return add_hist_entry(self, &entry, al, period);
395}
396
John Kacur3d1d07e2009-09-28 15:32:55 +0200397int64_t
398hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
399{
400 struct sort_entry *se;
401 int64_t cmp = 0;
402
403 list_for_each_entry(se, &hist_entry__sort_list, list) {
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200404 cmp = se->se_cmp(left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200405 if (cmp)
406 break;
407 }
408
409 return cmp;
410}
411
412int64_t
413hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
414{
415 struct sort_entry *se;
416 int64_t cmp = 0;
417
418 list_for_each_entry(se, &hist_entry__sort_list, list) {
419 int64_t (*f)(struct hist_entry *, struct hist_entry *);
420
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200421 f = se->se_collapse ?: se->se_cmp;
John Kacur3d1d07e2009-09-28 15:32:55 +0200422
423 cmp = f(left, right);
424 if (cmp)
425 break;
426 }
427
428 return cmp;
429}
430
431void hist_entry__free(struct hist_entry *he)
432{
Namhyung Kim580e3382012-11-07 16:27:14 +0900433 free(he->branch_info);
John Kacur3d1d07e2009-09-28 15:32:55 +0200434 free(he);
435}
436
437/*
438 * collapse the histogram
439 */
440
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300441static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100442 struct rb_root *root,
443 struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +0200444{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200445 struct rb_node **p = &root->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200446 struct rb_node *parent = NULL;
447 struct hist_entry *iter;
448 int64_t cmp;
449
450 while (*p != NULL) {
451 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300452 iter = rb_entry(parent, struct hist_entry, rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +0200453
454 cmp = hist_entry__collapse(iter, he);
455
456 if (!cmp) {
Namhyung Kim139c0812012-10-04 21:49:43 +0900457 he_stat__add_stat(&iter->stat, &he->stat);
Namhyung Kim9ec60972012-09-26 16:47:28 +0900458
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100459 if (symbol_conf.use_callchain) {
Namhyung Kim47260642012-05-31 14:43:26 +0900460 callchain_cursor_reset(&callchain_cursor);
461 callchain_merge(&callchain_cursor,
462 iter->callchain,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100463 he->callchain);
464 }
John Kacur3d1d07e2009-09-28 15:32:55 +0200465 hist_entry__free(he);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300466 return false;
John Kacur3d1d07e2009-09-28 15:32:55 +0200467 }
468
469 if (cmp < 0)
470 p = &(*p)->rb_left;
471 else
472 p = &(*p)->rb_right;
473 }
474
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300475 rb_link_node(&he->rb_node_in, parent, p);
476 rb_insert_color(&he->rb_node_in, root);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300477 return true;
John Kacur3d1d07e2009-09-28 15:32:55 +0200478}
479
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300480static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
481{
482 struct rb_root *root;
483
484 pthread_mutex_lock(&hists->lock);
485
486 root = hists->entries_in;
487 if (++hists->entries_in > &hists->entries_in_array[1])
488 hists->entries_in = &hists->entries_in_array[0];
489
490 pthread_mutex_unlock(&hists->lock);
491
492 return root;
493}
494
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200495static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
496{
497 hists__filter_entry_by_dso(hists, he);
498 hists__filter_entry_by_thread(hists, he);
Namhyung Kime94d53e2012-03-16 17:50:51 +0900499 hists__filter_entry_by_symbol(hists, he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200500}
501
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300502static void __hists__collapse_resort(struct hists *hists, bool threaded)
503{
504 struct rb_root *root;
505 struct rb_node *next;
506 struct hist_entry *n;
507
508 if (!sort__need_collapse && !threaded)
509 return;
510
511 root = hists__get_rotate_entries_in(hists);
512 next = rb_first(root);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300513
514 while (next) {
515 n = rb_entry(next, struct hist_entry, rb_node_in);
516 next = rb_next(&n->rb_node_in);
517
518 rb_erase(&n->rb_node_in, root);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200519 if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
520 /*
521 * If it wasn't combined with one of the entries already
522 * collapsed, we need to apply the filters that may have
523 * been set by, say, the hist_browser.
524 */
525 hists__apply_filters(hists, n);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200526 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300527 }
528}
529
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300530void hists__collapse_resort(struct hists *hists)
John Kacur3d1d07e2009-09-28 15:32:55 +0200531{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300532 return __hists__collapse_resort(hists, false);
533}
John Kacur3d1d07e2009-09-28 15:32:55 +0200534
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300535void hists__collapse_resort_threaded(struct hists *hists)
536{
537 return __hists__collapse_resort(hists, true);
John Kacur3d1d07e2009-09-28 15:32:55 +0200538}
539
540/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300541 * reverse the map, sort on period.
John Kacur3d1d07e2009-09-28 15:32:55 +0200542 */
543
Namhyung Kim29d720e2013-01-22 18:09:33 +0900544static int period_cmp(u64 period_a, u64 period_b)
545{
546 if (period_a > period_b)
547 return 1;
548 if (period_a < period_b)
549 return -1;
550 return 0;
551}
552
553static int hist_entry__sort_on_period(struct hist_entry *a,
554 struct hist_entry *b)
555{
556 int ret;
557 int i, nr_members;
558 struct perf_evsel *evsel;
559 struct hist_entry *pair;
560 u64 *periods_a, *periods_b;
561
562 ret = period_cmp(a->stat.period, b->stat.period);
563 if (ret || !symbol_conf.event_group)
564 return ret;
565
566 evsel = hists_to_evsel(a->hists);
567 nr_members = evsel->nr_members;
568 if (nr_members <= 1)
569 return ret;
570
571 periods_a = zalloc(sizeof(periods_a) * nr_members);
572 periods_b = zalloc(sizeof(periods_b) * nr_members);
573
574 if (!periods_a || !periods_b)
575 goto out;
576
577 list_for_each_entry(pair, &a->pairs.head, pairs.node) {
578 evsel = hists_to_evsel(pair->hists);
579 periods_a[perf_evsel__group_idx(evsel)] = pair->stat.period;
580 }
581
582 list_for_each_entry(pair, &b->pairs.head, pairs.node) {
583 evsel = hists_to_evsel(pair->hists);
584 periods_b[perf_evsel__group_idx(evsel)] = pair->stat.period;
585 }
586
587 for (i = 1; i < nr_members; i++) {
588 ret = period_cmp(periods_a[i], periods_b[i]);
589 if (ret)
590 break;
591 }
592
593out:
594 free(periods_a);
595 free(periods_b);
596
597 return ret;
598}
599
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300600static void __hists__insert_output_entry(struct rb_root *entries,
601 struct hist_entry *he,
602 u64 min_callchain_hits)
John Kacur3d1d07e2009-09-28 15:32:55 +0200603{
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300604 struct rb_node **p = &entries->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200605 struct rb_node *parent = NULL;
606 struct hist_entry *iter;
607
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200608 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melob9fb9302010-04-02 09:50:42 -0300609 callchain_param.sort(&he->sorted_chain, he->callchain,
John Kacur3d1d07e2009-09-28 15:32:55 +0200610 min_callchain_hits, &callchain_param);
611
612 while (*p != NULL) {
613 parent = *p;
614 iter = rb_entry(parent, struct hist_entry, rb_node);
615
Namhyung Kim29d720e2013-01-22 18:09:33 +0900616 if (hist_entry__sort_on_period(he, iter) > 0)
John Kacur3d1d07e2009-09-28 15:32:55 +0200617 p = &(*p)->rb_left;
618 else
619 p = &(*p)->rb_right;
620 }
621
622 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300623 rb_insert_color(&he->rb_node, entries);
John Kacur3d1d07e2009-09-28 15:32:55 +0200624}
625
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300626static void __hists__output_resort(struct hists *hists, bool threaded)
John Kacur3d1d07e2009-09-28 15:32:55 +0200627{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300628 struct rb_root *root;
John Kacur3d1d07e2009-09-28 15:32:55 +0200629 struct rb_node *next;
630 struct hist_entry *n;
John Kacur3d1d07e2009-09-28 15:32:55 +0200631 u64 min_callchain_hits;
632
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300633 min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
John Kacur3d1d07e2009-09-28 15:32:55 +0200634
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300635 if (sort__need_collapse || threaded)
636 root = &hists->entries_collapsed;
637 else
638 root = hists->entries_in;
639
640 next = rb_first(root);
641 hists->entries = RB_ROOT;
John Kacur3d1d07e2009-09-28 15:32:55 +0200642
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300643 hists->nr_entries = 0;
Arnaldo Carvalho de Melo79286312011-10-27 09:19:48 -0200644 hists->stats.total_period = 0;
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300645 hists__reset_col_len(hists);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300646
John Kacur3d1d07e2009-09-28 15:32:55 +0200647 while (next) {
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300648 n = rb_entry(next, struct hist_entry, rb_node_in);
649 next = rb_next(&n->rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +0200650
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300651 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300652 hists__inc_nr_entries(hists, n);
John Kacur3d1d07e2009-09-28 15:32:55 +0200653 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300654}
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200655
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300656void hists__output_resort(struct hists *hists)
657{
658 return __hists__output_resort(hists, false);
659}
660
661void hists__output_resort_threaded(struct hists *hists)
662{
663 return __hists__output_resort(hists, true);
John Kacur3d1d07e2009-09-28 15:32:55 +0200664}
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200665
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300666static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300667 enum hist_filter filter)
668{
669 h->filtered &= ~(1 << filter);
670 if (h->filtered)
671 return;
672
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300673 ++hists->nr_entries;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -0300674 if (h->ms.unfolded)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300675 hists->nr_entries += h->nr_rows;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -0300676 h->row_offset = 0;
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900677 hists->stats.total_period += h->stat.period;
678 hists->stats.nr_events[PERF_RECORD_SAMPLE] += h->stat.nr_events;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300679
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300680 hists__calc_col_len(hists, h);
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300681}
682
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200683
684static bool hists__filter_entry_by_dso(struct hists *hists,
685 struct hist_entry *he)
686{
687 if (hists->dso_filter != NULL &&
688 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
689 he->filtered |= (1 << HIST_FILTER__DSO);
690 return true;
691 }
692
693 return false;
694}
695
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200696void hists__filter_by_dso(struct hists *hists)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300697{
698 struct rb_node *nd;
699
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300700 hists->nr_entries = hists->stats.total_period = 0;
701 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
702 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300703
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300704 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300705 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
706
707 if (symbol_conf.exclude_other && !h->parent)
708 continue;
709
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200710 if (hists__filter_entry_by_dso(hists, h))
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300711 continue;
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300712
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300713 hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300714 }
715}
716
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200717static bool hists__filter_entry_by_thread(struct hists *hists,
718 struct hist_entry *he)
719{
720 if (hists->thread_filter != NULL &&
721 he->thread != hists->thread_filter) {
722 he->filtered |= (1 << HIST_FILTER__THREAD);
723 return true;
724 }
725
726 return false;
727}
728
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200729void hists__filter_by_thread(struct hists *hists)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300730{
731 struct rb_node *nd;
732
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300733 hists->nr_entries = hists->stats.total_period = 0;
734 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
735 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300736
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300737 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300738 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
739
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200740 if (hists__filter_entry_by_thread(hists, h))
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300741 continue;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300742
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300743 hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300744 }
745}
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300746
Namhyung Kime94d53e2012-03-16 17:50:51 +0900747static bool hists__filter_entry_by_symbol(struct hists *hists,
748 struct hist_entry *he)
749{
750 if (hists->symbol_filter_str != NULL &&
751 (!he->ms.sym || strstr(he->ms.sym->name,
752 hists->symbol_filter_str) == NULL)) {
753 he->filtered |= (1 << HIST_FILTER__SYMBOL);
754 return true;
755 }
756
757 return false;
758}
759
760void hists__filter_by_symbol(struct hists *hists)
761{
762 struct rb_node *nd;
763
764 hists->nr_entries = hists->stats.total_period = 0;
765 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
766 hists__reset_col_len(hists);
767
768 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
769 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
770
771 if (hists__filter_entry_by_symbol(hists, h))
772 continue;
773
774 hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
775 }
776}
777
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200778int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300779{
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200780 return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300781}
782
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200783int hist_entry__annotate(struct hist_entry *he, size_t privsize)
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300784{
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200785 return symbol__annotate(he->ms.sym, he->ms.map, privsize);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300786}
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -0300787
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -0300788void events_stats__inc(struct events_stats *stats, u32 type)
789{
790 ++stats->nr_events[0];
791 ++stats->nr_events[type];
792}
793
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300794void hists__inc_nr_events(struct hists *hists, u32 type)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -0300795{
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -0300796 events_stats__inc(&hists->stats, type);
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -0300797}
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300798
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300799static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
800 struct hist_entry *pair)
801{
Namhyung Kimce74f602012-12-10 17:29:55 +0900802 struct rb_root *root;
803 struct rb_node **p;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300804 struct rb_node *parent = NULL;
805 struct hist_entry *he;
806 int cmp;
807
Namhyung Kimce74f602012-12-10 17:29:55 +0900808 if (sort__need_collapse)
809 root = &hists->entries_collapsed;
810 else
811 root = hists->entries_in;
812
813 p = &root->rb_node;
814
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300815 while (*p != NULL) {
816 parent = *p;
Namhyung Kimce74f602012-12-10 17:29:55 +0900817 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300818
Namhyung Kimce74f602012-12-10 17:29:55 +0900819 cmp = hist_entry__collapse(he, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300820
821 if (!cmp)
822 goto out;
823
824 if (cmp < 0)
825 p = &(*p)->rb_left;
826 else
827 p = &(*p)->rb_right;
828 }
829
830 he = hist_entry__new(pair);
831 if (he) {
Arnaldo Carvalho de Melo30193d72012-11-12 13:20:03 -0300832 memset(&he->stat, 0, sizeof(he->stat));
833 he->hists = hists;
Namhyung Kimce74f602012-12-10 17:29:55 +0900834 rb_link_node(&he->rb_node_in, parent, p);
835 rb_insert_color(&he->rb_node_in, root);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300836 hists__inc_nr_entries(hists, he);
837 }
838out:
839 return he;
840}
841
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300842static struct hist_entry *hists__find_entry(struct hists *hists,
843 struct hist_entry *he)
844{
Namhyung Kimce74f602012-12-10 17:29:55 +0900845 struct rb_node *n;
846
847 if (sort__need_collapse)
848 n = hists->entries_collapsed.rb_node;
849 else
850 n = hists->entries_in->rb_node;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300851
852 while (n) {
Namhyung Kimce74f602012-12-10 17:29:55 +0900853 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
854 int64_t cmp = hist_entry__collapse(iter, he);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300855
856 if (cmp < 0)
857 n = n->rb_left;
858 else if (cmp > 0)
859 n = n->rb_right;
860 else
861 return iter;
862 }
863
864 return NULL;
865}
866
867/*
868 * Look for pairs to link to the leader buckets (hist_entries):
869 */
870void hists__match(struct hists *leader, struct hists *other)
871{
Namhyung Kimce74f602012-12-10 17:29:55 +0900872 struct rb_root *root;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300873 struct rb_node *nd;
874 struct hist_entry *pos, *pair;
875
Namhyung Kimce74f602012-12-10 17:29:55 +0900876 if (sort__need_collapse)
877 root = &leader->entries_collapsed;
878 else
879 root = leader->entries_in;
880
881 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
882 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300883 pair = hists__find_entry(other, pos);
884
885 if (pair)
Namhyung Kim5fa90412012-11-29 15:38:34 +0900886 hist_entry__add_pair(pair, pos);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300887 }
888}
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300889
890/*
891 * Look for entries in the other hists that are not present in the leader, if
892 * we find them, just add a dummy entry on the leader hists, with period=0,
893 * nr_events=0, to serve as the list header.
894 */
895int hists__link(struct hists *leader, struct hists *other)
896{
Namhyung Kimce74f602012-12-10 17:29:55 +0900897 struct rb_root *root;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300898 struct rb_node *nd;
899 struct hist_entry *pos, *pair;
900
Namhyung Kimce74f602012-12-10 17:29:55 +0900901 if (sort__need_collapse)
902 root = &other->entries_collapsed;
903 else
904 root = other->entries_in;
905
906 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
907 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300908
909 if (!hist_entry__has_pairs(pos)) {
910 pair = hists__add_dummy_entry(leader, pos);
911 if (pair == NULL)
912 return -1;
Namhyung Kim5fa90412012-11-29 15:38:34 +0900913 hist_entry__add_pair(pos, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300914 }
915 }
916
917 return 0;
918}