From efb98108fcd23b655fbb7a329d91d35f0c65d1e4 Mon Sep 17 00:00:00 2001 From: Tim Zhang Date: Thu, 29 Oct 2020 19:31:27 +0800 Subject: [PATCH 1/3] Fix warnings Got: cargo build --target x86_64-unknown-linux-musl Compiling cgroups v0.1.1-alpha.0 (/home/tim/project/cgroups-rs) warning: unused import: `nix::sys::statfs` --> src/hierarchies.rs:220:5 | 220 | use nix::sys::statfs; | ^^^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default warning: 1 warning emitted Signed-off-by: Tim Zhang --- src/hierarchies.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/hierarchies.rs b/src/hierarchies.rs index b47451d..e498f70 100644 --- a/src/hierarchies.rs +++ b/src/hierarchies.rs @@ -8,7 +8,6 @@ //! //! Currently, we only support the cgroupv1 hierarchy, but in the future we will add support for //! the Unified Hierarchy. -use nix::sys::statfs; use std::fs::{self, File}; use std::io::BufRead; @@ -220,6 +219,8 @@ pub const UNIFIED_MOUNTPOINT: &'static str = "/sys/fs/cgroup"; #[cfg(all(target_os = "linux", not(target_env = "musl")))] pub fn is_cgroup2_unified_mode() -> bool { + use nix::sys::statfs; + let path = Path::new(UNIFIED_MOUNTPOINT); let fs_stat = statfs::statfs(path); if fs_stat.is_err() { From 42eb32765bdef575eba3719abbd966958bd17671 Mon Sep 17 00:00:00 2001 From: Tim Zhang Date: Thu, 29 Oct 2020 19:32:55 +0800 Subject: [PATCH 2/3] Make methods to_controller and controller_name of Subsystem public We need these functions to detect whether subsystem path exists. Signed-off-by: Tim Zhang --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c910371..0761631 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -619,7 +619,7 @@ impl Subsystem { } } - fn to_controller(&self) -> &dyn Controller { + pub fn to_controller(&self) -> &dyn Controller { match self { Subsystem::Pid(cont) => cont, Subsystem::Mem(cont) => cont, @@ -638,7 +638,7 @@ impl Subsystem { } } - fn controller_name(&self) -> String { + pub fn controller_name(&self) -> String { self.to_controller().control_type().to_string() } } From 6f9e89572e917a58305051f50dd8875ff173af03 Mon Sep 17 00:00:00 2001 From: Tim Zhang Date: Thu, 29 Oct 2020 19:35:39 +0800 Subject: [PATCH 3/3] MemController: add method reset_max_usage We need a method to reset the max memory usage recorded. Signed-off-by: Tim Zhang --- src/memory.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/memory.rs b/src/memory.rs index 9f641e8..9b1e804 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -733,6 +733,15 @@ impl MemController { }) } + /// Reset the max memory usage recorded + pub fn reset_max_usage(&self) -> Result<()> { + self.open_path("memory.max_usage_in_bytes", true) + .and_then(|mut file| { + file.write_all("0".to_string().as_ref()) + .map_err(|e| Error::with_cause(WriteFailed, e)) + }) + } + /// Set the memory usage limit of the control group, in bytes. pub fn set_limit(&self, limit: i64) -> Result<()> { let mut file = "memory.limit_in_bytes";