Delve into clock rabbit hole

The worst issue I had with consts.sh for clock_gettime is how it defined
too many clocks. So I looked into these clocks all day to figure out how
how they overlap in functionality. I discovered counter-intuitive things
such as how CLOCK_MONOTONIC should be CLOCK_UPTIME on MacOS and BSD, and
that CLOCK_BOOTTIME should be CLOCK_MONOTONIC on MacOS / BSD. Windows 10
also has some incredible new APIs, that let us simplify clock_gettime().

  - Linux CLOCK_REALTIME         -> GetSystemTimePreciseAsFileTime()
  - Linux CLOCK_MONOTONIC        -> QueryUnbiasedInterruptTimePrecise()
  - Linux CLOCK_MONOTONIC_RAW    -> QueryUnbiasedInterruptTimePrecise()
  - Linux CLOCK_REALTIME_COARSE  -> GetSystemTimeAsFileTime()
  - Linux CLOCK_MONOTONIC_COARSE -> QueryUnbiasedInterruptTime()
  - Linux CLOCK_BOOTTIME         -> QueryInterruptTimePrecise()

Documentation on the clock crew has been added to clock_gettime() in the
docstring and in redbean's documentation too. You can read that to learn
interesting facts about eight essential clocks that survived this purge.
This is original research you will not find on Google, OpenAI, or Claude

I've tested this change by porting *NSYNC to become fully clock agnostic
since it has extensive tests for spotting irregularities in time. I have
also included these tests in the default build so they no longer need to
be run manually. Both CLOCK_REALTIME and CLOCK_MONOTONIC are good across
the entire amd64 and arm64 test fleets.
This commit is contained in:
Justine Tunney 2024-09-04 00:38:44 -07:00
parent 8f8145105c
commit dd8544c3bd
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
87 changed files with 939 additions and 900 deletions

View file

@ -0,0 +1,20 @@
#include "libc/nt/codegen.h"
.imp API-MS-Win-Core-Realtime-l1-1-1,__imp_QueryInterruptTime,QueryInterruptTime
.text.windows
.ftrace1
QueryInterruptTime:
.ftrace2
#ifdef __x86_64__
push %rbp
mov %rsp,%rbp
mov %rdi,%rcx
sub $32,%rsp
call *__imp_QueryInterruptTime(%rip)
leave
#elif defined(__aarch64__)
mov x0,#0
#endif
ret
.endfn QueryInterruptTime,globl
.previous

View file

@ -0,0 +1,20 @@
#include "libc/nt/codegen.h"
.imp API-MS-Win-Core-Realtime-l1-1-1,__imp_QueryInterruptTimePrecise,QueryInterruptTimePrecise
.text.windows
.ftrace1
QueryInterruptTimePrecise:
.ftrace2
#ifdef __x86_64__
push %rbp
mov %rsp,%rbp
mov %rdi,%rcx
sub $32,%rsp
call *__imp_QueryInterruptTimePrecise(%rip)
leave
#elif defined(__aarch64__)
mov x0,#0
#endif
ret
.endfn QueryInterruptTimePrecise,globl
.previous

View file

@ -0,0 +1,20 @@
#include "libc/nt/codegen.h"
.imp API-MS-Win-Core-Realtime-l1-1-1,__imp_QueryUnbiasedInterruptTimePrecise,QueryUnbiasedInterruptTimePrecise
.text.windows
.ftrace1
QueryUnbiasedInterruptTimePrecise:
.ftrace2
#ifdef __x86_64__
push %rbp
mov %rsp,%rbp
mov %rdi,%rcx
sub $32,%rsp
call *__imp_QueryUnbiasedInterruptTimePrecise(%rip)
leave
#elif defined(__aarch64__)
mov x0,#0
#endif
ret
.endfn QueryUnbiasedInterruptTimePrecise,globl
.previous

View file

@ -179,6 +179,27 @@ $(LIBC_NT_MEMORY_A).pkg: \
#───────────────────────────────────────────────────────────────────────────────
LIBC_NT_ARTIFACTS += LIBC_NT_REALTIME_A
LIBC_NT_REALTIME = $(LIBC_NT_REALTIME_A_DEPS) $(LIBC_NT_REALTIME_A)
LIBC_NT_REALTIME_A = o/$(MODE)/libc/nt/realtime.a
LIBC_NT_REALTIME_A_SRCS := $(wildcard libc/nt/API-MS-Win-Core-Realtime-l1-1-1/*.S)
LIBC_NT_REALTIME_A_OBJS = $(LIBC_NT_REALTIME_A_SRCS:%.S=o/$(MODE)/%.o)
LIBC_NT_REALTIME_A_CHECKS = $(LIBC_NT_REALTIME_A).pkg
LIBC_NT_REALTIME_A_DIRECTDEPS = LIBC_NT_KERNEL32
LIBC_NT_REALTIME_A_DEPS := \
$(call uniq,$(foreach x,$(LIBC_NT_REALTIME_A_DIRECTDEPS),$($(x))))
$(LIBC_NT_REALTIME_A): \
libc/nt/API-MS-Win-Core-Realtime-l1-1-1/ \
$(LIBC_NT_REALTIME_A).pkg \
$(LIBC_NT_REALTIME_A_OBJS)
$(LIBC_NT_REALTIME_A).pkg: \
$(LIBC_NT_REALTIME_A_OBJS) \
$(foreach x,$(LIBC_NT_REALTIME_A_DIRECTDEPS),$($(x)_A).pkg)
#───────────────────────────────────────────────────────────────────────────────
LIBC_NT_ARTIFACTS += LIBC_NT_USER32_A
LIBC_NT_USER32 = $(LIBC_NT_USER32_A_DEPS) $(LIBC_NT_USER32_A)
LIBC_NT_USER32_A = o/$(MODE)/libc/nt/user32.a

View file

@ -0,0 +1,20 @@
#include "libc/nt/codegen.h"
.imp kernel32,__imp_QueryUnbiasedInterruptTime,QueryUnbiasedInterruptTime
.text.windows
.ftrace1
QueryUnbiasedInterruptTime:
.ftrace2
#ifdef __x86_64__
push %rbp
mov %rsp,%rbp
mov %rdi,%rcx
sub $32,%rsp
call *__imp_QueryUnbiasedInterruptTime(%rip)
leave
#elif defined(__aarch64__)
mov x0,#0
#endif
ret
.endfn QueryUnbiasedInterruptTime,globl
.previous

View file

@ -218,8 +218,9 @@ imp 'Process32First' Process32FirstW kernel32 2
imp 'Process32Next' Process32NextW kernel32 2
imp 'PulseEvent' PulseEvent kernel32 1
imp 'PurgeComm' PurgeComm kernel32 2
imp 'QueryPerformanceCounter' QueryPerformanceCounter kernel32 1
imp 'QueryPerformanceCounter' QueryPerformanceCounter kernel32 1 # Windows 7+
imp 'QueryPerformanceFrequency' QueryPerformanceFrequency kernel32 1
imp 'QueryUnbiasedInterruptTime' QueryUnbiasedInterruptTime kernel32 1 # Windows 7+
imp 'ReadConsole' ReadConsoleW kernel32 5
imp 'ReadConsoleInput' ReadConsoleInputW kernel32 4
imp 'ReadConsoleOutput' ReadConsoleOutputW kernel32 5
@ -634,6 +635,13 @@ imp 'WakeByAddressSingle' WakeByAddressSingle API-MS-Win-Core-Synch-l1-2
imp 'MapViewOfFile3' MapViewOfFile3 API-MS-Win-Core-Memory-l1-1-6 9
imp 'VirtualAlloc2' VirtualAlloc2 API-MS-Win-Core-Memory-l1-1-6 7
# API-MS-Win-Core-Realtime-l1-1-1.dll (Windows 10+)
#
# Name Actual DLL Arity
imp 'QueryInterruptTime' QueryInterruptTime API-MS-Win-Core-Realtime-l1-1-1 1
imp 'QueryInterruptTimePrecise' QueryInterruptTimePrecise API-MS-Win-Core-Realtime-l1-1-1 1
imp 'QueryUnbiasedInterruptTimePrecise' QueryUnbiasedInterruptTimePrecise API-MS-Win-Core-Realtime-l1-1-1 1
# NTDLL.DLL
# BEYOND THE PALE
#

View file

@ -33,5 +33,10 @@ uint32_t GetTimeZoneInformation(
uint32_t GetDynamicTimeZoneInformation(
struct NtDynamicTimeZoneInformation *out_lpTimeZoneInformation);
bool32 QueryInterruptTime(uint64_t *); /* Windows 10+ */
bool32 QueryInterruptTimePrecise(uint64_t *); /* Windows 10+ */
bool32 QueryUnbiasedInterruptTime(uint64_t *); /* Windows 7+ */
bool32 QueryUnbiasedInterruptTimePrecise(uint64_t *); /* Windows 10+ */
COSMOPOLITAN_C_END_
#endif /* COSMOPOLITAN_LIBC_NT_TIME_H_ */