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