denet 0.7.0

a simple process monitor
Documentation
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
//! Host/NUMA/affinity environment snapshot for reproducibility.
//!
//! A one-shot `env` record captured at the start of monitoring. Fields are
//! best-effort: anything not readable (containers, non-x86, non-Linux)
//! degrades to `None` or empty.

use serde::{Deserialize, Serialize};
use std::fs;
use std::time::{SystemTime, UNIX_EPOCH};

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct EnvRecord {
    pub ts_ms: u64,
    pub host: String,
    pub kernel: String,
    pub lscpu: LsCpu,
    pub numa: Numa,
    /// CPU affinity inherited by the monitoring process, as a range list
    /// (e.g. "0-3,7-9"). Empty string if unknown.
    pub affinity_inherited: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cpu_governor: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cpu_freq_khz: Option<Vec<u64>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thp_enabled: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub smt_active: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cgroup: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct LsCpu {
    pub sockets: u32,
    pub cores_per_socket: u32,
    pub threads_per_core: u32,
    pub model: String,
}

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct Numa {
    pub nodes: u32,
    pub distances: Vec<Vec<u32>>,
    pub node_sizes_mb: Vec<u64>,
}

impl EnvRecord {
    /// Collect the environment snapshot. `pid` is used to look up cgroup
    /// membership; pass the monitored PID (not the monitor's own PID).
    pub fn collect(pid: u32) -> Self {
        let ts_ms = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map(|d| d.as_millis() as u64)
            .unwrap_or(0);

        Self {
            ts_ms,
            host: hostname(),
            kernel: kernel_release(),
            lscpu: lscpu(),
            numa: numa(),
            affinity_inherited: affinity_range_list(),
            cpu_governor: read_cpu_attr("scaling_governor", |s| s.trim().to_string()),
            cpu_freq_khz: read_cpu_attr("scaling_cur_freq", |s| s.trim().parse().ok())
                .map(|v: Vec<Option<u64>>| v.into_iter().flatten().collect()),
            thp_enabled: fs::read_to_string("/sys/kernel/mm/transparent_hugepage/enabled")
                .ok()
                .map(|s| s.trim().to_string()),
            smt_active: fs::read_to_string("/sys/devices/system/cpu/smt/active")
                .ok()
                .and_then(|s| s.trim().parse::<u8>().ok().map(|n| n != 0)),
            cgroup: fs::read_to_string(format!("/proc/{pid}/cgroup"))
                .ok()
                .map(|s| s.trim().to_string()),
        }
    }
}

// ---------- low-level collectors ----------

fn hostname() -> String {
    #[cfg(target_os = "linux")]
    {
        if let Ok(s) = fs::read_to_string("/proc/sys/kernel/hostname") {
            return s.trim().to_string();
        }
    }
    String::new()
}

fn kernel_release() -> String {
    #[cfg(target_os = "linux")]
    {
        if let Ok(s) = fs::read_to_string("/proc/sys/kernel/osrelease") {
            return s.trim().to_string();
        }
    }
    String::new()
}

fn lscpu() -> LsCpu {
    let model = fs::read_to_string("/proc/cpuinfo")
        .ok()
        .and_then(|s| parse_cpu_model(&s))
        .unwrap_or_default();

    let (sockets, cores_per_socket, threads_per_core) = cpu_topology().unwrap_or((0, 0, 0));

    LsCpu {
        sockets,
        cores_per_socket,
        threads_per_core,
        model,
    }
}

fn numa() -> Numa {
    let mut node_ids: Vec<u32> = match fs::read_dir("/sys/devices/system/node") {
        Ok(rd) => rd
            .filter_map(|e| e.ok())
            .filter_map(|e| {
                let n = e.file_name();
                let s = n.to_str()?;
                s.strip_prefix("node")?.parse::<u32>().ok()
            })
            .collect(),
        Err(_) => Vec::new(),
    };
    node_ids.sort_unstable();

    let nodes = node_ids.len() as u32;
    let distances: Vec<Vec<u32>> = node_ids
        .iter()
        .map(|id| {
            fs::read_to_string(format!("/sys/devices/system/node/node{id}/distance"))
                .ok()
                .map(|s| parse_distance_row(&s))
                .unwrap_or_default()
        })
        .collect();
    let node_sizes_mb: Vec<u64> = node_ids
        .iter()
        .map(|id| {
            fs::read_to_string(format!("/sys/devices/system/node/node{id}/meminfo"))
                .ok()
                .and_then(|s| parse_node_memtotal_mb(&s))
                .unwrap_or(0)
        })
        .collect();

    Numa {
        nodes,
        distances,
        node_sizes_mb,
    }
}

fn cpu_topology() -> Option<(u32, u32, u32)> {
    use std::collections::BTreeSet;
    let cpus = fs::read_dir("/sys/devices/system/cpu").ok()?;
    let mut sockets: BTreeSet<u32> = BTreeSet::new();
    let mut cores_per_socket: std::collections::HashMap<u32, BTreeSet<u32>> = Default::default();
    let mut siblings_count: Option<u32> = None;
    let mut total_cpus: u32 = 0;

    for entry in cpus.flatten() {
        let name = entry.file_name();
        let name = match name.to_str() {
            Some(s) => s.to_string(),
            None => continue,
        };
        if !name.starts_with("cpu") || !name[3..].chars().all(|c| c.is_ascii_digit()) {
            continue;
        }
        let topo = entry.path().join("topology");
        let pkg = fs::read_to_string(topo.join("physical_package_id"))
            .ok()
            .and_then(|s| s.trim().parse::<u32>().ok());
        let core = fs::read_to_string(topo.join("core_id"))
            .ok()
            .and_then(|s| s.trim().parse::<u32>().ok());
        let sibs = fs::read_to_string(topo.join("thread_siblings_list")).ok();

        if let (Some(p), Some(c)) = (pkg, core) {
            sockets.insert(p);
            cores_per_socket.entry(p).or_default().insert(c);
            total_cpus += 1;
            if siblings_count.is_none() {
                if let Some(list) = sibs {
                    siblings_count = Some(count_range_list(list.trim()) as u32);
                }
            }
        }
    }

    if total_cpus == 0 {
        return None;
    }
    let n_sockets = sockets.len() as u32;
    let cores = cores_per_socket
        .values()
        .map(|s| s.len() as u32)
        .max()
        .unwrap_or(0);
    let threads = siblings_count.unwrap_or(1).max(1);
    Some((n_sockets, cores, threads))
}

/// Read a per-cpu sysfs attribute (e.g. cpufreq/scaling_governor) for every
/// online CPU. Returns None if the attribute is unreadable for cpu0.
fn read_cpu_attr<T, F>(attr: &str, mut parse: F) -> Option<Vec<T>>
where
    F: FnMut(&str) -> T,
{
    let mut results = Vec::new();
    for cpu in 0u32.. {
        let path = format!("/sys/devices/system/cpu/cpu{cpu}/cpufreq/{attr}");
        match fs::read_to_string(&path) {
            Ok(s) => results.push(parse(&s)),
            Err(_) => break,
        }
    }
    if results.is_empty() {
        None
    } else {
        Some(results)
    }
}

#[cfg(target_os = "linux")]
fn affinity_range_list() -> String {
    let mut set = unsafe { std::mem::zeroed::<libc::cpu_set_t>() };
    let rc =
        unsafe { libc::sched_getaffinity(0, std::mem::size_of::<libc::cpu_set_t>(), &mut set) };
    if rc != 0 {
        return String::new();
    }
    let max = libc::CPU_SETSIZE as usize;
    let cpus: Vec<u32> = (0..max)
        .filter(|&i| unsafe { libc::CPU_ISSET(i, &set) })
        .map(|i| i as u32)
        .collect();
    format_range_list(&cpus)
}

#[cfg(not(target_os = "linux"))]
fn affinity_range_list() -> String {
    String::new()
}

// ---------- pure parsers (unit-testable) ----------

/// Parse the first `model name` line out of /proc/cpuinfo content.
pub fn parse_cpu_model(cpuinfo: &str) -> Option<String> {
    for line in cpuinfo.lines() {
        if let Some(rest) = line.strip_prefix("model name") {
            let v = rest.trim_start_matches(|c: char| c == ':' || c.is_whitespace());
            return Some(v.trim().to_string());
        }
    }
    None
}

/// Parse a `/sys/.../node{i}/distance` row: whitespace-separated u32s.
pub fn parse_distance_row(s: &str) -> Vec<u32> {
    s.split_whitespace()
        .filter_map(|t| t.parse::<u32>().ok())
        .collect()
}

/// Parse `MemTotal:` (in kB) out of a `/sys/.../node{i}/meminfo` and return MB.
pub fn parse_node_memtotal_mb(meminfo: &str) -> Option<u64> {
    for line in meminfo.lines() {
        // Format: "Node 0 MemTotal:       65814528 kB"
        let lower = line.to_ascii_lowercase();
        if let Some(idx) = lower.find("memtotal:") {
            let rest = &line[idx + "memtotal:".len()..];
            let mut it = rest.split_whitespace();
            if let Some(kb) = it.next().and_then(|t| t.parse::<u64>().ok()) {
                return Some(kb / 1024);
            }
        }
    }
    None
}

/// Format a sorted-or-unsorted list of CPU ids as a range list:
/// `[0,1,2,3,7,8,9] -> "0-3,7-9"`. Empty input returns "".
pub fn format_range_list(cpus: &[u32]) -> String {
    if cpus.is_empty() {
        return String::new();
    }
    let mut v = cpus.to_vec();
    v.sort_unstable();
    v.dedup();

    let mut out = String::new();
    let mut start = v[0];
    let mut prev = v[0];
    for &n in &v[1..] {
        if n == prev + 1 {
            prev = n;
            continue;
        }
        push_range(&mut out, start, prev);
        start = n;
        prev = n;
    }
    push_range(&mut out, start, prev);
    out
}

fn push_range(out: &mut String, start: u32, end: u32) {
    if !out.is_empty() {
        out.push(',');
    }
    if start == end {
        out.push_str(&start.to_string());
    } else {
        out.push_str(&format!("{start}-{end}"));
    }
}

/// Count the CPUs in a range-list string like "0,2-4,7" -> 5.
pub fn count_range_list(s: &str) -> usize {
    let mut total = 0;
    for part in s.split(',') {
        let part = part.trim();
        if part.is_empty() {
            continue;
        }
        if let Some((a, b)) = part.split_once('-') {
            let (a, b) = (a.parse::<u32>().ok(), b.parse::<u32>().ok());
            if let (Some(a), Some(b)) = (a, b) {
                if b >= a {
                    total += (b - a + 1) as usize;
                }
            }
        } else if part.parse::<u32>().is_ok() {
            total += 1;
        }
    }
    total
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn affinity_range_compression_basic() {
        assert_eq!(format_range_list(&[0, 1, 2, 3]), "0-3");
        assert_eq!(format_range_list(&[0, 1, 2, 3, 7, 8, 9]), "0-3,7-9");
        assert_eq!(format_range_list(&[5]), "5");
        assert_eq!(format_range_list(&[]), "");
    }

    #[test]
    fn affinity_range_compression_unsorted_and_duplicates() {
        assert_eq!(format_range_list(&[3, 0, 2, 1, 1]), "0-3");
        assert_eq!(format_range_list(&[7, 9, 8]), "7-9");
    }

    #[test]
    fn affinity_range_compression_singletons_mixed() {
        assert_eq!(format_range_list(&[0, 2, 4]), "0,2,4");
        assert_eq!(format_range_list(&[0, 1, 4, 5]), "0-1,4-5");
    }

    #[test]
    fn count_range_list_roundtrip() {
        assert_eq!(count_range_list("0-3,7-9"), 7);
        assert_eq!(count_range_list("0,2,4"), 3);
        assert_eq!(count_range_list("5"), 1);
        assert_eq!(count_range_list(""), 0);
    }

    #[test]
    fn count_range_list_handles_malformed_parts() {
        assert_eq!(count_range_list("0, ,2"), 2);
        assert_eq!(count_range_list("5-3"), 0); // inverted range
        assert_eq!(count_range_list("a-b,2"), 1);
    }

    #[test]
    fn numa_distance_parser_square_row() {
        assert_eq!(parse_distance_row("10 12 12 12"), vec![10, 12, 12, 12]);
        assert_eq!(parse_distance_row("10\n12\n12"), vec![10, 12, 12]);
        assert_eq!(parse_distance_row(""), Vec::<u32>::new());
    }

    #[test]
    fn meminfo_parses_memtotal_in_mb() {
        let s = "Node 0 MemTotal:       65814528 kB\nNode 0 Other: 0 kB\n";
        assert_eq!(parse_node_memtotal_mb(s), Some(64272));
    }

    #[test]
    fn meminfo_missing_returns_none() {
        assert_eq!(parse_node_memtotal_mb("Node 0 Free: 1 kB"), None);
    }

    #[test]
    fn cpu_model_parsed_from_proc_cpuinfo() {
        let s = "processor\t: 0\nvendor_id\t: AuthenticAMD\nmodel name\t: AMD EPYC 7742 64-Core Processor\ncache size\t: 512 KB\n";
        assert_eq!(
            parse_cpu_model(s),
            Some("AMD EPYC 7742 64-Core Processor".to_string())
        );
    }

    #[test]
    fn cpu_model_missing_returns_none() {
        assert_eq!(parse_cpu_model("processor: 0\n"), None);
    }

    #[test]
    fn env_record_serializes_with_optional_fields() {
        let env = EnvRecord {
            ts_ms: 1_700_000_000_000,
            host: "omnibenchmark".into(),
            kernel: "6.18.7-test".into(),
            lscpu: LsCpu {
                sockets: 1,
                cores_per_socket: 64,
                threads_per_core: 2,
                model: "AMD EPYC 7742".into(),
            },
            numa: Numa {
                nodes: 4,
                distances: vec![
                    vec![10, 12, 12, 12],
                    vec![12, 10, 12, 12],
                    vec![12, 12, 10, 12],
                    vec![12, 12, 12, 10],
                ],
                node_sizes_mb: vec![64272, 64500, 64500, 64481],
            },
            affinity_inherited: "0-127".into(),
            cpu_governor: Some(vec!["performance".into()]),
            cpu_freq_khz: Some(vec![2_400_000]),
            thp_enabled: Some("always [madvise] never".into()),
            smt_active: Some(true),
            cgroup: Some("0::/user.slice".into()),
        };
        let s = serde_json::to_string(&env).unwrap();
        assert!(s.contains("\"host\":\"omnibenchmark\""));
        assert!(s.contains("\"affinity_inherited\":\"0-127\""));
        assert!(s.contains("\"smt_active\":true"));

        // Optional Nones get elided.
        let mut env2 = env.clone();
        env2.cpu_governor = None;
        env2.cpu_freq_khz = None;
        env2.thp_enabled = None;
        env2.smt_active = None;
        env2.cgroup = None;
        let s2 = serde_json::to_string(&env2).unwrap();
        assert!(!s2.contains("cpu_governor"));
        assert!(!s2.contains("thp_enabled"));

        // Roundtrip preserves NUMA matrix.
        let back: EnvRecord = serde_json::from_str(&s).unwrap();
        assert_eq!(back.numa.distances[1][0], 12);
        assert_eq!(back.lscpu.cores_per_socket, 64);
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn collect_env_smoke_linux() {
        let env = EnvRecord::collect(std::process::id());
        assert!(!env.host.is_empty(), "host should be non-empty");
        assert!(!env.kernel.is_empty(), "kernel should be non-empty");
        assert!(
            !env.lscpu.model.is_empty(),
            "CPU model should be readable from /proc/cpuinfo"
        );
        assert!(env.ts_ms > 0);
        assert!(
            !env.affinity_inherited.is_empty(),
            "sched_getaffinity should populate affinity_inherited"
        );
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn affinity_range_list_returns_nonempty_on_linux() {
        let s = affinity_range_list();
        assert!(!s.is_empty());
        // Whatever subset is returned, count must match a positive number.
        assert!(count_range_list(&s) > 0);
    }

    #[test]
    fn cpu_topology_returns_some_on_linux_or_none_off() {
        // Non-fatal smoke test: the function shouldn't panic regardless.
        let _ = cpu_topology();
    }
}