From 912962ea5483856d1e4b6cd343f79b5f8f5444fc Mon Sep 17 00:00:00 2001 From: Sunrisepeak Date: Tue, 3 Nov 2026 09:23:45 +0900 Subject: [PATCH] 0.21.2 --- the monotonic source's granularity is the kernel's tick, the same at every start Co-authored-by: speak-agent <248744407+speak-agent@users.noreply.github.com> --- README.md | 11 +++++++- mcpp.toml | 2 +- src/process.cpp | 18 +------------ src/sys.h | 17 ++++++++++++ src/time.cpp | 22 +++++++-------- tests/conformance_env_time.cpp | 49 +++++++++++++++++++++++++++++++++- 6 files changed, 88 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 9ec4242..c3b8eb8 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ written on the kernel's own calls. openkal = "0.23.0" [target.'cfg(os = "macos")'.dependencies] -openkal-macos = "0.21.1" +openkal-macos = "0.21.2" ``` Its purpose is as much to test the specification as to be used. A specification @@ -207,6 +207,15 @@ wait reads its word and asks again on `EFAULT`, as the system's own C library does. The conformance suite asserts that 200 ms asleep is under 50 ms of processor time. +**The granularity is the machine's (0.21.2).** `kal_time_monotonic_granularity` +read the source until it changed, and the difference --- 41, 42 or more as the +reading loop was scheduled --- differed from one start of a program to the next. +It is now the kernel's tick (`mach_timebase_info`) rounded up: 42 ns on arm64, +1 ns on x86_64. openkal-image's runner compares it among the implementation's +facts before it uses a snapshot of a program's initialisers, and declined one +for that difference alone. The conformance suite asserts that another start of +a program states the same granularity. + ## What 0.20 adds (openkal 0.22) **The null device at a position.** `KAL_SPAWN_NULL_IN`, `KAL_SPAWN_NULL_OUT` and diff --git a/mcpp.toml b/mcpp.toml index b5c020c..b2d784c 100644 --- a/mcpp.toml +++ b/mcpp.toml @@ -1,7 +1,7 @@ [package] namespace = "mcpplibs" name = "openkal-macos" -version = "0.21.1" +version = "0.21.2" description = "An implementation of openkal for macOS, written on the kernel's own calls. Its purpose is as much to test the specification as to be used." license = "Apache-2.0" diff --git a/src/process.cpp b/src/process.cpp index fc20030..afd085e 100644 --- a/src/process.cpp +++ b/src/process.cpp @@ -488,22 +488,6 @@ okm_u32 start_mark(const bsdinfo& b) { return static_cast((us ^ (us >> 24) ^ (us >> 48)) & 0xffffffu); } -// The kernel reports processor time in its own ticks, and this is the ratio to -// nanoseconds (`mach_timebase_info', a Mach trap): 125/3 on the processors this -// system now runs on, 1/1 on the earlier ones. -void timebase(okm_u32& numer, okm_u32& denom) { - struct { okm_u32 numer, denom; } tb{1, 1}; -#if defined(__aarch64__) - register okm_long x0 __asm__("x0") = reinterpret_cast(&tb); - register okm_long x16 __asm__("x16") = -89; - __asm__ __volatile__("svc #0x80" : "+r"(x0) : "r"(x16) : "memory", "cc", "x1"); -#else - okm_long r; - __asm__ __volatile__("syscall" : "=a"(r) : "a"(0x1000000 + 89), "D"(&tb) : "rcx", "r11", "memory"); -#endif - numer = tb.numer ? tb.numer : 1; denom = tb.denom ? tb.denom : 1; -} - bool cpu_ns(okm_long pid, okm_u64& out) { alignas(8) unsigned char ri[400] = {}; // rusage_info_v0 and some const okm_long r = okm::sys(okm::nr_proc_info, 9 /* PROC_INFO_CALL_PIDRUSAGE */, pid, @@ -513,7 +497,7 @@ bool cpu_ns(okm_long pid, okm_u64& out) { okm::copy(reinterpret_cast(&user), reinterpret_cast(ri + 16), 8); okm::copy(reinterpret_cast(&sys), reinterpret_cast(ri + 24), 8); okm_u32 n = 1, d = 1; - timebase(n, d); + okm::timebase(n, d); out = (user + sys) / d * n + (user + sys) % d * n / d; return true; } diff --git a/src/sys.h b/src/sys.h index f034051..8b79f12 100644 --- a/src/sys.h +++ b/src/sys.h @@ -477,6 +477,23 @@ inline void relax() { #endif } +// The ratio of the kernel's ticks to nanoseconds (`mach_timebase_info', a Mach +// trap): 125/3 on the processors this system now runs on, 1/1 on the earlier +// ones. The kernel reports processor time in these ticks, and the monotonic +// source advances by one of them. +inline void timebase(okm_u32& numer, okm_u32& denom) { + struct { okm_u32 numer, denom; } tb{1, 1}; +#if defined(__aarch64__) + register okm_long x0 __asm__("x0") = reinterpret_cast(&tb); + register okm_long x16 __asm__("x16") = -89; + __asm__ __volatile__("svc #0x80" : "+r"(x0) : "r"(x16) : "memory", "cc", "x1"); +#else + okm_long r; + __asm__ __volatile__("syscall" : "=a"(r) : "a"(0x1000000 + 89), "D"(&tb) : "rcx", "r11", "memory"); +#endif + numer = tb.numer ? tb.numer : 1; denom = tb.denom ? tb.denom : 1; +} + // The identity of the calling context. // // Version 0.3 read it from the register each architecture reserves for the diff --git a/src/time.cpp b/src/time.cpp index 1d4daef..47e4683 100644 --- a/src/time.cpp +++ b/src/time.cpp @@ -40,17 +40,17 @@ kal_duration kal_time_wall(void) { } kal_duration kal_time_monotonic_granularity(void) { - // Measured rather than declared: the source is read until it changes, and - // the difference is what it can resolve. A granularity that was asserted - // rather than observed would be a claim about the machine this was written - // on rather than about the one it is running on. - static kal_duration remembered = 0; - if (remembered) return remembered; - const kal_duration first = kal_time_monotonic(); - kal_duration next = first; - for (int i = 0; i < 1000000 && next == first; ++i) next = kal_time_monotonic(); - remembered = next > first ? next - first : 1; - return remembered; + // The source advances by one of the kernel's ticks, and the kernel states + // what a tick is on the machine it runs on (okm::timebase): 41.67 ns on + // arm64, rounded up, and 1 ns on x86_64. Until 0.21.2 the source was read + // until it changed, and the difference --- 41, 42 or more, as the reading + // loop was scheduled --- differed from one start of a program to the next; + // a loader that keeps what a program's initialisers computed compares the + // implementation's facts, and declined it for that difference alone. + okm_u32 numer = 1, denom = 1; + okm::timebase(numer, denom); + const kal_duration ns = (static_cast(numer) + denom - 1) / denom; + return ns ? ns : 1; } void kal_time_sleep(kal_duration ns) { diff --git a/tests/conformance_env_time.cpp b/tests/conformance_env_time.cpp index 24bc772..5571f47 100644 --- a/tests/conformance_env_time.cpp +++ b/tests/conformance_env_time.cpp @@ -1,8 +1,16 @@ // Conformance: openkal.env and openkal.time. +// +// `_NSGetExecutablePath' and `realpath' are the system's own, declared without +// its headers as tests/conformance_spawn.cpp declares them: this test starts +// itself again by its name. import openkal.env; import openkal.time; import openkal.stream; import openkal.process; +import openkal.fs; + +extern "C" int _NSGetExecutablePath(char* buf, unsigned* size); +extern "C" char* realpath(const char* path, char* resolved); namespace { int failures = 0; @@ -18,6 +26,7 @@ kal_u64 processor_ns() { kal_process_close(me); return ok ? info.cpu_ns : ~0ull; } + void check(bool ok, const char* what) { if (ok) return; ++failures; @@ -27,9 +36,41 @@ void check(bool ok, const char* what) { kal::write(kal::err(), what, n); kal::write(kal::err(), "\n", 1); } + +bool is(const char* a, const char* b) { + while (*a && *a == *b) { ++a; ++b; } + return *a == *b; } -int main() { +// The granularity another start of this program states, or -1. +kal_duration granularity_elsewhere() { + char raw[1024], self[1024]; unsigned size = sizeof raw; + if (_NSGetExecutablePath(raw, &size) != 0 || realpath(raw, self) == nullptr) return ~0ull; + kal_dir root{}; + for (kal_uintptr i = 0; i < kal_fs_preopen_count(); ++i) { + kal_dir d{}; char n[8]; kal_uintptr l = 0; + if (kal_fs_preopen(i, &d, n, sizeof n, &l) == kal_ok && l == 1 && n[0] == '/') root = d; + } + const char* argv[2] = { "conformance_env_time", "--granularity" }; + const kal_uintptr lens[2] = { 20, 13 }; + const kal_spawn how{ root, kal::fs::working(), nullptr, nullptr, 0, 0 }; + kal_process p{}; + kal_uintptr n = 0; while (self[1 + n]) ++n; + if (kal_process_spawn(&how, self + 1, n, argv, lens, 2, nullptr, nullptr, 0, nullptr, &p) != kal_ok) + return ~0ull; + int status = -1, terminated = -1; + kal_process_wait(p, &status, &terminated); + kal_process_close(p); + return terminated == 0 && status >= 0 ? static_cast(status) : ~0ull; +} +} + +int main(int argc, char** argv) { + // The other start of granularity_elsewhere: its granularity as its status. + if (argc > 1 && is(argv[1], "--granularity")) { + const kal_duration g = kal::time::granularity(); + return g < 250 ? static_cast(g) : 250; + } // A program always receives the name it was started with, even where the // environment has none, in which case it is empty rather than absent. check(kal::env::arg_count() >= 1, "at least the program name is present"); @@ -66,6 +107,12 @@ int main() { check(c0 != ~0ull && c1 != ~0ull && c1 - c0 < 50 * 1000 * 1000, "200 ms asleep is under 50 ms of processor time"); + // THE GRANULARITY IS THE MACHINE'S, NOT A START'S (0.21.2): another start + // of this program states the same. A loader that keeps what a program's + // initialisers computed compares it among the implementation's facts. + check(granularity_elsewhere() == kal::time::granularity(), + "another start states the same granularity"); + // The wall source is claimed by this implementation, so it must report a // time after the specification was written rather than zero. check(kal::time::has(kal::time::wall_available), "the wall source is claimed");