Skip to content

Commit

Permalink
feat: Add debug logging to state file
Browse files Browse the repository at this point in the history
Signed-off-by: Felicitas Pojtinger <[email protected]>
  • Loading branch information
pojntfx committed Feb 4, 2025
1 parent cb42196 commit e696f69
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
17 changes: 14 additions & 3 deletions src/vmm/src/persist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use std::fmt::Debug;
use std::fs::{File, OpenOptions};
use std::io::{self, Write};
use std::io::{self, Read, Seek, SeekFrom, Write};
use std::os::unix::io::AsRawFd;
use std::os::unix::net::UnixStream;
use std::path::Path;
Expand Down Expand Up @@ -521,9 +521,20 @@ fn snapshot_state_from_file(
let snapshot = Snapshot::new(SNAPSHOT_VERSION);
let mut snapshot_reader =
File::open(snapshot_path).map_err(SnapshotStateFromFileError::Open)?;
let raw_snapshot_len: u64 =
Snapshot::deserialize(&mut snapshot_reader).map_err(SnapshotStateFromFileError::Meta)?;
let mut len_bytes = [0u8; 8];
snapshot_reader.read_exact(&mut len_bytes).unwrap();
println!("Read length bytes: {:?}", len_bytes);
let raw_snapshot_len = u64::from_le_bytes(len_bytes);
println!("Interpreted length: {}", raw_snapshot_len);

// Also, after reading the length, let's peek at the next few bytes:
let mut peek_buf = vec![0u8; 16];
snapshot_reader.read(&mut peek_buf).unwrap();
println!("Next few bytes after length: {:?}", peek_buf);
// Don't forget to seek back:
snapshot_reader.seek(SeekFrom::Current(-16)).unwrap();
let snapshot_len = u64_to_usize(raw_snapshot_len);
println!("Reading file with len {raw_snapshot_len} {snapshot_len}");
let state: MicrovmState = snapshot
.load_with_version_check(&mut snapshot_reader, snapshot_len)
.map_err(SnapshotStateFromFileError::Load)?;
Expand Down
16 changes: 14 additions & 2 deletions src/vmm/src/snapshot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
//! provided by the library clients (it is not tied to this crate).
pub mod crc;
mod persist;
use std::cmp::min;
use std::fmt::Debug;
use std::io::{Read, Write};

Expand Down Expand Up @@ -218,9 +219,20 @@ impl Snapshot {
crc_writer.flush().map_err(|_| SnapshotError::Flush)?;

let snapshot_len = snapshot_buf.len() as u64;
Self::serialize(writer, &snapshot_len)?;
println!("About to write length: {}", snapshot_len);
println!("Length bytes: {:?}", snapshot_len.to_le_bytes());
writer.write_all(&snapshot_len.to_le_bytes()).unwrap();
println!("Writing snapshot buffer of size: {}", snapshot_buf.len());
println!(
"First few bytes of snapshot: {:?}",
&snapshot_buf[..min(16, snapshot_buf.len())]
);

println!("Writing file with len {snapshot_len}");

writer.write_all(&snapshot_buf).map_err(|_| SnapshotError::Write)
writer
.write_all(&snapshot_buf)
.map_err(|_| SnapshotError::Write)
}

/// Save a snapshot with no CRC64 checksum included.
Expand Down

0 comments on commit e696f69

Please sign in to comment.