|
| 1 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | +// you may not use this file except in compliance with the License. |
| 3 | +// You may obtain a copy of the License at |
| 4 | +// |
| 5 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +// |
| 7 | +// Unless required by applicable law or agreed to in writing, software |
| 8 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +// See the License for the specific language governing permissions and |
| 11 | +// limitations under the License. |
| 12 | + |
| 13 | +use std::io; |
| 14 | + |
| 15 | +pub struct SystemMemoryInfo { |
| 16 | + pub total_physical: u64, |
| 17 | + pub available_physical: u64, |
| 18 | + pub total_virtual: u64, |
| 19 | + pub available_virtual: u64, |
| 20 | +} |
| 21 | + |
| 22 | +pub fn system_memory_info() -> io::Result<SystemMemoryInfo> { |
| 23 | + sys::system_memory_info() |
| 24 | +} |
| 25 | + |
| 26 | +#[cfg(target_os = "linux")] |
| 27 | +mod sys { |
| 28 | + use super::SystemMemoryInfo; |
| 29 | + use std::io; |
| 30 | + |
| 31 | + pub fn system_memory_info() -> io::Result<SystemMemoryInfo> { |
| 32 | + let content = std::fs::read_to_string("/proc/meminfo")?; |
| 33 | + |
| 34 | + let mut total_physical: Option<u64> = None; |
| 35 | + let mut available_physical: Option<u64> = None; |
| 36 | + let mut swap_total: u64 = 0; |
| 37 | + let mut swap_free: u64 = 0; |
| 38 | + |
| 39 | + for line in content.lines() { |
| 40 | + if let Some(v) = parse_meminfo_kb(line, "MemTotal:") { |
| 41 | + total_physical = Some(v); |
| 42 | + } else if let Some(v) = parse_meminfo_kb(line, "MemAvailable:") { |
| 43 | + available_physical = Some(v); |
| 44 | + } else if let Some(v) = parse_meminfo_kb(line, "SwapTotal:") { |
| 45 | + swap_total = v; |
| 46 | + } else if let Some(v) = parse_meminfo_kb(line, "SwapFree:") { |
| 47 | + swap_free = v; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + let total_phys = total_physical.ok_or_else(|| { |
| 52 | + io::Error::new( |
| 53 | + io::ErrorKind::NotFound, |
| 54 | + "MemTotal not found in /proc/meminfo", |
| 55 | + ) |
| 56 | + })?; |
| 57 | + let avail_phys = available_physical.unwrap_or(0); |
| 58 | + |
| 59 | + Ok(SystemMemoryInfo { |
| 60 | + total_physical: total_phys, |
| 61 | + available_physical: avail_phys, |
| 62 | + total_virtual: total_phys + swap_total, |
| 63 | + available_virtual: avail_phys + swap_free, |
| 64 | + }) |
| 65 | + } |
| 66 | + |
| 67 | + fn parse_meminfo_kb(line: &str, prefix: &str) -> Option<u64> { |
| 68 | + let rest = line.strip_prefix(prefix)?; |
| 69 | + let kb: u64 = rest.trim().trim_end_matches("kB").trim().parse().ok()?; |
| 70 | + Some(kb * 1024) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +#[cfg(target_os = "macos")] |
| 75 | +mod sys { |
| 76 | + use super::SystemMemoryInfo; |
| 77 | + use std::io; |
| 78 | + |
| 79 | + pub fn system_memory_info() -> io::Result<SystemMemoryInfo> { |
| 80 | + let total_physical = sysctl_u64("hw.memsize")?; |
| 81 | + |
| 82 | + let page_size = sysctl_u64("hw.pagesize").unwrap_or(4096); |
| 83 | + let vm_stats = read_vm_stat()?; |
| 84 | + |
| 85 | + let free_pages = vm_stats.free + vm_stats.inactive + vm_stats.purgeable; |
| 86 | + let available_physical = free_pages * page_size; |
| 87 | + |
| 88 | + let swap = read_swap_usage(); |
| 89 | + let swap_total = swap.0; |
| 90 | + let swap_free = swap_total.saturating_sub(swap.1); |
| 91 | + |
| 92 | + Ok(SystemMemoryInfo { |
| 93 | + total_physical, |
| 94 | + available_physical, |
| 95 | + total_virtual: total_physical + swap_total, |
| 96 | + available_virtual: available_physical + swap_free, |
| 97 | + }) |
| 98 | + } |
| 99 | + |
| 100 | + fn sysctl_u64(name: &str) -> io::Result<u64> { |
| 101 | + let output = std::process::Command::new("sysctl") |
| 102 | + .arg("-n") |
| 103 | + .arg(name) |
| 104 | + .output()?; |
| 105 | + if !output.status.success() { |
| 106 | + return Err(io::Error::new( |
| 107 | + io::ErrorKind::Other, |
| 108 | + format!("sysctl {name} failed"), |
| 109 | + )); |
| 110 | + } |
| 111 | + String::from_utf8_lossy(&output.stdout) |
| 112 | + .trim() |
| 113 | + .parse() |
| 114 | + .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)) |
| 115 | + } |
| 116 | + |
| 117 | + struct VmPages { |
| 118 | + free: u64, |
| 119 | + inactive: u64, |
| 120 | + purgeable: u64, |
| 121 | + } |
| 122 | + |
| 123 | + fn read_vm_stat() -> io::Result<VmPages> { |
| 124 | + let output = std::process::Command::new("vm_stat").output()?; |
| 125 | + let text = String::from_utf8_lossy(&output.stdout); |
| 126 | + |
| 127 | + let mut free = 0u64; |
| 128 | + let mut inactive = 0u64; |
| 129 | + let mut purgeable = 0u64; |
| 130 | + |
| 131 | + for line in text.lines() { |
| 132 | + if let Some(v) = parse_vm_stat_line(line, "Pages free") { |
| 133 | + free = v; |
| 134 | + } else if let Some(v) = parse_vm_stat_line(line, "Pages inactive") { |
| 135 | + inactive = v; |
| 136 | + } else if let Some(v) = parse_vm_stat_line(line, "Pages purgeable") { |
| 137 | + purgeable = v; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + Ok(VmPages { |
| 142 | + free, |
| 143 | + inactive, |
| 144 | + purgeable, |
| 145 | + }) |
| 146 | + } |
| 147 | + |
| 148 | + fn parse_vm_stat_line(line: &str, key: &str) -> Option<u64> { |
| 149 | + if !line.contains(key) { |
| 150 | + return None; |
| 151 | + } |
| 152 | + let val_str = line.rsplit(':').next()?.trim().trim_end_matches('.'); |
| 153 | + val_str.parse().ok() |
| 154 | + } |
| 155 | + |
| 156 | + fn read_swap_usage() -> (u64, u64) { |
| 157 | + let output = match std::process::Command::new("sysctl") |
| 158 | + .arg("-n") |
| 159 | + .arg("vm.swapusage") |
| 160 | + .output() |
| 161 | + { |
| 162 | + Ok(o) => o, |
| 163 | + Err(_) => return (0, 0), |
| 164 | + }; |
| 165 | + let text = String::from_utf8_lossy(&output.stdout); |
| 166 | + let mut total = 0u64; |
| 167 | + let mut used = 0u64; |
| 168 | + for part in text.split_whitespace() { |
| 169 | + if let Some(mb_str) = part.strip_suffix("M") { |
| 170 | + if let Ok(mb) = mb_str.parse::<f64>() { |
| 171 | + if total == 0 { |
| 172 | + total = (mb * 1024.0 * 1024.0) as u64; |
| 173 | + } else if used == 0 { |
| 174 | + used = (mb * 1024.0 * 1024.0) as u64; |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + (total, used) |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +#[cfg(target_os = "windows")] |
| 184 | +mod sys { |
| 185 | + use super::SystemMemoryInfo; |
| 186 | + use std::io; |
| 187 | + |
| 188 | + #[repr(C)] |
| 189 | + struct MemoryStatusEx { |
| 190 | + dw_length: u32, |
| 191 | + dw_memory_load: u32, |
| 192 | + ull_total_phys: u64, |
| 193 | + ull_avail_phys: u64, |
| 194 | + ull_total_page_file: u64, |
| 195 | + ull_avail_page_file: u64, |
| 196 | + ull_total_virtual: u64, |
| 197 | + ull_avail_virtual: u64, |
| 198 | + ull_avail_extended_virtual: u64, |
| 199 | + } |
| 200 | + |
| 201 | + extern "system" { |
| 202 | + fn GlobalMemoryStatusEx(lpBuffer: *mut MemoryStatusEx) -> i32; |
| 203 | + } |
| 204 | + |
| 205 | + pub fn system_memory_info() -> io::Result<SystemMemoryInfo> { |
| 206 | + unsafe { |
| 207 | + let mut status = std::mem::zeroed::<MemoryStatusEx>(); |
| 208 | + status.dw_length = std::mem::size_of::<MemoryStatusEx>() as u32; |
| 209 | + if GlobalMemoryStatusEx(&mut status) == 0 { |
| 210 | + return Err(io::Error::last_os_error()); |
| 211 | + } |
| 212 | + Ok(SystemMemoryInfo { |
| 213 | + total_physical: status.ull_total_phys, |
| 214 | + available_physical: status.ull_avail_phys, |
| 215 | + total_virtual: status.ull_total_virtual, |
| 216 | + available_virtual: status.ull_avail_virtual, |
| 217 | + }) |
| 218 | + } |
| 219 | + } |
| 220 | +} |
| 221 | + |
| 222 | +#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))] |
| 223 | +mod sys { |
| 224 | + use super::SystemMemoryInfo; |
| 225 | + use std::io; |
| 226 | + |
| 227 | + pub fn system_memory_info() -> io::Result<SystemMemoryInfo> { |
| 228 | + Err(io::Error::new( |
| 229 | + io::ErrorKind::Unsupported, |
| 230 | + "memory detection not supported on this platform", |
| 231 | + )) |
| 232 | + } |
| 233 | +} |
0 commit comments