mirror of
https://github.com/pithings/unkernel.git
synced 2026-12-04 23:19:45 +08:00
333 lines
14 KiB
TypeScript
333 lines
14 KiB
TypeScript
// SPDX-License-Identifier: MIT
|
|
// Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>
|
|
// Host side of kwemu's JIT tier (src/emu/jit.zig; contract in
|
|
// include/kw_emu.h and .agents/kwemu.md).
|
|
//
|
|
// A kernel whose kwemu is built with the tier (kernel/build.sh does so by
|
|
// default) imports three functions from module "unkernel":
|
|
//
|
|
// kw_host_jit_caps() -> u32 bit 0 available, bit 1 32-bit memory
|
|
// (the lowered vmlinux32.wasm), bit 2 eager
|
|
// (tests only; never set here)
|
|
// kw_host_jit_compile(ptr, len, slot) -> i32
|
|
// compile the module at ptr (it imports
|
|
// env.memory, exports "f") and install "f"
|
|
// in table slot `slot`; 0 ok, -1 failed
|
|
// kw_host_jit_free(slot) put the slot's original function back
|
|
//
|
|
// kwemu owns the slots: a pool of stub functions that are already in the
|
|
// kernel's exported __indirect_function_table, so the table never grows.
|
|
// Compiling is synchronous (new WebAssembly.Module / Instance); the modules
|
|
// are a few KiB. A failure returns -1 (kwemu then interprets that block for
|
|
// good) and never throws into the kernel. The only reference to a compiled
|
|
// module is its table slot, so a free (or the kernel being dropped)
|
|
// releases it. A kernel built without the tier never calls these imports.
|
|
//
|
|
// Released is not collected, though. A compiled module costs the engine
|
|
// more memory than the garbage collector sees (JavaScriptCore: about 24 KiB
|
|
// per real kwemu module, V8: about 12 KiB), and JavaScriptCore schedules its
|
|
// next collection from a heap size that includes the kernel's wasm memory:
|
|
// with 512 MiB of RAM it waited for about 3 GB of further allocation, which
|
|
// compiling never reaches, so freed modules were never collected. The host
|
|
// therefore counts what the engine has collected (a FinalizationRegistry on
|
|
// every module) and, while RETAINED_NUDGE or more freed modules are still
|
|
// uncollected, allocates a 64 MiB ArrayBuffer on every compile and free and
|
|
// detaches it at once (transfer(0); it is never touched, so it costs about
|
|
// 10-30 us). JavaScriptCore counts the allocation towards its next
|
|
// collection, which then reclaims the modules. V8 (Chromium) starts a
|
|
// collection only for an allocation about as large as the external memory
|
|
// it already has, which includes the kernel's wasm memory: with 512 MiB of
|
|
// RAM a quiet page reached the refusal limit with 64 and 256 MiB nudges and
|
|
// collected at once with 512 MiB. So while nudges see no collection for
|
|
// NUDGE_PATIENCE_MS, each one is twice as large (up to NUDGE_MAX; an
|
|
// allocation that fails halves it). If an engine still does not collect,
|
|
// compiles are refused (-1, kwemu interprets those blocks) while
|
|
// RETAINED_LIMIT freed modules are uncollected.
|
|
|
|
/** JIT counters of one kernel (Kernel.jit.stats). */
|
|
export interface JitStats {
|
|
/** The host offers the JIT (KernelConfig.jit, and the engine can compile synchronously). */
|
|
enabled: boolean;
|
|
/** The kernel's kwemu asked for it (kw_host_jit_caps was called): false for a kernel without the tier. */
|
|
requested: boolean;
|
|
/** Blocks compiled and installed. */
|
|
compiled: number;
|
|
/** Compiled blocks currently installed (compiled - freed). */
|
|
live: number;
|
|
/** Most blocks installed at once (kwemu's pool has 1024 slots). */
|
|
maxLive: number;
|
|
/** Slots kwemu released (block overwritten, evicted or cache reset). */
|
|
freed: number;
|
|
/** Compiles that failed (kwemu interprets those blocks), plus rejected calls. */
|
|
failed: number;
|
|
/** Total time spent compiling and instantiating, in ms. */
|
|
compileMs: number;
|
|
/** Total size of the compiled modules, in bytes. */
|
|
bytes: number;
|
|
/** Compiled modules the engine has garbage-collected (-1: no FinalizationRegistry, unknown). */
|
|
collected: number;
|
|
/** Freed modules not collected yet (freed - collected; -1 when unknown). */
|
|
retained: number;
|
|
/** Most freed modules uncollected at once (-1 when unknown). */
|
|
maxRetained: number;
|
|
/** ArrayBuffers allocated to prompt a collection (retained above RETAINED_NUDGE). */
|
|
gcNudges: number;
|
|
/** Compiles refused because retained reached RETAINED_LIMIT (kwemu interprets those blocks). */
|
|
refused: number;
|
|
}
|
|
|
|
/**
|
|
* kwemu's own counters, from a kernel built with -Dkwemu-stats
|
|
* (`kernel/build.sh stats`) that has compiled at least one block.
|
|
*/
|
|
export interface KwemuStats {
|
|
/** Guest instructions retired (struct kw_cpu icount). */
|
|
icount: number;
|
|
/**
|
|
* kwemu.Stats as 64-bit words, in the layout of src/emu/kwemu.zig: 11
|
|
* counters, op_hist[192], JIT insns, hand-backs, hb[192], hb_gen[40],
|
|
* jx[64] (indices in src/emu/jit.zig Jx).
|
|
*/
|
|
words: number[];
|
|
}
|
|
|
|
export interface JitHostOptions {
|
|
/** Offer the JIT (default true); false: kw_host_jit_caps() returns 0. */
|
|
enabled?: boolean;
|
|
/** The kernel's memory, read at call time (boot may switch memory types). */
|
|
memory: () => WebAssembly.Memory;
|
|
/** Whether that memory is a memory64 one, read at call time. */
|
|
memory64: () => boolean;
|
|
log?: (msg: string) => void;
|
|
/** Uncollected freed modules from which the host prompts a collection (default RETAINED_NUDGE; Infinity: never). */
|
|
retainedNudge?: number;
|
|
/** Uncollected freed modules at which compiles are refused (default RETAINED_LIMIT; Infinity: never). */
|
|
retainedLimit?: number;
|
|
}
|
|
|
|
const CAP_AVAILABLE = 1;
|
|
const CAP_MEM32 = 2;
|
|
/** kwemu's emit buffer is 16 KiB; anything much larger is not from kwemu. */
|
|
const MAX_MODULE = 1 << 20;
|
|
/** Failure messages logged before going quiet. */
|
|
const MAX_LOGGED = 5;
|
|
/** Words in kwemu.Stats with -Dkwemu-stats (src/emu/kwemu.zig), and the offset of icount in struct kw_cpu. */
|
|
const KWEMU_STATS_WORDS = 11 + 192 + 2 + 192 + 40 + 64;
|
|
const KW_CPU_ICOUNT = 32;
|
|
/** Uncollected freed modules from which every compile and free prompts a collection. */
|
|
const RETAINED_NUDGE = 256;
|
|
/** Uncollected freed modules at which compiles are refused until the engine catches up. */
|
|
const RETAINED_LIMIT = 2048;
|
|
/** Size of one prompt. Untouched and detached at once, so it costs address space only briefly. */
|
|
const NUDGE_BYTES = 64 << 20;
|
|
/** Largest prompt (the size doubles while prompts do not lead to a collection). */
|
|
const NUDGE_MAX = 2 ** 32;
|
|
/** Prompts without a collection for this long double the prompt size. */
|
|
const NUDGE_PATIENCE_MS = 250;
|
|
|
|
/** Table index API shared by table64 (BigInt indices) and 32-bit tables. */
|
|
interface AnyTable {
|
|
readonly length: number | bigint;
|
|
get(index: number | bigint): unknown;
|
|
set(index: number | bigint, value: unknown): void;
|
|
}
|
|
|
|
type Detachable = ArrayBuffer & { transfer?: (len: number) => ArrayBuffer };
|
|
|
|
export class JitHost {
|
|
readonly stats: JitStats;
|
|
#opts: JitHostOptions;
|
|
#table: AnyTable | undefined;
|
|
#big = false;
|
|
/** slot -> the function it held before its first compile (kwemu's stub). */
|
|
#saved = new Map<number, unknown>();
|
|
#logged = 0;
|
|
/** Collected-module counter, or undefined when the engine has no FinalizationRegistry. */
|
|
#fin: FinalizationRegistry<number> | undefined;
|
|
#nudgeAt: number;
|
|
#limit: number;
|
|
#nudgeBytes = NUDGE_BYTES;
|
|
/** When, and at which `collected` count, the prompt size was last reconsidered. */
|
|
#nudgeCheckAt = 0;
|
|
#nudgeCheckCollected = 0;
|
|
/** -Dkwemu-stats kernels: the stats-only exports, and the emit buffer of the first compile. */
|
|
#debugStats: ((cpu: bigint, out: bigint) => void) | undefined;
|
|
#cpuOfBuf: ((buf: bigint) => bigint) | undefined;
|
|
#buf: number | undefined;
|
|
|
|
constructor(opts: JitHostOptions) {
|
|
this.#opts = opts;
|
|
const enabled = opts.enabled !== false && typeof WebAssembly === "object" &&
|
|
typeof WebAssembly.Module === "function" && typeof WebAssembly.Instance === "function";
|
|
const tracked = typeof FinalizationRegistry === "function";
|
|
this.stats = {
|
|
enabled, requested: false, compiled: 0, live: 0, maxLive: 0, freed: 0, failed: 0, compileMs: 0, bytes: 0,
|
|
collected: tracked ? 0 : -1, retained: tracked ? 0 : -1, maxRetained: tracked ? 0 : -1, gcNudges: 0, refused: 0,
|
|
};
|
|
if (tracked) this.#fin = new FinalizationRegistry(() => this.#collected());
|
|
this.#nudgeAt = opts.retainedNudge ?? RETAINED_NUDGE;
|
|
this.#limit = opts.retainedLimit ?? RETAINED_LIMIT;
|
|
}
|
|
|
|
/** Bind to the kernel instance's function table; call after instantiation, before _start. */
|
|
attach(exports: WebAssembly.Exports): void {
|
|
const t = exports.__indirect_function_table;
|
|
this.#table = t instanceof WebAssembly.Table ? (t as unknown as AnyTable) : undefined;
|
|
this.#big = typeof this.#table?.length === "bigint";
|
|
const ds = exports.kw_debug_stats, cb = exports.kw_debug_cpu_of_jit_buf;
|
|
this.#debugStats = typeof ds === "function" ? (ds as (cpu: bigint, out: bigint) => void) : undefined;
|
|
this.#cpuOfBuf = typeof cb === "function" ? (cb as (buf: bigint) => bigint) : undefined;
|
|
this.#buf = undefined;
|
|
}
|
|
|
|
/**
|
|
* kwemu's counters, if the kernel exports them (-Dkwemu-stats) and has
|
|
* compiled a block (that is how the cpu is found). It writes the emit
|
|
* buffer, which is free outside kw_host_jit_compile, so any call from
|
|
* JavaScript is safe.
|
|
*/
|
|
kwemuStats(): KwemuStats | undefined {
|
|
if (!this.#debugStats || !this.#cpuOfBuf || this.#buf === undefined) return undefined;
|
|
const cpu = this.#cpuOfBuf(BigInt(this.#buf));
|
|
// The emit buffer (16 KiB) is free between compiles: copy the
|
|
// counters (4008 bytes) into it, then read them.
|
|
this.#debugStats(cpu, BigInt(this.#buf));
|
|
const v = new DataView(this.#opts.memory().buffer);
|
|
const words: number[] = [];
|
|
for (let i = 0; i < KWEMU_STATS_WORDS; i++) words.push(Number(v.getBigUint64(this.#buf + 8 * i, true)));
|
|
return { icount: Number(v.getBigUint64(Number(cpu) + KW_CPU_ICOUNT, true)), words };
|
|
}
|
|
|
|
/** The three `unkernel` imports. */
|
|
imports(): Record<string, unknown> {
|
|
return {
|
|
kw_host_jit_caps: (): number => this.caps(),
|
|
kw_host_jit_compile: (ptr: bigint | number, len: bigint | number, slot: number): number => this.compile(ptr, len, slot),
|
|
kw_host_jit_free: (slot: number): void => this.free(slot),
|
|
};
|
|
}
|
|
|
|
caps(): number {
|
|
this.stats.requested = true;
|
|
if (!this.stats.enabled) return 0;
|
|
if (!this.#table) {
|
|
this.#warn("the kernel exports no __indirect_function_table; JIT off");
|
|
return 0;
|
|
}
|
|
return CAP_AVAILABLE | (this.#opts.memory64() ? 0 : CAP_MEM32);
|
|
}
|
|
|
|
compile(ptr: bigint | number, len: bigint | number, slot: number): number {
|
|
const st = this.stats;
|
|
if (st.retained >= this.#nudgeAt) this.#nudge();
|
|
if (st.retained >= this.#limit) {
|
|
// Not a failure: the engine has not caught up with collecting yet.
|
|
if (st.refused++ === 0) this.#opts.log?.(`[jit] ${st.retained} freed modules not collected by the engine yet; refusing compiles until it catches up`);
|
|
return -1;
|
|
}
|
|
const t0 = performance.now();
|
|
try {
|
|
const table = this.#table;
|
|
if (!st.enabled || !table) throw new Error("JIT not enabled");
|
|
if (!this.#validSlot(slot)) throw new Error(`bad slot ${slot}`);
|
|
const p = Number(ptr), n = Number(len);
|
|
this.#buf ??= p;
|
|
const mem = this.#opts.memory();
|
|
if (!(n > 0 && n <= MAX_MODULE && p >= 0 && p + n <= mem.buffer.byteLength)) throw new Error(`bad module range ${p}+${n}`);
|
|
// The Module constructor copies the bytes, so a view into the
|
|
// kernel's memory is enough.
|
|
const module = new WebAssembly.Module(new Uint8Array(mem.buffer, p, n));
|
|
const f = new WebAssembly.Instance(module, { env: { memory: mem } }).exports.f;
|
|
if (typeof f !== "function") throw new Error("the module exports no function f");
|
|
const idx = this.#big ? BigInt(slot) : slot;
|
|
const prev = table.get(idx);
|
|
table.set(idx, f);
|
|
this.#fin?.register(module, 0);
|
|
// A compile into an installed slot (after a second kw_cpu_init,
|
|
// which forgets slots) replaces it and keeps the original stub.
|
|
if (this.#saved.has(slot)) this.#freed();
|
|
else this.#saved.set(slot, prev);
|
|
st.compiled++;
|
|
st.bytes += n;
|
|
return 0;
|
|
} catch (e) {
|
|
st.failed++;
|
|
this.#warn(`compile into slot ${slot} failed: ${(e as Error)?.message ?? e}`);
|
|
return -1;
|
|
} finally {
|
|
st.compileMs += performance.now() - t0;
|
|
st.live = this.#saved.size;
|
|
if (st.live > st.maxLive) st.maxLive = st.live;
|
|
}
|
|
}
|
|
|
|
free(slot: number): void {
|
|
try {
|
|
const orig = this.#saved.get(slot);
|
|
if (!this.#saved.has(slot) || !this.#table) throw new Error(`free of slot ${slot}, which holds no compiled block`);
|
|
this.#table.set(this.#big ? BigInt(slot) : slot, orig);
|
|
this.#saved.delete(slot);
|
|
this.#freed();
|
|
if (this.stats.retained >= this.#nudgeAt) this.#nudge();
|
|
} catch (e) {
|
|
this.stats.failed++;
|
|
this.#warn((e as Error)?.message ?? String(e));
|
|
} finally {
|
|
this.stats.live = this.#saved.size;
|
|
}
|
|
}
|
|
|
|
#freed(): void {
|
|
const st = this.stats;
|
|
st.freed++;
|
|
if (!this.#fin) return;
|
|
st.retained = st.freed - st.collected;
|
|
if (st.retained > st.maxRetained) st.maxRetained = st.retained;
|
|
}
|
|
|
|
#collected(): void {
|
|
const st = this.stats;
|
|
st.collected++;
|
|
// A module replaced without a free is counted as freed, so this
|
|
// never goes below 0; max() guards against the order of callbacks.
|
|
st.retained = Math.max(0, st.freed - st.collected);
|
|
}
|
|
|
|
/** Make the engine count a large allocation towards its next garbage collection. */
|
|
#nudge(): void {
|
|
// Collections are reported asynchronously (finalizers), so judge a
|
|
// size only after a while, never within one burst of compiles.
|
|
const now = performance.now();
|
|
if (this.stats.collected !== this.#nudgeCheckCollected) {
|
|
this.#nudgeCheckCollected = this.stats.collected;
|
|
this.#nudgeCheckAt = now;
|
|
} else if (now - this.#nudgeCheckAt >= NUDGE_PATIENCE_MS) {
|
|
if (this.#nudgeCheckAt > 0 && this.#nudgeBytes < NUDGE_MAX) this.#nudgeBytes *= 2;
|
|
this.#nudgeCheckAt = now;
|
|
}
|
|
try {
|
|
const ab = new ArrayBuffer(this.#nudgeBytes) as Detachable;
|
|
// Detaching frees the backing store now (no mapping waits for
|
|
// the collection); the engine has already counted it.
|
|
ab.transfer?.(0);
|
|
this.stats.gcNudges++;
|
|
} catch {
|
|
// Out of address space (phones): try smaller next time.
|
|
if (this.#nudgeBytes > 1 << 20) this.#nudgeBytes >>= 1;
|
|
}
|
|
}
|
|
|
|
/** An existing, non-null table entry that is not slot 0 (the null function). */
|
|
#validSlot(slot: number): boolean {
|
|
const table = this.#table!;
|
|
if (!Number.isInteger(slot) || slot <= 0 || slot >= Number(table.length)) return false;
|
|
return this.#saved.has(slot) || typeof table.get(this.#big ? BigInt(slot) : slot) === "function";
|
|
}
|
|
|
|
#warn(msg: string): void {
|
|
if (this.#logged >= MAX_LOGGED) return;
|
|
this.#logged++;
|
|
this.#opts.log?.(`[jit] ${msg}${this.#logged === MAX_LOGGED ? " (further JIT messages suppressed)" : ""}`);
|
|
}
|
|
}
|