sys-lib 0.7.0

System facts for Rust services: OS, CPU, memory, disks, network, host, this process, virtualization, power, and device identity, container-aware by default, plus build info, instance info, and a process guard. No subprocesses, no third-party dependencies by default.
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
//! The `/proc` and cgroup files behind the live readings: `/proc/meminfo`,
//! `/proc/stat`, `/proc/loadavg`, the pressure-stall files, `/proc/diskstats`,
//! `/proc/net/dev`, `/proc/net/sockstat`, `/proc/net/snmp`, `/proc/vmstat`,
//! `/proc/self/statm`, `/proc/self/stat`, `/proc/sys/fs/file-nr`, and the
//! cgroup memory files. Each parser is safe Rust over `&[u8]`, bounded,
//! allocation-free, and never panics.

use core::time::Duration;

use super::{lines, lossy, parse_hex, parse_u64, trim};

/// At most this many lines are read from a line-based file (T7).
const MAX_LINES: usize = 1 << 16;
/// At most this many routes are read.
const MAX_ROUTES: usize = 1 << 16;

/// The fields of `/proc/meminfo` sys-lib uses, in bytes (`kB` there is 1024).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) struct Meminfo {
    pub(crate) total: Option<u64>,
    pub(crate) free: Option<u64>,
    pub(crate) available: Option<u64>,
    pub(crate) buffers: Option<u64>,
    pub(crate) cached: Option<u64>,
    pub(crate) sreclaimable: Option<u64>,
    pub(crate) shmem: Option<u64>,
    pub(crate) swap_total: Option<u64>,
    pub(crate) swap_free: Option<u64>,
    pub(crate) huge_total: Option<u64>,
    pub(crate) huge_free: Option<u64>,
    pub(crate) huge_rsvd: Option<u64>,
    pub(crate) huge_size: Option<u64>,
}

impl Meminfo {
    /// Available memory: `MemAvailable`, or on kernels before 3.14
    /// `MemFree + Buffers + Cached` (E-28).
    pub(crate) fn available(&self) -> Option<u64> {
        self.available.or_else(|| {
            Some(
                self.free?
                    .saturating_add(self.buffers.unwrap_or(0))
                    .saturating_add(self.cached.unwrap_or(0)),
            )
        })
    }

    /// Reclaimable cache: `Cached + SReclaimable - Shmem`.
    pub(crate) fn cache(&self) -> Option<u64> {
        Some(
            self.cached?
                .saturating_add(self.sreclaimable.unwrap_or(0))
                .saturating_sub(self.shmem.unwrap_or(0)),
        )
    }
}

/// `Key:   value [kB]` with the value in bytes when the unit is `kB`.
fn kib_line(line: &[u8]) -> Option<(&[u8], u64)> {
    let colon = line.iter().position(|&b| b == b':')?;
    let (key, rest) = (&line[..colon], trim(&line[colon + 1..]));
    let (number, unit) = match rest.iter().position(|&b| b == b' ') {
        Some(i) => (&rest[..i], trim(&rest[i..])),
        None => (rest, &b""[..]),
    };
    let n = parse_u64(number)?;
    let value = match unit {
        b"kB" => n.checked_mul(1024)?,
        b"" => n,
        _ => return None,
    };
    Some((key, value))
}

/// Parses `/proc/meminfo`.
pub(crate) fn meminfo(data: &[u8]) -> Meminfo {
    let mut m = Meminfo::default();
    for (key, value) in lines(data, 512).filter_map(kib_line) {
        let slot = match key {
            b"MemTotal" => &mut m.total,
            b"MemFree" => &mut m.free,
            b"MemAvailable" => &mut m.available,
            b"Buffers" => &mut m.buffers,
            b"Cached" => &mut m.cached,
            b"SReclaimable" => &mut m.sreclaimable,
            b"Shmem" => &mut m.shmem,
            b"SwapTotal" => &mut m.swap_total,
            b"SwapFree" => &mut m.swap_free,
            b"HugePages_Total" => &mut m.huge_total,
            b"HugePages_Free" => &mut m.huge_free,
            b"HugePages_Rsvd" => &mut m.huge_rsvd,
            b"Hugepagesize" => &mut m.huge_size,
            _ => continue,
        };
        slot.get_or_insert(value);
    }
    m
}

/// One `cpu` line of `/proc/stat`: user, nice, system, idle, iowait, irq,
/// softirq, steal, in ticks (guest time is already in user and nice).
pub(crate) type CpuTicks = [u64; 8];

/// The values of a `cpu` or `cpuN` line, and the CPU number (`None` for the
/// total).
pub(crate) fn stat_cpu_line(line: &[u8]) -> Option<(Option<u32>, CpuTicks)> {
    let rest = line.strip_prefix(b"cpu")?;
    let split = rest.iter().position(|&b| b == b' ')?;
    let id = match &rest[..split] {
        b"" => None,
        digits => Some(u32::try_from(parse_u64(digits)?).ok()?),
    };
    let mut ticks = [0u64; 8];
    let mut fields = rest[split..]
        .split(|&b| b == b' ')
        .filter(|f| !f.is_empty());
    for slot in &mut ticks[..4] {
        *slot = parse_u64(fields.next()?)?;
    }
    // Older kernels stop after idle, iowait, or irq: the rest are zero.
    for slot in &mut ticks[4..] {
        match fields.next() {
            Some(f) => *slot = parse_u64(f)?,
            None => break,
        }
    }
    Some((id, ticks))
}

/// A `key N` line's value in `/proc/stat` (`btime`, `procs_running`).
pub(crate) fn space_value(data: &[u8], key: &[u8]) -> Option<u64> {
    lines(data, MAX_LINES).find_map(|line| {
        let rest = line.strip_prefix(key)?.strip_prefix(b" ")?;
        parse_u64(trim(rest))
    })
}

/// `/proc/loadavg`: `0.52 0.58 0.59 3/1234 56789` as the three averages,
/// runnable entities, and total entities.
pub(crate) fn loadavg(data: &[u8]) -> Option<(f32, f32, f32, u64, u64)> {
    let mut f = trim(data).split(|&b| b == b' ').filter(|f| !f.is_empty());
    let one = decimal(f.next()?)?;
    let five = decimal(f.next()?)?;
    let fifteen = decimal(f.next()?)?;
    let (running, total) = f.next().and_then(|rt| {
        let slash = rt.iter().position(|&b| b == b'/')?;
        Some((parse_u64(&rt[..slash])?, parse_u64(&rt[slash + 1..])?))
    })?;
    Some((one, five, fifteen, running, total))
}

/// A non-negative decimal such as `0.52` or `1234.5`, bounded, as `f32`.
fn decimal(data: &[u8]) -> Option<f32> {
    if data.is_empty() || data.len() > 24 {
        return None;
    }
    let (whole, frac) = match data.iter().position(|&b| b == b'.') {
        Some(i) => (&data[..i], &data[i + 1..]),
        None => (data, &b""[..]),
    };
    let whole = parse_u64(whole)?;
    let mut value = whole as f64;
    let mut scale = 0.1f64;
    for &b in frac.iter().take(9) {
        let digit = b.checked_sub(b'0').filter(|d| *d <= 9)?;
        value += f64::from(digit) * scale;
        scale /= 10.0;
    }
    if frac.iter().skip(9).any(|b| !b.is_ascii_digit()) {
        return None;
    }
    Some(value as f32)
}

/// One line of a pressure-stall file: `some avg10=1.53 avg60=0.89 avg300=0.40 total=12345`.
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct PsiLine {
    pub(crate) avg10: f32,
    pub(crate) avg60: f32,
    pub(crate) avg300: f32,
    pub(crate) total: Duration,
}

/// The `some` and `full` lines of a pressure-stall file.
pub(crate) fn psi(data: &[u8]) -> (Option<PsiLine>, Option<PsiLine>) {
    let mut some = None;
    let mut full = None;
    for line in lines(data, 8) {
        let (slot, rest) = if let Some(r) = line.strip_prefix(b"some ") {
            (&mut some, r)
        } else if let Some(r) = line.strip_prefix(b"full ") {
            (&mut full, r)
        } else {
            continue;
        };
        *slot = psi_fields(rest);
    }
    (some, full)
}

fn psi_fields(rest: &[u8]) -> Option<PsiLine> {
    let (mut a10, mut a60, mut a300, mut total) = (None, None, None, None);
    for field in rest.split(|&b| b == b' ').filter(|f| !f.is_empty()) {
        let eq = field.iter().position(|&b| b == b'=')?;
        let (key, value) = (&field[..eq], &field[eq + 1..]);
        match key {
            b"avg10" => a10 = decimal(value),
            b"avg60" => a60 = decimal(value),
            b"avg300" => a300 = decimal(value),
            // Microseconds.
            b"total" => total = parse_u64(value).map(Duration::from_micros),
            _ => {}
        }
    }
    Some(PsiLine {
        avg10: a10?,
        avg60: a60?,
        avg300: a300?,
        total: total?,
    })
}

/// One line of `/proc/diskstats`: the device name and the counters sys-lib
/// uses (fields 4 to 14).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct DiskLine<'a> {
    pub(crate) name: &'a [u8],
    pub(crate) reads: u64,
    pub(crate) sectors_read: u64,
    pub(crate) read_ms: u64,
    pub(crate) writes: u64,
    pub(crate) sectors_written: u64,
    pub(crate) write_ms: u64,
    pub(crate) io_ms: u64,
}

/// Parses one `/proc/diskstats` line.
pub(crate) fn diskstats_line(line: &[u8]) -> Option<DiskLine<'_>> {
    let mut f = line.split(|&b| b == b' ').filter(|f| !f.is_empty());
    let _major = f.next()?;
    let _minor = f.next()?;
    let name = f.next()?;
    let mut n = [0u64; 10];
    for slot in &mut n {
        *slot = parse_u64(f.next()?)?;
    }
    // reads, reads merged, sectors read, ms reading, writes, writes merged,
    // sectors written, ms writing, in flight, ms doing I/O.
    Some(DiskLine {
        name,
        reads: n[0],
        sectors_read: n[2],
        read_ms: n[3],
        writes: n[4],
        sectors_written: n[6],
        write_ms: n[7],
        io_ms: n[9],
    })
}

/// One interface line of `/proc/net/dev`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct NetDevLine<'a> {
    pub(crate) name: &'a [u8],
    pub(crate) rx_bytes: u64,
    pub(crate) rx_packets: u64,
    pub(crate) rx_errors: u64,
    pub(crate) rx_dropped: u64,
    pub(crate) tx_bytes: u64,
    pub(crate) tx_packets: u64,
    pub(crate) tx_errors: u64,
    pub(crate) tx_dropped: u64,
}

/// Parses one `/proc/net/dev` line (`  eth0: 1234 5 0 0 0 0 0 0 6789 ...`);
/// the two header lines give `None`.
pub(crate) fn net_dev_line(line: &[u8]) -> Option<NetDevLine<'_>> {
    let colon = line.iter().position(|&b| b == b':')?;
    let name = trim(&line[..colon]);
    if name.is_empty() || name.contains(&b'|') {
        return None;
    }
    let mut f = line[colon + 1..]
        .split(|&b| b == b' ')
        .filter(|f| !f.is_empty());
    let mut n = [0u64; 16];
    for slot in &mut n {
        *slot = parse_u64(f.next()?)?;
    }
    Some(NetDevLine {
        name,
        rx_bytes: n[0],
        rx_packets: n[1],
        rx_errors: n[2],
        rx_dropped: n[3],
        tx_bytes: n[8],
        tx_packets: n[9],
        tx_errors: n[10],
        tx_dropped: n[11],
    })
}

/// A value in `/proc/net/sockstat`: `TCP: inuse 5 orphan 0 tw 2 ...` gives
/// `sockstat(data, b"TCP", b"tw") == Some(2)`.
pub(crate) fn sockstat(data: &[u8], protocol: &[u8], key: &[u8]) -> Option<u64> {
    lines(data, 32).find_map(|line| {
        let rest = line.strip_prefix(protocol)?.strip_prefix(b":")?;
        let mut f = rest.split(|&b| b == b' ').filter(|f| !f.is_empty());
        while let Some(k) = f.next() {
            let v = f.next()?;
            if k == key {
                return parse_u64(v);
            }
        }
        None
    })
}

/// A value in `/proc/net/snmp`, whose protocols have a header line of names
/// and a line of values: `snmp(data, b"Tcp", b"CurrEstab")`.
pub(crate) fn snmp(data: &[u8], protocol: &[u8], key: &[u8]) -> Option<u64> {
    let mut rows = lines(data, 64).filter_map(|l| {
        let rest = l.strip_prefix(protocol)?.strip_prefix(b":")?;
        Some(rest)
    });
    let names = rows.next()?;
    let values = rows.next()?;
    let index = names
        .split(|&b| b == b' ')
        .filter(|f| !f.is_empty())
        .position(|n| n == key)?;
    let value = values
        .split(|&b| b == b' ')
        .filter(|f| !f.is_empty())
        .nth(index)?;
    // CurrEstab is a gauge; some counters are signed (`MaxConn -1`).
    parse_u64(value)
}

/// `/proc/self/statm`: the size and resident fields, in pages.
pub(crate) fn statm(data: &[u8]) -> Option<(u64, u64)> {
    let mut f = trim(data).split(|&b| b == b' ').filter(|f| !f.is_empty());
    Some((parse_u64(f.next()?)?, parse_u64(f.next()?)?))
}

/// Field 22 of `/proc/self/stat` (start time in ticks since boot). The command
/// name in field 2 is in parentheses and may contain spaces and parentheses,
/// so the fields are counted from the last `)`.
pub(crate) fn stat_start_ticks(data: &[u8]) -> Option<u64> {
    let close = data.iter().rposition(|&b| b == b')')?;
    // After ")": field 3 (state) is the first.
    let field = trim(&data[close + 1..])
        .split(|&b| b == b' ')
        .filter(|f| !f.is_empty())
        .nth(22 - 3)?;
    parse_u64(field)
}

/// `/proc/sys/fs/file-nr`: open, unused, and maximum file handles.
pub(crate) fn file_nr(data: &[u8]) -> Option<(u64, u64)> {
    let mut f = trim(data)
        .split(|&b| b == b'\t' || b == b' ')
        .filter(|f| !f.is_empty());
    let open = parse_u64(f.next()?)?;
    let _unused = f.next()?;
    let max = parse_u64(f.next()?)?;
    Some((open, max))
}

/// `/proc/uptime`'s first field, seconds since boot.
pub(crate) fn uptime(data: &[u8]) -> Option<Duration> {
    let first = trim(data).split(|&b| b == b' ').next()?;
    let secs = decimal(first)?;
    // A value past what a Duration holds is junk, not a panic (T6).
    Duration::try_from_secs_f64(f64::from(secs)).ok()
}

/// A cgroup memory value: a number of bytes, or `max` (no limit: `None`).
pub(crate) fn cgroup_bytes(data: &[u8]) -> Option<Option<u64>> {
    match trim(data) {
        b"max" => Some(None),
        v => parse_u64(v).map(Some),
    }
}

/// A transparent-huge-page mode file: `always [madvise] never`, the bracketed
/// word.
pub(crate) fn bracketed(data: &[u8]) -> Option<&[u8]> {
    let open = data.iter().position(|&b| b == b'[')?;
    let close = data[open..].iter().position(|&b| b == b']')? + open;
    Some(&data[open + 1..close])
}

/// The interface of the default route in `/proc/net/route`: destination and
/// mask zero, the `RTF_UP` flag set, the lowest metric.
pub(crate) fn default_route_v4(data: &[u8]) -> Option<String> {
    let mut best: Option<(u64, &[u8])> = None;
    for line in lines(data, MAX_ROUTES).skip(1) {
        let mut w = line
            .split(|b| b.is_ascii_whitespace())
            .filter(|w| !w.is_empty());
        let (Some(name), Some(destination), Some(_gateway), Some(flags)) =
            (w.next(), w.next(), w.next(), w.next())
        else {
            continue;
        };
        let Some(metric) = w.nth(2).and_then(parse_u64) else {
            continue;
        };
        let mask = w.next();
        let up = parse_hex(flags).is_some_and(|f| f & 1 != 0);
        if up
            && destination == b"00000000"
            && mask == Some(b"00000000")
            && best.is_none_or(|(m, _)| metric < m)
        {
            best = Some((metric, name));
        }
    }
    best.map(|(_, name)| lossy(name, 64))
}

/// The interface of the default route in `/proc/net/ipv6_route`: destination
/// `::/0`, up, not the kernel's unreachable entry on `lo`.
pub(crate) fn default_route_v6(data: &[u8]) -> Option<String> {
    let mut best: Option<(u64, &[u8])> = None;
    for line in lines(data, MAX_ROUTES) {
        let mut w = line
            .split(|b| b.is_ascii_whitespace())
            .filter(|w| !w.is_empty());
        let (Some(destination), Some(prefix)) = (w.next(), w.next()) else {
            continue;
        };
        // Source, its length, the next hop, then the metric.
        let Some(metric) = w.nth(3).and_then(parse_hex) else {
            continue;
        };
        // Reference count, use count, then the flags and the device.
        let (Some(flags), Some(name)) = (w.nth(2), w.next()) else {
            continue;
        };
        if destination.iter().all(|&b| b == b'0')
            && prefix == b"00"
            && name != b"lo"
            && parse_hex(flags).is_some_and(|f| f & 1 != 0)
            && best.is_none_or(|(m, _)| metric < m)
        {
            best = Some((metric, name));
        }
    }
    best.map(|(_, name)| lossy(name, 64))
}

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

    #[test]
    fn test_procfs_default_route_v4() {
        let data =
            b"Iface\tDestination\tGateway\tFlags\tRefCnt\tUse\tMetric\tMask\tMTU\tWindow\tIRTT\n\
            eth1\t00000000\t0101A8C0\t0003\t0\t0\t600\t00000000\t0\t0\t0\n\
            eth0\t00000000\t0100A8C0\t0003\t0\t0\t100\t00000000\t0\t0\t0\n\
            down0\t00000000\t0100A8C0\t0002\t0\t0\t1\t00000000\t0\t0\t0\n\
            eth2\t0000A8C0\t00000000\t0001\t0\t0\t0\t00FFFFFF\t0\t0\t0\n";
        assert_eq!(default_route_v4(data).as_deref(), Some("eth0"));
        assert_eq!(default_route_v4(b"Iface Destination\n"), None);
        assert_eq!(default_route_v4(b""), None);
    }

    #[test]
    fn test_procfs_default_route_v6() {
        let zeros = "00000000000000000000000000000000";
        let data = format!(
            "{zeros} 00 {zeros} 00 {zeros} ffffffff 00000001 00000000 00200200 lo\n\
             {zeros} 00 {zeros} 00 fe800000000000000000000000000001 00000400 00000001 00000000 00000003 wlan0\n\
             fe800000000000000000000000000000 40 {zeros} 00 {zeros} 00000100 00000001 00000000 00000001 wlan0\n"
        );
        assert_eq!(default_route_v6(data.as_bytes()).as_deref(), Some("wlan0"));
        assert_eq!(default_route_v6(b"junk\n"), None);
    }

    const MEMINFO: &[u8] = b"MemTotal:       32737560 kB\nMemFree:         1234560 kB\n\
        MemAvailable:   20000000 kB\nBuffers:          100000 kB\nCached:          9000000 kB\n\
        SwapCached:            0 kB\nShmem:            500000 kB\nSReclaimable:     400000 kB\n\
        SwapTotal:       8388604 kB\nSwapFree:        8000000 kB\nHugePages_Total:       4\n\
        HugePages_Free:        3\nHugePages_Rsvd:        1\nHugepagesize:       2048 kB\n";

    #[test]
    fn test_procfs_meminfo_fields_in_bytes() {
        let m = meminfo(MEMINFO);
        assert_eq!(m.total, Some(32_737_560 * 1024));
        assert_eq!(m.available(), Some(20_000_000 * 1024));
        assert_eq!(m.cache(), Some((9_000_000 + 400_000 - 500_000) * 1024));
        assert_eq!(
            (m.huge_total, m.huge_free, m.huge_rsvd),
            (Some(4), Some(3), Some(1))
        );
        assert_eq!(m.huge_size, Some(2 << 20));
        assert_eq!(m.swap_total, Some(8_388_604 * 1024));
    }

    #[test]
    fn test_procfs_meminfo_old_kernel_and_junk() {
        // E-28: no MemAvailable.
        let m = meminfo(b"MemTotal: 1000 kB\nMemFree: 100 kB\nBuffers: 10 kB\nCached: 50 kB\n");
        assert_eq!(m.available(), Some(160 * 1024));
        // Overflowing, negative, and unit-less junk is ignored, never a panic.
        let m = meminfo(
            b"MemTotal: 99999999999999999999 kB\nMemFree: -1 kB\nCached: 5 MB\nMemTotal: 7 kB\n",
        );
        assert_eq!((m.total, m.free, m.cached), (Some(7 * 1024), None, None));
    }

    #[test]
    fn test_procfs_stat_cpu_lines() {
        let (id, t) = stat_cpu_line(b"cpu  10 2 30 400 5 6 7 8 9 10").unwrap();
        assert_eq!((id, t), (None, [10, 2, 30, 400, 5, 6, 7, 8]));
        let (id, t) = stat_cpu_line(b"cpu17 1 2 3 4").unwrap();
        assert_eq!((id, t), (Some(17), [1, 2, 3, 4, 0, 0, 0, 0]));
        assert_eq!(stat_cpu_line(b"cpu 1 2 3"), None);
        assert_eq!(stat_cpu_line(b"cpux 1 2 3 4"), None);
        assert_eq!(stat_cpu_line(b"intr 1 2 3 4"), None);
        let stat = b"cpu  1 2 3 4\nbtime 1700000000\nprocs_running 3\n";
        assert_eq!(space_value(stat, b"btime"), Some(1_700_000_000));
        assert_eq!(space_value(stat, b"procs_running"), Some(3));
        assert_eq!(space_value(stat, b"procs_blocked"), None);
    }

    #[test]
    fn test_procfs_loadavg_and_decimal() {
        let (a, b, c, r, t) = loadavg(b"0.52 1.58 12.59 3/1234 56789\n").unwrap();
        assert!((a - 0.52).abs() < 1e-6 && (b - 1.58).abs() < 1e-6 && (c - 12.59).abs() < 1e-5);
        assert_eq!((r, t), (3, 1234));
        assert_eq!(loadavg(b"0.52 0.58"), None);
        assert_eq!(decimal(b"-1.0"), None);
        assert_eq!(decimal(b"1.2.3"), None);
        assert_eq!(decimal(b"7"), Some(7.0));
    }

    #[test]
    fn test_procfs_psi_some_and_full() {
        let (some, full) = psi(b"some avg10=1.53 avg60=0.89 avg300=0.40 total=12345\nfull avg10=0.00 avg60=0.00 avg300=0.00 total=0\n");
        let some = some.unwrap();
        assert!((some.avg10 - 1.53).abs() < 1e-6);
        assert_eq!(some.total, Duration::from_micros(12_345));
        assert_eq!(full.unwrap().avg60, 0.0);
        assert_eq!(psi(b"some avg10=1.0 avg60=2.0\n"), (None, None));
        assert_eq!(psi(b""), (None, None));
    }

    #[test]
    fn test_procfs_diskstats_line() {
        let l = diskstats_line(
            b" 259       0 nvme0n1 1000 5 20000 300 2000 10 40000 600 0 700 900 0 0 0 0",
        )
        .unwrap();
        assert_eq!(l.name, b"nvme0n1");
        assert_eq!((l.reads, l.sectors_read, l.read_ms), (1000, 20000, 300));
        assert_eq!(
            (l.writes, l.sectors_written, l.write_ms, l.io_ms),
            (2000, 40000, 600, 700)
        );
        assert_eq!(diskstats_line(b"8 0 sda 1 2 3"), None);
    }

    #[test]
    fn test_procfs_net_dev_line() {
        let l = net_dev_line(b"  eth0: 1234 5 1 2 0 0 0 0 6789 10 3 4 0 0 0 0").unwrap();
        assert_eq!(l.name, b"eth0");
        assert_eq!(
            (l.rx_bytes, l.rx_packets, l.rx_errors, l.rx_dropped),
            (1234, 5, 1, 2)
        );
        assert_eq!(
            (l.tx_bytes, l.tx_packets, l.tx_errors, l.tx_dropped),
            (6789, 10, 3, 4)
        );
        assert_eq!(
            net_dev_line(
                b"Inter-|   Receive                                                |  Transmit"
            ),
            None
        );
        assert_eq!(net_dev_line(b" face |bytes    packets errs drop"), None);
    }

    #[test]
    fn test_procfs_sockstat_and_snmp() {
        let s =
            b"sockets: used 300\nTCP: inuse 5 orphan 0 tw 2 alloc 7 mem 1\nUDP: inuse 3 mem 2\n";
        assert_eq!(sockstat(s, b"TCP", b"inuse"), Some(5));
        assert_eq!(sockstat(s, b"TCP", b"tw"), Some(2));
        assert_eq!(sockstat(s, b"UDP", b"inuse"), Some(3));
        assert_eq!(sockstat(s, b"TCP6", b"inuse"), None);
        let snmp_data = b"Ip: Forwarding DefaultTTL\nIp: 1 64\nTcp: RtoAlgorithm RtoMin MaxConn CurrEstab\nTcp: 1 200 -1 42\n";
        assert_eq!(snmp(snmp_data, b"Tcp", b"CurrEstab"), Some(42));
        assert_eq!(snmp(snmp_data, b"Tcp", b"MaxConn"), None);
        assert_eq!(snmp(snmp_data, b"Udp", b"InDatagrams"), None);
    }

    #[test]
    fn test_procfs_small_files() {
        assert_eq!(statm(b"5000 1200 300 10 0 900 0\n"), Some((5000, 1200)));
        let stat = b"4821 (my (odd) prog) S 1 4821 4821 0 -1 4194560 300 0 0 0 5 3 0 0 20 0 1 0 98765 1000000 250";
        assert_eq!(stat_start_ticks(stat), Some(98_765));
        assert_eq!(stat_start_ticks(b"1 (x) S 0"), None);
        assert_eq!(
            file_nr(b"7424\t0\t9223372036854775807\n"),
            Some((7424, 9_223_372_036_854_775_807))
        );
        assert_eq!(
            uptime(b"12345.67 400000.00\n"),
            Some(Duration::from_secs_f64(f64::from(12345.67f32)))
        );
        assert_eq!(
            uptime(b"18446744073709551615 0"),
            None,
            "past Duration::MAX"
        );
        assert_eq!(cgroup_bytes(b"max\n"), Some(None));
        assert_eq!(cgroup_bytes(b"268435456\n"), Some(Some(268_435_456)));
        assert_eq!(cgroup_bytes(b"junk"), None);
        assert_eq!(
            bracketed(b"always [madvise] never\n"),
            Some(&b"madvise"[..])
        );
        assert_eq!(bracketed(b"always madvise never"), None);
    }

    #[test]
    fn test_procfs_parsers_never_panic_on_odd_input() {
        for input in [
            &b""[..],
            b"\xff\xfe",
            b":",
            b"cpu ",
            b"(",
            b")",
            b"[",
            b"]",
            b"=",
            b"some =",
            b"a: b: c",
            b"\n\n\n",
        ] {
            let _ = meminfo(input);
            let _ = stat_cpu_line(input);
            let _ = space_value(input, b"btime");
            let _ = loadavg(input);
            let _ = psi(input);
            let _ = diskstats_line(input);
            let _ = net_dev_line(input);
            let _ = sockstat(input, b"TCP", b"inuse");
            let _ = snmp(input, b"Tcp", b"CurrEstab");
            let _ = statm(input);
            let _ = stat_start_ticks(input);
            let _ = file_nr(input);
            let _ = uptime(input);
            let _ = cgroup_bytes(input);
            let _ = bracketed(input);
        }
    }
}