mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2026-08-05 02:19:16 +00:00
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:
@@ -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)]
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user