blob: c226303e3da045743fe676c7c2ffc0ff13eac674 [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);
134 }
Jiri Olsab34b3bf2015-10-05 20:06:08 +0200135
136 if (h->mem_info->iaddr.sym) {
137 symlen = (int)h->mem_info->iaddr.sym->namelen + 4
138 + unresolved_col_width + 2;
139 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
140 symlen);
141 } else {
142 symlen = unresolved_col_width + 4 + 2;
143 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
144 symlen);
145 }
146
Stephane Eranian98a3b322013-01-24 16:10:35 +0100147 if (h->mem_info->daddr.map) {
148 symlen = dso__name_len(h->mem_info->daddr.map->dso);
149 hists__new_col_len(hists, HISTC_MEM_DADDR_DSO,
150 symlen);
151 } else {
152 symlen = unresolved_col_width + 4 + 2;
153 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
154 }
155 } else {
156 symlen = unresolved_col_width + 4 + 2;
157 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen);
Jiri Olsab34b3bf2015-10-05 20:06:08 +0200158 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL, symlen);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100159 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
160 }
161
Arnaldo Carvalho de Meloa4978ec2015-09-09 12:14:00 -0300162 hists__new_col_len(hists, HISTC_CPU, 3);
Kan Liang2e7ea3a2015-09-04 10:45:43 -0400163 hists__new_col_len(hists, HISTC_SOCKET, 6);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100164 hists__new_col_len(hists, HISTC_MEM_LOCKED, 6);
165 hists__new_col_len(hists, HISTC_MEM_TLB, 22);
166 hists__new_col_len(hists, HISTC_MEM_SNOOP, 12);
167 hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3);
168 hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12);
169 hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12);
Andi Kleen475eeab2013-09-20 07:40:43 -0700170
Arnaldo Carvalho de Meloe8e6d372015-08-10 16:53:54 -0300171 if (h->srcline)
172 hists__new_col_len(hists, HISTC_SRCLINE, strlen(h->srcline));
173
Andi Kleen31191a82015-08-07 15:54:24 -0700174 if (h->srcfile)
175 hists__new_col_len(hists, HISTC_SRCFILE, strlen(h->srcfile));
176
Andi Kleen475eeab2013-09-20 07:40:43 -0700177 if (h->transaction)
178 hists__new_col_len(hists, HISTC_TRANSACTION,
179 hist_entry__transaction_len());
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300180}
181
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900182void hists__output_recalc_col_len(struct hists *hists, int max_rows)
183{
184 struct rb_node *next = rb_first(&hists->entries);
185 struct hist_entry *n;
186 int row = 0;
187
188 hists__reset_col_len(hists);
189
190 while (next && row++ < max_rows) {
191 n = rb_entry(next, struct hist_entry, rb_node);
192 if (!n->filtered)
193 hists__calc_col_len(hists, n);
194 next = rb_next(&n->rb_node);
195 }
196}
197
Namhyung Kimf39056f2014-01-14 14:25:37 +0900198static void he_stat__add_cpumode_period(struct he_stat *he_stat,
199 unsigned int cpumode, u64 period)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800200{
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300201 switch (cpumode) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800202 case PERF_RECORD_MISC_KERNEL:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900203 he_stat->period_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800204 break;
205 case PERF_RECORD_MISC_USER:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900206 he_stat->period_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800207 break;
208 case PERF_RECORD_MISC_GUEST_KERNEL:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900209 he_stat->period_guest_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800210 break;
211 case PERF_RECORD_MISC_GUEST_USER:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900212 he_stat->period_guest_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800213 break;
214 default:
215 break;
216 }
217}
218
Andi Kleen05484292013-01-24 16:10:29 +0100219static void he_stat__add_period(struct he_stat *he_stat, u64 period,
220 u64 weight)
Namhyung Kim139c0812012-10-04 21:49:43 +0900221{
Stephane Eranian98a3b322013-01-24 16:10:35 +0100222
Namhyung Kim139c0812012-10-04 21:49:43 +0900223 he_stat->period += period;
Andi Kleen05484292013-01-24 16:10:29 +0100224 he_stat->weight += weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900225 he_stat->nr_events += 1;
226}
227
228static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
229{
230 dest->period += src->period;
231 dest->period_sys += src->period_sys;
232 dest->period_us += src->period_us;
233 dest->period_guest_sys += src->period_guest_sys;
234 dest->period_guest_us += src->period_guest_us;
235 dest->nr_events += src->nr_events;
Andi Kleen05484292013-01-24 16:10:29 +0100236 dest->weight += src->weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900237}
238
Namhyung Kimf39056f2014-01-14 14:25:37 +0900239static void he_stat__decay(struct he_stat *he_stat)
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300240{
Namhyung Kimf39056f2014-01-14 14:25:37 +0900241 he_stat->period = (he_stat->period * 7) / 8;
242 he_stat->nr_events = (he_stat->nr_events * 7) / 8;
Andi Kleen05484292013-01-24 16:10:29 +0100243 /* XXX need decay for weight too? */
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300244}
245
246static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
247{
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900248 u64 prev_period = he->stat.period;
Namhyung Kim3186b682014-04-22 13:44:23 +0900249 u64 diff;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200250
251 if (prev_period == 0)
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300252 return true;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200253
Namhyung Kimf39056f2014-01-14 14:25:37 +0900254 he_stat__decay(&he->stat);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900255 if (symbol_conf.cumulate_callchain)
256 he_stat__decay(he->stat_acc);
Namhyung Kim42b276a2016-01-05 12:06:00 +0900257 decay_callchain(he->callchain);
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200258
Namhyung Kim3186b682014-04-22 13:44:23 +0900259 diff = prev_period - he->stat.period;
260
261 hists->stats.total_period -= diff;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200262 if (!he->filtered)
Namhyung Kim3186b682014-04-22 13:44:23 +0900263 hists->stats.total_non_filtered_period -= diff;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200264
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900265 return he->stat.period == 0;
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300266}
267
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300268static void hists__delete_entry(struct hists *hists, struct hist_entry *he)
269{
270 rb_erase(&he->rb_node, &hists->entries);
271
272 if (sort__need_collapse)
273 rb_erase(&he->rb_node_in, &hists->entries_collapsed);
Namhyung Kim61fa0e92015-12-10 16:53:20 +0900274 else
275 rb_erase(&he->rb_node_in, hists->entries_in);
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300276
277 --hists->nr_entries;
278 if (!he->filtered)
279 --hists->nr_non_filtered_entries;
280
281 hist_entry__delete(he);
282}
283
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900284void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300285{
286 struct rb_node *next = rb_first(&hists->entries);
287 struct hist_entry *n;
288
289 while (next) {
290 n = rb_entry(next, struct hist_entry, rb_node);
291 next = rb_next(&n->rb_node);
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200292 if (((zap_user && n->level == '.') ||
293 (zap_kernel && n->level != '.') ||
Arnaldo Carvalho de Melo4c47f4f2015-03-17 17:18:58 -0300294 hists__decay_entry(hists, n))) {
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300295 hists__delete_entry(hists, n);
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300296 }
297 }
298}
299
Namhyung Kim701937b2014-08-12 17:16:05 +0900300void hists__delete_entries(struct hists *hists)
301{
302 struct rb_node *next = rb_first(&hists->entries);
303 struct hist_entry *n;
304
305 while (next) {
306 n = rb_entry(next, struct hist_entry, rb_node);
307 next = rb_next(&n->rb_node);
308
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300309 hists__delete_entry(hists, n);
Namhyung Kim701937b2014-08-12 17:16:05 +0900310 }
311}
312
John Kacur3d1d07e2009-09-28 15:32:55 +0200313/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300314 * histogram, sorted on item, collects periods
John Kacur3d1d07e2009-09-28 15:32:55 +0200315 */
316
Namhyung Kima0b51af2012-09-11 13:34:27 +0900317static struct hist_entry *hist_entry__new(struct hist_entry *template,
318 bool sample_self)
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300319{
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900320 size_t callchain_size = 0;
321 struct hist_entry *he;
322
Namhyung Kim82aa0192014-12-22 13:44:14 +0900323 if (symbol_conf.use_callchain)
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900324 callchain_size = sizeof(struct callchain_root);
325
326 he = zalloc(sizeof(*he) + callchain_size);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300327
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200328 if (he != NULL) {
329 *he = *template;
Namhyung Kimc4b35352012-10-04 21:49:42 +0900330
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900331 if (symbol_conf.cumulate_callchain) {
332 he->stat_acc = malloc(sizeof(he->stat));
333 if (he->stat_acc == NULL) {
334 free(he);
335 return NULL;
336 }
337 memcpy(he->stat_acc, &he->stat, sizeof(he->stat));
Namhyung Kima0b51af2012-09-11 13:34:27 +0900338 if (!sample_self)
339 memset(&he->stat, 0, sizeof(he->stat));
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900340 }
341
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300342 map__get(he->ms.map);
Stephane Eranian3cf0cb12013-01-14 15:02:45 +0100343
344 if (he->branch_info) {
Namhyung Kim26353a62013-04-01 20:35:17 +0900345 /*
346 * This branch info is (a part of) allocated from
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -0300347 * sample__resolve_bstack() and will be freed after
Namhyung Kim26353a62013-04-01 20:35:17 +0900348 * adding new entries. So we need to save a copy.
349 */
350 he->branch_info = malloc(sizeof(*he->branch_info));
351 if (he->branch_info == NULL) {
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300352 map__zput(he->ms.map);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900353 free(he->stat_acc);
Namhyung Kim26353a62013-04-01 20:35:17 +0900354 free(he);
355 return NULL;
356 }
357
358 memcpy(he->branch_info, template->branch_info,
359 sizeof(*he->branch_info));
360
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300361 map__get(he->branch_info->from.map);
362 map__get(he->branch_info->to.map);
Stephane Eranian3cf0cb12013-01-14 15:02:45 +0100363 }
364
Stephane Eranian98a3b322013-01-24 16:10:35 +0100365 if (he->mem_info) {
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300366 map__get(he->mem_info->iaddr.map);
367 map__get(he->mem_info->daddr.map);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100368 }
369
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300370 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200371 callchain_init(he->callchain);
Arnaldo Carvalho de Melob821c732012-10-25 14:42:45 -0200372
Namhyung Kim72392832015-12-24 11:16:17 +0900373 if (he->raw_data) {
374 he->raw_data = memdup(he->raw_data, he->raw_size);
375
376 if (he->raw_data == NULL) {
377 map__put(he->ms.map);
378 if (he->branch_info) {
379 map__put(he->branch_info->from.map);
380 map__put(he->branch_info->to.map);
381 free(he->branch_info);
382 }
383 if (he->mem_info) {
384 map__put(he->mem_info->iaddr.map);
385 map__put(he->mem_info->daddr.map);
386 }
387 free(he->stat_acc);
388 free(he);
389 return NULL;
390 }
391 }
Arnaldo Carvalho de Melob821c732012-10-25 14:42:45 -0200392 INIT_LIST_HEAD(&he->pairs.node);
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300393 thread__get(he->thread);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300394 }
395
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200396 return he;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300397}
398
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300399static u8 symbol__parent_filter(const struct symbol *parent)
400{
401 if (symbol_conf.exclude_other && parent == NULL)
402 return 1 << HIST_FILTER__PARENT;
403 return 0;
404}
405
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300406static struct hist_entry *hists__findnew_entry(struct hists *hists,
407 struct hist_entry *entry,
408 struct addr_location *al,
409 bool sample_self)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300410{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300411 struct rb_node **p;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300412 struct rb_node *parent = NULL;
413 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -0700414 int64_t cmp;
Namhyung Kimf1cbf782013-12-18 14:21:11 +0900415 u64 period = entry->stat.period;
416 u64 weight = entry->stat.weight;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300417
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300418 p = &hists->entries_in->rb_node;
419
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300420 while (*p != NULL) {
421 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300422 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300423
Namhyung Kim9afcf932012-12-10 17:29:54 +0900424 /*
425 * Make sure that it receives arguments in a same order as
426 * hist_entry__collapse() so that we can use an appropriate
427 * function when searching an entry regardless which sort
428 * keys were used.
429 */
430 cmp = hist_entry__cmp(he, entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300431
432 if (!cmp) {
Namhyung Kima0b51af2012-09-11 13:34:27 +0900433 if (sample_self)
434 he_stat__add_period(&he->stat, period, weight);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900435 if (symbol_conf.cumulate_callchain)
436 he_stat__add_period(he->stat_acc, period, weight);
David Miller63fa4712012-03-27 03:14:18 -0400437
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900438 /*
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -0300439 * This mem info was allocated from sample__resolve_mem
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900440 * and will not be used anymore.
441 */
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300442 zfree(&entry->mem_info);
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900443
David Miller63fa4712012-03-27 03:14:18 -0400444 /* If the map of an existing hist_entry has
445 * become out-of-date due to an exec() or
446 * similar, update it. Otherwise we will
447 * mis-adjust symbol addresses when computing
448 * the history counter to increment.
449 */
450 if (he->ms.map != entry->ms.map) {
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300451 map__put(he->ms.map);
452 he->ms.map = map__get(entry->ms.map);
David Miller63fa4712012-03-27 03:14:18 -0400453 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300454 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300455 }
456
457 if (cmp < 0)
458 p = &(*p)->rb_left;
459 else
460 p = &(*p)->rb_right;
461 }
462
Namhyung Kima0b51af2012-09-11 13:34:27 +0900463 he = hist_entry__new(entry, sample_self);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300464 if (!he)
Namhyung Kim27a0dcb2013-05-14 11:09:02 +0900465 return NULL;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300466
Namhyung Kim590cd342014-12-22 13:44:09 +0900467 hists->nr_entries++;
468
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300469 rb_link_node(&he->rb_node_in, parent, p);
470 rb_insert_color(&he->rb_node_in, hists->entries_in);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300471out:
Namhyung Kima0b51af2012-09-11 13:34:27 +0900472 if (sample_self)
473 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900474 if (symbol_conf.cumulate_callchain)
475 he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300476 return he;
477}
478
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300479struct hist_entry *__hists__add_entry(struct hists *hists,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100480 struct addr_location *al,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900481 struct symbol *sym_parent,
482 struct branch_info *bi,
483 struct mem_info *mi,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900484 struct perf_sample *sample,
Namhyung Kima0b51af2012-09-11 13:34:27 +0900485 bool sample_self)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100486{
487 struct hist_entry entry = {
488 .thread = al->thread,
Namhyung Kim4dfced32013-09-13 16:28:57 +0900489 .comm = thread__comm(al->thread),
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100490 .ms = {
491 .map = al->map,
492 .sym = al->sym,
493 },
Kan Liang0c4c4de2015-09-04 10:45:42 -0400494 .socket = al->socket,
Don Zickus7365be52014-05-27 12:28:05 -0400495 .cpu = al->cpu,
496 .cpumode = al->cpumode,
497 .ip = al->addr,
498 .level = al->level,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900499 .stat = {
Namhyung Kimc4b35352012-10-04 21:49:42 +0900500 .nr_events = 1,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900501 .period = sample->period,
502 .weight = sample->weight,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900503 },
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100504 .parent = sym_parent,
Namhyung Kim2c86c7c2014-03-17 18:18:54 -0300505 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300506 .hists = hists,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900507 .branch_info = bi,
508 .mem_info = mi,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900509 .transaction = sample->transaction,
Namhyung Kim72392832015-12-24 11:16:17 +0900510 .raw_data = sample->raw_data,
511 .raw_size = sample->raw_size,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100512 };
513
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300514 return hists__findnew_entry(hists, &entry, al, sample_self);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100515}
516
Namhyung Kim69bcb012013-10-30 09:40:34 +0900517static int
518iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
519 struct addr_location *al __maybe_unused)
520{
521 return 0;
522}
523
524static int
525iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
526 struct addr_location *al __maybe_unused)
527{
528 return 0;
529}
530
531static int
532iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
533{
534 struct perf_sample *sample = iter->sample;
535 struct mem_info *mi;
536
537 mi = sample__resolve_mem(sample, al);
538 if (mi == NULL)
539 return -ENOMEM;
540
541 iter->priv = mi;
542 return 0;
543}
544
545static int
546iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
547{
548 u64 cost;
549 struct mem_info *mi = iter->priv;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300550 struct hists *hists = evsel__hists(iter->evsel);
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900551 struct perf_sample *sample = iter->sample;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900552 struct hist_entry *he;
553
554 if (mi == NULL)
555 return -EINVAL;
556
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900557 cost = sample->weight;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900558 if (!cost)
559 cost = 1;
560
561 /*
562 * must pass period=weight in order to get the correct
563 * sorting from hists__collapse_resort() which is solely
564 * based on periods. We want sorting be done on nr_events * weight
565 * and this is indirectly achieved by passing period=weight here
566 * and the he_stat__add_period() function.
567 */
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900568 sample->period = cost;
569
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300570 he = __hists__add_entry(hists, al, iter->parent, NULL, mi,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900571 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900572 if (!he)
573 return -ENOMEM;
574
575 iter->he = he;
576 return 0;
577}
578
579static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900580iter_finish_mem_entry(struct hist_entry_iter *iter,
581 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900582{
583 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300584 struct hists *hists = evsel__hists(evsel);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900585 struct hist_entry *he = iter->he;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900586 int err = -EINVAL;
587
588 if (he == NULL)
589 goto out;
590
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300591 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900592
593 err = hist_entry__append_callchain(he, iter->sample);
594
595out:
596 /*
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300597 * We don't need to free iter->priv (mem_info) here since the mem info
598 * was either already freed in hists__findnew_entry() or passed to a
599 * new hist entry by hist_entry__new().
Namhyung Kim69bcb012013-10-30 09:40:34 +0900600 */
601 iter->priv = NULL;
602
603 iter->he = NULL;
604 return err;
605}
606
607static int
608iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
609{
610 struct branch_info *bi;
611 struct perf_sample *sample = iter->sample;
612
613 bi = sample__resolve_bstack(sample, al);
614 if (!bi)
615 return -ENOMEM;
616
617 iter->curr = 0;
618 iter->total = sample->branch_stack->nr;
619
620 iter->priv = bi;
621 return 0;
622}
623
624static int
625iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused,
626 struct addr_location *al __maybe_unused)
627{
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900628 /* to avoid calling callback function */
629 iter->he = NULL;
630
Namhyung Kim69bcb012013-10-30 09:40:34 +0900631 return 0;
632}
633
634static int
635iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
636{
637 struct branch_info *bi = iter->priv;
638 int i = iter->curr;
639
640 if (bi == NULL)
641 return 0;
642
643 if (iter->curr >= iter->total)
644 return 0;
645
646 al->map = bi[i].to.map;
647 al->sym = bi[i].to.sym;
648 al->addr = bi[i].to.addr;
649 return 1;
650}
651
652static int
653iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
654{
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900655 struct branch_info *bi;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900656 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300657 struct hists *hists = evsel__hists(evsel);
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900658 struct perf_sample *sample = iter->sample;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900659 struct hist_entry *he = NULL;
660 int i = iter->curr;
661 int err = 0;
662
663 bi = iter->priv;
664
665 if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym))
666 goto out;
667
668 /*
669 * The report shows the percentage of total branches captured
670 * and not events sampled. Thus we use a pseudo period of 1.
671 */
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900672 sample->period = 1;
673 sample->weight = bi->flags.cycles ? bi->flags.cycles : 1;
674
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300675 he = __hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900676 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900677 if (he == NULL)
678 return -ENOMEM;
679
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300680 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900681
682out:
683 iter->he = he;
684 iter->curr++;
685 return err;
686}
687
688static int
689iter_finish_branch_entry(struct hist_entry_iter *iter,
690 struct addr_location *al __maybe_unused)
691{
692 zfree(&iter->priv);
693 iter->he = NULL;
694
695 return iter->curr >= iter->total ? 0 : -1;
696}
697
698static int
699iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
700 struct addr_location *al __maybe_unused)
701{
702 return 0;
703}
704
705static int
706iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
707{
708 struct perf_evsel *evsel = iter->evsel;
709 struct perf_sample *sample = iter->sample;
710 struct hist_entry *he;
711
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300712 he = __hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900713 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900714 if (he == NULL)
715 return -ENOMEM;
716
717 iter->he = he;
718 return 0;
719}
720
721static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900722iter_finish_normal_entry(struct hist_entry_iter *iter,
723 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900724{
Namhyung Kim69bcb012013-10-30 09:40:34 +0900725 struct hist_entry *he = iter->he;
726 struct perf_evsel *evsel = iter->evsel;
727 struct perf_sample *sample = iter->sample;
728
729 if (he == NULL)
730 return 0;
731
732 iter->he = NULL;
733
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300734 hists__inc_nr_samples(evsel__hists(evsel), he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900735
736 return hist_entry__append_callchain(he, sample);
737}
738
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900739static int
Adrian Hunter96b40f32015-09-25 16:15:47 +0300740iter_prepare_cumulative_entry(struct hist_entry_iter *iter,
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900741 struct addr_location *al __maybe_unused)
742{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900743 struct hist_entry **he_cache;
744
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900745 callchain_cursor_commit(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900746
747 /*
748 * This is for detecting cycles or recursions so that they're
749 * cumulated only one time to prevent entries more than 100%
750 * overhead.
751 */
Adrian Hunter96b40f32015-09-25 16:15:47 +0300752 he_cache = malloc(sizeof(*he_cache) * (iter->max_stack + 1));
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900753 if (he_cache == NULL)
754 return -ENOMEM;
755
756 iter->priv = he_cache;
757 iter->curr = 0;
758
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900759 return 0;
760}
761
762static int
763iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
764 struct addr_location *al)
765{
766 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300767 struct hists *hists = evsel__hists(evsel);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900768 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900769 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900770 struct hist_entry *he;
771 int err = 0;
772
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300773 he = __hists__add_entry(hists, al, iter->parent, NULL, NULL,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900774 sample, true);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900775 if (he == NULL)
776 return -ENOMEM;
777
778 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900779 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900780
Namhyung Kim82aa0192014-12-22 13:44:14 +0900781 hist_entry__append_callchain(he, sample);
Namhyung Kimbe7f8552013-12-26 17:44:10 +0900782
783 /*
784 * We need to re-initialize the cursor since callchain_append()
785 * advanced the cursor to the end.
786 */
787 callchain_cursor_commit(&callchain_cursor);
788
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300789 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900790
791 return err;
792}
793
794static int
795iter_next_cumulative_entry(struct hist_entry_iter *iter,
796 struct addr_location *al)
797{
798 struct callchain_cursor_node *node;
799
800 node = callchain_cursor_current(&callchain_cursor);
801 if (node == NULL)
802 return 0;
803
Namhyung Kimc7405d82013-10-31 13:58:30 +0900804 return fill_callchain_info(al, node, iter->hide_unresolved);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900805}
806
807static int
808iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
809 struct addr_location *al)
810{
811 struct perf_evsel *evsel = iter->evsel;
812 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900813 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900814 struct hist_entry *he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900815 struct hist_entry he_tmp = {
Arnaldo Carvalho de Melo5cef8972015-08-10 15:45:55 -0300816 .hists = evsel__hists(evsel),
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900817 .cpu = al->cpu,
818 .thread = al->thread,
819 .comm = thread__comm(al->thread),
820 .ip = al->addr,
821 .ms = {
822 .map = al->map,
823 .sym = al->sym,
824 },
825 .parent = iter->parent,
Namhyung Kim72392832015-12-24 11:16:17 +0900826 .raw_data = sample->raw_data,
827 .raw_size = sample->raw_size,
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900828 };
829 int i;
Namhyung Kimbe7f8552013-12-26 17:44:10 +0900830 struct callchain_cursor cursor;
831
832 callchain_cursor_snapshot(&cursor, &callchain_cursor);
833
834 callchain_cursor_advance(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900835
836 /*
837 * Check if there's duplicate entries in the callchain.
838 * It's possible that it has cycles or recursive calls.
839 */
840 for (i = 0; i < iter->curr; i++) {
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900841 if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
842 /* to avoid calling callback function */
843 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900844 return 0;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900845 }
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900846 }
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900847
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300848 he = __hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900849 sample, false);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900850 if (he == NULL)
851 return -ENOMEM;
852
853 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900854 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900855
Namhyung Kim82aa0192014-12-22 13:44:14 +0900856 if (symbol_conf.use_callchain)
857 callchain_append(he->callchain, &cursor, sample->period);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900858 return 0;
859}
860
861static int
862iter_finish_cumulative_entry(struct hist_entry_iter *iter,
863 struct addr_location *al __maybe_unused)
864{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900865 zfree(&iter->priv);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900866 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900867
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900868 return 0;
869}
870
Namhyung Kim69bcb012013-10-30 09:40:34 +0900871const struct hist_iter_ops hist_iter_mem = {
872 .prepare_entry = iter_prepare_mem_entry,
873 .add_single_entry = iter_add_single_mem_entry,
874 .next_entry = iter_next_nop_entry,
875 .add_next_entry = iter_add_next_nop_entry,
876 .finish_entry = iter_finish_mem_entry,
877};
878
879const struct hist_iter_ops hist_iter_branch = {
880 .prepare_entry = iter_prepare_branch_entry,
881 .add_single_entry = iter_add_single_branch_entry,
882 .next_entry = iter_next_branch_entry,
883 .add_next_entry = iter_add_next_branch_entry,
884 .finish_entry = iter_finish_branch_entry,
885};
886
887const struct hist_iter_ops hist_iter_normal = {
888 .prepare_entry = iter_prepare_normal_entry,
889 .add_single_entry = iter_add_single_normal_entry,
890 .next_entry = iter_next_nop_entry,
891 .add_next_entry = iter_add_next_nop_entry,
892 .finish_entry = iter_finish_normal_entry,
893};
894
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900895const struct hist_iter_ops hist_iter_cumulative = {
896 .prepare_entry = iter_prepare_cumulative_entry,
897 .add_single_entry = iter_add_single_cumulative_entry,
898 .next_entry = iter_next_cumulative_entry,
899 .add_next_entry = iter_add_next_cumulative_entry,
900 .finish_entry = iter_finish_cumulative_entry,
901};
902
Namhyung Kim69bcb012013-10-30 09:40:34 +0900903int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900904 int max_stack_depth, void *arg)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900905{
906 int err, err2;
907
Namhyung Kim063bd932015-05-19 17:04:10 +0900908 err = sample__resolve_callchain(iter->sample, &iter->parent,
909 iter->evsel, al, max_stack_depth);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900910 if (err)
911 return err;
912
Adrian Hunter96b40f32015-09-25 16:15:47 +0300913 iter->max_stack = max_stack_depth;
914
Namhyung Kim69bcb012013-10-30 09:40:34 +0900915 err = iter->ops->prepare_entry(iter, al);
916 if (err)
917 goto out;
918
919 err = iter->ops->add_single_entry(iter, al);
920 if (err)
921 goto out;
922
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900923 if (iter->he && iter->add_entry_cb) {
924 err = iter->add_entry_cb(iter, al, true, arg);
925 if (err)
926 goto out;
927 }
928
Namhyung Kim69bcb012013-10-30 09:40:34 +0900929 while (iter->ops->next_entry(iter, al)) {
930 err = iter->ops->add_next_entry(iter, al);
931 if (err)
932 break;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900933
934 if (iter->he && iter->add_entry_cb) {
935 err = iter->add_entry_cb(iter, al, false, arg);
936 if (err)
937 goto out;
938 }
Namhyung Kim69bcb012013-10-30 09:40:34 +0900939 }
940
941out:
942 err2 = iter->ops->finish_entry(iter, al);
943 if (!err)
944 err = err2;
945
946 return err;
947}
948
John Kacur3d1d07e2009-09-28 15:32:55 +0200949int64_t
950hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
951{
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900952 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +0200953 int64_t cmp = 0;
954
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900955 perf_hpp__for_each_sort_list(fmt) {
Namhyung Kim87bbdf72015-01-08 09:45:46 +0900956 cmp = fmt->cmp(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200957 if (cmp)
958 break;
959 }
960
961 return cmp;
962}
963
964int64_t
965hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
966{
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900967 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +0200968 int64_t cmp = 0;
969
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900970 perf_hpp__for_each_sort_list(fmt) {
Namhyung Kim87bbdf72015-01-08 09:45:46 +0900971 cmp = fmt->collapse(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200972 if (cmp)
973 break;
974 }
975
976 return cmp;
977}
978
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -0300979void hist_entry__delete(struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +0200980{
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300981 thread__zput(he->thread);
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300982 map__zput(he->ms.map);
983
984 if (he->branch_info) {
985 map__zput(he->branch_info->from.map);
986 map__zput(he->branch_info->to.map);
987 zfree(&he->branch_info);
988 }
989
990 if (he->mem_info) {
991 map__zput(he->mem_info->iaddr.map);
992 map__zput(he->mem_info->daddr.map);
993 zfree(&he->mem_info);
994 }
995
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900996 zfree(&he->stat_acc);
Namhyung Kimf048d542013-09-11 14:09:28 +0900997 free_srcline(he->srcline);
Andi Kleen31191a82015-08-07 15:54:24 -0700998 if (he->srcfile && he->srcfile[0])
999 free(he->srcfile);
Namhyung Kimd1149602014-12-30 14:38:13 +09001000 free_callchain(he->callchain);
Namhyung Kim60517d22015-12-23 02:07:03 +09001001 free(he->trace_output);
Namhyung Kim72392832015-12-24 11:16:17 +09001002 free(he->raw_data);
John Kacur3d1d07e2009-09-28 15:32:55 +02001003 free(he);
1004}
1005
1006/*
1007 * collapse the histogram
1008 */
1009
Namhyung Kimfc284be2016-01-07 10:14:10 +01001010bool hists__collapse_insert_entry(struct hists *hists __maybe_unused,
1011 struct rb_root *root, struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +02001012{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -02001013 struct rb_node **p = &root->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +02001014 struct rb_node *parent = NULL;
1015 struct hist_entry *iter;
1016 int64_t cmp;
1017
1018 while (*p != NULL) {
1019 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001020 iter = rb_entry(parent, struct hist_entry, rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +02001021
1022 cmp = hist_entry__collapse(iter, he);
1023
1024 if (!cmp) {
Namhyung Kim139c0812012-10-04 21:49:43 +09001025 he_stat__add_stat(&iter->stat, &he->stat);
Namhyung Kimf8be1c82012-09-11 13:15:07 +09001026 if (symbol_conf.cumulate_callchain)
1027 he_stat__add_stat(iter->stat_acc, he->stat_acc);
Namhyung Kim9ec60972012-09-26 16:47:28 +09001028
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +01001029 if (symbol_conf.use_callchain) {
Namhyung Kim47260642012-05-31 14:43:26 +09001030 callchain_cursor_reset(&callchain_cursor);
1031 callchain_merge(&callchain_cursor,
1032 iter->callchain,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +01001033 he->callchain);
1034 }
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -03001035 hist_entry__delete(he);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -03001036 return false;
John Kacur3d1d07e2009-09-28 15:32:55 +02001037 }
1038
1039 if (cmp < 0)
1040 p = &(*p)->rb_left;
1041 else
1042 p = &(*p)->rb_right;
1043 }
Namhyung Kim740b97f2014-12-22 13:44:10 +09001044 hists->nr_entries++;
John Kacur3d1d07e2009-09-28 15:32:55 +02001045
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001046 rb_link_node(&he->rb_node_in, parent, p);
1047 rb_insert_color(&he->rb_node_in, root);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -03001048 return true;
John Kacur3d1d07e2009-09-28 15:32:55 +02001049}
1050
Namhyung Kimfc284be2016-01-07 10:14:10 +01001051struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001052{
1053 struct rb_root *root;
1054
1055 pthread_mutex_lock(&hists->lock);
1056
1057 root = hists->entries_in;
1058 if (++hists->entries_in > &hists->entries_in_array[1])
1059 hists->entries_in = &hists->entries_in_array[0];
1060
1061 pthread_mutex_unlock(&hists->lock);
1062
1063 return root;
1064}
1065
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001066static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
1067{
1068 hists__filter_entry_by_dso(hists, he);
1069 hists__filter_entry_by_thread(hists, he);
Namhyung Kime94d53e2012-03-16 17:50:51 +09001070 hists__filter_entry_by_symbol(hists, he);
Kan Liang21394d92015-09-04 10:45:44 -04001071 hists__filter_entry_by_socket(hists, he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001072}
1073
Namhyung Kimc1fb5652013-10-11 14:15:38 +09001074void hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001075{
1076 struct rb_root *root;
1077 struct rb_node *next;
1078 struct hist_entry *n;
1079
Namhyung Kim3a5714f2013-05-14 11:09:01 +09001080 if (!sort__need_collapse)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001081 return;
1082
Namhyung Kim740b97f2014-12-22 13:44:10 +09001083 hists->nr_entries = 0;
1084
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001085 root = hists__get_rotate_entries_in(hists);
Namhyung Kim740b97f2014-12-22 13:44:10 +09001086
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001087 next = rb_first(root);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001088
1089 while (next) {
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001090 if (session_done())
1091 break;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001092 n = rb_entry(next, struct hist_entry, rb_node_in);
1093 next = rb_next(&n->rb_node_in);
1094
1095 rb_erase(&n->rb_node_in, root);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001096 if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
1097 /*
1098 * If it wasn't combined with one of the entries already
1099 * collapsed, we need to apply the filters that may have
1100 * been set by, say, the hist_browser.
1101 */
1102 hists__apply_filters(hists, n);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001103 }
Namhyung Kimc1fb5652013-10-11 14:15:38 +09001104 if (prog)
1105 ui_progress__update(prog, 1);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001106 }
1107}
1108
Namhyung Kim043ca3892014-03-03 14:18:00 +09001109static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001110{
Namhyung Kim043ca3892014-03-03 14:18:00 +09001111 struct perf_hpp_fmt *fmt;
1112 int64_t cmp = 0;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001113
Namhyung Kim26d8b332014-03-03 16:16:20 +09001114 perf_hpp__for_each_sort_list(fmt) {
Namhyung Kim361459f2015-12-23 02:07:08 +09001115 if (perf_hpp__should_skip(fmt, a->hists))
Namhyung Kime67d49a2014-03-18 13:00:59 +09001116 continue;
1117
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001118 cmp = fmt->sort(fmt, a, b);
Namhyung Kim043ca3892014-03-03 14:18:00 +09001119 if (cmp)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001120 break;
1121 }
1122
Namhyung Kim043ca3892014-03-03 14:18:00 +09001123 return cmp;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001124}
1125
Namhyung Kim9283ba92014-04-24 16:37:26 +09001126static void hists__reset_filter_stats(struct hists *hists)
1127{
1128 hists->nr_non_filtered_entries = 0;
1129 hists->stats.total_non_filtered_period = 0;
1130}
1131
1132void hists__reset_stats(struct hists *hists)
1133{
1134 hists->nr_entries = 0;
1135 hists->stats.total_period = 0;
1136
1137 hists__reset_filter_stats(hists);
1138}
1139
1140static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
1141{
1142 hists->nr_non_filtered_entries++;
1143 hists->stats.total_non_filtered_period += h->stat.period;
1144}
1145
1146void hists__inc_stats(struct hists *hists, struct hist_entry *h)
1147{
1148 if (!h->filtered)
1149 hists__inc_filter_stats(hists, h);
1150
1151 hists->nr_entries++;
1152 hists->stats.total_period += h->stat.period;
1153}
1154
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001155static void __hists__insert_output_entry(struct rb_root *entries,
1156 struct hist_entry *he,
Kan Liangf9db0d02015-08-11 06:30:48 -04001157 u64 min_callchain_hits,
1158 bool use_callchain)
John Kacur3d1d07e2009-09-28 15:32:55 +02001159{
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001160 struct rb_node **p = &entries->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +02001161 struct rb_node *parent = NULL;
1162 struct hist_entry *iter;
1163
Kan Liangf9db0d02015-08-11 06:30:48 -04001164 if (use_callchain)
Arnaldo Carvalho de Melob9fb9302010-04-02 09:50:42 -03001165 callchain_param.sort(&he->sorted_chain, he->callchain,
John Kacur3d1d07e2009-09-28 15:32:55 +02001166 min_callchain_hits, &callchain_param);
1167
1168 while (*p != NULL) {
1169 parent = *p;
1170 iter = rb_entry(parent, struct hist_entry, rb_node);
1171
Namhyung Kim043ca3892014-03-03 14:18:00 +09001172 if (hist_entry__sort(he, iter) > 0)
John Kacur3d1d07e2009-09-28 15:32:55 +02001173 p = &(*p)->rb_left;
1174 else
1175 p = &(*p)->rb_right;
1176 }
1177
1178 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001179 rb_insert_color(&he->rb_node, entries);
John Kacur3d1d07e2009-09-28 15:32:55 +02001180}
1181
Namhyung Kim740b97f2014-12-22 13:44:10 +09001182void hists__output_resort(struct hists *hists, struct ui_progress *prog)
John Kacur3d1d07e2009-09-28 15:32:55 +02001183{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001184 struct rb_root *root;
John Kacur3d1d07e2009-09-28 15:32:55 +02001185 struct rb_node *next;
1186 struct hist_entry *n;
John Kacur3d1d07e2009-09-28 15:32:55 +02001187 u64 min_callchain_hits;
Kan Liangf9db0d02015-08-11 06:30:48 -04001188 struct perf_evsel *evsel = hists_to_evsel(hists);
Kan Liang9e207dd2015-08-11 06:30:49 -04001189 bool use_callchain;
1190
Namhyung Kim208e7602015-09-30 13:34:00 +09001191 if (evsel && symbol_conf.use_callchain && !symbol_conf.show_ref_callgraph)
Kan Liang9e207dd2015-08-11 06:30:49 -04001192 use_callchain = evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN;
1193 else
1194 use_callchain = symbol_conf.use_callchain;
John Kacur3d1d07e2009-09-28 15:32:55 +02001195
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001196 min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
John Kacur3d1d07e2009-09-28 15:32:55 +02001197
Namhyung Kim3a5714f2013-05-14 11:09:01 +09001198 if (sort__need_collapse)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001199 root = &hists->entries_collapsed;
1200 else
1201 root = hists->entries_in;
1202
1203 next = rb_first(root);
1204 hists->entries = RB_ROOT;
John Kacur3d1d07e2009-09-28 15:32:55 +02001205
Namhyung Kim9283ba92014-04-24 16:37:26 +09001206 hists__reset_stats(hists);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001207 hists__reset_col_len(hists);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -03001208
John Kacur3d1d07e2009-09-28 15:32:55 +02001209 while (next) {
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001210 n = rb_entry(next, struct hist_entry, rb_node_in);
1211 next = rb_next(&n->rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +02001212
Kan Liangf9db0d02015-08-11 06:30:48 -04001213 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits, use_callchain);
Namhyung Kim62638352014-04-24 16:21:46 +09001214 hists__inc_stats(hists, n);
Namhyung Kimae993ef2014-04-24 16:25:19 +09001215
1216 if (!n->filtered)
1217 hists__calc_col_len(hists, n);
Namhyung Kim740b97f2014-12-22 13:44:10 +09001218
1219 if (prog)
1220 ui_progress__update(prog, 1);
John Kacur3d1d07e2009-09-28 15:32:55 +02001221 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001222}
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -02001223
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001224static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001225 enum hist_filter filter)
1226{
1227 h->filtered &= ~(1 << filter);
1228 if (h->filtered)
1229 return;
1230
Namhyung Kim87e90f42014-04-24 16:44:16 +09001231 /* force fold unfiltered entry for simplicity */
Namhyung Kim3698dab2015-05-05 23:55:46 +09001232 h->unfolded = false;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -03001233 h->row_offset = 0;
He Kuanga8cd1f42015-03-11 20:36:03 +08001234 h->nr_rows = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001235
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001236 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001237
Namhyung Kim9283ba92014-04-24 16:37:26 +09001238 hists__inc_filter_stats(hists, h);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001239 hists__calc_col_len(hists, h);
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001240}
1241
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001242
1243static bool hists__filter_entry_by_dso(struct hists *hists,
1244 struct hist_entry *he)
1245{
1246 if (hists->dso_filter != NULL &&
1247 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
1248 he->filtered |= (1 << HIST_FILTER__DSO);
1249 return true;
1250 }
1251
1252 return false;
1253}
1254
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001255void hists__filter_by_dso(struct hists *hists)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001256{
1257 struct rb_node *nd;
1258
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001259 hists->stats.nr_non_filtered_samples = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001260
1261 hists__reset_filter_stats(hists);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001262 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001263
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001264 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001265 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1266
1267 if (symbol_conf.exclude_other && !h->parent)
1268 continue;
1269
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001270 if (hists__filter_entry_by_dso(hists, h))
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001271 continue;
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001272
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001273 hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001274 }
1275}
1276
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001277static bool hists__filter_entry_by_thread(struct hists *hists,
1278 struct hist_entry *he)
1279{
1280 if (hists->thread_filter != NULL &&
1281 he->thread != hists->thread_filter) {
1282 he->filtered |= (1 << HIST_FILTER__THREAD);
1283 return true;
1284 }
1285
1286 return false;
1287}
1288
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001289void hists__filter_by_thread(struct hists *hists)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001290{
1291 struct rb_node *nd;
1292
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001293 hists->stats.nr_non_filtered_samples = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001294
1295 hists__reset_filter_stats(hists);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001296 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001297
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001298 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001299 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1300
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001301 if (hists__filter_entry_by_thread(hists, h))
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001302 continue;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001303
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001304 hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001305 }
1306}
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001307
Namhyung Kime94d53e2012-03-16 17:50:51 +09001308static bool hists__filter_entry_by_symbol(struct hists *hists,
1309 struct hist_entry *he)
1310{
1311 if (hists->symbol_filter_str != NULL &&
1312 (!he->ms.sym || strstr(he->ms.sym->name,
1313 hists->symbol_filter_str) == NULL)) {
1314 he->filtered |= (1 << HIST_FILTER__SYMBOL);
1315 return true;
1316 }
1317
1318 return false;
1319}
1320
1321void hists__filter_by_symbol(struct hists *hists)
1322{
1323 struct rb_node *nd;
1324
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001325 hists->stats.nr_non_filtered_samples = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001326
1327 hists__reset_filter_stats(hists);
Namhyung Kime94d53e2012-03-16 17:50:51 +09001328 hists__reset_col_len(hists);
1329
1330 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1331 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1332
1333 if (hists__filter_entry_by_symbol(hists, h))
1334 continue;
1335
1336 hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
1337 }
1338}
1339
Kan Liang21394d92015-09-04 10:45:44 -04001340static bool hists__filter_entry_by_socket(struct hists *hists,
1341 struct hist_entry *he)
1342{
1343 if ((hists->socket_filter > -1) &&
1344 (he->socket != hists->socket_filter)) {
1345 he->filtered |= (1 << HIST_FILTER__SOCKET);
1346 return true;
1347 }
1348
1349 return false;
1350}
1351
Kan Liang84734b02015-09-04 10:45:45 -04001352void hists__filter_by_socket(struct hists *hists)
1353{
1354 struct rb_node *nd;
1355
1356 hists->stats.nr_non_filtered_samples = 0;
1357
1358 hists__reset_filter_stats(hists);
1359 hists__reset_col_len(hists);
1360
1361 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1362 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1363
1364 if (hists__filter_entry_by_socket(hists, h))
1365 continue;
1366
1367 hists__remove_entry_filter(hists, h, HIST_FILTER__SOCKET);
1368 }
1369}
1370
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001371void events_stats__inc(struct events_stats *stats, u32 type)
1372{
1373 ++stats->nr_events[0];
1374 ++stats->nr_events[type];
1375}
1376
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001377void hists__inc_nr_events(struct hists *hists, u32 type)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001378{
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001379 events_stats__inc(&hists->stats, type);
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001380}
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001381
Namhyung Kim1844dbc2014-05-28 14:12:18 +09001382void hists__inc_nr_samples(struct hists *hists, bool filtered)
1383{
1384 events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
1385 if (!filtered)
1386 hists->stats.nr_non_filtered_samples++;
1387}
1388
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001389static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
1390 struct hist_entry *pair)
1391{
Namhyung Kimce74f602012-12-10 17:29:55 +09001392 struct rb_root *root;
1393 struct rb_node **p;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001394 struct rb_node *parent = NULL;
1395 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -07001396 int64_t cmp;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001397
Namhyung Kimce74f602012-12-10 17:29:55 +09001398 if (sort__need_collapse)
1399 root = &hists->entries_collapsed;
1400 else
1401 root = hists->entries_in;
1402
1403 p = &root->rb_node;
1404
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001405 while (*p != NULL) {
1406 parent = *p;
Namhyung Kimce74f602012-12-10 17:29:55 +09001407 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001408
Namhyung Kimce74f602012-12-10 17:29:55 +09001409 cmp = hist_entry__collapse(he, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001410
1411 if (!cmp)
1412 goto out;
1413
1414 if (cmp < 0)
1415 p = &(*p)->rb_left;
1416 else
1417 p = &(*p)->rb_right;
1418 }
1419
Namhyung Kima0b51af2012-09-11 13:34:27 +09001420 he = hist_entry__new(pair, true);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001421 if (he) {
Arnaldo Carvalho de Melo30193d72012-11-12 13:20:03 -03001422 memset(&he->stat, 0, sizeof(he->stat));
1423 he->hists = hists;
Namhyung Kimce74f602012-12-10 17:29:55 +09001424 rb_link_node(&he->rb_node_in, parent, p);
1425 rb_insert_color(&he->rb_node_in, root);
Namhyung Kim62638352014-04-24 16:21:46 +09001426 hists__inc_stats(hists, he);
Jiri Olsae0af43d2012-12-01 21:18:20 +01001427 he->dummy = true;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001428 }
1429out:
1430 return he;
1431}
1432
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001433static struct hist_entry *hists__find_entry(struct hists *hists,
1434 struct hist_entry *he)
1435{
Namhyung Kimce74f602012-12-10 17:29:55 +09001436 struct rb_node *n;
1437
1438 if (sort__need_collapse)
1439 n = hists->entries_collapsed.rb_node;
1440 else
1441 n = hists->entries_in->rb_node;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001442
1443 while (n) {
Namhyung Kimce74f602012-12-10 17:29:55 +09001444 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
1445 int64_t cmp = hist_entry__collapse(iter, he);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001446
1447 if (cmp < 0)
1448 n = n->rb_left;
1449 else if (cmp > 0)
1450 n = n->rb_right;
1451 else
1452 return iter;
1453 }
1454
1455 return NULL;
1456}
1457
1458/*
1459 * Look for pairs to link to the leader buckets (hist_entries):
1460 */
1461void hists__match(struct hists *leader, struct hists *other)
1462{
Namhyung Kimce74f602012-12-10 17:29:55 +09001463 struct rb_root *root;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001464 struct rb_node *nd;
1465 struct hist_entry *pos, *pair;
1466
Namhyung Kimce74f602012-12-10 17:29:55 +09001467 if (sort__need_collapse)
1468 root = &leader->entries_collapsed;
1469 else
1470 root = leader->entries_in;
1471
1472 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
1473 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001474 pair = hists__find_entry(other, pos);
1475
1476 if (pair)
Namhyung Kim5fa90412012-11-29 15:38:34 +09001477 hist_entry__add_pair(pair, pos);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001478 }
1479}
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001480
1481/*
1482 * Look for entries in the other hists that are not present in the leader, if
1483 * we find them, just add a dummy entry on the leader hists, with period=0,
1484 * nr_events=0, to serve as the list header.
1485 */
1486int hists__link(struct hists *leader, struct hists *other)
1487{
Namhyung Kimce74f602012-12-10 17:29:55 +09001488 struct rb_root *root;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001489 struct rb_node *nd;
1490 struct hist_entry *pos, *pair;
1491
Namhyung Kimce74f602012-12-10 17:29:55 +09001492 if (sort__need_collapse)
1493 root = &other->entries_collapsed;
1494 else
1495 root = other->entries_in;
1496
1497 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
1498 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001499
1500 if (!hist_entry__has_pairs(pos)) {
1501 pair = hists__add_dummy_entry(leader, pos);
1502 if (pair == NULL)
1503 return -1;
Namhyung Kim5fa90412012-11-29 15:38:34 +09001504 hist_entry__add_pair(pos, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001505 }
1506 }
1507
1508 return 0;
1509}
Namhyung Kimf2148332014-01-14 11:52:48 +09001510
Andi Kleen578499982015-07-18 08:24:49 -07001511void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
1512 struct perf_sample *sample, bool nonany_branch_mode)
1513{
1514 struct branch_info *bi;
1515
1516 /* If we have branch cycles always annotate them. */
1517 if (bs && bs->nr && bs->entries[0].flags.cycles) {
1518 int i;
1519
1520 bi = sample__resolve_bstack(sample, al);
1521 if (bi) {
1522 struct addr_map_symbol *prev = NULL;
1523
1524 /*
1525 * Ignore errors, still want to process the
1526 * other entries.
1527 *
1528 * For non standard branch modes always
1529 * force no IPC (prev == NULL)
1530 *
1531 * Note that perf stores branches reversed from
1532 * program order!
1533 */
1534 for (i = bs->nr - 1; i >= 0; i--) {
1535 addr_map_symbol__account_cycles(&bi[i].from,
1536 nonany_branch_mode ? NULL : prev,
1537 bi[i].flags.cycles);
1538 prev = &bi[i].to;
1539 }
1540 free(bi);
1541 }
1542 }
1543}
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03001544
1545size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp)
1546{
1547 struct perf_evsel *pos;
1548 size_t ret = 0;
1549
1550 evlist__for_each(evlist, pos) {
1551 ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
1552 ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
1553 }
1554
1555 return ret;
1556}
1557
1558
Namhyung Kimf2148332014-01-14 11:52:48 +09001559u64 hists__total_period(struct hists *hists)
1560{
1561 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
1562 hists->stats.total_period;
1563}
Namhyung Kim33db4562014-02-07 12:06:07 +09001564
1565int parse_filter_percentage(const struct option *opt __maybe_unused,
1566 const char *arg, int unset __maybe_unused)
1567{
1568 if (!strcmp(arg, "relative"))
1569 symbol_conf.filter_relative = true;
1570 else if (!strcmp(arg, "absolute"))
1571 symbol_conf.filter_relative = false;
1572 else
1573 return -1;
1574
1575 return 0;
1576}
Namhyung Kim0b93da12014-01-14 12:02:15 +09001577
1578int perf_hist_config(const char *var, const char *value)
1579{
1580 if (!strcmp(var, "hist.percentage"))
1581 return parse_filter_percentage(NULL, value, 0);
1582
1583 return 0;
1584}
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03001585
Namhyung Kimfc284be2016-01-07 10:14:10 +01001586int __hists__init(struct hists *hists)
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03001587{
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03001588 memset(hists, 0, sizeof(*hists));
1589 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
1590 hists->entries_in = &hists->entries_in_array[0];
1591 hists->entries_collapsed = RB_ROOT;
1592 hists->entries = RB_ROOT;
1593 pthread_mutex_init(&hists->lock, NULL);
Kan Liang21394d92015-09-04 10:45:44 -04001594 hists->socket_filter = -1;
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03001595 return 0;
1596}
1597
Namhyung Kim61fa0e92015-12-10 16:53:20 +09001598static void hists__delete_remaining_entries(struct rb_root *root)
1599{
1600 struct rb_node *node;
1601 struct hist_entry *he;
1602
1603 while (!RB_EMPTY_ROOT(root)) {
1604 node = rb_first(root);
1605 rb_erase(node, root);
1606
1607 he = rb_entry(node, struct hist_entry, rb_node_in);
1608 hist_entry__delete(he);
1609 }
1610}
1611
1612static void hists__delete_all_entries(struct hists *hists)
1613{
1614 hists__delete_entries(hists);
1615 hists__delete_remaining_entries(&hists->entries_in_array[0]);
1616 hists__delete_remaining_entries(&hists->entries_in_array[1]);
1617 hists__delete_remaining_entries(&hists->entries_collapsed);
1618}
1619
Masami Hiramatsu17577de2015-12-09 11:11:29 +09001620static void hists_evsel__exit(struct perf_evsel *evsel)
1621{
1622 struct hists *hists = evsel__hists(evsel);
1623
Namhyung Kim61fa0e92015-12-10 16:53:20 +09001624 hists__delete_all_entries(hists);
Masami Hiramatsu17577de2015-12-09 11:11:29 +09001625}
1626
Namhyung Kimfc284be2016-01-07 10:14:10 +01001627static int hists_evsel__init(struct perf_evsel *evsel)
1628{
1629 struct hists *hists = evsel__hists(evsel);
1630
1631 __hists__init(hists);
1632 return 0;
1633}
1634
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03001635/*
1636 * XXX We probably need a hists_evsel__exit() to free the hist_entries
1637 * stored in the rbtree...
1638 */
1639
1640int hists__init(void)
1641{
1642 int err = perf_evsel__object_config(sizeof(struct hists_evsel),
Masami Hiramatsu17577de2015-12-09 11:11:29 +09001643 hists_evsel__init,
1644 hists_evsel__exit);
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03001645 if (err)
1646 fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);
1647
1648 return err;
1649}