blob: 1cc6ea4b7065dc7679c6bbf0b9048026af975239 [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
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900244void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300245{
246 struct rb_node *next = rb_first(&hists->entries);
247 struct hist_entry *n;
248
249 while (next) {
250 n = rb_entry(next, struct hist_entry, rb_node);
251 next = rb_next(&n->rb_node);
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300252 /*
253 * We may be annotating this, for instance, so keep it here in
254 * case some it gets new samples, we'll eventually free it when
255 * the user stops browsing and it agains gets fully decayed.
256 */
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200257 if (((zap_user && n->level == '.') ||
258 (zap_kernel && n->level != '.') ||
259 hists__decay_entry(hists, n)) &&
260 !n->used) {
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300261 rb_erase(&n->rb_node, &hists->entries);
262
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900263 if (sort__need_collapse)
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300264 rb_erase(&n->rb_node_in, &hists->entries_collapsed);
265
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300266 --hists->nr_entries;
Namhyung Kim3186b682014-04-22 13:44:23 +0900267 if (!n->filtered)
268 --hists->nr_non_filtered_entries;
269
270 hist_entry__free(n);
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300271 }
272 }
273}
274
Namhyung Kim701937b2014-08-12 17:16:05 +0900275void hists__delete_entries(struct hists *hists)
276{
277 struct rb_node *next = rb_first(&hists->entries);
278 struct hist_entry *n;
279
280 while (next) {
281 n = rb_entry(next, struct hist_entry, rb_node);
282 next = rb_next(&n->rb_node);
283
284 rb_erase(&n->rb_node, &hists->entries);
285
286 if (sort__need_collapse)
287 rb_erase(&n->rb_node_in, &hists->entries_collapsed);
288
289 --hists->nr_entries;
290 if (!n->filtered)
291 --hists->nr_non_filtered_entries;
292
293 hist_entry__free(n);
294 }
295}
296
John Kacur3d1d07e2009-09-28 15:32:55 +0200297/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300298 * histogram, sorted on item, collects periods
John Kacur3d1d07e2009-09-28 15:32:55 +0200299 */
300
Namhyung Kima0b51af2012-09-11 13:34:27 +0900301static struct hist_entry *hist_entry__new(struct hist_entry *template,
302 bool sample_self)
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300303{
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900304 size_t callchain_size = 0;
305 struct hist_entry *he;
306
307 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain)
308 callchain_size = sizeof(struct callchain_root);
309
310 he = zalloc(sizeof(*he) + callchain_size);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300311
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200312 if (he != NULL) {
313 *he = *template;
Namhyung Kimc4b35352012-10-04 21:49:42 +0900314
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900315 if (symbol_conf.cumulate_callchain) {
316 he->stat_acc = malloc(sizeof(he->stat));
317 if (he->stat_acc == NULL) {
318 free(he);
319 return NULL;
320 }
321 memcpy(he->stat_acc, &he->stat, sizeof(he->stat));
Namhyung Kima0b51af2012-09-11 13:34:27 +0900322 if (!sample_self)
323 memset(&he->stat, 0, sizeof(he->stat));
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900324 }
325
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200326 if (he->ms.map)
327 he->ms.map->referenced = true;
Stephane Eranian3cf0cb12013-01-14 15:02:45 +0100328
329 if (he->branch_info) {
Namhyung Kim26353a62013-04-01 20:35:17 +0900330 /*
331 * This branch info is (a part of) allocated from
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -0300332 * sample__resolve_bstack() and will be freed after
Namhyung Kim26353a62013-04-01 20:35:17 +0900333 * adding new entries. So we need to save a copy.
334 */
335 he->branch_info = malloc(sizeof(*he->branch_info));
336 if (he->branch_info == NULL) {
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900337 free(he->stat_acc);
Namhyung Kim26353a62013-04-01 20:35:17 +0900338 free(he);
339 return NULL;
340 }
341
342 memcpy(he->branch_info, template->branch_info,
343 sizeof(*he->branch_info));
344
Stephane Eranian3cf0cb12013-01-14 15:02:45 +0100345 if (he->branch_info->from.map)
346 he->branch_info->from.map->referenced = true;
347 if (he->branch_info->to.map)
348 he->branch_info->to.map->referenced = true;
349 }
350
Stephane Eranian98a3b322013-01-24 16:10:35 +0100351 if (he->mem_info) {
352 if (he->mem_info->iaddr.map)
353 he->mem_info->iaddr.map->referenced = true;
354 if (he->mem_info->daddr.map)
355 he->mem_info->daddr.map->referenced = true;
356 }
357
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300358 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200359 callchain_init(he->callchain);
Arnaldo Carvalho de Melob821c732012-10-25 14:42:45 -0200360
361 INIT_LIST_HEAD(&he->pairs.node);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300362 }
363
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200364 return he;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300365}
366
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300367static u8 symbol__parent_filter(const struct symbol *parent)
368{
369 if (symbol_conf.exclude_other && parent == NULL)
370 return 1 << HIST_FILTER__PARENT;
371 return 0;
372}
373
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100374static struct hist_entry *add_hist_entry(struct hists *hists,
Namhyung Kimf1cbf782013-12-18 14:21:11 +0900375 struct hist_entry *entry,
Namhyung Kima0b51af2012-09-11 13:34:27 +0900376 struct addr_location *al,
377 bool sample_self)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300378{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300379 struct rb_node **p;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300380 struct rb_node *parent = NULL;
381 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -0700382 int64_t cmp;
Namhyung Kimf1cbf782013-12-18 14:21:11 +0900383 u64 period = entry->stat.period;
384 u64 weight = entry->stat.weight;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300385
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300386 p = &hists->entries_in->rb_node;
387
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300388 while (*p != NULL) {
389 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300390 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300391
Namhyung Kim9afcf932012-12-10 17:29:54 +0900392 /*
393 * Make sure that it receives arguments in a same order as
394 * hist_entry__collapse() so that we can use an appropriate
395 * function when searching an entry regardless which sort
396 * keys were used.
397 */
398 cmp = hist_entry__cmp(he, entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300399
400 if (!cmp) {
Namhyung Kima0b51af2012-09-11 13:34:27 +0900401 if (sample_self)
402 he_stat__add_period(&he->stat, period, weight);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900403 if (symbol_conf.cumulate_callchain)
404 he_stat__add_period(he->stat_acc, period, weight);
David Miller63fa4712012-03-27 03:14:18 -0400405
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900406 /*
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -0300407 * This mem info was allocated from sample__resolve_mem
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900408 * and will not be used anymore.
409 */
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300410 zfree(&entry->mem_info);
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900411
David Miller63fa4712012-03-27 03:14:18 -0400412 /* If the map of an existing hist_entry has
413 * become out-of-date due to an exec() or
414 * similar, update it. Otherwise we will
415 * mis-adjust symbol addresses when computing
416 * the history counter to increment.
417 */
418 if (he->ms.map != entry->ms.map) {
419 he->ms.map = entry->ms.map;
420 if (he->ms.map)
421 he->ms.map->referenced = true;
422 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300423 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300424 }
425
426 if (cmp < 0)
427 p = &(*p)->rb_left;
428 else
429 p = &(*p)->rb_right;
430 }
431
Namhyung Kima0b51af2012-09-11 13:34:27 +0900432 he = hist_entry__new(entry, sample_self);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300433 if (!he)
Namhyung Kim27a0dcb2013-05-14 11:09:02 +0900434 return NULL;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300435
436 rb_link_node(&he->rb_node_in, parent, p);
437 rb_insert_color(&he->rb_node_in, hists->entries_in);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300438out:
Namhyung Kima0b51af2012-09-11 13:34:27 +0900439 if (sample_self)
440 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900441 if (symbol_conf.cumulate_callchain)
442 he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300443 return he;
444}
445
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300446struct hist_entry *__hists__add_entry(struct hists *hists,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100447 struct addr_location *al,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900448 struct symbol *sym_parent,
449 struct branch_info *bi,
450 struct mem_info *mi,
Namhyung Kima0b51af2012-09-11 13:34:27 +0900451 u64 period, u64 weight, u64 transaction,
452 bool sample_self)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100453{
454 struct hist_entry entry = {
455 .thread = al->thread,
Namhyung Kim4dfced32013-09-13 16:28:57 +0900456 .comm = thread__comm(al->thread),
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100457 .ms = {
458 .map = al->map,
459 .sym = al->sym,
460 },
Don Zickus7365be52014-05-27 12:28:05 -0400461 .cpu = al->cpu,
462 .cpumode = al->cpumode,
463 .ip = al->addr,
464 .level = al->level,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900465 .stat = {
Namhyung Kimc4b35352012-10-04 21:49:42 +0900466 .nr_events = 1,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900467 .period = period,
Andi Kleen05484292013-01-24 16:10:29 +0100468 .weight = weight,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900469 },
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100470 .parent = sym_parent,
Namhyung Kim2c86c7c2014-03-17 18:18:54 -0300471 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300472 .hists = hists,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900473 .branch_info = bi,
474 .mem_info = mi,
Andi Kleen475eeab2013-09-20 07:40:43 -0700475 .transaction = transaction,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100476 };
477
Namhyung Kima0b51af2012-09-11 13:34:27 +0900478 return add_hist_entry(hists, &entry, al, sample_self);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100479}
480
Namhyung Kim69bcb012013-10-30 09:40:34 +0900481static int
482iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
483 struct addr_location *al __maybe_unused)
484{
485 return 0;
486}
487
488static int
489iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
490 struct addr_location *al __maybe_unused)
491{
492 return 0;
493}
494
495static int
496iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
497{
498 struct perf_sample *sample = iter->sample;
499 struct mem_info *mi;
500
501 mi = sample__resolve_mem(sample, al);
502 if (mi == NULL)
503 return -ENOMEM;
504
505 iter->priv = mi;
506 return 0;
507}
508
509static int
510iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
511{
512 u64 cost;
513 struct mem_info *mi = iter->priv;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300514 struct hists *hists = evsel__hists(iter->evsel);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900515 struct hist_entry *he;
516
517 if (mi == NULL)
518 return -EINVAL;
519
520 cost = iter->sample->weight;
521 if (!cost)
522 cost = 1;
523
524 /*
525 * must pass period=weight in order to get the correct
526 * sorting from hists__collapse_resort() which is solely
527 * based on periods. We want sorting be done on nr_events * weight
528 * and this is indirectly achieved by passing period=weight here
529 * and the he_stat__add_period() function.
530 */
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300531 he = __hists__add_entry(hists, al, iter->parent, NULL, mi,
Namhyung Kima0b51af2012-09-11 13:34:27 +0900532 cost, cost, 0, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900533 if (!he)
534 return -ENOMEM;
535
536 iter->he = he;
537 return 0;
538}
539
540static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900541iter_finish_mem_entry(struct hist_entry_iter *iter,
542 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900543{
544 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300545 struct hists *hists = evsel__hists(evsel);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900546 struct hist_entry *he = iter->he;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900547 int err = -EINVAL;
548
549 if (he == NULL)
550 goto out;
551
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300552 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900553
554 err = hist_entry__append_callchain(he, iter->sample);
555
556out:
557 /*
558 * We don't need to free iter->priv (mem_info) here since
559 * the mem info was either already freed in add_hist_entry() or
560 * passed to a new hist entry by hist_entry__new().
561 */
562 iter->priv = NULL;
563
564 iter->he = NULL;
565 return err;
566}
567
568static int
569iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
570{
571 struct branch_info *bi;
572 struct perf_sample *sample = iter->sample;
573
574 bi = sample__resolve_bstack(sample, al);
575 if (!bi)
576 return -ENOMEM;
577
578 iter->curr = 0;
579 iter->total = sample->branch_stack->nr;
580
581 iter->priv = bi;
582 return 0;
583}
584
585static int
586iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused,
587 struct addr_location *al __maybe_unused)
588{
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900589 /* to avoid calling callback function */
590 iter->he = NULL;
591
Namhyung Kim69bcb012013-10-30 09:40:34 +0900592 return 0;
593}
594
595static int
596iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
597{
598 struct branch_info *bi = iter->priv;
599 int i = iter->curr;
600
601 if (bi == NULL)
602 return 0;
603
604 if (iter->curr >= iter->total)
605 return 0;
606
607 al->map = bi[i].to.map;
608 al->sym = bi[i].to.sym;
609 al->addr = bi[i].to.addr;
610 return 1;
611}
612
613static int
614iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
615{
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900616 struct branch_info *bi;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900617 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300618 struct hists *hists = evsel__hists(evsel);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900619 struct hist_entry *he = NULL;
620 int i = iter->curr;
621 int err = 0;
622
623 bi = iter->priv;
624
625 if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym))
626 goto out;
627
628 /*
629 * The report shows the percentage of total branches captured
630 * and not events sampled. Thus we use a pseudo period of 1.
631 */
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300632 he = __hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
Namhyung Kima0b51af2012-09-11 13:34:27 +0900633 1, 1, 0, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900634 if (he == NULL)
635 return -ENOMEM;
636
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300637 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900638
639out:
640 iter->he = he;
641 iter->curr++;
642 return err;
643}
644
645static int
646iter_finish_branch_entry(struct hist_entry_iter *iter,
647 struct addr_location *al __maybe_unused)
648{
649 zfree(&iter->priv);
650 iter->he = NULL;
651
652 return iter->curr >= iter->total ? 0 : -1;
653}
654
655static int
656iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
657 struct addr_location *al __maybe_unused)
658{
659 return 0;
660}
661
662static int
663iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
664{
665 struct perf_evsel *evsel = iter->evsel;
666 struct perf_sample *sample = iter->sample;
667 struct hist_entry *he;
668
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300669 he = __hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
Namhyung Kim69bcb012013-10-30 09:40:34 +0900670 sample->period, sample->weight,
Namhyung Kima0b51af2012-09-11 13:34:27 +0900671 sample->transaction, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900672 if (he == NULL)
673 return -ENOMEM;
674
675 iter->he = he;
676 return 0;
677}
678
679static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900680iter_finish_normal_entry(struct hist_entry_iter *iter,
681 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900682{
Namhyung Kim69bcb012013-10-30 09:40:34 +0900683 struct hist_entry *he = iter->he;
684 struct perf_evsel *evsel = iter->evsel;
685 struct perf_sample *sample = iter->sample;
686
687 if (he == NULL)
688 return 0;
689
690 iter->he = NULL;
691
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300692 hists__inc_nr_samples(evsel__hists(evsel), he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900693
694 return hist_entry__append_callchain(he, sample);
695}
696
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900697static int
698iter_prepare_cumulative_entry(struct hist_entry_iter *iter __maybe_unused,
699 struct addr_location *al __maybe_unused)
700{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900701 struct hist_entry **he_cache;
702
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900703 callchain_cursor_commit(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900704
705 /*
706 * This is for detecting cycles or recursions so that they're
707 * cumulated only one time to prevent entries more than 100%
708 * overhead.
709 */
710 he_cache = malloc(sizeof(*he_cache) * (PERF_MAX_STACK_DEPTH + 1));
711 if (he_cache == NULL)
712 return -ENOMEM;
713
714 iter->priv = he_cache;
715 iter->curr = 0;
716
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900717 return 0;
718}
719
720static int
721iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
722 struct addr_location *al)
723{
724 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300725 struct hists *hists = evsel__hists(evsel);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900726 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900727 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900728 struct hist_entry *he;
729 int err = 0;
730
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300731 he = __hists__add_entry(hists, al, iter->parent, NULL, NULL,
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900732 sample->period, sample->weight,
733 sample->transaction, true);
734 if (he == NULL)
735 return -ENOMEM;
736
737 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900738 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900739
Namhyung Kimbe7f8552013-12-26 17:44:10 +0900740 callchain_append(he->callchain, &callchain_cursor, sample->period);
741
742 /*
743 * We need to re-initialize the cursor since callchain_append()
744 * advanced the cursor to the end.
745 */
746 callchain_cursor_commit(&callchain_cursor);
747
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300748 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900749
750 return err;
751}
752
753static int
754iter_next_cumulative_entry(struct hist_entry_iter *iter,
755 struct addr_location *al)
756{
757 struct callchain_cursor_node *node;
758
759 node = callchain_cursor_current(&callchain_cursor);
760 if (node == NULL)
761 return 0;
762
Namhyung Kimc7405d82013-10-31 13:58:30 +0900763 return fill_callchain_info(al, node, iter->hide_unresolved);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900764}
765
766static int
767iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
768 struct addr_location *al)
769{
770 struct perf_evsel *evsel = iter->evsel;
771 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900772 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900773 struct hist_entry *he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900774 struct hist_entry he_tmp = {
775 .cpu = al->cpu,
776 .thread = al->thread,
777 .comm = thread__comm(al->thread),
778 .ip = al->addr,
779 .ms = {
780 .map = al->map,
781 .sym = al->sym,
782 },
783 .parent = iter->parent,
784 };
785 int i;
Namhyung Kimbe7f8552013-12-26 17:44:10 +0900786 struct callchain_cursor cursor;
787
788 callchain_cursor_snapshot(&cursor, &callchain_cursor);
789
790 callchain_cursor_advance(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900791
792 /*
793 * Check if there's duplicate entries in the callchain.
794 * It's possible that it has cycles or recursive calls.
795 */
796 for (i = 0; i < iter->curr; i++) {
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900797 if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
798 /* to avoid calling callback function */
799 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900800 return 0;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900801 }
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900802 }
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900803
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300804 he = __hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900805 sample->period, sample->weight,
806 sample->transaction, false);
807 if (he == NULL)
808 return -ENOMEM;
809
810 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900811 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900812
Namhyung Kimbe7f8552013-12-26 17:44:10 +0900813 callchain_append(he->callchain, &cursor, sample->period);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900814 return 0;
815}
816
817static int
818iter_finish_cumulative_entry(struct hist_entry_iter *iter,
819 struct addr_location *al __maybe_unused)
820{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900821 zfree(&iter->priv);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900822 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900823
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900824 return 0;
825}
826
Namhyung Kim69bcb012013-10-30 09:40:34 +0900827const struct hist_iter_ops hist_iter_mem = {
828 .prepare_entry = iter_prepare_mem_entry,
829 .add_single_entry = iter_add_single_mem_entry,
830 .next_entry = iter_next_nop_entry,
831 .add_next_entry = iter_add_next_nop_entry,
832 .finish_entry = iter_finish_mem_entry,
833};
834
835const struct hist_iter_ops hist_iter_branch = {
836 .prepare_entry = iter_prepare_branch_entry,
837 .add_single_entry = iter_add_single_branch_entry,
838 .next_entry = iter_next_branch_entry,
839 .add_next_entry = iter_add_next_branch_entry,
840 .finish_entry = iter_finish_branch_entry,
841};
842
843const struct hist_iter_ops hist_iter_normal = {
844 .prepare_entry = iter_prepare_normal_entry,
845 .add_single_entry = iter_add_single_normal_entry,
846 .next_entry = iter_next_nop_entry,
847 .add_next_entry = iter_add_next_nop_entry,
848 .finish_entry = iter_finish_normal_entry,
849};
850
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900851const struct hist_iter_ops hist_iter_cumulative = {
852 .prepare_entry = iter_prepare_cumulative_entry,
853 .add_single_entry = iter_add_single_cumulative_entry,
854 .next_entry = iter_next_cumulative_entry,
855 .add_next_entry = iter_add_next_cumulative_entry,
856 .finish_entry = iter_finish_cumulative_entry,
857};
858
Namhyung Kim69bcb012013-10-30 09:40:34 +0900859int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
860 struct perf_evsel *evsel, struct perf_sample *sample,
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900861 int max_stack_depth, void *arg)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900862{
863 int err, err2;
864
865 err = sample__resolve_callchain(sample, &iter->parent, evsel, al,
866 max_stack_depth);
867 if (err)
868 return err;
869
870 iter->evsel = evsel;
871 iter->sample = sample;
872
873 err = iter->ops->prepare_entry(iter, al);
874 if (err)
875 goto out;
876
877 err = iter->ops->add_single_entry(iter, al);
878 if (err)
879 goto out;
880
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900881 if (iter->he && iter->add_entry_cb) {
882 err = iter->add_entry_cb(iter, al, true, arg);
883 if (err)
884 goto out;
885 }
886
Namhyung Kim69bcb012013-10-30 09:40:34 +0900887 while (iter->ops->next_entry(iter, al)) {
888 err = iter->ops->add_next_entry(iter, al);
889 if (err)
890 break;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900891
892 if (iter->he && iter->add_entry_cb) {
893 err = iter->add_entry_cb(iter, al, false, arg);
894 if (err)
895 goto out;
896 }
Namhyung Kim69bcb012013-10-30 09:40:34 +0900897 }
898
899out:
900 err2 = iter->ops->finish_entry(iter, al);
901 if (!err)
902 err = err2;
903
904 return err;
905}
906
John Kacur3d1d07e2009-09-28 15:32:55 +0200907int64_t
908hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
909{
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900910 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +0200911 int64_t cmp = 0;
912
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900913 perf_hpp__for_each_sort_list(fmt) {
Namhyung Kime67d49a2014-03-18 13:00:59 +0900914 if (perf_hpp__should_skip(fmt))
915 continue;
916
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900917 cmp = fmt->cmp(left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200918 if (cmp)
919 break;
920 }
921
922 return cmp;
923}
924
925int64_t
926hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
927{
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900928 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +0200929 int64_t cmp = 0;
930
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900931 perf_hpp__for_each_sort_list(fmt) {
Namhyung Kime67d49a2014-03-18 13:00:59 +0900932 if (perf_hpp__should_skip(fmt))
933 continue;
934
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900935 cmp = fmt->collapse(left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200936 if (cmp)
937 break;
938 }
939
940 return cmp;
941}
942
943void hist_entry__free(struct hist_entry *he)
944{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300945 zfree(&he->branch_info);
946 zfree(&he->mem_info);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900947 zfree(&he->stat_acc);
Namhyung Kimf048d542013-09-11 14:09:28 +0900948 free_srcline(he->srcline);
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 }
John Kacur3d1d07e2009-09-28 15:32:55 +0200982 hist_entry__free(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 Kim043ca3892014-03-03 14:18:00 +09001064 cmp = fmt->sort(a, b);
1065 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 */
1170 h->ms.unfolded = false;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -03001171 h->row_offset = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001172
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001173 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001174
Namhyung Kim9283ba92014-04-24 16:37:26 +09001175 hists__inc_filter_stats(hists, h);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001176 hists__calc_col_len(hists, h);
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001177}
1178
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001179
1180static bool hists__filter_entry_by_dso(struct hists *hists,
1181 struct hist_entry *he)
1182{
1183 if (hists->dso_filter != NULL &&
1184 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
1185 he->filtered |= (1 << HIST_FILTER__DSO);
1186 return true;
1187 }
1188
1189 return false;
1190}
1191
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001192void hists__filter_by_dso(struct hists *hists)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001193{
1194 struct rb_node *nd;
1195
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001196 hists->stats.nr_non_filtered_samples = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001197
1198 hists__reset_filter_stats(hists);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001199 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001200
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001201 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001202 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1203
1204 if (symbol_conf.exclude_other && !h->parent)
1205 continue;
1206
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001207 if (hists__filter_entry_by_dso(hists, h))
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001208 continue;
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001209
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001210 hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001211 }
1212}
1213
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001214static bool hists__filter_entry_by_thread(struct hists *hists,
1215 struct hist_entry *he)
1216{
1217 if (hists->thread_filter != NULL &&
1218 he->thread != hists->thread_filter) {
1219 he->filtered |= (1 << HIST_FILTER__THREAD);
1220 return true;
1221 }
1222
1223 return false;
1224}
1225
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001226void hists__filter_by_thread(struct hists *hists)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001227{
1228 struct rb_node *nd;
1229
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001230 hists->stats.nr_non_filtered_samples = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001231
1232 hists__reset_filter_stats(hists);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001233 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001234
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001235 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001236 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1237
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001238 if (hists__filter_entry_by_thread(hists, h))
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001239 continue;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001240
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001241 hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001242 }
1243}
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001244
Namhyung Kime94d53e2012-03-16 17:50:51 +09001245static bool hists__filter_entry_by_symbol(struct hists *hists,
1246 struct hist_entry *he)
1247{
1248 if (hists->symbol_filter_str != NULL &&
1249 (!he->ms.sym || strstr(he->ms.sym->name,
1250 hists->symbol_filter_str) == NULL)) {
1251 he->filtered |= (1 << HIST_FILTER__SYMBOL);
1252 return true;
1253 }
1254
1255 return false;
1256}
1257
1258void hists__filter_by_symbol(struct hists *hists)
1259{
1260 struct rb_node *nd;
1261
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001262 hists->stats.nr_non_filtered_samples = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001263
1264 hists__reset_filter_stats(hists);
Namhyung Kime94d53e2012-03-16 17:50:51 +09001265 hists__reset_col_len(hists);
1266
1267 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1268 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1269
1270 if (hists__filter_entry_by_symbol(hists, h))
1271 continue;
1272
1273 hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
1274 }
1275}
1276
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001277void events_stats__inc(struct events_stats *stats, u32 type)
1278{
1279 ++stats->nr_events[0];
1280 ++stats->nr_events[type];
1281}
1282
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001283void hists__inc_nr_events(struct hists *hists, u32 type)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001284{
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001285 events_stats__inc(&hists->stats, type);
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001286}
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001287
Namhyung Kim1844dbc2014-05-28 14:12:18 +09001288void hists__inc_nr_samples(struct hists *hists, bool filtered)
1289{
1290 events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
1291 if (!filtered)
1292 hists->stats.nr_non_filtered_samples++;
1293}
1294
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001295static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
1296 struct hist_entry *pair)
1297{
Namhyung Kimce74f602012-12-10 17:29:55 +09001298 struct rb_root *root;
1299 struct rb_node **p;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001300 struct rb_node *parent = NULL;
1301 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -07001302 int64_t cmp;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001303
Namhyung Kimce74f602012-12-10 17:29:55 +09001304 if (sort__need_collapse)
1305 root = &hists->entries_collapsed;
1306 else
1307 root = hists->entries_in;
1308
1309 p = &root->rb_node;
1310
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001311 while (*p != NULL) {
1312 parent = *p;
Namhyung Kimce74f602012-12-10 17:29:55 +09001313 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001314
Namhyung Kimce74f602012-12-10 17:29:55 +09001315 cmp = hist_entry__collapse(he, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001316
1317 if (!cmp)
1318 goto out;
1319
1320 if (cmp < 0)
1321 p = &(*p)->rb_left;
1322 else
1323 p = &(*p)->rb_right;
1324 }
1325
Namhyung Kima0b51af2012-09-11 13:34:27 +09001326 he = hist_entry__new(pair, true);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001327 if (he) {
Arnaldo Carvalho de Melo30193d72012-11-12 13:20:03 -03001328 memset(&he->stat, 0, sizeof(he->stat));
1329 he->hists = hists;
Namhyung Kimce74f602012-12-10 17:29:55 +09001330 rb_link_node(&he->rb_node_in, parent, p);
1331 rb_insert_color(&he->rb_node_in, root);
Namhyung Kim62638352014-04-24 16:21:46 +09001332 hists__inc_stats(hists, he);
Jiri Olsae0af43d2012-12-01 21:18:20 +01001333 he->dummy = true;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001334 }
1335out:
1336 return he;
1337}
1338
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001339static struct hist_entry *hists__find_entry(struct hists *hists,
1340 struct hist_entry *he)
1341{
Namhyung Kimce74f602012-12-10 17:29:55 +09001342 struct rb_node *n;
1343
1344 if (sort__need_collapse)
1345 n = hists->entries_collapsed.rb_node;
1346 else
1347 n = hists->entries_in->rb_node;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001348
1349 while (n) {
Namhyung Kimce74f602012-12-10 17:29:55 +09001350 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
1351 int64_t cmp = hist_entry__collapse(iter, he);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001352
1353 if (cmp < 0)
1354 n = n->rb_left;
1355 else if (cmp > 0)
1356 n = n->rb_right;
1357 else
1358 return iter;
1359 }
1360
1361 return NULL;
1362}
1363
1364/*
1365 * Look for pairs to link to the leader buckets (hist_entries):
1366 */
1367void hists__match(struct hists *leader, struct hists *other)
1368{
Namhyung Kimce74f602012-12-10 17:29:55 +09001369 struct rb_root *root;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001370 struct rb_node *nd;
1371 struct hist_entry *pos, *pair;
1372
Namhyung Kimce74f602012-12-10 17:29:55 +09001373 if (sort__need_collapse)
1374 root = &leader->entries_collapsed;
1375 else
1376 root = leader->entries_in;
1377
1378 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
1379 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001380 pair = hists__find_entry(other, pos);
1381
1382 if (pair)
Namhyung Kim5fa90412012-11-29 15:38:34 +09001383 hist_entry__add_pair(pair, pos);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001384 }
1385}
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001386
1387/*
1388 * Look for entries in the other hists that are not present in the leader, if
1389 * we find them, just add a dummy entry on the leader hists, with period=0,
1390 * nr_events=0, to serve as the list header.
1391 */
1392int hists__link(struct hists *leader, struct hists *other)
1393{
Namhyung Kimce74f602012-12-10 17:29:55 +09001394 struct rb_root *root;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001395 struct rb_node *nd;
1396 struct hist_entry *pos, *pair;
1397
Namhyung Kimce74f602012-12-10 17:29:55 +09001398 if (sort__need_collapse)
1399 root = &other->entries_collapsed;
1400 else
1401 root = other->entries_in;
1402
1403 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
1404 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001405
1406 if (!hist_entry__has_pairs(pos)) {
1407 pair = hists__add_dummy_entry(leader, pos);
1408 if (pair == NULL)
1409 return -1;
Namhyung Kim5fa90412012-11-29 15:38:34 +09001410 hist_entry__add_pair(pos, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001411 }
1412 }
1413
1414 return 0;
1415}
Namhyung Kimf2148332014-01-14 11:52:48 +09001416
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03001417
1418size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp)
1419{
1420 struct perf_evsel *pos;
1421 size_t ret = 0;
1422
1423 evlist__for_each(evlist, pos) {
1424 ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
1425 ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
1426 }
1427
1428 return ret;
1429}
1430
1431
Namhyung Kimf2148332014-01-14 11:52:48 +09001432u64 hists__total_period(struct hists *hists)
1433{
1434 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
1435 hists->stats.total_period;
1436}
Namhyung Kim33db4562014-02-07 12:06:07 +09001437
1438int parse_filter_percentage(const struct option *opt __maybe_unused,
1439 const char *arg, int unset __maybe_unused)
1440{
1441 if (!strcmp(arg, "relative"))
1442 symbol_conf.filter_relative = true;
1443 else if (!strcmp(arg, "absolute"))
1444 symbol_conf.filter_relative = false;
1445 else
1446 return -1;
1447
1448 return 0;
1449}
Namhyung Kim0b93da12014-01-14 12:02:15 +09001450
1451int perf_hist_config(const char *var, const char *value)
1452{
1453 if (!strcmp(var, "hist.percentage"))
1454 return parse_filter_percentage(NULL, value, 0);
1455
1456 return 0;
1457}
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03001458
1459static int hists_evsel__init(struct perf_evsel *evsel)
1460{
1461 struct hists *hists = evsel__hists(evsel);
1462
1463 memset(hists, 0, sizeof(*hists));
1464 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
1465 hists->entries_in = &hists->entries_in_array[0];
1466 hists->entries_collapsed = RB_ROOT;
1467 hists->entries = RB_ROOT;
1468 pthread_mutex_init(&hists->lock, NULL);
1469 return 0;
1470}
1471
1472/*
1473 * XXX We probably need a hists_evsel__exit() to free the hist_entries
1474 * stored in the rbtree...
1475 */
1476
1477int hists__init(void)
1478{
1479 int err = perf_evsel__object_config(sizeof(struct hists_evsel),
1480 hists_evsel__init, NULL);
1481 if (err)
1482 fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);
1483
1484 return err;
1485}