Skip to content

Commit b1622ac

Browse files
ilstamzulinx86
authored andcommitted
tests: memory.rs: Extend test_dump_dirty() with trailing clean case
The test_dump_dirty() test currently checks the file contents of a differential snapshot where the last page of both memory regions is dirty. Extend this test to also test the case where the last page of both regions is clean. Additionally, check that the logical size of the resulting file is different than the physical size due to the holes representing clean pages. Finally, make sure that if the KVM dirty bitmap is larger than the region size, the extra bits are ignored. Signed-off-by: Ilias Stamatis <ilstam@amazon.com>
1 parent 524c6d8 commit b1622ac

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

src/vmm/src/vstate/memory.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,7 @@ mod tests {
867867

868868
use std::collections::HashMap;
869869
use std::io::{Read, Seek, Write};
870+
use std::os::unix::fs::MetadataExt;
870871

871872
use vmm_sys_util::tempfile::TempFile;
872873

@@ -1248,6 +1249,50 @@ mod tests {
12481249
reader.read_to_end(&mut diff_file_content).unwrap();
12491250
assert_eq!(expected_file_contents, diff_file_content);
12501251

1252+
// Take a 3rd snapshot
1253+
1254+
// Firecracker Dirty Bitmap:
1255+
// First region pages: [dirty, clean]
1256+
// Second region pages: [dirty, clean]
1257+
guest_memory.write(&twos, region_1_address).unwrap();
1258+
guest_memory.write(&ones, region_2_address).unwrap();
1259+
// KVM dirty bitmap:
1260+
// First region pages: [clean, clean]
1261+
// Second region pages: [clean, clean]
1262+
kvm_dirty_bitmap.insert(0, vec![0b00]);
1263+
kvm_dirty_bitmap.insert(1, vec![0b00]);
1264+
1265+
let file = TempFile::new().unwrap();
1266+
let logical_size = page_size as u64 * 4;
1267+
file.as_file().set_len(logical_size).unwrap();
1268+
1269+
let mut reader = file.into_file();
1270+
guest_memory
1271+
.dump_dirty(&mut reader, &kvm_dirty_bitmap)
1272+
.unwrap();
1273+
1274+
// Check that only the dirty regions are dumped.
1275+
let mut diff_file_content = Vec::new();
1276+
// The resulting file is a sparse file with holes.
1277+
let expected_file_contents = [
1278+
twos.as_slice(),
1279+
zeros.as_slice(), // hole
1280+
ones.as_slice(),
1281+
zeros.as_slice(), // hole
1282+
]
1283+
.concat();
1284+
reader.seek(SeekFrom::Start(0)).unwrap();
1285+
reader.read_to_end(&mut diff_file_content).unwrap();
1286+
1287+
assert_eq!(expected_file_contents, diff_file_content);
1288+
1289+
// Make sure that only 2 of the pages are written in the file and the
1290+
// other two are holes.
1291+
let metadata = reader.metadata().unwrap();
1292+
let physical_size = metadata.blocks() * 512;
1293+
assert_eq!(physical_size, 2 * page_size as u64);
1294+
assert_ne!(physical_size, logical_size);
1295+
12511296
// Test with bitmaps that are too large or too small
12521297
kvm_dirty_bitmap.insert(0, vec![0b1, 0b01]);
12531298
kvm_dirty_bitmap.insert(1, vec![0b10]);

0 commit comments

Comments
 (0)