diff --git a/src/android/mod.rs b/src/android/mod.rs index d58143d..c236413 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 9746b4f..457218f 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 ca48b92..1d1a62e 100644 --- a/src/linux/mod.rs +++ b/src/linux/mod.rs @@ -641,6 +641,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 1b612bd..4f82cc0 100644 --- a/src/macos/mod.rs +++ b/src/macos/mod.rs @@ -488,6 +488,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 f9395af..d31f146 100644 --- a/src/netbsd/mod.rs +++ b/src/netbsd/mod.rs @@ -369,6 +369,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 81a26ad..cd7b2d9 100644 --- a/src/openwrt/mod.rs +++ b/src/openwrt/mod.rs @@ -287,6 +287,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 f7eff47..6efc250 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -222,6 +222,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 1d399d9..a90b40b 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 {