vm-migration: Introduce new constructor for Snapshot

This simplifies the Snapshot creation as we expect a SnapshotData to be
provided most of the time.

Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
This commit is contained in:
Sebastien Boeuf 2022-12-02 16:03:50 +01:00
parent 4ae6b595d7
commit 3931b99d4e
5 changed files with 20 additions and 30 deletions

View File

@ -148,15 +148,19 @@ pub struct Snapshot {
} }
impl Snapshot { impl Snapshot {
pub fn from_data(data: SnapshotData) -> Self {
Snapshot {
snapshot_data: Some(data),
..Default::default()
}
}
/// Create from state that can be serialized /// Create from state that can be serialized
pub fn new_from_state<T>(state: &T) -> Result<Self, MigratableError> pub fn new_from_state<T>(state: &T) -> Result<Self, MigratableError>
where where
T: Serialize, T: Serialize,
{ {
let mut snapshot_data = Snapshot::default(); Ok(Snapshot::from_data(SnapshotData::new_from_state(state)?))
snapshot_data.add_data(SnapshotData::new_from_state(state)?);
Ok(snapshot_data)
} }
/// Create from versioned state /// Create from versioned state
@ -164,10 +168,9 @@ impl Snapshot {
where where
T: Versionize + VersionMapped, T: Versionize + VersionMapped,
{ {
let mut snapshot_data = Snapshot::default(); Ok(Snapshot::from_data(SnapshotData::new_from_versioned_state(
snapshot_data.add_data(SnapshotData::new_from_versioned_state(state)?); state,
)?))
Ok(snapshot_data)
} }
/// Add a sub-component's Snapshot to the Snapshot. /// Add a sub-component's Snapshot to the Snapshot.
@ -175,11 +178,6 @@ impl Snapshot {
self.snapshots.insert(id, snapshot); self.snapshots.insert(id, snapshot);
} }
/// Add a SnapshotData to the component snapshot data.
pub fn add_data(&mut self, section: SnapshotData) {
self.snapshot_data = Some(section);
}
/// Generate the state data from the snapshot /// Generate the state data from the snapshot
pub fn to_state<'a, T>(&'a self) -> Result<T, MigratableError> pub fn to_state<'a, T>(&'a self) -> Result<T, MigratableError>
where where

View File

@ -399,12 +399,11 @@ impl Snapshottable for Vcpu {
.state() .state()
.map_err(|e| MigratableError::Pause(anyhow!("Could not get vCPU state {:?}", e)))?; .map_err(|e| MigratableError::Pause(anyhow!("Could not get vCPU state {:?}", e)))?;
let mut vcpu_snapshot = Snapshot::default(); self.saved_state = Some(saved_state.clone());
vcpu_snapshot.add_data(SnapshotData::new_from_state(&saved_state)?);
self.saved_state = Some(saved_state); Ok(Snapshot::from_data(SnapshotData::new_from_state(
&saved_state,
Ok(vcpu_snapshot) )?))
} }
} }

View File

@ -4445,7 +4445,7 @@ impl Snapshottable for DeviceManager {
} }
fn snapshot(&mut self) -> std::result::Result<Snapshot, MigratableError> { fn snapshot(&mut self) -> std::result::Result<Snapshot, MigratableError> {
let mut snapshot = Snapshot::default(); let mut snapshot = Snapshot::from_data(SnapshotData::new_from_state(&self.state())?);
// We aggregate all devices snapshots. // We aggregate all devices snapshots.
for (_, device_node) in self.device_tree.lock().unwrap().iter() { for (_, device_node) in self.device_tree.lock().unwrap().iter() {
@ -4455,9 +4455,6 @@ impl Snapshottable for DeviceManager {
} }
} }
// Then we store the DeviceManager state.
snapshot.add_data(SnapshotData::new_from_state(&self.state())?);
Ok(snapshot) Ok(snapshot)
} }
} }

View File

@ -2428,8 +2428,6 @@ impl Snapshottable for MemoryManager {
} }
fn snapshot(&mut self) -> result::Result<Snapshot, MigratableError> { fn snapshot(&mut self) -> result::Result<Snapshot, MigratableError> {
let mut memory_manager_snapshot = Snapshot::default();
let memory_ranges = self.memory_range_table(true)?; let memory_ranges = self.memory_range_table(true)?;
// Store locally this list of ranges as it will be used through the // Store locally this list of ranges as it will be used through the
@ -2442,11 +2440,9 @@ impl Snapshottable for MemoryManager {
// memory range content for the ranges requiring it. // memory range content for the ranges requiring it.
self.snapshot_memory_ranges = memory_ranges; self.snapshot_memory_ranges = memory_ranges;
memory_manager_snapshot.add_data(SnapshotData::new_from_versioned_state( Ok(Snapshot::from_data(SnapshotData::new_from_versioned_state(
&self.snapshot_data(), &self.snapshot_data(),
)?); )?))
Ok(memory_manager_snapshot)
} }
} }

View File

@ -2508,7 +2508,6 @@ impl Snapshottable for Vm {
})? })?
}; };
let mut vm_snapshot = Snapshot::default();
let vm_snapshot_data = serde_json::to_vec(&VmSnapshot { let vm_snapshot_data = serde_json::to_vec(&VmSnapshot {
#[cfg(all(feature = "kvm", target_arch = "x86_64"))] #[cfg(all(feature = "kvm", target_arch = "x86_64"))]
clock: self.saved_clock, clock: self.saved_clock,
@ -2517,6 +2516,8 @@ impl Snapshottable for Vm {
}) })
.map_err(|e| MigratableError::Snapshot(e.into()))?; .map_err(|e| MigratableError::Snapshot(e.into()))?;
let mut vm_snapshot = Snapshot::from_data(SnapshotData(vm_snapshot_data));
let (id, snapshot) = { let (id, snapshot) = {
let mut cpu_manager = self.cpu_manager.lock().unwrap(); let mut cpu_manager = self.cpu_manager.lock().unwrap();
(cpu_manager.id(), cpu_manager.snapshot()?) (cpu_manager.id(), cpu_manager.snapshot()?)
@ -2532,7 +2533,6 @@ impl Snapshottable for Vm {
(device_manager.id(), device_manager.snapshot()?) (device_manager.id(), device_manager.snapshot()?)
}; };
vm_snapshot.add_snapshot(id, snapshot); vm_snapshot.add_snapshot(id, snapshot);
vm_snapshot.add_data(SnapshotData(vm_snapshot_data));
event!("vm", "snapshotted"); event!("vm", "snapshotted");
Ok(vm_snapshot) Ok(vm_snapshot)