KVM: selftests: Provide generic way to read system counter
PATCH 1/1 KVM: selftests: Provide generic way to read system counter
Provide a generic function to read the system counter from the guest for timing purposes. An increasingly common and important way to measure guest performance is to measure the amount of time different actions take in the guest. Provide also a mathematical conversion from cycles to nanoseconds and a macro for timing individual statements.
Substitute the previous custom implementation of a similar function in system_counter_offset_test with this new implementation.
---
These functions were originally part of my patch to introduce latency measurements into dirty_log_perf_test. [1] Sean Christopherson suggested lifting these functions into their own patch in generic code so they can be used by any test. [2] Ricardo Koller suggested the addition of the MEASURE macro to more easily time individual statements. [3]
[1] https://lore.kernel.org/kvm/[email protected]/
[2] https://lore.kernel.org/kvm/[email protected]/
[3] https://lore.kernel.org/kvm/[email protected]/
.../testing/selftests/kvm/include/test_util.h | 3 ++ tools/testing/selftests/kvm/lib/test_util.c | 37 +++++++++++++++++++ .../kvm/system_counter_offset_test.c | 10 +---- 3 files changed, 42 insertions(+), 8 deletions(-)
tools/testing/selftests/kvm/include/test_util.h +3 -0
@@ -84,6 +84,9 @@ struct guest_random_state {struct guest_random_state new_guest_random_state (uint32_t seed);uint32_t guest_random_u32 (struct guest_random_state *state); +uint64_t guest_cycles_read (void ); +double guest_cycles_to_ns (double cycles); +enum vm_mem_backing_src_type {VM_MEM_SRC_ANONYMOUS ,VM_MEM_SRC_ANONYMOUS_THP ,
tools/testing/selftests/kvm/lib/test_util.c +37 -0
@@ -15,6 +15,11 @@#include <linux/mman.h> #include "linux/kernel.h" +#if defined(__aarch64__) +#include "aarch64/arch_timer.h" +#elif defined(__x86_64__) +#include "x86_64/processor.h" +#endif #include "test_util.h" /*
@@ -34,6 +39,38 @@ uint32_t guest_random_u32(struct guest_random_state *state)return state->seed; } +uint64_t guest_cycles_read (void ) +{ +#if defined(__aarch64__) +return timer_get_cntct (VIRTUAL ); +#elif defined(__x86_64__) +return rdtsc (); +#else +#warn __func__ " is not implemented for this architecture, will return 0" +return 0 ; +#endif +} + +double guest_cycles_to_ns (double cycles) +{ +#if defined(__aarch64__) +return cycles * (1e9 /timer_get_cntfrq ()); +#elif defined(__x86_64__) +return cycles * (1e9 / (KVM_GET_TSC_KHZ *1000 )); +#else +#warn __func__ " is not implemented for this architecture, will return 0" +return 0.0 ; +#endif +} + +#define MEASURE_CYCLES (x) \ + ({ \ + uint64_t start; \ + start = guest_cycles_read(); \ + x; \ + guest_cycles_read() - start; \ + }) +/* * Parses "[0-9]+[kmgt]?". */
tools/testing/selftests/kvm/system_counter_offset_test.c +2 -8
@@ -39,14 +39,9 @@ static void setup_system_counter(struct kvm_vcpu *vcpu, struct test_case *test) &test->tsc_offset); } -static uint64_t guest_read_system_counter (struct test_case *test) -{ -return rdtsc (); -} -static uint64_t host_read_guest_system_counter (struct test_case *test) { -return rdtsc () + test->tsc_offset; +return guest_cycles_read () + test->tsc_offset; }#else /* __x86_64__ */
@@ -63,9 +58,8 @@ static void guest_main(void)int i;for (i =0 ; i <ARRAY_SIZE (test_cases); i++) { -struct test_case *test = &test_cases[i]; -GUEST_SYNC_CLOCK (i,guest_read_system_counter (test)); +GUEST_SYNC_CLOCK (i,guest_cycles_read ()); } }
--
2.39.2.637.g21b0678d19-goog
Hi Colton,
Quoted 19 lines · Colton Lewis
Provide a generic function to read the system counter from the guest for timing purposes. An increasingly common and important way to measure guest performance is to measure the amount of time different actions take in the guest. Provide also a mathematical conversion from cycles to nanoseconds and a macro for timing individual statements.
Substitute the previous custom implementation of a similar function in
system_counter_offset_testwith this new implementation.Signed-off-by Colton Lewis <[email protected]>---
These functions were originally part of my patch to introduce latency measurements into
dirty_log_perf_test. [1] Sean Christopherson suggested lifting these functions into their own patch in generic code so they can be used by any test. [2] Ricardo Koller suggested the addition of the MEASURE macro to more easily time individual statements. [3]
Quoted 3 lines
[1] https://lore.kernel.org/kvm/[email protected]/
[2] https://lore.kernel.org/kvm/[email protected]/
[3] https://lore.kernel.org/kvm/[email protected]/
This patch doesn't make a great deal of sense outside of [1]. Can you send this as part of your larger series next time around?
Quoted 24 lines
.../testing/selftests/kvm/include/test_util.h | 3 ++ tools/testing/selftests/kvm/lib/test_util.c | 37 +++++++++++++++++++ .../kvm/system_counter_offset_test.c | 10 +---- 3 files changed, 42 insertions(+), 8 deletions(-)tools/testing/selftests/kvm/include/test_util.h +2
-0@@ -84,6 +84,9 @@ struct guest_random_state {struct guest_random_state new_guest_random_state (uint32_t seed);uint32_t guest_random_u32 (struct guest_random_state *state); +uint64_t guest_cycles_read (void ); +double guest_cycles_to_ns (double cycles);enum vm_mem_backing_src_type {VM_MEM_SRC_ANONYMOUS ,VM_MEM_SRC_ANONYMOUS_THP ,tools/testing/selftests/kvm/lib/test_util.c +0
-0
tools/testing/selftests/kvm/lib/test_util.c line 22
Quoted 8 lines
tools/testing/selftests/kvm/lib/test_util.c +5
-0@@ -15,6 +15,11 @@#include <linux/mman.h> #include "linux/kernel.h" +#if defined(__aarch64__) +#include "aarch64/arch_timer.h" +#elif defined(__x86_64__) +#include "x86_64/processor.h" +#endif
I believe we place 'include/$(ARCH)/' on the include path, so the 'x86_64' can be dropped.
tools/testing/selftests/kvm/lib/test_util.c uint32_t guest_random_u32(struct guest_random_state *state) · line 42
Quoted 8 lines
tools/testing/selftests/kvm/lib/test_util.c +1
-0#include "test_util.h" /* @@ -34,6 +39,38 @@ uint32_t guest_random_u32(struct guest_random_state *state)return state->seed; } +uint64_t guest_cycles_read (void )
Do we need the 'guest_' prefix in here? At least for x86 and arm64 the same instructions can be used in host userspace and guest kernel for accessing the counter.
tools/testing/selftests/kvm/lib/test_util.c uint32_t guest_random_u32(struct guest_random_state *state) · line 52
Quoted 10 lines
tools/testing/selftests/kvm/lib/test_util.c +10
-0+{ +#if defined(__aarch64__) +return timer_get_cntct (VIRTUAL ); +#elif defined(__x86_64__) +return rdtsc (); +#else +#warn __func__ " is not implemented for this architecture, will return 0" +return 0 ; +#endif +}
I think it would be cleaner if each architecture just provided its own implementation of this function instead of ifdeffery here.
If an arch doesn't implement the requisite helper then it will become painfully obvious at compile time.
tools/testing/selftests/kvm/lib/test_util.c uint32_t guest_random_u32(struct guest_random_state *state) · line 59
Quoted 6 lines
tools/testing/selftests/kvm/lib/test_util.c +6
-0+double guest_cycles_to_ns (double cycles) +{ +#if defined(__aarch64__) +return cycles * (1e9 /timer_get_cntfrq ()); +#elif defined(__x86_64__) +return cycles * (1e9 / (KVM_GET_TSC_KHZ *1000 ));
The x86 implementation is wrong. KVM_GET_TSC_KHZ is the ioctl needed to get at the guest's TSC frequency (from the perpsective of host userspace).
So at the very least this needs to do an ioctl on the VM fd to work out the frequency. This is expected to be called in host userspace, right?
Quoted 24 lines
tools/testing/selftests/kvm/lib/test_util.c +14
-0+#else +#warn __func__ " is not implemented for this architecture, will return 0" +return 0.0 ; +#endif +} + +#define MEASURE_CYCLES (x) \ + ({ \ + uint64_t start; \ + start = guest_cycles_read(); \ + x; \ + guest_cycles_read() - start; \ + }) +/* * Parses "[0-9]+[kmgt]?". */ tools/testing/selftests/kvm/system_counter_offset_test.c +0
-0@@ -39,14 +39,9 @@ static void setup_system_counter(struct kvm_vcpu *vcpu, struct test_case *test) &test->tsc_offset); }
tools/testing/selftests/kvm/system_counter_offset_test.c static void setup_system_counter(struct kvm_vcpu *vcpu, struct test_case *test) · line 44
Quoted 10 lines
tools/testing/selftests/kvm/system_counter_offset_test.c +1
-6-static uint64_t guest_read_system_counter (struct test_case *test) -{ -return rdtsc (); -} -static uint64_t host_read_guest_system_counter (struct test_case *test) { -return rdtsc () + test->tsc_offset; +return guest_cycles_read () + test->tsc_offset;
The 'guest_' part is rather confusing here. What this function is really trying to do is read the counter from host userspace and apply an offset to construct the expected value for the test.
It all compiles down to the same thing, but as written is seemingly wrong.
Signature · 2 lines
Thanks, Oliver
Quoted 7 lines · Oliver Upton
Quoted 6 lines
These functions were originally part of my patch to introduce latency measurements into
dirty_log_perf_test. [1] Sean Christopherson suggested lifting these functions into their own patch in generic code so they can be used by any test. [2] Ricardo Koller suggested the addition of the MEASURE macro to more easily time individual statements. [3]
Quoted 6 lines
Quoted 3 lines
[1] https://lore.kernel.org/kvm/[email protected]/
[2] https://lore.kernel.org/kvm/[email protected]/
[3] https://lore.kernel.org/kvm/[email protected]/This patch doesn't make a great deal of sense outside of [1]. Can you send this as part of your larger series next time around?
I copied the wrong email link where Sean suggested this should be generic code in a separate patch. I may have been mistaken in thinking he meant to upstream it separately.
https://lore.kernel.org/kvm/[email protected]/
But yes, I would prefer to keep this as part of the larger series.
Quoted 11 lines
tools/testing/selftests/kvm/lib/test_util.c line 22
Quoted 8 lines
#include <linux/mman.h> #include "linux/kernel.h"tools/testing/selftests/kvm/lib/test_util.c +5
-0+#if defined(__aarch64__) +#include "aarch64/arch_timer.h" +#elif defined(__x86_64__) +#include "x86_64/processor.h" +#endif I believe we place 'include/$(ARCH)/' on the include path, so the '
x86_64' can be dropped.
Good to know.
Quoted 8 lines
Quoted 7 lines
#include "test_util.h"/*
tools/testing/selftests/kvm/lib/test_util.c +0
-0@@ -34,6 +39,38 @@ uint32_t guest_random_u32(struct guest_random_state *state)return state->seed; }
Quoted 5 lines
tools/testing/selftests/kvm/lib/test_util.c uint32_t guest_random_u32(struct guest_random_state *state) · line 42
Quoted 1 line
tools/testing/selftests/kvm/lib/test_util.c +1
-0+uint64_t guest_cycles_read (void )Do we need the '
guest_' prefix in here? At least for x86 and arm64 the same instructions can be used in host userspace and guest kernel for accessing the counter.
It's intended for guest space, but I take your point it isn't necessary.
Quoted 15 lines
tools/testing/selftests/kvm/lib/test_util.c uint32_t guest_random_u32(struct guest_random_state *state) · line 52
Quoted 11 lines
tools/testing/selftests/kvm/lib/test_util.c +7
-0+{ +#if defined(__aarch64__) +return timer_get_cntct (VIRTUAL ); +#elif defined(__x86_64__) +return rdtsc (); +#else +#warn __func__ " is not implemented for this architecture, will return0"
tools/testing/selftests/kvm/lib/test_util.c +3
-0+return 0 ; +#endif +}I think it would be cleaner if each architecture just provided its own implementation of this function instead of ifdeffery here.
Quoted 2 lines
If an arch doesn't implement the requisite helper then it will become painfully obvious at compile time.
Good point. That will be more clear.
Quoted 10 lines
tools/testing/selftests/kvm/lib/test_util.c uint32_t guest_random_u32(struct guest_random_state *state) · line 59
Quoted 6 lines
tools/testing/selftests/kvm/lib/test_util.c +6
-0+double guest_cycles_to_ns (double cycles) +{ +#if defined(__aarch64__) +return cycles * (1e9 /timer_get_cntfrq ()); +#elif defined(__x86_64__) +return cycles * (1e9 / (KVM_GET_TSC_KHZ *1000 ));The x86 implementation is wrong.
KVM_GET_TSC_KHZis the ioctl needed to get at the guest's TSC frequency (from the perpsective of host userspace).
Oops. Absent minded on that one. Forgot it wasn't just a value I could use.
Quoted 2 lines
So at the very least this needs to do an ioctl on the VM fd to work out the frequency. This is expected to be called in host userspace, right?
That's the plan yes.
Quoted 23 lines
Quoted 8 lines
tools/testing/selftests/kvm/system_counter_offset_test.c +0
-0@@ -39,14 +39,9 @@ static void setup_system_counter(struct kvm_vcpu *vcpu, struct test_case *test) &test->tsc_offset); }tools/testing/selftests/kvm/system_counter_offset_test.c static void setup_system_counter(struct kvm_vcpu · line 45
Quoted 10 lines
tools/testing/selftests/kvm/system_counter_offset_test.c +1
-6-static uint64_t guest_read_system_counter (struct test_case *test) -{ -return rdtsc (); -} -static uint64_t host_read_guest_system_counter (struct test_case *test) { -return rdtsc () + test->tsc_offset; +return guest_cycles_read () + test->tsc_offset;The '
guest_' part is rather confusing here. What this function is really trying to do is read the counter from host userspace and apply an offset to construct the expected value for the test.
Quoted 2 lines
It all compiles down to the same thing, but as written is seemingly wrong.
I'll eliminate the guest_prefix.
Sean Christopherson
reviewerQuoted 16 lines · Colton Lewis
Quoted 7 lines · Oliver Upton
Quoted 6 lines
These functions were originally part of my patch to introduce latency measurements into
dirty_log_perf_test. [1] Sean Christopherson suggested lifting these functions into their own patch in generic code so they can be used by any test. [2] Ricardo Koller suggested the addition of the MEASURE macro to more easily time individual statements. [3]Quoted 6 lines
Quoted 3 lines
[1] https://lore.kernel.org/kvm/[email protected]/
[2] https://lore.kernel.org/kvm/[email protected]/
[3] https://lore.kernel.org/kvm/[email protected]/This patch doesn't make a great deal of sense outside of [1]. Can you send this as part of your larger series next time around?
Quoted 7 lines
I copied the wrong email link where Sean suggested this should be generic code in a separate patch. I may have been mistaken in thinking he meant to upstream it separately.
https://lore.kernel.org/kvm/[email protected]/
But yes, I would prefer to keep this as part of the larger series.
Yeah, I just meant put it into a separate patch, but keep it in the series.