| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
use std::sync::{Arc, Mutex}; |
| 29 |
use std::time::{Duration, Instant}; |
| 30 |
|
| 31 |
|
| 32 |
pub(super) struct BlockingProbe { |
| 33 |
samples: Arc<Mutex<Vec<Duration>>>, |
| 34 |
sampler: tokio::task::JoinHandle<()>, |
| 35 |
} |
| 36 |
|
| 37 |
impl BlockingProbe { |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
pub(super) fn start(interval: Duration) -> Self { |
| 43 |
let samples = Arc::new(Mutex::new(Vec::new())); |
| 44 |
let into = Arc::clone(&samples); |
| 45 |
|
| 46 |
let sampler = tokio::spawn(async move { |
| 47 |
loop { |
| 48 |
let queued = Instant::now(); |
| 49 |
|
| 50 |
|
| 51 |
let waited = tokio::task::spawn_blocking(move || queued.elapsed()).await; |
| 52 |
if let Ok(waited) = waited { |
| 53 |
into.lock().unwrap().push(waited); |
| 54 |
} |
| 55 |
tokio::time::sleep(interval).await; |
| 56 |
} |
| 57 |
}); |
| 58 |
|
| 59 |
BlockingProbe { samples, sampler } |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
pub(super) fn finish(self) -> BlockingReport { |
| 64 |
self.sampler.abort(); |
| 65 |
let mut samples = std::mem::take(&mut *self.samples.lock().unwrap()); |
| 66 |
samples.sort(); |
| 67 |
BlockingReport::of(&samples) |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
pub(super) struct BlockingReport { |
| 73 |
pub count: usize, |
| 74 |
pub p50: Duration, |
| 75 |
pub p95: Duration, |
| 76 |
pub p99: Duration, |
| 77 |
pub max: Duration, |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
pub stalls: usize, |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
pub(super) const STALL: Duration = Duration::from_millis(1); |
| 88 |
|
| 89 |
impl BlockingReport { |
| 90 |
|
| 91 |
pub(super) fn print(&self) { |
| 92 |
println!(" Blocking-pool dispatch delay ({} samples):", self.count); |
| 93 |
if self.count == 0 { |
| 94 |
println!(" no samples"); |
| 95 |
println!(); |
| 96 |
return; |
| 97 |
} |
| 98 |
println!( |
| 99 |
" p50 {} p95 {} p99 {} max {}", |
| 100 |
format_dur(self.p50), |
| 101 |
format_dur(self.p95), |
| 102 |
format_dur(self.p99), |
| 103 |
format_dur(self.max), |
| 104 |
); |
| 105 |
println!( |
| 106 |
" stalls over {}: {} ({:.1}%)", |
| 107 |
format_dur(STALL), |
| 108 |
self.stalls, |
| 109 |
self.stalls as f64 / self.count as f64 * 100.0, |
| 110 |
); |
| 111 |
if self.stalls == 0 { |
| 112 |
println!(" the pool had headroom throughout"); |
| 113 |
} |
| 114 |
println!(); |
| 115 |
} |
| 116 |
|
| 117 |
fn of(sorted: &[Duration]) -> Self { |
| 118 |
if sorted.is_empty() { |
| 119 |
return BlockingReport { |
| 120 |
count: 0, |
| 121 |
p50: Duration::ZERO, |
| 122 |
p95: Duration::ZERO, |
| 123 |
p99: Duration::ZERO, |
| 124 |
max: Duration::ZERO, |
| 125 |
stalls: 0, |
| 126 |
}; |
| 127 |
} |
| 128 |
let at = |pct: f64| { |
| 129 |
let idx = ((sorted.len() as f64 * pct) as usize).min(sorted.len() - 1); |
| 130 |
sorted[idx] |
| 131 |
}; |
| 132 |
BlockingReport { |
| 133 |
count: sorted.len(), |
| 134 |
p50: at(0.50), |
| 135 |
p95: at(0.95), |
| 136 |
p99: at(0.99), |
| 137 |
max: sorted[sorted.len() - 1], |
| 138 |
stalls: sorted.iter().filter(|d| **d > STALL).count(), |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
fn format_dur(d: Duration) -> String { |
| 145 |
let us = d.as_micros(); |
| 146 |
if us >= 1000 { |
| 147 |
format!("{:.1}ms", us as f64 / 1000.0) |
| 148 |
} else { |
| 149 |
format!("{us}us") |
| 150 |
} |
| 151 |
} |
| 152 |
|