blob: 72b4eec820c3aac05f4c1990d596bf971afc102a [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;
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);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300162}
163
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900164void hists__output_recalc_col_len(struct hists *hists, int max_rows)
165{
166 struct rb_node *next = rb_first(&hists->entries);
167 struct hist_entry *n;
168 int row = 0;
169
170 hists__reset_col_len(hists);
171
172 while (next && row++ < max_rows) {
173 n = rb_entry(next, struct hist_entry, rb_node);
174 if (!n->filtered)
175 hists__calc_col_len(hists, n);
176 next = rb_next(&n->rb_node);
177 }
178}
179
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200180static void hist_entry__add_cpumode_period(struct hist_entry *he,
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300181 unsigned int cpumode, u64 period)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800182{
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300183 switch (cpumode) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800184 case PERF_RECORD_MISC_KERNEL:
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900185 he->stat.period_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800186 break;
187 case PERF_RECORD_MISC_USER:
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900188 he->stat.period_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800189 break;
190 case PERF_RECORD_MISC_GUEST_KERNEL:
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900191 he->stat.period_guest_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800192 break;
193 case PERF_RECORD_MISC_GUEST_USER:
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900194 he->stat.period_guest_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800195 break;
196 default:
197 break;
198 }
199}
200
Andi Kleen05484292013-01-24 16:10:29 +0100201static void he_stat__add_period(struct he_stat *he_stat, u64 period,
202 u64 weight)
Namhyung Kim139c0812012-10-04 21:49:43 +0900203{
Stephane Eranian98a3b322013-01-24 16:10:35 +0100204
Namhyung Kim139c0812012-10-04 21:49:43 +0900205 he_stat->period += period;
Andi Kleen05484292013-01-24 16:10:29 +0100206 he_stat->weight += weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900207 he_stat->nr_events += 1;
208}
209
210static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
211{
212 dest->period += src->period;
213 dest->period_sys += src->period_sys;
214 dest->period_us += src->period_us;
215 dest->period_guest_sys += src->period_guest_sys;
216 dest->period_guest_us += src->period_guest_us;
217 dest->nr_events += src->nr_events;
Andi Kleen05484292013-01-24 16:10:29 +0100218 dest->weight += src->weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900219}
220
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300221static void hist_entry__decay(struct hist_entry *he)
222{
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900223 he->stat.period = (he->stat.period * 7) / 8;
224 he->stat.nr_events = (he->stat.nr_events * 7) / 8;
Andi Kleen05484292013-01-24 16:10:29 +0100225 /* XXX need decay for weight too? */
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300226}
227
228static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
229{
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900230 u64 prev_period = he->stat.period;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200231
232 if (prev_period == 0)
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300233 return true;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200234
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300235 hist_entry__decay(he);
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200236
237 if (!he->filtered)
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900238 hists->stats.total_period -= prev_period - he->stat.period;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200239
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900240 return he->stat.period == 0;
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300241}
242
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200243static void __hists__decay_entries(struct hists *hists, bool zap_user,
244 bool zap_kernel, bool threaded)
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300245{
246 struct rb_node *next = rb_first(&hists->entries);
247 struct hist_entry *n;
248
249 while (next) {
250 n = rb_entry(next, struct hist_entry, rb_node);
251 next = rb_next(&n->rb_node);
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300252 /*
253 * We may be annotating this, for instance, so keep it here in
254 * case some it gets new samples, we'll eventually free it when
255 * the user stops browsing and it agains gets fully decayed.
256 */
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200257 if (((zap_user && n->level == '.') ||
258 (zap_kernel && n->level != '.') ||
259 hists__decay_entry(hists, n)) &&
260 !n->used) {
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300261 rb_erase(&n->rb_node, &hists->entries);
262
Arnaldo Carvalho de Meloe345fa12011-10-13 09:06:54 -0300263 if (sort__need_collapse || threaded)
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300264 rb_erase(&n->rb_node_in, &hists->entries_collapsed);
265
266 hist_entry__free(n);
267 --hists->nr_entries;
268 }
269 }
270}
271
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200272void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
Arnaldo Carvalho de Meloe345fa12011-10-13 09:06:54 -0300273{
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200274 return __hists__decay_entries(hists, zap_user, zap_kernel, false);
Arnaldo Carvalho de Meloe345fa12011-10-13 09:06:54 -0300275}
276
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200277void hists__decay_entries_threaded(struct hists *hists,
278 bool zap_user, bool zap_kernel)
Arnaldo Carvalho de Meloe345fa12011-10-13 09:06:54 -0300279{
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200280 return __hists__decay_entries(hists, zap_user, zap_kernel, true);
Arnaldo Carvalho de Meloe345fa12011-10-13 09:06:54 -0300281}
282
John Kacur3d1d07e2009-09-28 15:32:55 +0200283/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300284 * histogram, sorted on item, collects periods
John Kacur3d1d07e2009-09-28 15:32:55 +0200285 */
286
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300287static struct hist_entry *hist_entry__new(struct hist_entry *template)
288{
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +0200289 size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0;
Stephane Eranian98a3b322013-01-24 16:10:35 +0100290 struct hist_entry *he = zalloc(sizeof(*he) + callchain_size);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300291
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200292 if (he != NULL) {
293 *he = *template;
Namhyung Kimc4b35352012-10-04 21:49:42 +0900294
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200295 if (he->ms.map)
296 he->ms.map->referenced = true;
Stephane Eranian3cf0cb12013-01-14 15:02:45 +0100297
298 if (he->branch_info) {
Namhyung Kim26353a62013-04-01 20:35:17 +0900299 /*
300 * This branch info is (a part of) allocated from
301 * machine__resolve_bstack() and will be freed after
302 * adding new entries. So we need to save a copy.
303 */
304 he->branch_info = malloc(sizeof(*he->branch_info));
305 if (he->branch_info == NULL) {
306 free(he);
307 return NULL;
308 }
309
310 memcpy(he->branch_info, template->branch_info,
311 sizeof(*he->branch_info));
312
Stephane Eranian3cf0cb12013-01-14 15:02:45 +0100313 if (he->branch_info->from.map)
314 he->branch_info->from.map->referenced = true;
315 if (he->branch_info->to.map)
316 he->branch_info->to.map->referenced = true;
317 }
318
Stephane Eranian98a3b322013-01-24 16:10:35 +0100319 if (he->mem_info) {
320 if (he->mem_info->iaddr.map)
321 he->mem_info->iaddr.map->referenced = true;
322 if (he->mem_info->daddr.map)
323 he->mem_info->daddr.map->referenced = true;
324 }
325
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300326 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200327 callchain_init(he->callchain);
Arnaldo Carvalho de Melob821c732012-10-25 14:42:45 -0200328
329 INIT_LIST_HEAD(&he->pairs.node);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300330 }
331
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200332 return he;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300333}
334
Namhyung Kim66f97ed2012-12-10 17:29:56 +0900335void hists__inc_nr_entries(struct hists *hists, struct hist_entry *h)
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300336{
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300337 if (!h->filtered) {
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300338 hists__calc_col_len(hists, h);
339 ++hists->nr_entries;
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900340 hists->stats.total_period += h->stat.period;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300341 }
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300342}
343
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300344static u8 symbol__parent_filter(const struct symbol *parent)
345{
346 if (symbol_conf.exclude_other && parent == NULL)
347 return 1 << HIST_FILTER__PARENT;
348 return 0;
349}
350
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100351static struct hist_entry *add_hist_entry(struct hists *hists,
352 struct hist_entry *entry,
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300353 struct addr_location *al,
Andi Kleen05484292013-01-24 16:10:29 +0100354 u64 period,
355 u64 weight)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300356{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300357 struct rb_node **p;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300358 struct rb_node *parent = NULL;
359 struct hist_entry *he;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300360 int cmp;
361
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300362 pthread_mutex_lock(&hists->lock);
363
364 p = &hists->entries_in->rb_node;
365
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300366 while (*p != NULL) {
367 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300368 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300369
Namhyung Kim9afcf932012-12-10 17:29:54 +0900370 /*
371 * Make sure that it receives arguments in a same order as
372 * hist_entry__collapse() so that we can use an appropriate
373 * function when searching an entry regardless which sort
374 * keys were used.
375 */
376 cmp = hist_entry__cmp(he, entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300377
378 if (!cmp) {
Andi Kleen05484292013-01-24 16:10:29 +0100379 he_stat__add_period(&he->stat, period, weight);
David Miller63fa4712012-03-27 03:14:18 -0400380
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900381 /*
382 * This mem info was allocated from machine__resolve_mem
383 * and will not be used anymore.
384 */
385 free(entry->mem_info);
386
David Miller63fa4712012-03-27 03:14:18 -0400387 /* If the map of an existing hist_entry has
388 * become out-of-date due to an exec() or
389 * similar, update it. Otherwise we will
390 * mis-adjust symbol addresses when computing
391 * the history counter to increment.
392 */
393 if (he->ms.map != entry->ms.map) {
394 he->ms.map = entry->ms.map;
395 if (he->ms.map)
396 he->ms.map->referenced = true;
397 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300398 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300399 }
400
401 if (cmp < 0)
402 p = &(*p)->rb_left;
403 else
404 p = &(*p)->rb_right;
405 }
406
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100407 he = hist_entry__new(entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300408 if (!he)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300409 goto out_unlock;
410
411 rb_link_node(&he->rb_node_in, parent, p);
412 rb_insert_color(&he->rb_node_in, hists->entries_in);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300413out:
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300414 hist_entry__add_cpumode_period(he, al->cpumode, period);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300415out_unlock:
416 pthread_mutex_unlock(&hists->lock);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300417 return he;
418}
419
Stephane Eranian98a3b322013-01-24 16:10:35 +0100420struct hist_entry *__hists__add_mem_entry(struct hists *self,
421 struct addr_location *al,
422 struct symbol *sym_parent,
423 struct mem_info *mi,
424 u64 period,
425 u64 weight)
426{
427 struct hist_entry entry = {
428 .thread = al->thread,
429 .ms = {
430 .map = al->map,
431 .sym = al->sym,
432 },
433 .stat = {
434 .period = period,
435 .weight = weight,
436 .nr_events = 1,
437 },
438 .cpu = al->cpu,
439 .ip = al->addr,
440 .level = al->level,
441 .parent = sym_parent,
442 .filtered = symbol__parent_filter(sym_parent),
443 .hists = self,
444 .mem_info = mi,
445 .branch_info = NULL,
446 };
447 return add_hist_entry(self, &entry, al, period, weight);
448}
449
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100450struct hist_entry *__hists__add_branch_entry(struct hists *self,
451 struct addr_location *al,
452 struct symbol *sym_parent,
453 struct branch_info *bi,
Andi Kleen05484292013-01-24 16:10:29 +0100454 u64 period,
455 u64 weight)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100456{
457 struct hist_entry entry = {
458 .thread = al->thread,
459 .ms = {
460 .map = bi->to.map,
461 .sym = bi->to.sym,
462 },
463 .cpu = al->cpu,
464 .ip = bi->to.addr,
465 .level = al->level,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900466 .stat = {
467 .period = period,
Namhyung Kimc4b35352012-10-04 21:49:42 +0900468 .nr_events = 1,
Andi Kleen05484292013-01-24 16:10:29 +0100469 .weight = weight,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900470 },
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100471 .parent = sym_parent,
472 .filtered = symbol__parent_filter(sym_parent),
473 .branch_info = bi,
Jiri Olsaae359f12012-10-04 21:49:35 +0900474 .hists = self,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100475 .mem_info = NULL,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100476 };
477
Andi Kleen05484292013-01-24 16:10:29 +0100478 return add_hist_entry(self, &entry, al, period, weight);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100479}
480
481struct hist_entry *__hists__add_entry(struct hists *self,
482 struct addr_location *al,
Andi Kleen05484292013-01-24 16:10:29 +0100483 struct symbol *sym_parent, u64 period,
484 u64 weight)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100485{
486 struct hist_entry entry = {
487 .thread = al->thread,
488 .ms = {
489 .map = al->map,
490 .sym = al->sym,
491 },
492 .cpu = al->cpu,
493 .ip = al->addr,
494 .level = al->level,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900495 .stat = {
496 .period = period,
Namhyung Kimc4b35352012-10-04 21:49:42 +0900497 .nr_events = 1,
Andi Kleen05484292013-01-24 16:10:29 +0100498 .weight = weight,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900499 },
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100500 .parent = sym_parent,
501 .filtered = symbol__parent_filter(sym_parent),
Jiri Olsaae359f12012-10-04 21:49:35 +0900502 .hists = self,
Stephane Eranian98a3b322013-01-24 16:10:35 +0100503 .branch_info = NULL,
504 .mem_info = NULL,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100505 };
506
Andi Kleen05484292013-01-24 16:10:29 +0100507 return add_hist_entry(self, &entry, al, period, weight);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100508}
509
John Kacur3d1d07e2009-09-28 15:32:55 +0200510int64_t
511hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
512{
513 struct sort_entry *se;
514 int64_t cmp = 0;
515
516 list_for_each_entry(se, &hist_entry__sort_list, list) {
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200517 cmp = se->se_cmp(left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200518 if (cmp)
519 break;
520 }
521
522 return cmp;
523}
524
525int64_t
526hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
527{
528 struct sort_entry *se;
529 int64_t cmp = 0;
530
531 list_for_each_entry(se, &hist_entry__sort_list, list) {
532 int64_t (*f)(struct hist_entry *, struct hist_entry *);
533
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200534 f = se->se_collapse ?: se->se_cmp;
John Kacur3d1d07e2009-09-28 15:32:55 +0200535
536 cmp = f(left, right);
537 if (cmp)
538 break;
539 }
540
541 return cmp;
542}
543
544void hist_entry__free(struct hist_entry *he)
545{
Namhyung Kim580e3382012-11-07 16:27:14 +0900546 free(he->branch_info);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100547 free(he->mem_info);
John Kacur3d1d07e2009-09-28 15:32:55 +0200548 free(he);
549}
550
551/*
552 * collapse the histogram
553 */
554
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300555static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100556 struct rb_root *root,
557 struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +0200558{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200559 struct rb_node **p = &root->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200560 struct rb_node *parent = NULL;
561 struct hist_entry *iter;
562 int64_t cmp;
563
564 while (*p != NULL) {
565 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300566 iter = rb_entry(parent, struct hist_entry, rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +0200567
568 cmp = hist_entry__collapse(iter, he);
569
570 if (!cmp) {
Namhyung Kim139c0812012-10-04 21:49:43 +0900571 he_stat__add_stat(&iter->stat, &he->stat);
Namhyung Kim9ec60972012-09-26 16:47:28 +0900572
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100573 if (symbol_conf.use_callchain) {
Namhyung Kim47260642012-05-31 14:43:26 +0900574 callchain_cursor_reset(&callchain_cursor);
575 callchain_merge(&callchain_cursor,
576 iter->callchain,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100577 he->callchain);
578 }
John Kacur3d1d07e2009-09-28 15:32:55 +0200579 hist_entry__free(he);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300580 return false;
John Kacur3d1d07e2009-09-28 15:32:55 +0200581 }
582
583 if (cmp < 0)
584 p = &(*p)->rb_left;
585 else
586 p = &(*p)->rb_right;
587 }
588
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300589 rb_link_node(&he->rb_node_in, parent, p);
590 rb_insert_color(&he->rb_node_in, root);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300591 return true;
John Kacur3d1d07e2009-09-28 15:32:55 +0200592}
593
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300594static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
595{
596 struct rb_root *root;
597
598 pthread_mutex_lock(&hists->lock);
599
600 root = hists->entries_in;
601 if (++hists->entries_in > &hists->entries_in_array[1])
602 hists->entries_in = &hists->entries_in_array[0];
603
604 pthread_mutex_unlock(&hists->lock);
605
606 return root;
607}
608
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200609static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
610{
611 hists__filter_entry_by_dso(hists, he);
612 hists__filter_entry_by_thread(hists, he);
Namhyung Kime94d53e2012-03-16 17:50:51 +0900613 hists__filter_entry_by_symbol(hists, he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200614}
615
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300616static void __hists__collapse_resort(struct hists *hists, bool threaded)
617{
618 struct rb_root *root;
619 struct rb_node *next;
620 struct hist_entry *n;
621
622 if (!sort__need_collapse && !threaded)
623 return;
624
625 root = hists__get_rotate_entries_in(hists);
626 next = rb_first(root);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300627
628 while (next) {
629 n = rb_entry(next, struct hist_entry, rb_node_in);
630 next = rb_next(&n->rb_node_in);
631
632 rb_erase(&n->rb_node_in, root);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200633 if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
634 /*
635 * If it wasn't combined with one of the entries already
636 * collapsed, we need to apply the filters that may have
637 * been set by, say, the hist_browser.
638 */
639 hists__apply_filters(hists, n);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200640 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300641 }
642}
643
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300644void hists__collapse_resort(struct hists *hists)
John Kacur3d1d07e2009-09-28 15:32:55 +0200645{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300646 return __hists__collapse_resort(hists, false);
647}
John Kacur3d1d07e2009-09-28 15:32:55 +0200648
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300649void hists__collapse_resort_threaded(struct hists *hists)
650{
651 return __hists__collapse_resort(hists, true);
John Kacur3d1d07e2009-09-28 15:32:55 +0200652}
653
654/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300655 * reverse the map, sort on period.
John Kacur3d1d07e2009-09-28 15:32:55 +0200656 */
657
Namhyung Kim29d720e2013-01-22 18:09:33 +0900658static int period_cmp(u64 period_a, u64 period_b)
659{
660 if (period_a > period_b)
661 return 1;
662 if (period_a < period_b)
663 return -1;
664 return 0;
665}
666
667static int hist_entry__sort_on_period(struct hist_entry *a,
668 struct hist_entry *b)
669{
670 int ret;
671 int i, nr_members;
672 struct perf_evsel *evsel;
673 struct hist_entry *pair;
674 u64 *periods_a, *periods_b;
675
676 ret = period_cmp(a->stat.period, b->stat.period);
677 if (ret || !symbol_conf.event_group)
678 return ret;
679
680 evsel = hists_to_evsel(a->hists);
681 nr_members = evsel->nr_members;
682 if (nr_members <= 1)
683 return ret;
684
685 periods_a = zalloc(sizeof(periods_a) * nr_members);
686 periods_b = zalloc(sizeof(periods_b) * nr_members);
687
688 if (!periods_a || !periods_b)
689 goto out;
690
691 list_for_each_entry(pair, &a->pairs.head, pairs.node) {
692 evsel = hists_to_evsel(pair->hists);
693 periods_a[perf_evsel__group_idx(evsel)] = pair->stat.period;
694 }
695
696 list_for_each_entry(pair, &b->pairs.head, pairs.node) {
697 evsel = hists_to_evsel(pair->hists);
698 periods_b[perf_evsel__group_idx(evsel)] = pair->stat.period;
699 }
700
701 for (i = 1; i < nr_members; i++) {
702 ret = period_cmp(periods_a[i], periods_b[i]);
703 if (ret)
704 break;
705 }
706
707out:
708 free(periods_a);
709 free(periods_b);
710
711 return ret;
712}
713
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300714static void __hists__insert_output_entry(struct rb_root *entries,
715 struct hist_entry *he,
716 u64 min_callchain_hits)
John Kacur3d1d07e2009-09-28 15:32:55 +0200717{
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300718 struct rb_node **p = &entries->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200719 struct rb_node *parent = NULL;
720 struct hist_entry *iter;
721
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200722 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melob9fb9302010-04-02 09:50:42 -0300723 callchain_param.sort(&he->sorted_chain, he->callchain,
John Kacur3d1d07e2009-09-28 15:32:55 +0200724 min_callchain_hits, &callchain_param);
725
726 while (*p != NULL) {
727 parent = *p;
728 iter = rb_entry(parent, struct hist_entry, rb_node);
729
Namhyung Kim29d720e2013-01-22 18:09:33 +0900730 if (hist_entry__sort_on_period(he, iter) > 0)
John Kacur3d1d07e2009-09-28 15:32:55 +0200731 p = &(*p)->rb_left;
732 else
733 p = &(*p)->rb_right;
734 }
735
736 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300737 rb_insert_color(&he->rb_node, entries);
John Kacur3d1d07e2009-09-28 15:32:55 +0200738}
739
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300740static void __hists__output_resort(struct hists *hists, bool threaded)
John Kacur3d1d07e2009-09-28 15:32:55 +0200741{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300742 struct rb_root *root;
John Kacur3d1d07e2009-09-28 15:32:55 +0200743 struct rb_node *next;
744 struct hist_entry *n;
John Kacur3d1d07e2009-09-28 15:32:55 +0200745 u64 min_callchain_hits;
746
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300747 min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
John Kacur3d1d07e2009-09-28 15:32:55 +0200748
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300749 if (sort__need_collapse || threaded)
750 root = &hists->entries_collapsed;
751 else
752 root = hists->entries_in;
753
754 next = rb_first(root);
755 hists->entries = RB_ROOT;
John Kacur3d1d07e2009-09-28 15:32:55 +0200756
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300757 hists->nr_entries = 0;
Arnaldo Carvalho de Melo79286312011-10-27 09:19:48 -0200758 hists->stats.total_period = 0;
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300759 hists__reset_col_len(hists);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300760
John Kacur3d1d07e2009-09-28 15:32:55 +0200761 while (next) {
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300762 n = rb_entry(next, struct hist_entry, rb_node_in);
763 next = rb_next(&n->rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +0200764
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300765 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300766 hists__inc_nr_entries(hists, n);
John Kacur3d1d07e2009-09-28 15:32:55 +0200767 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300768}
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200769
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300770void hists__output_resort(struct hists *hists)
771{
772 return __hists__output_resort(hists, false);
773}
774
775void hists__output_resort_threaded(struct hists *hists)
776{
777 return __hists__output_resort(hists, true);
John Kacur3d1d07e2009-09-28 15:32:55 +0200778}
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200779
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300780static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300781 enum hist_filter filter)
782{
783 h->filtered &= ~(1 << filter);
784 if (h->filtered)
785 return;
786
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300787 ++hists->nr_entries;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -0300788 if (h->ms.unfolded)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300789 hists->nr_entries += h->nr_rows;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -0300790 h->row_offset = 0;
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900791 hists->stats.total_period += h->stat.period;
792 hists->stats.nr_events[PERF_RECORD_SAMPLE] += h->stat.nr_events;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300793
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300794 hists__calc_col_len(hists, h);
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300795}
796
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200797
798static bool hists__filter_entry_by_dso(struct hists *hists,
799 struct hist_entry *he)
800{
801 if (hists->dso_filter != NULL &&
802 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
803 he->filtered |= (1 << HIST_FILTER__DSO);
804 return true;
805 }
806
807 return false;
808}
809
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200810void hists__filter_by_dso(struct hists *hists)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300811{
812 struct rb_node *nd;
813
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300814 hists->nr_entries = hists->stats.total_period = 0;
815 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
816 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300817
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300818 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300819 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
820
821 if (symbol_conf.exclude_other && !h->parent)
822 continue;
823
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200824 if (hists__filter_entry_by_dso(hists, h))
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300825 continue;
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300826
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300827 hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300828 }
829}
830
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200831static bool hists__filter_entry_by_thread(struct hists *hists,
832 struct hist_entry *he)
833{
834 if (hists->thread_filter != NULL &&
835 he->thread != hists->thread_filter) {
836 he->filtered |= (1 << HIST_FILTER__THREAD);
837 return true;
838 }
839
840 return false;
841}
842
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200843void hists__filter_by_thread(struct hists *hists)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300844{
845 struct rb_node *nd;
846
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300847 hists->nr_entries = hists->stats.total_period = 0;
848 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
849 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300850
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300851 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300852 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
853
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200854 if (hists__filter_entry_by_thread(hists, h))
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300855 continue;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300856
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300857 hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300858 }
859}
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300860
Namhyung Kime94d53e2012-03-16 17:50:51 +0900861static bool hists__filter_entry_by_symbol(struct hists *hists,
862 struct hist_entry *he)
863{
864 if (hists->symbol_filter_str != NULL &&
865 (!he->ms.sym || strstr(he->ms.sym->name,
866 hists->symbol_filter_str) == NULL)) {
867 he->filtered |= (1 << HIST_FILTER__SYMBOL);
868 return true;
869 }
870
871 return false;
872}
873
874void hists__filter_by_symbol(struct hists *hists)
875{
876 struct rb_node *nd;
877
878 hists->nr_entries = hists->stats.total_period = 0;
879 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
880 hists__reset_col_len(hists);
881
882 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
883 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
884
885 if (hists__filter_entry_by_symbol(hists, h))
886 continue;
887
888 hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
889 }
890}
891
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200892int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300893{
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200894 return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300895}
896
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200897int hist_entry__annotate(struct hist_entry *he, size_t privsize)
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300898{
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200899 return symbol__annotate(he->ms.sym, he->ms.map, privsize);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300900}
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -0300901
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -0300902void events_stats__inc(struct events_stats *stats, u32 type)
903{
904 ++stats->nr_events[0];
905 ++stats->nr_events[type];
906}
907
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300908void hists__inc_nr_events(struct hists *hists, u32 type)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -0300909{
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -0300910 events_stats__inc(&hists->stats, type);
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -0300911}
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300912
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300913static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
914 struct hist_entry *pair)
915{
Namhyung Kimce74f602012-12-10 17:29:55 +0900916 struct rb_root *root;
917 struct rb_node **p;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300918 struct rb_node *parent = NULL;
919 struct hist_entry *he;
920 int cmp;
921
Namhyung Kimce74f602012-12-10 17:29:55 +0900922 if (sort__need_collapse)
923 root = &hists->entries_collapsed;
924 else
925 root = hists->entries_in;
926
927 p = &root->rb_node;
928
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300929 while (*p != NULL) {
930 parent = *p;
Namhyung Kimce74f602012-12-10 17:29:55 +0900931 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300932
Namhyung Kimce74f602012-12-10 17:29:55 +0900933 cmp = hist_entry__collapse(he, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300934
935 if (!cmp)
936 goto out;
937
938 if (cmp < 0)
939 p = &(*p)->rb_left;
940 else
941 p = &(*p)->rb_right;
942 }
943
944 he = hist_entry__new(pair);
945 if (he) {
Arnaldo Carvalho de Melo30193d72012-11-12 13:20:03 -0300946 memset(&he->stat, 0, sizeof(he->stat));
947 he->hists = hists;
Namhyung Kimce74f602012-12-10 17:29:55 +0900948 rb_link_node(&he->rb_node_in, parent, p);
949 rb_insert_color(&he->rb_node_in, root);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300950 hists__inc_nr_entries(hists, he);
951 }
952out:
953 return he;
954}
955
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300956static struct hist_entry *hists__find_entry(struct hists *hists,
957 struct hist_entry *he)
958{
Namhyung Kimce74f602012-12-10 17:29:55 +0900959 struct rb_node *n;
960
961 if (sort__need_collapse)
962 n = hists->entries_collapsed.rb_node;
963 else
964 n = hists->entries_in->rb_node;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300965
966 while (n) {
Namhyung Kimce74f602012-12-10 17:29:55 +0900967 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
968 int64_t cmp = hist_entry__collapse(iter, he);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300969
970 if (cmp < 0)
971 n = n->rb_left;
972 else if (cmp > 0)
973 n = n->rb_right;
974 else
975 return iter;
976 }
977
978 return NULL;
979}
980
981/*
982 * Look for pairs to link to the leader buckets (hist_entries):
983 */
984void hists__match(struct hists *leader, struct hists *other)
985{
Namhyung Kimce74f602012-12-10 17:29:55 +0900986 struct rb_root *root;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300987 struct rb_node *nd;
988 struct hist_entry *pos, *pair;
989
Namhyung Kimce74f602012-12-10 17:29:55 +0900990 if (sort__need_collapse)
991 root = &leader->entries_collapsed;
992 else
993 root = leader->entries_in;
994
995 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
996 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300997 pair = hists__find_entry(other, pos);
998
999 if (pair)
Namhyung Kim5fa90412012-11-29 15:38:34 +09001000 hist_entry__add_pair(pair, pos);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001001 }
1002}
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001003
1004/*
1005 * Look for entries in the other hists that are not present in the leader, if
1006 * we find them, just add a dummy entry on the leader hists, with period=0,
1007 * nr_events=0, to serve as the list header.
1008 */
1009int hists__link(struct hists *leader, struct hists *other)
1010{
Namhyung Kimce74f602012-12-10 17:29:55 +09001011 struct rb_root *root;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001012 struct rb_node *nd;
1013 struct hist_entry *pos, *pair;
1014
Namhyung Kimce74f602012-12-10 17:29:55 +09001015 if (sort__need_collapse)
1016 root = &other->entries_collapsed;
1017 else
1018 root = other->entries_in;
1019
1020 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
1021 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001022
1023 if (!hist_entry__has_pairs(pos)) {
1024 pair = hists__add_dummy_entry(leader, pos);
1025 if (pair == NULL)
1026 return -1;
Namhyung Kim5fa90412012-11-29 15:38:34 +09001027 hist_entry__add_pair(pos, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001028 }
1029 }
1030
1031 return 0;
1032}