Repository navigation
Description
With cpus: 2, the first vm.spawn() returns about 350 ms later than with cpus: 1 (Node 26, bundled kernel, a NIC attached). Two separate costs make up almost all of it:
- About 150 ms in
createKernel(): deriving the SMP kernel from the bundle (smpKernelFromBundle/applyKernelPatch). - About 190 ms in the guest: the boot-time
udhcpcmakes sixsynchronize_net()calls. Each waits for a normal RCU grace period of 12 to 42 ms once a second CPU is online.kwtty servestarts after/usr/lib/kw/net wait, so the first spawn waits for all six.
Secondary CPU bring-up is a third, smaller cost: 30 ms for one extra CPU, 72 ms for three.
Versions
- unkernel npm
0.2.0-261125-cec0a49, its bundled kernel (Linux 7.3.4+, HZ=100). - Node 26.13.1 and headless Chromium 158.0.8127.36, Linux x86_64, Ryzen 7 7840U.
- The Chromium numbers come from tabc, which calls
createKernel()inline inside its own Web Worker. I did not runworker: true.
Reproduction (Node, no browser)
npm i [email protected]
node smp-start.mjs
smp-start.mjs
// Time from createKernel() to the first program's output, by CPU count.
// Run: npm i [email protected] && node smp-start.mjs
import { createKernel } from "unkernel";
// A backend that connects nowhere. The SDK's own stack answers DHCP, and
// a NIC is all the guest's start-up needs to run udhcpc.
const net = {
connect() {
throw new Error("no network");
},
lookup() {
return Promise.reject(Object.assign(new Error("no network"), { code: "ENOTFOUND" }));
},
};
async function run(label, opts) {
const marks = {};
const t0 = performance.now();
const vm = await createKernel({
net,
log: () => {},
onOutput: () => {},
onProgress: (p) => {
if (p.stage === "compiled" || p.stage === "start") {
marks[p.stage] = performance.now() - t0;
}
},
...opts,
});
const created = performance.now() - t0;
const program = await vm.spawn("/bin/cat", ["/proc/uptime"], { pty: false });
const spawned = performance.now() - t0;
let out = "";
for await (const chunk of program.stdout) {
out += new TextDecoder().decode(chunk);
}
await program.ended;
const ms = (n) => n.toFixed(0).padStart(5);
console.log(
`${label.padEnd(26)} compiled ${ms(marks.compiled)} kernel start ${ms(marks.start)} createKernel ${ms(created)} ` +
`spawn returned ${ms(spawned)} guest uptime ${out.split(" ")[0]} s`,
);
await vm.shutdown?.().catch(() => {});
vm.terminate?.();
}
const EXPEDITED = "console=hvc0 rdinit=/init rcupdate.rcu_expedited=1";
for (let i = 0; i < 3; i++) {
await run("1 CPU", { cpus: 1 });
await run("2 CPUs", { cpus: 2 });
await run("2 CPUs, expedited RCU", { cpus: 2, cmdline: EXPEDITED });
await run("2 CPUs, no NIC", { cpus: 2, net: undefined });
}
process.exit(0);Output, milliseconds since createKernel() was called, three rounds:
1 CPU compiled 25 kernel start 46 createKernel 78 spawn returned 212 guest uptime 0.13 s
2 CPUs compiled 150 kernel start 167 createKernel 194 spawn returned 559 guest uptime 0.35 s
2 CPUs, expedited RCU compiled 146 kernel start 167 createKernel 183 spawn returned 327 guest uptime 0.14 s
2 CPUs, no NIC compiled 150 kernel start 172 createKernel 187 spawn returned 328 guest uptime 0.13 s
1 CPU compiled 16 kernel start 37 createKernel 51 spawn returned 162 guest uptime 0.10 s
2 CPUs compiled 135 kernel start 159 createKernel 173 spawn returned 521 guest uptime 0.34 s
2 CPUs, expedited RCU compiled 139 kernel start 166 createKernel 182 spawn returned 325 guest uptime 0.13 s
2 CPUs, no NIC compiled 155 kernel start 186 createKernel 202 spawn returned 346 guest uptime 0.13 s
1 CPU compiled 14 kernel start 42 createKernel 59 spawn returned 170 guest uptime 0.10 s
2 CPUs compiled 157 kernel start 189 createKernel 206 spawn returned 553 guest uptime 0.33 s
2 CPUs, expedited RCU compiled 171 kernel start 210 createKernel 227 spawn returned 375 guest uptime 0.13 s
2 CPUs, no NIC compiled 167 kernel start 202 createKernel 220 spawn returned 371 guest uptime 0.14 s
"Expedited RCU" is cmdline: "console=hvc0 rdinit=/init rcupdate.rcu_expedited=1". "No NIC" omits net, so the guest has no eth0 and runs no udhcpc.
Cost 1: deriving the SMP kernel, about 150 ms per boot
loadKernelModule with smp: true reads the whole bundle, runs smpKernelFromBundle, then calls WebAssembly.compile. The UP path uses compileStreaming. I added timestamps inside loadKernelModule in Chromium:
| step | time |
|---|---|
| fetch the 11.3 MB bundle into memory | 21 ms |
smpKernelFromBundle (output 10.7 MB) |
149 to 171 ms (medians of five batches of 6 to 8 boots) |
WebAssembly.compile of the result |
18 ms |
| whole "compile" stage, 2 CPUs | 183 to 210 ms |
whole "compile" stage, 1 CPU (compileStreaming) |
32 to 36 ms |
With { verify: false } the patch step took 151 ms against 171 ms in the same batch (8 boots each), so the two SHA-256 passes are about 20 ms of it. The rest is the patch itself. In Node, five calls in one process took 140, 108, 117, 104 and 97 ms, so a warm JIT does not remove it. I did not split the remainder between applyRemap and the add/copy loop.
createKernel() in total: 80 to 92 ms on one CPU, 227 to 264 ms on two (medians per batch). Forcing the SMP kernel onto one CPU gives 238 to 264 ms, so the second CPU thread is not what costs here.
Deriving the SMP kernel once ahead of time removes the cost. I wrote the output of smpKernelFromBundle to a file, served it, and passed kernel: await WebAssembly.compileStreaming(fetch(url)). createKernel({ cpus: 2 }) then took 79 ms (median of 8), the same as the UP kernel.
Possible fixes: publish the derived SMP kernel as a second file next to unkernel.wasm, or export a supported way to derive it at build time (today smpKernelFromBundle is only reachable through _chunks/core.mjs).
Cost 2: RCU grace periods during the boot-time DHCP, about 190 ms
Chromium, time from the kernel's "start" stage to the first vm.spawn() returning. Median of 8 boots, range in brackets. For these runs I lowered the spawn connect retry (RETRY_MS in tty.ts) from 100 ms to 2 ms so that the result is not rounded up to the next retry.
| configuration | start to first spawn |
|---|---|
| UP kernel, 1 CPU | 187 ms [179-198] |
SMP kernel, cpus: 2, maxcpus=1 |
196 ms [189-217] |
| SMP kernel, 2 CPUs | 416 ms [393-447] |
SMP kernel, 2 CPUs, isolcpus=1 |
426 ms [404-615] |
SMP kernel, 2 CPUs, rcupdate.rcu_expedited=1 |
229 ms [209-394] |
| SMP kernel, 4 CPUs | 506 ms [473-574] |
SMP kernel, 4 CPUs, rcupdate.rcu_expedited=1 |
290 ms [256-358] |
The SMP kernel with one CPU online costs 9 ms. A second online CPU costs 220 ms more, and it costs the same when no task may run on that CPU (isolcpus=1). Expedited RCU removes 187 ms of the 220.
strace -f -T of udhcpc -i eth0 -q -n -t 1 -T 1 in a 2-CPU guest, three runs. Every call slower than 4 ms:
0.031280 bind(5, {sa_family=AF_PACKET, ...
0.038204 bind(6, {sa_family=AF_PACKET, ...
0.038340 close(6)
0.017350 bind(6, {sa_family=AF_PACKET, ...
0.039239 close(6)
0.017444 close(5)
total 0.182 s (the other two runs: 0.190 s and 0.175 s, the same six calls)
In a 1-CPU guest no call in the same trace takes 4 ms. packet_do_bind() and packet_release() call synchronize_net(). With one CPU online synchronize_rcu() returns at once. With two it waits for a grace period, and at HZ=100 with jiffies_till_first_fqs=1 that is 1 to 4 ticks.
The same udhcpc command, timed in a loop of 10 in one 2-CPU guest: 200 ms per run, and 6 ms per run after echo 1 > /sys/kernel/rcu_expedited. On a 1-CPU guest: 5 ms.
The first spawn waits for this because the base inittab starts the terminal server with /bin/sh -c "/usr/lib/kw/net wait 2>/dev/null; exec /usr/bin/kwbox tty serve".
Possible fixes, none of which I tested beyond the boot parameter:
- Boot the SMP kernel with expedited grace periods, at least until user space is up (
rcupdate.rcu_expedited=1, or the kernel's own boot-time expediting kept on until init finishes). I did not measure what expedited grace periods cost a busy guest later. - Do not hold
kwtty serveback for DHCP. A program that needs the network can runnet waititself.
Secondary CPU bring-up
From dmesg, "smp: Bringing up secondary CPUs" to "Brought up": 30 ms with 2 CPUs (0.0169 to 0.0464) and 72 ms with 4 (0.0176 to 0.0893). One boot each. I did not look into what the 24 to 30 ms per CPU is.
What I ruled out
- JIT:
stats().jitafter the first program shows 2 modules compiled and 3 ms of compile time on both kernels. - Tasks running on the second CPU:
isolcpus=1changes nothing. - Steady-state speed: after boot, fork, exec, tmpfs/proc/sysfs/overlay mounts and
ip linkup/down took the same time on 1 and 2 CPUs within 2 ms per operation (loops of 10, timed with /proc/uptime, one guest each). The exception was a cgroup2 mount and umount at 8 ms against 3 ms.
Not measured
worker: true.- Firefox and Safari.
- The console ring and IPI latency on their own. The 9 ms difference between the UP kernel and the SMP kernel held to one CPU bounds everything that does not need a second online CPU.
Activity
Boot the guest with expedited RCU grace periods
Boot unkernel on several CPUs 0.4 s sooner