blob: 44a8456cea1042b293b82529586e93b77d76869c [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Frederic Weisbecker8a0ecfb2010-05-13 19:47:16 +02002#include "util.h"
Frederic Weisbecker598357e2010-05-21 12:48:39 +02003#include "build-id.h"
John Kacur3d1d07e2009-09-28 15:32:55 +02004#include "hist.h"
Krister Johansen9c68ae92017-01-05 22:23:31 -08005#include "map.h"
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -02006#include "session.h"
Hari Bathinid890a982017-03-08 02:12:13 +05307#include "namespaces.h"
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -02008#include "sort.h"
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03009#include "evlist.h"
Namhyung Kim29d720e2013-01-22 18:09:33 +090010#include "evsel.h"
Namhyung Kim69bcb012013-10-30 09:40:34 +090011#include "annotate.h"
Arnaldo Carvalho de Melo632a5ca2017-04-17 16:30:49 -030012#include "srcline.h"
Arnaldo Carvalho de Meloe7ff8922017-04-19 21:34:35 -030013#include "thread.h"
Namhyung Kim740b97f2014-12-22 13:44:10 +090014#include "ui/progress.h"
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030015#include <errno.h>
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -020016#include <math.h>
Arnaldo Carvalho de Melo391e4202017-04-19 18:51:14 -030017#include <sys/param.h>
John Kacur3d1d07e2009-09-28 15:32:55 +020018
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -020019static bool hists__filter_entry_by_dso(struct hists *hists,
20 struct hist_entry *he);
21static bool hists__filter_entry_by_thread(struct hists *hists,
22 struct hist_entry *he);
Namhyung Kime94d53e2012-03-16 17:50:51 +090023static bool hists__filter_entry_by_symbol(struct hists *hists,
24 struct hist_entry *he);
Kan Liang21394d92015-09-04 10:45:44 -040025static bool hists__filter_entry_by_socket(struct hists *hists,
26 struct hist_entry *he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -020027
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030028u16 hists__col_len(struct hists *hists, enum hist_column col)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030029{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030030 return hists->col_len[col];
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030031}
32
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030033void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030034{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030035 hists->col_len[col] = len;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030036}
37
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030038bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030039{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030040 if (len > hists__col_len(hists, col)) {
41 hists__set_col_len(hists, col, len);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030042 return true;
43 }
44 return false;
45}
46
Namhyung Kim7ccf4f92012-08-20 13:52:05 +090047void hists__reset_col_len(struct hists *hists)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030048{
49 enum hist_column col;
50
51 for (col = 0; col < HISTC_NR_COLS; ++col)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030052 hists__set_col_len(hists, col, 0);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030053}
54
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010055static void hists__set_unres_dso_col_len(struct hists *hists, int dso)
56{
57 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
58
59 if (hists__col_len(hists, dso) < unresolved_col_width &&
60 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
61 !symbol_conf.dso_list)
62 hists__set_col_len(hists, dso, unresolved_col_width);
63}
64
Namhyung Kim7ccf4f92012-08-20 13:52:05 +090065void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030066{
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010067 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
Stephane Eranian98a3b322013-01-24 16:10:35 +010068 int symlen;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030069 u16 len;
70
Namhyung Kimded19d52013-04-01 20:35:19 +090071 /*
72 * +4 accounts for '[x] ' priv level info
73 * +2 accounts for 0x prefix on raw addresses
74 * +3 accounts for ' y ' symtab origin info
75 */
76 if (h->ms.sym) {
77 symlen = h->ms.sym->namelen + 4;
Namhyung Kimbb963e12017-02-17 17:17:38 +090078 if (verbose > 0)
Namhyung Kimded19d52013-04-01 20:35:19 +090079 symlen += BITS_PER_LONG / 4 + 2 + 3;
80 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
81 } else {
Stephane Eranian98a3b322013-01-24 16:10:35 +010082 symlen = unresolved_col_width + 4 + 2;
83 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010084 hists__set_unres_dso_col_len(hists, HISTC_DSO);
Stephane Eranian98a3b322013-01-24 16:10:35 +010085 }
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030086
87 len = thread__comm_len(h->thread);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030088 if (hists__new_col_len(hists, HISTC_COMM, len))
Jiri Olsa89c7cb22016-06-20 23:58:19 +020089 hists__set_col_len(hists, HISTC_THREAD, len + 8);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030090
91 if (h->ms.map) {
92 len = dso__name_len(h->ms.map->dso);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030093 hists__new_col_len(hists, HISTC_DSO, len);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030094 }
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010095
Namhyung Kimcb993742012-12-27 18:11:42 +090096 if (h->parent)
97 hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen);
98
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010099 if (h->branch_info) {
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100100 if (h->branch_info->from.sym) {
101 symlen = (int)h->branch_info->from.sym->namelen + 4;
Namhyung Kimbb963e12017-02-17 17:17:38 +0900102 if (verbose > 0)
Namhyung Kimded19d52013-04-01 20:35:19 +0900103 symlen += BITS_PER_LONG / 4 + 2 + 3;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100104 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
105
106 symlen = dso__name_len(h->branch_info->from.map->dso);
107 hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
108 } else {
109 symlen = unresolved_col_width + 4 + 2;
110 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
111 hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
112 }
113
114 if (h->branch_info->to.sym) {
115 symlen = (int)h->branch_info->to.sym->namelen + 4;
Namhyung Kimbb963e12017-02-17 17:17:38 +0900116 if (verbose > 0)
Namhyung Kimded19d52013-04-01 20:35:19 +0900117 symlen += BITS_PER_LONG / 4 + 2 + 3;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100118 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
119
120 symlen = dso__name_len(h->branch_info->to.map->dso);
121 hists__new_col_len(hists, HISTC_DSO_TO, symlen);
122 } else {
123 symlen = unresolved_col_width + 4 + 2;
124 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
125 hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
126 }
Andi Kleen508be0d2016-05-20 13:15:08 -0700127
128 if (h->branch_info->srcline_from)
129 hists__new_col_len(hists, HISTC_SRCLINE_FROM,
130 strlen(h->branch_info->srcline_from));
131 if (h->branch_info->srcline_to)
132 hists__new_col_len(hists, HISTC_SRCLINE_TO,
133 strlen(h->branch_info->srcline_to));
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100134 }
Stephane Eranian98a3b322013-01-24 16:10:35 +0100135
136 if (h->mem_info) {
Stephane Eranian98a3b322013-01-24 16:10:35 +0100137 if (h->mem_info->daddr.sym) {
138 symlen = (int)h->mem_info->daddr.sym->namelen + 4
139 + unresolved_col_width + 2;
140 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
141 symlen);
Don Zickus9b32ba72014-06-01 15:38:29 +0200142 hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
143 symlen + 1);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100144 } else {
145 symlen = unresolved_col_width + 4 + 2;
146 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
147 symlen);
Jiri Olsa08059092016-01-20 12:56:33 +0100148 hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
149 symlen);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100150 }
Jiri Olsab34b3bf2015-10-05 20:06:08 +0200151
152 if (h->mem_info->iaddr.sym) {
153 symlen = (int)h->mem_info->iaddr.sym->namelen + 4
154 + unresolved_col_width + 2;
155 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
156 symlen);
157 } else {
158 symlen = unresolved_col_width + 4 + 2;
159 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
160 symlen);
161 }
162
Stephane Eranian98a3b322013-01-24 16:10:35 +0100163 if (h->mem_info->daddr.map) {
164 symlen = dso__name_len(h->mem_info->daddr.map->dso);
165 hists__new_col_len(hists, HISTC_MEM_DADDR_DSO,
166 symlen);
167 } else {
168 symlen = unresolved_col_width + 4 + 2;
169 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
170 }
Kan Liang8780fb22017-08-29 13:11:09 -0400171
172 hists__new_col_len(hists, HISTC_MEM_PHYS_DADDR,
173 unresolved_col_width + 4 + 2);
174
Stephane Eranian98a3b322013-01-24 16:10:35 +0100175 } else {
176 symlen = unresolved_col_width + 4 + 2;
177 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen);
Jiri Olsab34b3bf2015-10-05 20:06:08 +0200178 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL, symlen);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100179 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
180 }
181
Hari Bathinid890a982017-03-08 02:12:13 +0530182 hists__new_col_len(hists, HISTC_CGROUP_ID, 20);
Arnaldo Carvalho de Meloa4978ec2015-09-09 12:14:00 -0300183 hists__new_col_len(hists, HISTC_CPU, 3);
Kan Liang2e7ea3a2015-09-04 10:45:43 -0400184 hists__new_col_len(hists, HISTC_SOCKET, 6);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100185 hists__new_col_len(hists, HISTC_MEM_LOCKED, 6);
186 hists__new_col_len(hists, HISTC_MEM_TLB, 22);
187 hists__new_col_len(hists, HISTC_MEM_SNOOP, 12);
188 hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3);
189 hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12);
190 hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12);
Andi Kleen475eeab2013-09-20 07:40:43 -0700191
Jiri Olsaf666ac02016-09-19 15:10:10 +0200192 if (h->srcline) {
193 len = MAX(strlen(h->srcline), strlen(sort_srcline.se_header));
194 hists__new_col_len(hists, HISTC_SRCLINE, len);
195 }
Arnaldo Carvalho de Meloe8e6d372015-08-10 16:53:54 -0300196
Andi Kleen31191a82015-08-07 15:54:24 -0700197 if (h->srcfile)
198 hists__new_col_len(hists, HISTC_SRCFILE, strlen(h->srcfile));
199
Andi Kleen475eeab2013-09-20 07:40:43 -0700200 if (h->transaction)
201 hists__new_col_len(hists, HISTC_TRANSACTION,
202 hist_entry__transaction_len());
Namhyung Kim0c0af782016-02-21 23:22:38 +0900203
204 if (h->trace_output)
205 hists__new_col_len(hists, HISTC_TRACE, strlen(h->trace_output));
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300206}
207
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900208void hists__output_recalc_col_len(struct hists *hists, int max_rows)
209{
210 struct rb_node *next = rb_first(&hists->entries);
211 struct hist_entry *n;
212 int row = 0;
213
214 hists__reset_col_len(hists);
215
216 while (next && row++ < max_rows) {
217 n = rb_entry(next, struct hist_entry, rb_node);
218 if (!n->filtered)
219 hists__calc_col_len(hists, n);
220 next = rb_next(&n->rb_node);
221 }
222}
223
Namhyung Kimf39056f2014-01-14 14:25:37 +0900224static void he_stat__add_cpumode_period(struct he_stat *he_stat,
225 unsigned int cpumode, u64 period)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800226{
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300227 switch (cpumode) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800228 case PERF_RECORD_MISC_KERNEL:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900229 he_stat->period_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800230 break;
231 case PERF_RECORD_MISC_USER:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900232 he_stat->period_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800233 break;
234 case PERF_RECORD_MISC_GUEST_KERNEL:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900235 he_stat->period_guest_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800236 break;
237 case PERF_RECORD_MISC_GUEST_USER:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900238 he_stat->period_guest_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800239 break;
240 default:
241 break;
242 }
243}
244
Andi Kleen05484292013-01-24 16:10:29 +0100245static void he_stat__add_period(struct he_stat *he_stat, u64 period,
246 u64 weight)
Namhyung Kim139c0812012-10-04 21:49:43 +0900247{
Stephane Eranian98a3b322013-01-24 16:10:35 +0100248
Namhyung Kim139c0812012-10-04 21:49:43 +0900249 he_stat->period += period;
Andi Kleen05484292013-01-24 16:10:29 +0100250 he_stat->weight += weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900251 he_stat->nr_events += 1;
252}
253
254static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
255{
256 dest->period += src->period;
257 dest->period_sys += src->period_sys;
258 dest->period_us += src->period_us;
259 dest->period_guest_sys += src->period_guest_sys;
260 dest->period_guest_us += src->period_guest_us;
261 dest->nr_events += src->nr_events;
Andi Kleen05484292013-01-24 16:10:29 +0100262 dest->weight += src->weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900263}
264
Namhyung Kimf39056f2014-01-14 14:25:37 +0900265static void he_stat__decay(struct he_stat *he_stat)
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300266{
Namhyung Kimf39056f2014-01-14 14:25:37 +0900267 he_stat->period = (he_stat->period * 7) / 8;
268 he_stat->nr_events = (he_stat->nr_events * 7) / 8;
Andi Kleen05484292013-01-24 16:10:29 +0100269 /* XXX need decay for weight too? */
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300270}
271
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900272static void hists__delete_entry(struct hists *hists, struct hist_entry *he);
273
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300274static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
275{
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900276 u64 prev_period = he->stat.period;
Namhyung Kim3186b682014-04-22 13:44:23 +0900277 u64 diff;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200278
279 if (prev_period == 0)
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300280 return true;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200281
Namhyung Kimf39056f2014-01-14 14:25:37 +0900282 he_stat__decay(&he->stat);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900283 if (symbol_conf.cumulate_callchain)
284 he_stat__decay(he->stat_acc);
Namhyung Kim42b276a2016-01-05 12:06:00 +0900285 decay_callchain(he->callchain);
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200286
Namhyung Kim3186b682014-04-22 13:44:23 +0900287 diff = prev_period - he->stat.period;
288
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900289 if (!he->depth) {
290 hists->stats.total_period -= diff;
291 if (!he->filtered)
292 hists->stats.total_non_filtered_period -= diff;
293 }
294
295 if (!he->leaf) {
296 struct hist_entry *child;
297 struct rb_node *node = rb_first(&he->hroot_out);
298 while (node) {
299 child = rb_entry(node, struct hist_entry, rb_node);
300 node = rb_next(node);
301
302 if (hists__decay_entry(hists, child))
303 hists__delete_entry(hists, child);
304 }
305 }
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200306
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900307 return he->stat.period == 0;
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300308}
309
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300310static void hists__delete_entry(struct hists *hists, struct hist_entry *he)
311{
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900312 struct rb_root *root_in;
313 struct rb_root *root_out;
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300314
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900315 if (he->parent_he) {
316 root_in = &he->parent_he->hroot_in;
317 root_out = &he->parent_he->hroot_out;
318 } else {
Jiri Olsa52225032016-05-03 13:54:42 +0200319 if (hists__has(hists, need_collapse))
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900320 root_in = &hists->entries_collapsed;
321 else
322 root_in = hists->entries_in;
323 root_out = &hists->entries;
324 }
325
326 rb_erase(&he->rb_node_in, root_in);
327 rb_erase(&he->rb_node, root_out);
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300328
329 --hists->nr_entries;
330 if (!he->filtered)
331 --hists->nr_non_filtered_entries;
332
333 hist_entry__delete(he);
334}
335
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900336void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300337{
338 struct rb_node *next = rb_first(&hists->entries);
339 struct hist_entry *n;
340
341 while (next) {
342 n = rb_entry(next, struct hist_entry, rb_node);
343 next = rb_next(&n->rb_node);
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200344 if (((zap_user && n->level == '.') ||
345 (zap_kernel && n->level != '.') ||
Arnaldo Carvalho de Melo4c47f4f2015-03-17 17:18:58 -0300346 hists__decay_entry(hists, n))) {
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300347 hists__delete_entry(hists, n);
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300348 }
349 }
350}
351
Namhyung Kim701937b2014-08-12 17:16:05 +0900352void hists__delete_entries(struct hists *hists)
353{
354 struct rb_node *next = rb_first(&hists->entries);
355 struct hist_entry *n;
356
357 while (next) {
358 n = rb_entry(next, struct hist_entry, rb_node);
359 next = rb_next(&n->rb_node);
360
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300361 hists__delete_entry(hists, n);
Namhyung Kim701937b2014-08-12 17:16:05 +0900362 }
363}
364
John Kacur3d1d07e2009-09-28 15:32:55 +0200365/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300366 * histogram, sorted on item, collects periods
John Kacur3d1d07e2009-09-28 15:32:55 +0200367 */
368
Jiri Olsa0a269a62016-07-05 08:56:03 +0200369static int hist_entry__init(struct hist_entry *he,
370 struct hist_entry *template,
371 bool sample_self)
372{
373 *he = *template;
374
375 if (symbol_conf.cumulate_callchain) {
376 he->stat_acc = malloc(sizeof(he->stat));
377 if (he->stat_acc == NULL)
378 return -ENOMEM;
379 memcpy(he->stat_acc, &he->stat, sizeof(he->stat));
380 if (!sample_self)
381 memset(&he->stat, 0, sizeof(he->stat));
382 }
383
384 map__get(he->ms.map);
385
386 if (he->branch_info) {
387 /*
388 * This branch info is (a part of) allocated from
389 * sample__resolve_bstack() and will be freed after
390 * adding new entries. So we need to save a copy.
391 */
392 he->branch_info = malloc(sizeof(*he->branch_info));
393 if (he->branch_info == NULL) {
394 map__zput(he->ms.map);
395 free(he->stat_acc);
396 return -ENOMEM;
397 }
398
399 memcpy(he->branch_info, template->branch_info,
400 sizeof(*he->branch_info));
401
402 map__get(he->branch_info->from.map);
403 map__get(he->branch_info->to.map);
404 }
405
406 if (he->mem_info) {
407 map__get(he->mem_info->iaddr.map);
408 map__get(he->mem_info->daddr.map);
409 }
410
411 if (symbol_conf.use_callchain)
412 callchain_init(he->callchain);
413
414 if (he->raw_data) {
415 he->raw_data = memdup(he->raw_data, he->raw_size);
416
417 if (he->raw_data == NULL) {
418 map__put(he->ms.map);
419 if (he->branch_info) {
420 map__put(he->branch_info->from.map);
421 map__put(he->branch_info->to.map);
422 free(he->branch_info);
423 }
424 if (he->mem_info) {
425 map__put(he->mem_info->iaddr.map);
426 map__put(he->mem_info->daddr.map);
427 }
428 free(he->stat_acc);
429 return -ENOMEM;
430 }
431 }
432 INIT_LIST_HEAD(&he->pairs.node);
433 thread__get(he->thread);
Namhyung Kimd2580c72016-09-13 16:45:48 +0900434 he->hroot_in = RB_ROOT;
435 he->hroot_out = RB_ROOT;
Jiri Olsa0a269a62016-07-05 08:56:03 +0200436
437 if (!symbol_conf.report_hierarchy)
438 he->leaf = true;
439
440 return 0;
441}
442
Jiri Olsaf542e762016-07-05 08:56:04 +0200443static void *hist_entry__zalloc(size_t size)
444{
445 return zalloc(size + sizeof(struct hist_entry));
446}
447
448static void hist_entry__free(void *ptr)
449{
450 free(ptr);
451}
452
453static struct hist_entry_ops default_ops = {
454 .new = hist_entry__zalloc,
455 .free = hist_entry__free,
456};
457
Namhyung Kima0b51af2012-09-11 13:34:27 +0900458static struct hist_entry *hist_entry__new(struct hist_entry *template,
459 bool sample_self)
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300460{
Jiri Olsaf542e762016-07-05 08:56:04 +0200461 struct hist_entry_ops *ops = template->ops;
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900462 size_t callchain_size = 0;
463 struct hist_entry *he;
Jiri Olsa0a269a62016-07-05 08:56:03 +0200464 int err = 0;
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900465
Jiri Olsaf542e762016-07-05 08:56:04 +0200466 if (!ops)
467 ops = template->ops = &default_ops;
468
Namhyung Kim82aa0192014-12-22 13:44:14 +0900469 if (symbol_conf.use_callchain)
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900470 callchain_size = sizeof(struct callchain_root);
471
Jiri Olsaf542e762016-07-05 08:56:04 +0200472 he = ops->new(callchain_size);
Jiri Olsa0a269a62016-07-05 08:56:03 +0200473 if (he) {
474 err = hist_entry__init(he, template, sample_self);
Jiri Olsaf542e762016-07-05 08:56:04 +0200475 if (err) {
476 ops->free(he);
477 he = NULL;
478 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300479 }
480
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200481 return he;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300482}
483
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300484static u8 symbol__parent_filter(const struct symbol *parent)
485{
486 if (symbol_conf.exclude_other && parent == NULL)
487 return 1 << HIST_FILTER__PARENT;
488 return 0;
489}
490
Namhyung Kim467ef102016-02-16 23:08:19 +0900491static void hist_entry__add_callchain_period(struct hist_entry *he, u64 period)
492{
493 if (!symbol_conf.use_callchain)
494 return;
495
496 he->hists->callchain_period += period;
497 if (!he->filtered)
498 he->hists->callchain_non_filtered_period += period;
499}
500
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300501static struct hist_entry *hists__findnew_entry(struct hists *hists,
502 struct hist_entry *entry,
503 struct addr_location *al,
504 bool sample_self)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300505{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300506 struct rb_node **p;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300507 struct rb_node *parent = NULL;
508 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -0700509 int64_t cmp;
Namhyung Kimf1cbf782013-12-18 14:21:11 +0900510 u64 period = entry->stat.period;
511 u64 weight = entry->stat.weight;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300512
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300513 p = &hists->entries_in->rb_node;
514
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300515 while (*p != NULL) {
516 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300517 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300518
Namhyung Kim9afcf932012-12-10 17:29:54 +0900519 /*
520 * Make sure that it receives arguments in a same order as
521 * hist_entry__collapse() so that we can use an appropriate
522 * function when searching an entry regardless which sort
523 * keys were used.
524 */
525 cmp = hist_entry__cmp(he, entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300526
527 if (!cmp) {
Namhyung Kim0f584742016-01-28 00:40:49 +0900528 if (sample_self) {
Namhyung Kima0b51af2012-09-11 13:34:27 +0900529 he_stat__add_period(&he->stat, period, weight);
Namhyung Kim467ef102016-02-16 23:08:19 +0900530 hist_entry__add_callchain_period(he, period);
Namhyung Kim0f584742016-01-28 00:40:49 +0900531 }
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900532 if (symbol_conf.cumulate_callchain)
533 he_stat__add_period(he->stat_acc, period, weight);
David Miller63fa4712012-03-27 03:14:18 -0400534
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900535 /*
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -0300536 * This mem info was allocated from sample__resolve_mem
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900537 * and will not be used anymore.
538 */
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300539 zfree(&entry->mem_info);
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900540
David Miller63fa4712012-03-27 03:14:18 -0400541 /* If the map of an existing hist_entry has
542 * become out-of-date due to an exec() or
543 * similar, update it. Otherwise we will
544 * mis-adjust symbol addresses when computing
545 * the history counter to increment.
546 */
547 if (he->ms.map != entry->ms.map) {
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300548 map__put(he->ms.map);
549 he->ms.map = map__get(entry->ms.map);
David Miller63fa4712012-03-27 03:14:18 -0400550 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300551 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300552 }
553
554 if (cmp < 0)
555 p = &(*p)->rb_left;
556 else
557 p = &(*p)->rb_right;
558 }
559
Namhyung Kima0b51af2012-09-11 13:34:27 +0900560 he = hist_entry__new(entry, sample_self);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300561 if (!he)
Namhyung Kim27a0dcb2013-05-14 11:09:02 +0900562 return NULL;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300563
Namhyung Kim0f584742016-01-28 00:40:49 +0900564 if (sample_self)
Namhyung Kim467ef102016-02-16 23:08:19 +0900565 hist_entry__add_callchain_period(he, period);
566 hists->nr_entries++;
Namhyung Kim590cd342014-12-22 13:44:09 +0900567
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300568 rb_link_node(&he->rb_node_in, parent, p);
569 rb_insert_color(&he->rb_node_in, hists->entries_in);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300570out:
Namhyung Kima0b51af2012-09-11 13:34:27 +0900571 if (sample_self)
572 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900573 if (symbol_conf.cumulate_callchain)
574 he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300575 return he;
576}
577
Jiri Olsaa5051972016-07-05 08:56:05 +0200578static struct hist_entry*
579__hists__add_entry(struct hists *hists,
580 struct addr_location *al,
581 struct symbol *sym_parent,
582 struct branch_info *bi,
583 struct mem_info *mi,
584 struct perf_sample *sample,
585 bool sample_self,
586 struct hist_entry_ops *ops)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100587{
Hari Bathinid890a982017-03-08 02:12:13 +0530588 struct namespaces *ns = thread__namespaces(al->thread);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100589 struct hist_entry entry = {
590 .thread = al->thread,
Namhyung Kim4dfced32013-09-13 16:28:57 +0900591 .comm = thread__comm(al->thread),
Hari Bathinid890a982017-03-08 02:12:13 +0530592 .cgroup_id = {
593 .dev = ns ? ns->link_info[CGROUP_NS_INDEX].dev : 0,
594 .ino = ns ? ns->link_info[CGROUP_NS_INDEX].ino : 0,
595 },
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100596 .ms = {
597 .map = al->map,
598 .sym = al->sym,
599 },
Milian Wolff1fb7d062017-10-19 13:38:35 +0200600 .srcline = al->srcline ? strdup(al->srcline) : NULL,
Kan Liang0c4c4deb2015-09-04 10:45:42 -0400601 .socket = al->socket,
Don Zickus7365be52014-05-27 12:28:05 -0400602 .cpu = al->cpu,
603 .cpumode = al->cpumode,
604 .ip = al->addr,
605 .level = al->level,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900606 .stat = {
Namhyung Kimc4b35352012-10-04 21:49:42 +0900607 .nr_events = 1,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900608 .period = sample->period,
609 .weight = sample->weight,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900610 },
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100611 .parent = sym_parent,
Namhyung Kim2c86c7c2014-03-17 18:18:54 -0300612 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300613 .hists = hists,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900614 .branch_info = bi,
615 .mem_info = mi,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900616 .transaction = sample->transaction,
Namhyung Kim72392832015-12-24 11:16:17 +0900617 .raw_data = sample->raw_data,
618 .raw_size = sample->raw_size,
Jiri Olsaa5051972016-07-05 08:56:05 +0200619 .ops = ops,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100620 };
621
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300622 return hists__findnew_entry(hists, &entry, al, sample_self);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100623}
624
Jiri Olsaa5051972016-07-05 08:56:05 +0200625struct hist_entry *hists__add_entry(struct hists *hists,
626 struct addr_location *al,
627 struct symbol *sym_parent,
628 struct branch_info *bi,
629 struct mem_info *mi,
630 struct perf_sample *sample,
631 bool sample_self)
632{
633 return __hists__add_entry(hists, al, sym_parent, bi, mi,
634 sample, sample_self, NULL);
635}
636
637struct hist_entry *hists__add_entry_ops(struct hists *hists,
638 struct hist_entry_ops *ops,
639 struct addr_location *al,
640 struct symbol *sym_parent,
641 struct branch_info *bi,
642 struct mem_info *mi,
643 struct perf_sample *sample,
644 bool sample_self)
645{
646 return __hists__add_entry(hists, al, sym_parent, bi, mi,
647 sample, sample_self, ops);
648}
649
Namhyung Kim69bcb012013-10-30 09:40:34 +0900650static int
651iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
652 struct addr_location *al __maybe_unused)
653{
654 return 0;
655}
656
657static int
658iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
659 struct addr_location *al __maybe_unused)
660{
661 return 0;
662}
663
664static int
665iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
666{
667 struct perf_sample *sample = iter->sample;
668 struct mem_info *mi;
669
670 mi = sample__resolve_mem(sample, al);
671 if (mi == NULL)
672 return -ENOMEM;
673
674 iter->priv = mi;
675 return 0;
676}
677
678static int
679iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
680{
681 u64 cost;
682 struct mem_info *mi = iter->priv;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300683 struct hists *hists = evsel__hists(iter->evsel);
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900684 struct perf_sample *sample = iter->sample;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900685 struct hist_entry *he;
686
687 if (mi == NULL)
688 return -EINVAL;
689
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900690 cost = sample->weight;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900691 if (!cost)
692 cost = 1;
693
694 /*
695 * must pass period=weight in order to get the correct
696 * sorting from hists__collapse_resort() which is solely
697 * based on periods. We want sorting be done on nr_events * weight
698 * and this is indirectly achieved by passing period=weight here
699 * and the he_stat__add_period() function.
700 */
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900701 sample->period = cost;
702
Jiri Olsa0102ef32016-06-14 20:19:21 +0200703 he = hists__add_entry(hists, al, iter->parent, NULL, mi,
704 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900705 if (!he)
706 return -ENOMEM;
707
708 iter->he = he;
709 return 0;
710}
711
712static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900713iter_finish_mem_entry(struct hist_entry_iter *iter,
714 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900715{
716 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300717 struct hists *hists = evsel__hists(evsel);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900718 struct hist_entry *he = iter->he;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900719 int err = -EINVAL;
720
721 if (he == NULL)
722 goto out;
723
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300724 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900725
726 err = hist_entry__append_callchain(he, iter->sample);
727
728out:
729 /*
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300730 * We don't need to free iter->priv (mem_info) here since the mem info
731 * was either already freed in hists__findnew_entry() or passed to a
732 * new hist entry by hist_entry__new().
Namhyung Kim69bcb012013-10-30 09:40:34 +0900733 */
734 iter->priv = NULL;
735
736 iter->he = NULL;
737 return err;
738}
739
740static int
741iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
742{
743 struct branch_info *bi;
744 struct perf_sample *sample = iter->sample;
745
746 bi = sample__resolve_bstack(sample, al);
747 if (!bi)
748 return -ENOMEM;
749
750 iter->curr = 0;
751 iter->total = sample->branch_stack->nr;
752
753 iter->priv = bi;
754 return 0;
755}
756
757static int
Jin Yao2d78b182017-07-18 20:13:14 +0800758iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused,
Namhyung Kim69bcb012013-10-30 09:40:34 +0900759 struct addr_location *al __maybe_unused)
760{
761 return 0;
762}
763
764static int
765iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
766{
767 struct branch_info *bi = iter->priv;
768 int i = iter->curr;
769
770 if (bi == NULL)
771 return 0;
772
773 if (iter->curr >= iter->total)
774 return 0;
775
776 al->map = bi[i].to.map;
777 al->sym = bi[i].to.sym;
778 al->addr = bi[i].to.addr;
779 return 1;
780}
781
782static int
783iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
784{
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900785 struct branch_info *bi;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900786 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300787 struct hists *hists = evsel__hists(evsel);
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900788 struct perf_sample *sample = iter->sample;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900789 struct hist_entry *he = NULL;
790 int i = iter->curr;
791 int err = 0;
792
793 bi = iter->priv;
794
795 if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym))
796 goto out;
797
798 /*
799 * The report shows the percentage of total branches captured
800 * and not events sampled. Thus we use a pseudo period of 1.
801 */
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900802 sample->period = 1;
803 sample->weight = bi->flags.cycles ? bi->flags.cycles : 1;
804
Jiri Olsa0102ef32016-06-14 20:19:21 +0200805 he = hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
806 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900807 if (he == NULL)
808 return -ENOMEM;
809
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300810 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900811
812out:
813 iter->he = he;
814 iter->curr++;
815 return err;
816}
817
818static int
819iter_finish_branch_entry(struct hist_entry_iter *iter,
820 struct addr_location *al __maybe_unused)
821{
822 zfree(&iter->priv);
823 iter->he = NULL;
824
825 return iter->curr >= iter->total ? 0 : -1;
826}
827
828static int
829iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
830 struct addr_location *al __maybe_unused)
831{
832 return 0;
833}
834
835static int
836iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
837{
838 struct perf_evsel *evsel = iter->evsel;
839 struct perf_sample *sample = iter->sample;
840 struct hist_entry *he;
841
Jiri Olsa0102ef32016-06-14 20:19:21 +0200842 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
843 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900844 if (he == NULL)
845 return -ENOMEM;
846
847 iter->he = he;
848 return 0;
849}
850
851static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900852iter_finish_normal_entry(struct hist_entry_iter *iter,
853 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900854{
Namhyung Kim69bcb012013-10-30 09:40:34 +0900855 struct hist_entry *he = iter->he;
856 struct perf_evsel *evsel = iter->evsel;
857 struct perf_sample *sample = iter->sample;
858
859 if (he == NULL)
860 return 0;
861
862 iter->he = NULL;
863
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300864 hists__inc_nr_samples(evsel__hists(evsel), he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900865
866 return hist_entry__append_callchain(he, sample);
867}
868
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900869static int
Adrian Hunter96b40f32015-09-25 16:15:47 +0300870iter_prepare_cumulative_entry(struct hist_entry_iter *iter,
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900871 struct addr_location *al __maybe_unused)
872{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900873 struct hist_entry **he_cache;
874
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900875 callchain_cursor_commit(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900876
877 /*
878 * This is for detecting cycles or recursions so that they're
879 * cumulated only one time to prevent entries more than 100%
880 * overhead.
881 */
Jiri Olsae3ebaa42018-02-16 13:36:19 +0100882 he_cache = malloc(sizeof(*he_cache) * (callchain_cursor.nr + 1));
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900883 if (he_cache == NULL)
884 return -ENOMEM;
885
886 iter->priv = he_cache;
887 iter->curr = 0;
888
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900889 return 0;
890}
891
892static int
893iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
894 struct addr_location *al)
895{
896 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300897 struct hists *hists = evsel__hists(evsel);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900898 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900899 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900900 struct hist_entry *he;
901 int err = 0;
902
Jiri Olsa0102ef32016-06-14 20:19:21 +0200903 he = hists__add_entry(hists, al, iter->parent, NULL, NULL,
904 sample, true);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900905 if (he == NULL)
906 return -ENOMEM;
907
908 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900909 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900910
Namhyung Kim82aa0192014-12-22 13:44:14 +0900911 hist_entry__append_callchain(he, sample);
Namhyung Kimbe7f8552013-12-26 17:44:10 +0900912
913 /*
914 * We need to re-initialize the cursor since callchain_append()
915 * advanced the cursor to the end.
916 */
917 callchain_cursor_commit(&callchain_cursor);
918
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300919 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900920
921 return err;
922}
923
924static int
925iter_next_cumulative_entry(struct hist_entry_iter *iter,
926 struct addr_location *al)
927{
928 struct callchain_cursor_node *node;
929
930 node = callchain_cursor_current(&callchain_cursor);
931 if (node == NULL)
932 return 0;
933
Namhyung Kimc7405d82013-10-31 13:58:30 +0900934 return fill_callchain_info(al, node, iter->hide_unresolved);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900935}
936
937static int
938iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
939 struct addr_location *al)
940{
941 struct perf_evsel *evsel = iter->evsel;
942 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900943 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900944 struct hist_entry *he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900945 struct hist_entry he_tmp = {
Arnaldo Carvalho de Melo5cef8972015-08-10 15:45:55 -0300946 .hists = evsel__hists(evsel),
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900947 .cpu = al->cpu,
948 .thread = al->thread,
949 .comm = thread__comm(al->thread),
950 .ip = al->addr,
951 .ms = {
952 .map = al->map,
953 .sym = al->sym,
954 },
Milian Wolff1fb7d062017-10-19 13:38:35 +0200955 .srcline = al->srcline ? strdup(al->srcline) : NULL,
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900956 .parent = iter->parent,
Namhyung Kim72392832015-12-24 11:16:17 +0900957 .raw_data = sample->raw_data,
958 .raw_size = sample->raw_size,
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900959 };
960 int i;
Namhyung Kimbe7f8552013-12-26 17:44:10 +0900961 struct callchain_cursor cursor;
962
963 callchain_cursor_snapshot(&cursor, &callchain_cursor);
964
965 callchain_cursor_advance(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900966
967 /*
968 * Check if there's duplicate entries in the callchain.
969 * It's possible that it has cycles or recursive calls.
970 */
971 for (i = 0; i < iter->curr; i++) {
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900972 if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
973 /* to avoid calling callback function */
974 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900975 return 0;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900976 }
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900977 }
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900978
Jiri Olsa0102ef32016-06-14 20:19:21 +0200979 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
980 sample, false);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900981 if (he == NULL)
982 return -ENOMEM;
983
984 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900985 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900986
Namhyung Kim82aa0192014-12-22 13:44:14 +0900987 if (symbol_conf.use_callchain)
988 callchain_append(he->callchain, &cursor, sample->period);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900989 return 0;
990}
991
992static int
993iter_finish_cumulative_entry(struct hist_entry_iter *iter,
994 struct addr_location *al __maybe_unused)
995{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900996 zfree(&iter->priv);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900997 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900998
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900999 return 0;
1000}
1001
Namhyung Kim69bcb012013-10-30 09:40:34 +09001002const struct hist_iter_ops hist_iter_mem = {
1003 .prepare_entry = iter_prepare_mem_entry,
1004 .add_single_entry = iter_add_single_mem_entry,
1005 .next_entry = iter_next_nop_entry,
1006 .add_next_entry = iter_add_next_nop_entry,
1007 .finish_entry = iter_finish_mem_entry,
1008};
1009
1010const struct hist_iter_ops hist_iter_branch = {
1011 .prepare_entry = iter_prepare_branch_entry,
1012 .add_single_entry = iter_add_single_branch_entry,
1013 .next_entry = iter_next_branch_entry,
1014 .add_next_entry = iter_add_next_branch_entry,
1015 .finish_entry = iter_finish_branch_entry,
1016};
1017
1018const struct hist_iter_ops hist_iter_normal = {
1019 .prepare_entry = iter_prepare_normal_entry,
1020 .add_single_entry = iter_add_single_normal_entry,
1021 .next_entry = iter_next_nop_entry,
1022 .add_next_entry = iter_add_next_nop_entry,
1023 .finish_entry = iter_finish_normal_entry,
1024};
1025
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001026const struct hist_iter_ops hist_iter_cumulative = {
1027 .prepare_entry = iter_prepare_cumulative_entry,
1028 .add_single_entry = iter_add_single_cumulative_entry,
1029 .next_entry = iter_next_cumulative_entry,
1030 .add_next_entry = iter_add_next_cumulative_entry,
1031 .finish_entry = iter_finish_cumulative_entry,
1032};
1033
Namhyung Kim69bcb012013-10-30 09:40:34 +09001034int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001035 int max_stack_depth, void *arg)
Namhyung Kim69bcb012013-10-30 09:40:34 +09001036{
1037 int err, err2;
Krister Johansen9c68ae92017-01-05 22:23:31 -08001038 struct map *alm = NULL;
1039
1040 if (al && al->map)
1041 alm = map__get(al->map);
Namhyung Kim69bcb012013-10-30 09:40:34 +09001042
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001043 err = sample__resolve_callchain(iter->sample, &callchain_cursor, &iter->parent,
Namhyung Kim063bd932015-05-19 17:04:10 +09001044 iter->evsel, al, max_stack_depth);
Namhyung Kim69bcb012013-10-30 09:40:34 +09001045 if (err)
1046 return err;
1047
Namhyung Kim69bcb012013-10-30 09:40:34 +09001048 err = iter->ops->prepare_entry(iter, al);
1049 if (err)
1050 goto out;
1051
1052 err = iter->ops->add_single_entry(iter, al);
1053 if (err)
1054 goto out;
1055
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001056 if (iter->he && iter->add_entry_cb) {
1057 err = iter->add_entry_cb(iter, al, true, arg);
1058 if (err)
1059 goto out;
1060 }
1061
Namhyung Kim69bcb012013-10-30 09:40:34 +09001062 while (iter->ops->next_entry(iter, al)) {
1063 err = iter->ops->add_next_entry(iter, al);
1064 if (err)
1065 break;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001066
1067 if (iter->he && iter->add_entry_cb) {
1068 err = iter->add_entry_cb(iter, al, false, arg);
1069 if (err)
1070 goto out;
1071 }
Namhyung Kim69bcb012013-10-30 09:40:34 +09001072 }
1073
1074out:
1075 err2 = iter->ops->finish_entry(iter, al);
1076 if (!err)
1077 err = err2;
1078
Krister Johansen9c68ae92017-01-05 22:23:31 -08001079 map__put(alm);
1080
Namhyung Kim69bcb012013-10-30 09:40:34 +09001081 return err;
1082}
1083
John Kacur3d1d07e2009-09-28 15:32:55 +02001084int64_t
1085hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
1086{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001087 struct hists *hists = left->hists;
Namhyung Kim093f0ef32014-03-03 12:07:47 +09001088 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +02001089 int64_t cmp = 0;
1090
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001091 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim84b6ee82016-02-27 03:52:43 +09001092 if (perf_hpp__is_dynamic_entry(fmt) &&
1093 !perf_hpp__defined_dynamic_entry(fmt, hists))
1094 continue;
1095
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001096 cmp = fmt->cmp(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +02001097 if (cmp)
1098 break;
1099 }
1100
1101 return cmp;
1102}
1103
1104int64_t
1105hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
1106{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001107 struct hists *hists = left->hists;
Namhyung Kim093f0ef32014-03-03 12:07:47 +09001108 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +02001109 int64_t cmp = 0;
1110
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001111 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim84b6ee82016-02-27 03:52:43 +09001112 if (perf_hpp__is_dynamic_entry(fmt) &&
1113 !perf_hpp__defined_dynamic_entry(fmt, hists))
1114 continue;
1115
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001116 cmp = fmt->collapse(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +02001117 if (cmp)
1118 break;
1119 }
1120
1121 return cmp;
1122}
1123
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -03001124void hist_entry__delete(struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +02001125{
Jiri Olsaf542e762016-07-05 08:56:04 +02001126 struct hist_entry_ops *ops = he->ops;
1127
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001128 thread__zput(he->thread);
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -03001129 map__zput(he->ms.map);
1130
1131 if (he->branch_info) {
1132 map__zput(he->branch_info->from.map);
1133 map__zput(he->branch_info->to.map);
Andi Kleen508be0d2016-05-20 13:15:08 -07001134 free_srcline(he->branch_info->srcline_from);
1135 free_srcline(he->branch_info->srcline_to);
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -03001136 zfree(&he->branch_info);
1137 }
1138
1139 if (he->mem_info) {
1140 map__zput(he->mem_info->iaddr.map);
1141 map__zput(he->mem_info->daddr.map);
1142 zfree(&he->mem_info);
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
Jin Yaob49a8212017-05-08 18:43:02 +08001762 use_callchain |= symbol_conf.show_branchflag_count;
1763
Jiri Olsa52c5cc32016-08-01 20:02:34 +02001764 output_resort(evsel__hists(evsel), prog, use_callchain, NULL);
Jiri Olsa452ce032016-01-18 10:24:00 +01001765}
1766
1767void hists__output_resort(struct hists *hists, struct ui_progress *prog)
1768{
Jiri Olsa52c5cc32016-08-01 20:02:34 +02001769 output_resort(hists, prog, symbol_conf.use_callchain, NULL);
1770}
1771
1772void hists__output_resort_cb(struct hists *hists, struct ui_progress *prog,
1773 hists__resort_cb_t cb)
1774{
1775 output_resort(hists, prog, symbol_conf.use_callchain, cb);
Jiri Olsa01441af2016-01-18 10:23:59 +01001776}
1777
Namhyung Kim8c018722016-02-25 00:13:36 +09001778static bool can_goto_child(struct hist_entry *he, enum hierarchy_move_dir hmd)
1779{
1780 if (he->leaf || hmd == HMD_FORCE_SIBLING)
1781 return false;
1782
1783 if (he->unfolded || hmd == HMD_FORCE_CHILD)
1784 return true;
1785
1786 return false;
1787}
1788
1789struct rb_node *rb_hierarchy_last(struct rb_node *node)
1790{
1791 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1792
1793 while (can_goto_child(he, HMD_NORMAL)) {
1794 node = rb_last(&he->hroot_out);
1795 he = rb_entry(node, struct hist_entry, rb_node);
1796 }
1797 return node;
1798}
1799
1800struct rb_node *__rb_hierarchy_next(struct rb_node *node, enum hierarchy_move_dir hmd)
1801{
1802 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1803
1804 if (can_goto_child(he, hmd))
1805 node = rb_first(&he->hroot_out);
1806 else
1807 node = rb_next(node);
1808
1809 while (node == NULL) {
1810 he = he->parent_he;
1811 if (he == NULL)
1812 break;
1813
1814 node = rb_next(&he->rb_node);
1815 }
1816 return node;
1817}
1818
1819struct rb_node *rb_hierarchy_prev(struct rb_node *node)
1820{
1821 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1822
1823 node = rb_prev(node);
1824 if (node)
1825 return rb_hierarchy_last(node);
1826
1827 he = he->parent_he;
1828 if (he == NULL)
1829 return NULL;
1830
1831 return &he->rb_node;
1832}
1833
Namhyung Kima7b58952016-02-26 21:13:16 +09001834bool hist_entry__has_hierarchy_children(struct hist_entry *he, float limit)
1835{
1836 struct rb_node *node;
1837 struct hist_entry *child;
1838 float percent;
1839
1840 if (he->leaf)
1841 return false;
1842
1843 node = rb_first(&he->hroot_out);
1844 child = rb_entry(node, struct hist_entry, rb_node);
1845
1846 while (node && child->filtered) {
1847 node = rb_next(node);
1848 child = rb_entry(node, struct hist_entry, rb_node);
1849 }
1850
1851 if (node)
1852 percent = hist_entry__get_percent_limit(child);
1853 else
1854 percent = 0;
1855
1856 return node && percent >= limit;
1857}
1858
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001859static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001860 enum hist_filter filter)
1861{
1862 h->filtered &= ~(1 << filter);
Namhyung Kim155e9af2016-02-25 00:13:38 +09001863
1864 if (symbol_conf.report_hierarchy) {
1865 struct hist_entry *parent = h->parent_he;
1866
1867 while (parent) {
1868 he_stat__add_stat(&parent->stat, &h->stat);
1869
1870 parent->filtered &= ~(1 << filter);
1871
1872 if (parent->filtered)
1873 goto next;
1874
1875 /* force fold unfiltered entry for simplicity */
1876 parent->unfolded = false;
Namhyung Kim79dded82016-02-26 21:13:19 +09001877 parent->has_no_entry = false;
Namhyung Kim155e9af2016-02-25 00:13:38 +09001878 parent->row_offset = 0;
1879 parent->nr_rows = 0;
1880next:
1881 parent = parent->parent_he;
1882 }
1883 }
1884
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001885 if (h->filtered)
1886 return;
1887
Namhyung Kim87e90f42014-04-24 16:44:16 +09001888 /* force fold unfiltered entry for simplicity */
Namhyung Kim3698dab2015-05-05 23:55:46 +09001889 h->unfolded = false;
Namhyung Kim79dded82016-02-26 21:13:19 +09001890 h->has_no_entry = false;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -03001891 h->row_offset = 0;
He Kuanga8cd1f42015-03-11 20:36:03 +08001892 h->nr_rows = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001893
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001894 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001895
Namhyung Kim9283ba92014-04-24 16:37:26 +09001896 hists__inc_filter_stats(hists, h);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001897 hists__calc_col_len(hists, h);
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001898}
1899
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001900
1901static bool hists__filter_entry_by_dso(struct hists *hists,
1902 struct hist_entry *he)
1903{
1904 if (hists->dso_filter != NULL &&
1905 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
1906 he->filtered |= (1 << HIST_FILTER__DSO);
1907 return true;
1908 }
1909
1910 return false;
1911}
1912
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001913static bool hists__filter_entry_by_thread(struct hists *hists,
1914 struct hist_entry *he)
1915{
1916 if (hists->thread_filter != NULL &&
1917 he->thread != hists->thread_filter) {
1918 he->filtered |= (1 << HIST_FILTER__THREAD);
1919 return true;
1920 }
1921
1922 return false;
1923}
1924
Namhyung Kime94d53e2012-03-16 17:50:51 +09001925static bool hists__filter_entry_by_symbol(struct hists *hists,
1926 struct hist_entry *he)
1927{
1928 if (hists->symbol_filter_str != NULL &&
1929 (!he->ms.sym || strstr(he->ms.sym->name,
1930 hists->symbol_filter_str) == NULL)) {
1931 he->filtered |= (1 << HIST_FILTER__SYMBOL);
1932 return true;
1933 }
1934
1935 return false;
1936}
1937
Kan Liang21394d92015-09-04 10:45:44 -04001938static bool hists__filter_entry_by_socket(struct hists *hists,
1939 struct hist_entry *he)
1940{
1941 if ((hists->socket_filter > -1) &&
1942 (he->socket != hists->socket_filter)) {
1943 he->filtered |= (1 << HIST_FILTER__SOCKET);
1944 return true;
1945 }
1946
1947 return false;
1948}
1949
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001950typedef bool (*filter_fn_t)(struct hists *hists, struct hist_entry *he);
1951
1952static void hists__filter_by_type(struct hists *hists, int type, filter_fn_t filter)
Kan Liang84734b02015-09-04 10:45:45 -04001953{
1954 struct rb_node *nd;
1955
1956 hists->stats.nr_non_filtered_samples = 0;
1957
1958 hists__reset_filter_stats(hists);
1959 hists__reset_col_len(hists);
1960
1961 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1962 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1963
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001964 if (filter(hists, h))
Kan Liang84734b02015-09-04 10:45:45 -04001965 continue;
1966
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001967 hists__remove_entry_filter(hists, h, type);
Kan Liang84734b02015-09-04 10:45:45 -04001968 }
1969}
1970
Namhyung Kim70642852016-02-25 00:13:39 +09001971static void resort_filtered_entry(struct rb_root *root, struct hist_entry *he)
1972{
1973 struct rb_node **p = &root->rb_node;
1974 struct rb_node *parent = NULL;
1975 struct hist_entry *iter;
1976 struct rb_root new_root = RB_ROOT;
1977 struct rb_node *nd;
1978
1979 while (*p != NULL) {
1980 parent = *p;
1981 iter = rb_entry(parent, struct hist_entry, rb_node);
1982
1983 if (hist_entry__sort(he, iter) > 0)
1984 p = &(*p)->rb_left;
1985 else
1986 p = &(*p)->rb_right;
1987 }
1988
1989 rb_link_node(&he->rb_node, parent, p);
1990 rb_insert_color(&he->rb_node, root);
1991
1992 if (he->leaf || he->filtered)
1993 return;
1994
1995 nd = rb_first(&he->hroot_out);
1996 while (nd) {
1997 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1998
1999 nd = rb_next(nd);
2000 rb_erase(&h->rb_node, &he->hroot_out);
2001
2002 resort_filtered_entry(&new_root, h);
2003 }
2004
2005 he->hroot_out = new_root;
2006}
2007
Namhyung Kim155e9af2016-02-25 00:13:38 +09002008static void hists__filter_hierarchy(struct hists *hists, int type, const void *arg)
2009{
2010 struct rb_node *nd;
Namhyung Kim70642852016-02-25 00:13:39 +09002011 struct rb_root new_root = RB_ROOT;
Namhyung Kim155e9af2016-02-25 00:13:38 +09002012
2013 hists->stats.nr_non_filtered_samples = 0;
2014
2015 hists__reset_filter_stats(hists);
2016 hists__reset_col_len(hists);
2017
2018 nd = rb_first(&hists->entries);
2019 while (nd) {
2020 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2021 int ret;
2022
2023 ret = hist_entry__filter(h, type, arg);
2024
2025 /*
2026 * case 1. non-matching type
2027 * zero out the period, set filter marker and move to child
2028 */
2029 if (ret < 0) {
2030 memset(&h->stat, 0, sizeof(h->stat));
2031 h->filtered |= (1 << type);
2032
2033 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_CHILD);
2034 }
2035 /*
2036 * case 2. matched type (filter out)
2037 * set filter marker and move to next
2038 */
2039 else if (ret == 1) {
2040 h->filtered |= (1 << type);
2041
2042 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
2043 }
2044 /*
2045 * case 3. ok (not filtered)
2046 * add period to hists and parents, erase the filter marker
2047 * and move to next sibling
2048 */
2049 else {
2050 hists__remove_entry_filter(hists, h, type);
2051
2052 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
2053 }
2054 }
Namhyung Kim70642852016-02-25 00:13:39 +09002055
Namhyung Kimf7fb5382016-03-09 22:47:02 +09002056 hierarchy_recalc_total_periods(hists);
2057
Namhyung Kim70642852016-02-25 00:13:39 +09002058 /*
2059 * resort output after applying a new filter since filter in a lower
2060 * hierarchy can change periods in a upper hierarchy.
2061 */
2062 nd = rb_first(&hists->entries);
2063 while (nd) {
2064 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2065
2066 nd = rb_next(nd);
2067 rb_erase(&h->rb_node, &hists->entries);
2068
2069 resort_filtered_entry(&new_root, h);
2070 }
2071
2072 hists->entries = new_root;
Namhyung Kim155e9af2016-02-25 00:13:38 +09002073}
2074
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002075void hists__filter_by_thread(struct hists *hists)
2076{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002077 if (symbol_conf.report_hierarchy)
2078 hists__filter_hierarchy(hists, HIST_FILTER__THREAD,
2079 hists->thread_filter);
2080 else
2081 hists__filter_by_type(hists, HIST_FILTER__THREAD,
2082 hists__filter_entry_by_thread);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002083}
2084
2085void hists__filter_by_dso(struct hists *hists)
2086{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002087 if (symbol_conf.report_hierarchy)
2088 hists__filter_hierarchy(hists, HIST_FILTER__DSO,
2089 hists->dso_filter);
2090 else
2091 hists__filter_by_type(hists, HIST_FILTER__DSO,
2092 hists__filter_entry_by_dso);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002093}
2094
2095void hists__filter_by_symbol(struct hists *hists)
2096{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002097 if (symbol_conf.report_hierarchy)
2098 hists__filter_hierarchy(hists, HIST_FILTER__SYMBOL,
2099 hists->symbol_filter_str);
2100 else
2101 hists__filter_by_type(hists, HIST_FILTER__SYMBOL,
2102 hists__filter_entry_by_symbol);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002103}
2104
2105void hists__filter_by_socket(struct hists *hists)
2106{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002107 if (symbol_conf.report_hierarchy)
2108 hists__filter_hierarchy(hists, HIST_FILTER__SOCKET,
2109 &hists->socket_filter);
2110 else
2111 hists__filter_by_type(hists, HIST_FILTER__SOCKET,
2112 hists__filter_entry_by_socket);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002113}
2114
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03002115void events_stats__inc(struct events_stats *stats, u32 type)
2116{
2117 ++stats->nr_events[0];
2118 ++stats->nr_events[type];
2119}
2120
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03002121void hists__inc_nr_events(struct hists *hists, u32 type)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03002122{
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03002123 events_stats__inc(&hists->stats, type);
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03002124}
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002125
Namhyung Kim1844dbc2014-05-28 14:12:18 +09002126void hists__inc_nr_samples(struct hists *hists, bool filtered)
2127{
2128 events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
2129 if (!filtered)
2130 hists->stats.nr_non_filtered_samples++;
2131}
2132
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002133static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
2134 struct hist_entry *pair)
2135{
Namhyung Kimce74f602012-12-10 17:29:55 +09002136 struct rb_root *root;
2137 struct rb_node **p;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002138 struct rb_node *parent = NULL;
2139 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -07002140 int64_t cmp;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002141
Jiri Olsa52225032016-05-03 13:54:42 +02002142 if (hists__has(hists, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002143 root = &hists->entries_collapsed;
2144 else
2145 root = hists->entries_in;
2146
2147 p = &root->rb_node;
2148
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002149 while (*p != NULL) {
2150 parent = *p;
Namhyung Kimce74f602012-12-10 17:29:55 +09002151 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002152
Namhyung Kimce74f602012-12-10 17:29:55 +09002153 cmp = hist_entry__collapse(he, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002154
2155 if (!cmp)
2156 goto out;
2157
2158 if (cmp < 0)
2159 p = &(*p)->rb_left;
2160 else
2161 p = &(*p)->rb_right;
2162 }
2163
Namhyung Kima0b51af2012-09-11 13:34:27 +09002164 he = hist_entry__new(pair, true);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002165 if (he) {
Arnaldo Carvalho de Melo30193d72012-11-12 13:20:03 -03002166 memset(&he->stat, 0, sizeof(he->stat));
2167 he->hists = hists;
Kan Liang09623d72016-04-24 23:28:09 -07002168 if (symbol_conf.cumulate_callchain)
2169 memset(he->stat_acc, 0, sizeof(he->stat));
Namhyung Kimce74f602012-12-10 17:29:55 +09002170 rb_link_node(&he->rb_node_in, parent, p);
2171 rb_insert_color(&he->rb_node_in, root);
Namhyung Kim62638352014-04-24 16:21:46 +09002172 hists__inc_stats(hists, he);
Jiri Olsae0af43d2012-12-01 21:18:20 +01002173 he->dummy = true;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002174 }
2175out:
2176 return he;
2177}
2178
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002179static struct hist_entry *add_dummy_hierarchy_entry(struct hists *hists,
2180 struct rb_root *root,
2181 struct hist_entry *pair)
2182{
2183 struct rb_node **p;
2184 struct rb_node *parent = NULL;
2185 struct hist_entry *he;
2186 struct perf_hpp_fmt *fmt;
2187
2188 p = &root->rb_node;
2189 while (*p != NULL) {
2190 int64_t cmp = 0;
2191
2192 parent = *p;
2193 he = rb_entry(parent, struct hist_entry, rb_node_in);
2194
2195 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
2196 cmp = fmt->collapse(fmt, he, pair);
2197 if (cmp)
2198 break;
2199 }
2200 if (!cmp)
2201 goto out;
2202
2203 if (cmp < 0)
2204 p = &parent->rb_left;
2205 else
2206 p = &parent->rb_right;
2207 }
2208
2209 he = hist_entry__new(pair, true);
2210 if (he) {
2211 rb_link_node(&he->rb_node_in, parent, p);
2212 rb_insert_color(&he->rb_node_in, root);
2213
2214 he->dummy = true;
2215 he->hists = hists;
2216 memset(&he->stat, 0, sizeof(he->stat));
2217 hists__inc_stats(hists, he);
2218 }
2219out:
2220 return he;
2221}
2222
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002223static struct hist_entry *hists__find_entry(struct hists *hists,
2224 struct hist_entry *he)
2225{
Namhyung Kimce74f602012-12-10 17:29:55 +09002226 struct rb_node *n;
2227
Jiri Olsa52225032016-05-03 13:54:42 +02002228 if (hists__has(hists, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002229 n = hists->entries_collapsed.rb_node;
2230 else
2231 n = hists->entries_in->rb_node;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002232
2233 while (n) {
Namhyung Kimce74f602012-12-10 17:29:55 +09002234 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
2235 int64_t cmp = hist_entry__collapse(iter, he);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002236
2237 if (cmp < 0)
2238 n = n->rb_left;
2239 else if (cmp > 0)
2240 n = n->rb_right;
2241 else
2242 return iter;
2243 }
2244
2245 return NULL;
2246}
2247
Namhyung Kim09034de2016-09-13 16:45:46 +09002248static struct hist_entry *hists__find_hierarchy_entry(struct rb_root *root,
2249 struct hist_entry *he)
2250{
2251 struct rb_node *n = root->rb_node;
2252
2253 while (n) {
2254 struct hist_entry *iter;
2255 struct perf_hpp_fmt *fmt;
2256 int64_t cmp = 0;
2257
2258 iter = rb_entry(n, struct hist_entry, rb_node_in);
2259 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
2260 cmp = fmt->collapse(fmt, iter, he);
2261 if (cmp)
2262 break;
2263 }
2264
2265 if (cmp < 0)
2266 n = n->rb_left;
2267 else if (cmp > 0)
2268 n = n->rb_right;
2269 else
2270 return iter;
2271 }
2272
2273 return NULL;
2274}
2275
2276static void hists__match_hierarchy(struct rb_root *leader_root,
2277 struct rb_root *other_root)
2278{
2279 struct rb_node *nd;
2280 struct hist_entry *pos, *pair;
2281
2282 for (nd = rb_first(leader_root); nd; nd = rb_next(nd)) {
2283 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2284 pair = hists__find_hierarchy_entry(other_root, pos);
2285
2286 if (pair) {
2287 hist_entry__add_pair(pair, pos);
2288 hists__match_hierarchy(&pos->hroot_in, &pair->hroot_in);
2289 }
2290 }
2291}
2292
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002293/*
2294 * Look for pairs to link to the leader buckets (hist_entries):
2295 */
2296void hists__match(struct hists *leader, struct hists *other)
2297{
Namhyung Kimce74f602012-12-10 17:29:55 +09002298 struct rb_root *root;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002299 struct rb_node *nd;
2300 struct hist_entry *pos, *pair;
2301
Namhyung Kim09034de2016-09-13 16:45:46 +09002302 if (symbol_conf.report_hierarchy) {
2303 /* hierarchy report always collapses entries */
2304 return hists__match_hierarchy(&leader->entries_collapsed,
2305 &other->entries_collapsed);
2306 }
2307
Jiri Olsa52225032016-05-03 13:54:42 +02002308 if (hists__has(leader, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002309 root = &leader->entries_collapsed;
2310 else
2311 root = leader->entries_in;
2312
2313 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
2314 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002315 pair = hists__find_entry(other, pos);
2316
2317 if (pair)
Namhyung Kim5fa90412012-11-29 15:38:34 +09002318 hist_entry__add_pair(pair, pos);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002319 }
2320}
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002321
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002322static int hists__link_hierarchy(struct hists *leader_hists,
2323 struct hist_entry *parent,
2324 struct rb_root *leader_root,
2325 struct rb_root *other_root)
2326{
2327 struct rb_node *nd;
2328 struct hist_entry *pos, *leader;
2329
2330 for (nd = rb_first(other_root); nd; nd = rb_next(nd)) {
2331 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2332
2333 if (hist_entry__has_pairs(pos)) {
2334 bool found = false;
2335
2336 list_for_each_entry(leader, &pos->pairs.head, pairs.node) {
2337 if (leader->hists == leader_hists) {
2338 found = true;
2339 break;
2340 }
2341 }
2342 if (!found)
2343 return -1;
2344 } else {
2345 leader = add_dummy_hierarchy_entry(leader_hists,
2346 leader_root, pos);
2347 if (leader == NULL)
2348 return -1;
2349
2350 /* do not point parent in the pos */
2351 leader->parent_he = parent;
2352
2353 hist_entry__add_pair(pos, leader);
2354 }
2355
2356 if (!pos->leaf) {
2357 if (hists__link_hierarchy(leader_hists, leader,
2358 &leader->hroot_in,
2359 &pos->hroot_in) < 0)
2360 return -1;
2361 }
2362 }
2363 return 0;
2364}
2365
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002366/*
2367 * Look for entries in the other hists that are not present in the leader, if
2368 * we find them, just add a dummy entry on the leader hists, with period=0,
2369 * nr_events=0, to serve as the list header.
2370 */
2371int hists__link(struct hists *leader, struct hists *other)
2372{
Namhyung Kimce74f602012-12-10 17:29:55 +09002373 struct rb_root *root;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002374 struct rb_node *nd;
2375 struct hist_entry *pos, *pair;
2376
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002377 if (symbol_conf.report_hierarchy) {
2378 /* hierarchy report always collapses entries */
2379 return hists__link_hierarchy(leader, NULL,
2380 &leader->entries_collapsed,
2381 &other->entries_collapsed);
2382 }
2383
Jiri Olsa52225032016-05-03 13:54:42 +02002384 if (hists__has(other, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002385 root = &other->entries_collapsed;
2386 else
2387 root = other->entries_in;
2388
2389 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
2390 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002391
2392 if (!hist_entry__has_pairs(pos)) {
2393 pair = hists__add_dummy_entry(leader, pos);
2394 if (pair == NULL)
2395 return -1;
Namhyung Kim5fa90412012-11-29 15:38:34 +09002396 hist_entry__add_pair(pos, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002397 }
2398 }
2399
2400 return 0;
2401}
Namhyung Kimf2148332014-01-14 11:52:48 +09002402
Andi Kleen578499982015-07-18 08:24:49 -07002403void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
2404 struct perf_sample *sample, bool nonany_branch_mode)
2405{
2406 struct branch_info *bi;
2407
2408 /* If we have branch cycles always annotate them. */
2409 if (bs && bs->nr && bs->entries[0].flags.cycles) {
2410 int i;
2411
2412 bi = sample__resolve_bstack(sample, al);
2413 if (bi) {
2414 struct addr_map_symbol *prev = NULL;
2415
2416 /*
2417 * Ignore errors, still want to process the
2418 * other entries.
2419 *
2420 * For non standard branch modes always
2421 * force no IPC (prev == NULL)
2422 *
2423 * Note that perf stores branches reversed from
2424 * program order!
2425 */
2426 for (i = bs->nr - 1; i >= 0; i--) {
2427 addr_map_symbol__account_cycles(&bi[i].from,
2428 nonany_branch_mode ? NULL : prev,
2429 bi[i].flags.cycles);
2430 prev = &bi[i].to;
2431 }
2432 free(bi);
2433 }
2434 }
2435}
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03002436
2437size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp)
2438{
2439 struct perf_evsel *pos;
2440 size_t ret = 0;
2441
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002442 evlist__for_each_entry(evlist, pos) {
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03002443 ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
2444 ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
2445 }
2446
2447 return ret;
2448}
2449
2450
Namhyung Kimf2148332014-01-14 11:52:48 +09002451u64 hists__total_period(struct hists *hists)
2452{
2453 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
2454 hists->stats.total_period;
2455}
Namhyung Kim33db4562014-02-07 12:06:07 +09002456
2457int parse_filter_percentage(const struct option *opt __maybe_unused,
2458 const char *arg, int unset __maybe_unused)
2459{
2460 if (!strcmp(arg, "relative"))
2461 symbol_conf.filter_relative = true;
2462 else if (!strcmp(arg, "absolute"))
2463 symbol_conf.filter_relative = false;
Arnaldo Carvalho de Meloecc4c562017-01-24 13:44:10 -03002464 else {
Colin Ian Kinga596a872017-03-30 10:54:40 +01002465 pr_debug("Invalid percentage: %s\n", arg);
Namhyung Kim33db4562014-02-07 12:06:07 +09002466 return -1;
Arnaldo Carvalho de Meloecc4c562017-01-24 13:44:10 -03002467 }
Namhyung Kim33db4562014-02-07 12:06:07 +09002468
2469 return 0;
2470}
Namhyung Kim0b93da12014-01-14 12:02:15 +09002471
2472int perf_hist_config(const char *var, const char *value)
2473{
2474 if (!strcmp(var, "hist.percentage"))
2475 return parse_filter_percentage(NULL, value, 0);
2476
2477 return 0;
2478}
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002479
Jiri Olsa5b658552016-01-18 10:24:22 +01002480int __hists__init(struct hists *hists, struct perf_hpp_list *hpp_list)
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002481{
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002482 memset(hists, 0, sizeof(*hists));
2483 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
2484 hists->entries_in = &hists->entries_in_array[0];
2485 hists->entries_collapsed = RB_ROOT;
2486 hists->entries = RB_ROOT;
2487 pthread_mutex_init(&hists->lock, NULL);
Kan Liang21394d92015-09-04 10:45:44 -04002488 hists->socket_filter = -1;
Jiri Olsa5b658552016-01-18 10:24:22 +01002489 hists->hpp_list = hpp_list;
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002490 INIT_LIST_HEAD(&hists->hpp_formats);
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002491 return 0;
2492}
2493
Namhyung Kim61fa0e92015-12-10 16:53:20 +09002494static void hists__delete_remaining_entries(struct rb_root *root)
2495{
2496 struct rb_node *node;
2497 struct hist_entry *he;
2498
2499 while (!RB_EMPTY_ROOT(root)) {
2500 node = rb_first(root);
2501 rb_erase(node, root);
2502
2503 he = rb_entry(node, struct hist_entry, rb_node_in);
2504 hist_entry__delete(he);
2505 }
2506}
2507
2508static void hists__delete_all_entries(struct hists *hists)
2509{
2510 hists__delete_entries(hists);
2511 hists__delete_remaining_entries(&hists->entries_in_array[0]);
2512 hists__delete_remaining_entries(&hists->entries_in_array[1]);
2513 hists__delete_remaining_entries(&hists->entries_collapsed);
2514}
2515
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002516static void hists_evsel__exit(struct perf_evsel *evsel)
2517{
2518 struct hists *hists = evsel__hists(evsel);
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002519 struct perf_hpp_fmt *fmt, *pos;
2520 struct perf_hpp_list_node *node, *tmp;
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002521
Namhyung Kim61fa0e92015-12-10 16:53:20 +09002522 hists__delete_all_entries(hists);
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002523
2524 list_for_each_entry_safe(node, tmp, &hists->hpp_formats, list) {
2525 perf_hpp_list__for_each_format_safe(&node->hpp, fmt, pos) {
2526 list_del(&fmt->list);
2527 free(fmt);
2528 }
2529 list_del(&node->list);
2530 free(node);
2531 }
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002532}
2533
Namhyung Kimfc284be2016-01-07 10:14:10 +01002534static int hists_evsel__init(struct perf_evsel *evsel)
2535{
2536 struct hists *hists = evsel__hists(evsel);
2537
Jiri Olsa5b658552016-01-18 10:24:22 +01002538 __hists__init(hists, &perf_hpp_list);
Namhyung Kimfc284be2016-01-07 10:14:10 +01002539 return 0;
2540}
2541
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002542/*
2543 * XXX We probably need a hists_evsel__exit() to free the hist_entries
2544 * stored in the rbtree...
2545 */
2546
2547int hists__init(void)
2548{
2549 int err = perf_evsel__object_config(sizeof(struct hists_evsel),
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002550 hists_evsel__init,
2551 hists_evsel__exit);
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002552 if (err)
2553 fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);
2554
2555 return err;
2556}
Jiri Olsa94b3dc32016-01-18 10:24:13 +01002557
2558void perf_hpp_list__init(struct perf_hpp_list *list)
2559{
2560 INIT_LIST_HEAD(&list->fields);
2561 INIT_LIST_HEAD(&list->sorts);
2562}