Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/synapse/blob/main/crates/synapse-bench/src/rusage.rs # synapse A component of the AienOS ecosystem. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `synapse / crates / synapse-bench / src / rusage.rs` 118 lines ยท 4152 bytes Raw Blame History 1 //! CPU time and peak RSS attribution via `getrusage(2)`. 2 //! 3 //! `RUSAGE_SELF` covers every thread of the arm process. `RUSAGE_CHILDREN` covers 4 //! only children that have terminated and been waited for (every `git` spawn in 5 //! this harness is waited for). CPU times are deltas over the measured window and 6 //! can be summed. `ru_maxrss` is a lifetime high-water mark: for `RUSAGE_CHILDREN` 7 //! it is the RSS of the single largest waited-for child, never a sum, so self and 8 //! child peaks are reported separately and never added or subtracted. 9 10 use serde::{Deserialize, Serialize}; 11 12 /// One `getrusage` reading. 13 #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)] 14 pub struct RusageSample { 15 pub user_us: u64, 16 pub sys_us: u64, 17 /// Linux reports `ru_maxrss` in kibibytes. 18 pub maxrss_kib: u64, 19 } 20 21 fn sample(who: libc::c_int) -> Result { 22 // SAFETY: `rusage` is a plain C struct; an all-zero bit pattern is valid. 23 let mut ru: libc::rusage = unsafe { std::mem::zeroed() }; 24 // SAFETY: `ru` is a valid, exclusively borrowed `rusage` for the kernel to fill. 25 let rc = unsafe { libc::getrusage(who, &mut ru) }; 26 if rc != 0 { 27 return Err(format!( 28 "getrusage failed: {}", 29 std::io::Error::last_os_error() 30 )); 31 } 32 let tv = |t: libc::timeval| (t.tv_sec as u64) * 1_000_000 + t.tv_usec as u64; 33 Ok(RusageSample { 34 user_us: tv(ru.ru_utime), 35 sys_us: tv(ru.ru_stime), 36 maxrss_kib: ru.ru_maxrss as u64, 37 }) 38 } 39 40 pub fn sample_self() -> Result { 41 sample(libc::RUSAGE_SELF) 42 } 43 44 pub fn sample_children() -> Result { 45 sample(libc::RUSAGE_CHILDREN) 46 } 47 48 /// CPU and memory attribution for one measured window of an arm process. 49 #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] 50 pub struct CpuUsage { 51 pub self_user_ms: f64, 52 pub self_sys_ms: f64, 53 pub children_user_ms: f64, 54 pub children_sys_ms: f64, 55 /// Self plus waited-children CPU time (user + system) inside the window. 56 pub total_cpu_ms: f64, 57 /// Lifetime peak RSS of the arm process (includes setup, for example the 58 /// in-memory base tree held by the Synapse service). 59 pub peak_rss_self_kib: u64, 60 /// Peak RSS of the largest single waited-for child process (not a sum). 61 pub peak_rss_largest_child_kib: u64, 62 } 63 64 /// Start/finish pair for a measured window. 65 #[derive(Debug, Clone, Copy)] 66 pub struct RusageWindow { 67 self_start: RusageSample, 68 children_start: RusageSample, 69 } 70 71 impl RusageWindow { 72 pub fn start() -> Result { 73 Ok(Self { 74 self_start: sample_self()?, 75 children_start: sample_children()?, 76 }) 77 } 78 79 pub fn finish(&self) -> Result { 80 let s = sample_self()?; 81 let c = sample_children()?; 82 let ms = |a: u64, b: u64| crate::stats::round3(a.saturating_sub(b) as f64 / 1000.0); 83 let self_user_ms = ms(s.user_us, self.self_start.user_us); 84 let self_sys_ms = ms(s.sys_us, self.self_start.sys_us); 85 let children_user_ms = ms(c.user_us, self.children_start.user_us); 86 let children_sys_ms = ms(c.sys_us, self.children_start.sys_us); 87 Ok(CpuUsage { 88 self_user_ms, 89 self_sys_ms, 90 children_user_ms, 91 children_sys_ms, 92 total_cpu_ms: crate::stats::round3( 93 self_user_ms + self_sys_ms + children_user_ms + children_sys_ms, 94 ), 95 peak_rss_self_kib: s.maxrss_kib, 96 peak_rss_largest_child_kib: c.maxrss_kib, 97 }) 98 } 99 } 100 101 #[cfg(test)] 102 mod tests { 103 use super::*; 104 105 #[test] 106 fn window_counts_waited_child_cpu() { 107 let w = RusageWindow::start().unwrap(); 108 let status = std::process::Command::new("git") 109 .arg("--version") 110 .output() 111 .unwrap(); 112 assert!(status.status.success()); 113 let usage = w.finish().unwrap(); 114 assert!(usage.peak_rss_self_kib > 0); 115 assert!(usage.peak_rss_largest_child_kib > 0); 116 assert!(usage.total_cpu_ms >= 0.0); 117 } 118 }