From 61a0957a65cd60aa9de4520c65b4d65ba9afd952 Mon Sep 17 00:00:00 2001 From: Tim Zhang Date: Mon, 7 Dec 2020 15:53:14 +0800 Subject: [PATCH] Fix set_swappiness in cgroup v2 The file should be memory.swap.max in cgroup v2. Signed-off-by: Tim Zhang --- src/memory.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/memory.rs b/src/memory.rs index 7e87a96..6243a15 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -818,11 +818,15 @@ impl MemController { /// /// Note that a value of zero does not imply that the process will not be swapped out. pub fn set_swappiness(&self, swp: u64) -> Result<()> { - self.open_path("memory.swappiness", true) - .and_then(|mut file| { - file.write_all(swp.to_string().as_ref()) - .map_err(|e| Error::with_cause(WriteFailed, e)) - }) + let mut file = "memory.swappiness"; + if self.v2 { + file = "memory.swap.max" + } + + self.open_path(file, true).and_then(|mut file| { + file.write_all(swp.to_string().as_ref()) + .map_err(|e| Error::with_cause(WriteFailed, e)) + }) } pub fn disable_oom_killer(&self) -> Result<()> {