blob: dbab977f3a44d442c74a093a607ee93f8479f62a [file] [log] [blame]
Frederic Weisbecker8a0ecfb2010-05-13 19:47:16 +02001#include "util.h"
Frederic Weisbecker598357e2010-05-21 12:48:39 +02002#include "build-id.h"
John Kacur3d1d07e2009-09-28 15:32:55 +02003#include "hist.h"
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -02004#include "session.h"
5#include "sort.h"
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03006#include "evlist.h"
Namhyung Kim29d720e2013-01-22 18:09:33 +09007#include "evsel.h"
Namhyung Kim69bcb012013-10-30 09:40:34 +09008#include "annotate.h"
Namhyung Kim740b97f2014-12-22 13:44:10 +09009#include "ui/progress.h"
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -020010#include <math.h>
John Kacur3d1d07e2009-09-28 15:32:55 +020011
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -020012static bool hists__filter_entry_by_dso(struct hists *hists,
13 struct hist_entry *he);
14static bool hists__filter_entry_by_thread(struct hists *hists,
15 struct hist_entry *he);
Namhyung Kime94d53e2012-03-16 17:50:51 +090016static bool hists__filter_entry_by_symbol(struct hists *hists,
17 struct hist_entry *he);
Kan Liang21394d92015-09-04 10:45:44 -040018static bool hists__filter_entry_by_socket(struct hists *hists,
19 struct hist_entry *he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -020020
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030021u16 hists__col_len(struct hists *hists, enum hist_column col)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030022{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030023 return hists->col_len[col];
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030024}
25
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030026void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030027{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030028 hists->col_len[col] = len;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030029}
30
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030031bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030032{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030033 if (len > hists__col_len(hists, col)) {
34 hists__set_col_len(hists, col, len);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030035 return true;
36 }
37 return false;
38}
39
Namhyung Kim7ccf4f92012-08-20 13:52:05 +090040void hists__reset_col_len(struct hists *hists)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030041{
42 enum hist_column col;
43
44 for (col = 0; col < HISTC_NR_COLS; ++col)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030045 hists__set_col_len(hists, col, 0);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030046}
47
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010048static void hists__set_unres_dso_col_len(struct hists *hists, int dso)
49{
50 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
51
52 if (hists__col_len(hists, dso) < unresolved_col_width &&
53 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
54 !symbol_conf.dso_list)
55 hists__set_col_len(hists, dso, unresolved_col_width);
56}
57
Namhyung Kim7ccf4f92012-08-20 13:52:05 +090058void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030059{
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010060 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
Stephane Eranian98a3b322013-01-24 16:10:35 +010061 int symlen;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030062 u16 len;
63
Namhyung Kimded19d52013-04-01 20:35:19 +090064 /*
65 * +4 accounts for '[x] ' priv level info
66 * +2 accounts for 0x prefix on raw addresses
67 * +3 accounts for ' y ' symtab origin info
68 */
69 if (h->ms.sym) {
70 symlen = h->ms.sym->namelen + 4;
71 if (verbose)
72 symlen += BITS_PER_LONG / 4 + 2 + 3;
73 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
74 } else {
Stephane Eranian98a3b322013-01-24 16:10:35 +010075 symlen = unresolved_col_width + 4 + 2;
76 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010077 hists__set_unres_dso_col_len(hists, HISTC_DSO);
Stephane Eranian98a3b322013-01-24 16:10:35 +010078 }
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030079
80 len = thread__comm_len(h->thread);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030081 if (hists__new_col_len(hists, HISTC_COMM, len))
82 hists__set_col_len(hists, HISTC_THREAD, len + 6);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030083
84 if (h->ms.map) {
85 len = dso__name_len(h->ms.map->dso);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030086 hists__new_col_len(hists, HISTC_DSO, len);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030087 }
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010088
Namhyung Kimcb993742012-12-27 18:11:42 +090089 if (h->parent)
90 hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen);
91
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010092 if (h->branch_info) {
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010093 if (h->branch_info->from.sym) {
94 symlen = (int)h->branch_info->from.sym->namelen + 4;
Namhyung Kimded19d52013-04-01 20:35:19 +090095 if (verbose)
96 symlen += BITS_PER_LONG / 4 + 2 + 3;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010097 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
98
99 symlen = dso__name_len(h->branch_info->from.map->dso);
100 hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
101 } else {
102 symlen = unresolved_col_width + 4 + 2;
103 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
104 hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
105 }
106
107 if (h->branch_info->to.sym) {
108 symlen = (int)h->branch_info->to.sym->namelen + 4;
Namhyung Kimded19d52013-04-01 20:35:19 +0900109 if (verbose)
110 symlen += BITS_PER_LONG / 4 + 2 + 3;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100111 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
112
113 symlen = dso__name_len(h->branch_info->to.map->dso);
114 hists__new_col_len(hists, HISTC_DSO_TO, symlen);
115 } else {
116 symlen = unresolved_col_width + 4 + 2;
117 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
118 hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
119 }
120 }
Stephane Eranian98a3b322013-01-24 16:10:35 +0100121
122 if (h->mem_info) {
Stephane Eranian98a3b322013-01-24 16:10:35 +0100123 if (h->mem_info->daddr.sym) {
124 symlen = (int)h->mem_info->daddr.sym->namelen + 4
125 + unresolved_col_width + 2;
126 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
127 symlen);
Don Zickus9b32ba72014-06-01 15:38:29 +0200128 hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
129 symlen + 1);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100130 } else {
131 symlen = unresolved_col_width + 4 + 2;
132 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
133 symlen);
Jiri Olsa08059092016-01-20 12:56:33 +0100134 hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
135 symlen);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100136 }
Jiri Olsab34b3bf2015-10-05 20:06:08 +0200137
138 if (h->mem_info->iaddr.sym) {
139 symlen = (int)h->mem_info->iaddr.sym->namelen + 4
140 + unresolved_col_width + 2;
141 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
142 symlen);
143 } else {
144 symlen = unresolved_col_width + 4 + 2;
145 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
146 symlen);
147 }
148
Stephane Eranian98a3b322013-01-24 16:10:35 +0100149 if (h->mem_info->daddr.map) {
150 symlen = dso__name_len(h->mem_info->daddr.map->dso);
151 hists__new_col_len(hists, HISTC_MEM_DADDR_DSO,
152 symlen);
153 } else {
154 symlen = unresolved_col_width + 4 + 2;
155 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
156 }
157 } else {
158 symlen = unresolved_col_width + 4 + 2;
159 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen);
Jiri Olsab34b3bf2015-10-05 20:06:08 +0200160 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL, symlen);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100161 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
162 }
163
Arnaldo Carvalho de Meloa4978ec2015-09-09 12:14:00 -0300164 hists__new_col_len(hists, HISTC_CPU, 3);
Kan Liang2e7ea3a2015-09-04 10:45:43 -0400165 hists__new_col_len(hists, HISTC_SOCKET, 6);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100166 hists__new_col_len(hists, HISTC_MEM_LOCKED, 6);
167 hists__new_col_len(hists, HISTC_MEM_TLB, 22);
168 hists__new_col_len(hists, HISTC_MEM_SNOOP, 12);
169 hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3);
170 hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12);
171 hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12);
Andi Kleen475eeab2013-09-20 07:40:43 -0700172
Arnaldo Carvalho de Meloe8e6d372015-08-10 16:53:54 -0300173 if (h->srcline)
174 hists__new_col_len(hists, HISTC_SRCLINE, strlen(h->srcline));
175
Andi Kleen31191a82015-08-07 15:54:24 -0700176 if (h->srcfile)
177 hists__new_col_len(hists, HISTC_SRCFILE, strlen(h->srcfile));
178
Andi Kleen475eeab2013-09-20 07:40:43 -0700179 if (h->transaction)
180 hists__new_col_len(hists, HISTC_TRANSACTION,
181 hist_entry__transaction_len());
Namhyung Kim0c0af782016-02-21 23:22:38 +0900182
183 if (h->trace_output)
184 hists__new_col_len(hists, HISTC_TRACE, strlen(h->trace_output));
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300185}
186
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900187void hists__output_recalc_col_len(struct hists *hists, int max_rows)
188{
189 struct rb_node *next = rb_first(&hists->entries);
190 struct hist_entry *n;
191 int row = 0;
192
193 hists__reset_col_len(hists);
194
195 while (next && row++ < max_rows) {
196 n = rb_entry(next, struct hist_entry, rb_node);
197 if (!n->filtered)
198 hists__calc_col_len(hists, n);
199 next = rb_next(&n->rb_node);
200 }
201}
202
Namhyung Kimf39056f2014-01-14 14:25:37 +0900203static void he_stat__add_cpumode_period(struct he_stat *he_stat,
204 unsigned int cpumode, u64 period)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800205{
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300206 switch (cpumode) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800207 case PERF_RECORD_MISC_KERNEL:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900208 he_stat->period_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800209 break;
210 case PERF_RECORD_MISC_USER:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900211 he_stat->period_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800212 break;
213 case PERF_RECORD_MISC_GUEST_KERNEL:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900214 he_stat->period_guest_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800215 break;
216 case PERF_RECORD_MISC_GUEST_USER:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900217 he_stat->period_guest_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800218 break;
219 default:
220 break;
221 }
222}
223
Andi Kleen05484292013-01-24 16:10:29 +0100224static void he_stat__add_period(struct he_stat *he_stat, u64 period,
225 u64 weight)
Namhyung Kim139c0812012-10-04 21:49:43 +0900226{
Stephane Eranian98a3b322013-01-24 16:10:35 +0100227
Namhyung Kim139c0812012-10-04 21:49:43 +0900228 he_stat->period += period;
Andi Kleen05484292013-01-24 16:10:29 +0100229 he_stat->weight += weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900230 he_stat->nr_events += 1;
231}
232
233static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
234{
235 dest->period += src->period;
236 dest->period_sys += src->period_sys;
237 dest->period_us += src->period_us;
238 dest->period_guest_sys += src->period_guest_sys;
239 dest->period_guest_us += src->period_guest_us;
240 dest->nr_events += src->nr_events;
Andi Kleen05484292013-01-24 16:10:29 +0100241 dest->weight += src->weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900242}
243
Namhyung Kimf39056f2014-01-14 14:25:37 +0900244static void he_stat__decay(struct he_stat *he_stat)
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300245{
Namhyung Kimf39056f2014-01-14 14:25:37 +0900246 he_stat->period = (he_stat->period * 7) / 8;
247 he_stat->nr_events = (he_stat->nr_events * 7) / 8;
Andi Kleen05484292013-01-24 16:10:29 +0100248 /* XXX need decay for weight too? */
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300249}
250
251static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
252{
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900253 u64 prev_period = he->stat.period;
Namhyung Kim3186b682014-04-22 13:44:23 +0900254 u64 diff;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200255
256 if (prev_period == 0)
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300257 return true;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200258
Namhyung Kimf39056f2014-01-14 14:25:37 +0900259 he_stat__decay(&he->stat);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900260 if (symbol_conf.cumulate_callchain)
261 he_stat__decay(he->stat_acc);
Namhyung Kim42b276a2016-01-05 12:06:00 +0900262 decay_callchain(he->callchain);
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200263
Namhyung Kim3186b682014-04-22 13:44:23 +0900264 diff = prev_period - he->stat.period;
265
266 hists->stats.total_period -= diff;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200267 if (!he->filtered)
Namhyung Kim3186b682014-04-22 13:44:23 +0900268 hists->stats.total_non_filtered_period -= diff;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200269
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900270 return he->stat.period == 0;
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300271}
272
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300273static void hists__delete_entry(struct hists *hists, struct hist_entry *he)
274{
275 rb_erase(&he->rb_node, &hists->entries);
276
277 if (sort__need_collapse)
278 rb_erase(&he->rb_node_in, &hists->entries_collapsed);
Namhyung Kim61fa0e92015-12-10 16:53:20 +0900279 else
280 rb_erase(&he->rb_node_in, hists->entries_in);
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300281
282 --hists->nr_entries;
283 if (!he->filtered)
284 --hists->nr_non_filtered_entries;
285
286 hist_entry__delete(he);
287}
288
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900289void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300290{
291 struct rb_node *next = rb_first(&hists->entries);
292 struct hist_entry *n;
293
294 while (next) {
295 n = rb_entry(next, struct hist_entry, rb_node);
296 next = rb_next(&n->rb_node);
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200297 if (((zap_user && n->level == '.') ||
298 (zap_kernel && n->level != '.') ||
Arnaldo Carvalho de Melo4c47f4f2015-03-17 17:18:58 -0300299 hists__decay_entry(hists, n))) {
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300300 hists__delete_entry(hists, n);
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300301 }
302 }
303}
304
Namhyung Kim701937b2014-08-12 17:16:05 +0900305void hists__delete_entries(struct hists *hists)
306{
307 struct rb_node *next = rb_first(&hists->entries);
308 struct hist_entry *n;
309
310 while (next) {
311 n = rb_entry(next, struct hist_entry, rb_node);
312 next = rb_next(&n->rb_node);
313
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300314 hists__delete_entry(hists, n);
Namhyung Kim701937b2014-08-12 17:16:05 +0900315 }
316}
317
John Kacur3d1d07e2009-09-28 15:32:55 +0200318/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300319 * histogram, sorted on item, collects periods
John Kacur3d1d07e2009-09-28 15:32:55 +0200320 */
321
Namhyung Kima0b51af2012-09-11 13:34:27 +0900322static struct hist_entry *hist_entry__new(struct hist_entry *template,
323 bool sample_self)
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300324{
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900325 size_t callchain_size = 0;
326 struct hist_entry *he;
327
Namhyung Kim82aa0192014-12-22 13:44:14 +0900328 if (symbol_conf.use_callchain)
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900329 callchain_size = sizeof(struct callchain_root);
330
331 he = zalloc(sizeof(*he) + callchain_size);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300332
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200333 if (he != NULL) {
334 *he = *template;
Namhyung Kimc4b35352012-10-04 21:49:42 +0900335
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900336 if (symbol_conf.cumulate_callchain) {
337 he->stat_acc = malloc(sizeof(he->stat));
338 if (he->stat_acc == NULL) {
339 free(he);
340 return NULL;
341 }
342 memcpy(he->stat_acc, &he->stat, sizeof(he->stat));
Namhyung Kima0b51af2012-09-11 13:34:27 +0900343 if (!sample_self)
344 memset(&he->stat, 0, sizeof(he->stat));
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900345 }
346
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300347 map__get(he->ms.map);
Stephane Eranian3cf0cb12013-01-14 15:02:45 +0100348
349 if (he->branch_info) {
Namhyung Kim26353a62013-04-01 20:35:17 +0900350 /*
351 * This branch info is (a part of) allocated from
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -0300352 * sample__resolve_bstack() and will be freed after
Namhyung Kim26353a62013-04-01 20:35:17 +0900353 * adding new entries. So we need to save a copy.
354 */
355 he->branch_info = malloc(sizeof(*he->branch_info));
356 if (he->branch_info == NULL) {
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300357 map__zput(he->ms.map);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900358 free(he->stat_acc);
Namhyung Kim26353a62013-04-01 20:35:17 +0900359 free(he);
360 return NULL;
361 }
362
363 memcpy(he->branch_info, template->branch_info,
364 sizeof(*he->branch_info));
365
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300366 map__get(he->branch_info->from.map);
367 map__get(he->branch_info->to.map);
Stephane Eranian3cf0cb12013-01-14 15:02:45 +0100368 }
369
Stephane Eranian98a3b322013-01-24 16:10:35 +0100370 if (he->mem_info) {
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300371 map__get(he->mem_info->iaddr.map);
372 map__get(he->mem_info->daddr.map);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100373 }
374
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300375 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200376 callchain_init(he->callchain);
Arnaldo Carvalho de Melob821c732012-10-25 14:42:45 -0200377
Namhyung Kim72392832015-12-24 11:16:17 +0900378 if (he->raw_data) {
379 he->raw_data = memdup(he->raw_data, he->raw_size);
380
381 if (he->raw_data == NULL) {
382 map__put(he->ms.map);
383 if (he->branch_info) {
384 map__put(he->branch_info->from.map);
385 map__put(he->branch_info->to.map);
386 free(he->branch_info);
387 }
388 if (he->mem_info) {
389 map__put(he->mem_info->iaddr.map);
390 map__put(he->mem_info->daddr.map);
391 }
392 free(he->stat_acc);
393 free(he);
394 return NULL;
395 }
396 }
Arnaldo Carvalho de Melob821c732012-10-25 14:42:45 -0200397 INIT_LIST_HEAD(&he->pairs.node);
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300398 thread__get(he->thread);
Namhyung Kimaef810e2016-02-25 00:13:34 +0900399
400 if (!symbol_conf.report_hierarchy)
401 he->leaf = true;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300402 }
403
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200404 return he;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300405}
406
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300407static u8 symbol__parent_filter(const struct symbol *parent)
408{
409 if (symbol_conf.exclude_other && parent == NULL)
410 return 1 << HIST_FILTER__PARENT;
411 return 0;
412}
413
Namhyung Kim467ef102016-02-16 23:08:19 +0900414static void hist_entry__add_callchain_period(struct hist_entry *he, u64 period)
415{
416 if (!symbol_conf.use_callchain)
417 return;
418
419 he->hists->callchain_period += period;
420 if (!he->filtered)
421 he->hists->callchain_non_filtered_period += period;
422}
423
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300424static struct hist_entry *hists__findnew_entry(struct hists *hists,
425 struct hist_entry *entry,
426 struct addr_location *al,
427 bool sample_self)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300428{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300429 struct rb_node **p;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300430 struct rb_node *parent = NULL;
431 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -0700432 int64_t cmp;
Namhyung Kimf1cbf782013-12-18 14:21:11 +0900433 u64 period = entry->stat.period;
434 u64 weight = entry->stat.weight;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300435
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300436 p = &hists->entries_in->rb_node;
437
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300438 while (*p != NULL) {
439 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300440 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300441
Namhyung Kim9afcf932012-12-10 17:29:54 +0900442 /*
443 * Make sure that it receives arguments in a same order as
444 * hist_entry__collapse() so that we can use an appropriate
445 * function when searching an entry regardless which sort
446 * keys were used.
447 */
448 cmp = hist_entry__cmp(he, entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300449
450 if (!cmp) {
Namhyung Kim0f584742016-01-28 00:40:49 +0900451 if (sample_self) {
Namhyung Kima0b51af2012-09-11 13:34:27 +0900452 he_stat__add_period(&he->stat, period, weight);
Namhyung Kim467ef102016-02-16 23:08:19 +0900453 hist_entry__add_callchain_period(he, period);
Namhyung Kim0f584742016-01-28 00:40:49 +0900454 }
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900455 if (symbol_conf.cumulate_callchain)
456 he_stat__add_period(he->stat_acc, period, weight);
David Miller63fa4712012-03-27 03:14:18 -0400457
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900458 /*
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -0300459 * This mem info was allocated from sample__resolve_mem
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900460 * and will not be used anymore.
461 */
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300462 zfree(&entry->mem_info);
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900463
David Miller63fa4712012-03-27 03:14:18 -0400464 /* If the map of an existing hist_entry has
465 * become out-of-date due to an exec() or
466 * similar, update it. Otherwise we will
467 * mis-adjust symbol addresses when computing
468 * the history counter to increment.
469 */
470 if (he->ms.map != entry->ms.map) {
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300471 map__put(he->ms.map);
472 he->ms.map = map__get(entry->ms.map);
David Miller63fa4712012-03-27 03:14:18 -0400473 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300474 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300475 }
476
477 if (cmp < 0)
478 p = &(*p)->rb_left;
479 else
480 p = &(*p)->rb_right;
481 }
482
Namhyung Kima0b51af2012-09-11 13:34:27 +0900483 he = hist_entry__new(entry, sample_self);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300484 if (!he)
Namhyung Kim27a0dcb2013-05-14 11:09:02 +0900485 return NULL;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300486
Namhyung Kim0f584742016-01-28 00:40:49 +0900487 if (sample_self)
Namhyung Kim467ef102016-02-16 23:08:19 +0900488 hist_entry__add_callchain_period(he, period);
489 hists->nr_entries++;
Namhyung Kim590cd342014-12-22 13:44:09 +0900490
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300491 rb_link_node(&he->rb_node_in, parent, p);
492 rb_insert_color(&he->rb_node_in, hists->entries_in);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300493out:
Namhyung Kima0b51af2012-09-11 13:34:27 +0900494 if (sample_self)
495 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900496 if (symbol_conf.cumulate_callchain)
497 he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300498 return he;
499}
500
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300501struct hist_entry *__hists__add_entry(struct hists *hists,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100502 struct addr_location *al,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900503 struct symbol *sym_parent,
504 struct branch_info *bi,
505 struct mem_info *mi,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900506 struct perf_sample *sample,
Namhyung Kima0b51af2012-09-11 13:34:27 +0900507 bool sample_self)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100508{
509 struct hist_entry entry = {
510 .thread = al->thread,
Namhyung Kim4dfced32013-09-13 16:28:57 +0900511 .comm = thread__comm(al->thread),
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100512 .ms = {
513 .map = al->map,
514 .sym = al->sym,
515 },
Kan Liang0c4c4deb2015-09-04 10:45:42 -0400516 .socket = al->socket,
Don Zickus7365be52014-05-27 12:28:05 -0400517 .cpu = al->cpu,
518 .cpumode = al->cpumode,
519 .ip = al->addr,
520 .level = al->level,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900521 .stat = {
Namhyung Kimc4b35352012-10-04 21:49:42 +0900522 .nr_events = 1,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900523 .period = sample->period,
524 .weight = sample->weight,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900525 },
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100526 .parent = sym_parent,
Namhyung Kim2c86c7c2014-03-17 18:18:54 -0300527 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300528 .hists = hists,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900529 .branch_info = bi,
530 .mem_info = mi,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900531 .transaction = sample->transaction,
Namhyung Kim72392832015-12-24 11:16:17 +0900532 .raw_data = sample->raw_data,
533 .raw_size = sample->raw_size,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100534 };
535
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300536 return hists__findnew_entry(hists, &entry, al, sample_self);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100537}
538
Namhyung Kim69bcb012013-10-30 09:40:34 +0900539static int
540iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
541 struct addr_location *al __maybe_unused)
542{
543 return 0;
544}
545
546static int
547iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
548 struct addr_location *al __maybe_unused)
549{
550 return 0;
551}
552
553static int
554iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
555{
556 struct perf_sample *sample = iter->sample;
557 struct mem_info *mi;
558
559 mi = sample__resolve_mem(sample, al);
560 if (mi == NULL)
561 return -ENOMEM;
562
563 iter->priv = mi;
564 return 0;
565}
566
567static int
568iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
569{
570 u64 cost;
571 struct mem_info *mi = iter->priv;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300572 struct hists *hists = evsel__hists(iter->evsel);
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900573 struct perf_sample *sample = iter->sample;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900574 struct hist_entry *he;
575
576 if (mi == NULL)
577 return -EINVAL;
578
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900579 cost = sample->weight;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900580 if (!cost)
581 cost = 1;
582
583 /*
584 * must pass period=weight in order to get the correct
585 * sorting from hists__collapse_resort() which is solely
586 * based on periods. We want sorting be done on nr_events * weight
587 * and this is indirectly achieved by passing period=weight here
588 * and the he_stat__add_period() function.
589 */
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900590 sample->period = cost;
591
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300592 he = __hists__add_entry(hists, al, iter->parent, NULL, mi,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900593 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900594 if (!he)
595 return -ENOMEM;
596
597 iter->he = he;
598 return 0;
599}
600
601static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900602iter_finish_mem_entry(struct hist_entry_iter *iter,
603 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900604{
605 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300606 struct hists *hists = evsel__hists(evsel);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900607 struct hist_entry *he = iter->he;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900608 int err = -EINVAL;
609
610 if (he == NULL)
611 goto out;
612
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300613 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900614
615 err = hist_entry__append_callchain(he, iter->sample);
616
617out:
618 /*
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300619 * We don't need to free iter->priv (mem_info) here since the mem info
620 * was either already freed in hists__findnew_entry() or passed to a
621 * new hist entry by hist_entry__new().
Namhyung Kim69bcb012013-10-30 09:40:34 +0900622 */
623 iter->priv = NULL;
624
625 iter->he = NULL;
626 return err;
627}
628
629static int
630iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
631{
632 struct branch_info *bi;
633 struct perf_sample *sample = iter->sample;
634
635 bi = sample__resolve_bstack(sample, al);
636 if (!bi)
637 return -ENOMEM;
638
639 iter->curr = 0;
640 iter->total = sample->branch_stack->nr;
641
642 iter->priv = bi;
643 return 0;
644}
645
646static int
647iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused,
648 struct addr_location *al __maybe_unused)
649{
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900650 /* to avoid calling callback function */
651 iter->he = NULL;
652
Namhyung Kim69bcb012013-10-30 09:40:34 +0900653 return 0;
654}
655
656static int
657iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
658{
659 struct branch_info *bi = iter->priv;
660 int i = iter->curr;
661
662 if (bi == NULL)
663 return 0;
664
665 if (iter->curr >= iter->total)
666 return 0;
667
668 al->map = bi[i].to.map;
669 al->sym = bi[i].to.sym;
670 al->addr = bi[i].to.addr;
671 return 1;
672}
673
674static int
675iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
676{
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900677 struct branch_info *bi;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900678 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300679 struct hists *hists = evsel__hists(evsel);
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900680 struct perf_sample *sample = iter->sample;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900681 struct hist_entry *he = NULL;
682 int i = iter->curr;
683 int err = 0;
684
685 bi = iter->priv;
686
687 if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym))
688 goto out;
689
690 /*
691 * The report shows the percentage of total branches captured
692 * and not events sampled. Thus we use a pseudo period of 1.
693 */
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900694 sample->period = 1;
695 sample->weight = bi->flags.cycles ? bi->flags.cycles : 1;
696
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300697 he = __hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900698 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900699 if (he == NULL)
700 return -ENOMEM;
701
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300702 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900703
704out:
705 iter->he = he;
706 iter->curr++;
707 return err;
708}
709
710static int
711iter_finish_branch_entry(struct hist_entry_iter *iter,
712 struct addr_location *al __maybe_unused)
713{
714 zfree(&iter->priv);
715 iter->he = NULL;
716
717 return iter->curr >= iter->total ? 0 : -1;
718}
719
720static int
721iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
722 struct addr_location *al __maybe_unused)
723{
724 return 0;
725}
726
727static int
728iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
729{
730 struct perf_evsel *evsel = iter->evsel;
731 struct perf_sample *sample = iter->sample;
732 struct hist_entry *he;
733
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300734 he = __hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900735 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900736 if (he == NULL)
737 return -ENOMEM;
738
739 iter->he = he;
740 return 0;
741}
742
743static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900744iter_finish_normal_entry(struct hist_entry_iter *iter,
745 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900746{
Namhyung Kim69bcb012013-10-30 09:40:34 +0900747 struct hist_entry *he = iter->he;
748 struct perf_evsel *evsel = iter->evsel;
749 struct perf_sample *sample = iter->sample;
750
751 if (he == NULL)
752 return 0;
753
754 iter->he = NULL;
755
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300756 hists__inc_nr_samples(evsel__hists(evsel), he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900757
758 return hist_entry__append_callchain(he, sample);
759}
760
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900761static int
Adrian Hunter96b40f32015-09-25 16:15:47 +0300762iter_prepare_cumulative_entry(struct hist_entry_iter *iter,
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900763 struct addr_location *al __maybe_unused)
764{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900765 struct hist_entry **he_cache;
766
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900767 callchain_cursor_commit(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900768
769 /*
770 * This is for detecting cycles or recursions so that they're
771 * cumulated only one time to prevent entries more than 100%
772 * overhead.
773 */
Adrian Hunter96b40f32015-09-25 16:15:47 +0300774 he_cache = malloc(sizeof(*he_cache) * (iter->max_stack + 1));
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900775 if (he_cache == NULL)
776 return -ENOMEM;
777
778 iter->priv = he_cache;
779 iter->curr = 0;
780
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900781 return 0;
782}
783
784static int
785iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
786 struct addr_location *al)
787{
788 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300789 struct hists *hists = evsel__hists(evsel);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900790 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900791 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900792 struct hist_entry *he;
793 int err = 0;
794
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300795 he = __hists__add_entry(hists, al, iter->parent, NULL, NULL,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900796 sample, true);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900797 if (he == NULL)
798 return -ENOMEM;
799
800 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900801 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900802
Namhyung Kim82aa0192014-12-22 13:44:14 +0900803 hist_entry__append_callchain(he, sample);
Namhyung Kimbe7f8552013-12-26 17:44:10 +0900804
805 /*
806 * We need to re-initialize the cursor since callchain_append()
807 * advanced the cursor to the end.
808 */
809 callchain_cursor_commit(&callchain_cursor);
810
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300811 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900812
813 return err;
814}
815
816static int
817iter_next_cumulative_entry(struct hist_entry_iter *iter,
818 struct addr_location *al)
819{
820 struct callchain_cursor_node *node;
821
822 node = callchain_cursor_current(&callchain_cursor);
823 if (node == NULL)
824 return 0;
825
Namhyung Kimc7405d82013-10-31 13:58:30 +0900826 return fill_callchain_info(al, node, iter->hide_unresolved);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900827}
828
829static int
830iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
831 struct addr_location *al)
832{
833 struct perf_evsel *evsel = iter->evsel;
834 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900835 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900836 struct hist_entry *he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900837 struct hist_entry he_tmp = {
Arnaldo Carvalho de Melo5cef8972015-08-10 15:45:55 -0300838 .hists = evsel__hists(evsel),
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900839 .cpu = al->cpu,
840 .thread = al->thread,
841 .comm = thread__comm(al->thread),
842 .ip = al->addr,
843 .ms = {
844 .map = al->map,
845 .sym = al->sym,
846 },
847 .parent = iter->parent,
Namhyung Kim72392832015-12-24 11:16:17 +0900848 .raw_data = sample->raw_data,
849 .raw_size = sample->raw_size,
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900850 };
851 int i;
Namhyung Kimbe7f8552013-12-26 17:44:10 +0900852 struct callchain_cursor cursor;
853
854 callchain_cursor_snapshot(&cursor, &callchain_cursor);
855
856 callchain_cursor_advance(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900857
858 /*
859 * Check if there's duplicate entries in the callchain.
860 * It's possible that it has cycles or recursive calls.
861 */
862 for (i = 0; i < iter->curr; i++) {
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900863 if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
864 /* to avoid calling callback function */
865 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900866 return 0;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900867 }
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900868 }
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900869
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300870 he = __hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900871 sample, false);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900872 if (he == NULL)
873 return -ENOMEM;
874
875 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900876 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900877
Namhyung Kim82aa0192014-12-22 13:44:14 +0900878 if (symbol_conf.use_callchain)
879 callchain_append(he->callchain, &cursor, sample->period);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900880 return 0;
881}
882
883static int
884iter_finish_cumulative_entry(struct hist_entry_iter *iter,
885 struct addr_location *al __maybe_unused)
886{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900887 zfree(&iter->priv);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900888 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900889
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900890 return 0;
891}
892
Namhyung Kim69bcb012013-10-30 09:40:34 +0900893const struct hist_iter_ops hist_iter_mem = {
894 .prepare_entry = iter_prepare_mem_entry,
895 .add_single_entry = iter_add_single_mem_entry,
896 .next_entry = iter_next_nop_entry,
897 .add_next_entry = iter_add_next_nop_entry,
898 .finish_entry = iter_finish_mem_entry,
899};
900
901const struct hist_iter_ops hist_iter_branch = {
902 .prepare_entry = iter_prepare_branch_entry,
903 .add_single_entry = iter_add_single_branch_entry,
904 .next_entry = iter_next_branch_entry,
905 .add_next_entry = iter_add_next_branch_entry,
906 .finish_entry = iter_finish_branch_entry,
907};
908
909const struct hist_iter_ops hist_iter_normal = {
910 .prepare_entry = iter_prepare_normal_entry,
911 .add_single_entry = iter_add_single_normal_entry,
912 .next_entry = iter_next_nop_entry,
913 .add_next_entry = iter_add_next_nop_entry,
914 .finish_entry = iter_finish_normal_entry,
915};
916
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900917const struct hist_iter_ops hist_iter_cumulative = {
918 .prepare_entry = iter_prepare_cumulative_entry,
919 .add_single_entry = iter_add_single_cumulative_entry,
920 .next_entry = iter_next_cumulative_entry,
921 .add_next_entry = iter_add_next_cumulative_entry,
922 .finish_entry = iter_finish_cumulative_entry,
923};
924
Namhyung Kim69bcb012013-10-30 09:40:34 +0900925int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900926 int max_stack_depth, void *arg)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900927{
928 int err, err2;
929
Namhyung Kim063bd932015-05-19 17:04:10 +0900930 err = sample__resolve_callchain(iter->sample, &iter->parent,
931 iter->evsel, al, max_stack_depth);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900932 if (err)
933 return err;
934
Adrian Hunter96b40f32015-09-25 16:15:47 +0300935 iter->max_stack = max_stack_depth;
936
Namhyung Kim69bcb012013-10-30 09:40:34 +0900937 err = iter->ops->prepare_entry(iter, al);
938 if (err)
939 goto out;
940
941 err = iter->ops->add_single_entry(iter, al);
942 if (err)
943 goto out;
944
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900945 if (iter->he && iter->add_entry_cb) {
946 err = iter->add_entry_cb(iter, al, true, arg);
947 if (err)
948 goto out;
949 }
950
Namhyung Kim69bcb012013-10-30 09:40:34 +0900951 while (iter->ops->next_entry(iter, al)) {
952 err = iter->ops->add_next_entry(iter, al);
953 if (err)
954 break;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900955
956 if (iter->he && iter->add_entry_cb) {
957 err = iter->add_entry_cb(iter, al, false, arg);
958 if (err)
959 goto out;
960 }
Namhyung Kim69bcb012013-10-30 09:40:34 +0900961 }
962
963out:
964 err2 = iter->ops->finish_entry(iter, al);
965 if (!err)
966 err = err2;
967
968 return err;
969}
970
John Kacur3d1d07e2009-09-28 15:32:55 +0200971int64_t
972hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
973{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +0100974 struct hists *hists = left->hists;
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900975 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +0200976 int64_t cmp = 0;
977
Jiri Olsaaa6f50a2016-01-18 10:24:24 +0100978 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim87bbdf72015-01-08 09:45:46 +0900979 cmp = fmt->cmp(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200980 if (cmp)
981 break;
982 }
983
984 return cmp;
985}
986
987int64_t
988hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
989{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +0100990 struct hists *hists = left->hists;
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900991 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +0200992 int64_t cmp = 0;
993
Jiri Olsaaa6f50a2016-01-18 10:24:24 +0100994 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim87bbdf72015-01-08 09:45:46 +0900995 cmp = fmt->collapse(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200996 if (cmp)
997 break;
998 }
999
1000 return cmp;
1001}
1002
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -03001003void hist_entry__delete(struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +02001004{
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001005 thread__zput(he->thread);
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -03001006 map__zput(he->ms.map);
1007
1008 if (he->branch_info) {
1009 map__zput(he->branch_info->from.map);
1010 map__zput(he->branch_info->to.map);
1011 zfree(&he->branch_info);
1012 }
1013
1014 if (he->mem_info) {
1015 map__zput(he->mem_info->iaddr.map);
1016 map__zput(he->mem_info->daddr.map);
1017 zfree(&he->mem_info);
1018 }
1019
Namhyung Kimf8be1c82012-09-11 13:15:07 +09001020 zfree(&he->stat_acc);
Namhyung Kimf048d542013-09-11 14:09:28 +09001021 free_srcline(he->srcline);
Andi Kleen31191a82015-08-07 15:54:24 -07001022 if (he->srcfile && he->srcfile[0])
1023 free(he->srcfile);
Namhyung Kimd1149602014-12-30 14:38:13 +09001024 free_callchain(he->callchain);
Namhyung Kim60517d22015-12-23 02:07:03 +09001025 free(he->trace_output);
Namhyung Kim72392832015-12-24 11:16:17 +09001026 free(he->raw_data);
John Kacur3d1d07e2009-09-28 15:32:55 +02001027 free(he);
1028}
1029
1030/*
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -03001031 * If this is not the last column, then we need to pad it according to the
1032 * pre-calculated max lenght for this column, otherwise don't bother adding
1033 * spaces because that would break viewing this with, for instance, 'less',
1034 * that would show tons of trailing spaces when a long C++ demangled method
1035 * names is sampled.
1036*/
1037int hist_entry__snprintf_alignment(struct hist_entry *he, struct perf_hpp *hpp,
1038 struct perf_hpp_fmt *fmt, int printed)
1039{
1040 if (!list_is_last(&fmt->list, &he->hists->hpp_list->fields)) {
1041 const int width = fmt->width(fmt, hpp, hists_to_evsel(he->hists));
1042 if (printed < width) {
1043 advance_hpp(hpp, printed);
1044 printed = scnprintf(hpp->buf, hpp->size, "%-*s", width - printed, " ");
1045 }
1046 }
1047
1048 return printed;
1049}
1050
1051/*
John Kacur3d1d07e2009-09-28 15:32:55 +02001052 * collapse the histogram
1053 */
1054
Namhyung Kimaef810e2016-02-25 00:13:34 +09001055static void hists__apply_filters(struct hists *hists, struct hist_entry *he);
1056
1057static struct hist_entry *hierarchy_insert_entry(struct hists *hists,
1058 struct rb_root *root,
1059 struct hist_entry *he,
1060 struct perf_hpp_fmt *fmt)
1061{
1062 struct rb_node **p = &root->rb_node;
1063 struct rb_node *parent = NULL;
1064 struct hist_entry *iter, *new;
1065 int64_t cmp;
1066
1067 while (*p != NULL) {
1068 parent = *p;
1069 iter = rb_entry(parent, struct hist_entry, rb_node_in);
1070
1071 cmp = fmt->collapse(fmt, iter, he);
1072 if (!cmp) {
1073 he_stat__add_stat(&iter->stat, &he->stat);
1074 return iter;
1075 }
1076
1077 if (cmp < 0)
1078 p = &parent->rb_left;
1079 else
1080 p = &parent->rb_right;
1081 }
1082
1083 new = hist_entry__new(he, true);
1084 if (new == NULL)
1085 return NULL;
1086
1087 hists__apply_filters(hists, new);
1088 hists->nr_entries++;
1089
1090 /* save related format for output */
1091 new->fmt = fmt;
1092
1093 /* some fields are now passed to 'new' */
1094 if (perf_hpp__is_trace_entry(fmt))
1095 he->trace_output = NULL;
1096 else
1097 new->trace_output = NULL;
1098
1099 if (perf_hpp__is_srcline_entry(fmt))
1100 he->srcline = NULL;
1101 else
1102 new->srcline = NULL;
1103
1104 if (perf_hpp__is_srcfile_entry(fmt))
1105 he->srcfile = NULL;
1106 else
1107 new->srcfile = NULL;
1108
1109 rb_link_node(&new->rb_node_in, parent, p);
1110 rb_insert_color(&new->rb_node_in, root);
1111 return new;
1112}
1113
1114static int hists__hierarchy_insert_entry(struct hists *hists,
1115 struct rb_root *root,
1116 struct hist_entry *he)
1117{
1118 struct perf_hpp_fmt *fmt;
1119 struct hist_entry *new_he = NULL;
1120 struct hist_entry *parent = NULL;
1121 int depth = 0;
1122 int ret = 0;
1123
1124 hists__for_each_sort_list(hists, fmt) {
1125 if (!perf_hpp__is_sort_entry(fmt) &&
1126 !perf_hpp__is_dynamic_entry(fmt))
1127 continue;
1128 if (perf_hpp__should_skip(fmt, hists))
1129 continue;
1130
1131 /* insert copy of 'he' for each fmt into the hierarchy */
1132 new_he = hierarchy_insert_entry(hists, root, he, fmt);
1133 if (new_he == NULL) {
1134 ret = -1;
1135 break;
1136 }
1137
1138 root = &new_he->hroot_in;
1139 new_he->parent_he = parent;
1140 new_he->depth = depth++;
1141 parent = new_he;
1142 }
1143
1144 if (new_he) {
1145 new_he->leaf = true;
1146
1147 if (symbol_conf.use_callchain) {
1148 callchain_cursor_reset(&callchain_cursor);
1149 if (callchain_merge(&callchain_cursor,
1150 new_he->callchain,
1151 he->callchain) < 0)
1152 ret = -1;
1153 }
1154 }
1155
1156 /* 'he' is no longer used */
1157 hist_entry__delete(he);
1158
1159 /* return 0 (or -1) since it already applied filters */
1160 return ret;
1161}
1162
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001163int hists__collapse_insert_entry(struct hists *hists, struct rb_root *root,
1164 struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +02001165{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -02001166 struct rb_node **p = &root->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +02001167 struct rb_node *parent = NULL;
1168 struct hist_entry *iter;
1169 int64_t cmp;
1170
Namhyung Kimaef810e2016-02-25 00:13:34 +09001171 if (symbol_conf.report_hierarchy)
1172 return hists__hierarchy_insert_entry(hists, root, he);
1173
John Kacur3d1d07e2009-09-28 15:32:55 +02001174 while (*p != NULL) {
1175 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001176 iter = rb_entry(parent, struct hist_entry, rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +02001177
1178 cmp = hist_entry__collapse(iter, he);
1179
1180 if (!cmp) {
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001181 int ret = 0;
1182
Namhyung Kim139c0812012-10-04 21:49:43 +09001183 he_stat__add_stat(&iter->stat, &he->stat);
Namhyung Kimf8be1c82012-09-11 13:15:07 +09001184 if (symbol_conf.cumulate_callchain)
1185 he_stat__add_stat(iter->stat_acc, he->stat_acc);
Namhyung Kim9ec60972012-09-26 16:47:28 +09001186
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +01001187 if (symbol_conf.use_callchain) {
Namhyung Kim47260642012-05-31 14:43:26 +09001188 callchain_cursor_reset(&callchain_cursor);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001189 if (callchain_merge(&callchain_cursor,
1190 iter->callchain,
1191 he->callchain) < 0)
1192 ret = -1;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +01001193 }
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -03001194 hist_entry__delete(he);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001195 return ret;
John Kacur3d1d07e2009-09-28 15:32:55 +02001196 }
1197
1198 if (cmp < 0)
1199 p = &(*p)->rb_left;
1200 else
1201 p = &(*p)->rb_right;
1202 }
Namhyung Kim740b97f2014-12-22 13:44:10 +09001203 hists->nr_entries++;
John Kacur3d1d07e2009-09-28 15:32:55 +02001204
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001205 rb_link_node(&he->rb_node_in, parent, p);
1206 rb_insert_color(&he->rb_node_in, root);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001207 return 1;
John Kacur3d1d07e2009-09-28 15:32:55 +02001208}
1209
Namhyung Kimfc284be2016-01-07 10:14:10 +01001210struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001211{
1212 struct rb_root *root;
1213
1214 pthread_mutex_lock(&hists->lock);
1215
1216 root = hists->entries_in;
1217 if (++hists->entries_in > &hists->entries_in_array[1])
1218 hists->entries_in = &hists->entries_in_array[0];
1219
1220 pthread_mutex_unlock(&hists->lock);
1221
1222 return root;
1223}
1224
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001225static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
1226{
1227 hists__filter_entry_by_dso(hists, he);
1228 hists__filter_entry_by_thread(hists, he);
Namhyung Kime94d53e2012-03-16 17:50:51 +09001229 hists__filter_entry_by_symbol(hists, he);
Kan Liang21394d92015-09-04 10:45:44 -04001230 hists__filter_entry_by_socket(hists, he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001231}
1232
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001233int hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001234{
1235 struct rb_root *root;
1236 struct rb_node *next;
1237 struct hist_entry *n;
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001238 int ret;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001239
Namhyung Kim3a5714f2013-05-14 11:09:01 +09001240 if (!sort__need_collapse)
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001241 return 0;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001242
Namhyung Kim740b97f2014-12-22 13:44:10 +09001243 hists->nr_entries = 0;
1244
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001245 root = hists__get_rotate_entries_in(hists);
Namhyung Kim740b97f2014-12-22 13:44:10 +09001246
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001247 next = rb_first(root);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001248
1249 while (next) {
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001250 if (session_done())
1251 break;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001252 n = rb_entry(next, struct hist_entry, rb_node_in);
1253 next = rb_next(&n->rb_node_in);
1254
1255 rb_erase(&n->rb_node_in, root);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001256 ret = hists__collapse_insert_entry(hists, &hists->entries_collapsed, n);
1257 if (ret < 0)
1258 return -1;
1259
1260 if (ret) {
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001261 /*
1262 * If it wasn't combined with one of the entries already
1263 * collapsed, we need to apply the filters that may have
1264 * been set by, say, the hist_browser.
1265 */
1266 hists__apply_filters(hists, n);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001267 }
Namhyung Kimc1fb5652013-10-11 14:15:38 +09001268 if (prog)
1269 ui_progress__update(prog, 1);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001270 }
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001271 return 0;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001272}
1273
Namhyung Kim043ca3892014-03-03 14:18:00 +09001274static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001275{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001276 struct hists *hists = a->hists;
Namhyung Kim043ca3892014-03-03 14:18:00 +09001277 struct perf_hpp_fmt *fmt;
1278 int64_t cmp = 0;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001279
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001280 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim361459f2015-12-23 02:07:08 +09001281 if (perf_hpp__should_skip(fmt, a->hists))
Namhyung Kime67d49a2014-03-18 13:00:59 +09001282 continue;
1283
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001284 cmp = fmt->sort(fmt, a, b);
Namhyung Kim043ca3892014-03-03 14:18:00 +09001285 if (cmp)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001286 break;
1287 }
1288
Namhyung Kim043ca3892014-03-03 14:18:00 +09001289 return cmp;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001290}
1291
Namhyung Kim9283ba92014-04-24 16:37:26 +09001292static void hists__reset_filter_stats(struct hists *hists)
1293{
1294 hists->nr_non_filtered_entries = 0;
1295 hists->stats.total_non_filtered_period = 0;
1296}
1297
1298void hists__reset_stats(struct hists *hists)
1299{
1300 hists->nr_entries = 0;
1301 hists->stats.total_period = 0;
1302
1303 hists__reset_filter_stats(hists);
1304}
1305
1306static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
1307{
1308 hists->nr_non_filtered_entries++;
1309 hists->stats.total_non_filtered_period += h->stat.period;
1310}
1311
1312void hists__inc_stats(struct hists *hists, struct hist_entry *h)
1313{
1314 if (!h->filtered)
1315 hists__inc_filter_stats(hists, h);
1316
1317 hists->nr_entries++;
1318 hists->stats.total_period += h->stat.period;
1319}
1320
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001321static void hierarchy_insert_output_entry(struct rb_root *root,
1322 struct hist_entry *he)
1323{
1324 struct rb_node **p = &root->rb_node;
1325 struct rb_node *parent = NULL;
1326 struct hist_entry *iter;
1327
1328 while (*p != NULL) {
1329 parent = *p;
1330 iter = rb_entry(parent, struct hist_entry, rb_node);
1331
1332 if (hist_entry__sort(he, iter) > 0)
1333 p = &parent->rb_left;
1334 else
1335 p = &parent->rb_right;
1336 }
1337
1338 rb_link_node(&he->rb_node, parent, p);
1339 rb_insert_color(&he->rb_node, root);
1340}
1341
1342static void hists__hierarchy_output_resort(struct hists *hists,
1343 struct ui_progress *prog,
1344 struct rb_root *root_in,
1345 struct rb_root *root_out,
1346 u64 min_callchain_hits,
1347 bool use_callchain)
1348{
1349 struct rb_node *node;
1350 struct hist_entry *he;
1351
1352 *root_out = RB_ROOT;
1353 node = rb_first(root_in);
1354
1355 while (node) {
1356 he = rb_entry(node, struct hist_entry, rb_node_in);
1357 node = rb_next(node);
1358
1359 hierarchy_insert_output_entry(root_out, he);
1360
1361 if (prog)
1362 ui_progress__update(prog, 1);
1363
1364 if (!he->leaf) {
1365 hists__hierarchy_output_resort(hists, prog,
1366 &he->hroot_in,
1367 &he->hroot_out,
1368 min_callchain_hits,
1369 use_callchain);
1370 hists->nr_entries++;
1371 if (!he->filtered) {
1372 hists->nr_non_filtered_entries++;
1373 hists__calc_col_len(hists, he);
1374 }
1375
1376 continue;
1377 }
1378
1379 /* only update stat for leaf entries to avoid duplication */
1380 hists__inc_stats(hists, he);
1381 if (!he->filtered)
1382 hists__calc_col_len(hists, he);
1383
1384 if (!use_callchain)
1385 continue;
1386
1387 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1388 u64 total = he->stat.period;
1389
1390 if (symbol_conf.cumulate_callchain)
1391 total = he->stat_acc->period;
1392
1393 min_callchain_hits = total * (callchain_param.min_percent / 100);
1394 }
1395
1396 callchain_param.sort(&he->sorted_chain, he->callchain,
1397 min_callchain_hits, &callchain_param);
1398 }
1399}
1400
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001401static void __hists__insert_output_entry(struct rb_root *entries,
1402 struct hist_entry *he,
Kan Liangf9db0d02015-08-11 06:30:48 -04001403 u64 min_callchain_hits,
1404 bool use_callchain)
John Kacur3d1d07e2009-09-28 15:32:55 +02001405{
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001406 struct rb_node **p = &entries->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +02001407 struct rb_node *parent = NULL;
1408 struct hist_entry *iter;
1409
Namhyung Kim744070e2016-01-28 00:40:48 +09001410 if (use_callchain) {
1411 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1412 u64 total = he->stat.period;
1413
1414 if (symbol_conf.cumulate_callchain)
1415 total = he->stat_acc->period;
1416
1417 min_callchain_hits = total * (callchain_param.min_percent / 100);
1418 }
Arnaldo Carvalho de Melob9fb9302010-04-02 09:50:42 -03001419 callchain_param.sort(&he->sorted_chain, he->callchain,
John Kacur3d1d07e2009-09-28 15:32:55 +02001420 min_callchain_hits, &callchain_param);
Namhyung Kim744070e2016-01-28 00:40:48 +09001421 }
John Kacur3d1d07e2009-09-28 15:32:55 +02001422
1423 while (*p != NULL) {
1424 parent = *p;
1425 iter = rb_entry(parent, struct hist_entry, rb_node);
1426
Namhyung Kim043ca3892014-03-03 14:18:00 +09001427 if (hist_entry__sort(he, iter) > 0)
John Kacur3d1d07e2009-09-28 15:32:55 +02001428 p = &(*p)->rb_left;
1429 else
1430 p = &(*p)->rb_right;
1431 }
1432
1433 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001434 rb_insert_color(&he->rb_node, entries);
John Kacur3d1d07e2009-09-28 15:32:55 +02001435}
1436
Jiri Olsa01441af2016-01-18 10:23:59 +01001437static void output_resort(struct hists *hists, struct ui_progress *prog,
1438 bool use_callchain)
John Kacur3d1d07e2009-09-28 15:32:55 +02001439{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001440 struct rb_root *root;
John Kacur3d1d07e2009-09-28 15:32:55 +02001441 struct rb_node *next;
1442 struct hist_entry *n;
Namhyung Kim467ef102016-02-16 23:08:19 +09001443 u64 callchain_total;
John Kacur3d1d07e2009-09-28 15:32:55 +02001444 u64 min_callchain_hits;
1445
Namhyung Kim467ef102016-02-16 23:08:19 +09001446 callchain_total = hists->callchain_period;
1447 if (symbol_conf.filter_relative)
1448 callchain_total = hists->callchain_non_filtered_period;
1449
1450 min_callchain_hits = callchain_total * (callchain_param.min_percent / 100);
John Kacur3d1d07e2009-09-28 15:32:55 +02001451
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001452 hists__reset_stats(hists);
1453 hists__reset_col_len(hists);
1454
1455 if (symbol_conf.report_hierarchy) {
1456 return hists__hierarchy_output_resort(hists, prog,
1457 &hists->entries_collapsed,
1458 &hists->entries,
1459 min_callchain_hits,
1460 use_callchain);
1461 }
1462
Namhyung Kim3a5714f2013-05-14 11:09:01 +09001463 if (sort__need_collapse)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001464 root = &hists->entries_collapsed;
1465 else
1466 root = hists->entries_in;
1467
1468 next = rb_first(root);
1469 hists->entries = RB_ROOT;
John Kacur3d1d07e2009-09-28 15:32:55 +02001470
1471 while (next) {
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001472 n = rb_entry(next, struct hist_entry, rb_node_in);
1473 next = rb_next(&n->rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +02001474
Kan Liangf9db0d02015-08-11 06:30:48 -04001475 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits, use_callchain);
Namhyung Kim62638352014-04-24 16:21:46 +09001476 hists__inc_stats(hists, n);
Namhyung Kimae993ef2014-04-24 16:25:19 +09001477
1478 if (!n->filtered)
1479 hists__calc_col_len(hists, n);
Namhyung Kim740b97f2014-12-22 13:44:10 +09001480
1481 if (prog)
1482 ui_progress__update(prog, 1);
John Kacur3d1d07e2009-09-28 15:32:55 +02001483 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001484}
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -02001485
Jiri Olsa452ce032016-01-18 10:24:00 +01001486void perf_evsel__output_resort(struct perf_evsel *evsel, struct ui_progress *prog)
Jiri Olsa01441af2016-01-18 10:23:59 +01001487{
Jiri Olsa01441af2016-01-18 10:23:59 +01001488 bool use_callchain;
1489
1490 if (evsel && symbol_conf.use_callchain && !symbol_conf.show_ref_callgraph)
1491 use_callchain = evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN;
1492 else
1493 use_callchain = symbol_conf.use_callchain;
1494
Jiri Olsa452ce032016-01-18 10:24:00 +01001495 output_resort(evsel__hists(evsel), prog, use_callchain);
1496}
1497
1498void hists__output_resort(struct hists *hists, struct ui_progress *prog)
1499{
1500 output_resort(hists, prog, symbol_conf.use_callchain);
Jiri Olsa01441af2016-01-18 10:23:59 +01001501}
1502
Namhyung Kim8c018722016-02-25 00:13:36 +09001503static bool can_goto_child(struct hist_entry *he, enum hierarchy_move_dir hmd)
1504{
1505 if (he->leaf || hmd == HMD_FORCE_SIBLING)
1506 return false;
1507
1508 if (he->unfolded || hmd == HMD_FORCE_CHILD)
1509 return true;
1510
1511 return false;
1512}
1513
1514struct rb_node *rb_hierarchy_last(struct rb_node *node)
1515{
1516 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1517
1518 while (can_goto_child(he, HMD_NORMAL)) {
1519 node = rb_last(&he->hroot_out);
1520 he = rb_entry(node, struct hist_entry, rb_node);
1521 }
1522 return node;
1523}
1524
1525struct rb_node *__rb_hierarchy_next(struct rb_node *node, enum hierarchy_move_dir hmd)
1526{
1527 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1528
1529 if (can_goto_child(he, hmd))
1530 node = rb_first(&he->hroot_out);
1531 else
1532 node = rb_next(node);
1533
1534 while (node == NULL) {
1535 he = he->parent_he;
1536 if (he == NULL)
1537 break;
1538
1539 node = rb_next(&he->rb_node);
1540 }
1541 return node;
1542}
1543
1544struct rb_node *rb_hierarchy_prev(struct rb_node *node)
1545{
1546 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1547
1548 node = rb_prev(node);
1549 if (node)
1550 return rb_hierarchy_last(node);
1551
1552 he = he->parent_he;
1553 if (he == NULL)
1554 return NULL;
1555
1556 return &he->rb_node;
1557}
1558
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001559static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001560 enum hist_filter filter)
1561{
1562 h->filtered &= ~(1 << filter);
Namhyung Kim155e9af2016-02-25 00:13:38 +09001563
1564 if (symbol_conf.report_hierarchy) {
1565 struct hist_entry *parent = h->parent_he;
1566
1567 while (parent) {
1568 he_stat__add_stat(&parent->stat, &h->stat);
1569
1570 parent->filtered &= ~(1 << filter);
1571
1572 if (parent->filtered)
1573 goto next;
1574
1575 /* force fold unfiltered entry for simplicity */
1576 parent->unfolded = false;
1577 parent->row_offset = 0;
1578 parent->nr_rows = 0;
1579next:
1580 parent = parent->parent_he;
1581 }
1582 }
1583
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001584 if (h->filtered)
1585 return;
1586
Namhyung Kim87e90f42014-04-24 16:44:16 +09001587 /* force fold unfiltered entry for simplicity */
Namhyung Kim3698dab2015-05-05 23:55:46 +09001588 h->unfolded = false;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -03001589 h->row_offset = 0;
He Kuanga8cd1f42015-03-11 20:36:03 +08001590 h->nr_rows = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001591
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001592 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001593
Namhyung Kim9283ba92014-04-24 16:37:26 +09001594 hists__inc_filter_stats(hists, h);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001595 hists__calc_col_len(hists, h);
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001596}
1597
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001598
1599static bool hists__filter_entry_by_dso(struct hists *hists,
1600 struct hist_entry *he)
1601{
1602 if (hists->dso_filter != NULL &&
1603 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
1604 he->filtered |= (1 << HIST_FILTER__DSO);
1605 return true;
1606 }
1607
1608 return false;
1609}
1610
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001611static bool hists__filter_entry_by_thread(struct hists *hists,
1612 struct hist_entry *he)
1613{
1614 if (hists->thread_filter != NULL &&
1615 he->thread != hists->thread_filter) {
1616 he->filtered |= (1 << HIST_FILTER__THREAD);
1617 return true;
1618 }
1619
1620 return false;
1621}
1622
Namhyung Kime94d53e2012-03-16 17:50:51 +09001623static bool hists__filter_entry_by_symbol(struct hists *hists,
1624 struct hist_entry *he)
1625{
1626 if (hists->symbol_filter_str != NULL &&
1627 (!he->ms.sym || strstr(he->ms.sym->name,
1628 hists->symbol_filter_str) == NULL)) {
1629 he->filtered |= (1 << HIST_FILTER__SYMBOL);
1630 return true;
1631 }
1632
1633 return false;
1634}
1635
Kan Liang21394d92015-09-04 10:45:44 -04001636static bool hists__filter_entry_by_socket(struct hists *hists,
1637 struct hist_entry *he)
1638{
1639 if ((hists->socket_filter > -1) &&
1640 (he->socket != hists->socket_filter)) {
1641 he->filtered |= (1 << HIST_FILTER__SOCKET);
1642 return true;
1643 }
1644
1645 return false;
1646}
1647
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001648typedef bool (*filter_fn_t)(struct hists *hists, struct hist_entry *he);
1649
1650static void hists__filter_by_type(struct hists *hists, int type, filter_fn_t filter)
Kan Liang84734b02015-09-04 10:45:45 -04001651{
1652 struct rb_node *nd;
1653
1654 hists->stats.nr_non_filtered_samples = 0;
1655
1656 hists__reset_filter_stats(hists);
1657 hists__reset_col_len(hists);
1658
1659 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1660 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1661
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001662 if (filter(hists, h))
Kan Liang84734b02015-09-04 10:45:45 -04001663 continue;
1664
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001665 hists__remove_entry_filter(hists, h, type);
Kan Liang84734b02015-09-04 10:45:45 -04001666 }
1667}
1668
Namhyung Kim155e9af2016-02-25 00:13:38 +09001669static void hists__filter_hierarchy(struct hists *hists, int type, const void *arg)
1670{
1671 struct rb_node *nd;
1672
1673 hists->stats.nr_non_filtered_samples = 0;
1674
1675 hists__reset_filter_stats(hists);
1676 hists__reset_col_len(hists);
1677
1678 nd = rb_first(&hists->entries);
1679 while (nd) {
1680 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1681 int ret;
1682
1683 ret = hist_entry__filter(h, type, arg);
1684
1685 /*
1686 * case 1. non-matching type
1687 * zero out the period, set filter marker and move to child
1688 */
1689 if (ret < 0) {
1690 memset(&h->stat, 0, sizeof(h->stat));
1691 h->filtered |= (1 << type);
1692
1693 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_CHILD);
1694 }
1695 /*
1696 * case 2. matched type (filter out)
1697 * set filter marker and move to next
1698 */
1699 else if (ret == 1) {
1700 h->filtered |= (1 << type);
1701
1702 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
1703 }
1704 /*
1705 * case 3. ok (not filtered)
1706 * add period to hists and parents, erase the filter marker
1707 * and move to next sibling
1708 */
1709 else {
1710 hists__remove_entry_filter(hists, h, type);
1711
1712 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
1713 }
1714 }
1715}
1716
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001717void hists__filter_by_thread(struct hists *hists)
1718{
Namhyung Kim155e9af2016-02-25 00:13:38 +09001719 if (symbol_conf.report_hierarchy)
1720 hists__filter_hierarchy(hists, HIST_FILTER__THREAD,
1721 hists->thread_filter);
1722 else
1723 hists__filter_by_type(hists, HIST_FILTER__THREAD,
1724 hists__filter_entry_by_thread);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001725}
1726
1727void hists__filter_by_dso(struct hists *hists)
1728{
Namhyung Kim155e9af2016-02-25 00:13:38 +09001729 if (symbol_conf.report_hierarchy)
1730 hists__filter_hierarchy(hists, HIST_FILTER__DSO,
1731 hists->dso_filter);
1732 else
1733 hists__filter_by_type(hists, HIST_FILTER__DSO,
1734 hists__filter_entry_by_dso);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001735}
1736
1737void hists__filter_by_symbol(struct hists *hists)
1738{
Namhyung Kim155e9af2016-02-25 00:13:38 +09001739 if (symbol_conf.report_hierarchy)
1740 hists__filter_hierarchy(hists, HIST_FILTER__SYMBOL,
1741 hists->symbol_filter_str);
1742 else
1743 hists__filter_by_type(hists, HIST_FILTER__SYMBOL,
1744 hists__filter_entry_by_symbol);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001745}
1746
1747void hists__filter_by_socket(struct hists *hists)
1748{
Namhyung Kim155e9af2016-02-25 00:13:38 +09001749 if (symbol_conf.report_hierarchy)
1750 hists__filter_hierarchy(hists, HIST_FILTER__SOCKET,
1751 &hists->socket_filter);
1752 else
1753 hists__filter_by_type(hists, HIST_FILTER__SOCKET,
1754 hists__filter_entry_by_socket);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001755}
1756
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001757void events_stats__inc(struct events_stats *stats, u32 type)
1758{
1759 ++stats->nr_events[0];
1760 ++stats->nr_events[type];
1761}
1762
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001763void hists__inc_nr_events(struct hists *hists, u32 type)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001764{
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001765 events_stats__inc(&hists->stats, type);
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001766}
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001767
Namhyung Kim1844dbc2014-05-28 14:12:18 +09001768void hists__inc_nr_samples(struct hists *hists, bool filtered)
1769{
1770 events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
1771 if (!filtered)
1772 hists->stats.nr_non_filtered_samples++;
1773}
1774
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001775static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
1776 struct hist_entry *pair)
1777{
Namhyung Kimce74f602012-12-10 17:29:55 +09001778 struct rb_root *root;
1779 struct rb_node **p;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001780 struct rb_node *parent = NULL;
1781 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -07001782 int64_t cmp;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001783
Namhyung Kimce74f602012-12-10 17:29:55 +09001784 if (sort__need_collapse)
1785 root = &hists->entries_collapsed;
1786 else
1787 root = hists->entries_in;
1788
1789 p = &root->rb_node;
1790
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001791 while (*p != NULL) {
1792 parent = *p;
Namhyung Kimce74f602012-12-10 17:29:55 +09001793 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001794
Namhyung Kimce74f602012-12-10 17:29:55 +09001795 cmp = hist_entry__collapse(he, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001796
1797 if (!cmp)
1798 goto out;
1799
1800 if (cmp < 0)
1801 p = &(*p)->rb_left;
1802 else
1803 p = &(*p)->rb_right;
1804 }
1805
Namhyung Kima0b51af2012-09-11 13:34:27 +09001806 he = hist_entry__new(pair, true);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001807 if (he) {
Arnaldo Carvalho de Melo30193d72012-11-12 13:20:03 -03001808 memset(&he->stat, 0, sizeof(he->stat));
1809 he->hists = hists;
Namhyung Kimce74f602012-12-10 17:29:55 +09001810 rb_link_node(&he->rb_node_in, parent, p);
1811 rb_insert_color(&he->rb_node_in, root);
Namhyung Kim62638352014-04-24 16:21:46 +09001812 hists__inc_stats(hists, he);
Jiri Olsae0af43d2012-12-01 21:18:20 +01001813 he->dummy = true;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001814 }
1815out:
1816 return he;
1817}
1818
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001819static struct hist_entry *hists__find_entry(struct hists *hists,
1820 struct hist_entry *he)
1821{
Namhyung Kimce74f602012-12-10 17:29:55 +09001822 struct rb_node *n;
1823
1824 if (sort__need_collapse)
1825 n = hists->entries_collapsed.rb_node;
1826 else
1827 n = hists->entries_in->rb_node;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001828
1829 while (n) {
Namhyung Kimce74f602012-12-10 17:29:55 +09001830 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
1831 int64_t cmp = hist_entry__collapse(iter, he);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001832
1833 if (cmp < 0)
1834 n = n->rb_left;
1835 else if (cmp > 0)
1836 n = n->rb_right;
1837 else
1838 return iter;
1839 }
1840
1841 return NULL;
1842}
1843
1844/*
1845 * Look for pairs to link to the leader buckets (hist_entries):
1846 */
1847void hists__match(struct hists *leader, struct hists *other)
1848{
Namhyung Kimce74f602012-12-10 17:29:55 +09001849 struct rb_root *root;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001850 struct rb_node *nd;
1851 struct hist_entry *pos, *pair;
1852
Namhyung Kimce74f602012-12-10 17:29:55 +09001853 if (sort__need_collapse)
1854 root = &leader->entries_collapsed;
1855 else
1856 root = leader->entries_in;
1857
1858 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
1859 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001860 pair = hists__find_entry(other, pos);
1861
1862 if (pair)
Namhyung Kim5fa90412012-11-29 15:38:34 +09001863 hist_entry__add_pair(pair, pos);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001864 }
1865}
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001866
1867/*
1868 * Look for entries in the other hists that are not present in the leader, if
1869 * we find them, just add a dummy entry on the leader hists, with period=0,
1870 * nr_events=0, to serve as the list header.
1871 */
1872int hists__link(struct hists *leader, struct hists *other)
1873{
Namhyung Kimce74f602012-12-10 17:29:55 +09001874 struct rb_root *root;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001875 struct rb_node *nd;
1876 struct hist_entry *pos, *pair;
1877
Namhyung Kimce74f602012-12-10 17:29:55 +09001878 if (sort__need_collapse)
1879 root = &other->entries_collapsed;
1880 else
1881 root = other->entries_in;
1882
1883 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
1884 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001885
1886 if (!hist_entry__has_pairs(pos)) {
1887 pair = hists__add_dummy_entry(leader, pos);
1888 if (pair == NULL)
1889 return -1;
Namhyung Kim5fa90412012-11-29 15:38:34 +09001890 hist_entry__add_pair(pos, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001891 }
1892 }
1893
1894 return 0;
1895}
Namhyung Kimf2148332014-01-14 11:52:48 +09001896
Andi Kleen578499982015-07-18 08:24:49 -07001897void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
1898 struct perf_sample *sample, bool nonany_branch_mode)
1899{
1900 struct branch_info *bi;
1901
1902 /* If we have branch cycles always annotate them. */
1903 if (bs && bs->nr && bs->entries[0].flags.cycles) {
1904 int i;
1905
1906 bi = sample__resolve_bstack(sample, al);
1907 if (bi) {
1908 struct addr_map_symbol *prev = NULL;
1909
1910 /*
1911 * Ignore errors, still want to process the
1912 * other entries.
1913 *
1914 * For non standard branch modes always
1915 * force no IPC (prev == NULL)
1916 *
1917 * Note that perf stores branches reversed from
1918 * program order!
1919 */
1920 for (i = bs->nr - 1; i >= 0; i--) {
1921 addr_map_symbol__account_cycles(&bi[i].from,
1922 nonany_branch_mode ? NULL : prev,
1923 bi[i].flags.cycles);
1924 prev = &bi[i].to;
1925 }
1926 free(bi);
1927 }
1928 }
1929}
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03001930
1931size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp)
1932{
1933 struct perf_evsel *pos;
1934 size_t ret = 0;
1935
1936 evlist__for_each(evlist, pos) {
1937 ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
1938 ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
1939 }
1940
1941 return ret;
1942}
1943
1944
Namhyung Kimf2148332014-01-14 11:52:48 +09001945u64 hists__total_period(struct hists *hists)
1946{
1947 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
1948 hists->stats.total_period;
1949}
Namhyung Kim33db4562014-02-07 12:06:07 +09001950
1951int parse_filter_percentage(const struct option *opt __maybe_unused,
1952 const char *arg, int unset __maybe_unused)
1953{
1954 if (!strcmp(arg, "relative"))
1955 symbol_conf.filter_relative = true;
1956 else if (!strcmp(arg, "absolute"))
1957 symbol_conf.filter_relative = false;
1958 else
1959 return -1;
1960
1961 return 0;
1962}
Namhyung Kim0b93da12014-01-14 12:02:15 +09001963
1964int perf_hist_config(const char *var, const char *value)
1965{
1966 if (!strcmp(var, "hist.percentage"))
1967 return parse_filter_percentage(NULL, value, 0);
1968
1969 return 0;
1970}
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03001971
Jiri Olsa5b658552016-01-18 10:24:22 +01001972int __hists__init(struct hists *hists, struct perf_hpp_list *hpp_list)
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03001973{
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03001974 memset(hists, 0, sizeof(*hists));
1975 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
1976 hists->entries_in = &hists->entries_in_array[0];
1977 hists->entries_collapsed = RB_ROOT;
1978 hists->entries = RB_ROOT;
1979 pthread_mutex_init(&hists->lock, NULL);
Kan Liang21394d92015-09-04 10:45:44 -04001980 hists->socket_filter = -1;
Jiri Olsa5b658552016-01-18 10:24:22 +01001981 hists->hpp_list = hpp_list;
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03001982 return 0;
1983}
1984
Namhyung Kim61fa0e92015-12-10 16:53:20 +09001985static void hists__delete_remaining_entries(struct rb_root *root)
1986{
1987 struct rb_node *node;
1988 struct hist_entry *he;
1989
1990 while (!RB_EMPTY_ROOT(root)) {
1991 node = rb_first(root);
1992 rb_erase(node, root);
1993
1994 he = rb_entry(node, struct hist_entry, rb_node_in);
1995 hist_entry__delete(he);
1996 }
1997}
1998
1999static void hists__delete_all_entries(struct hists *hists)
2000{
2001 hists__delete_entries(hists);
2002 hists__delete_remaining_entries(&hists->entries_in_array[0]);
2003 hists__delete_remaining_entries(&hists->entries_in_array[1]);
2004 hists__delete_remaining_entries(&hists->entries_collapsed);
2005}
2006
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002007static void hists_evsel__exit(struct perf_evsel *evsel)
2008{
2009 struct hists *hists = evsel__hists(evsel);
2010
Namhyung Kim61fa0e92015-12-10 16:53:20 +09002011 hists__delete_all_entries(hists);
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002012}
2013
Namhyung Kimfc284be2016-01-07 10:14:10 +01002014static int hists_evsel__init(struct perf_evsel *evsel)
2015{
2016 struct hists *hists = evsel__hists(evsel);
2017
Jiri Olsa5b658552016-01-18 10:24:22 +01002018 __hists__init(hists, &perf_hpp_list);
Namhyung Kimfc284be2016-01-07 10:14:10 +01002019 return 0;
2020}
2021
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002022/*
2023 * XXX We probably need a hists_evsel__exit() to free the hist_entries
2024 * stored in the rbtree...
2025 */
2026
2027int hists__init(void)
2028{
2029 int err = perf_evsel__object_config(sizeof(struct hists_evsel),
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002030 hists_evsel__init,
2031 hists_evsel__exit);
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002032 if (err)
2033 fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);
2034
2035 return err;
2036}
Jiri Olsa94b3dc32016-01-18 10:24:13 +01002037
2038void perf_hpp_list__init(struct perf_hpp_list *list)
2039{
2040 INIT_LIST_HEAD(&list->fields);
2041 INIT_LIST_HEAD(&list->sorts);
2042}