Issue #3493 has been updated by tuxillo.

Status changed from New to Resolved
% Done changed from 0 to 100

e00a1f839896ead4c23efce94f8428d03a3d0a14

----------------------------------------
Bug #3493: kernel: CLOCK_MONOTONIC_FAST steps back a tick in every process
http://bugs.dragonflybsd.org/issues/3493#change-15001

* Author: tuxillo
* Status: Resolved
* Priority: Normal
* Target version: 6.6
* Start date: 2026-12-03
----------------------------------------
As of master 21db329551, @CLOCK_MONOTONIC_FAST@ and @CLOCK_UPTIME_FAST@ step 
back by up to a tick in every process: the 11th clock_gettime() returns less 
than the 10th, by 10 ms at hz 100. Zig's std.time.Instant uses 
@CLOCK_MONOTONIC_FAST@ and panics when a later reading is smaller, which breaks 
the build of x11/ghostty in DPorts.

libc's clock_gettime() (lib/libc/upmap/ukp_clock.c) makes the system call for 
its first 10 calls, then reads ts_uptime from the kpmap, which hardclock() 
fills with the time of the current tick. The system call answers with 
getnanouptime(), which counts from gd_hardclock.time, and the systimer moves 
that to the cpu's next tick as soon as hardclock() returns. So between ticks 
the system call is up to a tick in the future, ahead of CLOCK_MONOTONIC, and an 
absolute clock_nanosleep() on these clocks returns early by the caller's own 
reading. @CLOCK_REALTIME_FAST@ is fine: getnanotime() returns the time cached 
at the tick, as the kpmap does.

The attached patch makes @kern_clock_gettime()@ read the kpmap for these two 
clocks, with the upticks protocol from sys/upmap.h, and fall back to 
getnanouptime() when there is no kpmap.

The attached fastclock.c looks, in 20 new processes per clock, for a reading 
below the previous one, a FAST reading ahead of the precise clock, and an early 
absolute clock_nanosleep(). On master the two clocks failed all three checks in 
20 of 20 processes in 3 of 3 runs, on a 12-thread Coffee Lake desktop and in an 
8-vCPU NVMM guest. With the patch, in the same guest, every check passes in 3 
of 3 runs. I haven't run the patched kernel on the desktop.

---Files--------------------------------
fastclock.c (5.09 KB)
0001-kernel-FAST-uptime-clocks-from-the-kpmap-in-clock_ge.patch (3.39 KB)


-- 
You have received this notification because you have either subscribed to it, 
or are involved in it.
To change your notification preferences, please click here: 
http://bugs.dragonflybsd.org/my/account

Reply via email to