use std::sync::atomic::{AtomicU64, Ordering};
use super::metric::{MetricKey, MetricKind, MetricState};
use super::registry::MetricPair;
pub const FIBERS_LIVE: &str = "id_effect_fibers_live";
pub const FIBERS_STARTED_TOTAL: &str = "id_effect_fibers_started_total";
static STARTED: AtomicU64 = AtomicU64::new(0);
static LIVE: AtomicU64 = AtomicU64::new(0);
#[inline]
pub(crate) fn fiber_started() {
STARTED.fetch_add(1, Ordering::Relaxed);
LIVE.fetch_add(1, Ordering::Relaxed);
}
#[inline]
pub(crate) fn fiber_finished() {
LIVE.fetch_sub(1, Ordering::Relaxed);
}
pub fn fibers_live() -> u64 {
LIVE.load(Ordering::Relaxed)
}
pub fn fibers_started() -> u64 {
STARTED.load(Ordering::Relaxed)
}
pub fn snapshot() -> Vec<MetricPair> {
vec![
MetricPair {
key: MetricKey::new(FIBERS_LIVE, MetricKind::Gauge),
state: MetricState::Gauge {
value: i64::try_from(fibers_live()).unwrap_or(i64::MAX),
},
},
MetricPair {
key: MetricKey::new(FIBERS_STARTED_TOTAL, MetricKind::Counter),
state: MetricState::Counter {
count: fibers_started(),
},
},
]
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use std::sync::atomic::AtomicUsize;
use std::time::{Duration, Instant};
use super::*;
use crate::{HNil, Never, Runtime, sleep, sync};
#[test]
fn blocked_fibers_are_live_and_counted_as_started() {
const N: u64 = 8;
let started_before = fibers_started();
let running = Arc::new(AtomicUsize::new(0));
let handles: Vec<_> = (0..N)
.map(|_| {
let running = Arc::clone(&running);
Runtime::global().fork(
sync::<_, Never, HNil>(move || {
running.fetch_add(1, Ordering::SeqCst);
})
.then(sleep(Duration::from_secs(3600))),
)
})
.collect();
let deadline = Instant::now() + Duration::from_secs(10);
while running.load(Ordering::SeqCst) < N as usize {
assert!(Instant::now() < deadline, "forked fibers never ran");
std::thread::sleep(Duration::from_millis(1));
}
assert!(fibers_live() >= N, "live {} < {N}", fibers_live());
assert!(fibers_started() >= started_before + N);
let snap = snapshot();
assert_eq!(snap[0].key.name(), FIBERS_LIVE);
assert!(matches!(snap[0].state, MetricState::Gauge { value } if value >= N as i64));
assert_eq!(snap[1].key.name(), FIBERS_STARTED_TOTAL);
for h in &handles {
h.interrupt_now();
}
for h in &handles {
let deadline = Instant::now() + Duration::from_secs(10);
while !h.fiber().is_done() {
assert!(
Instant::now() < deadline,
"interrupted fiber never finished"
);
std::thread::sleep(Duration::from_millis(1));
}
}
}
}