From dea732ed8d5f2ab49de538b46d5eb9b517a1be64 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Sat, 28 Nov 2026 20:31:04 +0000 Subject: [PATCH] js: warn when --cpus asks for more CPUs than the host has threads The CLI boots any count from 1 to 32, given by --cpus or taken from a snapshot. Above os.availableParallelism() the CPU threads take turns, and a CPU that waits for another (an IPI, a TLB shootdown) waits until the host schedules that one. It still boots, now with a warning on stderr. The --help text and js.md say so. Co-authored-by: xfy --- .agents/js.md | 5 +++-- js/cli.ts | 12 +++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/.agents/js.md b/.agents/js.md index e34095a..a94fa09 100644 --- a/.agents/js.md +++ b/.agents/js.md @@ -549,8 +549,9 @@ parts that are easy to get wrong. - An SMP kernel (kernel/smp.md) imports `wasm_init_flag`, `wasm_cpu_count` and `wasm_cpu_start`. `CreateKernelOptions.cpus` (CLI - `--cpus N`, which implies `--worker`; `WorkerBootSpec.cpus`) sets how many - CPUs `Kernel` offers. A UP kernel ignores it with a log line, so does a + `--cpus N`, which implies `--worker` and warns above + `os.availableParallelism()`; `WorkerBootSpec.cpus`) sets how many CPUs + `Kernel` offers. A UP kernel ignores it with a log line, so does a platform without `Platform.spawnCpu` or one whose `Platform.whyNoCpus()` gives a reason for the kernel's thread (browsers: below); `stats().cpus` says how many it got. The memory is shared (the diff --git a/js/cli.ts b/js/cli.ts index f527c45..fdf944b 100644 --- a/js/cli.ts +++ b/js/cli.ts @@ -8,6 +8,7 @@ import { existsSync, readFileSync, statSync, writeFileSync } from "node:fs"; import * as nodeModule from "node:module"; import { createServer, type Server, type Socket } from "node:net"; +import { availableParallelism } from "node:os"; import { fileURLToPath } from "node:url"; import path from "node:path"; import type { BlockDevice } from "./core/block.ts"; @@ -140,7 +141,9 @@ everything else is messages either way. N CPUs, each another instance of the kernel in its own thread on a shared memory, each with its own JIT; it implies --worker, and --stats prints the JIT counters per CPU. A UP kernel runs on one CPU (a log line says so). An SMP kernel has every device -(their host side and their I/O stay with CPU 0) and snapshots. +(their host side and their I/O stay with CPU 0) and snapshots. More CPUs than +the host has threads (os.availableParallelism()) boot with a warning: the CPUs +take turns, and waits for another CPU (IPIs, TLB shootdowns) get slow. --display gives the guest a display: /dev/fb0 (kwfb, 32 bpp XRGB8888), whose memory exists while a program has it open; 640x480, or --display=WxH (the @@ -298,6 +301,13 @@ if (!Number.isInteger(smpCpus) || smpCpus < 1 || smpCpus > 32) { process.stderr.write("[host] --cpus N: 1 to 32 CPUs\n"); process.exit(1); } +// More CPUs than host threads (--cpus, or a snapshot's count) still boots, but +// the CPU threads take turns, and a CPU that waits for another (an IPI, a TLB +// shootdown) waits until the host runs that one. +const hostThreads = availableParallelism(); +if (smpCpus > hostThreads) { + process.stderr.write(`[host] warning: ${smpCpus} CPUs on ${hostThreads} host CPU thread${hostThreads === 1 ? "" : "s"} (os.availableParallelism()): the CPUs take turns, and waits for another CPU (IPIs, TLB shootdowns) get slow\n`); +} // SMP: every CPU in a worker, CPU 0 in the kernel worker. const worker = !!workerArg || smpCpus > 1; const workerTransport = workerArg?.slice(9);