blob: 97ddd18acd7c196b7df69cf0037529eacf01258a [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
Andi Kleen05484292013-01-24 16:10:29 +0100158static void he_stat__add_period(struct he_stat *he_stat, u64 period,
159 u64 weight)
Namhyung Kim139c0812012-10-04 21:49:43 +0900160{
161 he_stat->period += period;
Andi Kleen05484292013-01-24 16:10:29 +0100162 he_stat->weight += weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900163 he_stat->nr_events += 1;
164}
165
166static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
167{
168 dest->period += src->period;
169 dest->period_sys += src->period_sys;
170 dest->period_us += src->period_us;
171 dest->period_guest_sys += src->period_guest_sys;
172 dest->period_guest_us += src->period_guest_us;
173 dest->nr_events += src->nr_events;
Andi Kleen05484292013-01-24 16:10:29 +0100174 dest->weight += src->weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900175}
176
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300177static void hist_entry__decay(struct hist_entry *he)
178{
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900179 he->stat.period = (he->stat.period * 7) / 8;
180 he->stat.nr_events = (he->stat.nr_events * 7) / 8;
Andi Kleen05484292013-01-24 16:10:29 +0100181 /* XXX need decay for weight too? */
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300182}
183
184static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
185{
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900186 u64 prev_period = he->stat.period;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200187
188 if (prev_period == 0)
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300189 return true;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200190
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300191 hist_entry__decay(he);
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200192
193 if (!he->filtered)
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900194 hists->stats.total_period -= prev_period - he->stat.period;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200195
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900196 return he->stat.period == 0;
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300197}
198
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200199static void __hists__decay_entries(struct hists *hists, bool zap_user,
200 bool zap_kernel, bool threaded)
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300201{
202 struct rb_node *next = rb_first(&hists->entries);
203 struct hist_entry *n;
204
205 while (next) {
206 n = rb_entry(next, struct hist_entry, rb_node);
207 next = rb_next(&n->rb_node);
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300208 /*
209 * We may be annotating this, for instance, so keep it here in
210 * case some it gets new samples, we'll eventually free it when
211 * the user stops browsing and it agains gets fully decayed.
212 */
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200213 if (((zap_user && n->level == '.') ||
214 (zap_kernel && n->level != '.') ||
215 hists__decay_entry(hists, n)) &&
216 !n->used) {
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300217 rb_erase(&n->rb_node, &hists->entries);
218
Arnaldo Carvalho de Meloe345fa12011-10-13 09:06:54 -0300219 if (sort__need_collapse || threaded)
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300220 rb_erase(&n->rb_node_in, &hists->entries_collapsed);
221
222 hist_entry__free(n);
223 --hists->nr_entries;
224 }
225 }
226}
227
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200228void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
Arnaldo Carvalho de Meloe345fa12011-10-13 09:06:54 -0300229{
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200230 return __hists__decay_entries(hists, zap_user, zap_kernel, false);
Arnaldo Carvalho de Meloe345fa12011-10-13 09:06:54 -0300231}
232
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200233void hists__decay_entries_threaded(struct hists *hists,
234 bool zap_user, bool zap_kernel)
Arnaldo Carvalho de Meloe345fa12011-10-13 09:06:54 -0300235{
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200236 return __hists__decay_entries(hists, zap_user, zap_kernel, true);
Arnaldo Carvalho de Meloe345fa12011-10-13 09:06:54 -0300237}
238
John Kacur3d1d07e2009-09-28 15:32:55 +0200239/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300240 * histogram, sorted on item, collects periods
John Kacur3d1d07e2009-09-28 15:32:55 +0200241 */
242
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300243static struct hist_entry *hist_entry__new(struct hist_entry *template)
244{
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +0200245 size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0;
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200246 struct hist_entry *he = malloc(sizeof(*he) + callchain_size);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300247
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200248 if (he != NULL) {
249 *he = *template;
Namhyung Kimc4b35352012-10-04 21:49:42 +0900250
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200251 if (he->ms.map)
252 he->ms.map->referenced = true;
Stephane Eranian3cf0cb12013-01-14 15:02:45 +0100253
254 if (he->branch_info) {
255 if (he->branch_info->from.map)
256 he->branch_info->from.map->referenced = true;
257 if (he->branch_info->to.map)
258 he->branch_info->to.map->referenced = true;
259 }
260
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300261 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200262 callchain_init(he->callchain);
Arnaldo Carvalho de Melob821c732012-10-25 14:42:45 -0200263
264 INIT_LIST_HEAD(&he->pairs.node);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300265 }
266
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200267 return he;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300268}
269
Namhyung Kim66f97ed2012-12-10 17:29:56 +0900270void hists__inc_nr_entries(struct hists *hists, struct hist_entry *h)
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300271{
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300272 if (!h->filtered) {
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300273 hists__calc_col_len(hists, h);
274 ++hists->nr_entries;
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900275 hists->stats.total_period += h->stat.period;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300276 }
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300277}
278
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300279static u8 symbol__parent_filter(const struct symbol *parent)
280{
281 if (symbol_conf.exclude_other && parent == NULL)
282 return 1 << HIST_FILTER__PARENT;
283 return 0;
284}
285
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100286static struct hist_entry *add_hist_entry(struct hists *hists,
287 struct hist_entry *entry,
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300288 struct addr_location *al,
Andi Kleen05484292013-01-24 16:10:29 +0100289 u64 period,
290 u64 weight)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300291{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300292 struct rb_node **p;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300293 struct rb_node *parent = NULL;
294 struct hist_entry *he;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300295 int cmp;
296
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300297 pthread_mutex_lock(&hists->lock);
298
299 p = &hists->entries_in->rb_node;
300
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300301 while (*p != NULL) {
302 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300303 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300304
Namhyung Kim9afcf932012-12-10 17:29:54 +0900305 /*
306 * Make sure that it receives arguments in a same order as
307 * hist_entry__collapse() so that we can use an appropriate
308 * function when searching an entry regardless which sort
309 * keys were used.
310 */
311 cmp = hist_entry__cmp(he, entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300312
313 if (!cmp) {
Andi Kleen05484292013-01-24 16:10:29 +0100314 he_stat__add_period(&he->stat, period, weight);
David Miller63fa4712012-03-27 03:14:18 -0400315
316 /* If the map of an existing hist_entry has
317 * become out-of-date due to an exec() or
318 * similar, update it. Otherwise we will
319 * mis-adjust symbol addresses when computing
320 * the history counter to increment.
321 */
322 if (he->ms.map != entry->ms.map) {
323 he->ms.map = entry->ms.map;
324 if (he->ms.map)
325 he->ms.map->referenced = true;
326 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300327 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300328 }
329
330 if (cmp < 0)
331 p = &(*p)->rb_left;
332 else
333 p = &(*p)->rb_right;
334 }
335
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100336 he = hist_entry__new(entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300337 if (!he)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300338 goto out_unlock;
339
340 rb_link_node(&he->rb_node_in, parent, p);
341 rb_insert_color(&he->rb_node_in, hists->entries_in);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300342out:
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300343 hist_entry__add_cpumode_period(he, al->cpumode, period);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300344out_unlock:
345 pthread_mutex_unlock(&hists->lock);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300346 return he;
347}
348
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100349struct hist_entry *__hists__add_branch_entry(struct hists *self,
350 struct addr_location *al,
351 struct symbol *sym_parent,
352 struct branch_info *bi,
Andi Kleen05484292013-01-24 16:10:29 +0100353 u64 period,
354 u64 weight)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100355{
356 struct hist_entry entry = {
357 .thread = al->thread,
358 .ms = {
359 .map = bi->to.map,
360 .sym = bi->to.sym,
361 },
362 .cpu = al->cpu,
363 .ip = bi->to.addr,
364 .level = al->level,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900365 .stat = {
366 .period = period,
Namhyung Kimc4b35352012-10-04 21:49:42 +0900367 .nr_events = 1,
Andi Kleen05484292013-01-24 16:10:29 +0100368 .weight = weight,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900369 },
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100370 .parent = sym_parent,
371 .filtered = symbol__parent_filter(sym_parent),
372 .branch_info = bi,
Jiri Olsaae359f12012-10-04 21:49:35 +0900373 .hists = self,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100374 };
375
Andi Kleen05484292013-01-24 16:10:29 +0100376 return add_hist_entry(self, &entry, al, period, weight);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100377}
378
379struct hist_entry *__hists__add_entry(struct hists *self,
380 struct addr_location *al,
Andi Kleen05484292013-01-24 16:10:29 +0100381 struct symbol *sym_parent, u64 period,
382 u64 weight)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100383{
384 struct hist_entry entry = {
385 .thread = al->thread,
386 .ms = {
387 .map = al->map,
388 .sym = al->sym,
389 },
390 .cpu = al->cpu,
391 .ip = al->addr,
392 .level = al->level,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900393 .stat = {
394 .period = period,
Namhyung Kimc4b35352012-10-04 21:49:42 +0900395 .nr_events = 1,
Andi Kleen05484292013-01-24 16:10:29 +0100396 .weight = weight,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900397 },
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100398 .parent = sym_parent,
399 .filtered = symbol__parent_filter(sym_parent),
Jiri Olsaae359f12012-10-04 21:49:35 +0900400 .hists = self,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100401 };
402
Andi Kleen05484292013-01-24 16:10:29 +0100403 return add_hist_entry(self, &entry, al, period, weight);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100404}
405
John Kacur3d1d07e2009-09-28 15:32:55 +0200406int64_t
407hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
408{
409 struct sort_entry *se;
410 int64_t cmp = 0;
411
412 list_for_each_entry(se, &hist_entry__sort_list, list) {
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200413 cmp = se->se_cmp(left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200414 if (cmp)
415 break;
416 }
417
418 return cmp;
419}
420
421int64_t
422hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
423{
424 struct sort_entry *se;
425 int64_t cmp = 0;
426
427 list_for_each_entry(se, &hist_entry__sort_list, list) {
428 int64_t (*f)(struct hist_entry *, struct hist_entry *);
429
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200430 f = se->se_collapse ?: se->se_cmp;
John Kacur3d1d07e2009-09-28 15:32:55 +0200431
432 cmp = f(left, right);
433 if (cmp)
434 break;
435 }
436
437 return cmp;
438}
439
440void hist_entry__free(struct hist_entry *he)
441{
Namhyung Kim580e3382012-11-07 16:27:14 +0900442 free(he->branch_info);
John Kacur3d1d07e2009-09-28 15:32:55 +0200443 free(he);
444}
445
446/*
447 * collapse the histogram
448 */
449
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300450static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100451 struct rb_root *root,
452 struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +0200453{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200454 struct rb_node **p = &root->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200455 struct rb_node *parent = NULL;
456 struct hist_entry *iter;
457 int64_t cmp;
458
459 while (*p != NULL) {
460 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300461 iter = rb_entry(parent, struct hist_entry, rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +0200462
463 cmp = hist_entry__collapse(iter, he);
464
465 if (!cmp) {
Namhyung Kim139c0812012-10-04 21:49:43 +0900466 he_stat__add_stat(&iter->stat, &he->stat);
Namhyung Kim9ec60972012-09-26 16:47:28 +0900467
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100468 if (symbol_conf.use_callchain) {
Namhyung Kim47260642012-05-31 14:43:26 +0900469 callchain_cursor_reset(&callchain_cursor);
470 callchain_merge(&callchain_cursor,
471 iter->callchain,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100472 he->callchain);
473 }
John Kacur3d1d07e2009-09-28 15:32:55 +0200474 hist_entry__free(he);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300475 return false;
John Kacur3d1d07e2009-09-28 15:32:55 +0200476 }
477
478 if (cmp < 0)
479 p = &(*p)->rb_left;
480 else
481 p = &(*p)->rb_right;
482 }
483
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300484 rb_link_node(&he->rb_node_in, parent, p);
485 rb_insert_color(&he->rb_node_in, root);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300486 return true;
John Kacur3d1d07e2009-09-28 15:32:55 +0200487}
488
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300489static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
490{
491 struct rb_root *root;
492
493 pthread_mutex_lock(&hists->lock);
494
495 root = hists->entries_in;
496 if (++hists->entries_in > &hists->entries_in_array[1])
497 hists->entries_in = &hists->entries_in_array[0];
498
499 pthread_mutex_unlock(&hists->lock);
500
501 return root;
502}
503
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200504static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
505{
506 hists__filter_entry_by_dso(hists, he);
507 hists__filter_entry_by_thread(hists, he);
Namhyung Kime94d53e2012-03-16 17:50:51 +0900508 hists__filter_entry_by_symbol(hists, he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200509}
510
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300511static void __hists__collapse_resort(struct hists *hists, bool threaded)
512{
513 struct rb_root *root;
514 struct rb_node *next;
515 struct hist_entry *n;
516
517 if (!sort__need_collapse && !threaded)
518 return;
519
520 root = hists__get_rotate_entries_in(hists);
521 next = rb_first(root);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300522
523 while (next) {
524 n = rb_entry(next, struct hist_entry, rb_node_in);
525 next = rb_next(&n->rb_node_in);
526
527 rb_erase(&n->rb_node_in, root);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200528 if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
529 /*
530 * If it wasn't combined with one of the entries already
531 * collapsed, we need to apply the filters that may have
532 * been set by, say, the hist_browser.
533 */
534 hists__apply_filters(hists, n);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200535 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300536 }
537}
538
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300539void hists__collapse_resort(struct hists *hists)
John Kacur3d1d07e2009-09-28 15:32:55 +0200540{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300541 return __hists__collapse_resort(hists, false);
542}
John Kacur3d1d07e2009-09-28 15:32:55 +0200543
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300544void hists__collapse_resort_threaded(struct hists *hists)
545{
546 return __hists__collapse_resort(hists, true);
John Kacur3d1d07e2009-09-28 15:32:55 +0200547}
548
549/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300550 * reverse the map, sort on period.
John Kacur3d1d07e2009-09-28 15:32:55 +0200551 */
552
Namhyung Kim29d720e2013-01-22 18:09:33 +0900553static int period_cmp(u64 period_a, u64 period_b)
554{
555 if (period_a > period_b)
556 return 1;
557 if (period_a < period_b)
558 return -1;
559 return 0;
560}
561
562static int hist_entry__sort_on_period(struct hist_entry *a,
563 struct hist_entry *b)
564{
565 int ret;
566 int i, nr_members;
567 struct perf_evsel *evsel;
568 struct hist_entry *pair;
569 u64 *periods_a, *periods_b;
570
571 ret = period_cmp(a->stat.period, b->stat.period);
572 if (ret || !symbol_conf.event_group)
573 return ret;
574
575 evsel = hists_to_evsel(a->hists);
576 nr_members = evsel->nr_members;
577 if (nr_members <= 1)
578 return ret;
579
580 periods_a = zalloc(sizeof(periods_a) * nr_members);
581 periods_b = zalloc(sizeof(periods_b) * nr_members);
582
583 if (!periods_a || !periods_b)
584 goto out;
585
586 list_for_each_entry(pair, &a->pairs.head, pairs.node) {
587 evsel = hists_to_evsel(pair->hists);
588 periods_a[perf_evsel__group_idx(evsel)] = pair->stat.period;
589 }
590
591 list_for_each_entry(pair, &b->pairs.head, pairs.node) {
592 evsel = hists_to_evsel(pair->hists);
593 periods_b[perf_evsel__group_idx(evsel)] = pair->stat.period;
594 }
595
596 for (i = 1; i < nr_members; i++) {
597 ret = period_cmp(periods_a[i], periods_b[i]);
598 if (ret)
599 break;
600 }
601
602out:
603 free(periods_a);
604 free(periods_b);
605
606 return ret;
607}
608
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300609static void __hists__insert_output_entry(struct rb_root *entries,
610 struct hist_entry *he,
611 u64 min_callchain_hits)
John Kacur3d1d07e2009-09-28 15:32:55 +0200612{
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300613 struct rb_node **p = &entries->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200614 struct rb_node *parent = NULL;
615 struct hist_entry *iter;
616
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200617 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melob9fb9302010-04-02 09:50:42 -0300618 callchain_param.sort(&he->sorted_chain, he->callchain,
John Kacur3d1d07e2009-09-28 15:32:55 +0200619 min_callchain_hits, &callchain_param);
620
621 while (*p != NULL) {
622 parent = *p;
623 iter = rb_entry(parent, struct hist_entry, rb_node);
624
Namhyung Kim29d720e2013-01-22 18:09:33 +0900625 if (hist_entry__sort_on_period(he, iter) > 0)
John Kacur3d1d07e2009-09-28 15:32:55 +0200626 p = &(*p)->rb_left;
627 else
628 p = &(*p)->rb_right;
629 }
630
631 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300632 rb_insert_color(&he->rb_node, entries);
John Kacur3d1d07e2009-09-28 15:32:55 +0200633}
634
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300635static void __hists__output_resort(struct hists *hists, bool threaded)
John Kacur3d1d07e2009-09-28 15:32:55 +0200636{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300637 struct rb_root *root;
John Kacur3d1d07e2009-09-28 15:32:55 +0200638 struct rb_node *next;
639 struct hist_entry *n;
John Kacur3d1d07e2009-09-28 15:32:55 +0200640 u64 min_callchain_hits;
641
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300642 min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
John Kacur3d1d07e2009-09-28 15:32:55 +0200643
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300644 if (sort__need_collapse || threaded)
645 root = &hists->entries_collapsed;
646 else
647 root = hists->entries_in;
648
649 next = rb_first(root);
650 hists->entries = RB_ROOT;
John Kacur3d1d07e2009-09-28 15:32:55 +0200651
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300652 hists->nr_entries = 0;
Arnaldo Carvalho de Melo79286312011-10-27 09:19:48 -0200653 hists->stats.total_period = 0;
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300654 hists__reset_col_len(hists);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300655
John Kacur3d1d07e2009-09-28 15:32:55 +0200656 while (next) {
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300657 n = rb_entry(next, struct hist_entry, rb_node_in);
658 next = rb_next(&n->rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +0200659
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300660 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300661 hists__inc_nr_entries(hists, n);
John Kacur3d1d07e2009-09-28 15:32:55 +0200662 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300663}
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200664
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300665void hists__output_resort(struct hists *hists)
666{
667 return __hists__output_resort(hists, false);
668}
669
670void hists__output_resort_threaded(struct hists *hists)
671{
672 return __hists__output_resort(hists, true);
John Kacur3d1d07e2009-09-28 15:32:55 +0200673}
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200674
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300675static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300676 enum hist_filter filter)
677{
678 h->filtered &= ~(1 << filter);
679 if (h->filtered)
680 return;
681
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300682 ++hists->nr_entries;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -0300683 if (h->ms.unfolded)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300684 hists->nr_entries += h->nr_rows;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -0300685 h->row_offset = 0;
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900686 hists->stats.total_period += h->stat.period;
687 hists->stats.nr_events[PERF_RECORD_SAMPLE] += h->stat.nr_events;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300688
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300689 hists__calc_col_len(hists, h);
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300690}
691
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200692
693static bool hists__filter_entry_by_dso(struct hists *hists,
694 struct hist_entry *he)
695{
696 if (hists->dso_filter != NULL &&
697 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
698 he->filtered |= (1 << HIST_FILTER__DSO);
699 return true;
700 }
701
702 return false;
703}
704
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200705void hists__filter_by_dso(struct hists *hists)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300706{
707 struct rb_node *nd;
708
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300709 hists->nr_entries = hists->stats.total_period = 0;
710 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
711 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300712
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300713 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300714 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
715
716 if (symbol_conf.exclude_other && !h->parent)
717 continue;
718
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200719 if (hists__filter_entry_by_dso(hists, h))
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300720 continue;
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300721
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300722 hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300723 }
724}
725
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200726static bool hists__filter_entry_by_thread(struct hists *hists,
727 struct hist_entry *he)
728{
729 if (hists->thread_filter != NULL &&
730 he->thread != hists->thread_filter) {
731 he->filtered |= (1 << HIST_FILTER__THREAD);
732 return true;
733 }
734
735 return false;
736}
737
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200738void hists__filter_by_thread(struct hists *hists)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300739{
740 struct rb_node *nd;
741
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300742 hists->nr_entries = hists->stats.total_period = 0;
743 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
744 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300745
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300746 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300747 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
748
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200749 if (hists__filter_entry_by_thread(hists, h))
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300750 continue;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300751
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300752 hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300753 }
754}
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300755
Namhyung Kime94d53e2012-03-16 17:50:51 +0900756static bool hists__filter_entry_by_symbol(struct hists *hists,
757 struct hist_entry *he)
758{
759 if (hists->symbol_filter_str != NULL &&
760 (!he->ms.sym || strstr(he->ms.sym->name,
761 hists->symbol_filter_str) == NULL)) {
762 he->filtered |= (1 << HIST_FILTER__SYMBOL);
763 return true;
764 }
765
766 return false;
767}
768
769void hists__filter_by_symbol(struct hists *hists)
770{
771 struct rb_node *nd;
772
773 hists->nr_entries = hists->stats.total_period = 0;
774 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
775 hists__reset_col_len(hists);
776
777 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
778 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
779
780 if (hists__filter_entry_by_symbol(hists, h))
781 continue;
782
783 hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
784 }
785}
786
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200787int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300788{
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200789 return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300790}
791
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200792int hist_entry__annotate(struct hist_entry *he, size_t privsize)
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300793{
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200794 return symbol__annotate(he->ms.sym, he->ms.map, privsize);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300795}
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -0300796
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -0300797void events_stats__inc(struct events_stats *stats, u32 type)
798{
799 ++stats->nr_events[0];
800 ++stats->nr_events[type];
801}
802
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300803void hists__inc_nr_events(struct hists *hists, u32 type)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -0300804{
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -0300805 events_stats__inc(&hists->stats, type);
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -0300806}
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300807
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300808static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
809 struct hist_entry *pair)
810{
Namhyung Kimce74f602012-12-10 17:29:55 +0900811 struct rb_root *root;
812 struct rb_node **p;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300813 struct rb_node *parent = NULL;
814 struct hist_entry *he;
815 int cmp;
816
Namhyung Kimce74f602012-12-10 17:29:55 +0900817 if (sort__need_collapse)
818 root = &hists->entries_collapsed;
819 else
820 root = hists->entries_in;
821
822 p = &root->rb_node;
823
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300824 while (*p != NULL) {
825 parent = *p;
Namhyung Kimce74f602012-12-10 17:29:55 +0900826 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300827
Namhyung Kimce74f602012-12-10 17:29:55 +0900828 cmp = hist_entry__collapse(he, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300829
830 if (!cmp)
831 goto out;
832
833 if (cmp < 0)
834 p = &(*p)->rb_left;
835 else
836 p = &(*p)->rb_right;
837 }
838
839 he = hist_entry__new(pair);
840 if (he) {
Arnaldo Carvalho de Melo30193d72012-11-12 13:20:03 -0300841 memset(&he->stat, 0, sizeof(he->stat));
842 he->hists = hists;
Namhyung Kimce74f602012-12-10 17:29:55 +0900843 rb_link_node(&he->rb_node_in, parent, p);
844 rb_insert_color(&he->rb_node_in, root);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300845 hists__inc_nr_entries(hists, he);
846 }
847out:
848 return he;
849}
850
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300851static struct hist_entry *hists__find_entry(struct hists *hists,
852 struct hist_entry *he)
853{
Namhyung Kimce74f602012-12-10 17:29:55 +0900854 struct rb_node *n;
855
856 if (sort__need_collapse)
857 n = hists->entries_collapsed.rb_node;
858 else
859 n = hists->entries_in->rb_node;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300860
861 while (n) {
Namhyung Kimce74f602012-12-10 17:29:55 +0900862 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
863 int64_t cmp = hist_entry__collapse(iter, he);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300864
865 if (cmp < 0)
866 n = n->rb_left;
867 else if (cmp > 0)
868 n = n->rb_right;
869 else
870 return iter;
871 }
872
873 return NULL;
874}
875
876/*
877 * Look for pairs to link to the leader buckets (hist_entries):
878 */
879void hists__match(struct hists *leader, struct hists *other)
880{
Namhyung Kimce74f602012-12-10 17:29:55 +0900881 struct rb_root *root;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300882 struct rb_node *nd;
883 struct hist_entry *pos, *pair;
884
Namhyung Kimce74f602012-12-10 17:29:55 +0900885 if (sort__need_collapse)
886 root = &leader->entries_collapsed;
887 else
888 root = leader->entries_in;
889
890 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
891 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300892 pair = hists__find_entry(other, pos);
893
894 if (pair)
Namhyung Kim5fa90412012-11-29 15:38:34 +0900895 hist_entry__add_pair(pair, pos);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300896 }
897}
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300898
899/*
900 * Look for entries in the other hists that are not present in the leader, if
901 * we find them, just add a dummy entry on the leader hists, with period=0,
902 * nr_events=0, to serve as the list header.
903 */
904int hists__link(struct hists *leader, struct hists *other)
905{
Namhyung Kimce74f602012-12-10 17:29:55 +0900906 struct rb_root *root;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300907 struct rb_node *nd;
908 struct hist_entry *pos, *pair;
909
Namhyung Kimce74f602012-12-10 17:29:55 +0900910 if (sort__need_collapse)
911 root = &other->entries_collapsed;
912 else
913 root = other->entries_in;
914
915 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
916 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300917
918 if (!hist_entry__has_pairs(pos)) {
919 pair = hists__add_dummy_entry(leader, pos);
920 if (pair == NULL)
921 return -1;
Namhyung Kim5fa90412012-11-29 15:38:34 +0900922 hist_entry__add_pair(pos, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300923 }
924 }
925
926 return 0;
927}