blob: 30df6187ee026acf0fb9f12013fbde891d3bdd70 [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"
Namhyung Kim69bcb012013-10-30 09:40:34 +09007#include "annotate.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
John Kacur3d1d07e2009-09-28 15:32:55 +020017struct callchain_param callchain_param = {
18 .mode = CHAIN_GRAPH_REL,
Sam Liaod797fdc2011-06-07 23:49:46 +080019 .min_percent = 0.5,
Andi Kleen99571ab2013-07-18 15:33:57 -070020 .order = ORDER_CALLEE,
21 .key = CCKEY_FUNCTION
John Kacur3d1d07e2009-09-28 15:32:55 +020022};
23
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030024u16 hists__col_len(struct hists *hists, enum hist_column col)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030025{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030026 return hists->col_len[col];
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030027}
28
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030029void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030030{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030031 hists->col_len[col] = len;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030032}
33
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030034bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030035{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030036 if (len > hists__col_len(hists, col)) {
37 hists__set_col_len(hists, col, len);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030038 return true;
39 }
40 return false;
41}
42
Namhyung Kim7ccf4f92012-08-20 13:52:05 +090043void hists__reset_col_len(struct hists *hists)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030044{
45 enum hist_column col;
46
47 for (col = 0; col < HISTC_NR_COLS; ++col)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030048 hists__set_col_len(hists, col, 0);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030049}
50
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010051static void hists__set_unres_dso_col_len(struct hists *hists, int dso)
52{
53 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
54
55 if (hists__col_len(hists, dso) < unresolved_col_width &&
56 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
57 !symbol_conf.dso_list)
58 hists__set_col_len(hists, dso, unresolved_col_width);
59}
60
Namhyung Kim7ccf4f92012-08-20 13:52:05 +090061void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030062{
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010063 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
Stephane Eranian98a3b322013-01-24 16:10:35 +010064 int symlen;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030065 u16 len;
66
Namhyung Kimded19d52013-04-01 20:35:19 +090067 /*
68 * +4 accounts for '[x] ' priv level info
69 * +2 accounts for 0x prefix on raw addresses
70 * +3 accounts for ' y ' symtab origin info
71 */
72 if (h->ms.sym) {
73 symlen = h->ms.sym->namelen + 4;
74 if (verbose)
75 symlen += BITS_PER_LONG / 4 + 2 + 3;
76 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
77 } else {
Stephane Eranian98a3b322013-01-24 16:10:35 +010078 symlen = unresolved_col_width + 4 + 2;
79 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010080 hists__set_unres_dso_col_len(hists, HISTC_DSO);
Stephane Eranian98a3b322013-01-24 16:10:35 +010081 }
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030082
83 len = thread__comm_len(h->thread);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030084 if (hists__new_col_len(hists, HISTC_COMM, len))
85 hists__set_col_len(hists, HISTC_THREAD, len + 6);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030086
87 if (h->ms.map) {
88 len = dso__name_len(h->ms.map->dso);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030089 hists__new_col_len(hists, HISTC_DSO, len);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030090 }
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010091
Namhyung Kimcb993742012-12-27 18:11:42 +090092 if (h->parent)
93 hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen);
94
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010095 if (h->branch_info) {
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010096 if (h->branch_info->from.sym) {
97 symlen = (int)h->branch_info->from.sym->namelen + 4;
Namhyung Kimded19d52013-04-01 20:35:19 +090098 if (verbose)
99 symlen += BITS_PER_LONG / 4 + 2 + 3;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100100 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
101
102 symlen = dso__name_len(h->branch_info->from.map->dso);
103 hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
104 } else {
105 symlen = unresolved_col_width + 4 + 2;
106 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
107 hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
108 }
109
110 if (h->branch_info->to.sym) {
111 symlen = (int)h->branch_info->to.sym->namelen + 4;
Namhyung Kimded19d52013-04-01 20:35:19 +0900112 if (verbose)
113 symlen += BITS_PER_LONG / 4 + 2 + 3;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100114 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
115
116 symlen = dso__name_len(h->branch_info->to.map->dso);
117 hists__new_col_len(hists, HISTC_DSO_TO, symlen);
118 } else {
119 symlen = unresolved_col_width + 4 + 2;
120 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
121 hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
122 }
123 }
Stephane Eranian98a3b322013-01-24 16:10:35 +0100124
125 if (h->mem_info) {
Stephane Eranian98a3b322013-01-24 16:10:35 +0100126 if (h->mem_info->daddr.sym) {
127 symlen = (int)h->mem_info->daddr.sym->namelen + 4
128 + unresolved_col_width + 2;
129 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
130 symlen);
Don Zickus9b32ba72014-06-01 15:38:29 +0200131 hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
132 symlen + 1);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100133 } else {
134 symlen = unresolved_col_width + 4 + 2;
135 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
136 symlen);
137 }
138 if (h->mem_info->daddr.map) {
139 symlen = dso__name_len(h->mem_info->daddr.map->dso);
140 hists__new_col_len(hists, HISTC_MEM_DADDR_DSO,
141 symlen);
142 } else {
143 symlen = unresolved_col_width + 4 + 2;
144 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
145 }
146 } else {
147 symlen = unresolved_col_width + 4 + 2;
148 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen);
149 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
150 }
151
152 hists__new_col_len(hists, HISTC_MEM_LOCKED, 6);
153 hists__new_col_len(hists, HISTC_MEM_TLB, 22);
154 hists__new_col_len(hists, HISTC_MEM_SNOOP, 12);
155 hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3);
156 hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12);
157 hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12);
Andi Kleen475eeab2013-09-20 07:40:43 -0700158
159 if (h->transaction)
160 hists__new_col_len(hists, HISTC_TRANSACTION,
161 hist_entry__transaction_len());
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
Namhyung Kimf39056f2014-01-14 14:25:37 +0900180static void he_stat__add_cpumode_period(struct he_stat *he_stat,
181 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 Kimf39056f2014-01-14 14:25:37 +0900185 he_stat->period_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800186 break;
187 case PERF_RECORD_MISC_USER:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900188 he_stat->period_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800189 break;
190 case PERF_RECORD_MISC_GUEST_KERNEL:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900191 he_stat->period_guest_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800192 break;
193 case PERF_RECORD_MISC_GUEST_USER:
Namhyung Kimf39056f2014-01-14 14:25:37 +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
Namhyung Kimf39056f2014-01-14 14:25:37 +0900221static void he_stat__decay(struct he_stat *he_stat)
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300222{
Namhyung Kimf39056f2014-01-14 14:25:37 +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;
Namhyung Kim3186b682014-04-22 13:44:23 +0900231 u64 diff;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200232
233 if (prev_period == 0)
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300234 return true;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200235
Namhyung Kimf39056f2014-01-14 14:25:37 +0900236 he_stat__decay(&he->stat);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900237 if (symbol_conf.cumulate_callchain)
238 he_stat__decay(he->stat_acc);
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200239
Namhyung Kim3186b682014-04-22 13:44:23 +0900240 diff = prev_period - he->stat.period;
241
242 hists->stats.total_period -= diff;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200243 if (!he->filtered)
Namhyung Kim3186b682014-04-22 13:44:23 +0900244 hists->stats.total_non_filtered_period -= diff;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200245
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900246 return he->stat.period == 0;
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300247}
248
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900249void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300250{
251 struct rb_node *next = rb_first(&hists->entries);
252 struct hist_entry *n;
253
254 while (next) {
255 n = rb_entry(next, struct hist_entry, rb_node);
256 next = rb_next(&n->rb_node);
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300257 /*
258 * We may be annotating this, for instance, so keep it here in
259 * case some it gets new samples, we'll eventually free it when
260 * the user stops browsing and it agains gets fully decayed.
261 */
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200262 if (((zap_user && n->level == '.') ||
263 (zap_kernel && n->level != '.') ||
264 hists__decay_entry(hists, n)) &&
265 !n->used) {
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300266 rb_erase(&n->rb_node, &hists->entries);
267
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900268 if (sort__need_collapse)
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300269 rb_erase(&n->rb_node_in, &hists->entries_collapsed);
270
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300271 --hists->nr_entries;
Namhyung Kim3186b682014-04-22 13:44:23 +0900272 if (!n->filtered)
273 --hists->nr_non_filtered_entries;
274
275 hist_entry__free(n);
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300276 }
277 }
278}
279
John Kacur3d1d07e2009-09-28 15:32:55 +0200280/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300281 * histogram, sorted on item, collects periods
John Kacur3d1d07e2009-09-28 15:32:55 +0200282 */
283
Namhyung Kima0b51af2012-09-11 13:34:27 +0900284static struct hist_entry *hist_entry__new(struct hist_entry *template,
285 bool sample_self)
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300286{
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900287 size_t callchain_size = 0;
288 struct hist_entry *he;
289
290 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain)
291 callchain_size = sizeof(struct callchain_root);
292
293 he = zalloc(sizeof(*he) + callchain_size);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300294
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200295 if (he != NULL) {
296 *he = *template;
Namhyung Kimc4b35352012-10-04 21:49:42 +0900297
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900298 if (symbol_conf.cumulate_callchain) {
299 he->stat_acc = malloc(sizeof(he->stat));
300 if (he->stat_acc == NULL) {
301 free(he);
302 return NULL;
303 }
304 memcpy(he->stat_acc, &he->stat, sizeof(he->stat));
Namhyung Kima0b51af2012-09-11 13:34:27 +0900305 if (!sample_self)
306 memset(&he->stat, 0, sizeof(he->stat));
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900307 }
308
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200309 if (he->ms.map)
310 he->ms.map->referenced = true;
Stephane Eranian3cf0cb12013-01-14 15:02:45 +0100311
312 if (he->branch_info) {
Namhyung Kim26353a62013-04-01 20:35:17 +0900313 /*
314 * This branch info is (a part of) allocated from
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -0300315 * sample__resolve_bstack() and will be freed after
Namhyung Kim26353a62013-04-01 20:35:17 +0900316 * adding new entries. So we need to save a copy.
317 */
318 he->branch_info = malloc(sizeof(*he->branch_info));
319 if (he->branch_info == NULL) {
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900320 free(he->stat_acc);
Namhyung Kim26353a62013-04-01 20:35:17 +0900321 free(he);
322 return NULL;
323 }
324
325 memcpy(he->branch_info, template->branch_info,
326 sizeof(*he->branch_info));
327
Stephane Eranian3cf0cb12013-01-14 15:02:45 +0100328 if (he->branch_info->from.map)
329 he->branch_info->from.map->referenced = true;
330 if (he->branch_info->to.map)
331 he->branch_info->to.map->referenced = true;
332 }
333
Stephane Eranian98a3b322013-01-24 16:10:35 +0100334 if (he->mem_info) {
335 if (he->mem_info->iaddr.map)
336 he->mem_info->iaddr.map->referenced = true;
337 if (he->mem_info->daddr.map)
338 he->mem_info->daddr.map->referenced = true;
339 }
340
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300341 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200342 callchain_init(he->callchain);
Arnaldo Carvalho de Melob821c732012-10-25 14:42:45 -0200343
344 INIT_LIST_HEAD(&he->pairs.node);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300345 }
346
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200347 return he;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300348}
349
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300350static u8 symbol__parent_filter(const struct symbol *parent)
351{
352 if (symbol_conf.exclude_other && parent == NULL)
353 return 1 << HIST_FILTER__PARENT;
354 return 0;
355}
356
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100357static struct hist_entry *add_hist_entry(struct hists *hists,
Namhyung Kimf1cbf782013-12-18 14:21:11 +0900358 struct hist_entry *entry,
Namhyung Kima0b51af2012-09-11 13:34:27 +0900359 struct addr_location *al,
360 bool sample_self)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300361{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300362 struct rb_node **p;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300363 struct rb_node *parent = NULL;
364 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -0700365 int64_t cmp;
Namhyung Kimf1cbf782013-12-18 14:21:11 +0900366 u64 period = entry->stat.period;
367 u64 weight = entry->stat.weight;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300368
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300369 p = &hists->entries_in->rb_node;
370
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300371 while (*p != NULL) {
372 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300373 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300374
Namhyung Kim9afcf932012-12-10 17:29:54 +0900375 /*
376 * Make sure that it receives arguments in a same order as
377 * hist_entry__collapse() so that we can use an appropriate
378 * function when searching an entry regardless which sort
379 * keys were used.
380 */
381 cmp = hist_entry__cmp(he, entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300382
383 if (!cmp) {
Namhyung Kima0b51af2012-09-11 13:34:27 +0900384 if (sample_self)
385 he_stat__add_period(&he->stat, period, weight);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900386 if (symbol_conf.cumulate_callchain)
387 he_stat__add_period(he->stat_acc, period, weight);
David Miller63fa4712012-03-27 03:14:18 -0400388
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900389 /*
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -0300390 * This mem info was allocated from sample__resolve_mem
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900391 * and will not be used anymore.
392 */
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300393 zfree(&entry->mem_info);
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900394
David Miller63fa4712012-03-27 03:14:18 -0400395 /* If the map of an existing hist_entry has
396 * become out-of-date due to an exec() or
397 * similar, update it. Otherwise we will
398 * mis-adjust symbol addresses when computing
399 * the history counter to increment.
400 */
401 if (he->ms.map != entry->ms.map) {
402 he->ms.map = entry->ms.map;
403 if (he->ms.map)
404 he->ms.map->referenced = true;
405 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300406 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300407 }
408
409 if (cmp < 0)
410 p = &(*p)->rb_left;
411 else
412 p = &(*p)->rb_right;
413 }
414
Namhyung Kima0b51af2012-09-11 13:34:27 +0900415 he = hist_entry__new(entry, sample_self);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300416 if (!he)
Namhyung Kim27a0dcb2013-05-14 11:09:02 +0900417 return NULL;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300418
419 rb_link_node(&he->rb_node_in, parent, p);
420 rb_insert_color(&he->rb_node_in, hists->entries_in);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300421out:
Namhyung Kima0b51af2012-09-11 13:34:27 +0900422 if (sample_self)
423 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900424 if (symbol_conf.cumulate_callchain)
425 he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300426 return he;
427}
428
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300429struct hist_entry *__hists__add_entry(struct hists *hists,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100430 struct addr_location *al,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900431 struct symbol *sym_parent,
432 struct branch_info *bi,
433 struct mem_info *mi,
Namhyung Kima0b51af2012-09-11 13:34:27 +0900434 u64 period, u64 weight, u64 transaction,
435 bool sample_self)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100436{
437 struct hist_entry entry = {
438 .thread = al->thread,
Namhyung Kim4dfced32013-09-13 16:28:57 +0900439 .comm = thread__comm(al->thread),
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100440 .ms = {
441 .map = al->map,
442 .sym = al->sym,
443 },
Don Zickus7365be52014-05-27 12:28:05 -0400444 .cpu = al->cpu,
445 .cpumode = al->cpumode,
446 .ip = al->addr,
447 .level = al->level,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900448 .stat = {
Namhyung Kimc4b35352012-10-04 21:49:42 +0900449 .nr_events = 1,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900450 .period = period,
Andi Kleen05484292013-01-24 16:10:29 +0100451 .weight = weight,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900452 },
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100453 .parent = sym_parent,
Namhyung Kim2c86c7c2014-03-17 18:18:54 -0300454 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300455 .hists = hists,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900456 .branch_info = bi,
457 .mem_info = mi,
Andi Kleen475eeab2013-09-20 07:40:43 -0700458 .transaction = transaction,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100459 };
460
Namhyung Kima0b51af2012-09-11 13:34:27 +0900461 return add_hist_entry(hists, &entry, al, sample_self);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100462}
463
Namhyung Kim69bcb012013-10-30 09:40:34 +0900464static int
465iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
466 struct addr_location *al __maybe_unused)
467{
468 return 0;
469}
470
471static int
472iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
473 struct addr_location *al __maybe_unused)
474{
475 return 0;
476}
477
478static int
479iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
480{
481 struct perf_sample *sample = iter->sample;
482 struct mem_info *mi;
483
484 mi = sample__resolve_mem(sample, al);
485 if (mi == NULL)
486 return -ENOMEM;
487
488 iter->priv = mi;
489 return 0;
490}
491
492static int
493iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
494{
495 u64 cost;
496 struct mem_info *mi = iter->priv;
497 struct hist_entry *he;
498
499 if (mi == NULL)
500 return -EINVAL;
501
502 cost = iter->sample->weight;
503 if (!cost)
504 cost = 1;
505
506 /*
507 * must pass period=weight in order to get the correct
508 * sorting from hists__collapse_resort() which is solely
509 * based on periods. We want sorting be done on nr_events * weight
510 * and this is indirectly achieved by passing period=weight here
511 * and the he_stat__add_period() function.
512 */
513 he = __hists__add_entry(&iter->evsel->hists, al, iter->parent, NULL, mi,
Namhyung Kima0b51af2012-09-11 13:34:27 +0900514 cost, cost, 0, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900515 if (!he)
516 return -ENOMEM;
517
518 iter->he = he;
519 return 0;
520}
521
522static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900523iter_finish_mem_entry(struct hist_entry_iter *iter,
524 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900525{
526 struct perf_evsel *evsel = iter->evsel;
527 struct hist_entry *he = iter->he;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900528 int err = -EINVAL;
529
530 if (he == NULL)
531 goto out;
532
Namhyung Kim69bcb012013-10-30 09:40:34 +0900533 hists__inc_nr_samples(&evsel->hists, he->filtered);
534
535 err = hist_entry__append_callchain(he, iter->sample);
536
537out:
538 /*
539 * We don't need to free iter->priv (mem_info) here since
540 * the mem info was either already freed in add_hist_entry() or
541 * passed to a new hist entry by hist_entry__new().
542 */
543 iter->priv = NULL;
544
545 iter->he = NULL;
546 return err;
547}
548
549static int
550iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
551{
552 struct branch_info *bi;
553 struct perf_sample *sample = iter->sample;
554
555 bi = sample__resolve_bstack(sample, al);
556 if (!bi)
557 return -ENOMEM;
558
559 iter->curr = 0;
560 iter->total = sample->branch_stack->nr;
561
562 iter->priv = bi;
563 return 0;
564}
565
566static int
567iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused,
568 struct addr_location *al __maybe_unused)
569{
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900570 /* to avoid calling callback function */
571 iter->he = NULL;
572
Namhyung Kim69bcb012013-10-30 09:40:34 +0900573 return 0;
574}
575
576static int
577iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
578{
579 struct branch_info *bi = iter->priv;
580 int i = iter->curr;
581
582 if (bi == NULL)
583 return 0;
584
585 if (iter->curr >= iter->total)
586 return 0;
587
588 al->map = bi[i].to.map;
589 al->sym = bi[i].to.sym;
590 al->addr = bi[i].to.addr;
591 return 1;
592}
593
594static int
595iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
596{
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900597 struct branch_info *bi;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900598 struct perf_evsel *evsel = iter->evsel;
599 struct hist_entry *he = NULL;
600 int i = iter->curr;
601 int err = 0;
602
603 bi = iter->priv;
604
605 if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym))
606 goto out;
607
608 /*
609 * The report shows the percentage of total branches captured
610 * and not events sampled. Thus we use a pseudo period of 1.
611 */
612 he = __hists__add_entry(&evsel->hists, al, iter->parent, &bi[i], NULL,
Namhyung Kima0b51af2012-09-11 13:34:27 +0900613 1, 1, 0, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900614 if (he == NULL)
615 return -ENOMEM;
616
Namhyung Kim69bcb012013-10-30 09:40:34 +0900617 hists__inc_nr_samples(&evsel->hists, he->filtered);
618
619out:
620 iter->he = he;
621 iter->curr++;
622 return err;
623}
624
625static int
626iter_finish_branch_entry(struct hist_entry_iter *iter,
627 struct addr_location *al __maybe_unused)
628{
629 zfree(&iter->priv);
630 iter->he = NULL;
631
632 return iter->curr >= iter->total ? 0 : -1;
633}
634
635static int
636iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
637 struct addr_location *al __maybe_unused)
638{
639 return 0;
640}
641
642static int
643iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
644{
645 struct perf_evsel *evsel = iter->evsel;
646 struct perf_sample *sample = iter->sample;
647 struct hist_entry *he;
648
649 he = __hists__add_entry(&evsel->hists, al, iter->parent, NULL, NULL,
650 sample->period, sample->weight,
Namhyung Kima0b51af2012-09-11 13:34:27 +0900651 sample->transaction, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900652 if (he == NULL)
653 return -ENOMEM;
654
655 iter->he = he;
656 return 0;
657}
658
659static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900660iter_finish_normal_entry(struct hist_entry_iter *iter,
661 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900662{
Namhyung Kim69bcb012013-10-30 09:40:34 +0900663 struct hist_entry *he = iter->he;
664 struct perf_evsel *evsel = iter->evsel;
665 struct perf_sample *sample = iter->sample;
666
667 if (he == NULL)
668 return 0;
669
670 iter->he = NULL;
671
Namhyung Kim69bcb012013-10-30 09:40:34 +0900672 hists__inc_nr_samples(&evsel->hists, he->filtered);
673
674 return hist_entry__append_callchain(he, sample);
675}
676
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900677static int
678iter_prepare_cumulative_entry(struct hist_entry_iter *iter __maybe_unused,
679 struct addr_location *al __maybe_unused)
680{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900681 struct hist_entry **he_cache;
682
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900683 callchain_cursor_commit(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900684
685 /*
686 * This is for detecting cycles or recursions so that they're
687 * cumulated only one time to prevent entries more than 100%
688 * overhead.
689 */
690 he_cache = malloc(sizeof(*he_cache) * (PERF_MAX_STACK_DEPTH + 1));
691 if (he_cache == NULL)
692 return -ENOMEM;
693
694 iter->priv = he_cache;
695 iter->curr = 0;
696
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900697 return 0;
698}
699
700static int
701iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
702 struct addr_location *al)
703{
704 struct perf_evsel *evsel = iter->evsel;
705 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900706 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900707 struct hist_entry *he;
708 int err = 0;
709
710 he = __hists__add_entry(&evsel->hists, al, iter->parent, NULL, NULL,
711 sample->period, sample->weight,
712 sample->transaction, true);
713 if (he == NULL)
714 return -ENOMEM;
715
716 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900717 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900718
Namhyung Kimbe7f8552013-12-26 17:44:10 +0900719 callchain_append(he->callchain, &callchain_cursor, sample->period);
720
721 /*
722 * We need to re-initialize the cursor since callchain_append()
723 * advanced the cursor to the end.
724 */
725 callchain_cursor_commit(&callchain_cursor);
726
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900727 hists__inc_nr_samples(&evsel->hists, he->filtered);
728
729 return err;
730}
731
732static int
733iter_next_cumulative_entry(struct hist_entry_iter *iter,
734 struct addr_location *al)
735{
736 struct callchain_cursor_node *node;
737
738 node = callchain_cursor_current(&callchain_cursor);
739 if (node == NULL)
740 return 0;
741
Namhyung Kimc7405d82013-10-31 13:58:30 +0900742 return fill_callchain_info(al, node, iter->hide_unresolved);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900743}
744
745static int
746iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
747 struct addr_location *al)
748{
749 struct perf_evsel *evsel = iter->evsel;
750 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900751 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900752 struct hist_entry *he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900753 struct hist_entry he_tmp = {
754 .cpu = al->cpu,
755 .thread = al->thread,
756 .comm = thread__comm(al->thread),
757 .ip = al->addr,
758 .ms = {
759 .map = al->map,
760 .sym = al->sym,
761 },
762 .parent = iter->parent,
763 };
764 int i;
Namhyung Kimbe7f8552013-12-26 17:44:10 +0900765 struct callchain_cursor cursor;
766
767 callchain_cursor_snapshot(&cursor, &callchain_cursor);
768
769 callchain_cursor_advance(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900770
771 /*
772 * Check if there's duplicate entries in the callchain.
773 * It's possible that it has cycles or recursive calls.
774 */
775 for (i = 0; i < iter->curr; i++) {
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900776 if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
777 /* to avoid calling callback function */
778 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900779 return 0;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900780 }
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900781 }
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900782
783 he = __hists__add_entry(&evsel->hists, al, iter->parent, NULL, NULL,
784 sample->period, sample->weight,
785 sample->transaction, false);
786 if (he == NULL)
787 return -ENOMEM;
788
789 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900790 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900791
Namhyung Kimbe7f8552013-12-26 17:44:10 +0900792 callchain_append(he->callchain, &cursor, sample->period);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900793 return 0;
794}
795
796static int
797iter_finish_cumulative_entry(struct hist_entry_iter *iter,
798 struct addr_location *al __maybe_unused)
799{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900800 zfree(&iter->priv);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900801 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900802
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900803 return 0;
804}
805
Namhyung Kim69bcb012013-10-30 09:40:34 +0900806const struct hist_iter_ops hist_iter_mem = {
807 .prepare_entry = iter_prepare_mem_entry,
808 .add_single_entry = iter_add_single_mem_entry,
809 .next_entry = iter_next_nop_entry,
810 .add_next_entry = iter_add_next_nop_entry,
811 .finish_entry = iter_finish_mem_entry,
812};
813
814const struct hist_iter_ops hist_iter_branch = {
815 .prepare_entry = iter_prepare_branch_entry,
816 .add_single_entry = iter_add_single_branch_entry,
817 .next_entry = iter_next_branch_entry,
818 .add_next_entry = iter_add_next_branch_entry,
819 .finish_entry = iter_finish_branch_entry,
820};
821
822const struct hist_iter_ops hist_iter_normal = {
823 .prepare_entry = iter_prepare_normal_entry,
824 .add_single_entry = iter_add_single_normal_entry,
825 .next_entry = iter_next_nop_entry,
826 .add_next_entry = iter_add_next_nop_entry,
827 .finish_entry = iter_finish_normal_entry,
828};
829
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900830const struct hist_iter_ops hist_iter_cumulative = {
831 .prepare_entry = iter_prepare_cumulative_entry,
832 .add_single_entry = iter_add_single_cumulative_entry,
833 .next_entry = iter_next_cumulative_entry,
834 .add_next_entry = iter_add_next_cumulative_entry,
835 .finish_entry = iter_finish_cumulative_entry,
836};
837
Namhyung Kim69bcb012013-10-30 09:40:34 +0900838int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
839 struct perf_evsel *evsel, struct perf_sample *sample,
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900840 int max_stack_depth, void *arg)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900841{
842 int err, err2;
843
844 err = sample__resolve_callchain(sample, &iter->parent, evsel, al,
845 max_stack_depth);
846 if (err)
847 return err;
848
849 iter->evsel = evsel;
850 iter->sample = sample;
851
852 err = iter->ops->prepare_entry(iter, al);
853 if (err)
854 goto out;
855
856 err = iter->ops->add_single_entry(iter, al);
857 if (err)
858 goto out;
859
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900860 if (iter->he && iter->add_entry_cb) {
861 err = iter->add_entry_cb(iter, al, true, arg);
862 if (err)
863 goto out;
864 }
865
Namhyung Kim69bcb012013-10-30 09:40:34 +0900866 while (iter->ops->next_entry(iter, al)) {
867 err = iter->ops->add_next_entry(iter, al);
868 if (err)
869 break;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900870
871 if (iter->he && iter->add_entry_cb) {
872 err = iter->add_entry_cb(iter, al, false, arg);
873 if (err)
874 goto out;
875 }
Namhyung Kim69bcb012013-10-30 09:40:34 +0900876 }
877
878out:
879 err2 = iter->ops->finish_entry(iter, al);
880 if (!err)
881 err = err2;
882
883 return err;
884}
885
John Kacur3d1d07e2009-09-28 15:32:55 +0200886int64_t
887hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
888{
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900889 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +0200890 int64_t cmp = 0;
891
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900892 perf_hpp__for_each_sort_list(fmt) {
Namhyung Kime67d49a2014-03-18 13:00:59 +0900893 if (perf_hpp__should_skip(fmt))
894 continue;
895
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900896 cmp = fmt->cmp(left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200897 if (cmp)
898 break;
899 }
900
901 return cmp;
902}
903
904int64_t
905hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
906{
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900907 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +0200908 int64_t cmp = 0;
909
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900910 perf_hpp__for_each_sort_list(fmt) {
Namhyung Kime67d49a2014-03-18 13:00:59 +0900911 if (perf_hpp__should_skip(fmt))
912 continue;
913
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900914 cmp = fmt->collapse(left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200915 if (cmp)
916 break;
917 }
918
919 return cmp;
920}
921
922void hist_entry__free(struct hist_entry *he)
923{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300924 zfree(&he->branch_info);
925 zfree(&he->mem_info);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900926 zfree(&he->stat_acc);
Namhyung Kimf048d542013-09-11 14:09:28 +0900927 free_srcline(he->srcline);
John Kacur3d1d07e2009-09-28 15:32:55 +0200928 free(he);
929}
930
931/*
932 * collapse the histogram
933 */
934
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300935static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100936 struct rb_root *root,
937 struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +0200938{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200939 struct rb_node **p = &root->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200940 struct rb_node *parent = NULL;
941 struct hist_entry *iter;
942 int64_t cmp;
943
944 while (*p != NULL) {
945 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300946 iter = rb_entry(parent, struct hist_entry, rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +0200947
948 cmp = hist_entry__collapse(iter, he);
949
950 if (!cmp) {
Namhyung Kim139c0812012-10-04 21:49:43 +0900951 he_stat__add_stat(&iter->stat, &he->stat);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900952 if (symbol_conf.cumulate_callchain)
953 he_stat__add_stat(iter->stat_acc, he->stat_acc);
Namhyung Kim9ec60972012-09-26 16:47:28 +0900954
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100955 if (symbol_conf.use_callchain) {
Namhyung Kim47260642012-05-31 14:43:26 +0900956 callchain_cursor_reset(&callchain_cursor);
957 callchain_merge(&callchain_cursor,
958 iter->callchain,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100959 he->callchain);
960 }
John Kacur3d1d07e2009-09-28 15:32:55 +0200961 hist_entry__free(he);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300962 return false;
John Kacur3d1d07e2009-09-28 15:32:55 +0200963 }
964
965 if (cmp < 0)
966 p = &(*p)->rb_left;
967 else
968 p = &(*p)->rb_right;
969 }
970
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300971 rb_link_node(&he->rb_node_in, parent, p);
972 rb_insert_color(&he->rb_node_in, root);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300973 return true;
John Kacur3d1d07e2009-09-28 15:32:55 +0200974}
975
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300976static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
977{
978 struct rb_root *root;
979
980 pthread_mutex_lock(&hists->lock);
981
982 root = hists->entries_in;
983 if (++hists->entries_in > &hists->entries_in_array[1])
984 hists->entries_in = &hists->entries_in_array[0];
985
986 pthread_mutex_unlock(&hists->lock);
987
988 return root;
989}
990
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200991static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
992{
993 hists__filter_entry_by_dso(hists, he);
994 hists__filter_entry_by_thread(hists, he);
Namhyung Kime94d53e2012-03-16 17:50:51 +0900995 hists__filter_entry_by_symbol(hists, he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200996}
997
Namhyung Kimc1fb5652013-10-11 14:15:38 +0900998void hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300999{
1000 struct rb_root *root;
1001 struct rb_node *next;
1002 struct hist_entry *n;
1003
Namhyung Kim3a5714f2013-05-14 11:09:01 +09001004 if (!sort__need_collapse)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001005 return;
1006
1007 root = hists__get_rotate_entries_in(hists);
1008 next = rb_first(root);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001009
1010 while (next) {
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001011 if (session_done())
1012 break;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001013 n = rb_entry(next, struct hist_entry, rb_node_in);
1014 next = rb_next(&n->rb_node_in);
1015
1016 rb_erase(&n->rb_node_in, root);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001017 if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
1018 /*
1019 * If it wasn't combined with one of the entries already
1020 * collapsed, we need to apply the filters that may have
1021 * been set by, say, the hist_browser.
1022 */
1023 hists__apply_filters(hists, n);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001024 }
Namhyung Kimc1fb5652013-10-11 14:15:38 +09001025 if (prog)
1026 ui_progress__update(prog, 1);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001027 }
1028}
1029
Namhyung Kim043ca3892014-03-03 14:18:00 +09001030static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001031{
Namhyung Kim043ca3892014-03-03 14:18:00 +09001032 struct perf_hpp_fmt *fmt;
1033 int64_t cmp = 0;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001034
Namhyung Kim26d8b332014-03-03 16:16:20 +09001035 perf_hpp__for_each_sort_list(fmt) {
Namhyung Kime67d49a2014-03-18 13:00:59 +09001036 if (perf_hpp__should_skip(fmt))
1037 continue;
1038
Namhyung Kim043ca3892014-03-03 14:18:00 +09001039 cmp = fmt->sort(a, b);
1040 if (cmp)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001041 break;
1042 }
1043
Namhyung Kim043ca3892014-03-03 14:18:00 +09001044 return cmp;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001045}
1046
Namhyung Kim9283ba92014-04-24 16:37:26 +09001047static void hists__reset_filter_stats(struct hists *hists)
1048{
1049 hists->nr_non_filtered_entries = 0;
1050 hists->stats.total_non_filtered_period = 0;
1051}
1052
1053void hists__reset_stats(struct hists *hists)
1054{
1055 hists->nr_entries = 0;
1056 hists->stats.total_period = 0;
1057
1058 hists__reset_filter_stats(hists);
1059}
1060
1061static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
1062{
1063 hists->nr_non_filtered_entries++;
1064 hists->stats.total_non_filtered_period += h->stat.period;
1065}
1066
1067void hists__inc_stats(struct hists *hists, struct hist_entry *h)
1068{
1069 if (!h->filtered)
1070 hists__inc_filter_stats(hists, h);
1071
1072 hists->nr_entries++;
1073 hists->stats.total_period += h->stat.period;
1074}
1075
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001076static void __hists__insert_output_entry(struct rb_root *entries,
1077 struct hist_entry *he,
1078 u64 min_callchain_hits)
John Kacur3d1d07e2009-09-28 15:32:55 +02001079{
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001080 struct rb_node **p = &entries->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +02001081 struct rb_node *parent = NULL;
1082 struct hist_entry *iter;
1083
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -02001084 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melob9fb9302010-04-02 09:50:42 -03001085 callchain_param.sort(&he->sorted_chain, he->callchain,
John Kacur3d1d07e2009-09-28 15:32:55 +02001086 min_callchain_hits, &callchain_param);
1087
1088 while (*p != NULL) {
1089 parent = *p;
1090 iter = rb_entry(parent, struct hist_entry, rb_node);
1091
Namhyung Kim043ca3892014-03-03 14:18:00 +09001092 if (hist_entry__sort(he, iter) > 0)
John Kacur3d1d07e2009-09-28 15:32:55 +02001093 p = &(*p)->rb_left;
1094 else
1095 p = &(*p)->rb_right;
1096 }
1097
1098 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001099 rb_insert_color(&he->rb_node, entries);
John Kacur3d1d07e2009-09-28 15:32:55 +02001100}
1101
Namhyung Kim3a5714f2013-05-14 11:09:01 +09001102void hists__output_resort(struct hists *hists)
John Kacur3d1d07e2009-09-28 15:32:55 +02001103{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001104 struct rb_root *root;
John Kacur3d1d07e2009-09-28 15:32:55 +02001105 struct rb_node *next;
1106 struct hist_entry *n;
John Kacur3d1d07e2009-09-28 15:32:55 +02001107 u64 min_callchain_hits;
1108
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001109 min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
John Kacur3d1d07e2009-09-28 15:32:55 +02001110
Namhyung Kim3a5714f2013-05-14 11:09:01 +09001111 if (sort__need_collapse)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001112 root = &hists->entries_collapsed;
1113 else
1114 root = hists->entries_in;
1115
1116 next = rb_first(root);
1117 hists->entries = RB_ROOT;
John Kacur3d1d07e2009-09-28 15:32:55 +02001118
Namhyung Kim9283ba92014-04-24 16:37:26 +09001119 hists__reset_stats(hists);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001120 hists__reset_col_len(hists);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -03001121
John Kacur3d1d07e2009-09-28 15:32:55 +02001122 while (next) {
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001123 n = rb_entry(next, struct hist_entry, rb_node_in);
1124 next = rb_next(&n->rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +02001125
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001126 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
Namhyung Kim62638352014-04-24 16:21:46 +09001127 hists__inc_stats(hists, n);
Namhyung Kimae993ef2014-04-24 16:25:19 +09001128
1129 if (!n->filtered)
1130 hists__calc_col_len(hists, n);
John Kacur3d1d07e2009-09-28 15:32:55 +02001131 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001132}
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -02001133
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001134static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001135 enum hist_filter filter)
1136{
1137 h->filtered &= ~(1 << filter);
1138 if (h->filtered)
1139 return;
1140
Namhyung Kim87e90f42014-04-24 16:44:16 +09001141 /* force fold unfiltered entry for simplicity */
1142 h->ms.unfolded = false;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -03001143 h->row_offset = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001144
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001145 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001146
Namhyung Kim9283ba92014-04-24 16:37:26 +09001147 hists__inc_filter_stats(hists, h);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001148 hists__calc_col_len(hists, h);
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001149}
1150
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001151
1152static bool hists__filter_entry_by_dso(struct hists *hists,
1153 struct hist_entry *he)
1154{
1155 if (hists->dso_filter != NULL &&
1156 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
1157 he->filtered |= (1 << HIST_FILTER__DSO);
1158 return true;
1159 }
1160
1161 return false;
1162}
1163
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001164void hists__filter_by_dso(struct hists *hists)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001165{
1166 struct rb_node *nd;
1167
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001168 hists->stats.nr_non_filtered_samples = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001169
1170 hists__reset_filter_stats(hists);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001171 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001172
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001173 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001174 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1175
1176 if (symbol_conf.exclude_other && !h->parent)
1177 continue;
1178
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001179 if (hists__filter_entry_by_dso(hists, h))
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001180 continue;
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001181
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001182 hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001183 }
1184}
1185
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001186static bool hists__filter_entry_by_thread(struct hists *hists,
1187 struct hist_entry *he)
1188{
1189 if (hists->thread_filter != NULL &&
1190 he->thread != hists->thread_filter) {
1191 he->filtered |= (1 << HIST_FILTER__THREAD);
1192 return true;
1193 }
1194
1195 return false;
1196}
1197
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001198void hists__filter_by_thread(struct hists *hists)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001199{
1200 struct rb_node *nd;
1201
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001202 hists->stats.nr_non_filtered_samples = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001203
1204 hists__reset_filter_stats(hists);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001205 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001206
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001207 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001208 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1209
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001210 if (hists__filter_entry_by_thread(hists, h))
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001211 continue;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001212
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001213 hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001214 }
1215}
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001216
Namhyung Kime94d53e2012-03-16 17:50:51 +09001217static bool hists__filter_entry_by_symbol(struct hists *hists,
1218 struct hist_entry *he)
1219{
1220 if (hists->symbol_filter_str != NULL &&
1221 (!he->ms.sym || strstr(he->ms.sym->name,
1222 hists->symbol_filter_str) == NULL)) {
1223 he->filtered |= (1 << HIST_FILTER__SYMBOL);
1224 return true;
1225 }
1226
1227 return false;
1228}
1229
1230void hists__filter_by_symbol(struct hists *hists)
1231{
1232 struct rb_node *nd;
1233
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001234 hists->stats.nr_non_filtered_samples = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001235
1236 hists__reset_filter_stats(hists);
Namhyung Kime94d53e2012-03-16 17:50:51 +09001237 hists__reset_col_len(hists);
1238
1239 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1240 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1241
1242 if (hists__filter_entry_by_symbol(hists, h))
1243 continue;
1244
1245 hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
1246 }
1247}
1248
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001249void events_stats__inc(struct events_stats *stats, u32 type)
1250{
1251 ++stats->nr_events[0];
1252 ++stats->nr_events[type];
1253}
1254
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001255void hists__inc_nr_events(struct hists *hists, u32 type)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001256{
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001257 events_stats__inc(&hists->stats, type);
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001258}
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001259
Namhyung Kim1844dbc2014-05-28 14:12:18 +09001260void hists__inc_nr_samples(struct hists *hists, bool filtered)
1261{
1262 events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
1263 if (!filtered)
1264 hists->stats.nr_non_filtered_samples++;
1265}
1266
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001267static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
1268 struct hist_entry *pair)
1269{
Namhyung Kimce74f602012-12-10 17:29:55 +09001270 struct rb_root *root;
1271 struct rb_node **p;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001272 struct rb_node *parent = NULL;
1273 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -07001274 int64_t cmp;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001275
Namhyung Kimce74f602012-12-10 17:29:55 +09001276 if (sort__need_collapse)
1277 root = &hists->entries_collapsed;
1278 else
1279 root = hists->entries_in;
1280
1281 p = &root->rb_node;
1282
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001283 while (*p != NULL) {
1284 parent = *p;
Namhyung Kimce74f602012-12-10 17:29:55 +09001285 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001286
Namhyung Kimce74f602012-12-10 17:29:55 +09001287 cmp = hist_entry__collapse(he, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001288
1289 if (!cmp)
1290 goto out;
1291
1292 if (cmp < 0)
1293 p = &(*p)->rb_left;
1294 else
1295 p = &(*p)->rb_right;
1296 }
1297
Namhyung Kima0b51af2012-09-11 13:34:27 +09001298 he = hist_entry__new(pair, true);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001299 if (he) {
Arnaldo Carvalho de Melo30193d72012-11-12 13:20:03 -03001300 memset(&he->stat, 0, sizeof(he->stat));
1301 he->hists = hists;
Namhyung Kimce74f602012-12-10 17:29:55 +09001302 rb_link_node(&he->rb_node_in, parent, p);
1303 rb_insert_color(&he->rb_node_in, root);
Namhyung Kim62638352014-04-24 16:21:46 +09001304 hists__inc_stats(hists, he);
Jiri Olsae0af43d2012-12-01 21:18:20 +01001305 he->dummy = true;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001306 }
1307out:
1308 return he;
1309}
1310
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001311static struct hist_entry *hists__find_entry(struct hists *hists,
1312 struct hist_entry *he)
1313{
Namhyung Kimce74f602012-12-10 17:29:55 +09001314 struct rb_node *n;
1315
1316 if (sort__need_collapse)
1317 n = hists->entries_collapsed.rb_node;
1318 else
1319 n = hists->entries_in->rb_node;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001320
1321 while (n) {
Namhyung Kimce74f602012-12-10 17:29:55 +09001322 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
1323 int64_t cmp = hist_entry__collapse(iter, he);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001324
1325 if (cmp < 0)
1326 n = n->rb_left;
1327 else if (cmp > 0)
1328 n = n->rb_right;
1329 else
1330 return iter;
1331 }
1332
1333 return NULL;
1334}
1335
1336/*
1337 * Look for pairs to link to the leader buckets (hist_entries):
1338 */
1339void hists__match(struct hists *leader, struct hists *other)
1340{
Namhyung Kimce74f602012-12-10 17:29:55 +09001341 struct rb_root *root;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001342 struct rb_node *nd;
1343 struct hist_entry *pos, *pair;
1344
Namhyung Kimce74f602012-12-10 17:29:55 +09001345 if (sort__need_collapse)
1346 root = &leader->entries_collapsed;
1347 else
1348 root = leader->entries_in;
1349
1350 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
1351 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001352 pair = hists__find_entry(other, pos);
1353
1354 if (pair)
Namhyung Kim5fa90412012-11-29 15:38:34 +09001355 hist_entry__add_pair(pair, pos);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001356 }
1357}
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001358
1359/*
1360 * Look for entries in the other hists that are not present in the leader, if
1361 * we find them, just add a dummy entry on the leader hists, with period=0,
1362 * nr_events=0, to serve as the list header.
1363 */
1364int hists__link(struct hists *leader, struct hists *other)
1365{
Namhyung Kimce74f602012-12-10 17:29:55 +09001366 struct rb_root *root;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001367 struct rb_node *nd;
1368 struct hist_entry *pos, *pair;
1369
Namhyung Kimce74f602012-12-10 17:29:55 +09001370 if (sort__need_collapse)
1371 root = &other->entries_collapsed;
1372 else
1373 root = other->entries_in;
1374
1375 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
1376 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001377
1378 if (!hist_entry__has_pairs(pos)) {
1379 pair = hists__add_dummy_entry(leader, pos);
1380 if (pair == NULL)
1381 return -1;
Namhyung Kim5fa90412012-11-29 15:38:34 +09001382 hist_entry__add_pair(pos, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001383 }
1384 }
1385
1386 return 0;
1387}
Namhyung Kimf2148332014-01-14 11:52:48 +09001388
1389u64 hists__total_period(struct hists *hists)
1390{
1391 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
1392 hists->stats.total_period;
1393}
Namhyung Kim33db4562014-02-07 12:06:07 +09001394
1395int parse_filter_percentage(const struct option *opt __maybe_unused,
1396 const char *arg, int unset __maybe_unused)
1397{
1398 if (!strcmp(arg, "relative"))
1399 symbol_conf.filter_relative = true;
1400 else if (!strcmp(arg, "absolute"))
1401 symbol_conf.filter_relative = false;
1402 else
1403 return -1;
1404
1405 return 0;
1406}
Namhyung Kim0b93da12014-01-14 12:02:15 +09001407
1408int perf_hist_config(const char *var, const char *value)
1409{
1410 if (!strcmp(var, "hist.percentage"))
1411 return parse_filter_percentage(NULL, value, 0);
1412
1413 return 0;
1414}