| 1020 | /// mark, including peaks that occur between our explicit samples. |
| 1021 | #[cfg(any(target_os = "macos", target_os = "linux"))] |
| 1022 | fn rolling_memory_sample( |
| 1023 | phase: &str, |
| 1024 | processed: usize, |
| 1025 | loaded: usize, |
| 1026 | queued: usize, |
| 1027 | reserved: usize, |
| 1028 | ) -> CliResult<()> { |
| 1029 | let Some(path) = std::env::var_os("ATOMIC_ROLLING_MEMORY_LOG") else { |
| 1030 | return Ok(()); |
| 1031 | }; |
| 1032 | let output = std::process::Command::new("ps") |
| 1033 | .args(["-o", "rss=", "-p", &std::process::id().to_string()]) |
| 1034 | .output() |
| 1035 | .map_err(|e| git_error(format!("cannot sample RSS: {e}")))?; |
| 1036 | if !output.status.success() { |
| 1037 | return Err(git_error("ps failed to sample rolling process RSS")); |
| 1038 | } |
| 1039 | let rss_kib: u64 = String::from_utf8_lossy(&output.stdout) |
| 1040 | .trim() |
| 1041 | .parse() |
| 1042 | .map_err(|e| git_error(format!("invalid ps RSS: {e}")))?; |
| 1043 | let mut usage = std::mem::MaybeUninit::<libc::rusage>::uninit(); |
| 1044 | // SAFETY: getrusage initializes the provided rusage on success. |
| 1045 | if unsafe { libc::getrusage(libc::RUSAGE_SELF, usage.as_mut_ptr()) } != 0 { |
| 1046 | return Err(git_error(format!( |
| 1047 | "getrusage failed: {}", |
| 1048 | std::io::Error::last_os_error() |
| 1049 | ))); |
| 1050 | } |
| 1051 | // SAFETY: the successful call above initialized this value. |
| 1052 | let peak_raw = unsafe { usage.assume_init() }.ru_maxrss as u64; |
| 1053 | #[cfg(target_os = "macos")] |
| 1054 | let peak_rss_bytes = peak_raw; |
| 1055 | #[cfg(target_os = "linux")] |
| 1056 | let peak_rss_bytes = peak_raw * 1024; |
| 1057 | let sample = serde_json::json!({ |
| 1058 | "phase": phase, "processed": processed, "loaded": loaded, |
| 1059 | "queued": queued, "reserved_blob_bytes": reserved, |
| 1060 | "rss_bytes": rss_kib * 1024, "peak_rss_bytes": peak_rss_bytes, |
| 1061 | }); |
| 1062 | use std::io::Write; |
| 1063 | let mut file = fs::OpenOptions::new() |
| 1064 | .create(true) |
| 1065 | .append(true) |
| 1066 | .open(path) |
| 1067 | .map_err(|e| git_error(format!("cannot open memory log: {e}")))?; |
| 1068 | writeln!(file, "{sample}") |
| 1069 | .map_err(|e| git_error(format!("cannot write memory log: {e}")))?; |
| 1070 | if std::env::var_os("ATOMIC_ROLLING_DASHBOARD").is_none() { |
| 1071 | println!("ROLLING MEMORY {sample}"); |
| 1072 | } |
| 1073 | Ok(()) |
| 1074 | } |
| 1075 | |
| 1076 | #[cfg(not(any(target_os = "macos", target_os = "linux")))] |
| 1077 | fn rolling_memory_sample( |