Skip to content

js: keep one copy of a restored machine's memory in worker mode - #209

Draft
Aditya1404Sal wants to merge 1 commit into
pithings:mainfrom
Aditya1404Sal:pr/worker-one-copy
Draft

Aditya1404Sal wants to merge 1 commit into
pithings:mainfrom
Aditya1404Sal:pr/worker-one-copy

Conversation

@Aditya1404Sal

Copy link
Copy Markdown

What

One commit, in the SDK's worker mode.

  • With transferRestore (WorkerOption), a snapshot memory of 2 GiB or
    more is shared with the worker, not transferred. Now the caller's view
    of it is given up as a transferred buffer would be: snapshot.memory
    becomes an empty Uint8Array.
  • The worker drops its own reference once the kernel's memory holds the
    snapshot (or the boot failed). The Kernel keeps the snapshot object,
    without its memory.
  • So a restored tab keeps one copy of the machine's memory, not two,
    once the worker's engine has collected (How tested).
  • A snapshot now copies the memory first. A copy that cannot be
    allocated refuses the snapshot ("snapshot: the memory could not be
    copied (...)") before any disk is sealed, and the guest goes on.
  • .agents/js.md says both; the transferRestore doc comment says what
    happens to a shared memory.

Why

.agents/js.md:629-633 at b5d1726: "with transferRestore: true
(WorkerOption) the caller's buffer is handed over and detached, for a
snapshot read for one boot (the CLI's --restore, the page); a shared
memory (2 GiB or more) is posted as it is." So for a machine of 2 GiB or
more, the caller's snapshot, the boot spec in the worker and the
Kernel's options all kept the decoded memory for the whole session,
beside the kernel's own memory.

docs/1.sdk/14.snapshots.md:26 at b5d1726 asks the caller to "drop it
once createKernel() has resolved: the restored kernel has its own
copy". In worker mode a caller cannot drop the worker's references.

The copy first: at b5d1726 the disks are sealed (their next writes
start a new generation) before the memory is copied. A memory of 2 GiB
or more is a large allocation in a tab, so it is now made before
anything changes.

This PR does not need the GPU snapshots PR (pr/gpu-snapshot). Both
change the code that takes a snapshot in js/core/kernel.ts, which that
PR splits into #take(). Whichever lands second needs a small rebase
in kernel.ts and in one sentence of .agents/js.md: the copy goes at the
top of #take(). In that form this change applies on that PR with no
conflict.

How tested

All on 2026-12-04, on this branch's code as it is now. After the runs
only one doc sentence and the commit message changed. macOS on an Apple
M5 Pro, AC power, Low Power Mode off; Node 26.0.0. The kernel is
b5d1726's own default bundle (#c7a405545d92). The 1-minute load is
given for each run; none is a timing.

  • pnpm typecheck and pnpm typecheck:web: pass (load 10.4).

  • pnpm test:api with b5d1726's bundle: 169 tests, 156 pass, 10
    skipped, 3 fail. b5d1726 gives the same counts and the same three
    failures (two nodeFs mount tests: macOS refuses file names that are
    not UTF-8; "worker mode rejects device objects": it needs a built
    js/unkernel.wasm) (load 3.58 at the start).

  • The changed test, "worker mode: snapshot() in the worker, resumed
    inline and in a worker (a 2 GiB memory is shared, not detached;
    transferRestore drops the caller's view)": with b5d1726's code it
    fails at the new assertion ("transferRestore gives up the caller's
    view of it, as of a transferred one": 2147483648 !== 0); with this PR
    it passes (load 8.17).

  • Memory on Node, a script (not in this branch) run on b5d1726's code
    and on this branch's, twice each in turn. It boots the base shell in
    worker mode (2 GiB, so the snapshot's memory is shared), writes 512
    MiB to /tmp, takes snapshot({ stop: true }), and restores it in a
    new worker with transferRestore while it keeps the snapshot object,
    as the page or the CLI does. Then the guest writes 256 MiB more. The
    process's resident size (RSS, MiB; gc() on the main thread before
    each sample):

    b5d1726 this PR
    the caller's snapshot.memory after createKernel() 2048 MiB 0 (empty)
    holding only the snapshot 763, 761 758, 758
    after the restore, the guest idle: at 0 s, then at 30 s 1328 to 1316, 1341 to 1328 1323 to 1313, 1323 to 1311
    after the guest wrote 256 MiB more 1582, 1596 1031, 1014
    peak (getrusage) 1582, 1596 1356, 1354

    So, once the guest runs on, the process holds 551 and 582 MiB less
    (the snapshot's copy) and peaks 226 and 242 MiB lower. Right after the
    restore nothing has changed yet: the copy goes only when the worker's
    engine next collects, which an idle guest did not cause within 30 s
    (loads 5.56, 3.64, 9.54 and 5.48 at the starts).

Cost

  • The SDK's dist (the .mjs files): +1,144 bytes (gzip -9 +235), against
    b5d1726's build.
  • The renderer and the kernel bundle: unchanged.

Notes for review

  • A shared buffer cannot be detached. It is freed only once both
    threads have collected their objects for it, and a page cannot make
    the worker's engine collect. So for a while after a restore (or after
    back to back snapshots of such a machine) two copies can still be
    there. .agents/js.md says so.
  • With transferRestore the caller's snapshot is single use for a
    shared memory too, as it already was for a transferred one: its
    memory is empty after createKernel().

A snapshot of 2 GiB or more crosses to the kernel worker by reference
(a SharedArrayBuffer), so the caller's snapshot, the boot spec and the
Kernel's options all kept the decoded memory for the whole session,
beside the kernel's own memory. With transferRestore the caller's view
is now given up as a transferred buffer would be (an empty one), and the
worker drops its reference once the kernel's memory holds the snapshot,
so a restored tab keeps one copy, not two (once the worker's engine
collects).

A snapshot copies the memory first: a copy that cannot be allocated now
refuses the snapshot ("snapshot: the memory could not be copied") before
any disk is sealed, and the guest goes on.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant