summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorClement Tsang <34804052+ClementTsang@users.noreply.github.com>2024-03-28 02:24:14 +0000
committerGitHub <noreply@github.com>2024-03-27 22:24:14 -0400
commit2ee0df1502925e1a5cae75f543110233006ef743 (patch)
tree12022bd2405ff880f6fc7671f74da767a007555f /src
parentc651e9a904bf25a95cbda20846115d4bf08de73d (diff)
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.
Diffstat (limited to 'src')
-rw-r--r--src/data_collection.rs5
-rw-r--r--src/data_collection/memory.rs17
-rw-r--r--src/data_collection/memory/sysinfo.rs1
-rw-r--r--src/data_collection/memory/windows.rs20
4 files changed, 19 insertions, 24 deletions
diff --git a/src/data_collection.rs b/src/data_collection.rs
index 7320a4f0..e97b34bc 100644
--- a/src/data_collection.rs
+++ b/src/data_collection.rs
@@ -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")]
{
diff --git a/src/data_collection/memory.rs b/src/data_collection/memory.rs
index dee65d8d..98cfebda 100644
--- a/src/data_collection/memory.rs
+++ b/src/data_collection/memory.rs
@@ -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;
diff --git a/src/data_collection/memory/sysinfo.rs b/src/data_collection/memory/sysinfo.rs
index 86ef5381..330bb9b0 100644
--- a/src/data_collection/memory/sysinfo.rs
+++ b/src/data_collection/memory/sysinfo.rs
@@ -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();
diff --git a/src/data_collection/memory/windows.rs b/src/data_collection/memory/windows.rs
index ba56df18..70792079 100644
--- a/src/data_collection/memory/windows.rs
+++ b/src/data_collection/memory/windows.rs
@@ -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