-
Notifications
You must be signed in to change notification settings - Fork 346
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce libcgroup examples and README updates
Signed-off-by: Kris Nóva <kris@nivenly.com>
- Loading branch information
Showing
4 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,48 @@ | ||
# libcgroups | ||
|
||
Youki crate for creating and managing cgroups. | ||
|
||
By default, youki will determine which cgroup hierarchy to use based on the state of the system at runtime. | ||
|
||
The cgroup implementation uses a public trait called `CgroupManager` which can be an implementation of: | ||
|
||
- Cgroup v1 hierarchy | ||
- Cgroup v2 hierarchy | ||
|
||
```rust | ||
pub trait CgroupManager { | ||
fn add_task(&self, pid: Pid) -> Result<()>; | ||
fn apply(&self, controller_opt: &ControllerOpt) -> Result<()>; | ||
fn remove(&self) -> Result<()>; | ||
fn freeze(&self, state: FreezerState) -> Result<()>; | ||
fn stats(&self) -> Result<Stats>; | ||
fn get_all_pids(&self) -> Result<Vec<Pid>>; | ||
} | ||
``` | ||
|
||
The determination is made by `get_cgroup_setup()` function. | ||
|
||
```rust | ||
/// Determines the cgroup setup of the system. Systems typically have one of | ||
/// three setups: | ||
/// - Unified: Pure cgroup v2 system. | ||
/// - Legacy: Pure cgroup v1 system. | ||
/// - Hybrid: Hybrid is basically a cgroup v1 system, except for | ||
/// an additional unified hierarchy which doesn't have any | ||
/// controllers attached. Resource control can purely be achieved | ||
/// through the cgroup v1 hierarchy, not through the cgroup v2 hierarchy. | ||
pub fn get_cgroup_setup() -> Result<CgroupSetup> | ||
``` | ||
### Running Examples | ||
|
||
There are example files in the `libcgroup/examples/*.rs` pattern. | ||
The nightly cargo build is required to run the examples. | ||
Run the examples below as follows: | ||
```bash | ||
rustup install nightly # Install nightly cargo | ||
cargo +nightly run --example examples/bpf # Run BPF example | ||
cargo +nightly run --example examples/create # Run create example | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use anyhow::Result; | ||
use libcgroups::common::{create_cgroup_manager, DEFAULT_CGROUP_ROOT}; | ||
use nix::libc::pid_t; | ||
use nix::unistd::Pid; | ||
use std::path::Path; | ||
use std::process::Command; | ||
|
||
fn main() -> Result<()> { | ||
// Create cgroup manager | ||
let manager = create_cgroup_manager(Path::new(DEFAULT_CGROUP_ROOT), false, "example-cgroup")?; | ||
|
||
// Run process | ||
let cmd = Command::new("sh") | ||
.args(["-c", "sleep 100"]) | ||
.spawn() | ||
.expect("spawning sleep"); | ||
|
||
// Add the new process to the cgroup | ||
manager.add_task(Pid::from_raw(cmd.id() as pid_t))?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters