1 // SPDX-License-Identifier: GPL-2.0
12 CTX_BIT_USER = 1 << 0,
13 CTX_BIT_KERNEL = 1 << 1,
15 CTX_BIT_HOST = 1 << 3,
16 CTX_BIT_IDLE = 1 << 4,
20 #define NUM_CTX CTX_BIT_MAX
23 * AGGR_GLOBAL: Use CPU 0
24 * AGGR_SOCKET: Use first CPU of socket
25 * AGGR_CORE: Use first CPU of core
26 * AGGR_NONE: Use matching CPU
27 * AGGR_THREAD: Not supported?
29 static struct stats runtime_nsecs_stats[MAX_NR_CPUS];
30 static struct stats runtime_cycles_stats[NUM_CTX][MAX_NR_CPUS];
31 static struct stats runtime_stalled_cycles_front_stats[NUM_CTX][MAX_NR_CPUS];
32 static struct stats runtime_stalled_cycles_back_stats[NUM_CTX][MAX_NR_CPUS];
33 static struct stats runtime_branches_stats[NUM_CTX][MAX_NR_CPUS];
34 static struct stats runtime_cacherefs_stats[NUM_CTX][MAX_NR_CPUS];
35 static struct stats runtime_l1_dcache_stats[NUM_CTX][MAX_NR_CPUS];
36 static struct stats runtime_l1_icache_stats[NUM_CTX][MAX_NR_CPUS];
37 static struct stats runtime_ll_cache_stats[NUM_CTX][MAX_NR_CPUS];
38 static struct stats runtime_itlb_cache_stats[NUM_CTX][MAX_NR_CPUS];
39 static struct stats runtime_dtlb_cache_stats[NUM_CTX][MAX_NR_CPUS];
40 static struct stats runtime_cycles_in_tx_stats[NUM_CTX][MAX_NR_CPUS];
41 static struct stats runtime_transaction_stats[NUM_CTX][MAX_NR_CPUS];
42 static struct stats runtime_elision_stats[NUM_CTX][MAX_NR_CPUS];
43 static struct stats runtime_topdown_total_slots[NUM_CTX][MAX_NR_CPUS];
44 static struct stats runtime_topdown_slots_issued[NUM_CTX][MAX_NR_CPUS];
45 static struct stats runtime_topdown_slots_retired[NUM_CTX][MAX_NR_CPUS];
46 static struct stats runtime_topdown_fetch_bubbles[NUM_CTX][MAX_NR_CPUS];
47 static struct stats runtime_topdown_recovery_bubbles[NUM_CTX][MAX_NR_CPUS];
48 static struct stats runtime_smi_num_stats[NUM_CTX][MAX_NR_CPUS];
49 static struct stats runtime_aperf_stats[NUM_CTX][MAX_NR_CPUS];
50 static struct rblist runtime_saved_values;
51 static bool have_frontend_stalled;
53 struct stats walltime_nsecs_stats;
56 struct rb_node rb_node;
57 struct perf_evsel *evsel;
63 static int saved_value_cmp(struct rb_node *rb_node, const void *entry)
65 struct saved_value *a = container_of(rb_node,
68 const struct saved_value *b = entry;
71 return a->ctx - b->ctx;
73 return a->cpu - b->cpu;
74 if (a->evsel == b->evsel)
76 if ((char *)a->evsel < (char *)b->evsel)
81 static struct rb_node *saved_value_new(struct rblist *rblist __maybe_unused,
84 struct saved_value *nd = malloc(sizeof(struct saved_value));
88 memcpy(nd, entry, sizeof(struct saved_value));
92 static struct saved_value *saved_value_lookup(struct perf_evsel *evsel,
97 struct saved_value dm = {
102 nd = rblist__find(&runtime_saved_values, &dm);
104 return container_of(nd, struct saved_value, rb_node);
106 rblist__add_node(&runtime_saved_values, &dm);
107 nd = rblist__find(&runtime_saved_values, &dm);
109 return container_of(nd, struct saved_value, rb_node);
114 void perf_stat__init_shadow_stats(void)
116 have_frontend_stalled = pmu_have_event("cpu", "stalled-cycles-frontend");
117 rblist__init(&runtime_saved_values);
118 runtime_saved_values.node_cmp = saved_value_cmp;
119 runtime_saved_values.node_new = saved_value_new;
120 /* No delete for now */
123 static int evsel_context(struct perf_evsel *evsel)
127 if (evsel->attr.exclude_kernel)
128 ctx |= CTX_BIT_KERNEL;
129 if (evsel->attr.exclude_user)
131 if (evsel->attr.exclude_hv)
133 if (evsel->attr.exclude_host)
135 if (evsel->attr.exclude_idle)
141 void perf_stat__reset_shadow_stats(void)
143 struct rb_node *pos, *next;
145 memset(runtime_nsecs_stats, 0, sizeof(runtime_nsecs_stats));
146 memset(runtime_cycles_stats, 0, sizeof(runtime_cycles_stats));
147 memset(runtime_stalled_cycles_front_stats, 0, sizeof(runtime_stalled_cycles_front_stats));
148 memset(runtime_stalled_cycles_back_stats, 0, sizeof(runtime_stalled_cycles_back_stats));
149 memset(runtime_branches_stats, 0, sizeof(runtime_branches_stats));
150 memset(runtime_cacherefs_stats, 0, sizeof(runtime_cacherefs_stats));
151 memset(runtime_l1_dcache_stats, 0, sizeof(runtime_l1_dcache_stats));
152 memset(runtime_l1_icache_stats, 0, sizeof(runtime_l1_icache_stats));
153 memset(runtime_ll_cache_stats, 0, sizeof(runtime_ll_cache_stats));
154 memset(runtime_itlb_cache_stats, 0, sizeof(runtime_itlb_cache_stats));
155 memset(runtime_dtlb_cache_stats, 0, sizeof(runtime_dtlb_cache_stats));
156 memset(runtime_cycles_in_tx_stats, 0,
157 sizeof(runtime_cycles_in_tx_stats));
158 memset(runtime_transaction_stats, 0,
159 sizeof(runtime_transaction_stats));
160 memset(runtime_elision_stats, 0, sizeof(runtime_elision_stats));
161 memset(&walltime_nsecs_stats, 0, sizeof(walltime_nsecs_stats));
162 memset(runtime_topdown_total_slots, 0, sizeof(runtime_topdown_total_slots));
163 memset(runtime_topdown_slots_retired, 0, sizeof(runtime_topdown_slots_retired));
164 memset(runtime_topdown_slots_issued, 0, sizeof(runtime_topdown_slots_issued));
165 memset(runtime_topdown_fetch_bubbles, 0, sizeof(runtime_topdown_fetch_bubbles));
166 memset(runtime_topdown_recovery_bubbles, 0, sizeof(runtime_topdown_recovery_bubbles));
167 memset(runtime_smi_num_stats, 0, sizeof(runtime_smi_num_stats));
168 memset(runtime_aperf_stats, 0, sizeof(runtime_aperf_stats));
170 next = rb_first(&runtime_saved_values.entries);
174 memset(&container_of(pos, struct saved_value, rb_node)->stats,
176 sizeof(struct stats));
181 * Update various tracking values we maintain to print
182 * more semantic information such as miss/hit ratios,
183 * instruction rates, etc:
185 void perf_stat__update_shadow_stats(struct perf_evsel *counter, u64 *count,
188 int ctx = evsel_context(counter);
190 if (perf_evsel__match(counter, SOFTWARE, SW_TASK_CLOCK) ||
191 perf_evsel__match(counter, SOFTWARE, SW_CPU_CLOCK))
192 update_stats(&runtime_nsecs_stats[cpu], count[0]);
193 else if (perf_evsel__match(counter, HARDWARE, HW_CPU_CYCLES))
194 update_stats(&runtime_cycles_stats[ctx][cpu], count[0]);
195 else if (perf_stat_evsel__is(counter, CYCLES_IN_TX))
196 update_stats(&runtime_cycles_in_tx_stats[ctx][cpu], count[0]);
197 else if (perf_stat_evsel__is(counter, TRANSACTION_START))
198 update_stats(&runtime_transaction_stats[ctx][cpu], count[0]);
199 else if (perf_stat_evsel__is(counter, ELISION_START))
200 update_stats(&runtime_elision_stats[ctx][cpu], count[0]);
201 else if (perf_stat_evsel__is(counter, TOPDOWN_TOTAL_SLOTS))
202 update_stats(&runtime_topdown_total_slots[ctx][cpu], count[0]);
203 else if (perf_stat_evsel__is(counter, TOPDOWN_SLOTS_ISSUED))
204 update_stats(&runtime_topdown_slots_issued[ctx][cpu], count[0]);
205 else if (perf_stat_evsel__is(counter, TOPDOWN_SLOTS_RETIRED))
206 update_stats(&runtime_topdown_slots_retired[ctx][cpu], count[0]);
207 else if (perf_stat_evsel__is(counter, TOPDOWN_FETCH_BUBBLES))
208 update_stats(&runtime_topdown_fetch_bubbles[ctx][cpu],count[0]);
209 else if (perf_stat_evsel__is(counter, TOPDOWN_RECOVERY_BUBBLES))
210 update_stats(&runtime_topdown_recovery_bubbles[ctx][cpu], count[0]);
211 else if (perf_evsel__match(counter, HARDWARE, HW_STALLED_CYCLES_FRONTEND))
212 update_stats(&runtime_stalled_cycles_front_stats[ctx][cpu], count[0]);
213 else if (perf_evsel__match(counter, HARDWARE, HW_STALLED_CYCLES_BACKEND))
214 update_stats(&runtime_stalled_cycles_back_stats[ctx][cpu], count[0]);
215 else if (perf_evsel__match(counter, HARDWARE, HW_BRANCH_INSTRUCTIONS))
216 update_stats(&runtime_branches_stats[ctx][cpu], count[0]);
217 else if (perf_evsel__match(counter, HARDWARE, HW_CACHE_REFERENCES))
218 update_stats(&runtime_cacherefs_stats[ctx][cpu], count[0]);
219 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_L1D))
220 update_stats(&runtime_l1_dcache_stats[ctx][cpu], count[0]);
221 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_L1I))
222 update_stats(&runtime_ll_cache_stats[ctx][cpu], count[0]);
223 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_LL))
224 update_stats(&runtime_ll_cache_stats[ctx][cpu], count[0]);
225 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_DTLB))
226 update_stats(&runtime_dtlb_cache_stats[ctx][cpu], count[0]);
227 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_ITLB))
228 update_stats(&runtime_itlb_cache_stats[ctx][cpu], count[0]);
229 else if (perf_stat_evsel__is(counter, SMI_NUM))
230 update_stats(&runtime_smi_num_stats[ctx][cpu], count[0]);
231 else if (perf_stat_evsel__is(counter, APERF))
232 update_stats(&runtime_aperf_stats[ctx][cpu], count[0]);
234 if (counter->collect_stat) {
235 struct saved_value *v = saved_value_lookup(counter, cpu, ctx,
237 update_stats(&v->stats, count[0]);
241 /* used for get_ratio_color() */
243 GRC_STALLED_CYCLES_FE,
244 GRC_STALLED_CYCLES_BE,
249 static const char *get_ratio_color(enum grc_type type, double ratio)
251 static const double grc_table[GRC_MAX_NR][3] = {
252 [GRC_STALLED_CYCLES_FE] = { 50.0, 30.0, 10.0 },
253 [GRC_STALLED_CYCLES_BE] = { 75.0, 50.0, 20.0 },
254 [GRC_CACHE_MISSES] = { 20.0, 10.0, 5.0 },
256 const char *color = PERF_COLOR_NORMAL;
258 if (ratio > grc_table[type][0])
259 color = PERF_COLOR_RED;
260 else if (ratio > grc_table[type][1])
261 color = PERF_COLOR_MAGENTA;
262 else if (ratio > grc_table[type][2])
263 color = PERF_COLOR_YELLOW;
268 static struct perf_evsel *perf_stat__find_event(struct perf_evlist *evsel_list,
271 struct perf_evsel *c2;
273 evlist__for_each_entry (evsel_list, c2) {
274 if (!strcasecmp(c2->name, name) && !c2->collect_stat)
280 /* Mark MetricExpr target events and link events using them to them. */
281 void perf_stat__collect_metric_expr(struct perf_evlist *evsel_list)
283 struct perf_evsel *counter, *leader, **metric_events, *oc;
285 const char **metric_names;
287 int num_metric_names;
289 evlist__for_each_entry(evsel_list, counter) {
290 bool invalid = false;
292 leader = counter->leader;
293 if (!counter->metric_expr)
295 metric_events = counter->metric_events;
296 if (!metric_events) {
297 if (expr__find_other(counter->metric_expr, counter->name,
298 &metric_names, &num_metric_names) < 0)
301 metric_events = calloc(sizeof(struct perf_evsel *),
302 num_metric_names + 1);
305 counter->metric_events = metric_events;
308 for (i = 0; i < num_metric_names; i++) {
311 /* Search in group */
312 for_each_group_member (oc, leader) {
313 if (!strcasecmp(oc->name, metric_names[i]) &&
321 /* Search ignoring groups */
322 oc = perf_stat__find_event(evsel_list, metric_names[i]);
325 /* Deduping one is good enough to handle duplicated PMUs. */
326 static char *printed;
329 * Adding events automatically would be difficult, because
330 * it would risk creating groups that are not schedulable.
331 * perf stat doesn't understand all the scheduling constraints
332 * of events. So we ask the user instead to add the missing
335 if (!printed || strcasecmp(printed, metric_names[i])) {
337 "Add %s event to groups to get metric expression for %s\n",
340 printed = strdup(metric_names[i]);
345 metric_events[i] = oc;
346 oc->collect_stat = true;
348 metric_events[i] = NULL;
352 counter->metric_events = NULL;
353 counter->metric_expr = NULL;
358 static void print_stalled_cycles_frontend(int cpu,
359 struct perf_evsel *evsel, double avg,
360 struct perf_stat_output_ctx *out)
362 double total, ratio = 0.0;
364 int ctx = evsel_context(evsel);
366 total = avg_stats(&runtime_cycles_stats[ctx][cpu]);
369 ratio = avg / total * 100.0;
371 color = get_ratio_color(GRC_STALLED_CYCLES_FE, ratio);
374 out->print_metric(out->ctx, color, "%7.2f%%", "frontend cycles idle",
377 out->print_metric(out->ctx, NULL, NULL, "frontend cycles idle", 0);
380 static void print_stalled_cycles_backend(int cpu,
381 struct perf_evsel *evsel, double avg,
382 struct perf_stat_output_ctx *out)
384 double total, ratio = 0.0;
386 int ctx = evsel_context(evsel);
388 total = avg_stats(&runtime_cycles_stats[ctx][cpu]);
391 ratio = avg / total * 100.0;
393 color = get_ratio_color(GRC_STALLED_CYCLES_BE, ratio);
395 out->print_metric(out->ctx, color, "%7.2f%%", "backend cycles idle", ratio);
398 static void print_branch_misses(int cpu,
399 struct perf_evsel *evsel,
401 struct perf_stat_output_ctx *out)
403 double total, ratio = 0.0;
405 int ctx = evsel_context(evsel);
407 total = avg_stats(&runtime_branches_stats[ctx][cpu]);
410 ratio = avg / total * 100.0;
412 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
414 out->print_metric(out->ctx, color, "%7.2f%%", "of all branches", ratio);
417 static void print_l1_dcache_misses(int cpu,
418 struct perf_evsel *evsel,
420 struct perf_stat_output_ctx *out)
422 double total, ratio = 0.0;
424 int ctx = evsel_context(evsel);
426 total = avg_stats(&runtime_l1_dcache_stats[ctx][cpu]);
429 ratio = avg / total * 100.0;
431 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
433 out->print_metric(out->ctx, color, "%7.2f%%", "of all L1-dcache hits", ratio);
436 static void print_l1_icache_misses(int cpu,
437 struct perf_evsel *evsel,
439 struct perf_stat_output_ctx *out)
441 double total, ratio = 0.0;
443 int ctx = evsel_context(evsel);
445 total = avg_stats(&runtime_l1_icache_stats[ctx][cpu]);
448 ratio = avg / total * 100.0;
450 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
451 out->print_metric(out->ctx, color, "%7.2f%%", "of all L1-icache hits", ratio);
454 static void print_dtlb_cache_misses(int cpu,
455 struct perf_evsel *evsel,
457 struct perf_stat_output_ctx *out)
459 double total, ratio = 0.0;
461 int ctx = evsel_context(evsel);
463 total = avg_stats(&runtime_dtlb_cache_stats[ctx][cpu]);
466 ratio = avg / total * 100.0;
468 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
469 out->print_metric(out->ctx, color, "%7.2f%%", "of all dTLB cache hits", ratio);
472 static void print_itlb_cache_misses(int cpu,
473 struct perf_evsel *evsel,
475 struct perf_stat_output_ctx *out)
477 double total, ratio = 0.0;
479 int ctx = evsel_context(evsel);
481 total = avg_stats(&runtime_itlb_cache_stats[ctx][cpu]);
484 ratio = avg / total * 100.0;
486 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
487 out->print_metric(out->ctx, color, "%7.2f%%", "of all iTLB cache hits", ratio);
490 static void print_ll_cache_misses(int cpu,
491 struct perf_evsel *evsel,
493 struct perf_stat_output_ctx *out)
495 double total, ratio = 0.0;
497 int ctx = evsel_context(evsel);
499 total = avg_stats(&runtime_ll_cache_stats[ctx][cpu]);
502 ratio = avg / total * 100.0;
504 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
505 out->print_metric(out->ctx, color, "%7.2f%%", "of all LL-cache hits", ratio);
509 * High level "TopDown" CPU core pipe line bottleneck break down.
511 * Basic concept following
512 * Yasin, A Top Down Method for Performance analysis and Counter architecture
515 * The CPU pipeline is divided into 4 areas that can be bottlenecks:
517 * Frontend -> Backend -> Retiring
518 * BadSpeculation in addition means out of order execution that is thrown away
519 * (for example branch mispredictions)
520 * Frontend is instruction decoding.
521 * Backend is execution, like computation and accessing data in memory
522 * Retiring is good execution that is not directly bottlenecked
524 * The formulas are computed in slots.
525 * A slot is an entry in the pipeline each for the pipeline width
526 * (for example a 4-wide pipeline has 4 slots for each cycle)
529 * BadSpeculation = ((SlotsIssued - SlotsRetired) + RecoveryBubbles) /
531 * Retiring = SlotsRetired / TotalSlots
532 * FrontendBound = FetchBubbles / TotalSlots
533 * BackendBound = 1.0 - BadSpeculation - Retiring - FrontendBound
535 * The kernel provides the mapping to the low level CPU events and any scaling
536 * needed for the CPU pipeline width, for example:
538 * TotalSlots = Cycles * 4
540 * The scaling factor is communicated in the sysfs unit.
542 * In some cases the CPU may not be able to measure all the formulas due to
543 * missing events. In this case multiple formulas are combined, as possible.
545 * Full TopDown supports more levels to sub-divide each area: for example
546 * BackendBound into computing bound and memory bound. For now we only
547 * support Level 1 TopDown.
550 static double sanitize_val(double x)
552 if (x < 0 && x >= -0.02)
557 static double td_total_slots(int ctx, int cpu)
559 return avg_stats(&runtime_topdown_total_slots[ctx][cpu]);
562 static double td_bad_spec(int ctx, int cpu)
568 total = avg_stats(&runtime_topdown_slots_issued[ctx][cpu]) -
569 avg_stats(&runtime_topdown_slots_retired[ctx][cpu]) +
570 avg_stats(&runtime_topdown_recovery_bubbles[ctx][cpu]);
571 total_slots = td_total_slots(ctx, cpu);
573 bad_spec = total / total_slots;
574 return sanitize_val(bad_spec);
577 static double td_retiring(int ctx, int cpu)
580 double total_slots = td_total_slots(ctx, cpu);
581 double ret_slots = avg_stats(&runtime_topdown_slots_retired[ctx][cpu]);
584 retiring = ret_slots / total_slots;
588 static double td_fe_bound(int ctx, int cpu)
591 double total_slots = td_total_slots(ctx, cpu);
592 double fetch_bub = avg_stats(&runtime_topdown_fetch_bubbles[ctx][cpu]);
595 fe_bound = fetch_bub / total_slots;
599 static double td_be_bound(int ctx, int cpu)
601 double sum = (td_fe_bound(ctx, cpu) +
602 td_bad_spec(ctx, cpu) +
603 td_retiring(ctx, cpu));
606 return sanitize_val(1.0 - sum);
609 static void print_smi_cost(int cpu, struct perf_evsel *evsel,
610 struct perf_stat_output_ctx *out)
612 double smi_num, aperf, cycles, cost = 0.0;
613 int ctx = evsel_context(evsel);
614 const char *color = NULL;
616 smi_num = avg_stats(&runtime_smi_num_stats[ctx][cpu]);
617 aperf = avg_stats(&runtime_aperf_stats[ctx][cpu]);
618 cycles = avg_stats(&runtime_cycles_stats[ctx][cpu]);
620 if ((cycles == 0) || (aperf == 0))
624 cost = (aperf - cycles) / aperf * 100.00;
627 color = PERF_COLOR_RED;
628 out->print_metric(out->ctx, color, "%8.1f%%", "SMI cycles%", cost);
629 out->print_metric(out->ctx, NULL, "%4.0f", "SMI#", smi_num);
632 void perf_stat__print_shadow_stats(struct perf_evsel *evsel,
634 struct perf_stat_output_ctx *out)
636 void *ctxp = out->ctx;
637 print_metric_t print_metric = out->print_metric;
638 double total, ratio = 0.0, total2;
639 const char *color = NULL;
640 int ctx = evsel_context(evsel);
642 if (perf_evsel__match(evsel, HARDWARE, HW_INSTRUCTIONS)) {
643 total = avg_stats(&runtime_cycles_stats[ctx][cpu]);
646 print_metric(ctxp, NULL, "%7.2f ",
647 "insn per cycle", ratio);
649 print_metric(ctxp, NULL, NULL, "insn per cycle", 0);
651 total = avg_stats(&runtime_stalled_cycles_front_stats[ctx][cpu]);
652 total = max(total, avg_stats(&runtime_stalled_cycles_back_stats[ctx][cpu]));
657 print_metric(ctxp, NULL, "%7.2f ",
658 "stalled cycles per insn",
660 } else if (have_frontend_stalled) {
661 print_metric(ctxp, NULL, NULL,
662 "stalled cycles per insn", 0);
664 } else if (perf_evsel__match(evsel, HARDWARE, HW_BRANCH_MISSES)) {
665 if (runtime_branches_stats[ctx][cpu].n != 0)
666 print_branch_misses(cpu, evsel, avg, out);
668 print_metric(ctxp, NULL, NULL, "of all branches", 0);
670 evsel->attr.type == PERF_TYPE_HW_CACHE &&
671 evsel->attr.config == ( PERF_COUNT_HW_CACHE_L1D |
672 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
673 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
674 if (runtime_l1_dcache_stats[ctx][cpu].n != 0)
675 print_l1_dcache_misses(cpu, evsel, avg, out);
677 print_metric(ctxp, NULL, NULL, "of all L1-dcache hits", 0);
679 evsel->attr.type == PERF_TYPE_HW_CACHE &&
680 evsel->attr.config == ( PERF_COUNT_HW_CACHE_L1I |
681 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
682 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
683 if (runtime_l1_icache_stats[ctx][cpu].n != 0)
684 print_l1_icache_misses(cpu, evsel, avg, out);
686 print_metric(ctxp, NULL, NULL, "of all L1-icache hits", 0);
688 evsel->attr.type == PERF_TYPE_HW_CACHE &&
689 evsel->attr.config == ( PERF_COUNT_HW_CACHE_DTLB |
690 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
691 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
692 if (runtime_dtlb_cache_stats[ctx][cpu].n != 0)
693 print_dtlb_cache_misses(cpu, evsel, avg, out);
695 print_metric(ctxp, NULL, NULL, "of all dTLB cache hits", 0);
697 evsel->attr.type == PERF_TYPE_HW_CACHE &&
698 evsel->attr.config == ( PERF_COUNT_HW_CACHE_ITLB |
699 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
700 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
701 if (runtime_itlb_cache_stats[ctx][cpu].n != 0)
702 print_itlb_cache_misses(cpu, evsel, avg, out);
704 print_metric(ctxp, NULL, NULL, "of all iTLB cache hits", 0);
706 evsel->attr.type == PERF_TYPE_HW_CACHE &&
707 evsel->attr.config == ( PERF_COUNT_HW_CACHE_LL |
708 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
709 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16))) {
710 if (runtime_ll_cache_stats[ctx][cpu].n != 0)
711 print_ll_cache_misses(cpu, evsel, avg, out);
713 print_metric(ctxp, NULL, NULL, "of all LL-cache hits", 0);
714 } else if (perf_evsel__match(evsel, HARDWARE, HW_CACHE_MISSES)) {
715 total = avg_stats(&runtime_cacherefs_stats[ctx][cpu]);
718 ratio = avg * 100 / total;
720 if (runtime_cacherefs_stats[ctx][cpu].n != 0)
721 print_metric(ctxp, NULL, "%8.3f %%",
722 "of all cache refs", ratio);
724 print_metric(ctxp, NULL, NULL, "of all cache refs", 0);
725 } else if (perf_evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES_FRONTEND)) {
726 print_stalled_cycles_frontend(cpu, evsel, avg, out);
727 } else if (perf_evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES_BACKEND)) {
728 print_stalled_cycles_backend(cpu, evsel, avg, out);
729 } else if (perf_evsel__match(evsel, HARDWARE, HW_CPU_CYCLES)) {
730 total = avg_stats(&runtime_nsecs_stats[cpu]);
734 print_metric(ctxp, NULL, "%8.3f", "GHz", ratio);
736 print_metric(ctxp, NULL, NULL, "Ghz", 0);
738 } else if (perf_stat_evsel__is(evsel, CYCLES_IN_TX)) {
739 total = avg_stats(&runtime_cycles_stats[ctx][cpu]);
741 print_metric(ctxp, NULL,
742 "%7.2f%%", "transactional cycles",
743 100.0 * (avg / total));
745 print_metric(ctxp, NULL, NULL, "transactional cycles",
747 } else if (perf_stat_evsel__is(evsel, CYCLES_IN_TX_CP)) {
748 total = avg_stats(&runtime_cycles_stats[ctx][cpu]);
749 total2 = avg_stats(&runtime_cycles_in_tx_stats[ctx][cpu]);
753 print_metric(ctxp, NULL, "%7.2f%%", "aborted cycles",
754 100.0 * ((total2-avg) / total));
756 print_metric(ctxp, NULL, NULL, "aborted cycles", 0);
757 } else if (perf_stat_evsel__is(evsel, TRANSACTION_START)) {
758 total = avg_stats(&runtime_cycles_in_tx_stats[ctx][cpu]);
763 if (runtime_cycles_in_tx_stats[ctx][cpu].n != 0)
764 print_metric(ctxp, NULL, "%8.0f",
765 "cycles / transaction", ratio);
767 print_metric(ctxp, NULL, NULL, "cycles / transaction",
769 } else if (perf_stat_evsel__is(evsel, ELISION_START)) {
770 total = avg_stats(&runtime_cycles_in_tx_stats[ctx][cpu]);
775 print_metric(ctxp, NULL, "%8.0f", "cycles / elision", ratio);
776 } else if (perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK) ||
777 perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK)) {
778 if ((ratio = avg_stats(&walltime_nsecs_stats)) != 0)
779 print_metric(ctxp, NULL, "%8.3f", "CPUs utilized",
782 print_metric(ctxp, NULL, NULL, "CPUs utilized", 0);
783 } else if (perf_stat_evsel__is(evsel, TOPDOWN_FETCH_BUBBLES)) {
784 double fe_bound = td_fe_bound(ctx, cpu);
787 color = PERF_COLOR_RED;
788 print_metric(ctxp, color, "%8.1f%%", "frontend bound",
790 } else if (perf_stat_evsel__is(evsel, TOPDOWN_SLOTS_RETIRED)) {
791 double retiring = td_retiring(ctx, cpu);
794 color = PERF_COLOR_GREEN;
795 print_metric(ctxp, color, "%8.1f%%", "retiring",
797 } else if (perf_stat_evsel__is(evsel, TOPDOWN_RECOVERY_BUBBLES)) {
798 double bad_spec = td_bad_spec(ctx, cpu);
801 color = PERF_COLOR_RED;
802 print_metric(ctxp, color, "%8.1f%%", "bad speculation",
804 } else if (perf_stat_evsel__is(evsel, TOPDOWN_SLOTS_ISSUED)) {
805 double be_bound = td_be_bound(ctx, cpu);
806 const char *name = "backend bound";
807 static int have_recovery_bubbles = -1;
809 /* In case the CPU does not support topdown-recovery-bubbles */
810 if (have_recovery_bubbles < 0)
811 have_recovery_bubbles = pmu_have_event("cpu",
812 "topdown-recovery-bubbles");
813 if (!have_recovery_bubbles)
814 name = "backend bound/bad spec";
817 color = PERF_COLOR_RED;
818 if (td_total_slots(ctx, cpu) > 0)
819 print_metric(ctxp, color, "%8.1f%%", name,
822 print_metric(ctxp, NULL, NULL, name, 0);
823 } else if (evsel->metric_expr) {
824 struct parse_ctx pctx;
827 expr__ctx_init(&pctx);
828 expr__add_id(&pctx, evsel->name, avg);
829 for (i = 0; evsel->metric_events[i]; i++) {
830 struct saved_value *v;
832 v = saved_value_lookup(evsel->metric_events[i], cpu, ctx, false);
835 expr__add_id(&pctx, evsel->metric_events[i]->name,
836 avg_stats(&v->stats));
838 if (!evsel->metric_events[i]) {
839 const char *p = evsel->metric_expr;
841 if (expr__parse(&ratio, &pctx, &p) == 0)
842 print_metric(ctxp, NULL, "%8.1f",
845 out->force_header ? evsel->name : "",
848 print_metric(ctxp, NULL, NULL, "", 0);
850 print_metric(ctxp, NULL, NULL, "", 0);
851 } else if (runtime_nsecs_stats[cpu].n != 0) {
855 total = avg_stats(&runtime_nsecs_stats[cpu]);
858 ratio = 1000.0 * avg / total;
863 snprintf(unit_buf, sizeof(unit_buf), "%c/sec", unit);
864 print_metric(ctxp, NULL, "%8.3f", unit_buf, ratio);
865 } else if (perf_stat_evsel__is(evsel, SMI_NUM)) {
866 print_smi_cost(cpu, evsel, out);
868 print_metric(ctxp, NULL, NULL, NULL, 0);