1 // SPDX-License-Identifier: GPL-2.0
10 #include "metricgroup.h"
12 #include <linux/zalloc.h>
15 * AGGR_GLOBAL: Use CPU 0
16 * AGGR_SOCKET: Use first CPU of socket
17 * AGGR_DIE: Use first CPU of die
18 * AGGR_CORE: Use first CPU of core
19 * AGGR_NONE: Use matching CPU
20 * AGGR_THREAD: Not supported?
23 struct runtime_stat rt_stat;
24 struct stats walltime_nsecs_stats;
27 struct rb_node rb_node;
33 struct runtime_stat *stat;
39 static int saved_value_cmp(struct rb_node *rb_node, const void *entry)
41 struct saved_value *a = container_of(rb_node,
44 const struct saved_value *b = entry;
47 return a->cpu - b->cpu;
50 * Previously the rbtree was used to link generic metrics.
51 * The keys were evsel/cpu. Now the rbtree is extended to support
52 * per-thread shadow stats. For shadow stats case, the keys
53 * are cpu/type/ctx/stat (evsel is NULL). For generic metrics
54 * case, the keys are still evsel/cpu (type/ctx/stat are 0 or NULL).
56 if (a->type != b->type)
57 return a->type - b->type;
60 return a->ctx - b->ctx;
62 if (a->cgrp != b->cgrp)
63 return (char *)a->cgrp < (char *)b->cgrp ? -1 : +1;
65 if (a->evsel == NULL && b->evsel == NULL) {
66 if (a->stat == b->stat)
69 if ((char *)a->stat < (char *)b->stat)
75 if (a->evsel == b->evsel)
77 if ((char *)a->evsel < (char *)b->evsel)
82 static struct rb_node *saved_value_new(struct rblist *rblist __maybe_unused,
85 struct saved_value *nd = malloc(sizeof(struct saved_value));
89 memcpy(nd, entry, sizeof(struct saved_value));
93 static void saved_value_delete(struct rblist *rblist __maybe_unused,
94 struct rb_node *rb_node)
96 struct saved_value *v;
99 v = container_of(rb_node, struct saved_value, rb_node);
103 static struct saved_value *saved_value_lookup(struct evsel *evsel,
108 struct runtime_stat *st,
111 struct rblist *rblist;
113 struct saved_value dm = {
122 rblist = &st->value_list;
124 /* don't use context info for clock events */
125 if (type == STAT_NSECS)
128 nd = rblist__find(rblist, &dm);
130 return container_of(nd, struct saved_value, rb_node);
132 rblist__add_node(rblist, &dm);
133 nd = rblist__find(rblist, &dm);
135 return container_of(nd, struct saved_value, rb_node);
140 void runtime_stat__init(struct runtime_stat *st)
142 struct rblist *rblist = &st->value_list;
144 rblist__init(rblist);
145 rblist->node_cmp = saved_value_cmp;
146 rblist->node_new = saved_value_new;
147 rblist->node_delete = saved_value_delete;
150 void runtime_stat__exit(struct runtime_stat *st)
152 rblist__exit(&st->value_list);
155 void perf_stat__init_shadow_stats(void)
157 runtime_stat__init(&rt_stat);
160 static int evsel_context(struct evsel *evsel)
164 if (evsel->core.attr.exclude_kernel)
165 ctx |= CTX_BIT_KERNEL;
166 if (evsel->core.attr.exclude_user)
168 if (evsel->core.attr.exclude_hv)
170 if (evsel->core.attr.exclude_host)
172 if (evsel->core.attr.exclude_idle)
178 static void reset_stat(struct runtime_stat *st)
180 struct rblist *rblist;
181 struct rb_node *pos, *next;
183 rblist = &st->value_list;
184 next = rb_first_cached(&rblist->entries);
188 memset(&container_of(pos, struct saved_value, rb_node)->stats,
190 sizeof(struct stats));
194 void perf_stat__reset_shadow_stats(void)
196 reset_stat(&rt_stat);
197 memset(&walltime_nsecs_stats, 0, sizeof(walltime_nsecs_stats));
200 void perf_stat__reset_shadow_per_stat(struct runtime_stat *st)
205 struct runtime_stat_data {
210 static void update_runtime_stat(struct runtime_stat *st,
213 struct runtime_stat_data *rsd)
215 struct saved_value *v = saved_value_lookup(NULL, cpu, true, type,
216 rsd->ctx, st, rsd->cgrp);
219 update_stats(&v->stats, count);
223 * Update various tracking values we maintain to print
224 * more semantic information such as miss/hit ratios,
225 * instruction rates, etc:
227 void perf_stat__update_shadow_stats(struct evsel *counter, u64 count,
228 int cpu, struct runtime_stat *st)
230 u64 count_ns = count;
231 struct saved_value *v;
232 struct runtime_stat_data rsd = {
233 .ctx = evsel_context(counter),
234 .cgrp = counter->cgrp,
237 count *= counter->scale;
239 if (evsel__is_clock(counter))
240 update_runtime_stat(st, STAT_NSECS, cpu, count_ns, &rsd);
241 else if (evsel__match(counter, HARDWARE, HW_CPU_CYCLES))
242 update_runtime_stat(st, STAT_CYCLES, cpu, count, &rsd);
243 else if (perf_stat_evsel__is(counter, CYCLES_IN_TX))
244 update_runtime_stat(st, STAT_CYCLES_IN_TX, cpu, count, &rsd);
245 else if (perf_stat_evsel__is(counter, TRANSACTION_START))
246 update_runtime_stat(st, STAT_TRANSACTION, cpu, count, &rsd);
247 else if (perf_stat_evsel__is(counter, ELISION_START))
248 update_runtime_stat(st, STAT_ELISION, cpu, count, &rsd);
249 else if (perf_stat_evsel__is(counter, TOPDOWN_TOTAL_SLOTS))
250 update_runtime_stat(st, STAT_TOPDOWN_TOTAL_SLOTS,
252 else if (perf_stat_evsel__is(counter, TOPDOWN_SLOTS_ISSUED))
253 update_runtime_stat(st, STAT_TOPDOWN_SLOTS_ISSUED,
255 else if (perf_stat_evsel__is(counter, TOPDOWN_SLOTS_RETIRED))
256 update_runtime_stat(st, STAT_TOPDOWN_SLOTS_RETIRED,
258 else if (perf_stat_evsel__is(counter, TOPDOWN_FETCH_BUBBLES))
259 update_runtime_stat(st, STAT_TOPDOWN_FETCH_BUBBLES,
261 else if (perf_stat_evsel__is(counter, TOPDOWN_RECOVERY_BUBBLES))
262 update_runtime_stat(st, STAT_TOPDOWN_RECOVERY_BUBBLES,
264 else if (perf_stat_evsel__is(counter, TOPDOWN_RETIRING))
265 update_runtime_stat(st, STAT_TOPDOWN_RETIRING,
267 else if (perf_stat_evsel__is(counter, TOPDOWN_BAD_SPEC))
268 update_runtime_stat(st, STAT_TOPDOWN_BAD_SPEC,
270 else if (perf_stat_evsel__is(counter, TOPDOWN_FE_BOUND))
271 update_runtime_stat(st, STAT_TOPDOWN_FE_BOUND,
273 else if (perf_stat_evsel__is(counter, TOPDOWN_BE_BOUND))
274 update_runtime_stat(st, STAT_TOPDOWN_BE_BOUND,
276 else if (perf_stat_evsel__is(counter, TOPDOWN_HEAVY_OPS))
277 update_runtime_stat(st, STAT_TOPDOWN_HEAVY_OPS,
279 else if (perf_stat_evsel__is(counter, TOPDOWN_BR_MISPREDICT))
280 update_runtime_stat(st, STAT_TOPDOWN_BR_MISPREDICT,
282 else if (perf_stat_evsel__is(counter, TOPDOWN_FETCH_LAT))
283 update_runtime_stat(st, STAT_TOPDOWN_FETCH_LAT,
285 else if (perf_stat_evsel__is(counter, TOPDOWN_MEM_BOUND))
286 update_runtime_stat(st, STAT_TOPDOWN_MEM_BOUND,
288 else if (evsel__match(counter, HARDWARE, HW_STALLED_CYCLES_FRONTEND))
289 update_runtime_stat(st, STAT_STALLED_CYCLES_FRONT,
291 else if (evsel__match(counter, HARDWARE, HW_STALLED_CYCLES_BACKEND))
292 update_runtime_stat(st, STAT_STALLED_CYCLES_BACK,
294 else if (evsel__match(counter, HARDWARE, HW_BRANCH_INSTRUCTIONS))
295 update_runtime_stat(st, STAT_BRANCHES, cpu, count, &rsd);
296 else if (evsel__match(counter, HARDWARE, HW_CACHE_REFERENCES))
297 update_runtime_stat(st, STAT_CACHEREFS, cpu, count, &rsd);
298 else if (evsel__match(counter, HW_CACHE, HW_CACHE_L1D))
299 update_runtime_stat(st, STAT_L1_DCACHE, cpu, count, &rsd);
300 else if (evsel__match(counter, HW_CACHE, HW_CACHE_L1I))
301 update_runtime_stat(st, STAT_L1_ICACHE, cpu, count, &rsd);
302 else if (evsel__match(counter, HW_CACHE, HW_CACHE_LL))
303 update_runtime_stat(st, STAT_LL_CACHE, cpu, count, &rsd);
304 else if (evsel__match(counter, HW_CACHE, HW_CACHE_DTLB))
305 update_runtime_stat(st, STAT_DTLB_CACHE, cpu, count, &rsd);
306 else if (evsel__match(counter, HW_CACHE, HW_CACHE_ITLB))
307 update_runtime_stat(st, STAT_ITLB_CACHE, cpu, count, &rsd);
308 else if (perf_stat_evsel__is(counter, SMI_NUM))
309 update_runtime_stat(st, STAT_SMI_NUM, cpu, count, &rsd);
310 else if (perf_stat_evsel__is(counter, APERF))
311 update_runtime_stat(st, STAT_APERF, cpu, count, &rsd);
313 if (counter->collect_stat) {
314 v = saved_value_lookup(counter, cpu, true, STAT_NONE, 0, st,
316 update_stats(&v->stats, count);
317 if (counter->metric_leader)
318 v->metric_total += count;
319 } else if (counter->metric_leader) {
320 v = saved_value_lookup(counter->metric_leader,
321 cpu, true, STAT_NONE, 0, st, rsd.cgrp);
322 v->metric_total += count;
327 /* used for get_ratio_color() */
329 GRC_STALLED_CYCLES_FE,
330 GRC_STALLED_CYCLES_BE,
335 static const char *get_ratio_color(enum grc_type type, double ratio)
337 static const double grc_table[GRC_MAX_NR][3] = {
338 [GRC_STALLED_CYCLES_FE] = { 50.0, 30.0, 10.0 },
339 [GRC_STALLED_CYCLES_BE] = { 75.0, 50.0, 20.0 },
340 [GRC_CACHE_MISSES] = { 20.0, 10.0, 5.0 },
342 const char *color = PERF_COLOR_NORMAL;
344 if (ratio > grc_table[type][0])
345 color = PERF_COLOR_RED;
346 else if (ratio > grc_table[type][1])
347 color = PERF_COLOR_MAGENTA;
348 else if (ratio > grc_table[type][2])
349 color = PERF_COLOR_YELLOW;
354 static struct evsel *perf_stat__find_event(struct evlist *evsel_list,
359 evlist__for_each_entry (evsel_list, c2) {
360 if (!strcasecmp(c2->name, name) && !c2->collect_stat)
366 /* Mark MetricExpr target events and link events using them to them. */
367 void perf_stat__collect_metric_expr(struct evlist *evsel_list)
369 struct evsel *counter, *leader, **metric_events, *oc;
371 struct expr_parse_ctx ctx;
372 struct hashmap_entry *cur;
376 expr__ctx_init(&ctx);
377 evlist__for_each_entry(evsel_list, counter) {
378 bool invalid = false;
380 leader = counter->leader;
381 if (!counter->metric_expr)
384 expr__ctx_clear(&ctx);
385 metric_events = counter->metric_events;
386 if (!metric_events) {
387 if (expr__find_other(counter->metric_expr,
392 metric_events = calloc(sizeof(struct evsel *),
393 hashmap__size(&ctx.ids) + 1);
394 if (!metric_events) {
395 expr__ctx_clear(&ctx);
398 counter->metric_events = metric_events;
402 hashmap__for_each_entry((&ctx.ids), cur, bkt) {
403 const char *metric_name = (const char *)cur->key;
407 /* Search in group */
408 for_each_group_member (oc, leader) {
409 if (!strcasecmp(oc->name,
418 /* Search ignoring groups */
419 oc = perf_stat__find_event(evsel_list,
423 /* Deduping one is good enough to handle duplicated PMUs. */
424 static char *printed;
427 * Adding events automatically would be difficult, because
428 * it would risk creating groups that are not schedulable.
429 * perf stat doesn't understand all the scheduling constraints
430 * of events. So we ask the user instead to add the missing
434 strcasecmp(printed, metric_name)) {
436 "Add %s event to groups to get metric expression for %s\n",
439 printed = strdup(metric_name);
444 metric_events[i++] = oc;
445 oc->collect_stat = true;
447 metric_events[i] = NULL;
450 counter->metric_events = NULL;
451 counter->metric_expr = NULL;
454 expr__ctx_clear(&ctx);
457 static double runtime_stat_avg(struct runtime_stat *st,
458 enum stat_type type, int cpu,
459 struct runtime_stat_data *rsd)
461 struct saved_value *v;
463 v = saved_value_lookup(NULL, cpu, false, type, rsd->ctx, st, rsd->cgrp);
467 return avg_stats(&v->stats);
470 static double runtime_stat_n(struct runtime_stat *st,
471 enum stat_type type, int cpu,
472 struct runtime_stat_data *rsd)
474 struct saved_value *v;
476 v = saved_value_lookup(NULL, cpu, false, type, rsd->ctx, st, rsd->cgrp);
483 static void print_stalled_cycles_frontend(struct perf_stat_config *config,
485 struct perf_stat_output_ctx *out,
486 struct runtime_stat *st,
487 struct runtime_stat_data *rsd)
489 double total, ratio = 0.0;
492 total = runtime_stat_avg(st, STAT_CYCLES, cpu, rsd);
495 ratio = avg / total * 100.0;
497 color = get_ratio_color(GRC_STALLED_CYCLES_FE, ratio);
500 out->print_metric(config, out->ctx, color, "%7.2f%%", "frontend cycles idle",
503 out->print_metric(config, out->ctx, NULL, NULL, "frontend cycles idle", 0);
506 static void print_stalled_cycles_backend(struct perf_stat_config *config,
508 struct perf_stat_output_ctx *out,
509 struct runtime_stat *st,
510 struct runtime_stat_data *rsd)
512 double total, ratio = 0.0;
515 total = runtime_stat_avg(st, STAT_CYCLES, cpu, rsd);
518 ratio = avg / total * 100.0;
520 color = get_ratio_color(GRC_STALLED_CYCLES_BE, ratio);
522 out->print_metric(config, out->ctx, color, "%7.2f%%", "backend cycles idle", ratio);
525 static void print_branch_misses(struct perf_stat_config *config,
527 struct perf_stat_output_ctx *out,
528 struct runtime_stat *st,
529 struct runtime_stat_data *rsd)
531 double total, ratio = 0.0;
534 total = runtime_stat_avg(st, STAT_BRANCHES, cpu, rsd);
537 ratio = avg / total * 100.0;
539 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
541 out->print_metric(config, out->ctx, color, "%7.2f%%", "of all branches", ratio);
544 static void print_l1_dcache_misses(struct perf_stat_config *config,
546 struct perf_stat_output_ctx *out,
547 struct runtime_stat *st,
548 struct runtime_stat_data *rsd)
550 double total, ratio = 0.0;
553 total = runtime_stat_avg(st, STAT_L1_DCACHE, cpu, rsd);
556 ratio = avg / total * 100.0;
558 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
560 out->print_metric(config, out->ctx, color, "%7.2f%%", "of all L1-dcache accesses", ratio);
563 static void print_l1_icache_misses(struct perf_stat_config *config,
565 struct perf_stat_output_ctx *out,
566 struct runtime_stat *st,
567 struct runtime_stat_data *rsd)
569 double total, ratio = 0.0;
572 total = runtime_stat_avg(st, STAT_L1_ICACHE, cpu, rsd);
575 ratio = avg / total * 100.0;
577 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
578 out->print_metric(config, out->ctx, color, "%7.2f%%", "of all L1-icache accesses", ratio);
581 static void print_dtlb_cache_misses(struct perf_stat_config *config,
583 struct perf_stat_output_ctx *out,
584 struct runtime_stat *st,
585 struct runtime_stat_data *rsd)
587 double total, ratio = 0.0;
590 total = runtime_stat_avg(st, STAT_DTLB_CACHE, cpu, rsd);
593 ratio = avg / total * 100.0;
595 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
596 out->print_metric(config, out->ctx, color, "%7.2f%%", "of all dTLB cache accesses", ratio);
599 static void print_itlb_cache_misses(struct perf_stat_config *config,
601 struct perf_stat_output_ctx *out,
602 struct runtime_stat *st,
603 struct runtime_stat_data *rsd)
605 double total, ratio = 0.0;
608 total = runtime_stat_avg(st, STAT_ITLB_CACHE, cpu, rsd);
611 ratio = avg / total * 100.0;
613 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
614 out->print_metric(config, out->ctx, color, "%7.2f%%", "of all iTLB cache accesses", ratio);
617 static void print_ll_cache_misses(struct perf_stat_config *config,
619 struct perf_stat_output_ctx *out,
620 struct runtime_stat *st,
621 struct runtime_stat_data *rsd)
623 double total, ratio = 0.0;
626 total = runtime_stat_avg(st, STAT_LL_CACHE, cpu, rsd);
629 ratio = avg / total * 100.0;
631 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
632 out->print_metric(config, out->ctx, color, "%7.2f%%", "of all LL-cache accesses", ratio);
636 * High level "TopDown" CPU core pipe line bottleneck break down.
638 * Basic concept following
639 * Yasin, A Top Down Method for Performance analysis and Counter architecture
642 * The CPU pipeline is divided into 4 areas that can be bottlenecks:
644 * Frontend -> Backend -> Retiring
645 * BadSpeculation in addition means out of order execution that is thrown away
646 * (for example branch mispredictions)
647 * Frontend is instruction decoding.
648 * Backend is execution, like computation and accessing data in memory
649 * Retiring is good execution that is not directly bottlenecked
651 * The formulas are computed in slots.
652 * A slot is an entry in the pipeline each for the pipeline width
653 * (for example a 4-wide pipeline has 4 slots for each cycle)
656 * BadSpeculation = ((SlotsIssued - SlotsRetired) + RecoveryBubbles) /
658 * Retiring = SlotsRetired / TotalSlots
659 * FrontendBound = FetchBubbles / TotalSlots
660 * BackendBound = 1.0 - BadSpeculation - Retiring - FrontendBound
662 * The kernel provides the mapping to the low level CPU events and any scaling
663 * needed for the CPU pipeline width, for example:
665 * TotalSlots = Cycles * 4
667 * The scaling factor is communicated in the sysfs unit.
669 * In some cases the CPU may not be able to measure all the formulas due to
670 * missing events. In this case multiple formulas are combined, as possible.
672 * Full TopDown supports more levels to sub-divide each area: for example
673 * BackendBound into computing bound and memory bound. For now we only
674 * support Level 1 TopDown.
677 static double sanitize_val(double x)
679 if (x < 0 && x >= -0.02)
684 static double td_total_slots(int cpu, struct runtime_stat *st,
685 struct runtime_stat_data *rsd)
687 return runtime_stat_avg(st, STAT_TOPDOWN_TOTAL_SLOTS, cpu, rsd);
690 static double td_bad_spec(int cpu, struct runtime_stat *st,
691 struct runtime_stat_data *rsd)
697 total = runtime_stat_avg(st, STAT_TOPDOWN_SLOTS_ISSUED, cpu, rsd) -
698 runtime_stat_avg(st, STAT_TOPDOWN_SLOTS_RETIRED, cpu, rsd) +
699 runtime_stat_avg(st, STAT_TOPDOWN_RECOVERY_BUBBLES, cpu, rsd);
701 total_slots = td_total_slots(cpu, st, rsd);
703 bad_spec = total / total_slots;
704 return sanitize_val(bad_spec);
707 static double td_retiring(int cpu, struct runtime_stat *st,
708 struct runtime_stat_data *rsd)
711 double total_slots = td_total_slots(cpu, st, rsd);
712 double ret_slots = runtime_stat_avg(st, STAT_TOPDOWN_SLOTS_RETIRED,
716 retiring = ret_slots / total_slots;
720 static double td_fe_bound(int cpu, struct runtime_stat *st,
721 struct runtime_stat_data *rsd)
724 double total_slots = td_total_slots(cpu, st, rsd);
725 double fetch_bub = runtime_stat_avg(st, STAT_TOPDOWN_FETCH_BUBBLES,
729 fe_bound = fetch_bub / total_slots;
733 static double td_be_bound(int cpu, struct runtime_stat *st,
734 struct runtime_stat_data *rsd)
736 double sum = (td_fe_bound(cpu, st, rsd) +
737 td_bad_spec(cpu, st, rsd) +
738 td_retiring(cpu, st, rsd));
741 return sanitize_val(1.0 - sum);
745 * Kernel reports metrics multiplied with slots. To get back
746 * the ratios we need to recreate the sum.
749 static double td_metric_ratio(int cpu, enum stat_type type,
750 struct runtime_stat *stat,
751 struct runtime_stat_data *rsd)
753 double sum = runtime_stat_avg(stat, STAT_TOPDOWN_RETIRING, cpu, rsd) +
754 runtime_stat_avg(stat, STAT_TOPDOWN_FE_BOUND, cpu, rsd) +
755 runtime_stat_avg(stat, STAT_TOPDOWN_BE_BOUND, cpu, rsd) +
756 runtime_stat_avg(stat, STAT_TOPDOWN_BAD_SPEC, cpu, rsd);
757 double d = runtime_stat_avg(stat, type, cpu, rsd);
765 * ... but only if most of the values are actually available.
766 * We allow two missing.
769 static bool full_td(int cpu, struct runtime_stat *stat,
770 struct runtime_stat_data *rsd)
774 if (runtime_stat_avg(stat, STAT_TOPDOWN_RETIRING, cpu, rsd) > 0)
776 if (runtime_stat_avg(stat, STAT_TOPDOWN_BE_BOUND, cpu, rsd) > 0)
778 if (runtime_stat_avg(stat, STAT_TOPDOWN_FE_BOUND, cpu, rsd) > 0)
780 if (runtime_stat_avg(stat, STAT_TOPDOWN_BAD_SPEC, cpu, rsd) > 0)
785 static void print_smi_cost(struct perf_stat_config *config, int cpu,
786 struct perf_stat_output_ctx *out,
787 struct runtime_stat *st,
788 struct runtime_stat_data *rsd)
790 double smi_num, aperf, cycles, cost = 0.0;
791 const char *color = NULL;
793 smi_num = runtime_stat_avg(st, STAT_SMI_NUM, cpu, rsd);
794 aperf = runtime_stat_avg(st, STAT_APERF, cpu, rsd);
795 cycles = runtime_stat_avg(st, STAT_CYCLES, cpu, rsd);
797 if ((cycles == 0) || (aperf == 0))
801 cost = (aperf - cycles) / aperf * 100.00;
804 color = PERF_COLOR_RED;
805 out->print_metric(config, out->ctx, color, "%8.1f%%", "SMI cycles%", cost);
806 out->print_metric(config, out->ctx, NULL, "%4.0f", "SMI#", smi_num);
809 static int prepare_metric(struct evsel **metric_events,
810 struct metric_ref *metric_refs,
811 struct expr_parse_ctx *pctx,
813 struct runtime_stat *st)
819 expr__ctx_init(pctx);
820 for (i = 0; metric_events[i]; i++) {
821 struct saved_value *v;
823 u64 metric_total = 0;
825 if (!strcmp(metric_events[i]->name, "duration_time")) {
826 stats = &walltime_nsecs_stats;
829 v = saved_value_lookup(metric_events[i], cpu, false,
831 metric_events[i]->cgrp);
838 metric_total = v->metric_total;
841 n = strdup(metric_events[i]->name);
845 * This display code with --no-merge adds [cpu] postfixes.
846 * These are not supported by the parser. Remove everything
854 expr__add_id_val(pctx, n, metric_total);
856 expr__add_id_val(pctx, n, avg_stats(stats)*scale);
859 for (j = 0; metric_refs && metric_refs[j].metric_name; j++) {
860 ret = expr__add_ref(pctx, &metric_refs[j]);
868 static void generic_metric(struct perf_stat_config *config,
869 const char *metric_expr,
870 struct evsel **metric_events,
871 struct metric_ref *metric_refs,
873 const char *metric_name,
874 const char *metric_unit,
877 struct perf_stat_output_ctx *out,
878 struct runtime_stat *st)
880 print_metric_t print_metric = out->print_metric;
881 struct expr_parse_ctx pctx;
884 void *ctxp = out->ctx;
886 i = prepare_metric(metric_events, metric_refs, &pctx, cpu, st);
890 if (!metric_events[i]) {
891 if (expr__parse(&ratio, &pctx, metric_expr, runtime) == 0) {
895 if (metric_unit && metric_name) {
896 if (perf_pmu__convert_scale(metric_unit,
897 &unit, &scale) >= 0) {
900 if (strstr(metric_expr, "?"))
901 scnprintf(metric_bf, sizeof(metric_bf),
902 "%s %s_%d", unit, metric_name, runtime);
904 scnprintf(metric_bf, sizeof(metric_bf),
905 "%s %s", unit, metric_name);
907 print_metric(config, ctxp, NULL, "%8.1f",
910 print_metric(config, ctxp, NULL, "%8.2f",
913 out->force_header ? name : "",
917 print_metric(config, ctxp, NULL, NULL,
919 (metric_name ? metric_name : name) : "", 0);
922 print_metric(config, ctxp, NULL, NULL,
924 (metric_name ? metric_name : name) : "", 0);
927 expr__ctx_clear(&pctx);
930 double test_generic_metric(struct metric_expr *mexp, int cpu, struct runtime_stat *st)
932 struct expr_parse_ctx pctx;
935 if (prepare_metric(mexp->metric_events, mexp->metric_refs, &pctx, cpu, st) < 0)
938 if (expr__parse(&ratio, &pctx, mexp->metric_expr, 1))
942 expr__ctx_clear(&pctx);
946 void perf_stat__print_shadow_stats(struct perf_stat_config *config,
949 struct perf_stat_output_ctx *out,
950 struct rblist *metric_events,
951 struct runtime_stat *st)
953 void *ctxp = out->ctx;
954 print_metric_t print_metric = out->print_metric;
955 double total, ratio = 0.0, total2;
956 const char *color = NULL;
957 struct runtime_stat_data rsd = {
958 .ctx = evsel_context(evsel),
961 struct metric_event *me;
964 if (evsel__match(evsel, HARDWARE, HW_INSTRUCTIONS)) {
965 total = runtime_stat_avg(st, STAT_CYCLES, cpu, &rsd);
969 print_metric(config, ctxp, NULL, "%7.2f ",
970 "insn per cycle", ratio);
972 print_metric(config, ctxp, NULL, NULL, "insn per cycle", 0);
975 total = runtime_stat_avg(st, STAT_STALLED_CYCLES_FRONT, cpu, &rsd);
977 total = max(total, runtime_stat_avg(st,
978 STAT_STALLED_CYCLES_BACK,
982 out->new_line(config, ctxp);
984 print_metric(config, ctxp, NULL, "%7.2f ",
985 "stalled cycles per insn",
988 } else if (evsel__match(evsel, HARDWARE, HW_BRANCH_MISSES)) {
989 if (runtime_stat_n(st, STAT_BRANCHES, cpu, &rsd) != 0)
990 print_branch_misses(config, cpu, avg, out, st, &rsd);
992 print_metric(config, ctxp, NULL, NULL, "of all branches", 0);
994 evsel->core.attr.type == PERF_TYPE_HW_CACHE &&
995 evsel->core.attr.config == ( PERF_COUNT_HW_CACHE_L1D |
996 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
997 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
999 if (runtime_stat_n(st, STAT_L1_DCACHE, cpu, &rsd) != 0)
1000 print_l1_dcache_misses(config, cpu, avg, out, st, &rsd);
1002 print_metric(config, ctxp, NULL, NULL, "of all L1-dcache accesses", 0);
1004 evsel->core.attr.type == PERF_TYPE_HW_CACHE &&
1005 evsel->core.attr.config == ( PERF_COUNT_HW_CACHE_L1I |
1006 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
1007 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
1009 if (runtime_stat_n(st, STAT_L1_ICACHE, cpu, &rsd) != 0)
1010 print_l1_icache_misses(config, cpu, avg, out, st, &rsd);
1012 print_metric(config, ctxp, NULL, NULL, "of all L1-icache accesses", 0);
1014 evsel->core.attr.type == PERF_TYPE_HW_CACHE &&
1015 evsel->core.attr.config == ( PERF_COUNT_HW_CACHE_DTLB |
1016 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
1017 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
1019 if (runtime_stat_n(st, STAT_DTLB_CACHE, cpu, &rsd) != 0)
1020 print_dtlb_cache_misses(config, cpu, avg, out, st, &rsd);
1022 print_metric(config, ctxp, NULL, NULL, "of all dTLB cache accesses", 0);
1024 evsel->core.attr.type == PERF_TYPE_HW_CACHE &&
1025 evsel->core.attr.config == ( PERF_COUNT_HW_CACHE_ITLB |
1026 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
1027 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
1029 if (runtime_stat_n(st, STAT_ITLB_CACHE, cpu, &rsd) != 0)
1030 print_itlb_cache_misses(config, cpu, avg, out, st, &rsd);
1032 print_metric(config, ctxp, NULL, NULL, "of all iTLB cache accesses", 0);
1034 evsel->core.attr.type == PERF_TYPE_HW_CACHE &&
1035 evsel->core.attr.config == ( PERF_COUNT_HW_CACHE_LL |
1036 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
1037 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
1039 if (runtime_stat_n(st, STAT_LL_CACHE, cpu, &rsd) != 0)
1040 print_ll_cache_misses(config, cpu, avg, out, st, &rsd);
1042 print_metric(config, ctxp, NULL, NULL, "of all LL-cache accesses", 0);
1043 } else if (evsel__match(evsel, HARDWARE, HW_CACHE_MISSES)) {
1044 total = runtime_stat_avg(st, STAT_CACHEREFS, cpu, &rsd);
1047 ratio = avg * 100 / total;
1049 if (runtime_stat_n(st, STAT_CACHEREFS, cpu, &rsd) != 0)
1050 print_metric(config, ctxp, NULL, "%8.3f %%",
1051 "of all cache refs", ratio);
1053 print_metric(config, ctxp, NULL, NULL, "of all cache refs", 0);
1054 } else if (evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES_FRONTEND)) {
1055 print_stalled_cycles_frontend(config, cpu, avg, out, st, &rsd);
1056 } else if (evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES_BACKEND)) {
1057 print_stalled_cycles_backend(config, cpu, avg, out, st, &rsd);
1058 } else if (evsel__match(evsel, HARDWARE, HW_CPU_CYCLES)) {
1059 total = runtime_stat_avg(st, STAT_NSECS, cpu, &rsd);
1062 ratio = avg / total;
1063 print_metric(config, ctxp, NULL, "%8.3f", "GHz", ratio);
1065 print_metric(config, ctxp, NULL, NULL, "Ghz", 0);
1067 } else if (perf_stat_evsel__is(evsel, CYCLES_IN_TX)) {
1068 total = runtime_stat_avg(st, STAT_CYCLES, cpu, &rsd);
1071 print_metric(config, ctxp, NULL,
1072 "%7.2f%%", "transactional cycles",
1073 100.0 * (avg / total));
1075 print_metric(config, ctxp, NULL, NULL, "transactional cycles",
1077 } else if (perf_stat_evsel__is(evsel, CYCLES_IN_TX_CP)) {
1078 total = runtime_stat_avg(st, STAT_CYCLES, cpu, &rsd);
1079 total2 = runtime_stat_avg(st, STAT_CYCLES_IN_TX, cpu, &rsd);
1084 print_metric(config, ctxp, NULL, "%7.2f%%", "aborted cycles",
1085 100.0 * ((total2-avg) / total));
1087 print_metric(config, ctxp, NULL, NULL, "aborted cycles", 0);
1088 } else if (perf_stat_evsel__is(evsel, TRANSACTION_START)) {
1089 total = runtime_stat_avg(st, STAT_CYCLES_IN_TX, cpu, &rsd);
1092 ratio = total / avg;
1094 if (runtime_stat_n(st, STAT_CYCLES_IN_TX, cpu, &rsd) != 0)
1095 print_metric(config, ctxp, NULL, "%8.0f",
1096 "cycles / transaction", ratio);
1098 print_metric(config, ctxp, NULL, NULL, "cycles / transaction",
1100 } else if (perf_stat_evsel__is(evsel, ELISION_START)) {
1101 total = runtime_stat_avg(st, STAT_CYCLES_IN_TX, cpu, &rsd);
1104 ratio = total / avg;
1106 print_metric(config, ctxp, NULL, "%8.0f", "cycles / elision", ratio);
1107 } else if (evsel__is_clock(evsel)) {
1108 if ((ratio = avg_stats(&walltime_nsecs_stats)) != 0)
1109 print_metric(config, ctxp, NULL, "%8.3f", "CPUs utilized",
1110 avg / (ratio * evsel->scale));
1112 print_metric(config, ctxp, NULL, NULL, "CPUs utilized", 0);
1113 } else if (perf_stat_evsel__is(evsel, TOPDOWN_FETCH_BUBBLES)) {
1114 double fe_bound = td_fe_bound(cpu, st, &rsd);
1117 color = PERF_COLOR_RED;
1118 print_metric(config, ctxp, color, "%8.1f%%", "frontend bound",
1120 } else if (perf_stat_evsel__is(evsel, TOPDOWN_SLOTS_RETIRED)) {
1121 double retiring = td_retiring(cpu, st, &rsd);
1124 color = PERF_COLOR_GREEN;
1125 print_metric(config, ctxp, color, "%8.1f%%", "retiring",
1127 } else if (perf_stat_evsel__is(evsel, TOPDOWN_RECOVERY_BUBBLES)) {
1128 double bad_spec = td_bad_spec(cpu, st, &rsd);
1131 color = PERF_COLOR_RED;
1132 print_metric(config, ctxp, color, "%8.1f%%", "bad speculation",
1134 } else if (perf_stat_evsel__is(evsel, TOPDOWN_SLOTS_ISSUED)) {
1135 double be_bound = td_be_bound(cpu, st, &rsd);
1136 const char *name = "backend bound";
1137 static int have_recovery_bubbles = -1;
1139 /* In case the CPU does not support topdown-recovery-bubbles */
1140 if (have_recovery_bubbles < 0)
1141 have_recovery_bubbles = pmu_have_event("cpu",
1142 "topdown-recovery-bubbles");
1143 if (!have_recovery_bubbles)
1144 name = "backend bound/bad spec";
1147 color = PERF_COLOR_RED;
1148 if (td_total_slots(cpu, st, &rsd) > 0)
1149 print_metric(config, ctxp, color, "%8.1f%%", name,
1152 print_metric(config, ctxp, NULL, NULL, name, 0);
1153 } else if (perf_stat_evsel__is(evsel, TOPDOWN_RETIRING) &&
1154 full_td(cpu, st, &rsd)) {
1155 double retiring = td_metric_ratio(cpu,
1156 STAT_TOPDOWN_RETIRING, st,
1159 color = PERF_COLOR_GREEN;
1160 print_metric(config, ctxp, color, "%8.1f%%", "retiring",
1162 } else if (perf_stat_evsel__is(evsel, TOPDOWN_FE_BOUND) &&
1163 full_td(cpu, st, &rsd)) {
1164 double fe_bound = td_metric_ratio(cpu,
1165 STAT_TOPDOWN_FE_BOUND, st,
1168 color = PERF_COLOR_RED;
1169 print_metric(config, ctxp, color, "%8.1f%%", "frontend bound",
1171 } else if (perf_stat_evsel__is(evsel, TOPDOWN_BE_BOUND) &&
1172 full_td(cpu, st, &rsd)) {
1173 double be_bound = td_metric_ratio(cpu,
1174 STAT_TOPDOWN_BE_BOUND, st,
1177 color = PERF_COLOR_RED;
1178 print_metric(config, ctxp, color, "%8.1f%%", "backend bound",
1180 } else if (perf_stat_evsel__is(evsel, TOPDOWN_BAD_SPEC) &&
1181 full_td(cpu, st, &rsd)) {
1182 double bad_spec = td_metric_ratio(cpu,
1183 STAT_TOPDOWN_BAD_SPEC, st,
1186 color = PERF_COLOR_RED;
1187 print_metric(config, ctxp, color, "%8.1f%%", "bad speculation",
1189 } else if (perf_stat_evsel__is(evsel, TOPDOWN_HEAVY_OPS) &&
1190 full_td(cpu, st, &rsd) && (config->topdown_level > 1)) {
1191 double retiring = td_metric_ratio(cpu,
1192 STAT_TOPDOWN_RETIRING, st,
1194 double heavy_ops = td_metric_ratio(cpu,
1195 STAT_TOPDOWN_HEAVY_OPS, st,
1197 double light_ops = retiring - heavy_ops;
1199 if (retiring > 0.7 && heavy_ops > 0.1)
1200 color = PERF_COLOR_GREEN;
1201 print_metric(config, ctxp, color, "%8.1f%%", "heavy operations",
1203 if (retiring > 0.7 && light_ops > 0.6)
1204 color = PERF_COLOR_GREEN;
1207 print_metric(config, ctxp, color, "%8.1f%%", "light operations",
1209 } else if (perf_stat_evsel__is(evsel, TOPDOWN_BR_MISPREDICT) &&
1210 full_td(cpu, st, &rsd) && (config->topdown_level > 1)) {
1211 double bad_spec = td_metric_ratio(cpu,
1212 STAT_TOPDOWN_BAD_SPEC, st,
1214 double br_mis = td_metric_ratio(cpu,
1215 STAT_TOPDOWN_BR_MISPREDICT, st,
1217 double m_clears = bad_spec - br_mis;
1219 if (bad_spec > 0.1 && br_mis > 0.05)
1220 color = PERF_COLOR_RED;
1221 print_metric(config, ctxp, color, "%8.1f%%", "branch mispredict",
1223 if (bad_spec > 0.1 && m_clears > 0.05)
1224 color = PERF_COLOR_RED;
1227 print_metric(config, ctxp, color, "%8.1f%%", "machine clears",
1229 } else if (perf_stat_evsel__is(evsel, TOPDOWN_FETCH_LAT) &&
1230 full_td(cpu, st, &rsd) && (config->topdown_level > 1)) {
1231 double fe_bound = td_metric_ratio(cpu,
1232 STAT_TOPDOWN_FE_BOUND, st,
1234 double fetch_lat = td_metric_ratio(cpu,
1235 STAT_TOPDOWN_FETCH_LAT, st,
1237 double fetch_bw = fe_bound - fetch_lat;
1239 if (fe_bound > 0.2 && fetch_lat > 0.15)
1240 color = PERF_COLOR_RED;
1241 print_metric(config, ctxp, color, "%8.1f%%", "fetch latency",
1243 if (fe_bound > 0.2 && fetch_bw > 0.1)
1244 color = PERF_COLOR_RED;
1247 print_metric(config, ctxp, color, "%8.1f%%", "fetch bandwidth",
1249 } else if (perf_stat_evsel__is(evsel, TOPDOWN_MEM_BOUND) &&
1250 full_td(cpu, st, &rsd) && (config->topdown_level > 1)) {
1251 double be_bound = td_metric_ratio(cpu,
1252 STAT_TOPDOWN_BE_BOUND, st,
1254 double mem_bound = td_metric_ratio(cpu,
1255 STAT_TOPDOWN_MEM_BOUND, st,
1257 double core_bound = be_bound - mem_bound;
1259 if (be_bound > 0.2 && mem_bound > 0.2)
1260 color = PERF_COLOR_RED;
1261 print_metric(config, ctxp, color, "%8.1f%%", "memory bound",
1263 if (be_bound > 0.2 && core_bound > 0.1)
1264 color = PERF_COLOR_RED;
1267 print_metric(config, ctxp, color, "%8.1f%%", "Core bound",
1269 } else if (evsel->metric_expr) {
1270 generic_metric(config, evsel->metric_expr, evsel->metric_events, NULL,
1271 evsel->name, evsel->metric_name, NULL, 1, cpu, out, st);
1272 } else if (runtime_stat_n(st, STAT_NSECS, cpu, &rsd) != 0) {
1276 total = runtime_stat_avg(st, STAT_NSECS, cpu, &rsd);
1279 ratio = 1000.0 * avg / total;
1280 if (ratio < 0.001) {
1284 snprintf(unit_buf, sizeof(unit_buf), "%c/sec", unit);
1285 print_metric(config, ctxp, NULL, "%8.3f", unit_buf, ratio);
1286 } else if (perf_stat_evsel__is(evsel, SMI_NUM)) {
1287 print_smi_cost(config, cpu, out, st, &rsd);
1292 if ((me = metricgroup__lookup(metric_events, evsel, false)) != NULL) {
1293 struct metric_expr *mexp;
1295 list_for_each_entry (mexp, &me->head, nd) {
1297 out->new_line(config, ctxp);
1298 generic_metric(config, mexp->metric_expr, mexp->metric_events,
1299 mexp->metric_refs, evsel->name, mexp->metric_name,
1300 mexp->metric_unit, mexp->runtime, cpu, out, st);
1304 print_metric(config, ctxp, NULL, NULL, NULL, 0);