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