gander-core 0.6.4

Platform data collection and process model for gander. No UI dependency.
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
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
//! Reading `/proc` and `/sys`.
//!
//! All hot-path reads go through `thread_local!` scratch buffers so a scan
//! allocates nothing per process (`docs/DESIGN.md` §9.4).

use std::cell::RefCell;
use std::fs;
use std::io;
use std::os::unix::ffi::OsStrExt;
use std::path::{Path, PathBuf};

use crate::linux::parse::GpuUsage;
use crate::row::Row;
use crate::table::{Cache, Stable};
use crate::Pid;

thread_local! {
    static BUF: RefCell<Vec<u8>> = RefCell::new(Vec::with_capacity(4096));
    static PATH: RefCell<Vec<u8>> = RefCell::new(Vec::with_capacity(64));
}

/// Read a whole file into the thread-local buffer, returning its length.
///
/// `ENOENT` is the common case (the process exited between `readdir` and now),
/// so it is reported as `None` rather than as an error.
fn read_into_buf(path: &[u8]) -> Option<usize> {
    let mut file = fs::File::open(std::ffi::OsStr::from_bytes(path)).ok()?;
    BUF.with(|b| {
        let mut b = b.borrow_mut();
        b.clear();
        use std::io::Read as _;
        file.read_to_end(&mut b).ok()
    })
}

/// Run `f` with the contents of `path`, using the shared scratch buffer.
fn with_file<R>(path: &[u8], f: impl FnOnce(&[u8]) -> R) -> Option<R> {
    let n = read_into_buf(path)?;
    BUF.with(|b| {
        let b = b.borrow();
        Some(f(&b[..n]))
    })
}

/// Build `"<root>/<pid>/<suffix>"` into the thread-local path buffer and run `f`
/// with the resulting path.
fn proc_path(root: &Path, pid: Pid, suffix: &str, f: impl FnOnce(&[u8])) {
    PATH.with(|p| {
        let mut p = p.borrow_mut();
        p.clear();
        p.extend_from_slice(root.as_os_str().as_bytes());
        p.push(b'/');
        write_pid(&mut p, pid);
        if !suffix.is_empty() {
            p.push(b'/');
            p.extend_from_slice(suffix.as_bytes());
        }
        f(&p);
    })
}

fn write_pid(out: &mut Vec<u8>, mut pid: Pid) {
    let mut tmp = [0u8; 10];
    let mut i = tmp.len();
    if pid == 0 {
        out.push(b'0');
        return;
    }
    while pid > 0 {
        i -= 1;
        tmp[i] = b'0' + (pid % 10) as u8;
        pid /= 10;
    }
    out.extend_from_slice(&tmp[i..]);
}

/// List process IDs by scanning `/proc` for numeric directory entries.
pub fn list_pids(root: &Path) -> io::Result<Vec<Pid>> {
    let mut out = Vec::with_capacity(512);
    for entry in fs::read_dir(root)? {
        let Ok(entry) = entry else { continue };
        let name = entry.file_name();
        let bytes = name.as_bytes();
        if bytes.is_empty() || !bytes.iter().all(u8::is_ascii_digit) {
            continue;
        }
        if let Ok(pid) = name.to_string_lossy().parse::<Pid>() {
            out.push(pid);
        }
    }
    Ok(out)
}

/// List the thread ids inside a process (`/proc/<pid>/task`).
///
/// A process is its own thread group leader, so the leader's tid equals its pid
/// and appears in this list too; callers filter or keep it deliberately.
pub fn list_tids(root: &Path, pid: Pid) -> io::Result<Vec<Pid>> {
    let mut out = Vec::with_capacity(8);
    let dir = root.join(pid.to_string()).join("task");
    for entry in fs::read_dir(dir)? {
        let Ok(entry) = entry else { continue };
        let name = entry.file_name();
        let bytes = name.as_bytes();
        if bytes.is_empty() || !bytes.iter().all(u8::is_ascii_digit) {
            continue;
        }
        if let Ok(tid) = name.to_string_lossy().parse::<Pid>() {
            out.push(tid);
        }
    }
    Ok(out)
}

/// Read `stat` for one thread of `pid`.
///
/// `/proc/<pid>/task/<tid>/stat` is the same format as a process's `stat`, with
/// the thread's own CPU counters. The identity files (`status`, `cmdline`) are
/// per-process, so a thread row reuses its leader's.
pub fn read_thread_stat(
    root: &Path,
    pid: Pid,
    tid: Pid,
) -> Option<(crate::row::CommBuf, super::parse::StatFields)> {
    let path = root
        .join(pid.to_string())
        .join("task")
        .join(tid.to_string())
        .join("stat");
    let bytes = fs::read(path).ok()?;
    let (_pid, comm, fields) = super::parse::parse_stat(&bytes)?;
    Some((comm, fields))
}

/// Read the `stat` file of one process.
pub fn read_stat(root: &Path, pid: Pid) -> Option<(crate::row::CommBuf, super::parse::StatFields)> {
    let mut slot = None;
    proc_path(root, pid, "stat", |p| {
        slot = with_file(p, super::parse::parse_stat).flatten();
    });
    let (_pid, comm, fields) = slot?;
    Some((comm, fields))
}

/// Join a NUL-separated `cmdline` into a displayable single line.
fn join_argv(b: &[u8]) -> String {
    let mut s = String::with_capacity(b.len());
    let mut first = true;
    for part in b.split(|c| *c == 0) {
        if part.is_empty() {
            continue;
        }
        if !first {
            s.push(' ');
        }
        s.push_str(&String::from_utf8_lossy(part));
        first = false;
    }
    s
}

/// Apply `stat` fields to a row.
pub fn apply_stat(out: &mut Row, fields: &super::parse::StatFields, page_size: u64) {
    super::parse::fill_row(out, fields, page_size);
}

/// Fetch (and cache) the immutable identity of one process.
pub fn read_identity(root: &Path, pid: Pid, starttime: u64, cache: &mut Cache) -> Stable {
    if let Some(hit) = cache.get(pid, starttime) {
        return hit.clone();
    }

    let mut stable = Stable::default();

    proc_path(root, pid, "status", |p| {
        if let Some(uid) = with_file(p, super::parse::parse_status_uid).flatten() {
            stable.uid = uid;
        }
    });

    let mut cmdline = None;
    proc_path(root, pid, "cmdline", |p| {
        cmdline = with_file(p, join_argv).filter(|s| !s.is_empty());
    });
    stable.cmdline = cmdline.map(String::into_boxed_str);

    let mut exe = None;
    let mut exe_deleted = false;
    proc_path(root, pid, "exe", |p| {
        if let Ok(target) = fs::read_link(std::ffi::OsStr::from_bytes(p)) {
            let text = target.to_string_lossy();
            // The kernel appends " (deleted)" to the link target once the file
            // backing a running process is unlinked or replaced. This is the only
            // available signal: an mtime comparison needs the path to still
            // exist, which is precisely what deleting it removes.
            exe_deleted = text.ends_with(" (deleted)");
            let path = text.strip_suffix(" (deleted)").unwrap_or(&text);
            exe = Some(path.to_string().into_boxed_str());
        }
    });
    stable.exe = exe;
    stable.exe_deleted = exe_deleted;

    // The working directory. A `readlink` like `exe`, and readable for the same
    // processes (the link is only resolvable by the owner or CAP_SYS_PTRACE), so a
    // failure is another user's process and yields `None`, which renders `-`.
    proc_path(root, pid, "cwd", |p| {
        if let Ok(target) = fs::read_link(std::ffi::OsStr::from_bytes(p)) {
            stable.cwd = Some(target.to_string_lossy().into_owned().into_boxed_str());
        }
    });

    // Session id is not part of the identity cache on Linux: it arrives in field 4
    // of `stat`, which the scan already parses, so `fill_row` sets it. The `stable`
    // field exists for macOS, where `getsid` is a syscall of its own.

    let mut cgroup = None;
    #[cfg(target_os = "linux")]
    let mut container = None;
    #[cfg(target_os = "linux")]
    let mut container_runtime = None;
    proc_path(root, pid, "cgroup", |p| {
        if let Some(entries) = with_file(p, super::fsops::parse_cgroup) {
            // The path, not the raw line: `12:cpuset:/docker/abc` carries the
            // hierarchy id and controller list, which no column wants.
            cgroup = super::fsops::preferred_path(&entries)
                .map(|s| s.to_string().into_boxed_str());
            #[cfg(target_os = "linux")]
            if let Some(c) = super::fsops::container_from_cgroup(&entries) {
                container = Some(c.id.into_boxed_str());
                container_runtime = Some(c.runtime.to_string().into_boxed_str());
            }
        }
    });
    stable.cgroup = cgroup;

    // A container with its own cgroup namespace reports `/` in
    // `/proc/<pid>/cgroup`, so the path above says nothing. The PID namespace
    // still distinguishes a *child* container, and without this the difference
    // between "not containerised" and "containerised but unidentified" is lost.
    //
    // Only consulted when the path did not already identify a container: the path
    // is the *informative* signal (it names the runtime and the id), the namespace
    // is the *membership* signal, and the path wins when both exist.
    //
    // The id is left as `?` rather than filled with something we made up. A
    // namespace inode is not a container id, and printing one would look like an
    // identifier while telling the reader nothing they can act on.
    #[cfg(target_os = "linux")]
    if container.is_none()
        && matches!(
            super::fsops::in_different_pid_namespace(root, pid),
            Some(true)
        )
    {
        container = Some("?".to_string().into_boxed_str());
        container_runtime = Some("container".to_string().into_boxed_str());
    }

    #[cfg(target_os = "linux")]
    {
        stable.container = container;
        stable.container_runtime = container_runtime;
    }

    stable.is_kernel_thread = stable.cmdline.is_none() && stable.exe.is_none();

    cache.insert(pid, starttime, stable.clone());
    stable
}

/// Read `/proc/meminfo`.
pub fn read_meminfo(
    root: &Path,
    mem: &mut crate::system::MemInfo,
    swap: &mut crate::system::SwapInfo,
) {
    let mut path = PathBuf::from(root);
    path.push("meminfo");
    with_file(path.as_os_str().as_bytes(), |b| {
        super::parse::parse_meminfo(b, mem, swap)
    });
}

/// Read `/proc/stat`.
pub fn read_proc_stat(root: &Path) -> Vec<crate::system::CpuTimes> {
    let mut path = PathBuf::from(root);
    path.push("stat");
    with_file(path.as_os_str().as_bytes(), super::parse::parse_proc_stat).unwrap_or_default()
}

/// Read `/proc/loadavg`.
pub fn read_loadavg(root: &Path) -> Option<[f64; 3]> {
    let mut path = PathBuf::from(root);
    path.push("loadavg");
    with_file(path.as_os_str().as_bytes(), super::parse::parse_loadavg)
}

/// Read `/proc/uptime`, returning whole seconds.
pub fn read_uptime(root: &Path) -> Option<u64> {
    let mut path = PathBuf::from(root);
    path.push("uptime");
    with_file(path.as_os_str().as_bytes(), |b| {
        let s = String::from_utf8_lossy(b);
        let secs = s.split_whitespace().next()?;
        secs.split('.').next()?.parse::<u64>().ok()
    })
    .flatten()
}

/// Whether `/proc/<pid>` still refers to the same process identified by
/// `starttime`. Used to detect PID reuse between two samples.
pub fn is_same_process(root: &Path, pid: Pid, starttime: u64) -> bool {
    read_stat(root, pid)
        .map(|(_, fields)| fields.starttime == starttime)
        .unwrap_or(false)
}

/// Read host-wide network counters from `/proc/net/dev`.
///
/// Missing on a system with no networking at all; that is reported as zeroes
/// rather than as an error, because the meter should show `0 B/s`, not vanish.
pub fn read_net_counters(root: &Path) -> crate::system::NetCounters {
    let mut path = PathBuf::from(root);
    path.push("net");
    path.push("dev");
    with_file(path.as_os_str().as_bytes(), super::parse::parse_net_dev).unwrap_or_default()
}

/// Read host-wide disk counters from `/proc/diskstats`.
///
/// The authoritative "is this a whole disk" test is the presence of
/// `/sys/block/<name>`; it is consulted per device, because `/proc/diskstats`
/// mixes disks and partitions in one file. When `/sys` is not readable (a
/// fixture tree, a container) the name heuristic is used instead.
pub fn read_disk_counters(root: &Path) -> crate::system::DiskCounters {
    // `root` is `/proc`; sysfs lives beside it at `/sys` for the live tree. A
    // fixture root has neither, and the heuristic covers that case.
    let sys_block = root
        .parent()
        .map(|p| p.join("sys/block"))
        .filter(|p| p.is_dir());

    let mut path = PathBuf::from(root);
    path.push("diskstats");
    with_file(path.as_os_str().as_bytes(), |b| {
        super::parse::parse_diskstats(b, |name| match &sys_block {
            Some(dir) => dir
                .join(std::ffi::OsStr::from_bytes(name))
                .is_dir(),
            None => super::parse::looks_like_whole_disk(name),
        })
    })
    .unwrap_or_default()
}

/// Read `/proc/pressure/{cpu,memory,io,irq}`.
///
/// Returns `None` when none of them exist. PSI arrived in Linux 4.20 and is only
/// present when `CONFIG_PSI=y`; on older kernels or a `psi=0` boot parameter the
/// files are absent, and a meter that drew four empty bars would look like an
/// idle machine rather than an unavailable feature.
///
/// The four files are read individually: a kernel can expose `cpu` and `memory`
/// but not `irq` (the per-resource files are gated on the kernel's own feature
/// detection), so one missing file must not discard the other three.
pub fn read_psi(root: &Path) -> Option<crate::system::Psi> {
    let mut psi = crate::system::Psi::default();
    let read = |name: &str| -> Option<crate::system::PsiResource> {
        let path = root.join("pressure").join(name);
        with_file(path.as_os_str().as_bytes(), super::parse::parse_psi)
    };

    if let Some(r) = read("cpu") {
        psi.cpu = r;
    }
    if let Some(r) = read("memory") {
        psi.memory = r;
    }
    if let Some(r) = read("io") {
        psi.io = r;
    }
    if let Some(r) = read("irq") {
        psi.irq = r;
    }

    if psi.any_present() {
        Some(psi)
    } else {
        None
    }
}

/// Read every zram device's `/sys/block/zram<N>/mm_stat`.
///
/// `root` here is `/proc`, and sysfs lives beside it at `/sys`. A fixture tree or
/// a container with a masked `/sys` simply reports no devices.
pub fn read_zram(root: &Path) -> crate::system::ZramInfo {
    let mut out = crate::system::ZramInfo::default();
    let Some(sys) = root.parent().map(|p| p.join("sys/block")) else {
        return out;
    };
    let Ok(entries) = fs::read_dir(&sys) else {
        return out;
    };

    for entry in entries.flatten() {
        let name = entry.file_name();
        let name_bytes = name.as_bytes();
        // `zram0`, `zram1`, … — a device, not some other entry that happens to
        // start with the same letters.
        if !name_bytes.starts_with(b"zram")
            || !name_bytes[b"zram".len()..].iter().all(u8::is_ascii_digit)
            || name_bytes.len() == b"zram".len()
        {
            continue;
        }
        let Ok(name_str) = std::str::from_utf8(name_bytes) else {
            continue;
        };
        let mm_stat = sys.join(&name).join("mm_stat");
        if let Some(device) = with_file(mm_stat.as_os_str().as_bytes(), |b| {
            super::parse::parse_zram_mm_stat(b, name_str)
        })
        .flatten()
        {
            out.devices.push(device);
        }
    }

    out.devices.sort_by(|a, b| a.name.cmp(&b.name));
    out
}

/// Read `/proc/<pid>/fdinfo/*` and sum every DRM descriptor's counters.
///
/// # Cost
///
/// One `readdir` plus one open/read per file descriptor. On a desktop this is a
/// few thousand small reads per sample, which is why it is gated behind
/// `ScanOptions::gpu` — the same treatment `smaps_rollup` gets. A process with no
/// descriptors (kernel threads have no `fd` directory at all) costs one failed
/// `readdir`.
///
/// `None` means "no GPU work visible": either the process holds no DRM fd, or the
/// files are unreadable because it belongs to another user.
pub fn read_gpu_usage(proc_root: &Path, pid: crate::Pid) -> Option<GpuUsage> {
    let fdinfo = proc_root.join(pid.to_string()).join("fdinfo");
    let entries = fs::read_dir(fdinfo).ok()?;

    let mut total: Option<GpuUsage> = None;
    for entry in entries.flatten() {
        let path = entry.path();
        let Ok(bytes) = fs::read(&path) else {
            continue;
        };
        let Some(usage) = super::parse::parse_fdinfo_drm(&bytes) else {
            continue;
        };
        let acc = total.get_or_insert_with(GpuUsage::default);
        acc.gpu_ns = acc.gpu_ns.saturating_add(usage.gpu_ns);
        acc.busy_ns = acc.busy_ns.saturating_add(usage.busy_ns);
        acc.mem_ns = acc.mem_ns.saturating_add(usage.mem_ns);
    }
    total
}

/// Read `max_pool_percent` from `/sys/module/zswap/parameters`.
///
/// Used to give the zswap meter a ceiling. Returns `None` when zswap is not
/// loaded or the parameter is unreadable, in which case the meter scales against
/// the largest value it has seen instead.
pub fn read_zswap_max_percent(root: &Path) -> Option<u64> {
    let sys = root.parent()?.join("sys/module/zswap/parameters");
    with_file(sys.join("max_pool_percent").as_os_str().as_bytes(), |b| {
        let text = String::from_utf8_lossy(b);
        text.trim().parse::<u64>().ok()
    })
    .flatten()
}

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

    #[test]
    fn pid_writing() {
        for pid in [0u32, 1, 9, 10, 42, 1234, 4_194_304] {
            let mut v = Vec::new();
            write_pid(&mut v, pid);
            assert_eq!(String::from_utf8(v).unwrap(), pid.to_string());
        }
    }

    #[test]
    fn pid_path_shape() {
        let mut seen = String::new();
        proc_path(Path::new("/proc"), 42, "stat", |b| {
            seen = String::from_utf8_lossy(b).into_owned();
        });
        assert_eq!(seen, "/proc/42/stat");

        let mut root_only = String::new();
        proc_path(Path::new("/proc"), 1, "", |b| {
            root_only = String::from_utf8_lossy(b).into_owned();
        });
        assert_eq!(root_only, "/proc/1");
    }

    #[test]
    fn argv_joining() {
        assert_eq!(join_argv(b"/bin/sh\0-c\0echo hi\0"), "/bin/sh -c echo hi");
        assert_eq!(join_argv(b""), "");
        assert_eq!(join_argv(b"\0\0"), "");
    }

    #[test]
    fn fixture_scan() {
        let root = Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/fixtures/proc"));
        if !root.exists() {
            return; // fixtures are added in a later step
        }
        let pids = list_pids(root).expect("readable fixture root");
        assert!(!pids.is_empty());
    }
}