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_CORE: Use first CPU of core
16 * AGGR_NONE: Use matching CPU
17 * AGGR_THREAD: Not supported?
19 static struct stats runtime_nsecs_stats[MAX_NR_CPUS];
20 static struct stats runtime_cycles_stats[NUM_CTX][MAX_NR_CPUS];
21 static struct stats runtime_stalled_cycles_front_stats[NUM_CTX][MAX_NR_CPUS];
22 static struct stats runtime_stalled_cycles_back_stats[NUM_CTX][MAX_NR_CPUS];
23 static struct stats runtime_branches_stats[NUM_CTX][MAX_NR_CPUS];
24 static struct stats runtime_cacherefs_stats[NUM_CTX][MAX_NR_CPUS];
25 static struct stats runtime_l1_dcache_stats[NUM_CTX][MAX_NR_CPUS];
26 static struct stats runtime_l1_icache_stats[NUM_CTX][MAX_NR_CPUS];
27 static struct stats runtime_ll_cache_stats[NUM_CTX][MAX_NR_CPUS];
28 static struct stats runtime_itlb_cache_stats[NUM_CTX][MAX_NR_CPUS];
29 static struct stats runtime_dtlb_cache_stats[NUM_CTX][MAX_NR_CPUS];
30 static struct stats runtime_cycles_in_tx_stats[NUM_CTX][MAX_NR_CPUS];
31 static struct stats runtime_transaction_stats[NUM_CTX][MAX_NR_CPUS];
32 static struct stats runtime_elision_stats[NUM_CTX][MAX_NR_CPUS];
33 static struct stats runtime_topdown_total_slots[NUM_CTX][MAX_NR_CPUS];
34 static struct stats runtime_topdown_slots_issued[NUM_CTX][MAX_NR_CPUS];
35 static struct stats runtime_topdown_slots_retired[NUM_CTX][MAX_NR_CPUS];
36 static struct stats runtime_topdown_fetch_bubbles[NUM_CTX][MAX_NR_CPUS];
37 static struct stats runtime_topdown_recovery_bubbles[NUM_CTX][MAX_NR_CPUS];
38 static struct stats runtime_smi_num_stats[NUM_CTX][MAX_NR_CPUS];
39 static struct stats runtime_aperf_stats[NUM_CTX][MAX_NR_CPUS];
40 static struct rblist runtime_saved_values;
41 static bool have_frontend_stalled;
43 struct stats walltime_nsecs_stats;
46 struct rb_node rb_node;
47 struct perf_evsel *evsel;
52 static int saved_value_cmp(struct rb_node *rb_node, const void *entry)
54 struct saved_value *a = container_of(rb_node,
57 const struct saved_value *b = entry;
60 return a->cpu - b->cpu;
61 if (a->evsel == b->evsel)
63 if ((char *)a->evsel < (char *)b->evsel)
68 static struct rb_node *saved_value_new(struct rblist *rblist __maybe_unused,
71 struct saved_value *nd = malloc(sizeof(struct saved_value));
75 memcpy(nd, entry, sizeof(struct saved_value));
79 static void saved_value_delete(struct rblist *rblist __maybe_unused,
80 struct rb_node *rb_node)
82 struct saved_value *v;
85 v = container_of(rb_node, struct saved_value, rb_node);
89 static struct saved_value *saved_value_lookup(struct perf_evsel *evsel,
94 struct saved_value dm = {
98 nd = rblist__find(&runtime_saved_values, &dm);
100 return container_of(nd, struct saved_value, rb_node);
102 rblist__add_node(&runtime_saved_values, &dm);
103 nd = rblist__find(&runtime_saved_values, &dm);
105 return container_of(nd, struct saved_value, rb_node);
110 void perf_stat__init_shadow_stats(void)
112 have_frontend_stalled = pmu_have_event("cpu", "stalled-cycles-frontend");
113 rblist__init(&runtime_saved_values);
114 runtime_saved_values.node_cmp = saved_value_cmp;
115 runtime_saved_values.node_new = saved_value_new;
116 runtime_saved_values.node_delete = saved_value_delete;
119 static int evsel_context(struct perf_evsel *evsel)
123 if (evsel->attr.exclude_kernel)
124 ctx |= CTX_BIT_KERNEL;
125 if (evsel->attr.exclude_user)
127 if (evsel->attr.exclude_hv)
129 if (evsel->attr.exclude_host)
131 if (evsel->attr.exclude_idle)
137 void perf_stat__reset_shadow_stats(void)
139 struct rb_node *pos, *next;
141 memset(runtime_nsecs_stats, 0, sizeof(runtime_nsecs_stats));
142 memset(runtime_cycles_stats, 0, sizeof(runtime_cycles_stats));
143 memset(runtime_stalled_cycles_front_stats, 0, sizeof(runtime_stalled_cycles_front_stats));
144 memset(runtime_stalled_cycles_back_stats, 0, sizeof(runtime_stalled_cycles_back_stats));
145 memset(runtime_branches_stats, 0, sizeof(runtime_branches_stats));
146 memset(runtime_cacherefs_stats, 0, sizeof(runtime_cacherefs_stats));
147 memset(runtime_l1_dcache_stats, 0, sizeof(runtime_l1_dcache_stats));
148 memset(runtime_l1_icache_stats, 0, sizeof(runtime_l1_icache_stats));
149 memset(runtime_ll_cache_stats, 0, sizeof(runtime_ll_cache_stats));
150 memset(runtime_itlb_cache_stats, 0, sizeof(runtime_itlb_cache_stats));
151 memset(runtime_dtlb_cache_stats, 0, sizeof(runtime_dtlb_cache_stats));
152 memset(runtime_cycles_in_tx_stats, 0,
153 sizeof(runtime_cycles_in_tx_stats));
154 memset(runtime_transaction_stats, 0,
155 sizeof(runtime_transaction_stats));
156 memset(runtime_elision_stats, 0, sizeof(runtime_elision_stats));
157 memset(&walltime_nsecs_stats, 0, sizeof(walltime_nsecs_stats));
158 memset(runtime_topdown_total_slots, 0, sizeof(runtime_topdown_total_slots));
159 memset(runtime_topdown_slots_retired, 0, sizeof(runtime_topdown_slots_retired));
160 memset(runtime_topdown_slots_issued, 0, sizeof(runtime_topdown_slots_issued));
161 memset(runtime_topdown_fetch_bubbles, 0, sizeof(runtime_topdown_fetch_bubbles));
162 memset(runtime_topdown_recovery_bubbles, 0, sizeof(runtime_topdown_recovery_bubbles));
163 memset(runtime_smi_num_stats, 0, sizeof(runtime_smi_num_stats));
164 memset(runtime_aperf_stats, 0, sizeof(runtime_aperf_stats));
166 next = rb_first(&runtime_saved_values.entries);
170 memset(&container_of(pos, struct saved_value, rb_node)->stats,
172 sizeof(struct stats));
177 * Update various tracking values we maintain to print
178 * more semantic information such as miss/hit ratios,
179 * instruction rates, etc:
181 void perf_stat__update_shadow_stats(struct perf_evsel *counter, u64 count,
184 int ctx = evsel_context(counter);
186 count *= counter->scale;
188 if (perf_evsel__match(counter, SOFTWARE, SW_TASK_CLOCK) ||
189 perf_evsel__match(counter, SOFTWARE, SW_CPU_CLOCK))
190 update_stats(&runtime_nsecs_stats[cpu], count);
191 else if (perf_evsel__match(counter, HARDWARE, HW_CPU_CYCLES))
192 update_stats(&runtime_cycles_stats[ctx][cpu], count);
193 else if (perf_stat_evsel__is(counter, CYCLES_IN_TX))
194 update_stats(&runtime_cycles_in_tx_stats[ctx][cpu], count);
195 else if (perf_stat_evsel__is(counter, TRANSACTION_START))
196 update_stats(&runtime_transaction_stats[ctx][cpu], count);
197 else if (perf_stat_evsel__is(counter, ELISION_START))
198 update_stats(&runtime_elision_stats[ctx][cpu], count);
199 else if (perf_stat_evsel__is(counter, TOPDOWN_TOTAL_SLOTS))
200 update_stats(&runtime_topdown_total_slots[ctx][cpu], count);
201 else if (perf_stat_evsel__is(counter, TOPDOWN_SLOTS_ISSUED))
202 update_stats(&runtime_topdown_slots_issued[ctx][cpu], count);
203 else if (perf_stat_evsel__is(counter, TOPDOWN_SLOTS_RETIRED))
204 update_stats(&runtime_topdown_slots_retired[ctx][cpu], count);
205 else if (perf_stat_evsel__is(counter, TOPDOWN_FETCH_BUBBLES))
206 update_stats(&runtime_topdown_fetch_bubbles[ctx][cpu], count);
207 else if (perf_stat_evsel__is(counter, TOPDOWN_RECOVERY_BUBBLES))
208 update_stats(&runtime_topdown_recovery_bubbles[ctx][cpu], count);
209 else if (perf_evsel__match(counter, HARDWARE, HW_STALLED_CYCLES_FRONTEND))
210 update_stats(&runtime_stalled_cycles_front_stats[ctx][cpu], count);
211 else if (perf_evsel__match(counter, HARDWARE, HW_STALLED_CYCLES_BACKEND))
212 update_stats(&runtime_stalled_cycles_back_stats[ctx][cpu], count);
213 else if (perf_evsel__match(counter, HARDWARE, HW_BRANCH_INSTRUCTIONS))
214 update_stats(&runtime_branches_stats[ctx][cpu], count);
215 else if (perf_evsel__match(counter, HARDWARE, HW_CACHE_REFERENCES))
216 update_stats(&runtime_cacherefs_stats[ctx][cpu], count);
217 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_L1D))
218 update_stats(&runtime_l1_dcache_stats[ctx][cpu], count);
219 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_L1I))
220 update_stats(&runtime_ll_cache_stats[ctx][cpu], count);
221 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_LL))
222 update_stats(&runtime_ll_cache_stats[ctx][cpu], count);
223 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_DTLB))
224 update_stats(&runtime_dtlb_cache_stats[ctx][cpu], count);
225 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_ITLB))
226 update_stats(&runtime_itlb_cache_stats[ctx][cpu], count);
227 else if (perf_stat_evsel__is(counter, SMI_NUM))
228 update_stats(&runtime_smi_num_stats[ctx][cpu], count);
229 else if (perf_stat_evsel__is(counter, APERF))
230 update_stats(&runtime_aperf_stats[ctx][cpu], count);
232 if (counter->collect_stat) {
233 struct saved_value *v = saved_value_lookup(counter, cpu, true);
234 update_stats(&v->stats, count);
238 /* used for get_ratio_color() */
240 GRC_STALLED_CYCLES_FE,
241 GRC_STALLED_CYCLES_BE,
246 static const char *get_ratio_color(enum grc_type type, double ratio)
248 static const double grc_table[GRC_MAX_NR][3] = {
249 [GRC_STALLED_CYCLES_FE] = { 50.0, 30.0, 10.0 },
250 [GRC_STALLED_CYCLES_BE] = { 75.0, 50.0, 20.0 },
251 [GRC_CACHE_MISSES] = { 20.0, 10.0, 5.0 },
253 const char *color = PERF_COLOR_NORMAL;
255 if (ratio > grc_table[type][0])
256 color = PERF_COLOR_RED;
257 else if (ratio > grc_table[type][1])
258 color = PERF_COLOR_MAGENTA;
259 else if (ratio > grc_table[type][2])
260 color = PERF_COLOR_YELLOW;
265 static struct perf_evsel *perf_stat__find_event(struct perf_evlist *evsel_list,
268 struct perf_evsel *c2;
270 evlist__for_each_entry (evsel_list, c2) {
271 if (!strcasecmp(c2->name, name))
277 /* Mark MetricExpr target events and link events using them to them. */
278 void perf_stat__collect_metric_expr(struct perf_evlist *evsel_list)
280 struct perf_evsel *counter, *leader, **metric_events, *oc;
282 const char **metric_names;
284 int num_metric_names;
286 evlist__for_each_entry(evsel_list, counter) {
287 bool invalid = false;
289 leader = counter->leader;
290 if (!counter->metric_expr)
292 metric_events = counter->metric_events;
293 if (!metric_events) {
294 if (expr__find_other(counter->metric_expr, counter->name,
295 &metric_names, &num_metric_names) < 0)
298 metric_events = calloc(sizeof(struct perf_evsel *),
299 num_metric_names + 1);
302 counter->metric_events = metric_events;
305 for (i = 0; i < num_metric_names; i++) {
308 /* Search in group */
309 for_each_group_member (oc, leader) {
310 if (!strcasecmp(oc->name, metric_names[i])) {
317 /* Search ignoring groups */
318 oc = perf_stat__find_event(evsel_list, metric_names[i]);
321 /* Deduping one is good enough to handle duplicated PMUs. */
322 static char *printed;
325 * Adding events automatically would be difficult, because
326 * it would risk creating groups that are not schedulable.
327 * perf stat doesn't understand all the scheduling constraints
328 * of events. So we ask the user instead to add the missing
331 if (!printed || strcasecmp(printed, metric_names[i])) {
333 "Add %s event to groups to get metric expression for %s\n",
336 printed = strdup(metric_names[i]);
341 metric_events[i] = oc;
342 oc->collect_stat = true;
344 metric_events[i] = NULL;
348 counter->metric_events = NULL;
349 counter->metric_expr = NULL;
354 static void print_stalled_cycles_frontend(int cpu,
355 struct perf_evsel *evsel, double avg,
356 struct perf_stat_output_ctx *out)
358 double total, ratio = 0.0;
360 int ctx = evsel_context(evsel);
362 total = avg_stats(&runtime_cycles_stats[ctx][cpu]);
365 ratio = avg / total * 100.0;
367 color = get_ratio_color(GRC_STALLED_CYCLES_FE, ratio);
370 out->print_metric(out->ctx, color, "%7.2f%%", "frontend cycles idle",
373 out->print_metric(out->ctx, NULL, NULL, "frontend cycles idle", 0);
376 static void print_stalled_cycles_backend(int cpu,
377 struct perf_evsel *evsel, double avg,
378 struct perf_stat_output_ctx *out)
380 double total, ratio = 0.0;
382 int ctx = evsel_context(evsel);
384 total = avg_stats(&runtime_cycles_stats[ctx][cpu]);
387 ratio = avg / total * 100.0;
389 color = get_ratio_color(GRC_STALLED_CYCLES_BE, ratio);
391 out->print_metric(out->ctx, color, "%7.2f%%", "backend cycles idle", ratio);
394 static void print_branch_misses(int cpu,
395 struct perf_evsel *evsel,
397 struct perf_stat_output_ctx *out)
399 double total, ratio = 0.0;
401 int ctx = evsel_context(evsel);
403 total = avg_stats(&runtime_branches_stats[ctx][cpu]);
406 ratio = avg / total * 100.0;
408 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
410 out->print_metric(out->ctx, color, "%7.2f%%", "of all branches", ratio);
413 static void print_l1_dcache_misses(int cpu,
414 struct perf_evsel *evsel,
416 struct perf_stat_output_ctx *out)
418 double total, ratio = 0.0;
420 int ctx = evsel_context(evsel);
422 total = avg_stats(&runtime_l1_dcache_stats[ctx][cpu]);
425 ratio = avg / total * 100.0;
427 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
429 out->print_metric(out->ctx, color, "%7.2f%%", "of all L1-dcache hits", ratio);
432 static void print_l1_icache_misses(int cpu,
433 struct perf_evsel *evsel,
435 struct perf_stat_output_ctx *out)
437 double total, ratio = 0.0;
439 int ctx = evsel_context(evsel);
441 total = avg_stats(&runtime_l1_icache_stats[ctx][cpu]);
444 ratio = avg / total * 100.0;
446 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
447 out->print_metric(out->ctx, color, "%7.2f%%", "of all L1-icache hits", ratio);
450 static void print_dtlb_cache_misses(int cpu,
451 struct perf_evsel *evsel,
453 struct perf_stat_output_ctx *out)
455 double total, ratio = 0.0;
457 int ctx = evsel_context(evsel);
459 total = avg_stats(&runtime_dtlb_cache_stats[ctx][cpu]);
462 ratio = avg / total * 100.0;
464 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
465 out->print_metric(out->ctx, color, "%7.2f%%", "of all dTLB cache hits", ratio);
468 static void print_itlb_cache_misses(int cpu,
469 struct perf_evsel *evsel,
471 struct perf_stat_output_ctx *out)
473 double total, ratio = 0.0;
475 int ctx = evsel_context(evsel);
477 total = avg_stats(&runtime_itlb_cache_stats[ctx][cpu]);
480 ratio = avg / total * 100.0;
482 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
483 out->print_metric(out->ctx, color, "%7.2f%%", "of all iTLB cache hits", ratio);
486 static void print_ll_cache_misses(int cpu,
487 struct perf_evsel *evsel,
489 struct perf_stat_output_ctx *out)
491 double total, ratio = 0.0;
493 int ctx = evsel_context(evsel);
495 total = avg_stats(&runtime_ll_cache_stats[ctx][cpu]);
498 ratio = avg / total * 100.0;
500 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
501 out->print_metric(out->ctx, color, "%7.2f%%", "of all LL-cache hits", ratio);
505 * High level "TopDown" CPU core pipe line bottleneck break down.
507 * Basic concept following
508 * Yasin, A Top Down Method for Performance analysis and Counter architecture
511 * The CPU pipeline is divided into 4 areas that can be bottlenecks:
513 * Frontend -> Backend -> Retiring
514 * BadSpeculation in addition means out of order execution that is thrown away
515 * (for example branch mispredictions)
516 * Frontend is instruction decoding.
517 * Backend is execution, like computation and accessing data in memory
518 * Retiring is good execution that is not directly bottlenecked
520 * The formulas are computed in slots.
521 * A slot is an entry in the pipeline each for the pipeline width
522 * (for example a 4-wide pipeline has 4 slots for each cycle)
525 * BadSpeculation = ((SlotsIssued - SlotsRetired) + RecoveryBubbles) /
527 * Retiring = SlotsRetired / TotalSlots
528 * FrontendBound = FetchBubbles / TotalSlots
529 * BackendBound = 1.0 - BadSpeculation - Retiring - FrontendBound
531 * The kernel provides the mapping to the low level CPU events and any scaling
532 * needed for the CPU pipeline width, for example:
534 * TotalSlots = Cycles * 4
536 * The scaling factor is communicated in the sysfs unit.
538 * In some cases the CPU may not be able to measure all the formulas due to
539 * missing events. In this case multiple formulas are combined, as possible.
541 * Full TopDown supports more levels to sub-divide each area: for example
542 * BackendBound into computing bound and memory bound. For now we only
543 * support Level 1 TopDown.
546 static double sanitize_val(double x)
548 if (x < 0 && x >= -0.02)
553 static double td_total_slots(int ctx, int cpu)
555 return avg_stats(&runtime_topdown_total_slots[ctx][cpu]);
558 static double td_bad_spec(int ctx, int cpu)
564 total = avg_stats(&runtime_topdown_slots_issued[ctx][cpu]) -
565 avg_stats(&runtime_topdown_slots_retired[ctx][cpu]) +
566 avg_stats(&runtime_topdown_recovery_bubbles[ctx][cpu]);
567 total_slots = td_total_slots(ctx, cpu);
569 bad_spec = total / total_slots;
570 return sanitize_val(bad_spec);
573 static double td_retiring(int ctx, int cpu)
576 double total_slots = td_total_slots(ctx, cpu);
577 double ret_slots = avg_stats(&runtime_topdown_slots_retired[ctx][cpu]);
580 retiring = ret_slots / total_slots;
584 static double td_fe_bound(int ctx, int cpu)
587 double total_slots = td_total_slots(ctx, cpu);
588 double fetch_bub = avg_stats(&runtime_topdown_fetch_bubbles[ctx][cpu]);
591 fe_bound = fetch_bub / total_slots;
595 static double td_be_bound(int ctx, int cpu)
597 double sum = (td_fe_bound(ctx, cpu) +
598 td_bad_spec(ctx, cpu) +
599 td_retiring(ctx, cpu));
602 return sanitize_val(1.0 - sum);
605 static void print_smi_cost(int cpu, struct perf_evsel *evsel,
606 struct perf_stat_output_ctx *out)
608 double smi_num, aperf, cycles, cost = 0.0;
609 int ctx = evsel_context(evsel);
610 const char *color = NULL;
612 smi_num = avg_stats(&runtime_smi_num_stats[ctx][cpu]);
613 aperf = avg_stats(&runtime_aperf_stats[ctx][cpu]);
614 cycles = avg_stats(&runtime_cycles_stats[ctx][cpu]);
616 if ((cycles == 0) || (aperf == 0))
620 cost = (aperf - cycles) / aperf * 100.00;
623 color = PERF_COLOR_RED;
624 out->print_metric(out->ctx, color, "%8.1f%%", "SMI cycles%", cost);
625 out->print_metric(out->ctx, NULL, "%4.0f", "SMI#", smi_num);
628 static void generic_metric(const char *metric_expr,
629 struct perf_evsel **metric_events,
631 const char *metric_name,
634 struct perf_stat_output_ctx *out)
636 print_metric_t print_metric = out->print_metric;
637 struct parse_ctx pctx;
640 void *ctxp = out->ctx;
642 expr__ctx_init(&pctx);
643 expr__add_id(&pctx, name, avg);
644 for (i = 0; metric_events[i]; i++) {
645 struct saved_value *v;
649 if (!strcmp(metric_events[i]->name, "duration_time")) {
650 stats = &walltime_nsecs_stats;
653 v = saved_value_lookup(metric_events[i], cpu, false);
659 expr__add_id(&pctx, metric_events[i]->name, avg_stats(stats)*scale);
661 if (!metric_events[i]) {
662 const char *p = metric_expr;
664 if (expr__parse(&ratio, &pctx, &p) == 0)
665 print_metric(ctxp, NULL, "%8.1f",
668 out->force_header ? name : "",
671 print_metric(ctxp, NULL, NULL,
673 (metric_name ? metric_name : name) : "", 0);
675 print_metric(ctxp, NULL, NULL, "", 0);
678 void perf_stat__print_shadow_stats(struct perf_evsel *evsel,
680 struct perf_stat_output_ctx *out,
681 struct rblist *metric_events)
683 void *ctxp = out->ctx;
684 print_metric_t print_metric = out->print_metric;
685 double total, ratio = 0.0, total2;
686 const char *color = NULL;
687 int ctx = evsel_context(evsel);
688 struct metric_event *me;
691 if (perf_evsel__match(evsel, HARDWARE, HW_INSTRUCTIONS)) {
692 total = avg_stats(&runtime_cycles_stats[ctx][cpu]);
695 print_metric(ctxp, NULL, "%7.2f ",
696 "insn per cycle", ratio);
698 print_metric(ctxp, NULL, NULL, "insn per cycle", 0);
700 total = avg_stats(&runtime_stalled_cycles_front_stats[ctx][cpu]);
701 total = max(total, avg_stats(&runtime_stalled_cycles_back_stats[ctx][cpu]));
706 print_metric(ctxp, NULL, "%7.2f ",
707 "stalled cycles per insn",
709 } else if (have_frontend_stalled) {
710 print_metric(ctxp, NULL, NULL,
711 "stalled cycles per insn", 0);
713 } else if (perf_evsel__match(evsel, HARDWARE, HW_BRANCH_MISSES)) {
714 if (runtime_branches_stats[ctx][cpu].n != 0)
715 print_branch_misses(cpu, evsel, avg, out);
717 print_metric(ctxp, NULL, NULL, "of all branches", 0);
719 evsel->attr.type == PERF_TYPE_HW_CACHE &&
720 evsel->attr.config == ( PERF_COUNT_HW_CACHE_L1D |
721 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
722 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
723 if (runtime_l1_dcache_stats[ctx][cpu].n != 0)
724 print_l1_dcache_misses(cpu, evsel, avg, out);
726 print_metric(ctxp, NULL, NULL, "of all L1-dcache hits", 0);
728 evsel->attr.type == PERF_TYPE_HW_CACHE &&
729 evsel->attr.config == ( PERF_COUNT_HW_CACHE_L1I |
730 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
731 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
732 if (runtime_l1_icache_stats[ctx][cpu].n != 0)
733 print_l1_icache_misses(cpu, evsel, avg, out);
735 print_metric(ctxp, NULL, NULL, "of all L1-icache hits", 0);
737 evsel->attr.type == PERF_TYPE_HW_CACHE &&
738 evsel->attr.config == ( PERF_COUNT_HW_CACHE_DTLB |
739 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
740 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
741 if (runtime_dtlb_cache_stats[ctx][cpu].n != 0)
742 print_dtlb_cache_misses(cpu, evsel, avg, out);
744 print_metric(ctxp, NULL, NULL, "of all dTLB cache hits", 0);
746 evsel->attr.type == PERF_TYPE_HW_CACHE &&
747 evsel->attr.config == ( PERF_COUNT_HW_CACHE_ITLB |
748 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
749 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
750 if (runtime_itlb_cache_stats[ctx][cpu].n != 0)
751 print_itlb_cache_misses(cpu, evsel, avg, out);
753 print_metric(ctxp, NULL, NULL, "of all iTLB cache hits", 0);
755 evsel->attr.type == PERF_TYPE_HW_CACHE &&
756 evsel->attr.config == ( PERF_COUNT_HW_CACHE_LL |
757 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
758 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
759 if (runtime_ll_cache_stats[ctx][cpu].n != 0)
760 print_ll_cache_misses(cpu, evsel, avg, out);
762 print_metric(ctxp, NULL, NULL, "of all LL-cache hits", 0);
763 } else if (perf_evsel__match(evsel, HARDWARE, HW_CACHE_MISSES)) {
764 total = avg_stats(&runtime_cacherefs_stats[ctx][cpu]);
767 ratio = avg * 100 / total;
769 if (runtime_cacherefs_stats[ctx][cpu].n != 0)
770 print_metric(ctxp, NULL, "%8.3f %%",
771 "of all cache refs", ratio);
773 print_metric(ctxp, NULL, NULL, "of all cache refs", 0);
774 } else if (perf_evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES_FRONTEND)) {
775 print_stalled_cycles_frontend(cpu, evsel, avg, out);
776 } else if (perf_evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES_BACKEND)) {
777 print_stalled_cycles_backend(cpu, evsel, avg, out);
778 } else if (perf_evsel__match(evsel, HARDWARE, HW_CPU_CYCLES)) {
779 total = avg_stats(&runtime_nsecs_stats[cpu]);
783 print_metric(ctxp, NULL, "%8.3f", "GHz", ratio);
785 print_metric(ctxp, NULL, NULL, "Ghz", 0);
787 } else if (perf_stat_evsel__is(evsel, CYCLES_IN_TX)) {
788 total = avg_stats(&runtime_cycles_stats[ctx][cpu]);
790 print_metric(ctxp, NULL,
791 "%7.2f%%", "transactional cycles",
792 100.0 * (avg / total));
794 print_metric(ctxp, NULL, NULL, "transactional cycles",
796 } else if (perf_stat_evsel__is(evsel, CYCLES_IN_TX_CP)) {
797 total = avg_stats(&runtime_cycles_stats[ctx][cpu]);
798 total2 = avg_stats(&runtime_cycles_in_tx_stats[ctx][cpu]);
802 print_metric(ctxp, NULL, "%7.2f%%", "aborted cycles",
803 100.0 * ((total2-avg) / total));
805 print_metric(ctxp, NULL, NULL, "aborted cycles", 0);
806 } else if (perf_stat_evsel__is(evsel, TRANSACTION_START)) {
807 total = avg_stats(&runtime_cycles_in_tx_stats[ctx][cpu]);
812 if (runtime_cycles_in_tx_stats[ctx][cpu].n != 0)
813 print_metric(ctxp, NULL, "%8.0f",
814 "cycles / transaction", ratio);
816 print_metric(ctxp, NULL, NULL, "cycles / transaction",
818 } else if (perf_stat_evsel__is(evsel, ELISION_START)) {
819 total = avg_stats(&runtime_cycles_in_tx_stats[ctx][cpu]);
824 print_metric(ctxp, NULL, "%8.0f", "cycles / elision", ratio);
825 } else if (perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK) ||
826 perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK)) {
827 if ((ratio = avg_stats(&walltime_nsecs_stats)) != 0)
828 print_metric(ctxp, NULL, "%8.3f", "CPUs utilized",
831 print_metric(ctxp, NULL, NULL, "CPUs utilized", 0);
832 } else if (perf_stat_evsel__is(evsel, TOPDOWN_FETCH_BUBBLES)) {
833 double fe_bound = td_fe_bound(ctx, cpu);
836 color = PERF_COLOR_RED;
837 print_metric(ctxp, color, "%8.1f%%", "frontend bound",
839 } else if (perf_stat_evsel__is(evsel, TOPDOWN_SLOTS_RETIRED)) {
840 double retiring = td_retiring(ctx, cpu);
843 color = PERF_COLOR_GREEN;
844 print_metric(ctxp, color, "%8.1f%%", "retiring",
846 } else if (perf_stat_evsel__is(evsel, TOPDOWN_RECOVERY_BUBBLES)) {
847 double bad_spec = td_bad_spec(ctx, cpu);
850 color = PERF_COLOR_RED;
851 print_metric(ctxp, color, "%8.1f%%", "bad speculation",
853 } else if (perf_stat_evsel__is(evsel, TOPDOWN_SLOTS_ISSUED)) {
854 double be_bound = td_be_bound(ctx, cpu);
855 const char *name = "backend bound";
856 static int have_recovery_bubbles = -1;
858 /* In case the CPU does not support topdown-recovery-bubbles */
859 if (have_recovery_bubbles < 0)
860 have_recovery_bubbles = pmu_have_event("cpu",
861 "topdown-recovery-bubbles");
862 if (!have_recovery_bubbles)
863 name = "backend bound/bad spec";
866 color = PERF_COLOR_RED;
867 if (td_total_slots(ctx, cpu) > 0)
868 print_metric(ctxp, color, "%8.1f%%", name,
871 print_metric(ctxp, NULL, NULL, name, 0);
872 } else if (evsel->metric_expr) {
873 generic_metric(evsel->metric_expr, evsel->metric_events, evsel->name,
874 evsel->metric_name, avg, cpu, out);
875 } else if (runtime_nsecs_stats[cpu].n != 0) {
879 total = avg_stats(&runtime_nsecs_stats[cpu]);
882 ratio = 1000.0 * avg / total;
887 snprintf(unit_buf, sizeof(unit_buf), "%c/sec", unit);
888 print_metric(ctxp, NULL, "%8.3f", unit_buf, ratio);
889 } else if (perf_stat_evsel__is(evsel, SMI_NUM)) {
890 print_smi_cost(cpu, evsel, out);
895 if ((me = metricgroup__lookup(metric_events, evsel, false)) != NULL) {
896 struct metric_expr *mexp;
898 list_for_each_entry (mexp, &me->head, nd) {
901 generic_metric(mexp->metric_expr, mexp->metric_events,
902 evsel->name, mexp->metric_name,
907 print_metric(ctxp, NULL, NULL, NULL, 0);