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