Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add oom_score_adj Process methods #298

Merged
merged 2 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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