KVM pvclock

A short article on how KVM’s paravirtualized clock source works (mostly adapted from Glauber’s notes here).

kvmclock or KVM pvclock lets guests read the host’s wall clock time. It’s really very simple: the guest sets aside a page of its RAM and asks the host to write time into that page (using an MSR). The host writes a structure containing the current time to this page — in theory the host updates this page constantly, but in reality that would be wasteful and the structure is only updated just before reentering the guest after some VM event.

The structure that is written contains the time at the moment it was written, plus the guest’s TSC at that moment, plus the current scale of TSC to real time (which can change). The guest can read its own TSC a little bit later, work out the difference between TSC now and TSC when the host measured it, scale that to seconds, and add that to the time in the struct to get an estimate of wall clock time. As long as the host doesn’t allow too much time to pass between updates, and doesn’t do something like scale the CPU speed or migrate the guest without updating the struct, the guest can get an estimate for wall clock time without involving the hypervisor at all.

struct pvclock_vcpu_time_info {
        u32   version;
        u32   pad0;
        u64   tsc_timestamp;
        u64   system_time;
        u32   tsc_to_system_mul;
        s8    tsc_shift;
        u8    flags;
        u8    pad[2];
} __attribute__((__packed__));

Finally, how do you find out if your Linux guests are using KVM pvclock? Ask the kernel like this:

$ cat /sys/devices/system/clocksource/clocksource0/current_clocksource
kvm-clock

5 Comments

Filed under Uncategorized

5 responses to “KVM pvclock

  1. al patel

    Hi Richard,

    I am having trouble keeping the time in sync. between a guest and the host.
    We use the latest version of ubunut (11.04). When guest is started, time is in sync with the host.

    Then I use the “date –set command” to change host time. I don’t see that time being reflected in the guest.

    Can you let me know what I am doing wrong? Any pointers will be very helpful.

    Thanks
    -a

  2. al patel

    Richard,

    THanks. My problem is that we prefer to not run NTPd in the guest due to some reasons. This is an embedded box and one reason is we want to save bandwidth and minimize bootstrap and runtime configuration (there being other reasons which are specific to our case).

    Hence looking at how best to keep the guest clock in sync with the host. The host platform runs NTP.

    Thanks
    Alpesh

    • Avi

      Hi Richard,
      the kvm paravirtualization looks perfect in theory. But in practice – i dont see any host updates. for example when my VM is booting – i see delta time of ~1ms between host and guest clocks – which is very big delta. then if i set the host clock – i dont see ant impact on the VM clock
      Thanks Avi

  3. system_time is read from a monotonic clock, same as the posix monotonic clock. So not wall clock time, and you can’t change it.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.