|
1 | 1 | # cgroups-rs  |
2 | 2 | Native Rust library for managing control groups under Linux |
3 | 3 |
|
4 | | -# Example |
| 4 | +Right now the crate only support the original, V1 hierarchy, however support |
| 5 | +is planned for the Unified hierarchy. |
5 | 6 |
|
6 | | -## Create a control group, and limit the pid resource |
| 7 | +# Examples |
| 8 | + |
| 9 | +## Create a control group using the builder pattern |
7 | 10 |
|
8 | 11 | ``` rust |
9 | 12 | // Acquire a handle for the V1 cgroup hierarchy. |
10 | 13 | let hier = ::hierarchies::V1::new(); |
11 | | -// Create a control group named "example" in the hierarchy. |
12 | | -let cg = Cgroup::new(&hier, String::from("example"), 0); |
13 | | -{ |
14 | | - // Get a handle to the pids controller of the control group. |
15 | | - let pids: &PidController = cg.controller_of().expect("No pids controller in V1 hierarchy!"); |
16 | | - // Set the maximum amount of processes in the cgroup. |
17 | | - pids.set_pid_max(PidMax::Value(10)); |
18 | | - // Check that this has had the desired effect by reading the value back from the kernel. |
19 | | - assert_eq!(pids.get_pid_max(), Some(PidMax::Value(10))); |
20 | | -} |
21 | | -// Once done, delete the control group (and its associated controllers). |
| 14 | + |
| 15 | +// Use the builder pattern (see the documentation to create the control group) |
| 16 | +// |
| 17 | +// This creates a control group named "example" in the V1 hierarchy. |
| 18 | +let cg: Cgroup = CgroupBuilder::new("example", &v1) |
| 19 | + .cpu() |
| 20 | + .shares(85) |
| 21 | + .done() |
| 22 | + .build(); |
| 23 | + |
| 24 | +// Now `cg` is a control group that gets 85% of the CPU time in relative to |
| 25 | +// other control groups. |
| 26 | + |
| 27 | +// Get a handle to the CPU controller. |
| 28 | +let cpus: &CpuController = cg.controller_of().unwrap(); |
| 29 | +cpus.add_task(1234u64); |
| 30 | + |
| 31 | +// [...] |
| 32 | + |
| 33 | +// Finally, clean up and delete the control group. |
22 | 34 | cg.delete(); |
| 35 | + |
| 36 | +// Note that `Cgroup` does not implement `Drop` and therefore when the |
| 37 | +// structure is dropped, the Cgroup will stay around. This is because, later |
| 38 | +// you can then re-create the `Cgroup` using `load()`. We aren't too set on |
| 39 | +// this behavior, so it might change in the feature. Rest assured, it will be a |
| 40 | +// major version change. |
23 | 41 | ``` |
24 | 42 |
|
25 | 43 | # Disclaimer |
26 | 44 |
|
27 | 45 | This crate is licensed under: |
28 | 46 |
|
29 | 47 | - MIT License (see LICENSE-MIT); or |
30 | | -- Apache 2.0 LIcense (see LICENSE-Apache-2.0), |
| 48 | +- Apache 2.0 License (see LICENSE-Apache-2.0), |
31 | 49 |
|
32 | 50 | at your option. |
33 | 51 |
|
|
0 commit comments