diff --git a/rasp/librasp/Cargo.toml b/rasp/librasp/Cargo.toml index adeb7b21a..a47bae776 100644 --- a/rasp/librasp/Cargo.toml +++ b/rasp/librasp/Cargo.toml @@ -30,6 +30,7 @@ version-compare = "0.0.10" coarsetime = "0.1" wait-timeout = "0.2.0" lazy_static = "1.4.0" +cgroups-rs = "0.2.6" # plugins plugins = { path = "../../plugins/lib/rust"} diff --git a/rasp/librasp/src/lib.rs b/rasp/librasp/src/lib.rs index 766b01d13..4509cde96 100644 --- a/rasp/librasp/src/lib.rs +++ b/rasp/librasp/src/lib.rs @@ -20,9 +20,28 @@ pub mod async_command { use anyhow::{anyhow, Result}; use libc::{kill, SIGKILL}; use log::*; - + use cgroups_rs::{self, cgroup_builder::CgroupBuilder, CgroupPid, Controller}; use crate::comm::Control; + pub fn set_child_cgroup(pid: i32) -> Result<()> { + let hier = cgroups_rs::hierarchies::auto(); + let child_cg = CgroupBuilder::new("rasp_child_cgroup") + .memory() + .memory_hard_limit(1024 * 1024 * 200) + .done() + .cpu() + .quota(1000 * 10).done() + .build(hier); + // start + + + let mems: &cgroups_rs::memory::MemController = child_cg.controller_of().unwrap(); + mems.add_task(&CgroupPid::from(pid as u64))?; + let cpus: &cgroups_rs::cpu::CpuController = child_cg.controller_of().unwrap(); + cpus.add_task(&CgroupPid::from(pid as u64))?; + return Ok(()); + } + pub fn run_async_process(command: &mut Command) -> Result<(ExitStatus, String, String)> { // start let mut child = match command @@ -37,6 +56,7 @@ pub mod async_command { } }; let pid = child.id(); + set_child_cgroup(pid as i32)?; let (stdout, stderr) = (child.stdout.take(), child.stderr.take()); let child_ctrl = Control::new(); let mut wait_child_ctrl = child_ctrl.clone(); @@ -53,7 +73,7 @@ pub mod async_command { return Ok(status); } Ok(None) => { - sleep(Duration::from_secs(1)); + // sleep(Duration::from_secs(1)); } Err(e) => { warn!("attempting wait failed: {}", e); diff --git a/rasp/librasp/src/manager.rs b/rasp/librasp/src/manager.rs index 2673c2236..cbfb27c61 100644 --- a/rasp/librasp/src/manager.rs +++ b/rasp/librasp/src/manager.rs @@ -210,7 +210,7 @@ impl RASPManager { }) } } - let mut need_write_config = true; + let mut need_write_config = false; for m in messages.iter() { if m.message_type >= 12 && m.message_type <= 14 { need_write_config = false;