Skip to content

Commit

Permalink
feat: implement swap usage for linux (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro authored Oct 29, 2024
1 parent 432f639 commit a7d5001
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 0 deletions.
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 @@ -642,6 +642,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 @@ -489,6 +489,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 @@ -379,6 +379,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 @@ -288,6 +288,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 @@ -224,6 +224,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

0 comments on commit a7d5001

Please sign in to comment.