xref: /OK3568_Linux_fs/kernel/tools/perf/util/stat-shadow.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun #include <stdio.h>
3*4882a593Smuzhiyun #include "evsel.h"
4*4882a593Smuzhiyun #include "stat.h"
5*4882a593Smuzhiyun #include "color.h"
6*4882a593Smuzhiyun #include "pmu.h"
7*4882a593Smuzhiyun #include "rblist.h"
8*4882a593Smuzhiyun #include "evlist.h"
9*4882a593Smuzhiyun #include "expr.h"
10*4882a593Smuzhiyun #include "metricgroup.h"
11*4882a593Smuzhiyun #include <linux/zalloc.h>
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun /*
14*4882a593Smuzhiyun  * AGGR_GLOBAL: Use CPU 0
15*4882a593Smuzhiyun  * AGGR_SOCKET: Use first CPU of socket
16*4882a593Smuzhiyun  * AGGR_DIE: Use first CPU of die
17*4882a593Smuzhiyun  * AGGR_CORE: Use first CPU of core
18*4882a593Smuzhiyun  * AGGR_NONE: Use matching CPU
19*4882a593Smuzhiyun  * AGGR_THREAD: Not supported?
20*4882a593Smuzhiyun  */
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun struct runtime_stat rt_stat;
23*4882a593Smuzhiyun struct stats walltime_nsecs_stats;
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun struct saved_value {
26*4882a593Smuzhiyun 	struct rb_node rb_node;
27*4882a593Smuzhiyun 	struct evsel *evsel;
28*4882a593Smuzhiyun 	enum stat_type type;
29*4882a593Smuzhiyun 	int ctx;
30*4882a593Smuzhiyun 	int cpu;
31*4882a593Smuzhiyun 	struct runtime_stat *stat;
32*4882a593Smuzhiyun 	struct stats stats;
33*4882a593Smuzhiyun 	u64 metric_total;
34*4882a593Smuzhiyun 	int metric_other;
35*4882a593Smuzhiyun };
36*4882a593Smuzhiyun 
saved_value_cmp(struct rb_node * rb_node,const void * entry)37*4882a593Smuzhiyun static int saved_value_cmp(struct rb_node *rb_node, const void *entry)
38*4882a593Smuzhiyun {
39*4882a593Smuzhiyun 	struct saved_value *a = container_of(rb_node,
40*4882a593Smuzhiyun 					     struct saved_value,
41*4882a593Smuzhiyun 					     rb_node);
42*4882a593Smuzhiyun 	const struct saved_value *b = entry;
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun 	if (a->cpu != b->cpu)
45*4882a593Smuzhiyun 		return a->cpu - b->cpu;
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun 	/*
48*4882a593Smuzhiyun 	 * Previously the rbtree was used to link generic metrics.
49*4882a593Smuzhiyun 	 * The keys were evsel/cpu. Now the rbtree is extended to support
50*4882a593Smuzhiyun 	 * per-thread shadow stats. For shadow stats case, the keys
51*4882a593Smuzhiyun 	 * are cpu/type/ctx/stat (evsel is NULL). For generic metrics
52*4882a593Smuzhiyun 	 * case, the keys are still evsel/cpu (type/ctx/stat are 0 or NULL).
53*4882a593Smuzhiyun 	 */
54*4882a593Smuzhiyun 	if (a->type != b->type)
55*4882a593Smuzhiyun 		return a->type - b->type;
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 	if (a->ctx != b->ctx)
58*4882a593Smuzhiyun 		return a->ctx - b->ctx;
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	if (a->evsel == NULL && b->evsel == NULL) {
61*4882a593Smuzhiyun 		if (a->stat == b->stat)
62*4882a593Smuzhiyun 			return 0;
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 		if ((char *)a->stat < (char *)b->stat)
65*4882a593Smuzhiyun 			return -1;
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 		return 1;
68*4882a593Smuzhiyun 	}
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 	if (a->evsel == b->evsel)
71*4882a593Smuzhiyun 		return 0;
72*4882a593Smuzhiyun 	if ((char *)a->evsel < (char *)b->evsel)
73*4882a593Smuzhiyun 		return -1;
74*4882a593Smuzhiyun 	return +1;
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun 
saved_value_new(struct rblist * rblist __maybe_unused,const void * entry)77*4882a593Smuzhiyun static struct rb_node *saved_value_new(struct rblist *rblist __maybe_unused,
78*4882a593Smuzhiyun 				     const void *entry)
79*4882a593Smuzhiyun {
80*4882a593Smuzhiyun 	struct saved_value *nd = malloc(sizeof(struct saved_value));
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	if (!nd)
83*4882a593Smuzhiyun 		return NULL;
84*4882a593Smuzhiyun 	memcpy(nd, entry, sizeof(struct saved_value));
85*4882a593Smuzhiyun 	return &nd->rb_node;
86*4882a593Smuzhiyun }
87*4882a593Smuzhiyun 
saved_value_delete(struct rblist * rblist __maybe_unused,struct rb_node * rb_node)88*4882a593Smuzhiyun static void saved_value_delete(struct rblist *rblist __maybe_unused,
89*4882a593Smuzhiyun 			       struct rb_node *rb_node)
90*4882a593Smuzhiyun {
91*4882a593Smuzhiyun 	struct saved_value *v;
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	BUG_ON(!rb_node);
94*4882a593Smuzhiyun 	v = container_of(rb_node, struct saved_value, rb_node);
95*4882a593Smuzhiyun 	free(v);
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun 
saved_value_lookup(struct evsel * evsel,int cpu,bool create,enum stat_type type,int ctx,struct runtime_stat * st)98*4882a593Smuzhiyun static struct saved_value *saved_value_lookup(struct evsel *evsel,
99*4882a593Smuzhiyun 					      int cpu,
100*4882a593Smuzhiyun 					      bool create,
101*4882a593Smuzhiyun 					      enum stat_type type,
102*4882a593Smuzhiyun 					      int ctx,
103*4882a593Smuzhiyun 					      struct runtime_stat *st)
104*4882a593Smuzhiyun {
105*4882a593Smuzhiyun 	struct rblist *rblist;
106*4882a593Smuzhiyun 	struct rb_node *nd;
107*4882a593Smuzhiyun 	struct saved_value dm = {
108*4882a593Smuzhiyun 		.cpu = cpu,
109*4882a593Smuzhiyun 		.evsel = evsel,
110*4882a593Smuzhiyun 		.type = type,
111*4882a593Smuzhiyun 		.ctx = ctx,
112*4882a593Smuzhiyun 		.stat = st,
113*4882a593Smuzhiyun 	};
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	rblist = &st->value_list;
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	nd = rblist__find(rblist, &dm);
118*4882a593Smuzhiyun 	if (nd)
119*4882a593Smuzhiyun 		return container_of(nd, struct saved_value, rb_node);
120*4882a593Smuzhiyun 	if (create) {
121*4882a593Smuzhiyun 		rblist__add_node(rblist, &dm);
122*4882a593Smuzhiyun 		nd = rblist__find(rblist, &dm);
123*4882a593Smuzhiyun 		if (nd)
124*4882a593Smuzhiyun 			return container_of(nd, struct saved_value, rb_node);
125*4882a593Smuzhiyun 	}
126*4882a593Smuzhiyun 	return NULL;
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun 
runtime_stat__init(struct runtime_stat * st)129*4882a593Smuzhiyun void runtime_stat__init(struct runtime_stat *st)
130*4882a593Smuzhiyun {
131*4882a593Smuzhiyun 	struct rblist *rblist = &st->value_list;
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun 	rblist__init(rblist);
134*4882a593Smuzhiyun 	rblist->node_cmp = saved_value_cmp;
135*4882a593Smuzhiyun 	rblist->node_new = saved_value_new;
136*4882a593Smuzhiyun 	rblist->node_delete = saved_value_delete;
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun 
runtime_stat__exit(struct runtime_stat * st)139*4882a593Smuzhiyun void runtime_stat__exit(struct runtime_stat *st)
140*4882a593Smuzhiyun {
141*4882a593Smuzhiyun 	rblist__exit(&st->value_list);
142*4882a593Smuzhiyun }
143*4882a593Smuzhiyun 
perf_stat__init_shadow_stats(void)144*4882a593Smuzhiyun void perf_stat__init_shadow_stats(void)
145*4882a593Smuzhiyun {
146*4882a593Smuzhiyun 	runtime_stat__init(&rt_stat);
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun 
evsel_context(struct evsel * evsel)149*4882a593Smuzhiyun static int evsel_context(struct evsel *evsel)
150*4882a593Smuzhiyun {
151*4882a593Smuzhiyun 	int ctx = 0;
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 	if (evsel->core.attr.exclude_kernel)
154*4882a593Smuzhiyun 		ctx |= CTX_BIT_KERNEL;
155*4882a593Smuzhiyun 	if (evsel->core.attr.exclude_user)
156*4882a593Smuzhiyun 		ctx |= CTX_BIT_USER;
157*4882a593Smuzhiyun 	if (evsel->core.attr.exclude_hv)
158*4882a593Smuzhiyun 		ctx |= CTX_BIT_HV;
159*4882a593Smuzhiyun 	if (evsel->core.attr.exclude_host)
160*4882a593Smuzhiyun 		ctx |= CTX_BIT_HOST;
161*4882a593Smuzhiyun 	if (evsel->core.attr.exclude_idle)
162*4882a593Smuzhiyun 		ctx |= CTX_BIT_IDLE;
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun 	return ctx;
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun 
reset_stat(struct runtime_stat * st)167*4882a593Smuzhiyun static void reset_stat(struct runtime_stat *st)
168*4882a593Smuzhiyun {
169*4882a593Smuzhiyun 	struct rblist *rblist;
170*4882a593Smuzhiyun 	struct rb_node *pos, *next;
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 	rblist = &st->value_list;
173*4882a593Smuzhiyun 	next = rb_first_cached(&rblist->entries);
174*4882a593Smuzhiyun 	while (next) {
175*4882a593Smuzhiyun 		pos = next;
176*4882a593Smuzhiyun 		next = rb_next(pos);
177*4882a593Smuzhiyun 		memset(&container_of(pos, struct saved_value, rb_node)->stats,
178*4882a593Smuzhiyun 		       0,
179*4882a593Smuzhiyun 		       sizeof(struct stats));
180*4882a593Smuzhiyun 	}
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun 
perf_stat__reset_shadow_stats(void)183*4882a593Smuzhiyun void perf_stat__reset_shadow_stats(void)
184*4882a593Smuzhiyun {
185*4882a593Smuzhiyun 	reset_stat(&rt_stat);
186*4882a593Smuzhiyun 	memset(&walltime_nsecs_stats, 0, sizeof(walltime_nsecs_stats));
187*4882a593Smuzhiyun }
188*4882a593Smuzhiyun 
perf_stat__reset_shadow_per_stat(struct runtime_stat * st)189*4882a593Smuzhiyun void perf_stat__reset_shadow_per_stat(struct runtime_stat *st)
190*4882a593Smuzhiyun {
191*4882a593Smuzhiyun 	reset_stat(st);
192*4882a593Smuzhiyun }
193*4882a593Smuzhiyun 
update_runtime_stat(struct runtime_stat * st,enum stat_type type,int ctx,int cpu,u64 count)194*4882a593Smuzhiyun static void update_runtime_stat(struct runtime_stat *st,
195*4882a593Smuzhiyun 				enum stat_type type,
196*4882a593Smuzhiyun 				int ctx, int cpu, u64 count)
197*4882a593Smuzhiyun {
198*4882a593Smuzhiyun 	struct saved_value *v = saved_value_lookup(NULL, cpu, true,
199*4882a593Smuzhiyun 						   type, ctx, st);
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 	if (v)
202*4882a593Smuzhiyun 		update_stats(&v->stats, count);
203*4882a593Smuzhiyun }
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun /*
206*4882a593Smuzhiyun  * Update various tracking values we maintain to print
207*4882a593Smuzhiyun  * more semantic information such as miss/hit ratios,
208*4882a593Smuzhiyun  * instruction rates, etc:
209*4882a593Smuzhiyun  */
perf_stat__update_shadow_stats(struct evsel * counter,u64 count,int cpu,struct runtime_stat * st)210*4882a593Smuzhiyun void perf_stat__update_shadow_stats(struct evsel *counter, u64 count,
211*4882a593Smuzhiyun 				    int cpu, struct runtime_stat *st)
212*4882a593Smuzhiyun {
213*4882a593Smuzhiyun 	int ctx = evsel_context(counter);
214*4882a593Smuzhiyun 	u64 count_ns = count;
215*4882a593Smuzhiyun 	struct saved_value *v;
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun 	count *= counter->scale;
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun 	if (evsel__is_clock(counter))
220*4882a593Smuzhiyun 		update_runtime_stat(st, STAT_NSECS, 0, cpu, count_ns);
221*4882a593Smuzhiyun 	else if (evsel__match(counter, HARDWARE, HW_CPU_CYCLES))
222*4882a593Smuzhiyun 		update_runtime_stat(st, STAT_CYCLES, ctx, cpu, count);
223*4882a593Smuzhiyun 	else if (perf_stat_evsel__is(counter, CYCLES_IN_TX))
224*4882a593Smuzhiyun 		update_runtime_stat(st, STAT_CYCLES_IN_TX, ctx, cpu, count);
225*4882a593Smuzhiyun 	else if (perf_stat_evsel__is(counter, TRANSACTION_START))
226*4882a593Smuzhiyun 		update_runtime_stat(st, STAT_TRANSACTION, ctx, cpu, count);
227*4882a593Smuzhiyun 	else if (perf_stat_evsel__is(counter, ELISION_START))
228*4882a593Smuzhiyun 		update_runtime_stat(st, STAT_ELISION, ctx, cpu, count);
229*4882a593Smuzhiyun 	else if (perf_stat_evsel__is(counter, TOPDOWN_TOTAL_SLOTS))
230*4882a593Smuzhiyun 		update_runtime_stat(st, STAT_TOPDOWN_TOTAL_SLOTS,
231*4882a593Smuzhiyun 				    ctx, cpu, count);
232*4882a593Smuzhiyun 	else if (perf_stat_evsel__is(counter, TOPDOWN_SLOTS_ISSUED))
233*4882a593Smuzhiyun 		update_runtime_stat(st, STAT_TOPDOWN_SLOTS_ISSUED,
234*4882a593Smuzhiyun 				    ctx, cpu, count);
235*4882a593Smuzhiyun 	else if (perf_stat_evsel__is(counter, TOPDOWN_SLOTS_RETIRED))
236*4882a593Smuzhiyun 		update_runtime_stat(st, STAT_TOPDOWN_SLOTS_RETIRED,
237*4882a593Smuzhiyun 				    ctx, cpu, count);
238*4882a593Smuzhiyun 	else if (perf_stat_evsel__is(counter, TOPDOWN_FETCH_BUBBLES))
239*4882a593Smuzhiyun 		update_runtime_stat(st, STAT_TOPDOWN_FETCH_BUBBLES,
240*4882a593Smuzhiyun 				    ctx, cpu, count);
241*4882a593Smuzhiyun 	else if (perf_stat_evsel__is(counter, TOPDOWN_RECOVERY_BUBBLES))
242*4882a593Smuzhiyun 		update_runtime_stat(st, STAT_TOPDOWN_RECOVERY_BUBBLES,
243*4882a593Smuzhiyun 				    ctx, cpu, count);
244*4882a593Smuzhiyun 	else if (perf_stat_evsel__is(counter, TOPDOWN_RETIRING))
245*4882a593Smuzhiyun 		update_runtime_stat(st, STAT_TOPDOWN_RETIRING,
246*4882a593Smuzhiyun 				    ctx, cpu, count);
247*4882a593Smuzhiyun 	else if (perf_stat_evsel__is(counter, TOPDOWN_BAD_SPEC))
248*4882a593Smuzhiyun 		update_runtime_stat(st, STAT_TOPDOWN_BAD_SPEC,
249*4882a593Smuzhiyun 				    ctx, cpu, count);
250*4882a593Smuzhiyun 	else if (perf_stat_evsel__is(counter, TOPDOWN_FE_BOUND))
251*4882a593Smuzhiyun 		update_runtime_stat(st, STAT_TOPDOWN_FE_BOUND,
252*4882a593Smuzhiyun 				    ctx, cpu, count);
253*4882a593Smuzhiyun 	else if (perf_stat_evsel__is(counter, TOPDOWN_BE_BOUND))
254*4882a593Smuzhiyun 		update_runtime_stat(st, STAT_TOPDOWN_BE_BOUND,
255*4882a593Smuzhiyun 				    ctx, cpu, count);
256*4882a593Smuzhiyun 	else if (evsel__match(counter, HARDWARE, HW_STALLED_CYCLES_FRONTEND))
257*4882a593Smuzhiyun 		update_runtime_stat(st, STAT_STALLED_CYCLES_FRONT,
258*4882a593Smuzhiyun 				    ctx, cpu, count);
259*4882a593Smuzhiyun 	else if (evsel__match(counter, HARDWARE, HW_STALLED_CYCLES_BACKEND))
260*4882a593Smuzhiyun 		update_runtime_stat(st, STAT_STALLED_CYCLES_BACK,
261*4882a593Smuzhiyun 				    ctx, cpu, count);
262*4882a593Smuzhiyun 	else if (evsel__match(counter, HARDWARE, HW_BRANCH_INSTRUCTIONS))
263*4882a593Smuzhiyun 		update_runtime_stat(st, STAT_BRANCHES, ctx, cpu, count);
264*4882a593Smuzhiyun 	else if (evsel__match(counter, HARDWARE, HW_CACHE_REFERENCES))
265*4882a593Smuzhiyun 		update_runtime_stat(st, STAT_CACHEREFS, ctx, cpu, count);
266*4882a593Smuzhiyun 	else if (evsel__match(counter, HW_CACHE, HW_CACHE_L1D))
267*4882a593Smuzhiyun 		update_runtime_stat(st, STAT_L1_DCACHE, ctx, cpu, count);
268*4882a593Smuzhiyun 	else if (evsel__match(counter, HW_CACHE, HW_CACHE_L1I))
269*4882a593Smuzhiyun 		update_runtime_stat(st, STAT_L1_ICACHE, ctx, cpu, count);
270*4882a593Smuzhiyun 	else if (evsel__match(counter, HW_CACHE, HW_CACHE_LL))
271*4882a593Smuzhiyun 		update_runtime_stat(st, STAT_LL_CACHE, ctx, cpu, count);
272*4882a593Smuzhiyun 	else if (evsel__match(counter, HW_CACHE, HW_CACHE_DTLB))
273*4882a593Smuzhiyun 		update_runtime_stat(st, STAT_DTLB_CACHE, ctx, cpu, count);
274*4882a593Smuzhiyun 	else if (evsel__match(counter, HW_CACHE, HW_CACHE_ITLB))
275*4882a593Smuzhiyun 		update_runtime_stat(st, STAT_ITLB_CACHE, ctx, cpu, count);
276*4882a593Smuzhiyun 	else if (perf_stat_evsel__is(counter, SMI_NUM))
277*4882a593Smuzhiyun 		update_runtime_stat(st, STAT_SMI_NUM, ctx, cpu, count);
278*4882a593Smuzhiyun 	else if (perf_stat_evsel__is(counter, APERF))
279*4882a593Smuzhiyun 		update_runtime_stat(st, STAT_APERF, ctx, cpu, count);
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 	if (counter->collect_stat) {
282*4882a593Smuzhiyun 		v = saved_value_lookup(counter, cpu, true, STAT_NONE, 0, st);
283*4882a593Smuzhiyun 		update_stats(&v->stats, count);
284*4882a593Smuzhiyun 		if (counter->metric_leader)
285*4882a593Smuzhiyun 			v->metric_total += count;
286*4882a593Smuzhiyun 	} else if (counter->metric_leader) {
287*4882a593Smuzhiyun 		v = saved_value_lookup(counter->metric_leader,
288*4882a593Smuzhiyun 				       cpu, true, STAT_NONE, 0, st);
289*4882a593Smuzhiyun 		v->metric_total += count;
290*4882a593Smuzhiyun 		v->metric_other++;
291*4882a593Smuzhiyun 	}
292*4882a593Smuzhiyun }
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun /* used for get_ratio_color() */
295*4882a593Smuzhiyun enum grc_type {
296*4882a593Smuzhiyun 	GRC_STALLED_CYCLES_FE,
297*4882a593Smuzhiyun 	GRC_STALLED_CYCLES_BE,
298*4882a593Smuzhiyun 	GRC_CACHE_MISSES,
299*4882a593Smuzhiyun 	GRC_MAX_NR
300*4882a593Smuzhiyun };
301*4882a593Smuzhiyun 
get_ratio_color(enum grc_type type,double ratio)302*4882a593Smuzhiyun static const char *get_ratio_color(enum grc_type type, double ratio)
303*4882a593Smuzhiyun {
304*4882a593Smuzhiyun 	static const double grc_table[GRC_MAX_NR][3] = {
305*4882a593Smuzhiyun 		[GRC_STALLED_CYCLES_FE] = { 50.0, 30.0, 10.0 },
306*4882a593Smuzhiyun 		[GRC_STALLED_CYCLES_BE] = { 75.0, 50.0, 20.0 },
307*4882a593Smuzhiyun 		[GRC_CACHE_MISSES] 	= { 20.0, 10.0, 5.0 },
308*4882a593Smuzhiyun 	};
309*4882a593Smuzhiyun 	const char *color = PERF_COLOR_NORMAL;
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun 	if (ratio > grc_table[type][0])
312*4882a593Smuzhiyun 		color = PERF_COLOR_RED;
313*4882a593Smuzhiyun 	else if (ratio > grc_table[type][1])
314*4882a593Smuzhiyun 		color = PERF_COLOR_MAGENTA;
315*4882a593Smuzhiyun 	else if (ratio > grc_table[type][2])
316*4882a593Smuzhiyun 		color = PERF_COLOR_YELLOW;
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 	return color;
319*4882a593Smuzhiyun }
320*4882a593Smuzhiyun 
perf_stat__find_event(struct evlist * evsel_list,const char * name)321*4882a593Smuzhiyun static struct evsel *perf_stat__find_event(struct evlist *evsel_list,
322*4882a593Smuzhiyun 						const char *name)
323*4882a593Smuzhiyun {
324*4882a593Smuzhiyun 	struct evsel *c2;
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun 	evlist__for_each_entry (evsel_list, c2) {
327*4882a593Smuzhiyun 		if (!strcasecmp(c2->name, name) && !c2->collect_stat)
328*4882a593Smuzhiyun 			return c2;
329*4882a593Smuzhiyun 	}
330*4882a593Smuzhiyun 	return NULL;
331*4882a593Smuzhiyun }
332*4882a593Smuzhiyun 
333*4882a593Smuzhiyun /* Mark MetricExpr target events and link events using them to them. */
perf_stat__collect_metric_expr(struct evlist * evsel_list)334*4882a593Smuzhiyun void perf_stat__collect_metric_expr(struct evlist *evsel_list)
335*4882a593Smuzhiyun {
336*4882a593Smuzhiyun 	struct evsel *counter, *leader, **metric_events, *oc;
337*4882a593Smuzhiyun 	bool found;
338*4882a593Smuzhiyun 	struct expr_parse_ctx ctx;
339*4882a593Smuzhiyun 	struct hashmap_entry *cur;
340*4882a593Smuzhiyun 	size_t bkt;
341*4882a593Smuzhiyun 	int i;
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun 	expr__ctx_init(&ctx);
344*4882a593Smuzhiyun 	evlist__for_each_entry(evsel_list, counter) {
345*4882a593Smuzhiyun 		bool invalid = false;
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun 		leader = counter->leader;
348*4882a593Smuzhiyun 		if (!counter->metric_expr)
349*4882a593Smuzhiyun 			continue;
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun 		expr__ctx_clear(&ctx);
352*4882a593Smuzhiyun 		metric_events = counter->metric_events;
353*4882a593Smuzhiyun 		if (!metric_events) {
354*4882a593Smuzhiyun 			if (expr__find_other(counter->metric_expr,
355*4882a593Smuzhiyun 					     counter->name,
356*4882a593Smuzhiyun 					     &ctx, 1) < 0)
357*4882a593Smuzhiyun 				continue;
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun 			metric_events = calloc(sizeof(struct evsel *),
360*4882a593Smuzhiyun 					       hashmap__size(&ctx.ids) + 1);
361*4882a593Smuzhiyun 			if (!metric_events) {
362*4882a593Smuzhiyun 				expr__ctx_clear(&ctx);
363*4882a593Smuzhiyun 				return;
364*4882a593Smuzhiyun 			}
365*4882a593Smuzhiyun 			counter->metric_events = metric_events;
366*4882a593Smuzhiyun 		}
367*4882a593Smuzhiyun 
368*4882a593Smuzhiyun 		i = 0;
369*4882a593Smuzhiyun 		hashmap__for_each_entry((&ctx.ids), cur, bkt) {
370*4882a593Smuzhiyun 			const char *metric_name = (const char *)cur->key;
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun 			found = false;
373*4882a593Smuzhiyun 			if (leader) {
374*4882a593Smuzhiyun 				/* Search in group */
375*4882a593Smuzhiyun 				for_each_group_member (oc, leader) {
376*4882a593Smuzhiyun 					if (!strcasecmp(oc->name,
377*4882a593Smuzhiyun 							metric_name) &&
378*4882a593Smuzhiyun 						!oc->collect_stat) {
379*4882a593Smuzhiyun 						found = true;
380*4882a593Smuzhiyun 						break;
381*4882a593Smuzhiyun 					}
382*4882a593Smuzhiyun 				}
383*4882a593Smuzhiyun 			}
384*4882a593Smuzhiyun 			if (!found) {
385*4882a593Smuzhiyun 				/* Search ignoring groups */
386*4882a593Smuzhiyun 				oc = perf_stat__find_event(evsel_list,
387*4882a593Smuzhiyun 							   metric_name);
388*4882a593Smuzhiyun 			}
389*4882a593Smuzhiyun 			if (!oc) {
390*4882a593Smuzhiyun 				/* Deduping one is good enough to handle duplicated PMUs. */
391*4882a593Smuzhiyun 				static char *printed;
392*4882a593Smuzhiyun 
393*4882a593Smuzhiyun 				/*
394*4882a593Smuzhiyun 				 * Adding events automatically would be difficult, because
395*4882a593Smuzhiyun 				 * it would risk creating groups that are not schedulable.
396*4882a593Smuzhiyun 				 * perf stat doesn't understand all the scheduling constraints
397*4882a593Smuzhiyun 				 * of events. So we ask the user instead to add the missing
398*4882a593Smuzhiyun 				 * events.
399*4882a593Smuzhiyun 				 */
400*4882a593Smuzhiyun 				if (!printed ||
401*4882a593Smuzhiyun 				    strcasecmp(printed, metric_name)) {
402*4882a593Smuzhiyun 					fprintf(stderr,
403*4882a593Smuzhiyun 						"Add %s event to groups to get metric expression for %s\n",
404*4882a593Smuzhiyun 						metric_name,
405*4882a593Smuzhiyun 						counter->name);
406*4882a593Smuzhiyun 					printed = strdup(metric_name);
407*4882a593Smuzhiyun 				}
408*4882a593Smuzhiyun 				invalid = true;
409*4882a593Smuzhiyun 				continue;
410*4882a593Smuzhiyun 			}
411*4882a593Smuzhiyun 			metric_events[i++] = oc;
412*4882a593Smuzhiyun 			oc->collect_stat = true;
413*4882a593Smuzhiyun 		}
414*4882a593Smuzhiyun 		metric_events[i] = NULL;
415*4882a593Smuzhiyun 		if (invalid) {
416*4882a593Smuzhiyun 			free(metric_events);
417*4882a593Smuzhiyun 			counter->metric_events = NULL;
418*4882a593Smuzhiyun 			counter->metric_expr = NULL;
419*4882a593Smuzhiyun 		}
420*4882a593Smuzhiyun 	}
421*4882a593Smuzhiyun 	expr__ctx_clear(&ctx);
422*4882a593Smuzhiyun }
423*4882a593Smuzhiyun 
runtime_stat_avg(struct runtime_stat * st,enum stat_type type,int ctx,int cpu)424*4882a593Smuzhiyun static double runtime_stat_avg(struct runtime_stat *st,
425*4882a593Smuzhiyun 			       enum stat_type type, int ctx, int cpu)
426*4882a593Smuzhiyun {
427*4882a593Smuzhiyun 	struct saved_value *v;
428*4882a593Smuzhiyun 
429*4882a593Smuzhiyun 	v = saved_value_lookup(NULL, cpu, false, type, ctx, st);
430*4882a593Smuzhiyun 	if (!v)
431*4882a593Smuzhiyun 		return 0.0;
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun 	return avg_stats(&v->stats);
434*4882a593Smuzhiyun }
435*4882a593Smuzhiyun 
runtime_stat_n(struct runtime_stat * st,enum stat_type type,int ctx,int cpu)436*4882a593Smuzhiyun static double runtime_stat_n(struct runtime_stat *st,
437*4882a593Smuzhiyun 			     enum stat_type type, int ctx, int cpu)
438*4882a593Smuzhiyun {
439*4882a593Smuzhiyun 	struct saved_value *v;
440*4882a593Smuzhiyun 
441*4882a593Smuzhiyun 	v = saved_value_lookup(NULL, cpu, false, type, ctx, st);
442*4882a593Smuzhiyun 	if (!v)
443*4882a593Smuzhiyun 		return 0.0;
444*4882a593Smuzhiyun 
445*4882a593Smuzhiyun 	return v->stats.n;
446*4882a593Smuzhiyun }
447*4882a593Smuzhiyun 
print_stalled_cycles_frontend(struct perf_stat_config * config,int cpu,struct evsel * evsel,double avg,struct perf_stat_output_ctx * out,struct runtime_stat * st)448*4882a593Smuzhiyun static void print_stalled_cycles_frontend(struct perf_stat_config *config,
449*4882a593Smuzhiyun 					  int cpu,
450*4882a593Smuzhiyun 					  struct evsel *evsel, double avg,
451*4882a593Smuzhiyun 					  struct perf_stat_output_ctx *out,
452*4882a593Smuzhiyun 					  struct runtime_stat *st)
453*4882a593Smuzhiyun {
454*4882a593Smuzhiyun 	double total, ratio = 0.0;
455*4882a593Smuzhiyun 	const char *color;
456*4882a593Smuzhiyun 	int ctx = evsel_context(evsel);
457*4882a593Smuzhiyun 
458*4882a593Smuzhiyun 	total = runtime_stat_avg(st, STAT_CYCLES, ctx, cpu);
459*4882a593Smuzhiyun 
460*4882a593Smuzhiyun 	if (total)
461*4882a593Smuzhiyun 		ratio = avg / total * 100.0;
462*4882a593Smuzhiyun 
463*4882a593Smuzhiyun 	color = get_ratio_color(GRC_STALLED_CYCLES_FE, ratio);
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun 	if (ratio)
466*4882a593Smuzhiyun 		out->print_metric(config, out->ctx, color, "%7.2f%%", "frontend cycles idle",
467*4882a593Smuzhiyun 				  ratio);
468*4882a593Smuzhiyun 	else
469*4882a593Smuzhiyun 		out->print_metric(config, out->ctx, NULL, NULL, "frontend cycles idle", 0);
470*4882a593Smuzhiyun }
471*4882a593Smuzhiyun 
print_stalled_cycles_backend(struct perf_stat_config * config,int cpu,struct evsel * evsel,double avg,struct perf_stat_output_ctx * out,struct runtime_stat * st)472*4882a593Smuzhiyun static void print_stalled_cycles_backend(struct perf_stat_config *config,
473*4882a593Smuzhiyun 					 int cpu,
474*4882a593Smuzhiyun 					 struct evsel *evsel, double avg,
475*4882a593Smuzhiyun 					 struct perf_stat_output_ctx *out,
476*4882a593Smuzhiyun 					 struct runtime_stat *st)
477*4882a593Smuzhiyun {
478*4882a593Smuzhiyun 	double total, ratio = 0.0;
479*4882a593Smuzhiyun 	const char *color;
480*4882a593Smuzhiyun 	int ctx = evsel_context(evsel);
481*4882a593Smuzhiyun 
482*4882a593Smuzhiyun 	total = runtime_stat_avg(st, STAT_CYCLES, ctx, cpu);
483*4882a593Smuzhiyun 
484*4882a593Smuzhiyun 	if (total)
485*4882a593Smuzhiyun 		ratio = avg / total * 100.0;
486*4882a593Smuzhiyun 
487*4882a593Smuzhiyun 	color = get_ratio_color(GRC_STALLED_CYCLES_BE, ratio);
488*4882a593Smuzhiyun 
489*4882a593Smuzhiyun 	out->print_metric(config, out->ctx, color, "%7.2f%%", "backend cycles idle", ratio);
490*4882a593Smuzhiyun }
491*4882a593Smuzhiyun 
print_branch_misses(struct perf_stat_config * config,int cpu,struct evsel * evsel,double avg,struct perf_stat_output_ctx * out,struct runtime_stat * st)492*4882a593Smuzhiyun static void print_branch_misses(struct perf_stat_config *config,
493*4882a593Smuzhiyun 				int cpu,
494*4882a593Smuzhiyun 				struct evsel *evsel,
495*4882a593Smuzhiyun 				double avg,
496*4882a593Smuzhiyun 				struct perf_stat_output_ctx *out,
497*4882a593Smuzhiyun 				struct runtime_stat *st)
498*4882a593Smuzhiyun {
499*4882a593Smuzhiyun 	double total, ratio = 0.0;
500*4882a593Smuzhiyun 	const char *color;
501*4882a593Smuzhiyun 	int ctx = evsel_context(evsel);
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun 	total = runtime_stat_avg(st, STAT_BRANCHES, ctx, cpu);
504*4882a593Smuzhiyun 
505*4882a593Smuzhiyun 	if (total)
506*4882a593Smuzhiyun 		ratio = avg / total * 100.0;
507*4882a593Smuzhiyun 
508*4882a593Smuzhiyun 	color = get_ratio_color(GRC_CACHE_MISSES, ratio);
509*4882a593Smuzhiyun 
510*4882a593Smuzhiyun 	out->print_metric(config, out->ctx, color, "%7.2f%%", "of all branches", ratio);
511*4882a593Smuzhiyun }
512*4882a593Smuzhiyun 
print_l1_dcache_misses(struct perf_stat_config * config,int cpu,struct evsel * evsel,double avg,struct perf_stat_output_ctx * out,struct runtime_stat * st)513*4882a593Smuzhiyun static void print_l1_dcache_misses(struct perf_stat_config *config,
514*4882a593Smuzhiyun 				   int cpu,
515*4882a593Smuzhiyun 				   struct evsel *evsel,
516*4882a593Smuzhiyun 				   double avg,
517*4882a593Smuzhiyun 				   struct perf_stat_output_ctx *out,
518*4882a593Smuzhiyun 				   struct runtime_stat *st)
519*4882a593Smuzhiyun 
520*4882a593Smuzhiyun {
521*4882a593Smuzhiyun 	double total, ratio = 0.0;
522*4882a593Smuzhiyun 	const char *color;
523*4882a593Smuzhiyun 	int ctx = evsel_context(evsel);
524*4882a593Smuzhiyun 
525*4882a593Smuzhiyun 	total = runtime_stat_avg(st, STAT_L1_DCACHE, ctx, cpu);
526*4882a593Smuzhiyun 
527*4882a593Smuzhiyun 	if (total)
528*4882a593Smuzhiyun 		ratio = avg / total * 100.0;
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun 	color = get_ratio_color(GRC_CACHE_MISSES, ratio);
531*4882a593Smuzhiyun 
532*4882a593Smuzhiyun 	out->print_metric(config, out->ctx, color, "%7.2f%%", "of all L1-dcache accesses", ratio);
533*4882a593Smuzhiyun }
534*4882a593Smuzhiyun 
print_l1_icache_misses(struct perf_stat_config * config,int cpu,struct evsel * evsel,double avg,struct perf_stat_output_ctx * out,struct runtime_stat * st)535*4882a593Smuzhiyun static void print_l1_icache_misses(struct perf_stat_config *config,
536*4882a593Smuzhiyun 				   int cpu,
537*4882a593Smuzhiyun 				   struct evsel *evsel,
538*4882a593Smuzhiyun 				   double avg,
539*4882a593Smuzhiyun 				   struct perf_stat_output_ctx *out,
540*4882a593Smuzhiyun 				   struct runtime_stat *st)
541*4882a593Smuzhiyun 
542*4882a593Smuzhiyun {
543*4882a593Smuzhiyun 	double total, ratio = 0.0;
544*4882a593Smuzhiyun 	const char *color;
545*4882a593Smuzhiyun 	int ctx = evsel_context(evsel);
546*4882a593Smuzhiyun 
547*4882a593Smuzhiyun 	total = runtime_stat_avg(st, STAT_L1_ICACHE, ctx, cpu);
548*4882a593Smuzhiyun 
549*4882a593Smuzhiyun 	if (total)
550*4882a593Smuzhiyun 		ratio = avg / total * 100.0;
551*4882a593Smuzhiyun 
552*4882a593Smuzhiyun 	color = get_ratio_color(GRC_CACHE_MISSES, ratio);
553*4882a593Smuzhiyun 	out->print_metric(config, out->ctx, color, "%7.2f%%", "of all L1-icache accesses", ratio);
554*4882a593Smuzhiyun }
555*4882a593Smuzhiyun 
print_dtlb_cache_misses(struct perf_stat_config * config,int cpu,struct evsel * evsel,double avg,struct perf_stat_output_ctx * out,struct runtime_stat * st)556*4882a593Smuzhiyun static void print_dtlb_cache_misses(struct perf_stat_config *config,
557*4882a593Smuzhiyun 				    int cpu,
558*4882a593Smuzhiyun 				    struct evsel *evsel,
559*4882a593Smuzhiyun 				    double avg,
560*4882a593Smuzhiyun 				    struct perf_stat_output_ctx *out,
561*4882a593Smuzhiyun 				    struct runtime_stat *st)
562*4882a593Smuzhiyun {
563*4882a593Smuzhiyun 	double total, ratio = 0.0;
564*4882a593Smuzhiyun 	const char *color;
565*4882a593Smuzhiyun 	int ctx = evsel_context(evsel);
566*4882a593Smuzhiyun 
567*4882a593Smuzhiyun 	total = runtime_stat_avg(st, STAT_DTLB_CACHE, ctx, cpu);
568*4882a593Smuzhiyun 
569*4882a593Smuzhiyun 	if (total)
570*4882a593Smuzhiyun 		ratio = avg / total * 100.0;
571*4882a593Smuzhiyun 
572*4882a593Smuzhiyun 	color = get_ratio_color(GRC_CACHE_MISSES, ratio);
573*4882a593Smuzhiyun 	out->print_metric(config, out->ctx, color, "%7.2f%%", "of all dTLB cache accesses", ratio);
574*4882a593Smuzhiyun }
575*4882a593Smuzhiyun 
print_itlb_cache_misses(struct perf_stat_config * config,int cpu,struct evsel * evsel,double avg,struct perf_stat_output_ctx * out,struct runtime_stat * st)576*4882a593Smuzhiyun static void print_itlb_cache_misses(struct perf_stat_config *config,
577*4882a593Smuzhiyun 				    int cpu,
578*4882a593Smuzhiyun 				    struct evsel *evsel,
579*4882a593Smuzhiyun 				    double avg,
580*4882a593Smuzhiyun 				    struct perf_stat_output_ctx *out,
581*4882a593Smuzhiyun 				    struct runtime_stat *st)
582*4882a593Smuzhiyun {
583*4882a593Smuzhiyun 	double total, ratio = 0.0;
584*4882a593Smuzhiyun 	const char *color;
585*4882a593Smuzhiyun 	int ctx = evsel_context(evsel);
586*4882a593Smuzhiyun 
587*4882a593Smuzhiyun 	total = runtime_stat_avg(st, STAT_ITLB_CACHE, ctx, cpu);
588*4882a593Smuzhiyun 
589*4882a593Smuzhiyun 	if (total)
590*4882a593Smuzhiyun 		ratio = avg / total * 100.0;
591*4882a593Smuzhiyun 
592*4882a593Smuzhiyun 	color = get_ratio_color(GRC_CACHE_MISSES, ratio);
593*4882a593Smuzhiyun 	out->print_metric(config, out->ctx, color, "%7.2f%%", "of all iTLB cache accesses", ratio);
594*4882a593Smuzhiyun }
595*4882a593Smuzhiyun 
print_ll_cache_misses(struct perf_stat_config * config,int cpu,struct evsel * evsel,double avg,struct perf_stat_output_ctx * out,struct runtime_stat * st)596*4882a593Smuzhiyun static void print_ll_cache_misses(struct perf_stat_config *config,
597*4882a593Smuzhiyun 				  int cpu,
598*4882a593Smuzhiyun 				  struct evsel *evsel,
599*4882a593Smuzhiyun 				  double avg,
600*4882a593Smuzhiyun 				  struct perf_stat_output_ctx *out,
601*4882a593Smuzhiyun 				  struct runtime_stat *st)
602*4882a593Smuzhiyun {
603*4882a593Smuzhiyun 	double total, ratio = 0.0;
604*4882a593Smuzhiyun 	const char *color;
605*4882a593Smuzhiyun 	int ctx = evsel_context(evsel);
606*4882a593Smuzhiyun 
607*4882a593Smuzhiyun 	total = runtime_stat_avg(st, STAT_LL_CACHE, ctx, cpu);
608*4882a593Smuzhiyun 
609*4882a593Smuzhiyun 	if (total)
610*4882a593Smuzhiyun 		ratio = avg / total * 100.0;
611*4882a593Smuzhiyun 
612*4882a593Smuzhiyun 	color = get_ratio_color(GRC_CACHE_MISSES, ratio);
613*4882a593Smuzhiyun 	out->print_metric(config, out->ctx, color, "%7.2f%%", "of all LL-cache accesses", ratio);
614*4882a593Smuzhiyun }
615*4882a593Smuzhiyun 
616*4882a593Smuzhiyun /*
617*4882a593Smuzhiyun  * High level "TopDown" CPU core pipe line bottleneck break down.
618*4882a593Smuzhiyun  *
619*4882a593Smuzhiyun  * Basic concept following
620*4882a593Smuzhiyun  * Yasin, A Top Down Method for Performance analysis and Counter architecture
621*4882a593Smuzhiyun  * ISPASS14
622*4882a593Smuzhiyun  *
623*4882a593Smuzhiyun  * The CPU pipeline is divided into 4 areas that can be bottlenecks:
624*4882a593Smuzhiyun  *
625*4882a593Smuzhiyun  * Frontend -> Backend -> Retiring
626*4882a593Smuzhiyun  * BadSpeculation in addition means out of order execution that is thrown away
627*4882a593Smuzhiyun  * (for example branch mispredictions)
628*4882a593Smuzhiyun  * Frontend is instruction decoding.
629*4882a593Smuzhiyun  * Backend is execution, like computation and accessing data in memory
630*4882a593Smuzhiyun  * Retiring is good execution that is not directly bottlenecked
631*4882a593Smuzhiyun  *
632*4882a593Smuzhiyun  * The formulas are computed in slots.
633*4882a593Smuzhiyun  * A slot is an entry in the pipeline each for the pipeline width
634*4882a593Smuzhiyun  * (for example a 4-wide pipeline has 4 slots for each cycle)
635*4882a593Smuzhiyun  *
636*4882a593Smuzhiyun  * Formulas:
637*4882a593Smuzhiyun  * BadSpeculation = ((SlotsIssued - SlotsRetired) + RecoveryBubbles) /
638*4882a593Smuzhiyun  *			TotalSlots
639*4882a593Smuzhiyun  * Retiring = SlotsRetired / TotalSlots
640*4882a593Smuzhiyun  * FrontendBound = FetchBubbles / TotalSlots
641*4882a593Smuzhiyun  * BackendBound = 1.0 - BadSpeculation - Retiring - FrontendBound
642*4882a593Smuzhiyun  *
643*4882a593Smuzhiyun  * The kernel provides the mapping to the low level CPU events and any scaling
644*4882a593Smuzhiyun  * needed for the CPU pipeline width, for example:
645*4882a593Smuzhiyun  *
646*4882a593Smuzhiyun  * TotalSlots = Cycles * 4
647*4882a593Smuzhiyun  *
648*4882a593Smuzhiyun  * The scaling factor is communicated in the sysfs unit.
649*4882a593Smuzhiyun  *
650*4882a593Smuzhiyun  * In some cases the CPU may not be able to measure all the formulas due to
651*4882a593Smuzhiyun  * missing events. In this case multiple formulas are combined, as possible.
652*4882a593Smuzhiyun  *
653*4882a593Smuzhiyun  * Full TopDown supports more levels to sub-divide each area: for example
654*4882a593Smuzhiyun  * BackendBound into computing bound and memory bound. For now we only
655*4882a593Smuzhiyun  * support Level 1 TopDown.
656*4882a593Smuzhiyun  */
657*4882a593Smuzhiyun 
sanitize_val(double x)658*4882a593Smuzhiyun static double sanitize_val(double x)
659*4882a593Smuzhiyun {
660*4882a593Smuzhiyun 	if (x < 0 && x >= -0.02)
661*4882a593Smuzhiyun 		return 0.0;
662*4882a593Smuzhiyun 	return x;
663*4882a593Smuzhiyun }
664*4882a593Smuzhiyun 
td_total_slots(int ctx,int cpu,struct runtime_stat * st)665*4882a593Smuzhiyun static double td_total_slots(int ctx, int cpu, struct runtime_stat *st)
666*4882a593Smuzhiyun {
667*4882a593Smuzhiyun 	return runtime_stat_avg(st, STAT_TOPDOWN_TOTAL_SLOTS, ctx, cpu);
668*4882a593Smuzhiyun }
669*4882a593Smuzhiyun 
td_bad_spec(int ctx,int cpu,struct runtime_stat * st)670*4882a593Smuzhiyun static double td_bad_spec(int ctx, int cpu, struct runtime_stat *st)
671*4882a593Smuzhiyun {
672*4882a593Smuzhiyun 	double bad_spec = 0;
673*4882a593Smuzhiyun 	double total_slots;
674*4882a593Smuzhiyun 	double total;
675*4882a593Smuzhiyun 
676*4882a593Smuzhiyun 	total = runtime_stat_avg(st, STAT_TOPDOWN_SLOTS_ISSUED, ctx, cpu) -
677*4882a593Smuzhiyun 		runtime_stat_avg(st, STAT_TOPDOWN_SLOTS_RETIRED, ctx, cpu) +
678*4882a593Smuzhiyun 		runtime_stat_avg(st, STAT_TOPDOWN_RECOVERY_BUBBLES, ctx, cpu);
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun 	total_slots = td_total_slots(ctx, cpu, st);
681*4882a593Smuzhiyun 	if (total_slots)
682*4882a593Smuzhiyun 		bad_spec = total / total_slots;
683*4882a593Smuzhiyun 	return sanitize_val(bad_spec);
684*4882a593Smuzhiyun }
685*4882a593Smuzhiyun 
td_retiring(int ctx,int cpu,struct runtime_stat * st)686*4882a593Smuzhiyun static double td_retiring(int ctx, int cpu, struct runtime_stat *st)
687*4882a593Smuzhiyun {
688*4882a593Smuzhiyun 	double retiring = 0;
689*4882a593Smuzhiyun 	double total_slots = td_total_slots(ctx, cpu, st);
690*4882a593Smuzhiyun 	double ret_slots = runtime_stat_avg(st, STAT_TOPDOWN_SLOTS_RETIRED,
691*4882a593Smuzhiyun 					    ctx, cpu);
692*4882a593Smuzhiyun 
693*4882a593Smuzhiyun 	if (total_slots)
694*4882a593Smuzhiyun 		retiring = ret_slots / total_slots;
695*4882a593Smuzhiyun 	return retiring;
696*4882a593Smuzhiyun }
697*4882a593Smuzhiyun 
td_fe_bound(int ctx,int cpu,struct runtime_stat * st)698*4882a593Smuzhiyun static double td_fe_bound(int ctx, int cpu, struct runtime_stat *st)
699*4882a593Smuzhiyun {
700*4882a593Smuzhiyun 	double fe_bound = 0;
701*4882a593Smuzhiyun 	double total_slots = td_total_slots(ctx, cpu, st);
702*4882a593Smuzhiyun 	double fetch_bub = runtime_stat_avg(st, STAT_TOPDOWN_FETCH_BUBBLES,
703*4882a593Smuzhiyun 					    ctx, cpu);
704*4882a593Smuzhiyun 
705*4882a593Smuzhiyun 	if (total_slots)
706*4882a593Smuzhiyun 		fe_bound = fetch_bub / total_slots;
707*4882a593Smuzhiyun 	return fe_bound;
708*4882a593Smuzhiyun }
709*4882a593Smuzhiyun 
td_be_bound(int ctx,int cpu,struct runtime_stat * st)710*4882a593Smuzhiyun static double td_be_bound(int ctx, int cpu, struct runtime_stat *st)
711*4882a593Smuzhiyun {
712*4882a593Smuzhiyun 	double sum = (td_fe_bound(ctx, cpu, st) +
713*4882a593Smuzhiyun 		      td_bad_spec(ctx, cpu, st) +
714*4882a593Smuzhiyun 		      td_retiring(ctx, cpu, st));
715*4882a593Smuzhiyun 	if (sum == 0)
716*4882a593Smuzhiyun 		return 0;
717*4882a593Smuzhiyun 	return sanitize_val(1.0 - sum);
718*4882a593Smuzhiyun }
719*4882a593Smuzhiyun 
720*4882a593Smuzhiyun /*
721*4882a593Smuzhiyun  * Kernel reports metrics multiplied with slots. To get back
722*4882a593Smuzhiyun  * the ratios we need to recreate the sum.
723*4882a593Smuzhiyun  */
724*4882a593Smuzhiyun 
td_metric_ratio(int ctx,int cpu,enum stat_type type,struct runtime_stat * stat)725*4882a593Smuzhiyun static double td_metric_ratio(int ctx, int cpu,
726*4882a593Smuzhiyun 			      enum stat_type type,
727*4882a593Smuzhiyun 			      struct runtime_stat *stat)
728*4882a593Smuzhiyun {
729*4882a593Smuzhiyun 	double sum = runtime_stat_avg(stat, STAT_TOPDOWN_RETIRING, ctx, cpu) +
730*4882a593Smuzhiyun 		runtime_stat_avg(stat, STAT_TOPDOWN_FE_BOUND, ctx, cpu) +
731*4882a593Smuzhiyun 		runtime_stat_avg(stat, STAT_TOPDOWN_BE_BOUND, ctx, cpu) +
732*4882a593Smuzhiyun 		runtime_stat_avg(stat, STAT_TOPDOWN_BAD_SPEC, ctx, cpu);
733*4882a593Smuzhiyun 	double d = runtime_stat_avg(stat, type, ctx, cpu);
734*4882a593Smuzhiyun 
735*4882a593Smuzhiyun 	if (sum)
736*4882a593Smuzhiyun 		return d / sum;
737*4882a593Smuzhiyun 	return 0;
738*4882a593Smuzhiyun }
739*4882a593Smuzhiyun 
740*4882a593Smuzhiyun /*
741*4882a593Smuzhiyun  * ... but only if most of the values are actually available.
742*4882a593Smuzhiyun  * We allow two missing.
743*4882a593Smuzhiyun  */
744*4882a593Smuzhiyun 
full_td(int ctx,int cpu,struct runtime_stat * stat)745*4882a593Smuzhiyun static bool full_td(int ctx, int cpu,
746*4882a593Smuzhiyun 		    struct runtime_stat *stat)
747*4882a593Smuzhiyun {
748*4882a593Smuzhiyun 	int c = 0;
749*4882a593Smuzhiyun 
750*4882a593Smuzhiyun 	if (runtime_stat_avg(stat, STAT_TOPDOWN_RETIRING, ctx, cpu) > 0)
751*4882a593Smuzhiyun 		c++;
752*4882a593Smuzhiyun 	if (runtime_stat_avg(stat, STAT_TOPDOWN_BE_BOUND, ctx, cpu) > 0)
753*4882a593Smuzhiyun 		c++;
754*4882a593Smuzhiyun 	if (runtime_stat_avg(stat, STAT_TOPDOWN_FE_BOUND, ctx, cpu) > 0)
755*4882a593Smuzhiyun 		c++;
756*4882a593Smuzhiyun 	if (runtime_stat_avg(stat, STAT_TOPDOWN_BAD_SPEC, ctx, cpu) > 0)
757*4882a593Smuzhiyun 		c++;
758*4882a593Smuzhiyun 	return c >= 2;
759*4882a593Smuzhiyun }
760*4882a593Smuzhiyun 
print_smi_cost(struct perf_stat_config * config,int cpu,struct evsel * evsel,struct perf_stat_output_ctx * out,struct runtime_stat * st)761*4882a593Smuzhiyun static void print_smi_cost(struct perf_stat_config *config,
762*4882a593Smuzhiyun 			   int cpu, struct evsel *evsel,
763*4882a593Smuzhiyun 			   struct perf_stat_output_ctx *out,
764*4882a593Smuzhiyun 			   struct runtime_stat *st)
765*4882a593Smuzhiyun {
766*4882a593Smuzhiyun 	double smi_num, aperf, cycles, cost = 0.0;
767*4882a593Smuzhiyun 	int ctx = evsel_context(evsel);
768*4882a593Smuzhiyun 	const char *color = NULL;
769*4882a593Smuzhiyun 
770*4882a593Smuzhiyun 	smi_num = runtime_stat_avg(st, STAT_SMI_NUM, ctx, cpu);
771*4882a593Smuzhiyun 	aperf = runtime_stat_avg(st, STAT_APERF, ctx, cpu);
772*4882a593Smuzhiyun 	cycles = runtime_stat_avg(st, STAT_CYCLES, ctx, cpu);
773*4882a593Smuzhiyun 
774*4882a593Smuzhiyun 	if ((cycles == 0) || (aperf == 0))
775*4882a593Smuzhiyun 		return;
776*4882a593Smuzhiyun 
777*4882a593Smuzhiyun 	if (smi_num)
778*4882a593Smuzhiyun 		cost = (aperf - cycles) / aperf * 100.00;
779*4882a593Smuzhiyun 
780*4882a593Smuzhiyun 	if (cost > 10)
781*4882a593Smuzhiyun 		color = PERF_COLOR_RED;
782*4882a593Smuzhiyun 	out->print_metric(config, out->ctx, color, "%8.1f%%", "SMI cycles%", cost);
783*4882a593Smuzhiyun 	out->print_metric(config, out->ctx, NULL, "%4.0f", "SMI#", smi_num);
784*4882a593Smuzhiyun }
785*4882a593Smuzhiyun 
prepare_metric(struct evsel ** metric_events,struct metric_ref * metric_refs,struct expr_parse_ctx * pctx,int cpu,struct runtime_stat * st)786*4882a593Smuzhiyun static int prepare_metric(struct evsel **metric_events,
787*4882a593Smuzhiyun 			  struct metric_ref *metric_refs,
788*4882a593Smuzhiyun 			  struct expr_parse_ctx *pctx,
789*4882a593Smuzhiyun 			  int cpu,
790*4882a593Smuzhiyun 			  struct runtime_stat *st)
791*4882a593Smuzhiyun {
792*4882a593Smuzhiyun 	double scale;
793*4882a593Smuzhiyun 	char *n, *pn;
794*4882a593Smuzhiyun 	int i, j, ret;
795*4882a593Smuzhiyun 
796*4882a593Smuzhiyun 	expr__ctx_init(pctx);
797*4882a593Smuzhiyun 	for (i = 0; metric_events[i]; i++) {
798*4882a593Smuzhiyun 		struct saved_value *v;
799*4882a593Smuzhiyun 		struct stats *stats;
800*4882a593Smuzhiyun 		u64 metric_total = 0;
801*4882a593Smuzhiyun 
802*4882a593Smuzhiyun 		if (!strcmp(metric_events[i]->name, "duration_time")) {
803*4882a593Smuzhiyun 			stats = &walltime_nsecs_stats;
804*4882a593Smuzhiyun 			scale = 1e-9;
805*4882a593Smuzhiyun 		} else {
806*4882a593Smuzhiyun 			v = saved_value_lookup(metric_events[i], cpu, false,
807*4882a593Smuzhiyun 					       STAT_NONE, 0, st);
808*4882a593Smuzhiyun 			if (!v)
809*4882a593Smuzhiyun 				break;
810*4882a593Smuzhiyun 			stats = &v->stats;
811*4882a593Smuzhiyun 			scale = 1.0;
812*4882a593Smuzhiyun 
813*4882a593Smuzhiyun 			if (v->metric_other)
814*4882a593Smuzhiyun 				metric_total = v->metric_total;
815*4882a593Smuzhiyun 		}
816*4882a593Smuzhiyun 
817*4882a593Smuzhiyun 		n = strdup(metric_events[i]->name);
818*4882a593Smuzhiyun 		if (!n)
819*4882a593Smuzhiyun 			return -ENOMEM;
820*4882a593Smuzhiyun 		/*
821*4882a593Smuzhiyun 		 * This display code with --no-merge adds [cpu] postfixes.
822*4882a593Smuzhiyun 		 * These are not supported by the parser. Remove everything
823*4882a593Smuzhiyun 		 * after the space.
824*4882a593Smuzhiyun 		 */
825*4882a593Smuzhiyun 		pn = strchr(n, ' ');
826*4882a593Smuzhiyun 		if (pn)
827*4882a593Smuzhiyun 			*pn = 0;
828*4882a593Smuzhiyun 
829*4882a593Smuzhiyun 		if (metric_total)
830*4882a593Smuzhiyun 			expr__add_id_val(pctx, n, metric_total);
831*4882a593Smuzhiyun 		else
832*4882a593Smuzhiyun 			expr__add_id_val(pctx, n, avg_stats(stats)*scale);
833*4882a593Smuzhiyun 	}
834*4882a593Smuzhiyun 
835*4882a593Smuzhiyun 	for (j = 0; metric_refs && metric_refs[j].metric_name; j++) {
836*4882a593Smuzhiyun 		ret = expr__add_ref(pctx, &metric_refs[j]);
837*4882a593Smuzhiyun 		if (ret)
838*4882a593Smuzhiyun 			return ret;
839*4882a593Smuzhiyun 	}
840*4882a593Smuzhiyun 
841*4882a593Smuzhiyun 	return i;
842*4882a593Smuzhiyun }
843*4882a593Smuzhiyun 
generic_metric(struct perf_stat_config * config,const char * metric_expr,struct evsel ** metric_events,struct metric_ref * metric_refs,char * name,const char * metric_name,const char * metric_unit,int runtime,int cpu,struct perf_stat_output_ctx * out,struct runtime_stat * st)844*4882a593Smuzhiyun static void generic_metric(struct perf_stat_config *config,
845*4882a593Smuzhiyun 			   const char *metric_expr,
846*4882a593Smuzhiyun 			   struct evsel **metric_events,
847*4882a593Smuzhiyun 			   struct metric_ref *metric_refs,
848*4882a593Smuzhiyun 			   char *name,
849*4882a593Smuzhiyun 			   const char *metric_name,
850*4882a593Smuzhiyun 			   const char *metric_unit,
851*4882a593Smuzhiyun 			   int runtime,
852*4882a593Smuzhiyun 			   int cpu,
853*4882a593Smuzhiyun 			   struct perf_stat_output_ctx *out,
854*4882a593Smuzhiyun 			   struct runtime_stat *st)
855*4882a593Smuzhiyun {
856*4882a593Smuzhiyun 	print_metric_t print_metric = out->print_metric;
857*4882a593Smuzhiyun 	struct expr_parse_ctx pctx;
858*4882a593Smuzhiyun 	double ratio, scale;
859*4882a593Smuzhiyun 	int i;
860*4882a593Smuzhiyun 	void *ctxp = out->ctx;
861*4882a593Smuzhiyun 
862*4882a593Smuzhiyun 	i = prepare_metric(metric_events, metric_refs, &pctx, cpu, st);
863*4882a593Smuzhiyun 	if (i < 0)
864*4882a593Smuzhiyun 		return;
865*4882a593Smuzhiyun 
866*4882a593Smuzhiyun 	if (!metric_events[i]) {
867*4882a593Smuzhiyun 		if (expr__parse(&ratio, &pctx, metric_expr, runtime) == 0) {
868*4882a593Smuzhiyun 			char *unit;
869*4882a593Smuzhiyun 			char metric_bf[64];
870*4882a593Smuzhiyun 
871*4882a593Smuzhiyun 			if (metric_unit && metric_name) {
872*4882a593Smuzhiyun 				if (perf_pmu__convert_scale(metric_unit,
873*4882a593Smuzhiyun 					&unit, &scale) >= 0) {
874*4882a593Smuzhiyun 					ratio *= scale;
875*4882a593Smuzhiyun 				}
876*4882a593Smuzhiyun 				if (strstr(metric_expr, "?"))
877*4882a593Smuzhiyun 					scnprintf(metric_bf, sizeof(metric_bf),
878*4882a593Smuzhiyun 					  "%s  %s_%d", unit, metric_name, runtime);
879*4882a593Smuzhiyun 				else
880*4882a593Smuzhiyun 					scnprintf(metric_bf, sizeof(metric_bf),
881*4882a593Smuzhiyun 					  "%s  %s", unit, metric_name);
882*4882a593Smuzhiyun 
883*4882a593Smuzhiyun 				print_metric(config, ctxp, NULL, "%8.1f",
884*4882a593Smuzhiyun 					     metric_bf, ratio);
885*4882a593Smuzhiyun 			} else {
886*4882a593Smuzhiyun 				print_metric(config, ctxp, NULL, "%8.2f",
887*4882a593Smuzhiyun 					metric_name ?
888*4882a593Smuzhiyun 					metric_name :
889*4882a593Smuzhiyun 					out->force_header ?  name : "",
890*4882a593Smuzhiyun 					ratio);
891*4882a593Smuzhiyun 			}
892*4882a593Smuzhiyun 		} else {
893*4882a593Smuzhiyun 			print_metric(config, ctxp, NULL, NULL,
894*4882a593Smuzhiyun 				     out->force_header ?
895*4882a593Smuzhiyun 				     (metric_name ? metric_name : name) : "", 0);
896*4882a593Smuzhiyun 		}
897*4882a593Smuzhiyun 	} else {
898*4882a593Smuzhiyun 		print_metric(config, ctxp, NULL, NULL,
899*4882a593Smuzhiyun 			     out->force_header ?
900*4882a593Smuzhiyun 			     (metric_name ? metric_name : name) : "", 0);
901*4882a593Smuzhiyun 	}
902*4882a593Smuzhiyun 
903*4882a593Smuzhiyun 	expr__ctx_clear(&pctx);
904*4882a593Smuzhiyun }
905*4882a593Smuzhiyun 
test_generic_metric(struct metric_expr * mexp,int cpu,struct runtime_stat * st)906*4882a593Smuzhiyun double test_generic_metric(struct metric_expr *mexp, int cpu, struct runtime_stat *st)
907*4882a593Smuzhiyun {
908*4882a593Smuzhiyun 	struct expr_parse_ctx pctx;
909*4882a593Smuzhiyun 	double ratio = 0.0;
910*4882a593Smuzhiyun 
911*4882a593Smuzhiyun 	if (prepare_metric(mexp->metric_events, mexp->metric_refs, &pctx, cpu, st) < 0)
912*4882a593Smuzhiyun 		goto out;
913*4882a593Smuzhiyun 
914*4882a593Smuzhiyun 	if (expr__parse(&ratio, &pctx, mexp->metric_expr, 1))
915*4882a593Smuzhiyun 		ratio = 0.0;
916*4882a593Smuzhiyun 
917*4882a593Smuzhiyun out:
918*4882a593Smuzhiyun 	expr__ctx_clear(&pctx);
919*4882a593Smuzhiyun 	return ratio;
920*4882a593Smuzhiyun }
921*4882a593Smuzhiyun 
perf_stat__print_shadow_stats(struct perf_stat_config * config,struct evsel * evsel,double avg,int cpu,struct perf_stat_output_ctx * out,struct rblist * metric_events,struct runtime_stat * st)922*4882a593Smuzhiyun void perf_stat__print_shadow_stats(struct perf_stat_config *config,
923*4882a593Smuzhiyun 				   struct evsel *evsel,
924*4882a593Smuzhiyun 				   double avg, int cpu,
925*4882a593Smuzhiyun 				   struct perf_stat_output_ctx *out,
926*4882a593Smuzhiyun 				   struct rblist *metric_events,
927*4882a593Smuzhiyun 				   struct runtime_stat *st)
928*4882a593Smuzhiyun {
929*4882a593Smuzhiyun 	void *ctxp = out->ctx;
930*4882a593Smuzhiyun 	print_metric_t print_metric = out->print_metric;
931*4882a593Smuzhiyun 	double total, ratio = 0.0, total2;
932*4882a593Smuzhiyun 	const char *color = NULL;
933*4882a593Smuzhiyun 	int ctx = evsel_context(evsel);
934*4882a593Smuzhiyun 	struct metric_event *me;
935*4882a593Smuzhiyun 	int num = 1;
936*4882a593Smuzhiyun 
937*4882a593Smuzhiyun 	if (evsel__match(evsel, HARDWARE, HW_INSTRUCTIONS)) {
938*4882a593Smuzhiyun 		total = runtime_stat_avg(st, STAT_CYCLES, ctx, cpu);
939*4882a593Smuzhiyun 
940*4882a593Smuzhiyun 		if (total) {
941*4882a593Smuzhiyun 			ratio = avg / total;
942*4882a593Smuzhiyun 			print_metric(config, ctxp, NULL, "%7.2f ",
943*4882a593Smuzhiyun 					"insn per cycle", ratio);
944*4882a593Smuzhiyun 		} else {
945*4882a593Smuzhiyun 			print_metric(config, ctxp, NULL, NULL, "insn per cycle", 0);
946*4882a593Smuzhiyun 		}
947*4882a593Smuzhiyun 
948*4882a593Smuzhiyun 		total = runtime_stat_avg(st, STAT_STALLED_CYCLES_FRONT,
949*4882a593Smuzhiyun 					 ctx, cpu);
950*4882a593Smuzhiyun 
951*4882a593Smuzhiyun 		total = max(total, runtime_stat_avg(st,
952*4882a593Smuzhiyun 						    STAT_STALLED_CYCLES_BACK,
953*4882a593Smuzhiyun 						    ctx, cpu));
954*4882a593Smuzhiyun 
955*4882a593Smuzhiyun 		if (total && avg) {
956*4882a593Smuzhiyun 			out->new_line(config, ctxp);
957*4882a593Smuzhiyun 			ratio = total / avg;
958*4882a593Smuzhiyun 			print_metric(config, ctxp, NULL, "%7.2f ",
959*4882a593Smuzhiyun 					"stalled cycles per insn",
960*4882a593Smuzhiyun 					ratio);
961*4882a593Smuzhiyun 		}
962*4882a593Smuzhiyun 	} else if (evsel__match(evsel, HARDWARE, HW_BRANCH_MISSES)) {
963*4882a593Smuzhiyun 		if (runtime_stat_n(st, STAT_BRANCHES, ctx, cpu) != 0)
964*4882a593Smuzhiyun 			print_branch_misses(config, cpu, evsel, avg, out, st);
965*4882a593Smuzhiyun 		else
966*4882a593Smuzhiyun 			print_metric(config, ctxp, NULL, NULL, "of all branches", 0);
967*4882a593Smuzhiyun 	} else if (
968*4882a593Smuzhiyun 		evsel->core.attr.type == PERF_TYPE_HW_CACHE &&
969*4882a593Smuzhiyun 		evsel->core.attr.config ==  ( PERF_COUNT_HW_CACHE_L1D |
970*4882a593Smuzhiyun 					((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
971*4882a593Smuzhiyun 					 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
972*4882a593Smuzhiyun 
973*4882a593Smuzhiyun 		if (runtime_stat_n(st, STAT_L1_DCACHE, ctx, cpu) != 0)
974*4882a593Smuzhiyun 			print_l1_dcache_misses(config, cpu, evsel, avg, out, st);
975*4882a593Smuzhiyun 		else
976*4882a593Smuzhiyun 			print_metric(config, ctxp, NULL, NULL, "of all L1-dcache accesses", 0);
977*4882a593Smuzhiyun 	} else if (
978*4882a593Smuzhiyun 		evsel->core.attr.type == PERF_TYPE_HW_CACHE &&
979*4882a593Smuzhiyun 		evsel->core.attr.config ==  ( PERF_COUNT_HW_CACHE_L1I |
980*4882a593Smuzhiyun 					((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
981*4882a593Smuzhiyun 					 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
982*4882a593Smuzhiyun 
983*4882a593Smuzhiyun 		if (runtime_stat_n(st, STAT_L1_ICACHE, ctx, cpu) != 0)
984*4882a593Smuzhiyun 			print_l1_icache_misses(config, cpu, evsel, avg, out, st);
985*4882a593Smuzhiyun 		else
986*4882a593Smuzhiyun 			print_metric(config, ctxp, NULL, NULL, "of all L1-icache accesses", 0);
987*4882a593Smuzhiyun 	} else if (
988*4882a593Smuzhiyun 		evsel->core.attr.type == PERF_TYPE_HW_CACHE &&
989*4882a593Smuzhiyun 		evsel->core.attr.config ==  ( PERF_COUNT_HW_CACHE_DTLB |
990*4882a593Smuzhiyun 					((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
991*4882a593Smuzhiyun 					 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
992*4882a593Smuzhiyun 
993*4882a593Smuzhiyun 		if (runtime_stat_n(st, STAT_DTLB_CACHE, ctx, cpu) != 0)
994*4882a593Smuzhiyun 			print_dtlb_cache_misses(config, cpu, evsel, avg, out, st);
995*4882a593Smuzhiyun 		else
996*4882a593Smuzhiyun 			print_metric(config, ctxp, NULL, NULL, "of all dTLB cache accesses", 0);
997*4882a593Smuzhiyun 	} else if (
998*4882a593Smuzhiyun 		evsel->core.attr.type == PERF_TYPE_HW_CACHE &&
999*4882a593Smuzhiyun 		evsel->core.attr.config ==  ( PERF_COUNT_HW_CACHE_ITLB |
1000*4882a593Smuzhiyun 					((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
1001*4882a593Smuzhiyun 					 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
1002*4882a593Smuzhiyun 
1003*4882a593Smuzhiyun 		if (runtime_stat_n(st, STAT_ITLB_CACHE, ctx, cpu) != 0)
1004*4882a593Smuzhiyun 			print_itlb_cache_misses(config, cpu, evsel, avg, out, st);
1005*4882a593Smuzhiyun 		else
1006*4882a593Smuzhiyun 			print_metric(config, ctxp, NULL, NULL, "of all iTLB cache accesses", 0);
1007*4882a593Smuzhiyun 	} else if (
1008*4882a593Smuzhiyun 		evsel->core.attr.type == PERF_TYPE_HW_CACHE &&
1009*4882a593Smuzhiyun 		evsel->core.attr.config ==  ( PERF_COUNT_HW_CACHE_LL |
1010*4882a593Smuzhiyun 					((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
1011*4882a593Smuzhiyun 					 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
1012*4882a593Smuzhiyun 
1013*4882a593Smuzhiyun 		if (runtime_stat_n(st, STAT_LL_CACHE, ctx, cpu) != 0)
1014*4882a593Smuzhiyun 			print_ll_cache_misses(config, cpu, evsel, avg, out, st);
1015*4882a593Smuzhiyun 		else
1016*4882a593Smuzhiyun 			print_metric(config, ctxp, NULL, NULL, "of all LL-cache accesses", 0);
1017*4882a593Smuzhiyun 	} else if (evsel__match(evsel, HARDWARE, HW_CACHE_MISSES)) {
1018*4882a593Smuzhiyun 		total = runtime_stat_avg(st, STAT_CACHEREFS, ctx, cpu);
1019*4882a593Smuzhiyun 
1020*4882a593Smuzhiyun 		if (total)
1021*4882a593Smuzhiyun 			ratio = avg * 100 / total;
1022*4882a593Smuzhiyun 
1023*4882a593Smuzhiyun 		if (runtime_stat_n(st, STAT_CACHEREFS, ctx, cpu) != 0)
1024*4882a593Smuzhiyun 			print_metric(config, ctxp, NULL, "%8.3f %%",
1025*4882a593Smuzhiyun 				     "of all cache refs", ratio);
1026*4882a593Smuzhiyun 		else
1027*4882a593Smuzhiyun 			print_metric(config, ctxp, NULL, NULL, "of all cache refs", 0);
1028*4882a593Smuzhiyun 	} else if (evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES_FRONTEND)) {
1029*4882a593Smuzhiyun 		print_stalled_cycles_frontend(config, cpu, evsel, avg, out, st);
1030*4882a593Smuzhiyun 	} else if (evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES_BACKEND)) {
1031*4882a593Smuzhiyun 		print_stalled_cycles_backend(config, cpu, evsel, avg, out, st);
1032*4882a593Smuzhiyun 	} else if (evsel__match(evsel, HARDWARE, HW_CPU_CYCLES)) {
1033*4882a593Smuzhiyun 		total = runtime_stat_avg(st, STAT_NSECS, 0, cpu);
1034*4882a593Smuzhiyun 
1035*4882a593Smuzhiyun 		if (total) {
1036*4882a593Smuzhiyun 			ratio = avg / total;
1037*4882a593Smuzhiyun 			print_metric(config, ctxp, NULL, "%8.3f", "GHz", ratio);
1038*4882a593Smuzhiyun 		} else {
1039*4882a593Smuzhiyun 			print_metric(config, ctxp, NULL, NULL, "Ghz", 0);
1040*4882a593Smuzhiyun 		}
1041*4882a593Smuzhiyun 	} else if (perf_stat_evsel__is(evsel, CYCLES_IN_TX)) {
1042*4882a593Smuzhiyun 		total = runtime_stat_avg(st, STAT_CYCLES, ctx, cpu);
1043*4882a593Smuzhiyun 
1044*4882a593Smuzhiyun 		if (total)
1045*4882a593Smuzhiyun 			print_metric(config, ctxp, NULL,
1046*4882a593Smuzhiyun 					"%7.2f%%", "transactional cycles",
1047*4882a593Smuzhiyun 					100.0 * (avg / total));
1048*4882a593Smuzhiyun 		else
1049*4882a593Smuzhiyun 			print_metric(config, ctxp, NULL, NULL, "transactional cycles",
1050*4882a593Smuzhiyun 				     0);
1051*4882a593Smuzhiyun 	} else if (perf_stat_evsel__is(evsel, CYCLES_IN_TX_CP)) {
1052*4882a593Smuzhiyun 		total = runtime_stat_avg(st, STAT_CYCLES, ctx, cpu);
1053*4882a593Smuzhiyun 		total2 = runtime_stat_avg(st, STAT_CYCLES_IN_TX, ctx, cpu);
1054*4882a593Smuzhiyun 
1055*4882a593Smuzhiyun 		if (total2 < avg)
1056*4882a593Smuzhiyun 			total2 = avg;
1057*4882a593Smuzhiyun 		if (total)
1058*4882a593Smuzhiyun 			print_metric(config, ctxp, NULL, "%7.2f%%", "aborted cycles",
1059*4882a593Smuzhiyun 				100.0 * ((total2-avg) / total));
1060*4882a593Smuzhiyun 		else
1061*4882a593Smuzhiyun 			print_metric(config, ctxp, NULL, NULL, "aborted cycles", 0);
1062*4882a593Smuzhiyun 	} else if (perf_stat_evsel__is(evsel, TRANSACTION_START)) {
1063*4882a593Smuzhiyun 		total = runtime_stat_avg(st, STAT_CYCLES_IN_TX,
1064*4882a593Smuzhiyun 					 ctx, cpu);
1065*4882a593Smuzhiyun 
1066*4882a593Smuzhiyun 		if (avg)
1067*4882a593Smuzhiyun 			ratio = total / avg;
1068*4882a593Smuzhiyun 
1069*4882a593Smuzhiyun 		if (runtime_stat_n(st, STAT_CYCLES_IN_TX, ctx, cpu) != 0)
1070*4882a593Smuzhiyun 			print_metric(config, ctxp, NULL, "%8.0f",
1071*4882a593Smuzhiyun 				     "cycles / transaction", ratio);
1072*4882a593Smuzhiyun 		else
1073*4882a593Smuzhiyun 			print_metric(config, ctxp, NULL, NULL, "cycles / transaction",
1074*4882a593Smuzhiyun 				      0);
1075*4882a593Smuzhiyun 	} else if (perf_stat_evsel__is(evsel, ELISION_START)) {
1076*4882a593Smuzhiyun 		total = runtime_stat_avg(st, STAT_CYCLES_IN_TX,
1077*4882a593Smuzhiyun 					 ctx, cpu);
1078*4882a593Smuzhiyun 
1079*4882a593Smuzhiyun 		if (avg)
1080*4882a593Smuzhiyun 			ratio = total / avg;
1081*4882a593Smuzhiyun 
1082*4882a593Smuzhiyun 		print_metric(config, ctxp, NULL, "%8.0f", "cycles / elision", ratio);
1083*4882a593Smuzhiyun 	} else if (evsel__is_clock(evsel)) {
1084*4882a593Smuzhiyun 		if ((ratio = avg_stats(&walltime_nsecs_stats)) != 0)
1085*4882a593Smuzhiyun 			print_metric(config, ctxp, NULL, "%8.3f", "CPUs utilized",
1086*4882a593Smuzhiyun 				     avg / (ratio * evsel->scale));
1087*4882a593Smuzhiyun 		else
1088*4882a593Smuzhiyun 			print_metric(config, ctxp, NULL, NULL, "CPUs utilized", 0);
1089*4882a593Smuzhiyun 	} else if (perf_stat_evsel__is(evsel, TOPDOWN_FETCH_BUBBLES)) {
1090*4882a593Smuzhiyun 		double fe_bound = td_fe_bound(ctx, cpu, st);
1091*4882a593Smuzhiyun 
1092*4882a593Smuzhiyun 		if (fe_bound > 0.2)
1093*4882a593Smuzhiyun 			color = PERF_COLOR_RED;
1094*4882a593Smuzhiyun 		print_metric(config, ctxp, color, "%8.1f%%", "frontend bound",
1095*4882a593Smuzhiyun 				fe_bound * 100.);
1096*4882a593Smuzhiyun 	} else if (perf_stat_evsel__is(evsel, TOPDOWN_SLOTS_RETIRED)) {
1097*4882a593Smuzhiyun 		double retiring = td_retiring(ctx, cpu, st);
1098*4882a593Smuzhiyun 
1099*4882a593Smuzhiyun 		if (retiring > 0.7)
1100*4882a593Smuzhiyun 			color = PERF_COLOR_GREEN;
1101*4882a593Smuzhiyun 		print_metric(config, ctxp, color, "%8.1f%%", "retiring",
1102*4882a593Smuzhiyun 				retiring * 100.);
1103*4882a593Smuzhiyun 	} else if (perf_stat_evsel__is(evsel, TOPDOWN_RECOVERY_BUBBLES)) {
1104*4882a593Smuzhiyun 		double bad_spec = td_bad_spec(ctx, cpu, st);
1105*4882a593Smuzhiyun 
1106*4882a593Smuzhiyun 		if (bad_spec > 0.1)
1107*4882a593Smuzhiyun 			color = PERF_COLOR_RED;
1108*4882a593Smuzhiyun 		print_metric(config, ctxp, color, "%8.1f%%", "bad speculation",
1109*4882a593Smuzhiyun 				bad_spec * 100.);
1110*4882a593Smuzhiyun 	} else if (perf_stat_evsel__is(evsel, TOPDOWN_SLOTS_ISSUED)) {
1111*4882a593Smuzhiyun 		double be_bound = td_be_bound(ctx, cpu, st);
1112*4882a593Smuzhiyun 		const char *name = "backend bound";
1113*4882a593Smuzhiyun 		static int have_recovery_bubbles = -1;
1114*4882a593Smuzhiyun 
1115*4882a593Smuzhiyun 		/* In case the CPU does not support topdown-recovery-bubbles */
1116*4882a593Smuzhiyun 		if (have_recovery_bubbles < 0)
1117*4882a593Smuzhiyun 			have_recovery_bubbles = pmu_have_event("cpu",
1118*4882a593Smuzhiyun 					"topdown-recovery-bubbles");
1119*4882a593Smuzhiyun 		if (!have_recovery_bubbles)
1120*4882a593Smuzhiyun 			name = "backend bound/bad spec";
1121*4882a593Smuzhiyun 
1122*4882a593Smuzhiyun 		if (be_bound > 0.2)
1123*4882a593Smuzhiyun 			color = PERF_COLOR_RED;
1124*4882a593Smuzhiyun 		if (td_total_slots(ctx, cpu, st) > 0)
1125*4882a593Smuzhiyun 			print_metric(config, ctxp, color, "%8.1f%%", name,
1126*4882a593Smuzhiyun 					be_bound * 100.);
1127*4882a593Smuzhiyun 		else
1128*4882a593Smuzhiyun 			print_metric(config, ctxp, NULL, NULL, name, 0);
1129*4882a593Smuzhiyun 	} else if (perf_stat_evsel__is(evsel, TOPDOWN_RETIRING) &&
1130*4882a593Smuzhiyun 			full_td(ctx, cpu, st)) {
1131*4882a593Smuzhiyun 		double retiring = td_metric_ratio(ctx, cpu,
1132*4882a593Smuzhiyun 						  STAT_TOPDOWN_RETIRING, st);
1133*4882a593Smuzhiyun 
1134*4882a593Smuzhiyun 		if (retiring > 0.7)
1135*4882a593Smuzhiyun 			color = PERF_COLOR_GREEN;
1136*4882a593Smuzhiyun 		print_metric(config, ctxp, color, "%8.1f%%", "retiring",
1137*4882a593Smuzhiyun 				retiring * 100.);
1138*4882a593Smuzhiyun 	} else if (perf_stat_evsel__is(evsel, TOPDOWN_FE_BOUND) &&
1139*4882a593Smuzhiyun 			full_td(ctx, cpu, st)) {
1140*4882a593Smuzhiyun 		double fe_bound = td_metric_ratio(ctx, cpu,
1141*4882a593Smuzhiyun 						  STAT_TOPDOWN_FE_BOUND, st);
1142*4882a593Smuzhiyun 
1143*4882a593Smuzhiyun 		if (fe_bound > 0.2)
1144*4882a593Smuzhiyun 			color = PERF_COLOR_RED;
1145*4882a593Smuzhiyun 		print_metric(config, ctxp, color, "%8.1f%%", "frontend bound",
1146*4882a593Smuzhiyun 				fe_bound * 100.);
1147*4882a593Smuzhiyun 	} else if (perf_stat_evsel__is(evsel, TOPDOWN_BE_BOUND) &&
1148*4882a593Smuzhiyun 			full_td(ctx, cpu, st)) {
1149*4882a593Smuzhiyun 		double be_bound = td_metric_ratio(ctx, cpu,
1150*4882a593Smuzhiyun 						  STAT_TOPDOWN_BE_BOUND, st);
1151*4882a593Smuzhiyun 
1152*4882a593Smuzhiyun 		if (be_bound > 0.2)
1153*4882a593Smuzhiyun 			color = PERF_COLOR_RED;
1154*4882a593Smuzhiyun 		print_metric(config, ctxp, color, "%8.1f%%", "backend bound",
1155*4882a593Smuzhiyun 				be_bound * 100.);
1156*4882a593Smuzhiyun 	} else if (perf_stat_evsel__is(evsel, TOPDOWN_BAD_SPEC) &&
1157*4882a593Smuzhiyun 			full_td(ctx, cpu, st)) {
1158*4882a593Smuzhiyun 		double bad_spec = td_metric_ratio(ctx, cpu,
1159*4882a593Smuzhiyun 						  STAT_TOPDOWN_BAD_SPEC, st);
1160*4882a593Smuzhiyun 
1161*4882a593Smuzhiyun 		if (bad_spec > 0.1)
1162*4882a593Smuzhiyun 			color = PERF_COLOR_RED;
1163*4882a593Smuzhiyun 		print_metric(config, ctxp, color, "%8.1f%%", "bad speculation",
1164*4882a593Smuzhiyun 				bad_spec * 100.);
1165*4882a593Smuzhiyun 	} else if (evsel->metric_expr) {
1166*4882a593Smuzhiyun 		generic_metric(config, evsel->metric_expr, evsel->metric_events, NULL,
1167*4882a593Smuzhiyun 				evsel->name, evsel->metric_name, NULL, 1, cpu, out, st);
1168*4882a593Smuzhiyun 	} else if (runtime_stat_n(st, STAT_NSECS, 0, cpu) != 0) {
1169*4882a593Smuzhiyun 		char unit = 'M';
1170*4882a593Smuzhiyun 		char unit_buf[10];
1171*4882a593Smuzhiyun 
1172*4882a593Smuzhiyun 		total = runtime_stat_avg(st, STAT_NSECS, 0, cpu);
1173*4882a593Smuzhiyun 
1174*4882a593Smuzhiyun 		if (total)
1175*4882a593Smuzhiyun 			ratio = 1000.0 * avg / total;
1176*4882a593Smuzhiyun 		if (ratio < 0.001) {
1177*4882a593Smuzhiyun 			ratio *= 1000;
1178*4882a593Smuzhiyun 			unit = 'K';
1179*4882a593Smuzhiyun 		}
1180*4882a593Smuzhiyun 		snprintf(unit_buf, sizeof(unit_buf), "%c/sec", unit);
1181*4882a593Smuzhiyun 		print_metric(config, ctxp, NULL, "%8.3f", unit_buf, ratio);
1182*4882a593Smuzhiyun 	} else if (perf_stat_evsel__is(evsel, SMI_NUM)) {
1183*4882a593Smuzhiyun 		print_smi_cost(config, cpu, evsel, out, st);
1184*4882a593Smuzhiyun 	} else {
1185*4882a593Smuzhiyun 		num = 0;
1186*4882a593Smuzhiyun 	}
1187*4882a593Smuzhiyun 
1188*4882a593Smuzhiyun 	if ((me = metricgroup__lookup(metric_events, evsel, false)) != NULL) {
1189*4882a593Smuzhiyun 		struct metric_expr *mexp;
1190*4882a593Smuzhiyun 
1191*4882a593Smuzhiyun 		list_for_each_entry (mexp, &me->head, nd) {
1192*4882a593Smuzhiyun 			if (num++ > 0)
1193*4882a593Smuzhiyun 				out->new_line(config, ctxp);
1194*4882a593Smuzhiyun 			generic_metric(config, mexp->metric_expr, mexp->metric_events,
1195*4882a593Smuzhiyun 					mexp->metric_refs, evsel->name, mexp->metric_name,
1196*4882a593Smuzhiyun 					mexp->metric_unit, mexp->runtime, cpu, out, st);
1197*4882a593Smuzhiyun 		}
1198*4882a593Smuzhiyun 	}
1199*4882a593Smuzhiyun 	if (num == 0)
1200*4882a593Smuzhiyun 		print_metric(config, ctxp, NULL, NULL, NULL, 0);
1201*4882a593Smuzhiyun }
1202