Files
unkernel/js/web/web-worker-entry.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

27 lines
1.4 KiB
TypeScript

// SPDX-License-Identifier: MIT
// Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>
// The kernel worker's entry in browsers (started by worker.ts): serve the
// kernel (core/worker-serve.ts) over the worker's own message port, with
// the browser device kinds (devices.ts: the IndexedDB data disk, HTTP
// disks, Wisp), starting an SMP kernel's other CPUs as nested workers
// (cpu.ts). Every
// URL it gets is absolute (in the package it runs from a Blob URL, whose
// base means nothing), and it finds nothing through import.meta.url. The
// build bundles it into a module that imports nothing.
import { serveKernel } from "../core/worker-serve.ts";
import type { ToWorker } from "../core/worker-protocol.ts";
import { spawnWebCpu, webCpuBlock } from "./cpu.ts";
import { webDevices } from "./devices.ts";
import { openWebUsbByKey } from "./usb.ts";
/** The dedicated worker's global (the page's tsconfig has no webworker lib). */
const scope = globalThis as unknown as {
postMessage(msg: unknown, transfer: Transferable[]): void;
addEventListener(type: "message", fn: (e: MessageEvent<ToWorker>) => void): void;
};
serveKernel({
post: (msg, transfer) => scope.postMessage(msg, transfer ?? []),
listen: (handler) => scope.addEventListener("message", (e) => handler(e.data)),
}, { devices: webDevices, spawnCpu: spawnWebCpu, whyNoCpus: () => webCpuBlock(), openUsb: openWebUsbByKey });