blob: 4038b086cb80dc84e8014376afe880a9c7579089 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Arnaldo Carvalho de Melob10ba7f2019-01-29 11:11:04 +01002#include "callchain.h"
Arnaldo Carvalho de Melob4209022019-08-29 15:56:40 -03003#include "debug.h"
Arnaldo Carvalho de Melo4a3cec82019-08-30 11:11:01 -03004#include "dso.h"
Frederic Weisbecker598357e2010-05-21 12:48:39 +02005#include "build-id.h"
John Kacur3d1d07e2009-09-28 15:32:55 +02006#include "hist.h"
Krister Johansen9c68ae92017-01-05 22:23:31 -08007#include "map.h"
Arnaldo Carvalho de Melod3300a32019-08-30 15:09:54 -03008#include "map_symbol.h"
9#include "branch.h"
10#include "mem-events.h"
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -020011#include "session.h"
Hari Bathinid890a982017-03-08 02:12:13 +053012#include "namespaces.h"
Namhyung Kimb629f3e2020-03-25 21:45:32 +090013#include "cgroup.h"
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -020014#include "sort.h"
Arnaldo Carvalho de Melo25c312d2018-04-02 14:24:28 -030015#include "units.h"
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -030016#include "evlist.h"
Namhyung Kim29d720e2013-01-22 18:09:33 +090017#include "evsel.h"
Namhyung Kim69bcb012013-10-30 09:40:34 +090018#include "annotate.h"
Arnaldo Carvalho de Melo632a5ca2017-04-17 16:30:49 -030019#include "srcline.h"
Arnaldo Carvalho de Melodaecf9e2019-01-28 00:03:34 +010020#include "symbol.h"
Arnaldo Carvalho de Meloe7ff8922017-04-19 21:34:35 -030021#include "thread.h"
Jin Yao60414412019-11-07 15:47:14 +080022#include "block-info.h"
Namhyung Kim740b97f2014-12-22 13:44:10 +090023#include "ui/progress.h"
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030024#include <errno.h>
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -020025#include <math.h>
Arnaldo Carvalho de Melo25c312d2018-04-02 14:24:28 -030026#include <inttypes.h>
Arnaldo Carvalho de Melo391e4202017-04-19 18:51:14 -030027#include <sys/param.h>
Arnaldo Carvalho de Melo5c9dbe62019-08-30 11:54:00 -030028#include <linux/rbtree.h>
Arnaldo Carvalho de Melo8520a982019-08-29 16:18:59 -030029#include <linux/string.h>
Andi Kleen37239082019-03-11 07:44:54 -070030#include <linux/time64.h>
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -030031#include <linux/zalloc.h>
John Kacur3d1d07e2009-09-28 15:32:55 +020032
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -020033static bool hists__filter_entry_by_dso(struct hists *hists,
34 struct hist_entry *he);
35static bool hists__filter_entry_by_thread(struct hists *hists,
36 struct hist_entry *he);
Namhyung Kime94d53e2012-03-16 17:50:51 +090037static bool hists__filter_entry_by_symbol(struct hists *hists,
38 struct hist_entry *he);
Kan Liang21394d92015-09-04 10:45:44 -040039static bool hists__filter_entry_by_socket(struct hists *hists,
40 struct hist_entry *he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -020041
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030042u16 hists__col_len(struct hists *hists, enum hist_column col)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030043{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030044 return hists->col_len[col];
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030045}
46
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030047void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030048{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030049 hists->col_len[col] = len;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030050}
51
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030052bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030053{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030054 if (len > hists__col_len(hists, col)) {
55 hists__set_col_len(hists, col, len);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030056 return true;
57 }
58 return false;
59}
60
Namhyung Kim7ccf4f92012-08-20 13:52:05 +090061void hists__reset_col_len(struct hists *hists)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030062{
63 enum hist_column col;
64
65 for (col = 0; col < HISTC_NR_COLS; ++col)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030066 hists__set_col_len(hists, col, 0);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030067}
68
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010069static void hists__set_unres_dso_col_len(struct hists *hists, int dso)
70{
71 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
72
73 if (hists__col_len(hists, dso) < unresolved_col_width &&
74 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
75 !symbol_conf.dso_list)
76 hists__set_col_len(hists, dso, unresolved_col_width);
77}
78
Namhyung Kim7ccf4f92012-08-20 13:52:05 +090079void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030080{
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010081 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
Stephane Eranian98a3b322013-01-24 16:10:35 +010082 int symlen;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030083 u16 len;
84
Jin Yao0bdf1812019-11-07 15:47:13 +080085 if (h->block_info)
86 return;
Namhyung Kimded19d52013-04-01 20:35:19 +090087 /*
88 * +4 accounts for '[x] ' priv level info
89 * +2 accounts for 0x prefix on raw addresses
90 * +3 accounts for ' y ' symtab origin info
91 */
92 if (h->ms.sym) {
93 symlen = h->ms.sym->namelen + 4;
Namhyung Kimbb963e12017-02-17 17:17:38 +090094 if (verbose > 0)
Namhyung Kimded19d52013-04-01 20:35:19 +090095 symlen += BITS_PER_LONG / 4 + 2 + 3;
96 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
97 } else {
Stephane Eranian98a3b322013-01-24 16:10:35 +010098 symlen = unresolved_col_width + 4 + 2;
99 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100100 hists__set_unres_dso_col_len(hists, HISTC_DSO);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100101 }
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300102
103 len = thread__comm_len(h->thread);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300104 if (hists__new_col_len(hists, HISTC_COMM, len))
Jiri Olsa89c7cb22016-06-20 23:58:19 +0200105 hists__set_col_len(hists, HISTC_THREAD, len + 8);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300106
107 if (h->ms.map) {
108 len = dso__name_len(h->ms.map->dso);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300109 hists__new_col_len(hists, HISTC_DSO, len);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300110 }
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100111
Namhyung Kimcb993742012-12-27 18:11:42 +0900112 if (h->parent)
113 hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen);
114
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100115 if (h->branch_info) {
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300116 if (h->branch_info->from.ms.sym) {
117 symlen = (int)h->branch_info->from.ms.sym->namelen + 4;
Namhyung Kimbb963e12017-02-17 17:17:38 +0900118 if (verbose > 0)
Namhyung Kimded19d52013-04-01 20:35:19 +0900119 symlen += BITS_PER_LONG / 4 + 2 + 3;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100120 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
121
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300122 symlen = dso__name_len(h->branch_info->from.ms.map->dso);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100123 hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
124 } else {
125 symlen = unresolved_col_width + 4 + 2;
126 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
127 hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
128 }
129
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300130 if (h->branch_info->to.ms.sym) {
131 symlen = (int)h->branch_info->to.ms.sym->namelen + 4;
Namhyung Kimbb963e12017-02-17 17:17:38 +0900132 if (verbose > 0)
Namhyung Kimded19d52013-04-01 20:35:19 +0900133 symlen += BITS_PER_LONG / 4 + 2 + 3;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100134 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
135
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300136 symlen = dso__name_len(h->branch_info->to.ms.map->dso);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100137 hists__new_col_len(hists, HISTC_DSO_TO, symlen);
138 } else {
139 symlen = unresolved_col_width + 4 + 2;
140 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
141 hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
142 }
Andi Kleen508be0d2016-05-20 13:15:08 -0700143
144 if (h->branch_info->srcline_from)
145 hists__new_col_len(hists, HISTC_SRCLINE_FROM,
146 strlen(h->branch_info->srcline_from));
147 if (h->branch_info->srcline_to)
148 hists__new_col_len(hists, HISTC_SRCLINE_TO,
149 strlen(h->branch_info->srcline_to));
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100150 }
Stephane Eranian98a3b322013-01-24 16:10:35 +0100151
152 if (h->mem_info) {
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300153 if (h->mem_info->daddr.ms.sym) {
154 symlen = (int)h->mem_info->daddr.ms.sym->namelen + 4
Stephane Eranian98a3b322013-01-24 16:10:35 +0100155 + unresolved_col_width + 2;
156 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
157 symlen);
Don Zickus9b32ba72014-06-01 15:38:29 +0200158 hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
159 symlen + 1);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100160 } else {
161 symlen = unresolved_col_width + 4 + 2;
162 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
163 symlen);
Jiri Olsa08059092016-01-20 12:56:33 +0100164 hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
165 symlen);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100166 }
Jiri Olsab34b3bf2015-10-05 20:06:08 +0200167
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300168 if (h->mem_info->iaddr.ms.sym) {
169 symlen = (int)h->mem_info->iaddr.ms.sym->namelen + 4
Jiri Olsab34b3bf2015-10-05 20:06:08 +0200170 + unresolved_col_width + 2;
171 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
172 symlen);
173 } else {
174 symlen = unresolved_col_width + 4 + 2;
175 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
176 symlen);
177 }
178
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300179 if (h->mem_info->daddr.ms.map) {
180 symlen = dso__name_len(h->mem_info->daddr.ms.map->dso);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100181 hists__new_col_len(hists, HISTC_MEM_DADDR_DSO,
182 symlen);
183 } else {
184 symlen = unresolved_col_width + 4 + 2;
185 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
186 }
Kan Liang8780fb22017-08-29 13:11:09 -0400187
188 hists__new_col_len(hists, HISTC_MEM_PHYS_DADDR,
189 unresolved_col_width + 4 + 2);
190
Kan Lianga50d03e2020-12-16 10:57:58 -0800191 hists__new_col_len(hists, HISTC_MEM_DATA_PAGE_SIZE,
192 unresolved_col_width + 4 + 2);
193
Stephane Eranian98a3b322013-01-24 16:10:35 +0100194 } else {
195 symlen = unresolved_col_width + 4 + 2;
196 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen);
Jiri Olsab34b3bf2015-10-05 20:06:08 +0200197 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL, symlen);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100198 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
199 }
200
Namhyung Kimb629f3e2020-03-25 21:45:32 +0900201 hists__new_col_len(hists, HISTC_CGROUP, 6);
Hari Bathinid890a982017-03-08 02:12:13 +0530202 hists__new_col_len(hists, HISTC_CGROUP_ID, 20);
Arnaldo Carvalho de Meloa4978ec2015-09-09 12:14:00 -0300203 hists__new_col_len(hists, HISTC_CPU, 3);
Kan Liang2e7ea3a2015-09-04 10:45:43 -0400204 hists__new_col_len(hists, HISTC_SOCKET, 6);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100205 hists__new_col_len(hists, HISTC_MEM_LOCKED, 6);
206 hists__new_col_len(hists, HISTC_MEM_TLB, 22);
207 hists__new_col_len(hists, HISTC_MEM_SNOOP, 12);
208 hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3);
209 hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12);
210 hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12);
Kan Lianga054c292021-02-02 12:09:07 -0800211 hists__new_col_len(hists, HISTC_MEM_BLOCKED, 10);
Andi Kleen3dab6ac2019-08-23 14:03:38 -0700212 if (symbol_conf.nanosecs)
213 hists__new_col_len(hists, HISTC_TIME, 16);
214 else
215 hists__new_col_len(hists, HISTC_TIME, 12);
Stephane Eranian9fd74f22021-01-05 11:57:51 -0800216 hists__new_col_len(hists, HISTC_CODE_PAGE_SIZE, 6);
Andi Kleen475eeab2013-09-20 07:40:43 -0700217
Jiri Olsaf666ac02016-09-19 15:10:10 +0200218 if (h->srcline) {
219 len = MAX(strlen(h->srcline), strlen(sort_srcline.se_header));
220 hists__new_col_len(hists, HISTC_SRCLINE, len);
221 }
Arnaldo Carvalho de Meloe8e6d372015-08-10 16:53:54 -0300222
Andi Kleen31191a82015-08-07 15:54:24 -0700223 if (h->srcfile)
224 hists__new_col_len(hists, HISTC_SRCFILE, strlen(h->srcfile));
225
Andi Kleen475eeab2013-09-20 07:40:43 -0700226 if (h->transaction)
227 hists__new_col_len(hists, HISTC_TRANSACTION,
228 hist_entry__transaction_len());
Namhyung Kim0c0af782016-02-21 23:22:38 +0900229
230 if (h->trace_output)
231 hists__new_col_len(hists, HISTC_TRACE, strlen(h->trace_output));
Namhyung Kimb629f3e2020-03-25 21:45:32 +0900232
233 if (h->cgroup) {
234 const char *cgrp_name = "unknown";
235 struct cgroup *cgrp = cgroup__find(h->ms.maps->machine->env,
236 h->cgroup);
237 if (cgrp != NULL)
238 cgrp_name = cgrp->name;
239
240 hists__new_col_len(hists, HISTC_CGROUP, strlen(cgrp_name));
241 }
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300242}
243
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900244void hists__output_recalc_col_len(struct hists *hists, int max_rows)
245{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800246 struct rb_node *next = rb_first_cached(&hists->entries);
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900247 struct hist_entry *n;
248 int row = 0;
249
250 hists__reset_col_len(hists);
251
252 while (next && row++ < max_rows) {
253 n = rb_entry(next, struct hist_entry, rb_node);
254 if (!n->filtered)
255 hists__calc_col_len(hists, n);
256 next = rb_next(&n->rb_node);
257 }
258}
259
Namhyung Kimf39056f2014-01-14 14:25:37 +0900260static void he_stat__add_cpumode_period(struct he_stat *he_stat,
261 unsigned int cpumode, u64 period)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800262{
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300263 switch (cpumode) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800264 case PERF_RECORD_MISC_KERNEL:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900265 he_stat->period_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800266 break;
267 case PERF_RECORD_MISC_USER:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900268 he_stat->period_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800269 break;
270 case PERF_RECORD_MISC_GUEST_KERNEL:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900271 he_stat->period_guest_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800272 break;
273 case PERF_RECORD_MISC_GUEST_USER:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900274 he_stat->period_guest_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800275 break;
276 default:
277 break;
278 }
279}
280
Andi Kleen37239082019-03-11 07:44:54 -0700281static long hist_time(unsigned long htime)
282{
283 unsigned long time_quantum = symbol_conf.time_quantum;
284 if (time_quantum)
285 return (htime / time_quantum) * time_quantum;
286 return htime;
287}
288
Andi Kleen05484292013-01-24 16:10:29 +0100289static void he_stat__add_period(struct he_stat *he_stat, u64 period,
290 u64 weight)
Namhyung Kim139c0812012-10-04 21:49:43 +0900291{
Stephane Eranian98a3b322013-01-24 16:10:35 +0100292
Namhyung Kim139c0812012-10-04 21:49:43 +0900293 he_stat->period += period;
Andi Kleen05484292013-01-24 16:10:29 +0100294 he_stat->weight += weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900295 he_stat->nr_events += 1;
296}
297
298static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
299{
300 dest->period += src->period;
301 dest->period_sys += src->period_sys;
302 dest->period_us += src->period_us;
303 dest->period_guest_sys += src->period_guest_sys;
304 dest->period_guest_us += src->period_guest_us;
305 dest->nr_events += src->nr_events;
Andi Kleen05484292013-01-24 16:10:29 +0100306 dest->weight += src->weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900307}
308
Namhyung Kimf39056f2014-01-14 14:25:37 +0900309static void he_stat__decay(struct he_stat *he_stat)
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300310{
Namhyung Kimf39056f2014-01-14 14:25:37 +0900311 he_stat->period = (he_stat->period * 7) / 8;
312 he_stat->nr_events = (he_stat->nr_events * 7) / 8;
Andi Kleen05484292013-01-24 16:10:29 +0100313 /* XXX need decay for weight too? */
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300314}
315
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900316static void hists__delete_entry(struct hists *hists, struct hist_entry *he);
317
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300318static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
319{
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900320 u64 prev_period = he->stat.period;
Namhyung Kim3186b682014-04-22 13:44:23 +0900321 u64 diff;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200322
323 if (prev_period == 0)
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300324 return true;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200325
Namhyung Kimf39056f2014-01-14 14:25:37 +0900326 he_stat__decay(&he->stat);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900327 if (symbol_conf.cumulate_callchain)
328 he_stat__decay(he->stat_acc);
Namhyung Kim42b276a2016-01-05 12:06:00 +0900329 decay_callchain(he->callchain);
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200330
Namhyung Kim3186b682014-04-22 13:44:23 +0900331 diff = prev_period - he->stat.period;
332
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900333 if (!he->depth) {
334 hists->stats.total_period -= diff;
335 if (!he->filtered)
336 hists->stats.total_non_filtered_period -= diff;
337 }
338
339 if (!he->leaf) {
340 struct hist_entry *child;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800341 struct rb_node *node = rb_first_cached(&he->hroot_out);
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900342 while (node) {
343 child = rb_entry(node, struct hist_entry, rb_node);
344 node = rb_next(node);
345
346 if (hists__decay_entry(hists, child))
347 hists__delete_entry(hists, child);
348 }
349 }
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200350
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900351 return he->stat.period == 0;
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300352}
353
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300354static void hists__delete_entry(struct hists *hists, struct hist_entry *he)
355{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800356 struct rb_root_cached *root_in;
357 struct rb_root_cached *root_out;
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300358
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900359 if (he->parent_he) {
360 root_in = &he->parent_he->hroot_in;
361 root_out = &he->parent_he->hroot_out;
362 } else {
Jiri Olsa52225032016-05-03 13:54:42 +0200363 if (hists__has(hists, need_collapse))
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900364 root_in = &hists->entries_collapsed;
365 else
366 root_in = hists->entries_in;
367 root_out = &hists->entries;
368 }
369
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800370 rb_erase_cached(&he->rb_node_in, root_in);
371 rb_erase_cached(&he->rb_node, root_out);
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300372
373 --hists->nr_entries;
374 if (!he->filtered)
375 --hists->nr_non_filtered_entries;
376
377 hist_entry__delete(he);
378}
379
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900380void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300381{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800382 struct rb_node *next = rb_first_cached(&hists->entries);
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300383 struct hist_entry *n;
384
385 while (next) {
386 n = rb_entry(next, struct hist_entry, rb_node);
387 next = rb_next(&n->rb_node);
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200388 if (((zap_user && n->level == '.') ||
389 (zap_kernel && n->level != '.') ||
Arnaldo Carvalho de Melo4c47f4f2015-03-17 17:18:58 -0300390 hists__decay_entry(hists, n))) {
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300391 hists__delete_entry(hists, n);
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300392 }
393 }
394}
395
Namhyung Kim701937b2014-08-12 17:16:05 +0900396void hists__delete_entries(struct hists *hists)
397{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800398 struct rb_node *next = rb_first_cached(&hists->entries);
Namhyung Kim701937b2014-08-12 17:16:05 +0900399 struct hist_entry *n;
400
401 while (next) {
402 n = rb_entry(next, struct hist_entry, rb_node);
403 next = rb_next(&n->rb_node);
404
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300405 hists__delete_entry(hists, n);
Namhyung Kim701937b2014-08-12 17:16:05 +0900406 }
407}
408
Jin Yaob10c78c2019-06-28 17:23:03 +0800409struct hist_entry *hists__get_entry(struct hists *hists, int idx)
410{
411 struct rb_node *next = rb_first_cached(&hists->entries);
412 struct hist_entry *n;
413 int i = 0;
414
415 while (next) {
416 n = rb_entry(next, struct hist_entry, rb_node);
417 if (i == idx)
418 return n;
419
420 next = rb_next(&n->rb_node);
421 i++;
422 }
423
424 return NULL;
425}
426
John Kacur3d1d07e2009-09-28 15:32:55 +0200427/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300428 * histogram, sorted on item, collects periods
John Kacur3d1d07e2009-09-28 15:32:55 +0200429 */
430
Jiri Olsa0a269a62016-07-05 08:56:03 +0200431static int hist_entry__init(struct hist_entry *he,
432 struct hist_entry *template,
Arnaldo Carvalho de Melo41477ac2018-06-07 14:19:54 -0300433 bool sample_self,
434 size_t callchain_size)
Jiri Olsa0a269a62016-07-05 08:56:03 +0200435{
436 *he = *template;
Arnaldo Carvalho de Melo41477ac2018-06-07 14:19:54 -0300437 he->callchain_size = callchain_size;
Jiri Olsa0a269a62016-07-05 08:56:03 +0200438
439 if (symbol_conf.cumulate_callchain) {
440 he->stat_acc = malloc(sizeof(he->stat));
441 if (he->stat_acc == NULL)
442 return -ENOMEM;
443 memcpy(he->stat_acc, &he->stat, sizeof(he->stat));
444 if (!sample_self)
445 memset(&he->stat, 0, sizeof(he->stat));
446 }
447
448 map__get(he->ms.map);
449
450 if (he->branch_info) {
451 /*
452 * This branch info is (a part of) allocated from
453 * sample__resolve_bstack() and will be freed after
454 * adding new entries. So we need to save a copy.
455 */
456 he->branch_info = malloc(sizeof(*he->branch_info));
Jiri Olsac5758912019-03-05 16:25:30 +0100457 if (he->branch_info == NULL)
458 goto err;
Jiri Olsa0a269a62016-07-05 08:56:03 +0200459
460 memcpy(he->branch_info, template->branch_info,
461 sizeof(*he->branch_info));
462
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300463 map__get(he->branch_info->from.ms.map);
464 map__get(he->branch_info->to.ms.map);
Jiri Olsa0a269a62016-07-05 08:56:03 +0200465 }
466
467 if (he->mem_info) {
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300468 map__get(he->mem_info->iaddr.ms.map);
469 map__get(he->mem_info->daddr.ms.map);
Jiri Olsa0a269a62016-07-05 08:56:03 +0200470 }
471
Arnaldo Carvalho de Melofabd37b2018-05-29 13:59:24 -0300472 if (hist_entry__has_callchains(he) && symbol_conf.use_callchain)
Jiri Olsa0a269a62016-07-05 08:56:03 +0200473 callchain_init(he->callchain);
474
475 if (he->raw_data) {
476 he->raw_data = memdup(he->raw_data, he->raw_size);
Jiri Olsac5758912019-03-05 16:25:30 +0100477 if (he->raw_data == NULL)
478 goto err_infos;
Jiri Olsa0a269a62016-07-05 08:56:03 +0200479 }
Jiri Olsa26349582019-03-05 16:25:31 +0100480
481 if (he->srcline) {
482 he->srcline = strdup(he->srcline);
483 if (he->srcline == NULL)
484 goto err_rawdata;
485 }
486
Andi Kleen4968ac82019-03-11 07:44:58 -0700487 if (symbol_conf.res_sample) {
488 he->res_samples = calloc(sizeof(struct res_sample),
489 symbol_conf.res_sample);
490 if (!he->res_samples)
491 goto err_srcline;
492 }
493
Jiri Olsa0a269a62016-07-05 08:56:03 +0200494 INIT_LIST_HEAD(&he->pairs.node);
495 thread__get(he->thread);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800496 he->hroot_in = RB_ROOT_CACHED;
497 he->hroot_out = RB_ROOT_CACHED;
Jiri Olsa0a269a62016-07-05 08:56:03 +0200498
499 if (!symbol_conf.report_hierarchy)
500 he->leaf = true;
501
502 return 0;
Jiri Olsac5758912019-03-05 16:25:30 +0100503
Andi Kleen4968ac82019-03-11 07:44:58 -0700504err_srcline:
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -0300505 zfree(&he->srcline);
Andi Kleen4968ac82019-03-11 07:44:58 -0700506
Jiri Olsa26349582019-03-05 16:25:31 +0100507err_rawdata:
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -0300508 zfree(&he->raw_data);
Jiri Olsa26349582019-03-05 16:25:31 +0100509
Jiri Olsac5758912019-03-05 16:25:30 +0100510err_infos:
511 if (he->branch_info) {
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300512 map__put(he->branch_info->from.ms.map);
513 map__put(he->branch_info->to.ms.map);
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -0300514 zfree(&he->branch_info);
Jiri Olsac5758912019-03-05 16:25:30 +0100515 }
516 if (he->mem_info) {
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300517 map__put(he->mem_info->iaddr.ms.map);
518 map__put(he->mem_info->daddr.ms.map);
Jiri Olsac5758912019-03-05 16:25:30 +0100519 }
520err:
521 map__zput(he->ms.map);
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -0300522 zfree(&he->stat_acc);
Jiri Olsac5758912019-03-05 16:25:30 +0100523 return -ENOMEM;
Jiri Olsa0a269a62016-07-05 08:56:03 +0200524}
525
Jiri Olsaf542e762016-07-05 08:56:04 +0200526static void *hist_entry__zalloc(size_t size)
527{
528 return zalloc(size + sizeof(struct hist_entry));
529}
530
531static void hist_entry__free(void *ptr)
532{
533 free(ptr);
534}
535
536static struct hist_entry_ops default_ops = {
537 .new = hist_entry__zalloc,
538 .free = hist_entry__free,
539};
540
Namhyung Kima0b51af2012-09-11 13:34:27 +0900541static struct hist_entry *hist_entry__new(struct hist_entry *template,
542 bool sample_self)
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300543{
Jiri Olsaf542e762016-07-05 08:56:04 +0200544 struct hist_entry_ops *ops = template->ops;
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900545 size_t callchain_size = 0;
546 struct hist_entry *he;
Jiri Olsa0a269a62016-07-05 08:56:03 +0200547 int err = 0;
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900548
Jiri Olsaf542e762016-07-05 08:56:04 +0200549 if (!ops)
550 ops = template->ops = &default_ops;
551
Namhyung Kim82aa0192014-12-22 13:44:14 +0900552 if (symbol_conf.use_callchain)
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900553 callchain_size = sizeof(struct callchain_root);
554
Jiri Olsaf542e762016-07-05 08:56:04 +0200555 he = ops->new(callchain_size);
Jiri Olsa0a269a62016-07-05 08:56:03 +0200556 if (he) {
Arnaldo Carvalho de Melo41477ac2018-06-07 14:19:54 -0300557 err = hist_entry__init(he, template, sample_self, callchain_size);
Jiri Olsaf542e762016-07-05 08:56:04 +0200558 if (err) {
559 ops->free(he);
560 he = NULL;
561 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300562 }
563
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200564 return he;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300565}
566
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300567static u8 symbol__parent_filter(const struct symbol *parent)
568{
569 if (symbol_conf.exclude_other && parent == NULL)
570 return 1 << HIST_FILTER__PARENT;
571 return 0;
572}
573
Namhyung Kim467ef102016-02-16 23:08:19 +0900574static void hist_entry__add_callchain_period(struct hist_entry *he, u64 period)
575{
Arnaldo Carvalho de Melofabd37b2018-05-29 13:59:24 -0300576 if (!hist_entry__has_callchains(he) || !symbol_conf.use_callchain)
Namhyung Kim467ef102016-02-16 23:08:19 +0900577 return;
578
579 he->hists->callchain_period += period;
580 if (!he->filtered)
581 he->hists->callchain_non_filtered_period += period;
582}
583
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300584static struct hist_entry *hists__findnew_entry(struct hists *hists,
585 struct hist_entry *entry,
586 struct addr_location *al,
587 bool sample_self)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300588{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300589 struct rb_node **p;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300590 struct rb_node *parent = NULL;
591 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -0700592 int64_t cmp;
Namhyung Kimf1cbf782013-12-18 14:21:11 +0900593 u64 period = entry->stat.period;
594 u64 weight = entry->stat.weight;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800595 bool leftmost = true;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300596
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800597 p = &hists->entries_in->rb_root.rb_node;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300598
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300599 while (*p != NULL) {
600 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300601 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300602
Namhyung Kim9afcf932012-12-10 17:29:54 +0900603 /*
604 * Make sure that it receives arguments in a same order as
605 * hist_entry__collapse() so that we can use an appropriate
606 * function when searching an entry regardless which sort
607 * keys were used.
608 */
609 cmp = hist_entry__cmp(he, entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300610
611 if (!cmp) {
Namhyung Kim0f584742016-01-28 00:40:49 +0900612 if (sample_self) {
Namhyung Kima0b51af2012-09-11 13:34:27 +0900613 he_stat__add_period(&he->stat, period, weight);
Namhyung Kim467ef102016-02-16 23:08:19 +0900614 hist_entry__add_callchain_period(he, period);
Namhyung Kim0f584742016-01-28 00:40:49 +0900615 }
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900616 if (symbol_conf.cumulate_callchain)
617 he_stat__add_period(he->stat_acc, period, weight);
David Miller63fa4712012-03-27 03:14:18 -0400618
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900619 /*
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -0300620 * This mem info was allocated from sample__resolve_mem
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900621 * and will not be used anymore.
622 */
Jiri Olsa9f874982018-03-07 16:50:06 +0100623 mem_info__zput(entry->mem_info);
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900624
Jin Yaofe962452019-06-28 17:22:59 +0800625 block_info__zput(entry->block_info);
626
David Miller63fa4712012-03-27 03:14:18 -0400627 /* If the map of an existing hist_entry has
628 * become out-of-date due to an exec() or
629 * similar, update it. Otherwise we will
630 * mis-adjust symbol addresses when computing
631 * the history counter to increment.
632 */
633 if (he->ms.map != entry->ms.map) {
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300634 map__put(he->ms.map);
635 he->ms.map = map__get(entry->ms.map);
David Miller63fa4712012-03-27 03:14:18 -0400636 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300637 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300638 }
639
640 if (cmp < 0)
641 p = &(*p)->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800642 else {
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300643 p = &(*p)->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800644 leftmost = false;
645 }
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300646 }
647
Namhyung Kima0b51af2012-09-11 13:34:27 +0900648 he = hist_entry__new(entry, sample_self);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300649 if (!he)
Namhyung Kim27a0dcb2013-05-14 11:09:02 +0900650 return NULL;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300651
Namhyung Kim0f584742016-01-28 00:40:49 +0900652 if (sample_self)
Namhyung Kim467ef102016-02-16 23:08:19 +0900653 hist_entry__add_callchain_period(he, period);
654 hists->nr_entries++;
Namhyung Kim590cd342014-12-22 13:44:09 +0900655
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300656 rb_link_node(&he->rb_node_in, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800657 rb_insert_color_cached(&he->rb_node_in, hists->entries_in, leftmost);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300658out:
Namhyung Kima0b51af2012-09-11 13:34:27 +0900659 if (sample_self)
660 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900661 if (symbol_conf.cumulate_callchain)
662 he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300663 return he;
664}
665
Andi Kleen4968ac82019-03-11 07:44:58 -0700666static unsigned random_max(unsigned high)
667{
668 unsigned thresh = -high % high;
669 for (;;) {
670 unsigned r = random();
671 if (r >= thresh)
672 return r % high;
673 }
674}
675
676static void hists__res_sample(struct hist_entry *he, struct perf_sample *sample)
677{
678 struct res_sample *r;
679 int j;
680
681 if (he->num_res < symbol_conf.res_sample) {
682 j = he->num_res++;
683 } else {
684 j = random_max(symbol_conf.res_sample);
685 }
686 r = &he->res_samples[j];
687 r->time = sample->time;
688 r->cpu = sample->cpu;
689 r->tid = sample->tid;
690}
691
Jiri Olsaa5051972016-07-05 08:56:05 +0200692static struct hist_entry*
693__hists__add_entry(struct hists *hists,
694 struct addr_location *al,
695 struct symbol *sym_parent,
696 struct branch_info *bi,
697 struct mem_info *mi,
Jin Yaofe962452019-06-28 17:22:59 +0800698 struct block_info *block_info,
Jiri Olsaa5051972016-07-05 08:56:05 +0200699 struct perf_sample *sample,
700 bool sample_self,
701 struct hist_entry_ops *ops)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100702{
Hari Bathinid890a982017-03-08 02:12:13 +0530703 struct namespaces *ns = thread__namespaces(al->thread);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100704 struct hist_entry entry = {
705 .thread = al->thread,
Namhyung Kim4dfced32013-09-13 16:28:57 +0900706 .comm = thread__comm(al->thread),
Hari Bathinid890a982017-03-08 02:12:13 +0530707 .cgroup_id = {
708 .dev = ns ? ns->link_info[CGROUP_NS_INDEX].dev : 0,
709 .ino = ns ? ns->link_info[CGROUP_NS_INDEX].ino : 0,
710 },
Namhyung Kimb629f3e2020-03-25 21:45:32 +0900711 .cgroup = sample->cgroup,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100712 .ms = {
Arnaldo Carvalho de Melof2eaea02019-11-25 22:15:35 -0300713 .maps = al->maps,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100714 .map = al->map,
715 .sym = al->sym,
716 },
Jiri Olsa26349582019-03-05 16:25:31 +0100717 .srcline = (char *) al->srcline,
Kan Liang0c4c4deb2015-09-04 10:45:42 -0400718 .socket = al->socket,
Don Zickus7365be52014-05-27 12:28:05 -0400719 .cpu = al->cpu,
720 .cpumode = al->cpumode,
721 .ip = al->addr,
722 .level = al->level,
Stephane Eranian9fd74f22021-01-05 11:57:51 -0800723 .code_page_size = sample->code_page_size,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900724 .stat = {
Namhyung Kimc4b35352012-10-04 21:49:42 +0900725 .nr_events = 1,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900726 .period = sample->period,
727 .weight = sample->weight,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900728 },
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100729 .parent = sym_parent,
Namhyung Kim2c86c7c2014-03-17 18:18:54 -0300730 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300731 .hists = hists,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900732 .branch_info = bi,
733 .mem_info = mi,
Jin Yaofe962452019-06-28 17:22:59 +0800734 .block_info = block_info,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900735 .transaction = sample->transaction,
Namhyung Kim72392832015-12-24 11:16:17 +0900736 .raw_data = sample->raw_data,
737 .raw_size = sample->raw_size,
Jiri Olsaa5051972016-07-05 08:56:05 +0200738 .ops = ops,
Andi Kleen37239082019-03-11 07:44:54 -0700739 .time = hist_time(sample->time),
Arnaldo Carvalho de Meloc9d36622018-06-07 14:42:27 -0300740 }, *he = hists__findnew_entry(hists, &entry, al, sample_self);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100741
Arnaldo Carvalho de Meloc9d36622018-06-07 14:42:27 -0300742 if (!hists->has_callchains && he && he->callchain_size != 0)
743 hists->has_callchains = true;
Andi Kleen4968ac82019-03-11 07:44:58 -0700744 if (he && symbol_conf.res_sample)
745 hists__res_sample(he, sample);
Arnaldo Carvalho de Meloc9d36622018-06-07 14:42:27 -0300746 return he;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100747}
748
Jiri Olsaa5051972016-07-05 08:56:05 +0200749struct hist_entry *hists__add_entry(struct hists *hists,
750 struct addr_location *al,
751 struct symbol *sym_parent,
752 struct branch_info *bi,
753 struct mem_info *mi,
754 struct perf_sample *sample,
755 bool sample_self)
756{
Jin Yaofe962452019-06-28 17:22:59 +0800757 return __hists__add_entry(hists, al, sym_parent, bi, mi, NULL,
Jiri Olsaa5051972016-07-05 08:56:05 +0200758 sample, sample_self, NULL);
759}
760
761struct hist_entry *hists__add_entry_ops(struct hists *hists,
762 struct hist_entry_ops *ops,
763 struct addr_location *al,
764 struct symbol *sym_parent,
765 struct branch_info *bi,
766 struct mem_info *mi,
767 struct perf_sample *sample,
768 bool sample_self)
769{
Jin Yaofe962452019-06-28 17:22:59 +0800770 return __hists__add_entry(hists, al, sym_parent, bi, mi, NULL,
Jiri Olsaa5051972016-07-05 08:56:05 +0200771 sample, sample_self, ops);
772}
773
Jin Yaofe962452019-06-28 17:22:59 +0800774struct hist_entry *hists__add_entry_block(struct hists *hists,
775 struct addr_location *al,
776 struct block_info *block_info)
777{
778 struct hist_entry entry = {
779 .block_info = block_info,
780 .hists = hists,
Jin Yaob65a7d32019-11-07 15:47:16 +0800781 .ms = {
Arnaldo Carvalho de Melof2eaea02019-11-25 22:15:35 -0300782 .maps = al->maps,
Jin Yaob65a7d32019-11-07 15:47:16 +0800783 .map = al->map,
784 .sym = al->sym,
785 },
Jin Yaofe962452019-06-28 17:22:59 +0800786 }, *he = hists__findnew_entry(hists, &entry, al, false);
787
788 return he;
789}
790
Namhyung Kim69bcb012013-10-30 09:40:34 +0900791static int
792iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
793 struct addr_location *al __maybe_unused)
794{
795 return 0;
796}
797
798static int
799iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
800 struct addr_location *al __maybe_unused)
801{
802 return 0;
803}
804
805static int
806iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
807{
808 struct perf_sample *sample = iter->sample;
809 struct mem_info *mi;
810
811 mi = sample__resolve_mem(sample, al);
812 if (mi == NULL)
813 return -ENOMEM;
814
815 iter->priv = mi;
816 return 0;
817}
818
819static int
820iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
821{
822 u64 cost;
823 struct mem_info *mi = iter->priv;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300824 struct hists *hists = evsel__hists(iter->evsel);
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900825 struct perf_sample *sample = iter->sample;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900826 struct hist_entry *he;
827
828 if (mi == NULL)
829 return -EINVAL;
830
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900831 cost = sample->weight;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900832 if (!cost)
833 cost = 1;
834
835 /*
836 * must pass period=weight in order to get the correct
837 * sorting from hists__collapse_resort() which is solely
838 * based on periods. We want sorting be done on nr_events * weight
839 * and this is indirectly achieved by passing period=weight here
840 * and the he_stat__add_period() function.
841 */
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900842 sample->period = cost;
843
Jiri Olsa0102ef32016-06-14 20:19:21 +0200844 he = hists__add_entry(hists, al, iter->parent, NULL, mi,
845 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900846 if (!he)
847 return -ENOMEM;
848
849 iter->he = he;
850 return 0;
851}
852
853static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900854iter_finish_mem_entry(struct hist_entry_iter *iter,
855 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900856{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200857 struct evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300858 struct hists *hists = evsel__hists(evsel);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900859 struct hist_entry *he = iter->he;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900860 int err = -EINVAL;
861
862 if (he == NULL)
863 goto out;
864
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300865 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900866
867 err = hist_entry__append_callchain(he, iter->sample);
868
869out:
870 /*
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300871 * We don't need to free iter->priv (mem_info) here since the mem info
872 * was either already freed in hists__findnew_entry() or passed to a
873 * new hist entry by hist_entry__new().
Namhyung Kim69bcb012013-10-30 09:40:34 +0900874 */
875 iter->priv = NULL;
876
877 iter->he = NULL;
878 return err;
879}
880
881static int
882iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
883{
884 struct branch_info *bi;
885 struct perf_sample *sample = iter->sample;
886
887 bi = sample__resolve_bstack(sample, al);
888 if (!bi)
889 return -ENOMEM;
890
891 iter->curr = 0;
892 iter->total = sample->branch_stack->nr;
893
894 iter->priv = bi;
895 return 0;
896}
897
898static int
Jin Yao2d78b182017-07-18 20:13:14 +0800899iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused,
Namhyung Kim69bcb012013-10-30 09:40:34 +0900900 struct addr_location *al __maybe_unused)
901{
902 return 0;
903}
904
905static int
906iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
907{
908 struct branch_info *bi = iter->priv;
909 int i = iter->curr;
910
911 if (bi == NULL)
912 return 0;
913
914 if (iter->curr >= iter->total)
915 return 0;
916
Arnaldo Carvalho de Melof2eaea02019-11-25 22:15:35 -0300917 al->maps = bi[i].to.ms.maps;
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300918 al->map = bi[i].to.ms.map;
919 al->sym = bi[i].to.ms.sym;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900920 al->addr = bi[i].to.addr;
921 return 1;
922}
923
924static int
925iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
926{
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900927 struct branch_info *bi;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200928 struct evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300929 struct hists *hists = evsel__hists(evsel);
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900930 struct perf_sample *sample = iter->sample;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900931 struct hist_entry *he = NULL;
932 int i = iter->curr;
933 int err = 0;
934
935 bi = iter->priv;
936
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300937 if (iter->hide_unresolved && !(bi[i].from.ms.sym && bi[i].to.ms.sym))
Namhyung Kim69bcb012013-10-30 09:40:34 +0900938 goto out;
939
940 /*
941 * The report shows the percentage of total branches captured
942 * and not events sampled. Thus we use a pseudo period of 1.
943 */
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900944 sample->period = 1;
945 sample->weight = bi->flags.cycles ? bi->flags.cycles : 1;
946
Jiri Olsa0102ef32016-06-14 20:19:21 +0200947 he = hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
948 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900949 if (he == NULL)
950 return -ENOMEM;
951
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300952 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900953
954out:
955 iter->he = he;
956 iter->curr++;
957 return err;
958}
959
960static int
961iter_finish_branch_entry(struct hist_entry_iter *iter,
962 struct addr_location *al __maybe_unused)
963{
964 zfree(&iter->priv);
965 iter->he = NULL;
966
967 return iter->curr >= iter->total ? 0 : -1;
968}
969
970static int
971iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
972 struct addr_location *al __maybe_unused)
973{
974 return 0;
975}
976
977static int
978iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
979{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200980 struct evsel *evsel = iter->evsel;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900981 struct perf_sample *sample = iter->sample;
982 struct hist_entry *he;
983
Jiri Olsa0102ef32016-06-14 20:19:21 +0200984 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
985 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900986 if (he == NULL)
987 return -ENOMEM;
988
989 iter->he = he;
990 return 0;
991}
992
993static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900994iter_finish_normal_entry(struct hist_entry_iter *iter,
995 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900996{
Namhyung Kim69bcb012013-10-30 09:40:34 +0900997 struct hist_entry *he = iter->he;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200998 struct evsel *evsel = iter->evsel;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900999 struct perf_sample *sample = iter->sample;
1000
1001 if (he == NULL)
1002 return 0;
1003
1004 iter->he = NULL;
1005
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -03001006 hists__inc_nr_samples(evsel__hists(evsel), he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +09001007
1008 return hist_entry__append_callchain(he, sample);
1009}
1010
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001011static int
Adrian Hunter96b40f32015-09-25 16:15:47 +03001012iter_prepare_cumulative_entry(struct hist_entry_iter *iter,
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001013 struct addr_location *al __maybe_unused)
1014{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001015 struct hist_entry **he_cache;
1016
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001017 callchain_cursor_commit(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001018
1019 /*
1020 * This is for detecting cycles or recursions so that they're
1021 * cumulated only one time to prevent entries more than 100%
1022 * overhead.
1023 */
Jiri Olsae3ebaa42018-02-16 13:36:19 +01001024 he_cache = malloc(sizeof(*he_cache) * (callchain_cursor.nr + 1));
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001025 if (he_cache == NULL)
1026 return -ENOMEM;
1027
1028 iter->priv = he_cache;
1029 iter->curr = 0;
1030
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001031 return 0;
1032}
1033
1034static int
1035iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
1036 struct addr_location *al)
1037{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001038 struct evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -03001039 struct hists *hists = evsel__hists(evsel);
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001040 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001041 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001042 struct hist_entry *he;
1043 int err = 0;
1044
Jiri Olsa0102ef32016-06-14 20:19:21 +02001045 he = hists__add_entry(hists, al, iter->parent, NULL, NULL,
1046 sample, true);
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001047 if (he == NULL)
1048 return -ENOMEM;
1049
1050 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001051 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001052
Namhyung Kim82aa0192014-12-22 13:44:14 +09001053 hist_entry__append_callchain(he, sample);
Namhyung Kimbe7f8552013-12-26 17:44:10 +09001054
1055 /*
1056 * We need to re-initialize the cursor since callchain_append()
1057 * advanced the cursor to the end.
1058 */
1059 callchain_cursor_commit(&callchain_cursor);
1060
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -03001061 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001062
1063 return err;
1064}
1065
1066static int
1067iter_next_cumulative_entry(struct hist_entry_iter *iter,
1068 struct addr_location *al)
1069{
1070 struct callchain_cursor_node *node;
1071
1072 node = callchain_cursor_current(&callchain_cursor);
1073 if (node == NULL)
1074 return 0;
1075
Namhyung Kimc7405d82013-10-31 13:58:30 +09001076 return fill_callchain_info(al, node, iter->hide_unresolved);
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001077}
1078
Kan Liang12e89e62020-03-19 13:25:17 -07001079static bool
1080hist_entry__fast__sym_diff(struct hist_entry *left,
1081 struct hist_entry *right)
1082{
1083 struct symbol *sym_l = left->ms.sym;
1084 struct symbol *sym_r = right->ms.sym;
1085
1086 if (!sym_l && !sym_r)
1087 return left->ip != right->ip;
1088
1089 return !!_sort__sym_cmp(sym_l, sym_r);
1090}
1091
1092
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001093static int
1094iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
1095 struct addr_location *al)
1096{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001097 struct evsel *evsel = iter->evsel;
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001098 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001099 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001100 struct hist_entry *he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001101 struct hist_entry he_tmp = {
Arnaldo Carvalho de Melo5cef8972015-08-10 15:45:55 -03001102 .hists = evsel__hists(evsel),
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001103 .cpu = al->cpu,
1104 .thread = al->thread,
1105 .comm = thread__comm(al->thread),
1106 .ip = al->addr,
1107 .ms = {
Arnaldo Carvalho de Melof2eaea02019-11-25 22:15:35 -03001108 .maps = al->maps,
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001109 .map = al->map,
1110 .sym = al->sym,
1111 },
Jiri Olsa26349582019-03-05 16:25:31 +01001112 .srcline = (char *) al->srcline,
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001113 .parent = iter->parent,
Namhyung Kim72392832015-12-24 11:16:17 +09001114 .raw_data = sample->raw_data,
1115 .raw_size = sample->raw_size,
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001116 };
1117 int i;
Namhyung Kimbe7f8552013-12-26 17:44:10 +09001118 struct callchain_cursor cursor;
Kan Liang12e89e62020-03-19 13:25:17 -07001119 bool fast = hists__has(he_tmp.hists, sym);
Namhyung Kimbe7f8552013-12-26 17:44:10 +09001120
1121 callchain_cursor_snapshot(&cursor, &callchain_cursor);
1122
1123 callchain_cursor_advance(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001124
1125 /*
1126 * Check if there's duplicate entries in the callchain.
1127 * It's possible that it has cycles or recursive calls.
1128 */
1129 for (i = 0; i < iter->curr; i++) {
Kan Liang12e89e62020-03-19 13:25:17 -07001130 /*
1131 * For most cases, there are no duplicate entries in callchain.
1132 * The symbols are usually different. Do a quick check for
1133 * symbols first.
1134 */
1135 if (fast && hist_entry__fast__sym_diff(he_cache[i], &he_tmp))
1136 continue;
1137
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001138 if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
1139 /* to avoid calling callback function */
1140 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001141 return 0;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001142 }
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001143 }
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001144
Jiri Olsa0102ef32016-06-14 20:19:21 +02001145 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
1146 sample, false);
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001147 if (he == NULL)
1148 return -ENOMEM;
1149
1150 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001151 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001152
Arnaldo Carvalho de Melofabd37b2018-05-29 13:59:24 -03001153 if (hist_entry__has_callchains(he) && symbol_conf.use_callchain)
Namhyung Kim82aa0192014-12-22 13:44:14 +09001154 callchain_append(he->callchain, &cursor, sample->period);
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001155 return 0;
1156}
1157
1158static int
1159iter_finish_cumulative_entry(struct hist_entry_iter *iter,
1160 struct addr_location *al __maybe_unused)
1161{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001162 zfree(&iter->priv);
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001163 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001164
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001165 return 0;
1166}
1167
Namhyung Kim69bcb012013-10-30 09:40:34 +09001168const struct hist_iter_ops hist_iter_mem = {
1169 .prepare_entry = iter_prepare_mem_entry,
1170 .add_single_entry = iter_add_single_mem_entry,
1171 .next_entry = iter_next_nop_entry,
1172 .add_next_entry = iter_add_next_nop_entry,
1173 .finish_entry = iter_finish_mem_entry,
1174};
1175
1176const struct hist_iter_ops hist_iter_branch = {
1177 .prepare_entry = iter_prepare_branch_entry,
1178 .add_single_entry = iter_add_single_branch_entry,
1179 .next_entry = iter_next_branch_entry,
1180 .add_next_entry = iter_add_next_branch_entry,
1181 .finish_entry = iter_finish_branch_entry,
1182};
1183
1184const struct hist_iter_ops hist_iter_normal = {
1185 .prepare_entry = iter_prepare_normal_entry,
1186 .add_single_entry = iter_add_single_normal_entry,
1187 .next_entry = iter_next_nop_entry,
1188 .add_next_entry = iter_add_next_nop_entry,
1189 .finish_entry = iter_finish_normal_entry,
1190};
1191
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001192const struct hist_iter_ops hist_iter_cumulative = {
1193 .prepare_entry = iter_prepare_cumulative_entry,
1194 .add_single_entry = iter_add_single_cumulative_entry,
1195 .next_entry = iter_next_cumulative_entry,
1196 .add_next_entry = iter_add_next_cumulative_entry,
1197 .finish_entry = iter_finish_cumulative_entry,
1198};
1199
Namhyung Kim69bcb012013-10-30 09:40:34 +09001200int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001201 int max_stack_depth, void *arg)
Namhyung Kim69bcb012013-10-30 09:40:34 +09001202{
1203 int err, err2;
Krister Johansen9c68ae92017-01-05 22:23:31 -08001204 struct map *alm = NULL;
1205
Arnaldo Carvalho de Melo362379a2018-05-24 11:20:39 -03001206 if (al)
Krister Johansen9c68ae92017-01-05 22:23:31 -08001207 alm = map__get(al->map);
Namhyung Kim69bcb012013-10-30 09:40:34 +09001208
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001209 err = sample__resolve_callchain(iter->sample, &callchain_cursor, &iter->parent,
Namhyung Kim063bd932015-05-19 17:04:10 +09001210 iter->evsel, al, max_stack_depth);
Changbin Ducb6186a2019-03-16 16:05:49 +08001211 if (err) {
1212 map__put(alm);
Namhyung Kim69bcb012013-10-30 09:40:34 +09001213 return err;
Changbin Ducb6186a2019-03-16 16:05:49 +08001214 }
Namhyung Kim69bcb012013-10-30 09:40:34 +09001215
Namhyung Kim69bcb012013-10-30 09:40:34 +09001216 err = iter->ops->prepare_entry(iter, al);
1217 if (err)
1218 goto out;
1219
1220 err = iter->ops->add_single_entry(iter, al);
1221 if (err)
1222 goto out;
1223
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001224 if (iter->he && iter->add_entry_cb) {
1225 err = iter->add_entry_cb(iter, al, true, arg);
1226 if (err)
1227 goto out;
1228 }
1229
Namhyung Kim69bcb012013-10-30 09:40:34 +09001230 while (iter->ops->next_entry(iter, al)) {
1231 err = iter->ops->add_next_entry(iter, al);
1232 if (err)
1233 break;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001234
1235 if (iter->he && iter->add_entry_cb) {
1236 err = iter->add_entry_cb(iter, al, false, arg);
1237 if (err)
1238 goto out;
1239 }
Namhyung Kim69bcb012013-10-30 09:40:34 +09001240 }
1241
1242out:
1243 err2 = iter->ops->finish_entry(iter, al);
1244 if (!err)
1245 err = err2;
1246
Krister Johansen9c68ae92017-01-05 22:23:31 -08001247 map__put(alm);
1248
Namhyung Kim69bcb012013-10-30 09:40:34 +09001249 return err;
1250}
1251
John Kacur3d1d07e2009-09-28 15:32:55 +02001252int64_t
1253hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
1254{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001255 struct hists *hists = left->hists;
Namhyung Kim093f0ef32014-03-03 12:07:47 +09001256 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +02001257 int64_t cmp = 0;
1258
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001259 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim84b6ee82016-02-27 03:52:43 +09001260 if (perf_hpp__is_dynamic_entry(fmt) &&
1261 !perf_hpp__defined_dynamic_entry(fmt, hists))
1262 continue;
1263
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001264 cmp = fmt->cmp(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +02001265 if (cmp)
1266 break;
1267 }
1268
1269 return cmp;
1270}
1271
1272int64_t
1273hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
1274{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001275 struct hists *hists = left->hists;
Namhyung Kim093f0ef32014-03-03 12:07:47 +09001276 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +02001277 int64_t cmp = 0;
1278
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001279 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim84b6ee82016-02-27 03:52:43 +09001280 if (perf_hpp__is_dynamic_entry(fmt) &&
1281 !perf_hpp__defined_dynamic_entry(fmt, hists))
1282 continue;
1283
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001284 cmp = fmt->collapse(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +02001285 if (cmp)
1286 break;
1287 }
1288
1289 return cmp;
1290}
1291
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -03001292void hist_entry__delete(struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +02001293{
Jiri Olsaf542e762016-07-05 08:56:04 +02001294 struct hist_entry_ops *ops = he->ops;
1295
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001296 thread__zput(he->thread);
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -03001297 map__zput(he->ms.map);
1298
1299 if (he->branch_info) {
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -03001300 map__zput(he->branch_info->from.ms.map);
1301 map__zput(he->branch_info->to.ms.map);
Andi Kleen508be0d2016-05-20 13:15:08 -07001302 free_srcline(he->branch_info->srcline_from);
1303 free_srcline(he->branch_info->srcline_to);
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -03001304 zfree(&he->branch_info);
1305 }
1306
1307 if (he->mem_info) {
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -03001308 map__zput(he->mem_info->iaddr.ms.map);
1309 map__zput(he->mem_info->daddr.ms.map);
Jiri Olsa9f874982018-03-07 16:50:06 +01001310 mem_info__zput(he->mem_info);
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -03001311 }
1312
Jin Yao99150a12019-06-28 17:23:01 +08001313 if (he->block_info)
1314 block_info__zput(he->block_info);
1315
Andi Kleen4968ac82019-03-11 07:44:58 -07001316 zfree(&he->res_samples);
Namhyung Kimf8be1c82012-09-11 13:15:07 +09001317 zfree(&he->stat_acc);
Namhyung Kimf048d542013-09-11 14:09:28 +09001318 free_srcline(he->srcline);
Andi Kleen31191a82015-08-07 15:54:24 -07001319 if (he->srcfile && he->srcfile[0])
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -03001320 zfree(&he->srcfile);
Namhyung Kimd1149602014-12-30 14:38:13 +09001321 free_callchain(he->callchain);
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -03001322 zfree(&he->trace_output);
1323 zfree(&he->raw_data);
Jiri Olsaf542e762016-07-05 08:56:04 +02001324 ops->free(he);
John Kacur3d1d07e2009-09-28 15:32:55 +02001325}
1326
1327/*
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -03001328 * If this is not the last column, then we need to pad it according to the
Ingo Molnaradba1632018-12-03 11:22:00 +01001329 * pre-calculated max length for this column, otherwise don't bother adding
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -03001330 * spaces because that would break viewing this with, for instance, 'less',
1331 * that would show tons of trailing spaces when a long C++ demangled method
1332 * names is sampled.
1333*/
1334int hist_entry__snprintf_alignment(struct hist_entry *he, struct perf_hpp *hpp,
1335 struct perf_hpp_fmt *fmt, int printed)
1336{
1337 if (!list_is_last(&fmt->list, &he->hists->hpp_list->fields)) {
Jiri Olsada1b0402016-06-14 20:19:20 +02001338 const int width = fmt->width(fmt, hpp, he->hists);
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -03001339 if (printed < width) {
1340 advance_hpp(hpp, printed);
1341 printed = scnprintf(hpp->buf, hpp->size, "%-*s", width - printed, " ");
1342 }
1343 }
1344
1345 return printed;
1346}
1347
1348/*
John Kacur3d1d07e2009-09-28 15:32:55 +02001349 * collapse the histogram
1350 */
1351
Namhyung Kimaef810e2016-02-25 00:13:34 +09001352static void hists__apply_filters(struct hists *hists, struct hist_entry *he);
Namhyung Kimaec13a72016-03-09 22:46:58 +09001353static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *he,
1354 enum hist_filter type);
1355
1356typedef bool (*fmt_chk_fn)(struct perf_hpp_fmt *fmt);
1357
1358static bool check_thread_entry(struct perf_hpp_fmt *fmt)
1359{
1360 return perf_hpp__is_thread_entry(fmt) || perf_hpp__is_comm_entry(fmt);
1361}
1362
1363static void hist_entry__check_and_remove_filter(struct hist_entry *he,
1364 enum hist_filter type,
1365 fmt_chk_fn check)
1366{
1367 struct perf_hpp_fmt *fmt;
1368 bool type_match = false;
1369 struct hist_entry *parent = he->parent_he;
1370
1371 switch (type) {
1372 case HIST_FILTER__THREAD:
1373 if (symbol_conf.comm_list == NULL &&
1374 symbol_conf.pid_list == NULL &&
1375 symbol_conf.tid_list == NULL)
1376 return;
1377 break;
1378 case HIST_FILTER__DSO:
1379 if (symbol_conf.dso_list == NULL)
1380 return;
1381 break;
1382 case HIST_FILTER__SYMBOL:
1383 if (symbol_conf.sym_list == NULL)
1384 return;
1385 break;
1386 case HIST_FILTER__PARENT:
1387 case HIST_FILTER__GUEST:
1388 case HIST_FILTER__HOST:
1389 case HIST_FILTER__SOCKET:
Jiri Olsa9857b712016-08-17 14:55:23 +02001390 case HIST_FILTER__C2C:
Namhyung Kimaec13a72016-03-09 22:46:58 +09001391 default:
1392 return;
1393 }
1394
1395 /* if it's filtered by own fmt, it has to have filter bits */
1396 perf_hpp_list__for_each_format(he->hpp_list, fmt) {
1397 if (check(fmt)) {
1398 type_match = true;
1399 break;
1400 }
1401 }
1402
1403 if (type_match) {
1404 /*
1405 * If the filter is for current level entry, propagate
1406 * filter marker to parents. The marker bit was
1407 * already set by default so it only needs to clear
1408 * non-filtered entries.
1409 */
1410 if (!(he->filtered & (1 << type))) {
1411 while (parent) {
1412 parent->filtered &= ~(1 << type);
1413 parent = parent->parent_he;
1414 }
1415 }
1416 } else {
1417 /*
1418 * If current entry doesn't have matching formats, set
1419 * filter marker for upper level entries. it will be
1420 * cleared if its lower level entries is not filtered.
1421 *
1422 * For lower-level entries, it inherits parent's
1423 * filter bit so that lower level entries of a
1424 * non-filtered entry won't set the filter marker.
1425 */
1426 if (parent == NULL)
1427 he->filtered |= (1 << type);
1428 else
1429 he->filtered |= (parent->filtered & (1 << type));
1430 }
1431}
1432
1433static void hist_entry__apply_hierarchy_filters(struct hist_entry *he)
1434{
1435 hist_entry__check_and_remove_filter(he, HIST_FILTER__THREAD,
1436 check_thread_entry);
1437
1438 hist_entry__check_and_remove_filter(he, HIST_FILTER__DSO,
1439 perf_hpp__is_dso_entry);
1440
1441 hist_entry__check_and_remove_filter(he, HIST_FILTER__SYMBOL,
1442 perf_hpp__is_sym_entry);
1443
1444 hists__apply_filters(he->hists, he);
1445}
Namhyung Kimaef810e2016-02-25 00:13:34 +09001446
1447static struct hist_entry *hierarchy_insert_entry(struct hists *hists,
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001448 struct rb_root_cached *root,
Namhyung Kimaef810e2016-02-25 00:13:34 +09001449 struct hist_entry *he,
Namhyung Kimaec13a72016-03-09 22:46:58 +09001450 struct hist_entry *parent_he,
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001451 struct perf_hpp_list *hpp_list)
Namhyung Kimaef810e2016-02-25 00:13:34 +09001452{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001453 struct rb_node **p = &root->rb_root.rb_node;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001454 struct rb_node *parent = NULL;
1455 struct hist_entry *iter, *new;
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001456 struct perf_hpp_fmt *fmt;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001457 int64_t cmp;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001458 bool leftmost = true;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001459
1460 while (*p != NULL) {
1461 parent = *p;
1462 iter = rb_entry(parent, struct hist_entry, rb_node_in);
1463
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001464 cmp = 0;
1465 perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1466 cmp = fmt->collapse(fmt, iter, he);
1467 if (cmp)
1468 break;
1469 }
1470
Namhyung Kimaef810e2016-02-25 00:13:34 +09001471 if (!cmp) {
1472 he_stat__add_stat(&iter->stat, &he->stat);
1473 return iter;
1474 }
1475
1476 if (cmp < 0)
1477 p = &parent->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001478 else {
Namhyung Kimaef810e2016-02-25 00:13:34 +09001479 p = &parent->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001480 leftmost = false;
1481 }
Namhyung Kimaef810e2016-02-25 00:13:34 +09001482 }
1483
1484 new = hist_entry__new(he, true);
1485 if (new == NULL)
1486 return NULL;
1487
Namhyung Kimaef810e2016-02-25 00:13:34 +09001488 hists->nr_entries++;
1489
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001490 /* save related format list for output */
1491 new->hpp_list = hpp_list;
Namhyung Kimaec13a72016-03-09 22:46:58 +09001492 new->parent_he = parent_he;
1493
1494 hist_entry__apply_hierarchy_filters(new);
Namhyung Kimaef810e2016-02-25 00:13:34 +09001495
1496 /* some fields are now passed to 'new' */
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001497 perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1498 if (perf_hpp__is_trace_entry(fmt) || perf_hpp__is_dynamic_entry(fmt))
1499 he->trace_output = NULL;
1500 else
1501 new->trace_output = NULL;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001502
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001503 if (perf_hpp__is_srcline_entry(fmt))
1504 he->srcline = NULL;
1505 else
1506 new->srcline = NULL;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001507
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001508 if (perf_hpp__is_srcfile_entry(fmt))
1509 he->srcfile = NULL;
1510 else
1511 new->srcfile = NULL;
1512 }
Namhyung Kimaef810e2016-02-25 00:13:34 +09001513
1514 rb_link_node(&new->rb_node_in, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001515 rb_insert_color_cached(&new->rb_node_in, root, leftmost);
Namhyung Kimaef810e2016-02-25 00:13:34 +09001516 return new;
1517}
1518
1519static int hists__hierarchy_insert_entry(struct hists *hists,
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001520 struct rb_root_cached *root,
Namhyung Kimaef810e2016-02-25 00:13:34 +09001521 struct hist_entry *he)
1522{
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001523 struct perf_hpp_list_node *node;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001524 struct hist_entry *new_he = NULL;
1525 struct hist_entry *parent = NULL;
1526 int depth = 0;
1527 int ret = 0;
1528
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001529 list_for_each_entry(node, &hists->hpp_formats, list) {
1530 /* skip period (overhead) and elided columns */
1531 if (node->level == 0 || node->skip)
Namhyung Kimaef810e2016-02-25 00:13:34 +09001532 continue;
1533
1534 /* insert copy of 'he' for each fmt into the hierarchy */
Namhyung Kimaec13a72016-03-09 22:46:58 +09001535 new_he = hierarchy_insert_entry(hists, root, he, parent, &node->hpp);
Namhyung Kimaef810e2016-02-25 00:13:34 +09001536 if (new_he == NULL) {
1537 ret = -1;
1538 break;
1539 }
1540
1541 root = &new_he->hroot_in;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001542 new_he->depth = depth++;
1543 parent = new_he;
1544 }
1545
1546 if (new_he) {
1547 new_he->leaf = true;
1548
Arnaldo Carvalho de Melofabd37b2018-05-29 13:59:24 -03001549 if (hist_entry__has_callchains(new_he) &&
1550 symbol_conf.use_callchain) {
Namhyung Kimaef810e2016-02-25 00:13:34 +09001551 callchain_cursor_reset(&callchain_cursor);
1552 if (callchain_merge(&callchain_cursor,
1553 new_he->callchain,
1554 he->callchain) < 0)
1555 ret = -1;
1556 }
1557 }
1558
1559 /* 'he' is no longer used */
1560 hist_entry__delete(he);
1561
1562 /* return 0 (or -1) since it already applied filters */
1563 return ret;
1564}
1565
Jiri Olsa592dac62016-03-24 13:52:17 +01001566static int hists__collapse_insert_entry(struct hists *hists,
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001567 struct rb_root_cached *root,
Jiri Olsa592dac62016-03-24 13:52:17 +01001568 struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +02001569{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001570 struct rb_node **p = &root->rb_root.rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +02001571 struct rb_node *parent = NULL;
1572 struct hist_entry *iter;
1573 int64_t cmp;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001574 bool leftmost = true;
John Kacur3d1d07e2009-09-28 15:32:55 +02001575
Namhyung Kimaef810e2016-02-25 00:13:34 +09001576 if (symbol_conf.report_hierarchy)
1577 return hists__hierarchy_insert_entry(hists, root, he);
1578
John Kacur3d1d07e2009-09-28 15:32:55 +02001579 while (*p != NULL) {
1580 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001581 iter = rb_entry(parent, struct hist_entry, rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +02001582
1583 cmp = hist_entry__collapse(iter, he);
1584
1585 if (!cmp) {
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001586 int ret = 0;
1587
Namhyung Kim139c0812012-10-04 21:49:43 +09001588 he_stat__add_stat(&iter->stat, &he->stat);
Namhyung Kimf8be1c82012-09-11 13:15:07 +09001589 if (symbol_conf.cumulate_callchain)
1590 he_stat__add_stat(iter->stat_acc, he->stat_acc);
Namhyung Kim9ec60972012-09-26 16:47:28 +09001591
Arnaldo Carvalho de Melofabd37b2018-05-29 13:59:24 -03001592 if (hist_entry__has_callchains(he) && symbol_conf.use_callchain) {
Namhyung Kim47260642012-05-31 14:43:26 +09001593 callchain_cursor_reset(&callchain_cursor);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001594 if (callchain_merge(&callchain_cursor,
1595 iter->callchain,
1596 he->callchain) < 0)
1597 ret = -1;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +01001598 }
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -03001599 hist_entry__delete(he);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001600 return ret;
John Kacur3d1d07e2009-09-28 15:32:55 +02001601 }
1602
1603 if (cmp < 0)
1604 p = &(*p)->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001605 else {
John Kacur3d1d07e2009-09-28 15:32:55 +02001606 p = &(*p)->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001607 leftmost = false;
1608 }
John Kacur3d1d07e2009-09-28 15:32:55 +02001609 }
Namhyung Kim740b97f2014-12-22 13:44:10 +09001610 hists->nr_entries++;
John Kacur3d1d07e2009-09-28 15:32:55 +02001611
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001612 rb_link_node(&he->rb_node_in, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001613 rb_insert_color_cached(&he->rb_node_in, root, leftmost);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001614 return 1;
John Kacur3d1d07e2009-09-28 15:32:55 +02001615}
1616
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001617struct rb_root_cached *hists__get_rotate_entries_in(struct hists *hists)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001618{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001619 struct rb_root_cached *root;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001620
1621 pthread_mutex_lock(&hists->lock);
1622
1623 root = hists->entries_in;
1624 if (++hists->entries_in > &hists->entries_in_array[1])
1625 hists->entries_in = &hists->entries_in_array[0];
1626
1627 pthread_mutex_unlock(&hists->lock);
1628
1629 return root;
1630}
1631
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001632static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
1633{
1634 hists__filter_entry_by_dso(hists, he);
1635 hists__filter_entry_by_thread(hists, he);
Namhyung Kime94d53e2012-03-16 17:50:51 +09001636 hists__filter_entry_by_symbol(hists, he);
Kan Liang21394d92015-09-04 10:45:44 -04001637 hists__filter_entry_by_socket(hists, he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001638}
1639
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001640int hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001641{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001642 struct rb_root_cached *root;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001643 struct rb_node *next;
1644 struct hist_entry *n;
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001645 int ret;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001646
Jiri Olsa52225032016-05-03 13:54:42 +02001647 if (!hists__has(hists, need_collapse))
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001648 return 0;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001649
Namhyung Kim740b97f2014-12-22 13:44:10 +09001650 hists->nr_entries = 0;
1651
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001652 root = hists__get_rotate_entries_in(hists);
Namhyung Kim740b97f2014-12-22 13:44:10 +09001653
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001654 next = rb_first_cached(root);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001655
1656 while (next) {
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001657 if (session_done())
1658 break;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001659 n = rb_entry(next, struct hist_entry, rb_node_in);
1660 next = rb_next(&n->rb_node_in);
1661
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001662 rb_erase_cached(&n->rb_node_in, root);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001663 ret = hists__collapse_insert_entry(hists, &hists->entries_collapsed, n);
1664 if (ret < 0)
1665 return -1;
1666
1667 if (ret) {
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001668 /*
1669 * If it wasn't combined with one of the entries already
1670 * collapsed, we need to apply the filters that may have
1671 * been set by, say, the hist_browser.
1672 */
1673 hists__apply_filters(hists, n);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001674 }
Namhyung Kimc1fb5652013-10-11 14:15:38 +09001675 if (prog)
1676 ui_progress__update(prog, 1);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001677 }
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001678 return 0;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001679}
1680
Jiri Olsa722ddfd2019-11-05 00:27:11 +01001681static int64_t hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001682{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001683 struct hists *hists = a->hists;
Namhyung Kim043ca3892014-03-03 14:18:00 +09001684 struct perf_hpp_fmt *fmt;
1685 int64_t cmp = 0;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001686
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001687 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim361459f2015-12-23 02:07:08 +09001688 if (perf_hpp__should_skip(fmt, a->hists))
Namhyung Kime67d49a2014-03-18 13:00:59 +09001689 continue;
1690
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001691 cmp = fmt->sort(fmt, a, b);
Namhyung Kim043ca3892014-03-03 14:18:00 +09001692 if (cmp)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001693 break;
1694 }
1695
Namhyung Kim043ca3892014-03-03 14:18:00 +09001696 return cmp;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001697}
1698
Namhyung Kim9283ba92014-04-24 16:37:26 +09001699static void hists__reset_filter_stats(struct hists *hists)
1700{
1701 hists->nr_non_filtered_entries = 0;
1702 hists->stats.total_non_filtered_period = 0;
1703}
1704
1705void hists__reset_stats(struct hists *hists)
1706{
1707 hists->nr_entries = 0;
1708 hists->stats.total_period = 0;
1709
1710 hists__reset_filter_stats(hists);
1711}
1712
1713static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
1714{
1715 hists->nr_non_filtered_entries++;
1716 hists->stats.total_non_filtered_period += h->stat.period;
1717}
1718
1719void hists__inc_stats(struct hists *hists, struct hist_entry *h)
1720{
1721 if (!h->filtered)
1722 hists__inc_filter_stats(hists, h);
1723
1724 hists->nr_entries++;
1725 hists->stats.total_period += h->stat.period;
1726}
1727
Namhyung Kimf7fb5382016-03-09 22:47:02 +09001728static void hierarchy_recalc_total_periods(struct hists *hists)
1729{
1730 struct rb_node *node;
1731 struct hist_entry *he;
1732
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001733 node = rb_first_cached(&hists->entries);
Namhyung Kimf7fb5382016-03-09 22:47:02 +09001734
1735 hists->stats.total_period = 0;
1736 hists->stats.total_non_filtered_period = 0;
1737
1738 /*
1739 * recalculate total period using top-level entries only
1740 * since lower level entries only see non-filtered entries
1741 * but upper level entries have sum of both entries.
1742 */
1743 while (node) {
1744 he = rb_entry(node, struct hist_entry, rb_node);
1745 node = rb_next(node);
1746
1747 hists->stats.total_period += he->stat.period;
1748 if (!he->filtered)
1749 hists->stats.total_non_filtered_period += he->stat.period;
1750 }
1751}
1752
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001753static void hierarchy_insert_output_entry(struct rb_root_cached *root,
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001754 struct hist_entry *he)
1755{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001756 struct rb_node **p = &root->rb_root.rb_node;
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001757 struct rb_node *parent = NULL;
1758 struct hist_entry *iter;
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001759 struct perf_hpp_fmt *fmt;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001760 bool leftmost = true;
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001761
1762 while (*p != NULL) {
1763 parent = *p;
1764 iter = rb_entry(parent, struct hist_entry, rb_node);
1765
1766 if (hist_entry__sort(he, iter) > 0)
1767 p = &parent->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001768 else {
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001769 p = &parent->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001770 leftmost = false;
1771 }
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001772 }
1773
1774 rb_link_node(&he->rb_node, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001775 rb_insert_color_cached(&he->rb_node, root, leftmost);
Namhyung Kimabab5e72016-02-27 03:52:47 +09001776
1777 /* update column width of dynamic entry */
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001778 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
1779 if (perf_hpp__is_dynamic_entry(fmt))
1780 fmt->sort(fmt, he, NULL);
1781 }
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001782}
1783
1784static void hists__hierarchy_output_resort(struct hists *hists,
1785 struct ui_progress *prog,
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001786 struct rb_root_cached *root_in,
1787 struct rb_root_cached *root_out,
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001788 u64 min_callchain_hits,
1789 bool use_callchain)
1790{
1791 struct rb_node *node;
1792 struct hist_entry *he;
1793
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001794 *root_out = RB_ROOT_CACHED;
1795 node = rb_first_cached(root_in);
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001796
1797 while (node) {
1798 he = rb_entry(node, struct hist_entry, rb_node_in);
1799 node = rb_next(node);
1800
1801 hierarchy_insert_output_entry(root_out, he);
1802
1803 if (prog)
1804 ui_progress__update(prog, 1);
1805
Namhyung Kimc72ab442016-11-08 22:08:33 +09001806 hists->nr_entries++;
1807 if (!he->filtered) {
1808 hists->nr_non_filtered_entries++;
1809 hists__calc_col_len(hists, he);
1810 }
1811
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001812 if (!he->leaf) {
1813 hists__hierarchy_output_resort(hists, prog,
1814 &he->hroot_in,
1815 &he->hroot_out,
1816 min_callchain_hits,
1817 use_callchain);
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001818 continue;
1819 }
1820
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001821 if (!use_callchain)
1822 continue;
1823
1824 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1825 u64 total = he->stat.period;
1826
1827 if (symbol_conf.cumulate_callchain)
1828 total = he->stat_acc->period;
1829
1830 min_callchain_hits = total * (callchain_param.min_percent / 100);
1831 }
1832
1833 callchain_param.sort(&he->sorted_chain, he->callchain,
1834 min_callchain_hits, &callchain_param);
1835 }
1836}
1837
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001838static void __hists__insert_output_entry(struct rb_root_cached *entries,
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001839 struct hist_entry *he,
Kan Liangf9db0d02015-08-11 06:30:48 -04001840 u64 min_callchain_hits,
1841 bool use_callchain)
John Kacur3d1d07e2009-09-28 15:32:55 +02001842{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001843 struct rb_node **p = &entries->rb_root.rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +02001844 struct rb_node *parent = NULL;
1845 struct hist_entry *iter;
Namhyung Kimabab5e72016-02-27 03:52:47 +09001846 struct perf_hpp_fmt *fmt;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001847 bool leftmost = true;
John Kacur3d1d07e2009-09-28 15:32:55 +02001848
Namhyung Kim744070e2016-01-28 00:40:48 +09001849 if (use_callchain) {
1850 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1851 u64 total = he->stat.period;
1852
1853 if (symbol_conf.cumulate_callchain)
1854 total = he->stat_acc->period;
1855
1856 min_callchain_hits = total * (callchain_param.min_percent / 100);
1857 }
Arnaldo Carvalho de Melob9fb9302010-04-02 09:50:42 -03001858 callchain_param.sort(&he->sorted_chain, he->callchain,
John Kacur3d1d07e2009-09-28 15:32:55 +02001859 min_callchain_hits, &callchain_param);
Namhyung Kim744070e2016-01-28 00:40:48 +09001860 }
John Kacur3d1d07e2009-09-28 15:32:55 +02001861
1862 while (*p != NULL) {
1863 parent = *p;
1864 iter = rb_entry(parent, struct hist_entry, rb_node);
1865
Namhyung Kim043ca3892014-03-03 14:18:00 +09001866 if (hist_entry__sort(he, iter) > 0)
John Kacur3d1d07e2009-09-28 15:32:55 +02001867 p = &(*p)->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001868 else {
John Kacur3d1d07e2009-09-28 15:32:55 +02001869 p = &(*p)->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001870 leftmost = false;
1871 }
John Kacur3d1d07e2009-09-28 15:32:55 +02001872 }
1873
1874 rb_link_node(&he->rb_node, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001875 rb_insert_color_cached(&he->rb_node, entries, leftmost);
Namhyung Kimabab5e72016-02-27 03:52:47 +09001876
1877 perf_hpp_list__for_each_sort_list(&perf_hpp_list, fmt) {
1878 if (perf_hpp__is_dynamic_entry(fmt) &&
1879 perf_hpp__defined_dynamic_entry(fmt, he->hists))
1880 fmt->sort(fmt, he, NULL); /* update column width */
1881 }
John Kacur3d1d07e2009-09-28 15:32:55 +02001882}
1883
Jiri Olsa01441af2016-01-18 10:23:59 +01001884static void output_resort(struct hists *hists, struct ui_progress *prog,
Jiri Olsae4c38fd2019-02-04 15:18:06 +01001885 bool use_callchain, hists__resort_cb_t cb,
1886 void *cb_arg)
John Kacur3d1d07e2009-09-28 15:32:55 +02001887{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001888 struct rb_root_cached *root;
John Kacur3d1d07e2009-09-28 15:32:55 +02001889 struct rb_node *next;
1890 struct hist_entry *n;
Namhyung Kim467ef102016-02-16 23:08:19 +09001891 u64 callchain_total;
John Kacur3d1d07e2009-09-28 15:32:55 +02001892 u64 min_callchain_hits;
1893
Namhyung Kim467ef102016-02-16 23:08:19 +09001894 callchain_total = hists->callchain_period;
1895 if (symbol_conf.filter_relative)
1896 callchain_total = hists->callchain_non_filtered_period;
1897
1898 min_callchain_hits = callchain_total * (callchain_param.min_percent / 100);
John Kacur3d1d07e2009-09-28 15:32:55 +02001899
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001900 hists__reset_stats(hists);
1901 hists__reset_col_len(hists);
1902
1903 if (symbol_conf.report_hierarchy) {
Namhyung Kimf7fb5382016-03-09 22:47:02 +09001904 hists__hierarchy_output_resort(hists, prog,
1905 &hists->entries_collapsed,
1906 &hists->entries,
1907 min_callchain_hits,
1908 use_callchain);
1909 hierarchy_recalc_total_periods(hists);
1910 return;
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001911 }
1912
Jiri Olsa52225032016-05-03 13:54:42 +02001913 if (hists__has(hists, need_collapse))
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001914 root = &hists->entries_collapsed;
1915 else
1916 root = hists->entries_in;
1917
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001918 next = rb_first_cached(root);
1919 hists->entries = RB_ROOT_CACHED;
John Kacur3d1d07e2009-09-28 15:32:55 +02001920
1921 while (next) {
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001922 n = rb_entry(next, struct hist_entry, rb_node_in);
1923 next = rb_next(&n->rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +02001924
Jiri Olsae4c38fd2019-02-04 15:18:06 +01001925 if (cb && cb(n, cb_arg))
Jiri Olsa52c5cc32016-08-01 20:02:34 +02001926 continue;
1927
Kan Liangf9db0d02015-08-11 06:30:48 -04001928 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits, use_callchain);
Namhyung Kim62638352014-04-24 16:21:46 +09001929 hists__inc_stats(hists, n);
Namhyung Kimae993ef2014-04-24 16:25:19 +09001930
1931 if (!n->filtered)
1932 hists__calc_col_len(hists, n);
Namhyung Kim740b97f2014-12-22 13:44:10 +09001933
1934 if (prog)
1935 ui_progress__update(prog, 1);
John Kacur3d1d07e2009-09-28 15:32:55 +02001936 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001937}
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -02001938
Arnaldo Carvalho de Melo10c513f2020-05-06 12:58:55 -03001939void evsel__output_resort_cb(struct evsel *evsel, struct ui_progress *prog,
1940 hists__resort_cb_t cb, void *cb_arg)
Jiri Olsa01441af2016-01-18 10:23:59 +01001941{
Jiri Olsa01441af2016-01-18 10:23:59 +01001942 bool use_callchain;
1943
1944 if (evsel && symbol_conf.use_callchain && !symbol_conf.show_ref_callgraph)
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -03001945 use_callchain = evsel__has_callchain(evsel);
Jiri Olsa01441af2016-01-18 10:23:59 +01001946 else
1947 use_callchain = symbol_conf.use_callchain;
1948
Jin Yaob49a8212017-05-08 18:43:02 +08001949 use_callchain |= symbol_conf.show_branchflag_count;
1950
Jiri Olsa57496182019-02-04 15:18:07 +01001951 output_resort(evsel__hists(evsel), prog, use_callchain, cb, cb_arg);
1952}
1953
Arnaldo Carvalho de Melo10c513f2020-05-06 12:58:55 -03001954void evsel__output_resort(struct evsel *evsel, struct ui_progress *prog)
Jiri Olsa57496182019-02-04 15:18:07 +01001955{
Arnaldo Carvalho de Melo10c513f2020-05-06 12:58:55 -03001956 return evsel__output_resort_cb(evsel, prog, NULL, NULL);
Jiri Olsa452ce032016-01-18 10:24:00 +01001957}
1958
1959void hists__output_resort(struct hists *hists, struct ui_progress *prog)
1960{
Jiri Olsae4c38fd2019-02-04 15:18:06 +01001961 output_resort(hists, prog, symbol_conf.use_callchain, NULL, NULL);
Jiri Olsa52c5cc32016-08-01 20:02:34 +02001962}
1963
1964void hists__output_resort_cb(struct hists *hists, struct ui_progress *prog,
1965 hists__resort_cb_t cb)
1966{
Jiri Olsae4c38fd2019-02-04 15:18:06 +01001967 output_resort(hists, prog, symbol_conf.use_callchain, cb, NULL);
Jiri Olsa01441af2016-01-18 10:23:59 +01001968}
1969
Namhyung Kim8c018722016-02-25 00:13:36 +09001970static bool can_goto_child(struct hist_entry *he, enum hierarchy_move_dir hmd)
1971{
1972 if (he->leaf || hmd == HMD_FORCE_SIBLING)
1973 return false;
1974
1975 if (he->unfolded || hmd == HMD_FORCE_CHILD)
1976 return true;
1977
1978 return false;
1979}
1980
1981struct rb_node *rb_hierarchy_last(struct rb_node *node)
1982{
1983 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1984
1985 while (can_goto_child(he, HMD_NORMAL)) {
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001986 node = rb_last(&he->hroot_out.rb_root);
Namhyung Kim8c018722016-02-25 00:13:36 +09001987 he = rb_entry(node, struct hist_entry, rb_node);
1988 }
1989 return node;
1990}
1991
1992struct rb_node *__rb_hierarchy_next(struct rb_node *node, enum hierarchy_move_dir hmd)
1993{
1994 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1995
1996 if (can_goto_child(he, hmd))
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001997 node = rb_first_cached(&he->hroot_out);
Namhyung Kim8c018722016-02-25 00:13:36 +09001998 else
1999 node = rb_next(node);
2000
2001 while (node == NULL) {
2002 he = he->parent_he;
2003 if (he == NULL)
2004 break;
2005
2006 node = rb_next(&he->rb_node);
2007 }
2008 return node;
2009}
2010
2011struct rb_node *rb_hierarchy_prev(struct rb_node *node)
2012{
2013 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
2014
2015 node = rb_prev(node);
2016 if (node)
2017 return rb_hierarchy_last(node);
2018
2019 he = he->parent_he;
2020 if (he == NULL)
2021 return NULL;
2022
2023 return &he->rb_node;
2024}
2025
Namhyung Kima7b58952016-02-26 21:13:16 +09002026bool hist_entry__has_hierarchy_children(struct hist_entry *he, float limit)
2027{
2028 struct rb_node *node;
2029 struct hist_entry *child;
2030 float percent;
2031
2032 if (he->leaf)
2033 return false;
2034
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002035 node = rb_first_cached(&he->hroot_out);
Namhyung Kima7b58952016-02-26 21:13:16 +09002036 child = rb_entry(node, struct hist_entry, rb_node);
2037
2038 while (node && child->filtered) {
2039 node = rb_next(node);
2040 child = rb_entry(node, struct hist_entry, rb_node);
2041 }
2042
2043 if (node)
2044 percent = hist_entry__get_percent_limit(child);
2045 else
2046 percent = 0;
2047
2048 return node && percent >= limit;
2049}
2050
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03002051static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03002052 enum hist_filter filter)
2053{
2054 h->filtered &= ~(1 << filter);
Namhyung Kim155e9af2016-02-25 00:13:38 +09002055
2056 if (symbol_conf.report_hierarchy) {
2057 struct hist_entry *parent = h->parent_he;
2058
2059 while (parent) {
2060 he_stat__add_stat(&parent->stat, &h->stat);
2061
2062 parent->filtered &= ~(1 << filter);
2063
2064 if (parent->filtered)
2065 goto next;
2066
2067 /* force fold unfiltered entry for simplicity */
2068 parent->unfolded = false;
Namhyung Kim79dded82016-02-26 21:13:19 +09002069 parent->has_no_entry = false;
Namhyung Kim155e9af2016-02-25 00:13:38 +09002070 parent->row_offset = 0;
2071 parent->nr_rows = 0;
2072next:
2073 parent = parent->parent_he;
2074 }
2075 }
2076
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03002077 if (h->filtered)
2078 return;
2079
Namhyung Kim87e90f42014-04-24 16:44:16 +09002080 /* force fold unfiltered entry for simplicity */
Namhyung Kim3698dab2015-05-05 23:55:46 +09002081 h->unfolded = false;
Namhyung Kim79dded82016-02-26 21:13:19 +09002082 h->has_no_entry = false;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -03002083 h->row_offset = 0;
He Kuanga8cd1f42015-03-11 20:36:03 +08002084 h->nr_rows = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09002085
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09002086 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03002087
Namhyung Kim9283ba92014-04-24 16:37:26 +09002088 hists__inc_filter_stats(hists, h);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03002089 hists__calc_col_len(hists, h);
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03002090}
2091
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02002092
2093static bool hists__filter_entry_by_dso(struct hists *hists,
2094 struct hist_entry *he)
2095{
2096 if (hists->dso_filter != NULL &&
2097 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
2098 he->filtered |= (1 << HIST_FILTER__DSO);
2099 return true;
2100 }
2101
2102 return false;
2103}
2104
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02002105static bool hists__filter_entry_by_thread(struct hists *hists,
2106 struct hist_entry *he)
2107{
2108 if (hists->thread_filter != NULL &&
2109 he->thread != hists->thread_filter) {
2110 he->filtered |= (1 << HIST_FILTER__THREAD);
2111 return true;
2112 }
2113
2114 return false;
2115}
2116
Namhyung Kime94d53e2012-03-16 17:50:51 +09002117static bool hists__filter_entry_by_symbol(struct hists *hists,
2118 struct hist_entry *he)
2119{
2120 if (hists->symbol_filter_str != NULL &&
2121 (!he->ms.sym || strstr(he->ms.sym->name,
2122 hists->symbol_filter_str) == NULL)) {
2123 he->filtered |= (1 << HIST_FILTER__SYMBOL);
2124 return true;
2125 }
2126
2127 return false;
2128}
2129
Kan Liang21394d92015-09-04 10:45:44 -04002130static bool hists__filter_entry_by_socket(struct hists *hists,
2131 struct hist_entry *he)
2132{
2133 if ((hists->socket_filter > -1) &&
2134 (he->socket != hists->socket_filter)) {
2135 he->filtered |= (1 << HIST_FILTER__SOCKET);
2136 return true;
2137 }
2138
2139 return false;
2140}
2141
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002142typedef bool (*filter_fn_t)(struct hists *hists, struct hist_entry *he);
2143
2144static void hists__filter_by_type(struct hists *hists, int type, filter_fn_t filter)
Kan Liang84734b02015-09-04 10:45:45 -04002145{
2146 struct rb_node *nd;
2147
2148 hists->stats.nr_non_filtered_samples = 0;
2149
2150 hists__reset_filter_stats(hists);
2151 hists__reset_col_len(hists);
2152
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002153 for (nd = rb_first_cached(&hists->entries); nd; nd = rb_next(nd)) {
Kan Liang84734b02015-09-04 10:45:45 -04002154 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2155
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002156 if (filter(hists, h))
Kan Liang84734b02015-09-04 10:45:45 -04002157 continue;
2158
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002159 hists__remove_entry_filter(hists, h, type);
Kan Liang84734b02015-09-04 10:45:45 -04002160 }
2161}
2162
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002163static void resort_filtered_entry(struct rb_root_cached *root,
2164 struct hist_entry *he)
Namhyung Kim70642852016-02-25 00:13:39 +09002165{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002166 struct rb_node **p = &root->rb_root.rb_node;
Namhyung Kim70642852016-02-25 00:13:39 +09002167 struct rb_node *parent = NULL;
2168 struct hist_entry *iter;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002169 struct rb_root_cached new_root = RB_ROOT_CACHED;
Namhyung Kim70642852016-02-25 00:13:39 +09002170 struct rb_node *nd;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002171 bool leftmost = true;
Namhyung Kim70642852016-02-25 00:13:39 +09002172
2173 while (*p != NULL) {
2174 parent = *p;
2175 iter = rb_entry(parent, struct hist_entry, rb_node);
2176
2177 if (hist_entry__sort(he, iter) > 0)
2178 p = &(*p)->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002179 else {
Namhyung Kim70642852016-02-25 00:13:39 +09002180 p = &(*p)->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002181 leftmost = false;
2182 }
Namhyung Kim70642852016-02-25 00:13:39 +09002183 }
2184
2185 rb_link_node(&he->rb_node, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002186 rb_insert_color_cached(&he->rb_node, root, leftmost);
Namhyung Kim70642852016-02-25 00:13:39 +09002187
2188 if (he->leaf || he->filtered)
2189 return;
2190
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002191 nd = rb_first_cached(&he->hroot_out);
Namhyung Kim70642852016-02-25 00:13:39 +09002192 while (nd) {
2193 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2194
2195 nd = rb_next(nd);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002196 rb_erase_cached(&h->rb_node, &he->hroot_out);
Namhyung Kim70642852016-02-25 00:13:39 +09002197
2198 resort_filtered_entry(&new_root, h);
2199 }
2200
2201 he->hroot_out = new_root;
2202}
2203
Namhyung Kim155e9af2016-02-25 00:13:38 +09002204static void hists__filter_hierarchy(struct hists *hists, int type, const void *arg)
2205{
2206 struct rb_node *nd;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002207 struct rb_root_cached new_root = RB_ROOT_CACHED;
Namhyung Kim155e9af2016-02-25 00:13:38 +09002208
2209 hists->stats.nr_non_filtered_samples = 0;
2210
2211 hists__reset_filter_stats(hists);
2212 hists__reset_col_len(hists);
2213
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002214 nd = rb_first_cached(&hists->entries);
Namhyung Kim155e9af2016-02-25 00:13:38 +09002215 while (nd) {
2216 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2217 int ret;
2218
2219 ret = hist_entry__filter(h, type, arg);
2220
2221 /*
2222 * case 1. non-matching type
2223 * zero out the period, set filter marker and move to child
2224 */
2225 if (ret < 0) {
2226 memset(&h->stat, 0, sizeof(h->stat));
2227 h->filtered |= (1 << type);
2228
2229 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_CHILD);
2230 }
2231 /*
2232 * case 2. matched type (filter out)
2233 * set filter marker and move to next
2234 */
2235 else if (ret == 1) {
2236 h->filtered |= (1 << type);
2237
2238 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
2239 }
2240 /*
2241 * case 3. ok (not filtered)
2242 * add period to hists and parents, erase the filter marker
2243 * and move to next sibling
2244 */
2245 else {
2246 hists__remove_entry_filter(hists, h, type);
2247
2248 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
2249 }
2250 }
Namhyung Kim70642852016-02-25 00:13:39 +09002251
Namhyung Kimf7fb5382016-03-09 22:47:02 +09002252 hierarchy_recalc_total_periods(hists);
2253
Namhyung Kim70642852016-02-25 00:13:39 +09002254 /*
2255 * resort output after applying a new filter since filter in a lower
2256 * hierarchy can change periods in a upper hierarchy.
2257 */
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002258 nd = rb_first_cached(&hists->entries);
Namhyung Kim70642852016-02-25 00:13:39 +09002259 while (nd) {
2260 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2261
2262 nd = rb_next(nd);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002263 rb_erase_cached(&h->rb_node, &hists->entries);
Namhyung Kim70642852016-02-25 00:13:39 +09002264
2265 resort_filtered_entry(&new_root, h);
2266 }
2267
2268 hists->entries = new_root;
Namhyung Kim155e9af2016-02-25 00:13:38 +09002269}
2270
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002271void hists__filter_by_thread(struct hists *hists)
2272{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002273 if (symbol_conf.report_hierarchy)
2274 hists__filter_hierarchy(hists, HIST_FILTER__THREAD,
2275 hists->thread_filter);
2276 else
2277 hists__filter_by_type(hists, HIST_FILTER__THREAD,
2278 hists__filter_entry_by_thread);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002279}
2280
2281void hists__filter_by_dso(struct hists *hists)
2282{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002283 if (symbol_conf.report_hierarchy)
2284 hists__filter_hierarchy(hists, HIST_FILTER__DSO,
2285 hists->dso_filter);
2286 else
2287 hists__filter_by_type(hists, HIST_FILTER__DSO,
2288 hists__filter_entry_by_dso);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002289}
2290
2291void hists__filter_by_symbol(struct hists *hists)
2292{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002293 if (symbol_conf.report_hierarchy)
2294 hists__filter_hierarchy(hists, HIST_FILTER__SYMBOL,
2295 hists->symbol_filter_str);
2296 else
2297 hists__filter_by_type(hists, HIST_FILTER__SYMBOL,
2298 hists__filter_entry_by_symbol);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002299}
2300
2301void hists__filter_by_socket(struct hists *hists)
2302{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002303 if (symbol_conf.report_hierarchy)
2304 hists__filter_hierarchy(hists, HIST_FILTER__SOCKET,
2305 &hists->socket_filter);
2306 else
2307 hists__filter_by_type(hists, HIST_FILTER__SOCKET,
2308 hists__filter_entry_by_socket);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002309}
2310
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03002311void events_stats__inc(struct events_stats *stats, u32 type)
2312{
2313 ++stats->nr_events[0];
2314 ++stats->nr_events[type];
2315}
2316
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03002317void hists__inc_nr_events(struct hists *hists, u32 type)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03002318{
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03002319 events_stats__inc(&hists->stats, type);
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03002320}
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002321
Namhyung Kim1844dbc2014-05-28 14:12:18 +09002322void hists__inc_nr_samples(struct hists *hists, bool filtered)
2323{
2324 events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
2325 if (!filtered)
2326 hists->stats.nr_non_filtered_samples++;
2327}
2328
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002329static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
2330 struct hist_entry *pair)
2331{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002332 struct rb_root_cached *root;
Namhyung Kimce74f602012-12-10 17:29:55 +09002333 struct rb_node **p;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002334 struct rb_node *parent = NULL;
2335 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -07002336 int64_t cmp;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002337 bool leftmost = true;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002338
Jiri Olsa52225032016-05-03 13:54:42 +02002339 if (hists__has(hists, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002340 root = &hists->entries_collapsed;
2341 else
2342 root = hists->entries_in;
2343
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002344 p = &root->rb_root.rb_node;
Namhyung Kimce74f602012-12-10 17:29:55 +09002345
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002346 while (*p != NULL) {
2347 parent = *p;
Namhyung Kimce74f602012-12-10 17:29:55 +09002348 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002349
Namhyung Kimce74f602012-12-10 17:29:55 +09002350 cmp = hist_entry__collapse(he, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002351
2352 if (!cmp)
2353 goto out;
2354
2355 if (cmp < 0)
2356 p = &(*p)->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002357 else {
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002358 p = &(*p)->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002359 leftmost = false;
2360 }
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002361 }
2362
Namhyung Kima0b51af2012-09-11 13:34:27 +09002363 he = hist_entry__new(pair, true);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002364 if (he) {
Arnaldo Carvalho de Melo30193d72012-11-12 13:20:03 -03002365 memset(&he->stat, 0, sizeof(he->stat));
2366 he->hists = hists;
Kan Liang09623d72016-04-24 23:28:09 -07002367 if (symbol_conf.cumulate_callchain)
2368 memset(he->stat_acc, 0, sizeof(he->stat));
Namhyung Kimce74f602012-12-10 17:29:55 +09002369 rb_link_node(&he->rb_node_in, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002370 rb_insert_color_cached(&he->rb_node_in, root, leftmost);
Namhyung Kim62638352014-04-24 16:21:46 +09002371 hists__inc_stats(hists, he);
Jiri Olsae0af43d2012-12-01 21:18:20 +01002372 he->dummy = true;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002373 }
2374out:
2375 return he;
2376}
2377
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002378static struct hist_entry *add_dummy_hierarchy_entry(struct hists *hists,
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002379 struct rb_root_cached *root,
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002380 struct hist_entry *pair)
2381{
2382 struct rb_node **p;
2383 struct rb_node *parent = NULL;
2384 struct hist_entry *he;
2385 struct perf_hpp_fmt *fmt;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002386 bool leftmost = true;
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002387
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002388 p = &root->rb_root.rb_node;
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002389 while (*p != NULL) {
2390 int64_t cmp = 0;
2391
2392 parent = *p;
2393 he = rb_entry(parent, struct hist_entry, rb_node_in);
2394
2395 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
2396 cmp = fmt->collapse(fmt, he, pair);
2397 if (cmp)
2398 break;
2399 }
2400 if (!cmp)
2401 goto out;
2402
2403 if (cmp < 0)
2404 p = &parent->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002405 else {
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002406 p = &parent->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002407 leftmost = false;
2408 }
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002409 }
2410
2411 he = hist_entry__new(pair, true);
2412 if (he) {
2413 rb_link_node(&he->rb_node_in, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002414 rb_insert_color_cached(&he->rb_node_in, root, leftmost);
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002415
2416 he->dummy = true;
2417 he->hists = hists;
2418 memset(&he->stat, 0, sizeof(he->stat));
2419 hists__inc_stats(hists, he);
2420 }
2421out:
2422 return he;
2423}
2424
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002425static struct hist_entry *hists__find_entry(struct hists *hists,
2426 struct hist_entry *he)
2427{
Namhyung Kimce74f602012-12-10 17:29:55 +09002428 struct rb_node *n;
2429
Jiri Olsa52225032016-05-03 13:54:42 +02002430 if (hists__has(hists, need_collapse))
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002431 n = hists->entries_collapsed.rb_root.rb_node;
Namhyung Kimce74f602012-12-10 17:29:55 +09002432 else
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002433 n = hists->entries_in->rb_root.rb_node;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002434
2435 while (n) {
Namhyung Kimce74f602012-12-10 17:29:55 +09002436 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
2437 int64_t cmp = hist_entry__collapse(iter, he);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002438
2439 if (cmp < 0)
2440 n = n->rb_left;
2441 else if (cmp > 0)
2442 n = n->rb_right;
2443 else
2444 return iter;
2445 }
2446
2447 return NULL;
2448}
2449
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002450static struct hist_entry *hists__find_hierarchy_entry(struct rb_root_cached *root,
Namhyung Kim09034de2016-09-13 16:45:46 +09002451 struct hist_entry *he)
2452{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002453 struct rb_node *n = root->rb_root.rb_node;
Namhyung Kim09034de2016-09-13 16:45:46 +09002454
2455 while (n) {
2456 struct hist_entry *iter;
2457 struct perf_hpp_fmt *fmt;
2458 int64_t cmp = 0;
2459
2460 iter = rb_entry(n, struct hist_entry, rb_node_in);
2461 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
2462 cmp = fmt->collapse(fmt, iter, he);
2463 if (cmp)
2464 break;
2465 }
2466
2467 if (cmp < 0)
2468 n = n->rb_left;
2469 else if (cmp > 0)
2470 n = n->rb_right;
2471 else
2472 return iter;
2473 }
2474
2475 return NULL;
2476}
2477
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002478static void hists__match_hierarchy(struct rb_root_cached *leader_root,
2479 struct rb_root_cached *other_root)
Namhyung Kim09034de2016-09-13 16:45:46 +09002480{
2481 struct rb_node *nd;
2482 struct hist_entry *pos, *pair;
2483
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002484 for (nd = rb_first_cached(leader_root); nd; nd = rb_next(nd)) {
Namhyung Kim09034de2016-09-13 16:45:46 +09002485 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2486 pair = hists__find_hierarchy_entry(other_root, pos);
2487
2488 if (pair) {
2489 hist_entry__add_pair(pair, pos);
2490 hists__match_hierarchy(&pos->hroot_in, &pair->hroot_in);
2491 }
2492 }
2493}
2494
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002495/*
2496 * Look for pairs to link to the leader buckets (hist_entries):
2497 */
2498void hists__match(struct hists *leader, struct hists *other)
2499{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002500 struct rb_root_cached *root;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002501 struct rb_node *nd;
Namhyung Kimbe5863b2019-08-28 08:15:55 +09002502 struct hist_entry *pos, *pair;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002503
Namhyung Kim09034de2016-09-13 16:45:46 +09002504 if (symbol_conf.report_hierarchy) {
2505 /* hierarchy report always collapses entries */
2506 return hists__match_hierarchy(&leader->entries_collapsed,
2507 &other->entries_collapsed);
2508 }
2509
Jiri Olsa52225032016-05-03 13:54:42 +02002510 if (hists__has(leader, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002511 root = &leader->entries_collapsed;
2512 else
2513 root = leader->entries_in;
2514
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002515 for (nd = rb_first_cached(root); nd; nd = rb_next(nd)) {
Namhyung Kimce74f602012-12-10 17:29:55 +09002516 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002517 pair = hists__find_entry(other, pos);
2518
Namhyung Kimbe5863b2019-08-28 08:15:55 +09002519 if (pair)
Namhyung Kim5fa90412012-11-29 15:38:34 +09002520 hist_entry__add_pair(pair, pos);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002521 }
2522}
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002523
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002524static int hists__link_hierarchy(struct hists *leader_hists,
2525 struct hist_entry *parent,
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002526 struct rb_root_cached *leader_root,
2527 struct rb_root_cached *other_root)
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002528{
2529 struct rb_node *nd;
2530 struct hist_entry *pos, *leader;
2531
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002532 for (nd = rb_first_cached(other_root); nd; nd = rb_next(nd)) {
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002533 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2534
2535 if (hist_entry__has_pairs(pos)) {
2536 bool found = false;
2537
2538 list_for_each_entry(leader, &pos->pairs.head, pairs.node) {
2539 if (leader->hists == leader_hists) {
2540 found = true;
2541 break;
2542 }
2543 }
2544 if (!found)
2545 return -1;
2546 } else {
2547 leader = add_dummy_hierarchy_entry(leader_hists,
2548 leader_root, pos);
2549 if (leader == NULL)
2550 return -1;
2551
2552 /* do not point parent in the pos */
2553 leader->parent_he = parent;
2554
2555 hist_entry__add_pair(pos, leader);
2556 }
2557
2558 if (!pos->leaf) {
2559 if (hists__link_hierarchy(leader_hists, leader,
2560 &leader->hroot_in,
2561 &pos->hroot_in) < 0)
2562 return -1;
2563 }
2564 }
2565 return 0;
2566}
2567
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002568/*
2569 * Look for entries in the other hists that are not present in the leader, if
2570 * we find them, just add a dummy entry on the leader hists, with period=0,
2571 * nr_events=0, to serve as the list header.
2572 */
2573int hists__link(struct hists *leader, struct hists *other)
2574{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002575 struct rb_root_cached *root;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002576 struct rb_node *nd;
2577 struct hist_entry *pos, *pair;
2578
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002579 if (symbol_conf.report_hierarchy) {
2580 /* hierarchy report always collapses entries */
2581 return hists__link_hierarchy(leader, NULL,
2582 &leader->entries_collapsed,
2583 &other->entries_collapsed);
2584 }
2585
Jiri Olsa52225032016-05-03 13:54:42 +02002586 if (hists__has(other, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002587 root = &other->entries_collapsed;
2588 else
2589 root = other->entries_in;
2590
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002591 for (nd = rb_first_cached(root); nd; nd = rb_next(nd)) {
Namhyung Kimce74f602012-12-10 17:29:55 +09002592 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002593
2594 if (!hist_entry__has_pairs(pos)) {
2595 pair = hists__add_dummy_entry(leader, pos);
2596 if (pair == NULL)
2597 return -1;
Namhyung Kim5fa90412012-11-29 15:38:34 +09002598 hist_entry__add_pair(pos, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002599 }
2600 }
2601
2602 return 0;
2603}
Namhyung Kimf2148332014-01-14 11:52:48 +09002604
Namhyung Kimbe5863b2019-08-28 08:15:55 +09002605int hists__unlink(struct hists *hists)
2606{
2607 struct rb_root_cached *root;
2608 struct rb_node *nd;
2609 struct hist_entry *pos;
2610
2611 if (hists__has(hists, need_collapse))
2612 root = &hists->entries_collapsed;
2613 else
2614 root = hists->entries_in;
2615
2616 for (nd = rb_first_cached(root); nd; nd = rb_next(nd)) {
2617 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2618 list_del_init(&pos->pairs.node);
2619 }
2620
2621 return 0;
2622}
2623
Andi Kleen578499982015-07-18 08:24:49 -07002624void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
Jin Yao7841f402019-11-07 15:47:15 +08002625 struct perf_sample *sample, bool nonany_branch_mode,
2626 u64 *total_cycles)
Andi Kleen578499982015-07-18 08:24:49 -07002627{
2628 struct branch_info *bi;
Kan Liang42bbabe2020-02-28 08:30:00 -08002629 struct branch_entry *entries = perf_sample__branch_entries(sample);
Andi Kleen578499982015-07-18 08:24:49 -07002630
2631 /* If we have branch cycles always annotate them. */
Kan Liang42bbabe2020-02-28 08:30:00 -08002632 if (bs && bs->nr && entries[0].flags.cycles) {
Andi Kleen578499982015-07-18 08:24:49 -07002633 int i;
2634
2635 bi = sample__resolve_bstack(sample, al);
2636 if (bi) {
2637 struct addr_map_symbol *prev = NULL;
2638
2639 /*
2640 * Ignore errors, still want to process the
2641 * other entries.
2642 *
2643 * For non standard branch modes always
2644 * force no IPC (prev == NULL)
2645 *
2646 * Note that perf stores branches reversed from
2647 * program order!
2648 */
2649 for (i = bs->nr - 1; i >= 0; i--) {
2650 addr_map_symbol__account_cycles(&bi[i].from,
2651 nonany_branch_mode ? NULL : prev,
2652 bi[i].flags.cycles);
2653 prev = &bi[i].to;
Jin Yao7841f402019-11-07 15:47:15 +08002654
2655 if (total_cycles)
2656 *total_cycles += bi[i].flags.cycles;
Andi Kleen578499982015-07-18 08:24:49 -07002657 }
2658 free(bi);
2659 }
2660 }
2661}
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03002662
Arnaldo Carvalho de Melo71273722020-11-30 14:55:12 -03002663size_t evlist__fprintf_nr_events(struct evlist *evlist, FILE *fp)
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03002664{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002665 struct evsel *pos;
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03002666 size_t ret = 0;
2667
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002668 evlist__for_each_entry(evlist, pos) {
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -03002669 ret += fprintf(fp, "%s stats:\n", evsel__name(pos));
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03002670 ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
2671 }
2672
2673 return ret;
2674}
2675
2676
Namhyung Kimf2148332014-01-14 11:52:48 +09002677u64 hists__total_period(struct hists *hists)
2678{
2679 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
2680 hists->stats.total_period;
2681}
Namhyung Kim33db4562014-02-07 12:06:07 +09002682
Arnaldo Carvalho de Melo25c312d2018-04-02 14:24:28 -03002683int __hists__scnprintf_title(struct hists *hists, char *bf, size_t size, bool show_freq)
2684{
2685 char unit;
2686 int printed;
2687 const struct dso *dso = hists->dso_filter;
Namhyung Kim7cb10a082019-05-27 15:11:49 +09002688 struct thread *thread = hists->thread_filter;
Arnaldo Carvalho de Melo25c312d2018-04-02 14:24:28 -03002689 int socket_id = hists->socket_filter;
2690 unsigned long nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE];
2691 u64 nr_events = hists->stats.total_period;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002692 struct evsel *evsel = hists_to_evsel(hists);
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -03002693 const char *ev_name = evsel__name(evsel);
Arnaldo Carvalho de Melo25c312d2018-04-02 14:24:28 -03002694 char buf[512], sample_freq_str[64] = "";
2695 size_t buflen = sizeof(buf);
2696 char ref[30] = " show reference callgraph, ";
2697 bool enable_ref = false;
2698
2699 if (symbol_conf.filter_relative) {
2700 nr_samples = hists->stats.nr_non_filtered_samples;
2701 nr_events = hists->stats.total_non_filtered_period;
2702 }
2703
Arnaldo Carvalho de Meloc754c382020-04-30 10:51:16 -03002704 if (evsel__is_group_event(evsel)) {
Jiri Olsa32dcd022019-07-21 13:23:51 +02002705 struct evsel *pos;
Arnaldo Carvalho de Melo25c312d2018-04-02 14:24:28 -03002706
Arnaldo Carvalho de Melo347c7512020-04-29 16:09:12 -03002707 evsel__group_desc(evsel, buf, buflen);
Arnaldo Carvalho de Melo25c312d2018-04-02 14:24:28 -03002708 ev_name = buf;
2709
2710 for_each_group_member(pos, evsel) {
2711 struct hists *pos_hists = evsel__hists(pos);
2712
2713 if (symbol_conf.filter_relative) {
2714 nr_samples += pos_hists->stats.nr_non_filtered_samples;
2715 nr_events += pos_hists->stats.total_non_filtered_period;
2716 } else {
2717 nr_samples += pos_hists->stats.nr_events[PERF_RECORD_SAMPLE];
2718 nr_events += pos_hists->stats.total_period;
2719 }
2720 }
2721 }
2722
2723 if (symbol_conf.show_ref_callgraph &&
2724 strstr(ev_name, "call-graph=no"))
2725 enable_ref = true;
2726
2727 if (show_freq)
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002728 scnprintf(sample_freq_str, sizeof(sample_freq_str), " %d Hz,", evsel->core.attr.sample_freq);
Arnaldo Carvalho de Melo25c312d2018-04-02 14:24:28 -03002729
2730 nr_samples = convert_unit(nr_samples, &unit);
2731 printed = scnprintf(bf, size,
2732 "Samples: %lu%c of event%s '%s',%s%sEvent count (approx.): %" PRIu64,
Jiri Olsa5643b1a2019-07-21 13:24:46 +02002733 nr_samples, unit, evsel->core.nr_members > 1 ? "s" : "",
Arnaldo Carvalho de Melo25c312d2018-04-02 14:24:28 -03002734 ev_name, sample_freq_str, enable_ref ? ref : " ", nr_events);
2735
2736
2737 if (hists->uid_filter_str)
2738 printed += snprintf(bf + printed, size - printed,
2739 ", UID: %s", hists->uid_filter_str);
2740 if (thread) {
2741 if (hists__has(hists, thread)) {
2742 printed += scnprintf(bf + printed, size - printed,
2743 ", Thread: %s(%d)",
2744 (thread->comm_set ? thread__comm_str(thread) : ""),
2745 thread->tid);
2746 } else {
2747 printed += scnprintf(bf + printed, size - printed,
2748 ", Thread: %s",
2749 (thread->comm_set ? thread__comm_str(thread) : ""));
2750 }
2751 }
2752 if (dso)
2753 printed += scnprintf(bf + printed, size - printed,
2754 ", DSO: %s", dso->short_name);
2755 if (socket_id > -1)
2756 printed += scnprintf(bf + printed, size - printed,
2757 ", Processor Socket: %d", socket_id);
2758
2759 return printed;
2760}
2761
Namhyung Kim33db4562014-02-07 12:06:07 +09002762int parse_filter_percentage(const struct option *opt __maybe_unused,
2763 const char *arg, int unset __maybe_unused)
2764{
2765 if (!strcmp(arg, "relative"))
2766 symbol_conf.filter_relative = true;
2767 else if (!strcmp(arg, "absolute"))
2768 symbol_conf.filter_relative = false;
Arnaldo Carvalho de Meloecc4c562017-01-24 13:44:10 -03002769 else {
Colin Ian Kinga596a872017-03-30 10:54:40 +01002770 pr_debug("Invalid percentage: %s\n", arg);
Namhyung Kim33db4562014-02-07 12:06:07 +09002771 return -1;
Arnaldo Carvalho de Meloecc4c562017-01-24 13:44:10 -03002772 }
Namhyung Kim33db4562014-02-07 12:06:07 +09002773
2774 return 0;
2775}
Namhyung Kim0b93da12014-01-14 12:02:15 +09002776
2777int perf_hist_config(const char *var, const char *value)
2778{
2779 if (!strcmp(var, "hist.percentage"))
2780 return parse_filter_percentage(NULL, value, 0);
2781
2782 return 0;
2783}
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002784
Jiri Olsa5b658552016-01-18 10:24:22 +01002785int __hists__init(struct hists *hists, struct perf_hpp_list *hpp_list)
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002786{
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002787 memset(hists, 0, sizeof(*hists));
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002788 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT_CACHED;
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002789 hists->entries_in = &hists->entries_in_array[0];
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002790 hists->entries_collapsed = RB_ROOT_CACHED;
2791 hists->entries = RB_ROOT_CACHED;
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002792 pthread_mutex_init(&hists->lock, NULL);
Kan Liang21394d92015-09-04 10:45:44 -04002793 hists->socket_filter = -1;
Jiri Olsa5b658552016-01-18 10:24:22 +01002794 hists->hpp_list = hpp_list;
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002795 INIT_LIST_HEAD(&hists->hpp_formats);
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002796 return 0;
2797}
2798
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002799static void hists__delete_remaining_entries(struct rb_root_cached *root)
Namhyung Kim61fa0e92015-12-10 16:53:20 +09002800{
2801 struct rb_node *node;
2802 struct hist_entry *he;
2803
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002804 while (!RB_EMPTY_ROOT(&root->rb_root)) {
2805 node = rb_first_cached(root);
2806 rb_erase_cached(node, root);
Namhyung Kim61fa0e92015-12-10 16:53:20 +09002807
2808 he = rb_entry(node, struct hist_entry, rb_node_in);
2809 hist_entry__delete(he);
2810 }
2811}
2812
2813static void hists__delete_all_entries(struct hists *hists)
2814{
2815 hists__delete_entries(hists);
2816 hists__delete_remaining_entries(&hists->entries_in_array[0]);
2817 hists__delete_remaining_entries(&hists->entries_in_array[1]);
2818 hists__delete_remaining_entries(&hists->entries_collapsed);
2819}
2820
Jiri Olsa32dcd022019-07-21 13:23:51 +02002821static void hists_evsel__exit(struct evsel *evsel)
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002822{
2823 struct hists *hists = evsel__hists(evsel);
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002824 struct perf_hpp_fmt *fmt, *pos;
2825 struct perf_hpp_list_node *node, *tmp;
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002826
Namhyung Kim61fa0e92015-12-10 16:53:20 +09002827 hists__delete_all_entries(hists);
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002828
2829 list_for_each_entry_safe(node, tmp, &hists->hpp_formats, list) {
2830 perf_hpp_list__for_each_format_safe(&node->hpp, fmt, pos) {
Arnaldo Carvalho de Meloe56fbc92019-07-04 12:13:46 -03002831 list_del_init(&fmt->list);
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002832 free(fmt);
2833 }
Arnaldo Carvalho de Meloe56fbc92019-07-04 12:13:46 -03002834 list_del_init(&node->list);
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002835 free(node);
2836 }
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002837}
2838
Jiri Olsa32dcd022019-07-21 13:23:51 +02002839static int hists_evsel__init(struct evsel *evsel)
Namhyung Kimfc284be2016-01-07 10:14:10 +01002840{
2841 struct hists *hists = evsel__hists(evsel);
2842
Jiri Olsa5b658552016-01-18 10:24:22 +01002843 __hists__init(hists, &perf_hpp_list);
Namhyung Kimfc284be2016-01-07 10:14:10 +01002844 return 0;
2845}
2846
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002847/*
2848 * XXX We probably need a hists_evsel__exit() to free the hist_entries
2849 * stored in the rbtree...
2850 */
2851
2852int hists__init(void)
2853{
Arnaldo Carvalho de Melo4c703822020-05-06 12:55:06 -03002854 int err = evsel__object_config(sizeof(struct hists_evsel),
2855 hists_evsel__init, hists_evsel__exit);
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002856 if (err)
2857 fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);
2858
2859 return err;
2860}
Jiri Olsa94b3dc32016-01-18 10:24:13 +01002861
2862void perf_hpp_list__init(struct perf_hpp_list *list)
2863{
2864 INIT_LIST_HEAD(&list->fields);
2865 INIT_LIST_HEAD(&list->sorts);
2866}