blob: dfff2ee8effb6cd067bd331f57487d6cecfd1587 [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);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900235 if (symbol_conf.cumulate_callchain)
236 he_stat__decay(he->stat_acc);
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200237
Namhyung Kim3186b682014-04-22 13:44:23 +0900238 diff = prev_period - he->stat.period;
239
240 hists->stats.total_period -= diff;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200241 if (!he->filtered)
Namhyung Kim3186b682014-04-22 13:44:23 +0900242 hists->stats.total_non_filtered_period -= diff;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200243
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900244 return he->stat.period == 0;
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300245}
246
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900247void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300248{
249 struct rb_node *next = rb_first(&hists->entries);
250 struct hist_entry *n;
251
252 while (next) {
253 n = rb_entry(next, struct hist_entry, rb_node);
254 next = rb_next(&n->rb_node);
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300255 /*
256 * We may be annotating this, for instance, so keep it here in
257 * case some it gets new samples, we'll eventually free it when
258 * the user stops browsing and it agains gets fully decayed.
259 */
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200260 if (((zap_user && n->level == '.') ||
261 (zap_kernel && n->level != '.') ||
262 hists__decay_entry(hists, n)) &&
263 !n->used) {
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300264 rb_erase(&n->rb_node, &hists->entries);
265
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900266 if (sort__need_collapse)
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300267 rb_erase(&n->rb_node_in, &hists->entries_collapsed);
268
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300269 --hists->nr_entries;
Namhyung Kim3186b682014-04-22 13:44:23 +0900270 if (!n->filtered)
271 --hists->nr_non_filtered_entries;
272
273 hist_entry__free(n);
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300274 }
275 }
276}
277
John Kacur3d1d07e2009-09-28 15:32:55 +0200278/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300279 * histogram, sorted on item, collects periods
John Kacur3d1d07e2009-09-28 15:32:55 +0200280 */
281
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300282static struct hist_entry *hist_entry__new(struct hist_entry *template)
283{
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900284 size_t callchain_size = 0;
285 struct hist_entry *he;
286
287 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain)
288 callchain_size = sizeof(struct callchain_root);
289
290 he = zalloc(sizeof(*he) + callchain_size);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300291
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200292 if (he != NULL) {
293 *he = *template;
Namhyung Kimc4b35352012-10-04 21:49:42 +0900294
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900295 if (symbol_conf.cumulate_callchain) {
296 he->stat_acc = malloc(sizeof(he->stat));
297 if (he->stat_acc == NULL) {
298 free(he);
299 return NULL;
300 }
301 memcpy(he->stat_acc, &he->stat, sizeof(he->stat));
302 }
303
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200304 if (he->ms.map)
305 he->ms.map->referenced = true;
Stephane Eranian3cf0cb12013-01-14 15:02:45 +0100306
307 if (he->branch_info) {
Namhyung Kim26353a62013-04-01 20:35:17 +0900308 /*
309 * This branch info is (a part of) allocated from
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -0300310 * sample__resolve_bstack() and will be freed after
Namhyung Kim26353a62013-04-01 20:35:17 +0900311 * adding new entries. So we need to save a copy.
312 */
313 he->branch_info = malloc(sizeof(*he->branch_info));
314 if (he->branch_info == NULL) {
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900315 free(he->stat_acc);
Namhyung Kim26353a62013-04-01 20:35:17 +0900316 free(he);
317 return NULL;
318 }
319
320 memcpy(he->branch_info, template->branch_info,
321 sizeof(*he->branch_info));
322
Stephane Eranian3cf0cb12013-01-14 15:02:45 +0100323 if (he->branch_info->from.map)
324 he->branch_info->from.map->referenced = true;
325 if (he->branch_info->to.map)
326 he->branch_info->to.map->referenced = true;
327 }
328
Stephane Eranian98a3b322013-01-24 16:10:35 +0100329 if (he->mem_info) {
330 if (he->mem_info->iaddr.map)
331 he->mem_info->iaddr.map->referenced = true;
332 if (he->mem_info->daddr.map)
333 he->mem_info->daddr.map->referenced = true;
334 }
335
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300336 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200337 callchain_init(he->callchain);
Arnaldo Carvalho de Melob821c732012-10-25 14:42:45 -0200338
339 INIT_LIST_HEAD(&he->pairs.node);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300340 }
341
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200342 return he;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300343}
344
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300345static u8 symbol__parent_filter(const struct symbol *parent)
346{
347 if (symbol_conf.exclude_other && parent == NULL)
348 return 1 << HIST_FILTER__PARENT;
349 return 0;
350}
351
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100352static struct hist_entry *add_hist_entry(struct hists *hists,
Namhyung Kimf1cbf782013-12-18 14:21:11 +0900353 struct hist_entry *entry,
354 struct addr_location *al)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300355{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300356 struct rb_node **p;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300357 struct rb_node *parent = NULL;
358 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -0700359 int64_t cmp;
Namhyung Kimf1cbf782013-12-18 14:21:11 +0900360 u64 period = entry->stat.period;
361 u64 weight = entry->stat.weight;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300362
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300363 p = &hists->entries_in->rb_node;
364
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300365 while (*p != NULL) {
366 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300367 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300368
Namhyung Kim9afcf932012-12-10 17:29:54 +0900369 /*
370 * Make sure that it receives arguments in a same order as
371 * hist_entry__collapse() so that we can use an appropriate
372 * function when searching an entry regardless which sort
373 * keys were used.
374 */
375 cmp = hist_entry__cmp(he, entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300376
377 if (!cmp) {
Andi Kleen05484292013-01-24 16:10:29 +0100378 he_stat__add_period(&he->stat, period, weight);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900379 if (symbol_conf.cumulate_callchain)
380 he_stat__add_period(he->stat_acc, period, weight);
David Miller63fa4712012-03-27 03:14:18 -0400381
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900382 /*
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -0300383 * This mem info was allocated from sample__resolve_mem
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900384 * and will not be used anymore.
385 */
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300386 zfree(&entry->mem_info);
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900387
David Miller63fa4712012-03-27 03:14:18 -0400388 /* If the map of an existing hist_entry has
389 * become out-of-date due to an exec() or
390 * similar, update it. Otherwise we will
391 * mis-adjust symbol addresses when computing
392 * the history counter to increment.
393 */
394 if (he->ms.map != entry->ms.map) {
395 he->ms.map = entry->ms.map;
396 if (he->ms.map)
397 he->ms.map->referenced = true;
398 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300399 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300400 }
401
402 if (cmp < 0)
403 p = &(*p)->rb_left;
404 else
405 p = &(*p)->rb_right;
406 }
407
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100408 he = hist_entry__new(entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300409 if (!he)
Namhyung Kim27a0dcb2013-05-14 11:09:02 +0900410 return NULL;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300411
412 rb_link_node(&he->rb_node_in, parent, p);
413 rb_insert_color(&he->rb_node_in, hists->entries_in);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300414out:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900415 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900416 if (symbol_conf.cumulate_callchain)
417 he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300418 return he;
419}
420
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300421struct hist_entry *__hists__add_entry(struct hists *hists,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100422 struct addr_location *al,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900423 struct symbol *sym_parent,
424 struct branch_info *bi,
425 struct mem_info *mi,
426 u64 period, u64 weight, u64 transaction)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100427{
428 struct hist_entry entry = {
429 .thread = al->thread,
Namhyung Kim4dfced32013-09-13 16:28:57 +0900430 .comm = thread__comm(al->thread),
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100431 .ms = {
432 .map = al->map,
433 .sym = al->sym,
434 },
435 .cpu = al->cpu,
436 .ip = al->addr,
437 .level = al->level,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900438 .stat = {
Namhyung Kimc4b35352012-10-04 21:49:42 +0900439 .nr_events = 1,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900440 .period = period,
Andi Kleen05484292013-01-24 16:10:29 +0100441 .weight = weight,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900442 },
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100443 .parent = sym_parent,
Namhyung Kim2c86c7c2014-03-17 18:18:54 -0300444 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300445 .hists = hists,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900446 .branch_info = bi,
447 .mem_info = mi,
Andi Kleen475eeab2013-09-20 07:40:43 -0700448 .transaction = transaction,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100449 };
450
Namhyung Kimf1cbf782013-12-18 14:21:11 +0900451 return add_hist_entry(hists, &entry, al);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100452}
453
Namhyung Kim69bcb012013-10-30 09:40:34 +0900454static int
455iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
456 struct addr_location *al __maybe_unused)
457{
458 return 0;
459}
460
461static int
462iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
463 struct addr_location *al __maybe_unused)
464{
465 return 0;
466}
467
468static int
469iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
470{
471 struct perf_sample *sample = iter->sample;
472 struct mem_info *mi;
473
474 mi = sample__resolve_mem(sample, al);
475 if (mi == NULL)
476 return -ENOMEM;
477
478 iter->priv = mi;
479 return 0;
480}
481
482static int
483iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
484{
485 u64 cost;
486 struct mem_info *mi = iter->priv;
487 struct hist_entry *he;
488
489 if (mi == NULL)
490 return -EINVAL;
491
492 cost = iter->sample->weight;
493 if (!cost)
494 cost = 1;
495
496 /*
497 * must pass period=weight in order to get the correct
498 * sorting from hists__collapse_resort() which is solely
499 * based on periods. We want sorting be done on nr_events * weight
500 * and this is indirectly achieved by passing period=weight here
501 * and the he_stat__add_period() function.
502 */
503 he = __hists__add_entry(&iter->evsel->hists, al, iter->parent, NULL, mi,
504 cost, cost, 0);
505 if (!he)
506 return -ENOMEM;
507
508 iter->he = he;
509 return 0;
510}
511
512static int
513iter_finish_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
514{
515 struct perf_evsel *evsel = iter->evsel;
516 struct hist_entry *he = iter->he;
517 struct mem_info *mx;
518 int err = -EINVAL;
519
520 if (he == NULL)
521 goto out;
522
523 if (ui__has_annotation()) {
524 err = hist_entry__inc_addr_samples(he, evsel->idx, al->addr);
525 if (err)
526 goto out;
527
528 mx = he->mem_info;
529 err = addr_map_symbol__inc_samples(&mx->daddr, evsel->idx);
530 if (err)
531 goto out;
532 }
533
534 hists__inc_nr_samples(&evsel->hists, he->filtered);
535
536 err = hist_entry__append_callchain(he, iter->sample);
537
538out:
539 /*
540 * We don't need to free iter->priv (mem_info) here since
541 * the mem info was either already freed in add_hist_entry() or
542 * passed to a new hist entry by hist_entry__new().
543 */
544 iter->priv = NULL;
545
546 iter->he = NULL;
547 return err;
548}
549
550static int
551iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
552{
553 struct branch_info *bi;
554 struct perf_sample *sample = iter->sample;
555
556 bi = sample__resolve_bstack(sample, al);
557 if (!bi)
558 return -ENOMEM;
559
560 iter->curr = 0;
561 iter->total = sample->branch_stack->nr;
562
563 iter->priv = bi;
564 return 0;
565}
566
567static int
568iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused,
569 struct addr_location *al __maybe_unused)
570{
571 return 0;
572}
573
574static int
575iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
576{
577 struct branch_info *bi = iter->priv;
578 int i = iter->curr;
579
580 if (bi == NULL)
581 return 0;
582
583 if (iter->curr >= iter->total)
584 return 0;
585
586 al->map = bi[i].to.map;
587 al->sym = bi[i].to.sym;
588 al->addr = bi[i].to.addr;
589 return 1;
590}
591
592static int
593iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
594{
595 struct branch_info *bi, *bx;
596 struct perf_evsel *evsel = iter->evsel;
597 struct hist_entry *he = NULL;
598 int i = iter->curr;
599 int err = 0;
600
601 bi = iter->priv;
602
603 if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym))
604 goto out;
605
606 /*
607 * The report shows the percentage of total branches captured
608 * and not events sampled. Thus we use a pseudo period of 1.
609 */
610 he = __hists__add_entry(&evsel->hists, al, iter->parent, &bi[i], NULL,
611 1, 1, 0);
612 if (he == NULL)
613 return -ENOMEM;
614
615 if (ui__has_annotation()) {
616 bx = he->branch_info;
617 err = addr_map_symbol__inc_samples(&bx->from, evsel->idx);
618 if (err)
619 goto out;
620
621 err = addr_map_symbol__inc_samples(&bx->to, evsel->idx);
622 if (err)
623 goto out;
624 }
625
626 hists__inc_nr_samples(&evsel->hists, he->filtered);
627
628out:
629 iter->he = he;
630 iter->curr++;
631 return err;
632}
633
634static int
635iter_finish_branch_entry(struct hist_entry_iter *iter,
636 struct addr_location *al __maybe_unused)
637{
638 zfree(&iter->priv);
639 iter->he = NULL;
640
641 return iter->curr >= iter->total ? 0 : -1;
642}
643
644static int
645iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
646 struct addr_location *al __maybe_unused)
647{
648 return 0;
649}
650
651static int
652iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
653{
654 struct perf_evsel *evsel = iter->evsel;
655 struct perf_sample *sample = iter->sample;
656 struct hist_entry *he;
657
658 he = __hists__add_entry(&evsel->hists, al, iter->parent, NULL, NULL,
659 sample->period, sample->weight,
660 sample->transaction);
661 if (he == NULL)
662 return -ENOMEM;
663
664 iter->he = he;
665 return 0;
666}
667
668static int
669iter_finish_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
670{
671 int err;
672 struct hist_entry *he = iter->he;
673 struct perf_evsel *evsel = iter->evsel;
674 struct perf_sample *sample = iter->sample;
675
676 if (he == NULL)
677 return 0;
678
679 iter->he = NULL;
680
681 if (ui__has_annotation()) {
682 err = hist_entry__inc_addr_samples(he, evsel->idx, al->addr);
683 if (err)
684 return err;
685 }
686
687 hists__inc_nr_samples(&evsel->hists, he->filtered);
688
689 return hist_entry__append_callchain(he, sample);
690}
691
692const struct hist_iter_ops hist_iter_mem = {
693 .prepare_entry = iter_prepare_mem_entry,
694 .add_single_entry = iter_add_single_mem_entry,
695 .next_entry = iter_next_nop_entry,
696 .add_next_entry = iter_add_next_nop_entry,
697 .finish_entry = iter_finish_mem_entry,
698};
699
700const struct hist_iter_ops hist_iter_branch = {
701 .prepare_entry = iter_prepare_branch_entry,
702 .add_single_entry = iter_add_single_branch_entry,
703 .next_entry = iter_next_branch_entry,
704 .add_next_entry = iter_add_next_branch_entry,
705 .finish_entry = iter_finish_branch_entry,
706};
707
708const struct hist_iter_ops hist_iter_normal = {
709 .prepare_entry = iter_prepare_normal_entry,
710 .add_single_entry = iter_add_single_normal_entry,
711 .next_entry = iter_next_nop_entry,
712 .add_next_entry = iter_add_next_nop_entry,
713 .finish_entry = iter_finish_normal_entry,
714};
715
716int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
717 struct perf_evsel *evsel, struct perf_sample *sample,
718 int max_stack_depth)
719{
720 int err, err2;
721
722 err = sample__resolve_callchain(sample, &iter->parent, evsel, al,
723 max_stack_depth);
724 if (err)
725 return err;
726
727 iter->evsel = evsel;
728 iter->sample = sample;
729
730 err = iter->ops->prepare_entry(iter, al);
731 if (err)
732 goto out;
733
734 err = iter->ops->add_single_entry(iter, al);
735 if (err)
736 goto out;
737
738 while (iter->ops->next_entry(iter, al)) {
739 err = iter->ops->add_next_entry(iter, al);
740 if (err)
741 break;
742 }
743
744out:
745 err2 = iter->ops->finish_entry(iter, al);
746 if (!err)
747 err = err2;
748
749 return err;
750}
751
John Kacur3d1d07e2009-09-28 15:32:55 +0200752int64_t
753hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
754{
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900755 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +0200756 int64_t cmp = 0;
757
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900758 perf_hpp__for_each_sort_list(fmt) {
Namhyung Kime67d49a2014-03-18 13:00:59 +0900759 if (perf_hpp__should_skip(fmt))
760 continue;
761
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900762 cmp = fmt->cmp(left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200763 if (cmp)
764 break;
765 }
766
767 return cmp;
768}
769
770int64_t
771hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
772{
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900773 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +0200774 int64_t cmp = 0;
775
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900776 perf_hpp__for_each_sort_list(fmt) {
Namhyung Kime67d49a2014-03-18 13:00:59 +0900777 if (perf_hpp__should_skip(fmt))
778 continue;
779
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900780 cmp = fmt->collapse(left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200781 if (cmp)
782 break;
783 }
784
785 return cmp;
786}
787
788void hist_entry__free(struct hist_entry *he)
789{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300790 zfree(&he->branch_info);
791 zfree(&he->mem_info);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900792 zfree(&he->stat_acc);
Namhyung Kimf048d542013-09-11 14:09:28 +0900793 free_srcline(he->srcline);
John Kacur3d1d07e2009-09-28 15:32:55 +0200794 free(he);
795}
796
797/*
798 * collapse the histogram
799 */
800
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300801static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100802 struct rb_root *root,
803 struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +0200804{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200805 struct rb_node **p = &root->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200806 struct rb_node *parent = NULL;
807 struct hist_entry *iter;
808 int64_t cmp;
809
810 while (*p != NULL) {
811 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300812 iter = rb_entry(parent, struct hist_entry, rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +0200813
814 cmp = hist_entry__collapse(iter, he);
815
816 if (!cmp) {
Namhyung Kim139c0812012-10-04 21:49:43 +0900817 he_stat__add_stat(&iter->stat, &he->stat);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900818 if (symbol_conf.cumulate_callchain)
819 he_stat__add_stat(iter->stat_acc, he->stat_acc);
Namhyung Kim9ec60972012-09-26 16:47:28 +0900820
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100821 if (symbol_conf.use_callchain) {
Namhyung Kim47260642012-05-31 14:43:26 +0900822 callchain_cursor_reset(&callchain_cursor);
823 callchain_merge(&callchain_cursor,
824 iter->callchain,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100825 he->callchain);
826 }
John Kacur3d1d07e2009-09-28 15:32:55 +0200827 hist_entry__free(he);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300828 return false;
John Kacur3d1d07e2009-09-28 15:32:55 +0200829 }
830
831 if (cmp < 0)
832 p = &(*p)->rb_left;
833 else
834 p = &(*p)->rb_right;
835 }
836
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300837 rb_link_node(&he->rb_node_in, parent, p);
838 rb_insert_color(&he->rb_node_in, root);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300839 return true;
John Kacur3d1d07e2009-09-28 15:32:55 +0200840}
841
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300842static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
843{
844 struct rb_root *root;
845
846 pthread_mutex_lock(&hists->lock);
847
848 root = hists->entries_in;
849 if (++hists->entries_in > &hists->entries_in_array[1])
850 hists->entries_in = &hists->entries_in_array[0];
851
852 pthread_mutex_unlock(&hists->lock);
853
854 return root;
855}
856
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200857static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
858{
859 hists__filter_entry_by_dso(hists, he);
860 hists__filter_entry_by_thread(hists, he);
Namhyung Kime94d53e2012-03-16 17:50:51 +0900861 hists__filter_entry_by_symbol(hists, he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200862}
863
Namhyung Kimc1fb5652013-10-11 14:15:38 +0900864void hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300865{
866 struct rb_root *root;
867 struct rb_node *next;
868 struct hist_entry *n;
869
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900870 if (!sort__need_collapse)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300871 return;
872
873 root = hists__get_rotate_entries_in(hists);
874 next = rb_first(root);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300875
876 while (next) {
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -0300877 if (session_done())
878 break;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300879 n = rb_entry(next, struct hist_entry, rb_node_in);
880 next = rb_next(&n->rb_node_in);
881
882 rb_erase(&n->rb_node_in, root);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200883 if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
884 /*
885 * If it wasn't combined with one of the entries already
886 * collapsed, we need to apply the filters that may have
887 * been set by, say, the hist_browser.
888 */
889 hists__apply_filters(hists, n);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200890 }
Namhyung Kimc1fb5652013-10-11 14:15:38 +0900891 if (prog)
892 ui_progress__update(prog, 1);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300893 }
894}
895
Namhyung Kim043ca3892014-03-03 14:18:00 +0900896static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
Namhyung Kim29d720e2013-01-22 18:09:33 +0900897{
Namhyung Kim043ca3892014-03-03 14:18:00 +0900898 struct perf_hpp_fmt *fmt;
899 int64_t cmp = 0;
Namhyung Kim29d720e2013-01-22 18:09:33 +0900900
Namhyung Kim26d8b332014-03-03 16:16:20 +0900901 perf_hpp__for_each_sort_list(fmt) {
Namhyung Kime67d49a2014-03-18 13:00:59 +0900902 if (perf_hpp__should_skip(fmt))
903 continue;
904
Namhyung Kim043ca3892014-03-03 14:18:00 +0900905 cmp = fmt->sort(a, b);
906 if (cmp)
Namhyung Kim29d720e2013-01-22 18:09:33 +0900907 break;
908 }
909
Namhyung Kim043ca3892014-03-03 14:18:00 +0900910 return cmp;
Namhyung Kim29d720e2013-01-22 18:09:33 +0900911}
912
Namhyung Kim9283ba92014-04-24 16:37:26 +0900913static void hists__reset_filter_stats(struct hists *hists)
914{
915 hists->nr_non_filtered_entries = 0;
916 hists->stats.total_non_filtered_period = 0;
917}
918
919void hists__reset_stats(struct hists *hists)
920{
921 hists->nr_entries = 0;
922 hists->stats.total_period = 0;
923
924 hists__reset_filter_stats(hists);
925}
926
927static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
928{
929 hists->nr_non_filtered_entries++;
930 hists->stats.total_non_filtered_period += h->stat.period;
931}
932
933void hists__inc_stats(struct hists *hists, struct hist_entry *h)
934{
935 if (!h->filtered)
936 hists__inc_filter_stats(hists, h);
937
938 hists->nr_entries++;
939 hists->stats.total_period += h->stat.period;
940}
941
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300942static void __hists__insert_output_entry(struct rb_root *entries,
943 struct hist_entry *he,
944 u64 min_callchain_hits)
John Kacur3d1d07e2009-09-28 15:32:55 +0200945{
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300946 struct rb_node **p = &entries->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200947 struct rb_node *parent = NULL;
948 struct hist_entry *iter;
949
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200950 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melob9fb9302010-04-02 09:50:42 -0300951 callchain_param.sort(&he->sorted_chain, he->callchain,
John Kacur3d1d07e2009-09-28 15:32:55 +0200952 min_callchain_hits, &callchain_param);
953
954 while (*p != NULL) {
955 parent = *p;
956 iter = rb_entry(parent, struct hist_entry, rb_node);
957
Namhyung Kim043ca3892014-03-03 14:18:00 +0900958 if (hist_entry__sort(he, iter) > 0)
John Kacur3d1d07e2009-09-28 15:32:55 +0200959 p = &(*p)->rb_left;
960 else
961 p = &(*p)->rb_right;
962 }
963
964 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300965 rb_insert_color(&he->rb_node, entries);
John Kacur3d1d07e2009-09-28 15:32:55 +0200966}
967
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900968void hists__output_resort(struct hists *hists)
John Kacur3d1d07e2009-09-28 15:32:55 +0200969{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300970 struct rb_root *root;
John Kacur3d1d07e2009-09-28 15:32:55 +0200971 struct rb_node *next;
972 struct hist_entry *n;
John Kacur3d1d07e2009-09-28 15:32:55 +0200973 u64 min_callchain_hits;
974
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300975 min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
John Kacur3d1d07e2009-09-28 15:32:55 +0200976
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900977 if (sort__need_collapse)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300978 root = &hists->entries_collapsed;
979 else
980 root = hists->entries_in;
981
982 next = rb_first(root);
983 hists->entries = RB_ROOT;
John Kacur3d1d07e2009-09-28 15:32:55 +0200984
Namhyung Kim9283ba92014-04-24 16:37:26 +0900985 hists__reset_stats(hists);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300986 hists__reset_col_len(hists);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300987
John Kacur3d1d07e2009-09-28 15:32:55 +0200988 while (next) {
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300989 n = rb_entry(next, struct hist_entry, rb_node_in);
990 next = rb_next(&n->rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +0200991
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300992 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
Namhyung Kim62638352014-04-24 16:21:46 +0900993 hists__inc_stats(hists, n);
Namhyung Kimae993ef2014-04-24 16:25:19 +0900994
995 if (!n->filtered)
996 hists__calc_col_len(hists, n);
John Kacur3d1d07e2009-09-28 15:32:55 +0200997 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300998}
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200999
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001000static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001001 enum hist_filter filter)
1002{
1003 h->filtered &= ~(1 << filter);
1004 if (h->filtered)
1005 return;
1006
Namhyung Kim87e90f42014-04-24 16:44:16 +09001007 /* force fold unfiltered entry for simplicity */
1008 h->ms.unfolded = false;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -03001009 h->row_offset = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001010
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001011 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001012
Namhyung Kim9283ba92014-04-24 16:37:26 +09001013 hists__inc_filter_stats(hists, h);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001014 hists__calc_col_len(hists, h);
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001015}
1016
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001017
1018static bool hists__filter_entry_by_dso(struct hists *hists,
1019 struct hist_entry *he)
1020{
1021 if (hists->dso_filter != NULL &&
1022 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
1023 he->filtered |= (1 << HIST_FILTER__DSO);
1024 return true;
1025 }
1026
1027 return false;
1028}
1029
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001030void hists__filter_by_dso(struct hists *hists)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001031{
1032 struct rb_node *nd;
1033
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001034 hists->stats.nr_non_filtered_samples = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001035
1036 hists__reset_filter_stats(hists);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001037 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001038
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001039 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001040 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1041
1042 if (symbol_conf.exclude_other && !h->parent)
1043 continue;
1044
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001045 if (hists__filter_entry_by_dso(hists, h))
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001046 continue;
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001047
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001048 hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001049 }
1050}
1051
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001052static bool hists__filter_entry_by_thread(struct hists *hists,
1053 struct hist_entry *he)
1054{
1055 if (hists->thread_filter != NULL &&
1056 he->thread != hists->thread_filter) {
1057 he->filtered |= (1 << HIST_FILTER__THREAD);
1058 return true;
1059 }
1060
1061 return false;
1062}
1063
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001064void hists__filter_by_thread(struct hists *hists)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001065{
1066 struct rb_node *nd;
1067
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001068 hists->stats.nr_non_filtered_samples = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001069
1070 hists__reset_filter_stats(hists);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001071 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001072
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001073 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001074 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1075
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001076 if (hists__filter_entry_by_thread(hists, h))
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001077 continue;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001078
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001079 hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001080 }
1081}
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001082
Namhyung Kime94d53e2012-03-16 17:50:51 +09001083static bool hists__filter_entry_by_symbol(struct hists *hists,
1084 struct hist_entry *he)
1085{
1086 if (hists->symbol_filter_str != NULL &&
1087 (!he->ms.sym || strstr(he->ms.sym->name,
1088 hists->symbol_filter_str) == NULL)) {
1089 he->filtered |= (1 << HIST_FILTER__SYMBOL);
1090 return true;
1091 }
1092
1093 return false;
1094}
1095
1096void hists__filter_by_symbol(struct hists *hists)
1097{
1098 struct rb_node *nd;
1099
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001100 hists->stats.nr_non_filtered_samples = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001101
1102 hists__reset_filter_stats(hists);
Namhyung Kime94d53e2012-03-16 17:50:51 +09001103 hists__reset_col_len(hists);
1104
1105 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1106 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1107
1108 if (hists__filter_entry_by_symbol(hists, h))
1109 continue;
1110
1111 hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
1112 }
1113}
1114
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001115void events_stats__inc(struct events_stats *stats, u32 type)
1116{
1117 ++stats->nr_events[0];
1118 ++stats->nr_events[type];
1119}
1120
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001121void hists__inc_nr_events(struct hists *hists, u32 type)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001122{
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001123 events_stats__inc(&hists->stats, type);
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001124}
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001125
Namhyung Kim1844dbc2014-05-28 14:12:18 +09001126void hists__inc_nr_samples(struct hists *hists, bool filtered)
1127{
1128 events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
1129 if (!filtered)
1130 hists->stats.nr_non_filtered_samples++;
1131}
1132
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001133static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
1134 struct hist_entry *pair)
1135{
Namhyung Kimce74f602012-12-10 17:29:55 +09001136 struct rb_root *root;
1137 struct rb_node **p;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001138 struct rb_node *parent = NULL;
1139 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -07001140 int64_t cmp;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001141
Namhyung Kimce74f602012-12-10 17:29:55 +09001142 if (sort__need_collapse)
1143 root = &hists->entries_collapsed;
1144 else
1145 root = hists->entries_in;
1146
1147 p = &root->rb_node;
1148
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001149 while (*p != NULL) {
1150 parent = *p;
Namhyung Kimce74f602012-12-10 17:29:55 +09001151 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001152
Namhyung Kimce74f602012-12-10 17:29:55 +09001153 cmp = hist_entry__collapse(he, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001154
1155 if (!cmp)
1156 goto out;
1157
1158 if (cmp < 0)
1159 p = &(*p)->rb_left;
1160 else
1161 p = &(*p)->rb_right;
1162 }
1163
1164 he = hist_entry__new(pair);
1165 if (he) {
Arnaldo Carvalho de Melo30193d72012-11-12 13:20:03 -03001166 memset(&he->stat, 0, sizeof(he->stat));
1167 he->hists = hists;
Namhyung Kimce74f602012-12-10 17:29:55 +09001168 rb_link_node(&he->rb_node_in, parent, p);
1169 rb_insert_color(&he->rb_node_in, root);
Namhyung Kim62638352014-04-24 16:21:46 +09001170 hists__inc_stats(hists, he);
Jiri Olsae0af43d2012-12-01 21:18:20 +01001171 he->dummy = true;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001172 }
1173out:
1174 return he;
1175}
1176
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001177static struct hist_entry *hists__find_entry(struct hists *hists,
1178 struct hist_entry *he)
1179{
Namhyung Kimce74f602012-12-10 17:29:55 +09001180 struct rb_node *n;
1181
1182 if (sort__need_collapse)
1183 n = hists->entries_collapsed.rb_node;
1184 else
1185 n = hists->entries_in->rb_node;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001186
1187 while (n) {
Namhyung Kimce74f602012-12-10 17:29:55 +09001188 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
1189 int64_t cmp = hist_entry__collapse(iter, he);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001190
1191 if (cmp < 0)
1192 n = n->rb_left;
1193 else if (cmp > 0)
1194 n = n->rb_right;
1195 else
1196 return iter;
1197 }
1198
1199 return NULL;
1200}
1201
1202/*
1203 * Look for pairs to link to the leader buckets (hist_entries):
1204 */
1205void hists__match(struct hists *leader, struct hists *other)
1206{
Namhyung Kimce74f602012-12-10 17:29:55 +09001207 struct rb_root *root;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001208 struct rb_node *nd;
1209 struct hist_entry *pos, *pair;
1210
Namhyung Kimce74f602012-12-10 17:29:55 +09001211 if (sort__need_collapse)
1212 root = &leader->entries_collapsed;
1213 else
1214 root = leader->entries_in;
1215
1216 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
1217 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001218 pair = hists__find_entry(other, pos);
1219
1220 if (pair)
Namhyung Kim5fa90412012-11-29 15:38:34 +09001221 hist_entry__add_pair(pair, pos);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001222 }
1223}
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001224
1225/*
1226 * Look for entries in the other hists that are not present in the leader, if
1227 * we find them, just add a dummy entry on the leader hists, with period=0,
1228 * nr_events=0, to serve as the list header.
1229 */
1230int hists__link(struct hists *leader, struct hists *other)
1231{
Namhyung Kimce74f602012-12-10 17:29:55 +09001232 struct rb_root *root;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001233 struct rb_node *nd;
1234 struct hist_entry *pos, *pair;
1235
Namhyung Kimce74f602012-12-10 17:29:55 +09001236 if (sort__need_collapse)
1237 root = &other->entries_collapsed;
1238 else
1239 root = other->entries_in;
1240
1241 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
1242 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001243
1244 if (!hist_entry__has_pairs(pos)) {
1245 pair = hists__add_dummy_entry(leader, pos);
1246 if (pair == NULL)
1247 return -1;
Namhyung Kim5fa90412012-11-29 15:38:34 +09001248 hist_entry__add_pair(pos, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001249 }
1250 }
1251
1252 return 0;
1253}
Namhyung Kimf2148332014-01-14 11:52:48 +09001254
1255u64 hists__total_period(struct hists *hists)
1256{
1257 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
1258 hists->stats.total_period;
1259}
Namhyung Kim33db4562014-02-07 12:06:07 +09001260
1261int parse_filter_percentage(const struct option *opt __maybe_unused,
1262 const char *arg, int unset __maybe_unused)
1263{
1264 if (!strcmp(arg, "relative"))
1265 symbol_conf.filter_relative = true;
1266 else if (!strcmp(arg, "absolute"))
1267 symbol_conf.filter_relative = false;
1268 else
1269 return -1;
1270
1271 return 0;
1272}
Namhyung Kim0b93da12014-01-14 12:02:15 +09001273
1274int perf_hist_config(const char *var, const char *value)
1275{
1276 if (!strcmp(var, "hist.percentage"))
1277 return parse_filter_percentage(NULL, value, 0);
1278
1279 return 0;
1280}