时间的一种保持方式是通过时钟中断计数,进而换算得到,这种方式在虚拟机里有问题,因为时钟中断不能准时到达guest os。
另外一种方式,如模拟HPET,guest os当需要的时候会去读当前的时间,这种方式会使得虚拟机频繁的退出,严重影响性能。
kvmclock是一个半虚拟化的时钟,guest感知。guest向hypervisor询问时间,同时保证时钟的稳定和准确。默认情况下guest os会使用kvmclock,如fedora17
# cat /sys/devices/system/clocksource/clocksource0/current_clocksource
kvm-clock
kvmclock原理
guest注册内存页,里面包含kvmclock数据,该页在guest整个生命周期内存在,hypervisor会持续写这个页。
有个结构体pvclock_vcpu_time_info
其中,
pvclock_vcpu_time_info.tsc_to_system_mul 存放当前一nanoseconds多少个counter值,cpu频率调整后该值也会变
pvclock_vcpu_time_info.system_time 最近一次从host读到的时间,通过当前注册的时钟源读取
pvclock_vcpu_time_info.tsc_timestamp: the guest-visible tsc (result of rdtsc + tsc_offset) of this cpu at the moment we recorded system_time. Note that some time is inevitably spent between system_time and tsc_timestamp measurements. Guests can subtract this quantity from the current value of tsc to obtain a delta to be added to system_time
Glauber Costa曾爆出kvmclock在smp上存在同步问题,并做了fix
问题
http://www.novell.com/support/kb/doc.php?id=7008698
http://article.gmane.org/gmane.comp.emulators.kvm.devel/51604/
http://s19n.net/articles/2011/kvm_clock.html
就此来看,pvclock只是半虚拟化了tsc,其他的时钟还是存在性能问题
参考:
https://lkml.org/lkml/2010/4/15/355
http://rwmj.wordpress.com/2010/10/15/kvm-pvclock/
http://www.linux-kvm.org/page/KVMClock
https://access.redhat.com/site/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Virtualization_Administration_Guide/sect-Virtualization-Tips_and_tricks-Libvirt_Managed_Timers.html