mod error;
use std::collections::HashMap;
pub use error::{Error, Result};
mod fs;
pub use fs::FsManager;
mod systemd;
pub use systemd::SystemdManager;
mod conv;
use oci_spec::runtime::LinuxResources;
use crate::systemd::SLICE_SUFFIX;
use crate::{CgroupPid, CgroupStats, FreezerState};
pub fn is_systemd_cgroup(cgroups_path: &str) -> bool {
let parts: Vec<&str> = cgroups_path.split(':').collect();
parts.len() == 3 && parts[0].ends_with(SLICE_SUFFIX)
}
pub trait Manager: Send + Sync {
fn add_proc(&mut self, tgid: CgroupPid) -> Result<()>;
fn add_thread(&mut self, pid: CgroupPid) -> Result<()>;
fn pids(&self) -> Result<Vec<CgroupPid>>;
fn freeze(&self, state: FreezerState) -> Result<()>;
fn destroy(&mut self) -> Result<()>;
fn set(&mut self, resources: &LinuxResources) -> Result<()>;
fn cgroup_path(&self, subsystem: Option<&str>) -> Result<String>;
fn enable_cpus_topdown(&self, cpus: &str) -> Result<()>;
fn stats(&self) -> CgroupStats;
fn paths(&self) -> &HashMap<String, String>;
fn mounts(&self) -> &HashMap<String, String>;
fn systemd(&self) -> bool;
fn v2(&self) -> bool;
}
#[cfg(test)]
mod tests {
pub const MEMORY_512M: i64 = 512 * 1024 * 1024; pub const MEMORY_1G: i64 = 1024 * 1024 * 1024; pub const MEMORY_2G: i64 = 2 * 1024 * 1024 * 1024;
#[macro_export]
macro_rules! skip_if_cgroups_v1 {
() => {
if !$crate::fs::hierarchies::is_cgroup2_unified_mode() {
eprintln!("Skipping test in cgroups v1 mode");
return;
}
};
}
#[macro_export]
macro_rules! skip_if_cgroups_v2 {
() => {
if $crate::fs::hierarchies::is_cgroup2_unified_mode() {
eprintln!("Skipping test in cgroups v2 mode");
return;
}
};
}
}