blob: f5b388e50265a97fc277ea3c8ce173bc6100373f [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
John Kacur3d1d07e2009-09-28 15:32:55 +020016struct callchain_param callchain_param = {
17 .mode = CHAIN_GRAPH_REL,
Sam Liaod797fdc2011-06-07 23:49:46 +080018 .min_percent = 0.5,
Andi Kleen99571ab2013-07-18 15:33:57 -070019 .order = ORDER_CALLEE,
20 .key = CCKEY_FUNCTION
John Kacur3d1d07e2009-09-28 15:32:55 +020021};
22
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030023u16 hists__col_len(struct hists *hists, enum hist_column col)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030024{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030025 return hists->col_len[col];
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030026}
27
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030028void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030029{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030030 hists->col_len[col] = len;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030031}
32
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030033bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030034{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030035 if (len > hists__col_len(hists, col)) {
36 hists__set_col_len(hists, col, len);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030037 return true;
38 }
39 return false;
40}
41
Namhyung Kim7ccf4f92012-08-20 13:52:05 +090042void hists__reset_col_len(struct hists *hists)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030043{
44 enum hist_column col;
45
46 for (col = 0; col < HISTC_NR_COLS; ++col)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030047 hists__set_col_len(hists, col, 0);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030048}
49
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010050static void hists__set_unres_dso_col_len(struct hists *hists, int dso)
51{
52 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
53
54 if (hists__col_len(hists, dso) < unresolved_col_width &&
55 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
56 !symbol_conf.dso_list)
57 hists__set_col_len(hists, dso, unresolved_col_width);
58}
59
Namhyung Kim7ccf4f92012-08-20 13:52:05 +090060void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030061{
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010062 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
Stephane Eranian98a3b322013-01-24 16:10:35 +010063 int symlen;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030064 u16 len;
65
Namhyung Kimded19d52013-04-01 20:35:19 +090066 /*
67 * +4 accounts for '[x] ' priv level info
68 * +2 accounts for 0x prefix on raw addresses
69 * +3 accounts for ' y ' symtab origin info
70 */
71 if (h->ms.sym) {
72 symlen = h->ms.sym->namelen + 4;
73 if (verbose)
74 symlen += BITS_PER_LONG / 4 + 2 + 3;
75 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
76 } else {
Stephane Eranian98a3b322013-01-24 16:10:35 +010077 symlen = unresolved_col_width + 4 + 2;
78 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010079 hists__set_unres_dso_col_len(hists, HISTC_DSO);
Stephane Eranian98a3b322013-01-24 16:10:35 +010080 }
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030081
82 len = thread__comm_len(h->thread);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030083 if (hists__new_col_len(hists, HISTC_COMM, len))
84 hists__set_col_len(hists, HISTC_THREAD, len + 6);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030085
86 if (h->ms.map) {
87 len = dso__name_len(h->ms.map->dso);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030088 hists__new_col_len(hists, HISTC_DSO, len);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030089 }
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010090
Namhyung Kimcb993742012-12-27 18:11:42 +090091 if (h->parent)
92 hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen);
93
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010094 if (h->branch_info) {
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010095 if (h->branch_info->from.sym) {
96 symlen = (int)h->branch_info->from.sym->namelen + 4;
Namhyung Kimded19d52013-04-01 20:35:19 +090097 if (verbose)
98 symlen += BITS_PER_LONG / 4 + 2 + 3;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010099 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
100
101 symlen = dso__name_len(h->branch_info->from.map->dso);
102 hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
103 } else {
104 symlen = unresolved_col_width + 4 + 2;
105 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
106 hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
107 }
108
109 if (h->branch_info->to.sym) {
110 symlen = (int)h->branch_info->to.sym->namelen + 4;
Namhyung Kimded19d52013-04-01 20:35:19 +0900111 if (verbose)
112 symlen += BITS_PER_LONG / 4 + 2 + 3;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100113 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
114
115 symlen = dso__name_len(h->branch_info->to.map->dso);
116 hists__new_col_len(hists, HISTC_DSO_TO, symlen);
117 } else {
118 symlen = unresolved_col_width + 4 + 2;
119 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
120 hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
121 }
122 }
Stephane Eranian98a3b322013-01-24 16:10:35 +0100123
124 if (h->mem_info) {
Stephane Eranian98a3b322013-01-24 16:10:35 +0100125 if (h->mem_info->daddr.sym) {
126 symlen = (int)h->mem_info->daddr.sym->namelen + 4
127 + unresolved_col_width + 2;
128 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
129 symlen);
130 } else {
131 symlen = unresolved_col_width + 4 + 2;
132 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
133 symlen);
134 }
135 if (h->mem_info->daddr.map) {
136 symlen = dso__name_len(h->mem_info->daddr.map->dso);
137 hists__new_col_len(hists, HISTC_MEM_DADDR_DSO,
138 symlen);
139 } else {
140 symlen = unresolved_col_width + 4 + 2;
141 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
142 }
143 } else {
144 symlen = unresolved_col_width + 4 + 2;
145 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen);
146 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
147 }
148
149 hists__new_col_len(hists, HISTC_MEM_LOCKED, 6);
150 hists__new_col_len(hists, HISTC_MEM_TLB, 22);
151 hists__new_col_len(hists, HISTC_MEM_SNOOP, 12);
152 hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3);
153 hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12);
154 hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12);
Andi Kleen475eeab2013-09-20 07:40:43 -0700155
156 if (h->transaction)
157 hists__new_col_len(hists, HISTC_TRANSACTION,
158 hist_entry__transaction_len());
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300159}
160
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900161void hists__output_recalc_col_len(struct hists *hists, int max_rows)
162{
163 struct rb_node *next = rb_first(&hists->entries);
164 struct hist_entry *n;
165 int row = 0;
166
167 hists__reset_col_len(hists);
168
169 while (next && row++ < max_rows) {
170 n = rb_entry(next, struct hist_entry, rb_node);
171 if (!n->filtered)
172 hists__calc_col_len(hists, n);
173 next = rb_next(&n->rb_node);
174 }
175}
176
Namhyung Kimf39056f2014-01-14 14:25:37 +0900177static void he_stat__add_cpumode_period(struct he_stat *he_stat,
178 unsigned int cpumode, u64 period)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800179{
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300180 switch (cpumode) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800181 case PERF_RECORD_MISC_KERNEL:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900182 he_stat->period_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800183 break;
184 case PERF_RECORD_MISC_USER:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900185 he_stat->period_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800186 break;
187 case PERF_RECORD_MISC_GUEST_KERNEL:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900188 he_stat->period_guest_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800189 break;
190 case PERF_RECORD_MISC_GUEST_USER:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900191 he_stat->period_guest_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800192 break;
193 default:
194 break;
195 }
196}
197
Andi Kleen05484292013-01-24 16:10:29 +0100198static void he_stat__add_period(struct he_stat *he_stat, u64 period,
199 u64 weight)
Namhyung Kim139c0812012-10-04 21:49:43 +0900200{
Stephane Eranian98a3b322013-01-24 16:10:35 +0100201
Namhyung Kim139c0812012-10-04 21:49:43 +0900202 he_stat->period += period;
Andi Kleen05484292013-01-24 16:10:29 +0100203 he_stat->weight += weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900204 he_stat->nr_events += 1;
205}
206
207static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
208{
209 dest->period += src->period;
210 dest->period_sys += src->period_sys;
211 dest->period_us += src->period_us;
212 dest->period_guest_sys += src->period_guest_sys;
213 dest->period_guest_us += src->period_guest_us;
214 dest->nr_events += src->nr_events;
Andi Kleen05484292013-01-24 16:10:29 +0100215 dest->weight += src->weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900216}
217
Namhyung Kimf39056f2014-01-14 14:25:37 +0900218static void he_stat__decay(struct he_stat *he_stat)
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300219{
Namhyung Kimf39056f2014-01-14 14:25:37 +0900220 he_stat->period = (he_stat->period * 7) / 8;
221 he_stat->nr_events = (he_stat->nr_events * 7) / 8;
Andi Kleen05484292013-01-24 16:10:29 +0100222 /* XXX need decay for weight too? */
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300223}
224
225static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
226{
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900227 u64 prev_period = he->stat.period;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200228
229 if (prev_period == 0)
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300230 return true;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200231
Namhyung Kimf39056f2014-01-14 14:25:37 +0900232 he_stat__decay(&he->stat);
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200233
234 if (!he->filtered)
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900235 hists->stats.total_period -= prev_period - he->stat.period;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200236
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900237 return he->stat.period == 0;
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300238}
239
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900240void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300241{
242 struct rb_node *next = rb_first(&hists->entries);
243 struct hist_entry *n;
244
245 while (next) {
246 n = rb_entry(next, struct hist_entry, rb_node);
247 next = rb_next(&n->rb_node);
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300248 /*
249 * We may be annotating this, for instance, so keep it here in
250 * case some it gets new samples, we'll eventually free it when
251 * the user stops browsing and it agains gets fully decayed.
252 */
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200253 if (((zap_user && n->level == '.') ||
254 (zap_kernel && n->level != '.') ||
255 hists__decay_entry(hists, n)) &&
256 !n->used) {
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300257 rb_erase(&n->rb_node, &hists->entries);
258
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900259 if (sort__need_collapse)
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300260 rb_erase(&n->rb_node_in, &hists->entries_collapsed);
261
262 hist_entry__free(n);
263 --hists->nr_entries;
264 }
265 }
266}
267
John Kacur3d1d07e2009-09-28 15:32:55 +0200268/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300269 * histogram, sorted on item, collects periods
John Kacur3d1d07e2009-09-28 15:32:55 +0200270 */
271
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300272static struct hist_entry *hist_entry__new(struct hist_entry *template)
273{
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +0200274 size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0;
Stephane Eranian98a3b322013-01-24 16:10:35 +0100275 struct hist_entry *he = zalloc(sizeof(*he) + callchain_size);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300276
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200277 if (he != NULL) {
278 *he = *template;
Namhyung Kimc4b35352012-10-04 21:49:42 +0900279
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200280 if (he->ms.map)
281 he->ms.map->referenced = true;
Stephane Eranian3cf0cb12013-01-14 15:02:45 +0100282
283 if (he->branch_info) {
Namhyung Kim26353a62013-04-01 20:35:17 +0900284 /*
285 * This branch info is (a part of) allocated from
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -0300286 * sample__resolve_bstack() and will be freed after
Namhyung Kim26353a62013-04-01 20:35:17 +0900287 * adding new entries. So we need to save a copy.
288 */
289 he->branch_info = malloc(sizeof(*he->branch_info));
290 if (he->branch_info == NULL) {
291 free(he);
292 return NULL;
293 }
294
295 memcpy(he->branch_info, template->branch_info,
296 sizeof(*he->branch_info));
297
Stephane Eranian3cf0cb12013-01-14 15:02:45 +0100298 if (he->branch_info->from.map)
299 he->branch_info->from.map->referenced = true;
300 if (he->branch_info->to.map)
301 he->branch_info->to.map->referenced = true;
302 }
303
Stephane Eranian98a3b322013-01-24 16:10:35 +0100304 if (he->mem_info) {
305 if (he->mem_info->iaddr.map)
306 he->mem_info->iaddr.map->referenced = true;
307 if (he->mem_info->daddr.map)
308 he->mem_info->daddr.map->referenced = true;
309 }
310
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300311 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200312 callchain_init(he->callchain);
Arnaldo Carvalho de Melob821c732012-10-25 14:42:45 -0200313
314 INIT_LIST_HEAD(&he->pairs.node);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300315 }
316
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200317 return he;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300318}
319
Namhyung Kim62638352014-04-24 16:21:46 +0900320void hists__inc_stats(struct hists *hists, struct hist_entry *h)
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300321{
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300322 if (!h->filtered) {
Namhyung Kimf2148332014-01-14 11:52:48 +0900323 hists->nr_non_filtered_entries++;
324 hists->stats.total_non_filtered_period += h->stat.period;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300325 }
Namhyung Kimf2148332014-01-14 11:52:48 +0900326 hists->nr_entries++;
327 hists->stats.total_period += h->stat.period;
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300328}
329
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300330static u8 symbol__parent_filter(const struct symbol *parent)
331{
332 if (symbol_conf.exclude_other && parent == NULL)
333 return 1 << HIST_FILTER__PARENT;
334 return 0;
335}
336
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100337static struct hist_entry *add_hist_entry(struct hists *hists,
Namhyung Kimf1cbf782013-12-18 14:21:11 +0900338 struct hist_entry *entry,
339 struct addr_location *al)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300340{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300341 struct rb_node **p;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300342 struct rb_node *parent = NULL;
343 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -0700344 int64_t cmp;
Namhyung Kimf1cbf782013-12-18 14:21:11 +0900345 u64 period = entry->stat.period;
346 u64 weight = entry->stat.weight;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300347
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300348 p = &hists->entries_in->rb_node;
349
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300350 while (*p != NULL) {
351 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300352 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300353
Namhyung Kim9afcf932012-12-10 17:29:54 +0900354 /*
355 * Make sure that it receives arguments in a same order as
356 * hist_entry__collapse() so that we can use an appropriate
357 * function when searching an entry regardless which sort
358 * keys were used.
359 */
360 cmp = hist_entry__cmp(he, entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300361
362 if (!cmp) {
Andi Kleen05484292013-01-24 16:10:29 +0100363 he_stat__add_period(&he->stat, period, weight);
David Miller63fa4712012-03-27 03:14:18 -0400364
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900365 /*
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -0300366 * This mem info was allocated from sample__resolve_mem
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900367 * and will not be used anymore.
368 */
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300369 zfree(&entry->mem_info);
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900370
David Miller63fa4712012-03-27 03:14:18 -0400371 /* If the map of an existing hist_entry has
372 * become out-of-date due to an exec() or
373 * similar, update it. Otherwise we will
374 * mis-adjust symbol addresses when computing
375 * the history counter to increment.
376 */
377 if (he->ms.map != entry->ms.map) {
378 he->ms.map = entry->ms.map;
379 if (he->ms.map)
380 he->ms.map->referenced = true;
381 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300382 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300383 }
384
385 if (cmp < 0)
386 p = &(*p)->rb_left;
387 else
388 p = &(*p)->rb_right;
389 }
390
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100391 he = hist_entry__new(entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300392 if (!he)
Namhyung Kim27a0dcb2013-05-14 11:09:02 +0900393 return NULL;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300394
Namhyung Kimc1fb5652013-10-11 14:15:38 +0900395 hists->nr_entries++;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300396 rb_link_node(&he->rb_node_in, parent, p);
397 rb_insert_color(&he->rb_node_in, hists->entries_in);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300398out:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900399 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300400 return he;
401}
402
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300403struct hist_entry *__hists__add_entry(struct hists *hists,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100404 struct addr_location *al,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900405 struct symbol *sym_parent,
406 struct branch_info *bi,
407 struct mem_info *mi,
408 u64 period, u64 weight, u64 transaction)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100409{
410 struct hist_entry entry = {
411 .thread = al->thread,
Namhyung Kim4dfced32013-09-13 16:28:57 +0900412 .comm = thread__comm(al->thread),
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100413 .ms = {
414 .map = al->map,
415 .sym = al->sym,
416 },
417 .cpu = al->cpu,
418 .ip = al->addr,
419 .level = al->level,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900420 .stat = {
Namhyung Kimc4b35352012-10-04 21:49:42 +0900421 .nr_events = 1,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900422 .period = period,
Andi Kleen05484292013-01-24 16:10:29 +0100423 .weight = weight,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900424 },
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100425 .parent = sym_parent,
Namhyung Kim2c86c7c2014-03-17 18:18:54 -0300426 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300427 .hists = hists,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900428 .branch_info = bi,
429 .mem_info = mi,
Andi Kleen475eeab2013-09-20 07:40:43 -0700430 .transaction = transaction,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100431 };
432
Namhyung Kimf1cbf782013-12-18 14:21:11 +0900433 return add_hist_entry(hists, &entry, al);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100434}
435
John Kacur3d1d07e2009-09-28 15:32:55 +0200436int64_t
437hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
438{
439 struct sort_entry *se;
440 int64_t cmp = 0;
441
442 list_for_each_entry(se, &hist_entry__sort_list, list) {
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200443 cmp = se->se_cmp(left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200444 if (cmp)
445 break;
446 }
447
448 return cmp;
449}
450
451int64_t
452hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
453{
454 struct sort_entry *se;
455 int64_t cmp = 0;
456
457 list_for_each_entry(se, &hist_entry__sort_list, list) {
458 int64_t (*f)(struct hist_entry *, struct hist_entry *);
459
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200460 f = se->se_collapse ?: se->se_cmp;
John Kacur3d1d07e2009-09-28 15:32:55 +0200461
462 cmp = f(left, right);
463 if (cmp)
464 break;
465 }
466
467 return cmp;
468}
469
470void hist_entry__free(struct hist_entry *he)
471{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300472 zfree(&he->branch_info);
473 zfree(&he->mem_info);
Namhyung Kimf048d542013-09-11 14:09:28 +0900474 free_srcline(he->srcline);
John Kacur3d1d07e2009-09-28 15:32:55 +0200475 free(he);
476}
477
478/*
479 * collapse the histogram
480 */
481
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300482static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100483 struct rb_root *root,
484 struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +0200485{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200486 struct rb_node **p = &root->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200487 struct rb_node *parent = NULL;
488 struct hist_entry *iter;
489 int64_t cmp;
490
491 while (*p != NULL) {
492 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300493 iter = rb_entry(parent, struct hist_entry, rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +0200494
495 cmp = hist_entry__collapse(iter, he);
496
497 if (!cmp) {
Namhyung Kim139c0812012-10-04 21:49:43 +0900498 he_stat__add_stat(&iter->stat, &he->stat);
Namhyung Kim9ec60972012-09-26 16:47:28 +0900499
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100500 if (symbol_conf.use_callchain) {
Namhyung Kim47260642012-05-31 14:43:26 +0900501 callchain_cursor_reset(&callchain_cursor);
502 callchain_merge(&callchain_cursor,
503 iter->callchain,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100504 he->callchain);
505 }
John Kacur3d1d07e2009-09-28 15:32:55 +0200506 hist_entry__free(he);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300507 return false;
John Kacur3d1d07e2009-09-28 15:32:55 +0200508 }
509
510 if (cmp < 0)
511 p = &(*p)->rb_left;
512 else
513 p = &(*p)->rb_right;
514 }
515
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300516 rb_link_node(&he->rb_node_in, parent, p);
517 rb_insert_color(&he->rb_node_in, root);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300518 return true;
John Kacur3d1d07e2009-09-28 15:32:55 +0200519}
520
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300521static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
522{
523 struct rb_root *root;
524
525 pthread_mutex_lock(&hists->lock);
526
527 root = hists->entries_in;
528 if (++hists->entries_in > &hists->entries_in_array[1])
529 hists->entries_in = &hists->entries_in_array[0];
530
531 pthread_mutex_unlock(&hists->lock);
532
533 return root;
534}
535
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200536static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
537{
538 hists__filter_entry_by_dso(hists, he);
539 hists__filter_entry_by_thread(hists, he);
Namhyung Kime94d53e2012-03-16 17:50:51 +0900540 hists__filter_entry_by_symbol(hists, he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200541}
542
Namhyung Kimc1fb5652013-10-11 14:15:38 +0900543void hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300544{
545 struct rb_root *root;
546 struct rb_node *next;
547 struct hist_entry *n;
548
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900549 if (!sort__need_collapse)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300550 return;
551
552 root = hists__get_rotate_entries_in(hists);
553 next = rb_first(root);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300554
555 while (next) {
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -0300556 if (session_done())
557 break;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300558 n = rb_entry(next, struct hist_entry, rb_node_in);
559 next = rb_next(&n->rb_node_in);
560
561 rb_erase(&n->rb_node_in, root);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200562 if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
563 /*
564 * If it wasn't combined with one of the entries already
565 * collapsed, we need to apply the filters that may have
566 * been set by, say, the hist_browser.
567 */
568 hists__apply_filters(hists, n);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200569 }
Namhyung Kimc1fb5652013-10-11 14:15:38 +0900570 if (prog)
571 ui_progress__update(prog, 1);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300572 }
573}
574
John Kacur3d1d07e2009-09-28 15:32:55 +0200575/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300576 * reverse the map, sort on period.
John Kacur3d1d07e2009-09-28 15:32:55 +0200577 */
578
Namhyung Kim29d720e2013-01-22 18:09:33 +0900579static int period_cmp(u64 period_a, u64 period_b)
580{
581 if (period_a > period_b)
582 return 1;
583 if (period_a < period_b)
584 return -1;
585 return 0;
586}
587
588static int hist_entry__sort_on_period(struct hist_entry *a,
589 struct hist_entry *b)
590{
591 int ret;
592 int i, nr_members;
593 struct perf_evsel *evsel;
594 struct hist_entry *pair;
595 u64 *periods_a, *periods_b;
596
597 ret = period_cmp(a->stat.period, b->stat.period);
598 if (ret || !symbol_conf.event_group)
599 return ret;
600
601 evsel = hists_to_evsel(a->hists);
602 nr_members = evsel->nr_members;
603 if (nr_members <= 1)
604 return ret;
605
606 periods_a = zalloc(sizeof(periods_a) * nr_members);
607 periods_b = zalloc(sizeof(periods_b) * nr_members);
608
609 if (!periods_a || !periods_b)
610 goto out;
611
612 list_for_each_entry(pair, &a->pairs.head, pairs.node) {
613 evsel = hists_to_evsel(pair->hists);
614 periods_a[perf_evsel__group_idx(evsel)] = pair->stat.period;
615 }
616
617 list_for_each_entry(pair, &b->pairs.head, pairs.node) {
618 evsel = hists_to_evsel(pair->hists);
619 periods_b[perf_evsel__group_idx(evsel)] = pair->stat.period;
620 }
621
622 for (i = 1; i < nr_members; i++) {
623 ret = period_cmp(periods_a[i], periods_b[i]);
624 if (ret)
625 break;
626 }
627
628out:
629 free(periods_a);
630 free(periods_b);
631
632 return ret;
633}
634
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300635static void __hists__insert_output_entry(struct rb_root *entries,
636 struct hist_entry *he,
637 u64 min_callchain_hits)
John Kacur3d1d07e2009-09-28 15:32:55 +0200638{
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300639 struct rb_node **p = &entries->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200640 struct rb_node *parent = NULL;
641 struct hist_entry *iter;
642
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200643 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melob9fb9302010-04-02 09:50:42 -0300644 callchain_param.sort(&he->sorted_chain, he->callchain,
John Kacur3d1d07e2009-09-28 15:32:55 +0200645 min_callchain_hits, &callchain_param);
646
647 while (*p != NULL) {
648 parent = *p;
649 iter = rb_entry(parent, struct hist_entry, rb_node);
650
Namhyung Kim29d720e2013-01-22 18:09:33 +0900651 if (hist_entry__sort_on_period(he, iter) > 0)
John Kacur3d1d07e2009-09-28 15:32:55 +0200652 p = &(*p)->rb_left;
653 else
654 p = &(*p)->rb_right;
655 }
656
657 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300658 rb_insert_color(&he->rb_node, entries);
John Kacur3d1d07e2009-09-28 15:32:55 +0200659}
660
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900661void hists__output_resort(struct hists *hists)
John Kacur3d1d07e2009-09-28 15:32:55 +0200662{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300663 struct rb_root *root;
John Kacur3d1d07e2009-09-28 15:32:55 +0200664 struct rb_node *next;
665 struct hist_entry *n;
John Kacur3d1d07e2009-09-28 15:32:55 +0200666 u64 min_callchain_hits;
667
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300668 min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
John Kacur3d1d07e2009-09-28 15:32:55 +0200669
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900670 if (sort__need_collapse)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300671 root = &hists->entries_collapsed;
672 else
673 root = hists->entries_in;
674
675 next = rb_first(root);
676 hists->entries = RB_ROOT;
John Kacur3d1d07e2009-09-28 15:32:55 +0200677
Namhyung Kimf2148332014-01-14 11:52:48 +0900678 hists->nr_non_filtered_entries = 0;
679 hists->stats.total_period = 0;
680 hists->stats.total_non_filtered_period = 0;
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300681 hists__reset_col_len(hists);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300682
John Kacur3d1d07e2009-09-28 15:32:55 +0200683 while (next) {
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300684 n = rb_entry(next, struct hist_entry, rb_node_in);
685 next = rb_next(&n->rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +0200686
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300687 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
Namhyung Kim62638352014-04-24 16:21:46 +0900688 hists__inc_stats(hists, n);
Namhyung Kimae993ef2014-04-24 16:25:19 +0900689
690 if (!n->filtered)
691 hists__calc_col_len(hists, n);
John Kacur3d1d07e2009-09-28 15:32:55 +0200692 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300693}
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200694
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300695static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300696 enum hist_filter filter)
697{
698 h->filtered &= ~(1 << filter);
699 if (h->filtered)
700 return;
701
Namhyung Kim1ab1fa52013-12-26 15:11:52 +0900702 ++hists->nr_non_filtered_entries;
Namhyung Kimf2148332014-01-14 11:52:48 +0900703 if (h->ms.unfolded)
Namhyung Kim1ab1fa52013-12-26 15:11:52 +0900704 hists->nr_non_filtered_entries += h->nr_rows;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -0300705 h->row_offset = 0;
Namhyung Kim1ab1fa52013-12-26 15:11:52 +0900706 hists->stats.total_non_filtered_period += h->stat.period;
Namhyung Kim1ab1fa52013-12-26 15:11:52 +0900707 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300708
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300709 hists__calc_col_len(hists, h);
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300710}
711
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200712
713static bool hists__filter_entry_by_dso(struct hists *hists,
714 struct hist_entry *he)
715{
716 if (hists->dso_filter != NULL &&
717 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
718 he->filtered |= (1 << HIST_FILTER__DSO);
719 return true;
720 }
721
722 return false;
723}
724
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200725void hists__filter_by_dso(struct hists *hists)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300726{
727 struct rb_node *nd;
728
Namhyung Kimf2148332014-01-14 11:52:48 +0900729 hists->nr_non_filtered_entries = 0;
730 hists->stats.total_non_filtered_period = 0;
Namhyung Kim1ab1fa52013-12-26 15:11:52 +0900731 hists->stats.nr_non_filtered_samples = 0;
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300732 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300733
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300734 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300735 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
736
737 if (symbol_conf.exclude_other && !h->parent)
738 continue;
739
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200740 if (hists__filter_entry_by_dso(hists, h))
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300741 continue;
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300742
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300743 hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300744 }
745}
746
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200747static bool hists__filter_entry_by_thread(struct hists *hists,
748 struct hist_entry *he)
749{
750 if (hists->thread_filter != NULL &&
751 he->thread != hists->thread_filter) {
752 he->filtered |= (1 << HIST_FILTER__THREAD);
753 return true;
754 }
755
756 return false;
757}
758
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200759void hists__filter_by_thread(struct hists *hists)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300760{
761 struct rb_node *nd;
762
Namhyung Kimf2148332014-01-14 11:52:48 +0900763 hists->nr_non_filtered_entries = 0;
764 hists->stats.total_non_filtered_period = 0;
Namhyung Kim1ab1fa52013-12-26 15:11:52 +0900765 hists->stats.nr_non_filtered_samples = 0;
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300766 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
Namhyung Kimf2148332014-01-14 11:52:48 +0900795 hists->nr_non_filtered_entries = 0;
796 hists->stats.total_non_filtered_period = 0;
Namhyung Kim1ab1fa52013-12-26 15:11:52 +0900797 hists->stats.nr_non_filtered_samples = 0;
Namhyung Kime94d53e2012-03-16 17:50:51 +0900798 hists__reset_col_len(hists);
799
800 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
801 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
802
803 if (hists__filter_entry_by_symbol(hists, h))
804 continue;
805
806 hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
807 }
808}
809
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -0300810void events_stats__inc(struct events_stats *stats, u32 type)
811{
812 ++stats->nr_events[0];
813 ++stats->nr_events[type];
814}
815
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300816void hists__inc_nr_events(struct hists *hists, u32 type)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -0300817{
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -0300818 events_stats__inc(&hists->stats, type);
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -0300819}
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300820
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300821static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
822 struct hist_entry *pair)
823{
Namhyung Kimce74f602012-12-10 17:29:55 +0900824 struct rb_root *root;
825 struct rb_node **p;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300826 struct rb_node *parent = NULL;
827 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -0700828 int64_t cmp;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300829
Namhyung Kimce74f602012-12-10 17:29:55 +0900830 if (sort__need_collapse)
831 root = &hists->entries_collapsed;
832 else
833 root = hists->entries_in;
834
835 p = &root->rb_node;
836
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300837 while (*p != NULL) {
838 parent = *p;
Namhyung Kimce74f602012-12-10 17:29:55 +0900839 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300840
Namhyung Kimce74f602012-12-10 17:29:55 +0900841 cmp = hist_entry__collapse(he, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300842
843 if (!cmp)
844 goto out;
845
846 if (cmp < 0)
847 p = &(*p)->rb_left;
848 else
849 p = &(*p)->rb_right;
850 }
851
852 he = hist_entry__new(pair);
853 if (he) {
Arnaldo Carvalho de Melo30193d72012-11-12 13:20:03 -0300854 memset(&he->stat, 0, sizeof(he->stat));
855 he->hists = hists;
Namhyung Kimce74f602012-12-10 17:29:55 +0900856 rb_link_node(&he->rb_node_in, parent, p);
857 rb_insert_color(&he->rb_node_in, root);
Namhyung Kim62638352014-04-24 16:21:46 +0900858 hists__inc_stats(hists, he);
Jiri Olsae0af43d2012-12-01 21:18:20 +0100859 he->dummy = true;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300860 }
861out:
862 return he;
863}
864
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300865static struct hist_entry *hists__find_entry(struct hists *hists,
866 struct hist_entry *he)
867{
Namhyung Kimce74f602012-12-10 17:29:55 +0900868 struct rb_node *n;
869
870 if (sort__need_collapse)
871 n = hists->entries_collapsed.rb_node;
872 else
873 n = hists->entries_in->rb_node;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300874
875 while (n) {
Namhyung Kimce74f602012-12-10 17:29:55 +0900876 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
877 int64_t cmp = hist_entry__collapse(iter, he);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300878
879 if (cmp < 0)
880 n = n->rb_left;
881 else if (cmp > 0)
882 n = n->rb_right;
883 else
884 return iter;
885 }
886
887 return NULL;
888}
889
890/*
891 * Look for pairs to link to the leader buckets (hist_entries):
892 */
893void hists__match(struct hists *leader, struct hists *other)
894{
Namhyung Kimce74f602012-12-10 17:29:55 +0900895 struct rb_root *root;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300896 struct rb_node *nd;
897 struct hist_entry *pos, *pair;
898
Namhyung Kimce74f602012-12-10 17:29:55 +0900899 if (sort__need_collapse)
900 root = &leader->entries_collapsed;
901 else
902 root = leader->entries_in;
903
904 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
905 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300906 pair = hists__find_entry(other, pos);
907
908 if (pair)
Namhyung Kim5fa90412012-11-29 15:38:34 +0900909 hist_entry__add_pair(pair, pos);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300910 }
911}
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300912
913/*
914 * Look for entries in the other hists that are not present in the leader, if
915 * we find them, just add a dummy entry on the leader hists, with period=0,
916 * nr_events=0, to serve as the list header.
917 */
918int hists__link(struct hists *leader, struct hists *other)
919{
Namhyung Kimce74f602012-12-10 17:29:55 +0900920 struct rb_root *root;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300921 struct rb_node *nd;
922 struct hist_entry *pos, *pair;
923
Namhyung Kimce74f602012-12-10 17:29:55 +0900924 if (sort__need_collapse)
925 root = &other->entries_collapsed;
926 else
927 root = other->entries_in;
928
929 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
930 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300931
932 if (!hist_entry__has_pairs(pos)) {
933 pair = hists__add_dummy_entry(leader, pos);
934 if (pair == NULL)
935 return -1;
Namhyung Kim5fa90412012-11-29 15:38:34 +0900936 hist_entry__add_pair(pos, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300937 }
938 }
939
940 return 0;
941}
Namhyung Kimf2148332014-01-14 11:52:48 +0900942
943u64 hists__total_period(struct hists *hists)
944{
945 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
946 hists->stats.total_period;
947}
Namhyung Kim33db4562014-02-07 12:06:07 +0900948
949int parse_filter_percentage(const struct option *opt __maybe_unused,
950 const char *arg, int unset __maybe_unused)
951{
952 if (!strcmp(arg, "relative"))
953 symbol_conf.filter_relative = true;
954 else if (!strcmp(arg, "absolute"))
955 symbol_conf.filter_relative = false;
956 else
957 return -1;
958
959 return 0;
960}
Namhyung Kim0b93da12014-01-14 12:02:15 +0900961
962int perf_hist_config(const char *var, const char *value)
963{
964 if (!strcmp(var, "hist.percentage"))
965 return parse_filter_percentage(NULL, value, 0);
966
967 return 0;
968}