RE: [PATCH v6 03/10] KVM: selftests: Use flag CLOCK_MONOTONIC_RAW for timing

From: David Laight
Date: Mon Apr 19 2021 - 04:22:36 EST


From: wangyanan (Y)
> Sent: 19 April 2021 07:40
>
> Hi Paolo,
>
> On 2021/4/17 21:23, Paolo Bonzini wrote:
> > On 30/03/21 10:08, Yanan Wang wrote:
> >> In addition to function of CLOCK_MONOTONIC, flag CLOCK_MONOTONIC_RAW can
> >> also shield possiable impact of NTP, which can provide more robustness.
> >>
> >> Suggested-by: Vitaly Kuznetsov<vkuznets@xxxxxxxxxx>
> >> Signed-off-by: Yanan Wang<wangyanan55@xxxxxxxxxx>
> >> Reviewed-by: Ben Gardon<bgardon@xxxxxxxxxx>
> >> Reviewed-by: Andrew Jones<drjones@xxxxxxxxxx>
> >
> > I'm not sure about this one, is the effect visible?
> >
> In practice, difference between results got with CLOCK_MONOTONIC and
> CLOCK_MONOTONIC_RAW
> actually is too little to be visible. But if just in theory,
> CLOCK_MONOTONIC_RAW can ensure time results
> of the compared tests are based on the same local oscillator frequency,
> which is not subject to possible
> NTP frequency adjustment. Change in this patch seems like a bit of
> optimization.

The real annoyance is when NTP is realigning the local clock.
This typically happens after boot - but can take quite a few
minutes (don't think it can quite get to an hour).
(I think something similar is caused by leap seconds.)

During this period CLOCK_MONOTONIC can run at a significantly
different rate from 'real time'.
This may not matter for timing self tests, but is significant
for RTP audio.

The problem there is that you want the NTP corrected time
during 'normal running' because the small correction (for
crystal error) is useful.

But the kernel HR timers are only defined for CLOCK_MONOTONIC
and the userspace requests for CLOCK_MONOTONIC_RAW are likely
to be real system calls.

What you really want is a clock whose frequency is adjusted
by NTP but doesn't have the NTP offset adjuctments.
In reality this ought to be CLOCK_MONOTONIC.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)