diff --git a/src/android/mod.rs b/src/android/mod.rs index aa4c7f0..06db653 100644 --- a/src/android/mod.rs +++ b/src/android/mod.rs @@ -358,6 +358,18 @@ impl MemoryReadout for AndroidMemoryReadout { Ok(total - free - cached - reclaimable - buffers) } + + fn swap_total(&self) -> Result { + return Err(ReadoutError::NotImplemented); + } + + fn swap_free(&self) -> Result { + return Err(ReadoutError::NotImplemented); + } + + fn swap_used(&self) -> Result { + return Err(ReadoutError::NotImplemented); + } } impl ProductReadout for AndroidProductReadout { diff --git a/src/freebsd/mod.rs b/src/freebsd/mod.rs index 5ac976d..e067b9d 100644 --- a/src/freebsd/mod.rs +++ b/src/freebsd/mod.rs @@ -349,6 +349,18 @@ impl MemoryReadout for FreeBSDMemoryReadout { Ok(total - free) } + + fn swap_total(&self) -> Result { + return Err(ReadoutError::NotImplemented); + } + + fn swap_free(&self) -> Result { + return Err(ReadoutError::NotImplemented); + } + + fn swap_used(&self) -> Result { + return Err(ReadoutError::NotImplemented); + } } impl ProductReadout for FreeBSDProductReadout { diff --git a/src/linux/mod.rs b/src/linux/mod.rs index 173c07e..63f0b15 100644 --- a/src/linux/mod.rs +++ b/src/linux/mod.rs @@ -642,6 +642,45 @@ impl MemoryReadout for LinuxMemoryReadout { available => Ok(total - available), } } + + fn swap_total(&self) -> Result { + 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 { + 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 { + 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(), + )) + } + } } impl ProductReadout for LinuxProductReadout { diff --git a/src/macos/mod.rs b/src/macos/mod.rs index b0e9170..74bd17e 100644 --- a/src/macos/mod.rs +++ b/src/macos/mod.rs @@ -489,6 +489,18 @@ impl MemoryReadout for MacOSMemoryReadout { Ok(used) } + + fn swap_total(&self) -> Result { + return Err(ReadoutError::NotImplemented); + } + + fn swap_free(&self) -> Result { + return Err(ReadoutError::NotImplemented); + } + + fn swap_used(&self) -> Result { + return Err(ReadoutError::NotImplemented); + } } impl MacOSMemoryReadout { diff --git a/src/netbsd/mod.rs b/src/netbsd/mod.rs index 29019e3..b2ae99b 100644 --- a/src/netbsd/mod.rs +++ b/src/netbsd/mod.rs @@ -379,6 +379,18 @@ impl MemoryReadout for NetBSDMemoryReadout { Ok(total - free) } + + fn swap_total(&self) -> Result { + return Err(ReadoutError::NotImplemented); + } + + fn swap_free(&self) -> Result { + return Err(ReadoutError::NotImplemented); + } + + fn swap_used(&self) -> Result { + return Err(ReadoutError::NotImplemented); + } } impl ProductReadout for NetBSDProductReadout { diff --git a/src/openwrt/mod.rs b/src/openwrt/mod.rs index 90b717f..a9476eb 100644 --- a/src/openwrt/mod.rs +++ b/src/openwrt/mod.rs @@ -288,6 +288,18 @@ impl MemoryReadout for OpenWrtMemoryReadout { Ok(total - free - cached - buffers) } + + fn swap_total(&self) -> Result { + return Err(ReadoutError::NotImplemented); + } + + fn swap_free(&self) -> Result { + return Err(ReadoutError::NotImplemented); + } + + fn swap_used(&self) -> Result { + return Err(ReadoutError::NotImplemented); + } } impl PackageReadout for OpenWrtPackageReadout { diff --git a/src/traits.rs b/src/traits.rs index 5ed9313..f3071c0 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -224,6 +224,15 @@ pub trait MemoryReadout { /// This function should return the amount of currently used memory in kilobytes. fn used(&self) -> Result; + + /// This function should return of the total available swap in kilobytes. + fn swap_total(&self) -> Result; + + /// This function should return the amount of the free available swap in kilobytes. + fn swap_free(&self) -> Result; + + /// This function should return the amount of currently used swap in kilobytes. + fn swap_used(&self) -> Result; } /** diff --git a/src/windows/mod.rs b/src/windows/mod.rs index 1bd0426..c99eedc 100644 --- a/src/windows/mod.rs +++ b/src/windows/mod.rs @@ -130,6 +130,18 @@ impl MemoryReadout for WindowsMemoryReadout { let memory_status = WindowsMemoryReadout::get_memory_status()?; Ok((memory_status.ullTotalPhys - memory_status.ullAvailPhys) / 1024u64) } + + fn swap_total(&self) -> Result { + return Err(ReadoutError::NotImplemented); + } + + fn swap_free(&self) -> Result { + return Err(ReadoutError::NotImplemented); + } + + fn swap_used(&self) -> Result { + return Err(ReadoutError::NotImplemented); + } } impl WindowsMemoryReadout {