use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use crate::error::IronError;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum CgroupVersion {
V1,
V2,
Unknown,
}
impl std::fmt::Display for CgroupVersion {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::V1 => write!(f, "v1"),
Self::V2 => write!(f, "v2"),
Self::Unknown => write!(f, "unknown"),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CgroupCpu {
pub quota_us: i64,
pub period_us: u64,
pub shares: u64,
pub weight: u64,
pub usage_us: u64,
pub per_cpu_usage_us: Vec<u64>,
pub throttled_periods: u64,
pub throttled_time_us: u64,
pub effective_cpus: f64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CgroupMemory {
pub usage_bytes: u64,
pub limit_bytes: u64,
pub swap_usage_bytes: u64,
pub swap_limit_bytes: u64,
pub cache_bytes: u64,
pub rss_bytes: u64,
pub oom_events: u64,
pub usage_pct: f64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CgroupIo {
pub read_bytes: u64,
pub write_bytes: u64,
pub read_ios: u64,
pub write_ios: u64,
pub weight: u64,
pub device_limits: HashMap<String, u64>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CgroupPids {
pub current: u64,
pub limit: u64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CgroupInfo {
pub path: String,
pub name: String,
pub is_container: bool,
pub container_runtime: Option<String>,
pub cpu: Option<CgroupCpu>,
pub memory: Option<CgroupMemory>,
pub io: Option<CgroupIo>,
pub pids: Option<CgroupPids>,
pub controllers: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CgroupOverview {
pub version: CgroupVersion,
pub available_controllers: Vec<String>,
pub cgroups: Vec<CgroupInfo>,
pub container_count: usize,
pub total_memory_limit_bytes: u64,
pub total_effective_cpus: f64,
}
pub struct CgroupMonitor {
overview: CgroupOverview,
}
impl CgroupMonitor {
pub fn new() -> Result<Self, IronError> {
let overview = Self::scan()?;
Ok(Self { overview })
}
pub fn refresh(&mut self) -> Result<(), IronError> {
self.overview = Self::scan()?;
Ok(())
}
pub fn overview(&self) -> &CgroupOverview {
&self.overview
}
pub fn containers(&self) -> Vec<&CgroupInfo> {
self.overview
.cgroups
.iter()
.filter(|c| c.is_container)
.collect()
}
pub fn memory_pressure_cgroups(&self) -> Vec<&CgroupInfo> {
self.overview
.cgroups
.iter()
.filter(|c| {
c.memory
.as_ref()
.map(|m| m.usage_pct > 80.0)
.unwrap_or(false)
})
.collect()
}
pub fn throttled_cgroups(&self) -> Vec<&CgroupInfo> {
self.overview
.cgroups
.iter()
.filter(|c| {
c.cpu
.as_ref()
.map(|cpu| cpu.throttled_periods > 0)
.unwrap_or(false)
})
.collect()
}
#[cfg(target_os = "linux")]
fn scan() -> Result<CgroupOverview, IronError> {
let version = Self::detect_version();
let available_controllers = Self::list_controllers(&version);
let cgroups = match version {
CgroupVersion::V2 => Self::scan_v2()?,
CgroupVersion::V1 => Self::scan_v1()?,
_ => Vec::new(),
};
let container_count = cgroups.iter().filter(|c| c.is_container).count();
let total_memory: u64 = cgroups
.iter()
.filter_map(|c| c.memory.as_ref())
.filter(|m| m.limit_bytes < u64::MAX)
.map(|m| m.limit_bytes)
.sum();
let total_cpus: f64 = cgroups
.iter()
.filter_map(|c| c.cpu.as_ref())
.map(|c| c.effective_cpus)
.sum();
Ok(CgroupOverview {
version,
available_controllers,
cgroups,
container_count,
total_memory_limit_bytes: total_memory,
total_effective_cpus: total_cpus,
})
}
#[cfg(target_os = "linux")]
fn detect_version() -> CgroupVersion {
let v2_path = std::path::Path::new("/sys/fs/cgroup/cgroup.controllers");
if v2_path.exists() {
return CgroupVersion::V2;
}
let v1_cpu = std::path::Path::new("/sys/fs/cgroup/cpu");
if v1_cpu.exists() {
return CgroupVersion::V1;
}
CgroupVersion::Unknown
}
#[cfg(target_os = "linux")]
fn list_controllers(version: &CgroupVersion) -> Vec<String> {
match version {
CgroupVersion::V2 => std::fs::read_to_string("/sys/fs/cgroup/cgroup.controllers")
.map(|s| s.split_whitespace().map(String::from).collect())
.unwrap_or_default(),
CgroupVersion::V1 => {
std::fs::read_dir("/sys/fs/cgroup")
.map(|entries| {
entries
.flatten()
.filter(|e| e.path().is_dir())
.map(|e| e.file_name().to_string_lossy().to_string())
.collect()
})
.unwrap_or_default()
}
_ => Vec::new(),
}
}
#[cfg(target_os = "linux")]
fn scan_v2() -> Result<Vec<CgroupInfo>, IronError> {
let mut cgroups = Vec::new();
let base = std::path::Path::new("/sys/fs/cgroup");
Self::scan_v2_dir(base, "", &mut cgroups, 0)?;
Ok(cgroups)
}
#[cfg(target_os = "linux")]
fn scan_v2_dir(
dir: &std::path::Path,
rel_path: &str,
cgroups: &mut Vec<CgroupInfo>,
depth: u32,
) -> Result<(), IronError> {
if depth > 4 {
return Ok(()); }
if let Ok(entries) = std::fs::read_dir(dir) {
for entry in entries.flatten() {
let path = entry.path();
if !path.is_dir() {
continue;
}
let name = entry.file_name().to_string_lossy().to_string();
if name.starts_with('.') {
continue;
}
let cg_path = format!("{}/{}", rel_path, name);
if path.join("cgroup.procs").exists() {
let is_container = name.contains("docker-")
|| name.contains("cri-containerd-")
|| name.contains("libpod-")
|| name.contains("lxc-")
|| name.ends_with(".scope");
let container_runtime = if name.contains("docker-") {
Some("docker".into())
} else if name.contains("cri-containerd-") {
Some("containerd".into())
} else if name.contains("libpod-") {
Some("podman".into())
} else if name.contains("lxc-") {
Some("lxc".into())
} else {
None
};
let friendly_name = if is_container {
name.split('-')
.next_back()
.unwrap_or(&name)
.replace(".scope", "")
} else {
name.clone()
};
let controllers = std::fs::read_to_string(path.join("cgroup.controllers"))
.map(|s| s.split_whitespace().map(String::from).collect())
.unwrap_or_default();
let cpu = Self::read_cpu_v2(&path);
let memory = Self::read_memory_v2(&path);
let pids = Self::read_pids_v2(&path);
cgroups.push(CgroupInfo {
path: cg_path.clone(),
name: friendly_name,
is_container,
container_runtime,
cpu,
memory,
io: None,
pids,
controllers,
});
}
Self::scan_v2_dir(&path, &cg_path, cgroups, depth + 1)?;
}
}
Ok(())
}
#[cfg(target_os = "linux")]
fn read_cpu_v2(path: &std::path::Path) -> Option<CgroupCpu> {
let max_str = std::fs::read_to_string(path.join("cpu.max")).ok()?;
let parts: Vec<&str> = max_str.split_whitespace().collect();
let quota = if parts.first() == Some(&"max") {
-1i64
} else {
parts
.first()
.and_then(|s| s.parse::<i64>().ok())
.unwrap_or(-1)
};
let period = parts
.get(1)
.and_then(|s| s.parse::<u64>().ok())
.unwrap_or(100_000);
let weight = std::fs::read_to_string(path.join("cpu.weight"))
.ok()
.and_then(|s| s.trim().parse::<u64>().ok())
.unwrap_or(100);
let stat_str = std::fs::read_to_string(path.join("cpu.stat")).unwrap_or_default();
let mut usage_us = 0u64;
let mut throttled_periods = 0u64;
let mut throttled_time = 0u64;
for line in stat_str.lines() {
let parts: Vec<&str> = line.split_whitespace().collect();
if parts.len() >= 2 {
match parts[0] {
"usage_usec" => usage_us = parts[1].parse().unwrap_or(0),
"nr_throttled" => throttled_periods = parts[1].parse().unwrap_or(0),
"throttled_usec" => throttled_time = parts[1].parse().unwrap_or(0),
_ => {}
}
}
}
let effective_cpus = if quota < 0 {
0.0 } else {
quota as f64 / period as f64
};
Some(CgroupCpu {
quota_us: quota,
period_us: period,
shares: 0,
weight,
usage_us,
per_cpu_usage_us: Vec::new(),
throttled_periods,
throttled_time_us: throttled_time,
effective_cpus,
})
}
#[cfg(target_os = "linux")]
fn read_memory_v2(path: &std::path::Path) -> Option<CgroupMemory> {
let usage = std::fs::read_to_string(path.join("memory.current"))
.ok()
.and_then(|s| s.trim().parse::<u64>().ok())
.unwrap_or(0);
let limit_str = std::fs::read_to_string(path.join("memory.max"))
.ok()
.unwrap_or_else(|| "max".into());
let limit = if limit_str.trim() == "max" {
u64::MAX
} else {
limit_str.trim().parse::<u64>().unwrap_or(u64::MAX)
};
let swap_usage = std::fs::read_to_string(path.join("memory.swap.current"))
.ok()
.and_then(|s| s.trim().parse::<u64>().ok())
.unwrap_or(0);
let swap_limit_str = std::fs::read_to_string(path.join("memory.swap.max"))
.ok()
.unwrap_or_else(|| "max".into());
let swap_limit = if swap_limit_str.trim() == "max" {
u64::MAX
} else {
swap_limit_str.trim().parse::<u64>().unwrap_or(u64::MAX)
};
let stat_str = std::fs::read_to_string(path.join("memory.stat")).unwrap_or_default();
let mut cache = 0u64;
let mut rss = 0u64;
for line in stat_str.lines() {
let parts: Vec<&str> = line.split_whitespace().collect();
if parts.len() >= 2 {
match parts[0] {
"file" => cache = parts[1].parse().unwrap_or(0),
"anon" => rss = parts[1].parse().unwrap_or(0),
_ => {}
}
}
}
let events_str = std::fs::read_to_string(path.join("memory.events")).unwrap_or_default();
let mut oom_events = 0u64;
for line in events_str.lines() {
if let Some(val) = line.strip_prefix("oom_kill ") {
oom_events = val.trim().parse().unwrap_or(0);
}
}
let usage_pct = if limit < u64::MAX && limit > 0 {
(usage as f64 / limit as f64) * 100.0
} else {
0.0
};
Some(CgroupMemory {
usage_bytes: usage,
limit_bytes: limit,
swap_usage_bytes: swap_usage,
swap_limit_bytes: swap_limit,
cache_bytes: cache,
rss_bytes: rss,
oom_events,
usage_pct,
})
}
#[cfg(target_os = "linux")]
fn read_pids_v2(path: &std::path::Path) -> Option<CgroupPids> {
let current = std::fs::read_to_string(path.join("pids.current"))
.ok()
.and_then(|s| s.trim().parse::<u64>().ok())?;
let limit_str = std::fs::read_to_string(path.join("pids.max"))
.ok()
.unwrap_or_else(|| "max".into());
let limit = if limit_str.trim() == "max" {
u64::MAX
} else {
limit_str.trim().parse::<u64>().unwrap_or(u64::MAX)
};
Some(CgroupPids { current, limit })
}
#[cfg(target_os = "linux")]
fn scan_v1() -> Result<Vec<CgroupInfo>, IronError> {
Ok(Vec::new())
}
#[cfg(not(target_os = "linux"))]
fn scan() -> Result<CgroupOverview, IronError> {
Ok(CgroupOverview {
version: CgroupVersion::Unknown,
available_controllers: Vec::new(),
cgroups: Vec::new(),
container_count: 0,
total_memory_limit_bytes: 0,
total_effective_cpus: 0.0,
})
}
}
impl Default for CgroupMonitor {
fn default() -> Self {
Self::new().unwrap_or(Self {
overview: CgroupOverview {
version: CgroupVersion::Unknown,
available_controllers: Vec::new(),
cgroups: Vec::new(),
container_count: 0,
total_memory_limit_bytes: 0,
total_effective_cpus: 0.0,
},
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_cgroup_version_display() {
assert_eq!(CgroupVersion::V2.to_string(), "v2");
assert_eq!(CgroupVersion::V1.to_string(), "v1");
}
#[test]
fn test_effective_cpus() {
let cpu = CgroupCpu {
quota_us: 200_000,
period_us: 100_000,
shares: 0,
weight: 100,
usage_us: 0,
per_cpu_usage_us: Vec::new(),
throttled_periods: 0,
throttled_time_us: 0,
effective_cpus: 2.0,
};
assert!((cpu.effective_cpus - 2.0).abs() < 0.01);
}
#[test]
fn test_memory_usage_pct() {
let mem = CgroupMemory {
usage_bytes: 500 * 1024 * 1024, limit_bytes: 1024 * 1024 * 1024, swap_usage_bytes: 0,
swap_limit_bytes: u64::MAX,
cache_bytes: 100 * 1024 * 1024,
rss_bytes: 400 * 1024 * 1024,
oom_events: 0,
usage_pct: 50.0 * 500.0 / 512.0, };
assert!(mem.usage_pct > 0.0 && mem.usage_pct < 100.0);
}
#[test]
fn test_container_detection() {
let cg = CgroupInfo {
path: "/system.slice/docker-abc123def456.scope".into(),
name: "abc123def456".into(),
is_container: true,
container_runtime: Some("docker".into()),
cpu: None,
memory: None,
io: None,
pids: None,
controllers: vec!["cpu".into(), "memory".into()],
};
assert!(cg.is_container);
assert_eq!(cg.container_runtime, Some("docker".into()));
}
#[test]
fn test_monitor_default() {
let monitor = CgroupMonitor::default();
let _overview = monitor.overview();
}
#[test]
fn test_serialization() {
let overview = CgroupOverview {
version: CgroupVersion::V2,
available_controllers: vec!["cpu".into(), "memory".into()],
cgroups: Vec::new(),
container_count: 0,
total_memory_limit_bytes: 0,
total_effective_cpus: 0.0,
};
let json = serde_json::to_string(&overview).unwrap();
assert!(json.contains("V2"));
let _: CgroupOverview = serde_json::from_str(&json).unwrap();
}
}