blob: cc22b9158b93c41fd0d44cd451ba189c56bc19dd [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"
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03006#include "evlist.h"
Namhyung Kim29d720e2013-01-22 18:09:33 +09007#include "evsel.h"
Namhyung Kim69bcb012013-10-30 09:40:34 +09008#include "annotate.h"
Namhyung Kim740b97f2014-12-22 13:44:10 +09009#include "ui/progress.h"
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -020010#include <math.h>
John Kacur3d1d07e2009-09-28 15:32:55 +020011
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -020012static bool hists__filter_entry_by_dso(struct hists *hists,
13 struct hist_entry *he);
14static bool hists__filter_entry_by_thread(struct hists *hists,
15 struct hist_entry *he);
Namhyung Kime94d53e2012-03-16 17:50:51 +090016static bool hists__filter_entry_by_symbol(struct hists *hists,
17 struct hist_entry *he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -020018
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030019u16 hists__col_len(struct hists *hists, enum hist_column col)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030020{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030021 return hists->col_len[col];
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030022}
23
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030024void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030025{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030026 hists->col_len[col] = len;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030027}
28
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030029bool hists__new_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 if (len > hists__col_len(hists, col)) {
32 hists__set_col_len(hists, col, len);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030033 return true;
34 }
35 return false;
36}
37
Namhyung Kim7ccf4f92012-08-20 13:52:05 +090038void hists__reset_col_len(struct hists *hists)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030039{
40 enum hist_column col;
41
42 for (col = 0; col < HISTC_NR_COLS; ++col)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030043 hists__set_col_len(hists, col, 0);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030044}
45
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010046static void hists__set_unres_dso_col_len(struct hists *hists, int dso)
47{
48 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
49
50 if (hists__col_len(hists, dso) < unresolved_col_width &&
51 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
52 !symbol_conf.dso_list)
53 hists__set_col_len(hists, dso, unresolved_col_width);
54}
55
Namhyung Kim7ccf4f92012-08-20 13:52:05 +090056void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030057{
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010058 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
Stephane Eranian98a3b322013-01-24 16:10:35 +010059 int symlen;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030060 u16 len;
61
Namhyung Kimded19d52013-04-01 20:35:19 +090062 /*
63 * +4 accounts for '[x] ' priv level info
64 * +2 accounts for 0x prefix on raw addresses
65 * +3 accounts for ' y ' symtab origin info
66 */
67 if (h->ms.sym) {
68 symlen = h->ms.sym->namelen + 4;
69 if (verbose)
70 symlen += BITS_PER_LONG / 4 + 2 + 3;
71 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
72 } else {
Stephane Eranian98a3b322013-01-24 16:10:35 +010073 symlen = unresolved_col_width + 4 + 2;
74 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010075 hists__set_unres_dso_col_len(hists, HISTC_DSO);
Stephane Eranian98a3b322013-01-24 16:10:35 +010076 }
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030077
78 len = thread__comm_len(h->thread);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030079 if (hists__new_col_len(hists, HISTC_COMM, len))
80 hists__set_col_len(hists, HISTC_THREAD, len + 6);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030081
82 if (h->ms.map) {
83 len = dso__name_len(h->ms.map->dso);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030084 hists__new_col_len(hists, HISTC_DSO, len);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030085 }
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010086
Namhyung Kimcb993742012-12-27 18:11:42 +090087 if (h->parent)
88 hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen);
89
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010090 if (h->branch_info) {
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010091 if (h->branch_info->from.sym) {
92 symlen = (int)h->branch_info->from.sym->namelen + 4;
Namhyung Kimded19d52013-04-01 20:35:19 +090093 if (verbose)
94 symlen += BITS_PER_LONG / 4 + 2 + 3;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010095 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
96
97 symlen = dso__name_len(h->branch_info->from.map->dso);
98 hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
99 } else {
100 symlen = unresolved_col_width + 4 + 2;
101 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
102 hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
103 }
104
105 if (h->branch_info->to.sym) {
106 symlen = (int)h->branch_info->to.sym->namelen + 4;
Namhyung Kimded19d52013-04-01 20:35:19 +0900107 if (verbose)
108 symlen += BITS_PER_LONG / 4 + 2 + 3;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100109 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
110
111 symlen = dso__name_len(h->branch_info->to.map->dso);
112 hists__new_col_len(hists, HISTC_DSO_TO, symlen);
113 } else {
114 symlen = unresolved_col_width + 4 + 2;
115 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
116 hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
117 }
118 }
Stephane Eranian98a3b322013-01-24 16:10:35 +0100119
120 if (h->mem_info) {
Stephane Eranian98a3b322013-01-24 16:10:35 +0100121 if (h->mem_info->daddr.sym) {
122 symlen = (int)h->mem_info->daddr.sym->namelen + 4
123 + unresolved_col_width + 2;
124 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
125 symlen);
Don Zickus9b32ba72014-06-01 15:38:29 +0200126 hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
127 symlen + 1);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100128 } else {
129 symlen = unresolved_col_width + 4 + 2;
130 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
131 symlen);
132 }
133 if (h->mem_info->daddr.map) {
134 symlen = dso__name_len(h->mem_info->daddr.map->dso);
135 hists__new_col_len(hists, HISTC_MEM_DADDR_DSO,
136 symlen);
137 } else {
138 symlen = unresolved_col_width + 4 + 2;
139 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
140 }
141 } else {
142 symlen = unresolved_col_width + 4 + 2;
143 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen);
144 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
145 }
146
147 hists__new_col_len(hists, HISTC_MEM_LOCKED, 6);
148 hists__new_col_len(hists, HISTC_MEM_TLB, 22);
149 hists__new_col_len(hists, HISTC_MEM_SNOOP, 12);
150 hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3);
151 hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12);
152 hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12);
Andi Kleen475eeab2013-09-20 07:40:43 -0700153
154 if (h->transaction)
155 hists__new_col_len(hists, HISTC_TRANSACTION,
156 hist_entry__transaction_len());
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300157}
158
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900159void hists__output_recalc_col_len(struct hists *hists, int max_rows)
160{
161 struct rb_node *next = rb_first(&hists->entries);
162 struct hist_entry *n;
163 int row = 0;
164
165 hists__reset_col_len(hists);
166
167 while (next && row++ < max_rows) {
168 n = rb_entry(next, struct hist_entry, rb_node);
169 if (!n->filtered)
170 hists__calc_col_len(hists, n);
171 next = rb_next(&n->rb_node);
172 }
173}
174
Namhyung Kimf39056f2014-01-14 14:25:37 +0900175static void he_stat__add_cpumode_period(struct he_stat *he_stat,
176 unsigned int cpumode, u64 period)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800177{
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300178 switch (cpumode) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800179 case PERF_RECORD_MISC_KERNEL:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900180 he_stat->period_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800181 break;
182 case PERF_RECORD_MISC_USER:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900183 he_stat->period_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800184 break;
185 case PERF_RECORD_MISC_GUEST_KERNEL:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900186 he_stat->period_guest_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800187 break;
188 case PERF_RECORD_MISC_GUEST_USER:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900189 he_stat->period_guest_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800190 break;
191 default:
192 break;
193 }
194}
195
Andi Kleen05484292013-01-24 16:10:29 +0100196static void he_stat__add_period(struct he_stat *he_stat, u64 period,
197 u64 weight)
Namhyung Kim139c0812012-10-04 21:49:43 +0900198{
Stephane Eranian98a3b322013-01-24 16:10:35 +0100199
Namhyung Kim139c0812012-10-04 21:49:43 +0900200 he_stat->period += period;
Andi Kleen05484292013-01-24 16:10:29 +0100201 he_stat->weight += weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900202 he_stat->nr_events += 1;
203}
204
205static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
206{
207 dest->period += src->period;
208 dest->period_sys += src->period_sys;
209 dest->period_us += src->period_us;
210 dest->period_guest_sys += src->period_guest_sys;
211 dest->period_guest_us += src->period_guest_us;
212 dest->nr_events += src->nr_events;
Andi Kleen05484292013-01-24 16:10:29 +0100213 dest->weight += src->weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900214}
215
Namhyung Kimf39056f2014-01-14 14:25:37 +0900216static void he_stat__decay(struct he_stat *he_stat)
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300217{
Namhyung Kimf39056f2014-01-14 14:25:37 +0900218 he_stat->period = (he_stat->period * 7) / 8;
219 he_stat->nr_events = (he_stat->nr_events * 7) / 8;
Andi Kleen05484292013-01-24 16:10:29 +0100220 /* XXX need decay for weight too? */
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300221}
222
223static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
224{
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900225 u64 prev_period = he->stat.period;
Namhyung Kim3186b682014-04-22 13:44:23 +0900226 u64 diff;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200227
228 if (prev_period == 0)
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300229 return true;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200230
Namhyung Kimf39056f2014-01-14 14:25:37 +0900231 he_stat__decay(&he->stat);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900232 if (symbol_conf.cumulate_callchain)
233 he_stat__decay(he->stat_acc);
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
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300244static void hists__delete_entry(struct hists *hists, struct hist_entry *he)
245{
246 rb_erase(&he->rb_node, &hists->entries);
247
248 if (sort__need_collapse)
249 rb_erase(&he->rb_node_in, &hists->entries_collapsed);
250
251 --hists->nr_entries;
252 if (!he->filtered)
253 --hists->nr_non_filtered_entries;
254
255 hist_entry__delete(he);
256}
257
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900258void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300259{
260 struct rb_node *next = rb_first(&hists->entries);
261 struct hist_entry *n;
262
263 while (next) {
264 n = rb_entry(next, struct hist_entry, rb_node);
265 next = rb_next(&n->rb_node);
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200266 if (((zap_user && n->level == '.') ||
267 (zap_kernel && n->level != '.') ||
Arnaldo Carvalho de Melo4c47f4f2015-03-17 17:18:58 -0300268 hists__decay_entry(hists, n))) {
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300269 hists__delete_entry(hists, n);
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300270 }
271 }
272}
273
Namhyung Kim701937b2014-08-12 17:16:05 +0900274void hists__delete_entries(struct hists *hists)
275{
276 struct rb_node *next = rb_first(&hists->entries);
277 struct hist_entry *n;
278
279 while (next) {
280 n = rb_entry(next, struct hist_entry, rb_node);
281 next = rb_next(&n->rb_node);
282
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300283 hists__delete_entry(hists, n);
Namhyung Kim701937b2014-08-12 17:16:05 +0900284 }
285}
286
John Kacur3d1d07e2009-09-28 15:32:55 +0200287/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300288 * histogram, sorted on item, collects periods
John Kacur3d1d07e2009-09-28 15:32:55 +0200289 */
290
Namhyung Kima0b51af2012-09-11 13:34:27 +0900291static struct hist_entry *hist_entry__new(struct hist_entry *template,
292 bool sample_self)
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300293{
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900294 size_t callchain_size = 0;
295 struct hist_entry *he;
296
Namhyung Kim82aa0192014-12-22 13:44:14 +0900297 if (symbol_conf.use_callchain)
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900298 callchain_size = sizeof(struct callchain_root);
299
300 he = zalloc(sizeof(*he) + callchain_size);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300301
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200302 if (he != NULL) {
303 *he = *template;
Namhyung Kimc4b35352012-10-04 21:49:42 +0900304
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900305 if (symbol_conf.cumulate_callchain) {
306 he->stat_acc = malloc(sizeof(he->stat));
307 if (he->stat_acc == NULL) {
308 free(he);
309 return NULL;
310 }
311 memcpy(he->stat_acc, &he->stat, sizeof(he->stat));
Namhyung Kima0b51af2012-09-11 13:34:27 +0900312 if (!sample_self)
313 memset(&he->stat, 0, sizeof(he->stat));
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900314 }
315
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200316 if (he->ms.map)
317 he->ms.map->referenced = true;
Stephane Eranian3cf0cb12013-01-14 15:02:45 +0100318
319 if (he->branch_info) {
Namhyung Kim26353a62013-04-01 20:35:17 +0900320 /*
321 * This branch info is (a part of) allocated from
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -0300322 * sample__resolve_bstack() and will be freed after
Namhyung Kim26353a62013-04-01 20:35:17 +0900323 * adding new entries. So we need to save a copy.
324 */
325 he->branch_info = malloc(sizeof(*he->branch_info));
326 if (he->branch_info == NULL) {
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900327 free(he->stat_acc);
Namhyung Kim26353a62013-04-01 20:35:17 +0900328 free(he);
329 return NULL;
330 }
331
332 memcpy(he->branch_info, template->branch_info,
333 sizeof(*he->branch_info));
334
Stephane Eranian3cf0cb12013-01-14 15:02:45 +0100335 if (he->branch_info->from.map)
336 he->branch_info->from.map->referenced = true;
337 if (he->branch_info->to.map)
338 he->branch_info->to.map->referenced = true;
339 }
340
Stephane Eranian98a3b322013-01-24 16:10:35 +0100341 if (he->mem_info) {
342 if (he->mem_info->iaddr.map)
343 he->mem_info->iaddr.map->referenced = true;
344 if (he->mem_info->daddr.map)
345 he->mem_info->daddr.map->referenced = true;
346 }
347
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300348 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200349 callchain_init(he->callchain);
Arnaldo Carvalho de Melob821c732012-10-25 14:42:45 -0200350
351 INIT_LIST_HEAD(&he->pairs.node);
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300352 thread__get(he->thread);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300353 }
354
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200355 return he;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300356}
357
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300358static u8 symbol__parent_filter(const struct symbol *parent)
359{
360 if (symbol_conf.exclude_other && parent == NULL)
361 return 1 << HIST_FILTER__PARENT;
362 return 0;
363}
364
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100365static struct hist_entry *add_hist_entry(struct hists *hists,
Namhyung Kimf1cbf782013-12-18 14:21:11 +0900366 struct hist_entry *entry,
Namhyung Kima0b51af2012-09-11 13:34:27 +0900367 struct addr_location *al,
368 bool sample_self)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300369{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300370 struct rb_node **p;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300371 struct rb_node *parent = NULL;
372 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -0700373 int64_t cmp;
Namhyung Kimf1cbf782013-12-18 14:21:11 +0900374 u64 period = entry->stat.period;
375 u64 weight = entry->stat.weight;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300376
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300377 p = &hists->entries_in->rb_node;
378
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300379 while (*p != NULL) {
380 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300381 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300382
Namhyung Kim9afcf932012-12-10 17:29:54 +0900383 /*
384 * Make sure that it receives arguments in a same order as
385 * hist_entry__collapse() so that we can use an appropriate
386 * function when searching an entry regardless which sort
387 * keys were used.
388 */
389 cmp = hist_entry__cmp(he, entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300390
391 if (!cmp) {
Namhyung Kima0b51af2012-09-11 13:34:27 +0900392 if (sample_self)
393 he_stat__add_period(&he->stat, period, weight);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900394 if (symbol_conf.cumulate_callchain)
395 he_stat__add_period(he->stat_acc, period, weight);
David Miller63fa4712012-03-27 03:14:18 -0400396
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900397 /*
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -0300398 * This mem info was allocated from sample__resolve_mem
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900399 * and will not be used anymore.
400 */
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300401 zfree(&entry->mem_info);
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900402
David Miller63fa4712012-03-27 03:14:18 -0400403 /* If the map of an existing hist_entry has
404 * become out-of-date due to an exec() or
405 * similar, update it. Otherwise we will
406 * mis-adjust symbol addresses when computing
407 * the history counter to increment.
408 */
409 if (he->ms.map != entry->ms.map) {
410 he->ms.map = entry->ms.map;
411 if (he->ms.map)
412 he->ms.map->referenced = true;
413 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300414 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300415 }
416
417 if (cmp < 0)
418 p = &(*p)->rb_left;
419 else
420 p = &(*p)->rb_right;
421 }
422
Namhyung Kima0b51af2012-09-11 13:34:27 +0900423 he = hist_entry__new(entry, sample_self);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300424 if (!he)
Namhyung Kim27a0dcb2013-05-14 11:09:02 +0900425 return NULL;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300426
Namhyung Kim590cd342014-12-22 13:44:09 +0900427 hists->nr_entries++;
428
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300429 rb_link_node(&he->rb_node_in, parent, p);
430 rb_insert_color(&he->rb_node_in, hists->entries_in);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300431out:
Namhyung Kima0b51af2012-09-11 13:34:27 +0900432 if (sample_self)
433 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900434 if (symbol_conf.cumulate_callchain)
435 he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300436 return he;
437}
438
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300439struct hist_entry *__hists__add_entry(struct hists *hists,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100440 struct addr_location *al,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900441 struct symbol *sym_parent,
442 struct branch_info *bi,
443 struct mem_info *mi,
Namhyung Kima0b51af2012-09-11 13:34:27 +0900444 u64 period, u64 weight, u64 transaction,
445 bool sample_self)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100446{
447 struct hist_entry entry = {
448 .thread = al->thread,
Namhyung Kim4dfced32013-09-13 16:28:57 +0900449 .comm = thread__comm(al->thread),
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100450 .ms = {
451 .map = al->map,
452 .sym = al->sym,
453 },
Don Zickus7365be52014-05-27 12:28:05 -0400454 .cpu = al->cpu,
455 .cpumode = al->cpumode,
456 .ip = al->addr,
457 .level = al->level,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900458 .stat = {
Namhyung Kimc4b35352012-10-04 21:49:42 +0900459 .nr_events = 1,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900460 .period = period,
Andi Kleen05484292013-01-24 16:10:29 +0100461 .weight = weight,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900462 },
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100463 .parent = sym_parent,
Namhyung Kim2c86c7c2014-03-17 18:18:54 -0300464 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300465 .hists = hists,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900466 .branch_info = bi,
467 .mem_info = mi,
Andi Kleen475eeab2013-09-20 07:40:43 -0700468 .transaction = transaction,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100469 };
470
Namhyung Kima0b51af2012-09-11 13:34:27 +0900471 return add_hist_entry(hists, &entry, al, sample_self);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100472}
473
Namhyung Kim69bcb012013-10-30 09:40:34 +0900474static int
475iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
476 struct addr_location *al __maybe_unused)
477{
478 return 0;
479}
480
481static int
482iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
483 struct addr_location *al __maybe_unused)
484{
485 return 0;
486}
487
488static int
489iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
490{
491 struct perf_sample *sample = iter->sample;
492 struct mem_info *mi;
493
494 mi = sample__resolve_mem(sample, al);
495 if (mi == NULL)
496 return -ENOMEM;
497
498 iter->priv = mi;
499 return 0;
500}
501
502static int
503iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
504{
505 u64 cost;
506 struct mem_info *mi = iter->priv;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300507 struct hists *hists = evsel__hists(iter->evsel);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900508 struct hist_entry *he;
509
510 if (mi == NULL)
511 return -EINVAL;
512
513 cost = iter->sample->weight;
514 if (!cost)
515 cost = 1;
516
517 /*
518 * must pass period=weight in order to get the correct
519 * sorting from hists__collapse_resort() which is solely
520 * based on periods. We want sorting be done on nr_events * weight
521 * and this is indirectly achieved by passing period=weight here
522 * and the he_stat__add_period() function.
523 */
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300524 he = __hists__add_entry(hists, al, iter->parent, NULL, mi,
Namhyung Kima0b51af2012-09-11 13:34:27 +0900525 cost, cost, 0, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900526 if (!he)
527 return -ENOMEM;
528
529 iter->he = he;
530 return 0;
531}
532
533static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900534iter_finish_mem_entry(struct hist_entry_iter *iter,
535 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900536{
537 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300538 struct hists *hists = evsel__hists(evsel);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900539 struct hist_entry *he = iter->he;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900540 int err = -EINVAL;
541
542 if (he == NULL)
543 goto out;
544
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300545 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900546
547 err = hist_entry__append_callchain(he, iter->sample);
548
549out:
550 /*
551 * We don't need to free iter->priv (mem_info) here since
552 * the mem info was either already freed in add_hist_entry() or
553 * passed to a new hist entry by hist_entry__new().
554 */
555 iter->priv = NULL;
556
557 iter->he = NULL;
558 return err;
559}
560
561static int
562iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
563{
564 struct branch_info *bi;
565 struct perf_sample *sample = iter->sample;
566
567 bi = sample__resolve_bstack(sample, al);
568 if (!bi)
569 return -ENOMEM;
570
571 iter->curr = 0;
572 iter->total = sample->branch_stack->nr;
573
574 iter->priv = bi;
575 return 0;
576}
577
578static int
579iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused,
580 struct addr_location *al __maybe_unused)
581{
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900582 /* to avoid calling callback function */
583 iter->he = NULL;
584
Namhyung Kim69bcb012013-10-30 09:40:34 +0900585 return 0;
586}
587
588static int
589iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
590{
591 struct branch_info *bi = iter->priv;
592 int i = iter->curr;
593
594 if (bi == NULL)
595 return 0;
596
597 if (iter->curr >= iter->total)
598 return 0;
599
600 al->map = bi[i].to.map;
601 al->sym = bi[i].to.sym;
602 al->addr = bi[i].to.addr;
603 return 1;
604}
605
606static int
607iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
608{
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900609 struct branch_info *bi;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900610 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300611 struct hists *hists = evsel__hists(evsel);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900612 struct hist_entry *he = NULL;
613 int i = iter->curr;
614 int err = 0;
615
616 bi = iter->priv;
617
618 if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym))
619 goto out;
620
621 /*
622 * The report shows the percentage of total branches captured
623 * and not events sampled. Thus we use a pseudo period of 1.
624 */
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300625 he = __hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
Namhyung Kima0b51af2012-09-11 13:34:27 +0900626 1, 1, 0, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900627 if (he == NULL)
628 return -ENOMEM;
629
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300630 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900631
632out:
633 iter->he = he;
634 iter->curr++;
635 return err;
636}
637
638static int
639iter_finish_branch_entry(struct hist_entry_iter *iter,
640 struct addr_location *al __maybe_unused)
641{
642 zfree(&iter->priv);
643 iter->he = NULL;
644
645 return iter->curr >= iter->total ? 0 : -1;
646}
647
648static int
649iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
650 struct addr_location *al __maybe_unused)
651{
652 return 0;
653}
654
655static int
656iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
657{
658 struct perf_evsel *evsel = iter->evsel;
659 struct perf_sample *sample = iter->sample;
660 struct hist_entry *he;
661
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300662 he = __hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
Namhyung Kim69bcb012013-10-30 09:40:34 +0900663 sample->period, sample->weight,
Namhyung Kima0b51af2012-09-11 13:34:27 +0900664 sample->transaction, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900665 if (he == NULL)
666 return -ENOMEM;
667
668 iter->he = he;
669 return 0;
670}
671
672static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900673iter_finish_normal_entry(struct hist_entry_iter *iter,
674 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900675{
Namhyung Kim69bcb012013-10-30 09:40:34 +0900676 struct hist_entry *he = iter->he;
677 struct perf_evsel *evsel = iter->evsel;
678 struct perf_sample *sample = iter->sample;
679
680 if (he == NULL)
681 return 0;
682
683 iter->he = NULL;
684
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300685 hists__inc_nr_samples(evsel__hists(evsel), he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900686
687 return hist_entry__append_callchain(he, sample);
688}
689
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900690static int
691iter_prepare_cumulative_entry(struct hist_entry_iter *iter __maybe_unused,
692 struct addr_location *al __maybe_unused)
693{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900694 struct hist_entry **he_cache;
695
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900696 callchain_cursor_commit(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900697
698 /*
699 * This is for detecting cycles or recursions so that they're
700 * cumulated only one time to prevent entries more than 100%
701 * overhead.
702 */
703 he_cache = malloc(sizeof(*he_cache) * (PERF_MAX_STACK_DEPTH + 1));
704 if (he_cache == NULL)
705 return -ENOMEM;
706
707 iter->priv = he_cache;
708 iter->curr = 0;
709
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900710 return 0;
711}
712
713static int
714iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
715 struct addr_location *al)
716{
717 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300718 struct hists *hists = evsel__hists(evsel);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900719 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900720 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900721 struct hist_entry *he;
722 int err = 0;
723
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300724 he = __hists__add_entry(hists, al, iter->parent, NULL, NULL,
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900725 sample->period, sample->weight,
726 sample->transaction, true);
727 if (he == NULL)
728 return -ENOMEM;
729
730 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900731 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900732
Namhyung Kim82aa0192014-12-22 13:44:14 +0900733 hist_entry__append_callchain(he, sample);
Namhyung Kimbe7f8552013-12-26 17:44:10 +0900734
735 /*
736 * We need to re-initialize the cursor since callchain_append()
737 * advanced the cursor to the end.
738 */
739 callchain_cursor_commit(&callchain_cursor);
740
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300741 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900742
743 return err;
744}
745
746static int
747iter_next_cumulative_entry(struct hist_entry_iter *iter,
748 struct addr_location *al)
749{
750 struct callchain_cursor_node *node;
751
752 node = callchain_cursor_current(&callchain_cursor);
753 if (node == NULL)
754 return 0;
755
Namhyung Kimc7405d82013-10-31 13:58:30 +0900756 return fill_callchain_info(al, node, iter->hide_unresolved);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900757}
758
759static int
760iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
761 struct addr_location *al)
762{
763 struct perf_evsel *evsel = iter->evsel;
764 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900765 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900766 struct hist_entry *he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900767 struct hist_entry he_tmp = {
768 .cpu = al->cpu,
769 .thread = al->thread,
770 .comm = thread__comm(al->thread),
771 .ip = al->addr,
772 .ms = {
773 .map = al->map,
774 .sym = al->sym,
775 },
776 .parent = iter->parent,
777 };
778 int i;
Namhyung Kimbe7f8552013-12-26 17:44:10 +0900779 struct callchain_cursor cursor;
780
781 callchain_cursor_snapshot(&cursor, &callchain_cursor);
782
783 callchain_cursor_advance(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900784
785 /*
786 * Check if there's duplicate entries in the callchain.
787 * It's possible that it has cycles or recursive calls.
788 */
789 for (i = 0; i < iter->curr; i++) {
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900790 if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
791 /* to avoid calling callback function */
792 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900793 return 0;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900794 }
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900795 }
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900796
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300797 he = __hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900798 sample->period, sample->weight,
799 sample->transaction, false);
800 if (he == NULL)
801 return -ENOMEM;
802
803 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900804 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900805
Namhyung Kim82aa0192014-12-22 13:44:14 +0900806 if (symbol_conf.use_callchain)
807 callchain_append(he->callchain, &cursor, sample->period);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900808 return 0;
809}
810
811static int
812iter_finish_cumulative_entry(struct hist_entry_iter *iter,
813 struct addr_location *al __maybe_unused)
814{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900815 zfree(&iter->priv);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900816 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900817
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900818 return 0;
819}
820
Namhyung Kim69bcb012013-10-30 09:40:34 +0900821const struct hist_iter_ops hist_iter_mem = {
822 .prepare_entry = iter_prepare_mem_entry,
823 .add_single_entry = iter_add_single_mem_entry,
824 .next_entry = iter_next_nop_entry,
825 .add_next_entry = iter_add_next_nop_entry,
826 .finish_entry = iter_finish_mem_entry,
827};
828
829const struct hist_iter_ops hist_iter_branch = {
830 .prepare_entry = iter_prepare_branch_entry,
831 .add_single_entry = iter_add_single_branch_entry,
832 .next_entry = iter_next_branch_entry,
833 .add_next_entry = iter_add_next_branch_entry,
834 .finish_entry = iter_finish_branch_entry,
835};
836
837const struct hist_iter_ops hist_iter_normal = {
838 .prepare_entry = iter_prepare_normal_entry,
839 .add_single_entry = iter_add_single_normal_entry,
840 .next_entry = iter_next_nop_entry,
841 .add_next_entry = iter_add_next_nop_entry,
842 .finish_entry = iter_finish_normal_entry,
843};
844
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900845const struct hist_iter_ops hist_iter_cumulative = {
846 .prepare_entry = iter_prepare_cumulative_entry,
847 .add_single_entry = iter_add_single_cumulative_entry,
848 .next_entry = iter_next_cumulative_entry,
849 .add_next_entry = iter_add_next_cumulative_entry,
850 .finish_entry = iter_finish_cumulative_entry,
851};
852
Namhyung Kim69bcb012013-10-30 09:40:34 +0900853int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
854 struct perf_evsel *evsel, struct perf_sample *sample,
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900855 int max_stack_depth, void *arg)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900856{
857 int err, err2;
858
859 err = sample__resolve_callchain(sample, &iter->parent, evsel, al,
860 max_stack_depth);
861 if (err)
862 return err;
863
864 iter->evsel = evsel;
865 iter->sample = sample;
866
867 err = iter->ops->prepare_entry(iter, al);
868 if (err)
869 goto out;
870
871 err = iter->ops->add_single_entry(iter, al);
872 if (err)
873 goto out;
874
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900875 if (iter->he && iter->add_entry_cb) {
876 err = iter->add_entry_cb(iter, al, true, arg);
877 if (err)
878 goto out;
879 }
880
Namhyung Kim69bcb012013-10-30 09:40:34 +0900881 while (iter->ops->next_entry(iter, al)) {
882 err = iter->ops->add_next_entry(iter, al);
883 if (err)
884 break;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900885
886 if (iter->he && iter->add_entry_cb) {
887 err = iter->add_entry_cb(iter, al, false, arg);
888 if (err)
889 goto out;
890 }
Namhyung Kim69bcb012013-10-30 09:40:34 +0900891 }
892
893out:
894 err2 = iter->ops->finish_entry(iter, al);
895 if (!err)
896 err = err2;
897
898 return err;
899}
900
John Kacur3d1d07e2009-09-28 15:32:55 +0200901int64_t
902hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
903{
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900904 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +0200905 int64_t cmp = 0;
906
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900907 perf_hpp__for_each_sort_list(fmt) {
Namhyung Kime67d49a2014-03-18 13:00:59 +0900908 if (perf_hpp__should_skip(fmt))
909 continue;
910
Namhyung Kim87bbdf72015-01-08 09:45:46 +0900911 cmp = fmt->cmp(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200912 if (cmp)
913 break;
914 }
915
916 return cmp;
917}
918
919int64_t
920hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
921{
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900922 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +0200923 int64_t cmp = 0;
924
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900925 perf_hpp__for_each_sort_list(fmt) {
Namhyung Kime67d49a2014-03-18 13:00:59 +0900926 if (perf_hpp__should_skip(fmt))
927 continue;
928
Namhyung Kim87bbdf72015-01-08 09:45:46 +0900929 cmp = fmt->collapse(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200930 if (cmp)
931 break;
932 }
933
934 return cmp;
935}
936
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -0300937void hist_entry__delete(struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +0200938{
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300939 thread__zput(he->thread);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300940 zfree(&he->branch_info);
941 zfree(&he->mem_info);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900942 zfree(&he->stat_acc);
Namhyung Kimf048d542013-09-11 14:09:28 +0900943 free_srcline(he->srcline);
Namhyung Kimd1149602014-12-30 14:38:13 +0900944 free_callchain(he->callchain);
John Kacur3d1d07e2009-09-28 15:32:55 +0200945 free(he);
946}
947
948/*
949 * collapse the histogram
950 */
951
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300952static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100953 struct rb_root *root,
954 struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +0200955{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200956 struct rb_node **p = &root->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200957 struct rb_node *parent = NULL;
958 struct hist_entry *iter;
959 int64_t cmp;
960
961 while (*p != NULL) {
962 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300963 iter = rb_entry(parent, struct hist_entry, rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +0200964
965 cmp = hist_entry__collapse(iter, he);
966
967 if (!cmp) {
Namhyung Kim139c0812012-10-04 21:49:43 +0900968 he_stat__add_stat(&iter->stat, &he->stat);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900969 if (symbol_conf.cumulate_callchain)
970 he_stat__add_stat(iter->stat_acc, he->stat_acc);
Namhyung Kim9ec60972012-09-26 16:47:28 +0900971
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100972 if (symbol_conf.use_callchain) {
Namhyung Kim47260642012-05-31 14:43:26 +0900973 callchain_cursor_reset(&callchain_cursor);
974 callchain_merge(&callchain_cursor,
975 iter->callchain,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100976 he->callchain);
977 }
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -0300978 hist_entry__delete(he);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300979 return false;
John Kacur3d1d07e2009-09-28 15:32:55 +0200980 }
981
982 if (cmp < 0)
983 p = &(*p)->rb_left;
984 else
985 p = &(*p)->rb_right;
986 }
Namhyung Kim740b97f2014-12-22 13:44:10 +0900987 hists->nr_entries++;
John Kacur3d1d07e2009-09-28 15:32:55 +0200988
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300989 rb_link_node(&he->rb_node_in, parent, p);
990 rb_insert_color(&he->rb_node_in, root);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300991 return true;
John Kacur3d1d07e2009-09-28 15:32:55 +0200992}
993
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300994static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
995{
996 struct rb_root *root;
997
998 pthread_mutex_lock(&hists->lock);
999
1000 root = hists->entries_in;
1001 if (++hists->entries_in > &hists->entries_in_array[1])
1002 hists->entries_in = &hists->entries_in_array[0];
1003
1004 pthread_mutex_unlock(&hists->lock);
1005
1006 return root;
1007}
1008
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001009static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
1010{
1011 hists__filter_entry_by_dso(hists, he);
1012 hists__filter_entry_by_thread(hists, he);
Namhyung Kime94d53e2012-03-16 17:50:51 +09001013 hists__filter_entry_by_symbol(hists, he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001014}
1015
Namhyung Kimc1fb5652013-10-11 14:15:38 +09001016void hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001017{
1018 struct rb_root *root;
1019 struct rb_node *next;
1020 struct hist_entry *n;
1021
Namhyung Kim3a5714f2013-05-14 11:09:01 +09001022 if (!sort__need_collapse)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001023 return;
1024
Namhyung Kim740b97f2014-12-22 13:44:10 +09001025 hists->nr_entries = 0;
1026
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001027 root = hists__get_rotate_entries_in(hists);
Namhyung Kim740b97f2014-12-22 13:44:10 +09001028
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001029 next = rb_first(root);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001030
1031 while (next) {
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001032 if (session_done())
1033 break;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001034 n = rb_entry(next, struct hist_entry, rb_node_in);
1035 next = rb_next(&n->rb_node_in);
1036
1037 rb_erase(&n->rb_node_in, root);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001038 if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
1039 /*
1040 * If it wasn't combined with one of the entries already
1041 * collapsed, we need to apply the filters that may have
1042 * been set by, say, the hist_browser.
1043 */
1044 hists__apply_filters(hists, n);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001045 }
Namhyung Kimc1fb5652013-10-11 14:15:38 +09001046 if (prog)
1047 ui_progress__update(prog, 1);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001048 }
1049}
1050
Namhyung Kim043ca3892014-03-03 14:18:00 +09001051static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001052{
Namhyung Kim043ca3892014-03-03 14:18:00 +09001053 struct perf_hpp_fmt *fmt;
1054 int64_t cmp = 0;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001055
Namhyung Kim26d8b332014-03-03 16:16:20 +09001056 perf_hpp__for_each_sort_list(fmt) {
Namhyung Kime67d49a2014-03-18 13:00:59 +09001057 if (perf_hpp__should_skip(fmt))
1058 continue;
1059
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001060 cmp = fmt->sort(fmt, a, b);
Namhyung Kim043ca3892014-03-03 14:18:00 +09001061 if (cmp)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001062 break;
1063 }
1064
Namhyung Kim043ca3892014-03-03 14:18:00 +09001065 return cmp;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001066}
1067
Namhyung Kim9283ba92014-04-24 16:37:26 +09001068static void hists__reset_filter_stats(struct hists *hists)
1069{
1070 hists->nr_non_filtered_entries = 0;
1071 hists->stats.total_non_filtered_period = 0;
1072}
1073
1074void hists__reset_stats(struct hists *hists)
1075{
1076 hists->nr_entries = 0;
1077 hists->stats.total_period = 0;
1078
1079 hists__reset_filter_stats(hists);
1080}
1081
1082static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
1083{
1084 hists->nr_non_filtered_entries++;
1085 hists->stats.total_non_filtered_period += h->stat.period;
1086}
1087
1088void hists__inc_stats(struct hists *hists, struct hist_entry *h)
1089{
1090 if (!h->filtered)
1091 hists__inc_filter_stats(hists, h);
1092
1093 hists->nr_entries++;
1094 hists->stats.total_period += h->stat.period;
1095}
1096
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001097static void __hists__insert_output_entry(struct rb_root *entries,
1098 struct hist_entry *he,
1099 u64 min_callchain_hits)
John Kacur3d1d07e2009-09-28 15:32:55 +02001100{
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001101 struct rb_node **p = &entries->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +02001102 struct rb_node *parent = NULL;
1103 struct hist_entry *iter;
1104
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -02001105 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melob9fb9302010-04-02 09:50:42 -03001106 callchain_param.sort(&he->sorted_chain, he->callchain,
John Kacur3d1d07e2009-09-28 15:32:55 +02001107 min_callchain_hits, &callchain_param);
1108
1109 while (*p != NULL) {
1110 parent = *p;
1111 iter = rb_entry(parent, struct hist_entry, rb_node);
1112
Namhyung Kim043ca3892014-03-03 14:18:00 +09001113 if (hist_entry__sort(he, iter) > 0)
John Kacur3d1d07e2009-09-28 15:32:55 +02001114 p = &(*p)->rb_left;
1115 else
1116 p = &(*p)->rb_right;
1117 }
1118
1119 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001120 rb_insert_color(&he->rb_node, entries);
John Kacur3d1d07e2009-09-28 15:32:55 +02001121}
1122
Namhyung Kim740b97f2014-12-22 13:44:10 +09001123void hists__output_resort(struct hists *hists, struct ui_progress *prog)
John Kacur3d1d07e2009-09-28 15:32:55 +02001124{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001125 struct rb_root *root;
John Kacur3d1d07e2009-09-28 15:32:55 +02001126 struct rb_node *next;
1127 struct hist_entry *n;
John Kacur3d1d07e2009-09-28 15:32:55 +02001128 u64 min_callchain_hits;
1129
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001130 min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
John Kacur3d1d07e2009-09-28 15:32:55 +02001131
Namhyung Kim3a5714f2013-05-14 11:09:01 +09001132 if (sort__need_collapse)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001133 root = &hists->entries_collapsed;
1134 else
1135 root = hists->entries_in;
1136
1137 next = rb_first(root);
1138 hists->entries = RB_ROOT;
John Kacur3d1d07e2009-09-28 15:32:55 +02001139
Namhyung Kim9283ba92014-04-24 16:37:26 +09001140 hists__reset_stats(hists);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001141 hists__reset_col_len(hists);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -03001142
John Kacur3d1d07e2009-09-28 15:32:55 +02001143 while (next) {
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001144 n = rb_entry(next, struct hist_entry, rb_node_in);
1145 next = rb_next(&n->rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +02001146
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001147 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
Namhyung Kim62638352014-04-24 16:21:46 +09001148 hists__inc_stats(hists, n);
Namhyung Kimae993ef2014-04-24 16:25:19 +09001149
1150 if (!n->filtered)
1151 hists__calc_col_len(hists, n);
Namhyung Kim740b97f2014-12-22 13:44:10 +09001152
1153 if (prog)
1154 ui_progress__update(prog, 1);
John Kacur3d1d07e2009-09-28 15:32:55 +02001155 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001156}
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -02001157
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001158static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001159 enum hist_filter filter)
1160{
1161 h->filtered &= ~(1 << filter);
1162 if (h->filtered)
1163 return;
1164
Namhyung Kim87e90f42014-04-24 16:44:16 +09001165 /* force fold unfiltered entry for simplicity */
1166 h->ms.unfolded = false;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -03001167 h->row_offset = 0;
He Kuanga8cd1f42015-03-11 20:36:03 +08001168 h->nr_rows = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001169
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001170 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001171
Namhyung Kim9283ba92014-04-24 16:37:26 +09001172 hists__inc_filter_stats(hists, h);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001173 hists__calc_col_len(hists, h);
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001174}
1175
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001176
1177static bool hists__filter_entry_by_dso(struct hists *hists,
1178 struct hist_entry *he)
1179{
1180 if (hists->dso_filter != NULL &&
1181 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
1182 he->filtered |= (1 << HIST_FILTER__DSO);
1183 return true;
1184 }
1185
1186 return false;
1187}
1188
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001189void hists__filter_by_dso(struct hists *hists)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001190{
1191 struct rb_node *nd;
1192
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001193 hists->stats.nr_non_filtered_samples = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001194
1195 hists__reset_filter_stats(hists);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001196 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001197
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001198 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001199 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1200
1201 if (symbol_conf.exclude_other && !h->parent)
1202 continue;
1203
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001204 if (hists__filter_entry_by_dso(hists, h))
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001205 continue;
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001206
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001207 hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001208 }
1209}
1210
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001211static bool hists__filter_entry_by_thread(struct hists *hists,
1212 struct hist_entry *he)
1213{
1214 if (hists->thread_filter != NULL &&
1215 he->thread != hists->thread_filter) {
1216 he->filtered |= (1 << HIST_FILTER__THREAD);
1217 return true;
1218 }
1219
1220 return false;
1221}
1222
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001223void hists__filter_by_thread(struct hists *hists)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001224{
1225 struct rb_node *nd;
1226
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001227 hists->stats.nr_non_filtered_samples = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001228
1229 hists__reset_filter_stats(hists);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001230 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001231
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001232 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001233 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1234
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001235 if (hists__filter_entry_by_thread(hists, h))
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001236 continue;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001237
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001238 hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001239 }
1240}
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001241
Namhyung Kime94d53e2012-03-16 17:50:51 +09001242static bool hists__filter_entry_by_symbol(struct hists *hists,
1243 struct hist_entry *he)
1244{
1245 if (hists->symbol_filter_str != NULL &&
1246 (!he->ms.sym || strstr(he->ms.sym->name,
1247 hists->symbol_filter_str) == NULL)) {
1248 he->filtered |= (1 << HIST_FILTER__SYMBOL);
1249 return true;
1250 }
1251
1252 return false;
1253}
1254
1255void hists__filter_by_symbol(struct hists *hists)
1256{
1257 struct rb_node *nd;
1258
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001259 hists->stats.nr_non_filtered_samples = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001260
1261 hists__reset_filter_stats(hists);
Namhyung Kime94d53e2012-03-16 17:50:51 +09001262 hists__reset_col_len(hists);
1263
1264 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1265 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1266
1267 if (hists__filter_entry_by_symbol(hists, h))
1268 continue;
1269
1270 hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
1271 }
1272}
1273
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001274void events_stats__inc(struct events_stats *stats, u32 type)
1275{
1276 ++stats->nr_events[0];
1277 ++stats->nr_events[type];
1278}
1279
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001280void hists__inc_nr_events(struct hists *hists, u32 type)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001281{
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001282 events_stats__inc(&hists->stats, type);
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001283}
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001284
Namhyung Kim1844dbc2014-05-28 14:12:18 +09001285void hists__inc_nr_samples(struct hists *hists, bool filtered)
1286{
1287 events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
1288 if (!filtered)
1289 hists->stats.nr_non_filtered_samples++;
1290}
1291
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001292static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
1293 struct hist_entry *pair)
1294{
Namhyung Kimce74f602012-12-10 17:29:55 +09001295 struct rb_root *root;
1296 struct rb_node **p;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001297 struct rb_node *parent = NULL;
1298 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -07001299 int64_t cmp;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001300
Namhyung Kimce74f602012-12-10 17:29:55 +09001301 if (sort__need_collapse)
1302 root = &hists->entries_collapsed;
1303 else
1304 root = hists->entries_in;
1305
1306 p = &root->rb_node;
1307
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001308 while (*p != NULL) {
1309 parent = *p;
Namhyung Kimce74f602012-12-10 17:29:55 +09001310 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001311
Namhyung Kimce74f602012-12-10 17:29:55 +09001312 cmp = hist_entry__collapse(he, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001313
1314 if (!cmp)
1315 goto out;
1316
1317 if (cmp < 0)
1318 p = &(*p)->rb_left;
1319 else
1320 p = &(*p)->rb_right;
1321 }
1322
Namhyung Kima0b51af2012-09-11 13:34:27 +09001323 he = hist_entry__new(pair, true);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001324 if (he) {
Arnaldo Carvalho de Melo30193d72012-11-12 13:20:03 -03001325 memset(&he->stat, 0, sizeof(he->stat));
1326 he->hists = hists;
Namhyung Kimce74f602012-12-10 17:29:55 +09001327 rb_link_node(&he->rb_node_in, parent, p);
1328 rb_insert_color(&he->rb_node_in, root);
Namhyung Kim62638352014-04-24 16:21:46 +09001329 hists__inc_stats(hists, he);
Jiri Olsae0af43d2012-12-01 21:18:20 +01001330 he->dummy = true;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001331 }
1332out:
1333 return he;
1334}
1335
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001336static struct hist_entry *hists__find_entry(struct hists *hists,
1337 struct hist_entry *he)
1338{
Namhyung Kimce74f602012-12-10 17:29:55 +09001339 struct rb_node *n;
1340
1341 if (sort__need_collapse)
1342 n = hists->entries_collapsed.rb_node;
1343 else
1344 n = hists->entries_in->rb_node;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001345
1346 while (n) {
Namhyung Kimce74f602012-12-10 17:29:55 +09001347 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
1348 int64_t cmp = hist_entry__collapse(iter, he);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001349
1350 if (cmp < 0)
1351 n = n->rb_left;
1352 else if (cmp > 0)
1353 n = n->rb_right;
1354 else
1355 return iter;
1356 }
1357
1358 return NULL;
1359}
1360
1361/*
1362 * Look for pairs to link to the leader buckets (hist_entries):
1363 */
1364void hists__match(struct hists *leader, struct hists *other)
1365{
Namhyung Kimce74f602012-12-10 17:29:55 +09001366 struct rb_root *root;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -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 = &leader->entries_collapsed;
1372 else
1373 root = leader->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 Melo95529be2012-11-08 17:54:33 -03001377 pair = hists__find_entry(other, pos);
1378
1379 if (pair)
Namhyung Kim5fa90412012-11-29 15:38:34 +09001380 hist_entry__add_pair(pair, pos);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001381 }
1382}
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001383
1384/*
1385 * Look for entries in the other hists that are not present in the leader, if
1386 * we find them, just add a dummy entry on the leader hists, with period=0,
1387 * nr_events=0, to serve as the list header.
1388 */
1389int hists__link(struct hists *leader, struct hists *other)
1390{
Namhyung Kimce74f602012-12-10 17:29:55 +09001391 struct rb_root *root;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001392 struct rb_node *nd;
1393 struct hist_entry *pos, *pair;
1394
Namhyung Kimce74f602012-12-10 17:29:55 +09001395 if (sort__need_collapse)
1396 root = &other->entries_collapsed;
1397 else
1398 root = other->entries_in;
1399
1400 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
1401 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001402
1403 if (!hist_entry__has_pairs(pos)) {
1404 pair = hists__add_dummy_entry(leader, pos);
1405 if (pair == NULL)
1406 return -1;
Namhyung Kim5fa90412012-11-29 15:38:34 +09001407 hist_entry__add_pair(pos, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001408 }
1409 }
1410
1411 return 0;
1412}
Namhyung Kimf2148332014-01-14 11:52:48 +09001413
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03001414
1415size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp)
1416{
1417 struct perf_evsel *pos;
1418 size_t ret = 0;
1419
1420 evlist__for_each(evlist, pos) {
1421 ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
1422 ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
1423 }
1424
1425 return ret;
1426}
1427
1428
Namhyung Kimf2148332014-01-14 11:52:48 +09001429u64 hists__total_period(struct hists *hists)
1430{
1431 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
1432 hists->stats.total_period;
1433}
Namhyung Kim33db4562014-02-07 12:06:07 +09001434
1435int parse_filter_percentage(const struct option *opt __maybe_unused,
1436 const char *arg, int unset __maybe_unused)
1437{
1438 if (!strcmp(arg, "relative"))
1439 symbol_conf.filter_relative = true;
1440 else if (!strcmp(arg, "absolute"))
1441 symbol_conf.filter_relative = false;
1442 else
1443 return -1;
1444
1445 return 0;
1446}
Namhyung Kim0b93da12014-01-14 12:02:15 +09001447
1448int perf_hist_config(const char *var, const char *value)
1449{
1450 if (!strcmp(var, "hist.percentage"))
1451 return parse_filter_percentage(NULL, value, 0);
1452
1453 return 0;
1454}
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03001455
1456static int hists_evsel__init(struct perf_evsel *evsel)
1457{
1458 struct hists *hists = evsel__hists(evsel);
1459
1460 memset(hists, 0, sizeof(*hists));
1461 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
1462 hists->entries_in = &hists->entries_in_array[0];
1463 hists->entries_collapsed = RB_ROOT;
1464 hists->entries = RB_ROOT;
1465 pthread_mutex_init(&hists->lock, NULL);
1466 return 0;
1467}
1468
1469/*
1470 * XXX We probably need a hists_evsel__exit() to free the hist_entries
1471 * stored in the rbtree...
1472 */
1473
1474int hists__init(void)
1475{
1476 int err = perf_evsel__object_config(sizeof(struct hists_evsel),
1477 hists_evsel__init, NULL);
1478 if (err)
1479 fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);
1480
1481 return err;
1482}