//! `PressureConfig::state` reified sans-IO: load shedding is a pure classifier //! of a resource sample plus prior shedding state (the hysteresis latch). No //! I/O, no clock — the env read that builds the config stays at the edge //! (`main.rs`). This is the degenerate case of the pattern: a pure decision //! with no ports and no effects, included to prove the seam is uniform. It is //! the extraction target for `main.rs`'s private `PressureConfig`. /// Watermarks and ceilings. Built once from the environment by the caller; /// the core never reads the environment itself. #[derive(Clone, Copy)] pub struct PressureConfig { pub resident_high: Option, pub resident_low: usize, pub rss_high_bytes: Option, pub cpu_high_x100: Option, } /// A resource sample — the only input the classifier reads. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct Load { pub resident_cells: usize, pub rss_bytes: u64, pub cpu_percent_x100: u64, } #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct PressureState { pub admission_blocked: bool, pub shedding: bool, pub trigger: Option<&'static str>, } impl PressureConfig { /// The first ceiling the sample crosses, or `None`. pub fn trigger(self, s: Load) -> Option<&'static str> { if self.resident_high.is_some_and(|h| s.resident_cells >= h) { return Some("resident_cells"); } if self.rss_high_bytes.is_some_and(|h| s.rss_bytes >= h) { return Some("rss"); } if self.cpu_high_x100.is_some_and(|h| s.cpu_percent_x100 >= h) { return Some("cpu"); } None } /// Back under every low watermark (80% of each high) — the latch release. pub fn relieved(self, s: Load) -> bool { let resident_ok = self.resident_high.is_none_or(|_| s.resident_cells <= self.resident_low); let rss_ok = self .rss_high_bytes .is_none_or(|h| s.rss_bytes <= h.saturating_mul(4) / 5); let cpu_ok = self .cpu_high_x100 .is_none_or(|h| s.cpu_percent_x100 <= h.saturating_mul(4) / 5); resident_ok && rss_ok && cpu_ok } /// How far to shed down to for a given trigger. pub fn release_target(self, resident_cells: usize, reason: &str) -> usize { if reason == "resident_cells" { self.resident_low } else { resident_cells.saturating_sub((resident_cells / 10).max(1)) } } /// The resource whose low watermark still holds the shedding latch. An /// instantaneous trigger wins; inside a hysteresis band, preserve the /// resource-specific reason until every configured low watermark clears. pub fn shedding_trigger( self, s: Load, was_shedding: bool, ) -> Option<&'static str> { if let Some(trigger) = self.trigger(s) { return Some(trigger); } if !was_shedding || self.relieved(s) { return None; } if self.resident_high.is_some() && s.resident_cells > self.resident_low { return Some("resident_cells"); } if self .rss_high_bytes .is_some_and(|h| s.rss_bytes > h.saturating_mul(4) / 5) { return Some("rss"); } if self .cpu_high_x100 .is_some_and(|h| s.cpu_percent_x100 > h.saturating_mul(4) / 5) { return Some("cpu"); } None } /// The transition: `was_shedding` is the only carried state — the latch /// that keeps shedding between the high crossing and the low release. pub fn state(self, s: Load, was_shedding: bool) -> PressureState { let trigger = self.trigger(s); let admission_blocked = trigger.is_some(); let shedding = self.shedding_trigger(s, was_shedding).is_some(); PressureState { admission_blocked, shedding, trigger } } } /// Admission order for cells waiting on resident capacity. /// /// A hibernated cell whose node is at its resident ceiling must wait for a /// slot. celld's executor waits on a `Notify`: every waiter wakes on each /// release and races to reserve. That is unfair by construction, and the /// 2026-08-02 working-set pilot measured the consequence — 13 requests /// timed out after 20 s while roughly 2,336 slots were freed around them, /// at 117 evictions/s. They never ran out of capacity; they lost every race. /// /// Admission is therefore a QUEUE decision, and a pure one: given the /// waiters in arrival order and the slots available, who is admitted? #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct Waiter { /// Arrival tick — smaller is older. pub arrived_at: u64, pub id: u64, } /// Admit up to `slots` waiters, oldest first. Returns their ids. /// /// FIFO is the whole policy. It costs nothing over a race and converts /// "eventually, probably" into a bound: a waiter with `k` waiters ahead of /// it is admitted within `k` releases, so no arrival pattern can starve it. pub fn admit(waiters: &[Waiter], slots: usize) -> Vec { let mut order: Vec<&Waiter> = waiters.iter().collect(); order.sort_by_key(|w| (w.arrived_at, w.id)); order.into_iter().take(slots).map(|w| w.id).collect() } /// May one more cell be admitted to residency right now? /// /// Two independent facts gate admission, and conflating them is what turned /// the 2,000-ring lab workload into a cliff rather than a plateau. /// /// `sampled_pressure` is the hysteresis latch from [`PressureConfig::state`]: /// a periodic view of RSS, CPU, and residency, and the only thing entitled to /// say "this node is overloaded". The headroom test below is the other, and it /// is instantaneous. /// /// A request that merely lost a race for the last slot has learned the second /// fact, not the first. If such a waiter is allowed to assert node-wide /// pressure, every other cell on the node is refused until the next sample — /// including cells that would fit — and the waiters keep the latch hot for one /// another. Capacity that exists then goes unused for a full sampling period. pub fn may_admit( owned: usize, pending: usize, resident_high: Option, sampled_pressure: bool, ) -> bool { if sampled_pressure { return false; } resident_high.is_none_or(|high| owned.saturating_add(pending) < high) }