blob: 51ad03a799ec9b977656986b5de4e776561b416a [file] [log] [blame]
Jiri Olsaf87027b2015-06-03 16:25:59 +02001#include <stdio.h>
2#include "evsel.h"
3#include "stat.h"
4#include "color.h"
Andi Kleenfb4605b2016-03-01 10:57:52 -08005#include "pmu.h"
Andi Kleen37932c12017-03-20 13:17:08 -07006#include "rblist.h"
7#include "evlist.h"
8#include "expr.h"
Andi Kleenb18f3e32017-08-31 12:40:31 -07009#include "metricgroup.h"
Jiri Olsaf87027b2015-06-03 16:25:59 +020010
11enum {
12 CTX_BIT_USER = 1 << 0,
13 CTX_BIT_KERNEL = 1 << 1,
14 CTX_BIT_HV = 1 << 2,
15 CTX_BIT_HOST = 1 << 3,
16 CTX_BIT_IDLE = 1 << 4,
17 CTX_BIT_MAX = 1 << 5,
18};
19
20#define NUM_CTX CTX_BIT_MAX
21
Andi Kleen44d49a62016-02-29 14:36:22 -080022/*
23 * AGGR_GLOBAL: Use CPU 0
24 * AGGR_SOCKET: Use first CPU of socket
25 * AGGR_CORE: Use first CPU of core
26 * AGGR_NONE: Use matching CPU
27 * AGGR_THREAD: Not supported?
28 */
Jiri Olsaf87027b2015-06-03 16:25:59 +020029static struct stats runtime_nsecs_stats[MAX_NR_CPUS];
30static struct stats runtime_cycles_stats[NUM_CTX][MAX_NR_CPUS];
31static struct stats runtime_stalled_cycles_front_stats[NUM_CTX][MAX_NR_CPUS];
32static struct stats runtime_stalled_cycles_back_stats[NUM_CTX][MAX_NR_CPUS];
33static struct stats runtime_branches_stats[NUM_CTX][MAX_NR_CPUS];
34static struct stats runtime_cacherefs_stats[NUM_CTX][MAX_NR_CPUS];
35static struct stats runtime_l1_dcache_stats[NUM_CTX][MAX_NR_CPUS];
36static struct stats runtime_l1_icache_stats[NUM_CTX][MAX_NR_CPUS];
37static struct stats runtime_ll_cache_stats[NUM_CTX][MAX_NR_CPUS];
38static struct stats runtime_itlb_cache_stats[NUM_CTX][MAX_NR_CPUS];
39static struct stats runtime_dtlb_cache_stats[NUM_CTX][MAX_NR_CPUS];
40static struct stats runtime_cycles_in_tx_stats[NUM_CTX][MAX_NR_CPUS];
41static struct stats runtime_transaction_stats[NUM_CTX][MAX_NR_CPUS];
42static struct stats runtime_elision_stats[NUM_CTX][MAX_NR_CPUS];
Andi Kleen239bd472016-05-24 12:52:37 -070043static struct stats runtime_topdown_total_slots[NUM_CTX][MAX_NR_CPUS];
44static struct stats runtime_topdown_slots_issued[NUM_CTX][MAX_NR_CPUS];
45static struct stats runtime_topdown_slots_retired[NUM_CTX][MAX_NR_CPUS];
46static struct stats runtime_topdown_fetch_bubbles[NUM_CTX][MAX_NR_CPUS];
47static struct stats runtime_topdown_recovery_bubbles[NUM_CTX][MAX_NR_CPUS];
Kan Liangdaefd0b2017-05-26 12:05:38 -070048static struct stats runtime_smi_num_stats[NUM_CTX][MAX_NR_CPUS];
49static struct stats runtime_aperf_stats[NUM_CTX][MAX_NR_CPUS];
Andi Kleen37932c12017-03-20 13:17:08 -070050static struct rblist runtime_saved_values;
Andi Kleenfb4605b2016-03-01 10:57:52 -080051static bool have_frontend_stalled;
Jiri Olsaf87027b2015-06-03 16:25:59 +020052
53struct stats walltime_nsecs_stats;
54
Andi Kleen37932c12017-03-20 13:17:08 -070055struct saved_value {
56 struct rb_node rb_node;
57 struct perf_evsel *evsel;
58 int cpu;
Andi Kleen37932c12017-03-20 13:17:08 -070059 struct stats stats;
60};
61
62static int saved_value_cmp(struct rb_node *rb_node, const void *entry)
63{
64 struct saved_value *a = container_of(rb_node,
65 struct saved_value,
66 rb_node);
67 const struct saved_value *b = entry;
68
Andi Kleen37932c12017-03-20 13:17:08 -070069 if (a->cpu != b->cpu)
70 return a->cpu - b->cpu;
Andi Kleen5e976652017-07-24 16:40:03 -070071 if (a->evsel == b->evsel)
72 return 0;
73 if ((char *)a->evsel < (char *)b->evsel)
74 return -1;
75 return +1;
Andi Kleen37932c12017-03-20 13:17:08 -070076}
77
78static struct rb_node *saved_value_new(struct rblist *rblist __maybe_unused,
79 const void *entry)
80{
81 struct saved_value *nd = malloc(sizeof(struct saved_value));
82
83 if (!nd)
84 return NULL;
85 memcpy(nd, entry, sizeof(struct saved_value));
86 return &nd->rb_node;
87}
88
89static struct saved_value *saved_value_lookup(struct perf_evsel *evsel,
Andi Kleen4e1a0962017-08-31 12:40:33 -070090 int cpu,
Andi Kleen37932c12017-03-20 13:17:08 -070091 bool create)
92{
93 struct rb_node *nd;
94 struct saved_value dm = {
95 .cpu = cpu,
Andi Kleen37932c12017-03-20 13:17:08 -070096 .evsel = evsel,
97 };
98 nd = rblist__find(&runtime_saved_values, &dm);
99 if (nd)
100 return container_of(nd, struct saved_value, rb_node);
101 if (create) {
102 rblist__add_node(&runtime_saved_values, &dm);
103 nd = rblist__find(&runtime_saved_values, &dm);
104 if (nd)
105 return container_of(nd, struct saved_value, rb_node);
106 }
107 return NULL;
108}
109
Andi Kleenfb4605b2016-03-01 10:57:52 -0800110void perf_stat__init_shadow_stats(void)
111{
112 have_frontend_stalled = pmu_have_event("cpu", "stalled-cycles-frontend");
Andi Kleen37932c12017-03-20 13:17:08 -0700113 rblist__init(&runtime_saved_values);
114 runtime_saved_values.node_cmp = saved_value_cmp;
115 runtime_saved_values.node_new = saved_value_new;
116 /* No delete for now */
Andi Kleenfb4605b2016-03-01 10:57:52 -0800117}
118
Jiri Olsaf87027b2015-06-03 16:25:59 +0200119static int evsel_context(struct perf_evsel *evsel)
120{
121 int ctx = 0;
122
123 if (evsel->attr.exclude_kernel)
124 ctx |= CTX_BIT_KERNEL;
125 if (evsel->attr.exclude_user)
126 ctx |= CTX_BIT_USER;
127 if (evsel->attr.exclude_hv)
128 ctx |= CTX_BIT_HV;
129 if (evsel->attr.exclude_host)
130 ctx |= CTX_BIT_HOST;
131 if (evsel->attr.exclude_idle)
132 ctx |= CTX_BIT_IDLE;
133
134 return ctx;
135}
136
137void perf_stat__reset_shadow_stats(void)
138{
Andi Kleen37932c12017-03-20 13:17:08 -0700139 struct rb_node *pos, *next;
140
Jiri Olsaf87027b2015-06-03 16:25:59 +0200141 memset(runtime_nsecs_stats, 0, sizeof(runtime_nsecs_stats));
142 memset(runtime_cycles_stats, 0, sizeof(runtime_cycles_stats));
143 memset(runtime_stalled_cycles_front_stats, 0, sizeof(runtime_stalled_cycles_front_stats));
144 memset(runtime_stalled_cycles_back_stats, 0, sizeof(runtime_stalled_cycles_back_stats));
145 memset(runtime_branches_stats, 0, sizeof(runtime_branches_stats));
146 memset(runtime_cacherefs_stats, 0, sizeof(runtime_cacherefs_stats));
147 memset(runtime_l1_dcache_stats, 0, sizeof(runtime_l1_dcache_stats));
148 memset(runtime_l1_icache_stats, 0, sizeof(runtime_l1_icache_stats));
149 memset(runtime_ll_cache_stats, 0, sizeof(runtime_ll_cache_stats));
150 memset(runtime_itlb_cache_stats, 0, sizeof(runtime_itlb_cache_stats));
151 memset(runtime_dtlb_cache_stats, 0, sizeof(runtime_dtlb_cache_stats));
152 memset(runtime_cycles_in_tx_stats, 0,
153 sizeof(runtime_cycles_in_tx_stats));
154 memset(runtime_transaction_stats, 0,
155 sizeof(runtime_transaction_stats));
156 memset(runtime_elision_stats, 0, sizeof(runtime_elision_stats));
157 memset(&walltime_nsecs_stats, 0, sizeof(walltime_nsecs_stats));
Andi Kleen239bd472016-05-24 12:52:37 -0700158 memset(runtime_topdown_total_slots, 0, sizeof(runtime_topdown_total_slots));
159 memset(runtime_topdown_slots_retired, 0, sizeof(runtime_topdown_slots_retired));
160 memset(runtime_topdown_slots_issued, 0, sizeof(runtime_topdown_slots_issued));
161 memset(runtime_topdown_fetch_bubbles, 0, sizeof(runtime_topdown_fetch_bubbles));
162 memset(runtime_topdown_recovery_bubbles, 0, sizeof(runtime_topdown_recovery_bubbles));
Kan Liangdaefd0b2017-05-26 12:05:38 -0700163 memset(runtime_smi_num_stats, 0, sizeof(runtime_smi_num_stats));
164 memset(runtime_aperf_stats, 0, sizeof(runtime_aperf_stats));
Andi Kleen37932c12017-03-20 13:17:08 -0700165
166 next = rb_first(&runtime_saved_values.entries);
167 while (next) {
168 pos = next;
169 next = rb_next(pos);
170 memset(&container_of(pos, struct saved_value, rb_node)->stats,
171 0,
172 sizeof(struct stats));
173 }
Jiri Olsaf87027b2015-06-03 16:25:59 +0200174}
175
176/*
177 * Update various tracking values we maintain to print
178 * more semantic information such as miss/hit ratios,
179 * instruction rates, etc:
180 */
Jiri Olsa54830dd2017-01-23 22:42:56 +0100181void perf_stat__update_shadow_stats(struct perf_evsel *counter, u64 count,
Jiri Olsaf87027b2015-06-03 16:25:59 +0200182 int cpu)
183{
184 int ctx = evsel_context(counter);
185
Jiri Olsa54830dd2017-01-23 22:42:56 +0100186 count *= counter->scale;
187
Namhyung Kimdaf4f472016-05-13 15:01:02 +0900188 if (perf_evsel__match(counter, SOFTWARE, SW_TASK_CLOCK) ||
189 perf_evsel__match(counter, SOFTWARE, SW_CPU_CLOCK))
Jiri Olsa54830dd2017-01-23 22:42:56 +0100190 update_stats(&runtime_nsecs_stats[cpu], count);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200191 else if (perf_evsel__match(counter, HARDWARE, HW_CPU_CYCLES))
Jiri Olsa54830dd2017-01-23 22:42:56 +0100192 update_stats(&runtime_cycles_stats[ctx][cpu], count);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200193 else if (perf_stat_evsel__is(counter, CYCLES_IN_TX))
Jiri Olsa54830dd2017-01-23 22:42:56 +0100194 update_stats(&runtime_cycles_in_tx_stats[ctx][cpu], count);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200195 else if (perf_stat_evsel__is(counter, TRANSACTION_START))
Jiri Olsa54830dd2017-01-23 22:42:56 +0100196 update_stats(&runtime_transaction_stats[ctx][cpu], count);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200197 else if (perf_stat_evsel__is(counter, ELISION_START))
Jiri Olsa54830dd2017-01-23 22:42:56 +0100198 update_stats(&runtime_elision_stats[ctx][cpu], count);
Andi Kleen239bd472016-05-24 12:52:37 -0700199 else if (perf_stat_evsel__is(counter, TOPDOWN_TOTAL_SLOTS))
Jiri Olsa54830dd2017-01-23 22:42:56 +0100200 update_stats(&runtime_topdown_total_slots[ctx][cpu], count);
Andi Kleen239bd472016-05-24 12:52:37 -0700201 else if (perf_stat_evsel__is(counter, TOPDOWN_SLOTS_ISSUED))
Jiri Olsa54830dd2017-01-23 22:42:56 +0100202 update_stats(&runtime_topdown_slots_issued[ctx][cpu], count);
Andi Kleen239bd472016-05-24 12:52:37 -0700203 else if (perf_stat_evsel__is(counter, TOPDOWN_SLOTS_RETIRED))
Jiri Olsa54830dd2017-01-23 22:42:56 +0100204 update_stats(&runtime_topdown_slots_retired[ctx][cpu], count);
Andi Kleen239bd472016-05-24 12:52:37 -0700205 else if (perf_stat_evsel__is(counter, TOPDOWN_FETCH_BUBBLES))
Jiri Olsa54830dd2017-01-23 22:42:56 +0100206 update_stats(&runtime_topdown_fetch_bubbles[ctx][cpu], count);
Andi Kleen239bd472016-05-24 12:52:37 -0700207 else if (perf_stat_evsel__is(counter, TOPDOWN_RECOVERY_BUBBLES))
Jiri Olsa54830dd2017-01-23 22:42:56 +0100208 update_stats(&runtime_topdown_recovery_bubbles[ctx][cpu], count);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200209 else if (perf_evsel__match(counter, HARDWARE, HW_STALLED_CYCLES_FRONTEND))
Jiri Olsa54830dd2017-01-23 22:42:56 +0100210 update_stats(&runtime_stalled_cycles_front_stats[ctx][cpu], count);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200211 else if (perf_evsel__match(counter, HARDWARE, HW_STALLED_CYCLES_BACKEND))
Jiri Olsa54830dd2017-01-23 22:42:56 +0100212 update_stats(&runtime_stalled_cycles_back_stats[ctx][cpu], count);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200213 else if (perf_evsel__match(counter, HARDWARE, HW_BRANCH_INSTRUCTIONS))
Jiri Olsa54830dd2017-01-23 22:42:56 +0100214 update_stats(&runtime_branches_stats[ctx][cpu], count);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200215 else if (perf_evsel__match(counter, HARDWARE, HW_CACHE_REFERENCES))
Jiri Olsa54830dd2017-01-23 22:42:56 +0100216 update_stats(&runtime_cacherefs_stats[ctx][cpu], count);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200217 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_L1D))
Jiri Olsa54830dd2017-01-23 22:42:56 +0100218 update_stats(&runtime_l1_dcache_stats[ctx][cpu], count);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200219 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_L1I))
Jiri Olsa54830dd2017-01-23 22:42:56 +0100220 update_stats(&runtime_ll_cache_stats[ctx][cpu], count);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200221 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_LL))
Jiri Olsa54830dd2017-01-23 22:42:56 +0100222 update_stats(&runtime_ll_cache_stats[ctx][cpu], count);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200223 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_DTLB))
Jiri Olsa54830dd2017-01-23 22:42:56 +0100224 update_stats(&runtime_dtlb_cache_stats[ctx][cpu], count);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200225 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_ITLB))
Jiri Olsa54830dd2017-01-23 22:42:56 +0100226 update_stats(&runtime_itlb_cache_stats[ctx][cpu], count);
Kan Liangdaefd0b2017-05-26 12:05:38 -0700227 else if (perf_stat_evsel__is(counter, SMI_NUM))
Jiri Olsa54830dd2017-01-23 22:42:56 +0100228 update_stats(&runtime_smi_num_stats[ctx][cpu], count);
Kan Liangdaefd0b2017-05-26 12:05:38 -0700229 else if (perf_stat_evsel__is(counter, APERF))
Jiri Olsa54830dd2017-01-23 22:42:56 +0100230 update_stats(&runtime_aperf_stats[ctx][cpu], count);
Andi Kleen37932c12017-03-20 13:17:08 -0700231
232 if (counter->collect_stat) {
Andi Kleen4e1a0962017-08-31 12:40:33 -0700233 struct saved_value *v = saved_value_lookup(counter, cpu, true);
Jiri Olsa54830dd2017-01-23 22:42:56 +0100234 update_stats(&v->stats, count);
Andi Kleen37932c12017-03-20 13:17:08 -0700235 }
Jiri Olsaf87027b2015-06-03 16:25:59 +0200236}
237
238/* used for get_ratio_color() */
239enum grc_type {
240 GRC_STALLED_CYCLES_FE,
241 GRC_STALLED_CYCLES_BE,
242 GRC_CACHE_MISSES,
243 GRC_MAX_NR
244};
245
246static const char *get_ratio_color(enum grc_type type, double ratio)
247{
248 static const double grc_table[GRC_MAX_NR][3] = {
249 [GRC_STALLED_CYCLES_FE] = { 50.0, 30.0, 10.0 },
250 [GRC_STALLED_CYCLES_BE] = { 75.0, 50.0, 20.0 },
251 [GRC_CACHE_MISSES] = { 20.0, 10.0, 5.0 },
252 };
253 const char *color = PERF_COLOR_NORMAL;
254
255 if (ratio > grc_table[type][0])
256 color = PERF_COLOR_RED;
257 else if (ratio > grc_table[type][1])
258 color = PERF_COLOR_MAGENTA;
259 else if (ratio > grc_table[type][2])
260 color = PERF_COLOR_YELLOW;
261
262 return color;
263}
264
Andi Kleen37932c12017-03-20 13:17:08 -0700265static struct perf_evsel *perf_stat__find_event(struct perf_evlist *evsel_list,
266 const char *name)
267{
268 struct perf_evsel *c2;
269
270 evlist__for_each_entry (evsel_list, c2) {
271 if (!strcasecmp(c2->name, name))
272 return c2;
273 }
274 return NULL;
275}
276
277/* Mark MetricExpr target events and link events using them to them. */
278void perf_stat__collect_metric_expr(struct perf_evlist *evsel_list)
279{
280 struct perf_evsel *counter, *leader, **metric_events, *oc;
281 bool found;
282 const char **metric_names;
283 int i;
284 int num_metric_names;
285
286 evlist__for_each_entry(evsel_list, counter) {
287 bool invalid = false;
288
289 leader = counter->leader;
290 if (!counter->metric_expr)
291 continue;
292 metric_events = counter->metric_events;
293 if (!metric_events) {
294 if (expr__find_other(counter->metric_expr, counter->name,
295 &metric_names, &num_metric_names) < 0)
296 continue;
297
298 metric_events = calloc(sizeof(struct perf_evsel *),
299 num_metric_names + 1);
300 if (!metric_events)
301 return;
302 counter->metric_events = metric_events;
303 }
304
305 for (i = 0; i < num_metric_names; i++) {
306 found = false;
307 if (leader) {
308 /* Search in group */
309 for_each_group_member (oc, leader) {
310 if (!strcasecmp(oc->name, metric_names[i])) {
311 found = true;
312 break;
313 }
314 }
315 }
316 if (!found) {
317 /* Search ignoring groups */
318 oc = perf_stat__find_event(evsel_list, metric_names[i]);
319 }
320 if (!oc) {
321 /* Deduping one is good enough to handle duplicated PMUs. */
322 static char *printed;
323
324 /*
325 * Adding events automatically would be difficult, because
326 * it would risk creating groups that are not schedulable.
327 * perf stat doesn't understand all the scheduling constraints
328 * of events. So we ask the user instead to add the missing
329 * events.
330 */
331 if (!printed || strcasecmp(printed, metric_names[i])) {
332 fprintf(stderr,
333 "Add %s event to groups to get metric expression for %s\n",
334 metric_names[i],
335 counter->name);
336 printed = strdup(metric_names[i]);
337 }
338 invalid = true;
339 continue;
340 }
341 metric_events[i] = oc;
342 oc->collect_stat = true;
343 }
344 metric_events[i] = NULL;
345 free(metric_names);
346 if (invalid) {
347 free(metric_events);
348 counter->metric_events = NULL;
349 counter->metric_expr = NULL;
350 }
351 }
352}
353
Andi Kleen140aead2016-01-30 09:06:49 -0800354static void print_stalled_cycles_frontend(int cpu,
Arnaldo Carvalho de Melob8f8eb82016-03-22 13:09:37 -0300355 struct perf_evsel *evsel, double avg,
Andi Kleen140aead2016-01-30 09:06:49 -0800356 struct perf_stat_output_ctx *out)
Jiri Olsaf87027b2015-06-03 16:25:59 +0200357{
358 double total, ratio = 0.0;
359 const char *color;
360 int ctx = evsel_context(evsel);
361
362 total = avg_stats(&runtime_cycles_stats[ctx][cpu]);
363
364 if (total)
365 ratio = avg / total * 100.0;
366
367 color = get_ratio_color(GRC_STALLED_CYCLES_FE, ratio);
368
Andi Kleen140aead2016-01-30 09:06:49 -0800369 if (ratio)
370 out->print_metric(out->ctx, color, "%7.2f%%", "frontend cycles idle",
371 ratio);
372 else
373 out->print_metric(out->ctx, NULL, NULL, "frontend cycles idle", 0);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200374}
375
Andi Kleen140aead2016-01-30 09:06:49 -0800376static void print_stalled_cycles_backend(int cpu,
Arnaldo Carvalho de Melob8f8eb82016-03-22 13:09:37 -0300377 struct perf_evsel *evsel, double avg,
Andi Kleen140aead2016-01-30 09:06:49 -0800378 struct perf_stat_output_ctx *out)
Jiri Olsaf87027b2015-06-03 16:25:59 +0200379{
380 double total, ratio = 0.0;
381 const char *color;
382 int ctx = evsel_context(evsel);
383
384 total = avg_stats(&runtime_cycles_stats[ctx][cpu]);
385
386 if (total)
387 ratio = avg / total * 100.0;
388
389 color = get_ratio_color(GRC_STALLED_CYCLES_BE, ratio);
390
Namhyung Kimb0404be2016-05-13 15:01:01 +0900391 out->print_metric(out->ctx, color, "%7.2f%%", "backend cycles idle", ratio);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200392}
393
Andi Kleen140aead2016-01-30 09:06:49 -0800394static void print_branch_misses(int cpu,
Arnaldo Carvalho de Melob8f8eb82016-03-22 13:09:37 -0300395 struct perf_evsel *evsel,
Andi Kleen140aead2016-01-30 09:06:49 -0800396 double avg,
397 struct perf_stat_output_ctx *out)
Jiri Olsaf87027b2015-06-03 16:25:59 +0200398{
399 double total, ratio = 0.0;
400 const char *color;
401 int ctx = evsel_context(evsel);
402
403 total = avg_stats(&runtime_branches_stats[ctx][cpu]);
404
405 if (total)
406 ratio = avg / total * 100.0;
407
408 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
409
Andi Kleen140aead2016-01-30 09:06:49 -0800410 out->print_metric(out->ctx, color, "%7.2f%%", "of all branches", ratio);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200411}
412
Andi Kleen140aead2016-01-30 09:06:49 -0800413static void print_l1_dcache_misses(int cpu,
Arnaldo Carvalho de Melob8f8eb82016-03-22 13:09:37 -0300414 struct perf_evsel *evsel,
Andi Kleen140aead2016-01-30 09:06:49 -0800415 double avg,
416 struct perf_stat_output_ctx *out)
Jiri Olsaf87027b2015-06-03 16:25:59 +0200417{
418 double total, ratio = 0.0;
419 const char *color;
420 int ctx = evsel_context(evsel);
421
422 total = avg_stats(&runtime_l1_dcache_stats[ctx][cpu]);
423
424 if (total)
425 ratio = avg / total * 100.0;
426
427 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
428
Andi Kleen140aead2016-01-30 09:06:49 -0800429 out->print_metric(out->ctx, color, "%7.2f%%", "of all L1-dcache hits", ratio);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200430}
431
Andi Kleen140aead2016-01-30 09:06:49 -0800432static void print_l1_icache_misses(int cpu,
Arnaldo Carvalho de Melob8f8eb82016-03-22 13:09:37 -0300433 struct perf_evsel *evsel,
Andi Kleen140aead2016-01-30 09:06:49 -0800434 double avg,
435 struct perf_stat_output_ctx *out)
Jiri Olsaf87027b2015-06-03 16:25:59 +0200436{
437 double total, ratio = 0.0;
438 const char *color;
439 int ctx = evsel_context(evsel);
440
441 total = avg_stats(&runtime_l1_icache_stats[ctx][cpu]);
442
443 if (total)
444 ratio = avg / total * 100.0;
445
446 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
Andi Kleen140aead2016-01-30 09:06:49 -0800447 out->print_metric(out->ctx, color, "%7.2f%%", "of all L1-icache hits", ratio);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200448}
449
Andi Kleen140aead2016-01-30 09:06:49 -0800450static void print_dtlb_cache_misses(int cpu,
Arnaldo Carvalho de Melob8f8eb82016-03-22 13:09:37 -0300451 struct perf_evsel *evsel,
Andi Kleen140aead2016-01-30 09:06:49 -0800452 double avg,
453 struct perf_stat_output_ctx *out)
Jiri Olsaf87027b2015-06-03 16:25:59 +0200454{
455 double total, ratio = 0.0;
456 const char *color;
457 int ctx = evsel_context(evsel);
458
459 total = avg_stats(&runtime_dtlb_cache_stats[ctx][cpu]);
460
461 if (total)
462 ratio = avg / total * 100.0;
463
464 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
Andi Kleen140aead2016-01-30 09:06:49 -0800465 out->print_metric(out->ctx, color, "%7.2f%%", "of all dTLB cache hits", ratio);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200466}
467
Andi Kleen140aead2016-01-30 09:06:49 -0800468static void print_itlb_cache_misses(int cpu,
Arnaldo Carvalho de Melob8f8eb82016-03-22 13:09:37 -0300469 struct perf_evsel *evsel,
Andi Kleen140aead2016-01-30 09:06:49 -0800470 double avg,
471 struct perf_stat_output_ctx *out)
Jiri Olsaf87027b2015-06-03 16:25:59 +0200472{
473 double total, ratio = 0.0;
474 const char *color;
475 int ctx = evsel_context(evsel);
476
477 total = avg_stats(&runtime_itlb_cache_stats[ctx][cpu]);
478
479 if (total)
480 ratio = avg / total * 100.0;
481
482 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
Andi Kleen140aead2016-01-30 09:06:49 -0800483 out->print_metric(out->ctx, color, "%7.2f%%", "of all iTLB cache hits", ratio);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200484}
485
Andi Kleen140aead2016-01-30 09:06:49 -0800486static void print_ll_cache_misses(int cpu,
Arnaldo Carvalho de Melob8f8eb82016-03-22 13:09:37 -0300487 struct perf_evsel *evsel,
Andi Kleen140aead2016-01-30 09:06:49 -0800488 double avg,
489 struct perf_stat_output_ctx *out)
Jiri Olsaf87027b2015-06-03 16:25:59 +0200490{
491 double total, ratio = 0.0;
492 const char *color;
493 int ctx = evsel_context(evsel);
494
495 total = avg_stats(&runtime_ll_cache_stats[ctx][cpu]);
496
497 if (total)
498 ratio = avg / total * 100.0;
499
500 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
Andi Kleen140aead2016-01-30 09:06:49 -0800501 out->print_metric(out->ctx, color, "%7.2f%%", "of all LL-cache hits", ratio);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200502}
503
Andi Kleen239bd472016-05-24 12:52:37 -0700504/*
505 * High level "TopDown" CPU core pipe line bottleneck break down.
506 *
507 * Basic concept following
508 * Yasin, A Top Down Method for Performance analysis and Counter architecture
509 * ISPASS14
510 *
511 * The CPU pipeline is divided into 4 areas that can be bottlenecks:
512 *
513 * Frontend -> Backend -> Retiring
514 * BadSpeculation in addition means out of order execution that is thrown away
515 * (for example branch mispredictions)
516 * Frontend is instruction decoding.
517 * Backend is execution, like computation and accessing data in memory
518 * Retiring is good execution that is not directly bottlenecked
519 *
520 * The formulas are computed in slots.
521 * A slot is an entry in the pipeline each for the pipeline width
522 * (for example a 4-wide pipeline has 4 slots for each cycle)
523 *
524 * Formulas:
525 * BadSpeculation = ((SlotsIssued - SlotsRetired) + RecoveryBubbles) /
526 * TotalSlots
527 * Retiring = SlotsRetired / TotalSlots
528 * FrontendBound = FetchBubbles / TotalSlots
529 * BackendBound = 1.0 - BadSpeculation - Retiring - FrontendBound
530 *
531 * The kernel provides the mapping to the low level CPU events and any scaling
532 * needed for the CPU pipeline width, for example:
533 *
534 * TotalSlots = Cycles * 4
535 *
536 * The scaling factor is communicated in the sysfs unit.
537 *
538 * In some cases the CPU may not be able to measure all the formulas due to
539 * missing events. In this case multiple formulas are combined, as possible.
540 *
541 * Full TopDown supports more levels to sub-divide each area: for example
542 * BackendBound into computing bound and memory bound. For now we only
543 * support Level 1 TopDown.
544 */
545
546static double sanitize_val(double x)
547{
548 if (x < 0 && x >= -0.02)
549 return 0.0;
550 return x;
551}
552
553static double td_total_slots(int ctx, int cpu)
554{
555 return avg_stats(&runtime_topdown_total_slots[ctx][cpu]);
556}
557
558static double td_bad_spec(int ctx, int cpu)
559{
560 double bad_spec = 0;
561 double total_slots;
562 double total;
563
564 total = avg_stats(&runtime_topdown_slots_issued[ctx][cpu]) -
565 avg_stats(&runtime_topdown_slots_retired[ctx][cpu]) +
566 avg_stats(&runtime_topdown_recovery_bubbles[ctx][cpu]);
567 total_slots = td_total_slots(ctx, cpu);
568 if (total_slots)
569 bad_spec = total / total_slots;
570 return sanitize_val(bad_spec);
571}
572
573static double td_retiring(int ctx, int cpu)
574{
575 double retiring = 0;
576 double total_slots = td_total_slots(ctx, cpu);
577 double ret_slots = avg_stats(&runtime_topdown_slots_retired[ctx][cpu]);
578
579 if (total_slots)
580 retiring = ret_slots / total_slots;
581 return retiring;
582}
583
584static double td_fe_bound(int ctx, int cpu)
585{
586 double fe_bound = 0;
587 double total_slots = td_total_slots(ctx, cpu);
588 double fetch_bub = avg_stats(&runtime_topdown_fetch_bubbles[ctx][cpu]);
589
590 if (total_slots)
591 fe_bound = fetch_bub / total_slots;
592 return fe_bound;
593}
594
595static double td_be_bound(int ctx, int cpu)
596{
597 double sum = (td_fe_bound(ctx, cpu) +
598 td_bad_spec(ctx, cpu) +
599 td_retiring(ctx, cpu));
600 if (sum == 0)
601 return 0;
602 return sanitize_val(1.0 - sum);
603}
604
Kan Liangdaefd0b2017-05-26 12:05:38 -0700605static void print_smi_cost(int cpu, struct perf_evsel *evsel,
606 struct perf_stat_output_ctx *out)
607{
608 double smi_num, aperf, cycles, cost = 0.0;
609 int ctx = evsel_context(evsel);
610 const char *color = NULL;
611
612 smi_num = avg_stats(&runtime_smi_num_stats[ctx][cpu]);
613 aperf = avg_stats(&runtime_aperf_stats[ctx][cpu]);
614 cycles = avg_stats(&runtime_cycles_stats[ctx][cpu]);
615
616 if ((cycles == 0) || (aperf == 0))
617 return;
618
619 if (smi_num)
620 cost = (aperf - cycles) / aperf * 100.00;
621
622 if (cost > 10)
623 color = PERF_COLOR_RED;
624 out->print_metric(out->ctx, color, "%8.1f%%", "SMI cycles%", cost);
625 out->print_metric(out->ctx, NULL, "%4.0f", "SMI#", smi_num);
626}
627
Andi Kleenbba49af2017-08-31 12:40:28 -0700628static void generic_metric(const char *metric_expr,
629 struct perf_evsel **metric_events,
630 char *name,
631 const char *metric_name,
632 double avg,
633 int cpu,
Andi Kleenbba49af2017-08-31 12:40:28 -0700634 struct perf_stat_output_ctx *out)
635{
636 print_metric_t print_metric = out->print_metric;
637 struct parse_ctx pctx;
638 double ratio;
639 int i;
640 void *ctxp = out->ctx;
641
642 expr__ctx_init(&pctx);
643 expr__add_id(&pctx, name, avg);
644 for (i = 0; metric_events[i]; i++) {
645 struct saved_value *v;
Andi Kleenfd48aad2017-08-31 12:40:34 -0700646 struct stats *stats;
647 double scale;
Andi Kleenbba49af2017-08-31 12:40:28 -0700648
Andi Kleenfd48aad2017-08-31 12:40:34 -0700649 if (!strcmp(metric_events[i]->name, "duration_time")) {
650 stats = &walltime_nsecs_stats;
651 scale = 1e-9;
652 } else {
653 v = saved_value_lookup(metric_events[i], cpu, false);
654 if (!v)
655 break;
656 stats = &v->stats;
657 scale = 1.0;
658 }
659 expr__add_id(&pctx, metric_events[i]->name, avg_stats(stats)*scale);
Andi Kleenbba49af2017-08-31 12:40:28 -0700660 }
661 if (!metric_events[i]) {
662 const char *p = metric_expr;
663
664 if (expr__parse(&ratio, &pctx, &p) == 0)
665 print_metric(ctxp, NULL, "%8.1f",
666 metric_name ?
667 metric_name :
668 out->force_header ? name : "",
669 ratio);
670 else
Andi Kleen4ed962e2017-08-31 12:40:29 -0700671 print_metric(ctxp, NULL, NULL,
672 out->force_header ?
673 (metric_name ? metric_name : name) : "", 0);
Andi Kleenbba49af2017-08-31 12:40:28 -0700674 } else
675 print_metric(ctxp, NULL, NULL, "", 0);
676}
677
Andi Kleen140aead2016-01-30 09:06:49 -0800678void perf_stat__print_shadow_stats(struct perf_evsel *evsel,
679 double avg, int cpu,
Andi Kleenb18f3e32017-08-31 12:40:31 -0700680 struct perf_stat_output_ctx *out,
681 struct rblist *metric_events)
Jiri Olsaf87027b2015-06-03 16:25:59 +0200682{
Andi Kleen140aead2016-01-30 09:06:49 -0800683 void *ctxp = out->ctx;
684 print_metric_t print_metric = out->print_metric;
Jiri Olsaf87027b2015-06-03 16:25:59 +0200685 double total, ratio = 0.0, total2;
Andi Kleen239bd472016-05-24 12:52:37 -0700686 const char *color = NULL;
Jiri Olsaf87027b2015-06-03 16:25:59 +0200687 int ctx = evsel_context(evsel);
Andi Kleenb18f3e32017-08-31 12:40:31 -0700688 struct metric_event *me;
689 int num = 1;
Jiri Olsaf87027b2015-06-03 16:25:59 +0200690
691 if (perf_evsel__match(evsel, HARDWARE, HW_INSTRUCTIONS)) {
692 total = avg_stats(&runtime_cycles_stats[ctx][cpu]);
693 if (total) {
694 ratio = avg / total;
Andi Kleen140aead2016-01-30 09:06:49 -0800695 print_metric(ctxp, NULL, "%7.2f ",
696 "insn per cycle", ratio);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200697 } else {
Andi Kleen140aead2016-01-30 09:06:49 -0800698 print_metric(ctxp, NULL, NULL, "insn per cycle", 0);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200699 }
700 total = avg_stats(&runtime_stalled_cycles_front_stats[ctx][cpu]);
701 total = max(total, avg_stats(&runtime_stalled_cycles_back_stats[ctx][cpu]));
702
703 if (total && avg) {
Andi Kleen92a61f62016-02-29 14:36:21 -0800704 out->new_line(ctxp);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200705 ratio = total / avg;
Andi Kleen140aead2016-01-30 09:06:49 -0800706 print_metric(ctxp, NULL, "%7.2f ",
707 "stalled cycles per insn",
708 ratio);
Andi Kleenfb4605b2016-03-01 10:57:52 -0800709 } else if (have_frontend_stalled) {
Andi Kleen140aead2016-01-30 09:06:49 -0800710 print_metric(ctxp, NULL, NULL,
711 "stalled cycles per insn", 0);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200712 }
Andi Kleen140aead2016-01-30 09:06:49 -0800713 } else if (perf_evsel__match(evsel, HARDWARE, HW_BRANCH_MISSES)) {
714 if (runtime_branches_stats[ctx][cpu].n != 0)
715 print_branch_misses(cpu, evsel, avg, out);
716 else
717 print_metric(ctxp, NULL, NULL, "of all branches", 0);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200718 } else if (
719 evsel->attr.type == PERF_TYPE_HW_CACHE &&
720 evsel->attr.config == ( PERF_COUNT_HW_CACHE_L1D |
721 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
Andi Kleen140aead2016-01-30 09:06:49 -0800722 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
723 if (runtime_l1_dcache_stats[ctx][cpu].n != 0)
724 print_l1_dcache_misses(cpu, evsel, avg, out);
725 else
726 print_metric(ctxp, NULL, NULL, "of all L1-dcache hits", 0);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200727 } else if (
728 evsel->attr.type == PERF_TYPE_HW_CACHE &&
729 evsel->attr.config == ( PERF_COUNT_HW_CACHE_L1I |
730 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
Andi Kleen140aead2016-01-30 09:06:49 -0800731 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
732 if (runtime_l1_icache_stats[ctx][cpu].n != 0)
733 print_l1_icache_misses(cpu, evsel, avg, out);
734 else
735 print_metric(ctxp, NULL, NULL, "of all L1-icache hits", 0);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200736 } else if (
737 evsel->attr.type == PERF_TYPE_HW_CACHE &&
738 evsel->attr.config == ( PERF_COUNT_HW_CACHE_DTLB |
739 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
Andi Kleen140aead2016-01-30 09:06:49 -0800740 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
741 if (runtime_dtlb_cache_stats[ctx][cpu].n != 0)
742 print_dtlb_cache_misses(cpu, evsel, avg, out);
743 else
744 print_metric(ctxp, NULL, NULL, "of all dTLB cache hits", 0);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200745 } else if (
746 evsel->attr.type == PERF_TYPE_HW_CACHE &&
747 evsel->attr.config == ( PERF_COUNT_HW_CACHE_ITLB |
748 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
Andi Kleen140aead2016-01-30 09:06:49 -0800749 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
750 if (runtime_itlb_cache_stats[ctx][cpu].n != 0)
751 print_itlb_cache_misses(cpu, evsel, avg, out);
752 else
753 print_metric(ctxp, NULL, NULL, "of all iTLB cache hits", 0);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200754 } else if (
755 evsel->attr.type == PERF_TYPE_HW_CACHE &&
756 evsel->attr.config == ( PERF_COUNT_HW_CACHE_LL |
757 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
Andi Kleen140aead2016-01-30 09:06:49 -0800758 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
759 if (runtime_ll_cache_stats[ctx][cpu].n != 0)
760 print_ll_cache_misses(cpu, evsel, avg, out);
761 else
762 print_metric(ctxp, NULL, NULL, "of all LL-cache hits", 0);
763 } else if (perf_evsel__match(evsel, HARDWARE, HW_CACHE_MISSES)) {
Jiri Olsaf87027b2015-06-03 16:25:59 +0200764 total = avg_stats(&runtime_cacherefs_stats[ctx][cpu]);
765
766 if (total)
767 ratio = avg * 100 / total;
768
Andi Kleen140aead2016-01-30 09:06:49 -0800769 if (runtime_cacherefs_stats[ctx][cpu].n != 0)
770 print_metric(ctxp, NULL, "%8.3f %%",
771 "of all cache refs", ratio);
772 else
773 print_metric(ctxp, NULL, NULL, "of all cache refs", 0);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200774 } else if (perf_evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES_FRONTEND)) {
Andi Kleen140aead2016-01-30 09:06:49 -0800775 print_stalled_cycles_frontend(cpu, evsel, avg, out);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200776 } else if (perf_evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES_BACKEND)) {
Andi Kleen140aead2016-01-30 09:06:49 -0800777 print_stalled_cycles_backend(cpu, evsel, avg, out);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200778 } else if (perf_evsel__match(evsel, HARDWARE, HW_CPU_CYCLES)) {
779 total = avg_stats(&runtime_nsecs_stats[cpu]);
780
781 if (total) {
782 ratio = avg / total;
Andi Kleen140aead2016-01-30 09:06:49 -0800783 print_metric(ctxp, NULL, "%8.3f", "GHz", ratio);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200784 } else {
Andi Kleen140aead2016-01-30 09:06:49 -0800785 print_metric(ctxp, NULL, NULL, "Ghz", 0);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200786 }
787 } else if (perf_stat_evsel__is(evsel, CYCLES_IN_TX)) {
788 total = avg_stats(&runtime_cycles_stats[ctx][cpu]);
789 if (total)
Andi Kleen140aead2016-01-30 09:06:49 -0800790 print_metric(ctxp, NULL,
791 "%7.2f%%", "transactional cycles",
792 100.0 * (avg / total));
793 else
794 print_metric(ctxp, NULL, NULL, "transactional cycles",
795 0);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200796 } else if (perf_stat_evsel__is(evsel, CYCLES_IN_TX_CP)) {
797 total = avg_stats(&runtime_cycles_stats[ctx][cpu]);
798 total2 = avg_stats(&runtime_cycles_in_tx_stats[ctx][cpu]);
799 if (total2 < avg)
800 total2 = avg;
801 if (total)
Andi Kleen140aead2016-01-30 09:06:49 -0800802 print_metric(ctxp, NULL, "%7.2f%%", "aborted cycles",
Jiri Olsaf87027b2015-06-03 16:25:59 +0200803 100.0 * ((total2-avg) / total));
Andi Kleen140aead2016-01-30 09:06:49 -0800804 else
805 print_metric(ctxp, NULL, NULL, "aborted cycles", 0);
806 } else if (perf_stat_evsel__is(evsel, TRANSACTION_START)) {
Jiri Olsaf87027b2015-06-03 16:25:59 +0200807 total = avg_stats(&runtime_cycles_in_tx_stats[ctx][cpu]);
808
Andi Kleen54976282015-07-27 16:24:51 -0700809 if (avg)
Jiri Olsaf87027b2015-06-03 16:25:59 +0200810 ratio = total / avg;
811
Andi Kleen140aead2016-01-30 09:06:49 -0800812 if (runtime_cycles_in_tx_stats[ctx][cpu].n != 0)
813 print_metric(ctxp, NULL, "%8.0f",
814 "cycles / transaction", ratio);
815 else
816 print_metric(ctxp, NULL, NULL, "cycles / transaction",
817 0);
818 } else if (perf_stat_evsel__is(evsel, ELISION_START)) {
Jiri Olsaf87027b2015-06-03 16:25:59 +0200819 total = avg_stats(&runtime_cycles_in_tx_stats[ctx][cpu]);
820
Andi Kleen54976282015-07-27 16:24:51 -0700821 if (avg)
Jiri Olsaf87027b2015-06-03 16:25:59 +0200822 ratio = total / avg;
823
Andi Kleen140aead2016-01-30 09:06:49 -0800824 print_metric(ctxp, NULL, "%8.0f", "cycles / elision", ratio);
Namhyung Kimdaf4f472016-05-13 15:01:02 +0900825 } else if (perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK) ||
826 perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK)) {
Andi Kleen4579ecc2015-11-02 17:50:20 -0800827 if ((ratio = avg_stats(&walltime_nsecs_stats)) != 0)
Andi Kleen140aead2016-01-30 09:06:49 -0800828 print_metric(ctxp, NULL, "%8.3f", "CPUs utilized",
829 avg / ratio);
Andi Kleen4579ecc2015-11-02 17:50:20 -0800830 else
Andi Kleen140aead2016-01-30 09:06:49 -0800831 print_metric(ctxp, NULL, NULL, "CPUs utilized", 0);
Andi Kleen239bd472016-05-24 12:52:37 -0700832 } else if (perf_stat_evsel__is(evsel, TOPDOWN_FETCH_BUBBLES)) {
833 double fe_bound = td_fe_bound(ctx, cpu);
834
835 if (fe_bound > 0.2)
836 color = PERF_COLOR_RED;
837 print_metric(ctxp, color, "%8.1f%%", "frontend bound",
838 fe_bound * 100.);
839 } else if (perf_stat_evsel__is(evsel, TOPDOWN_SLOTS_RETIRED)) {
840 double retiring = td_retiring(ctx, cpu);
841
842 if (retiring > 0.7)
843 color = PERF_COLOR_GREEN;
844 print_metric(ctxp, color, "%8.1f%%", "retiring",
845 retiring * 100.);
846 } else if (perf_stat_evsel__is(evsel, TOPDOWN_RECOVERY_BUBBLES)) {
847 double bad_spec = td_bad_spec(ctx, cpu);
848
849 if (bad_spec > 0.1)
850 color = PERF_COLOR_RED;
851 print_metric(ctxp, color, "%8.1f%%", "bad speculation",
852 bad_spec * 100.);
853 } else if (perf_stat_evsel__is(evsel, TOPDOWN_SLOTS_ISSUED)) {
854 double be_bound = td_be_bound(ctx, cpu);
855 const char *name = "backend bound";
856 static int have_recovery_bubbles = -1;
857
858 /* In case the CPU does not support topdown-recovery-bubbles */
859 if (have_recovery_bubbles < 0)
860 have_recovery_bubbles = pmu_have_event("cpu",
861 "topdown-recovery-bubbles");
862 if (!have_recovery_bubbles)
863 name = "backend bound/bad spec";
864
865 if (be_bound > 0.2)
866 color = PERF_COLOR_RED;
867 if (td_total_slots(ctx, cpu) > 0)
868 print_metric(ctxp, color, "%8.1f%%", name,
869 be_bound * 100.);
870 else
871 print_metric(ctxp, NULL, NULL, name, 0);
Andi Kleen37932c12017-03-20 13:17:08 -0700872 } else if (evsel->metric_expr) {
Andi Kleenbba49af2017-08-31 12:40:28 -0700873 generic_metric(evsel->metric_expr, evsel->metric_events, evsel->name,
Andi Kleen4e1a0962017-08-31 12:40:33 -0700874 evsel->metric_name, avg, cpu, out);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200875 } else if (runtime_nsecs_stats[cpu].n != 0) {
876 char unit = 'M';
Andi Kleen140aead2016-01-30 09:06:49 -0800877 char unit_buf[10];
Jiri Olsaf87027b2015-06-03 16:25:59 +0200878
879 total = avg_stats(&runtime_nsecs_stats[cpu]);
880
881 if (total)
882 ratio = 1000.0 * avg / total;
883 if (ratio < 0.001) {
884 ratio *= 1000;
885 unit = 'K';
886 }
Andi Kleen140aead2016-01-30 09:06:49 -0800887 snprintf(unit_buf, sizeof(unit_buf), "%c/sec", unit);
888 print_metric(ctxp, NULL, "%8.3f", unit_buf, ratio);
Kan Liangdaefd0b2017-05-26 12:05:38 -0700889 } else if (perf_stat_evsel__is(evsel, SMI_NUM)) {
890 print_smi_cost(cpu, evsel, out);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200891 } else {
Andi Kleenb18f3e32017-08-31 12:40:31 -0700892 num = 0;
Jiri Olsaf87027b2015-06-03 16:25:59 +0200893 }
Andi Kleenb18f3e32017-08-31 12:40:31 -0700894
895 if ((me = metricgroup__lookup(metric_events, evsel, false)) != NULL) {
896 struct metric_expr *mexp;
897
898 list_for_each_entry (mexp, &me->head, nd) {
899 if (num++ > 0)
900 out->new_line(ctxp);
901 generic_metric(mexp->metric_expr, mexp->metric_events,
902 evsel->name, mexp->metric_name,
Andi Kleen4e1a0962017-08-31 12:40:33 -0700903 avg, cpu, out);
Andi Kleenb18f3e32017-08-31 12:40:31 -0700904 }
905 }
906 if (num == 0)
907 print_metric(ctxp, NULL, NULL, NULL, 0);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200908}