sys-lib 0.5.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, zero third-party dependencies.
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
//! Disks and filesystems: free space, the mounted filesystems, and how hard the
//! disks are working.
//!
//! ```
//! use sys_lib as sys;
//!
//! let s = sys::disk::space(".")?;
//! println!("{} available of {} ({} used)", s.available, s.total, s.used_percent());
//! # Ok::<(), sys_lib::Error>(())
//! ```

use core::time::Duration;
use std::{
    path::{Path, PathBuf},
    time::Instant,
};

use crate::{Bytes, Error, Percent, Pressure, Rate, Result};

/// The space on one filesystem.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[non_exhaustive]
pub struct Space {
    /// The filesystem's size.
    pub total: Bytes,
    /// Every free block, the root reserve included.
    pub free: Bytes,
    /// What an unprivileged user can use (the root reserve excluded).
    pub available: Bytes,
}

impl Space {
    /// A reading with its invariants applied: `available <= free <= total`.
    #[cfg_attr(not(any(target_os = "linux", target_os = "android")), allow(dead_code))] // the platforms that sum or build these
    pub(crate) fn new(total: u64, free: u64, available: u64) -> Space {
        let free = free.min(total);
        Space {
            total: Bytes::new(total),
            free: Bytes::new(free),
            available: Bytes::new(available.min(free)),
        }
    }

    /// Space in use: `total - free`.
    #[must_use]
    pub fn used(&self) -> Bytes {
        self.total.saturating_sub(self.free)
    }

    /// [`used`](Space::used) as a share of `total`.
    #[must_use]
    pub fn used_percent(&self) -> Percent {
        self.used().percent_of(self.total)
    }
}

/// The space on the filesystem holding `path`: one system call, no
/// allocation.
///
/// This blocks if `path` is on a dead network mount: pointing it there is the
/// caller's choice.
///
/// ```
/// use sys_lib as sys;
///
/// let s = sys::disk::space(".")?;
/// if s.available < sys::Bytes::from_gib(10) { /* refuse compaction */ }
/// # Ok::<(), sys_lib::Error>(())
/// ```
///
/// # Errors
///
/// `NotFound` when `path` does not exist; `PermissionDenied`; `Os` when the
/// call fails.
pub fn space(path: impl AsRef<Path>) -> Result<Space> {
    crate::sys::disk_space(path.as_ref())
}

/// One mounted filesystem.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Disk {
    /// Where it is mounted.
    pub mount_point: PathBuf,
    /// The device: `/dev/nvme0n1p2`, `C:`, `\\?\Volume{...}\`.
    pub device: String,
    /// The volume label (Windows, macOS).
    pub label: Option<String>,
    /// The filesystem type: `ext4`, `btrfs`, `apfs`, `NTFS`, `overlay`.
    pub filesystem: String,
    /// What kind of storage it is.
    pub kind: DiskKind,
    /// The space, or `None` for media not present (an empty card reader) and
    /// network mounts.
    pub space: Option<Space>,
    /// Mounted read-only.
    pub read_only: bool,
    /// On removable media.
    pub removable: bool,
}

/// What kind of storage a [`Disk`] is on. Telling solid-state from rotating
/// disks arrives in 0.5 (DSK-04); until then a fixed local disk is `Unknown`.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[non_exhaustive]
pub enum DiskKind {
    /// Solid-state.
    Ssd,
    /// Rotating.
    Hdd,
    /// Removable media (USB sticks, card readers).
    Removable,
    /// A network filesystem.
    Network,
    /// Optical media.
    Optical,
    /// Memory-backed (`tmpfs`, a RAM disk).
    Ram,
    /// A virtual disk or filesystem (an overlay root in a container).
    Virtual,
    /// Not known.
    Unknown,
}

/// Which filesystems [`list_with`] includes.
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub struct ListOptions {
    /// Include network filesystems (default false: `statvfs` on a dead NFS
    /// mount can hang).
    pub include_network: bool,
    /// Include pseudo filesystems: `proc`, `sysfs`, `cgroup`, `tmpfs`,
    /// `devtmpfs`, `squashfs`, ... (default false).
    pub include_pseudo: bool,
    /// Include bind mounts and further mounts of one filesystem (default
    /// false: each filesystem once, by device number).
    pub include_duplicates: bool,
    /// The most mounts read; more is `TooLarge` (default 4 096).
    pub max_mounts: usize,
}

impl Default for ListOptions {
    fn default() -> Self {
        ListOptions {
            include_network: false,
            include_pseudo: false,
            include_duplicates: false,
            max_mounts: 4_096,
        }
    }
}

/// The local, real filesystems, sorted by mount point, each once.
///
/// Never blocks on a dead network mount (network filesystems are left out),
/// and on Windows never pops up "insert a disk" for an empty drive (threat
/// T8). Inside a container it shows the container's mounts.
///
/// ```
/// for d in sys_lib::disk::list()? {
///     println!("{:<20} {:<8} {:?} {:?}", d.mount_point.display(), d.filesystem, d.kind, d.space.map(|s| s.available));
/// }
/// # Ok::<(), sys_lib::Error>(())
/// ```
///
/// # Errors
///
/// `TooLarge` with more than 4 096 mounts; `NotFound` when the mount table is
/// hidden.
pub fn list() -> Result<Vec<Disk>> {
    list_with(ListOptions::default())
}

/// The filesystems `options` selects, sorted by mount point.
///
/// ```
/// use sys_lib::disk::{self, ListOptions};
///
/// let mut options = ListOptions::default();
/// options.include_network = true;      // accept the risk of a hang on a dead mount
/// let all = disk::list_with(options)?;
/// # Ok::<(), sys_lib::Error>(())
/// ```
///
/// # Errors
///
/// As [`list`], with `options.max_mounts` as the budget.
pub fn list_with(options: ListOptions) -> Result<Vec<Disk>> {
    let mut disks = crate::sys::disks(&options)?;
    disks.sort_by(|a, b| a.mount_point.cmp(&b.mount_point));
    Ok(disks)
}

/// The mount holding `path`. Only that filesystem is asked for its space, so a
/// dead network mount elsewhere does not matter; `path` on one blocks.
///
/// ```
/// let d = sys_lib::disk::of(".")?;
/// println!("{} on {} ({:?})", d.filesystem, d.mount_point.display(), d.kind);
/// # Ok::<(), sys_lib::Error>(())
/// ```
///
/// # Errors
///
/// `NotFound` when `path` does not exist or the mount table is hidden;
/// `PermissionDenied` when it cannot be reached.
pub fn of(path: impl AsRef<Path>) -> Result<Disk> {
    crate::sys::disk_of(path.as_ref())
}

/// The interval [`io`] measures over: 1 s.
pub const DEFAULT_WINDOW: Duration = Duration::from_secs(1);

const MIN_WINDOW: Duration = Duration::from_millis(100);
const MAX_WINDOW: Duration = Duration::from_secs(60);

/// How hard the disks worked over an interval.
#[derive(Clone, Copy, PartialEq, Debug)]
#[non_exhaustive]
pub struct DiskIo {
    /// Read speed.
    pub read: Rate,
    /// Write speed.
    pub write: Rate,
    /// Read operations per second.
    pub reads_per_sec: f64,
    /// Write operations per second.
    pub writes_per_sec: f64,
    /// The share of the interval with I/O in flight, when reported.
    pub busy: Option<Percent>,
    /// The time per completed operation, when reported.
    pub average_latency: Option<Duration>,
    /// The time the values cover.
    pub interval: Duration,
}

/// Cumulative I/O counters since boot.
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
#[non_exhaustive]
pub struct IoTotals {
    /// Bytes read.
    pub read: Bytes,
    /// Bytes written.
    pub written: Bytes,
    /// Read operations completed.
    pub reads: u64,
    /// Write operations completed.
    pub writes: u64,
    /// Time with I/O in flight, when reported.
    pub io_time: Option<Duration>,
}

/// One disk's rates.
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct DeviceIo {
    /// The disk: `nvme0n1`, `disk0`, `PhysicalDrive0`.
    pub device: String,
    /// Its rates.
    pub io: DiskIo,
}

/// One disk's cumulative counters.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct IoCounters {
    /// The disk: `nvme0n1`, `disk0`, `PhysicalDrive0`.
    pub device: String,
    /// Its counters.
    pub totals: IoTotals,
}

/// Cumulative counters as a platform reports them.
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub(crate) struct DiskRaw {
    pub(crate) read: u64,
    pub(crate) written: u64,
    pub(crate) reads: u64,
    pub(crate) writes: u64,
    /// Time with I/O in flight.
    pub(crate) busy: Option<Duration>,
    /// Time spent on completed operations (read plus write).
    pub(crate) op_time: Option<Duration>,
    /// How many disks the counters cover (1 for one disk), so a sum's busy
    /// time becomes the mean share across disks.
    pub(crate) disks: u32,
}

impl DiskRaw {
    #[cfg_attr(not(any(target_os = "linux", target_os = "android")), allow(dead_code))] // the platforms that sum or build these
    pub(crate) fn add(&mut self, other: &DiskRaw) {
        self.read = self.read.saturating_add(other.read);
        self.written = self.written.saturating_add(other.written);
        self.reads = self.reads.saturating_add(other.reads);
        self.writes = self.writes.saturating_add(other.writes);
        self.busy = add_opt(self.busy, other.busy);
        self.op_time = add_opt(self.op_time, other.op_time);
        self.disks = self.disks.saturating_add(other.disks);
    }

    fn totals(&self) -> IoTotals {
        IoTotals {
            read: Bytes::new(self.read),
            written: Bytes::new(self.written),
            reads: self.reads,
            writes: self.writes,
            io_time: self.busy,
        }
    }

    /// The rates between `earlier` and `self`. A counter that went backwards
    /// (a disk removed and re-attached) counts as no activity: the caller
    /// re-bases.
    fn rates_since(&self, earlier: &DiskRaw, interval: Duration) -> DiskIo {
        let d = |new: u64, old: u64| new.saturating_sub(old);
        let secs = interval.as_secs_f64();
        let ops = d(self.reads, earlier.reads).saturating_add(d(self.writes, earlier.writes));
        let per_sec = |n: u64| if secs > 0.0 { n as f64 / secs } else { 0.0 };
        DiskIo {
            read: Rate::from_delta(d(self.read, earlier.read), interval),
            write: Rate::from_delta(d(self.written, earlier.written), interval),
            reads_per_sec: per_sec(d(self.reads, earlier.reads)),
            writes_per_sec: per_sec(d(self.writes, earlier.writes)),
            // Several disks: the mean share of the interval each was busy.
            busy: match (self.busy, earlier.busy) {
                (Some(n), Some(o)) if !interval.is_zero() => Some(Percent::from_ratio_u128(
                    n.checked_sub(o).unwrap_or_default().as_nanos(),
                    interval.as_nanos() * u128::from(self.disks.max(1)),
                )),
                _ => None,
            },
            average_latency: match (self.op_time, earlier.op_time) {
                (Some(n), Some(o)) if ops > 0 => {
                    let spent = n.checked_sub(o).unwrap_or_default();
                    Some(spent / u32::try_from(ops).unwrap_or(u32::MAX))
                }
                _ => None,
            },
            interval,
        }
    }
}

#[cfg_attr(not(any(target_os = "linux", target_os = "android")), allow(dead_code))]
fn add_opt(a: Option<Duration>, b: Option<Duration>) -> Option<Duration> {
    match (a, b) {
        (Some(a), Some(b)) => Some(a.saturating_add(b)),
        (a, None) => a,
        (None, b) => b,
    }
}

/// Disk activity over the next second, every physical disk together.
/// **Blocks for [`DEFAULT_WINDOW`]**; a monitor keeps an [`IoSampler`].
///
/// Only physical disks are counted (partitions, device-mapper and RAID
/// members, loop, RAM, and zram devices are left out), so nothing is counted
/// twice. Inside a container the disks are the host's; this process's own I/O
/// is [`process::io`](crate::process::io).
///
/// ```
/// let io = sys_lib::disk::io()?;            // blocks 1 s
/// println!("read {} write {} ({:.0} IOPS, busy {:?})", io.read, io.write, io.reads_per_sec + io.writes_per_sec, io.busy);
/// # Ok::<(), sys_lib::Error>(())
/// ```
///
/// # Errors
///
/// As [`IoSampler::sample`].
pub fn io() -> Result<DiskIo> {
    io_over(DEFAULT_WINDOW)
}

/// Disk activity over `window`, clamped to 100 ms ..= 60 s. **Blocks for the
/// window.**
///
/// # Errors
///
/// As [`IoSampler::sample`].
pub fn io_over(window: Duration) -> Result<DiskIo> {
    let mut sampler = IoSampler::totals_only()?;
    std::thread::sleep(window.clamp(MIN_WINDOW, MAX_WINDOW));
    sampler.sample()
}

/// Bytes and operations since boot, every physical disk together: one read,
/// no waiting, no allocation.
///
/// # Errors
///
/// `NotFound` when the counters are hidden; `Malformed`; `Unsupported` where
/// the OS gives no disk counters to an unprivileged process.
pub fn io_totals() -> Result<IoTotals> {
    crate::sys::disk_io_totals().map(|r| r.totals())
}

/// The cumulative counters of each physical disk (Tier 2).
///
/// # Errors
///
/// As [`io_totals`].
pub fn io_counters() -> Result<Vec<IoCounters>> {
    Ok(crate::sys::disk_io_counters()?
        .into_iter()
        .map(|(device, raw)| IoCounters {
            device,
            totals: raw.totals(),
        })
        .collect())
}

/// How much time tasks were stalled on I/O ([`Pressure`]). Linux: the cgroup's
/// `io.pressure`, else `/proc/pressure/io`. macOS and Windows: `None`.
///
/// ```
/// use sys_lib::{self as sys, PressureLevel};
///
/// if let Some(p) = sys::disk::pressure()? {
///     if p.level >= PressureLevel::Warning { /* postpone compaction */ }
/// }
/// # Ok::<(), sys_lib::Error>(())
/// ```
///
/// # Errors
///
/// `Malformed` when the kernel's file does not parse.
pub fn pressure() -> Result<Option<Pressure>> {
    crate::sys::disk_pressure()
}

/// Measures disk activity between samples. Owned by the caller. `sample`
/// never blocks and allocates nothing.
///
/// ```no_run
/// use std::time::Duration;
///
/// let mut disks = sys_lib::disk::IoSampler::new()?;
/// std::thread::sleep(Duration::from_secs(5));
/// let io = disks.sample()?;
/// println!("read {} write {}", io.read, io.write);
/// for d in disks.sample_per_disk()? {
///     println!("  {}: busy {:?}", d.device, d.io.busy);
/// }
/// # Ok::<(), sys_lib::Error>(())
/// ```
#[derive(Debug)]
pub struct IoSampler {
    total: DiskRaw,
    at: Instant,
    devices: Vec<(String, DiskRaw)>,
    devices_at: Instant,
}

impl IoSampler {
    /// Takes the first reading, of the total and of each disk.
    ///
    /// # Errors
    ///
    /// As [`io_totals`].
    pub fn new() -> Result<IoSampler> {
        let now = Instant::now();
        Ok(IoSampler {
            total: crate::sys::disk_io_totals()?,
            at: now,
            devices: crate::sys::disk_io_counters()?,
            devices_at: now,
        })
    }

    fn totals_only() -> Result<IoSampler> {
        let now = Instant::now();
        Ok(IoSampler {
            total: crate::sys::disk_io_totals()?,
            at: now,
            devices: Vec::new(),
            devices_at: now,
        })
    }

    /// Activity since the previous sample, every physical disk together. A
    /// counter that went backwards re-bases the sampler and reads as no
    /// activity for that interval.
    ///
    /// # Errors
    ///
    /// `TooSoon` when no time passed since the previous sample.
    pub fn sample(&mut self) -> Result<DiskIo> {
        let now = crate::sys::disk_io_totals()?;
        let at = Instant::now();
        let interval = at.duration_since(self.at);
        if interval.is_zero() {
            return Err(too_soon(interval));
        }
        let io = now.rates_since(&self.total, interval);
        self.total = now;
        self.at = at;
        Ok(io)
    }

    /// Activity since the previous per-disk sample, for each disk (allocates
    /// the `Vec`). A disk seen for the first time reads as no activity.
    ///
    /// # Errors
    ///
    /// `TooSoon` when no time passed since the previous sample.
    pub fn sample_per_disk(&mut self) -> Result<Vec<DeviceIo>> {
        let now = crate::sys::disk_io_counters()?;
        let at = Instant::now();
        let interval = at.duration_since(self.devices_at);
        if interval.is_zero() {
            return Err(too_soon(interval));
        }
        let out = now
            .iter()
            .map(|(device, raw)| {
                let earlier = self
                    .devices
                    .iter()
                    .find(|(d, _)| d == device)
                    .map_or(*raw, |(_, r)| *r);
                DeviceIo {
                    device: device.clone(),
                    io: raw.rates_since(&earlier, interval),
                }
            })
            .collect();
        self.devices = now;
        self.devices_at = at;
        Ok(out)
    }
}

fn too_soon(elapsed: Duration) -> Error {
    Error::TooSoon {
        elapsed,
        minimum: Duration::from_millis(1),
    }
}

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

    #[test]
    fn test_disk_busy_across_two_disks_is_the_mean() {
        let one = |busy_ms: u64| DiskRaw {
            busy: Some(Duration::from_millis(busy_ms)),
            disks: 1,
            ..DiskRaw::default()
        };
        let (mut before, mut after) = (one(0), one(600));
        before.add(&one(0));
        after.add(&one(600));
        let io = after.rates_since(&before, Duration::from_secs(1));
        assert_eq!(io.busy.unwrap().to_string(), "60.0%");
    }

    #[test]
    fn test_disk_space_invariants() {
        let s = Space::new(100, 150, 200);
        assert_eq!(
            (s.total.raw(), s.free.raw(), s.available.raw()),
            (100, 100, 100)
        );
        let s = Space::new(1000, 400, 300);
        assert_eq!(s.used().raw(), 600);
        assert_eq!(s.used_percent().to_string(), "60.0%");
        assert_eq!(Space::new(0, 0, 0).used_percent(), Percent::ZERO);
    }

    #[test]
    fn test_disk_rates_and_backwards_counters() {
        let a = DiskRaw {
            read: 1 << 20,
            written: 0,
            reads: 10,
            writes: 0,
            busy: Some(Duration::from_millis(100)),
            op_time: Some(Duration::from_millis(20)),
            disks: 1,
        };
        let b = DiskRaw {
            read: 3 << 20,
            written: 1 << 20,
            reads: 30,
            writes: 10,
            busy: Some(Duration::from_millis(600)),
            op_time: Some(Duration::from_millis(80)),
            disks: 1,
        };
        let io = b.rates_since(&a, Duration::from_secs(1));
        assert_eq!(io.read.to_string(), "2 MiB/s");
        assert_eq!(io.write.to_string(), "1 MiB/s");
        assert!((io.reads_per_sec - 20.0).abs() < 1e-9 && (io.writes_per_sec - 10.0).abs() < 1e-9);
        assert_eq!(io.busy.unwrap().to_string(), "50.0%");
        assert_eq!(io.average_latency, Some(Duration::from_millis(2)));
        // A disk re-attached: every counter went backwards. No negative rates.
        let back = a.rates_since(&b, Duration::from_secs(1));
        assert_eq!((back.read, back.write), (Rate::ZERO, Rate::ZERO));
        assert_eq!(back.busy.unwrap(), Percent::ZERO);
        assert_eq!(back.average_latency, None);
    }

    #[test]
    fn test_disk_live_space_and_list() {
        let s = space(".").unwrap();
        assert!(s.total.raw() > 0 && s.available <= s.free && s.free <= s.total);
        let disks = list().unwrap();
        assert!(
            disks
                .windows(2)
                .all(|w| w[0].mount_point <= w[1].mount_point)
        );
        for d in &disks {
            assert!(!d.filesystem.is_empty(), "{d:?}");
        }
        assert!(matches!(space("/definitely/not/here/sys-lib"), Err(e) if e.is_unavailable()));
    }
}