mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-06-19 12:14:11 +00:00
feat(runtime-rs): calculate initial size
In this commit, we refactored the logic of static resource management. We defined the sandbox size calculated from PodSandbox's annotation and SingleContainer's spec as initial size, which will always be the sandbox size when booting the VM. The configuration static_sandbox_resource_mgmt controls whether we will modify the sandbox size in the following container operation. Signed-off-by: Yushuo <y-shuo@linux.alibaba.com> Signed-off-by: Ji-Xinyou <jerryji0414@outlook.com>
This commit is contained in:
parent
aaa96c749b
commit
67972ec48a
@ -41,4 +41,5 @@ logging = { path = "../../../libs/logging" }
|
|||||||
oci = { path = "../../../libs/oci" }
|
oci = { path = "../../../libs/oci" }
|
||||||
actix-rt = "2.7.0"
|
actix-rt = "2.7.0"
|
||||||
persist = { path = "../persist"}
|
persist = { path = "../persist"}
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
|
@ -13,17 +13,16 @@ use kata_types::{
|
|||||||
cpu::LinuxContainerCpuResources, k8s::container_type,
|
cpu::LinuxContainerCpuResources, k8s::container_type,
|
||||||
};
|
};
|
||||||
|
|
||||||
// static resource that StaticResourceManager needs, this is the spec for the
|
// initial resource that InitialSizeManager needs, this is the spec for the
|
||||||
// sandbox/container's workload
|
// sandbox/container's workload
|
||||||
#[derive(Clone, Copy, Debug)]
|
#[derive(Clone, Copy, Debug)]
|
||||||
struct StaticResource {
|
struct InitialSize {
|
||||||
vcpu: u32,
|
vcpu: u32,
|
||||||
mem_mb: u32,
|
mem_mb: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
// generate static resource(vcpu and memory in MiB) from spec's information
|
// generate initial resource(vcpu and memory in MiB) from spec's information
|
||||||
// used for static resource management
|
impl TryFrom<&oci::Spec> for InitialSize {
|
||||||
impl TryFrom<&oci::Spec> for StaticResource {
|
|
||||||
type Error = anyhow::Error;
|
type Error = anyhow::Error;
|
||||||
fn try_from(spec: &oci::Spec) -> Result<Self> {
|
fn try_from(spec: &oci::Spec) -> Result<Self> {
|
||||||
let mut vcpu: u32 = 0;
|
let mut vcpu: u32 = 0;
|
||||||
@ -65,31 +64,32 @@ impl TryFrom<&oci::Spec> for StaticResource {
|
|||||||
}
|
}
|
||||||
info!(
|
info!(
|
||||||
sl!(),
|
sl!(),
|
||||||
"static resource mgmt result: vcpu={}, mem_mb={}", vcpu, mem_mb
|
"(from PodSandbox's annotation / SingleContainer's spec) initial size: vcpu={}, mem_mb={}", vcpu, mem_mb
|
||||||
);
|
);
|
||||||
Ok(Self { vcpu, mem_mb })
|
Ok(Self { vcpu, mem_mb })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// StaticResourceManager is responsible for static resource management
|
// InitialSizeManager is responsible for initial vcpu/mem management
|
||||||
//
|
//
|
||||||
// static resource management sizing information is optionally provided, either by
|
// inital vcpu/mem management sizing information is optionally provided, either by
|
||||||
// upper layer runtime (containerd / crio) or by the container spec itself (when it
|
// upper layer runtime (containerd / crio) or by the container spec itself (when it
|
||||||
// is a standalone single container such as the one started with *docker run*)
|
// is a standalone single container such as the one started with *docker run*)
|
||||||
//
|
//
|
||||||
// the sizing information uses three values, cpu quota, cpu period and memory limit,
|
// the sizing information uses three values, cpu quota, cpu period and memory limit,
|
||||||
// and with above values it calculates the # vcpus and memory for the workload and
|
// and with above values it calculates the # vcpus and memory for the workload
|
||||||
// add them to default value of the config
|
//
|
||||||
|
// if the workload # of vcpus and memory is invalid for vmms, we still use default
|
||||||
|
// value in toml_config
|
||||||
#[derive(Clone, Copy, Debug)]
|
#[derive(Clone, Copy, Debug)]
|
||||||
pub struct StaticResourceManager {
|
pub struct InitialSizeManager {
|
||||||
resource: StaticResource,
|
resource: InitialSize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StaticResourceManager {
|
impl InitialSizeManager {
|
||||||
pub fn new(spec: &oci::Spec) -> Result<Self> {
|
pub fn new(spec: &oci::Spec) -> Result<Self> {
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
resource: StaticResource::try_from(spec)
|
resource: InitialSize::try_from(spec).context("failed to construct static resource")?,
|
||||||
.context("failed to construct static resource")?,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,8 +100,13 @@ impl StaticResourceManager {
|
|||||||
.hypervisor
|
.hypervisor
|
||||||
.get_mut(hypervisor_name)
|
.get_mut(hypervisor_name)
|
||||||
.context("failed to get hypervisor config")?;
|
.context("failed to get hypervisor config")?;
|
||||||
hv.cpu_info.default_vcpus += self.resource.vcpu as i32;
|
|
||||||
hv.memory_info.default_memory += self.resource.mem_mb;
|
if self.resource.vcpu > 0 {
|
||||||
|
hv.cpu_info.default_vcpus = self.resource.vcpu as i32
|
||||||
|
}
|
||||||
|
if self.resource.mem_mb > 0 {
|
||||||
|
hv.memory_info.default_memory = self.resource.mem_mb;
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -151,7 +156,7 @@ mod tests {
|
|||||||
struct TestData<'a> {
|
struct TestData<'a> {
|
||||||
desc: &'a str,
|
desc: &'a str,
|
||||||
input: InputData,
|
input: InputData,
|
||||||
result: StaticResource,
|
result: InitialSize,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_test_data() -> Vec<TestData<'static>> {
|
fn get_test_data() -> Vec<TestData<'static>> {
|
||||||
@ -163,7 +168,7 @@ mod tests {
|
|||||||
quota: None,
|
quota: None,
|
||||||
memory: None,
|
memory: None,
|
||||||
},
|
},
|
||||||
result: StaticResource { vcpu: 0, mem_mb: 0 },
|
result: InitialSize { vcpu: 0, mem_mb: 0 },
|
||||||
},
|
},
|
||||||
TestData {
|
TestData {
|
||||||
desc: "normal resource limit",
|
desc: "normal resource limit",
|
||||||
@ -173,7 +178,7 @@ mod tests {
|
|||||||
quota: Some(220_000),
|
quota: Some(220_000),
|
||||||
memory: Some(1024 * 1024 * 512),
|
memory: Some(1024 * 1024 * 512),
|
||||||
},
|
},
|
||||||
result: StaticResource {
|
result: InitialSize {
|
||||||
vcpu: 3,
|
vcpu: 3,
|
||||||
mem_mb: 512,
|
mem_mb: 512,
|
||||||
},
|
},
|
||||||
@ -183,7 +188,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_static_resource_mgmt_sandbox() {
|
fn test_initial_size_sandbox() {
|
||||||
let tests = get_test_data();
|
let tests = get_test_data();
|
||||||
|
|
||||||
// run tests
|
// run tests
|
||||||
@ -210,22 +215,22 @@ mod tests {
|
|||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
let static_resource = StaticResource::try_from(&spec);
|
let initial_size = InitialSize::try_from(&spec);
|
||||||
assert!(
|
assert!(
|
||||||
static_resource.is_ok(),
|
initial_size.is_ok(),
|
||||||
"test[{}]: {:?} should be ok",
|
"test[{}]: {:?} should be ok",
|
||||||
i,
|
i,
|
||||||
d.desc
|
d.desc
|
||||||
);
|
);
|
||||||
|
|
||||||
let static_resource = static_resource.unwrap();
|
let initial_size = initial_size.unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
static_resource.vcpu, d.result.vcpu,
|
initial_size.vcpu, d.result.vcpu,
|
||||||
"test[{}]: {:?} vcpu should be {}",
|
"test[{}]: {:?} vcpu should be {}",
|
||||||
i, d.desc, d.result.vcpu,
|
i, d.desc, d.result.vcpu,
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
static_resource.mem_mb, d.result.mem_mb,
|
initial_size.mem_mb, d.result.mem_mb,
|
||||||
"test[{}]: {:?} memory should be {}",
|
"test[{}]: {:?} memory should be {}",
|
||||||
i, d.desc, d.result.mem_mb,
|
i, d.desc, d.result.mem_mb,
|
||||||
);
|
);
|
||||||
@ -233,7 +238,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_static_resource_mgmt_container() {
|
fn test_initial_size_container() {
|
||||||
let tests = get_test_data();
|
let tests = get_test_data();
|
||||||
|
|
||||||
// run tests
|
// run tests
|
||||||
@ -261,22 +266,22 @@ mod tests {
|
|||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
let static_resource = StaticResource::try_from(&spec);
|
let initial_size = InitialSize::try_from(&spec);
|
||||||
assert!(
|
assert!(
|
||||||
static_resource.is_ok(),
|
initial_size.is_ok(),
|
||||||
"test[{}]: {:?} should be ok",
|
"test[{}]: {:?} should be ok",
|
||||||
i,
|
i,
|
||||||
d.desc
|
d.desc
|
||||||
);
|
);
|
||||||
|
|
||||||
let static_resource = static_resource.unwrap();
|
let initial_size = initial_size.unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
static_resource.vcpu, d.result.vcpu,
|
initial_size.vcpu, d.result.vcpu,
|
||||||
"test[{}]: {:?} vcpu should be {}",
|
"test[{}]: {:?} vcpu should be {}",
|
||||||
i, d.desc, d.result.vcpu,
|
i, d.desc, d.result.vcpu,
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
static_resource.mem_mb, d.result.mem_mb,
|
initial_size.mem_mb, d.result.mem_mb,
|
||||||
"test[{}]: {:?} memory should be {}",
|
"test[{}]: {:?} memory should be {}",
|
||||||
i, d.desc, d.result.mem_mb,
|
i, d.desc, d.result.mem_mb,
|
||||||
);
|
);
|
@ -5,3 +5,4 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
pub mod cpu;
|
pub mod cpu;
|
||||||
|
pub mod initial_size;
|
||||||
|
@ -317,7 +317,7 @@ impl ResourceManagerInner {
|
|||||||
sb_bindmnt.cleanup_sandbox_bind_mounts()
|
sb_bindmnt.cleanup_sandbox_bind_mounts()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn cleanup(&self) -> Result<()> {
|
pub async fn cleanup(&self) -> Result<()> {
|
||||||
// clean up cgroup
|
// clean up cgroup
|
||||||
self.cgroups_resource
|
self.cgroups_resource
|
||||||
|
@ -13,4 +13,3 @@ pub mod manager;
|
|||||||
pub use manager::RuntimeHandlerManager;
|
pub use manager::RuntimeHandlerManager;
|
||||||
pub use shim_interface;
|
pub use shim_interface;
|
||||||
mod shim_mgmt;
|
mod shim_mgmt;
|
||||||
mod static_resource;
|
|
||||||
|
@ -21,7 +21,7 @@ use kata_types::{
|
|||||||
use linux_container::LinuxContainer;
|
use linux_container::LinuxContainer;
|
||||||
use netns_rs::NetNs;
|
use netns_rs::NetNs;
|
||||||
use persist::sandbox_persist::Persist;
|
use persist::sandbox_persist::Persist;
|
||||||
use resource::network::generate_netns_name;
|
use resource::{cpu_mem::initial_size::InitialSizeManager, network::generate_netns_name};
|
||||||
use shim_interface::shim_mgmt::ERR_NO_SHIM_SERVER;
|
use shim_interface::shim_mgmt::ERR_NO_SHIM_SERVER;
|
||||||
use tokio::fs;
|
use tokio::fs;
|
||||||
use tokio::sync::{mpsc::Sender, RwLock};
|
use tokio::sync::{mpsc::Sender, RwLock};
|
||||||
@ -35,7 +35,6 @@ use virt_container::{
|
|||||||
use wasm_container::WasmContainer;
|
use wasm_container::WasmContainer;
|
||||||
|
|
||||||
use crate::shim_mgmt::server::MgmtServer;
|
use crate::shim_mgmt::server::MgmtServer;
|
||||||
use crate::static_resource::StaticResourceManager;
|
|
||||||
|
|
||||||
struct RuntimeHandlerManagerInner {
|
struct RuntimeHandlerManagerInner {
|
||||||
id: String,
|
id: String,
|
||||||
@ -423,14 +422,11 @@ fn load_config(spec: &oci::Spec, option: &Option<Vec<u8>>) -> Result<TomlConfig>
|
|||||||
// 2. If this is not a sandbox infrastructure container, but instead a standalone single container (analogous to "docker run..."),
|
// 2. If this is not a sandbox infrastructure container, but instead a standalone single container (analogous to "docker run..."),
|
||||||
// then the container spec itself will contain appropriate sizing information for the entire sandbox (since it is
|
// then the container spec itself will contain appropriate sizing information for the entire sandbox (since it is
|
||||||
// a single container.
|
// a single container.
|
||||||
if toml_config.runtime.static_sandbox_resource_mgmt {
|
let initial_size_manager =
|
||||||
info!(sl!(), "static resource management enabled");
|
InitialSizeManager::new(spec).context("failed to construct static resource manager")?;
|
||||||
let static_resource_manager = StaticResourceManager::new(spec)
|
initial_size_manager
|
||||||
.context("failed to construct static resource manager")?;
|
.setup_config(&mut toml_config)
|
||||||
static_resource_manager
|
.context("failed to setup static resource mgmt config")?;
|
||||||
.setup_config(&mut toml_config)
|
|
||||||
.context("failed to setup static resource mgmt config")?;
|
|
||||||
}
|
|
||||||
|
|
||||||
info!(sl!(), "get config content {:?}", &toml_config);
|
info!(sl!(), "get config content {:?}", &toml_config);
|
||||||
Ok(toml_config)
|
Ok(toml_config)
|
||||||
|
Loading…
Reference in New Issue
Block a user