mirror of
https://github.com/mii443/izoli.git
synced 2025-08-22 16:05:40 +00:00
add set cpu.max, cgroup option
This commit is contained in:
@ -1,6 +1,6 @@
|
|||||||
use izolilib::{
|
use izolilib::{
|
||||||
cgroup::cgroup::CGroup,
|
cgroup::{cgroup::CGroup, cgroup_option::CGroupOption, cpu_limit::CpuLimit},
|
||||||
izolibox::{CGroupOption, IzoliBox},
|
izolibox::IzoliBox,
|
||||||
};
|
};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
@ -21,7 +21,15 @@ fn main() {
|
|||||||
.add_subtree_control(cgroup.get_controllers().unwrap())
|
.add_subtree_control(cgroup.get_controllers().unwrap())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let izolibox = IzoliBox::new(1, Some(CGroupOption {}));
|
let izolibox = IzoliBox::new(
|
||||||
|
1,
|
||||||
|
Some(CGroupOption {
|
||||||
|
cpu_max: Some(CpuLimit {
|
||||||
|
max: izolilib::cgroup::limit_value::CGroupLimitValue::Value(1000),
|
||||||
|
period: 100000,
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
);
|
||||||
let pid = izolibox
|
let pid = izolibox
|
||||||
.enter(Box::new(|| {
|
.enter(Box::new(|| {
|
||||||
println!("Isolated process: {}", std::process::id());
|
println!("Isolated process: {}", std::process::id());
|
||||||
|
@ -7,8 +7,8 @@ use std::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
cgroup_stat::CGroupStat, controller::Controller, cpu_limit::CpuLimit,
|
cgroup_option::CGroupOption, cgroup_stat::CGroupStat, controller::Controller,
|
||||||
limit_value::CGroupLimitValue,
|
cpu_limit::CpuLimit, limit_value::CGroupLimitValue,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct CGroup {
|
pub struct CGroup {
|
||||||
@ -41,6 +41,14 @@ impl CGroup {
|
|||||||
fs::create_dir_all(root)
|
fs::create_dir_all(root)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn apply_options(&self, option: &CGroupOption) -> Result<(), std::io::Error> {
|
||||||
|
if let Some(cpu_max) = &option.cpu_max {
|
||||||
|
self.set_cpu_max(cpu_max)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn enter(&self) -> Result<(), std::io::Error> {
|
pub fn enter(&self) -> Result<(), std::io::Error> {
|
||||||
let pid = std::process::id();
|
let pid = std::process::id();
|
||||||
|
|
||||||
@ -178,6 +186,14 @@ impl CGroup {
|
|||||||
Ok(CpuLimit::from_str(&max).unwrap())
|
Ok(CpuLimit::from_str(&max).unwrap())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// cpu write
|
||||||
|
|
||||||
|
pub fn set_cpu_max(&self, cpu_limit: &CpuLimit) -> Result<(), std::io::Error> {
|
||||||
|
let to_write = cpu_limit.to_string();
|
||||||
|
|
||||||
|
self.write("cpu.max", &to_write)
|
||||||
|
}
|
||||||
|
|
||||||
fn write_value<T>(&self, name: &str, value: T) -> Result<(), std::io::Error>
|
fn write_value<T>(&self, name: &str, value: T) -> Result<(), std::io::Error>
|
||||||
where
|
where
|
||||||
T: fmt::Display,
|
T: fmt::Display,
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
pub mod cgroup;
|
pub mod cgroup;
|
||||||
|
pub mod cgroup_option;
|
||||||
pub mod cgroup_stat;
|
pub mod cgroup_stat;
|
||||||
pub mod controller;
|
pub mod controller;
|
||||||
pub mod cpu_limit;
|
pub mod cpu_limit;
|
||||||
|
@ -4,12 +4,10 @@ use nix::{
|
|||||||
unistd::Pid,
|
unistd::Pid,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::cgroup::cgroup::CGroup;
|
use crate::cgroup::{cgroup::CGroup, cgroup_option::CGroupOption};
|
||||||
|
|
||||||
const STACK_SIZE: usize = 8192;
|
const STACK_SIZE: usize = 8192;
|
||||||
|
|
||||||
pub struct CGroupOption {}
|
|
||||||
|
|
||||||
pub struct IzoliBox {
|
pub struct IzoliBox {
|
||||||
pub id: usize,
|
pub id: usize,
|
||||||
pub cgroup_option: Option<CGroupOption>,
|
pub cgroup_option: Option<CGroupOption>,
|
||||||
@ -27,8 +25,9 @@ impl IzoliBox {
|
|||||||
| CloneFlags::CLONE_NEWIPC
|
| CloneFlags::CLONE_NEWIPC
|
||||||
| CloneFlags::CLONE_NEWPID;
|
| CloneFlags::CLONE_NEWPID;
|
||||||
|
|
||||||
if let Some(_cgroup_option) = &self.cgroup_option {
|
if let Some(cgroup_option) = &self.cgroup_option {
|
||||||
let cgroup = CGroup::new(&format!("izoli/box_{}", self.id)).unwrap();
|
let cgroup = CGroup::new(&format!("izoli/box_{}", self.id)).unwrap();
|
||||||
|
cgroup.apply_options(cgroup_option).unwrap();
|
||||||
cgroup.enter().unwrap();
|
cgroup.enter().unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user