pub struct Cgroup { /* private fields */ }
Expand description
A control group is the central structure to this crate.
§What are control groups?
Lifting over from the Linux kernel sources:
Control Groups provide a mechanism for aggregating/partitioning sets of tasks, and all their future children, into hierarchical groups with specialized behaviour.
This crate is an attempt at providing a Rust-native way of managing these cgroups.
Implementations§
Source§impl Cgroup
impl Cgroup
pub fn v2(&self) -> bool
Sourcepub fn new<P: AsRef<Path>>(hier: Box<dyn Hierarchy>, path: P) -> Result<Cgroup>
pub fn new<P: AsRef<Path>>(hier: Box<dyn Hierarchy>, path: P) -> Result<Cgroup>
Create a new control group in the hierarchy hier
, with name path
.
Returns a handle to the control group that can be used to manipulate it.
Sourcepub fn new_with_specified_controllers<P: AsRef<Path>>(
hier: Box<dyn Hierarchy>,
path: P,
specified_controllers: Option<Vec<String>>,
) -> Result<Cgroup>
pub fn new_with_specified_controllers<P: AsRef<Path>>( hier: Box<dyn Hierarchy>, path: P, specified_controllers: Option<Vec<String>>, ) -> Result<Cgroup>
Create a new control group in the hierarchy hier
, with name path
.
Returns a handle to the control group that can be used to manipulate it.
Sourcepub fn new_with_relative_paths<P: AsRef<Path>>(
hier: Box<dyn Hierarchy>,
path: P,
relative_paths: HashMap<String, String>,
) -> Result<Cgroup>
pub fn new_with_relative_paths<P: AsRef<Path>>( hier: Box<dyn Hierarchy>, path: P, relative_paths: HashMap<String, String>, ) -> Result<Cgroup>
Create a new control group in the hierarchy hier
, with name path
and relative_paths
Returns a handle to the control group that can be used to manipulate it.
Note that this method is only meaningful for cgroup v1, call it is equivalent to call new
in the v2 mode.
Sourcepub fn load<P: AsRef<Path>>(hier: Box<dyn Hierarchy>, path: P) -> Cgroup
pub fn load<P: AsRef<Path>>(hier: Box<dyn Hierarchy>, path: P) -> Cgroup
Create a handle for a control group in the hierarchy hier
, with name path
.
Returns a handle to the control group (that possibly does not exist until create()
has
been called on the cgroup.
Sourcepub fn load_with_specified_controllers<P: AsRef<Path>>(
hier: Box<dyn Hierarchy>,
path: P,
specified_controllers: Vec<String>,
) -> Cgroup
pub fn load_with_specified_controllers<P: AsRef<Path>>( hier: Box<dyn Hierarchy>, path: P, specified_controllers: Vec<String>, ) -> Cgroup
Create a handle for a specified control group in the hierarchy hier
, with name path
.
Returns a handle to the control group (that possibly does not exist until create()
has
been called on the cgroup.
Sourcepub fn load_with_relative_paths<P: AsRef<Path>>(
hier: Box<dyn Hierarchy>,
path: P,
relative_paths: HashMap<String, String>,
) -> Cgroup
pub fn load_with_relative_paths<P: AsRef<Path>>( hier: Box<dyn Hierarchy>, path: P, relative_paths: HashMap<String, String>, ) -> Cgroup
Create a handle for a control group in the hierarchy hier
, with name path
and relative_paths
Returns a handle to the control group (that possibly does not exist until create()
has
been called on the cgroup.
Note that this method is only meaningful for cgroup v1, call it is equivalent to call load
in the v2 mode
Sourcepub fn subsystems(&self) -> &Vec<Subsystem>
pub fn subsystems(&self) -> &Vec<Subsystem>
The list of subsystems that this control group supports.
Sourcepub fn delete(&self) -> Result<()>
pub fn delete(&self) -> Result<()>
Deletes the control group.
Note that this function makes no effort in cleaning up the descendant and the underlying system call will fail if there are any descendants. Thus, one should check whether it was actually removed, and remove the descendants first if not. In the future, this behavior will change.
Sourcepub fn apply(&self, res: &Resources) -> Result<()>
pub fn apply(&self, res: &Resources) -> Result<()>
Apply a set of resource limits to the control group.
Sourcepub fn controller_of<'a, T>(&'a self) -> Option<&'a T>
pub fn controller_of<'a, T>(&'a self) -> Option<&'a T>
Retrieve a container based on type inference.
§Example:
let pids: &PidController = control_group.controller_of()
.expect("No pids controller attached!");
let cpu: &CpuController = control_group.controller_of()
.expect("No cpu controller attached!");
Sourcepub fn remove_task_by_tgid(&self, tgid: CgroupPid) -> Result<()>
pub fn remove_task_by_tgid(&self, tgid: CgroupPid) -> Result<()>
Removes tasks from the control group by thread group id.
Note that this means that the task will be moved back to the root control group in the hierarchy and any rules applied to that control group will still apply to the proc.
Sourcepub fn remove_task(&self, tid: CgroupPid) -> Result<()>
pub fn remove_task(&self, tid: CgroupPid) -> Result<()>
Removes a task from the control group.
Note that this means that the task will be moved back to the root control group in the hierarchy and any rules applied to that control group will still apply to the task.
Sourcepub fn move_task_to_parent_by_tgid(&self, tgid: CgroupPid) -> Result<()>
pub fn move_task_to_parent_by_tgid(&self, tgid: CgroupPid) -> Result<()>
Moves tasks to the parent control group by thread group id.
Sourcepub fn move_task_to_parent(&self, tid: CgroupPid) -> Result<()>
pub fn move_task_to_parent(&self, tid: CgroupPid) -> Result<()>
Moves a task to the parent control group.
Sourcepub fn parent_control_group(&self) -> Cgroup
pub fn parent_control_group(&self) -> Cgroup
Return a handle to the parent control group in the hierarchy.
Sourcepub fn kill(&self) -> Result<()>
pub fn kill(&self) -> Result<()>
Kill every process in the control group. Only supported for v2 cgroups and on kernels 5.14+. This will fail with InvalidOperation if the ‘cgroup.kill’ file does not exist.
Sourcepub fn add_task_by_tgid(&self, tgid: CgroupPid) -> Result<()>
pub fn add_task_by_tgid(&self, tgid: CgroupPid) -> Result<()>
Attach tasks to the control group by thread group id.
Sourcepub fn set_cgroup_type(&self, cgroup_type: &str) -> Result<()>
pub fn set_cgroup_type(&self, cgroup_type: &str) -> Result<()>
set cgroup.type
Sourcepub fn get_cgroup_type(&self) -> Result<String>
pub fn get_cgroup_type(&self) -> Result<String>
get cgroup.type
Sourcepub fn set_notify_on_release(&self, enable: bool) -> Result<()>
pub fn set_notify_on_release(&self, enable: bool) -> Result<()>
Set notify_on_release to the control group.
Sourcepub fn set_release_agent(&self, path: &str) -> Result<()>
pub fn set_release_agent(&self, path: &str) -> Result<()>
Set release_agent
Sourcepub fn procs(&self) -> Vec<CgroupPid>
pub fn procs(&self) -> Vec<CgroupPid>
Returns an Iterator that can be used to iterate over the procs that are currently in the control group.