add memory.max

This commit is contained in:
mii
2024-10-29 17:44:56 +09:00
parent 4af5995a58
commit 5b2e01f7be
3 changed files with 27 additions and 1 deletions

View File

@ -29,6 +29,7 @@ fn main() {
max: CGroupLimitValue::Max, max: CGroupLimitValue::Max,
period: 100000, period: 100000,
}), }),
memory_max: Some(CGroupLimitValue::Value(1024 * 1024 * 2)),
..Default::default() ..Default::default()
}), }),
new_net: true, new_net: true,

View File

@ -53,6 +53,11 @@ impl CGroup {
self.set_cpu_max(cpu_max)?; self.set_cpu_max(cpu_max)?;
} }
if let Some(memory_max) = &option.memory_max {
info!("setting memory.max");
self.set_memory_max(memory_max)?;
}
Ok(()) Ok(())
} }
@ -204,6 +209,25 @@ impl CGroup {
self.write("cpu.max", &to_write) self.write("cpu.max", &to_write)
} }
// memory read
pub fn get_memory_max(&self) -> Result<CGroupLimitValue<u32>, std::io::Error> {
let max = self.read("memory.max")?;
Ok(CGroupLimitValue::from_str(&max).unwrap())
}
// memory write
pub fn set_memory_max(
&self,
memory_limit: &CGroupLimitValue<u32>,
) -> Result<(), std::io::Error> {
let to_write = memory_limit.to_string();
self.write("memory.max", &to_write)
}
fn write_value<T>(&self, name: &str, value: T) -> Result<(), std::io::Error> fn write_value<T>(&self, name: &str, value: T) -> Result<(), std::io::Error>
where where
T: fmt::Display, T: fmt::Display,

View File

@ -1,6 +1,7 @@
use super::cpu_limit::CpuLimit; use super::{cpu_limit::CpuLimit, limit_value::CGroupLimitValue};
#[derive(Debug, Clone, Copy, Default)] #[derive(Debug, Clone, Copy, Default)]
pub struct CGroupOption { pub struct CGroupOption {
pub cpu_max: Option<CpuLimit>, pub cpu_max: Option<CpuLimit>,
pub memory_max: Option<CGroupLimitValue<u32>>,
} }