Merge pull request #255 from sched-ext/rustland-core-relax-mm-constraint

scx_rustland_core: relax compact unevictable memory constraint
This commit is contained in:
Andrea Righi 2024-04-30 18:29:05 +02:00 committed by GitHub
commit 33092f096e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 49 additions and 25 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "scx_rustland_core"
version = "0.3.0"
version = "0.4.0"
edition = "2021"
authors = ["Andrea Righi <andrea.righi@canonical.com>"]
license = "GPL-2.0-only"

View File

@ -6,6 +6,7 @@
use std::alloc::{GlobalAlloc, Layout};
use std::fs::File;
use std::io::{BufRead, BufReader, Write};
use std::num::ParseIntError;
use buddy_alloc::{BuddyAllocParam, FastAllocParam, NonThreadsafeAlloc};
@ -47,7 +48,10 @@ pub struct UserAllocator {
impl UserAllocator {
pub fn lock_memory(&self) {
unsafe {
VM.save();
match VM.save() {
Ok(_) => {}
Err(res) => eprintln!("WARNING: {}\n", res),
};
// Call setrlimit to set the locked-in-memory limit to unlimited.
let new_rlimit = libc::rlimit {
@ -69,7 +73,10 @@ impl UserAllocator {
pub fn unlock_memory(&self) {
unsafe {
VM.restore();
match VM.restore() {
Ok(_) => {}
Err(res) => eprintln!("WARNING: {}\n", res),
}
};
}
}
@ -104,44 +111,61 @@ struct VmSettings {
impl VmSettings {
// Return the content of a procfs file as i32.
fn read_procfs(&self, file_path: &str) -> i32 {
let file = File::open(file_path).expect(&format!("Failed to open {}", file_path));
fn read_procfs(&self, file_path: &str) -> Result<i32, String> {
// Attempt to open the file
let file = match File::open(file_path) {
Ok(f) => f,
Err(err) => return Err(format!("Failed to open {}: {}", file_path, err)),
};
let reader = BufReader::new(file);
if let Some(Ok(line)) = reader.lines().next() {
let value: i32 = match line.trim().parse() {
Ok(v) => v,
Err(_) => panic!("Failed to parse {}", file_path),
};
value
let value: Result<i32, ParseIntError> = line.trim().parse();
match value {
Ok(v) => Ok(v),
Err(err) => Err(format!("Failed to parse {}: {}", file_path, err)),
}
} else {
panic!("empty {}", file_path);
Err(format!("File is empty: {}", file_path))
}
}
// Write an i32 to a file in procfs.
fn write_procfs(&self, file_path: &str, value: i32) {
let mut file = File::create(file_path).expect(&format!("Failed to open {}", file_path));
file.write_all(value.to_string().as_bytes())
.expect(&format!("Failed to write to {}", file_path));
fn write_procfs(&self, file_path: &str, value: i32) -> Result<(), String> {
// Attempt to create or open the file
let mut file = match File::create(file_path) {
Ok(f) => f,
Err(err) => return Err(format!("Failed to open {}: {}", file_path, err)),
};
// Attempt to write the value to the file
if let Err(err) = write!(file, "{}", value) {
return Err(format!("Failed to write to {}: {}", file_path, err));
}
Ok(()) // Return Ok if writing was successful
}
// Save all the sysctl VM settings in the internal state.
fn save(&self) {
fn save(&self) -> Result<(), String> {
let compact_unevictable_allowed = "/proc/sys/vm/compact_unevictable_allowed";
let value = self.read_procfs(compact_unevictable_allowed);
let value = self.read_procfs(compact_unevictable_allowed)?;
unsafe {
VM.compact_unevictable_allowed = value;
};
self.write_procfs(compact_unevictable_allowed, 0);
self.write_procfs(compact_unevictable_allowed, 0)?;
Ok(())
}
// Restore all the previous sysctl vm settings.
fn restore(&self) {
fn restore(&self) -> Result<(), String> {
let compact_unevictable_allowed = "/proc/sys/vm/compact_unevictable_allowed";
let value = unsafe { VM.compact_unevictable_allowed };
self.write_procfs(compact_unevictable_allowed, value);
self.write_procfs(compact_unevictable_allowed, value)?;
Ok(())
}
}

View File

@ -12,11 +12,11 @@ ctrlc = { version = "3.1", features = ["termination"] }
libbpf-rs = "0.23"
libc = "0.2.137"
scx_utils = { path = "../../../rust/scx_utils", version = "0.8" }
scx_rustland_core = { path = "../../../rust/scx_rustland_core", version = "0.3" }
scx_rustland_core = { path = "../../../rust/scx_rustland_core", version = "0.4" }
[build-dependencies]
scx_utils = { path = "../../../rust/scx_utils", version = "0.8" }
scx_rustland_core = { path = "../../../rust/scx_rustland_core", version = "0.3" }
scx_rustland_core = { path = "../../../rust/scx_rustland_core", version = "0.4" }
[features]
enable_backtrace = []

View File

@ -16,12 +16,12 @@ libc = "0.2.137"
log = "0.4.17"
ordered-float = "3.4.0"
scx_utils = { path = "../../../rust/scx_utils", version = "0.8" }
scx_rustland_core = { path = "../../../rust/scx_rustland_core", version = "0.3" }
scx_rustland_core = { path = "../../../rust/scx_rustland_core", version = "0.4" }
simplelog = "0.12.0"
[build-dependencies]
scx_utils = { path = "../../../rust/scx_utils", version = "0.8" }
scx_rustland_core = { path = "../../../rust/scx_rustland_core", version = "0.3" }
scx_rustland_core = { path = "../../../rust/scx_rustland_core", version = "0.4" }
[features]
enable_backtrace = []