diff --git a/src/agent/rustjail/src/cgroups/fs/mod.rs b/src/agent/rustjail/src/cgroups/fs/mod.rs index 492aad33d2..e099086fc6 100644 --- a/src/agent/rustjail/src/cgroups/fs/mod.rs +++ b/src/agent/rustjail/src/cgroups/fs/mod.rs @@ -1008,11 +1008,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); @@ -1026,8 +1026,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); @@ -1036,11 +1036,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 { @@ -1056,10 +1054,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(()) diff --git a/src/agent/rustjail/src/cgroups/mock.rs b/src/agent/rustjail/src/cgroups/mock.rs new file mode 100644 index 0000000000..e1603c8468 --- /dev/null +++ b/src/agent/rustjail/src/cgroups/mock.rs @@ -0,0 +1,74 @@ +// Copyright (c) 2020 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +use protobuf::{CachedSize, SingularPtrField, UnknownFields}; + +use crate::cgroups::Manager as CgroupManager; +use crate::protocols::agent::{BlkioStats, CgroupStats, CpuStats, MemoryStats, PidsStats}; +use anyhow::Result; +use cgroups::freezer::FreezerState; +use libc::{self, pid_t}; +use oci::LinuxResources; +use std::collections::HashMap; +use std::string::String; + +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct Manager { + pub paths: HashMap, + pub mounts: HashMap, + pub cpath: String, +} + +impl CgroupManager for Manager { + fn apply(&self, _: pid_t) -> Result<()> { + Ok(()) + } + + fn set(&self, _: &LinuxResources, _: bool) -> Result<()> { + Ok(()) + } + + fn get_stats(&self) -> Result { + Ok(CgroupStats { + cpu_stats: SingularPtrField::some(CpuStats::default()), + memory_stats: SingularPtrField::some(MemoryStats::new()), + pids_stats: SingularPtrField::some(PidsStats::new()), + blkio_stats: SingularPtrField::some(BlkioStats::new()), + hugetlb_stats: HashMap::new(), + unknown_fields: UnknownFields::default(), + cached_size: CachedSize::default(), + }) + } + + fn freeze(&self, _: FreezerState) -> Result<()> { + Ok(()) + } + + fn destroy(&mut self) -> Result<()> { + Ok(()) + } + + fn get_pids(&self) -> Result> { + Ok(Vec::new()) + } +} + +impl Manager { + pub fn new(cpath: &str) -> Result { + Ok(Self { + paths: HashMap::new(), + mounts: HashMap::new(), + cpath: cpath.to_string(), + }) + } + + pub fn update_cpuset_path(&self, _: &str, _: &str) -> Result<()> { + Ok(()) + } + + pub fn get_cg_path(&self, _: &str) -> Option { + Some("".to_string()) + } +} diff --git a/src/agent/src/sandbox.rs b/src/agent/src/sandbox.rs index 5ba25218bc..53bf053dfd 100644 --- a/src/agent/src/sandbox.rs +++ b/src/agent/src/sandbox.rs @@ -236,14 +236,29 @@ impl Sandbox { return Ok(()); } - 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(())