mirror of
https://github.com/kata-containers/cgroups-rs.git
synced 2026-08-05 02:13:23 +00:00
Compare commits
25 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
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 |
2
.github/workflows/bvt.yaml
vendored
2
.github/workflows/bvt.yaml
vendored
@@ -1,7 +1,7 @@
|
|||||||
name: BVT
|
name: BVT
|
||||||
on: [pull_request]
|
on: [pull_request]
|
||||||
env:
|
env:
|
||||||
RUST_VERSION: 1.52
|
RUST_VERSION: 1.69.0
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
name: Build
|
name: Build
|
||||||
|
|||||||
4
.gitignore
vendored
4
.gitignore
vendored
@@ -8,7 +8,3 @@ Cargo.lock
|
|||||||
|
|
||||||
# These are backup files generated by rustfmt
|
# These are backup files generated by rustfmt
|
||||||
**/*.rs.bk
|
**/*.rs.bk
|
||||||
|
|
||||||
/target
|
|
||||||
**/*.rs.bk
|
|
||||||
Cargo.lock
|
|
||||||
|
|||||||
@@ -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.1"
|
version = "0.3.4"
|
||||||
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"
|
||||||
|
|||||||
@@ -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.
|
||||||
///
|
///
|
||||||
///
|
///
|
||||||
@@ -68,8 +73,13 @@ impl Cgroup {
|
|||||||
self.hier.v2()
|
self.hier.v2()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Return the path the cgroup is located at.
|
||||||
|
pub fn path(&self) -> &str {
|
||||||
|
&self.path
|
||||||
|
}
|
||||||
|
|
||||||
/// Create this control group.
|
/// Create this control group.
|
||||||
fn create(&self) -> Result<()> {
|
pub fn create(&self) -> Result<()> {
|
||||||
if self.hier.v2() {
|
if self.hier.v2() {
|
||||||
create_v2_cgroup(self.hier.root(), &self.path, &self.specified_controllers)
|
create_v2_cgroup(self.hier.root(), &self.path, &self.specified_controllers)
|
||||||
} else {
|
} else {
|
||||||
@@ -346,7 +356,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 +384,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))
|
||||||
@@ -474,6 +497,13 @@ impl Cgroup {
|
|||||||
v.dedup();
|
v.dedup();
|
||||||
v
|
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";
|
pub const UNIFIED_MOUNTPOINT: &str = "/sys/fs/cgroup";
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ pub enum DeviceType {
|
|||||||
Block,
|
Block,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::derivable_impls)]
|
||||||
impl Default for DeviceType {
|
impl Default for DeviceType {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
DeviceType::All
|
DeviceType::All
|
||||||
@@ -170,9 +171,9 @@ impl ControllerInternal for DevicesController {
|
|||||||
|
|
||||||
for i in &res.devices {
|
for i in &res.devices {
|
||||||
if i.allow {
|
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 {
|
} else {
|
||||||
let _ = self.deny_device(i.devtype, i.major, i.minor, &i.access);
|
self.deny_device(i.devtype, i.major, i.minor, &i.access)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -238,7 +239,13 @@ impl DevicesController {
|
|||||||
let final_str = format!("{} {}:{} {}", devtype.to_char(), major, minor, perms);
|
let final_str = format!("{} {}:{} {}", devtype.to_char(), major, minor, perms);
|
||||||
self.open_path("devices.allow", true).and_then(|mut file| {
|
self.open_path("devices.allow", true).and_then(|mut file| {
|
||||||
file.write_all(final_str.as_ref()).map_err(|e| {
|
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);
|
let final_str = format!("{} {}:{} {}", devtype.to_char(), major, minor, perms);
|
||||||
self.open_path("devices.deny", true).and_then(|mut file| {
|
self.open_path("devices.deny", true).and_then(|mut file| {
|
||||||
file.write_all(final_str.as_ref()).map_err(|e| {
|
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,
|
||||||
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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,
|
||||||
|
|||||||
@@ -309,43 +309,16 @@ impl Default for V2 {
|
|||||||
|
|
||||||
pub const UNIFIED_MOUNTPOINT: &str = "/sys/fs/cgroup";
|
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 {
|
pub fn is_cgroup2_unified_mode() -> bool {
|
||||||
use nix::sys::statfs;
|
use nix::sys::statfs;
|
||||||
|
|
||||||
let path = std::path::Path::new(UNIFIED_MOUNTPOINT);
|
let path = std::path::Path::new(UNIFIED_MOUNTPOINT);
|
||||||
let fs_stat = statfs::statfs(path);
|
let fs_stat = match statfs::statfs(path) {
|
||||||
if fs_stat.is_err() {
|
Ok(fs_stat) => fs_stat,
|
||||||
return false;
|
Err(_) => return false,
|
||||||
}
|
};
|
||||||
|
|
||||||
// FIXME notwork, nix will not compile CGROUP2_SUPER_MAGIC because not(target_env = "musl")
|
fs_stat.filesystem_type() == statfs::CGROUP2_SUPER_MAGIC
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn auto() -> Box<dyn Hierarchy> {
|
pub fn auto() -> Box<dyn Hierarchy> {
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -879,6 +879,7 @@ pub enum MaxValue {
|
|||||||
Value(i64),
|
Value(i64),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::derivable_impls)]
|
||||||
impl Default for MaxValue {
|
impl Default for MaxValue {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
MaxValue::Max
|
MaxValue::Max
|
||||||
|
|||||||
@@ -593,7 +593,10 @@ impl MemController {
|
|||||||
.open_path("memory.current", false)
|
.open_path("memory.current", false)
|
||||||
.and_then(read_u64_from)
|
.and_then(read_u64_from)
|
||||||
.unwrap_or(0),
|
.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,
|
move_charge_at_immigrate: 0,
|
||||||
numa_stat: NumaStat::default(),
|
numa_stat: NumaStat::default(),
|
||||||
oom_control: OomControl::default(),
|
oom_control: OomControl::default(),
|
||||||
@@ -736,7 +739,10 @@ impl MemController {
|
|||||||
.open_path("memory.swap.current", false)
|
.open_path("memory.swap.current", false)
|
||||||
.and_then(read_u64_from)
|
.and_then(read_u64_from)
|
||||||
.unwrap_or(0),
|
.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 +850,16 @@ impl MemController {
|
|||||||
/// Set the memory usage limit of the control group, in bytes.
|
/// Set the memory usage limit of the control group, in bytes.
|
||||||
pub fn set_limit(&self, limit: i64) -> Result<()> {
|
pub fn set_limit(&self, limit: i64) -> Result<()> {
|
||||||
let mut file_name = "memory.limit_in_bytes";
|
let mut file_name = "memory.limit_in_bytes";
|
||||||
|
let mut limit_str = limit.to_string();
|
||||||
if self.v2 {
|
if self.v2 {
|
||||||
file_name = "memory.max";
|
file_name = "memory.max";
|
||||||
|
if limit == -1 {
|
||||||
|
limit_str = "max".to_string();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
self.open_path(file_name, true).and_then(|mut file| {
|
self.open_path(file_name, true).and_then(|mut file| {
|
||||||
file.write_all(limit.to_string().as_ref()).map_err(|e| {
|
file.write_all(limit_str.as_ref())
|
||||||
Error::with_cause(WriteFailed(file_name.to_string(), limit.to_string()), e)
|
.map_err(|e| Error::with_cause(WriteFailed(file_name.to_string(), limit_str), e))
|
||||||
})
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -881,13 +890,16 @@ impl MemController {
|
|||||||
/// Set the memory+swap limit of the control group, in bytes.
|
/// Set the memory+swap limit of the control group, in bytes.
|
||||||
pub fn set_memswap_limit(&self, limit: i64) -> Result<()> {
|
pub fn set_memswap_limit(&self, limit: i64) -> Result<()> {
|
||||||
let mut file_name = "memory.memsw.limit_in_bytes";
|
let mut file_name = "memory.memsw.limit_in_bytes";
|
||||||
|
let mut limit_str = limit.to_string();
|
||||||
if self.v2 {
|
if self.v2 {
|
||||||
file_name = "memory.swap.max";
|
file_name = "memory.swap.max";
|
||||||
|
if limit == -1 {
|
||||||
|
limit_str = "max".to_string();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
self.open_path(file_name, true).and_then(|mut file| {
|
self.open_path(file_name, true).and_then(|mut file| {
|
||||||
file.write_all(limit.to_string().as_ref()).map_err(|e| {
|
file.write_all(limit_str.as_ref())
|
||||||
Error::with_cause(WriteFailed(file_name.to_string(), limit.to_string()), e)
|
.map_err(|e| Error::with_cause(WriteFailed(file_name.to_string(), limit_str), e))
|
||||||
})
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
181
tests/cgroup.rs
181
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() {
|
||||||
@@ -74,7 +199,7 @@ fn test_kill_cgroup() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
assert!(!status.is_none());
|
assert!(status.is_some());
|
||||||
}
|
}
|
||||||
cg.delete().unwrap();
|
cg.delete().unwrap();
|
||||||
}
|
}
|
||||||
@@ -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