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: swap support for windows, android, openwrt and netbsd #180

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
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
15 changes: 15 additions & 0 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,21 @@ impl MemoryReadout for MacOSMemoryReadout {
// Get the currently used memory.
Ok(256 * 1024) // Return 256mb in kilobytes.
}

fn swap_total(&self) -> Result<u64, ReadoutError> {
// Get the total virtual memory of the machine
Ok(512 * 1024) // Return 512mb in kilobytes.
}

fn swap_free(&self) -> Result<u64, ReadoutError> {
// Get the currently free virtual memory
Ok(256 * 1024) // Return 256mb in kilobytes.
}

fn swap_used(&self) -> Result<u64, ReadoutError> {
// Get the currently used virtual memory
Ok(256 * 1024) // Return 256mb in kilobytes.
}
}

```
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