Tests for kvmclock
Subsystems (3)
v2 cover Tests for kvmclock
The following series implements three tests for kvmclock:
Wall clock test: check whether the kvmclock could return a correct TOD by pass the correct one to test and check it with the value returned by kvmclock.
Monotonic cycle test: check whether the cycles of kvmclock grows monotonically in smp guests.
Performance test: Measure the performance ( through guest TSC cycles ) of kvmclock driver for both with and without the guest side adjustment.
Change since V1:
- Change the max cpu to 64 in order to test in larger systems.
- Make the per-cpu structure hv_clock 4 byte aligned.
- Fix the bug of the clobber list in atomic64_cmpxchg().
- Add the test of pure performance test based on guest TSC cycle.
- Record the count of stall when doing monotonic cycle test.
- Introduce some new type definitions.
- Fix the bug of the wallclock calculation and test.
Here is a test result I got form a guest with 64 vcpus on a 64 cores intel X7550:
The test command is qemu-system-x86_64 -device testdev,chardev=testlog -chardev file,id=testlog,path=msr.out -kernel ./x86/kvmclock_test.flat -smp 64 --append 100000
For 64bit guests:
The guest cycles spent on geting raw cycles is about 21543417. The guest cycles spent on geting adjusted cycles is 1067264988.
Which means it takes about 50x more time on correcting the cycles than just returning cycles supplied by hypervisor.
For 32bit guests:
The guest cycles spent on geting raw cycles is about 26916562. The guest cycles spent on geting adjusted cycles is 2020119174.
Which means it taks about 75x more time on correcting the cycles than just returning cycles supplied by hypervisor.
And for 4 core guests: we only take about 10x-15x more time on correcting.
Pay attention:
1 The performance test just read cycle and do not check anything else. 2 I've run the test for serverl times, the result are similar. 3 The cycles were measured from guest, so maybe not accurate.
Here is a sample test results of 64bit guest with 64 vcpus:
......
Check the stability of raw cycle ...
Worst warp -10
Worst warp -6537
Worst warp -11170
Worst warp -11370
Worst warp -11372
Worst warp -31754
Worst warp -31876
Worst warp -31904
Worst warp -36771
Worst warp -37382
Worst warp -37688
Worst warp -37707
Total vcpus: 64 Test loops: 100000 Total warps: 53811 Total stalls: 408 Worst warp: -37707 Raw cycle is not stable Monotonic cycle test: Total vcpus: 64 Test loops: 100000 Total warps: 0 Total stalls: 214 Worst warp: 0 Measure the performance of raw cycle ... Total vcpus: 64 Test loops: 100000 TSC cycles: 21543417 Measure the performance of adjusted cycle ... Total vcpus: 64 Test loops: 100000 TSC cycles: 1067264988
---
Jason Wang (8):
Introduce some type definiations.
Increase max_cpu to 64
Introduce memory barriers.
Introduce atomic operations
Export tsc related helpers
Introduce atol()
Add a simple kvmclock driver
Add tests for kvm-clock
config-x86-common.mak | 7 + lib/libcflat.h | 5 + lib/string.c | 31 +++++ lib/x86/atomic.c | 37 ++++++ lib/x86/atomic.h | 164 +++++++++++++++++++++++++++++ lib/x86/processor.h | 22 ++++ lib/x86/smp.h | 4 + x86/README | 1 x86/cstart.S | 2 x86/cstart64.S | 2 x86/kvmclock.c | 279 +++++++++++++++++++++++++++++++++++++++++++++++++ x86/kvmclock.h | 60 +++++++++++ x86/kvmclock_test.c | 166 +++++++++++++++++++++++++++++ x86/tsc.c | 16 --- x86/unittests.cfg | 5 + x86/vmexit.c | 15 --- 16 files changed, 783 insertions(+), 33 deletions(-) create mode 100644 lib/x86/atomic.c create mode 100644 lib/x86/atomic.h create mode 100644 x86/kvmclock.c create mode 100644 x86/kvmclock.h create mode 100644 x86/kvmclock_test.c
Signature · 1 line
Jason Wang
PATCH v2 1/8 Introduce some type definiations.
--- lib/libcflat.h | 4 ++++ 1 files changed, 4 insertions(+), 0 deletions(-)
lib/libcflat.h +4 -0
@@ -23,10 +23,14 @@#include <stdarg.h> typedef unsigned char u8 ; +typedef signed char s8 ;typedef unsigned short u16 ; +typedef signed short s16 ;typedef unsigned u32 ; +typedef signed s32 ;typedef unsigned long ulong ;typedef unsigned long long u64 ; +typedef signed long long s64 ;typedef unsigned long size_t ;typedef _Bool bool ;
PATCH v2 2/8 Increase max_cpu to 64
In reply to Jason Wang--- x86/cstart.S | 2 +- x86/cstart64.S | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
x86/cstart.S +1 -1
@@ -6,7 +6,7 @@ boot_idt = 0 ipi_vector = 0x20 -max_cpus = 4 +max_cpus = 64 .bss
x86/cstart64.S +1 -1
@@ -6,7 +6,7 @@ boot_idt = 0 ipi_vector = 0x20 -max_cpus = 4 +max_cpus = 64 .bss
PATCH v2 3/8 Introduce memory barriers.
In reply to Jason Wang--- lib/x86/smp.h | 4 ++++ 1 files changed, 4 insertions(+), 0 deletions(-)
lib/x86/smp.h +4 -0
@@ -1,6 +1,10 @@#ifndef __SMP_H#define __SMP_H +#define mb () asm volatile("mfence":::"memory") +#define rmb () asm volatile("lfence":::"memory") +#define wmb () asm volatile("sfence" ::: "memory") +struct spinlock {int v; };
PATCH v2 4/8 Introduce atomic operations
In reply to Jason Wang--- config-x86-common.mak | 1 lib/x86/atomic.c | 37 +++++++++++ lib/x86/atomic.h | 164 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 202 insertions(+), 0 deletions(-) create mode 100644 lib/x86/atomic.c create mode 100644 lib/x86/atomic.h
config-x86-common.mak +1 -0
@@ -10,6 +10,7 @@ cflatobjs += \ cflatobjs += lib/x86/fwcfg.o cflatobjs += lib/x86/apic.o +cflatobjs += lib/x86/atomic.o $(libcflat): LDFLAGS += -nostdlib $(libcflat): CFLAGS += -ffreestanding -I lib
lib/x86/atomic.c new file +37 -0
@@ -0,0 +1,37 @@ +#include <libcflat.h> +#include "atomic.h" + +#ifdef __i386__ + +u64 atomic64_cmpxchg (atomic64_t *v,u64 old,u64 new) +{ +u32 low = new; +u32 high = new >>32 ; + + asmvolatile ("lock cmpxchg8b %1\n" + :"+A" (old), +"+m" (*(volatile long long *)&v->counter) + :"b" (low),"c" (high) + :"memory" + ); + +return old; +} + +#else + +u64 atomic64_cmpxchg (atomic64_t *v,u64 old,u64 new) +{ +u64 ret; +u64 _old = old; +u64 _new = new; + + asmvolatile ("lock cmpxchgq %2,%1" + :"=a" (ret),"+m" (*(volatile long *)&v->counter) + :"r" (_new),"0" (_old) + :"memory" + ); +return ret; +} + +#endif
lib/x86/atomic.h new file +164 -0
164 lines not on this page · raw message
PATCH v2 5/8 Export tsc related helpers
In reply to Jason Wang--- lib/x86/processor.h | 22 ++++++++++++++++++++++ x86/tsc.c | 16 +--------------- x86/vmexit.c | 15 --------------- 3 files changed, 23 insertions(+), 30 deletions(-)
lib/x86/processor.h +22 -0
@@ -258,4 +258,26 @@ static inline void sti(void) asm volatile ("sti"); } +static inline unsigned long long rdtsc () +{ +long long r; + +#ifdef __x86_64__ +unsigned a, d; + + asmvolatile ("rdtsc" :"=a" (a),"=d" (d)); + r = a | ((long long )d <<32 ); +#else + asmvolatile ("rdtsc" :"=A" (r)); +#endif +return r; +} + +static inline void wrtsc (u64 tsc) +{ +unsigned a = tsc, d = tsc >>32 ; + + asmvolatile ("wrmsr" : :"a" (a),"d" (d),"c" (0x10 )); +} +#endif
x86/tsc.c +1 -15
@@ -1,19 +1,5 @@#include "libcflat.h" - -u64 rdtsc (void ) -{ -unsigned a, d; - - asmvolatile ("rdtsc" :"=a" (a),"=d" (d)); -return a | (u64 )d <<32 ; -} - -void wrtsc (u64 tsc) -{ -unsigned a = tsc, d = tsc >>32 ; - - asmvolatile ("wrmsr" : :"a" (a),"d" (d),"c" (0x10 )); -} +#include "processor.h" void test_wrtsc (u64 t1) {
x86/vmexit.c +0 -15
@@ -3,21 +3,6 @@#include "smp.h" #include "processor.h" -static inline unsigned long long rdtsc () -{ -long long r; - -#ifdef __x86_64__ -unsigned a, d; - - asmvolatile ("rdtsc" :"=a" (a),"=d" (d)); - r = a | ((long long )d <<32 ); -#else - asmvolatile ("rdtsc" :"=A" (r)); -#endif -return r; -} -static unsigned int inl (unsigned short port) {unsigned int val;
PATCH v2 6/8 Introduce atol()
In reply to Jason Wang--- lib/libcflat.h | 1 + lib/string.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 0 deletions(-)
lib/libcflat.h +1 -0
@@ -50,6 +50,7 @@ extern void puts(const char *s);extern void *memset (void *s,int c,size_t n); +extern long atol (const char *ptr);#define ARRAY_SIZE (_a) (sizeof(_a)/sizeof((_a)[0]))#endif
lib/string.c +31 -0
@@ -30,3 +30,34 @@ void *memset(void *s, int c, size_t n)return s; } + +long atol (const char *ptr) +{ +long acc =0 ; +const char *s = ptr; +int neg, c; + +while (*s ==' ' || *s =='\t' ) + s++; +if (*s =='-' ){ + neg =1 ; + s++; + }else { + neg =0 ; +if (*s =='+' ) + s++; + } + +while (*s) { +if (*s <'0' || *s >'9' ) +break ; + c = *s -'0' ; + acc = acc *10 + c; + s++; + } + +if (neg) + acc = -acc; + +return acc; +}
PATCH v2 7/8 Add a simple kvmclock driver
In reply to Jason WangMost of the codes were borrowed from arxh/x86/kernel/kvmclock.c. A special bit: PV_CLOCK_CYCLE_RAW_TEST_BIT is used to notify the driver to return unadjusted cycles.
--- x86/kvmclock.c | 279 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ x86/kvmclock.h | 60 ++++++++++++ 2 files changed, 339 insertions(+), 0 deletions(-) create mode 100644 x86/kvmclock.c create mode 100644 x86/kvmclock.h
x86/kvmclock.c new file +279 -0
@@ -0,0 +1,279 @@ +#include "libcflat.h" +#include "smp.h" +#include "atomic.h" +#include "processor.h" +#include "kvmclock.h" + +#define unlikely (x) __builtin_expect(!!(x), 0) +#define likely (x) __builtin_expect(!!(x), 1) + + +struct pvclock_vcpu_time_info __attribute__((aligned (4 ))) hv_clock[MAX_CPU ]; +struct pvclock_wall_clock wall_clock; +static unsigned char valid_flags =0 ; +static atomic64_t last_value =ATOMIC64_INIT (0 ); + +/* +* Scale a 64-bit delta by scaling and multiplying by a 32-bit fraction, +* yielding a 64-bit result. +*/ +static inline u64 scale_delta (u64 delta,u32 mul_frac,int shift) +{ +u64 product; +#ifdef __i386__ +u32 tmp1, tmp2; +#endif + +if (shift <0 ) + delta >>= -shift; +else + delta <<= shift; + +#ifdef __i386__ + __asm__ ( +"mul %5 ; " +"mov %4,%%eax ; " +"mov %%edx,%4 ; " +"mul %5 ; " +"xor %5,%5 ; " +"add %4,%%eax ; " +"adc %5,%%edx ; " + :"=A" (product),"=r" (tmp1),"=r" (tmp2) + :"a" ((u32 )delta),"1" ((u32)(delta >>32 )),"2" (mul_frac) ); +#elif defined(__x86_64__) + __asm__ ( +"mul %%rdx ; shrd $32,%%rdx,%%rax" + :"=a" (product) :"0" (delta),"d" ((u64 )mul_frac) ); +#else +#error implement me! +#endif + +return product; +} + +#ifdef __i386__ +# define do_div (n,base) ({ \ + u32 __base = (base); \ + u32 __rem; \ + __rem = ((u64)(n)) % __base; \ + (n) = ((u64)(n)) / __base; \ + __rem; \ + }) +#else +u32 __attribute__((weak))__div64_32 (u64 *n,u32 base) +{ +u64 rem = *n; +u64 b = base; +u64 res, d =1 ; +u32 high = rem >>32 ; + +/* Reduce the thing a bit first */ + res =0 ; +if (high >= base) { + high /= base; + res = (u64 ) high <<32 ; + rem -= (u64) (high*base) <<32 ; + } + +while ((s64 )b >0 && b < rem) { + b = b+b; + d = d+d; + } + +do { +if (rem >= b) { + rem -= b; + res += d; + } + b >>=1 ; + d >>=1 ; + }while (d); + + *n = res; +return rem; +} + +# define do_div (n,base) ({ \ + u32 __base = (base); \ + u32 __rem; \ + (void)(((typeof((n)) *)0) == ((u64 *)0)); \ + if (likely(((n) >> 32) == 0)) { \ + __rem = (u32)(n) % __base; \ + (n) = (u32)(n) / __base; \ + } else \ + __rem = __div64_32(&(n), __base); \ + __rem; \ + }) +#endif + +/** +* set_normalized_timespec - set timespec sec and nsec parts and normalize +* +* @ts: pointer to timespec variable to be set +* @sec: seconds to set +* @nsec: nanoseconds to set +* +* Set seconds and nanoseconds field of a timespec variable and +* normalize to the timespec storage format +* +* Note: The tv_nsec part is always in the range of +* 0 <= tv_nsec < NSEC_PER_SEC +* For negative values only the tv_sec field is negative ! +*/ +void set_normalized_timespec (struct timespec *ts,long sec,s64 nsec) +{ +while (nsec >=NSEC_PER_SEC ) { +/* +* The following asm() prevents the compiler from +* optimising this loop into a modulo operation. See +* also __iter_div_u64_rem() in include/linux/time.h +*/ + asm("" :"+rm" (nsec)); + nsec -=NSEC_PER_SEC ; + ++sec; + } +while (nsec <0 ) { + asm("" :"+rm" (nsec)); + nsec +=NSEC_PER_SEC ; + --sec; + } + ts->tv_sec = sec; + ts->tv_nsec = nsec; +} + +static u64 pvclock_get_nsec_offset (struct pvclock_shadow_time *shadow) +{ +u64 delta =rdtsc () - shadow->tsc_timestamp; +return scale_delta (delta, shadow->tsc_to_nsec_mul, shadow->tsc_shift); +} + +/* +* Reads a consistent set of time-base values from hypervisor, +* into a shadow data area. +*/ +static unsigned pvclock_get_time_values (struct pvclock_shadow_time *dst, +struct pvclock_vcpu_time_info *src) +{ +do { + dst->version = src->version; +rmb ();/* fetch version before data */ + dst->tsc_timestamp = src->tsc_timestamp; + dst->system_timestamp = src->system_time; + dst->tsc_to_nsec_mul = src->tsc_to_system_mul; + dst->tsc_shift = src->tsc_shift; + dst->flags = src->flags; +rmb ();/* test version after fetching data */ + }while ((src->version &1 ) || (dst->version != src->version)); + +return dst->version; +} + +cycle_t pvclock_clocksource_read (struct pvclock_vcpu_time_info *src) +{ +struct pvclock_shadow_time shadow; +unsigned version; +cycle_t ret, offset; +u64 last; + +do { + version =pvclock_get_time_values (&shadow, src); +barrier (); + offset =pvclock_get_nsec_offset (&shadow); + ret = shadow.system_timestamp + offset; +barrier (); + }while (version != src->version); + +if ((valid_flags &PVCLOCK_RAW_CYCLE_BIT ) || + ((valid_flags &PVCLOCK_TSC_STABLE_BIT ) && + (shadow.flags &PVCLOCK_TSC_STABLE_BIT ))) +return ret; + +/* +* Assumption here is that last_value, a global accumulator, always goes +* forward. If we are less than that, we should not be much smaller. +* We assume there is an error marging we're inside, and then the correction +* does not sacrifice accuracy. +* +* For reads: global may have changed between test and return, +* but this means someone else updated poked the clock at a later time. +* We just need to make sure we are not seeing a backwards event. +* +* For updates: last_value = ret is not enough, since two vcpus could be +* updating at the same time, and one of them could be slightly behind, +* making the assumption that last_value always go forward fail to hold. +*/ + last =atomic64_read (&last_value); +do { +if (ret < last) +return last; + last =atomic64_cmpxchg (&last_value, last, ret); + }while (unlikely (last != ret)); + +return ret; +} + +cycle_t kvm_clock_read () +{ +struct pvclock_vcpu_time_info *src; +cycle_t ret; +int index =smp_id (); + + src = &hv_clock[index]; + ret =pvclock_clocksource_read (src); +return ret; +} + +void kvm_clock_init (void *data) +{ +int index =smp_id (); +struct pvclock_vcpu_time_info *hvc = &hv_clock[index]; + +printf ("kvm-clock: cpu %d, msr 0x:%lx \n" , index, hvc); +wrmsr (MSR_KVM_SYSTEM_TIME , (unsigned long )hvc |1 ); +} + +void kvm_clock_clear (void *data) +{ +wrmsr (MSR_KVM_SYSTEM_TIME ,0LL ); +} + +void pvclock_read_wallclock (struct pvclock_wall_clock *wall_clock, +struct pvclock_vcpu_time_info *vcpu_time, +struct timespec *ts) +{ +u32 version; +u64 delta; +struct timespec now; + +/* get wallclock at system boot */ +do { + version = wall_clock->version; +rmb ();/* fetch version before time */ + now.tv_sec = wall_clock->sec; + now.tv_nsec = wall_clock->nsec; +rmb ();/* fetch time before checking version */ + }while ((wall_clock->version &1 ) || (version != wall_clock->version)); + + delta =pvclock_clocksource_read (vcpu_time);/* time since system boot */ + delta += now.tv_sec * (u64 )NSEC_PER_SEC + now.tv_nsec; + + now.tv_nsec =do_div (delta,NSEC_PER_SEC ); + now.tv_sec = delta; + +set_normalized_timespec (ts, now.tv_sec, now.tv_nsec); +} + +void kvm_get_wallclock (struct timespec *ts) +{ +struct pvclock_vcpu_time_info *vcpu_time; +int index =smp_id (); + +wrmsr (MSR_KVM_WALL_CLOCK , (unsigned long )&wall_clock); + vcpu_time = &hv_clock[index]; +pvclock_read_wallclock (&wall_clock, vcpu_time, ts); +} + +void pvclock_set_flags (unsigned char flags) +{ + valid_flags = flags; +}
x86/kvmclock.h new file +60 -0
60 lines not on this page · raw message
PATCH v2 8/8 Add tests for kvm-clock
In reply to Jason WangThis patch implements three tests for kvmclock. First one check whether the date of time returned by kvmclock matches the value got from host. Second one check whether the cycle of kvmclock grows monotonically in smp guest and the count of warps and stalls are also recorded. The last test is a performance test to measure the guest cycles cost when trying to read cycle of kvmclock.
Three parameters were accepted by the test: test loops, seconds since 1970-01-01 00:00:00 UTC which could be easily get through date +%s and the max accepted offset value between the tod of guest and host.
--- config-x86-common.mak | 6 +- x86/README | 1 x86/kvmclock_test.c | 166 +++++++++++++++++++++++++++++++++++++++++++++++++ x86/unittests.cfg | 5 + 4 files changed, 177 insertions(+), 1 deletions(-) create mode 100644 x86/kvmclock_test.c
config-x86-common.mak +5 -1
@@ -26,7 +26,8 @@ FLATLIBS = lib/libcflat.a $(libgcc) tests-common = $(TEST_DIR)/vmexit.flat $(TEST_DIR)/tsc.flat \ $(TEST_DIR)/smptest.flat $(TEST_DIR)/port80.flat \ $(TEST_DIR)/realmode.flat $(TEST_DIR)/msr.flat \ - $(TEST_DIR)/hypercall.flat $(TEST_DIR)/sieve.flat + $(TEST_DIR)/hypercall.flat $(TEST_DIR)/sieve.flat \ + $(TEST_DIR)/kvmclock_test.flat tests_and_config = $(TEST_DIR)/*.flat $(TEST_DIR)/unittests.cfg
@@ -70,6 +71,9 @@ $(TEST_DIR)/rmap_chain.flat: $(cstart.o) $(TEST_DIR)/rmap_chain.o \ $(TEST_DIR)/svm.flat: $(cstart.o) $(TEST_DIR)/vm.o +$(TEST_DIR)/kvmclock_test.flat: $(cstart.o) $(TEST_DIR)/kvmclock.o \ + $(TEST_DIR)/kvmclock_test.o + arch_clean: $(RM) $(TEST_DIR)/*.o $(TEST_DIR)/*.flat \ $(TEST_DIR)/.*.d $(TEST_DIR)/lib/.*.d $(TEST_DIR)/lib/*.o
x86/README +1 -0
@@ -12,3 +12,4 @@ sieve: heavy memory access with no paging and with paging static and with paging smptest: run smp_id() on every cpu and compares return value to number tsc: write to tsc(0) and write to tsc(100000000000) and read it back vmexit: long loops for each: cpuid, vmcall, mov_from_cr8, mov_to_cr8, inl_pmtimer, ipi, ipi+halt +kvmclock_test: test of wallclock, monotonic cycle and performance of kvmclock
x86/kvmclock_test.c new file +166 -0
166 lines not on this page · raw message
x86/unittests.cfg +5 -0
@@ -63,3 +63,8 @@ extra_params = -enable-nesting -cpu qemu64,+svm file = svm.flat smp = 2 extra_params = -cpu qemu64,-svm + +[kvmclock_test] +file = kvmclock_test.flat +smp = 2 +extra_params = --append "10000000 `date +%s`"