Last active
August 13, 2023 06:17
-
-
Save jcaesar/2905b680aa71e971cebbbbf8d601a3d5 to your computer and use it in GitHub Desktop.
x
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/target | |
/Cargo.lock |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "pressure_valve" | |
version = "0.1.0" | |
edition = "2021" | |
[[example]] | |
name = "cv1" | |
path = "cv1.rs" | |
[[example]] | |
name = "cv2" | |
path = "cv2.rs" | |
[dependencies] | |
inotify = { version = "0.10.2", default-features = false } | |
nix = { version = "0.26.2", default-features = false, features = ["event", "fs"] } | |
procfs = { version = "0.15.1", default-features = false } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use nix::{ | |
sys::eventfd::{eventfd, EfdFlags}, | |
unistd::read, | |
}; | |
use std::{fs::File, io::Write, os::fd::AsRawFd, path::PathBuf, time::Instant}; | |
fn main() { | |
let start = Instant::now(); | |
let base = PathBuf::from("/sys/fs/cgroup/memory/"); | |
let plf = File::options() | |
.read(true) | |
.open(base.join("memory.pressure_level")) | |
.expect("memory.pressure_level not available"); | |
let mut ctl = File::options() | |
.write(true) | |
.open(base.join("cgroup.event_control")) | |
.expect("cgroup.event_control not available"); | |
for lv in ["low", "medium", "critical"] { | |
let efd = eventfd(0, EfdFlags::EFD_CLOEXEC).unwrap(); | |
ctl.write_all(format!("{} {} {}", efd, plf.as_raw_fd(), lv).as_bytes()) | |
.expect("Register event"); | |
std::thread::spawn(move || loop { | |
read(efd, &mut [0; 8]).expect("Wait for event"); | |
println!("{} {:.6}", lv, start.elapsed().as_secs_f32()); | |
}); | |
} | |
let mut hog = vec![0usize; 0]; | |
loop { | |
hog.push(hog.len()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::{path::PathBuf, time::Instant}; | |
fn main() { | |
let start = Instant::now(); | |
let group = procfs::process::Process::myself() | |
.unwrap() | |
.cgroups() | |
.unwrap() | |
.into_iter() | |
.find(|g| g.hierarchy == 0) | |
.expect("No v2 cgroup in /proc/self/cgroup"); | |
let path = PathBuf::from("/sys/fs/cgroup") | |
.join(group.pathname.strip_prefix("/").unwrap()) | |
.join("memory.events.local"); | |
let mut inotify = inotify::Inotify::init().expect("Can't use inotify"); | |
inotify | |
.watches() | |
.add(&path, inotify::WatchMask::MODIFY) | |
.expect("Failed to add file watch"); | |
std::thread::spawn(move || loop { | |
let buf = &mut [0; 1024]; | |
inotify.read_events_blocking(buf).unwrap(); | |
let content = std::fs::read(&path).unwrap(); | |
let content = std::str::from_utf8(&content).unwrap(); | |
println!( | |
"{} {:.6}", | |
content.replace("\n", " "), | |
start.elapsed().as_secs_f32() | |
); | |
}); | |
let mut hog = vec![0usize; 0]; | |
loop { | |
hog.push(hog.len()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment