Skip to content

perf(vm): stop repeating per-launch overlay writes and hashes before the VM starts - #6056

Open
balajinvda wants to merge 3 commits into
ai-openshell:mainfrom
balajinvda:perf/6054-vm-launch-bookkeeping/balajinvda
Open

balajinvda wants to merge 3 commits into
ai-openshell:mainfrom
balajinvda:perf/6054-vm-launch-bookkeeping/balajinvda

Conversation

@balajinvda

Copy link
Copy Markdown
Contributor

Summary

Removes three pieces of per-launch bookkeeping the VM driver repeats on every sandbox create with an unchanged result: one debugfs process per overlay command, hashing the embedded guest binaries for the image cache key, and hashing the ~50 MB cached host supervisor. One commit per change; behaviour is unchanged apart from timing.

Related Issue

Closes #6054

Changes

  • perf(vm): write a launch's overlay files in one debugfs run: RootfsImageBatch (in rootfs.rs) stages the same mkdir / rm / write / set_inode_field commands in the same order and runs them through the existing run_debugfs_batch (debugfs -w -f): one run for the guest overlay files, one for the boundary bundle, one for the generation cleanup. debugfs reports a failing command on stderr and exits 0 in both modes, so the disk contents and the success or failure are the same as with separate runs. A path debugfs would read as two lines is refused when staged, before anything is written; staged temp files are removed when the batch runs or is dropped. write_rootfs_image_file keeps its signature (used by runtime.rs) and becomes a one-file batch; run_debugfs is now test-only. The vm.prepare_guest span moves to a new prepare_guest_overlay and so also covers the mount manifest and the write.
  • perf(vm): hash the guest runtime identity at build time: build.rs hashes exactly the bytes the crate embeds (from OUT_DIR, plus the init script, which it now watches) and sandbox_guest_runtime_identity() returns that constant. Same value, so existing cached root disks stay valid. Adds sha2 as a build dependency (already a dependency; Cargo.lock unchanged).
  • perf(vm): hash the cached host supervisor once per file version: validate_host_supervisor remembers the last file it hashed and found good, keyed by path, dev, inode, size, mode, mtime, ctime and the embedded digest. Any write, chmod, chown, rename or replacement changes ctime or inode and is hashed again. Because ctime comes from a coarse clock, a file is remembered only once its ctime is at least 1 s old (git's "racily clean" rule), and only if its stamp did not change while it was hashed. A refused file is never remembered; a new driver process hashes again.
  • Measured on an x86-64 KVM host without SHA instructions with these three commits and VM driver starts an image preparation worker on every create, even for a cached digest-pinned image #6055's change (p50, ten re-creates of a cached digest-pinned image): image resolution 114 → 1–2 ms, create_sandbox received → launcher spawned 367 → 41–67 ms; the overlay writes take 13–27 ms of that. These measurements were taken on the v0.1.7 driver with the same changes; the code paths are unchanged on main, which this branch is based on (659b45f3b). Per-item numbers and which are inferred are in VM driver repeats per-launch bookkeeping before the VM starts: a debugfs process per overlay command, guest identity and host supervisor re-hashed #6054.

Testing

  • Checks appropriate to the affected code and behavior pass: cargo fmt -p openshell-driver-vm -- --check, cargo clippy -p openshell-driver-vm --all-targets -- -D warnings, cargo test -p openshell-driver-vm (lib: 305 passed), cargo check -p openshell-driver-vm --no-default-features
  • Unit tests added/updated (if applicable):
    • rootfs_image_batch_writes_what_one_debugfs_per_command_wrote: applies the launch's shape of writes, modes and removals (a replaced file, a new nested directory, a path with a space, harmless failures on a missing file) one debugfs -R per command to one copy of an image and as one batch to another, then compares every entry's mode, owner, size and contents; checks the staged files are gone.
    • rootfs_image_batch_refuses_a_path_debugfs_reads_as_two_lines: newline/CR paths are refused at staging, only valid commands are staged, a dropped batch removes its staged files.
    • guest_runtime_identity_built_once_matches_the_embedded_bytes: the build-time value equals the hash of the embedded bytes computed at run time.
    • host_supervisor_validated_once_per_file_version: counts hashes across a just-written file (not remembered), a settled file (hashed once), a same-size rewrite in place, replacement by rename, a chmod that drops the execute bit, another embedded digest and a fresh process.
    • post_overlay_provisioning_stages_record_child_spans updated for prepare_guest_overlay.
  • E2E tests added/updated (if applicable)

The unit tests ran on Linux x86-64 with e2fsprogs installed; the build used stub embedded runtime artifacts (OPENSHELL_VM_RUNTIME_COMPRESSED_DIR unset). The same three changes, on the v0.1.7 driver, booted VM sandboxes from a cached digest-pinned image on an x86-64 KVM host, and the unit tests above (in their v0.1.7 form) also passed on arm64.

Checklist

  • Follows Conventional Commits
  • Commits are signed off (DCO)
  • Architecture docs updated (if applicable)

A sandbox launch writes its per-sandbox files into the overlay ext4 (the
init drop-ins and their manifest, the mount manifest, then the boundary
configuration, certificate and key) with one `debugfs -w -R` process per
command: a mkdir per parent directory, rm and write per file,
set_inode_field per mode. That is about thirty debugfs processes per
launch. Measured with the same debugfs, overlay template and commands on
an x86-64 workstation: 152 to 183 ms as separate processes, 7 to 9 ms as
one `debugfs -f` run.

RootfsImageBatch stages the same commands in the same order and runs
them as one `debugfs -w -f` process (the existing run_debugfs_batch): one
run for the guest overlay files, one for the boundary bundle (the
boundary provisioning sits between the two), and one for the three
removals of a runtime generation's material. debugfs reports a failing
command (an existing directory, a missing file) on stderr and exits 0 in
both modes, so a batch leaves the same disk contents and the same
success or failure as the separate runs. A path debugfs would read as
two lines is refused when it is staged, before anything is written, and
staged contents are removed when the batch has run or is dropped.

The vm.prepare_guest span moves to prepare_guest_overlay, which stages
the drop-ins and the mount manifest and writes them, so it now covers
the mount manifest and the write itself.

Test: rootfs_image_batch_writes_what_one_debugfs_per_command_wrote applies
the launch's shape of writes, modes and removals (including the harmless
failures) both ways to copies of one image and compares every entry's
mode, owner, size and contents; rootfs_image_batch_refuses_a_path_debugfs_reads_as_two_lines
covers the refusal and the cleanup of a dropped batch.

Closes ai-openshell#6054

Signed-off-by: Balaji Ganesan <bganesan@nvidia.com>
Every bootstrap root disk's image cache key includes
sandbox_guest_runtime_identity(), a SHA-256 over the embedded sandbox,
vm-init and umoci blobs and the guest init script. It was computed on
every call, and every launch resolves its root disk in a fresh image
preparation worker process, so every launch hashed the embedded blobs
again, even on a cache hit. On an x86-64 host without SHA instructions
most of the 90 ms between the worker's "ensuring cached root disk image"
and its cache hit is this hash (inferred from the driver log gap, not
profiled).

build.rs now hashes exactly the files the crate embeds (from OUT_DIR,
plus the init script, which build.rs now watches) and the function
returns that constant. The value is unchanged, so cached root disks stay
valid.

Test: guest_runtime_identity_built_once_matches_the_embedded_bytes holds
the build-time value equal to the hash of the embedded bytes computed at
run time, as before.

Closes ai-openshell#6054

Signed-off-by: Balaji Ganesan <bganesan@nvidia.com>
Every launch validates the cached host supervisor in host_supervisor_binary
by hashing all of it (about 50 MB) on an async runtime thread. On an
x86-64 host without SHA instructions the driver log shows about 280 ms
between "launcher spawned" and the host supervisor's start, consistent
with a software SHA-256 of that file (inferred, not profiled). It runs
while the guest boots, so it is mostly hidden today, but it becomes the
floor of a launch once the guest boots faster, and it holds a runtime
thread meanwhile.

A file this driver process hashed and found good is not hashed again
while its device, inode, size, mode, mtime and ctime are unchanged. Any
write, chmod, chown, rename or replacement changes the ctime (set by the
kernel; no caller can set it back) or the inode, and the file is hashed
again. The kernel stamps ctime from a coarse clock, so a rewrite in the
same tick as the file's last change can keep its ctime: only a file
whose ctime is at least 1 s old is remembered (git's "racily clean"
rule), so any later change gets a later ctime. Only a file unchanged
across its own hash is remembered, a refused file never is, the memory
is per embedded supervisor digest, and a new driver process hashes again
on its first launch.

Test: host_supervisor_validated_once_per_file_version counts the hashes
across a just-written file (not remembered), a settled file (hashed
once), a same-size rewrite in place, a replacement by rename, a chmod
that drops the execute bit, another embedded digest and a fresh process.

Closes ai-openshell#6054

Signed-off-by: Balaji Ganesan <bganesan@nvidia.com>

This branch has not been deployed

No deployments
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.

VM driver repeats per-launch bookkeeping before the VM starts: a debugfs process per overlay command, guest identity and host supervisor re-hashed

1 participant