Skip to content

Commit

Permalink
feat: swap support for windows, android, openwrt and netbsd
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Oct 30, 2024
1 parent a7d5001 commit 912d7ab
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 19 deletions.
33 changes: 30 additions & 3 deletions src/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,15 +360,42 @@ impl MemoryReadout for AndroidMemoryReadout {
}

fn swap_total(&self) -> Result<u64, ReadoutError> {
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<u64, ReadoutError> {
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<u64, ReadoutError> {
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(),
))
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/freebsd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,15 +351,15 @@ impl MemoryReadout for FreeBSDMemoryReadout {
}

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

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

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

Expand Down
6 changes: 3 additions & 3 deletions src/macos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,15 +491,15 @@ impl MemoryReadout for MacOSMemoryReadout {
}

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

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

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

Expand Down
9 changes: 6 additions & 3 deletions src/netbsd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,15 +381,18 @@ impl MemoryReadout for NetBSDMemoryReadout {
}

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

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

fn swap_used(&self) -> Result<u64, ReadoutError> {
return Err(ReadoutError::NotImplemented);
let total = self.swap_total()?;
let free = self.swap_free()?;

Ok(total - free)
}
}

Expand Down
33 changes: 30 additions & 3 deletions src/openwrt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,15 +290,42 @@ impl MemoryReadout for OpenWrtMemoryReadout {
}

fn swap_total(&self) -> Result<u64, ReadoutError> {
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<u64, ReadoutError> {
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<u64, ReadoutError> {
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(),
))
}
}
}

Expand Down
12 changes: 8 additions & 4 deletions src/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ impl MemoryReadout for WindowsMemoryReadout {
}

fn free(&self) -> Result<u64, ReadoutError> {
Err(ReadoutError::NotImplemented)
let memory_status = WindowsMemoryReadout::get_memory_status()?;
Ok(memory_status.ullAvailPhys / 1024u64)
}

fn buffers(&self) -> Result<u64, ReadoutError> {
Expand All @@ -132,15 +133,18 @@ impl MemoryReadout for WindowsMemoryReadout {
}

fn swap_total(&self) -> Result<u64, ReadoutError> {
return Err(ReadoutError::NotImplemented);
let memory_status = WindowsMemoryReadout::get_memory_status()?;
Ok(memory_status.ullTotalVirtual / 1024u64)
}

fn swap_free(&self) -> Result<u64, ReadoutError> {
return Err(ReadoutError::NotImplemented);
let memory_status = WindowsMemoryReadout::get_memory_status()?;
Ok(memory_status.ullAvailVirtual / 1024u64)
}

fn swap_used(&self) -> Result<u64, ReadoutError> {
return Err(ReadoutError::NotImplemented);
let memory_status = WindowsMemoryReadout::get_memory_status()?;
Ok((memory_status.ullTotalVirtual - memory_status.ullAvailVirtual) / 1024u64)
}
}

Expand Down

0 comments on commit 912d7ab

Please sign in to comment.