Clock: make monotonic_ms monotonic, so a clock jump can't freeze the session
CI / test (push) Failing after 52s
CI / test (push) Failing after 52s
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
f51ce088ad
commit
4fd2641c80
4 files changed
+30
-3
No files matched your search
@@ -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)
|
||||||
|
}
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
import {
|
import {
|
||||||
"moonbitlang/core/builtin",
|
"moonbitlang/core/builtin",
|
||||||
"moonbitlang/core/env" @env,
|
|
||||||
"chandra/desktop/interface/chandra/host/sys" @hostsys,
|
"chandra/desktop/interface/chandra/host/sys" @hostsys,
|
||||||
"chandra/desktop/interface/chandra/host/raster" @hostraster,
|
"chandra/desktop/interface/chandra/host/raster" @hostraster,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -329,9 +329,12 @@ pub fn read_proc_bytes(path : String) -> Bytes {
|
|||||||
c_read_proc(str_to_utf8_bytes(path))
|
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 {
|
pub fn monotonic_ms() -> UInt64 {
|
||||||
@env.now()
|
c_monotonic_ms()
|
||||||
}
|
}
|
||||||
|
|
||||||
///| Print a line to stderr (stdout on native).
|
///| Print a line to stderr (stdout on native).
|
||||||
|
|||||||
@@ -383,6 +383,15 @@ int32_t mp_getpid(void) {
|
|||||||
return (int32_t)getpid();
|
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). */
|
/* 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
|
MOONBIT_FFI_EXPORT
|
||||||
int32_t mp_wall_time_field(int32_t which) {
|
int32_t mp_wall_time_field(int32_t which) {
|
||||||
|
|||||||
Reference in new issue
Block a user