KVM: selftests: Calculate memory latency stats
v3 cover KVM: selftests: Calculate memory latency stats
Sample the latency of memory accesses in dirty_log_perf_test and report summary stats to give a picture of the latency distribution. Specifically, focus on the right tail with the 50th, 90th, and 99th percentile reported in ns.
v3:
* Move timing functions to lib/$ARCH/processor.c
* Add memory barriers to prevent reordering measured memory accesses
* Remove floating point from conversion functions
* Change command line flag to -l for latency and provide help summary
* Do not show latency measurements unless -l is given with argument
* Change output formatting to seconds for consistency with other output
v2: https://lore.kernel.org/kvm/[email protected]/
v1: https://lore.kernel.org/kvm/[email protected]/
Colton Lewis (2):
KVM: selftests: Provide generic way to read system counter
KVM: selftests: Print summary stats of memory latency distribution
.../selftests/kvm/access_tracking_perf_test.c | 3 +- .../selftests/kvm/demand_paging_test.c | 2 +- .../selftests/kvm/dirty_log_perf_test.c | 21 +++++- .../selftests/kvm/include/aarch64/processor.h | 13 ++++ .../testing/selftests/kvm/include/memstress.h | 10 ++- .../selftests/kvm/include/x86_64/processor.h | 13 ++++ .../selftests/kvm/lib/aarch64/processor.c | 12 ++++ tools/testing/selftests/kvm/lib/memstress.c | 68 ++++++++++++++++--- .../selftests/kvm/lib/x86_64/processor.c | 13 ++++ .../kvm/memslot_modification_stress_test.c | 2 +- .../kvm/system_counter_offset_test.c | 10 +-- 11 files changed, 143 insertions(+), 24 deletions(-)
--
2.40.0.348.gf938b09366-goog
PATCH v3 1/2 KVM: selftests: Provide generic way to read system counter
Provide a generic function cycles_read to read the system counter from the guest for timing purposes and a helper cycles_to_ns to convert to nanoseconds. cycles_to_ns may overflow if the cycles argument goes above 10 billion, but that is far outside the intended use of these functions and different timing instruments should be
used. clocks_calc_mult_shift could be used to solve this problem, but importing clocksource code here was annoying.
Substitute the previous custom implementation of a similar function in system_counter_offset_test with this new implementation.
--- .../selftests/kvm/include/aarch64/processor.h | 3 +++ .../selftests/kvm/include/x86_64/processor.h | 3 +++ tools/testing/selftests/kvm/lib/aarch64/processor.c | 12 ++++++++++++ tools/testing/selftests/kvm/lib/x86_64/processor.c | 13 +++++++++++++ .../selftests/kvm/system_counter_offset_test.c | 10 ++-------- 5 files changed, 33 insertions(+), 8 deletions(-)
tools/testing/selftests/kvm/include/aarch64/processor.h +3 -0
@@ -216,4 +216,7 @@ void smccc_hvc(uint32_t function_id, uint64_t arg0, uint64_t arg1,uint32_t guest_get_vcpuid (void ); +uint64_t cycles_read (void ); +uint64_t cycles_to_ns (struct kvm_vcpu *vcpu,uint64_t cycles); +#endif /* SELFTEST_KVM_PROCESSOR_H */
tools/testing/selftests/kvm/include/x86_64/processor.h +3 -0
@@ -1134,4 +1134,7 @@ void virt_map_level(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,#define PFERR_GUEST_PAGE_MASK BIT_ULL(PFERR_GUEST_PAGE_BIT)#define PFERR_IMPLICIT_ACCESS BIT_ULL(PFERR_IMPLICIT_ACCESS_BIT) +uint64_t cycles_read (void ); +uint64_t cycles_to_ns (struct kvm_vcpu *vcpu,uint64_t cycles); +#endif /* SELFTEST_KVM_PROCESSOR_H */
tools/testing/selftests/kvm/lib/aarch64/processor.c +12 -0
@@ -8,6 +8,7 @@#include <linux/compiler.h> #include <assert.h> +#include "arch_timer.h" #include "guest_modes.h" #include "kvm_util.h" #include "processor.h"
@@ -551,3 +552,14 @@ void vm_vaddr_populate_bitmap(struct kvm_vm *vm)sparsebit_set_num (vm->vpages_valid,0 , (1ULL << vm->va_bits) >> vm->page_shift); } + +uint64_t cycles_read (void ) +{ +return timer_get_cntct (VIRTUAL ); +} + +uint64_t cycles_to_ns (struct kvm_vcpu *vcpu,uint64_t cycles) +{ +TEST_ASSERT (cycles <10000000000 ,"Conversion to ns may overflow" ); +return cycles *NSEC_PER_SEC /timer_get_cntfrq (); +}
tools/testing/selftests/kvm/lib/x86_64/processor.c +13 -0
@@ -1270,3 +1270,16 @@ void kvm_selftest_arch_init(void) host_cpu_is_intel =this_cpu_is_intel (); host_cpu_is_amd =this_cpu_is_amd (); } + +uint64_t cycles_read (void ) +{ +return rdtsc (); +} + +uint64_t cycles_to_ns (struct kvm_vcpu *vcpu,uint64_t cycles) +{ +uint64_t tsc_khz =__vcpu_ioctl (vcpu,KVM_GET_TSC_KHZ ,NULL ); + +TEST_ASSERT (cycles <10000000000 ,"Conversion to ns may overflow" ); +return cycles *NSEC_PER_SEC / (tsc_khz *1000 ); +}
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 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,cycles_read ()); } }
--
2.40.0.348.gf938b09366-goog
PATCH v3 2/2 KVM: selftests: Print summary stats of memory latency distribution
In reply to Colton LewisPrint summary stats of the memory latency distribution in nanoseconds for dirty_log_perf_test. For every iteration, this prints the minimum, the 50th percentile, the 90th percentile, the 99th percentile, and the maximum. These percentiles are common in latency testing to give a picture of the right half of the distribution, particularly the worst cases.
Stats are calculated by sorting the samples taken from all vcpus and picking from the index corresponding with each percentile.
Because keeping all samples is impractical due to the space required in some cases (pooled memory w/ 64 vcpus would be 64 GB/vcpu * 64 vcpus * 250,000 samples/GB * 8 bytes/sample ~ 8 GB extra memory just for samples), reservoir sampling [1] is used to only keep a small number of samples per vcpu (settable via command argument). There is a tradeoff between stat accuracy and memory usage. More samples means more accuracy but also more memory use.
Because other selftests use memstress.c, those tests were hardcoded to take 0 samples at this time.
[1] https://en.wikipedia.org/wiki/Reservoir_sampling#Simple:_Algorithm_R
--- .../selftests/kvm/access_tracking_perf_test.c | 3 +- .../selftests/kvm/demand_paging_test.c | 2 +- .../selftests/kvm/dirty_log_perf_test.c | 21 +++++- .../selftests/kvm/include/aarch64/processor.h | 10 +++ .../testing/selftests/kvm/include/memstress.h | 10 ++- .../selftests/kvm/include/x86_64/processor.h | 10 +++ tools/testing/selftests/kvm/lib/memstress.c | 68 ++++++++++++++++--- .../kvm/memslot_modification_stress_test.c | 2 +- 8 files changed, 110 insertions(+), 16 deletions(-)
tools/testing/selftests/kvm/access_tracking_perf_test.c +2 -1
@@ -306,7 +306,8 @@ static void run_test(enum vm_guest_mode mode, void *arg)struct kvm_vm *vm;int nr_vcpus = params->nr_vcpus; - vm =memstress_create_vm (mode, nr_vcpus, params->vcpu_memory_bytes,1 , + vm =memstress_create_vm (mode, nr_vcpus, params->vcpu_memory_bytes, +0 ,1 , params->backing_src, !overlap_memory_access);memstress_start_vcpu_threads (nr_vcpus, vcpu_thread_main);
tools/testing/selftests/kvm/demand_paging_test.c +1 -1
@@ -135,7 +135,7 @@ static void run_test(enum vm_guest_mode mode, void *arg)struct kvm_vm *vm;int i; - vm =memstress_create_vm (mode, nr_vcpus, guest_percpu_mem_size,1 , + vm =memstress_create_vm (mode, nr_vcpus, guest_percpu_mem_size,0 ,1 , p->src_type, p->partition_vcpu_memory_access); demand_paging_size =get_backing_src_pagesz (p->src_type);
tools/testing/selftests/kvm/dirty_log_perf_test.c +18 -3
@@ -133,6 +133,7 @@ struct test_params {int slots;uint32_t write_percent;uint32_t random_seed; +uint64_t samples_per_vcpu;bool random_access; };
@@ -224,6 +225,7 @@ static void run_test(enum vm_guest_mode mode, void *arg)int i; vm =memstress_create_vm (mode, nr_vcpus, guest_percpu_mem_size, + p->samples_per_vcpu, p->slots, p->backing_src, p->partition_vcpu_memory_access);
@@ -275,6 +277,9 @@ static void run_test(enum vm_guest_mode mode, void *arg)pr_info ("Populate memory time: %ld.%.9lds\n" , ts_diff.tv_sec, ts_diff.tv_nsec); +if (p->samples_per_vcpu) +memstress_print_percentiles (vm, nr_vcpus); +/* Enable dirty logging */ clock_gettime (CLOCK_MONOTONIC , &start);enable_dirty_logging (vm, p->slots);
@@ -305,6 +310,9 @@ static void run_test(enum vm_guest_mode mode, void *arg)pr_info ("Iteration %d dirty memory time: %ld.%.9lds\n" , iteration, ts_diff.tv_sec, ts_diff.tv_nsec); +if (p->samples_per_vcpu) +memstress_print_percentiles (vm, nr_vcpus); +clock_gettime (CLOCK_MONOTONIC , &start);get_dirty_log (vm, bitmaps, p->slots); ts_diff =timespec_elapsed (start);
@@ -367,8 +375,8 @@ static void run_test(enum vm_guest_mode mode, void *arg)static void help (char *name) {puts ("" ); -printf ("usage: %s [-h] [-a] [-i iterations] [-p offset] [-g] " -"[-m mode] [-n] [-b vcpu bytes] [-v vcpus] [-o] [-r random seed ] [-s mem type]" +printf ("usage: %s [-h] [-a] [-i iterations] [-p offset] [-g] [-l samples per vcpu] " +"[-m mode] [-n] [-b vcpu bytes] [-v vcpus] [-o] [-r random seed ] [-s mem type] " "[-x memslots] [-w percentage] [-c physical cpus to run test on]\n" , name);puts ("" );printf (" -a: access memory randomly rather than in order.\n" );
@@ -382,6 +390,9 @@ static void help(char *name) " is not enabled).\n");printf (" -p: specify guest physical test memory offset\n" " Warning: a low offset can conflict with the loaded test code.\n" ); +printf (" -l: Turn on latency measurements and specify number of samples\n" +" to store per vcpu. More samples means more accuracy at the cost\n" +" of additional memory use. 1000 is a good value.\n" );guest_modes_help ();printf (" -n: Run the vCPUs in nested mode (L2)\n" ); printf(" -e: Run vCPUs while dirty logging is being disabled. This\n"
@@ -428,6 +439,7 @@ int main(int argc, char *argv[]) .slots = 1, .random_seed = 1, .write_percent = 100, + .samples_per_vcpu = 0, };int opt;
@@ -438,7 +450,7 @@ int main(int argc, char *argv[])guest_modes_append_default (); -while ((opt =getopt (argc, argv,"ab:c:eghi:m:nop:r:s:v:x:w:" )) !=-1 ) { +while ((opt =getopt (argc, argv,"ab:c:eghi:l:m:nop:r:s:v:x:w:" )) !=-1 ) {switch (opt) {case 'a' : p.random_access = true;
@@ -462,6 +474,9 @@ int main(int argc, char *argv[])case 'i' : p.iterations =atoi_positive ("Number of iterations" , optarg);break ; +case 'l' : + p.samples_per_vcpu =atoi_positive ("Number of samples/vcpu" , optarg); +break ;case 'm' :guest_modes_cmdline (optarg);break ;
tools/testing/selftests/kvm/include/aarch64/processor.h +10 -0
@@ -219,4 +219,14 @@ uint32_t guest_get_vcpuid(void);uint64_t cycles_read (void );uint64_t cycles_to_ns (struct kvm_vcpu *vcpu,uint64_t cycles); +#define MEASURE_CYCLES (x) \ + ({ \ + uint64_t start; \ + start = cycles_read(); \ + isb(); \ + x; \ + dsb(nsh); \ + cycles_read() - start; \ + }) +#endif /* SELFTEST_KVM_PROCESSOR_H */
tools/testing/selftests/kvm/include/memstress.h +9 -1
@@ -37,6 +37,12 @@ struct memstress_args {uint64_t guest_page_size;uint32_t random_seed;uint32_t write_percent; +uint64_t samples_per_vcpu; +/* Store all samples in a flat array so they can be easily +* sorted later. +*/ +vm_vaddr_t latency_samples; +/* Run vCPUs in L2 instead of L1, if the architecture supports it. */ bool nested;
@@ -56,7 +62,8 @@ struct memstress_args {extern struct memstress_args memstress_args;struct kvm_vm *memstress_create_vm (enum vm_guest_mode mode,int nr_vcpus, -uint64_t vcpu_memory_bytes,int slots, +uint64_t vcpu_memory_bytes, +uint64_t samples_per_vcpu,int slots,enum vm_mem_backing_src_type backing_src,bool partition_vcpu_memory_access);void memstress_destroy_vm (struct kvm_vm *vm);
@@ -72,4 +79,5 @@ void memstress_guest_code(uint32_t vcpu_id);uint64_t memstress_nested_pages (int nr_vcpus);void memstress_setup_nested (struct kvm_vm *vm,int nr_vcpus,struct kvm_vcpu *vcpus[]); +void memstress_print_percentiles (struct kvm_vm *vm,int nr_vcpus);#endif /* SELFTEST_KVM_MEMSTRESS_H */
tools/testing/selftests/kvm/include/x86_64/processor.h +10 -0
@@ -1137,4 +1137,14 @@ void virt_map_level(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,uint64_t cycles_read (void );uint64_t cycles_to_ns (struct kvm_vcpu *vcpu,uint64_t cycles); +#define MEASURE_CYCLES (x) \ + ({ \ + uint64_t start; \ + start = cycles_read(); \ + asm volatile("mfence"); \ + x; \ + asm volatile("mfence"); \ + cycles_read() - start; \ + }) +#endif /* SELFTEST_KVM_PROCESSOR_H */
tools/testing/selftests/kvm/lib/memstress.c +59 -9
134 lines not on this page · raw message
tools/testing/selftests/kvm/memslot_modification_stress_test.c +1 -1
@@ -95,7 +95,7 @@ static void run_test(enum vm_guest_mode mode, void *arg)struct test_params *p = arg;struct kvm_vm *vm; - vm =memstress_create_vm (mode, nr_vcpus, guest_percpu_mem_size,1 , + vm =memstress_create_vm (mode, nr_vcpus, guest_percpu_mem_size,0 ,1 ,VM_MEM_SRC_ANONYMOUS , p->partition_vcpu_memory_access);
--
2.40.0.348.gf938b09366-goog
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [PATCH v3 2/2] KVM: selftests: Print summary stats of memory latency distribution
Quoted 9 lines · Sean Christopherson
Quoted 8 lines · Colton Lewis
tools/testing/selftests/kvm/include/aarch64/processor.h +0
-0@@ -219,4 +219,14 @@ uint32_t guest_get_vcpuid(void);uint64_t cycles_read (void );uint64_t cycles_to_ns (struct kvm_vcpu *vcpu,uint64_t cycles);
Quoted 8 lines
tools/testing/selftests/kvm/include/aarch64/processor.h uint32_t guest_get_vcpuid(void); · line 226
Quoted 5 lines
tools/testing/selftests/kvm/include/aarch64/processor.h +5
-0+#define MEASURE_CYCLES (x) \ + ({ \ + uint64_t start; \ + start = cycles_read(); \ + isb(); \Would it make sense to put the necessary barriers inside the
cycles_read()(or whatever we end up calling it)? Or does that not make sense on ARM?
+1. Additionally, the function should have a name that implies ordering, like read_system_counter_ordered() or similar.
Quoted 2 lines
tools/testing/selftests/kvm/include/aarch64/processor.h uint32_t guest_get_vcpuid(void); · line 228
Quoted 2 lines
tools/testing/selftests/kvm/include/aarch64/processor.h +2
-0+ x; \ +dsb (nsh); \
I assume you're doing this because you want to wait for outstanding loads and stores to complete due to 'x', right?
My knee-jerk reaction was that you could just do an mb() and share the implementation between arches, but it would seem the tools/ flavor of the barrier demotes to a DMB because... reasons.
Quoted 14 lines
Quoted 4 lines
tools/testing/selftests/kvm/include/aarch64/processor.h +3
-0+cycles_read () - start; \ + }) +#endif /* SELFTEST_KVM_PROCESSOR_H */ ...
Quoted 8 lines
tools/testing/selftests/kvm/include/x86_64/processor.h +0
-0@@ -1137,4 +1137,14 @@ void virt_map_level(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,uint64_t cycles_read (void );uint64_t cycles_to_ns (struct kvm_vcpu *vcpu,uint64_t cycles);
Quoted 11 lines
tools/testing/selftests/kvm/include/x86_64/processor.h void virt_map_level(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, · line 1144
Quoted 5 lines
tools/testing/selftests/kvm/include/x86_64/processor.h +5
-0+#define MEASURE_CYCLES (x) \ + ({ \ + uint64_t start; \ + start = cycles_read(); \ + asm volatile("mfence"); \This is incorrect as placing the barrier after the RDTSC allows the RDTSC to be executed before earlier loads, e.g. could measure memory accesses from whatever was before
MEASURE_CYCLES(). And per the kernel'srdtsc_ordered(), it sounds like RDTSC can only be hoisted before prior loads, i.e. will be ordered with respect to future loads and stores.
Same thing goes for the arm64 variant of the function... You want to insert an isb() immediately _before_ you read the counter register to avoid speculation.
arch_timer_read_cntvct_el0() back over in the kernel is a good example of this. You can very likely ignore the ECV alternative for now.
Signature · 2 lines
Thanks, Oliver
Re: [PATCH v3 2/2] KVM: selftests: Print summary stats of memory latency distribution
Quoted 20 lines · Oliver Upton
Quoted 10 lines · Sean Christopherson
Review
Quoted 1 line · Colton Lewis
(quoted diff) +0
-0
b/tools/testing/selftests/kvm/include/aarch64/processor.hQuoted 7 lines
index
f65e491763e0..d441f485e9c6100644tools/testing/selftests/kvm/include/aarch64/processor.h +0
-0@@ -219,4 +219,14 @@ uint32_t guest_get_vcpuid(void);uint64_t cycles_read (void );uint64_t cycles_to_ns (struct kvm_vcpu *vcpu,uint64_t cycles);Quoted 8 lines
tools/testing/selftests/kvm/include/aarch64/processor.h uint32_t guest_get_vcpuid(void); · line 226
Quoted 5 lines
tools/testing/selftests/kvm/include/aarch64/processor.h +5
-0+#define MEASURE_CYCLES (x) \ + ({ \ + uint64_t start; \ + start = cycles_read(); \ + isb(); \Would it make sense to put the necessary barriers inside the
cycles_read()(or whatever we end up calling it)? Or does that not make sense on ARM?
Quoted 2 lines
+1. Additionally, the function should have a name that implies ordering, like
read_system_counter_ordered()or similar.
cycles_read() is currently a wrapper for timer_get_cntct(), which has an isb() at the beginning but not the end. I think it would make more sense to add the barrier there if there is no objection.
Quoted 5 lines
Quoted 2 lines
tools/testing/selftests/kvm/include/aarch64/processor.h uint32_t guest_get_vcpuid(void); · line 228
Quoted 2 lines
tools/testing/selftests/kvm/include/aarch64/processor.h +2
-0+ x; \ +dsb (nsh); \I assume you're doing this because you want to wait for outstanding loads and stores to complete due to 'x', right?
Correct.
Quoted 3 lines
My knee-jerk reaction was that you could just do an
mb()and share the implementation between arches, but it would seem thetools/flavor of the barrier demotes to a DMB because... reasons.
Yep, and what I read from the ARM manual says it has to be DSB.
Quoted 16 lines
Quoted 16 lines
Quoted 4 lines
tools/testing/selftests/kvm/include/aarch64/processor.h +3
-0+cycles_read () - start; \ + }) +#endif /* SELFTEST_KVM_PROCESSOR_H */ ...
Review
Quoted 1 line
(quoted diff) +0
-0
b/tools/testing/selftests/kvm/include/x86_64/processor.htools/testing/selftests/kvm/include/x86_64/processor.h void virt_map_level(struct kvm_vm *vm, uint64_t
Quoted 4 lines
index
5d977f95d5f5..7352e02db4ee100644tools/testing/selftests/kvm/include/x86_64/processor.h +0
-0@@ -1137,4 +1137,14 @@ void virt_map_level(struct kvm_vm *vm, uint64_tvaddr,
uint64_tpaddr,Quoted 3 lines
tools/testing/selftests/kvm/include/x86_64/processor.h +0
-0uint64_t cycles_read (void );uint64_t cycles_to_ns (struct kvm_vcpu *vcpu,uint64_t cycles);
Quoted 11 lines
Quoted 11 lines
tools/testing/selftests/kvm/include/x86_64/processor.h void virt_map_level(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, · line 1144
Quoted 5 lines
tools/testing/selftests/kvm/include/x86_64/processor.h +5
-0+#define MEASURE_CYCLES (x) \ + ({ \ + uint64_t start; \ + start = cycles_read(); \ + asm volatile("mfence"); \This is incorrect as placing the barrier after the RDTSC allows the RDTSC to be executed before earlier loads, e.g. could measure memory accesses from whatever was before
MEASURE_CYCLES(). And per the kernel'srdtsc_ordered(), it sounds like RDTSC can only be hoisted before prior loads, i.e. will be ordered with respect to future loads and stores.
Interesting, so I will swap the fence and the cycles_read()
Quoted 3 lines
Same thing goes for the arm64 variant of the function... You want to insert an
isb()immediately _before_ you read the counter register to avoid speculation.
That's taken care of. See my earlier comment about timer_get_cntct()