change: use sysinfo's swap value for Windows (#1436)

* some consts

* change: use sysinfo's swap calculation for windows

I'll add an additional field for committed in a separate PR which shows
the previous value.
This commit is contained in:
Clement Tsang 2024-03-28 02:24:14 +00:00 committed by GitHub
parent c651e9a904
commit 2ee0df1502
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 19 additions and 24 deletions

View File

@ -412,10 +412,7 @@ impl DataCollector {
self.data.cache = memory::get_cache_usage(&self.sys.system);
}
self.data.swap = memory::get_swap_usage(
#[cfg(not(target_os = "windows"))]
&self.sys.system,
);
self.data.swap = memory::get_swap_usage(&self.sys.system);
#[cfg(feature = "zfs")]
{

View File

@ -2,18 +2,15 @@
#[cfg(not(target_os = "windows"))]
pub(crate) use self::sysinfo::get_cache_usage;
pub(crate) use self::sysinfo::get_ram_usage;
pub(crate) use self::sysinfo::{get_ram_usage, get_swap_usage};
pub mod sysinfo;
cfg_if::cfg_if! {
if #[cfg(target_os = "windows")] {
pub mod windows;
pub(crate) use self::windows::get_swap_usage;
} else {
pub(crate) use self::sysinfo::get_swap_usage;
}
}
// cfg_if::cfg_if! {
// if #[cfg(target_os = "windows")] {
// mod windows;
// pub(crate) use self::windows::get_committed_usage;
// }
// }
#[cfg(feature = "zfs")]
pub mod arc;

View File

@ -21,7 +21,6 @@ pub(crate) fn get_ram_usage(sys: &System) -> Option<MemHarvest> {
}
/// Returns SWAP usage.
#[cfg(not(target_os = "windows"))]
pub(crate) fn get_swap_usage(sys: &System) -> Option<MemHarvest> {
let mem_used = sys.used_swap();
let mem_total = sys.total_swap();

View File

@ -4,24 +4,26 @@ use windows::Win32::System::ProcessStatus::{GetPerformanceInfo, PERFORMANCE_INFO
use crate::data_collection::memory::MemHarvest;
// TODO: Note this actually calculates the total *committed* usage. Rename and change label for accuracy!
const PERFORMANCE_INFORMATION_SIZE: u32 = size_of::<PERFORMANCE_INFORMATION>() as _;
/// Get the committed memory usage.
///
/// Code based on [sysinfo's](https://github.com/GuillaumeGomez/sysinfo/blob/6f8178495adcf3ca4696a9ec548586cf6a621bc8/src/windows/system.rs#L169).
pub(crate) fn get_swap_usage() -> Option<MemHarvest> {
pub(crate) fn get_committed_usage() -> Option<MemHarvest> {
// SAFETY: The safety invariant is that we only touch what's in `perf_info` if it succeeds, and that
// the bindings are "safe" to use with how we call them.
unsafe {
let mut perf_info: PERFORMANCE_INFORMATION = zeroed();
if GetPerformanceInfo(&mut perf_info, size_of::<PERFORMANCE_INFORMATION>() as u32).is_ok() {
// Saturating sub by perf_info.PhysicalTotal for what sysinfo does.
let swap_total = perf_info.PageSize.saturating_mul(perf_info.CommitLimit) as u64;
let swap_used = perf_info.PageSize.saturating_mul(perf_info.CommitTotal) as u64;
if GetPerformanceInfo(&mut perf_info, PERFORMANCE_INFORMATION_SIZE).is_ok() {
let page_size = perf_info.PageSize;
let committed_total = page_size.saturating_mul(perf_info.CommitLimit) as u64;
let committed_used = page_size.saturating_mul(perf_info.CommitTotal) as u64;
Some(MemHarvest {
used_bytes: swap_used,
total_bytes: swap_total,
use_percent: Some(swap_used as f64 / swap_total as f64 * 100.0),
used_bytes: committed_used,
total_bytes: committed_total,
use_percent: Some(committed_used as f64 / committed_total as f64 * 100.0),
})
} else {
None