Struct Cgroup

Source
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

Source

pub fn v2(&self) -> bool

Source

pub fn path(&self) -> &str

Return the path the cgroup is located at.

Source

pub fn create(&self) -> Result<()>

Create this control group.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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

Source

pub fn subsystems(&self) -> &Vec<Subsystem>

The list of subsystems that this control group supports.

Source

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.

Source

pub fn apply(&self, res: &Resources) -> Result<()>

Apply a set of resource limits to the control group.

Source

pub fn controller_of<'a, T>(&'a self) -> Option<&'a T>
where &'a T: From<&'a Subsystem>, T: Controller + ControllIdentifier,

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!");
Source

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.

Source

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.

Source

pub fn move_task_to_parent_by_tgid(&self, tgid: CgroupPid) -> Result<()>

Moves tasks to the parent control group by thread group id.

Source

pub fn move_task_to_parent(&self, tid: CgroupPid) -> Result<()>

Moves a task to the parent control group.

Source

pub fn parent_control_group(&self) -> Cgroup

Return a handle to the parent control group in the hierarchy.

Source

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.

Source

pub fn add_task(&self, tid: CgroupPid) -> Result<()>

Attach a task to the control group.

Source

pub fn add_task_by_tgid(&self, tgid: CgroupPid) -> Result<()>

Attach tasks to the control group by thread group id.

Source

pub fn set_cgroup_type(&self, cgroup_type: &str) -> Result<()>

set cgroup.type

Source

pub fn get_cgroup_type(&self) -> Result<String>

get cgroup.type

Source

pub fn set_notify_on_release(&self, enable: bool) -> Result<()>

Set notify_on_release to the control group.

Source

pub fn set_release_agent(&self, path: &str) -> Result<()>

Set release_agent

Source

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.

Source

pub fn tasks(&self) -> Vec<CgroupPid>

Returns an Iterator that can be used to iterate over the tasks that are currently in the control group.

Source

pub fn exists(&self) -> bool

Checks if the cgroup exists.

Returns true if at least one subsystem exists.

Trait Implementations§

Source§

impl Clone for Cgroup

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Cgroup

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Cgroup

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Cgroup

§

impl !RefUnwindSafe for Cgroup

§

impl Send for Cgroup

§

impl Sync for Cgroup

§

impl Unpin for Cgroup

§

impl !UnwindSafe for Cgroup

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.