blob: d8662356de202ea26c45ee22d90a41f0f3dd1a42 [file] [log] [blame]
Frederic Weisbecker8a0ecfb2010-05-13 19:47:16 +02001#include "util.h"
Frederic Weisbecker598357e2010-05-21 12:48:39 +02002#include "build-id.h"
John Kacur3d1d07e2009-09-28 15:32:55 +02003#include "hist.h"
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -02004#include "session.h"
5#include "sort.h"
Namhyung Kim29d720e2013-01-22 18:09:33 +09006#include "evsel.h"
Namhyung Kim69bcb012013-10-30 09:40:34 +09007#include "annotate.h"
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -02008#include <math.h>
John Kacur3d1d07e2009-09-28 15:32:55 +02009
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -020010static bool hists__filter_entry_by_dso(struct hists *hists,
11 struct hist_entry *he);
12static bool hists__filter_entry_by_thread(struct hists *hists,
13 struct hist_entry *he);
Namhyung Kime94d53e2012-03-16 17:50:51 +090014static bool hists__filter_entry_by_symbol(struct hists *hists,
15 struct hist_entry *he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -020016
John Kacur3d1d07e2009-09-28 15:32:55 +020017struct callchain_param callchain_param = {
18 .mode = CHAIN_GRAPH_REL,
Sam Liaod797fdc2011-06-07 23:49:46 +080019 .min_percent = 0.5,
Andi Kleen99571ab2013-07-18 15:33:57 -070020 .order = ORDER_CALLEE,
21 .key = CCKEY_FUNCTION
John Kacur3d1d07e2009-09-28 15:32:55 +020022};
23
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030024u16 hists__col_len(struct hists *hists, enum hist_column col)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030025{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030026 return hists->col_len[col];
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030027}
28
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030029void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030030{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030031 hists->col_len[col] = len;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030032}
33
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030034bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030035{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030036 if (len > hists__col_len(hists, col)) {
37 hists__set_col_len(hists, col, len);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030038 return true;
39 }
40 return false;
41}
42
Namhyung Kim7ccf4f92012-08-20 13:52:05 +090043void hists__reset_col_len(struct hists *hists)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030044{
45 enum hist_column col;
46
47 for (col = 0; col < HISTC_NR_COLS; ++col)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030048 hists__set_col_len(hists, col, 0);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030049}
50
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010051static void hists__set_unres_dso_col_len(struct hists *hists, int dso)
52{
53 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
54
55 if (hists__col_len(hists, dso) < unresolved_col_width &&
56 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
57 !symbol_conf.dso_list)
58 hists__set_col_len(hists, dso, unresolved_col_width);
59}
60
Namhyung Kim7ccf4f92012-08-20 13:52:05 +090061void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030062{
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010063 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
Stephane Eranian98a3b322013-01-24 16:10:35 +010064 int symlen;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030065 u16 len;
66
Namhyung Kimded19d52013-04-01 20:35:19 +090067 /*
68 * +4 accounts for '[x] ' priv level info
69 * +2 accounts for 0x prefix on raw addresses
70 * +3 accounts for ' y ' symtab origin info
71 */
72 if (h->ms.sym) {
73 symlen = h->ms.sym->namelen + 4;
74 if (verbose)
75 symlen += BITS_PER_LONG / 4 + 2 + 3;
76 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
77 } else {
Stephane Eranian98a3b322013-01-24 16:10:35 +010078 symlen = unresolved_col_width + 4 + 2;
79 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010080 hists__set_unres_dso_col_len(hists, HISTC_DSO);
Stephane Eranian98a3b322013-01-24 16:10:35 +010081 }
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030082
83 len = thread__comm_len(h->thread);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030084 if (hists__new_col_len(hists, HISTC_COMM, len))
85 hists__set_col_len(hists, HISTC_THREAD, len + 6);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030086
87 if (h->ms.map) {
88 len = dso__name_len(h->ms.map->dso);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030089 hists__new_col_len(hists, HISTC_DSO, len);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030090 }
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010091
Namhyung Kimcb993742012-12-27 18:11:42 +090092 if (h->parent)
93 hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen);
94
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010095 if (h->branch_info) {
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010096 if (h->branch_info->from.sym) {
97 symlen = (int)h->branch_info->from.sym->namelen + 4;
Namhyung Kimded19d52013-04-01 20:35:19 +090098 if (verbose)
99 symlen += BITS_PER_LONG / 4 + 2 + 3;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100100 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
101
102 symlen = dso__name_len(h->branch_info->from.map->dso);
103 hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
104 } else {
105 symlen = unresolved_col_width + 4 + 2;
106 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
107 hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
108 }
109
110 if (h->branch_info->to.sym) {
111 symlen = (int)h->branch_info->to.sym->namelen + 4;
Namhyung Kimded19d52013-04-01 20:35:19 +0900112 if (verbose)
113 symlen += BITS_PER_LONG / 4 + 2 + 3;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100114 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
115
116 symlen = dso__name_len(h->branch_info->to.map->dso);
117 hists__new_col_len(hists, HISTC_DSO_TO, symlen);
118 } else {
119 symlen = unresolved_col_width + 4 + 2;
120 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
121 hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
122 }
123 }
Stephane Eranian98a3b322013-01-24 16:10:35 +0100124
125 if (h->mem_info) {
Stephane Eranian98a3b322013-01-24 16:10:35 +0100126 if (h->mem_info->daddr.sym) {
127 symlen = (int)h->mem_info->daddr.sym->namelen + 4
128 + unresolved_col_width + 2;
129 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
130 symlen);
131 } else {
132 symlen = unresolved_col_width + 4 + 2;
133 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
134 symlen);
135 }
136 if (h->mem_info->daddr.map) {
137 symlen = dso__name_len(h->mem_info->daddr.map->dso);
138 hists__new_col_len(hists, HISTC_MEM_DADDR_DSO,
139 symlen);
140 } else {
141 symlen = unresolved_col_width + 4 + 2;
142 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
143 }
144 } else {
145 symlen = unresolved_col_width + 4 + 2;
146 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen);
147 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
148 }
149
150 hists__new_col_len(hists, HISTC_MEM_LOCKED, 6);
151 hists__new_col_len(hists, HISTC_MEM_TLB, 22);
152 hists__new_col_len(hists, HISTC_MEM_SNOOP, 12);
153 hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3);
154 hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12);
155 hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12);
Andi Kleen475eeab2013-09-20 07:40:43 -0700156
157 if (h->transaction)
158 hists__new_col_len(hists, HISTC_TRANSACTION,
159 hist_entry__transaction_len());
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300160}
161
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900162void hists__output_recalc_col_len(struct hists *hists, int max_rows)
163{
164 struct rb_node *next = rb_first(&hists->entries);
165 struct hist_entry *n;
166 int row = 0;
167
168 hists__reset_col_len(hists);
169
170 while (next && row++ < max_rows) {
171 n = rb_entry(next, struct hist_entry, rb_node);
172 if (!n->filtered)
173 hists__calc_col_len(hists, n);
174 next = rb_next(&n->rb_node);
175 }
176}
177
Namhyung Kimf39056f2014-01-14 14:25:37 +0900178static void he_stat__add_cpumode_period(struct he_stat *he_stat,
179 unsigned int cpumode, u64 period)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800180{
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300181 switch (cpumode) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800182 case PERF_RECORD_MISC_KERNEL:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900183 he_stat->period_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800184 break;
185 case PERF_RECORD_MISC_USER:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900186 he_stat->period_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800187 break;
188 case PERF_RECORD_MISC_GUEST_KERNEL:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900189 he_stat->period_guest_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800190 break;
191 case PERF_RECORD_MISC_GUEST_USER:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900192 he_stat->period_guest_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800193 break;
194 default:
195 break;
196 }
197}
198
Andi Kleen05484292013-01-24 16:10:29 +0100199static void he_stat__add_period(struct he_stat *he_stat, u64 period,
200 u64 weight)
Namhyung Kim139c0812012-10-04 21:49:43 +0900201{
Stephane Eranian98a3b322013-01-24 16:10:35 +0100202
Namhyung Kim139c0812012-10-04 21:49:43 +0900203 he_stat->period += period;
Andi Kleen05484292013-01-24 16:10:29 +0100204 he_stat->weight += weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900205 he_stat->nr_events += 1;
206}
207
208static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
209{
210 dest->period += src->period;
211 dest->period_sys += src->period_sys;
212 dest->period_us += src->period_us;
213 dest->period_guest_sys += src->period_guest_sys;
214 dest->period_guest_us += src->period_guest_us;
215 dest->nr_events += src->nr_events;
Andi Kleen05484292013-01-24 16:10:29 +0100216 dest->weight += src->weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900217}
218
Namhyung Kimf39056f2014-01-14 14:25:37 +0900219static void he_stat__decay(struct he_stat *he_stat)
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300220{
Namhyung Kimf39056f2014-01-14 14:25:37 +0900221 he_stat->period = (he_stat->period * 7) / 8;
222 he_stat->nr_events = (he_stat->nr_events * 7) / 8;
Andi Kleen05484292013-01-24 16:10:29 +0100223 /* XXX need decay for weight too? */
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300224}
225
226static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
227{
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900228 u64 prev_period = he->stat.period;
Namhyung Kim3186b682014-04-22 13:44:23 +0900229 u64 diff;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200230
231 if (prev_period == 0)
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300232 return true;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200233
Namhyung Kimf39056f2014-01-14 14:25:37 +0900234 he_stat__decay(&he->stat);
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200235
Namhyung Kim3186b682014-04-22 13:44:23 +0900236 diff = prev_period - he->stat.period;
237
238 hists->stats.total_period -= diff;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200239 if (!he->filtered)
Namhyung Kim3186b682014-04-22 13:44:23 +0900240 hists->stats.total_non_filtered_period -= diff;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200241
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900242 return he->stat.period == 0;
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300243}
244
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900245void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300246{
247 struct rb_node *next = rb_first(&hists->entries);
248 struct hist_entry *n;
249
250 while (next) {
251 n = rb_entry(next, struct hist_entry, rb_node);
252 next = rb_next(&n->rb_node);
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300253 /*
254 * We may be annotating this, for instance, so keep it here in
255 * case some it gets new samples, we'll eventually free it when
256 * the user stops browsing and it agains gets fully decayed.
257 */
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200258 if (((zap_user && n->level == '.') ||
259 (zap_kernel && n->level != '.') ||
260 hists__decay_entry(hists, n)) &&
261 !n->used) {
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300262 rb_erase(&n->rb_node, &hists->entries);
263
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900264 if (sort__need_collapse)
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300265 rb_erase(&n->rb_node_in, &hists->entries_collapsed);
266
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300267 --hists->nr_entries;
Namhyung Kim3186b682014-04-22 13:44:23 +0900268 if (!n->filtered)
269 --hists->nr_non_filtered_entries;
270
271 hist_entry__free(n);
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300272 }
273 }
274}
275
John Kacur3d1d07e2009-09-28 15:32:55 +0200276/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300277 * histogram, sorted on item, collects periods
John Kacur3d1d07e2009-09-28 15:32:55 +0200278 */
279
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300280static struct hist_entry *hist_entry__new(struct hist_entry *template)
281{
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +0200282 size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0;
Stephane Eranian98a3b322013-01-24 16:10:35 +0100283 struct hist_entry *he = zalloc(sizeof(*he) + callchain_size);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300284
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200285 if (he != NULL) {
286 *he = *template;
Namhyung Kimc4b35352012-10-04 21:49:42 +0900287
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200288 if (he->ms.map)
289 he->ms.map->referenced = true;
Stephane Eranian3cf0cb12013-01-14 15:02:45 +0100290
291 if (he->branch_info) {
Namhyung Kim26353a62013-04-01 20:35:17 +0900292 /*
293 * This branch info is (a part of) allocated from
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -0300294 * sample__resolve_bstack() and will be freed after
Namhyung Kim26353a62013-04-01 20:35:17 +0900295 * adding new entries. So we need to save a copy.
296 */
297 he->branch_info = malloc(sizeof(*he->branch_info));
298 if (he->branch_info == NULL) {
299 free(he);
300 return NULL;
301 }
302
303 memcpy(he->branch_info, template->branch_info,
304 sizeof(*he->branch_info));
305
Stephane Eranian3cf0cb12013-01-14 15:02:45 +0100306 if (he->branch_info->from.map)
307 he->branch_info->from.map->referenced = true;
308 if (he->branch_info->to.map)
309 he->branch_info->to.map->referenced = true;
310 }
311
Stephane Eranian98a3b322013-01-24 16:10:35 +0100312 if (he->mem_info) {
313 if (he->mem_info->iaddr.map)
314 he->mem_info->iaddr.map->referenced = true;
315 if (he->mem_info->daddr.map)
316 he->mem_info->daddr.map->referenced = true;
317 }
318
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300319 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200320 callchain_init(he->callchain);
Arnaldo Carvalho de Melob821c732012-10-25 14:42:45 -0200321
322 INIT_LIST_HEAD(&he->pairs.node);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300323 }
324
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200325 return he;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300326}
327
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300328static u8 symbol__parent_filter(const struct symbol *parent)
329{
330 if (symbol_conf.exclude_other && parent == NULL)
331 return 1 << HIST_FILTER__PARENT;
332 return 0;
333}
334
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100335static struct hist_entry *add_hist_entry(struct hists *hists,
Namhyung Kimf1cbf782013-12-18 14:21:11 +0900336 struct hist_entry *entry,
337 struct addr_location *al)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300338{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300339 struct rb_node **p;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300340 struct rb_node *parent = NULL;
341 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -0700342 int64_t cmp;
Namhyung Kimf1cbf782013-12-18 14:21:11 +0900343 u64 period = entry->stat.period;
344 u64 weight = entry->stat.weight;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300345
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300346 p = &hists->entries_in->rb_node;
347
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300348 while (*p != NULL) {
349 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300350 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300351
Namhyung Kim9afcf932012-12-10 17:29:54 +0900352 /*
353 * Make sure that it receives arguments in a same order as
354 * hist_entry__collapse() so that we can use an appropriate
355 * function when searching an entry regardless which sort
356 * keys were used.
357 */
358 cmp = hist_entry__cmp(he, entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300359
360 if (!cmp) {
Andi Kleen05484292013-01-24 16:10:29 +0100361 he_stat__add_period(&he->stat, period, weight);
David Miller63fa4712012-03-27 03:14:18 -0400362
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900363 /*
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -0300364 * This mem info was allocated from sample__resolve_mem
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900365 * and will not be used anymore.
366 */
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300367 zfree(&entry->mem_info);
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900368
David Miller63fa4712012-03-27 03:14:18 -0400369 /* If the map of an existing hist_entry has
370 * become out-of-date due to an exec() or
371 * similar, update it. Otherwise we will
372 * mis-adjust symbol addresses when computing
373 * the history counter to increment.
374 */
375 if (he->ms.map != entry->ms.map) {
376 he->ms.map = entry->ms.map;
377 if (he->ms.map)
378 he->ms.map->referenced = true;
379 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300380 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300381 }
382
383 if (cmp < 0)
384 p = &(*p)->rb_left;
385 else
386 p = &(*p)->rb_right;
387 }
388
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100389 he = hist_entry__new(entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300390 if (!he)
Namhyung Kim27a0dcb2013-05-14 11:09:02 +0900391 return NULL;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300392
393 rb_link_node(&he->rb_node_in, parent, p);
394 rb_insert_color(&he->rb_node_in, hists->entries_in);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300395out:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900396 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300397 return he;
398}
399
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300400struct hist_entry *__hists__add_entry(struct hists *hists,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100401 struct addr_location *al,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900402 struct symbol *sym_parent,
403 struct branch_info *bi,
404 struct mem_info *mi,
405 u64 period, u64 weight, u64 transaction)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100406{
407 struct hist_entry entry = {
408 .thread = al->thread,
Namhyung Kim4dfced32013-09-13 16:28:57 +0900409 .comm = thread__comm(al->thread),
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100410 .ms = {
411 .map = al->map,
412 .sym = al->sym,
413 },
414 .cpu = al->cpu,
415 .ip = al->addr,
416 .level = al->level,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900417 .stat = {
Namhyung Kimc4b35352012-10-04 21:49:42 +0900418 .nr_events = 1,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900419 .period = period,
Andi Kleen05484292013-01-24 16:10:29 +0100420 .weight = weight,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900421 },
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100422 .parent = sym_parent,
Namhyung Kim2c86c7c2014-03-17 18:18:54 -0300423 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300424 .hists = hists,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900425 .branch_info = bi,
426 .mem_info = mi,
Andi Kleen475eeab2013-09-20 07:40:43 -0700427 .transaction = transaction,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100428 };
429
Namhyung Kimf1cbf782013-12-18 14:21:11 +0900430 return add_hist_entry(hists, &entry, al);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100431}
432
Namhyung Kim69bcb012013-10-30 09:40:34 +0900433static int
434iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
435 struct addr_location *al __maybe_unused)
436{
437 return 0;
438}
439
440static int
441iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
442 struct addr_location *al __maybe_unused)
443{
444 return 0;
445}
446
447static int
448iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
449{
450 struct perf_sample *sample = iter->sample;
451 struct mem_info *mi;
452
453 mi = sample__resolve_mem(sample, al);
454 if (mi == NULL)
455 return -ENOMEM;
456
457 iter->priv = mi;
458 return 0;
459}
460
461static int
462iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
463{
464 u64 cost;
465 struct mem_info *mi = iter->priv;
466 struct hist_entry *he;
467
468 if (mi == NULL)
469 return -EINVAL;
470
471 cost = iter->sample->weight;
472 if (!cost)
473 cost = 1;
474
475 /*
476 * must pass period=weight in order to get the correct
477 * sorting from hists__collapse_resort() which is solely
478 * based on periods. We want sorting be done on nr_events * weight
479 * and this is indirectly achieved by passing period=weight here
480 * and the he_stat__add_period() function.
481 */
482 he = __hists__add_entry(&iter->evsel->hists, al, iter->parent, NULL, mi,
483 cost, cost, 0);
484 if (!he)
485 return -ENOMEM;
486
487 iter->he = he;
488 return 0;
489}
490
491static int
492iter_finish_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
493{
494 struct perf_evsel *evsel = iter->evsel;
495 struct hist_entry *he = iter->he;
496 struct mem_info *mx;
497 int err = -EINVAL;
498
499 if (he == NULL)
500 goto out;
501
502 if (ui__has_annotation()) {
503 err = hist_entry__inc_addr_samples(he, evsel->idx, al->addr);
504 if (err)
505 goto out;
506
507 mx = he->mem_info;
508 err = addr_map_symbol__inc_samples(&mx->daddr, evsel->idx);
509 if (err)
510 goto out;
511 }
512
513 hists__inc_nr_samples(&evsel->hists, he->filtered);
514
515 err = hist_entry__append_callchain(he, iter->sample);
516
517out:
518 /*
519 * We don't need to free iter->priv (mem_info) here since
520 * the mem info was either already freed in add_hist_entry() or
521 * passed to a new hist entry by hist_entry__new().
522 */
523 iter->priv = NULL;
524
525 iter->he = NULL;
526 return err;
527}
528
529static int
530iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
531{
532 struct branch_info *bi;
533 struct perf_sample *sample = iter->sample;
534
535 bi = sample__resolve_bstack(sample, al);
536 if (!bi)
537 return -ENOMEM;
538
539 iter->curr = 0;
540 iter->total = sample->branch_stack->nr;
541
542 iter->priv = bi;
543 return 0;
544}
545
546static int
547iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused,
548 struct addr_location *al __maybe_unused)
549{
550 return 0;
551}
552
553static int
554iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
555{
556 struct branch_info *bi = iter->priv;
557 int i = iter->curr;
558
559 if (bi == NULL)
560 return 0;
561
562 if (iter->curr >= iter->total)
563 return 0;
564
565 al->map = bi[i].to.map;
566 al->sym = bi[i].to.sym;
567 al->addr = bi[i].to.addr;
568 return 1;
569}
570
571static int
572iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
573{
574 struct branch_info *bi, *bx;
575 struct perf_evsel *evsel = iter->evsel;
576 struct hist_entry *he = NULL;
577 int i = iter->curr;
578 int err = 0;
579
580 bi = iter->priv;
581
582 if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym))
583 goto out;
584
585 /*
586 * The report shows the percentage of total branches captured
587 * and not events sampled. Thus we use a pseudo period of 1.
588 */
589 he = __hists__add_entry(&evsel->hists, al, iter->parent, &bi[i], NULL,
590 1, 1, 0);
591 if (he == NULL)
592 return -ENOMEM;
593
594 if (ui__has_annotation()) {
595 bx = he->branch_info;
596 err = addr_map_symbol__inc_samples(&bx->from, evsel->idx);
597 if (err)
598 goto out;
599
600 err = addr_map_symbol__inc_samples(&bx->to, evsel->idx);
601 if (err)
602 goto out;
603 }
604
605 hists__inc_nr_samples(&evsel->hists, he->filtered);
606
607out:
608 iter->he = he;
609 iter->curr++;
610 return err;
611}
612
613static int
614iter_finish_branch_entry(struct hist_entry_iter *iter,
615 struct addr_location *al __maybe_unused)
616{
617 zfree(&iter->priv);
618 iter->he = NULL;
619
620 return iter->curr >= iter->total ? 0 : -1;
621}
622
623static int
624iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
625 struct addr_location *al __maybe_unused)
626{
627 return 0;
628}
629
630static int
631iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
632{
633 struct perf_evsel *evsel = iter->evsel;
634 struct perf_sample *sample = iter->sample;
635 struct hist_entry *he;
636
637 he = __hists__add_entry(&evsel->hists, al, iter->parent, NULL, NULL,
638 sample->period, sample->weight,
639 sample->transaction);
640 if (he == NULL)
641 return -ENOMEM;
642
643 iter->he = he;
644 return 0;
645}
646
647static int
648iter_finish_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
649{
650 int err;
651 struct hist_entry *he = iter->he;
652 struct perf_evsel *evsel = iter->evsel;
653 struct perf_sample *sample = iter->sample;
654
655 if (he == NULL)
656 return 0;
657
658 iter->he = NULL;
659
660 if (ui__has_annotation()) {
661 err = hist_entry__inc_addr_samples(he, evsel->idx, al->addr);
662 if (err)
663 return err;
664 }
665
666 hists__inc_nr_samples(&evsel->hists, he->filtered);
667
668 return hist_entry__append_callchain(he, sample);
669}
670
671const struct hist_iter_ops hist_iter_mem = {
672 .prepare_entry = iter_prepare_mem_entry,
673 .add_single_entry = iter_add_single_mem_entry,
674 .next_entry = iter_next_nop_entry,
675 .add_next_entry = iter_add_next_nop_entry,
676 .finish_entry = iter_finish_mem_entry,
677};
678
679const struct hist_iter_ops hist_iter_branch = {
680 .prepare_entry = iter_prepare_branch_entry,
681 .add_single_entry = iter_add_single_branch_entry,
682 .next_entry = iter_next_branch_entry,
683 .add_next_entry = iter_add_next_branch_entry,
684 .finish_entry = iter_finish_branch_entry,
685};
686
687const struct hist_iter_ops hist_iter_normal = {
688 .prepare_entry = iter_prepare_normal_entry,
689 .add_single_entry = iter_add_single_normal_entry,
690 .next_entry = iter_next_nop_entry,
691 .add_next_entry = iter_add_next_nop_entry,
692 .finish_entry = iter_finish_normal_entry,
693};
694
695int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
696 struct perf_evsel *evsel, struct perf_sample *sample,
697 int max_stack_depth)
698{
699 int err, err2;
700
701 err = sample__resolve_callchain(sample, &iter->parent, evsel, al,
702 max_stack_depth);
703 if (err)
704 return err;
705
706 iter->evsel = evsel;
707 iter->sample = sample;
708
709 err = iter->ops->prepare_entry(iter, al);
710 if (err)
711 goto out;
712
713 err = iter->ops->add_single_entry(iter, al);
714 if (err)
715 goto out;
716
717 while (iter->ops->next_entry(iter, al)) {
718 err = iter->ops->add_next_entry(iter, al);
719 if (err)
720 break;
721 }
722
723out:
724 err2 = iter->ops->finish_entry(iter, al);
725 if (!err)
726 err = err2;
727
728 return err;
729}
730
John Kacur3d1d07e2009-09-28 15:32:55 +0200731int64_t
732hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
733{
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900734 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +0200735 int64_t cmp = 0;
736
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900737 perf_hpp__for_each_sort_list(fmt) {
Namhyung Kime67d49a2014-03-18 13:00:59 +0900738 if (perf_hpp__should_skip(fmt))
739 continue;
740
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900741 cmp = fmt->cmp(left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200742 if (cmp)
743 break;
744 }
745
746 return cmp;
747}
748
749int64_t
750hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
751{
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900752 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +0200753 int64_t cmp = 0;
754
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900755 perf_hpp__for_each_sort_list(fmt) {
Namhyung Kime67d49a2014-03-18 13:00:59 +0900756 if (perf_hpp__should_skip(fmt))
757 continue;
758
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900759 cmp = fmt->collapse(left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200760 if (cmp)
761 break;
762 }
763
764 return cmp;
765}
766
767void hist_entry__free(struct hist_entry *he)
768{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300769 zfree(&he->branch_info);
770 zfree(&he->mem_info);
Namhyung Kimf048d542013-09-11 14:09:28 +0900771 free_srcline(he->srcline);
John Kacur3d1d07e2009-09-28 15:32:55 +0200772 free(he);
773}
774
775/*
776 * collapse the histogram
777 */
778
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300779static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100780 struct rb_root *root,
781 struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +0200782{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200783 struct rb_node **p = &root->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200784 struct rb_node *parent = NULL;
785 struct hist_entry *iter;
786 int64_t cmp;
787
788 while (*p != NULL) {
789 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300790 iter = rb_entry(parent, struct hist_entry, rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +0200791
792 cmp = hist_entry__collapse(iter, he);
793
794 if (!cmp) {
Namhyung Kim139c0812012-10-04 21:49:43 +0900795 he_stat__add_stat(&iter->stat, &he->stat);
Namhyung Kim9ec60972012-09-26 16:47:28 +0900796
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100797 if (symbol_conf.use_callchain) {
Namhyung Kim47260642012-05-31 14:43:26 +0900798 callchain_cursor_reset(&callchain_cursor);
799 callchain_merge(&callchain_cursor,
800 iter->callchain,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100801 he->callchain);
802 }
John Kacur3d1d07e2009-09-28 15:32:55 +0200803 hist_entry__free(he);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300804 return false;
John Kacur3d1d07e2009-09-28 15:32:55 +0200805 }
806
807 if (cmp < 0)
808 p = &(*p)->rb_left;
809 else
810 p = &(*p)->rb_right;
811 }
812
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300813 rb_link_node(&he->rb_node_in, parent, p);
814 rb_insert_color(&he->rb_node_in, root);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300815 return true;
John Kacur3d1d07e2009-09-28 15:32:55 +0200816}
817
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300818static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
819{
820 struct rb_root *root;
821
822 pthread_mutex_lock(&hists->lock);
823
824 root = hists->entries_in;
825 if (++hists->entries_in > &hists->entries_in_array[1])
826 hists->entries_in = &hists->entries_in_array[0];
827
828 pthread_mutex_unlock(&hists->lock);
829
830 return root;
831}
832
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200833static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
834{
835 hists__filter_entry_by_dso(hists, he);
836 hists__filter_entry_by_thread(hists, he);
Namhyung Kime94d53e2012-03-16 17:50:51 +0900837 hists__filter_entry_by_symbol(hists, he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200838}
839
Namhyung Kimc1fb5652013-10-11 14:15:38 +0900840void hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300841{
842 struct rb_root *root;
843 struct rb_node *next;
844 struct hist_entry *n;
845
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900846 if (!sort__need_collapse)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300847 return;
848
849 root = hists__get_rotate_entries_in(hists);
850 next = rb_first(root);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300851
852 while (next) {
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -0300853 if (session_done())
854 break;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300855 n = rb_entry(next, struct hist_entry, rb_node_in);
856 next = rb_next(&n->rb_node_in);
857
858 rb_erase(&n->rb_node_in, root);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200859 if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
860 /*
861 * If it wasn't combined with one of the entries already
862 * collapsed, we need to apply the filters that may have
863 * been set by, say, the hist_browser.
864 */
865 hists__apply_filters(hists, n);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200866 }
Namhyung Kimc1fb5652013-10-11 14:15:38 +0900867 if (prog)
868 ui_progress__update(prog, 1);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300869 }
870}
871
Namhyung Kim043ca3892014-03-03 14:18:00 +0900872static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
Namhyung Kim29d720e2013-01-22 18:09:33 +0900873{
Namhyung Kim043ca3892014-03-03 14:18:00 +0900874 struct perf_hpp_fmt *fmt;
875 int64_t cmp = 0;
Namhyung Kim29d720e2013-01-22 18:09:33 +0900876
Namhyung Kim26d8b332014-03-03 16:16:20 +0900877 perf_hpp__for_each_sort_list(fmt) {
Namhyung Kime67d49a2014-03-18 13:00:59 +0900878 if (perf_hpp__should_skip(fmt))
879 continue;
880
Namhyung Kim043ca3892014-03-03 14:18:00 +0900881 cmp = fmt->sort(a, b);
882 if (cmp)
Namhyung Kim29d720e2013-01-22 18:09:33 +0900883 break;
884 }
885
Namhyung Kim043ca3892014-03-03 14:18:00 +0900886 return cmp;
Namhyung Kim29d720e2013-01-22 18:09:33 +0900887}
888
Namhyung Kim9283ba92014-04-24 16:37:26 +0900889static void hists__reset_filter_stats(struct hists *hists)
890{
891 hists->nr_non_filtered_entries = 0;
892 hists->stats.total_non_filtered_period = 0;
893}
894
895void hists__reset_stats(struct hists *hists)
896{
897 hists->nr_entries = 0;
898 hists->stats.total_period = 0;
899
900 hists__reset_filter_stats(hists);
901}
902
903static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
904{
905 hists->nr_non_filtered_entries++;
906 hists->stats.total_non_filtered_period += h->stat.period;
907}
908
909void hists__inc_stats(struct hists *hists, struct hist_entry *h)
910{
911 if (!h->filtered)
912 hists__inc_filter_stats(hists, h);
913
914 hists->nr_entries++;
915 hists->stats.total_period += h->stat.period;
916}
917
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300918static void __hists__insert_output_entry(struct rb_root *entries,
919 struct hist_entry *he,
920 u64 min_callchain_hits)
John Kacur3d1d07e2009-09-28 15:32:55 +0200921{
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300922 struct rb_node **p = &entries->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200923 struct rb_node *parent = NULL;
924 struct hist_entry *iter;
925
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200926 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melob9fb9302010-04-02 09:50:42 -0300927 callchain_param.sort(&he->sorted_chain, he->callchain,
John Kacur3d1d07e2009-09-28 15:32:55 +0200928 min_callchain_hits, &callchain_param);
929
930 while (*p != NULL) {
931 parent = *p;
932 iter = rb_entry(parent, struct hist_entry, rb_node);
933
Namhyung Kim043ca3892014-03-03 14:18:00 +0900934 if (hist_entry__sort(he, iter) > 0)
John Kacur3d1d07e2009-09-28 15:32:55 +0200935 p = &(*p)->rb_left;
936 else
937 p = &(*p)->rb_right;
938 }
939
940 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300941 rb_insert_color(&he->rb_node, entries);
John Kacur3d1d07e2009-09-28 15:32:55 +0200942}
943
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900944void hists__output_resort(struct hists *hists)
John Kacur3d1d07e2009-09-28 15:32:55 +0200945{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300946 struct rb_root *root;
John Kacur3d1d07e2009-09-28 15:32:55 +0200947 struct rb_node *next;
948 struct hist_entry *n;
John Kacur3d1d07e2009-09-28 15:32:55 +0200949 u64 min_callchain_hits;
950
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300951 min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
John Kacur3d1d07e2009-09-28 15:32:55 +0200952
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900953 if (sort__need_collapse)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300954 root = &hists->entries_collapsed;
955 else
956 root = hists->entries_in;
957
958 next = rb_first(root);
959 hists->entries = RB_ROOT;
John Kacur3d1d07e2009-09-28 15:32:55 +0200960
Namhyung Kim9283ba92014-04-24 16:37:26 +0900961 hists__reset_stats(hists);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300962 hists__reset_col_len(hists);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300963
John Kacur3d1d07e2009-09-28 15:32:55 +0200964 while (next) {
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300965 n = rb_entry(next, struct hist_entry, rb_node_in);
966 next = rb_next(&n->rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +0200967
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300968 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
Namhyung Kim62638352014-04-24 16:21:46 +0900969 hists__inc_stats(hists, n);
Namhyung Kimae993ef2014-04-24 16:25:19 +0900970
971 if (!n->filtered)
972 hists__calc_col_len(hists, n);
John Kacur3d1d07e2009-09-28 15:32:55 +0200973 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300974}
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200975
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300976static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300977 enum hist_filter filter)
978{
979 h->filtered &= ~(1 << filter);
980 if (h->filtered)
981 return;
982
Namhyung Kim87e90f42014-04-24 16:44:16 +0900983 /* force fold unfiltered entry for simplicity */
984 h->ms.unfolded = false;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -0300985 h->row_offset = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +0900986
Namhyung Kim1ab1fa52013-12-26 15:11:52 +0900987 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300988
Namhyung Kim9283ba92014-04-24 16:37:26 +0900989 hists__inc_filter_stats(hists, h);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300990 hists__calc_col_len(hists, h);
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300991}
992
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200993
994static bool hists__filter_entry_by_dso(struct hists *hists,
995 struct hist_entry *he)
996{
997 if (hists->dso_filter != NULL &&
998 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
999 he->filtered |= (1 << HIST_FILTER__DSO);
1000 return true;
1001 }
1002
1003 return false;
1004}
1005
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001006void hists__filter_by_dso(struct hists *hists)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001007{
1008 struct rb_node *nd;
1009
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001010 hists->stats.nr_non_filtered_samples = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001011
1012 hists__reset_filter_stats(hists);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001013 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001014
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001015 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001016 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1017
1018 if (symbol_conf.exclude_other && !h->parent)
1019 continue;
1020
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001021 if (hists__filter_entry_by_dso(hists, h))
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001022 continue;
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001023
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001024 hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001025 }
1026}
1027
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001028static bool hists__filter_entry_by_thread(struct hists *hists,
1029 struct hist_entry *he)
1030{
1031 if (hists->thread_filter != NULL &&
1032 he->thread != hists->thread_filter) {
1033 he->filtered |= (1 << HIST_FILTER__THREAD);
1034 return true;
1035 }
1036
1037 return false;
1038}
1039
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001040void hists__filter_by_thread(struct hists *hists)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001041{
1042 struct rb_node *nd;
1043
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001044 hists->stats.nr_non_filtered_samples = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001045
1046 hists__reset_filter_stats(hists);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001047 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001048
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001049 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001050 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1051
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001052 if (hists__filter_entry_by_thread(hists, h))
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001053 continue;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001054
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001055 hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001056 }
1057}
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001058
Namhyung Kime94d53e2012-03-16 17:50:51 +09001059static bool hists__filter_entry_by_symbol(struct hists *hists,
1060 struct hist_entry *he)
1061{
1062 if (hists->symbol_filter_str != NULL &&
1063 (!he->ms.sym || strstr(he->ms.sym->name,
1064 hists->symbol_filter_str) == NULL)) {
1065 he->filtered |= (1 << HIST_FILTER__SYMBOL);
1066 return true;
1067 }
1068
1069 return false;
1070}
1071
1072void hists__filter_by_symbol(struct hists *hists)
1073{
1074 struct rb_node *nd;
1075
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001076 hists->stats.nr_non_filtered_samples = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001077
1078 hists__reset_filter_stats(hists);
Namhyung Kime94d53e2012-03-16 17:50:51 +09001079 hists__reset_col_len(hists);
1080
1081 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1082 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1083
1084 if (hists__filter_entry_by_symbol(hists, h))
1085 continue;
1086
1087 hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
1088 }
1089}
1090
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001091void events_stats__inc(struct events_stats *stats, u32 type)
1092{
1093 ++stats->nr_events[0];
1094 ++stats->nr_events[type];
1095}
1096
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001097void hists__inc_nr_events(struct hists *hists, u32 type)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001098{
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001099 events_stats__inc(&hists->stats, type);
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001100}
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001101
Namhyung Kim1844dbc2014-05-28 14:12:18 +09001102void hists__inc_nr_samples(struct hists *hists, bool filtered)
1103{
1104 events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
1105 if (!filtered)
1106 hists->stats.nr_non_filtered_samples++;
1107}
1108
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001109static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
1110 struct hist_entry *pair)
1111{
Namhyung Kimce74f602012-12-10 17:29:55 +09001112 struct rb_root *root;
1113 struct rb_node **p;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001114 struct rb_node *parent = NULL;
1115 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -07001116 int64_t cmp;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001117
Namhyung Kimce74f602012-12-10 17:29:55 +09001118 if (sort__need_collapse)
1119 root = &hists->entries_collapsed;
1120 else
1121 root = hists->entries_in;
1122
1123 p = &root->rb_node;
1124
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001125 while (*p != NULL) {
1126 parent = *p;
Namhyung Kimce74f602012-12-10 17:29:55 +09001127 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001128
Namhyung Kimce74f602012-12-10 17:29:55 +09001129 cmp = hist_entry__collapse(he, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001130
1131 if (!cmp)
1132 goto out;
1133
1134 if (cmp < 0)
1135 p = &(*p)->rb_left;
1136 else
1137 p = &(*p)->rb_right;
1138 }
1139
1140 he = hist_entry__new(pair);
1141 if (he) {
Arnaldo Carvalho de Melo30193d72012-11-12 13:20:03 -03001142 memset(&he->stat, 0, sizeof(he->stat));
1143 he->hists = hists;
Namhyung Kimce74f602012-12-10 17:29:55 +09001144 rb_link_node(&he->rb_node_in, parent, p);
1145 rb_insert_color(&he->rb_node_in, root);
Namhyung Kim62638352014-04-24 16:21:46 +09001146 hists__inc_stats(hists, he);
Jiri Olsae0af43d2012-12-01 21:18:20 +01001147 he->dummy = true;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001148 }
1149out:
1150 return he;
1151}
1152
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001153static struct hist_entry *hists__find_entry(struct hists *hists,
1154 struct hist_entry *he)
1155{
Namhyung Kimce74f602012-12-10 17:29:55 +09001156 struct rb_node *n;
1157
1158 if (sort__need_collapse)
1159 n = hists->entries_collapsed.rb_node;
1160 else
1161 n = hists->entries_in->rb_node;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001162
1163 while (n) {
Namhyung Kimce74f602012-12-10 17:29:55 +09001164 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
1165 int64_t cmp = hist_entry__collapse(iter, he);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001166
1167 if (cmp < 0)
1168 n = n->rb_left;
1169 else if (cmp > 0)
1170 n = n->rb_right;
1171 else
1172 return iter;
1173 }
1174
1175 return NULL;
1176}
1177
1178/*
1179 * Look for pairs to link to the leader buckets (hist_entries):
1180 */
1181void hists__match(struct hists *leader, struct hists *other)
1182{
Namhyung Kimce74f602012-12-10 17:29:55 +09001183 struct rb_root *root;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001184 struct rb_node *nd;
1185 struct hist_entry *pos, *pair;
1186
Namhyung Kimce74f602012-12-10 17:29:55 +09001187 if (sort__need_collapse)
1188 root = &leader->entries_collapsed;
1189 else
1190 root = leader->entries_in;
1191
1192 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
1193 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001194 pair = hists__find_entry(other, pos);
1195
1196 if (pair)
Namhyung Kim5fa90412012-11-29 15:38:34 +09001197 hist_entry__add_pair(pair, pos);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001198 }
1199}
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001200
1201/*
1202 * Look for entries in the other hists that are not present in the leader, if
1203 * we find them, just add a dummy entry on the leader hists, with period=0,
1204 * nr_events=0, to serve as the list header.
1205 */
1206int hists__link(struct hists *leader, struct hists *other)
1207{
Namhyung Kimce74f602012-12-10 17:29:55 +09001208 struct rb_root *root;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001209 struct rb_node *nd;
1210 struct hist_entry *pos, *pair;
1211
Namhyung Kimce74f602012-12-10 17:29:55 +09001212 if (sort__need_collapse)
1213 root = &other->entries_collapsed;
1214 else
1215 root = other->entries_in;
1216
1217 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
1218 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001219
1220 if (!hist_entry__has_pairs(pos)) {
1221 pair = hists__add_dummy_entry(leader, pos);
1222 if (pair == NULL)
1223 return -1;
Namhyung Kim5fa90412012-11-29 15:38:34 +09001224 hist_entry__add_pair(pos, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001225 }
1226 }
1227
1228 return 0;
1229}
Namhyung Kimf2148332014-01-14 11:52:48 +09001230
1231u64 hists__total_period(struct hists *hists)
1232{
1233 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
1234 hists->stats.total_period;
1235}
Namhyung Kim33db4562014-02-07 12:06:07 +09001236
1237int parse_filter_percentage(const struct option *opt __maybe_unused,
1238 const char *arg, int unset __maybe_unused)
1239{
1240 if (!strcmp(arg, "relative"))
1241 symbol_conf.filter_relative = true;
1242 else if (!strcmp(arg, "absolute"))
1243 symbol_conf.filter_relative = false;
1244 else
1245 return -1;
1246
1247 return 0;
1248}
Namhyung Kim0b93da12014-01-14 12:02:15 +09001249
1250int perf_hist_config(const char *var, const char *value)
1251{
1252 if (!strcmp(var, "hist.percentage"))
1253 return parse_filter_percentage(NULL, value, 0);
1254
1255 return 0;
1256}