Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions src/hyperlight_common/src/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@ pub struct GuestMemoryRegion {
pub ptr: u64,
}

impl GuestMemoryRegion {
/// Size of a serialized `GuestMemoryRegion` in bytes.
pub const SERIALIZED_SIZE: usize = core::mem::size_of::<Self>();

/// Write this region's fields in native-endian byte order to `buf`.
/// Returns `Ok(())` on success, or `Err` if `buf` is too small.
pub fn write_to(&self, buf: &mut [u8]) -> Result<(), &'static str> {
if buf.len() < Self::SERIALIZED_SIZE {
return Err("buffer too small for GuestMemoryRegion");
}
let s = core::mem::size_of::<u64>();
buf[..s].copy_from_slice(&self.size.to_ne_bytes());
buf[s..s * 2].copy_from_slice(&self.ptr.to_ne_bytes());
Ok(())
}
}

/// Maximum length of a file mapping label (excluding null terminator).
pub const FILE_MAPPING_LABEL_MAX_LEN: usize = 63;

Expand Down Expand Up @@ -80,3 +97,28 @@ pub struct HyperlightPEB {
#[cfg(feature = "nanvix-unstable")]
pub file_mappings: GuestMemoryRegion,
}

impl HyperlightPEB {
/// Write the PEB fields in native-endian byte order to `buf`.
/// The buffer must be at least `size_of::<HyperlightPEB>()` bytes.
/// Returns `Err` if the buffer is too small.
pub fn write_to(&self, buf: &mut [u8]) -> Result<(), &'static str> {
if buf.len() < core::mem::size_of::<Self>() {
return Err("buffer too small for HyperlightPEB");
}
let regions = [
&self.input_stack,
&self.output_stack,
&self.init_data,
&self.guest_heap,
#[cfg(feature = "nanvix-unstable")]
&self.file_mappings,
];
let mut offset = 0;
for region in regions {
region.write_to(&mut buf[offset..])?;
offset += GuestMemoryRegion::SERIALIZED_SIZE;
}
Ok(())
}
}
1 change: 1 addition & 0 deletions src/hyperlight_host/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ metrics = "0.24.3"
serde_json = "1.0"
elfcore = { version = "2.0", optional = true }
uuid = { version = "1.23.1", features = ["v4"] }
bytemuck = { version = "1.16", features = ["derive"] }

[target.'cfg(windows)'.dependencies]
windows = { version = "0.62", features = [
Expand Down
100 changes: 99 additions & 1 deletion src/hyperlight_host/benches/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,103 @@ fn shared_memory_benchmark(c: &mut Criterion) {
group.finish();
}

// ============================================================================
// Benchmark Category: Snapshot Files
// ============================================================================

fn snapshot_file_benchmark(c: &mut Criterion) {
use hyperlight_host::HostFunctions;
use hyperlight_host::sandbox::snapshot::Snapshot;

let mut group = c.benchmark_group("snapshot_files");

// Pre-create snapshot files for all sizes
let dirs: Vec<_> = SandboxSize::all()
.iter()
.map(|size| {
let dir = tempfile::tempdir().unwrap();
let snap_path = dir.path().join(format!("{}.hls", size.name()));
let snapshot = {
let mut sbox = create_multiuse_sandbox_with_size(*size);
sbox.snapshot().unwrap()
};
snapshot.to_file(&snap_path).unwrap();
(dir, snapshot)
})
.collect();

// Benchmark: save_snapshot
for (i, size) in SandboxSize::all().iter().enumerate() {
let snap_dir = tempfile::tempdir().unwrap();
let path = snap_dir.path().join("bench.hls");
let snapshot = &dirs[i].1;
group.bench_function(format!("save_snapshot/{}", size.name()), |b| {
b.iter(|| {
snapshot.to_file(&path).unwrap();
});
});
}

// Benchmark: load_snapshot (mmap + header parse + hash verify)
for (i, size) in SandboxSize::all().iter().enumerate() {
let snap_path = dirs[i].0.path().join(format!("{}.hls", size.name()));
group.bench_function(format!("load_snapshot/{}", size.name()), |b| {
b.iter(|| {
let _ = Snapshot::from_file(&snap_path).unwrap();
});
});
}

// Benchmark: cold_start_via_evolve (new + evolve + call)
for size in SandboxSize::all() {
group.bench_function(format!("cold_start_via_evolve/{}", size.name()), |b| {
b.iter(|| {
let mut sbox = create_multiuse_sandbox_with_size(size);
sbox.call::<String>("Echo", "hello\n".to_string()).unwrap();
});
});
}

// Benchmark: cold_start_via_snapshot (load + from_snapshot + call)
for (i, size) in SandboxSize::all().iter().enumerate() {
let snap_path = dirs[i].0.path().join(format!("{}.hls", size.name()));
group.bench_function(format!("cold_start_via_snapshot/{}", size.name()), |b| {
b.iter(|| {
let loaded = Snapshot::from_file(&snap_path).unwrap();
let mut sbox = MultiUseSandbox::from_snapshot(
std::sync::Arc::new(loaded),
HostFunctions::default(),
None,
)
.unwrap();
sbox.call::<String>("Echo", "hello\n".to_string()).unwrap();
});
});
}

// Benchmark: cold_start_via_snapshot_unchecked (no hash verify)
for (i, size) in SandboxSize::all().iter().enumerate() {
let snap_path = dirs[i].0.path().join(format!("{}.hls", size.name()));
group.bench_function(
format!("cold_start_via_snapshot_unchecked/{}", size.name()),
|b| {
b.iter(|| {
let loaded = Snapshot::from_file_unchecked(&snap_path).unwrap();
let mut sbox = MultiUseSandbox::from_snapshot(
std::sync::Arc::new(loaded),
HostFunctions::default(),
None,
)
.unwrap();
sbox.call::<String>("Echo", "hello\n".to_string()).unwrap();
});
},
);
}

group.finish();
}

criterion_group! {
name = benches;
config = Criterion::default();
Expand All @@ -561,6 +658,7 @@ criterion_group! {
guest_call_benchmark_large_param,
function_call_serialization_benchmark,
sample_workloads_benchmark,
shared_memory_benchmark
shared_memory_benchmark,
snapshot_file_benchmark
}
criterion_main!(benches);
Loading
Loading