qemu-discuss
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Issue with time going backwards on OSX


From: Christopher Snowhill (kode54)
Subject: Re: Issue with time going backwards on OSX
Date: Wed, 22 Sep 2021 12:04:40 -0700



On Sep 22, 2021, at 8:09 AM, Joe Tanen <jtanen@fb.com> wrote:

Hello,
 
I'm using qemu-system-aarch64 on OSX. While reading cntvct_el0, I've seen the ticks count go backwards. I dug into the qemu source a bit, and this register is eventually backed by a call to clock_gettime(CLOCK_MONOTONIC), which should not go backwards.
 
Before I started delving into debugging qemu, I wanted to verify that this wasn't an OS issue. I wrote a simple test that read CLOCK_MONOTONIC every 100ms, compared the values to make sure they always increased, and let it run overnight. Lo and behold, I saw time go backwards. I repeated the test with CLOCK_MONOTONIC_RAW, and I did not see time go backwards. When I ran this test on a Fedora PC, I never saw time go backwards. I'm going to pursue what I believe to be the underlying issue separately, but my qemu questions are these:
 
- Has anybody else seen this register go backwards? I don't see this running qemu-system-aarch64 on a Fedora PC, but I'm a small sample size.
- Would it make sense to use CLOCK_MONOTONIC_RAW (if available) instead of CLOCK_MONOTONIC in qemu? My issue aside, CLOCK_MONOTONIC_RAW feels closer to giving the guest direct access to a hardware clock. The code already checks to see if CLOCK_MONOTONIC is available, and, if not, it reverts to a simple gettimeofday(). It would be easy to add another check to init_get_clock() to enable this behavior. I'm testing this myself, but I wanted to see if there was upstream interest in this and/or if there were reasons to prefer the current way.
 
Thanks, and best regards,
Joe

It may already be going backwards on macOS if it's backed by the system time instead of an actual monotonic clock, since macOS periodically synchronizes its system clock with NTP.

It may be worth checking if CLOCK_MONOTONIC continues to run backwards if the machine is completely detached from the network, and also if CLOCK_MONOTONIC_RAW is always available on macOS. This may not matter much for older versions, since the ARM target is only available with Big Sur or newer anyway.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]