use serde::Serialize;
use std::sync::Mutex;
use std::time::{Duration, Instant};
const CACHE_FOR: Duration = Duration::from_secs(4);
const SAMPLE: Duration = Duration::from_millis(400);
#[derive(Serialize, Clone, Debug, PartialEq)]
pub struct Resources {
pub v: u32,
pub as_of: String,
pub host: Host,
pub host_used: Usage,
pub zakuro: Zakuro,
}
#[derive(Serialize, Clone, Debug, PartialEq)]
pub struct Host {
pub cpus: usize,
pub memory_bytes: Option<u64>,
pub disk_bytes: Option<u64>,
pub disk_free_bytes: Option<u64>,
}
#[derive(Serialize, Clone, Debug, Default, PartialEq)]
pub struct Usage {
pub cpu_cores: Option<f64>,
pub memory_bytes: Option<u64>,
}
#[derive(Serialize, Clone, Debug, PartialEq)]
pub struct Zakuro {
pub engine: Option<String>,
pub budget: Option<Budget>,
pub vm: Option<Vm>,
pub used: Usage,
pub used_scope: &'static str,
}
#[derive(Serialize, Clone, Debug, PartialEq)]
pub struct Budget {
pub cpu_cores: f64,
pub memory_bytes: u64,
pub disk_bytes: u64,
pub gpu: String,
}
#[derive(Serialize, Clone, Debug, PartialEq)]
pub struct Vm {
pub name: String,
pub state: String,
pub cpus: u32,
pub memory_bytes: u64,
pub disk_bytes: u64,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Ticks {
pub busy: u64,
pub total: u64,
}
pub fn busy_fraction(a: Ticks, b: Ticks) -> Option<f64> {
let total = b.total.checked_sub(a.total)?;
let busy = b.busy.checked_sub(a.busy)?;
(total > 0).then(|| (busy as f64 / total as f64).clamp(0.0, 1.0))
}
pub fn parse_proc_stat(text: &str) -> Option<Ticks> {
let line = text.lines().find(|l| l.starts_with("cpu "))?;
let n: Vec<u64> = line
.split_whitespace()
.skip(1)
.filter_map(|x| x.parse().ok())
.collect();
if n.len() < 4 {
return None;
}
let total: u64 = n.iter().take(8).sum();
let idle = n[3] + n.get(4).copied().unwrap_or(0);
Some(Ticks {
busy: total - idle,
total,
})
}
pub fn meminfo_used(text: &str) -> Option<u64> {
let kb = |key: &str| -> Option<u64> {
text.lines()
.find(|l| l.starts_with(key))?
.split_whitespace()
.nth(1)?
.parse()
.ok()
};
Some(kb("MemTotal:")?.saturating_sub(kb("MemAvailable:")?) * 1024)
}
pub fn tree_usage(ps: &str, root: u32) -> Usage {
let rows: Vec<(u32, u32, f64, u64)> = ps
.lines()
.filter_map(|l| {
let mut f = l.split_whitespace();
Some((
f.next()?.parse().ok()?,
f.next()?.parse().ok()?,
f.next()?.parse().ok()?,
f.next()?.parse().ok()?,
))
})
.collect();
let mut inside = std::collections::HashSet::from([root]);
loop {
let before = inside.len();
for (pid, ppid, _, _) in &rows {
if inside.contains(ppid) {
inside.insert(*pid);
}
}
if inside.len() == before {
break;
}
}
let (mut cpu, mut rss) = (0.0, 0u64);
for (pid, _, pcpu, kb) in &rows {
if inside.contains(pid) {
cpu += pcpu / 100.0;
rss += kb * 1024;
}
}
Usage {
cpu_cores: Some(cpu),
memory_bytes: Some(rss),
}
}
fn add(a: Option<f64>, b: Option<f64>) -> Option<f64> {
match (a, b) {
(None, None) => None,
(x, y) => Some(x.unwrap_or(0.0) + y.unwrap_or(0.0)),
}
}
fn add_u(a: Option<u64>, b: Option<u64>) -> Option<u64> {
match (a, b) {
(None, None) => None,
(x, y) => Some(x.unwrap_or(0) + y.unwrap_or(0)),
}
}
const VM_PROBE: &str =
"head -1 /proc/stat; sleep 0.4; echo ---; head -1 /proc/stat; echo ---; cat /proc/meminfo";
pub fn parse_vm_probe(out: &str) -> (Option<f64>, Option<u64>) {
let parts: Vec<&str> = out.split("---").collect();
let cpu = match (
parts.first().and_then(|p| parse_proc_stat(p)),
parts.get(1).and_then(|p| parse_proc_stat(p)),
) {
(Some(a), Some(b)) => busy_fraction(a, b),
_ => None,
};
(cpu, parts.get(2).and_then(|p| meminfo_used(p)))
}
pub fn gpu_label(g: &crate::sandbox::Gpus) -> String {
use crate::sandbox::Gpus;
match g {
Gpus::None => "none".into(),
Gpus::All => "all".into(),
Gpus::Count(n) => n.to_string(),
Gpus::Spec(s) => s.clone(),
}
}
#[cfg(target_os = "macos")]
#[allow(deprecated)] fn host_ticks() -> Option<Ticks> {
let mut info: libc::host_cpu_load_info = unsafe { std::mem::zeroed() };
let mut count = libc::HOST_CPU_LOAD_INFO_COUNT;
#[allow(deprecated)]
let kr = unsafe {
libc::host_statistics(
libc::mach_host_self(),
libc::HOST_CPU_LOAD_INFO,
&mut info as *mut _ as libc::host_info_t,
&mut count,
)
};
if kr != 0 {
return None;
}
let t = info.cpu_ticks.map(u64::from);
let total: u64 = t.iter().sum();
Some(Ticks {
busy: total - t[libc::CPU_STATE_IDLE as usize],
total,
})
}
#[cfg(target_os = "linux")]
fn host_ticks() -> Option<Ticks> {
parse_proc_stat(&std::fs::read_to_string("/proc/stat").ok()?)
}
#[cfg(not(any(target_os = "macos", target_os = "linux")))]
fn host_ticks() -> Option<Ticks> {
None
}
#[cfg(target_os = "macos")]
#[allow(deprecated)] fn host_memory_used() -> Option<u64> {
let mut vm: libc::vm_statistics64 = unsafe { std::mem::zeroed() };
let mut count = libc::HOST_VM_INFO64_COUNT;
let kr = unsafe {
libc::host_statistics64(
libc::mach_host_self(),
libc::HOST_VM_INFO64,
&mut vm as *mut _ as libc::host_info64_t,
&mut count,
)
};
if kr != 0 {
return None;
}
let page = unsafe { libc::sysconf(libc::_SC_PAGESIZE) };
if page <= 0 {
return None;
}
let pages = u64::from(vm.internal_page_count).saturating_sub(u64::from(vm.purgeable_count))
+ u64::from(vm.wire_count)
+ u64::from(vm.compressor_page_count);
Some(pages * page as u64)
}
#[cfg(target_os = "linux")]
fn host_memory_used() -> Option<u64> {
meminfo_used(&std::fs::read_to_string("/proc/meminfo").ok()?)
}
#[cfg(not(any(target_os = "macos", target_os = "linux")))]
fn host_memory_used() -> Option<u64> {
None
}
fn disk_total(path: &std::path::Path) -> Option<u64> {
use std::os::unix::ffi::OsStrExt;
let c = std::ffi::CString::new(path.as_os_str().as_bytes()).ok()?;
let mut st: libc::statvfs = unsafe { std::mem::zeroed() };
if unsafe { libc::statvfs(c.as_ptr(), &mut st) } != 0 {
return None;
}
#[allow(clippy::unnecessary_cast)]
Some(st.f_blocks as u64 * st.f_frsize as u64)
}
type VmInfo = Option<(String, u32, u64, u64)>;
type VmExec = Box<dyn Fn() -> Option<String>>;
fn vm_reading(engine: &crate::sandbox::engine::Engine) -> Option<(Vm, Usage)> {
use crate::sandbox::engine::Engine;
let (name, info, exec): (String, VmInfo, VmExec) = match engine {
Engine::Colima { .. } => {
let rt = crate::sandbox::colima::ColimaRuntime::shell();
let info = rt
.info()
.ok()
.flatten()
.map(|i| (i.status, i.cpus, i.memory_bytes, i.disk_bytes));
let name = rt.profile().to_string();
(name, info, Box::new(move || rt.exec_sh(VM_PROBE).ok()))
}
Engine::Lima { .. } => {
let rt = crate::sandbox::lima::LimaRuntime::shell();
let info = rt
.info()
.ok()
.flatten()
.map(|i| (i.status, i.cpus, i.memory_bytes, i.disk_bytes));
let name = rt.instance().to_string();
(name, info, Box::new(move || rt.exec_sh(VM_PROBE).ok()))
}
_ => return None,
};
let Some((status, cpus, memory_bytes, disk_bytes)) = info else {
return Some((
Vm {
name,
state: "not_created".into(),
cpus: 0,
memory_bytes: 0,
disk_bytes: 0,
},
Usage::default(),
));
};
let state = status.to_ascii_lowercase();
let used = if state == "running" {
let (busy, mem) = exec().map(|o| parse_vm_probe(&o)).unwrap_or((None, None));
Usage {
cpu_cores: busy.map(|f| f * f64::from(cpus)),
memory_bytes: mem,
}
} else {
Usage::default()
};
Some((
Vm {
name,
state,
cpus,
memory_bytes,
disk_bytes,
},
used,
))
}
fn snapshot_now() -> Resources {
use crate::sandbox::engine::Engine;
let hr = crate::sandbox::HostResources::detect();
let home = crate::credentials::dir().unwrap_or_else(|| "/".into());
let disk_path = if home.exists() { home } else { "/".into() };
let active = crate::sandbox::engine::active();
let engine = active.engine.clone();
let vm = engine
.clone()
.map(|e| std::thread::spawn(move || vm_reading(&e)));
let t0 = host_ticks();
std::thread::sleep(SAMPLE);
let t1 = host_ticks();
let host_cpu = match (t0, t1) {
(Some(a), Some(b)) => busy_fraction(a, b).map(|f| f * hr.logical_cpus as f64),
_ => None,
};
let procs = std::process::Command::new("ps")
.args(["-A", "-o", "pid=,ppid=,pcpu=,rss="])
.output()
.ok()
.map(|o| tree_usage(&String::from_utf8_lossy(&o.stdout), std::process::id()))
.unwrap_or_default();
let vm = vm.and_then(|h| h.join().ok()).flatten();
let (used, scope) = match &vm {
Some((_, in_vm)) => (
Usage {
cpu_cores: add(in_vm.cpu_cores, procs.cpu_cores),
memory_bytes: add_u(in_vm.memory_bytes, procs.memory_bytes),
},
"vm+processes",
),
None => (procs, "processes"),
};
let budget =
crate::policy::machine_budget(crate::credentials::machine_dir().as_deref(), &|k| {
std::env::var(k).ok()
})
.ok()
.and_then(|b| {
let l = b.total(&hr).ok()?;
Some(Budget {
cpu_cores: f64::from(l.cpu_millis) / 1000.0,
memory_bytes: l.memory_bytes,
disk_bytes: b.disk_bytes,
gpu: gpu_label(&l.gpus),
})
});
Resources {
v: 1,
as_of: chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, true),
host: Host {
cpus: hr.logical_cpus,
memory_bytes: hr.memory_bytes,
disk_bytes: disk_total(&disk_path),
disk_free_bytes: crate::setup::free_disk(&disk_path),
},
host_used: Usage {
cpu_cores: host_cpu,
memory_bytes: host_memory_used(),
},
zakuro: Zakuro {
engine: engine.as_ref().map(|e| {
match e {
Engine::Lima { .. } => "Lima VM",
Engine::Colima { .. } => "Colima VM",
e if e.is_lxc() => "LXC container",
_ => "Docker",
}
.to_string()
}),
budget,
vm: vm.map(|(v, _)| v),
used,
used_scope: scope,
},
}
}
pub fn snapshot() -> Resources {
static C: Mutex<Option<(Instant, Resources)>> = Mutex::new(None);
let mut g = C.lock().unwrap_or_else(|p| p.into_inner());
if let Some((at, r)) = g.as_ref() {
if at.elapsed() < CACHE_FOR {
return r.clone();
}
}
let r = snapshot_now();
*g = Some((Instant::now(), r.clone()));
r
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn proc_stat_counts_iowait_as_idle() {
let a = parse_proc_stat("cpu 100 0 100 700 100 0 0 0 0 0\ncpu0 1 2 3 4\n").unwrap();
assert_eq!(
a,
Ticks {
busy: 200,
total: 1000
}
);
let b = parse_proc_stat("cpu 200 0 200 1300 300 0 0 0 0 0\n").unwrap();
assert_eq!(busy_fraction(a, b), Some(0.2));
assert_eq!(
busy_fraction(b, a),
None,
"a counter going back is no reading"
);
assert!(parse_proc_stat("intr 1 2").is_none());
}
#[test]
fn meminfo_in_use_is_total_less_available() {
let m = "MemTotal: 8000000 kB\nMemFree: 100 kB\nMemAvailable: 6000000 kB\n";
assert_eq!(meminfo_used(m), Some(2_000_000 * 1024));
assert_eq!(meminfo_used("MemTotal: 1 kB\n"), None);
}
#[test]
fn the_vm_probe_reads_cpu_and_memory() {
let out = "cpu 0 0 0 100 0 0 0 0\n---\ncpu 50 0 0 150 0 0 0 0\n---\nMemTotal: 4000 kB\nMemAvailable: 1000 kB\n";
assert_eq!(parse_vm_probe(out), (Some(0.5), Some(3000 * 1024)));
assert_eq!(parse_vm_probe("ssh: connect failed"), (None, None));
}
#[test]
fn the_tree_is_the_agent_and_everything_under_it() {
let ps = " 12 11 50.0 2048\n 10 1 1.0 1024\n 11 10 25.0 1024\n 20 1 99.0 9999\n";
let u = tree_usage(ps, 10);
assert_eq!(u.memory_bytes, Some(4096 * 1024));
assert!((u.cpu_cores.unwrap() - 0.76).abs() < 1e-9);
}
#[test]
fn missing_halves_stay_unknown_only_when_both_are() {
assert_eq!(add(None, None), None);
assert_eq!(add(Some(1.0), None), Some(1.0));
assert_eq!(add_u(None, Some(2)), Some(2));
}
#[test]
fn the_real_host_reads() {
assert!(host_ticks().is_some());
assert!(host_memory_used().is_some_and(|m| m > 0));
}
}
#[cfg(test)]
mod live {
#[test]
#[ignore = "reads the real host and may ssh into the zakuro VM"]
fn print_real_snapshot() {
println!(
"{}",
serde_json::to_string_pretty(&super::snapshot()).unwrap()
);
}
}