blob: 4fd37d6708cb973d2653c1ec4f6f17b086c712d0 [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 Meloab81f3fd2011-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 Meloab81f3fd2011-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);
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200257
Namhyung Kim3186b682014-04-22 13:44:23 +0900258 diff = prev_period - he->stat.period;
259
260 hists->stats.total_period -= diff;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200261 if (!he->filtered)
Namhyung Kim3186b682014-04-22 13:44:23 +0900262 hists->stats.total_non_filtered_period -= diff;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200263
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900264 return he->stat.period == 0;
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300265}
266
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300267static void hists__delete_entry(struct hists *hists, struct hist_entry *he)
268{
269 rb_erase(&he->rb_node, &hists->entries);
270
271 if (sort__need_collapse)
272 rb_erase(&he->rb_node_in, &hists->entries_collapsed);
273
274 --hists->nr_entries;
275 if (!he->filtered)
276 --hists->nr_non_filtered_entries;
277
278 hist_entry__delete(he);
279}
280
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900281void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300282{
283 struct rb_node *next = rb_first(&hists->entries);
284 struct hist_entry *n;
285
286 while (next) {
287 n = rb_entry(next, struct hist_entry, rb_node);
288 next = rb_next(&n->rb_node);
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200289 if (((zap_user && n->level == '.') ||
290 (zap_kernel && n->level != '.') ||
Arnaldo Carvalho de Melo4c47f4f2015-03-17 17:18:58 -0300291 hists__decay_entry(hists, n))) {
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300292 hists__delete_entry(hists, n);
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300293 }
294 }
295}
296
Namhyung Kim701937b2014-08-12 17:16:05 +0900297void hists__delete_entries(struct hists *hists)
298{
299 struct rb_node *next = rb_first(&hists->entries);
300 struct hist_entry *n;
301
302 while (next) {
303 n = rb_entry(next, struct hist_entry, rb_node);
304 next = rb_next(&n->rb_node);
305
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300306 hists__delete_entry(hists, n);
Namhyung Kim701937b2014-08-12 17:16:05 +0900307 }
308}
309
John Kacur3d1d07e2009-09-28 15:32:55 +0200310/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300311 * histogram, sorted on item, collects periods
John Kacur3d1d07e2009-09-28 15:32:55 +0200312 */
313
Namhyung Kima0b51af2012-09-11 13:34:27 +0900314static struct hist_entry *hist_entry__new(struct hist_entry *template,
315 bool sample_self)
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300316{
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900317 size_t callchain_size = 0;
318 struct hist_entry *he;
319
Namhyung Kim82aa0192014-12-22 13:44:14 +0900320 if (symbol_conf.use_callchain)
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900321 callchain_size = sizeof(struct callchain_root);
322
323 he = zalloc(sizeof(*he) + callchain_size);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300324
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200325 if (he != NULL) {
326 *he = *template;
Namhyung Kimc4b35352012-10-04 21:49:42 +0900327
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900328 if (symbol_conf.cumulate_callchain) {
329 he->stat_acc = malloc(sizeof(he->stat));
330 if (he->stat_acc == NULL) {
331 free(he);
332 return NULL;
333 }
334 memcpy(he->stat_acc, &he->stat, sizeof(he->stat));
Namhyung Kima0b51af2012-09-11 13:34:27 +0900335 if (!sample_self)
336 memset(&he->stat, 0, sizeof(he->stat));
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900337 }
338
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300339 map__get(he->ms.map);
Stephane Eranian3cf0cb12013-01-14 15:02:45 +0100340
341 if (he->branch_info) {
Namhyung Kim26353a62013-04-01 20:35:17 +0900342 /*
343 * This branch info is (a part of) allocated from
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -0300344 * sample__resolve_bstack() and will be freed after
Namhyung Kim26353a62013-04-01 20:35:17 +0900345 * adding new entries. So we need to save a copy.
346 */
347 he->branch_info = malloc(sizeof(*he->branch_info));
348 if (he->branch_info == NULL) {
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300349 map__zput(he->ms.map);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900350 free(he->stat_acc);
Namhyung Kim26353a62013-04-01 20:35:17 +0900351 free(he);
352 return NULL;
353 }
354
355 memcpy(he->branch_info, template->branch_info,
356 sizeof(*he->branch_info));
357
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300358 map__get(he->branch_info->from.map);
359 map__get(he->branch_info->to.map);
Stephane Eranian3cf0cb12013-01-14 15:02:45 +0100360 }
361
Stephane Eranian98a3b322013-01-24 16:10:35 +0100362 if (he->mem_info) {
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300363 map__get(he->mem_info->iaddr.map);
364 map__get(he->mem_info->daddr.map);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100365 }
366
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300367 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200368 callchain_init(he->callchain);
Arnaldo Carvalho de Melob821c732012-10-25 14:42:45 -0200369
370 INIT_LIST_HEAD(&he->pairs.node);
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300371 thread__get(he->thread);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300372 }
373
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200374 return he;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300375}
376
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300377static u8 symbol__parent_filter(const struct symbol *parent)
378{
379 if (symbol_conf.exclude_other && parent == NULL)
380 return 1 << HIST_FILTER__PARENT;
381 return 0;
382}
383
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300384static struct hist_entry *hists__findnew_entry(struct hists *hists,
385 struct hist_entry *entry,
386 struct addr_location *al,
387 bool sample_self)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300388{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300389 struct rb_node **p;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300390 struct rb_node *parent = NULL;
391 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -0700392 int64_t cmp;
Namhyung Kimf1cbf782013-12-18 14:21:11 +0900393 u64 period = entry->stat.period;
394 u64 weight = entry->stat.weight;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300395
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300396 p = &hists->entries_in->rb_node;
397
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300398 while (*p != NULL) {
399 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300400 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300401
Namhyung Kim9afcf932012-12-10 17:29:54 +0900402 /*
403 * Make sure that it receives arguments in a same order as
404 * hist_entry__collapse() so that we can use an appropriate
405 * function when searching an entry regardless which sort
406 * keys were used.
407 */
408 cmp = hist_entry__cmp(he, entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300409
410 if (!cmp) {
Namhyung Kima0b51af2012-09-11 13:34:27 +0900411 if (sample_self)
412 he_stat__add_period(&he->stat, period, weight);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900413 if (symbol_conf.cumulate_callchain)
414 he_stat__add_period(he->stat_acc, period, weight);
David Miller63fa4712012-03-27 03:14:18 -0400415
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900416 /*
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -0300417 * This mem info was allocated from sample__resolve_mem
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900418 * and will not be used anymore.
419 */
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300420 zfree(&entry->mem_info);
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900421
David Miller63fa4712012-03-27 03:14:18 -0400422 /* If the map of an existing hist_entry has
423 * become out-of-date due to an exec() or
424 * similar, update it. Otherwise we will
425 * mis-adjust symbol addresses when computing
426 * the history counter to increment.
427 */
428 if (he->ms.map != entry->ms.map) {
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300429 map__put(he->ms.map);
430 he->ms.map = map__get(entry->ms.map);
David Miller63fa4712012-03-27 03:14:18 -0400431 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300432 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300433 }
434
435 if (cmp < 0)
436 p = &(*p)->rb_left;
437 else
438 p = &(*p)->rb_right;
439 }
440
Namhyung Kima0b51af2012-09-11 13:34:27 +0900441 he = hist_entry__new(entry, sample_self);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300442 if (!he)
Namhyung Kim27a0dcb2013-05-14 11:09:02 +0900443 return NULL;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300444
Namhyung Kim590cd342014-12-22 13:44:09 +0900445 hists->nr_entries++;
446
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300447 rb_link_node(&he->rb_node_in, parent, p);
448 rb_insert_color(&he->rb_node_in, hists->entries_in);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300449out:
Namhyung Kima0b51af2012-09-11 13:34:27 +0900450 if (sample_self)
451 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900452 if (symbol_conf.cumulate_callchain)
453 he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300454 return he;
455}
456
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300457struct hist_entry *__hists__add_entry(struct hists *hists,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100458 struct addr_location *al,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900459 struct symbol *sym_parent,
460 struct branch_info *bi,
461 struct mem_info *mi,
Namhyung Kima0b51af2012-09-11 13:34:27 +0900462 u64 period, u64 weight, u64 transaction,
463 bool sample_self)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100464{
465 struct hist_entry entry = {
466 .thread = al->thread,
Namhyung Kim4dfced32013-09-13 16:28:57 +0900467 .comm = thread__comm(al->thread),
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100468 .ms = {
469 .map = al->map,
470 .sym = al->sym,
471 },
Kan Liang0c4c4deb2015-09-04 10:45:42 -0400472 .socket = al->socket,
Don Zickus7365be52014-05-27 12:28:05 -0400473 .cpu = al->cpu,
474 .cpumode = al->cpumode,
475 .ip = al->addr,
476 .level = al->level,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900477 .stat = {
Namhyung Kimc4b35352012-10-04 21:49:42 +0900478 .nr_events = 1,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900479 .period = period,
Andi Kleen05484292013-01-24 16:10:29 +0100480 .weight = weight,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900481 },
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100482 .parent = sym_parent,
Namhyung Kim2c86c7c2014-03-17 18:18:54 -0300483 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300484 .hists = hists,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900485 .branch_info = bi,
486 .mem_info = mi,
Andi Kleen475eeab2013-09-20 07:40:43 -0700487 .transaction = transaction,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100488 };
489
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300490 return hists__findnew_entry(hists, &entry, al, sample_self);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100491}
492
Namhyung Kim69bcb012013-10-30 09:40:34 +0900493static int
494iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
495 struct addr_location *al __maybe_unused)
496{
497 return 0;
498}
499
500static int
501iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
502 struct addr_location *al __maybe_unused)
503{
504 return 0;
505}
506
507static int
508iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
509{
510 struct perf_sample *sample = iter->sample;
511 struct mem_info *mi;
512
513 mi = sample__resolve_mem(sample, al);
514 if (mi == NULL)
515 return -ENOMEM;
516
517 iter->priv = mi;
518 return 0;
519}
520
521static int
522iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
523{
524 u64 cost;
525 struct mem_info *mi = iter->priv;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300526 struct hists *hists = evsel__hists(iter->evsel);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900527 struct hist_entry *he;
528
529 if (mi == NULL)
530 return -EINVAL;
531
532 cost = iter->sample->weight;
533 if (!cost)
534 cost = 1;
535
536 /*
537 * must pass period=weight in order to get the correct
538 * sorting from hists__collapse_resort() which is solely
539 * based on periods. We want sorting be done on nr_events * weight
540 * and this is indirectly achieved by passing period=weight here
541 * and the he_stat__add_period() function.
542 */
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300543 he = __hists__add_entry(hists, al, iter->parent, NULL, mi,
Namhyung Kima0b51af2012-09-11 13:34:27 +0900544 cost, cost, 0, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900545 if (!he)
546 return -ENOMEM;
547
548 iter->he = he;
549 return 0;
550}
551
552static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900553iter_finish_mem_entry(struct hist_entry_iter *iter,
554 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900555{
556 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300557 struct hists *hists = evsel__hists(evsel);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900558 struct hist_entry *he = iter->he;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900559 int err = -EINVAL;
560
561 if (he == NULL)
562 goto out;
563
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300564 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900565
566 err = hist_entry__append_callchain(he, iter->sample);
567
568out:
569 /*
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300570 * We don't need to free iter->priv (mem_info) here since the mem info
571 * was either already freed in hists__findnew_entry() or passed to a
572 * new hist entry by hist_entry__new().
Namhyung Kim69bcb012013-10-30 09:40:34 +0900573 */
574 iter->priv = NULL;
575
576 iter->he = NULL;
577 return err;
578}
579
580static int
581iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
582{
583 struct branch_info *bi;
584 struct perf_sample *sample = iter->sample;
585
586 bi = sample__resolve_bstack(sample, al);
587 if (!bi)
588 return -ENOMEM;
589
590 iter->curr = 0;
591 iter->total = sample->branch_stack->nr;
592
593 iter->priv = bi;
594 return 0;
595}
596
597static int
598iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused,
599 struct addr_location *al __maybe_unused)
600{
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900601 /* to avoid calling callback function */
602 iter->he = NULL;
603
Namhyung Kim69bcb012013-10-30 09:40:34 +0900604 return 0;
605}
606
607static int
608iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
609{
610 struct branch_info *bi = iter->priv;
611 int i = iter->curr;
612
613 if (bi == NULL)
614 return 0;
615
616 if (iter->curr >= iter->total)
617 return 0;
618
619 al->map = bi[i].to.map;
620 al->sym = bi[i].to.sym;
621 al->addr = bi[i].to.addr;
622 return 1;
623}
624
625static int
626iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
627{
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900628 struct branch_info *bi;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900629 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300630 struct hists *hists = evsel__hists(evsel);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900631 struct hist_entry *he = NULL;
632 int i = iter->curr;
633 int err = 0;
634
635 bi = iter->priv;
636
637 if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym))
638 goto out;
639
640 /*
641 * The report shows the percentage of total branches captured
642 * and not events sampled. Thus we use a pseudo period of 1.
643 */
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300644 he = __hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
Andi Kleen0e332f02015-07-18 08:24:46 -0700645 1, bi->flags.cycles ? bi->flags.cycles : 1,
646 0, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900647 if (he == NULL)
648 return -ENOMEM;
649
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300650 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900651
652out:
653 iter->he = he;
654 iter->curr++;
655 return err;
656}
657
658static int
659iter_finish_branch_entry(struct hist_entry_iter *iter,
660 struct addr_location *al __maybe_unused)
661{
662 zfree(&iter->priv);
663 iter->he = NULL;
664
665 return iter->curr >= iter->total ? 0 : -1;
666}
667
668static int
669iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
670 struct addr_location *al __maybe_unused)
671{
672 return 0;
673}
674
675static int
676iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
677{
678 struct perf_evsel *evsel = iter->evsel;
679 struct perf_sample *sample = iter->sample;
680 struct hist_entry *he;
681
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300682 he = __hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
Namhyung Kim69bcb012013-10-30 09:40:34 +0900683 sample->period, sample->weight,
Namhyung Kima0b51af2012-09-11 13:34:27 +0900684 sample->transaction, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900685 if (he == NULL)
686 return -ENOMEM;
687
688 iter->he = he;
689 return 0;
690}
691
692static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900693iter_finish_normal_entry(struct hist_entry_iter *iter,
694 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900695{
Namhyung Kim69bcb012013-10-30 09:40:34 +0900696 struct hist_entry *he = iter->he;
697 struct perf_evsel *evsel = iter->evsel;
698 struct perf_sample *sample = iter->sample;
699
700 if (he == NULL)
701 return 0;
702
703 iter->he = NULL;
704
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300705 hists__inc_nr_samples(evsel__hists(evsel), he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900706
707 return hist_entry__append_callchain(he, sample);
708}
709
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900710static int
Adrian Hunter96b40f32015-09-25 16:15:47 +0300711iter_prepare_cumulative_entry(struct hist_entry_iter *iter,
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900712 struct addr_location *al __maybe_unused)
713{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900714 struct hist_entry **he_cache;
715
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900716 callchain_cursor_commit(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900717
718 /*
719 * This is for detecting cycles or recursions so that they're
720 * cumulated only one time to prevent entries more than 100%
721 * overhead.
722 */
Adrian Hunter96b40f32015-09-25 16:15:47 +0300723 he_cache = malloc(sizeof(*he_cache) * (iter->max_stack + 1));
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900724 if (he_cache == NULL)
725 return -ENOMEM;
726
727 iter->priv = he_cache;
728 iter->curr = 0;
729
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900730 return 0;
731}
732
733static int
734iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
735 struct addr_location *al)
736{
737 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300738 struct hists *hists = evsel__hists(evsel);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900739 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900740 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900741 struct hist_entry *he;
742 int err = 0;
743
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300744 he = __hists__add_entry(hists, al, iter->parent, NULL, NULL,
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900745 sample->period, sample->weight,
746 sample->transaction, true);
747 if (he == NULL)
748 return -ENOMEM;
749
750 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900751 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900752
Namhyung Kim82aa0192014-12-22 13:44:14 +0900753 hist_entry__append_callchain(he, sample);
Namhyung Kimbe7f8552013-12-26 17:44:10 +0900754
755 /*
756 * We need to re-initialize the cursor since callchain_append()
757 * advanced the cursor to the end.
758 */
759 callchain_cursor_commit(&callchain_cursor);
760
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300761 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900762
763 return err;
764}
765
766static int
767iter_next_cumulative_entry(struct hist_entry_iter *iter,
768 struct addr_location *al)
769{
770 struct callchain_cursor_node *node;
771
772 node = callchain_cursor_current(&callchain_cursor);
773 if (node == NULL)
774 return 0;
775
Namhyung Kimc7405d82013-10-31 13:58:30 +0900776 return fill_callchain_info(al, node, iter->hide_unresolved);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900777}
778
779static int
780iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
781 struct addr_location *al)
782{
783 struct perf_evsel *evsel = iter->evsel;
784 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900785 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900786 struct hist_entry *he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900787 struct hist_entry he_tmp = {
Arnaldo Carvalho de Melo5cef8972015-08-10 15:45:55 -0300788 .hists = evsel__hists(evsel),
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900789 .cpu = al->cpu,
790 .thread = al->thread,
791 .comm = thread__comm(al->thread),
792 .ip = al->addr,
793 .ms = {
794 .map = al->map,
795 .sym = al->sym,
796 },
797 .parent = iter->parent,
798 };
799 int i;
Namhyung Kimbe7f8552013-12-26 17:44:10 +0900800 struct callchain_cursor cursor;
801
802 callchain_cursor_snapshot(&cursor, &callchain_cursor);
803
804 callchain_cursor_advance(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900805
806 /*
807 * Check if there's duplicate entries in the callchain.
808 * It's possible that it has cycles or recursive calls.
809 */
810 for (i = 0; i < iter->curr; i++) {
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900811 if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
812 /* to avoid calling callback function */
813 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900814 return 0;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900815 }
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900816 }
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900817
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300818 he = __hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900819 sample->period, sample->weight,
820 sample->transaction, false);
821 if (he == NULL)
822 return -ENOMEM;
823
824 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900825 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900826
Namhyung Kim82aa0192014-12-22 13:44:14 +0900827 if (symbol_conf.use_callchain)
828 callchain_append(he->callchain, &cursor, sample->period);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900829 return 0;
830}
831
832static int
833iter_finish_cumulative_entry(struct hist_entry_iter *iter,
834 struct addr_location *al __maybe_unused)
835{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900836 zfree(&iter->priv);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900837 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900838
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900839 return 0;
840}
841
Namhyung Kim69bcb012013-10-30 09:40:34 +0900842const struct hist_iter_ops hist_iter_mem = {
843 .prepare_entry = iter_prepare_mem_entry,
844 .add_single_entry = iter_add_single_mem_entry,
845 .next_entry = iter_next_nop_entry,
846 .add_next_entry = iter_add_next_nop_entry,
847 .finish_entry = iter_finish_mem_entry,
848};
849
850const struct hist_iter_ops hist_iter_branch = {
851 .prepare_entry = iter_prepare_branch_entry,
852 .add_single_entry = iter_add_single_branch_entry,
853 .next_entry = iter_next_branch_entry,
854 .add_next_entry = iter_add_next_branch_entry,
855 .finish_entry = iter_finish_branch_entry,
856};
857
858const struct hist_iter_ops hist_iter_normal = {
859 .prepare_entry = iter_prepare_normal_entry,
860 .add_single_entry = iter_add_single_normal_entry,
861 .next_entry = iter_next_nop_entry,
862 .add_next_entry = iter_add_next_nop_entry,
863 .finish_entry = iter_finish_normal_entry,
864};
865
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900866const struct hist_iter_ops hist_iter_cumulative = {
867 .prepare_entry = iter_prepare_cumulative_entry,
868 .add_single_entry = iter_add_single_cumulative_entry,
869 .next_entry = iter_next_cumulative_entry,
870 .add_next_entry = iter_add_next_cumulative_entry,
871 .finish_entry = iter_finish_cumulative_entry,
872};
873
Namhyung Kim69bcb012013-10-30 09:40:34 +0900874int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900875 int max_stack_depth, void *arg)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900876{
877 int err, err2;
878
Namhyung Kim063bd932015-05-19 17:04:10 +0900879 err = sample__resolve_callchain(iter->sample, &iter->parent,
880 iter->evsel, al, max_stack_depth);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900881 if (err)
882 return err;
883
Adrian Hunter96b40f32015-09-25 16:15:47 +0300884 iter->max_stack = max_stack_depth;
885
Namhyung Kim69bcb012013-10-30 09:40:34 +0900886 err = iter->ops->prepare_entry(iter, al);
887 if (err)
888 goto out;
889
890 err = iter->ops->add_single_entry(iter, al);
891 if (err)
892 goto out;
893
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900894 if (iter->he && iter->add_entry_cb) {
895 err = iter->add_entry_cb(iter, al, true, arg);
896 if (err)
897 goto out;
898 }
899
Namhyung Kim69bcb012013-10-30 09:40:34 +0900900 while (iter->ops->next_entry(iter, al)) {
901 err = iter->ops->add_next_entry(iter, al);
902 if (err)
903 break;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900904
905 if (iter->he && iter->add_entry_cb) {
906 err = iter->add_entry_cb(iter, al, false, arg);
907 if (err)
908 goto out;
909 }
Namhyung Kim69bcb012013-10-30 09:40:34 +0900910 }
911
912out:
913 err2 = iter->ops->finish_entry(iter, al);
914 if (!err)
915 err = err2;
916
917 return err;
918}
919
John Kacur3d1d07e2009-09-28 15:32:55 +0200920int64_t
921hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
922{
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900923 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +0200924 int64_t cmp = 0;
925
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900926 perf_hpp__for_each_sort_list(fmt) {
Namhyung Kime67d49a2014-03-18 13:00:59 +0900927 if (perf_hpp__should_skip(fmt))
928 continue;
929
Namhyung Kim87bbdf72015-01-08 09:45:46 +0900930 cmp = fmt->cmp(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200931 if (cmp)
932 break;
933 }
934
935 return cmp;
936}
937
938int64_t
939hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
940{
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900941 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +0200942 int64_t cmp = 0;
943
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900944 perf_hpp__for_each_sort_list(fmt) {
Namhyung Kime67d49a2014-03-18 13:00:59 +0900945 if (perf_hpp__should_skip(fmt))
946 continue;
947
Namhyung Kim87bbdf72015-01-08 09:45:46 +0900948 cmp = fmt->collapse(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200949 if (cmp)
950 break;
951 }
952
953 return cmp;
954}
955
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -0300956void hist_entry__delete(struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +0200957{
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300958 thread__zput(he->thread);
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300959 map__zput(he->ms.map);
960
961 if (he->branch_info) {
962 map__zput(he->branch_info->from.map);
963 map__zput(he->branch_info->to.map);
964 zfree(&he->branch_info);
965 }
966
967 if (he->mem_info) {
968 map__zput(he->mem_info->iaddr.map);
969 map__zput(he->mem_info->daddr.map);
970 zfree(&he->mem_info);
971 }
972
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900973 zfree(&he->stat_acc);
Namhyung Kimf048d542013-09-11 14:09:28 +0900974 free_srcline(he->srcline);
Andi Kleen31191a82015-08-07 15:54:24 -0700975 if (he->srcfile && he->srcfile[0])
976 free(he->srcfile);
Namhyung Kimd1149602014-12-30 14:38:13 +0900977 free_callchain(he->callchain);
John Kacur3d1d07e2009-09-28 15:32:55 +0200978 free(he);
979}
980
981/*
982 * collapse the histogram
983 */
984
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300985static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100986 struct rb_root *root,
987 struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +0200988{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200989 struct rb_node **p = &root->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200990 struct rb_node *parent = NULL;
991 struct hist_entry *iter;
992 int64_t cmp;
993
994 while (*p != NULL) {
995 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300996 iter = rb_entry(parent, struct hist_entry, rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +0200997
998 cmp = hist_entry__collapse(iter, he);
999
1000 if (!cmp) {
Namhyung Kim139c0812012-10-04 21:49:43 +09001001 he_stat__add_stat(&iter->stat, &he->stat);
Namhyung Kimf8be1c82012-09-11 13:15:07 +09001002 if (symbol_conf.cumulate_callchain)
1003 he_stat__add_stat(iter->stat_acc, he->stat_acc);
Namhyung Kim9ec60972012-09-26 16:47:28 +09001004
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +01001005 if (symbol_conf.use_callchain) {
Namhyung Kim47260642012-05-31 14:43:26 +09001006 callchain_cursor_reset(&callchain_cursor);
1007 callchain_merge(&callchain_cursor,
1008 iter->callchain,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +01001009 he->callchain);
1010 }
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -03001011 hist_entry__delete(he);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -03001012 return false;
John Kacur3d1d07e2009-09-28 15:32:55 +02001013 }
1014
1015 if (cmp < 0)
1016 p = &(*p)->rb_left;
1017 else
1018 p = &(*p)->rb_right;
1019 }
Namhyung Kim740b97f2014-12-22 13:44:10 +09001020 hists->nr_entries++;
John Kacur3d1d07e2009-09-28 15:32:55 +02001021
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001022 rb_link_node(&he->rb_node_in, parent, p);
1023 rb_insert_color(&he->rb_node_in, root);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -03001024 return true;
John Kacur3d1d07e2009-09-28 15:32:55 +02001025}
1026
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001027static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
1028{
1029 struct rb_root *root;
1030
1031 pthread_mutex_lock(&hists->lock);
1032
1033 root = hists->entries_in;
1034 if (++hists->entries_in > &hists->entries_in_array[1])
1035 hists->entries_in = &hists->entries_in_array[0];
1036
1037 pthread_mutex_unlock(&hists->lock);
1038
1039 return root;
1040}
1041
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001042static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
1043{
1044 hists__filter_entry_by_dso(hists, he);
1045 hists__filter_entry_by_thread(hists, he);
Namhyung Kime94d53e2012-03-16 17:50:51 +09001046 hists__filter_entry_by_symbol(hists, he);
Kan Liang21394d92015-09-04 10:45:44 -04001047 hists__filter_entry_by_socket(hists, he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001048}
1049
Namhyung Kimc1fb5652013-10-11 14:15:38 +09001050void hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001051{
1052 struct rb_root *root;
1053 struct rb_node *next;
1054 struct hist_entry *n;
1055
Namhyung Kim3a5714f2013-05-14 11:09:01 +09001056 if (!sort__need_collapse)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001057 return;
1058
Namhyung Kim740b97f2014-12-22 13:44:10 +09001059 hists->nr_entries = 0;
1060
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001061 root = hists__get_rotate_entries_in(hists);
Namhyung Kim740b97f2014-12-22 13:44:10 +09001062
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001063 next = rb_first(root);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001064
1065 while (next) {
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001066 if (session_done())
1067 break;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001068 n = rb_entry(next, struct hist_entry, rb_node_in);
1069 next = rb_next(&n->rb_node_in);
1070
1071 rb_erase(&n->rb_node_in, root);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001072 if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
1073 /*
1074 * If it wasn't combined with one of the entries already
1075 * collapsed, we need to apply the filters that may have
1076 * been set by, say, the hist_browser.
1077 */
1078 hists__apply_filters(hists, n);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001079 }
Namhyung Kimc1fb5652013-10-11 14:15:38 +09001080 if (prog)
1081 ui_progress__update(prog, 1);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001082 }
1083}
1084
Namhyung Kim043ca3892014-03-03 14:18:00 +09001085static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001086{
Namhyung Kim043ca3892014-03-03 14:18:00 +09001087 struct perf_hpp_fmt *fmt;
1088 int64_t cmp = 0;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001089
Namhyung Kim26d8b332014-03-03 16:16:20 +09001090 perf_hpp__for_each_sort_list(fmt) {
Namhyung Kime67d49a2014-03-18 13:00:59 +09001091 if (perf_hpp__should_skip(fmt))
1092 continue;
1093
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001094 cmp = fmt->sort(fmt, a, b);
Namhyung Kim043ca3892014-03-03 14:18:00 +09001095 if (cmp)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001096 break;
1097 }
1098
Namhyung Kim043ca3892014-03-03 14:18:00 +09001099 return cmp;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001100}
1101
Namhyung Kim9283ba92014-04-24 16:37:26 +09001102static void hists__reset_filter_stats(struct hists *hists)
1103{
1104 hists->nr_non_filtered_entries = 0;
1105 hists->stats.total_non_filtered_period = 0;
1106}
1107
1108void hists__reset_stats(struct hists *hists)
1109{
1110 hists->nr_entries = 0;
1111 hists->stats.total_period = 0;
1112
1113 hists__reset_filter_stats(hists);
1114}
1115
1116static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
1117{
1118 hists->nr_non_filtered_entries++;
1119 hists->stats.total_non_filtered_period += h->stat.period;
1120}
1121
1122void hists__inc_stats(struct hists *hists, struct hist_entry *h)
1123{
1124 if (!h->filtered)
1125 hists__inc_filter_stats(hists, h);
1126
1127 hists->nr_entries++;
1128 hists->stats.total_period += h->stat.period;
1129}
1130
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001131static void __hists__insert_output_entry(struct rb_root *entries,
1132 struct hist_entry *he,
Kan Liangf9db0d02015-08-11 06:30:48 -04001133 u64 min_callchain_hits,
1134 bool use_callchain)
John Kacur3d1d07e2009-09-28 15:32:55 +02001135{
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001136 struct rb_node **p = &entries->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +02001137 struct rb_node *parent = NULL;
1138 struct hist_entry *iter;
1139
Kan Liangf9db0d02015-08-11 06:30:48 -04001140 if (use_callchain)
Arnaldo Carvalho de Melob9fb9302010-04-02 09:50:42 -03001141 callchain_param.sort(&he->sorted_chain, he->callchain,
John Kacur3d1d07e2009-09-28 15:32:55 +02001142 min_callchain_hits, &callchain_param);
1143
1144 while (*p != NULL) {
1145 parent = *p;
1146 iter = rb_entry(parent, struct hist_entry, rb_node);
1147
Namhyung Kim043ca3892014-03-03 14:18:00 +09001148 if (hist_entry__sort(he, iter) > 0)
John Kacur3d1d07e2009-09-28 15:32:55 +02001149 p = &(*p)->rb_left;
1150 else
1151 p = &(*p)->rb_right;
1152 }
1153
1154 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001155 rb_insert_color(&he->rb_node, entries);
John Kacur3d1d07e2009-09-28 15:32:55 +02001156}
1157
Namhyung Kim740b97f2014-12-22 13:44:10 +09001158void hists__output_resort(struct hists *hists, struct ui_progress *prog)
John Kacur3d1d07e2009-09-28 15:32:55 +02001159{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001160 struct rb_root *root;
John Kacur3d1d07e2009-09-28 15:32:55 +02001161 struct rb_node *next;
1162 struct hist_entry *n;
John Kacur3d1d07e2009-09-28 15:32:55 +02001163 u64 min_callchain_hits;
Kan Liangf9db0d02015-08-11 06:30:48 -04001164 struct perf_evsel *evsel = hists_to_evsel(hists);
Kan Liang9e207dd2015-08-11 06:30:49 -04001165 bool use_callchain;
1166
Namhyung Kim208e7602015-09-30 13:34:00 +09001167 if (evsel && symbol_conf.use_callchain && !symbol_conf.show_ref_callgraph)
Kan Liang9e207dd2015-08-11 06:30:49 -04001168 use_callchain = evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN;
1169 else
1170 use_callchain = symbol_conf.use_callchain;
John Kacur3d1d07e2009-09-28 15:32:55 +02001171
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001172 min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
John Kacur3d1d07e2009-09-28 15:32:55 +02001173
Namhyung Kim3a5714f2013-05-14 11:09:01 +09001174 if (sort__need_collapse)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001175 root = &hists->entries_collapsed;
1176 else
1177 root = hists->entries_in;
1178
1179 next = rb_first(root);
1180 hists->entries = RB_ROOT;
John Kacur3d1d07e2009-09-28 15:32:55 +02001181
Namhyung Kim9283ba92014-04-24 16:37:26 +09001182 hists__reset_stats(hists);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001183 hists__reset_col_len(hists);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -03001184
John Kacur3d1d07e2009-09-28 15:32:55 +02001185 while (next) {
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001186 n = rb_entry(next, struct hist_entry, rb_node_in);
1187 next = rb_next(&n->rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +02001188
Kan Liangf9db0d02015-08-11 06:30:48 -04001189 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits, use_callchain);
Namhyung Kim62638352014-04-24 16:21:46 +09001190 hists__inc_stats(hists, n);
Namhyung Kimae993ef2014-04-24 16:25:19 +09001191
1192 if (!n->filtered)
1193 hists__calc_col_len(hists, n);
Namhyung Kim740b97f2014-12-22 13:44:10 +09001194
1195 if (prog)
1196 ui_progress__update(prog, 1);
John Kacur3d1d07e2009-09-28 15:32:55 +02001197 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001198}
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -02001199
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001200static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001201 enum hist_filter filter)
1202{
1203 h->filtered &= ~(1 << filter);
1204 if (h->filtered)
1205 return;
1206
Namhyung Kim87e90f42014-04-24 16:44:16 +09001207 /* force fold unfiltered entry for simplicity */
Namhyung Kim3698dab2015-05-05 23:55:46 +09001208 h->unfolded = false;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -03001209 h->row_offset = 0;
He Kuanga8cd1f42015-03-11 20:36:03 +08001210 h->nr_rows = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001211
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001212 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001213
Namhyung Kim9283ba92014-04-24 16:37:26 +09001214 hists__inc_filter_stats(hists, h);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001215 hists__calc_col_len(hists, h);
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001216}
1217
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001218
1219static bool hists__filter_entry_by_dso(struct hists *hists,
1220 struct hist_entry *he)
1221{
1222 if (hists->dso_filter != NULL &&
1223 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
1224 he->filtered |= (1 << HIST_FILTER__DSO);
1225 return true;
1226 }
1227
1228 return false;
1229}
1230
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001231void hists__filter_by_dso(struct hists *hists)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001232{
1233 struct rb_node *nd;
1234
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001235 hists->stats.nr_non_filtered_samples = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001236
1237 hists__reset_filter_stats(hists);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001238 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001239
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001240 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001241 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1242
1243 if (symbol_conf.exclude_other && !h->parent)
1244 continue;
1245
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001246 if (hists__filter_entry_by_dso(hists, h))
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001247 continue;
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001248
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001249 hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001250 }
1251}
1252
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001253static bool hists__filter_entry_by_thread(struct hists *hists,
1254 struct hist_entry *he)
1255{
1256 if (hists->thread_filter != NULL &&
1257 he->thread != hists->thread_filter) {
1258 he->filtered |= (1 << HIST_FILTER__THREAD);
1259 return true;
1260 }
1261
1262 return false;
1263}
1264
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001265void hists__filter_by_thread(struct hists *hists)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001266{
1267 struct rb_node *nd;
1268
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001269 hists->stats.nr_non_filtered_samples = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001270
1271 hists__reset_filter_stats(hists);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001272 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001273
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001274 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001275 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1276
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001277 if (hists__filter_entry_by_thread(hists, h))
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001278 continue;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001279
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001280 hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001281 }
1282}
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001283
Namhyung Kime94d53e2012-03-16 17:50:51 +09001284static bool hists__filter_entry_by_symbol(struct hists *hists,
1285 struct hist_entry *he)
1286{
1287 if (hists->symbol_filter_str != NULL &&
1288 (!he->ms.sym || strstr(he->ms.sym->name,
1289 hists->symbol_filter_str) == NULL)) {
1290 he->filtered |= (1 << HIST_FILTER__SYMBOL);
1291 return true;
1292 }
1293
1294 return false;
1295}
1296
1297void hists__filter_by_symbol(struct hists *hists)
1298{
1299 struct rb_node *nd;
1300
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001301 hists->stats.nr_non_filtered_samples = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001302
1303 hists__reset_filter_stats(hists);
Namhyung Kime94d53e2012-03-16 17:50:51 +09001304 hists__reset_col_len(hists);
1305
1306 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1307 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1308
1309 if (hists__filter_entry_by_symbol(hists, h))
1310 continue;
1311
1312 hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
1313 }
1314}
1315
Kan Liang21394d92015-09-04 10:45:44 -04001316static bool hists__filter_entry_by_socket(struct hists *hists,
1317 struct hist_entry *he)
1318{
1319 if ((hists->socket_filter > -1) &&
1320 (he->socket != hists->socket_filter)) {
1321 he->filtered |= (1 << HIST_FILTER__SOCKET);
1322 return true;
1323 }
1324
1325 return false;
1326}
1327
Kan Liang84734b02015-09-04 10:45:45 -04001328void hists__filter_by_socket(struct hists *hists)
1329{
1330 struct rb_node *nd;
1331
1332 hists->stats.nr_non_filtered_samples = 0;
1333
1334 hists__reset_filter_stats(hists);
1335 hists__reset_col_len(hists);
1336
1337 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1338 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1339
1340 if (hists__filter_entry_by_socket(hists, h))
1341 continue;
1342
1343 hists__remove_entry_filter(hists, h, HIST_FILTER__SOCKET);
1344 }
1345}
1346
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001347void events_stats__inc(struct events_stats *stats, u32 type)
1348{
1349 ++stats->nr_events[0];
1350 ++stats->nr_events[type];
1351}
1352
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001353void hists__inc_nr_events(struct hists *hists, u32 type)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001354{
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001355 events_stats__inc(&hists->stats, type);
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001356}
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001357
Namhyung Kim1844dbc2014-05-28 14:12:18 +09001358void hists__inc_nr_samples(struct hists *hists, bool filtered)
1359{
1360 events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
1361 if (!filtered)
1362 hists->stats.nr_non_filtered_samples++;
1363}
1364
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001365static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
1366 struct hist_entry *pair)
1367{
Namhyung Kimce74f602012-12-10 17:29:55 +09001368 struct rb_root *root;
1369 struct rb_node **p;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001370 struct rb_node *parent = NULL;
1371 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -07001372 int64_t cmp;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001373
Namhyung Kimce74f602012-12-10 17:29:55 +09001374 if (sort__need_collapse)
1375 root = &hists->entries_collapsed;
1376 else
1377 root = hists->entries_in;
1378
1379 p = &root->rb_node;
1380
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001381 while (*p != NULL) {
1382 parent = *p;
Namhyung Kimce74f602012-12-10 17:29:55 +09001383 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001384
Namhyung Kimce74f602012-12-10 17:29:55 +09001385 cmp = hist_entry__collapse(he, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001386
1387 if (!cmp)
1388 goto out;
1389
1390 if (cmp < 0)
1391 p = &(*p)->rb_left;
1392 else
1393 p = &(*p)->rb_right;
1394 }
1395
Namhyung Kima0b51af2012-09-11 13:34:27 +09001396 he = hist_entry__new(pair, true);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001397 if (he) {
Arnaldo Carvalho de Melo30193d72012-11-12 13:20:03 -03001398 memset(&he->stat, 0, sizeof(he->stat));
1399 he->hists = hists;
Namhyung Kimce74f602012-12-10 17:29:55 +09001400 rb_link_node(&he->rb_node_in, parent, p);
1401 rb_insert_color(&he->rb_node_in, root);
Namhyung Kim62638352014-04-24 16:21:46 +09001402 hists__inc_stats(hists, he);
Jiri Olsae0af43d2012-12-01 21:18:20 +01001403 he->dummy = true;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001404 }
1405out:
1406 return he;
1407}
1408
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001409static struct hist_entry *hists__find_entry(struct hists *hists,
1410 struct hist_entry *he)
1411{
Namhyung Kimce74f602012-12-10 17:29:55 +09001412 struct rb_node *n;
1413
1414 if (sort__need_collapse)
1415 n = hists->entries_collapsed.rb_node;
1416 else
1417 n = hists->entries_in->rb_node;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001418
1419 while (n) {
Namhyung Kimce74f602012-12-10 17:29:55 +09001420 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
1421 int64_t cmp = hist_entry__collapse(iter, he);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001422
1423 if (cmp < 0)
1424 n = n->rb_left;
1425 else if (cmp > 0)
1426 n = n->rb_right;
1427 else
1428 return iter;
1429 }
1430
1431 return NULL;
1432}
1433
1434/*
1435 * Look for pairs to link to the leader buckets (hist_entries):
1436 */
1437void hists__match(struct hists *leader, struct hists *other)
1438{
Namhyung Kimce74f602012-12-10 17:29:55 +09001439 struct rb_root *root;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001440 struct rb_node *nd;
1441 struct hist_entry *pos, *pair;
1442
Namhyung Kimce74f602012-12-10 17:29:55 +09001443 if (sort__need_collapse)
1444 root = &leader->entries_collapsed;
1445 else
1446 root = leader->entries_in;
1447
1448 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
1449 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001450 pair = hists__find_entry(other, pos);
1451
1452 if (pair)
Namhyung Kim5fa90412012-11-29 15:38:34 +09001453 hist_entry__add_pair(pair, pos);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001454 }
1455}
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001456
1457/*
1458 * Look for entries in the other hists that are not present in the leader, if
1459 * we find them, just add a dummy entry on the leader hists, with period=0,
1460 * nr_events=0, to serve as the list header.
1461 */
1462int hists__link(struct hists *leader, struct hists *other)
1463{
Namhyung Kimce74f602012-12-10 17:29:55 +09001464 struct rb_root *root;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001465 struct rb_node *nd;
1466 struct hist_entry *pos, *pair;
1467
Namhyung Kimce74f602012-12-10 17:29:55 +09001468 if (sort__need_collapse)
1469 root = &other->entries_collapsed;
1470 else
1471 root = other->entries_in;
1472
1473 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
1474 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001475
1476 if (!hist_entry__has_pairs(pos)) {
1477 pair = hists__add_dummy_entry(leader, pos);
1478 if (pair == NULL)
1479 return -1;
Namhyung Kim5fa90412012-11-29 15:38:34 +09001480 hist_entry__add_pair(pos, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001481 }
1482 }
1483
1484 return 0;
1485}
Namhyung Kimf2148332014-01-14 11:52:48 +09001486
Andi Kleen578499982015-07-18 08:24:49 -07001487void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
1488 struct perf_sample *sample, bool nonany_branch_mode)
1489{
1490 struct branch_info *bi;
1491
1492 /* If we have branch cycles always annotate them. */
1493 if (bs && bs->nr && bs->entries[0].flags.cycles) {
1494 int i;
1495
1496 bi = sample__resolve_bstack(sample, al);
1497 if (bi) {
1498 struct addr_map_symbol *prev = NULL;
1499
1500 /*
1501 * Ignore errors, still want to process the
1502 * other entries.
1503 *
1504 * For non standard branch modes always
1505 * force no IPC (prev == NULL)
1506 *
1507 * Note that perf stores branches reversed from
1508 * program order!
1509 */
1510 for (i = bs->nr - 1; i >= 0; i--) {
1511 addr_map_symbol__account_cycles(&bi[i].from,
1512 nonany_branch_mode ? NULL : prev,
1513 bi[i].flags.cycles);
1514 prev = &bi[i].to;
1515 }
1516 free(bi);
1517 }
1518 }
1519}
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03001520
1521size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp)
1522{
1523 struct perf_evsel *pos;
1524 size_t ret = 0;
1525
1526 evlist__for_each(evlist, pos) {
1527 ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
1528 ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
1529 }
1530
1531 return ret;
1532}
1533
1534
Namhyung Kimf2148332014-01-14 11:52:48 +09001535u64 hists__total_period(struct hists *hists)
1536{
1537 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
1538 hists->stats.total_period;
1539}
Namhyung Kim33db4562014-02-07 12:06:07 +09001540
1541int parse_filter_percentage(const struct option *opt __maybe_unused,
1542 const char *arg, int unset __maybe_unused)
1543{
1544 if (!strcmp(arg, "relative"))
1545 symbol_conf.filter_relative = true;
1546 else if (!strcmp(arg, "absolute"))
1547 symbol_conf.filter_relative = false;
1548 else
1549 return -1;
1550
1551 return 0;
1552}
Namhyung Kim0b93da12014-01-14 12:02:15 +09001553
1554int perf_hist_config(const char *var, const char *value)
1555{
1556 if (!strcmp(var, "hist.percentage"))
1557 return parse_filter_percentage(NULL, value, 0);
1558
1559 return 0;
1560}
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03001561
1562static int hists_evsel__init(struct perf_evsel *evsel)
1563{
1564 struct hists *hists = evsel__hists(evsel);
1565
1566 memset(hists, 0, sizeof(*hists));
1567 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
1568 hists->entries_in = &hists->entries_in_array[0];
1569 hists->entries_collapsed = RB_ROOT;
1570 hists->entries = RB_ROOT;
1571 pthread_mutex_init(&hists->lock, NULL);
Kan Liang21394d92015-09-04 10:45:44 -04001572 hists->socket_filter = -1;
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03001573 return 0;
1574}
1575
1576/*
1577 * XXX We probably need a hists_evsel__exit() to free the hist_entries
1578 * stored in the rbtree...
1579 */
1580
1581int hists__init(void)
1582{
1583 int err = perf_evsel__object_config(sizeof(struct hists_evsel),
1584 hists_evsel__init, NULL);
1585 if (err)
1586 fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);
1587
1588 return err;
1589}