diff options
author | Clement Tsang <34804052+ClementTsang@users.noreply.github.com> | 2023-03-08 00:08:02 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-08 00:08:02 -0500 |
commit | 82c0c3b0e5561008255b7f023e7eee43db593946 (patch) | |
tree | 26f62400852d3882126d0bd9d5f5fd6cd05bfbe6 /src/app/data_harvester | |
parent | 10d7226b19345d6808d03ac34be3f82a85229a4d (diff) |
refactor: use internal committed memory calculation for Windows (#1051)
* refactor: use internal committed memory calculation for Windows
* comments
Diffstat (limited to 'src/app/data_harvester')
-rw-r--r-- | src/app/data_harvester/memory.rs | 12 | ||||
-rw-r--r-- | src/app/data_harvester/memory/sysinfo.rs | 1 | ||||
-rw-r--r-- | src/app/data_harvester/memory/windows.rs | 32 |
3 files changed, 44 insertions, 1 deletions
diff --git a/src/app/data_harvester/memory.rs b/src/app/data_harvester/memory.rs index 18ac94cd..02c0fa9c 100644 --- a/src/app/data_harvester/memory.rs +++ b/src/app/data_harvester/memory.rs @@ -1,7 +1,17 @@ //! Memory data collection. pub mod sysinfo; -pub(crate) use self::sysinfo::{get_ram_usage, get_swap_usage}; +pub(crate) use self::sysinfo::get_ram_usage; + +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(feature = "gpu")] pub mod gpu; diff --git a/src/app/data_harvester/memory/sysinfo.rs b/src/app/data_harvester/memory/sysinfo.rs index d0c5c8ca..4cb8bdb0 100644 --- a/src/app/data_harvester/memory/sysinfo.rs +++ b/src/app/data_harvester/memory/sysinfo.rs @@ -21,6 +21,7 @@ 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_in_kib = sys.used_swap() / 1024; let mem_total_in_kib = sys.total_swap() / 1024; diff --git a/src/app/data_harvester/memory/windows.rs b/src/app/data_harvester/memory/windows.rs new file mode 100644 index 00000000..5ead4b52 --- /dev/null +++ b/src/app/data_harvester/memory/windows.rs @@ -0,0 +1,32 @@ +use std::mem::{size_of, zeroed}; +use windows::Win32::Foundation::TRUE; +use windows::Win32::System::ProcessStatus::{K32GetPerformanceInfo, PERFORMANCE_INFORMATION}; + +use crate::data_harvester::memory::MemHarvest; + +// TODO: Note this actually calculates the total *committed* usage. Rename and change label for accuracy! +/// 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> { + // 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 K32GetPerformanceInfo(&mut perf_info, size_of::<PERFORMANCE_INFORMATION>() as u32) + == TRUE + { + // 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; + + Some(MemHarvest { + total_kib: swap_total / 1024, + used_kib: swap_used / 1024, + use_percent: Some(swap_used as f64 / swap_total as f64 * 100.0), + }) + } else { + None + } + } +} |