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 20, 2023
1 parent 27512b6 commit 001e6a5
Show file tree
Hide file tree
Showing 18 changed files with 400 additions and 73 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.

26 changes: 11 additions & 15 deletions src/runtime-rs/crates/hypervisor/src/dragonball/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,12 +385,12 @@ impl DragonballInner {
req_mem_mb: u32,
curr_mem_mb: u32,
) -> Result<(u32, MemoryConfig)> {
let mem_device_to_insert = match req_mem_mb.cmp(&curr_mem_mb) {
match req_mem_mb.cmp(&curr_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 {
let add_mem_mb = req_mem_mb - curr_mem_mb;
self.vmm_instance.insert_mem_device(MemDeviceConfigInfo {
mem_id: format!("mem{}", curr_mem_mb),
size_mib: add_mem_mb as u64,
capacity_mib: add_mem_mb as u64,
Expand All @@ -399,9 +399,7 @@ impl DragonballInner {
guest_numa_node_id: None,
use_shared_irq: None,
use_generic_irq: None,
})
} else {
None
})?;
}
}
Ordering::Less => {
Expand All @@ -417,18 +415,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
4 changes: 2 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,8 @@ 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, curr_mem_mb: u32)
-> Result<(u32, MemoryConfig)>;

// device manager
async fn add_device(&self, device: DeviceType) -> Result<()>;
Expand All @@ -108,6 +110,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)>;
}
23 changes: 3 additions & 20 deletions src/runtime-rs/crates/resource/src/cpu_mem/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use std::{
sync::Arc,
};

use agent::{Agent, OnlineCPUMemRequest};
use anyhow::{Context, Ok, Result};
use hypervisor::Hypervisor;
use kata_types::{config::TomlConfig, cpu::LinuxContainerCpuResources};
Expand Down Expand Up @@ -52,7 +51,6 @@ impl CpuResource {
linux_cpus: Option<&LinuxCpu>,
op: ResourceUpdateOp,
hypervisor: &dyn Hypervisor,
agent: &dyn Agent,
) -> Result<()> {
self.update_container_cpu_resources(cid, linux_cpus, op)
.await
Expand All @@ -67,13 +65,13 @@ impl CpuResource {
}

let curr_vcpus = self
.do_update_cpu_resources(vcpu_required, op, hypervisor, agent)
.do_update_cpu_resources(vcpu_required, op, hypervisor)
.await?;
self.update_current_vcpu(curr_vcpus).await;
Ok(())
}

async fn current_vcpu(&self) -> u32 {
pub(crate) async fn current_vcpu(&self) -> u32 {
let current_vcpu = self.current_vcpu.read().await;
*current_vcpu
}
Expand Down Expand Up @@ -148,7 +146,6 @@ impl CpuResource {
new_vcpus: u32,
op: ResourceUpdateOp,
hypervisor: &dyn Hypervisor,
agent: &dyn Agent,
) -> Result<u32> {
let old_vcpus = self.current_vcpu().await;

Expand All @@ -164,25 +161,11 @@ impl CpuResource {
// the number of vcpus would not be lower than the default size
let new_vcpus = cmp::max(new_vcpus, self.default_vcpu);

let (old, new) = hypervisor
let (_, new) = hypervisor
.resize_vcpu(old_vcpus, new_vcpus)
.await
.context("resize vcpus")?;

if old < new {
let add = new - old;
info!(sl!(), "request to onlineCpuMem with {:?} cpus", add);

agent
.online_cpu_mem(OnlineCPUMemRequest {
wait: false,
nb_cpus: new,
cpu_only: true,
})
.await
.context("online vcpus")?;
}

Ok(new)
}
}

0 comments on commit 001e6a5

Please sign in to comment.