blob: 040f34c79a53e8707debbb71798b504e5115a58c [file] [log] [blame]
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02001#include "annotate.h"
Frederic Weisbecker8a0ecfb2010-05-13 19:47:16 +02002#include "util.h"
Frederic Weisbecker598357e2010-05-21 12:48:39 +02003#include "build-id.h"
John Kacur3d1d07e2009-09-28 15:32:55 +02004#include "hist.h"
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -02005#include "session.h"
6#include "sort.h"
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -02007#include <math.h>
John Kacur3d1d07e2009-09-28 15:32:55 +02008
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02009static bool hists__filter_entry_by_dso(struct hists *hists,
10 struct hist_entry *he);
11static bool hists__filter_entry_by_thread(struct hists *hists,
12 struct hist_entry *he);
Namhyung Kime94d53e2012-03-16 17:50:51 +090013static bool hists__filter_entry_by_symbol(struct hists *hists,
14 struct hist_entry *he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -020015
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -030016enum hist_filter {
17 HIST_FILTER__DSO,
18 HIST_FILTER__THREAD,
19 HIST_FILTER__PARENT,
Namhyung Kime94d53e2012-03-16 17:50:51 +090020 HIST_FILTER__SYMBOL,
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -030021};
22
John Kacur3d1d07e2009-09-28 15:32:55 +020023struct callchain_param callchain_param = {
24 .mode = CHAIN_GRAPH_REL,
Sam Liaod797fdc2011-06-07 23:49:46 +080025 .min_percent = 0.5,
26 .order = ORDER_CALLEE
John Kacur3d1d07e2009-09-28 15:32:55 +020027};
28
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030029u16 hists__col_len(struct hists *hists, enum hist_column col)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030030{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030031 return hists->col_len[col];
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030032}
33
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030034void hists__set_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 hists->col_len[col] = len;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030037}
38
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030039bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030040{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030041 if (len > hists__col_len(hists, col)) {
42 hists__set_col_len(hists, col, len);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030043 return true;
44 }
45 return false;
46}
47
Namhyung Kim7ccf4f92012-08-20 13:52:05 +090048void hists__reset_col_len(struct hists *hists)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030049{
50 enum hist_column col;
51
52 for (col = 0; col < HISTC_NR_COLS; ++col)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030053 hists__set_col_len(hists, col, 0);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030054}
55
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010056static void hists__set_unres_dso_col_len(struct hists *hists, int dso)
57{
58 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
59
60 if (hists__col_len(hists, dso) < unresolved_col_width &&
61 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
62 !symbol_conf.dso_list)
63 hists__set_col_len(hists, dso, unresolved_col_width);
64}
65
Namhyung Kim7ccf4f92012-08-20 13:52:05 +090066void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030067{
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010068 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030069 u16 len;
70
71 if (h->ms.sym)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010072 hists__new_col_len(hists, HISTC_SYMBOL, h->ms.sym->namelen + 4);
73 else
74 hists__set_unres_dso_col_len(hists, HISTC_DSO);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030075
76 len = thread__comm_len(h->thread);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030077 if (hists__new_col_len(hists, HISTC_COMM, len))
78 hists__set_col_len(hists, HISTC_THREAD, len + 6);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030079
80 if (h->ms.map) {
81 len = dso__name_len(h->ms.map->dso);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030082 hists__new_col_len(hists, HISTC_DSO, len);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030083 }
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010084
85 if (h->branch_info) {
86 int symlen;
87 /*
88 * +4 accounts for '[x] ' priv level info
89 * +2 account of 0x prefix on raw addresses
90 */
91 if (h->branch_info->from.sym) {
92 symlen = (int)h->branch_info->from.sym->namelen + 4;
93 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
94
95 symlen = dso__name_len(h->branch_info->from.map->dso);
96 hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
97 } else {
98 symlen = unresolved_col_width + 4 + 2;
99 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
100 hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
101 }
102
103 if (h->branch_info->to.sym) {
104 symlen = (int)h->branch_info->to.sym->namelen + 4;
105 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
106
107 symlen = dso__name_len(h->branch_info->to.map->dso);
108 hists__new_col_len(hists, HISTC_DSO_TO, symlen);
109 } else {
110 symlen = unresolved_col_width + 4 + 2;
111 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
112 hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
113 }
114 }
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300115}
116
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900117void hists__output_recalc_col_len(struct hists *hists, int max_rows)
118{
119 struct rb_node *next = rb_first(&hists->entries);
120 struct hist_entry *n;
121 int row = 0;
122
123 hists__reset_col_len(hists);
124
125 while (next && row++ < max_rows) {
126 n = rb_entry(next, struct hist_entry, rb_node);
127 if (!n->filtered)
128 hists__calc_col_len(hists, n);
129 next = rb_next(&n->rb_node);
130 }
131}
132
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200133static void hist_entry__add_cpumode_period(struct hist_entry *he,
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300134 unsigned int cpumode, u64 period)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800135{
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300136 switch (cpumode) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800137 case PERF_RECORD_MISC_KERNEL:
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200138 he->period_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800139 break;
140 case PERF_RECORD_MISC_USER:
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200141 he->period_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800142 break;
143 case PERF_RECORD_MISC_GUEST_KERNEL:
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200144 he->period_guest_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800145 break;
146 case PERF_RECORD_MISC_GUEST_USER:
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200147 he->period_guest_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800148 break;
149 default:
150 break;
151 }
152}
153
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300154static void hist_entry__decay(struct hist_entry *he)
155{
156 he->period = (he->period * 7) / 8;
157 he->nr_events = (he->nr_events * 7) / 8;
158}
159
160static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
161{
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200162 u64 prev_period = he->period;
163
164 if (prev_period == 0)
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300165 return true;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200166
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300167 hist_entry__decay(he);
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200168
169 if (!he->filtered)
170 hists->stats.total_period -= prev_period - he->period;
171
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300172 return he->period == 0;
173}
174
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200175static void __hists__decay_entries(struct hists *hists, bool zap_user,
176 bool zap_kernel, bool threaded)
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300177{
178 struct rb_node *next = rb_first(&hists->entries);
179 struct hist_entry *n;
180
181 while (next) {
182 n = rb_entry(next, struct hist_entry, rb_node);
183 next = rb_next(&n->rb_node);
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300184 /*
185 * We may be annotating this, for instance, so keep it here in
186 * case some it gets new samples, we'll eventually free it when
187 * the user stops browsing and it agains gets fully decayed.
188 */
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200189 if (((zap_user && n->level == '.') ||
190 (zap_kernel && n->level != '.') ||
191 hists__decay_entry(hists, n)) &&
192 !n->used) {
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300193 rb_erase(&n->rb_node, &hists->entries);
194
Arnaldo Carvalho de Meloe345fa12011-10-13 09:06:54 -0300195 if (sort__need_collapse || threaded)
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300196 rb_erase(&n->rb_node_in, &hists->entries_collapsed);
197
198 hist_entry__free(n);
199 --hists->nr_entries;
200 }
201 }
202}
203
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200204void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
Arnaldo Carvalho de Meloe345fa12011-10-13 09:06:54 -0300205{
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200206 return __hists__decay_entries(hists, zap_user, zap_kernel, false);
Arnaldo Carvalho de Meloe345fa12011-10-13 09:06:54 -0300207}
208
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200209void hists__decay_entries_threaded(struct hists *hists,
210 bool zap_user, bool zap_kernel)
Arnaldo Carvalho de Meloe345fa12011-10-13 09:06:54 -0300211{
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200212 return __hists__decay_entries(hists, zap_user, zap_kernel, true);
Arnaldo Carvalho de Meloe345fa12011-10-13 09:06:54 -0300213}
214
John Kacur3d1d07e2009-09-28 15:32:55 +0200215/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300216 * histogram, sorted on item, collects periods
John Kacur3d1d07e2009-09-28 15:32:55 +0200217 */
218
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300219static struct hist_entry *hist_entry__new(struct hist_entry *template)
220{
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +0200221 size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0;
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200222 struct hist_entry *he = malloc(sizeof(*he) + callchain_size);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300223
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200224 if (he != NULL) {
225 *he = *template;
226 he->nr_events = 1;
227 if (he->ms.map)
228 he->ms.map->referenced = true;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300229 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200230 callchain_init(he->callchain);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300231 }
232
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200233 return he;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300234}
235
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300236static void hists__inc_nr_entries(struct hists *hists, struct hist_entry *h)
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300237{
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300238 if (!h->filtered) {
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300239 hists__calc_col_len(hists, h);
240 ++hists->nr_entries;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300241 hists->stats.total_period += h->period;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300242 }
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300243}
244
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300245static u8 symbol__parent_filter(const struct symbol *parent)
246{
247 if (symbol_conf.exclude_other && parent == NULL)
248 return 1 << HIST_FILTER__PARENT;
249 return 0;
250}
251
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100252static struct hist_entry *add_hist_entry(struct hists *hists,
253 struct hist_entry *entry,
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300254 struct addr_location *al,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100255 u64 period)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300256{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300257 struct rb_node **p;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300258 struct rb_node *parent = NULL;
259 struct hist_entry *he;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300260 int cmp;
261
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300262 pthread_mutex_lock(&hists->lock);
263
264 p = &hists->entries_in->rb_node;
265
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300266 while (*p != NULL) {
267 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300268 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300269
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100270 cmp = hist_entry__cmp(entry, he);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300271
272 if (!cmp) {
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300273 he->period += period;
274 ++he->nr_events;
David Miller63fa4712012-03-27 03:14:18 -0400275
276 /* If the map of an existing hist_entry has
277 * become out-of-date due to an exec() or
278 * similar, update it. Otherwise we will
279 * mis-adjust symbol addresses when computing
280 * the history counter to increment.
281 */
282 if (he->ms.map != entry->ms.map) {
283 he->ms.map = entry->ms.map;
284 if (he->ms.map)
285 he->ms.map->referenced = true;
286 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300287 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300288 }
289
290 if (cmp < 0)
291 p = &(*p)->rb_left;
292 else
293 p = &(*p)->rb_right;
294 }
295
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100296 he = hist_entry__new(entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300297 if (!he)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300298 goto out_unlock;
299
300 rb_link_node(&he->rb_node_in, parent, p);
301 rb_insert_color(&he->rb_node_in, hists->entries_in);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300302out:
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300303 hist_entry__add_cpumode_period(he, al->cpumode, period);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300304out_unlock:
305 pthread_mutex_unlock(&hists->lock);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300306 return he;
307}
308
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100309struct hist_entry *__hists__add_branch_entry(struct hists *self,
310 struct addr_location *al,
311 struct symbol *sym_parent,
312 struct branch_info *bi,
313 u64 period)
314{
315 struct hist_entry entry = {
316 .thread = al->thread,
317 .ms = {
318 .map = bi->to.map,
319 .sym = bi->to.sym,
320 },
321 .cpu = al->cpu,
322 .ip = bi->to.addr,
323 .level = al->level,
324 .period = period,
325 .parent = sym_parent,
326 .filtered = symbol__parent_filter(sym_parent),
327 .branch_info = bi,
Jiri Olsaae359f12012-10-04 21:49:35 +0900328 .hists = self,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100329 };
330
331 return add_hist_entry(self, &entry, al, period);
332}
333
334struct hist_entry *__hists__add_entry(struct hists *self,
335 struct addr_location *al,
336 struct symbol *sym_parent, u64 period)
337{
338 struct hist_entry entry = {
339 .thread = al->thread,
340 .ms = {
341 .map = al->map,
342 .sym = al->sym,
343 },
344 .cpu = al->cpu,
345 .ip = al->addr,
346 .level = al->level,
347 .period = period,
348 .parent = sym_parent,
349 .filtered = symbol__parent_filter(sym_parent),
Jiri Olsaae359f12012-10-04 21:49:35 +0900350 .hists = self,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100351 };
352
353 return add_hist_entry(self, &entry, al, period);
354}
355
John Kacur3d1d07e2009-09-28 15:32:55 +0200356int64_t
357hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
358{
359 struct sort_entry *se;
360 int64_t cmp = 0;
361
362 list_for_each_entry(se, &hist_entry__sort_list, list) {
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200363 cmp = se->se_cmp(left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200364 if (cmp)
365 break;
366 }
367
368 return cmp;
369}
370
371int64_t
372hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
373{
374 struct sort_entry *se;
375 int64_t cmp = 0;
376
377 list_for_each_entry(se, &hist_entry__sort_list, list) {
378 int64_t (*f)(struct hist_entry *, struct hist_entry *);
379
Frederic Weisbeckerfcd14982010-04-14 19:11:29 +0200380 f = se->se_collapse ?: se->se_cmp;
John Kacur3d1d07e2009-09-28 15:32:55 +0200381
382 cmp = f(left, right);
383 if (cmp)
384 break;
385 }
386
387 return cmp;
388}
389
390void hist_entry__free(struct hist_entry *he)
391{
392 free(he);
393}
394
395/*
396 * collapse the histogram
397 */
398
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300399static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100400 struct rb_root *root,
401 struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +0200402{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200403 struct rb_node **p = &root->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200404 struct rb_node *parent = NULL;
405 struct hist_entry *iter;
406 int64_t cmp;
407
408 while (*p != NULL) {
409 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300410 iter = rb_entry(parent, struct hist_entry, rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +0200411
412 cmp = hist_entry__collapse(iter, he);
413
414 if (!cmp) {
Namhyung Kim9ec60972012-09-26 16:47:28 +0900415 iter->period += he->period;
416 iter->period_sys += he->period_sys;
417 iter->period_us += he->period_us;
418 iter->period_guest_sys += he->period_guest_sys;
419 iter->period_guest_us += he->period_guest_us;
420 iter->nr_events += he->nr_events;
421
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100422 if (symbol_conf.use_callchain) {
Namhyung Kim47260642012-05-31 14:43:26 +0900423 callchain_cursor_reset(&callchain_cursor);
424 callchain_merge(&callchain_cursor,
425 iter->callchain,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100426 he->callchain);
427 }
John Kacur3d1d07e2009-09-28 15:32:55 +0200428 hist_entry__free(he);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300429 return false;
John Kacur3d1d07e2009-09-28 15:32:55 +0200430 }
431
432 if (cmp < 0)
433 p = &(*p)->rb_left;
434 else
435 p = &(*p)->rb_right;
436 }
437
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300438 rb_link_node(&he->rb_node_in, parent, p);
439 rb_insert_color(&he->rb_node_in, root);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300440 return true;
John Kacur3d1d07e2009-09-28 15:32:55 +0200441}
442
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300443static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
444{
445 struct rb_root *root;
446
447 pthread_mutex_lock(&hists->lock);
448
449 root = hists->entries_in;
450 if (++hists->entries_in > &hists->entries_in_array[1])
451 hists->entries_in = &hists->entries_in_array[0];
452
453 pthread_mutex_unlock(&hists->lock);
454
455 return root;
456}
457
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200458static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
459{
460 hists__filter_entry_by_dso(hists, he);
461 hists__filter_entry_by_thread(hists, he);
Namhyung Kime94d53e2012-03-16 17:50:51 +0900462 hists__filter_entry_by_symbol(hists, he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200463}
464
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300465static void __hists__collapse_resort(struct hists *hists, bool threaded)
466{
467 struct rb_root *root;
468 struct rb_node *next;
469 struct hist_entry *n;
470
471 if (!sort__need_collapse && !threaded)
472 return;
473
474 root = hists__get_rotate_entries_in(hists);
475 next = rb_first(root);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300476
477 while (next) {
478 n = rb_entry(next, struct hist_entry, rb_node_in);
479 next = rb_next(&n->rb_node_in);
480
481 rb_erase(&n->rb_node_in, root);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200482 if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
483 /*
484 * If it wasn't combined with one of the entries already
485 * collapsed, we need to apply the filters that may have
486 * been set by, say, the hist_browser.
487 */
488 hists__apply_filters(hists, n);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200489 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300490 }
491}
492
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300493void hists__collapse_resort(struct hists *hists)
John Kacur3d1d07e2009-09-28 15:32:55 +0200494{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300495 return __hists__collapse_resort(hists, false);
496}
John Kacur3d1d07e2009-09-28 15:32:55 +0200497
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300498void hists__collapse_resort_threaded(struct hists *hists)
499{
500 return __hists__collapse_resort(hists, true);
John Kacur3d1d07e2009-09-28 15:32:55 +0200501}
502
503/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300504 * reverse the map, sort on period.
John Kacur3d1d07e2009-09-28 15:32:55 +0200505 */
506
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300507static void __hists__insert_output_entry(struct rb_root *entries,
508 struct hist_entry *he,
509 u64 min_callchain_hits)
John Kacur3d1d07e2009-09-28 15:32:55 +0200510{
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300511 struct rb_node **p = &entries->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200512 struct rb_node *parent = NULL;
513 struct hist_entry *iter;
514
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -0200515 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melob9fb9302010-04-02 09:50:42 -0300516 callchain_param.sort(&he->sorted_chain, he->callchain,
John Kacur3d1d07e2009-09-28 15:32:55 +0200517 min_callchain_hits, &callchain_param);
518
519 while (*p != NULL) {
520 parent = *p;
521 iter = rb_entry(parent, struct hist_entry, rb_node);
522
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300523 if (he->period > iter->period)
John Kacur3d1d07e2009-09-28 15:32:55 +0200524 p = &(*p)->rb_left;
525 else
526 p = &(*p)->rb_right;
527 }
528
529 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -0300530 rb_insert_color(&he->rb_node, entries);
John Kacur3d1d07e2009-09-28 15:32:55 +0200531}
532
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300533static void __hists__output_resort(struct hists *hists, bool threaded)
John Kacur3d1d07e2009-09-28 15:32:55 +0200534{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300535 struct rb_root *root;
John Kacur3d1d07e2009-09-28 15:32:55 +0200536 struct rb_node *next;
537 struct hist_entry *n;
John Kacur3d1d07e2009-09-28 15:32:55 +0200538 u64 min_callchain_hits;
539
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300540 min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
John Kacur3d1d07e2009-09-28 15:32:55 +0200541
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300542 if (sort__need_collapse || threaded)
543 root = &hists->entries_collapsed;
544 else
545 root = hists->entries_in;
546
547 next = rb_first(root);
548 hists->entries = RB_ROOT;
John Kacur3d1d07e2009-09-28 15:32:55 +0200549
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300550 hists->nr_entries = 0;
Arnaldo Carvalho de Melo79286312011-10-27 09:19:48 -0200551 hists->stats.total_period = 0;
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300552 hists__reset_col_len(hists);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300553
John Kacur3d1d07e2009-09-28 15:32:55 +0200554 while (next) {
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300555 n = rb_entry(next, struct hist_entry, rb_node_in);
556 next = rb_next(&n->rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +0200557
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300558 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300559 hists__inc_nr_entries(hists, n);
John Kacur3d1d07e2009-09-28 15:32:55 +0200560 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300561}
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200562
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300563void hists__output_resort(struct hists *hists)
564{
565 return __hists__output_resort(hists, false);
566}
567
568void hists__output_resort_threaded(struct hists *hists)
569{
570 return __hists__output_resort(hists, true);
John Kacur3d1d07e2009-09-28 15:32:55 +0200571}
Arnaldo Carvalho de Melo4ecf84d02009-12-16 12:27:09 -0200572
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300573static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300574 enum hist_filter filter)
575{
576 h->filtered &= ~(1 << filter);
577 if (h->filtered)
578 return;
579
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300580 ++hists->nr_entries;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -0300581 if (h->ms.unfolded)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300582 hists->nr_entries += h->nr_rows;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -0300583 h->row_offset = 0;
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300584 hists->stats.total_period += h->period;
585 hists->stats.nr_events[PERF_RECORD_SAMPLE] += h->nr_events;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300586
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300587 hists__calc_col_len(hists, h);
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300588}
589
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200590
591static bool hists__filter_entry_by_dso(struct hists *hists,
592 struct hist_entry *he)
593{
594 if (hists->dso_filter != NULL &&
595 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
596 he->filtered |= (1 << HIST_FILTER__DSO);
597 return true;
598 }
599
600 return false;
601}
602
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200603void hists__filter_by_dso(struct hists *hists)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300604{
605 struct rb_node *nd;
606
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300607 hists->nr_entries = hists->stats.total_period = 0;
608 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
609 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300610
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300611 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300612 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
613
614 if (symbol_conf.exclude_other && !h->parent)
615 continue;
616
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200617 if (hists__filter_entry_by_dso(hists, h))
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300618 continue;
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300619
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300620 hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300621 }
622}
623
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200624static bool hists__filter_entry_by_thread(struct hists *hists,
625 struct hist_entry *he)
626{
627 if (hists->thread_filter != NULL &&
628 he->thread != hists->thread_filter) {
629 he->filtered |= (1 << HIST_FILTER__THREAD);
630 return true;
631 }
632
633 return false;
634}
635
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200636void hists__filter_by_thread(struct hists *hists)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300637{
638 struct rb_node *nd;
639
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300640 hists->nr_entries = hists->stats.total_period = 0;
641 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
642 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300643
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300644 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300645 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
646
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -0200647 if (hists__filter_entry_by_thread(hists, h))
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300648 continue;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -0300649
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300650 hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300651 }
652}
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300653
Namhyung Kime94d53e2012-03-16 17:50:51 +0900654static bool hists__filter_entry_by_symbol(struct hists *hists,
655 struct hist_entry *he)
656{
657 if (hists->symbol_filter_str != NULL &&
658 (!he->ms.sym || strstr(he->ms.sym->name,
659 hists->symbol_filter_str) == NULL)) {
660 he->filtered |= (1 << HIST_FILTER__SYMBOL);
661 return true;
662 }
663
664 return false;
665}
666
667void hists__filter_by_symbol(struct hists *hists)
668{
669 struct rb_node *nd;
670
671 hists->nr_entries = hists->stats.total_period = 0;
672 hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
673 hists__reset_col_len(hists);
674
675 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
676 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
677
678 if (hists__filter_entry_by_symbol(hists, h))
679 continue;
680
681 hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
682 }
683}
684
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200685int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300686{
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200687 return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300688}
689
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200690int hist_entry__annotate(struct hist_entry *he, size_t privsize)
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300691{
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200692 return symbol__annotate(he->ms.sym, he->ms.map, privsize);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300693}
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -0300694
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300695void hists__inc_nr_events(struct hists *hists, u32 type)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -0300696{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300697 ++hists->stats.nr_events[0];
698 ++hists->stats.nr_events[type];
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -0300699}