zc2 0.0.49

P2P compute broker with yen-denominated billing, WAL, and broker mesh support
//! Hardware identity of this machine, hashed into the report.
use serde::{Deserialize, Serialize};
use std::collections::HashSet;

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct GpuId {
    pub vendor: String,
    pub model: String,
    pub uuid: String,
    pub vram_bytes: u64,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct HardwareFingerprint {
    pub cpu_model: String,
    pub physical_cores: u32,
    pub logical_cores: u32,
    pub ram_bytes: u64,
    pub gpus: Vec<GpuId>,
}

/// MemTotal of one NUMA node. Reported beside the fingerprint, not inside it:
/// the fingerprint hash must not change for machines already benchmarked.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct NumaNodeMem {
    pub node: u32,
    pub mem_bytes: u64,
}

pub const SYSFS_NODE_DIR: &str = "/sys/devices/system/node";

/// Per-node MemTotal from `<root>/node*/meminfo`, ordered by node id. Empty
/// when the tree is absent (not Linux, or no NUMA support in the kernel).
pub fn numa_memory_from(root: &std::path::Path) -> Vec<NumaNodeMem> {
    let Ok(dir) = std::fs::read_dir(root) else {
        return vec![];
    };
    let mut nodes: Vec<NumaNodeMem> = dir
        .flatten()
        .filter_map(|e| {
            let name = e.file_name();
            let node = name.to_str()?.strip_prefix("node")?.parse::<u32>().ok()?;
            let text = std::fs::read_to_string(e.path().join("meminfo")).ok()?;
            // "Node 0 MemTotal:  65843200 kB"
            let kb = text
                .lines()
                .find_map(|l| l.split_once("MemTotal:"))?
                .1
                .split_whitespace()
                .next()?
                .parse::<u64>()
                .ok()?;
            Some(NumaNodeMem {
                node,
                mem_bytes: kb * 1024,
            })
        })
        .collect();
    nodes.sort_by_key(|n| n.node);
    nodes
}

/// Per-NUMA-node memory of this machine (Linux only; empty elsewhere).
pub fn numa_memory() -> Vec<NumaNodeMem> {
    if cfg!(target_os = "linux") {
        numa_memory_from(std::path::Path::new(SYSFS_NODE_DIR))
    } else {
        vec![]
    }
}

/// A note when the host was busy while measuring: shared load widens the
/// spread between identical machines. `None` at or below half the cores.
pub fn busy_host_note(load1: Option<f64>, logical_cores: u32) -> Option<String> {
    let l = load1?;
    (l > f64::from(logical_cores) / 2.0).then(|| {
        format!("host busy: 1-min load average {l:.1} on {logical_cores} logical cores; figures may read low")
    })
}

/// 1-minute load average, where the platform has one.
#[cfg(not(unix))]
pub fn load_average_1m() -> Option<f64> {
    None
}

/// 1-minute load average, where the platform has one.
#[cfg(unix)]
pub fn load_average_1m() -> Option<f64> {
    let mut l = [0f64; 1];
    // SAFETY: writes at most one f64 into the buffer we own.
    let n = unsafe { libc::getloadavg(l.as_mut_ptr(), 1) };
    (n == 1).then_some(l[0])
}

#[cfg(target_os = "macos")]
fn sysctl(key: &str) -> Option<String> {
    let out = std::process::Command::new("sysctl")
        .args(["-n", key])
        .output()
        .ok()?;
    if !out.status.success() {
        return None;
    }
    let s = String::from_utf8_lossy(&out.stdout).trim().to_string();
    (!s.is_empty()).then_some(s)
}

/// Unique `(physical id, core id)` pairs in /proc/cpuinfo text.
fn physical_cores_from_cpuinfo(text: &str) -> Option<u32> {
    let mut seen = HashSet::new();
    let (mut phys, mut core) = (None::<String>, None::<String>);
    for line in text.lines().chain(std::iter::once("")) {
        if line.trim().is_empty() {
            if let (Some(p), Some(c)) = (phys.take(), core.take()) {
                seen.insert((p, c));
            }
            phys = None;
            core = None;
            continue;
        }
        if let Some((k, v)) = line.split_once(':') {
            match k.trim() {
                "physical id" => phys = Some(v.trim().to_string()),
                "core id" => core = Some(v.trim().to_string()),
                _ => {}
            }
        }
    }
    (!seen.is_empty()).then_some(seen.len() as u32)
}

impl HardwareFingerprint {
    /// GPUs are passed in; the GPU probe owns detection.
    pub fn detect(gpus: Vec<GpuId>) -> Self {
        let logical_cores = std::thread::available_parallelism()
            .map(|n| n.get() as u32)
            .unwrap_or(1);
        // Refined below on Linux and macOS; other platforms keep the defaults.
        #[cfg_attr(not(any(target_os = "linux", target_os = "macos")), allow(unused_mut))]
        let mut physical_cores = logical_cores;
        #[cfg_attr(not(any(target_os = "linux", target_os = "macos")), allow(unused_mut))]
        let mut cpu_model = std::env::consts::ARCH.to_string();
        #[cfg_attr(not(any(target_os = "linux", target_os = "macos")), allow(unused_mut))]
        let mut ram_bytes = 0u64;

        #[cfg(target_os = "linux")]
        {
            if let Ok(t) = std::fs::read_to_string("/proc/cpuinfo") {
                if let Some(n) = physical_cores_from_cpuinfo(&t) {
                    physical_cores = n;
                }
                if let Some(m) = t
                    .lines()
                    .find(|l| l.starts_with("model name"))
                    .and_then(|l| l.split_once(':'))
                {
                    cpu_model = m.1.trim().to_string();
                }
            }
            if let Ok(t) = std::fs::read_to_string("/proc/meminfo") {
                if let Some(kb) = t
                    .lines()
                    .find(|l| l.starts_with("MemTotal:"))
                    .and_then(|l| l.split_whitespace().nth(1))
                    .and_then(|v| v.parse::<u64>().ok())
                {
                    ram_bytes = kb * 1024;
                }
            }
        }
        #[cfg(target_os = "macos")]
        {
            if let Some(n) = sysctl("hw.physicalcpu").and_then(|s| s.parse().ok()) {
                physical_cores = n;
            }
            if let Some(m) = sysctl("machdep.cpu.brand_string") {
                cpu_model = m;
            }
            if let Some(b) = sysctl("hw.memsize").and_then(|s| s.parse().ok()) {
                ram_bytes = b;
            }
        }

        HardwareFingerprint {
            cpu_model,
            physical_cores,
            logical_cores,
            ram_bytes,
            gpus,
        }
    }

    /// Field order is fixed, so the serialisation (and hash) is stable.
    pub fn sha256(&self) -> String {
        crate::broker::node_identity::sha256_hex(
            serde_json::to_string(self)
                .expect("fingerprint serialises")
                .as_bytes(),
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn sha256_is_stable_and_changes_with_hardware() {
        let a = HardwareFingerprint {
            cpu_model: "c".into(),
            physical_cores: 4,
            logical_cores: 8,
            ram_bytes: 1,
            gpus: vec![],
        };
        let mut b = a.clone();
        assert_eq!(a.sha256(), b.sha256());
        assert_eq!(a.sha256().len(), 64);
        b.gpus.push(GpuId {
            vendor: "nvidia".into(),
            model: "RTX 4090".into(),
            uuid: "GPU-1".into(),
            vram_bytes: 1,
        });
        assert_ne!(a.sha256(), b.sha256());
    }
    #[test]
    fn numa_memory_reads_each_node_and_tolerates_a_memless_one() {
        let d = std::env::temp_dir().join(format!("zc-numa-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&d);
        std::fs::create_dir_all(&d).unwrap();
        for (n, kb) in [(1, 0u64), (0, 32768)] {
            let p = d.join(format!("node{n}"));
            std::fs::create_dir(&p).unwrap();
            std::fs::write(
                p.join("meminfo"),
                format!("Node {n} MemTotal:       {kb} kB\nNode {n} MemFree: 1 kB\n"),
            )
            .unwrap();
        }
        std::fs::create_dir(d.join("power")).unwrap();
        let got = numa_memory_from(&d);
        assert_eq!(
            got,
            vec![
                NumaNodeMem {
                    node: 0,
                    mem_bytes: 32768 * 1024
                },
                NumaNodeMem {
                    node: 1,
                    mem_bytes: 0
                },
            ]
        );
        assert!(numa_memory_from(&d.join("missing")).is_empty());
        let _ = std::fs::remove_dir_all(&d);
    }
    #[test]
    fn busy_note_only_above_half_the_cores() {
        assert_eq!(busy_host_note(Some(16.0), 32), None);
        assert_eq!(busy_host_note(None, 32), None);
        let n = busy_host_note(Some(16.5), 32).unwrap();
        assert!(n.starts_with("host busy") && n.contains("16.5"));
    }
    #[test]
    fn detect_reports_this_machine() {
        let f = HardwareFingerprint::detect(vec![]);
        assert!(f.logical_cores >= 1 && f.ram_bytes > 0);
    }
    #[test]
    fn cpuinfo_counts_unique_physical_cores() {
        let t = "processor : 0\nphysical id : 0\ncore id : 0\n\nprocessor : 1\nphysical id : 0\ncore id : 0\n\nprocessor : 2\nphysical id : 0\ncore id : 1\n";
        assert_eq!(physical_cores_from_cpuinfo(t), Some(2));
        assert_eq!(physical_cores_from_cpuinfo("processor : 0\n"), None);
    }
}