From 5b2e01f7bee61543c0490a4f6ae9ada4cec65c4d Mon Sep 17 00:00:00 2001 From: mii Date: Tue, 29 Oct 2024 17:44:56 +0900 Subject: [PATCH] add memory.max --- src/bin/izoli.rs | 1 + src/cgroup/cgroup.rs | 24 ++++++++++++++++++++++++ src/cgroup/cgroup_option.rs | 3 ++- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/bin/izoli.rs b/src/bin/izoli.rs index b260bd5..3fcb99c 100644 --- a/src/bin/izoli.rs +++ b/src/bin/izoli.rs @@ -29,6 +29,7 @@ fn main() { max: CGroupLimitValue::Max, period: 100000, }), + memory_max: Some(CGroupLimitValue::Value(1024 * 1024 * 2)), ..Default::default() }), new_net: true, diff --git a/src/cgroup/cgroup.rs b/src/cgroup/cgroup.rs index 50202b6..0a99316 100644 --- a/src/cgroup/cgroup.rs +++ b/src/cgroup/cgroup.rs @@ -53,6 +53,11 @@ impl CGroup { 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(()) } @@ -204,6 +209,25 @@ impl CGroup { self.write("cpu.max", &to_write) } + // memory read + + pub fn get_memory_max(&self) -> Result, 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, + ) -> Result<(), std::io::Error> { + let to_write = memory_limit.to_string(); + + self.write("memory.max", &to_write) + } + fn write_value(&self, name: &str, value: T) -> Result<(), std::io::Error> where T: fmt::Display, diff --git a/src/cgroup/cgroup_option.rs b/src/cgroup/cgroup_option.rs index 78956e0..e33631a 100644 --- a/src/cgroup/cgroup_option.rs +++ b/src/cgroup/cgroup_option.rs @@ -1,6 +1,7 @@ -use super::cpu_limit::CpuLimit; +use super::{cpu_limit::CpuLimit, limit_value::CGroupLimitValue}; #[derive(Debug, Clone, Copy, Default)] pub struct CGroupOption { pub cpu_max: Option, + pub memory_max: Option>, }