mirror of
https://github.com/mii443/izoli.git
synced 2025-08-22 16:05:40 +00:00
add add/remove_subtree_control
This commit is contained in:
@ -1,7 +1,7 @@
|
|||||||
use izolilib::cgroup::cgroup::CGroup;
|
use izolilib::cgroup::{cgroup::CGroup, controller::Controller};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let cgroup = CGroup::new("").unwrap();
|
let cgroup = CGroup::new("test").unwrap();
|
||||||
println!("{:?}", cgroup.get_root_path());
|
println!("{:?}", cgroup.get_root_path());
|
||||||
println!("{}", cgroup.check_status());
|
println!("{}", cgroup.check_status());
|
||||||
println!("{:?}", cgroup.read("cgroup.type"));
|
println!("{:?}", cgroup.read("cgroup.type"));
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use std::{
|
use std::{
|
||||||
fs::{self, File},
|
fs::{self, File},
|
||||||
io::Read,
|
io::{Read, Write},
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
str::FromStr,
|
str::FromStr,
|
||||||
};
|
};
|
||||||
@ -38,6 +38,14 @@ impl CGroup {
|
|||||||
Ok(buf)
|
Ok(buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn write(&self, name: &str, data: &str) -> Result<(), std::io::Error> {
|
||||||
|
let path = self.get_file_path(name);
|
||||||
|
let mut file = File::options().append(true).open(path)?;
|
||||||
|
file.write_all(data.as_bytes())?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn check_status(&self) -> bool {
|
pub fn check_status(&self) -> bool {
|
||||||
let root = self.get_root_path();
|
let root = self.get_root_path();
|
||||||
|
|
||||||
@ -99,6 +107,35 @@ impl CGroup {
|
|||||||
self.get_limit_value("cgroup.max.descendants")
|
self.get_limit_value("cgroup.max.descendants")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// cgroup files write
|
||||||
|
|
||||||
|
pub fn add_subtree_control(&self, controllers: Vec<Controller>) -> Result<(), std::io::Error> {
|
||||||
|
let to_write = controllers
|
||||||
|
.iter()
|
||||||
|
.map(|controller| format!("+{}", controller))
|
||||||
|
.collect::<Vec<String>>()
|
||||||
|
.join(" ");
|
||||||
|
|
||||||
|
self.write("cgroup.subtree_control", &to_write)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn remove_subtree_control(
|
||||||
|
&self,
|
||||||
|
controllers: Vec<Controller>,
|
||||||
|
) -> Result<(), std::io::Error> {
|
||||||
|
let to_write = controllers
|
||||||
|
.iter()
|
||||||
|
.map(|controller| format!("-{}", controller))
|
||||||
|
.collect::<Vec<String>>()
|
||||||
|
.join(" ");
|
||||||
|
|
||||||
|
self.write("cgroup.subtree_control", &to_write)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
fn get_u32_list(&self, name: &str) -> Result<Vec<u32>, std::io::Error> {
|
fn get_u32_list(&self, name: &str) -> Result<Vec<u32>, std::io::Error> {
|
||||||
let procs = self
|
let procs = self
|
||||||
.read(name)?
|
.read(name)?
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use std::str::FromStr;
|
use std::{fmt, str::FromStr};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
pub enum Controller {
|
pub enum Controller {
|
||||||
@ -33,3 +33,19 @@ impl FromStr for Controller {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Controller {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
Controller::Cpu => write!(f, "cpu"),
|
||||||
|
Controller::Cpuset => write!(f, "cpuset"),
|
||||||
|
Controller::Memory => write!(f, "memory"),
|
||||||
|
Controller::Io => write!(f, "io"),
|
||||||
|
Controller::Hugetlb => write!(f, "hugetlb"),
|
||||||
|
Controller::Misc => write!(f, "misc"),
|
||||||
|
Controller::Pids => write!(f, "pids"),
|
||||||
|
Controller::Rdma => write!(f, "rdma"),
|
||||||
|
Controller::Unknown => write!(f, "unknown"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user