add get_controllers

This commit is contained in:
mii443
2024-10-17 05:59:47 +00:00
parent c827b2f5c7
commit bf62472a7f
3 changed files with 16 additions and 11 deletions

View File

@ -5,5 +5,6 @@ fn main() {
let cgroup = CGroup::new("test").unwrap();
println!("{:?}", cgroup.get_root_path());
println!("{}", cgroup.check_status());
println!("{:?}", cgroup.read("cgroup.controllers"));
println!("{:?}", cgroup.read("cgroup.type"));
println!("{:?}", cgroup.get_controllers());
}

View File

@ -2,18 +2,10 @@ use std::{
fs::{self, File},
io::Read,
path::{Path, PathBuf},
str::FromStr,
};
pub enum Controller {
Cpu,
Cpuset,
Memory,
Io,
Hugetlb,
Misc,
Pids,
Rdma,
}
use super::controller::Controller;
pub struct CGroup {
pub path: PathBuf,
@ -63,4 +55,15 @@ impl CGroup {
cgroup_root.join(&self.path)
}
pub fn get_controllers(&self) -> Result<Vec<Controller>, std::io::Error> {
let controllers = self
.read("cgroup.controllers")?
.trim()
.split(" ")
.map(|controller| Controller::from_str(controller).unwrap_or(Controller::Unknown))
.collect();
Ok(controllers)
}
}

View File

@ -1 +1,2 @@
pub mod cgroup;
pub mod controller;