virtio-balloon: Add memory_actual_size to vm.info to show memory actual size

The virtio-balloon change the memory size is asynchronous.
VirtioBalloonConfig.actual of balloon device show current balloon size.

This commit add memory_actual_size to vm.info to show memory actual size.

Signed-off-by: Hui Zhu <teawater@antfin.com>
This commit is contained in:
Hui Zhu
2020-08-25 14:44:21 +08:00
committed by Sebastien Boeuf
parent 9dffc5da5c
commit c75f8b2f89
8 changed files with 93 additions and 8 deletions

View File

@@ -145,6 +145,7 @@ pub type ApiResult<T> = std::result::Result<T, ApiError>;
pub struct VmInfo {
pub config: Arc<Mutex<VmConfig>>,
pub state: VmState,
pub memory_actual_size: u64,
}
#[derive(Clone, Deserialize, Serialize)]

View File

@@ -360,6 +360,9 @@ components:
state:
type: string
enum: [Created, Running, Shutdown, Paused]
memory_actual_size:
type: integer
format: int64
description: Virtual Machine information
VmCounters:

View File

@@ -534,6 +534,24 @@ impl MemoryConfig {
zones,
})
}
pub fn total_size(&self) -> u64 {
let mut size = self.size;
if let Some(hotplugged_size) = self.hotplugged_size {
size += hotplugged_size;
}
if let Some(zones) = &self.zones {
for zone in zones.iter() {
size += zone.size;
if let Some(hotplugged_size) = zone.hotplugged_size {
size += hotplugged_size;
}
}
}
size
}
}
impl Default for MemoryConfig {

View File

@@ -465,9 +465,17 @@ impl Vmm {
None => VmState::Created,
};
let config = Arc::clone(config);
let mut memory_actual_size = config.lock().unwrap().memory.total_size();
if let Some(vm) = &self.vm {
memory_actual_size -= vm.get_balloon_actual();
}
Ok(VmInfo {
config: Arc::clone(config),
config,
state,
memory_actual_size,
})
}
None => Err(VmError::VmNotCreated),

View File

@@ -1236,6 +1236,14 @@ impl MemoryManager {
Ok(balloon_size)
}
pub fn get_balloon_actual(&self) -> u64 {
if let Some(balloon) = &self.balloon {
return balloon.lock().unwrap().get_actual();
}
0
}
/// In case this function resulted in adding a new memory region to the
/// guest memory, the new region is returned to the caller. The virtio-mem
/// use case never adds a new region as the whole hotpluggable memory has

View File

@@ -1493,6 +1493,11 @@ impl Vm {
Ok(())
}
/// Gets the actual size of the balloon.
pub fn get_balloon_actual(&self) -> u64 {
self.memory_manager.lock().unwrap().get_balloon_actual()
}
}
impl Pausable for Vm {