Files
unkernel/js/web/platform.ts
pi0x 55b582b887 js: report the CPUs the guest runs on, and why it got fewer than asked (#159)
stats().cpus said what createKernel() was asked for, not what the guest
got: cpus above the kernel's NR_CPUS (16) left the guest on 16 while
stats().cpus and snapshots said more. A kernel without SMP, or a thread
that cannot start CPU threads, booted on one CPU with only a log line.

The host now offers the SMP kernel at most MAX_CPUS (exported), so
stats().cpus and the snapshot's count are what the guest runs on;
snapshotCpuMismatch() compares counts as the kernel takes them. When the
guest gets fewer CPUs than asked, stats().cpuLimit is { requested, reason }
("max", "module-not-smp", "kernel-not-smp", "not-isolated",
"not-worker", "unsupported"), in worker mode too. createKernel() still
boots, and the log line stays.

Co-authored-by: Pooya Parsa <pooya@pi0.io>
2026-12-02 15:12:07 +01:00

45 lines
2.3 KiB
TypeScript

// SPDX-License-Identifier: MIT
// Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>
// createKernel()'s browser platform: memory sized for the device (decided
// here, on the page's thread, since a worker has no say in it), worker mode
// on a Web Worker (worker.ts), and relative locations made absolute against
// the page, because the worker (a Blob URL in the package) has no useful
// base URL. Disk and network specs are built by webDevices (devices.ts).
// CPU threads for an SMP kernel (cpu.ts) start wherever the kernel runs in
// a worker of a cross-origin isolated page: worker mode's kernel worker,
// or a worker of the caller's own; never on the page's main thread
// (webCpuBlocker()).
// The entry (web.ts, or the page's web/src/sdk.ts) passes the
// default kernel's URL, the GL renderer's and the Vulkan translator's (web.ts only), so
// `new URL(..., import.meta.url)` stays in it.
import type { GlModuleSource } from "../core/gl.ts";
import type { Platform } from "../core/instance.ts";
import type { VkPlatform } from "../core/vk.ts";
import { DEFAULT_MEMORY_FALLBACK_MIB, autoMemoryMiB } from "./memory.ts";
import { spawnWebCpu, webCpuBlock } from "./cpu.ts";
import { webDevices } from "./devices.ts";
import { loadTailscale } from "../core/tailscale-load.ts";
import { spawnWebWorker } from "./worker.ts";
export function webPlatform(defaultKernel: URL, gl?: GlModuleSource, vk?: VkPlatform): Platform {
return {
defaultKernel: () => defaultKernel,
// The `gl` option's renderer (virgl.mjs and virgl.wasm), loaded only when asked for.
glModule: gl && (() => gl),
// The `vk` option's renderer and its translator (kwvk-naga.wasm), loaded only at the guest's first Vulkan instance.
vk: vk && (() => vk),
defaultMemoryMiB: () => autoMemoryMiB().mib,
defaultMemoryFallbackMiB: [...DEFAULT_MEMORY_FALLBACK_MIB],
spawnWorker: spawnWebWorker,
spawnCpu: spawnWebCpu,
whyNoCpus: () => webCpuBlock(),
absoluteUrl: (src) => new URL(src, location.href).href,
devices: webDevices,
// A distro URL (relative to the page) as a read-only "http" spec: read lazily with Range requests, no IndexedDB.
distroSpec: (src) => ({ kind: "http", url: new URL(src, location.href).href, readOnly: true }),
// net.tailscale: the client is a chunk of its own, imported only then.
tailscale: loadTailscale,
};
}