Merge pull request #1158 from liubin/fix/1156-fix-cpuset

handle vcpus properly utilized in the guest
This commit is contained in:
Peng Tao 2020-12-04 22:32:15 +08:00 committed by GitHub
commit 4bca7312c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 15 deletions

View File

@ -365,7 +365,9 @@ fn set_cpu_resources(cg: &cgroups::Cgroup, cpu: &LinuxCPU) -> Result<()> {
let cpuset_controller: &CpuSetController = cg.controller_of().unwrap();
if !cpu.cpus.is_empty() {
cpuset_controller.set_cpus(&cpu.cpus)?;
if let Err(e) = cpuset_controller.set_cpus(&cpu.cpus) {
warn!(sl!(), "write cpuset failed: {:?}", e);
}
}
if !cpu.mems.is_empty() {
@ -1016,11 +1018,11 @@ impl Manager {
})
}
pub fn update_cpuset_path(&self, cpuset_cpus: &str) -> Result<()> {
if cpuset_cpus == "" {
pub fn update_cpuset_path(&self, guest_cpuset: &str, container_cpuset: &str) -> Result<()> {
if guest_cpuset == "" {
return Ok(());
}
info!(sl!(), "update_cpuset_path to: {}", cpuset_cpus);
info!(sl!(), "update_cpuset_path to: {}", guest_cpuset);
let h = cgroups::hierarchies::auto();
let h = Box::new(&*h);
@ -1034,8 +1036,8 @@ impl Manager {
let h = cgroups::hierarchies::auto();
let h = Box::new(&*h);
let cg = load_or_create(h, &self.cpath);
let cpuset_controller: &CpuSetController = cg.controller_of().unwrap();
let path = cpuset_controller.path();
let container_cpuset_controller: &CpuSetController = cg.controller_of().unwrap();
let path = container_cpuset_controller.path();
let container_path = Path::new(path);
info!(sl!(), "container cpuset path: {:?}", &path);
@ -1044,11 +1046,9 @@ impl Manager {
if ancestor == root_path {
break;
}
if ancestor != container_path {
paths.push(ancestor);
}
paths.push(ancestor);
}
info!(sl!(), "paths to update cpuset: {:?}", &paths);
info!(sl!(), "parent paths to update cpuset: {:?}", &paths);
let mut i = paths.len();
loop {
@ -1064,10 +1064,20 @@ impl Manager {
.to_str()
.unwrap()
.trim_start_matches(root_path.to_str().unwrap());
info!(sl!(), "updating cpuset for path {:?}", &r_path);
info!(sl!(), "updating cpuset for parent path {:?}", &r_path);
let cg = load_or_create(h, &r_path);
let cpuset_controller: &CpuSetController = cg.controller_of().unwrap();
cpuset_controller.set_cpus(cpuset_cpus)?;
cpuset_controller.set_cpus(guest_cpuset)?;
}
if !container_cpuset.is_empty() {
info!(
sl!(),
"updating cpuset for container path: {:?} cpuset: {}",
&container_path,
container_cpuset
);
container_cpuset_controller.set_cpus(container_cpuset)?;
}
Ok(())

View File

@ -64,7 +64,7 @@ impl Manager {
})
}
pub fn update_cpuset_path(&self, _: &str) -> Result<()> {
pub fn update_cpuset_path(&self, _: &str, _: &str) -> Result<()> {
Ok(())
}

View File

@ -233,14 +233,29 @@ impl Sandbox {
online_memory(&self.logger)?;
}
let cpuset = rustjail_cgroups::fs::get_guest_cpuset()?;
let guest_cpuset = rustjail_cgroups::fs::get_guest_cpuset()?;
for (_, ctr) in self.containers.iter() {
let cpu = ctr
.config
.spec
.as_ref()
.unwrap()
.linux
.as_ref()
.unwrap()
.resources
.as_ref()
.unwrap()
.cpu
.as_ref();
let container_cpust = if let Some(c) = cpu { &c.cpus } else { "" };
info!(self.logger, "updating {}", ctr.id.as_str());
ctr.cgroup_manager
.as_ref()
.unwrap()
.update_cpuset_path(cpuset.as_str())?;
.update_cpuset_path(guest_cpuset.as_str(), &container_cpust)?;
}
Ok(())