Files
unkernel/docs/1.sdk/14.worker.md
pi0x 6234100fc9 docs: fill the SDK documentation gaps and refresh the README's features (#110)
* docs: document snapshots, host commands, Tailscale, VA-API, fetchRoutes, UDP, the microphone and the camera in the SDK docs and README, and list the newer devices and uses in the README's introduction

* docs: split the Display page into Display and Input, and Sound and Camera

---------

Co-authored-by: Pooya Parsa <pooya@pi0.io>
2026-11-30 11:23:00 +01:00

1.4 KiB

icon
icon
ph:cpu-duotone

Workers and SMP

Run the kernel off the main thread, on one or several CPUs.

const vm = await createKernel({
  worker: true,
  distro, // a path or URL
  net: { kind: "net", backend: { kind: "direct" } },
  disks: [{ kind: "file", path: "data.img" }],
});

The kernel runs in a worker thread or Web Worker, with the same API. Disks and the network are given as specs, which the worker builds: { kind: "file", path } on Node, { kind: "http", url } and { kind: "data", template: "bundled" } in browsers. Input and frames go through shared memory when it is available (vm.transport says).

::warning Worker mode is experimental. ::

Several CPUs

An SMP kernel (a bundle with the SMP patch) boots on several CPUs with cpus: 4, each CPU in a thread of its own on one shared memory.

In browsers that needs a cross-origin isolated page and worker: true, because a CPU blocks while it waits for another, which a page's main thread may not. webCpuBlocker() says why it cannot, and vm.stats().cpus how many CPUs the kernel got.

On a server, worker: true keeps the event loop free at no cost in speed, and cpus: 4 gets most of what more CPUs give (about 3x on parallel work; 8 or 16 add little, and each CPU adds to boot time and takes 9 MiB of guest memory). node test/profile.ts measures your host, and the page's /profile.html a browser.