blob: 2f6c5e6c16f900820ffda2b4b410ec558d955fa9 [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"
Krister Johansen9c68ae92017-01-05 22:23:31 -08004#include "map.h"
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -02005#include "session.h"
Hari Bathinid890a982017-03-08 02:12:13 +05306#include "namespaces.h"
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -02007#include "sort.h"
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03008#include "evlist.h"
Namhyung Kim29d720e2013-01-22 18:09:33 +09009#include "evsel.h"
Namhyung Kim69bcb012013-10-30 09:40:34 +090010#include "annotate.h"
Arnaldo Carvalho de Melo632a5ca2017-04-17 16:30:49 -030011#include "srcline.h"
Arnaldo Carvalho de Meloe7ff8922017-04-19 21:34:35 -030012#include "thread.h"
Namhyung Kim740b97f2014-12-22 13:44:10 +090013#include "ui/progress.h"
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030014#include <errno.h>
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -020015#include <math.h>
Arnaldo Carvalho de Melo391e4202017-04-19 18:51:14 -030016#include <sys/param.h>
John Kacur3d1d07e2009-09-28 15:32:55 +020017
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -020018static bool hists__filter_entry_by_dso(struct hists *hists,
19 struct hist_entry *he);
20static bool hists__filter_entry_by_thread(struct hists *hists,
21 struct hist_entry *he);
Namhyung Kime94d53e2012-03-16 17:50:51 +090022static bool hists__filter_entry_by_symbol(struct hists *hists,
23 struct hist_entry *he);
Kan Liang21394d92015-09-04 10:45:44 -040024static bool hists__filter_entry_by_socket(struct hists *hists,
25 struct hist_entry *he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -020026
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030027u16 hists__col_len(struct hists *hists, enum hist_column col)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030028{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030029 return hists->col_len[col];
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030030}
31
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030032void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030033{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030034 hists->col_len[col] = len;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030035}
36
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030037bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030038{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030039 if (len > hists__col_len(hists, col)) {
40 hists__set_col_len(hists, col, len);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030041 return true;
42 }
43 return false;
44}
45
Namhyung Kim7ccf4f92012-08-20 13:52:05 +090046void hists__reset_col_len(struct hists *hists)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030047{
48 enum hist_column col;
49
50 for (col = 0; col < HISTC_NR_COLS; ++col)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030051 hists__set_col_len(hists, col, 0);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030052}
53
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010054static void hists__set_unres_dso_col_len(struct hists *hists, int dso)
55{
56 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
57
58 if (hists__col_len(hists, dso) < unresolved_col_width &&
59 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
60 !symbol_conf.dso_list)
61 hists__set_col_len(hists, dso, unresolved_col_width);
62}
63
Namhyung Kim7ccf4f92012-08-20 13:52:05 +090064void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030065{
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010066 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
Stephane Eranian98a3b322013-01-24 16:10:35 +010067 int symlen;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030068 u16 len;
69
Namhyung Kimded19d52013-04-01 20:35:19 +090070 /*
71 * +4 accounts for '[x] ' priv level info
72 * +2 accounts for 0x prefix on raw addresses
73 * +3 accounts for ' y ' symtab origin info
74 */
75 if (h->ms.sym) {
76 symlen = h->ms.sym->namelen + 4;
Namhyung Kimbb963e12017-02-17 17:17:38 +090077 if (verbose > 0)
Namhyung Kimded19d52013-04-01 20:35:19 +090078 symlen += BITS_PER_LONG / 4 + 2 + 3;
79 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
80 } else {
Stephane Eranian98a3b322013-01-24 16:10:35 +010081 symlen = unresolved_col_width + 4 + 2;
82 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010083 hists__set_unres_dso_col_len(hists, HISTC_DSO);
Stephane Eranian98a3b322013-01-24 16:10:35 +010084 }
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030085
86 len = thread__comm_len(h->thread);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030087 if (hists__new_col_len(hists, HISTC_COMM, len))
Jiri Olsa89c7cb22016-06-20 23:58:19 +020088 hists__set_col_len(hists, HISTC_THREAD, len + 8);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030089
90 if (h->ms.map) {
91 len = dso__name_len(h->ms.map->dso);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030092 hists__new_col_len(hists, HISTC_DSO, len);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030093 }
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010094
Namhyung Kimcb993742012-12-27 18:11:42 +090095 if (h->parent)
96 hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen);
97
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010098 if (h->branch_info) {
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010099 if (h->branch_info->from.sym) {
100 symlen = (int)h->branch_info->from.sym->namelen + 4;
Namhyung Kimbb963e12017-02-17 17:17:38 +0900101 if (verbose > 0)
Namhyung Kimded19d52013-04-01 20:35:19 +0900102 symlen += BITS_PER_LONG / 4 + 2 + 3;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100103 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
104
105 symlen = dso__name_len(h->branch_info->from.map->dso);
106 hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
107 } else {
108 symlen = unresolved_col_width + 4 + 2;
109 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
110 hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
111 }
112
113 if (h->branch_info->to.sym) {
114 symlen = (int)h->branch_info->to.sym->namelen + 4;
Namhyung Kimbb963e12017-02-17 17:17:38 +0900115 if (verbose > 0)
Namhyung Kimded19d52013-04-01 20:35:19 +0900116 symlen += BITS_PER_LONG / 4 + 2 + 3;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100117 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
118
119 symlen = dso__name_len(h->branch_info->to.map->dso);
120 hists__new_col_len(hists, HISTC_DSO_TO, symlen);
121 } else {
122 symlen = unresolved_col_width + 4 + 2;
123 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
124 hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
125 }
Andi Kleen508be0d2016-05-20 13:15:08 -0700126
127 if (h->branch_info->srcline_from)
128 hists__new_col_len(hists, HISTC_SRCLINE_FROM,
129 strlen(h->branch_info->srcline_from));
130 if (h->branch_info->srcline_to)
131 hists__new_col_len(hists, HISTC_SRCLINE_TO,
132 strlen(h->branch_info->srcline_to));
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100133 }
Stephane Eranian98a3b322013-01-24 16:10:35 +0100134
135 if (h->mem_info) {
Stephane Eranian98a3b322013-01-24 16:10:35 +0100136 if (h->mem_info->daddr.sym) {
137 symlen = (int)h->mem_info->daddr.sym->namelen + 4
138 + unresolved_col_width + 2;
139 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
140 symlen);
Don Zickus9b32ba72014-06-01 15:38:29 +0200141 hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
142 symlen + 1);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100143 } else {
144 symlen = unresolved_col_width + 4 + 2;
145 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
146 symlen);
Jiri Olsa08059092016-01-20 12:56:33 +0100147 hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
148 symlen);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100149 }
Jiri Olsab34b3bf2015-10-05 20:06:08 +0200150
151 if (h->mem_info->iaddr.sym) {
152 symlen = (int)h->mem_info->iaddr.sym->namelen + 4
153 + unresolved_col_width + 2;
154 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
155 symlen);
156 } else {
157 symlen = unresolved_col_width + 4 + 2;
158 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
159 symlen);
160 }
161
Stephane Eranian98a3b322013-01-24 16:10:35 +0100162 if (h->mem_info->daddr.map) {
163 symlen = dso__name_len(h->mem_info->daddr.map->dso);
164 hists__new_col_len(hists, HISTC_MEM_DADDR_DSO,
165 symlen);
166 } else {
167 symlen = unresolved_col_width + 4 + 2;
168 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
169 }
170 } else {
171 symlen = unresolved_col_width + 4 + 2;
172 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen);
Jiri Olsab34b3bf2015-10-05 20:06:08 +0200173 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL, symlen);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100174 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
175 }
176
Hari Bathinid890a982017-03-08 02:12:13 +0530177 hists__new_col_len(hists, HISTC_CGROUP_ID, 20);
Arnaldo Carvalho de Meloa4978ec2015-09-09 12:14:00 -0300178 hists__new_col_len(hists, HISTC_CPU, 3);
Kan Liang2e7ea3a2015-09-04 10:45:43 -0400179 hists__new_col_len(hists, HISTC_SOCKET, 6);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100180 hists__new_col_len(hists, HISTC_MEM_LOCKED, 6);
181 hists__new_col_len(hists, HISTC_MEM_TLB, 22);
182 hists__new_col_len(hists, HISTC_MEM_SNOOP, 12);
183 hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3);
184 hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12);
185 hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12);
Andi Kleen475eeab2013-09-20 07:40:43 -0700186
Jiri Olsaf666ac02016-09-19 15:10:10 +0200187 if (h->srcline) {
188 len = MAX(strlen(h->srcline), strlen(sort_srcline.se_header));
189 hists__new_col_len(hists, HISTC_SRCLINE, len);
190 }
Arnaldo Carvalho de Meloe8e6d372015-08-10 16:53:54 -0300191
Andi Kleen31191a82015-08-07 15:54:24 -0700192 if (h->srcfile)
193 hists__new_col_len(hists, HISTC_SRCFILE, strlen(h->srcfile));
194
Andi Kleen475eeab2013-09-20 07:40:43 -0700195 if (h->transaction)
196 hists__new_col_len(hists, HISTC_TRANSACTION,
197 hist_entry__transaction_len());
Namhyung Kim0c0af782016-02-21 23:22:38 +0900198
199 if (h->trace_output)
200 hists__new_col_len(hists, HISTC_TRACE, strlen(h->trace_output));
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300201}
202
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900203void hists__output_recalc_col_len(struct hists *hists, int max_rows)
204{
205 struct rb_node *next = rb_first(&hists->entries);
206 struct hist_entry *n;
207 int row = 0;
208
209 hists__reset_col_len(hists);
210
211 while (next && row++ < max_rows) {
212 n = rb_entry(next, struct hist_entry, rb_node);
213 if (!n->filtered)
214 hists__calc_col_len(hists, n);
215 next = rb_next(&n->rb_node);
216 }
217}
218
Namhyung Kimf39056f2014-01-14 14:25:37 +0900219static void he_stat__add_cpumode_period(struct he_stat *he_stat,
220 unsigned int cpumode, u64 period)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800221{
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300222 switch (cpumode) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800223 case PERF_RECORD_MISC_KERNEL:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900224 he_stat->period_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800225 break;
226 case PERF_RECORD_MISC_USER:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900227 he_stat->period_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800228 break;
229 case PERF_RECORD_MISC_GUEST_KERNEL:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900230 he_stat->period_guest_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800231 break;
232 case PERF_RECORD_MISC_GUEST_USER:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900233 he_stat->period_guest_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800234 break;
235 default:
236 break;
237 }
238}
239
Andi Kleen05484292013-01-24 16:10:29 +0100240static void he_stat__add_period(struct he_stat *he_stat, u64 period,
241 u64 weight)
Namhyung Kim139c0812012-10-04 21:49:43 +0900242{
Stephane Eranian98a3b322013-01-24 16:10:35 +0100243
Namhyung Kim139c0812012-10-04 21:49:43 +0900244 he_stat->period += period;
Andi Kleen05484292013-01-24 16:10:29 +0100245 he_stat->weight += weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900246 he_stat->nr_events += 1;
247}
248
249static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
250{
251 dest->period += src->period;
252 dest->period_sys += src->period_sys;
253 dest->period_us += src->period_us;
254 dest->period_guest_sys += src->period_guest_sys;
255 dest->period_guest_us += src->period_guest_us;
256 dest->nr_events += src->nr_events;
Andi Kleen05484292013-01-24 16:10:29 +0100257 dest->weight += src->weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900258}
259
Namhyung Kimf39056f2014-01-14 14:25:37 +0900260static void he_stat__decay(struct he_stat *he_stat)
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300261{
Namhyung Kimf39056f2014-01-14 14:25:37 +0900262 he_stat->period = (he_stat->period * 7) / 8;
263 he_stat->nr_events = (he_stat->nr_events * 7) / 8;
Andi Kleen05484292013-01-24 16:10:29 +0100264 /* XXX need decay for weight too? */
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300265}
266
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900267static void hists__delete_entry(struct hists *hists, struct hist_entry *he);
268
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300269static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
270{
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900271 u64 prev_period = he->stat.period;
Namhyung Kim3186b682014-04-22 13:44:23 +0900272 u64 diff;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200273
274 if (prev_period == 0)
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300275 return true;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200276
Namhyung Kimf39056f2014-01-14 14:25:37 +0900277 he_stat__decay(&he->stat);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900278 if (symbol_conf.cumulate_callchain)
279 he_stat__decay(he->stat_acc);
Namhyung Kim42b276a2016-01-05 12:06:00 +0900280 decay_callchain(he->callchain);
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200281
Namhyung Kim3186b682014-04-22 13:44:23 +0900282 diff = prev_period - he->stat.period;
283
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900284 if (!he->depth) {
285 hists->stats.total_period -= diff;
286 if (!he->filtered)
287 hists->stats.total_non_filtered_period -= diff;
288 }
289
290 if (!he->leaf) {
291 struct hist_entry *child;
292 struct rb_node *node = rb_first(&he->hroot_out);
293 while (node) {
294 child = rb_entry(node, struct hist_entry, rb_node);
295 node = rb_next(node);
296
297 if (hists__decay_entry(hists, child))
298 hists__delete_entry(hists, child);
299 }
300 }
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200301
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900302 return he->stat.period == 0;
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300303}
304
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300305static void hists__delete_entry(struct hists *hists, struct hist_entry *he)
306{
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900307 struct rb_root *root_in;
308 struct rb_root *root_out;
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300309
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900310 if (he->parent_he) {
311 root_in = &he->parent_he->hroot_in;
312 root_out = &he->parent_he->hroot_out;
313 } else {
Jiri Olsa52225032016-05-03 13:54:42 +0200314 if (hists__has(hists, need_collapse))
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900315 root_in = &hists->entries_collapsed;
316 else
317 root_in = hists->entries_in;
318 root_out = &hists->entries;
319 }
320
321 rb_erase(&he->rb_node_in, root_in);
322 rb_erase(&he->rb_node, root_out);
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300323
324 --hists->nr_entries;
325 if (!he->filtered)
326 --hists->nr_non_filtered_entries;
327
328 hist_entry__delete(he);
329}
330
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900331void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300332{
333 struct rb_node *next = rb_first(&hists->entries);
334 struct hist_entry *n;
335
336 while (next) {
337 n = rb_entry(next, struct hist_entry, rb_node);
338 next = rb_next(&n->rb_node);
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200339 if (((zap_user && n->level == '.') ||
340 (zap_kernel && n->level != '.') ||
Arnaldo Carvalho de Melo4c47f4f2015-03-17 17:18:58 -0300341 hists__decay_entry(hists, n))) {
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300342 hists__delete_entry(hists, n);
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300343 }
344 }
345}
346
Namhyung Kim701937b2014-08-12 17:16:05 +0900347void hists__delete_entries(struct hists *hists)
348{
349 struct rb_node *next = rb_first(&hists->entries);
350 struct hist_entry *n;
351
352 while (next) {
353 n = rb_entry(next, struct hist_entry, rb_node);
354 next = rb_next(&n->rb_node);
355
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300356 hists__delete_entry(hists, n);
Namhyung Kim701937b2014-08-12 17:16:05 +0900357 }
358}
359
John Kacur3d1d07e2009-09-28 15:32:55 +0200360/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300361 * histogram, sorted on item, collects periods
John Kacur3d1d07e2009-09-28 15:32:55 +0200362 */
363
Jiri Olsa0a269a62016-07-05 08:56:03 +0200364static int hist_entry__init(struct hist_entry *he,
365 struct hist_entry *template,
366 bool sample_self)
367{
368 *he = *template;
369
370 if (symbol_conf.cumulate_callchain) {
371 he->stat_acc = malloc(sizeof(he->stat));
372 if (he->stat_acc == NULL)
373 return -ENOMEM;
374 memcpy(he->stat_acc, &he->stat, sizeof(he->stat));
375 if (!sample_self)
376 memset(&he->stat, 0, sizeof(he->stat));
377 }
378
379 map__get(he->ms.map);
380
381 if (he->branch_info) {
382 /*
383 * This branch info is (a part of) allocated from
384 * sample__resolve_bstack() and will be freed after
385 * adding new entries. So we need to save a copy.
386 */
387 he->branch_info = malloc(sizeof(*he->branch_info));
388 if (he->branch_info == NULL) {
389 map__zput(he->ms.map);
390 free(he->stat_acc);
391 return -ENOMEM;
392 }
393
394 memcpy(he->branch_info, template->branch_info,
395 sizeof(*he->branch_info));
396
397 map__get(he->branch_info->from.map);
398 map__get(he->branch_info->to.map);
399 }
400
401 if (he->mem_info) {
402 map__get(he->mem_info->iaddr.map);
403 map__get(he->mem_info->daddr.map);
404 }
405
406 if (symbol_conf.use_callchain)
407 callchain_init(he->callchain);
408
409 if (he->raw_data) {
410 he->raw_data = memdup(he->raw_data, he->raw_size);
411
412 if (he->raw_data == NULL) {
413 map__put(he->ms.map);
414 if (he->branch_info) {
415 map__put(he->branch_info->from.map);
416 map__put(he->branch_info->to.map);
417 free(he->branch_info);
418 }
419 if (he->mem_info) {
420 map__put(he->mem_info->iaddr.map);
421 map__put(he->mem_info->daddr.map);
422 }
423 free(he->stat_acc);
424 return -ENOMEM;
425 }
426 }
427 INIT_LIST_HEAD(&he->pairs.node);
428 thread__get(he->thread);
Namhyung Kimd2580c72016-09-13 16:45:48 +0900429 he->hroot_in = RB_ROOT;
430 he->hroot_out = RB_ROOT;
Jiri Olsa0a269a62016-07-05 08:56:03 +0200431
432 if (!symbol_conf.report_hierarchy)
433 he->leaf = true;
434
435 return 0;
436}
437
Jiri Olsaf542e762016-07-05 08:56:04 +0200438static void *hist_entry__zalloc(size_t size)
439{
440 return zalloc(size + sizeof(struct hist_entry));
441}
442
443static void hist_entry__free(void *ptr)
444{
445 free(ptr);
446}
447
448static struct hist_entry_ops default_ops = {
449 .new = hist_entry__zalloc,
450 .free = hist_entry__free,
451};
452
Namhyung Kima0b51af2012-09-11 13:34:27 +0900453static struct hist_entry *hist_entry__new(struct hist_entry *template,
454 bool sample_self)
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300455{
Jiri Olsaf542e762016-07-05 08:56:04 +0200456 struct hist_entry_ops *ops = template->ops;
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900457 size_t callchain_size = 0;
458 struct hist_entry *he;
Jiri Olsa0a269a62016-07-05 08:56:03 +0200459 int err = 0;
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900460
Jiri Olsaf542e762016-07-05 08:56:04 +0200461 if (!ops)
462 ops = template->ops = &default_ops;
463
Namhyung Kim82aa0192014-12-22 13:44:14 +0900464 if (symbol_conf.use_callchain)
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900465 callchain_size = sizeof(struct callchain_root);
466
Jiri Olsaf542e762016-07-05 08:56:04 +0200467 he = ops->new(callchain_size);
Jiri Olsa0a269a62016-07-05 08:56:03 +0200468 if (he) {
469 err = hist_entry__init(he, template, sample_self);
Jiri Olsaf542e762016-07-05 08:56:04 +0200470 if (err) {
471 ops->free(he);
472 he = NULL;
473 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300474 }
475
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200476 return he;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300477}
478
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300479static u8 symbol__parent_filter(const struct symbol *parent)
480{
481 if (symbol_conf.exclude_other && parent == NULL)
482 return 1 << HIST_FILTER__PARENT;
483 return 0;
484}
485
Namhyung Kim467ef102016-02-16 23:08:19 +0900486static void hist_entry__add_callchain_period(struct hist_entry *he, u64 period)
487{
488 if (!symbol_conf.use_callchain)
489 return;
490
491 he->hists->callchain_period += period;
492 if (!he->filtered)
493 he->hists->callchain_non_filtered_period += period;
494}
495
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300496static struct hist_entry *hists__findnew_entry(struct hists *hists,
497 struct hist_entry *entry,
498 struct addr_location *al,
499 bool sample_self)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300500{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300501 struct rb_node **p;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300502 struct rb_node *parent = NULL;
503 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -0700504 int64_t cmp;
Namhyung Kimf1cbf782013-12-18 14:21:11 +0900505 u64 period = entry->stat.period;
506 u64 weight = entry->stat.weight;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300507
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300508 p = &hists->entries_in->rb_node;
509
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300510 while (*p != NULL) {
511 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300512 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300513
Namhyung Kim9afcf932012-12-10 17:29:54 +0900514 /*
515 * Make sure that it receives arguments in a same order as
516 * hist_entry__collapse() so that we can use an appropriate
517 * function when searching an entry regardless which sort
518 * keys were used.
519 */
520 cmp = hist_entry__cmp(he, entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300521
522 if (!cmp) {
Namhyung Kim0f584742016-01-28 00:40:49 +0900523 if (sample_self) {
Namhyung Kima0b51af2012-09-11 13:34:27 +0900524 he_stat__add_period(&he->stat, period, weight);
Namhyung Kim467ef102016-02-16 23:08:19 +0900525 hist_entry__add_callchain_period(he, period);
Namhyung Kim0f584742016-01-28 00:40:49 +0900526 }
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900527 if (symbol_conf.cumulate_callchain)
528 he_stat__add_period(he->stat_acc, period, weight);
David Miller63fa4712012-03-27 03:14:18 -0400529
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900530 /*
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -0300531 * This mem info was allocated from sample__resolve_mem
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900532 * and will not be used anymore.
533 */
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300534 zfree(&entry->mem_info);
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900535
David Miller63fa4712012-03-27 03:14:18 -0400536 /* If the map of an existing hist_entry has
537 * become out-of-date due to an exec() or
538 * similar, update it. Otherwise we will
539 * mis-adjust symbol addresses when computing
540 * the history counter to increment.
541 */
542 if (he->ms.map != entry->ms.map) {
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300543 map__put(he->ms.map);
544 he->ms.map = map__get(entry->ms.map);
David Miller63fa4712012-03-27 03:14:18 -0400545 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300546 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300547 }
548
549 if (cmp < 0)
550 p = &(*p)->rb_left;
551 else
552 p = &(*p)->rb_right;
553 }
554
Namhyung Kima0b51af2012-09-11 13:34:27 +0900555 he = hist_entry__new(entry, sample_self);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300556 if (!he)
Namhyung Kim27a0dcb2013-05-14 11:09:02 +0900557 return NULL;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300558
Namhyung Kim0f584742016-01-28 00:40:49 +0900559 if (sample_self)
Namhyung Kim467ef102016-02-16 23:08:19 +0900560 hist_entry__add_callchain_period(he, period);
561 hists->nr_entries++;
Namhyung Kim590cd342014-12-22 13:44:09 +0900562
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300563 rb_link_node(&he->rb_node_in, parent, p);
564 rb_insert_color(&he->rb_node_in, hists->entries_in);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300565out:
Namhyung Kima0b51af2012-09-11 13:34:27 +0900566 if (sample_self)
567 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900568 if (symbol_conf.cumulate_callchain)
569 he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300570 return he;
571}
572
Jiri Olsaa5051972016-07-05 08:56:05 +0200573static struct hist_entry*
574__hists__add_entry(struct hists *hists,
575 struct addr_location *al,
576 struct symbol *sym_parent,
577 struct branch_info *bi,
578 struct mem_info *mi,
579 struct perf_sample *sample,
580 bool sample_self,
581 struct hist_entry_ops *ops)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100582{
Hari Bathinid890a982017-03-08 02:12:13 +0530583 struct namespaces *ns = thread__namespaces(al->thread);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100584 struct hist_entry entry = {
585 .thread = al->thread,
Namhyung Kim4dfced32013-09-13 16:28:57 +0900586 .comm = thread__comm(al->thread),
Hari Bathinid890a982017-03-08 02:12:13 +0530587 .cgroup_id = {
588 .dev = ns ? ns->link_info[CGROUP_NS_INDEX].dev : 0,
589 .ino = ns ? ns->link_info[CGROUP_NS_INDEX].ino : 0,
590 },
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100591 .ms = {
592 .map = al->map,
593 .sym = al->sym,
594 },
Kan Liang0c4c4deb2015-09-04 10:45:42 -0400595 .socket = al->socket,
Don Zickus7365be52014-05-27 12:28:05 -0400596 .cpu = al->cpu,
597 .cpumode = al->cpumode,
598 .ip = al->addr,
599 .level = al->level,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900600 .stat = {
Namhyung Kimc4b35352012-10-04 21:49:42 +0900601 .nr_events = 1,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900602 .period = sample->period,
603 .weight = sample->weight,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900604 },
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100605 .parent = sym_parent,
Namhyung Kim2c86c7c2014-03-17 18:18:54 -0300606 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300607 .hists = hists,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900608 .branch_info = bi,
609 .mem_info = mi,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900610 .transaction = sample->transaction,
Namhyung Kim72392832015-12-24 11:16:17 +0900611 .raw_data = sample->raw_data,
612 .raw_size = sample->raw_size,
Jiri Olsaa5051972016-07-05 08:56:05 +0200613 .ops = ops,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100614 };
615
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300616 return hists__findnew_entry(hists, &entry, al, sample_self);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100617}
618
Jiri Olsaa5051972016-07-05 08:56:05 +0200619struct hist_entry *hists__add_entry(struct hists *hists,
620 struct addr_location *al,
621 struct symbol *sym_parent,
622 struct branch_info *bi,
623 struct mem_info *mi,
624 struct perf_sample *sample,
625 bool sample_self)
626{
627 return __hists__add_entry(hists, al, sym_parent, bi, mi,
628 sample, sample_self, NULL);
629}
630
631struct hist_entry *hists__add_entry_ops(struct hists *hists,
632 struct hist_entry_ops *ops,
633 struct addr_location *al,
634 struct symbol *sym_parent,
635 struct branch_info *bi,
636 struct mem_info *mi,
637 struct perf_sample *sample,
638 bool sample_self)
639{
640 return __hists__add_entry(hists, al, sym_parent, bi, mi,
641 sample, sample_self, ops);
642}
643
Namhyung Kim69bcb012013-10-30 09:40:34 +0900644static int
645iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
646 struct addr_location *al __maybe_unused)
647{
648 return 0;
649}
650
651static int
652iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
653 struct addr_location *al __maybe_unused)
654{
655 return 0;
656}
657
658static int
659iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
660{
661 struct perf_sample *sample = iter->sample;
662 struct mem_info *mi;
663
664 mi = sample__resolve_mem(sample, al);
665 if (mi == NULL)
666 return -ENOMEM;
667
668 iter->priv = mi;
669 return 0;
670}
671
672static int
673iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
674{
675 u64 cost;
676 struct mem_info *mi = iter->priv;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300677 struct hists *hists = evsel__hists(iter->evsel);
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900678 struct perf_sample *sample = iter->sample;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900679 struct hist_entry *he;
680
681 if (mi == NULL)
682 return -EINVAL;
683
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900684 cost = sample->weight;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900685 if (!cost)
686 cost = 1;
687
688 /*
689 * must pass period=weight in order to get the correct
690 * sorting from hists__collapse_resort() which is solely
691 * based on periods. We want sorting be done on nr_events * weight
692 * and this is indirectly achieved by passing period=weight here
693 * and the he_stat__add_period() function.
694 */
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900695 sample->period = cost;
696
Jiri Olsa0102ef32016-06-14 20:19:21 +0200697 he = hists__add_entry(hists, al, iter->parent, NULL, mi,
698 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900699 if (!he)
700 return -ENOMEM;
701
702 iter->he = he;
703 return 0;
704}
705
706static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900707iter_finish_mem_entry(struct hist_entry_iter *iter,
708 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900709{
710 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300711 struct hists *hists = evsel__hists(evsel);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900712 struct hist_entry *he = iter->he;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900713 int err = -EINVAL;
714
715 if (he == NULL)
716 goto out;
717
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300718 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900719
720 err = hist_entry__append_callchain(he, iter->sample);
721
722out:
723 /*
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300724 * We don't need to free iter->priv (mem_info) here since the mem info
725 * was either already freed in hists__findnew_entry() or passed to a
726 * new hist entry by hist_entry__new().
Namhyung Kim69bcb012013-10-30 09:40:34 +0900727 */
728 iter->priv = NULL;
729
730 iter->he = NULL;
731 return err;
732}
733
734static int
735iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
736{
737 struct branch_info *bi;
738 struct perf_sample *sample = iter->sample;
739
740 bi = sample__resolve_bstack(sample, al);
741 if (!bi)
742 return -ENOMEM;
743
744 iter->curr = 0;
745 iter->total = sample->branch_stack->nr;
746
747 iter->priv = bi;
748 return 0;
749}
750
751static int
Jin Yao2d78b182017-07-18 20:13:14 +0800752iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused,
Namhyung Kim69bcb012013-10-30 09:40:34 +0900753 struct addr_location *al __maybe_unused)
754{
755 return 0;
756}
757
758static int
759iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
760{
761 struct branch_info *bi = iter->priv;
762 int i = iter->curr;
763
764 if (bi == NULL)
765 return 0;
766
767 if (iter->curr >= iter->total)
768 return 0;
769
770 al->map = bi[i].to.map;
771 al->sym = bi[i].to.sym;
772 al->addr = bi[i].to.addr;
773 return 1;
774}
775
776static int
777iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
778{
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900779 struct branch_info *bi;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900780 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300781 struct hists *hists = evsel__hists(evsel);
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900782 struct perf_sample *sample = iter->sample;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900783 struct hist_entry *he = NULL;
784 int i = iter->curr;
785 int err = 0;
786
787 bi = iter->priv;
788
789 if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym))
790 goto out;
791
792 /*
793 * The report shows the percentage of total branches captured
794 * and not events sampled. Thus we use a pseudo period of 1.
795 */
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900796 sample->period = 1;
797 sample->weight = bi->flags.cycles ? bi->flags.cycles : 1;
798
Jiri Olsa0102ef32016-06-14 20:19:21 +0200799 he = hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
800 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900801 if (he == NULL)
802 return -ENOMEM;
803
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300804 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900805
806out:
807 iter->he = he;
808 iter->curr++;
809 return err;
810}
811
812static int
813iter_finish_branch_entry(struct hist_entry_iter *iter,
814 struct addr_location *al __maybe_unused)
815{
816 zfree(&iter->priv);
817 iter->he = NULL;
818
819 return iter->curr >= iter->total ? 0 : -1;
820}
821
822static int
823iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
824 struct addr_location *al __maybe_unused)
825{
826 return 0;
827}
828
829static int
830iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
831{
832 struct perf_evsel *evsel = iter->evsel;
833 struct perf_sample *sample = iter->sample;
834 struct hist_entry *he;
835
Jiri Olsa0102ef32016-06-14 20:19:21 +0200836 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
837 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900838 if (he == NULL)
839 return -ENOMEM;
840
841 iter->he = he;
842 return 0;
843}
844
845static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900846iter_finish_normal_entry(struct hist_entry_iter *iter,
847 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900848{
Namhyung Kim69bcb012013-10-30 09:40:34 +0900849 struct hist_entry *he = iter->he;
850 struct perf_evsel *evsel = iter->evsel;
851 struct perf_sample *sample = iter->sample;
852
853 if (he == NULL)
854 return 0;
855
856 iter->he = NULL;
857
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300858 hists__inc_nr_samples(evsel__hists(evsel), he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900859
860 return hist_entry__append_callchain(he, sample);
861}
862
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900863static int
Adrian Hunter96b40f32015-09-25 16:15:47 +0300864iter_prepare_cumulative_entry(struct hist_entry_iter *iter,
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900865 struct addr_location *al __maybe_unused)
866{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900867 struct hist_entry **he_cache;
868
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900869 callchain_cursor_commit(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900870
871 /*
872 * This is for detecting cycles or recursions so that they're
873 * cumulated only one time to prevent entries more than 100%
874 * overhead.
875 */
Adrian Hunter96b40f32015-09-25 16:15:47 +0300876 he_cache = malloc(sizeof(*he_cache) * (iter->max_stack + 1));
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900877 if (he_cache == NULL)
878 return -ENOMEM;
879
880 iter->priv = he_cache;
881 iter->curr = 0;
882
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900883 return 0;
884}
885
886static int
887iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
888 struct addr_location *al)
889{
890 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300891 struct hists *hists = evsel__hists(evsel);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900892 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900893 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900894 struct hist_entry *he;
895 int err = 0;
896
Jiri Olsa0102ef32016-06-14 20:19:21 +0200897 he = hists__add_entry(hists, al, iter->parent, NULL, NULL,
898 sample, true);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900899 if (he == NULL)
900 return -ENOMEM;
901
902 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900903 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900904
Namhyung Kim82aa0192014-12-22 13:44:14 +0900905 hist_entry__append_callchain(he, sample);
Namhyung Kimbe7f8552013-12-26 17:44:10 +0900906
907 /*
908 * We need to re-initialize the cursor since callchain_append()
909 * advanced the cursor to the end.
910 */
911 callchain_cursor_commit(&callchain_cursor);
912
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300913 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900914
915 return err;
916}
917
918static int
919iter_next_cumulative_entry(struct hist_entry_iter *iter,
920 struct addr_location *al)
921{
922 struct callchain_cursor_node *node;
923
924 node = callchain_cursor_current(&callchain_cursor);
925 if (node == NULL)
926 return 0;
927
Namhyung Kimc7405d82013-10-31 13:58:30 +0900928 return fill_callchain_info(al, node, iter->hide_unresolved);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900929}
930
931static int
932iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
933 struct addr_location *al)
934{
935 struct perf_evsel *evsel = iter->evsel;
936 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900937 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900938 struct hist_entry *he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900939 struct hist_entry he_tmp = {
Arnaldo Carvalho de Melo5cef8972015-08-10 15:45:55 -0300940 .hists = evsel__hists(evsel),
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900941 .cpu = al->cpu,
942 .thread = al->thread,
943 .comm = thread__comm(al->thread),
944 .ip = al->addr,
945 .ms = {
946 .map = al->map,
947 .sym = al->sym,
948 },
949 .parent = iter->parent,
Namhyung Kim72392832015-12-24 11:16:17 +0900950 .raw_data = sample->raw_data,
951 .raw_size = sample->raw_size,
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900952 };
953 int i;
Namhyung Kimbe7f8552013-12-26 17:44:10 +0900954 struct callchain_cursor cursor;
955
956 callchain_cursor_snapshot(&cursor, &callchain_cursor);
957
958 callchain_cursor_advance(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900959
960 /*
961 * Check if there's duplicate entries in the callchain.
962 * It's possible that it has cycles or recursive calls.
963 */
964 for (i = 0; i < iter->curr; i++) {
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900965 if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
966 /* to avoid calling callback function */
967 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900968 return 0;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900969 }
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900970 }
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900971
Jiri Olsa0102ef32016-06-14 20:19:21 +0200972 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
973 sample, false);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900974 if (he == NULL)
975 return -ENOMEM;
976
977 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900978 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900979
Namhyung Kim82aa0192014-12-22 13:44:14 +0900980 if (symbol_conf.use_callchain)
981 callchain_append(he->callchain, &cursor, sample->period);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900982 return 0;
983}
984
985static int
986iter_finish_cumulative_entry(struct hist_entry_iter *iter,
987 struct addr_location *al __maybe_unused)
988{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900989 zfree(&iter->priv);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900990 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900991
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900992 return 0;
993}
994
Namhyung Kim69bcb012013-10-30 09:40:34 +0900995const struct hist_iter_ops hist_iter_mem = {
996 .prepare_entry = iter_prepare_mem_entry,
997 .add_single_entry = iter_add_single_mem_entry,
998 .next_entry = iter_next_nop_entry,
999 .add_next_entry = iter_add_next_nop_entry,
1000 .finish_entry = iter_finish_mem_entry,
1001};
1002
1003const struct hist_iter_ops hist_iter_branch = {
1004 .prepare_entry = iter_prepare_branch_entry,
1005 .add_single_entry = iter_add_single_branch_entry,
1006 .next_entry = iter_next_branch_entry,
1007 .add_next_entry = iter_add_next_branch_entry,
1008 .finish_entry = iter_finish_branch_entry,
1009};
1010
1011const struct hist_iter_ops hist_iter_normal = {
1012 .prepare_entry = iter_prepare_normal_entry,
1013 .add_single_entry = iter_add_single_normal_entry,
1014 .next_entry = iter_next_nop_entry,
1015 .add_next_entry = iter_add_next_nop_entry,
1016 .finish_entry = iter_finish_normal_entry,
1017};
1018
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001019const struct hist_iter_ops hist_iter_cumulative = {
1020 .prepare_entry = iter_prepare_cumulative_entry,
1021 .add_single_entry = iter_add_single_cumulative_entry,
1022 .next_entry = iter_next_cumulative_entry,
1023 .add_next_entry = iter_add_next_cumulative_entry,
1024 .finish_entry = iter_finish_cumulative_entry,
1025};
1026
Namhyung Kim69bcb012013-10-30 09:40:34 +09001027int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001028 int max_stack_depth, void *arg)
Namhyung Kim69bcb012013-10-30 09:40:34 +09001029{
1030 int err, err2;
Krister Johansen9c68ae92017-01-05 22:23:31 -08001031 struct map *alm = NULL;
1032
1033 if (al && al->map)
1034 alm = map__get(al->map);
Namhyung Kim69bcb012013-10-30 09:40:34 +09001035
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001036 err = sample__resolve_callchain(iter->sample, &callchain_cursor, &iter->parent,
Namhyung Kim063bd932015-05-19 17:04:10 +09001037 iter->evsel, al, max_stack_depth);
Namhyung Kim69bcb012013-10-30 09:40:34 +09001038 if (err)
1039 return err;
1040
Adrian Hunter96b40f32015-09-25 16:15:47 +03001041 iter->max_stack = max_stack_depth;
1042
Namhyung Kim69bcb012013-10-30 09:40:34 +09001043 err = iter->ops->prepare_entry(iter, al);
1044 if (err)
1045 goto out;
1046
1047 err = iter->ops->add_single_entry(iter, al);
1048 if (err)
1049 goto out;
1050
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001051 if (iter->he && iter->add_entry_cb) {
1052 err = iter->add_entry_cb(iter, al, true, arg);
1053 if (err)
1054 goto out;
1055 }
1056
Namhyung Kim69bcb012013-10-30 09:40:34 +09001057 while (iter->ops->next_entry(iter, al)) {
1058 err = iter->ops->add_next_entry(iter, al);
1059 if (err)
1060 break;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001061
1062 if (iter->he && iter->add_entry_cb) {
1063 err = iter->add_entry_cb(iter, al, false, arg);
1064 if (err)
1065 goto out;
1066 }
Namhyung Kim69bcb012013-10-30 09:40:34 +09001067 }
1068
1069out:
1070 err2 = iter->ops->finish_entry(iter, al);
1071 if (!err)
1072 err = err2;
1073
Krister Johansen9c68ae92017-01-05 22:23:31 -08001074 map__put(alm);
1075
Namhyung Kim69bcb012013-10-30 09:40:34 +09001076 return err;
1077}
1078
John Kacur3d1d07e2009-09-28 15:32:55 +02001079int64_t
1080hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
1081{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001082 struct hists *hists = left->hists;
Namhyung Kim093f0ef32014-03-03 12:07:47 +09001083 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +02001084 int64_t cmp = 0;
1085
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001086 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim84b6ee82016-02-27 03:52:43 +09001087 if (perf_hpp__is_dynamic_entry(fmt) &&
1088 !perf_hpp__defined_dynamic_entry(fmt, hists))
1089 continue;
1090
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001091 cmp = fmt->cmp(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +02001092 if (cmp)
1093 break;
1094 }
1095
1096 return cmp;
1097}
1098
1099int64_t
1100hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
1101{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001102 struct hists *hists = left->hists;
Namhyung Kim093f0ef32014-03-03 12:07:47 +09001103 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +02001104 int64_t cmp = 0;
1105
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001106 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim84b6ee82016-02-27 03:52:43 +09001107 if (perf_hpp__is_dynamic_entry(fmt) &&
1108 !perf_hpp__defined_dynamic_entry(fmt, hists))
1109 continue;
1110
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001111 cmp = fmt->collapse(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +02001112 if (cmp)
1113 break;
1114 }
1115
1116 return cmp;
1117}
1118
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -03001119void hist_entry__delete(struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +02001120{
Jiri Olsaf542e762016-07-05 08:56:04 +02001121 struct hist_entry_ops *ops = he->ops;
1122
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001123 thread__zput(he->thread);
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -03001124 map__zput(he->ms.map);
1125
1126 if (he->branch_info) {
1127 map__zput(he->branch_info->from.map);
1128 map__zput(he->branch_info->to.map);
Andi Kleen508be0d2016-05-20 13:15:08 -07001129 free_srcline(he->branch_info->srcline_from);
1130 free_srcline(he->branch_info->srcline_to);
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -03001131 zfree(&he->branch_info);
1132 }
1133
1134 if (he->mem_info) {
1135 map__zput(he->mem_info->iaddr.map);
1136 map__zput(he->mem_info->daddr.map);
1137 zfree(&he->mem_info);
1138 }
1139
Jin Yao0d3eb0b2017-03-26 04:34:29 +08001140 if (he->inline_node) {
1141 inline_node__delete(he->inline_node);
1142 he->inline_node = NULL;
1143 }
1144
Namhyung Kimf8be1c82012-09-11 13:15:07 +09001145 zfree(&he->stat_acc);
Namhyung Kimf048d542013-09-11 14:09:28 +09001146 free_srcline(he->srcline);
Andi Kleen31191a82015-08-07 15:54:24 -07001147 if (he->srcfile && he->srcfile[0])
1148 free(he->srcfile);
Namhyung Kimd1149602014-12-30 14:38:13 +09001149 free_callchain(he->callchain);
Namhyung Kim60517d22015-12-23 02:07:03 +09001150 free(he->trace_output);
Namhyung Kim72392832015-12-24 11:16:17 +09001151 free(he->raw_data);
Jiri Olsaf542e762016-07-05 08:56:04 +02001152 ops->free(he);
John Kacur3d1d07e2009-09-28 15:32:55 +02001153}
1154
1155/*
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -03001156 * If this is not the last column, then we need to pad it according to the
1157 * pre-calculated max lenght for this column, otherwise don't bother adding
1158 * spaces because that would break viewing this with, for instance, 'less',
1159 * that would show tons of trailing spaces when a long C++ demangled method
1160 * names is sampled.
1161*/
1162int hist_entry__snprintf_alignment(struct hist_entry *he, struct perf_hpp *hpp,
1163 struct perf_hpp_fmt *fmt, int printed)
1164{
1165 if (!list_is_last(&fmt->list, &he->hists->hpp_list->fields)) {
Jiri Olsada1b0402016-06-14 20:19:20 +02001166 const int width = fmt->width(fmt, hpp, he->hists);
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -03001167 if (printed < width) {
1168 advance_hpp(hpp, printed);
1169 printed = scnprintf(hpp->buf, hpp->size, "%-*s", width - printed, " ");
1170 }
1171 }
1172
1173 return printed;
1174}
1175
1176/*
John Kacur3d1d07e2009-09-28 15:32:55 +02001177 * collapse the histogram
1178 */
1179
Namhyung Kimaef810e2016-02-25 00:13:34 +09001180static void hists__apply_filters(struct hists *hists, struct hist_entry *he);
Namhyung Kimaec13a72016-03-09 22:46:58 +09001181static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *he,
1182 enum hist_filter type);
1183
1184typedef bool (*fmt_chk_fn)(struct perf_hpp_fmt *fmt);
1185
1186static bool check_thread_entry(struct perf_hpp_fmt *fmt)
1187{
1188 return perf_hpp__is_thread_entry(fmt) || perf_hpp__is_comm_entry(fmt);
1189}
1190
1191static void hist_entry__check_and_remove_filter(struct hist_entry *he,
1192 enum hist_filter type,
1193 fmt_chk_fn check)
1194{
1195 struct perf_hpp_fmt *fmt;
1196 bool type_match = false;
1197 struct hist_entry *parent = he->parent_he;
1198
1199 switch (type) {
1200 case HIST_FILTER__THREAD:
1201 if (symbol_conf.comm_list == NULL &&
1202 symbol_conf.pid_list == NULL &&
1203 symbol_conf.tid_list == NULL)
1204 return;
1205 break;
1206 case HIST_FILTER__DSO:
1207 if (symbol_conf.dso_list == NULL)
1208 return;
1209 break;
1210 case HIST_FILTER__SYMBOL:
1211 if (symbol_conf.sym_list == NULL)
1212 return;
1213 break;
1214 case HIST_FILTER__PARENT:
1215 case HIST_FILTER__GUEST:
1216 case HIST_FILTER__HOST:
1217 case HIST_FILTER__SOCKET:
Jiri Olsa9857b712016-08-17 14:55:23 +02001218 case HIST_FILTER__C2C:
Namhyung Kimaec13a72016-03-09 22:46:58 +09001219 default:
1220 return;
1221 }
1222
1223 /* if it's filtered by own fmt, it has to have filter bits */
1224 perf_hpp_list__for_each_format(he->hpp_list, fmt) {
1225 if (check(fmt)) {
1226 type_match = true;
1227 break;
1228 }
1229 }
1230
1231 if (type_match) {
1232 /*
1233 * If the filter is for current level entry, propagate
1234 * filter marker to parents. The marker bit was
1235 * already set by default so it only needs to clear
1236 * non-filtered entries.
1237 */
1238 if (!(he->filtered & (1 << type))) {
1239 while (parent) {
1240 parent->filtered &= ~(1 << type);
1241 parent = parent->parent_he;
1242 }
1243 }
1244 } else {
1245 /*
1246 * If current entry doesn't have matching formats, set
1247 * filter marker for upper level entries. it will be
1248 * cleared if its lower level entries is not filtered.
1249 *
1250 * For lower-level entries, it inherits parent's
1251 * filter bit so that lower level entries of a
1252 * non-filtered entry won't set the filter marker.
1253 */
1254 if (parent == NULL)
1255 he->filtered |= (1 << type);
1256 else
1257 he->filtered |= (parent->filtered & (1 << type));
1258 }
1259}
1260
1261static void hist_entry__apply_hierarchy_filters(struct hist_entry *he)
1262{
1263 hist_entry__check_and_remove_filter(he, HIST_FILTER__THREAD,
1264 check_thread_entry);
1265
1266 hist_entry__check_and_remove_filter(he, HIST_FILTER__DSO,
1267 perf_hpp__is_dso_entry);
1268
1269 hist_entry__check_and_remove_filter(he, HIST_FILTER__SYMBOL,
1270 perf_hpp__is_sym_entry);
1271
1272 hists__apply_filters(he->hists, he);
1273}
Namhyung Kimaef810e2016-02-25 00:13:34 +09001274
1275static struct hist_entry *hierarchy_insert_entry(struct hists *hists,
1276 struct rb_root *root,
1277 struct hist_entry *he,
Namhyung Kimaec13a72016-03-09 22:46:58 +09001278 struct hist_entry *parent_he,
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001279 struct perf_hpp_list *hpp_list)
Namhyung Kimaef810e2016-02-25 00:13:34 +09001280{
1281 struct rb_node **p = &root->rb_node;
1282 struct rb_node *parent = NULL;
1283 struct hist_entry *iter, *new;
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001284 struct perf_hpp_fmt *fmt;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001285 int64_t cmp;
1286
1287 while (*p != NULL) {
1288 parent = *p;
1289 iter = rb_entry(parent, struct hist_entry, rb_node_in);
1290
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001291 cmp = 0;
1292 perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1293 cmp = fmt->collapse(fmt, iter, he);
1294 if (cmp)
1295 break;
1296 }
1297
Namhyung Kimaef810e2016-02-25 00:13:34 +09001298 if (!cmp) {
1299 he_stat__add_stat(&iter->stat, &he->stat);
1300 return iter;
1301 }
1302
1303 if (cmp < 0)
1304 p = &parent->rb_left;
1305 else
1306 p = &parent->rb_right;
1307 }
1308
1309 new = hist_entry__new(he, true);
1310 if (new == NULL)
1311 return NULL;
1312
Namhyung Kimaef810e2016-02-25 00:13:34 +09001313 hists->nr_entries++;
1314
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001315 /* save related format list for output */
1316 new->hpp_list = hpp_list;
Namhyung Kimaec13a72016-03-09 22:46:58 +09001317 new->parent_he = parent_he;
1318
1319 hist_entry__apply_hierarchy_filters(new);
Namhyung Kimaef810e2016-02-25 00:13:34 +09001320
1321 /* some fields are now passed to 'new' */
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001322 perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1323 if (perf_hpp__is_trace_entry(fmt) || perf_hpp__is_dynamic_entry(fmt))
1324 he->trace_output = NULL;
1325 else
1326 new->trace_output = NULL;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001327
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001328 if (perf_hpp__is_srcline_entry(fmt))
1329 he->srcline = NULL;
1330 else
1331 new->srcline = NULL;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001332
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001333 if (perf_hpp__is_srcfile_entry(fmt))
1334 he->srcfile = NULL;
1335 else
1336 new->srcfile = NULL;
1337 }
Namhyung Kimaef810e2016-02-25 00:13:34 +09001338
1339 rb_link_node(&new->rb_node_in, parent, p);
1340 rb_insert_color(&new->rb_node_in, root);
1341 return new;
1342}
1343
1344static int hists__hierarchy_insert_entry(struct hists *hists,
1345 struct rb_root *root,
1346 struct hist_entry *he)
1347{
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001348 struct perf_hpp_list_node *node;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001349 struct hist_entry *new_he = NULL;
1350 struct hist_entry *parent = NULL;
1351 int depth = 0;
1352 int ret = 0;
1353
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001354 list_for_each_entry(node, &hists->hpp_formats, list) {
1355 /* skip period (overhead) and elided columns */
1356 if (node->level == 0 || node->skip)
Namhyung Kimaef810e2016-02-25 00:13:34 +09001357 continue;
1358
1359 /* insert copy of 'he' for each fmt into the hierarchy */
Namhyung Kimaec13a72016-03-09 22:46:58 +09001360 new_he = hierarchy_insert_entry(hists, root, he, parent, &node->hpp);
Namhyung Kimaef810e2016-02-25 00:13:34 +09001361 if (new_he == NULL) {
1362 ret = -1;
1363 break;
1364 }
1365
1366 root = &new_he->hroot_in;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001367 new_he->depth = depth++;
1368 parent = new_he;
1369 }
1370
1371 if (new_he) {
1372 new_he->leaf = true;
1373
1374 if (symbol_conf.use_callchain) {
1375 callchain_cursor_reset(&callchain_cursor);
1376 if (callchain_merge(&callchain_cursor,
1377 new_he->callchain,
1378 he->callchain) < 0)
1379 ret = -1;
1380 }
1381 }
1382
1383 /* 'he' is no longer used */
1384 hist_entry__delete(he);
1385
1386 /* return 0 (or -1) since it already applied filters */
1387 return ret;
1388}
1389
Jiri Olsa592dac62016-03-24 13:52:17 +01001390static int hists__collapse_insert_entry(struct hists *hists,
1391 struct rb_root *root,
1392 struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +02001393{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -02001394 struct rb_node **p = &root->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +02001395 struct rb_node *parent = NULL;
1396 struct hist_entry *iter;
1397 int64_t cmp;
1398
Namhyung Kimaef810e2016-02-25 00:13:34 +09001399 if (symbol_conf.report_hierarchy)
1400 return hists__hierarchy_insert_entry(hists, root, he);
1401
John Kacur3d1d07e2009-09-28 15:32:55 +02001402 while (*p != NULL) {
1403 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001404 iter = rb_entry(parent, struct hist_entry, rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +02001405
1406 cmp = hist_entry__collapse(iter, he);
1407
1408 if (!cmp) {
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001409 int ret = 0;
1410
Namhyung Kim139c0812012-10-04 21:49:43 +09001411 he_stat__add_stat(&iter->stat, &he->stat);
Namhyung Kimf8be1c82012-09-11 13:15:07 +09001412 if (symbol_conf.cumulate_callchain)
1413 he_stat__add_stat(iter->stat_acc, he->stat_acc);
Namhyung Kim9ec60972012-09-26 16:47:28 +09001414
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +01001415 if (symbol_conf.use_callchain) {
Namhyung Kim47260642012-05-31 14:43:26 +09001416 callchain_cursor_reset(&callchain_cursor);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001417 if (callchain_merge(&callchain_cursor,
1418 iter->callchain,
1419 he->callchain) < 0)
1420 ret = -1;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +01001421 }
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -03001422 hist_entry__delete(he);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001423 return ret;
John Kacur3d1d07e2009-09-28 15:32:55 +02001424 }
1425
1426 if (cmp < 0)
1427 p = &(*p)->rb_left;
1428 else
1429 p = &(*p)->rb_right;
1430 }
Namhyung Kim740b97f2014-12-22 13:44:10 +09001431 hists->nr_entries++;
John Kacur3d1d07e2009-09-28 15:32:55 +02001432
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001433 rb_link_node(&he->rb_node_in, parent, p);
1434 rb_insert_color(&he->rb_node_in, root);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001435 return 1;
John Kacur3d1d07e2009-09-28 15:32:55 +02001436}
1437
Namhyung Kimfc284be2016-01-07 10:14:10 +01001438struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001439{
1440 struct rb_root *root;
1441
1442 pthread_mutex_lock(&hists->lock);
1443
1444 root = hists->entries_in;
1445 if (++hists->entries_in > &hists->entries_in_array[1])
1446 hists->entries_in = &hists->entries_in_array[0];
1447
1448 pthread_mutex_unlock(&hists->lock);
1449
1450 return root;
1451}
1452
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001453static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
1454{
1455 hists__filter_entry_by_dso(hists, he);
1456 hists__filter_entry_by_thread(hists, he);
Namhyung Kime94d53e2012-03-16 17:50:51 +09001457 hists__filter_entry_by_symbol(hists, he);
Kan Liang21394d92015-09-04 10:45:44 -04001458 hists__filter_entry_by_socket(hists, he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001459}
1460
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001461int hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001462{
1463 struct rb_root *root;
1464 struct rb_node *next;
1465 struct hist_entry *n;
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001466 int ret;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001467
Jiri Olsa52225032016-05-03 13:54:42 +02001468 if (!hists__has(hists, need_collapse))
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001469 return 0;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001470
Namhyung Kim740b97f2014-12-22 13:44:10 +09001471 hists->nr_entries = 0;
1472
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001473 root = hists__get_rotate_entries_in(hists);
Namhyung Kim740b97f2014-12-22 13:44:10 +09001474
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001475 next = rb_first(root);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001476
1477 while (next) {
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001478 if (session_done())
1479 break;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001480 n = rb_entry(next, struct hist_entry, rb_node_in);
1481 next = rb_next(&n->rb_node_in);
1482
1483 rb_erase(&n->rb_node_in, root);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001484 ret = hists__collapse_insert_entry(hists, &hists->entries_collapsed, n);
1485 if (ret < 0)
1486 return -1;
1487
1488 if (ret) {
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001489 /*
1490 * If it wasn't combined with one of the entries already
1491 * collapsed, we need to apply the filters that may have
1492 * been set by, say, the hist_browser.
1493 */
1494 hists__apply_filters(hists, n);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001495 }
Namhyung Kimc1fb5652013-10-11 14:15:38 +09001496 if (prog)
1497 ui_progress__update(prog, 1);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001498 }
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001499 return 0;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001500}
1501
Namhyung Kim043ca3892014-03-03 14:18:00 +09001502static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001503{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001504 struct hists *hists = a->hists;
Namhyung Kim043ca3892014-03-03 14:18:00 +09001505 struct perf_hpp_fmt *fmt;
1506 int64_t cmp = 0;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001507
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001508 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim361459f2015-12-23 02:07:08 +09001509 if (perf_hpp__should_skip(fmt, a->hists))
Namhyung Kime67d49a2014-03-18 13:00:59 +09001510 continue;
1511
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001512 cmp = fmt->sort(fmt, a, b);
Namhyung Kim043ca3892014-03-03 14:18:00 +09001513 if (cmp)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001514 break;
1515 }
1516
Namhyung Kim043ca3892014-03-03 14:18:00 +09001517 return cmp;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001518}
1519
Namhyung Kim9283ba92014-04-24 16:37:26 +09001520static void hists__reset_filter_stats(struct hists *hists)
1521{
1522 hists->nr_non_filtered_entries = 0;
1523 hists->stats.total_non_filtered_period = 0;
1524}
1525
1526void hists__reset_stats(struct hists *hists)
1527{
1528 hists->nr_entries = 0;
1529 hists->stats.total_period = 0;
1530
1531 hists__reset_filter_stats(hists);
1532}
1533
1534static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
1535{
1536 hists->nr_non_filtered_entries++;
1537 hists->stats.total_non_filtered_period += h->stat.period;
1538}
1539
1540void hists__inc_stats(struct hists *hists, struct hist_entry *h)
1541{
1542 if (!h->filtered)
1543 hists__inc_filter_stats(hists, h);
1544
1545 hists->nr_entries++;
1546 hists->stats.total_period += h->stat.period;
1547}
1548
Namhyung Kimf7fb5382016-03-09 22:47:02 +09001549static void hierarchy_recalc_total_periods(struct hists *hists)
1550{
1551 struct rb_node *node;
1552 struct hist_entry *he;
1553
1554 node = rb_first(&hists->entries);
1555
1556 hists->stats.total_period = 0;
1557 hists->stats.total_non_filtered_period = 0;
1558
1559 /*
1560 * recalculate total period using top-level entries only
1561 * since lower level entries only see non-filtered entries
1562 * but upper level entries have sum of both entries.
1563 */
1564 while (node) {
1565 he = rb_entry(node, struct hist_entry, rb_node);
1566 node = rb_next(node);
1567
1568 hists->stats.total_period += he->stat.period;
1569 if (!he->filtered)
1570 hists->stats.total_non_filtered_period += he->stat.period;
1571 }
1572}
1573
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001574static void hierarchy_insert_output_entry(struct rb_root *root,
1575 struct hist_entry *he)
1576{
1577 struct rb_node **p = &root->rb_node;
1578 struct rb_node *parent = NULL;
1579 struct hist_entry *iter;
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001580 struct perf_hpp_fmt *fmt;
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001581
1582 while (*p != NULL) {
1583 parent = *p;
1584 iter = rb_entry(parent, struct hist_entry, rb_node);
1585
1586 if (hist_entry__sort(he, iter) > 0)
1587 p = &parent->rb_left;
1588 else
1589 p = &parent->rb_right;
1590 }
1591
1592 rb_link_node(&he->rb_node, parent, p);
1593 rb_insert_color(&he->rb_node, root);
Namhyung Kimabab5e72016-02-27 03:52:47 +09001594
1595 /* update column width of dynamic entry */
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001596 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
1597 if (perf_hpp__is_dynamic_entry(fmt))
1598 fmt->sort(fmt, he, NULL);
1599 }
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001600}
1601
1602static void hists__hierarchy_output_resort(struct hists *hists,
1603 struct ui_progress *prog,
1604 struct rb_root *root_in,
1605 struct rb_root *root_out,
1606 u64 min_callchain_hits,
1607 bool use_callchain)
1608{
1609 struct rb_node *node;
1610 struct hist_entry *he;
1611
1612 *root_out = RB_ROOT;
1613 node = rb_first(root_in);
1614
1615 while (node) {
1616 he = rb_entry(node, struct hist_entry, rb_node_in);
1617 node = rb_next(node);
1618
1619 hierarchy_insert_output_entry(root_out, he);
1620
1621 if (prog)
1622 ui_progress__update(prog, 1);
1623
Namhyung Kimc72ab442016-11-08 22:08:33 +09001624 hists->nr_entries++;
1625 if (!he->filtered) {
1626 hists->nr_non_filtered_entries++;
1627 hists__calc_col_len(hists, he);
1628 }
1629
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001630 if (!he->leaf) {
1631 hists__hierarchy_output_resort(hists, prog,
1632 &he->hroot_in,
1633 &he->hroot_out,
1634 min_callchain_hits,
1635 use_callchain);
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001636 continue;
1637 }
1638
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001639 if (!use_callchain)
1640 continue;
1641
1642 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1643 u64 total = he->stat.period;
1644
1645 if (symbol_conf.cumulate_callchain)
1646 total = he->stat_acc->period;
1647
1648 min_callchain_hits = total * (callchain_param.min_percent / 100);
1649 }
1650
1651 callchain_param.sort(&he->sorted_chain, he->callchain,
1652 min_callchain_hits, &callchain_param);
1653 }
1654}
1655
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001656static void __hists__insert_output_entry(struct rb_root *entries,
1657 struct hist_entry *he,
Kan Liangf9db0d02015-08-11 06:30:48 -04001658 u64 min_callchain_hits,
1659 bool use_callchain)
John Kacur3d1d07e2009-09-28 15:32:55 +02001660{
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001661 struct rb_node **p = &entries->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +02001662 struct rb_node *parent = NULL;
1663 struct hist_entry *iter;
Namhyung Kimabab5e72016-02-27 03:52:47 +09001664 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +02001665
Namhyung Kim744070e2016-01-28 00:40:48 +09001666 if (use_callchain) {
1667 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1668 u64 total = he->stat.period;
1669
1670 if (symbol_conf.cumulate_callchain)
1671 total = he->stat_acc->period;
1672
1673 min_callchain_hits = total * (callchain_param.min_percent / 100);
1674 }
Arnaldo Carvalho de Melob9fb9302010-04-02 09:50:42 -03001675 callchain_param.sort(&he->sorted_chain, he->callchain,
John Kacur3d1d07e2009-09-28 15:32:55 +02001676 min_callchain_hits, &callchain_param);
Namhyung Kim744070e2016-01-28 00:40:48 +09001677 }
John Kacur3d1d07e2009-09-28 15:32:55 +02001678
1679 while (*p != NULL) {
1680 parent = *p;
1681 iter = rb_entry(parent, struct hist_entry, rb_node);
1682
Namhyung Kim043ca3892014-03-03 14:18:00 +09001683 if (hist_entry__sort(he, iter) > 0)
John Kacur3d1d07e2009-09-28 15:32:55 +02001684 p = &(*p)->rb_left;
1685 else
1686 p = &(*p)->rb_right;
1687 }
1688
1689 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001690 rb_insert_color(&he->rb_node, entries);
Namhyung Kimabab5e72016-02-27 03:52:47 +09001691
1692 perf_hpp_list__for_each_sort_list(&perf_hpp_list, fmt) {
1693 if (perf_hpp__is_dynamic_entry(fmt) &&
1694 perf_hpp__defined_dynamic_entry(fmt, he->hists))
1695 fmt->sort(fmt, he, NULL); /* update column width */
1696 }
John Kacur3d1d07e2009-09-28 15:32:55 +02001697}
1698
Jiri Olsa01441af2016-01-18 10:23:59 +01001699static void output_resort(struct hists *hists, struct ui_progress *prog,
Jiri Olsa52c5cc32016-08-01 20:02:34 +02001700 bool use_callchain, hists__resort_cb_t cb)
John Kacur3d1d07e2009-09-28 15:32:55 +02001701{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001702 struct rb_root *root;
John Kacur3d1d07e2009-09-28 15:32:55 +02001703 struct rb_node *next;
1704 struct hist_entry *n;
Namhyung Kim467ef102016-02-16 23:08:19 +09001705 u64 callchain_total;
John Kacur3d1d07e2009-09-28 15:32:55 +02001706 u64 min_callchain_hits;
1707
Namhyung Kim467ef102016-02-16 23:08:19 +09001708 callchain_total = hists->callchain_period;
1709 if (symbol_conf.filter_relative)
1710 callchain_total = hists->callchain_non_filtered_period;
1711
1712 min_callchain_hits = callchain_total * (callchain_param.min_percent / 100);
John Kacur3d1d07e2009-09-28 15:32:55 +02001713
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001714 hists__reset_stats(hists);
1715 hists__reset_col_len(hists);
1716
1717 if (symbol_conf.report_hierarchy) {
Namhyung Kimf7fb5382016-03-09 22:47:02 +09001718 hists__hierarchy_output_resort(hists, prog,
1719 &hists->entries_collapsed,
1720 &hists->entries,
1721 min_callchain_hits,
1722 use_callchain);
1723 hierarchy_recalc_total_periods(hists);
1724 return;
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001725 }
1726
Jiri Olsa52225032016-05-03 13:54:42 +02001727 if (hists__has(hists, need_collapse))
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001728 root = &hists->entries_collapsed;
1729 else
1730 root = hists->entries_in;
1731
1732 next = rb_first(root);
1733 hists->entries = RB_ROOT;
John Kacur3d1d07e2009-09-28 15:32:55 +02001734
1735 while (next) {
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001736 n = rb_entry(next, struct hist_entry, rb_node_in);
1737 next = rb_next(&n->rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +02001738
Jiri Olsa52c5cc32016-08-01 20:02:34 +02001739 if (cb && cb(n))
1740 continue;
1741
Kan Liangf9db0d02015-08-11 06:30:48 -04001742 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits, use_callchain);
Namhyung Kim62638352014-04-24 16:21:46 +09001743 hists__inc_stats(hists, n);
Namhyung Kimae993ef2014-04-24 16:25:19 +09001744
1745 if (!n->filtered)
1746 hists__calc_col_len(hists, n);
Namhyung Kim740b97f2014-12-22 13:44:10 +09001747
1748 if (prog)
1749 ui_progress__update(prog, 1);
John Kacur3d1d07e2009-09-28 15:32:55 +02001750 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001751}
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -02001752
Jiri Olsa452ce032016-01-18 10:24:00 +01001753void perf_evsel__output_resort(struct perf_evsel *evsel, struct ui_progress *prog)
Jiri Olsa01441af2016-01-18 10:23:59 +01001754{
Jiri Olsa01441af2016-01-18 10:23:59 +01001755 bool use_callchain;
1756
1757 if (evsel && symbol_conf.use_callchain && !symbol_conf.show_ref_callgraph)
1758 use_callchain = evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN;
1759 else
1760 use_callchain = symbol_conf.use_callchain;
1761
Jiri Olsa52c5cc32016-08-01 20:02:34 +02001762 output_resort(evsel__hists(evsel), prog, use_callchain, NULL);
Jiri Olsa452ce032016-01-18 10:24:00 +01001763}
1764
1765void hists__output_resort(struct hists *hists, struct ui_progress *prog)
1766{
Jiri Olsa52c5cc32016-08-01 20:02:34 +02001767 output_resort(hists, prog, symbol_conf.use_callchain, NULL);
1768}
1769
1770void hists__output_resort_cb(struct hists *hists, struct ui_progress *prog,
1771 hists__resort_cb_t cb)
1772{
1773 output_resort(hists, prog, symbol_conf.use_callchain, cb);
Jiri Olsa01441af2016-01-18 10:23:59 +01001774}
1775
Namhyung Kim8c018722016-02-25 00:13:36 +09001776static bool can_goto_child(struct hist_entry *he, enum hierarchy_move_dir hmd)
1777{
1778 if (he->leaf || hmd == HMD_FORCE_SIBLING)
1779 return false;
1780
1781 if (he->unfolded || hmd == HMD_FORCE_CHILD)
1782 return true;
1783
1784 return false;
1785}
1786
1787struct rb_node *rb_hierarchy_last(struct rb_node *node)
1788{
1789 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1790
1791 while (can_goto_child(he, HMD_NORMAL)) {
1792 node = rb_last(&he->hroot_out);
1793 he = rb_entry(node, struct hist_entry, rb_node);
1794 }
1795 return node;
1796}
1797
1798struct rb_node *__rb_hierarchy_next(struct rb_node *node, enum hierarchy_move_dir hmd)
1799{
1800 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1801
1802 if (can_goto_child(he, hmd))
1803 node = rb_first(&he->hroot_out);
1804 else
1805 node = rb_next(node);
1806
1807 while (node == NULL) {
1808 he = he->parent_he;
1809 if (he == NULL)
1810 break;
1811
1812 node = rb_next(&he->rb_node);
1813 }
1814 return node;
1815}
1816
1817struct rb_node *rb_hierarchy_prev(struct rb_node *node)
1818{
1819 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1820
1821 node = rb_prev(node);
1822 if (node)
1823 return rb_hierarchy_last(node);
1824
1825 he = he->parent_he;
1826 if (he == NULL)
1827 return NULL;
1828
1829 return &he->rb_node;
1830}
1831
Namhyung Kima7b58952016-02-26 21:13:16 +09001832bool hist_entry__has_hierarchy_children(struct hist_entry *he, float limit)
1833{
1834 struct rb_node *node;
1835 struct hist_entry *child;
1836 float percent;
1837
1838 if (he->leaf)
1839 return false;
1840
1841 node = rb_first(&he->hroot_out);
1842 child = rb_entry(node, struct hist_entry, rb_node);
1843
1844 while (node && child->filtered) {
1845 node = rb_next(node);
1846 child = rb_entry(node, struct hist_entry, rb_node);
1847 }
1848
1849 if (node)
1850 percent = hist_entry__get_percent_limit(child);
1851 else
1852 percent = 0;
1853
1854 return node && percent >= limit;
1855}
1856
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001857static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001858 enum hist_filter filter)
1859{
1860 h->filtered &= ~(1 << filter);
Namhyung Kim155e9af2016-02-25 00:13:38 +09001861
1862 if (symbol_conf.report_hierarchy) {
1863 struct hist_entry *parent = h->parent_he;
1864
1865 while (parent) {
1866 he_stat__add_stat(&parent->stat, &h->stat);
1867
1868 parent->filtered &= ~(1 << filter);
1869
1870 if (parent->filtered)
1871 goto next;
1872
1873 /* force fold unfiltered entry for simplicity */
1874 parent->unfolded = false;
Namhyung Kim79dded82016-02-26 21:13:19 +09001875 parent->has_no_entry = false;
Namhyung Kim155e9af2016-02-25 00:13:38 +09001876 parent->row_offset = 0;
1877 parent->nr_rows = 0;
1878next:
1879 parent = parent->parent_he;
1880 }
1881 }
1882
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001883 if (h->filtered)
1884 return;
1885
Namhyung Kim87e90f42014-04-24 16:44:16 +09001886 /* force fold unfiltered entry for simplicity */
Namhyung Kim3698dab2015-05-05 23:55:46 +09001887 h->unfolded = false;
Namhyung Kim79dded82016-02-26 21:13:19 +09001888 h->has_no_entry = false;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -03001889 h->row_offset = 0;
He Kuanga8cd1f42015-03-11 20:36:03 +08001890 h->nr_rows = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001891
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001892 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001893
Namhyung Kim9283ba92014-04-24 16:37:26 +09001894 hists__inc_filter_stats(hists, h);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001895 hists__calc_col_len(hists, h);
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001896}
1897
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001898
1899static bool hists__filter_entry_by_dso(struct hists *hists,
1900 struct hist_entry *he)
1901{
1902 if (hists->dso_filter != NULL &&
1903 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
1904 he->filtered |= (1 << HIST_FILTER__DSO);
1905 return true;
1906 }
1907
1908 return false;
1909}
1910
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001911static bool hists__filter_entry_by_thread(struct hists *hists,
1912 struct hist_entry *he)
1913{
1914 if (hists->thread_filter != NULL &&
1915 he->thread != hists->thread_filter) {
1916 he->filtered |= (1 << HIST_FILTER__THREAD);
1917 return true;
1918 }
1919
1920 return false;
1921}
1922
Namhyung Kime94d53e2012-03-16 17:50:51 +09001923static bool hists__filter_entry_by_symbol(struct hists *hists,
1924 struct hist_entry *he)
1925{
1926 if (hists->symbol_filter_str != NULL &&
1927 (!he->ms.sym || strstr(he->ms.sym->name,
1928 hists->symbol_filter_str) == NULL)) {
1929 he->filtered |= (1 << HIST_FILTER__SYMBOL);
1930 return true;
1931 }
1932
1933 return false;
1934}
1935
Kan Liang21394d92015-09-04 10:45:44 -04001936static bool hists__filter_entry_by_socket(struct hists *hists,
1937 struct hist_entry *he)
1938{
1939 if ((hists->socket_filter > -1) &&
1940 (he->socket != hists->socket_filter)) {
1941 he->filtered |= (1 << HIST_FILTER__SOCKET);
1942 return true;
1943 }
1944
1945 return false;
1946}
1947
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001948typedef bool (*filter_fn_t)(struct hists *hists, struct hist_entry *he);
1949
1950static void hists__filter_by_type(struct hists *hists, int type, filter_fn_t filter)
Kan Liang84734b02015-09-04 10:45:45 -04001951{
1952 struct rb_node *nd;
1953
1954 hists->stats.nr_non_filtered_samples = 0;
1955
1956 hists__reset_filter_stats(hists);
1957 hists__reset_col_len(hists);
1958
1959 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1960 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1961
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001962 if (filter(hists, h))
Kan Liang84734b02015-09-04 10:45:45 -04001963 continue;
1964
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001965 hists__remove_entry_filter(hists, h, type);
Kan Liang84734b02015-09-04 10:45:45 -04001966 }
1967}
1968
Namhyung Kim70642852016-02-25 00:13:39 +09001969static void resort_filtered_entry(struct rb_root *root, struct hist_entry *he)
1970{
1971 struct rb_node **p = &root->rb_node;
1972 struct rb_node *parent = NULL;
1973 struct hist_entry *iter;
1974 struct rb_root new_root = RB_ROOT;
1975 struct rb_node *nd;
1976
1977 while (*p != NULL) {
1978 parent = *p;
1979 iter = rb_entry(parent, struct hist_entry, rb_node);
1980
1981 if (hist_entry__sort(he, iter) > 0)
1982 p = &(*p)->rb_left;
1983 else
1984 p = &(*p)->rb_right;
1985 }
1986
1987 rb_link_node(&he->rb_node, parent, p);
1988 rb_insert_color(&he->rb_node, root);
1989
1990 if (he->leaf || he->filtered)
1991 return;
1992
1993 nd = rb_first(&he->hroot_out);
1994 while (nd) {
1995 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1996
1997 nd = rb_next(nd);
1998 rb_erase(&h->rb_node, &he->hroot_out);
1999
2000 resort_filtered_entry(&new_root, h);
2001 }
2002
2003 he->hroot_out = new_root;
2004}
2005
Namhyung Kim155e9af2016-02-25 00:13:38 +09002006static void hists__filter_hierarchy(struct hists *hists, int type, const void *arg)
2007{
2008 struct rb_node *nd;
Namhyung Kim70642852016-02-25 00:13:39 +09002009 struct rb_root new_root = RB_ROOT;
Namhyung Kim155e9af2016-02-25 00:13:38 +09002010
2011 hists->stats.nr_non_filtered_samples = 0;
2012
2013 hists__reset_filter_stats(hists);
2014 hists__reset_col_len(hists);
2015
2016 nd = rb_first(&hists->entries);
2017 while (nd) {
2018 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2019 int ret;
2020
2021 ret = hist_entry__filter(h, type, arg);
2022
2023 /*
2024 * case 1. non-matching type
2025 * zero out the period, set filter marker and move to child
2026 */
2027 if (ret < 0) {
2028 memset(&h->stat, 0, sizeof(h->stat));
2029 h->filtered |= (1 << type);
2030
2031 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_CHILD);
2032 }
2033 /*
2034 * case 2. matched type (filter out)
2035 * set filter marker and move to next
2036 */
2037 else if (ret == 1) {
2038 h->filtered |= (1 << type);
2039
2040 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
2041 }
2042 /*
2043 * case 3. ok (not filtered)
2044 * add period to hists and parents, erase the filter marker
2045 * and move to next sibling
2046 */
2047 else {
2048 hists__remove_entry_filter(hists, h, type);
2049
2050 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
2051 }
2052 }
Namhyung Kim70642852016-02-25 00:13:39 +09002053
Namhyung Kimf7fb5382016-03-09 22:47:02 +09002054 hierarchy_recalc_total_periods(hists);
2055
Namhyung Kim70642852016-02-25 00:13:39 +09002056 /*
2057 * resort output after applying a new filter since filter in a lower
2058 * hierarchy can change periods in a upper hierarchy.
2059 */
2060 nd = rb_first(&hists->entries);
2061 while (nd) {
2062 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2063
2064 nd = rb_next(nd);
2065 rb_erase(&h->rb_node, &hists->entries);
2066
2067 resort_filtered_entry(&new_root, h);
2068 }
2069
2070 hists->entries = new_root;
Namhyung Kim155e9af2016-02-25 00:13:38 +09002071}
2072
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002073void hists__filter_by_thread(struct hists *hists)
2074{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002075 if (symbol_conf.report_hierarchy)
2076 hists__filter_hierarchy(hists, HIST_FILTER__THREAD,
2077 hists->thread_filter);
2078 else
2079 hists__filter_by_type(hists, HIST_FILTER__THREAD,
2080 hists__filter_entry_by_thread);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002081}
2082
2083void hists__filter_by_dso(struct hists *hists)
2084{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002085 if (symbol_conf.report_hierarchy)
2086 hists__filter_hierarchy(hists, HIST_FILTER__DSO,
2087 hists->dso_filter);
2088 else
2089 hists__filter_by_type(hists, HIST_FILTER__DSO,
2090 hists__filter_entry_by_dso);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002091}
2092
2093void hists__filter_by_symbol(struct hists *hists)
2094{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002095 if (symbol_conf.report_hierarchy)
2096 hists__filter_hierarchy(hists, HIST_FILTER__SYMBOL,
2097 hists->symbol_filter_str);
2098 else
2099 hists__filter_by_type(hists, HIST_FILTER__SYMBOL,
2100 hists__filter_entry_by_symbol);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002101}
2102
2103void hists__filter_by_socket(struct hists *hists)
2104{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002105 if (symbol_conf.report_hierarchy)
2106 hists__filter_hierarchy(hists, HIST_FILTER__SOCKET,
2107 &hists->socket_filter);
2108 else
2109 hists__filter_by_type(hists, HIST_FILTER__SOCKET,
2110 hists__filter_entry_by_socket);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002111}
2112
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03002113void events_stats__inc(struct events_stats *stats, u32 type)
2114{
2115 ++stats->nr_events[0];
2116 ++stats->nr_events[type];
2117}
2118
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03002119void hists__inc_nr_events(struct hists *hists, u32 type)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03002120{
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03002121 events_stats__inc(&hists->stats, type);
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03002122}
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002123
Namhyung Kim1844dbc2014-05-28 14:12:18 +09002124void hists__inc_nr_samples(struct hists *hists, bool filtered)
2125{
2126 events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
2127 if (!filtered)
2128 hists->stats.nr_non_filtered_samples++;
2129}
2130
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002131static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
2132 struct hist_entry *pair)
2133{
Namhyung Kimce74f602012-12-10 17:29:55 +09002134 struct rb_root *root;
2135 struct rb_node **p;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002136 struct rb_node *parent = NULL;
2137 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -07002138 int64_t cmp;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002139
Jiri Olsa52225032016-05-03 13:54:42 +02002140 if (hists__has(hists, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002141 root = &hists->entries_collapsed;
2142 else
2143 root = hists->entries_in;
2144
2145 p = &root->rb_node;
2146
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002147 while (*p != NULL) {
2148 parent = *p;
Namhyung Kimce74f602012-12-10 17:29:55 +09002149 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002150
Namhyung Kimce74f602012-12-10 17:29:55 +09002151 cmp = hist_entry__collapse(he, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002152
2153 if (!cmp)
2154 goto out;
2155
2156 if (cmp < 0)
2157 p = &(*p)->rb_left;
2158 else
2159 p = &(*p)->rb_right;
2160 }
2161
Namhyung Kima0b51af2012-09-11 13:34:27 +09002162 he = hist_entry__new(pair, true);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002163 if (he) {
Arnaldo Carvalho de Melo30193d72012-11-12 13:20:03 -03002164 memset(&he->stat, 0, sizeof(he->stat));
2165 he->hists = hists;
Kan Liang09623d72016-04-24 23:28:09 -07002166 if (symbol_conf.cumulate_callchain)
2167 memset(he->stat_acc, 0, sizeof(he->stat));
Namhyung Kimce74f602012-12-10 17:29:55 +09002168 rb_link_node(&he->rb_node_in, parent, p);
2169 rb_insert_color(&he->rb_node_in, root);
Namhyung Kim62638352014-04-24 16:21:46 +09002170 hists__inc_stats(hists, he);
Jiri Olsae0af43d2012-12-01 21:18:20 +01002171 he->dummy = true;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002172 }
2173out:
2174 return he;
2175}
2176
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002177static struct hist_entry *add_dummy_hierarchy_entry(struct hists *hists,
2178 struct rb_root *root,
2179 struct hist_entry *pair)
2180{
2181 struct rb_node **p;
2182 struct rb_node *parent = NULL;
2183 struct hist_entry *he;
2184 struct perf_hpp_fmt *fmt;
2185
2186 p = &root->rb_node;
2187 while (*p != NULL) {
2188 int64_t cmp = 0;
2189
2190 parent = *p;
2191 he = rb_entry(parent, struct hist_entry, rb_node_in);
2192
2193 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
2194 cmp = fmt->collapse(fmt, he, pair);
2195 if (cmp)
2196 break;
2197 }
2198 if (!cmp)
2199 goto out;
2200
2201 if (cmp < 0)
2202 p = &parent->rb_left;
2203 else
2204 p = &parent->rb_right;
2205 }
2206
2207 he = hist_entry__new(pair, true);
2208 if (he) {
2209 rb_link_node(&he->rb_node_in, parent, p);
2210 rb_insert_color(&he->rb_node_in, root);
2211
2212 he->dummy = true;
2213 he->hists = hists;
2214 memset(&he->stat, 0, sizeof(he->stat));
2215 hists__inc_stats(hists, he);
2216 }
2217out:
2218 return he;
2219}
2220
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002221static struct hist_entry *hists__find_entry(struct hists *hists,
2222 struct hist_entry *he)
2223{
Namhyung Kimce74f602012-12-10 17:29:55 +09002224 struct rb_node *n;
2225
Jiri Olsa52225032016-05-03 13:54:42 +02002226 if (hists__has(hists, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002227 n = hists->entries_collapsed.rb_node;
2228 else
2229 n = hists->entries_in->rb_node;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002230
2231 while (n) {
Namhyung Kimce74f602012-12-10 17:29:55 +09002232 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
2233 int64_t cmp = hist_entry__collapse(iter, he);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002234
2235 if (cmp < 0)
2236 n = n->rb_left;
2237 else if (cmp > 0)
2238 n = n->rb_right;
2239 else
2240 return iter;
2241 }
2242
2243 return NULL;
2244}
2245
Namhyung Kim09034de2016-09-13 16:45:46 +09002246static struct hist_entry *hists__find_hierarchy_entry(struct rb_root *root,
2247 struct hist_entry *he)
2248{
2249 struct rb_node *n = root->rb_node;
2250
2251 while (n) {
2252 struct hist_entry *iter;
2253 struct perf_hpp_fmt *fmt;
2254 int64_t cmp = 0;
2255
2256 iter = rb_entry(n, struct hist_entry, rb_node_in);
2257 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
2258 cmp = fmt->collapse(fmt, iter, he);
2259 if (cmp)
2260 break;
2261 }
2262
2263 if (cmp < 0)
2264 n = n->rb_left;
2265 else if (cmp > 0)
2266 n = n->rb_right;
2267 else
2268 return iter;
2269 }
2270
2271 return NULL;
2272}
2273
2274static void hists__match_hierarchy(struct rb_root *leader_root,
2275 struct rb_root *other_root)
2276{
2277 struct rb_node *nd;
2278 struct hist_entry *pos, *pair;
2279
2280 for (nd = rb_first(leader_root); nd; nd = rb_next(nd)) {
2281 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2282 pair = hists__find_hierarchy_entry(other_root, pos);
2283
2284 if (pair) {
2285 hist_entry__add_pair(pair, pos);
2286 hists__match_hierarchy(&pos->hroot_in, &pair->hroot_in);
2287 }
2288 }
2289}
2290
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002291/*
2292 * Look for pairs to link to the leader buckets (hist_entries):
2293 */
2294void hists__match(struct hists *leader, struct hists *other)
2295{
Namhyung Kimce74f602012-12-10 17:29:55 +09002296 struct rb_root *root;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002297 struct rb_node *nd;
2298 struct hist_entry *pos, *pair;
2299
Namhyung Kim09034de2016-09-13 16:45:46 +09002300 if (symbol_conf.report_hierarchy) {
2301 /* hierarchy report always collapses entries */
2302 return hists__match_hierarchy(&leader->entries_collapsed,
2303 &other->entries_collapsed);
2304 }
2305
Jiri Olsa52225032016-05-03 13:54:42 +02002306 if (hists__has(leader, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002307 root = &leader->entries_collapsed;
2308 else
2309 root = leader->entries_in;
2310
2311 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
2312 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002313 pair = hists__find_entry(other, pos);
2314
2315 if (pair)
Namhyung Kim5fa90412012-11-29 15:38:34 +09002316 hist_entry__add_pair(pair, pos);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002317 }
2318}
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002319
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002320static int hists__link_hierarchy(struct hists *leader_hists,
2321 struct hist_entry *parent,
2322 struct rb_root *leader_root,
2323 struct rb_root *other_root)
2324{
2325 struct rb_node *nd;
2326 struct hist_entry *pos, *leader;
2327
2328 for (nd = rb_first(other_root); nd; nd = rb_next(nd)) {
2329 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2330
2331 if (hist_entry__has_pairs(pos)) {
2332 bool found = false;
2333
2334 list_for_each_entry(leader, &pos->pairs.head, pairs.node) {
2335 if (leader->hists == leader_hists) {
2336 found = true;
2337 break;
2338 }
2339 }
2340 if (!found)
2341 return -1;
2342 } else {
2343 leader = add_dummy_hierarchy_entry(leader_hists,
2344 leader_root, pos);
2345 if (leader == NULL)
2346 return -1;
2347
2348 /* do not point parent in the pos */
2349 leader->parent_he = parent;
2350
2351 hist_entry__add_pair(pos, leader);
2352 }
2353
2354 if (!pos->leaf) {
2355 if (hists__link_hierarchy(leader_hists, leader,
2356 &leader->hroot_in,
2357 &pos->hroot_in) < 0)
2358 return -1;
2359 }
2360 }
2361 return 0;
2362}
2363
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002364/*
2365 * Look for entries in the other hists that are not present in the leader, if
2366 * we find them, just add a dummy entry on the leader hists, with period=0,
2367 * nr_events=0, to serve as the list header.
2368 */
2369int hists__link(struct hists *leader, struct hists *other)
2370{
Namhyung Kimce74f602012-12-10 17:29:55 +09002371 struct rb_root *root;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002372 struct rb_node *nd;
2373 struct hist_entry *pos, *pair;
2374
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002375 if (symbol_conf.report_hierarchy) {
2376 /* hierarchy report always collapses entries */
2377 return hists__link_hierarchy(leader, NULL,
2378 &leader->entries_collapsed,
2379 &other->entries_collapsed);
2380 }
2381
Jiri Olsa52225032016-05-03 13:54:42 +02002382 if (hists__has(other, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002383 root = &other->entries_collapsed;
2384 else
2385 root = other->entries_in;
2386
2387 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
2388 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002389
2390 if (!hist_entry__has_pairs(pos)) {
2391 pair = hists__add_dummy_entry(leader, pos);
2392 if (pair == NULL)
2393 return -1;
Namhyung Kim5fa90412012-11-29 15:38:34 +09002394 hist_entry__add_pair(pos, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002395 }
2396 }
2397
2398 return 0;
2399}
Namhyung Kimf2148332014-01-14 11:52:48 +09002400
Andi Kleen578499982015-07-18 08:24:49 -07002401void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
2402 struct perf_sample *sample, bool nonany_branch_mode)
2403{
2404 struct branch_info *bi;
2405
2406 /* If we have branch cycles always annotate them. */
2407 if (bs && bs->nr && bs->entries[0].flags.cycles) {
2408 int i;
2409
2410 bi = sample__resolve_bstack(sample, al);
2411 if (bi) {
2412 struct addr_map_symbol *prev = NULL;
2413
2414 /*
2415 * Ignore errors, still want to process the
2416 * other entries.
2417 *
2418 * For non standard branch modes always
2419 * force no IPC (prev == NULL)
2420 *
2421 * Note that perf stores branches reversed from
2422 * program order!
2423 */
2424 for (i = bs->nr - 1; i >= 0; i--) {
2425 addr_map_symbol__account_cycles(&bi[i].from,
2426 nonany_branch_mode ? NULL : prev,
2427 bi[i].flags.cycles);
2428 prev = &bi[i].to;
2429 }
2430 free(bi);
2431 }
2432 }
2433}
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03002434
2435size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp)
2436{
2437 struct perf_evsel *pos;
2438 size_t ret = 0;
2439
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002440 evlist__for_each_entry(evlist, pos) {
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03002441 ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
2442 ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
2443 }
2444
2445 return ret;
2446}
2447
2448
Namhyung Kimf2148332014-01-14 11:52:48 +09002449u64 hists__total_period(struct hists *hists)
2450{
2451 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
2452 hists->stats.total_period;
2453}
Namhyung Kim33db4562014-02-07 12:06:07 +09002454
2455int parse_filter_percentage(const struct option *opt __maybe_unused,
2456 const char *arg, int unset __maybe_unused)
2457{
2458 if (!strcmp(arg, "relative"))
2459 symbol_conf.filter_relative = true;
2460 else if (!strcmp(arg, "absolute"))
2461 symbol_conf.filter_relative = false;
Arnaldo Carvalho de Meloecc4c562017-01-24 13:44:10 -03002462 else {
Colin Ian Kinga596a872017-03-30 10:54:40 +01002463 pr_debug("Invalid percentage: %s\n", arg);
Namhyung Kim33db4562014-02-07 12:06:07 +09002464 return -1;
Arnaldo Carvalho de Meloecc4c562017-01-24 13:44:10 -03002465 }
Namhyung Kim33db4562014-02-07 12:06:07 +09002466
2467 return 0;
2468}
Namhyung Kim0b93da12014-01-14 12:02:15 +09002469
2470int perf_hist_config(const char *var, const char *value)
2471{
2472 if (!strcmp(var, "hist.percentage"))
2473 return parse_filter_percentage(NULL, value, 0);
2474
2475 return 0;
2476}
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002477
Jiri Olsa5b658552016-01-18 10:24:22 +01002478int __hists__init(struct hists *hists, struct perf_hpp_list *hpp_list)
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002479{
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002480 memset(hists, 0, sizeof(*hists));
2481 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
2482 hists->entries_in = &hists->entries_in_array[0];
2483 hists->entries_collapsed = RB_ROOT;
2484 hists->entries = RB_ROOT;
2485 pthread_mutex_init(&hists->lock, NULL);
Kan Liang21394d92015-09-04 10:45:44 -04002486 hists->socket_filter = -1;
Jiri Olsa5b658552016-01-18 10:24:22 +01002487 hists->hpp_list = hpp_list;
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002488 INIT_LIST_HEAD(&hists->hpp_formats);
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002489 return 0;
2490}
2491
Namhyung Kim61fa0e92015-12-10 16:53:20 +09002492static void hists__delete_remaining_entries(struct rb_root *root)
2493{
2494 struct rb_node *node;
2495 struct hist_entry *he;
2496
2497 while (!RB_EMPTY_ROOT(root)) {
2498 node = rb_first(root);
2499 rb_erase(node, root);
2500
2501 he = rb_entry(node, struct hist_entry, rb_node_in);
2502 hist_entry__delete(he);
2503 }
2504}
2505
2506static void hists__delete_all_entries(struct hists *hists)
2507{
2508 hists__delete_entries(hists);
2509 hists__delete_remaining_entries(&hists->entries_in_array[0]);
2510 hists__delete_remaining_entries(&hists->entries_in_array[1]);
2511 hists__delete_remaining_entries(&hists->entries_collapsed);
2512}
2513
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002514static void hists_evsel__exit(struct perf_evsel *evsel)
2515{
2516 struct hists *hists = evsel__hists(evsel);
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002517 struct perf_hpp_fmt *fmt, *pos;
2518 struct perf_hpp_list_node *node, *tmp;
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002519
Namhyung Kim61fa0e92015-12-10 16:53:20 +09002520 hists__delete_all_entries(hists);
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002521
2522 list_for_each_entry_safe(node, tmp, &hists->hpp_formats, list) {
2523 perf_hpp_list__for_each_format_safe(&node->hpp, fmt, pos) {
2524 list_del(&fmt->list);
2525 free(fmt);
2526 }
2527 list_del(&node->list);
2528 free(node);
2529 }
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002530}
2531
Namhyung Kimfc284be2016-01-07 10:14:10 +01002532static int hists_evsel__init(struct perf_evsel *evsel)
2533{
2534 struct hists *hists = evsel__hists(evsel);
2535
Jiri Olsa5b658552016-01-18 10:24:22 +01002536 __hists__init(hists, &perf_hpp_list);
Namhyung Kimfc284be2016-01-07 10:14:10 +01002537 return 0;
2538}
2539
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002540/*
2541 * XXX We probably need a hists_evsel__exit() to free the hist_entries
2542 * stored in the rbtree...
2543 */
2544
2545int hists__init(void)
2546{
2547 int err = perf_evsel__object_config(sizeof(struct hists_evsel),
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002548 hists_evsel__init,
2549 hists_evsel__exit);
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002550 if (err)
2551 fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);
2552
2553 return err;
2554}
Jiri Olsa94b3dc32016-01-18 10:24:13 +01002555
2556void perf_hpp_list__init(struct perf_hpp_list *list)
2557{
2558 INIT_LIST_HEAD(&list->fields);
2559 INIT_LIST_HEAD(&list->sorts);
2560}