| 1 | //! Collecting memory data using sysinfo. |
| 2 | |
| 3 | use std::num::NonZeroU64; |
| 4 | |
| 5 | #[cfg(target_os = "linux")] |
| 6 | use crate::collection::linux::cgroups::CgroupMemLimit; |
| 7 | use crate::collection::{DataCollector, memory::MemData}; |
| 8 | |
| 9 | #[inline] |
| 10 | fn get_usage(used: u64, total: u64) -> Option<MemData> { |
| 11 | NonZeroU64::new(total).map(|total_bytes| MemData { |
| 12 | total_bytes, |
| 13 | used_bytes: used, |
| 14 | }) |
| 15 | } |
| 16 | |
| 17 | /// Resolves the total memory to report given an optional cgroup limit and the physical total. |
| 18 | /// |
| 19 | /// cgroup v1 reports an "unlimited" limit as a very large value, which causes problems if taken |
| 20 | /// literally. This function caps it to the minimum of the cgroup total or the actual total to |
| 21 | /// avoid this problem. |
| 22 | #[cfg(target_os = "linux")] |
| 23 | #[inline] |
| 24 | fn resolve_cgroup_total(limit: Option<&CgroupMemLimit>, base_total: u64) -> u64 { |
| 25 | match limit { |
| 26 | Some(CgroupMemLimit::Bytes(bytes)) => (*bytes).min(base_total), |
| 27 | Some(CgroupMemLimit::Max) | None => base_total, |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | /// Returns memory (RAM) usage using sysinfo. |
| 32 | /// |
| 33 | /// On Linux, this will take cgroup usage/limits into account. |
| 34 | pub(crate) fn get_ram_usage(collector: &DataCollector) -> Option<MemData> { |
| 35 | let sys = &collector.sys.system; |
| 36 | |
| 37 | cfg_select! { |
| 38 | target_os = "linux" => { |
| 39 | let base_used = sys.used_memory(); |
| 40 | let base_total = sys.total_memory(); |
| 41 | |
| 42 | let (used, total) = match &collector.cgroup_memory_data.ram { |
| 43 | Some(cgroup_data) => { |
| 44 | let used = cgroup_data.used_bytes; |
| 45 | let total = resolve_cgroup_total(cgroup_data.limit.as_ref(), base_total); |
| 46 | |
| 47 | (used, total) |
| 48 | } |
| 49 | None => (base_used, base_total), |
| 50 | }; |
| 51 | |
| 52 | get_usage(used, total) |
| 53 | } |
| 54 | _ => { |
| 55 | get_usage(sys.used_memory(), sys.total_memory()) |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /// Returns SWAP usage using sysinfo. |
| 61 | /// |
| 62 | /// On Linux, this will take cgroup usage/limits into account. |
| 63 | #[cfg(not(target_os = "windows"))] |
| 64 | pub(crate) fn get_swap_usage(collector: &DataCollector) -> Option<MemData> { |
| 65 | let sys = &collector.sys.system; |
| 66 | |
| 67 | cfg_select! { |
| 68 | target_os = "linux" => { |
| 69 | let base_used = sys.used_swap(); |
| 70 | let base_total = sys.total_swap(); |
| 71 | |
| 72 | let (used, total) = match &collector.cgroup_memory_data.swap { |
| 73 | Some(cgroup_data) => { |
| 74 | let used = cgroup_data.used_bytes; |
| 75 | let total = resolve_cgroup_total(cgroup_data.limit.as_ref(), base_total); |
| 76 | |
| 77 | (used, total) |
| 78 | } |
| 79 | None => (base_used, base_total), |
| 80 | }; |
| 81 | |
| 82 | get_usage(used, total) |
| 83 | } |
| 84 | _ => { |
| 85 | get_usage(sys.used_swap(), sys.total_swap()) |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /// Returns cache usage using sysinfo. |
| 91 | /// |
| 92 | /// sysinfo has no way to do this directly but it should equal the difference |
| 93 | /// between the available and free memory. Free memory is defined as memory |
| 94 | /// not containing any data, which means cache and buffer memory are not |
| 95 | /// "free". Available memory is defined as memory able to be allocated by |
| 96 | /// processes, which includes cache and buffer memory. On Windows, this will |
| 97 | /// always be 0. |
| 98 | /// |
| 99 | /// For more information, see [sysinfo docs](https://docs.rs/sysinfo/latest/sysinfo/struct.System.html#method.available_memory) |
| 100 | /// and [this explanation on memory](https://askubuntu.com/questions/867068/what-is-available-memory-while-using-free-command) |
| 101 | #[cfg(not(target_os = "windows"))] |
| 102 | pub(crate) fn get_cache_usage(sys: &sysinfo::System) -> Option<MemData> { |
| 103 | let mem_used = sys.available_memory().saturating_sub(sys.free_memory()); |
| 104 | let mem_total = sys.total_memory(); |
| 105 | |
| 106 | get_usage(mem_used, mem_total) |
| 107 | } |
| 108 | |
| 109 | #[cfg(test)] |
| 110 | #[cfg(target_os = "linux")] |
| 111 | mod linux_tests { |
| 112 | use super::*; |
| 113 | |
| 114 | const BASE_TOTAL: u64 = 16 * 1024 * 1024 * 1024; // 16 GiB |
| 115 | |
| 116 | /// Regression test for <https://github.com/ClementTsang/bottom/issues/2092>. |
| 117 | #[test] |
| 118 | fn cap_cgroup_v1_limits() { |
| 119 | let sentinel = u64::MAX; |
| 120 | assert_eq!( |
| 121 | resolve_cgroup_total(Some(&CgroupMemLimit::Bytes(sentinel)), BASE_TOTAL), |
| 122 | BASE_TOTAL |
| 123 | ); |
| 124 | } |
| 125 | |
| 126 | #[test] |
| 127 | fn legit_cgroup_limit_works() { |
| 128 | let limit = 4 * 1024 * 1024 * 1024; // 4 GiB |
| 129 | assert_eq!( |
| 130 | resolve_cgroup_total(Some(&CgroupMemLimit::Bytes(limit)), BASE_TOTAL), |
| 131 | limit |
| 132 | ); |
| 133 | } |
| 134 | |
| 135 | #[test] |
| 136 | fn max_and_missing_limit_use_physical_total() { |
| 137 | assert_eq!( |
| 138 | resolve_cgroup_total(Some(&CgroupMemLimit::Max), BASE_TOTAL), |
| 139 | BASE_TOTAL |
| 140 | ); |
| 141 | assert_eq!(resolve_cgroup_total(None, BASE_TOTAL), BASE_TOTAL); |
| 142 | } |
| 143 | } |