Repository navigation
Description
On a normal bare metal Linux system:
Name Cost(ns) +/- Resol Mono Fail Warp Stal Regr
tsc 10.97 0.21% ---- Yes 0 0 0 0
gettimeofday 17.60 0.01% 1000KHz No 0 0 1000 0
17.60 0.01%
realtime 17.60 0.07% ---- Yes 0 0 0 0
realtime_crs 4.66 0.06% 100Hz No 999 0 0 0
3.33 142.09%
monotonic 17.20 0.06% ---- Yes 0 0 0 0
monotonic_crs 4.52 0.02% 100Hz No 999 0 0 0
3.34 142.09%
monotonic_raw 69.76 0.10% ---- Yes 0 0 0 0
boottime 73.53 0.05% ---- Yes 0 0 0 0
process 115.67 0.08% ---- Yes 0 0 0 0
thread 109.64 0.07% ---- Yes 0 0 0 0
clock 116.93 0.04% 1000KHz No 0 0 1000 0
getrusage 181.32 0.05% 100Hz No 996 3 3 0
ftime 20.78 0.04% 1000Hz No 995 0 4 0
time 2.84 0.03% 1Hz No 1000 0 0 0
On "Bash on Windows", the clock behavior isn't as nice:
Name Cost(ns) +/- Resol Mono Fail Warp Stal Regr
tsc 12.53 86.77% ---- Yes 0 0 0 0
gettimeofday 166.10 0.72% 1032Hz No 943 5 55 0
realtime 182.39 1.02% 3333KHz No 0 0 131 0
realtime_crs 168.97 0.83% 2144Hz No 934 0 65 0
monotonic 186.90 0.38% 1000KHz No 0 0 998 0
monotonic_crs 188.76 0.33% 1000KHz No 0 0 998 0
Failed to read from monotonic_raw clock (1, 4)
boottime 186.66 0.37% 1000KHz No 0 0 998 0
process 272.19 0.81% 64Hz No 996 3 3 0
thread 229.25 0.84% 64Hz No 997 2 2 0
clock 278.50 1.23% 64Hz No 996 3 3 0
getrusage 385.45 0.78% 64Hz No 995 4 4 0
ftime 207.14 0.81% 1000Hz No 959 0 40 0
time 156.06 0.86% 1Hz No 1000 0 0 0
To explain the columns a bit:
The Cost(ns) column is the time cost to read that clock in nanoseconds, as measured by the TSC (except when measuring the cost of reading the TSC itself, which is measured by looking at gettimeofday).
Resol is the observable tick rate of the clock.
Mono indicates whether the clock source is monotonic, with an additional restriction. Not only must the clock only move forward, it must never return the same value (i.e. high frequency).
Fail indicates the number of times the clock source failed to advance in >= 200 reads.
Warp indicates the number of times the clock jumped forward by an unexpectedly large span (threshold for this is currently 1ms).
Stal indicates the number of times the clock source failed to advance in more than 1, but less than 200 reads.
Regr indicates the number of times the clock moved backwards. This can happen due to hypervisor behavior, NTP adjustments, and other clock changes. It's extremely bad behavior if you use the clock source for timespan measurement (e.g. profiling, benchmarks, etc).
Activity
russalex commentedon Apr 9, 2016
Amazing bug. Thanks for the details. I am taking this back to the team. Will update here when I have more info.
HenkPoley commentedon Apr 15, 2016
What tool did you use to derive those numbers?
tycho commentedon Apr 15, 2016
Hi Henk Poley (@HenkPoley). I sent the source to Microsoft through other channels. But since people are asking, I've published the source code under the ISC license here: https://github.com/tycho/clockperf
tycho commentedon Aug 2, 2016
No obvious improvement on 14393.10:
russalex commentedon Aug 2, 2016
Still a great bug, but unfortunately we did not have enough time to dive into this as much as I would have liked to in this release. As of not it remains on the backlog.
tycho commentedon Aug 3, 2016
Russ Alexander (@russalex) Understood. Just wanted to post an update with the latest build. Not trying to force a change ahead of schedule. 😄
tycho commentedon Apr 27, 2018
It's been a while since I did an update on this issue, so let's see how things are in a very recent build of Windows (built April 20th, according to the version stamp).
It looks like clock sources have gotten much more expensive (2-3x?), though I'm not sure what system I originally measured on now so these results aren't exactly fair. But then again, I'd expect this more modern system to have unfairly better results, not worse.
This system is an Intel Core i7-7820X host, running build 17655:
As an aside...
CLOCK_REALTIMEmay appear to be monotonic above, but it's probably not. The criteria clockperf uses is "did we see a higher value on every read?" But that alleged monotonicity may just be because it's an expensive clock source to read. The claimed resolution (via theclock_getresAPI) is 3333KHz. In order to actually observe that frequency we'd need to get at least one identical read, and to do that we'd need to be able to read it faster than it updates. If the claimed resolution is to be believed, then the cost to read would need to be under 300ns for an identical read to happen. Right now it's far over that.Also, none of the
clock_gettimesources appear to have observable resolutions that match their claimedclock_getresvalues.On the plus side, it looks like
CLOCK_MONOTONIC_RAWgot implemented. Hooray!tycho commentedon Mar 2, 2019
Time for a pseudo-yearly update. Same host hardware as before, but now running on insider preview build number 18348.1 (19h1_release.190226-1407).
Observations:
CLOCK_REALTIME(but not coarse variant),CLOCK_MONOTONIC(and coarse/raw variants), andCLOCK_BOOTTIMEare all now 10MHz clocks (QueryPerformanceCounter under the hood, I presume?)CLOCK_PROCESS_CPUTIME_IDandCLOCK_THREAD_CPUTIME_IDare now reporting frequencies throughclock_getres()(previously they were readable withclock_gettime()but their frequencies could not be queried withclock_getres()for some reason)CLOCK_REALTIME_COARSEdoesn't match the results. It claims (viaclock_getres()) to be a 64Hz clock but it's observably ticking at 1KHz.clock()isn't ticking at the correct frequency. It's observably 64Hz, but it should be ticking at the constantCLOCKS_PER_SECvalue (1MHz). This is a side effect ofCLOCK_PROCESS_CPUTIME_IDbeing too low resolution, at 64Hz (glibc usesCLOCK_PROCESS_CPUTIME_IDto implementclock()under the hood).tycho commentedon Mar 4, 2019
I think that's a separate problem.
I'm not particularly concerned with the real time clock as I am with the high resolution clock sources that drat near everything uses. If a monotonic clock costs a lot to read, it'll artificially hurt performance of some benchmarks (e.g. running a workload like
fiowith latency measurement would have massive overhead just from reading the clock). It'll also hurt any real world applications that read the clock -- which is almost anything with a tick rate implemented (e.g. game servers).In some further testing in the past few days I noticed that
getrusage()andclock()start doing really weird stuff after a while on WSL, too. I don't think they actually hooked up proper process/thread time accounting to those APIs, which could screw with some benchmarks. For instance, theopenssl speedtool usesgetrusage()to determine CPU time used during a run, instead of using wall clock time.tycho commentedon Mar 4, 2019
This has nothing to do with timezones or the realtime clock.
EmpiricEmpire commentedon Mar 5, 2019
Yeah, silly Darthie.
h1z1 commentedon Apr 27, 2020
Been following this for a while. Curious Steven Noonan (@tycho) what hardware were you testing on because I find some systems are abysmal wrt clock performance regardless of what is tried. That is in Linux too.
Hi-Angel commentedon Apr 27, 2020
Just a sec. ago downloaded the code, built and tested. Looks fine for me. A laptop Dell Inspiron 5370 here, Archlinux, kernel 5.6.6, libc 2.31
Hi-Angel commentedon Apr 27, 2020
It varies though, I guess it depends on the current clock frequency.

h1z1 commentedon Apr 27, 2020
That is what I suspect in some cases - the hardware and or drivers. CPU affinity, CPU speed changes, system clocksource, etc all appear to cause instability that shouldn't be happening.
tycho commentedon Jun 23, 2020
This is much better under WSL2, since it doesn't need to rely on Win32 clock sources under the hood:
I'm going to call this resolved since WSL2 seems to be the way forward.
WSLUser commentedon Jun 23, 2020
This is much better under WSL2, since it doesn't need to rely on Win32 clock sources under the hoodWSL2 is a lightweight VM. So like other VMs, it gets it's clock source from the host. So what you said is completely wrong. There is an issue however with the clock drifting and is corrected by using
wsl --shutdown.So what we can say is "WSL2 uses the Hyper-V clock sync in the backend to the Win32 clock sources, which is much faster and more precise than the clock fetching performed by WSL1."
tycho commentedon Jun 23, 2020
You misinterpreted what I said.
With WSL1, it had to map all the Linux clock APIs like
gettimeofday/CLOCK_REALTIME/etc to Win32/NT clock APIs likeGetTickCount/timeGetTime/GetSystemTimeAsFileTime/etc. Nothing in WSL2 is doing a layer of indirection to map Linux clock APIs into Windows APIs. It's all effectively coming straight from the TSC, with a clock divider to make the TSC a constant 10MHz instead of the hardware-native TSC frequency (this is normally done for things like VM live migration, even though that doesn't make much sense in the case of a WSL guest). Even Win32 APIs rely on the TSC or HPET internally, but my point was that the extra layer of indirection WSL1 had between userspace and the hardware clock read is eliminated by WSL2.The clock drift/sync issues you're referring to sounds more like issues with the RTC drifting, which is a different story. And that is orthogonal to the high resolution clock sources. I'm also surprised to hear the RTC is drifting at all with WSL2, as I would have expected that to be handled by some Hyper-V enlightenment. The next time you see it drift, does
hwclock --hctosys --verboseas root in the WSL2 guest temporarily fix it? That should also tell you how bad the drift is at the time (look for theCalculated Hardware Clock drift is ...line), and it should get around needing to do a fullwsl --shutdown.