blob: e4e6249b87d4eef1cd89d3df1aa3da022b550f84 [file] [log] [blame]
Frederic Weisbecker8a0ecfb2010-05-13 19:47:16 +02001#include "util.h"
Frederic Weisbecker598357e2010-05-21 12:48:39 +02002#include "build-id.h"
John Kacur3d1d07e2009-09-28 15:32:55 +02003#include "hist.h"
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -02004#include "session.h"
5#include "sort.h"
Namhyung Kim29d720e2013-01-22 18:09:33 +09006#include "evsel.h"
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -02007#include <math.h>
John Kacur3d1d07e2009-09-28 15:32:55 +02008
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02009static bool hists__filter_entry_by_dso(struct hists *hists,
10 struct hist_entry *he);
11static bool hists__filter_entry_by_thread(struct hists *hists,
12 struct hist_entry *he);
Namhyung Kime94d53e2012-03-16 17:50:51 +090013static bool hists__filter_entry_by_symbol(struct hists *hists,
14 struct hist_entry *he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -020015
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -030016enum hist_filter {
17 HIST_FILTER__DSO,
18 HIST_FILTER__THREAD,
19 HIST_FILTER__PARENT,
Namhyung Kime94d53e2012-03-16 17:50:51 +090020 HIST_FILTER__SYMBOL,
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -030021};
22
John Kacur3d1d07e2009-09-28 15:32:55 +020023struct callchain_param callchain_param = {
24 .mode = CHAIN_GRAPH_REL,
Sam Liaod797fdc2011-06-07 23:49:46 +080025 .min_percent = 0.5,
Andi Kleen99571ab2013-07-18 15:33:57 -070026 .order = ORDER_CALLEE,
27 .key = CCKEY_FUNCTION
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;
Stephane Eranian98a3b322013-01-24 16:10:35 +010070 int symlen;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030071 u16 len;
72
Namhyung Kimded19d52013-04-01 20:35:19 +090073 /*
74 * +4 accounts for '[x] ' priv level info
75 * +2 accounts for 0x prefix on raw addresses
76 * +3 accounts for ' y ' symtab origin info
77 */
78 if (h->ms.sym) {
79 symlen = h->ms.sym->namelen + 4;
80 if (verbose)
81 symlen += BITS_PER_LONG / 4 + 2 + 3;
82 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
83 } else {
Stephane Eranian98a3b322013-01-24 16:10:35 +010084 symlen = unresolved_col_width + 4 + 2;
85 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010086 hists__set_unres_dso_col_len(hists, HISTC_DSO);
Stephane Eranian98a3b322013-01-24 16:10:35 +010087 }
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030088
89 len = thread__comm_len(h->thread);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030090 if (hists__new_col_len(hists, HISTC_COMM, len))
91 hists__set_col_len(hists, HISTC_THREAD, len + 6);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030092
93 if (h->ms.map) {
94 len = dso__name_len(h->ms.map->dso);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030095 hists__new_col_len(hists, HISTC_DSO, len);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030096 }
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010097
Namhyung Kimcb993742012-12-27 18:11:42 +090098 if (h->parent)
99 hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen);
100
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100101 if (h->branch_info) {
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100102 if (h->branch_info->from.sym) {
103 symlen = (int)h->branch_info->from.sym->namelen + 4;
Namhyung Kimded19d52013-04-01 20:35:19 +0900104 if (verbose)
105 symlen += BITS_PER_LONG / 4 + 2 + 3;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100106 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
107
108 symlen = dso__name_len(h->branch_info->from.map->dso);
109 hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
110 } else {
111 symlen = unresolved_col_width + 4 + 2;
112 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
113 hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
114 }
115
116 if (h->branch_info->to.sym) {
117 symlen = (int)h->branch_info->to.sym->namelen + 4;
Namhyung Kimded19d52013-04-01 20:35:19 +0900118 if (verbose)
119 symlen += BITS_PER_LONG / 4 + 2 + 3;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100120 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
121
122 symlen = dso__name_len(h->branch_info->to.map->dso);
123 hists__new_col_len(hists, HISTC_DSO_TO, symlen);
124 } else {
125 symlen = unresolved_col_width + 4 + 2;
126 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
127 hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
128 }
129 }
Stephane Eranian98a3b322013-01-24 16:10:35 +0100130
131 if (h->mem_info) {
Stephane Eranian98a3b322013-01-24 16:10:35 +0100132 if (h->mem_info->daddr.sym) {
133 symlen = (int)h->mem_info->daddr.sym->namelen + 4
134 + unresolved_col_width + 2;
135 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
136 symlen);
137 } else {
138 symlen = unresolved_col_width + 4 + 2;
139 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
140 symlen);
141 }
142 if (h->mem_info->daddr.map) {
143 symlen = dso__name_len(h->mem_info->daddr.map->dso);
144 hists__new_col_len(hists, HISTC_MEM_DADDR_DSO,
145 symlen);
146 } else {
147 symlen = unresolved_col_width + 4 + 2;
148 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
149 }
150 } else {
151 symlen = unresolved_col_width + 4 + 2;
152 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen);
153 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
154 }
155
156 hists__new_col_len(hists, HISTC_MEM_LOCKED, 6);
157 hists__new_col_len(hists, HISTC_MEM_TLB, 22);
158 hists__new_col_len(hists, HISTC_MEM_SNOOP, 12);
159 hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3);
160 hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12);
161 hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12);
Andi Kleen475eeab2013-09-20 07:40:43 -0700162
163 if (h->transaction)
164 hists__new_col_len(hists, HISTC_TRANSACTION,
165 hist_entry__transaction_len());
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300166}
167
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900168void hists__output_recalc_col_len(struct hists *hists, int max_rows)
169{
170 struct rb_node *next = rb_first(&hists->entries);
171 struct hist_entry *n;
172 int row = 0;
173
174 hists__reset_col_len(hists);
175
176 while (next && row++ < max_rows) {
177 n = rb_entry(next, struct hist_entry, rb_node);
178 if (!n->filtered)
179 hists__calc_col_len(hists, n);
180 next = rb_next(&n->rb_node);
181 }
182}
183
Namhyung Kimf39056f2014-01-14 14:25:37 +0900184static void he_stat__add_cpumode_period(struct he_stat *he_stat,
185 unsigned int cpumode, u64 period)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800186{
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300187 switch (cpumode) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800188 case PERF_RECORD_MISC_KERNEL:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900189 he_stat->period_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800190 break;
191 case PERF_RECORD_MISC_USER:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900192 he_stat->period_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800193 break;
194 case PERF_RECORD_MISC_GUEST_KERNEL:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900195 he_stat->period_guest_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800196 break;
197 case PERF_RECORD_MISC_GUEST_USER:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900198 he_stat->period_guest_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800199 break;
200 default:
201 break;
202 }
203}
204
Andi Kleen05484292013-01-24 16:10:29 +0100205static void he_stat__add_period(struct he_stat *he_stat, u64 period,
206 u64 weight)
Namhyung Kim139c0812012-10-04 21:49:43 +0900207{
Stephane Eranian98a3b322013-01-24 16:10:35 +0100208
Namhyung Kim139c0812012-10-04 21:49:43 +0900209 he_stat->period += period;
Andi Kleen05484292013-01-24 16:10:29 +0100210 he_stat->weight += weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900211 he_stat->nr_events += 1;
212}
213
214static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
215{
216 dest->period += src->period;
217 dest->period_sys += src->period_sys;
218 dest->period_us += src->period_us;
219 dest->period_guest_sys += src->period_guest_sys;
220 dest->period_guest_us += src->period_guest_us;
221 dest->nr_events += src->nr_events;
Andi Kleen05484292013-01-24 16:10:29 +0100222 dest->weight += src->weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900223}
224
Namhyung Kimf39056f2014-01-14 14:25:37 +0900225static void he_stat__decay(struct he_stat *he_stat)
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300226{
Namhyung Kimf39056f2014-01-14 14:25:37 +0900227 he_stat->period = (he_stat->period * 7) / 8;
228 he_stat->nr_events = (he_stat->nr_events * 7) / 8;
Andi Kleen05484292013-01-24 16:10:29 +0100229 /* XXX need decay for weight too? */
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300230}
231
232static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
233{
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900234 u64 prev_period = he->stat.period;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200235
236 if (prev_period == 0)
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300237 return true;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200238
Namhyung Kimf39056f2014-01-14 14:25:37 +0900239 he_stat__decay(&he->stat);
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200240
241 if (!he->filtered)
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900242 hists->stats.total_period -= prev_period - he->stat.period;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200243
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900244 return he->stat.period == 0;
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300245}
246
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900247void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300248{
249 struct rb_node *next = rb_first(&hists->entries);
250 struct hist_entry *n;
251
252 while (next) {
253 n = rb_entry(next, struct hist_entry, rb_node);
254 next = rb_next(&n->rb_node);
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300255 /*
256 * We may be annotating this, for instance, so keep it here in
257 * case some it gets new samples, we'll eventually free it when
258 * the user stops browsing and it agains gets fully decayed.
259 */
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200260 if (((zap_user && n->level == '.') ||
261 (zap_kernel && n->level != '.') ||
262 hists__decay_entry(hists, n)) &&
263 !n->used) {
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300264 rb_erase(&n->rb_node, &hists->entries);
265
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900266 if (sort__need_collapse)
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300267 rb_erase(&n->rb_node_in, &hists->entries_collapsed);
268
269 hist_entry__free(n);
270 --hists->nr_entries;
271 }
272 }
273}
274
John Kacur3d1d07e2009-09-28 15:32:55 +0200275/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300276 * histogram, sorted on item, collects periods
John Kacur3d1d07e2009-09-28 15:32:55 +0200277 */
278
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300279static struct hist_entry *hist_entry__new(struct hist_entry *template)
280{
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +0200281 size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0;
Stephane Eranian98a3b322013-01-24 16:10:35 +0100282 struct hist_entry *he = zalloc(sizeof(*he) + callchain_size);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300283
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200284 if (he != NULL) {
285 *he = *template;
Namhyung Kimc4b35352012-10-04 21:49:42 +0900286
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200287 if (he->ms.map)
288 he->ms.map->referenced = true;
Stephane Eranian3cf0cb12013-01-14 15:02:45 +0100289
290 if (he->branch_info) {
Namhyung Kim26353a62013-04-01 20:35:17 +0900291 /*
292 * This branch info is (a part of) allocated from
293 * machine__resolve_bstack() and will be freed after
294 * adding new entries. So we need to save a copy.
295 */
296 he->branch_info = malloc(sizeof(*he->branch_info));
297 if (he->branch_info == NULL) {
298 free(he);
299 return NULL;
300 }
301
302 memcpy(he->branch_info, template->branch_info,
303 sizeof(*he->branch_info));
304
Stephane Eranian3cf0cb12013-01-14 15:02:45 +0100305 if (he->branch_info->from.map)
306 he->branch_info->from.map->referenced = true;
307 if (he->branch_info->to.map)
308 he->branch_info->to.map->referenced = true;
309 }
310
Stephane Eranian98a3b322013-01-24 16:10:35 +0100311 if (he->mem_info) {
312 if (he->mem_info->iaddr.map)
313 he->mem_info->iaddr.map->referenced = true;
314 if (he->mem_info->daddr.map)
315 he->mem_info->daddr.map->referenced = true;
316 }
317
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300318 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200319 callchain_init(he->callchain);
Arnaldo Carvalho de Melob821c732012-10-25 14:42:45 -0200320
321 INIT_LIST_HEAD(&he->pairs.node);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300322 }
323
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200324 return he;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300325}
326
Namhyung Kim66f97ed2012-12-10 17:29:56 +0900327void hists__inc_nr_entries(struct hists *hists, struct hist_entry *h)
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300328{
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300329 if (!h->filtered) {
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300330 hists__calc_col_len(hists, h);
331 ++hists->nr_entries;
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900332 hists->stats.total_period += h->stat.period;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300333 }
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300334}
335
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300336static u8 symbol__parent_filter(const struct symbol *parent)
337{
338 if (symbol_conf.exclude_other && parent == NULL)
339 return 1 << HIST_FILTER__PARENT;
340 return 0;
341}
342
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100343static struct hist_entry *add_hist_entry(struct hists *hists,
Namhyung Kimf1cbf782013-12-18 14:21:11 +0900344 struct hist_entry *entry,
345 struct addr_location *al)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300346{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300347 struct rb_node **p;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300348 struct rb_node *parent = NULL;
349 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -0700350 int64_t cmp;
Namhyung Kimf1cbf782013-12-18 14:21:11 +0900351 u64 period = entry->stat.period;
352 u64 weight = entry->stat.weight;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300353
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300354 p = &hists->entries_in->rb_node;
355
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300356 while (*p != NULL) {
357 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300358 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300359
Namhyung Kim9afcf932012-12-10 17:29:54 +0900360 /*
361 * Make sure that it receives arguments in a same order as
362 * hist_entry__collapse() so that we can use an appropriate
363 * function when searching an entry regardless which sort
364 * keys were used.
365 */
366 cmp = hist_entry__cmp(he, entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300367
368 if (!cmp) {
Andi Kleen05484292013-01-24 16:10:29 +0100369 he_stat__add_period(&he->stat, period, weight);
David Miller63fa4712012-03-27 03:14:18 -0400370
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900371 /*
372 * This mem info was allocated from machine__resolve_mem
373 * and will not be used anymore.
374 */
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300375 zfree(&entry->mem_info);
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900376
David Miller63fa4712012-03-27 03:14:18 -0400377 /* If the map of an existing hist_entry has
378 * become out-of-date due to an exec() or
379 * similar, update it. Otherwise we will
380 * mis-adjust symbol addresses when computing
381 * the history counter to increment.
382 */
383 if (he->ms.map != entry->ms.map) {
384 he->ms.map = entry->ms.map;
385 if (he->ms.map)
386 he->ms.map->referenced = true;
387 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300388 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300389 }
390
391 if (cmp < 0)
392 p = &(*p)->rb_left;
393 else
394 p = &(*p)->rb_right;
395 }
396
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100397 he = hist_entry__new(entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300398 if (!he)
Namhyung Kim27a0dcb2013-05-14 11:09:02 +0900399 return NULL;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300400
Namhyung Kimc1fb5652013-10-11 14:15:38 +0900401 hists->nr_entries++;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300402 rb_link_node(&he->rb_node_in, parent, p);
403 rb_insert_color(&he->rb_node_in, hists->entries_in);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300404out:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900405 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300406 return he;
407}
408
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300409struct hist_entry *__hists__add_entry(struct hists *hists,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100410 struct addr_location *al,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900411 struct symbol *sym_parent,
412 struct branch_info *bi,
413 struct mem_info *mi,
414 u64 period, u64 weight, u64 transaction)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100415{
416 struct hist_entry entry = {
417 .thread = al->thread,
Namhyung Kim4dfced32013-09-13 16:28:57 +0900418 .comm = thread__comm(al->thread),
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100419 .ms = {
420 .map = al->map,
421 .sym = al->sym,
422 },
423 .cpu = al->cpu,
424 .ip = al->addr,
425 .level = al->level,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900426 .stat = {
Namhyung Kimc4b35352012-10-04 21:49:42 +0900427 .nr_events = 1,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900428 .period = period,
Andi Kleen05484292013-01-24 16:10:29 +0100429 .weight = weight,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900430 },
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100431 .parent = sym_parent,
432 .filtered = symbol__parent_filter(sym_parent),
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300433 .hists = hists,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900434 .branch_info = bi,
435 .mem_info = mi,
Andi Kleen475eeab2013-09-20 07:40:43 -0700436 .transaction = transaction,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100437 };
438
Namhyung Kimf1cbf782013-12-18 14:21:11 +0900439 return add_hist_entry(hists, &entry, al);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100440}
441
John Kacur3d1d07e2009-09-28 15:32:55 +0200442int64_t
443hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
444{
445 struct sort_entry *se;
446 int64_t cmp = 0;
447
448 list_for_each_entry(se, &hist_entry__sort_list, list) {
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200449 cmp = se->se_cmp(left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200450 if (cmp)
451 break;
452 }
453
454 return cmp;
455}
456
457int64_t
458hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
459{
460 struct sort_entry *se;
461 int64_t cmp = 0;
462
463 list_for_each_entry(se, &hist_entry__sort_list, list) {
464 int64_t (*f)(struct hist_entry *, struct hist_entry *);
465
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200466 f = se->se_collapse ?: se->se_cmp;
John Kacur3d1d07e2009-09-28 15:32:55 +0200467
468 cmp = f(left, right);
469 if (cmp)
470 break;
471 }
472
473 return cmp;
474}
475
476void hist_entry__free(struct hist_entry *he)
477{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300478 zfree(&he->branch_info);
479 zfree(&he->mem_info);
Namhyung Kimf048d542013-09-11 14:09:28 +0900480 free_srcline(he->srcline);
John Kacur3d1d07e2009-09-28 15:32:55 +0200481 free(he);
482}
483
484/*
485 * collapse the histogram
486 */
487
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300488static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100489 struct rb_root *root,
490 struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +0200491{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200492 struct rb_node **p = &root->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200493 struct rb_node *parent = NULL;
494 struct hist_entry *iter;
495 int64_t cmp;
496
497 while (*p != NULL) {
498 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300499 iter = rb_entry(parent, struct hist_entry, rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +0200500
501 cmp = hist_entry__collapse(iter, he);
502
503 if (!cmp) {
Namhyung Kim139c0812012-10-04 21:49:43 +0900504 he_stat__add_stat(&iter->stat, &he->stat);
Namhyung Kim9ec60972012-09-26 16:47:28 +0900505
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100506 if (symbol_conf.use_callchain) {
Namhyung Kim47260642012-05-31 14:43:26 +0900507 callchain_cursor_reset(&callchain_cursor);
508 callchain_merge(&callchain_cursor,
509 iter->callchain,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100510 he->callchain);
511 }
John Kacur3d1d07e2009-09-28 15:32:55 +0200512 hist_entry__free(he);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300513 return false;
John Kacur3d1d07e2009-09-28 15:32:55 +0200514 }
515
516 if (cmp < 0)
517 p = &(*p)->rb_left;
518 else
519 p = &(*p)->rb_right;
520 }
521
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300522 rb_link_node(&he->rb_node_in, parent, p);
523 rb_insert_color(&he->rb_node_in, root);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300524 return true;
John Kacur3d1d07e2009-09-28 15:32:55 +0200525}
526
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300527static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
528{
529 struct rb_root *root;
530
531 pthread_mutex_lock(&hists->lock);
532
533 root = hists->entries_in;
534 if (++hists->entries_in > &hists->entries_in_array[1])
535 hists->entries_in = &hists->entries_in_array[0];
536
537 pthread_mutex_unlock(&hists->lock);
538
539 return root;
540}
541
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200542static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
543{
544 hists__filter_entry_by_dso(hists, he);
545 hists__filter_entry_by_thread(hists, he);
Namhyung Kime94d53e2012-03-16 17:50:51 +0900546 hists__filter_entry_by_symbol(hists, he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200547}
548
Namhyung Kimc1fb5652013-10-11 14:15:38 +0900549void hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300550{
551 struct rb_root *root;
552 struct rb_node *next;
553 struct hist_entry *n;
554
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900555 if (!sort__need_collapse)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300556 return;
557
558 root = hists__get_rotate_entries_in(hists);
559 next = rb_first(root);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300560
561 while (next) {
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -0300562 if (session_done())
563 break;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300564 n = rb_entry(next, struct hist_entry, rb_node_in);
565 next = rb_next(&n->rb_node_in);
566
567 rb_erase(&n->rb_node_in, root);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200568 if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
569 /*
570 * If it wasn't combined with one of the entries already
571 * collapsed, we need to apply the filters that may have
572 * been set by, say, the hist_browser.
573 */
574 hists__apply_filters(hists, n);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200575 }
Namhyung Kimc1fb5652013-10-11 14:15:38 +0900576 if (prog)
577 ui_progress__update(prog, 1);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300578 }
579}
580
John Kacur3d1d07e2009-09-28 15:32:55 +0200581/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300582 * reverse the map, sort on period.
John Kacur3d1d07e2009-09-28 15:32:55 +0200583 */
584
Namhyung Kim29d720e2013-01-22 18:09:33 +0900585static int period_cmp(u64 period_a, u64 period_b)
586{
587 if (period_a > period_b)
588 return 1;
589 if (period_a < period_b)
590 return -1;
591 return 0;
592}
593
594static int hist_entry__sort_on_period(struct hist_entry *a,
595 struct hist_entry *b)
596{
597 int ret;
598 int i, nr_members;
599 struct perf_evsel *evsel;
600 struct hist_entry *pair;
601 u64 *periods_a, *periods_b;
602
603 ret = period_cmp(a->stat.period, b->stat.period);
604 if (ret || !symbol_conf.event_group)
605 return ret;
606
607 evsel = hists_to_evsel(a->hists);
608 nr_members = evsel->nr_members;
609 if (nr_members <= 1)
610 return ret;
611
612 periods_a = zalloc(sizeof(periods_a) * nr_members);
613 periods_b = zalloc(sizeof(periods_b) * nr_members);
614
615 if (!periods_a || !periods_b)
616 goto out;
617
618 list_for_each_entry(pair, &a->pairs.head, pairs.node) {
619 evsel = hists_to_evsel(pair->hists);
620 periods_a[perf_evsel__group_idx(evsel)] = pair->stat.period;
621 }
622
623 list_for_each_entry(pair, &b->pairs.head, pairs.node) {
624 evsel = hists_to_evsel(pair->hists);
625 periods_b[perf_evsel__group_idx(evsel)] = pair->stat.period;
626 }
627
628 for (i = 1; i < nr_members; i++) {
629 ret = period_cmp(periods_a[i], periods_b[i]);
630 if (ret)
631 break;
632 }
633
634out:
635 free(periods_a);
636 free(periods_b);
637
638 return ret;
639}
640
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300641static void __hists__insert_output_entry(struct rb_root *entries,
642 struct hist_entry *he,
643 u64 min_callchain_hits)
John Kacur3d1d07e2009-09-28 15:32:55 +0200644{
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300645 struct rb_node **p = &entries->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200646 struct rb_node *parent = NULL;
647 struct hist_entry *iter;
648
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200649 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melob9fb9302010-04-02 09:50:42 -0300650 callchain_param.sort(&he->sorted_chain, he->callchain,
John Kacur3d1d07e2009-09-28 15:32:55 +0200651 min_callchain_hits, &callchain_param);
652
653 while (*p != NULL) {
654 parent = *p;
655 iter = rb_entry(parent, struct hist_entry, rb_node);
656
Namhyung Kim29d720e2013-01-22 18:09:33 +0900657 if (hist_entry__sort_on_period(he, iter) > 0)
John Kacur3d1d07e2009-09-28 15:32:55 +0200658 p = &(*p)->rb_left;
659 else
660 p = &(*p)->rb_right;
661 }
662
663 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300664 rb_insert_color(&he->rb_node, entries);
John Kacur3d1d07e2009-09-28 15:32:55 +0200665}
666
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900667void hists__output_resort(struct hists *hists)
John Kacur3d1d07e2009-09-28 15:32:55 +0200668{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300669 struct rb_root *root;
John Kacur3d1d07e2009-09-28 15:32:55 +0200670 struct rb_node *next;
671 struct hist_entry *n;
John Kacur3d1d07e2009-09-28 15:32:55 +0200672 u64 min_callchain_hits;
673
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300674 min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
John Kacur3d1d07e2009-09-28 15:32:55 +0200675
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900676 if (sort__need_collapse)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300677 root = &hists->entries_collapsed;
678 else
679 root = hists->entries_in;
680
681 next = rb_first(root);
682 hists->entries = RB_ROOT;
John Kacur3d1d07e2009-09-28 15:32:55 +0200683
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300684 hists->nr_entries = 0;
Arnaldo Carvalho de Melo79286312011-10-27 09:19:48 -0200685 hists->stats.total_period = 0;
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300686 hists__reset_col_len(hists);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300687
John Kacur3d1d07e2009-09-28 15:32:55 +0200688 while (next) {
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300689 n = rb_entry(next, struct hist_entry, rb_node_in);
690 next = rb_next(&n->rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +0200691
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300692 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300693 hists__inc_nr_entries(hists, n);
John Kacur3d1d07e2009-09-28 15:32:55 +0200694 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300695}
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200696
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300697static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300698 enum hist_filter filter)
699{
700 h->filtered &= ~(1 << filter);
701 if (h->filtered)
702 return;
703
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300704 ++hists->nr_entries;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -0300705 if (h->ms.unfolded)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300706 hists->nr_entries += h->nr_rows;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -0300707 h->row_offset = 0;
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900708 hists->stats.total_period += h->stat.period;
709 hists->stats.nr_events[PERF_RECORD_SAMPLE] += h->stat.nr_events;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300710
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300711 hists__calc_col_len(hists, h);
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300712}
713
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200714
715static bool hists__filter_entry_by_dso(struct hists *hists,
716 struct hist_entry *he)
717{
718 if (hists->dso_filter != NULL &&
719 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
720 he->filtered |= (1 << HIST_FILTER__DSO);
721 return true;
722 }
723
724 return false;
725}
726
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200727void hists__filter_by_dso(struct hists *hists)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300728{
729 struct rb_node *nd;
730
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300731 hists->nr_entries = hists->stats.total_period = 0;
732 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
733 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300734
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300735 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300736 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
737
738 if (symbol_conf.exclude_other && !h->parent)
739 continue;
740
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200741 if (hists__filter_entry_by_dso(hists, h))
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300742 continue;
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300743
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300744 hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300745 }
746}
747
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200748static bool hists__filter_entry_by_thread(struct hists *hists,
749 struct hist_entry *he)
750{
751 if (hists->thread_filter != NULL &&
752 he->thread != hists->thread_filter) {
753 he->filtered |= (1 << HIST_FILTER__THREAD);
754 return true;
755 }
756
757 return false;
758}
759
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200760void hists__filter_by_thread(struct hists *hists)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300761{
762 struct rb_node *nd;
763
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300764 hists->nr_entries = hists->stats.total_period = 0;
765 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
766 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300767
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300768 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300769 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
770
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200771 if (hists__filter_entry_by_thread(hists, h))
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300772 continue;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300773
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300774 hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300775 }
776}
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300777
Namhyung Kime94d53e2012-03-16 17:50:51 +0900778static bool hists__filter_entry_by_symbol(struct hists *hists,
779 struct hist_entry *he)
780{
781 if (hists->symbol_filter_str != NULL &&
782 (!he->ms.sym || strstr(he->ms.sym->name,
783 hists->symbol_filter_str) == NULL)) {
784 he->filtered |= (1 << HIST_FILTER__SYMBOL);
785 return true;
786 }
787
788 return false;
789}
790
791void hists__filter_by_symbol(struct hists *hists)
792{
793 struct rb_node *nd;
794
795 hists->nr_entries = hists->stats.total_period = 0;
796 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
797 hists__reset_col_len(hists);
798
799 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
800 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
801
802 if (hists__filter_entry_by_symbol(hists, h))
803 continue;
804
805 hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
806 }
807}
808
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -0300809void events_stats__inc(struct events_stats *stats, u32 type)
810{
811 ++stats->nr_events[0];
812 ++stats->nr_events[type];
813}
814
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300815void hists__inc_nr_events(struct hists *hists, u32 type)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -0300816{
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -0300817 events_stats__inc(&hists->stats, type);
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -0300818}
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300819
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300820static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
821 struct hist_entry *pair)
822{
Namhyung Kimce74f602012-12-10 17:29:55 +0900823 struct rb_root *root;
824 struct rb_node **p;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300825 struct rb_node *parent = NULL;
826 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -0700827 int64_t cmp;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300828
Namhyung Kimce74f602012-12-10 17:29:55 +0900829 if (sort__need_collapse)
830 root = &hists->entries_collapsed;
831 else
832 root = hists->entries_in;
833
834 p = &root->rb_node;
835
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300836 while (*p != NULL) {
837 parent = *p;
Namhyung Kimce74f602012-12-10 17:29:55 +0900838 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300839
Namhyung Kimce74f602012-12-10 17:29:55 +0900840 cmp = hist_entry__collapse(he, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300841
842 if (!cmp)
843 goto out;
844
845 if (cmp < 0)
846 p = &(*p)->rb_left;
847 else
848 p = &(*p)->rb_right;
849 }
850
851 he = hist_entry__new(pair);
852 if (he) {
Arnaldo Carvalho de Melo30193d72012-11-12 13:20:03 -0300853 memset(&he->stat, 0, sizeof(he->stat));
854 he->hists = hists;
Namhyung Kimce74f602012-12-10 17:29:55 +0900855 rb_link_node(&he->rb_node_in, parent, p);
856 rb_insert_color(&he->rb_node_in, root);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300857 hists__inc_nr_entries(hists, he);
Jiri Olsae0af43d2012-12-01 21:18:20 +0100858 he->dummy = true;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300859 }
860out:
861 return he;
862}
863
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300864static struct hist_entry *hists__find_entry(struct hists *hists,
865 struct hist_entry *he)
866{
Namhyung Kimce74f602012-12-10 17:29:55 +0900867 struct rb_node *n;
868
869 if (sort__need_collapse)
870 n = hists->entries_collapsed.rb_node;
871 else
872 n = hists->entries_in->rb_node;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300873
874 while (n) {
Namhyung Kimce74f602012-12-10 17:29:55 +0900875 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
876 int64_t cmp = hist_entry__collapse(iter, he);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300877
878 if (cmp < 0)
879 n = n->rb_left;
880 else if (cmp > 0)
881 n = n->rb_right;
882 else
883 return iter;
884 }
885
886 return NULL;
887}
888
889/*
890 * Look for pairs to link to the leader buckets (hist_entries):
891 */
892void hists__match(struct hists *leader, struct hists *other)
893{
Namhyung Kimce74f602012-12-10 17:29:55 +0900894 struct rb_root *root;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300895 struct rb_node *nd;
896 struct hist_entry *pos, *pair;
897
Namhyung Kimce74f602012-12-10 17:29:55 +0900898 if (sort__need_collapse)
899 root = &leader->entries_collapsed;
900 else
901 root = leader->entries_in;
902
903 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
904 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300905 pair = hists__find_entry(other, pos);
906
907 if (pair)
Namhyung Kim5fa90412012-11-29 15:38:34 +0900908 hist_entry__add_pair(pair, pos);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300909 }
910}
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300911
912/*
913 * Look for entries in the other hists that are not present in the leader, if
914 * we find them, just add a dummy entry on the leader hists, with period=0,
915 * nr_events=0, to serve as the list header.
916 */
917int hists__link(struct hists *leader, struct hists *other)
918{
Namhyung Kimce74f602012-12-10 17:29:55 +0900919 struct rb_root *root;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300920 struct rb_node *nd;
921 struct hist_entry *pos, *pair;
922
Namhyung Kimce74f602012-12-10 17:29:55 +0900923 if (sort__need_collapse)
924 root = &other->entries_collapsed;
925 else
926 root = other->entries_in;
927
928 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
929 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300930
931 if (!hist_entry__has_pairs(pos)) {
932 pair = hists__add_dummy_entry(leader, pos);
933 if (pair == NULL)
934 return -1;
Namhyung Kim5fa90412012-11-29 15:38:34 +0900935 hist_entry__add_pair(pos, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300936 }
937 }
938
939 return 0;
940}