runtime-rs: add the memory prealloc support for qemu

Add the memory prealloc support for qemu hypervisor.
When it was enabled, all of the memory will be allocated
and locked. This is useful when you want to reserve all the
memory upfront or in the cases where you want memory latencies
to be very predictable.

Signed-off-by: Fupan Li <fupan.lfp@antgroup.com>
This commit is contained in:
Fupan Li 2025-06-12 21:57:32 +08:00
parent 707b8b8a98
commit fb7cfcd2fb

View File

@ -625,6 +625,7 @@ struct MemoryBackendFile {
size: u64, size: u64,
share: bool, share: bool,
readonly: bool, readonly: bool,
prealloc: bool,
} }
impl MemoryBackendFile { impl MemoryBackendFile {
@ -635,6 +636,7 @@ impl MemoryBackendFile {
size, size,
share: false, share: false,
readonly: false, readonly: false,
prealloc: false,
} }
} }
@ -647,6 +649,11 @@ impl MemoryBackendFile {
self.readonly = readonly; self.readonly = readonly;
self self
} }
fn set_prealloc(&mut self, prealloc: bool) -> &mut Self {
self.prealloc = prealloc;
self
}
} }
#[async_trait] #[async_trait]
@ -658,6 +665,10 @@ impl ToQemuParams for MemoryBackendFile {
params.push(format!("mem-path={}", self.mem_path)); params.push(format!("mem-path={}", self.mem_path));
params.push(format!("size={}", format_memory(self.size))); params.push(format!("size={}", format_memory(self.size)));
params.push(format!("share={}", if self.share { "on" } else { "off" })); params.push(format!("share={}", if self.share { "on" } else { "off" }));
params.push(format!(
"prealloc={}",
if self.prealloc { "on" } else { "off" }
));
params.push(format!( params.push(format!(
"readonly={}", "readonly={}",
if self.readonly { "on" } else { "off" } if self.readonly { "on" } else { "off" }
@ -2261,6 +2272,10 @@ impl<'a> QemuCmdLine<'a> {
MemoryBackendFile::new("entire-guest-memory-share", "/dev/shm", self.memory.size); MemoryBackendFile::new("entire-guest-memory-share", "/dev/shm", self.memory.size);
mem_file.set_share(true); mem_file.set_share(true);
if self.config.memory_info.enable_mem_prealloc {
mem_file.set_prealloc(true);
}
// don't put the /dev/shm memory backend file into the anonymous container, // don't put the /dev/shm memory backend file into the anonymous container,
// there has to be at most one of those so keep it by name in Memory instead // there has to be at most one of those so keep it by name in Memory instead
//self.devices.push(Box::new(mem_file)); //self.devices.push(Box::new(mem_file));