7
7
#include "pmu.h"
8
8
#include "processor.h"
9
9
10
- /* Number of LOOP instructions for the guest measurement payload. */
11
- #define NUM_BRANCHES 10
10
+ /* Number of iterations of the loop for the guest measurement payload. */
11
+ #define NUM_LOOPS 10
12
+
13
+ /* Each iteration of the loop retires one branch instruction. */
14
+ #define NUM_BRANCH_INSNS_RETIRED (NUM_LOOPS)
15
+
16
+ /* Number of instructions in each loop. */
17
+ #define NUM_INSNS_PER_LOOP 1
18
+
12
19
/*
13
20
* Number of "extra" instructions that will be counted, i.e. the number of
14
- * instructions that are needed to set up the loop and then disabled the
21
+ * instructions that are needed to set up the loop and then disable the
15
22
* counter. 1 CLFLUSH/CLFLUSHOPT/NOP, 1 MFENCE, 2 MOV, 2 XOR, 1 WRMSR.
16
23
*/
17
- #define NUM_EXTRA_INSNS 7
18
- #define NUM_INSNS_RETIRED (NUM_BRANCHES + NUM_EXTRA_INSNS)
24
+ #define NUM_EXTRA_INSNS 7
25
+
26
+ /* Total number of instructions retired within the measured section. */
27
+ #define NUM_INSNS_RETIRED (NUM_LOOPS * NUM_INSNS_PER_LOOP + NUM_EXTRA_INSNS)
28
+
19
29
20
30
static uint8_t kvm_pmu_version ;
21
31
static bool kvm_has_perf_caps ;
@@ -100,7 +110,7 @@ static void guest_assert_event_count(uint8_t idx,
100
110
GUEST_ASSERT_EQ (count , NUM_INSNS_RETIRED );
101
111
break ;
102
112
case INTEL_ARCH_BRANCHES_RETIRED_INDEX :
103
- GUEST_ASSERT_EQ (count , NUM_BRANCHES );
113
+ GUEST_ASSERT_EQ (count , NUM_BRANCH_INSNS_RETIRED );
104
114
break ;
105
115
case INTEL_ARCH_LLC_REFERENCES_INDEX :
106
116
case INTEL_ARCH_LLC_MISSES_INDEX :
@@ -120,7 +130,7 @@ static void guest_assert_event_count(uint8_t idx,
120
130
}
121
131
122
132
sanity_checks :
123
- __asm__ __volatile__("loop ." : "+c" ((int ){NUM_BRANCHES }));
133
+ __asm__ __volatile__("loop ." : "+c" ((int ){NUM_LOOPS }));
124
134
GUEST_ASSERT_EQ (_rdpmc (pmc ), count );
125
135
126
136
wrmsr (pmc_msr , 0xdead );
@@ -147,7 +157,7 @@ do { \
147
157
__asm__ __volatile__("wrmsr\n\t" \
148
158
clflush "\n\t" \
149
159
"mfence\n\t" \
150
- "1: mov $" __stringify(NUM_BRANCHES ) ", %%ecx\n\t" \
160
+ "1: mov $" __stringify(NUM_LOOPS ) ", %%ecx\n\t" \
151
161
FEP "loop .\n\t" \
152
162
FEP "mov %%edi, %%ecx\n\t" \
153
163
FEP "xor %%eax, %%eax\n\t" \
@@ -500,7 +510,7 @@ static void guest_test_fixed_counters(void)
500
510
wrmsr (MSR_CORE_PERF_FIXED_CTR0 + i , 0 );
501
511
wrmsr (MSR_CORE_PERF_FIXED_CTR_CTRL , FIXED_PMC_CTRL (i , FIXED_PMC_KERNEL ));
502
512
wrmsr (MSR_CORE_PERF_GLOBAL_CTRL , FIXED_PMC_GLOBAL_CTRL_ENABLE (i ));
503
- __asm__ __volatile__("loop ." : "+c" ((int ){NUM_BRANCHES }));
513
+ __asm__ __volatile__("loop ." : "+c" ((int ){NUM_LOOPS }));
504
514
wrmsr (MSR_CORE_PERF_GLOBAL_CTRL , 0 );
505
515
val = rdmsr (MSR_CORE_PERF_FIXED_CTR0 + i );
506
516
0 commit comments