blob: 038483a24a54633c8d86f69639c18d2f24edb4ea [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 Meloab81f3fd2011-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 Meloab81f3fd2011-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 Meloab81f3fd2011-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 Meloab81f3fd2011-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 Melodf71d952011-10-13 08:01:33 -0300266 /*
267 * We may be annotating this, for instance, so keep it here in
268 * case some it gets new samples, we'll eventually free it when
269 * the user stops browsing and it agains gets fully decayed.
270 */
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200271 if (((zap_user && n->level == '.') ||
272 (zap_kernel && n->level != '.') ||
273 hists__decay_entry(hists, n)) &&
274 !n->used) {
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300275 hists__delete_entry(hists, n);
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300276 }
277 }
278}
279
Namhyung Kim701937b2014-08-12 17:16:05 +0900280void hists__delete_entries(struct hists *hists)
281{
282 struct rb_node *next = rb_first(&hists->entries);
283 struct hist_entry *n;
284
285 while (next) {
286 n = rb_entry(next, struct hist_entry, rb_node);
287 next = rb_next(&n->rb_node);
288
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300289 hists__delete_entry(hists, n);
Namhyung Kim701937b2014-08-12 17:16:05 +0900290 }
291}
292
John Kacur3d1d07e2009-09-28 15:32:55 +0200293/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300294 * histogram, sorted on item, collects periods
John Kacur3d1d07e2009-09-28 15:32:55 +0200295 */
296
Namhyung Kima0b51af2012-09-11 13:34:27 +0900297static struct hist_entry *hist_entry__new(struct hist_entry *template,
298 bool sample_self)
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300299{
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900300 size_t callchain_size = 0;
301 struct hist_entry *he;
302
Namhyung Kim82aa0192014-12-22 13:44:14 +0900303 if (symbol_conf.use_callchain)
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900304 callchain_size = sizeof(struct callchain_root);
305
306 he = zalloc(sizeof(*he) + callchain_size);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300307
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200308 if (he != NULL) {
309 *he = *template;
Namhyung Kimc4b35352012-10-04 21:49:42 +0900310
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900311 if (symbol_conf.cumulate_callchain) {
312 he->stat_acc = malloc(sizeof(he->stat));
313 if (he->stat_acc == NULL) {
314 free(he);
315 return NULL;
316 }
317 memcpy(he->stat_acc, &he->stat, sizeof(he->stat));
Namhyung Kima0b51af2012-09-11 13:34:27 +0900318 if (!sample_self)
319 memset(&he->stat, 0, sizeof(he->stat));
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900320 }
321
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200322 if (he->ms.map)
323 he->ms.map->referenced = true;
Stephane Eranian3cf0cb12013-01-14 15:02:45 +0100324
325 if (he->branch_info) {
Namhyung Kim26353a62013-04-01 20:35:17 +0900326 /*
327 * This branch info is (a part of) allocated from
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -0300328 * sample__resolve_bstack() and will be freed after
Namhyung Kim26353a62013-04-01 20:35:17 +0900329 * adding new entries. So we need to save a copy.
330 */
331 he->branch_info = malloc(sizeof(*he->branch_info));
332 if (he->branch_info == NULL) {
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900333 free(he->stat_acc);
Namhyung Kim26353a62013-04-01 20:35:17 +0900334 free(he);
335 return NULL;
336 }
337
338 memcpy(he->branch_info, template->branch_info,
339 sizeof(*he->branch_info));
340
Stephane Eranian3cf0cb12013-01-14 15:02:45 +0100341 if (he->branch_info->from.map)
342 he->branch_info->from.map->referenced = true;
343 if (he->branch_info->to.map)
344 he->branch_info->to.map->referenced = true;
345 }
346
Stephane Eranian98a3b322013-01-24 16:10:35 +0100347 if (he->mem_info) {
348 if (he->mem_info->iaddr.map)
349 he->mem_info->iaddr.map->referenced = true;
350 if (he->mem_info->daddr.map)
351 he->mem_info->daddr.map->referenced = true;
352 }
353
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300354 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200355 callchain_init(he->callchain);
Arnaldo Carvalho de Melob821c732012-10-25 14:42:45 -0200356
357 INIT_LIST_HEAD(&he->pairs.node);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300358 }
359
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200360 return he;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300361}
362
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300363static u8 symbol__parent_filter(const struct symbol *parent)
364{
365 if (symbol_conf.exclude_other && parent == NULL)
366 return 1 << HIST_FILTER__PARENT;
367 return 0;
368}
369
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100370static struct hist_entry *add_hist_entry(struct hists *hists,
Namhyung Kimf1cbf782013-12-18 14:21:11 +0900371 struct hist_entry *entry,
Namhyung Kima0b51af2012-09-11 13:34:27 +0900372 struct addr_location *al,
373 bool sample_self)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300374{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300375 struct rb_node **p;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300376 struct rb_node *parent = NULL;
377 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -0700378 int64_t cmp;
Namhyung Kimf1cbf782013-12-18 14:21:11 +0900379 u64 period = entry->stat.period;
380 u64 weight = entry->stat.weight;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300381
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300382 p = &hists->entries_in->rb_node;
383
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300384 while (*p != NULL) {
385 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300386 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300387
Namhyung Kim9afcf932012-12-10 17:29:54 +0900388 /*
389 * Make sure that it receives arguments in a same order as
390 * hist_entry__collapse() so that we can use an appropriate
391 * function when searching an entry regardless which sort
392 * keys were used.
393 */
394 cmp = hist_entry__cmp(he, entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300395
396 if (!cmp) {
Namhyung Kima0b51af2012-09-11 13:34:27 +0900397 if (sample_self)
398 he_stat__add_period(&he->stat, period, weight);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900399 if (symbol_conf.cumulate_callchain)
400 he_stat__add_period(he->stat_acc, period, weight);
David Miller63fa4712012-03-27 03:14:18 -0400401
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900402 /*
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -0300403 * This mem info was allocated from sample__resolve_mem
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900404 * and will not be used anymore.
405 */
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300406 zfree(&entry->mem_info);
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900407
David Miller63fa4712012-03-27 03:14:18 -0400408 /* If the map of an existing hist_entry has
409 * become out-of-date due to an exec() or
410 * similar, update it. Otherwise we will
411 * mis-adjust symbol addresses when computing
412 * the history counter to increment.
413 */
414 if (he->ms.map != entry->ms.map) {
415 he->ms.map = entry->ms.map;
416 if (he->ms.map)
417 he->ms.map->referenced = true;
418 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300419 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300420 }
421
422 if (cmp < 0)
423 p = &(*p)->rb_left;
424 else
425 p = &(*p)->rb_right;
426 }
427
Namhyung Kima0b51af2012-09-11 13:34:27 +0900428 he = hist_entry__new(entry, sample_self);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300429 if (!he)
Namhyung Kim27a0dcb2013-05-14 11:09:02 +0900430 return NULL;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300431
432 rb_link_node(&he->rb_node_in, parent, p);
433 rb_insert_color(&he->rb_node_in, hists->entries_in);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300434out:
Namhyung Kima0b51af2012-09-11 13:34:27 +0900435 if (sample_self)
436 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900437 if (symbol_conf.cumulate_callchain)
438 he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300439 return he;
440}
441
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300442struct hist_entry *__hists__add_entry(struct hists *hists,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100443 struct addr_location *al,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900444 struct symbol *sym_parent,
445 struct branch_info *bi,
446 struct mem_info *mi,
Namhyung Kima0b51af2012-09-11 13:34:27 +0900447 u64 period, u64 weight, u64 transaction,
448 bool sample_self)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100449{
450 struct hist_entry entry = {
451 .thread = al->thread,
Namhyung Kim4dfced32013-09-13 16:28:57 +0900452 .comm = thread__comm(al->thread),
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100453 .ms = {
454 .map = al->map,
455 .sym = al->sym,
456 },
Don Zickus7365be52014-05-27 12:28:05 -0400457 .cpu = al->cpu,
458 .cpumode = al->cpumode,
459 .ip = al->addr,
460 .level = al->level,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900461 .stat = {
Namhyung Kimc4b35352012-10-04 21:49:42 +0900462 .nr_events = 1,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900463 .period = period,
Andi Kleen05484292013-01-24 16:10:29 +0100464 .weight = weight,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900465 },
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100466 .parent = sym_parent,
Namhyung Kim2c86c7c2014-03-17 18:18:54 -0300467 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300468 .hists = hists,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900469 .branch_info = bi,
470 .mem_info = mi,
Andi Kleen475eeab2013-09-20 07:40:43 -0700471 .transaction = transaction,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100472 };
473
Namhyung Kima0b51af2012-09-11 13:34:27 +0900474 return add_hist_entry(hists, &entry, al, sample_self);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100475}
476
Namhyung Kim69bcb012013-10-30 09:40:34 +0900477static int
478iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
479 struct addr_location *al __maybe_unused)
480{
481 return 0;
482}
483
484static int
485iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
486 struct addr_location *al __maybe_unused)
487{
488 return 0;
489}
490
491static int
492iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
493{
494 struct perf_sample *sample = iter->sample;
495 struct mem_info *mi;
496
497 mi = sample__resolve_mem(sample, al);
498 if (mi == NULL)
499 return -ENOMEM;
500
501 iter->priv = mi;
502 return 0;
503}
504
505static int
506iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
507{
508 u64 cost;
509 struct mem_info *mi = iter->priv;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300510 struct hists *hists = evsel__hists(iter->evsel);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900511 struct hist_entry *he;
512
513 if (mi == NULL)
514 return -EINVAL;
515
516 cost = iter->sample->weight;
517 if (!cost)
518 cost = 1;
519
520 /*
521 * must pass period=weight in order to get the correct
522 * sorting from hists__collapse_resort() which is solely
523 * based on periods. We want sorting be done on nr_events * weight
524 * and this is indirectly achieved by passing period=weight here
525 * and the he_stat__add_period() function.
526 */
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300527 he = __hists__add_entry(hists, al, iter->parent, NULL, mi,
Namhyung Kima0b51af2012-09-11 13:34:27 +0900528 cost, cost, 0, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900529 if (!he)
530 return -ENOMEM;
531
532 iter->he = he;
533 return 0;
534}
535
536static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900537iter_finish_mem_entry(struct hist_entry_iter *iter,
538 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900539{
540 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300541 struct hists *hists = evsel__hists(evsel);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900542 struct hist_entry *he = iter->he;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900543 int err = -EINVAL;
544
545 if (he == NULL)
546 goto out;
547
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300548 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900549
550 err = hist_entry__append_callchain(he, iter->sample);
551
552out:
553 /*
554 * We don't need to free iter->priv (mem_info) here since
555 * the mem info was either already freed in add_hist_entry() or
556 * passed to a new hist entry by hist_entry__new().
557 */
558 iter->priv = NULL;
559
560 iter->he = NULL;
561 return err;
562}
563
564static int
565iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
566{
567 struct branch_info *bi;
568 struct perf_sample *sample = iter->sample;
569
570 bi = sample__resolve_bstack(sample, al);
571 if (!bi)
572 return -ENOMEM;
573
574 iter->curr = 0;
575 iter->total = sample->branch_stack->nr;
576
577 iter->priv = bi;
578 return 0;
579}
580
581static int
582iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused,
583 struct addr_location *al __maybe_unused)
584{
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900585 /* to avoid calling callback function */
586 iter->he = NULL;
587
Namhyung Kim69bcb012013-10-30 09:40:34 +0900588 return 0;
589}
590
591static int
592iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
593{
594 struct branch_info *bi = iter->priv;
595 int i = iter->curr;
596
597 if (bi == NULL)
598 return 0;
599
600 if (iter->curr >= iter->total)
601 return 0;
602
603 al->map = bi[i].to.map;
604 al->sym = bi[i].to.sym;
605 al->addr = bi[i].to.addr;
606 return 1;
607}
608
609static int
610iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
611{
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900612 struct branch_info *bi;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900613 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300614 struct hists *hists = evsel__hists(evsel);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900615 struct hist_entry *he = NULL;
616 int i = iter->curr;
617 int err = 0;
618
619 bi = iter->priv;
620
621 if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym))
622 goto out;
623
624 /*
625 * The report shows the percentage of total branches captured
626 * and not events sampled. Thus we use a pseudo period of 1.
627 */
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300628 he = __hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
Namhyung Kima0b51af2012-09-11 13:34:27 +0900629 1, 1, 0, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900630 if (he == NULL)
631 return -ENOMEM;
632
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300633 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900634
635out:
636 iter->he = he;
637 iter->curr++;
638 return err;
639}
640
641static int
642iter_finish_branch_entry(struct hist_entry_iter *iter,
643 struct addr_location *al __maybe_unused)
644{
645 zfree(&iter->priv);
646 iter->he = NULL;
647
648 return iter->curr >= iter->total ? 0 : -1;
649}
650
651static int
652iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
653 struct addr_location *al __maybe_unused)
654{
655 return 0;
656}
657
658static int
659iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
660{
661 struct perf_evsel *evsel = iter->evsel;
662 struct perf_sample *sample = iter->sample;
663 struct hist_entry *he;
664
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300665 he = __hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
Namhyung Kim69bcb012013-10-30 09:40:34 +0900666 sample->period, sample->weight,
Namhyung Kima0b51af2012-09-11 13:34:27 +0900667 sample->transaction, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900668 if (he == NULL)
669 return -ENOMEM;
670
671 iter->he = he;
672 return 0;
673}
674
675static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900676iter_finish_normal_entry(struct hist_entry_iter *iter,
677 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900678{
Namhyung Kim69bcb012013-10-30 09:40:34 +0900679 struct hist_entry *he = iter->he;
680 struct perf_evsel *evsel = iter->evsel;
681 struct perf_sample *sample = iter->sample;
682
683 if (he == NULL)
684 return 0;
685
686 iter->he = NULL;
687
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300688 hists__inc_nr_samples(evsel__hists(evsel), he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900689
690 return hist_entry__append_callchain(he, sample);
691}
692
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900693static int
694iter_prepare_cumulative_entry(struct hist_entry_iter *iter __maybe_unused,
695 struct addr_location *al __maybe_unused)
696{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900697 struct hist_entry **he_cache;
698
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900699 callchain_cursor_commit(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900700
701 /*
702 * This is for detecting cycles or recursions so that they're
703 * cumulated only one time to prevent entries more than 100%
704 * overhead.
705 */
706 he_cache = malloc(sizeof(*he_cache) * (PERF_MAX_STACK_DEPTH + 1));
707 if (he_cache == NULL)
708 return -ENOMEM;
709
710 iter->priv = he_cache;
711 iter->curr = 0;
712
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900713 return 0;
714}
715
716static int
717iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
718 struct addr_location *al)
719{
720 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300721 struct hists *hists = evsel__hists(evsel);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900722 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900723 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900724 struct hist_entry *he;
725 int err = 0;
726
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300727 he = __hists__add_entry(hists, al, iter->parent, NULL, NULL,
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900728 sample->period, sample->weight,
729 sample->transaction, true);
730 if (he == NULL)
731 return -ENOMEM;
732
733 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900734 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900735
Namhyung Kim82aa0192014-12-22 13:44:14 +0900736 hist_entry__append_callchain(he, sample);
Namhyung Kimbe7f8552013-12-26 17:44:10 +0900737
738 /*
739 * We need to re-initialize the cursor since callchain_append()
740 * advanced the cursor to the end.
741 */
742 callchain_cursor_commit(&callchain_cursor);
743
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300744 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900745
746 return err;
747}
748
749static int
750iter_next_cumulative_entry(struct hist_entry_iter *iter,
751 struct addr_location *al)
752{
753 struct callchain_cursor_node *node;
754
755 node = callchain_cursor_current(&callchain_cursor);
756 if (node == NULL)
757 return 0;
758
Namhyung Kimc7405d82013-10-31 13:58:30 +0900759 return fill_callchain_info(al, node, iter->hide_unresolved);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900760}
761
762static int
763iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
764 struct addr_location *al)
765{
766 struct perf_evsel *evsel = iter->evsel;
767 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900768 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900769 struct hist_entry *he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900770 struct hist_entry he_tmp = {
771 .cpu = al->cpu,
772 .thread = al->thread,
773 .comm = thread__comm(al->thread),
774 .ip = al->addr,
775 .ms = {
776 .map = al->map,
777 .sym = al->sym,
778 },
779 .parent = iter->parent,
780 };
781 int i;
Namhyung Kimbe7f8552013-12-26 17:44:10 +0900782 struct callchain_cursor cursor;
783
784 callchain_cursor_snapshot(&cursor, &callchain_cursor);
785
786 callchain_cursor_advance(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900787
788 /*
789 * Check if there's duplicate entries in the callchain.
790 * It's possible that it has cycles or recursive calls.
791 */
792 for (i = 0; i < iter->curr; i++) {
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900793 if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
794 /* to avoid calling callback function */
795 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900796 return 0;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900797 }
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900798 }
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900799
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300800 he = __hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900801 sample->period, sample->weight,
802 sample->transaction, false);
803 if (he == NULL)
804 return -ENOMEM;
805
806 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900807 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900808
Namhyung Kim82aa0192014-12-22 13:44:14 +0900809 if (symbol_conf.use_callchain)
810 callchain_append(he->callchain, &cursor, sample->period);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900811 return 0;
812}
813
814static int
815iter_finish_cumulative_entry(struct hist_entry_iter *iter,
816 struct addr_location *al __maybe_unused)
817{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900818 zfree(&iter->priv);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900819 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900820
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900821 return 0;
822}
823
Namhyung Kim69bcb012013-10-30 09:40:34 +0900824const struct hist_iter_ops hist_iter_mem = {
825 .prepare_entry = iter_prepare_mem_entry,
826 .add_single_entry = iter_add_single_mem_entry,
827 .next_entry = iter_next_nop_entry,
828 .add_next_entry = iter_add_next_nop_entry,
829 .finish_entry = iter_finish_mem_entry,
830};
831
832const struct hist_iter_ops hist_iter_branch = {
833 .prepare_entry = iter_prepare_branch_entry,
834 .add_single_entry = iter_add_single_branch_entry,
835 .next_entry = iter_next_branch_entry,
836 .add_next_entry = iter_add_next_branch_entry,
837 .finish_entry = iter_finish_branch_entry,
838};
839
840const struct hist_iter_ops hist_iter_normal = {
841 .prepare_entry = iter_prepare_normal_entry,
842 .add_single_entry = iter_add_single_normal_entry,
843 .next_entry = iter_next_nop_entry,
844 .add_next_entry = iter_add_next_nop_entry,
845 .finish_entry = iter_finish_normal_entry,
846};
847
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900848const struct hist_iter_ops hist_iter_cumulative = {
849 .prepare_entry = iter_prepare_cumulative_entry,
850 .add_single_entry = iter_add_single_cumulative_entry,
851 .next_entry = iter_next_cumulative_entry,
852 .add_next_entry = iter_add_next_cumulative_entry,
853 .finish_entry = iter_finish_cumulative_entry,
854};
855
Namhyung Kim69bcb012013-10-30 09:40:34 +0900856int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
857 struct perf_evsel *evsel, struct perf_sample *sample,
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900858 int max_stack_depth, void *arg)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900859{
860 int err, err2;
861
862 err = sample__resolve_callchain(sample, &iter->parent, evsel, al,
863 max_stack_depth);
864 if (err)
865 return err;
866
867 iter->evsel = evsel;
868 iter->sample = sample;
869
870 err = iter->ops->prepare_entry(iter, al);
871 if (err)
872 goto out;
873
874 err = iter->ops->add_single_entry(iter, al);
875 if (err)
876 goto out;
877
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900878 if (iter->he && iter->add_entry_cb) {
879 err = iter->add_entry_cb(iter, al, true, arg);
880 if (err)
881 goto out;
882 }
883
Namhyung Kim69bcb012013-10-30 09:40:34 +0900884 while (iter->ops->next_entry(iter, al)) {
885 err = iter->ops->add_next_entry(iter, al);
886 if (err)
887 break;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900888
889 if (iter->he && iter->add_entry_cb) {
890 err = iter->add_entry_cb(iter, al, false, arg);
891 if (err)
892 goto out;
893 }
Namhyung Kim69bcb012013-10-30 09:40:34 +0900894 }
895
896out:
897 err2 = iter->ops->finish_entry(iter, al);
898 if (!err)
899 err = err2;
900
901 return err;
902}
903
John Kacur3d1d07e2009-09-28 15:32:55 +0200904int64_t
905hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
906{
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900907 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +0200908 int64_t cmp = 0;
909
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900910 perf_hpp__for_each_sort_list(fmt) {
Namhyung Kime67d49a2014-03-18 13:00:59 +0900911 if (perf_hpp__should_skip(fmt))
912 continue;
913
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900914 cmp = fmt->cmp(left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200915 if (cmp)
916 break;
917 }
918
919 return cmp;
920}
921
922int64_t
923hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
924{
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900925 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +0200926 int64_t cmp = 0;
927
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900928 perf_hpp__for_each_sort_list(fmt) {
Namhyung Kime67d49a2014-03-18 13:00:59 +0900929 if (perf_hpp__should_skip(fmt))
930 continue;
931
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900932 cmp = fmt->collapse(left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200933 if (cmp)
934 break;
935 }
936
937 return cmp;
938}
939
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -0300940void hist_entry__delete(struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +0200941{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300942 zfree(&he->branch_info);
943 zfree(&he->mem_info);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900944 zfree(&he->stat_acc);
Namhyung Kimf048d542013-09-11 14:09:28 +0900945 free_srcline(he->srcline);
Namhyung Kimd1149602014-12-30 14:38:13 +0900946 free_callchain(he->callchain);
John Kacur3d1d07e2009-09-28 15:32:55 +0200947 free(he);
948}
949
950/*
951 * collapse the histogram
952 */
953
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300954static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100955 struct rb_root *root,
956 struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +0200957{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200958 struct rb_node **p = &root->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200959 struct rb_node *parent = NULL;
960 struct hist_entry *iter;
961 int64_t cmp;
962
963 while (*p != NULL) {
964 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300965 iter = rb_entry(parent, struct hist_entry, rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +0200966
967 cmp = hist_entry__collapse(iter, he);
968
969 if (!cmp) {
Namhyung Kim139c0812012-10-04 21:49:43 +0900970 he_stat__add_stat(&iter->stat, &he->stat);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900971 if (symbol_conf.cumulate_callchain)
972 he_stat__add_stat(iter->stat_acc, he->stat_acc);
Namhyung Kim9ec60972012-09-26 16:47:28 +0900973
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100974 if (symbol_conf.use_callchain) {
Namhyung Kim47260642012-05-31 14:43:26 +0900975 callchain_cursor_reset(&callchain_cursor);
976 callchain_merge(&callchain_cursor,
977 iter->callchain,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100978 he->callchain);
979 }
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -0300980 hist_entry__delete(he);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300981 return false;
John Kacur3d1d07e2009-09-28 15:32:55 +0200982 }
983
984 if (cmp < 0)
985 p = &(*p)->rb_left;
986 else
987 p = &(*p)->rb_right;
988 }
Namhyung Kim740b97f2014-12-22 13:44:10 +0900989 hists->nr_entries++;
John Kacur3d1d07e2009-09-28 15:32:55 +0200990
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300991 rb_link_node(&he->rb_node_in, parent, p);
992 rb_insert_color(&he->rb_node_in, root);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300993 return true;
John Kacur3d1d07e2009-09-28 15:32:55 +0200994}
995
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300996static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
997{
998 struct rb_root *root;
999
1000 pthread_mutex_lock(&hists->lock);
1001
1002 root = hists->entries_in;
1003 if (++hists->entries_in > &hists->entries_in_array[1])
1004 hists->entries_in = &hists->entries_in_array[0];
1005
1006 pthread_mutex_unlock(&hists->lock);
1007
1008 return root;
1009}
1010
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001011static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
1012{
1013 hists__filter_entry_by_dso(hists, he);
1014 hists__filter_entry_by_thread(hists, he);
Namhyung Kime94d53e2012-03-16 17:50:51 +09001015 hists__filter_entry_by_symbol(hists, he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001016}
1017
Namhyung Kimc1fb5652013-10-11 14:15:38 +09001018void hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001019{
1020 struct rb_root *root;
1021 struct rb_node *next;
1022 struct hist_entry *n;
1023
Namhyung Kim3a5714f2013-05-14 11:09:01 +09001024 if (!sort__need_collapse)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001025 return;
1026
Namhyung Kim740b97f2014-12-22 13:44:10 +09001027 hists->nr_entries = 0;
1028
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001029 root = hists__get_rotate_entries_in(hists);
Namhyung Kim740b97f2014-12-22 13:44:10 +09001030
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001031 next = rb_first(root);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001032
1033 while (next) {
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001034 if (session_done())
1035 break;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001036 n = rb_entry(next, struct hist_entry, rb_node_in);
1037 next = rb_next(&n->rb_node_in);
1038
1039 rb_erase(&n->rb_node_in, root);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001040 if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
1041 /*
1042 * If it wasn't combined with one of the entries already
1043 * collapsed, we need to apply the filters that may have
1044 * been set by, say, the hist_browser.
1045 */
1046 hists__apply_filters(hists, n);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001047 }
Namhyung Kimc1fb5652013-10-11 14:15:38 +09001048 if (prog)
1049 ui_progress__update(prog, 1);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001050 }
1051}
1052
Namhyung Kim043ca3892014-03-03 14:18:00 +09001053static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001054{
Namhyung Kim043ca3892014-03-03 14:18:00 +09001055 struct perf_hpp_fmt *fmt;
1056 int64_t cmp = 0;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001057
Namhyung Kim26d8b332014-03-03 16:16:20 +09001058 perf_hpp__for_each_sort_list(fmt) {
Namhyung Kime67d49a2014-03-18 13:00:59 +09001059 if (perf_hpp__should_skip(fmt))
1060 continue;
1061
Namhyung Kim043ca3892014-03-03 14:18:00 +09001062 cmp = fmt->sort(a, b);
1063 if (cmp)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001064 break;
1065 }
1066
Namhyung Kim043ca3892014-03-03 14:18:00 +09001067 return cmp;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001068}
1069
Namhyung Kim9283ba92014-04-24 16:37:26 +09001070static void hists__reset_filter_stats(struct hists *hists)
1071{
1072 hists->nr_non_filtered_entries = 0;
1073 hists->stats.total_non_filtered_period = 0;
1074}
1075
1076void hists__reset_stats(struct hists *hists)
1077{
1078 hists->nr_entries = 0;
1079 hists->stats.total_period = 0;
1080
1081 hists__reset_filter_stats(hists);
1082}
1083
1084static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
1085{
1086 hists->nr_non_filtered_entries++;
1087 hists->stats.total_non_filtered_period += h->stat.period;
1088}
1089
1090void hists__inc_stats(struct hists *hists, struct hist_entry *h)
1091{
1092 if (!h->filtered)
1093 hists__inc_filter_stats(hists, h);
1094
1095 hists->nr_entries++;
1096 hists->stats.total_period += h->stat.period;
1097}
1098
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001099static void __hists__insert_output_entry(struct rb_root *entries,
1100 struct hist_entry *he,
1101 u64 min_callchain_hits)
John Kacur3d1d07e2009-09-28 15:32:55 +02001102{
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001103 struct rb_node **p = &entries->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +02001104 struct rb_node *parent = NULL;
1105 struct hist_entry *iter;
1106
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -02001107 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melob9fb9302010-04-02 09:50:42 -03001108 callchain_param.sort(&he->sorted_chain, he->callchain,
John Kacur3d1d07e2009-09-28 15:32:55 +02001109 min_callchain_hits, &callchain_param);
1110
1111 while (*p != NULL) {
1112 parent = *p;
1113 iter = rb_entry(parent, struct hist_entry, rb_node);
1114
Namhyung Kim043ca3892014-03-03 14:18:00 +09001115 if (hist_entry__sort(he, iter) > 0)
John Kacur3d1d07e2009-09-28 15:32:55 +02001116 p = &(*p)->rb_left;
1117 else
1118 p = &(*p)->rb_right;
1119 }
1120
1121 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001122 rb_insert_color(&he->rb_node, entries);
John Kacur3d1d07e2009-09-28 15:32:55 +02001123}
1124
Namhyung Kim740b97f2014-12-22 13:44:10 +09001125void hists__output_resort(struct hists *hists, struct ui_progress *prog)
John Kacur3d1d07e2009-09-28 15:32:55 +02001126{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001127 struct rb_root *root;
John Kacur3d1d07e2009-09-28 15:32:55 +02001128 struct rb_node *next;
1129 struct hist_entry *n;
John Kacur3d1d07e2009-09-28 15:32:55 +02001130 u64 min_callchain_hits;
1131
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001132 min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
John Kacur3d1d07e2009-09-28 15:32:55 +02001133
Namhyung Kim3a5714f2013-05-14 11:09:01 +09001134 if (sort__need_collapse)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001135 root = &hists->entries_collapsed;
1136 else
1137 root = hists->entries_in;
1138
1139 next = rb_first(root);
1140 hists->entries = RB_ROOT;
John Kacur3d1d07e2009-09-28 15:32:55 +02001141
Namhyung Kim9283ba92014-04-24 16:37:26 +09001142 hists__reset_stats(hists);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001143 hists__reset_col_len(hists);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -03001144
John Kacur3d1d07e2009-09-28 15:32:55 +02001145 while (next) {
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001146 n = rb_entry(next, struct hist_entry, rb_node_in);
1147 next = rb_next(&n->rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +02001148
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001149 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
Namhyung Kim62638352014-04-24 16:21:46 +09001150 hists__inc_stats(hists, n);
Namhyung Kimae993ef2014-04-24 16:25:19 +09001151
1152 if (!n->filtered)
1153 hists__calc_col_len(hists, n);
Namhyung Kim740b97f2014-12-22 13:44:10 +09001154
1155 if (prog)
1156 ui_progress__update(prog, 1);
John Kacur3d1d07e2009-09-28 15:32:55 +02001157 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001158}
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -02001159
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001160static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001161 enum hist_filter filter)
1162{
1163 h->filtered &= ~(1 << filter);
1164 if (h->filtered)
1165 return;
1166
Namhyung Kim87e90f42014-04-24 16:44:16 +09001167 /* force fold unfiltered entry for simplicity */
1168 h->ms.unfolded = false;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -03001169 h->row_offset = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001170
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001171 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001172
Namhyung Kim9283ba92014-04-24 16:37:26 +09001173 hists__inc_filter_stats(hists, h);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001174 hists__calc_col_len(hists, h);
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001175}
1176
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001177
1178static bool hists__filter_entry_by_dso(struct hists *hists,
1179 struct hist_entry *he)
1180{
1181 if (hists->dso_filter != NULL &&
1182 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
1183 he->filtered |= (1 << HIST_FILTER__DSO);
1184 return true;
1185 }
1186
1187 return false;
1188}
1189
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001190void hists__filter_by_dso(struct hists *hists)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001191{
1192 struct rb_node *nd;
1193
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001194 hists->stats.nr_non_filtered_samples = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001195
1196 hists__reset_filter_stats(hists);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001197 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001198
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001199 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001200 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1201
1202 if (symbol_conf.exclude_other && !h->parent)
1203 continue;
1204
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001205 if (hists__filter_entry_by_dso(hists, h))
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001206 continue;
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001207
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001208 hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001209 }
1210}
1211
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001212static bool hists__filter_entry_by_thread(struct hists *hists,
1213 struct hist_entry *he)
1214{
1215 if (hists->thread_filter != NULL &&
1216 he->thread != hists->thread_filter) {
1217 he->filtered |= (1 << HIST_FILTER__THREAD);
1218 return true;
1219 }
1220
1221 return false;
1222}
1223
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001224void hists__filter_by_thread(struct hists *hists)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001225{
1226 struct rb_node *nd;
1227
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001228 hists->stats.nr_non_filtered_samples = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001229
1230 hists__reset_filter_stats(hists);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001231 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001232
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001233 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001234 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1235
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001236 if (hists__filter_entry_by_thread(hists, h))
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001237 continue;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001238
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001239 hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001240 }
1241}
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001242
Namhyung Kime94d53e2012-03-16 17:50:51 +09001243static bool hists__filter_entry_by_symbol(struct hists *hists,
1244 struct hist_entry *he)
1245{
1246 if (hists->symbol_filter_str != NULL &&
1247 (!he->ms.sym || strstr(he->ms.sym->name,
1248 hists->symbol_filter_str) == NULL)) {
1249 he->filtered |= (1 << HIST_FILTER__SYMBOL);
1250 return true;
1251 }
1252
1253 return false;
1254}
1255
1256void hists__filter_by_symbol(struct hists *hists)
1257{
1258 struct rb_node *nd;
1259
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001260 hists->stats.nr_non_filtered_samples = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001261
1262 hists__reset_filter_stats(hists);
Namhyung Kime94d53e2012-03-16 17:50:51 +09001263 hists__reset_col_len(hists);
1264
1265 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1266 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1267
1268 if (hists__filter_entry_by_symbol(hists, h))
1269 continue;
1270
1271 hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
1272 }
1273}
1274
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001275void events_stats__inc(struct events_stats *stats, u32 type)
1276{
1277 ++stats->nr_events[0];
1278 ++stats->nr_events[type];
1279}
1280
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001281void hists__inc_nr_events(struct hists *hists, u32 type)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001282{
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001283 events_stats__inc(&hists->stats, type);
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001284}
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001285
Namhyung Kim1844dbc2014-05-28 14:12:18 +09001286void hists__inc_nr_samples(struct hists *hists, bool filtered)
1287{
1288 events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
1289 if (!filtered)
1290 hists->stats.nr_non_filtered_samples++;
1291}
1292
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001293static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
1294 struct hist_entry *pair)
1295{
Namhyung Kimce74f602012-12-10 17:29:55 +09001296 struct rb_root *root;
1297 struct rb_node **p;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001298 struct rb_node *parent = NULL;
1299 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -07001300 int64_t cmp;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001301
Namhyung Kimce74f602012-12-10 17:29:55 +09001302 if (sort__need_collapse)
1303 root = &hists->entries_collapsed;
1304 else
1305 root = hists->entries_in;
1306
1307 p = &root->rb_node;
1308
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001309 while (*p != NULL) {
1310 parent = *p;
Namhyung Kimce74f602012-12-10 17:29:55 +09001311 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001312
Namhyung Kimce74f602012-12-10 17:29:55 +09001313 cmp = hist_entry__collapse(he, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001314
1315 if (!cmp)
1316 goto out;
1317
1318 if (cmp < 0)
1319 p = &(*p)->rb_left;
1320 else
1321 p = &(*p)->rb_right;
1322 }
1323
Namhyung Kima0b51af2012-09-11 13:34:27 +09001324 he = hist_entry__new(pair, true);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001325 if (he) {
Arnaldo Carvalho de Melo30193d72012-11-12 13:20:03 -03001326 memset(&he->stat, 0, sizeof(he->stat));
1327 he->hists = hists;
Namhyung Kimce74f602012-12-10 17:29:55 +09001328 rb_link_node(&he->rb_node_in, parent, p);
1329 rb_insert_color(&he->rb_node_in, root);
Namhyung Kim62638352014-04-24 16:21:46 +09001330 hists__inc_stats(hists, he);
Jiri Olsae0af43d2012-12-01 21:18:20 +01001331 he->dummy = true;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001332 }
1333out:
1334 return he;
1335}
1336
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001337static struct hist_entry *hists__find_entry(struct hists *hists,
1338 struct hist_entry *he)
1339{
Namhyung Kimce74f602012-12-10 17:29:55 +09001340 struct rb_node *n;
1341
1342 if (sort__need_collapse)
1343 n = hists->entries_collapsed.rb_node;
1344 else
1345 n = hists->entries_in->rb_node;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001346
1347 while (n) {
Namhyung Kimce74f602012-12-10 17:29:55 +09001348 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
1349 int64_t cmp = hist_entry__collapse(iter, he);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001350
1351 if (cmp < 0)
1352 n = n->rb_left;
1353 else if (cmp > 0)
1354 n = n->rb_right;
1355 else
1356 return iter;
1357 }
1358
1359 return NULL;
1360}
1361
1362/*
1363 * Look for pairs to link to the leader buckets (hist_entries):
1364 */
1365void hists__match(struct hists *leader, struct hists *other)
1366{
Namhyung Kimce74f602012-12-10 17:29:55 +09001367 struct rb_root *root;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001368 struct rb_node *nd;
1369 struct hist_entry *pos, *pair;
1370
Namhyung Kimce74f602012-12-10 17:29:55 +09001371 if (sort__need_collapse)
1372 root = &leader->entries_collapsed;
1373 else
1374 root = leader->entries_in;
1375
1376 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
1377 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001378 pair = hists__find_entry(other, pos);
1379
1380 if (pair)
Namhyung Kim5fa90412012-11-29 15:38:34 +09001381 hist_entry__add_pair(pair, pos);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001382 }
1383}
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001384
1385/*
1386 * Look for entries in the other hists that are not present in the leader, if
1387 * we find them, just add a dummy entry on the leader hists, with period=0,
1388 * nr_events=0, to serve as the list header.
1389 */
1390int hists__link(struct hists *leader, struct hists *other)
1391{
Namhyung Kimce74f602012-12-10 17:29:55 +09001392 struct rb_root *root;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001393 struct rb_node *nd;
1394 struct hist_entry *pos, *pair;
1395
Namhyung Kimce74f602012-12-10 17:29:55 +09001396 if (sort__need_collapse)
1397 root = &other->entries_collapsed;
1398 else
1399 root = other->entries_in;
1400
1401 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
1402 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001403
1404 if (!hist_entry__has_pairs(pos)) {
1405 pair = hists__add_dummy_entry(leader, pos);
1406 if (pair == NULL)
1407 return -1;
Namhyung Kim5fa90412012-11-29 15:38:34 +09001408 hist_entry__add_pair(pos, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001409 }
1410 }
1411
1412 return 0;
1413}
Namhyung Kimf2148332014-01-14 11:52:48 +09001414
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03001415
1416size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp)
1417{
1418 struct perf_evsel *pos;
1419 size_t ret = 0;
1420
1421 evlist__for_each(evlist, pos) {
1422 ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
1423 ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
1424 }
1425
1426 return ret;
1427}
1428
1429
Namhyung Kimf2148332014-01-14 11:52:48 +09001430u64 hists__total_period(struct hists *hists)
1431{
1432 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
1433 hists->stats.total_period;
1434}
Namhyung Kim33db4562014-02-07 12:06:07 +09001435
1436int parse_filter_percentage(const struct option *opt __maybe_unused,
1437 const char *arg, int unset __maybe_unused)
1438{
1439 if (!strcmp(arg, "relative"))
1440 symbol_conf.filter_relative = true;
1441 else if (!strcmp(arg, "absolute"))
1442 symbol_conf.filter_relative = false;
1443 else
1444 return -1;
1445
1446 return 0;
1447}
Namhyung Kim0b93da12014-01-14 12:02:15 +09001448
1449int perf_hist_config(const char *var, const char *value)
1450{
1451 if (!strcmp(var, "hist.percentage"))
1452 return parse_filter_percentage(NULL, value, 0);
1453
1454 return 0;
1455}
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03001456
1457static int hists_evsel__init(struct perf_evsel *evsel)
1458{
1459 struct hists *hists = evsel__hists(evsel);
1460
1461 memset(hists, 0, sizeof(*hists));
1462 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
1463 hists->entries_in = &hists->entries_in_array[0];
1464 hists->entries_collapsed = RB_ROOT;
1465 hists->entries = RB_ROOT;
1466 pthread_mutex_init(&hists->lock, NULL);
1467 return 0;
1468}
1469
1470/*
1471 * XXX We probably need a hists_evsel__exit() to free the hist_entries
1472 * stored in the rbtree...
1473 */
1474
1475int hists__init(void)
1476{
1477 int err = perf_evsel__object_config(sizeof(struct hists_evsel),
1478 hists_evsel__init, NULL);
1479 if (err)
1480 fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);
1481
1482 return err;
1483}