diff --git a/crates/voelin-gpui/src/meter.rs b/crates/voelin-gpui/src/meter.rs index 1fe6596..10c1775 100644 --- a/crates/voelin-gpui/src/meter.rs +++ b/crates/voelin-gpui/src/meter.rs @@ -272,16 +272,32 @@ mod tests { return; } let a = Snapshot::now().expect("/proc/self"); - // Burn a little CPU on this thread so that the process has some to show. - let started = Instant::now(); - let mut x = 0u64; - while started.elapsed() < Duration::from_millis(60) { - x = x.wrapping_mul(31).wrapping_add(7); - } - std::hint::black_box(x); - let b = Snapshot::now().expect("/proc/self"); - let (cpu, threads) = cpu_between(&a, &b); - assert!(cpu >= 0.0 && b.rss_mib() > 1.0, "{cpu} {}", b.rss_mib()); - assert!(!threads.is_empty()); + // The kernel counts a process's CPU in ticks of 10 ms, and on a busy + // machine this thread gets a tick's worth of CPU only after a long wall + // time: burn CPU until a second reading shows the process's ticks above + // the first, and in a thread group (the process's total and a thread's + // own are rounded separately). The deadline only ends a hang + // (docs/testing/load.md); it never decides the result. + let deadline = Instant::now() + Duration::from_secs(30); + let (b, cpu) = loop { + let burn = Instant::now(); + let mut x = 0u64; + while burn.elapsed() < Duration::from_millis(2) { + x = x.wrapping_mul(31).wrapping_add(7); + } + std::hint::black_box(x); + let b = Snapshot::now().expect("/proc/self"); + let (cpu, threads) = cpu_between(&a, &b); + if b.process > a.process && !threads.is_empty() { + break (b, cpu); + } + assert!( + Instant::now() < deadline, + "no CPU tick showed in 30 s: {} -> {}", + a.process, + b.process + ); + }; + assert!(cpu > 0.0 && b.rss_mib() > 1.0, "{cpu} {}", b.rss_mib()); } }