Skip to content

Add explicit snapshot compaction with optional base-layer preservation - #2097

Open
jprendes wants to merge 2 commits into
hyperlight-dev:mainfrom
jprendes:feat/snapshot-compaction
Open

jprendes wants to merge 2 commits into
hyperlight-dev:mainfrom
jprendes:feat/snapshot-compaction

Conversation

@jprendes

Copy link
Copy Markdown
Contributor

Motivation

Repeated snapshot capture retains immutable memory layers and can eventually reach the layer limit. Give callers an explicit way to bound live snapshot memory without depending on intermediate checkpoints or changing the persistence format.

Changes

  • Add Snapshot::compact(&self, base: Option<&Snapshot>) -> Result<Arc<Self>>.
  • Without a base, pack live pages into a single memory layer. With a base, retain referenced identical base layers and pack remaining live pages into at most one new layer.
  • Match separately loaded base layers by GPA, geometry, and complete serialized blob contents; reject incompatible memory layouts.
  • Preserve page-table roots/topology, aliases, permissions, execution state, generation, metadata, transport state, and cached hashes of retained layers. Leave input snapshots unchanged.
  • Keep compaction opt-in and the OCI format unchanged. Saved snapshots remain self-contained; sharing or omitting base blobs during transport remains the caller’s responsibility.
  • Document the API and extend the existing multiple-page-table-root and persistence tests.

Validation

  • 178 focused snapshot compaction/persistence tests passed:
    cargo +1.99.1 test --offline --locked -p hyperlight-host --lib -- compact sandbox::snapshot::file_tests --test-threads=1
  • Multiple-page-table-root capture/restore regression passed:
    cargo +1.99.1 test --offline --locked -p hyperlight-host --lib capture_and_restore_preserve_multiple_page_table_roots
  • Strict host Clippy passed:
    cargo +1.99.1 clippy --offline --locked -p hyperlight-host --lib --tests -- -D warnings
  • Workspace formatting and whitespace checks passed.
  • The round-trip test exercises 35 capture/compact/restore cycles for both full and base-preserving compaction, then saves, checked-loads, and resumes execution. It also verifies retained hash-cache reuse.

Pack live snapshot pages into one memory layer, optionally retaining identical immutable base layers. Preserve execution state, page-table topology, metadata, transport state, and cached hashes without changing the OCI snapshot format.

Signed-off-by: Jorge Prendes <jorge.prendes@gmail.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Snapshot compaction breaks debug x86_64 builds with the gdb feature, and compact(None) can produce saved snapshots that both loaders reject when packed live data is smaller than the code region.

2 open issues
What changed in this PR

This PR adds opt-in snapshot compaction to Hyperlight’s host library, helping callers bound retained memory layers without changing the OCI persistence format.

Changes:

  • Add Snapshot::compact with optional preservation of identical base layers.
  • Preserve snapshot state and document compaction and persistence behavior.
  • Extend tests for aliases, permissions, multiple page-table roots, and repeated capture/compact/restore cycles.
File Description
src/​hyperlight_host/​src/​sandbox/​snapshot/​mod.rs Register compaction, enable state cloning, and extend multi-root tests.
src/​hyperlight_host/​src/​sandbox/​snapshot/​file_tests.rs Test persistence, execution continuity, and retained hash-cache reuse.
src/​hyperlight_host/​src/​sandbox/​snapshot/​compact.rs Implement compaction, base-layer matching, and focused tests.
src/​hyperlight_host/​src/​mem/​virtq/​mod.rs Enable cloning of captured transport state.
src/​hyperlight_host/​src/​mem/​layout.rs Enable layout equality checks for base compatibility.
docs/​snapshot-oci-format.md Document compaction modes and self-contained persistence.

Review effort: Balanced

@jprendes jprendes added the C-enhancement For PRs adding features, improving functionality, docs, tests, etc. label Nov 24, 2026
Keep the loader code-span invariant through packed-layer placement and minimal padding when all live pages reside in preserved base layers. Copy from the backing-independent slice accessor and cover sparse persistence plus debug gdb compaction.

Signed-off-by: Jorge Prendes <jorge.prendes@gmail.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@hyperlight-gh-bot

Copy link
Copy Markdown

Benchmark Results

Measured commit: 70771a221006
Baseline commit: fa1e797df82a

kvm / amd (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 320.32 ns (➖ 1.04x slower)
vec_bytes 97.36 ns (➖ 1.03x slower)
373.16 µs (➖ 1.00x slower)

payload_allocation

slot_pool_segmented
65536 164.58 ns (➖ 1.03x faster)
262144 601.48 ns (➖ 1.05x faster)

sandboxes

create_initialized_and_drop
medium 13.80 ms (➖ 1.03x faster)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
9.18 ns (➖ 1.01x faster) 9.15 ns (➖ 1.00x faster) 9.16 ns (➖ 1.00x slower)

snapshot_files

load_snapshot_unverified
small 135.23 µs (➖ 1.02x faster)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.29 µs (➖ 1.08x faster) 2.43 µs (➖ 1.09x slower)
262144 8.43 µs (➖ 1.01x slower) 8.87 µs (➖ 1.07x slower)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 6.77 µs (➖ 1.19x faster) 6.66 µs (➖ 1.16x faster)
262144 28.71 µs (➖ 1.13x faster)
8192 1.17 µs (➖ 1.03x faster) 1.17 µs (➖ 1.04x faster)
kvm / intel (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 294.70 ns (➖ 1.00x slower)
vec_bytes 98.54 ns (➖ 1.07x slower)
767.48 µs (➖ 1.01x slower)

payload_allocation

slot_pool_segmented
65536 205.76 ns (➖ 1.00x slower)
262144 588.37 ns (➖ 1.00x faster)

sandboxes

create_initialized_and_drop
medium 15.23 ms (➖ 1.03x faster)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
8.13 ns (➖ 1.01x slower) 8.07 ns (➖ 1.01x faster) 8.09 ns (➖ 1.00x faster)

snapshot_files

load_snapshot_unverified
small 73.42 µs (➖ 1.12x slower)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.50 µs (➖ 1.01x faster) 2.51 µs (➖ 1.01x faster)
262144 8.94 µs (➖ 1.00x faster) 9.00 µs (➖ 1.01x slower)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 8.28 µs (➖ 1.02x faster) 8.36 µs (➖ 1.00x faster)
262144 34.86 µs (➖ 1.03x faster)
8192 851.71 ns (➖ 1.03x slower) 850.98 ns (➖ 1.02x faster)
mshv3 / amd (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 337.54 ns (➖ 1.12x faster)
vec_bytes 104.17 ns (➖ 1.00x slower)
314.27 µs (➖ 1.00x faster)

payload_allocation

slot_pool_segmented
65536 182.01 ns (➖ 1.01x faster)
262144 675.43 ns (➖ 1.00x slower)

sandboxes

create_initialized_and_drop
medium 62.88 ms (➖ 1.00x faster)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
9.87 ns (➖ 1.00x faster) 9.90 ns (➖ 1.01x faster) 9.87 ns (➖ 1.00x faster)

snapshot_files

load_snapshot_unverified
small 107.73 µs (➖ 1.01x faster)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.45 µs (➖ 1.00x faster) 2.61 µs (➖ 1.01x faster)
262144 9.09 µs (➖ 1.01x slower) 9.69 µs (➖ 1.05x slower)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 8.76 µs (➖ 1.09x slower) 8.11 µs (➖ 1.00x slower)
262144 34.97 µs (➖ 1.01x faster)
8192 1.28 µs (➖ 1.01x faster) 1.28 µs (➖ 1.02x faster)
mshv3 / intel (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 347.52 ns (➖ 1.05x faster)
vec_bytes 85.42 ns (➖ 1.06x faster)
697.96 µs (➖ 1.00x slower)

payload_allocation

slot_pool_segmented
65536 269.90 ns (➖ 1.01x faster)
262144 714.42 ns (➖ 1.06x slower)

sandboxes

create_initialized_and_drop
medium 73.24 ms (➖ 1.03x faster)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
8.70 ns (➖ 1.01x faster) 8.72 ns (➖ 1.00x faster) 8.87 ns (➖ 1.02x slower)

snapshot_files

load_snapshot_unverified
small 53.62 µs (➖ 1.00x slower)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.28 µs (➖ 1.00x slower) 2.27 µs (➖ 1.00x slower)
262144 8.06 µs (➖ 1.02x slower) 7.90 µs (➖ 1.00x slower)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 7.49 µs (➖ 1.00x faster) 7.52 µs (➖ 1.00x faster)
262144 37.48 µs (➖ 1.14x faster)
8192 901.55 ns (➖ 1.01x faster) 907.44 ns (➖ 1.02x faster)
hyperv-ws2025 / amd (Windows) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 546.06 ns (➖ 1.01x faster)
vec_bytes 155.13 ns (➖ 1.07x faster)
1.92 ms (➖ 1.02x faster)

payload_allocation

slot_pool_segmented
65536 231.70 ns (➖ 1.01x slower)
262144 784.41 ns (➖ 1.00x faster)

sandboxes

create_initialized_and_drop
medium 12.52 ms (➖ 1.06x faster)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
10.03 ns (➖ 1.00x faster) 9.90 ns (➖ 1.00x slower) 9.98 ns (➖ 1.00x slower)

snapshot_files

load_snapshot_unverified
small 855.01 µs (➖ 1.09x slower)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.32 µs (➖ 1.01x faster) 2.31 µs (➖ 1.01x faster)
262144 9.07 µs (➖ 1.03x faster) 9.20 µs (➖ 1.03x slower)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 8.67 µs (➖ 1.08x faster) 8.86 µs (➖ 1.01x slower)
262144 38.73 µs (➖ 1.01x faster)
8192 1.26 µs (➖ 1.01x faster) 1.26 µs (➖ 1.02x faster)
hyperv-ws2025 / intel (Windows) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 560.95 ns (➖ 1.05x faster)
vec_bytes 160.62 ns (➖ 1.00x slower)
4.10 ms (➖ 1.01x faster)

payload_allocation

slot_pool_segmented
65536 217.47 ns (➖ 1.01x slower)
262144 745.41 ns (➖ 1.00x slower)

sandboxes

create_initialized_and_drop
medium 15.00 ms (➖ 1.01x faster)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
10.05 ns (➖ 1.02x faster) 9.77 ns (➖ 1.01x slower) 9.74 ns (➖ 1.01x faster)

snapshot_files

load_snapshot_unverified
small 713.06 µs (➖ 1.03x slower)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.30 µs (➖ 1.01x slower) 2.24 µs (➖ 1.04x faster)
262144 7.71 µs (➖ 1.01x slower) 7.59 µs (➖ 1.03x faster)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 8.35 µs (➖ 1.02x faster) 8.50 µs (➖ 1.02x slower)
262144 42.59 µs (➖ 1.13x faster)
8192 1.05 µs (➖ 1.16x faster) 1.14 µs (➖ 1.03x slower)

Reported by cargo ci bench-report --candidate run:45412912183 --config-file bench_report.toml.

@ludfjig ludfjig left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. The 30 layer limit is arbitrary, we might want to add a config on sandbox to customize this

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

C-enhancement For PRs adding features, improving functionality, docs, tests, etc.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants