From 4fd2641c80d5b788f197ecdc0ddd5480ef635331 Mon Sep 17 00:00:00 2001 From: nandithebull Date: Sun, 29 Nov 2026 02:22:27 -0800 Subject: [PATCH] Clock: make monotonic_ms monotonic, so a clock jump can't freeze the session It read the wall clock. When NTP moved a phone from 1970 to now, the frame loop's elapsed time overflowed and it slept for days. Co-Authored-By: Claude Opus 5.5 --- compositor/sys/clock_test.mbt | 16 ++++++++++++++++ compositor/sys/moon.pkg | 1 - compositor/sys/sys_native.mbt | 7 +++++-- compositor/sys/sys_stub.c | 9 +++++++++ 4 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 compositor/sys/clock_test.mbt diff --git a/compositor/sys/clock_test.mbt b/compositor/sys/clock_test.mbt new file mode 100644 index 0000000..2364612 --- /dev/null +++ b/compositor/sys/clock_test.mbt @@ -0,0 +1,16 @@ +///| +/// The main loop sleeps for a difference of two `monotonic_ms`: on the wall clock, NTP setting the +/// time (1970 to now on a phone) froze the session for days. CLOCK_MONOTONIC can't be later than +/// /proc/uptime, which also counts suspended time; the wall clock is far past it. +test "monotonic_ms is not the wall clock" { + let before = @sys.monotonic_ms() + let text = @sys.read_proc_bytes("/proc/uptime") + let mut secs = 0UL + for c in text { + guard c is (b'0'..=b'9') else { break } + secs = secs * 10UL + (c - b'0').to_uint64() + } + assert_true(secs > 0UL) + assert_true(before <= (secs + 1UL) * 1000UL) + assert_true(@sys.monotonic_ms() >= before) +} diff --git a/compositor/sys/moon.pkg b/compositor/sys/moon.pkg index 09303d1..29209dc 100644 --- a/compositor/sys/moon.pkg +++ b/compositor/sys/moon.pkg @@ -1,6 +1,5 @@ import { "moonbitlang/core/builtin", - "moonbitlang/core/env" @env, "chandra/desktop/interface/chandra/host/sys" @hostsys, "chandra/desktop/interface/chandra/host/raster" @hostraster, } diff --git a/compositor/sys/sys_native.mbt b/compositor/sys/sys_native.mbt index 042b816..f0180c4 100644 --- a/compositor/sys/sys_native.mbt +++ b/compositor/sys/sys_native.mbt @@ -329,9 +329,12 @@ pub fn read_proc_bytes(path : String) -> Bytes { c_read_proc(str_to_utf8_bytes(path)) } -///| Milliseconds on a monotonic clock. +///| +extern "C" fn c_monotonic_ms() -> UInt64 = "mp_monotonic_ms" + +///| Milliseconds on a monotonic clock (CLOCK_MONOTONIC: setting the time doesn't move it). pub fn monotonic_ms() -> UInt64 { - @env.now() + c_monotonic_ms() } ///| Print a line to stderr (stdout on native). diff --git a/compositor/sys/sys_stub.c b/compositor/sys/sys_stub.c index c212ca3..3e1232a 100644 --- a/compositor/sys/sys_stub.c +++ b/compositor/sys/sys_stub.c @@ -383,6 +383,15 @@ int32_t mp_getpid(void) { return (int32_t)getpid(); } +/* Not the wall clock: that jumps (a phone boots in 1970 until NTP answers), and the frame loop + * sleeps for the difference between two of these. */ +MOONBIT_FFI_EXPORT +uint64_t mp_monotonic_ms(void) { + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000; +} + /* Local wall-clock field: 0 year, 1 month(1-12), 2 day, 3 hour, 4 minute, 5 second, 6 weekday(0=Sun). */ MOONBIT_FFI_EXPORT int32_t mp_wall_time_field(int32_t which) {