Skip to content

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

Merged
jprendes merged 3 commits into
hyperlight-dev:mainfrom
jprendes:feat/snapshot-compaction
Nov 25, 2026
Merged

jprendes merged 3 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

This comment has been minimized.

ludfjig
ludfjig previously approved these changes Nov 24, 2026

@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

Retain bootstrap queue images from upstream and Clone support required by snapshot compaction.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Jorge Prendes <jorge.prendes@gmail.com>
@jprendes
jprendes enabled auto-merge (squash) November 25, 2026 19:50
@hyperlight-gh-bot

Copy link
Copy Markdown

Benchmark Results

Measured commit: 00df6242e70f
Baseline commit: c46735cd076c

kvm / amd (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 291.14 ns (➖ 1.14x faster)
vec_bytes 82.43 ns (➖ 1.03x slower)
371.94 µs (➖ 1.00x faster)

payload_allocation

slot_pool_segmented
65536 142.18 ns (➖ 1.10x faster)
262144 528.10 ns (➖ 1.03x faster)

sandboxes

create_initialized_and_drop
medium 13.44 ms (➖ 1.03x faster)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
7.69 ns (➖ 1.00x slower) 7.68 ns (➖ 1.02x faster) 7.68 ns (➖ 1.00x faster)

snapshot_files

load_snapshot_unverified
small 109.32 µs (➖ 1.02x slower)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 1.92 µs (➖ 1.08x faster) 2.04 µs (➖ 1.06x slower)
262144 7.87 µs (➖ 1.15x faster) 7.32 µs (➖ 1.04x slower)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 6.16 µs (➖ 1.01x faster) 6.07 µs (➖ 1.03x faster)
262144 28.39 µs (➖ 1.05x slower)
8192 1.02 µs (➖ 1.01x slower) 1.04 µs (➖ 1.02x faster)
kvm / intel (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 335.62 ns (➖ 1.10x slower)
vec_bytes 95.10 ns (➖ 1.00x faster)
760.05 µs (➖ 1.03x slower)

payload_allocation

slot_pool_segmented
65536 159.20 ns (➖ 1.04x slower)
262144 589.25 ns (➖ 1.04x slower)

sandboxes

create_initialized_and_drop
medium 15.50 ms (➖ 1.04x slower)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
8.16 ns (➖ 1.04x slower) 8.14 ns (➖ 1.04x slower) 8.17 ns (➖ 1.04x slower)

snapshot_files

load_snapshot_unverified
small 67.63 µs (➖ 1.09x slower)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.53 µs (➖ 1.05x slower) 2.49 µs (➖ 1.01x faster)
262144 8.91 µs (➖ 1.03x slower) 8.89 µs (➖ 1.03x slower)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 8.32 µs (➖ 1.01x slower) 8.28 µs (➖ 1.01x faster)
262144 34.88 µs (➖ 1.02x faster)
8192 866.96 ns (➖ 1.05x slower) 856.47 ns (➖ 1.02x slower)
mshv3 / amd (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 374.24 ns (➖ 1.05x faster)
vec_bytes 102.38 ns (➖ 1.06x slower)
368.25 µs (➖ 1.13x slower)

payload_allocation

slot_pool_segmented
65536 269.47 ns (➖ 1.28x slower)
262144 733.11 ns (➖ 1.00x slower)

sandboxes

create_initialized_and_drop
medium 69.07 ms (➖ 1.01x slower)

slot_pool

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

snapshot_files

load_snapshot_unverified
small 97.85 µs (➖ 1.07x faster)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.21 µs (➖ 1.00x faster) 2.49 µs (➖ 1.02x slower)
262144 9.77 µs (➖ 1.01x faster) 10.05 µs (➖ 1.05x faster)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 8.38 µs (➖ 1.16x faster) 8.48 µs (➖ 1.13x faster)
262144 36.19 µs (➖ 1.04x faster)
8192 1.24 µs (➖ 1.02x slower) 1.22 µs (➖ 1.05x faster)
mshv3 / intel (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 354.68 ns (➖ 1.10x faster)
vec_bytes 89.42 ns (➖ 1.05x slower)
712.01 µs (➖ 1.01x faster)

payload_allocation

slot_pool_segmented
65536 173.73 ns (➖ 1.00x slower)
262144 779.02 ns (➖ 1.23x slower)

sandboxes

create_initialized_and_drop
medium 72.20 ms (➖ 1.23x faster)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
8.69 ns (➖ 1.01x faster) 9.17 ns (➖ 1.06x faster) 8.78 ns (➖ 1.01x faster)

snapshot_files

load_snapshot_unverified
small 53.72 µs (➖ 1.01x faster)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.28 µs (➖ 1.00x faster) 2.26 µs (➖ 1.01x faster)
262144 8.01 µs (➖ 1.01x slower) 7.91 µs (➖ 1.04x faster)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 7.47 µs (➖ 1.00x faster) 7.49 µs (➖ 1.01x faster)
262144 39.57 µs (➖ 1.06x faster)
8192 916.26 ns (➖ 1.01x slower) 904.68 ns (➖ 1.01x faster)
hyperv-ws2025 / amd (Windows) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 568.67 ns (➖ 1.00x faster)
vec_bytes 160.86 ns (➖ 1.00x faster)
2.08 ms (➖ 1.01x slower)

payload_allocation

slot_pool_segmented
65536 231.83 ns (➖ 1.04x faster)
262144 793.56 ns (➖ 1.02x faster)

sandboxes

create_initialized_and_drop
medium 15.43 ms (➖ 1.07x slower)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
10.53 ns (➖ 1.03x faster) 10.10 ns (➖ 1.05x faster) 10.37 ns (➖ 1.04x faster)

snapshot_files

load_snapshot_unverified
small 710.72 µs (➖ 1.01x faster)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.30 µs (➖ 1.03x faster) 2.33 µs (➖ 1.01x slower)
262144 9.04 µs (➖ 1.01x faster) 9.23 µs (➖ 1.00x slower)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 8.91 µs (➖ 1.01x slower) 9.28 µs (➖ 1.05x slower)
262144 38.81 µs (➖ 1.15x faster)
8192 1.28 µs (➖ 1.00x slower) 1.29 µ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 526.31 ns (➖ 1.01x slower)
vec_bytes 142.91 ns (➖ 1.05x slower)
3.29 ms (➖ 1.02x faster)

payload_allocation

slot_pool_segmented
65536 197.54 ns (➖ 1.00x faster)
262144 681.24 ns (➖ 1.02x faster)

sandboxes

create_initialized_and_drop
medium 13.46 ms (➖ 1.02x faster)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
8.80 ns (➖ 1.01x faster) 8.81 ns (➖ 1.01x faster) 8.82 ns (➖ 1.01x faster)

snapshot_files

load_snapshot_unverified
small 478.65 µs (➖ 1.17x faster)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.56 µs (➖ 1.02x faster) 2.56 µs (➖ 1.01x faster)
262144 8.82 µs (➖ 1.01x faster) 8.82 µs (➖ 1.01x faster)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 9.65 µs (➖ 1.01x slower) 9.63 µs (➖ 1.03x slower)
262144 35.93 µs (➖ 1.01x faster)
8192 1.08 µs (➖ 1.02x faster) 1.08 µs (➖ 1.01x faster)

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

@jprendes
jprendes merged commit 9593132 into hyperlight-dev:main Nov 25, 2026
186 of 190 checks passed
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