Repository navigation
js: keep one copy of a restored machine's memory in worker mode - #209
Draft
Aditya1404Sal wants to merge 1 commit into
Draft
Aditya1404Sal wants to merge 1 commit into
Aditya1404Sal wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
One commit, in the SDK's worker mode.
transferRestore(WorkerOption), a snapshot memory of 2 GiB ormore is shared with the worker, not transferred. Now the caller's view
of it is given up as a transferred buffer would be:
snapshot.memorybecomes an empty
Uint8Array.snapshot (or the boot failed). The
Kernelkeeps the snapshot object,without its memory.
once the worker's engine has collected (How tested).
allocated refuses the snapshot ("snapshot: the memory could not be
copied (...)") before any disk is sealed, and the guest goes on.
transferRestoredoc comment says whathappens 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 sharedmemory (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 owncopy". 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 rebasein 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 noconflict.
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 isgiven for each run; none is a timing.
pnpm typecheckandpnpm typecheck:web: pass (load 10.4).pnpm test:apiwith b5d1726's bundle: 169 tests, 156 pass, 10skipped, 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 anew worker with
transferRestorewhile 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 beforeeach sample):
snapshot.memoryaftercreateKernel()getrusage)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
b5d1726's build.
Notes for review
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.
transferRestorethe caller's snapshot is single use for ashared memory too, as it already was for a transferred one: its
memoryis empty aftercreateKernel().