heim_memory/os/
linux.rs

1//! Linux-specific extensions
2
3/// Reference: https://gitlab.com/procps-ng/procps/blob/master/proc/sysinfo.c
4use heim_common::units::{information, Information};
5
6use crate::Memory;
7
8/// Linux-specific extension to [`Memory`]
9pub trait MemoryExt {
10    /// The amount of physical RAM used.
11    ///
12    /// It is designed for informational purposes only and might vary vastly
13    /// from platform to platform.
14    fn used(&self) -> Information;
15
16    /// The amount of physical RAM used for file buffers.
17    fn buffers(&self) -> Information;
18
19    /// The amount of physical RAM used as cache memory.
20    fn cached(&self) -> Information;
21
22    /// The amount of memory that may be simultaneously accessed by multiple processes.
23    fn shared(&self) -> Information;
24
25    /// The total amount of buffer or page cache memory, that is in active use.
26    ///
27    /// This is memory that has been recently used and is usually not reclaimed for other purposes.
28    fn active(&self) -> Information;
29
30    ///  The total amount of buffer or page cache memory, that are free and available.
31    ///
32    /// This is memory that has not been recently used and can be reclaimed for other purposes.
33    fn inactive(&self) -> Information;
34}
35
36#[cfg(target_os = "linux")]
37impl MemoryExt for Memory {
38    fn used(&self) -> Information {
39        let inner = self.as_ref();
40
41        let mut used = inner.total() - inner.free() - self.cached() - self.buffers();
42        if used <= Information::new::<information::byte>(0) {
43            // May be symptomatic of running within a LCX container where such
44            // values will be dramatically distorted over those of the host.
45            // Source: psutil
46            used = inner.total() - inner.free()
47        }
48
49        used
50    }
51
52    fn buffers(&self) -> Information {
53        self.as_ref().buffers()
54    }
55
56    fn cached(&self) -> Information {
57        self.as_ref().cached()
58    }
59
60    fn shared(&self) -> Information {
61        self.as_ref().shared()
62    }
63
64    fn active(&self) -> Information {
65        self.as_ref().active()
66    }
67
68    fn inactive(&self) -> Information {
69        self.as_ref().inactive()
70    }
71}