use core::fmt;
use core::time::Duration;
use std::collections::{BTreeMap, BTreeSet};
use tokio::time::Instant;
use crate::proc_table::ProcInstant;
mod os;
pub(crate) use os::StatsSweep;
const SWEEP_POLL_INTERVAL: Duration = Duration::from_millis(100);
const KILL_SETTLE: Duration = Duration::from_secs(1);
pub(crate) const KILL_SETTLE_POLL: Duration = Duration::from_millis(10);
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub(crate) struct LambSnapshot {
seen_at: BTreeMap<u32, ProcInstant>,
}
impl LambSnapshot {
pub(crate) fn new(pids: impl IntoIterator<Item = u32>, taken_at: ProcInstant) -> Self {
Self {
seen_at: pids.into_iter().map(|pid| (pid, taken_at)).collect(),
}
}
pub(crate) fn pids(&self) -> impl Iterator<Item = u32> + '_ {
self.seen_at.keys().copied()
}
#[cfg_attr(all(not(unix), not(test)), expect(dead_code))]
pub(crate) fn seen_at(&self, pid: u32) -> Option<ProcInstant> {
self.seen_at.get(&pid).copied()
}
pub(crate) fn latest(&self) -> Option<ProcInstant> {
self.seen_at.values().copied().max()
}
pub(crate) fn is_empty(&self) -> bool {
self.seen_at.is_empty()
}
#[must_use]
pub(crate) fn merge(mut self, other: Self) -> Self {
for (pid, seen) in other.seen_at {
let at = self.seen_at.entry(pid).or_insert(seen);
*at = (*at).max(seen);
}
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum LambSignal {
Term,
Kill,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum SignalError {
#[cfg_attr(not(unix), expect(dead_code))]
NotALamb(u32),
#[cfg_attr(all(not(unix), not(test)), expect(dead_code))]
Refused(String),
#[cfg(not(unix))]
Unsupported,
}
impl fmt::Display for SignalError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NotALamb(pid) => write!(f, "pid {pid} is never a lamb to signal"),
Self::Refused(reason) => write!(f, "the OS refused the signal: {reason}"),
#[cfg(not(unix))]
Self::Unsupported => f.write_str("this platform cannot signal a single lamb"),
}
}
}
impl core::error::Error for SignalError {}
pub(crate) trait LambSweep: Send + Sync {
fn snapshot(&self, root_pid: u32) -> LambSnapshot;
fn last_snapshot(&self, root_pid: u32) -> Option<LambSnapshot>;
fn survivors(&self, snapshot: &LambSnapshot) -> Vec<u32>;
fn descendants(&self, roots: &[u32]) -> LambSnapshot;
fn signal(&self, pid: u32, signal: LambSignal) -> Result<(), SignalError>;
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub(crate) struct SweepReport {
pub termed: Vec<u32>,
pub killed: Vec<u32>,
}
pub(crate) async fn sweep_lambs(
sweep: &dyn LambSweep,
snapshot: &LambSnapshot,
grace: Duration,
) -> SweepReport {
if snapshot.is_empty() {
return SweepReport::default();
}
let mut snapshot = snapshot.clone();
let mut termed = BTreeSet::new();
let deadline = Instant::now() + grace;
let killed = loop {
let roots = sweep.survivors(&snapshot);
if roots.is_empty() {
break roots;
}
let alive = walk_from(sweep, &mut snapshot, roots);
let first_seen: Vec<u32> = alive
.iter()
.copied()
.filter(|&pid| termed.insert(pid))
.collect();
deliver(sweep, &first_seen, LambSignal::Term);
let remaining = deadline.saturating_duration_since(Instant::now());
if remaining.is_zero() {
break alive;
}
tokio::time::sleep(remaining.min(SWEEP_POLL_INTERVAL)).await;
};
if termed.is_empty() {
return SweepReport::default();
}
let termed: Vec<u32> = termed.into_iter().collect();
tracing::info!(lambs = ?termed, "swept lambs that outlived their sheep");
if !killed.is_empty() {
tracing::warn!(lambs = ?killed, "lambs ignored SIGTERM for the whole grace; sending SIGKILL");
deliver(sweep, &killed, LambSignal::Kill);
let stuck = poll_until_gone(sweep, &snapshot, KILL_SETTLE, KILL_SETTLE_POLL).await;
if !stuck.is_empty() {
tracing::warn!(lambs = ?stuck, "lambs still running after SIGKILL; leaving them");
}
}
SweepReport { termed, killed }
}
fn walk_from(sweep: &dyn LambSweep, snapshot: &mut LambSnapshot, roots: Vec<u32>) -> Vec<u32> {
let born = sweep.descendants(&roots);
if born.is_empty() {
return roots;
}
let still = sweep.survivors(snapshot);
if !roots.iter().all(|root| still.contains(root)) {
tracing::debug!(
?roots,
"a lamb exited during the walk from it; walking again next look"
);
return roots
.into_iter()
.filter(|root| still.contains(root))
.collect();
}
let mut alive = roots;
alive.extend(born.pids());
alive.sort_unstable();
alive.dedup();
*snapshot = core::mem::take(snapshot).merge(born);
alive
}
async fn poll_until_gone(
sweep: &dyn LambSweep,
snapshot: &LambSnapshot,
bound: Duration,
every: Duration,
) -> Vec<u32> {
let deadline = Instant::now() + bound;
loop {
let remaining = deadline.saturating_duration_since(Instant::now());
tokio::time::sleep(remaining.min(every)).await;
let left = sweep.survivors(snapshot);
if left.is_empty() || Instant::now() >= deadline {
break left;
}
}
}
fn deliver(sweep: &dyn LambSweep, pids: &[u32], signal: LambSignal) {
for &pid in pids {
if let Err(error) = sweep.signal(pid, signal) {
tracing::warn!(pid, ?signal, %error, "lamb signal delivery failed");
}
}
}
#[cfg(test)]
mod tests;