Skip to content

Commit

Permalink
runtime-rs: support memory resize
Browse files Browse the repository at this point in the history
Fixes:#6875
Signed-off-by: Zhongtao Hu <zhongtaohu.tim@linux.alibaba.com>
  • Loading branch information
Tim-0731-Hzt committed Jun 21, 2023
1 parent 27512b6 commit 4ddb4f7
Show file tree
Hide file tree
Showing 22 changed files with 440 additions and 97 deletions.
118 changes: 115 additions & 3 deletions src/libs/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/runtime-rs/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ impl CloudHypervisorInner {
pub(crate) fn resize_memory(
&self,
_req_mem_mb: u32,
_curr_mem_mb: u32,
_default_mem_mb: u32,
) -> Result<(u32, MemoryConfig)> {
todo!()
}
Expand Down
4 changes: 2 additions & 2 deletions src/runtime-rs/crates/hypervisor/src/ch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,10 @@ impl Hypervisor for CloudHypervisor {
async fn resize_memory(
&self,
req_mem_mb: u32,
curr_mem_mb: u32,
default_mem_mb: u32,
) -> Result<(u32, MemoryConfig)> {
let inner = self.inner.read().await;
inner.resize_memory(req_mem_mb, curr_mem_mb)
inner.resize_memory(req_mem_mb, default_mem_mb)
}
}

Expand Down
77 changes: 46 additions & 31 deletions src/runtime-rs/crates/hypervisor/src/dragonball/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ pub struct DragonballInner {

/// the size of memory block of guest OS
pub(crate) guest_memory_block_size_mb: u32,

/// the hotplug memory size
pub(crate) mem_hotplug_size_mb: u32,
}

impl DragonballInner {
Expand All @@ -92,6 +95,7 @@ impl DragonballInner {
cached_block_devices: Default::default(),
capabilities,
guest_memory_block_size_mb: 0,
mem_hotplug_size_mb: 0,
}
}

Expand Down Expand Up @@ -379,36 +383,48 @@ impl DragonballInner {
Ok((old_vcpus, new_vcpus))
}

// curr_mem_m size = default + hotplug
pub(crate) fn resize_memory(
&self,
&mut self,
req_mem_mb: u32,
curr_mem_mb: u32,
default_mem_mb: u32,
) -> Result<(u32, MemoryConfig)> {
let mem_device_to_insert = match req_mem_mb.cmp(&curr_mem_mb) {
let had_mem_mb = default_mem_mb + self.mem_hotplug_size_mb;
match req_mem_mb.cmp(&had_mem_mb) {
Ordering::Greater => {
// We need to insert a new memory device
let add_mem_mb = req_mem_mb - curr_mem_mb;
if self.config.memory_info.enable_virtio_mem {
Some(MemDeviceConfigInfo {
mem_id: format!("mem{}", curr_mem_mb),
size_mib: add_mem_mb as u64,
capacity_mib: add_mem_mb as u64,
multi_region: false,
host_numa_node_id: None,
guest_numa_node_id: None,
use_shared_irq: None,
use_generic_irq: None,
})
} else {
None
}
// clean virtio-ballon device before hotplug memory
let balloon_config = BalloonDeviceConfigInfo {
balloon_id: "balloon0".to_owned(),
size_mib: 0,
use_shared_irq: None,
use_generic_irq: None,
f_deflate_on_oom: false,
f_reporting: false,
};
self.vmm_instance
.insert_balloon_device(balloon_config)
.context("failed to insert balloon device")?;

// update the hotplug size
self.mem_hotplug_size_mb = req_mem_mb - default_mem_mb;

// insert a new memory device
let add_mem_mb = req_mem_mb - had_mem_mb;
self.vmm_instance.insert_mem_device(MemDeviceConfigInfo {
mem_id: format!("mem{}", self.mem_hotplug_size_mb),
size_mib: add_mem_mb as u64,
capacity_mib: add_mem_mb as u64,
multi_region: false,
host_numa_node_id: None,
guest_numa_node_id: None,
use_shared_irq: None,
use_generic_irq: None,
})?;
}
Ordering::Less => {
// We need to insert a new balloon device to release memory
let balloon_config = BalloonDeviceConfigInfo {
balloon_id: format!("mem{}", curr_mem_mb),
size_mib: (curr_mem_mb - req_mem_mb) as u64,
balloon_id: "balloon0".to_owned(),
size_mib: (had_mem_mb - req_mem_mb) as u64,
use_shared_irq: None,
use_generic_irq: None,
f_deflate_on_oom: false,
Expand All @@ -417,18 +433,16 @@ impl DragonballInner {
self.vmm_instance
.insert_balloon_device(balloon_config)
.context("failed to insert balloon device")?;
None
}
Ordering::Equal => None, // Everything is already set up
Ordering::Equal => {
// Everything is already set up
warn!(
sl!(),
"memory size unchanged, no need to do memory resizing"
);
}
};

// If we have a memory device to insert, do it now
if let Some(mem_config) = mem_device_to_insert {
self.vmm_instance
.insert_mem_device(mem_config)
.context("failed to insert memory device")?;
}

Ok((
req_mem_mb,
MemoryConfig {
Expand Down Expand Up @@ -498,6 +512,7 @@ impl Persist for DragonballInner {
cached_block_devices: hypervisor_state.cached_block_devices,
capabilities: Capabilities::new(),
guest_memory_block_size_mb: 0,
mem_hotplug_size_mb: 0,
})
}
}
6 changes: 3 additions & 3 deletions src/runtime-rs/crates/hypervisor/src/dragonball/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,10 @@ impl Hypervisor for Dragonball {
async fn resize_memory(
&self,
req_mem_mb: u32,
curr_mem_mb: u32,
default_mem_mb: u32,
) -> Result<(u32, MemoryConfig)> {
let inner = self.inner.read().await;
inner.resize_memory(req_mem_mb, curr_mem_mb)
let mut inner = self.inner.write().await;
inner.resize_memory(req_mem_mb, default_mem_mb)
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/runtime-rs/crates/hypervisor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ pub trait Hypervisor: Send + Sync {
async fn save_vm(&self) -> Result<()>;
async fn resume_vm(&self) -> Result<()>;
async fn resize_vcpu(&self, old_vcpus: u32, new_vcpus: u32) -> Result<(u32, u32)>; // returns (old_vcpus, new_vcpus)
async fn resize_memory(
&self,
req_mem_mb: u32,
default_mem_mb: u32,
) -> Result<(u32, MemoryConfig)>;

// device manager
async fn add_device(&self, device: DeviceType) -> Result<()>;
Expand All @@ -108,6 +113,4 @@ pub trait Hypervisor: Send + Sync {
async fn set_capabilities(&self, flag: CapabilityBits);
async fn set_guest_memory_block_size(&self, size: u32);
async fn guest_memory_block_size(&self) -> u32;
async fn resize_memory(&self, req_mem_mb: u32, curr_mem_mb: u32)
-> Result<(u32, MemoryConfig)>;
}

0 comments on commit 4ddb4f7

Please sign in to comment.