Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement swap usage for linux #175

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,18 @@ impl MemoryReadout for AndroidMemoryReadout {

Ok(total - free - cached - reclaimable - buffers)
}

fn swap_total(&self) -> Result<u64, ReadoutError> {
return Err(ReadoutError::NotImplemented);
}

fn swap_free(&self) -> Result<u64, ReadoutError> {
return Err(ReadoutError::NotImplemented);
}

fn swap_used(&self) -> Result<u64, ReadoutError> {
return Err(ReadoutError::NotImplemented);
}
}

impl ProductReadout for AndroidProductReadout {
Expand Down
12 changes: 12 additions & 0 deletions src/freebsd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,18 @@ impl MemoryReadout for FreeBSDMemoryReadout {

Ok(total - free)
}

fn swap_total(&self) -> Result<u64, ReadoutError> {
return Err(ReadoutError::NotImplemented);
}

fn swap_free(&self) -> Result<u64, ReadoutError> {
return Err(ReadoutError::NotImplemented);
}

fn swap_used(&self) -> Result<u64, ReadoutError> {
return Err(ReadoutError::NotImplemented);
}
}

impl ProductReadout for FreeBSDProductReadout {
Expand Down
39 changes: 39 additions & 0 deletions src/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,45 @@ impl MemoryReadout for LinuxMemoryReadout {
available => Ok(total - available),
}
}

fn swap_total(&self) -> Result<u64, ReadoutError> {
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<u64, ReadoutError> {
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<u64, ReadoutError> {
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 {
Expand Down
12 changes: 12 additions & 0 deletions src/macos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,18 @@ impl MemoryReadout for MacOSMemoryReadout {

Ok(used)
}

fn swap_total(&self) -> Result<u64, ReadoutError> {
return Err(ReadoutError::NotImplemented);
}

fn swap_free(&self) -> Result<u64, ReadoutError> {
return Err(ReadoutError::NotImplemented);
}

fn swap_used(&self) -> Result<u64, ReadoutError> {
return Err(ReadoutError::NotImplemented);
}
}

impl MacOSMemoryReadout {
Expand Down
12 changes: 12 additions & 0 deletions src/netbsd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,18 @@ impl MemoryReadout for NetBSDMemoryReadout {

Ok(total - free)
}

fn swap_total(&self) -> Result<u64, ReadoutError> {
return Err(ReadoutError::NotImplemented);
}

fn swap_free(&self) -> Result<u64, ReadoutError> {
return Err(ReadoutError::NotImplemented);
}

fn swap_used(&self) -> Result<u64, ReadoutError> {
return Err(ReadoutError::NotImplemented);
}
}

impl ProductReadout for NetBSDProductReadout {
Expand Down
12 changes: 12 additions & 0 deletions src/openwrt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,18 @@ impl MemoryReadout for OpenWrtMemoryReadout {

Ok(total - free - cached - buffers)
}

fn swap_total(&self) -> Result<u64, ReadoutError> {
return Err(ReadoutError::NotImplemented);
}

fn swap_free(&self) -> Result<u64, ReadoutError> {
return Err(ReadoutError::NotImplemented);
}

fn swap_used(&self) -> Result<u64, ReadoutError> {
return Err(ReadoutError::NotImplemented);
}
}

impl PackageReadout for OpenWrtPackageReadout {
Expand Down
9 changes: 9 additions & 0 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,15 @@ pub trait MemoryReadout {

/// This function should return the amount of currently used memory in kilobytes.
fn used(&self) -> Result<u64, ReadoutError>;

/// This function should return of the total available swap in kilobytes.
fn swap_total(&self) -> Result<u64, ReadoutError>;

/// This function should return the amount of the free available swap in kilobytes.
fn swap_free(&self) -> Result<u64, ReadoutError>;

/// This function should return the amount of currently used swap in kilobytes.
fn swap_used(&self) -> Result<u64, ReadoutError>;
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64, ReadoutError> {
return Err(ReadoutError::NotImplemented);
}

fn swap_free(&self) -> Result<u64, ReadoutError> {
return Err(ReadoutError::NotImplemented);
}

fn swap_used(&self) -> Result<u64, ReadoutError> {
return Err(ReadoutError::NotImplemented);
}
}

impl WindowsMemoryReadout {
Expand Down