1 // SPDX-License-Identifier: GPL-2.0
10 #include "metricgroup.h"
13 * AGGR_GLOBAL: Use CPU 0
14 * AGGR_SOCKET: Use first CPU of socket
15 * AGGR_DIE: Use first CPU of die
16 * AGGR_CORE: Use first CPU of core
17 * AGGR_NONE: Use matching CPU
18 * AGGR_THREAD: Not supported?
20 static bool have_frontend_stalled;
22 struct runtime_stat rt_stat;
23 struct stats walltime_nsecs_stats;
26 struct rb_node rb_node;
27 struct perf_evsel *evsel;
31 struct runtime_stat *stat;
35 static int saved_value_cmp(struct rb_node *rb_node, const void *entry)
37 struct saved_value *a = container_of(rb_node,
40 const struct saved_value *b = entry;
43 return a->cpu - b->cpu;
46 * Previously the rbtree was used to link generic metrics.
47 * The keys were evsel/cpu. Now the rbtree is extended to support
48 * per-thread shadow stats. For shadow stats case, the keys
49 * are cpu/type/ctx/stat (evsel is NULL). For generic metrics
50 * case, the keys are still evsel/cpu (type/ctx/stat are 0 or NULL).
52 if (a->type != b->type)
53 return a->type - b->type;
56 return a->ctx - b->ctx;
58 if (a->evsel == NULL && b->evsel == NULL) {
59 if (a->stat == b->stat)
62 if ((char *)a->stat < (char *)b->stat)
68 if (a->evsel == b->evsel)
70 if ((char *)a->evsel < (char *)b->evsel)
75 static struct rb_node *saved_value_new(struct rblist *rblist __maybe_unused,
78 struct saved_value *nd = malloc(sizeof(struct saved_value));
82 memcpy(nd, entry, sizeof(struct saved_value));
86 static void saved_value_delete(struct rblist *rblist __maybe_unused,
87 struct rb_node *rb_node)
89 struct saved_value *v;
92 v = container_of(rb_node, struct saved_value, rb_node);
96 static struct saved_value *saved_value_lookup(struct perf_evsel *evsel,
101 struct runtime_stat *st)
103 struct rblist *rblist;
105 struct saved_value dm = {
113 rblist = &st->value_list;
115 nd = rblist__find(rblist, &dm);
117 return container_of(nd, struct saved_value, rb_node);
119 rblist__add_node(rblist, &dm);
120 nd = rblist__find(rblist, &dm);
122 return container_of(nd, struct saved_value, rb_node);
127 void runtime_stat__init(struct runtime_stat *st)
129 struct rblist *rblist = &st->value_list;
131 rblist__init(rblist);
132 rblist->node_cmp = saved_value_cmp;
133 rblist->node_new = saved_value_new;
134 rblist->node_delete = saved_value_delete;
137 void runtime_stat__exit(struct runtime_stat *st)
139 rblist__exit(&st->value_list);
142 void perf_stat__init_shadow_stats(void)
144 have_frontend_stalled = pmu_have_event("cpu", "stalled-cycles-frontend");
145 runtime_stat__init(&rt_stat);
148 static int evsel_context(struct perf_evsel *evsel)
152 if (evsel->attr.exclude_kernel)
153 ctx |= CTX_BIT_KERNEL;
154 if (evsel->attr.exclude_user)
156 if (evsel->attr.exclude_hv)
158 if (evsel->attr.exclude_host)
160 if (evsel->attr.exclude_idle)
166 static void reset_stat(struct runtime_stat *st)
168 struct rblist *rblist;
169 struct rb_node *pos, *next;
171 rblist = &st->value_list;
172 next = rb_first_cached(&rblist->entries);
176 memset(&container_of(pos, struct saved_value, rb_node)->stats,
178 sizeof(struct stats));
182 void perf_stat__reset_shadow_stats(void)
184 reset_stat(&rt_stat);
185 memset(&walltime_nsecs_stats, 0, sizeof(walltime_nsecs_stats));
188 void perf_stat__reset_shadow_per_stat(struct runtime_stat *st)
193 static void update_runtime_stat(struct runtime_stat *st,
195 int ctx, int cpu, u64 count)
197 struct saved_value *v = saved_value_lookup(NULL, cpu, true,
201 update_stats(&v->stats, count);
205 * Update various tracking values we maintain to print
206 * more semantic information such as miss/hit ratios,
207 * instruction rates, etc:
209 void perf_stat__update_shadow_stats(struct perf_evsel *counter, u64 count,
210 int cpu, struct runtime_stat *st)
212 int ctx = evsel_context(counter);
213 u64 count_ns = count;
215 count *= counter->scale;
217 if (perf_evsel__is_clock(counter))
218 update_runtime_stat(st, STAT_NSECS, 0, cpu, count_ns);
219 else if (perf_evsel__match(counter, HARDWARE, HW_CPU_CYCLES))
220 update_runtime_stat(st, STAT_CYCLES, ctx, cpu, count);
221 else if (perf_stat_evsel__is(counter, CYCLES_IN_TX))
222 update_runtime_stat(st, STAT_CYCLES_IN_TX, ctx, cpu, count);
223 else if (perf_stat_evsel__is(counter, TRANSACTION_START))
224 update_runtime_stat(st, STAT_TRANSACTION, ctx, cpu, count);
225 else if (perf_stat_evsel__is(counter, ELISION_START))
226 update_runtime_stat(st, STAT_ELISION, ctx, cpu, count);
227 else if (perf_stat_evsel__is(counter, TOPDOWN_TOTAL_SLOTS))
228 update_runtime_stat(st, STAT_TOPDOWN_TOTAL_SLOTS,
230 else if (perf_stat_evsel__is(counter, TOPDOWN_SLOTS_ISSUED))
231 update_runtime_stat(st, STAT_TOPDOWN_SLOTS_ISSUED,
233 else if (perf_stat_evsel__is(counter, TOPDOWN_SLOTS_RETIRED))
234 update_runtime_stat(st, STAT_TOPDOWN_SLOTS_RETIRED,
236 else if (perf_stat_evsel__is(counter, TOPDOWN_FETCH_BUBBLES))
237 update_runtime_stat(st, STAT_TOPDOWN_FETCH_BUBBLES,
239 else if (perf_stat_evsel__is(counter, TOPDOWN_RECOVERY_BUBBLES))
240 update_runtime_stat(st, STAT_TOPDOWN_RECOVERY_BUBBLES,
242 else if (perf_evsel__match(counter, HARDWARE, HW_STALLED_CYCLES_FRONTEND))
243 update_runtime_stat(st, STAT_STALLED_CYCLES_FRONT,
245 else if (perf_evsel__match(counter, HARDWARE, HW_STALLED_CYCLES_BACKEND))
246 update_runtime_stat(st, STAT_STALLED_CYCLES_BACK,
248 else if (perf_evsel__match(counter, HARDWARE, HW_BRANCH_INSTRUCTIONS))
249 update_runtime_stat(st, STAT_BRANCHES, ctx, cpu, count);
250 else if (perf_evsel__match(counter, HARDWARE, HW_CACHE_REFERENCES))
251 update_runtime_stat(st, STAT_CACHEREFS, ctx, cpu, count);
252 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_L1D))
253 update_runtime_stat(st, STAT_L1_DCACHE, ctx, cpu, count);
254 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_L1I))
255 update_runtime_stat(st, STAT_L1_ICACHE, ctx, cpu, count);
256 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_LL))
257 update_runtime_stat(st, STAT_LL_CACHE, ctx, cpu, count);
258 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_DTLB))
259 update_runtime_stat(st, STAT_DTLB_CACHE, ctx, cpu, count);
260 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_ITLB))
261 update_runtime_stat(st, STAT_ITLB_CACHE, ctx, cpu, count);
262 else if (perf_stat_evsel__is(counter, SMI_NUM))
263 update_runtime_stat(st, STAT_SMI_NUM, ctx, cpu, count);
264 else if (perf_stat_evsel__is(counter, APERF))
265 update_runtime_stat(st, STAT_APERF, ctx, cpu, count);
267 if (counter->collect_stat) {
268 struct saved_value *v = saved_value_lookup(counter, cpu, true,
270 update_stats(&v->stats, count);
274 /* used for get_ratio_color() */
276 GRC_STALLED_CYCLES_FE,
277 GRC_STALLED_CYCLES_BE,
282 static const char *get_ratio_color(enum grc_type type, double ratio)
284 static const double grc_table[GRC_MAX_NR][3] = {
285 [GRC_STALLED_CYCLES_FE] = { 50.0, 30.0, 10.0 },
286 [GRC_STALLED_CYCLES_BE] = { 75.0, 50.0, 20.0 },
287 [GRC_CACHE_MISSES] = { 20.0, 10.0, 5.0 },
289 const char *color = PERF_COLOR_NORMAL;
291 if (ratio > grc_table[type][0])
292 color = PERF_COLOR_RED;
293 else if (ratio > grc_table[type][1])
294 color = PERF_COLOR_MAGENTA;
295 else if (ratio > grc_table[type][2])
296 color = PERF_COLOR_YELLOW;
301 static struct perf_evsel *perf_stat__find_event(struct perf_evlist *evsel_list,
304 struct perf_evsel *c2;
306 evlist__for_each_entry (evsel_list, c2) {
307 if (!strcasecmp(c2->name, name) && !c2->collect_stat)
313 /* Mark MetricExpr target events and link events using them to them. */
314 void perf_stat__collect_metric_expr(struct perf_evlist *evsel_list)
316 struct perf_evsel *counter, *leader, **metric_events, *oc;
318 const char **metric_names;
320 int num_metric_names;
322 evlist__for_each_entry(evsel_list, counter) {
323 bool invalid = false;
325 leader = counter->leader;
326 if (!counter->metric_expr)
328 metric_events = counter->metric_events;
329 if (!metric_events) {
330 if (expr__find_other(counter->metric_expr, counter->name,
331 &metric_names, &num_metric_names) < 0)
334 metric_events = calloc(sizeof(struct perf_evsel *),
335 num_metric_names + 1);
338 counter->metric_events = metric_events;
341 for (i = 0; i < num_metric_names; i++) {
344 /* Search in group */
345 for_each_group_member (oc, leader) {
346 if (!strcasecmp(oc->name, metric_names[i]) &&
354 /* Search ignoring groups */
355 oc = perf_stat__find_event(evsel_list, metric_names[i]);
358 /* Deduping one is good enough to handle duplicated PMUs. */
359 static char *printed;
362 * Adding events automatically would be difficult, because
363 * it would risk creating groups that are not schedulable.
364 * perf stat doesn't understand all the scheduling constraints
365 * of events. So we ask the user instead to add the missing
368 if (!printed || strcasecmp(printed, metric_names[i])) {
370 "Add %s event to groups to get metric expression for %s\n",
373 printed = strdup(metric_names[i]);
378 metric_events[i] = oc;
379 oc->collect_stat = true;
381 metric_events[i] = NULL;
385 counter->metric_events = NULL;
386 counter->metric_expr = NULL;
391 static double runtime_stat_avg(struct runtime_stat *st,
392 enum stat_type type, int ctx, int cpu)
394 struct saved_value *v;
396 v = saved_value_lookup(NULL, cpu, false, type, ctx, st);
400 return avg_stats(&v->stats);
403 static double runtime_stat_n(struct runtime_stat *st,
404 enum stat_type type, int ctx, int cpu)
406 struct saved_value *v;
408 v = saved_value_lookup(NULL, cpu, false, type, ctx, st);
415 static void print_stalled_cycles_frontend(struct perf_stat_config *config,
417 struct perf_evsel *evsel, double avg,
418 struct perf_stat_output_ctx *out,
419 struct runtime_stat *st)
421 double total, ratio = 0.0;
423 int ctx = evsel_context(evsel);
425 total = runtime_stat_avg(st, STAT_CYCLES, ctx, cpu);
428 ratio = avg / total * 100.0;
430 color = get_ratio_color(GRC_STALLED_CYCLES_FE, ratio);
433 out->print_metric(config, out->ctx, color, "%7.2f%%", "frontend cycles idle",
436 out->print_metric(config, out->ctx, NULL, NULL, "frontend cycles idle", 0);
439 static void print_stalled_cycles_backend(struct perf_stat_config *config,
441 struct perf_evsel *evsel, double avg,
442 struct perf_stat_output_ctx *out,
443 struct runtime_stat *st)
445 double total, ratio = 0.0;
447 int ctx = evsel_context(evsel);
449 total = runtime_stat_avg(st, STAT_CYCLES, ctx, cpu);
452 ratio = avg / total * 100.0;
454 color = get_ratio_color(GRC_STALLED_CYCLES_BE, ratio);
456 out->print_metric(config, out->ctx, color, "%7.2f%%", "backend cycles idle", ratio);
459 static void print_branch_misses(struct perf_stat_config *config,
461 struct perf_evsel *evsel,
463 struct perf_stat_output_ctx *out,
464 struct runtime_stat *st)
466 double total, ratio = 0.0;
468 int ctx = evsel_context(evsel);
470 total = runtime_stat_avg(st, STAT_BRANCHES, ctx, cpu);
473 ratio = avg / total * 100.0;
475 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
477 out->print_metric(config, out->ctx, color, "%7.2f%%", "of all branches", ratio);
480 static void print_l1_dcache_misses(struct perf_stat_config *config,
482 struct perf_evsel *evsel,
484 struct perf_stat_output_ctx *out,
485 struct runtime_stat *st)
488 double total, ratio = 0.0;
490 int ctx = evsel_context(evsel);
492 total = runtime_stat_avg(st, STAT_L1_DCACHE, ctx, cpu);
495 ratio = avg / total * 100.0;
497 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
499 out->print_metric(config, out->ctx, color, "%7.2f%%", "of all L1-dcache hits", ratio);
502 static void print_l1_icache_misses(struct perf_stat_config *config,
504 struct perf_evsel *evsel,
506 struct perf_stat_output_ctx *out,
507 struct runtime_stat *st)
510 double total, ratio = 0.0;
512 int ctx = evsel_context(evsel);
514 total = runtime_stat_avg(st, STAT_L1_ICACHE, ctx, cpu);
517 ratio = avg / total * 100.0;
519 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
520 out->print_metric(config, out->ctx, color, "%7.2f%%", "of all L1-icache hits", ratio);
523 static void print_dtlb_cache_misses(struct perf_stat_config *config,
525 struct perf_evsel *evsel,
527 struct perf_stat_output_ctx *out,
528 struct runtime_stat *st)
530 double total, ratio = 0.0;
532 int ctx = evsel_context(evsel);
534 total = runtime_stat_avg(st, STAT_DTLB_CACHE, ctx, cpu);
537 ratio = avg / total * 100.0;
539 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
540 out->print_metric(config, out->ctx, color, "%7.2f%%", "of all dTLB cache hits", ratio);
543 static void print_itlb_cache_misses(struct perf_stat_config *config,
545 struct perf_evsel *evsel,
547 struct perf_stat_output_ctx *out,
548 struct runtime_stat *st)
550 double total, ratio = 0.0;
552 int ctx = evsel_context(evsel);
554 total = runtime_stat_avg(st, STAT_ITLB_CACHE, ctx, cpu);
557 ratio = avg / total * 100.0;
559 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
560 out->print_metric(config, out->ctx, color, "%7.2f%%", "of all iTLB cache hits", ratio);
563 static void print_ll_cache_misses(struct perf_stat_config *config,
565 struct perf_evsel *evsel,
567 struct perf_stat_output_ctx *out,
568 struct runtime_stat *st)
570 double total, ratio = 0.0;
572 int ctx = evsel_context(evsel);
574 total = runtime_stat_avg(st, STAT_LL_CACHE, ctx, cpu);
577 ratio = avg / total * 100.0;
579 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
580 out->print_metric(config, out->ctx, color, "%7.2f%%", "of all LL-cache hits", ratio);
584 * High level "TopDown" CPU core pipe line bottleneck break down.
586 * Basic concept following
587 * Yasin, A Top Down Method for Performance analysis and Counter architecture
590 * The CPU pipeline is divided into 4 areas that can be bottlenecks:
592 * Frontend -> Backend -> Retiring
593 * BadSpeculation in addition means out of order execution that is thrown away
594 * (for example branch mispredictions)
595 * Frontend is instruction decoding.
596 * Backend is execution, like computation and accessing data in memory
597 * Retiring is good execution that is not directly bottlenecked
599 * The formulas are computed in slots.
600 * A slot is an entry in the pipeline each for the pipeline width
601 * (for example a 4-wide pipeline has 4 slots for each cycle)
604 * BadSpeculation = ((SlotsIssued - SlotsRetired) + RecoveryBubbles) /
606 * Retiring = SlotsRetired / TotalSlots
607 * FrontendBound = FetchBubbles / TotalSlots
608 * BackendBound = 1.0 - BadSpeculation - Retiring - FrontendBound
610 * The kernel provides the mapping to the low level CPU events and any scaling
611 * needed for the CPU pipeline width, for example:
613 * TotalSlots = Cycles * 4
615 * The scaling factor is communicated in the sysfs unit.
617 * In some cases the CPU may not be able to measure all the formulas due to
618 * missing events. In this case multiple formulas are combined, as possible.
620 * Full TopDown supports more levels to sub-divide each area: for example
621 * BackendBound into computing bound and memory bound. For now we only
622 * support Level 1 TopDown.
625 static double sanitize_val(double x)
627 if (x < 0 && x >= -0.02)
632 static double td_total_slots(int ctx, int cpu, struct runtime_stat *st)
634 return runtime_stat_avg(st, STAT_TOPDOWN_TOTAL_SLOTS, ctx, cpu);
637 static double td_bad_spec(int ctx, int cpu, struct runtime_stat *st)
643 total = runtime_stat_avg(st, STAT_TOPDOWN_SLOTS_ISSUED, ctx, cpu) -
644 runtime_stat_avg(st, STAT_TOPDOWN_SLOTS_RETIRED, ctx, cpu) +
645 runtime_stat_avg(st, STAT_TOPDOWN_RECOVERY_BUBBLES, ctx, cpu);
647 total_slots = td_total_slots(ctx, cpu, st);
649 bad_spec = total / total_slots;
650 return sanitize_val(bad_spec);
653 static double td_retiring(int ctx, int cpu, struct runtime_stat *st)
656 double total_slots = td_total_slots(ctx, cpu, st);
657 double ret_slots = runtime_stat_avg(st, STAT_TOPDOWN_SLOTS_RETIRED,
661 retiring = ret_slots / total_slots;
665 static double td_fe_bound(int ctx, int cpu, struct runtime_stat *st)
668 double total_slots = td_total_slots(ctx, cpu, st);
669 double fetch_bub = runtime_stat_avg(st, STAT_TOPDOWN_FETCH_BUBBLES,
673 fe_bound = fetch_bub / total_slots;
677 static double td_be_bound(int ctx, int cpu, struct runtime_stat *st)
679 double sum = (td_fe_bound(ctx, cpu, st) +
680 td_bad_spec(ctx, cpu, st) +
681 td_retiring(ctx, cpu, st));
684 return sanitize_val(1.0 - sum);
687 static void print_smi_cost(struct perf_stat_config *config,
688 int cpu, struct perf_evsel *evsel,
689 struct perf_stat_output_ctx *out,
690 struct runtime_stat *st)
692 double smi_num, aperf, cycles, cost = 0.0;
693 int ctx = evsel_context(evsel);
694 const char *color = NULL;
696 smi_num = runtime_stat_avg(st, STAT_SMI_NUM, ctx, cpu);
697 aperf = runtime_stat_avg(st, STAT_APERF, ctx, cpu);
698 cycles = runtime_stat_avg(st, STAT_CYCLES, ctx, cpu);
700 if ((cycles == 0) || (aperf == 0))
704 cost = (aperf - cycles) / aperf * 100.00;
707 color = PERF_COLOR_RED;
708 out->print_metric(config, out->ctx, color, "%8.1f%%", "SMI cycles%", cost);
709 out->print_metric(config, out->ctx, NULL, "%4.0f", "SMI#", smi_num);
712 static void generic_metric(struct perf_stat_config *config,
713 const char *metric_expr,
714 struct perf_evsel **metric_events,
716 const char *metric_name,
719 struct perf_stat_output_ctx *out,
720 struct runtime_stat *st)
722 print_metric_t print_metric = out->print_metric;
723 struct parse_ctx pctx;
726 void *ctxp = out->ctx;
729 expr__ctx_init(&pctx);
730 expr__add_id(&pctx, name, avg);
731 for (i = 0; metric_events[i]; i++) {
732 struct saved_value *v;
736 if (!strcmp(metric_events[i]->name, "duration_time")) {
737 stats = &walltime_nsecs_stats;
740 v = saved_value_lookup(metric_events[i], cpu, false,
748 n = strdup(metric_events[i]->name);
752 * This display code with --no-merge adds [cpu] postfixes.
753 * These are not supported by the parser. Remove everything
759 expr__add_id(&pctx, n, avg_stats(stats)*scale);
761 if (!metric_events[i]) {
762 const char *p = metric_expr;
764 if (expr__parse(&ratio, &pctx, &p) == 0)
765 print_metric(config, ctxp, NULL, "%8.1f",
768 out->force_header ? name : "",
771 print_metric(config, ctxp, NULL, NULL,
773 (metric_name ? metric_name : name) : "", 0);
775 print_metric(config, ctxp, NULL, NULL, "", 0);
777 for (i = 1; i < pctx.num_ids; i++)
778 free((void *)pctx.ids[i].name);
781 void perf_stat__print_shadow_stats(struct perf_stat_config *config,
782 struct perf_evsel *evsel,
784 struct perf_stat_output_ctx *out,
785 struct rblist *metric_events,
786 struct runtime_stat *st)
788 void *ctxp = out->ctx;
789 print_metric_t print_metric = out->print_metric;
790 double total, ratio = 0.0, total2;
791 const char *color = NULL;
792 int ctx = evsel_context(evsel);
793 struct metric_event *me;
796 if (perf_evsel__match(evsel, HARDWARE, HW_INSTRUCTIONS)) {
797 total = runtime_stat_avg(st, STAT_CYCLES, ctx, cpu);
801 print_metric(config, ctxp, NULL, "%7.2f ",
802 "insn per cycle", ratio);
804 print_metric(config, ctxp, NULL, NULL, "insn per cycle", 0);
807 total = runtime_stat_avg(st, STAT_STALLED_CYCLES_FRONT,
810 total = max(total, runtime_stat_avg(st,
811 STAT_STALLED_CYCLES_BACK,
815 out->new_line(config, ctxp);
817 print_metric(config, ctxp, NULL, "%7.2f ",
818 "stalled cycles per insn",
820 } else if (have_frontend_stalled) {
821 print_metric(config, ctxp, NULL, NULL,
822 "stalled cycles per insn", 0);
824 } else if (perf_evsel__match(evsel, HARDWARE, HW_BRANCH_MISSES)) {
825 if (runtime_stat_n(st, STAT_BRANCHES, ctx, cpu) != 0)
826 print_branch_misses(config, cpu, evsel, avg, out, st);
828 print_metric(config, ctxp, NULL, NULL, "of all branches", 0);
830 evsel->attr.type == PERF_TYPE_HW_CACHE &&
831 evsel->attr.config == ( PERF_COUNT_HW_CACHE_L1D |
832 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
833 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
835 if (runtime_stat_n(st, STAT_L1_DCACHE, ctx, cpu) != 0)
836 print_l1_dcache_misses(config, cpu, evsel, avg, out, st);
838 print_metric(config, ctxp, NULL, NULL, "of all L1-dcache hits", 0);
840 evsel->attr.type == PERF_TYPE_HW_CACHE &&
841 evsel->attr.config == ( PERF_COUNT_HW_CACHE_L1I |
842 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
843 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
845 if (runtime_stat_n(st, STAT_L1_ICACHE, ctx, cpu) != 0)
846 print_l1_icache_misses(config, cpu, evsel, avg, out, st);
848 print_metric(config, ctxp, NULL, NULL, "of all L1-icache hits", 0);
850 evsel->attr.type == PERF_TYPE_HW_CACHE &&
851 evsel->attr.config == ( PERF_COUNT_HW_CACHE_DTLB |
852 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
853 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
855 if (runtime_stat_n(st, STAT_DTLB_CACHE, ctx, cpu) != 0)
856 print_dtlb_cache_misses(config, cpu, evsel, avg, out, st);
858 print_metric(config, ctxp, NULL, NULL, "of all dTLB cache hits", 0);
860 evsel->attr.type == PERF_TYPE_HW_CACHE &&
861 evsel->attr.config == ( PERF_COUNT_HW_CACHE_ITLB |
862 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
863 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
865 if (runtime_stat_n(st, STAT_ITLB_CACHE, ctx, cpu) != 0)
866 print_itlb_cache_misses(config, cpu, evsel, avg, out, st);
868 print_metric(config, ctxp, NULL, NULL, "of all iTLB cache hits", 0);
870 evsel->attr.type == PERF_TYPE_HW_CACHE &&
871 evsel->attr.config == ( PERF_COUNT_HW_CACHE_LL |
872 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
873 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
875 if (runtime_stat_n(st, STAT_LL_CACHE, ctx, cpu) != 0)
876 print_ll_cache_misses(config, cpu, evsel, avg, out, st);
878 print_metric(config, ctxp, NULL, NULL, "of all LL-cache hits", 0);
879 } else if (perf_evsel__match(evsel, HARDWARE, HW_CACHE_MISSES)) {
880 total = runtime_stat_avg(st, STAT_CACHEREFS, ctx, cpu);
883 ratio = avg * 100 / total;
885 if (runtime_stat_n(st, STAT_CACHEREFS, ctx, cpu) != 0)
886 print_metric(config, ctxp, NULL, "%8.3f %%",
887 "of all cache refs", ratio);
889 print_metric(config, ctxp, NULL, NULL, "of all cache refs", 0);
890 } else if (perf_evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES_FRONTEND)) {
891 print_stalled_cycles_frontend(config, cpu, evsel, avg, out, st);
892 } else if (perf_evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES_BACKEND)) {
893 print_stalled_cycles_backend(config, cpu, evsel, avg, out, st);
894 } else if (perf_evsel__match(evsel, HARDWARE, HW_CPU_CYCLES)) {
895 total = runtime_stat_avg(st, STAT_NSECS, 0, cpu);
899 print_metric(config, ctxp, NULL, "%8.3f", "GHz", ratio);
901 print_metric(config, ctxp, NULL, NULL, "Ghz", 0);
903 } else if (perf_stat_evsel__is(evsel, CYCLES_IN_TX)) {
904 total = runtime_stat_avg(st, STAT_CYCLES, ctx, cpu);
907 print_metric(config, ctxp, NULL,
908 "%7.2f%%", "transactional cycles",
909 100.0 * (avg / total));
911 print_metric(config, ctxp, NULL, NULL, "transactional cycles",
913 } else if (perf_stat_evsel__is(evsel, CYCLES_IN_TX_CP)) {
914 total = runtime_stat_avg(st, STAT_CYCLES, ctx, cpu);
915 total2 = runtime_stat_avg(st, STAT_CYCLES_IN_TX, ctx, cpu);
920 print_metric(config, ctxp, NULL, "%7.2f%%", "aborted cycles",
921 100.0 * ((total2-avg) / total));
923 print_metric(config, ctxp, NULL, NULL, "aborted cycles", 0);
924 } else if (perf_stat_evsel__is(evsel, TRANSACTION_START)) {
925 total = runtime_stat_avg(st, STAT_CYCLES_IN_TX,
931 if (runtime_stat_n(st, STAT_CYCLES_IN_TX, ctx, cpu) != 0)
932 print_metric(config, ctxp, NULL, "%8.0f",
933 "cycles / transaction", ratio);
935 print_metric(config, ctxp, NULL, NULL, "cycles / transaction",
937 } else if (perf_stat_evsel__is(evsel, ELISION_START)) {
938 total = runtime_stat_avg(st, STAT_CYCLES_IN_TX,
944 print_metric(config, ctxp, NULL, "%8.0f", "cycles / elision", ratio);
945 } else if (perf_evsel__is_clock(evsel)) {
946 if ((ratio = avg_stats(&walltime_nsecs_stats)) != 0)
947 print_metric(config, ctxp, NULL, "%8.3f", "CPUs utilized",
948 avg / (ratio * evsel->scale));
950 print_metric(config, ctxp, NULL, NULL, "CPUs utilized", 0);
951 } else if (perf_stat_evsel__is(evsel, TOPDOWN_FETCH_BUBBLES)) {
952 double fe_bound = td_fe_bound(ctx, cpu, st);
955 color = PERF_COLOR_RED;
956 print_metric(config, ctxp, color, "%8.1f%%", "frontend bound",
958 } else if (perf_stat_evsel__is(evsel, TOPDOWN_SLOTS_RETIRED)) {
959 double retiring = td_retiring(ctx, cpu, st);
962 color = PERF_COLOR_GREEN;
963 print_metric(config, ctxp, color, "%8.1f%%", "retiring",
965 } else if (perf_stat_evsel__is(evsel, TOPDOWN_RECOVERY_BUBBLES)) {
966 double bad_spec = td_bad_spec(ctx, cpu, st);
969 color = PERF_COLOR_RED;
970 print_metric(config, ctxp, color, "%8.1f%%", "bad speculation",
972 } else if (perf_stat_evsel__is(evsel, TOPDOWN_SLOTS_ISSUED)) {
973 double be_bound = td_be_bound(ctx, cpu, st);
974 const char *name = "backend bound";
975 static int have_recovery_bubbles = -1;
977 /* In case the CPU does not support topdown-recovery-bubbles */
978 if (have_recovery_bubbles < 0)
979 have_recovery_bubbles = pmu_have_event("cpu",
980 "topdown-recovery-bubbles");
981 if (!have_recovery_bubbles)
982 name = "backend bound/bad spec";
985 color = PERF_COLOR_RED;
986 if (td_total_slots(ctx, cpu, st) > 0)
987 print_metric(config, ctxp, color, "%8.1f%%", name,
990 print_metric(config, ctxp, NULL, NULL, name, 0);
991 } else if (evsel->metric_expr) {
992 generic_metric(config, evsel->metric_expr, evsel->metric_events, evsel->name,
993 evsel->metric_name, avg, cpu, out, st);
994 } else if (runtime_stat_n(st, STAT_NSECS, 0, cpu) != 0) {
998 total = runtime_stat_avg(st, STAT_NSECS, 0, cpu);
1001 ratio = 1000.0 * avg / total;
1002 if (ratio < 0.001) {
1006 snprintf(unit_buf, sizeof(unit_buf), "%c/sec", unit);
1007 print_metric(config, ctxp, NULL, "%8.3f", unit_buf, ratio);
1008 } else if (perf_stat_evsel__is(evsel, SMI_NUM)) {
1009 print_smi_cost(config, cpu, evsel, out, st);
1014 if ((me = metricgroup__lookup(metric_events, evsel, false)) != NULL) {
1015 struct metric_expr *mexp;
1017 list_for_each_entry (mexp, &me->head, nd) {
1019 out->new_line(config, ctxp);
1020 generic_metric(config, mexp->metric_expr, mexp->metric_events,
1021 evsel->name, mexp->metric_name,
1026 print_metric(config, ctxp, NULL, NULL, NULL, 0);