From 61181378309633b6b95c7f1f28cf92770695c6c8 Mon Sep 17 00:00:00 2001 From: futpib Date: Wed, 10 Jan 2024 19:06:11 +0400 Subject: [PATCH 1/2] Add oom_score_adj methods --- procfs-core/src/partitions.rs | 9 ++++----- procfs/src/process/mod.rs | 21 +++++++++++++++++++-- procfs/src/process/tests.rs | 4 ++++ 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/procfs-core/src/partitions.rs b/procfs-core/src/partitions.rs index ef297d5a..41a65869 100644 --- a/procfs-core/src/partitions.rs +++ b/procfs-core/src/partitions.rs @@ -18,7 +18,7 @@ pub struct PartitionEntry { /// Number of 1024 byte blocks pub blocks: u64, /// Device name - pub name: String + pub name: String, } impl super::FromBufRead for Vec { @@ -37,9 +37,9 @@ impl super::FromBufRead for Vec { let partition_entry = PartitionEntry { major, - minor, - blocks, - name + minor, + blocks, + name, }; vec.push(partition_entry); @@ -49,7 +49,6 @@ impl super::FromBufRead for Vec { } } - #[test] fn test_partitions() { use crate::FromBufRead; diff --git a/procfs/src/process/mod.rs b/procfs/src/process/mod.rs index 111aed2d..d05ddf7a 100644 --- a/procfs/src/process/mod.rs +++ b/procfs/src/process/mod.rs @@ -587,12 +587,29 @@ impl Process { /// A higher score means that the process is more likely to be selected by the OOM-killer. /// The basis for this score is the amount of memory used by the process, plus other factors. /// + /// Values range from 0 (never kill) to 1000 (always kill) inclusive. + /// /// (Since linux 2.6.11) - pub fn oom_score(&self) -> ProcResult { + pub fn oom_score(&self) -> ProcResult { let mut file = FileWrapper::open_at(&self.root, &self.fd, "oom_score")?; let mut oom = String::new(); file.read_to_string(&mut oom)?; - Ok(from_str!(u32, oom.trim())) + Ok(from_str!(u16, oom.trim())) + } + + /// Adjust score value is added to the oom score before choosing processes to kill. + /// + /// Values range from -1000 (never kill) to 1000 (always kill) inclusive. + pub fn oom_score_adj(&self) -> ProcResult { + let mut file = FileWrapper::open_at(&self.root, &self.fd, "oom_score_adj")?; + let mut oom = String::new(); + file.read_to_string(&mut oom)?; + Ok(from_str!(i16, oom.trim())) + } + + pub fn set_oom_score_adj(&self, new_oom_score_adj: i16) -> ProcResult<()> { + let path = self.root.join("oom_score_adj"); + write_value(path, new_oom_score_adj) } /// Set process memory information diff --git a/procfs/src/process/tests.rs b/procfs/src/process/tests.rs index c31181b5..ec7e4e30 100644 --- a/procfs/src/process/tests.rs +++ b/procfs/src/process/tests.rs @@ -174,6 +174,10 @@ fn test_all() { check_unwrap(&prc, prc.mountinfo()); check_unwrap(&prc, prc.mountstats()); check_unwrap(&prc, prc.oom_score()); + if let Some(oom_score_adj) = check_unwrap(&prc, prc.oom_score_adj()) { + assert!(oom_score_adj >= -1000 && oom_score_adj <= 1000); + check_unwrap(&prc, prc.set_oom_score_adj(oom_score_adj)); + } if let Some(tasks) = check_unwrap(&prc, prc.tasks()) { for task in tasks { From 6dc4765684556664098ff58a184b4b401d9c3842 Mon Sep 17 00:00:00 2001 From: futpib Date: Wed, 10 Jan 2024 19:07:36 +0400 Subject: [PATCH 2/2] Update support.md --- support.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/support.md b/support.md index 69e7c19c..37446335 100644 --- a/support.md +++ b/support.md @@ -36,7 +36,7 @@ This is an approximate list of all the files under the `/proc` mount, and an ind * [ ] `/proc/[pid]/numa_maps` * [ ] `/proc/[pid]/oom_adj` * [x] `/proc/[pid]/oom_score` - * [ ] `/proc/[pid]/oom_score_adj` + * [x] `/proc/[pid]/oom_score_adj` * [ ] `/proc/[pid]/pagemap` * [ ] `/proc/[pid]/personality` * [x] `/proc/[pid]/root`