Skip to content

Commit

Permalink
Merge pull request #298 from futpib/oom_score_adj
Browse files Browse the repository at this point in the history
Add oom_score_adj Process methods
  • Loading branch information
eminence authored Feb 3, 2024
2 parents 7491fd3 + 6dc4765 commit a8bda9e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
9 changes: 4 additions & 5 deletions procfs-core/src/partitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PartitionEntry> {
Expand All @@ -37,9 +37,9 @@ impl super::FromBufRead for Vec<PartitionEntry> {

let partition_entry = PartitionEntry {
major,
minor,
blocks,
name
minor,
blocks,
name,
};

vec.push(partition_entry);
Expand All @@ -49,7 +49,6 @@ impl super::FromBufRead for Vec<PartitionEntry> {
}
}


#[test]
fn test_partitions() {
use crate::FromBufRead;
Expand Down
21 changes: 19 additions & 2 deletions procfs/src/process/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32> {
pub fn oom_score(&self) -> ProcResult<u16> {
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<i16> {
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
Expand Down
4 changes: 4 additions & 0 deletions procfs/src/process/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion support.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down

0 comments on commit a8bda9e

Please sign in to comment.