Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion mcpp.toml
Original file line number Diff line number Diff line change
@@ -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"

Expand Down
18 changes: 1 addition & 17 deletions src/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -488,22 +488,6 @@ okm_u32 start_mark(const bsdinfo& b) {
return static_cast<okm_u32>((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<okm_long>(&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,
Expand All @@ -513,7 +497,7 @@ bool cpu_ns(okm_long pid, okm_u64& out) {
okm::copy(reinterpret_cast<char*>(&user), reinterpret_cast<const char*>(ri + 16), 8);
okm::copy(reinterpret_cast<char*>(&sys), reinterpret_cast<const char*>(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;
}
Expand Down
17 changes: 17 additions & 0 deletions src/sys.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<okm_long>(&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
Expand Down
22 changes: 11 additions & 11 deletions src/time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<kal_duration>(numer) + denom - 1) / denom;
return ns ? ns : 1;
}

void kal_time_sleep(kal_duration ns) {
Expand Down
49 changes: 48 additions & 1 deletion tests/conformance_env_time.cpp
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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<kal_duration>(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<int>(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");
Expand Down Expand Up @@ -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");
Expand Down
Loading