runtime-rs: Add reclaim_guest_freed_memory qemu support

Add reclaim_guest_freed_memory config to qemu in runtime-rs.

Fixes: #10710

Signed-off-by: Hui Zhu <teawater@antgroup.com>
This commit is contained in:
Hui Zhu 2025-01-06 19:37:04 +08:00
parent 8f550de88a
commit 487171d992
2 changed files with 38 additions and 0 deletions

View File

@ -143,6 +143,16 @@ default_maxvcpus = @DEFMAXVCPUS_QEMU@
# > 5 --> will be set to 5
default_bridges = @DEFBRIDGES@
# Reclaim guest freed memory.
# Enabling this will result in the VM balloon device having f_reporting=on set.
# Then the hypervisor will use it to reclaim guest freed memory.
# This is useful for reducing the amount of memory used by a VM.
# Enabling this feature may sometimes reduce the speed of memory access in
# the VM.
#
# Default false
#reclaim_guest_freed_memory = true
# Default memory size in MiB for SB/VM.
# If unspecified then it will be set @DEFMEMSZ@ MiB.
default_memory = @DEFMEMSZ@

View File

@ -1793,6 +1793,10 @@ impl<'a> QemuCmdLine<'a> {
qemu_cmd_line.add_scsi_controller();
}
if config.device_info.reclaim_guest_freed_memory {
qemu_cmd_line.add_virtio_balloon();
}
Ok(qemu_cmd_line)
}
@ -2007,6 +2011,11 @@ impl<'a> QemuCmdLine<'a> {
self.devices.push(Box::new(console_socket_chardev));
}
pub fn add_virtio_balloon(&mut self) {
let balloon_device = DeviceVirtioBalloon::new();
self.devices.push(Box::new(balloon_device));
}
pub async fn build(&self) -> Result<Vec<String>> {
let mut result = Vec::new();
@ -2071,3 +2080,22 @@ fn get_devno_ccw(ccw_subchannel: &mut Option<CcwSubChannel>, device_name: &str)
)
})
}
#[derive(Debug)]
struct DeviceVirtioBalloon {}
impl DeviceVirtioBalloon {
fn new() -> Self {
DeviceVirtioBalloon {}
}
}
#[async_trait]
impl ToQemuParams for DeviceVirtioBalloon {
async fn qemu_params(&self) -> Result<Vec<String>> {
Ok(vec![
"-device".to_owned(),
"virtio-balloon,free-page-reporting=on".to_owned(),
])
}
}