blob: 7f0236cea4fe5f79c6871bffdf1beb5dcfb52c1c [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;
Namhyung Kim3186b682014-04-22 13:44:23 +0900228 u64 diff;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200229
230 if (prev_period == 0)
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300231 return true;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200232
Namhyung Kimf39056f2014-01-14 14:25:37 +0900233 he_stat__decay(&he->stat);
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200234
Namhyung Kim3186b682014-04-22 13:44:23 +0900235 diff = prev_period - he->stat.period;
236
237 hists->stats.total_period -= diff;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200238 if (!he->filtered)
Namhyung Kim3186b682014-04-22 13:44:23 +0900239 hists->stats.total_non_filtered_period -= diff;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200240
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900241 return he->stat.period == 0;
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300242}
243
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900244void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
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
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900263 if (sort__need_collapse)
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300264 rb_erase(&n->rb_node_in, &hists->entries_collapsed);
265
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300266 --hists->nr_entries;
Namhyung Kim3186b682014-04-22 13:44:23 +0900267 if (!n->filtered)
268 --hists->nr_non_filtered_entries;
269
270 hist_entry__free(n);
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300271 }
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
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -0300293 * sample__resolve_bstack() and will be freed after
Namhyung Kim26353a62013-04-01 20:35:17 +0900294 * 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
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300327static u8 symbol__parent_filter(const struct symbol *parent)
328{
329 if (symbol_conf.exclude_other && parent == NULL)
330 return 1 << HIST_FILTER__PARENT;
331 return 0;
332}
333
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100334static struct hist_entry *add_hist_entry(struct hists *hists,
Namhyung Kimf1cbf782013-12-18 14:21:11 +0900335 struct hist_entry *entry,
336 struct addr_location *al)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300337{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300338 struct rb_node **p;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300339 struct rb_node *parent = NULL;
340 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -0700341 int64_t cmp;
Namhyung Kimf1cbf782013-12-18 14:21:11 +0900342 u64 period = entry->stat.period;
343 u64 weight = entry->stat.weight;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300344
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300345 p = &hists->entries_in->rb_node;
346
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300347 while (*p != NULL) {
348 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300349 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300350
Namhyung Kim9afcf932012-12-10 17:29:54 +0900351 /*
352 * Make sure that it receives arguments in a same order as
353 * hist_entry__collapse() so that we can use an appropriate
354 * function when searching an entry regardless which sort
355 * keys were used.
356 */
357 cmp = hist_entry__cmp(he, entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300358
359 if (!cmp) {
Andi Kleen05484292013-01-24 16:10:29 +0100360 he_stat__add_period(&he->stat, period, weight);
David Miller63fa4712012-03-27 03:14:18 -0400361
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900362 /*
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -0300363 * This mem info was allocated from sample__resolve_mem
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900364 * and will not be used anymore.
365 */
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300366 zfree(&entry->mem_info);
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900367
David Miller63fa4712012-03-27 03:14:18 -0400368 /* If the map of an existing hist_entry has
369 * become out-of-date due to an exec() or
370 * similar, update it. Otherwise we will
371 * mis-adjust symbol addresses when computing
372 * the history counter to increment.
373 */
374 if (he->ms.map != entry->ms.map) {
375 he->ms.map = entry->ms.map;
376 if (he->ms.map)
377 he->ms.map->referenced = true;
378 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300379 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300380 }
381
382 if (cmp < 0)
383 p = &(*p)->rb_left;
384 else
385 p = &(*p)->rb_right;
386 }
387
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100388 he = hist_entry__new(entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300389 if (!he)
Namhyung Kim27a0dcb2013-05-14 11:09:02 +0900390 return NULL;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300391
392 rb_link_node(&he->rb_node_in, parent, p);
393 rb_insert_color(&he->rb_node_in, hists->entries_in);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300394out:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900395 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300396 return he;
397}
398
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300399struct hist_entry *__hists__add_entry(struct hists *hists,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100400 struct addr_location *al,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900401 struct symbol *sym_parent,
402 struct branch_info *bi,
403 struct mem_info *mi,
404 u64 period, u64 weight, u64 transaction)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100405{
406 struct hist_entry entry = {
407 .thread = al->thread,
Namhyung Kim4dfced32013-09-13 16:28:57 +0900408 .comm = thread__comm(al->thread),
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100409 .ms = {
410 .map = al->map,
411 .sym = al->sym,
412 },
413 .cpu = al->cpu,
414 .ip = al->addr,
415 .level = al->level,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900416 .stat = {
Namhyung Kimc4b35352012-10-04 21:49:42 +0900417 .nr_events = 1,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900418 .period = period,
Andi Kleen05484292013-01-24 16:10:29 +0100419 .weight = weight,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900420 },
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100421 .parent = sym_parent,
Namhyung Kim2c86c7c2014-03-17 18:18:54 -0300422 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300423 .hists = hists,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900424 .branch_info = bi,
425 .mem_info = mi,
Andi Kleen475eeab2013-09-20 07:40:43 -0700426 .transaction = transaction,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100427 };
428
Namhyung Kimf1cbf782013-12-18 14:21:11 +0900429 return add_hist_entry(hists, &entry, al);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100430}
431
John Kacur3d1d07e2009-09-28 15:32:55 +0200432int64_t
433hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
434{
435 struct sort_entry *se;
436 int64_t cmp = 0;
437
438 list_for_each_entry(se, &hist_entry__sort_list, list) {
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200439 cmp = se->se_cmp(left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200440 if (cmp)
441 break;
442 }
443
444 return cmp;
445}
446
447int64_t
448hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
449{
450 struct sort_entry *se;
451 int64_t cmp = 0;
452
453 list_for_each_entry(se, &hist_entry__sort_list, list) {
454 int64_t (*f)(struct hist_entry *, struct hist_entry *);
455
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200456 f = se->se_collapse ?: se->se_cmp;
John Kacur3d1d07e2009-09-28 15:32:55 +0200457
458 cmp = f(left, right);
459 if (cmp)
460 break;
461 }
462
463 return cmp;
464}
465
466void hist_entry__free(struct hist_entry *he)
467{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300468 zfree(&he->branch_info);
469 zfree(&he->mem_info);
Namhyung Kimf048d542013-09-11 14:09:28 +0900470 free_srcline(he->srcline);
John Kacur3d1d07e2009-09-28 15:32:55 +0200471 free(he);
472}
473
474/*
475 * collapse the histogram
476 */
477
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300478static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100479 struct rb_root *root,
480 struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +0200481{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200482 struct rb_node **p = &root->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200483 struct rb_node *parent = NULL;
484 struct hist_entry *iter;
485 int64_t cmp;
486
487 while (*p != NULL) {
488 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300489 iter = rb_entry(parent, struct hist_entry, rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +0200490
491 cmp = hist_entry__collapse(iter, he);
492
493 if (!cmp) {
Namhyung Kim139c0812012-10-04 21:49:43 +0900494 he_stat__add_stat(&iter->stat, &he->stat);
Namhyung Kim9ec60972012-09-26 16:47:28 +0900495
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100496 if (symbol_conf.use_callchain) {
Namhyung Kim47260642012-05-31 14:43:26 +0900497 callchain_cursor_reset(&callchain_cursor);
498 callchain_merge(&callchain_cursor,
499 iter->callchain,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100500 he->callchain);
501 }
John Kacur3d1d07e2009-09-28 15:32:55 +0200502 hist_entry__free(he);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300503 return false;
John Kacur3d1d07e2009-09-28 15:32:55 +0200504 }
505
506 if (cmp < 0)
507 p = &(*p)->rb_left;
508 else
509 p = &(*p)->rb_right;
510 }
511
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300512 rb_link_node(&he->rb_node_in, parent, p);
513 rb_insert_color(&he->rb_node_in, root);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300514 return true;
John Kacur3d1d07e2009-09-28 15:32:55 +0200515}
516
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300517static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
518{
519 struct rb_root *root;
520
521 pthread_mutex_lock(&hists->lock);
522
523 root = hists->entries_in;
524 if (++hists->entries_in > &hists->entries_in_array[1])
525 hists->entries_in = &hists->entries_in_array[0];
526
527 pthread_mutex_unlock(&hists->lock);
528
529 return root;
530}
531
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200532static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
533{
534 hists__filter_entry_by_dso(hists, he);
535 hists__filter_entry_by_thread(hists, he);
Namhyung Kime94d53e2012-03-16 17:50:51 +0900536 hists__filter_entry_by_symbol(hists, he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200537}
538
Namhyung Kimc1fb5652013-10-11 14:15:38 +0900539void hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300540{
541 struct rb_root *root;
542 struct rb_node *next;
543 struct hist_entry *n;
544
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900545 if (!sort__need_collapse)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300546 return;
547
548 root = hists__get_rotate_entries_in(hists);
549 next = rb_first(root);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300550
551 while (next) {
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -0300552 if (session_done())
553 break;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300554 n = rb_entry(next, struct hist_entry, rb_node_in);
555 next = rb_next(&n->rb_node_in);
556
557 rb_erase(&n->rb_node_in, root);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200558 if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
559 /*
560 * If it wasn't combined with one of the entries already
561 * collapsed, we need to apply the filters that may have
562 * been set by, say, the hist_browser.
563 */
564 hists__apply_filters(hists, n);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200565 }
Namhyung Kimc1fb5652013-10-11 14:15:38 +0900566 if (prog)
567 ui_progress__update(prog, 1);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300568 }
569}
570
John Kacur3d1d07e2009-09-28 15:32:55 +0200571/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300572 * reverse the map, sort on period.
John Kacur3d1d07e2009-09-28 15:32:55 +0200573 */
574
Namhyung Kim29d720e2013-01-22 18:09:33 +0900575static int period_cmp(u64 period_a, u64 period_b)
576{
577 if (period_a > period_b)
578 return 1;
579 if (period_a < period_b)
580 return -1;
581 return 0;
582}
583
584static int hist_entry__sort_on_period(struct hist_entry *a,
585 struct hist_entry *b)
586{
587 int ret;
588 int i, nr_members;
589 struct perf_evsel *evsel;
590 struct hist_entry *pair;
591 u64 *periods_a, *periods_b;
592
593 ret = period_cmp(a->stat.period, b->stat.period);
594 if (ret || !symbol_conf.event_group)
595 return ret;
596
597 evsel = hists_to_evsel(a->hists);
598 nr_members = evsel->nr_members;
599 if (nr_members <= 1)
600 return ret;
601
602 periods_a = zalloc(sizeof(periods_a) * nr_members);
603 periods_b = zalloc(sizeof(periods_b) * nr_members);
604
605 if (!periods_a || !periods_b)
606 goto out;
607
608 list_for_each_entry(pair, &a->pairs.head, pairs.node) {
609 evsel = hists_to_evsel(pair->hists);
610 periods_a[perf_evsel__group_idx(evsel)] = pair->stat.period;
611 }
612
613 list_for_each_entry(pair, &b->pairs.head, pairs.node) {
614 evsel = hists_to_evsel(pair->hists);
615 periods_b[perf_evsel__group_idx(evsel)] = pair->stat.period;
616 }
617
618 for (i = 1; i < nr_members; i++) {
619 ret = period_cmp(periods_a[i], periods_b[i]);
620 if (ret)
621 break;
622 }
623
624out:
625 free(periods_a);
626 free(periods_b);
627
628 return ret;
629}
630
Namhyung Kim9283ba92014-04-24 16:37:26 +0900631static void hists__reset_filter_stats(struct hists *hists)
632{
633 hists->nr_non_filtered_entries = 0;
634 hists->stats.total_non_filtered_period = 0;
635}
636
637void hists__reset_stats(struct hists *hists)
638{
639 hists->nr_entries = 0;
640 hists->stats.total_period = 0;
641
642 hists__reset_filter_stats(hists);
643}
644
645static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
646{
647 hists->nr_non_filtered_entries++;
648 hists->stats.total_non_filtered_period += h->stat.period;
649}
650
651void hists__inc_stats(struct hists *hists, struct hist_entry *h)
652{
653 if (!h->filtered)
654 hists__inc_filter_stats(hists, h);
655
656 hists->nr_entries++;
657 hists->stats.total_period += h->stat.period;
658}
659
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300660static void __hists__insert_output_entry(struct rb_root *entries,
661 struct hist_entry *he,
662 u64 min_callchain_hits)
John Kacur3d1d07e2009-09-28 15:32:55 +0200663{
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300664 struct rb_node **p = &entries->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200665 struct rb_node *parent = NULL;
666 struct hist_entry *iter;
667
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200668 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melob9fb9302010-04-02 09:50:42 -0300669 callchain_param.sort(&he->sorted_chain, he->callchain,
John Kacur3d1d07e2009-09-28 15:32:55 +0200670 min_callchain_hits, &callchain_param);
671
672 while (*p != NULL) {
673 parent = *p;
674 iter = rb_entry(parent, struct hist_entry, rb_node);
675
Namhyung Kim29d720e2013-01-22 18:09:33 +0900676 if (hist_entry__sort_on_period(he, iter) > 0)
John Kacur3d1d07e2009-09-28 15:32:55 +0200677 p = &(*p)->rb_left;
678 else
679 p = &(*p)->rb_right;
680 }
681
682 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300683 rb_insert_color(&he->rb_node, entries);
John Kacur3d1d07e2009-09-28 15:32:55 +0200684}
685
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900686void hists__output_resort(struct hists *hists)
John Kacur3d1d07e2009-09-28 15:32:55 +0200687{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300688 struct rb_root *root;
John Kacur3d1d07e2009-09-28 15:32:55 +0200689 struct rb_node *next;
690 struct hist_entry *n;
John Kacur3d1d07e2009-09-28 15:32:55 +0200691 u64 min_callchain_hits;
692
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300693 min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
John Kacur3d1d07e2009-09-28 15:32:55 +0200694
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900695 if (sort__need_collapse)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300696 root = &hists->entries_collapsed;
697 else
698 root = hists->entries_in;
699
700 next = rb_first(root);
701 hists->entries = RB_ROOT;
John Kacur3d1d07e2009-09-28 15:32:55 +0200702
Namhyung Kim9283ba92014-04-24 16:37:26 +0900703 hists__reset_stats(hists);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300704 hists__reset_col_len(hists);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300705
John Kacur3d1d07e2009-09-28 15:32:55 +0200706 while (next) {
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300707 n = rb_entry(next, struct hist_entry, rb_node_in);
708 next = rb_next(&n->rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +0200709
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300710 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
Namhyung Kim62638352014-04-24 16:21:46 +0900711 hists__inc_stats(hists, n);
Namhyung Kimae993ef2014-04-24 16:25:19 +0900712
713 if (!n->filtered)
714 hists__calc_col_len(hists, n);
John Kacur3d1d07e2009-09-28 15:32:55 +0200715 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300716}
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200717
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300718static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300719 enum hist_filter filter)
720{
721 h->filtered &= ~(1 << filter);
722 if (h->filtered)
723 return;
724
Namhyung Kim87e90f42014-04-24 16:44:16 +0900725 /* force fold unfiltered entry for simplicity */
726 h->ms.unfolded = false;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -0300727 h->row_offset = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +0900728
Namhyung Kim1ab1fa52013-12-26 15:11:52 +0900729 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300730
Namhyung Kim9283ba92014-04-24 16:37:26 +0900731 hists__inc_filter_stats(hists, h);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300732 hists__calc_col_len(hists, h);
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300733}
734
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200735
736static bool hists__filter_entry_by_dso(struct hists *hists,
737 struct hist_entry *he)
738{
739 if (hists->dso_filter != NULL &&
740 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
741 he->filtered |= (1 << HIST_FILTER__DSO);
742 return true;
743 }
744
745 return false;
746}
747
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200748void hists__filter_by_dso(struct hists *hists)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300749{
750 struct rb_node *nd;
751
Namhyung Kim1ab1fa52013-12-26 15:11:52 +0900752 hists->stats.nr_non_filtered_samples = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +0900753
754 hists__reset_filter_stats(hists);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300755 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300756
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300757 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300758 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
759
760 if (symbol_conf.exclude_other && !h->parent)
761 continue;
762
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200763 if (hists__filter_entry_by_dso(hists, h))
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300764 continue;
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300765
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300766 hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300767 }
768}
769
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200770static bool hists__filter_entry_by_thread(struct hists *hists,
771 struct hist_entry *he)
772{
773 if (hists->thread_filter != NULL &&
774 he->thread != hists->thread_filter) {
775 he->filtered |= (1 << HIST_FILTER__THREAD);
776 return true;
777 }
778
779 return false;
780}
781
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200782void hists__filter_by_thread(struct hists *hists)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300783{
784 struct rb_node *nd;
785
Namhyung Kim1ab1fa52013-12-26 15:11:52 +0900786 hists->stats.nr_non_filtered_samples = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +0900787
788 hists__reset_filter_stats(hists);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300789 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300790
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300791 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300792 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
793
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200794 if (hists__filter_entry_by_thread(hists, h))
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300795 continue;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300796
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300797 hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300798 }
799}
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300800
Namhyung Kime94d53e2012-03-16 17:50:51 +0900801static bool hists__filter_entry_by_symbol(struct hists *hists,
802 struct hist_entry *he)
803{
804 if (hists->symbol_filter_str != NULL &&
805 (!he->ms.sym || strstr(he->ms.sym->name,
806 hists->symbol_filter_str) == NULL)) {
807 he->filtered |= (1 << HIST_FILTER__SYMBOL);
808 return true;
809 }
810
811 return false;
812}
813
814void hists__filter_by_symbol(struct hists *hists)
815{
816 struct rb_node *nd;
817
Namhyung Kim1ab1fa52013-12-26 15:11:52 +0900818 hists->stats.nr_non_filtered_samples = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +0900819
820 hists__reset_filter_stats(hists);
Namhyung Kime94d53e2012-03-16 17:50:51 +0900821 hists__reset_col_len(hists);
822
823 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
824 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
825
826 if (hists__filter_entry_by_symbol(hists, h))
827 continue;
828
829 hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
830 }
831}
832
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -0300833void events_stats__inc(struct events_stats *stats, u32 type)
834{
835 ++stats->nr_events[0];
836 ++stats->nr_events[type];
837}
838
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300839void hists__inc_nr_events(struct hists *hists, u32 type)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -0300840{
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -0300841 events_stats__inc(&hists->stats, type);
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -0300842}
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300843
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300844static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
845 struct hist_entry *pair)
846{
Namhyung Kimce74f602012-12-10 17:29:55 +0900847 struct rb_root *root;
848 struct rb_node **p;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300849 struct rb_node *parent = NULL;
850 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -0700851 int64_t cmp;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300852
Namhyung Kimce74f602012-12-10 17:29:55 +0900853 if (sort__need_collapse)
854 root = &hists->entries_collapsed;
855 else
856 root = hists->entries_in;
857
858 p = &root->rb_node;
859
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300860 while (*p != NULL) {
861 parent = *p;
Namhyung Kimce74f602012-12-10 17:29:55 +0900862 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300863
Namhyung Kimce74f602012-12-10 17:29:55 +0900864 cmp = hist_entry__collapse(he, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300865
866 if (!cmp)
867 goto out;
868
869 if (cmp < 0)
870 p = &(*p)->rb_left;
871 else
872 p = &(*p)->rb_right;
873 }
874
875 he = hist_entry__new(pair);
876 if (he) {
Arnaldo Carvalho de Melo30193d72012-11-12 13:20:03 -0300877 memset(&he->stat, 0, sizeof(he->stat));
878 he->hists = hists;
Namhyung Kimce74f602012-12-10 17:29:55 +0900879 rb_link_node(&he->rb_node_in, parent, p);
880 rb_insert_color(&he->rb_node_in, root);
Namhyung Kim62638352014-04-24 16:21:46 +0900881 hists__inc_stats(hists, he);
Jiri Olsae0af43d2012-12-01 21:18:20 +0100882 he->dummy = true;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300883 }
884out:
885 return he;
886}
887
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300888static struct hist_entry *hists__find_entry(struct hists *hists,
889 struct hist_entry *he)
890{
Namhyung Kimce74f602012-12-10 17:29:55 +0900891 struct rb_node *n;
892
893 if (sort__need_collapse)
894 n = hists->entries_collapsed.rb_node;
895 else
896 n = hists->entries_in->rb_node;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300897
898 while (n) {
Namhyung Kimce74f602012-12-10 17:29:55 +0900899 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
900 int64_t cmp = hist_entry__collapse(iter, he);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300901
902 if (cmp < 0)
903 n = n->rb_left;
904 else if (cmp > 0)
905 n = n->rb_right;
906 else
907 return iter;
908 }
909
910 return NULL;
911}
912
913/*
914 * Look for pairs to link to the leader buckets (hist_entries):
915 */
916void hists__match(struct hists *leader, struct hists *other)
917{
Namhyung Kimce74f602012-12-10 17:29:55 +0900918 struct rb_root *root;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300919 struct rb_node *nd;
920 struct hist_entry *pos, *pair;
921
Namhyung Kimce74f602012-12-10 17:29:55 +0900922 if (sort__need_collapse)
923 root = &leader->entries_collapsed;
924 else
925 root = leader->entries_in;
926
927 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
928 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300929 pair = hists__find_entry(other, pos);
930
931 if (pair)
Namhyung Kim5fa90412012-11-29 15:38:34 +0900932 hist_entry__add_pair(pair, pos);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -0300933 }
934}
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300935
936/*
937 * Look for entries in the other hists that are not present in the leader, if
938 * we find them, just add a dummy entry on the leader hists, with period=0,
939 * nr_events=0, to serve as the list header.
940 */
941int hists__link(struct hists *leader, struct hists *other)
942{
Namhyung Kimce74f602012-12-10 17:29:55 +0900943 struct rb_root *root;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300944 struct rb_node *nd;
945 struct hist_entry *pos, *pair;
946
Namhyung Kimce74f602012-12-10 17:29:55 +0900947 if (sort__need_collapse)
948 root = &other->entries_collapsed;
949 else
950 root = other->entries_in;
951
952 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
953 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300954
955 if (!hist_entry__has_pairs(pos)) {
956 pair = hists__add_dummy_entry(leader, pos);
957 if (pair == NULL)
958 return -1;
Namhyung Kim5fa90412012-11-29 15:38:34 +0900959 hist_entry__add_pair(pos, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -0300960 }
961 }
962
963 return 0;
964}
Namhyung Kimf2148332014-01-14 11:52:48 +0900965
966u64 hists__total_period(struct hists *hists)
967{
968 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
969 hists->stats.total_period;
970}
Namhyung Kim33db4562014-02-07 12:06:07 +0900971
972int parse_filter_percentage(const struct option *opt __maybe_unused,
973 const char *arg, int unset __maybe_unused)
974{
975 if (!strcmp(arg, "relative"))
976 symbol_conf.filter_relative = true;
977 else if (!strcmp(arg, "absolute"))
978 symbol_conf.filter_relative = false;
979 else
980 return -1;
981
982 return 0;
983}
Namhyung Kim0b93da12014-01-14 12:02:15 +0900984
985int perf_hist_config(const char *var, const char *value)
986{
987 if (!strcmp(var, "hist.percentage"))
988 return parse_filter_percentage(NULL, value, 0);
989
990 return 0;
991}