Skip to contentSkip to conversation

Monotonic clock with KVM pv-clock

DISCUSSION Nadav Har'El 3 participants 8 messages Jan 20, 2014 – Jan 22, 2014 KVM

lore
Share & tools
lore
#0 lore raw

Hi,

I'm trying to figure out how a guest OS can get a monotonic clock using KVM's paravirtual clock.

At first, I thought that the clock I get using KVM_SYSTEM_TIME is a monotonic clock, based on the host's monotonic clock. But I'm no longer sure this is actually done correctly:

1. What happens on live migration? On the new host, will the KVM_SYSTEM_TIME continue from the same point? Or will it jump because KVM_WALL_CLOCK (the wall time during boot) is different on the new host?

2. What happens when the wall-clock time is set on the host? I was hoping that only KVM_WALL_CLOCK changes and KVM_SYSTEM_TIME doesn't, but am no longer sure this is actually the case.

If KVM_SYSTEM_TIME is not a correct way to get a monotonic paravirtual clock from KVM, is there a correct way?

Thanks,
Nadav.

Signature · 4 lines
Nadav Har'El                        |      Monday, Jan 20 2014, 19 Shevat 5774
[email protected]             |-----------------------------------------
Phone +972-523-790466, ICQ 13349191 |Diplomat: A man who always remembers a
http://nadav.harel.org.il           |woman's birthday but never her age.
#1 lore raw
Quoted 7 lines · Nadav Har'El

Hi,

I'm trying to figure out how a guest OS can get a monotonic clock using KVM's paravirtual clock.

At first, I thought that the clock I get using KVM_SYSTEM_TIME is a monotonic clock, based on the host's monotonic clock.

It is. However, it is used in conjunction with TSC delta, part of the structure which is written at KVM_SYSTEM_TIME GPA. See pvclock_clocksource_read at arch/x86/kernel/pvclock.c.

Quoted 4 lines

But I'm no longer sure this is actually done correctly:

1. What happens on live migration? On the new host, will the KVM_SYSTEM_TIME continue from the same point?

Yes, from the same point where the VM was stopped on the source.

Quoted 2 lines

Or will it jump because
KVM_WALL_CLOCK (the wall time during boot) is different on the new host?

No, it won't, see the kernel commit which introduces KVM_SET_CLOCK/KVM_GET_CLOCK.

Quoted 3 lines

2. What happens when the wall-clock time is set on the host? I was hoping that only KVM_WALL_CLOCK changes and KVM_SYSTEM_TIME doesn't, but am no longer sure this is actually the case.

Yes, it is the case. The host clock which backs system_timestamp field of pvclock structure is

CLOCK_MONOTONIC
Clock that cannot be set and represents monotonic time since some unspecified starting point. This clock is not affected by discontinuous jumps in the system time (e.g., if the system administrator manually changes the clock), but is affected by the incremental adjustments per‐formed by adjtime(3) and NTP.

Quoted 2 lines

If KVM_SYSTEM_TIME is not a correct way to get a monotonic paravirtual clock from KVM, is there a correct way?

Inside a Linux guest? Can use sched_clock().

If not a Linux guest, either implement kvmclock-like driver (kvm-unit-test contains one).

Quoted 9 lines

Thanks,
Nadav.

Signature · 4 lines
Nadav Har'El                        |      Monday, Jan 20 2014, 19 Shevat 5774
[email protected]             |-----------------------------------------
Phone +972-523-790466, ICQ 13349191 |Diplomat: A man who always remembers a
http://nadav.harel.org.il           |woman's birthday but never her age.
#2 lore raw
Quoted 4 lines · Marcelo Tosatti
Quoted 2 lines · Nadav Har'El

If KVM_SYSTEM_TIME is not a correct way to get a monotonic paravirtual clock from KVM, is there a correct way?

Inside a Linux guest? Can use sched_clock().

I would like to mention that Linux guests usually do not use sched_clock() directly. The reason being that the kvm_clock based sched_clock() is not marked stable (sched_clock_stable is 0), which means that the pair of wrappers sched_clock_local()/sched_clock_remote() is used instead.

#3 lore raw
Quoted 10 lines · Fernando Luis Vazquez Cao
Quoted 4 lines · Marcelo Tosatti
Quoted 2 lines · Nadav Har'El

If KVM_SYSTEM_TIME is not a correct way to get a monotonic paravirtual clock from KVM, is there a correct way?

Inside a Linux guest? Can use sched_clock().

I would like to mention that Linux guests usually do not use sched_clock() directly. The reason being that the kvm_clock based sched_clock() is not marked stable (sched_clock_stable is 0), which means that the pair of wrappers sched_clock_local()/sched_clock_remote() is used instead.

Should verify the requirements of sched_clock_cpu() and enable sched_clock_stable in case it fulfills requirements (kvmclock_read can be nondecreasing due to TSC->nanosecond scaling, and not increase for a longer duration with global accumulator, due to cmpxchg).

#6 lore raw
In reply to Marcelo Tosatti

On Mon, Jan 20, 2014, Marcelo Tosatti wrote about "Re: Monotonic clock with KVM pv-clock":

Quoted 6 lines · Nadav Har'El
Quoted 5 lines

Hi,

I'm trying to figure out how a guest OS can get a monotonic clock using KVM's paravirtual clock.

Quoted 6 lines
Quoted 2 lines

At first, I thought that the clock I get using KVM_SYSTEM_TIME is a monotonic clock, based on the host's monotonic clock.

It is. However, it is used in conjunction with TSC delta, part of the structure which is written at KVM_SYSTEM_TIME GPA. See pvclock_clocksource_read at arch/x86/kernel/pvclock.c.

Sure, I'm using the whole protocol for KVM_SYSTEM_TIME. I just wondered if it has a meaningful definition (namely, of being a monotonic clock) when being used not in conjunction with the KVM_WALL_CLOCK protocol.

Quoted 7 lines
Quoted 3 lines

2. What happens when the wall-clock time is set on the host? I was hoping that only KVM_WALL_CLOCK changes and KVM_SYSTEM_TIME doesn't, but am no longer sure this is actually the case.

Yes, it is the case. The host clock which backs system_timestamp field of pvclock structure is

Quoted 6 lines

CLOCK_MONOTONIC
Clock that cannot be set and represents monotonic time since some unspecified starting point. This clock is not affected by discontinuous jumps in the system time (e.g., if the system administrator manually changes the clock), but is affected by the incremental adjustments per???formed by adjtime(3) and NTP.

Excellent.

Quoted 8 lines
Quoted 3 lines

If KVM_SYSTEM_TIME is not a correct way to get a monotonic paravirtual clock from KVM, is there a correct way?

Inside a Linux guest? Can use sched_clock().

If not a Linux guest, either implement kvmclock-like driver (kvm-unit-test contains one).

This is actually an OSv guest (http://osv.io/).

Until now we were using kvmclock by adding up the wallclock and systemtime protocol, and this resulted with a good wall-time clock (CLOCK_REALTIME), but I really wanted to also have a monotonic clock (CLOCK_MONOTONIC) for the guest.

I took a look and how a Linux guest uses the KVM pv clock, and thought that the "system time" part of the kvm clock protocol will be a good monotonic clock, but wasn't sure I was understanding this right.

Thanks for the clarifications!

Nadav.

Signature · 4 lines
Nadav Har'El                        |     Tuesday, Jan 21 2014, 20 Shevat 5774
[email protected]             |-----------------------------------------
Phone +972-523-790466, ICQ 13349191 |"The average person thinks he isn't." -
http://nadav.harel.org.il           |Father Larry Lorenzoni
#4 lore raw
In reply to Marcelo Tosatti
Quoted 5 lines · Marcelo Tosatti
Quoted 4 lines · Fernando Luis Vazquez Cao
Quoted 3 lines · Marcelo Tosatti
Quoted 2 lines · Nadav Har'El

If KVM_SYSTEM_TIME is not a correct way to get a monotonic paravirtual clock from KVM, is there a correct way?

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Inside a Linux guest? Can use sched_clock().

I would like to mention that Linux guests usually do not use sched_clock() directly. The reason being that the kvm_clock based sched_clock() is not marked stable (sched_clock_stable is 0), which means that the pair of wrappers sched_clock_local()/sched_clock_remote() is used instead.

Should verify the requirements of sched_clock_cpu() and enable sched_clock_stable in case it fulfills requirements

The requirements of cpu_clock() are (from code comments in kernel/sched/clock.c):

1. cpu_clock(i) provides a fast (execution time) high resolution
2. Clock with bounded drift between CPUs.
3. The value of cpu_clock(i) is monotonic for constant i (when comparing cpu_clock(i) to cpu_clock(j) for i != j, time can go backwards)
4. The timestamp returned is in nanoseconds.

Besides, for sched_clock() to be considered stable

5. it has to be synchronized across CPUs.

Quoted 1 line

(kvmclock_read can be nondecreasing due to TSC->nanosecond scaling,

s/nondecreasing/nonincreasing/? I guess you mean that consecutive calls to kvm_clock_read() can return the same nanosecond value when the TSC frequency is greater than 1GHz or there are frequency changes.

Quoted 2 lines

and not increase for a longer duration with global accumulator, due to cmpxchg)

Yes. If I understand correctly, that can happen if PVCLOCK_TSC_STABLE_BIT is not set.

I think that when PVCLOCK_TSC_STABLE_BIT is set we should be fine as long as backward motion is filtered out.

#7 lore raw
In reply to Nadav Har'El
Quoted 10 lines · Nadav Har'El

On Mon, Jan 20, 2014, Marcelo Tosatti wrote about "Re: Monotonic clock with KVM pv-clock":

Quoted 9 lines · Nadav Har'El
Quoted 7 lines

Hi,

I'm trying to figure out how a guest OS can get a monotonic clock using KVM's paravirtual clock.

At first, I thought that the clock I get using KVM_SYSTEM_TIME is a monotonic clock, based on the host's monotonic clock.

Quoted 7 lines
Quoted 3 lines

It is. However, it is used in conjunction with TSC delta, part of the structure which is written at KVM_SYSTEM_TIME GPA. See pvclock_clocksource_read at arch/x86/kernel/pvclock.c.

Sure, I'm using the whole protocol for KVM_SYSTEM_TIME. I just wondered if it has a meaningful definition (namely, of being a monotonic clock) when being used not in conjunction with the KVM_WALL_CLOCK protocol.

If flags field contains PVCLOCK_TSC_STABLE_BIT, yes, otherwise, no.

Should use a global accumulator with cmpxchg if PVCLOCK_TSC_STABLE_BIT is clear.

Quoted 45 lines
Quoted 7 lines
Quoted 3 lines

2. What happens when the wall-clock time is set on the host? I was hoping that only KVM_WALL_CLOCK changes and KVM_SYSTEM_TIME doesn't, but am no longer sure this is actually the case.

Yes, it is the case. The host clock which backs system_timestamp field of pvclock structure is

Quoted 6 lines

CLOCK_MONOTONIC
Clock that cannot be set and represents monotonic time since some unspecified starting point. This clock is not affected by discontinuous jumps in the system time (e.g., if the system administrator manually changes the clock), but is affected by the incremental adjustments per???formed by adjtime(3) and NTP.

Excellent.

Quoted 8 lines
Quoted 3 lines

If KVM_SYSTEM_TIME is not a correct way to get a monotonic paravirtual clock from KVM, is there a correct way?

Inside a Linux guest? Can use sched_clock().

If not a Linux guest, either implement kvmclock-like driver (kvm-unit-test contains one).

This is actually an OSv guest (http://osv.io/).

Until now we were using kvmclock by adding up the wallclock and systemtime protocol, and this resulted with a good wall-time clock (CLOCK_REALTIME), but I really wanted to also have a monotonic clock (CLOCK_MONOTONIC) for the guest.

I took a look and how a Linux guest uses the KVM pv clock, and thought that the "system time" part of the kvm clock protocol will be a good monotonic clock, but wasn't sure I was understanding this right.

Thanks for the clarifications!

Nadav.

Signature · 4 lines
Nadav Har'El                        |     Tuesday, Jan 21 2014, 20 Shevat 5774
[email protected]             |-----------------------------------------
Phone +972-523-790466, ICQ 13349191 |"The average person thinks he isn't." -
http://nadav.harel.org.il           |Father Larry Lorenzoni
#5 lore raw
In reply to Fernando Luis Vazquez Cao
Quoted 11 lines · Fernando Luis Vazquez Cao
Quoted 10 lines · Marcelo Tosatti
Quoted 9 lines · Fernando Luis Vazquez Cao
Quoted 4 lines · Marcelo Tosatti
Quoted 2 lines · Nadav Har'El

If KVM_SYSTEM_TIME is not a correct way to get a monotonic paravirtual clock from KVM, is there a correct way?

Inside a Linux guest? Can use sched_clock().

I would like to mention that Linux guests usually do not use sched_clock() directly. The reason being that the kvm_clock based sched_clock() is not marked stable (sched_clock_stable is 0), which means that the pair of wrappers sched_clock_local()/sched_clock_remote() is used instead.

Quoted 8 lines
Quoted 2 lines

Should verify the requirements of sched_clock_cpu() and enable sched_clock_stable in case it fulfills requirements

The requirements of cpu_clock() are (from code comments in kernel/sched/clock.c):

1. cpu_clock(i) provides a fast (execution time) high resolution
2. Clock with bounded drift between CPUs.

How high of a resolution ? Or what is the effect of lowering resolution?

Quoted 3 lines

3. The value of cpu_clock(i) is monotonic for constant i (when comparing cpu_clock(i)
to cpu_clock(j) for i != j, time can go backwards)

OK.

Quoted 1 line

4. The timestamp returned is in nanoseconds.

OK.

Quoted 3 lines

Besides, for sched_clock() to be considered stable

5. it has to be synchronized across CPUs.

This contradicts item 3 ?

Quoted 3 lines
Quoted 1 line

(kvmclock_read can be nondecreasing due to TSC->nanosecond scaling,

s/nondecreasing/nonincreasing/?

Yes.

Quoted 3 lines

I guess you mean that consecutive calls to kvm_clock_read() can return the same nanosecond value when the TSC frequency is greater than 1GHz or there are frequency changes.

Yes, when CPU can execute more than one kvm_clock_read's in one nanosecond.

Quoted 5 lines
Quoted 2 lines

and not increase for a longer duration with global accumulator, due to cmpxchg)

Yes. If I understand correctly, that can happen if PVCLOCK_TSC_STABLE_BIT is not set.

Yes.

Quoted 2 lines

I think that when PVCLOCK_TSC_STABLE_BIT is set we should be fine as long as backward motion is filtered out.

There can be no backward motion with PVCLOCK_TSC_STABLE_BIT (in theory, and no reports have been seen yet).

Do you have any numbers for the change?