mirror of
https://github.com/kata-containers/cgroups-rs.git
synced 2026-08-05 02:13:23 +00:00
Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1b61c07b69 | ||
|
|
45e1f0c274 | ||
|
|
41b5f9c25c | ||
|
|
93a59571e3 | ||
|
|
257012f2bb | ||
|
|
3dd0735324 | ||
|
|
6b338cf997 | ||
|
|
225be2cdbb | ||
|
|
51779d6915 | ||
|
|
5ea28f076c | ||
|
|
aa74f34a91 | ||
|
|
328428ace4 | ||
|
|
c8bb7e1c7e | ||
|
|
25a1340123 | ||
|
|
4203075f19 |
@@ -5,7 +5,7 @@ repository = "https://github.com/kata-containers/cgroups-rs"
|
|||||||
keywords = ["linux", "cgroup", "containers", "isolation"]
|
keywords = ["linux", "cgroup", "containers", "isolation"]
|
||||||
categories = ["os", "api-bindings", "os::unix-apis"]
|
categories = ["os", "api-bindings", "os::unix-apis"]
|
||||||
license = "MIT OR Apache-2.0"
|
license = "MIT OR Apache-2.0"
|
||||||
version = "0.3.0"
|
version = "0.3.2"
|
||||||
authors = ["The Kata Containers community <kata-dev@lists.katacontainers.io>", "Levente Kurusa <lkurusa@acm.org>", "Sam Wilson <tecywiz121@hotmail.com>"]
|
authors = ["The Kata Containers community <kata-dev@lists.katacontainers.io>", "Levente Kurusa <lkurusa@acm.org>", "Sam Wilson <tecywiz121@hotmail.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
homepage = "https://github.com/kata-containers/cgroups-rs"
|
homepage = "https://github.com/kata-containers/cgroups-rs"
|
||||||
|
|||||||
136
src/blkio.rs
136
src/blkio.rs
@@ -43,7 +43,7 @@ pub struct BlkIoData {
|
|||||||
pub data: u64,
|
pub data: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Eq, PartialEq, Debug)]
|
#[derive(Eq, PartialEq, Debug, Default)]
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
/// Per-device activity from the control group.
|
/// Per-device activity from the control group.
|
||||||
pub struct IoService {
|
pub struct IoService {
|
||||||
@@ -59,6 +59,8 @@ pub struct IoService {
|
|||||||
pub sync: u64,
|
pub sync: u64,
|
||||||
/// How many items were asynchronously transferred.
|
/// How many items were asynchronously transferred.
|
||||||
pub r#async: u64,
|
pub r#async: u64,
|
||||||
|
/// How many items were discarded.
|
||||||
|
pub discard: u64,
|
||||||
/// Total number of items transferred.
|
/// Total number of items transferred.
|
||||||
pub total: u64,
|
pub total: u64,
|
||||||
}
|
}
|
||||||
@@ -87,44 +89,62 @@ pub struct IoStat {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn parse_io_service(s: String) -> Result<Vec<IoService>> {
|
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)
|
.filter(|x| x.split_whitespace().count() == 3)
|
||||||
.map(|x| {
|
.map(|x| {
|
||||||
let mut spl = x.split_whitespace();
|
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)| {
|
.map(|(a, b, c)| {
|
||||||
let mut spl = a.split(':');
|
let mut spl = a.split(':');
|
||||||
(spl.next().unwrap(), spl.next().unwrap(), b, c)
|
(
|
||||||
})
|
spl.next().unwrap().parse::<i16>(),
|
||||||
.collect::<Vec<_>>()
|
spl.next().unwrap().parse::<i16>(),
|
||||||
.chunks(5)
|
b,
|
||||||
.map(|x| {
|
c,
|
||||||
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)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
.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 {
|
fn get_value(s: &str) -> String {
|
||||||
@@ -817,6 +837,7 @@ mod test {
|
|||||||
8:32 Write 0
|
8:32 Write 0
|
||||||
8:32 Sync 4280320
|
8:32 Sync 4280320
|
||||||
8:32 Async 0
|
8:32 Async 0
|
||||||
|
8:32 Discard 1
|
||||||
8:32 Total 4280320
|
8:32 Total 4280320
|
||||||
8:48 Read 5705479168
|
8:48 Read 5705479168
|
||||||
8:48 Write 56096055296
|
8:48 Write 56096055296
|
||||||
@@ -833,28 +854,6 @@ mod test {
|
|||||||
8:0 Sync 7192576
|
8:0 Sync 7192576
|
||||||
8:0 Async 0
|
8:0 Async 0
|
||||||
8:0 Total 7192576
|
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
|
Total 61823067136
|
||||||
";
|
";
|
||||||
|
|
||||||
@@ -884,6 +883,7 @@ Total 61823067136
|
|||||||
write: 0,
|
write: 0,
|
||||||
sync: 4280320,
|
sync: 4280320,
|
||||||
r#async: 0,
|
r#async: 0,
|
||||||
|
discard: 1,
|
||||||
total: 4280320,
|
total: 4280320,
|
||||||
},
|
},
|
||||||
IoService {
|
IoService {
|
||||||
@@ -893,6 +893,7 @@ Total 61823067136
|
|||||||
write: 56096055296,
|
write: 56096055296,
|
||||||
sync: 11213923328,
|
sync: 11213923328,
|
||||||
r#async: 50587611136,
|
r#async: 50587611136,
|
||||||
|
discard: 0,
|
||||||
total: 61801534464,
|
total: 61801534464,
|
||||||
},
|
},
|
||||||
IoService {
|
IoService {
|
||||||
@@ -902,6 +903,7 @@ Total 61823067136
|
|||||||
write: 0,
|
write: 0,
|
||||||
sync: 10059776,
|
sync: 10059776,
|
||||||
r#async: 0,
|
r#async: 0,
|
||||||
|
discard: 0,
|
||||||
total: 10059776,
|
total: 10059776,
|
||||||
},
|
},
|
||||||
IoService {
|
IoService {
|
||||||
@@ -911,12 +913,34 @@ Total 61823067136
|
|||||||
write: 0,
|
write: 0,
|
||||||
sync: 7192576,
|
sync: 7192576,
|
||||||
r#async: 0,
|
r#async: 0,
|
||||||
|
discard: 0,
|
||||||
total: 7192576,
|
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]
|
#[test]
|
||||||
|
|||||||
@@ -16,6 +16,11 @@ use std::convert::From;
|
|||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::{Path, PathBuf};
|
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.
|
/// A control group is the central structure to this crate.
|
||||||
///
|
///
|
||||||
///
|
///
|
||||||
@@ -346,7 +351,18 @@ impl Cgroup {
|
|||||||
let subsystems = self.subsystems();
|
let subsystems = self.subsystems();
|
||||||
if !subsystems.is_empty() {
|
if !subsystems.is_empty() {
|
||||||
let c = subsystems[0].to_controller();
|
let c = subsystems[0].to_controller();
|
||||||
c.add_task(&tid)
|
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 {
|
} else {
|
||||||
Err(Error::new(SubsystemsEmpty))
|
Err(Error::new(SubsystemsEmpty))
|
||||||
}
|
}
|
||||||
@@ -363,6 +379,8 @@ impl Cgroup {
|
|||||||
let subsystems = self.subsystems();
|
let subsystems = self.subsystems();
|
||||||
if !subsystems.is_empty() {
|
if !subsystems.is_empty() {
|
||||||
let c = subsystems[0].to_controller();
|
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)
|
c.add_task_by_tgid(&tgid)
|
||||||
} else {
|
} else {
|
||||||
Err(Error::new(SubsystemsEmpty))
|
Err(Error::new(SubsystemsEmpty))
|
||||||
|
|||||||
@@ -59,6 +59,10 @@ pub enum ErrorKind {
|
|||||||
#[error("using method in wrong cgroup version")]
|
#[error("using method in wrong cgroup version")]
|
||||||
CgroupVersion,
|
CgroupVersion,
|
||||||
|
|
||||||
|
/// Using method in wrong cgroup mode.
|
||||||
|
#[error("using method in wrong cgroup mode.")]
|
||||||
|
CgroupMode,
|
||||||
|
|
||||||
/// Subsystems is empty.
|
/// Subsystems is empty.
|
||||||
#[error("subsystems is empty")]
|
#[error("subsystems is empty")]
|
||||||
SubsystemsEmpty,
|
SubsystemsEmpty,
|
||||||
@@ -85,7 +89,7 @@ impl fmt::Display for Error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl StdError for Error {
|
impl StdError for Error {
|
||||||
fn cause(&self) -> Option<&dyn StdError> {
|
fn source(&self) -> Option<&(dyn StdError + 'static)> {
|
||||||
#[allow(clippy::manual_map)]
|
#[allow(clippy::manual_map)]
|
||||||
match self.cause {
|
match self.cause {
|
||||||
Some(ref x) => Some(&**x),
|
Some(ref x) => Some(&**x),
|
||||||
|
|||||||
@@ -138,8 +138,11 @@ impl HugeTlbController {
|
|||||||
/// Get the limit (in bytes) of how much memory can be backed by hugepages of a certain size
|
/// Get the limit (in bytes) of how much memory can be backed by hugepages of a certain size
|
||||||
/// (`hugetlb_size`).
|
/// (`hugetlb_size`).
|
||||||
pub fn limit_in_bytes(&self, hugetlb_size: &str) -> Result<u64> {
|
pub fn limit_in_bytes(&self, hugetlb_size: &str) -> Result<u64> {
|
||||||
self.open_path(&format!("hugetlb.{}.limit_in_bytes", hugetlb_size), false)
|
let mut file_name = format!("hugetlb.{}.limit_in_bytes", hugetlb_size);
|
||||||
.and_then(read_u64_from)
|
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
|
/// Get the current usage of memory that is backed by hugepages of a certain size
|
||||||
|
|||||||
@@ -604,9 +604,9 @@ pub struct CpuResources {
|
|||||||
/// Weight of how much of the total CPU time should this control group get. Note that this is
|
/// 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.
|
/// hierarchical, so this is weighted against the siblings of this control group.
|
||||||
pub shares: Option<u64>,
|
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>,
|
pub quota: Option<i64>,
|
||||||
/// Period of time in nanoseconds.
|
/// Period of time in microseconds.
|
||||||
pub period: Option<u64>,
|
pub period: Option<u64>,
|
||||||
/// This is currently a no-operation.
|
/// This is currently a no-operation.
|
||||||
pub realtime_runtime: Option<i64>,
|
pub realtime_runtime: Option<i64>,
|
||||||
|
|||||||
179
tests/cgroup.rs
179
tests/cgroup.rs
@@ -5,6 +5,10 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
//! Simple unit tests about the control groups system.
|
//! 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::memory::MemController;
|
||||||
use cgroups_rs::Controller;
|
use cgroups_rs::Controller;
|
||||||
use cgroups_rs::{Cgroup, CgroupPid, Subsystem};
|
use cgroups_rs::{Cgroup, CgroupPid, Subsystem};
|
||||||
@@ -36,6 +40,127 @@ fn test_procs_iterator_cgroup() {
|
|||||||
cg.delete().unwrap();
|
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();
|
||||||
|
|
||||||
|
let mut tasks = cg.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 removing it.
|
||||||
|
cg.remove_task(CgroupPid::from(pid)).unwrap();
|
||||||
|
tasks = cg.tasks().into_iter();
|
||||||
|
|
||||||
|
// Verify that it was indeed removed.
|
||||||
|
assert_eq!(tasks.next(), None);
|
||||||
|
}
|
||||||
|
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]
|
#[test]
|
||||||
fn test_kill_cgroup() {
|
fn test_kill_cgroup() {
|
||||||
if !cgroups_rs::hierarchies::is_cgroup2_unified_mode() {
|
if !cgroups_rs::hierarchies::is_cgroup2_unified_mode() {
|
||||||
@@ -146,57 +271,3 @@ fn test_cgroup_v2() {
|
|||||||
|
|
||||||
cg.delete().unwrap();
|
cg.delete().unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_tasks_iterator_cgroup_threaded_mode() {
|
|
||||||
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_threaded_mode")).unwrap();
|
|
||||||
let h = cgroups_rs::hierarchies::auto();
|
|
||||||
let specified_controllers = vec![String::from("cpuset"), String::from("cpu")];
|
|
||||||
let cg_threaded = Cgroup::new_with_specified_controllers(
|
|
||||||
h,
|
|
||||||
String::from("test_tasks_iterator_cgroup_threaded_mode/threaded"),
|
|
||||||
Some(specified_controllers),
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
cg_threaded.set_cgroup_type("threaded").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);
|
|
||||||
|
|
||||||
// Add a task to the sub control group.
|
|
||||||
cg_threaded.add_task(CgroupPid::from(pid)).unwrap();
|
|
||||||
|
|
||||||
let mut tasks = cg_threaded.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
|
|
||||||
.move_task_to_parent(CgroupPid::from(pid))
|
|
||||||
.unwrap();
|
|
||||||
tasks = cg_threaded.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.delete().unwrap();
|
|
||||||
cg.delete().unwrap();
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user