zc2 0.0.51

P2P compute broker with yen-denominated billing, WAL, and broker mesh support
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
//! `GET /v2/resources`: what this machine has, what the owner's own apps use,
//! and what Zakuro is allowed, has reserved and actually uses — the desktop
//! app's Resources screen.
//!
//! - `host`: capacity (logical CPUs, memory, disk of the zakuro state dir).
//! - `host_used`: the whole machine right now, Zakuro included (CPU over a
//!   short sample, memory as the OS counts it in use).
//! - `zakuro.budget`: the owner's sharing budget (`policy::node_budget`),
//!   resolved against this host: the most Zakuro may ever take.
//! - `zakuro.vm`: the Lima/Colima VM's real size and state. A running VM holds
//!   its memory from the host whether or not jobs use it.
//! - `zakuro.used`: what Zakuro uses now — inside the VM (its `/proc`) plus
//!   the agent's own process tree on the host (broker, host workers).
//!
//! Every reading that cannot be taken is `null` rather than zero.

use serde::Serialize;
use std::sync::Mutex;
use std::time::{Duration, Instant};

/// Readings are reused for this long: the window polls every few seconds and
/// each snapshot samples the CPU and may `ssh` into the VM.
const CACHE_FOR: Duration = Duration::from_secs(4);
/// The CPU sample window, on the host and in the VM.
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>,
}

/// `cpu_cores`: cores busy (2.5 = two and a half of `host.cpus`).
#[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 {
    /// "Colima VM", "Lima VM", "LXC container", "Docker", or null (none).
    pub engine: Option<String>,
    pub budget: Option<Budget>,
    pub vm: Option<Vm>,
    pub used: Usage,
    /// What `used` covers: `vm+processes` or `processes`.
    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,
    /// `none`, `all`, a count, or a device spec.
    pub gpu: String,
}

#[derive(Serialize, Clone, Debug, PartialEq)]
pub struct Vm {
    pub name: String,
    /// `running`, `stopped`, … as limactl/colima report it, lower-cased;
    /// `not_created` before the first share.
    pub state: String,
    pub cpus: u32,
    pub memory_bytes: u64,
    pub disk_bytes: u64,
}

/// Busy and total ticks of one `/proc/stat` or Mach CPU sample.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Ticks {
    pub busy: u64,
    pub total: u64,
}

/// Fraction of the time busy between two samples, 0..=1.
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))
}

/// The aggregate `cpu ` line of `/proc/stat`. Idle = idle + iowait.
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;
    }
    // user nice system idle iowait irq softirq steal (guest is inside user).
    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,
    })
}

/// In use = MemTotal − MemAvailable (`/proc/meminfo`, kB).
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)
}

/// CPU (cores) and memory of `root` and every process under it, from
/// `ps -A -o pid=,ppid=,pcpu=,rss=` (`pcpu` is percent of one core, `rss` kB).
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]);
    // Parents can be listed after children: repeat until nothing is added.
    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)),
    }
}

/// The in-VM probe: two `/proc/stat` samples around a sleep, then meminfo.
/// Parsed by [`parse_vm_probe`].
const VM_PROBE: &str =
    "head -1 /proc/stat; sleep 0.4; echo ---; head -1 /proc/stat; echo ---; cat /proc/meminfo";

/// `(busy fraction, memory in use)` from [`VM_PROBE`]'s output.
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)] // libc's mach_host_self / host_statistics: see host_memory_used.
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;
    // SAFETY: `info` is a host_cpu_load_info and `count` its size in
    // natural_t, as HOST_CPU_LOAD_INFO expects.
    #[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
}

/// Memory in use as Activity Monitor counts it: app memory (internal pages
/// less purgeable) + wired + compressed.
#[cfg(target_os = "macos")]
#[allow(deprecated)] // libc's mach_host_self: still the Mach API, mach2 is not a dependency.
fn host_memory_used() -> Option<u64> {
    let mut vm: libc::vm_statistics64 = unsafe { std::mem::zeroed() };
    let mut count = libc::HOST_VM_INFO64_COUNT;
    // SAFETY: `vm` is a vm_statistics64 and `count` its size in integer_t.
    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;
    }
    // SAFETY: sysconf has no preconditions.
    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() };
    // SAFETY: `c` is a valid NUL-terminated path and `st` a writable statvfs.
    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)
}

/// `(status, cpus, memory, disk)` from `limactl`/`colima list`.
type VmInfo = Option<(String, u32, u64, u64)>;
type VmExec = Box<dyn Fn() -> Option<String>>;

/// The VM's size and state, and its in-VM usage when it runs.
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();
    // The VM probe sleeps as long as the host sample: run them side by side.
    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"),
    };

    // The owner's limit for the whole machine, before the split between
    // its nodes (`GET /v2/local-nodes` has each node's share).
    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,
        },
    }
}

/// The cached snapshot (at most [`CACHE_FOR`] old). Blocking: call from
/// `spawn_blocking`.
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() {
        // 10 = agent → 11 broker → 12 worker; 20 is someone else's; the
        // worker is listed before its parent.
        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 {
    /// `cargo test --bin zc agent::resources::live -- --ignored --nocapture`
    #[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()
        );
    }
}