mirror of
https://github.com/kata-containers/cgroups-rs.git
synced 2026-08-05 02:13:23 +00:00
Compare commits
59 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
de9625ff57 | ||
|
|
b6b65f79d1 | ||
|
|
547fb08c03 | ||
|
|
ec9f3547ed | ||
|
|
82a6aa491a | ||
|
|
65c36214b7 | ||
|
|
e0d0b8f4bc | ||
|
|
db822470e5 | ||
|
|
362373b3ec | ||
|
|
eadbf53140 | ||
|
|
b3c57840ee | ||
|
|
b10e52d85f | ||
|
|
7d4d4579a3 | ||
|
|
eb3e37a4bc | ||
|
|
4005ad844d | ||
|
|
ef3497646f | ||
|
|
69ef63a0ef | ||
|
|
346844ca72 | ||
|
|
4f1fe13d91 | ||
|
|
17a6c6b842 | ||
|
|
3c4b724433 | ||
|
|
01885adb99 | ||
|
|
ce5f5f638e | ||
|
|
be837166e9 | ||
|
|
8d29c194e3 | ||
|
|
8a82ad0ac2 | ||
|
|
369f3bebed | ||
|
|
0b6b229a38 | ||
|
|
f55bdb1775 | ||
|
|
55505e0b3e | ||
|
|
df347c1db8 | ||
|
|
66a93b1c3d | ||
|
|
ca66292f5f | ||
|
|
89edba0f85 | ||
|
|
1b61c07b69 | ||
|
|
45e1f0c274 | ||
|
|
41b5f9c25c | ||
|
|
93a59571e3 | ||
|
|
257012f2bb | ||
|
|
3dd0735324 | ||
|
|
6b338cf997 | ||
|
|
225be2cdbb | ||
|
|
51779d6915 | ||
|
|
5ea28f076c | ||
|
|
aa74f34a91 | ||
|
|
328428ace4 | ||
|
|
c8bb7e1c7e | ||
|
|
25a1340123 | ||
|
|
4203075f19 | ||
|
|
a45ecf0884 | ||
|
|
2f60f213cc | ||
|
|
91146f0ea3 | ||
|
|
e845665b3a | ||
|
|
55034f5b05 | ||
|
|
e2c2618707 | ||
|
|
07878325c3 | ||
|
|
88fb33113d | ||
|
|
1211754b62 | ||
|
|
c9d02afe33 |
2
.github/workflows/bvt.yaml
vendored
2
.github/workflows/bvt.yaml
vendored
@@ -1,7 +1,7 @@
|
||||
name: BVT
|
||||
on: [pull_request]
|
||||
env:
|
||||
RUST_VERSION: 1.52
|
||||
RUST_VERSION: 1.69.0
|
||||
jobs:
|
||||
build:
|
||||
name: Build
|
||||
|
||||
4
.gitignore
vendored
4
.gitignore
vendored
@@ -8,7 +8,3 @@ Cargo.lock
|
||||
|
||||
# These are backup files generated by rustfmt
|
||||
**/*.rs.bk
|
||||
|
||||
/target
|
||||
**/*.rs.bk
|
||||
Cargo.lock
|
||||
|
||||
@@ -5,7 +5,7 @@ repository = "https://github.com/kata-containers/cgroups-rs"
|
||||
keywords = ["linux", "cgroup", "containers", "isolation"]
|
||||
categories = ["os", "api-bindings", "os::unix-apis"]
|
||||
license = "MIT OR Apache-2.0"
|
||||
version = "0.2.11"
|
||||
version = "0.3.5"
|
||||
authors = ["The Kata Containers community <kata-dev@lists.katacontainers.io>", "Levente Kurusa <lkurusa@acm.org>", "Sam Wilson <tecywiz121@hotmail.com>"]
|
||||
edition = "2018"
|
||||
homepage = "https://github.com/kata-containers/cgroups-rs"
|
||||
|
||||
142
src/blkio.rs
142
src/blkio.rs
@@ -43,7 +43,7 @@ pub struct BlkIoData {
|
||||
pub data: u64,
|
||||
}
|
||||
|
||||
#[derive(Eq, PartialEq, Debug)]
|
||||
#[derive(Eq, PartialEq, Debug, Default)]
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
/// Per-device activity from the control group.
|
||||
pub struct IoService {
|
||||
@@ -59,6 +59,8 @@ pub struct IoService {
|
||||
pub sync: u64,
|
||||
/// How many items were asynchronously transferred.
|
||||
pub r#async: u64,
|
||||
/// How many items were discarded.
|
||||
pub discard: u64,
|
||||
/// Total number of items transferred.
|
||||
pub total: u64,
|
||||
}
|
||||
@@ -87,44 +89,62 @@ pub struct IoStat {
|
||||
}
|
||||
|
||||
fn parse_io_service(s: String) -> Result<Vec<IoService>> {
|
||||
s.lines()
|
||||
let mut io_services = Vec::<IoService>::new();
|
||||
let mut io_service = IoService::default();
|
||||
|
||||
let lines = s
|
||||
.lines()
|
||||
.filter(|x| x.split_whitespace().count() == 3)
|
||||
.map(|x| {
|
||||
let mut spl = x.split_whitespace();
|
||||
(spl.next().unwrap(), spl.next().unwrap(), spl.next().unwrap())
|
||||
(
|
||||
spl.next().unwrap(),
|
||||
spl.next().unwrap(),
|
||||
spl.next().unwrap(),
|
||||
)
|
||||
})
|
||||
.map(|(a, b, c)| {
|
||||
let mut spl = a.split(':');
|
||||
(spl.next().unwrap(), spl.next().unwrap(), b, c)
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.chunks(5)
|
||||
.map(|x| {
|
||||
match x {
|
||||
[(major, minor, "Read", read_val), (_, _, "Write", write_val),
|
||||
(_, _, "Sync", sync_val), (_, _, "Async", async_val),
|
||||
(_, _, "Total", total_val)] =>
|
||||
Some(IoService {
|
||||
major: major.parse::<i16>().unwrap(),
|
||||
minor: minor.parse::<i16>().unwrap(),
|
||||
read: read_val.parse::<u64>().unwrap(),
|
||||
write: write_val.parse::<u64>().unwrap(),
|
||||
sync: sync_val.parse::<u64>().unwrap(),
|
||||
r#async: async_val.parse::<u64>().unwrap(),
|
||||
total: total_val.parse::<u64>().unwrap(),
|
||||
}),
|
||||
_ => None,
|
||||
}
|
||||
})
|
||||
.fold(Ok(Vec::new()), |acc, x| {
|
||||
if acc.is_err() || x.is_none() {
|
||||
Err(Error::new(ParseError))
|
||||
} else {
|
||||
let mut acc = acc.unwrap();
|
||||
acc.push(x.unwrap());
|
||||
Ok(acc)
|
||||
}
|
||||
(
|
||||
spl.next().unwrap().parse::<i16>(),
|
||||
spl.next().unwrap().parse::<i16>(),
|
||||
b,
|
||||
c,
|
||||
)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
for (major_num, minor_num, op, val) in lines.iter() {
|
||||
let major = *major_num.as_ref().map_err(|_| Error::new(ParseError))?;
|
||||
let minor = *minor_num.as_ref().map_err(|_| Error::new(ParseError))?;
|
||||
|
||||
if (major != io_service.major || minor != io_service.minor) && io_service.major != 0 {
|
||||
// new block device
|
||||
io_services.push(io_service);
|
||||
io_service = IoService::default();
|
||||
}
|
||||
|
||||
io_service.major = major;
|
||||
io_service.minor = minor;
|
||||
|
||||
let val = val.parse::<u64>().map_err(|_| Error::new(ParseError))?;
|
||||
|
||||
match *op {
|
||||
"Read" => io_service.read = val,
|
||||
"Write" => io_service.write = val,
|
||||
"Sync" => io_service.sync = val,
|
||||
"Async" => io_service.r#async = val,
|
||||
"Discard" => io_service.discard = val,
|
||||
"Total" => io_service.total = val,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
if io_service.major != 0 {
|
||||
io_services.push(io_service);
|
||||
}
|
||||
|
||||
Ok(io_services)
|
||||
}
|
||||
|
||||
fn get_value(s: &str) -> String {
|
||||
@@ -410,10 +430,10 @@ impl<'a> From<&'a Subsystem> for &'a BlkIoController {
|
||||
|
||||
impl BlkIoController {
|
||||
/// Constructs a new `BlkIoController` with `root` serving as the root of the control group.
|
||||
pub fn new(root: PathBuf, v2: bool) -> Self {
|
||||
pub fn new(point: PathBuf, root: PathBuf, v2: bool) -> Self {
|
||||
Self {
|
||||
base: root.clone(),
|
||||
path: root,
|
||||
base: root,
|
||||
path: point,
|
||||
v2,
|
||||
}
|
||||
}
|
||||
@@ -817,6 +837,7 @@ mod test {
|
||||
8:32 Write 0
|
||||
8:32 Sync 4280320
|
||||
8:32 Async 0
|
||||
8:32 Discard 1
|
||||
8:32 Total 4280320
|
||||
8:48 Read 5705479168
|
||||
8:48 Write 56096055296
|
||||
@@ -833,28 +854,6 @@ mod test {
|
||||
8:0 Sync 7192576
|
||||
8:0 Async 0
|
||||
8:0 Total 7192576
|
||||
Total 61823067136
|
||||
";
|
||||
|
||||
static TEST_WRONG_VALUE: &str = "\
|
||||
8:32 Read 4280320
|
||||
8:32 Write 0
|
||||
8:32 Async 0
|
||||
8:32 Total 4280320 8:48 Read 5705479168
|
||||
8:48 Write 56096055296
|
||||
8:48 Sync 11213923328
|
||||
8:48 Async 50587611136
|
||||
8:48 Total 61801534464
|
||||
8:16 Read 10059776
|
||||
8:16 Write 0
|
||||
8:16 Sync 10059776
|
||||
8:16 Async 0
|
||||
8:16 Total 10059776
|
||||
8:0 Read 7192576
|
||||
8:0 Write 0
|
||||
8:0 Sync 7192576
|
||||
8:0 Async 0
|
||||
8:0 Total 7192576
|
||||
Total 61823067136
|
||||
";
|
||||
|
||||
@@ -884,6 +883,7 @@ Total 61823067136
|
||||
write: 0,
|
||||
sync: 4280320,
|
||||
r#async: 0,
|
||||
discard: 1,
|
||||
total: 4280320,
|
||||
},
|
||||
IoService {
|
||||
@@ -893,6 +893,7 @@ Total 61823067136
|
||||
write: 56096055296,
|
||||
sync: 11213923328,
|
||||
r#async: 50587611136,
|
||||
discard: 0,
|
||||
total: 61801534464,
|
||||
},
|
||||
IoService {
|
||||
@@ -902,6 +903,7 @@ Total 61823067136
|
||||
write: 0,
|
||||
sync: 10059776,
|
||||
r#async: 0,
|
||||
discard: 0,
|
||||
total: 10059776,
|
||||
},
|
||||
IoService {
|
||||
@@ -911,12 +913,34 @@ Total 61823067136
|
||||
write: 0,
|
||||
sync: 7192576,
|
||||
r#async: 0,
|
||||
discard: 0,
|
||||
total: 7192576,
|
||||
}
|
||||
]
|
||||
);
|
||||
let err = parse_io_service(TEST_WRONG_VALUE.to_string()).unwrap_err();
|
||||
assert_eq!(err.kind(), &ErrorKind::ParseError,);
|
||||
|
||||
let invalid_values = vec![
|
||||
"\
|
||||
8:32 Read 4280320
|
||||
8:32 Write a
|
||||
8:32 Async 1
|
||||
",
|
||||
"\
|
||||
8:32 Read 4280320
|
||||
b:32 Write 1
|
||||
8:32 Async 1
|
||||
",
|
||||
"\
|
||||
8:32 Read 4280320
|
||||
8:32 Write 1
|
||||
8:c Async 1
|
||||
",
|
||||
];
|
||||
|
||||
for value in invalid_values {
|
||||
let err = parse_io_service(value.to_string()).unwrap_err();
|
||||
assert_eq!(err.kind(), &ErrorKind::ParseError,);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
323
src/cgroup.rs
323
src/cgroup.rs
@@ -9,6 +9,7 @@
|
||||
use crate::error::ErrorKind::*;
|
||||
use crate::error::*;
|
||||
|
||||
use crate::hierarchies::V1;
|
||||
use crate::{CgroupPid, ControllIdentifier, Controller, Hierarchy, Resources, Subsystem};
|
||||
|
||||
use std::collections::HashMap;
|
||||
@@ -16,6 +17,11 @@ use std::convert::From;
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
pub const CGROUP_MODE_DOMAIN: &str = "domain";
|
||||
pub const CGROUP_MODE_DOMAIN_THREADED: &str = "domain threaded";
|
||||
pub const CGROUP_MODE_DOMAIN_INVALID: &str = "domain invalid";
|
||||
pub const CGROUP_MODE_THREADED: &str = "threaded";
|
||||
|
||||
/// A control group is the central structure to this crate.
|
||||
///
|
||||
///
|
||||
@@ -36,14 +42,18 @@ pub struct Cgroup {
|
||||
/// The hierarchy.
|
||||
hier: Box<dyn Hierarchy>,
|
||||
path: String,
|
||||
|
||||
/// List of controllers specifically enabled in the control group.
|
||||
specified_controllers: Option<Vec<String>>,
|
||||
}
|
||||
|
||||
impl Clone for Cgroup {
|
||||
fn clone(&self) -> Self {
|
||||
Cgroup {
|
||||
subsystems: self.subsystems.clone(),
|
||||
path: self.path.clone(),
|
||||
hier: crate::hierarchies::auto(),
|
||||
path: self.path.clone(),
|
||||
specified_controllers: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -54,48 +64,72 @@ impl Default for Cgroup {
|
||||
subsystems: Vec::new(),
|
||||
hier: crate::hierarchies::auto(),
|
||||
path: "".to_string(),
|
||||
specified_controllers: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Cgroup {
|
||||
pub fn v2(&self) -> bool {
|
||||
self.hier.v2()
|
||||
}
|
||||
|
||||
/// Return the path the cgroup is located at.
|
||||
pub fn path(&self) -> &str {
|
||||
&self.path
|
||||
}
|
||||
|
||||
/// Create this control group.
|
||||
fn create(&self) {
|
||||
pub fn create(&self) -> Result<()> {
|
||||
if self.hier.v2() {
|
||||
let _ret = create_v2_cgroup(self.hier.root(), &self.path);
|
||||
create_v2_cgroup(self.hier.root(), &self.path, &self.specified_controllers)
|
||||
} else {
|
||||
for subsystem in &self.subsystems {
|
||||
subsystem.to_controller().create();
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn v2(&self) -> bool {
|
||||
self.hier.v2()
|
||||
}
|
||||
|
||||
/// 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.
|
||||
pub fn new<P: AsRef<Path>>(hier: Box<dyn Hierarchy>, path: P) -> Cgroup {
|
||||
pub fn new<P: AsRef<Path>>(hier: Box<dyn Hierarchy>, path: P) -> Result<Cgroup> {
|
||||
let cg = Cgroup::load(hier, path);
|
||||
cg.create();
|
||||
cg
|
||||
cg.create()?;
|
||||
Ok(cg)
|
||||
}
|
||||
|
||||
/// 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.
|
||||
pub fn new_with_specified_controllers<P: AsRef<Path>>(
|
||||
hier: Box<dyn Hierarchy>,
|
||||
path: P,
|
||||
specified_controllers: Option<Vec<String>>,
|
||||
) -> Result<Cgroup> {
|
||||
let cg = if let Some(sc) = specified_controllers {
|
||||
Cgroup::load_with_specified_controllers(hier, path, sc)
|
||||
} else {
|
||||
Cgroup::load(hier, path)
|
||||
};
|
||||
cg.create()?;
|
||||
Ok(cg)
|
||||
}
|
||||
|
||||
/// 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
|
||||
/// Note that this method is only meaningful for cgroup v1, call it is equivalent to call `new` in the v2 mode.
|
||||
pub fn new_with_relative_paths<P: AsRef<Path>>(
|
||||
hier: Box<dyn Hierarchy>,
|
||||
path: P,
|
||||
relative_paths: HashMap<String, String>,
|
||||
) -> Cgroup {
|
||||
) -> Result<Cgroup> {
|
||||
let cg = Cgroup::load_with_relative_paths(hier, path, relative_paths);
|
||||
cg.create();
|
||||
cg
|
||||
cg.create()?;
|
||||
Ok(cg)
|
||||
}
|
||||
|
||||
/// Create a handle for a control group in the hierarchy `hier`, with name `path`.
|
||||
@@ -116,6 +150,34 @@ impl Cgroup {
|
||||
path: path.to_str().unwrap().to_string(),
|
||||
subsystems,
|
||||
hier,
|
||||
specified_controllers: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// 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.
|
||||
pub fn load_with_specified_controllers<P: AsRef<Path>>(
|
||||
hier: Box<dyn Hierarchy>,
|
||||
path: P,
|
||||
specified_controllers: Vec<String>,
|
||||
) -> Cgroup {
|
||||
let path = path.as_ref();
|
||||
let mut subsystems = hier.subsystems();
|
||||
if path.as_os_str() != "" {
|
||||
subsystems = subsystems
|
||||
.into_iter()
|
||||
.filter(|x| specified_controllers.contains(&x.controller_name()))
|
||||
.map(|x| x.enter(path))
|
||||
.collect::<Vec<_>>();
|
||||
}
|
||||
|
||||
Cgroup {
|
||||
path: path.to_str().unwrap().to_string(),
|
||||
subsystems,
|
||||
hier,
|
||||
specified_controllers: Some(specified_controllers),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -159,6 +221,7 @@ impl Cgroup {
|
||||
subsystems,
|
||||
hier,
|
||||
path: path.to_str().unwrap().to_string(),
|
||||
specified_controllers: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -233,36 +296,137 @@ impl Cgroup {
|
||||
None
|
||||
}
|
||||
|
||||
/// 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.
|
||||
pub fn remove_task_by_tgid(&self, tgid: CgroupPid) -> Result<()> {
|
||||
self.hier.root_control_group().add_task_by_tgid(tgid)
|
||||
}
|
||||
|
||||
/// 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.
|
||||
pub fn remove_task(&self, pid: CgroupPid) {
|
||||
let _ = self.hier.root_control_group().add_task(pid);
|
||||
pub fn remove_task(&self, tid: CgroupPid) -> Result<()> {
|
||||
self.hier.root_control_group().add_task(tid)
|
||||
}
|
||||
|
||||
/// Moves tasks to the parent control group by thread group id.
|
||||
pub fn move_task_to_parent_by_tgid(&self, tgid: CgroupPid) -> Result<()> {
|
||||
self.hier
|
||||
.parent_control_group(&self.path)
|
||||
.add_task_by_tgid(tgid)
|
||||
}
|
||||
|
||||
/// Moves a task to the parent control group.
|
||||
pub fn move_task_to_parent(&self, tid: CgroupPid) -> Result<()> {
|
||||
self.hier.parent_control_group(&self.path).add_task(tid)
|
||||
}
|
||||
|
||||
/// Return a handle to the parent control group in the hierarchy.
|
||||
pub fn parent_control_group(&self) -> Cgroup {
|
||||
self.hier.parent_control_group(&self.path)
|
||||
}
|
||||
|
||||
/// 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.
|
||||
pub fn kill(&self) -> Result<()> {
|
||||
if !self.v2() {
|
||||
return Err(Error::new(CgroupVersion));
|
||||
}
|
||||
|
||||
let val = "1";
|
||||
let file_name = "cgroup.kill";
|
||||
let p = self.hier.root().join(self.path.clone()).join(file_name);
|
||||
|
||||
// If cgroup.kill doesn't exist they're not on 5.14+ so lets
|
||||
// surface some error the caller can check against.
|
||||
if !p.exists() {
|
||||
return Err(Error::new(InvalidOperation));
|
||||
}
|
||||
|
||||
fs::write(p, val)
|
||||
.map_err(|e| Error::with_cause(WriteFailed(file_name.to_string(), val.to_string()), e))
|
||||
}
|
||||
|
||||
/// Attach a task to the control group.
|
||||
pub fn add_task(&self, pid: CgroupPid) -> Result<()> {
|
||||
pub fn add_task(&self, tid: CgroupPid) -> Result<()> {
|
||||
if self.v2() {
|
||||
let subsystems = self.subsystems();
|
||||
if !subsystems.is_empty() {
|
||||
let c = subsystems[0].to_controller();
|
||||
c.add_task(&pid)
|
||||
let cgroup_type = self.get_cgroup_type()?;
|
||||
// In cgroup v2, writing to the cgroup.threads file is only supported in thread mode.
|
||||
if cgroup_type == *CGROUP_MODE_DOMAIN_THREADED
|
||||
|| cgroup_type == *CGROUP_MODE_THREADED
|
||||
{
|
||||
// It is used to move the threads of a process into a cgroup in thread mode.
|
||||
c.add_task(&tid)
|
||||
} else {
|
||||
// When the cgroup type is domain or domain invalid,
|
||||
// cgroup.threads cannot be written.
|
||||
Err(Error::new(CgroupMode))
|
||||
}
|
||||
} else {
|
||||
Ok(())
|
||||
Err(Error::new(SubsystemsEmpty))
|
||||
}
|
||||
} else {
|
||||
self.subsystems()
|
||||
.iter()
|
||||
.try_for_each(|sub| sub.to_controller().add_task(&pid))
|
||||
.try_for_each(|sub| sub.to_controller().add_task(&tid))
|
||||
}
|
||||
}
|
||||
|
||||
/// Attach a task to the control group by thread group id.
|
||||
pub fn add_task_by_tgid(&self, pid: CgroupPid) -> Result<()> {
|
||||
self.subsystems()
|
||||
.iter()
|
||||
.try_for_each(|sub| sub.to_controller().add_task_by_tgid(&pid))
|
||||
/// Attach tasks to the control group by thread group id.
|
||||
pub fn add_task_by_tgid(&self, tgid: CgroupPid) -> Result<()> {
|
||||
if self.v2() {
|
||||
let subsystems = self.subsystems();
|
||||
if !subsystems.is_empty() {
|
||||
let c = subsystems[0].to_controller();
|
||||
// It is used to move a thread of the process to a cgroup,
|
||||
// and other threads of the process will also move together.
|
||||
c.add_task_by_tgid(&tgid)
|
||||
} else {
|
||||
Err(Error::new(SubsystemsEmpty))
|
||||
}
|
||||
} else {
|
||||
self.subsystems()
|
||||
.iter()
|
||||
.try_for_each(|sub| sub.to_controller().add_task_by_tgid(&tgid))
|
||||
}
|
||||
}
|
||||
|
||||
/// set cgroup.type
|
||||
pub fn set_cgroup_type(&self, cgroup_type: &str) -> Result<()> {
|
||||
if self.v2() {
|
||||
let subsystems = self.subsystems();
|
||||
if !subsystems.is_empty() {
|
||||
let c = subsystems[0].to_controller();
|
||||
c.set_cgroup_type(cgroup_type)
|
||||
} else {
|
||||
Err(Error::new(SubsystemsEmpty))
|
||||
}
|
||||
} else {
|
||||
Err(Error::new(CgroupVersion))
|
||||
}
|
||||
}
|
||||
|
||||
/// get cgroup.type
|
||||
pub fn get_cgroup_type(&self) -> Result<String> {
|
||||
if self.v2() {
|
||||
let subsystems = self.subsystems();
|
||||
if !subsystems.is_empty() {
|
||||
let c = subsystems[0].to_controller();
|
||||
let cgroup_type = c.get_cgroup_type()?;
|
||||
Ok(cgroup_type)
|
||||
} else {
|
||||
Err(Error::new(SubsystemsEmpty))
|
||||
}
|
||||
} else {
|
||||
Err(Error::new(CgroupVersion))
|
||||
}
|
||||
}
|
||||
|
||||
/// Set notify_on_release to the control group.
|
||||
@@ -281,6 +445,33 @@ impl Cgroup {
|
||||
.try_for_each(|sub| sub.to_controller().set_release_agent(path))
|
||||
}
|
||||
|
||||
/// Returns an Iterator that can be used to iterate over the procs that are currently in the
|
||||
/// control group.
|
||||
pub fn procs(&self) -> Vec<CgroupPid> {
|
||||
// Collect the procs from all subsystems
|
||||
let mut v = if self.v2() {
|
||||
let subsystems = self.subsystems();
|
||||
if !subsystems.is_empty() {
|
||||
let c = subsystems[0].to_controller();
|
||||
c.procs()
|
||||
} else {
|
||||
vec![]
|
||||
}
|
||||
} else {
|
||||
self.subsystems()
|
||||
.iter()
|
||||
.map(|x| x.to_controller().procs())
|
||||
.fold(vec![], |mut acc, mut x| {
|
||||
acc.append(&mut x);
|
||||
acc
|
||||
})
|
||||
};
|
||||
|
||||
v.sort();
|
||||
v.dedup();
|
||||
v
|
||||
}
|
||||
|
||||
/// Returns an Iterator that can be used to iterate over the tasks that are currently in the
|
||||
/// control group.
|
||||
pub fn tasks(&self) -> Vec<CgroupPid> {
|
||||
@@ -307,6 +498,13 @@ impl Cgroup {
|
||||
v.dedup();
|
||||
v
|
||||
}
|
||||
|
||||
/// Checks if the cgroup exists.
|
||||
///
|
||||
/// Returns true if at least one subsystem exists.
|
||||
pub fn exists(&self) -> bool {
|
||||
self.subsystems().iter().any(|e| e.to_controller().exists())
|
||||
}
|
||||
}
|
||||
|
||||
pub const UNIFIED_MOUNTPOINT: &str = "/sys/fs/cgroup";
|
||||
@@ -324,13 +522,26 @@ fn supported_controllers() -> Vec<String> {
|
||||
let ret = fs::read_to_string(p.as_str());
|
||||
ret.unwrap_or_default()
|
||||
.split(' ')
|
||||
.map(|x| x.to_string())
|
||||
.map(|x| x.trim().to_string())
|
||||
.collect::<Vec<String>>()
|
||||
}
|
||||
|
||||
fn create_v2_cgroup(root: PathBuf, path: &str) -> Result<()> {
|
||||
fn create_v2_cgroup(
|
||||
root: PathBuf,
|
||||
path: &str,
|
||||
specified_controllers: &Option<Vec<String>>,
|
||||
) -> Result<()> {
|
||||
// controler list ["memory", "cpu"]
|
||||
let controllers = supported_controllers();
|
||||
let controllers = if let Some(s_controllers) = specified_controllers.clone() {
|
||||
if verify_supported_controllers(s_controllers.as_ref()) {
|
||||
s_controllers
|
||||
} else {
|
||||
return Err(Error::new(ErrorKind::SpecifiedControllers));
|
||||
}
|
||||
} else {
|
||||
supported_controllers()
|
||||
};
|
||||
|
||||
let mut fp = root;
|
||||
|
||||
// enable for root
|
||||
@@ -358,6 +569,16 @@ fn create_v2_cgroup(root: PathBuf, path: &str) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn verify_supported_controllers(controllers: &[String]) -> bool {
|
||||
let sc = supported_controllers();
|
||||
for controller in controllers.iter() {
|
||||
if !sc.contains(controller) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
pub fn get_cgroups_relative_paths() -> Result<HashMap<String, String>> {
|
||||
let path = "/proc/self/cgroup".to_string();
|
||||
get_cgroups_relative_paths_by_path(path)
|
||||
@@ -368,19 +589,47 @@ pub fn get_cgroups_relative_paths_by_pid(pid: u32) -> Result<HashMap<String, Str
|
||||
get_cgroups_relative_paths_by_path(path)
|
||||
}
|
||||
|
||||
fn get_cgroup_destination(mut mount_root: String, pidpath: String) -> String {
|
||||
if mount_root == "/" {
|
||||
mount_root = String::from("");
|
||||
}
|
||||
pidpath.trim_start_matches(&mount_root).to_string()
|
||||
}
|
||||
|
||||
pub fn existing_path(paths: HashMap<String, String>) -> Result<HashMap<String, String>> {
|
||||
let mount_roots_v1 = V1::new();
|
||||
let mut mount_roots_subsystems_map = HashMap::new();
|
||||
|
||||
for s in mount_roots_v1.subsystems().iter() {
|
||||
let controller_name = s.controller_name();
|
||||
let path_from_cgroup = paths
|
||||
.get(&controller_name)
|
||||
.ok_or(Error::new(Common(format!(
|
||||
"controller {} found in mountinfo, but not found in cgroup.",
|
||||
controller_name
|
||||
))))?;
|
||||
let path_from_mountinfo = s.to_controller().base().to_string_lossy().to_string();
|
||||
|
||||
let des_path = get_cgroup_destination(path_from_mountinfo, path_from_cgroup.to_owned());
|
||||
mount_roots_subsystems_map.insert(controller_name, des_path);
|
||||
}
|
||||
Ok(mount_roots_subsystems_map)
|
||||
}
|
||||
|
||||
fn get_cgroups_relative_paths_by_path(path: String) -> Result<HashMap<String, String>> {
|
||||
let mut m = HashMap::new();
|
||||
let content =
|
||||
fs::read_to_string(path.clone()).map_err(|e| Error::with_cause(ReadFailed(path), e))?;
|
||||
for l in content.lines() {
|
||||
let fl: Vec<&str> = l.split(':').collect();
|
||||
if fl.len() != 3 {
|
||||
continue;
|
||||
}
|
||||
|
||||
let keys: Vec<&str> = fl[1].split(',').collect();
|
||||
for key in &keys {
|
||||
m.insert(key.to_string(), fl[2].to_string());
|
||||
// cgroup path may have ":" , likes
|
||||
// "2:cpu,cpuacct:/system.slice/containerd.service/test.slice:cri-containerd:96b37a2edf84351487f42039e137427f1812f678850675fac214caf597ee5e4a"
|
||||
for line in content.lines() {
|
||||
if let Some((first_value_part, remaining_path)) =
|
||||
line.split_once(':').unwrap_or_default().1.split_once(':')
|
||||
{
|
||||
let keys: Vec<&str> = first_value_part.split(',').collect();
|
||||
keys.iter().for_each(|key| {
|
||||
m.insert(key.to_string(), remaining_path.to_string());
|
||||
});
|
||||
}
|
||||
}
|
||||
Ok(m)
|
||||
|
||||
@@ -57,11 +57,11 @@
|
||||
//! .read(6, 1, 10)
|
||||
//! .write(11, 1, 100)
|
||||
//! .done()
|
||||
//! .build(h);
|
||||
//! .build(h).unwrap();
|
||||
//! ```
|
||||
|
||||
use crate::{
|
||||
BlkIoDeviceResource, BlkIoDeviceThrottleResource, Cgroup, DeviceResource, Hierarchy,
|
||||
BlkIoDeviceResource, BlkIoDeviceThrottleResource, Cgroup, DeviceResource, Error, Hierarchy,
|
||||
HugePageResource, MaxValue, NetworkPriority, Resources,
|
||||
};
|
||||
|
||||
@@ -80,6 +80,8 @@ pub struct CgroupBuilder {
|
||||
name: String,
|
||||
/// Internal, unsupported field: use the associated builders instead.
|
||||
resources: Resources,
|
||||
/// List of controllers specifically enabled in the control group.
|
||||
specified_controllers: Option<Vec<String>>,
|
||||
}
|
||||
|
||||
impl CgroupBuilder {
|
||||
@@ -90,6 +92,7 @@ impl CgroupBuilder {
|
||||
CgroupBuilder {
|
||||
name: name.to_owned(),
|
||||
resources: Resources::default(),
|
||||
specified_controllers: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -134,10 +137,22 @@ impl CgroupBuilder {
|
||||
}
|
||||
|
||||
/// Finalize the control group, consuming the builder and creating the control group.
|
||||
pub fn build(self, hier: Box<dyn Hierarchy>) -> Cgroup {
|
||||
let cg = Cgroup::new(hier, self.name);
|
||||
let _ret = cg.apply(&self.resources);
|
||||
cg
|
||||
pub fn build(self, hier: Box<dyn Hierarchy>) -> Result<Cgroup, Error> {
|
||||
if let Some(controllers) = self.specified_controllers {
|
||||
let cg = Cgroup::new_with_specified_controllers(hier, self.name, Some(controllers))?;
|
||||
cg.apply(&self.resources)?;
|
||||
Ok(cg)
|
||||
} else {
|
||||
let cg = Cgroup::new(hier, self.name)?;
|
||||
cg.apply(&self.resources)?;
|
||||
Ok(cg)
|
||||
}
|
||||
}
|
||||
|
||||
/// Specifically enable some controllers in the control group.
|
||||
pub fn set_specified_controllers(mut self, specified_controllers: Vec<String>) -> Self {
|
||||
self.specified_controllers = Some(specified_controllers);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -59,7 +59,6 @@ impl ControllerInternal for CpuController {
|
||||
fn get_path(&self) -> &PathBuf {
|
||||
&self.path
|
||||
}
|
||||
|
||||
fn get_path_mut(&mut self) -> &mut PathBuf {
|
||||
&mut self.path
|
||||
}
|
||||
@@ -113,10 +112,10 @@ impl<'a> From<&'a Subsystem> for &'a CpuController {
|
||||
|
||||
impl CpuController {
|
||||
/// Contructs a new `CpuController` with `root` serving as the root of the control group.
|
||||
pub fn new(root: PathBuf, v2: bool) -> Self {
|
||||
pub fn new(point: PathBuf, root: PathBuf, v2: bool) -> Self {
|
||||
Self {
|
||||
base: root.clone(),
|
||||
path: root,
|
||||
base: root,
|
||||
path: point,
|
||||
v2,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,10 +100,10 @@ impl<'a> From<&'a Subsystem> for &'a CpuAcctController {
|
||||
|
||||
impl CpuAcctController {
|
||||
/// Contructs a new `CpuAcctController` with `root` serving as the root of the control group.
|
||||
pub fn new(root: PathBuf) -> Self {
|
||||
pub fn new(point: PathBuf, root: PathBuf) -> Self {
|
||||
Self {
|
||||
base: root.clone(),
|
||||
path: root,
|
||||
base: root,
|
||||
path: point,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -254,10 +254,10 @@ fn parse_range(s: String) -> Result<Vec<(u64, u64)>> {
|
||||
|
||||
impl CpuSetController {
|
||||
/// Contructs a new `CpuSetController` with `root` serving as the root of the control group.
|
||||
pub fn new(root: PathBuf, v2: bool) -> Self {
|
||||
pub fn new(point: PathBuf, root: PathBuf, v2: bool) -> Self {
|
||||
Self {
|
||||
base: root.clone(),
|
||||
path: root,
|
||||
base: root,
|
||||
path: point,
|
||||
v2,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,6 +46,7 @@ pub enum DeviceType {
|
||||
Block,
|
||||
}
|
||||
|
||||
#[allow(clippy::derivable_impls)]
|
||||
impl Default for DeviceType {
|
||||
fn default() -> Self {
|
||||
DeviceType::All
|
||||
@@ -170,9 +171,9 @@ impl ControllerInternal for DevicesController {
|
||||
|
||||
for i in &res.devices {
|
||||
if i.allow {
|
||||
let _ = self.allow_device(i.devtype, i.major, i.minor, &i.access);
|
||||
self.allow_device(i.devtype, i.major, i.minor, &i.access)?;
|
||||
} else {
|
||||
let _ = self.deny_device(i.devtype, i.major, i.minor, &i.access);
|
||||
self.deny_device(i.devtype, i.major, i.minor, &i.access)?;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -203,10 +204,10 @@ impl<'a> From<&'a Subsystem> for &'a DevicesController {
|
||||
|
||||
impl DevicesController {
|
||||
/// Constructs a new `DevicesController` with `root` serving as the root of the control group.
|
||||
pub fn new(root: PathBuf) -> Self {
|
||||
pub fn new(point: PathBuf, root: PathBuf) -> Self {
|
||||
Self {
|
||||
base: root.clone(),
|
||||
path: root,
|
||||
base: root,
|
||||
path: point,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -238,7 +239,13 @@ impl DevicesController {
|
||||
let final_str = format!("{} {}:{} {}", devtype.to_char(), major, minor, perms);
|
||||
self.open_path("devices.allow", true).and_then(|mut file| {
|
||||
file.write_all(final_str.as_ref()).map_err(|e| {
|
||||
Error::with_cause(WriteFailed("devices.allow".to_string(), final_str), e)
|
||||
Error::with_cause(
|
||||
WriteFailed(
|
||||
self.get_path().join("devices.allow").display().to_string(),
|
||||
final_str,
|
||||
),
|
||||
e,
|
||||
)
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -271,7 +278,13 @@ impl DevicesController {
|
||||
let final_str = format!("{} {}:{} {}", devtype.to_char(), major, minor, perms);
|
||||
self.open_path("devices.deny", true).and_then(|mut file| {
|
||||
file.write_all(final_str.as_ref()).map_err(|e| {
|
||||
Error::with_cause(WriteFailed("devices.deny".to_string(), final_str), e)
|
||||
Error::with_cause(
|
||||
WriteFailed(
|
||||
self.get_path().join("devices.deny").display().to_string(),
|
||||
final_str,
|
||||
),
|
||||
e,
|
||||
)
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -284,7 +297,7 @@ impl DevicesController {
|
||||
match res {
|
||||
Ok(_) => {
|
||||
s.lines().fold(Ok(Vec::new()), |acc, line| {
|
||||
let ls = line.to_string().split(|c| c == ' ' || c == ':').map(|x| x.to_string()).collect::<Vec<String>>();
|
||||
let ls = line.split(|c| c == ' ' || c == ':').map(|x| x.to_string()).collect::<Vec<String>>();
|
||||
if acc.is_err() || ls.len() != 4 {
|
||||
error!("allowed_devices: acc: {:?}, ls: {:?}", acc, ls);
|
||||
Err(Error::new(ParseError))
|
||||
|
||||
18
src/error.rs
18
src/error.rs
@@ -51,6 +51,22 @@ pub enum ErrorKind {
|
||||
#[error("invalid bytes size")]
|
||||
InvalidBytesSize,
|
||||
|
||||
/// The specified controller is not in the list of supported controllers.
|
||||
#[error("specified controller is not in the list of supported controllers")]
|
||||
SpecifiedControllers,
|
||||
|
||||
/// Using method in wrong cgroup version.
|
||||
#[error("using method in wrong cgroup version")]
|
||||
CgroupVersion,
|
||||
|
||||
/// Using method in wrong cgroup mode.
|
||||
#[error("using method in wrong cgroup mode.")]
|
||||
CgroupMode,
|
||||
|
||||
/// Subsystems is empty.
|
||||
#[error("subsystems is empty")]
|
||||
SubsystemsEmpty,
|
||||
|
||||
/// An unknown error has occured.
|
||||
#[error("an unknown error")]
|
||||
Other,
|
||||
@@ -73,7 +89,7 @@ impl fmt::Display for Error {
|
||||
}
|
||||
|
||||
impl StdError for Error {
|
||||
fn cause(&self) -> Option<&dyn StdError> {
|
||||
fn source(&self) -> Option<&(dyn StdError + 'static)> {
|
||||
#[allow(clippy::manual_map)]
|
||||
match self.cause {
|
||||
Some(ref x) => Some(&**x),
|
||||
|
||||
@@ -84,14 +84,13 @@ impl<'a> From<&'a Subsystem> for &'a FreezerController {
|
||||
|
||||
impl FreezerController {
|
||||
/// Contructs a new `FreezerController` with `root` serving as the root of the control group.
|
||||
pub fn new(root: PathBuf, v2: bool) -> Self {
|
||||
pub fn new(point: PathBuf, root: PathBuf, v2: bool) -> Self {
|
||||
Self {
|
||||
base: root.clone(),
|
||||
path: root,
|
||||
base: root,
|
||||
path: point,
|
||||
v2,
|
||||
}
|
||||
}
|
||||
|
||||
/// Freezes the processes in the control group.
|
||||
pub fn freeze(&self) -> Result<()> {
|
||||
let mut file_name = "freezer.state";
|
||||
|
||||
@@ -5,14 +5,11 @@
|
||||
//
|
||||
|
||||
//! This module represents the various control group hierarchies the Linux kernel supports.
|
||||
//!
|
||||
//! Currently, we only support the cgroupv1 hierarchy, but in the future we will add support for
|
||||
//! the Unified Hierarchy.
|
||||
|
||||
use std::fs;
|
||||
use std::fs::File;
|
||||
use std::io::{BufRead, BufReader};
|
||||
use std::path::PathBuf;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use crate::blkio::BlkIoController;
|
||||
use crate::cpu::CpuController;
|
||||
@@ -37,6 +34,8 @@ use crate::cgroup::Cgroup;
|
||||
/// See `proc(5)` for format details.
|
||||
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
|
||||
pub struct Mountinfo {
|
||||
/// Mount root directory of the file system.
|
||||
pub mount_root: PathBuf,
|
||||
/// Mount pathname relative to the process's root.
|
||||
pub mount_point: PathBuf,
|
||||
/// Filesystem type (main type with optional sub-type).
|
||||
@@ -57,6 +56,7 @@ pub(crate) fn parse_mountinfo_for_line(line: &str) -> Option<Mountinfo> {
|
||||
return None;
|
||||
}
|
||||
let mount_point = PathBuf::from(s0_values[4]);
|
||||
let mount_root = PathBuf::from(s0_values[3]);
|
||||
let fs_type_values: Vec<_> = s1_values[0].trim().split('.').collect();
|
||||
let fs_type = match fs_type_values.len() {
|
||||
1 => (fs_type_values[0].to_string(), None),
|
||||
@@ -69,6 +69,7 @@ pub(crate) fn parse_mountinfo_for_line(line: &str) -> Option<Mountinfo> {
|
||||
|
||||
let super_opts: Vec<String> = s1_values[2].trim().split(',').map(String::from).collect();
|
||||
Some(Mountinfo {
|
||||
mount_root,
|
||||
mount_point,
|
||||
fs_type,
|
||||
super_opts,
|
||||
@@ -123,47 +124,53 @@ impl Hierarchy for V1 {
|
||||
// The cgroup writeback feature requires cooperation between memcgs and blkcgs
|
||||
// To avoid exceptions, we should add_task for blkcg before memcg(push BlkIo before Mem)
|
||||
// For more Information: https://www.alibabacloud.com/help/doc-detail/155509.htm
|
||||
if let Some(root) = self.get_mount_point(Controllers::BlkIo) {
|
||||
subs.push(Subsystem::BlkIo(BlkIoController::new(root, false)));
|
||||
if let Some((point, root)) = self.get_mount_point(Controllers::BlkIo) {
|
||||
subs.push(Subsystem::BlkIo(BlkIoController::new(point, root, false)));
|
||||
}
|
||||
if let Some(root) = self.get_mount_point(Controllers::Mem) {
|
||||
subs.push(Subsystem::Mem(MemController::new(root, false)));
|
||||
if let Some((point, root)) = self.get_mount_point(Controllers::Mem) {
|
||||
subs.push(Subsystem::Mem(MemController::new(point, root, false)));
|
||||
}
|
||||
if let Some(root) = self.get_mount_point(Controllers::Pids) {
|
||||
subs.push(Subsystem::Pid(PidController::new(root, false)));
|
||||
if let Some((point, root)) = self.get_mount_point(Controllers::Pids) {
|
||||
subs.push(Subsystem::Pid(PidController::new(point, root, false)));
|
||||
}
|
||||
if let Some(root) = self.get_mount_point(Controllers::CpuSet) {
|
||||
subs.push(Subsystem::CpuSet(CpuSetController::new(root, false)));
|
||||
if let Some((point, root)) = self.get_mount_point(Controllers::CpuSet) {
|
||||
subs.push(Subsystem::CpuSet(CpuSetController::new(point, root, false)));
|
||||
}
|
||||
if let Some(root) = self.get_mount_point(Controllers::CpuAcct) {
|
||||
subs.push(Subsystem::CpuAcct(CpuAcctController::new(root)));
|
||||
if let Some((point, root)) = self.get_mount_point(Controllers::CpuAcct) {
|
||||
subs.push(Subsystem::CpuAcct(CpuAcctController::new(point, root)));
|
||||
}
|
||||
if let Some(root) = self.get_mount_point(Controllers::Cpu) {
|
||||
subs.push(Subsystem::Cpu(CpuController::new(root, false)));
|
||||
if let Some((point, root)) = self.get_mount_point(Controllers::Cpu) {
|
||||
subs.push(Subsystem::Cpu(CpuController::new(point, root, false)));
|
||||
}
|
||||
if let Some(root) = self.get_mount_point(Controllers::Devices) {
|
||||
subs.push(Subsystem::Devices(DevicesController::new(root)));
|
||||
if let Some((point, root)) = self.get_mount_point(Controllers::Devices) {
|
||||
subs.push(Subsystem::Devices(DevicesController::new(point, root)));
|
||||
}
|
||||
if let Some(root) = self.get_mount_point(Controllers::Freezer) {
|
||||
subs.push(Subsystem::Freezer(FreezerController::new(root, false)));
|
||||
if let Some((point, root)) = self.get_mount_point(Controllers::Freezer) {
|
||||
subs.push(Subsystem::Freezer(FreezerController::new(
|
||||
point, root, false,
|
||||
)));
|
||||
}
|
||||
if let Some(root) = self.get_mount_point(Controllers::NetCls) {
|
||||
subs.push(Subsystem::NetCls(NetClsController::new(root)));
|
||||
if let Some((point, root)) = self.get_mount_point(Controllers::NetCls) {
|
||||
subs.push(Subsystem::NetCls(NetClsController::new(point, root)));
|
||||
}
|
||||
if let Some(root) = self.get_mount_point(Controllers::PerfEvent) {
|
||||
subs.push(Subsystem::PerfEvent(PerfEventController::new(root)));
|
||||
if let Some((point, root)) = self.get_mount_point(Controllers::PerfEvent) {
|
||||
subs.push(Subsystem::PerfEvent(PerfEventController::new(point, root)));
|
||||
}
|
||||
if let Some(root) = self.get_mount_point(Controllers::NetPrio) {
|
||||
subs.push(Subsystem::NetPrio(NetPrioController::new(root)));
|
||||
if let Some((point, root)) = self.get_mount_point(Controllers::NetPrio) {
|
||||
subs.push(Subsystem::NetPrio(NetPrioController::new(point, root)));
|
||||
}
|
||||
if let Some(root) = self.get_mount_point(Controllers::HugeTlb) {
|
||||
subs.push(Subsystem::HugeTlb(HugeTlbController::new(root, false)));
|
||||
if let Some((point, root)) = self.get_mount_point(Controllers::HugeTlb) {
|
||||
subs.push(Subsystem::HugeTlb(HugeTlbController::new(
|
||||
point, root, false,
|
||||
)));
|
||||
}
|
||||
if let Some(root) = self.get_mount_point(Controllers::Rdma) {
|
||||
subs.push(Subsystem::Rdma(RdmaController::new(root)));
|
||||
if let Some((point, root)) = self.get_mount_point(Controllers::Rdma) {
|
||||
subs.push(Subsystem::Rdma(RdmaController::new(point, root)));
|
||||
}
|
||||
if let Some(root) = self.get_mount_point(Controllers::Systemd) {
|
||||
subs.push(Subsystem::Systemd(SystemdController::new(root, false)));
|
||||
if let Some((point, root)) = self.get_mount_point(Controllers::Systemd) {
|
||||
subs.push(Subsystem::Systemd(SystemdController::new(
|
||||
point, root, false,
|
||||
)));
|
||||
}
|
||||
|
||||
subs
|
||||
@@ -173,6 +180,12 @@ impl Hierarchy for V1 {
|
||||
Cgroup::load(auto(), "")
|
||||
}
|
||||
|
||||
fn parent_control_group(&self, path: &str) -> Cgroup {
|
||||
let path = Path::new(path);
|
||||
let parent_path = path.parent().unwrap().to_string_lossy().to_string();
|
||||
Cgroup::load(auto(), parent_path)
|
||||
}
|
||||
|
||||
fn root(&self) -> PathBuf {
|
||||
self.mountinfo
|
||||
.iter()
|
||||
@@ -212,29 +225,51 @@ impl Hierarchy for V2 {
|
||||
for s in controller_list {
|
||||
match s {
|
||||
"cpu" => {
|
||||
subs.push(Subsystem::Cpu(CpuController::new(self.root(), true)));
|
||||
subs.push(Subsystem::Cpu(CpuController::new(
|
||||
self.root(),
|
||||
PathBuf::from(""),
|
||||
true,
|
||||
)));
|
||||
}
|
||||
"io" => {
|
||||
subs.push(Subsystem::BlkIo(BlkIoController::new(self.root(), true)));
|
||||
subs.push(Subsystem::BlkIo(BlkIoController::new(
|
||||
self.root(),
|
||||
PathBuf::from(""),
|
||||
true,
|
||||
)));
|
||||
}
|
||||
"cpuset" => {
|
||||
subs.push(Subsystem::CpuSet(CpuSetController::new(self.root(), true)));
|
||||
subs.push(Subsystem::CpuSet(CpuSetController::new(
|
||||
self.root(),
|
||||
PathBuf::from(""),
|
||||
true,
|
||||
)));
|
||||
}
|
||||
"memory" => {
|
||||
subs.push(Subsystem::Mem(MemController::new(self.root(), true)));
|
||||
subs.push(Subsystem::Mem(MemController::new(
|
||||
self.root(),
|
||||
PathBuf::from(""),
|
||||
true,
|
||||
)));
|
||||
}
|
||||
"pids" => {
|
||||
subs.push(Subsystem::Pid(PidController::new(self.root(), true)));
|
||||
subs.push(Subsystem::Pid(PidController::new(
|
||||
self.root(),
|
||||
PathBuf::from(""),
|
||||
true,
|
||||
)));
|
||||
}
|
||||
"freezer" => {
|
||||
subs.push(Subsystem::Freezer(FreezerController::new(
|
||||
self.root(),
|
||||
PathBuf::from(""),
|
||||
true,
|
||||
)));
|
||||
}
|
||||
"hugetlb" => {
|
||||
subs.push(Subsystem::HugeTlb(HugeTlbController::new(
|
||||
self.root(),
|
||||
PathBuf::from(""),
|
||||
true,
|
||||
)));
|
||||
}
|
||||
@@ -249,6 +284,12 @@ impl Hierarchy for V2 {
|
||||
Cgroup::load(auto(), "")
|
||||
}
|
||||
|
||||
fn parent_control_group(&self, path: &str) -> Cgroup {
|
||||
let path = Path::new(path);
|
||||
let parent_path = path.parent().unwrap().to_string_lossy().to_string();
|
||||
Cgroup::load(auto(), parent_path)
|
||||
}
|
||||
|
||||
fn root(&self) -> PathBuf {
|
||||
PathBuf::from(self.root.clone())
|
||||
}
|
||||
@@ -263,10 +304,10 @@ impl V1 {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_mount_point(&self, controller: Controllers) -> Option<PathBuf> {
|
||||
pub fn get_mount_point(&self, controller: Controllers) -> Option<(PathBuf, PathBuf)> {
|
||||
self.mountinfo.iter().find_map(|m| {
|
||||
if m.fs_type.0 == "cgroup" && m.super_opts.contains(&controller.to_string()) {
|
||||
return Some(m.mount_point.clone());
|
||||
return Some((m.mount_point.to_owned(), m.mount_root.to_owned()));
|
||||
}
|
||||
None
|
||||
})
|
||||
@@ -297,43 +338,16 @@ impl Default for V2 {
|
||||
|
||||
pub const UNIFIED_MOUNTPOINT: &str = "/sys/fs/cgroup";
|
||||
|
||||
#[cfg(any(
|
||||
all(target_os = "linux", not(target_env = "musl")),
|
||||
target_os = "android"
|
||||
))]
|
||||
pub fn is_cgroup2_unified_mode() -> bool {
|
||||
use nix::sys::statfs;
|
||||
|
||||
let path = std::path::Path::new(UNIFIED_MOUNTPOINT);
|
||||
let fs_stat = statfs::statfs(path);
|
||||
if fs_stat.is_err() {
|
||||
return false;
|
||||
}
|
||||
let fs_stat = match statfs::statfs(path) {
|
||||
Ok(fs_stat) => fs_stat,
|
||||
Err(_) => return false,
|
||||
};
|
||||
|
||||
// FIXME notwork, nix will not compile CGROUP2_SUPER_MAGIC because not(target_env = "musl")
|
||||
fs_stat.unwrap().filesystem_type() == statfs::CGROUP2_SUPER_MAGIC
|
||||
}
|
||||
|
||||
pub const INIT_CGROUP_PATHS: &str = "/proc/1/cgroup";
|
||||
|
||||
#[cfg(all(target_os = "linux", target_env = "musl"))]
|
||||
pub fn is_cgroup2_unified_mode() -> bool {
|
||||
let lines = fs::read_to_string(INIT_CGROUP_PATHS);
|
||||
if lines.is_err() {
|
||||
return false;
|
||||
}
|
||||
|
||||
for line in lines.unwrap().lines() {
|
||||
let fields: Vec<&str> = line.split(':').collect();
|
||||
if fields.len() != 3 {
|
||||
continue;
|
||||
}
|
||||
if fields[0] != "0" {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
true
|
||||
fs_stat.filesystem_type() == statfs::CGROUP2_SUPER_MAGIC
|
||||
}
|
||||
|
||||
pub fn auto() -> Box<dyn Hierarchy> {
|
||||
@@ -352,19 +366,19 @@ mod tests {
|
||||
fn test_parse_mount() {
|
||||
let mountinfo = vec![
|
||||
("29 26 0:26 / /sys/fs/cgroup/cpuset,cpu,cpuacct rw,nosuid,nodev,noexec,relatime shared:10 - cgroup cgroup rw,cpuset,cpu,cpuacct",
|
||||
Mountinfo{mount_point: PathBuf::from("/sys/fs/cgroup/cpuset,cpu,cpuacct"), fs_type: ("cgroup".to_string(), None), super_opts: vec![
|
||||
Mountinfo{mount_root: PathBuf::from("/"), mount_point: PathBuf::from("/sys/fs/cgroup/cpuset,cpu,cpuacct"), fs_type: ("cgroup".to_string(), None), super_opts: vec![
|
||||
"rw".to_string(),
|
||||
"cpuset".to_string(),
|
||||
"cpu".to_string(),
|
||||
"cpuacct".to_string(),
|
||||
]}),
|
||||
("121 1731 0:42 / /shm rw,nosuid,nodev,noexec,relatime shared:68 master:66 - tmpfs shm rw,size=65536k",
|
||||
Mountinfo{mount_point: PathBuf::from("/shm"), fs_type: ("tmpfs".to_string(), None), super_opts: vec![
|
||||
Mountinfo{mount_root: PathBuf::from("/"), mount_point: PathBuf::from("/shm"), fs_type: ("tmpfs".to_string(), None), super_opts: vec![
|
||||
"rw".to_string(),
|
||||
"size=65536k".to_string(),
|
||||
]}),
|
||||
("121 1731 0:42 / /shm rw,nosuid,nodev,noexec,relatime shared:68 master:66 - tmpfs.123 shm rw,size=65536k",
|
||||
Mountinfo{mount_point: PathBuf::from("/shm"), fs_type: ("tmpfs".to_string(), Some("123".to_string())), super_opts: vec![
|
||||
Mountinfo{mount_root: PathBuf::from("/"), mount_point: PathBuf::from("/shm"), fs_type: ("tmpfs".to_string(), Some("123".to_string())), super_opts: vec![
|
||||
"rw".to_string(),
|
||||
"size=65536k".to_string(),
|
||||
]}),
|
||||
|
||||
@@ -88,11 +88,11 @@ impl<'a> From<&'a Subsystem> for &'a HugeTlbController {
|
||||
|
||||
impl HugeTlbController {
|
||||
/// Constructs a new `HugeTlbController` with `root` serving as the root of the control group.
|
||||
pub fn new(root: PathBuf, v2: bool) -> Self {
|
||||
pub fn new(point: PathBuf, root: PathBuf, v2: bool) -> Self {
|
||||
let sizes = get_hugepage_sizes();
|
||||
Self {
|
||||
base: root.clone(),
|
||||
path: root,
|
||||
base: root,
|
||||
path: point,
|
||||
sizes,
|
||||
v2,
|
||||
}
|
||||
@@ -138,8 +138,11 @@ impl HugeTlbController {
|
||||
/// Get the limit (in bytes) of how much memory can be backed by hugepages of a certain size
|
||||
/// (`hugetlb_size`).
|
||||
pub fn limit_in_bytes(&self, hugetlb_size: &str) -> Result<u64> {
|
||||
self.open_path(&format!("hugetlb.{}.limit_in_bytes", hugetlb_size), false)
|
||||
.and_then(read_u64_from)
|
||||
let mut file_name = format!("hugetlb.{}.limit_in_bytes", hugetlb_size);
|
||||
if self.v2 {
|
||||
file_name = format!("hugetlb.{}.max", hugetlb_size);
|
||||
}
|
||||
self.open_path(&file_name, false).and_then(read_u64_from)
|
||||
}
|
||||
|
||||
/// Get the current usage of memory that is backed by hugepages of a certain size
|
||||
|
||||
107
src/lib.rs
107
src/lib.rs
@@ -231,6 +231,7 @@ mod sealed {
|
||||
})
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn get(&self, key: &str) -> Result<String> {
|
||||
self.open_path(key, false).and_then(|mut file: File| {
|
||||
let mut string = String::new();
|
||||
@@ -255,6 +256,9 @@ pub trait Controller {
|
||||
/// The file system path to the controller.
|
||||
fn path(&self) -> &Path;
|
||||
|
||||
/// Root path of the file system to the controller.
|
||||
fn base(&self) -> &Path;
|
||||
|
||||
/// Apply a set of resources to the Controller, invoking its internal functions to pass the
|
||||
/// kernel the information.
|
||||
fn apply(&self, res: &Resources) -> Result<()>;
|
||||
@@ -280,9 +284,18 @@ pub trait Controller {
|
||||
/// Attach a task to this controller.
|
||||
fn add_task_by_tgid(&self, pid: &CgroupPid) -> Result<()>;
|
||||
|
||||
/// set cgroup type.
|
||||
fn set_cgroup_type(&self, cgroup_type: &str) -> Result<()>;
|
||||
|
||||
/// get cgroup type.
|
||||
fn get_cgroup_type(&self) -> Result<String>;
|
||||
|
||||
/// Get the list of tasks that this controller has.
|
||||
fn tasks(&self) -> Vec<CgroupPid>;
|
||||
|
||||
/// Get the list of procs that this controller has.
|
||||
fn procs(&self) -> Vec<CgroupPid>;
|
||||
|
||||
fn v2(&self) -> bool;
|
||||
}
|
||||
|
||||
@@ -298,6 +311,10 @@ where
|
||||
self.get_path()
|
||||
}
|
||||
|
||||
fn base(&self) -> &Path {
|
||||
self.get_base()
|
||||
}
|
||||
|
||||
/// Apply a set of resources to the Controller, invoking its internal functions to pass the
|
||||
/// kernel the information.
|
||||
fn apply(&self, res: &Resources) -> Result<()> {
|
||||
@@ -317,6 +334,9 @@ where
|
||||
|
||||
/// Set notify_on_release
|
||||
fn set_notify_on_release(&self, enable: bool) -> Result<()> {
|
||||
if self.is_v2() {
|
||||
return Err(Error::new(ErrorKind::CgroupVersion));
|
||||
}
|
||||
self.open_path("notify_on_release", true)
|
||||
.and_then(|mut file| {
|
||||
write!(file, "{}", enable as i32).map_err(|e| {
|
||||
@@ -330,6 +350,9 @@ where
|
||||
|
||||
/// Set release_agent
|
||||
fn set_release_agent(&self, path: &str) -> Result<()> {
|
||||
if self.is_v2() {
|
||||
return Err(Error::new(ErrorKind::CgroupVersion));
|
||||
}
|
||||
self.open_path("release_agent", true).and_then(|mut file| {
|
||||
file.write_all(path.as_bytes()).map_err(|e| {
|
||||
Error::with_cause(
|
||||
@@ -373,7 +396,7 @@ where
|
||||
fn add_task(&self, pid: &CgroupPid) -> Result<()> {
|
||||
let mut file_name = "tasks";
|
||||
if self.is_v2() {
|
||||
file_name = "cgroup.procs";
|
||||
file_name = "cgroup.threads";
|
||||
}
|
||||
self.open_path(file_name, true).and_then(|mut file| {
|
||||
file.write_all(pid.pid.to_string().as_ref()).map_err(|e| {
|
||||
@@ -387,23 +410,21 @@ where
|
||||
|
||||
/// Attach a task to this controller by thread group id.
|
||||
fn add_task_by_tgid(&self, pid: &CgroupPid) -> Result<()> {
|
||||
self.open_path("cgroup.procs", true).and_then(|mut file| {
|
||||
let file_name = "cgroup.procs";
|
||||
self.open_path(file_name, true).and_then(|mut file| {
|
||||
file.write_all(pid.pid.to_string().as_ref()).map_err(|e| {
|
||||
Error::with_cause(
|
||||
ErrorKind::WriteFailed("cgroup.procs".to_string(), pid.pid.to_string()),
|
||||
ErrorKind::WriteFailed(file_name.to_string(), pid.pid.to_string()),
|
||||
e,
|
||||
)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/// Get the list of tasks that this controller has.
|
||||
fn tasks(&self) -> Vec<CgroupPid> {
|
||||
let mut file = "tasks";
|
||||
if self.is_v2() {
|
||||
file = "cgroup.procs";
|
||||
}
|
||||
self.open_path(file, false)
|
||||
/// Get the list of procs that this controller has.
|
||||
fn procs(&self) -> Vec<CgroupPid> {
|
||||
let file_name = "cgroup.procs";
|
||||
self.open_path(file_name, false)
|
||||
.map(|file| {
|
||||
let bf = BufReader::new(file);
|
||||
let mut v = Vec::new();
|
||||
@@ -421,6 +442,64 @@ where
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Get the list of tasks that this controller has.
|
||||
fn tasks(&self) -> Vec<CgroupPid> {
|
||||
let mut file_name = "tasks";
|
||||
if self.is_v2() {
|
||||
file_name = "cgroup.threads";
|
||||
}
|
||||
self.open_path(file_name, false)
|
||||
.map(|file| {
|
||||
let bf = BufReader::new(file);
|
||||
let mut v = Vec::new();
|
||||
for line in bf.lines() {
|
||||
match line {
|
||||
Ok(line) => {
|
||||
let n = line.trim().parse().unwrap_or(0u64);
|
||||
v.push(n);
|
||||
}
|
||||
Err(_) => break,
|
||||
}
|
||||
}
|
||||
v.into_iter().map(CgroupPid::from).collect()
|
||||
})
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
/// set cgroup.type
|
||||
fn set_cgroup_type(&self, cgroup_type: &str) -> Result<()> {
|
||||
if !self.is_v2() {
|
||||
return Err(Error::new(ErrorKind::CgroupVersion));
|
||||
}
|
||||
let file_name = "cgroup.type";
|
||||
self.open_path(file_name, true).and_then(|mut file| {
|
||||
file.write_all(cgroup_type.as_bytes()).map_err(|e| {
|
||||
Error::with_cause(
|
||||
ErrorKind::WriteFailed(file_name.to_string(), cgroup_type.to_string()),
|
||||
e,
|
||||
)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/// get cgroup.type
|
||||
fn get_cgroup_type(&self) -> Result<String> {
|
||||
if !self.is_v2() {
|
||||
return Err(Error::new(ErrorKind::CgroupVersion));
|
||||
}
|
||||
let file_name = "cgroup.type";
|
||||
self.open_path(file_name, false).and_then(|mut file: File| {
|
||||
let mut string = String::new();
|
||||
match file.read_to_string(&mut string) {
|
||||
Ok(_) => Ok(string.trim().to_owned()),
|
||||
Err(e) => Err(Error::with_cause(
|
||||
ErrorKind::ReadFailed(file_name.to_string()),
|
||||
e,
|
||||
)),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn v2(&self) -> bool {
|
||||
self.is_v2()
|
||||
}
|
||||
@@ -468,6 +547,9 @@ pub trait Hierarchy: std::fmt::Debug + Send + Sync {
|
||||
/// Return a handle to the root control group in the hierarchy.
|
||||
fn root_control_group(&self) -> Cgroup;
|
||||
|
||||
/// Return a handle to the parent control group in the hierarchy.
|
||||
fn parent_control_group(&self, path: &str) -> Cgroup;
|
||||
|
||||
fn v2(&self) -> bool;
|
||||
}
|
||||
|
||||
@@ -530,9 +612,9 @@ pub struct CpuResources {
|
||||
/// Weight of how much of the total CPU time should this control group get. Note that this is
|
||||
/// hierarchical, so this is weighted against the siblings of this control group.
|
||||
pub shares: Option<u64>,
|
||||
/// In one `period`, how much can the tasks run in nanoseconds.
|
||||
/// In one `period`, how much can the tasks run in microseconds.
|
||||
pub quota: Option<i64>,
|
||||
/// Period of time in nanoseconds.
|
||||
/// Period of time in microseconds.
|
||||
pub period: Option<u64>,
|
||||
/// This is currently a no-operation.
|
||||
pub realtime_runtime: Option<i64>,
|
||||
@@ -805,6 +887,7 @@ pub enum MaxValue {
|
||||
Value(i64),
|
||||
}
|
||||
|
||||
#[allow(clippy::derivable_impls)]
|
||||
impl Default for MaxValue {
|
||||
fn default() -> Self {
|
||||
MaxValue::Max
|
||||
|
||||
@@ -540,10 +540,10 @@ impl ControllerInternal for MemController {
|
||||
|
||||
impl MemController {
|
||||
/// Contructs a new `MemController` with `root` serving as the root of the control group.
|
||||
pub fn new(root: PathBuf, v2: bool) -> Self {
|
||||
pub fn new(point: PathBuf, root: PathBuf, v2: bool) -> Self {
|
||||
Self {
|
||||
base: root.clone(),
|
||||
path: root,
|
||||
base: root,
|
||||
path: point,
|
||||
v2,
|
||||
}
|
||||
}
|
||||
@@ -573,18 +573,33 @@ impl MemController {
|
||||
|
||||
// for v2
|
||||
pub fn get_mem(&self) -> Result<SetMemory> {
|
||||
let mut m: SetMemory = Default::default();
|
||||
self.get_max_value("memory.high")
|
||||
.map(|x| m.high = Some(x))?;
|
||||
self.get_max_value("memory.low").map(|x| m.low = Some(x))?;
|
||||
self.get_max_value("memory.max").map(|x| m.max = Some(x))?;
|
||||
self.get_max_value("memory.min").map(|x| m.min = Some(x))?;
|
||||
let m = SetMemory {
|
||||
high: self
|
||||
.get_max_value("memory.high")
|
||||
.map_or(Some(MaxValue::default()), Some),
|
||||
low: self
|
||||
.get_max_value("memory.low")
|
||||
.map_or(Some(MaxValue::Value(0)), Some),
|
||||
max: self
|
||||
.get_max_value("memory.max")
|
||||
.map_or(Some(MaxValue::default()), Some),
|
||||
min: self
|
||||
.get_max_value("memory.min")
|
||||
.map_or(Some(MaxValue::Value(0)), Some),
|
||||
};
|
||||
|
||||
Ok(m)
|
||||
}
|
||||
|
||||
fn memory_stat_v2(&self) -> Memory {
|
||||
let set = self.get_mem().unwrap();
|
||||
// NOTE: get_mem() always returns T, but let's
|
||||
// still do `unwrap_or` for safety.
|
||||
let set = self.get_mem().unwrap_or(SetMemory {
|
||||
low: Some(MaxValue::Value(0)),
|
||||
high: Some(MaxValue::default()),
|
||||
max: Some(MaxValue::default()),
|
||||
min: Some(MaxValue::Value(0)),
|
||||
});
|
||||
|
||||
Memory {
|
||||
fail_cnt: 0,
|
||||
@@ -593,7 +608,10 @@ impl MemController {
|
||||
.open_path("memory.current", false)
|
||||
.and_then(read_u64_from)
|
||||
.unwrap_or(0),
|
||||
max_usage_in_bytes: 0,
|
||||
max_usage_in_bytes: self
|
||||
.open_path("memory.peak", false)
|
||||
.and_then(read_u64_from)
|
||||
.unwrap_or(0),
|
||||
move_charge_at_immigrate: 0,
|
||||
numa_stat: NumaStat::default(),
|
||||
oom_control: OomControl::default(),
|
||||
@@ -727,7 +745,7 @@ impl MemController {
|
||||
.open_path("memory.swap.events", false)
|
||||
.and_then(flat_keyed_to_hashmap)
|
||||
.map(|x| *x.get("fail").unwrap_or(&0) as u64)
|
||||
.unwrap(),
|
||||
.unwrap_or(0),
|
||||
limit_in_bytes: self
|
||||
.open_path("memory.swap.max", false)
|
||||
.and_then(read_i64_from)
|
||||
@@ -736,7 +754,10 @@ impl MemController {
|
||||
.open_path("memory.swap.current", false)
|
||||
.and_then(read_u64_from)
|
||||
.unwrap_or(0),
|
||||
max_usage_in_bytes: 0,
|
||||
max_usage_in_bytes: self
|
||||
.open_path("memory.swap.peak", false)
|
||||
.and_then(read_u64_from)
|
||||
.unwrap_or(0),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -844,13 +865,16 @@ impl MemController {
|
||||
/// Set the memory usage limit of the control group, in bytes.
|
||||
pub fn set_limit(&self, limit: i64) -> Result<()> {
|
||||
let mut file_name = "memory.limit_in_bytes";
|
||||
let mut limit_str = limit.to_string();
|
||||
if self.v2 {
|
||||
file_name = "memory.max";
|
||||
if limit == -1 {
|
||||
limit_str = "max".to_string();
|
||||
}
|
||||
}
|
||||
self.open_path(file_name, true).and_then(|mut file| {
|
||||
file.write_all(limit.to_string().as_ref()).map_err(|e| {
|
||||
Error::with_cause(WriteFailed(file_name.to_string(), limit.to_string()), e)
|
||||
})
|
||||
file.write_all(limit_str.as_ref())
|
||||
.map_err(|e| Error::with_cause(WriteFailed(file_name.to_string(), limit_str), e))
|
||||
})
|
||||
}
|
||||
|
||||
@@ -881,13 +905,16 @@ impl MemController {
|
||||
/// Set the memory+swap limit of the control group, in bytes.
|
||||
pub fn set_memswap_limit(&self, limit: i64) -> Result<()> {
|
||||
let mut file_name = "memory.memsw.limit_in_bytes";
|
||||
let mut limit_str = limit.to_string();
|
||||
if self.v2 {
|
||||
file_name = "memory.swap.max";
|
||||
if limit == -1 {
|
||||
limit_str = "max".to_string();
|
||||
}
|
||||
}
|
||||
self.open_path(file_name, true).and_then(|mut file| {
|
||||
file.write_all(limit.to_string().as_ref()).map_err(|e| {
|
||||
Error::with_cause(WriteFailed(file_name.to_string(), limit.to_string()), e)
|
||||
})
|
||||
file.write_all(limit_str.as_ref())
|
||||
.map_err(|e| Error::with_cause(WriteFailed(file_name.to_string(), limit_str), e))
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -76,10 +76,10 @@ impl<'a> From<&'a Subsystem> for &'a NetClsController {
|
||||
|
||||
impl NetClsController {
|
||||
/// Constructs a new `NetClsController` with `root` serving as the root of the control group.
|
||||
pub fn new(root: PathBuf) -> Self {
|
||||
pub fn new(point: PathBuf, root: PathBuf) -> Self {
|
||||
Self {
|
||||
base: root.clone(),
|
||||
path: root,
|
||||
base: root,
|
||||
path: point,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -79,10 +79,10 @@ impl<'a> From<&'a Subsystem> for &'a NetPrioController {
|
||||
|
||||
impl NetPrioController {
|
||||
/// Constructs a new `NetPrioController` with `root` serving as the root of the control group.
|
||||
pub fn new(root: PathBuf) -> Self {
|
||||
pub fn new(point: PathBuf, root: PathBuf) -> Self {
|
||||
Self {
|
||||
base: root.clone(),
|
||||
path: root,
|
||||
base: root,
|
||||
path: point,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -65,10 +65,10 @@ impl<'a> From<&'a Subsystem> for &'a PerfEventController {
|
||||
|
||||
impl PerfEventController {
|
||||
/// Constructs a new `PerfEventController` with `root` serving as the root of the control group.
|
||||
pub fn new(root: PathBuf) -> Self {
|
||||
pub fn new(point: PathBuf, root: PathBuf) -> Self {
|
||||
Self {
|
||||
base: root.clone(),
|
||||
path: root,
|
||||
base: root,
|
||||
path: point,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,10 +92,10 @@ impl<'a> From<&'a Subsystem> for &'a PidController {
|
||||
impl PidController {
|
||||
/// Constructors a new `PidController` instance, with `root` serving as the controller's root
|
||||
/// directory.
|
||||
pub fn new(root: PathBuf, v2: bool) -> Self {
|
||||
pub fn new(point: PathBuf, root: PathBuf, v2: bool) -> Self {
|
||||
Self {
|
||||
base: root.clone(),
|
||||
path: root,
|
||||
base: root,
|
||||
path: point,
|
||||
v2,
|
||||
}
|
||||
}
|
||||
|
||||
11
src/rdma.rs
11
src/rdma.rs
@@ -68,10 +68,10 @@ impl<'a> From<&'a Subsystem> for &'a RdmaController {
|
||||
|
||||
impl RdmaController {
|
||||
/// Constructs a new `RdmaController` with `root` serving as the root of the control group.
|
||||
pub fn new(root: PathBuf) -> Self {
|
||||
pub fn new(point: PathBuf, root: PathBuf) -> Self {
|
||||
Self {
|
||||
base: root.clone(),
|
||||
path: root,
|
||||
base: root,
|
||||
path: point,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,6 +81,11 @@ impl RdmaController {
|
||||
.and_then(read_string_from)
|
||||
}
|
||||
|
||||
/// Returns the max usage of RDMA/IB specific resources.
|
||||
pub fn max(&self) -> Result<String> {
|
||||
self.open_path("rdma.max", false).and_then(read_string_from)
|
||||
}
|
||||
|
||||
/// Set a maximum usage for each RDMA/IB resource.
|
||||
pub fn set_max(&self, max: &str) -> Result<()> {
|
||||
self.open_path("rdma.max", true).and_then(|mut file| {
|
||||
|
||||
@@ -62,10 +62,10 @@ impl<'a> From<&'a Subsystem> for &'a SystemdController {
|
||||
|
||||
impl SystemdController {
|
||||
/// Constructs a new `SystemdController` with `root` serving as the root of the control group.
|
||||
pub fn new(root: PathBuf, v2: bool) -> Self {
|
||||
pub fn new(point: PathBuf, root: PathBuf, v2: bool) -> Self {
|
||||
Self {
|
||||
base: root.clone(),
|
||||
path: root,
|
||||
base: root,
|
||||
path: point,
|
||||
_v2: v2,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,8 @@ pub fn test_cpu_res_build() {
|
||||
.cpu()
|
||||
.shares(85)
|
||||
.done()
|
||||
.build(h);
|
||||
.build(h)
|
||||
.unwrap();
|
||||
|
||||
{
|
||||
let cpu: &CpuController = cg.controller_of().unwrap();
|
||||
@@ -42,7 +43,8 @@ pub fn test_memory_res_build() {
|
||||
.swappiness(70)
|
||||
.memory_hard_limit(1024 * 1024 * 1024)
|
||||
.done()
|
||||
.build(h);
|
||||
.build(h)
|
||||
.unwrap();
|
||||
|
||||
{
|
||||
let c: &MemController = cg.controller_of().unwrap();
|
||||
@@ -64,7 +66,8 @@ pub fn test_pid_res_build() {
|
||||
.pid()
|
||||
.maximum_number_of_processes(MaxValue::Value(123))
|
||||
.done()
|
||||
.build(h);
|
||||
.build(h)
|
||||
.unwrap();
|
||||
|
||||
{
|
||||
let c: &PidController = cg.controller_of().unwrap();
|
||||
@@ -83,7 +86,8 @@ pub fn test_devices_res_build() {
|
||||
.devices()
|
||||
.device(1, 6, DeviceType::Char, true, vec![DevicePermissions::Read])
|
||||
.done()
|
||||
.build(h);
|
||||
.build(h)
|
||||
.unwrap();
|
||||
|
||||
{
|
||||
let c: &DevicesController = cg.controller_of().unwrap();
|
||||
@@ -113,7 +117,8 @@ pub fn test_network_res_build() {
|
||||
.network()
|
||||
.class_id(1337)
|
||||
.done()
|
||||
.build(h);
|
||||
.build(h)
|
||||
.unwrap();
|
||||
|
||||
{
|
||||
let c: &NetClsController = cg.controller_of().unwrap();
|
||||
@@ -134,7 +139,8 @@ pub fn test_hugepages_res_build() {
|
||||
.hugepages()
|
||||
.limit("2MB".to_string(), 4 * 2 * 1024 * 1024)
|
||||
.done()
|
||||
.build(h);
|
||||
.build(h)
|
||||
.unwrap();
|
||||
|
||||
{
|
||||
let c: &HugeTlbController = cg.controller_of().unwrap();
|
||||
@@ -152,7 +158,8 @@ pub fn test_blkio_res_build() {
|
||||
.blkio()
|
||||
.weight(100)
|
||||
.done()
|
||||
.build(h);
|
||||
.build(h)
|
||||
.unwrap();
|
||||
|
||||
{
|
||||
let c: &BlkIoController = cg.controller_of().unwrap();
|
||||
|
||||
184
tests/cgroup.rs
184
tests/cgroup.rs
@@ -5,29 +5,60 @@
|
||||
//
|
||||
|
||||
//! Simple unit tests about the control groups system.
|
||||
use cgroups_rs::cgroup::{
|
||||
CGROUP_MODE_DOMAIN, CGROUP_MODE_DOMAIN_INVALID, CGROUP_MODE_DOMAIN_THREADED,
|
||||
CGROUP_MODE_THREADED,
|
||||
};
|
||||
use cgroups_rs::memory::MemController;
|
||||
use cgroups_rs::Controller;
|
||||
use cgroups_rs::{Cgroup, CgroupPid, Subsystem};
|
||||
use std::process::Command;
|
||||
use std::thread::sleep;
|
||||
use std::time::Duration;
|
||||
|
||||
#[test]
|
||||
fn test_tasks_iterator() {
|
||||
fn test_procs_iterator_cgroup() {
|
||||
let h = cgroups_rs::hierarchies::auto();
|
||||
let pid = libc::pid_t::from(nix::unistd::getpid()) as u64;
|
||||
let cg = Cgroup::new(h, String::from("test_tasks_iterator"));
|
||||
let cg = Cgroup::new(h, String::from("test_procs_iterator_cgroup")).unwrap();
|
||||
{
|
||||
// Add a task to the control group.
|
||||
cg.add_task_by_tgid(CgroupPid::from(pid)).unwrap();
|
||||
|
||||
let mut procs = cg.procs().into_iter();
|
||||
// Verify that the task is indeed in the xcontrol group
|
||||
assert_eq!(procs.next(), Some(CgroupPid::from(pid)));
|
||||
assert_eq!(procs.next(), None);
|
||||
|
||||
// Now, try removing it.
|
||||
cg.remove_task_by_tgid(CgroupPid::from(pid)).unwrap();
|
||||
procs = cg.procs().into_iter();
|
||||
|
||||
// Verify that it was indeed removed.
|
||||
assert_eq!(procs.next(), None);
|
||||
}
|
||||
cg.delete().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tasks_iterator_cgroup_v1() {
|
||||
if cgroups_rs::hierarchies::is_cgroup2_unified_mode() {
|
||||
return;
|
||||
}
|
||||
let h = cgroups_rs::hierarchies::auto();
|
||||
let pid = libc::pid_t::from(nix::unistd::getpid()) as u64;
|
||||
let cg = Cgroup::new(h, String::from("test_tasks_iterator_cgroup_v1")).unwrap();
|
||||
{
|
||||
// Add a task to the control group.
|
||||
cg.add_task(CgroupPid::from(pid)).unwrap();
|
||||
|
||||
use std::{thread, time};
|
||||
thread::sleep(time::Duration::from_millis(100));
|
||||
|
||||
let mut tasks = cg.tasks().into_iter();
|
||||
// Verify that the task is indeed in the control group
|
||||
// Verify that the task is indeed in the xcontrol group
|
||||
assert_eq!(tasks.next(), Some(CgroupPid::from(pid)));
|
||||
assert_eq!(tasks.next(), None);
|
||||
|
||||
// Now, try removing it.
|
||||
cg.remove_task(CgroupPid::from(pid));
|
||||
cg.remove_task(CgroupPid::from(pid)).unwrap();
|
||||
tasks = cg.tasks().into_iter();
|
||||
|
||||
// Verify that it was indeed removed.
|
||||
@@ -36,6 +67,143 @@ fn test_tasks_iterator() {
|
||||
cg.delete().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tasks_iterator_cgroup_threaded_mode() {
|
||||
if !cgroups_rs::hierarchies::is_cgroup2_unified_mode() {
|
||||
return;
|
||||
}
|
||||
let pid = libc::pid_t::from(nix::unistd::getpid()) as u64;
|
||||
let cg = Cgroup::new(
|
||||
cgroups_rs::hierarchies::auto(),
|
||||
String::from("test_tasks_iterator_cgroup_threaded_mode"),
|
||||
)
|
||||
.unwrap();
|
||||
let cg_threaded_sub1 = Cgroup::new_with_specified_controllers(
|
||||
cgroups_rs::hierarchies::auto(),
|
||||
String::from("test_tasks_iterator_cgroup_threaded_mode/threaded_sub1"),
|
||||
Some(vec![String::from("cpuset"), String::from("cpu")]),
|
||||
)
|
||||
.unwrap();
|
||||
let cg_threaded_sub2 = Cgroup::new_with_specified_controllers(
|
||||
cgroups_rs::hierarchies::auto(),
|
||||
String::from("test_tasks_iterator_cgroup_threaded_mode/threaded_sub2"),
|
||||
Some(vec![String::from("cpuset"), String::from("cpu")]),
|
||||
)
|
||||
.unwrap();
|
||||
{
|
||||
// Verify that cgroup type of the control group is domain mode.
|
||||
assert_eq!(cg.get_cgroup_type().unwrap(), CGROUP_MODE_DOMAIN);
|
||||
|
||||
// Set cgroup type of the sub-control group is thread mode.
|
||||
cg_threaded_sub1
|
||||
.set_cgroup_type(CGROUP_MODE_THREADED)
|
||||
.unwrap();
|
||||
// Verify that cgroup type of the sub-control group is thread mode.
|
||||
assert_eq!(
|
||||
cg_threaded_sub1.get_cgroup_type().unwrap(),
|
||||
CGROUP_MODE_THREADED
|
||||
);
|
||||
// Verify that the cgroup type of the sub-control group that does
|
||||
// not set the cgroup type is domain invalid mode.
|
||||
assert_eq!(
|
||||
cg_threaded_sub2.get_cgroup_type().unwrap(),
|
||||
CGROUP_MODE_DOMAIN_INVALID
|
||||
);
|
||||
// Verify whether the cgroup type of the parent control group of
|
||||
// the control group whose cgroup type is set to thread mode is
|
||||
// domain thread mode.
|
||||
assert_eq!(cg.get_cgroup_type().unwrap(), CGROUP_MODE_DOMAIN_THREADED);
|
||||
|
||||
// Set cgroup type of the sub-control group is thread mode.
|
||||
cg_threaded_sub2
|
||||
.set_cgroup_type(CGROUP_MODE_THREADED)
|
||||
.unwrap();
|
||||
// Verify that cgroup type of the sub-control group is thread mode.
|
||||
assert_eq!(
|
||||
cg_threaded_sub2.get_cgroup_type().unwrap(),
|
||||
CGROUP_MODE_THREADED
|
||||
);
|
||||
|
||||
// Add a proc to the control group.
|
||||
cg.add_task_by_tgid(CgroupPid::from(pid)).unwrap();
|
||||
|
||||
let mut procs = cg.procs().into_iter();
|
||||
// Verify that the task is indeed in the x control group
|
||||
assert_eq!(procs.next(), Some(CgroupPid::from(pid)));
|
||||
assert_eq!(procs.next(), None);
|
||||
|
||||
// Add a task to the sub control group.
|
||||
cg_threaded_sub1.add_task(CgroupPid::from(pid)).unwrap();
|
||||
|
||||
let mut tasks = cg_threaded_sub1.tasks().into_iter();
|
||||
// Verify that the task is indeed in the xcontrol group
|
||||
assert_eq!(tasks.next(), Some(CgroupPid::from(pid)));
|
||||
assert_eq!(tasks.next(), None);
|
||||
|
||||
// Now, try move it to parent.
|
||||
cg_threaded_sub1
|
||||
.move_task_to_parent(CgroupPid::from(pid))
|
||||
.unwrap();
|
||||
tasks = cg_threaded_sub1.tasks().into_iter();
|
||||
|
||||
// Verify that it was indeed removed.
|
||||
assert_eq!(tasks.next(), None);
|
||||
|
||||
// Now, try removing it.
|
||||
cg.remove_task_by_tgid(CgroupPid::from(pid)).unwrap();
|
||||
procs = cg.procs().into_iter();
|
||||
|
||||
// Verify that it was indeed removed.
|
||||
assert_eq!(procs.next(), None);
|
||||
}
|
||||
cg_threaded_sub1.delete().unwrap();
|
||||
cg_threaded_sub2.delete().unwrap();
|
||||
cg.delete().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_kill_cgroup() {
|
||||
if !cgroups_rs::hierarchies::is_cgroup2_unified_mode() {
|
||||
return;
|
||||
}
|
||||
let h = cgroups_rs::hierarchies::auto();
|
||||
let cg = Cgroup::new(h, String::from("test_kill_cgroup")).unwrap();
|
||||
{
|
||||
// Spawn a proc, don't want to getpid(2) here.
|
||||
let mut child = Command::new("sleep").arg("infinity").spawn().unwrap();
|
||||
cg.add_task_by_tgid(CgroupPid::from(child.id() as u64))
|
||||
.unwrap();
|
||||
|
||||
let cg_procs = cg.procs();
|
||||
assert_eq!(cg_procs.len(), 1_usize);
|
||||
|
||||
// Now kill and wait on the proc.
|
||||
cg.kill().unwrap();
|
||||
|
||||
let mut tries = 0;
|
||||
let status: Option<std::process::ExitStatus> = loop {
|
||||
match child.try_wait() {
|
||||
Ok(Some(status)) => {
|
||||
break Some(status);
|
||||
}
|
||||
Ok(None) => {
|
||||
if tries > 3 {
|
||||
break None;
|
||||
}
|
||||
sleep(Duration::from_millis(100));
|
||||
tries += 1;
|
||||
}
|
||||
Err(e) => {
|
||||
child.kill().unwrap();
|
||||
panic!("error attempting to wait: {}", e);
|
||||
}
|
||||
}
|
||||
};
|
||||
assert!(status.is_some());
|
||||
}
|
||||
cg.delete().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cgroup_with_relative_paths() {
|
||||
if cgroups_rs::hierarchies::is_cgroup2_unified_mode() {
|
||||
@@ -83,7 +251,7 @@ fn test_cgroup_v2() {
|
||||
return;
|
||||
}
|
||||
let h = cgroups_rs::hierarchies::auto();
|
||||
let cg = Cgroup::new(h, String::from("test_v2"));
|
||||
let cg = Cgroup::new(h, String::from("test_v2")).unwrap();
|
||||
|
||||
let mem_controller: &MemController = cg.controller_of().unwrap();
|
||||
let (mem, swp, rev) = (4 * 1024 * 1000, 2 * 1024 * 1000, 1024 * 1000);
|
||||
|
||||
@@ -10,7 +10,7 @@ use cgroups_rs::Cgroup;
|
||||
#[test]
|
||||
fn test_cfs_quota_and_periods() {
|
||||
let h = cgroups_rs::hierarchies::auto();
|
||||
let cg = Cgroup::new(h, String::from("test_cfs_quota_and_periods"));
|
||||
let cg = Cgroup::new(h, String::from("test_cfs_quota_and_periods")).unwrap();
|
||||
|
||||
let cpu_controller: &CpuController = cg.controller_of().unwrap();
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ use std::fs;
|
||||
#[test]
|
||||
fn test_cpuset_memory_pressure_root_cg() {
|
||||
let h = cgroups_rs::hierarchies::auto();
|
||||
let cg = Cgroup::new(h, String::from("test_cpuset_memory_pressure_root_cg"));
|
||||
let cg = Cgroup::new(h, String::from("test_cpuset_memory_pressure_root_cg")).unwrap();
|
||||
{
|
||||
let cpuset: &CpuSetController = cg.controller_of().unwrap();
|
||||
|
||||
@@ -27,7 +27,7 @@ fn test_cpuset_memory_pressure_root_cg() {
|
||||
#[test]
|
||||
fn test_cpuset_set_cpus() {
|
||||
let h = cgroups_rs::hierarchies::auto();
|
||||
let cg = Cgroup::new(h, String::from("test_cpuset_set_cpus"));
|
||||
let cg = Cgroup::new(h, String::from("test_cpuset_set_cpus")).unwrap();
|
||||
{
|
||||
let cpuset: &CpuSetController = cg.controller_of().unwrap();
|
||||
|
||||
@@ -64,7 +64,7 @@ fn test_cpuset_set_cpus() {
|
||||
#[test]
|
||||
fn test_cpuset_set_cpus_add_task() {
|
||||
let h = cgroups_rs::hierarchies::auto();
|
||||
let cg = Cgroup::new(h, String::from("test_cpuset_set_cpus_add_task/sub-dir"));
|
||||
let cg = Cgroup::new(h, String::from("test_cpuset_set_cpus_add_task/sub-dir")).unwrap();
|
||||
|
||||
let cpuset: &CpuSetController = cg.controller_of().unwrap();
|
||||
let set = cpuset.cpuset();
|
||||
@@ -77,13 +77,13 @@ fn test_cpuset_set_cpus_add_task() {
|
||||
|
||||
// Add a task to the control group.
|
||||
let pid_i = libc::pid_t::from(nix::unistd::getpid()) as u64;
|
||||
let _ = cg.add_task(CgroupPid::from(pid_i));
|
||||
let _ = cg.add_task_by_tgid(CgroupPid::from(pid_i));
|
||||
let tasks = cg.tasks();
|
||||
assert!(!tasks.is_empty());
|
||||
println!("tasks after added: {:?}", tasks);
|
||||
|
||||
// remove task
|
||||
cg.remove_task(CgroupPid::from(pid_i));
|
||||
cg.remove_task_by_tgid(CgroupPid::from(pid_i)).unwrap();
|
||||
let tasks = cg.tasks();
|
||||
println!("tasks after deleted: {:?}", tasks);
|
||||
assert_eq!(0, tasks.len());
|
||||
|
||||
@@ -17,7 +17,7 @@ fn test_devices_parsing() {
|
||||
}
|
||||
|
||||
let h = cgroups_rs::hierarchies::auto();
|
||||
let cg = Cgroup::new(h, String::from("test_devices_parsing"));
|
||||
let cg = Cgroup::new(h, String::from("test_devices_parsing")).unwrap();
|
||||
{
|
||||
let devices: &DevicesController = cg.controller_of().unwrap();
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ fn test_hugetlb_sizes() {
|
||||
}
|
||||
|
||||
let h = cgroups_rs::hierarchies::auto();
|
||||
let cg = Cgroup::new(h, String::from("test_hugetlb_sizes"));
|
||||
let cg = Cgroup::new(h, String::from("test_hugetlb_sizes")).unwrap();
|
||||
{
|
||||
let hugetlb_controller: &HugeTlbController = cg.controller_of().unwrap();
|
||||
let _ = hugetlb_controller.get_sizes();
|
||||
|
||||
@@ -11,7 +11,7 @@ use cgroups_rs::{Cgroup, MaxValue};
|
||||
#[test]
|
||||
fn test_disable_oom_killer() {
|
||||
let h = cgroups_rs::hierarchies::auto();
|
||||
let cg = Cgroup::new(h, String::from("test_disable_oom_killer"));
|
||||
let cg = Cgroup::new(h, String::from("test_disable_oom_killer")).unwrap();
|
||||
{
|
||||
let mem_controller: &MemController = cg.controller_of().unwrap();
|
||||
|
||||
@@ -40,7 +40,7 @@ fn set_kmem_limit_v1() {
|
||||
return;
|
||||
}
|
||||
|
||||
let cg = Cgroup::new(h, String::from("set_kmem_limit_v1"));
|
||||
let cg = Cgroup::new(h, String::from("set_kmem_limit_v1")).unwrap();
|
||||
{
|
||||
let mem_controller: &MemController = cg.controller_of().unwrap();
|
||||
mem_controller.set_kmem_limit(1).unwrap();
|
||||
@@ -55,7 +55,7 @@ fn set_mem_v2() {
|
||||
return;
|
||||
}
|
||||
|
||||
let cg = Cgroup::new(h, String::from("set_mem_v2"));
|
||||
let cg = Cgroup::new(h, String::from("set_mem_v2")).unwrap();
|
||||
{
|
||||
let mem_controller: &MemController = cg.controller_of().unwrap();
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ use libc::pid_t;
|
||||
#[test]
|
||||
fn create_and_delete_cgroup() {
|
||||
let h = cgroups_rs::hierarchies::auto();
|
||||
let cg = Cgroup::new(h, String::from("create_and_delete_cgroup"));
|
||||
let cg = Cgroup::new(h, String::from("create_and_delete_cgroup")).unwrap();
|
||||
{
|
||||
let pidcontroller: &PidController = cg.controller_of().unwrap();
|
||||
pidcontroller.set_pid_max(MaxValue::Value(1337)).unwrap();
|
||||
@@ -31,7 +31,7 @@ fn create_and_delete_cgroup() {
|
||||
#[test]
|
||||
fn test_pids_current_is_zero() {
|
||||
let h = cgroups_rs::hierarchies::auto();
|
||||
let cg = Cgroup::new(h, String::from("test_pids_current_is_zero"));
|
||||
let cg = Cgroup::new(h, String::from("test_pids_current_is_zero")).unwrap();
|
||||
{
|
||||
let pidcontroller: &PidController = cg.controller_of().unwrap();
|
||||
let current = pidcontroller.get_pid_current();
|
||||
@@ -43,7 +43,7 @@ fn test_pids_current_is_zero() {
|
||||
#[test]
|
||||
fn test_pids_events_is_zero() {
|
||||
let h = cgroups_rs::hierarchies::auto();
|
||||
let cg = Cgroup::new(h, String::from("test_pids_events_is_zero"));
|
||||
let cg = Cgroup::new(h, String::from("test_pids_events_is_zero")).unwrap();
|
||||
{
|
||||
let pidcontroller: &PidController = cg.controller_of().unwrap();
|
||||
let events = pidcontroller.get_pid_events();
|
||||
@@ -56,7 +56,7 @@ fn test_pids_events_is_zero() {
|
||||
#[test]
|
||||
fn test_pid_events_is_not_zero() {
|
||||
let h = cgroups_rs::hierarchies::auto();
|
||||
let cg = Cgroup::new(h, String::from("test_pid_events_is_not_zero"));
|
||||
let cg = Cgroup::new(h, String::from("test_pid_events_is_not_zero")).unwrap();
|
||||
{
|
||||
let pids: &PidController = cg.controller_of().unwrap();
|
||||
let before = pids.get_pid_events();
|
||||
@@ -65,7 +65,7 @@ fn test_pid_events_is_not_zero() {
|
||||
match unsafe { fork() } {
|
||||
Ok(ForkResult::Parent { child, .. }) => {
|
||||
// move the process into the control group
|
||||
let _ = pids.add_task(&(pid_t::from(child) as u64).into());
|
||||
let _ = pids.add_task_by_tgid(&(pid_t::from(child) as u64).into());
|
||||
|
||||
println!("added task to cg: {:?}", child);
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ use cgroups_rs::{Cgroup, MaxValue, PidResources, Resources};
|
||||
#[test]
|
||||
fn pid_resources() {
|
||||
let h = cgroups_rs::hierarchies::auto();
|
||||
let cg = Cgroup::new(h, String::from("pid_resources"));
|
||||
let cg = Cgroup::new(h, String::from("pid_resources")).unwrap();
|
||||
{
|
||||
let res = Resources {
|
||||
pid: PidResources {
|
||||
|
||||
Reference in New Issue
Block a user