Skip to main content

feldera_observability/
system.rs

1//! System introspection utilities.
2
3/// Returns the memory, in megabytes (MB), of the current system, or `None`
4/// when the operating system does not report it.
5///
6/// This is the min of the host's available memory and the cgroup budget
7/// (what a container or Kubernetes pod memory limit sets).
8pub fn total_memory_megabyte() -> Option<u64> {
9    total_memory_bytes()
10        .map(|bytes| bytes / 1_000_000)
11        .filter(|&mb| mb > 0)
12}
13
14/// Same as [`total_memory_megabyte`], but in bytes.
15pub fn total_memory_bytes() -> Option<u64> {
16    let mut system = sysinfo::System::new();
17    system.refresh_memory();
18    let mut available = system.total_memory();
19    if let Some(limits) = own_cgroup_limits(&mut system).or_else(|| system.cgroup_limits()) {
20        available = available.min(limits.total_memory);
21    }
22    // `sysinfo` reports 0 on platforms it does not support.
23    if available > 0 { Some(available) } else { None }
24}
25
26/// Returns the resident set size of this process, in bytes, or `None` when the
27/// operating system does not report it.
28pub fn process_rss_bytes() -> Option<u64> {
29    memory_stats::memory_stats().map(|usage| usage.physical_mem as u64)
30}
31
32/// Returns the limits of this process's own cgroup, including limits set on
33/// its parent cgroups. Unlike [`sysinfo::System::cgroup_limits`], this works
34/// without a cgroup namespace, where `/sys/fs/cgroup` is the host's root.
35fn own_cgroup_limits(system: &mut sysinfo::System) -> Option<sysinfo::CGroupLimits> {
36    let pid = sysinfo::get_current_pid().ok()?;
37    system.refresh_processes_specifics(
38        sysinfo::ProcessesToUpdate::Some(&[pid]),
39        false,
40        sysinfo::ProcessRefreshKind::nothing(),
41    );
42    system.process(pid)?.cgroup_limits()
43}
44
45#[cfg(test)]
46mod tests {
47    /// Guards the `sysinfo` call sequence (`cgroup_limits` panics when memory
48    /// was not refreshed first).
49    #[cfg(target_os = "linux")]
50    #[test]
51    fn reports_nonzero_memory() {
52        let mb = super::total_memory_megabyte().expect("Linux reports available memory");
53        assert!(mb > 0, "available memory must be positive, got {mb} MB");
54    }
55
56    /// A running process always has a resident set.
57    #[cfg(any(target_os = "linux", target_os = "macos"))]
58    #[test]
59    fn reports_the_resident_set_of_this_process() {
60        let bytes = super::process_rss_bytes().expect("Linux and macOS report the resident set");
61        assert!(
62            bytes > 0,
63            "the resident set must be positive, got {bytes} bytes"
64        );
65    }
66
67    /// Runs the real cgroup lookup, which the DBSP tests mock out.
68    #[cfg(target_os = "linux")]
69    #[test]
70    fn available_memory_is_at_most_host_memory() {
71        let mut system = sysinfo::System::new();
72        system.refresh_memory();
73        let bytes = super::total_memory_bytes().expect("Linux reports available memory");
74        assert!(
75            bytes <= system.total_memory(),
76            "available memory {bytes} exceeds host memory {}",
77            system.total_memory()
78        );
79    }
80}