blob: 54fc0033dd6af5fdfffac2b43e52cbd62e1892f8 [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 Melo5c24b672015-06-15 23:29:51 -0300316 map__get(he->ms.map);
Stephane Eranian3cf0cb12013-01-14 15:02:45 +0100317
318 if (he->branch_info) {
Namhyung Kim26353a62013-04-01 20:35:17 +0900319 /*
320 * This branch info is (a part of) allocated from
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -0300321 * sample__resolve_bstack() and will be freed after
Namhyung Kim26353a62013-04-01 20:35:17 +0900322 * adding new entries. So we need to save a copy.
323 */
324 he->branch_info = malloc(sizeof(*he->branch_info));
325 if (he->branch_info == NULL) {
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300326 map__zput(he->ms.map);
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
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300335 map__get(he->branch_info->from.map);
336 map__get(he->branch_info->to.map);
Stephane Eranian3cf0cb12013-01-14 15:02:45 +0100337 }
338
Stephane Eranian98a3b322013-01-24 16:10:35 +0100339 if (he->mem_info) {
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300340 map__get(he->mem_info->iaddr.map);
341 map__get(he->mem_info->daddr.map);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100342 }
343
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300344 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200345 callchain_init(he->callchain);
Arnaldo Carvalho de Melob821c732012-10-25 14:42:45 -0200346
347 INIT_LIST_HEAD(&he->pairs.node);
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300348 thread__get(he->thread);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300349 }
350
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200351 return he;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300352}
353
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300354static u8 symbol__parent_filter(const struct symbol *parent)
355{
356 if (symbol_conf.exclude_other && parent == NULL)
357 return 1 << HIST_FILTER__PARENT;
358 return 0;
359}
360
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300361static struct hist_entry *hists__findnew_entry(struct hists *hists,
362 struct hist_entry *entry,
363 struct addr_location *al,
364 bool sample_self)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300365{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300366 struct rb_node **p;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300367 struct rb_node *parent = NULL;
368 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -0700369 int64_t cmp;
Namhyung Kimf1cbf782013-12-18 14:21:11 +0900370 u64 period = entry->stat.period;
371 u64 weight = entry->stat.weight;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300372
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300373 p = &hists->entries_in->rb_node;
374
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300375 while (*p != NULL) {
376 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300377 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300378
Namhyung Kim9afcf932012-12-10 17:29:54 +0900379 /*
380 * Make sure that it receives arguments in a same order as
381 * hist_entry__collapse() so that we can use an appropriate
382 * function when searching an entry regardless which sort
383 * keys were used.
384 */
385 cmp = hist_entry__cmp(he, entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300386
387 if (!cmp) {
Namhyung Kima0b51af2012-09-11 13:34:27 +0900388 if (sample_self)
389 he_stat__add_period(&he->stat, period, weight);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900390 if (symbol_conf.cumulate_callchain)
391 he_stat__add_period(he->stat_acc, period, weight);
David Miller63fa4712012-03-27 03:14:18 -0400392
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900393 /*
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -0300394 * This mem info was allocated from sample__resolve_mem
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900395 * and will not be used anymore.
396 */
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300397 zfree(&entry->mem_info);
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900398
David Miller63fa4712012-03-27 03:14:18 -0400399 /* If the map of an existing hist_entry has
400 * become out-of-date due to an exec() or
401 * similar, update it. Otherwise we will
402 * mis-adjust symbol addresses when computing
403 * the history counter to increment.
404 */
405 if (he->ms.map != entry->ms.map) {
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300406 map__put(he->ms.map);
407 he->ms.map = map__get(entry->ms.map);
David Miller63fa4712012-03-27 03:14:18 -0400408 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300409 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300410 }
411
412 if (cmp < 0)
413 p = &(*p)->rb_left;
414 else
415 p = &(*p)->rb_right;
416 }
417
Namhyung Kima0b51af2012-09-11 13:34:27 +0900418 he = hist_entry__new(entry, sample_self);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300419 if (!he)
Namhyung Kim27a0dcb2013-05-14 11:09:02 +0900420 return NULL;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300421
Namhyung Kim590cd342014-12-22 13:44:09 +0900422 hists->nr_entries++;
423
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300424 rb_link_node(&he->rb_node_in, parent, p);
425 rb_insert_color(&he->rb_node_in, hists->entries_in);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300426out:
Namhyung Kima0b51af2012-09-11 13:34:27 +0900427 if (sample_self)
428 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900429 if (symbol_conf.cumulate_callchain)
430 he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300431 return he;
432}
433
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300434struct hist_entry *__hists__add_entry(struct hists *hists,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100435 struct addr_location *al,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900436 struct symbol *sym_parent,
437 struct branch_info *bi,
438 struct mem_info *mi,
Namhyung Kima0b51af2012-09-11 13:34:27 +0900439 u64 period, u64 weight, u64 transaction,
440 bool sample_self)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100441{
442 struct hist_entry entry = {
443 .thread = al->thread,
Namhyung Kim4dfced32013-09-13 16:28:57 +0900444 .comm = thread__comm(al->thread),
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100445 .ms = {
446 .map = al->map,
447 .sym = al->sym,
448 },
Don Zickus7365be52014-05-27 12:28:05 -0400449 .cpu = al->cpu,
450 .cpumode = al->cpumode,
451 .ip = al->addr,
452 .level = al->level,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900453 .stat = {
Namhyung Kimc4b35352012-10-04 21:49:42 +0900454 .nr_events = 1,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900455 .period = period,
Andi Kleen05484292013-01-24 16:10:29 +0100456 .weight = weight,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900457 },
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100458 .parent = sym_parent,
Namhyung Kim2c86c7c2014-03-17 18:18:54 -0300459 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300460 .hists = hists,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900461 .branch_info = bi,
462 .mem_info = mi,
Andi Kleen475eeab2013-09-20 07:40:43 -0700463 .transaction = transaction,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100464 };
465
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300466 return hists__findnew_entry(hists, &entry, al, sample_self);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100467}
468
Namhyung Kim69bcb012013-10-30 09:40:34 +0900469static int
470iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
471 struct addr_location *al __maybe_unused)
472{
473 return 0;
474}
475
476static int
477iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
478 struct addr_location *al __maybe_unused)
479{
480 return 0;
481}
482
483static int
484iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
485{
486 struct perf_sample *sample = iter->sample;
487 struct mem_info *mi;
488
489 mi = sample__resolve_mem(sample, al);
490 if (mi == NULL)
491 return -ENOMEM;
492
493 iter->priv = mi;
494 return 0;
495}
496
497static int
498iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
499{
500 u64 cost;
501 struct mem_info *mi = iter->priv;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300502 struct hists *hists = evsel__hists(iter->evsel);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900503 struct hist_entry *he;
504
505 if (mi == NULL)
506 return -EINVAL;
507
508 cost = iter->sample->weight;
509 if (!cost)
510 cost = 1;
511
512 /*
513 * must pass period=weight in order to get the correct
514 * sorting from hists__collapse_resort() which is solely
515 * based on periods. We want sorting be done on nr_events * weight
516 * and this is indirectly achieved by passing period=weight here
517 * and the he_stat__add_period() function.
518 */
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300519 he = __hists__add_entry(hists, al, iter->parent, NULL, mi,
Namhyung Kima0b51af2012-09-11 13:34:27 +0900520 cost, cost, 0, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900521 if (!he)
522 return -ENOMEM;
523
524 iter->he = he;
525 return 0;
526}
527
528static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900529iter_finish_mem_entry(struct hist_entry_iter *iter,
530 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900531{
532 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300533 struct hists *hists = evsel__hists(evsel);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900534 struct hist_entry *he = iter->he;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900535 int err = -EINVAL;
536
537 if (he == NULL)
538 goto out;
539
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300540 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900541
542 err = hist_entry__append_callchain(he, iter->sample);
543
544out:
545 /*
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300546 * We don't need to free iter->priv (mem_info) here since the mem info
547 * was either already freed in hists__findnew_entry() or passed to a
548 * new hist entry by hist_entry__new().
Namhyung Kim69bcb012013-10-30 09:40:34 +0900549 */
550 iter->priv = NULL;
551
552 iter->he = NULL;
553 return err;
554}
555
556static int
557iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
558{
559 struct branch_info *bi;
560 struct perf_sample *sample = iter->sample;
561
562 bi = sample__resolve_bstack(sample, al);
563 if (!bi)
564 return -ENOMEM;
565
566 iter->curr = 0;
567 iter->total = sample->branch_stack->nr;
568
569 iter->priv = bi;
570 return 0;
571}
572
573static int
574iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused,
575 struct addr_location *al __maybe_unused)
576{
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900577 /* to avoid calling callback function */
578 iter->he = NULL;
579
Namhyung Kim69bcb012013-10-30 09:40:34 +0900580 return 0;
581}
582
583static int
584iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
585{
586 struct branch_info *bi = iter->priv;
587 int i = iter->curr;
588
589 if (bi == NULL)
590 return 0;
591
592 if (iter->curr >= iter->total)
593 return 0;
594
595 al->map = bi[i].to.map;
596 al->sym = bi[i].to.sym;
597 al->addr = bi[i].to.addr;
598 return 1;
599}
600
601static int
602iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
603{
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900604 struct branch_info *bi;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900605 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300606 struct hists *hists = evsel__hists(evsel);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900607 struct hist_entry *he = NULL;
608 int i = iter->curr;
609 int err = 0;
610
611 bi = iter->priv;
612
613 if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym))
614 goto out;
615
616 /*
617 * The report shows the percentage of total branches captured
618 * and not events sampled. Thus we use a pseudo period of 1.
619 */
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300620 he = __hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
Andi Kleen0e332f02015-07-18 08:24:46 -0700621 1, bi->flags.cycles ? bi->flags.cycles : 1,
622 0, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900623 if (he == NULL)
624 return -ENOMEM;
625
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300626 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900627
628out:
629 iter->he = he;
630 iter->curr++;
631 return err;
632}
633
634static int
635iter_finish_branch_entry(struct hist_entry_iter *iter,
636 struct addr_location *al __maybe_unused)
637{
638 zfree(&iter->priv);
639 iter->he = NULL;
640
641 return iter->curr >= iter->total ? 0 : -1;
642}
643
644static int
645iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
646 struct addr_location *al __maybe_unused)
647{
648 return 0;
649}
650
651static int
652iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
653{
654 struct perf_evsel *evsel = iter->evsel;
655 struct perf_sample *sample = iter->sample;
656 struct hist_entry *he;
657
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300658 he = __hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
Namhyung Kim69bcb012013-10-30 09:40:34 +0900659 sample->period, sample->weight,
Namhyung Kima0b51af2012-09-11 13:34:27 +0900660 sample->transaction, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900661 if (he == NULL)
662 return -ENOMEM;
663
664 iter->he = he;
665 return 0;
666}
667
668static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900669iter_finish_normal_entry(struct hist_entry_iter *iter,
670 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900671{
Namhyung Kim69bcb012013-10-30 09:40:34 +0900672 struct hist_entry *he = iter->he;
673 struct perf_evsel *evsel = iter->evsel;
674 struct perf_sample *sample = iter->sample;
675
676 if (he == NULL)
677 return 0;
678
679 iter->he = NULL;
680
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300681 hists__inc_nr_samples(evsel__hists(evsel), he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900682
683 return hist_entry__append_callchain(he, sample);
684}
685
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900686static int
687iter_prepare_cumulative_entry(struct hist_entry_iter *iter __maybe_unused,
688 struct addr_location *al __maybe_unused)
689{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900690 struct hist_entry **he_cache;
691
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900692 callchain_cursor_commit(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900693
694 /*
695 * This is for detecting cycles or recursions so that they're
696 * cumulated only one time to prevent entries more than 100%
697 * overhead.
698 */
699 he_cache = malloc(sizeof(*he_cache) * (PERF_MAX_STACK_DEPTH + 1));
700 if (he_cache == NULL)
701 return -ENOMEM;
702
703 iter->priv = he_cache;
704 iter->curr = 0;
705
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900706 return 0;
707}
708
709static int
710iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
711 struct addr_location *al)
712{
713 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300714 struct hists *hists = evsel__hists(evsel);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900715 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900716 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900717 struct hist_entry *he;
718 int err = 0;
719
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300720 he = __hists__add_entry(hists, al, iter->parent, NULL, NULL,
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900721 sample->period, sample->weight,
722 sample->transaction, true);
723 if (he == NULL)
724 return -ENOMEM;
725
726 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900727 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900728
Namhyung Kim82aa0192014-12-22 13:44:14 +0900729 hist_entry__append_callchain(he, sample);
Namhyung Kimbe7f8552013-12-26 17:44:10 +0900730
731 /*
732 * We need to re-initialize the cursor since callchain_append()
733 * advanced the cursor to the end.
734 */
735 callchain_cursor_commit(&callchain_cursor);
736
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300737 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900738
739 return err;
740}
741
742static int
743iter_next_cumulative_entry(struct hist_entry_iter *iter,
744 struct addr_location *al)
745{
746 struct callchain_cursor_node *node;
747
748 node = callchain_cursor_current(&callchain_cursor);
749 if (node == NULL)
750 return 0;
751
Namhyung Kimc7405d82013-10-31 13:58:30 +0900752 return fill_callchain_info(al, node, iter->hide_unresolved);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900753}
754
755static int
756iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
757 struct addr_location *al)
758{
759 struct perf_evsel *evsel = iter->evsel;
760 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900761 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900762 struct hist_entry *he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900763 struct hist_entry he_tmp = {
764 .cpu = al->cpu,
765 .thread = al->thread,
766 .comm = thread__comm(al->thread),
767 .ip = al->addr,
768 .ms = {
769 .map = al->map,
770 .sym = al->sym,
771 },
772 .parent = iter->parent,
773 };
774 int i;
Namhyung Kimbe7f8552013-12-26 17:44:10 +0900775 struct callchain_cursor cursor;
776
777 callchain_cursor_snapshot(&cursor, &callchain_cursor);
778
779 callchain_cursor_advance(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900780
781 /*
782 * Check if there's duplicate entries in the callchain.
783 * It's possible that it has cycles or recursive calls.
784 */
785 for (i = 0; i < iter->curr; i++) {
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900786 if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
787 /* to avoid calling callback function */
788 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900789 return 0;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900790 }
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900791 }
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900792
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300793 he = __hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900794 sample->period, sample->weight,
795 sample->transaction, false);
796 if (he == NULL)
797 return -ENOMEM;
798
799 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900800 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900801
Namhyung Kim82aa0192014-12-22 13:44:14 +0900802 if (symbol_conf.use_callchain)
803 callchain_append(he->callchain, &cursor, sample->period);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900804 return 0;
805}
806
807static int
808iter_finish_cumulative_entry(struct hist_entry_iter *iter,
809 struct addr_location *al __maybe_unused)
810{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900811 zfree(&iter->priv);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900812 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900813
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900814 return 0;
815}
816
Namhyung Kim69bcb012013-10-30 09:40:34 +0900817const struct hist_iter_ops hist_iter_mem = {
818 .prepare_entry = iter_prepare_mem_entry,
819 .add_single_entry = iter_add_single_mem_entry,
820 .next_entry = iter_next_nop_entry,
821 .add_next_entry = iter_add_next_nop_entry,
822 .finish_entry = iter_finish_mem_entry,
823};
824
825const struct hist_iter_ops hist_iter_branch = {
826 .prepare_entry = iter_prepare_branch_entry,
827 .add_single_entry = iter_add_single_branch_entry,
828 .next_entry = iter_next_branch_entry,
829 .add_next_entry = iter_add_next_branch_entry,
830 .finish_entry = iter_finish_branch_entry,
831};
832
833const struct hist_iter_ops hist_iter_normal = {
834 .prepare_entry = iter_prepare_normal_entry,
835 .add_single_entry = iter_add_single_normal_entry,
836 .next_entry = iter_next_nop_entry,
837 .add_next_entry = iter_add_next_nop_entry,
838 .finish_entry = iter_finish_normal_entry,
839};
840
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900841const struct hist_iter_ops hist_iter_cumulative = {
842 .prepare_entry = iter_prepare_cumulative_entry,
843 .add_single_entry = iter_add_single_cumulative_entry,
844 .next_entry = iter_next_cumulative_entry,
845 .add_next_entry = iter_add_next_cumulative_entry,
846 .finish_entry = iter_finish_cumulative_entry,
847};
848
Namhyung Kim69bcb012013-10-30 09:40:34 +0900849int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900850 int max_stack_depth, void *arg)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900851{
852 int err, err2;
853
Namhyung Kim063bd932015-05-19 17:04:10 +0900854 err = sample__resolve_callchain(iter->sample, &iter->parent,
855 iter->evsel, al, max_stack_depth);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900856 if (err)
857 return err;
858
Namhyung Kim69bcb012013-10-30 09:40:34 +0900859 err = iter->ops->prepare_entry(iter, al);
860 if (err)
861 goto out;
862
863 err = iter->ops->add_single_entry(iter, al);
864 if (err)
865 goto out;
866
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900867 if (iter->he && iter->add_entry_cb) {
868 err = iter->add_entry_cb(iter, al, true, arg);
869 if (err)
870 goto out;
871 }
872
Namhyung Kim69bcb012013-10-30 09:40:34 +0900873 while (iter->ops->next_entry(iter, al)) {
874 err = iter->ops->add_next_entry(iter, al);
875 if (err)
876 break;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900877
878 if (iter->he && iter->add_entry_cb) {
879 err = iter->add_entry_cb(iter, al, false, arg);
880 if (err)
881 goto out;
882 }
Namhyung Kim69bcb012013-10-30 09:40:34 +0900883 }
884
885out:
886 err2 = iter->ops->finish_entry(iter, al);
887 if (!err)
888 err = err2;
889
890 return err;
891}
892
John Kacur3d1d07e2009-09-28 15:32:55 +0200893int64_t
894hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
895{
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900896 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +0200897 int64_t cmp = 0;
898
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900899 perf_hpp__for_each_sort_list(fmt) {
Namhyung Kime67d49a2014-03-18 13:00:59 +0900900 if (perf_hpp__should_skip(fmt))
901 continue;
902
Namhyung Kim87bbdf72015-01-08 09:45:46 +0900903 cmp = fmt->cmp(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200904 if (cmp)
905 break;
906 }
907
908 return cmp;
909}
910
911int64_t
912hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
913{
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900914 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +0200915 int64_t cmp = 0;
916
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900917 perf_hpp__for_each_sort_list(fmt) {
Namhyung Kime67d49a2014-03-18 13:00:59 +0900918 if (perf_hpp__should_skip(fmt))
919 continue;
920
Namhyung Kim87bbdf72015-01-08 09:45:46 +0900921 cmp = fmt->collapse(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200922 if (cmp)
923 break;
924 }
925
926 return cmp;
927}
928
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -0300929void hist_entry__delete(struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +0200930{
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300931 thread__zput(he->thread);
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300932 map__zput(he->ms.map);
933
934 if (he->branch_info) {
935 map__zput(he->branch_info->from.map);
936 map__zput(he->branch_info->to.map);
937 zfree(&he->branch_info);
938 }
939
940 if (he->mem_info) {
941 map__zput(he->mem_info->iaddr.map);
942 map__zput(he->mem_info->daddr.map);
943 zfree(&he->mem_info);
944 }
945
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900946 zfree(&he->stat_acc);
Namhyung Kimf048d542013-09-11 14:09:28 +0900947 free_srcline(he->srcline);
Namhyung Kimd1149602014-12-30 14:38:13 +0900948 free_callchain(he->callchain);
John Kacur3d1d07e2009-09-28 15:32:55 +0200949 free(he);
950}
951
952/*
953 * collapse the histogram
954 */
955
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300956static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100957 struct rb_root *root,
958 struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +0200959{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200960 struct rb_node **p = &root->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200961 struct rb_node *parent = NULL;
962 struct hist_entry *iter;
963 int64_t cmp;
964
965 while (*p != NULL) {
966 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300967 iter = rb_entry(parent, struct hist_entry, rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +0200968
969 cmp = hist_entry__collapse(iter, he);
970
971 if (!cmp) {
Namhyung Kim139c0812012-10-04 21:49:43 +0900972 he_stat__add_stat(&iter->stat, &he->stat);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900973 if (symbol_conf.cumulate_callchain)
974 he_stat__add_stat(iter->stat_acc, he->stat_acc);
Namhyung Kim9ec60972012-09-26 16:47:28 +0900975
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100976 if (symbol_conf.use_callchain) {
Namhyung Kim47260642012-05-31 14:43:26 +0900977 callchain_cursor_reset(&callchain_cursor);
978 callchain_merge(&callchain_cursor,
979 iter->callchain,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100980 he->callchain);
981 }
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -0300982 hist_entry__delete(he);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300983 return false;
John Kacur3d1d07e2009-09-28 15:32:55 +0200984 }
985
986 if (cmp < 0)
987 p = &(*p)->rb_left;
988 else
989 p = &(*p)->rb_right;
990 }
Namhyung Kim740b97f2014-12-22 13:44:10 +0900991 hists->nr_entries++;
John Kacur3d1d07e2009-09-28 15:32:55 +0200992
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300993 rb_link_node(&he->rb_node_in, parent, p);
994 rb_insert_color(&he->rb_node_in, root);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300995 return true;
John Kacur3d1d07e2009-09-28 15:32:55 +0200996}
997
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300998static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
999{
1000 struct rb_root *root;
1001
1002 pthread_mutex_lock(&hists->lock);
1003
1004 root = hists->entries_in;
1005 if (++hists->entries_in > &hists->entries_in_array[1])
1006 hists->entries_in = &hists->entries_in_array[0];
1007
1008 pthread_mutex_unlock(&hists->lock);
1009
1010 return root;
1011}
1012
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001013static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
1014{
1015 hists__filter_entry_by_dso(hists, he);
1016 hists__filter_entry_by_thread(hists, he);
Namhyung Kime94d53e2012-03-16 17:50:51 +09001017 hists__filter_entry_by_symbol(hists, he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001018}
1019
Namhyung Kimc1fb5652013-10-11 14:15:38 +09001020void hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001021{
1022 struct rb_root *root;
1023 struct rb_node *next;
1024 struct hist_entry *n;
1025
Namhyung Kim3a5714f2013-05-14 11:09:01 +09001026 if (!sort__need_collapse)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001027 return;
1028
Namhyung Kim740b97f2014-12-22 13:44:10 +09001029 hists->nr_entries = 0;
1030
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001031 root = hists__get_rotate_entries_in(hists);
Namhyung Kim740b97f2014-12-22 13:44:10 +09001032
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001033 next = rb_first(root);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001034
1035 while (next) {
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001036 if (session_done())
1037 break;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001038 n = rb_entry(next, struct hist_entry, rb_node_in);
1039 next = rb_next(&n->rb_node_in);
1040
1041 rb_erase(&n->rb_node_in, root);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001042 if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
1043 /*
1044 * If it wasn't combined with one of the entries already
1045 * collapsed, we need to apply the filters that may have
1046 * been set by, say, the hist_browser.
1047 */
1048 hists__apply_filters(hists, n);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001049 }
Namhyung Kimc1fb5652013-10-11 14:15:38 +09001050 if (prog)
1051 ui_progress__update(prog, 1);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001052 }
1053}
1054
Namhyung Kim043ca3892014-03-03 14:18:00 +09001055static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001056{
Namhyung Kim043ca3892014-03-03 14:18:00 +09001057 struct perf_hpp_fmt *fmt;
1058 int64_t cmp = 0;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001059
Namhyung Kim26d8b332014-03-03 16:16:20 +09001060 perf_hpp__for_each_sort_list(fmt) {
Namhyung Kime67d49a2014-03-18 13:00:59 +09001061 if (perf_hpp__should_skip(fmt))
1062 continue;
1063
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001064 cmp = fmt->sort(fmt, a, b);
Namhyung Kim043ca3892014-03-03 14:18:00 +09001065 if (cmp)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001066 break;
1067 }
1068
Namhyung Kim043ca3892014-03-03 14:18:00 +09001069 return cmp;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001070}
1071
Namhyung Kim9283ba92014-04-24 16:37:26 +09001072static void hists__reset_filter_stats(struct hists *hists)
1073{
1074 hists->nr_non_filtered_entries = 0;
1075 hists->stats.total_non_filtered_period = 0;
1076}
1077
1078void hists__reset_stats(struct hists *hists)
1079{
1080 hists->nr_entries = 0;
1081 hists->stats.total_period = 0;
1082
1083 hists__reset_filter_stats(hists);
1084}
1085
1086static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
1087{
1088 hists->nr_non_filtered_entries++;
1089 hists->stats.total_non_filtered_period += h->stat.period;
1090}
1091
1092void hists__inc_stats(struct hists *hists, struct hist_entry *h)
1093{
1094 if (!h->filtered)
1095 hists__inc_filter_stats(hists, h);
1096
1097 hists->nr_entries++;
1098 hists->stats.total_period += h->stat.period;
1099}
1100
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001101static void __hists__insert_output_entry(struct rb_root *entries,
1102 struct hist_entry *he,
1103 u64 min_callchain_hits)
John Kacur3d1d07e2009-09-28 15:32:55 +02001104{
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001105 struct rb_node **p = &entries->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +02001106 struct rb_node *parent = NULL;
1107 struct hist_entry *iter;
1108
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -02001109 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melob9fb9302010-04-02 09:50:42 -03001110 callchain_param.sort(&he->sorted_chain, he->callchain,
John Kacur3d1d07e2009-09-28 15:32:55 +02001111 min_callchain_hits, &callchain_param);
1112
1113 while (*p != NULL) {
1114 parent = *p;
1115 iter = rb_entry(parent, struct hist_entry, rb_node);
1116
Namhyung Kim043ca3892014-03-03 14:18:00 +09001117 if (hist_entry__sort(he, iter) > 0)
John Kacur3d1d07e2009-09-28 15:32:55 +02001118 p = &(*p)->rb_left;
1119 else
1120 p = &(*p)->rb_right;
1121 }
1122
1123 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001124 rb_insert_color(&he->rb_node, entries);
John Kacur3d1d07e2009-09-28 15:32:55 +02001125}
1126
Namhyung Kim740b97f2014-12-22 13:44:10 +09001127void hists__output_resort(struct hists *hists, struct ui_progress *prog)
John Kacur3d1d07e2009-09-28 15:32:55 +02001128{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001129 struct rb_root *root;
John Kacur3d1d07e2009-09-28 15:32:55 +02001130 struct rb_node *next;
1131 struct hist_entry *n;
John Kacur3d1d07e2009-09-28 15:32:55 +02001132 u64 min_callchain_hits;
1133
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001134 min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
John Kacur3d1d07e2009-09-28 15:32:55 +02001135
Namhyung Kim3a5714f2013-05-14 11:09:01 +09001136 if (sort__need_collapse)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001137 root = &hists->entries_collapsed;
1138 else
1139 root = hists->entries_in;
1140
1141 next = rb_first(root);
1142 hists->entries = RB_ROOT;
John Kacur3d1d07e2009-09-28 15:32:55 +02001143
Namhyung Kim9283ba92014-04-24 16:37:26 +09001144 hists__reset_stats(hists);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001145 hists__reset_col_len(hists);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -03001146
John Kacur3d1d07e2009-09-28 15:32:55 +02001147 while (next) {
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001148 n = rb_entry(next, struct hist_entry, rb_node_in);
1149 next = rb_next(&n->rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +02001150
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001151 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
Namhyung Kim62638352014-04-24 16:21:46 +09001152 hists__inc_stats(hists, n);
Namhyung Kimae993ef2014-04-24 16:25:19 +09001153
1154 if (!n->filtered)
1155 hists__calc_col_len(hists, n);
Namhyung Kim740b97f2014-12-22 13:44:10 +09001156
1157 if (prog)
1158 ui_progress__update(prog, 1);
John Kacur3d1d07e2009-09-28 15:32:55 +02001159 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001160}
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -02001161
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001162static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001163 enum hist_filter filter)
1164{
1165 h->filtered &= ~(1 << filter);
1166 if (h->filtered)
1167 return;
1168
Namhyung Kim87e90f42014-04-24 16:44:16 +09001169 /* force fold unfiltered entry for simplicity */
Namhyung Kim3698dab2015-05-05 23:55:46 +09001170 h->unfolded = false;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -03001171 h->row_offset = 0;
He Kuanga8cd1f42015-03-11 20:36:03 +08001172 h->nr_rows = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001173
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001174 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001175
Namhyung Kim9283ba92014-04-24 16:37:26 +09001176 hists__inc_filter_stats(hists, h);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001177 hists__calc_col_len(hists, h);
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001178}
1179
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001180
1181static bool hists__filter_entry_by_dso(struct hists *hists,
1182 struct hist_entry *he)
1183{
1184 if (hists->dso_filter != NULL &&
1185 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
1186 he->filtered |= (1 << HIST_FILTER__DSO);
1187 return true;
1188 }
1189
1190 return false;
1191}
1192
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001193void hists__filter_by_dso(struct hists *hists)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001194{
1195 struct rb_node *nd;
1196
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001197 hists->stats.nr_non_filtered_samples = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001198
1199 hists__reset_filter_stats(hists);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001200 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001201
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001202 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001203 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1204
1205 if (symbol_conf.exclude_other && !h->parent)
1206 continue;
1207
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001208 if (hists__filter_entry_by_dso(hists, h))
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001209 continue;
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001210
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001211 hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001212 }
1213}
1214
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001215static bool hists__filter_entry_by_thread(struct hists *hists,
1216 struct hist_entry *he)
1217{
1218 if (hists->thread_filter != NULL &&
1219 he->thread != hists->thread_filter) {
1220 he->filtered |= (1 << HIST_FILTER__THREAD);
1221 return true;
1222 }
1223
1224 return false;
1225}
1226
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001227void hists__filter_by_thread(struct hists *hists)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001228{
1229 struct rb_node *nd;
1230
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001231 hists->stats.nr_non_filtered_samples = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001232
1233 hists__reset_filter_stats(hists);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001234 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001235
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001236 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001237 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1238
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001239 if (hists__filter_entry_by_thread(hists, h))
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001240 continue;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001241
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001242 hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001243 }
1244}
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001245
Namhyung Kime94d53e2012-03-16 17:50:51 +09001246static bool hists__filter_entry_by_symbol(struct hists *hists,
1247 struct hist_entry *he)
1248{
1249 if (hists->symbol_filter_str != NULL &&
1250 (!he->ms.sym || strstr(he->ms.sym->name,
1251 hists->symbol_filter_str) == NULL)) {
1252 he->filtered |= (1 << HIST_FILTER__SYMBOL);
1253 return true;
1254 }
1255
1256 return false;
1257}
1258
1259void hists__filter_by_symbol(struct hists *hists)
1260{
1261 struct rb_node *nd;
1262
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001263 hists->stats.nr_non_filtered_samples = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001264
1265 hists__reset_filter_stats(hists);
Namhyung Kime94d53e2012-03-16 17:50:51 +09001266 hists__reset_col_len(hists);
1267
1268 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1269 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1270
1271 if (hists__filter_entry_by_symbol(hists, h))
1272 continue;
1273
1274 hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
1275 }
1276}
1277
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001278void events_stats__inc(struct events_stats *stats, u32 type)
1279{
1280 ++stats->nr_events[0];
1281 ++stats->nr_events[type];
1282}
1283
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001284void hists__inc_nr_events(struct hists *hists, u32 type)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001285{
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001286 events_stats__inc(&hists->stats, type);
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001287}
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001288
Namhyung Kim1844dbc2014-05-28 14:12:18 +09001289void hists__inc_nr_samples(struct hists *hists, bool filtered)
1290{
1291 events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
1292 if (!filtered)
1293 hists->stats.nr_non_filtered_samples++;
1294}
1295
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001296static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
1297 struct hist_entry *pair)
1298{
Namhyung Kimce74f602012-12-10 17:29:55 +09001299 struct rb_root *root;
1300 struct rb_node **p;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001301 struct rb_node *parent = NULL;
1302 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -07001303 int64_t cmp;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001304
Namhyung Kimce74f602012-12-10 17:29:55 +09001305 if (sort__need_collapse)
1306 root = &hists->entries_collapsed;
1307 else
1308 root = hists->entries_in;
1309
1310 p = &root->rb_node;
1311
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001312 while (*p != NULL) {
1313 parent = *p;
Namhyung Kimce74f602012-12-10 17:29:55 +09001314 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001315
Namhyung Kimce74f602012-12-10 17:29:55 +09001316 cmp = hist_entry__collapse(he, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001317
1318 if (!cmp)
1319 goto out;
1320
1321 if (cmp < 0)
1322 p = &(*p)->rb_left;
1323 else
1324 p = &(*p)->rb_right;
1325 }
1326
Namhyung Kima0b51af2012-09-11 13:34:27 +09001327 he = hist_entry__new(pair, true);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001328 if (he) {
Arnaldo Carvalho de Melo30193d72012-11-12 13:20:03 -03001329 memset(&he->stat, 0, sizeof(he->stat));
1330 he->hists = hists;
Namhyung Kimce74f602012-12-10 17:29:55 +09001331 rb_link_node(&he->rb_node_in, parent, p);
1332 rb_insert_color(&he->rb_node_in, root);
Namhyung Kim62638352014-04-24 16:21:46 +09001333 hists__inc_stats(hists, he);
Jiri Olsae0af43d2012-12-01 21:18:20 +01001334 he->dummy = true;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001335 }
1336out:
1337 return he;
1338}
1339
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001340static struct hist_entry *hists__find_entry(struct hists *hists,
1341 struct hist_entry *he)
1342{
Namhyung Kimce74f602012-12-10 17:29:55 +09001343 struct rb_node *n;
1344
1345 if (sort__need_collapse)
1346 n = hists->entries_collapsed.rb_node;
1347 else
1348 n = hists->entries_in->rb_node;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001349
1350 while (n) {
Namhyung Kimce74f602012-12-10 17:29:55 +09001351 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
1352 int64_t cmp = hist_entry__collapse(iter, he);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001353
1354 if (cmp < 0)
1355 n = n->rb_left;
1356 else if (cmp > 0)
1357 n = n->rb_right;
1358 else
1359 return iter;
1360 }
1361
1362 return NULL;
1363}
1364
1365/*
1366 * Look for pairs to link to the leader buckets (hist_entries):
1367 */
1368void hists__match(struct hists *leader, struct hists *other)
1369{
Namhyung Kimce74f602012-12-10 17:29:55 +09001370 struct rb_root *root;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001371 struct rb_node *nd;
1372 struct hist_entry *pos, *pair;
1373
Namhyung Kimce74f602012-12-10 17:29:55 +09001374 if (sort__need_collapse)
1375 root = &leader->entries_collapsed;
1376 else
1377 root = leader->entries_in;
1378
1379 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
1380 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001381 pair = hists__find_entry(other, pos);
1382
1383 if (pair)
Namhyung Kim5fa90412012-11-29 15:38:34 +09001384 hist_entry__add_pair(pair, pos);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001385 }
1386}
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001387
1388/*
1389 * Look for entries in the other hists that are not present in the leader, if
1390 * we find them, just add a dummy entry on the leader hists, with period=0,
1391 * nr_events=0, to serve as the list header.
1392 */
1393int hists__link(struct hists *leader, struct hists *other)
1394{
Namhyung Kimce74f602012-12-10 17:29:55 +09001395 struct rb_root *root;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001396 struct rb_node *nd;
1397 struct hist_entry *pos, *pair;
1398
Namhyung Kimce74f602012-12-10 17:29:55 +09001399 if (sort__need_collapse)
1400 root = &other->entries_collapsed;
1401 else
1402 root = other->entries_in;
1403
1404 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
1405 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001406
1407 if (!hist_entry__has_pairs(pos)) {
1408 pair = hists__add_dummy_entry(leader, pos);
1409 if (pair == NULL)
1410 return -1;
Namhyung Kim5fa90412012-11-29 15:38:34 +09001411 hist_entry__add_pair(pos, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001412 }
1413 }
1414
1415 return 0;
1416}
Namhyung Kimf2148332014-01-14 11:52:48 +09001417
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03001418
1419size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp)
1420{
1421 struct perf_evsel *pos;
1422 size_t ret = 0;
1423
1424 evlist__for_each(evlist, pos) {
1425 ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
1426 ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
1427 }
1428
1429 return ret;
1430}
1431
1432
Namhyung Kimf2148332014-01-14 11:52:48 +09001433u64 hists__total_period(struct hists *hists)
1434{
1435 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
1436 hists->stats.total_period;
1437}
Namhyung Kim33db4562014-02-07 12:06:07 +09001438
1439int parse_filter_percentage(const struct option *opt __maybe_unused,
1440 const char *arg, int unset __maybe_unused)
1441{
1442 if (!strcmp(arg, "relative"))
1443 symbol_conf.filter_relative = true;
1444 else if (!strcmp(arg, "absolute"))
1445 symbol_conf.filter_relative = false;
1446 else
1447 return -1;
1448
1449 return 0;
1450}
Namhyung Kim0b93da12014-01-14 12:02:15 +09001451
1452int perf_hist_config(const char *var, const char *value)
1453{
1454 if (!strcmp(var, "hist.percentage"))
1455 return parse_filter_percentage(NULL, value, 0);
1456
1457 return 0;
1458}
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03001459
1460static int hists_evsel__init(struct perf_evsel *evsel)
1461{
1462 struct hists *hists = evsel__hists(evsel);
1463
1464 memset(hists, 0, sizeof(*hists));
1465 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
1466 hists->entries_in = &hists->entries_in_array[0];
1467 hists->entries_collapsed = RB_ROOT;
1468 hists->entries = RB_ROOT;
1469 pthread_mutex_init(&hists->lock, NULL);
1470 return 0;
1471}
1472
1473/*
1474 * XXX We probably need a hists_evsel__exit() to free the hist_entries
1475 * stored in the rbtree...
1476 */
1477
1478int hists__init(void)
1479{
1480 int err = perf_evsel__object_config(sizeof(struct hists_evsel),
1481 hists_evsel__init, NULL);
1482 if (err)
1483 fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);
1484
1485 return err;
1486}