diff --git a/src/android/mod.rs b/src/android/mod.rs index 06db653..75662e1 100644 --- a/src/android/mod.rs +++ b/src/android/mod.rs @@ -360,15 +360,42 @@ impl MemoryReadout for AndroidMemoryReadout { } fn swap_total(&self) -> Result { - return Err(ReadoutError::NotImplemented); + let mut info = self.sysinfo; + let info_ptr: *mut sysinfo = &mut info; + let ret = unsafe { sysinfo(info_ptr) }; + if ret != -1 { + Ok(info.totalswap as u64 * info.mem_unit as u64 / 1024) + } else { + Err(ReadoutError::Other( + "Something went wrong during the initialization of the sysinfo struct.".to_string(), + )) + } } fn swap_free(&self) -> Result { - return Err(ReadoutError::NotImplemented); + let mut info = self.sysinfo; + let info_ptr: *mut sysinfo = &mut info; + let ret = unsafe { sysinfo(info_ptr) }; + if ret != -1 { + Ok(info.freeswap as u64 * info.mem_unit as u64 / 1024) + } else { + Err(ReadoutError::Other( + "Something went wrong during the initialization of the sysinfo struct.".to_string(), + )) + } } fn swap_used(&self) -> Result { - return Err(ReadoutError::NotImplemented); + let mut info = self.sysinfo; + let info_ptr: *mut sysinfo = &mut info; + let ret = unsafe { sysinfo(info_ptr) }; + if ret != -1 { + Ok((info.totalswap as u64 - info.freeswap as u64) * info.mem_unit as u64 / 1024) + } else { + Err(ReadoutError::Other( + "Something went wrong during the initialization of the sysinfo struct.".to_string(), + )) + } } } diff --git a/src/freebsd/mod.rs b/src/freebsd/mod.rs index e067b9d..8b44e3e 100644 --- a/src/freebsd/mod.rs +++ b/src/freebsd/mod.rs @@ -351,15 +351,15 @@ impl MemoryReadout for FreeBSDMemoryReadout { } fn swap_total(&self) -> Result { - return Err(ReadoutError::NotImplemented); + Err(ReadoutError::NotImplemented) } fn swap_free(&self) -> Result { - return Err(ReadoutError::NotImplemented); + Err(ReadoutError::NotImplemented) } fn swap_used(&self) -> Result { - return Err(ReadoutError::NotImplemented); + Err(ReadoutError::NotImplemented) } } diff --git a/src/macos/mod.rs b/src/macos/mod.rs index 74bd17e..fdcaf2f 100644 --- a/src/macos/mod.rs +++ b/src/macos/mod.rs @@ -491,15 +491,15 @@ impl MemoryReadout for MacOSMemoryReadout { } fn swap_total(&self) -> Result { - return Err(ReadoutError::NotImplemented); + Err(ReadoutError::NotImplemented) } fn swap_free(&self) -> Result { - return Err(ReadoutError::NotImplemented); + Err(ReadoutError::NotImplemented) } fn swap_used(&self) -> Result { - return Err(ReadoutError::NotImplemented); + Err(ReadoutError::NotImplemented) } } diff --git a/src/netbsd/mod.rs b/src/netbsd/mod.rs index b2ae99b..d261496 100644 --- a/src/netbsd/mod.rs +++ b/src/netbsd/mod.rs @@ -381,15 +381,18 @@ impl MemoryReadout for NetBSDMemoryReadout { } fn swap_total(&self) -> Result { - return Err(ReadoutError::NotImplemented); + Ok(shared::get_meminfo_value("SwapTotal")) } fn swap_free(&self) -> Result { - return Err(ReadoutError::NotImplemented); + Ok(shared::get_meminfo_value("SwapFree")) } fn swap_used(&self) -> Result { - return Err(ReadoutError::NotImplemented); + let total = self.swap_total()?; + let free = self.swap_free()?; + + Ok(total - free) } } diff --git a/src/openwrt/mod.rs b/src/openwrt/mod.rs index a9476eb..5139617 100644 --- a/src/openwrt/mod.rs +++ b/src/openwrt/mod.rs @@ -290,15 +290,42 @@ impl MemoryReadout for OpenWrtMemoryReadout { } fn swap_total(&self) -> Result { - return Err(ReadoutError::NotImplemented); + let mut info = self.sysinfo; + let info_ptr: *mut sysinfo = &mut info; + let ret = unsafe { sysinfo(info_ptr) }; + if ret != -1 { + Ok(info.totalswap as u64 * info.mem_unit as u64 / 1024) + } else { + Err(ReadoutError::Other( + "Something went wrong during the initialization of the sysinfo struct.".to_string(), + )) + } } fn swap_free(&self) -> Result { - return Err(ReadoutError::NotImplemented); + let mut info = self.sysinfo; + let info_ptr: *mut sysinfo = &mut info; + let ret = unsafe { sysinfo(info_ptr) }; + if ret != -1 { + Ok(info.freeswap as u64 * info.mem_unit as u64 / 1024) + } else { + Err(ReadoutError::Other( + "Something went wrong during the initialization of the sysinfo struct.".to_string(), + )) + } } fn swap_used(&self) -> Result { - return Err(ReadoutError::NotImplemented); + let mut info = self.sysinfo; + let info_ptr: *mut sysinfo = &mut info; + let ret = unsafe { sysinfo(info_ptr) }; + if ret != -1 { + Ok((info.totalswap as u64 - info.freeswap as u64) * info.mem_unit as u64 / 1024) + } else { + Err(ReadoutError::Other( + "Something went wrong during the initialization of the sysinfo struct.".to_string(), + )) + } } } diff --git a/src/windows/mod.rs b/src/windows/mod.rs index c99eedc..ccaccc3 100644 --- a/src/windows/mod.rs +++ b/src/windows/mod.rs @@ -111,7 +111,8 @@ impl MemoryReadout for WindowsMemoryReadout { } fn free(&self) -> Result { - Err(ReadoutError::NotImplemented) + let memory_status = WindowsMemoryReadout::get_memory_status()?; + Ok(memory_status.ullAvailPhys / 1024u64) } fn buffers(&self) -> Result { @@ -132,15 +133,18 @@ impl MemoryReadout for WindowsMemoryReadout { } fn swap_total(&self) -> Result { - return Err(ReadoutError::NotImplemented); + let memory_status = WindowsMemoryReadout::get_memory_status()?; + Ok(memory_status.ullTotalVirtual / 1024u64) } fn swap_free(&self) -> Result { - return Err(ReadoutError::NotImplemented); + let memory_status = WindowsMemoryReadout::get_memory_status()?; + Ok(memory_status.ullAvailVirtual / 1024u64) } fn swap_used(&self) -> Result { - return Err(ReadoutError::NotImplemented); + let memory_status = WindowsMemoryReadout::get_memory_status()?; + Ok((memory_status.ullTotalVirtual - memory_status.ullAvailVirtual) / 1024u64) } }