From 7576e05e53750c223563477588c531af4334ee16 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Wed, 30 Oct 2024 12:20:26 +0100 Subject: [PATCH] feat: swap support for windows, android, openwrt and netbsd --- src/android/mod.rs | 33 ++++++++++++++++++++++++++++++--- src/freebsd/mod.rs | 6 +++--- src/macos/mod.rs | 6 +++--- src/netbsd/mod.rs | 9 ++++++--- src/openwrt/mod.rs | 33 ++++++++++++++++++++++++++++++--- src/traits.rs | 15 +++++++++++++++ src/windows/mod.rs | 12 ++++++++---- 7 files changed, 95 insertions(+), 19 deletions(-) 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/traits.rs b/src/traits.rs index f3071c0..ef79023 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -199,6 +199,21 @@ impl MemoryReadout for MacOSMemoryReadout { // Get the currently used memory. Ok(256 * 1024) // Return 256mb in kilobytes. } + + fn swap_total(&self) -> Result { + // Get the total virtual memory of the machine + Ok(512 * 1024) // Return 512mb in kilobytes. + } + + fn swap_free(&self) -> Result { + // Get the currently free virtual memory + Ok(256 * 1024) // Return 256mb in kilobytes. + } + + fn swap_used(&self) -> Result { + // Get the currently used virtual memory + Ok(256 * 1024) // Return 256mb in kilobytes. + } } ``` 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) } }