Debian Codegraph @

src/collection/memory/windows.rs

raw symbols SCIP
1 use std::{mem::zeroed, num::NonZeroU64};
2
3 use sysinfo::System;
4 use windows::{
5 Win32::{
6 Foundation::ERROR_SUCCESS,
7 System::Performance::{
8 PDH_FMT_COUNTERVALUE, PDH_FMT_DOUBLE, PDH_HCOUNTER, PDH_HQUERY, PdhAddEnglishCounterW,
9 PdhCloseQuery, PdhCollectQueryData, PdhGetFormattedCounterValue, PdhOpenQueryW,
10 PdhRemoveCounter,
11 },
12 },
13 core::w,
14 };
15
16 use crate::collection::{DataCollector, memory::MemData};
17
18 /// Get swap memory usage on Windows. This does it by using checking Windows'
19 /// performance counters. This is based on the technique done by psutil [here](https://github.com/giampaolo/psutil/pull/2160).
20 ///
21 /// Also see:
22 /// - <https://github.com/GuillaumeGomez/sysinfo/blob/master/src/windows/system.rs>
23 /// - <https://learn.microsoft.com/en-us/windows/win32/api/psapi/ns-psapi-performance_information>
24 /// - <https://en.wikipedia.org/wiki/Commit_charge>.
25 /// - <https://github.com/giampaolo/psutil/issues/2431>
26 /// - <https://github.com/oshi/oshi/issues/1175>
27 /// - <https://github.com/oshi/oshi/issues/1182>
28 pub(crate) fn get_swap_usage(collector: &DataCollector) -> Option<MemData> {
29 let sys = &collector.sys.system;
30 get_swap_usage_inner(sys)
31 }
32
33 fn get_swap_usage_inner(sys: &System) -> Option<MemData> {
34 let total_bytes = NonZeroU64::new(sys.total_swap())?;
35
36 // See https://kennykerr.ca/rust-getting-started/string-tutorial.html
37 let query = w!("\\Paging File(_Total)\\% Usage");
38
39 // SAFETY: Hits a few Windows APIs; this should be safe as we check each step,
40 // and we clean up at the end.
41 unsafe {
42 let mut query_handle: PDH_HQUERY = zeroed();
43 let mut counter_handle: PDH_HCOUNTER = zeroed();
44 let mut counter_value: PDH_FMT_COUNTERVALUE = zeroed();
45
46 if PdhOpenQueryW(None, 0, &mut query_handle) != ERROR_SUCCESS.0 {
47 return None;
48 }
49
50 if PdhAddEnglishCounterW(query_handle, query, 0, &mut counter_handle) != ERROR_SUCCESS.0 {
51 return None;
52 }
53
54 // May fail if swap is disabled.
55 if PdhCollectQueryData(query_handle) != ERROR_SUCCESS.0 {
56 return None;
57 }
58
59 if PdhGetFormattedCounterValue(counter_handle, PDH_FMT_DOUBLE, None, &mut counter_value)
60 != ERROR_SUCCESS.0
61 {
62 // If we fail, still clean up.
63 PdhCloseQuery(query_handle);
64 return None;
65 }
66
67 let use_percentage = counter_value.Anonymous.doubleValue;
68
69 // Cleanup.
70 PdhRemoveCounter(counter_handle);
71 PdhCloseQuery(query_handle);
72
73 let used_bytes = (total_bytes.get() as f64 / 100.0 * use_percentage) as u64;
74 Some(MemData {
75 used_bytes,
76 total_bytes,
77 })
78 }
79 }
80
81 #[cfg(all(target_os = "windows", test))]
82 mod tests {
83 use sysinfo::{MemoryRefreshKind, RefreshKind};
84
85 use super::*;
86
87 #[test]
88 fn test_windows_get_swap_usage() {
89 let sys = System::new_with_specifics(
90 RefreshKind::nothing().with_memory(MemoryRefreshKind::nothing().with_swap()),
91 );
92
93 let swap_usage = get_swap_usage_inner(&sys);
94 if sys.total_swap() > 0 {
95 // Not sure if we can guarantee this to always pass on a machine, so I'll just
96 // print out.
97 println!("swap: {swap_usage:?}");
98 } else {
99 println!("No swap, skipping.");
100 }
101 }
102 }