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