Skip to content

Commit ca7d1d0

Browse files
committed
fix(memory): report Windows working set and private commit (#1932)
1 parent 49ce5f4 commit ca7d1d0

2 files changed

Lines changed: 109 additions & 8 deletions

File tree

‎crates/jcode-base/Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ embeddings = ["dep:jcode-embedding"]
148148
bedrock = ["jcode-provider-bedrock/aws-sdk"]
149149

150150
[target.'cfg(windows)'.dependencies]
151-
windows-sys = { version = "0.59", features = ["Win32_Foundation", "Win32_System_Power", "Win32_System_Registry", "Win32_System_SystemServices", "Win32_System_Threading"] }
151+
windows-sys = { version = "0.59", features = ["Win32_Foundation", "Win32_System_Power", "Win32_System_ProcessStatus", "Win32_System_Registry", "Win32_System_SystemServices", "Win32_System_Threading"] }
152152

153153
[target.'cfg(target_os = "macos")'.dependencies]
154154
global-hotkey = "0.7"

‎crates/jcode-base/src/process_memory.rs‎

Lines changed: 108 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ struct JemallocProfilingMibs {
3232
pub struct ProcessMemorySnapshot {
3333
pub rss_bytes: Option<u64>,
3434
pub peak_rss_bytes: Option<u64>,
35+
/// Total virtual address space size, when available. Not private commit.
3536
pub virtual_bytes: Option<u64>,
3637
/// Number of OS threads (`Threads:` in `/proc/self/status`).
3738
#[serde(skip_serializing_if = "Option::is_none")]
@@ -47,6 +48,10 @@ pub struct ProcessMemorySnapshot {
4748

4849
#[derive(Debug, Clone, Default, Serialize)]
4950
pub struct OsProcessMemoryInfo {
51+
/// Windows private committed memory (`PROCESS_MEMORY_COUNTERS_EX.PrivateUsage`).
52+
/// Includes nonresident memory and is not virtual address space size or RSS.
53+
#[serde(skip_serializing_if = "Option::is_none")]
54+
pub private_commit_bytes: Option<u64>,
5055
pub pss_bytes: Option<u64>,
5156
/// Proportional set size of anonymous mappings (`Pss_Anon:` in
5257
/// smaps_rollup): heap + thread stacks + other private anon memory.
@@ -135,12 +140,6 @@ fn memory_history() -> &'static Mutex<VecDeque<ProcessMemoryHistoryEntry>> {
135140
MEMORY_HISTORY.get_or_init(|| Mutex::new(VecDeque::with_capacity(MAX_HISTORY_SAMPLES)))
136141
}
137142

138-
#[cfg(target_os = "linux")]
139-
pub fn snapshot() -> ProcessMemorySnapshot {
140-
snapshot_with_source("snapshot")
141-
}
142-
143-
#[cfg(not(target_os = "linux"))]
144143
pub fn snapshot() -> ProcessMemorySnapshot {
145144
snapshot_with_source("snapshot")
146145
}
@@ -177,7 +176,59 @@ pub fn snapshot_with_source(source: impl Into<String>) -> ProcessMemorySnapshot
177176
snapshot
178177
}
179178

180-
#[cfg(not(target_os = "linux"))]
179+
#[cfg(windows)]
180+
pub fn snapshot_with_source(source: impl Into<String>) -> ProcessMemorySnapshot {
181+
use windows_sys::Win32::System::ProcessStatus::{
182+
GetProcessMemoryInfo, PROCESS_MEMORY_COUNTERS, PROCESS_MEMORY_COUNTERS_EX,
183+
};
184+
use windows_sys::Win32::System::Threading::GetCurrentProcess;
185+
186+
let source = source.into();
187+
// SAFETY: This C structure contains only integer fields, so zero is valid.
188+
let mut counters: PROCESS_MEMORY_COUNTERS_EX = unsafe { std::mem::zeroed() };
189+
counters.cb = std::mem::size_of::<PROCESS_MEMORY_COUNTERS_EX>() as u32;
190+
// SAFETY: GetCurrentProcess returns a borrowed pseudo-handle (do not close it).
191+
// The API accepts an EX structure through the base pointer, with its full size.
192+
let success = unsafe {
193+
GetProcessMemoryInfo(
194+
GetCurrentProcess(),
195+
(&mut counters as *mut PROCESS_MEMORY_COUNTERS_EX).cast::<PROCESS_MEMORY_COUNTERS>(),
196+
counters.cb,
197+
)
198+
};
199+
let snapshot = if success != 0 {
200+
windows_memory_snapshot(
201+
counters.WorkingSetSize as u64,
202+
counters.PeakWorkingSetSize as u64,
203+
counters.PrivateUsage as u64,
204+
)
205+
} else {
206+
let error = std::io::Error::last_os_error();
207+
logging::warn(&format!(
208+
"process memory snapshot source={source} GetProcessMemoryInfo failed: {error}"
209+
));
210+
ProcessMemorySnapshot::default()
211+
};
212+
record_snapshot(source, snapshot.clone());
213+
snapshot
214+
}
215+
216+
#[cfg(any(windows, test))]
217+
fn windows_memory_snapshot(rss: u64, peak_rss: u64, private_commit: u64) -> ProcessMemorySnapshot {
218+
ProcessMemorySnapshot {
219+
rss_bytes: Some(rss),
220+
peak_rss_bytes: Some(peak_rss),
221+
// GetProcessMemoryInfo reports private commit, not total virtual space.
222+
virtual_bytes: None,
223+
os: Some(OsProcessMemoryInfo {
224+
private_commit_bytes: Some(private_commit),
225+
..OsProcessMemoryInfo::default()
226+
}),
227+
..ProcessMemorySnapshot::default()
228+
}
229+
}
230+
231+
#[cfg(not(any(target_os = "linux", windows)))]
181232
pub fn snapshot_with_source(source: impl Into<String>) -> ProcessMemorySnapshot {
182233
let source = source.into();
183234
logging::debug(&format!(
@@ -693,6 +744,7 @@ fn record_snapshot(source: String, snapshot: ProcessMemorySnapshot) {
693744
fn read_linux_memory_info(status: &str) -> Option<OsProcessMemoryInfo> {
694745
let smaps = std::fs::read_to_string("/proc/self/smaps_rollup").ok();
695746
let info = OsProcessMemoryInfo {
747+
private_commit_bytes: None,
696748
pss_bytes: smaps
697749
.as_deref()
698750
.and_then(|text| parse_proc_value_bytes(text, "Pss:")),
@@ -929,6 +981,55 @@ fn parse_proc_value_bytes(status: &str, key: &str) -> Option<u64> {
929981
mod tests {
930982
use super::*;
931983

984+
#[test]
985+
fn windows_memory_metrics_keep_private_commit_separate() {
986+
let snapshot = windows_memory_snapshot(4096, 8192, 16384);
987+
assert_eq!(snapshot.rss_bytes, Some(4096));
988+
assert_eq!(snapshot.peak_rss_bytes, Some(8192));
989+
assert_eq!(snapshot.virtual_bytes, None);
990+
assert_eq!(snapshot.thread_count, None);
991+
assert_eq!(snapshot.main_stack_bytes, None);
992+
let os = snapshot.os.as_ref().expect("Windows memory details");
993+
assert_eq!(os.private_commit_bytes, Some(16384));
994+
assert_eq!(os.private_dirty_bytes, None);
995+
assert_eq!(os.pss_bytes, None);
996+
let json = serde_json::to_value(&snapshot).unwrap();
997+
assert_eq!(json["os"]["private_commit_bytes"], 16384);
998+
assert!(json["virtual_bytes"].is_null());
999+
}
1000+
1001+
#[cfg(windows)]
1002+
#[test]
1003+
fn windows_native_snapshot_reports_working_set_and_records_history() {
1004+
let source = "windows_native_memory_smoke";
1005+
let snapshot = snapshot_with_source(source);
1006+
let rss = snapshot.rss_bytes.expect("current working set");
1007+
assert!(rss > 0);
1008+
assert!(snapshot.peak_rss_bytes.expect("peak working set") >= rss);
1009+
assert!(snapshot.os.as_ref().unwrap().private_commit_bytes.unwrap() > 0);
1010+
assert_eq!(snapshot.virtual_bytes, None);
1011+
assert!(history(MAX_HISTORY_SAMPLES).iter().any(|entry| {
1012+
entry.source == source && entry.snapshot.rss_bytes == snapshot.rss_bytes
1013+
}));
1014+
}
1015+
1016+
#[cfg(target_os = "linux")]
1017+
#[test]
1018+
fn linux_snapshot_preserves_resident_virtual_and_history_metrics() {
1019+
let source = "linux_memory_regression";
1020+
let snapshot = snapshot_with_source(source);
1021+
assert!(snapshot.rss_bytes.expect("VmRSS") > 0);
1022+
assert!(snapshot.peak_rss_bytes.expect("VmHWM") >= snapshot.rss_bytes.unwrap());
1023+
assert!(snapshot.virtual_bytes.expect("VmSize") >= snapshot.rss_bytes.unwrap());
1024+
let os = snapshot.os.as_ref().expect("Linux memory details");
1025+
assert_eq!(os.private_commit_bytes, None);
1026+
let json = serde_json::to_value(&snapshot).unwrap();
1027+
assert!(json["os"].get("private_commit_bytes").is_none());
1028+
assert!(history(MAX_HISTORY_SAMPLES).iter().any(|entry| {
1029+
entry.source == source && entry.snapshot.virtual_bytes == snapshot.virtual_bytes
1030+
}));
1031+
}
1032+
9321033
#[test]
9331034
fn estimate_json_bytes_matches_serialized_length_without_buffering_output() {
9341035
let value = serde_json::json!({

0 commit comments

Comments
 (0)